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>
12 /* We do not have hardware single-stepping on sparc64.
13 * So we implement software single-stepping with breakpoint
14 * traps. The top-level scheme is similar to that used
15 * in the x86 kprobes implementation.
17 * In the kprobe->ainsn.insn[] array we store the original
18 * instruction at index zero and a break instruction at
21 * When we hit a kprobe we:
22 * - Run the pre-handler
23 * - Remember "regs->tnpc" and interrupt level stored in
24 * "regs->tstate" so we can restore them later
25 * - Disable PIL interrupts
26 * - Set regs->tpc to point to kprobe->ainsn.insn[0]
27 * - Set regs->tnpc to point to kprobe->ainsn.insn[1]
28 * - Mark that we are actively in a kprobe
30 * At this point we wait for the second breakpoint at
31 * kprobe->ainsn.insn[1] to hit. When it does we:
32 * - Run the post-handler
33 * - Set regs->tpc to "remembered" regs->tnpc stored above,
34 * restore the PIL interrupt level in "regs->tstate" as well
35 * - Make any adjustments necessary to regs->tnpc in order
36 * to handle relative branches correctly. See below.
37 * - Mark that we are no longer actively in a kprobe.
40 int arch_prepare_kprobe(struct kprobe
*p
)
45 void arch_copy_kprobe(struct kprobe
*p
)
47 p
->ainsn
.insn
[0] = *p
->addr
;
48 p
->ainsn
.insn
[1] = BREAKPOINT_INSTRUCTION_2
;
52 void arch_arm_kprobe(struct kprobe
*p
)
54 *p
->addr
= BREAKPOINT_INSTRUCTION
;
58 void arch_disarm_kprobe(struct kprobe
*p
)
64 void arch_remove_kprobe(struct kprobe
*p
)
68 static struct kprobe
*current_kprobe
;
69 static unsigned long current_kprobe_orig_tnpc
;
70 static unsigned long current_kprobe_orig_tstate_pil
;
71 static unsigned int kprobe_status
;
72 static struct kprobe
*kprobe_prev
;
73 static unsigned long kprobe_orig_tnpc_prev
;
74 static unsigned long kprobe_orig_tstate_pil_prev
;
75 static unsigned int kprobe_status_prev
;
77 static inline void save_previous_kprobe(void)
79 kprobe_status_prev
= kprobe_status
;
80 kprobe_orig_tnpc_prev
= current_kprobe_orig_tnpc
;
81 kprobe_orig_tstate_pil_prev
= current_kprobe_orig_tstate_pil
;
82 kprobe_prev
= current_kprobe
;
85 static inline void restore_previous_kprobe(void)
87 kprobe_status
= kprobe_status_prev
;
88 current_kprobe_orig_tnpc
= kprobe_orig_tnpc_prev
;
89 current_kprobe_orig_tstate_pil
= kprobe_orig_tstate_pil_prev
;
90 current_kprobe
= kprobe_prev
;
93 static inline void set_current_kprobe(struct kprobe
*p
, struct pt_regs
*regs
)
95 current_kprobe_orig_tnpc
= regs
->tnpc
;
96 current_kprobe_orig_tstate_pil
= (regs
->tstate
& TSTATE_PIL
);
100 static inline void prepare_singlestep(struct kprobe
*p
, struct pt_regs
*regs
)
102 regs
->tstate
|= TSTATE_PIL
;
104 /*single step inline, if it a breakpoint instruction*/
105 if (p
->opcode
== BREAKPOINT_INSTRUCTION
) {
106 regs
->tpc
= (unsigned long) p
->addr
;
107 regs
->tnpc
= current_kprobe_orig_tnpc
;
109 regs
->tpc
= (unsigned long) &p
->ainsn
.insn
[0];
110 regs
->tnpc
= (unsigned long) &p
->ainsn
.insn
[1];
114 static int kprobe_handler(struct pt_regs
*regs
)
117 void *addr
= (void *) regs
->tpc
;
122 if (kprobe_running()) {
123 /* We *are* holding lock here, so this is safe.
124 * Disarm the probe we just hit, and ignore it.
126 p
= get_kprobe(addr
);
128 if (kprobe_status
== KPROBE_HIT_SS
) {
129 regs
->tstate
= ((regs
->tstate
& ~TSTATE_PIL
) |
130 current_kprobe_orig_tstate_pil
);
134 /* We have reentered the kprobe_handler(), since
135 * another probe was hit while within the handler.
136 * We here save the original kprobes variables and
137 * just single step on the instruction of the new probe
138 * without calling any user handlers.
140 save_previous_kprobe();
141 set_current_kprobe(p
, regs
);
143 kprobe_status
= KPROBE_REENTER
;
144 prepare_singlestep(p
, regs
);
148 if (p
->break_handler
&& p
->break_handler(p
, regs
))
151 /* If it's not ours, can't be delete race, (we hold lock). */
156 p
= get_kprobe(addr
);
159 if (*(u32
*)addr
!= BREAKPOINT_INSTRUCTION
) {
161 * The breakpoint instruction was removed right
162 * after we hit it. Another cpu has removed
163 * either a probepoint or a debugger breakpoint
164 * at this address. In either case, no further
165 * handling of this interrupt is appropriate.
169 /* Not one of ours: let kernel handle it */
173 set_current_kprobe(p
, regs
);
174 kprobe_status
= KPROBE_HIT_ACTIVE
;
175 if (p
->pre_handler
&& p
->pre_handler(p
, regs
))
179 prepare_singlestep(p
, regs
);
180 kprobe_status
= KPROBE_HIT_SS
;
184 preempt_enable_no_resched();
188 /* If INSN is a relative control transfer instruction,
189 * return the corrected branch destination value.
191 * The original INSN location was REAL_PC, it actually
192 * executed at PC and produced destination address NPC.
194 static unsigned long relbranch_fixup(u32 insn
, unsigned long real_pc
,
195 unsigned long pc
, unsigned long npc
)
197 /* Branch not taken, no mods necessary. */
198 if (npc
== pc
+ 0x4UL
)
199 return real_pc
+ 0x4UL
;
201 /* The three cases are call, branch w/prediction,
202 * and traditional branch.
204 if ((insn
& 0xc0000000) == 0x40000000 ||
205 (insn
& 0xc1c00000) == 0x00400000 ||
206 (insn
& 0xc1c00000) == 0x00800000) {
207 /* The instruction did all the work for us
208 * already, just apply the offset to the correct
209 * instruction location.
211 return (real_pc
+ (npc
- pc
));
214 return real_pc
+ 0x4UL
;
217 /* If INSN is an instruction which writes it's PC location
218 * into a destination register, fix that up.
220 static void retpc_fixup(struct pt_regs
*regs
, u32 insn
, unsigned long real_pc
)
222 unsigned long *slot
= NULL
;
224 /* Simplest cast is call, which always uses %o7 */
225 if ((insn
& 0xc0000000) == 0x40000000) {
226 slot
= ®s
->u_regs
[UREG_I7
];
229 /* Jmpl encodes the register inside of the opcode */
230 if ((insn
& 0xc1f80000) == 0x81c00000) {
231 unsigned long rd
= ((insn
>> 25) & 0x1f);
234 slot
= ®s
->u_regs
[rd
];
236 /* Hard case, it goes onto the stack. */
240 slot
= (unsigned long *)
241 (regs
->u_regs
[UREG_FP
] + STACK_BIAS
);
250 * Called after single-stepping. p->addr is the address of the
251 * instruction whose first byte has been replaced by the breakpoint
252 * instruction. To avoid the SMP problems that can occur when we
253 * temporarily put back the original opcode to single-step, we
254 * single-stepped a copy of the instruction. The address of this
255 * copy is p->ainsn.insn.
257 * This function prepares to return from the post-single-step
260 static void resume_execution(struct kprobe
*p
, struct pt_regs
*regs
)
262 u32 insn
= p
->ainsn
.insn
[0];
264 regs
->tpc
= current_kprobe_orig_tnpc
;
265 regs
->tnpc
= relbranch_fixup(insn
,
266 (unsigned long) p
->addr
,
267 (unsigned long) &p
->ainsn
.insn
[0],
269 retpc_fixup(regs
, insn
, (unsigned long) p
->addr
);
271 regs
->tstate
= ((regs
->tstate
& ~TSTATE_PIL
) |
272 current_kprobe_orig_tstate_pil
);
275 static inline int post_kprobe_handler(struct pt_regs
*regs
)
277 if (!kprobe_running())
280 if ((kprobe_status
!= KPROBE_REENTER
) && current_kprobe
->post_handler
) {
281 kprobe_status
= KPROBE_HIT_SSDONE
;
282 current_kprobe
->post_handler(current_kprobe
, regs
, 0);
285 resume_execution(current_kprobe
, regs
);
287 /*Restore back the original saved kprobes variables and continue. */
288 if (kprobe_status
== KPROBE_REENTER
) {
289 restore_previous_kprobe();
294 preempt_enable_no_resched();
299 /* Interrupts disabled, kprobe_lock held. */
300 static inline int kprobe_fault_handler(struct pt_regs
*regs
, int trapnr
)
302 if (current_kprobe
->fault_handler
303 && current_kprobe
->fault_handler(current_kprobe
, regs
, trapnr
))
306 if (kprobe_status
& KPROBE_HIT_SS
) {
307 resume_execution(current_kprobe
, regs
);
310 preempt_enable_no_resched();
316 * Wrapper routine to for handling exceptions.
318 int kprobe_exceptions_notify(struct notifier_block
*self
, unsigned long val
,
321 struct die_args
*args
= (struct die_args
*)data
;
324 if (kprobe_handler(args
->regs
))
328 if (post_kprobe_handler(args
->regs
))
332 if (kprobe_running() &&
333 kprobe_fault_handler(args
->regs
, args
->trapnr
))
337 if (kprobe_running() &&
338 kprobe_fault_handler(args
->regs
, args
->trapnr
))
347 asmlinkage
void kprobe_trap(unsigned long trap_level
, struct pt_regs
*regs
)
349 BUG_ON(trap_level
!= 0x170 && trap_level
!= 0x171);
351 if (user_mode(regs
)) {
353 bad_trap(regs
, trap_level
);
357 /* trap_level == 0x170 --> ta 0x70
358 * trap_level == 0x171 --> ta 0x71
360 if (notify_die((trap_level
== 0x170) ? DIE_DEBUG
: DIE_DEBUG_2
,
361 (trap_level
== 0x170) ? "debug" : "debug_2",
362 regs
, 0, trap_level
, SIGTRAP
) != NOTIFY_STOP
)
363 bad_trap(regs
, trap_level
);
366 /* Jprobes support. */
367 static struct pt_regs jprobe_saved_regs
;
368 static struct pt_regs
*jprobe_saved_regs_location
;
369 static struct sparc_stackf jprobe_saved_stack
;
371 int setjmp_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
373 struct jprobe
*jp
= container_of(p
, struct jprobe
, kp
);
375 jprobe_saved_regs_location
= regs
;
376 memcpy(&jprobe_saved_regs
, regs
, sizeof(*regs
));
378 /* Save a whole stack frame, this gets arguments
379 * pushed onto the stack after using up all the
382 memcpy(&jprobe_saved_stack
,
383 (char *) (regs
->u_regs
[UREG_FP
] + STACK_BIAS
),
384 sizeof(jprobe_saved_stack
));
386 regs
->tpc
= (unsigned long) jp
->entry
;
387 regs
->tnpc
= ((unsigned long) jp
->entry
) + 0x4UL
;
388 regs
->tstate
|= TSTATE_PIL
;
393 void jprobe_return(void)
395 preempt_enable_no_resched();
396 __asm__
__volatile__(
397 ".globl jprobe_return_trap_instruction\n"
398 "jprobe_return_trap_instruction:\n\t"
402 extern void jprobe_return_trap_instruction(void);
404 extern void __show_regs(struct pt_regs
* regs
);
406 int longjmp_break_handler(struct kprobe
*p
, struct pt_regs
*regs
)
408 u32
*addr
= (u32
*) regs
->tpc
;
410 if (addr
== (u32
*) jprobe_return_trap_instruction
) {
411 if (jprobe_saved_regs_location
!= regs
) {
412 printk("JPROBE: Current regs (%p) does not match "
413 "saved regs (%p).\n",
414 regs
, jprobe_saved_regs_location
);
415 printk("JPROBE: Saved registers\n");
416 __show_regs(jprobe_saved_regs_location
);
417 printk("JPROBE: Current registers\n");
421 /* Restore old register state. Do pt_regs
422 * first so that UREG_FP is the original one for
423 * the stack frame restore.
425 memcpy(regs
, &jprobe_saved_regs
, sizeof(*regs
));
427 memcpy((char *) (regs
->u_regs
[UREG_FP
] + STACK_BIAS
),
429 sizeof(jprobe_saved_stack
));
436 /* architecture specific initialization */
437 int arch_init_kprobes(void)