]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - include/linux/kprobes.h
Merge tag 'pm-5.14-rc8' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm
[mirror_ubuntu-jammy-kernel.git] / include / linux / kprobes.h
CommitLineData
1a59d1b8 1/* SPDX-License-Identifier: GPL-2.0-or-later */
1da177e4
LT
2#ifndef _LINUX_KPROBES_H
3#define _LINUX_KPROBES_H
4/*
5 * Kernel Probes (KProbes)
6 * include/linux/kprobes.h
7 *
1da177e4
LT
8 * Copyright (C) IBM Corporation, 2002, 2004
9 *
10 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
11 * Probes initial implementation ( includes suggestions from
12 * Rusty Russell).
13 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
14 * interface to access function arguments.
b94cce92
HN
15 * 2005-May Hien Nguyen <hien@us.ibm.com> and Jim Keniston
16 * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
17 * <prasanna@in.ibm.com> added function-return probes.
1da177e4 18 */
7d134b2c 19#include <linux/compiler.h>
36dcd67a 20#include <linux/linkage.h>
1da177e4
LT
21#include <linux/list.h>
22#include <linux/notifier.h>
23#include <linux/smp.h>
187f1882 24#include <linux/bug.h>
e6584523 25#include <linux/percpu.h>
3516a460
AM
26#include <linux/spinlock.h>
27#include <linux/rcupdate.h>
7a7d1cf9 28#include <linux/mutex.h>
ae6aa16f 29#include <linux/ftrace.h>
d741bf41 30#include <linux/refcount.h>
6e426e0f 31#include <linux/freelist.h>
7d134b2c 32#include <asm/kprobes.h>
b94cce92 33
00d7c05a 34#ifdef CONFIG_KPROBES
1da177e4 35
ea32c65c
PP
36/* kprobe_status settings */
37#define KPROBE_HIT_ACTIVE 0x00000001
38#define KPROBE_HIT_SS 0x00000002
39#define KPROBE_REENTER 0x00000004
40#define KPROBE_HIT_SSDONE 0x00000008
41
dc19835d 42#else /* CONFIG_KPROBES */
7d134b2c 43#include <asm-generic/kprobes.h>
dc19835d
MH
44typedef int kprobe_opcode_t;
45struct arch_specific_insn {
46 int dummy;
47};
dc19835d 48#endif /* CONFIG_KPROBES */
d0aaff97 49
1da177e4
LT
50struct kprobe;
51struct pt_regs;
b94cce92
HN
52struct kretprobe;
53struct kretprobe_instance;
1da177e4 54typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
1da177e4
LT
55typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *,
56 unsigned long flags);
b94cce92
HN
57typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,
58 struct pt_regs *);
59
1da177e4
LT
60struct kprobe {
61 struct hlist_node hlist;
62
64f562c6
AM
63 /* list of kprobes for multi-handler support */
64 struct list_head list;
65
ea32c65c
PP
66 /*count the number of times this probe was temporarily disarmed */
67 unsigned long nmissed;
68
1da177e4
LT
69 /* location of the probe point */
70 kprobe_opcode_t *addr;
71
3a872d89 72 /* Allow user to indicate symbol name of the probe point */
9b3af29b 73 const char *symbol_name;
3a872d89
AM
74
75 /* Offset into the symbol */
76 unsigned int offset;
77
1da177e4
LT
78 /* Called before addr is executed. */
79 kprobe_pre_handler_t pre_handler;
80
81 /* Called after addr is executed, unless... */
82 kprobe_post_handler_t post_handler;
83
1da177e4
LT
84 /* Saved opcode (which has been replaced with breakpoint) */
85 kprobe_opcode_t opcode;
86
87 /* copy of the original instruction */
88 struct arch_specific_insn ainsn;
e8386a0c 89
de5bd88d
MH
90 /*
91 * Indicates various status flags.
92 * Protected by kprobe_mutex after this kprobe is registered.
93 */
e8386a0c 94 u32 flags;
1da177e4
LT
95};
96
e8386a0c
MH
97/* Kprobe status flags */
98#define KPROBE_FLAG_GONE 1 /* breakpoint has already gone */
de5bd88d 99#define KPROBE_FLAG_DISABLED 2 /* probe is temporarily disabled */
afd66255
MH
100#define KPROBE_FLAG_OPTIMIZED 4 /*
101 * probe is really optimized.
102 * NOTE:
103 * this flag is only for optimized_kprobe.
104 */
ae6aa16f 105#define KPROBE_FLAG_FTRACE 8 /* probe is using ftrace */
e8386a0c 106
de5bd88d 107/* Has this kprobe gone ? */
e8386a0c
MH
108static inline int kprobe_gone(struct kprobe *p)
109{
110 return p->flags & KPROBE_FLAG_GONE;
111}
112
de5bd88d
MH
113/* Is this kprobe disabled ? */
114static inline int kprobe_disabled(struct kprobe *p)
115{
116 return p->flags & (KPROBE_FLAG_DISABLED | KPROBE_FLAG_GONE);
117}
afd66255
MH
118
119/* Is this kprobe really running optimized path ? */
120static inline int kprobe_optimized(struct kprobe *p)
121{
122 return p->flags & KPROBE_FLAG_OPTIMIZED;
123}
ae6aa16f
MH
124
125/* Is this kprobe uses ftrace ? */
126static inline int kprobe_ftrace(struct kprobe *p)
127{
128 return p->flags & KPROBE_FLAG_FTRACE;
129}
130
b94cce92
HN
131/*
132 * Function-return probe -
133 * Note:
134 * User needs to provide a handler function, and initialize maxactive.
135 * maxactive - The maximum number of instances of the probed function that
136 * can be active concurrently.
137 * nmissed - tracks the number of times the probed function's return was
138 * ignored, due to maxactive being too low.
139 *
140 */
d741bf41
PZ
141struct kretprobe_holder {
142 struct kretprobe *rp;
143 refcount_t ref;
144};
145
b94cce92
HN
146struct kretprobe {
147 struct kprobe kp;
148 kretprobe_handler_t handler;
f47cd9b5 149 kretprobe_handler_t entry_handler;
b94cce92
HN
150 int maxactive;
151 int nmissed;
f47cd9b5 152 size_t data_size;
6e426e0f 153 struct freelist_head freelist;
d741bf41 154 struct kretprobe_holder *rph;
b94cce92
HN
155};
156
157struct kretprobe_instance {
b3388178 158 union {
6e426e0f 159 struct freelist_node freelist;
b3388178
MH
160 struct rcu_head rcu;
161 };
6e426e0f 162 struct llist_node llist;
d741bf41 163 struct kretprobe_holder *rph;
802eae7c 164 kprobe_opcode_t *ret_addr;
3ff9c075 165 void *fp;
67a862a9 166 char data[];
b94cce92
HN
167};
168
f438d914
MH
169struct kretprobe_blackpoint {
170 const char *name;
171 void *addr;
172};
3d8d996e 173
376e2424
MH
174struct kprobe_blacklist_entry {
175 struct list_head list;
3d8d996e 176 unsigned long start_addr;
376e2424 177 unsigned long end_addr;
3d8d996e
SD
178};
179
dc19835d
MH
180#ifdef CONFIG_KPROBES
181DECLARE_PER_CPU(struct kprobe *, current_kprobe);
182DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
183
b1801812
IM
184/*
185 * For #ifdef avoidance:
186 */
187static inline int kprobes_built_in(void)
188{
189 return 1;
190}
191
66ada2cc
MH
192extern void kprobe_busy_begin(void);
193extern void kprobe_busy_end(void);
194
dc19835d
MH
195#ifdef CONFIG_KRETPROBES
196extern void arch_prepare_kretprobe(struct kretprobe_instance *ri,
197 struct pt_regs *regs);
198extern int arch_trampoline_kprobe(struct kprobe *p);
66ada2cc
MH
199
200/* If the trampoline handler called from a kprobe, use this version */
201unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs,
202 void *trampoline_address,
203 void *frame_pointer);
204
205static nokprobe_inline
206unsigned long kretprobe_trampoline_handler(struct pt_regs *regs,
207 void *trampoline_address,
208 void *frame_pointer)
209{
210 unsigned long ret;
211 /*
212 * Set a dummy kprobe for avoiding kretprobe recursion.
213 * Since kretprobe never runs in kprobe handler, no kprobe must
214 * be running at this point.
215 */
216 kprobe_busy_begin();
217 ret = __kretprobe_trampoline_handler(regs, trampoline_address, frame_pointer);
218 kprobe_busy_end();
219
220 return ret;
221}
222
d741bf41
PZ
223static nokprobe_inline struct kretprobe *get_kretprobe(struct kretprobe_instance *ri)
224{
225 RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
226 "Kretprobe is accessed from instance under preemptive context");
227
228 return READ_ONCE(ri->rph->rp);
229}
230
dc19835d
MH
231#else /* CONFIG_KRETPROBES */
232static inline void arch_prepare_kretprobe(struct kretprobe *rp,
233 struct pt_regs *regs)
234{
235}
236static inline int arch_trampoline_kprobe(struct kprobe *p)
237{
238 return 0;
239}
240#endif /* CONFIG_KRETPROBES */
241
f438d914
MH
242extern struct kretprobe_blackpoint kretprobe_blacklist[];
243
8c1c9356
AM
244#ifdef CONFIG_KPROBES_SANITY_TEST
245extern int init_test_probes(void);
246#else
247static inline int init_test_probes(void)
248{
249 return 0;
250}
251#endif /* CONFIG_KPROBES_SANITY_TEST */
252
1da177e4 253extern int arch_prepare_kprobe(struct kprobe *p);
7e1048b1
RL
254extern void arch_arm_kprobe(struct kprobe *p);
255extern void arch_disarm_kprobe(struct kprobe *p);
6772926b 256extern int arch_init_kprobes(void);
bf8d5c52 257extern void kprobes_inc_nmissed_count(struct kprobe *p);
be8f2743 258extern bool arch_within_kprobe_blacklist(unsigned long addr);
fb1a59fa 259extern int arch_populate_kprobe_blacklist(void);
659b957f 260extern bool arch_kprobe_on_func_entry(unsigned long offset);
97c753e6 261extern int kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset);
1da177e4 262
e5779e8e 263extern bool within_kprobe_blacklist(unsigned long addr);
fb1a59fa
MH
264extern int kprobe_add_ksym_blacklist(unsigned long entry);
265extern int kprobe_add_area_blacklist(unsigned long start, unsigned long end);
e5779e8e 266
c802d64a
HC
267struct kprobe_insn_cache {
268 struct mutex mutex;
af96397d
HC
269 void *(*alloc)(void); /* allocate insn page */
270 void (*free)(void *); /* free insn page */
d002b8bc 271 const char *sym; /* symbol for insn pages */
c802d64a
HC
272 struct list_head pages; /* list of kprobe_insn_page */
273 size_t insn_size; /* size of instruction slot */
274 int nr_garbage;
275};
276
5b485629 277#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
c802d64a
HC
278extern kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c);
279extern void __free_insn_slot(struct kprobe_insn_cache *c,
280 kprobe_opcode_t *slot, int dirty);
5b485629
MH
281/* sleep-less address checking routine */
282extern bool __is_insn_slot_addr(struct kprobe_insn_cache *c,
283 unsigned long addr);
c802d64a
HC
284
285#define DEFINE_INSN_CACHE_OPS(__name) \
286extern struct kprobe_insn_cache kprobe_##__name##_slots; \
287 \
288static inline kprobe_opcode_t *get_##__name##_slot(void) \
289{ \
290 return __get_insn_slot(&kprobe_##__name##_slots); \
291} \
292 \
293static inline void free_##__name##_slot(kprobe_opcode_t *slot, int dirty)\
294{ \
295 __free_insn_slot(&kprobe_##__name##_slots, slot, dirty); \
296} \
5b485629
MH
297 \
298static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
299{ \
300 return __is_insn_slot_addr(&kprobe_##__name##_slots, addr); \
301}
d002b8bc
AH
302#define KPROBE_INSN_PAGE_SYM "kprobe_insn_page"
303#define KPROBE_OPTINSN_PAGE_SYM "kprobe_optinsn_page"
304int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
305 unsigned long *value, char *type, char *sym);
5b485629
MH
306#else /* __ARCH_WANT_KPROBES_INSN_SLOT */
307#define DEFINE_INSN_CACHE_OPS(__name) \
308static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
309{ \
310 return 0; \
311}
312#endif
c802d64a
HC
313
314DEFINE_INSN_CACHE_OPS(insn);
315
afd66255
MH
316#ifdef CONFIG_OPTPROBES
317/*
318 * Internal structure for direct jump optimized probe
319 */
320struct optimized_kprobe {
321 struct kprobe kp;
322 struct list_head list; /* list for optimizing queue */
323 struct arch_optimized_insn optinsn;
324};
325
326/* Architecture dependent functions for direct jump optimization */
327extern int arch_prepared_optinsn(struct arch_optimized_insn *optinsn);
328extern int arch_check_optimized_kprobe(struct optimized_kprobe *op);
cbf6ab52
MH
329extern int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
330 struct kprobe *orig);
afd66255 331extern void arch_remove_optimized_kprobe(struct optimized_kprobe *op);
cd7ebe22 332extern void arch_optimize_kprobes(struct list_head *oplist);
f984ba4e
MH
333extern void arch_unoptimize_kprobes(struct list_head *oplist,
334 struct list_head *done_list);
afd66255 335extern void arch_unoptimize_kprobe(struct optimized_kprobe *op);
afd66255
MH
336extern int arch_within_optimized_kprobe(struct optimized_kprobe *op,
337 unsigned long addr);
338
339extern void opt_pre_handler(struct kprobe *p, struct pt_regs *regs);
b2be84df 340
c802d64a
HC
341DEFINE_INSN_CACHE_OPS(optinsn);
342
b2be84df
MH
343#ifdef CONFIG_SYSCTL
344extern int sysctl_kprobes_optimization;
345extern int proc_kprobes_optimization_handler(struct ctl_table *table,
32927393 346 int write, void *buffer,
b2be84df
MH
347 size_t *length, loff_t *ppos);
348#endif
30e7d894
TG
349extern void wait_for_kprobe_optimizer(void);
350#else
351static inline void wait_for_kprobe_optimizer(void) { }
afd66255 352#endif /* CONFIG_OPTPROBES */
e7dbfe34 353#ifdef CONFIG_KPROBES_ON_FTRACE
ae6aa16f 354extern void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
d19ad077 355 struct ftrace_ops *ops, struct ftrace_regs *fregs);
ae6aa16f
MH
356extern int arch_prepare_kprobe_ftrace(struct kprobe *p);
357#endif
358
f7f242ff 359int arch_check_ftrace_location(struct kprobe *p);
afd66255 360
d217d545 361/* Get the kprobe at this addr (if any) - called with preemption disabled */
1da177e4
LT
362struct kprobe *get_kprobe(void *addr);
363
e6584523
AM
364/* kprobe_running() will just return the current_kprobe on this CPU */
365static inline struct kprobe *kprobe_running(void)
366{
b76834bc 367 return (__this_cpu_read(current_kprobe));
e6584523
AM
368}
369
370static inline void reset_current_kprobe(void)
371{
b76834bc 372 __this_cpu_write(current_kprobe, NULL);
e6584523
AM
373}
374
375static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
376{
bdffd893 377 return this_cpu_ptr(&kprobe_ctlblk);
e6584523
AM
378}
379
290e3070 380kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset);
1da177e4
LT
381int register_kprobe(struct kprobe *p);
382void unregister_kprobe(struct kprobe *p);
9861668f
MH
383int register_kprobes(struct kprobe **kps, int num);
384void unregister_kprobes(struct kprobe **kps, int num);
3d7e3382 385unsigned long arch_deref_entry_point(void *);
1da177e4 386
b94cce92
HN
387int register_kretprobe(struct kretprobe *rp);
388void unregister_kretprobe(struct kretprobe *rp);
4a296e07
MH
389int register_kretprobes(struct kretprobe **rps, int num);
390void unregister_kretprobes(struct kretprobe **rps, int num);
b94cce92 391
b94cce92 392void kprobe_flush_task(struct task_struct *tk);
8c1c9356 393
82d083ab
MH
394void kprobe_free_init_mem(void);
395
de5bd88d
MH
396int disable_kprobe(struct kprobe *kp);
397int enable_kprobe(struct kprobe *kp);
398
24851d24
FW
399void dump_kprobe(struct kprobe *kp);
400
ad3bc25a 401void *alloc_insn_page(void);
ad3bc25a 402
7ee3e97e
CL
403void *alloc_optinsn_page(void);
404void free_optinsn_page(void *page);
405
d002b8bc
AH
406int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
407 char *sym);
408
409int arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
410 char *type, char *sym);
b1801812 411#else /* !CONFIG_KPROBES: */
00d7c05a 412
b1801812
IM
413static inline int kprobes_built_in(void)
414{
415 return 0;
416}
417static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
418{
419 return 0;
420}
785656a4
AS
421static inline struct kprobe *get_kprobe(void *addr)
422{
423 return NULL;
424}
e6584523 425static inline struct kprobe *kprobe_running(void)
1da177e4 426{
e6584523 427 return NULL;
1da177e4
LT
428}
429static inline int register_kprobe(struct kprobe *p)
430{
431 return -ENOSYS;
432}
9861668f
MH
433static inline int register_kprobes(struct kprobe **kps, int num)
434{
435 return -ENOSYS;
436}
1da177e4
LT
437static inline void unregister_kprobe(struct kprobe *p)
438{
439}
9861668f
MH
440static inline void unregister_kprobes(struct kprobe **kps, int num)
441{
442}
b94cce92
HN
443static inline int register_kretprobe(struct kretprobe *rp)
444{
445 return -ENOSYS;
446}
4a296e07
MH
447static inline int register_kretprobes(struct kretprobe **rps, int num)
448{
449 return -ENOSYS;
450}
b94cce92
HN
451static inline void unregister_kretprobe(struct kretprobe *rp)
452{
453}
4a296e07
MH
454static inline void unregister_kretprobes(struct kretprobe **rps, int num)
455{
456}
b94cce92
HN
457static inline void kprobe_flush_task(struct task_struct *tk)
458{
459}
82d083ab
MH
460static inline void kprobe_free_init_mem(void)
461{
462}
de5bd88d
MH
463static inline int disable_kprobe(struct kprobe *kp)
464{
465 return -ENOSYS;
466}
467static inline int enable_kprobe(struct kprobe *kp)
468{
469 return -ENOSYS;
470}
fab94075
BP
471
472static inline bool within_kprobe_blacklist(unsigned long addr)
473{
474 return true;
475}
d002b8bc
AH
476static inline int kprobe_get_kallsym(unsigned int symnum, unsigned long *value,
477 char *type, char *sym)
478{
479 return -ERANGE;
480}
b1801812 481#endif /* CONFIG_KPROBES */
8f9b1528
MH
482static inline int disable_kretprobe(struct kretprobe *rp)
483{
484 return disable_kprobe(&rp->kp);
485}
486static inline int enable_kretprobe(struct kretprobe *rp)
487{
488 return enable_kprobe(&rp->kp);
489}
8f9b1528 490
5b485629
MH
491#ifndef CONFIG_KPROBES
492static inline bool is_kprobe_insn_slot(unsigned long addr)
493{
494 return false;
495}
496#endif
497#ifndef CONFIG_OPTPROBES
498static inline bool is_kprobe_optinsn_slot(unsigned long addr)
499{
500 return false;
501}
502#endif
503
b98cca44
AK
504/* Returns true if kprobes handled the fault */
505static nokprobe_inline bool kprobe_page_fault(struct pt_regs *regs,
506 unsigned int trap)
507{
508 if (!kprobes_built_in())
509 return false;
510 if (user_mode(regs))
511 return false;
512 /*
513 * To be potentially processing a kprobe fault and to be allowed
514 * to call kprobe_running(), we have to be non-preemptible.
515 */
516 if (preemptible())
517 return false;
518 if (!kprobe_running())
519 return false;
520 return kprobe_fault_handler(regs, trap);
521}
522
b1801812 523#endif /* _LINUX_KPROBES_H */