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 DEFINE_PER_CPU(struct kprobe
*, current_kprobe
) = NULL
;
42 DEFINE_PER_CPU(struct kprobe_ctlblk
, kprobe_ctlblk
);
44 int __kprobes
arch_prepare_kprobe(struct kprobe
*p
)
49 void __kprobes
arch_copy_kprobe(struct kprobe
*p
)
51 p
->ainsn
.insn
[0] = *p
->addr
;
52 p
->ainsn
.insn
[1] = BREAKPOINT_INSTRUCTION_2
;
56 void __kprobes
arch_arm_kprobe(struct kprobe
*p
)
58 *p
->addr
= BREAKPOINT_INSTRUCTION
;
62 void __kprobes
arch_disarm_kprobe(struct kprobe
*p
)
68 void __kprobes
arch_remove_kprobe(struct kprobe
*p
)
72 static inline void save_previous_kprobe(struct kprobe_ctlblk
*kcb
)
74 kcb
->prev_kprobe
.kp
= kprobe_running();
75 kcb
->prev_kprobe
.status
= kcb
->kprobe_status
;
76 kcb
->prev_kprobe
.orig_tnpc
= kcb
->kprobe_orig_tnpc
;
77 kcb
->prev_kprobe
.orig_tstate_pil
= kcb
->kprobe_orig_tstate_pil
;
80 static inline void restore_previous_kprobe(struct kprobe_ctlblk
*kcb
)
82 __get_cpu_var(current_kprobe
) = kcb
->prev_kprobe
.kp
;
83 kcb
->kprobe_status
= kcb
->prev_kprobe
.status
;
84 kcb
->kprobe_orig_tnpc
= kcb
->prev_kprobe
.orig_tnpc
;
85 kcb
->kprobe_orig_tstate_pil
= kcb
->prev_kprobe
.orig_tstate_pil
;
88 static inline void set_current_kprobe(struct kprobe
*p
, struct pt_regs
*regs
,
89 struct kprobe_ctlblk
*kcb
)
91 __get_cpu_var(current_kprobe
) = p
;
92 kcb
->kprobe_orig_tnpc
= regs
->tnpc
;
93 kcb
->kprobe_orig_tstate_pil
= (regs
->tstate
& TSTATE_PIL
);
96 static inline void prepare_singlestep(struct kprobe
*p
, struct pt_regs
*regs
,
97 struct kprobe_ctlblk
*kcb
)
99 regs
->tstate
|= TSTATE_PIL
;
101 /*single step inline, if it a breakpoint instruction*/
102 if (p
->opcode
== BREAKPOINT_INSTRUCTION
) {
103 regs
->tpc
= (unsigned long) p
->addr
;
104 regs
->tnpc
= kcb
->kprobe_orig_tnpc
;
106 regs
->tpc
= (unsigned long) &p
->ainsn
.insn
[0];
107 regs
->tnpc
= (unsigned long) &p
->ainsn
.insn
[1];
111 static int __kprobes
kprobe_handler(struct pt_regs
*regs
)
114 void *addr
= (void *) regs
->tpc
;
116 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
118 if (kprobe_running()) {
119 /* We *are* holding lock here, so this is safe.
120 * Disarm the probe we just hit, and ignore it.
122 p
= get_kprobe(addr
);
124 if (kcb
->kprobe_status
== KPROBE_HIT_SS
) {
125 regs
->tstate
= ((regs
->tstate
& ~TSTATE_PIL
) |
126 kcb
->kprobe_orig_tstate_pil
);
130 /* We have reentered the kprobe_handler(), since
131 * another probe was hit while within the handler.
132 * We here save the original kprobes variables and
133 * just single step on the instruction of the new probe
134 * without calling any user handlers.
136 save_previous_kprobe(kcb
);
137 set_current_kprobe(p
, regs
, kcb
);
139 kcb
->kprobe_status
= KPROBE_REENTER
;
140 prepare_singlestep(p
, regs
, kcb
);
143 p
= __get_cpu_var(current_kprobe
);
144 if (p
->break_handler
&& p
->break_handler(p
, regs
))
147 /* If it's not ours, can't be delete race, (we hold lock). */
152 p
= get_kprobe(addr
);
155 if (*(u32
*)addr
!= BREAKPOINT_INSTRUCTION
) {
157 * The breakpoint instruction was removed right
158 * after we hit it. Another cpu has removed
159 * either a probepoint or a debugger breakpoint
160 * at this address. In either case, no further
161 * handling of this interrupt is appropriate.
165 /* Not one of ours: let kernel handle it */
170 * This preempt_disable() matches the preempt_enable_no_resched()
171 * in post_kprobes_handler()
174 set_current_kprobe(p
, regs
, kcb
);
175 kcb
->kprobe_status
= KPROBE_HIT_ACTIVE
;
176 if (p
->pre_handler
&& p
->pre_handler(p
, regs
))
180 prepare_singlestep(p
, regs
, kcb
);
181 kcb
->kprobe_status
= KPROBE_HIT_SS
;
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 __kprobes
relbranch_fixup(u32 insn
, unsigned long real_pc
,
198 /* Branch not taken, no mods necessary. */
199 if (npc
== pc
+ 0x4UL
)
200 return real_pc
+ 0x4UL
;
202 /* The three cases are call, branch w/prediction,
203 * and traditional branch.
205 if ((insn
& 0xc0000000) == 0x40000000 ||
206 (insn
& 0xc1c00000) == 0x00400000 ||
207 (insn
& 0xc1c00000) == 0x00800000) {
208 /* The instruction did all the work for us
209 * already, just apply the offset to the correct
210 * instruction location.
212 return (real_pc
+ (npc
- pc
));
215 return real_pc
+ 0x4UL
;
218 /* If INSN is an instruction which writes it's PC location
219 * into a destination register, fix that up.
221 static void __kprobes
retpc_fixup(struct pt_regs
*regs
, u32 insn
,
222 unsigned long real_pc
)
224 unsigned long *slot
= NULL
;
226 /* Simplest cast is call, which always uses %o7 */
227 if ((insn
& 0xc0000000) == 0x40000000) {
228 slot
= ®s
->u_regs
[UREG_I7
];
231 /* Jmpl encodes the register inside of the opcode */
232 if ((insn
& 0xc1f80000) == 0x81c00000) {
233 unsigned long rd
= ((insn
>> 25) & 0x1f);
236 slot
= ®s
->u_regs
[rd
];
238 /* Hard case, it goes onto the stack. */
242 slot
= (unsigned long *)
243 (regs
->u_regs
[UREG_FP
] + STACK_BIAS
);
252 * Called after single-stepping. p->addr is the address of the
253 * instruction whose first byte has been replaced by the breakpoint
254 * instruction. To avoid the SMP problems that can occur when we
255 * temporarily put back the original opcode to single-step, we
256 * single-stepped a copy of the instruction. The address of this
257 * copy is p->ainsn.insn.
259 * This function prepares to return from the post-single-step
262 static void __kprobes
resume_execution(struct kprobe
*p
,
263 struct pt_regs
*regs
, struct kprobe_ctlblk
*kcb
)
265 u32 insn
= p
->ainsn
.insn
[0];
267 regs
->tpc
= kcb
->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 kcb
->kprobe_orig_tstate_pil
);
278 static inline int post_kprobe_handler(struct pt_regs
*regs
)
280 struct kprobe
*cur
= kprobe_running();
281 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
286 if ((kcb
->kprobe_status
!= KPROBE_REENTER
) && cur
->post_handler
) {
287 kcb
->kprobe_status
= KPROBE_HIT_SSDONE
;
288 cur
->post_handler(cur
, regs
, 0);
291 resume_execution(cur
, regs
, kcb
);
293 /*Restore back the original saved kprobes variables and continue. */
294 if (kcb
->kprobe_status
== KPROBE_REENTER
) {
295 restore_previous_kprobe(kcb
);
298 reset_current_kprobe();
301 preempt_enable_no_resched();
306 /* Interrupts disabled, kprobe_lock held. */
307 static inline int kprobe_fault_handler(struct pt_regs
*regs
, int trapnr
)
309 struct kprobe
*cur
= kprobe_running();
310 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
312 if (cur
->fault_handler
&& cur
->fault_handler(cur
, regs
, trapnr
))
315 if (kcb
->kprobe_status
& KPROBE_HIT_SS
) {
316 resume_execution(cur
, regs
, kcb
);
318 reset_current_kprobe();
320 preempt_enable_no_resched();
326 * Wrapper routine to for handling exceptions.
328 int __kprobes
kprobe_exceptions_notify(struct notifier_block
*self
,
329 unsigned long val
, void *data
)
331 struct die_args
*args
= (struct die_args
*)data
;
332 int ret
= NOTIFY_DONE
;
337 if (kprobe_handler(args
->regs
))
341 if (post_kprobe_handler(args
->regs
))
346 if (kprobe_running() &&
347 kprobe_fault_handler(args
->regs
, args
->trapnr
))
357 asmlinkage
void __kprobes
kprobe_trap(unsigned long trap_level
,
358 struct pt_regs
*regs
)
360 BUG_ON(trap_level
!= 0x170 && trap_level
!= 0x171);
362 if (user_mode(regs
)) {
364 bad_trap(regs
, trap_level
);
368 /* trap_level == 0x170 --> ta 0x70
369 * trap_level == 0x171 --> ta 0x71
371 if (notify_die((trap_level
== 0x170) ? DIE_DEBUG
: DIE_DEBUG_2
,
372 (trap_level
== 0x170) ? "debug" : "debug_2",
373 regs
, 0, trap_level
, SIGTRAP
) != NOTIFY_STOP
)
374 bad_trap(regs
, trap_level
);
377 /* Jprobes support. */
378 int __kprobes
setjmp_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
380 struct jprobe
*jp
= container_of(p
, struct jprobe
, kp
);
381 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
383 kcb
->jprobe_saved_regs_location
= regs
;
384 memcpy(&(kcb
->jprobe_saved_regs
), regs
, sizeof(*regs
));
386 /* Save a whole stack frame, this gets arguments
387 * pushed onto the stack after using up all the
390 memcpy(&(kcb
->jprobe_saved_stack
),
391 (char *) (regs
->u_regs
[UREG_FP
] + STACK_BIAS
),
392 sizeof(kcb
->jprobe_saved_stack
));
394 regs
->tpc
= (unsigned long) jp
->entry
;
395 regs
->tnpc
= ((unsigned long) jp
->entry
) + 0x4UL
;
396 regs
->tstate
|= TSTATE_PIL
;
401 void __kprobes
jprobe_return(void)
403 __asm__
__volatile__(
404 ".globl jprobe_return_trap_instruction\n"
405 "jprobe_return_trap_instruction:\n\t"
409 extern void jprobe_return_trap_instruction(void);
411 extern void __show_regs(struct pt_regs
* regs
);
413 int __kprobes
longjmp_break_handler(struct kprobe
*p
, struct pt_regs
*regs
)
415 u32
*addr
= (u32
*) regs
->tpc
;
416 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
418 if (addr
== (u32
*) jprobe_return_trap_instruction
) {
419 if (kcb
->jprobe_saved_regs_location
!= regs
) {
420 printk("JPROBE: Current regs (%p) does not match "
421 "saved regs (%p).\n",
422 regs
, kcb
->jprobe_saved_regs_location
);
423 printk("JPROBE: Saved registers\n");
424 __show_regs(kcb
->jprobe_saved_regs_location
);
425 printk("JPROBE: Current registers\n");
429 /* Restore old register state. Do pt_regs
430 * first so that UREG_FP is the original one for
431 * the stack frame restore.
433 memcpy(regs
, &(kcb
->jprobe_saved_regs
), sizeof(*regs
));
435 memcpy((char *) (regs
->u_regs
[UREG_FP
] + STACK_BIAS
),
436 &(kcb
->jprobe_saved_stack
),
437 sizeof(kcb
->jprobe_saved_stack
));
444 /* architecture specific initialization */
445 int arch_init_kprobes(void)