]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - arch/x86/kernel/kprobes_32.c
x86: optimize page faults like all other achitectures and kill notifier cruft
[linux-2.6.git] / arch / x86 / kernel / kprobes_32.c
1 /*
2  *  Kernel Probes (KProbes)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * Copyright (C) IBM Corporation, 2002, 2004
19  *
20  * 2002-Oct     Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
21  *              Probes initial implementation ( includes contributions from
22  *              Rusty Russell).
23  * 2004-July    Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
24  *              interface to access function arguments.
25  * 2005-May     Hien Nguyen <hien@us.ibm.com>, Jim Keniston
26  *              <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
27  *              <prasanna@in.ibm.com> added function-return probes.
28  */
29
30 #include <linux/kprobes.h>
31 #include <linux/ptrace.h>
32 #include <linux/preempt.h>
33 #include <linux/kdebug.h>
34 #include <asm/cacheflush.h>
35 #include <asm/desc.h>
36 #include <asm/uaccess.h>
37 #include <asm/alternative.h>
38
39 void jprobe_return_end(void);
40
41 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
42 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
43
44 /* insert a jmp code */
45 static __always_inline void set_jmp_op(void *from, void *to)
46 {
47         struct __arch_jmp_op {
48                 char op;
49                 long raddr;
50         } __attribute__((packed)) *jop;
51         jop = (struct __arch_jmp_op *)from;
52         jop->raddr = (long)(to) - ((long)(from) + 5);
53         jop->op = RELATIVEJUMP_INSTRUCTION;
54 }
55
56 /*
57  * returns non-zero if opcodes can be boosted.
58  */
59 static __always_inline int can_boost(kprobe_opcode_t *opcodes)
60 {
61 #define W(row,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bb,bc,bd,be,bf)                \
62         (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) |   \
63           (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) |   \
64           (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) |   \
65           (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf))    \
66          << (row % 32))
67         /*
68          * Undefined/reserved opcodes, conditional jump, Opcode Extension
69          * Groups, and some special opcodes can not be boost.
70          */
71         static const unsigned long twobyte_is_boostable[256 / 32] = {
72                 /*      0 1 2 3 4 5 6 7 8 9 a b c d e f         */
73                 /*      -------------------------------         */
74                 W(0x00, 0,0,1,1,0,0,1,0,1,1,0,0,0,0,0,0)| /* 00 */
75                 W(0x10, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 10 */
76                 W(0x20, 1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0)| /* 20 */
77                 W(0x30, 0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 30 */
78                 W(0x40, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 40 */
79                 W(0x50, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 50 */
80                 W(0x60, 1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1)| /* 60 */
81                 W(0x70, 0,0,0,0,1,1,1,1,0,0,0,0,0,0,1,1), /* 70 */
82                 W(0x80, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 80 */
83                 W(0x90, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1), /* 90 */
84                 W(0xa0, 1,1,0,1,1,1,0,0,1,1,0,1,1,1,0,1)| /* a0 */
85                 W(0xb0, 1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1), /* b0 */
86                 W(0xc0, 1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1)| /* c0 */
87                 W(0xd0, 0,1,1,1,0,1,0,0,1,1,0,1,1,1,0,1), /* d0 */
88                 W(0xe0, 0,1,1,0,0,1,0,0,1,1,0,1,1,1,0,1)| /* e0 */
89                 W(0xf0, 0,1,1,1,0,1,0,0,1,1,1,0,1,1,1,0)  /* f0 */
90                 /*      -------------------------------         */
91                 /*      0 1 2 3 4 5 6 7 8 9 a b c d e f         */
92         };
93 #undef W
94         kprobe_opcode_t opcode;
95         kprobe_opcode_t *orig_opcodes = opcodes;
96 retry:
97         if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
98                 return 0;
99         opcode = *(opcodes++);
100
101         /* 2nd-byte opcode */
102         if (opcode == 0x0f) {
103                 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
104                         return 0;
105                 return test_bit(*opcodes, twobyte_is_boostable);
106         }
107
108         switch (opcode & 0xf0) {
109         case 0x60:
110                 if (0x63 < opcode && opcode < 0x67)
111                         goto retry; /* prefixes */
112                 /* can't boost Address-size override and bound */
113                 return (opcode != 0x62 && opcode != 0x67);
114         case 0x70:
115                 return 0; /* can't boost conditional jump */
116         case 0xc0:
117                 /* can't boost software-interruptions */
118                 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
119         case 0xd0:
120                 /* can boost AA* and XLAT */
121                 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
122         case 0xe0:
123                 /* can boost in/out and absolute jmps */
124                 return ((opcode & 0x04) || opcode == 0xea);
125         case 0xf0:
126                 if ((opcode & 0x0c) == 0 && opcode != 0xf1)
127                         goto retry; /* lock/rep(ne) prefix */
128                 /* clear and set flags can be boost */
129                 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
130         default:
131                 if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
132                         goto retry; /* prefixes */
133                 /* can't boost CS override and call */
134                 return (opcode != 0x2e && opcode != 0x9a);
135         }
136 }
137
138 /*
139  * returns non-zero if opcode modifies the interrupt flag.
140  */
141 static int __kprobes is_IF_modifier(kprobe_opcode_t opcode)
142 {
143         switch (opcode) {
144         case 0xfa:              /* cli */
145         case 0xfb:              /* sti */
146         case 0xcf:              /* iret/iretd */
147         case 0x9d:              /* popf/popfd */
148                 return 1;
149         }
150         return 0;
151 }
152
153 int __kprobes arch_prepare_kprobe(struct kprobe *p)
154 {
155         /* insn: must be on special executable page on i386. */
156         p->ainsn.insn = get_insn_slot();
157         if (!p->ainsn.insn)
158                 return -ENOMEM;
159
160         memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
161         p->opcode = *p->addr;
162         if (can_boost(p->addr)) {
163                 p->ainsn.boostable = 0;
164         } else {
165                 p->ainsn.boostable = -1;
166         }
167         return 0;
168 }
169
170 void __kprobes arch_arm_kprobe(struct kprobe *p)
171 {
172         text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
173 }
174
175 void __kprobes arch_disarm_kprobe(struct kprobe *p)
176 {
177         text_poke(p->addr, &p->opcode, 1);
178 }
179
180 void __kprobes arch_remove_kprobe(struct kprobe *p)
181 {
182         mutex_lock(&kprobe_mutex);
183         free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
184         mutex_unlock(&kprobe_mutex);
185 }
186
187 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
188 {
189         kcb->prev_kprobe.kp = kprobe_running();
190         kcb->prev_kprobe.status = kcb->kprobe_status;
191         kcb->prev_kprobe.old_eflags = kcb->kprobe_old_eflags;
192         kcb->prev_kprobe.saved_eflags = kcb->kprobe_saved_eflags;
193 }
194
195 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
196 {
197         __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
198         kcb->kprobe_status = kcb->prev_kprobe.status;
199         kcb->kprobe_old_eflags = kcb->prev_kprobe.old_eflags;
200         kcb->kprobe_saved_eflags = kcb->prev_kprobe.saved_eflags;
201 }
202
203 static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
204                                 struct kprobe_ctlblk *kcb)
205 {
206         __get_cpu_var(current_kprobe) = p;
207         kcb->kprobe_saved_eflags = kcb->kprobe_old_eflags
208                 = (regs->eflags & (TF_MASK | IF_MASK));
209         if (is_IF_modifier(p->opcode))
210                 kcb->kprobe_saved_eflags &= ~IF_MASK;
211 }
212
213 static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
214 {
215         regs->eflags |= TF_MASK;
216         regs->eflags &= ~IF_MASK;
217         /*single step inline if the instruction is an int3*/
218         if (p->opcode == BREAKPOINT_INSTRUCTION)
219                 regs->eip = (unsigned long)p->addr;
220         else
221                 regs->eip = (unsigned long)p->ainsn.insn;
222 }
223
224 /* Called with kretprobe_lock held */
225 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
226                                       struct pt_regs *regs)
227 {
228         unsigned long *sara = (unsigned long *)&regs->esp;
229
230         ri->ret_addr = (kprobe_opcode_t *) *sara;
231
232         /* Replace the return addr with trampoline addr */
233         *sara = (unsigned long) &kretprobe_trampoline;
234 }
235
236 /*
237  * Interrupts are disabled on entry as trap3 is an interrupt gate and they
238  * remain disabled thorough out this function.
239  */
240 static int __kprobes kprobe_handler(struct pt_regs *regs)
241 {
242         struct kprobe *p;
243         int ret = 0;
244         kprobe_opcode_t *addr;
245         struct kprobe_ctlblk *kcb;
246
247         addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
248
249         /*
250          * We don't want to be preempted for the entire
251          * duration of kprobe processing
252          */
253         preempt_disable();
254         kcb = get_kprobe_ctlblk();
255
256         /* Check we're not actually recursing */
257         if (kprobe_running()) {
258                 p = get_kprobe(addr);
259                 if (p) {
260                         if (kcb->kprobe_status == KPROBE_HIT_SS &&
261                                 *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
262                                 regs->eflags &= ~TF_MASK;
263                                 regs->eflags |= kcb->kprobe_saved_eflags;
264                                 goto no_kprobe;
265                         }
266                         /* We have reentered the kprobe_handler(), since
267                          * another probe was hit while within the handler.
268                          * We here save the original kprobes variables and
269                          * just single step on the instruction of the new probe
270                          * without calling any user handlers.
271                          */
272                         save_previous_kprobe(kcb);
273                         set_current_kprobe(p, regs, kcb);
274                         kprobes_inc_nmissed_count(p);
275                         prepare_singlestep(p, regs);
276                         kcb->kprobe_status = KPROBE_REENTER;
277                         return 1;
278                 } else {
279                         if (*addr != BREAKPOINT_INSTRUCTION) {
280                         /* The breakpoint instruction was removed by
281                          * another cpu right after we hit, no further
282                          * handling of this interrupt is appropriate
283                          */
284                                 regs->eip -= sizeof(kprobe_opcode_t);
285                                 ret = 1;
286                                 goto no_kprobe;
287                         }
288                         p = __get_cpu_var(current_kprobe);
289                         if (p->break_handler && p->break_handler(p, regs)) {
290                                 goto ss_probe;
291                         }
292                 }
293                 goto no_kprobe;
294         }
295
296         p = get_kprobe(addr);
297         if (!p) {
298                 if (*addr != BREAKPOINT_INSTRUCTION) {
299                         /*
300                          * The breakpoint instruction was removed right
301                          * after we hit it.  Another cpu has removed
302                          * either a probepoint or a debugger breakpoint
303                          * at this address.  In either case, no further
304                          * handling of this interrupt is appropriate.
305                          * Back up over the (now missing) int3 and run
306                          * the original instruction.
307                          */
308                         regs->eip -= sizeof(kprobe_opcode_t);
309                         ret = 1;
310                 }
311                 /* Not one of ours: let kernel handle it */
312                 goto no_kprobe;
313         }
314
315         set_current_kprobe(p, regs, kcb);
316         kcb->kprobe_status = KPROBE_HIT_ACTIVE;
317
318         if (p->pre_handler && p->pre_handler(p, regs))
319                 /* handler has already set things up, so skip ss setup */
320                 return 1;
321
322 ss_probe:
323 #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PM)
324         if (p->ainsn.boostable == 1 && !p->post_handler){
325                 /* Boost up -- we can execute copied instructions directly */
326                 reset_current_kprobe();
327                 regs->eip = (unsigned long)p->ainsn.insn;
328                 preempt_enable_no_resched();
329                 return 1;
330         }
331 #endif
332         prepare_singlestep(p, regs);
333         kcb->kprobe_status = KPROBE_HIT_SS;
334         return 1;
335
336 no_kprobe:
337         preempt_enable_no_resched();
338         return ret;
339 }
340
341 /*
342  * For function-return probes, init_kprobes() establishes a probepoint
343  * here. When a retprobed function returns, this probe is hit and
344  * trampoline_probe_handler() runs, calling the kretprobe's handler.
345  */
346  void __kprobes kretprobe_trampoline_holder(void)
347  {
348         asm volatile ( ".global kretprobe_trampoline\n"
349                         "kretprobe_trampoline: \n"
350                         "       pushf\n"
351                         /* skip cs, eip, orig_eax */
352                         "       subl $12, %esp\n"
353                         "       pushl %fs\n"
354                         "       pushl %ds\n"
355                         "       pushl %es\n"
356                         "       pushl %eax\n"
357                         "       pushl %ebp\n"
358                         "       pushl %edi\n"
359                         "       pushl %esi\n"
360                         "       pushl %edx\n"
361                         "       pushl %ecx\n"
362                         "       pushl %ebx\n"
363                         "       movl %esp, %eax\n"
364                         "       call trampoline_handler\n"
365                         /* move eflags to cs */
366                         "       movl 52(%esp), %edx\n"
367                         "       movl %edx, 48(%esp)\n"
368                         /* save true return address on eflags */
369                         "       movl %eax, 52(%esp)\n"
370                         "       popl %ebx\n"
371                         "       popl %ecx\n"
372                         "       popl %edx\n"
373                         "       popl %esi\n"
374                         "       popl %edi\n"
375                         "       popl %ebp\n"
376                         "       popl %eax\n"
377                         /* skip eip, orig_eax, es, ds, fs */
378                         "       addl $20, %esp\n"
379                         "       popf\n"
380                         "       ret\n");
381 }
382
383 /*
384  * Called from kretprobe_trampoline
385  */
386 fastcall void *__kprobes trampoline_handler(struct pt_regs *regs)
387 {
388         struct kretprobe_instance *ri = NULL;
389         struct hlist_head *head, empty_rp;
390         struct hlist_node *node, *tmp;
391         unsigned long flags, orig_ret_address = 0;
392         unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
393
394         INIT_HLIST_HEAD(&empty_rp);
395         spin_lock_irqsave(&kretprobe_lock, flags);
396         head = kretprobe_inst_table_head(current);
397         /* fixup registers */
398         regs->xcs = __KERNEL_CS | get_kernel_rpl();
399         regs->eip = trampoline_address;
400         regs->orig_eax = 0xffffffff;
401
402         /*
403          * It is possible to have multiple instances associated with a given
404          * task either because an multiple functions in the call path
405          * have a return probe installed on them, and/or more then one return
406          * return probe was registered for a target function.
407          *
408          * We can handle this because:
409          *     - instances are always inserted at the head of the list
410          *     - when multiple return probes are registered for the same
411          *       function, the first instance's ret_addr will point to the
412          *       real return address, and all the rest will point to
413          *       kretprobe_trampoline
414          */
415         hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
416                 if (ri->task != current)
417                         /* another task is sharing our hash bucket */
418                         continue;
419
420                 if (ri->rp && ri->rp->handler){
421                         __get_cpu_var(current_kprobe) = &ri->rp->kp;
422                         get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
423                         ri->rp->handler(ri, regs);
424                         __get_cpu_var(current_kprobe) = NULL;
425                 }
426
427                 orig_ret_address = (unsigned long)ri->ret_addr;
428                 recycle_rp_inst(ri, &empty_rp);
429
430                 if (orig_ret_address != trampoline_address)
431                         /*
432                          * This is the real return address. Any other
433                          * instances associated with this task are for
434                          * other calls deeper on the call stack
435                          */
436                         break;
437         }
438
439         kretprobe_assert(ri, orig_ret_address, trampoline_address);
440         spin_unlock_irqrestore(&kretprobe_lock, flags);
441
442         hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
443                 hlist_del(&ri->hlist);
444                 kfree(ri);
445         }
446         return (void*)orig_ret_address;
447 }
448
449 /*
450  * Called after single-stepping.  p->addr is the address of the
451  * instruction whose first byte has been replaced by the "int 3"
452  * instruction.  To avoid the SMP problems that can occur when we
453  * temporarily put back the original opcode to single-step, we
454  * single-stepped a copy of the instruction.  The address of this
455  * copy is p->ainsn.insn.
456  *
457  * This function prepares to return from the post-single-step
458  * interrupt.  We have to fix up the stack as follows:
459  *
460  * 0) Except in the case of absolute or indirect jump or call instructions,
461  * the new eip is relative to the copied instruction.  We need to make
462  * it relative to the original instruction.
463  *
464  * 1) If the single-stepped instruction was pushfl, then the TF and IF
465  * flags are set in the just-pushed eflags, and may need to be cleared.
466  *
467  * 2) If the single-stepped instruction was a call, the return address
468  * that is atop the stack is the address following the copied instruction.
469  * We need to make it the address following the original instruction.
470  *
471  * This function also checks instruction size for preparing direct execution.
472  */
473 static void __kprobes resume_execution(struct kprobe *p,
474                 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
475 {
476         unsigned long *tos = (unsigned long *)&regs->esp;
477         unsigned long copy_eip = (unsigned long)p->ainsn.insn;
478         unsigned long orig_eip = (unsigned long)p->addr;
479
480         regs->eflags &= ~TF_MASK;
481         switch (p->ainsn.insn[0]) {
482         case 0x9c:              /* pushfl */
483                 *tos &= ~(TF_MASK | IF_MASK);
484                 *tos |= kcb->kprobe_old_eflags;
485                 break;
486         case 0xc2:              /* iret/ret/lret */
487         case 0xc3:
488         case 0xca:
489         case 0xcb:
490         case 0xcf:
491         case 0xea:              /* jmp absolute -- eip is correct */
492                 /* eip is already adjusted, no more changes required */
493                 p->ainsn.boostable = 1;
494                 goto no_change;
495         case 0xe8:              /* call relative - Fix return addr */
496                 *tos = orig_eip + (*tos - copy_eip);
497                 break;
498         case 0x9a:              /* call absolute -- same as call absolute, indirect */
499                 *tos = orig_eip + (*tos - copy_eip);
500                 goto no_change;
501         case 0xff:
502                 if ((p->ainsn.insn[1] & 0x30) == 0x10) {
503                         /*
504                          * call absolute, indirect
505                          * Fix return addr; eip is correct.
506                          * But this is not boostable
507                          */
508                         *tos = orig_eip + (*tos - copy_eip);
509                         goto no_change;
510                 } else if (((p->ainsn.insn[1] & 0x31) == 0x20) ||       /* jmp near, absolute indirect */
511                            ((p->ainsn.insn[1] & 0x31) == 0x21)) {       /* jmp far, absolute indirect */
512                         /* eip is correct. And this is boostable */
513                         p->ainsn.boostable = 1;
514                         goto no_change;
515                 }
516         default:
517                 break;
518         }
519
520         if (p->ainsn.boostable == 0) {
521                 if ((regs->eip > copy_eip) &&
522                     (regs->eip - copy_eip) + 5 < MAX_INSN_SIZE) {
523                         /*
524                          * These instructions can be executed directly if it
525                          * jumps back to correct address.
526                          */
527                         set_jmp_op((void *)regs->eip,
528                                    (void *)orig_eip + (regs->eip - copy_eip));
529                         p->ainsn.boostable = 1;
530                 } else {
531                         p->ainsn.boostable = -1;
532                 }
533         }
534
535         regs->eip = orig_eip + (regs->eip - copy_eip);
536
537 no_change:
538         return;
539 }
540
541 /*
542  * Interrupts are disabled on entry as trap1 is an interrupt gate and they
543  * remain disabled thoroughout this function.
544  */
545 static int __kprobes post_kprobe_handler(struct pt_regs *regs)
546 {
547         struct kprobe *cur = kprobe_running();
548         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
549
550         if (!cur)
551                 return 0;
552
553         if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
554                 kcb->kprobe_status = KPROBE_HIT_SSDONE;
555                 cur->post_handler(cur, regs, 0);
556         }
557
558         resume_execution(cur, regs, kcb);
559         regs->eflags |= kcb->kprobe_saved_eflags;
560 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
561         if (raw_irqs_disabled_flags(regs->eflags))
562                 trace_hardirqs_off();
563         else
564                 trace_hardirqs_on();
565 #endif
566
567         /*Restore back the original saved kprobes variables and continue. */
568         if (kcb->kprobe_status == KPROBE_REENTER) {
569                 restore_previous_kprobe(kcb);
570                 goto out;
571         }
572         reset_current_kprobe();
573 out:
574         preempt_enable_no_resched();
575
576         /*
577          * if somebody else is singlestepping across a probe point, eflags
578          * will have TF set, in which case, continue the remaining processing
579          * of do_debug, as if this is not a probe hit.
580          */
581         if (regs->eflags & TF_MASK)
582                 return 0;
583
584         return 1;
585 }
586
587 int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
588 {
589         struct kprobe *cur = kprobe_running();
590         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
591
592         switch(kcb->kprobe_status) {
593         case KPROBE_HIT_SS:
594         case KPROBE_REENTER:
595                 /*
596                  * We are here because the instruction being single
597                  * stepped caused a page fault. We reset the current
598                  * kprobe and the eip points back to the probe address
599                  * and allow the page fault handler to continue as a
600                  * normal page fault.
601                  */
602                 regs->eip = (unsigned long)cur->addr;
603                 regs->eflags |= kcb->kprobe_old_eflags;
604                 if (kcb->kprobe_status == KPROBE_REENTER)
605                         restore_previous_kprobe(kcb);
606                 else
607                         reset_current_kprobe();
608                 preempt_enable_no_resched();
609                 break;
610         case KPROBE_HIT_ACTIVE:
611         case KPROBE_HIT_SSDONE:
612                 /*
613                  * We increment the nmissed count for accounting,
614                  * we can also use npre/npostfault count for accouting
615                  * these specific fault cases.
616                  */
617                 kprobes_inc_nmissed_count(cur);
618
619                 /*
620                  * We come here because instructions in the pre/post
621                  * handler caused the page_fault, this could happen
622                  * if handler tries to access user space by
623                  * copy_from_user(), get_user() etc. Let the
624                  * user-specified handler try to fix it first.
625                  */
626                 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
627                         return 1;
628
629                 /*
630                  * In case the user-specified fault handler returned
631                  * zero, try to fix up.
632                  */
633                 if (fixup_exception(regs))
634                         return 1;
635
636                 /*
637                  * fixup_exception() could not handle it,
638                  * Let do_page_fault() fix it.
639                  */
640                 break;
641         default:
642                 break;
643         }
644         return 0;
645 }
646
647 /*
648  * Wrapper routine to for handling exceptions.
649  */
650 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
651                                        unsigned long val, void *data)
652 {
653         struct die_args *args = (struct die_args *)data;
654         int ret = NOTIFY_DONE;
655
656         if (args->regs && user_mode_vm(args->regs))
657                 return ret;
658
659         switch (val) {
660         case DIE_INT3:
661                 if (kprobe_handler(args->regs))
662                         ret = NOTIFY_STOP;
663                 break;
664         case DIE_DEBUG:
665                 if (post_kprobe_handler(args->regs))
666                         ret = NOTIFY_STOP;
667                 break;
668         case DIE_GPF:
669                 /* kprobe_running() needs smp_processor_id() */
670                 preempt_disable();
671                 if (kprobe_running() &&
672                     kprobe_fault_handler(args->regs, args->trapnr))
673                         ret = NOTIFY_STOP;
674                 preempt_enable();
675                 break;
676         default:
677                 break;
678         }
679         return ret;
680 }
681
682 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
683 {
684         struct jprobe *jp = container_of(p, struct jprobe, kp);
685         unsigned long addr;
686         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
687
688         kcb->jprobe_saved_regs = *regs;
689         kcb->jprobe_saved_esp = &regs->esp;
690         addr = (unsigned long)(kcb->jprobe_saved_esp);
691
692         /*
693          * TBD: As Linus pointed out, gcc assumes that the callee
694          * owns the argument space and could overwrite it, e.g.
695          * tailcall optimization. So, to be absolutely safe
696          * we also save and restore enough stack bytes to cover
697          * the argument area.
698          */
699         memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
700                         MIN_STACK_SIZE(addr));
701         regs->eflags &= ~IF_MASK;
702         trace_hardirqs_off();
703         regs->eip = (unsigned long)(jp->entry);
704         return 1;
705 }
706
707 void __kprobes jprobe_return(void)
708 {
709         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
710
711         asm volatile ("       xchgl   %%ebx,%%esp     \n"
712                       "       int3                      \n"
713                       "       .globl jprobe_return_end  \n"
714                       "       jprobe_return_end:        \n"
715                       "       nop                       \n"::"b"
716                       (kcb->jprobe_saved_esp):"memory");
717 }
718
719 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
720 {
721         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
722         u8 *addr = (u8 *) (regs->eip - 1);
723         unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_esp);
724         struct jprobe *jp = container_of(p, struct jprobe, kp);
725
726         if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
727                 if (&regs->esp != kcb->jprobe_saved_esp) {
728                         struct pt_regs *saved_regs =
729                             container_of(kcb->jprobe_saved_esp,
730                                             struct pt_regs, esp);
731                         printk("current esp %p does not match saved esp %p\n",
732                                &regs->esp, kcb->jprobe_saved_esp);
733                         printk("Saved registers for jprobe %p\n", jp);
734                         show_registers(saved_regs);
735                         printk("Current registers\n");
736                         show_registers(regs);
737                         BUG();
738                 }
739                 *regs = kcb->jprobe_saved_regs;
740                 memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
741                        MIN_STACK_SIZE(stack_addr));
742                 preempt_enable_no_resched();
743                 return 1;
744         }
745         return 0;
746 }
747
748 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
749 {
750         return 0;
751 }
752
753 int __init arch_init_kprobes(void)
754 {
755         return 0;
756 }