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>
9 #include <asm/kdebug.h>
10 #include <asm/signal.h>
11 #include <asm/cacheflush.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 __kprobes arch_prepare_kprobe(struct kprobe *p)
46 void __kprobes arch_copy_kprobe(struct kprobe *p)
48 p->ainsn.insn[0] = *p->addr;
49 p->ainsn.insn[1] = BREAKPOINT_INSTRUCTION_2;
53 void __kprobes arch_arm_kprobe(struct kprobe *p)
55 *p->addr = BREAKPOINT_INSTRUCTION;
59 void __kprobes arch_disarm_kprobe(struct kprobe *p)
65 void __kprobes arch_remove_kprobe(struct kprobe *p)
69 static struct kprobe *current_kprobe;
70 static unsigned long current_kprobe_orig_tnpc;
71 static unsigned long current_kprobe_orig_tstate_pil;
72 static unsigned int kprobe_status;
73 static struct kprobe *kprobe_prev;
74 static unsigned long kprobe_orig_tnpc_prev;
75 static unsigned long kprobe_orig_tstate_pil_prev;
76 static unsigned int kprobe_status_prev;
78 static inline void save_previous_kprobe(void)
80 kprobe_status_prev = kprobe_status;
81 kprobe_orig_tnpc_prev = current_kprobe_orig_tnpc;
82 kprobe_orig_tstate_pil_prev = current_kprobe_orig_tstate_pil;
83 kprobe_prev = current_kprobe;
86 static inline void restore_previous_kprobe(void)
88 kprobe_status = kprobe_status_prev;
89 current_kprobe_orig_tnpc = kprobe_orig_tnpc_prev;
90 current_kprobe_orig_tstate_pil = kprobe_orig_tstate_pil_prev;
91 current_kprobe = kprobe_prev;
94 static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs)
96 current_kprobe_orig_tnpc = regs->tnpc;
97 current_kprobe_orig_tstate_pil = (regs->tstate & TSTATE_PIL);
101 static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
103 regs->tstate |= TSTATE_PIL;
105 /*single step inline, if it a breakpoint instruction*/
106 if (p->opcode == BREAKPOINT_INSTRUCTION) {
107 regs->tpc = (unsigned long) p->addr;
108 regs->tnpc = current_kprobe_orig_tnpc;
110 regs->tpc = (unsigned long) &p->ainsn.insn[0];
111 regs->tnpc = (unsigned long) &p->ainsn.insn[1];
115 static int __kprobes kprobe_handler(struct pt_regs *regs)
118 void *addr = (void *) regs->tpc;
123 if (kprobe_running()) {
124 /* We *are* holding lock here, so this is safe.
125 * Disarm the probe we just hit, and ignore it.
127 p = get_kprobe(addr);
129 if (kprobe_status == KPROBE_HIT_SS) {
130 regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
131 current_kprobe_orig_tstate_pil);
135 /* We have reentered the kprobe_handler(), since
136 * another probe was hit while within the handler.
137 * We here save the original kprobes variables and
138 * just single step on the instruction of the new probe
139 * without calling any user handlers.
141 save_previous_kprobe();
142 set_current_kprobe(p, regs);
144 kprobe_status = KPROBE_REENTER;
145 prepare_singlestep(p, regs);
149 if (p->break_handler && p->break_handler(p, regs))
152 /* If it's not ours, can't be delete race, (we hold lock). */
157 p = get_kprobe(addr);
160 if (*(u32 *)addr != BREAKPOINT_INSTRUCTION) {
162 * The breakpoint instruction was removed right
163 * after we hit it. Another cpu has removed
164 * either a probepoint or a debugger breakpoint
165 * at this address. In either case, no further
166 * handling of this interrupt is appropriate.
170 /* Not one of ours: let kernel handle it */
174 set_current_kprobe(p, regs);
175 kprobe_status = KPROBE_HIT_ACTIVE;
176 if (p->pre_handler && p->pre_handler(p, regs))
180 prepare_singlestep(p, regs);
181 kprobe_status = KPROBE_HIT_SS;
185 preempt_enable_no_resched();
189 /* If INSN is a relative control transfer instruction,
190 * return the corrected branch destination value.
192 * The original INSN location was REAL_PC, it actually
193 * executed at PC and produced destination address NPC.
195 static unsigned long __kprobes relbranch_fixup(u32 insn, unsigned long real_pc,
199 /* Branch not taken, no mods necessary. */
200 if (npc == pc + 0x4UL)
201 return real_pc + 0x4UL;
203 /* The three cases are call, branch w/prediction,
204 * and traditional branch.
206 if ((insn & 0xc0000000) == 0x40000000 ||
207 (insn & 0xc1c00000) == 0x00400000 ||
208 (insn & 0xc1c00000) == 0x00800000) {
209 /* The instruction did all the work for us
210 * already, just apply the offset to the correct
211 * instruction location.
213 return (real_pc + (npc - pc));
216 return real_pc + 0x4UL;
219 /* If INSN is an instruction which writes it's PC location
220 * into a destination register, fix that up.
222 static void __kprobes retpc_fixup(struct pt_regs *regs, u32 insn,
223 unsigned long real_pc)
225 unsigned long *slot = NULL;
227 /* Simplest cast is call, which always uses %o7 */
228 if ((insn & 0xc0000000) == 0x40000000) {
229 slot = ®s->u_regs[UREG_I7];
232 /* Jmpl encodes the register inside of the opcode */
233 if ((insn & 0xc1f80000) == 0x81c00000) {
234 unsigned long rd = ((insn >> 25) & 0x1f);
237 slot = ®s->u_regs[rd];
239 /* Hard case, it goes onto the stack. */
243 slot = (unsigned long *)
244 (regs->u_regs[UREG_FP] + STACK_BIAS);
253 * Called after single-stepping. p->addr is the address of the
254 * instruction whose first byte has been replaced by the breakpoint
255 * instruction. To avoid the SMP problems that can occur when we
256 * temporarily put back the original opcode to single-step, we
257 * single-stepped a copy of the instruction. The address of this
258 * copy is p->ainsn.insn.
260 * This function prepares to return from the post-single-step
263 static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
265 u32 insn = p->ainsn.insn[0];
267 regs->tpc = current_kprobe_orig_tnpc;
268 regs->tnpc = relbranch_fixup(insn,
269 (unsigned long) p->addr,
270 (unsigned long) &p->ainsn.insn[0],
272 retpc_fixup(regs, insn, (unsigned long) p->addr);
274 regs->tstate = ((regs->tstate & ~TSTATE_PIL) |
275 current_kprobe_orig_tstate_pil);
278 static inline int post_kprobe_handler(struct pt_regs *regs)
280 if (!kprobe_running())
283 if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) {
284 kprobe_status = KPROBE_HIT_SSDONE;
285 current_kprobe->post_handler(current_kprobe, regs, 0);
288 resume_execution(current_kprobe, regs);
290 /*Restore back the original saved kprobes variables and continue. */
291 if (kprobe_status == KPROBE_REENTER) {
292 restore_previous_kprobe();
297 preempt_enable_no_resched();
302 /* Interrupts disabled, kprobe_lock held. */
303 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
305 if (current_kprobe->fault_handler
306 && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
309 if (kprobe_status & KPROBE_HIT_SS) {
310 resume_execution(current_kprobe, regs);
313 preempt_enable_no_resched();
319 * Wrapper routine to for handling exceptions.
321 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
322 unsigned long val, void *data)
324 struct die_args *args = (struct die_args *)data;
327 if (kprobe_handler(args->regs))
331 if (post_kprobe_handler(args->regs))
335 if (kprobe_running() &&
336 kprobe_fault_handler(args->regs, args->trapnr))
340 if (kprobe_running() &&
341 kprobe_fault_handler(args->regs, args->trapnr))
350 asmlinkage void __kprobes kprobe_trap(unsigned long trap_level,
351 struct pt_regs *regs)
353 BUG_ON(trap_level != 0x170 && trap_level != 0x171);
355 if (user_mode(regs)) {
357 bad_trap(regs, trap_level);
361 /* trap_level == 0x170 --> ta 0x70
362 * trap_level == 0x171 --> ta 0x71
364 if (notify_die((trap_level == 0x170) ? DIE_DEBUG : DIE_DEBUG_2,
365 (trap_level == 0x170) ? "debug" : "debug_2",
366 regs, 0, trap_level, SIGTRAP) != NOTIFY_STOP)
367 bad_trap(regs, trap_level);
370 /* Jprobes support. */
371 static struct pt_regs jprobe_saved_regs;
372 static struct pt_regs *jprobe_saved_regs_location;
373 static struct sparc_stackf jprobe_saved_stack;
375 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
377 struct jprobe *jp = container_of(p, struct jprobe, kp);
379 jprobe_saved_regs_location = regs;
380 memcpy(&jprobe_saved_regs, regs, sizeof(*regs));
382 /* Save a whole stack frame, this gets arguments
383 * pushed onto the stack after using up all the
386 memcpy(&jprobe_saved_stack,
387 (char *) (regs->u_regs[UREG_FP] + STACK_BIAS),
388 sizeof(jprobe_saved_stack));
390 regs->tpc = (unsigned long) jp->entry;
391 regs->tnpc = ((unsigned long) jp->entry) + 0x4UL;
392 regs->tstate |= TSTATE_PIL;
397 void __kprobes jprobe_return(void)
399 preempt_enable_no_resched();
400 __asm__ __volatile__(
401 ".globl jprobe_return_trap_instruction\n"
402 "jprobe_return_trap_instruction:\n\t"
406 extern void jprobe_return_trap_instruction(void);
408 extern void __show_regs(struct pt_regs * regs);
410 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
412 u32 *addr = (u32 *) regs->tpc;
414 if (addr == (u32 *) jprobe_return_trap_instruction) {
415 if (jprobe_saved_regs_location != regs) {
416 printk("JPROBE: Current regs (%p) does not match "
417 "saved regs (%p).\n",
418 regs, jprobe_saved_regs_location);
419 printk("JPROBE: Saved registers\n");
420 __show_regs(jprobe_saved_regs_location);
421 printk("JPROBE: Current registers\n");
425 /* Restore old register state. Do pt_regs
426 * first so that UREG_FP is the original one for
427 * the stack frame restore.
429 memcpy(regs, &jprobe_saved_regs, sizeof(*regs));
431 memcpy((char *) (regs->u_regs[UREG_FP] + STACK_BIAS),
433 sizeof(jprobe_saved_stack));
440 /* architecture specific initialization */
441 int arch_init_kprobes(void)