xref: /openbmc/linux/arch/riscv/kernel/probes/kprobes.c (revision 79e790ff)
1 // SPDX-License-Identifier: GPL-2.0+
2 
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>
11 #include <asm/bug.h>
12 #include <asm/patch.h>
13 
14 #include "decode-insn.h"
15 
16 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
17 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
18 
19 static void __kprobes
20 post_kprobe_handler(struct kprobe_ctlblk *, struct pt_regs *);
21 
22 static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
23 {
24 	unsigned long offset = GET_INSN_LENGTH(p->opcode);
25 
26 	p->ainsn.api.restore = (unsigned long)p->addr + offset;
27 
28 	patch_text(p->ainsn.api.insn, p->opcode);
29 	patch_text((void *)((unsigned long)(p->ainsn.api.insn) + offset),
30 		   __BUG_INSN_32);
31 }
32 
33 static void __kprobes arch_prepare_simulate(struct kprobe *p)
34 {
35 	p->ainsn.api.restore = 0;
36 }
37 
38 static void __kprobes arch_simulate_insn(struct kprobe *p, struct pt_regs *regs)
39 {
40 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
41 
42 	if (p->ainsn.api.handler)
43 		p->ainsn.api.handler((u32)p->opcode,
44 					(unsigned long)p->addr, regs);
45 
46 	post_kprobe_handler(kcb, regs);
47 }
48 
49 int __kprobes arch_prepare_kprobe(struct kprobe *p)
50 {
51 	unsigned long probe_addr = (unsigned long)p->addr;
52 
53 	if (probe_addr & 0x1) {
54 		pr_warn("Address not aligned.\n");
55 
56 		return -EINVAL;
57 	}
58 
59 	/* copy instruction */
60 	p->opcode = *p->addr;
61 
62 	/* decode instruction */
63 	switch (riscv_probe_decode_insn(p->addr, &p->ainsn.api)) {
64 	case INSN_REJECTED:	/* insn not supported */
65 		return -EINVAL;
66 
67 	case INSN_GOOD_NO_SLOT:	/* insn need simulation */
68 		p->ainsn.api.insn = NULL;
69 		break;
70 
71 	case INSN_GOOD:	/* instruction uses slot */
72 		p->ainsn.api.insn = get_insn_slot();
73 		if (!p->ainsn.api.insn)
74 			return -ENOMEM;
75 		break;
76 	}
77 
78 	/* prepare the instruction */
79 	if (p->ainsn.api.insn)
80 		arch_prepare_ss_slot(p);
81 	else
82 		arch_prepare_simulate(p);
83 
84 	return 0;
85 }
86 
87 void *alloc_insn_page(void)
88 {
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));
93 }
94 
95 /* install breakpoint in text */
96 void __kprobes arch_arm_kprobe(struct kprobe *p)
97 {
98 	if ((p->opcode & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
99 		patch_text(p->addr, __BUG_INSN_32);
100 	else
101 		patch_text(p->addr, __BUG_INSN_16);
102 }
103 
104 /* remove breakpoint from text */
105 void __kprobes arch_disarm_kprobe(struct kprobe *p)
106 {
107 	patch_text(p->addr, p->opcode);
108 }
109 
110 void __kprobes arch_remove_kprobe(struct kprobe *p)
111 {
112 }
113 
114 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
115 {
116 	kcb->prev_kprobe.kp = kprobe_running();
117 	kcb->prev_kprobe.status = kcb->kprobe_status;
118 }
119 
120 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
121 {
122 	__this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
123 	kcb->kprobe_status = kcb->prev_kprobe.status;
124 }
125 
126 static void __kprobes set_current_kprobe(struct kprobe *p)
127 {
128 	__this_cpu_write(current_kprobe, p);
129 }
130 
131 /*
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.
138  */
139 static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
140 						struct pt_regs *regs)
141 {
142 	kcb->saved_status = regs->status;
143 	regs->status &= ~SR_SPIE;
144 }
145 
146 static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
147 						struct pt_regs *regs)
148 {
149 	regs->status = kcb->saved_status;
150 }
151 
152 static void __kprobes
153 set_ss_context(struct kprobe_ctlblk *kcb, unsigned long addr, struct kprobe *p)
154 {
155 	unsigned long offset = GET_INSN_LENGTH(p->opcode);
156 
157 	kcb->ss_ctx.ss_pending = true;
158 	kcb->ss_ctx.match_addr = addr + offset;
159 }
160 
161 static void __kprobes clear_ss_context(struct kprobe_ctlblk *kcb)
162 {
163 	kcb->ss_ctx.ss_pending = false;
164 	kcb->ss_ctx.match_addr = 0;
165 }
166 
167 static void __kprobes setup_singlestep(struct kprobe *p,
168 				       struct pt_regs *regs,
169 				       struct kprobe_ctlblk *kcb, int reenter)
170 {
171 	unsigned long slot;
172 
173 	if (reenter) {
174 		save_previous_kprobe(kcb);
175 		set_current_kprobe(p);
176 		kcb->kprobe_status = KPROBE_REENTER;
177 	} else {
178 		kcb->kprobe_status = KPROBE_HIT_SS;
179 	}
180 
181 	if (p->ainsn.api.insn) {
182 		/* prepare for single stepping */
183 		slot = (unsigned long)p->ainsn.api.insn;
184 
185 		set_ss_context(kcb, slot, p);	/* mark pending ss */
186 
187 		/* IRQs and single stepping do not mix well. */
188 		kprobes_save_local_irqflag(kcb, regs);
189 
190 		instruction_pointer_set(regs, slot);
191 	} else {
192 		/* insn simulation */
193 		arch_simulate_insn(p, regs);
194 	}
195 }
196 
197 static int __kprobes reenter_kprobe(struct kprobe *p,
198 				    struct pt_regs *regs,
199 				    struct kprobe_ctlblk *kcb)
200 {
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);
206 		break;
207 	case KPROBE_HIT_SS:
208 	case KPROBE_REENTER:
209 		pr_warn("Unrecoverable kprobe detected.\n");
210 		dump_kprobe(p);
211 		BUG();
212 		break;
213 	default:
214 		WARN_ON(1);
215 		return 0;
216 	}
217 
218 	return 1;
219 }
220 
221 static void __kprobes
222 post_kprobe_handler(struct kprobe_ctlblk *kcb, struct pt_regs *regs)
223 {
224 	struct kprobe *cur = kprobe_running();
225 
226 	if (!cur)
227 		return;
228 
229 	/* return addr restore if non-branching insn */
230 	if (cur->ainsn.api.restore != 0)
231 		regs->epc = cur->ainsn.api.restore;
232 
233 	/* restore back original saved kprobe variables and continue */
234 	if (kcb->kprobe_status == KPROBE_REENTER) {
235 		restore_previous_kprobe(kcb);
236 		return;
237 	}
238 
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.
244 		 */
245 		cur->post_handler(cur, regs, 0);
246 	}
247 
248 	reset_current_kprobe();
249 }
250 
251 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int trapnr)
252 {
253 	struct kprobe *cur = kprobe_running();
254 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
255 
256 	switch (kcb->kprobe_status) {
257 	case KPROBE_HIT_SS:
258 	case KPROBE_REENTER:
259 		/*
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
264 		 * normal page fault.
265 		 */
266 		regs->epc = (unsigned long) cur->addr;
267 		BUG_ON(!instruction_pointer(regs));
268 
269 		if (kcb->kprobe_status == KPROBE_REENTER)
270 			restore_previous_kprobe(kcb);
271 		else {
272 			kprobes_restore_local_irqflag(kcb, regs);
273 			reset_current_kprobe();
274 		}
275 
276 		break;
277 	case KPROBE_HIT_ACTIVE:
278 	case KPROBE_HIT_SSDONE:
279 		/*
280 		 * We increment the nmissed count for accounting,
281 		 * we can also use npre/npostfault count for accounting
282 		 * these specific fault cases.
283 		 */
284 		kprobes_inc_nmissed_count(cur);
285 
286 		/*
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.
292 		 */
293 		if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
294 			return 1;
295 
296 		/*
297 		 * In case the user-specified fault handler returned
298 		 * zero, try to fix up.
299 		 */
300 		if (fixup_exception(regs))
301 			return 1;
302 	}
303 	return 0;
304 }
305 
306 bool __kprobes
307 kprobe_breakpoint_handler(struct pt_regs *regs)
308 {
309 	struct kprobe *p, *cur_kprobe;
310 	struct kprobe_ctlblk *kcb;
311 	unsigned long addr = instruction_pointer(regs);
312 
313 	kcb = get_kprobe_ctlblk();
314 	cur_kprobe = kprobe_running();
315 
316 	p = get_kprobe((kprobe_opcode_t *) addr);
317 
318 	if (p) {
319 		if (cur_kprobe) {
320 			if (reenter_kprobe(p, regs, kcb))
321 				return true;
322 		} else {
323 			/* Probe hit */
324 			set_current_kprobe(p);
325 			kcb->kprobe_status = KPROBE_HIT_ACTIVE;
326 
327 			/*
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.
333 			 *
334 			 * pre_handler can hit a breakpoint and can step thru
335 			 * before return.
336 			 */
337 			if (!p->pre_handler || !p->pre_handler(p, regs))
338 				setup_singlestep(p, regs, kcb, 0);
339 			else
340 				reset_current_kprobe();
341 		}
342 		return true;
343 	}
344 
345 	/*
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.
352 	 */
353 	return false;
354 }
355 
356 bool __kprobes
357 kprobe_single_step_handler(struct pt_regs *regs)
358 {
359 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
360 
361 	if ((kcb->ss_ctx.ss_pending)
362 	    && (kcb->ss_ctx.match_addr == instruction_pointer(regs))) {
363 		clear_ss_context(kcb);	/* clear pending ss */
364 
365 		kprobes_restore_local_irqflag(kcb, regs);
366 
367 		post_kprobe_handler(kcb, regs);
368 		return true;
369 	}
370 	return false;
371 }
372 
373 /*
374  * Provide a blacklist of symbols identifying ranges which cannot be kprobed.
375  * This blacklist is exposed to userspace via debugfs (kprobes/blacklist).
376  */
377 int __init arch_populate_kprobe_blacklist(void)
378 {
379 	int ret;
380 
381 	ret = kprobe_add_area_blacklist((unsigned long)__irqentry_text_start,
382 					(unsigned long)__irqentry_text_end);
383 	return ret;
384 }
385 
386 void __kprobes __used *trampoline_probe_handler(struct pt_regs *regs)
387 {
388 	return (void *)kretprobe_trampoline_handler(regs, &kretprobe_trampoline, NULL);
389 }
390 
391 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
392 				      struct pt_regs *regs)
393 {
394 	ri->ret_addr = (kprobe_opcode_t *)regs->ra;
395 	ri->fp = NULL;
396 	regs->ra = (unsigned long) &kretprobe_trampoline;
397 }
398 
399 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
400 {
401 	return 0;
402 }
403 
404 int __init arch_init_kprobes(void)
405 {
406 	return 0;
407 }
408