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