1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _LINUX_KPROBES_H
3 #define _LINUX_KPROBES_H
5 * Kernel Probes (KProbes)
7 * Copyright (C) IBM Corporation, 2002, 2004
9 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
10 * Probes initial implementation ( includes suggestions from
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.
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/refcount.h>
30 #include <linux/freelist.h>
31 #include <asm/kprobes.h>
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
41 #else /* !CONFIG_KPROBES */
42 #include <asm-generic/kprobes.h>
43 typedef int kprobe_opcode_t;
44 struct arch_specific_insn {
47 #endif /* CONFIG_KPROBES */
52 struct kretprobe_instance;
53 typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
54 typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *,
56 typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,
60 struct hlist_node hlist;
62 /* list of kprobes for multi-handler support */
63 struct list_head list;
65 /*count the number of times this probe was temporarily disarmed */
66 unsigned long nmissed;
68 /* location of the probe point */
69 kprobe_opcode_t *addr;
71 /* Allow user to indicate symbol name of the probe point */
72 const char *symbol_name;
74 /* Offset into the symbol */
77 /* Called before addr is executed. */
78 kprobe_pre_handler_t pre_handler;
80 /* Called after addr is executed, unless... */
81 kprobe_post_handler_t post_handler;
83 /* Saved opcode (which has been replaced with breakpoint) */
84 kprobe_opcode_t opcode;
86 /* copy of the original instruction */
87 struct arch_specific_insn ainsn;
90 * Indicates various status flags.
91 * Protected by kprobe_mutex after this kprobe is registered.
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.
102 * this flag is only for optimized_kprobe.
104 #define KPROBE_FLAG_FTRACE 8 /* probe is using ftrace */
106 /* Has this kprobe gone ? */
107 static inline bool kprobe_gone(struct kprobe *p)
109 return p->flags & KPROBE_FLAG_GONE;
112 /* Is this kprobe disabled ? */
113 static inline bool kprobe_disabled(struct kprobe *p)
115 return p->flags & (KPROBE_FLAG_DISABLED | KPROBE_FLAG_GONE);
118 /* Is this kprobe really running optimized path ? */
119 static inline bool kprobe_optimized(struct kprobe *p)
121 return p->flags & KPROBE_FLAG_OPTIMIZED;
124 /* Is this kprobe uses ftrace ? */
125 static inline bool kprobe_ftrace(struct kprobe *p)
127 return p->flags & KPROBE_FLAG_FTRACE;
131 * Function-return probe -
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.
140 struct kretprobe_holder {
141 struct kretprobe *rp;
147 kretprobe_handler_t handler;
148 kretprobe_handler_t entry_handler;
152 struct freelist_head freelist;
153 struct kretprobe_holder *rph;
156 #define KRETPROBE_MAX_DATA_SIZE 4096
158 struct kretprobe_instance {
160 struct freelist_node freelist;
163 struct llist_node llist;
164 struct kretprobe_holder *rph;
165 kprobe_opcode_t *ret_addr;
170 struct kretprobe_blackpoint {
175 struct kprobe_blacklist_entry {
176 struct list_head list;
177 unsigned long start_addr;
178 unsigned long end_addr;
181 #ifdef CONFIG_KPROBES
182 DECLARE_PER_CPU(struct kprobe *, current_kprobe);
183 DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
185 extern void kprobe_busy_begin(void);
186 extern void kprobe_busy_end(void);
188 #ifdef CONFIG_KRETPROBES
189 extern void arch_prepare_kretprobe(struct kretprobe_instance *ri,
190 struct pt_regs *regs);
191 extern int arch_trampoline_kprobe(struct kprobe *p);
193 void arch_kretprobe_fixup_return(struct pt_regs *regs,
194 kprobe_opcode_t *correct_ret_addr);
196 void __kretprobe_trampoline(void);
198 * Since some architecture uses structured function pointer,
199 * use dereference_function_descriptor() to get real function address.
201 static nokprobe_inline void *kretprobe_trampoline_addr(void)
203 return dereference_kernel_function_descriptor(__kretprobe_trampoline);
206 /* If the trampoline handler called from a kprobe, use this version */
207 unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs,
208 void *frame_pointer);
210 static nokprobe_inline
211 unsigned long kretprobe_trampoline_handler(struct pt_regs *regs,
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.
221 ret = __kretprobe_trampoline_handler(regs, frame_pointer);
227 static nokprobe_inline struct kretprobe *get_kretprobe(struct kretprobe_instance *ri)
229 RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
230 "Kretprobe is accessed from instance under preemptive context");
232 return READ_ONCE(ri->rph->rp);
235 #else /* !CONFIG_KRETPROBES */
236 static inline void arch_prepare_kretprobe(struct kretprobe *rp,
237 struct pt_regs *regs)
240 static inline int arch_trampoline_kprobe(struct kprobe *p)
244 #endif /* CONFIG_KRETPROBES */
246 /* Markers of '_kprobe_blacklist' section */
247 extern unsigned long __start_kprobe_blacklist[];
248 extern unsigned long __stop_kprobe_blacklist[];
250 extern struct kretprobe_blackpoint kretprobe_blacklist[];
252 #ifdef CONFIG_KPROBES_SANITY_TEST
253 extern int init_test_probes(void);
254 #else /* !CONFIG_KPROBES_SANITY_TEST */
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,
345 kprobe_opcode_t *addr);
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);
356 #endif /* CONFIG_SYSCTL */
357 extern void wait_for_kprobe_optimizer(void);
358 #else /* !CONFIG_OPTPROBES */
359 static inline void wait_for_kprobe_optimizer(void) { }
360 #endif /* CONFIG_OPTPROBES */
362 #ifdef CONFIG_KPROBES_ON_FTRACE
363 extern void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
364 struct ftrace_ops *ops, struct ftrace_regs *fregs);
365 extern int arch_prepare_kprobe_ftrace(struct kprobe *p);
367 static inline int arch_prepare_kprobe_ftrace(struct kprobe *p)
371 #endif /* CONFIG_KPROBES_ON_FTRACE */
373 /* Get the kprobe at this addr (if any) - called with preemption disabled */
374 struct kprobe *get_kprobe(void *addr);
376 /* kprobe_running() will just return the current_kprobe on this CPU */
377 static inline struct kprobe *kprobe_running(void)
379 return __this_cpu_read(current_kprobe);
382 static inline void reset_current_kprobe(void)
384 __this_cpu_write(current_kprobe, NULL);
387 static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
389 return this_cpu_ptr(&kprobe_ctlblk);
392 kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset);
393 int register_kprobe(struct kprobe *p);
394 void unregister_kprobe(struct kprobe *p);
395 int register_kprobes(struct kprobe **kps, int num);
396 void unregister_kprobes(struct kprobe **kps, int num);
398 int register_kretprobe(struct kretprobe *rp);
399 void unregister_kretprobe(struct kretprobe *rp);
400 int register_kretprobes(struct kretprobe **rps, int num);
401 void unregister_kretprobes(struct kretprobe **rps, int num);
403 void kprobe_flush_task(struct task_struct *tk);
405 void kprobe_free_init_mem(void);
407 int disable_kprobe(struct kprobe *kp);
408 int enable_kprobe(struct kprobe *kp);
410 void dump_kprobe(struct kprobe *kp);
412 void *alloc_insn_page(void);
414 void *alloc_optinsn_page(void);
415 void free_optinsn_page(void *page);
417 int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
420 int arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
421 char *type, char *sym);
422 #else /* !CONFIG_KPROBES: */
424 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
428 static inline struct kprobe *get_kprobe(void *addr)
432 static inline struct kprobe *kprobe_running(void)
436 static inline int register_kprobe(struct kprobe *p)
440 static inline int register_kprobes(struct kprobe **kps, int num)
444 static inline void unregister_kprobe(struct kprobe *p)
447 static inline void unregister_kprobes(struct kprobe **kps, int num)
450 static inline int register_kretprobe(struct kretprobe *rp)
454 static inline int register_kretprobes(struct kretprobe **rps, int num)
458 static inline void unregister_kretprobe(struct kretprobe *rp)
461 static inline void unregister_kretprobes(struct kretprobe **rps, int num)
464 static inline void kprobe_flush_task(struct task_struct *tk)
467 static inline void kprobe_free_init_mem(void)
470 static inline int disable_kprobe(struct kprobe *kp)
474 static inline int enable_kprobe(struct kprobe *kp)
479 static inline bool within_kprobe_blacklist(unsigned long addr)
483 static inline int kprobe_get_kallsym(unsigned int symnum, unsigned long *value,
484 char *type, char *sym)
488 #endif /* CONFIG_KPROBES */
490 static inline int disable_kretprobe(struct kretprobe *rp)
492 return disable_kprobe(&rp->kp);
494 static inline int enable_kretprobe(struct kretprobe *rp)
496 return enable_kprobe(&rp->kp);
499 #ifndef CONFIG_KPROBES
500 static inline bool is_kprobe_insn_slot(unsigned long addr)
504 #endif /* !CONFIG_KPROBES */
506 #ifndef CONFIG_OPTPROBES
507 static inline bool is_kprobe_optinsn_slot(unsigned long addr)
511 #endif /* !CONFIG_OPTPROBES */
513 #ifdef CONFIG_KRETPROBES
514 static nokprobe_inline bool is_kretprobe_trampoline(unsigned long addr)
516 return (void *)addr == kretprobe_trampoline_addr();
519 unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp,
520 struct llist_node **cur);
522 static nokprobe_inline bool is_kretprobe_trampoline(unsigned long addr)
527 static nokprobe_inline
528 unsigned long kretprobe_find_ret_addr(struct task_struct *tsk, void *fp,
529 struct llist_node **cur)
535 /* Returns true if kprobes handled the fault */
536 static nokprobe_inline bool kprobe_page_fault(struct pt_regs *regs,
539 if (!IS_ENABLED(CONFIG_KPROBES))
544 * To be potentially processing a kprobe fault and to be allowed
545 * to call kprobe_running(), we have to be non-preemptible.
549 if (!kprobe_running())
551 return kprobe_fault_handler(regs, trap);
554 #endif /* _LINUX_KPROBES_H */