Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Kernel Probes (KProbes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | * |
| 18 | * Copyright (C) IBM Corporation, 2002, 2004 |
| 19 | * |
| 20 | * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel |
| 21 | * Probes initial implementation ( includes contributions from |
| 22 | * Rusty Russell). |
| 23 | * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes |
| 24 | * interface to access function arguments. |
| 25 | * 2004-Nov Ananth N Mavinakayanahalli <ananth@in.ibm.com> kprobes port |
| 26 | * for PPC64 |
| 27 | */ |
| 28 | |
| 29 | #include <linux/config.h> |
| 30 | #include <linux/kprobes.h> |
| 31 | #include <linux/ptrace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/preempt.h> |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 33 | #include <asm/cacheflush.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <asm/kdebug.h> |
| 35 | #include <asm/sstep.h> |
| 36 | |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 37 | DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; |
| 38 | DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Prasanna S Panchamukhi | bb144a8 | 2005-09-06 15:19:29 -0700 | [diff] [blame] | 40 | int __kprobes arch_prepare_kprobe(struct kprobe *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
Ananth N Mavinakayanahalli | 63224d1e8 | 2005-06-08 15:49:41 -0700 | [diff] [blame] | 42 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | kprobe_opcode_t insn = *p->addr; |
| 44 | |
Ananth N Mavinakayanahalli | 63224d1e8 | 2005-06-08 15:49:41 -0700 | [diff] [blame] | 45 | if ((unsigned long)p->addr & 0x03) { |
| 46 | printk("Attempt to register kprobe at an unaligned address\n"); |
| 47 | ret = -EINVAL; |
| 48 | } else if (IS_MTMSRD(insn) || IS_RFID(insn)) { |
| 49 | printk("Cannot register a kprobe on rfid or mtmsrd\n"); |
| 50 | ret = -EINVAL; |
| 51 | } |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 52 | |
| 53 | /* insn must be on a special executable page on ppc64 */ |
| 54 | if (!ret) { |
Ananth N Mavinakayanahalli | 2d8ab6a | 2005-10-01 13:14:17 -0400 | [diff] [blame] | 55 | p->ainsn.insn = get_insn_slot(); |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 56 | if (!p->ainsn.insn) |
| 57 | ret = -ENOMEM; |
| 58 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Anil S Keshavamurthy | 49a2a1b | 2006-01-09 20:52:43 -0800 | [diff] [blame] | 60 | if (!ret) { |
| 61 | memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t)); |
| 62 | p->opcode = *p->addr; |
| 63 | } |
| 64 | |
| 65 | return ret; |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Prasanna S Panchamukhi | bb144a8 | 2005-09-06 15:19:29 -0700 | [diff] [blame] | 68 | void __kprobes arch_arm_kprobe(struct kprobe *p) |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 69 | { |
| 70 | *p->addr = BREAKPOINT_INSTRUCTION; |
| 71 | flush_icache_range((unsigned long) p->addr, |
| 72 | (unsigned long) p->addr + sizeof(kprobe_opcode_t)); |
| 73 | } |
| 74 | |
Prasanna S Panchamukhi | bb144a8 | 2005-09-06 15:19:29 -0700 | [diff] [blame] | 75 | void __kprobes arch_disarm_kprobe(struct kprobe *p) |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 76 | { |
| 77 | *p->addr = p->opcode; |
| 78 | flush_icache_range((unsigned long) p->addr, |
| 79 | (unsigned long) p->addr + sizeof(kprobe_opcode_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Ananth N Mavinakayanahalli | 0498b63 | 2006-01-09 20:52:46 -0800 | [diff] [blame] | 82 | void __kprobes arch_remove_kprobe(struct kprobe *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | { |
Ingo Molnar | 7a7d1cf | 2006-03-23 03:00:35 -0800 | [diff] [blame] | 84 | mutex_lock(&kprobe_mutex); |
Ananth N Mavinakayanahalli | 2d8ab6a | 2005-10-01 13:14:17 -0400 | [diff] [blame] | 85 | free_insn_slot(p->ainsn.insn); |
Ingo Molnar | 7a7d1cf | 2006-03-23 03:00:35 -0800 | [diff] [blame] | 86 | mutex_unlock(&kprobe_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs) |
| 90 | { |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 91 | kprobe_opcode_t insn = *p->ainsn.insn; |
| 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | regs->msr |= MSR_SE; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 94 | |
| 95 | /* single step inline if it is a trap variant */ |
Keshavamurthy Anil S | deac66a | 2005-09-06 15:19:35 -0700 | [diff] [blame] | 96 | if (is_trap(insn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | regs->nip = (unsigned long)p->addr; |
| 98 | else |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 99 | regs->nip = (unsigned long)p->ainsn.insn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 102 | static inline void save_previous_kprobe(struct kprobe_ctlblk *kcb) |
Prasanna S Panchamukhi | 42cc206 | 2005-06-23 00:09:38 -0700 | [diff] [blame] | 103 | { |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 104 | kcb->prev_kprobe.kp = kprobe_running(); |
| 105 | kcb->prev_kprobe.status = kcb->kprobe_status; |
| 106 | kcb->prev_kprobe.saved_msr = kcb->kprobe_saved_msr; |
Prasanna S Panchamukhi | 42cc206 | 2005-06-23 00:09:38 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 109 | static inline void restore_previous_kprobe(struct kprobe_ctlblk *kcb) |
Prasanna S Panchamukhi | 42cc206 | 2005-06-23 00:09:38 -0700 | [diff] [blame] | 110 | { |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 111 | __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp; |
| 112 | kcb->kprobe_status = kcb->prev_kprobe.status; |
| 113 | kcb->kprobe_saved_msr = kcb->prev_kprobe.saved_msr; |
| 114 | } |
| 115 | |
| 116 | static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs, |
| 117 | struct kprobe_ctlblk *kcb) |
| 118 | { |
| 119 | __get_cpu_var(current_kprobe) = p; |
| 120 | kcb->kprobe_saved_msr = regs->msr; |
Prasanna S Panchamukhi | 42cc206 | 2005-06-23 00:09:38 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Ananth N Mavinakayanahalli | 991a51d | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 123 | /* Called with kretprobe_lock held */ |
Prasanna S Panchamukhi | bb144a8 | 2005-09-06 15:19:29 -0700 | [diff] [blame] | 124 | void __kprobes arch_prepare_kretprobe(struct kretprobe *rp, |
| 125 | struct pt_regs *regs) |
Rusty Lynch | 97f7943 | 2005-06-27 15:17:15 -0700 | [diff] [blame] | 126 | { |
| 127 | struct kretprobe_instance *ri; |
| 128 | |
| 129 | if ((ri = get_free_rp_inst(rp)) != NULL) { |
| 130 | ri->rp = rp; |
| 131 | ri->task = current; |
| 132 | ri->ret_addr = (kprobe_opcode_t *)regs->link; |
| 133 | |
| 134 | /* Replace the return addr with trampoline addr */ |
| 135 | regs->link = (unsigned long)kretprobe_trampoline; |
| 136 | add_rp_inst(ri); |
| 137 | } else { |
| 138 | rp->nmissed++; |
| 139 | } |
| 140 | } |
| 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | static inline int kprobe_handler(struct pt_regs *regs) |
| 143 | { |
| 144 | struct kprobe *p; |
| 145 | int ret = 0; |
| 146 | unsigned int *addr = (unsigned int *)regs->nip; |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 147 | struct kprobe_ctlblk *kcb; |
| 148 | |
| 149 | /* |
| 150 | * We don't want to be preempted for the entire |
| 151 | * duration of kprobe processing |
| 152 | */ |
| 153 | preempt_disable(); |
| 154 | kcb = get_kprobe_ctlblk(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
| 156 | /* Check we're not actually recursing */ |
| 157 | if (kprobe_running()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | p = get_kprobe(addr); |
| 159 | if (p) { |
Keshavamurthy Anil S | deac66a | 2005-09-06 15:19:35 -0700 | [diff] [blame] | 160 | kprobe_opcode_t insn = *p->ainsn.insn; |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 161 | if (kcb->kprobe_status == KPROBE_HIT_SS && |
Keshavamurthy Anil S | deac66a | 2005-09-06 15:19:35 -0700 | [diff] [blame] | 162 | is_trap(insn)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | regs->msr &= ~MSR_SE; |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 164 | regs->msr |= kcb->kprobe_saved_msr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | goto no_kprobe; |
| 166 | } |
Prasanna S Panchamukhi | 42cc206 | 2005-06-23 00:09:38 -0700 | [diff] [blame] | 167 | /* We have reentered the kprobe_handler(), since |
| 168 | * another probe was hit while within the handler. |
| 169 | * We here save the original kprobes variables and |
| 170 | * just single step on the instruction of the new probe |
| 171 | * without calling any user handlers. |
| 172 | */ |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 173 | save_previous_kprobe(kcb); |
| 174 | set_current_kprobe(p, regs, kcb); |
| 175 | kcb->kprobe_saved_msr = regs->msr; |
Keshavamurthy Anil S | bf8d5c5 | 2005-12-12 00:37:34 -0800 | [diff] [blame] | 176 | kprobes_inc_nmissed_count(p); |
Prasanna S Panchamukhi | 42cc206 | 2005-06-23 00:09:38 -0700 | [diff] [blame] | 177 | prepare_singlestep(p, regs); |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 178 | kcb->kprobe_status = KPROBE_REENTER; |
Prasanna S Panchamukhi | 42cc206 | 2005-06-23 00:09:38 -0700 | [diff] [blame] | 179 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } else { |
Keshavamurthy Anil S | eb3a729 | 2006-01-11 12:17:42 -0800 | [diff] [blame] | 181 | if (*addr != BREAKPOINT_INSTRUCTION) { |
| 182 | /* If trap variant, then it belongs not to us */ |
| 183 | kprobe_opcode_t cur_insn = *addr; |
| 184 | if (is_trap(cur_insn)) |
| 185 | goto no_kprobe; |
| 186 | /* The breakpoint instruction was removed by |
| 187 | * another cpu right after we hit, no further |
| 188 | * handling of this interrupt is appropriate |
| 189 | */ |
| 190 | ret = 1; |
| 191 | goto no_kprobe; |
| 192 | } |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 193 | p = __get_cpu_var(current_kprobe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | if (p->break_handler && p->break_handler(p, regs)) { |
| 195 | goto ss_probe; |
| 196 | } |
| 197 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | goto no_kprobe; |
| 199 | } |
| 200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | p = get_kprobe(addr); |
| 202 | if (!p) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | if (*addr != BREAKPOINT_INSTRUCTION) { |
| 204 | /* |
| 205 | * PowerPC has multiple variants of the "trap" |
| 206 | * instruction. If the current instruction is a |
| 207 | * trap variant, it could belong to someone else |
| 208 | */ |
| 209 | kprobe_opcode_t cur_insn = *addr; |
Keshavamurthy Anil S | deac66a | 2005-09-06 15:19:35 -0700 | [diff] [blame] | 210 | if (is_trap(cur_insn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | goto no_kprobe; |
| 212 | /* |
| 213 | * The breakpoint instruction was removed right |
| 214 | * after we hit it. Another cpu has removed |
| 215 | * either a probepoint or a debugger breakpoint |
| 216 | * at this address. In either case, no further |
| 217 | * handling of this interrupt is appropriate. |
| 218 | */ |
| 219 | ret = 1; |
| 220 | } |
| 221 | /* Not one of ours: let kernel handle it */ |
| 222 | goto no_kprobe; |
| 223 | } |
| 224 | |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 225 | kcb->kprobe_status = KPROBE_HIT_ACTIVE; |
| 226 | set_current_kprobe(p, regs, kcb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | if (p->pre_handler && p->pre_handler(p, regs)) |
| 228 | /* handler has already set things up, so skip ss setup */ |
| 229 | return 1; |
| 230 | |
| 231 | ss_probe: |
| 232 | prepare_singlestep(p, regs); |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 233 | kcb->kprobe_status = KPROBE_HIT_SS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | return 1; |
| 235 | |
| 236 | no_kprobe: |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 237 | preempt_enable_no_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | return ret; |
| 239 | } |
| 240 | |
| 241 | /* |
Rusty Lynch | 97f7943 | 2005-06-27 15:17:15 -0700 | [diff] [blame] | 242 | * Function return probe trampoline: |
| 243 | * - init_kprobes() establishes a probepoint here |
| 244 | * - When the probed function returns, this probe |
| 245 | * causes the handlers to fire |
| 246 | */ |
| 247 | void kretprobe_trampoline_holder(void) |
| 248 | { |
| 249 | asm volatile(".global kretprobe_trampoline\n" |
| 250 | "kretprobe_trampoline:\n" |
| 251 | "nop\n"); |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * Called when the probe at kretprobe trampoline is hit |
| 256 | */ |
Prasanna S Panchamukhi | bb144a8 | 2005-09-06 15:19:29 -0700 | [diff] [blame] | 257 | int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs) |
Rusty Lynch | 97f7943 | 2005-06-27 15:17:15 -0700 | [diff] [blame] | 258 | { |
| 259 | struct kretprobe_instance *ri = NULL; |
| 260 | struct hlist_head *head; |
| 261 | struct hlist_node *node, *tmp; |
Ananth N Mavinakayanahalli | 991a51d | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 262 | unsigned long flags, orig_ret_address = 0; |
Rusty Lynch | 97f7943 | 2005-06-27 15:17:15 -0700 | [diff] [blame] | 263 | unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline; |
| 264 | |
Ananth N Mavinakayanahalli | 991a51d | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 265 | spin_lock_irqsave(&kretprobe_lock, flags); |
Rusty Lynch | 97f7943 | 2005-06-27 15:17:15 -0700 | [diff] [blame] | 266 | head = kretprobe_inst_table_head(current); |
| 267 | |
| 268 | /* |
| 269 | * It is possible to have multiple instances associated with a given |
| 270 | * task either because an multiple functions in the call path |
| 271 | * have a return probe installed on them, and/or more then one return |
| 272 | * return probe was registered for a target function. |
| 273 | * |
| 274 | * We can handle this because: |
| 275 | * - instances are always inserted at the head of the list |
| 276 | * - when multiple return probes are registered for the same |
| 277 | * function, the first instance's ret_addr will point to the |
| 278 | * real return address, and all the rest will point to |
| 279 | * kretprobe_trampoline |
| 280 | */ |
| 281 | hlist_for_each_entry_safe(ri, node, tmp, head, hlist) { |
| 282 | if (ri->task != current) |
| 283 | /* another task is sharing our hash bucket */ |
| 284 | continue; |
| 285 | |
| 286 | if (ri->rp && ri->rp->handler) |
| 287 | ri->rp->handler(ri, regs); |
| 288 | |
| 289 | orig_ret_address = (unsigned long)ri->ret_addr; |
| 290 | recycle_rp_inst(ri); |
| 291 | |
| 292 | if (orig_ret_address != trampoline_address) |
| 293 | /* |
| 294 | * This is the real return address. Any other |
| 295 | * instances associated with this task are for |
| 296 | * other calls deeper on the call stack |
| 297 | */ |
| 298 | break; |
| 299 | } |
| 300 | |
| 301 | BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address)); |
| 302 | regs->nip = orig_ret_address; |
| 303 | |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 304 | reset_current_kprobe(); |
Ananth N Mavinakayanahalli | 991a51d | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 305 | spin_unlock_irqrestore(&kretprobe_lock, flags); |
Ananth N Mavinakayanahalli | 66ff2d0 | 2005-11-07 01:00:07 -0800 | [diff] [blame] | 306 | preempt_enable_no_resched(); |
Rusty Lynch | 97f7943 | 2005-06-27 15:17:15 -0700 | [diff] [blame] | 307 | |
| 308 | /* |
| 309 | * By returning a non-zero value, we are telling |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 310 | * kprobe_handler() that we don't want the post_handler |
| 311 | * to run (and have re-enabled preemption) |
Rusty Lynch | 97f7943 | 2005-06-27 15:17:15 -0700 | [diff] [blame] | 312 | */ |
| 313 | return 1; |
| 314 | } |
| 315 | |
| 316 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | * Called after single-stepping. p->addr is the address of the |
| 318 | * instruction whose first byte has been replaced by the "breakpoint" |
| 319 | * instruction. To avoid the SMP problems that can occur when we |
| 320 | * temporarily put back the original opcode to single-step, we |
| 321 | * single-stepped a copy of the instruction. The address of this |
| 322 | * copy is p->ainsn.insn. |
| 323 | */ |
Prasanna S Panchamukhi | bb144a8 | 2005-09-06 15:19:29 -0700 | [diff] [blame] | 324 | static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | { |
| 326 | int ret; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 327 | unsigned int insn = *p->ainsn.insn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
| 329 | regs->nip = (unsigned long)p->addr; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 330 | ret = emulate_step(regs, insn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | if (ret == 0) |
| 332 | regs->nip = (unsigned long)p->addr + 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static inline int post_kprobe_handler(struct pt_regs *regs) |
| 336 | { |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 337 | struct kprobe *cur = kprobe_running(); |
| 338 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 339 | |
| 340 | if (!cur) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | return 0; |
| 342 | |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 343 | if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) { |
| 344 | kcb->kprobe_status = KPROBE_HIT_SSDONE; |
| 345 | cur->post_handler(cur, regs, 0); |
Prasanna S Panchamukhi | 42cc206 | 2005-06-23 00:09:38 -0700 | [diff] [blame] | 346 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 348 | resume_execution(cur, regs); |
| 349 | regs->msr |= kcb->kprobe_saved_msr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
Prasanna S Panchamukhi | 42cc206 | 2005-06-23 00:09:38 -0700 | [diff] [blame] | 351 | /*Restore back the original saved kprobes variables and continue. */ |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 352 | if (kcb->kprobe_status == KPROBE_REENTER) { |
| 353 | restore_previous_kprobe(kcb); |
Prasanna S Panchamukhi | 42cc206 | 2005-06-23 00:09:38 -0700 | [diff] [blame] | 354 | goto out; |
| 355 | } |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 356 | reset_current_kprobe(); |
Prasanna S Panchamukhi | 42cc206 | 2005-06-23 00:09:38 -0700 | [diff] [blame] | 357 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | preempt_enable_no_resched(); |
| 359 | |
| 360 | /* |
| 361 | * if somebody else is singlestepping across a probe point, msr |
| 362 | * will have SE set, in which case, continue the remaining processing |
| 363 | * of do_debug, as if this is not a probe hit. |
| 364 | */ |
| 365 | if (regs->msr & MSR_SE) |
| 366 | return 0; |
| 367 | |
| 368 | return 1; |
| 369 | } |
| 370 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr) |
| 372 | { |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 373 | struct kprobe *cur = kprobe_running(); |
| 374 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 375 | |
| 376 | if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | return 1; |
| 378 | |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 379 | if (kcb->kprobe_status & KPROBE_HIT_SS) { |
| 380 | resume_execution(cur, regs); |
Ananth N Mavinakayanahalli | f829fd2 | 2005-06-08 15:50:00 -0700 | [diff] [blame] | 381 | regs->msr &= ~MSR_SE; |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 382 | regs->msr |= kcb->kprobe_saved_msr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 384 | reset_current_kprobe(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | preempt_enable_no_resched(); |
| 386 | } |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * Wrapper routine to for handling exceptions. |
| 392 | */ |
Prasanna S Panchamukhi | bb144a8 | 2005-09-06 15:19:29 -0700 | [diff] [blame] | 393 | int __kprobes kprobe_exceptions_notify(struct notifier_block *self, |
| 394 | unsigned long val, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | { |
| 396 | struct die_args *args = (struct die_args *)data; |
| 397 | int ret = NOTIFY_DONE; |
| 398 | |
bibo,mao | 2326c77 | 2006-03-26 01:38:21 -0800 | [diff] [blame^] | 399 | if (args->regs && user_mode(args->regs)) |
| 400 | return ret; |
| 401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | switch (val) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | case DIE_BPT: |
| 404 | if (kprobe_handler(args->regs)) |
| 405 | ret = NOTIFY_STOP; |
| 406 | break; |
| 407 | case DIE_SSTEP: |
| 408 | if (post_kprobe_handler(args->regs)) |
| 409 | ret = NOTIFY_STOP; |
| 410 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | case DIE_PAGE_FAULT: |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 412 | /* kprobe_running() needs smp_processor_id() */ |
| 413 | preempt_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | if (kprobe_running() && |
| 415 | kprobe_fault_handler(args->regs, args->trapnr)) |
| 416 | ret = NOTIFY_STOP; |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 417 | preempt_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | break; |
| 419 | default: |
| 420 | break; |
| 421 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | return ret; |
| 423 | } |
| 424 | |
Prasanna S Panchamukhi | bb144a8 | 2005-09-06 15:19:29 -0700 | [diff] [blame] | 425 | int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | { |
| 427 | struct jprobe *jp = container_of(p, struct jprobe, kp); |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 428 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 430 | memcpy(&kcb->jprobe_saved_regs, regs, sizeof(struct pt_regs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
| 432 | /* setup return addr to the jprobe handler routine */ |
| 433 | regs->nip = (unsigned long)(((func_descr_t *)jp->entry)->entry); |
| 434 | regs->gpr[2] = (unsigned long)(((func_descr_t *)jp->entry)->toc); |
| 435 | |
| 436 | return 1; |
| 437 | } |
| 438 | |
Prasanna S Panchamukhi | bb144a8 | 2005-09-06 15:19:29 -0700 | [diff] [blame] | 439 | void __kprobes jprobe_return(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
| 441 | asm volatile("trap" ::: "memory"); |
| 442 | } |
| 443 | |
Prasanna S Panchamukhi | bb144a8 | 2005-09-06 15:19:29 -0700 | [diff] [blame] | 444 | void __kprobes jprobe_return_end(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | { |
| 446 | }; |
| 447 | |
Prasanna S Panchamukhi | bb144a8 | 2005-09-06 15:19:29 -0700 | [diff] [blame] | 448 | int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | { |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 450 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | /* |
| 453 | * FIXME - we should ideally be validating that we got here 'cos |
| 454 | * of the "trap" in jprobe_return() above, before restoring the |
| 455 | * saved regs... |
| 456 | */ |
Ananth N Mavinakayanahalli | 0dc036c | 2005-11-07 01:00:10 -0800 | [diff] [blame] | 457 | memcpy(regs, &kcb->jprobe_saved_regs, sizeof(struct pt_regs)); |
Ananth N Mavinakayanahalli | d217d54 | 2005-11-07 01:00:14 -0800 | [diff] [blame] | 458 | preempt_enable_no_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | return 1; |
| 460 | } |
Rusty Lynch | 97f7943 | 2005-06-27 15:17:15 -0700 | [diff] [blame] | 461 | |
| 462 | static struct kprobe trampoline_p = { |
| 463 | .addr = (kprobe_opcode_t *) &kretprobe_trampoline, |
| 464 | .pre_handler = trampoline_probe_handler |
| 465 | }; |
| 466 | |
Rusty Lynch | 6772926 | 2005-07-05 18:54:50 -0700 | [diff] [blame] | 467 | int __init arch_init_kprobes(void) |
Rusty Lynch | 97f7943 | 2005-06-27 15:17:15 -0700 | [diff] [blame] | 468 | { |
| 469 | return register_kprobe(&trampoline_p); |
| 470 | } |