Merge branch 'upstream' of git://ftp.linux-mips.org/pub/scm/upstream-linus
[linux-2.6] / 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/kprobes.h>
32 #include <linux/ptrace.h>
33 #include <linux/preempt.h>
34 #include <asm/cacheflush.h>
35 #include <asm/kdebug.h>
36 #include <asm/desc.h>
37 #include <asm/uaccess.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         *p->addr = BREAKPOINT_INSTRUCTION;
173         flush_icache_range((unsigned long) p->addr,
174                            (unsigned long) p->addr + sizeof(kprobe_opcode_t));
175 }
176
177 void __kprobes arch_disarm_kprobe(struct kprobe *p)
178 {
179         *p->addr = p->opcode;
180         flush_icache_range((unsigned long) p->addr,
181                            (unsigned long) p->addr + sizeof(kprobe_opcode_t));
182 }
183
184 void __kprobes arch_remove_kprobe(struct kprobe *p)
185 {
186         mutex_lock(&kprobe_mutex);
187         free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
188         mutex_unlock(&kprobe_mutex);
189 }
190
191 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
192 {
193         kcb->prev_kprobe.kp = kprobe_running();
194         kcb->prev_kprobe.status = kcb->kprobe_status;
195         kcb->prev_kprobe.old_eflags = kcb->kprobe_old_eflags;
196         kcb->prev_kprobe.saved_eflags = kcb->kprobe_saved_eflags;
197 }
198
199 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
200 {
201         __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
202         kcb->kprobe_status = kcb->prev_kprobe.status;
203         kcb->kprobe_old_eflags = kcb->prev_kprobe.old_eflags;
204         kcb->kprobe_saved_eflags = kcb->prev_kprobe.saved_eflags;
205 }
206
207 static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
208                                 struct kprobe_ctlblk *kcb)
209 {
210         __get_cpu_var(current_kprobe) = p;
211         kcb->kprobe_saved_eflags = kcb->kprobe_old_eflags
212                 = (regs->eflags & (TF_MASK | IF_MASK));
213         if (is_IF_modifier(p->opcode))
214                 kcb->kprobe_saved_eflags &= ~IF_MASK;
215 }
216
217 static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
218 {
219         regs->eflags |= TF_MASK;
220         regs->eflags &= ~IF_MASK;
221         /*single step inline if the instruction is an int3*/
222         if (p->opcode == BREAKPOINT_INSTRUCTION)
223                 regs->eip = (unsigned long)p->addr;
224         else
225                 regs->eip = (unsigned long)p->ainsn.insn;
226 }
227
228 /* Called with kretprobe_lock held */
229 void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
230                                       struct pt_regs *regs)
231 {
232         unsigned long *sara = (unsigned long *)&regs->esp;
233
234         struct kretprobe_instance *ri;
235
236         if ((ri = get_free_rp_inst(rp)) != NULL) {
237                 ri->rp = rp;
238                 ri->task = current;
239                 ri->ret_addr = (kprobe_opcode_t *) *sara;
240
241                 /* Replace the return addr with trampoline addr */
242                 *sara = (unsigned long) &kretprobe_trampoline;
243                 add_rp_inst(ri);
244         } else {
245                 rp->nmissed++;
246         }
247 }
248
249 /*
250  * Interrupts are disabled on entry as trap3 is an interrupt gate and they
251  * remain disabled thorough out this function.
252  */
253 static int __kprobes kprobe_handler(struct pt_regs *regs)
254 {
255         struct kprobe *p;
256         int ret = 0;
257         kprobe_opcode_t *addr;
258         struct kprobe_ctlblk *kcb;
259
260         addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
261
262         /*
263          * We don't want to be preempted for the entire
264          * duration of kprobe processing
265          */
266         preempt_disable();
267         kcb = get_kprobe_ctlblk();
268
269         /* Check we're not actually recursing */
270         if (kprobe_running()) {
271                 p = get_kprobe(addr);
272                 if (p) {
273                         if (kcb->kprobe_status == KPROBE_HIT_SS &&
274                                 *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
275                                 regs->eflags &= ~TF_MASK;
276                                 regs->eflags |= kcb->kprobe_saved_eflags;
277                                 goto no_kprobe;
278                         }
279                         /* We have reentered the kprobe_handler(), since
280                          * another probe was hit while within the handler.
281                          * We here save the original kprobes variables and
282                          * just single step on the instruction of the new probe
283                          * without calling any user handlers.
284                          */
285                         save_previous_kprobe(kcb);
286                         set_current_kprobe(p, regs, kcb);
287                         kprobes_inc_nmissed_count(p);
288                         prepare_singlestep(p, regs);
289                         kcb->kprobe_status = KPROBE_REENTER;
290                         return 1;
291                 } else {
292                         if (*addr != BREAKPOINT_INSTRUCTION) {
293                         /* The breakpoint instruction was removed by
294                          * another cpu right after we hit, no further
295                          * handling of this interrupt is appropriate
296                          */
297                                 regs->eip -= sizeof(kprobe_opcode_t);
298                                 ret = 1;
299                                 goto no_kprobe;
300                         }
301                         p = __get_cpu_var(current_kprobe);
302                         if (p->break_handler && p->break_handler(p, regs)) {
303                                 goto ss_probe;
304                         }
305                 }
306                 goto no_kprobe;
307         }
308
309         p = get_kprobe(addr);
310         if (!p) {
311                 if (*addr != BREAKPOINT_INSTRUCTION) {
312                         /*
313                          * The breakpoint instruction was removed right
314                          * after we hit it.  Another cpu has removed
315                          * either a probepoint or a debugger breakpoint
316                          * at this address.  In either case, no further
317                          * handling of this interrupt is appropriate.
318                          * Back up over the (now missing) int3 and run
319                          * the original instruction.
320                          */
321                         regs->eip -= sizeof(kprobe_opcode_t);
322                         ret = 1;
323                 }
324                 /* Not one of ours: let kernel handle it */
325                 goto no_kprobe;
326         }
327
328         set_current_kprobe(p, regs, kcb);
329         kcb->kprobe_status = KPROBE_HIT_ACTIVE;
330
331         if (p->pre_handler && p->pre_handler(p, regs))
332                 /* handler has already set things up, so skip ss setup */
333                 return 1;
334
335 ss_probe:
336 #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PM)
337         if (p->ainsn.boostable == 1 && !p->post_handler){
338                 /* Boost up -- we can execute copied instructions directly */
339                 reset_current_kprobe();
340                 regs->eip = (unsigned long)p->ainsn.insn;
341                 preempt_enable_no_resched();
342                 return 1;
343         }
344 #endif
345         prepare_singlestep(p, regs);
346         kcb->kprobe_status = KPROBE_HIT_SS;
347         return 1;
348
349 no_kprobe:
350         preempt_enable_no_resched();
351         return ret;
352 }
353
354 /*
355  * For function-return probes, init_kprobes() establishes a probepoint
356  * here. When a retprobed function returns, this probe is hit and
357  * trampoline_probe_handler() runs, calling the kretprobe's handler.
358  */
359  void __kprobes kretprobe_trampoline_holder(void)
360  {
361         asm volatile ( ".global kretprobe_trampoline\n"
362                         "kretprobe_trampoline: \n"
363                         "       pushf\n"
364                         /* skip cs, eip, orig_eax */
365                         "       subl $12, %esp\n"
366                         "       pushl %gs\n"
367                         "       pushl %ds\n"
368                         "       pushl %es\n"
369                         "       pushl %eax\n"
370                         "       pushl %ebp\n"
371                         "       pushl %edi\n"
372                         "       pushl %esi\n"
373                         "       pushl %edx\n"
374                         "       pushl %ecx\n"
375                         "       pushl %ebx\n"
376                         "       movl %esp, %eax\n"
377                         "       call trampoline_handler\n"
378                         /* move eflags to cs */
379                         "       movl 52(%esp), %edx\n"
380                         "       movl %edx, 48(%esp)\n"
381                         /* save true return address on eflags */
382                         "       movl %eax, 52(%esp)\n"
383                         "       popl %ebx\n"
384                         "       popl %ecx\n"
385                         "       popl %edx\n"
386                         "       popl %esi\n"
387                         "       popl %edi\n"
388                         "       popl %ebp\n"
389                         "       popl %eax\n"
390                         /* skip eip, orig_eax, es, ds, gs */
391                         "       addl $20, %esp\n"
392                         "       popf\n"
393                         "       ret\n");
394 }
395
396 /*
397  * Called from kretprobe_trampoline
398  */
399 fastcall void *__kprobes trampoline_handler(struct pt_regs *regs)
400 {
401         struct kretprobe_instance *ri = NULL;
402         struct hlist_head *head, empty_rp;
403         struct hlist_node *node, *tmp;
404         unsigned long flags, orig_ret_address = 0;
405         unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
406
407         INIT_HLIST_HEAD(&empty_rp);
408         spin_lock_irqsave(&kretprobe_lock, flags);
409         head = kretprobe_inst_table_head(current);
410         /* fixup registers */
411         regs->xcs = __KERNEL_CS;
412         regs->eip = trampoline_address;
413         regs->orig_eax = 0xffffffff;
414
415         /*
416          * It is possible to have multiple instances associated with a given
417          * task either because an multiple functions in the call path
418          * have a return probe installed on them, and/or more then one return
419          * return probe was registered for a target function.
420          *
421          * We can handle this because:
422          *     - instances are always inserted at the head of the list
423          *     - when multiple return probes are registered for the same
424          *       function, the first instance's ret_addr will point to the
425          *       real return address, and all the rest will point to
426          *       kretprobe_trampoline
427          */
428         hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
429                 if (ri->task != current)
430                         /* another task is sharing our hash bucket */
431                         continue;
432
433                 if (ri->rp && ri->rp->handler){
434                         __get_cpu_var(current_kprobe) = &ri->rp->kp;
435                         get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
436                         ri->rp->handler(ri, regs);
437                         __get_cpu_var(current_kprobe) = NULL;
438                 }
439
440                 orig_ret_address = (unsigned long)ri->ret_addr;
441                 recycle_rp_inst(ri, &empty_rp);
442
443                 if (orig_ret_address != trampoline_address)
444                         /*
445                          * This is the real return address. Any other
446                          * instances associated with this task are for
447                          * other calls deeper on the call stack
448                          */
449                         break;
450         }
451
452         BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
453
454         spin_unlock_irqrestore(&kretprobe_lock, flags);
455
456         hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
457                 hlist_del(&ri->hlist);
458                 kfree(ri);
459         }
460         return (void*)orig_ret_address;
461 }
462
463 /*
464  * Called after single-stepping.  p->addr is the address of the
465  * instruction whose first byte has been replaced by the "int 3"
466  * instruction.  To avoid the SMP problems that can occur when we
467  * temporarily put back the original opcode to single-step, we
468  * single-stepped a copy of the instruction.  The address of this
469  * copy is p->ainsn.insn.
470  *
471  * This function prepares to return from the post-single-step
472  * interrupt.  We have to fix up the stack as follows:
473  *
474  * 0) Except in the case of absolute or indirect jump or call instructions,
475  * the new eip is relative to the copied instruction.  We need to make
476  * it relative to the original instruction.
477  *
478  * 1) If the single-stepped instruction was pushfl, then the TF and IF
479  * flags are set in the just-pushed eflags, and may need to be cleared.
480  *
481  * 2) If the single-stepped instruction was a call, the return address
482  * that is atop the stack is the address following the copied instruction.
483  * We need to make it the address following the original instruction.
484  *
485  * This function also checks instruction size for preparing direct execution.
486  */
487 static void __kprobes resume_execution(struct kprobe *p,
488                 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
489 {
490         unsigned long *tos = (unsigned long *)&regs->esp;
491         unsigned long copy_eip = (unsigned long)p->ainsn.insn;
492         unsigned long orig_eip = (unsigned long)p->addr;
493
494         regs->eflags &= ~TF_MASK;
495         switch (p->ainsn.insn[0]) {
496         case 0x9c:              /* pushfl */
497                 *tos &= ~(TF_MASK | IF_MASK);
498                 *tos |= kcb->kprobe_old_eflags;
499                 break;
500         case 0xc2:              /* iret/ret/lret */
501         case 0xc3:
502         case 0xca:
503         case 0xcb:
504         case 0xcf:
505         case 0xea:              /* jmp absolute -- eip is correct */
506                 /* eip is already adjusted, no more changes required */
507                 p->ainsn.boostable = 1;
508                 goto no_change;
509         case 0xe8:              /* call relative - Fix return addr */
510                 *tos = orig_eip + (*tos - copy_eip);
511                 break;
512         case 0x9a:              /* call absolute -- same as call absolute, indirect */
513                 *tos = orig_eip + (*tos - copy_eip);
514                 goto no_change;
515         case 0xff:
516                 if ((p->ainsn.insn[1] & 0x30) == 0x10) {
517                         /*
518                          * call absolute, indirect
519                          * Fix return addr; eip is correct.
520                          * But this is not boostable
521                          */
522                         *tos = orig_eip + (*tos - copy_eip);
523                         goto no_change;
524                 } else if (((p->ainsn.insn[1] & 0x31) == 0x20) ||       /* jmp near, absolute indirect */
525                            ((p->ainsn.insn[1] & 0x31) == 0x21)) {       /* jmp far, absolute indirect */
526                         /* eip is correct. And this is boostable */
527                         p->ainsn.boostable = 1;
528                         goto no_change;
529                 }
530         default:
531                 break;
532         }
533
534         if (p->ainsn.boostable == 0) {
535                 if ((regs->eip > copy_eip) &&
536                     (regs->eip - copy_eip) + 5 < MAX_INSN_SIZE) {
537                         /*
538                          * These instructions can be executed directly if it
539                          * jumps back to correct address.
540                          */
541                         set_jmp_op((void *)regs->eip,
542                                    (void *)orig_eip + (regs->eip - copy_eip));
543                         p->ainsn.boostable = 1;
544                 } else {
545                         p->ainsn.boostable = -1;
546                 }
547         }
548
549         regs->eip = orig_eip + (regs->eip - copy_eip);
550
551 no_change:
552         return;
553 }
554
555 /*
556  * Interrupts are disabled on entry as trap1 is an interrupt gate and they
557  * remain disabled thoroughout this function.
558  */
559 static int __kprobes post_kprobe_handler(struct pt_regs *regs)
560 {
561         struct kprobe *cur = kprobe_running();
562         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
563
564         if (!cur)
565                 return 0;
566
567         if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
568                 kcb->kprobe_status = KPROBE_HIT_SSDONE;
569                 cur->post_handler(cur, regs, 0);
570         }
571
572         resume_execution(cur, regs, kcb);
573         regs->eflags |= kcb->kprobe_saved_eflags;
574
575         /*Restore back the original saved kprobes variables and continue. */
576         if (kcb->kprobe_status == KPROBE_REENTER) {
577                 restore_previous_kprobe(kcb);
578                 goto out;
579         }
580         reset_current_kprobe();
581 out:
582         preempt_enable_no_resched();
583
584         /*
585          * if somebody else is singlestepping across a probe point, eflags
586          * will have TF set, in which case, continue the remaining processing
587          * of do_debug, as if this is not a probe hit.
588          */
589         if (regs->eflags & TF_MASK)
590                 return 0;
591
592         return 1;
593 }
594
595 static int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
596 {
597         struct kprobe *cur = kprobe_running();
598         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
599
600         switch(kcb->kprobe_status) {
601         case KPROBE_HIT_SS:
602         case KPROBE_REENTER:
603                 /*
604                  * We are here because the instruction being single
605                  * stepped caused a page fault. We reset the current
606                  * kprobe and the eip points back to the probe address
607                  * and allow the page fault handler to continue as a
608                  * normal page fault.
609                  */
610                 regs->eip = (unsigned long)cur->addr;
611                 regs->eflags |= kcb->kprobe_old_eflags;
612                 if (kcb->kprobe_status == KPROBE_REENTER)
613                         restore_previous_kprobe(kcb);
614                 else
615                         reset_current_kprobe();
616                 preempt_enable_no_resched();
617                 break;
618         case KPROBE_HIT_ACTIVE:
619         case KPROBE_HIT_SSDONE:
620                 /*
621                  * We increment the nmissed count for accounting,
622                  * we can also use npre/npostfault count for accouting
623                  * these specific fault cases.
624                  */
625                 kprobes_inc_nmissed_count(cur);
626
627                 /*
628                  * We come here because instructions in the pre/post
629                  * handler caused the page_fault, this could happen
630                  * if handler tries to access user space by
631                  * copy_from_user(), get_user() etc. Let the
632                  * user-specified handler try to fix it first.
633                  */
634                 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
635                         return 1;
636
637                 /*
638                  * In case the user-specified fault handler returned
639                  * zero, try to fix up.
640                  */
641                 if (fixup_exception(regs))
642                         return 1;
643
644                 /*
645                  * fixup_exception() could not handle it,
646                  * Let do_page_fault() fix it.
647                  */
648                 break;
649         default:
650                 break;
651         }
652         return 0;
653 }
654
655 /*
656  * Wrapper routine to for handling exceptions.
657  */
658 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
659                                        unsigned long val, void *data)
660 {
661         struct die_args *args = (struct die_args *)data;
662         int ret = NOTIFY_DONE;
663
664         if (args->regs && user_mode_vm(args->regs))
665                 return ret;
666
667         switch (val) {
668         case DIE_INT3:
669                 if (kprobe_handler(args->regs))
670                         ret = NOTIFY_STOP;
671                 break;
672         case DIE_DEBUG:
673                 if (post_kprobe_handler(args->regs))
674                         ret = NOTIFY_STOP;
675                 break;
676         case DIE_GPF:
677         case DIE_PAGE_FAULT:
678                 /* kprobe_running() needs smp_processor_id() */
679                 preempt_disable();
680                 if (kprobe_running() &&
681                     kprobe_fault_handler(args->regs, args->trapnr))
682                         ret = NOTIFY_STOP;
683                 preempt_enable();
684                 break;
685         default:
686                 break;
687         }
688         return ret;
689 }
690
691 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
692 {
693         struct jprobe *jp = container_of(p, struct jprobe, kp);
694         unsigned long addr;
695         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
696
697         kcb->jprobe_saved_regs = *regs;
698         kcb->jprobe_saved_esp = &regs->esp;
699         addr = (unsigned long)(kcb->jprobe_saved_esp);
700
701         /*
702          * TBD: As Linus pointed out, gcc assumes that the callee
703          * owns the argument space and could overwrite it, e.g.
704          * tailcall optimization. So, to be absolutely safe
705          * we also save and restore enough stack bytes to cover
706          * the argument area.
707          */
708         memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
709                         MIN_STACK_SIZE(addr));
710         regs->eflags &= ~IF_MASK;
711         regs->eip = (unsigned long)(jp->entry);
712         return 1;
713 }
714
715 void __kprobes jprobe_return(void)
716 {
717         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
718
719         asm volatile ("       xchgl   %%ebx,%%esp     \n"
720                       "       int3                      \n"
721                       "       .globl jprobe_return_end  \n"
722                       "       jprobe_return_end:        \n"
723                       "       nop                       \n"::"b"
724                       (kcb->jprobe_saved_esp):"memory");
725 }
726
727 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
728 {
729         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
730         u8 *addr = (u8 *) (regs->eip - 1);
731         unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_esp);
732         struct jprobe *jp = container_of(p, struct jprobe, kp);
733
734         if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
735                 if (&regs->esp != kcb->jprobe_saved_esp) {
736                         struct pt_regs *saved_regs =
737                             container_of(kcb->jprobe_saved_esp,
738                                             struct pt_regs, esp);
739                         printk("current esp %p does not match saved esp %p\n",
740                                &regs->esp, kcb->jprobe_saved_esp);
741                         printk("Saved registers for jprobe %p\n", jp);
742                         show_registers(saved_regs);
743                         printk("Current registers\n");
744                         show_registers(regs);
745                         BUG();
746                 }
747                 *regs = kcb->jprobe_saved_regs;
748                 memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
749                        MIN_STACK_SIZE(stack_addr));
750                 preempt_enable_no_resched();
751                 return 1;
752         }
753         return 0;
754 }
755
756 int __init arch_init_kprobes(void)
757 {
758         return 0;
759 }