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