blob: 4dbebd319b6f5d3117449ba5e3087b19b7cc22a1 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0-or-later */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002#ifndef _LINUX_KPROBES_H
3#define _LINUX_KPROBES_H
4/*
5 * Kernel Probes (KProbes)
6 * include/linux/kprobes.h
7 *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008 * 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
Olivier Deprez157378f2022-04-04 15:47:50 +0200158#define KRETPROBE_MAX_DATA_SIZE 4096
159
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000160struct kretprobe_instance {
Olivier Deprez157378f2022-04-04 15:47:50 +0200161 union {
162 struct hlist_node hlist;
163 struct rcu_head rcu;
164 };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000165 struct kretprobe *rp;
166 kprobe_opcode_t *ret_addr;
167 struct task_struct *task;
David Brazdil0f672f62019-12-10 10:32:29 +0000168 void *fp;
Olivier Deprez157378f2022-04-04 15:47:50 +0200169 char data[];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000170};
171
172struct kretprobe_blackpoint {
173 const char *name;
174 void *addr;
175};
176
177struct kprobe_blacklist_entry {
178 struct list_head list;
179 unsigned long start_addr;
180 unsigned long end_addr;
181};
182
183#ifdef CONFIG_KPROBES
184DECLARE_PER_CPU(struct kprobe *, current_kprobe);
185DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
186
187/*
188 * For #ifdef avoidance:
189 */
190static inline int kprobes_built_in(void)
191{
192 return 1;
193}
194
Olivier Deprez157378f2022-04-04 15:47:50 +0200195extern void kprobe_busy_begin(void);
196extern void kprobe_busy_end(void);
197
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000198#ifdef CONFIG_KRETPROBES
199extern void arch_prepare_kretprobe(struct kretprobe_instance *ri,
200 struct pt_regs *regs);
201extern int arch_trampoline_kprobe(struct kprobe *p);
Olivier Deprez157378f2022-04-04 15:47:50 +0200202
203/* If the trampoline handler called from a kprobe, use this version */
204unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs,
205 void *trampoline_address,
206 void *frame_pointer);
207
208static nokprobe_inline
209unsigned long kretprobe_trampoline_handler(struct pt_regs *regs,
210 void *trampoline_address,
211 void *frame_pointer)
212{
213 unsigned long ret;
214 /*
215 * Set a dummy kprobe for avoiding kretprobe recursion.
216 * Since kretprobe never runs in kprobe handler, no kprobe must
217 * be running at this point.
218 */
219 kprobe_busy_begin();
220 ret = __kretprobe_trampoline_handler(regs, trampoline_address, frame_pointer);
221 kprobe_busy_end();
222
223 return ret;
224}
225
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000226#else /* CONFIG_KRETPROBES */
227static inline void arch_prepare_kretprobe(struct kretprobe *rp,
228 struct pt_regs *regs)
229{
230}
231static inline int arch_trampoline_kprobe(struct kprobe *p)
232{
233 return 0;
234}
235#endif /* CONFIG_KRETPROBES */
236
237extern struct kretprobe_blackpoint kretprobe_blacklist[];
238
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000239#ifdef CONFIG_KPROBES_SANITY_TEST
240extern int init_test_probes(void);
241#else
242static inline int init_test_probes(void)
243{
244 return 0;
245}
246#endif /* CONFIG_KPROBES_SANITY_TEST */
247
248extern int arch_prepare_kprobe(struct kprobe *p);
249extern void arch_arm_kprobe(struct kprobe *p);
250extern void arch_disarm_kprobe(struct kprobe *p);
251extern int arch_init_kprobes(void);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000252extern void kprobes_inc_nmissed_count(struct kprobe *p);
253extern bool arch_within_kprobe_blacklist(unsigned long addr);
David Brazdil0f672f62019-12-10 10:32:29 +0000254extern int arch_populate_kprobe_blacklist(void);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000255extern bool arch_kprobe_on_func_entry(unsigned long offset);
Olivier Deprez0e641232021-09-23 10:07:05 +0200256extern int kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000257
258extern bool within_kprobe_blacklist(unsigned long addr);
David Brazdil0f672f62019-12-10 10:32:29 +0000259extern int kprobe_add_ksym_blacklist(unsigned long entry);
260extern int kprobe_add_area_blacklist(unsigned long start, unsigned long end);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000261
262struct kprobe_insn_cache {
263 struct mutex mutex;
264 void *(*alloc)(void); /* allocate insn page */
265 void (*free)(void *); /* free insn page */
Olivier Deprez157378f2022-04-04 15:47:50 +0200266 const char *sym; /* symbol for insn pages */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000267 struct list_head pages; /* list of kprobe_insn_page */
268 size_t insn_size; /* size of instruction slot */
269 int nr_garbage;
270};
271
272#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
273extern kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c);
274extern void __free_insn_slot(struct kprobe_insn_cache *c,
275 kprobe_opcode_t *slot, int dirty);
276/* sleep-less address checking routine */
277extern bool __is_insn_slot_addr(struct kprobe_insn_cache *c,
278 unsigned long addr);
279
280#define DEFINE_INSN_CACHE_OPS(__name) \
281extern struct kprobe_insn_cache kprobe_##__name##_slots; \
282 \
283static inline kprobe_opcode_t *get_##__name##_slot(void) \
284{ \
285 return __get_insn_slot(&kprobe_##__name##_slots); \
286} \
287 \
288static inline void free_##__name##_slot(kprobe_opcode_t *slot, int dirty)\
289{ \
290 __free_insn_slot(&kprobe_##__name##_slots, slot, dirty); \
291} \
292 \
293static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
294{ \
295 return __is_insn_slot_addr(&kprobe_##__name##_slots, addr); \
296}
Olivier Deprez157378f2022-04-04 15:47:50 +0200297#define KPROBE_INSN_PAGE_SYM "kprobe_insn_page"
298#define KPROBE_OPTINSN_PAGE_SYM "kprobe_optinsn_page"
299int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
300 unsigned long *value, char *type, char *sym);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000301#else /* __ARCH_WANT_KPROBES_INSN_SLOT */
302#define DEFINE_INSN_CACHE_OPS(__name) \
303static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
304{ \
305 return 0; \
306}
307#endif
308
309DEFINE_INSN_CACHE_OPS(insn);
310
311#ifdef CONFIG_OPTPROBES
312/*
313 * Internal structure for direct jump optimized probe
314 */
315struct optimized_kprobe {
316 struct kprobe kp;
317 struct list_head list; /* list for optimizing queue */
318 struct arch_optimized_insn optinsn;
319};
320
321/* Architecture dependent functions for direct jump optimization */
322extern int arch_prepared_optinsn(struct arch_optimized_insn *optinsn);
323extern int arch_check_optimized_kprobe(struct optimized_kprobe *op);
324extern int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
325 struct kprobe *orig);
326extern void arch_remove_optimized_kprobe(struct optimized_kprobe *op);
327extern void arch_optimize_kprobes(struct list_head *oplist);
328extern void arch_unoptimize_kprobes(struct list_head *oplist,
329 struct list_head *done_list);
330extern void arch_unoptimize_kprobe(struct optimized_kprobe *op);
331extern int arch_within_optimized_kprobe(struct optimized_kprobe *op,
332 unsigned long addr);
333
334extern void opt_pre_handler(struct kprobe *p, struct pt_regs *regs);
335
336DEFINE_INSN_CACHE_OPS(optinsn);
337
338#ifdef CONFIG_SYSCTL
339extern int sysctl_kprobes_optimization;
340extern int proc_kprobes_optimization_handler(struct ctl_table *table,
Olivier Deprez157378f2022-04-04 15:47:50 +0200341 int write, void *buffer,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000342 size_t *length, loff_t *ppos);
343#endif
344extern void wait_for_kprobe_optimizer(void);
345#else
346static inline void wait_for_kprobe_optimizer(void) { }
347#endif /* CONFIG_OPTPROBES */
348#ifdef CONFIG_KPROBES_ON_FTRACE
349extern void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
350 struct ftrace_ops *ops, struct pt_regs *regs);
351extern int arch_prepare_kprobe_ftrace(struct kprobe *p);
352#endif
353
354int arch_check_ftrace_location(struct kprobe *p);
355
356/* Get the kprobe at this addr (if any) - called with preemption disabled */
357struct kprobe *get_kprobe(void *addr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000358
359/* kprobe_running() will just return the current_kprobe on this CPU */
360static inline struct kprobe *kprobe_running(void)
361{
362 return (__this_cpu_read(current_kprobe));
363}
364
365static inline void reset_current_kprobe(void)
366{
367 __this_cpu_write(current_kprobe, NULL);
368}
369
370static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
371{
372 return this_cpu_ptr(&kprobe_ctlblk);
373}
374
375kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset);
376int register_kprobe(struct kprobe *p);
377void unregister_kprobe(struct kprobe *p);
378int register_kprobes(struct kprobe **kps, int num);
379void unregister_kprobes(struct kprobe **kps, int num);
380unsigned long arch_deref_entry_point(void *);
381
382int register_kretprobe(struct kretprobe *rp);
383void unregister_kretprobe(struct kretprobe *rp);
384int register_kretprobes(struct kretprobe **rps, int num);
385void unregister_kretprobes(struct kretprobe **rps, int num);
386
387void kprobe_flush_task(struct task_struct *tk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000388
Olivier Deprez0e641232021-09-23 10:07:05 +0200389void kprobe_free_init_mem(void);
390
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000391int disable_kprobe(struct kprobe *kp);
392int enable_kprobe(struct kprobe *kp);
393
394void dump_kprobe(struct kprobe *kp);
395
David Brazdil0f672f62019-12-10 10:32:29 +0000396void *alloc_insn_page(void);
397void free_insn_page(void *page);
398
Olivier Deprez157378f2022-04-04 15:47:50 +0200399int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
400 char *sym);
401
402int arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
403 char *type, char *sym);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000404#else /* !CONFIG_KPROBES: */
405
406static inline int kprobes_built_in(void)
407{
408 return 0;
409}
410static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
411{
412 return 0;
413}
414static inline struct kprobe *get_kprobe(void *addr)
415{
416 return NULL;
417}
418static inline struct kprobe *kprobe_running(void)
419{
420 return NULL;
421}
422static inline int register_kprobe(struct kprobe *p)
423{
424 return -ENOSYS;
425}
426static inline int register_kprobes(struct kprobe **kps, int num)
427{
428 return -ENOSYS;
429}
430static inline void unregister_kprobe(struct kprobe *p)
431{
432}
433static inline void unregister_kprobes(struct kprobe **kps, int num)
434{
435}
436static inline int register_kretprobe(struct kretprobe *rp)
437{
438 return -ENOSYS;
439}
440static inline int register_kretprobes(struct kretprobe **rps, int num)
441{
442 return -ENOSYS;
443}
444static inline void unregister_kretprobe(struct kretprobe *rp)
445{
446}
447static inline void unregister_kretprobes(struct kretprobe **rps, int num)
448{
449}
450static inline void kprobe_flush_task(struct task_struct *tk)
451{
452}
Olivier Deprez0e641232021-09-23 10:07:05 +0200453static inline void kprobe_free_init_mem(void)
454{
455}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000456static inline int disable_kprobe(struct kprobe *kp)
457{
458 return -ENOSYS;
459}
460static inline int enable_kprobe(struct kprobe *kp)
461{
462 return -ENOSYS;
463}
David Brazdil0f672f62019-12-10 10:32:29 +0000464
465static inline bool within_kprobe_blacklist(unsigned long addr)
466{
467 return true;
468}
Olivier Deprez157378f2022-04-04 15:47:50 +0200469static inline int kprobe_get_kallsym(unsigned int symnum, unsigned long *value,
470 char *type, char *sym)
471{
472 return -ERANGE;
473}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000474#endif /* CONFIG_KPROBES */
475static inline int disable_kretprobe(struct kretprobe *rp)
476{
477 return disable_kprobe(&rp->kp);
478}
479static inline int enable_kretprobe(struct kretprobe *rp)
480{
481 return enable_kprobe(&rp->kp);
482}
483
484#ifndef CONFIG_KPROBES
485static inline bool is_kprobe_insn_slot(unsigned long addr)
486{
487 return false;
488}
489#endif
490#ifndef CONFIG_OPTPROBES
491static inline bool is_kprobe_optinsn_slot(unsigned long addr)
492{
493 return false;
494}
495#endif
496
David Brazdil0f672f62019-12-10 10:32:29 +0000497/* Returns true if kprobes handled the fault */
498static nokprobe_inline bool kprobe_page_fault(struct pt_regs *regs,
499 unsigned int trap)
500{
501 if (!kprobes_built_in())
502 return false;
503 if (user_mode(regs))
504 return false;
505 /*
506 * To be potentially processing a kprobe fault and to be allowed
507 * to call kprobe_running(), we have to be non-preemptible.
508 */
509 if (preemptible())
510 return false;
511 if (!kprobe_running())
512 return false;
513 return kprobe_fault_handler(regs, trap);
514}
515
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000516#endif /* _LINUX_KPROBES_H */