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.
28 #include <linux/config.h>
29 #include <linux/kprobes.h>
30 #include <linux/ptrace.h>
31 #include <linux/spinlock.h>
32 #include <linux/preempt.h>
33 #include <asm/kdebug.h>
35 /* kprobe_status settings */
36 #define KPROBE_HIT_ACTIVE 0x00000001
37 #define KPROBE_HIT_SS 0x00000002
39 static struct kprobe *current_kprobe;
40 static unsigned long kprobe_status, kprobe_old_eflags, kprobe_saved_eflags;
41 static struct pt_regs jprobe_saved_regs;
42 static long *jprobe_saved_esp;
43 /* copy of the kernel stack at the probe fire time */
44 static kprobe_opcode_t jprobes_stack[MAX_STACK_SIZE];
47 * returns non-zero if opcode modifies the interrupt flag.
49 static inline int is_IF_modifier(kprobe_opcode_t opcode)
54 case 0xcf: /* iret/iretd */
55 case 0x9d: /* popf/popfd */
61 void arch_prepare_kprobe(struct kprobe *p)
63 memcpy(p->insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
66 static inline void disarm_kprobe(struct kprobe *p, struct pt_regs *regs)
69 regs->eip = (unsigned long)p->addr;
72 static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
74 regs->eflags |= TF_MASK;
75 regs->eflags &= ~IF_MASK;
76 regs->eip = (unsigned long)&p->insn;
80 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
81 * remain disabled thorough out this function.
83 static inline int kprobe_handler(struct pt_regs *regs)
87 u8 *addr = (u8 *) (regs->eip - 1);
89 /* We're in an interrupt, but this is clear and BUG()-safe. */
92 /* Check we're not actually recursing */
93 if (kprobe_running()) {
94 /* We *are* holding lock here, so this is safe.
95 Disarm the probe we just hit, and ignore it. */
98 disarm_kprobe(p, regs);
102 if (p->break_handler && p->break_handler(p, regs)) {
106 /* If it's not ours, can't be delete race, (we hold lock). */
111 p = get_kprobe(addr);
114 if (*addr != BREAKPOINT_INSTRUCTION) {
116 * The breakpoint instruction was removed right
117 * after we hit it. Another cpu has removed
118 * either a probepoint or a debugger breakpoint
119 * at this address. In either case, no further
120 * handling of this interrupt is appropriate.
124 /* Not one of ours: let kernel handle it */
128 kprobe_status = KPROBE_HIT_ACTIVE;
130 kprobe_saved_eflags = kprobe_old_eflags
131 = (regs->eflags & (TF_MASK | IF_MASK));
132 if (is_IF_modifier(p->opcode))
133 kprobe_saved_eflags &= ~IF_MASK;
135 if (p->pre_handler(p, regs)) {
136 /* handler has already set things up, so skip ss setup */
141 prepare_singlestep(p, regs);
142 kprobe_status = KPROBE_HIT_SS;
146 preempt_enable_no_resched();
151 * Called after single-stepping. p->addr is the address of the
152 * instruction whose first byte has been replaced by the "int 3"
153 * instruction. To avoid the SMP problems that can occur when we
154 * temporarily put back the original opcode to single-step, we
155 * single-stepped a copy of the instruction. The address of this
158 * This function prepares to return from the post-single-step
159 * interrupt. We have to fix up the stack as follows:
161 * 0) Except in the case of absolute or indirect jump or call instructions,
162 * the new eip is relative to the copied instruction. We need to make
163 * it relative to the original instruction.
165 * 1) If the single-stepped instruction was pushfl, then the TF and IF
166 * flags are set in the just-pushed eflags, and may need to be cleared.
168 * 2) If the single-stepped instruction was a call, the return address
169 * that is atop the stack is the address following the copied instruction.
170 * We need to make it the address following the original instruction.
172 static void resume_execution(struct kprobe *p, struct pt_regs *regs)
174 unsigned long *tos = (unsigned long *)®s->esp;
175 unsigned long next_eip = 0;
176 unsigned long copy_eip = (unsigned long)&p->insn;
177 unsigned long orig_eip = (unsigned long)p->addr;
179 switch (p->insn[0]) {
180 case 0x9c: /* pushfl */
181 *tos &= ~(TF_MASK | IF_MASK);
182 *tos |= kprobe_old_eflags;
184 case 0xe8: /* call relative - Fix return addr */
185 *tos = orig_eip + (*tos - copy_eip);
188 if ((p->insn[1] & 0x30) == 0x10) {
189 /* call absolute, indirect */
190 /* Fix return addr; eip is correct. */
191 next_eip = regs->eip;
192 *tos = orig_eip + (*tos - copy_eip);
193 } else if (((p->insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
194 ((p->insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
195 /* eip is correct. */
196 next_eip = regs->eip;
199 case 0xea: /* jmp absolute -- eip is correct */
200 next_eip = regs->eip;
206 regs->eflags &= ~TF_MASK;
208 regs->eip = next_eip;
210 regs->eip = orig_eip + (regs->eip - copy_eip);
215 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
216 * remain disabled thoroughout this function. And we hold kprobe lock.
218 static inline int post_kprobe_handler(struct pt_regs *regs)
220 if (!kprobe_running())
223 if (current_kprobe->post_handler)
224 current_kprobe->post_handler(current_kprobe, regs, 0);
226 resume_execution(current_kprobe, regs);
227 regs->eflags |= kprobe_saved_eflags;
230 preempt_enable_no_resched();
233 * if somebody else is singlestepping across a probe point, eflags
234 * will have TF set, in which case, continue the remaining processing
235 * of do_debug, as if this is not a probe hit.
237 if (regs->eflags & TF_MASK)
243 /* Interrupts disabled, kprobe_lock held. */
244 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
246 if (current_kprobe->fault_handler
247 && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
250 if (kprobe_status & KPROBE_HIT_SS) {
251 resume_execution(current_kprobe, regs);
252 regs->eflags |= kprobe_old_eflags;
255 preempt_enable_no_resched();
261 * Wrapper routine to for handling exceptions.
263 int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
266 struct die_args *args = (struct die_args *)data;
269 if (kprobe_handler(args->regs))
273 if (post_kprobe_handler(args->regs))
277 if (kprobe_running() &&
278 kprobe_fault_handler(args->regs, args->trapnr))
282 if (kprobe_running() &&
283 kprobe_fault_handler(args->regs, args->trapnr))
292 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
294 struct jprobe *jp = container_of(p, struct jprobe, kp);
297 jprobe_saved_regs = *regs;
298 jprobe_saved_esp = ®s->esp;
299 addr = (unsigned long)jprobe_saved_esp;
302 * TBD: As Linus pointed out, gcc assumes that the callee
303 * owns the argument space and could overwrite it, e.g.
304 * tailcall optimization. So, to be absolutely safe
305 * we also save and restore enough stack bytes to cover
308 memcpy(jprobes_stack, (kprobe_opcode_t *) addr, MIN_STACK_SIZE(addr));
309 regs->eflags &= ~IF_MASK;
310 regs->eip = (unsigned long)(jp->entry);
314 void jprobe_return(void)
316 preempt_enable_no_resched();
317 asm volatile (" xchgl %%ebx,%%esp \n"
319 (jprobe_saved_esp):"memory");
321 void jprobe_return_end(void)
325 int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
327 u8 *addr = (u8 *) (regs->eip - 1);
328 unsigned long stack_addr = (unsigned long)jprobe_saved_esp;
329 struct jprobe *jp = container_of(p, struct jprobe, kp);
331 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
332 if (®s->esp != jprobe_saved_esp) {
333 struct pt_regs *saved_regs =
334 container_of(jprobe_saved_esp, struct pt_regs, esp);
335 printk("current esp %p does not match saved esp %p\n",
336 ®s->esp, jprobe_saved_esp);
337 printk("Saved registers for jprobe %p\n", jp);
338 show_registers(saved_regs);
339 printk("Current registers\n");
340 show_registers(regs);
343 *regs = jprobe_saved_regs;
344 memcpy((kprobe_opcode_t *) stack_addr, jprobes_stack,
345 MIN_STACK_SIZE(stack_addr));