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