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