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