1 /*
2 * Kernel Probes (KProbes)
3 * arch/i386/kernel/kprobes.c
4 *
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.
9 *
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.
14 *
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.
18 *
19 * Copyright (C) IBM Corporation, 2002, 2004
20 *
21 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
22 * Probes initial implementation ( includes contributions from
23 * Rusty Russell).
24 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
25 * interface to access function arguments.
26 */
27
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>
34 #include <asm/desc.h>
35
36 /* kprobe_status settings */
37 #define KPROBE_HIT_ACTIVE 0x00000001
38 #define KPROBE_HIT_SS 0x00000002
39
40 static struct kprobe *current_kprobe;
41 static unsigned long kprobe_status, kprobe_old_eflags, kprobe_saved_eflags;
42 static struct pt_regs jprobe_saved_regs;
43 static long *jprobe_saved_esp;
44 /* copy of the kernel stack at the probe fire time */
45 static kprobe_opcode_t jprobes_stack[MAX_STACK_SIZE];
46 void jprobe_return_end(void);
47
48 /*
49 * returns non-zero if opcode modifies the interrupt flag.
50 */
51 static inline int is_IF_modifier(kprobe_opcode_t opcode)
52 {
53 switch (opcode) {
54 case 0xfa: /* cli */
55 case 0xfb: /* sti */
56 case 0xcf: /* iret/iretd */
57 case 0x9d: /* popf/popfd */
58 return 1;
59 }
60 return 0;
61 }
62
63 int arch_prepare_kprobe(struct kprobe *p)
64 {
65 return 0;
66 }
67
68 void arch_copy_kprobe(struct kprobe *p)
69 {
70 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
71 }
72
73 void arch_remove_kprobe(struct kprobe *p)
74 {
75 }
76
77 static inline void disarm_kprobe(struct kprobe *p, struct pt_regs *regs)
78 {
79 *p->addr = p->opcode;
80 regs->eip = (unsigned long)p->addr;
81 }
82
83 static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
84 {
85 regs->eflags |= TF_MASK;
86 regs->eflags &= ~IF_MASK;
87 regs->eip = (unsigned long)&p->ainsn.insn;
88 }
89
90 /*
91 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
92 * remain disabled thorough out this function.
93 */
94 static int kprobe_handler(struct pt_regs *regs)
95 {
96 struct kprobe *p;
97 int ret = 0;
98 kprobe_opcode_t *addr = NULL;
99 unsigned long *lp;
100
101 /* We're in an interrupt, but this is clear and BUG()-safe. */
102 preempt_disable();
103 /* Check if the application is using LDT entry for its code segment and
104 * calculate the address by reading the base address from the LDT entry.
105 */
106 if ((regs->xcs & 4) && (current->mm)) {
107 lp = (unsigned long *) ((unsigned long)((regs->xcs >> 3) * 8)
108 + (char *) current->mm->context.ldt);
109 addr = (kprobe_opcode_t *) (get_desc_base(lp) + regs->eip -
110 sizeof(kprobe_opcode_t));
111 } else {
112 addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
113 }
114 /* Check we're not actually recursing */
115 if (kprobe_running()) {
116 /* We *are* holding lock here, so this is safe.
117 Disarm the probe we just hit, and ignore it. */
118 p = get_kprobe(addr);
119 if (p) {
120 disarm_kprobe(p, regs);
121 ret = 1;
122 } else {
123 p = current_kprobe;
124 if (p->break_handler && p->break_handler(p, regs)) {
125 goto ss_probe;
126 }
127 }
128 /* If it's not ours, can't be delete race, (we hold lock). */
129 goto no_kprobe;
130 }
131
132 lock_kprobes();
133 p = get_kprobe(addr);
134 if (!p) {
135 unlock_kprobes();
136 if (regs->eflags & VM_MASK) {
137 /* We are in virtual-8086 mode. Return 0 */
138 goto no_kprobe;
139 }
140
141 if (*addr != BREAKPOINT_INSTRUCTION) {
142 /*
143 * The breakpoint instruction was removed right
144 * after we hit it. Another cpu has removed
145 * either a probepoint or a debugger breakpoint
146 * at this address. In either case, no further
147 * handling of this interrupt is appropriate.
148 */
149 ret = 1;
150 }
151 /* Not one of ours: let kernel handle it */
152 goto no_kprobe;
153 }
154
155 kprobe_status = KPROBE_HIT_ACTIVE;
156 current_kprobe = p;
157 kprobe_saved_eflags = kprobe_old_eflags
158 = (regs->eflags & (TF_MASK | IF_MASK));
159 if (is_IF_modifier(p->opcode))
160 kprobe_saved_eflags &= ~IF_MASK;
161
162 if (p->pre_handler(p, regs)) {
163 /* handler has already set things up, so skip ss setup */
164 return 1;
165 }
166
167 ss_probe:
168 prepare_singlestep(p, regs);
169 kprobe_status = KPROBE_HIT_SS;
170 return 1;
171
172 no_kprobe:
173 preempt_enable_no_resched();
174 return ret;
175 }
176
177 /*
178 * Called after single-stepping. p->addr is the address of the
179 * instruction whose first byte has been replaced by the "int 3"
180 * instruction. To avoid the SMP problems that can occur when we
181 * temporarily put back the original opcode to single-step, we
182 * single-stepped a copy of the instruction. The address of this
183 * copy is p->ainsn.insn.
184 *
185 * This function prepares to return from the post-single-step
186 * interrupt. We have to fix up the stack as follows:
187 *
188 * 0) Except in the case of absolute or indirect jump or call instructions,
189 * the new eip is relative to the copied instruction. We need to make
190 * it relative to the original instruction.
191 *
192 * 1) If the single-stepped instruction was pushfl, then the TF and IF
193 * flags are set in the just-pushed eflags, and may need to be cleared.
194 *
195 * 2) If the single-stepped instruction was a call, the return address
196 * that is atop the stack is the address following the copied instruction.
197 * We need to make it the address following the original instruction.
198 */
199 static void resume_execution(struct kprobe *p, struct pt_regs *regs)
200 {
201 unsigned long *tos = (unsigned long *)®s->esp;
202 unsigned long next_eip = 0;
203 unsigned long copy_eip = (unsigned long)&p->ainsn.insn;
204 unsigned long orig_eip = (unsigned long)p->addr;
205
206 switch (p->ainsn.insn[0]) {
207 case 0x9c: /* pushfl */
208 *tos &= ~(TF_MASK | IF_MASK);
209 *tos |= kprobe_old_eflags;
210 break;
211 case 0xe8: /* call relative - Fix return addr */
212 *tos = orig_eip + (*tos - copy_eip);
213 break;
214 case 0xff:
215 if ((p->ainsn.insn[1] & 0x30) == 0x10) {
216 /* call absolute, indirect */
217 /* Fix return addr; eip is correct. */
218 next_eip = regs->eip;
219 *tos = orig_eip + (*tos - copy_eip);
220 } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
221 ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
222 /* eip is correct. */
223 next_eip = regs->eip;
224 }
225 break;
226 case 0xea: /* jmp absolute -- eip is correct */
227 next_eip = regs->eip;
228 break;
229 default:
230 break;
231 }
232
233 regs->eflags &= ~TF_MASK;
234 if (next_eip) {
235 regs->eip = next_eip;
236 } else {
237 regs->eip = orig_eip + (regs->eip - copy_eip);
238 }
239 }
240
241 /*
242 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
243 * remain disabled thoroughout this function. And we hold kprobe lock.
244 */
245 static inline int post_kprobe_handler(struct pt_regs *regs)
246 {
247 if (!kprobe_running())
248 return 0;
249
250 if (current_kprobe->post_handler)
251 current_kprobe->post_handler(current_kprobe, regs, 0);
252
253 resume_execution(current_kprobe, regs);
254 regs->eflags |= kprobe_saved_eflags;
255
256 unlock_kprobes();
257 preempt_enable_no_resched();
258
259 /*
260 * if somebody else is singlestepping across a probe point, eflags
261 * will have TF set, in which case, continue the remaining processing
262 * of do_debug, as if this is not a probe hit.
263 */
264 if (regs->eflags & TF_MASK)
265 return 0;
266
267 return 1;
268 }
269
270 /* Interrupts disabled, kprobe_lock held. */
271 static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
272 {
273 if (current_kprobe->fault_handler
274 && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
275 return 1;
276
277 if (kprobe_status & KPROBE_HIT_SS) {
278 resume_execution(current_kprobe, regs);
279 regs->eflags |= kprobe_old_eflags;
280
281 unlock_kprobes();
282 preempt_enable_no_resched();
283 }
284 return 0;
285 }
286
287 /*
288 * Wrapper routine to for handling exceptions.
289 */
290 int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
291 void *data)
292 {
293 struct die_args *args = (struct die_args *)data;
294 switch (val) {
295 case DIE_INT3:
296 if (kprobe_handler(args->regs))
297 return NOTIFY_STOP;
298 break;
299 case DIE_DEBUG:
300 if (post_kprobe_handler(args->regs))
301 return NOTIFY_STOP;
302 break;
303 case DIE_GPF:
304 if (kprobe_running() &&
305 kprobe_fault_handler(args->regs, args->trapnr))
306 return NOTIFY_STOP;
307 break;
308 case DIE_PAGE_FAULT:
309 if (kprobe_running() &&
310 kprobe_fault_handler(args->regs, args->trapnr))
311 return NOTIFY_STOP;
312 break;
313 default:
314 break;
315 }
316 return NOTIFY_DONE;
317 }
318
319 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
320 {
321 struct jprobe *jp = container_of(p, struct jprobe, kp);
322 unsigned long addr;
323
324 jprobe_saved_regs = *regs;
325 jprobe_saved_esp = ®s->esp;
326 addr = (unsigned long)jprobe_saved_esp;
327
328 /*
329 * TBD: As Linus pointed out, gcc assumes that the callee
330 * owns the argument space and could overwrite it, e.g.
331 * tailcall optimization. So, to be absolutely safe
332 * we also save and restore enough stack bytes to cover
333 * the argument area.
334 */
335 memcpy(jprobes_stack, (kprobe_opcode_t *) addr, MIN_STACK_SIZE(addr));
336 regs->eflags &= ~IF_MASK;
337 regs->eip = (unsigned long)(jp->entry);
338 return 1;
339 }
340
341 void jprobe_return(void)
342 {
343 preempt_enable_no_resched();
344 asm volatile (" xchgl %%ebx,%%esp \n"
345 " int3 \n"
346 " .globl jprobe_return_end \n"
347 " jprobe_return_end: \n"
348 " nop \n"::"b"
349 (jprobe_saved_esp):"memory");
350 }
351
352 int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
353 {
354 u8 *addr = (u8 *) (regs->eip - 1);
355 unsigned long stack_addr = (unsigned long)jprobe_saved_esp;
356 struct jprobe *jp = container_of(p, struct jprobe, kp);
357
358 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
359 if (®s->esp != jprobe_saved_esp) {
360 struct pt_regs *saved_regs =
361 container_of(jprobe_saved_esp, struct pt_regs, esp);
362 printk("current esp %p does not match saved esp %p\n",
363 ®s->esp, jprobe_saved_esp);
364 printk("Saved registers for jprobe %p\n", jp);
365 show_registers(saved_regs);
366 printk("Current registers\n");
367 show_registers(regs);
368 BUG();
369 }
370 *regs = jprobe_saved_regs;
371 memcpy((kprobe_opcode_t *) stack_addr, jprobes_stack,
372 MIN_STACK_SIZE(stack_addr));
373 return 1;
374 }
375 return 0;
376 }
377
|
This page was automatically generated by the
LXR engine.
|