init from v2.6.32.60
[mach-moxart.git] / arch / x86 / kernel / kprobes.c
blob7a67820311fafc702cc747ff8b45492683173db0
1 /*
2 * Kernel Probes (KProbes)
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.
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.
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.
18 * Copyright (C) IBM Corporation, 2002, 2004
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.
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>
52 #include <asm/cacheflush.h>
53 #include <asm/desc.h>
54 #include <asm/pgtable.h>
55 #include <asm/uaccess.h>
56 #include <asm/alternative.h>
58 void jprobe_return_end(void);
60 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
61 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
63 #ifdef CONFIG_X86_64
64 #define stack_addr(regs) ((unsigned long *)regs->sp)
65 #else
67 * "&regs->sp" looks wrong, but it's correct for x86_32. x86_32 CPUs
68 * don't save the ss and esp registers if the CPU is already in kernel
69 * mode when it traps. So for kprobes, regs->sp and regs->ss are not
70 * the [nonexistent] saved stack pointer and ss register, but rather
71 * the top 8 bytes of the pre-int3 stack. So &regs->sp happens to
72 * point to the top of the pre-int3 stack.
74 #define stack_addr(regs) ((unsigned long *)&regs->sp)
75 #endif
77 #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
78 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
79 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
80 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
81 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
82 << (row % 32))
84 * Undefined/reserved opcodes, conditional jump, Opcode Extension
85 * Groups, and some special opcodes can not boost.
86 * This is non-const to keep gcc from statically optimizing it out, as
87 * variable_test_bit makes gcc think only *(unsigned long*) is used.
89 static 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 */
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 */
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 */
155 #undef W
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 */
162 const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
164 /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
165 static void __kprobes set_jmp_op(void *from, void *to)
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;
177 * Check for the REX prefix which can only exist on X86_64
178 * X86_32 always returns 0
180 static int __kprobes is_REX_prefix(kprobe_opcode_t *insn)
182 #ifdef CONFIG_X86_64
183 if ((*insn & 0xf0) == 0x40)
184 return 1;
185 #endif
186 return 0;
190 * Returns non-zero if opcode is boostable.
191 * RIP relative instructions are adjusted at copying time in 64 bits mode
193 static int __kprobes can_boost(kprobe_opcode_t *opcodes)
195 kprobe_opcode_t opcode;
196 kprobe_opcode_t *orig_opcodes = opcodes;
198 if (search_exception_tables((unsigned long)opcodes))
199 return 0; /* Page fault may occur on this address. */
201 retry:
202 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
203 return 0;
204 opcode = *(opcodes++);
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);
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);
250 * Returns non-zero if opcode modifies the interrupt flag.
252 static int __kprobes is_IF_modifier(kprobe_opcode_t *insn)
254 switch (*insn) {
255 case 0xfa: /* cli */
256 case 0xfb: /* sti */
257 case 0xcf: /* iret/iretd */
258 case 0x9d: /* popf/popfd */
259 return 1;
263 * on X86_64, 0x40-0x4f are REX prefixes so we need to look
264 * at the next byte instead.. but of course not recurse infinitely
266 if (is_REX_prefix(insn))
267 return is_IF_modifier(++insn);
269 return 0;
273 * Adjust the displacement if the instruction uses the %rip-relative
274 * addressing mode.
275 * If it does, Return the address of the 32-bit displacement word.
276 * If not, return null.
277 * Only applicable to 64-bit x86.
279 static void __kprobes fix_riprel(struct kprobe *p)
281 #ifdef CONFIG_X86_64
282 u8 *insn = p->ainsn.insn;
283 s64 disp;
284 int need_modrm;
286 /* Skip legacy instruction prefixes. */
287 while (1) {
288 switch (*insn) {
289 case 0x66:
290 case 0x67:
291 case 0x2e:
292 case 0x3e:
293 case 0x26:
294 case 0x64:
295 case 0x65:
296 case 0x36:
297 case 0xf0:
298 case 0xf3:
299 case 0xf2:
300 ++insn;
301 continue;
303 break;
306 /* Skip REX instruction prefix. */
307 if (is_REX_prefix(insn))
308 ++insn;
310 if (*insn == 0x0f) {
311 /* Two-byte opcode. */
312 ++insn;
313 need_modrm = test_bit(*insn,
314 (unsigned long *)twobyte_has_modrm);
315 } else
316 /* One-byte opcode. */
317 need_modrm = test_bit(*insn,
318 (unsigned long *)onebyte_has_modrm);
320 if (need_modrm) {
321 u8 modrm = *++insn;
322 if ((modrm & 0xc7) == 0x05) {
323 /* %rip+disp32 addressing mode */
324 /* Displacement follows ModRM byte. */
325 ++insn;
327 * The copied instruction uses the %rip-relative
328 * addressing mode. Adjust the displacement for the
329 * difference between the original location of this
330 * instruction and the location of the copy that will
331 * actually be run. The tricky bit here is making sure
332 * that the sign extension happens correctly in this
333 * calculation, since we need a signed 32-bit result to
334 * be sign-extended to 64 bits when it's added to the
335 * %rip value and yield the same 64-bit result that the
336 * sign-extension of the original signed 32-bit
337 * displacement would have given.
339 disp = (u8 *) p->addr + *((s32 *) insn) -
340 (u8 *) p->ainsn.insn;
341 BUG_ON((s64) (s32) disp != disp); /* Sanity check. */
342 *(s32 *)insn = (s32) disp;
345 #endif
348 static void __kprobes arch_copy_kprobe(struct kprobe *p)
350 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
352 fix_riprel(p);
354 if (can_boost(p->addr))
355 p->ainsn.boostable = 0;
356 else
357 p->ainsn.boostable = -1;
359 p->opcode = *p->addr;
362 int __kprobes arch_prepare_kprobe(struct kprobe *p)
364 /* insn: must be on special executable page on x86. */
365 p->ainsn.insn = get_insn_slot();
366 if (!p->ainsn.insn)
367 return -ENOMEM;
368 arch_copy_kprobe(p);
369 return 0;
372 void __kprobes arch_arm_kprobe(struct kprobe *p)
374 text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
377 void __kprobes arch_disarm_kprobe(struct kprobe *p)
379 text_poke(p->addr, &p->opcode, 1);
382 void __kprobes arch_remove_kprobe(struct kprobe *p)
384 if (p->ainsn.insn) {
385 free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
386 p->ainsn.insn = NULL;
390 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
392 kcb->prev_kprobe.kp = kprobe_running();
393 kcb->prev_kprobe.status = kcb->kprobe_status;
394 kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
395 kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
398 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
400 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
401 kcb->kprobe_status = kcb->prev_kprobe.status;
402 kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
403 kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
406 static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
407 struct kprobe_ctlblk *kcb)
409 __get_cpu_var(current_kprobe) = p;
410 kcb->kprobe_saved_flags = kcb->kprobe_old_flags
411 = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
412 if (is_IF_modifier(p->ainsn.insn))
413 kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
416 static void __kprobes clear_btf(void)
418 if (test_thread_flag(TIF_DEBUGCTLMSR))
419 update_debugctlmsr(0);
422 static void __kprobes restore_btf(void)
424 if (test_thread_flag(TIF_DEBUGCTLMSR))
425 update_debugctlmsr(current->thread.debugctlmsr);
428 static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
430 clear_btf();
431 regs->flags |= X86_EFLAGS_TF;
432 regs->flags &= ~X86_EFLAGS_IF;
433 /* single step inline if the instruction is an int3 */
434 if (p->opcode == BREAKPOINT_INSTRUCTION)
435 regs->ip = (unsigned long)p->addr;
436 else
437 regs->ip = (unsigned long)p->ainsn.insn;
440 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
441 struct pt_regs *regs)
443 unsigned long *sara = stack_addr(regs);
445 ri->ret_addr = (kprobe_opcode_t *) *sara;
447 /* Replace the return addr with trampoline addr */
448 *sara = (unsigned long) &kretprobe_trampoline;
451 static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs,
452 struct kprobe_ctlblk *kcb)
454 #if !defined(CONFIG_PREEMPT) || defined(CONFIG_FREEZER)
455 if (p->ainsn.boostable == 1 && !p->post_handler) {
456 /* Boost up -- we can execute copied instructions directly */
457 reset_current_kprobe();
458 regs->ip = (unsigned long)p->ainsn.insn;
459 preempt_enable_no_resched();
460 return;
462 #endif
463 prepare_singlestep(p, regs);
464 kcb->kprobe_status = KPROBE_HIT_SS;
468 * We have reentered the kprobe_handler(), since another probe was hit while
469 * within the handler. We save the original kprobes variables and just single
470 * step on the instruction of the new probe without calling any user handlers.
472 static int __kprobes reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
473 struct kprobe_ctlblk *kcb)
475 switch (kcb->kprobe_status) {
476 case KPROBE_HIT_SSDONE:
477 #ifdef CONFIG_X86_64
478 /* TODO: Provide re-entrancy from post_kprobes_handler() and
479 * avoid exception stack corruption while single-stepping on
480 * the instruction of the new probe.
482 arch_disarm_kprobe(p);
483 regs->ip = (unsigned long)p->addr;
484 reset_current_kprobe();
485 preempt_enable_no_resched();
486 break;
487 #endif
488 case KPROBE_HIT_ACTIVE:
489 save_previous_kprobe(kcb);
490 set_current_kprobe(p, regs, kcb);
491 kprobes_inc_nmissed_count(p);
492 prepare_singlestep(p, regs);
493 kcb->kprobe_status = KPROBE_REENTER;
494 break;
495 case KPROBE_HIT_SS:
496 if (p == kprobe_running()) {
497 regs->flags &= ~X86_EFLAGS_TF;
498 regs->flags |= kcb->kprobe_saved_flags;
499 return 0;
500 } else {
501 /* A probe has been hit in the codepath leading up
502 * to, or just after, single-stepping of a probed
503 * instruction. This entire codepath should strictly
504 * reside in .kprobes.text section. Raise a warning
505 * to highlight this peculiar case.
508 default:
509 /* impossible cases */
510 WARN_ON(1);
511 return 0;
514 return 1;
518 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
519 * remain disabled thorough out this function.
521 static int __kprobes kprobe_handler(struct pt_regs *regs)
523 kprobe_opcode_t *addr;
524 struct kprobe *p;
525 struct kprobe_ctlblk *kcb;
527 addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
528 if (*addr != BREAKPOINT_INSTRUCTION) {
530 * The breakpoint instruction was removed right
531 * after we hit it. Another cpu has removed
532 * either a probepoint or a debugger breakpoint
533 * at this address. In either case, no further
534 * handling of this interrupt is appropriate.
535 * Back up over the (now missing) int3 and run
536 * the original instruction.
538 regs->ip = (unsigned long)addr;
539 return 1;
543 * We don't want to be preempted for the entire
544 * duration of kprobe processing. We conditionally
545 * re-enable preemption at the end of this function,
546 * and also in reenter_kprobe() and setup_singlestep().
548 preempt_disable();
550 kcb = get_kprobe_ctlblk();
551 p = get_kprobe(addr);
553 if (p) {
554 if (kprobe_running()) {
555 if (reenter_kprobe(p, regs, kcb))
556 return 1;
557 } else {
558 set_current_kprobe(p, regs, kcb);
559 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
562 * If we have no pre-handler or it returned 0, we
563 * continue with normal processing. If we have a
564 * pre-handler and it returned non-zero, it prepped
565 * for calling the break_handler below on re-entry
566 * for jprobe processing, so get out doing nothing
567 * more here.
569 if (!p->pre_handler || !p->pre_handler(p, regs))
570 setup_singlestep(p, regs, kcb);
571 return 1;
573 } else if (kprobe_running()) {
574 p = __get_cpu_var(current_kprobe);
575 if (p->break_handler && p->break_handler(p, regs)) {
576 setup_singlestep(p, regs, kcb);
577 return 1;
579 } /* else: not a kprobe fault; let the kernel handle it */
581 preempt_enable_no_resched();
582 return 0;
586 * When a retprobed function returns, this code saves registers and
587 * calls trampoline_handler() runs, which calls the kretprobe's handler.
589 static void __used __kprobes kretprobe_trampoline_holder(void)
591 asm volatile (
592 ".global kretprobe_trampoline\n"
593 "kretprobe_trampoline: \n"
594 #ifdef CONFIG_X86_64
595 /* We don't bother saving the ss register */
596 " pushq %rsp\n"
597 " pushfq\n"
599 * Skip cs, ip, orig_ax.
600 * trampoline_handler() will plug in these values
602 " subq $24, %rsp\n"
603 " pushq %rdi\n"
604 " pushq %rsi\n"
605 " pushq %rdx\n"
606 " pushq %rcx\n"
607 " pushq %rax\n"
608 " pushq %r8\n"
609 " pushq %r9\n"
610 " pushq %r10\n"
611 " pushq %r11\n"
612 " pushq %rbx\n"
613 " pushq %rbp\n"
614 " pushq %r12\n"
615 " pushq %r13\n"
616 " pushq %r14\n"
617 " pushq %r15\n"
618 " movq %rsp, %rdi\n"
619 " call trampoline_handler\n"
620 /* Replace saved sp with true return address. */
621 " movq %rax, 152(%rsp)\n"
622 " popq %r15\n"
623 " popq %r14\n"
624 " popq %r13\n"
625 " popq %r12\n"
626 " popq %rbp\n"
627 " popq %rbx\n"
628 " popq %r11\n"
629 " popq %r10\n"
630 " popq %r9\n"
631 " popq %r8\n"
632 " popq %rax\n"
633 " popq %rcx\n"
634 " popq %rdx\n"
635 " popq %rsi\n"
636 " popq %rdi\n"
637 /* Skip orig_ax, ip, cs */
638 " addq $24, %rsp\n"
639 " popfq\n"
640 #else
641 " pushf\n"
643 * Skip cs, ip, orig_ax and gs.
644 * trampoline_handler() will plug in these values
646 " subl $16, %esp\n"
647 " pushl %fs\n"
648 " pushl %es\n"
649 " pushl %ds\n"
650 " pushl %eax\n"
651 " pushl %ebp\n"
652 " pushl %edi\n"
653 " pushl %esi\n"
654 " pushl %edx\n"
655 " pushl %ecx\n"
656 " pushl %ebx\n"
657 " movl %esp, %eax\n"
658 " call trampoline_handler\n"
659 /* Move flags to cs */
660 " movl 56(%esp), %edx\n"
661 " movl %edx, 52(%esp)\n"
662 /* Replace saved flags with true return address. */
663 " movl %eax, 56(%esp)\n"
664 " popl %ebx\n"
665 " popl %ecx\n"
666 " popl %edx\n"
667 " popl %esi\n"
668 " popl %edi\n"
669 " popl %ebp\n"
670 " popl %eax\n"
671 /* Skip ds, es, fs, gs, orig_ax and ip */
672 " addl $24, %esp\n"
673 " popf\n"
674 #endif
675 " ret\n");
679 * Called from kretprobe_trampoline
681 static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
683 struct kretprobe_instance *ri = NULL;
684 struct hlist_head *head, empty_rp;
685 struct hlist_node *node, *tmp;
686 unsigned long flags, orig_ret_address = 0;
687 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
689 INIT_HLIST_HEAD(&empty_rp);
690 kretprobe_hash_lock(current, &head, &flags);
691 /* fixup registers */
692 #ifdef CONFIG_X86_64
693 regs->cs = __KERNEL_CS;
694 #else
695 regs->cs = __KERNEL_CS | get_kernel_rpl();
696 regs->gs = 0;
697 #endif
698 regs->ip = trampoline_address;
699 regs->orig_ax = ~0UL;
702 * It is possible to have multiple instances associated with a given
703 * task either because multiple functions in the call path have
704 * return probes installed on them, and/or more than one
705 * return probe was registered for a target function.
707 * We can handle this because:
708 * - instances are always pushed into the head of the list
709 * - when multiple return probes are registered for the same
710 * function, the (chronologically) first instance's ret_addr
711 * will be the real return address, and all the rest will
712 * point to kretprobe_trampoline.
714 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
715 if (ri->task != current)
716 /* another task is sharing our hash bucket */
717 continue;
719 if (ri->rp && ri->rp->handler) {
720 __get_cpu_var(current_kprobe) = &ri->rp->kp;
721 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
722 ri->rp->handler(ri, regs);
723 __get_cpu_var(current_kprobe) = NULL;
726 orig_ret_address = (unsigned long)ri->ret_addr;
727 recycle_rp_inst(ri, &empty_rp);
729 if (orig_ret_address != trampoline_address)
731 * This is the real return address. Any other
732 * instances associated with this task are for
733 * other calls deeper on the call stack
735 break;
738 kretprobe_assert(ri, orig_ret_address, trampoline_address);
740 kretprobe_hash_unlock(current, &flags);
742 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
743 hlist_del(&ri->hlist);
744 kfree(ri);
746 return (void *)orig_ret_address;
750 * Called after single-stepping. p->addr is the address of the
751 * instruction whose first byte has been replaced by the "int 3"
752 * instruction. To avoid the SMP problems that can occur when we
753 * temporarily put back the original opcode to single-step, we
754 * single-stepped a copy of the instruction. The address of this
755 * copy is p->ainsn.insn.
757 * This function prepares to return from the post-single-step
758 * interrupt. We have to fix up the stack as follows:
760 * 0) Except in the case of absolute or indirect jump or call instructions,
761 * the new ip is relative to the copied instruction. We need to make
762 * it relative to the original instruction.
764 * 1) If the single-stepped instruction was pushfl, then the TF and IF
765 * flags are set in the just-pushed flags, and may need to be cleared.
767 * 2) If the single-stepped instruction was a call, the return address
768 * that is atop the stack is the address following the copied instruction.
769 * We need to make it the address following the original instruction.
771 * If this is the first time we've single-stepped the instruction at
772 * this probepoint, and the instruction is boostable, boost it: add a
773 * jump instruction after the copied instruction, that jumps to the next
774 * instruction after the probepoint.
776 static void __kprobes resume_execution(struct kprobe *p,
777 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
779 unsigned long *tos = stack_addr(regs);
780 unsigned long copy_ip = (unsigned long)p->ainsn.insn;
781 unsigned long orig_ip = (unsigned long)p->addr;
782 kprobe_opcode_t *insn = p->ainsn.insn;
784 /*skip the REX prefix*/
785 if (is_REX_prefix(insn))
786 insn++;
788 regs->flags &= ~X86_EFLAGS_TF;
789 switch (*insn) {
790 case 0x9c: /* pushfl */
791 *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
792 *tos |= kcb->kprobe_old_flags;
793 break;
794 case 0xc2: /* iret/ret/lret */
795 case 0xc3:
796 case 0xca:
797 case 0xcb:
798 case 0xcf:
799 case 0xea: /* jmp absolute -- ip is correct */
800 /* ip is already adjusted, no more changes required */
801 p->ainsn.boostable = 1;
802 goto no_change;
803 case 0xe8: /* call relative - Fix return addr */
804 *tos = orig_ip + (*tos - copy_ip);
805 break;
806 #ifdef CONFIG_X86_32
807 case 0x9a: /* call absolute -- same as call absolute, indirect */
808 *tos = orig_ip + (*tos - copy_ip);
809 goto no_change;
810 #endif
811 case 0xff:
812 if ((insn[1] & 0x30) == 0x10) {
814 * call absolute, indirect
815 * Fix return addr; ip is correct.
816 * But this is not boostable
818 *tos = orig_ip + (*tos - copy_ip);
819 goto no_change;
820 } else if (((insn[1] & 0x31) == 0x20) ||
821 ((insn[1] & 0x31) == 0x21)) {
823 * jmp near and far, absolute indirect
824 * ip is correct. And this is boostable
826 p->ainsn.boostable = 1;
827 goto no_change;
829 default:
830 break;
833 if (p->ainsn.boostable == 0) {
834 if ((regs->ip > copy_ip) &&
835 (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
837 * These instructions can be executed directly if it
838 * jumps back to correct address.
840 set_jmp_op((void *)regs->ip,
841 (void *)orig_ip + (regs->ip - copy_ip));
842 p->ainsn.boostable = 1;
843 } else {
844 p->ainsn.boostable = -1;
848 regs->ip += orig_ip - copy_ip;
850 no_change:
851 restore_btf();
855 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
856 * remain disabled thoroughout this function.
858 static int __kprobes post_kprobe_handler(struct pt_regs *regs)
860 struct kprobe *cur = kprobe_running();
861 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
863 if (!cur)
864 return 0;
866 resume_execution(cur, regs, kcb);
867 regs->flags |= kcb->kprobe_saved_flags;
869 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
870 kcb->kprobe_status = KPROBE_HIT_SSDONE;
871 cur->post_handler(cur, regs, 0);
874 /* Restore back the original saved kprobes variables and continue. */
875 if (kcb->kprobe_status == KPROBE_REENTER) {
876 restore_previous_kprobe(kcb);
877 goto out;
879 reset_current_kprobe();
880 out:
881 preempt_enable_no_resched();
884 * if somebody else is singlestepping across a probe point, flags
885 * will have TF set, in which case, continue the remaining processing
886 * of do_debug, as if this is not a probe hit.
888 if (regs->flags & X86_EFLAGS_TF)
889 return 0;
891 return 1;
894 int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
896 struct kprobe *cur = kprobe_running();
897 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
899 switch (kcb->kprobe_status) {
900 case KPROBE_HIT_SS:
901 case KPROBE_REENTER:
903 * We are here because the instruction being single
904 * stepped caused a page fault. We reset the current
905 * kprobe and the ip points back to the probe address
906 * and allow the page fault handler to continue as a
907 * normal page fault.
909 regs->ip = (unsigned long)cur->addr;
910 regs->flags |= kcb->kprobe_old_flags;
911 if (kcb->kprobe_status == KPROBE_REENTER)
912 restore_previous_kprobe(kcb);
913 else
914 reset_current_kprobe();
915 preempt_enable_no_resched();
916 break;
917 case KPROBE_HIT_ACTIVE:
918 case KPROBE_HIT_SSDONE:
920 * We increment the nmissed count for accounting,
921 * we can also use npre/npostfault count for accounting
922 * these specific fault cases.
924 kprobes_inc_nmissed_count(cur);
927 * We come here because instructions in the pre/post
928 * handler caused the page_fault, this could happen
929 * if handler tries to access user space by
930 * copy_from_user(), get_user() etc. Let the
931 * user-specified handler try to fix it first.
933 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
934 return 1;
937 * In case the user-specified fault handler returned
938 * zero, try to fix up.
940 if (fixup_exception(regs))
941 return 1;
944 * fixup routine could not handle it,
945 * Let do_page_fault() fix it.
947 break;
948 default:
949 break;
951 return 0;
955 * Wrapper routine for handling exceptions.
957 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
958 unsigned long val, void *data)
960 struct die_args *args = data;
961 int ret = NOTIFY_DONE;
963 if (args->regs && user_mode_vm(args->regs))
964 return ret;
966 switch (val) {
967 case DIE_INT3:
968 if (kprobe_handler(args->regs))
969 ret = NOTIFY_STOP;
970 break;
971 case DIE_DEBUG:
972 if (post_kprobe_handler(args->regs))
973 ret = NOTIFY_STOP;
974 break;
975 case DIE_GPF:
977 * To be potentially processing a kprobe fault and to
978 * trust the result from kprobe_running(), we have
979 * be non-preemptible.
981 if (!preemptible() && kprobe_running() &&
982 kprobe_fault_handler(args->regs, args->trapnr))
983 ret = NOTIFY_STOP;
984 break;
985 default:
986 break;
988 return ret;
991 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
993 struct jprobe *jp = container_of(p, struct jprobe, kp);
994 unsigned long addr;
995 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
997 kcb->jprobe_saved_regs = *regs;
998 kcb->jprobe_saved_sp = stack_addr(regs);
999 addr = (unsigned long)(kcb->jprobe_saved_sp);
1002 * As Linus pointed out, gcc assumes that the callee
1003 * owns the argument space and could overwrite it, e.g.
1004 * tailcall optimization. So, to be absolutely safe
1005 * we also save and restore enough stack bytes to cover
1006 * the argument area.
1008 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
1009 MIN_STACK_SIZE(addr));
1010 regs->flags &= ~X86_EFLAGS_IF;
1011 trace_hardirqs_off();
1012 regs->ip = (unsigned long)(jp->entry);
1013 return 1;
1016 void __kprobes jprobe_return(void)
1018 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1020 asm volatile (
1021 #ifdef CONFIG_X86_64
1022 " xchg %%rbx,%%rsp \n"
1023 #else
1024 " xchgl %%ebx,%%esp \n"
1025 #endif
1026 " int3 \n"
1027 " .globl jprobe_return_end\n"
1028 " jprobe_return_end: \n"
1029 " nop \n"::"b"
1030 (kcb->jprobe_saved_sp):"memory");
1033 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
1035 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1036 u8 *addr = (u8 *) (regs->ip - 1);
1037 struct jprobe *jp = container_of(p, struct jprobe, kp);
1039 if ((addr > (u8 *) jprobe_return) &&
1040 (addr < (u8 *) jprobe_return_end)) {
1041 if (stack_addr(regs) != kcb->jprobe_saved_sp) {
1042 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
1043 printk(KERN_ERR
1044 "current sp %p does not match saved sp %p\n",
1045 stack_addr(regs), kcb->jprobe_saved_sp);
1046 printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
1047 show_registers(saved_regs);
1048 printk(KERN_ERR "Current registers\n");
1049 show_registers(regs);
1050 BUG();
1052 *regs = kcb->jprobe_saved_regs;
1053 memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp),
1054 kcb->jprobes_stack,
1055 MIN_STACK_SIZE(kcb->jprobe_saved_sp));
1056 preempt_enable_no_resched();
1057 return 1;
1059 return 0;
1062 int __init arch_init_kprobes(void)
1064 return 0;
1067 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
1069 return 0;