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