[PATCH] Kprobes: Track kprobe on a per_cpu basis - base changes
[linux-block.git] / arch / i386 / kernel / kprobes.c
CommitLineData
1da177e4
LT
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.
b94cce92
HN
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.
1da177e4
LT
29 */
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>
7e1048b1 36#include <asm/cacheflush.h>
1da177e4
LT
37#include <asm/kdebug.h>
38#include <asm/desc.h>
39
1da177e4
LT
40static struct kprobe *current_kprobe;
41static unsigned long kprobe_status, kprobe_old_eflags, kprobe_saved_eflags;
417c8da6
PP
42static struct kprobe *kprobe_prev;
43static unsigned long kprobe_status_prev, kprobe_old_eflags_prev, kprobe_saved_eflags_prev;
1da177e4
LT
44static struct pt_regs jprobe_saved_regs;
45static long *jprobe_saved_esp;
46/* copy of the kernel stack at the probe fire time */
47static kprobe_opcode_t jprobes_stack[MAX_STACK_SIZE];
48void jprobe_return_end(void);
49
50/*
51 * returns non-zero if opcode modifies the interrupt flag.
52 */
53static inline int is_IF_modifier(kprobe_opcode_t opcode)
54{
55 switch (opcode) {
56 case 0xfa: /* cli */
57 case 0xfb: /* sti */
58 case 0xcf: /* iret/iretd */
59 case 0x9d: /* popf/popfd */
60 return 1;
61 }
62 return 0;
63}
64
3d97ae5b 65int __kprobes arch_prepare_kprobe(struct kprobe *p)
1da177e4
LT
66{
67 return 0;
68}
69
3d97ae5b 70void __kprobes arch_copy_kprobe(struct kprobe *p)
1da177e4
LT
71{
72 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
7e1048b1 73 p->opcode = *p->addr;
1da177e4
LT
74}
75
3d97ae5b 76void __kprobes arch_arm_kprobe(struct kprobe *p)
1da177e4 77{
7e1048b1
RL
78 *p->addr = BREAKPOINT_INSTRUCTION;
79 flush_icache_range((unsigned long) p->addr,
80 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
1da177e4
LT
81}
82
3d97ae5b 83void __kprobes arch_disarm_kprobe(struct kprobe *p)
1da177e4
LT
84{
85 *p->addr = p->opcode;
7e1048b1
RL
86 flush_icache_range((unsigned long) p->addr,
87 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
88}
89
3d97ae5b 90void __kprobes arch_remove_kprobe(struct kprobe *p)
7e1048b1 91{
1da177e4
LT
92}
93
417c8da6
PP
94static inline void save_previous_kprobe(void)
95{
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;
100}
101
102static inline void restore_previous_kprobe(void)
103{
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;
108}
109
110static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs)
111{
112 current_kprobe = p;
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;
117}
118
1da177e4
LT
119static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
120{
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;
126 else
127 regs->eip = (unsigned long)&p->ainsn.insn;
128}
129
3d97ae5b
PP
130void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
131 struct pt_regs *regs)
b94cce92
HN
132{
133 unsigned long *sara = (unsigned long *)&regs->esp;
4bdbd37f
RL
134 struct kretprobe_instance *ri;
135
136 if ((ri = get_free_rp_inst(rp)) != NULL) {
137 ri->rp = rp;
138 ri->task = current;
139 ri->ret_addr = (kprobe_opcode_t *) *sara;
b94cce92 140
b94cce92
HN
141 /* Replace the return addr with trampoline addr */
142 *sara = (unsigned long) &kretprobe_trampoline;
b94cce92 143
4bdbd37f
RL
144 add_rp_inst(ri);
145 } else {
146 rp->nmissed++;
147 }
b94cce92
HN
148}
149
1da177e4
LT
150/*
151 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
152 * remain disabled thorough out this function.
153 */
3d97ae5b 154static int __kprobes kprobe_handler(struct pt_regs *regs)
1da177e4
LT
155{
156 struct kprobe *p;
157 int ret = 0;
158 kprobe_opcode_t *addr = NULL;
159 unsigned long *lp;
160
1da177e4
LT
161 /* Check if the application is using LDT entry for its code segment and
162 * calculate the address by reading the base address from the LDT entry.
163 */
164 if ((regs->xcs & 4) && (current->mm)) {
165 lp = (unsigned long *) ((unsigned long)((regs->xcs >> 3) * 8)
166 + (char *) current->mm->context.ldt);
167 addr = (kprobe_opcode_t *) (get_desc_base(lp) + regs->eip -
168 sizeof(kprobe_opcode_t));
169 } else {
170 addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
171 }
172 /* Check we're not actually recursing */
173 if (kprobe_running()) {
174 /* We *are* holding lock here, so this is safe.
175 Disarm the probe we just hit, and ignore it. */
176 p = get_kprobe(addr);
177 if (p) {
deac66ae
KA
178 if (kprobe_status == KPROBE_HIT_SS &&
179 *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
1da177e4
LT
180 regs->eflags &= ~TF_MASK;
181 regs->eflags |= kprobe_saved_eflags;
182 unlock_kprobes();
183 goto no_kprobe;
184 }
417c8da6
PP
185 /* We have reentered the kprobe_handler(), since
186 * another probe was hit while within the handler.
187 * We here save the original kprobes variables and
188 * just single step on the instruction of the new probe
189 * without calling any user handlers.
190 */
191 save_previous_kprobe();
192 set_current_kprobe(p, regs);
193 p->nmissed++;
194 prepare_singlestep(p, regs);
195 kprobe_status = KPROBE_REENTER;
196 return 1;
1da177e4
LT
197 } else {
198 p = current_kprobe;
199 if (p->break_handler && p->break_handler(p, regs)) {
200 goto ss_probe;
201 }
202 }
203 /* If it's not ours, can't be delete race, (we hold lock). */
204 goto no_kprobe;
205 }
206
207 lock_kprobes();
208 p = get_kprobe(addr);
209 if (!p) {
210 unlock_kprobes();
211 if (regs->eflags & VM_MASK) {
212 /* We are in virtual-8086 mode. Return 0 */
213 goto no_kprobe;
214 }
215
216 if (*addr != BREAKPOINT_INSTRUCTION) {
217 /*
218 * The breakpoint instruction was removed right
219 * after we hit it. Another cpu has removed
220 * either a probepoint or a debugger breakpoint
221 * at this address. In either case, no further
222 * handling of this interrupt is appropriate.
bce06494
JK
223 * Back up over the (now missing) int3 and run
224 * the original instruction.
1da177e4 225 */
bce06494 226 regs->eip -= sizeof(kprobe_opcode_t);
1da177e4
LT
227 ret = 1;
228 }
229 /* Not one of ours: let kernel handle it */
230 goto no_kprobe;
231 }
232
66ff2d06
AM
233 /*
234 * This preempt_disable() matches the preempt_enable_no_resched()
235 * in post_kprobe_handler()
236 */
237 preempt_disable();
1da177e4 238 kprobe_status = KPROBE_HIT_ACTIVE;
417c8da6 239 set_current_kprobe(p, regs);
1da177e4
LT
240
241 if (p->pre_handler && p->pre_handler(p, regs))
242 /* handler has already set things up, so skip ss setup */
243 return 1;
244
245ss_probe:
246 prepare_singlestep(p, regs);
247 kprobe_status = KPROBE_HIT_SS;
248 return 1;
249
250no_kprobe:
1da177e4
LT
251 return ret;
252}
253
b94cce92
HN
254/*
255 * For function-return probes, init_kprobes() establishes a probepoint
256 * here. When a retprobed function returns, this probe is hit and
257 * trampoline_probe_handler() runs, calling the kretprobe's handler.
258 */
259 void kretprobe_trampoline_holder(void)
260 {
261 asm volatile ( ".global kretprobe_trampoline\n"
262 "kretprobe_trampoline: \n"
263 "nop\n");
264 }
265
266/*
267 * Called when we hit the probe point at kretprobe_trampoline
268 */
3d97ae5b 269int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
b94cce92 270{
4bdbd37f
RL
271 struct kretprobe_instance *ri = NULL;
272 struct hlist_head *head;
273 struct hlist_node *node, *tmp;
274 unsigned long orig_ret_address = 0;
275 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
b94cce92 276
4bdbd37f 277 head = kretprobe_inst_table_head(current);
b94cce92 278
4bdbd37f
RL
279 /*
280 * It is possible to have multiple instances associated with a given
281 * task either because an multiple functions in the call path
282 * have a return probe installed on them, and/or more then one return
283 * return probe was registered for a target function.
284 *
285 * We can handle this because:
286 * - instances are always inserted at the head of the list
287 * - when multiple return probes are registered for the same
288 * function, the first instance's ret_addr will point to the
289 * real return address, and all the rest will point to
290 * kretprobe_trampoline
291 */
292 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
293 if (ri->task != current)
294 /* another task is sharing our hash bucket */
295 continue;
296
297 if (ri->rp && ri->rp->handler)
298 ri->rp->handler(ri, regs);
299
300 orig_ret_address = (unsigned long)ri->ret_addr;
b94cce92 301 recycle_rp_inst(ri);
4bdbd37f
RL
302
303 if (orig_ret_address != trampoline_address)
304 /*
305 * This is the real return address. Any other
306 * instances associated with this task are for
307 * other calls deeper on the call stack
308 */
309 break;
b94cce92 310 }
4bdbd37f
RL
311
312 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
313 regs->eip = orig_ret_address;
314
315 unlock_kprobes();
316 preempt_enable_no_resched();
317
66ff2d06
AM
318 /*
319 * By returning a non-zero value, we are telling
320 * kprobe_handler() that we have handled unlocking
321 * and re-enabling preemption
322 */
4bdbd37f 323 return 1;
b94cce92
HN
324}
325
1da177e4
LT
326/*
327 * Called after single-stepping. p->addr is the address of the
328 * instruction whose first byte has been replaced by the "int 3"
329 * instruction. To avoid the SMP problems that can occur when we
330 * temporarily put back the original opcode to single-step, we
331 * single-stepped a copy of the instruction. The address of this
332 * copy is p->ainsn.insn.
333 *
334 * This function prepares to return from the post-single-step
335 * interrupt. We have to fix up the stack as follows:
336 *
337 * 0) Except in the case of absolute or indirect jump or call instructions,
338 * the new eip is relative to the copied instruction. We need to make
339 * it relative to the original instruction.
340 *
341 * 1) If the single-stepped instruction was pushfl, then the TF and IF
342 * flags are set in the just-pushed eflags, and may need to be cleared.
343 *
344 * 2) If the single-stepped instruction was a call, the return address
345 * that is atop the stack is the address following the copied instruction.
346 * We need to make it the address following the original instruction.
347 */
3d97ae5b 348static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs)
1da177e4
LT
349{
350 unsigned long *tos = (unsigned long *)&regs->esp;
351 unsigned long next_eip = 0;
352 unsigned long copy_eip = (unsigned long)&p->ainsn.insn;
353 unsigned long orig_eip = (unsigned long)p->addr;
354
355 switch (p->ainsn.insn[0]) {
356 case 0x9c: /* pushfl */
357 *tos &= ~(TF_MASK | IF_MASK);
358 *tos |= kprobe_old_eflags;
359 break;
0b9e2cac
PP
360 case 0xc3: /* ret/lret */
361 case 0xcb:
362 case 0xc2:
363 case 0xca:
364 regs->eflags &= ~TF_MASK;
365 /* eip is already adjusted, no more changes required*/
366 return;
1da177e4
LT
367 case 0xe8: /* call relative - Fix return addr */
368 *tos = orig_eip + (*tos - copy_eip);
369 break;
370 case 0xff:
371 if ((p->ainsn.insn[1] & 0x30) == 0x10) {
372 /* call absolute, indirect */
373 /* Fix return addr; eip is correct. */
374 next_eip = regs->eip;
375 *tos = orig_eip + (*tos - copy_eip);
376 } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
377 ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
378 /* eip is correct. */
379 next_eip = regs->eip;
380 }
381 break;
382 case 0xea: /* jmp absolute -- eip is correct */
383 next_eip = regs->eip;
384 break;
385 default:
386 break;
387 }
388
389 regs->eflags &= ~TF_MASK;
390 if (next_eip) {
391 regs->eip = next_eip;
392 } else {
393 regs->eip = orig_eip + (regs->eip - copy_eip);
394 }
395}
396
397/*
398 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
399 * remain disabled thoroughout this function. And we hold kprobe lock.
400 */
401static inline int post_kprobe_handler(struct pt_regs *regs)
402{
403 if (!kprobe_running())
404 return 0;
405
417c8da6
PP
406 if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) {
407 kprobe_status = KPROBE_HIT_SSDONE;
1da177e4 408 current_kprobe->post_handler(current_kprobe, regs, 0);
417c8da6 409 }
1da177e4 410
4bdbd37f 411 resume_execution(current_kprobe, regs);
1da177e4
LT
412 regs->eflags |= kprobe_saved_eflags;
413
417c8da6
PP
414 /*Restore back the original saved kprobes variables and continue. */
415 if (kprobe_status == KPROBE_REENTER) {
416 restore_previous_kprobe();
417 goto out;
418 }
1da177e4 419 unlock_kprobes();
417c8da6 420out:
1da177e4
LT
421 preempt_enable_no_resched();
422
423 /*
424 * if somebody else is singlestepping across a probe point, eflags
425 * will have TF set, in which case, continue the remaining processing
426 * of do_debug, as if this is not a probe hit.
427 */
428 if (regs->eflags & TF_MASK)
429 return 0;
430
431 return 1;
432}
433
434/* Interrupts disabled, kprobe_lock held. */
435static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
436{
437 if (current_kprobe->fault_handler
438 && current_kprobe->fault_handler(current_kprobe, regs, trapnr))
439 return 1;
440
441 if (kprobe_status & KPROBE_HIT_SS) {
442 resume_execution(current_kprobe, regs);
443 regs->eflags |= kprobe_old_eflags;
444
445 unlock_kprobes();
446 preempt_enable_no_resched();
447 }
448 return 0;
449}
450
451/*
452 * Wrapper routine to for handling exceptions.
453 */
3d97ae5b
PP
454int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
455 unsigned long val, void *data)
1da177e4
LT
456{
457 struct die_args *args = (struct die_args *)data;
66ff2d06
AM
458 int ret = NOTIFY_DONE;
459
460 preempt_disable();
1da177e4
LT
461 switch (val) {
462 case DIE_INT3:
463 if (kprobe_handler(args->regs))
66ff2d06 464 ret = NOTIFY_STOP;
1da177e4
LT
465 break;
466 case DIE_DEBUG:
467 if (post_kprobe_handler(args->regs))
66ff2d06 468 ret = NOTIFY_STOP;
1da177e4
LT
469 break;
470 case DIE_GPF:
1da177e4
LT
471 case DIE_PAGE_FAULT:
472 if (kprobe_running() &&
473 kprobe_fault_handler(args->regs, args->trapnr))
66ff2d06 474 ret = NOTIFY_STOP;
1da177e4
LT
475 break;
476 default:
477 break;
478 }
66ff2d06
AM
479 preempt_enable();
480 return ret;
1da177e4
LT
481}
482
3d97ae5b 483int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
1da177e4
LT
484{
485 struct jprobe *jp = container_of(p, struct jprobe, kp);
486 unsigned long addr;
487
488 jprobe_saved_regs = *regs;
489 jprobe_saved_esp = &regs->esp;
490 addr = (unsigned long)jprobe_saved_esp;
491
492 /*
493 * TBD: As Linus pointed out, gcc assumes that the callee
494 * owns the argument space and could overwrite it, e.g.
495 * tailcall optimization. So, to be absolutely safe
496 * we also save and restore enough stack bytes to cover
497 * the argument area.
498 */
499 memcpy(jprobes_stack, (kprobe_opcode_t *) addr, MIN_STACK_SIZE(addr));
500 regs->eflags &= ~IF_MASK;
501 regs->eip = (unsigned long)(jp->entry);
502 return 1;
503}
504
3d97ae5b 505void __kprobes jprobe_return(void)
1da177e4 506{
1da177e4
LT
507 asm volatile (" xchgl %%ebx,%%esp \n"
508 " int3 \n"
509 " .globl jprobe_return_end \n"
510 " jprobe_return_end: \n"
511 " nop \n"::"b"
512 (jprobe_saved_esp):"memory");
513}
514
3d97ae5b 515int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
1da177e4
LT
516{
517 u8 *addr = (u8 *) (regs->eip - 1);
518 unsigned long stack_addr = (unsigned long)jprobe_saved_esp;
519 struct jprobe *jp = container_of(p, struct jprobe, kp);
520
521 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
522 if (&regs->esp != jprobe_saved_esp) {
523 struct pt_regs *saved_regs =
524 container_of(jprobe_saved_esp, struct pt_regs, esp);
525 printk("current esp %p does not match saved esp %p\n",
526 &regs->esp, jprobe_saved_esp);
527 printk("Saved registers for jprobe %p\n", jp);
528 show_registers(saved_regs);
529 printk("Current registers\n");
530 show_registers(regs);
531 BUG();
532 }
533 *regs = jprobe_saved_regs;
534 memcpy((kprobe_opcode_t *) stack_addr, jprobes_stack,
535 MIN_STACK_SIZE(stack_addr));
536 return 1;
537 }
538 return 0;
539}
4bdbd37f
RL
540
541static struct kprobe trampoline_p = {
542 .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
543 .pre_handler = trampoline_probe_handler
544};
545
6772926b 546int __init arch_init_kprobes(void)
4bdbd37f
RL
547{
548 return register_kprobe(&trampoline_p);
549}