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