11da177e4SLinus Torvalds #ifndef _LINUX_KPROBES_H 21da177e4SLinus Torvalds #define _LINUX_KPROBES_H 31da177e4SLinus Torvalds /* 41da177e4SLinus Torvalds * Kernel Probes (KProbes) 51da177e4SLinus Torvalds * include/linux/kprobes.h 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 81da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 91da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 101da177e4SLinus Torvalds * (at your option) any later version. 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, 131da177e4SLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of 141da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 151da177e4SLinus Torvalds * GNU General Public License for more details. 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License 181da177e4SLinus Torvalds * along with this program; if not, write to the Free Software 191da177e4SLinus Torvalds * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * Copyright (C) IBM Corporation, 2002, 2004 221da177e4SLinus Torvalds * 231da177e4SLinus Torvalds * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel 241da177e4SLinus Torvalds * Probes initial implementation ( includes suggestions from 251da177e4SLinus Torvalds * Rusty Russell). 261da177e4SLinus Torvalds * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes 271da177e4SLinus Torvalds * interface to access function arguments. 28b94cce92SHien Nguyen * 2005-May Hien Nguyen <hien@us.ibm.com> and Jim Keniston 29b94cce92SHien Nguyen * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi 30b94cce92SHien Nguyen * <prasanna@in.ibm.com> added function-return probes. 311da177e4SLinus Torvalds */ 3236dcd67aSIngo Molnar #include <linux/linkage.h> 331da177e4SLinus Torvalds #include <linux/list.h> 341da177e4SLinus Torvalds #include <linux/notifier.h> 351da177e4SLinus Torvalds #include <linux/smp.h> 36e6584523SAnanth N Mavinakayanahalli #include <linux/percpu.h> 373516a460SAnanth N Mavinakayanahalli #include <linux/spinlock.h> 383516a460SAnanth N Mavinakayanahalli #include <linux/rcupdate.h> 397a7d1cf9SIngo Molnar #include <linux/mutex.h> 40b94cce92SHien Nguyen 4100d7c05aSKeshavamurthy Anil S #ifdef CONFIG_KPROBES 421da177e4SLinus Torvalds #include <asm/kprobes.h> 431da177e4SLinus Torvalds 44ea32c65cSPrasanna S Panchamukhi /* kprobe_status settings */ 45ea32c65cSPrasanna S Panchamukhi #define KPROBE_HIT_ACTIVE 0x00000001 46ea32c65cSPrasanna S Panchamukhi #define KPROBE_HIT_SS 0x00000002 47ea32c65cSPrasanna S Panchamukhi #define KPROBE_REENTER 0x00000004 48ea32c65cSPrasanna S Panchamukhi #define KPROBE_HIT_SSDONE 0x00000008 49ea32c65cSPrasanna S Panchamukhi 50d0aaff97SPrasanna S Panchamukhi /* Attach to insert probes on any functions which should be ignored*/ 5136dcd67aSIngo Molnar #define __kprobes __attribute__((__section__(".kprobes.text"))) notrace 52*dc19835dSMasami Hiramatsu #else /* CONFIG_KPROBES */ 53*dc19835dSMasami Hiramatsu typedef int kprobe_opcode_t; 54*dc19835dSMasami Hiramatsu struct arch_specific_insn { 55*dc19835dSMasami Hiramatsu int dummy; 56*dc19835dSMasami Hiramatsu }; 57*dc19835dSMasami Hiramatsu #define __kprobes notrace 58*dc19835dSMasami Hiramatsu #endif /* CONFIG_KPROBES */ 59d0aaff97SPrasanna S Panchamukhi 601da177e4SLinus Torvalds struct kprobe; 611da177e4SLinus Torvalds struct pt_regs; 62b94cce92SHien Nguyen struct kretprobe; 63b94cce92SHien Nguyen struct kretprobe_instance; 641da177e4SLinus Torvalds typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *); 651da177e4SLinus Torvalds typedef int (*kprobe_break_handler_t) (struct kprobe *, struct pt_regs *); 661da177e4SLinus Torvalds typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *, 671da177e4SLinus Torvalds unsigned long flags); 681da177e4SLinus Torvalds typedef int (*kprobe_fault_handler_t) (struct kprobe *, struct pt_regs *, 691da177e4SLinus Torvalds int trapnr); 70b94cce92SHien Nguyen typedef int (*kretprobe_handler_t) (struct kretprobe_instance *, 71b94cce92SHien Nguyen struct pt_regs *); 72b94cce92SHien Nguyen 731da177e4SLinus Torvalds struct kprobe { 741da177e4SLinus Torvalds struct hlist_node hlist; 751da177e4SLinus Torvalds 7664f562c6SAnanth N Mavinakayanahalli /* list of kprobes for multi-handler support */ 7764f562c6SAnanth N Mavinakayanahalli struct list_head list; 7864f562c6SAnanth N Mavinakayanahalli 79ea32c65cSPrasanna S Panchamukhi /*count the number of times this probe was temporarily disarmed */ 80ea32c65cSPrasanna S Panchamukhi unsigned long nmissed; 81ea32c65cSPrasanna S Panchamukhi 821da177e4SLinus Torvalds /* location of the probe point */ 831da177e4SLinus Torvalds kprobe_opcode_t *addr; 841da177e4SLinus Torvalds 853a872d89SAnanth N Mavinakayanahalli /* Allow user to indicate symbol name of the probe point */ 869b3af29bSAnanth N Mavinakayanahalli const char *symbol_name; 873a872d89SAnanth N Mavinakayanahalli 883a872d89SAnanth N Mavinakayanahalli /* Offset into the symbol */ 893a872d89SAnanth N Mavinakayanahalli unsigned int offset; 903a872d89SAnanth N Mavinakayanahalli 911da177e4SLinus Torvalds /* Called before addr is executed. */ 921da177e4SLinus Torvalds kprobe_pre_handler_t pre_handler; 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds /* Called after addr is executed, unless... */ 951da177e4SLinus Torvalds kprobe_post_handler_t post_handler; 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds /* ... called if executing addr causes a fault (eg. page fault). 981da177e4SLinus Torvalds * Return 1 if it handled fault, otherwise kernel will see it. */ 991da177e4SLinus Torvalds kprobe_fault_handler_t fault_handler; 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds /* ... called if breakpoint trap occurs in probe handler. 1021da177e4SLinus Torvalds * Return 1 if it handled break, otherwise kernel will see it. */ 1031da177e4SLinus Torvalds kprobe_break_handler_t break_handler; 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds /* Saved opcode (which has been replaced with breakpoint) */ 1061da177e4SLinus Torvalds kprobe_opcode_t opcode; 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds /* copy of the original instruction */ 1091da177e4SLinus Torvalds struct arch_specific_insn ainsn; 110e8386a0cSMasami Hiramatsu 111e8386a0cSMasami Hiramatsu /* Indicates various status flags. Protected by kprobe_mutex. */ 112e8386a0cSMasami Hiramatsu u32 flags; 1131da177e4SLinus Torvalds }; 1141da177e4SLinus Torvalds 115e8386a0cSMasami Hiramatsu /* Kprobe status flags */ 116e8386a0cSMasami Hiramatsu #define KPROBE_FLAG_GONE 1 /* breakpoint has already gone */ 117e8386a0cSMasami Hiramatsu 118e8386a0cSMasami Hiramatsu static inline int kprobe_gone(struct kprobe *p) 119e8386a0cSMasami Hiramatsu { 120e8386a0cSMasami Hiramatsu return p->flags & KPROBE_FLAG_GONE; 121e8386a0cSMasami Hiramatsu } 122e8386a0cSMasami Hiramatsu 1231da177e4SLinus Torvalds /* 1241da177e4SLinus Torvalds * Special probe type that uses setjmp-longjmp type tricks to resume 1251da177e4SLinus Torvalds * execution at a specified entry with a matching prototype corresponding 1261da177e4SLinus Torvalds * to the probed function - a trick to enable arguments to become 1271da177e4SLinus Torvalds * accessible seamlessly by probe handling logic. 1281da177e4SLinus Torvalds * Note: 1291da177e4SLinus Torvalds * Because of the way compilers allocate stack space for local variables 1301da177e4SLinus Torvalds * etc upfront, regardless of sub-scopes within a function, this mirroring 1311da177e4SLinus Torvalds * principle currently works only for probes placed on function entry points. 1321da177e4SLinus Torvalds */ 1331da177e4SLinus Torvalds struct jprobe { 1341da177e4SLinus Torvalds struct kprobe kp; 13581eae375SMichael Ellerman void *entry; /* probe handling code to jump to */ 1361da177e4SLinus Torvalds }; 1371da177e4SLinus Torvalds 1389e367d85SMichael Ellerman /* For backward compatibility with old code using JPROBE_ENTRY() */ 1399e367d85SMichael Ellerman #define JPROBE_ENTRY(handler) (handler) 1409e367d85SMichael Ellerman 141b94cce92SHien Nguyen /* 142b94cce92SHien Nguyen * Function-return probe - 143b94cce92SHien Nguyen * Note: 144b94cce92SHien Nguyen * User needs to provide a handler function, and initialize maxactive. 145b94cce92SHien Nguyen * maxactive - The maximum number of instances of the probed function that 146b94cce92SHien Nguyen * can be active concurrently. 147b94cce92SHien Nguyen * nmissed - tracks the number of times the probed function's return was 148b94cce92SHien Nguyen * ignored, due to maxactive being too low. 149b94cce92SHien Nguyen * 150b94cce92SHien Nguyen */ 151b94cce92SHien Nguyen struct kretprobe { 152b94cce92SHien Nguyen struct kprobe kp; 153b94cce92SHien Nguyen kretprobe_handler_t handler; 154f47cd9b5SAbhishek Sagar kretprobe_handler_t entry_handler; 155b94cce92SHien Nguyen int maxactive; 156b94cce92SHien Nguyen int nmissed; 157f47cd9b5SAbhishek Sagar size_t data_size; 158b94cce92SHien Nguyen struct hlist_head free_instances; 159ef53d9c5SSrinivasa D S spinlock_t lock; 160b94cce92SHien Nguyen }; 161b94cce92SHien Nguyen 162b94cce92SHien Nguyen struct kretprobe_instance { 163b94cce92SHien Nguyen struct hlist_node hlist; 164b94cce92SHien Nguyen struct kretprobe *rp; 165802eae7cSRusty Lynch kprobe_opcode_t *ret_addr; 166802eae7cSRusty Lynch struct task_struct *task; 167f47cd9b5SAbhishek Sagar char data[0]; 168b94cce92SHien Nguyen }; 169b94cce92SHien Nguyen 170f438d914SMasami Hiramatsu struct kretprobe_blackpoint { 171f438d914SMasami Hiramatsu const char *name; 172f438d914SMasami Hiramatsu void *addr; 173f438d914SMasami Hiramatsu }; 1743d8d996eSSrinivasa Ds 1753d8d996eSSrinivasa Ds struct kprobe_blackpoint { 1763d8d996eSSrinivasa Ds const char *name; 1773d8d996eSSrinivasa Ds unsigned long start_addr; 1783d8d996eSSrinivasa Ds unsigned long range; 1793d8d996eSSrinivasa Ds }; 1803d8d996eSSrinivasa Ds 181*dc19835dSMasami Hiramatsu #ifdef CONFIG_KPROBES 182*dc19835dSMasami Hiramatsu DECLARE_PER_CPU(struct kprobe *, current_kprobe); 183*dc19835dSMasami Hiramatsu DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); 184*dc19835dSMasami Hiramatsu 185*dc19835dSMasami Hiramatsu #ifdef CONFIG_KRETPROBES 186*dc19835dSMasami Hiramatsu extern void arch_prepare_kretprobe(struct kretprobe_instance *ri, 187*dc19835dSMasami Hiramatsu struct pt_regs *regs); 188*dc19835dSMasami Hiramatsu extern int arch_trampoline_kprobe(struct kprobe *p); 189*dc19835dSMasami Hiramatsu #else /* CONFIG_KRETPROBES */ 190*dc19835dSMasami Hiramatsu static inline void arch_prepare_kretprobe(struct kretprobe *rp, 191*dc19835dSMasami Hiramatsu struct pt_regs *regs) 192*dc19835dSMasami Hiramatsu { 193*dc19835dSMasami Hiramatsu } 194*dc19835dSMasami Hiramatsu static inline int arch_trampoline_kprobe(struct kprobe *p) 195*dc19835dSMasami Hiramatsu { 196*dc19835dSMasami Hiramatsu return 0; 197*dc19835dSMasami Hiramatsu } 198*dc19835dSMasami Hiramatsu #endif /* CONFIG_KRETPROBES */ 199*dc19835dSMasami Hiramatsu 200f438d914SMasami Hiramatsu extern struct kretprobe_blackpoint kretprobe_blacklist[]; 201f438d914SMasami Hiramatsu 2020f95b7fcSAnanth N Mavinakayanahalli static inline void kretprobe_assert(struct kretprobe_instance *ri, 2030f95b7fcSAnanth N Mavinakayanahalli unsigned long orig_ret_address, unsigned long trampoline_address) 2040f95b7fcSAnanth N Mavinakayanahalli { 2050f95b7fcSAnanth N Mavinakayanahalli if (!orig_ret_address || (orig_ret_address == trampoline_address)) { 2060f95b7fcSAnanth N Mavinakayanahalli printk("kretprobe BUG!: Processing kretprobe %p @ %p\n", 2070f95b7fcSAnanth N Mavinakayanahalli ri->rp, ri->rp->kp.addr); 2080f95b7fcSAnanth N Mavinakayanahalli BUG(); 2090f95b7fcSAnanth N Mavinakayanahalli } 2100f95b7fcSAnanth N Mavinakayanahalli } 2110f95b7fcSAnanth N Mavinakayanahalli 2128c1c9356SAnanth N Mavinakayanahalli #ifdef CONFIG_KPROBES_SANITY_TEST 2138c1c9356SAnanth N Mavinakayanahalli extern int init_test_probes(void); 2148c1c9356SAnanth N Mavinakayanahalli #else 2158c1c9356SAnanth N Mavinakayanahalli static inline int init_test_probes(void) 2168c1c9356SAnanth N Mavinakayanahalli { 2178c1c9356SAnanth N Mavinakayanahalli return 0; 2188c1c9356SAnanth N Mavinakayanahalli } 2198c1c9356SAnanth N Mavinakayanahalli #endif /* CONFIG_KPROBES_SANITY_TEST */ 2208c1c9356SAnanth N Mavinakayanahalli 2211da177e4SLinus Torvalds extern int arch_prepare_kprobe(struct kprobe *p); 2227e1048b1SRusty Lynch extern void arch_arm_kprobe(struct kprobe *p); 2237e1048b1SRusty Lynch extern void arch_disarm_kprobe(struct kprobe *p); 2246772926bSRusty Lynch extern int arch_init_kprobes(void); 2251da177e4SLinus Torvalds extern void show_registers(struct pt_regs *regs); 2269ec4b1f3SAnanth N Mavinakayanahalli extern kprobe_opcode_t *get_insn_slot(void); 227b4c6c34aSMasami Hiramatsu extern void free_insn_slot(kprobe_opcode_t *slot, int dirty); 228bf8d5c52SKeshavamurthy Anil S extern void kprobes_inc_nmissed_count(struct kprobe *p); 2291da177e4SLinus Torvalds 230d217d545SAnanth N Mavinakayanahalli /* Get the kprobe at this addr (if any) - called with preemption disabled */ 2311da177e4SLinus Torvalds struct kprobe *get_kprobe(void *addr); 232ef53d9c5SSrinivasa D S void kretprobe_hash_lock(struct task_struct *tsk, 233ef53d9c5SSrinivasa D S struct hlist_head **head, unsigned long *flags); 234ef53d9c5SSrinivasa D S void kretprobe_hash_unlock(struct task_struct *tsk, unsigned long *flags); 235b94cce92SHien Nguyen struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk); 2361da177e4SLinus Torvalds 237e6584523SAnanth N Mavinakayanahalli /* kprobe_running() will just return the current_kprobe on this CPU */ 238e6584523SAnanth N Mavinakayanahalli static inline struct kprobe *kprobe_running(void) 239e6584523SAnanth N Mavinakayanahalli { 240e6584523SAnanth N Mavinakayanahalli return (__get_cpu_var(current_kprobe)); 241e6584523SAnanth N Mavinakayanahalli } 242e6584523SAnanth N Mavinakayanahalli 243e6584523SAnanth N Mavinakayanahalli static inline void reset_current_kprobe(void) 244e6584523SAnanth N Mavinakayanahalli { 245e6584523SAnanth N Mavinakayanahalli __get_cpu_var(current_kprobe) = NULL; 246e6584523SAnanth N Mavinakayanahalli } 247e6584523SAnanth N Mavinakayanahalli 248e6584523SAnanth N Mavinakayanahalli static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void) 249e6584523SAnanth N Mavinakayanahalli { 250e6584523SAnanth N Mavinakayanahalli return (&__get_cpu_var(kprobe_ctlblk)); 251e6584523SAnanth N Mavinakayanahalli } 252e6584523SAnanth N Mavinakayanahalli 2531da177e4SLinus Torvalds int register_kprobe(struct kprobe *p); 2541da177e4SLinus Torvalds void unregister_kprobe(struct kprobe *p); 2559861668fSMasami Hiramatsu int register_kprobes(struct kprobe **kps, int num); 2569861668fSMasami Hiramatsu void unregister_kprobes(struct kprobe **kps, int num); 2571da177e4SLinus Torvalds int setjmp_pre_handler(struct kprobe *, struct pt_regs *); 2581da177e4SLinus Torvalds int longjmp_break_handler(struct kprobe *, struct pt_regs *); 2591da177e4SLinus Torvalds int register_jprobe(struct jprobe *p); 2601da177e4SLinus Torvalds void unregister_jprobe(struct jprobe *p); 26126b31c19SMasami Hiramatsu int register_jprobes(struct jprobe **jps, int num); 26226b31c19SMasami Hiramatsu void unregister_jprobes(struct jprobe **jps, int num); 2631da177e4SLinus Torvalds void jprobe_return(void); 2643d7e3382SMichael Ellerman unsigned long arch_deref_entry_point(void *); 2651da177e4SLinus Torvalds 266b94cce92SHien Nguyen int register_kretprobe(struct kretprobe *rp); 267b94cce92SHien Nguyen void unregister_kretprobe(struct kretprobe *rp); 2684a296e07SMasami Hiramatsu int register_kretprobes(struct kretprobe **rps, int num); 2694a296e07SMasami Hiramatsu void unregister_kretprobes(struct kretprobe **rps, int num); 270b94cce92SHien Nguyen 271b94cce92SHien Nguyen void kprobe_flush_task(struct task_struct *tk); 27299219a3fSbibo,mao void recycle_rp_inst(struct kretprobe_instance *ri, struct hlist_head *head); 2738c1c9356SAnanth N Mavinakayanahalli 274b94cce92SHien Nguyen #else /* CONFIG_KPROBES */ 27500d7c05aSKeshavamurthy Anil S 276785656a4SAbhishek Sagar static inline struct kprobe *get_kprobe(void *addr) 277785656a4SAbhishek Sagar { 278785656a4SAbhishek Sagar return NULL; 279785656a4SAbhishek Sagar } 280e6584523SAnanth N Mavinakayanahalli static inline struct kprobe *kprobe_running(void) 2811da177e4SLinus Torvalds { 282e6584523SAnanth N Mavinakayanahalli return NULL; 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds static inline int register_kprobe(struct kprobe *p) 2851da177e4SLinus Torvalds { 2861da177e4SLinus Torvalds return -ENOSYS; 2871da177e4SLinus Torvalds } 2889861668fSMasami Hiramatsu static inline int register_kprobes(struct kprobe **kps, int num) 2899861668fSMasami Hiramatsu { 2909861668fSMasami Hiramatsu return -ENOSYS; 2919861668fSMasami Hiramatsu } 2921da177e4SLinus Torvalds static inline void unregister_kprobe(struct kprobe *p) 2931da177e4SLinus Torvalds { 2941da177e4SLinus Torvalds } 2959861668fSMasami Hiramatsu static inline void unregister_kprobes(struct kprobe **kps, int num) 2969861668fSMasami Hiramatsu { 2979861668fSMasami Hiramatsu } 2981da177e4SLinus Torvalds static inline int register_jprobe(struct jprobe *p) 2991da177e4SLinus Torvalds { 3001da177e4SLinus Torvalds return -ENOSYS; 3011da177e4SLinus Torvalds } 30226b31c19SMasami Hiramatsu static inline int register_jprobes(struct jprobe **jps, int num) 30326b31c19SMasami Hiramatsu { 30426b31c19SMasami Hiramatsu return -ENOSYS; 30526b31c19SMasami Hiramatsu } 3061da177e4SLinus Torvalds static inline void unregister_jprobe(struct jprobe *p) 3071da177e4SLinus Torvalds { 3081da177e4SLinus Torvalds } 30926b31c19SMasami Hiramatsu static inline void unregister_jprobes(struct jprobe **jps, int num) 31026b31c19SMasami Hiramatsu { 31126b31c19SMasami Hiramatsu } 3121da177e4SLinus Torvalds static inline void jprobe_return(void) 3131da177e4SLinus Torvalds { 3141da177e4SLinus Torvalds } 315b94cce92SHien Nguyen static inline int register_kretprobe(struct kretprobe *rp) 316b94cce92SHien Nguyen { 317b94cce92SHien Nguyen return -ENOSYS; 318b94cce92SHien Nguyen } 3194a296e07SMasami Hiramatsu static inline int register_kretprobes(struct kretprobe **rps, int num) 3204a296e07SMasami Hiramatsu { 3214a296e07SMasami Hiramatsu return -ENOSYS; 3224a296e07SMasami Hiramatsu } 323b94cce92SHien Nguyen static inline void unregister_kretprobe(struct kretprobe *rp) 324b94cce92SHien Nguyen { 325b94cce92SHien Nguyen } 3264a296e07SMasami Hiramatsu static inline void unregister_kretprobes(struct kretprobe **rps, int num) 3274a296e07SMasami Hiramatsu { 3284a296e07SMasami Hiramatsu } 329b94cce92SHien Nguyen static inline void kprobe_flush_task(struct task_struct *tk) 330b94cce92SHien Nguyen { 331b94cce92SHien Nguyen } 332b94cce92SHien Nguyen #endif /* CONFIG_KPROBES */ 3331da177e4SLinus Torvalds #endif /* _LINUX_KPROBES_H */ 334