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

source code of linux/include/linux/kprobes.h