xref: /linux/include/linux/kprobes.h (revision e8386a0cb22f4a2d439384212c494ad0bda848fe)
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
52d0aaff97SPrasanna S Panchamukhi 
531da177e4SLinus Torvalds struct kprobe;
541da177e4SLinus Torvalds struct pt_regs;
55b94cce92SHien Nguyen struct kretprobe;
56b94cce92SHien Nguyen struct kretprobe_instance;
571da177e4SLinus Torvalds typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
581da177e4SLinus Torvalds typedef int (*kprobe_break_handler_t) (struct kprobe *, struct pt_regs *);
591da177e4SLinus Torvalds typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *,
601da177e4SLinus Torvalds 				       unsigned long flags);
611da177e4SLinus Torvalds typedef int (*kprobe_fault_handler_t) (struct kprobe *, struct pt_regs *,
621da177e4SLinus Torvalds 				       int trapnr);
63b94cce92SHien Nguyen typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,
64b94cce92SHien Nguyen 				    struct pt_regs *);
65b94cce92SHien Nguyen 
661da177e4SLinus Torvalds struct kprobe {
671da177e4SLinus Torvalds 	struct hlist_node hlist;
681da177e4SLinus Torvalds 
6964f562c6SAnanth N Mavinakayanahalli 	/* list of kprobes for multi-handler support */
7064f562c6SAnanth N Mavinakayanahalli 	struct list_head list;
7164f562c6SAnanth N Mavinakayanahalli 
72ea32c65cSPrasanna S Panchamukhi 	/*count the number of times this probe was temporarily disarmed */
73ea32c65cSPrasanna S Panchamukhi 	unsigned long nmissed;
74ea32c65cSPrasanna S Panchamukhi 
751da177e4SLinus Torvalds 	/* location of the probe point */
761da177e4SLinus Torvalds 	kprobe_opcode_t *addr;
771da177e4SLinus Torvalds 
783a872d89SAnanth N Mavinakayanahalli 	/* Allow user to indicate symbol name of the probe point */
799b3af29bSAnanth N Mavinakayanahalli 	const char *symbol_name;
803a872d89SAnanth N Mavinakayanahalli 
813a872d89SAnanth N Mavinakayanahalli 	/* Offset into the symbol */
823a872d89SAnanth N Mavinakayanahalli 	unsigned int offset;
833a872d89SAnanth N Mavinakayanahalli 
841da177e4SLinus Torvalds 	/* Called before addr is executed. */
851da177e4SLinus Torvalds 	kprobe_pre_handler_t pre_handler;
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	/* Called after addr is executed, unless... */
881da177e4SLinus Torvalds 	kprobe_post_handler_t post_handler;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	/* ... called if executing addr causes a fault (eg. page fault).
911da177e4SLinus Torvalds 	 * Return 1 if it handled fault, otherwise kernel will see it. */
921da177e4SLinus Torvalds 	kprobe_fault_handler_t fault_handler;
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 	/* ... called if breakpoint trap occurs in probe handler.
951da177e4SLinus Torvalds 	 * Return 1 if it handled break, otherwise kernel will see it. */
961da177e4SLinus Torvalds 	kprobe_break_handler_t break_handler;
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	/* Saved opcode (which has been replaced with breakpoint) */
991da177e4SLinus Torvalds 	kprobe_opcode_t opcode;
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 	/* copy of the original instruction */
1021da177e4SLinus Torvalds 	struct arch_specific_insn ainsn;
103*e8386a0cSMasami Hiramatsu 
104*e8386a0cSMasami Hiramatsu 	/* Indicates various status flags.  Protected by kprobe_mutex. */
105*e8386a0cSMasami Hiramatsu 	u32 flags;
1061da177e4SLinus Torvalds };
1071da177e4SLinus Torvalds 
108*e8386a0cSMasami Hiramatsu /* Kprobe status flags */
109*e8386a0cSMasami Hiramatsu #define KPROBE_FLAG_GONE	1 /* breakpoint has already gone */
110*e8386a0cSMasami Hiramatsu 
111*e8386a0cSMasami Hiramatsu static inline int kprobe_gone(struct kprobe *p)
112*e8386a0cSMasami Hiramatsu {
113*e8386a0cSMasami Hiramatsu 	return p->flags & KPROBE_FLAG_GONE;
114*e8386a0cSMasami Hiramatsu }
115*e8386a0cSMasami Hiramatsu 
1161da177e4SLinus Torvalds /*
1171da177e4SLinus Torvalds  * Special probe type that uses setjmp-longjmp type tricks to resume
1181da177e4SLinus Torvalds  * execution at a specified entry with a matching prototype corresponding
1191da177e4SLinus Torvalds  * to the probed function - a trick to enable arguments to become
1201da177e4SLinus Torvalds  * accessible seamlessly by probe handling logic.
1211da177e4SLinus Torvalds  * Note:
1221da177e4SLinus Torvalds  * Because of the way compilers allocate stack space for local variables
1231da177e4SLinus Torvalds  * etc upfront, regardless of sub-scopes within a function, this mirroring
1241da177e4SLinus Torvalds  * principle currently works only for probes placed on function entry points.
1251da177e4SLinus Torvalds  */
1261da177e4SLinus Torvalds struct jprobe {
1271da177e4SLinus Torvalds 	struct kprobe kp;
12881eae375SMichael Ellerman 	void *entry;	/* probe handling code to jump to */
1291da177e4SLinus Torvalds };
1301da177e4SLinus Torvalds 
1319e367d85SMichael Ellerman /* For backward compatibility with old code using JPROBE_ENTRY() */
1329e367d85SMichael Ellerman #define JPROBE_ENTRY(handler)	(handler)
1339e367d85SMichael Ellerman 
134e6584523SAnanth N Mavinakayanahalli DECLARE_PER_CPU(struct kprobe *, current_kprobe);
135e6584523SAnanth N Mavinakayanahalli DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
136e6584523SAnanth N Mavinakayanahalli 
1379edddaa2SAnanth N Mavinakayanahalli #ifdef CONFIG_KRETPROBES
1384c4308cbSChristoph Hellwig extern void arch_prepare_kretprobe(struct kretprobe_instance *ri,
1394c4308cbSChristoph Hellwig 				   struct pt_regs *regs);
140bf8f6e5bSAnanth N Mavinakayanahalli extern int arch_trampoline_kprobe(struct kprobe *p);
1419edddaa2SAnanth N Mavinakayanahalli #else /* CONFIG_KRETPROBES */
142b94cce92SHien Nguyen static inline void arch_prepare_kretprobe(struct kretprobe *rp,
143b94cce92SHien Nguyen 					struct pt_regs *regs)
144b94cce92SHien Nguyen {
145b94cce92SHien Nguyen }
146bf8f6e5bSAnanth N Mavinakayanahalli static inline int arch_trampoline_kprobe(struct kprobe *p)
147bf8f6e5bSAnanth N Mavinakayanahalli {
148bf8f6e5bSAnanth N Mavinakayanahalli 	return 0;
149bf8f6e5bSAnanth N Mavinakayanahalli }
1509edddaa2SAnanth N Mavinakayanahalli #endif /* CONFIG_KRETPROBES */
151b94cce92SHien Nguyen /*
152b94cce92SHien Nguyen  * Function-return probe -
153b94cce92SHien Nguyen  * Note:
154b94cce92SHien Nguyen  * User needs to provide a handler function, and initialize maxactive.
155b94cce92SHien Nguyen  * maxactive - The maximum number of instances of the probed function that
156b94cce92SHien Nguyen  * can be active concurrently.
157b94cce92SHien Nguyen  * nmissed - tracks the number of times the probed function's return was
158b94cce92SHien Nguyen  * ignored, due to maxactive being too low.
159b94cce92SHien Nguyen  *
160b94cce92SHien Nguyen  */
161b94cce92SHien Nguyen struct kretprobe {
162b94cce92SHien Nguyen 	struct kprobe kp;
163b94cce92SHien Nguyen 	kretprobe_handler_t handler;
164f47cd9b5SAbhishek Sagar 	kretprobe_handler_t entry_handler;
165b94cce92SHien Nguyen 	int maxactive;
166b94cce92SHien Nguyen 	int nmissed;
167f47cd9b5SAbhishek Sagar 	size_t data_size;
168b94cce92SHien Nguyen 	struct hlist_head free_instances;
169ef53d9c5SSrinivasa D S 	spinlock_t lock;
170b94cce92SHien Nguyen };
171b94cce92SHien Nguyen 
172b94cce92SHien Nguyen struct kretprobe_instance {
173b94cce92SHien Nguyen 	struct hlist_node hlist;
174b94cce92SHien Nguyen 	struct kretprobe *rp;
175802eae7cSRusty Lynch 	kprobe_opcode_t *ret_addr;
176802eae7cSRusty Lynch 	struct task_struct *task;
177f47cd9b5SAbhishek Sagar 	char data[0];
178b94cce92SHien Nguyen };
179b94cce92SHien Nguyen 
180f438d914SMasami Hiramatsu struct kretprobe_blackpoint {
181f438d914SMasami Hiramatsu 	const char *name;
182f438d914SMasami Hiramatsu 	void *addr;
183f438d914SMasami Hiramatsu };
1843d8d996eSSrinivasa Ds 
1853d8d996eSSrinivasa Ds struct kprobe_blackpoint {
1863d8d996eSSrinivasa Ds 	const char *name;
1873d8d996eSSrinivasa Ds 	unsigned long start_addr;
1883d8d996eSSrinivasa Ds 	unsigned long range;
1893d8d996eSSrinivasa Ds };
1903d8d996eSSrinivasa Ds 
191f438d914SMasami Hiramatsu extern struct kretprobe_blackpoint kretprobe_blacklist[];
192f438d914SMasami Hiramatsu 
1930f95b7fcSAnanth N Mavinakayanahalli static inline void kretprobe_assert(struct kretprobe_instance *ri,
1940f95b7fcSAnanth N Mavinakayanahalli 	unsigned long orig_ret_address, unsigned long trampoline_address)
1950f95b7fcSAnanth N Mavinakayanahalli {
1960f95b7fcSAnanth N Mavinakayanahalli 	if (!orig_ret_address || (orig_ret_address == trampoline_address)) {
1970f95b7fcSAnanth N Mavinakayanahalli 		printk("kretprobe BUG!: Processing kretprobe %p @ %p\n",
1980f95b7fcSAnanth N Mavinakayanahalli 				ri->rp, ri->rp->kp.addr);
1990f95b7fcSAnanth N Mavinakayanahalli 		BUG();
2000f95b7fcSAnanth N Mavinakayanahalli 	}
2010f95b7fcSAnanth N Mavinakayanahalli }
2020f95b7fcSAnanth N Mavinakayanahalli 
2038c1c9356SAnanth N Mavinakayanahalli #ifdef CONFIG_KPROBES_SANITY_TEST
2048c1c9356SAnanth N Mavinakayanahalli extern int init_test_probes(void);
2058c1c9356SAnanth N Mavinakayanahalli #else
2068c1c9356SAnanth N Mavinakayanahalli static inline int init_test_probes(void)
2078c1c9356SAnanth N Mavinakayanahalli {
2088c1c9356SAnanth N Mavinakayanahalli 	return 0;
2098c1c9356SAnanth N Mavinakayanahalli }
2108c1c9356SAnanth N Mavinakayanahalli #endif /* CONFIG_KPROBES_SANITY_TEST */
2118c1c9356SAnanth N Mavinakayanahalli 
2121da177e4SLinus Torvalds extern int arch_prepare_kprobe(struct kprobe *p);
2137e1048b1SRusty Lynch extern void arch_arm_kprobe(struct kprobe *p);
2147e1048b1SRusty Lynch extern void arch_disarm_kprobe(struct kprobe *p);
2156772926bSRusty Lynch extern int arch_init_kprobes(void);
2161da177e4SLinus Torvalds extern void show_registers(struct pt_regs *regs);
2179ec4b1f3SAnanth N Mavinakayanahalli extern kprobe_opcode_t *get_insn_slot(void);
218b4c6c34aSMasami Hiramatsu extern void free_insn_slot(kprobe_opcode_t *slot, int dirty);
219bf8d5c52SKeshavamurthy Anil S extern void kprobes_inc_nmissed_count(struct kprobe *p);
2201da177e4SLinus Torvalds 
221d217d545SAnanth N Mavinakayanahalli /* Get the kprobe at this addr (if any) - called with preemption disabled */
2221da177e4SLinus Torvalds struct kprobe *get_kprobe(void *addr);
223ef53d9c5SSrinivasa D S void kretprobe_hash_lock(struct task_struct *tsk,
224ef53d9c5SSrinivasa D S 			 struct hlist_head **head, unsigned long *flags);
225ef53d9c5SSrinivasa D S void kretprobe_hash_unlock(struct task_struct *tsk, unsigned long *flags);
226b94cce92SHien Nguyen struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk);
2271da177e4SLinus Torvalds 
228e6584523SAnanth N Mavinakayanahalli /* kprobe_running() will just return the current_kprobe on this CPU */
229e6584523SAnanth N Mavinakayanahalli static inline struct kprobe *kprobe_running(void)
230e6584523SAnanth N Mavinakayanahalli {
231e6584523SAnanth N Mavinakayanahalli 	return (__get_cpu_var(current_kprobe));
232e6584523SAnanth N Mavinakayanahalli }
233e6584523SAnanth N Mavinakayanahalli 
234e6584523SAnanth N Mavinakayanahalli static inline void reset_current_kprobe(void)
235e6584523SAnanth N Mavinakayanahalli {
236e6584523SAnanth N Mavinakayanahalli 	__get_cpu_var(current_kprobe) = NULL;
237e6584523SAnanth N Mavinakayanahalli }
238e6584523SAnanth N Mavinakayanahalli 
239e6584523SAnanth N Mavinakayanahalli static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
240e6584523SAnanth N Mavinakayanahalli {
241e6584523SAnanth N Mavinakayanahalli 	return (&__get_cpu_var(kprobe_ctlblk));
242e6584523SAnanth N Mavinakayanahalli }
243e6584523SAnanth N Mavinakayanahalli 
2441da177e4SLinus Torvalds int register_kprobe(struct kprobe *p);
2451da177e4SLinus Torvalds void unregister_kprobe(struct kprobe *p);
2469861668fSMasami Hiramatsu int register_kprobes(struct kprobe **kps, int num);
2479861668fSMasami Hiramatsu void unregister_kprobes(struct kprobe **kps, int num);
2481da177e4SLinus Torvalds int setjmp_pre_handler(struct kprobe *, struct pt_regs *);
2491da177e4SLinus Torvalds int longjmp_break_handler(struct kprobe *, struct pt_regs *);
2501da177e4SLinus Torvalds int register_jprobe(struct jprobe *p);
2511da177e4SLinus Torvalds void unregister_jprobe(struct jprobe *p);
25226b31c19SMasami Hiramatsu int register_jprobes(struct jprobe **jps, int num);
25326b31c19SMasami Hiramatsu void unregister_jprobes(struct jprobe **jps, int num);
2541da177e4SLinus Torvalds void jprobe_return(void);
2553d7e3382SMichael Ellerman unsigned long arch_deref_entry_point(void *);
2561da177e4SLinus Torvalds 
257b94cce92SHien Nguyen int register_kretprobe(struct kretprobe *rp);
258b94cce92SHien Nguyen void unregister_kretprobe(struct kretprobe *rp);
2594a296e07SMasami Hiramatsu int register_kretprobes(struct kretprobe **rps, int num);
2604a296e07SMasami Hiramatsu void unregister_kretprobes(struct kretprobe **rps, int num);
261b94cce92SHien Nguyen 
262b94cce92SHien Nguyen void kprobe_flush_task(struct task_struct *tk);
26399219a3fSbibo,mao void recycle_rp_inst(struct kretprobe_instance *ri, struct hlist_head *head);
2648c1c9356SAnanth N Mavinakayanahalli 
265b94cce92SHien Nguyen #else /* CONFIG_KPROBES */
26600d7c05aSKeshavamurthy Anil S 
26736dcd67aSIngo Molnar #define __kprobes	notrace
26800d7c05aSKeshavamurthy Anil S struct jprobe;
26900d7c05aSKeshavamurthy Anil S struct kretprobe;
27000d7c05aSKeshavamurthy Anil S 
271785656a4SAbhishek Sagar static inline struct kprobe *get_kprobe(void *addr)
272785656a4SAbhishek Sagar {
273785656a4SAbhishek Sagar 	return NULL;
274785656a4SAbhishek Sagar }
275e6584523SAnanth N Mavinakayanahalli static inline struct kprobe *kprobe_running(void)
2761da177e4SLinus Torvalds {
277e6584523SAnanth N Mavinakayanahalli 	return NULL;
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds static inline int register_kprobe(struct kprobe *p)
2801da177e4SLinus Torvalds {
2811da177e4SLinus Torvalds 	return -ENOSYS;
2821da177e4SLinus Torvalds }
2839861668fSMasami Hiramatsu static inline int register_kprobes(struct kprobe **kps, int num)
2849861668fSMasami Hiramatsu {
2859861668fSMasami Hiramatsu 	return -ENOSYS;
2869861668fSMasami Hiramatsu }
2871da177e4SLinus Torvalds static inline void unregister_kprobe(struct kprobe *p)
2881da177e4SLinus Torvalds {
2891da177e4SLinus Torvalds }
2909861668fSMasami Hiramatsu static inline void unregister_kprobes(struct kprobe **kps, int num)
2919861668fSMasami Hiramatsu {
2929861668fSMasami Hiramatsu }
2931da177e4SLinus Torvalds static inline int register_jprobe(struct jprobe *p)
2941da177e4SLinus Torvalds {
2951da177e4SLinus Torvalds 	return -ENOSYS;
2961da177e4SLinus Torvalds }
29726b31c19SMasami Hiramatsu static inline int register_jprobes(struct jprobe **jps, int num)
29826b31c19SMasami Hiramatsu {
29926b31c19SMasami Hiramatsu 	return -ENOSYS;
30026b31c19SMasami Hiramatsu }
3011da177e4SLinus Torvalds static inline void unregister_jprobe(struct jprobe *p)
3021da177e4SLinus Torvalds {
3031da177e4SLinus Torvalds }
30426b31c19SMasami Hiramatsu static inline void unregister_jprobes(struct jprobe **jps, int num)
30526b31c19SMasami Hiramatsu {
30626b31c19SMasami Hiramatsu }
3071da177e4SLinus Torvalds static inline void jprobe_return(void)
3081da177e4SLinus Torvalds {
3091da177e4SLinus Torvalds }
310b94cce92SHien Nguyen static inline int register_kretprobe(struct kretprobe *rp)
311b94cce92SHien Nguyen {
312b94cce92SHien Nguyen 	return -ENOSYS;
313b94cce92SHien Nguyen }
3144a296e07SMasami Hiramatsu static inline int register_kretprobes(struct kretprobe **rps, int num)
3154a296e07SMasami Hiramatsu {
3164a296e07SMasami Hiramatsu 	return -ENOSYS;
3174a296e07SMasami Hiramatsu }
318b94cce92SHien Nguyen static inline void unregister_kretprobe(struct kretprobe *rp)
319b94cce92SHien Nguyen {
320b94cce92SHien Nguyen }
3214a296e07SMasami Hiramatsu static inline void unregister_kretprobes(struct kretprobe **rps, int num)
3224a296e07SMasami Hiramatsu {
3234a296e07SMasami Hiramatsu }
324b94cce92SHien Nguyen static inline void kprobe_flush_task(struct task_struct *tk)
325b94cce92SHien Nguyen {
326b94cce92SHien Nguyen }
327b94cce92SHien Nguyen #endif				/* CONFIG_KPROBES */
3281da177e4SLinus Torvalds #endif				/* _LINUX_KPROBES_H */
329