[PATCH] kprobes: Temporary disarming of reentrant probe for x86_64
[linux-2.6.git] / arch / x86_64 / kernel / kprobes.c
1 /*
2  *  Kernel Probes (KProbes)
3  *  arch/x86_64/kernel/kprobes.c
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Copyright (C) IBM Corporation, 2002, 2004
20  *
21  * 2002-Oct     Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
22  *              Probes initial implementation ( includes contributions from
23  *              Rusty Russell).
24  * 2004-July    Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
25  *              interface to access function arguments.
26  * 2004-Oct     Jim Keniston <kenistoj@us.ibm.com> and Prasanna S Panchamukhi
27  *              <prasanna@in.ibm.com> adapted for x86_64
28  * 2005-Mar     Roland McGrath <roland@redhat.com>
29  *              Fixed to handle %rip-relative addressing mode correctly.
30  * 2005-May     Rusty Lynch <rusty.lynch@intel.com>
31  *              Added function return probes functionality
32  */
33
34 #include <linux/config.h>
35 #include <linux/kprobes.h>
36 #include <linux/ptrace.h>
37 #include <linux/spinlock.h>
38 #include <linux/string.h>
39 #include <linux/slab.h>
40 #include <linux/preempt.h>
41 #include <linux/moduleloader.h>
42 #include <asm/cacheflush.h>
43 #include <asm/pgtable.h>
44 #include <asm/kdebug.h>
45
46 static DECLARE_MUTEX(kprobe_mutex);
47
48 static struct kprobe *current_kprobe;
49 static unsigned long kprobe_status, kprobe_old_rflags, kprobe_saved_rflags;
50 static struct kprobe *kprobe_prev;
51 static unsigned long kprobe_status_prev, kprobe_old_rflags_prev, kprobe_saved_rflags_prev;
52 static struct pt_regs jprobe_saved_regs;
53 static long *jprobe_saved_rsp;
54 static kprobe_opcode_t *get_insn_slot(void);
55 static void free_insn_slot(kprobe_opcode_t *slot);
56 void jprobe_return_end(void);
57
58 /* copy of the kernel stack at the probe fire time */
59 static kprobe_opcode_t jprobes_stack[MAX_STACK_SIZE];
60
61 /*
62  * returns non-zero if opcode modifies the interrupt flag.
63  */
64 static inline int is_IF_modifier(kprobe_opcode_t *insn)
65 {
66         switch (*insn) {
67         case 0xfa:              /* cli */
68         case 0xfb:              /* sti */
69         case 0xcf:              /* iret/iretd */
70         case 0x9d:              /* popf/popfd */
71                 return 1;
72         }
73
74         if (*insn  >= 0x40 && *insn <= 0x4f && *++insn == 0xcf)
75                 return 1;
76         return 0;
77 }
78
79 int arch_prepare_kprobe(struct kprobe *p)
80 {
81         /* insn: must be on special executable page on x86_64. */
82         up(&kprobe_mutex);
83         p->ainsn.insn = get_insn_slot();
84         down(&kprobe_mutex);
85         if (!p->ainsn.insn) {
86                 return -ENOMEM;
87         }
88         return 0;
89 }
90
91 /*
92  * Determine if the instruction uses the %rip-relative addressing mode.
93  * If it does, return the address of the 32-bit displacement word.
94  * If not, return null.
95  */
96 static inline s32 *is_riprel(u8 *insn)
97 {
98 #define W(row,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bb,bc,bd,be,bf)                \
99         (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) |   \
100           (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) |   \
101           (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) |   \
102           (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf))    \
103          << (row % 64))
104         static const u64 onebyte_has_modrm[256 / 64] = {
105                 /*      0 1 2 3 4 5 6 7 8 9 a b c d e f         */
106                 /*      -------------------------------         */
107                 W(0x00, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 00 */
108                 W(0x10, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 10 */
109                 W(0x20, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 20 */
110                 W(0x30, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0), /* 30 */
111                 W(0x40, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 40 */
112                 W(0x50, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 50 */
113                 W(0x60, 0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,0)| /* 60 */
114                 W(0x70, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 70 */
115                 W(0x80, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 80 */
116                 W(0x90, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 90 */
117                 W(0xa0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* a0 */
118                 W(0xb0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* b0 */
119                 W(0xc0, 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0)| /* c0 */
120                 W(0xd0, 1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1)| /* d0 */
121                 W(0xe0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* e0 */
122                 W(0xf0, 0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1)  /* f0 */
123                 /*      -------------------------------         */
124                 /*      0 1 2 3 4 5 6 7 8 9 a b c d e f         */
125         };
126         static const u64 twobyte_has_modrm[256 / 64] = {
127                 /*      0 1 2 3 4 5 6 7 8 9 a b c d e f         */
128                 /*      -------------------------------         */
129                 W(0x00, 1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,1)| /* 0f */
130                 W(0x10, 1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0)| /* 1f */
131                 W(0x20, 1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1)| /* 2f */
132                 W(0x30, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 3f */
133                 W(0x40, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 4f */
134                 W(0x50, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 5f */
135                 W(0x60, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 6f */
136                 W(0x70, 1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1), /* 7f */
137                 W(0x80, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 8f */
138                 W(0x90, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 9f */
139                 W(0xa0, 0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1)| /* af */
140                 W(0xb0, 1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1), /* bf */
141                 W(0xc0, 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0)| /* cf */
142                 W(0xd0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* df */
143                 W(0xe0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* ef */
144                 W(0xf0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0)  /* ff */
145                 /*      -------------------------------         */
146                 /*      0 1 2 3 4 5 6 7 8 9 a b c d e f         */
147         };
148 #undef  W
149         int need_modrm;
150
151         /* Skip legacy instruction prefixes.  */
152         while (1) {
153                 switch (*insn) {
154                 case 0x66:
155                 case 0x67:
156                 case 0x2e:
157                 case 0x3e:
158                 case 0x26:
159                 case 0x64:
160                 case 0x65:
161                 case 0x36:
162                 case 0xf0:
163                 case 0xf3:
164                 case 0xf2:
165                         ++insn;
166                         continue;
167                 }
168                 break;
169         }
170
171         /* Skip REX instruction prefix.  */
172         if ((*insn & 0xf0) == 0x40)
173                 ++insn;
174
175         if (*insn == 0x0f) {    /* Two-byte opcode.  */
176                 ++insn;
177                 need_modrm = test_bit(*insn, twobyte_has_modrm);
178         } else {                /* One-byte opcode.  */
179                 need_modrm = test_bit(*insn, onebyte_has_modrm);
180         }
181
182         if (need_modrm) {
183                 u8 modrm = *++insn;
184                 if ((modrm & 0xc7) == 0x05) { /* %rip+disp32 addressing mode */
185                         /* Displacement follows ModRM byte.  */
186                         return (s32 *) ++insn;
187                 }
188         }
189
190         /* No %rip-relative addressing mode here.  */
191         return NULL;
192 }
193
194 void arch_copy_kprobe(struct kprobe *p)
195 {
196         s32 *ripdisp;
197         memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE);
198         ripdisp = is_riprel(p->ainsn.insn);
199         if (ripdisp) {
200                 /*
201                  * The copied instruction uses the %rip-relative
202                  * addressing mode.  Adjust the displacement for the
203                  * difference between the original location of this
204                  * instruction and the location of the copy that will
205                  * actually be run.  The tricky bit here is making sure
206                  * that the sign extension happens correctly in this
207                  * calculation, since we need a signed 32-bit result to
208                  * be sign-extended to 64 bits when it's added to the
209                  * %rip value and yield the same 64-bit result that the
210                  * sign-extension of the original signed 32-bit
211                  * displacement would have given.
212                  */
213                 s64 disp = (u8 *) p->addr + *ripdisp - (u8 *) p->ainsn.insn;
214                 BUG_ON((s64) (s32) disp != disp); /* Sanity check.  */
215                 *ripdisp = disp;
216         }
217         p->opcode = *p->addr;
218 }
219
220 void arch_arm_kprobe(struct kprobe *p)
221 {
222         *p->addr = BREAKPOINT_INSTRUCTION;
223         flush_icache_range((unsigned long) p->addr,
224                            (unsigned long) p->addr + sizeof(kprobe_opcode_t));
225 }
226
227 void arch_disarm_kprobe(struct kprobe *p)
228 {
229         *p->addr = p->opcode;
230         flush_icache_range((unsigned long) p->addr,
231                            (unsigned long) p->addr + sizeof(kprobe_opcode_t));
232 }
233
234 void arch_remove_kprobe(struct kprobe *p)
235 {
236         up(&kprobe_mutex);
237         free_insn_slot(p->ainsn.insn);
238         down(&kprobe_mutex);
239 }
240
241 static inline void save_previous_kprobe(void)
242 {
243         kprobe_prev = current_kprobe;
244         kprobe_status_prev = kprobe_status;
245         kprobe_old_rflags_prev = kprobe_old_rflags;
246         kprobe_saved_rflags_prev = kprobe_saved_rflags;
247 }
248
249 static inline void restore_previous_kprobe(void)
250 {
251         current_kprobe = kprobe_prev;
252         kprobe_status = kprobe_status_prev;
253         kprobe_old_rflags = kprobe_old_rflags_prev;
254         kprobe_saved_rflags = kprobe_saved_rflags_prev;
255 }
256
257 static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs)
258 {
259         current_kprobe = p;
260         kprobe_saved_rflags = kprobe_old_rflags
261                 = (regs->eflags & (TF_MASK | IF_MASK));
262         if (is_IF_modifier(p->ainsn.insn))
263                 kprobe_saved_rflags &= ~IF_MASK;
264 }
265
266 static void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
267 {
268         regs->eflags |= TF_MASK;
269         regs->eflags &= ~IF_MASK;
270         /*single step inline if the instruction is an int3*/
271         if (p->opcode == BREAKPOINT_INSTRUCTION)
272                 regs->rip = (unsigned long)p->addr;
273         else
274                 regs->rip = (unsigned long)p->ainsn.insn;
275 }
276
277 struct task_struct  *arch_get_kprobe_task(void *ptr)
278 {
279         return ((struct thread_info *) (((unsigned long) ptr) &
280                                         (~(THREAD_SIZE -1))))->task;
281 }
282
283 void arch_prepare_kretprobe(struct kretprobe *rp, struct pt_regs *regs)
284 {
285         unsigned long *sara = (unsigned long *)regs->rsp;
286         struct kretprobe_instance *ri;
287         static void *orig_ret_addr;
288
289         /*
290          * Save the return address when the return probe hits
291          * the first time, and use it to populate the (krprobe
292          * instance)->ret_addr for subsequent return probes at
293          * the same addrress since stack address would have
294          * the kretprobe_trampoline by then.
295          */
296         if (((void*) *sara) != kretprobe_trampoline)
297                 orig_ret_addr = (void*) *sara;
298
299         if ((ri = get_free_rp_inst(rp)) != NULL) {
300                 ri->rp = rp;
301                 ri->stack_addr = sara;
302                 ri->ret_addr = orig_ret_addr;
303                 add_rp_inst(ri);
304                 /* Replace the return addr with trampoline addr */
305                 *sara = (unsigned long) &kretprobe_trampoline;
306         } else {
307                 rp->nmissed++;
308         }
309 }
310
311 void arch_kprobe_flush_task(struct task_struct *tk)
312 {
313         struct kretprobe_instance *ri;
314         while ((ri = get_rp_inst_tsk(tk)) != NULL) {
315                 *((unsigned long *)(ri->stack_addr)) =
316                                         (unsigned long) ri->ret_addr;
317                 recycle_rp_inst(ri);
318         }
319 }
320
321 /*
322  * Interrupts are disabled on entry as trap3 is an interrupt gate and they
323  * remain disabled thorough out this function.
324  */
325 int kprobe_handler(struct pt_regs *regs)
326 {
327         struct kprobe *p;
328         int ret = 0;
329         kprobe_opcode_t *addr = (kprobe_opcode_t *)(regs->rip - sizeof(kprobe_opcode_t));
330
331         /* We're in an interrupt, but this is clear and BUG()-safe. */
332         preempt_disable();
333
334         /* Check we're not actually recursing */
335         if (kprobe_running()) {
336                 /* We *are* holding lock here, so this is safe.
337                    Disarm the probe we just hit, and ignore it. */
338                 p = get_kprobe(addr);
339                 if (p) {
340                         if (kprobe_status == KPROBE_HIT_SS) {
341                                 regs->eflags &= ~TF_MASK;
342                                 regs->eflags |= kprobe_saved_rflags;
343                                 unlock_kprobes();
344                                 goto no_kprobe;
345                         } else if (kprobe_status == KPROBE_HIT_SSDONE) {
346                                 /* TODO: Provide re-entrancy from
347                                  * post_kprobes_handler() and avoid exception
348                                  * stack corruption while single-stepping on
349                                  * the instruction of the new probe.
350                                  */
351                                 arch_disarm_kprobe(p);
352                                 regs->rip = (unsigned long)p->addr;
353                                 ret = 1;
354                         } else {
355                                 /* We have reentered the kprobe_handler(), since
356                                  * another probe was hit while within the
357                                  * handler. We here save the original kprobe
358                                  * variables and just single step on instruction
359                                  * of the new probe without calling any user
360                                  * handlers.
361                                  */
362                                 save_previous_kprobe();
363                                 set_current_kprobe(p, regs);
364                                 p->nmissed++;
365                                 prepare_singlestep(p, regs);
366                                 kprobe_status = KPROBE_REENTER;
367                                 return 1;
368                         }
369                 } else {
370                         p = current_kprobe;
371                         if (p->break_handler && p->break_handler(p, regs)) {
372                                 goto ss_probe;
373                         }
374                 }
375                 /* If it's not ours, can't be delete race, (we hold lock). */
376                 goto no_kprobe;
377         }
378
379         lock_kprobes();
380         p = get_kprobe(addr);
381         if (!p) {
382                 unlock_kprobes();
383                 if (*addr != BREAKPOINT_INSTRUCTION) {
384                         /*
385                          * The breakpoint instruction was removed right
386                          * after we hit it.  Another cpu has removed
387                          * either a probepoint or a debugger breakpoint
388                          * at this address.  In either case, no further
389                          * handling of this interrupt is appropriate.
390                          */
391                         ret = 1;
392                 }
393                 /* Not one of ours: let kernel handle it */
394                 goto no_kprobe;
395         }
396
397         kprobe_status = KPROBE_HIT_ACTIVE;
398         set_current_kprobe(p, regs);
399
400         if (p->pre_handler && p->pre_handler(p, regs))
401                 /* handler has already set things up, so skip ss setup */
402                 return 1;
403
404 ss_probe:
405         prepare_singlestep(p, regs);
406         kprobe_status = KPROBE_HIT_SS;
407         return 1;
408
409 no_kprobe:
410         preempt_enable_no_resched();
411         return ret;
412 }
413
414 /*
415  * For function-return probes, init_kprobes() establishes a probepoint
416  * here. When a retprobed function returns, this probe is hit and
417  * trampoline_probe_handler() runs, calling the kretprobe's handler.
418  */
419  void kretprobe_trampoline_holder(void)
420  {
421         asm volatile (  ".global kretprobe_trampoline\n"
422                         "kretprobe_trampoline: \n"
423                         "nop\n");
424  }
425
426 /*
427  * Called when we hit the probe point at kretprobe_trampoline
428  */
429 int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
430 {
431         struct task_struct *tsk;
432         struct kretprobe_instance *ri;
433         struct hlist_head *head;
434         struct hlist_node *node;
435         unsigned long *sara = (unsigned long *)regs->rsp - 1;
436
437         tsk = arch_get_kprobe_task(sara);
438         head = kretprobe_inst_table_head(tsk);
439
440         hlist_for_each_entry(ri, node, head, hlist) {
441                 if (ri->stack_addr == sara && ri->rp) {
442                         if (ri->rp->handler)
443                                 ri->rp->handler(ri, regs);
444                 }
445         }
446         return 0;
447 }
448
449 void trampoline_post_handler(struct kprobe *p, struct pt_regs *regs,
450                                                 unsigned long flags)
451 {
452         struct kretprobe_instance *ri;
453         /* RA already popped */
454         unsigned long *sara = ((unsigned long *)regs->rsp) - 1;
455
456         while ((ri = get_rp_inst(sara))) {
457                 regs->rip = (unsigned long)ri->ret_addr;
458                 recycle_rp_inst(ri);
459         }
460         regs->eflags &= ~TF_MASK;
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 rip 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 static void resume_execution(struct kprobe *p, struct pt_regs *regs)
486 {
487         unsigned long *tos = (unsigned long *)regs->rsp;
488         unsigned long next_rip = 0;
489         unsigned long copy_rip = (unsigned long)p->ainsn.insn;
490         unsigned long orig_rip = (unsigned long)p->addr;
491         kprobe_opcode_t *insn = p->ainsn.insn;
492
493         /*skip the REX prefix*/
494         if (*insn >= 0x40 && *insn <= 0x4f)
495                 insn++;
496
497         switch (*insn) {
498         case 0x9c:              /* pushfl */
499                 *tos &= ~(TF_MASK | IF_MASK);
500                 *tos |= kprobe_old_rflags;
501                 break;
502         case 0xc3:              /* ret/lret */
503         case 0xcb:
504         case 0xc2:
505         case 0xca:
506                 regs->eflags &= ~TF_MASK;
507                 /* rip is already adjusted, no more changes required*/
508                 return;
509         case 0xe8:              /* call relative - Fix return addr */
510                 *tos = orig_rip + (*tos - copy_rip);
511                 break;
512         case 0xff:
513                 if ((*insn & 0x30) == 0x10) {
514                         /* call absolute, indirect */
515                         /* Fix return addr; rip is correct. */
516                         next_rip = regs->rip;
517                         *tos = orig_rip + (*tos - copy_rip);
518                 } else if (((*insn & 0x31) == 0x20) ||  /* jmp near, absolute indirect */
519                            ((*insn & 0x31) == 0x21)) {  /* jmp far, absolute indirect */
520                         /* rip is correct. */
521                         next_rip = regs->rip;
522                 }
523                 break;
524         case 0xea:              /* jmp absolute -- rip is correct */
525                 next_rip = regs->rip;
526                 break;
527         default:
528                 break;
529         }
530
531         regs->eflags &= ~TF_MASK;
532         if (next_rip) {
533                 regs->rip = next_rip;
534         } else {
535                 regs->rip = orig_rip + (regs->rip - copy_rip);
536         }
537 }
538
539 /*
540  * Interrupts are disabled on entry as trap1 is an interrupt gate and they
541  * remain disabled thoroughout this function.  And we hold kprobe lock.
542  */
543 int post_kprobe_handler(struct pt_regs *regs)
544 {
545         if (!kprobe_running())
546                 return 0;
547
548         if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) {
549                 kprobe_status = KPROBE_HIT_SSDONE;
550                 current_kprobe->post_handler(current_kprobe, regs, 0);
551         }
552
553         if (current_kprobe->post_handler != trampoline_post_handler)
554                 resume_execution(current_kprobe, regs);
555         regs->eflags |= kprobe_saved_rflags;
556
557         /* Restore the original saved kprobes variables and continue. */
558         if (kprobe_status == KPROBE_REENTER) {
559                 restore_previous_kprobe();
560                 goto out;
561         } else {
562                 unlock_kprobes();
563         }
564 out:
565         preempt_enable_no_resched();
566
567         /*
568          * if somebody else is singlestepping across a probe point, eflags
569          * will have TF set, in which case, continue the remaining processing
570          * of do_debug, as if this is not a probe hit.
571          */
572         if (regs->eflags & TF_MASK)
573                 return 0;
574
575         return 1;
576 }
577
578 /* Interrupts disabled, kprobe_lock held. */
579 int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
580 {
581         if (current_kprobe->fault_handler
582             && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
583                 return 1;
584
585         if (kprobe_status & KPROBE_HIT_SS) {
586                 resume_execution(current_kprobe, regs);
587                 regs->eflags |= kprobe_old_rflags;
588
589                 unlock_kprobes();
590                 preempt_enable_no_resched();
591         }
592         return 0;
593 }
594
595 /*
596  * Wrapper routine for handling exceptions.
597  */
598 int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
599                              void *data)
600 {
601         struct die_args *args = (struct die_args *)data;
602         switch (val) {
603         case DIE_INT3:
604                 if (kprobe_handler(args->regs))
605                         return NOTIFY_STOP;
606                 break;
607         case DIE_DEBUG:
608                 if (post_kprobe_handler(args->regs))
609                         return NOTIFY_STOP;
610                 break;
611         case DIE_GPF:
612                 if (kprobe_running() &&
613                     kprobe_fault_handler(args->regs, args->trapnr))
614                         return NOTIFY_STOP;
615                 break;
616         case DIE_PAGE_FAULT:
617                 if (kprobe_running() &&
618                     kprobe_fault_handler(args->regs, args->trapnr))
619                         return NOTIFY_STOP;
620                 break;
621         default:
622                 break;
623         }
624         return NOTIFY_DONE;
625 }
626
627 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
628 {
629         struct jprobe *jp = container_of(p, struct jprobe, kp);
630         unsigned long addr;
631
632         jprobe_saved_regs = *regs;
633         jprobe_saved_rsp = (long *) regs->rsp;
634         addr = (unsigned long)jprobe_saved_rsp;
635         /*
636          * As Linus pointed out, gcc assumes that the callee
637          * owns the argument space and could overwrite it, e.g.
638          * tailcall optimization. So, to be absolutely safe
639          * we also save and restore enough stack bytes to cover
640          * the argument area.
641          */
642         memcpy(jprobes_stack, (kprobe_opcode_t *) addr, MIN_STACK_SIZE(addr));
643         regs->eflags &= ~IF_MASK;
644         regs->rip = (unsigned long)(jp->entry);
645         return 1;
646 }
647
648 void jprobe_return(void)
649 {
650         preempt_enable_no_resched();
651         asm volatile ("       xchg   %%rbx,%%rsp     \n"
652                       "       int3                      \n"
653                       "       .globl jprobe_return_end  \n"
654                       "       jprobe_return_end:        \n"
655                       "       nop                       \n"::"b"
656                       (jprobe_saved_rsp):"memory");
657 }
658
659 int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
660 {
661         u8 *addr = (u8 *) (regs->rip - 1);
662         unsigned long stack_addr = (unsigned long)jprobe_saved_rsp;
663         struct jprobe *jp = container_of(p, struct jprobe, kp);
664
665         if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
666                 if ((long *)regs->rsp != jprobe_saved_rsp) {
667                         struct pt_regs *saved_regs =
668                             container_of(jprobe_saved_rsp, struct pt_regs, rsp);
669                         printk("current rsp %p does not match saved rsp %p\n",
670                                (long *)regs->rsp, jprobe_saved_rsp);
671                         printk("Saved registers for jprobe %p\n", jp);
672                         show_registers(saved_regs);
673                         printk("Current registers\n");
674                         show_registers(regs);
675                         BUG();
676                 }
677                 *regs = jprobe_saved_regs;
678                 memcpy((kprobe_opcode_t *) stack_addr, jprobes_stack,
679                        MIN_STACK_SIZE(stack_addr));
680                 return 1;
681         }
682         return 0;
683 }
684
685 /*
686  * kprobe->ainsn.insn points to the copy of the instruction to be single-stepped.
687  * By default on x86_64, pages we get from kmalloc or vmalloc are not
688  * executable.  Single-stepping an instruction on such a page yields an
689  * oops.  So instead of storing the instruction copies in their respective
690  * kprobe objects, we allocate a page, map it executable, and store all the
691  * instruction copies there.  (We can allocate additional pages if somebody
692  * inserts a huge number of probes.)  Each page can hold up to INSNS_PER_PAGE
693  * instruction slots, each of which is MAX_INSN_SIZE*sizeof(kprobe_opcode_t)
694  * bytes.
695  */
696 #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE*sizeof(kprobe_opcode_t)))
697 struct kprobe_insn_page {
698         struct hlist_node hlist;
699         kprobe_opcode_t *insns;         /* page of instruction slots */
700         char slot_used[INSNS_PER_PAGE];
701         int nused;
702 };
703
704 static struct hlist_head kprobe_insn_pages;
705
706 /**
707  * get_insn_slot() - Find a slot on an executable page for an instruction.
708  * We allocate an executable page if there's no room on existing ones.
709  */
710 static kprobe_opcode_t *get_insn_slot(void)
711 {
712         struct kprobe_insn_page *kip;
713         struct hlist_node *pos;
714
715         hlist_for_each(pos, &kprobe_insn_pages) {
716                 kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
717                 if (kip->nused < INSNS_PER_PAGE) {
718                         int i;
719                         for (i = 0; i < INSNS_PER_PAGE; i++) {
720                                 if (!kip->slot_used[i]) {
721                                         kip->slot_used[i] = 1;
722                                         kip->nused++;
723                                         return kip->insns + (i*MAX_INSN_SIZE);
724                                 }
725                         }
726                         /* Surprise!  No unused slots.  Fix kip->nused. */
727                         kip->nused = INSNS_PER_PAGE;
728                 }
729         }
730
731         /* All out of space.  Need to allocate a new page. Use slot 0.*/
732         kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
733         if (!kip) {
734                 return NULL;
735         }
736
737         /*
738          * For the %rip-relative displacement fixups to be doable, we
739          * need our instruction copy to be within +/- 2GB of any data it
740          * might access via %rip.  That is, within 2GB of where the
741          * kernel image and loaded module images reside.  So we allocate
742          * a page in the module loading area.
743          */
744         kip->insns = module_alloc(PAGE_SIZE);
745         if (!kip->insns) {
746                 kfree(kip);
747                 return NULL;
748         }
749         INIT_HLIST_NODE(&kip->hlist);
750         hlist_add_head(&kip->hlist, &kprobe_insn_pages);
751         memset(kip->slot_used, 0, INSNS_PER_PAGE);
752         kip->slot_used[0] = 1;
753         kip->nused = 1;
754         return kip->insns;
755 }
756
757 /**
758  * free_insn_slot() - Free instruction slot obtained from get_insn_slot().
759  */
760 static void free_insn_slot(kprobe_opcode_t *slot)
761 {
762         struct kprobe_insn_page *kip;
763         struct hlist_node *pos;
764
765         hlist_for_each(pos, &kprobe_insn_pages) {
766                 kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
767                 if (kip->insns <= slot
768                     && slot < kip->insns+(INSNS_PER_PAGE*MAX_INSN_SIZE)) {
769                         int i = (slot - kip->insns) / MAX_INSN_SIZE;
770                         kip->slot_used[i] = 0;
771                         kip->nused--;
772                         if (kip->nused == 0) {
773                                 /*
774                                  * Page is no longer in use.  Free it unless
775                                  * it's the last one.  We keep the last one
776                                  * so as not to have to set it up again the
777                                  * next time somebody inserts a probe.
778                                  */
779                                 hlist_del(&kip->hlist);
780                                 if (hlist_empty(&kprobe_insn_pages)) {
781                                         INIT_HLIST_NODE(&kip->hlist);
782                                         hlist_add_head(&kip->hlist,
783                                                 &kprobe_insn_pages);
784                                 } else {
785                                         module_free(NULL, kip->insns);
786                                         kfree(kip);
787                                 }
788                         }
789                         return;
790                 }
791         }
792 }