Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2#ifndef _LINUX_KPROBES_H
3#define _LINUX_KPROBES_H
4/*
5 * Kernel Probes (KProbes)
6 * include/linux/kprobes.h
7 *
8 * Copyright (C) IBM Corporation, 2002, 2004
9 *
10 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
11 * Probes initial implementation ( includes suggestions from
12 * Rusty Russell).
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.
18 */
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>
31
32#ifdef CONFIG_KPROBES
33
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
39
40#else /* CONFIG_KPROBES */
41#include <asm-generic/kprobes.h>
42typedef int kprobe_opcode_t;
43struct arch_specific_insn {
44 int dummy;
45};
46#endif /* CONFIG_KPROBES */
47
48struct kprobe;
49struct pt_regs;
50struct kretprobe;
51struct kretprobe_instance;
52typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
53typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *,
54 unsigned long flags);
55typedef int (*kprobe_fault_handler_t) (struct kprobe *, struct pt_regs *,
56 int trapnr);
57typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,
58 struct pt_regs *);
59
60struct kprobe {
61 struct hlist_node hlist;
62
63 /* list of kprobes for multi-handler support */
64 struct list_head list;
65
66 /*count the number of times this probe was temporarily disarmed */
67 unsigned long nmissed;
68
69 /* location of the probe point */
70 kprobe_opcode_t *addr;
71
72 /* Allow user to indicate symbol name of the probe point */
73 const char *symbol_name;
74
75 /* Offset into the symbol */
76 unsigned int offset;
77
78 /* Called before addr is executed. */
79 kprobe_pre_handler_t pre_handler;
80
81 /* Called after addr is executed, unless... */
82 kprobe_post_handler_t post_handler;
83
84 /*
85 * ... called if executing addr causes a fault (eg. page fault).
86 * Return 1 if it handled fault, otherwise kernel will see it.
87 */
88 kprobe_fault_handler_t fault_handler;
89
90 /* Saved opcode (which has been replaced with breakpoint) */
91 kprobe_opcode_t opcode;
92
93 /* copy of the original instruction */
94 struct arch_specific_insn ainsn;
95
96 /*
97 * Indicates various status flags.
98 * Protected by kprobe_mutex after this kprobe is registered.
99 */
100 u32 flags;
101};
102
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.
108 * NOTE:
109 * this flag is only for optimized_kprobe.
110 */
111#define KPROBE_FLAG_FTRACE 8 /* probe is using ftrace */
112
113/* Has this kprobe gone ? */
114static inline int kprobe_gone(struct kprobe *p)
115{
116 return p->flags & KPROBE_FLAG_GONE;
117}
118
119/* Is this kprobe disabled ? */
120static inline int kprobe_disabled(struct kprobe *p)
121{
122 return p->flags & (KPROBE_FLAG_DISABLED | KPROBE_FLAG_GONE);
123}
124
125/* Is this kprobe really running optimized path ? */
126static inline int kprobe_optimized(struct kprobe *p)
127{
128 return p->flags & KPROBE_FLAG_OPTIMIZED;
129}
130
131/* Is this kprobe uses ftrace ? */
132static inline int kprobe_ftrace(struct kprobe *p)
133{
134 return p->flags & KPROBE_FLAG_FTRACE;
135}
136
137/*
138 * Function-return probe -
139 * Note:
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.
145 *
146 */
147struct kretprobe {
148 struct kprobe kp;
149 kretprobe_handler_t handler;
150 kretprobe_handler_t entry_handler;
151 int maxactive;
152 int nmissed;
153 size_t data_size;
154 struct hlist_head free_instances;
155 raw_spinlock_t lock;
156};
157
158struct kretprobe_instance {
159 struct hlist_node hlist;
160 struct kretprobe *rp;
161 kprobe_opcode_t *ret_addr;
162 struct task_struct *task;
163 void *fp;
164 char data[];
165};
166
167struct kretprobe_blackpoint {
168 const char *name;
169 void *addr;
170};
171
172struct kprobe_blacklist_entry {
173 struct list_head list;
174 unsigned long start_addr;
175 unsigned long end_addr;
176};
177
178#ifdef CONFIG_KPROBES
179DECLARE_PER_CPU(struct kprobe *, current_kprobe);
180DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
181
182/*
183 * For #ifdef avoidance:
184 */
185static inline int kprobes_built_in(void)
186{
187 return 1;
188}
189
190#ifdef CONFIG_KRETPROBES
191extern void arch_prepare_kretprobe(struct kretprobe_instance *ri,
192 struct pt_regs *regs);
193extern int arch_trampoline_kprobe(struct kprobe *p);
194#else /* CONFIG_KRETPROBES */
195static inline void arch_prepare_kretprobe(struct kretprobe *rp,
196 struct pt_regs *regs)
197{
198}
199static inline int arch_trampoline_kprobe(struct kprobe *p)
200{
201 return 0;
202}
203#endif /* CONFIG_KRETPROBES */
204
205extern struct kretprobe_blackpoint kretprobe_blacklist[];
206
207static inline void kretprobe_assert(struct kretprobe_instance *ri,
208 unsigned long orig_ret_address, unsigned long trampoline_address)
209{
210 if (!orig_ret_address || (orig_ret_address == trampoline_address)) {
211 printk("kretprobe BUG!: Processing kretprobe %p @ %p\n",
212 ri->rp, ri->rp->kp.addr);
213 BUG();
214 }
215}
216
217#ifdef CONFIG_KPROBES_SANITY_TEST
218extern int init_test_probes(void);
219#else
220static inline int init_test_probes(void)
221{
222 return 0;
223}
224#endif /* CONFIG_KPROBES_SANITY_TEST */
225
226extern int arch_prepare_kprobe(struct kprobe *p);
227extern void arch_arm_kprobe(struct kprobe *p);
228extern void arch_disarm_kprobe(struct kprobe *p);
229extern int arch_init_kprobes(void);
230extern void kprobes_inc_nmissed_count(struct kprobe *p);
231extern bool arch_within_kprobe_blacklist(unsigned long addr);
232extern int arch_populate_kprobe_blacklist(void);
233extern bool arch_kprobe_on_func_entry(unsigned long offset);
234extern bool kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset);
235
236extern bool within_kprobe_blacklist(unsigned long addr);
237extern int kprobe_add_ksym_blacklist(unsigned long entry);
238extern int kprobe_add_area_blacklist(unsigned long start, unsigned long end);
239
240struct kprobe_insn_cache {
241 struct mutex mutex;
242 void *(*alloc)(void); /* allocate insn page */
243 void (*free)(void *); /* free insn page */
244 const char *sym; /* symbol for insn pages */
245 struct list_head pages; /* list of kprobe_insn_page */
246 size_t insn_size; /* size of instruction slot */
247 int nr_garbage;
248};
249
250#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
251extern kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c);
252extern void __free_insn_slot(struct kprobe_insn_cache *c,
253 kprobe_opcode_t *slot, int dirty);
254/* sleep-less address checking routine */
255extern bool __is_insn_slot_addr(struct kprobe_insn_cache *c,
256 unsigned long addr);
257
258#define DEFINE_INSN_CACHE_OPS(__name) \
259extern struct kprobe_insn_cache kprobe_##__name##_slots; \
260 \
261static inline kprobe_opcode_t *get_##__name##_slot(void) \
262{ \
263 return __get_insn_slot(&kprobe_##__name##_slots); \
264} \
265 \
266static inline void free_##__name##_slot(kprobe_opcode_t *slot, int dirty)\
267{ \
268 __free_insn_slot(&kprobe_##__name##_slots, slot, dirty); \
269} \
270 \
271static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
272{ \
273 return __is_insn_slot_addr(&kprobe_##__name##_slots, addr); \
274}
275#define KPROBE_INSN_PAGE_SYM "kprobe_insn_page"
276#define KPROBE_OPTINSN_PAGE_SYM "kprobe_optinsn_page"
277int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
278 unsigned long *value, char *type, char *sym);
279#else /* __ARCH_WANT_KPROBES_INSN_SLOT */
280#define DEFINE_INSN_CACHE_OPS(__name) \
281static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
282{ \
283 return 0; \
284}
285#endif
286
287DEFINE_INSN_CACHE_OPS(insn);
288
289#ifdef CONFIG_OPTPROBES
290/*
291 * Internal structure for direct jump optimized probe
292 */
293struct optimized_kprobe {
294 struct kprobe kp;
295 struct list_head list; /* list for optimizing queue */
296 struct arch_optimized_insn optinsn;
297};
298
299/* Architecture dependent functions for direct jump optimization */
300extern int arch_prepared_optinsn(struct arch_optimized_insn *optinsn);
301extern int arch_check_optimized_kprobe(struct optimized_kprobe *op);
302extern int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
303 struct kprobe *orig);
304extern void arch_remove_optimized_kprobe(struct optimized_kprobe *op);
305extern void arch_optimize_kprobes(struct list_head *oplist);
306extern void arch_unoptimize_kprobes(struct list_head *oplist,
307 struct list_head *done_list);
308extern void arch_unoptimize_kprobe(struct optimized_kprobe *op);
309extern int arch_within_optimized_kprobe(struct optimized_kprobe *op,
310 unsigned long addr);
311
312extern void opt_pre_handler(struct kprobe *p, struct pt_regs *regs);
313
314DEFINE_INSN_CACHE_OPS(optinsn);
315
316#ifdef CONFIG_SYSCTL
317extern int sysctl_kprobes_optimization;
318extern int proc_kprobes_optimization_handler(struct ctl_table *table,
319 int write, void *buffer,
320 size_t *length, loff_t *ppos);
321#endif
322extern void wait_for_kprobe_optimizer(void);
323#else
324static inline void wait_for_kprobe_optimizer(void) { }
325#endif /* CONFIG_OPTPROBES */
326#ifdef CONFIG_KPROBES_ON_FTRACE
327extern void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
328 struct ftrace_ops *ops, struct pt_regs *regs);
329extern int arch_prepare_kprobe_ftrace(struct kprobe *p);
330#endif
331
332int arch_check_ftrace_location(struct kprobe *p);
333
334/* Get the kprobe at this addr (if any) - called with preemption disabled */
335struct kprobe *get_kprobe(void *addr);
336void kretprobe_hash_lock(struct task_struct *tsk,
337 struct hlist_head **head, unsigned long *flags);
338void kretprobe_hash_unlock(struct task_struct *tsk, unsigned long *flags);
339struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk);
340
341/* kprobe_running() will just return the current_kprobe on this CPU */
342static inline struct kprobe *kprobe_running(void)
343{
344 return (__this_cpu_read(current_kprobe));
345}
346
347static inline void reset_current_kprobe(void)
348{
349 __this_cpu_write(current_kprobe, NULL);
350}
351
352static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
353{
354 return this_cpu_ptr(&kprobe_ctlblk);
355}
356
357extern struct kprobe kprobe_busy;
358void kprobe_busy_begin(void);
359void kprobe_busy_end(void);
360
361kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset);
362int register_kprobe(struct kprobe *p);
363void unregister_kprobe(struct kprobe *p);
364int register_kprobes(struct kprobe **kps, int num);
365void unregister_kprobes(struct kprobe **kps, int num);
366unsigned long arch_deref_entry_point(void *);
367
368int register_kretprobe(struct kretprobe *rp);
369void unregister_kretprobe(struct kretprobe *rp);
370int register_kretprobes(struct kretprobe **rps, int num);
371void unregister_kretprobes(struct kretprobe **rps, int num);
372
373void kprobe_flush_task(struct task_struct *tk);
374void recycle_rp_inst(struct kretprobe_instance *ri, struct hlist_head *head);
375
376void kprobe_free_init_mem(void);
377
378int disable_kprobe(struct kprobe *kp);
379int enable_kprobe(struct kprobe *kp);
380
381void dump_kprobe(struct kprobe *kp);
382
383void *alloc_insn_page(void);
384void free_insn_page(void *page);
385
386int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
387 char *sym);
388
389int arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
390 char *type, char *sym);
391#else /* !CONFIG_KPROBES: */
392
393static inline int kprobes_built_in(void)
394{
395 return 0;
396}
397static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
398{
399 return 0;
400}
401static inline struct kprobe *get_kprobe(void *addr)
402{
403 return NULL;
404}
405static inline struct kprobe *kprobe_running(void)
406{
407 return NULL;
408}
409static inline int register_kprobe(struct kprobe *p)
410{
411 return -ENOSYS;
412}
413static inline int register_kprobes(struct kprobe **kps, int num)
414{
415 return -ENOSYS;
416}
417static inline void unregister_kprobe(struct kprobe *p)
418{
419}
420static inline void unregister_kprobes(struct kprobe **kps, int num)
421{
422}
423static inline int register_kretprobe(struct kretprobe *rp)
424{
425 return -ENOSYS;
426}
427static inline int register_kretprobes(struct kretprobe **rps, int num)
428{
429 return -ENOSYS;
430}
431static inline void unregister_kretprobe(struct kretprobe *rp)
432{
433}
434static inline void unregister_kretprobes(struct kretprobe **rps, int num)
435{
436}
437static inline void kprobe_flush_task(struct task_struct *tk)
438{
439}
440static inline void kprobe_free_init_mem(void)
441{
442}
443static inline int disable_kprobe(struct kprobe *kp)
444{
445 return -ENOSYS;
446}
447static inline int enable_kprobe(struct kprobe *kp)
448{
449 return -ENOSYS;
450}
451
452static inline bool within_kprobe_blacklist(unsigned long addr)
453{
454 return true;
455}
456static inline int kprobe_get_kallsym(unsigned int symnum, unsigned long *value,
457 char *type, char *sym)
458{
459 return -ERANGE;
460}
461#endif /* CONFIG_KPROBES */
462static inline int disable_kretprobe(struct kretprobe *rp)
463{
464 return disable_kprobe(&rp->kp);
465}
466static inline int enable_kretprobe(struct kretprobe *rp)
467{
468 return enable_kprobe(&rp->kp);
469}
470
471#ifndef CONFIG_KPROBES
472static inline bool is_kprobe_insn_slot(unsigned long addr)
473{
474 return false;
475}
476#endif
477#ifndef CONFIG_OPTPROBES
478static inline bool is_kprobe_optinsn_slot(unsigned long addr)
479{
480 return false;
481}
482#endif
483
484/* Returns true if kprobes handled the fault */
485static nokprobe_inline bool kprobe_page_fault(struct pt_regs *regs,
486 unsigned int trap)
487{
488 if (!kprobes_built_in())
489 return false;
490 if (user_mode(regs))
491 return false;
492 /*
493 * To be potentially processing a kprobe fault and to be allowed
494 * to call kprobe_running(), we have to be non-preemptible.
495 */
496 if (preemptible())
497 return false;
498 if (!kprobe_running())
499 return false;
500 return kprobe_fault_handler(regs, trap);
501}
502
503#endif /* _LINUX_KPROBES_H */