kprobes.h 9.3 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/linkage.h>
L
Linus Torvalds 已提交
33 34 35
#include <linux/list.h>
#include <linux/notifier.h>
#include <linux/smp.h>
36
#include <linux/percpu.h>
37 38
#include <linux/spinlock.h>
#include <linux/rcupdate.h>
I
Ingo Molnar 已提交
39
#include <linux/mutex.h>
40

41
#ifdef CONFIG_KPROBES
L
Linus Torvalds 已提交
42 43
#include <asm/kprobes.h>

44 45 46 47 48 49
/* kprobe_status settings */
#define KPROBE_HIT_ACTIVE	0x00000001
#define KPROBE_HIT_SS		0x00000002
#define KPROBE_REENTER		0x00000004
#define KPROBE_HIT_SSDONE	0x00000008

50
/* Attach to insert probes on any functions which should be ignored*/
51
#define __kprobes	__attribute__((__section__(".kprobes.text"))) notrace
52

L
Linus Torvalds 已提交
53 54
struct kprobe;
struct pt_regs;
55 56
struct kretprobe;
struct kretprobe_instance;
L
Linus Torvalds 已提交
57 58 59 60 61 62
typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *);
typedef int (*kprobe_break_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);
63 64 65
typedef int (*kretprobe_handler_t) (struct kretprobe_instance *,
				    struct pt_regs *);

L
Linus Torvalds 已提交
66 67 68
struct kprobe {
	struct hlist_node hlist;

69 70 71
	/* list of kprobes for multi-handler support */
	struct list_head list;

72 73 74
	/* Indicates that the corresponding module has been ref counted */
	unsigned int mod_refcounted;

75 76 77
	/*count the number of times this probe was temporarily disarmed */
	unsigned long nmissed;

L
Linus Torvalds 已提交
78 79 80
	/* location of the probe point */
	kprobe_opcode_t *addr;

81
	/* Allow user to indicate symbol name of the probe point */
82
	const char *symbol_name;
83 84 85 86

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

L
Linus Torvalds 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	/* Called before addr is executed. */
	kprobe_pre_handler_t pre_handler;

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

	/* ... called if executing addr causes a fault (eg. page fault).
	 * Return 1 if it handled fault, otherwise kernel will see it. */
	kprobe_fault_handler_t fault_handler;

	/* ... called if breakpoint trap occurs in probe handler.
	 * Return 1 if it handled break, otherwise kernel will see it. */
	kprobe_break_handler_t break_handler;

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

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

/*
 * Special probe type that uses setjmp-longjmp type tricks to resume
 * execution at a specified entry with a matching prototype corresponding
 * to the probed function - a trick to enable arguments to become
 * accessible seamlessly by probe handling logic.
 * Note:
 * Because of the way compilers allocate stack space for local variables
 * etc upfront, regardless of sub-scopes within a function, this mirroring
 * principle currently works only for probes placed on function entry points.
 */
struct jprobe {
	struct kprobe kp;
120
	void *entry;	/* probe handling code to jump to */
L
Linus Torvalds 已提交
121 122
};

123 124 125
/* For backward compatibility with old code using JPROBE_ENTRY() */
#define JPROBE_ENTRY(handler)	(handler)

126 127 128
DECLARE_PER_CPU(struct kprobe *, current_kprobe);
DECLARE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);

129
#ifdef CONFIG_KRETPROBES
130 131
extern void arch_prepare_kretprobe(struct kretprobe_instance *ri,
				   struct pt_regs *regs);
132
extern int arch_trampoline_kprobe(struct kprobe *p);
133
#else /* CONFIG_KRETPROBES */
134 135 136 137
static inline void arch_prepare_kretprobe(struct kretprobe *rp,
					struct pt_regs *regs)
{
}
138 139 140 141
static inline int arch_trampoline_kprobe(struct kprobe *p)
{
	return 0;
}
142
#endif /* CONFIG_KRETPROBES */
143 144 145 146 147 148 149 150 151 152 153 154 155
/*
 * 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;
156
	kretprobe_handler_t entry_handler;
157 158
	int maxactive;
	int nmissed;
159
	size_t data_size;
160
	struct hlist_head free_instances;
161
	spinlock_t lock;
162 163 164 165 166
};

struct kretprobe_instance {
	struct hlist_node hlist;
	struct kretprobe *rp;
167 168
	kprobe_opcode_t *ret_addr;
	struct task_struct *task;
169
	char data[0];
170 171
};

172 173 174 175
struct kretprobe_blackpoint {
	const char *name;
	void *addr;
};
176 177 178 179 180 181 182

struct kprobe_blackpoint {
	const char *name;
	unsigned long start_addr;
	unsigned long range;
};

183 184
extern struct kretprobe_blackpoint kretprobe_blacklist[];

185 186 187 188 189 190 191 192 193 194
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();
	}
}

195 196 197 198 199 200 201 202 203
#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 已提交
204
extern int arch_prepare_kprobe(struct kprobe *p);
205 206
extern void arch_arm_kprobe(struct kprobe *p);
extern void arch_disarm_kprobe(struct kprobe *p);
207
extern int arch_init_kprobes(void);
L
Linus Torvalds 已提交
208
extern void show_registers(struct pt_regs *regs);
209
extern kprobe_opcode_t *get_insn_slot(void);
210
extern void free_insn_slot(kprobe_opcode_t *slot, int dirty);
211
extern void kprobes_inc_nmissed_count(struct kprobe *p);
L
Linus Torvalds 已提交
212

213
/* Get the kprobe at this addr (if any) - called with preemption disabled */
L
Linus Torvalds 已提交
214
struct kprobe *get_kprobe(void *addr);
215 216 217
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);
218
struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk);
L
Linus Torvalds 已提交
219

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
/* kprobe_running() will just return the current_kprobe on this CPU */
static inline struct kprobe *kprobe_running(void)
{
	return (__get_cpu_var(current_kprobe));
}

static inline void reset_current_kprobe(void)
{
	__get_cpu_var(current_kprobe) = NULL;
}

static inline struct kprobe_ctlblk *get_kprobe_ctlblk(void)
{
	return (&__get_cpu_var(kprobe_ctlblk));
}

L
Linus Torvalds 已提交
236 237
int register_kprobe(struct kprobe *p);
void unregister_kprobe(struct kprobe *p);
238 239
int register_kprobes(struct kprobe **kps, int num);
void unregister_kprobes(struct kprobe **kps, int num);
L
Linus Torvalds 已提交
240 241 242 243
int setjmp_pre_handler(struct kprobe *, struct pt_regs *);
int longjmp_break_handler(struct kprobe *, struct pt_regs *);
int register_jprobe(struct jprobe *p);
void unregister_jprobe(struct jprobe *p);
244 245
int register_jprobes(struct jprobe **jps, int num);
void unregister_jprobes(struct jprobe **jps, int num);
L
Linus Torvalds 已提交
246
void jprobe_return(void);
247
unsigned long arch_deref_entry_point(void *);
L
Linus Torvalds 已提交
248

249 250
int register_kretprobe(struct kretprobe *rp);
void unregister_kretprobe(struct kretprobe *rp);
251 252
int register_kretprobes(struct kretprobe **rps, int num);
void unregister_kretprobes(struct kretprobe **rps, int num);
253 254

void kprobe_flush_task(struct task_struct *tk);
255
void recycle_rp_inst(struct kretprobe_instance *ri, struct hlist_head *head);
256

257
#else /* CONFIG_KPROBES */
258

259
#define __kprobes	notrace
260 261 262
struct jprobe;
struct kretprobe;

263 264 265 266
static inline struct kprobe *get_kprobe(void *addr)
{
	return NULL;
}
267
static inline struct kprobe *kprobe_running(void)
L
Linus Torvalds 已提交
268
{
269
	return NULL;
L
Linus Torvalds 已提交
270 271 272 273 274
}
static inline int register_kprobe(struct kprobe *p)
{
	return -ENOSYS;
}
275 276 277 278
static inline int register_kprobes(struct kprobe **kps, int num)
{
	return -ENOSYS;
}
L
Linus Torvalds 已提交
279 280 281
static inline void unregister_kprobe(struct kprobe *p)
{
}
282 283 284
static inline void unregister_kprobes(struct kprobe **kps, int num)
{
}
L
Linus Torvalds 已提交
285 286 287 288
static inline int register_jprobe(struct jprobe *p)
{
	return -ENOSYS;
}
289 290 291 292
static inline int register_jprobes(struct jprobe **jps, int num)
{
	return -ENOSYS;
}
L
Linus Torvalds 已提交
293 294 295
static inline void unregister_jprobe(struct jprobe *p)
{
}
296 297 298
static inline void unregister_jprobes(struct jprobe **jps, int num)
{
}
L
Linus Torvalds 已提交
299 300 301
static inline void jprobe_return(void)
{
}
302 303 304 305
static inline int register_kretprobe(struct kretprobe *rp)
{
	return -ENOSYS;
}
306 307 308 309
static inline int register_kretprobes(struct kretprobe **rps, int num)
{
	return -ENOSYS;
}
310 311 312
static inline void unregister_kretprobe(struct kretprobe *rp)
{
}
313 314 315
static inline void unregister_kretprobes(struct kretprobe **rps, int num)
{
}
316 317 318 319
static inline void kprobe_flush_task(struct task_struct *tk)
{
}
#endif				/* CONFIG_KPROBES */
L
Linus Torvalds 已提交
320
#endif				/* _LINUX_KPROBES_H */