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