1 // SPDX-License-Identifier: GPL-2.0+
3 #include <linux/kprobes.h>
4 #include <linux/extable.h>
5 #include <linux/slab.h>
6 #include <linux/stop_machine.h>
7 #include <asm/ptrace.h>
8 #include <linux/uaccess.h>
9 #include <asm/sections.h>
10 #include <asm/cacheflush.h>
12 #include <asm/patch.h>
14 #include "decode-insn.h"
16 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
17 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
20 post_kprobe_handler(struct kprobe_ctlblk *, struct pt_regs *);
22 static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
24 unsigned long offset = GET_INSN_LENGTH(p->opcode);
26 p->ainsn.api.restore = (unsigned long)p->addr + offset;
28 patch_text(p->ainsn.api.insn, p->opcode);
29 patch_text((void *)((unsigned long)(p->ainsn.api.insn) + offset),
33 static void __kprobes arch_prepare_simulate(struct kprobe *p)
35 p->ainsn.api.restore = 0;
38 static void __kprobes arch_simulate_insn(struct kprobe *p, struct pt_regs *regs)
40 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
42 if (p->ainsn.api.handler)
43 p->ainsn.api.handler((u32)p->opcode,
44 (unsigned long)p->addr, regs);
46 post_kprobe_handler(kcb, regs);
49 int __kprobes arch_prepare_kprobe(struct kprobe *p)
51 unsigned long probe_addr = (unsigned long)p->addr;
53 if (probe_addr & 0x1) {
54 pr_warn("Address not aligned.\n");
59 /* copy instruction */
62 /* decode instruction */
63 switch (riscv_probe_decode_insn(p->addr, &p->ainsn.api)) {
64 case INSN_REJECTED: /* insn not supported */
67 case INSN_GOOD_NO_SLOT: /* insn need simulation */
68 p->ainsn.api.insn = NULL;
71 case INSN_GOOD: /* instruction uses slot */
72 p->ainsn.api.insn = get_insn_slot();
73 if (!p->ainsn.api.insn)
78 /* prepare the instruction */
79 if (p->ainsn.api.insn)
80 arch_prepare_ss_slot(p);
82 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));
95 /* install breakpoint in text */
96 void __kprobes arch_arm_kprobe(struct kprobe *p)
98 if ((p->opcode & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
99 patch_text(p->addr, __BUG_INSN_32);
101 patch_text(p->addr, __BUG_INSN_16);
104 /* remove breakpoint from text */
105 void __kprobes arch_disarm_kprobe(struct kprobe *p)
107 patch_text(p->addr, p->opcode);
110 void __kprobes arch_remove_kprobe(struct kprobe *p)
114 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
116 kcb->prev_kprobe.kp = kprobe_running();
117 kcb->prev_kprobe.status = kcb->kprobe_status;
120 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
122 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
123 kcb->kprobe_status = kcb->prev_kprobe.status;
126 static void __kprobes set_current_kprobe(struct kprobe *p)
128 __this_cpu_write(current_kprobe, p);
132 * Interrupts need to be disabled before single-step mode is set, and not
133 * reenabled until after single-step mode ends.
134 * Without disabling interrupt on local CPU, there is a chance of
135 * interrupt occurrence in the period of exception return and start of
136 * out-of-line single-step, that result in wrongly single stepping
137 * into the interrupt handler.
139 static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
140 struct pt_regs *regs)
142 kcb->saved_status = regs->status;
143 regs->status &= ~SR_SPIE;
146 static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
147 struct pt_regs *regs)
149 regs->status = kcb->saved_status;
152 static void __kprobes
153 set_ss_context(struct kprobe_ctlblk *kcb, unsigned long addr, struct kprobe *p)
155 unsigned long offset = GET_INSN_LENGTH(p->opcode);
157 kcb->ss_ctx.ss_pending = true;
158 kcb->ss_ctx.match_addr = addr + offset;
161 static void __kprobes clear_ss_context(struct kprobe_ctlblk *kcb)
163 kcb->ss_ctx.ss_pending = false;
164 kcb->ss_ctx.match_addr = 0;
167 static void __kprobes setup_singlestep(struct kprobe *p,
168 struct pt_regs *regs,
169 struct kprobe_ctlblk *kcb, int reenter)
174 save_previous_kprobe(kcb);
175 set_current_kprobe(p);
176 kcb->kprobe_status = KPROBE_REENTER;
178 kcb->kprobe_status = KPROBE_HIT_SS;
181 if (p->ainsn.api.insn) {
182 /* prepare for single stepping */
183 slot = (unsigned long)p->ainsn.api.insn;
185 set_ss_context(kcb, slot, p); /* mark pending ss */
187 /* IRQs and single stepping do not mix well. */
188 kprobes_save_local_irqflag(kcb, regs);
190 instruction_pointer_set(regs, slot);
192 /* insn simulation */
193 arch_simulate_insn(p, regs);
197 static int __kprobes reenter_kprobe(struct kprobe *p,
198 struct pt_regs *regs,
199 struct kprobe_ctlblk *kcb)
201 switch (kcb->kprobe_status) {
202 case KPROBE_HIT_SSDONE:
203 case KPROBE_HIT_ACTIVE:
204 kprobes_inc_nmissed_count(p);
205 setup_singlestep(p, regs, kcb, 1);
209 pr_warn("Unrecoverable kprobe detected.\n");
221 static void __kprobes
222 post_kprobe_handler(struct kprobe_ctlblk *kcb, struct pt_regs *regs)
224 struct kprobe *cur = kprobe_running();
229 /* return addr restore if non-branching insn */
230 if (cur->ainsn.api.restore != 0)
231 regs->epc = cur->ainsn.api.restore;
233 /* restore back original saved kprobe variables and continue */
234 if (kcb->kprobe_status == KPROBE_REENTER) {
235 restore_previous_kprobe(kcb);
239 /* call post handler */
240 kcb->kprobe_status = KPROBE_HIT_SSDONE;
241 if (cur->post_handler) {
242 /* post_handler can hit breakpoint and single step
243 * again, so we enable D-flag for recursive exception.
245 cur->post_handler(cur, regs, 0);
248 reset_current_kprobe();
251 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int trapnr)
253 struct kprobe *cur = kprobe_running();
254 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
256 switch (kcb->kprobe_status) {
260 * We are here because the instruction being single
261 * stepped caused a page fault. We reset the current
262 * kprobe and the ip points back to the probe address
263 * and allow the page fault handler to continue as a
266 regs->epc = (unsigned long) cur->addr;
267 BUG_ON(!instruction_pointer(regs));
269 if (kcb->kprobe_status == KPROBE_REENTER)
270 restore_previous_kprobe(kcb);
272 kprobes_restore_local_irqflag(kcb, regs);
273 reset_current_kprobe();
277 case KPROBE_HIT_ACTIVE:
278 case KPROBE_HIT_SSDONE:
280 * We increment the nmissed count for accounting,
281 * we can also use npre/npostfault count for accounting
282 * these specific fault cases.
284 kprobes_inc_nmissed_count(cur);
287 * We come here because instructions in the pre/post
288 * handler caused the page_fault, this could happen
289 * if handler tries to access user space by
290 * copy_from_user(), get_user() etc. Let the
291 * user-specified handler try to fix it first.
293 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
297 * In case the user-specified fault handler returned
298 * zero, try to fix up.
300 if (fixup_exception(regs))
307 kprobe_breakpoint_handler(struct pt_regs *regs)
309 struct kprobe *p, *cur_kprobe;
310 struct kprobe_ctlblk *kcb;
311 unsigned long addr = instruction_pointer(regs);
313 kcb = get_kprobe_ctlblk();
314 cur_kprobe = kprobe_running();
316 p = get_kprobe((kprobe_opcode_t *) addr);
320 if (reenter_kprobe(p, regs, kcb))
324 set_current_kprobe(p);
325 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
328 * If we have no pre-handler or it returned 0, we
329 * continue with normal processing. If we have a
330 * pre-handler and it returned non-zero, it will
331 * modify the execution path and no need to single
332 * stepping. Let's just reset current kprobe and exit.
334 * pre_handler can hit a breakpoint and can step thru
337 if (!p->pre_handler || !p->pre_handler(p, regs))
338 setup_singlestep(p, regs, kcb, 0);
340 reset_current_kprobe();
346 * The breakpoint instruction was removed right
347 * after we hit it. Another cpu has removed
348 * either a probepoint or a debugger breakpoint
349 * at this address. In either case, no further
350 * handling of this interrupt is appropriate.
351 * Return back to original instruction, and continue.
357 kprobe_single_step_handler(struct pt_regs *regs)
359 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
361 if ((kcb->ss_ctx.ss_pending)
362 && (kcb->ss_ctx.match_addr == instruction_pointer(regs))) {
363 clear_ss_context(kcb); /* clear pending ss */
365 kprobes_restore_local_irqflag(kcb, regs);
367 post_kprobe_handler(kcb, regs);
374 * Provide a blacklist of symbols identifying ranges which cannot be kprobed.
375 * This blacklist is exposed to userspace via debugfs (kprobes/blacklist).
377 int __init arch_populate_kprobe_blacklist(void)
381 ret = kprobe_add_area_blacklist((unsigned long)__irqentry_text_start,
382 (unsigned long)__irqentry_text_end);
386 void __kprobes __used *trampoline_probe_handler(struct pt_regs *regs)
388 return (void *)kretprobe_trampoline_handler(regs, &kretprobe_trampoline, NULL);
391 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
392 struct pt_regs *regs)
394 ri->ret_addr = (kprobe_opcode_t *)regs->ra;
396 regs->ra = (unsigned long) &kretprobe_trampoline;
399 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
404 int __init arch_init_kprobes(void)