2d7763749b1b6daa73e39174fbf6cd04917b8f5a
[cascardo/linux.git] / arch / x86 / kernel / kprobes_64.c
1 /*
2  *  Kernel Probes (KProbes)
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * Copyright (C) IBM Corporation, 2002, 2004
19  *
20  * 2002-Oct     Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
21  *              Probes initial implementation ( includes contributions from
22  *              Rusty Russell).
23  * 2004-July    Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
24  *              interface to access function arguments.
25  * 2004-Oct     Jim Keniston <kenistoj@us.ibm.com> and Prasanna S Panchamukhi
26  *              <prasanna@in.ibm.com> adapted for x86_64
27  * 2005-Mar     Roland McGrath <roland@redhat.com>
28  *              Fixed to handle %rip-relative addressing mode correctly.
29  * 2005-May     Rusty Lynch <rusty.lynch@intel.com>
30  *              Added function return probes functionality
31  * 2007-Dec     Masami Hiramatsu <mhiramat@redhat.com> added kprobe-booster
32  *              and kretprobe-booster for x86-64
33  */
34
35 #include <linux/kprobes.h>
36 #include <linux/ptrace.h>
37 #include <linux/string.h>
38 #include <linux/slab.h>
39 #include <linux/preempt.h>
40 #include <linux/module.h>
41 #include <linux/kdebug.h>
42
43 #include <asm/cacheflush.h>
44 #include <asm/desc.h>
45 #include <asm/pgtable.h>
46 #include <asm/uaccess.h>
47 #include <asm/alternative.h>
48
49 void jprobe_return_end(void);
50
51 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
52 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
53
54 #define stack_addr(regs) ((unsigned long *)regs->sp)
55
56 #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
57         (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) |   \
58           (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) |   \
59           (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) |   \
60           (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf))    \
61          << (row % 32))
62         /*
63          * Undefined/reserved opcodes, conditional jump, Opcode Extension
64          * Groups, and some special opcodes can not boost.
65          */
66 static const u32 twobyte_is_boostable[256 / 32] = {
67         /*      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f          */
68         /*      ----------------------------------------------          */
69         W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
70         W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 10 */
71         W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
72         W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
73         W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
74         W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
75         W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
76         W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
77         W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
78         W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
79         W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
80         W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
81         W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
82         W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
83         W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
84         W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0)   /* f0 */
85         /*      -----------------------------------------------         */
86         /*      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f          */
87 };
88 static const u32 onebyte_has_modrm[256 / 32] = {
89         /*      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f          */
90         /*      -----------------------------------------------         */
91         W(0x00, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 00 */
92         W(0x10, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) , /* 10 */
93         W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 20 */
94         W(0x30, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) , /* 30 */
95         W(0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 40 */
96         W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
97         W(0x60, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0) | /* 60 */
98         W(0x70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 70 */
99         W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
100         W(0x90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 90 */
101         W(0xa0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* a0 */
102         W(0xb0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* b0 */
103         W(0xc0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0) | /* c0 */
104         W(0xd0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
105         W(0xe0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* e0 */
106         W(0xf0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1)   /* f0 */
107         /*      -----------------------------------------------         */
108         /*      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f          */
109 };
110 static const u32 twobyte_has_modrm[256 / 32] = {
111         /*      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f          */
112         /*      -----------------------------------------------         */
113         W(0x00, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1) | /* 0f */
114         W(0x10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0) , /* 1f */
115         W(0x20, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* 2f */
116         W(0x30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 3f */
117         W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 4f */
118         W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 5f */
119         W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 6f */
120         W(0x70, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1) , /* 7f */
121         W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 8f */
122         W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 9f */
123         W(0xa0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) | /* af */
124         W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1) , /* bf */
125         W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0) | /* cf */
126         W(0xd0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* df */
127         W(0xe0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* ef */
128         W(0xf0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0)   /* ff */
129         /*      -----------------------------------------------         */
130         /*      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f          */
131 };
132 #undef W
133
134 struct kretprobe_blackpoint kretprobe_blacklist[] = {
135         {"__switch_to", }, /* This function switches only current task, but
136                               doesn't switch kernel stack.*/
137         {NULL, NULL}    /* Terminator */
138 };
139 const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
140
141 /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
142 static __always_inline void set_jmp_op(void *from, void *to)
143 {
144         struct __arch_jmp_op {
145                 char op;
146                 s32 raddr;
147         } __attribute__((packed)) * jop;
148         jop = (struct __arch_jmp_op *)from;
149         jop->raddr = (s32)((long)(to) - ((long)(from) + 5));
150         jop->op = RELATIVEJUMP_INSTRUCTION;
151 }
152
153 /*
154  * returns non-zero if opcode is boostable.
155  * RIP relative instructions are adjusted at copying time
156  */
157 static __always_inline int can_boost(kprobe_opcode_t *opcodes)
158 {
159         kprobe_opcode_t opcode;
160         kprobe_opcode_t *orig_opcodes = opcodes;
161
162 retry:
163         if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
164                 return 0;
165         opcode = *(opcodes++);
166
167         /* 2nd-byte opcode */
168         if (opcode == 0x0f) {
169                 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
170                         return 0;
171                 return test_bit(*opcodes,
172                                 (unsigned long *)twobyte_is_boostable);
173         }
174
175         switch (opcode & 0xf0) {
176         case 0x40:
177                 goto retry; /* REX prefix is boostable */
178         case 0x60:
179                 if (0x63 < opcode && opcode < 0x67)
180                         goto retry; /* prefixes */
181                 /* can't boost Address-size override and bound */
182                 return (opcode != 0x62 && opcode != 0x67);
183         case 0x70:
184                 return 0; /* can't boost conditional jump */
185         case 0xc0:
186                 /* can't boost software-interruptions */
187                 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
188         case 0xd0:
189                 /* can boost AA* and XLAT */
190                 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
191         case 0xe0:
192                 /* can boost in/out and absolute jmps */
193                 return ((opcode & 0x04) || opcode == 0xea);
194         case 0xf0:
195                 if ((opcode & 0x0c) == 0 && opcode != 0xf1)
196                         goto retry; /* lock/rep(ne) prefix */
197                 /* clear and set flags are boostable */
198                 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
199         default:
200                 /* segment override prefixes are boostable */
201                 if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
202                         goto retry; /* prefixes */
203                 /* CS override prefix and call are not boostable */
204                 return (opcode != 0x2e && opcode != 0x9a);
205         }
206 }
207
208 /*
209  * returns non-zero if opcode modifies the interrupt flag.
210  */
211 static int __kprobes is_IF_modifier(kprobe_opcode_t *insn)
212 {
213         switch (*insn) {
214         case 0xfa:              /* cli */
215         case 0xfb:              /* sti */
216         case 0xcf:              /* iret/iretd */
217         case 0x9d:              /* popf/popfd */
218                 return 1;
219         }
220
221         /*
222          * on 64 bit x86, 0x40-0x4f are prefixes so we need to look
223          * at the next byte instead.. but of course not recurse infinitely
224          */
225         if (*insn  >= 0x40 && *insn <= 0x4f)
226                 return is_IF_modifier(++insn);
227         return 0;
228 }
229
230 /*
231  * Adjust the displacement if the instruction uses the %rip-relative
232  * addressing mode.
233  * If it does, Return the address of the 32-bit displacement word.
234  * If not, return null.
235  */
236 static void __kprobes fix_riprel(struct kprobe *p)
237 {
238         u8 *insn = p->ainsn.insn;
239         s64 disp;
240         int need_modrm;
241
242         /* Skip legacy instruction prefixes.  */
243         while (1) {
244                 switch (*insn) {
245                 case 0x66:
246                 case 0x67:
247                 case 0x2e:
248                 case 0x3e:
249                 case 0x26:
250                 case 0x64:
251                 case 0x65:
252                 case 0x36:
253                 case 0xf0:
254                 case 0xf3:
255                 case 0xf2:
256                         ++insn;
257                         continue;
258                 }
259                 break;
260         }
261
262         /* Skip REX instruction prefix.  */
263         if ((*insn & 0xf0) == 0x40)
264                 ++insn;
265
266         if (*insn == 0x0f) {    /* Two-byte opcode.  */
267                 ++insn;
268                 need_modrm = test_bit(*insn,
269                                       (unsigned long *)twobyte_has_modrm);
270         } else                  /* One-byte opcode.  */
271                 need_modrm = test_bit(*insn,
272                                       (unsigned long *)onebyte_has_modrm);
273
274         if (need_modrm) {
275                 u8 modrm = *++insn;
276                 if ((modrm & 0xc7) == 0x05) { /* %rip+disp32 addressing mode */
277                         /* Displacement follows ModRM byte.  */
278                         ++insn;
279                         /*
280                          * The copied instruction uses the %rip-relative
281                          * addressing mode.  Adjust the displacement for the
282                          * difference between the original location of this
283                          * instruction and the location of the copy that will
284                          * actually be run.  The tricky bit here is making sure
285                          * that the sign extension happens correctly in this
286                          * calculation, since we need a signed 32-bit result to
287                          * be sign-extended to 64 bits when it's added to the
288                          * %rip value and yield the same 64-bit result that the
289                          * sign-extension of the original signed 32-bit
290                          * displacement would have given.
291                          */
292                         disp = (u8 *) p->addr + *((s32 *) insn) -
293                                (u8 *) p->ainsn.insn;
294                         BUG_ON((s64) (s32) disp != disp); /* Sanity check.  */
295                         *(s32 *)insn = (s32) disp;
296                 }
297         }
298 }
299
300 static void __kprobes arch_copy_kprobe(struct kprobe *p)
301 {
302         memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
303         fix_riprel(p);
304         if (can_boost(p->addr))
305                 p->ainsn.boostable = 0;
306         else
307                 p->ainsn.boostable = -1;
308
309         p->opcode = *p->addr;
310 }
311
312 int __kprobes arch_prepare_kprobe(struct kprobe *p)
313 {
314         /* insn: must be on special executable page on x86. */
315         p->ainsn.insn = get_insn_slot();
316         if (!p->ainsn.insn)
317                 return -ENOMEM;
318         arch_copy_kprobe(p);
319         return 0;
320 }
321
322 void __kprobes arch_arm_kprobe(struct kprobe *p)
323 {
324         text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
325 }
326
327 void __kprobes arch_disarm_kprobe(struct kprobe *p)
328 {
329         text_poke(p->addr, &p->opcode, 1);
330 }
331
332 void __kprobes arch_remove_kprobe(struct kprobe *p)
333 {
334         mutex_lock(&kprobe_mutex);
335         free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
336         mutex_unlock(&kprobe_mutex);
337 }
338
339 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
340 {
341         kcb->prev_kprobe.kp = kprobe_running();
342         kcb->prev_kprobe.status = kcb->kprobe_status;
343         kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
344         kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
345 }
346
347 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
348 {
349         __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
350         kcb->kprobe_status = kcb->prev_kprobe.status;
351         kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
352         kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
353 }
354
355 static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
356                                 struct kprobe_ctlblk *kcb)
357 {
358         __get_cpu_var(current_kprobe) = p;
359         kcb->kprobe_saved_flags = kcb->kprobe_old_flags
360                 = (regs->flags & (TF_MASK | IF_MASK));
361         if (is_IF_modifier(p->ainsn.insn))
362                 kcb->kprobe_saved_flags &= ~IF_MASK;
363 }
364
365 static __always_inline void clear_btf(void)
366 {
367         if (test_thread_flag(TIF_DEBUGCTLMSR))
368                 wrmsrl(MSR_IA32_DEBUGCTLMSR, 0);
369 }
370
371 static __always_inline void restore_btf(void)
372 {
373         if (test_thread_flag(TIF_DEBUGCTLMSR))
374                 wrmsrl(MSR_IA32_DEBUGCTLMSR, current->thread.debugctlmsr);
375 }
376
377 static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
378 {
379         clear_btf();
380         regs->flags |= TF_MASK;
381         regs->flags &= ~IF_MASK;
382         /*single step inline if the instruction is an int3*/
383         if (p->opcode == BREAKPOINT_INSTRUCTION)
384                 regs->ip = (unsigned long)p->addr;
385         else
386                 regs->ip = (unsigned long)p->ainsn.insn;
387 }
388
389 /* Called with kretprobe_lock held */
390 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
391                                       struct pt_regs *regs)
392 {
393         unsigned long *sara = stack_addr(regs);
394
395         ri->ret_addr = (kprobe_opcode_t *) *sara;
396
397         /* Replace the return addr with trampoline addr */
398         *sara = (unsigned long) &kretprobe_trampoline;
399 }
400
401 /*
402  * Interrupts are disabled on entry as trap3 is an interrupt gate and they
403  * remain disabled thorough out this function.
404  */
405 static int __kprobes kprobe_handler(struct pt_regs *regs)
406 {
407         struct kprobe *p;
408         int ret = 0;
409         kprobe_opcode_t *addr;
410         struct kprobe_ctlblk *kcb;
411
412         addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
413
414         /*
415          * We don't want to be preempted for the entire
416          * duration of kprobe processing
417          */
418         preempt_disable();
419         kcb = get_kprobe_ctlblk();
420
421         /* Check we're not actually recursing */
422         if (kprobe_running()) {
423                 p = get_kprobe(addr);
424                 if (p) {
425                         if (kcb->kprobe_status == KPROBE_HIT_SS &&
426                                 *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
427                                 regs->flags &= ~TF_MASK;
428                                 regs->flags |= kcb->kprobe_saved_flags;
429                                 goto no_kprobe;
430                         } else if (kcb->kprobe_status == KPROBE_HIT_SSDONE) {
431                                 /* TODO: Provide re-entrancy from
432                                  * post_kprobes_handler() and avoid exception
433                                  * stack corruption while single-stepping on
434                                  * the instruction of the new probe.
435                                  */
436                                 arch_disarm_kprobe(p);
437                                 regs->ip = (unsigned long)p->addr;
438                                 reset_current_kprobe();
439                                 return 1;
440                         }
441                         /* We have reentered the kprobe_handler(), since
442                          * another probe was hit while within the handler.
443                          * We here save the original kprobes variables and
444                          * just single step on the instruction of the new probe
445                          * without calling any user handlers.
446                          */
447                         save_previous_kprobe(kcb);
448                         set_current_kprobe(p, regs, kcb);
449                         kprobes_inc_nmissed_count(p);
450                         prepare_singlestep(p, regs);
451                         kcb->kprobe_status = KPROBE_REENTER;
452                         return 1;
453                 } else {
454                         if (*addr != BREAKPOINT_INSTRUCTION) {
455                         /* The breakpoint instruction was removed by
456                          * another cpu right after we hit, no further
457                          * handling of this interrupt is appropriate
458                          */
459                                 regs->ip = (unsigned long)addr;
460                                 ret = 1;
461                                 goto no_kprobe;
462                         }
463                         p = __get_cpu_var(current_kprobe);
464                         if (p->break_handler && p->break_handler(p, regs)) {
465                                 goto ss_probe;
466                         }
467                 }
468                 goto no_kprobe;
469         }
470
471         p = get_kprobe(addr);
472         if (!p) {
473                 if (*addr != BREAKPOINT_INSTRUCTION) {
474                         /*
475                          * The breakpoint instruction was removed right
476                          * after we hit it.  Another cpu has removed
477                          * either a probepoint or a debugger breakpoint
478                          * at this address.  In either case, no further
479                          * handling of this interrupt is appropriate.
480                          * Back up over the (now missing) int3 and run
481                          * the original instruction.
482                          */
483                         regs->ip = (unsigned long)addr;
484                         ret = 1;
485                 }
486                 /* Not one of ours: let kernel handle it */
487                 goto no_kprobe;
488         }
489
490         set_current_kprobe(p, regs, kcb);
491         kcb->kprobe_status = KPROBE_HIT_ACTIVE;
492
493         if (p->pre_handler && p->pre_handler(p, regs))
494                 /* handler has already set things up, so skip ss setup */
495                 return 1;
496
497 ss_probe:
498 #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PM)
499         if (p->ainsn.boostable == 1 && !p->post_handler) {
500                 /* Boost up -- we can execute copied instructions directly */
501                 reset_current_kprobe();
502                 regs->ip = (unsigned long)p->ainsn.insn;
503                 preempt_enable_no_resched();
504                 return 1;
505         }
506 #endif
507         prepare_singlestep(p, regs);
508         kcb->kprobe_status = KPROBE_HIT_SS;
509         return 1;
510
511 no_kprobe:
512         preempt_enable_no_resched();
513         return ret;
514 }
515
516 /*
517  * When a retprobed function returns, this code saves registers and
518  * calls trampoline_handler() runs, which calls the kretprobe's handler.
519  */
520  void __kprobes kretprobe_trampoline_holder(void)
521  {
522         asm volatile (  ".global kretprobe_trampoline\n"
523                         "kretprobe_trampoline: \n"
524                         /* We don't bother saving the ss register */
525                         "       pushq %rsp\n"
526                         "       pushfq\n"
527                         /*
528                          * Skip cs, ip, orig_ax.
529                          * trampoline_handler() will plug in these values
530                          */
531                         "       subq $24, %rsp\n"
532                         "       pushq %rdi\n"
533                         "       pushq %rsi\n"
534                         "       pushq %rdx\n"
535                         "       pushq %rcx\n"
536                         "       pushq %rax\n"
537                         "       pushq %r8\n"
538                         "       pushq %r9\n"
539                         "       pushq %r10\n"
540                         "       pushq %r11\n"
541                         "       pushq %rbx\n"
542                         "       pushq %rbp\n"
543                         "       pushq %r12\n"
544                         "       pushq %r13\n"
545                         "       pushq %r14\n"
546                         "       pushq %r15\n"
547                         "       movq %rsp, %rdi\n"
548                         "       call trampoline_handler\n"
549                         /* Replace saved sp with true return address. */
550                         "       movq %rax, 152(%rsp)\n"
551                         "       popq %r15\n"
552                         "       popq %r14\n"
553                         "       popq %r13\n"
554                         "       popq %r12\n"
555                         "       popq %rbp\n"
556                         "       popq %rbx\n"
557                         "       popq %r11\n"
558                         "       popq %r10\n"
559                         "       popq %r9\n"
560                         "       popq %r8\n"
561                         "       popq %rax\n"
562                         "       popq %rcx\n"
563                         "       popq %rdx\n"
564                         "       popq %rsi\n"
565                         "       popq %rdi\n"
566                         /* Skip orig_ax, ip, cs */
567                         "       addq $24, %rsp\n"
568                         "       popfq\n"
569                         "       ret\n");
570  }
571
572 /*
573  * Called from kretprobe_trampoline
574  */
575 fastcall void * __kprobes trampoline_handler(struct pt_regs *regs)
576 {
577         struct kretprobe_instance *ri = NULL;
578         struct hlist_head *head, empty_rp;
579         struct hlist_node *node, *tmp;
580         unsigned long flags, orig_ret_address = 0;
581         unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
582
583         INIT_HLIST_HEAD(&empty_rp);
584         spin_lock_irqsave(&kretprobe_lock, flags);
585         head = kretprobe_inst_table_head(current);
586         /* fixup registers */
587         regs->cs = __KERNEL_CS;
588         regs->ip = trampoline_address;
589         regs->orig_ax = ~0UL;
590
591         /*
592          * It is possible to have multiple instances associated with a given
593          * task either because multiple functions in the call path have
594          * return probes installed on them, and/or more then one
595          * return probe was registered for a target function.
596          *
597          * We can handle this because:
598          *     - instances are always pushed into the head of the list
599          *     - when multiple return probes are registered for the same
600          *       function, the (chronologically) first instance's ret_addr
601          *       will be the real return address, and all the rest will
602          *       point to kretprobe_trampoline.
603          */
604         hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
605                 if (ri->task != current)
606                         /* another task is sharing our hash bucket */
607                         continue;
608
609                 if (ri->rp && ri->rp->handler) {
610                         __get_cpu_var(current_kprobe) = &ri->rp->kp;
611                         get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
612                         ri->rp->handler(ri, regs);
613                         __get_cpu_var(current_kprobe) = NULL;
614                 }
615
616                 orig_ret_address = (unsigned long)ri->ret_addr;
617                 recycle_rp_inst(ri, &empty_rp);
618
619                 if (orig_ret_address != trampoline_address)
620                         /*
621                          * This is the real return address. Any other
622                          * instances associated with this task are for
623                          * other calls deeper on the call stack
624                          */
625                         break;
626         }
627
628         kretprobe_assert(ri, orig_ret_address, trampoline_address);
629
630         spin_unlock_irqrestore(&kretprobe_lock, flags);
631
632         hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
633                 hlist_del(&ri->hlist);
634                 kfree(ri);
635         }
636         return (void *)orig_ret_address;
637 }
638
639 /*
640  * Called after single-stepping.  p->addr is the address of the
641  * instruction whose first byte has been replaced by the "int 3"
642  * instruction.  To avoid the SMP problems that can occur when we
643  * temporarily put back the original opcode to single-step, we
644  * single-stepped a copy of the instruction.  The address of this
645  * copy is p->ainsn.insn.
646  *
647  * This function prepares to return from the post-single-step
648  * interrupt.  We have to fix up the stack as follows:
649  *
650  * 0) Except in the case of absolute or indirect jump or call instructions,
651  * the new ip is relative to the copied instruction.  We need to make
652  * it relative to the original instruction.
653  *
654  * 1) If the single-stepped instruction was pushfl, then the TF and IF
655  * flags are set in the just-pushed flags, and may need to be cleared.
656  *
657  * 2) If the single-stepped instruction was a call, the return address
658  * that is atop the stack is the address following the copied instruction.
659  * We need to make it the address following the original instruction.
660  *
661  * If this is the first time we've single-stepped the instruction at
662  * this probepoint, and the instruction is boostable, boost it: add a
663  * jump instruction after the copied instruction, that jumps to the next
664  * instruction after the probepoint.
665  */
666 static void __kprobes resume_execution(struct kprobe *p,
667                 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
668 {
669         unsigned long *tos = stack_addr(regs);
670         unsigned long copy_ip = (unsigned long)p->ainsn.insn;
671         unsigned long orig_ip = (unsigned long)p->addr;
672         kprobe_opcode_t *insn = p->ainsn.insn;
673
674         /*skip the REX prefix*/
675         if (*insn >= 0x40 && *insn <= 0x4f)
676                 insn++;
677
678         regs->flags &= ~TF_MASK;
679         switch (*insn) {
680         case 0x9c:      /* pushfl */
681                 *tos &= ~(TF_MASK | IF_MASK);
682                 *tos |= kcb->kprobe_old_flags;
683                 break;
684         case 0xc2:      /* iret/ret/lret */
685         case 0xc3:
686         case 0xca:
687         case 0xcb:
688         case 0xcf:
689         case 0xea:      /* jmp absolute -- ip is correct */
690                 /* ip is already adjusted, no more changes required */
691                 p->ainsn.boostable = 1;
692                 goto no_change;
693         case 0xe8:      /* call relative - Fix return addr */
694                 *tos = orig_ip + (*tos - copy_ip);
695                 break;
696         case 0xff:
697                 if ((insn[1] & 0x30) == 0x10) {
698                         /*
699                          * call absolute, indirect
700                          * Fix return addr; ip is correct.
701                          * But this is not boostable
702                          */
703                         *tos = orig_ip + (*tos - copy_ip);
704                         goto no_change;
705                 } else if (((insn[1] & 0x31) == 0x20) ||
706                            ((insn[1] & 0x31) == 0x21)) {
707                         /*
708                          * jmp near and far, absolute indirect
709                          * ip is correct. And this is boostable
710                          */
711                         p->ainsn.boostable = 1;
712                         goto no_change;
713                 }
714         default:
715                 break;
716         }
717
718         if (p->ainsn.boostable == 0) {
719                 if ((regs->ip > copy_ip) &&
720                     (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
721                         /*
722                          * These instructions can be executed directly if it
723                          * jumps back to correct address.
724                          */
725                         set_jmp_op((void *)regs->ip,
726                                    (void *)orig_ip + (regs->ip - copy_ip));
727                         p->ainsn.boostable = 1;
728                 } else {
729                         p->ainsn.boostable = -1;
730                 }
731         }
732
733         regs->ip += orig_ip - copy_ip;
734
735 no_change:
736         restore_btf();
737
738         return;
739 }
740
741 /*
742  * Interrupts are disabled on entry as trap1 is an interrupt gate and they
743  * remain disabled thoroughout this function.
744  */
745 static int __kprobes post_kprobe_handler(struct pt_regs *regs)
746 {
747         struct kprobe *cur = kprobe_running();
748         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
749
750         if (!cur)
751                 return 0;
752
753         if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
754                 kcb->kprobe_status = KPROBE_HIT_SSDONE;
755                 cur->post_handler(cur, regs, 0);
756         }
757
758         resume_execution(cur, regs, kcb);
759         regs->flags |= kcb->kprobe_saved_flags;
760         trace_hardirqs_fixup_flags(regs->flags);
761
762         /* Restore back the original saved kprobes variables and continue. */
763         if (kcb->kprobe_status == KPROBE_REENTER) {
764                 restore_previous_kprobe(kcb);
765                 goto out;
766         }
767         reset_current_kprobe();
768 out:
769         preempt_enable_no_resched();
770
771         /*
772          * if somebody else is singlestepping across a probe point, flags
773          * will have TF set, in which case, continue the remaining processing
774          * of do_debug, as if this is not a probe hit.
775          */
776         if (regs->flags & TF_MASK)
777                 return 0;
778
779         return 1;
780 }
781
782 int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
783 {
784         struct kprobe *cur = kprobe_running();
785         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
786         const struct exception_table_entry *fixup;
787
788         switch(kcb->kprobe_status) {
789         case KPROBE_HIT_SS:
790         case KPROBE_REENTER:
791                 /*
792                  * We are here because the instruction being single
793                  * stepped caused a page fault. We reset the current
794                  * kprobe and the ip points back to the probe address
795                  * and allow the page fault handler to continue as a
796                  * normal page fault.
797                  */
798                 regs->ip = (unsigned long)cur->addr;
799                 regs->flags |= kcb->kprobe_old_flags;
800                 if (kcb->kprobe_status == KPROBE_REENTER)
801                         restore_previous_kprobe(kcb);
802                 else
803                         reset_current_kprobe();
804                 preempt_enable_no_resched();
805                 break;
806         case KPROBE_HIT_ACTIVE:
807         case KPROBE_HIT_SSDONE:
808                 /*
809                  * We increment the nmissed count for accounting,
810                  * we can also use npre/npostfault count for accounting
811                  * these specific fault cases.
812                  */
813                 kprobes_inc_nmissed_count(cur);
814
815                 /*
816                  * We come here because instructions in the pre/post
817                  * handler caused the page_fault, this could happen
818                  * if handler tries to access user space by
819                  * copy_from_user(), get_user() etc. Let the
820                  * user-specified handler try to fix it first.
821                  */
822                 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
823                         return 1;
824
825                 /*
826                  * In case the user-specified fault handler returned
827                  * zero, try to fix up.
828                  */
829                 fixup = search_exception_tables(regs->ip);
830                 if (fixup) {
831                         regs->ip = fixup->fixup;
832                         return 1;
833                 }
834
835                 /*
836                  * fixup routine could not handle it,
837                  * Let do_page_fault() fix it.
838                  */
839                 break;
840         default:
841                 break;
842         }
843         return 0;
844 }
845
846 /*
847  * Wrapper routine for handling exceptions.
848  */
849 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
850                                        unsigned long val, void *data)
851 {
852         struct die_args *args = (struct die_args *)data;
853         int ret = NOTIFY_DONE;
854
855         if (args->regs && user_mode_vm(args->regs))
856                 return ret;
857
858         switch (val) {
859         case DIE_INT3:
860                 if (kprobe_handler(args->regs))
861                         ret = NOTIFY_STOP;
862                 break;
863         case DIE_DEBUG:
864                 if (post_kprobe_handler(args->regs))
865                         ret = NOTIFY_STOP;
866                 break;
867         case DIE_GPF:
868                 /* kprobe_running() needs smp_processor_id() */
869                 preempt_disable();
870                 if (kprobe_running() &&
871                     kprobe_fault_handler(args->regs, args->trapnr))
872                         ret = NOTIFY_STOP;
873                 preempt_enable();
874                 break;
875         default:
876                 break;
877         }
878         return ret;
879 }
880
881 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
882 {
883         struct jprobe *jp = container_of(p, struct jprobe, kp);
884         unsigned long addr;
885         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
886
887         kcb->jprobe_saved_regs = *regs;
888         kcb->jprobe_saved_sp = stack_addr(regs);
889         addr = (unsigned long)(kcb->jprobe_saved_sp);
890
891         /*
892          * As Linus pointed out, gcc assumes that the callee
893          * owns the argument space and could overwrite it, e.g.
894          * tailcall optimization. So, to be absolutely safe
895          * we also save and restore enough stack bytes to cover
896          * the argument area.
897          */
898         memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
899                         MIN_STACK_SIZE(addr));
900         regs->flags &= ~IF_MASK;
901         trace_hardirqs_off();
902         regs->ip = (unsigned long)(jp->entry);
903         return 1;
904 }
905
906 void __kprobes jprobe_return(void)
907 {
908         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
909
910         asm volatile ("       xchg   %%rbx,%%rsp     \n"
911                       "       int3                      \n"
912                       "       .globl jprobe_return_end  \n"
913                       "       jprobe_return_end:        \n"
914                       "       nop                       \n"::"b"
915                       (kcb->jprobe_saved_sp):"memory");
916 }
917
918 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
919 {
920         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
921         u8 *addr = (u8 *) (regs->ip - 1);
922         struct jprobe *jp = container_of(p, struct jprobe, kp);
923
924         if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
925                 if (stack_addr(regs) != kcb->jprobe_saved_sp) {
926                         struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
927                         printk("current sp %p does not match saved sp %p\n",
928                                stack_addr(regs), kcb->jprobe_saved_sp);
929                         printk("Saved registers for jprobe %p\n", jp);
930                         show_registers(saved_regs);
931                         printk("Current registers\n");
932                         show_registers(regs);
933                         BUG();
934                 }
935                 *regs = kcb->jprobe_saved_regs;
936                 memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp),
937                        kcb->jprobes_stack,
938                        MIN_STACK_SIZE(kcb->jprobe_saved_sp));
939                 preempt_enable_no_resched();
940                 return 1;
941         }
942         return 0;
943 }
944
945 int __init arch_init_kprobes(void)
946 {
947         return 0;
948 }
949
950 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
951 {
952         return 0;
953 }