xref: /linux/arch/powerpc/kernel/kprobes.c (revision 850507f30c38dff21ed557cb98ab16db26c32bbc)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Kernel Probes (KProbes)
4  *
5  * Copyright (C) IBM Corporation, 2002, 2004
6  *
7  * 2002-Oct	Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
8  *		Probes initial implementation ( includes contributions from
9  *		Rusty Russell).
10  * 2004-July	Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
11  *		interface to access function arguments.
12  * 2004-Nov	Ananth N Mavinakayanahalli <ananth@in.ibm.com> kprobes port
13  *		for PPC64
14  */
15 
16 #include <linux/kprobes.h>
17 #include <linux/ptrace.h>
18 #include <linux/preempt.h>
19 #include <linux/extable.h>
20 #include <linux/kdebug.h>
21 #include <linux/slab.h>
22 #include <asm/code-patching.h>
23 #include <asm/cacheflush.h>
24 #include <asm/sstep.h>
25 #include <asm/sections.h>
26 #include <linux/uaccess.h>
27 
28 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
29 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
30 
31 struct kretprobe_blackpoint kretprobe_blacklist[] = {{NULL, NULL}};
32 
33 bool arch_within_kprobe_blacklist(unsigned long addr)
34 {
35 	return  (addr >= (unsigned long)__kprobes_text_start &&
36 		 addr < (unsigned long)__kprobes_text_end) ||
37 		(addr >= (unsigned long)_stext &&
38 		 addr < (unsigned long)__head_end);
39 }
40 
41 kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset)
42 {
43 	kprobe_opcode_t *addr = NULL;
44 
45 #ifdef PPC64_ELF_ABI_v2
46 	/* PPC64 ABIv2 needs local entry point */
47 	addr = (kprobe_opcode_t *)kallsyms_lookup_name(name);
48 	if (addr && !offset) {
49 #ifdef CONFIG_KPROBES_ON_FTRACE
50 		unsigned long faddr;
51 		/*
52 		 * Per livepatch.h, ftrace location is always within the first
53 		 * 16 bytes of a function on powerpc with -mprofile-kernel.
54 		 */
55 		faddr = ftrace_location_range((unsigned long)addr,
56 					      (unsigned long)addr + 16);
57 		if (faddr)
58 			addr = (kprobe_opcode_t *)faddr;
59 		else
60 #endif
61 			addr = (kprobe_opcode_t *)ppc_function_entry(addr);
62 	}
63 #elif defined(PPC64_ELF_ABI_v1)
64 	/*
65 	 * 64bit powerpc ABIv1 uses function descriptors:
66 	 * - Check for the dot variant of the symbol first.
67 	 * - If that fails, try looking up the symbol provided.
68 	 *
69 	 * This ensures we always get to the actual symbol and not
70 	 * the descriptor.
71 	 *
72 	 * Also handle <module:symbol> format.
73 	 */
74 	char dot_name[MODULE_NAME_LEN + 1 + KSYM_NAME_LEN];
75 	bool dot_appended = false;
76 	const char *c;
77 	ssize_t ret = 0;
78 	int len = 0;
79 
80 	if ((c = strnchr(name, MODULE_NAME_LEN, ':')) != NULL) {
81 		c++;
82 		len = c - name;
83 		memcpy(dot_name, name, len);
84 	} else
85 		c = name;
86 
87 	if (*c != '\0' && *c != '.') {
88 		dot_name[len++] = '.';
89 		dot_appended = true;
90 	}
91 	ret = strscpy(dot_name + len, c, KSYM_NAME_LEN);
92 	if (ret > 0)
93 		addr = (kprobe_opcode_t *)kallsyms_lookup_name(dot_name);
94 
95 	/* Fallback to the original non-dot symbol lookup */
96 	if (!addr && dot_appended)
97 		addr = (kprobe_opcode_t *)kallsyms_lookup_name(name);
98 #else
99 	addr = (kprobe_opcode_t *)kallsyms_lookup_name(name);
100 #endif
101 
102 	return addr;
103 }
104 
105 int arch_prepare_kprobe(struct kprobe *p)
106 {
107 	int ret = 0;
108 	kprobe_opcode_t insn = *p->addr;
109 
110 	if ((unsigned long)p->addr & 0x03) {
111 		printk("Attempt to register kprobe at an unaligned address\n");
112 		ret = -EINVAL;
113 	} else if (IS_MTMSRD(insn) || IS_RFID(insn) || IS_RFI(insn)) {
114 		printk("Cannot register a kprobe on rfi/rfid or mtmsr[d]\n");
115 		ret = -EINVAL;
116 	}
117 
118 	/* insn must be on a special executable page on ppc64.  This is
119 	 * not explicitly required on ppc32 (right now), but it doesn't hurt */
120 	if (!ret) {
121 		p->ainsn.insn = get_insn_slot();
122 		if (!p->ainsn.insn)
123 			ret = -ENOMEM;
124 	}
125 
126 	if (!ret) {
127 		memcpy(p->ainsn.insn, p->addr,
128 				MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
129 		p->opcode = *p->addr;
130 		flush_icache_range((unsigned long)p->ainsn.insn,
131 			(unsigned long)p->ainsn.insn + sizeof(kprobe_opcode_t));
132 	}
133 
134 	p->ainsn.boostable = 0;
135 	return ret;
136 }
137 NOKPROBE_SYMBOL(arch_prepare_kprobe);
138 
139 void arch_arm_kprobe(struct kprobe *p)
140 {
141 	patch_instruction(p->addr, BREAKPOINT_INSTRUCTION);
142 }
143 NOKPROBE_SYMBOL(arch_arm_kprobe);
144 
145 void arch_disarm_kprobe(struct kprobe *p)
146 {
147 	patch_instruction(p->addr, p->opcode);
148 }
149 NOKPROBE_SYMBOL(arch_disarm_kprobe);
150 
151 void arch_remove_kprobe(struct kprobe *p)
152 {
153 	if (p->ainsn.insn) {
154 		free_insn_slot(p->ainsn.insn, 0);
155 		p->ainsn.insn = NULL;
156 	}
157 }
158 NOKPROBE_SYMBOL(arch_remove_kprobe);
159 
160 static nokprobe_inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
161 {
162 	enable_single_step(regs);
163 
164 	/*
165 	 * On powerpc we should single step on the original
166 	 * instruction even if the probed insn is a trap
167 	 * variant as values in regs could play a part in
168 	 * if the trap is taken or not
169 	 */
170 	regs->nip = (unsigned long)p->ainsn.insn;
171 }
172 
173 static nokprobe_inline void save_previous_kprobe(struct kprobe_ctlblk *kcb)
174 {
175 	kcb->prev_kprobe.kp = kprobe_running();
176 	kcb->prev_kprobe.status = kcb->kprobe_status;
177 	kcb->prev_kprobe.saved_msr = kcb->kprobe_saved_msr;
178 }
179 
180 static nokprobe_inline void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
181 {
182 	__this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
183 	kcb->kprobe_status = kcb->prev_kprobe.status;
184 	kcb->kprobe_saved_msr = kcb->prev_kprobe.saved_msr;
185 }
186 
187 static nokprobe_inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
188 				struct kprobe_ctlblk *kcb)
189 {
190 	__this_cpu_write(current_kprobe, p);
191 	kcb->kprobe_saved_msr = regs->msr;
192 }
193 
194 bool arch_kprobe_on_func_entry(unsigned long offset)
195 {
196 #ifdef PPC64_ELF_ABI_v2
197 #ifdef CONFIG_KPROBES_ON_FTRACE
198 	return offset <= 16;
199 #else
200 	return offset <= 8;
201 #endif
202 #else
203 	return !offset;
204 #endif
205 }
206 
207 void arch_prepare_kretprobe(struct kretprobe_instance *ri, struct pt_regs *regs)
208 {
209 	ri->ret_addr = (kprobe_opcode_t *)regs->link;
210 
211 	/* Replace the return addr with trampoline addr */
212 	regs->link = (unsigned long)kretprobe_trampoline;
213 }
214 NOKPROBE_SYMBOL(arch_prepare_kretprobe);
215 
216 static int try_to_emulate(struct kprobe *p, struct pt_regs *regs)
217 {
218 	int ret;
219 	unsigned int insn = *p->ainsn.insn;
220 
221 	/* regs->nip is also adjusted if emulate_step returns 1 */
222 	ret = emulate_step(regs, insn);
223 	if (ret > 0) {
224 		/*
225 		 * Once this instruction has been boosted
226 		 * successfully, set the boostable flag
227 		 */
228 		if (unlikely(p->ainsn.boostable == 0))
229 			p->ainsn.boostable = 1;
230 	} else if (ret < 0) {
231 		/*
232 		 * We don't allow kprobes on mtmsr(d)/rfi(d), etc.
233 		 * So, we should never get here... but, its still
234 		 * good to catch them, just in case...
235 		 */
236 		printk("Can't step on instruction %x\n", insn);
237 		BUG();
238 	} else {
239 		/*
240 		 * If we haven't previously emulated this instruction, then it
241 		 * can't be boosted. Note it down so we don't try to do so again.
242 		 *
243 		 * If, however, we had emulated this instruction in the past,
244 		 * then this is just an error with the current run (for
245 		 * instance, exceptions due to a load/store). We return 0 so
246 		 * that this is now single-stepped, but continue to try
247 		 * emulating it in subsequent probe hits.
248 		 */
249 		if (unlikely(p->ainsn.boostable != 1))
250 			p->ainsn.boostable = -1;
251 	}
252 
253 	return ret;
254 }
255 NOKPROBE_SYMBOL(try_to_emulate);
256 
257 int kprobe_handler(struct pt_regs *regs)
258 {
259 	struct kprobe *p;
260 	int ret = 0;
261 	unsigned int *addr = (unsigned int *)regs->nip;
262 	struct kprobe_ctlblk *kcb;
263 
264 	if (user_mode(regs))
265 		return 0;
266 
267 	/*
268 	 * We don't want to be preempted for the entire
269 	 * duration of kprobe processing
270 	 */
271 	preempt_disable();
272 	kcb = get_kprobe_ctlblk();
273 
274 	p = get_kprobe(addr);
275 	if (!p) {
276 		if (*addr != BREAKPOINT_INSTRUCTION) {
277 			/*
278 			 * PowerPC has multiple variants of the "trap"
279 			 * instruction. If the current instruction is a
280 			 * trap variant, it could belong to someone else
281 			 */
282 			kprobe_opcode_t cur_insn = *addr;
283 			if (is_trap(cur_insn))
284 				goto no_kprobe;
285 			/*
286 			 * The breakpoint instruction was removed right
287 			 * after we hit it.  Another cpu has removed
288 			 * either a probepoint or a debugger breakpoint
289 			 * at this address.  In either case, no further
290 			 * handling of this interrupt is appropriate.
291 			 */
292 			ret = 1;
293 		}
294 		/* Not one of ours: let kernel handle it */
295 		goto no_kprobe;
296 	}
297 
298 	/* Check we're not actually recursing */
299 	if (kprobe_running()) {
300 		kprobe_opcode_t insn = *p->ainsn.insn;
301 		if (kcb->kprobe_status == KPROBE_HIT_SS && is_trap(insn)) {
302 			/* Turn off 'trace' bits */
303 			regs->msr &= ~MSR_SINGLESTEP;
304 			regs->msr |= kcb->kprobe_saved_msr;
305 			goto no_kprobe;
306 		}
307 
308 		/*
309 		 * We have reentered the kprobe_handler(), since another probe
310 		 * was hit while within the handler. We here save the original
311 		 * kprobes variables and just single step on the instruction of
312 		 * the new probe without calling any user handlers.
313 		 */
314 		save_previous_kprobe(kcb);
315 		set_current_kprobe(p, regs, kcb);
316 		kprobes_inc_nmissed_count(p);
317 		kcb->kprobe_status = KPROBE_REENTER;
318 		if (p->ainsn.boostable >= 0) {
319 			ret = try_to_emulate(p, regs);
320 
321 			if (ret > 0) {
322 				restore_previous_kprobe(kcb);
323 				preempt_enable_no_resched();
324 				return 1;
325 			}
326 		}
327 		prepare_singlestep(p, regs);
328 		return 1;
329 	}
330 
331 	kcb->kprobe_status = KPROBE_HIT_ACTIVE;
332 	set_current_kprobe(p, regs, kcb);
333 	if (p->pre_handler && p->pre_handler(p, regs)) {
334 		/* handler changed execution path, so skip ss setup */
335 		reset_current_kprobe();
336 		preempt_enable_no_resched();
337 		return 1;
338 	}
339 
340 	if (p->ainsn.boostable >= 0) {
341 		ret = try_to_emulate(p, regs);
342 
343 		if (ret > 0) {
344 			if (p->post_handler)
345 				p->post_handler(p, regs, 0);
346 
347 			kcb->kprobe_status = KPROBE_HIT_SSDONE;
348 			reset_current_kprobe();
349 			preempt_enable_no_resched();
350 			return 1;
351 		}
352 	}
353 	prepare_singlestep(p, regs);
354 	kcb->kprobe_status = KPROBE_HIT_SS;
355 	return 1;
356 
357 no_kprobe:
358 	preempt_enable_no_resched();
359 	return ret;
360 }
361 NOKPROBE_SYMBOL(kprobe_handler);
362 
363 /*
364  * Function return probe trampoline:
365  * 	- init_kprobes() establishes a probepoint here
366  * 	- When the probed function returns, this probe
367  * 		causes the handlers to fire
368  */
369 asm(".global kretprobe_trampoline\n"
370 	".type kretprobe_trampoline, @function\n"
371 	"kretprobe_trampoline:\n"
372 	"nop\n"
373 	"blr\n"
374 	".size kretprobe_trampoline, .-kretprobe_trampoline\n");
375 
376 /*
377  * Called when the probe at kretprobe trampoline is hit
378  */
379 static int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
380 {
381 	struct kretprobe_instance *ri = NULL;
382 	struct hlist_head *head, empty_rp;
383 	struct hlist_node *tmp;
384 	unsigned long flags, orig_ret_address = 0;
385 	unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
386 
387 	INIT_HLIST_HEAD(&empty_rp);
388 	kretprobe_hash_lock(current, &head, &flags);
389 
390 	/*
391 	 * It is possible to have multiple instances associated with a given
392 	 * task either because an multiple functions in the call path
393 	 * have a return probe installed on them, and/or more than one return
394 	 * return probe was registered for a target function.
395 	 *
396 	 * We can handle this because:
397 	 *     - instances are always inserted at the head of the list
398 	 *     - when multiple return probes are registered for the same
399 	 *       function, the first instance's ret_addr will point to the
400 	 *       real return address, and all the rest will point to
401 	 *       kretprobe_trampoline
402 	 */
403 	hlist_for_each_entry_safe(ri, tmp, head, hlist) {
404 		if (ri->task != current)
405 			/* another task is sharing our hash bucket */
406 			continue;
407 
408 		if (ri->rp && ri->rp->handler)
409 			ri->rp->handler(ri, regs);
410 
411 		orig_ret_address = (unsigned long)ri->ret_addr;
412 		recycle_rp_inst(ri, &empty_rp);
413 
414 		if (orig_ret_address != trampoline_address)
415 			/*
416 			 * This is the real return address. Any other
417 			 * instances associated with this task are for
418 			 * other calls deeper on the call stack
419 			 */
420 			break;
421 	}
422 
423 	kretprobe_assert(ri, orig_ret_address, trampoline_address);
424 
425 	/*
426 	 * We get here through one of two paths:
427 	 * 1. by taking a trap -> kprobe_handler() -> here
428 	 * 2. by optprobe branch -> optimized_callback() -> opt_pre_handler() -> here
429 	 *
430 	 * When going back through (1), we need regs->nip to be setup properly
431 	 * as it is used to determine the return address from the trap.
432 	 * For (2), since nip is not honoured with optprobes, we instead setup
433 	 * the link register properly so that the subsequent 'blr' in
434 	 * kretprobe_trampoline jumps back to the right instruction.
435 	 *
436 	 * For nip, we should set the address to the previous instruction since
437 	 * we end up emulating it in kprobe_handler(), which increments the nip
438 	 * again.
439 	 */
440 	regs->nip = orig_ret_address - 4;
441 	regs->link = orig_ret_address;
442 
443 	kretprobe_hash_unlock(current, &flags);
444 
445 	hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
446 		hlist_del(&ri->hlist);
447 		kfree(ri);
448 	}
449 
450 	return 0;
451 }
452 NOKPROBE_SYMBOL(trampoline_probe_handler);
453 
454 /*
455  * Called after single-stepping.  p->addr is the address of the
456  * instruction whose first byte has been replaced by the "breakpoint"
457  * instruction.  To avoid the SMP problems that can occur when we
458  * temporarily put back the original opcode to single-step, we
459  * single-stepped a copy of the instruction.  The address of this
460  * copy is p->ainsn.insn.
461  */
462 int kprobe_post_handler(struct pt_regs *regs)
463 {
464 	struct kprobe *cur = kprobe_running();
465 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
466 
467 	if (!cur || user_mode(regs))
468 		return 0;
469 
470 	/* make sure we got here for instruction we have a kprobe on */
471 	if (((unsigned long)cur->ainsn.insn + 4) != regs->nip)
472 		return 0;
473 
474 	if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
475 		kcb->kprobe_status = KPROBE_HIT_SSDONE;
476 		cur->post_handler(cur, regs, 0);
477 	}
478 
479 	/* Adjust nip to after the single-stepped instruction */
480 	regs->nip = (unsigned long)cur->addr + 4;
481 	regs->msr |= kcb->kprobe_saved_msr;
482 
483 	/*Restore back the original saved kprobes variables and continue. */
484 	if (kcb->kprobe_status == KPROBE_REENTER) {
485 		restore_previous_kprobe(kcb);
486 		goto out;
487 	}
488 	reset_current_kprobe();
489 out:
490 	preempt_enable_no_resched();
491 
492 	/*
493 	 * if somebody else is singlestepping across a probe point, msr
494 	 * will have DE/SE set, in which case, continue the remaining processing
495 	 * of do_debug, as if this is not a probe hit.
496 	 */
497 	if (regs->msr & MSR_SINGLESTEP)
498 		return 0;
499 
500 	return 1;
501 }
502 NOKPROBE_SYMBOL(kprobe_post_handler);
503 
504 int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
505 {
506 	struct kprobe *cur = kprobe_running();
507 	struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
508 	const struct exception_table_entry *entry;
509 
510 	switch(kcb->kprobe_status) {
511 	case KPROBE_HIT_SS:
512 	case KPROBE_REENTER:
513 		/*
514 		 * We are here because the instruction being single
515 		 * stepped caused a page fault. We reset the current
516 		 * kprobe and the nip points back to the probe address
517 		 * and allow the page fault handler to continue as a
518 		 * normal page fault.
519 		 */
520 		regs->nip = (unsigned long)cur->addr;
521 		regs->msr &= ~MSR_SINGLESTEP; /* Turn off 'trace' bits */
522 		regs->msr |= kcb->kprobe_saved_msr;
523 		if (kcb->kprobe_status == KPROBE_REENTER)
524 			restore_previous_kprobe(kcb);
525 		else
526 			reset_current_kprobe();
527 		preempt_enable_no_resched();
528 		break;
529 	case KPROBE_HIT_ACTIVE:
530 	case KPROBE_HIT_SSDONE:
531 		/*
532 		 * We increment the nmissed count for accounting,
533 		 * we can also use npre/npostfault count for accounting
534 		 * these specific fault cases.
535 		 */
536 		kprobes_inc_nmissed_count(cur);
537 
538 		/*
539 		 * We come here because instructions in the pre/post
540 		 * handler caused the page_fault, this could happen
541 		 * if handler tries to access user space by
542 		 * copy_from_user(), get_user() etc. Let the
543 		 * user-specified handler try to fix it first.
544 		 */
545 		if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
546 			return 1;
547 
548 		/*
549 		 * In case the user-specified fault handler returned
550 		 * zero, try to fix up.
551 		 */
552 		if ((entry = search_exception_tables(regs->nip)) != NULL) {
553 			regs->nip = extable_fixup(entry);
554 			return 1;
555 		}
556 
557 		/*
558 		 * fixup_exception() could not handle it,
559 		 * Let do_page_fault() fix it.
560 		 */
561 		break;
562 	default:
563 		break;
564 	}
565 	return 0;
566 }
567 NOKPROBE_SYMBOL(kprobe_fault_handler);
568 
569 unsigned long arch_deref_entry_point(void *entry)
570 {
571 #ifdef PPC64_ELF_ABI_v1
572 	if (!kernel_text_address((unsigned long)entry))
573 		return ppc_global_function_entry(entry);
574 	else
575 #endif
576 		return (unsigned long)entry;
577 }
578 NOKPROBE_SYMBOL(arch_deref_entry_point);
579 
580 static struct kprobe trampoline_p = {
581 	.addr = (kprobe_opcode_t *) &kretprobe_trampoline,
582 	.pre_handler = trampoline_probe_handler
583 };
584 
585 int __init arch_init_kprobes(void)
586 {
587 	return register_kprobe(&trampoline_p);
588 }
589 
590 int arch_trampoline_kprobe(struct kprobe *p)
591 {
592 	if (p->addr == (kprobe_opcode_t *)&kretprobe_trampoline)
593 		return 1;
594 
595 	return 0;
596 }
597 NOKPROBE_SYMBOL(arch_trampoline_kprobe);
598