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 struct kretprobe_instance
{
159 struct freelist_node freelist
;
162 struct llist_node llist
;
163 struct kretprobe_holder
*rph
;
164 kprobe_opcode_t
*ret_addr
;
169 struct kretprobe_blackpoint
{
174 struct kprobe_blacklist_entry
{
175 struct list_head list
;
176 unsigned long start_addr
;
177 unsigned long end_addr
;
180 #ifdef CONFIG_KPROBES
181 DECLARE_PER_CPU(struct kprobe
*, current_kprobe
);
182 DECLARE_PER_CPU(struct kprobe_ctlblk
, kprobe_ctlblk
);
185 * For #ifdef avoidance:
187 static inline int kprobes_built_in(void)
192 extern void kprobe_busy_begin(void);
193 extern void kprobe_busy_end(void);
195 #ifdef CONFIG_KRETPROBES
196 extern void arch_prepare_kretprobe(struct kretprobe_instance
*ri
,
197 struct pt_regs
*regs
);
198 extern int arch_trampoline_kprobe(struct kprobe
*p
);
200 /* If the trampoline handler called from a kprobe, use this version */
201 unsigned long __kretprobe_trampoline_handler(struct pt_regs
*regs
,
202 void *trampoline_address
,
203 void *frame_pointer
);
205 static nokprobe_inline
206 unsigned long kretprobe_trampoline_handler(struct pt_regs
*regs
,
207 void *trampoline_address
,
212 * Set a dummy kprobe for avoiding kretprobe recursion.
213 * Since kretprobe never runs in kprobe handler, no kprobe must
214 * be running at this point.
217 ret
= __kretprobe_trampoline_handler(regs
, trampoline_address
, frame_pointer
);
223 static nokprobe_inline
struct kretprobe
*get_kretprobe(struct kretprobe_instance
*ri
)
225 RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
226 "Kretprobe is accessed from instance under preemptive context");
228 return READ_ONCE(ri
->rph
->rp
);
231 #else /* CONFIG_KRETPROBES */
232 static inline void arch_prepare_kretprobe(struct kretprobe
*rp
,
233 struct pt_regs
*regs
)
236 static inline int arch_trampoline_kprobe(struct kprobe
*p
)
240 #endif /* CONFIG_KRETPROBES */
242 extern struct kretprobe_blackpoint kretprobe_blacklist
[];
244 #ifdef CONFIG_KPROBES_SANITY_TEST
245 extern int init_test_probes(void);
247 static inline int init_test_probes(void)
251 #endif /* CONFIG_KPROBES_SANITY_TEST */
253 extern int arch_prepare_kprobe(struct kprobe
*p
);
254 extern void arch_arm_kprobe(struct kprobe
*p
);
255 extern void arch_disarm_kprobe(struct kprobe
*p
);
256 extern int arch_init_kprobes(void);
257 extern void kprobes_inc_nmissed_count(struct kprobe
*p
);
258 extern bool arch_within_kprobe_blacklist(unsigned long addr
);
259 extern int arch_populate_kprobe_blacklist(void);
260 extern bool arch_kprobe_on_func_entry(unsigned long offset
);
261 extern int kprobe_on_func_entry(kprobe_opcode_t
*addr
, const char *sym
, unsigned long offset
);
263 extern bool within_kprobe_blacklist(unsigned long addr
);
264 extern int kprobe_add_ksym_blacklist(unsigned long entry
);
265 extern int kprobe_add_area_blacklist(unsigned long start
, unsigned long end
);
267 struct kprobe_insn_cache
{
269 void *(*alloc
)(void); /* allocate insn page */
270 void (*free
)(void *); /* free insn page */
271 const char *sym
; /* symbol for insn pages */
272 struct list_head pages
; /* list of kprobe_insn_page */
273 size_t insn_size
; /* size of instruction slot */
277 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
278 extern kprobe_opcode_t
*__get_insn_slot(struct kprobe_insn_cache
*c
);
279 extern void __free_insn_slot(struct kprobe_insn_cache
*c
,
280 kprobe_opcode_t
*slot
, int dirty
);
281 /* sleep-less address checking routine */
282 extern bool __is_insn_slot_addr(struct kprobe_insn_cache
*c
,
285 #define DEFINE_INSN_CACHE_OPS(__name) \
286 extern struct kprobe_insn_cache kprobe_##__name##_slots; \
288 static inline kprobe_opcode_t *get_##__name##_slot(void) \
290 return __get_insn_slot(&kprobe_##__name##_slots); \
293 static inline void free_##__name##_slot(kprobe_opcode_t *slot, int dirty)\
295 __free_insn_slot(&kprobe_##__name##_slots, slot, dirty); \
298 static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
300 return __is_insn_slot_addr(&kprobe_##__name##_slots, addr); \
302 #define KPROBE_INSN_PAGE_SYM "kprobe_insn_page"
303 #define KPROBE_OPTINSN_PAGE_SYM "kprobe_optinsn_page"
304 int kprobe_cache_get_kallsym(struct kprobe_insn_cache
*c
, unsigned int *symnum
,
305 unsigned long *value
, char *type
, char *sym
);
306 #else /* __ARCH_WANT_KPROBES_INSN_SLOT */
307 #define DEFINE_INSN_CACHE_OPS(__name) \
308 static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
314 DEFINE_INSN_CACHE_OPS(insn
);
316 #ifdef CONFIG_OPTPROBES
318 * Internal structure for direct jump optimized probe
320 struct optimized_kprobe
{
322 struct list_head list
; /* list for optimizing queue */
323 struct arch_optimized_insn optinsn
;
326 /* Architecture dependent functions for direct jump optimization */
327 extern int arch_prepared_optinsn(struct arch_optimized_insn
*optinsn
);
328 extern int arch_check_optimized_kprobe(struct optimized_kprobe
*op
);
329 extern int arch_prepare_optimized_kprobe(struct optimized_kprobe
*op
,
330 struct kprobe
*orig
);
331 extern void arch_remove_optimized_kprobe(struct optimized_kprobe
*op
);
332 extern void arch_optimize_kprobes(struct list_head
*oplist
);
333 extern void arch_unoptimize_kprobes(struct list_head
*oplist
,
334 struct list_head
*done_list
);
335 extern void arch_unoptimize_kprobe(struct optimized_kprobe
*op
);
336 extern int arch_within_optimized_kprobe(struct optimized_kprobe
*op
,
339 extern void opt_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
);
341 DEFINE_INSN_CACHE_OPS(optinsn
);
344 extern int sysctl_kprobes_optimization
;
345 extern int proc_kprobes_optimization_handler(struct ctl_table
*table
,
346 int write
, void *buffer
,
347 size_t *length
, loff_t
*ppos
);
349 extern void wait_for_kprobe_optimizer(void);
351 static inline void wait_for_kprobe_optimizer(void) { }
352 #endif /* CONFIG_OPTPROBES */
353 #ifdef CONFIG_KPROBES_ON_FTRACE
354 extern void kprobe_ftrace_handler(unsigned long ip
, unsigned long parent_ip
,
355 struct ftrace_ops
*ops
, struct ftrace_regs
*fregs
);
356 extern int arch_prepare_kprobe_ftrace(struct kprobe
*p
);
359 int arch_check_ftrace_location(struct kprobe
*p
);
361 /* Get the kprobe at this addr (if any) - called with preemption disabled */
362 struct kprobe
*get_kprobe(void *addr
);
364 /* kprobe_running() will just return the current_kprobe on this CPU */
365 static inline struct kprobe
*kprobe_running(void)
367 return (__this_cpu_read(current_kprobe
));
370 static inline void reset_current_kprobe(void)
372 __this_cpu_write(current_kprobe
, NULL
);
375 static inline struct kprobe_ctlblk
*get_kprobe_ctlblk(void)
377 return this_cpu_ptr(&kprobe_ctlblk
);
380 kprobe_opcode_t
*kprobe_lookup_name(const char *name
, unsigned int offset
);
381 int register_kprobe(struct kprobe
*p
);
382 void unregister_kprobe(struct kprobe
*p
);
383 int register_kprobes(struct kprobe
**kps
, int num
);
384 void unregister_kprobes(struct kprobe
**kps
, int num
);
385 unsigned long arch_deref_entry_point(void *);
387 int register_kretprobe(struct kretprobe
*rp
);
388 void unregister_kretprobe(struct kretprobe
*rp
);
389 int register_kretprobes(struct kretprobe
**rps
, int num
);
390 void unregister_kretprobes(struct kretprobe
**rps
, int num
);
392 void kprobe_flush_task(struct task_struct
*tk
);
394 void kprobe_free_init_mem(void);
396 int disable_kprobe(struct kprobe
*kp
);
397 int enable_kprobe(struct kprobe
*kp
);
399 void dump_kprobe(struct kprobe
*kp
);
401 void *alloc_insn_page(void);
403 void *alloc_optinsn_page(void);
404 void free_optinsn_page(void *page
);
406 int kprobe_get_kallsym(unsigned int symnum
, unsigned long *value
, char *type
,
409 int arch_kprobe_get_kallsym(unsigned int *symnum
, unsigned long *value
,
410 char *type
, char *sym
);
411 #else /* !CONFIG_KPROBES: */
413 static inline int kprobes_built_in(void)
417 static inline int kprobe_fault_handler(struct pt_regs
*regs
, int trapnr
)
421 static inline struct kprobe
*get_kprobe(void *addr
)
425 static inline struct kprobe
*kprobe_running(void)
429 static inline int register_kprobe(struct kprobe
*p
)
433 static inline int register_kprobes(struct kprobe
**kps
, int num
)
437 static inline void unregister_kprobe(struct kprobe
*p
)
440 static inline void unregister_kprobes(struct kprobe
**kps
, int num
)
443 static inline int register_kretprobe(struct kretprobe
*rp
)
447 static inline int register_kretprobes(struct kretprobe
**rps
, int num
)
451 static inline void unregister_kretprobe(struct kretprobe
*rp
)
454 static inline void unregister_kretprobes(struct kretprobe
**rps
, int num
)
457 static inline void kprobe_flush_task(struct task_struct
*tk
)
460 static inline void kprobe_free_init_mem(void)
463 static inline int disable_kprobe(struct kprobe
*kp
)
467 static inline int enable_kprobe(struct kprobe
*kp
)
472 static inline bool within_kprobe_blacklist(unsigned long addr
)
476 static inline int kprobe_get_kallsym(unsigned int symnum
, unsigned long *value
,
477 char *type
, char *sym
)
481 #endif /* CONFIG_KPROBES */
482 static inline int disable_kretprobe(struct kretprobe
*rp
)
484 return disable_kprobe(&rp
->kp
);
486 static inline int enable_kretprobe(struct kretprobe
*rp
)
488 return enable_kprobe(&rp
->kp
);
491 #ifndef CONFIG_KPROBES
492 static inline bool is_kprobe_insn_slot(unsigned long addr
)
497 #ifndef CONFIG_OPTPROBES
498 static inline bool is_kprobe_optinsn_slot(unsigned long addr
)
504 /* Returns true if kprobes handled the fault */
505 static nokprobe_inline
bool kprobe_page_fault(struct pt_regs
*regs
,
508 if (!kprobes_built_in())
513 * To be potentially processing a kprobe fault and to be allowed
514 * to call kprobe_running(), we have to be non-preemptible.
518 if (!kprobe_running())
520 return kprobe_fault_handler(regs
, trap
);
523 #endif /* _LINUX_KPROBES_H */