2 * Kernel Probes (KProbes)
3 * arch/i386/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 * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
27 * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
28 * <prasanna@in.ibm.com> added function-return probes.
31 #include <linux/config.h>
32 #include <linux/kprobes.h>
33 #include <linux/ptrace.h>
34 #include <linux/spinlock.h>
35 #include <linux/preempt.h>
36 #include <asm/cacheflush.h>
37 #include <asm/kdebug.h>
40 /* kprobe_status settings */
41 #define KPROBE_HIT_ACTIVE 0x00000001
42 #define KPROBE_HIT_SS 0x00000002
44 static struct kprobe *current_kprobe;
45 static unsigned long kprobe_status, kprobe_old_eflags, kprobe_saved_eflags;
46 static struct pt_regs jprobe_saved_regs;
47 static long *jprobe_saved_esp;
48 /* copy of the kernel stack at the probe fire time */
49 static kprobe_opcode_t jprobes_stack[MAX_STACK_SIZE];
50 void jprobe_return_end(void);
53 * returns non-zero if opcode modifies the interrupt flag.
55 static inline int is_IF_modifier(kprobe_opcode_t opcode)
60 case 0xcf: /* iret/iretd */
61 case 0x9d: /* popf/popfd */
67 int arch_prepare_kprobe(struct kprobe *p)
72 void arch_copy_kprobe(struct kprobe *p)
74 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
78 void arch_arm_kprobe(struct kprobe *p)
80 *p->addr = BREAKPOINT_INSTRUCTION;
81 flush_icache_range((unsigned long) p->addr,
82 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
85 void arch_disarm_kprobe(struct kprobe *p)
88 flush_icache_range((unsigned long) p->addr,
89 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
92 void arch_remove_kprobe(struct kprobe *p)
96 static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
98 regs->eflags |= TF_MASK;
99 regs->eflags &= ~IF_MASK;
100 /*single step inline if the instruction is an int3*/
101 if (p->opcode == BREAKPOINT_INSTRUCTION)
102 regs->eip = (unsigned long)p->addr;
104 regs->eip = (unsigned long)&p->ainsn.insn;
107 struct task_struct *arch_get_kprobe_task(void *ptr)
109 return ((struct thread_info *) (((unsigned long) ptr) &
110 (~(THREAD_SIZE -1))))->task;
113 void arch_prepare_kretprobe(struct kretprobe *rp, struct pt_regs *regs)
115 unsigned long *sara = (unsigned long *)®s->esp;
116 struct kretprobe_instance *ri;
117 static void *orig_ret_addr;
120 * Save the return address when the return probe hits
121 * the first time, and use it to populate the (krprobe
122 * instance)->ret_addr for subsequent return probes at
123 * the same addrress since stack address would have
124 * the kretprobe_trampoline by then.
126 if (((void*) *sara) != kretprobe_trampoline)
127 orig_ret_addr = (void*) *sara;
129 if ((ri = get_free_rp_inst(rp)) != NULL) {
131 ri->stack_addr = sara;
132 ri->ret_addr = orig_ret_addr;
134 /* Replace the return addr with trampoline addr */
135 *sara = (unsigned long) &kretprobe_trampoline;
141 void arch_kprobe_flush_task(struct task_struct *tk)
143 struct kretprobe_instance *ri;
144 while ((ri = get_rp_inst_tsk(tk)) != NULL) {
145 *((unsigned long *)(ri->stack_addr)) =
146 (unsigned long) ri->ret_addr;
152 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
153 * remain disabled thorough out this function.
155 static int kprobe_handler(struct pt_regs *regs)
159 kprobe_opcode_t *addr = NULL;
162 /* We're in an interrupt, but this is clear and BUG()-safe. */
164 /* Check if the application is using LDT entry for its code segment and
165 * calculate the address by reading the base address from the LDT entry.
167 if ((regs->xcs & 4) && (current->mm)) {
168 lp = (unsigned long *) ((unsigned long)((regs->xcs >> 3) * 8)
169 + (char *) current->mm->context.ldt);
170 addr = (kprobe_opcode_t *) (get_desc_base(lp) + regs->eip -
171 sizeof(kprobe_opcode_t));
173 addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
175 /* Check we're not actually recursing */
176 if (kprobe_running()) {
177 /* We *are* holding lock here, so this is safe.
178 Disarm the probe we just hit, and ignore it. */
179 p = get_kprobe(addr);
181 if (kprobe_status == KPROBE_HIT_SS) {
182 regs->eflags &= ~TF_MASK;
183 regs->eflags |= kprobe_saved_eflags;
187 arch_disarm_kprobe(p);
188 regs->eip = (unsigned long)p->addr;
192 if (p->break_handler && p->break_handler(p, regs)) {
196 /* If it's not ours, can't be delete race, (we hold lock). */
201 p = get_kprobe(addr);
204 if (regs->eflags & VM_MASK) {
205 /* We are in virtual-8086 mode. Return 0 */
209 if (*addr != BREAKPOINT_INSTRUCTION) {
211 * The breakpoint instruction was removed right
212 * after we hit it. Another cpu has removed
213 * either a probepoint or a debugger breakpoint
214 * at this address. In either case, no further
215 * handling of this interrupt is appropriate.
219 /* Not one of ours: let kernel handle it */
223 kprobe_status = KPROBE_HIT_ACTIVE;
225 kprobe_saved_eflags = kprobe_old_eflags
226 = (regs->eflags & (TF_MASK | IF_MASK));
227 if (is_IF_modifier(p->opcode))
228 kprobe_saved_eflags &= ~IF_MASK;
230 if (p->pre_handler && p->pre_handler(p, regs))
231 /* handler has already set things up, so skip ss setup */
235 prepare_singlestep(p, regs);
236 kprobe_status = KPROBE_HIT_SS;
240 preempt_enable_no_resched();
245 * For function-return probes, init_kprobes() establishes a probepoint
246 * here. When a retprobed function returns, this probe is hit and
247 * trampoline_probe_handler() runs, calling the kretprobe's handler.
249 void kretprobe_trampoline_holder(void)
251 asm volatile ( ".global kretprobe_trampoline\n"
252 "kretprobe_trampoline: \n"
257 * Called when we hit the probe point at kretprobe_trampoline
259 int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
261 struct task_struct *tsk;
262 struct kretprobe_instance *ri;
263 struct hlist_head *head;
264 struct hlist_node *node;
265 unsigned long *sara = ((unsigned long *) ®s->esp) - 1;
267 tsk = arch_get_kprobe_task(sara);
268 head = kretprobe_inst_table_head(tsk);
270 hlist_for_each_entry(ri, node, head, hlist) {
271 if (ri->stack_addr == sara && ri->rp) {
273 ri->rp->handler(ri, regs);
279 void trampoline_post_handler(struct kprobe *p, struct pt_regs *regs,
282 struct kretprobe_instance *ri;
283 /* RA already popped */
284 unsigned long *sara = ((unsigned long *)®s->esp) - 1;
286 while ((ri = get_rp_inst(sara))) {
287 regs->eip = (unsigned long)ri->ret_addr;
290 regs->eflags &= ~TF_MASK;
294 * Called after single-stepping. p->addr is the address of the
295 * instruction whose first byte has been replaced by the "int 3"
296 * instruction. To avoid the SMP problems that can occur when we
297 * temporarily put back the original opcode to single-step, we
298 * single-stepped a copy of the instruction. The address of this
299 * copy is p->ainsn.insn.
301 * This function prepares to return from the post-single-step
302 * interrupt. We have to fix up the stack as follows:
304 * 0) Except in the case of absolute or indirect jump or call instructions,
305 * the new eip is relative to the copied instruction. We need to make
306 * it relative to the original instruction.
308 * 1) If the single-stepped instruction was pushfl, then the TF and IF
309 * flags are set in the just-pushed eflags, and may need to be cleared.
311 * 2) If the single-stepped instruction was a call, the return address
312 * that is atop the stack is the address following the copied instruction.
313 * We need to make it the address following the original instruction.
315 static void resume_execution(struct kprobe *p, struct pt_regs *regs)
317 unsigned long *tos = (unsigned long *)®s->esp;
318 unsigned long next_eip = 0;
319 unsigned long copy_eip = (unsigned long)&p->ainsn.insn;
320 unsigned long orig_eip = (unsigned long)p->addr;
322 switch (p->ainsn.insn[0]) {
323 case 0x9c: /* pushfl */
324 *tos &= ~(TF_MASK | IF_MASK);
325 *tos |= kprobe_old_eflags;
327 case 0xc3: /* ret/lret */
331 regs->eflags &= ~TF_MASK;
332 /* eip is already adjusted, no more changes required*/
334 case 0xe8: /* call relative - Fix return addr */
335 *tos = orig_eip + (*tos - copy_eip);
338 if ((p->ainsn.insn[1] & 0x30) == 0x10) {
339 /* call absolute, indirect */
340 /* Fix return addr; eip is correct. */
341 next_eip = regs->eip;
342 *tos = orig_eip + (*tos - copy_eip);
343 } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
344 ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
345 /* eip is correct. */
346 next_eip = regs->eip;
349 case 0xea: /* jmp absolute -- eip is correct */
350 next_eip = regs->eip;
356 regs->eflags &= ~TF_MASK;
358 regs->eip = next_eip;
360 regs->eip = orig_eip + (regs->eip - copy_eip);
365 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
366 * remain disabled thoroughout this function. And we hold kprobe lock.
368 static inline int post_kprobe_handler(struct pt_regs *regs)
370 if (!kprobe_running())
373 if (current_kprobe->post_handler)
374 current_kprobe->post_handler(current_kprobe, regs, 0);
376 if (current_kprobe->post_handler != trampoline_post_handler)
377 resume_execution(current_kprobe, regs);
378 regs->eflags |= kprobe_saved_eflags;
381 preempt_enable_no_resched();
384 * if somebody else is singlestepping across a probe point, eflags
385 * will have TF set, in which case, continue the remaining processing
386 * of do_debug, as if this is not a probe hit.
388 if (regs->eflags & TF_MASK)
394 /* Interrupts disabled, kprobe_lock held. */
395 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
397 if (current_kprobe->fault_handler
398 && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
401 if (kprobe_status & KPROBE_HIT_SS) {
402 resume_execution(current_kprobe, regs);
403 regs->eflags |= kprobe_old_eflags;
406 preempt_enable_no_resched();
412 * Wrapper routine to for handling exceptions.
414 int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
417 struct die_args *args = (struct die_args *)data;
420 if (kprobe_handler(args->regs))
424 if (post_kprobe_handler(args->regs))
428 if (kprobe_running() &&
429 kprobe_fault_handler(args->regs, args->trapnr))
433 if (kprobe_running() &&
434 kprobe_fault_handler(args->regs, args->trapnr))
443 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
445 struct jprobe *jp = container_of(p, struct jprobe, kp);
448 jprobe_saved_regs = *regs;
449 jprobe_saved_esp = ®s->esp;
450 addr = (unsigned long)jprobe_saved_esp;
453 * TBD: As Linus pointed out, gcc assumes that the callee
454 * owns the argument space and could overwrite it, e.g.
455 * tailcall optimization. So, to be absolutely safe
456 * we also save and restore enough stack bytes to cover
459 memcpy(jprobes_stack, (kprobe_opcode_t *) addr, MIN_STACK_SIZE(addr));
460 regs->eflags &= ~IF_MASK;
461 regs->eip = (unsigned long)(jp->entry);
465 void jprobe_return(void)
467 preempt_enable_no_resched();
468 asm volatile (" xchgl %%ebx,%%esp \n"
470 " .globl jprobe_return_end \n"
471 " jprobe_return_end: \n"
473 (jprobe_saved_esp):"memory");
476 int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
478 u8 *addr = (u8 *) (regs->eip - 1);
479 unsigned long stack_addr = (unsigned long)jprobe_saved_esp;
480 struct jprobe *jp = container_of(p, struct jprobe, kp);
482 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
483 if (®s->esp != jprobe_saved_esp) {
484 struct pt_regs *saved_regs =
485 container_of(jprobe_saved_esp, struct pt_regs, esp);
486 printk("current esp %p does not match saved esp %p\n",
487 ®s->esp, jprobe_saved_esp);
488 printk("Saved registers for jprobe %p\n", jp);
489 show_registers(saved_regs);
490 printk("Current registers\n");
491 show_registers(regs);
494 *regs = jprobe_saved_regs;
495 memcpy((kprobe_opcode_t *) stack_addr, jprobes_stack,
496 MIN_STACK_SIZE(stack_addr));