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 | |
Masami Hiramatsu | 311ac88 | 2006-03-26 01:38:17 -0800 | [diff] [blame] | 44 | /* insert a jmp code */ |
| 45 | static inline void set_jmp_op(void *from, void *to) |
| 46 | { |
| 47 | struct __arch_jmp_op { |
| 48 | char op; |
| 49 | long raddr; |
| 50 | } __attribute__((packed)) *jop; |
| 51 | jop = (struct __arch_jmp_op *)from; |
| 52 | jop->raddr = (long)(to) - ((long)(from) + 5); |
| 53 | jop->op = RELATIVEJUMP_INSTRUCTION; |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * returns non-zero if opcodes can be boosted. |
| 58 | */ |
| 59 | static inline int can_boost(kprobe_opcode_t opcode) |
| 60 | { |
| 61 | switch (opcode & 0xf0 ) { |
| 62 | case 0x70: |
| 63 | return 0; /* can't boost conditional jump */ |
| 64 | case 0x90: |
| 65 | /* can't boost call and pushf */ |
| 66 | return opcode != 0x9a && opcode != 0x9c; |
| 67 | case 0xc0: |
| 68 | /* can't boost undefined opcodes and soft-interruptions */ |
| 69 | return (0xc1 < opcode && opcode < 0xc6) || |
| 70 | (0xc7 < opcode && opcode < 0xcc) || opcode == 0xcf; |
| 71 | case 0xd0: |
| 72 | /* can boost AA* and XLAT */ |
| 73 | return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7); |
| 74 | case 0xe0: |
| 75 | /* can boost in/out and (may be) jmps */ |
| 76 | return (0xe3 < opcode && opcode != 0xe8); |
| 77 | case 0xf0: |
| 78 | /* clear and set flags can be boost */ |
| 79 | return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe)); |
| 80 | default: |
| 81 | /* currently, can't boost 2 bytes opcodes */ |
| 82 | return opcode != 0x0f; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | /* |
| 88 | * returns non-zero if opcode modifies the interrupt flag. |
| 89 | */ |
| 90 | static inline int is_IF_modifier(kprobe_opcode_t opcode) |
| 91 | { |
| 92 | switch (opcode) { |
| 93 | case 0xfa: /* cli */ |
| 94 | case 0xfb: /* sti */ |
| 95 | case 0xcf: /* iret/iretd */ |
| 96 | case 0x9d: /* popf/popfd */ |
| 97 | return 1; |
| 98 | } |
| 99 | return 0; |
| 100 | } |
| 101 | |
Prasanna S Panchamukhi | 3d97ae5 | 2005-09-06 15:19:27 -0700 | [diff] [blame] | 102 | int __kprobes arch_prepare_kprobe(struct kprobe *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
Prasanna S Panchamukhi | 124d90b | 2006-02-24 13:04:08 -0800 | [diff] [blame] | 104 | /* insn: must be on special executable page on i386. */ |
| 105 | p->ainsn.insn = get_insn_slot(); |
| 106 | if (!p->ainsn.insn) |
| 107 | return -ENOMEM; |
| 108 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | 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] | 110 | p->opcode = *p->addr; |
Masami Hiramatsu | 311ac88 | 2006-03-26 01:38:17 -0800 | [diff] [blame] | 111 | if (can_boost(p->opcode)) { |
| 112 | p->ainsn.boostable = 0; |
| 113 | } else { |
| 114 | p->ainsn.boostable = -1; |
| 115 | } |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 116 | return 0; |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Prasanna S Panchamukhi | 3d97ae5 | 2005-09-06 15:19:27 -0700 | [diff] [blame] | 119 | void __kprobes arch_arm_kprobe(struct kprobe *p) |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 120 | { |
| 121 | *p->addr = BREAKPOINT_INSTRUCTION; |
| 122 | flush_icache_range((unsigned long) p->addr, |
| 123 | (unsigned long) p->addr + sizeof(kprobe_opcode_t)); |
| 124 | } |
| 125 | |
Prasanna S Panchamukhi | 3d97ae5 | 2005-09-06 15:19:27 -0700 | [diff] [blame] | 126 | void __kprobes arch_disarm_kprobe(struct kprobe *p) |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 127 | { |
| 128 | *p->addr = p->opcode; |
| 129 | flush_icache_range((unsigned long) p->addr, |
| 130 | (unsigned long) p->addr + sizeof(kprobe_opcode_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Prasanna S Panchamukhi | 124d90b | 2006-02-24 13:04:08 -0800 | [diff] [blame] | 133 | void __kprobes arch_remove_kprobe(struct kprobe *p) |
| 134 | { |
Ingo Molnar | 7a7d1cf | 2006-03-23 03:00:35 -0800 | [diff] [blame] | 135 | mutex_lock(&kprobe_mutex); |
Prasanna S Panchamukhi | 124d90b | 2006-02-24 13:04:08 -0800 | [diff] [blame] | 136 | free_insn_slot(p->ainsn.insn); |
Ingo Molnar | 7a7d1cf | 2006-03-23 03:00:35 -0800 | [diff] [blame] | 137 | mutex_unlock(&kprobe_mutex); |
Prasanna S Panchamukhi | 124d90b | 2006-02-24 13:04:08 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 140 | static inline void save_previous_kprobe(struct kprobe_ctlblk *kcb) |
Prasanna S Panchamukhi | 417c8da | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 141 | { |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 142 | kcb->prev_kprobe.kp = kprobe_running(); |
| 143 | kcb->prev_kprobe.status = kcb->kprobe_status; |
| 144 | kcb->prev_kprobe.old_eflags = kcb->kprobe_old_eflags; |
| 145 | kcb->prev_kprobe.saved_eflags = kcb->kprobe_saved_eflags; |
Prasanna S Panchamukhi | 417c8da | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 148 | static inline void restore_previous_kprobe(struct kprobe_ctlblk *kcb) |
Prasanna S Panchamukhi | 417c8da | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 149 | { |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 150 | __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp; |
| 151 | kcb->kprobe_status = kcb->prev_kprobe.status; |
| 152 | kcb->kprobe_old_eflags = kcb->prev_kprobe.old_eflags; |
| 153 | kcb->kprobe_saved_eflags = kcb->prev_kprobe.saved_eflags; |
Prasanna S Panchamukhi | 417c8da | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 156 | static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs, |
| 157 | struct kprobe_ctlblk *kcb) |
Prasanna S Panchamukhi | 417c8da | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 158 | { |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 159 | __get_cpu_var(current_kprobe) = p; |
| 160 | kcb->kprobe_saved_eflags = kcb->kprobe_old_eflags |
Prasanna S Panchamukhi | 417c8da | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 161 | = (regs->eflags & (TF_MASK | IF_MASK)); |
| 162 | if (is_IF_modifier(p->opcode)) |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 163 | kcb->kprobe_saved_eflags &= ~IF_MASK; |
Prasanna S Panchamukhi | 417c8da | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs) |
| 167 | { |
| 168 | regs->eflags |= TF_MASK; |
| 169 | regs->eflags &= ~IF_MASK; |
| 170 | /*single step inline if the instruction is an int3*/ |
| 171 | if (p->opcode == BREAKPOINT_INSTRUCTION) |
| 172 | regs->eip = (unsigned long)p->addr; |
| 173 | else |
Prasanna S Panchamukhi | 124d90b | 2006-02-24 13:04:08 -0800 | [diff] [blame] | 174 | regs->eip = (unsigned long)p->ainsn.insn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Ananth N Mavinakayanahalli | 991a51d | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 177 | /* Called with kretprobe_lock held */ |
Prasanna S Panchamukhi | 3d97ae5 | 2005-09-06 15:19:27 -0700 | [diff] [blame] | 178 | void __kprobes arch_prepare_kretprobe(struct kretprobe *rp, |
| 179 | struct pt_regs *regs) |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 180 | { |
| 181 | unsigned long *sara = (unsigned long *)®s->esp; |
Rusty Lynch | 4bdbd37 | 2005-06-27 15:17:09 -0700 | [diff] [blame] | 182 | struct kretprobe_instance *ri; |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 183 | |
Rusty Lynch | 4bdbd37 | 2005-06-27 15:17:09 -0700 | [diff] [blame] | 184 | if ((ri = get_free_rp_inst(rp)) != NULL) { |
| 185 | ri->rp = rp; |
| 186 | ri->task = current; |
| 187 | ri->ret_addr = (kprobe_opcode_t *) *sara; |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 188 | |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 189 | /* Replace the return addr with trampoline addr */ |
| 190 | *sara = (unsigned long) &kretprobe_trampoline; |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 191 | |
Rusty Lynch | 4bdbd37 | 2005-06-27 15:17:09 -0700 | [diff] [blame] | 192 | add_rp_inst(ri); |
| 193 | } else { |
| 194 | rp->nmissed++; |
| 195 | } |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | /* |
| 199 | * Interrupts are disabled on entry as trap3 is an interrupt gate and they |
| 200 | * remain disabled thorough out this function. |
| 201 | */ |
Prasanna S Panchamukhi | 3d97ae5 | 2005-09-06 15:19:27 -0700 | [diff] [blame] | 202 | static int __kprobes kprobe_handler(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
| 204 | struct kprobe *p; |
| 205 | int ret = 0; |
| 206 | kprobe_opcode_t *addr = NULL; |
| 207 | unsigned long *lp; |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 208 | struct kprobe_ctlblk *kcb; |
Masami Hiramatsu | 311ac88 | 2006-03-26 01:38:17 -0800 | [diff] [blame] | 209 | #ifdef CONFIG_PREEMPT |
| 210 | unsigned pre_preempt_count = preempt_count(); |
| 211 | #endif /* CONFIG_PREEMPT */ |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 212 | |
| 213 | /* |
| 214 | * We don't want to be preempted for the entire |
| 215 | * duration of kprobe processing |
| 216 | */ |
| 217 | preempt_disable(); |
| 218 | kcb = get_kprobe_ctlblk(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | /* Check if the application is using LDT entry for its code segment and |
| 221 | * calculate the address by reading the base address from the LDT entry. |
| 222 | */ |
| 223 | if ((regs->xcs & 4) && (current->mm)) { |
| 224 | lp = (unsigned long *) ((unsigned long)((regs->xcs >> 3) * 8) |
| 225 | + (char *) current->mm->context.ldt); |
| 226 | addr = (kprobe_opcode_t *) (get_desc_base(lp) + regs->eip - |
| 227 | sizeof(kprobe_opcode_t)); |
| 228 | } else { |
| 229 | addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t)); |
| 230 | } |
| 231 | /* Check we're not actually recursing */ |
| 232 | if (kprobe_running()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | p = get_kprobe(addr); |
| 234 | if (p) { |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 235 | if (kcb->kprobe_status == KPROBE_HIT_SS && |
Keshavamurthy Anil S | deac66a | 2005-09-06 15:19:35 -0700 | [diff] [blame] | 236 | *p->ainsn.insn == BREAKPOINT_INSTRUCTION) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | regs->eflags &= ~TF_MASK; |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 238 | regs->eflags |= kcb->kprobe_saved_eflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | goto no_kprobe; |
| 240 | } |
Prasanna S Panchamukhi | 417c8da | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 241 | /* We have reentered the kprobe_handler(), since |
| 242 | * another probe was hit while within the handler. |
| 243 | * We here save the original kprobes variables and |
| 244 | * just single step on the instruction of the new probe |
| 245 | * without calling any user handlers. |
| 246 | */ |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 247 | save_previous_kprobe(kcb); |
| 248 | set_current_kprobe(p, regs, kcb); |
Keshavamurthy Anil S | bf8d5c5 | 2005-12-12 00:37:34 -0800 | [diff] [blame] | 249 | kprobes_inc_nmissed_count(p); |
Prasanna S Panchamukhi | 417c8da | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 250 | prepare_singlestep(p, regs); |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 251 | kcb->kprobe_status = KPROBE_REENTER; |
Prasanna S Panchamukhi | 417c8da | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 252 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } else { |
Keshavamurthy Anil S | eb3a729 | 2006-01-11 12:17:42 -0800 | [diff] [blame] | 254 | if (regs->eflags & VM_MASK) { |
| 255 | /* We are in virtual-8086 mode. Return 0 */ |
| 256 | goto no_kprobe; |
| 257 | } |
| 258 | if (*addr != BREAKPOINT_INSTRUCTION) { |
| 259 | /* The breakpoint instruction was removed by |
| 260 | * another cpu right after we hit, no further |
| 261 | * handling of this interrupt is appropriate |
| 262 | */ |
| 263 | regs->eip -= sizeof(kprobe_opcode_t); |
| 264 | ret = 1; |
| 265 | goto no_kprobe; |
| 266 | } |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 267 | p = __get_cpu_var(current_kprobe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | if (p->break_handler && p->break_handler(p, regs)) { |
| 269 | goto ss_probe; |
| 270 | } |
| 271 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | goto no_kprobe; |
| 273 | } |
| 274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | p = get_kprobe(addr); |
| 276 | if (!p) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | if (regs->eflags & VM_MASK) { |
| 278 | /* We are in virtual-8086 mode. Return 0 */ |
| 279 | goto no_kprobe; |
| 280 | } |
| 281 | |
| 282 | if (*addr != BREAKPOINT_INSTRUCTION) { |
| 283 | /* |
| 284 | * The breakpoint instruction was removed right |
| 285 | * after we hit it. Another cpu has removed |
| 286 | * either a probepoint or a debugger breakpoint |
| 287 | * at this address. In either case, no further |
| 288 | * handling of this interrupt is appropriate. |
Jim Keniston | bce0649 | 2005-09-06 15:19:34 -0700 | [diff] [blame] | 289 | * Back up over the (now missing) int3 and run |
| 290 | * the original instruction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | */ |
Jim Keniston | bce0649 | 2005-09-06 15:19:34 -0700 | [diff] [blame] | 292 | regs->eip -= sizeof(kprobe_opcode_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | ret = 1; |
| 294 | } |
| 295 | /* Not one of ours: let kernel handle it */ |
| 296 | goto no_kprobe; |
| 297 | } |
| 298 | |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 299 | set_current_kprobe(p, regs, kcb); |
| 300 | kcb->kprobe_status = KPROBE_HIT_ACTIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
| 302 | if (p->pre_handler && p->pre_handler(p, regs)) |
| 303 | /* handler has already set things up, so skip ss setup */ |
| 304 | return 1; |
| 305 | |
Masami Hiramatsu | 311ac88 | 2006-03-26 01:38:17 -0800 | [diff] [blame] | 306 | if (p->ainsn.boostable == 1 && |
| 307 | #ifdef CONFIG_PREEMPT |
| 308 | !(pre_preempt_count) && /* |
| 309 | * This enables booster when the direct |
| 310 | * execution path aren't preempted. |
| 311 | */ |
| 312 | #endif /* CONFIG_PREEMPT */ |
| 313 | !p->post_handler && !p->break_handler ) { |
| 314 | /* Boost up -- we can execute copied instructions directly */ |
| 315 | reset_current_kprobe(); |
| 316 | regs->eip = (unsigned long)p->ainsn.insn; |
| 317 | preempt_enable_no_resched(); |
| 318 | return 1; |
| 319 | } |
| 320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | ss_probe: |
| 322 | prepare_singlestep(p, regs); |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 323 | kcb->kprobe_status = KPROBE_HIT_SS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | return 1; |
| 325 | |
| 326 | no_kprobe: |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 327 | preempt_enable_no_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | return ret; |
| 329 | } |
| 330 | |
| 331 | /* |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 332 | * For function-return probes, init_kprobes() establishes a probepoint |
| 333 | * here. When a retprobed function returns, this probe is hit and |
| 334 | * trampoline_probe_handler() runs, calling the kretprobe's handler. |
| 335 | */ |
Masami Hiramatsu | c9becf5 | 2006-03-26 01:38:19 -0800 | [diff] [blame^] | 336 | void __kprobes kretprobe_trampoline_holder(void) |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 337 | { |
Masami Hiramatsu | c9becf5 | 2006-03-26 01:38:19 -0800 | [diff] [blame^] | 338 | asm volatile ( ".global kretprobe_trampoline\n" |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 339 | "kretprobe_trampoline: \n" |
Masami Hiramatsu | c9becf5 | 2006-03-26 01:38:19 -0800 | [diff] [blame^] | 340 | " pushf\n" |
| 341 | /* skip cs, eip, orig_eax, es, ds */ |
| 342 | " subl $20, %esp\n" |
| 343 | " pushl %eax\n" |
| 344 | " pushl %ebp\n" |
| 345 | " pushl %edi\n" |
| 346 | " pushl %esi\n" |
| 347 | " pushl %edx\n" |
| 348 | " pushl %ecx\n" |
| 349 | " pushl %ebx\n" |
| 350 | " movl %esp, %eax\n" |
| 351 | " call trampoline_handler\n" |
| 352 | /* move eflags to cs */ |
| 353 | " movl 48(%esp), %edx\n" |
| 354 | " movl %edx, 44(%esp)\n" |
| 355 | /* save true return address on eflags */ |
| 356 | " movl %eax, 48(%esp)\n" |
| 357 | " popl %ebx\n" |
| 358 | " popl %ecx\n" |
| 359 | " popl %edx\n" |
| 360 | " popl %esi\n" |
| 361 | " popl %edi\n" |
| 362 | " popl %ebp\n" |
| 363 | " popl %eax\n" |
| 364 | /* skip eip, orig_eax, es, ds */ |
| 365 | " addl $16, %esp\n" |
| 366 | " popf\n" |
| 367 | " ret\n"); |
| 368 | } |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 369 | |
| 370 | /* |
Masami Hiramatsu | c9becf5 | 2006-03-26 01:38:19 -0800 | [diff] [blame^] | 371 | * Called from kretprobe_trampoline |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 372 | */ |
Masami Hiramatsu | c9becf5 | 2006-03-26 01:38:19 -0800 | [diff] [blame^] | 373 | fastcall void *__kprobes trampoline_handler(struct pt_regs *regs) |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 374 | { |
Rusty Lynch | 4bdbd37 | 2005-06-27 15:17:09 -0700 | [diff] [blame] | 375 | struct kretprobe_instance *ri = NULL; |
| 376 | struct hlist_head *head; |
| 377 | struct hlist_node *node, *tmp; |
Ananth N Mavinakayanahalli | 991a51d | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 378 | unsigned long flags, orig_ret_address = 0; |
Rusty Lynch | 4bdbd37 | 2005-06-27 15:17:09 -0700 | [diff] [blame] | 379 | unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline; |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 380 | |
Ananth N Mavinakayanahalli | 991a51d | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 381 | spin_lock_irqsave(&kretprobe_lock, flags); |
Rusty Lynch | 4bdbd37 | 2005-06-27 15:17:09 -0700 | [diff] [blame] | 382 | head = kretprobe_inst_table_head(current); |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 383 | |
Rusty Lynch | 4bdbd37 | 2005-06-27 15:17:09 -0700 | [diff] [blame] | 384 | /* |
| 385 | * It is possible to have multiple instances associated with a given |
| 386 | * task either because an multiple functions in the call path |
| 387 | * have a return probe installed on them, and/or more then one return |
| 388 | * return probe was registered for a target function. |
| 389 | * |
| 390 | * We can handle this because: |
| 391 | * - instances are always inserted at the head of the list |
| 392 | * - when multiple return probes are registered for the same |
| 393 | * function, the first instance's ret_addr will point to the |
| 394 | * real return address, and all the rest will point to |
| 395 | * kretprobe_trampoline |
| 396 | */ |
| 397 | hlist_for_each_entry_safe(ri, node, tmp, head, hlist) { |
| 398 | if (ri->task != current) |
| 399 | /* another task is sharing our hash bucket */ |
| 400 | continue; |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 401 | |
Masami Hiramatsu | c9becf5 | 2006-03-26 01:38:19 -0800 | [diff] [blame^] | 402 | if (ri->rp && ri->rp->handler){ |
| 403 | __get_cpu_var(current_kprobe) = &ri->rp->kp; |
Rusty Lynch | 4bdbd37 | 2005-06-27 15:17:09 -0700 | [diff] [blame] | 404 | ri->rp->handler(ri, regs); |
Masami Hiramatsu | c9becf5 | 2006-03-26 01:38:19 -0800 | [diff] [blame^] | 405 | __get_cpu_var(current_kprobe) = NULL; |
| 406 | } |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 407 | |
Rusty Lynch | 4bdbd37 | 2005-06-27 15:17:09 -0700 | [diff] [blame] | 408 | orig_ret_address = (unsigned long)ri->ret_addr; |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 409 | recycle_rp_inst(ri); |
Rusty Lynch | 4bdbd37 | 2005-06-27 15:17:09 -0700 | [diff] [blame] | 410 | |
| 411 | if (orig_ret_address != trampoline_address) |
| 412 | /* |
| 413 | * This is the real return address. Any other |
| 414 | * instances associated with this task are for |
| 415 | * other calls deeper on the call stack |
| 416 | */ |
| 417 | break; |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 418 | } |
Rusty Lynch | 4bdbd37 | 2005-06-27 15:17:09 -0700 | [diff] [blame] | 419 | |
| 420 | BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address)); |
Rusty Lynch | 4bdbd37 | 2005-06-27 15:17:09 -0700 | [diff] [blame] | 421 | |
Ananth N Mavinakayanahalli | 991a51d | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 422 | spin_unlock_irqrestore(&kretprobe_lock, flags); |
Rusty Lynch | 4bdbd37 | 2005-06-27 15:17:09 -0700 | [diff] [blame] | 423 | |
Masami Hiramatsu | c9becf5 | 2006-03-26 01:38:19 -0800 | [diff] [blame^] | 424 | return (void*)orig_ret_address; |
Hien Nguyen | b94cce9 | 2005-06-23 00:09:19 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | * Called after single-stepping. p->addr is the address of the |
| 429 | * instruction whose first byte has been replaced by the "int 3" |
| 430 | * instruction. To avoid the SMP problems that can occur when we |
| 431 | * temporarily put back the original opcode to single-step, we |
| 432 | * single-stepped a copy of the instruction. The address of this |
| 433 | * copy is p->ainsn.insn. |
| 434 | * |
| 435 | * This function prepares to return from the post-single-step |
| 436 | * interrupt. We have to fix up the stack as follows: |
| 437 | * |
| 438 | * 0) Except in the case of absolute or indirect jump or call instructions, |
| 439 | * the new eip is relative to the copied instruction. We need to make |
| 440 | * it relative to the original instruction. |
| 441 | * |
| 442 | * 1) If the single-stepped instruction was pushfl, then the TF and IF |
| 443 | * flags are set in the just-pushed eflags, and may need to be cleared. |
| 444 | * |
| 445 | * 2) If the single-stepped instruction was a call, the return address |
| 446 | * that is atop the stack is the address following the copied instruction. |
| 447 | * We need to make it the address following the original instruction. |
Masami Hiramatsu | 311ac88 | 2006-03-26 01:38:17 -0800 | [diff] [blame] | 448 | * |
| 449 | * This function also checks instruction size for preparing direct execution. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | */ |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 451 | static void __kprobes resume_execution(struct kprobe *p, |
| 452 | struct pt_regs *regs, struct kprobe_ctlblk *kcb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | { |
| 454 | unsigned long *tos = (unsigned long *)®s->esp; |
Prasanna S Panchamukhi | 124d90b | 2006-02-24 13:04:08 -0800 | [diff] [blame] | 455 | unsigned long copy_eip = (unsigned long)p->ainsn.insn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | unsigned long orig_eip = (unsigned long)p->addr; |
| 457 | |
Masami Hiramatsu | b50ea74 | 2006-03-26 01:38:13 -0800 | [diff] [blame] | 458 | regs->eflags &= ~TF_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | switch (p->ainsn.insn[0]) { |
| 460 | case 0x9c: /* pushfl */ |
| 461 | *tos &= ~(TF_MASK | IF_MASK); |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 462 | *tos |= kcb->kprobe_old_eflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | break; |
Prasanna S Panchamukhi | 0b9e2ca | 2005-05-05 16:15:40 -0700 | [diff] [blame] | 464 | case 0xc3: /* ret/lret */ |
| 465 | case 0xcb: |
| 466 | case 0xc2: |
| 467 | case 0xca: |
Masami Hiramatsu | b50ea74 | 2006-03-26 01:38:13 -0800 | [diff] [blame] | 468 | case 0xea: /* jmp absolute -- eip is correct */ |
| 469 | /* eip is already adjusted, no more changes required */ |
Masami Hiramatsu | 311ac88 | 2006-03-26 01:38:17 -0800 | [diff] [blame] | 470 | p->ainsn.boostable = 1; |
Masami Hiramatsu | b50ea74 | 2006-03-26 01:38:13 -0800 | [diff] [blame] | 471 | goto no_change; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | case 0xe8: /* call relative - Fix return addr */ |
| 473 | *tos = orig_eip + (*tos - copy_eip); |
| 474 | break; |
| 475 | case 0xff: |
| 476 | if ((p->ainsn.insn[1] & 0x30) == 0x10) { |
| 477 | /* call absolute, indirect */ |
Masami Hiramatsu | 311ac88 | 2006-03-26 01:38:17 -0800 | [diff] [blame] | 478 | /* |
| 479 | * Fix return addr; eip is correct. |
| 480 | * But this is not boostable |
| 481 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | *tos = orig_eip + (*tos - copy_eip); |
Masami Hiramatsu | b50ea74 | 2006-03-26 01:38:13 -0800 | [diff] [blame] | 483 | goto no_change; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */ |
| 485 | ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */ |
Masami Hiramatsu | 311ac88 | 2006-03-26 01:38:17 -0800 | [diff] [blame] | 486 | /* eip is correct. And this is boostable */ |
| 487 | p->ainsn.boostable = 1; |
Masami Hiramatsu | b50ea74 | 2006-03-26 01:38:13 -0800 | [diff] [blame] | 488 | goto no_change; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | default: |
| 491 | break; |
| 492 | } |
| 493 | |
Masami Hiramatsu | 311ac88 | 2006-03-26 01:38:17 -0800 | [diff] [blame] | 494 | if (p->ainsn.boostable == 0) { |
| 495 | if ((regs->eip > copy_eip) && |
| 496 | (regs->eip - copy_eip) + 5 < MAX_INSN_SIZE) { |
| 497 | /* |
| 498 | * These instructions can be executed directly if it |
| 499 | * jumps back to correct address. |
| 500 | */ |
| 501 | set_jmp_op((void *)regs->eip, |
| 502 | (void *)orig_eip + (regs->eip - copy_eip)); |
| 503 | p->ainsn.boostable = 1; |
| 504 | } else { |
| 505 | p->ainsn.boostable = -1; |
| 506 | } |
| 507 | } |
| 508 | |
Masami Hiramatsu | b50ea74 | 2006-03-26 01:38:13 -0800 | [diff] [blame] | 509 | regs->eip = orig_eip + (regs->eip - copy_eip); |
| 510 | |
| 511 | no_change: |
| 512 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /* |
| 516 | * 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] | 517 | * remain disabled thoroughout this function. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | */ |
| 519 | static inline int post_kprobe_handler(struct pt_regs *regs) |
| 520 | { |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 521 | struct kprobe *cur = kprobe_running(); |
| 522 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 523 | |
| 524 | if (!cur) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | return 0; |
| 526 | |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 527 | if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) { |
| 528 | kcb->kprobe_status = KPROBE_HIT_SSDONE; |
| 529 | cur->post_handler(cur, regs, 0); |
Prasanna S Panchamukhi | 417c8da | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 530 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 532 | resume_execution(cur, regs, kcb); |
| 533 | regs->eflags |= kcb->kprobe_saved_eflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | |
Prasanna S Panchamukhi | 417c8da | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 535 | /*Restore back the original saved kprobes variables and continue. */ |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 536 | if (kcb->kprobe_status == KPROBE_REENTER) { |
| 537 | restore_previous_kprobe(kcb); |
Prasanna S Panchamukhi | 417c8da | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 538 | goto out; |
| 539 | } |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 540 | reset_current_kprobe(); |
Prasanna S Panchamukhi | 417c8da | 2005-06-23 00:09:37 -0700 | [diff] [blame] | 541 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | preempt_enable_no_resched(); |
| 543 | |
| 544 | /* |
| 545 | * if somebody else is singlestepping across a probe point, eflags |
| 546 | * will have TF set, in which case, continue the remaining processing |
| 547 | * of do_debug, as if this is not a probe hit. |
| 548 | */ |
| 549 | if (regs->eflags & TF_MASK) |
| 550 | return 0; |
| 551 | |
| 552 | return 1; |
| 553 | } |
| 554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr) |
| 556 | { |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 557 | struct kprobe *cur = kprobe_running(); |
| 558 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 559 | |
| 560 | if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | return 1; |
| 562 | |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 563 | if (kcb->kprobe_status & KPROBE_HIT_SS) { |
| 564 | resume_execution(cur, regs, kcb); |
| 565 | regs->eflags |= kcb->kprobe_old_eflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 567 | reset_current_kprobe(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | preempt_enable_no_resched(); |
| 569 | } |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | * Wrapper routine to for handling exceptions. |
| 575 | */ |
Prasanna S Panchamukhi | 3d97ae5 | 2005-09-06 15:19:27 -0700 | [diff] [blame] | 576 | int __kprobes kprobe_exceptions_notify(struct notifier_block *self, |
| 577 | unsigned long val, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | { |
| 579 | struct die_args *args = (struct die_args *)data; |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 580 | int ret = NOTIFY_DONE; |
| 581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | switch (val) { |
| 583 | case DIE_INT3: |
| 584 | if (kprobe_handler(args->regs)) |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 585 | ret = NOTIFY_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | break; |
| 587 | case DIE_DEBUG: |
| 588 | if (post_kprobe_handler(args->regs)) |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 589 | ret = NOTIFY_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | break; |
| 591 | case DIE_GPF: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | case DIE_PAGE_FAULT: |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 593 | /* kprobe_running() needs smp_processor_id() */ |
| 594 | preempt_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | if (kprobe_running() && |
| 596 | kprobe_fault_handler(args->regs, args->trapnr)) |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 597 | ret = NOTIFY_STOP; |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 598 | preempt_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | break; |
| 600 | default: |
| 601 | break; |
| 602 | } |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 603 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Prasanna S Panchamukhi | 3d97ae5 | 2005-09-06 15:19:27 -0700 | [diff] [blame] | 606 | int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | { |
| 608 | struct jprobe *jp = container_of(p, struct jprobe, kp); |
| 609 | unsigned long addr; |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 610 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 612 | kcb->jprobe_saved_regs = *regs; |
| 613 | kcb->jprobe_saved_esp = ®s->esp; |
| 614 | addr = (unsigned long)(kcb->jprobe_saved_esp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | |
| 616 | /* |
| 617 | * TBD: As Linus pointed out, gcc assumes that the callee |
| 618 | * owns the argument space and could overwrite it, e.g. |
| 619 | * tailcall optimization. So, to be absolutely safe |
| 620 | * we also save and restore enough stack bytes to cover |
| 621 | * the argument area. |
| 622 | */ |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 623 | memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr, |
| 624 | MIN_STACK_SIZE(addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | regs->eflags &= ~IF_MASK; |
| 626 | regs->eip = (unsigned long)(jp->entry); |
| 627 | return 1; |
| 628 | } |
| 629 | |
Prasanna S Panchamukhi | 3d97ae5 | 2005-09-06 15:19:27 -0700 | [diff] [blame] | 630 | void __kprobes jprobe_return(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | { |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 632 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 633 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | asm volatile (" xchgl %%ebx,%%esp \n" |
| 635 | " int3 \n" |
| 636 | " .globl jprobe_return_end \n" |
| 637 | " jprobe_return_end: \n" |
| 638 | " nop \n"::"b" |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 639 | (kcb->jprobe_saved_esp):"memory"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Prasanna S Panchamukhi | 3d97ae5 | 2005-09-06 15:19:27 -0700 | [diff] [blame] | 642 | int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | { |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 644 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | u8 *addr = (u8 *) (regs->eip - 1); |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 646 | unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_esp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | struct jprobe *jp = container_of(p, struct jprobe, kp); |
| 648 | |
| 649 | if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) { |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 650 | if (®s->esp != kcb->jprobe_saved_esp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | struct pt_regs *saved_regs = |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 652 | container_of(kcb->jprobe_saved_esp, |
| 653 | struct pt_regs, esp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | printk("current esp %p does not match saved esp %p\n", |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 655 | ®s->esp, kcb->jprobe_saved_esp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | printk("Saved registers for jprobe %p\n", jp); |
| 657 | show_registers(saved_regs); |
| 658 | printk("Current registers\n"); |
| 659 | show_registers(regs); |
| 660 | BUG(); |
| 661 | } |
Ananth N Mavinakayanahalli | 9a0e3a8 | 2005-11-07 01:00:08 -0800 | [diff] [blame] | 662 | *regs = kcb->jprobe_saved_regs; |
| 663 | memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | MIN_STACK_SIZE(stack_addr)); |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 665 | preempt_enable_no_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | return 1; |
| 667 | } |
| 668 | return 0; |
| 669 | } |
Rusty Lynch | 4bdbd37 | 2005-06-27 15:17:09 -0700 | [diff] [blame] | 670 | |
Rusty Lynch | 6772926 | 2005-07-05 18:54:50 -0700 | [diff] [blame] | 671 | int __init arch_init_kprobes(void) |
Rusty Lynch | 4bdbd37 | 2005-06-27 15:17:09 -0700 | [diff] [blame] | 672 | { |
Masami Hiramatsu | c9becf5 | 2006-03-26 01:38:19 -0800 | [diff] [blame^] | 673 | return 0; |
Rusty Lynch | 4bdbd37 | 2005-06-27 15:17:09 -0700 | [diff] [blame] | 674 | } |