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