xref: /linux/include/linux/kprobes.h (revision d0aaff9796c3310326d10da44fc0faed352a1d29)
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  */
321da177e4SLinus Torvalds #include <linux/config.h>
331da177e4SLinus Torvalds #include <linux/list.h>
341da177e4SLinus Torvalds #include <linux/notifier.h>
351da177e4SLinus Torvalds #include <linux/smp.h>
36b94cce92SHien Nguyen 
371da177e4SLinus Torvalds #include <asm/kprobes.h>
381da177e4SLinus Torvalds 
39ea32c65cSPrasanna S Panchamukhi /* kprobe_status settings */
40ea32c65cSPrasanna S Panchamukhi #define KPROBE_HIT_ACTIVE	0x00000001
41ea32c65cSPrasanna S Panchamukhi #define KPROBE_HIT_SS		0x00000002
42ea32c65cSPrasanna S Panchamukhi #define KPROBE_REENTER		0x00000004
43ea32c65cSPrasanna S Panchamukhi #define KPROBE_HIT_SSDONE	0x00000008
44ea32c65cSPrasanna S Panchamukhi 
45*d0aaff97SPrasanna S Panchamukhi /* Attach to insert probes on any functions which should be ignored*/
46*d0aaff97SPrasanna S Panchamukhi #define __kprobes	__attribute__((__section__(".kprobes.text")))
47*d0aaff97SPrasanna S Panchamukhi 
481da177e4SLinus Torvalds struct kprobe;
491da177e4SLinus Torvalds struct pt_regs;
50b94cce92SHien Nguyen struct kretprobe;
51b94cce92SHien Nguyen struct kretprobe_instance;
521da177e4SLinus Torvalds typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
531da177e4SLinus Torvalds typedef int (*kprobe_break_handler_t) (struct kprobe *, struct pt_regs *);
541da177e4SLinus Torvalds typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *,
551da177e4SLinus Torvalds 				       unsigned long flags);
561da177e4SLinus Torvalds typedef int (*kprobe_fault_handler_t) (struct kprobe *, struct pt_regs *,
571da177e4SLinus Torvalds 				       int trapnr);
58b94cce92SHien Nguyen typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,
59b94cce92SHien Nguyen 				    struct pt_regs *);
60b94cce92SHien Nguyen 
611da177e4SLinus Torvalds struct kprobe {
621da177e4SLinus Torvalds 	struct hlist_node hlist;
631da177e4SLinus Torvalds 
6464f562c6SAnanth N Mavinakayanahalli 	/* list of kprobes for multi-handler support */
6564f562c6SAnanth N Mavinakayanahalli 	struct list_head list;
6664f562c6SAnanth N Mavinakayanahalli 
67ea32c65cSPrasanna S Panchamukhi 	/*count the number of times this probe was temporarily disarmed */
68ea32c65cSPrasanna S Panchamukhi 	unsigned long nmissed;
69ea32c65cSPrasanna S Panchamukhi 
701da177e4SLinus Torvalds 	/* location of the probe point */
711da177e4SLinus Torvalds 	kprobe_opcode_t *addr;
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds 	/* Called before addr is executed. */
741da177e4SLinus Torvalds 	kprobe_pre_handler_t pre_handler;
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds 	/* Called after addr is executed, unless... */
771da177e4SLinus Torvalds 	kprobe_post_handler_t post_handler;
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	/* ... called if executing addr causes a fault (eg. page fault).
801da177e4SLinus Torvalds 	 * Return 1 if it handled fault, otherwise kernel will see it. */
811da177e4SLinus Torvalds 	kprobe_fault_handler_t fault_handler;
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 	/* ... called if breakpoint trap occurs in probe handler.
841da177e4SLinus Torvalds 	 * Return 1 if it handled break, otherwise kernel will see it. */
851da177e4SLinus Torvalds 	kprobe_break_handler_t break_handler;
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	/* Saved opcode (which has been replaced with breakpoint) */
881da177e4SLinus Torvalds 	kprobe_opcode_t opcode;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	/* copy of the original instruction */
911da177e4SLinus Torvalds 	struct arch_specific_insn ainsn;
921da177e4SLinus Torvalds };
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds /*
951da177e4SLinus Torvalds  * Special probe type that uses setjmp-longjmp type tricks to resume
961da177e4SLinus Torvalds  * execution at a specified entry with a matching prototype corresponding
971da177e4SLinus Torvalds  * to the probed function - a trick to enable arguments to become
981da177e4SLinus Torvalds  * accessible seamlessly by probe handling logic.
991da177e4SLinus Torvalds  * Note:
1001da177e4SLinus Torvalds  * Because of the way compilers allocate stack space for local variables
1011da177e4SLinus Torvalds  * etc upfront, regardless of sub-scopes within a function, this mirroring
1021da177e4SLinus Torvalds  * principle currently works only for probes placed on function entry points.
1031da177e4SLinus Torvalds  */
1041da177e4SLinus Torvalds struct jprobe {
1051da177e4SLinus Torvalds 	struct kprobe kp;
1061da177e4SLinus Torvalds 	kprobe_opcode_t *entry;	/* probe handling code to jump to */
1071da177e4SLinus Torvalds };
1081da177e4SLinus Torvalds 
109b94cce92SHien Nguyen #ifdef ARCH_SUPPORTS_KRETPROBES
110b94cce92SHien Nguyen extern void arch_prepare_kretprobe(struct kretprobe *rp, struct pt_regs *regs);
111b94cce92SHien Nguyen #else /* ARCH_SUPPORTS_KRETPROBES */
112b94cce92SHien Nguyen static inline void arch_prepare_kretprobe(struct kretprobe *rp,
113b94cce92SHien Nguyen 					struct pt_regs *regs)
114b94cce92SHien Nguyen {
115b94cce92SHien Nguyen }
116b94cce92SHien Nguyen #endif /* ARCH_SUPPORTS_KRETPROBES */
117b94cce92SHien Nguyen /*
118b94cce92SHien Nguyen  * Function-return probe -
119b94cce92SHien Nguyen  * Note:
120b94cce92SHien Nguyen  * User needs to provide a handler function, and initialize maxactive.
121b94cce92SHien Nguyen  * maxactive - The maximum number of instances of the probed function that
122b94cce92SHien Nguyen  * can be active concurrently.
123b94cce92SHien Nguyen  * nmissed - tracks the number of times the probed function's return was
124b94cce92SHien Nguyen  * ignored, due to maxactive being too low.
125b94cce92SHien Nguyen  *
126b94cce92SHien Nguyen  */
127b94cce92SHien Nguyen struct kretprobe {
128b94cce92SHien Nguyen 	struct kprobe kp;
129b94cce92SHien Nguyen 	kretprobe_handler_t handler;
130b94cce92SHien Nguyen 	int maxactive;
131b94cce92SHien Nguyen 	int nmissed;
132b94cce92SHien Nguyen 	struct hlist_head free_instances;
133b94cce92SHien Nguyen 	struct hlist_head used_instances;
134b94cce92SHien Nguyen };
135b94cce92SHien Nguyen 
136b94cce92SHien Nguyen struct kretprobe_instance {
137b94cce92SHien Nguyen 	struct hlist_node uflist; /* either on free list or used list */
138b94cce92SHien Nguyen 	struct hlist_node hlist;
139b94cce92SHien Nguyen 	struct kretprobe *rp;
140802eae7cSRusty Lynch 	kprobe_opcode_t *ret_addr;
141802eae7cSRusty Lynch 	struct task_struct *task;
142b94cce92SHien Nguyen };
143b94cce92SHien Nguyen 
1441da177e4SLinus Torvalds #ifdef CONFIG_KPROBES
1451da177e4SLinus Torvalds /* Locks kprobe: irq must be disabled */
1461da177e4SLinus Torvalds void lock_kprobes(void);
1471da177e4SLinus Torvalds void unlock_kprobes(void);
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds /* kprobe running now on this CPU? */
1501da177e4SLinus Torvalds static inline int kprobe_running(void)
1511da177e4SLinus Torvalds {
1521da177e4SLinus Torvalds 	extern unsigned int kprobe_cpu;
1531da177e4SLinus Torvalds 	return kprobe_cpu == smp_processor_id();
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds extern int arch_prepare_kprobe(struct kprobe *p);
1571da177e4SLinus Torvalds extern void arch_copy_kprobe(struct kprobe *p);
1587e1048b1SRusty Lynch extern void arch_arm_kprobe(struct kprobe *p);
1597e1048b1SRusty Lynch extern void arch_disarm_kprobe(struct kprobe *p);
1601da177e4SLinus Torvalds extern void arch_remove_kprobe(struct kprobe *p);
1616772926bSRusty Lynch extern int arch_init_kprobes(void);
1621da177e4SLinus Torvalds extern void show_registers(struct pt_regs *regs);
1639ec4b1f3SAnanth N Mavinakayanahalli extern kprobe_opcode_t *get_insn_slot(void);
1649ec4b1f3SAnanth N Mavinakayanahalli extern void free_insn_slot(kprobe_opcode_t *slot);
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds /* Get the kprobe at this addr (if any).  Must have called lock_kprobes */
1671da177e4SLinus Torvalds struct kprobe *get_kprobe(void *addr);
168b94cce92SHien Nguyen struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk);
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds int register_kprobe(struct kprobe *p);
1711da177e4SLinus Torvalds void unregister_kprobe(struct kprobe *p);
1721da177e4SLinus Torvalds int setjmp_pre_handler(struct kprobe *, struct pt_regs *);
1731da177e4SLinus Torvalds int longjmp_break_handler(struct kprobe *, struct pt_regs *);
1741da177e4SLinus Torvalds int register_jprobe(struct jprobe *p);
1751da177e4SLinus Torvalds void unregister_jprobe(struct jprobe *p);
1761da177e4SLinus Torvalds void jprobe_return(void);
1771da177e4SLinus Torvalds 
178b94cce92SHien Nguyen int register_kretprobe(struct kretprobe *rp);
179b94cce92SHien Nguyen void unregister_kretprobe(struct kretprobe *rp);
180b94cce92SHien Nguyen 
181b94cce92SHien Nguyen struct kretprobe_instance *get_free_rp_inst(struct kretprobe *rp);
182b94cce92SHien Nguyen void add_rp_inst(struct kretprobe_instance *ri);
183b94cce92SHien Nguyen void kprobe_flush_task(struct task_struct *tk);
184b94cce92SHien Nguyen void recycle_rp_inst(struct kretprobe_instance *ri);
185b94cce92SHien Nguyen #else /* CONFIG_KPROBES */
1861da177e4SLinus Torvalds static inline int kprobe_running(void)
1871da177e4SLinus Torvalds {
1881da177e4SLinus Torvalds 	return 0;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds static inline int register_kprobe(struct kprobe *p)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	return -ENOSYS;
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds static inline void unregister_kprobe(struct kprobe *p)
1951da177e4SLinus Torvalds {
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds static inline int register_jprobe(struct jprobe *p)
1981da177e4SLinus Torvalds {
1991da177e4SLinus Torvalds 	return -ENOSYS;
2001da177e4SLinus Torvalds }
2011da177e4SLinus Torvalds static inline void unregister_jprobe(struct jprobe *p)
2021da177e4SLinus Torvalds {
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds static inline void jprobe_return(void)
2051da177e4SLinus Torvalds {
2061da177e4SLinus Torvalds }
207b94cce92SHien Nguyen static inline int register_kretprobe(struct kretprobe *rp)
208b94cce92SHien Nguyen {
209b94cce92SHien Nguyen 	return -ENOSYS;
210b94cce92SHien Nguyen }
211b94cce92SHien Nguyen static inline void unregister_kretprobe(struct kretprobe *rp)
212b94cce92SHien Nguyen {
213b94cce92SHien Nguyen }
214b94cce92SHien Nguyen static inline void kprobe_flush_task(struct task_struct *tk)
215b94cce92SHien Nguyen {
216b94cce92SHien Nguyen }
217b94cce92SHien Nguyen #endif				/* CONFIG_KPROBES */
2181da177e4SLinus Torvalds #endif				/* _LINUX_KPROBES_H */
219