Back to home page

OSCL-LXR

 
 

    


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