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->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->insn[0]
28 * - Set regs->tnpc to point to kprobe->insn[1]
29 * - Mark that we are actively in a kprobe
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.
41 void arch_prepare_kprobe(struct kprobe *p)
43 p->insn[0] = *p->addr;
44 p->insn[1] = BREAKPOINT_INSTRUCTION_2;
47 /* kprobe_status settings */
48 #define KPROBE_HIT_ACTIVE 0x00000001
49 #define KPROBE_HIT_SS 0x00000002
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;
56 static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
58 current_kprobe_orig_tnpc = regs->tnpc;
59 current_kprobe_orig_tstate_pil = (regs->tstate & TSTATE_PIL);
60 regs->tstate |= TSTATE_PIL;
62 regs->tpc = (unsigned long) &p->insn[0];
63 regs->tnpc = (unsigned long) &p->insn[1];
66 static inline void disarm_kprobe(struct kprobe *p, struct pt_regs *regs)
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);
77 static int kprobe_handler(struct pt_regs *regs)
80 void *addr = (void *) regs->tpc;
85 if (kprobe_running()) {
86 /* We *are* holding lock here, so this is safe.
87 * Disarm the probe we just hit, and ignore it.
91 disarm_kprobe(p, regs);
95 if (p->break_handler && p->break_handler(p, regs))
98 /* If it's not ours, can't be delete race, (we hold lock). */
103 p = get_kprobe(addr);
106 if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) {
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.
116 /* Not one of ours: let kernel handle it */
120 kprobe_status = KPROBE_HIT_ACTIVE;
122 if (p->pre_handler(p, regs))
126 prepare_singlestep(p, regs);
127 kprobe_status = KPROBE_HIT_SS;
131 preempt_enable_no_resched();
135 /* If INSN is a relative control transfer instruction,
136 * return the corrected branch destination value.
138 * The original INSN location was REAL_PC, it actually
139 * executed at PC and produced destination address NPC.
141 static unsigned long relbranch_fixup(u32 insn, unsigned long real_pc,
142 unsigned long pc, unsigned long npc)
144 /* Branch not taken, no mods necessary. */
145 if (npc == pc + 0x4UL)
146 return real_pc + 0x4UL;
148 /* The three cases are call, branch w/prediction,
149 * and traditional branch.
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.
158 return (real_pc + (npc - pc));
161 return real_pc + 0x4UL;
164 /* If INSN is an instruction which writes it's PC location
165 * into a destination register, fix that up.
167 static void retpc_fixup(struct pt_regs *regs, u32 insn, unsigned long real_pc)
169 unsigned long *slot = NULL;
171 /* Simplest cast is call, which always uses %o7 */
172 if ((insn & 0xc0000000) == 0x40000000) {
173 slot = ®s->u_regs[UREG_I7];
176 /* Jmpl encodes the register inside of the opcode */
177 if ((insn & 0xc1f80000) == 0x81c00000) {
178 unsigned long rd = ((insn >> 25) & 0x1f);
181 slot = ®s->u_regs[rd];
183 /* Hard case, it goes onto the stack. */
187 slot = (unsigned long *)
188 (regs->u_regs[UREG_FP] + STACK_BIAS);
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
204 * This function prepares to return from the post-single-step
207 static void resume_execution(struct kprobe *p, struct pt_regs *regs)
209 u32 insn = p->insn[0];
211 regs->tpc = current_kprobe_orig_tnpc;
212 regs->tnpc = relbranch_fixup(insn,
213 (unsigned long) p->addr,
214 (unsigned long) &p->insn[0],
216 retpc_fixup(regs, insn, (unsigned long) p->addr);
218 regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
219 current_kprobe_orig_tstate_pil);
222 static inline int post_kprobe_handler(struct pt_regs *regs)
224 if (!kprobe_running())
227 if (current_kprobe->post_handler)
228 current_kprobe->post_handler(current_kprobe, regs, 0);
230 resume_execution(current_kprobe, regs);
233 preempt_enable_no_resched();
238 /* Interrupts disabled, kprobe_lock held. */
239 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
241 if (current_kprobe->fault_handler
242 && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
245 if (kprobe_status & KPROBE_HIT_SS) {
246 resume_execution(current_kprobe, regs);
249 preempt_enable_no_resched();
255 * Wrapper routine to for handling exceptions.
257 int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
260 struct die_args *args = (struct die_args *)data;
263 if (kprobe_handler(args->regs))
267 if (post_kprobe_handler(args->regs))
271 if (kprobe_running() &&
272 kprobe_fault_handler(args->regs, args->trapnr))
276 if (kprobe_running() &&
277 kprobe_fault_handler(args->regs, args->trapnr))
286 asmlinkage void kprobe_trap(unsigned long trap_level, struct pt_regs *regs)
288 BUG_ON(trap_level != 0x170 && trap_level != 0x171);
290 if (user_mode(regs)) {
292 bad_trap(regs, trap_level);
296 /* trap_level == 0x170 --> ta 0x70
297 * trap_level == 0x171 --> ta 0x71
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);
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;
310 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
312 struct jprobe *jp = container_of(p, struct jprobe, kp);
314 jprobe_saved_regs_location = regs;
315 memcpy(&jprobe_saved_regs, regs, sizeof(*regs));
317 /* Save a whole stack frame, this gets arguments
318 * pushed onto the stack after using up all the
321 memcpy(&jprobe_saved_stack,
322 (char *) (regs->u_regs[UREG_FP] + STACK_BIAS),
323 sizeof(jprobe_saved_stack));
325 regs->tpc = (unsigned long) jp->entry;
326 regs->tnpc = ((unsigned long) jp->entry) + 0x4UL;
327 regs->tstate |= TSTATE_PIL;
332 void jprobe_return(void)
334 preempt_enable_no_resched();
335 __asm__ __volatile__(
336 ".globl jprobe_return_trap_instruction\n"
337 "jprobe_return_trap_instruction:\n\t"
341 extern void jprobe_return_trap_instruction(void);
343 extern void __show_regs(struct pt_regs * regs);
345 int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
347 u32 *addr = (u32 *) regs->tpc;
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");
360 /* Restore old register state. Do pt_regs
361 * first so that UREG_FP is the original one for
362 * the stack frame restore.
364 memcpy(regs, &jprobe_saved_regs, sizeof(*regs));
366 memcpy((char *) (regs->u_regs[UREG_FP] + STACK_BIAS),
368 sizeof(jprobe_saved_stack));