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