[PATCH] Kprobes: rearrange preempt_disable/enable() calls
[pandora-kernel.git] / arch / sparc64 / kernel / kprobes.c
1 /* arch/sparc64/kernel/kprobes.c
2  *
3  * Copyright (C) 2004 David S. Miller <davem@davemloft.net>
4  */
5
6 #include <linux/config.h>
7 #include <linux/kernel.h>
8 #include <linux/kprobes.h>
9 #include <asm/kdebug.h>
10 #include <asm/signal.h>
11 #include <asm/cacheflush.h>
12
13 /* We do not have hardware single-stepping on sparc64.
14  * So we implement software single-stepping with breakpoint
15  * traps.  The top-level scheme is similar to that used
16  * in the x86 kprobes implementation.
17  *
18  * In the kprobe->ainsn.insn[] array we store the original
19  * instruction at index zero and a break instruction at
20  * index one.
21  *
22  * When we hit a kprobe we:
23  * - Run the pre-handler
24  * - Remember "regs->tnpc" and interrupt level stored in
25  *   "regs->tstate" so we can restore them later
26  * - Disable PIL interrupts
27  * - Set regs->tpc to point to kprobe->ainsn.insn[0]
28  * - Set regs->tnpc to point to kprobe->ainsn.insn[1]
29  * - Mark that we are actively in a kprobe
30  *
31  * At this point we wait for the second breakpoint at
32  * kprobe->ainsn.insn[1] to hit.  When it does we:
33  * - Run the post-handler
34  * - Set regs->tpc to "remembered" regs->tnpc stored above,
35  *   restore the PIL interrupt level in "regs->tstate" as well
36  * - Make any adjustments necessary to regs->tnpc in order
37  *   to handle relative branches correctly.  See below.
38  * - Mark that we are no longer actively in a kprobe.
39  */
40
41 int __kprobes arch_prepare_kprobe(struct kprobe *p)
42 {
43         return 0;
44 }
45
46 void __kprobes arch_copy_kprobe(struct kprobe *p)
47 {
48         p->ainsn.insn[0] = *p->addr;
49         p->ainsn.insn[1] = BREAKPOINT_INSTRUCTION_2;
50         p->opcode = *p->addr;
51 }
52
53 void __kprobes arch_arm_kprobe(struct kprobe *p)
54 {
55         *p->addr = BREAKPOINT_INSTRUCTION;
56         flushi(p->addr);
57 }
58
59 void __kprobes arch_disarm_kprobe(struct kprobe *p)
60 {
61         *p->addr = p->opcode;
62         flushi(p->addr);
63 }
64
65 void __kprobes arch_remove_kprobe(struct kprobe *p)
66 {
67 }
68
69 static struct kprobe *current_kprobe;
70 static unsigned long current_kprobe_orig_tnpc;
71 static unsigned long current_kprobe_orig_tstate_pil;
72 static unsigned int kprobe_status;
73 static struct kprobe *kprobe_prev;
74 static unsigned long kprobe_orig_tnpc_prev;
75 static unsigned long kprobe_orig_tstate_pil_prev;
76 static unsigned int kprobe_status_prev;
77
78 static inline void save_previous_kprobe(void)
79 {
80         kprobe_status_prev = kprobe_status;
81         kprobe_orig_tnpc_prev = current_kprobe_orig_tnpc;
82         kprobe_orig_tstate_pil_prev = current_kprobe_orig_tstate_pil;
83         kprobe_prev = current_kprobe;
84 }
85
86 static inline void restore_previous_kprobe(void)
87 {
88         kprobe_status = kprobe_status_prev;
89         current_kprobe_orig_tnpc = kprobe_orig_tnpc_prev;
90         current_kprobe_orig_tstate_pil = kprobe_orig_tstate_pil_prev;
91         current_kprobe = kprobe_prev;
92 }
93
94 static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs)
95 {
96         current_kprobe_orig_tnpc = regs->tnpc;
97         current_kprobe_orig_tstate_pil = (regs->tstate & TSTATE_PIL);
98         current_kprobe = p;
99 }
100
101 static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
102 {
103         regs->tstate |= TSTATE_PIL;
104
105         /*single step inline, if it a breakpoint instruction*/
106         if (p->opcode == BREAKPOINT_INSTRUCTION) {
107                 regs->tpc = (unsigned long) p->addr;
108                 regs->tnpc = current_kprobe_orig_tnpc;
109         } else {
110                 regs->tpc = (unsigned long) &p->ainsn.insn[0];
111                 regs->tnpc = (unsigned long) &p->ainsn.insn[1];
112         }
113 }
114
115 static int __kprobes kprobe_handler(struct pt_regs *regs)
116 {
117         struct kprobe *p;
118         void *addr = (void *) regs->tpc;
119         int ret = 0;
120
121         if (kprobe_running()) {
122                 /* We *are* holding lock here, so this is safe.
123                  * Disarm the probe we just hit, and ignore it.
124                  */
125                 p = get_kprobe(addr);
126                 if (p) {
127                         if (kprobe_status == KPROBE_HIT_SS) {
128                                 regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
129                                         current_kprobe_orig_tstate_pil);
130                                 unlock_kprobes();
131                                 goto no_kprobe;
132                         }
133                         /* We have reentered the kprobe_handler(), since
134                          * another probe was hit while within the handler.
135                          * We here save the original kprobes variables and
136                          * just single step on the instruction of the new probe
137                          * without calling any user handlers.
138                          */
139                         save_previous_kprobe();
140                         set_current_kprobe(p, regs);
141                         p->nmissed++;
142                         kprobe_status = KPROBE_REENTER;
143                         prepare_singlestep(p, regs);
144                         return 1;
145                 } else {
146                         p = current_kprobe;
147                         if (p->break_handler && p->break_handler(p, regs))
148                                 goto ss_probe;
149                 }
150                 /* If it's not ours, can't be delete race, (we hold lock). */
151                 goto no_kprobe;
152         }
153
154         lock_kprobes();
155         p = get_kprobe(addr);
156         if (!p) {
157                 unlock_kprobes();
158                 if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) {
159                         /*
160                          * The breakpoint instruction was removed right
161                          * after we hit it.  Another cpu has removed
162                          * either a probepoint or a debugger breakpoint
163                          * at this address.  In either case, no further
164                          * handling of this interrupt is appropriate.
165                          */
166                         ret = 1;
167                 }
168                 /* Not one of ours: let kernel handle it */
169                 goto no_kprobe;
170         }
171
172         /*
173          * This preempt_disable() matches the preempt_enable_no_resched()
174          * in post_kprobes_handler()
175          */
176         preempt_disable();
177         set_current_kprobe(p, regs);
178         kprobe_status = KPROBE_HIT_ACTIVE;
179         if (p->pre_handler && p->pre_handler(p, regs))
180                 return 1;
181
182 ss_probe:
183         prepare_singlestep(p, regs);
184         kprobe_status = KPROBE_HIT_SS;
185         return 1;
186
187 no_kprobe:
188         return ret;
189 }
190
191 /* If INSN is a relative control transfer instruction,
192  * return the corrected branch destination value.
193  *
194  * The original INSN location was REAL_PC, it actually
195  * executed at PC and produced destination address NPC.
196  */
197 static unsigned long __kprobes relbranch_fixup(u32 insn, unsigned long real_pc,
198                                                unsigned long pc,
199                                                unsigned long npc)
200 {
201         /* Branch not taken, no mods necessary.  */
202         if (npc == pc + 0x4UL)
203                 return real_pc + 0x4UL;
204
205         /* The three cases are call, branch w/prediction,
206          * and traditional branch.
207          */
208         if ((insn & 0xc0000000) == 0x40000000 ||
209             (insn & 0xc1c00000) == 0x00400000 ||
210             (insn & 0xc1c00000) == 0x00800000) {
211                 /* The instruction did all the work for us
212                  * already, just apply the offset to the correct
213                  * instruction location.
214                  */
215                 return (real_pc + (npc - pc));
216         }
217
218         return real_pc + 0x4UL;
219 }
220
221 /* If INSN is an instruction which writes it's PC location
222  * into a destination register, fix that up.
223  */
224 static void __kprobes retpc_fixup(struct pt_regs *regs, u32 insn,
225                                   unsigned long real_pc)
226 {
227         unsigned long *slot = NULL;
228
229         /* Simplest cast is call, which always uses %o7 */
230         if ((insn & 0xc0000000) == 0x40000000) {
231                 slot = &regs->u_regs[UREG_I7];
232         }
233
234         /* Jmpl encodes the register inside of the opcode */
235         if ((insn & 0xc1f80000) == 0x81c00000) {
236                 unsigned long rd = ((insn >> 25) & 0x1f);
237
238                 if (rd <= 15) {
239                         slot = &regs->u_regs[rd];
240                 } else {
241                         /* Hard case, it goes onto the stack. */
242                         flushw_all();
243
244                         rd -= 16;
245                         slot = (unsigned long *)
246                                 (regs->u_regs[UREG_FP] + STACK_BIAS);
247                         slot += rd;
248                 }
249         }
250         if (slot != NULL)
251                 *slot = real_pc;
252 }
253
254 /*
255  * Called after single-stepping.  p->addr is the address of the
256  * instruction whose first byte has been replaced by the breakpoint
257  * instruction.  To avoid the SMP problems that can occur when we
258  * temporarily put back the original opcode to single-step, we
259  * single-stepped a copy of the instruction.  The address of this
260  * copy is p->ainsn.insn.
261  *
262  * This function prepares to return from the post-single-step
263  * breakpoint trap.
264  */
265 static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
266 {
267         u32 insn = p->ainsn.insn[0];
268
269         regs->tpc = current_kprobe_orig_tnpc;
270         regs->tnpc = relbranch_fixup(insn,
271                                      (unsigned long) p->addr,
272                                      (unsigned long) &p->ainsn.insn[0],
273                                      regs->tnpc);
274         retpc_fixup(regs, insn, (unsigned long) p->addr);
275
276         regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
277                         current_kprobe_orig_tstate_pil);
278 }
279
280 static inline int post_kprobe_handler(struct pt_regs *regs)
281 {
282         if (!kprobe_running())
283                 return 0;
284
285         if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) {
286                 kprobe_status = KPROBE_HIT_SSDONE;
287                 current_kprobe->post_handler(current_kprobe, regs, 0);
288         }
289
290         resume_execution(current_kprobe, regs);
291
292         /*Restore back the original saved kprobes variables and continue. */
293         if (kprobe_status == KPROBE_REENTER) {
294                 restore_previous_kprobe();
295                 goto out;
296         }
297         unlock_kprobes();
298 out:
299         preempt_enable_no_resched();
300
301         return 1;
302 }
303
304 /* Interrupts disabled, kprobe_lock held. */
305 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
306 {
307         if (current_kprobe->fault_handler
308             && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
309                 return 1;
310
311         if (kprobe_status & KPROBE_HIT_SS) {
312                 resume_execution(current_kprobe, regs);
313
314                 unlock_kprobes();
315                 preempt_enable_no_resched();
316         }
317         return 0;
318 }
319
320 /*
321  * Wrapper routine to for handling exceptions.
322  */
323 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
324                                        unsigned long val, void *data)
325 {
326         struct die_args *args = (struct die_args *)data;
327         int ret = NOTIFY_DONE;
328
329         preempt_disable();
330         switch (val) {
331         case DIE_DEBUG:
332                 if (kprobe_handler(args->regs))
333                         ret = NOTIFY_STOP;
334                 break;
335         case DIE_DEBUG_2:
336                 if (post_kprobe_handler(args->regs))
337                         ret = NOTIFY_STOP;
338                 break;
339         case DIE_GPF:
340         case DIE_PAGE_FAULT:
341                 if (kprobe_running() &&
342                     kprobe_fault_handler(args->regs, args->trapnr))
343                         ret = NOTIFY_STOP;
344                 break;
345         default:
346                 break;
347         }
348         preempt_enable();
349         return ret;
350 }
351
352 asmlinkage void __kprobes kprobe_trap(unsigned long trap_level,
353                                       struct pt_regs *regs)
354 {
355         BUG_ON(trap_level != 0x170 && trap_level != 0x171);
356
357         if (user_mode(regs)) {
358                 local_irq_enable();
359                 bad_trap(regs, trap_level);
360                 return;
361         }
362
363         /* trap_level == 0x170 --> ta 0x70
364          * trap_level == 0x171 --> ta 0x71
365          */
366         if (notify_die((trap_level == 0x170) ? DIE_DEBUG : DIE_DEBUG_2,
367                        (trap_level == 0x170) ? "debug" : "debug_2",
368                        regs, 0, trap_level, SIGTRAP) != NOTIFY_STOP)
369                 bad_trap(regs, trap_level);
370 }
371
372 /* Jprobes support.  */
373 static struct pt_regs jprobe_saved_regs;
374 static struct pt_regs *jprobe_saved_regs_location;
375 static struct sparc_stackf jprobe_saved_stack;
376
377 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
378 {
379         struct jprobe *jp = container_of(p, struct jprobe, kp);
380
381         jprobe_saved_regs_location = regs;
382         memcpy(&jprobe_saved_regs, regs, sizeof(*regs));
383
384         /* Save a whole stack frame, this gets arguments
385          * pushed onto the stack after using up all the
386          * arg registers.
387          */
388         memcpy(&jprobe_saved_stack,
389                (char *) (regs->u_regs[UREG_FP] + STACK_BIAS),
390                sizeof(jprobe_saved_stack));
391
392         regs->tpc  = (unsigned long) jp->entry;
393         regs->tnpc = ((unsigned long) jp->entry) + 0x4UL;
394         regs->tstate |= TSTATE_PIL;
395
396         return 1;
397 }
398
399 void __kprobes jprobe_return(void)
400 {
401         __asm__ __volatile__(
402                 ".globl jprobe_return_trap_instruction\n"
403 "jprobe_return_trap_instruction:\n\t"
404                 "ta 0x70");
405 }
406
407 extern void jprobe_return_trap_instruction(void);
408
409 extern void __show_regs(struct pt_regs * regs);
410
411 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
412 {
413         u32 *addr = (u32 *) regs->tpc;
414
415         if (addr == (u32 *) jprobe_return_trap_instruction) {
416                 if (jprobe_saved_regs_location != regs) {
417                         printk("JPROBE: Current regs (%p) does not match "
418                                "saved regs (%p).\n",
419                                regs, jprobe_saved_regs_location);
420                         printk("JPROBE: Saved registers\n");
421                         __show_regs(jprobe_saved_regs_location);
422                         printk("JPROBE: Current registers\n");
423                         __show_regs(regs);
424                         BUG();
425                 }
426                 /* Restore old register state.  Do pt_regs
427                  * first so that UREG_FP is the original one for
428                  * the stack frame restore.
429                  */
430                 memcpy(regs, &jprobe_saved_regs, sizeof(*regs));
431
432                 memcpy((char *) (regs->u_regs[UREG_FP] + STACK_BIAS),
433                        &jprobe_saved_stack,
434                        sizeof(jprobe_saved_stack));
435
436                 return 1;
437         }
438         return 0;
439 }
440
441 /* architecture specific initialization */
442 int arch_init_kprobes(void)
443 {
444         return 0;
445 }