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 static struct kprobe *current_kprobe;
41 static unsigned long kprobe_status, kprobe_old_eflags, kprobe_saved_eflags;
42 static struct kprobe *kprobe_prev;
43 static unsigned long kprobe_status_prev, kprobe_old_eflags_prev, kprobe_saved_eflags_prev;
44 static struct pt_regs jprobe_saved_regs;
45 static long *jprobe_saved_esp;
46 /* copy of the kernel stack at the probe fire time */
47 static kprobe_opcode_t jprobes_stack[MAX_STACK_SIZE];
48 void jprobe_return_end(void);
51 * returns non-zero if opcode modifies the interrupt flag.
53 static inline int is_IF_modifier(kprobe_opcode_t opcode)
58 case 0xcf: /* iret/iretd */
59 case 0x9d: /* popf/popfd */
65 int __kprobes arch_prepare_kprobe(struct kprobe *p)
70 void __kprobes arch_copy_kprobe(struct kprobe *p)
72 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
76 void __kprobes arch_arm_kprobe(struct kprobe *p)
78 *p->addr = BREAKPOINT_INSTRUCTION;
79 flush_icache_range((unsigned long) p->addr,
80 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
83 void __kprobes arch_disarm_kprobe(struct kprobe *p)
86 flush_icache_range((unsigned long) p->addr,
87 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
90 void __kprobes arch_remove_kprobe(struct kprobe *p)
94 static inline void save_previous_kprobe(void)
96 kprobe_prev = current_kprobe;
97 kprobe_status_prev = kprobe_status;
98 kprobe_old_eflags_prev = kprobe_old_eflags;
99 kprobe_saved_eflags_prev = kprobe_saved_eflags;
102 static inline void restore_previous_kprobe(void)
104 current_kprobe = kprobe_prev;
105 kprobe_status = kprobe_status_prev;
106 kprobe_old_eflags = kprobe_old_eflags_prev;
107 kprobe_saved_eflags = kprobe_saved_eflags_prev;
110 static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs)
113 kprobe_saved_eflags = kprobe_old_eflags
114 = (regs->eflags & (TF_MASK | IF_MASK));
115 if (is_IF_modifier(p->opcode))
116 kprobe_saved_eflags &= ~IF_MASK;
119 static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
121 regs->eflags |= TF_MASK;
122 regs->eflags &= ~IF_MASK;
123 /*single step inline if the instruction is an int3*/
124 if (p->opcode == BREAKPOINT_INSTRUCTION)
125 regs->eip = (unsigned long)p->addr;
127 regs->eip = (unsigned long)&p->ainsn.insn;
130 void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
131 struct pt_regs *regs)
133 unsigned long *sara = (unsigned long *)®s->esp;
134 struct kretprobe_instance *ri;
136 if ((ri = get_free_rp_inst(rp)) != NULL) {
139 ri->ret_addr = (kprobe_opcode_t *) *sara;
141 /* Replace the return addr with trampoline addr */
142 *sara = (unsigned long) &kretprobe_trampoline;
151 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
152 * remain disabled thorough out this function.
154 static int __kprobes kprobe_handler(struct pt_regs *regs)
158 kprobe_opcode_t *addr = NULL;
161 /* We're in an interrupt, but this is clear and BUG()-safe. */
163 /* Check if the application is using LDT entry for its code segment and
164 * calculate the address by reading the base address from the LDT entry.
166 if ((regs->xcs & 4) && (current->mm)) {
167 lp = (unsigned long *) ((unsigned long)((regs->xcs >> 3) * 8)
168 + (char *) current->mm->context.ldt);
169 addr = (kprobe_opcode_t *) (get_desc_base(lp) + regs->eip -
170 sizeof(kprobe_opcode_t));
172 addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
174 /* Check we're not actually recursing */
175 if (kprobe_running()) {
176 /* We *are* holding lock here, so this is safe.
177 Disarm the probe we just hit, and ignore it. */
178 p = get_kprobe(addr);
180 if (kprobe_status == KPROBE_HIT_SS) {
181 regs->eflags &= ~TF_MASK;
182 regs->eflags |= kprobe_saved_eflags;
186 /* We have reentered the kprobe_handler(), since
187 * another probe was hit while within the handler.
188 * We here save the original kprobes variables and
189 * just single step on the instruction of the new probe
190 * without calling any user handlers.
192 save_previous_kprobe();
193 set_current_kprobe(p, regs);
195 prepare_singlestep(p, regs);
196 kprobe_status = KPROBE_REENTER;
200 if (p->break_handler && p->break_handler(p, regs)) {
204 /* If it's not ours, can't be delete race, (we hold lock). */
209 p = get_kprobe(addr);
212 if (regs->eflags & VM_MASK) {
213 /* We are in virtual-8086 mode. Return 0 */
217 if (*addr != BREAKPOINT_INSTRUCTION) {
219 * The breakpoint instruction was removed right
220 * after we hit it. Another cpu has removed
221 * either a probepoint or a debugger breakpoint
222 * at this address. In either case, no further
223 * handling of this interrupt is appropriate.
224 * Back up over the (now missing) int3 and run
225 * the original instruction.
227 regs->eip -= sizeof(kprobe_opcode_t);
230 /* Not one of ours: let kernel handle it */
234 kprobe_status = KPROBE_HIT_ACTIVE;
235 set_current_kprobe(p, regs);
237 if (p->pre_handler && p->pre_handler(p, regs))
238 /* handler has already set things up, so skip ss setup */
242 prepare_singlestep(p, regs);
243 kprobe_status = KPROBE_HIT_SS;
247 preempt_enable_no_resched();
252 * For function-return probes, init_kprobes() establishes a probepoint
253 * here. When a retprobed function returns, this probe is hit and
254 * trampoline_probe_handler() runs, calling the kretprobe's handler.
256 void kretprobe_trampoline_holder(void)
258 asm volatile ( ".global kretprobe_trampoline\n"
259 "kretprobe_trampoline: \n"
264 * Called when we hit the probe point at kretprobe_trampoline
266 int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
268 struct kretprobe_instance *ri = NULL;
269 struct hlist_head *head;
270 struct hlist_node *node, *tmp;
271 unsigned long orig_ret_address = 0;
272 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
274 head = kretprobe_inst_table_head(current);
277 * It is possible to have multiple instances associated with a given
278 * task either because an multiple functions in the call path
279 * have a return probe installed on them, and/or more then one return
280 * return probe was registered for a target function.
282 * We can handle this because:
283 * - instances are always inserted at the head of the list
284 * - when multiple return probes are registered for the same
285 * function, the first instance's ret_addr will point to the
286 * real return address, and all the rest will point to
287 * kretprobe_trampoline
289 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
290 if (ri->task != current)
291 /* another task is sharing our hash bucket */
294 if (ri->rp && ri->rp->handler)
295 ri->rp->handler(ri, regs);
297 orig_ret_address = (unsigned long)ri->ret_addr;
300 if (orig_ret_address != trampoline_address)
302 * This is the real return address. Any other
303 * instances associated with this task are for
304 * other calls deeper on the call stack
309 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
310 regs->eip = orig_ret_address;
313 preempt_enable_no_resched();
316 * By returning a non-zero value, we are telling
317 * kprobe_handler() that we have handled unlocking
318 * and re-enabling preemption.
324 * Called after single-stepping. p->addr is the address of the
325 * instruction whose first byte has been replaced by the "int 3"
326 * instruction. To avoid the SMP problems that can occur when we
327 * temporarily put back the original opcode to single-step, we
328 * single-stepped a copy of the instruction. The address of this
329 * copy is p->ainsn.insn.
331 * This function prepares to return from the post-single-step
332 * interrupt. We have to fix up the stack as follows:
334 * 0) Except in the case of absolute or indirect jump or call instructions,
335 * the new eip is relative to the copied instruction. We need to make
336 * it relative to the original instruction.
338 * 1) If the single-stepped instruction was pushfl, then the TF and IF
339 * flags are set in the just-pushed eflags, and may need to be cleared.
341 * 2) If the single-stepped instruction was a call, the return address
342 * that is atop the stack is the address following the copied instruction.
343 * We need to make it the address following the original instruction.
345 static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
347 unsigned long *tos = (unsigned long *)®s->esp;
348 unsigned long next_eip = 0;
349 unsigned long copy_eip = (unsigned long)&p->ainsn.insn;
350 unsigned long orig_eip = (unsigned long)p->addr;
352 switch (p->ainsn.insn[0]) {
353 case 0x9c: /* pushfl */
354 *tos &= ~(TF_MASK | IF_MASK);
355 *tos |= kprobe_old_eflags;
357 case 0xc3: /* ret/lret */
361 regs->eflags &= ~TF_MASK;
362 /* eip is already adjusted, no more changes required*/
364 case 0xe8: /* call relative - Fix return addr */
365 *tos = orig_eip + (*tos - copy_eip);
368 if ((p->ainsn.insn[1] & 0x30) == 0x10) {
369 /* call absolute, indirect */
370 /* Fix return addr; eip is correct. */
371 next_eip = regs->eip;
372 *tos = orig_eip + (*tos - copy_eip);
373 } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
374 ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
375 /* eip is correct. */
376 next_eip = regs->eip;
379 case 0xea: /* jmp absolute -- eip is correct */
380 next_eip = regs->eip;
386 regs->eflags &= ~TF_MASK;
388 regs->eip = next_eip;
390 regs->eip = orig_eip + (regs->eip - copy_eip);
395 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
396 * remain disabled thoroughout this function. And we hold kprobe lock.
398 static inline int post_kprobe_handler(struct pt_regs *regs)
400 if (!kprobe_running())
403 if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) {
404 kprobe_status = KPROBE_HIT_SSDONE;
405 current_kprobe->post_handler(current_kprobe, regs, 0);
408 resume_execution(current_kprobe, regs);
409 regs->eflags |= kprobe_saved_eflags;
411 /*Restore back the original saved kprobes variables and continue. */
412 if (kprobe_status == KPROBE_REENTER) {
413 restore_previous_kprobe();
418 preempt_enable_no_resched();
421 * if somebody else is singlestepping across a probe point, eflags
422 * will have TF set, in which case, continue the remaining processing
423 * of do_debug, as if this is not a probe hit.
425 if (regs->eflags & TF_MASK)
431 /* Interrupts disabled, kprobe_lock held. */
432 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
434 if (current_kprobe->fault_handler
435 && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
438 if (kprobe_status & KPROBE_HIT_SS) {
439 resume_execution(current_kprobe, regs);
440 regs->eflags |= kprobe_old_eflags;
443 preempt_enable_no_resched();
449 * Wrapper routine to for handling exceptions.
451 int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
452 unsigned long val, void *data)
454 struct die_args *args = (struct die_args *)data;
457 if (kprobe_handler(args->regs))
461 if (post_kprobe_handler(args->regs))
465 if (kprobe_running() &&
466 kprobe_fault_handler(args->regs, args->trapnr))
470 if (kprobe_running() &&
471 kprobe_fault_handler(args->regs, args->trapnr))
480 int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
482 struct jprobe *jp = container_of(p, struct jprobe, kp);
485 jprobe_saved_regs = *regs;
486 jprobe_saved_esp = ®s->esp;
487 addr = (unsigned long)jprobe_saved_esp;
490 * TBD: As Linus pointed out, gcc assumes that the callee
491 * owns the argument space and could overwrite it, e.g.
492 * tailcall optimization. So, to be absolutely safe
493 * we also save and restore enough stack bytes to cover
496 memcpy(jprobes_stack, (kprobe_opcode_t *) addr, MIN_STACK_SIZE(addr));
497 regs->eflags &= ~IF_MASK;
498 regs->eip = (unsigned long)(jp->entry);
502 void __kprobes jprobe_return(void)
504 preempt_enable_no_resched();
505 asm volatile (" xchgl %%ebx,%%esp \n"
507 " .globl jprobe_return_end \n"
508 " jprobe_return_end: \n"
510 (jprobe_saved_esp):"memory");
513 int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
515 u8 *addr = (u8 *) (regs->eip - 1);
516 unsigned long stack_addr = (unsigned long)jprobe_saved_esp;
517 struct jprobe *jp = container_of(p, struct jprobe, kp);
519 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
520 if (®s->esp != jprobe_saved_esp) {
521 struct pt_regs *saved_regs =
522 container_of(jprobe_saved_esp, struct pt_regs, esp);
523 printk("current esp %p does not match saved esp %p\n",
524 ®s->esp, jprobe_saved_esp);
525 printk("Saved registers for jprobe %p\n", jp);
526 show_registers(saved_regs);
527 printk("Current registers\n");
528 show_registers(regs);
531 *regs = jprobe_saved_regs;
532 memcpy((kprobe_opcode_t *) stack_addr, jprobes_stack,
533 MIN_STACK_SIZE(stack_addr));
539 static struct kprobe trampoline_p = {
540 .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
541 .pre_handler = trampoline_probe_handler
544 int __init arch_init_kprobes(void)
546 return register_kprobe(&trampoline_p);