xref: /linux/include/linux/kprobes.h (revision c1aac62f36c1e37ee81c9e09ee9ee733eef05dcb)
1 #ifndef _LINUX_KPROBES_H
2 #define _LINUX_KPROBES_H
3 /*
4  *  Kernel Probes (KProbes)
5  *  include/linux/kprobes.h
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  *
21  * Copyright (C) IBM Corporation, 2002, 2004
22  *
23  * 2002-Oct	Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
24  *		Probes initial implementation ( includes suggestions from
25  *		Rusty Russell).
26  * 2004-July	Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
27  *		interface to access function arguments.
28  * 2005-May	Hien Nguyen <hien@us.ibm.com> and Jim Keniston
29  *		<jkenisto@us.ibm.com>  and Prasanna S Panchamukhi
30  *		<prasanna@in.ibm.com> added function-return probes.
31  */
32 #include <linux/compiler.h>	/* for __kprobes */
33 #include <linux/linkage.h>
34 #include <linux/list.h>
35 #include <linux/notifier.h>
36 #include <linux/smp.h>
37 #include <linux/bug.h>
38 #include <linux/percpu.h>
39 #include <linux/spinlock.h>
40 #include <linux/rcupdate.h>
41 #include <linux/mutex.h>
42 #include <linux/ftrace.h>
43 
44 #ifdef CONFIG_KPROBES
45 #include <asm/kprobes.h>
46 
47 /* kprobe_status settings */
48 #define KPROBE_HIT_ACTIVE	0x00000001
49 #define KPROBE_HIT_SS		0x00000002
50 #define KPROBE_REENTER		0x00000004
51 #define KPROBE_HIT_SSDONE	0x00000008
52 
53 #else /* CONFIG_KPROBES */
54 typedef int kprobe_opcode_t;
55 struct arch_specific_insn {
56 	int dummy;
57 };
58 #endif /* CONFIG_KPROBES */
59 
60 struct kprobe;
61 struct pt_regs;
62 struct kretprobe;
63 struct kretprobe_instance;
64 typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
65 typedef int (*kprobe_break_handler_t) (struct kprobe *, struct pt_regs *);
66 typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *,
67 				       unsigned long flags);
68 typedef int (*kprobe_fault_handler_t) (struct kprobe *, struct pt_regs *,
69 				       int trapnr);
70 typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,
71 				    struct pt_regs *);
72 
73 struct kprobe {
74 	struct hlist_node hlist;
75 
76 	/* list of kprobes for multi-handler support */
77 	struct list_head list;
78 
79 	/*count the number of times this probe was temporarily disarmed */
80 	unsigned long nmissed;
81 
82 	/* location of the probe point */
83 	kprobe_opcode_t *addr;
84 
85 	/* Allow user to indicate symbol name of the probe point */
86 	const char *symbol_name;
87 
88 	/* Offset into the symbol */
89 	unsigned int offset;
90 
91 	/* Called before addr is executed. */
92 	kprobe_pre_handler_t pre_handler;
93 
94 	/* Called after addr is executed, unless... */
95 	kprobe_post_handler_t post_handler;
96 
97 	/*
98 	 * ... called if executing addr causes a fault (eg. page fault).
99 	 * Return 1 if it handled fault, otherwise kernel will see it.
100 	 */
101 	kprobe_fault_handler_t fault_handler;
102 
103 	/*
104 	 * ... called if breakpoint trap occurs in probe handler.
105 	 * Return 1 if it handled break, otherwise kernel will see it.
106 	 */
107 	kprobe_break_handler_t break_handler;
108 
109 	/* Saved opcode (which has been replaced with breakpoint) */
110 	kprobe_opcode_t opcode;
111 
112 	/* copy of the original instruction */
113 	struct arch_specific_insn ainsn;
114 
115 	/*
116 	 * Indicates various status flags.
117 	 * Protected by kprobe_mutex after this kprobe is registered.
118 	 */
119 	u32 flags;
120 };
121 
122 /* Kprobe status flags */
123 #define KPROBE_FLAG_GONE	1 /* breakpoint has already gone */
124 #define KPROBE_FLAG_DISABLED	2 /* probe is temporarily disabled */
125 #define KPROBE_FLAG_OPTIMIZED	4 /*
126 				   * probe is really optimized.
127 				   * NOTE:
128 				   * this flag is only for optimized_kprobe.
129 				   */
130 #define KPROBE_FLAG_FTRACE	8 /* probe is using ftrace */
131 
132 /* Has this kprobe gone ? */
133 static inline int kprobe_gone(struct kprobe *p)
134 {
135 	return p->flags & KPROBE_FLAG_GONE;
136 }
137 
138 /* Is this kprobe disabled ? */
139 static inline int kprobe_disabled(struct kprobe *p)
140 {
141 	return p->flags & (KPROBE_FLAG_DISABLED | KPROBE_FLAG_GONE);
142 }
143 
144 /* Is this kprobe really running optimized path ? */
145 static inline int kprobe_optimized(struct kprobe *p)
146 {
147 	return p->flags & KPROBE_FLAG_OPTIMIZED;
148 }
149 
150 /* Is this kprobe uses ftrace ? */
151 static inline int kprobe_ftrace(struct kprobe *p)
152 {
153 	return p->flags & KPROBE_FLAG_FTRACE;
154 }
155 
156 /*
157  * Special probe type that uses setjmp-longjmp type tricks to resume
158  * execution at a specified entry with a matching prototype corresponding
159  * to the probed function - a trick to enable arguments to become
160  * accessible seamlessly by probe handling logic.
161  * Note:
162  * Because of the way compilers allocate stack space for local variables
163  * etc upfront, regardless of sub-scopes within a function, this mirroring
164  * principle currently works only for probes placed on function entry points.
165  */
166 struct jprobe {
167 	struct kprobe kp;
168 	void *entry;	/* probe handling code to jump to */
169 };
170 
171 /* For backward compatibility with old code using JPROBE_ENTRY() */
172 #define JPROBE_ENTRY(handler)	(handler)
173 
174 /*
175  * Function-return probe -
176  * Note:
177  * User needs to provide a handler function, and initialize maxactive.
178  * maxactive - The maximum number of instances of the probed function that
179  * can be active concurrently.
180  * nmissed - tracks the number of times the probed function's return was
181  * ignored, due to maxactive being too low.
182  *
183  */
184 struct kretprobe {
185 	struct kprobe kp;
186 	kretprobe_handler_t handler;
187 	kretprobe_handler_t entry_handler;
188 	int maxactive;
189 	int nmissed;
190 	size_t data_size;
191 	struct hlist_head free_instances;
192 	raw_spinlock_t lock;
193 };
194 
195 struct kretprobe_instance {
196 	struct hlist_node hlist;
197 	struct kretprobe *rp;
198 	kprobe_opcode_t *ret_addr;
199 	struct task_struct *task;
200 	char data[0];
201 };
202 
203 struct kretprobe_blackpoint {
204 	const char *name;
205 	void *addr;
206 };
207 
208 struct kprobe_blacklist_entry {
209 	struct list_head list;
210 	unsigned long start_addr;
211 	unsigned long end_addr;
212 };
213 
214 #ifdef CONFIG_KPROBES
215 DECLARE_PER_CPU(struct kprobe *, current_kprobe);
216 DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
217 
218 /*
219  * For #ifdef avoidance:
220  */
221 static inline int kprobes_built_in(void)
222 {
223 	return 1;
224 }
225 
226 #ifdef CONFIG_KRETPROBES
227 extern void arch_prepare_kretprobe(struct kretprobe_instance *ri,
228 				   struct pt_regs *regs);
229 extern int arch_trampoline_kprobe(struct kprobe *p);
230 #else /* CONFIG_KRETPROBES */
231 static inline void arch_prepare_kretprobe(struct kretprobe *rp,
232 					struct pt_regs *regs)
233 {
234 }
235 static inline int arch_trampoline_kprobe(struct kprobe *p)
236 {
237 	return 0;
238 }
239 #endif /* CONFIG_KRETPROBES */
240 
241 extern struct kretprobe_blackpoint kretprobe_blacklist[];
242 
243 static inline void kretprobe_assert(struct kretprobe_instance *ri,
244 	unsigned long orig_ret_address, unsigned long trampoline_address)
245 {
246 	if (!orig_ret_address || (orig_ret_address == trampoline_address)) {
247 		printk("kretprobe BUG!: Processing kretprobe %p @ %p\n",
248 				ri->rp, ri->rp->kp.addr);
249 		BUG();
250 	}
251 }
252 
253 #ifdef CONFIG_KPROBES_SANITY_TEST
254 extern int init_test_probes(void);
255 #else
256 static inline int init_test_probes(void)
257 {
258 	return 0;
259 }
260 #endif /* CONFIG_KPROBES_SANITY_TEST */
261 
262 extern int arch_prepare_kprobe(struct kprobe *p);
263 extern void arch_arm_kprobe(struct kprobe *p);
264 extern void arch_disarm_kprobe(struct kprobe *p);
265 extern int arch_init_kprobes(void);
266 extern void show_registers(struct pt_regs *regs);
267 extern void kprobes_inc_nmissed_count(struct kprobe *p);
268 extern bool arch_within_kprobe_blacklist(unsigned long addr);
269 
270 extern bool within_kprobe_blacklist(unsigned long addr);
271 
272 struct kprobe_insn_cache {
273 	struct mutex mutex;
274 	void *(*alloc)(void);	/* allocate insn page */
275 	void (*free)(void *);	/* free insn page */
276 	struct list_head pages; /* list of kprobe_insn_page */
277 	size_t insn_size;	/* size of instruction slot */
278 	int nr_garbage;
279 };
280 
281 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
282 extern kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c);
283 extern void __free_insn_slot(struct kprobe_insn_cache *c,
284 			     kprobe_opcode_t *slot, int dirty);
285 /* sleep-less address checking routine  */
286 extern bool __is_insn_slot_addr(struct kprobe_insn_cache *c,
287 				unsigned long addr);
288 
289 #define DEFINE_INSN_CACHE_OPS(__name)					\
290 extern struct kprobe_insn_cache kprobe_##__name##_slots;		\
291 									\
292 static inline kprobe_opcode_t *get_##__name##_slot(void)		\
293 {									\
294 	return __get_insn_slot(&kprobe_##__name##_slots);		\
295 }									\
296 									\
297 static inline void free_##__name##_slot(kprobe_opcode_t *slot, int dirty)\
298 {									\
299 	__free_insn_slot(&kprobe_##__name##_slots, slot, dirty);	\
300 }									\
301 									\
302 static inline bool is_kprobe_##__name##_slot(unsigned long addr)	\
303 {									\
304 	return __is_insn_slot_addr(&kprobe_##__name##_slots, addr);	\
305 }
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 __user *buffer,
347 					     size_t *length, loff_t *ppos);
348 #endif
349 #endif /* CONFIG_OPTPROBES */
350 #ifdef CONFIG_KPROBES_ON_FTRACE
351 extern void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
352 				  struct ftrace_ops *ops, struct pt_regs *regs);
353 extern int arch_prepare_kprobe_ftrace(struct kprobe *p);
354 #endif
355 
356 int arch_check_ftrace_location(struct kprobe *p);
357 
358 /* Get the kprobe at this addr (if any) - called with preemption disabled */
359 struct kprobe *get_kprobe(void *addr);
360 void kretprobe_hash_lock(struct task_struct *tsk,
361 			 struct hlist_head **head, unsigned long *flags);
362 void kretprobe_hash_unlock(struct task_struct *tsk, unsigned long *flags);
363 struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk);
364 
365 /* kprobe_running() will just return the current_kprobe on this CPU */
366 static inline struct kprobe *kprobe_running(void)
367 {
368 	return (__this_cpu_read(current_kprobe));
369 }
370 
371 static inline void reset_current_kprobe(void)
372 {
373 	__this_cpu_write(current_kprobe, NULL);
374 }
375 
376 static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
377 {
378 	return this_cpu_ptr(&kprobe_ctlblk);
379 }
380 
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 int setjmp_pre_handler(struct kprobe *, struct pt_regs *);
386 int longjmp_break_handler(struct kprobe *, struct pt_regs *);
387 int register_jprobe(struct jprobe *p);
388 void unregister_jprobe(struct jprobe *p);
389 int register_jprobes(struct jprobe **jps, int num);
390 void unregister_jprobes(struct jprobe **jps, int num);
391 void jprobe_return(void);
392 unsigned long arch_deref_entry_point(void *);
393 
394 int register_kretprobe(struct kretprobe *rp);
395 void unregister_kretprobe(struct kretprobe *rp);
396 int register_kretprobes(struct kretprobe **rps, int num);
397 void unregister_kretprobes(struct kretprobe **rps, int num);
398 
399 void kprobe_flush_task(struct task_struct *tk);
400 void recycle_rp_inst(struct kretprobe_instance *ri, struct hlist_head *head);
401 
402 int disable_kprobe(struct kprobe *kp);
403 int enable_kprobe(struct kprobe *kp);
404 
405 void dump_kprobe(struct kprobe *kp);
406 
407 #else /* !CONFIG_KPROBES: */
408 
409 static inline int kprobes_built_in(void)
410 {
411 	return 0;
412 }
413 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
414 {
415 	return 0;
416 }
417 static inline struct kprobe *get_kprobe(void *addr)
418 {
419 	return NULL;
420 }
421 static inline struct kprobe *kprobe_running(void)
422 {
423 	return NULL;
424 }
425 static inline int register_kprobe(struct kprobe *p)
426 {
427 	return -ENOSYS;
428 }
429 static inline int register_kprobes(struct kprobe **kps, int num)
430 {
431 	return -ENOSYS;
432 }
433 static inline void unregister_kprobe(struct kprobe *p)
434 {
435 }
436 static inline void unregister_kprobes(struct kprobe **kps, int num)
437 {
438 }
439 static inline int register_jprobe(struct jprobe *p)
440 {
441 	return -ENOSYS;
442 }
443 static inline int register_jprobes(struct jprobe **jps, int num)
444 {
445 	return -ENOSYS;
446 }
447 static inline void unregister_jprobe(struct jprobe *p)
448 {
449 }
450 static inline void unregister_jprobes(struct jprobe **jps, int num)
451 {
452 }
453 static inline void jprobe_return(void)
454 {
455 }
456 static inline int register_kretprobe(struct kretprobe *rp)
457 {
458 	return -ENOSYS;
459 }
460 static inline int register_kretprobes(struct kretprobe **rps, int num)
461 {
462 	return -ENOSYS;
463 }
464 static inline void unregister_kretprobe(struct kretprobe *rp)
465 {
466 }
467 static inline void unregister_kretprobes(struct kretprobe **rps, int num)
468 {
469 }
470 static inline void kprobe_flush_task(struct task_struct *tk)
471 {
472 }
473 static inline int disable_kprobe(struct kprobe *kp)
474 {
475 	return -ENOSYS;
476 }
477 static inline int enable_kprobe(struct kprobe *kp)
478 {
479 	return -ENOSYS;
480 }
481 #endif /* CONFIG_KPROBES */
482 static inline int disable_kretprobe(struct kretprobe *rp)
483 {
484 	return disable_kprobe(&rp->kp);
485 }
486 static inline int enable_kretprobe(struct kretprobe *rp)
487 {
488 	return enable_kprobe(&rp->kp);
489 }
490 static inline int disable_jprobe(struct jprobe *jp)
491 {
492 	return disable_kprobe(&jp->kp);
493 }
494 static inline int enable_jprobe(struct jprobe *jp)
495 {
496 	return enable_kprobe(&jp->kp);
497 }
498 
499 #ifndef CONFIG_KPROBES
500 static inline bool is_kprobe_insn_slot(unsigned long addr)
501 {
502 	return false;
503 }
504 #endif
505 #ifndef CONFIG_OPTPROBES
506 static inline bool is_kprobe_optinsn_slot(unsigned long addr)
507 {
508 	return false;
509 }
510 #endif
511 
512 #ifdef CONFIG_KPROBES
513 /*
514  * Blacklist ganerating macro. Specify functions which is not probed
515  * by using this macro.
516  */
517 #define __NOKPROBE_SYMBOL(fname)			\
518 static unsigned long __used				\
519 	__attribute__((section("_kprobe_blacklist")))	\
520 	_kbl_addr_##fname = (unsigned long)fname;
521 #define NOKPROBE_SYMBOL(fname)	__NOKPROBE_SYMBOL(fname)
522 #else
523 #define NOKPROBE_SYMBOL(fname)
524 #endif
525 
526 #endif /* _LINUX_KPROBES_H */
527