[PATCH] Kprobes/IA64: fix race when break hits and kprobe not found
[linux-block.git] / arch / i386 / kernel / kprobes.c
CommitLineData
1da177e4
LT
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.
b94cce92
HN
26 * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
27 * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
28 * <prasanna@in.ibm.com> added function-return probes.
1da177e4
LT
29 */
30
31#include <linux/config.h>
32#include <linux/kprobes.h>
33#include <linux/ptrace.h>
34#include <linux/spinlock.h>
35#include <linux/preempt.h>
7e1048b1 36#include <asm/cacheflush.h>
1da177e4
LT
37#include <asm/kdebug.h>
38#include <asm/desc.h>
39
1da177e4
LT
40static struct kprobe *current_kprobe;
41static unsigned long kprobe_status, kprobe_old_eflags, kprobe_saved_eflags;
417c8da6
PP
42static struct kprobe *kprobe_prev;
43static unsigned long kprobe_status_prev, kprobe_old_eflags_prev, kprobe_saved_eflags_prev;
1da177e4
LT
44static struct pt_regs jprobe_saved_regs;
45static long *jprobe_saved_esp;
46/* copy of the kernel stack at the probe fire time */
47static kprobe_opcode_t jprobes_stack[MAX_STACK_SIZE];
48void jprobe_return_end(void);
49
50/*
51 * returns non-zero if opcode modifies the interrupt flag.
52 */
53static inline int is_IF_modifier(kprobe_opcode_t opcode)
54{
55 switch (opcode) {
56 case 0xfa: /* cli */
57 case 0xfb: /* sti */
58 case 0xcf: /* iret/iretd */
59 case 0x9d: /* popf/popfd */
60 return 1;
61 }
62 return 0;
63}
64
3d97ae5b 65int __kprobes arch_prepare_kprobe(struct kprobe *p)
1da177e4
LT
66{
67 return 0;
68}
69
3d97ae5b 70void __kprobes arch_copy_kprobe(struct kprobe *p)
1da177e4
LT
71{
72 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
7e1048b1 73 p->opcode = *p->addr;
1da177e4
LT
74}
75
3d97ae5b 76void __kprobes arch_arm_kprobe(struct kprobe *p)
1da177e4 77{
7e1048b1
RL
78 *p->addr = BREAKPOINT_INSTRUCTION;
79 flush_icache_range((unsigned long) p->addr,
80 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
1da177e4
LT
81}
82
3d97ae5b 83void __kprobes arch_disarm_kprobe(struct kprobe *p)
1da177e4
LT
84{
85 *p->addr = p->opcode;
7e1048b1
RL
86 flush_icache_range((unsigned long) p->addr,
87 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
88}
89
3d97ae5b 90void __kprobes arch_remove_kprobe(struct kprobe *p)
7e1048b1 91{
1da177e4
LT
92}
93
417c8da6
PP
94static inline void save_previous_kprobe(void)
95{
96 kprobe_prev = current_kprobe;
97 kprobe_status_prev = kprobe_status;
98 kprobe_old_eflags_prev = kprobe_old_eflags;
99 kprobe_saved_eflags_prev = kprobe_saved_eflags;
100}
101
102static inline void restore_previous_kprobe(void)
103{
104 current_kprobe = kprobe_prev;
105 kprobe_status = kprobe_status_prev;
106 kprobe_old_eflags = kprobe_old_eflags_prev;
107 kprobe_saved_eflags = kprobe_saved_eflags_prev;
108}
109
110static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs)
111{
112 current_kprobe = p;
113 kprobe_saved_eflags = kprobe_old_eflags
114 = (regs->eflags & (TF_MASK | IF_MASK));
115 if (is_IF_modifier(p->opcode))
116 kprobe_saved_eflags &= ~IF_MASK;
117}
118
1da177e4
LT
119static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
120{
121 regs->eflags |= TF_MASK;
122 regs->eflags &= ~IF_MASK;
123 /*single step inline if the instruction is an int3*/
124 if (p->opcode == BREAKPOINT_INSTRUCTION)
125 regs->eip = (unsigned long)p->addr;
126 else
127 regs->eip = (unsigned long)&p->ainsn.insn;
128}
129
3d97ae5b
PP
130void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
131 struct pt_regs *regs)
b94cce92
HN
132{
133 unsigned long *sara = (unsigned long *)&regs->esp;
4bdbd37f
RL
134 struct kretprobe_instance *ri;
135
136 if ((ri = get_free_rp_inst(rp)) != NULL) {
137 ri->rp = rp;
138 ri->task = current;
139 ri->ret_addr = (kprobe_opcode_t *) *sara;
b94cce92 140
b94cce92
HN
141 /* Replace the return addr with trampoline addr */
142 *sara = (unsigned long) &kretprobe_trampoline;
b94cce92 143
4bdbd37f
RL
144 add_rp_inst(ri);
145 } else {
146 rp->nmissed++;
147 }
b94cce92
HN
148}
149
1da177e4
LT
150/*
151 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
152 * remain disabled thorough out this function.
153 */
3d97ae5b 154static int __kprobes kprobe_handler(struct pt_regs *regs)
1da177e4
LT
155{
156 struct kprobe *p;
157 int ret = 0;
158 kprobe_opcode_t *addr = NULL;
159 unsigned long *lp;
160
161 /* We're in an interrupt, but this is clear and BUG()-safe. */
162 preempt_disable();
163 /* Check if the application is using LDT entry for its code segment and
164 * calculate the address by reading the base address from the LDT entry.
165 */
166 if ((regs->xcs & 4) && (current->mm)) {
167 lp = (unsigned long *) ((unsigned long)((regs->xcs >> 3) * 8)
168 + (char *) current->mm->context.ldt);
169 addr = (kprobe_opcode_t *) (get_desc_base(lp) + regs->eip -
170 sizeof(kprobe_opcode_t));
171 } else {
172 addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
173 }
174 /* Check we're not actually recursing */
175 if (kprobe_running()) {
176 /* We *are* holding lock here, so this is safe.
177 Disarm the probe we just hit, and ignore it. */
178 p = get_kprobe(addr);
179 if (p) {
180 if (kprobe_status == KPROBE_HIT_SS) {
181 regs->eflags &= ~TF_MASK;
182 regs->eflags |= kprobe_saved_eflags;
183 unlock_kprobes();
184 goto no_kprobe;
185 }
417c8da6
PP
186 /* We have reentered the kprobe_handler(), since
187 * another probe was hit while within the handler.
188 * We here save the original kprobes variables and
189 * just single step on the instruction of the new probe
190 * without calling any user handlers.
191 */
192 save_previous_kprobe();
193 set_current_kprobe(p, regs);
194 p->nmissed++;
195 prepare_singlestep(p, regs);
196 kprobe_status = KPROBE_REENTER;
197 return 1;
1da177e4
LT
198 } else {
199 p = current_kprobe;
200 if (p->break_handler && p->break_handler(p, regs)) {
201 goto ss_probe;
202 }
203 }
204 /* If it's not ours, can't be delete race, (we hold lock). */
205 goto no_kprobe;
206 }
207
208 lock_kprobes();
209 p = get_kprobe(addr);
210 if (!p) {
211 unlock_kprobes();
212 if (regs->eflags & VM_MASK) {
213 /* We are in virtual-8086 mode. Return 0 */
214 goto no_kprobe;
215 }
216
217 if (*addr != BREAKPOINT_INSTRUCTION) {
218 /*
219 * The breakpoint instruction was removed right
220 * after we hit it. Another cpu has removed
221 * either a probepoint or a debugger breakpoint
222 * at this address. In either case, no further
223 * handling of this interrupt is appropriate.
224 */
225 ret = 1;
226 }
227 /* Not one of ours: let kernel handle it */
228 goto no_kprobe;
229 }
230
231 kprobe_status = KPROBE_HIT_ACTIVE;
417c8da6 232 set_current_kprobe(p, regs);
1da177e4
LT
233
234 if (p->pre_handler && p->pre_handler(p, regs))
235 /* handler has already set things up, so skip ss setup */
236 return 1;
237
238ss_probe:
239 prepare_singlestep(p, regs);
240 kprobe_status = KPROBE_HIT_SS;
241 return 1;
242
243no_kprobe:
244 preempt_enable_no_resched();
245 return ret;
246}
247
b94cce92
HN
248/*
249 * For function-return probes, init_kprobes() establishes a probepoint
250 * here. When a retprobed function returns, this probe is hit and
251 * trampoline_probe_handler() runs, calling the kretprobe's handler.
252 */
253 void kretprobe_trampoline_holder(void)
254 {
255 asm volatile ( ".global kretprobe_trampoline\n"
256 "kretprobe_trampoline: \n"
257 "nop\n");
258 }
259
260/*
261 * Called when we hit the probe point at kretprobe_trampoline
262 */
3d97ae5b 263int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
b94cce92 264{
4bdbd37f
RL
265 struct kretprobe_instance *ri = NULL;
266 struct hlist_head *head;
267 struct hlist_node *node, *tmp;
268 unsigned long orig_ret_address = 0;
269 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
b94cce92 270
4bdbd37f 271 head = kretprobe_inst_table_head(current);
b94cce92 272
4bdbd37f
RL
273 /*
274 * It is possible to have multiple instances associated with a given
275 * task either because an multiple functions in the call path
276 * have a return probe installed on them, and/or more then one return
277 * return probe was registered for a target function.
278 *
279 * We can handle this because:
280 * - instances are always inserted at the head of the list
281 * - when multiple return probes are registered for the same
282 * function, the first instance's ret_addr will point to the
283 * real return address, and all the rest will point to
284 * kretprobe_trampoline
285 */
286 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
287 if (ri->task != current)
288 /* another task is sharing our hash bucket */
289 continue;
290
291 if (ri->rp && ri->rp->handler)
292 ri->rp->handler(ri, regs);
293
294 orig_ret_address = (unsigned long)ri->ret_addr;
b94cce92 295 recycle_rp_inst(ri);
4bdbd37f
RL
296
297 if (orig_ret_address != trampoline_address)
298 /*
299 * This is the real return address. Any other
300 * instances associated with this task are for
301 * other calls deeper on the call stack
302 */
303 break;
b94cce92 304 }
4bdbd37f
RL
305
306 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
307 regs->eip = orig_ret_address;
308
309 unlock_kprobes();
310 preempt_enable_no_resched();
311
312 /*
313 * By returning a non-zero value, we are telling
314 * kprobe_handler() that we have handled unlocking
315 * and re-enabling preemption.
316 */
317 return 1;
b94cce92
HN
318}
319
1da177e4
LT
320/*
321 * Called after single-stepping. p->addr is the address of the
322 * instruction whose first byte has been replaced by the "int 3"
323 * instruction. To avoid the SMP problems that can occur when we
324 * temporarily put back the original opcode to single-step, we
325 * single-stepped a copy of the instruction. The address of this
326 * copy is p->ainsn.insn.
327 *
328 * This function prepares to return from the post-single-step
329 * interrupt. We have to fix up the stack as follows:
330 *
331 * 0) Except in the case of absolute or indirect jump or call instructions,
332 * the new eip is relative to the copied instruction. We need to make
333 * it relative to the original instruction.
334 *
335 * 1) If the single-stepped instruction was pushfl, then the TF and IF
336 * flags are set in the just-pushed eflags, and may need to be cleared.
337 *
338 * 2) If the single-stepped instruction was a call, the return address
339 * that is atop the stack is the address following the copied instruction.
340 * We need to make it the address following the original instruction.
341 */
3d97ae5b 342static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
1da177e4
LT
343{
344 unsigned long *tos = (unsigned long *)&regs->esp;
345 unsigned long next_eip = 0;
346 unsigned long copy_eip = (unsigned long)&p->ainsn.insn;
347 unsigned long orig_eip = (unsigned long)p->addr;
348
349 switch (p->ainsn.insn[0]) {
350 case 0x9c: /* pushfl */
351 *tos &= ~(TF_MASK | IF_MASK);
352 *tos |= kprobe_old_eflags;
353 break;
0b9e2cac
PP
354 case 0xc3: /* ret/lret */
355 case 0xcb:
356 case 0xc2:
357 case 0xca:
358 regs->eflags &= ~TF_MASK;
359 /* eip is already adjusted, no more changes required*/
360 return;
1da177e4
LT
361 case 0xe8: /* call relative - Fix return addr */
362 *tos = orig_eip + (*tos - copy_eip);
363 break;
364 case 0xff:
365 if ((p->ainsn.insn[1] & 0x30) == 0x10) {
366 /* call absolute, indirect */
367 /* Fix return addr; eip is correct. */
368 next_eip = regs->eip;
369 *tos = orig_eip + (*tos - copy_eip);
370 } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
371 ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
372 /* eip is correct. */
373 next_eip = regs->eip;
374 }
375 break;
376 case 0xea: /* jmp absolute -- eip is correct */
377 next_eip = regs->eip;
378 break;
379 default:
380 break;
381 }
382
383 regs->eflags &= ~TF_MASK;
384 if (next_eip) {
385 regs->eip = next_eip;
386 } else {
387 regs->eip = orig_eip + (regs->eip - copy_eip);
388 }
389}
390
391/*
392 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
393 * remain disabled thoroughout this function. And we hold kprobe lock.
394 */
395static inline int post_kprobe_handler(struct pt_regs *regs)
396{
397 if (!kprobe_running())
398 return 0;
399
417c8da6
PP
400 if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) {
401 kprobe_status = KPROBE_HIT_SSDONE;
1da177e4 402 current_kprobe->post_handler(current_kprobe, regs, 0);
417c8da6 403 }
1da177e4 404
4bdbd37f 405 resume_execution(current_kprobe, regs);
1da177e4
LT
406 regs->eflags |= kprobe_saved_eflags;
407
417c8da6
PP
408 /*Restore back the original saved kprobes variables and continue. */
409 if (kprobe_status == KPROBE_REENTER) {
410 restore_previous_kprobe();
411 goto out;
412 }
1da177e4 413 unlock_kprobes();
417c8da6 414out:
1da177e4
LT
415 preempt_enable_no_resched();
416
417 /*
418 * if somebody else is singlestepping across a probe point, eflags
419 * will have TF set, in which case, continue the remaining processing
420 * of do_debug, as if this is not a probe hit.
421 */
422 if (regs->eflags & TF_MASK)
423 return 0;
424
425 return 1;
426}
427
428/* Interrupts disabled, kprobe_lock held. */
429static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
430{
431 if (current_kprobe->fault_handler
432 && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
433 return 1;
434
435 if (kprobe_status & KPROBE_HIT_SS) {
436 resume_execution(current_kprobe, regs);
437 regs->eflags |= kprobe_old_eflags;
438
439 unlock_kprobes();
440 preempt_enable_no_resched();
441 }
442 return 0;
443}
444
445/*
446 * Wrapper routine to for handling exceptions.
447 */
3d97ae5b
PP
448int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
449 unsigned long val, void *data)
1da177e4
LT
450{
451 struct die_args *args = (struct die_args *)data;
452 switch (val) {
453 case DIE_INT3:
454 if (kprobe_handler(args->regs))
455 return NOTIFY_STOP;
456 break;
457 case DIE_DEBUG:
458 if (post_kprobe_handler(args->regs))
459 return NOTIFY_STOP;
460 break;
461 case DIE_GPF:
462 if (kprobe_running() &&
463 kprobe_fault_handler(args->regs, args->trapnr))
464 return NOTIFY_STOP;
465 break;
466 case DIE_PAGE_FAULT:
467 if (kprobe_running() &&
468 kprobe_fault_handler(args->regs, args->trapnr))
469 return NOTIFY_STOP;
470 break;
471 default:
472 break;
473 }
474 return NOTIFY_DONE;
475}
476
3d97ae5b 477int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
1da177e4
LT
478{
479 struct jprobe *jp = container_of(p, struct jprobe, kp);
480 unsigned long addr;
481
482 jprobe_saved_regs = *regs;
483 jprobe_saved_esp = &regs->esp;
484 addr = (unsigned long)jprobe_saved_esp;
485
486 /*
487 * TBD: As Linus pointed out, gcc assumes that the callee
488 * owns the argument space and could overwrite it, e.g.
489 * tailcall optimization. So, to be absolutely safe
490 * we also save and restore enough stack bytes to cover
491 * the argument area.
492 */
493 memcpy(jprobes_stack, (kprobe_opcode_t *) addr, MIN_STACK_SIZE(addr));
494 regs->eflags &= ~IF_MASK;
495 regs->eip = (unsigned long)(jp->entry);
496 return 1;
497}
498
3d97ae5b 499void __kprobes jprobe_return(void)
1da177e4
LT
500{
501 preempt_enable_no_resched();
502 asm volatile (" xchgl %%ebx,%%esp \n"
503 " int3 \n"
504 " .globl jprobe_return_end \n"
505 " jprobe_return_end: \n"
506 " nop \n"::"b"
507 (jprobe_saved_esp):"memory");
508}
509
3d97ae5b 510int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
1da177e4
LT
511{
512 u8 *addr = (u8 *) (regs->eip - 1);
513 unsigned long stack_addr = (unsigned long)jprobe_saved_esp;
514 struct jprobe *jp = container_of(p, struct jprobe, kp);
515
516 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
517 if (&regs->esp != jprobe_saved_esp) {
518 struct pt_regs *saved_regs =
519 container_of(jprobe_saved_esp, struct pt_regs, esp);
520 printk("current esp %p does not match saved esp %p\n",
521 &regs->esp, jprobe_saved_esp);
522 printk("Saved registers for jprobe %p\n", jp);
523 show_registers(saved_regs);
524 printk("Current registers\n");
525 show_registers(regs);
526 BUG();
527 }
528 *regs = jprobe_saved_regs;
529 memcpy((kprobe_opcode_t *) stack_addr, jprobes_stack,
530 MIN_STACK_SIZE(stack_addr));
531 return 1;
532 }
533 return 0;
534}
4bdbd37f
RL
535
536static struct kprobe trampoline_p = {
537 .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
538 .pre_handler = trampoline_probe_handler
539};
540
6772926b 541int __init arch_init_kprobes(void)
4bdbd37f
RL
542{
543 return register_kprobe(&trampoline_p);
544}