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