1 /* arch/sparc64/kernel/kprobes.c
3 * Copyright (C) 2004 David S. Miller <davem@davemloft.net>
6 #include <linux/config.h>
7 #include <linux/kernel.h>
8 #include <linux/kprobes.h>
10 #include <asm/kdebug.h>
11 #include <asm/signal.h>
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.
18 * In the kprobe->ainsn.insn[] array we store the original
19 * instruction at index zero and a break instruction at
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
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.
41 int arch_prepare_kprobe(struct kprobe *p)
46 void arch_copy_kprobe(struct kprobe *p)
48 p->ainsn.insn[0] = *p->addr;
49 p->ainsn.insn[1] = BREAKPOINT_INSTRUCTION_2;
52 void arch_remove_kprobe(struct kprobe *p)
56 /* kprobe_status settings */
57 #define KPROBE_HIT_ACTIVE 0x00000001
58 #define KPROBE_HIT_SS 0x00000002
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;
65 static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
67 current_kprobe_orig_tnpc = regs->tnpc;
68 current_kprobe_orig_tstate_pil = (regs->tstate & TSTATE_PIL);
69 regs->tstate |= TSTATE_PIL;
71 regs->tpc = (unsigned long) &p->ainsn.insn[0];
72 regs->tnpc = (unsigned long) &p->ainsn.insn[1];
75 static inline void disarm_kprobe(struct kprobe *p, struct pt_regs *regs)
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);
86 static int kprobe_handler(struct pt_regs *regs)
89 void *addr = (void *) regs->tpc;
94 if (kprobe_running()) {
95 /* We *are* holding lock here, so this is safe.
96 * Disarm the probe we just hit, and ignore it.
100 disarm_kprobe(p, regs);
104 if (p->break_handler && p->break_handler(p, regs))
107 /* If it's not ours, can't be delete race, (we hold lock). */
112 p = get_kprobe(addr);
115 if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) {
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.
125 /* Not one of ours: let kernel handle it */
129 kprobe_status = KPROBE_HIT_ACTIVE;
131 if (p->pre_handler(p, regs))
135 prepare_singlestep(p, regs);
136 kprobe_status = KPROBE_HIT_SS;
140 preempt_enable_no_resched();
144 /* If INSN is a relative control transfer instruction,
145 * return the corrected branch destination value.
147 * The original INSN location was REAL_PC, it actually
148 * executed at PC and produced destination address NPC.
150 static unsigned long relbranch_fixup(u32 insn, unsigned long real_pc,
151 unsigned long pc, unsigned long npc)
153 /* Branch not taken, no mods necessary. */
154 if (npc == pc + 0x4UL)
155 return real_pc + 0x4UL;
157 /* The three cases are call, branch w/prediction,
158 * and traditional branch.
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.
167 return (real_pc + (npc - pc));
170 return real_pc + 0x4UL;
173 /* If INSN is an instruction which writes it's PC location
174 * into a destination register, fix that up.
176 static void retpc_fixup(struct pt_regs *regs, u32 insn, unsigned long real_pc)
178 unsigned long *slot = NULL;
180 /* Simplest cast is call, which always uses %o7 */
181 if ((insn & 0xc0000000) == 0x40000000) {
182 slot = ®s->u_regs[UREG_I7];
185 /* Jmpl encodes the register inside of the opcode */
186 if ((insn & 0xc1f80000) == 0x81c00000) {
187 unsigned long rd = ((insn >> 25) & 0x1f);
190 slot = ®s->u_regs[rd];
192 /* Hard case, it goes onto the stack. */
196 slot = (unsigned long *)
197 (regs->u_regs[UREG_FP] + STACK_BIAS);
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.
213 * This function prepares to return from the post-single-step
216 static void resume_execution(struct kprobe *p, struct pt_regs *regs)
218 u32 insn = p->ainsn.insn[0];
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],
225 retpc_fixup(regs, insn, (unsigned long) p->addr);
227 regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
228 current_kprobe_orig_tstate_pil);
231 static inline int post_kprobe_handler(struct pt_regs *regs)
233 if (!kprobe_running())
236 if (current_kprobe->post_handler)
237 current_kprobe->post_handler(current_kprobe, regs, 0);
239 resume_execution(current_kprobe, regs);
242 preempt_enable_no_resched();
247 /* Interrupts disabled, kprobe_lock held. */
248 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
250 if (current_kprobe->fault_handler
251 && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
254 if (kprobe_status & KPROBE_HIT_SS) {
255 resume_execution(current_kprobe, regs);
258 preempt_enable_no_resched();
264 * Wrapper routine to for handling exceptions.
266 int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
269 struct die_args *args = (struct die_args *)data;
272 if (kprobe_handler(args->regs))
276 if (post_kprobe_handler(args->regs))
280 if (kprobe_running() &&
281 kprobe_fault_handler(args->regs, args->trapnr))
285 if (kprobe_running() &&
286 kprobe_fault_handler(args->regs, args->trapnr))
295 asmlinkage void kprobe_trap(unsigned long trap_level, struct pt_regs *regs)
297 BUG_ON(trap_level != 0x170 && trap_level != 0x171);
299 if (user_mode(regs)) {
301 bad_trap(regs, trap_level);
305 /* trap_level == 0x170 --> ta 0x70
306 * trap_level == 0x171 --> ta 0x71
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);
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;
319 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
321 struct jprobe *jp = container_of(p, struct jprobe, kp);
323 jprobe_saved_regs_location = regs;
324 memcpy(&jprobe_saved_regs, regs, sizeof(*regs));
326 /* Save a whole stack frame, this gets arguments
327 * pushed onto the stack after using up all the
330 memcpy(&jprobe_saved_stack,
331 (char *) (regs->u_regs[UREG_FP] + STACK_BIAS),
332 sizeof(jprobe_saved_stack));
334 regs->tpc = (unsigned long) jp->entry;
335 regs->tnpc = ((unsigned long) jp->entry) + 0x4UL;
336 regs->tstate |= TSTATE_PIL;
341 void jprobe_return(void)
343 preempt_enable_no_resched();
344 __asm__ __volatile__(
345 ".globl jprobe_return_trap_instruction\n"
346 "jprobe_return_trap_instruction:\n\t"
350 extern void jprobe_return_trap_instruction(void);
352 extern void __show_regs(struct pt_regs * regs);
354 int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
356 u32 *addr = (u32 *) regs->tpc;
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");
369 /* Restore old register state. Do pt_regs
370 * first so that UREG_FP is the original one for
371 * the stack frame restore.
373 memcpy(regs, &jprobe_saved_regs, sizeof(*regs));
375 memcpy((char *) (regs->u_regs[UREG_FP] + STACK_BIAS),
377 sizeof(jprobe_saved_stack));