xref: /openbmc/linux/arch/x86/kernel/kprobes/ftrace.c (revision 6f4eaea2)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Dynamic Ftrace based Kprobes Optimization
4  *
5  * Copyright (C) Hitachi Ltd., 2012
6  */
7 #include <linux/kprobes.h>
8 #include <linux/ptrace.h>
9 #include <linux/hardirq.h>
10 #include <linux/preempt.h>
11 #include <linux/ftrace.h>
12 
13 #include "common.h"
14 
15 /* Ftrace callback handler for kprobes -- called under preepmt disabed */
16 void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
17 			   struct ftrace_ops *ops, struct ftrace_regs *fregs)
18 {
19 	struct pt_regs *regs = ftrace_get_regs(fregs);
20 	struct kprobe *p;
21 	struct kprobe_ctlblk *kcb;
22 	int bit;
23 
24 	bit = ftrace_test_recursion_trylock(ip, parent_ip);
25 	if (bit < 0)
26 		return;
27 
28 	preempt_disable_notrace();
29 	p = get_kprobe((kprobe_opcode_t *)ip);
30 	if (unlikely(!p) || kprobe_disabled(p))
31 		goto out;
32 
33 	kcb = get_kprobe_ctlblk();
34 	if (kprobe_running()) {
35 		kprobes_inc_nmissed_count(p);
36 	} else {
37 		unsigned long orig_ip = regs->ip;
38 		/* Kprobe handler expects regs->ip = ip + 1 as breakpoint hit */
39 		regs->ip = ip + sizeof(kprobe_opcode_t);
40 
41 		__this_cpu_write(current_kprobe, p);
42 		kcb->kprobe_status = KPROBE_HIT_ACTIVE;
43 		if (!p->pre_handler || !p->pre_handler(p, regs)) {
44 			/*
45 			 * Emulate singlestep (and also recover regs->ip)
46 			 * as if there is a 5byte nop
47 			 */
48 			regs->ip = (unsigned long)p->addr + MCOUNT_INSN_SIZE;
49 			if (unlikely(p->post_handler)) {
50 				kcb->kprobe_status = KPROBE_HIT_SSDONE;
51 				p->post_handler(p, regs, 0);
52 			}
53 			regs->ip = orig_ip;
54 		}
55 		/*
56 		 * If pre_handler returns !0, it changes regs->ip. We have to
57 		 * skip emulating post_handler.
58 		 */
59 		__this_cpu_write(current_kprobe, NULL);
60 	}
61 out:
62 	preempt_enable_notrace();
63 	ftrace_test_recursion_unlock(bit);
64 }
65 NOKPROBE_SYMBOL(kprobe_ftrace_handler);
66 
67 int arch_prepare_kprobe_ftrace(struct kprobe *p)
68 {
69 	p->ainsn.insn = NULL;
70 	p->ainsn.boostable = false;
71 	return 0;
72 }
73