x86, perf, bts, mm: Delete the never used BTS-ptrace code
[deliverable/linux.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>
c0f7ac3a 52#include <linux/ftrace.h>
9ec4b1f3 53
8533bbe9
MH
54#include <asm/cacheflush.h>
55#include <asm/desc.h>
1da177e4 56#include <asm/pgtable.h>
c28f8966 57#include <asm/uaccess.h>
19d36ccd 58#include <asm/alternative.h>
b46b3d70 59#include <asm/insn.h>
62edab90 60#include <asm/debugreg.h>
1da177e4 61
1da177e4
LT
62void jprobe_return_end(void);
63
e7a510f9
AM
64DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
65DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
1da177e4 66
98272ed0 67#define stack_addr(regs) ((unsigned long *)kernel_stack_pointer(regs))
8533bbe9
MH
68
69#define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
70 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
71 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
72 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
73 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
74 << (row % 32))
75 /*
76 * Undefined/reserved opcodes, conditional jump, Opcode Extension
77 * Groups, and some special opcodes can not boost.
78 */
79static const u32 twobyte_is_boostable[256 / 32] = {
80 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
81 /* ---------------------------------------------- */
82 W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
83 W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 10 */
84 W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
85 W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
86 W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
87 W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
88 W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
89 W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
90 W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
91 W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
92 W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
93 W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
94 W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
95 W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
96 W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
97 W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
98 /* ----------------------------------------------- */
99 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
100};
8533bbe9
MH
101#undef W
102
f438d914
MH
103struct kretprobe_blackpoint kretprobe_blacklist[] = {
104 {"__switch_to", }, /* This function switches only current task, but
105 doesn't switch kernel stack.*/
106 {NULL, NULL} /* Terminator */
107};
108const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
109
c0f7ac3a 110static void __kprobes __synthesize_relative_insn(void *from, void *to, u8 op)
aa470140 111{
c0f7ac3a
MH
112 struct __arch_relative_insn {
113 u8 op;
aa470140 114 s32 raddr;
c0f7ac3a
MH
115 } __attribute__((packed)) *insn;
116
117 insn = (struct __arch_relative_insn *)from;
118 insn->raddr = (s32)((long)(to) - ((long)(from) + 5));
119 insn->op = op;
120}
121
122/* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
123static void __kprobes synthesize_reljump(void *from, void *to)
124{
125 __synthesize_relative_insn(from, to, RELATIVEJUMP_OPCODE);
aa470140
MH
126}
127
9930927f
HH
128/*
129 * Check for the REX prefix which can only exist on X86_64
130 * X86_32 always returns 0
131 */
132static int __kprobes is_REX_prefix(kprobe_opcode_t *insn)
133{
134#ifdef CONFIG_X86_64
135 if ((*insn & 0xf0) == 0x40)
136 return 1;
137#endif
138 return 0;
139}
140
aa470140 141/*
d6be29b8
MH
142 * Returns non-zero if opcode is boostable.
143 * RIP relative instructions are adjusted at copying time in 64 bits mode
aa470140 144 */
e7b5e11e 145static int __kprobes can_boost(kprobe_opcode_t *opcodes)
aa470140 146{
aa470140
MH
147 kprobe_opcode_t opcode;
148 kprobe_opcode_t *orig_opcodes = opcodes;
149
cde5edbd 150 if (search_exception_tables((unsigned long)opcodes))
30390880
MH
151 return 0; /* Page fault may occur on this address. */
152
aa470140
MH
153retry:
154 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
155 return 0;
156 opcode = *(opcodes++);
157
158 /* 2nd-byte opcode */
159 if (opcode == 0x0f) {
160 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
161 return 0;
8533bbe9
MH
162 return test_bit(*opcodes,
163 (unsigned long *)twobyte_is_boostable);
aa470140
MH
164 }
165
166 switch (opcode & 0xf0) {
d6be29b8 167#ifdef CONFIG_X86_64
aa470140
MH
168 case 0x40:
169 goto retry; /* REX prefix is boostable */
d6be29b8 170#endif
aa470140
MH
171 case 0x60:
172 if (0x63 < opcode && opcode < 0x67)
173 goto retry; /* prefixes */
174 /* can't boost Address-size override and bound */
175 return (opcode != 0x62 && opcode != 0x67);
176 case 0x70:
177 return 0; /* can't boost conditional jump */
178 case 0xc0:
179 /* can't boost software-interruptions */
180 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
181 case 0xd0:
182 /* can boost AA* and XLAT */
183 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
184 case 0xe0:
185 /* can boost in/out and absolute jmps */
186 return ((opcode & 0x04) || opcode == 0xea);
187 case 0xf0:
188 if ((opcode & 0x0c) == 0 && opcode != 0xf1)
189 goto retry; /* lock/rep(ne) prefix */
190 /* clear and set flags are boostable */
191 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
192 default:
193 /* segment override prefixes are boostable */
194 if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
195 goto retry; /* prefixes */
196 /* CS override prefix and call are not boostable */
197 return (opcode != 0x2e && opcode != 0x9a);
198 }
199}
200
b46b3d70
MH
201/* Recover the probed instruction at addr for further analysis. */
202static int recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)
203{
204 struct kprobe *kp;
205 kp = get_kprobe((void *)addr);
206 if (!kp)
207 return -EINVAL;
208
209 /*
210 * Basically, kp->ainsn.insn has an original instruction.
211 * However, RIP-relative instruction can not do single-stepping
c0f7ac3a 212 * at different place, __copy_instruction() tweaks the displacement of
b46b3d70
MH
213 * that instruction. In that case, we can't recover the instruction
214 * from the kp->ainsn.insn.
215 *
216 * On the other hand, kp->opcode has a copy of the first byte of
217 * the probed instruction, which is overwritten by int3. And
218 * the instruction at kp->addr is not modified by kprobes except
219 * for the first byte, we can recover the original instruction
220 * from it and kp->opcode.
221 */
222 memcpy(buf, kp->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
223 buf[0] = kp->opcode;
224 return 0;
225}
226
227/* Dummy buffers for kallsyms_lookup */
228static char __dummy_buf[KSYM_NAME_LEN];
229
230/* Check if paddr is at an instruction boundary */
231static int __kprobes can_probe(unsigned long paddr)
232{
233 int ret;
234 unsigned long addr, offset = 0;
235 struct insn insn;
236 kprobe_opcode_t buf[MAX_INSN_SIZE];
237
238 if (!kallsyms_lookup(paddr, NULL, &offset, NULL, __dummy_buf))
239 return 0;
240
241 /* Decode instructions */
242 addr = paddr - offset;
243 while (addr < paddr) {
244 kernel_insn_init(&insn, (void *)addr);
245 insn_get_opcode(&insn);
246
247 /*
248 * Check if the instruction has been modified by another
249 * kprobe, in which case we replace the breakpoint by the
250 * original instruction in our buffer.
251 */
252 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
253 ret = recover_probed_instruction(buf, addr);
254 if (ret)
255 /*
256 * Another debugging subsystem might insert
257 * this breakpoint. In that case, we can't
258 * recover it.
259 */
260 return 0;
261 kernel_insn_init(&insn, buf);
262 }
263 insn_get_length(&insn);
264 addr += insn.length;
265 }
266
267 return (addr == paddr);
268}
269
1da177e4 270/*
d6be29b8 271 * Returns non-zero if opcode modifies the interrupt flag.
1da177e4 272 */
8645419c 273static int __kprobes is_IF_modifier(kprobe_opcode_t *insn)
1da177e4
LT
274{
275 switch (*insn) {
276 case 0xfa: /* cli */
277 case 0xfb: /* sti */
278 case 0xcf: /* iret/iretd */
279 case 0x9d: /* popf/popfd */
280 return 1;
281 }
9930927f 282
8533bbe9 283 /*
9930927f 284 * on X86_64, 0x40-0x4f are REX prefixes so we need to look
8533bbe9
MH
285 * at the next byte instead.. but of course not recurse infinitely
286 */
9930927f 287 if (is_REX_prefix(insn))
8533bbe9 288 return is_IF_modifier(++insn);
9930927f 289
1da177e4
LT
290 return 0;
291}
292
293/*
c0f7ac3a
MH
294 * Copy an instruction and adjust the displacement if the instruction
295 * uses the %rip-relative addressing mode.
aa470140 296 * If it does, Return the address of the 32-bit displacement word.
1da177e4 297 * If not, return null.
31f80e45 298 * Only applicable to 64-bit x86.
1da177e4 299 */
c0f7ac3a 300static int __kprobes __copy_instruction(u8 *dest, u8 *src, int recover)
1da177e4 301{
89ae465b 302 struct insn insn;
c0f7ac3a
MH
303 int ret;
304 kprobe_opcode_t buf[MAX_INSN_SIZE];
1da177e4 305
c0f7ac3a
MH
306 kernel_insn_init(&insn, src);
307 if (recover) {
308 insn_get_opcode(&insn);
309 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
310 ret = recover_probed_instruction(buf,
311 (unsigned long)src);
312 if (ret)
313 return 0;
314 kernel_insn_init(&insn, buf);
315 }
316 }
317 insn_get_length(&insn);
318 memcpy(dest, insn.kaddr, insn.length);
319
320#ifdef CONFIG_X86_64
89ae465b
MH
321 if (insn_rip_relative(&insn)) {
322 s64 newdisp;
323 u8 *disp;
c0f7ac3a 324 kernel_insn_init(&insn, dest);
89ae465b
MH
325 insn_get_displacement(&insn);
326 /*
327 * The copied instruction uses the %rip-relative addressing
328 * mode. Adjust the displacement for the difference between
329 * the original location of this instruction and the location
330 * of the copy that will actually be run. The tricky bit here
331 * is making sure that the sign extension happens correctly in
332 * this calculation, since we need a signed 32-bit result to
333 * be sign-extended to 64 bits when it's added to the %rip
334 * value and yield the same 64-bit result that the sign-
335 * extension of the original signed 32-bit displacement would
336 * have given.
337 */
c0f7ac3a
MH
338 newdisp = (u8 *) src + (s64) insn.displacement.value -
339 (u8 *) dest;
89ae465b 340 BUG_ON((s64) (s32) newdisp != newdisp); /* Sanity check. */
c0f7ac3a 341 disp = (u8 *) dest + insn_offset_displacement(&insn);
89ae465b 342 *(s32 *) disp = (s32) newdisp;
1da177e4 343 }
d6be29b8 344#endif
c0f7ac3a 345 return insn.length;
31f80e45 346}
1da177e4 347
f709b122 348static void __kprobes arch_copy_kprobe(struct kprobe *p)
1da177e4 349{
c0f7ac3a
MH
350 /*
351 * Copy an instruction without recovering int3, because it will be
352 * put by another subsystem.
353 */
354 __copy_instruction(p->ainsn.insn, p->addr, 0);
31f80e45 355
8533bbe9 356 if (can_boost(p->addr))
aa470140 357 p->ainsn.boostable = 0;
8533bbe9 358 else
aa470140 359 p->ainsn.boostable = -1;
8533bbe9 360
7e1048b1 361 p->opcode = *p->addr;
1da177e4
LT
362}
363
8533bbe9
MH
364int __kprobes arch_prepare_kprobe(struct kprobe *p)
365{
4554dbcb
MH
366 if (alternatives_text_reserved(p->addr, p->addr))
367 return -EINVAL;
368
b46b3d70
MH
369 if (!can_probe((unsigned long)p->addr))
370 return -EILSEQ;
8533bbe9
MH
371 /* insn: must be on special executable page on x86. */
372 p->ainsn.insn = get_insn_slot();
373 if (!p->ainsn.insn)
374 return -ENOMEM;
375 arch_copy_kprobe(p);
376 return 0;
377}
378
0f2fbdcb 379void __kprobes arch_arm_kprobe(struct kprobe *p)
1da177e4 380{
19d36ccd 381 text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
1da177e4
LT
382}
383
0f2fbdcb 384void __kprobes arch_disarm_kprobe(struct kprobe *p)
1da177e4 385{
19d36ccd 386 text_poke(p->addr, &p->opcode, 1);
7e1048b1
RL
387}
388
0498b635 389void __kprobes arch_remove_kprobe(struct kprobe *p)
7e1048b1 390{
12941560
MH
391 if (p->ainsn.insn) {
392 free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
393 p->ainsn.insn = NULL;
394 }
1da177e4
LT
395}
396
3b60211c 397static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
aa3d7e3d 398{
e7a510f9
AM
399 kcb->prev_kprobe.kp = kprobe_running();
400 kcb->prev_kprobe.status = kcb->kprobe_status;
8533bbe9
MH
401 kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
402 kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
aa3d7e3d
PP
403}
404
3b60211c 405static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
aa3d7e3d 406{
e7a510f9
AM
407 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
408 kcb->kprobe_status = kcb->prev_kprobe.status;
8533bbe9
MH
409 kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
410 kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
aa3d7e3d
PP
411}
412
3b60211c 413static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
e7a510f9 414 struct kprobe_ctlblk *kcb)
aa3d7e3d 415{
e7a510f9 416 __get_cpu_var(current_kprobe) = p;
8533bbe9 417 kcb->kprobe_saved_flags = kcb->kprobe_old_flags
053de044 418 = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
aa3d7e3d 419 if (is_IF_modifier(p->ainsn.insn))
053de044 420 kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
aa3d7e3d
PP
421}
422
e7b5e11e 423static void __kprobes clear_btf(void)
1ecc798c 424{
faa4602e 425 /* XXX */
1ecc798c
RM
426}
427
e7b5e11e 428static void __kprobes restore_btf(void)
1ecc798c 429{
faa4602e 430 /* XXX */
1ecc798c
RM
431}
432
4c4308cb 433void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
0f2fbdcb 434 struct pt_regs *regs)
73649dab 435{
8533bbe9 436 unsigned long *sara = stack_addr(regs);
ba8af12f 437
4c4308cb 438 ri->ret_addr = (kprobe_opcode_t *) *sara;
8533bbe9 439
4c4308cb
CH
440 /* Replace the return addr with trampoline addr */
441 *sara = (unsigned long) &kretprobe_trampoline;
73649dab 442}
f315decb 443
c0f7ac3a
MH
444#ifdef CONFIG_OPTPROBES
445static int __kprobes setup_detour_execution(struct kprobe *p,
446 struct pt_regs *regs,
447 int reenter);
448#else
449#define setup_detour_execution(p, regs, reenter) (0)
450#endif
451
f315decb 452static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs,
0f94eb63 453 struct kprobe_ctlblk *kcb, int reenter)
f315decb 454{
c0f7ac3a
MH
455 if (setup_detour_execution(p, regs, reenter))
456 return;
457
615d0ebb 458#if !defined(CONFIG_PREEMPT)
f315decb
AS
459 if (p->ainsn.boostable == 1 && !p->post_handler) {
460 /* Boost up -- we can execute copied instructions directly */
0f94eb63
MH
461 if (!reenter)
462 reset_current_kprobe();
463 /*
464 * Reentering boosted probe doesn't reset current_kprobe,
465 * nor set current_kprobe, because it doesn't use single
466 * stepping.
467 */
f315decb
AS
468 regs->ip = (unsigned long)p->ainsn.insn;
469 preempt_enable_no_resched();
470 return;
471 }
472#endif
0f94eb63
MH
473 if (reenter) {
474 save_previous_kprobe(kcb);
475 set_current_kprobe(p, regs, kcb);
476 kcb->kprobe_status = KPROBE_REENTER;
477 } else
478 kcb->kprobe_status = KPROBE_HIT_SS;
479 /* Prepare real single stepping */
480 clear_btf();
481 regs->flags |= X86_EFLAGS_TF;
482 regs->flags &= ~X86_EFLAGS_IF;
483 /* single step inline if the instruction is an int3 */
484 if (p->opcode == BREAKPOINT_INSTRUCTION)
485 regs->ip = (unsigned long)p->addr;
486 else
487 regs->ip = (unsigned long)p->ainsn.insn;
f315decb
AS
488}
489
40102d4a
HH
490/*
491 * We have reentered the kprobe_handler(), since another probe was hit while
492 * within the handler. We save the original kprobes variables and just single
493 * step on the instruction of the new probe without calling any user handlers.
494 */
59e87cdc
MH
495static int __kprobes reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
496 struct kprobe_ctlblk *kcb)
40102d4a 497{
f315decb
AS
498 switch (kcb->kprobe_status) {
499 case KPROBE_HIT_SSDONE:
f315decb 500 case KPROBE_HIT_ACTIVE:
fb8830e7 501 kprobes_inc_nmissed_count(p);
0f94eb63 502 setup_singlestep(p, regs, kcb, 1);
f315decb
AS
503 break;
504 case KPROBE_HIT_SS:
e9afe9e1
MH
505 /* A probe has been hit in the codepath leading up to, or just
506 * after, single-stepping of a probed instruction. This entire
507 * codepath should strictly reside in .kprobes.text section.
508 * Raise a BUG or we'll continue in an endless reentering loop
509 * and eventually a stack overflow.
510 */
511 printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n",
512 p->addr);
513 dump_kprobe(p);
514 BUG();
f315decb
AS
515 default:
516 /* impossible cases */
517 WARN_ON(1);
fb8830e7 518 return 0;
59e87cdc 519 }
f315decb 520
59e87cdc 521 return 1;
40102d4a 522}
73649dab 523
8533bbe9
MH
524/*
525 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
af901ca1 526 * remain disabled throughout this function.
8533bbe9
MH
527 */
528static int __kprobes kprobe_handler(struct pt_regs *regs)
1da177e4 529{
8533bbe9 530 kprobe_opcode_t *addr;
f315decb 531 struct kprobe *p;
d217d545
AM
532 struct kprobe_ctlblk *kcb;
533
8533bbe9 534 addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
f315decb
AS
535 if (*addr != BREAKPOINT_INSTRUCTION) {
536 /*
537 * The breakpoint instruction was removed right
538 * after we hit it. Another cpu has removed
539 * either a probepoint or a debugger breakpoint
540 * at this address. In either case, no further
541 * handling of this interrupt is appropriate.
542 * Back up over the (now missing) int3 and run
543 * the original instruction.
544 */
545 regs->ip = (unsigned long)addr;
546 return 1;
547 }
8533bbe9 548
d217d545
AM
549 /*
550 * We don't want to be preempted for the entire
f315decb
AS
551 * duration of kprobe processing. We conditionally
552 * re-enable preemption at the end of this function,
553 * and also in reenter_kprobe() and setup_singlestep().
d217d545
AM
554 */
555 preempt_disable();
1da177e4 556
f315decb 557 kcb = get_kprobe_ctlblk();
b9760156 558 p = get_kprobe(addr);
f315decb 559
b9760156 560 if (p) {
b9760156 561 if (kprobe_running()) {
f315decb
AS
562 if (reenter_kprobe(p, regs, kcb))
563 return 1;
1da177e4 564 } else {
b9760156
HH
565 set_current_kprobe(p, regs, kcb);
566 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
f315decb 567
1da177e4 568 /*
f315decb
AS
569 * If we have no pre-handler or it returned 0, we
570 * continue with normal processing. If we have a
571 * pre-handler and it returned non-zero, it prepped
572 * for calling the break_handler below on re-entry
573 * for jprobe processing, so get out doing nothing
574 * more here.
1da177e4 575 */
f315decb 576 if (!p->pre_handler || !p->pre_handler(p, regs))
0f94eb63 577 setup_singlestep(p, regs, kcb, 0);
f315decb 578 return 1;
b9760156 579 }
f315decb
AS
580 } else if (kprobe_running()) {
581 p = __get_cpu_var(current_kprobe);
582 if (p->break_handler && p->break_handler(p, regs)) {
0f94eb63 583 setup_singlestep(p, regs, kcb, 0);
f315decb 584 return 1;
1da177e4 585 }
f315decb 586 } /* else: not a kprobe fault; let the kernel handle it */
1da177e4 587
d217d545 588 preempt_enable_no_resched();
f315decb 589 return 0;
1da177e4
LT
590}
591
f007ea26
MH
592#ifdef CONFIG_X86_64
593#define SAVE_REGS_STRING \
594 /* Skip cs, ip, orig_ax. */ \
595 " subq $24, %rsp\n" \
596 " pushq %rdi\n" \
597 " pushq %rsi\n" \
598 " pushq %rdx\n" \
599 " pushq %rcx\n" \
600 " pushq %rax\n" \
601 " pushq %r8\n" \
602 " pushq %r9\n" \
603 " pushq %r10\n" \
604 " pushq %r11\n" \
605 " pushq %rbx\n" \
606 " pushq %rbp\n" \
607 " pushq %r12\n" \
608 " pushq %r13\n" \
609 " pushq %r14\n" \
610 " pushq %r15\n"
611#define RESTORE_REGS_STRING \
612 " popq %r15\n" \
613 " popq %r14\n" \
614 " popq %r13\n" \
615 " popq %r12\n" \
616 " popq %rbp\n" \
617 " popq %rbx\n" \
618 " popq %r11\n" \
619 " popq %r10\n" \
620 " popq %r9\n" \
621 " popq %r8\n" \
622 " popq %rax\n" \
623 " popq %rcx\n" \
624 " popq %rdx\n" \
625 " popq %rsi\n" \
626 " popq %rdi\n" \
627 /* Skip orig_ax, ip, cs */ \
628 " addq $24, %rsp\n"
629#else
630#define SAVE_REGS_STRING \
631 /* Skip cs, ip, orig_ax and gs. */ \
632 " subl $16, %esp\n" \
633 " pushl %fs\n" \
634 " pushl %ds\n" \
635 " pushl %es\n" \
636 " pushl %eax\n" \
637 " pushl %ebp\n" \
638 " pushl %edi\n" \
639 " pushl %esi\n" \
640 " pushl %edx\n" \
641 " pushl %ecx\n" \
642 " pushl %ebx\n"
643#define RESTORE_REGS_STRING \
644 " popl %ebx\n" \
645 " popl %ecx\n" \
646 " popl %edx\n" \
647 " popl %esi\n" \
648 " popl %edi\n" \
649 " popl %ebp\n" \
650 " popl %eax\n" \
651 /* Skip ds, es, fs, gs, orig_ax, and ip. Note: don't pop cs here*/\
652 " addl $24, %esp\n"
653#endif
654
73649dab 655/*
da07ab03
MH
656 * When a retprobed function returns, this code saves registers and
657 * calls trampoline_handler() runs, which calls the kretprobe's handler.
73649dab 658 */
f1452d42 659static void __used __kprobes kretprobe_trampoline_holder(void)
1017579a 660{
d6be29b8
MH
661 asm volatile (
662 ".global kretprobe_trampoline\n"
da07ab03 663 "kretprobe_trampoline: \n"
d6be29b8 664#ifdef CONFIG_X86_64
da07ab03
MH
665 /* We don't bother saving the ss register */
666 " pushq %rsp\n"
667 " pushfq\n"
f007ea26 668 SAVE_REGS_STRING
da07ab03
MH
669 " movq %rsp, %rdi\n"
670 " call trampoline_handler\n"
671 /* Replace saved sp with true return address. */
672 " movq %rax, 152(%rsp)\n"
f007ea26 673 RESTORE_REGS_STRING
da07ab03 674 " popfq\n"
d6be29b8
MH
675#else
676 " pushf\n"
f007ea26 677 SAVE_REGS_STRING
d6be29b8
MH
678 " movl %esp, %eax\n"
679 " call trampoline_handler\n"
680 /* Move flags to cs */
fee039a1
MH
681 " movl 56(%esp), %edx\n"
682 " movl %edx, 52(%esp)\n"
d6be29b8 683 /* Replace saved flags with true return address. */
fee039a1 684 " movl %eax, 56(%esp)\n"
f007ea26 685 RESTORE_REGS_STRING
d6be29b8
MH
686 " popf\n"
687#endif
da07ab03 688 " ret\n");
1017579a 689}
73649dab
RL
690
691/*
da07ab03 692 * Called from kretprobe_trampoline
73649dab 693 */
f1452d42 694static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
73649dab 695{
62c27be0 696 struct kretprobe_instance *ri = NULL;
99219a3f 697 struct hlist_head *head, empty_rp;
62c27be0 698 struct hlist_node *node, *tmp;
991a51d8 699 unsigned long flags, orig_ret_address = 0;
d6be29b8 700 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
73649dab 701
99219a3f 702 INIT_HLIST_HEAD(&empty_rp);
ef53d9c5 703 kretprobe_hash_lock(current, &head, &flags);
8533bbe9 704 /* fixup registers */
d6be29b8 705#ifdef CONFIG_X86_64
da07ab03 706 regs->cs = __KERNEL_CS;
d6be29b8
MH
707#else
708 regs->cs = __KERNEL_CS | get_kernel_rpl();
fee039a1 709 regs->gs = 0;
d6be29b8 710#endif
da07ab03 711 regs->ip = trampoline_address;
8533bbe9 712 regs->orig_ax = ~0UL;
73649dab 713
ba8af12f
RL
714 /*
715 * It is possible to have multiple instances associated with a given
8533bbe9 716 * task either because multiple functions in the call path have
025dfdaf 717 * return probes installed on them, and/or more than one
ba8af12f
RL
718 * return probe was registered for a target function.
719 *
720 * We can handle this because:
8533bbe9 721 * - instances are always pushed into the head of the list
ba8af12f 722 * - when multiple return probes are registered for the same
8533bbe9
MH
723 * function, the (chronologically) first instance's ret_addr
724 * will be the real return address, and all the rest will
725 * point to kretprobe_trampoline.
ba8af12f
RL
726 */
727 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
62c27be0 728 if (ri->task != current)
ba8af12f 729 /* another task is sharing our hash bucket */
62c27be0 730 continue;
ba8af12f 731
da07ab03
MH
732 if (ri->rp && ri->rp->handler) {
733 __get_cpu_var(current_kprobe) = &ri->rp->kp;
734 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
ba8af12f 735 ri->rp->handler(ri, regs);
da07ab03
MH
736 __get_cpu_var(current_kprobe) = NULL;
737 }
ba8af12f
RL
738
739 orig_ret_address = (unsigned long)ri->ret_addr;
99219a3f 740 recycle_rp_inst(ri, &empty_rp);
ba8af12f
RL
741
742 if (orig_ret_address != trampoline_address)
743 /*
744 * This is the real return address. Any other
745 * instances associated with this task are for
746 * other calls deeper on the call stack
747 */
748 break;
73649dab 749 }
ba8af12f 750
0f95b7fc 751 kretprobe_assert(ri, orig_ret_address, trampoline_address);
ba8af12f 752
ef53d9c5 753 kretprobe_hash_unlock(current, &flags);
ba8af12f 754
99219a3f 755 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
756 hlist_del(&ri->hlist);
757 kfree(ri);
758 }
da07ab03 759 return (void *)orig_ret_address;
73649dab
RL
760}
761
1da177e4
LT
762/*
763 * Called after single-stepping. p->addr is the address of the
764 * instruction whose first byte has been replaced by the "int 3"
765 * instruction. To avoid the SMP problems that can occur when we
766 * temporarily put back the original opcode to single-step, we
767 * single-stepped a copy of the instruction. The address of this
768 * copy is p->ainsn.insn.
769 *
770 * This function prepares to return from the post-single-step
771 * interrupt. We have to fix up the stack as follows:
772 *
773 * 0) Except in the case of absolute or indirect jump or call instructions,
65ea5b03 774 * the new ip is relative to the copied instruction. We need to make
1da177e4
LT
775 * it relative to the original instruction.
776 *
777 * 1) If the single-stepped instruction was pushfl, then the TF and IF
65ea5b03 778 * flags are set in the just-pushed flags, and may need to be cleared.
1da177e4
LT
779 *
780 * 2) If the single-stepped instruction was a call, the return address
781 * that is atop the stack is the address following the copied instruction.
782 * We need to make it the address following the original instruction.
aa470140
MH
783 *
784 * If this is the first time we've single-stepped the instruction at
785 * this probepoint, and the instruction is boostable, boost it: add a
786 * jump instruction after the copied instruction, that jumps to the next
787 * instruction after the probepoint.
1da177e4 788 */
e7a510f9
AM
789static void __kprobes resume_execution(struct kprobe *p,
790 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
1da177e4 791{
8533bbe9
MH
792 unsigned long *tos = stack_addr(regs);
793 unsigned long copy_ip = (unsigned long)p->ainsn.insn;
794 unsigned long orig_ip = (unsigned long)p->addr;
1da177e4
LT
795 kprobe_opcode_t *insn = p->ainsn.insn;
796
797 /*skip the REX prefix*/
9930927f 798 if (is_REX_prefix(insn))
1da177e4
LT
799 insn++;
800
053de044 801 regs->flags &= ~X86_EFLAGS_TF;
1da177e4 802 switch (*insn) {
0b0122fa 803 case 0x9c: /* pushfl */
053de044 804 *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
8533bbe9 805 *tos |= kcb->kprobe_old_flags;
1da177e4 806 break;
0b0122fa
MH
807 case 0xc2: /* iret/ret/lret */
808 case 0xc3:
0b9e2cac 809 case 0xca:
0b0122fa
MH
810 case 0xcb:
811 case 0xcf:
812 case 0xea: /* jmp absolute -- ip is correct */
813 /* ip is already adjusted, no more changes required */
aa470140 814 p->ainsn.boostable = 1;
0b0122fa
MH
815 goto no_change;
816 case 0xe8: /* call relative - Fix return addr */
8533bbe9 817 *tos = orig_ip + (*tos - copy_ip);
1da177e4 818 break;
e7b5e11e 819#ifdef CONFIG_X86_32
d6be29b8
MH
820 case 0x9a: /* call absolute -- same as call absolute, indirect */
821 *tos = orig_ip + (*tos - copy_ip);
822 goto no_change;
823#endif
1da177e4 824 case 0xff:
dc49e344 825 if ((insn[1] & 0x30) == 0x10) {
8533bbe9
MH
826 /*
827 * call absolute, indirect
828 * Fix return addr; ip is correct.
829 * But this is not boostable
830 */
831 *tos = orig_ip + (*tos - copy_ip);
0b0122fa 832 goto no_change;
8533bbe9
MH
833 } else if (((insn[1] & 0x31) == 0x20) ||
834 ((insn[1] & 0x31) == 0x21)) {
835 /*
836 * jmp near and far, absolute indirect
837 * ip is correct. And this is boostable
838 */
aa470140 839 p->ainsn.boostable = 1;
0b0122fa 840 goto no_change;
1da177e4 841 }
1da177e4
LT
842 default:
843 break;
844 }
845
aa470140 846 if (p->ainsn.boostable == 0) {
8533bbe9
MH
847 if ((regs->ip > copy_ip) &&
848 (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
aa470140
MH
849 /*
850 * These instructions can be executed directly if it
851 * jumps back to correct address.
852 */
c0f7ac3a
MH
853 synthesize_reljump((void *)regs->ip,
854 (void *)orig_ip + (regs->ip - copy_ip));
aa470140
MH
855 p->ainsn.boostable = 1;
856 } else {
857 p->ainsn.boostable = -1;
858 }
859 }
860
8533bbe9 861 regs->ip += orig_ip - copy_ip;
65ea5b03 862
0b0122fa 863no_change:
1ecc798c 864 restore_btf();
1da177e4
LT
865}
866
8533bbe9
MH
867/*
868 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
af901ca1 869 * remain disabled throughout this function.
8533bbe9
MH
870 */
871static int __kprobes post_kprobe_handler(struct pt_regs *regs)
1da177e4 872{
e7a510f9
AM
873 struct kprobe *cur = kprobe_running();
874 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
875
876 if (!cur)
1da177e4
LT
877 return 0;
878
acb5b8a2
YL
879 resume_execution(cur, regs, kcb);
880 regs->flags |= kcb->kprobe_saved_flags;
acb5b8a2 881
e7a510f9
AM
882 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
883 kcb->kprobe_status = KPROBE_HIT_SSDONE;
884 cur->post_handler(cur, regs, 0);
aa3d7e3d 885 }
1da177e4 886
8533bbe9 887 /* Restore back the original saved kprobes variables and continue. */
e7a510f9
AM
888 if (kcb->kprobe_status == KPROBE_REENTER) {
889 restore_previous_kprobe(kcb);
aa3d7e3d 890 goto out;
aa3d7e3d 891 }
e7a510f9 892 reset_current_kprobe();
aa3d7e3d 893out:
1da177e4
LT
894 preempt_enable_no_resched();
895
896 /*
65ea5b03 897 * if somebody else is singlestepping across a probe point, flags
1da177e4
LT
898 * will have TF set, in which case, continue the remaining processing
899 * of do_debug, as if this is not a probe hit.
900 */
053de044 901 if (regs->flags & X86_EFLAGS_TF)
1da177e4
LT
902 return 0;
903
904 return 1;
905}
906
0f2fbdcb 907int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
1da177e4 908{
e7a510f9
AM
909 struct kprobe *cur = kprobe_running();
910 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
911
d6be29b8 912 switch (kcb->kprobe_status) {
c28f8966
PP
913 case KPROBE_HIT_SS:
914 case KPROBE_REENTER:
915 /*
916 * We are here because the instruction being single
917 * stepped caused a page fault. We reset the current
65ea5b03 918 * kprobe and the ip points back to the probe address
c28f8966
PP
919 * and allow the page fault handler to continue as a
920 * normal page fault.
921 */
65ea5b03 922 regs->ip = (unsigned long)cur->addr;
8533bbe9 923 regs->flags |= kcb->kprobe_old_flags;
c28f8966
PP
924 if (kcb->kprobe_status == KPROBE_REENTER)
925 restore_previous_kprobe(kcb);
926 else
927 reset_current_kprobe();
1da177e4 928 preempt_enable_no_resched();
c28f8966
PP
929 break;
930 case KPROBE_HIT_ACTIVE:
931 case KPROBE_HIT_SSDONE:
932 /*
933 * We increment the nmissed count for accounting,
8533bbe9 934 * we can also use npre/npostfault count for accounting
c28f8966
PP
935 * these specific fault cases.
936 */
937 kprobes_inc_nmissed_count(cur);
938
939 /*
940 * We come here because instructions in the pre/post
941 * handler caused the page_fault, this could happen
942 * if handler tries to access user space by
943 * copy_from_user(), get_user() etc. Let the
944 * user-specified handler try to fix it first.
945 */
946 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
947 return 1;
948
949 /*
950 * In case the user-specified fault handler returned
951 * zero, try to fix up.
952 */
d6be29b8
MH
953 if (fixup_exception(regs))
954 return 1;
6d48583b 955
c28f8966 956 /*
8533bbe9 957 * fixup routine could not handle it,
c28f8966
PP
958 * Let do_page_fault() fix it.
959 */
960 break;
961 default:
962 break;
1da177e4
LT
963 }
964 return 0;
965}
966
967/*
968 * Wrapper routine for handling exceptions.
969 */
0f2fbdcb
PP
970int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
971 unsigned long val, void *data)
1da177e4 972{
ade1af77 973 struct die_args *args = data;
66ff2d06
AM
974 int ret = NOTIFY_DONE;
975
8533bbe9 976 if (args->regs && user_mode_vm(args->regs))
2326c770 977 return ret;
978
1da177e4
LT
979 switch (val) {
980 case DIE_INT3:
981 if (kprobe_handler(args->regs))
66ff2d06 982 ret = NOTIFY_STOP;
1da177e4
LT
983 break;
984 case DIE_DEBUG:
62edab90
P
985 if (post_kprobe_handler(args->regs)) {
986 /*
987 * Reset the BS bit in dr6 (pointed by args->err) to
988 * denote completion of processing
989 */
990 (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
66ff2d06 991 ret = NOTIFY_STOP;
62edab90 992 }
1da177e4
LT
993 break;
994 case DIE_GPF:
b506a9d0
QB
995 /*
996 * To be potentially processing a kprobe fault and to
997 * trust the result from kprobe_running(), we have
998 * be non-preemptible.
999 */
1000 if (!preemptible() && kprobe_running() &&
1da177e4 1001 kprobe_fault_handler(args->regs, args->trapnr))
66ff2d06 1002 ret = NOTIFY_STOP;
1da177e4
LT
1003 break;
1004 default:
1005 break;
1006 }
66ff2d06 1007 return ret;
1da177e4
LT
1008}
1009
0f2fbdcb 1010int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
1da177e4
LT
1011{
1012 struct jprobe *jp = container_of(p, struct jprobe, kp);
1013 unsigned long addr;
e7a510f9 1014 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1da177e4 1015
e7a510f9 1016 kcb->jprobe_saved_regs = *regs;
8533bbe9
MH
1017 kcb->jprobe_saved_sp = stack_addr(regs);
1018 addr = (unsigned long)(kcb->jprobe_saved_sp);
1019
1da177e4
LT
1020 /*
1021 * As Linus pointed out, gcc assumes that the callee
1022 * owns the argument space and could overwrite it, e.g.
1023 * tailcall optimization. So, to be absolutely safe
1024 * we also save and restore enough stack bytes to cover
1025 * the argument area.
1026 */
e7a510f9 1027 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
d6be29b8 1028 MIN_STACK_SIZE(addr));
053de044 1029 regs->flags &= ~X86_EFLAGS_IF;
58dfe883 1030 trace_hardirqs_off();
65ea5b03 1031 regs->ip = (unsigned long)(jp->entry);
1da177e4
LT
1032 return 1;
1033}
1034
0f2fbdcb 1035void __kprobes jprobe_return(void)
1da177e4 1036{
e7a510f9
AM
1037 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1038
d6be29b8
MH
1039 asm volatile (
1040#ifdef CONFIG_X86_64
1041 " xchg %%rbx,%%rsp \n"
1042#else
1043 " xchgl %%ebx,%%esp \n"
1044#endif
1045 " int3 \n"
1046 " .globl jprobe_return_end\n"
1047 " jprobe_return_end: \n"
1048 " nop \n"::"b"
1049 (kcb->jprobe_saved_sp):"memory");
1da177e4
LT
1050}
1051
0f2fbdcb 1052int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
1da177e4 1053{
e7a510f9 1054 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
65ea5b03 1055 u8 *addr = (u8 *) (regs->ip - 1);
1da177e4
LT
1056 struct jprobe *jp = container_of(p, struct jprobe, kp);
1057
d6be29b8
MH
1058 if ((addr > (u8 *) jprobe_return) &&
1059 (addr < (u8 *) jprobe_return_end)) {
8533bbe9 1060 if (stack_addr(regs) != kcb->jprobe_saved_sp) {
29b6cd79 1061 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
d6be29b8
MH
1062 printk(KERN_ERR
1063 "current sp %p does not match saved sp %p\n",
8533bbe9 1064 stack_addr(regs), kcb->jprobe_saved_sp);
d6be29b8 1065 printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
1da177e4 1066 show_registers(saved_regs);
d6be29b8 1067 printk(KERN_ERR "Current registers\n");
1da177e4
LT
1068 show_registers(regs);
1069 BUG();
1070 }
e7a510f9 1071 *regs = kcb->jprobe_saved_regs;
8533bbe9
MH
1072 memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp),
1073 kcb->jprobes_stack,
1074 MIN_STACK_SIZE(kcb->jprobe_saved_sp));
d217d545 1075 preempt_enable_no_resched();
1da177e4
LT
1076 return 1;
1077 }
1078 return 0;
1079}
ba8af12f 1080
c0f7ac3a
MH
1081
1082#ifdef CONFIG_OPTPROBES
1083
1084/* Insert a call instruction at address 'from', which calls address 'to'.*/
1085static void __kprobes synthesize_relcall(void *from, void *to)
1086{
1087 __synthesize_relative_insn(from, to, RELATIVECALL_OPCODE);
1088}
1089
1090/* Insert a move instruction which sets a pointer to eax/rdi (1st arg). */
1091static void __kprobes synthesize_set_arg1(kprobe_opcode_t *addr,
1092 unsigned long val)
1093{
1094#ifdef CONFIG_X86_64
1095 *addr++ = 0x48;
1096 *addr++ = 0xbf;
1097#else
1098 *addr++ = 0xb8;
1099#endif
1100 *(unsigned long *)addr = val;
1101}
1102
1103void __kprobes kprobes_optinsn_template_holder(void)
1104{
1105 asm volatile (
1106 ".global optprobe_template_entry\n"
1107 "optprobe_template_entry: \n"
1108#ifdef CONFIG_X86_64
1109 /* We don't bother saving the ss register */
1110 " pushq %rsp\n"
1111 " pushfq\n"
1112 SAVE_REGS_STRING
1113 " movq %rsp, %rsi\n"
1114 ".global optprobe_template_val\n"
1115 "optprobe_template_val: \n"
1116 ASM_NOP5
1117 ASM_NOP5
1118 ".global optprobe_template_call\n"
1119 "optprobe_template_call: \n"
1120 ASM_NOP5
1121 /* Move flags to rsp */
1122 " movq 144(%rsp), %rdx\n"
1123 " movq %rdx, 152(%rsp)\n"
1124 RESTORE_REGS_STRING
1125 /* Skip flags entry */
1126 " addq $8, %rsp\n"
1127 " popfq\n"
1128#else /* CONFIG_X86_32 */
1129 " pushf\n"
1130 SAVE_REGS_STRING
1131 " movl %esp, %edx\n"
1132 ".global optprobe_template_val\n"
1133 "optprobe_template_val: \n"
1134 ASM_NOP5
1135 ".global optprobe_template_call\n"
1136 "optprobe_template_call: \n"
1137 ASM_NOP5
1138 RESTORE_REGS_STRING
1139 " addl $4, %esp\n" /* skip cs */
1140 " popf\n"
1141#endif
1142 ".global optprobe_template_end\n"
1143 "optprobe_template_end: \n");
1144}
1145
1146#define TMPL_MOVE_IDX \
1147 ((long)&optprobe_template_val - (long)&optprobe_template_entry)
1148#define TMPL_CALL_IDX \
1149 ((long)&optprobe_template_call - (long)&optprobe_template_entry)
1150#define TMPL_END_IDX \
1151 ((long)&optprobe_template_end - (long)&optprobe_template_entry)
1152
1153#define INT3_SIZE sizeof(kprobe_opcode_t)
1154
1155/* Optimized kprobe call back function: called from optinsn */
1156static void __kprobes optimized_callback(struct optimized_kprobe *op,
1157 struct pt_regs *regs)
1158{
1159 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1160
1161 preempt_disable();
1162 if (kprobe_running()) {
1163 kprobes_inc_nmissed_count(&op->kp);
1164 } else {
1165 /* Save skipped registers */
1166#ifdef CONFIG_X86_64
1167 regs->cs = __KERNEL_CS;
1168#else
1169 regs->cs = __KERNEL_CS | get_kernel_rpl();
1170 regs->gs = 0;
1171#endif
1172 regs->ip = (unsigned long)op->kp.addr + INT3_SIZE;
1173 regs->orig_ax = ~0UL;
1174
1175 __get_cpu_var(current_kprobe) = &op->kp;
1176 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
1177 opt_pre_handler(&op->kp, regs);
1178 __get_cpu_var(current_kprobe) = NULL;
1179 }
1180 preempt_enable_no_resched();
1181}
1182
1183static int __kprobes copy_optimized_instructions(u8 *dest, u8 *src)
1184{
1185 int len = 0, ret;
1186
1187 while (len < RELATIVEJUMP_SIZE) {
1188 ret = __copy_instruction(dest + len, src + len, 1);
1189 if (!ret || !can_boost(dest + len))
1190 return -EINVAL;
1191 len += ret;
1192 }
1193 /* Check whether the address range is reserved */
1194 if (ftrace_text_reserved(src, src + len - 1) ||
1195 alternatives_text_reserved(src, src + len - 1))
1196 return -EBUSY;
1197
1198 return len;
1199}
1200
1201/* Check whether insn is indirect jump */
1202static int __kprobes insn_is_indirect_jump(struct insn *insn)
1203{
1204 return ((insn->opcode.bytes[0] == 0xff &&
1205 (X86_MODRM_REG(insn->modrm.value) & 6) == 4) || /* Jump */
1206 insn->opcode.bytes[0] == 0xea); /* Segment based jump */
1207}
1208
1209/* Check whether insn jumps into specified address range */
1210static int insn_jump_into_range(struct insn *insn, unsigned long start, int len)
1211{
1212 unsigned long target = 0;
1213
1214 switch (insn->opcode.bytes[0]) {
1215 case 0xe0: /* loopne */
1216 case 0xe1: /* loope */
1217 case 0xe2: /* loop */
1218 case 0xe3: /* jcxz */
1219 case 0xe9: /* near relative jump */
1220 case 0xeb: /* short relative jump */
1221 break;
1222 case 0x0f:
1223 if ((insn->opcode.bytes[1] & 0xf0) == 0x80) /* jcc near */
1224 break;
1225 return 0;
1226 default:
1227 if ((insn->opcode.bytes[0] & 0xf0) == 0x70) /* jcc short */
1228 break;
1229 return 0;
1230 }
1231 target = (unsigned long)insn->next_byte + insn->immediate.value;
1232
1233 return (start <= target && target <= start + len);
1234}
1235
1236/* Decode whole function to ensure any instructions don't jump into target */
1237static int __kprobes can_optimize(unsigned long paddr)
1238{
1239 int ret;
1240 unsigned long addr, size = 0, offset = 0;
1241 struct insn insn;
1242 kprobe_opcode_t buf[MAX_INSN_SIZE];
1243 /* Dummy buffers for lookup_symbol_attrs */
1244 static char __dummy_buf[KSYM_NAME_LEN];
1245
1246 /* Lookup symbol including addr */
1247 if (!kallsyms_lookup(paddr, &size, &offset, NULL, __dummy_buf))
1248 return 0;
1249
1250 /* Check there is enough space for a relative jump. */
1251 if (size - offset < RELATIVEJUMP_SIZE)
1252 return 0;
1253
1254 /* Decode instructions */
1255 addr = paddr - offset;
1256 while (addr < paddr - offset + size) { /* Decode until function end */
1257 if (search_exception_tables(addr))
1258 /*
1259 * Since some fixup code will jumps into this function,
1260 * we can't optimize kprobe in this function.
1261 */
1262 return 0;
1263 kernel_insn_init(&insn, (void *)addr);
1264 insn_get_opcode(&insn);
1265 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
1266 ret = recover_probed_instruction(buf, addr);
1267 if (ret)
1268 return 0;
1269 kernel_insn_init(&insn, buf);
1270 }
1271 insn_get_length(&insn);
1272 /* Recover address */
1273 insn.kaddr = (void *)addr;
1274 insn.next_byte = (void *)(addr + insn.length);
1275 /* Check any instructions don't jump into target */
1276 if (insn_is_indirect_jump(&insn) ||
1277 insn_jump_into_range(&insn, paddr + INT3_SIZE,
1278 RELATIVE_ADDR_SIZE))
1279 return 0;
1280 addr += insn.length;
1281 }
1282
1283 return 1;
1284}
1285
1286/* Check optimized_kprobe can actually be optimized. */
1287int __kprobes arch_check_optimized_kprobe(struct optimized_kprobe *op)
1288{
1289 int i;
1290 struct kprobe *p;
1291
1292 for (i = 1; i < op->optinsn.size; i++) {
1293 p = get_kprobe(op->kp.addr + i);
1294 if (p && !kprobe_disabled(p))
1295 return -EEXIST;
1296 }
1297
1298 return 0;
1299}
1300
1301/* Check the addr is within the optimized instructions. */
1302int __kprobes arch_within_optimized_kprobe(struct optimized_kprobe *op,
1303 unsigned long addr)
1304{
1305 return ((unsigned long)op->kp.addr <= addr &&
1306 (unsigned long)op->kp.addr + op->optinsn.size > addr);
1307}
1308
1309/* Free optimized instruction slot */
1310static __kprobes
1311void __arch_remove_optimized_kprobe(struct optimized_kprobe *op, int dirty)
1312{
1313 if (op->optinsn.insn) {
1314 free_optinsn_slot(op->optinsn.insn, dirty);
1315 op->optinsn.insn = NULL;
1316 op->optinsn.size = 0;
1317 }
1318}
1319
1320void __kprobes arch_remove_optimized_kprobe(struct optimized_kprobe *op)
1321{
1322 __arch_remove_optimized_kprobe(op, 1);
1323}
1324
1325/*
1326 * Copy replacing target instructions
1327 * Target instructions MUST be relocatable (checked inside)
1328 */
1329int __kprobes arch_prepare_optimized_kprobe(struct optimized_kprobe *op)
1330{
1331 u8 *buf;
1332 int ret;
1333 long rel;
1334
1335 if (!can_optimize((unsigned long)op->kp.addr))
1336 return -EILSEQ;
1337
1338 op->optinsn.insn = get_optinsn_slot();
1339 if (!op->optinsn.insn)
1340 return -ENOMEM;
1341
1342 /*
1343 * Verify if the address gap is in 2GB range, because this uses
1344 * a relative jump.
1345 */
1346 rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE;
1347 if (abs(rel) > 0x7fffffff)
1348 return -ERANGE;
1349
1350 buf = (u8 *)op->optinsn.insn;
1351
1352 /* Copy instructions into the out-of-line buffer */
1353 ret = copy_optimized_instructions(buf + TMPL_END_IDX, op->kp.addr);
1354 if (ret < 0) {
1355 __arch_remove_optimized_kprobe(op, 0);
1356 return ret;
1357 }
1358 op->optinsn.size = ret;
1359
1360 /* Copy arch-dep-instance from template */
1361 memcpy(buf, &optprobe_template_entry, TMPL_END_IDX);
1362
1363 /* Set probe information */
1364 synthesize_set_arg1(buf + TMPL_MOVE_IDX, (unsigned long)op);
1365
1366 /* Set probe function call */
1367 synthesize_relcall(buf + TMPL_CALL_IDX, optimized_callback);
1368
1369 /* Set returning jmp instruction at the tail of out-of-line buffer */
1370 synthesize_reljump(buf + TMPL_END_IDX + op->optinsn.size,
1371 (u8 *)op->kp.addr + op->optinsn.size);
1372
1373 flush_icache_range((unsigned long) buf,
1374 (unsigned long) buf + TMPL_END_IDX +
1375 op->optinsn.size + RELATIVEJUMP_SIZE);
1376 return 0;
1377}
1378
1379/* Replace a breakpoint (int3) with a relative jump. */
1380int __kprobes arch_optimize_kprobe(struct optimized_kprobe *op)
1381{
1382 unsigned char jmp_code[RELATIVEJUMP_SIZE];
1383 s32 rel = (s32)((long)op->optinsn.insn -
1384 ((long)op->kp.addr + RELATIVEJUMP_SIZE));
1385
1386 /* Backup instructions which will be replaced by jump address */
1387 memcpy(op->optinsn.copied_insn, op->kp.addr + INT3_SIZE,
1388 RELATIVE_ADDR_SIZE);
1389
1390 jmp_code[0] = RELATIVEJUMP_OPCODE;
1391 *(s32 *)(&jmp_code[1]) = rel;
1392
1393 /*
1394 * text_poke_smp doesn't support NMI/MCE code modifying.
1395 * However, since kprobes itself also doesn't support NMI/MCE
1396 * code probing, it's not a problem.
1397 */
1398 text_poke_smp(op->kp.addr, jmp_code, RELATIVEJUMP_SIZE);
1399 return 0;
1400}
1401
1402/* Replace a relative jump with a breakpoint (int3). */
1403void __kprobes arch_unoptimize_kprobe(struct optimized_kprobe *op)
1404{
1405 u8 buf[RELATIVEJUMP_SIZE];
1406
1407 /* Set int3 to first byte for kprobes */
1408 buf[0] = BREAKPOINT_INSTRUCTION;
1409 memcpy(buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE);
1410 text_poke_smp(op->kp.addr, buf, RELATIVEJUMP_SIZE);
1411}
1412
1413static int __kprobes setup_detour_execution(struct kprobe *p,
1414 struct pt_regs *regs,
1415 int reenter)
1416{
1417 struct optimized_kprobe *op;
1418
1419 if (p->flags & KPROBE_FLAG_OPTIMIZED) {
1420 /* This kprobe is really able to run optimized path. */
1421 op = container_of(p, struct optimized_kprobe, kp);
1422 /* Detour through copied instructions */
1423 regs->ip = (unsigned long)op->optinsn.insn + TMPL_END_IDX;
1424 if (!reenter)
1425 reset_current_kprobe();
1426 preempt_enable_no_resched();
1427 return 1;
1428 }
1429 return 0;
1430}
1431#endif
1432
6772926b 1433int __init arch_init_kprobes(void)
ba8af12f 1434{
da07ab03 1435 return 0;
ba8af12f 1436}
bf8f6e5b
AM
1437
1438int __kprobes arch_trampoline_kprobe(struct kprobe *p)
1439{
bf8f6e5b
AM
1440 return 0;
1441}
This page took 1.145867 seconds and 5 git commands to generate.