xref: /linux-6.15/include/linux/kprobes.h (revision df019b1d)
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 <[email protected]> Kernel
241da177e4SLinus Torvalds  *		Probes initial implementation ( includes suggestions from
251da177e4SLinus Torvalds  *		Rusty Russell).
261da177e4SLinus Torvalds  * 2004-July	Suparna Bhattacharya <[email protected]> added jumper probes
271da177e4SLinus Torvalds  *		interface to access function arguments.
28b94cce92SHien Nguyen  * 2005-May	Hien Nguyen <[email protected]> and Jim Keniston
29b94cce92SHien Nguyen  *		<[email protected]>  and Prasanna S Panchamukhi
30b94cce92SHien Nguyen  *		<[email protected]> 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>
36e6584523SAnanth N Mavinakayanahalli #include <linux/percpu.h>
373516a460SAnanth N Mavinakayanahalli #include <linux/spinlock.h>
383516a460SAnanth N Mavinakayanahalli #include <linux/rcupdate.h>
39b94cce92SHien Nguyen 
4000d7c05aSKeshavamurthy Anil S #ifdef CONFIG_KPROBES
411da177e4SLinus Torvalds #include <asm/kprobes.h>
421da177e4SLinus Torvalds 
43ea32c65cSPrasanna S Panchamukhi /* kprobe_status settings */
44ea32c65cSPrasanna S Panchamukhi #define KPROBE_HIT_ACTIVE	0x00000001
45ea32c65cSPrasanna S Panchamukhi #define KPROBE_HIT_SS		0x00000002
46ea32c65cSPrasanna S Panchamukhi #define KPROBE_REENTER		0x00000004
47ea32c65cSPrasanna S Panchamukhi #define KPROBE_HIT_SSDONE	0x00000008
48ea32c65cSPrasanna S Panchamukhi 
49d0aaff97SPrasanna S Panchamukhi /* Attach to insert probes on any functions which should be ignored*/
50d0aaff97SPrasanna S Panchamukhi #define __kprobes	__attribute__((__section__(".kprobes.text")))
51d0aaff97SPrasanna S Panchamukhi 
521da177e4SLinus Torvalds struct kprobe;
531da177e4SLinus Torvalds struct pt_regs;
54b94cce92SHien Nguyen struct kretprobe;
55b94cce92SHien Nguyen struct kretprobe_instance;
561da177e4SLinus Torvalds typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
571da177e4SLinus Torvalds typedef int (*kprobe_break_handler_t) (struct kprobe *, struct pt_regs *);
581da177e4SLinus Torvalds typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *,
591da177e4SLinus Torvalds 				       unsigned long flags);
601da177e4SLinus Torvalds typedef int (*kprobe_fault_handler_t) (struct kprobe *, struct pt_regs *,
611da177e4SLinus Torvalds 				       int trapnr);
62b94cce92SHien Nguyen typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,
63b94cce92SHien Nguyen 				    struct pt_regs *);
64b94cce92SHien Nguyen 
651da177e4SLinus Torvalds struct kprobe {
661da177e4SLinus Torvalds 	struct hlist_node hlist;
671da177e4SLinus Torvalds 
6864f562c6SAnanth N Mavinakayanahalli 	/* list of kprobes for multi-handler support */
6964f562c6SAnanth N Mavinakayanahalli 	struct list_head list;
7064f562c6SAnanth N Mavinakayanahalli 
71*df019b1dSKeshavamurthy Anil S 	/* Indicates that the corresponding module has been ref counted */
72*df019b1dSKeshavamurthy Anil S 	unsigned int mod_refcounted;
73*df019b1dSKeshavamurthy Anil S 
74ea32c65cSPrasanna S Panchamukhi 	/*count the number of times this probe was temporarily disarmed */
75ea32c65cSPrasanna S Panchamukhi 	unsigned long nmissed;
76ea32c65cSPrasanna S Panchamukhi 
771da177e4SLinus Torvalds 	/* location of the probe point */
781da177e4SLinus Torvalds 	kprobe_opcode_t *addr;
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	/* Called before addr is executed. */
811da177e4SLinus Torvalds 	kprobe_pre_handler_t pre_handler;
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 	/* Called after addr is executed, unless... */
841da177e4SLinus Torvalds 	kprobe_post_handler_t post_handler;
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds 	/* ... called if executing addr causes a fault (eg. page fault).
871da177e4SLinus Torvalds 	 * Return 1 if it handled fault, otherwise kernel will see it. */
881da177e4SLinus Torvalds 	kprobe_fault_handler_t fault_handler;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	/* ... called if breakpoint trap occurs in probe handler.
911da177e4SLinus Torvalds 	 * Return 1 if it handled break, otherwise kernel will see it. */
921da177e4SLinus Torvalds 	kprobe_break_handler_t break_handler;
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 	/* Saved opcode (which has been replaced with breakpoint) */
951da177e4SLinus Torvalds 	kprobe_opcode_t opcode;
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	/* copy of the original instruction */
981da177e4SLinus Torvalds 	struct arch_specific_insn ainsn;
991da177e4SLinus Torvalds };
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds /*
1021da177e4SLinus Torvalds  * Special probe type that uses setjmp-longjmp type tricks to resume
1031da177e4SLinus Torvalds  * execution at a specified entry with a matching prototype corresponding
1041da177e4SLinus Torvalds  * to the probed function - a trick to enable arguments to become
1051da177e4SLinus Torvalds  * accessible seamlessly by probe handling logic.
1061da177e4SLinus Torvalds  * Note:
1071da177e4SLinus Torvalds  * Because of the way compilers allocate stack space for local variables
1081da177e4SLinus Torvalds  * etc upfront, regardless of sub-scopes within a function, this mirroring
1091da177e4SLinus Torvalds  * principle currently works only for probes placed on function entry points.
1101da177e4SLinus Torvalds  */
1111da177e4SLinus Torvalds struct jprobe {
1121da177e4SLinus Torvalds 	struct kprobe kp;
1131da177e4SLinus Torvalds 	kprobe_opcode_t *entry;	/* probe handling code to jump to */
1141da177e4SLinus Torvalds };
1151da177e4SLinus Torvalds 
116e6584523SAnanth N Mavinakayanahalli DECLARE_PER_CPU(struct kprobe *, current_kprobe);
117e6584523SAnanth N Mavinakayanahalli DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
118e6584523SAnanth N Mavinakayanahalli 
119b94cce92SHien Nguyen #ifdef ARCH_SUPPORTS_KRETPROBES
120b94cce92SHien Nguyen extern void arch_prepare_kretprobe(struct kretprobe *rp, struct pt_regs *regs);
121b94cce92SHien Nguyen #else /* ARCH_SUPPORTS_KRETPROBES */
122b94cce92SHien Nguyen static inline void arch_prepare_kretprobe(struct kretprobe *rp,
123b94cce92SHien Nguyen 					struct pt_regs *regs)
124b94cce92SHien Nguyen {
125b94cce92SHien Nguyen }
126b94cce92SHien Nguyen #endif /* ARCH_SUPPORTS_KRETPROBES */
127b94cce92SHien Nguyen /*
128b94cce92SHien Nguyen  * Function-return probe -
129b94cce92SHien Nguyen  * Note:
130b94cce92SHien Nguyen  * User needs to provide a handler function, and initialize maxactive.
131b94cce92SHien Nguyen  * maxactive - The maximum number of instances of the probed function that
132b94cce92SHien Nguyen  * can be active concurrently.
133b94cce92SHien Nguyen  * nmissed - tracks the number of times the probed function's return was
134b94cce92SHien Nguyen  * ignored, due to maxactive being too low.
135b94cce92SHien Nguyen  *
136b94cce92SHien Nguyen  */
137b94cce92SHien Nguyen struct kretprobe {
138b94cce92SHien Nguyen 	struct kprobe kp;
139b94cce92SHien Nguyen 	kretprobe_handler_t handler;
140b94cce92SHien Nguyen 	int maxactive;
141b94cce92SHien Nguyen 	int nmissed;
142b94cce92SHien Nguyen 	struct hlist_head free_instances;
143b94cce92SHien Nguyen 	struct hlist_head used_instances;
144b94cce92SHien Nguyen };
145b94cce92SHien Nguyen 
146b94cce92SHien Nguyen struct kretprobe_instance {
147b94cce92SHien Nguyen 	struct hlist_node uflist; /* either on free list or used list */
148b94cce92SHien Nguyen 	struct hlist_node hlist;
149b94cce92SHien Nguyen 	struct kretprobe *rp;
150802eae7cSRusty Lynch 	kprobe_opcode_t *ret_addr;
151802eae7cSRusty Lynch 	struct task_struct *task;
152b94cce92SHien Nguyen };
153b94cce92SHien Nguyen 
1543516a460SAnanth N Mavinakayanahalli extern spinlock_t kretprobe_lock;
1550498b635SAnanth N Mavinakayanahalli extern struct semaphore kprobe_mutex;
1561da177e4SLinus Torvalds extern int arch_prepare_kprobe(struct kprobe *p);
1577e1048b1SRusty Lynch extern void arch_arm_kprobe(struct kprobe *p);
1587e1048b1SRusty Lynch extern void arch_disarm_kprobe(struct kprobe *p);
1596772926bSRusty Lynch extern int arch_init_kprobes(void);
1601da177e4SLinus Torvalds extern void show_registers(struct pt_regs *regs);
1619ec4b1f3SAnanth N Mavinakayanahalli extern kprobe_opcode_t *get_insn_slot(void);
1629ec4b1f3SAnanth N Mavinakayanahalli extern void free_insn_slot(kprobe_opcode_t *slot);
163bf8d5c52SKeshavamurthy Anil S extern void kprobes_inc_nmissed_count(struct kprobe *p);
1641da177e4SLinus Torvalds 
165d217d545SAnanth N Mavinakayanahalli /* Get the kprobe at this addr (if any) - called with preemption disabled */
1661da177e4SLinus Torvalds struct kprobe *get_kprobe(void *addr);
167b94cce92SHien Nguyen struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk);
1681da177e4SLinus Torvalds 
169e6584523SAnanth N Mavinakayanahalli /* kprobe_running() will just return the current_kprobe on this CPU */
170e6584523SAnanth N Mavinakayanahalli static inline struct kprobe *kprobe_running(void)
171e6584523SAnanth N Mavinakayanahalli {
172e6584523SAnanth N Mavinakayanahalli 	return (__get_cpu_var(current_kprobe));
173e6584523SAnanth N Mavinakayanahalli }
174e6584523SAnanth N Mavinakayanahalli 
175e6584523SAnanth N Mavinakayanahalli static inline void reset_current_kprobe(void)
176e6584523SAnanth N Mavinakayanahalli {
177e6584523SAnanth N Mavinakayanahalli 	__get_cpu_var(current_kprobe) = NULL;
178e6584523SAnanth N Mavinakayanahalli }
179e6584523SAnanth N Mavinakayanahalli 
180e6584523SAnanth N Mavinakayanahalli static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
181e6584523SAnanth N Mavinakayanahalli {
182e6584523SAnanth N Mavinakayanahalli 	return (&__get_cpu_var(kprobe_ctlblk));
183e6584523SAnanth N Mavinakayanahalli }
184e6584523SAnanth N Mavinakayanahalli 
1851da177e4SLinus Torvalds int register_kprobe(struct kprobe *p);
1861da177e4SLinus Torvalds void unregister_kprobe(struct kprobe *p);
1871da177e4SLinus Torvalds int setjmp_pre_handler(struct kprobe *, struct pt_regs *);
1881da177e4SLinus Torvalds int longjmp_break_handler(struct kprobe *, struct pt_regs *);
1891da177e4SLinus Torvalds int register_jprobe(struct jprobe *p);
1901da177e4SLinus Torvalds void unregister_jprobe(struct jprobe *p);
1911da177e4SLinus Torvalds void jprobe_return(void);
1921da177e4SLinus Torvalds 
193b94cce92SHien Nguyen int register_kretprobe(struct kretprobe *rp);
194b94cce92SHien Nguyen void unregister_kretprobe(struct kretprobe *rp);
195b94cce92SHien Nguyen 
196b94cce92SHien Nguyen struct kretprobe_instance *get_free_rp_inst(struct kretprobe *rp);
197b94cce92SHien Nguyen void add_rp_inst(struct kretprobe_instance *ri);
198b94cce92SHien Nguyen void kprobe_flush_task(struct task_struct *tk);
199b94cce92SHien Nguyen void recycle_rp_inst(struct kretprobe_instance *ri);
200b94cce92SHien Nguyen #else /* CONFIG_KPROBES */
20100d7c05aSKeshavamurthy Anil S 
20200d7c05aSKeshavamurthy Anil S #define __kprobes	/**/
20300d7c05aSKeshavamurthy Anil S struct jprobe;
20400d7c05aSKeshavamurthy Anil S struct kretprobe;
20500d7c05aSKeshavamurthy Anil S 
206e6584523SAnanth N Mavinakayanahalli static inline struct kprobe *kprobe_running(void)
2071da177e4SLinus Torvalds {
208e6584523SAnanth N Mavinakayanahalli 	return NULL;
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds static inline int register_kprobe(struct kprobe *p)
2111da177e4SLinus Torvalds {
2121da177e4SLinus Torvalds 	return -ENOSYS;
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds static inline void unregister_kprobe(struct kprobe *p)
2151da177e4SLinus Torvalds {
2161da177e4SLinus Torvalds }
2171da177e4SLinus Torvalds static inline int register_jprobe(struct jprobe *p)
2181da177e4SLinus Torvalds {
2191da177e4SLinus Torvalds 	return -ENOSYS;
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds static inline void unregister_jprobe(struct jprobe *p)
2221da177e4SLinus Torvalds {
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds static inline void jprobe_return(void)
2251da177e4SLinus Torvalds {
2261da177e4SLinus Torvalds }
227b94cce92SHien Nguyen static inline int register_kretprobe(struct kretprobe *rp)
228b94cce92SHien Nguyen {
229b94cce92SHien Nguyen 	return -ENOSYS;
230b94cce92SHien Nguyen }
231b94cce92SHien Nguyen static inline void unregister_kretprobe(struct kretprobe *rp)
232b94cce92SHien Nguyen {
233b94cce92SHien Nguyen }
234b94cce92SHien Nguyen static inline void kprobe_flush_task(struct task_struct *tk)
235b94cce92SHien Nguyen {
236b94cce92SHien Nguyen }
237b94cce92SHien Nguyen #endif				/* CONFIG_KPROBES */
2381da177e4SLinus Torvalds #endif				/* _LINUX_KPROBES_H */
239