Merge branch 'akpm'
[linux-2.6/next.git] / arch / x86 / kernel / kprobes.c
blobf1a6244d7d93769c35d219b79de6358d8a4096e3
1 /*
2 * Kernel Probes (KProbes)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * Copyright (C) IBM Corporation, 2002, 2004
20 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
21 * Probes initial implementation ( includes contributions from
22 * Rusty Russell).
23 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
24 * interface to access function arguments.
25 * 2004-Oct Jim Keniston <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
26 * <prasanna@in.ibm.com> adapted for x86_64 from i386.
27 * 2005-Mar Roland McGrath <roland@redhat.com>
28 * Fixed to handle %rip-relative addressing mode correctly.
29 * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
30 * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
31 * <prasanna@in.ibm.com> added function-return probes.
32 * 2005-May Rusty Lynch <rusty.lynch@intel.com>
33 * Added function return probes functionality
34 * 2006-Feb Masami Hiramatsu <hiramatu@sdl.hitachi.co.jp> added
35 * kprobe-booster and kretprobe-booster for i386.
36 * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com> added kprobe-booster
37 * and kretprobe-booster for x86-64
38 * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com>, Arjan van de Ven
39 * <arjan@infradead.org> and Jim Keniston <jkenisto@us.ibm.com>
40 * unified x86 kprobes code.
43 #include <linux/kprobes.h>
44 #include <linux/ptrace.h>
45 #include <linux/string.h>
46 #include <linux/slab.h>
47 #include <linux/hardirq.h>
48 #include <linux/preempt.h>
49 #include <linux/module.h>
50 #include <linux/kdebug.h>
51 #include <linux/kallsyms.h>
52 #include <linux/ftrace.h>
54 #include <asm/cacheflush.h>
55 #include <asm/desc.h>
56 #include <asm/pgtable.h>
57 #include <asm/uaccess.h>
58 #include <asm/alternative.h>
59 #include <asm/insn.h>
60 #include <asm/debugreg.h>
62 void jprobe_return_end(void);
64 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
65 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
67 #define stack_addr(regs) ((unsigned long *)kernel_stack_pointer(regs))
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))
76 * Undefined/reserved opcodes, conditional jump, Opcode Extension
77 * Groups, and some special opcodes can not boost.
79 static 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 */
101 #undef W
103 struct kretprobe_blackpoint kretprobe_blacklist[] = {
104 {"__switch_to", }, /* This function switches only current task, but
105 doesn't switch kernel stack.*/
106 {NULL, NULL} /* Terminator */
108 const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
110 static void __kprobes __synthesize_relative_insn(void *from, void *to, u8 op)
112 struct __arch_relative_insn {
113 u8 op;
114 s32 raddr;
115 } __attribute__((packed)) *insn;
117 insn = (struct __arch_relative_insn *)from;
118 insn->raddr = (s32)((long)(to) - ((long)(from) + 5));
119 insn->op = op;
122 /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
123 static void __kprobes synthesize_reljump(void *from, void *to)
125 __synthesize_relative_insn(from, to, RELATIVEJUMP_OPCODE);
129 * Skip the prefixes of the instruction.
131 static kprobe_opcode_t *__kprobes skip_prefixes(kprobe_opcode_t *insn)
133 insn_attr_t attr;
135 attr = inat_get_opcode_attribute((insn_byte_t)*insn);
136 while (inat_is_legacy_prefix(attr)) {
137 insn++;
138 attr = inat_get_opcode_attribute((insn_byte_t)*insn);
140 #ifdef CONFIG_X86_64
141 if (inat_is_rex_prefix(attr))
142 insn++;
143 #endif
144 return insn;
148 * Returns non-zero if opcode is boostable.
149 * RIP relative instructions are adjusted at copying time in 64 bits mode
151 static int __kprobes can_boost(kprobe_opcode_t *opcodes)
153 kprobe_opcode_t opcode;
154 kprobe_opcode_t *orig_opcodes = opcodes;
156 if (search_exception_tables((unsigned long)opcodes))
157 return 0; /* Page fault may occur on this address. */
159 retry:
160 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
161 return 0;
162 opcode = *(opcodes++);
164 /* 2nd-byte opcode */
165 if (opcode == 0x0f) {
166 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
167 return 0;
168 return test_bit(*opcodes,
169 (unsigned long *)twobyte_is_boostable);
172 switch (opcode & 0xf0) {
173 #ifdef CONFIG_X86_64
174 case 0x40:
175 goto retry; /* REX prefix is boostable */
176 #endif
177 case 0x60:
178 if (0x63 < opcode && opcode < 0x67)
179 goto retry; /* prefixes */
180 /* can't boost Address-size override and bound */
181 return (opcode != 0x62 && opcode != 0x67);
182 case 0x70:
183 return 0; /* can't boost conditional jump */
184 case 0xc0:
185 /* can't boost software-interruptions */
186 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
187 case 0xd0:
188 /* can boost AA* and XLAT */
189 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
190 case 0xe0:
191 /* can boost in/out and absolute jmps */
192 return ((opcode & 0x04) || opcode == 0xea);
193 case 0xf0:
194 if ((opcode & 0x0c) == 0 && opcode != 0xf1)
195 goto retry; /* lock/rep(ne) prefix */
196 /* clear and set flags are boostable */
197 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
198 default:
199 /* segment override prefixes are boostable */
200 if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
201 goto retry; /* prefixes */
202 /* CS override prefix and call are not boostable */
203 return (opcode != 0x2e && opcode != 0x9a);
207 /* Recover the probed instruction at addr for further analysis. */
208 static int recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)
210 struct kprobe *kp;
211 kp = get_kprobe((void *)addr);
212 if (!kp)
213 return -EINVAL;
216 * Basically, kp->ainsn.insn has an original instruction.
217 * However, RIP-relative instruction can not do single-stepping
218 * at different place, __copy_instruction() tweaks the displacement of
219 * that instruction. In that case, we can't recover the instruction
220 * from the kp->ainsn.insn.
222 * On the other hand, kp->opcode has a copy of the first byte of
223 * the probed instruction, which is overwritten by int3. And
224 * the instruction at kp->addr is not modified by kprobes except
225 * for the first byte, we can recover the original instruction
226 * from it and kp->opcode.
228 memcpy(buf, kp->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
229 buf[0] = kp->opcode;
230 return 0;
233 /* Check if paddr is at an instruction boundary */
234 static int __kprobes can_probe(unsigned long paddr)
236 int ret;
237 unsigned long addr, offset = 0;
238 struct insn insn;
239 kprobe_opcode_t buf[MAX_INSN_SIZE];
241 if (!kallsyms_lookup_size_offset(paddr, NULL, &offset))
242 return 0;
244 /* Decode instructions */
245 addr = paddr - offset;
246 while (addr < paddr) {
247 kernel_insn_init(&insn, (void *)addr);
248 insn_get_opcode(&insn);
251 * Check if the instruction has been modified by another
252 * kprobe, in which case we replace the breakpoint by the
253 * original instruction in our buffer.
255 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
256 ret = recover_probed_instruction(buf, addr);
257 if (ret)
259 * Another debugging subsystem might insert
260 * this breakpoint. In that case, we can't
261 * recover it.
263 return 0;
264 kernel_insn_init(&insn, buf);
266 insn_get_length(&insn);
267 addr += insn.length;
270 return (addr == paddr);
274 * Returns non-zero if opcode modifies the interrupt flag.
276 static int __kprobes is_IF_modifier(kprobe_opcode_t *insn)
278 /* Skip prefixes */
279 insn = skip_prefixes(insn);
281 switch (*insn) {
282 case 0xfa: /* cli */
283 case 0xfb: /* sti */
284 case 0xcf: /* iret/iretd */
285 case 0x9d: /* popf/popfd */
286 return 1;
289 return 0;
293 * Copy an instruction and adjust the displacement if the instruction
294 * uses the %rip-relative addressing mode.
295 * If it does, Return the address of the 32-bit displacement word.
296 * If not, return null.
297 * Only applicable to 64-bit x86.
299 static int __kprobes __copy_instruction(u8 *dest, u8 *src, int recover)
301 struct insn insn;
302 int ret;
303 kprobe_opcode_t buf[MAX_INSN_SIZE];
305 kernel_insn_init(&insn, src);
306 if (recover) {
307 insn_get_opcode(&insn);
308 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
309 ret = recover_probed_instruction(buf,
310 (unsigned long)src);
311 if (ret)
312 return 0;
313 kernel_insn_init(&insn, buf);
316 insn_get_length(&insn);
317 memcpy(dest, insn.kaddr, insn.length);
319 #ifdef CONFIG_X86_64
320 if (insn_rip_relative(&insn)) {
321 s64 newdisp;
322 u8 *disp;
323 kernel_insn_init(&insn, dest);
324 insn_get_displacement(&insn);
326 * The copied instruction uses the %rip-relative addressing
327 * mode. Adjust the displacement for the difference between
328 * the original location of this instruction and the location
329 * of the copy that will actually be run. The tricky bit here
330 * is making sure that the sign extension happens correctly in
331 * this calculation, since we need a signed 32-bit result to
332 * be sign-extended to 64 bits when it's added to the %rip
333 * value and yield the same 64-bit result that the sign-
334 * extension of the original signed 32-bit displacement would
335 * have given.
337 newdisp = (u8 *) src + (s64) insn.displacement.value -
338 (u8 *) dest;
339 BUG_ON((s64) (s32) newdisp != newdisp); /* Sanity check. */
340 disp = (u8 *) dest + insn_offset_displacement(&insn);
341 *(s32 *) disp = (s32) newdisp;
343 #endif
344 return insn.length;
347 static void __kprobes arch_copy_kprobe(struct kprobe *p)
350 * Copy an instruction without recovering int3, because it will be
351 * put by another subsystem.
353 __copy_instruction(p->ainsn.insn, p->addr, 0);
355 if (can_boost(p->addr))
356 p->ainsn.boostable = 0;
357 else
358 p->ainsn.boostable = -1;
360 p->opcode = *p->addr;
363 int __kprobes arch_prepare_kprobe(struct kprobe *p)
365 if (alternatives_text_reserved(p->addr, p->addr))
366 return -EINVAL;
368 if (!can_probe((unsigned long)p->addr))
369 return -EILSEQ;
370 /* insn: must be on special executable page on x86. */
371 p->ainsn.insn = get_insn_slot();
372 if (!p->ainsn.insn)
373 return -ENOMEM;
374 arch_copy_kprobe(p);
375 return 0;
378 void __kprobes arch_arm_kprobe(struct kprobe *p)
380 text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
383 void __kprobes arch_disarm_kprobe(struct kprobe *p)
385 text_poke(p->addr, &p->opcode, 1);
388 void __kprobes arch_remove_kprobe(struct kprobe *p)
390 if (p->ainsn.insn) {
391 free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
392 p->ainsn.insn = NULL;
396 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
398 kcb->prev_kprobe.kp = kprobe_running();
399 kcb->prev_kprobe.status = kcb->kprobe_status;
400 kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
401 kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
404 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
406 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
407 kcb->kprobe_status = kcb->prev_kprobe.status;
408 kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
409 kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
412 static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
413 struct kprobe_ctlblk *kcb)
415 __this_cpu_write(current_kprobe, p);
416 kcb->kprobe_saved_flags = kcb->kprobe_old_flags
417 = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
418 if (is_IF_modifier(p->ainsn.insn))
419 kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
422 static void __kprobes clear_btf(void)
424 if (test_thread_flag(TIF_BLOCKSTEP)) {
425 unsigned long debugctl = get_debugctlmsr();
427 debugctl &= ~DEBUGCTLMSR_BTF;
428 update_debugctlmsr(debugctl);
432 static void __kprobes restore_btf(void)
434 if (test_thread_flag(TIF_BLOCKSTEP)) {
435 unsigned long debugctl = get_debugctlmsr();
437 debugctl |= DEBUGCTLMSR_BTF;
438 update_debugctlmsr(debugctl);
442 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
443 struct pt_regs *regs)
445 unsigned long *sara = stack_addr(regs);
447 ri->ret_addr = (kprobe_opcode_t *) *sara;
449 /* Replace the return addr with trampoline addr */
450 *sara = (unsigned long) &kretprobe_trampoline;
453 #ifdef CONFIG_OPTPROBES
454 static int __kprobes setup_detour_execution(struct kprobe *p,
455 struct pt_regs *regs,
456 int reenter);
457 #else
458 #define setup_detour_execution(p, regs, reenter) (0)
459 #endif
461 static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs,
462 struct kprobe_ctlblk *kcb, int reenter)
464 if (setup_detour_execution(p, regs, reenter))
465 return;
467 #if !defined(CONFIG_PREEMPT)
468 if (p->ainsn.boostable == 1 && !p->post_handler) {
469 /* Boost up -- we can execute copied instructions directly */
470 if (!reenter)
471 reset_current_kprobe();
473 * Reentering boosted probe doesn't reset current_kprobe,
474 * nor set current_kprobe, because it doesn't use single
475 * stepping.
477 regs->ip = (unsigned long)p->ainsn.insn;
478 preempt_enable_no_resched();
479 return;
481 #endif
482 if (reenter) {
483 save_previous_kprobe(kcb);
484 set_current_kprobe(p, regs, kcb);
485 kcb->kprobe_status = KPROBE_REENTER;
486 } else
487 kcb->kprobe_status = KPROBE_HIT_SS;
488 /* Prepare real single stepping */
489 clear_btf();
490 regs->flags |= X86_EFLAGS_TF;
491 regs->flags &= ~X86_EFLAGS_IF;
492 /* single step inline if the instruction is an int3 */
493 if (p->opcode == BREAKPOINT_INSTRUCTION)
494 regs->ip = (unsigned long)p->addr;
495 else
496 regs->ip = (unsigned long)p->ainsn.insn;
500 * We have reentered the kprobe_handler(), since another probe was hit while
501 * within the handler. We save the original kprobes variables and just single
502 * step on the instruction of the new probe without calling any user handlers.
504 static int __kprobes reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
505 struct kprobe_ctlblk *kcb)
507 switch (kcb->kprobe_status) {
508 case KPROBE_HIT_SSDONE:
509 case KPROBE_HIT_ACTIVE:
510 kprobes_inc_nmissed_count(p);
511 setup_singlestep(p, regs, kcb, 1);
512 break;
513 case KPROBE_HIT_SS:
514 /* A probe has been hit in the codepath leading up to, or just
515 * after, single-stepping of a probed instruction. This entire
516 * codepath should strictly reside in .kprobes.text section.
517 * Raise a BUG or we'll continue in an endless reentering loop
518 * and eventually a stack overflow.
520 printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n",
521 p->addr);
522 dump_kprobe(p);
523 BUG();
524 default:
525 /* impossible cases */
526 WARN_ON(1);
527 return 0;
530 return 1;
534 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
535 * remain disabled throughout this function.
537 static int __kprobes kprobe_handler(struct pt_regs *regs)
539 kprobe_opcode_t *addr;
540 struct kprobe *p;
541 struct kprobe_ctlblk *kcb;
543 addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
545 * We don't want to be preempted for the entire
546 * duration of kprobe processing. We conditionally
547 * re-enable preemption at the end of this function,
548 * and also in reenter_kprobe() and setup_singlestep().
550 preempt_disable();
552 kcb = get_kprobe_ctlblk();
553 p = get_kprobe(addr);
555 if (p) {
556 if (kprobe_running()) {
557 if (reenter_kprobe(p, regs, kcb))
558 return 1;
559 } else {
560 set_current_kprobe(p, regs, kcb);
561 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
564 * If we have no pre-handler or it returned 0, we
565 * continue with normal processing. If we have a
566 * pre-handler and it returned non-zero, it prepped
567 * for calling the break_handler below on re-entry
568 * for jprobe processing, so get out doing nothing
569 * more here.
571 if (!p->pre_handler || !p->pre_handler(p, regs))
572 setup_singlestep(p, regs, kcb, 0);
573 return 1;
575 } else if (*addr != BREAKPOINT_INSTRUCTION) {
577 * The breakpoint instruction was removed right
578 * after we hit it. Another cpu has removed
579 * either a probepoint or a debugger breakpoint
580 * at this address. In either case, no further
581 * handling of this interrupt is appropriate.
582 * Back up over the (now missing) int3 and run
583 * the original instruction.
585 regs->ip = (unsigned long)addr;
586 preempt_enable_no_resched();
587 return 1;
588 } else if (kprobe_running()) {
589 p = __this_cpu_read(current_kprobe);
590 if (p->break_handler && p->break_handler(p, regs)) {
591 setup_singlestep(p, regs, kcb, 0);
592 return 1;
594 } /* else: not a kprobe fault; let the kernel handle it */
596 preempt_enable_no_resched();
597 return 0;
600 #ifdef CONFIG_X86_64
601 #define SAVE_REGS_STRING \
602 /* Skip cs, ip, orig_ax. */ \
603 " subq $24, %rsp\n" \
604 " pushq %rdi\n" \
605 " pushq %rsi\n" \
606 " pushq %rdx\n" \
607 " pushq %rcx\n" \
608 " pushq %rax\n" \
609 " pushq %r8\n" \
610 " pushq %r9\n" \
611 " pushq %r10\n" \
612 " pushq %r11\n" \
613 " pushq %rbx\n" \
614 " pushq %rbp\n" \
615 " pushq %r12\n" \
616 " pushq %r13\n" \
617 " pushq %r14\n" \
618 " pushq %r15\n"
619 #define RESTORE_REGS_STRING \
620 " popq %r15\n" \
621 " popq %r14\n" \
622 " popq %r13\n" \
623 " popq %r12\n" \
624 " popq %rbp\n" \
625 " popq %rbx\n" \
626 " popq %r11\n" \
627 " popq %r10\n" \
628 " popq %r9\n" \
629 " popq %r8\n" \
630 " popq %rax\n" \
631 " popq %rcx\n" \
632 " popq %rdx\n" \
633 " popq %rsi\n" \
634 " popq %rdi\n" \
635 /* Skip orig_ax, ip, cs */ \
636 " addq $24, %rsp\n"
637 #else
638 #define SAVE_REGS_STRING \
639 /* Skip cs, ip, orig_ax and gs. */ \
640 " subl $16, %esp\n" \
641 " pushl %fs\n" \
642 " pushl %es\n" \
643 " pushl %ds\n" \
644 " pushl %eax\n" \
645 " pushl %ebp\n" \
646 " pushl %edi\n" \
647 " pushl %esi\n" \
648 " pushl %edx\n" \
649 " pushl %ecx\n" \
650 " pushl %ebx\n"
651 #define RESTORE_REGS_STRING \
652 " popl %ebx\n" \
653 " popl %ecx\n" \
654 " popl %edx\n" \
655 " popl %esi\n" \
656 " popl %edi\n" \
657 " popl %ebp\n" \
658 " popl %eax\n" \
659 /* Skip ds, es, fs, gs, orig_ax, and ip. Note: don't pop cs here*/\
660 " addl $24, %esp\n"
661 #endif
664 * When a retprobed function returns, this code saves registers and
665 * calls trampoline_handler() runs, which calls the kretprobe's handler.
667 static void __used __kprobes kretprobe_trampoline_holder(void)
669 asm volatile (
670 ".global kretprobe_trampoline\n"
671 "kretprobe_trampoline: \n"
672 #ifdef CONFIG_X86_64
673 /* We don't bother saving the ss register */
674 " pushq %rsp\n"
675 " pushfq\n"
676 SAVE_REGS_STRING
677 " movq %rsp, %rdi\n"
678 " call trampoline_handler\n"
679 /* Replace saved sp with true return address. */
680 " movq %rax, 152(%rsp)\n"
681 RESTORE_REGS_STRING
682 " popfq\n"
683 #else
684 " pushf\n"
685 SAVE_REGS_STRING
686 " movl %esp, %eax\n"
687 " call trampoline_handler\n"
688 /* Move flags to cs */
689 " movl 56(%esp), %edx\n"
690 " movl %edx, 52(%esp)\n"
691 /* Replace saved flags with true return address. */
692 " movl %eax, 56(%esp)\n"
693 RESTORE_REGS_STRING
694 " popf\n"
695 #endif
696 " ret\n");
700 * Called from kretprobe_trampoline
702 static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
704 struct kretprobe_instance *ri = NULL;
705 struct hlist_head *head, empty_rp;
706 struct hlist_node *node, *tmp;
707 unsigned long flags, orig_ret_address = 0;
708 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
709 kprobe_opcode_t *correct_ret_addr = NULL;
711 INIT_HLIST_HEAD(&empty_rp);
712 kretprobe_hash_lock(current, &head, &flags);
713 /* fixup registers */
714 #ifdef CONFIG_X86_64
715 regs->cs = __KERNEL_CS;
716 #else
717 regs->cs = __KERNEL_CS | get_kernel_rpl();
718 regs->gs = 0;
719 #endif
720 regs->ip = trampoline_address;
721 regs->orig_ax = ~0UL;
724 * It is possible to have multiple instances associated with a given
725 * task either because multiple functions in the call path have
726 * return probes installed on them, and/or more than one
727 * return probe was registered for a target function.
729 * We can handle this because:
730 * - instances are always pushed into the head of the list
731 * - when multiple return probes are registered for the same
732 * function, the (chronologically) first instance's ret_addr
733 * will be the real return address, and all the rest will
734 * point to kretprobe_trampoline.
736 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
737 if (ri->task != current)
738 /* another task is sharing our hash bucket */
739 continue;
741 orig_ret_address = (unsigned long)ri->ret_addr;
743 if (orig_ret_address != trampoline_address)
745 * This is the real return address. Any other
746 * instances associated with this task are for
747 * other calls deeper on the call stack
749 break;
752 kretprobe_assert(ri, orig_ret_address, trampoline_address);
754 correct_ret_addr = ri->ret_addr;
755 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
756 if (ri->task != current)
757 /* another task is sharing our hash bucket */
758 continue;
760 orig_ret_address = (unsigned long)ri->ret_addr;
761 if (ri->rp && ri->rp->handler) {
762 __this_cpu_write(current_kprobe, &ri->rp->kp);
763 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
764 ri->ret_addr = correct_ret_addr;
765 ri->rp->handler(ri, regs);
766 __this_cpu_write(current_kprobe, NULL);
769 recycle_rp_inst(ri, &empty_rp);
771 if (orig_ret_address != trampoline_address)
773 * This is the real return address. Any other
774 * instances associated with this task are for
775 * other calls deeper on the call stack
777 break;
780 kretprobe_hash_unlock(current, &flags);
782 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
783 hlist_del(&ri->hlist);
784 kfree(ri);
786 return (void *)orig_ret_address;
790 * Called after single-stepping. p->addr is the address of the
791 * instruction whose first byte has been replaced by the "int 3"
792 * instruction. To avoid the SMP problems that can occur when we
793 * temporarily put back the original opcode to single-step, we
794 * single-stepped a copy of the instruction. The address of this
795 * copy is p->ainsn.insn.
797 * This function prepares to return from the post-single-step
798 * interrupt. We have to fix up the stack as follows:
800 * 0) Except in the case of absolute or indirect jump or call instructions,
801 * the new ip is relative to the copied instruction. We need to make
802 * it relative to the original instruction.
804 * 1) If the single-stepped instruction was pushfl, then the TF and IF
805 * flags are set in the just-pushed flags, and may need to be cleared.
807 * 2) If the single-stepped instruction was a call, the return address
808 * that is atop the stack is the address following the copied instruction.
809 * We need to make it the address following the original instruction.
811 * If this is the first time we've single-stepped the instruction at
812 * this probepoint, and the instruction is boostable, boost it: add a
813 * jump instruction after the copied instruction, that jumps to the next
814 * instruction after the probepoint.
816 static void __kprobes resume_execution(struct kprobe *p,
817 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
819 unsigned long *tos = stack_addr(regs);
820 unsigned long copy_ip = (unsigned long)p->ainsn.insn;
821 unsigned long orig_ip = (unsigned long)p->addr;
822 kprobe_opcode_t *insn = p->ainsn.insn;
824 /* Skip prefixes */
825 insn = skip_prefixes(insn);
827 regs->flags &= ~X86_EFLAGS_TF;
828 switch (*insn) {
829 case 0x9c: /* pushfl */
830 *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
831 *tos |= kcb->kprobe_old_flags;
832 break;
833 case 0xc2: /* iret/ret/lret */
834 case 0xc3:
835 case 0xca:
836 case 0xcb:
837 case 0xcf:
838 case 0xea: /* jmp absolute -- ip is correct */
839 /* ip is already adjusted, no more changes required */
840 p->ainsn.boostable = 1;
841 goto no_change;
842 case 0xe8: /* call relative - Fix return addr */
843 *tos = orig_ip + (*tos - copy_ip);
844 break;
845 #ifdef CONFIG_X86_32
846 case 0x9a: /* call absolute -- same as call absolute, indirect */
847 *tos = orig_ip + (*tos - copy_ip);
848 goto no_change;
849 #endif
850 case 0xff:
851 if ((insn[1] & 0x30) == 0x10) {
853 * call absolute, indirect
854 * Fix return addr; ip is correct.
855 * But this is not boostable
857 *tos = orig_ip + (*tos - copy_ip);
858 goto no_change;
859 } else if (((insn[1] & 0x31) == 0x20) ||
860 ((insn[1] & 0x31) == 0x21)) {
862 * jmp near and far, absolute indirect
863 * ip is correct. And this is boostable
865 p->ainsn.boostable = 1;
866 goto no_change;
868 default:
869 break;
872 if (p->ainsn.boostable == 0) {
873 if ((regs->ip > copy_ip) &&
874 (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
876 * These instructions can be executed directly if it
877 * jumps back to correct address.
879 synthesize_reljump((void *)regs->ip,
880 (void *)orig_ip + (regs->ip - copy_ip));
881 p->ainsn.boostable = 1;
882 } else {
883 p->ainsn.boostable = -1;
887 regs->ip += orig_ip - copy_ip;
889 no_change:
890 restore_btf();
894 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
895 * remain disabled throughout this function.
897 static int __kprobes post_kprobe_handler(struct pt_regs *regs)
899 struct kprobe *cur = kprobe_running();
900 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
902 if (!cur)
903 return 0;
905 resume_execution(cur, regs, kcb);
906 regs->flags |= kcb->kprobe_saved_flags;
908 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
909 kcb->kprobe_status = KPROBE_HIT_SSDONE;
910 cur->post_handler(cur, regs, 0);
913 /* Restore back the original saved kprobes variables and continue. */
914 if (kcb->kprobe_status == KPROBE_REENTER) {
915 restore_previous_kprobe(kcb);
916 goto out;
918 reset_current_kprobe();
919 out:
920 preempt_enable_no_resched();
923 * if somebody else is singlestepping across a probe point, flags
924 * will have TF set, in which case, continue the remaining processing
925 * of do_debug, as if this is not a probe hit.
927 if (regs->flags & X86_EFLAGS_TF)
928 return 0;
930 return 1;
933 int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
935 struct kprobe *cur = kprobe_running();
936 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
938 switch (kcb->kprobe_status) {
939 case KPROBE_HIT_SS:
940 case KPROBE_REENTER:
942 * We are here because the instruction being single
943 * stepped caused a page fault. We reset the current
944 * kprobe and the ip points back to the probe address
945 * and allow the page fault handler to continue as a
946 * normal page fault.
948 regs->ip = (unsigned long)cur->addr;
949 regs->flags |= kcb->kprobe_old_flags;
950 if (kcb->kprobe_status == KPROBE_REENTER)
951 restore_previous_kprobe(kcb);
952 else
953 reset_current_kprobe();
954 preempt_enable_no_resched();
955 break;
956 case KPROBE_HIT_ACTIVE:
957 case KPROBE_HIT_SSDONE:
959 * We increment the nmissed count for accounting,
960 * we can also use npre/npostfault count for accounting
961 * these specific fault cases.
963 kprobes_inc_nmissed_count(cur);
966 * We come here because instructions in the pre/post
967 * handler caused the page_fault, this could happen
968 * if handler tries to access user space by
969 * copy_from_user(), get_user() etc. Let the
970 * user-specified handler try to fix it first.
972 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
973 return 1;
976 * In case the user-specified fault handler returned
977 * zero, try to fix up.
979 if (fixup_exception(regs))
980 return 1;
983 * fixup routine could not handle it,
984 * Let do_page_fault() fix it.
986 break;
987 default:
988 break;
990 return 0;
994 * Wrapper routine for handling exceptions.
996 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
997 unsigned long val, void *data)
999 struct die_args *args = data;
1000 int ret = NOTIFY_DONE;
1002 if (args->regs && user_mode_vm(args->regs))
1003 return ret;
1005 switch (val) {
1006 case DIE_INT3:
1007 if (kprobe_handler(args->regs))
1008 ret = NOTIFY_STOP;
1009 break;
1010 case DIE_DEBUG:
1011 if (post_kprobe_handler(args->regs)) {
1013 * Reset the BS bit in dr6 (pointed by args->err) to
1014 * denote completion of processing
1016 (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
1017 ret = NOTIFY_STOP;
1019 break;
1020 case DIE_GPF:
1022 * To be potentially processing a kprobe fault and to
1023 * trust the result from kprobe_running(), we have
1024 * be non-preemptible.
1026 if (!preemptible() && kprobe_running() &&
1027 kprobe_fault_handler(args->regs, args->trapnr))
1028 ret = NOTIFY_STOP;
1029 break;
1030 default:
1031 break;
1033 return ret;
1036 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
1038 struct jprobe *jp = container_of(p, struct jprobe, kp);
1039 unsigned long addr;
1040 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1042 kcb->jprobe_saved_regs = *regs;
1043 kcb->jprobe_saved_sp = stack_addr(regs);
1044 addr = (unsigned long)(kcb->jprobe_saved_sp);
1047 * As Linus pointed out, gcc assumes that the callee
1048 * owns the argument space and could overwrite it, e.g.
1049 * tailcall optimization. So, to be absolutely safe
1050 * we also save and restore enough stack bytes to cover
1051 * the argument area.
1053 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
1054 MIN_STACK_SIZE(addr));
1055 regs->flags &= ~X86_EFLAGS_IF;
1056 trace_hardirqs_off();
1057 regs->ip = (unsigned long)(jp->entry);
1058 return 1;
1061 void __kprobes jprobe_return(void)
1063 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1065 asm volatile (
1066 #ifdef CONFIG_X86_64
1067 " xchg %%rbx,%%rsp \n"
1068 #else
1069 " xchgl %%ebx,%%esp \n"
1070 #endif
1071 " int3 \n"
1072 " .globl jprobe_return_end\n"
1073 " jprobe_return_end: \n"
1074 " nop \n"::"b"
1075 (kcb->jprobe_saved_sp):"memory");
1078 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
1080 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1081 u8 *addr = (u8 *) (regs->ip - 1);
1082 struct jprobe *jp = container_of(p, struct jprobe, kp);
1084 if ((addr > (u8 *) jprobe_return) &&
1085 (addr < (u8 *) jprobe_return_end)) {
1086 if (stack_addr(regs) != kcb->jprobe_saved_sp) {
1087 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
1088 printk(KERN_ERR
1089 "current sp %p does not match saved sp %p\n",
1090 stack_addr(regs), kcb->jprobe_saved_sp);
1091 printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
1092 show_registers(saved_regs);
1093 printk(KERN_ERR "Current registers\n");
1094 show_registers(regs);
1095 BUG();
1097 *regs = kcb->jprobe_saved_regs;
1098 memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp),
1099 kcb->jprobes_stack,
1100 MIN_STACK_SIZE(kcb->jprobe_saved_sp));
1101 preempt_enable_no_resched();
1102 return 1;
1104 return 0;
1108 #ifdef CONFIG_OPTPROBES
1110 /* Insert a call instruction at address 'from', which calls address 'to'.*/
1111 static void __kprobes synthesize_relcall(void *from, void *to)
1113 __synthesize_relative_insn(from, to, RELATIVECALL_OPCODE);
1116 /* Insert a move instruction which sets a pointer to eax/rdi (1st arg). */
1117 static void __kprobes synthesize_set_arg1(kprobe_opcode_t *addr,
1118 unsigned long val)
1120 #ifdef CONFIG_X86_64
1121 *addr++ = 0x48;
1122 *addr++ = 0xbf;
1123 #else
1124 *addr++ = 0xb8;
1125 #endif
1126 *(unsigned long *)addr = val;
1129 static void __used __kprobes kprobes_optinsn_template_holder(void)
1131 asm volatile (
1132 ".global optprobe_template_entry\n"
1133 "optprobe_template_entry: \n"
1134 #ifdef CONFIG_X86_64
1135 /* We don't bother saving the ss register */
1136 " pushq %rsp\n"
1137 " pushfq\n"
1138 SAVE_REGS_STRING
1139 " movq %rsp, %rsi\n"
1140 ".global optprobe_template_val\n"
1141 "optprobe_template_val: \n"
1142 ASM_NOP5
1143 ASM_NOP5
1144 ".global optprobe_template_call\n"
1145 "optprobe_template_call: \n"
1146 ASM_NOP5
1147 /* Move flags to rsp */
1148 " movq 144(%rsp), %rdx\n"
1149 " movq %rdx, 152(%rsp)\n"
1150 RESTORE_REGS_STRING
1151 /* Skip flags entry */
1152 " addq $8, %rsp\n"
1153 " popfq\n"
1154 #else /* CONFIG_X86_32 */
1155 " pushf\n"
1156 SAVE_REGS_STRING
1157 " movl %esp, %edx\n"
1158 ".global optprobe_template_val\n"
1159 "optprobe_template_val: \n"
1160 ASM_NOP5
1161 ".global optprobe_template_call\n"
1162 "optprobe_template_call: \n"
1163 ASM_NOP5
1164 RESTORE_REGS_STRING
1165 " addl $4, %esp\n" /* skip cs */
1166 " popf\n"
1167 #endif
1168 ".global optprobe_template_end\n"
1169 "optprobe_template_end: \n");
1172 #define TMPL_MOVE_IDX \
1173 ((long)&optprobe_template_val - (long)&optprobe_template_entry)
1174 #define TMPL_CALL_IDX \
1175 ((long)&optprobe_template_call - (long)&optprobe_template_entry)
1176 #define TMPL_END_IDX \
1177 ((long)&optprobe_template_end - (long)&optprobe_template_entry)
1179 #define INT3_SIZE sizeof(kprobe_opcode_t)
1181 /* Optimized kprobe call back function: called from optinsn */
1182 static void __kprobes optimized_callback(struct optimized_kprobe *op,
1183 struct pt_regs *regs)
1185 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1186 unsigned long flags;
1188 /* This is possible if op is under delayed unoptimizing */
1189 if (kprobe_disabled(&op->kp))
1190 return;
1192 local_irq_save(flags);
1193 if (kprobe_running()) {
1194 kprobes_inc_nmissed_count(&op->kp);
1195 } else {
1196 /* Save skipped registers */
1197 #ifdef CONFIG_X86_64
1198 regs->cs = __KERNEL_CS;
1199 #else
1200 regs->cs = __KERNEL_CS | get_kernel_rpl();
1201 regs->gs = 0;
1202 #endif
1203 regs->ip = (unsigned long)op->kp.addr + INT3_SIZE;
1204 regs->orig_ax = ~0UL;
1206 __this_cpu_write(current_kprobe, &op->kp);
1207 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
1208 opt_pre_handler(&op->kp, regs);
1209 __this_cpu_write(current_kprobe, NULL);
1211 local_irq_restore(flags);
1214 static int __kprobes copy_optimized_instructions(u8 *dest, u8 *src)
1216 int len = 0, ret;
1218 while (len < RELATIVEJUMP_SIZE) {
1219 ret = __copy_instruction(dest + len, src + len, 1);
1220 if (!ret || !can_boost(dest + len))
1221 return -EINVAL;
1222 len += ret;
1224 /* Check whether the address range is reserved */
1225 if (ftrace_text_reserved(src, src + len - 1) ||
1226 alternatives_text_reserved(src, src + len - 1) ||
1227 jump_label_text_reserved(src, src + len - 1))
1228 return -EBUSY;
1230 return len;
1233 /* Check whether insn is indirect jump */
1234 static int __kprobes insn_is_indirect_jump(struct insn *insn)
1236 return ((insn->opcode.bytes[0] == 0xff &&
1237 (X86_MODRM_REG(insn->modrm.value) & 6) == 4) || /* Jump */
1238 insn->opcode.bytes[0] == 0xea); /* Segment based jump */
1241 /* Check whether insn jumps into specified address range */
1242 static int insn_jump_into_range(struct insn *insn, unsigned long start, int len)
1244 unsigned long target = 0;
1246 switch (insn->opcode.bytes[0]) {
1247 case 0xe0: /* loopne */
1248 case 0xe1: /* loope */
1249 case 0xe2: /* loop */
1250 case 0xe3: /* jcxz */
1251 case 0xe9: /* near relative jump */
1252 case 0xeb: /* short relative jump */
1253 break;
1254 case 0x0f:
1255 if ((insn->opcode.bytes[1] & 0xf0) == 0x80) /* jcc near */
1256 break;
1257 return 0;
1258 default:
1259 if ((insn->opcode.bytes[0] & 0xf0) == 0x70) /* jcc short */
1260 break;
1261 return 0;
1263 target = (unsigned long)insn->next_byte + insn->immediate.value;
1265 return (start <= target && target <= start + len);
1268 /* Decode whole function to ensure any instructions don't jump into target */
1269 static int __kprobes can_optimize(unsigned long paddr)
1271 int ret;
1272 unsigned long addr, size = 0, offset = 0;
1273 struct insn insn;
1274 kprobe_opcode_t buf[MAX_INSN_SIZE];
1276 /* Lookup symbol including addr */
1277 if (!kallsyms_lookup_size_offset(paddr, &size, &offset))
1278 return 0;
1281 * Do not optimize in the entry code due to the unstable
1282 * stack handling.
1284 if ((paddr >= (unsigned long )__entry_text_start) &&
1285 (paddr < (unsigned long )__entry_text_end))
1286 return 0;
1288 /* Check there is enough space for a relative jump. */
1289 if (size - offset < RELATIVEJUMP_SIZE)
1290 return 0;
1292 /* Decode instructions */
1293 addr = paddr - offset;
1294 while (addr < paddr - offset + size) { /* Decode until function end */
1295 if (search_exception_tables(addr))
1297 * Since some fixup code will jumps into this function,
1298 * we can't optimize kprobe in this function.
1300 return 0;
1301 kernel_insn_init(&insn, (void *)addr);
1302 insn_get_opcode(&insn);
1303 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
1304 ret = recover_probed_instruction(buf, addr);
1305 if (ret)
1306 return 0;
1307 kernel_insn_init(&insn, buf);
1309 insn_get_length(&insn);
1310 /* Recover address */
1311 insn.kaddr = (void *)addr;
1312 insn.next_byte = (void *)(addr + insn.length);
1313 /* Check any instructions don't jump into target */
1314 if (insn_is_indirect_jump(&insn) ||
1315 insn_jump_into_range(&insn, paddr + INT3_SIZE,
1316 RELATIVE_ADDR_SIZE))
1317 return 0;
1318 addr += insn.length;
1321 return 1;
1324 /* Check optimized_kprobe can actually be optimized. */
1325 int __kprobes arch_check_optimized_kprobe(struct optimized_kprobe *op)
1327 int i;
1328 struct kprobe *p;
1330 for (i = 1; i < op->optinsn.size; i++) {
1331 p = get_kprobe(op->kp.addr + i);
1332 if (p && !kprobe_disabled(p))
1333 return -EEXIST;
1336 return 0;
1339 /* Check the addr is within the optimized instructions. */
1340 int __kprobes arch_within_optimized_kprobe(struct optimized_kprobe *op,
1341 unsigned long addr)
1343 return ((unsigned long)op->kp.addr <= addr &&
1344 (unsigned long)op->kp.addr + op->optinsn.size > addr);
1347 /* Free optimized instruction slot */
1348 static __kprobes
1349 void __arch_remove_optimized_kprobe(struct optimized_kprobe *op, int dirty)
1351 if (op->optinsn.insn) {
1352 free_optinsn_slot(op->optinsn.insn, dirty);
1353 op->optinsn.insn = NULL;
1354 op->optinsn.size = 0;
1358 void __kprobes arch_remove_optimized_kprobe(struct optimized_kprobe *op)
1360 __arch_remove_optimized_kprobe(op, 1);
1364 * Copy replacing target instructions
1365 * Target instructions MUST be relocatable (checked inside)
1367 int __kprobes arch_prepare_optimized_kprobe(struct optimized_kprobe *op)
1369 u8 *buf;
1370 int ret;
1371 long rel;
1373 if (!can_optimize((unsigned long)op->kp.addr))
1374 return -EILSEQ;
1376 op->optinsn.insn = get_optinsn_slot();
1377 if (!op->optinsn.insn)
1378 return -ENOMEM;
1381 * Verify if the address gap is in 2GB range, because this uses
1382 * a relative jump.
1384 rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE;
1385 if (abs(rel) > 0x7fffffff)
1386 return -ERANGE;
1388 buf = (u8 *)op->optinsn.insn;
1390 /* Copy instructions into the out-of-line buffer */
1391 ret = copy_optimized_instructions(buf + TMPL_END_IDX, op->kp.addr);
1392 if (ret < 0) {
1393 __arch_remove_optimized_kprobe(op, 0);
1394 return ret;
1396 op->optinsn.size = ret;
1398 /* Copy arch-dep-instance from template */
1399 memcpy(buf, &optprobe_template_entry, TMPL_END_IDX);
1401 /* Set probe information */
1402 synthesize_set_arg1(buf + TMPL_MOVE_IDX, (unsigned long)op);
1404 /* Set probe function call */
1405 synthesize_relcall(buf + TMPL_CALL_IDX, optimized_callback);
1407 /* Set returning jmp instruction at the tail of out-of-line buffer */
1408 synthesize_reljump(buf + TMPL_END_IDX + op->optinsn.size,
1409 (u8 *)op->kp.addr + op->optinsn.size);
1411 flush_icache_range((unsigned long) buf,
1412 (unsigned long) buf + TMPL_END_IDX +
1413 op->optinsn.size + RELATIVEJUMP_SIZE);
1414 return 0;
1417 #define MAX_OPTIMIZE_PROBES 256
1418 static struct text_poke_param *jump_poke_params;
1419 static struct jump_poke_buffer {
1420 u8 buf[RELATIVEJUMP_SIZE];
1421 } *jump_poke_bufs;
1423 static void __kprobes setup_optimize_kprobe(struct text_poke_param *tprm,
1424 u8 *insn_buf,
1425 struct optimized_kprobe *op)
1427 s32 rel = (s32)((long)op->optinsn.insn -
1428 ((long)op->kp.addr + RELATIVEJUMP_SIZE));
1430 /* Backup instructions which will be replaced by jump address */
1431 memcpy(op->optinsn.copied_insn, op->kp.addr + INT3_SIZE,
1432 RELATIVE_ADDR_SIZE);
1434 insn_buf[0] = RELATIVEJUMP_OPCODE;
1435 *(s32 *)(&insn_buf[1]) = rel;
1437 tprm->addr = op->kp.addr;
1438 tprm->opcode = insn_buf;
1439 tprm->len = RELATIVEJUMP_SIZE;
1443 * Replace breakpoints (int3) with relative jumps.
1444 * Caller must call with locking kprobe_mutex and text_mutex.
1446 void __kprobes arch_optimize_kprobes(struct list_head *oplist)
1448 struct optimized_kprobe *op, *tmp;
1449 int c = 0;
1451 list_for_each_entry_safe(op, tmp, oplist, list) {
1452 WARN_ON(kprobe_disabled(&op->kp));
1453 /* Setup param */
1454 setup_optimize_kprobe(&jump_poke_params[c],
1455 jump_poke_bufs[c].buf, op);
1456 list_del_init(&op->list);
1457 if (++c >= MAX_OPTIMIZE_PROBES)
1458 break;
1462 * text_poke_smp doesn't support NMI/MCE code modifying.
1463 * However, since kprobes itself also doesn't support NMI/MCE
1464 * code probing, it's not a problem.
1466 text_poke_smp_batch(jump_poke_params, c);
1469 static void __kprobes setup_unoptimize_kprobe(struct text_poke_param *tprm,
1470 u8 *insn_buf,
1471 struct optimized_kprobe *op)
1473 /* Set int3 to first byte for kprobes */
1474 insn_buf[0] = BREAKPOINT_INSTRUCTION;
1475 memcpy(insn_buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE);
1477 tprm->addr = op->kp.addr;
1478 tprm->opcode = insn_buf;
1479 tprm->len = RELATIVEJUMP_SIZE;
1483 * Recover original instructions and breakpoints from relative jumps.
1484 * Caller must call with locking kprobe_mutex.
1486 extern void arch_unoptimize_kprobes(struct list_head *oplist,
1487 struct list_head *done_list)
1489 struct optimized_kprobe *op, *tmp;
1490 int c = 0;
1492 list_for_each_entry_safe(op, tmp, oplist, list) {
1493 /* Setup param */
1494 setup_unoptimize_kprobe(&jump_poke_params[c],
1495 jump_poke_bufs[c].buf, op);
1496 list_move(&op->list, done_list);
1497 if (++c >= MAX_OPTIMIZE_PROBES)
1498 break;
1502 * text_poke_smp doesn't support NMI/MCE code modifying.
1503 * However, since kprobes itself also doesn't support NMI/MCE
1504 * code probing, it's not a problem.
1506 text_poke_smp_batch(jump_poke_params, c);
1509 /* Replace a relative jump with a breakpoint (int3). */
1510 void __kprobes arch_unoptimize_kprobe(struct optimized_kprobe *op)
1512 u8 buf[RELATIVEJUMP_SIZE];
1514 /* Set int3 to first byte for kprobes */
1515 buf[0] = BREAKPOINT_INSTRUCTION;
1516 memcpy(buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE);
1517 text_poke_smp(op->kp.addr, buf, RELATIVEJUMP_SIZE);
1520 static int __kprobes setup_detour_execution(struct kprobe *p,
1521 struct pt_regs *regs,
1522 int reenter)
1524 struct optimized_kprobe *op;
1526 if (p->flags & KPROBE_FLAG_OPTIMIZED) {
1527 /* This kprobe is really able to run optimized path. */
1528 op = container_of(p, struct optimized_kprobe, kp);
1529 /* Detour through copied instructions */
1530 regs->ip = (unsigned long)op->optinsn.insn + TMPL_END_IDX;
1531 if (!reenter)
1532 reset_current_kprobe();
1533 preempt_enable_no_resched();
1534 return 1;
1536 return 0;
1539 static int __kprobes init_poke_params(void)
1541 /* Allocate code buffer and parameter array */
1542 jump_poke_bufs = kmalloc(sizeof(struct jump_poke_buffer) *
1543 MAX_OPTIMIZE_PROBES, GFP_KERNEL);
1544 if (!jump_poke_bufs)
1545 return -ENOMEM;
1547 jump_poke_params = kmalloc(sizeof(struct text_poke_param) *
1548 MAX_OPTIMIZE_PROBES, GFP_KERNEL);
1549 if (!jump_poke_params) {
1550 kfree(jump_poke_bufs);
1551 jump_poke_bufs = NULL;
1552 return -ENOMEM;
1555 return 0;
1557 #else /* !CONFIG_OPTPROBES */
1558 static int __kprobes init_poke_params(void)
1560 return 0;
1562 #endif
1564 int __init arch_init_kprobes(void)
1566 return init_poke_params();
1569 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
1571 return 0;