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
;
121 if (kprobe_running()) {
122 /* We *are* holding lock here, so this is safe.
123 * Disarm the probe we just hit, and ignore it.
125 p
= get_kprobe(addr
);
127 if (kprobe_status
== KPROBE_HIT_SS
) {
128 regs
->tstate
= ((regs
->tstate
& ~TSTATE_PIL
) |
129 current_kprobe_orig_tstate_pil
);
133 /* We have reentered the kprobe_handler(), since
134 * another probe was hit while within the handler.
135 * We here save the original kprobes variables and
136 * just single step on the instruction of the new probe
137 * without calling any user handlers.
139 save_previous_kprobe();
140 set_current_kprobe(p
, regs
);
142 kprobe_status
= KPROBE_REENTER
;
143 prepare_singlestep(p
, regs
);
147 if (p
->break_handler
&& p
->break_handler(p
, regs
))
150 /* If it's not ours, can't be delete race, (we hold lock). */
155 p
= get_kprobe(addr
);
158 if (*(u32
*)addr
!= BREAKPOINT_INSTRUCTION
) {
160 * The breakpoint instruction was removed right
161 * after we hit it. Another cpu has removed
162 * either a probepoint or a debugger breakpoint
163 * at this address. In either case, no further
164 * handling of this interrupt is appropriate.
168 /* Not one of ours: let kernel handle it */
173 * This preempt_disable() matches the preempt_enable_no_resched()
174 * in post_kprobes_handler()
177 set_current_kprobe(p
, regs
);
178 kprobe_status
= KPROBE_HIT_ACTIVE
;
179 if (p
->pre_handler
&& p
->pre_handler(p
, regs
))
183 prepare_singlestep(p
, regs
);
184 kprobe_status
= KPROBE_HIT_SS
;
191 /* If INSN is a relative control transfer instruction,
192 * return the corrected branch destination value.
194 * The original INSN location was REAL_PC, it actually
195 * executed at PC and produced destination address NPC.
197 static unsigned long __kprobes
relbranch_fixup(u32 insn
, unsigned long real_pc
,
201 /* Branch not taken, no mods necessary. */
202 if (npc
== pc
+ 0x4UL
)
203 return real_pc
+ 0x4UL
;
205 /* The three cases are call, branch w/prediction,
206 * and traditional branch.
208 if ((insn
& 0xc0000000) == 0x40000000 ||
209 (insn
& 0xc1c00000) == 0x00400000 ||
210 (insn
& 0xc1c00000) == 0x00800000) {
211 /* The instruction did all the work for us
212 * already, just apply the offset to the correct
213 * instruction location.
215 return (real_pc
+ (npc
- pc
));
218 return real_pc
+ 0x4UL
;
221 /* If INSN is an instruction which writes it's PC location
222 * into a destination register, fix that up.
224 static void __kprobes
retpc_fixup(struct pt_regs
*regs
, u32 insn
,
225 unsigned long real_pc
)
227 unsigned long *slot
= NULL
;
229 /* Simplest cast is call, which always uses %o7 */
230 if ((insn
& 0xc0000000) == 0x40000000) {
231 slot
= ®s
->u_regs
[UREG_I7
];
234 /* Jmpl encodes the register inside of the opcode */
235 if ((insn
& 0xc1f80000) == 0x81c00000) {
236 unsigned long rd
= ((insn
>> 25) & 0x1f);
239 slot
= ®s
->u_regs
[rd
];
241 /* Hard case, it goes onto the stack. */
245 slot
= (unsigned long *)
246 (regs
->u_regs
[UREG_FP
] + STACK_BIAS
);
255 * Called after single-stepping. p->addr is the address of the
256 * instruction whose first byte has been replaced by the breakpoint
257 * instruction. To avoid the SMP problems that can occur when we
258 * temporarily put back the original opcode to single-step, we
259 * single-stepped a copy of the instruction. The address of this
260 * copy is p->ainsn.insn.
262 * This function prepares to return from the post-single-step
265 static void __kprobes
resume_execution(struct kprobe
*p
, struct pt_regs
*regs
)
267 u32 insn
= p
->ainsn
.insn
[0];
269 regs
->tpc
= current_kprobe_orig_tnpc
;
270 regs
->tnpc
= relbranch_fixup(insn
,
271 (unsigned long) p
->addr
,
272 (unsigned long) &p
->ainsn
.insn
[0],
274 retpc_fixup(regs
, insn
, (unsigned long) p
->addr
);
276 regs
->tstate
= ((regs
->tstate
& ~TSTATE_PIL
) |
277 current_kprobe_orig_tstate_pil
);
280 static inline int post_kprobe_handler(struct pt_regs
*regs
)
282 if (!kprobe_running())
285 if ((kprobe_status
!= KPROBE_REENTER
) && current_kprobe
->post_handler
) {
286 kprobe_status
= KPROBE_HIT_SSDONE
;
287 current_kprobe
->post_handler(current_kprobe
, regs
, 0);
290 resume_execution(current_kprobe
, regs
);
292 /*Restore back the original saved kprobes variables and continue. */
293 if (kprobe_status
== KPROBE_REENTER
) {
294 restore_previous_kprobe();
299 preempt_enable_no_resched();
304 /* Interrupts disabled, kprobe_lock held. */
305 static inline int kprobe_fault_handler(struct pt_regs
*regs
, int trapnr
)
307 if (current_kprobe
->fault_handler
308 && current_kprobe
->fault_handler(current_kprobe
, regs
, trapnr
))
311 if (kprobe_status
& KPROBE_HIT_SS
) {
312 resume_execution(current_kprobe
, regs
);
315 preempt_enable_no_resched();
321 * Wrapper routine to for handling exceptions.
323 int __kprobes
kprobe_exceptions_notify(struct notifier_block
*self
,
324 unsigned long val
, void *data
)
326 struct die_args
*args
= (struct die_args
*)data
;
327 int ret
= NOTIFY_DONE
;
332 if (kprobe_handler(args
->regs
))
336 if (post_kprobe_handler(args
->regs
))
341 if (kprobe_running() &&
342 kprobe_fault_handler(args
->regs
, args
->trapnr
))
352 asmlinkage
void __kprobes
kprobe_trap(unsigned long trap_level
,
353 struct pt_regs
*regs
)
355 BUG_ON(trap_level
!= 0x170 && trap_level
!= 0x171);
357 if (user_mode(regs
)) {
359 bad_trap(regs
, trap_level
);
363 /* trap_level == 0x170 --> ta 0x70
364 * trap_level == 0x171 --> ta 0x71
366 if (notify_die((trap_level
== 0x170) ? DIE_DEBUG
: DIE_DEBUG_2
,
367 (trap_level
== 0x170) ? "debug" : "debug_2",
368 regs
, 0, trap_level
, SIGTRAP
) != NOTIFY_STOP
)
369 bad_trap(regs
, trap_level
);
372 /* Jprobes support. */
373 static struct pt_regs jprobe_saved_regs
;
374 static struct pt_regs
*jprobe_saved_regs_location
;
375 static struct sparc_stackf jprobe_saved_stack
;
377 int __kprobes
setjmp_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
379 struct jprobe
*jp
= container_of(p
, struct jprobe
, kp
);
381 jprobe_saved_regs_location
= regs
;
382 memcpy(&jprobe_saved_regs
, regs
, sizeof(*regs
));
384 /* Save a whole stack frame, this gets arguments
385 * pushed onto the stack after using up all the
388 memcpy(&jprobe_saved_stack
,
389 (char *) (regs
->u_regs
[UREG_FP
] + STACK_BIAS
),
390 sizeof(jprobe_saved_stack
));
392 regs
->tpc
= (unsigned long) jp
->entry
;
393 regs
->tnpc
= ((unsigned long) jp
->entry
) + 0x4UL
;
394 regs
->tstate
|= TSTATE_PIL
;
399 void __kprobes
jprobe_return(void)
401 __asm__
__volatile__(
402 ".globl jprobe_return_trap_instruction\n"
403 "jprobe_return_trap_instruction:\n\t"
407 extern void jprobe_return_trap_instruction(void);
409 extern void __show_regs(struct pt_regs
* regs
);
411 int __kprobes
longjmp_break_handler(struct kprobe
*p
, struct pt_regs
*regs
)
413 u32
*addr
= (u32
*) regs
->tpc
;
415 if (addr
== (u32
*) jprobe_return_trap_instruction
) {
416 if (jprobe_saved_regs_location
!= regs
) {
417 printk("JPROBE: Current regs (%p) does not match "
418 "saved regs (%p).\n",
419 regs
, jprobe_saved_regs_location
);
420 printk("JPROBE: Saved registers\n");
421 __show_regs(jprobe_saved_regs_location
);
422 printk("JPROBE: Current registers\n");
426 /* Restore old register state. Do pt_regs
427 * first so that UREG_FP is the original one for
428 * the stack frame restore.
430 memcpy(regs
, &jprobe_saved_regs
, sizeof(*regs
));
432 memcpy((char *) (regs
->u_regs
[UREG_FP
] + STACK_BIAS
),
434 sizeof(jprobe_saved_stack
));
441 /* architecture specific initialization */
442 int arch_init_kprobes(void)