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