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
10 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
11 * Probes initial implementation ( includes suggestions from
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.
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 (*kretprobe_handler_t) (struct kretprobe_instance *,
61 struct hlist_node hlist;
63 /* list of kprobes for multi-handler support */
64 struct list_head list;
66 /*count the number of times this probe was temporarily disarmed */
67 unsigned long nmissed;
69 /* location of the probe point */
70 kprobe_opcode_t *addr;
72 /* Allow user to indicate symbol name of the probe point */
73 const char *symbol_name;
75 /* Offset into the symbol */
78 /* Called before addr is executed. */
79 kprobe_pre_handler_t pre_handler;
81 /* Called after addr is executed, unless... */
82 kprobe_post_handler_t post_handler;
84 /* Saved opcode (which has been replaced with breakpoint) */
85 kprobe_opcode_t opcode;
87 /* copy of the original instruction */
88 struct arch_specific_insn ainsn;
91 * Indicates various status flags.
92 * Protected by kprobe_mutex after this kprobe is registered.
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.
103 * this flag is only for optimized_kprobe.
105 #define KPROBE_FLAG_FTRACE 8 /* probe is using ftrace */
107 /* Has this kprobe gone ? */
108 static inline int kprobe_gone(struct kprobe *p)
110 return p->flags & KPROBE_FLAG_GONE;
113 /* Is this kprobe disabled ? */
114 static inline int kprobe_disabled(struct kprobe *p)
116 return p->flags & (KPROBE_FLAG_DISABLED | KPROBE_FLAG_GONE);
119 /* Is this kprobe really running optimized path ? */
120 static inline int kprobe_optimized(struct kprobe *p)
122 return p->flags & KPROBE_FLAG_OPTIMIZED;
125 /* Is this kprobe uses ftrace ? */
126 static inline int kprobe_ftrace(struct kprobe *p)
128 return p->flags & KPROBE_FLAG_FTRACE;
132 * Function-return probe -
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.
141 struct kretprobe_holder {
142 struct kretprobe *rp;
148 kretprobe_handler_t handler;
149 kretprobe_handler_t entry_handler;
153 struct freelist_head freelist;
154 struct kretprobe_holder *rph;
157 #define KRETPROBE_MAX_DATA_SIZE 4096
159 struct kretprobe_instance {
161 struct freelist_node freelist;
164 struct llist_node llist;
165 struct kretprobe_holder *rph;
166 kprobe_opcode_t *ret_addr;
171 struct kretprobe_blackpoint {
176 struct kprobe_blacklist_entry {
177 struct list_head list;
178 unsigned long start_addr;
179 unsigned long end_addr;
182 #ifdef CONFIG_KPROBES
183 DECLARE_PER_CPU(struct kprobe *, current_kprobe);
184 DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
187 * For #ifdef avoidance:
189 static inline int kprobes_built_in(void)
194 extern void kprobe_busy_begin(void);
195 extern void kprobe_busy_end(void);
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);
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);
207 static nokprobe_inline
208 unsigned long kretprobe_trampoline_handler(struct pt_regs *regs,
209 void *trampoline_address,
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.
219 ret = __kretprobe_trampoline_handler(regs, trampoline_address, frame_pointer);
225 static nokprobe_inline struct kretprobe *get_kretprobe(struct kretprobe_instance *ri)
227 RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
228 "Kretprobe is accessed from instance under preemptive context");
230 return READ_ONCE(ri->rph->rp);
233 #else /* CONFIG_KRETPROBES */
234 static inline void arch_prepare_kretprobe(struct kretprobe *rp,
235 struct pt_regs *regs)
238 static inline int arch_trampoline_kprobe(struct kprobe *p)
242 #endif /* CONFIG_KRETPROBES */
244 extern struct kretprobe_blackpoint kretprobe_blacklist[];
246 #ifdef CONFIG_KPROBES_SANITY_TEST
247 extern int init_test_probes(void);
249 static inline int init_test_probes(void)
253 #endif /* CONFIG_KPROBES_SANITY_TEST */
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);
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);
269 struct kprobe_insn_cache {
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 */
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,
287 #define DEFINE_INSN_CACHE_OPS(__name) \
288 extern struct kprobe_insn_cache kprobe_##__name##_slots; \
290 static inline kprobe_opcode_t *get_##__name##_slot(void) \
292 return __get_insn_slot(&kprobe_##__name##_slots); \
295 static inline void free_##__name##_slot(kprobe_opcode_t *slot, int dirty)\
297 __free_insn_slot(&kprobe_##__name##_slots, slot, dirty); \
300 static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
302 return __is_insn_slot_addr(&kprobe_##__name##_slots, addr); \
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) \
316 DEFINE_INSN_CACHE_OPS(insn);
318 #ifdef CONFIG_OPTPROBES
320 * Internal structure for direct jump optimized probe
322 struct optimized_kprobe {
324 struct list_head list; /* list for optimizing queue */
325 struct arch_optimized_insn optinsn;
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,
341 extern void opt_pre_handler(struct kprobe *p, struct pt_regs *regs);
343 DEFINE_INSN_CACHE_OPS(optinsn);
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);
351 extern void wait_for_kprobe_optimizer(void);
352 bool optprobe_queued_unopt(struct optimized_kprobe *op);
353 bool kprobe_disarmed(struct kprobe *p);
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);
363 int arch_check_ftrace_location(struct kprobe *p);
365 /* Get the kprobe at this addr (if any) - called with preemption disabled */
366 struct kprobe *get_kprobe(void *addr);
368 /* kprobe_running() will just return the current_kprobe on this CPU */
369 static inline struct kprobe *kprobe_running(void)
371 return (__this_cpu_read(current_kprobe));
374 static inline void reset_current_kprobe(void)
376 __this_cpu_write(current_kprobe, NULL);
379 static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
381 return this_cpu_ptr(&kprobe_ctlblk);
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 *);
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);
396 void kprobe_flush_task(struct task_struct *tk);
398 void kprobe_free_init_mem(void);
400 int disable_kprobe(struct kprobe *kp);
401 int enable_kprobe(struct kprobe *kp);
403 void dump_kprobe(struct kprobe *kp);
405 void *alloc_insn_page(void);
407 void *alloc_optinsn_page(void);
408 void free_optinsn_page(void *page);
410 int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
413 int arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
414 char *type, char *sym);
415 #else /* !CONFIG_KPROBES: */
417 static inline int kprobes_built_in(void)
421 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
425 static inline struct kprobe *get_kprobe(void *addr)
429 static inline struct kprobe *kprobe_running(void)
433 static inline int register_kprobe(struct kprobe *p)
437 static inline int register_kprobes(struct kprobe **kps, int num)
441 static inline void unregister_kprobe(struct kprobe *p)
444 static inline void unregister_kprobes(struct kprobe **kps, int num)
447 static inline int register_kretprobe(struct kretprobe *rp)
451 static inline int register_kretprobes(struct kretprobe **rps, int num)
455 static inline void unregister_kretprobe(struct kretprobe *rp)
458 static inline void unregister_kretprobes(struct kretprobe **rps, int num)
461 static inline void kprobe_flush_task(struct task_struct *tk)
464 static inline void kprobe_free_init_mem(void)
467 static inline int disable_kprobe(struct kprobe *kp)
471 static inline int enable_kprobe(struct kprobe *kp)
476 static inline bool within_kprobe_blacklist(unsigned long addr)
480 static inline int kprobe_get_kallsym(unsigned int symnum, unsigned long *value,
481 char *type, char *sym)
485 #endif /* CONFIG_KPROBES */
486 static inline int disable_kretprobe(struct kretprobe *rp)
488 return disable_kprobe(&rp->kp);
490 static inline int enable_kretprobe(struct kretprobe *rp)
492 return enable_kprobe(&rp->kp);
495 #ifndef CONFIG_KPROBES
496 static inline bool is_kprobe_insn_slot(unsigned long addr)
501 #ifndef CONFIG_OPTPROBES
502 static inline bool is_kprobe_optinsn_slot(unsigned long addr)
508 /* Returns true if kprobes handled the fault */
509 static nokprobe_inline bool kprobe_page_fault(struct pt_regs *regs,
512 if (!kprobes_built_in())
517 * To be potentially processing a kprobe fault and to be allowed
518 * to call kprobe_running(), we have to be non-preemptible.
522 if (!kprobe_running())
524 return kprobe_fault_handler(regs, trap);
527 #endif /* _LINUX_KPROBES_H */