GNU Linux-libre 5.15.131-gnu
[releases.git] / include / linux / kprobes.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _LINUX_KPROBES_H
3 #define _LINUX_KPROBES_H
4 /*
5  *  Kernel Probes (KProbes)
6  *  include/linux/kprobes.h
7  *
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.
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.
18  */
19 #include <linux/compiler.h>
20 #include <linux/linkage.h>
21 #include <linux/list.h>
22 #include <linux/notifier.h>
23 #include <linux/smp.h>
24 #include <linux/bug.h>
25 #include <linux/percpu.h>
26 #include <linux/spinlock.h>
27 #include <linux/rcupdate.h>
28 #include <linux/mutex.h>
29 #include <linux/ftrace.h>
30 #include <linux/refcount.h>
31 #include <linux/freelist.h>
32 #include <asm/kprobes.h>
33
34 #ifdef CONFIG_KPROBES
35
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
42 #else /* CONFIG_KPROBES */
43 #include <asm-generic/kprobes.h>
44 typedef int kprobe_opcode_t;
45 struct arch_specific_insn {
46         int dummy;
47 };
48 #endif /* CONFIG_KPROBES */
49
50 struct kprobe;
51 struct pt_regs;
52 struct kretprobe;
53 struct kretprobe_instance;
54 typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
55 typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *,
56                                        unsigned long flags);
57 typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,
58                                     struct pt_regs *);
59
60 struct kprobe {
61         struct hlist_node hlist;
62
63         /* list of kprobes for multi-handler support */
64         struct list_head list;
65
66         /*count the number of times this probe was temporarily disarmed */
67         unsigned long nmissed;
68
69         /* location of the probe point */
70         kprobe_opcode_t *addr;
71
72         /* Allow user to indicate symbol name of the probe point */
73         const char *symbol_name;
74
75         /* Offset into the symbol */
76         unsigned int offset;
77
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
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;
89
90         /*
91          * Indicates various status flags.
92          * Protected by kprobe_mutex after this kprobe is registered.
93          */
94         u32 flags;
95 };
96
97 /* Kprobe status flags */
98 #define KPROBE_FLAG_GONE        1 /* breakpoint has already gone */
99 #define KPROBE_FLAG_DISABLED    2 /* probe is temporarily disabled */
100 #define KPROBE_FLAG_OPTIMIZED   4 /*
101                                    * probe is really optimized.
102                                    * NOTE:
103                                    * this flag is only for optimized_kprobe.
104                                    */
105 #define KPROBE_FLAG_FTRACE      8 /* probe is using ftrace */
106
107 /* Has this kprobe gone ? */
108 static inline int kprobe_gone(struct kprobe *p)
109 {
110         return p->flags & KPROBE_FLAG_GONE;
111 }
112
113 /* Is this kprobe disabled ? */
114 static inline int kprobe_disabled(struct kprobe *p)
115 {
116         return p->flags & (KPROBE_FLAG_DISABLED | KPROBE_FLAG_GONE);
117 }
118
119 /* Is this kprobe really running optimized path ? */
120 static inline int kprobe_optimized(struct kprobe *p)
121 {
122         return p->flags & KPROBE_FLAG_OPTIMIZED;
123 }
124
125 /* Is this kprobe uses ftrace ? */
126 static inline int kprobe_ftrace(struct kprobe *p)
127 {
128         return p->flags & KPROBE_FLAG_FTRACE;
129 }
130
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  */
141 struct kretprobe_holder {
142         struct kretprobe        *rp;
143         refcount_t              ref;
144 };
145
146 struct kretprobe {
147         struct kprobe kp;
148         kretprobe_handler_t handler;
149         kretprobe_handler_t entry_handler;
150         int maxactive;
151         int nmissed;
152         size_t data_size;
153         struct freelist_head freelist;
154         struct kretprobe_holder *rph;
155 };
156
157 #define KRETPROBE_MAX_DATA_SIZE 4096
158
159 struct kretprobe_instance {
160         union {
161                 struct freelist_node freelist;
162                 struct rcu_head rcu;
163         };
164         struct llist_node llist;
165         struct kretprobe_holder *rph;
166         kprobe_opcode_t *ret_addr;
167         void *fp;
168         char data[];
169 };
170
171 struct kretprobe_blackpoint {
172         const char *name;
173         void *addr;
174 };
175
176 struct kprobe_blacklist_entry {
177         struct list_head list;
178         unsigned long start_addr;
179         unsigned long end_addr;
180 };
181
182 #ifdef CONFIG_KPROBES
183 DECLARE_PER_CPU(struct kprobe *, current_kprobe);
184 DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
185
186 /*
187  * For #ifdef avoidance:
188  */
189 static inline int kprobes_built_in(void)
190 {
191         return 1;
192 }
193
194 extern void kprobe_busy_begin(void);
195 extern void kprobe_busy_end(void);
196
197 #ifdef CONFIG_KRETPROBES
198 extern void arch_prepare_kretprobe(struct kretprobe_instance *ri,
199                                    struct pt_regs *regs);
200 extern int arch_trampoline_kprobe(struct kprobe *p);
201
202 /* If the trampoline handler called from a kprobe, use this version */
203 unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs,
204                                 void *trampoline_address,
205                                 void *frame_pointer);
206
207 static nokprobe_inline
208 unsigned long kretprobe_trampoline_handler(struct pt_regs *regs,
209                                 void *trampoline_address,
210                                 void *frame_pointer)
211 {
212         unsigned long ret;
213         /*
214          * Set a dummy kprobe for avoiding kretprobe recursion.
215          * Since kretprobe never runs in kprobe handler, no kprobe must
216          * be running at this point.
217          */
218         kprobe_busy_begin();
219         ret = __kretprobe_trampoline_handler(regs, trampoline_address, frame_pointer);
220         kprobe_busy_end();
221
222         return ret;
223 }
224
225 static nokprobe_inline struct kretprobe *get_kretprobe(struct kretprobe_instance *ri)
226 {
227         RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
228                 "Kretprobe is accessed from instance under preemptive context");
229
230         return READ_ONCE(ri->rph->rp);
231 }
232
233 #else /* CONFIG_KRETPROBES */
234 static inline void arch_prepare_kretprobe(struct kretprobe *rp,
235                                         struct pt_regs *regs)
236 {
237 }
238 static inline int arch_trampoline_kprobe(struct kprobe *p)
239 {
240         return 0;
241 }
242 #endif /* CONFIG_KRETPROBES */
243
244 extern struct kretprobe_blackpoint kretprobe_blacklist[];
245
246 #ifdef CONFIG_KPROBES_SANITY_TEST
247 extern int init_test_probes(void);
248 #else
249 static inline int init_test_probes(void)
250 {
251         return 0;
252 }
253 #endif /* CONFIG_KPROBES_SANITY_TEST */
254
255 extern int arch_prepare_kprobe(struct kprobe *p);
256 extern void arch_arm_kprobe(struct kprobe *p);
257 extern void arch_disarm_kprobe(struct kprobe *p);
258 extern int arch_init_kprobes(void);
259 extern void kprobes_inc_nmissed_count(struct kprobe *p);
260 extern bool arch_within_kprobe_blacklist(unsigned long addr);
261 extern int arch_populate_kprobe_blacklist(void);
262 extern bool arch_kprobe_on_func_entry(unsigned long offset);
263 extern int kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset);
264
265 extern bool within_kprobe_blacklist(unsigned long addr);
266 extern int kprobe_add_ksym_blacklist(unsigned long entry);
267 extern int kprobe_add_area_blacklist(unsigned long start, unsigned long end);
268
269 struct kprobe_insn_cache {
270         struct mutex mutex;
271         void *(*alloc)(void);   /* allocate insn page */
272         void (*free)(void *);   /* free insn page */
273         const char *sym;        /* symbol for insn pages */
274         struct list_head pages; /* list of kprobe_insn_page */
275         size_t insn_size;       /* size of instruction slot */
276         int nr_garbage;
277 };
278
279 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
280 extern kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c);
281 extern void __free_insn_slot(struct kprobe_insn_cache *c,
282                              kprobe_opcode_t *slot, int dirty);
283 /* sleep-less address checking routine  */
284 extern bool __is_insn_slot_addr(struct kprobe_insn_cache *c,
285                                 unsigned long addr);
286
287 #define DEFINE_INSN_CACHE_OPS(__name)                                   \
288 extern struct kprobe_insn_cache kprobe_##__name##_slots;                \
289                                                                         \
290 static inline kprobe_opcode_t *get_##__name##_slot(void)                \
291 {                                                                       \
292         return __get_insn_slot(&kprobe_##__name##_slots);               \
293 }                                                                       \
294                                                                         \
295 static inline void free_##__name##_slot(kprobe_opcode_t *slot, int dirty)\
296 {                                                                       \
297         __free_insn_slot(&kprobe_##__name##_slots, slot, dirty);        \
298 }                                                                       \
299                                                                         \
300 static inline bool is_kprobe_##__name##_slot(unsigned long addr)        \
301 {                                                                       \
302         return __is_insn_slot_addr(&kprobe_##__name##_slots, addr);     \
303 }
304 #define KPROBE_INSN_PAGE_SYM            "kprobe_insn_page"
305 #define KPROBE_OPTINSN_PAGE_SYM         "kprobe_optinsn_page"
306 int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
307                              unsigned long *value, char *type, char *sym);
308 #else /* __ARCH_WANT_KPROBES_INSN_SLOT */
309 #define DEFINE_INSN_CACHE_OPS(__name)                                   \
310 static inline bool is_kprobe_##__name##_slot(unsigned long addr)        \
311 {                                                                       \
312         return 0;                                                       \
313 }
314 #endif
315
316 DEFINE_INSN_CACHE_OPS(insn);
317
318 #ifdef CONFIG_OPTPROBES
319 /*
320  * Internal structure for direct jump optimized probe
321  */
322 struct optimized_kprobe {
323         struct kprobe kp;
324         struct list_head list;  /* list for optimizing queue */
325         struct arch_optimized_insn optinsn;
326 };
327
328 /* Architecture dependent functions for direct jump optimization */
329 extern int arch_prepared_optinsn(struct arch_optimized_insn *optinsn);
330 extern int arch_check_optimized_kprobe(struct optimized_kprobe *op);
331 extern int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
332                                          struct kprobe *orig);
333 extern void arch_remove_optimized_kprobe(struct optimized_kprobe *op);
334 extern void arch_optimize_kprobes(struct list_head *oplist);
335 extern void arch_unoptimize_kprobes(struct list_head *oplist,
336                                     struct list_head *done_list);
337 extern void arch_unoptimize_kprobe(struct optimized_kprobe *op);
338 extern int arch_within_optimized_kprobe(struct optimized_kprobe *op,
339                                         unsigned long addr);
340
341 extern void opt_pre_handler(struct kprobe *p, struct pt_regs *regs);
342
343 DEFINE_INSN_CACHE_OPS(optinsn);
344
345 #ifdef CONFIG_SYSCTL
346 extern int sysctl_kprobes_optimization;
347 extern int proc_kprobes_optimization_handler(struct ctl_table *table,
348                                              int write, void *buffer,
349                                              size_t *length, loff_t *ppos);
350 #endif
351 extern void wait_for_kprobe_optimizer(void);
352 bool optprobe_queued_unopt(struct optimized_kprobe *op);
353 bool kprobe_disarmed(struct kprobe *p);
354 #else
355 static inline void wait_for_kprobe_optimizer(void) { }
356 #endif /* CONFIG_OPTPROBES */
357 #ifdef CONFIG_KPROBES_ON_FTRACE
358 extern void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
359                                   struct ftrace_ops *ops, struct ftrace_regs *fregs);
360 extern int arch_prepare_kprobe_ftrace(struct kprobe *p);
361 #endif
362
363 int arch_check_ftrace_location(struct kprobe *p);
364
365 /* Get the kprobe at this addr (if any) - called with preemption disabled */
366 struct kprobe *get_kprobe(void *addr);
367
368 /* kprobe_running() will just return the current_kprobe on this CPU */
369 static inline struct kprobe *kprobe_running(void)
370 {
371         return (__this_cpu_read(current_kprobe));
372 }
373
374 static inline void reset_current_kprobe(void)
375 {
376         __this_cpu_write(current_kprobe, NULL);
377 }
378
379 static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
380 {
381         return this_cpu_ptr(&kprobe_ctlblk);
382 }
383
384 kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset);
385 int register_kprobe(struct kprobe *p);
386 void unregister_kprobe(struct kprobe *p);
387 int register_kprobes(struct kprobe **kps, int num);
388 void unregister_kprobes(struct kprobe **kps, int num);
389 unsigned long arch_deref_entry_point(void *);
390
391 int register_kretprobe(struct kretprobe *rp);
392 void unregister_kretprobe(struct kretprobe *rp);
393 int register_kretprobes(struct kretprobe **rps, int num);
394 void unregister_kretprobes(struct kretprobe **rps, int num);
395
396 void kprobe_flush_task(struct task_struct *tk);
397
398 void kprobe_free_init_mem(void);
399
400 int disable_kprobe(struct kprobe *kp);
401 int enable_kprobe(struct kprobe *kp);
402
403 void dump_kprobe(struct kprobe *kp);
404
405 void *alloc_insn_page(void);
406
407 void *alloc_optinsn_page(void);
408 void free_optinsn_page(void *page);
409
410 int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
411                        char *sym);
412
413 int arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
414                             char *type, char *sym);
415 #else /* !CONFIG_KPROBES: */
416
417 static inline int kprobes_built_in(void)
418 {
419         return 0;
420 }
421 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
422 {
423         return 0;
424 }
425 static inline struct kprobe *get_kprobe(void *addr)
426 {
427         return NULL;
428 }
429 static inline struct kprobe *kprobe_running(void)
430 {
431         return NULL;
432 }
433 static inline int register_kprobe(struct kprobe *p)
434 {
435         return -ENOSYS;
436 }
437 static inline int register_kprobes(struct kprobe **kps, int num)
438 {
439         return -ENOSYS;
440 }
441 static inline void unregister_kprobe(struct kprobe *p)
442 {
443 }
444 static inline void unregister_kprobes(struct kprobe **kps, int num)
445 {
446 }
447 static inline int register_kretprobe(struct kretprobe *rp)
448 {
449         return -ENOSYS;
450 }
451 static inline int register_kretprobes(struct kretprobe **rps, int num)
452 {
453         return -ENOSYS;
454 }
455 static inline void unregister_kretprobe(struct kretprobe *rp)
456 {
457 }
458 static inline void unregister_kretprobes(struct kretprobe **rps, int num)
459 {
460 }
461 static inline void kprobe_flush_task(struct task_struct *tk)
462 {
463 }
464 static inline void kprobe_free_init_mem(void)
465 {
466 }
467 static inline int disable_kprobe(struct kprobe *kp)
468 {
469         return -ENOSYS;
470 }
471 static inline int enable_kprobe(struct kprobe *kp)
472 {
473         return -ENOSYS;
474 }
475
476 static inline bool within_kprobe_blacklist(unsigned long addr)
477 {
478         return true;
479 }
480 static inline int kprobe_get_kallsym(unsigned int symnum, unsigned long *value,
481                                      char *type, char *sym)
482 {
483         return -ERANGE;
484 }
485 #endif /* CONFIG_KPROBES */
486 static inline int disable_kretprobe(struct kretprobe *rp)
487 {
488         return disable_kprobe(&rp->kp);
489 }
490 static inline int enable_kretprobe(struct kretprobe *rp)
491 {
492         return enable_kprobe(&rp->kp);
493 }
494
495 #ifndef CONFIG_KPROBES
496 static inline bool is_kprobe_insn_slot(unsigned long addr)
497 {
498         return false;
499 }
500 #endif
501 #ifndef CONFIG_OPTPROBES
502 static inline bool is_kprobe_optinsn_slot(unsigned long addr)
503 {
504         return false;
505 }
506 #endif
507
508 /* Returns true if kprobes handled the fault */
509 static nokprobe_inline bool kprobe_page_fault(struct pt_regs *regs,
510                                               unsigned int trap)
511 {
512         if (!kprobes_built_in())
513                 return false;
514         if (user_mode(regs))
515                 return false;
516         /*
517          * To be potentially processing a kprobe fault and to be allowed
518          * to call kprobe_running(), we have to be non-preemptible.
519          */
520         if (preemptible())
521                 return false;
522         if (!kprobe_running())
523                 return false;
524         return kprobe_fault_handler(regs, trap);
525 }
526
527 #endif /* _LINUX_KPROBES_H */