kprobes: Checks probe address is instruction boudary on x86
[pandora-kernel.git] / arch / x86 / kernel / kprobes.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 <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
26  *              <prasanna@in.ibm.com> adapted for x86_64 from i386.
27  * 2005-Mar     Roland McGrath <roland@redhat.com>
28  *              Fixed to handle %rip-relative addressing mode correctly.
29  * 2005-May     Hien Nguyen <hien@us.ibm.com>, Jim Keniston
30  *              <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
31  *              <prasanna@in.ibm.com> added function-return probes.
32  * 2005-May     Rusty Lynch <rusty.lynch@intel.com>
33  *              Added function return probes functionality
34  * 2006-Feb     Masami Hiramatsu <hiramatu@sdl.hitachi.co.jp> added
35  *              kprobe-booster and kretprobe-booster for i386.
36  * 2007-Dec     Masami Hiramatsu <mhiramat@redhat.com> added kprobe-booster
37  *              and kretprobe-booster for x86-64
38  * 2007-Dec     Masami Hiramatsu <mhiramat@redhat.com>, Arjan van de Ven
39  *              <arjan@infradead.org> and Jim Keniston <jkenisto@us.ibm.com>
40  *              unified x86 kprobes code.
41  */
42
43 #include <linux/kprobes.h>
44 #include <linux/ptrace.h>
45 #include <linux/string.h>
46 #include <linux/slab.h>
47 #include <linux/hardirq.h>
48 #include <linux/preempt.h>
49 #include <linux/module.h>
50 #include <linux/kdebug.h>
51 #include <linux/kallsyms.h>
52
53 #include <asm/cacheflush.h>
54 #include <asm/desc.h>
55 #include <asm/pgtable.h>
56 #include <asm/uaccess.h>
57 #include <asm/alternative.h>
58 #include <asm/insn.h>
59
60 void jprobe_return_end(void);
61
62 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
63 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
64
65 #ifdef CONFIG_X86_64
66 #define stack_addr(regs) ((unsigned long *)regs->sp)
67 #else
68 /*
69  * "&regs->sp" looks wrong, but it's correct for x86_32.  x86_32 CPUs
70  * don't save the ss and esp registers if the CPU is already in kernel
71  * mode when it traps.  So for kprobes, regs->sp and regs->ss are not
72  * the [nonexistent] saved stack pointer and ss register, but rather
73  * the top 8 bytes of the pre-int3 stack.  So &regs->sp happens to
74  * point to the top of the pre-int3 stack.
75  */
76 #define stack_addr(regs) ((unsigned long *)&regs->sp)
77 #endif
78
79 #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
80         (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) |   \
81           (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) |   \
82           (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) |   \
83           (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf))    \
84          << (row % 32))
85         /*
86          * Undefined/reserved opcodes, conditional jump, Opcode Extension
87          * Groups, and some special opcodes can not boost.
88          */
89 static const u32 twobyte_is_boostable[256 / 32] = {
90         /*      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f          */
91         /*      ----------------------------------------------          */
92         W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
93         W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 10 */
94         W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
95         W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
96         W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
97         W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
98         W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
99         W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
100         W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
101         W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
102         W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
103         W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
104         W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
105         W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
106         W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
107         W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0)   /* f0 */
108         /*      -----------------------------------------------         */
109         /*      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f          */
110 };
111 static const u32 onebyte_has_modrm[256 / 32] = {
112         /*      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f          */
113         /*      -----------------------------------------------         */
114         W(0x00, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 00 */
115         W(0x10, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) , /* 10 */
116         W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 20 */
117         W(0x30, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) , /* 30 */
118         W(0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 40 */
119         W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
120         W(0x60, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0) | /* 60 */
121         W(0x70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 70 */
122         W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
123         W(0x90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 90 */
124         W(0xa0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* a0 */
125         W(0xb0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* b0 */
126         W(0xc0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0) | /* c0 */
127         W(0xd0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
128         W(0xe0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* e0 */
129         W(0xf0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1)   /* f0 */
130         /*      -----------------------------------------------         */
131         /*      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f          */
132 };
133 static const u32 twobyte_has_modrm[256 / 32] = {
134         /*      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f          */
135         /*      -----------------------------------------------         */
136         W(0x00, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1) | /* 0f */
137         W(0x10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0) , /* 1f */
138         W(0x20, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* 2f */
139         W(0x30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 3f */
140         W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 4f */
141         W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 5f */
142         W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 6f */
143         W(0x70, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1) , /* 7f */
144         W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 8f */
145         W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 9f */
146         W(0xa0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) | /* af */
147         W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1) , /* bf */
148         W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0) | /* cf */
149         W(0xd0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* df */
150         W(0xe0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* ef */
151         W(0xf0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0)   /* ff */
152         /*      -----------------------------------------------         */
153         /*      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f          */
154 };
155 #undef W
156
157 struct kretprobe_blackpoint kretprobe_blacklist[] = {
158         {"__switch_to", }, /* This function switches only current task, but
159                               doesn't switch kernel stack.*/
160         {NULL, NULL}    /* Terminator */
161 };
162 const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
163
164 /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
165 static void __kprobes set_jmp_op(void *from, void *to)
166 {
167         struct __arch_jmp_op {
168                 char op;
169                 s32 raddr;
170         } __attribute__((packed)) * jop;
171         jop = (struct __arch_jmp_op *)from;
172         jop->raddr = (s32)((long)(to) - ((long)(from) + 5));
173         jop->op = RELATIVEJUMP_INSTRUCTION;
174 }
175
176 /*
177  * Check for the REX prefix which can only exist on X86_64
178  * X86_32 always returns 0
179  */
180 static int __kprobes is_REX_prefix(kprobe_opcode_t *insn)
181 {
182 #ifdef CONFIG_X86_64
183         if ((*insn & 0xf0) == 0x40)
184                 return 1;
185 #endif
186         return 0;
187 }
188
189 /*
190  * Returns non-zero if opcode is boostable.
191  * RIP relative instructions are adjusted at copying time in 64 bits mode
192  */
193 static int __kprobes can_boost(kprobe_opcode_t *opcodes)
194 {
195         kprobe_opcode_t opcode;
196         kprobe_opcode_t *orig_opcodes = opcodes;
197
198         if (search_exception_tables((unsigned long)opcodes))
199                 return 0;       /* Page fault may occur on this address. */
200
201 retry:
202         if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
203                 return 0;
204         opcode = *(opcodes++);
205
206         /* 2nd-byte opcode */
207         if (opcode == 0x0f) {
208                 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
209                         return 0;
210                 return test_bit(*opcodes,
211                                 (unsigned long *)twobyte_is_boostable);
212         }
213
214         switch (opcode & 0xf0) {
215 #ifdef CONFIG_X86_64
216         case 0x40:
217                 goto retry; /* REX prefix is boostable */
218 #endif
219         case 0x60:
220                 if (0x63 < opcode && opcode < 0x67)
221                         goto retry; /* prefixes */
222                 /* can't boost Address-size override and bound */
223                 return (opcode != 0x62 && opcode != 0x67);
224         case 0x70:
225                 return 0; /* can't boost conditional jump */
226         case 0xc0:
227                 /* can't boost software-interruptions */
228                 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
229         case 0xd0:
230                 /* can boost AA* and XLAT */
231                 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
232         case 0xe0:
233                 /* can boost in/out and absolute jmps */
234                 return ((opcode & 0x04) || opcode == 0xea);
235         case 0xf0:
236                 if ((opcode & 0x0c) == 0 && opcode != 0xf1)
237                         goto retry; /* lock/rep(ne) prefix */
238                 /* clear and set flags are boostable */
239                 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
240         default:
241                 /* segment override prefixes are boostable */
242                 if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
243                         goto retry; /* prefixes */
244                 /* CS override prefix and call are not boostable */
245                 return (opcode != 0x2e && opcode != 0x9a);
246         }
247 }
248
249 /* Recover the probed instruction at addr for further analysis. */
250 static int recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)
251 {
252         struct kprobe *kp;
253         kp = get_kprobe((void *)addr);
254         if (!kp)
255                 return -EINVAL;
256
257         /*
258          *  Basically, kp->ainsn.insn has an original instruction.
259          *  However, RIP-relative instruction can not do single-stepping
260          *  at different place, fix_riprel() tweaks the displacement of
261          *  that instruction. In that case, we can't recover the instruction
262          *  from the kp->ainsn.insn.
263          *
264          *  On the other hand, kp->opcode has a copy of the first byte of
265          *  the probed instruction, which is overwritten by int3. And
266          *  the instruction at kp->addr is not modified by kprobes except
267          *  for the first byte, we can recover the original instruction
268          *  from it and kp->opcode.
269          */
270         memcpy(buf, kp->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
271         buf[0] = kp->opcode;
272         return 0;
273 }
274
275 /* Dummy buffers for kallsyms_lookup */
276 static char __dummy_buf[KSYM_NAME_LEN];
277
278 /* Check if paddr is at an instruction boundary */
279 static int __kprobes can_probe(unsigned long paddr)
280 {
281         int ret;
282         unsigned long addr, offset = 0;
283         struct insn insn;
284         kprobe_opcode_t buf[MAX_INSN_SIZE];
285
286         if (!kallsyms_lookup(paddr, NULL, &offset, NULL, __dummy_buf))
287                 return 0;
288
289         /* Decode instructions */
290         addr = paddr - offset;
291         while (addr < paddr) {
292                 kernel_insn_init(&insn, (void *)addr);
293                 insn_get_opcode(&insn);
294
295                 /*
296                  * Check if the instruction has been modified by another
297                  * kprobe, in which case we replace the breakpoint by the
298                  * original instruction in our buffer.
299                  */
300                 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
301                         ret = recover_probed_instruction(buf, addr);
302                         if (ret)
303                                 /*
304                                  * Another debugging subsystem might insert
305                                  * this breakpoint. In that case, we can't
306                                  * recover it.
307                                  */
308                                 return 0;
309                         kernel_insn_init(&insn, buf);
310                 }
311                 insn_get_length(&insn);
312                 addr += insn.length;
313         }
314
315         return (addr == paddr);
316 }
317
318 /*
319  * Returns non-zero if opcode modifies the interrupt flag.
320  */
321 static int __kprobes is_IF_modifier(kprobe_opcode_t *insn)
322 {
323         switch (*insn) {
324         case 0xfa:              /* cli */
325         case 0xfb:              /* sti */
326         case 0xcf:              /* iret/iretd */
327         case 0x9d:              /* popf/popfd */
328                 return 1;
329         }
330
331         /*
332          * on X86_64, 0x40-0x4f are REX prefixes so we need to look
333          * at the next byte instead.. but of course not recurse infinitely
334          */
335         if (is_REX_prefix(insn))
336                 return is_IF_modifier(++insn);
337
338         return 0;
339 }
340
341 /*
342  * Adjust the displacement if the instruction uses the %rip-relative
343  * addressing mode.
344  * If it does, Return the address of the 32-bit displacement word.
345  * If not, return null.
346  * Only applicable to 64-bit x86.
347  */
348 static void __kprobes fix_riprel(struct kprobe *p)
349 {
350 #ifdef CONFIG_X86_64
351         u8 *insn = p->ainsn.insn;
352         s64 disp;
353         int need_modrm;
354
355         /* Skip legacy instruction prefixes.  */
356         while (1) {
357                 switch (*insn) {
358                 case 0x66:
359                 case 0x67:
360                 case 0x2e:
361                 case 0x3e:
362                 case 0x26:
363                 case 0x64:
364                 case 0x65:
365                 case 0x36:
366                 case 0xf0:
367                 case 0xf3:
368                 case 0xf2:
369                         ++insn;
370                         continue;
371                 }
372                 break;
373         }
374
375         /* Skip REX instruction prefix.  */
376         if (is_REX_prefix(insn))
377                 ++insn;
378
379         if (*insn == 0x0f) {
380                 /* Two-byte opcode.  */
381                 ++insn;
382                 need_modrm = test_bit(*insn,
383                                       (unsigned long *)twobyte_has_modrm);
384         } else
385                 /* One-byte opcode.  */
386                 need_modrm = test_bit(*insn,
387                                       (unsigned long *)onebyte_has_modrm);
388
389         if (need_modrm) {
390                 u8 modrm = *++insn;
391                 if ((modrm & 0xc7) == 0x05) {
392                         /* %rip+disp32 addressing mode */
393                         /* Displacement follows ModRM byte.  */
394                         ++insn;
395                         /*
396                          * The copied instruction uses the %rip-relative
397                          * addressing mode.  Adjust the displacement for the
398                          * difference between the original location of this
399                          * instruction and the location of the copy that will
400                          * actually be run.  The tricky bit here is making sure
401                          * that the sign extension happens correctly in this
402                          * calculation, since we need a signed 32-bit result to
403                          * be sign-extended to 64 bits when it's added to the
404                          * %rip value and yield the same 64-bit result that the
405                          * sign-extension of the original signed 32-bit
406                          * displacement would have given.
407                          */
408                         disp = (u8 *) p->addr + *((s32 *) insn) -
409                                (u8 *) p->ainsn.insn;
410                         BUG_ON((s64) (s32) disp != disp); /* Sanity check.  */
411                         *(s32 *)insn = (s32) disp;
412                 }
413         }
414 #endif
415 }
416
417 static void __kprobes arch_copy_kprobe(struct kprobe *p)
418 {
419         memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
420
421         fix_riprel(p);
422
423         if (can_boost(p->addr))
424                 p->ainsn.boostable = 0;
425         else
426                 p->ainsn.boostable = -1;
427
428         p->opcode = *p->addr;
429 }
430
431 int __kprobes arch_prepare_kprobe(struct kprobe *p)
432 {
433         if (!can_probe((unsigned long)p->addr))
434                 return -EILSEQ;
435         /* insn: must be on special executable page on x86. */
436         p->ainsn.insn = get_insn_slot();
437         if (!p->ainsn.insn)
438                 return -ENOMEM;
439         arch_copy_kprobe(p);
440         return 0;
441 }
442
443 void __kprobes arch_arm_kprobe(struct kprobe *p)
444 {
445         text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
446 }
447
448 void __kprobes arch_disarm_kprobe(struct kprobe *p)
449 {
450         text_poke(p->addr, &p->opcode, 1);
451 }
452
453 void __kprobes arch_remove_kprobe(struct kprobe *p)
454 {
455         if (p->ainsn.insn) {
456                 free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
457                 p->ainsn.insn = NULL;
458         }
459 }
460
461 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
462 {
463         kcb->prev_kprobe.kp = kprobe_running();
464         kcb->prev_kprobe.status = kcb->kprobe_status;
465         kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
466         kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
467 }
468
469 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
470 {
471         __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
472         kcb->kprobe_status = kcb->prev_kprobe.status;
473         kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
474         kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
475 }
476
477 static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
478                                 struct kprobe_ctlblk *kcb)
479 {
480         __get_cpu_var(current_kprobe) = p;
481         kcb->kprobe_saved_flags = kcb->kprobe_old_flags
482                 = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
483         if (is_IF_modifier(p->ainsn.insn))
484                 kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
485 }
486
487 static void __kprobes clear_btf(void)
488 {
489         if (test_thread_flag(TIF_DEBUGCTLMSR))
490                 update_debugctlmsr(0);
491 }
492
493 static void __kprobes restore_btf(void)
494 {
495         if (test_thread_flag(TIF_DEBUGCTLMSR))
496                 update_debugctlmsr(current->thread.debugctlmsr);
497 }
498
499 static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
500 {
501         clear_btf();
502         regs->flags |= X86_EFLAGS_TF;
503         regs->flags &= ~X86_EFLAGS_IF;
504         /* single step inline if the instruction is an int3 */
505         if (p->opcode == BREAKPOINT_INSTRUCTION)
506                 regs->ip = (unsigned long)p->addr;
507         else
508                 regs->ip = (unsigned long)p->ainsn.insn;
509 }
510
511 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
512                                       struct pt_regs *regs)
513 {
514         unsigned long *sara = stack_addr(regs);
515
516         ri->ret_addr = (kprobe_opcode_t *) *sara;
517
518         /* Replace the return addr with trampoline addr */
519         *sara = (unsigned long) &kretprobe_trampoline;
520 }
521
522 static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs,
523                                        struct kprobe_ctlblk *kcb)
524 {
525 #if !defined(CONFIG_PREEMPT) || defined(CONFIG_FREEZER)
526         if (p->ainsn.boostable == 1 && !p->post_handler) {
527                 /* Boost up -- we can execute copied instructions directly */
528                 reset_current_kprobe();
529                 regs->ip = (unsigned long)p->ainsn.insn;
530                 preempt_enable_no_resched();
531                 return;
532         }
533 #endif
534         prepare_singlestep(p, regs);
535         kcb->kprobe_status = KPROBE_HIT_SS;
536 }
537
538 /*
539  * We have reentered the kprobe_handler(), since another probe was hit while
540  * within the handler. We save the original kprobes variables and just single
541  * step on the instruction of the new probe without calling any user handlers.
542  */
543 static int __kprobes reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
544                                     struct kprobe_ctlblk *kcb)
545 {
546         switch (kcb->kprobe_status) {
547         case KPROBE_HIT_SSDONE:
548 #ifdef CONFIG_X86_64
549                 /* TODO: Provide re-entrancy from post_kprobes_handler() and
550                  * avoid exception stack corruption while single-stepping on
551                  * the instruction of the new probe.
552                  */
553                 arch_disarm_kprobe(p);
554                 regs->ip = (unsigned long)p->addr;
555                 reset_current_kprobe();
556                 preempt_enable_no_resched();
557                 break;
558 #endif
559         case KPROBE_HIT_ACTIVE:
560                 save_previous_kprobe(kcb);
561                 set_current_kprobe(p, regs, kcb);
562                 kprobes_inc_nmissed_count(p);
563                 prepare_singlestep(p, regs);
564                 kcb->kprobe_status = KPROBE_REENTER;
565                 break;
566         case KPROBE_HIT_SS:
567                 if (p == kprobe_running()) {
568                         regs->flags &= ~X86_EFLAGS_TF;
569                         regs->flags |= kcb->kprobe_saved_flags;
570                         return 0;
571                 } else {
572                         /* A probe has been hit in the codepath leading up
573                          * to, or just after, single-stepping of a probed
574                          * instruction. This entire codepath should strictly
575                          * reside in .kprobes.text section. Raise a warning
576                          * to highlight this peculiar case.
577                          */
578                 }
579         default:
580                 /* impossible cases */
581                 WARN_ON(1);
582                 return 0;
583         }
584
585         return 1;
586 }
587
588 /*
589  * Interrupts are disabled on entry as trap3 is an interrupt gate and they
590  * remain disabled thorough out this function.
591  */
592 static int __kprobes kprobe_handler(struct pt_regs *regs)
593 {
594         kprobe_opcode_t *addr;
595         struct kprobe *p;
596         struct kprobe_ctlblk *kcb;
597
598         addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
599         if (*addr != BREAKPOINT_INSTRUCTION) {
600                 /*
601                  * The breakpoint instruction was removed right
602                  * after we hit it.  Another cpu has removed
603                  * either a probepoint or a debugger breakpoint
604                  * at this address.  In either case, no further
605                  * handling of this interrupt is appropriate.
606                  * Back up over the (now missing) int3 and run
607                  * the original instruction.
608                  */
609                 regs->ip = (unsigned long)addr;
610                 return 1;
611         }
612
613         /*
614          * We don't want to be preempted for the entire
615          * duration of kprobe processing. We conditionally
616          * re-enable preemption at the end of this function,
617          * and also in reenter_kprobe() and setup_singlestep().
618          */
619         preempt_disable();
620
621         kcb = get_kprobe_ctlblk();
622         p = get_kprobe(addr);
623
624         if (p) {
625                 if (kprobe_running()) {
626                         if (reenter_kprobe(p, regs, kcb))
627                                 return 1;
628                 } else {
629                         set_current_kprobe(p, regs, kcb);
630                         kcb->kprobe_status = KPROBE_HIT_ACTIVE;
631
632                         /*
633                          * If we have no pre-handler or it returned 0, we
634                          * continue with normal processing.  If we have a
635                          * pre-handler and it returned non-zero, it prepped
636                          * for calling the break_handler below on re-entry
637                          * for jprobe processing, so get out doing nothing
638                          * more here.
639                          */
640                         if (!p->pre_handler || !p->pre_handler(p, regs))
641                                 setup_singlestep(p, regs, kcb);
642                         return 1;
643                 }
644         } else if (kprobe_running()) {
645                 p = __get_cpu_var(current_kprobe);
646                 if (p->break_handler && p->break_handler(p, regs)) {
647                         setup_singlestep(p, regs, kcb);
648                         return 1;
649                 }
650         } /* else: not a kprobe fault; let the kernel handle it */
651
652         preempt_enable_no_resched();
653         return 0;
654 }
655
656 /*
657  * When a retprobed function returns, this code saves registers and
658  * calls trampoline_handler() runs, which calls the kretprobe's handler.
659  */
660 static void __used __kprobes kretprobe_trampoline_holder(void)
661 {
662         asm volatile (
663                         ".global kretprobe_trampoline\n"
664                         "kretprobe_trampoline: \n"
665 #ifdef CONFIG_X86_64
666                         /* We don't bother saving the ss register */
667                         "       pushq %rsp\n"
668                         "       pushfq\n"
669                         /*
670                          * Skip cs, ip, orig_ax.
671                          * trampoline_handler() will plug in these values
672                          */
673                         "       subq $24, %rsp\n"
674                         "       pushq %rdi\n"
675                         "       pushq %rsi\n"
676                         "       pushq %rdx\n"
677                         "       pushq %rcx\n"
678                         "       pushq %rax\n"
679                         "       pushq %r8\n"
680                         "       pushq %r9\n"
681                         "       pushq %r10\n"
682                         "       pushq %r11\n"
683                         "       pushq %rbx\n"
684                         "       pushq %rbp\n"
685                         "       pushq %r12\n"
686                         "       pushq %r13\n"
687                         "       pushq %r14\n"
688                         "       pushq %r15\n"
689                         "       movq %rsp, %rdi\n"
690                         "       call trampoline_handler\n"
691                         /* Replace saved sp with true return address. */
692                         "       movq %rax, 152(%rsp)\n"
693                         "       popq %r15\n"
694                         "       popq %r14\n"
695                         "       popq %r13\n"
696                         "       popq %r12\n"
697                         "       popq %rbp\n"
698                         "       popq %rbx\n"
699                         "       popq %r11\n"
700                         "       popq %r10\n"
701                         "       popq %r9\n"
702                         "       popq %r8\n"
703                         "       popq %rax\n"
704                         "       popq %rcx\n"
705                         "       popq %rdx\n"
706                         "       popq %rsi\n"
707                         "       popq %rdi\n"
708                         /* Skip orig_ax, ip, cs */
709                         "       addq $24, %rsp\n"
710                         "       popfq\n"
711 #else
712                         "       pushf\n"
713                         /*
714                          * Skip cs, ip, orig_ax and gs.
715                          * trampoline_handler() will plug in these values
716                          */
717                         "       subl $16, %esp\n"
718                         "       pushl %fs\n"
719                         "       pushl %es\n"
720                         "       pushl %ds\n"
721                         "       pushl %eax\n"
722                         "       pushl %ebp\n"
723                         "       pushl %edi\n"
724                         "       pushl %esi\n"
725                         "       pushl %edx\n"
726                         "       pushl %ecx\n"
727                         "       pushl %ebx\n"
728                         "       movl %esp, %eax\n"
729                         "       call trampoline_handler\n"
730                         /* Move flags to cs */
731                         "       movl 56(%esp), %edx\n"
732                         "       movl %edx, 52(%esp)\n"
733                         /* Replace saved flags with true return address. */
734                         "       movl %eax, 56(%esp)\n"
735                         "       popl %ebx\n"
736                         "       popl %ecx\n"
737                         "       popl %edx\n"
738                         "       popl %esi\n"
739                         "       popl %edi\n"
740                         "       popl %ebp\n"
741                         "       popl %eax\n"
742                         /* Skip ds, es, fs, gs, orig_ax and ip */
743                         "       addl $24, %esp\n"
744                         "       popf\n"
745 #endif
746                         "       ret\n");
747 }
748
749 /*
750  * Called from kretprobe_trampoline
751  */
752 static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
753 {
754         struct kretprobe_instance *ri = NULL;
755         struct hlist_head *head, empty_rp;
756         struct hlist_node *node, *tmp;
757         unsigned long flags, orig_ret_address = 0;
758         unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
759
760         INIT_HLIST_HEAD(&empty_rp);
761         kretprobe_hash_lock(current, &head, &flags);
762         /* fixup registers */
763 #ifdef CONFIG_X86_64
764         regs->cs = __KERNEL_CS;
765 #else
766         regs->cs = __KERNEL_CS | get_kernel_rpl();
767         regs->gs = 0;
768 #endif
769         regs->ip = trampoline_address;
770         regs->orig_ax = ~0UL;
771
772         /*
773          * It is possible to have multiple instances associated with a given
774          * task either because multiple functions in the call path have
775          * return probes installed on them, and/or more than one
776          * return probe was registered for a target function.
777          *
778          * We can handle this because:
779          *     - instances are always pushed into the head of the list
780          *     - when multiple return probes are registered for the same
781          *       function, the (chronologically) first instance's ret_addr
782          *       will be the real return address, and all the rest will
783          *       point to kretprobe_trampoline.
784          */
785         hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
786                 if (ri->task != current)
787                         /* another task is sharing our hash bucket */
788                         continue;
789
790                 if (ri->rp && ri->rp->handler) {
791                         __get_cpu_var(current_kprobe) = &ri->rp->kp;
792                         get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
793                         ri->rp->handler(ri, regs);
794                         __get_cpu_var(current_kprobe) = NULL;
795                 }
796
797                 orig_ret_address = (unsigned long)ri->ret_addr;
798                 recycle_rp_inst(ri, &empty_rp);
799
800                 if (orig_ret_address != trampoline_address)
801                         /*
802                          * This is the real return address. Any other
803                          * instances associated with this task are for
804                          * other calls deeper on the call stack
805                          */
806                         break;
807         }
808
809         kretprobe_assert(ri, orig_ret_address, trampoline_address);
810
811         kretprobe_hash_unlock(current, &flags);
812
813         hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
814                 hlist_del(&ri->hlist);
815                 kfree(ri);
816         }
817         return (void *)orig_ret_address;
818 }
819
820 /*
821  * Called after single-stepping.  p->addr is the address of the
822  * instruction whose first byte has been replaced by the "int 3"
823  * instruction.  To avoid the SMP problems that can occur when we
824  * temporarily put back the original opcode to single-step, we
825  * single-stepped a copy of the instruction.  The address of this
826  * copy is p->ainsn.insn.
827  *
828  * This function prepares to return from the post-single-step
829  * interrupt.  We have to fix up the stack as follows:
830  *
831  * 0) Except in the case of absolute or indirect jump or call instructions,
832  * the new ip is relative to the copied instruction.  We need to make
833  * it relative to the original instruction.
834  *
835  * 1) If the single-stepped instruction was pushfl, then the TF and IF
836  * flags are set in the just-pushed flags, and may need to be cleared.
837  *
838  * 2) If the single-stepped instruction was a call, the return address
839  * that is atop the stack is the address following the copied instruction.
840  * We need to make it the address following the original instruction.
841  *
842  * If this is the first time we've single-stepped the instruction at
843  * this probepoint, and the instruction is boostable, boost it: add a
844  * jump instruction after the copied instruction, that jumps to the next
845  * instruction after the probepoint.
846  */
847 static void __kprobes resume_execution(struct kprobe *p,
848                 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
849 {
850         unsigned long *tos = stack_addr(regs);
851         unsigned long copy_ip = (unsigned long)p->ainsn.insn;
852         unsigned long orig_ip = (unsigned long)p->addr;
853         kprobe_opcode_t *insn = p->ainsn.insn;
854
855         /*skip the REX prefix*/
856         if (is_REX_prefix(insn))
857                 insn++;
858
859         regs->flags &= ~X86_EFLAGS_TF;
860         switch (*insn) {
861         case 0x9c:      /* pushfl */
862                 *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
863                 *tos |= kcb->kprobe_old_flags;
864                 break;
865         case 0xc2:      /* iret/ret/lret */
866         case 0xc3:
867         case 0xca:
868         case 0xcb:
869         case 0xcf:
870         case 0xea:      /* jmp absolute -- ip is correct */
871                 /* ip is already adjusted, no more changes required */
872                 p->ainsn.boostable = 1;
873                 goto no_change;
874         case 0xe8:      /* call relative - Fix return addr */
875                 *tos = orig_ip + (*tos - copy_ip);
876                 break;
877 #ifdef CONFIG_X86_32
878         case 0x9a:      /* call absolute -- same as call absolute, indirect */
879                 *tos = orig_ip + (*tos - copy_ip);
880                 goto no_change;
881 #endif
882         case 0xff:
883                 if ((insn[1] & 0x30) == 0x10) {
884                         /*
885                          * call absolute, indirect
886                          * Fix return addr; ip is correct.
887                          * But this is not boostable
888                          */
889                         *tos = orig_ip + (*tos - copy_ip);
890                         goto no_change;
891                 } else if (((insn[1] & 0x31) == 0x20) ||
892                            ((insn[1] & 0x31) == 0x21)) {
893                         /*
894                          * jmp near and far, absolute indirect
895                          * ip is correct. And this is boostable
896                          */
897                         p->ainsn.boostable = 1;
898                         goto no_change;
899                 }
900         default:
901                 break;
902         }
903
904         if (p->ainsn.boostable == 0) {
905                 if ((regs->ip > copy_ip) &&
906                     (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
907                         /*
908                          * These instructions can be executed directly if it
909                          * jumps back to correct address.
910                          */
911                         set_jmp_op((void *)regs->ip,
912                                    (void *)orig_ip + (regs->ip - copy_ip));
913                         p->ainsn.boostable = 1;
914                 } else {
915                         p->ainsn.boostable = -1;
916                 }
917         }
918
919         regs->ip += orig_ip - copy_ip;
920
921 no_change:
922         restore_btf();
923 }
924
925 /*
926  * Interrupts are disabled on entry as trap1 is an interrupt gate and they
927  * remain disabled thoroughout this function.
928  */
929 static int __kprobes post_kprobe_handler(struct pt_regs *regs)
930 {
931         struct kprobe *cur = kprobe_running();
932         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
933
934         if (!cur)
935                 return 0;
936
937         resume_execution(cur, regs, kcb);
938         regs->flags |= kcb->kprobe_saved_flags;
939
940         if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
941                 kcb->kprobe_status = KPROBE_HIT_SSDONE;
942                 cur->post_handler(cur, regs, 0);
943         }
944
945         /* Restore back the original saved kprobes variables and continue. */
946         if (kcb->kprobe_status == KPROBE_REENTER) {
947                 restore_previous_kprobe(kcb);
948                 goto out;
949         }
950         reset_current_kprobe();
951 out:
952         preempt_enable_no_resched();
953
954         /*
955          * if somebody else is singlestepping across a probe point, flags
956          * will have TF set, in which case, continue the remaining processing
957          * of do_debug, as if this is not a probe hit.
958          */
959         if (regs->flags & X86_EFLAGS_TF)
960                 return 0;
961
962         return 1;
963 }
964
965 int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
966 {
967         struct kprobe *cur = kprobe_running();
968         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
969
970         switch (kcb->kprobe_status) {
971         case KPROBE_HIT_SS:
972         case KPROBE_REENTER:
973                 /*
974                  * We are here because the instruction being single
975                  * stepped caused a page fault. We reset the current
976                  * kprobe and the ip points back to the probe address
977                  * and allow the page fault handler to continue as a
978                  * normal page fault.
979                  */
980                 regs->ip = (unsigned long)cur->addr;
981                 regs->flags |= kcb->kprobe_old_flags;
982                 if (kcb->kprobe_status == KPROBE_REENTER)
983                         restore_previous_kprobe(kcb);
984                 else
985                         reset_current_kprobe();
986                 preempt_enable_no_resched();
987                 break;
988         case KPROBE_HIT_ACTIVE:
989         case KPROBE_HIT_SSDONE:
990                 /*
991                  * We increment the nmissed count for accounting,
992                  * we can also use npre/npostfault count for accounting
993                  * these specific fault cases.
994                  */
995                 kprobes_inc_nmissed_count(cur);
996
997                 /*
998                  * We come here because instructions in the pre/post
999                  * handler caused the page_fault, this could happen
1000                  * if handler tries to access user space by
1001                  * copy_from_user(), get_user() etc. Let the
1002                  * user-specified handler try to fix it first.
1003                  */
1004                 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
1005                         return 1;
1006
1007                 /*
1008                  * In case the user-specified fault handler returned
1009                  * zero, try to fix up.
1010                  */
1011                 if (fixup_exception(regs))
1012                         return 1;
1013
1014                 /*
1015                  * fixup routine could not handle it,
1016                  * Let do_page_fault() fix it.
1017                  */
1018                 break;
1019         default:
1020                 break;
1021         }
1022         return 0;
1023 }
1024
1025 /*
1026  * Wrapper routine for handling exceptions.
1027  */
1028 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
1029                                        unsigned long val, void *data)
1030 {
1031         struct die_args *args = data;
1032         int ret = NOTIFY_DONE;
1033
1034         if (args->regs && user_mode_vm(args->regs))
1035                 return ret;
1036
1037         switch (val) {
1038         case DIE_INT3:
1039                 if (kprobe_handler(args->regs))
1040                         ret = NOTIFY_STOP;
1041                 break;
1042         case DIE_DEBUG:
1043                 if (post_kprobe_handler(args->regs))
1044                         ret = NOTIFY_STOP;
1045                 break;
1046         case DIE_GPF:
1047                 /*
1048                  * To be potentially processing a kprobe fault and to
1049                  * trust the result from kprobe_running(), we have
1050                  * be non-preemptible.
1051                  */
1052                 if (!preemptible() && kprobe_running() &&
1053                     kprobe_fault_handler(args->regs, args->trapnr))
1054                         ret = NOTIFY_STOP;
1055                 break;
1056         default:
1057                 break;
1058         }
1059         return ret;
1060 }
1061
1062 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
1063 {
1064         struct jprobe *jp = container_of(p, struct jprobe, kp);
1065         unsigned long addr;
1066         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1067
1068         kcb->jprobe_saved_regs = *regs;
1069         kcb->jprobe_saved_sp = stack_addr(regs);
1070         addr = (unsigned long)(kcb->jprobe_saved_sp);
1071
1072         /*
1073          * As Linus pointed out, gcc assumes that the callee
1074          * owns the argument space and could overwrite it, e.g.
1075          * tailcall optimization. So, to be absolutely safe
1076          * we also save and restore enough stack bytes to cover
1077          * the argument area.
1078          */
1079         memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
1080                MIN_STACK_SIZE(addr));
1081         regs->flags &= ~X86_EFLAGS_IF;
1082         trace_hardirqs_off();
1083         regs->ip = (unsigned long)(jp->entry);
1084         return 1;
1085 }
1086
1087 void __kprobes jprobe_return(void)
1088 {
1089         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1090
1091         asm volatile (
1092 #ifdef CONFIG_X86_64
1093                         "       xchg   %%rbx,%%rsp      \n"
1094 #else
1095                         "       xchgl   %%ebx,%%esp     \n"
1096 #endif
1097                         "       int3                    \n"
1098                         "       .globl jprobe_return_end\n"
1099                         "       jprobe_return_end:      \n"
1100                         "       nop                     \n"::"b"
1101                         (kcb->jprobe_saved_sp):"memory");
1102 }
1103
1104 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
1105 {
1106         struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1107         u8 *addr = (u8 *) (regs->ip - 1);
1108         struct jprobe *jp = container_of(p, struct jprobe, kp);
1109
1110         if ((addr > (u8 *) jprobe_return) &&
1111             (addr < (u8 *) jprobe_return_end)) {
1112                 if (stack_addr(regs) != kcb->jprobe_saved_sp) {
1113                         struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
1114                         printk(KERN_ERR
1115                                "current sp %p does not match saved sp %p\n",
1116                                stack_addr(regs), kcb->jprobe_saved_sp);
1117                         printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
1118                         show_registers(saved_regs);
1119                         printk(KERN_ERR "Current registers\n");
1120                         show_registers(regs);
1121                         BUG();
1122                 }
1123                 *regs = kcb->jprobe_saved_regs;
1124                 memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp),
1125                        kcb->jprobes_stack,
1126                        MIN_STACK_SIZE(kcb->jprobe_saved_sp));
1127                 preempt_enable_no_resched();
1128                 return 1;
1129         }
1130         return 0;
1131 }
1132
1133 int __init arch_init_kprobes(void)
1134 {
1135         return 0;
1136 }
1137
1138 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
1139 {
1140         return 0;
1141 }