xref: /linux/include/linux/kprobes.h (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
11a59d1b8SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
21da177e4SLinus Torvalds #ifndef _LINUX_KPROBES_H
31da177e4SLinus Torvalds #define _LINUX_KPROBES_H
41da177e4SLinus Torvalds /*
51da177e4SLinus Torvalds  *  Kernel Probes (KProbes)
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Copyright (C) IBM Corporation, 2002, 2004
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * 2002-Oct	Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
101da177e4SLinus Torvalds  *		Probes initial implementation ( includes suggestions from
111da177e4SLinus Torvalds  *		Rusty Russell).
121da177e4SLinus Torvalds  * 2004-July	Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
131da177e4SLinus Torvalds  *		interface to access function arguments.
14b94cce92SHien Nguyen  * 2005-May	Hien Nguyen <hien@us.ibm.com> and Jim Keniston
15b94cce92SHien Nguyen  *		<jkenisto@us.ibm.com>  and Prasanna S Panchamukhi
16b94cce92SHien Nguyen  *		<prasanna@in.ibm.com> added function-return probes.
171da177e4SLinus Torvalds  */
187d134b2cSLuis R. Rodriguez #include <linux/compiler.h>
1936dcd67aSIngo Molnar #include <linux/linkage.h>
201da177e4SLinus Torvalds #include <linux/list.h>
211da177e4SLinus Torvalds #include <linux/notifier.h>
221da177e4SLinus Torvalds #include <linux/smp.h>
23187f1882SPaul Gortmaker #include <linux/bug.h>
24e6584523SAnanth N Mavinakayanahalli #include <linux/percpu.h>
253516a460SAnanth N Mavinakayanahalli #include <linux/spinlock.h>
263516a460SAnanth N Mavinakayanahalli #include <linux/rcupdate.h>
277a7d1cf9SIngo Molnar #include <linux/mutex.h>
28ae6aa16fSMasami Hiramatsu #include <linux/ftrace.h>
294bbd9345Swuqiang.matt #include <linux/objpool.h>
3073f9b911SMasami Hiramatsu #include <linux/rethook.h>
317d134b2cSLuis R. Rodriguez #include <asm/kprobes.h>
32b94cce92SHien Nguyen 
3300d7c05aSKeshavamurthy Anil S #ifdef CONFIG_KPROBES
341da177e4SLinus Torvalds 
35ea32c65cSPrasanna S Panchamukhi /* kprobe_status settings */
36ea32c65cSPrasanna S Panchamukhi #define KPROBE_HIT_ACTIVE	0x00000001
37ea32c65cSPrasanna S Panchamukhi #define KPROBE_HIT_SS		0x00000002
38ea32c65cSPrasanna S Panchamukhi #define KPROBE_REENTER		0x00000004
39ea32c65cSPrasanna S Panchamukhi #define KPROBE_HIT_SSDONE	0x00000008
40ea32c65cSPrasanna S Panchamukhi 
41223a76b2SMasami Hiramatsu #else /* !CONFIG_KPROBES */
427d134b2cSLuis R. Rodriguez #include <asm-generic/kprobes.h>
43dc19835dSMasami Hiramatsu typedef int kprobe_opcode_t;
44dc19835dSMasami Hiramatsu struct arch_specific_insn {
45dc19835dSMasami Hiramatsu 	int dummy;
46dc19835dSMasami Hiramatsu };
47dc19835dSMasami Hiramatsu #endif /* CONFIG_KPROBES */
48d0aaff97SPrasanna S Panchamukhi 
491da177e4SLinus Torvalds struct kprobe;
501da177e4SLinus Torvalds struct pt_regs;
51b94cce92SHien Nguyen struct kretprobe;
52b94cce92SHien Nguyen struct kretprobe_instance;
531da177e4SLinus Torvalds typedef int (*kprobe_pre_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);
56b94cce92SHien Nguyen typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,
57b94cce92SHien Nguyen 				    struct pt_regs *);
58b94cce92SHien Nguyen 
591da177e4SLinus Torvalds struct kprobe {
601da177e4SLinus Torvalds 	struct hlist_node hlist;
611da177e4SLinus Torvalds 
6264f562c6SAnanth N Mavinakayanahalli 	/* list of kprobes for multi-handler support */
6364f562c6SAnanth N Mavinakayanahalli 	struct list_head list;
6464f562c6SAnanth N Mavinakayanahalli 
65ea32c65cSPrasanna S Panchamukhi 	/*count the number of times this probe was temporarily disarmed */
66ea32c65cSPrasanna S Panchamukhi 	unsigned long nmissed;
67ea32c65cSPrasanna S Panchamukhi 
681da177e4SLinus Torvalds 	/* location of the probe point */
691da177e4SLinus Torvalds 	kprobe_opcode_t *addr;
701da177e4SLinus Torvalds 
713a872d89SAnanth N Mavinakayanahalli 	/* Allow user to indicate symbol name of the probe point */
729b3af29bSAnanth N Mavinakayanahalli 	const char *symbol_name;
733a872d89SAnanth N Mavinakayanahalli 
743a872d89SAnanth N Mavinakayanahalli 	/* Offset into the symbol */
753a872d89SAnanth N Mavinakayanahalli 	unsigned int offset;
763a872d89SAnanth N Mavinakayanahalli 
771da177e4SLinus Torvalds 	/* Called before addr is executed. */
781da177e4SLinus Torvalds 	kprobe_pre_handler_t pre_handler;
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	/* Called after addr is executed, unless... */
811da177e4SLinus Torvalds 	kprobe_post_handler_t post_handler;
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 	/* Saved opcode (which has been replaced with breakpoint) */
841da177e4SLinus Torvalds 	kprobe_opcode_t opcode;
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds 	/* copy of the original instruction */
871da177e4SLinus Torvalds 	struct arch_specific_insn ainsn;
88e8386a0cSMasami Hiramatsu 
89de5bd88dSMasami Hiramatsu 	/*
90de5bd88dSMasami Hiramatsu 	 * Indicates various status flags.
91de5bd88dSMasami Hiramatsu 	 * Protected by kprobe_mutex after this kprobe is registered.
92de5bd88dSMasami Hiramatsu 	 */
93e8386a0cSMasami Hiramatsu 	u32 flags;
941da177e4SLinus Torvalds };
951da177e4SLinus Torvalds 
96e8386a0cSMasami Hiramatsu /* Kprobe status flags */
97e8386a0cSMasami Hiramatsu #define KPROBE_FLAG_GONE	1 /* breakpoint has already gone */
98de5bd88dSMasami Hiramatsu #define KPROBE_FLAG_DISABLED	2 /* probe is temporarily disabled */
99afd66255SMasami Hiramatsu #define KPROBE_FLAG_OPTIMIZED	4 /*
100afd66255SMasami Hiramatsu 				   * probe is really optimized.
101afd66255SMasami Hiramatsu 				   * NOTE:
102afd66255SMasami Hiramatsu 				   * this flag is only for optimized_kprobe.
103afd66255SMasami Hiramatsu 				   */
104ae6aa16fSMasami Hiramatsu #define KPROBE_FLAG_FTRACE	8 /* probe is using ftrace */
105bf7a87f1SJiri Olsa #define KPROBE_FLAG_ON_FUNC_ENTRY	16 /* probe is on the function entry */
106e8386a0cSMasami Hiramatsu 
107de5bd88dSMasami Hiramatsu /* Has this kprobe gone ? */
kprobe_gone(struct kprobe * p)10829e8077aSMasami Hiramatsu static inline bool kprobe_gone(struct kprobe *p)
109e8386a0cSMasami Hiramatsu {
110e8386a0cSMasami Hiramatsu 	return p->flags & KPROBE_FLAG_GONE;
111e8386a0cSMasami Hiramatsu }
112e8386a0cSMasami Hiramatsu 
113de5bd88dSMasami Hiramatsu /* Is this kprobe disabled ? */
kprobe_disabled(struct kprobe * p)11429e8077aSMasami Hiramatsu static inline bool kprobe_disabled(struct kprobe *p)
115de5bd88dSMasami Hiramatsu {
116de5bd88dSMasami Hiramatsu 	return p->flags & (KPROBE_FLAG_DISABLED | KPROBE_FLAG_GONE);
117de5bd88dSMasami Hiramatsu }
118afd66255SMasami Hiramatsu 
119afd66255SMasami Hiramatsu /* Is this kprobe really running optimized path ? */
kprobe_optimized(struct kprobe * p)12029e8077aSMasami Hiramatsu static inline bool kprobe_optimized(struct kprobe *p)
121afd66255SMasami Hiramatsu {
122afd66255SMasami Hiramatsu 	return p->flags & KPROBE_FLAG_OPTIMIZED;
123afd66255SMasami Hiramatsu }
124ae6aa16fSMasami Hiramatsu 
125ae6aa16fSMasami Hiramatsu /* Is this kprobe uses ftrace ? */
kprobe_ftrace(struct kprobe * p)12629e8077aSMasami Hiramatsu static inline bool kprobe_ftrace(struct kprobe *p)
127ae6aa16fSMasami Hiramatsu {
128ae6aa16fSMasami Hiramatsu 	return p->flags & KPROBE_FLAG_FTRACE;
129ae6aa16fSMasami Hiramatsu }
130ae6aa16fSMasami Hiramatsu 
1311da177e4SLinus Torvalds /*
132b94cce92SHien Nguyen  * Function-return probe -
133b94cce92SHien Nguyen  * Note:
134b94cce92SHien Nguyen  * User needs to provide a handler function, and initialize maxactive.
135b94cce92SHien Nguyen  * maxactive - The maximum number of instances of the probed function that
136b94cce92SHien Nguyen  * can be active concurrently.
137b94cce92SHien Nguyen  * nmissed - tracks the number of times the probed function's return was
138b94cce92SHien Nguyen  * ignored, due to maxactive being too low.
139b94cce92SHien Nguyen  *
140b94cce92SHien Nguyen  */
141d741bf41SPeter Zijlstra struct kretprobe_holder {
142d839a656SJP Kobryn 	struct kretprobe __rcu *rp;
1434bbd9345Swuqiang.matt 	struct objpool_head	pool;
144d741bf41SPeter Zijlstra };
145d741bf41SPeter Zijlstra 
146b94cce92SHien Nguyen struct kretprobe {
147b94cce92SHien Nguyen 	struct kprobe kp;
148b94cce92SHien Nguyen 	kretprobe_handler_t handler;
149f47cd9b5SAbhishek Sagar 	kretprobe_handler_t entry_handler;
150b94cce92SHien Nguyen 	int maxactive;
151b94cce92SHien Nguyen 	int nmissed;
152f47cd9b5SAbhishek Sagar 	size_t data_size;
15373f9b911SMasami Hiramatsu #ifdef CONFIG_KRETPROBE_ON_RETHOOK
15473f9b911SMasami Hiramatsu 	struct rethook *rh;
15573f9b911SMasami Hiramatsu #else
156d741bf41SPeter Zijlstra 	struct kretprobe_holder *rph;
15773f9b911SMasami Hiramatsu #endif
158b94cce92SHien Nguyen };
159b94cce92SHien Nguyen 
1606bbfa441SMasami Hiramatsu #define KRETPROBE_MAX_DATA_SIZE	4096
1616bbfa441SMasami Hiramatsu 
162b94cce92SHien Nguyen struct kretprobe_instance {
16373f9b911SMasami Hiramatsu #ifdef CONFIG_KRETPROBE_ON_RETHOOK
16473f9b911SMasami Hiramatsu 	struct rethook_node node;
16573f9b911SMasami Hiramatsu #else
166b3388178SMasami Hiramatsu 	struct rcu_head rcu;
1676e426e0fSPeter Zijlstra 	struct llist_node llist;
168d741bf41SPeter Zijlstra 	struct kretprobe_holder *rph;
169802eae7cSRusty Lynch 	kprobe_opcode_t *ret_addr;
1703ff9c075SMasami Hiramatsu 	void *fp;
17173f9b911SMasami Hiramatsu #endif
17267a862a9SGustavo A. R. Silva 	char data[];
173b94cce92SHien Nguyen };
174b94cce92SHien Nguyen 
175f438d914SMasami Hiramatsu struct kretprobe_blackpoint {
176f438d914SMasami Hiramatsu 	const char *name;
177f438d914SMasami Hiramatsu 	void *addr;
178f438d914SMasami Hiramatsu };
1793d8d996eSSrinivasa Ds 
180376e2424SMasami Hiramatsu struct kprobe_blacklist_entry {
181376e2424SMasami Hiramatsu 	struct list_head list;
1823d8d996eSSrinivasa Ds 	unsigned long start_addr;
183376e2424SMasami Hiramatsu 	unsigned long end_addr;
1843d8d996eSSrinivasa Ds };
1853d8d996eSSrinivasa Ds 
186dc19835dSMasami Hiramatsu #ifdef CONFIG_KPROBES
187dc19835dSMasami Hiramatsu DECLARE_PER_CPU(struct kprobe *, current_kprobe);
188dc19835dSMasami Hiramatsu DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
189dc19835dSMasami Hiramatsu 
19066ada2ccSMasami Hiramatsu extern void kprobe_busy_begin(void);
19166ada2ccSMasami Hiramatsu extern void kprobe_busy_end(void);
19266ada2ccSMasami Hiramatsu 
193dc19835dSMasami Hiramatsu #ifdef CONFIG_KRETPROBES
19473f9b911SMasami Hiramatsu /* Check whether @p is used for implementing a trampoline. */
195dc19835dSMasami Hiramatsu extern int arch_trampoline_kprobe(struct kprobe *p);
19666ada2ccSMasami Hiramatsu 
19773f9b911SMasami Hiramatsu #ifdef CONFIG_KRETPROBE_ON_RETHOOK
get_kretprobe(struct kretprobe_instance * ri)19873f9b911SMasami Hiramatsu static nokprobe_inline struct kretprobe *get_kretprobe(struct kretprobe_instance *ri)
19973f9b911SMasami Hiramatsu {
200a1461f1fSMasami Hiramatsu (Google) 	/* rethook::data is non-changed field, so that you can access it freely. */
201a1461f1fSMasami Hiramatsu (Google) 	return (struct kretprobe *)ri->node.rethook->data;
20273f9b911SMasami Hiramatsu }
get_kretprobe_retaddr(struct kretprobe_instance * ri)20373f9b911SMasami Hiramatsu static nokprobe_inline unsigned long get_kretprobe_retaddr(struct kretprobe_instance *ri)
20473f9b911SMasami Hiramatsu {
20573f9b911SMasami Hiramatsu 	return ri->node.ret_addr;
20673f9b911SMasami Hiramatsu }
20773f9b911SMasami Hiramatsu #else
20873f9b911SMasami Hiramatsu extern void arch_prepare_kretprobe(struct kretprobe_instance *ri,
20973f9b911SMasami Hiramatsu 				   struct pt_regs *regs);
210bf094cffSMasami Hiramatsu void arch_kretprobe_fixup_return(struct pt_regs *regs,
211bf094cffSMasami Hiramatsu 				 kprobe_opcode_t *correct_ret_addr);
212bf094cffSMasami Hiramatsu 
213adf8a61aSMasami Hiramatsu void __kretprobe_trampoline(void);
21496fed8acSMasami Hiramatsu /*
21596fed8acSMasami Hiramatsu  * Since some architecture uses structured function pointer,
21696fed8acSMasami Hiramatsu  * use dereference_function_descriptor() to get real function address.
21796fed8acSMasami Hiramatsu  */
kretprobe_trampoline_addr(void)21896fed8acSMasami Hiramatsu static nokprobe_inline void *kretprobe_trampoline_addr(void)
21996fed8acSMasami Hiramatsu {
220adf8a61aSMasami Hiramatsu 	return dereference_kernel_function_descriptor(__kretprobe_trampoline);
22196fed8acSMasami Hiramatsu }
22296fed8acSMasami Hiramatsu 
22366ada2ccSMasami Hiramatsu /* If the trampoline handler called from a kprobe, use this version */
22466ada2ccSMasami Hiramatsu unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs,
22566ada2ccSMasami Hiramatsu 					     void *frame_pointer);
22666ada2ccSMasami Hiramatsu 
22766ada2ccSMasami Hiramatsu static nokprobe_inline
kretprobe_trampoline_handler(struct pt_regs * regs,void * frame_pointer)22866ada2ccSMasami Hiramatsu unsigned long kretprobe_trampoline_handler(struct pt_regs *regs,
22966ada2ccSMasami Hiramatsu 					   void *frame_pointer)
23066ada2ccSMasami Hiramatsu {
23166ada2ccSMasami Hiramatsu 	unsigned long ret;
23266ada2ccSMasami Hiramatsu 	/*
23366ada2ccSMasami Hiramatsu 	 * Set a dummy kprobe for avoiding kretprobe recursion.
23466ada2ccSMasami Hiramatsu 	 * Since kretprobe never runs in kprobe handler, no kprobe must
23566ada2ccSMasami Hiramatsu 	 * be running at this point.
23666ada2ccSMasami Hiramatsu 	 */
23766ada2ccSMasami Hiramatsu 	kprobe_busy_begin();
23896fed8acSMasami Hiramatsu 	ret = __kretprobe_trampoline_handler(regs, frame_pointer);
23966ada2ccSMasami Hiramatsu 	kprobe_busy_end();
24066ada2ccSMasami Hiramatsu 
24166ada2ccSMasami Hiramatsu 	return ret;
24266ada2ccSMasami Hiramatsu }
24366ada2ccSMasami Hiramatsu 
get_kretprobe(struct kretprobe_instance * ri)244d741bf41SPeter Zijlstra static nokprobe_inline struct kretprobe *get_kretprobe(struct kretprobe_instance *ri)
245d741bf41SPeter Zijlstra {
246d839a656SJP Kobryn 	return rcu_dereference_check(ri->rph->rp, rcu_read_lock_any_held());
247d741bf41SPeter Zijlstra }
248d741bf41SPeter Zijlstra 
get_kretprobe_retaddr(struct kretprobe_instance * ri)24973f9b911SMasami Hiramatsu static nokprobe_inline unsigned long get_kretprobe_retaddr(struct kretprobe_instance *ri)
25073f9b911SMasami Hiramatsu {
25173f9b911SMasami Hiramatsu 	return (unsigned long)ri->ret_addr;
25273f9b911SMasami Hiramatsu }
25373f9b911SMasami Hiramatsu #endif /* CONFIG_KRETPROBE_ON_RETHOOK */
25473f9b911SMasami Hiramatsu 
255223a76b2SMasami Hiramatsu #else /* !CONFIG_KRETPROBES */
arch_prepare_kretprobe(struct kretprobe * rp,struct pt_regs * regs)256dc19835dSMasami Hiramatsu static inline void arch_prepare_kretprobe(struct kretprobe *rp,
257dc19835dSMasami Hiramatsu 					struct pt_regs *regs)
258dc19835dSMasami Hiramatsu {
259dc19835dSMasami Hiramatsu }
arch_trampoline_kprobe(struct kprobe * p)260dc19835dSMasami Hiramatsu static inline int arch_trampoline_kprobe(struct kprobe *p)
261dc19835dSMasami Hiramatsu {
262dc19835dSMasami Hiramatsu 	return 0;
263dc19835dSMasami Hiramatsu }
264dc19835dSMasami Hiramatsu #endif /* CONFIG_KRETPROBES */
265dc19835dSMasami Hiramatsu 
266223a76b2SMasami Hiramatsu /* Markers of '_kprobe_blacklist' section */
267223a76b2SMasami Hiramatsu extern unsigned long __start_kprobe_blacklist[];
268223a76b2SMasami Hiramatsu extern unsigned long __stop_kprobe_blacklist[];
269223a76b2SMasami Hiramatsu 
270f438d914SMasami Hiramatsu extern struct kretprobe_blackpoint kretprobe_blacklist[];
271f438d914SMasami Hiramatsu 
2721da177e4SLinus Torvalds extern int arch_prepare_kprobe(struct kprobe *p);
2737e1048b1SRusty Lynch extern void arch_arm_kprobe(struct kprobe *p);
2747e1048b1SRusty Lynch extern void arch_disarm_kprobe(struct kprobe *p);
2756772926bSRusty Lynch extern int arch_init_kprobes(void);
276bf8d5c52SKeshavamurthy Anil S extern void kprobes_inc_nmissed_count(struct kprobe *p);
277be8f2743SMasami Hiramatsu extern bool arch_within_kprobe_blacklist(unsigned long addr);
278fb1a59faSMasami Hiramatsu extern int arch_populate_kprobe_blacklist(void);
27997c753e6SMasami Hiramatsu extern int kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset);
2801da177e4SLinus Torvalds 
281e5779e8eSAndy Lutomirski extern bool within_kprobe_blacklist(unsigned long addr);
282fb1a59faSMasami Hiramatsu extern int kprobe_add_ksym_blacklist(unsigned long entry);
283fb1a59faSMasami Hiramatsu extern int kprobe_add_area_blacklist(unsigned long start, unsigned long end);
284e5779e8eSAndy Lutomirski 
285c802d64aSHeiko Carstens struct kprobe_insn_cache {
286c802d64aSHeiko Carstens 	struct mutex mutex;
287af96397dSHeiko Carstens 	void *(*alloc)(void);	/* allocate insn page */
288af96397dSHeiko Carstens 	void (*free)(void *);	/* free insn page */
289d002b8bcSAdrian Hunter 	const char *sym;	/* symbol for insn pages */
290c802d64aSHeiko Carstens 	struct list_head pages; /* list of kprobe_insn_page */
291c802d64aSHeiko Carstens 	size_t insn_size;	/* size of instruction slot */
292c802d64aSHeiko Carstens 	int nr_garbage;
293c802d64aSHeiko Carstens };
294c802d64aSHeiko Carstens 
2955b485629SMasami Hiramatsu #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
296c802d64aSHeiko Carstens extern kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c);
297c802d64aSHeiko Carstens extern void __free_insn_slot(struct kprobe_insn_cache *c,
298c802d64aSHeiko Carstens 			     kprobe_opcode_t *slot, int dirty);
2995b485629SMasami Hiramatsu /* sleep-less address checking routine  */
3005b485629SMasami Hiramatsu extern bool __is_insn_slot_addr(struct kprobe_insn_cache *c,
3015b485629SMasami Hiramatsu 				unsigned long addr);
302c802d64aSHeiko Carstens 
303c802d64aSHeiko Carstens #define DEFINE_INSN_CACHE_OPS(__name)					\
304c802d64aSHeiko Carstens extern struct kprobe_insn_cache kprobe_##__name##_slots;		\
305c802d64aSHeiko Carstens 									\
306c802d64aSHeiko Carstens static inline kprobe_opcode_t *get_##__name##_slot(void)		\
307c802d64aSHeiko Carstens {									\
308c802d64aSHeiko Carstens 	return __get_insn_slot(&kprobe_##__name##_slots);		\
309c802d64aSHeiko Carstens }									\
310c802d64aSHeiko Carstens 									\
311c802d64aSHeiko Carstens static inline void free_##__name##_slot(kprobe_opcode_t *slot, int dirty)\
312c802d64aSHeiko Carstens {									\
313c802d64aSHeiko Carstens 	__free_insn_slot(&kprobe_##__name##_slots, slot, dirty);	\
314c802d64aSHeiko Carstens }									\
3155b485629SMasami Hiramatsu 									\
3165b485629SMasami Hiramatsu static inline bool is_kprobe_##__name##_slot(unsigned long addr)	\
3175b485629SMasami Hiramatsu {									\
3185b485629SMasami Hiramatsu 	return __is_insn_slot_addr(&kprobe_##__name##_slots, addr);	\
3195b485629SMasami Hiramatsu }
320d002b8bcSAdrian Hunter #define KPROBE_INSN_PAGE_SYM		"kprobe_insn_page"
321d002b8bcSAdrian Hunter #define KPROBE_OPTINSN_PAGE_SYM		"kprobe_optinsn_page"
322d002b8bcSAdrian Hunter int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
323d002b8bcSAdrian Hunter 			     unsigned long *value, char *type, char *sym);
324223a76b2SMasami Hiramatsu #else /* !__ARCH_WANT_KPROBES_INSN_SLOT */
3255b485629SMasami Hiramatsu #define DEFINE_INSN_CACHE_OPS(__name)					\
3265b485629SMasami Hiramatsu static inline bool is_kprobe_##__name##_slot(unsigned long addr)	\
3275b485629SMasami Hiramatsu {									\
3285b485629SMasami Hiramatsu 	return 0;							\
3295b485629SMasami Hiramatsu }
3305b485629SMasami Hiramatsu #endif
331c802d64aSHeiko Carstens 
332c802d64aSHeiko Carstens DEFINE_INSN_CACHE_OPS(insn);
333c802d64aSHeiko Carstens 
334afd66255SMasami Hiramatsu #ifdef CONFIG_OPTPROBES
335afd66255SMasami Hiramatsu /*
336afd66255SMasami Hiramatsu  * Internal structure for direct jump optimized probe
337afd66255SMasami Hiramatsu  */
338afd66255SMasami Hiramatsu struct optimized_kprobe {
339afd66255SMasami Hiramatsu 	struct kprobe kp;
340afd66255SMasami Hiramatsu 	struct list_head list;	/* list for optimizing queue */
341afd66255SMasami Hiramatsu 	struct arch_optimized_insn optinsn;
342afd66255SMasami Hiramatsu };
343afd66255SMasami Hiramatsu 
344afd66255SMasami Hiramatsu /* Architecture dependent functions for direct jump optimization */
345afd66255SMasami Hiramatsu extern int arch_prepared_optinsn(struct arch_optimized_insn *optinsn);
346afd66255SMasami Hiramatsu extern int arch_check_optimized_kprobe(struct optimized_kprobe *op);
347cbf6ab52SMasami Hiramatsu extern int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
348cbf6ab52SMasami Hiramatsu 					 struct kprobe *orig);
349afd66255SMasami Hiramatsu extern void arch_remove_optimized_kprobe(struct optimized_kprobe *op);
350cd7ebe22SMasami Hiramatsu extern void arch_optimize_kprobes(struct list_head *oplist);
351f984ba4eSMasami Hiramatsu extern void arch_unoptimize_kprobes(struct list_head *oplist,
352f984ba4eSMasami Hiramatsu 				    struct list_head *done_list);
353afd66255SMasami Hiramatsu extern void arch_unoptimize_kprobe(struct optimized_kprobe *op);
354afd66255SMasami Hiramatsu extern int arch_within_optimized_kprobe(struct optimized_kprobe *op,
355c42421e2SMasami Hiramatsu 					kprobe_opcode_t *addr);
356afd66255SMasami Hiramatsu 
357afd66255SMasami Hiramatsu extern void opt_pre_handler(struct kprobe *p, struct pt_regs *regs);
358b2be84dfSMasami Hiramatsu 
359c802d64aSHeiko Carstens DEFINE_INSN_CACHE_OPS(optinsn);
360c802d64aSHeiko Carstens 
36130e7d894SThomas Gleixner extern void wait_for_kprobe_optimizer(void);
362868a6fc0SYang Jihong bool optprobe_queued_unopt(struct optimized_kprobe *op);
363f1c97a1bSYang Jihong bool kprobe_disarmed(struct kprobe *p);
364223a76b2SMasami Hiramatsu #else /* !CONFIG_OPTPROBES */
wait_for_kprobe_optimizer(void)36530e7d894SThomas Gleixner static inline void wait_for_kprobe_optimizer(void) { }
366afd66255SMasami Hiramatsu #endif /* CONFIG_OPTPROBES */
367223a76b2SMasami Hiramatsu 
368e7dbfe34SMasami Hiramatsu #ifdef CONFIG_KPROBES_ON_FTRACE
369ae6aa16fSMasami Hiramatsu extern void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
370d19ad077SSteven Rostedt (VMware) 				  struct ftrace_ops *ops, struct ftrace_regs *fregs);
371ae6aa16fSMasami Hiramatsu extern int arch_prepare_kprobe_ftrace(struct kprobe *p);
372*1a7d0890SStephen Brennan /* Set when ftrace has been killed: kprobes on ftrace must be disabled for safety */
373*1a7d0890SStephen Brennan extern bool kprobe_ftrace_disabled __read_mostly;
374*1a7d0890SStephen Brennan extern void kprobe_ftrace_kill(void);
37502afb8d6SPunit Agrawal #else
arch_prepare_kprobe_ftrace(struct kprobe * p)37602afb8d6SPunit Agrawal static inline int arch_prepare_kprobe_ftrace(struct kprobe *p)
37702afb8d6SPunit Agrawal {
37802afb8d6SPunit Agrawal 	return -EINVAL;
37902afb8d6SPunit Agrawal }
kprobe_ftrace_kill(void)380*1a7d0890SStephen Brennan static inline void kprobe_ftrace_kill(void) {}
381223a76b2SMasami Hiramatsu #endif /* CONFIG_KPROBES_ON_FTRACE */
382ae6aa16fSMasami Hiramatsu 
383d217d545SAnanth N Mavinakayanahalli /* Get the kprobe at this addr (if any) - called with preemption disabled */
3841da177e4SLinus Torvalds struct kprobe *get_kprobe(void *addr);
3851da177e4SLinus Torvalds 
386e6584523SAnanth N Mavinakayanahalli /* kprobe_running() will just return the current_kprobe on this CPU */
kprobe_running(void)387e6584523SAnanth N Mavinakayanahalli static inline struct kprobe *kprobe_running(void)
388e6584523SAnanth N Mavinakayanahalli {
389223a76b2SMasami Hiramatsu 	return __this_cpu_read(current_kprobe);
390e6584523SAnanth N Mavinakayanahalli }
391e6584523SAnanth N Mavinakayanahalli 
reset_current_kprobe(void)392e6584523SAnanth N Mavinakayanahalli static inline void reset_current_kprobe(void)
393e6584523SAnanth N Mavinakayanahalli {
394b76834bcSChristoph Lameter 	__this_cpu_write(current_kprobe, NULL);
395e6584523SAnanth N Mavinakayanahalli }
396e6584523SAnanth N Mavinakayanahalli 
get_kprobe_ctlblk(void)397e6584523SAnanth N Mavinakayanahalli static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
398e6584523SAnanth N Mavinakayanahalli {
399bdffd893SChristoph Lameter 	return this_cpu_ptr(&kprobe_ctlblk);
400e6584523SAnanth N Mavinakayanahalli }
401e6584523SAnanth N Mavinakayanahalli 
402290e3070SNaveen N. Rao kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset);
403cc66bb91SPeter Zijlstra kprobe_opcode_t *arch_adjust_kprobe_addr(unsigned long addr, unsigned long offset, bool *on_func_entry);
404cc66bb91SPeter Zijlstra 
4051da177e4SLinus Torvalds int register_kprobe(struct kprobe *p);
4061da177e4SLinus Torvalds void unregister_kprobe(struct kprobe *p);
4079861668fSMasami Hiramatsu int register_kprobes(struct kprobe **kps, int num);
4089861668fSMasami Hiramatsu void unregister_kprobes(struct kprobe **kps, int num);
4091da177e4SLinus Torvalds 
410b94cce92SHien Nguyen int register_kretprobe(struct kretprobe *rp);
411b94cce92SHien Nguyen void unregister_kretprobe(struct kretprobe *rp);
4124a296e07SMasami Hiramatsu int register_kretprobes(struct kretprobe **rps, int num);
4134a296e07SMasami Hiramatsu void unregister_kretprobes(struct kretprobe **rps, int num);
414b94cce92SHien Nguyen 
41543994049SMasami Hiramatsu #if defined(CONFIG_KRETPROBE_ON_RETHOOK) || !defined(CONFIG_KRETPROBES)
41673f9b911SMasami Hiramatsu #define kprobe_flush_task(tk)	do {} while (0)
41773f9b911SMasami Hiramatsu #else
418b94cce92SHien Nguyen void kprobe_flush_task(struct task_struct *tk);
41973f9b911SMasami Hiramatsu #endif
4208c1c9356SAnanth N Mavinakayanahalli 
42182d083abSMasami Hiramatsu void kprobe_free_init_mem(void);
42282d083abSMasami Hiramatsu 
423de5bd88dSMasami Hiramatsu int disable_kprobe(struct kprobe *kp);
424de5bd88dSMasami Hiramatsu int enable_kprobe(struct kprobe *kp);
425de5bd88dSMasami Hiramatsu 
42624851d24SFrederic Weisbecker void dump_kprobe(struct kprobe *kp);
42724851d24SFrederic Weisbecker 
428ad3bc25aSBorislav Petkov void *alloc_insn_page(void);
429ad3bc25aSBorislav Petkov 
4307ee3e97eSChristophe Leroy void *alloc_optinsn_page(void);
4317ee3e97eSChristophe Leroy void free_optinsn_page(void *page);
4327ee3e97eSChristophe Leroy 
433d002b8bcSAdrian Hunter int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
434d002b8bcSAdrian Hunter 		       char *sym);
435d002b8bcSAdrian Hunter 
436d002b8bcSAdrian Hunter int arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
437d002b8bcSAdrian Hunter 			    char *type, char *sym);
438abc28463SArnd Bergmann 
439abc28463SArnd Bergmann int kprobe_exceptions_notify(struct notifier_block *self,
440abc28463SArnd Bergmann 			     unsigned long val, void *data);
441abc28463SArnd Bergmann 
442b1801812SIngo Molnar #else /* !CONFIG_KPROBES: */
44300d7c05aSKeshavamurthy Anil S 
kprobe_fault_handler(struct pt_regs * regs,int trapnr)444b1801812SIngo Molnar static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
445b1801812SIngo Molnar {
446b1801812SIngo Molnar 	return 0;
447b1801812SIngo Molnar }
get_kprobe(void * addr)448785656a4SAbhishek Sagar static inline struct kprobe *get_kprobe(void *addr)
449785656a4SAbhishek Sagar {
450785656a4SAbhishek Sagar 	return NULL;
451785656a4SAbhishek Sagar }
kprobe_running(void)452e6584523SAnanth N Mavinakayanahalli static inline struct kprobe *kprobe_running(void)
4531da177e4SLinus Torvalds {
454e6584523SAnanth N Mavinakayanahalli 	return NULL;
4551da177e4SLinus Torvalds }
456ab51e15dSMasami Hiramatsu #define kprobe_busy_begin()	do {} while (0)
457ab51e15dSMasami Hiramatsu #define kprobe_busy_end()	do {} while (0)
458ab51e15dSMasami Hiramatsu 
register_kprobe(struct kprobe * p)4591da177e4SLinus Torvalds static inline int register_kprobe(struct kprobe *p)
4601da177e4SLinus Torvalds {
461223a76b2SMasami Hiramatsu 	return -EOPNOTSUPP;
4621da177e4SLinus Torvalds }
register_kprobes(struct kprobe ** kps,int num)4639861668fSMasami Hiramatsu static inline int register_kprobes(struct kprobe **kps, int num)
4649861668fSMasami Hiramatsu {
465223a76b2SMasami Hiramatsu 	return -EOPNOTSUPP;
4669861668fSMasami Hiramatsu }
unregister_kprobe(struct kprobe * p)4671da177e4SLinus Torvalds static inline void unregister_kprobe(struct kprobe *p)
4681da177e4SLinus Torvalds {
4691da177e4SLinus Torvalds }
unregister_kprobes(struct kprobe ** kps,int num)4709861668fSMasami Hiramatsu static inline void unregister_kprobes(struct kprobe **kps, int num)
4719861668fSMasami Hiramatsu {
4729861668fSMasami Hiramatsu }
register_kretprobe(struct kretprobe * rp)473b94cce92SHien Nguyen static inline int register_kretprobe(struct kretprobe *rp)
474b94cce92SHien Nguyen {
475223a76b2SMasami Hiramatsu 	return -EOPNOTSUPP;
476b94cce92SHien Nguyen }
register_kretprobes(struct kretprobe ** rps,int num)4774a296e07SMasami Hiramatsu static inline int register_kretprobes(struct kretprobe **rps, int num)
4784a296e07SMasami Hiramatsu {
479223a76b2SMasami Hiramatsu 	return -EOPNOTSUPP;
4804a296e07SMasami Hiramatsu }
unregister_kretprobe(struct kretprobe * rp)481b94cce92SHien Nguyen static inline void unregister_kretprobe(struct kretprobe *rp)
482b94cce92SHien Nguyen {
483b94cce92SHien Nguyen }
unregister_kretprobes(struct kretprobe ** rps,int num)4844a296e07SMasami Hiramatsu static inline void unregister_kretprobes(struct kretprobe **rps, int num)
4854a296e07SMasami Hiramatsu {
4864a296e07SMasami Hiramatsu }
kprobe_flush_task(struct task_struct * tk)487b94cce92SHien Nguyen static inline void kprobe_flush_task(struct task_struct *tk)
488b94cce92SHien Nguyen {
489b94cce92SHien Nguyen }
kprobe_free_init_mem(void)49082d083abSMasami Hiramatsu static inline void kprobe_free_init_mem(void)
49182d083abSMasami Hiramatsu {
49282d083abSMasami Hiramatsu }
kprobe_ftrace_kill(void)493*1a7d0890SStephen Brennan static inline void kprobe_ftrace_kill(void)
494*1a7d0890SStephen Brennan {
495*1a7d0890SStephen Brennan }
disable_kprobe(struct kprobe * kp)496de5bd88dSMasami Hiramatsu static inline int disable_kprobe(struct kprobe *kp)
497de5bd88dSMasami Hiramatsu {
498223a76b2SMasami Hiramatsu 	return -EOPNOTSUPP;
499de5bd88dSMasami Hiramatsu }
enable_kprobe(struct kprobe * kp)500de5bd88dSMasami Hiramatsu static inline int enable_kprobe(struct kprobe *kp)
501de5bd88dSMasami Hiramatsu {
502223a76b2SMasami Hiramatsu 	return -EOPNOTSUPP;
503de5bd88dSMasami Hiramatsu }
504fab94075SBorislav Petkov 
within_kprobe_blacklist(unsigned long addr)505fab94075SBorislav Petkov static inline bool within_kprobe_blacklist(unsigned long addr)
506fab94075SBorislav Petkov {
507fab94075SBorislav Petkov 	return true;
508fab94075SBorislav Petkov }
kprobe_get_kallsym(unsigned int symnum,unsigned long * value,char * type,char * sym)509d002b8bcSAdrian Hunter static inline int kprobe_get_kallsym(unsigned int symnum, unsigned long *value,
510d002b8bcSAdrian Hunter 				     char *type, char *sym)
511d002b8bcSAdrian Hunter {
512d002b8bcSAdrian Hunter 	return -ERANGE;
513d002b8bcSAdrian Hunter }
514b94cce92SHien Nguyen #endif /* CONFIG_KPROBES */
515223a76b2SMasami Hiramatsu 
disable_kretprobe(struct kretprobe * rp)5168f9b1528SMasami Hiramatsu static inline int disable_kretprobe(struct kretprobe *rp)
5178f9b1528SMasami Hiramatsu {
5188f9b1528SMasami Hiramatsu 	return disable_kprobe(&rp->kp);
5198f9b1528SMasami Hiramatsu }
enable_kretprobe(struct kretprobe * rp)5208f9b1528SMasami Hiramatsu static inline int enable_kretprobe(struct kretprobe *rp)
5218f9b1528SMasami Hiramatsu {
5228f9b1528SMasami Hiramatsu 	return enable_kprobe(&rp->kp);
5238f9b1528SMasami Hiramatsu }
5248f9b1528SMasami Hiramatsu 
5255b485629SMasami Hiramatsu #ifndef CONFIG_KPROBES
is_kprobe_insn_slot(unsigned long addr)5265b485629SMasami Hiramatsu static inline bool is_kprobe_insn_slot(unsigned long addr)
5275b485629SMasami Hiramatsu {
5285b485629SMasami Hiramatsu 	return false;
5295b485629SMasami Hiramatsu }
530223a76b2SMasami Hiramatsu #endif /* !CONFIG_KPROBES */
531223a76b2SMasami Hiramatsu 
5325b485629SMasami Hiramatsu #ifndef CONFIG_OPTPROBES
is_kprobe_optinsn_slot(unsigned long addr)5335b485629SMasami Hiramatsu static inline bool is_kprobe_optinsn_slot(unsigned long addr)
5345b485629SMasami Hiramatsu {
5355b485629SMasami Hiramatsu 	return false;
5365b485629SMasami Hiramatsu }
537223a76b2SMasami Hiramatsu #endif /* !CONFIG_OPTPROBES */
5385b485629SMasami Hiramatsu 
53903bac0dfSMasami Hiramatsu #ifdef CONFIG_KRETPROBES
54073f9b911SMasami Hiramatsu #ifdef CONFIG_KRETPROBE_ON_RETHOOK
is_kretprobe_trampoline(unsigned long addr)54173f9b911SMasami Hiramatsu static nokprobe_inline bool is_kretprobe_trampoline(unsigned long addr)
54273f9b911SMasami Hiramatsu {
54373f9b911SMasami Hiramatsu 	return is_rethook_trampoline(addr);
54473f9b911SMasami Hiramatsu }
54573f9b911SMasami Hiramatsu 
54673f9b911SMasami Hiramatsu static nokprobe_inline
kretprobe_find_ret_addr(struct task_struct * tsk,void * fp,struct llist_node ** cur)54773f9b911SMasami Hiramatsu unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp,
54873f9b911SMasami Hiramatsu 				      struct llist_node **cur)
54973f9b911SMasami Hiramatsu {
55073f9b911SMasami Hiramatsu 	return rethook_find_ret_addr(tsk, (unsigned long)fp, cur);
55173f9b911SMasami Hiramatsu }
55273f9b911SMasami Hiramatsu #else
is_kretprobe_trampoline(unsigned long addr)55303bac0dfSMasami Hiramatsu static nokprobe_inline bool is_kretprobe_trampoline(unsigned long addr)
55403bac0dfSMasami Hiramatsu {
55503bac0dfSMasami Hiramatsu 	return (void *)addr == kretprobe_trampoline_addr();
55603bac0dfSMasami Hiramatsu }
55703bac0dfSMasami Hiramatsu 
55803bac0dfSMasami Hiramatsu unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp,
55903bac0dfSMasami Hiramatsu 				      struct llist_node **cur);
56073f9b911SMasami Hiramatsu #endif
56103bac0dfSMasami Hiramatsu #else
is_kretprobe_trampoline(unsigned long addr)56203bac0dfSMasami Hiramatsu static nokprobe_inline bool is_kretprobe_trampoline(unsigned long addr)
56303bac0dfSMasami Hiramatsu {
56403bac0dfSMasami Hiramatsu 	return false;
56503bac0dfSMasami Hiramatsu }
56603bac0dfSMasami Hiramatsu 
56703bac0dfSMasami Hiramatsu static nokprobe_inline
kretprobe_find_ret_addr(struct task_struct * tsk,void * fp,struct llist_node ** cur)56803bac0dfSMasami Hiramatsu unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp,
56903bac0dfSMasami Hiramatsu 				      struct llist_node **cur)
57003bac0dfSMasami Hiramatsu {
57103bac0dfSMasami Hiramatsu 	return 0;
57203bac0dfSMasami Hiramatsu }
57303bac0dfSMasami Hiramatsu #endif
57403bac0dfSMasami Hiramatsu 
575b98cca44SAnshuman Khandual /* Returns true if kprobes handled the fault */
kprobe_page_fault(struct pt_regs * regs,unsigned int trap)576b98cca44SAnshuman Khandual static nokprobe_inline bool kprobe_page_fault(struct pt_regs *regs,
577b98cca44SAnshuman Khandual 					      unsigned int trap)
578b98cca44SAnshuman Khandual {
579dfc05b55SMasami Hiramatsu 	if (!IS_ENABLED(CONFIG_KPROBES))
580b98cca44SAnshuman Khandual 		return false;
581b98cca44SAnshuman Khandual 	if (user_mode(regs))
582b98cca44SAnshuman Khandual 		return false;
583b98cca44SAnshuman Khandual 	/*
584b98cca44SAnshuman Khandual 	 * To be potentially processing a kprobe fault and to be allowed
585b98cca44SAnshuman Khandual 	 * to call kprobe_running(), we have to be non-preemptible.
586b98cca44SAnshuman Khandual 	 */
587b98cca44SAnshuman Khandual 	if (preemptible())
588b98cca44SAnshuman Khandual 		return false;
589b98cca44SAnshuman Khandual 	if (!kprobe_running())
590b98cca44SAnshuman Khandual 		return false;
591b98cca44SAnshuman Khandual 	return kprobe_fault_handler(regs, trap);
592b98cca44SAnshuman Khandual }
593b98cca44SAnshuman Khandual 
5941da177e4SLinus Torvalds #endif /* _LINUX_KPROBES_H */
595