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/objpool.h>
30 #include <linux/rethook.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 */
105 #define KPROBE_FLAG_ON_FUNC_ENTRY 16 /* probe is on the function entry */
107 /* Has this kprobe gone ? */
108 static inline bool kprobe_gone(struct kprobe
*p
)
110 return p
->flags
& KPROBE_FLAG_GONE
;
113 /* Is this kprobe disabled ? */
114 static inline bool 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 bool kprobe_optimized(struct kprobe
*p
)
122 return p
->flags
& KPROBE_FLAG_OPTIMIZED
;
125 /* Is this kprobe uses ftrace ? */
126 static inline bool 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
;
143 struct objpool_head pool
;
148 kretprobe_handler_t handler
;
149 kretprobe_handler_t entry_handler
;
153 #ifdef CONFIG_KRETPROBE_ON_RETHOOK
156 struct kretprobe_holder
*rph
;
160 #define KRETPROBE_MAX_DATA_SIZE 4096
162 struct kretprobe_instance
{
163 #ifdef CONFIG_KRETPROBE_ON_RETHOOK
164 struct rethook_node node
;
167 struct llist_node llist
;
168 struct kretprobe_holder
*rph
;
169 kprobe_opcode_t
*ret_addr
;
175 struct kretprobe_blackpoint
{
180 struct kprobe_blacklist_entry
{
181 struct list_head list
;
182 unsigned long start_addr
;
183 unsigned long end_addr
;
186 #ifdef CONFIG_KPROBES
187 DECLARE_PER_CPU(struct kprobe
*, current_kprobe
);
188 DECLARE_PER_CPU(struct kprobe_ctlblk
, kprobe_ctlblk
);
190 extern void kprobe_busy_begin(void);
191 extern void kprobe_busy_end(void);
193 #ifdef CONFIG_KRETPROBES
194 /* Check whether @p is used for implementing a trampoline. */
195 extern int arch_trampoline_kprobe(struct kprobe
*p
);
197 #ifdef CONFIG_KRETPROBE_ON_RETHOOK
198 static nokprobe_inline
struct kretprobe
*get_kretprobe(struct kretprobe_instance
*ri
)
200 RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
201 "Kretprobe is accessed from instance under preemptive context");
203 return (struct kretprobe
*)READ_ONCE(ri
->node
.rethook
->data
);
205 static nokprobe_inline
unsigned long get_kretprobe_retaddr(struct kretprobe_instance
*ri
)
207 return ri
->node
.ret_addr
;
210 extern void arch_prepare_kretprobe(struct kretprobe_instance
*ri
,
211 struct pt_regs
*regs
);
212 void arch_kretprobe_fixup_return(struct pt_regs
*regs
,
213 kprobe_opcode_t
*correct_ret_addr
);
215 void __kretprobe_trampoline(void);
217 * Since some architecture uses structured function pointer,
218 * use dereference_function_descriptor() to get real function address.
220 static nokprobe_inline
void *kretprobe_trampoline_addr(void)
222 return dereference_kernel_function_descriptor(__kretprobe_trampoline
);
225 /* If the trampoline handler called from a kprobe, use this version */
226 unsigned long __kretprobe_trampoline_handler(struct pt_regs
*regs
,
227 void *frame_pointer
);
229 static nokprobe_inline
230 unsigned long kretprobe_trampoline_handler(struct pt_regs
*regs
,
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.
240 ret
= __kretprobe_trampoline_handler(regs
, frame_pointer
);
246 static nokprobe_inline
struct kretprobe
*get_kretprobe(struct kretprobe_instance
*ri
)
248 RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
249 "Kretprobe is accessed from instance under preemptive context");
251 return READ_ONCE(ri
->rph
->rp
);
254 static nokprobe_inline
unsigned long get_kretprobe_retaddr(struct kretprobe_instance
*ri
)
256 return (unsigned long)ri
->ret_addr
;
258 #endif /* CONFIG_KRETPROBE_ON_RETHOOK */
260 #else /* !CONFIG_KRETPROBES */
261 static inline void arch_prepare_kretprobe(struct kretprobe
*rp
,
262 struct pt_regs
*regs
)
265 static inline int arch_trampoline_kprobe(struct kprobe
*p
)
269 #endif /* CONFIG_KRETPROBES */
271 /* Markers of '_kprobe_blacklist' section */
272 extern unsigned long __start_kprobe_blacklist
[];
273 extern unsigned long __stop_kprobe_blacklist
[];
275 extern struct kretprobe_blackpoint kretprobe_blacklist
[];
277 #ifdef CONFIG_KPROBES_SANITY_TEST
278 extern int init_test_probes(void);
279 #else /* !CONFIG_KPROBES_SANITY_TEST */
280 static inline int init_test_probes(void)
284 #endif /* CONFIG_KPROBES_SANITY_TEST */
286 extern int arch_prepare_kprobe(struct kprobe
*p
);
287 extern void arch_arm_kprobe(struct kprobe
*p
);
288 extern void arch_disarm_kprobe(struct kprobe
*p
);
289 extern int arch_init_kprobes(void);
290 extern void kprobes_inc_nmissed_count(struct kprobe
*p
);
291 extern bool arch_within_kprobe_blacklist(unsigned long addr
);
292 extern int arch_populate_kprobe_blacklist(void);
293 extern int kprobe_on_func_entry(kprobe_opcode_t
*addr
, const char *sym
, unsigned long offset
);
295 extern bool within_kprobe_blacklist(unsigned long addr
);
296 extern int kprobe_add_ksym_blacklist(unsigned long entry
);
297 extern int kprobe_add_area_blacklist(unsigned long start
, unsigned long end
);
299 struct kprobe_insn_cache
{
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 */
309 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
310 extern kprobe_opcode_t
*__get_insn_slot(struct kprobe_insn_cache
*c
);
311 extern void __free_insn_slot(struct kprobe_insn_cache
*c
,
312 kprobe_opcode_t
*slot
, int dirty
);
313 /* sleep-less address checking routine */
314 extern bool __is_insn_slot_addr(struct kprobe_insn_cache
*c
,
317 #define DEFINE_INSN_CACHE_OPS(__name) \
318 extern struct kprobe_insn_cache kprobe_##__name##_slots; \
320 static inline kprobe_opcode_t *get_##__name##_slot(void) \
322 return __get_insn_slot(&kprobe_##__name##_slots); \
325 static inline void free_##__name##_slot(kprobe_opcode_t *slot, int dirty)\
327 __free_insn_slot(&kprobe_##__name##_slots, slot, dirty); \
330 static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
332 return __is_insn_slot_addr(&kprobe_##__name##_slots, addr); \
334 #define KPROBE_INSN_PAGE_SYM "kprobe_insn_page"
335 #define KPROBE_OPTINSN_PAGE_SYM "kprobe_optinsn_page"
336 int 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) \
340 static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
346 DEFINE_INSN_CACHE_OPS(insn
);
348 #ifdef CONFIG_OPTPROBES
350 * Internal structure for direct jump optimized probe
352 struct optimized_kprobe
{
354 struct list_head list
; /* list for optimizing queue */
355 struct arch_optimized_insn optinsn
;
358 /* Architecture dependent functions for direct jump optimization */
359 extern int arch_prepared_optinsn(struct arch_optimized_insn
*optinsn
);
360 extern int arch_check_optimized_kprobe(struct optimized_kprobe
*op
);
361 extern int arch_prepare_optimized_kprobe(struct optimized_kprobe
*op
,
362 struct kprobe
*orig
);
363 extern void arch_remove_optimized_kprobe(struct optimized_kprobe
*op
);
364 extern void arch_optimize_kprobes(struct list_head
*oplist
);
365 extern void arch_unoptimize_kprobes(struct list_head
*oplist
,
366 struct list_head
*done_list
);
367 extern void arch_unoptimize_kprobe(struct optimized_kprobe
*op
);
368 extern int arch_within_optimized_kprobe(struct optimized_kprobe
*op
,
369 kprobe_opcode_t
*addr
);
371 extern void opt_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
);
373 DEFINE_INSN_CACHE_OPS(optinsn
);
375 extern void wait_for_kprobe_optimizer(void);
376 bool optprobe_queued_unopt(struct optimized_kprobe
*op
);
377 bool kprobe_disarmed(struct kprobe
*p
);
378 #else /* !CONFIG_OPTPROBES */
379 static inline void wait_for_kprobe_optimizer(void) { }
380 #endif /* CONFIG_OPTPROBES */
382 #ifdef CONFIG_KPROBES_ON_FTRACE
383 extern void kprobe_ftrace_handler(unsigned long ip
, unsigned long parent_ip
,
384 struct ftrace_ops
*ops
, struct ftrace_regs
*fregs
);
385 extern int arch_prepare_kprobe_ftrace(struct kprobe
*p
);
387 static inline int arch_prepare_kprobe_ftrace(struct kprobe
*p
)
391 #endif /* CONFIG_KPROBES_ON_FTRACE */
393 /* Get the kprobe at this addr (if any) - called with preemption disabled */
394 struct kprobe
*get_kprobe(void *addr
);
396 /* kprobe_running() will just return the current_kprobe on this CPU */
397 static inline struct kprobe
*kprobe_running(void)
399 return __this_cpu_read(current_kprobe
);
402 static inline void reset_current_kprobe(void)
404 __this_cpu_write(current_kprobe
, NULL
);
407 static inline struct kprobe_ctlblk
*get_kprobe_ctlblk(void)
409 return this_cpu_ptr(&kprobe_ctlblk
);
412 kprobe_opcode_t
*kprobe_lookup_name(const char *name
, unsigned int offset
);
413 kprobe_opcode_t
*arch_adjust_kprobe_addr(unsigned long addr
, unsigned long offset
, bool *on_func_entry
);
415 int register_kprobe(struct kprobe
*p
);
416 void unregister_kprobe(struct kprobe
*p
);
417 int register_kprobes(struct kprobe
**kps
, int num
);
418 void unregister_kprobes(struct kprobe
**kps
, int num
);
420 int register_kretprobe(struct kretprobe
*rp
);
421 void unregister_kretprobe(struct kretprobe
*rp
);
422 int register_kretprobes(struct kretprobe
**rps
, int num
);
423 void unregister_kretprobes(struct kretprobe
**rps
, int num
);
425 #if defined(CONFIG_KRETPROBE_ON_RETHOOK) || !defined(CONFIG_KRETPROBES)
426 #define kprobe_flush_task(tk) do {} while (0)
428 void kprobe_flush_task(struct task_struct
*tk
);
431 void kprobe_free_init_mem(void);
433 int disable_kprobe(struct kprobe
*kp
);
434 int enable_kprobe(struct kprobe
*kp
);
436 void dump_kprobe(struct kprobe
*kp
);
438 void *alloc_insn_page(void);
440 void *alloc_optinsn_page(void);
441 void free_optinsn_page(void *page
);
443 int kprobe_get_kallsym(unsigned int symnum
, unsigned long *value
, char *type
,
446 int arch_kprobe_get_kallsym(unsigned int *symnum
, unsigned long *value
,
447 char *type
, char *sym
);
449 int kprobe_exceptions_notify(struct notifier_block
*self
,
450 unsigned long val
, void *data
);
452 #else /* !CONFIG_KPROBES: */
454 static inline int kprobe_fault_handler(struct pt_regs
*regs
, int trapnr
)
458 static inline struct kprobe
*get_kprobe(void *addr
)
462 static inline struct kprobe
*kprobe_running(void)
466 #define kprobe_busy_begin() do {} while (0)
467 #define kprobe_busy_end() do {} while (0)
469 static inline int register_kprobe(struct kprobe
*p
)
473 static inline int register_kprobes(struct kprobe
**kps
, int num
)
477 static inline void unregister_kprobe(struct kprobe
*p
)
480 static inline void unregister_kprobes(struct kprobe
**kps
, int num
)
483 static inline int register_kretprobe(struct kretprobe
*rp
)
487 static inline int register_kretprobes(struct kretprobe
**rps
, int num
)
491 static inline void unregister_kretprobe(struct kretprobe
*rp
)
494 static inline void unregister_kretprobes(struct kretprobe
**rps
, int num
)
497 static inline void kprobe_flush_task(struct task_struct
*tk
)
500 static inline void kprobe_free_init_mem(void)
503 static inline int disable_kprobe(struct kprobe
*kp
)
507 static inline int enable_kprobe(struct kprobe
*kp
)
512 static inline bool within_kprobe_blacklist(unsigned long addr
)
516 static inline int kprobe_get_kallsym(unsigned int symnum
, unsigned long *value
,
517 char *type
, char *sym
)
521 #endif /* CONFIG_KPROBES */
523 static inline int disable_kretprobe(struct kretprobe
*rp
)
525 return disable_kprobe(&rp
->kp
);
527 static inline int enable_kretprobe(struct kretprobe
*rp
)
529 return enable_kprobe(&rp
->kp
);
532 #ifndef CONFIG_KPROBES
533 static inline bool is_kprobe_insn_slot(unsigned long addr
)
537 #endif /* !CONFIG_KPROBES */
539 #ifndef CONFIG_OPTPROBES
540 static inline bool is_kprobe_optinsn_slot(unsigned long addr
)
544 #endif /* !CONFIG_OPTPROBES */
546 #ifdef CONFIG_KRETPROBES
547 #ifdef CONFIG_KRETPROBE_ON_RETHOOK
548 static nokprobe_inline
bool is_kretprobe_trampoline(unsigned long addr
)
550 return is_rethook_trampoline(addr
);
553 static nokprobe_inline
554 unsigned long kretprobe_find_ret_addr(struct task_struct
*tsk
, void *fp
,
555 struct llist_node
**cur
)
557 return rethook_find_ret_addr(tsk
, (unsigned long)fp
, cur
);
560 static nokprobe_inline
bool is_kretprobe_trampoline(unsigned long addr
)
562 return (void *)addr
== kretprobe_trampoline_addr();
565 unsigned long kretprobe_find_ret_addr(struct task_struct
*tsk
, void *fp
,
566 struct llist_node
**cur
);
569 static nokprobe_inline
bool is_kretprobe_trampoline(unsigned long addr
)
574 static nokprobe_inline
575 unsigned long kretprobe_find_ret_addr(struct task_struct
*tsk
, void *fp
,
576 struct llist_node
**cur
)
582 /* Returns true if kprobes handled the fault */
583 static nokprobe_inline
bool kprobe_page_fault(struct pt_regs
*regs
,
586 if (!IS_ENABLED(CONFIG_KPROBES
))
591 * To be potentially processing a kprobe fault and to be allowed
592 * to call kprobe_running(), we have to be non-preemptible.
596 if (!kprobe_running())
598 return kprobe_fault_handler(regs
, trap
);
601 #endif /* _LINUX_KPROBES_H */