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