Merge tag 'sound-5.16-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai...
[linux-block.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)
1da177e4 6 *
1da177e4
LT
7 * Copyright (C) IBM Corporation, 2002, 2004
8 *
9 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
10 * Probes initial implementation ( includes suggestions from
11 * Rusty Russell).
12 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
13 * interface to access function arguments.
b94cce92
HN
14 * 2005-May Hien Nguyen <hien@us.ibm.com> and Jim Keniston
15 * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
16 * <prasanna@in.ibm.com> added function-return probes.
1da177e4 17 */
7d134b2c 18#include <linux/compiler.h>
36dcd67a 19#include <linux/linkage.h>
1da177e4
LT
20#include <linux/list.h>
21#include <linux/notifier.h>
22#include <linux/smp.h>
187f1882 23#include <linux/bug.h>
e6584523 24#include <linux/percpu.h>
3516a460
AM
25#include <linux/spinlock.h>
26#include <linux/rcupdate.h>
7a7d1cf9 27#include <linux/mutex.h>
ae6aa16f 28#include <linux/ftrace.h>
d741bf41 29#include <linux/refcount.h>
6e426e0f 30#include <linux/freelist.h>
7d134b2c 31#include <asm/kprobes.h>
b94cce92 32
00d7c05a 33#ifdef CONFIG_KPROBES
1da177e4 34
ea32c65c
PP
35/* kprobe_status settings */
36#define KPROBE_HIT_ACTIVE 0x00000001
37#define KPROBE_HIT_SS 0x00000002
38#define KPROBE_REENTER 0x00000004
39#define KPROBE_HIT_SSDONE 0x00000008
40
223a76b2 41#else /* !CONFIG_KPROBES */
7d134b2c 42#include <asm-generic/kprobes.h>
dc19835d
MH
43typedef int kprobe_opcode_t;
44struct arch_specific_insn {
45 int dummy;
46};
dc19835d 47#endif /* CONFIG_KPROBES */
d0aaff97 48
1da177e4
LT
49struct kprobe;
50struct pt_regs;
b94cce92
HN
51struct kretprobe;
52struct kretprobe_instance;
1da177e4 53typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
1da177e4
LT
54typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *,
55 unsigned long flags);
b94cce92
HN
56typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,
57 struct pt_regs *);
58
1da177e4
LT
59struct kprobe {
60 struct hlist_node hlist;
61
64f562c6
AM
62 /* list of kprobes for multi-handler support */
63 struct list_head list;
64
ea32c65c
PP
65 /*count the number of times this probe was temporarily disarmed */
66 unsigned long nmissed;
67
1da177e4
LT
68 /* location of the probe point */
69 kprobe_opcode_t *addr;
70
3a872d89 71 /* Allow user to indicate symbol name of the probe point */
9b3af29b 72 const char *symbol_name;
3a872d89
AM
73
74 /* Offset into the symbol */
75 unsigned int offset;
76
1da177e4
LT
77 /* Called before addr is executed. */
78 kprobe_pre_handler_t pre_handler;
79
80 /* Called after addr is executed, unless... */
81 kprobe_post_handler_t post_handler;
82
1da177e4
LT
83 /* Saved opcode (which has been replaced with breakpoint) */
84 kprobe_opcode_t opcode;
85
86 /* copy of the original instruction */
87 struct arch_specific_insn ainsn;
e8386a0c 88
de5bd88d
MH
89 /*
90 * Indicates various status flags.
91 * Protected by kprobe_mutex after this kprobe is registered.
92 */
e8386a0c 93 u32 flags;
1da177e4
LT
94};
95
e8386a0c
MH
96/* Kprobe status flags */
97#define KPROBE_FLAG_GONE 1 /* breakpoint has already gone */
de5bd88d 98#define KPROBE_FLAG_DISABLED 2 /* probe is temporarily disabled */
afd66255
MH
99#define KPROBE_FLAG_OPTIMIZED 4 /*
100 * probe is really optimized.
101 * NOTE:
102 * this flag is only for optimized_kprobe.
103 */
ae6aa16f 104#define KPROBE_FLAG_FTRACE 8 /* probe is using ftrace */
e8386a0c 105
de5bd88d 106/* Has this kprobe gone ? */
29e8077a 107static inline bool kprobe_gone(struct kprobe *p)
e8386a0c
MH
108{
109 return p->flags & KPROBE_FLAG_GONE;
110}
111
de5bd88d 112/* Is this kprobe disabled ? */
29e8077a 113static inline bool kprobe_disabled(struct kprobe *p)
de5bd88d
MH
114{
115 return p->flags & (KPROBE_FLAG_DISABLED | KPROBE_FLAG_GONE);
116}
afd66255
MH
117
118/* Is this kprobe really running optimized path ? */
29e8077a 119static inline bool kprobe_optimized(struct kprobe *p)
afd66255
MH
120{
121 return p->flags & KPROBE_FLAG_OPTIMIZED;
122}
ae6aa16f
MH
123
124/* Is this kprobe uses ftrace ? */
29e8077a 125static inline bool kprobe_ftrace(struct kprobe *p)
ae6aa16f
MH
126{
127 return p->flags & KPROBE_FLAG_FTRACE;
128}
129
b94cce92
HN
130/*
131 * Function-return probe -
132 * Note:
133 * User needs to provide a handler function, and initialize maxactive.
134 * maxactive - The maximum number of instances of the probed function that
135 * can be active concurrently.
136 * nmissed - tracks the number of times the probed function's return was
137 * ignored, due to maxactive being too low.
138 *
139 */
d741bf41
PZ
140struct kretprobe_holder {
141 struct kretprobe *rp;
142 refcount_t ref;
143};
144
b94cce92
HN
145struct kretprobe {
146 struct kprobe kp;
147 kretprobe_handler_t handler;
f47cd9b5 148 kretprobe_handler_t entry_handler;
b94cce92
HN
149 int maxactive;
150 int nmissed;
f47cd9b5 151 size_t data_size;
6e426e0f 152 struct freelist_head freelist;
d741bf41 153 struct kretprobe_holder *rph;
b94cce92
HN
154};
155
6bbfa441
MH
156#define KRETPROBE_MAX_DATA_SIZE 4096
157
b94cce92 158struct kretprobe_instance {
b3388178 159 union {
6e426e0f 160 struct freelist_node freelist;
b3388178
MH
161 struct rcu_head rcu;
162 };
6e426e0f 163 struct llist_node llist;
d741bf41 164 struct kretprobe_holder *rph;
802eae7c 165 kprobe_opcode_t *ret_addr;
3ff9c075 166 void *fp;
67a862a9 167 char data[];
b94cce92
HN
168};
169
f438d914
MH
170struct kretprobe_blackpoint {
171 const char *name;
172 void *addr;
173};
3d8d996e 174
376e2424
MH
175struct kprobe_blacklist_entry {
176 struct list_head list;
3d8d996e 177 unsigned long start_addr;
376e2424 178 unsigned long end_addr;
3d8d996e
SD
179};
180
dc19835d
MH
181#ifdef CONFIG_KPROBES
182DECLARE_PER_CPU(struct kprobe *, current_kprobe);
183DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
184
66ada2cc
MH
185extern void kprobe_busy_begin(void);
186extern void kprobe_busy_end(void);
187
dc19835d
MH
188#ifdef CONFIG_KRETPROBES
189extern void arch_prepare_kretprobe(struct kretprobe_instance *ri,
190 struct pt_regs *regs);
191extern int arch_trampoline_kprobe(struct kprobe *p);
66ada2cc 192
bf094cff
MH
193void arch_kretprobe_fixup_return(struct pt_regs *regs,
194 kprobe_opcode_t *correct_ret_addr);
195
adf8a61a 196void __kretprobe_trampoline(void);
96fed8ac
MH
197/*
198 * Since some architecture uses structured function pointer,
199 * use dereference_function_descriptor() to get real function address.
200 */
201static nokprobe_inline void *kretprobe_trampoline_addr(void)
202{
adf8a61a 203 return dereference_kernel_function_descriptor(__kretprobe_trampoline);
96fed8ac
MH
204}
205
66ada2cc
MH
206/* If the trampoline handler called from a kprobe, use this version */
207unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs,
96fed8ac 208 void *frame_pointer);
66ada2cc
MH
209
210static nokprobe_inline
211unsigned long kretprobe_trampoline_handler(struct pt_regs *regs,
96fed8ac 212 void *frame_pointer)
66ada2cc
MH
213{
214 unsigned long ret;
215 /*
216 * Set a dummy kprobe for avoiding kretprobe recursion.
217 * Since kretprobe never runs in kprobe handler, no kprobe must
218 * be running at this point.
219 */
220 kprobe_busy_begin();
96fed8ac 221 ret = __kretprobe_trampoline_handler(regs, frame_pointer);
66ada2cc
MH
222 kprobe_busy_end();
223
224 return ret;
225}
226
d741bf41
PZ
227static nokprobe_inline struct kretprobe *get_kretprobe(struct kretprobe_instance *ri)
228{
229 RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
230 "Kretprobe is accessed from instance under preemptive context");
231
232 return READ_ONCE(ri->rph->rp);
233}
234
223a76b2 235#else /* !CONFIG_KRETPROBES */
dc19835d
MH
236static inline void arch_prepare_kretprobe(struct kretprobe *rp,
237 struct pt_regs *regs)
238{
239}
240static inline int arch_trampoline_kprobe(struct kprobe *p)
241{
242 return 0;
243}
244#endif /* CONFIG_KRETPROBES */
245
223a76b2
MH
246/* Markers of '_kprobe_blacklist' section */
247extern unsigned long __start_kprobe_blacklist[];
248extern unsigned long __stop_kprobe_blacklist[];
249
f438d914
MH
250extern struct kretprobe_blackpoint kretprobe_blacklist[];
251
8c1c9356
AM
252#ifdef CONFIG_KPROBES_SANITY_TEST
253extern int init_test_probes(void);
223a76b2 254#else /* !CONFIG_KPROBES_SANITY_TEST */
8c1c9356
AM
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);
223a76b2 314#else /* !__ARCH_WANT_KPROBES_INSN_SLOT */
5b485629
MH
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 344extern int arch_within_optimized_kprobe(struct optimized_kprobe *op,
c42421e2 345 kprobe_opcode_t *addr);
afd66255
MH
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 355 size_t *length, loff_t *ppos);
223a76b2 356#endif /* CONFIG_SYSCTL */
30e7d894 357extern void wait_for_kprobe_optimizer(void);
223a76b2 358#else /* !CONFIG_OPTPROBES */
30e7d894 359static inline void wait_for_kprobe_optimizer(void) { }
afd66255 360#endif /* CONFIG_OPTPROBES */
223a76b2 361
e7dbfe34 362#ifdef CONFIG_KPROBES_ON_FTRACE
ae6aa16f 363extern void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
d19ad077 364 struct ftrace_ops *ops, struct ftrace_regs *fregs);
ae6aa16f 365extern int arch_prepare_kprobe_ftrace(struct kprobe *p);
02afb8d6
PA
366#else
367static inline int arch_prepare_kprobe_ftrace(struct kprobe *p)
368{
369 return -EINVAL;
370}
223a76b2 371#endif /* CONFIG_KPROBES_ON_FTRACE */
ae6aa16f 372
d217d545 373/* Get the kprobe at this addr (if any) - called with preemption disabled */
1da177e4
LT
374struct kprobe *get_kprobe(void *addr);
375
e6584523
AM
376/* kprobe_running() will just return the current_kprobe on this CPU */
377static inline struct kprobe *kprobe_running(void)
378{
223a76b2 379 return __this_cpu_read(current_kprobe);
e6584523
AM
380}
381
382static inline void reset_current_kprobe(void)
383{
b76834bc 384 __this_cpu_write(current_kprobe, NULL);
e6584523
AM
385}
386
387static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
388{
bdffd893 389 return this_cpu_ptr(&kprobe_ctlblk);
e6584523
AM
390}
391
290e3070 392kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset);
1da177e4
LT
393int register_kprobe(struct kprobe *p);
394void unregister_kprobe(struct kprobe *p);
9861668f
MH
395int register_kprobes(struct kprobe **kps, int num);
396void unregister_kprobes(struct kprobe **kps, int num);
1da177e4 397
b94cce92
HN
398int register_kretprobe(struct kretprobe *rp);
399void unregister_kretprobe(struct kretprobe *rp);
4a296e07
MH
400int register_kretprobes(struct kretprobe **rps, int num);
401void unregister_kretprobes(struct kretprobe **rps, int num);
b94cce92 402
b94cce92 403void kprobe_flush_task(struct task_struct *tk);
8c1c9356 404
82d083ab
MH
405void kprobe_free_init_mem(void);
406
de5bd88d
MH
407int disable_kprobe(struct kprobe *kp);
408int enable_kprobe(struct kprobe *kp);
409
24851d24
FW
410void dump_kprobe(struct kprobe *kp);
411
ad3bc25a 412void *alloc_insn_page(void);
ad3bc25a 413
7ee3e97e
CL
414void *alloc_optinsn_page(void);
415void free_optinsn_page(void *page);
416
d002b8bc
AH
417int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
418 char *sym);
419
420int arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
421 char *type, char *sym);
b1801812 422#else /* !CONFIG_KPROBES: */
00d7c05a 423
b1801812
IM
424static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
425{
426 return 0;
427}
785656a4
AS
428static inline struct kprobe *get_kprobe(void *addr)
429{
430 return NULL;
431}
e6584523 432static inline struct kprobe *kprobe_running(void)
1da177e4 433{
e6584523 434 return NULL;
1da177e4
LT
435}
436static inline int register_kprobe(struct kprobe *p)
437{
223a76b2 438 return -EOPNOTSUPP;
1da177e4 439}
9861668f
MH
440static inline int register_kprobes(struct kprobe **kps, int num)
441{
223a76b2 442 return -EOPNOTSUPP;
9861668f 443}
1da177e4
LT
444static inline void unregister_kprobe(struct kprobe *p)
445{
446}
9861668f
MH
447static inline void unregister_kprobes(struct kprobe **kps, int num)
448{
449}
b94cce92
HN
450static inline int register_kretprobe(struct kretprobe *rp)
451{
223a76b2 452 return -EOPNOTSUPP;
b94cce92 453}
4a296e07
MH
454static inline int register_kretprobes(struct kretprobe **rps, int num)
455{
223a76b2 456 return -EOPNOTSUPP;
4a296e07 457}
b94cce92
HN
458static inline void unregister_kretprobe(struct kretprobe *rp)
459{
460}
4a296e07
MH
461static inline void unregister_kretprobes(struct kretprobe **rps, int num)
462{
463}
b94cce92
HN
464static inline void kprobe_flush_task(struct task_struct *tk)
465{
466}
82d083ab
MH
467static inline void kprobe_free_init_mem(void)
468{
469}
de5bd88d
MH
470static inline int disable_kprobe(struct kprobe *kp)
471{
223a76b2 472 return -EOPNOTSUPP;
de5bd88d
MH
473}
474static inline int enable_kprobe(struct kprobe *kp)
475{
223a76b2 476 return -EOPNOTSUPP;
de5bd88d 477}
fab94075
BP
478
479static inline bool within_kprobe_blacklist(unsigned long addr)
480{
481 return true;
482}
d002b8bc
AH
483static inline int kprobe_get_kallsym(unsigned int symnum, unsigned long *value,
484 char *type, char *sym)
485{
486 return -ERANGE;
487}
b1801812 488#endif /* CONFIG_KPROBES */
223a76b2 489
8f9b1528
MH
490static inline int disable_kretprobe(struct kretprobe *rp)
491{
492 return disable_kprobe(&rp->kp);
493}
494static inline int enable_kretprobe(struct kretprobe *rp)
495{
496 return enable_kprobe(&rp->kp);
497}
8f9b1528 498
5b485629
MH
499#ifndef CONFIG_KPROBES
500static inline bool is_kprobe_insn_slot(unsigned long addr)
501{
502 return false;
503}
223a76b2
MH
504#endif /* !CONFIG_KPROBES */
505
5b485629
MH
506#ifndef CONFIG_OPTPROBES
507static inline bool is_kprobe_optinsn_slot(unsigned long addr)
508{
509 return false;
510}
223a76b2 511#endif /* !CONFIG_OPTPROBES */
5b485629 512
03bac0df
MH
513#ifdef CONFIG_KRETPROBES
514static nokprobe_inline bool is_kretprobe_trampoline(unsigned long addr)
515{
516 return (void *)addr == kretprobe_trampoline_addr();
517}
518
519unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp,
520 struct llist_node **cur);
521#else
522static nokprobe_inline bool is_kretprobe_trampoline(unsigned long addr)
523{
524 return false;
525}
526
527static nokprobe_inline
528unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp,
529 struct llist_node **cur)
530{
531 return 0;
532}
533#endif
534
b98cca44
AK
535/* Returns true if kprobes handled the fault */
536static nokprobe_inline bool kprobe_page_fault(struct pt_regs *regs,
537 unsigned int trap)
538{
dfc05b55 539 if (!IS_ENABLED(CONFIG_KPROBES))
b98cca44
AK
540 return false;
541 if (user_mode(regs))
542 return false;
543 /*
544 * To be potentially processing a kprobe fault and to be allowed
545 * to call kprobe_running(), we have to be non-preemptible.
546 */
547 if (preemptible())
548 return false;
549 if (!kprobe_running())
550 return false;
551 return kprobe_fault_handler(regs, trap);
552}
553
b1801812 554#endif /* _LINUX_KPROBES_H */