]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - include/linux/kprobes.h
memblock: fix memblock_phys_alloc() section mismatch error
[mirror_ubuntu-focal-kernel.git] / include / linux / kprobes.h
CommitLineData
1a59d1b8 1/* SPDX-License-Identifier: GPL-2.0-or-later */
1da177e4
LT
2#ifndef _LINUX_KPROBES_H
3#define _LINUX_KPROBES_H
4/*
5 * Kernel Probes (KProbes)
6 * include/linux/kprobes.h
7 *
1da177e4
LT
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.
b94cce92
HN
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.
1da177e4 18 */
7d134b2c 19#include <linux/compiler.h>
36dcd67a 20#include <linux/linkage.h>
1da177e4
LT
21#include <linux/list.h>
22#include <linux/notifier.h>
23#include <linux/smp.h>
187f1882 24#include <linux/bug.h>
e6584523 25#include <linux/percpu.h>
3516a460
AM
26#include <linux/spinlock.h>
27#include <linux/rcupdate.h>
7a7d1cf9 28#include <linux/mutex.h>
ae6aa16f 29#include <linux/ftrace.h>
7d134b2c 30#include <asm/kprobes.h>
b94cce92 31
00d7c05a 32#ifdef CONFIG_KPROBES
1da177e4 33
ea32c65c
PP
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
dc19835d 40#else /* CONFIG_KPROBES */
7d134b2c 41#include <asm-generic/kprobes.h>
dc19835d
MH
42typedef int kprobe_opcode_t;
43struct arch_specific_insn {
44 int dummy;
45};
dc19835d 46#endif /* CONFIG_KPROBES */
d0aaff97 47
1da177e4
LT
48struct kprobe;
49struct pt_regs;
b94cce92
HN
50struct kretprobe;
51struct kretprobe_instance;
1da177e4 52typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
1da177e4
LT
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);
b94cce92
HN
57typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,
58 struct pt_regs *);
59
1da177e4
LT
60struct kprobe {
61 struct hlist_node hlist;
62
64f562c6
AM
63 /* list of kprobes for multi-handler support */
64 struct list_head list;
65
ea32c65c
PP
66 /*count the number of times this probe was temporarily disarmed */
67 unsigned long nmissed;
68
1da177e4
LT
69 /* location of the probe point */
70 kprobe_opcode_t *addr;
71
3a872d89 72 /* Allow user to indicate symbol name of the probe point */
9b3af29b 73 const char *symbol_name;
3a872d89
AM
74
75 /* Offset into the symbol */
76 unsigned int offset;
77
1da177e4
LT
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
cc00e9cf
MH
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 */
1da177e4
LT
88 kprobe_fault_handler_t fault_handler;
89
1da177e4
LT
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;
e8386a0c 95
de5bd88d
MH
96 /*
97 * Indicates various status flags.
98 * Protected by kprobe_mutex after this kprobe is registered.
99 */
e8386a0c 100 u32 flags;
1da177e4
LT
101};
102
e8386a0c
MH
103/* Kprobe status flags */
104#define KPROBE_FLAG_GONE 1 /* breakpoint has already gone */
de5bd88d 105#define KPROBE_FLAG_DISABLED 2 /* probe is temporarily disabled */
afd66255
MH
106#define KPROBE_FLAG_OPTIMIZED 4 /*
107 * probe is really optimized.
108 * NOTE:
109 * this flag is only for optimized_kprobe.
110 */
ae6aa16f 111#define KPROBE_FLAG_FTRACE 8 /* probe is using ftrace */
e8386a0c 112
de5bd88d 113/* Has this kprobe gone ? */
e8386a0c
MH
114static inline int kprobe_gone(struct kprobe *p)
115{
116 return p->flags & KPROBE_FLAG_GONE;
117}
118
de5bd88d
MH
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}
afd66255
MH
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}
ae6aa16f
MH
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
b94cce92
HN
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;
f47cd9b5 150 kretprobe_handler_t entry_handler;
b94cce92
HN
151 int maxactive;
152 int nmissed;
f47cd9b5 153 size_t data_size;
b94cce92 154 struct hlist_head free_instances;
ec484608 155 raw_spinlock_t lock;
b94cce92
HN
156};
157
d0c736f3
MH
158#define KRETPROBE_MAX_DATA_SIZE 4096
159
b94cce92 160struct kretprobe_instance {
b94cce92
HN
161 struct hlist_node hlist;
162 struct kretprobe *rp;
802eae7c
RL
163 kprobe_opcode_t *ret_addr;
164 struct task_struct *task;
3ff9c075 165 void *fp;
f47cd9b5 166 char data[0];
b94cce92
HN
167};
168
f438d914
MH
169struct kretprobe_blackpoint {
170 const char *name;
171 void *addr;
172};
3d8d996e 173
376e2424
MH
174struct kprobe_blacklist_entry {
175 struct list_head list;
3d8d996e 176 unsigned long start_addr;
376e2424 177 unsigned long end_addr;
3d8d996e
SD
178};
179
dc19835d
MH
180#ifdef CONFIG_KPROBES
181DECLARE_PER_CPU(struct kprobe *, current_kprobe);
182DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
183
b1801812
IM
184/*
185 * For #ifdef avoidance:
186 */
187static inline int kprobes_built_in(void)
188{
189 return 1;
190}
191
dc19835d
MH
192#ifdef CONFIG_KRETPROBES
193extern void arch_prepare_kretprobe(struct kretprobe_instance *ri,
194 struct pt_regs *regs);
195extern int arch_trampoline_kprobe(struct kprobe *p);
196#else /* CONFIG_KRETPROBES */
197static inline void arch_prepare_kretprobe(struct kretprobe *rp,
198 struct pt_regs *regs)
199{
200}
201static inline int arch_trampoline_kprobe(struct kprobe *p)
202{
203 return 0;
204}
205#endif /* CONFIG_KRETPROBES */
206
f438d914
MH
207extern struct kretprobe_blackpoint kretprobe_blacklist[];
208
0f95b7fc
AM
209static inline void kretprobe_assert(struct kretprobe_instance *ri,
210 unsigned long orig_ret_address, unsigned long trampoline_address)
211{
212 if (!orig_ret_address || (orig_ret_address == trampoline_address)) {
213 printk("kretprobe BUG!: Processing kretprobe %p @ %p\n",
214 ri->rp, ri->rp->kp.addr);
215 BUG();
216 }
217}
218
8c1c9356
AM
219#ifdef CONFIG_KPROBES_SANITY_TEST
220extern int init_test_probes(void);
221#else
222static inline int init_test_probes(void)
223{
224 return 0;
225}
226#endif /* CONFIG_KPROBES_SANITY_TEST */
227
1da177e4 228extern int arch_prepare_kprobe(struct kprobe *p);
7e1048b1
RL
229extern void arch_arm_kprobe(struct kprobe *p);
230extern void arch_disarm_kprobe(struct kprobe *p);
6772926b 231extern int arch_init_kprobes(void);
1da177e4 232extern void show_registers(struct pt_regs *regs);
bf8d5c52 233extern void kprobes_inc_nmissed_count(struct kprobe *p);
be8f2743 234extern bool arch_within_kprobe_blacklist(unsigned long addr);
fb1a59fa 235extern int arch_populate_kprobe_blacklist(void);
659b957f 236extern bool arch_kprobe_on_func_entry(unsigned long offset);
4842b178 237extern int kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset);
1da177e4 238
e5779e8e 239extern bool within_kprobe_blacklist(unsigned long addr);
fb1a59fa
MH
240extern int kprobe_add_ksym_blacklist(unsigned long entry);
241extern int kprobe_add_area_blacklist(unsigned long start, unsigned long end);
e5779e8e 242
c802d64a
HC
243struct kprobe_insn_cache {
244 struct mutex mutex;
af96397d
HC
245 void *(*alloc)(void); /* allocate insn page */
246 void (*free)(void *); /* free insn page */
c802d64a
HC
247 struct list_head pages; /* list of kprobe_insn_page */
248 size_t insn_size; /* size of instruction slot */
249 int nr_garbage;
250};
251
5b485629 252#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
c802d64a
HC
253extern kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c);
254extern void __free_insn_slot(struct kprobe_insn_cache *c,
255 kprobe_opcode_t *slot, int dirty);
5b485629
MH
256/* sleep-less address checking routine */
257extern bool __is_insn_slot_addr(struct kprobe_insn_cache *c,
258 unsigned long addr);
c802d64a
HC
259
260#define DEFINE_INSN_CACHE_OPS(__name) \
261extern struct kprobe_insn_cache kprobe_##__name##_slots; \
262 \
263static inline kprobe_opcode_t *get_##__name##_slot(void) \
264{ \
265 return __get_insn_slot(&kprobe_##__name##_slots); \
266} \
267 \
268static inline void free_##__name##_slot(kprobe_opcode_t *slot, int dirty)\
269{ \
270 __free_insn_slot(&kprobe_##__name##_slots, slot, dirty); \
271} \
5b485629
MH
272 \
273static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
274{ \
275 return __is_insn_slot_addr(&kprobe_##__name##_slots, addr); \
276}
277#else /* __ARCH_WANT_KPROBES_INSN_SLOT */
278#define DEFINE_INSN_CACHE_OPS(__name) \
279static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
280{ \
281 return 0; \
282}
283#endif
c802d64a
HC
284
285DEFINE_INSN_CACHE_OPS(insn);
286
afd66255
MH
287#ifdef CONFIG_OPTPROBES
288/*
289 * Internal structure for direct jump optimized probe
290 */
291struct optimized_kprobe {
292 struct kprobe kp;
293 struct list_head list; /* list for optimizing queue */
294 struct arch_optimized_insn optinsn;
295};
296
297/* Architecture dependent functions for direct jump optimization */
298extern int arch_prepared_optinsn(struct arch_optimized_insn *optinsn);
299extern int arch_check_optimized_kprobe(struct optimized_kprobe *op);
cbf6ab52
MH
300extern int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
301 struct kprobe *orig);
afd66255 302extern void arch_remove_optimized_kprobe(struct optimized_kprobe *op);
cd7ebe22 303extern void arch_optimize_kprobes(struct list_head *oplist);
f984ba4e
MH
304extern void arch_unoptimize_kprobes(struct list_head *oplist,
305 struct list_head *done_list);
afd66255 306extern void arch_unoptimize_kprobe(struct optimized_kprobe *op);
afd66255
MH
307extern int arch_within_optimized_kprobe(struct optimized_kprobe *op,
308 unsigned long addr);
309
310extern void opt_pre_handler(struct kprobe *p, struct pt_regs *regs);
b2be84df 311
c802d64a
HC
312DEFINE_INSN_CACHE_OPS(optinsn);
313
b2be84df
MH
314#ifdef CONFIG_SYSCTL
315extern int sysctl_kprobes_optimization;
316extern int proc_kprobes_optimization_handler(struct ctl_table *table,
317 int write, void __user *buffer,
318 size_t *length, loff_t *ppos);
319#endif
30e7d894
TG
320extern void wait_for_kprobe_optimizer(void);
321#else
322static inline void wait_for_kprobe_optimizer(void) { }
afd66255 323#endif /* CONFIG_OPTPROBES */
e7dbfe34 324#ifdef CONFIG_KPROBES_ON_FTRACE
ae6aa16f 325extern void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
e5253896 326 struct ftrace_ops *ops, struct pt_regs *regs);
ae6aa16f
MH
327extern int arch_prepare_kprobe_ftrace(struct kprobe *p);
328#endif
329
f7f242ff 330int arch_check_ftrace_location(struct kprobe *p);
afd66255 331
d217d545 332/* Get the kprobe at this addr (if any) - called with preemption disabled */
1da177e4 333struct kprobe *get_kprobe(void *addr);
ef53d9c5
S
334void kretprobe_hash_lock(struct task_struct *tsk,
335 struct hlist_head **head, unsigned long *flags);
336void kretprobe_hash_unlock(struct task_struct *tsk, unsigned long *flags);
b94cce92 337struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk);
1da177e4 338
e6584523
AM
339/* kprobe_running() will just return the current_kprobe on this CPU */
340static inline struct kprobe *kprobe_running(void)
341{
b76834bc 342 return (__this_cpu_read(current_kprobe));
e6584523
AM
343}
344
345static inline void reset_current_kprobe(void)
346{
b76834bc 347 __this_cpu_write(current_kprobe, NULL);
e6584523
AM
348}
349
350static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
351{
bdffd893 352 return this_cpu_ptr(&kprobe_ctlblk);
e6584523
AM
353}
354
570a8f9e
JO
355extern struct kprobe kprobe_busy;
356void kprobe_busy_begin(void);
357void kprobe_busy_end(void);
358
290e3070 359kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset);
1da177e4
LT
360int register_kprobe(struct kprobe *p);
361void unregister_kprobe(struct kprobe *p);
9861668f
MH
362int register_kprobes(struct kprobe **kps, int num);
363void unregister_kprobes(struct kprobe **kps, int num);
3d7e3382 364unsigned long arch_deref_entry_point(void *);
1da177e4 365
b94cce92
HN
366int register_kretprobe(struct kretprobe *rp);
367void unregister_kretprobe(struct kretprobe *rp);
4a296e07
MH
368int register_kretprobes(struct kretprobe **rps, int num);
369void unregister_kretprobes(struct kretprobe **rps, int num);
b94cce92 370
b94cce92 371void kprobe_flush_task(struct task_struct *tk);
99219a3f 372void recycle_rp_inst(struct kretprobe_instance *ri, struct hlist_head *head);
8c1c9356 373
3192ce45
MH
374void kprobe_free_init_mem(void);
375
de5bd88d
MH
376int disable_kprobe(struct kprobe *kp);
377int enable_kprobe(struct kprobe *kp);
378
24851d24
FW
379void dump_kprobe(struct kprobe *kp);
380
ad3bc25a
BP
381void *alloc_insn_page(void);
382void free_insn_page(void *page);
383
b1801812 384#else /* !CONFIG_KPROBES: */
00d7c05a 385
b1801812
IM
386static inline int kprobes_built_in(void)
387{
388 return 0;
389}
390static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
391{
392 return 0;
393}
785656a4
AS
394static inline struct kprobe *get_kprobe(void *addr)
395{
396 return NULL;
397}
e6584523 398static inline struct kprobe *kprobe_running(void)
1da177e4 399{
e6584523 400 return NULL;
1da177e4
LT
401}
402static inline int register_kprobe(struct kprobe *p)
403{
404 return -ENOSYS;
405}
9861668f
MH
406static inline int register_kprobes(struct kprobe **kps, int num)
407{
408 return -ENOSYS;
409}
1da177e4
LT
410static inline void unregister_kprobe(struct kprobe *p)
411{
412}
9861668f
MH
413static inline void unregister_kprobes(struct kprobe **kps, int num)
414{
415}
b94cce92
HN
416static inline int register_kretprobe(struct kretprobe *rp)
417{
418 return -ENOSYS;
419}
4a296e07
MH
420static inline int register_kretprobes(struct kretprobe **rps, int num)
421{
422 return -ENOSYS;
423}
b94cce92
HN
424static inline void unregister_kretprobe(struct kretprobe *rp)
425{
426}
4a296e07
MH
427static inline void unregister_kretprobes(struct kretprobe **rps, int num)
428{
429}
b94cce92
HN
430static inline void kprobe_flush_task(struct task_struct *tk)
431{
432}
3192ce45
MH
433static inline void kprobe_free_init_mem(void)
434{
435}
de5bd88d
MH
436static inline int disable_kprobe(struct kprobe *kp)
437{
438 return -ENOSYS;
439}
440static inline int enable_kprobe(struct kprobe *kp)
441{
442 return -ENOSYS;
443}
fab94075
BP
444
445static inline bool within_kprobe_blacklist(unsigned long addr)
446{
447 return true;
448}
b1801812 449#endif /* CONFIG_KPROBES */
8f9b1528
MH
450static inline int disable_kretprobe(struct kretprobe *rp)
451{
452 return disable_kprobe(&rp->kp);
453}
454static inline int enable_kretprobe(struct kretprobe *rp)
455{
456 return enable_kprobe(&rp->kp);
457}
8f9b1528 458
5b485629
MH
459#ifndef CONFIG_KPROBES
460static inline bool is_kprobe_insn_slot(unsigned long addr)
461{
462 return false;
463}
464#endif
465#ifndef CONFIG_OPTPROBES
466static inline bool is_kprobe_optinsn_slot(unsigned long addr)
467{
468 return false;
469}
470#endif
471
b98cca44
AK
472/* Returns true if kprobes handled the fault */
473static nokprobe_inline bool kprobe_page_fault(struct pt_regs *regs,
474 unsigned int trap)
475{
476 if (!kprobes_built_in())
477 return false;
478 if (user_mode(regs))
479 return false;
480 /*
481 * To be potentially processing a kprobe fault and to be allowed
482 * to call kprobe_running(), we have to be non-preemptible.
483 */
484 if (preemptible())
485 return false;
486 if (!kprobe_running())
487 return false;
488 return kprobe_fault_handler(regs, trap);
489}
490
b1801812 491#endif /* _LINUX_KPROBES_H */