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