1 // SPDX-License-Identifier: GPL-2.0-only
3 * Kernel Probes (KProbes)
4 * arch/mips/kernel/kprobes.c
6 * Copyright 2006 Sony Corp.
7 * Copyright 2010 Cavium Networks
9 * Some portions copied from the powerpc version.
11 * Copyright (C) IBM Corporation, 2002, 2004
14 #define pr_fmt(fmt) "kprobes: " fmt
16 #include <linux/kprobes.h>
17 #include <linux/preempt.h>
18 #include <linux/uaccess.h>
19 #include <linux/kdebug.h>
20 #include <linux/slab.h>
22 #include <asm/ptrace.h>
23 #include <asm/branch.h>
24 #include <asm/break.h>
26 #include "probes-common.h"
28 static const union mips_instruction breakpoint_insn
= {
31 .code
= BRK_KPROBE_BP
,
36 static const union mips_instruction breakpoint2_insn
= {
39 .code
= BRK_KPROBE_SSTEPBP
,
44 DEFINE_PER_CPU(struct kprobe
*, current_kprobe
);
45 DEFINE_PER_CPU(struct kprobe_ctlblk
, kprobe_ctlblk
);
47 static int insn_has_delayslot(union mips_instruction insn
)
49 return __insn_has_delay_slot(insn
);
51 NOKPROBE_SYMBOL(insn_has_delayslot
);
54 * insn_has_ll_or_sc function checks whether instruction is ll or sc
55 * one; putting breakpoint on top of atomic ll/sc pair is bad idea;
56 * so we need to prevent it and refuse kprobes insertion for such
57 * instructions; cannot do much about breakpoint in the middle of
58 * ll/sc pair; it is up to user to avoid those places
60 static int insn_has_ll_or_sc(union mips_instruction insn
)
64 switch (insn
.i_format
.opcode
) {
76 NOKPROBE_SYMBOL(insn_has_ll_or_sc
);
78 int arch_prepare_kprobe(struct kprobe
*p
)
80 union mips_instruction insn
;
81 union mips_instruction prev_insn
;
86 if (insn_has_ll_or_sc(insn
)) {
87 pr_notice("Kprobes for ll and sc instructions are not supported\n");
92 if (copy_from_kernel_nofault(&prev_insn
, p
->addr
- 1,
93 sizeof(mips_instruction
)) == 0 &&
94 insn_has_delayslot(prev_insn
)) {
95 pr_notice("Kprobes for branch delayslot are not supported\n");
100 if (__insn_is_compact_branch(insn
)) {
101 pr_notice("Kprobes for compact branches are not supported\n");
106 /* insn: must be on special executable page on mips. */
107 p
->ainsn
.insn
= get_insn_slot();
108 if (!p
->ainsn
.insn
) {
114 * In the kprobe->ainsn.insn[] array we store the original
115 * instruction at index zero and a break trap instruction at
118 * On MIPS arch if the instruction at probed address is a
119 * branch instruction, we need to execute the instruction at
120 * Branch Delayslot (BD) at the time of probe hit. As MIPS also
121 * doesn't have single stepping support, the BD instruction can
122 * not be executed in-line and it would be executed on SSOL slot
123 * using a normal breakpoint instruction in the next slot.
124 * So, read the instruction and save it for later execution.
126 if (insn_has_delayslot(insn
))
127 memcpy(&p
->ainsn
.insn
[0], p
->addr
+ 1, sizeof(kprobe_opcode_t
));
129 memcpy(&p
->ainsn
.insn
[0], p
->addr
, sizeof(kprobe_opcode_t
));
131 p
->ainsn
.insn
[1] = breakpoint2_insn
;
132 p
->opcode
= *p
->addr
;
137 NOKPROBE_SYMBOL(arch_prepare_kprobe
);
139 void arch_arm_kprobe(struct kprobe
*p
)
141 *p
->addr
= breakpoint_insn
;
144 NOKPROBE_SYMBOL(arch_arm_kprobe
);
146 void arch_disarm_kprobe(struct kprobe
*p
)
148 *p
->addr
= p
->opcode
;
151 NOKPROBE_SYMBOL(arch_disarm_kprobe
);
153 void arch_remove_kprobe(struct kprobe
*p
)
156 free_insn_slot(p
->ainsn
.insn
, 0);
157 p
->ainsn
.insn
= NULL
;
160 NOKPROBE_SYMBOL(arch_remove_kprobe
);
162 static void save_previous_kprobe(struct kprobe_ctlblk
*kcb
)
164 kcb
->prev_kprobe
.kp
= kprobe_running();
165 kcb
->prev_kprobe
.status
= kcb
->kprobe_status
;
166 kcb
->prev_kprobe
.old_SR
= kcb
->kprobe_old_SR
;
167 kcb
->prev_kprobe
.saved_SR
= kcb
->kprobe_saved_SR
;
168 kcb
->prev_kprobe
.saved_epc
= kcb
->kprobe_saved_epc
;
171 static void restore_previous_kprobe(struct kprobe_ctlblk
*kcb
)
173 __this_cpu_write(current_kprobe
, kcb
->prev_kprobe
.kp
);
174 kcb
->kprobe_status
= kcb
->prev_kprobe
.status
;
175 kcb
->kprobe_old_SR
= kcb
->prev_kprobe
.old_SR
;
176 kcb
->kprobe_saved_SR
= kcb
->prev_kprobe
.saved_SR
;
177 kcb
->kprobe_saved_epc
= kcb
->prev_kprobe
.saved_epc
;
180 static void set_current_kprobe(struct kprobe
*p
, struct pt_regs
*regs
,
181 struct kprobe_ctlblk
*kcb
)
183 __this_cpu_write(current_kprobe
, p
);
184 kcb
->kprobe_saved_SR
= kcb
->kprobe_old_SR
= (regs
->cp0_status
& ST0_IE
);
185 kcb
->kprobe_saved_epc
= regs
->cp0_epc
;
189 * evaluate_branch_instrucion -
191 * Evaluate the branch instruction at probed address during probe hit. The
192 * result of evaluation would be the updated epc. The insturction in delayslot
193 * would actually be single stepped using a normal breakpoint) on SSOL slot.
195 * The result is also saved in the kprobe control block for later use,
196 * in case we need to execute the delayslot instruction. The latter will be
197 * false for NOP instruction in dealyslot and the branch-likely instructions
198 * when the branch is taken. And for those cases we set a flag as
199 * SKIP_DELAYSLOT in the kprobe control block
201 static int evaluate_branch_instruction(struct kprobe
*p
, struct pt_regs
*regs
,
202 struct kprobe_ctlblk
*kcb
)
204 union mips_instruction insn
= p
->opcode
;
212 if (p
->ainsn
.insn
->word
== 0)
213 kcb
->flags
|= SKIP_DELAYSLOT
;
215 kcb
->flags
&= ~SKIP_DELAYSLOT
;
217 ret
= __compute_return_epc_for_insn(regs
, insn
);
221 if (ret
== BRANCH_LIKELY_TAKEN
)
222 kcb
->flags
|= SKIP_DELAYSLOT
;
224 kcb
->target_epc
= regs
->cp0_epc
;
229 pr_notice("Failed to emulate branch instruction because of unaligned epc - sending SIGBUS to %s.\n", current
->comm
);
235 static void prepare_singlestep(struct kprobe
*p
, struct pt_regs
*regs
,
236 struct kprobe_ctlblk
*kcb
)
240 regs
->cp0_status
&= ~ST0_IE
;
242 /* single step inline if the instruction is a break */
243 if (p
->opcode
.word
== breakpoint_insn
.word
||
244 p
->opcode
.word
== breakpoint2_insn
.word
)
245 regs
->cp0_epc
= (unsigned long)p
->addr
;
246 else if (insn_has_delayslot(p
->opcode
)) {
247 ret
= evaluate_branch_instruction(p
, regs
, kcb
);
251 regs
->cp0_epc
= (unsigned long)&p
->ainsn
.insn
[0];
255 * Called after single-stepping. p->addr is the address of the
256 * instruction whose first byte has been replaced by the "break 0"
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
263 * breakpoint trap. In case of branch instructions, the target
264 * epc to be restored.
266 static void resume_execution(struct kprobe
*p
,
267 struct pt_regs
*regs
,
268 struct kprobe_ctlblk
*kcb
)
270 if (insn_has_delayslot(p
->opcode
))
271 regs
->cp0_epc
= kcb
->target_epc
;
273 unsigned long orig_epc
= kcb
->kprobe_saved_epc
;
274 regs
->cp0_epc
= orig_epc
+ 4;
277 NOKPROBE_SYMBOL(resume_execution
);
279 static int kprobe_handler(struct pt_regs
*regs
)
283 kprobe_opcode_t
*addr
;
284 struct kprobe_ctlblk
*kcb
;
286 addr
= (kprobe_opcode_t
*) regs
->cp0_epc
;
289 * We don't want to be preempted for the entire
290 * duration of kprobe processing
293 kcb
= get_kprobe_ctlblk();
295 /* Check we're not actually recursing */
296 if (kprobe_running()) {
297 p
= get_kprobe(addr
);
299 if (kcb
->kprobe_status
== KPROBE_HIT_SS
&&
300 p
->ainsn
.insn
->word
== breakpoint_insn
.word
) {
301 regs
->cp0_status
&= ~ST0_IE
;
302 regs
->cp0_status
|= kcb
->kprobe_saved_SR
;
306 * We have reentered the kprobe_handler(), since
307 * another probe was hit while within the handler.
308 * We here save the original kprobes variables and
309 * just single step on the instruction of the new probe
310 * without calling any user handlers.
312 save_previous_kprobe(kcb
);
313 set_current_kprobe(p
, regs
, kcb
);
314 kprobes_inc_nmissed_count(p
);
315 prepare_singlestep(p
, regs
, kcb
);
316 kcb
->kprobe_status
= KPROBE_REENTER
;
317 if (kcb
->flags
& SKIP_DELAYSLOT
) {
318 resume_execution(p
, regs
, kcb
);
319 restore_previous_kprobe(kcb
);
320 preempt_enable_no_resched();
323 } else if (addr
->word
!= breakpoint_insn
.word
) {
325 * The breakpoint instruction was removed by
326 * another cpu right after we hit, no further
327 * handling of this interrupt is appropriate
334 p
= get_kprobe(addr
);
336 if (addr
->word
!= breakpoint_insn
.word
) {
338 * The breakpoint instruction was removed right
339 * after we hit it. Another cpu has removed
340 * either a probepoint or a debugger breakpoint
341 * at this address. In either case, no further
342 * handling of this interrupt is appropriate.
346 /* Not one of ours: let kernel handle it */
350 set_current_kprobe(p
, regs
, kcb
);
351 kcb
->kprobe_status
= KPROBE_HIT_ACTIVE
;
353 if (p
->pre_handler
&& p
->pre_handler(p
, regs
)) {
354 /* handler has already set things up, so skip ss setup */
355 reset_current_kprobe();
356 preempt_enable_no_resched();
360 prepare_singlestep(p
, regs
, kcb
);
361 if (kcb
->flags
& SKIP_DELAYSLOT
) {
362 kcb
->kprobe_status
= KPROBE_HIT_SSDONE
;
364 p
->post_handler(p
, regs
, 0);
365 resume_execution(p
, regs
, kcb
);
366 preempt_enable_no_resched();
368 kcb
->kprobe_status
= KPROBE_HIT_SS
;
373 preempt_enable_no_resched();
377 NOKPROBE_SYMBOL(kprobe_handler
);
379 static inline int post_kprobe_handler(struct pt_regs
*regs
)
381 struct kprobe
*cur
= kprobe_running();
382 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
387 if ((kcb
->kprobe_status
!= KPROBE_REENTER
) && cur
->post_handler
) {
388 kcb
->kprobe_status
= KPROBE_HIT_SSDONE
;
389 cur
->post_handler(cur
, regs
, 0);
392 resume_execution(cur
, regs
, kcb
);
394 regs
->cp0_status
|= kcb
->kprobe_saved_SR
;
396 /* Restore back the original saved kprobes variables and continue. */
397 if (kcb
->kprobe_status
== KPROBE_REENTER
) {
398 restore_previous_kprobe(kcb
);
401 reset_current_kprobe();
403 preempt_enable_no_resched();
408 int kprobe_fault_handler(struct pt_regs
*regs
, int trapnr
)
410 struct kprobe
*cur
= kprobe_running();
411 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
413 if (kcb
->kprobe_status
& KPROBE_HIT_SS
) {
414 resume_execution(cur
, regs
, kcb
);
415 regs
->cp0_status
|= kcb
->kprobe_old_SR
;
417 reset_current_kprobe();
418 preempt_enable_no_resched();
424 * Wrapper routine for handling exceptions.
426 int kprobe_exceptions_notify(struct notifier_block
*self
,
427 unsigned long val
, void *data
)
430 struct die_args
*args
= (struct die_args
*)data
;
431 int ret
= NOTIFY_DONE
;
435 if (kprobe_handler(args
->regs
))
439 if (post_kprobe_handler(args
->regs
))
444 /* kprobe_running() needs smp_processor_id() */
448 && kprobe_fault_handler(args
->regs
, args
->trapnr
))
457 NOKPROBE_SYMBOL(kprobe_exceptions_notify
);
460 * Function return probe trampoline:
461 * - init_kprobes() establishes a probepoint here
462 * - When the probed function returns, this probe causes the
465 static void __used
kretprobe_trampoline_holder(void)
469 /* Keep the assembler from reordering and placing JR here. */
472 ".global __kretprobe_trampoline\n"
473 "__kretprobe_trampoline:\n\t"
479 void __kretprobe_trampoline(void);
481 void arch_prepare_kretprobe(struct kretprobe_instance
*ri
,
482 struct pt_regs
*regs
)
484 ri
->ret_addr
= (kprobe_opcode_t
*) regs
->regs
[31];
487 /* Replace the return addr with trampoline addr */
488 regs
->regs
[31] = (unsigned long)__kretprobe_trampoline
;
490 NOKPROBE_SYMBOL(arch_prepare_kretprobe
);
493 * Called when the probe at kretprobe trampoline is hit
495 static int trampoline_probe_handler(struct kprobe
*p
,
496 struct pt_regs
*regs
)
498 instruction_pointer(regs
) = __kretprobe_trampoline_handler(regs
, NULL
);
500 * By returning a non-zero value, we are telling
501 * kprobe_handler() that we don't want the post_handler
502 * to run (and have re-enabled preemption)
506 NOKPROBE_SYMBOL(trampoline_probe_handler
);
508 int arch_trampoline_kprobe(struct kprobe
*p
)
510 if (p
->addr
== (kprobe_opcode_t
*)__kretprobe_trampoline
)
515 NOKPROBE_SYMBOL(arch_trampoline_kprobe
);
517 static struct kprobe trampoline_p
= {
518 .addr
= (kprobe_opcode_t
*)__kretprobe_trampoline
,
519 .pre_handler
= trampoline_probe_handler
522 int __init
arch_init_kprobes(void)
524 return register_kprobe(&trampoline_p
);