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 <linux/refcount.h>
31 #include <linux/freelist.h>
32 #include <asm/kprobes.h>
33
34 #ifdef CONFIG_KPROBES
35
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
41
42 #else /* CONFIG_KPROBES */
43 #include <asm-generic/kprobes.h>
44 typedef int kprobe_opcode_t;
45 struct arch_specific_insn {
46 int dummy;
47 };
48 #endif /* CONFIG_KPROBES */
49
50 struct kprobe;
51 struct pt_regs;
52 struct kretprobe;
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 *,
56 unsigned long flags);
57 typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,
58 struct pt_regs *);
59
60 struct 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 /* Saved opcode (which has been replaced with breakpoint) */
85 kprobe_opcode_t opcode;
86
87 /* copy of the original instruction */
88 struct arch_specific_insn ainsn;
89
90 /*
91 * Indicates various status flags.
92 * Protected by kprobe_mutex after this kprobe is registered.
93 */
94 u32 flags;
95 };
96
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.
102 * NOTE:
103 * this flag is only for optimized_kprobe.
104 */
105 #define KPROBE_FLAG_FTRACE 8 /* probe is using ftrace */
106
107 /* Has this kprobe gone ? */
kprobe_gone(struct kprobe * p)108 static inline int kprobe_gone(struct kprobe *p)
109 {
110 return p->flags & KPROBE_FLAG_GONE;
111 }
112
113 /* Is this kprobe disabled ? */
kprobe_disabled(struct kprobe * p)114 static inline int kprobe_disabled(struct kprobe *p)
115 {
116 return p->flags & (KPROBE_FLAG_DISABLED | KPROBE_FLAG_GONE);
117 }
118
119 /* Is this kprobe really running optimized path ? */
kprobe_optimized(struct kprobe * p)120 static inline int kprobe_optimized(struct kprobe *p)
121 {
122 return p->flags & KPROBE_FLAG_OPTIMIZED;
123 }
124
125 /* Is this kprobe uses ftrace ? */
kprobe_ftrace(struct kprobe * p)126 static inline int kprobe_ftrace(struct kprobe *p)
127 {
128 return p->flags & KPROBE_FLAG_FTRACE;
129 }
130
131 /*
132 * Function-return probe -
133 * Note:
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.
139 *
140 */
141 struct kretprobe_holder {
142 struct kretprobe *rp;
143 refcount_t ref;
144 };
145
146 struct kretprobe {
147 struct kprobe kp;
148 kretprobe_handler_t handler;
149 kretprobe_handler_t entry_handler;
150 int maxactive;
151 int nmissed;
152 size_t data_size;
153 struct freelist_head freelist;
154 struct kretprobe_holder *rph;
155 };
156
157 struct kretprobe_instance {
158 union {
159 struct freelist_node freelist;
160 struct rcu_head rcu;
161 };
162 struct llist_node llist;
163 struct kretprobe_holder *rph;
164 kprobe_opcode_t *ret_addr;
165 void *fp;
166 char data[];
167 };
168
169 struct kretprobe_blackpoint {
170 const char *name;
171 void *addr;
172 };
173
174 struct kprobe_blacklist_entry {
175 struct list_head list;
176 unsigned long start_addr;
177 unsigned long end_addr;
178 };
179
180 #ifdef CONFIG_KPROBES
181 DECLARE_PER_CPU(struct kprobe *, current_kprobe);
182 DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
183
184 /*
185 * For #ifdef avoidance:
186 */
kprobes_built_in(void)187 static inline int kprobes_built_in(void)
188 {
189 return 1;
190 }
191
192 extern void kprobe_busy_begin(void);
193 extern void kprobe_busy_end(void);
194
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);
199
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);
204
205 static nokprobe_inline
kretprobe_trampoline_handler(struct pt_regs * regs,void * trampoline_address,void * frame_pointer)206 unsigned long kretprobe_trampoline_handler(struct pt_regs *regs,
207 void *trampoline_address,
208 void *frame_pointer)
209 {
210 unsigned long ret;
211 /*
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.
215 */
216 kprobe_busy_begin();
217 ret = __kretprobe_trampoline_handler(regs, trampoline_address, frame_pointer);
218 kprobe_busy_end();
219
220 return ret;
221 }
222
get_kretprobe(struct kretprobe_instance * ri)223 static nokprobe_inline struct kretprobe *get_kretprobe(struct kretprobe_instance *ri)
224 {
225 RCU_LOCKDEP_WARN(!rcu_read_lock_any_held(),
226 "Kretprobe is accessed from instance under preemptive context");
227
228 return READ_ONCE(ri->rph->rp);
229 }
230
231 #else /* CONFIG_KRETPROBES */
arch_prepare_kretprobe(struct kretprobe * rp,struct pt_regs * regs)232 static inline void arch_prepare_kretprobe(struct kretprobe *rp,
233 struct pt_regs *regs)
234 {
235 }
arch_trampoline_kprobe(struct kprobe * p)236 static inline int arch_trampoline_kprobe(struct kprobe *p)
237 {
238 return 0;
239 }
240 #endif /* CONFIG_KRETPROBES */
241
242 extern struct kretprobe_blackpoint kretprobe_blacklist[];
243
244 #ifdef CONFIG_KPROBES_SANITY_TEST
245 extern int init_test_probes(void);
246 #else
init_test_probes(void)247 static inline int init_test_probes(void)
248 {
249 return 0;
250 }
251 #endif /* CONFIG_KPROBES_SANITY_TEST */
252
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);
262
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);
266
267 struct kprobe_insn_cache {
268 struct mutex mutex;
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 */
274 int nr_garbage;
275 };
276
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,
283 unsigned long addr);
284
285 #define DEFINE_INSN_CACHE_OPS(__name) \
286 extern struct kprobe_insn_cache kprobe_##__name##_slots; \
287 \
288 static inline kprobe_opcode_t *get_##__name##_slot(void) \
289 { \
290 return __get_insn_slot(&kprobe_##__name##_slots); \
291 } \
292 \
293 static inline void free_##__name##_slot(kprobe_opcode_t *slot, int dirty)\
294 { \
295 __free_insn_slot(&kprobe_##__name##_slots, slot, dirty); \
296 } \
297 \
298 static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
299 { \
300 return __is_insn_slot_addr(&kprobe_##__name##_slots, addr); \
301 }
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) \
309 { \
310 return 0; \
311 }
312 #endif
313
314 DEFINE_INSN_CACHE_OPS(insn);
315
316 #ifdef CONFIG_OPTPROBES
317 /*
318 * Internal structure for direct jump optimized probe
319 */
320 struct optimized_kprobe {
321 struct kprobe kp;
322 struct list_head list; /* list for optimizing queue */
323 struct arch_optimized_insn optinsn;
324 };
325
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,
337 unsigned long addr);
338
339 extern void opt_pre_handler(struct kprobe *p, struct pt_regs *regs);
340
341 DEFINE_INSN_CACHE_OPS(optinsn);
342
343 #ifdef CONFIG_SYSCTL
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);
348 #endif
349 extern void wait_for_kprobe_optimizer(void);
350 #else
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);
357 #endif
358
359 int arch_check_ftrace_location(struct kprobe *p);
360
361 /* Get the kprobe at this addr (if any) - called with preemption disabled */
362 struct kprobe *get_kprobe(void *addr);
363
364 /* kprobe_running() will just return the current_kprobe on this CPU */
kprobe_running(void)365 static inline struct kprobe *kprobe_running(void)
366 {
367 return (__this_cpu_read(current_kprobe));
368 }
369
reset_current_kprobe(void)370 static inline void reset_current_kprobe(void)
371 {
372 __this_cpu_write(current_kprobe, NULL);
373 }
374
get_kprobe_ctlblk(void)375 static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
376 {
377 return this_cpu_ptr(&kprobe_ctlblk);
378 }
379
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 *);
386
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);
391
392 void kprobe_flush_task(struct task_struct *tk);
393
394 void kprobe_free_init_mem(void);
395
396 int disable_kprobe(struct kprobe *kp);
397 int enable_kprobe(struct kprobe *kp);
398
399 void dump_kprobe(struct kprobe *kp);
400
401 void *alloc_insn_page(void);
402
403 void *alloc_optinsn_page(void);
404 void free_optinsn_page(void *page);
405
406 int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
407 char *sym);
408
409 int arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
410 char *type, char *sym);
411 #else /* !CONFIG_KPROBES: */
412
kprobes_built_in(void)413 static inline int kprobes_built_in(void)
414 {
415 return 0;
416 }
kprobe_fault_handler(struct pt_regs * regs,int trapnr)417 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
418 {
419 return 0;
420 }
get_kprobe(void * addr)421 static inline struct kprobe *get_kprobe(void *addr)
422 {
423 return NULL;
424 }
kprobe_running(void)425 static inline struct kprobe *kprobe_running(void)
426 {
427 return NULL;
428 }
register_kprobe(struct kprobe * p)429 static inline int register_kprobe(struct kprobe *p)
430 {
431 return -ENOSYS;
432 }
register_kprobes(struct kprobe ** kps,int num)433 static inline int register_kprobes(struct kprobe **kps, int num)
434 {
435 return -ENOSYS;
436 }
unregister_kprobe(struct kprobe * p)437 static inline void unregister_kprobe(struct kprobe *p)
438 {
439 }
unregister_kprobes(struct kprobe ** kps,int num)440 static inline void unregister_kprobes(struct kprobe **kps, int num)
441 {
442 }
register_kretprobe(struct kretprobe * rp)443 static inline int register_kretprobe(struct kretprobe *rp)
444 {
445 return -ENOSYS;
446 }
register_kretprobes(struct kretprobe ** rps,int num)447 static inline int register_kretprobes(struct kretprobe **rps, int num)
448 {
449 return -ENOSYS;
450 }
unregister_kretprobe(struct kretprobe * rp)451 static inline void unregister_kretprobe(struct kretprobe *rp)
452 {
453 }
unregister_kretprobes(struct kretprobe ** rps,int num)454 static inline void unregister_kretprobes(struct kretprobe **rps, int num)
455 {
456 }
kprobe_flush_task(struct task_struct * tk)457 static inline void kprobe_flush_task(struct task_struct *tk)
458 {
459 }
kprobe_free_init_mem(void)460 static inline void kprobe_free_init_mem(void)
461 {
462 }
disable_kprobe(struct kprobe * kp)463 static inline int disable_kprobe(struct kprobe *kp)
464 {
465 return -ENOSYS;
466 }
enable_kprobe(struct kprobe * kp)467 static inline int enable_kprobe(struct kprobe *kp)
468 {
469 return -ENOSYS;
470 }
471
within_kprobe_blacklist(unsigned long addr)472 static inline bool within_kprobe_blacklist(unsigned long addr)
473 {
474 return true;
475 }
kprobe_get_kallsym(unsigned int symnum,unsigned long * value,char * type,char * sym)476 static inline int kprobe_get_kallsym(unsigned int symnum, unsigned long *value,
477 char *type, char *sym)
478 {
479 return -ERANGE;
480 }
481 #endif /* CONFIG_KPROBES */
disable_kretprobe(struct kretprobe * rp)482 static inline int disable_kretprobe(struct kretprobe *rp)
483 {
484 return disable_kprobe(&rp->kp);
485 }
enable_kretprobe(struct kretprobe * rp)486 static inline int enable_kretprobe(struct kretprobe *rp)
487 {
488 return enable_kprobe(&rp->kp);
489 }
490
491 #ifndef CONFIG_KPROBES
is_kprobe_insn_slot(unsigned long addr)492 static inline bool is_kprobe_insn_slot(unsigned long addr)
493 {
494 return false;
495 }
496 #endif
497 #ifndef CONFIG_OPTPROBES
is_kprobe_optinsn_slot(unsigned long addr)498 static inline bool is_kprobe_optinsn_slot(unsigned long addr)
499 {
500 return false;
501 }
502 #endif
503
504 /* Returns true if kprobes handled the fault */
kprobe_page_fault(struct pt_regs * regs,unsigned int trap)505 static nokprobe_inline bool kprobe_page_fault(struct pt_regs *regs,
506 unsigned int trap)
507 {
508 if (!kprobes_built_in())
509 return false;
510 if (user_mode(regs))
511 return false;
512 /*
513 * To be potentially processing a kprobe fault and to be allowed
514 * to call kprobe_running(), we have to be non-preemptible.
515 */
516 if (preemptible())
517 return false;
518 if (!kprobe_running())
519 return false;
520 return kprobe_fault_handler(regs, trap);
521 }
522
523 #endif /* _LINUX_KPROBES_H */
524