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];
45 void jprobe_return_end(void);
48 * returns non-zero if opcode modifies the interrupt flag.
50 static inline int is_IF_modifier(kprobe_opcode_t opcode)
55 case 0xcf: /* iret/iretd */
56 case 0x9d: /* popf/popfd */
62 int arch_prepare_kprobe(struct kprobe *p)
64 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
68 void arch_remove_kprobe(struct kprobe *p)
72 static inline void disarm_kprobe(struct kprobe *p, struct pt_regs *regs)
75 regs->eip = (unsigned long)p->addr;
78 static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
80 regs->eflags |= TF_MASK;
81 regs->eflags &= ~IF_MASK;
82 regs->eip = (unsigned long)&p->ainsn.insn;
86 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
87 * remain disabled thorough out this function.
89 static inline int kprobe_handler(struct pt_regs *regs)
93 u8 *addr = (u8 *) (regs->eip - 1);
95 /* We're in an interrupt, but this is clear and BUG()-safe. */
98 /* Check we're not actually recursing */
99 if (kprobe_running()) {
100 /* We *are* holding lock here, so this is safe.
101 Disarm the probe we just hit, and ignore it. */
102 p = get_kprobe(addr);
104 disarm_kprobe(p, regs);
108 if (p->break_handler && p->break_handler(p, regs)) {
112 /* If it's not ours, can't be delete race, (we hold lock). */
117 p = get_kprobe(addr);
120 if (regs->eflags & VM_MASK) {
121 /* We are in virtual-8086 mode. Return 0 */
125 if (*addr != BREAKPOINT_INSTRUCTION) {
127 * The breakpoint instruction was removed right
128 * after we hit it. Another cpu has removed
129 * either a probepoint or a debugger breakpoint
130 * at this address. In either case, no further
131 * handling of this interrupt is appropriate.
135 /* Not one of ours: let kernel handle it */
139 kprobe_status = KPROBE_HIT_ACTIVE;
141 kprobe_saved_eflags = kprobe_old_eflags
142 = (regs->eflags & (TF_MASK | IF_MASK));
143 if (is_IF_modifier(p->opcode))
144 kprobe_saved_eflags &= ~IF_MASK;
146 if (p->pre_handler(p, regs)) {
147 /* handler has already set things up, so skip ss setup */
152 prepare_singlestep(p, regs);
153 kprobe_status = KPROBE_HIT_SS;
157 preempt_enable_no_resched();
162 * Called after single-stepping. p->addr is the address of the
163 * instruction whose first byte has been replaced by the "int 3"
164 * instruction. To avoid the SMP problems that can occur when we
165 * temporarily put back the original opcode to single-step, we
166 * single-stepped a copy of the instruction. The address of this
167 * copy is p->ainsn.insn.
169 * This function prepares to return from the post-single-step
170 * interrupt. We have to fix up the stack as follows:
172 * 0) Except in the case of absolute or indirect jump or call instructions,
173 * the new eip is relative to the copied instruction. We need to make
174 * it relative to the original instruction.
176 * 1) If the single-stepped instruction was pushfl, then the TF and IF
177 * flags are set in the just-pushed eflags, and may need to be cleared.
179 * 2) If the single-stepped instruction was a call, the return address
180 * that is atop the stack is the address following the copied instruction.
181 * We need to make it the address following the original instruction.
183 static void resume_execution(struct kprobe *p, struct pt_regs *regs)
185 unsigned long *tos = (unsigned long *)®s->esp;
186 unsigned long next_eip = 0;
187 unsigned long copy_eip = (unsigned long)&p->ainsn.insn;
188 unsigned long orig_eip = (unsigned long)p->addr;
190 switch (p->ainsn.insn[0]) {
191 case 0x9c: /* pushfl */
192 *tos &= ~(TF_MASK | IF_MASK);
193 *tos |= kprobe_old_eflags;
195 case 0xe8: /* call relative - Fix return addr */
196 *tos = orig_eip + (*tos - copy_eip);
199 if ((p->ainsn.insn[1] & 0x30) == 0x10) {
200 /* call absolute, indirect */
201 /* Fix return addr; eip is correct. */
202 next_eip = regs->eip;
203 *tos = orig_eip + (*tos - copy_eip);
204 } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
205 ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
206 /* eip is correct. */
207 next_eip = regs->eip;
210 case 0xea: /* jmp absolute -- eip is correct */
211 next_eip = regs->eip;
217 regs->eflags &= ~TF_MASK;
219 regs->eip = next_eip;
221 regs->eip = orig_eip + (regs->eip - copy_eip);
226 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
227 * remain disabled thoroughout this function. And we hold kprobe lock.
229 static inline int post_kprobe_handler(struct pt_regs *regs)
231 if (!kprobe_running())
234 if (current_kprobe->post_handler)
235 current_kprobe->post_handler(current_kprobe, regs, 0);
237 resume_execution(current_kprobe, regs);
238 regs->eflags |= kprobe_saved_eflags;
241 preempt_enable_no_resched();
244 * if somebody else is singlestepping across a probe point, eflags
245 * will have TF set, in which case, continue the remaining processing
246 * of do_debug, as if this is not a probe hit.
248 if (regs->eflags & TF_MASK)
254 /* Interrupts disabled, kprobe_lock held. */
255 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
257 if (current_kprobe->fault_handler
258 && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
261 if (kprobe_status & KPROBE_HIT_SS) {
262 resume_execution(current_kprobe, regs);
263 regs->eflags |= kprobe_old_eflags;
266 preempt_enable_no_resched();
272 * Wrapper routine to for handling exceptions.
274 int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
277 struct die_args *args = (struct die_args *)data;
280 if (kprobe_handler(args->regs))
284 if (post_kprobe_handler(args->regs))
288 if (kprobe_running() &&
289 kprobe_fault_handler(args->regs, args->trapnr))
293 if (kprobe_running() &&
294 kprobe_fault_handler(args->regs, args->trapnr))
303 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
305 struct jprobe *jp = container_of(p, struct jprobe, kp);
308 jprobe_saved_regs = *regs;
309 jprobe_saved_esp = ®s->esp;
310 addr = (unsigned long)jprobe_saved_esp;
313 * TBD: As Linus pointed out, gcc assumes that the callee
314 * owns the argument space and could overwrite it, e.g.
315 * tailcall optimization. So, to be absolutely safe
316 * we also save and restore enough stack bytes to cover
319 memcpy(jprobes_stack, (kprobe_opcode_t *) addr, MIN_STACK_SIZE(addr));
320 regs->eflags &= ~IF_MASK;
321 regs->eip = (unsigned long)(jp->entry);
325 void jprobe_return(void)
327 preempt_enable_no_resched();
328 asm volatile (" xchgl %%ebx,%%esp \n"
330 " .globl jprobe_return_end \n"
331 " jprobe_return_end: \n"
333 (jprobe_saved_esp):"memory");
336 int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
338 u8 *addr = (u8 *) (regs->eip - 1);
339 unsigned long stack_addr = (unsigned long)jprobe_saved_esp;
340 struct jprobe *jp = container_of(p, struct jprobe, kp);
342 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
343 if (®s->esp != jprobe_saved_esp) {
344 struct pt_regs *saved_regs =
345 container_of(jprobe_saved_esp, struct pt_regs, esp);
346 printk("current esp %p does not match saved esp %p\n",
347 ®s->esp, jprobe_saved_esp);
348 printk("Saved registers for jprobe %p\n", jp);
349 show_registers(saved_regs);
350 printk("Current registers\n");
351 show_registers(regs);
354 *regs = jprobe_saved_regs;
355 memcpy((kprobe_opcode_t *) stack_addr, jprobes_stack,
356 MIN_STACK_SIZE(stack_addr));