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