1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/highmem.h>
3 #include <linux/kdebug.h>
4 #include <linux/types.h>
5 #include <linux/notifier.h>
6 #include <linux/sched.h>
7 #include <linux/uprobes.h>
9 #include <asm/branch.h>
10 #include <asm/cpu-features.h>
11 #include <asm/ptrace.h>
13 #include "probes-common.h"
15 static inline int insn_has_delay_slot(const union mips_instruction insn
)
17 return __insn_has_delay_slot(insn
);
21 * arch_uprobe_analyze_insn - instruction analysis including validity and fixups.
22 * @mm: the probed address space.
23 * @arch_uprobe: the probepoint information.
24 * @addr: virtual address at which to install the probepoint
25 * Return 0 on success or a -ve number on error.
27 int arch_uprobe_analyze_insn(struct arch_uprobe
*aup
,
28 struct mm_struct
*mm
, unsigned long addr
)
30 union mips_instruction inst
;
33 * For the time being this also blocks attempts to use uprobes with
34 * MIPS16 and microMIPS.
39 inst
.word
= aup
->insn
[0];
41 if (__insn_is_compact_branch(inst
)) {
42 pr_notice("Uprobes for compact branches are not supported\n");
46 aup
->ixol
[0] = aup
->insn
[insn_has_delay_slot(inst
)];
47 aup
->ixol
[1] = UPROBE_BRK_UPROBE_XOL
; /* NOP */
53 * is_trap_insn - check if the instruction is a trap variant
54 * @insn: instruction to be checked.
55 * Returns true if @insn is a trap variant.
57 * This definition overrides the weak definition in kernel/events/uprobes.c.
58 * and is needed for the case where an architecture has multiple trap
59 * instructions (like PowerPC or MIPS). We treat BREAK just like the more
60 * modern conditional trap instructions.
62 bool is_trap_insn(uprobe_opcode_t
*insn
)
64 union mips_instruction inst
;
68 switch (inst
.i_format
.opcode
) {
70 switch (inst
.r_format
.func
) {
82 case bcond_op
: /* Yes, really ... */
83 switch (inst
.u_format
.rt
) {
98 #define UPROBE_TRAP_NR ULONG_MAX
101 * arch_uprobe_pre_xol - prepare to execute out of line.
102 * @auprobe: the probepoint information.
103 * @regs: reflects the saved user state of current task.
105 int arch_uprobe_pre_xol(struct arch_uprobe
*aup
, struct pt_regs
*regs
)
107 struct uprobe_task
*utask
= current
->utask
;
110 * Now find the EPC where to resume after the breakpoint has been
111 * dealt with. This may require emulation of a branch.
113 aup
->resume_epc
= regs
->cp0_epc
+ 4;
114 if (insn_has_delay_slot((union mips_instruction
) aup
->insn
[0])) {
118 __compute_return_epc_for_insn(regs
,
119 (union mips_instruction
) aup
->insn
[0]);
120 aup
->resume_epc
= regs
->cp0_epc
;
122 utask
->autask
.saved_trap_nr
= current
->thread
.trap_nr
;
123 current
->thread
.trap_nr
= UPROBE_TRAP_NR
;
124 regs
->cp0_epc
= current
->utask
->xol_vaddr
;
129 int arch_uprobe_post_xol(struct arch_uprobe
*aup
, struct pt_regs
*regs
)
131 struct uprobe_task
*utask
= current
->utask
;
133 current
->thread
.trap_nr
= utask
->autask
.saved_trap_nr
;
134 regs
->cp0_epc
= aup
->resume_epc
;
140 * If xol insn itself traps and generates a signal(Say,
141 * SIGILL/SIGSEGV/etc), then detect the case where a singlestepped
142 * instruction jumps back to its own address. It is assumed that anything
143 * like do_page_fault/do_trap/etc sets thread.trap_nr != -1.
145 * arch_uprobe_pre_xol/arch_uprobe_post_xol save/restore thread.trap_nr,
146 * arch_uprobe_xol_was_trapped() simply checks that ->trap_nr is not equal to
147 * UPROBE_TRAP_NR == -1 set by arch_uprobe_pre_xol().
149 bool arch_uprobe_xol_was_trapped(struct task_struct
*tsk
)
151 if (tsk
->thread
.trap_nr
!= UPROBE_TRAP_NR
)
157 int arch_uprobe_exception_notify(struct notifier_block
*self
,
158 unsigned long val
, void *data
)
160 struct die_args
*args
= data
;
161 struct pt_regs
*regs
= args
->regs
;
163 /* regs == NULL is a kernel bug */
167 /* We are only interested in userspace traps */
168 if (!user_mode(regs
))
173 if (uprobe_pre_sstep_notifier(regs
))
177 if (uprobe_post_sstep_notifier(regs
))
187 * This function gets called when XOL instruction either gets trapped or
188 * the thread has a fatal signal. Reset the instruction pointer to its
189 * probed address for the potential restart or for post mortem analysis.
191 void arch_uprobe_abort_xol(struct arch_uprobe
*aup
,
192 struct pt_regs
*regs
)
194 struct uprobe_task
*utask
= current
->utask
;
196 instruction_pointer_set(regs
, utask
->vaddr
);
199 unsigned long arch_uretprobe_hijack_return_addr(
200 unsigned long trampoline_vaddr
, struct pt_regs
*regs
)
206 /* Replace the return address with the trampoline address */
207 regs
->regs
[31] = trampoline_vaddr
;
213 * set_swbp - store breakpoint at a given address.
214 * @auprobe: arch specific probepoint information.
215 * @mm: the probed process address space.
216 * @vaddr: the virtual address to insert the opcode.
218 * For mm @mm, store the breakpoint instruction at @vaddr.
219 * Return 0 (success) or a negative errno.
221 * This version overrides the weak version in kernel/events/uprobes.c.
222 * It is required to handle MIPS16 and microMIPS.
224 int __weak
set_swbp(struct arch_uprobe
*auprobe
, struct mm_struct
*mm
,
227 return uprobe_write_opcode(mm
, vaddr
, UPROBE_SWBP_INSN
);
230 void arch_uprobe_copy_ixol(struct page
*page
, unsigned long vaddr
,
231 void *src
, unsigned long len
)
233 unsigned long kaddr
, kstart
;
235 /* Initialize the slot */
236 kaddr
= (unsigned long)kmap_atomic(page
);
237 kstart
= kaddr
+ (vaddr
& ~PAGE_MASK
);
238 memcpy((void *)kstart
, src
, len
);
239 flush_icache_range(kstart
, kstart
+ len
);
240 kunmap_atomic((void *)kaddr
);
244 * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
245 * @regs: Reflects the saved state of the task after it has hit a breakpoint
247 * Return the address of the breakpoint instruction.
249 * This overrides the weak version in kernel/events/uprobes.c.
251 unsigned long uprobe_get_swbp_addr(struct pt_regs
*regs
)
253 return instruction_pointer(regs
);
257 * See if the instruction can be emulated.
258 * Returns true if instruction was emulated, false otherwise.
260 * For now we always emulate so this function just returns 0.
262 bool arch_uprobe_skip_sstep(struct arch_uprobe
*auprobe
, struct pt_regs
*regs
)