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 struct kprobe *p; 10 struct pt_regs *regs; 11 struct kprobe_ctlblk *kcb; 12 int bit; 13 14 if (unlikely(kprobe_ftrace_disabled)) 15 return; 16 17 bit = ftrace_test_recursion_trylock(ip, parent_ip); 18 if (bit < 0) 19 return; 20 21 p = get_kprobe((kprobe_opcode_t *)ip); 22 if (unlikely(!p) || kprobe_disabled(p)) 23 goto out; 24 25 regs = ftrace_get_regs(fregs); 26 kcb = get_kprobe_ctlblk(); 27 if (kprobe_running()) { 28 kprobes_inc_nmissed_count(p); 29 } else { 30 unsigned long orig_ip = instruction_pointer(regs); 31 32 instruction_pointer_set(regs, ip); 33 34 __this_cpu_write(current_kprobe, p); 35 kcb->kprobe_status = KPROBE_HIT_ACTIVE; 36 if (!p->pre_handler || !p->pre_handler(p, regs)) { 37 /* 38 * Emulate singlestep (and also recover regs->pc) 39 * as if there is a nop 40 */ 41 instruction_pointer_set(regs, 42 (unsigned long)p->addr + MCOUNT_INSN_SIZE); 43 if (unlikely(p->post_handler)) { 44 kcb->kprobe_status = KPROBE_HIT_SSDONE; 45 p->post_handler(p, regs, 0); 46 } 47 instruction_pointer_set(regs, orig_ip); 48 } 49 50 /* 51 * If pre_handler returns !0, it changes regs->pc. We have to 52 * skip emulating post_handler. 53 */ 54 __this_cpu_write(current_kprobe, NULL); 55 } 56 out: 57 ftrace_test_recursion_unlock(bit); 58 } 59 NOKPROBE_SYMBOL(kprobe_ftrace_handler); 60 61 int arch_prepare_kprobe_ftrace(struct kprobe *p) 62 { 63 p->ainsn.api.insn = NULL; 64 return 0; 65 } 66