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