1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _LINUX_KPROBES_H
3 #define _LINUX_KPROBES_H
5 * Kernel Probes (KProbes)
6 * include/linux/kprobes.h
8 * Copyright (C) IBM Corporation, 2002, 2004
11 * Probes initial implementation ( includes suggestions from
14 * interface to access function arguments.
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>
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
42 #else /* CONFIG_KPROBES */
43 #include <asm-generic/kprobes.h>
44 typedef int kprobe_opcode_t;
45 struct arch_specific_insn {
48 #endif /* CONFIG_KPROBES */
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 *,
57 typedef int (*kprobe_fault_handler_t) (struct kprobe *, struct pt_regs *,
59 typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,
63 struct hlist_node hlist;
65 /* list of kprobes for multi-handler support */
66 struct list_head list;
68 /*count the number of times this probe was temporarily disarmed */
69 unsigned long nmissed;
71 /* location of the probe point */
72 kprobe_opcode_t *addr;
74 /* Allow user to indicate symbol name of the probe point */
75 const char *symbol_name;
77 /* Offset into the symbol */
80 /* Called before addr is executed. */
81 kprobe_pre_handler_t pre_handler;
83 /* Called after addr is executed, unless... */
84 kprobe_post_handler_t post_handler;
87 * ... called if executing addr causes a fault (eg. page fault).
88 * Return 1 if it handled fault, otherwise kernel will see it.
90 kprobe_fault_handler_t fault_handler;
92 /* Saved opcode (which has been replaced with breakpoint) */
93 kprobe_opcode_t opcode;
95 /* copy of the original instruction */
96 struct arch_specific_insn ainsn;
99 * Indicates various status flags.
100 * Protected by kprobe_mutex after this kprobe is registered.
105 /* Kprobe status flags */
106 #define KPROBE_FLAG_GONE 1 /* breakpoint has already gone */
107 #define KPROBE_FLAG_DISABLED 2 /* probe is temporarily disabled */
108 #define KPROBE_FLAG_OPTIMIZED 4 /*
109 * probe is really optimized.
111 * this flag is only for optimized_kprobe.
113 #define KPROBE_FLAG_FTRACE 8 /* probe is using ftrace */
115 /* Has this kprobe gone ? */
116 static inline int kprobe_gone(struct kprobe *p)
118 return p->flags & KPROBE_FLAG_GONE;
121 /* Is this kprobe disabled ? */
122 static inline int kprobe_disabled(struct kprobe *p)
124 return p->flags & (KPROBE_FLAG_DISABLED | KPROBE_FLAG_GONE);
127 /* Is this kprobe really running optimized path ? */
128 static inline int kprobe_optimized(struct kprobe *p)
130 return p->flags & KPROBE_FLAG_OPTIMIZED;
133 /* Is this kprobe uses ftrace ? */
134 static inline int kprobe_ftrace(struct kprobe *p)
136 return p->flags & KPROBE_FLAG_FTRACE;
140 * Function-return probe -
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.
149 struct kretprobe_holder {
150 struct kretprobe *rp;
156 kretprobe_handler_t handler;
157 kretprobe_handler_t entry_handler;
161 struct freelist_head freelist;
162 struct kretprobe_holder *rph;
165 struct kretprobe_instance {
167 struct freelist_node freelist;
170 struct llist_node llist;
171 struct kretprobe_holder *rph;
172 kprobe_opcode_t *ret_addr;
177 struct kretprobe_blackpoint {
182 struct kprobe_blacklist_entry {
183 struct list_head list;
184 unsigned long start_addr;
185 unsigned long end_addr;
188 #ifdef CONFIG_KPROBES
189 DECLARE_PER_CPU(struct kprobe *, current_kprobe);
190 DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
193 * For #ifdef avoidance:
195 static inline int kprobes_built_in(void)
200 extern void kprobe_busy_begin(void);
201 extern void kprobe_busy_end(void);
203 #ifdef CONFIG_KRETPROBES
204 extern void arch_prepare_kretprobe(struct kretprobe_instance *ri,
205 struct pt_regs *regs);
206 extern int arch_trampoline_kprobe(struct kprobe *p);
208 /* If the trampoline handler called from a kprobe, use this version */
209 unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs,
210 void *trampoline_address,
211 void *frame_pointer);
213 static nokprobe_inline
214 unsigned long kretprobe_trampoline_handler(struct pt_regs *regs,
215 void *trampoline_address,
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.
225 ret = __kretprobe_trampoline_handler(regs, trampoline_address, frame_pointer);
231 static nokprobe_inline struct kretprobe *get_kretprobe(struct kretprobe_instance *ri)
233 RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
234 "Kretprobe is accessed from instance under preemptive context");
236 return READ_ONCE(ri->rph->rp);
239 #else /* CONFIG_KRETPROBES */
240 static inline void arch_prepare_kretprobe(struct kretprobe *rp,
241 struct pt_regs *regs)
244 static inline int arch_trampoline_kprobe(struct kprobe *p)
248 #endif /* CONFIG_KRETPROBES */
250 extern struct kretprobe_blackpoint kretprobe_blacklist[];
252 #ifdef CONFIG_KPROBES_SANITY_TEST
253 extern int init_test_probes(void);
255 static inline int init_test_probes(void)
259 #endif /* CONFIG_KPROBES_SANITY_TEST */
261 extern int arch_prepare_kprobe(struct kprobe *p);
262 extern void arch_arm_kprobe(struct kprobe *p);
263 extern void arch_disarm_kprobe(struct kprobe *p);
264 extern int arch_init_kprobes(void);
265 extern void kprobes_inc_nmissed_count(struct kprobe *p);
266 extern bool arch_within_kprobe_blacklist(unsigned long addr);
267 extern int arch_populate_kprobe_blacklist(void);
268 extern bool arch_kprobe_on_func_entry(unsigned long offset);
269 extern int kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset);
271 extern bool within_kprobe_blacklist(unsigned long addr);
272 extern int kprobe_add_ksym_blacklist(unsigned long entry);
273 extern int kprobe_add_area_blacklist(unsigned long start, unsigned long end);
275 struct kprobe_insn_cache {
277 void *(*alloc)(void); /* allocate insn page */
278 void (*free)(void *); /* free insn page */
279 const char *sym; /* symbol for insn pages */
280 struct list_head pages; /* list of kprobe_insn_page */
281 size_t insn_size; /* size of instruction slot */
285 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
286 extern kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c);
287 extern void __free_insn_slot(struct kprobe_insn_cache *c,
288 kprobe_opcode_t *slot, int dirty);
289 /* sleep-less address checking routine */
290 extern bool __is_insn_slot_addr(struct kprobe_insn_cache *c,
293 #define DEFINE_INSN_CACHE_OPS(__name) \
294 extern struct kprobe_insn_cache kprobe_##__name##_slots; \
296 static inline kprobe_opcode_t *get_##__name##_slot(void) \
298 return __get_insn_slot(&kprobe_##__name##_slots); \
301 static inline void free_##__name##_slot(kprobe_opcode_t *slot, int dirty)\
303 __free_insn_slot(&kprobe_##__name##_slots, slot, dirty); \
306 static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
308 return __is_insn_slot_addr(&kprobe_##__name##_slots, addr); \
310 #define KPROBE_INSN_PAGE_SYM "kprobe_insn_page"
311 #define KPROBE_OPTINSN_PAGE_SYM "kprobe_optinsn_page"
312 int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
313 unsigned long *value, char *type, char *sym);
314 #else /* __ARCH_WANT_KPROBES_INSN_SLOT */
315 #define DEFINE_INSN_CACHE_OPS(__name) \
316 static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
322 DEFINE_INSN_CACHE_OPS(insn);
324 #ifdef CONFIG_OPTPROBES
326 * Internal structure for direct jump optimized probe
328 struct optimized_kprobe {
330 struct list_head list; /* list for optimizing queue */
331 struct arch_optimized_insn optinsn;
334 /* Architecture dependent functions for direct jump optimization */
335 extern int arch_prepared_optinsn(struct arch_optimized_insn *optinsn);
336 extern int arch_check_optimized_kprobe(struct optimized_kprobe *op);
337 extern int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
338 struct kprobe *orig);
339 extern void arch_remove_optimized_kprobe(struct optimized_kprobe *op);
340 extern void arch_optimize_kprobes(struct list_head *oplist);
341 extern void arch_unoptimize_kprobes(struct list_head *oplist,
342 struct list_head *done_list);
343 extern void arch_unoptimize_kprobe(struct optimized_kprobe *op);
344 extern int arch_within_optimized_kprobe(struct optimized_kprobe *op,
347 extern void opt_pre_handler(struct kprobe *p, struct pt_regs *regs);
349 DEFINE_INSN_CACHE_OPS(optinsn);
352 extern int sysctl_kprobes_optimization;
353 extern int proc_kprobes_optimization_handler(struct ctl_table *table,
354 int write, void *buffer,
355 size_t *length, loff_t *ppos);
357 extern void wait_for_kprobe_optimizer(void);
359 static inline void wait_for_kprobe_optimizer(void) { }
360 #endif /* CONFIG_OPTPROBES */
361 #ifdef CONFIG_KPROBES_ON_FTRACE
362 extern void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
363 struct ftrace_ops *ops, struct ftrace_regs *fregs);
364 extern int arch_prepare_kprobe_ftrace(struct kprobe *p);
367 int arch_check_ftrace_location(struct kprobe *p);
369 /* Get the kprobe at this addr (if any) - called with preemption disabled */
370 struct kprobe *get_kprobe(void *addr);
372 /* kprobe_running() will just return the current_kprobe on this CPU */
373 static inline struct kprobe *kprobe_running(void)
375 return (__this_cpu_read(current_kprobe));
378 static inline void reset_current_kprobe(void)
380 __this_cpu_write(current_kprobe, NULL);
383 static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
385 return this_cpu_ptr(&kprobe_ctlblk);
388 kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset);
389 int register_kprobe(struct kprobe *p);
390 void unregister_kprobe(struct kprobe *p);
391 int register_kprobes(struct kprobe **kps, int num);
392 void unregister_kprobes(struct kprobe **kps, int num);
393 unsigned long arch_deref_entry_point(void *);
395 int register_kretprobe(struct kretprobe *rp);
396 void unregister_kretprobe(struct kretprobe *rp);
397 int register_kretprobes(struct kretprobe **rps, int num);
398 void unregister_kretprobes(struct kretprobe **rps, int num);
400 void kprobe_flush_task(struct task_struct *tk);
402 void kprobe_free_init_mem(void);
404 int disable_kprobe(struct kprobe *kp);
405 int enable_kprobe(struct kprobe *kp);
407 void dump_kprobe(struct kprobe *kp);
409 void *alloc_insn_page(void);
410 void free_insn_page(void *page);
412 int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
415 int arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
416 char *type, char *sym);
417 #else /* !CONFIG_KPROBES: */
419 static inline int kprobes_built_in(void)
423 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
427 static inline struct kprobe *get_kprobe(void *addr)
431 static inline struct kprobe *kprobe_running(void)
435 static inline int register_kprobe(struct kprobe *p)
439 static inline int register_kprobes(struct kprobe **kps, int num)
443 static inline void unregister_kprobe(struct kprobe *p)
446 static inline void unregister_kprobes(struct kprobe **kps, int num)
449 static inline int register_kretprobe(struct kretprobe *rp)
453 static inline int register_kretprobes(struct kretprobe **rps, int num)
457 static inline void unregister_kretprobe(struct kretprobe *rp)
460 static inline void unregister_kretprobes(struct kretprobe **rps, int num)
463 static inline void kprobe_flush_task(struct task_struct *tk)
466 static inline void kprobe_free_init_mem(void)
469 static inline int disable_kprobe(struct kprobe *kp)
473 static inline int enable_kprobe(struct kprobe *kp)
478 static inline bool within_kprobe_blacklist(unsigned long addr)
482 static inline int kprobe_get_kallsym(unsigned int symnum, unsigned long *value,
483 char *type, char *sym)
487 #endif /* CONFIG_KPROBES */
488 static inline int disable_kretprobe(struct kretprobe *rp)
490 return disable_kprobe(&rp->kp);
492 static inline int enable_kretprobe(struct kretprobe *rp)
494 return enable_kprobe(&rp->kp);
497 #ifndef CONFIG_KPROBES
498 static inline bool is_kprobe_insn_slot(unsigned long addr)
503 #ifndef CONFIG_OPTPROBES
504 static inline bool is_kprobe_optinsn_slot(unsigned long addr)
510 /* Returns true if kprobes handled the fault */
511 static nokprobe_inline bool kprobe_page_fault(struct pt_regs *regs,
514 if (!kprobes_built_in())
519 * To be potentially processing a kprobe fault and to be allowed
520 * to call kprobe_running(), we have to be non-preemptible.
524 if (!kprobe_running())
526 return kprobe_fault_handler(regs, trap);
529 #endif /* _LINUX_KPROBES_H */