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
30 #include <linux/config.h>
31 #include <linux/kprobes.h>
32 #include <linux/ptrace.h>
33 #include <linux/spinlock.h>
34 #include <linux/string.h>
35 #include <linux/slab.h>
36 #include <linux/preempt.h>
37 #include <linux/vmalloc.h>
39 #include <asm/pgtable.h>
40 #include <asm/kdebug.h>
42 /* kprobe_status settings */
43 #define KPROBE_HIT_ACTIVE 0x00000001
44 #define KPROBE_HIT_SS 0x00000002
46 static struct kprobe *current_kprobe;
47 static unsigned long kprobe_status, kprobe_old_rflags, kprobe_saved_rflags;
48 static struct pt_regs jprobe_saved_regs;
49 static long *jprobe_saved_rsp;
50 static kprobe_opcode_t *get_insn_slot(void);
51 static void free_insn_slot(kprobe_opcode_t *slot);
52 void jprobe_return_end(void);
54 /* copy of the kernel stack at the probe fire time */
55 static kprobe_opcode_t jprobes_stack[MAX_STACK_SIZE];
58 * returns non-zero if opcode modifies the interrupt flag.
60 static inline int is_IF_modifier(kprobe_opcode_t *insn)
65 case 0xcf: /* iret/iretd */
66 case 0x9d: /* popf/popfd */
70 if (*insn >= 0x40 && *insn <= 0x4f && *++insn == 0xcf)
75 int arch_prepare_kprobe(struct kprobe *p)
77 /* insn: must be on special executable page on x86_64. */
78 p->ainsn.insn = get_insn_slot();
82 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE);
86 void arch_remove_kprobe(struct kprobe *p)
88 free_insn_slot(p->ainsn.insn);
91 static inline void disarm_kprobe(struct kprobe *p, struct pt_regs *regs)
94 regs->rip = (unsigned long)p->addr;
97 static void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
99 regs->eflags |= TF_MASK;
100 regs->eflags &= ~IF_MASK;
102 regs->rip = (unsigned long)p->ainsn.insn;
106 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
107 * remain disabled thorough out this function.
109 int kprobe_handler(struct pt_regs *regs)
113 kprobe_opcode_t *addr = (kprobe_opcode_t *)(regs->rip - sizeof(kprobe_opcode_t));
115 /* We're in an interrupt, but this is clear and BUG()-safe. */
118 /* Check we're not actually recursing */
119 if (kprobe_running()) {
120 /* We *are* holding lock here, so this is safe.
121 Disarm the probe we just hit, and ignore it. */
122 p = get_kprobe(addr);
124 disarm_kprobe(p, regs);
128 if (p->break_handler && p->break_handler(p, regs)) {
132 /* If it's not ours, can't be delete race, (we hold lock). */
137 p = get_kprobe(addr);
140 if (*addr != BREAKPOINT_INSTRUCTION) {
142 * The breakpoint instruction was removed right
143 * after we hit it. Another cpu has removed
144 * either a probepoint or a debugger breakpoint
145 * at this address. In either case, no further
146 * handling of this interrupt is appropriate.
150 /* Not one of ours: let kernel handle it */
154 kprobe_status = KPROBE_HIT_ACTIVE;
156 kprobe_saved_rflags = kprobe_old_rflags
157 = (regs->eflags & (TF_MASK | IF_MASK));
158 if (is_IF_modifier(p->ainsn.insn))
159 kprobe_saved_rflags &= ~IF_MASK;
161 if (p->pre_handler(p, regs)) {
162 /* handler has already set things up, so skip ss setup */
167 prepare_singlestep(p, regs);
168 kprobe_status = KPROBE_HIT_SS;
172 preempt_enable_no_resched();
177 * Called after single-stepping. p->addr is the address of the
178 * instruction whose first byte has been replaced by the "int 3"
179 * instruction. To avoid the SMP problems that can occur when we
180 * temporarily put back the original opcode to single-step, we
181 * single-stepped a copy of the instruction. The address of this
182 * copy is p->ainsn.insn.
184 * This function prepares to return from the post-single-step
185 * interrupt. We have to fix up the stack as follows:
187 * 0) Except in the case of absolute or indirect jump or call instructions,
188 * the new rip is relative to the copied instruction. We need to make
189 * it relative to the original instruction.
191 * 1) If the single-stepped instruction was pushfl, then the TF and IF
192 * flags are set in the just-pushed eflags, and may need to be cleared.
194 * 2) If the single-stepped instruction was a call, the return address
195 * that is atop the stack is the address following the copied instruction.
196 * We need to make it the address following the original instruction.
198 static void resume_execution(struct kprobe *p, struct pt_regs *regs)
200 unsigned long *tos = (unsigned long *)regs->rsp;
201 unsigned long next_rip = 0;
202 unsigned long copy_rip = (unsigned long)p->ainsn.insn;
203 unsigned long orig_rip = (unsigned long)p->addr;
204 kprobe_opcode_t *insn = p->ainsn.insn;
206 /*skip the REX prefix*/
207 if (*insn >= 0x40 && *insn <= 0x4f)
211 case 0x9c: /* pushfl */
212 *tos &= ~(TF_MASK | IF_MASK);
213 *tos |= kprobe_old_rflags;
215 case 0xe8: /* call relative - Fix return addr */
216 *tos = orig_rip + (*tos - copy_rip);
219 if ((*insn & 0x30) == 0x10) {
220 /* call absolute, indirect */
221 /* Fix return addr; rip is correct. */
222 next_rip = regs->rip;
223 *tos = orig_rip + (*tos - copy_rip);
224 } else if (((*insn & 0x31) == 0x20) || /* jmp near, absolute indirect */
225 ((*insn & 0x31) == 0x21)) { /* jmp far, absolute indirect */
226 /* rip is correct. */
227 next_rip = regs->rip;
230 case 0xea: /* jmp absolute -- rip is correct */
231 next_rip = regs->rip;
237 regs->eflags &= ~TF_MASK;
239 regs->rip = next_rip;
241 regs->rip = orig_rip + (regs->rip - copy_rip);
246 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
247 * remain disabled thoroughout this function. And we hold kprobe lock.
249 int post_kprobe_handler(struct pt_regs *regs)
251 if (!kprobe_running())
254 if (current_kprobe->post_handler)
255 current_kprobe->post_handler(current_kprobe, regs, 0);
257 resume_execution(current_kprobe, regs);
258 regs->eflags |= kprobe_saved_rflags;
261 preempt_enable_no_resched();
264 * if somebody else is singlestepping across a probe point, eflags
265 * will have TF set, in which case, continue the remaining processing
266 * of do_debug, as if this is not a probe hit.
268 if (regs->eflags & TF_MASK)
274 /* Interrupts disabled, kprobe_lock held. */
275 int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
277 if (current_kprobe->fault_handler
278 && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
281 if (kprobe_status & KPROBE_HIT_SS) {
282 resume_execution(current_kprobe, regs);
283 regs->eflags |= kprobe_old_rflags;
286 preempt_enable_no_resched();
292 * Wrapper routine for handling exceptions.
294 int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
297 struct die_args *args = (struct die_args *)data;
300 if (kprobe_handler(args->regs))
304 if (post_kprobe_handler(args->regs))
308 if (kprobe_running() &&
309 kprobe_fault_handler(args->regs, args->trapnr))
313 if (kprobe_running() &&
314 kprobe_fault_handler(args->regs, args->trapnr))
323 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
325 struct jprobe *jp = container_of(p, struct jprobe, kp);
328 jprobe_saved_regs = *regs;
329 jprobe_saved_rsp = (long *) regs->rsp;
330 addr = (unsigned long)jprobe_saved_rsp;
332 * As Linus pointed out, gcc assumes that the callee
333 * owns the argument space and could overwrite it, e.g.
334 * tailcall optimization. So, to be absolutely safe
335 * we also save and restore enough stack bytes to cover
338 memcpy(jprobes_stack, (kprobe_opcode_t *) addr, MIN_STACK_SIZE(addr));
339 regs->eflags &= ~IF_MASK;
340 regs->rip = (unsigned long)(jp->entry);
344 void jprobe_return(void)
346 preempt_enable_no_resched();
347 asm volatile (" xchg %%rbx,%%rsp \n"
349 " .globl jprobe_return_end \n"
350 " jprobe_return_end: \n"
352 (jprobe_saved_rsp):"memory");
355 int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
357 u8 *addr = (u8 *) (regs->rip - 1);
358 unsigned long stack_addr = (unsigned long)jprobe_saved_rsp;
359 struct jprobe *jp = container_of(p, struct jprobe, kp);
361 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
362 if ((long *)regs->rsp != jprobe_saved_rsp) {
363 struct pt_regs *saved_regs =
364 container_of(jprobe_saved_rsp, struct pt_regs, rsp);
365 printk("current rsp %p does not match saved rsp %p\n",
366 (long *)regs->rsp, jprobe_saved_rsp);
367 printk("Saved registers for jprobe %p\n", jp);
368 show_registers(saved_regs);
369 printk("Current registers\n");
370 show_registers(regs);
373 *regs = jprobe_saved_regs;
374 memcpy((kprobe_opcode_t *) stack_addr, jprobes_stack,
375 MIN_STACK_SIZE(stack_addr));
382 * kprobe->ainsn.insn points to the copy of the instruction to be single-stepped.
383 * By default on x86_64, pages we get from kmalloc or vmalloc are not
384 * executable. Single-stepping an instruction on such a page yields an
385 * oops. So instead of storing the instruction copies in their respective
386 * kprobe objects, we allocate a page, map it executable, and store all the
387 * instruction copies there. (We can allocate additional pages if somebody
388 * inserts a huge number of probes.) Each page can hold up to INSNS_PER_PAGE
389 * instruction slots, each of which is MAX_INSN_SIZE*sizeof(kprobe_opcode_t)
392 #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE*sizeof(kprobe_opcode_t)))
393 struct kprobe_insn_page {
394 struct hlist_node hlist;
395 kprobe_opcode_t *insns; /* page of instruction slots */
396 char slot_used[INSNS_PER_PAGE];
400 static struct hlist_head kprobe_insn_pages;
403 * get_insn_slot() - Find a slot on an executable page for an instruction.
404 * We allocate an executable page if there's no room on existing ones.
406 static kprobe_opcode_t *get_insn_slot(void)
408 struct kprobe_insn_page *kip;
409 struct hlist_node *pos;
411 hlist_for_each(pos, &kprobe_insn_pages) {
412 kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
413 if (kip->nused < INSNS_PER_PAGE) {
415 for (i = 0; i < INSNS_PER_PAGE; i++) {
416 if (!kip->slot_used[i]) {
417 kip->slot_used[i] = 1;
419 return kip->insns + (i*MAX_INSN_SIZE);
422 /* Surprise! No unused slots. Fix kip->nused. */
423 kip->nused = INSNS_PER_PAGE;
427 /* All out of space. Need to allocate a new page. Use slot 0.*/
428 kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_ATOMIC);
432 kip->insns = (kprobe_opcode_t*) __vmalloc(PAGE_SIZE,
433 GFP_ATOMIC|__GFP_HIGHMEM, __pgprot(__PAGE_KERNEL_EXEC));
438 INIT_HLIST_NODE(&kip->hlist);
439 hlist_add_head(&kip->hlist, &kprobe_insn_pages);
440 memset(kip->slot_used, 0, INSNS_PER_PAGE);
441 kip->slot_used[0] = 1;
447 * free_insn_slot() - Free instruction slot obtained from get_insn_slot().
449 static void free_insn_slot(kprobe_opcode_t *slot)
451 struct kprobe_insn_page *kip;
452 struct hlist_node *pos;
454 hlist_for_each(pos, &kprobe_insn_pages) {
455 kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
456 if (kip->insns <= slot
457 && slot < kip->insns+(INSNS_PER_PAGE*MAX_INSN_SIZE)) {
458 int i = (slot - kip->insns) / MAX_INSN_SIZE;
459 kip->slot_used[i] = 0;
461 if (kip->nused == 0) {
463 * Page is no longer in use. Free it unless
464 * it's the last one. We keep the last one
465 * so as not to have to set it up again the
466 * next time somebody inserts a probe.
468 hlist_del(&kip->hlist);
469 if (hlist_empty(&kprobe_insn_pages)) {
470 INIT_HLIST_NODE(&kip->hlist);
471 hlist_add_head(&kip->hlist,