Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 #include <linux/kprobes.h>
0004 
0005 /* Ftrace callback handler for kprobes -- called under preepmt disabled */
0006 void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
0007                struct ftrace_ops *ops, struct ftrace_regs *fregs)
0008 {
0009     int bit;
0010     bool lr_saver = false;
0011     struct kprobe *p;
0012     struct kprobe_ctlblk *kcb;
0013     struct pt_regs *regs;
0014 
0015     bit = ftrace_test_recursion_trylock(ip, parent_ip);
0016     if (bit < 0)
0017         return;
0018 
0019     regs = ftrace_get_regs(fregs);
0020     p = get_kprobe((kprobe_opcode_t *)ip);
0021     if (!p) {
0022         p = get_kprobe((kprobe_opcode_t *)(ip - MCOUNT_INSN_SIZE));
0023         if (unlikely(!p) || kprobe_disabled(p))
0024             goto out;
0025         lr_saver = true;
0026     }
0027 
0028     kcb = get_kprobe_ctlblk();
0029     if (kprobe_running()) {
0030         kprobes_inc_nmissed_count(p);
0031     } else {
0032         unsigned long orig_ip = instruction_pointer(regs);
0033 
0034         if (lr_saver)
0035             ip -= MCOUNT_INSN_SIZE;
0036         instruction_pointer_set(regs, ip);
0037         __this_cpu_write(current_kprobe, p);
0038         kcb->kprobe_status = KPROBE_HIT_ACTIVE;
0039         if (!p->pre_handler || !p->pre_handler(p, regs)) {
0040             /*
0041              * Emulate singlestep (and also recover regs->pc)
0042              * as if there is a nop
0043              */
0044             instruction_pointer_set(regs,
0045                 (unsigned long)p->addr + MCOUNT_INSN_SIZE);
0046             if (unlikely(p->post_handler)) {
0047                 kcb->kprobe_status = KPROBE_HIT_SSDONE;
0048                 p->post_handler(p, regs, 0);
0049             }
0050             instruction_pointer_set(regs, orig_ip);
0051         }
0052         /*
0053          * If pre_handler returns !0, it changes regs->pc. We have to
0054          * skip emulating post_handler.
0055          */
0056         __this_cpu_write(current_kprobe, NULL);
0057     }
0058 out:
0059     ftrace_test_recursion_unlock(bit);
0060 }
0061 NOKPROBE_SYMBOL(kprobe_ftrace_handler);
0062 
0063 int arch_prepare_kprobe_ftrace(struct kprobe *p)
0064 {
0065     p->ainsn.api.insn = NULL;
0066     return 0;
0067 }