blob: 2314d8d306fd09da6ad6f07610ba155f8d5f6062 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
Hien Nguyenb94cce92005-06-23 00:09:19 -070026 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
30
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>
Rusty Lynch7e1048b2005-06-23 00:09:25 -070036#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/kdebug.h>
38#include <asm/desc.h>
39
40/* kprobe_status settings */
41#define KPROBE_HIT_ACTIVE 0x00000001
42#define KPROBE_HIT_SS 0x00000002
43
44static struct kprobe *current_kprobe;
45static unsigned long kprobe_status, kprobe_old_eflags, kprobe_saved_eflags;
46static struct pt_regs jprobe_saved_regs;
47static long *jprobe_saved_esp;
48/* copy of the kernel stack at the probe fire time */
49static kprobe_opcode_t jprobes_stack[MAX_STACK_SIZE];
50void jprobe_return_end(void);
51
52/*
53 * returns non-zero if opcode modifies the interrupt flag.
54 */
55static inline int is_IF_modifier(kprobe_opcode_t opcode)
56{
57 switch (opcode) {
58 case 0xfa: /* cli */
59 case 0xfb: /* sti */
60 case 0xcf: /* iret/iretd */
61 case 0x9d: /* popf/popfd */
62 return 1;
63 }
64 return 0;
65}
66
67int arch_prepare_kprobe(struct kprobe *p)
68{
69 return 0;
70}
71
72void arch_copy_kprobe(struct kprobe *p)
73{
74 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
Rusty Lynch7e1048b2005-06-23 00:09:25 -070075 p->opcode = *p->addr;
76}
77
78void arch_arm_kprobe(struct kprobe *p)
79{
80 *p->addr = BREAKPOINT_INSTRUCTION;
81 flush_icache_range((unsigned long) p->addr,
82 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
83}
84
85void arch_disarm_kprobe(struct kprobe *p)
86{
87 *p->addr = p->opcode;
88 flush_icache_range((unsigned long) p->addr,
89 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
92void arch_remove_kprobe(struct kprobe *p)
93{
94}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
97{
98 regs->eflags |= TF_MASK;
99 regs->eflags &= ~IF_MASK;
100 /*single step inline if the instruction is an int3*/
101 if (p->opcode == BREAKPOINT_INSTRUCTION)
102 regs->eip = (unsigned long)p->addr;
103 else
104 regs->eip = (unsigned long)&p->ainsn.insn;
105}
106
Hien Nguyenb94cce92005-06-23 00:09:19 -0700107struct task_struct *arch_get_kprobe_task(void *ptr)
108{
109 return ((struct thread_info *) (((unsigned long) ptr) &
110 (~(THREAD_SIZE -1))))->task;
111}
112
113void arch_prepare_kretprobe(struct kretprobe *rp, struct pt_regs *regs)
114{
115 unsigned long *sara = (unsigned long *)&regs->esp;
116 struct kretprobe_instance *ri;
117 static void *orig_ret_addr;
118
119 /*
120 * Save the return address when the return probe hits
121 * the first time, and use it to populate the (krprobe
122 * instance)->ret_addr for subsequent return probes at
123 * the same addrress since stack address would have
124 * the kretprobe_trampoline by then.
125 */
126 if (((void*) *sara) != kretprobe_trampoline)
127 orig_ret_addr = (void*) *sara;
128
129 if ((ri = get_free_rp_inst(rp)) != NULL) {
130 ri->rp = rp;
131 ri->stack_addr = sara;
132 ri->ret_addr = orig_ret_addr;
133 add_rp_inst(ri);
134 /* Replace the return addr with trampoline addr */
135 *sara = (unsigned long) &kretprobe_trampoline;
136 } else {
137 rp->nmissed++;
138 }
139}
140
141void arch_kprobe_flush_task(struct task_struct *tk, spinlock_t *kp_lock)
142{
143 unsigned long flags = 0;
144 struct kretprobe_instance *ri;
145 spin_lock_irqsave(kp_lock, flags);
146 while ((ri = get_rp_inst_tsk(tk)) != NULL) {
147 *((unsigned long *)(ri->stack_addr)) =
148 (unsigned long) ri->ret_addr;
149 recycle_rp_inst(ri);
150 }
151 spin_unlock_irqrestore(kp_lock, flags);
152}
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/*
155 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
156 * remain disabled thorough out this function.
157 */
158static int kprobe_handler(struct pt_regs *regs)
159{
160 struct kprobe *p;
161 int ret = 0;
162 kprobe_opcode_t *addr = NULL;
163 unsigned long *lp;
164
165 /* We're in an interrupt, but this is clear and BUG()-safe. */
166 preempt_disable();
167 /* Check if the application is using LDT entry for its code segment and
168 * calculate the address by reading the base address from the LDT entry.
169 */
170 if ((regs->xcs & 4) && (current->mm)) {
171 lp = (unsigned long *) ((unsigned long)((regs->xcs >> 3) * 8)
172 + (char *) current->mm->context.ldt);
173 addr = (kprobe_opcode_t *) (get_desc_base(lp) + regs->eip -
174 sizeof(kprobe_opcode_t));
175 } else {
176 addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
177 }
178 /* Check we're not actually recursing */
179 if (kprobe_running()) {
180 /* We *are* holding lock here, so this is safe.
181 Disarm the probe we just hit, and ignore it. */
182 p = get_kprobe(addr);
183 if (p) {
184 if (kprobe_status == KPROBE_HIT_SS) {
185 regs->eflags &= ~TF_MASK;
186 regs->eflags |= kprobe_saved_eflags;
187 unlock_kprobes();
188 goto no_kprobe;
189 }
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700190 arch_disarm_kprobe(p);
191 regs->eip = (unsigned long)p->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 ret = 1;
193 } else {
194 p = current_kprobe;
195 if (p->break_handler && p->break_handler(p, regs)) {
196 goto ss_probe;
197 }
198 }
199 /* If it's not ours, can't be delete race, (we hold lock). */
200 goto no_kprobe;
201 }
202
203 lock_kprobes();
204 p = get_kprobe(addr);
205 if (!p) {
206 unlock_kprobes();
207 if (regs->eflags & VM_MASK) {
208 /* We are in virtual-8086 mode. Return 0 */
209 goto no_kprobe;
210 }
211
212 if (*addr != BREAKPOINT_INSTRUCTION) {
213 /*
214 * The breakpoint instruction was removed right
215 * after we hit it. Another cpu has removed
216 * either a probepoint or a debugger breakpoint
217 * at this address. In either case, no further
218 * handling of this interrupt is appropriate.
219 */
220 ret = 1;
221 }
222 /* Not one of ours: let kernel handle it */
223 goto no_kprobe;
224 }
225
226 kprobe_status = KPROBE_HIT_ACTIVE;
227 current_kprobe = p;
228 kprobe_saved_eflags = kprobe_old_eflags
229 = (regs->eflags & (TF_MASK | IF_MASK));
230 if (is_IF_modifier(p->opcode))
231 kprobe_saved_eflags &= ~IF_MASK;
232
233 if (p->pre_handler && p->pre_handler(p, regs))
234 /* handler has already set things up, so skip ss setup */
235 return 1;
236
237ss_probe:
238 prepare_singlestep(p, regs);
239 kprobe_status = KPROBE_HIT_SS;
240 return 1;
241
242no_kprobe:
243 preempt_enable_no_resched();
244 return ret;
245}
246
247/*
Hien Nguyenb94cce92005-06-23 00:09:19 -0700248 * For function-return probes, init_kprobes() establishes a probepoint
249 * here. When a retprobed function returns, this probe is hit and
250 * trampoline_probe_handler() runs, calling the kretprobe's handler.
251 */
252 void kretprobe_trampoline_holder(void)
253 {
254 asm volatile ( ".global kretprobe_trampoline\n"
255 "kretprobe_trampoline: \n"
256 "nop\n");
257 }
258
259/*
260 * Called when we hit the probe point at kretprobe_trampoline
261 */
262int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
263{
264 struct task_struct *tsk;
265 struct kretprobe_instance *ri;
266 struct hlist_head *head;
267 struct hlist_node *node;
268 unsigned long *sara = ((unsigned long *) &regs->esp) - 1;
269
270 tsk = arch_get_kprobe_task(sara);
271 head = kretprobe_inst_table_head(tsk);
272
273 hlist_for_each_entry(ri, node, head, hlist) {
274 if (ri->stack_addr == sara && ri->rp) {
275 if (ri->rp->handler)
276 ri->rp->handler(ri, regs);
277 }
278 }
279 return 0;
280}
281
282void trampoline_post_handler(struct kprobe *p, struct pt_regs *regs,
283 unsigned long flags)
284{
285 struct kretprobe_instance *ri;
286 /* RA already popped */
287 unsigned long *sara = ((unsigned long *)&regs->esp) - 1;
288
289 while ((ri = get_rp_inst(sara))) {
290 regs->eip = (unsigned long)ri->ret_addr;
291 recycle_rp_inst(ri);
292 }
293 regs->eflags &= ~TF_MASK;
294}
295
296/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * Called after single-stepping. p->addr is the address of the
298 * instruction whose first byte has been replaced by the "int 3"
299 * instruction. To avoid the SMP problems that can occur when we
300 * temporarily put back the original opcode to single-step, we
301 * single-stepped a copy of the instruction. The address of this
302 * copy is p->ainsn.insn.
303 *
304 * This function prepares to return from the post-single-step
305 * interrupt. We have to fix up the stack as follows:
306 *
307 * 0) Except in the case of absolute or indirect jump or call instructions,
308 * the new eip is relative to the copied instruction. We need to make
309 * it relative to the original instruction.
310 *
311 * 1) If the single-stepped instruction was pushfl, then the TF and IF
312 * flags are set in the just-pushed eflags, and may need to be cleared.
313 *
314 * 2) If the single-stepped instruction was a call, the return address
315 * that is atop the stack is the address following the copied instruction.
316 * We need to make it the address following the original instruction.
317 */
318static void resume_execution(struct kprobe *p, struct pt_regs *regs)
319{
320 unsigned long *tos = (unsigned long *)&regs->esp;
321 unsigned long next_eip = 0;
322 unsigned long copy_eip = (unsigned long)&p->ainsn.insn;
323 unsigned long orig_eip = (unsigned long)p->addr;
324
325 switch (p->ainsn.insn[0]) {
326 case 0x9c: /* pushfl */
327 *tos &= ~(TF_MASK | IF_MASK);
328 *tos |= kprobe_old_eflags;
329 break;
Prasanna S Panchamukhi0b9e2ca2005-05-05 16:15:40 -0700330 case 0xc3: /* ret/lret */
331 case 0xcb:
332 case 0xc2:
333 case 0xca:
334 regs->eflags &= ~TF_MASK;
335 /* eip is already adjusted, no more changes required*/
336 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 case 0xe8: /* call relative - Fix return addr */
338 *tos = orig_eip + (*tos - copy_eip);
339 break;
340 case 0xff:
341 if ((p->ainsn.insn[1] & 0x30) == 0x10) {
342 /* call absolute, indirect */
343 /* Fix return addr; eip is correct. */
344 next_eip = regs->eip;
345 *tos = orig_eip + (*tos - copy_eip);
346 } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
347 ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
348 /* eip is correct. */
349 next_eip = regs->eip;
350 }
351 break;
352 case 0xea: /* jmp absolute -- eip is correct */
353 next_eip = regs->eip;
354 break;
355 default:
356 break;
357 }
358
359 regs->eflags &= ~TF_MASK;
360 if (next_eip) {
361 regs->eip = next_eip;
362 } else {
363 regs->eip = orig_eip + (regs->eip - copy_eip);
364 }
365}
366
367/*
368 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
369 * remain disabled thoroughout this function. And we hold kprobe lock.
370 */
371static inline int post_kprobe_handler(struct pt_regs *regs)
372{
373 if (!kprobe_running())
374 return 0;
375
376 if (current_kprobe->post_handler)
377 current_kprobe->post_handler(current_kprobe, regs, 0);
378
Hien Nguyenb94cce92005-06-23 00:09:19 -0700379 if (current_kprobe->post_handler != trampoline_post_handler)
380 resume_execution(current_kprobe, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 regs->eflags |= kprobe_saved_eflags;
382
383 unlock_kprobes();
384 preempt_enable_no_resched();
385
386 /*
387 * if somebody else is singlestepping across a probe point, eflags
388 * will have TF set, in which case, continue the remaining processing
389 * of do_debug, as if this is not a probe hit.
390 */
391 if (regs->eflags & TF_MASK)
392 return 0;
393
394 return 1;
395}
396
397/* Interrupts disabled, kprobe_lock held. */
398static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
399{
400 if (current_kprobe->fault_handler
401 && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
402 return 1;
403
404 if (kprobe_status & KPROBE_HIT_SS) {
405 resume_execution(current_kprobe, regs);
406 regs->eflags |= kprobe_old_eflags;
407
408 unlock_kprobes();
409 preempt_enable_no_resched();
410 }
411 return 0;
412}
413
414/*
415 * Wrapper routine to for handling exceptions.
416 */
417int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
418 void *data)
419{
420 struct die_args *args = (struct die_args *)data;
421 switch (val) {
422 case DIE_INT3:
423 if (kprobe_handler(args->regs))
424 return NOTIFY_STOP;
425 break;
426 case DIE_DEBUG:
427 if (post_kprobe_handler(args->regs))
428 return NOTIFY_STOP;
429 break;
430 case DIE_GPF:
431 if (kprobe_running() &&
432 kprobe_fault_handler(args->regs, args->trapnr))
433 return NOTIFY_STOP;
434 break;
435 case DIE_PAGE_FAULT:
436 if (kprobe_running() &&
437 kprobe_fault_handler(args->regs, args->trapnr))
438 return NOTIFY_STOP;
439 break;
440 default:
441 break;
442 }
443 return NOTIFY_DONE;
444}
445
446int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
447{
448 struct jprobe *jp = container_of(p, struct jprobe, kp);
449 unsigned long addr;
450
451 jprobe_saved_regs = *regs;
452 jprobe_saved_esp = &regs->esp;
453 addr = (unsigned long)jprobe_saved_esp;
454
455 /*
456 * TBD: As Linus pointed out, gcc assumes that the callee
457 * owns the argument space and could overwrite it, e.g.
458 * tailcall optimization. So, to be absolutely safe
459 * we also save and restore enough stack bytes to cover
460 * the argument area.
461 */
462 memcpy(jprobes_stack, (kprobe_opcode_t *) addr, MIN_STACK_SIZE(addr));
463 regs->eflags &= ~IF_MASK;
464 regs->eip = (unsigned long)(jp->entry);
465 return 1;
466}
467
468void jprobe_return(void)
469{
470 preempt_enable_no_resched();
471 asm volatile (" xchgl %%ebx,%%esp \n"
472 " int3 \n"
473 " .globl jprobe_return_end \n"
474 " jprobe_return_end: \n"
475 " nop \n"::"b"
476 (jprobe_saved_esp):"memory");
477}
478
479int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
480{
481 u8 *addr = (u8 *) (regs->eip - 1);
482 unsigned long stack_addr = (unsigned long)jprobe_saved_esp;
483 struct jprobe *jp = container_of(p, struct jprobe, kp);
484
485 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
486 if (&regs->esp != jprobe_saved_esp) {
487 struct pt_regs *saved_regs =
488 container_of(jprobe_saved_esp, struct pt_regs, esp);
489 printk("current esp %p does not match saved esp %p\n",
490 &regs->esp, jprobe_saved_esp);
491 printk("Saved registers for jprobe %p\n", jp);
492 show_registers(saved_regs);
493 printk("Current registers\n");
494 show_registers(regs);
495 BUG();
496 }
497 *regs = jprobe_saved_regs;
498 memcpy((kprobe_opcode_t *) stack_addr, jprobes_stack,
499 MIN_STACK_SIZE(stack_addr));
500 return 1;
501 }
502 return 0;
503}