mfd: Fix mismatch in twl4030 mutex lock-unlock
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / kernel / kprobes.c
blob794bc95134cd5f45f85345f6a006a9d45442fcb3
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.
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.
81 static u32 twobyte_is_boostable[256 / 32] = {
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 */
103 #undef W
105 struct kretprobe_blackpoint kretprobe_blacklist[] = {
106 {"__switch_to", }, /* This function switches only current task, but
107 doesn't switch kernel stack.*/
108 {NULL, NULL} /* Terminator */
110 const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
112 static void __kprobes __synthesize_relative_insn(void *from, void *to, u8 op)
114 struct __arch_relative_insn {
115 u8 op;
116 s32 raddr;
117 } __attribute__((packed)) *insn;
119 insn = (struct __arch_relative_insn *)from;
120 insn->raddr = (s32)((long)(to) - ((long)(from) + 5));
121 insn->op = op;
124 /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
125 static void __kprobes synthesize_reljump(void *from, void *to)
127 __synthesize_relative_insn(from, to, RELATIVEJUMP_OPCODE);
131 * Skip the prefixes of the instruction.
133 static kprobe_opcode_t *__kprobes skip_prefixes(kprobe_opcode_t *insn)
135 insn_attr_t attr;
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);
142 #ifdef CONFIG_X86_64
143 if (inat_is_rex_prefix(attr))
144 insn++;
145 #endif
146 return insn;
150 * Returns non-zero if opcode is boostable.
151 * RIP relative instructions are adjusted at copying time in 64 bits mode
153 static int __kprobes can_boost(kprobe_opcode_t *opcodes)
155 kprobe_opcode_t opcode;
156 kprobe_opcode_t *orig_opcodes = opcodes;
158 if (search_exception_tables((unsigned long)opcodes))
159 return 0; /* Page fault may occur on this address. */
161 retry:
162 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
163 return 0;
164 opcode = *(opcodes++);
166 /* 2nd-byte opcode */
167 if (opcode == 0x0f) {
168 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
169 return 0;
170 return test_bit(*opcodes,
171 (unsigned long *)twobyte_is_boostable);
174 switch (opcode & 0xf0) {
175 #ifdef CONFIG_X86_64
176 case 0x40:
177 goto retry; /* REX prefix is boostable */
178 #endif
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);
209 /* Recover the probed instruction at addr for further analysis. */
210 static int recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)
212 struct kprobe *kp;
213 kp = get_kprobe((void *)addr);
214 if (!kp)
215 return -EINVAL;
218 * Basically, kp->ainsn.insn has an original instruction.
219 * However, RIP-relative instruction can not do single-stepping
220 * at different place, __copy_instruction() tweaks the displacement of
221 * that instruction. In that case, we can't recover the instruction
222 * from the kp->ainsn.insn.
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.
230 memcpy(buf, kp->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
231 buf[0] = kp->opcode;
232 return 0;
235 /* Check if paddr is at an instruction boundary */
236 static int __kprobes can_probe(unsigned long paddr)
238 int ret;
239 unsigned long addr, offset = 0;
240 struct insn insn;
241 kprobe_opcode_t buf[MAX_INSN_SIZE];
243 if (!kallsyms_lookup_size_offset(paddr, NULL, &offset))
244 return 0;
246 /* Decode instructions */
247 addr = paddr - offset;
248 while (addr < paddr) {
249 kernel_insn_init(&insn, (void *)addr);
250 insn_get_opcode(&insn);
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.
257 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION) {
258 ret = recover_probed_instruction(buf, addr);
259 if (ret)
261 * Another debugging subsystem might insert
262 * this breakpoint. In that case, we can't
263 * recover it.
265 return 0;
266 kernel_insn_init(&insn, buf);
268 insn_get_length(&insn);
269 addr += insn.length;
272 return (addr == paddr);
276 * Returns non-zero if opcode modifies the interrupt flag.
278 static int __kprobes is_IF_modifier(kprobe_opcode_t *insn)
280 /* Skip prefixes */
281 insn = skip_prefixes(insn);
283 switch (*insn) {
284 case 0xfa: /* cli */
285 case 0xfb: /* sti */
286 case 0xcf: /* iret/iretd */
287 case 0x9d: /* popf/popfd */
288 return 1;
291 return 0;
295 * Copy an instruction and adjust the displacement if the instruction
296 * uses the %rip-relative addressing mode.
297 * If it does, Return the address of the 32-bit displacement word.
298 * If not, return null.
299 * Only applicable to 64-bit x86.
301 static int __kprobes __copy_instruction(u8 *dest, u8 *src, int recover)
303 struct insn insn;
304 int ret;
305 kprobe_opcode_t buf[MAX_INSN_SIZE];
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);
318 insn_get_length(&insn);
319 memcpy(dest, insn.kaddr, insn.length);
321 #ifdef CONFIG_X86_64
322 if (insn_rip_relative(&insn)) {
323 s64 newdisp;
324 u8 *disp;
325 kernel_insn_init(&insn, dest);
326 insn_get_displacement(&insn);
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.
339 newdisp = (u8 *) src + (s64) insn.displacement.value -
340 (u8 *) dest;
341 BUG_ON((s64) (s32) newdisp != newdisp); /* Sanity check. */
342 disp = (u8 *) dest + insn_offset_displacement(&insn);
343 *(s32 *) disp = (s32) newdisp;
345 #endif
346 return insn.length;
349 static void __kprobes arch_copy_kprobe(struct kprobe *p)
352 * Copy an instruction without recovering int3, because it will be
353 * put by another subsystem.
355 __copy_instruction(p->ainsn.insn, p->addr, 0);
357 if (can_boost(p->addr))
358 p->ainsn.boostable = 0;
359 else
360 p->ainsn.boostable = -1;
362 p->opcode = *p->addr;
365 int __kprobes arch_prepare_kprobe(struct kprobe *p)
367 if (alternatives_text_reserved(p->addr, p->addr))
368 return -EINVAL;
370 if (!can_probe((unsigned long)p->addr))
371 return -EILSEQ;
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;
380 void __kprobes arch_arm_kprobe(struct kprobe *p)
382 text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
385 void __kprobes arch_disarm_kprobe(struct kprobe *p)
387 text_poke(p->addr, &p->opcode, 1);
390 void __kprobes arch_remove_kprobe(struct kprobe *p)
392 if (p->ainsn.insn) {
393 free_insn_slot(p->ainsn.insn, (p->ainsn.boostable == 1));
394 p->ainsn.insn = NULL;
398 static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
400 kcb->prev_kprobe.kp = kprobe_running();
401 kcb->prev_kprobe.status = kcb->kprobe_status;
402 kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
403 kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
406 static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
408 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
409 kcb->kprobe_status = kcb->prev_kprobe.status;
410 kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
411 kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
414 static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
415 struct kprobe_ctlblk *kcb)
417 __this_cpu_write(current_kprobe, p);
418 kcb->kprobe_saved_flags = kcb->kprobe_old_flags
419 = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
420 if (is_IF_modifier(p->ainsn.insn))
421 kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
424 static void __kprobes clear_btf(void)
426 if (test_thread_flag(TIF_BLOCKSTEP)) {
427 unsigned long debugctl = get_debugctlmsr();
429 debugctl &= ~DEBUGCTLMSR_BTF;
430 update_debugctlmsr(debugctl);
434 static void __kprobes restore_btf(void)
436 if (test_thread_flag(TIF_BLOCKSTEP)) {
437 unsigned long debugctl = get_debugctlmsr();
439 debugctl |= DEBUGCTLMSR_BTF;
440 update_debugctlmsr(debugctl);
444 void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri,
445 struct pt_regs *regs)
447 unsigned long *sara = stack_addr(regs);
449 ri->ret_addr = (kprobe_opcode_t *) *sara;
451 /* Replace the return addr with trampoline addr */
452 *sara = (unsigned long) &kretprobe_trampoline;
455 #ifdef CONFIG_OPTPROBES
456 static 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
463 static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs,
464 struct kprobe_ctlblk *kcb, int reenter)
466 if (setup_detour_execution(p, regs, reenter))
467 return;
469 #if !defined(CONFIG_PREEMPT)
470 if (p->ainsn.boostable == 1 && !p->post_handler) {
471 /* Boost up -- we can execute copied instructions directly */
472 if (!reenter)
473 reset_current_kprobe();
475 * Reentering boosted probe doesn't reset current_kprobe,
476 * nor set current_kprobe, because it doesn't use single
477 * stepping.
479 regs->ip = (unsigned long)p->ainsn.insn;
480 preempt_enable_no_resched();
481 return;
483 #endif
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;
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.
506 static int __kprobes reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
507 struct kprobe_ctlblk *kcb)
509 switch (kcb->kprobe_status) {
510 case KPROBE_HIT_SSDONE:
511 case KPROBE_HIT_ACTIVE:
512 kprobes_inc_nmissed_count(p);
513 setup_singlestep(p, regs, kcb, 1);
514 break;
515 case KPROBE_HIT_SS:
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.
522 printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n",
523 p->addr);
524 dump_kprobe(p);
525 BUG();
526 default:
527 /* impossible cases */
528 WARN_ON(1);
529 return 0;
532 return 1;
536 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
537 * remain disabled throughout this function.
539 static int __kprobes kprobe_handler(struct pt_regs *regs)
541 kprobe_opcode_t *addr;
542 struct kprobe *p;
543 struct kprobe_ctlblk *kcb;
545 addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
547 * We don't want to be preempted for the entire
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().
552 preempt_disable();
554 kcb = get_kprobe_ctlblk();
555 p = get_kprobe(addr);
557 if (p) {
558 if (kprobe_running()) {
559 if (reenter_kprobe(p, regs, kcb))
560 return 1;
561 } else {
562 set_current_kprobe(p, regs, kcb);
563 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
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.
573 if (!p->pre_handler || !p->pre_handler(p, regs))
574 setup_singlestep(p, regs, kcb, 0);
575 return 1;
577 } else if (*addr != BREAKPOINT_INSTRUCTION) {
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.
587 regs->ip = (unsigned long)addr;
588 preempt_enable_no_resched();
589 return 1;
590 } else if (kprobe_running()) {
591 p = __this_cpu_read(current_kprobe);
592 if (p->break_handler && p->break_handler(p, regs)) {
593 setup_singlestep(p, regs, kcb, 0);
594 return 1;
596 } /* else: not a kprobe fault; let the kernel handle it */
598 preempt_enable_no_resched();
599 return 0;
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" \
644 " pushl %es\n" \
645 " pushl %ds\n" \
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
666 * When a retprobed function returns, this code saves registers and
667 * calls trampoline_handler() runs, which calls the kretprobe's handler.
669 static void __used __kprobes kretprobe_trampoline_holder(void)
671 asm volatile (
672 ".global kretprobe_trampoline\n"
673 "kretprobe_trampoline: \n"
674 #ifdef CONFIG_X86_64
675 /* We don't bother saving the ss register */
676 " pushq %rsp\n"
677 " pushfq\n"
678 SAVE_REGS_STRING
679 " movq %rsp, %rdi\n"
680 " call trampoline_handler\n"
681 /* Replace saved sp with true return address. */
682 " movq %rax, 152(%rsp)\n"
683 RESTORE_REGS_STRING
684 " popfq\n"
685 #else
686 " pushf\n"
687 SAVE_REGS_STRING
688 " movl %esp, %eax\n"
689 " call trampoline_handler\n"
690 /* Move flags to cs */
691 " movl 56(%esp), %edx\n"
692 " movl %edx, 52(%esp)\n"
693 /* Replace saved flags with true return address. */
694 " movl %eax, 56(%esp)\n"
695 RESTORE_REGS_STRING
696 " popf\n"
697 #endif
698 " ret\n");
702 * Called from kretprobe_trampoline
704 static __used __kprobes void *trampoline_handler(struct pt_regs *regs)
706 struct kretprobe_instance *ri = NULL;
707 struct hlist_head *head, empty_rp;
708 struct hlist_node *node, *tmp;
709 unsigned long flags, orig_ret_address = 0;
710 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
711 kprobe_opcode_t *correct_ret_addr = NULL;
713 INIT_HLIST_HEAD(&empty_rp);
714 kretprobe_hash_lock(current, &head, &flags);
715 /* fixup registers */
716 #ifdef CONFIG_X86_64
717 regs->cs = __KERNEL_CS;
718 #else
719 regs->cs = __KERNEL_CS | get_kernel_rpl();
720 regs->gs = 0;
721 #endif
722 regs->ip = trampoline_address;
723 regs->orig_ax = ~0UL;
726 * It is possible to have multiple instances associated with a given
727 * task either because multiple functions in the call path have
728 * return probes installed on them, and/or more than one
729 * return probe was registered for a target function.
731 * We can handle this because:
732 * - instances are always pushed into the head of the list
733 * - when multiple return probes are registered for the same
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.
738 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
739 if (ri->task != current)
740 /* another task is sharing our hash bucket */
741 continue;
743 orig_ret_address = (unsigned long)ri->ret_addr;
745 if (orig_ret_address != trampoline_address)
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
751 break;
754 kretprobe_assert(ri, orig_ret_address, trampoline_address);
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;
762 orig_ret_address = (unsigned long)ri->ret_addr;
763 if (ri->rp && ri->rp->handler) {
764 __this_cpu_write(current_kprobe, &ri->rp->kp);
765 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
766 ri->ret_addr = correct_ret_addr;
767 ri->rp->handler(ri, regs);
768 __this_cpu_write(current_kprobe, NULL);
771 recycle_rp_inst(ri, &empty_rp);
773 if (orig_ret_address != trampoline_address)
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
779 break;
782 kretprobe_hash_unlock(current, &flags);
784 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
785 hlist_del(&ri->hlist);
786 kfree(ri);
788 return (void *)orig_ret_address;
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.
799 * This function prepares to return from the post-single-step
800 * interrupt. We have to fix up the stack as follows:
802 * 0) Except in the case of absolute or indirect jump or call instructions,
803 * the new ip is relative to the copied instruction. We need to make
804 * it relative to the original instruction.
806 * 1) If the single-stepped instruction was pushfl, then the TF and IF
807 * flags are set in the just-pushed flags, and may need to be cleared.
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.
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.
818 static void __kprobes resume_execution(struct kprobe *p,
819 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
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;
824 kprobe_opcode_t *insn = p->ainsn.insn;
826 /* Skip prefixes */
827 insn = skip_prefixes(insn);
829 regs->flags &= ~X86_EFLAGS_TF;
830 switch (*insn) {
831 case 0x9c: /* pushfl */
832 *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
833 *tos |= kcb->kprobe_old_flags;
834 break;
835 case 0xc2: /* iret/ret/lret */
836 case 0xc3:
837 case 0xca:
838 case 0xcb:
839 case 0xcf:
840 case 0xea: /* jmp absolute -- ip is correct */
841 /* ip is already adjusted, no more changes required */
842 p->ainsn.boostable = 1;
843 goto no_change;
844 case 0xe8: /* call relative - Fix return addr */
845 *tos = orig_ip + (*tos - copy_ip);
846 break;
847 #ifdef CONFIG_X86_32
848 case 0x9a: /* call absolute -- same as call absolute, indirect */
849 *tos = orig_ip + (*tos - copy_ip);
850 goto no_change;
851 #endif
852 case 0xff:
853 if ((insn[1] & 0x30) == 0x10) {
855 * call absolute, indirect
856 * Fix return addr; ip is correct.
857 * But this is not boostable
859 *tos = orig_ip + (*tos - copy_ip);
860 goto no_change;
861 } else if (((insn[1] & 0x31) == 0x20) ||
862 ((insn[1] & 0x31) == 0x21)) {
864 * jmp near and far, absolute indirect
865 * ip is correct. And this is boostable
867 p->ainsn.boostable = 1;
868 goto no_change;
870 default:
871 break;
874 if (p->ainsn.boostable == 0) {
875 if ((regs->ip > copy_ip) &&
876 (regs->ip - copy_ip) + 5 < MAX_INSN_SIZE) {
878 * These instructions can be executed directly if it
879 * jumps back to correct address.
881 synthesize_reljump((void *)regs->ip,
882 (void *)orig_ip + (regs->ip - copy_ip));
883 p->ainsn.boostable = 1;
884 } else {
885 p->ainsn.boostable = -1;
889 regs->ip += orig_ip - copy_ip;
891 no_change:
892 restore_btf();
896 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
897 * remain disabled throughout this function.
899 static int __kprobes post_kprobe_handler(struct pt_regs *regs)
901 struct kprobe *cur = kprobe_running();
902 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
904 if (!cur)
905 return 0;
907 resume_execution(cur, regs, kcb);
908 regs->flags |= kcb->kprobe_saved_flags;
910 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
911 kcb->kprobe_status = KPROBE_HIT_SSDONE;
912 cur->post_handler(cur, regs, 0);
915 /* Restore back the original saved kprobes variables and continue. */
916 if (kcb->kprobe_status == KPROBE_REENTER) {
917 restore_previous_kprobe(kcb);
918 goto out;
920 reset_current_kprobe();
921 out:
922 preempt_enable_no_resched();
925 * if somebody else is singlestepping across a probe point, flags
926 * will have TF set, in which case, continue the remaining processing
927 * of do_debug, as if this is not a probe hit.
929 if (regs->flags & X86_EFLAGS_TF)
930 return 0;
932 return 1;
935 int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
937 struct kprobe *cur = kprobe_running();
938 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
940 switch (kcb->kprobe_status) {
941 case KPROBE_HIT_SS:
942 case KPROBE_REENTER:
944 * We are here because the instruction being single
945 * stepped caused a page fault. We reset the current
946 * kprobe and the ip points back to the probe address
947 * and allow the page fault handler to continue as a
948 * normal page fault.
950 regs->ip = (unsigned long)cur->addr;
951 regs->flags |= kcb->kprobe_old_flags;
952 if (kcb->kprobe_status == KPROBE_REENTER)
953 restore_previous_kprobe(kcb);
954 else
955 reset_current_kprobe();
956 preempt_enable_no_resched();
957 break;
958 case KPROBE_HIT_ACTIVE:
959 case KPROBE_HIT_SSDONE:
961 * We increment the nmissed count for accounting,
962 * we can also use npre/npostfault count for accounting
963 * these specific fault cases.
965 kprobes_inc_nmissed_count(cur);
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.
974 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
975 return 1;
978 * In case the user-specified fault handler returned
979 * zero, try to fix up.
981 if (fixup_exception(regs))
982 return 1;
985 * fixup routine could not handle it,
986 * Let do_page_fault() fix it.
988 break;
989 default:
990 break;
992 return 0;
996 * Wrapper routine for handling exceptions.
998 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
999 unsigned long val, void *data)
1001 struct die_args *args = data;
1002 int ret = NOTIFY_DONE;
1004 if (args->regs && user_mode_vm(args->regs))
1005 return ret;
1007 switch (val) {
1008 case DIE_INT3:
1009 if (kprobe_handler(args->regs))
1010 ret = NOTIFY_STOP;
1011 break;
1012 case DIE_DEBUG:
1013 if (post_kprobe_handler(args->regs)) {
1015 * Reset the BS bit in dr6 (pointed by args->err) to
1016 * denote completion of processing
1018 (*(unsigned long *)ERR_PTR(args->err)) &= ~DR_STEP;
1019 ret = NOTIFY_STOP;
1021 break;
1022 case DIE_GPF:
1024 * To be potentially processing a kprobe fault and to
1025 * trust the result from kprobe_running(), we have
1026 * be non-preemptible.
1028 if (!preemptible() && kprobe_running() &&
1029 kprobe_fault_handler(args->regs, args->trapnr))
1030 ret = NOTIFY_STOP;
1031 break;
1032 default:
1033 break;
1035 return ret;
1038 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
1040 struct jprobe *jp = container_of(p, struct jprobe, kp);
1041 unsigned long addr;
1042 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1044 kcb->jprobe_saved_regs = *regs;
1045 kcb->jprobe_saved_sp = stack_addr(regs);
1046 addr = (unsigned long)(kcb->jprobe_saved_sp);
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.
1055 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
1056 MIN_STACK_SIZE(addr));
1057 regs->flags &= ~X86_EFLAGS_IF;
1058 trace_hardirqs_off();
1059 regs->ip = (unsigned long)(jp->entry);
1060 return 1;
1063 void __kprobes jprobe_return(void)
1065 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
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");
1080 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
1082 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1083 u8 *addr = (u8 *) (regs->ip - 1);
1084 struct jprobe *jp = container_of(p, struct jprobe, kp);
1086 if ((addr > (u8 *) jprobe_return) &&
1087 (addr < (u8 *) jprobe_return_end)) {
1088 if (stack_addr(regs) != kcb->jprobe_saved_sp) {
1089 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
1090 printk(KERN_ERR
1091 "current sp %p does not match saved sp %p\n",
1092 stack_addr(regs), kcb->jprobe_saved_sp);
1093 printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
1094 show_registers(saved_regs);
1095 printk(KERN_ERR "Current registers\n");
1096 show_registers(regs);
1097 BUG();
1099 *regs = kcb->jprobe_saved_regs;
1100 memcpy((kprobe_opcode_t *)(kcb->jprobe_saved_sp),
1101 kcb->jprobes_stack,
1102 MIN_STACK_SIZE(kcb->jprobe_saved_sp));
1103 preempt_enable_no_resched();
1104 return 1;
1106 return 0;
1110 #ifdef CONFIG_OPTPROBES
1112 /* Insert a call instruction at address 'from', which calls address 'to'.*/
1113 static void __kprobes synthesize_relcall(void *from, void *to)
1115 __synthesize_relative_insn(from, to, RELATIVECALL_OPCODE);
1118 /* Insert a move instruction which sets a pointer to eax/rdi (1st arg). */
1119 static void __kprobes synthesize_set_arg1(kprobe_opcode_t *addr,
1120 unsigned long val)
1122 #ifdef CONFIG_X86_64
1123 *addr++ = 0x48;
1124 *addr++ = 0xbf;
1125 #else
1126 *addr++ = 0xb8;
1127 #endif
1128 *(unsigned long *)addr = val;
1131 static void __used __kprobes kprobes_optinsn_template_holder(void)
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");
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)
1181 #define INT3_SIZE sizeof(kprobe_opcode_t)
1183 /* Optimized kprobe call back function: called from optinsn */
1184 static void __kprobes optimized_callback(struct optimized_kprobe *op,
1185 struct pt_regs *regs)
1187 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1188 unsigned long flags;
1190 /* This is possible if op is under delayed unoptimizing */
1191 if (kprobe_disabled(&op->kp))
1192 return;
1194 local_irq_save(flags);
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;
1208 __this_cpu_write(current_kprobe, &op->kp);
1209 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
1210 opt_pre_handler(&op->kp, regs);
1211 __this_cpu_write(current_kprobe, NULL);
1213 local_irq_restore(flags);
1216 static int __kprobes copy_optimized_instructions(u8 *dest, u8 *src)
1218 int len = 0, ret;
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;
1226 /* Check whether the address range is reserved */
1227 if (ftrace_text_reserved(src, src + len - 1) ||
1228 alternatives_text_reserved(src, src + len - 1) ||
1229 jump_label_text_reserved(src, src + len - 1))
1230 return -EBUSY;
1232 return len;
1235 /* Check whether insn is indirect jump */
1236 static int __kprobes insn_is_indirect_jump(struct insn *insn)
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 */
1243 /* Check whether insn jumps into specified address range */
1244 static int insn_jump_into_range(struct insn *insn, unsigned long start, int len)
1246 unsigned long target = 0;
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;
1265 target = (unsigned long)insn->next_byte + insn->immediate.value;
1267 return (start <= target && target <= start + len);
1270 /* Decode whole function to ensure any instructions don't jump into target */
1271 static int __kprobes can_optimize(unsigned long paddr)
1273 int ret;
1274 unsigned long addr, size = 0, offset = 0;
1275 struct insn insn;
1276 kprobe_opcode_t buf[MAX_INSN_SIZE];
1278 /* Lookup symbol including addr */
1279 if (!kallsyms_lookup_size_offset(paddr, &size, &offset))
1280 return 0;
1283 * Do not optimize in the entry code due to the unstable
1284 * stack handling.
1286 if ((paddr >= (unsigned long )__entry_text_start) &&
1287 (paddr < (unsigned long )__entry_text_end))
1288 return 0;
1290 /* Check there is enough space for a relative jump. */
1291 if (size - offset < RELATIVEJUMP_SIZE)
1292 return 0;
1294 /* Decode instructions */
1295 addr = paddr - offset;
1296 while (addr < paddr - offset + size) { /* Decode until function end */
1297 if (search_exception_tables(addr))
1299 * Since some fixup code will jumps into this function,
1300 * we can't optimize kprobe in this function.
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);
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;
1323 return 1;
1326 /* Check optimized_kprobe can actually be optimized. */
1327 int __kprobes arch_check_optimized_kprobe(struct optimized_kprobe *op)
1329 int i;
1330 struct kprobe *p;
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;
1338 return 0;
1341 /* Check the addr is within the optimized instructions. */
1342 int __kprobes arch_within_optimized_kprobe(struct optimized_kprobe *op,
1343 unsigned long addr)
1345 return ((unsigned long)op->kp.addr <= addr &&
1346 (unsigned long)op->kp.addr + op->optinsn.size > addr);
1349 /* Free optimized instruction slot */
1350 static __kprobes
1351 void __arch_remove_optimized_kprobe(struct optimized_kprobe *op, int dirty)
1353 if (op->optinsn.insn) {
1354 free_optinsn_slot(op->optinsn.insn, dirty);
1355 op->optinsn.insn = NULL;
1356 op->optinsn.size = 0;
1360 void __kprobes arch_remove_optimized_kprobe(struct optimized_kprobe *op)
1362 __arch_remove_optimized_kprobe(op, 1);
1366 * Copy replacing target instructions
1367 * Target instructions MUST be relocatable (checked inside)
1369 int __kprobes arch_prepare_optimized_kprobe(struct optimized_kprobe *op)
1371 u8 *buf;
1372 int ret;
1373 long rel;
1375 if (!can_optimize((unsigned long)op->kp.addr))
1376 return -EILSEQ;
1378 op->optinsn.insn = get_optinsn_slot();
1379 if (!op->optinsn.insn)
1380 return -ENOMEM;
1383 * Verify if the address gap is in 2GB range, because this uses
1384 * a relative jump.
1386 rel = (long)op->optinsn.insn - (long)op->kp.addr + RELATIVEJUMP_SIZE;
1387 if (abs(rel) > 0x7fffffff)
1388 return -ERANGE;
1390 buf = (u8 *)op->optinsn.insn;
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;
1398 op->optinsn.size = ret;
1400 /* Copy arch-dep-instance from template */
1401 memcpy(buf, &optprobe_template_entry, TMPL_END_IDX);
1403 /* Set probe information */
1404 synthesize_set_arg1(buf + TMPL_MOVE_IDX, (unsigned long)op);
1406 /* Set probe function call */
1407 synthesize_relcall(buf + TMPL_CALL_IDX, optimized_callback);
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);
1413 flush_icache_range((unsigned long) buf,
1414 (unsigned long) buf + TMPL_END_IDX +
1415 op->optinsn.size + RELATIVEJUMP_SIZE);
1416 return 0;
1419 #define MAX_OPTIMIZE_PROBES 256
1420 static struct text_poke_param *jump_poke_params;
1421 static struct jump_poke_buffer {
1422 u8 buf[RELATIVEJUMP_SIZE];
1423 } *jump_poke_bufs;
1425 static void __kprobes setup_optimize_kprobe(struct text_poke_param *tprm,
1426 u8 *insn_buf,
1427 struct optimized_kprobe *op)
1429 s32 rel = (s32)((long)op->optinsn.insn -
1430 ((long)op->kp.addr + RELATIVEJUMP_SIZE));
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);
1436 insn_buf[0] = RELATIVEJUMP_OPCODE;
1437 *(s32 *)(&insn_buf[1]) = rel;
1439 tprm->addr = op->kp.addr;
1440 tprm->opcode = insn_buf;
1441 tprm->len = RELATIVEJUMP_SIZE;
1445 * Replace breakpoints (int3) with relative jumps.
1446 * Caller must call with locking kprobe_mutex and text_mutex.
1448 void __kprobes arch_optimize_kprobes(struct list_head *oplist)
1450 struct optimized_kprobe *op, *tmp;
1451 int c = 0;
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;
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.
1468 text_poke_smp_batch(jump_poke_params, c);
1471 static void __kprobes setup_unoptimize_kprobe(struct text_poke_param *tprm,
1472 u8 *insn_buf,
1473 struct optimized_kprobe *op)
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);
1479 tprm->addr = op->kp.addr;
1480 tprm->opcode = insn_buf;
1481 tprm->len = RELATIVEJUMP_SIZE;
1485 * Recover original instructions and breakpoints from relative jumps.
1486 * Caller must call with locking kprobe_mutex.
1488 extern void arch_unoptimize_kprobes(struct list_head *oplist,
1489 struct list_head *done_list)
1491 struct optimized_kprobe *op, *tmp;
1492 int c = 0;
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;
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.
1508 text_poke_smp_batch(jump_poke_params, c);
1511 /* Replace a relative jump with a breakpoint (int3). */
1512 void __kprobes arch_unoptimize_kprobe(struct optimized_kprobe *op)
1514 u8 buf[RELATIVEJUMP_SIZE];
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);
1522 static int __kprobes setup_detour_execution(struct kprobe *p,
1523 struct pt_regs *regs,
1524 int reenter)
1526 struct optimized_kprobe *op;
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;
1538 return 0;
1541 static int __kprobes init_poke_params(void)
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;
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;
1557 return 0;
1559 #else /* !CONFIG_OPTPROBES */
1560 static int __kprobes init_poke_params(void)
1562 return 0;
1564 #endif
1566 int __init arch_init_kprobes(void)
1568 return init_poke_params();
1571 int __kprobes arch_trampoline_kprobe(struct kprobe *p)
1573 return 0;