xref: /linux/arch/csky/kernel/probes/ftrace.c (revision f76fbbbb5061fe14824ba5807c44bd7400a6b4e1)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/kprobes.h>
4 
5 /* Ftrace callback handler for kprobes -- called under preepmt disabled */
6 void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
7 			   struct ftrace_ops *ops, struct ftrace_regs *fregs)
8 {
9 	int bit;
10 	bool lr_saver = false;
11 	struct kprobe *p;
12 	struct kprobe_ctlblk *kcb;
13 	struct pt_regs *regs;
14 
15 	bit = ftrace_test_recursion_trylock(ip, parent_ip);
16 	if (bit < 0)
17 		return;
18 
19 	regs = ftrace_get_regs(fregs);
20 	preempt_disable_notrace();
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 			goto out;
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 out:
60 	preempt_enable_notrace();
61 	ftrace_test_recursion_unlock(bit);
62 }
63 NOKPROBE_SYMBOL(kprobe_ftrace_handler);
64 
65 int arch_prepare_kprobe_ftrace(struct kprobe *p)
66 {
67 	p->ainsn.api.insn = NULL;
68 	return 0;
69 }
70