xref: /openbmc/linux/arch/arm64/kernel/probes/kprobes.c (revision 25b892b5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * arch/arm64/kernel/probes/kprobes.c
4  *
5  * Kprobes support for ARM64
6  *
7  * Copyright (C) 2013 Linaro Limited.
8  * Author: Sandeepa Prabhu <sandeepa.prabhu@linaro.org>
9  */
10 #include <linux/extable.h>
11 #include <linux/kasan.h>
12 #include <linux/kernel.h>
13 #include <linux/kprobes.h>
14 #include <linux/sched/debug.h>
15 #include <linux/set_memory.h>
16 #include <linux/slab.h>
17 #include <linux/stop_machine.h>
18 #include <linux/stringify.h>
19 #include <linux/uaccess.h>
20 #include <linux/vmalloc.h>
21 
22 #include <asm/cacheflush.h>
23 #include <asm/daifflags.h>
24 #include <asm/debug-monitors.h>
25 #include <asm/insn.h>
26 #include <asm/irq.h>
27 #include <asm/patching.h>
28 #include <asm/ptrace.h>
29 #include <asm/sections.h>
30 #include <asm/system_misc.h>
31 #include <asm/traps.h>
32 
33 #include "decode-insn.h"
34 
35 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
36 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
37 
38 static void __kprobes
39 post_kprobe_handler(struct kprobe *, struct kprobe_ctlblk *, struct pt_regs *);
40 
41 static void __kprobes arch_prepare_ss_slot(struct kprobe *p)
42 {
43 	kprobe_opcode_t *addr = p->ainsn.api.insn;
44 	void *addrs[] = {addr, addr + 1};
45 	u32 insns[] = {p->opcode, BRK64_OPCODE_KPROBES_SS};
46 
47 	/* prepare insn slot */
48 	aarch64_insn_patch_text(addrs, insns, 2);
49 
50 	flush_icache_range((uintptr_t)addr, (uintptr_t)(addr + MAX_INSN_SIZE));
51 
52 	/*
53 	 * Needs restoring of return address after stepping xol.
54 	 */
55 	p->ainsn.api.restore = (unsigned long) p->addr +
56 	  sizeof(kprobe_opcode_t);
57 }
58 
59 static void __kprobes arch_prepare_simulate(struct kprobe *p)
60 {
61 	/* This instructions is not executed xol. No need to adjust the PC */
62 	p->ainsn.api.restore = 0;
63 }
64 
65 static void __kprobes arch_simulate_insn(struct kprobe *p, struct pt_regs *regs)
66 {
67 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
68 
69 	if (p->ainsn.api.handler)
70 		p->ainsn.api.handler((u32)p->opcode, (long)p->addr, regs);
71 
72 	/* single step simulated, now go for post processing */
73 	post_kprobe_handler(p, kcb, regs);
74 }
75 
76 int __kprobes arch_prepare_kprobe(struct kprobe *p)
77 {
78 	unsigned long probe_addr = (unsigned long)p->addr;
79 
80 	if (probe_addr & 0x3)
81 		return -EINVAL;
82 
83 	/* copy instruction */
84 	p->opcode = le32_to_cpu(*p->addr);
85 
86 	if (search_exception_tables(probe_addr))
87 		return -EINVAL;
88 
89 	/* decode instruction */
90 	switch (arm_kprobe_decode_insn(p->addr, &p->ainsn)) {
91 	case INSN_REJECTED:	/* insn not supported */
92 		return -EINVAL;
93 
94 	case INSN_GOOD_NO_SLOT:	/* insn need simulation */
95 		p->ainsn.api.insn = NULL;
96 		break;
97 
98 	case INSN_GOOD:	/* instruction uses slot */
99 		p->ainsn.api.insn = get_insn_slot();
100 		if (!p->ainsn.api.insn)
101 			return -ENOMEM;
102 		break;
103 	}
104 
105 	/* prepare the instruction */
106 	if (p->ainsn.api.insn)
107 		arch_prepare_ss_slot(p);
108 	else
109 		arch_prepare_simulate(p);
110 
111 	return 0;
112 }
113 
114 void *alloc_insn_page(void)
115 {
116 	return __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, VMALLOC_END,
117 			GFP_KERNEL, PAGE_KERNEL_ROX, VM_FLUSH_RESET_PERMS,
118 			NUMA_NO_NODE, __builtin_return_address(0));
119 }
120 
121 /* arm kprobe: install breakpoint in text */
122 void __kprobes arch_arm_kprobe(struct kprobe *p)
123 {
124 	void *addr = p->addr;
125 	u32 insn = BRK64_OPCODE_KPROBES;
126 
127 	aarch64_insn_patch_text(&addr, &insn, 1);
128 }
129 
130 /* disarm kprobe: remove breakpoint from text */
131 void __kprobes arch_disarm_kprobe(struct kprobe *p)
132 {
133 	void *addr = p->addr;
134 
135 	aarch64_insn_patch_text(&addr, &p->opcode, 1);
136 }
137 
138 void __kprobes arch_remove_kprobe(struct kprobe *p)
139 {
140 	if (p->ainsn.api.insn) {
141 		free_insn_slot(p->ainsn.api.insn, 0);
142 		p->ainsn.api.insn = NULL;
143 	}
144 }
145 
146 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
147 {
148 	kcb->prev_kprobe.kp = kprobe_running();
149 	kcb->prev_kprobe.status = kcb->kprobe_status;
150 }
151 
152 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
153 {
154 	__this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
155 	kcb->kprobe_status = kcb->prev_kprobe.status;
156 }
157 
158 static void __kprobes set_current_kprobe(struct kprobe *p)
159 {
160 	__this_cpu_write(current_kprobe, p);
161 }
162 
163 /*
164  * Mask all of DAIF while executing the instruction out-of-line, to keep things
165  * simple and avoid nesting exceptions. Interrupts do have to be disabled since
166  * the kprobe state is per-CPU and doesn't get migrated.
167  */
168 static void __kprobes kprobes_save_local_irqflag(struct kprobe_ctlblk *kcb,
169 						struct pt_regs *regs)
170 {
171 	kcb->saved_irqflag = regs->pstate & DAIF_MASK;
172 	regs->pstate |= DAIF_MASK;
173 }
174 
175 static void __kprobes kprobes_restore_local_irqflag(struct kprobe_ctlblk *kcb,
176 						struct pt_regs *regs)
177 {
178 	regs->pstate &= ~DAIF_MASK;
179 	regs->pstate |= kcb->saved_irqflag;
180 }
181 
182 static void __kprobes setup_singlestep(struct kprobe *p,
183 				       struct pt_regs *regs,
184 				       struct kprobe_ctlblk *kcb, int reenter)
185 {
186 	unsigned long slot;
187 
188 	if (reenter) {
189 		save_previous_kprobe(kcb);
190 		set_current_kprobe(p);
191 		kcb->kprobe_status = KPROBE_REENTER;
192 	} else {
193 		kcb->kprobe_status = KPROBE_HIT_SS;
194 	}
195 
196 
197 	if (p->ainsn.api.insn) {
198 		/* prepare for single stepping */
199 		slot = (unsigned long)p->ainsn.api.insn;
200 
201 		kprobes_save_local_irqflag(kcb, regs);
202 		instruction_pointer_set(regs, slot);
203 	} else {
204 		/* insn simulation */
205 		arch_simulate_insn(p, regs);
206 	}
207 }
208 
209 static int __kprobes reenter_kprobe(struct kprobe *p,
210 				    struct pt_regs *regs,
211 				    struct kprobe_ctlblk *kcb)
212 {
213 	switch (kcb->kprobe_status) {
214 	case KPROBE_HIT_SSDONE:
215 	case KPROBE_HIT_ACTIVE:
216 		kprobes_inc_nmissed_count(p);
217 		setup_singlestep(p, regs, kcb, 1);
218 		break;
219 	case KPROBE_HIT_SS:
220 	case KPROBE_REENTER:
221 		pr_warn("Unrecoverable kprobe detected.\n");
222 		dump_kprobe(p);
223 		BUG();
224 		break;
225 	default:
226 		WARN_ON(1);
227 		return 0;
228 	}
229 
230 	return 1;
231 }
232 
233 static void __kprobes
234 post_kprobe_handler(struct kprobe *cur, struct kprobe_ctlblk *kcb, struct pt_regs *regs)
235 {
236 	/* return addr restore if non-branching insn */
237 	if (cur->ainsn.api.restore != 0)
238 		instruction_pointer_set(regs, cur->ainsn.api.restore);
239 
240 	/* restore back original saved kprobe variables and continue */
241 	if (kcb->kprobe_status == KPROBE_REENTER) {
242 		restore_previous_kprobe(kcb);
243 		return;
244 	}
245 	/* call post handler */
246 	kcb->kprobe_status = KPROBE_HIT_SSDONE;
247 	if (cur->post_handler)
248 		cur->post_handler(cur, regs, 0);
249 
250 	reset_current_kprobe();
251 }
252 
253 int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned int fsr)
254 {
255 	struct kprobe *cur = kprobe_running();
256 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
257 
258 	switch (kcb->kprobe_status) {
259 	case KPROBE_HIT_SS:
260 	case KPROBE_REENTER:
261 		/*
262 		 * We are here because the instruction being single
263 		 * stepped caused a page fault. We reset the current
264 		 * kprobe and the ip points back to the probe address
265 		 * and allow the page fault handler to continue as a
266 		 * normal page fault.
267 		 */
268 		instruction_pointer_set(regs, (unsigned long) cur->addr);
269 		BUG_ON(!instruction_pointer(regs));
270 
271 		if (kcb->kprobe_status == KPROBE_REENTER) {
272 			restore_previous_kprobe(kcb);
273 		} else {
274 			kprobes_restore_local_irqflag(kcb, regs);
275 			reset_current_kprobe();
276 		}
277 
278 		break;
279 	case KPROBE_HIT_ACTIVE:
280 	case KPROBE_HIT_SSDONE:
281 		/*
282 		 * In case the user-specified fault handler returned
283 		 * zero, try to fix up.
284 		 */
285 		if (fixup_exception(regs))
286 			return 1;
287 	}
288 	return 0;
289 }
290 
291 static void __kprobes kprobe_handler(struct pt_regs *regs)
292 {
293 	struct kprobe *p, *cur_kprobe;
294 	struct kprobe_ctlblk *kcb;
295 	unsigned long addr = instruction_pointer(regs);
296 
297 	kcb = get_kprobe_ctlblk();
298 	cur_kprobe = kprobe_running();
299 
300 	p = get_kprobe((kprobe_opcode_t *) addr);
301 
302 	if (p) {
303 		if (cur_kprobe) {
304 			if (reenter_kprobe(p, regs, kcb))
305 				return;
306 		} else {
307 			/* Probe hit */
308 			set_current_kprobe(p);
309 			kcb->kprobe_status = KPROBE_HIT_ACTIVE;
310 
311 			/*
312 			 * If we have no pre-handler or it returned 0, we
313 			 * continue with normal processing.  If we have a
314 			 * pre-handler and it returned non-zero, it will
315 			 * modify the execution path and no need to single
316 			 * stepping. Let's just reset current kprobe and exit.
317 			 */
318 			if (!p->pre_handler || !p->pre_handler(p, regs)) {
319 				setup_singlestep(p, regs, kcb, 0);
320 			} else
321 				reset_current_kprobe();
322 		}
323 	}
324 	/*
325 	 * The breakpoint instruction was removed right
326 	 * after we hit it.  Another cpu has removed
327 	 * either a probepoint or a debugger breakpoint
328 	 * at this address.  In either case, no further
329 	 * handling of this interrupt is appropriate.
330 	 * Return back to original instruction, and continue.
331 	 */
332 }
333 
334 static int __kprobes
335 kprobe_breakpoint_ss_handler(struct pt_regs *regs, unsigned int esr)
336 {
337 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
338 	unsigned long addr = instruction_pointer(regs);
339 	struct kprobe *cur = kprobe_running();
340 
341 	if (cur && (kcb->kprobe_status & (KPROBE_HIT_SS | KPROBE_REENTER)) &&
342 	    ((unsigned long)&cur->ainsn.api.insn[1] == addr)) {
343 		kprobes_restore_local_irqflag(kcb, regs);
344 		post_kprobe_handler(cur, kcb, regs);
345 
346 		return DBG_HOOK_HANDLED;
347 	}
348 
349 	/* not ours, kprobes should ignore it */
350 	return DBG_HOOK_ERROR;
351 }
352 
353 static struct break_hook kprobes_break_ss_hook = {
354 	.imm = KPROBES_BRK_SS_IMM,
355 	.fn = kprobe_breakpoint_ss_handler,
356 };
357 
358 static int __kprobes
359 kprobe_breakpoint_handler(struct pt_regs *regs, unsigned int esr)
360 {
361 	kprobe_handler(regs);
362 	return DBG_HOOK_HANDLED;
363 }
364 
365 static struct break_hook kprobes_break_hook = {
366 	.imm = KPROBES_BRK_IMM,
367 	.fn = kprobe_breakpoint_handler,
368 };
369 
370 /*
371  * Provide a blacklist of symbols identifying ranges which cannot be kprobed.
372  * This blacklist is exposed to userspace via debugfs (kprobes/blacklist).
373  */
374 int __init arch_populate_kprobe_blacklist(void)
375 {
376 	int ret;
377 
378 	ret = kprobe_add_area_blacklist((unsigned long)__entry_text_start,
379 					(unsigned long)__entry_text_end);
380 	if (ret)
381 		return ret;
382 	ret = kprobe_add_area_blacklist((unsigned long)__irqentry_text_start,
383 					(unsigned long)__irqentry_text_end);
384 	if (ret)
385 		return ret;
386 	ret = kprobe_add_area_blacklist((unsigned long)__idmap_text_start,
387 					(unsigned long)__idmap_text_end);
388 	if (ret)
389 		return ret;
390 	ret = kprobe_add_area_blacklist((unsigned long)__hyp_text_start,
391 					(unsigned long)__hyp_text_end);
392 	if (ret || is_kernel_in_hyp_mode())
393 		return ret;
394 	ret = kprobe_add_area_blacklist((unsigned long)__hyp_idmap_text_start,
395 					(unsigned long)__hyp_idmap_text_end);
396 	return ret;
397 }
398 
399 void __kprobes __used *trampoline_probe_handler(struct pt_regs *regs)
400 {
401 	return (void *)kretprobe_trampoline_handler(regs, &kretprobe_trampoline,
402 					(void *)kernel_stack_pointer(regs));
403 }
404 
405 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
406 				      struct pt_regs *regs)
407 {
408 	ri->ret_addr = (kprobe_opcode_t *)regs->regs[30];
409 	ri->fp = (void *)kernel_stack_pointer(regs);
410 
411 	/* replace return addr (x30) with trampoline */
412 	regs->regs[30] = (long)&kretprobe_trampoline;
413 }
414 
415 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
416 {
417 	return 0;
418 }
419 
420 int __init arch_init_kprobes(void)
421 {
422 	register_kernel_break_hook(&kprobes_break_hook);
423 	register_kernel_break_hook(&kprobes_break_ss_hook);
424 
425 	return 0;
426 }
427