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