This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / x86_64 / kernel / kprobes.c
1 /*
2  *  Kernel Probes (KProbes)
3  *  arch/x86_64/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  * 2004-Oct     Jim Keniston <kenistoj@us.ibm.com> and Prasanna S Panchamukhi
27  *              <prasanna@in.ibm.com> adapted for x86_64
28  */
29
30 #include <linux/config.h>
31 #include <linux/kprobes.h>
32 #include <linux/ptrace.h>
33 #include <linux/spinlock.h>
34 #include <linux/string.h>
35 #include <linux/slab.h>
36 #include <linux/preempt.h>
37 #include <linux/vmalloc.h>
38
39 #include <asm/pgtable.h>
40 #include <asm/kdebug.h>
41
42 /* kprobe_status settings */
43 #define KPROBE_HIT_ACTIVE       0x00000001
44 #define KPROBE_HIT_SS           0x00000002
45
46 static struct kprobe *current_kprobe;
47 static unsigned long kprobe_status, kprobe_old_rflags, kprobe_saved_rflags;
48 static struct pt_regs jprobe_saved_regs;
49 static long *jprobe_saved_rsp;
50 static kprobe_opcode_t *get_insn_slot(void);
51 static void free_insn_slot(kprobe_opcode_t *slot);
52 void jprobe_return_end(void);
53
54 /* copy of the kernel stack at the probe fire time */
55 static kprobe_opcode_t jprobes_stack[MAX_STACK_SIZE];
56
57 /*
58  * returns non-zero if opcode modifies the interrupt flag.
59  */
60 static inline int is_IF_modifier(kprobe_opcode_t *insn)
61 {
62         switch (*insn) {
63         case 0xfa:              /* cli */
64         case 0xfb:              /* sti */
65         case 0xcf:              /* iret/iretd */
66         case 0x9d:              /* popf/popfd */
67                 return 1;
68         }
69
70         if (*insn  >= 0x40 && *insn <= 0x4f && *++insn == 0xcf)
71                 return 1;
72         return 0;
73 }
74
75 int arch_prepare_kprobe(struct kprobe *p)
76 {
77         /* insn: must be on special executable page on x86_64. */
78         p->ainsn.insn = get_insn_slot();
79         if (!p->ainsn.insn) {
80                 return -ENOMEM;
81         }
82         memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE);
83         return 0;
84 }
85
86 void arch_remove_kprobe(struct kprobe *p)
87 {
88         free_insn_slot(p->ainsn.insn);
89 }
90
91 static inline void disarm_kprobe(struct kprobe *p, struct pt_regs *regs)
92 {
93         *p->addr = p->opcode;
94         regs->rip = (unsigned long)p->addr;
95 }
96
97 static void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
98 {
99         regs->eflags |= TF_MASK;
100         regs->eflags &= ~IF_MASK;
101
102         regs->rip = (unsigned long)p->ainsn.insn;
103 }
104
105 /*
106  * Interrupts are disabled on entry as trap3 is an interrupt gate and they
107  * remain disabled thorough out this function.
108  */
109 int kprobe_handler(struct pt_regs *regs)
110 {
111         struct kprobe *p;
112         int ret = 0;
113         kprobe_opcode_t *addr = (kprobe_opcode_t *)(regs->rip - sizeof(kprobe_opcode_t));
114
115         /* We're in an interrupt, but this is clear and BUG()-safe. */
116         preempt_disable();
117
118         /* Check we're not actually recursing */
119         if (kprobe_running()) {
120                 /* We *are* holding lock here, so this is safe.
121                    Disarm the probe we just hit, and ignore it. */
122                 p = get_kprobe(addr);
123                 if (p) {
124                         disarm_kprobe(p, regs);
125                         ret = 1;
126                 } else {
127                         p = current_kprobe;
128                         if (p->break_handler && p->break_handler(p, regs)) {
129                                 goto ss_probe;
130                         }
131                 }
132                 /* If it's not ours, can't be delete race, (we hold lock). */
133                 goto no_kprobe;
134         }
135
136         lock_kprobes();
137         p = get_kprobe(addr);
138         if (!p) {
139                 unlock_kprobes();
140                 if (*addr != BREAKPOINT_INSTRUCTION) {
141                         /*
142                          * The breakpoint instruction was removed right
143                          * after we hit it.  Another cpu has removed
144                          * either a probepoint or a debugger breakpoint
145                          * at this address.  In either case, no further
146                          * handling of this interrupt is appropriate.
147                          */
148                         ret = 1;
149                 }
150                 /* Not one of ours: let kernel handle it */
151                 goto no_kprobe;
152         }
153
154         kprobe_status = KPROBE_HIT_ACTIVE;
155         current_kprobe = p;
156         kprobe_saved_rflags = kprobe_old_rflags
157             = (regs->eflags & (TF_MASK | IF_MASK));
158         if (is_IF_modifier(p->ainsn.insn))
159                 kprobe_saved_rflags &= ~IF_MASK;
160
161         if (p->pre_handler(p, regs)) {
162                 /* handler has already set things up, so skip ss setup */
163                 return 1;
164         }
165
166       ss_probe:
167         prepare_singlestep(p, regs);
168         kprobe_status = KPROBE_HIT_SS;
169         return 1;
170
171       no_kprobe:
172         preempt_enable_no_resched();
173         return ret;
174 }
175
176 /*
177  * Called after single-stepping.  p->addr is the address of the
178  * instruction whose first byte has been replaced by the "int 3"
179  * instruction.  To avoid the SMP problems that can occur when we
180  * temporarily put back the original opcode to single-step, we
181  * single-stepped a copy of the instruction.  The address of this
182  * copy is p->ainsn.insn.
183  *
184  * This function prepares to return from the post-single-step
185  * interrupt.  We have to fix up the stack as follows:
186  *
187  * 0) Except in the case of absolute or indirect jump or call instructions,
188  * the new rip is relative to the copied instruction.  We need to make
189  * it relative to the original instruction.
190  *
191  * 1) If the single-stepped instruction was pushfl, then the TF and IF
192  * flags are set in the just-pushed eflags, and may need to be cleared.
193  *
194  * 2) If the single-stepped instruction was a call, the return address
195  * that is atop the stack is the address following the copied instruction.
196  * We need to make it the address following the original instruction.
197  */
198 static void resume_execution(struct kprobe *p, struct pt_regs *regs)
199 {
200         unsigned long *tos = (unsigned long *)regs->rsp;
201         unsigned long next_rip = 0;
202         unsigned long copy_rip = (unsigned long)p->ainsn.insn;
203         unsigned long orig_rip = (unsigned long)p->addr;
204         kprobe_opcode_t *insn = p->ainsn.insn;
205
206         /*skip the REX prefix*/
207         if (*insn >= 0x40 && *insn <= 0x4f)
208                 insn++;
209
210         switch (*insn) {
211         case 0x9c:              /* pushfl */
212                 *tos &= ~(TF_MASK | IF_MASK);
213                 *tos |= kprobe_old_rflags;
214                 break;
215         case 0xe8:              /* call relative - Fix return addr */
216                 *tos = orig_rip + (*tos - copy_rip);
217                 break;
218         case 0xff:
219                 if ((*insn & 0x30) == 0x10) {
220                         /* call absolute, indirect */
221                         /* Fix return addr; rip is correct. */
222                         next_rip = regs->rip;
223                         *tos = orig_rip + (*tos - copy_rip);
224                 } else if (((*insn & 0x31) == 0x20) ||  /* jmp near, absolute indirect */
225                            ((*insn & 0x31) == 0x21)) {  /* jmp far, absolute indirect */
226                         /* rip is correct. */
227                         next_rip = regs->rip;
228                 }
229                 break;
230         case 0xea:              /* jmp absolute -- rip is correct */
231                 next_rip = regs->rip;
232                 break;
233         default:
234                 break;
235         }
236
237         regs->eflags &= ~TF_MASK;
238         if (next_rip) {
239                 regs->rip = next_rip;
240         } else {
241                 regs->rip = orig_rip + (regs->rip - copy_rip);
242         }
243 }
244
245 /*
246  * Interrupts are disabled on entry as trap1 is an interrupt gate and they
247  * remain disabled thoroughout this function.  And we hold kprobe lock.
248  */
249 int post_kprobe_handler(struct pt_regs *regs)
250 {
251         if (!kprobe_running())
252                 return 0;
253
254         if (current_kprobe->post_handler)
255                 current_kprobe->post_handler(current_kprobe, regs, 0);
256
257         resume_execution(current_kprobe, regs);
258         regs->eflags |= kprobe_saved_rflags;
259
260         unlock_kprobes();
261         preempt_enable_no_resched();
262
263         /*
264          * if somebody else is singlestepping across a probe point, eflags
265          * will have TF set, in which case, continue the remaining processing
266          * of do_debug, as if this is not a probe hit.
267          */
268         if (regs->eflags & TF_MASK)
269                 return 0;
270
271         return 1;
272 }
273
274 /* Interrupts disabled, kprobe_lock held. */
275 int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
276 {
277         if (current_kprobe->fault_handler
278             && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
279                 return 1;
280
281         if (kprobe_status & KPROBE_HIT_SS) {
282                 resume_execution(current_kprobe, regs);
283                 regs->eflags |= kprobe_old_rflags;
284
285                 unlock_kprobes();
286                 preempt_enable_no_resched();
287         }
288         return 0;
289 }
290
291 /*
292  * Wrapper routine for handling exceptions.
293  */
294 int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
295                              void *data)
296 {
297         struct die_args *args = (struct die_args *)data;
298         switch (val) {
299         case DIE_INT3:
300                 if (kprobe_handler(args->regs))
301                         return NOTIFY_STOP;
302                 break;
303         case DIE_DEBUG:
304                 if (post_kprobe_handler(args->regs))
305                         return NOTIFY_STOP;
306                 break;
307         case DIE_GPF:
308                 if (kprobe_running() &&
309                     kprobe_fault_handler(args->regs, args->trapnr))
310                         return NOTIFY_STOP;
311                 break;
312         case DIE_PAGE_FAULT:
313                 if (kprobe_running() &&
314                     kprobe_fault_handler(args->regs, args->trapnr))
315                         return NOTIFY_STOP;
316                 break;
317         default:
318                 break;
319         }
320         return NOTIFY_DONE;
321 }
322
323 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
324 {
325         struct jprobe *jp = container_of(p, struct jprobe, kp);
326         unsigned long addr;
327
328         jprobe_saved_regs = *regs;
329         jprobe_saved_rsp = (long *) regs->rsp;
330         addr = (unsigned long)jprobe_saved_rsp;
331         /*
332          * As Linus pointed out, gcc assumes that the callee
333          * owns the argument space and could overwrite it, e.g.
334          * tailcall optimization. So, to be absolutely safe
335          * we also save and restore enough stack bytes to cover
336          * the argument area.
337          */
338         memcpy(jprobes_stack, (kprobe_opcode_t *) addr, MIN_STACK_SIZE(addr));
339         regs->eflags &= ~IF_MASK;
340         regs->rip = (unsigned long)(jp->entry);
341         return 1;
342 }
343
344 void jprobe_return(void)
345 {
346         preempt_enable_no_resched();
347         asm volatile ("       xchg   %%rbx,%%rsp     \n"
348                       "       int3                      \n"
349                       "       .globl jprobe_return_end  \n"
350                       "       jprobe_return_end:        \n"
351                       "       nop                       \n"::"b"
352                       (jprobe_saved_rsp):"memory");
353 }
354
355 int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
356 {
357         u8 *addr = (u8 *) (regs->rip - 1);
358         unsigned long stack_addr = (unsigned long)jprobe_saved_rsp;
359         struct jprobe *jp = container_of(p, struct jprobe, kp);
360
361         if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
362                 if ((long *)regs->rsp != jprobe_saved_rsp) {
363                         struct pt_regs *saved_regs =
364                             container_of(jprobe_saved_rsp, struct pt_regs, rsp);
365                         printk("current rsp %p does not match saved rsp %p\n",
366                                (long *)regs->rsp, jprobe_saved_rsp);
367                         printk("Saved registers for jprobe %p\n", jp);
368                         show_registers(saved_regs);
369                         printk("Current registers\n");
370                         show_registers(regs);
371                         BUG();
372                 }
373                 *regs = jprobe_saved_regs;
374                 memcpy((kprobe_opcode_t *) stack_addr, jprobes_stack,
375                        MIN_STACK_SIZE(stack_addr));
376                 return 1;
377         }
378         return 0;
379 }
380
381 /*
382  * kprobe->ainsn.insn points to the copy of the instruction to be single-stepped.
383  * By default on x86_64, pages we get from kmalloc or vmalloc are not
384  * executable.  Single-stepping an instruction on such a page yields an
385  * oops.  So instead of storing the instruction copies in their respective
386  * kprobe objects, we allocate a page, map it executable, and store all the
387  * instruction copies there.  (We can allocate additional pages if somebody
388  * inserts a huge number of probes.)  Each page can hold up to INSNS_PER_PAGE
389  * instruction slots, each of which is MAX_INSN_SIZE*sizeof(kprobe_opcode_t)
390  * bytes.
391  */
392 #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE*sizeof(kprobe_opcode_t)))
393 struct kprobe_insn_page {
394         struct hlist_node hlist;
395         kprobe_opcode_t *insns;         /* page of instruction slots */
396         char slot_used[INSNS_PER_PAGE];
397         int nused;
398 };
399
400 static struct hlist_head kprobe_insn_pages;
401
402 /**
403  * get_insn_slot() - Find a slot on an executable page for an instruction.
404  * We allocate an executable page if there's no room on existing ones.
405  */
406 static kprobe_opcode_t *get_insn_slot(void)
407 {
408         struct kprobe_insn_page *kip;
409         struct hlist_node *pos;
410
411         hlist_for_each(pos, &kprobe_insn_pages) {
412                 kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
413                 if (kip->nused < INSNS_PER_PAGE) {
414                         int i;
415                         for (i = 0; i < INSNS_PER_PAGE; i++) {
416                                 if (!kip->slot_used[i]) {
417                                         kip->slot_used[i] = 1;
418                                         kip->nused++;
419                                         return kip->insns + (i*MAX_INSN_SIZE);
420                                 }
421                         }
422                         /* Surprise!  No unused slots.  Fix kip->nused. */
423                         kip->nused = INSNS_PER_PAGE;
424                 }
425         }
426
427         /* All out of space.  Need to allocate a new page. Use slot 0.*/
428         kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_ATOMIC);
429         if (!kip) {
430                 return NULL;
431         }
432         kip->insns = (kprobe_opcode_t*) __vmalloc(PAGE_SIZE,
433                 GFP_ATOMIC|__GFP_HIGHMEM, __pgprot(__PAGE_KERNEL_EXEC));
434         if (!kip->insns) {
435                 kfree(kip);
436                 return NULL;
437         }
438         INIT_HLIST_NODE(&kip->hlist);
439         hlist_add_head(&kip->hlist, &kprobe_insn_pages);
440         memset(kip->slot_used, 0, INSNS_PER_PAGE);
441         kip->slot_used[0] = 1;
442         kip->nused = 1;
443         return kip->insns;
444 }
445
446 /**
447  * free_insn_slot() - Free instruction slot obtained from get_insn_slot().
448  */
449 static void free_insn_slot(kprobe_opcode_t *slot)
450 {
451         struct kprobe_insn_page *kip;
452         struct hlist_node *pos;
453
454         hlist_for_each(pos, &kprobe_insn_pages) {
455                 kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
456                 if (kip->insns <= slot
457                     && slot < kip->insns+(INSNS_PER_PAGE*MAX_INSN_SIZE)) {
458                         int i = (slot - kip->insns) / MAX_INSN_SIZE;
459                         kip->slot_used[i] = 0;
460                         kip->nused--;
461                         if (kip->nused == 0) {
462                                 /*
463                                  * Page is no longer in use.  Free it unless
464                                  * it's the last one.  We keep the last one
465                                  * so as not to have to set it up again the
466                                  * next time somebody inserts a probe.
467                                  */
468                                 hlist_del(&kip->hlist);
469                                 if (hlist_empty(&kprobe_insn_pages)) {
470                                         INIT_HLIST_NODE(&kip->hlist);
471                                         hlist_add_head(&kip->hlist,
472                                                 &kprobe_insn_pages);
473                                 } else {
474                                         vfree(kip->insns);
475                                         kfree(kip);
476                                 }
477                         }
478                         return;
479                 }
480         }
481 }