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 <asm/kprobes.h>
34 /* kprobe_status settings */
35 #define KPROBE_HIT_ACTIVE 0x00000001
36 #define KPROBE_HIT_SS 0x00000002
37 #define KPROBE_REENTER 0x00000004
38 #define KPROBE_HIT_SSDONE 0x00000008
40 #else /* CONFIG_KPROBES */
41 #include <asm-generic/kprobes.h>
42 typedef int kprobe_opcode_t;
43 struct arch_specific_insn {
46 #endif /* CONFIG_KPROBES */
51 struct kretprobe_instance;
52 typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
53 typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *,
55 typedef int (*kprobe_fault_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;
85 * ... called if executing addr causes a fault (eg. page fault).
86 * Return 1 if it handled fault, otherwise kernel will see it.
88 kprobe_fault_handler_t fault_handler;
90 /* Saved opcode (which has been replaced with breakpoint) */
91 kprobe_opcode_t opcode;
93 /* copy of the original instruction */
94 struct arch_specific_insn ainsn;
97 * Indicates various status flags.
98 * Protected by kprobe_mutex after this kprobe is registered.
103 /* Kprobe status flags */
104 #define KPROBE_FLAG_GONE 1 /* breakpoint has already gone */
105 #define KPROBE_FLAG_DISABLED 2 /* probe is temporarily disabled */
106 #define KPROBE_FLAG_OPTIMIZED 4 /*
107 * probe is really optimized.
109 * this flag is only for optimized_kprobe.
111 #define KPROBE_FLAG_FTRACE 8 /* probe is using ftrace */
113 /* Has this kprobe gone ? */
114 static inline int kprobe_gone(struct kprobe *p)
116 return p->flags & KPROBE_FLAG_GONE;
119 /* Is this kprobe disabled ? */
120 static inline int kprobe_disabled(struct kprobe *p)
122 return p->flags & (KPROBE_FLAG_DISABLED | KPROBE_FLAG_GONE);
125 /* Is this kprobe really running optimized path ? */
126 static inline int kprobe_optimized(struct kprobe *p)
128 return p->flags & KPROBE_FLAG_OPTIMIZED;
131 /* Is this kprobe uses ftrace ? */
132 static inline int kprobe_ftrace(struct kprobe *p)
134 return p->flags & KPROBE_FLAG_FTRACE;
138 * Function-return probe -
140 * User needs to provide a handler function, and initialize maxactive.
141 * maxactive - The maximum number of instances of the probed function that
142 * can be active concurrently.
143 * nmissed - tracks the number of times the probed function's return was
144 * ignored, due to maxactive being too low.
149 kretprobe_handler_t handler;
150 kretprobe_handler_t entry_handler;
154 struct hlist_head free_instances;
158 struct kretprobe_instance {
160 struct hlist_node hlist;
163 struct kretprobe *rp;
164 kprobe_opcode_t *ret_addr;
165 struct task_struct *task;
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);
186 * For #ifdef avoidance:
188 static inline int kprobes_built_in(void)
193 extern void kprobe_busy_begin(void);
194 extern void kprobe_busy_end(void);
196 #ifdef CONFIG_KRETPROBES
197 extern void arch_prepare_kretprobe(struct kretprobe_instance *ri,
198 struct pt_regs *regs);
199 extern int arch_trampoline_kprobe(struct kprobe *p);
201 /* If the trampoline handler called from a kprobe, use this version */
202 unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs,
203 void *trampoline_address,
204 void *frame_pointer);
206 static nokprobe_inline
207 unsigned long kretprobe_trampoline_handler(struct pt_regs *regs,
208 void *trampoline_address,
213 * Set a dummy kprobe for avoiding kretprobe recursion.
214 * Since kretprobe never runs in kprobe handler, no kprobe must
215 * be running at this point.
218 ret = __kretprobe_trampoline_handler(regs, trampoline_address, frame_pointer);
224 #else /* CONFIG_KRETPROBES */
225 static inline void arch_prepare_kretprobe(struct kretprobe *rp,
226 struct pt_regs *regs)
229 static inline int arch_trampoline_kprobe(struct kprobe *p)
233 #endif /* CONFIG_KRETPROBES */
235 extern struct kretprobe_blackpoint kretprobe_blacklist[];
237 #ifdef CONFIG_KPROBES_SANITY_TEST
238 extern int init_test_probes(void);
240 static inline int init_test_probes(void)
244 #endif /* CONFIG_KPROBES_SANITY_TEST */
246 extern int arch_prepare_kprobe(struct kprobe *p);
247 extern void arch_arm_kprobe(struct kprobe *p);
248 extern void arch_disarm_kprobe(struct kprobe *p);
249 extern int arch_init_kprobes(void);
250 extern void kprobes_inc_nmissed_count(struct kprobe *p);
251 extern bool arch_within_kprobe_blacklist(unsigned long addr);
252 extern int arch_populate_kprobe_blacklist(void);
253 extern bool arch_kprobe_on_func_entry(unsigned long offset);
254 extern bool kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset);
256 extern bool within_kprobe_blacklist(unsigned long addr);
257 extern int kprobe_add_ksym_blacklist(unsigned long entry);
258 extern int kprobe_add_area_blacklist(unsigned long start, unsigned long end);
260 struct kprobe_insn_cache {
262 void *(*alloc)(void); /* allocate insn page */
263 void (*free)(void *); /* free insn page */
264 const char *sym; /* symbol for insn pages */
265 struct list_head pages; /* list of kprobe_insn_page */
266 size_t insn_size; /* size of instruction slot */
270 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
271 extern kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c);
272 extern void __free_insn_slot(struct kprobe_insn_cache *c,
273 kprobe_opcode_t *slot, int dirty);
274 /* sleep-less address checking routine */
275 extern bool __is_insn_slot_addr(struct kprobe_insn_cache *c,
278 #define DEFINE_INSN_CACHE_OPS(__name) \
279 extern struct kprobe_insn_cache kprobe_##__name##_slots; \
281 static inline kprobe_opcode_t *get_##__name##_slot(void) \
283 return __get_insn_slot(&kprobe_##__name##_slots); \
286 static inline void free_##__name##_slot(kprobe_opcode_t *slot, int dirty)\
288 __free_insn_slot(&kprobe_##__name##_slots, slot, dirty); \
291 static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
293 return __is_insn_slot_addr(&kprobe_##__name##_slots, addr); \
295 #define KPROBE_INSN_PAGE_SYM "kprobe_insn_page"
296 #define KPROBE_OPTINSN_PAGE_SYM "kprobe_optinsn_page"
297 int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
298 unsigned long *value, char *type, char *sym);
299 #else /* __ARCH_WANT_KPROBES_INSN_SLOT */
300 #define DEFINE_INSN_CACHE_OPS(__name) \
301 static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
307 DEFINE_INSN_CACHE_OPS(insn);
309 #ifdef CONFIG_OPTPROBES
311 * Internal structure for direct jump optimized probe
313 struct optimized_kprobe {
315 struct list_head list; /* list for optimizing queue */
316 struct arch_optimized_insn optinsn;
319 /* Architecture dependent functions for direct jump optimization */
320 extern int arch_prepared_optinsn(struct arch_optimized_insn *optinsn);
321 extern int arch_check_optimized_kprobe(struct optimized_kprobe *op);
322 extern int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
323 struct kprobe *orig);
324 extern void arch_remove_optimized_kprobe(struct optimized_kprobe *op);
325 extern void arch_optimize_kprobes(struct list_head *oplist);
326 extern void arch_unoptimize_kprobes(struct list_head *oplist,
327 struct list_head *done_list);
328 extern void arch_unoptimize_kprobe(struct optimized_kprobe *op);
329 extern int arch_within_optimized_kprobe(struct optimized_kprobe *op,
332 extern void opt_pre_handler(struct kprobe *p, struct pt_regs *regs);
334 DEFINE_INSN_CACHE_OPS(optinsn);
337 extern int sysctl_kprobes_optimization;
338 extern int proc_kprobes_optimization_handler(struct ctl_table *table,
339 int write, void *buffer,
340 size_t *length, loff_t *ppos);
342 extern void wait_for_kprobe_optimizer(void);
344 static inline void wait_for_kprobe_optimizer(void) { }
345 #endif /* CONFIG_OPTPROBES */
346 #ifdef CONFIG_KPROBES_ON_FTRACE
347 extern void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
348 struct ftrace_ops *ops, struct pt_regs *regs);
349 extern int arch_prepare_kprobe_ftrace(struct kprobe *p);
352 int arch_check_ftrace_location(struct kprobe *p);
354 /* Get the kprobe at this addr (if any) - called with preemption disabled */
355 struct kprobe *get_kprobe(void *addr);
357 /* kprobe_running() will just return the current_kprobe on this CPU */
358 static inline struct kprobe *kprobe_running(void)
360 return (__this_cpu_read(current_kprobe));
363 static inline void reset_current_kprobe(void)
365 __this_cpu_write(current_kprobe, NULL);
368 static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
370 return this_cpu_ptr(&kprobe_ctlblk);
373 kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset);
374 int register_kprobe(struct kprobe *p);
375 void unregister_kprobe(struct kprobe *p);
376 int register_kprobes(struct kprobe **kps, int num);
377 void unregister_kprobes(struct kprobe **kps, int num);
378 unsigned long arch_deref_entry_point(void *);
380 int register_kretprobe(struct kretprobe *rp);
381 void unregister_kretprobe(struct kretprobe *rp);
382 int register_kretprobes(struct kretprobe **rps, int num);
383 void unregister_kretprobes(struct kretprobe **rps, int num);
385 void kprobe_flush_task(struct task_struct *tk);
387 void kprobe_free_init_mem(void);
389 int disable_kprobe(struct kprobe *kp);
390 int enable_kprobe(struct kprobe *kp);
392 void dump_kprobe(struct kprobe *kp);
394 void *alloc_insn_page(void);
395 void free_insn_page(void *page);
397 int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
400 int arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
401 char *type, char *sym);
402 #else /* !CONFIG_KPROBES: */
404 static inline int kprobes_built_in(void)
408 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
412 static inline struct kprobe *get_kprobe(void *addr)
416 static inline struct kprobe *kprobe_running(void)
420 static inline int register_kprobe(struct kprobe *p)
424 static inline int register_kprobes(struct kprobe **kps, int num)
428 static inline void unregister_kprobe(struct kprobe *p)
431 static inline void unregister_kprobes(struct kprobe **kps, int num)
434 static inline int register_kretprobe(struct kretprobe *rp)
438 static inline int register_kretprobes(struct kretprobe **rps, int num)
442 static inline void unregister_kretprobe(struct kretprobe *rp)
445 static inline void unregister_kretprobes(struct kretprobe **rps, int num)
448 static inline void kprobe_flush_task(struct task_struct *tk)
451 static inline void kprobe_free_init_mem(void)
454 static inline int disable_kprobe(struct kprobe *kp)
458 static inline int enable_kprobe(struct kprobe *kp)
463 static inline bool within_kprobe_blacklist(unsigned long addr)
467 static inline int kprobe_get_kallsym(unsigned int symnum, unsigned long *value,
468 char *type, char *sym)
472 #endif /* CONFIG_KPROBES */
473 static inline int disable_kretprobe(struct kretprobe *rp)
475 return disable_kprobe(&rp->kp);
477 static inline int enable_kretprobe(struct kretprobe *rp)
479 return enable_kprobe(&rp->kp);
482 #ifndef CONFIG_KPROBES
483 static inline bool is_kprobe_insn_slot(unsigned long addr)
488 #ifndef CONFIG_OPTPROBES
489 static inline bool is_kprobe_optinsn_slot(unsigned long addr)
495 /* Returns true if kprobes handled the fault */
496 static nokprobe_inline bool kprobe_page_fault(struct pt_regs *regs,
499 if (!kprobes_built_in())
504 * To be potentially processing a kprobe fault and to be allowed
505 * to call kprobe_running(), we have to be non-preemptible.
509 if (!kprobe_running())
511 return kprobe_fault_handler(regs, trap);
514 #endif /* _LINUX_KPROBES_H */