2 * Kernel Probes (KProbes)
3 * arch/x86_64/kernel/kprobes.c
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * Copyright (C) IBM Corporation, 2002, 2004
21 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
22 * Probes initial implementation ( includes contributions from
24 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
25 * interface to access function arguments.
26 * 2004-Oct Jim Keniston <kenistoj@us.ibm.com> and Prasanna S Panchamukhi
27 * <prasanna@in.ibm.com> adapted for x86_64
28 * 2005-Mar Roland McGrath <roland@redhat.com>
29 * Fixed to handle %rip-relative addressing mode correctly.
30 * 2005-May Rusty Lynch <rusty.lynch@intel.com>
31 * Added function return probes functionality
34 #include <linux/config.h>
35 #include <linux/kprobes.h>
36 #include <linux/ptrace.h>
37 #include <linux/spinlock.h>
38 #include <linux/string.h>
39 #include <linux/slab.h>
40 #include <linux/preempt.h>
42 #include <asm/cacheflush.h>
43 #include <asm/pgtable.h>
44 #include <asm/kdebug.h>
46 static DECLARE_MUTEX(kprobe_mutex);
48 static struct kprobe *current_kprobe;
49 static unsigned long kprobe_status, kprobe_old_rflags, kprobe_saved_rflags;
50 static struct kprobe *kprobe_prev;
51 static unsigned long kprobe_status_prev, kprobe_old_rflags_prev, kprobe_saved_rflags_prev;
52 static struct pt_regs jprobe_saved_regs;
53 static long *jprobe_saved_rsp;
54 void jprobe_return_end(void);
56 /* copy of the kernel stack at the probe fire time */
57 static kprobe_opcode_t jprobes_stack[MAX_STACK_SIZE];
60 * returns non-zero if opcode modifies the interrupt flag.
62 static inline int is_IF_modifier(kprobe_opcode_t *insn)
67 case 0xcf: /* iret/iretd */
68 case 0x9d: /* popf/popfd */
72 if (*insn >= 0x40 && *insn <= 0x4f && *++insn == 0xcf)
77 int __kprobes arch_prepare_kprobe(struct kprobe *p)
79 /* insn: must be on special executable page on x86_64. */
81 p->ainsn.insn = get_insn_slot();
90 * Determine if the instruction uses the %rip-relative addressing mode.
91 * If it does, return the address of the 32-bit displacement word.
92 * If not, return null.
94 static inline s32 *is_riprel(u8 *insn)
96 #define W(row,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bb,bc,bd,be,bf) \
97 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
98 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
99 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
100 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
102 static const u64 onebyte_has_modrm[256 / 64] = {
103 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
104 /* ------------------------------- */
105 W(0x00, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 00 */
106 W(0x10, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 10 */
107 W(0x20, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0)| /* 20 */
108 W(0x30, 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0), /* 30 */
109 W(0x40, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 40 */
110 W(0x50, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 50 */
111 W(0x60, 0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,0)| /* 60 */
112 W(0x70, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 70 */
113 W(0x80, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 80 */
114 W(0x90, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 90 */
115 W(0xa0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* a0 */
116 W(0xb0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* b0 */
117 W(0xc0, 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0)| /* c0 */
118 W(0xd0, 1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1)| /* d0 */
119 W(0xe0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* e0 */
120 W(0xf0, 0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1) /* f0 */
121 /* ------------------------------- */
122 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
124 static const u64 twobyte_has_modrm[256 / 64] = {
125 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
126 /* ------------------------------- */
127 W(0x00, 1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,1)| /* 0f */
128 W(0x10, 1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0)| /* 1f */
129 W(0x20, 1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1)| /* 2f */
130 W(0x30, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 3f */
131 W(0x40, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 4f */
132 W(0x50, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 5f */
133 W(0x60, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 6f */
134 W(0x70, 1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1), /* 7f */
135 W(0x80, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 8f */
136 W(0x90, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 9f */
137 W(0xa0, 0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1)| /* af */
138 W(0xb0, 1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1), /* bf */
139 W(0xc0, 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0)| /* cf */
140 W(0xd0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* df */
141 W(0xe0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* ef */
142 W(0xf0, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0) /* ff */
143 /* ------------------------------- */
144 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
149 /* Skip legacy instruction prefixes. */
169 /* Skip REX instruction prefix. */
170 if ((*insn & 0xf0) == 0x40)
173 if (*insn == 0x0f) { /* Two-byte opcode. */
175 need_modrm = test_bit(*insn, twobyte_has_modrm);
176 } else { /* One-byte opcode. */
177 need_modrm = test_bit(*insn, onebyte_has_modrm);
182 if ((modrm & 0xc7) == 0x05) { /* %rip+disp32 addressing mode */
183 /* Displacement follows ModRM byte. */
184 return (s32 *) ++insn;
188 /* No %rip-relative addressing mode here. */
192 void __kprobes arch_copy_kprobe(struct kprobe *p)
195 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE);
196 ripdisp = is_riprel(p->ainsn.insn);
199 * The copied instruction uses the %rip-relative
200 * addressing mode. Adjust the displacement for the
201 * difference between the original location of this
202 * instruction and the location of the copy that will
203 * actually be run. The tricky bit here is making sure
204 * that the sign extension happens correctly in this
205 * calculation, since we need a signed 32-bit result to
206 * be sign-extended to 64 bits when it's added to the
207 * %rip value and yield the same 64-bit result that the
208 * sign-extension of the original signed 32-bit
209 * displacement would have given.
211 s64 disp = (u8 *) p->addr + *ripdisp - (u8 *) p->ainsn.insn;
212 BUG_ON((s64) (s32) disp != disp); /* Sanity check. */
215 p->opcode = *p->addr;
218 void __kprobes arch_arm_kprobe(struct kprobe *p)
220 *p->addr = BREAKPOINT_INSTRUCTION;
221 flush_icache_range((unsigned long) p->addr,
222 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
225 void __kprobes arch_disarm_kprobe(struct kprobe *p)
227 *p->addr = p->opcode;
228 flush_icache_range((unsigned long) p->addr,
229 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
232 void __kprobes arch_remove_kprobe(struct kprobe *p)
235 free_insn_slot(p->ainsn.insn);
239 static inline void save_previous_kprobe(void)
241 kprobe_prev = current_kprobe;
242 kprobe_status_prev = kprobe_status;
243 kprobe_old_rflags_prev = kprobe_old_rflags;
244 kprobe_saved_rflags_prev = kprobe_saved_rflags;
247 static inline void restore_previous_kprobe(void)
249 current_kprobe = kprobe_prev;
250 kprobe_status = kprobe_status_prev;
251 kprobe_old_rflags = kprobe_old_rflags_prev;
252 kprobe_saved_rflags = kprobe_saved_rflags_prev;
255 static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs)
258 kprobe_saved_rflags = kprobe_old_rflags
259 = (regs->eflags & (TF_MASK | IF_MASK));
260 if (is_IF_modifier(p->ainsn.insn))
261 kprobe_saved_rflags &= ~IF_MASK;
264 static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
266 regs->eflags |= TF_MASK;
267 regs->eflags &= ~IF_MASK;
268 /*single step inline if the instruction is an int3*/
269 if (p->opcode == BREAKPOINT_INSTRUCTION)
270 regs->rip = (unsigned long)p->addr;
272 regs->rip = (unsigned long)p->ainsn.insn;
275 void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
276 struct pt_regs *regs)
278 unsigned long *sara = (unsigned long *)regs->rsp;
279 struct kretprobe_instance *ri;
281 if ((ri = get_free_rp_inst(rp)) != NULL) {
284 ri->ret_addr = (kprobe_opcode_t *) *sara;
286 /* Replace the return addr with trampoline addr */
287 *sara = (unsigned long) &kretprobe_trampoline;
296 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
297 * remain disabled thorough out this function.
299 int __kprobes kprobe_handler(struct pt_regs *regs)
303 kprobe_opcode_t *addr = (kprobe_opcode_t *)(regs->rip - sizeof(kprobe_opcode_t));
305 /* We're in an interrupt, but this is clear and BUG()-safe. */
308 /* Check we're not actually recursing */
309 if (kprobe_running()) {
310 /* We *are* holding lock here, so this is safe.
311 Disarm the probe we just hit, and ignore it. */
312 p = get_kprobe(addr);
314 if (kprobe_status == KPROBE_HIT_SS) {
315 regs->eflags &= ~TF_MASK;
316 regs->eflags |= kprobe_saved_rflags;
319 } else if (kprobe_status == KPROBE_HIT_SSDONE) {
320 /* TODO: Provide re-entrancy from
321 * post_kprobes_handler() and avoid exception
322 * stack corruption while single-stepping on
323 * the instruction of the new probe.
325 arch_disarm_kprobe(p);
326 regs->rip = (unsigned long)p->addr;
329 /* We have reentered the kprobe_handler(), since
330 * another probe was hit while within the
331 * handler. We here save the original kprobe
332 * variables and just single step on instruction
333 * of the new probe without calling any user
336 save_previous_kprobe();
337 set_current_kprobe(p, regs);
339 prepare_singlestep(p, regs);
340 kprobe_status = KPROBE_REENTER;
345 if (p->break_handler && p->break_handler(p, regs)) {
349 /* If it's not ours, can't be delete race, (we hold lock). */
354 p = get_kprobe(addr);
357 if (*addr != BREAKPOINT_INSTRUCTION) {
359 * The breakpoint instruction was removed right
360 * after we hit it. Another cpu has removed
361 * either a probepoint or a debugger breakpoint
362 * at this address. In either case, no further
363 * handling of this interrupt is appropriate.
367 /* Not one of ours: let kernel handle it */
371 kprobe_status = KPROBE_HIT_ACTIVE;
372 set_current_kprobe(p, regs);
374 if (p->pre_handler && p->pre_handler(p, regs))
375 /* handler has already set things up, so skip ss setup */
379 prepare_singlestep(p, regs);
380 kprobe_status = KPROBE_HIT_SS;
384 preempt_enable_no_resched();
389 * For function-return probes, init_kprobes() establishes a probepoint
390 * here. When a retprobed function returns, this probe is hit and
391 * trampoline_probe_handler() runs, calling the kretprobe's handler.
393 void kretprobe_trampoline_holder(void)
395 asm volatile ( ".global kretprobe_trampoline\n"
396 "kretprobe_trampoline: \n"
401 * Called when we hit the probe point at kretprobe_trampoline
403 int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
405 struct kretprobe_instance *ri = NULL;
406 struct hlist_head *head;
407 struct hlist_node *node, *tmp;
408 unsigned long orig_ret_address = 0;
409 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
411 head = kretprobe_inst_table_head(current);
414 * It is possible to have multiple instances associated with a given
415 * task either because an multiple functions in the call path
416 * have a return probe installed on them, and/or more then one return
417 * return probe was registered for a target function.
419 * We can handle this because:
420 * - instances are always inserted at the head of the list
421 * - when multiple return probes are registered for the same
422 * function, the first instance's ret_addr will point to the
423 * real return address, and all the rest will point to
424 * kretprobe_trampoline
426 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
427 if (ri->task != current)
428 /* another task is sharing our hash bucket */
431 if (ri->rp && ri->rp->handler)
432 ri->rp->handler(ri, regs);
434 orig_ret_address = (unsigned long)ri->ret_addr;
437 if (orig_ret_address != trampoline_address)
439 * This is the real return address. Any other
440 * instances associated with this task are for
441 * other calls deeper on the call stack
446 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
447 regs->rip = orig_ret_address;
450 preempt_enable_no_resched();
453 * By returning a non-zero value, we are telling
454 * kprobe_handler() that we have handled unlocking
455 * and re-enabling preemption.
461 * Called after single-stepping. p->addr is the address of the
462 * instruction whose first byte has been replaced by the "int 3"
463 * instruction. To avoid the SMP problems that can occur when we
464 * temporarily put back the original opcode to single-step, we
465 * single-stepped a copy of the instruction. The address of this
466 * copy is p->ainsn.insn.
468 * This function prepares to return from the post-single-step
469 * interrupt. We have to fix up the stack as follows:
471 * 0) Except in the case of absolute or indirect jump or call instructions,
472 * the new rip is relative to the copied instruction. We need to make
473 * it relative to the original instruction.
475 * 1) If the single-stepped instruction was pushfl, then the TF and IF
476 * flags are set in the just-pushed eflags, and may need to be cleared.
478 * 2) If the single-stepped instruction was a call, the return address
479 * that is atop the stack is the address following the copied instruction.
480 * We need to make it the address following the original instruction.
482 static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
484 unsigned long *tos = (unsigned long *)regs->rsp;
485 unsigned long next_rip = 0;
486 unsigned long copy_rip = (unsigned long)p->ainsn.insn;
487 unsigned long orig_rip = (unsigned long)p->addr;
488 kprobe_opcode_t *insn = p->ainsn.insn;
490 /*skip the REX prefix*/
491 if (*insn >= 0x40 && *insn <= 0x4f)
495 case 0x9c: /* pushfl */
496 *tos &= ~(TF_MASK | IF_MASK);
497 *tos |= kprobe_old_rflags;
499 case 0xc3: /* ret/lret */
503 regs->eflags &= ~TF_MASK;
504 /* rip is already adjusted, no more changes required*/
506 case 0xe8: /* call relative - Fix return addr */
507 *tos = orig_rip + (*tos - copy_rip);
510 if ((*insn & 0x30) == 0x10) {
511 /* call absolute, indirect */
512 /* Fix return addr; rip is correct. */
513 next_rip = regs->rip;
514 *tos = orig_rip + (*tos - copy_rip);
515 } else if (((*insn & 0x31) == 0x20) || /* jmp near, absolute indirect */
516 ((*insn & 0x31) == 0x21)) { /* jmp far, absolute indirect */
517 /* rip is correct. */
518 next_rip = regs->rip;
521 case 0xea: /* jmp absolute -- rip is correct */
522 next_rip = regs->rip;
528 regs->eflags &= ~TF_MASK;
530 regs->rip = next_rip;
532 regs->rip = orig_rip + (regs->rip - copy_rip);
537 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
538 * remain disabled thoroughout this function. And we hold kprobe lock.
540 int __kprobes post_kprobe_handler(struct pt_regs *regs)
542 if (!kprobe_running())
545 if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) {
546 kprobe_status = KPROBE_HIT_SSDONE;
547 current_kprobe->post_handler(current_kprobe, regs, 0);
550 resume_execution(current_kprobe, regs);
551 regs->eflags |= kprobe_saved_rflags;
553 /* Restore the original saved kprobes variables and continue. */
554 if (kprobe_status == KPROBE_REENTER) {
555 restore_previous_kprobe();
561 preempt_enable_no_resched();
564 * if somebody else is singlestepping across a probe point, eflags
565 * will have TF set, in which case, continue the remaining processing
566 * of do_debug, as if this is not a probe hit.
568 if (regs->eflags & TF_MASK)
574 /* Interrupts disabled, kprobe_lock held. */
575 int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
577 if (current_kprobe->fault_handler
578 && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
581 if (kprobe_status & KPROBE_HIT_SS) {
582 resume_execution(current_kprobe, regs);
583 regs->eflags |= kprobe_old_rflags;
586 preempt_enable_no_resched();
592 * Wrapper routine for handling exceptions.
594 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
595 unsigned long val, void *data)
597 struct die_args *args = (struct die_args *)data;
600 if (kprobe_handler(args->regs))
604 if (post_kprobe_handler(args->regs))
608 if (kprobe_running() &&
609 kprobe_fault_handler(args->regs, args->trapnr))
613 if (kprobe_running() &&
614 kprobe_fault_handler(args->regs, args->trapnr))
623 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
625 struct jprobe *jp = container_of(p, struct jprobe, kp);
628 jprobe_saved_regs = *regs;
629 jprobe_saved_rsp = (long *) regs->rsp;
630 addr = (unsigned long)jprobe_saved_rsp;
632 * As Linus pointed out, gcc assumes that the callee
633 * owns the argument space and could overwrite it, e.g.
634 * tailcall optimization. So, to be absolutely safe
635 * we also save and restore enough stack bytes to cover
638 memcpy(jprobes_stack, (kprobe_opcode_t *) addr, MIN_STACK_SIZE(addr));
639 regs->eflags &= ~IF_MASK;
640 regs->rip = (unsigned long)(jp->entry);
644 void __kprobes jprobe_return(void)
646 preempt_enable_no_resched();
647 asm volatile (" xchg %%rbx,%%rsp \n"
649 " .globl jprobe_return_end \n"
650 " jprobe_return_end: \n"
652 (jprobe_saved_rsp):"memory");
655 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
657 u8 *addr = (u8 *) (regs->rip - 1);
658 unsigned long stack_addr = (unsigned long)jprobe_saved_rsp;
659 struct jprobe *jp = container_of(p, struct jprobe, kp);
661 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
662 if ((long *)regs->rsp != jprobe_saved_rsp) {
663 struct pt_regs *saved_regs =
664 container_of(jprobe_saved_rsp, struct pt_regs, rsp);
665 printk("current rsp %p does not match saved rsp %p\n",
666 (long *)regs->rsp, jprobe_saved_rsp);
667 printk("Saved registers for jprobe %p\n", jp);
668 show_registers(saved_regs);
669 printk("Current registers\n");
670 show_registers(regs);
673 *regs = jprobe_saved_regs;
674 memcpy((kprobe_opcode_t *) stack_addr, jprobes_stack,
675 MIN_STACK_SIZE(stack_addr));
681 static struct kprobe trampoline_p = {
682 .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
683 .pre_handler = trampoline_probe_handler
686 int __init arch_init_kprobes(void)
688 return register_kprobe(&trampoline_p);