xref: /openbmc/linux/arch/csky/kernel/probes/ftrace.c (revision 78bb17f7)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/kprobes.h>
4 
5 int arch_check_ftrace_location(struct kprobe *p)
6 {
7 	if (ftrace_location((unsigned long)p->addr))
8 		p->flags |= KPROBE_FLAG_FTRACE;
9 	return 0;
10 }
11 
12 /* Ftrace callback handler for kprobes -- called under preepmt disabed */
13 void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
14 			   struct ftrace_ops *ops, struct pt_regs *regs)
15 {
16 	bool lr_saver = false;
17 	struct kprobe *p;
18 	struct kprobe_ctlblk *kcb;
19 
20 	/* Preempt is disabled by ftrace */
21 	p = get_kprobe((kprobe_opcode_t *)ip);
22 	if (!p) {
23 		p = get_kprobe((kprobe_opcode_t *)(ip - MCOUNT_INSN_SIZE));
24 		if (unlikely(!p) || kprobe_disabled(p))
25 			return;
26 		lr_saver = true;
27 	}
28 
29 	kcb = get_kprobe_ctlblk();
30 	if (kprobe_running()) {
31 		kprobes_inc_nmissed_count(p);
32 	} else {
33 		unsigned long orig_ip = instruction_pointer(regs);
34 
35 		if (lr_saver)
36 			ip -= MCOUNT_INSN_SIZE;
37 		instruction_pointer_set(regs, ip);
38 		__this_cpu_write(current_kprobe, p);
39 		kcb->kprobe_status = KPROBE_HIT_ACTIVE;
40 		if (!p->pre_handler || !p->pre_handler(p, regs)) {
41 			/*
42 			 * Emulate singlestep (and also recover regs->pc)
43 			 * as if there is a nop
44 			 */
45 			instruction_pointer_set(regs,
46 				(unsigned long)p->addr + MCOUNT_INSN_SIZE);
47 			if (unlikely(p->post_handler)) {
48 				kcb->kprobe_status = KPROBE_HIT_SSDONE;
49 				p->post_handler(p, regs, 0);
50 			}
51 			instruction_pointer_set(regs, orig_ip);
52 		}
53 		/*
54 		 * If pre_handler returns !0, it changes regs->pc. We have to
55 		 * skip emulating post_handler.
56 		 */
57 		__this_cpu_write(current_kprobe, NULL);
58 	}
59 }
60 NOKPROBE_SYMBOL(kprobe_ftrace_handler);
61 
62 int arch_prepare_kprobe_ftrace(struct kprobe *p)
63 {
64 	p->ainsn.api.insn = NULL;
65 	return 0;
66 }
67