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 /* install breakpoint in text */
88 void __kprobes arch_arm_kprobe(struct kprobe *p)
90 if ((p->opcode & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
91 patch_text(p->addr, __BUG_INSN_32);
93 patch_text(p->addr, __BUG_INSN_16);
96 /* remove breakpoint from text */
97 void __kprobes arch_disarm_kprobe(struct kprobe *p)
99 patch_text(p->addr, p->opcode);
102 void __kprobes arch_remove_kprobe(struct kprobe *p)
106 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
108 kcb->prev_kprobe.kp = kprobe_running();
109 kcb->prev_kprobe.status = kcb->kprobe_status;
112 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
114 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
115 kcb->kprobe_status = kcb->prev_kprobe.status;
118 static void __kprobes set_current_kprobe(struct kprobe *p)
120 __this_cpu_write(current_kprobe, p);
124 * Interrupts need to be disabled before single-step mode is set, and not
125 * reenabled until after single-step mode ends.
126 * Without disabling interrupt on local CPU, there is a chance of
127 * interrupt occurrence in the period of exception return and start of
128 * out-of-line single-step, that result in wrongly single stepping
129 * into the interrupt handler.
131 static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
132 struct pt_regs *regs)
134 kcb->saved_status = regs->status;
135 regs->status &= ~SR_SPIE;
138 static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
139 struct pt_regs *regs)
141 regs->status = kcb->saved_status;
144 static void __kprobes
145 set_ss_context(struct kprobe_ctlblk *kcb, unsigned long addr, struct kprobe *p)
147 unsigned long offset = GET_INSN_LENGTH(p->opcode);
149 kcb->ss_ctx.ss_pending = true;
150 kcb->ss_ctx.match_addr = addr + offset;
153 static void __kprobes clear_ss_context(struct kprobe_ctlblk *kcb)
155 kcb->ss_ctx.ss_pending = false;
156 kcb->ss_ctx.match_addr = 0;
159 static void __kprobes setup_singlestep(struct kprobe *p,
160 struct pt_regs *regs,
161 struct kprobe_ctlblk *kcb, int reenter)
166 save_previous_kprobe(kcb);
167 set_current_kprobe(p);
168 kcb->kprobe_status = KPROBE_REENTER;
170 kcb->kprobe_status = KPROBE_HIT_SS;
173 if (p->ainsn.api.insn) {
174 /* prepare for single stepping */
175 slot = (unsigned long)p->ainsn.api.insn;
177 set_ss_context(kcb, slot, p); /* mark pending ss */
179 /* IRQs and single stepping do not mix well. */
180 kprobes_save_local_irqflag(kcb, regs);
182 instruction_pointer_set(regs, slot);
184 /* insn simulation */
185 arch_simulate_insn(p, regs);
189 static int __kprobes reenter_kprobe(struct kprobe *p,
190 struct pt_regs *regs,
191 struct kprobe_ctlblk *kcb)
193 switch (kcb->kprobe_status) {
194 case KPROBE_HIT_SSDONE:
195 case KPROBE_HIT_ACTIVE:
196 kprobes_inc_nmissed_count(p);
197 setup_singlestep(p, regs, kcb, 1);
201 pr_warn("Unrecoverable kprobe detected.\n");
213 static void __kprobes
214 post_kprobe_handler(struct kprobe_ctlblk *kcb, struct pt_regs *regs)
216 struct kprobe *cur = kprobe_running();
221 /* return addr restore if non-branching insn */
222 if (cur->ainsn.api.restore != 0)
223 regs->epc = cur->ainsn.api.restore;
225 /* restore back original saved kprobe variables and continue */
226 if (kcb->kprobe_status == KPROBE_REENTER) {
227 restore_previous_kprobe(kcb);
231 /* call post handler */
232 kcb->kprobe_status = KPROBE_HIT_SSDONE;
233 if (cur->post_handler) {
234 /* post_handler can hit breakpoint and single step
235 * again, so we enable D-flag for recursive exception.
237 cur->post_handler(cur, regs, 0);
240 reset_current_kprobe();
243 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int trapnr)
245 struct kprobe *cur = kprobe_running();
246 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
248 switch (kcb->kprobe_status) {
252 * We are here because the instruction being single
253 * stepped caused a page fault. We reset the current
254 * kprobe and the ip points back to the probe address
255 * and allow the page fault handler to continue as a
258 regs->epc = (unsigned long) cur->addr;
259 BUG_ON(!instruction_pointer(regs));
261 if (kcb->kprobe_status == KPROBE_REENTER)
262 restore_previous_kprobe(kcb);
264 reset_current_kprobe();
267 case KPROBE_HIT_ACTIVE:
268 case KPROBE_HIT_SSDONE:
270 * We increment the nmissed count for accounting,
271 * we can also use npre/npostfault count for accounting
272 * these specific fault cases.
274 kprobes_inc_nmissed_count(cur);
277 * We come here because instructions in the pre/post
278 * handler caused the page_fault, this could happen
279 * if handler tries to access user space by
280 * copy_from_user(), get_user() etc. Let the
281 * user-specified handler try to fix it first.
283 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
287 * In case the user-specified fault handler returned
288 * zero, try to fix up.
290 if (fixup_exception(regs))
297 kprobe_breakpoint_handler(struct pt_regs *regs)
299 struct kprobe *p, *cur_kprobe;
300 struct kprobe_ctlblk *kcb;
301 unsigned long addr = instruction_pointer(regs);
303 kcb = get_kprobe_ctlblk();
304 cur_kprobe = kprobe_running();
306 p = get_kprobe((kprobe_opcode_t *) addr);
310 if (reenter_kprobe(p, regs, kcb))
314 set_current_kprobe(p);
315 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
318 * If we have no pre-handler or it returned 0, we
319 * continue with normal processing. If we have a
320 * pre-handler and it returned non-zero, it will
321 * modify the execution path and no need to single
322 * stepping. Let's just reset current kprobe and exit.
324 * pre_handler can hit a breakpoint and can step thru
327 if (!p->pre_handler || !p->pre_handler(p, regs))
328 setup_singlestep(p, regs, kcb, 0);
330 reset_current_kprobe();
336 * The breakpoint instruction was removed right
337 * after we hit it. Another cpu has removed
338 * either a probepoint or a debugger breakpoint
339 * at this address. In either case, no further
340 * handling of this interrupt is appropriate.
341 * Return back to original instruction, and continue.
347 kprobe_single_step_handler(struct pt_regs *regs)
349 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
351 if ((kcb->ss_ctx.ss_pending)
352 && (kcb->ss_ctx.match_addr == instruction_pointer(regs))) {
353 clear_ss_context(kcb); /* clear pending ss */
355 kprobes_restore_local_irqflag(kcb, regs);
357 post_kprobe_handler(kcb, regs);
364 * Provide a blacklist of symbols identifying ranges which cannot be kprobed.
365 * This blacklist is exposed to userspace via debugfs (kprobes/blacklist).
367 int __init arch_populate_kprobe_blacklist(void)
371 ret = kprobe_add_area_blacklist((unsigned long)__irqentry_text_start,
372 (unsigned long)__irqentry_text_end);
376 void __kprobes __used *trampoline_probe_handler(struct pt_regs *regs)
378 return (void *)kretprobe_trampoline_handler(regs, &kretprobe_trampoline, NULL);
381 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
382 struct pt_regs *regs)
384 ri->ret_addr = (kprobe_opcode_t *)regs->ra;
386 regs->ra = (unsigned long) &kretprobe_trampoline;
389 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
394 int __init arch_init_kprobes(void)