Commit | Line | Data |
---|---|---|
1da177e4 LT |
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. | |
b94cce92 HN |
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. | |
1da177e4 LT |
31 | */ |
32 | #include <linux/config.h> | |
33 | #include <linux/list.h> | |
34 | #include <linux/notifier.h> | |
35 | #include <linux/smp.h> | |
b94cce92 HN |
36 | #include <linux/spinlock.h> |
37 | ||
1da177e4 LT |
38 | #include <asm/kprobes.h> |
39 | ||
40 | struct kprobe; | |
41 | struct pt_regs; | |
b94cce92 HN |
42 | struct kretprobe; |
43 | struct kretprobe_instance; | |
1da177e4 LT |
44 | typedef int (*kprobe_pre_handler_t) (struct kprobe *, struct pt_regs *); |
45 | typedef int (*kprobe_break_handler_t) (struct kprobe *, struct pt_regs *); | |
46 | typedef void (*kprobe_post_handler_t) (struct kprobe *, struct pt_regs *, | |
47 | unsigned long flags); | |
48 | typedef int (*kprobe_fault_handler_t) (struct kprobe *, struct pt_regs *, | |
49 | int trapnr); | |
b94cce92 HN |
50 | typedef int (*kretprobe_handler_t) (struct kretprobe_instance *, |
51 | struct pt_regs *); | |
52 | ||
1da177e4 LT |
53 | struct kprobe { |
54 | struct hlist_node hlist; | |
55 | ||
64f562c6 AM |
56 | /* list of kprobes for multi-handler support */ |
57 | struct list_head list; | |
58 | ||
1da177e4 LT |
59 | /* location of the probe point */ |
60 | kprobe_opcode_t *addr; | |
61 | ||
62 | /* Called before addr is executed. */ | |
63 | kprobe_pre_handler_t pre_handler; | |
64 | ||
65 | /* Called after addr is executed, unless... */ | |
66 | kprobe_post_handler_t post_handler; | |
67 | ||
68 | /* ... called if executing addr causes a fault (eg. page fault). | |
69 | * Return 1 if it handled fault, otherwise kernel will see it. */ | |
70 | kprobe_fault_handler_t fault_handler; | |
71 | ||
72 | /* ... called if breakpoint trap occurs in probe handler. | |
73 | * Return 1 if it handled break, otherwise kernel will see it. */ | |
74 | kprobe_break_handler_t break_handler; | |
75 | ||
76 | /* Saved opcode (which has been replaced with breakpoint) */ | |
77 | kprobe_opcode_t opcode; | |
78 | ||
79 | /* copy of the original instruction */ | |
80 | struct arch_specific_insn ainsn; | |
81 | }; | |
82 | ||
83 | /* | |
84 | * Special probe type that uses setjmp-longjmp type tricks to resume | |
85 | * execution at a specified entry with a matching prototype corresponding | |
86 | * to the probed function - a trick to enable arguments to become | |
87 | * accessible seamlessly by probe handling logic. | |
88 | * Note: | |
89 | * Because of the way compilers allocate stack space for local variables | |
90 | * etc upfront, regardless of sub-scopes within a function, this mirroring | |
91 | * principle currently works only for probes placed on function entry points. | |
92 | */ | |
93 | struct jprobe { | |
94 | struct kprobe kp; | |
95 | kprobe_opcode_t *entry; /* probe handling code to jump to */ | |
96 | }; | |
97 | ||
b94cce92 HN |
98 | #ifdef ARCH_SUPPORTS_KRETPROBES |
99 | extern int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs); | |
100 | extern void trampoline_post_handler(struct kprobe *p, struct pt_regs *regs, | |
101 | unsigned long flags); | |
102 | extern struct task_struct *arch_get_kprobe_task(void *ptr); | |
103 | extern void arch_prepare_kretprobe(struct kretprobe *rp, struct pt_regs *regs); | |
104 | extern void arch_kprobe_flush_task(struct task_struct *tk, spinlock_t *kp_lock); | |
105 | #else /* ARCH_SUPPORTS_KRETPROBES */ | |
106 | static inline void kretprobe_trampoline(void) | |
107 | { | |
108 | } | |
109 | static inline int trampoline_probe_handler(struct kprobe *p, | |
110 | struct pt_regs *regs) | |
111 | { | |
112 | return 0; | |
113 | } | |
114 | static inline void trampoline_post_handler(struct kprobe *p, | |
115 | struct pt_regs *regs, unsigned long flags) | |
116 | { | |
117 | } | |
118 | static inline void arch_prepare_kretprobe(struct kretprobe *rp, | |
119 | struct pt_regs *regs) | |
120 | { | |
121 | } | |
122 | static inline void arch_kprobe_flush_task(struct task_struct *tk) | |
123 | { | |
124 | } | |
125 | #define arch_get_kprobe_task(ptr) ((struct task_struct *)NULL) | |
126 | #endif /* ARCH_SUPPORTS_KRETPROBES */ | |
127 | /* | |
128 | * Function-return probe - | |
129 | * Note: | |
130 | * User needs to provide a handler function, and initialize maxactive. | |
131 | * maxactive - The maximum number of instances of the probed function that | |
132 | * can be active concurrently. | |
133 | * nmissed - tracks the number of times the probed function's return was | |
134 | * ignored, due to maxactive being too low. | |
135 | * | |
136 | */ | |
137 | struct kretprobe { | |
138 | struct kprobe kp; | |
139 | kretprobe_handler_t handler; | |
140 | int maxactive; | |
141 | int nmissed; | |
142 | struct hlist_head free_instances; | |
143 | struct hlist_head used_instances; | |
144 | }; | |
145 | ||
146 | struct kretprobe_instance { | |
147 | struct hlist_node uflist; /* either on free list or used list */ | |
148 | struct hlist_node hlist; | |
149 | struct kretprobe *rp; | |
150 | void *ret_addr; | |
151 | void *stack_addr; | |
152 | }; | |
153 | ||
1da177e4 LT |
154 | #ifdef CONFIG_KPROBES |
155 | /* Locks kprobe: irq must be disabled */ | |
156 | void lock_kprobes(void); | |
157 | void unlock_kprobes(void); | |
158 | ||
159 | /* kprobe running now on this CPU? */ | |
160 | static inline int kprobe_running(void) | |
161 | { | |
162 | extern unsigned int kprobe_cpu; | |
163 | return kprobe_cpu == smp_processor_id(); | |
164 | } | |
165 | ||
166 | extern int arch_prepare_kprobe(struct kprobe *p); | |
167 | extern void arch_copy_kprobe(struct kprobe *p); | |
168 | extern void arch_remove_kprobe(struct kprobe *p); | |
169 | extern void show_registers(struct pt_regs *regs); | |
170 | ||
171 | /* Get the kprobe at this addr (if any). Must have called lock_kprobes */ | |
172 | struct kprobe *get_kprobe(void *addr); | |
b94cce92 | 173 | struct hlist_head * kretprobe_inst_table_head(struct task_struct *tsk); |
1da177e4 LT |
174 | |
175 | int register_kprobe(struct kprobe *p); | |
176 | void unregister_kprobe(struct kprobe *p); | |
177 | int setjmp_pre_handler(struct kprobe *, struct pt_regs *); | |
178 | int longjmp_break_handler(struct kprobe *, struct pt_regs *); | |
179 | int register_jprobe(struct jprobe *p); | |
180 | void unregister_jprobe(struct jprobe *p); | |
181 | void jprobe_return(void); | |
182 | ||
b94cce92 HN |
183 | int register_kretprobe(struct kretprobe *rp); |
184 | void unregister_kretprobe(struct kretprobe *rp); | |
185 | ||
186 | struct kretprobe_instance *get_free_rp_inst(struct kretprobe *rp); | |
187 | struct kretprobe_instance *get_rp_inst(void *sara); | |
188 | struct kretprobe_instance *get_rp_inst_tsk(struct task_struct *tk); | |
189 | void add_rp_inst(struct kretprobe_instance *ri); | |
190 | void kprobe_flush_task(struct task_struct *tk); | |
191 | void recycle_rp_inst(struct kretprobe_instance *ri); | |
192 | #else /* CONFIG_KPROBES */ | |
1da177e4 LT |
193 | static inline int kprobe_running(void) |
194 | { | |
195 | return 0; | |
196 | } | |
197 | static inline int register_kprobe(struct kprobe *p) | |
198 | { | |
199 | return -ENOSYS; | |
200 | } | |
201 | static inline void unregister_kprobe(struct kprobe *p) | |
202 | { | |
203 | } | |
204 | static inline int register_jprobe(struct jprobe *p) | |
205 | { | |
206 | return -ENOSYS; | |
207 | } | |
208 | static inline void unregister_jprobe(struct jprobe *p) | |
209 | { | |
210 | } | |
211 | static inline void jprobe_return(void) | |
212 | { | |
213 | } | |
b94cce92 HN |
214 | static inline int register_kretprobe(struct kretprobe *rp) |
215 | { | |
216 | return -ENOSYS; | |
217 | } | |
218 | static inline void unregister_kretprobe(struct kretprobe *rp) | |
219 | { | |
220 | } | |
221 | static inline void kprobe_flush_task(struct task_struct *tk) | |
222 | { | |
223 | } | |
224 | #endif /* CONFIG_KPROBES */ | |
1da177e4 | 225 | #endif /* _LINUX_KPROBES_H */ |