diff options
Diffstat (limited to 'arch/i386/kernel/kprobes.c')
-rw-r--r-- | arch/i386/kernel/kprobes.c | 385 |
1 files changed, 385 insertions, 0 deletions
diff --git a/arch/i386/kernel/kprobes.c b/arch/i386/kernel/kprobes.c new file mode 100644 index 000000000000..671681659243 --- /dev/null +++ b/arch/i386/kernel/kprobes.c | |||
@@ -0,0 +1,385 @@ | |||
1 | /* | ||
2 | * Kernel Probes (KProbes) | ||
3 | * arch/i386/kernel/kprobes.c | ||
4 | * | ||
5 | * This program is free software; you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation; either version 2 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program; if not, write to the Free Software | ||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
18 | * | ||
19 | * Copyright (C) IBM Corporation, 2002, 2004 | ||
20 | * | ||
21 | * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel | ||
22 | * Probes initial implementation ( includes contributions from | ||
23 | * Rusty Russell). | ||
24 | * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes | ||
25 | * interface to access function arguments. | ||
26 | */ | ||
27 | |||
28 | #include <linux/config.h> | ||
29 | #include <linux/kprobes.h> | ||
30 | #include <linux/ptrace.h> | ||
31 | #include <linux/spinlock.h> | ||
32 | #include <linux/preempt.h> | ||
33 | #include <asm/kdebug.h> | ||
34 | #include <asm/desc.h> | ||
35 | |||
36 | /* kprobe_status settings */ | ||
37 | #define KPROBE_HIT_ACTIVE 0x00000001 | ||
38 | #define KPROBE_HIT_SS 0x00000002 | ||
39 | |||
40 | static struct kprobe *current_kprobe; | ||
41 | static unsigned long kprobe_status, kprobe_old_eflags, kprobe_saved_eflags; | ||
42 | static struct pt_regs jprobe_saved_regs; | ||
43 | static long *jprobe_saved_esp; | ||
44 | /* copy of the kernel stack at the probe fire time */ | ||
45 | static kprobe_opcode_t jprobes_stack[MAX_STACK_SIZE]; | ||
46 | void jprobe_return_end(void); | ||
47 | |||
48 | /* | ||
49 | * returns non-zero if opcode modifies the interrupt flag. | ||
50 | */ | ||
51 | static inline int is_IF_modifier(kprobe_opcode_t opcode) | ||
52 | { | ||
53 | switch (opcode) { | ||
54 | case 0xfa: /* cli */ | ||
55 | case 0xfb: /* sti */ | ||
56 | case 0xcf: /* iret/iretd */ | ||
57 | case 0x9d: /* popf/popfd */ | ||
58 | return 1; | ||
59 | } | ||
60 | return 0; | ||
61 | } | ||
62 | |||
63 | int arch_prepare_kprobe(struct kprobe *p) | ||
64 | { | ||
65 | return 0; | ||
66 | } | ||
67 | |||
68 | void arch_copy_kprobe(struct kprobe *p) | ||
69 | { | ||
70 | memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t)); | ||
71 | } | ||
72 | |||
73 | void arch_remove_kprobe(struct kprobe *p) | ||
74 | { | ||
75 | } | ||
76 | |||
77 | static inline void disarm_kprobe(struct kprobe *p, struct pt_regs *regs) | ||
78 | { | ||
79 | *p->addr = p->opcode; | ||
80 | regs->eip = (unsigned long)p->addr; | ||
81 | } | ||
82 | |||
83 | static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs) | ||
84 | { | ||
85 | regs->eflags |= TF_MASK; | ||
86 | regs->eflags &= ~IF_MASK; | ||
87 | /*single step inline if the instruction is an int3*/ | ||
88 | if (p->opcode == BREAKPOINT_INSTRUCTION) | ||
89 | regs->eip = (unsigned long)p->addr; | ||
90 | else | ||
91 | regs->eip = (unsigned long)&p->ainsn.insn; | ||
92 | } | ||
93 | |||
94 | /* | ||
95 | * Interrupts are disabled on entry as trap3 is an interrupt gate and they | ||
96 | * remain disabled thorough out this function. | ||
97 | */ | ||
98 | static int kprobe_handler(struct pt_regs *regs) | ||
99 | { | ||
100 | struct kprobe *p; | ||
101 | int ret = 0; | ||
102 | kprobe_opcode_t *addr = NULL; | ||
103 | unsigned long *lp; | ||
104 | |||
105 | /* We're in an interrupt, but this is clear and BUG()-safe. */ | ||
106 | preempt_disable(); | ||
107 | /* Check if the application is using LDT entry for its code segment and | ||
108 | * calculate the address by reading the base address from the LDT entry. | ||
109 | */ | ||
110 | if ((regs->xcs & 4) && (current->mm)) { | ||
111 | lp = (unsigned long *) ((unsigned long)((regs->xcs >> 3) * 8) | ||
112 | + (char *) current->mm->context.ldt); | ||
113 | addr = (kprobe_opcode_t *) (get_desc_base(lp) + regs->eip - | ||
114 | sizeof(kprobe_opcode_t)); | ||
115 | } else { | ||
116 | addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t)); | ||
117 | } | ||
118 | /* Check we're not actually recursing */ | ||
119 | if (kprobe_running()) { | ||
120 | /* We *are* holding lock here, so this is safe. | ||
121 | Disarm the probe we just hit, and ignore it. */ | ||
122 | p = get_kprobe(addr); | ||
123 | if (p) { | ||
124 | if (kprobe_status == KPROBE_HIT_SS) { | ||
125 | regs->eflags &= ~TF_MASK; | ||
126 | regs->eflags |= kprobe_saved_eflags; | ||
127 | unlock_kprobes(); | ||
128 | goto no_kprobe; | ||
129 | } | ||
130 | disarm_kprobe(p, regs); | ||
131 | ret = 1; | ||
132 | } else { | ||
133 | p = current_kprobe; | ||
134 | if (p->break_handler && p->break_handler(p, regs)) { | ||
135 | goto ss_probe; | ||
136 | } | ||
137 | } | ||
138 | /* If it's not ours, can't be delete race, (we hold lock). */ | ||
139 | goto no_kprobe; | ||
140 | } | ||
141 | |||
142 | lock_kprobes(); | ||
143 | p = get_kprobe(addr); | ||
144 | if (!p) { | ||
145 | unlock_kprobes(); | ||
146 | if (regs->eflags & VM_MASK) { | ||
147 | /* We are in virtual-8086 mode. Return 0 */ | ||
148 | goto no_kprobe; | ||
149 | } | ||
150 | |||
151 | if (*addr != BREAKPOINT_INSTRUCTION) { | ||
152 | /* | ||
153 | * The breakpoint instruction was removed right | ||
154 | * after we hit it. Another cpu has removed | ||
155 | * either a probepoint or a debugger breakpoint | ||
156 | * at this address. In either case, no further | ||
157 | * handling of this interrupt is appropriate. | ||
158 | */ | ||
159 | ret = 1; | ||
160 | } | ||
161 | /* Not one of ours: let kernel handle it */ | ||
162 | goto no_kprobe; | ||
163 | } | ||
164 | |||
165 | kprobe_status = KPROBE_HIT_ACTIVE; | ||
166 | current_kprobe = p; | ||
167 | kprobe_saved_eflags = kprobe_old_eflags | ||
168 | = (regs->eflags & (TF_MASK | IF_MASK)); | ||
169 | if (is_IF_modifier(p->opcode)) | ||
170 | kprobe_saved_eflags &= ~IF_MASK; | ||
171 | |||
172 | if (p->pre_handler && p->pre_handler(p, regs)) | ||
173 | /* handler has already set things up, so skip ss setup */ | ||
174 | return 1; | ||
175 | |||
176 | ss_probe: | ||
177 | prepare_singlestep(p, regs); | ||
178 | kprobe_status = KPROBE_HIT_SS; | ||
179 | return 1; | ||
180 | |||
181 | no_kprobe: | ||
182 | preempt_enable_no_resched(); | ||
183 | return ret; | ||
184 | } | ||
185 | |||
186 | /* | ||
187 | * Called after single-stepping. p->addr is the address of the | ||
188 | * instruction whose first byte has been replaced by the "int 3" | ||
189 | * instruction. To avoid the SMP problems that can occur when we | ||
190 | * temporarily put back the original opcode to single-step, we | ||
191 | * single-stepped a copy of the instruction. The address of this | ||
192 | * copy is p->ainsn.insn. | ||
193 | * | ||
194 | * This function prepares to return from the post-single-step | ||
195 | * interrupt. We have to fix up the stack as follows: | ||
196 | * | ||
197 | * 0) Except in the case of absolute or indirect jump or call instructions, | ||
198 | * the new eip is relative to the copied instruction. We need to make | ||
199 | * it relative to the original instruction. | ||
200 | * | ||
201 | * 1) If the single-stepped instruction was pushfl, then the TF and IF | ||
202 | * flags are set in the just-pushed eflags, and may need to be cleared. | ||
203 | * | ||
204 | * 2) If the single-stepped instruction was a call, the return address | ||
205 | * that is atop the stack is the address following the copied instruction. | ||
206 | * We need to make it the address following the original instruction. | ||
207 | */ | ||
208 | static void resume_execution(struct kprobe *p, struct pt_regs *regs) | ||
209 | { | ||
210 | unsigned long *tos = (unsigned long *)®s->esp; | ||
211 | unsigned long next_eip = 0; | ||
212 | unsigned long copy_eip = (unsigned long)&p->ainsn.insn; | ||
213 | unsigned long orig_eip = (unsigned long)p->addr; | ||
214 | |||
215 | switch (p->ainsn.insn[0]) { | ||
216 | case 0x9c: /* pushfl */ | ||
217 | *tos &= ~(TF_MASK | IF_MASK); | ||
218 | *tos |= kprobe_old_eflags; | ||
219 | break; | ||
220 | case 0xe8: /* call relative - Fix return addr */ | ||
221 | *tos = orig_eip + (*tos - copy_eip); | ||
222 | break; | ||
223 | case 0xff: | ||
224 | if ((p->ainsn.insn[1] & 0x30) == 0x10) { | ||
225 | /* call absolute, indirect */ | ||
226 | /* Fix return addr; eip is correct. */ | ||
227 | next_eip = regs->eip; | ||
228 | *tos = orig_eip + (*tos - copy_eip); | ||
229 | } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */ | ||
230 | ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */ | ||
231 | /* eip is correct. */ | ||
232 | next_eip = regs->eip; | ||
233 | } | ||
234 | break; | ||
235 | case 0xea: /* jmp absolute -- eip is correct */ | ||
236 | next_eip = regs->eip; | ||
237 | break; | ||
238 | default: | ||
239 | break; | ||
240 | } | ||
241 | |||
242 | regs->eflags &= ~TF_MASK; | ||
243 | if (next_eip) { | ||
244 | regs->eip = next_eip; | ||
245 | } else { | ||
246 | regs->eip = orig_eip + (regs->eip - copy_eip); | ||
247 | } | ||
248 | } | ||
249 | |||
250 | /* | ||
251 | * Interrupts are disabled on entry as trap1 is an interrupt gate and they | ||
252 | * remain disabled thoroughout this function. And we hold kprobe lock. | ||
253 | */ | ||
254 | static inline int post_kprobe_handler(struct pt_regs *regs) | ||
255 | { | ||
256 | if (!kprobe_running()) | ||
257 | return 0; | ||
258 | |||
259 | if (current_kprobe->post_handler) | ||
260 | current_kprobe->post_handler(current_kprobe, regs, 0); | ||
261 | |||
262 | resume_execution(current_kprobe, regs); | ||
263 | regs->eflags |= kprobe_saved_eflags; | ||
264 | |||
265 | unlock_kprobes(); | ||
266 | preempt_enable_no_resched(); | ||
267 | |||
268 | /* | ||
269 | * if somebody else is singlestepping across a probe point, eflags | ||
270 | * will have TF set, in which case, continue the remaining processing | ||
271 | * of do_debug, as if this is not a probe hit. | ||
272 | */ | ||
273 | if (regs->eflags & TF_MASK) | ||
274 | return 0; | ||
275 | |||
276 | return 1; | ||
277 | } | ||
278 | |||
279 | /* Interrupts disabled, kprobe_lock held. */ | ||
280 | static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr) | ||
281 | { | ||
282 | if (current_kprobe->fault_handler | ||
283 | && current_kprobe->fault_handler(current_kprobe, regs, trapnr)) | ||
284 | return 1; | ||
285 | |||
286 | if (kprobe_status & KPROBE_HIT_SS) { | ||
287 | resume_execution(current_kprobe, regs); | ||
288 | regs->eflags |= kprobe_old_eflags; | ||
289 | |||
290 | unlock_kprobes(); | ||
291 | preempt_enable_no_resched(); | ||
292 | } | ||
293 | return 0; | ||
294 | } | ||
295 | |||
296 | /* | ||
297 | * Wrapper routine to for handling exceptions. | ||
298 | */ | ||
299 | int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val, | ||
300 | void *data) | ||
301 | { | ||
302 | struct die_args *args = (struct die_args *)data; | ||
303 | switch (val) { | ||
304 | case DIE_INT3: | ||
305 | if (kprobe_handler(args->regs)) | ||
306 | return NOTIFY_STOP; | ||
307 | break; | ||
308 | case DIE_DEBUG: | ||
309 | if (post_kprobe_handler(args->regs)) | ||
310 | return NOTIFY_STOP; | ||
311 | break; | ||
312 | case DIE_GPF: | ||
313 | if (kprobe_running() && | ||
314 | kprobe_fault_handler(args->regs, args->trapnr)) | ||
315 | return NOTIFY_STOP; | ||
316 | break; | ||
317 | case DIE_PAGE_FAULT: | ||
318 | if (kprobe_running() && | ||
319 | kprobe_fault_handler(args->regs, args->trapnr)) | ||
320 | return NOTIFY_STOP; | ||
321 | break; | ||
322 | default: | ||
323 | break; | ||
324 | } | ||
325 | return NOTIFY_DONE; | ||
326 | } | ||
327 | |||
328 | int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs) | ||
329 | { | ||
330 | struct jprobe *jp = container_of(p, struct jprobe, kp); | ||
331 | unsigned long addr; | ||
332 | |||
333 | jprobe_saved_regs = *regs; | ||
334 | jprobe_saved_esp = ®s->esp; | ||
335 | addr = (unsigned long)jprobe_saved_esp; | ||
336 | |||
337 | /* | ||
338 | * TBD: As Linus pointed out, gcc assumes that the callee | ||
339 | * owns the argument space and could overwrite it, e.g. | ||
340 | * tailcall optimization. So, to be absolutely safe | ||
341 | * we also save and restore enough stack bytes to cover | ||
342 | * the argument area. | ||
343 | */ | ||
344 | memcpy(jprobes_stack, (kprobe_opcode_t *) addr, MIN_STACK_SIZE(addr)); | ||
345 | regs->eflags &= ~IF_MASK; | ||
346 | regs->eip = (unsigned long)(jp->entry); | ||
347 | return 1; | ||
348 | } | ||
349 | |||
350 | void jprobe_return(void) | ||
351 | { | ||
352 | preempt_enable_no_resched(); | ||
353 | asm volatile (" xchgl %%ebx,%%esp \n" | ||
354 | " int3 \n" | ||
355 | " .globl jprobe_return_end \n" | ||
356 | " jprobe_return_end: \n" | ||
357 | " nop \n"::"b" | ||
358 | (jprobe_saved_esp):"memory"); | ||
359 | } | ||
360 | |||
361 | int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) | ||
362 | { | ||
363 | u8 *addr = (u8 *) (regs->eip - 1); | ||
364 | unsigned long stack_addr = (unsigned long)jprobe_saved_esp; | ||
365 | struct jprobe *jp = container_of(p, struct jprobe, kp); | ||
366 | |||
367 | if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) { | ||
368 | if (®s->esp != jprobe_saved_esp) { | ||
369 | struct pt_regs *saved_regs = | ||
370 | container_of(jprobe_saved_esp, struct pt_regs, esp); | ||
371 | printk("current esp %p does not match saved esp %p\n", | ||
372 | ®s->esp, jprobe_saved_esp); | ||
373 | printk("Saved registers for jprobe %p\n", jp); | ||
374 | show_registers(saved_regs); | ||
375 | printk("Current registers\n"); | ||
376 | show_registers(regs); | ||
377 | BUG(); | ||
378 | } | ||
379 | *regs = jprobe_saved_regs; | ||
380 | memcpy((kprobe_opcode_t *) stack_addr, jprobes_stack, | ||
381 | MIN_STACK_SIZE(stack_addr)); | ||
382 | return 1; | ||
383 | } | ||
384 | return 0; | ||
385 | } | ||