]>
Commit | Line | Data |
---|---|---|
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 |
42 | typedef int kprobe_opcode_t; |
43 | struct arch_specific_insn { | |
44 | int dummy; | |
45 | }; | |
dc19835d | 46 | #endif /* CONFIG_KPROBES */ |
d0aaff97 | 47 | |
1da177e4 LT |
48 | struct kprobe; |
49 | struct pt_regs; | |
b94cce92 HN |
50 | struct kretprobe; |
51 | struct kretprobe_instance; | |
1da177e4 | 52 | typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *); |
1da177e4 LT |
53 | typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *, |
54 | unsigned long flags); | |
55 | typedef int (*kprobe_fault_handler_t) (struct kprobe *, struct pt_regs *, | |
56 | int trapnr); | |
b94cce92 HN |
57 | typedef int (*kretprobe_handler_t) (struct kretprobe_instance *, |
58 | struct pt_regs *); | |
59 | ||
1da177e4 LT |
60 | struct 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 |
114 | static inline int kprobe_gone(struct kprobe *p) |
115 | { | |
116 | return p->flags & KPROBE_FLAG_GONE; | |
117 | } | |
118 | ||
de5bd88d MH |
119 | /* Is this kprobe disabled ? */ |
120 | static 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 ? */ | |
126 | static 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 ? */ | |
132 | static 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 | */ | |
147 | struct 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 | ||
158 | struct kretprobe_instance { | |
b94cce92 HN |
159 | struct hlist_node hlist; |
160 | struct kretprobe *rp; | |
802eae7c RL |
161 | kprobe_opcode_t *ret_addr; |
162 | struct task_struct *task; | |
3ff9c075 | 163 | void *fp; |
f47cd9b5 | 164 | char data[0]; |
b94cce92 HN |
165 | }; |
166 | ||
f438d914 MH |
167 | struct kretprobe_blackpoint { |
168 | const char *name; | |
169 | void *addr; | |
170 | }; | |
3d8d996e | 171 | |
376e2424 MH |
172 | struct kprobe_blacklist_entry { |
173 | struct list_head list; | |
3d8d996e | 174 | unsigned long start_addr; |
376e2424 | 175 | unsigned long end_addr; |
3d8d996e SD |
176 | }; |
177 | ||
dc19835d MH |
178 | #ifdef CONFIG_KPROBES |
179 | DECLARE_PER_CPU(struct kprobe *, current_kprobe); | |
180 | DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); | |
181 | ||
b1801812 IM |
182 | /* |
183 | * For #ifdef avoidance: | |
184 | */ | |
185 | static inline int kprobes_built_in(void) | |
186 | { | |
187 | return 1; | |
188 | } | |
189 | ||
dc19835d MH |
190 | #ifdef CONFIG_KRETPROBES |
191 | extern void arch_prepare_kretprobe(struct kretprobe_instance *ri, | |
192 | struct pt_regs *regs); | |
193 | extern int arch_trampoline_kprobe(struct kprobe *p); | |
194 | #else /* CONFIG_KRETPROBES */ | |
195 | static inline void arch_prepare_kretprobe(struct kretprobe *rp, | |
196 | struct pt_regs *regs) | |
197 | { | |
198 | } | |
199 | static inline int arch_trampoline_kprobe(struct kprobe *p) | |
200 | { | |
201 | return 0; | |
202 | } | |
203 | #endif /* CONFIG_KRETPROBES */ | |
204 | ||
f438d914 MH |
205 | extern struct kretprobe_blackpoint kretprobe_blacklist[]; |
206 | ||
0f95b7fc AM |
207 | static 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 | ||
8c1c9356 AM |
217 | #ifdef CONFIG_KPROBES_SANITY_TEST |
218 | extern int init_test_probes(void); | |
219 | #else | |
220 | static inline int init_test_probes(void) | |
221 | { | |
222 | return 0; | |
223 | } | |
224 | #endif /* CONFIG_KPROBES_SANITY_TEST */ | |
225 | ||
1da177e4 | 226 | extern int arch_prepare_kprobe(struct kprobe *p); |
7e1048b1 RL |
227 | extern void arch_arm_kprobe(struct kprobe *p); |
228 | extern void arch_disarm_kprobe(struct kprobe *p); | |
6772926b | 229 | extern int arch_init_kprobes(void); |
1da177e4 | 230 | extern void show_registers(struct pt_regs *regs); |
bf8d5c52 | 231 | extern void kprobes_inc_nmissed_count(struct kprobe *p); |
be8f2743 | 232 | extern bool arch_within_kprobe_blacklist(unsigned long addr); |
fb1a59fa | 233 | extern int arch_populate_kprobe_blacklist(void); |
659b957f NR |
234 | extern bool arch_kprobe_on_func_entry(unsigned long offset); |
235 | extern bool kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset); | |
1da177e4 | 236 | |
e5779e8e | 237 | extern bool within_kprobe_blacklist(unsigned long addr); |
fb1a59fa MH |
238 | extern int kprobe_add_ksym_blacklist(unsigned long entry); |
239 | extern int kprobe_add_area_blacklist(unsigned long start, unsigned long end); | |
e5779e8e | 240 | |
c802d64a HC |
241 | struct kprobe_insn_cache { |
242 | struct mutex mutex; | |
af96397d HC |
243 | void *(*alloc)(void); /* allocate insn page */ |
244 | void (*free)(void *); /* free insn page */ | |
c802d64a HC |
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 | ||
5b485629 | 250 | #ifdef __ARCH_WANT_KPROBES_INSN_SLOT |
c802d64a HC |
251 | extern kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c); |
252 | extern void __free_insn_slot(struct kprobe_insn_cache *c, | |
253 | kprobe_opcode_t *slot, int dirty); | |
5b485629 MH |
254 | /* sleep-less address checking routine */ |
255 | extern bool __is_insn_slot_addr(struct kprobe_insn_cache *c, | |
256 | unsigned long addr); | |
c802d64a HC |
257 | |
258 | #define DEFINE_INSN_CACHE_OPS(__name) \ | |
259 | extern struct kprobe_insn_cache kprobe_##__name##_slots; \ | |
260 | \ | |
261 | static inline kprobe_opcode_t *get_##__name##_slot(void) \ | |
262 | { \ | |
263 | return __get_insn_slot(&kprobe_##__name##_slots); \ | |
264 | } \ | |
265 | \ | |
266 | static inline void free_##__name##_slot(kprobe_opcode_t *slot, int dirty)\ | |
267 | { \ | |
268 | __free_insn_slot(&kprobe_##__name##_slots, slot, dirty); \ | |
269 | } \ | |
5b485629 MH |
270 | \ |
271 | static inline bool is_kprobe_##__name##_slot(unsigned long addr) \ | |
272 | { \ | |
273 | return __is_insn_slot_addr(&kprobe_##__name##_slots, addr); \ | |
274 | } | |
275 | #else /* __ARCH_WANT_KPROBES_INSN_SLOT */ | |
276 | #define DEFINE_INSN_CACHE_OPS(__name) \ | |
277 | static inline bool is_kprobe_##__name##_slot(unsigned long addr) \ | |
278 | { \ | |
279 | return 0; \ | |
280 | } | |
281 | #endif | |
c802d64a HC |
282 | |
283 | DEFINE_INSN_CACHE_OPS(insn); | |
284 | ||
afd66255 MH |
285 | #ifdef CONFIG_OPTPROBES |
286 | /* | |
287 | * Internal structure for direct jump optimized probe | |
288 | */ | |
289 | struct optimized_kprobe { | |
290 | struct kprobe kp; | |
291 | struct list_head list; /* list for optimizing queue */ | |
292 | struct arch_optimized_insn optinsn; | |
293 | }; | |
294 | ||
295 | /* Architecture dependent functions for direct jump optimization */ | |
296 | extern int arch_prepared_optinsn(struct arch_optimized_insn *optinsn); | |
297 | extern int arch_check_optimized_kprobe(struct optimized_kprobe *op); | |
cbf6ab52 MH |
298 | extern int arch_prepare_optimized_kprobe(struct optimized_kprobe *op, |
299 | struct kprobe *orig); | |
afd66255 | 300 | extern void arch_remove_optimized_kprobe(struct optimized_kprobe *op); |
cd7ebe22 | 301 | extern void arch_optimize_kprobes(struct list_head *oplist); |
f984ba4e MH |
302 | extern void arch_unoptimize_kprobes(struct list_head *oplist, |
303 | struct list_head *done_list); | |
afd66255 | 304 | extern void arch_unoptimize_kprobe(struct optimized_kprobe *op); |
afd66255 MH |
305 | extern int arch_within_optimized_kprobe(struct optimized_kprobe *op, |
306 | unsigned long addr); | |
307 | ||
308 | extern void opt_pre_handler(struct kprobe *p, struct pt_regs *regs); | |
b2be84df | 309 | |
c802d64a HC |
310 | DEFINE_INSN_CACHE_OPS(optinsn); |
311 | ||
b2be84df MH |
312 | #ifdef CONFIG_SYSCTL |
313 | extern int sysctl_kprobes_optimization; | |
314 | extern int proc_kprobes_optimization_handler(struct ctl_table *table, | |
32927393 | 315 | int write, void *buffer, |
b2be84df MH |
316 | size_t *length, loff_t *ppos); |
317 | #endif | |
30e7d894 TG |
318 | extern void wait_for_kprobe_optimizer(void); |
319 | #else | |
320 | static inline void wait_for_kprobe_optimizer(void) { } | |
afd66255 | 321 | #endif /* CONFIG_OPTPROBES */ |
e7dbfe34 | 322 | #ifdef CONFIG_KPROBES_ON_FTRACE |
ae6aa16f | 323 | extern void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip, |
e5253896 | 324 | struct ftrace_ops *ops, struct pt_regs *regs); |
ae6aa16f MH |
325 | extern int arch_prepare_kprobe_ftrace(struct kprobe *p); |
326 | #endif | |
327 | ||
f7f242ff | 328 | int arch_check_ftrace_location(struct kprobe *p); |
afd66255 | 329 | |
d217d545 | 330 | /* Get the kprobe at this addr (if any) - called with preemption disabled */ |
1da177e4 | 331 | struct kprobe *get_kprobe(void *addr); |
ef53d9c5 S |
332 | void kretprobe_hash_lock(struct task_struct *tsk, |
333 | struct hlist_head **head, unsigned long *flags); | |
334 | void kretprobe_hash_unlock(struct task_struct *tsk, unsigned long *flags); | |
b94cce92 | 335 | struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk); |
1da177e4 | 336 | |
e6584523 AM |
337 | /* kprobe_running() will just return the current_kprobe on this CPU */ |
338 | static inline struct kprobe *kprobe_running(void) | |
339 | { | |
b76834bc | 340 | return (__this_cpu_read(current_kprobe)); |
e6584523 AM |
341 | } |
342 | ||
343 | static inline void reset_current_kprobe(void) | |
344 | { | |
b76834bc | 345 | __this_cpu_write(current_kprobe, NULL); |
e6584523 AM |
346 | } |
347 | ||
348 | static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void) | |
349 | { | |
bdffd893 | 350 | return this_cpu_ptr(&kprobe_ctlblk); |
e6584523 AM |
351 | } |
352 | ||
290e3070 | 353 | kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset); |
1da177e4 LT |
354 | int register_kprobe(struct kprobe *p); |
355 | void unregister_kprobe(struct kprobe *p); | |
9861668f MH |
356 | int register_kprobes(struct kprobe **kps, int num); |
357 | void unregister_kprobes(struct kprobe **kps, int num); | |
3d7e3382 | 358 | unsigned long arch_deref_entry_point(void *); |
1da177e4 | 359 | |
b94cce92 HN |
360 | int register_kretprobe(struct kretprobe *rp); |
361 | void unregister_kretprobe(struct kretprobe *rp); | |
4a296e07 MH |
362 | int register_kretprobes(struct kretprobe **rps, int num); |
363 | void unregister_kretprobes(struct kretprobe **rps, int num); | |
b94cce92 | 364 | |
b94cce92 | 365 | void kprobe_flush_task(struct task_struct *tk); |
99219a3f | 366 | void recycle_rp_inst(struct kretprobe_instance *ri, struct hlist_head *head); |
8c1c9356 | 367 | |
de5bd88d MH |
368 | int disable_kprobe(struct kprobe *kp); |
369 | int enable_kprobe(struct kprobe *kp); | |
370 | ||
24851d24 FW |
371 | void dump_kprobe(struct kprobe *kp); |
372 | ||
ad3bc25a BP |
373 | void *alloc_insn_page(void); |
374 | void free_insn_page(void *page); | |
375 | ||
b1801812 | 376 | #else /* !CONFIG_KPROBES: */ |
00d7c05a | 377 | |
b1801812 IM |
378 | static inline int kprobes_built_in(void) |
379 | { | |
380 | return 0; | |
381 | } | |
382 | static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr) | |
383 | { | |
384 | return 0; | |
385 | } | |
785656a4 AS |
386 | static inline struct kprobe *get_kprobe(void *addr) |
387 | { | |
388 | return NULL; | |
389 | } | |
e6584523 | 390 | static inline struct kprobe *kprobe_running(void) |
1da177e4 | 391 | { |
e6584523 | 392 | return NULL; |
1da177e4 LT |
393 | } |
394 | static inline int register_kprobe(struct kprobe *p) | |
395 | { | |
396 | return -ENOSYS; | |
397 | } | |
9861668f MH |
398 | static inline int register_kprobes(struct kprobe **kps, int num) |
399 | { | |
400 | return -ENOSYS; | |
401 | } | |
1da177e4 LT |
402 | static inline void unregister_kprobe(struct kprobe *p) |
403 | { | |
404 | } | |
9861668f MH |
405 | static inline void unregister_kprobes(struct kprobe **kps, int num) |
406 | { | |
407 | } | |
b94cce92 HN |
408 | static inline int register_kretprobe(struct kretprobe *rp) |
409 | { | |
410 | return -ENOSYS; | |
411 | } | |
4a296e07 MH |
412 | static inline int register_kretprobes(struct kretprobe **rps, int num) |
413 | { | |
414 | return -ENOSYS; | |
415 | } | |
b94cce92 HN |
416 | static inline void unregister_kretprobe(struct kretprobe *rp) |
417 | { | |
418 | } | |
4a296e07 MH |
419 | static inline void unregister_kretprobes(struct kretprobe **rps, int num) |
420 | { | |
421 | } | |
b94cce92 HN |
422 | static inline void kprobe_flush_task(struct task_struct *tk) |
423 | { | |
424 | } | |
de5bd88d MH |
425 | static inline int disable_kprobe(struct kprobe *kp) |
426 | { | |
427 | return -ENOSYS; | |
428 | } | |
429 | static inline int enable_kprobe(struct kprobe *kp) | |
430 | { | |
431 | return -ENOSYS; | |
432 | } | |
fab94075 BP |
433 | |
434 | static inline bool within_kprobe_blacklist(unsigned long addr) | |
435 | { | |
436 | return true; | |
437 | } | |
b1801812 | 438 | #endif /* CONFIG_KPROBES */ |
8f9b1528 MH |
439 | static inline int disable_kretprobe(struct kretprobe *rp) |
440 | { | |
441 | return disable_kprobe(&rp->kp); | |
442 | } | |
443 | static inline int enable_kretprobe(struct kretprobe *rp) | |
444 | { | |
445 | return enable_kprobe(&rp->kp); | |
446 | } | |
8f9b1528 | 447 | |
5b485629 MH |
448 | #ifndef CONFIG_KPROBES |
449 | static inline bool is_kprobe_insn_slot(unsigned long addr) | |
450 | { | |
451 | return false; | |
452 | } | |
453 | #endif | |
454 | #ifndef CONFIG_OPTPROBES | |
455 | static inline bool is_kprobe_optinsn_slot(unsigned long addr) | |
456 | { | |
457 | return false; | |
458 | } | |
459 | #endif | |
460 | ||
b98cca44 AK |
461 | /* Returns true if kprobes handled the fault */ |
462 | static nokprobe_inline bool kprobe_page_fault(struct pt_regs *regs, | |
463 | unsigned int trap) | |
464 | { | |
465 | if (!kprobes_built_in()) | |
466 | return false; | |
467 | if (user_mode(regs)) | |
468 | return false; | |
469 | /* | |
470 | * To be potentially processing a kprobe fault and to be allowed | |
471 | * to call kprobe_running(), we have to be non-preemptible. | |
472 | */ | |
473 | if (preemptible()) | |
474 | return false; | |
475 | if (!kprobe_running()) | |
476 | return false; | |
477 | return kprobe_fault_handler(regs, trap); | |
478 | } | |
479 | ||
b1801812 | 480 | #endif /* _LINUX_KPROBES_H */ |