1 // SPDX-License-Identifier: GPL-2.0+
3 #define pr_fmt(fmt) "kprobes: " fmt
5 #include <linux/kprobes.h>
6 #include <linux/extable.h>
7 #include <linux/slab.h>
8 #include <linux/stop_machine.h>
9 #include <asm/ptrace.h>
10 #include <linux/uaccess.h>
11 #include <asm/sections.h>
12 #include <asm/cacheflush.h>
14 #include <asm/patch.h>
16 #include "decode-insn.h"
18 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
19 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
22 post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
24 static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
26 unsigned long offset = GET_INSN_LENGTH(p->opcode);
28 p->ainsn.api.restore = (unsigned long)p->addr + offset;
30 patch_text(p->ainsn.api.insn, p->opcode);
31 patch_text((void *)((unsigned long)(p->ainsn.api.insn) + offset),
35 static void __kprobes arch_prepare_simulate(struct kprobe *p)
37 p->ainsn.api.restore = 0;
40 static void __kprobes arch_simulate_insn(struct kprobe *p, struct pt_regs *regs)
42 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
44 if (p->ainsn.api.handler)
45 p->ainsn.api.handler((u32)p->opcode,
46 (unsigned long)p->addr, regs);
48 post_kprobe_handler(p, kcb, regs);
51 int __kprobes arch_prepare_kprobe(struct kprobe *p)
53 unsigned long probe_addr = (unsigned long)p->addr;
58 /* copy instruction */
61 /* decode instruction */
62 switch (riscv_probe_decode_insn(p->addr, &p->ainsn.api)) {
63 case INSN_REJECTED: /* insn not supported */
66 case INSN_GOOD_NO_SLOT: /* insn need simulation */
67 p->ainsn.api.insn = NULL;
70 case INSN_GOOD: /* instruction uses slot */
71 p->ainsn.api.insn = get_insn_slot();
72 if (!p->ainsn.api.insn)
77 /* prepare the instruction */
78 if (p->ainsn.api.insn)
79 arch_prepare_ss_slot(p);
81 arch_prepare_simulate(p);
87 void *alloc_insn_page(void)
89 return __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, VMALLOC_END,
90 GFP_KERNEL, PAGE_KERNEL_READ_EXEC,
91 VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
92 __builtin_return_address(0));
96 /* install breakpoint in text */
97 void __kprobes arch_arm_kprobe(struct kprobe *p)
99 if ((p->opcode & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
100 patch_text(p->addr, __BUG_INSN_32);
102 patch_text(p->addr, __BUG_INSN_16);
105 /* remove breakpoint from text */
106 void __kprobes arch_disarm_kprobe(struct kprobe *p)
108 patch_text(p->addr, p->opcode);
111 void __kprobes arch_remove_kprobe(struct kprobe *p)
115 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
117 kcb->prev_kprobe.kp = kprobe_running();
118 kcb->prev_kprobe.status = kcb->kprobe_status;
121 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
123 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
124 kcb->kprobe_status = kcb->prev_kprobe.status;
127 static void __kprobes set_current_kprobe(struct kprobe *p)
129 __this_cpu_write(current_kprobe, p);
133 * Interrupts need to be disabled before single-step mode is set, and not
134 * reenabled until after single-step mode ends.
135 * Without disabling interrupt on local CPU, there is a chance of
136 * interrupt occurrence in the period of exception return and start of
137 * out-of-line single-step, that result in wrongly single stepping
138 * into the interrupt handler.
140 static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
141 struct pt_regs *regs)
143 kcb->saved_status = regs->status;
144 regs->status &= ~SR_SPIE;
147 static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
148 struct pt_regs *regs)
150 regs->status = kcb->saved_status;
153 static void __kprobes setup_singlestep(struct kprobe *p,
154 struct pt_regs *regs,
155 struct kprobe_ctlblk *kcb, int reenter)
160 save_previous_kprobe(kcb);
161 set_current_kprobe(p);
162 kcb->kprobe_status = KPROBE_REENTER;
164 kcb->kprobe_status = KPROBE_HIT_SS;
167 if (p->ainsn.api.insn) {
168 /* prepare for single stepping */
169 slot = (unsigned long)p->ainsn.api.insn;
171 /* IRQs and single stepping do not mix well. */
172 kprobes_save_local_irqflag(kcb, regs);
174 instruction_pointer_set(regs, slot);
176 /* insn simulation */
177 arch_simulate_insn(p, regs);
181 static int __kprobes reenter_kprobe(struct kprobe *p,
182 struct pt_regs *regs,
183 struct kprobe_ctlblk *kcb)
185 switch (kcb->kprobe_status) {
186 case KPROBE_HIT_SSDONE:
187 case KPROBE_HIT_ACTIVE:
188 kprobes_inc_nmissed_count(p);
189 setup_singlestep(p, regs, kcb, 1);
193 pr_warn("Failed to recover from reentered kprobes.\n");
205 static void __kprobes
206 post_kprobe_handler(struct kprobe *cur, struct kprobe_ctlblk *kcb, struct pt_regs *regs)
208 /* return addr restore if non-branching insn */
209 if (cur->ainsn.api.restore != 0)
210 regs->epc = cur->ainsn.api.restore;
212 /* restore back original saved kprobe variables and continue */
213 if (kcb->kprobe_status == KPROBE_REENTER) {
214 restore_previous_kprobe(kcb);
218 /* call post handler */
219 kcb->kprobe_status = KPROBE_HIT_SSDONE;
220 if (cur->post_handler) {
221 /* post_handler can hit breakpoint and single step
222 * again, so we enable D-flag for recursive exception.
224 cur->post_handler(cur, regs, 0);
227 reset_current_kprobe();
230 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int trapnr)
232 struct kprobe *cur = kprobe_running();
233 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
235 switch (kcb->kprobe_status) {
239 * We are here because the instruction being single
240 * stepped caused a page fault. We reset the current
241 * kprobe and the ip points back to the probe address
242 * and allow the page fault handler to continue as a
245 regs->epc = (unsigned long) cur->addr;
246 BUG_ON(!instruction_pointer(regs));
248 if (kcb->kprobe_status == KPROBE_REENTER)
249 restore_previous_kprobe(kcb);
251 kprobes_restore_local_irqflag(kcb, regs);
252 reset_current_kprobe();
256 case KPROBE_HIT_ACTIVE:
257 case KPROBE_HIT_SSDONE:
259 * In case the user-specified fault handler returned
260 * zero, try to fix up.
262 if (fixup_exception(regs))
269 kprobe_breakpoint_handler(struct pt_regs *regs)
271 struct kprobe *p, *cur_kprobe;
272 struct kprobe_ctlblk *kcb;
273 unsigned long addr = instruction_pointer(regs);
275 kcb = get_kprobe_ctlblk();
276 cur_kprobe = kprobe_running();
278 p = get_kprobe((kprobe_opcode_t *) addr);
282 if (reenter_kprobe(p, regs, kcb))
286 set_current_kprobe(p);
287 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
290 * If we have no pre-handler or it returned 0, we
291 * continue with normal processing. If we have a
292 * pre-handler and it returned non-zero, it will
293 * modify the execution path and no need to single
294 * stepping. Let's just reset current kprobe and exit.
296 * pre_handler can hit a breakpoint and can step thru
299 if (!p->pre_handler || !p->pre_handler(p, regs))
300 setup_singlestep(p, regs, kcb, 0);
302 reset_current_kprobe();
308 * The breakpoint instruction was removed right
309 * after we hit it. Another cpu has removed
310 * either a probepoint or a debugger breakpoint
311 * at this address. In either case, no further
312 * handling of this interrupt is appropriate.
313 * Return back to original instruction, and continue.
319 kprobe_single_step_handler(struct pt_regs *regs)
321 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
322 unsigned long addr = instruction_pointer(regs);
323 struct kprobe *cur = kprobe_running();
325 if (cur && (kcb->kprobe_status & (KPROBE_HIT_SS | KPROBE_REENTER)) &&
326 ((unsigned long)&cur->ainsn.api.insn[0] + GET_INSN_LENGTH(cur->opcode) == addr)) {
327 kprobes_restore_local_irqflag(kcb, regs);
328 post_kprobe_handler(cur, kcb, regs);
331 /* not ours, kprobes should ignore it */
336 * Provide a blacklist of symbols identifying ranges which cannot be kprobed.
337 * This blacklist is exposed to userspace via debugfs (kprobes/blacklist).
339 int __init arch_populate_kprobe_blacklist(void)
343 ret = kprobe_add_area_blacklist((unsigned long)__irqentry_text_start,
344 (unsigned long)__irqentry_text_end);
348 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
353 int __init arch_init_kprobes(void)