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