kprobes.h 12.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#ifndef _LINUX_KPROBES_H
#define _LINUX_KPROBES_H
/*
 *  Kernel Probes (KProbes)
 *  include/linux/kprobes.h
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Copyright (C) IBM Corporation, 2002, 2004
 *
 * 2002-Oct	Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
 *		Probes initial implementation ( includes suggestions from
 *		Rusty Russell).
 * 2004-July	Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
 *		interface to access function arguments.
28 29 30
 * 2005-May	Hien Nguyen <hien@us.ibm.com> and Jim Keniston
 *		<jkenisto@us.ibm.com>  and Prasanna S Panchamukhi
 *		<prasanna@in.ibm.com> added function-return probes.
L
Linus Torvalds 已提交
31
 */
32
#include <linux/compiler.h>
33
#include <linux/linkage.h>
L
Linus Torvalds 已提交
34 35 36
#include <linux/list.h>
#include <linux/notifier.h>
#include <linux/smp.h>
37
#include <linux/bug.h>
38
#include <linux/percpu.h>
39 40
#include <linux/spinlock.h>
#include <linux/rcupdate.h>
I
Ingo Molnar 已提交
41
#include <linux/mutex.h>
42
#include <linux/ftrace.h>
43
#include <asm/kprobes.h>
44

45
#ifdef CONFIG_KPROBES
L
Linus Torvalds 已提交
46

47 48 49 50 51 52
/* kprobe_status settings */
#define KPROBE_HIT_ACTIVE	0x00000001
#define KPROBE_HIT_SS		0x00000002
#define KPROBE_REENTER		0x00000004
#define KPROBE_HIT_SSDONE	0x00000008

53
#else /* CONFIG_KPROBES */
54
#include <asm-generic/kprobes.h>
55 56 57 58 59
typedef int kprobe_opcode_t;
struct arch_specific_insn {
	int dummy;
};
#endif /* CONFIG_KPROBES */
60

L
Linus Torvalds 已提交
61 62
struct kprobe;
struct pt_regs;
63 64
struct kretprobe;
struct kretprobe_instance;
L
Linus Torvalds 已提交
65 66 67 68 69
typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *,
				       unsigned long flags);
typedef int (*kprobe_fault_handler_t) (struct kprobe *, struct pt_regs *,
				       int trapnr);
70 71 72
typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,
				    struct pt_regs *);

L
Linus Torvalds 已提交
73 74 75
struct kprobe {
	struct hlist_node hlist;

76 77 78
	/* list of kprobes for multi-handler support */
	struct list_head list;

79 80 81
	/*count the number of times this probe was temporarily disarmed */
	unsigned long nmissed;

L
Linus Torvalds 已提交
82 83 84
	/* location of the probe point */
	kprobe_opcode_t *addr;

85
	/* Allow user to indicate symbol name of the probe point */
86
	const char *symbol_name;
87 88 89 90

	/* Offset into the symbol */
	unsigned int offset;

L
Linus Torvalds 已提交
91 92 93 94 95 96
	/* Called before addr is executed. */
	kprobe_pre_handler_t pre_handler;

	/* Called after addr is executed, unless... */
	kprobe_post_handler_t post_handler;

97 98 99 100
	/*
	 * ... called if executing addr causes a fault (eg. page fault).
	 * Return 1 if it handled fault, otherwise kernel will see it.
	 */
L
Linus Torvalds 已提交
101 102 103 104 105 106 107
	kprobe_fault_handler_t fault_handler;

	/* Saved opcode (which has been replaced with breakpoint) */
	kprobe_opcode_t opcode;

	/* copy of the original instruction */
	struct arch_specific_insn ainsn;
108

109 110 111 112
	/*
	 * Indicates various status flags.
	 * Protected by kprobe_mutex after this kprobe is registered.
	 */
113
	u32 flags;
L
Linus Torvalds 已提交
114 115
};

116 117
/* Kprobe status flags */
#define KPROBE_FLAG_GONE	1 /* breakpoint has already gone */
118
#define KPROBE_FLAG_DISABLED	2 /* probe is temporarily disabled */
119 120 121 122 123
#define KPROBE_FLAG_OPTIMIZED	4 /*
				   * probe is really optimized.
				   * NOTE:
				   * this flag is only for optimized_kprobe.
				   */
124
#define KPROBE_FLAG_FTRACE	8 /* probe is using ftrace */
125

126
/* Has this kprobe gone ? */
127 128 129 130 131
static inline int kprobe_gone(struct kprobe *p)
{
	return p->flags & KPROBE_FLAG_GONE;
}

132 133 134 135 136
/* Is this kprobe disabled ? */
static inline int kprobe_disabled(struct kprobe *p)
{
	return p->flags & (KPROBE_FLAG_DISABLED | KPROBE_FLAG_GONE);
}
137 138 139 140 141 142

/* Is this kprobe really running optimized path ? */
static inline int kprobe_optimized(struct kprobe *p)
{
	return p->flags & KPROBE_FLAG_OPTIMIZED;
}
143 144 145 146 147 148 149

/* Is this kprobe uses ftrace ? */
static inline int kprobe_ftrace(struct kprobe *p)
{
	return p->flags & KPROBE_FLAG_FTRACE;
}

150 151 152 153 154 155 156 157 158 159 160 161 162
/*
 * Function-return probe -
 * Note:
 * User needs to provide a handler function, and initialize maxactive.
 * maxactive - The maximum number of instances of the probed function that
 * can be active concurrently.
 * nmissed - tracks the number of times the probed function's return was
 * ignored, due to maxactive being too low.
 *
 */
struct kretprobe {
	struct kprobe kp;
	kretprobe_handler_t handler;
163
	kretprobe_handler_t entry_handler;
164 165
	int maxactive;
	int nmissed;
166
	size_t data_size;
167
	struct hlist_head free_instances;
168
	raw_spinlock_t lock;
169 170 171 172 173
};

struct kretprobe_instance {
	struct hlist_node hlist;
	struct kretprobe *rp;
174 175
	kprobe_opcode_t *ret_addr;
	struct task_struct *task;
176
	void *fp;
177
	char data[0];
178 179
};

180 181 182 183
struct kretprobe_blackpoint {
	const char *name;
	void *addr;
};
184

185 186
struct kprobe_blacklist_entry {
	struct list_head list;
187
	unsigned long start_addr;
188
	unsigned long end_addr;
189 190
};

191 192 193 194
#ifdef CONFIG_KPROBES
DECLARE_PER_CPU(struct kprobe *, current_kprobe);
DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);

195 196 197 198 199 200 201 202
/*
 * For #ifdef avoidance:
 */
static inline int kprobes_built_in(void)
{
	return 1;
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
#ifdef CONFIG_KRETPROBES
extern void arch_prepare_kretprobe(struct kretprobe_instance *ri,
				   struct pt_regs *regs);
extern int arch_trampoline_kprobe(struct kprobe *p);
#else /* CONFIG_KRETPROBES */
static inline void arch_prepare_kretprobe(struct kretprobe *rp,
					struct pt_regs *regs)
{
}
static inline int arch_trampoline_kprobe(struct kprobe *p)
{
	return 0;
}
#endif /* CONFIG_KRETPROBES */

218 219
extern struct kretprobe_blackpoint kretprobe_blacklist[];

220 221 222 223 224 225 226 227 228 229
static inline void kretprobe_assert(struct kretprobe_instance *ri,
	unsigned long orig_ret_address, unsigned long trampoline_address)
{
	if (!orig_ret_address || (orig_ret_address == trampoline_address)) {
		printk("kretprobe BUG!: Processing kretprobe %p @ %p\n",
				ri->rp, ri->rp->kp.addr);
		BUG();
	}
}

230 231 232 233 234 235 236 237 238
#ifdef CONFIG_KPROBES_SANITY_TEST
extern int init_test_probes(void);
#else
static inline int init_test_probes(void)
{
	return 0;
}
#endif /* CONFIG_KPROBES_SANITY_TEST */

L
Linus Torvalds 已提交
239
extern int arch_prepare_kprobe(struct kprobe *p);
240 241
extern void arch_arm_kprobe(struct kprobe *p);
extern void arch_disarm_kprobe(struct kprobe *p);
242
extern int arch_init_kprobes(void);
L
Linus Torvalds 已提交
243
extern void show_registers(struct pt_regs *regs);
244
extern void kprobes_inc_nmissed_count(struct kprobe *p);
245
extern bool arch_within_kprobe_blacklist(unsigned long addr);
246 247
extern bool arch_kprobe_on_func_entry(unsigned long offset);
extern bool kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset);
L
Linus Torvalds 已提交
248

249 250
extern bool within_kprobe_blacklist(unsigned long addr);

H
Heiko Carstens 已提交
251 252
struct kprobe_insn_cache {
	struct mutex mutex;
253 254
	void *(*alloc)(void);	/* allocate insn page */
	void (*free)(void *);	/* free insn page */
H
Heiko Carstens 已提交
255 256 257 258 259
	struct list_head pages; /* list of kprobe_insn_page */
	size_t insn_size;	/* size of instruction slot */
	int nr_garbage;
};

260
#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
H
Heiko Carstens 已提交
261 262 263
extern kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c);
extern void __free_insn_slot(struct kprobe_insn_cache *c,
			     kprobe_opcode_t *slot, int dirty);
264 265 266
/* sleep-less address checking routine  */
extern bool __is_insn_slot_addr(struct kprobe_insn_cache *c,
				unsigned long addr);
H
Heiko Carstens 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279

#define DEFINE_INSN_CACHE_OPS(__name)					\
extern struct kprobe_insn_cache kprobe_##__name##_slots;		\
									\
static inline kprobe_opcode_t *get_##__name##_slot(void)		\
{									\
	return __get_insn_slot(&kprobe_##__name##_slots);		\
}									\
									\
static inline void free_##__name##_slot(kprobe_opcode_t *slot, int dirty)\
{									\
	__free_insn_slot(&kprobe_##__name##_slots, slot, dirty);	\
}									\
280 281 282 283 284 285 286 287 288 289 290 291
									\
static inline bool is_kprobe_##__name##_slot(unsigned long addr)	\
{									\
	return __is_insn_slot_addr(&kprobe_##__name##_slots, addr);	\
}
#else /* __ARCH_WANT_KPROBES_INSN_SLOT */
#define DEFINE_INSN_CACHE_OPS(__name)					\
static inline bool is_kprobe_##__name##_slot(unsigned long addr)	\
{									\
	return 0;							\
}
#endif
H
Heiko Carstens 已提交
292 293 294

DEFINE_INSN_CACHE_OPS(insn);

295 296 297 298 299 300 301 302 303 304 305 306 307
#ifdef CONFIG_OPTPROBES
/*
 * Internal structure for direct jump optimized probe
 */
struct optimized_kprobe {
	struct kprobe kp;
	struct list_head list;	/* list for optimizing queue */
	struct arch_optimized_insn optinsn;
};

/* Architecture dependent functions for direct jump optimization */
extern int arch_prepared_optinsn(struct arch_optimized_insn *optinsn);
extern int arch_check_optimized_kprobe(struct optimized_kprobe *op);
308 309
extern int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
					 struct kprobe *orig);
310
extern void arch_remove_optimized_kprobe(struct optimized_kprobe *op);
311
extern void arch_optimize_kprobes(struct list_head *oplist);
312 313
extern void arch_unoptimize_kprobes(struct list_head *oplist,
				    struct list_head *done_list);
314 315 316 317 318
extern void arch_unoptimize_kprobe(struct optimized_kprobe *op);
extern int arch_within_optimized_kprobe(struct optimized_kprobe *op,
					unsigned long addr);

extern void opt_pre_handler(struct kprobe *p, struct pt_regs *regs);
319

H
Heiko Carstens 已提交
320 321
DEFINE_INSN_CACHE_OPS(optinsn);

322 323 324 325 326 327
#ifdef CONFIG_SYSCTL
extern int sysctl_kprobes_optimization;
extern int proc_kprobes_optimization_handler(struct ctl_table *table,
					     int write, void __user *buffer,
					     size_t *length, loff_t *ppos);
#endif
328 329 330
extern void wait_for_kprobe_optimizer(void);
#else
static inline void wait_for_kprobe_optimizer(void) { }
331
#endif /* CONFIG_OPTPROBES */
332
#ifdef CONFIG_KPROBES_ON_FTRACE
333
extern void kprobe_ftrace_handler(unsigned long ip, unsigned long parent_ip,
334
				  struct ftrace_ops *ops, struct pt_regs *regs);
335 336 337
extern int arch_prepare_kprobe_ftrace(struct kprobe *p);
#endif

338
int arch_check_ftrace_location(struct kprobe *p);
339

340
/* Get the kprobe at this addr (if any) - called with preemption disabled */
L
Linus Torvalds 已提交
341
struct kprobe *get_kprobe(void *addr);
342 343 344
void kretprobe_hash_lock(struct task_struct *tsk,
			 struct hlist_head **head, unsigned long *flags);
void kretprobe_hash_unlock(struct task_struct *tsk, unsigned long *flags);
345
struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk);
L
Linus Torvalds 已提交
346

347 348 349
/* kprobe_running() will just return the current_kprobe on this CPU */
static inline struct kprobe *kprobe_running(void)
{
C
Christoph Lameter 已提交
350
	return (__this_cpu_read(current_kprobe));
351 352 353 354
}

static inline void reset_current_kprobe(void)
{
C
Christoph Lameter 已提交
355
	__this_cpu_write(current_kprobe, NULL);
356 357 358 359
}

static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
{
360
	return this_cpu_ptr(&kprobe_ctlblk);
361 362
}

363
kprobe_opcode_t *kprobe_lookup_name(const char *name, unsigned int offset);
L
Linus Torvalds 已提交
364 365
int register_kprobe(struct kprobe *p);
void unregister_kprobe(struct kprobe *p);
366 367
int register_kprobes(struct kprobe **kps, int num);
void unregister_kprobes(struct kprobe **kps, int num);
368
unsigned long arch_deref_entry_point(void *);
L
Linus Torvalds 已提交
369

370 371
int register_kretprobe(struct kretprobe *rp);
void unregister_kretprobe(struct kretprobe *rp);
372 373
int register_kretprobes(struct kretprobe **rps, int num);
void unregister_kretprobes(struct kretprobe **rps, int num);
374 375

void kprobe_flush_task(struct task_struct *tk);
376
void recycle_rp_inst(struct kretprobe_instance *ri, struct hlist_head *head);
377

378 379 380
int disable_kprobe(struct kprobe *kp);
int enable_kprobe(struct kprobe *kp);

381 382
void dump_kprobe(struct kprobe *kp);

383
#else /* !CONFIG_KPROBES: */
384

385 386 387 388 389 390 391 392
static inline int kprobes_built_in(void)
{
	return 0;
}
static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
{
	return 0;
}
393 394 395 396
static inline struct kprobe *get_kprobe(void *addr)
{
	return NULL;
}
397
static inline struct kprobe *kprobe_running(void)
L
Linus Torvalds 已提交
398
{
399
	return NULL;
L
Linus Torvalds 已提交
400 401 402 403 404
}
static inline int register_kprobe(struct kprobe *p)
{
	return -ENOSYS;
}
405 406 407 408
static inline int register_kprobes(struct kprobe **kps, int num)
{
	return -ENOSYS;
}
L
Linus Torvalds 已提交
409 410 411
static inline void unregister_kprobe(struct kprobe *p)
{
}
412 413 414
static inline void unregister_kprobes(struct kprobe **kps, int num)
{
}
415 416 417 418
static inline int register_kretprobe(struct kretprobe *rp)
{
	return -ENOSYS;
}
419 420 421 422
static inline int register_kretprobes(struct kretprobe **rps, int num)
{
	return -ENOSYS;
}
423 424 425
static inline void unregister_kretprobe(struct kretprobe *rp)
{
}
426 427 428
static inline void unregister_kretprobes(struct kretprobe **rps, int num)
{
}
429 430 431
static inline void kprobe_flush_task(struct task_struct *tk)
{
}
432 433 434 435 436 437 438 439
static inline int disable_kprobe(struct kprobe *kp)
{
	return -ENOSYS;
}
static inline int enable_kprobe(struct kprobe *kp)
{
	return -ENOSYS;
}
440
#endif /* CONFIG_KPROBES */
441 442 443 444 445 446 447 448 449
static inline int disable_kretprobe(struct kretprobe *rp)
{
	return disable_kprobe(&rp->kp);
}
static inline int enable_kretprobe(struct kretprobe *rp)
{
	return enable_kprobe(&rp->kp);
}

450 451 452 453 454 455 456 457 458 459 460 461 462
#ifndef CONFIG_KPROBES
static inline bool is_kprobe_insn_slot(unsigned long addr)
{
	return false;
}
#endif
#ifndef CONFIG_OPTPROBES
static inline bool is_kprobe_optinsn_slot(unsigned long addr)
{
	return false;
}
#endif

463
#endif /* _LINUX_KPROBES_H */