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/kdebug.h>
39 /* kprobe_status settings */
40 #define KPROBE_HIT_ACTIVE 0x00000001
41 #define KPROBE_HIT_SS 0x00000002
43 static struct kprobe *current_kprobe;
44 static unsigned long kprobe_status, kprobe_old_eflags, kprobe_saved_eflags;
45 static struct pt_regs jprobe_saved_regs;
46 static long *jprobe_saved_esp;
47 /* copy of the kernel stack at the probe fire time */
48 static kprobe_opcode_t jprobes_stack[MAX_STACK_SIZE];
49 void jprobe_return_end(void);
52 * returns non-zero if opcode modifies the interrupt flag.
54 static inline int is_IF_modifier(kprobe_opcode_t opcode)
59 case 0xcf: /* iret/iretd */
60 case 0x9d: /* popf/popfd */
66 int arch_prepare_kprobe(struct kprobe *p)
71 void arch_copy_kprobe(struct kprobe *p)
73 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
76 void arch_remove_kprobe(struct kprobe *p)
80 static inline void disarm_kprobe(struct kprobe *p, struct pt_regs *regs)
83 regs->eip = (unsigned long)p->addr;
86 static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
88 regs->eflags |= TF_MASK;
89 regs->eflags &= ~IF_MASK;
90 /*single step inline if the instruction is an int3*/
91 if (p->opcode == BREAKPOINT_INSTRUCTION)
92 regs->eip = (unsigned long)p->addr;
94 regs->eip = (unsigned long)&p->ainsn.insn;
97 struct task_struct *arch_get_kprobe_task(void *ptr)
99 return ((struct thread_info *) (((unsigned long) ptr) &
100 (~(THREAD_SIZE -1))))->task;
103 void arch_prepare_kretprobe(struct kretprobe *rp, struct pt_regs *regs)
105 unsigned long *sara = (unsigned long *)®s->esp;
106 struct kretprobe_instance *ri;
107 static void *orig_ret_addr;
110 * Save the return address when the return probe hits
111 * the first time, and use it to populate the (krprobe
112 * instance)->ret_addr for subsequent return probes at
113 * the same addrress since stack address would have
114 * the kretprobe_trampoline by then.
116 if (((void*) *sara) != kretprobe_trampoline)
117 orig_ret_addr = (void*) *sara;
119 if ((ri = get_free_rp_inst(rp)) != NULL) {
121 ri->stack_addr = sara;
122 ri->ret_addr = orig_ret_addr;
124 /* Replace the return addr with trampoline addr */
125 *sara = (unsigned long) &kretprobe_trampoline;
131 void arch_kprobe_flush_task(struct task_struct *tk, spinlock_t *kp_lock)
133 unsigned long flags = 0;
134 struct kretprobe_instance *ri;
135 spin_lock_irqsave(kp_lock, flags);
136 while ((ri = get_rp_inst_tsk(tk)) != NULL) {
137 *((unsigned long *)(ri->stack_addr)) =
138 (unsigned long) ri->ret_addr;
141 spin_unlock_irqrestore(kp_lock, flags);
145 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
146 * remain disabled thorough out this function.
148 static int kprobe_handler(struct pt_regs *regs)
152 kprobe_opcode_t *addr = NULL;
155 /* We're in an interrupt, but this is clear and BUG()-safe. */
157 /* Check if the application is using LDT entry for its code segment and
158 * calculate the address by reading the base address from the LDT entry.
160 if ((regs->xcs & 4) && (current->mm)) {
161 lp = (unsigned long *) ((unsigned long)((regs->xcs >> 3) * 8)
162 + (char *) current->mm->context.ldt);
163 addr = (kprobe_opcode_t *) (get_desc_base(lp) + regs->eip -
164 sizeof(kprobe_opcode_t));
166 addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
168 /* Check we're not actually recursing */
169 if (kprobe_running()) {
170 /* We *are* holding lock here, so this is safe.
171 Disarm the probe we just hit, and ignore it. */
172 p = get_kprobe(addr);
174 if (kprobe_status == KPROBE_HIT_SS) {
175 regs->eflags &= ~TF_MASK;
176 regs->eflags |= kprobe_saved_eflags;
180 disarm_kprobe(p, regs);
184 if (p->break_handler && p->break_handler(p, regs)) {
188 /* If it's not ours, can't be delete race, (we hold lock). */
193 p = get_kprobe(addr);
196 if (regs->eflags & VM_MASK) {
197 /* We are in virtual-8086 mode. Return 0 */
201 if (*addr != BREAKPOINT_INSTRUCTION) {
203 * The breakpoint instruction was removed right
204 * after we hit it. Another cpu has removed
205 * either a probepoint or a debugger breakpoint
206 * at this address. In either case, no further
207 * handling of this interrupt is appropriate.
211 /* Not one of ours: let kernel handle it */
215 kprobe_status = KPROBE_HIT_ACTIVE;
217 kprobe_saved_eflags = kprobe_old_eflags
218 = (regs->eflags & (TF_MASK | IF_MASK));
219 if (is_IF_modifier(p->opcode))
220 kprobe_saved_eflags &= ~IF_MASK;
222 if (p->pre_handler && p->pre_handler(p, regs))
223 /* handler has already set things up, so skip ss setup */
227 prepare_singlestep(p, regs);
228 kprobe_status = KPROBE_HIT_SS;
232 preempt_enable_no_resched();
237 * For function-return probes, init_kprobes() establishes a probepoint
238 * here. When a retprobed function returns, this probe is hit and
239 * trampoline_probe_handler() runs, calling the kretprobe's handler.
241 void kretprobe_trampoline_holder(void)
243 asm volatile ( ".global kretprobe_trampoline\n"
244 "kretprobe_trampoline: \n"
249 * Called when we hit the probe point at kretprobe_trampoline
251 int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
253 struct task_struct *tsk;
254 struct kretprobe_instance *ri;
255 struct hlist_head *head;
256 struct hlist_node *node;
257 unsigned long *sara = ((unsigned long *) ®s->esp) - 1;
259 tsk = arch_get_kprobe_task(sara);
260 head = kretprobe_inst_table_head(tsk);
262 hlist_for_each_entry(ri, node, head, hlist) {
263 if (ri->stack_addr == sara && ri->rp) {
265 ri->rp->handler(ri, regs);
271 void trampoline_post_handler(struct kprobe *p, struct pt_regs *regs,
274 struct kretprobe_instance *ri;
275 /* RA already popped */
276 unsigned long *sara = ((unsigned long *)®s->esp) - 1;
278 while ((ri = get_rp_inst(sara))) {
279 regs->eip = (unsigned long)ri->ret_addr;
282 regs->eflags &= ~TF_MASK;
286 * Called after single-stepping. p->addr is the address of the
287 * instruction whose first byte has been replaced by the "int 3"
288 * instruction. To avoid the SMP problems that can occur when we
289 * temporarily put back the original opcode to single-step, we
290 * single-stepped a copy of the instruction. The address of this
291 * copy is p->ainsn.insn.
293 * This function prepares to return from the post-single-step
294 * interrupt. We have to fix up the stack as follows:
296 * 0) Except in the case of absolute or indirect jump or call instructions,
297 * the new eip is relative to the copied instruction. We need to make
298 * it relative to the original instruction.
300 * 1) If the single-stepped instruction was pushfl, then the TF and IF
301 * flags are set in the just-pushed eflags, and may need to be cleared.
303 * 2) If the single-stepped instruction was a call, the return address
304 * that is atop the stack is the address following the copied instruction.
305 * We need to make it the address following the original instruction.
307 static void resume_execution(struct kprobe *p, struct pt_regs *regs)
309 unsigned long *tos = (unsigned long *)®s->esp;
310 unsigned long next_eip = 0;
311 unsigned long copy_eip = (unsigned long)&p->ainsn.insn;
312 unsigned long orig_eip = (unsigned long)p->addr;
314 switch (p->ainsn.insn[0]) {
315 case 0x9c: /* pushfl */
316 *tos &= ~(TF_MASK | IF_MASK);
317 *tos |= kprobe_old_eflags;
319 case 0xc3: /* ret/lret */
323 regs->eflags &= ~TF_MASK;
324 /* eip is already adjusted, no more changes required*/
326 case 0xe8: /* call relative - Fix return addr */
327 *tos = orig_eip + (*tos - copy_eip);
330 if ((p->ainsn.insn[1] & 0x30) == 0x10) {
331 /* call absolute, indirect */
332 /* Fix return addr; eip is correct. */
333 next_eip = regs->eip;
334 *tos = orig_eip + (*tos - copy_eip);
335 } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
336 ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
337 /* eip is correct. */
338 next_eip = regs->eip;
341 case 0xea: /* jmp absolute -- eip is correct */
342 next_eip = regs->eip;
348 regs->eflags &= ~TF_MASK;
350 regs->eip = next_eip;
352 regs->eip = orig_eip + (regs->eip - copy_eip);
357 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
358 * remain disabled thoroughout this function. And we hold kprobe lock.
360 static inline int post_kprobe_handler(struct pt_regs *regs)
362 if (!kprobe_running())
365 if (current_kprobe->post_handler)
366 current_kprobe->post_handler(current_kprobe, regs, 0);
368 if (current_kprobe->post_handler != trampoline_post_handler)
369 resume_execution(current_kprobe, regs);
370 regs->eflags |= kprobe_saved_eflags;
373 preempt_enable_no_resched();
376 * if somebody else is singlestepping across a probe point, eflags
377 * will have TF set, in which case, continue the remaining processing
378 * of do_debug, as if this is not a probe hit.
380 if (regs->eflags & TF_MASK)
386 /* Interrupts disabled, kprobe_lock held. */
387 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
389 if (current_kprobe->fault_handler
390 && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
393 if (kprobe_status & KPROBE_HIT_SS) {
394 resume_execution(current_kprobe, regs);
395 regs->eflags |= kprobe_old_eflags;
398 preempt_enable_no_resched();
404 * Wrapper routine to for handling exceptions.
406 int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
409 struct die_args *args = (struct die_args *)data;
412 if (kprobe_handler(args->regs))
416 if (post_kprobe_handler(args->regs))
420 if (kprobe_running() &&
421 kprobe_fault_handler(args->regs, args->trapnr))
425 if (kprobe_running() &&
426 kprobe_fault_handler(args->regs, args->trapnr))
435 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
437 struct jprobe *jp = container_of(p, struct jprobe, kp);
440 jprobe_saved_regs = *regs;
441 jprobe_saved_esp = ®s->esp;
442 addr = (unsigned long)jprobe_saved_esp;
445 * TBD: As Linus pointed out, gcc assumes that the callee
446 * owns the argument space and could overwrite it, e.g.
447 * tailcall optimization. So, to be absolutely safe
448 * we also save and restore enough stack bytes to cover
451 memcpy(jprobes_stack, (kprobe_opcode_t *) addr, MIN_STACK_SIZE(addr));
452 regs->eflags &= ~IF_MASK;
453 regs->eip = (unsigned long)(jp->entry);
457 void jprobe_return(void)
459 preempt_enable_no_resched();
460 asm volatile (" xchgl %%ebx,%%esp \n"
462 " .globl jprobe_return_end \n"
463 " jprobe_return_end: \n"
465 (jprobe_saved_esp):"memory");
468 int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
470 u8 *addr = (u8 *) (regs->eip - 1);
471 unsigned long stack_addr = (unsigned long)jprobe_saved_esp;
472 struct jprobe *jp = container_of(p, struct jprobe, kp);
474 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
475 if (®s->esp != jprobe_saved_esp) {
476 struct pt_regs *saved_regs =
477 container_of(jprobe_saved_esp, struct pt_regs, esp);
478 printk("current esp %p does not match saved esp %p\n",
479 ®s->esp, jprobe_saved_esp);
480 printk("Saved registers for jprobe %p\n", jp);
481 show_registers(saved_regs);
482 printk("Current registers\n");
483 show_registers(regs);
486 *regs = jprobe_saved_regs;
487 memcpy((kprobe_opcode_t *) stack_addr, jprobes_stack,
488 MIN_STACK_SIZE(stack_addr));