This commit was manufactured by cvs2svn to create branch 'vserver'.
[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  */
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
35 /* kprobe_status settings */
36 #define KPROBE_HIT_ACTIVE       0x00000001
37 #define KPROBE_HIT_SS           0x00000002
38
39 static struct kprobe *current_kprobe;
40 static unsigned long kprobe_status, kprobe_old_eflags, kprobe_saved_eflags;
41 static struct pt_regs jprobe_saved_regs;
42 static long *jprobe_saved_esp;
43 /* copy of the kernel stack at the probe fire time */
44 static kprobe_opcode_t jprobes_stack[MAX_STACK_SIZE];
45
46 /*
47  * returns non-zero if opcode modifies the interrupt flag.
48  */
49 static inline int is_IF_modifier(kprobe_opcode_t opcode)
50 {
51         switch (opcode) {
52         case 0xfa:              /* cli */
53         case 0xfb:              /* sti */
54         case 0xcf:              /* iret/iretd */
55         case 0x9d:              /* popf/popfd */
56                 return 1;
57         }
58         return 0;
59 }
60
61 void arch_prepare_kprobe(struct kprobe *p)
62 {
63         memcpy(p->insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
64 }
65
66 static inline void disarm_kprobe(struct kprobe *p, struct pt_regs *regs)
67 {
68         *p->addr = p->opcode;
69         regs->eip = (unsigned long)p->addr;
70 }
71
72 static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
73 {
74         regs->eflags |= TF_MASK;
75         regs->eflags &= ~IF_MASK;
76         regs->eip = (unsigned long)&p->insn;
77 }
78
79 /*
80  * Interrupts are disabled on entry as trap3 is an interrupt gate and they
81  * remain disabled thorough out this function.
82  */
83 static inline int kprobe_handler(struct pt_regs *regs)
84 {
85         struct kprobe *p;
86         int ret = 0;
87         u8 *addr = (u8 *) (regs->eip - 1);
88
89         /* We're in an interrupt, but this is clear and BUG()-safe. */
90         preempt_disable();
91
92         /* Check we're not actually recursing */
93         if (kprobe_running()) {
94                 /* We *are* holding lock here, so this is safe.
95                    Disarm the probe we just hit, and ignore it. */
96                 p = get_kprobe(addr);
97                 if (p) {
98                         disarm_kprobe(p, regs);
99                         ret = 1;
100                 } else {
101                         p = current_kprobe;
102                         if (p->break_handler && p->break_handler(p, regs)) {
103                                 goto ss_probe;
104                         }
105                 }
106                 /* If it's not ours, can't be delete race, (we hold lock). */
107                 goto no_kprobe;
108         }
109
110         lock_kprobes();
111         p = get_kprobe(addr);
112         if (!p) {
113                 unlock_kprobes();
114                 if (*addr != BREAKPOINT_INSTRUCTION) {
115                         /*
116                          * The breakpoint instruction was removed right
117                          * after we hit it.  Another cpu has removed
118                          * either a probepoint or a debugger breakpoint
119                          * at this address.  In either case, no further
120                          * handling of this interrupt is appropriate.
121                          */
122                         ret = 1;
123                 }
124                 /* Not one of ours: let kernel handle it */
125                 goto no_kprobe;
126         }
127
128         kprobe_status = KPROBE_HIT_ACTIVE;
129         current_kprobe = p;
130         kprobe_saved_eflags = kprobe_old_eflags
131             = (regs->eflags & (TF_MASK | IF_MASK));
132         if (is_IF_modifier(p->opcode))
133                 kprobe_saved_eflags &= ~IF_MASK;
134
135         if (p->pre_handler(p, regs)) {
136                 /* handler has already set things up, so skip ss setup */
137                 return 1;
138         }
139
140       ss_probe:
141         prepare_singlestep(p, regs);
142         kprobe_status = KPROBE_HIT_SS;
143         return 1;
144
145       no_kprobe:
146         preempt_enable_no_resched();
147         return ret;
148 }
149
150 /*
151  * Called after single-stepping.  p->addr is the address of the
152  * instruction whose first byte has been replaced by the "int 3"
153  * instruction.  To avoid the SMP problems that can occur when we
154  * temporarily put back the original opcode to single-step, we
155  * single-stepped a copy of the instruction.  The address of this
156  * copy is p->insn.
157  *
158  * This function prepares to return from the post-single-step
159  * interrupt.  We have to fix up the stack as follows:
160  *
161  * 0) Except in the case of absolute or indirect jump or call instructions,
162  * the new eip is relative to the copied instruction.  We need to make
163  * it relative to the original instruction.
164  *
165  * 1) If the single-stepped instruction was pushfl, then the TF and IF
166  * flags are set in the just-pushed eflags, and may need to be cleared.
167  *
168  * 2) If the single-stepped instruction was a call, the return address
169  * that is atop the stack is the address following the copied instruction.
170  * We need to make it the address following the original instruction.
171  */
172 static void resume_execution(struct kprobe *p, struct pt_regs *regs)
173 {
174         unsigned long *tos = (unsigned long *)&regs->esp;
175         unsigned long next_eip = 0;
176         unsigned long copy_eip = (unsigned long)&p->insn;
177         unsigned long orig_eip = (unsigned long)p->addr;
178
179         switch (p->insn[0]) {
180         case 0x9c:              /* pushfl */
181                 *tos &= ~(TF_MASK | IF_MASK);
182                 *tos |= kprobe_old_eflags;
183                 break;
184         case 0xe8:              /* call relative - Fix return addr */
185                 *tos = orig_eip + (*tos - copy_eip);
186                 break;
187         case 0xff:
188                 if ((p->insn[1] & 0x30) == 0x10) {
189                         /* call absolute, indirect */
190                         /* Fix return addr; eip is correct. */
191                         next_eip = regs->eip;
192                         *tos = orig_eip + (*tos - copy_eip);
193                 } else if (((p->insn[1] & 0x31) == 0x20) ||     /* jmp near, absolute indirect */
194                            ((p->insn[1] & 0x31) == 0x21)) {     /* jmp far, absolute indirect */
195                         /* eip is correct. */
196                         next_eip = regs->eip;
197                 }
198                 break;
199         case 0xea:              /* jmp absolute -- eip is correct */
200                 next_eip = regs->eip;
201                 break;
202         default:
203                 break;
204         }
205
206         regs->eflags &= ~TF_MASK;
207         if (next_eip) {
208                 regs->eip = next_eip;
209         } else {
210                 regs->eip = orig_eip + (regs->eip - copy_eip);
211         }
212 }
213
214 /*
215  * Interrupts are disabled on entry as trap1 is an interrupt gate and they
216  * remain disabled thoroughout this function.  And we hold kprobe lock.
217  */
218 static inline int post_kprobe_handler(struct pt_regs *regs)
219 {
220         if (!kprobe_running())
221                 return 0;
222
223         if (current_kprobe->post_handler)
224                 current_kprobe->post_handler(current_kprobe, regs, 0);
225
226         resume_execution(current_kprobe, regs);
227         regs->eflags |= kprobe_saved_eflags;
228
229         unlock_kprobes();
230         preempt_enable_no_resched();
231
232         /*
233          * if somebody else is singlestepping across a probe point, eflags
234          * will have TF set, in which case, continue the remaining processing
235          * of do_debug, as if this is not a probe hit.
236          */
237         if (regs->eflags & TF_MASK)
238                 return 0;
239
240         return 1;
241 }
242
243 /* Interrupts disabled, kprobe_lock held. */
244 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
245 {
246         if (current_kprobe->fault_handler
247             && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
248                 return 1;
249
250         if (kprobe_status & KPROBE_HIT_SS) {
251                 resume_execution(current_kprobe, regs);
252                 regs->eflags |= kprobe_old_eflags;
253
254                 unlock_kprobes();
255                 preempt_enable_no_resched();
256         }
257         return 0;
258 }
259
260 /*
261  * Wrapper routine to for handling exceptions.
262  */
263 int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
264                              void *data)
265 {
266         struct die_args *args = (struct die_args *)data;
267         switch (val) {
268         case DIE_INT3:
269                 if (kprobe_handler(args->regs))
270                         return NOTIFY_STOP;
271                 break;
272         case DIE_DEBUG:
273                 if (post_kprobe_handler(args->regs))
274                         return NOTIFY_STOP;
275                 break;
276         case DIE_GPF:
277                 if (kprobe_running() &&
278                     kprobe_fault_handler(args->regs, args->trapnr))
279                         return NOTIFY_STOP;
280                 break;
281         case DIE_PAGE_FAULT:
282                 if (kprobe_running() &&
283                     kprobe_fault_handler(args->regs, args->trapnr))
284                         return NOTIFY_STOP;
285                 break;
286         default:
287                 break;
288         }
289         return NOTIFY_DONE;
290 }
291
292 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
293 {
294         struct jprobe *jp = container_of(p, struct jprobe, kp);
295         unsigned long addr;
296
297         jprobe_saved_regs = *regs;
298         jprobe_saved_esp = &regs->esp;
299         addr = (unsigned long)jprobe_saved_esp;
300
301         /*
302          * TBD: As Linus pointed out, gcc assumes that the callee
303          * owns the argument space and could overwrite it, e.g.
304          * tailcall optimization. So, to be absolutely safe
305          * we also save and restore enough stack bytes to cover
306          * the argument area.
307          */
308         memcpy(jprobes_stack, (kprobe_opcode_t *) addr, MIN_STACK_SIZE(addr));
309         regs->eflags &= ~IF_MASK;
310         regs->eip = (unsigned long)(jp->entry);
311         return 1;
312 }
313
314 void jprobe_return(void)
315 {
316         preempt_enable_no_resched();
317         asm volatile ("       xchgl   %%ebx,%%esp     \n"
318                       "       int3                      \n"::"b"
319                       (jprobe_saved_esp):"memory");
320 }
321 void jprobe_return_end(void)
322 {
323 };
324
325 int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
326 {
327         u8 *addr = (u8 *) (regs->eip - 1);
328         unsigned long stack_addr = (unsigned long)jprobe_saved_esp;
329         struct jprobe *jp = container_of(p, struct jprobe, kp);
330
331         if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
332                 if (&regs->esp != jprobe_saved_esp) {
333                         struct pt_regs *saved_regs =
334                             container_of(jprobe_saved_esp, struct pt_regs, esp);
335                         printk("current esp %p does not match saved esp %p\n",
336                                &regs->esp, jprobe_saved_esp);
337                         printk("Saved registers for jprobe %p\n", jp);
338                         show_registers(saved_regs);
339                         printk("Current registers\n");
340                         show_registers(regs);
341                         BUG();
342                 }
343                 *regs = jprobe_saved_regs;
344                 memcpy((kprobe_opcode_t *) stack_addr, jprobes_stack,
345                        MIN_STACK_SIZE(stack_addr));
346                 return 1;
347         }
348         return 0;
349 }