1 // SPDX-License-Identifier: GPL-2.0
3 * Kernel probes (kprobes) for SuperH
5 * Copyright (C) 2007 Chris Smith <chris.smith@st.com>
6 * Copyright (C) 2006 Lineo Solutions, Inc.
8 #include <linux/kprobes.h>
9 #include <linux/extable.h>
10 #include <linux/ptrace.h>
11 #include <linux/preempt.h>
12 #include <linux/kdebug.h>
13 #include <linux/slab.h>
14 #include <asm/cacheflush.h>
15 #include <linux/uaccess.h>
17 DEFINE_PER_CPU(struct kprobe
*, current_kprobe
) = NULL
;
18 DEFINE_PER_CPU(struct kprobe_ctlblk
, kprobe_ctlblk
);
20 static DEFINE_PER_CPU(struct kprobe
, saved_current_opcode
);
21 static DEFINE_PER_CPU(struct kprobe
, saved_next_opcode
);
22 static DEFINE_PER_CPU(struct kprobe
, saved_next_opcode2
);
24 #define OPCODE_JMP(x) (((x) & 0xF0FF) == 0x402b)
25 #define OPCODE_JSR(x) (((x) & 0xF0FF) == 0x400b)
26 #define OPCODE_BRA(x) (((x) & 0xF000) == 0xa000)
27 #define OPCODE_BRAF(x) (((x) & 0xF0FF) == 0x0023)
28 #define OPCODE_BSR(x) (((x) & 0xF000) == 0xb000)
29 #define OPCODE_BSRF(x) (((x) & 0xF0FF) == 0x0003)
31 #define OPCODE_BF_S(x) (((x) & 0xFF00) == 0x8f00)
32 #define OPCODE_BT_S(x) (((x) & 0xFF00) == 0x8d00)
34 #define OPCODE_BF(x) (((x) & 0xFF00) == 0x8b00)
35 #define OPCODE_BT(x) (((x) & 0xFF00) == 0x8900)
37 #define OPCODE_RTS(x) (((x) & 0x000F) == 0x000b)
38 #define OPCODE_RTE(x) (((x) & 0xFFFF) == 0x002b)
40 int __kprobes
arch_prepare_kprobe(struct kprobe
*p
)
42 kprobe_opcode_t opcode
= *(kprobe_opcode_t
*) (p
->addr
);
44 if (OPCODE_RTE(opcode
))
45 return -EFAULT
; /* Bad breakpoint */
52 void __kprobes
arch_copy_kprobe(struct kprobe
*p
)
54 memcpy(p
->ainsn
.insn
, p
->addr
, MAX_INSN_SIZE
* sizeof(kprobe_opcode_t
));
58 void __kprobes
arch_arm_kprobe(struct kprobe
*p
)
60 *p
->addr
= BREAKPOINT_INSTRUCTION
;
61 flush_icache_range((unsigned long)p
->addr
,
62 (unsigned long)p
->addr
+ sizeof(kprobe_opcode_t
));
65 void __kprobes
arch_disarm_kprobe(struct kprobe
*p
)
68 flush_icache_range((unsigned long)p
->addr
,
69 (unsigned long)p
->addr
+ sizeof(kprobe_opcode_t
));
72 int __kprobes
arch_trampoline_kprobe(struct kprobe
*p
)
74 if (*p
->addr
== BREAKPOINT_INSTRUCTION
)
81 * If an illegal slot instruction exception occurs for an address
82 * containing a kprobe, remove the probe.
84 * Returns 0 if the exception was handled successfully, 1 otherwise.
86 int __kprobes
kprobe_handle_illslot(unsigned long pc
)
88 struct kprobe
*p
= get_kprobe((kprobe_opcode_t
*) pc
+ 1);
91 printk("Warning: removing kprobe from delay slot: 0x%.8x\n",
92 (unsigned int)pc
+ 2);
100 void __kprobes
arch_remove_kprobe(struct kprobe
*p
)
102 struct kprobe
*saved
= this_cpu_ptr(&saved_next_opcode
);
105 arch_disarm_kprobe(p
);
106 arch_disarm_kprobe(saved
);
111 saved
= this_cpu_ptr(&saved_next_opcode2
);
113 arch_disarm_kprobe(saved
);
121 static void __kprobes
save_previous_kprobe(struct kprobe_ctlblk
*kcb
)
123 kcb
->prev_kprobe
.kp
= kprobe_running();
124 kcb
->prev_kprobe
.status
= kcb
->kprobe_status
;
127 static void __kprobes
restore_previous_kprobe(struct kprobe_ctlblk
*kcb
)
129 __this_cpu_write(current_kprobe
, kcb
->prev_kprobe
.kp
);
130 kcb
->kprobe_status
= kcb
->prev_kprobe
.status
;
133 static void __kprobes
set_current_kprobe(struct kprobe
*p
, struct pt_regs
*regs
,
134 struct kprobe_ctlblk
*kcb
)
136 __this_cpu_write(current_kprobe
, p
);
140 * Singlestep is implemented by disabling the current kprobe and setting one
141 * on the next instruction, following branches. Two probes are set if the
142 * branch is conditional.
144 static void __kprobes
prepare_singlestep(struct kprobe
*p
, struct pt_regs
*regs
)
146 __this_cpu_write(saved_current_opcode
.addr
, (kprobe_opcode_t
*)regs
->pc
);
149 struct kprobe
*op1
, *op2
;
151 arch_disarm_kprobe(p
);
153 op1
= this_cpu_ptr(&saved_next_opcode
);
154 op2
= this_cpu_ptr(&saved_next_opcode2
);
156 if (OPCODE_JSR(p
->opcode
) || OPCODE_JMP(p
->opcode
)) {
157 unsigned int reg_nr
= ((p
->opcode
>> 8) & 0x000F);
158 op1
->addr
= (kprobe_opcode_t
*) regs
->regs
[reg_nr
];
159 } else if (OPCODE_BRA(p
->opcode
) || OPCODE_BSR(p
->opcode
)) {
160 unsigned long disp
= (p
->opcode
& 0x0FFF);
162 (kprobe_opcode_t
*) (regs
->pc
+ 4 + disp
* 2);
164 } else if (OPCODE_BRAF(p
->opcode
) || OPCODE_BSRF(p
->opcode
)) {
165 unsigned int reg_nr
= ((p
->opcode
>> 8) & 0x000F);
167 (kprobe_opcode_t
*) (regs
->pc
+ 4 +
170 } else if (OPCODE_RTS(p
->opcode
)) {
171 op1
->addr
= (kprobe_opcode_t
*) regs
->pr
;
173 } else if (OPCODE_BF(p
->opcode
) || OPCODE_BT(p
->opcode
)) {
174 unsigned long disp
= (p
->opcode
& 0x00FF);
176 op1
->addr
= p
->addr
+ 1;
179 (kprobe_opcode_t
*) (regs
->pc
+ 4 + disp
* 2);
180 op2
->opcode
= *(op2
->addr
);
181 arch_arm_kprobe(op2
);
183 } else if (OPCODE_BF_S(p
->opcode
) || OPCODE_BT_S(p
->opcode
)) {
184 unsigned long disp
= (p
->opcode
& 0x00FF);
186 op1
->addr
= p
->addr
+ 2;
189 (kprobe_opcode_t
*) (regs
->pc
+ 4 + disp
* 2);
190 op2
->opcode
= *(op2
->addr
);
191 arch_arm_kprobe(op2
);
194 op1
->addr
= p
->addr
+ 1;
197 op1
->opcode
= *(op1
->addr
);
198 arch_arm_kprobe(op1
);
202 /* Called with kretprobe_lock held */
203 void __kprobes
arch_prepare_kretprobe(struct kretprobe_instance
*ri
,
204 struct pt_regs
*regs
)
206 ri
->ret_addr
= (kprobe_opcode_t
*) regs
->pr
;
208 /* Replace the return addr with trampoline addr */
209 regs
->pr
= (unsigned long)kretprobe_trampoline
;
212 static int __kprobes
kprobe_handler(struct pt_regs
*regs
)
216 kprobe_opcode_t
*addr
= NULL
;
217 struct kprobe_ctlblk
*kcb
;
220 * We don't want to be preempted for the entire
221 * duration of kprobe processing
224 kcb
= get_kprobe_ctlblk();
226 addr
= (kprobe_opcode_t
*) (regs
->pc
);
228 /* Check we're not actually recursing */
229 if (kprobe_running()) {
230 p
= get_kprobe(addr
);
232 if (kcb
->kprobe_status
== KPROBE_HIT_SS
&&
233 *p
->ainsn
.insn
== BREAKPOINT_INSTRUCTION
) {
236 /* We have reentered the kprobe_handler(), since
237 * another probe was hit while within the handler.
238 * We here save the original kprobes variables and
239 * just single step on the instruction of the new probe
240 * without calling any user handlers.
242 save_previous_kprobe(kcb
);
243 set_current_kprobe(p
, regs
, kcb
);
244 kprobes_inc_nmissed_count(p
);
245 prepare_singlestep(p
, regs
);
246 kcb
->kprobe_status
= KPROBE_REENTER
;
252 p
= get_kprobe(addr
);
254 /* Not one of ours: let kernel handle it */
255 if (*(kprobe_opcode_t
*)addr
!= BREAKPOINT_INSTRUCTION
) {
257 * The breakpoint instruction was removed right
258 * after we hit it. Another cpu has removed
259 * either a probepoint or a debugger breakpoint
260 * at this address. In either case, no further
261 * handling of this interrupt is appropriate.
269 set_current_kprobe(p
, regs
, kcb
);
270 kcb
->kprobe_status
= KPROBE_HIT_ACTIVE
;
272 if (p
->pre_handler
&& p
->pre_handler(p
, regs
)) {
273 /* handler has already set things up, so skip ss setup */
274 reset_current_kprobe();
275 preempt_enable_no_resched();
279 prepare_singlestep(p
, regs
);
280 kcb
->kprobe_status
= KPROBE_HIT_SS
;
284 preempt_enable_no_resched();
289 * For function-return probes, init_kprobes() establishes a probepoint
290 * here. When a retprobed function returns, this probe is hit and
291 * trampoline_probe_handler() runs, calling the kretprobe's handler.
293 static void __used
kretprobe_trampoline_holder(void)
295 asm volatile (".globl kretprobe_trampoline\n"
296 "kretprobe_trampoline:\n\t"
301 * Called when we hit the probe point at kretprobe_trampoline
303 int __kprobes
trampoline_probe_handler(struct kprobe
*p
, struct pt_regs
*regs
)
305 struct kretprobe_instance
*ri
= NULL
;
306 struct hlist_head
*head
, empty_rp
;
307 struct hlist_node
*tmp
;
308 unsigned long flags
, orig_ret_address
= 0;
309 unsigned long trampoline_address
= (unsigned long)&kretprobe_trampoline
;
311 INIT_HLIST_HEAD(&empty_rp
);
312 kretprobe_hash_lock(current
, &head
, &flags
);
315 * It is possible to have multiple instances associated with a given
316 * task either because an multiple functions in the call path
317 * have a return probe installed on them, and/or more then one return
318 * return probe was registered for a target function.
320 * We can handle this because:
321 * - instances are always inserted at the head of the list
322 * - when multiple return probes are registered for the same
323 * function, the first instance's ret_addr will point to the
324 * real return address, and all the rest will point to
325 * kretprobe_trampoline
327 hlist_for_each_entry_safe(ri
, tmp
, head
, hlist
) {
328 if (ri
->task
!= current
)
329 /* another task is sharing our hash bucket */
332 if (ri
->rp
&& ri
->rp
->handler
) {
333 __this_cpu_write(current_kprobe
, &ri
->rp
->kp
);
334 ri
->rp
->handler(ri
, regs
);
335 __this_cpu_write(current_kprobe
, NULL
);
338 orig_ret_address
= (unsigned long)ri
->ret_addr
;
339 recycle_rp_inst(ri
, &empty_rp
);
341 if (orig_ret_address
!= trampoline_address
)
343 * This is the real return address. Any other
344 * instances associated with this task are for
345 * other calls deeper on the call stack
350 kretprobe_assert(ri
, orig_ret_address
, trampoline_address
);
352 regs
->pc
= orig_ret_address
;
353 kretprobe_hash_unlock(current
, &flags
);
355 hlist_for_each_entry_safe(ri
, tmp
, &empty_rp
, hlist
) {
356 hlist_del(&ri
->hlist
);
360 return orig_ret_address
;
363 static int __kprobes
post_kprobe_handler(struct pt_regs
*regs
)
365 struct kprobe
*cur
= kprobe_running();
366 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
367 kprobe_opcode_t
*addr
= NULL
;
368 struct kprobe
*p
= NULL
;
373 if ((kcb
->kprobe_status
!= KPROBE_REENTER
) && cur
->post_handler
) {
374 kcb
->kprobe_status
= KPROBE_HIT_SSDONE
;
375 cur
->post_handler(cur
, regs
, 0);
378 p
= this_cpu_ptr(&saved_next_opcode
);
380 arch_disarm_kprobe(p
);
384 addr
= __this_cpu_read(saved_current_opcode
.addr
);
385 __this_cpu_write(saved_current_opcode
.addr
, NULL
);
387 p
= get_kprobe(addr
);
390 p
= this_cpu_ptr(&saved_next_opcode2
);
392 arch_disarm_kprobe(p
);
398 /* Restore back the original saved kprobes variables and continue. */
399 if (kcb
->kprobe_status
== KPROBE_REENTER
) {
400 restore_previous_kprobe(kcb
);
404 reset_current_kprobe();
407 preempt_enable_no_resched();
412 int __kprobes
kprobe_fault_handler(struct pt_regs
*regs
, int trapnr
)
414 struct kprobe
*cur
= kprobe_running();
415 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
416 const struct exception_table_entry
*entry
;
418 switch (kcb
->kprobe_status
) {
422 * We are here because the instruction being single
423 * stepped caused a page fault. We reset the current
424 * kprobe, point the pc back to the probe address
425 * and allow the page fault handler to continue as a
428 regs
->pc
= (unsigned long)cur
->addr
;
429 if (kcb
->kprobe_status
== KPROBE_REENTER
)
430 restore_previous_kprobe(kcb
);
432 reset_current_kprobe();
433 preempt_enable_no_resched();
435 case KPROBE_HIT_ACTIVE
:
436 case KPROBE_HIT_SSDONE
:
438 * We increment the nmissed count for accounting,
439 * we can also use npre/npostfault count for accounting
440 * these specific fault cases.
442 kprobes_inc_nmissed_count(cur
);
445 * We come here because instructions in the pre/post
446 * handler caused the page_fault, this could happen
447 * if handler tries to access user space by
448 * copy_from_user(), get_user() etc. Let the
449 * user-specified handler try to fix it first.
451 if (cur
->fault_handler
&& cur
->fault_handler(cur
, regs
, trapnr
))
455 * In case the user-specified fault handler returned
456 * zero, try to fix up.
458 if ((entry
= search_exception_tables(regs
->pc
)) != NULL
) {
459 regs
->pc
= entry
->fixup
;
464 * fixup_exception() could not handle it,
465 * Let do_page_fault() fix it.
476 * Wrapper routine to for handling exceptions.
478 int __kprobes
kprobe_exceptions_notify(struct notifier_block
*self
,
479 unsigned long val
, void *data
)
481 struct kprobe
*p
= NULL
;
482 struct die_args
*args
= (struct die_args
*)data
;
483 int ret
= NOTIFY_DONE
;
484 kprobe_opcode_t
*addr
= NULL
;
485 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
487 addr
= (kprobe_opcode_t
*) (args
->regs
->pc
);
488 if (val
== DIE_TRAP
&&
489 args
->trapnr
== (BREAKPOINT_INSTRUCTION
& 0xff)) {
490 if (!kprobe_running()) {
491 if (kprobe_handler(args
->regs
)) {
494 /* Not a kprobe trap */
498 p
= get_kprobe(addr
);
499 if ((kcb
->kprobe_status
== KPROBE_HIT_SS
) ||
500 (kcb
->kprobe_status
== KPROBE_REENTER
)) {
501 if (post_kprobe_handler(args
->regs
))
504 if (kprobe_handler(args
->regs
))
513 static struct kprobe trampoline_p
= {
514 .addr
= (kprobe_opcode_t
*)&kretprobe_trampoline
,
515 .pre_handler
= trampoline_probe_handler
518 int __init
arch_init_kprobes(void)
520 return register_kprobe(&trampoline_p
);