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); |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | static struct kprobe *current_kprobe; |
| 42 | static unsigned long kprobe_status, kprobe_saved_msr; |
Prasanna S Panchamukhi | 42cc206 | 2005-06-23 00:09:38 -0700 | [diff] [blame] | 43 | static struct kprobe *kprobe_prev; |
| 44 | static unsigned long kprobe_status_prev, kprobe_saved_msr_prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | static struct pt_regs jprobe_saved_regs; |
| 46 | |
Prasanna S Panchamukhi | bb144a8 | 2005-09-06 15:19:29 -0700 | [diff] [blame^] | 47 | int __kprobes arch_prepare_kprobe(struct kprobe *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
Ananth N Mavinakayanahalli | 63224d1e8 | 2005-06-08 15:49:41 -0700 | [diff] [blame] | 49 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | kprobe_opcode_t insn = *p->addr; |
| 51 | |
Ananth N Mavinakayanahalli | 63224d1e8 | 2005-06-08 15:49:41 -0700 | [diff] [blame] | 52 | if ((unsigned long)p->addr & 0x03) { |
| 53 | printk("Attempt to register kprobe at an unaligned address\n"); |
| 54 | ret = -EINVAL; |
| 55 | } else if (IS_MTMSRD(insn) || IS_RFID(insn)) { |
| 56 | printk("Cannot register a kprobe on rfid or mtmsrd\n"); |
| 57 | ret = -EINVAL; |
| 58 | } |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 59 | |
| 60 | /* insn must be on a special executable page on ppc64 */ |
| 61 | if (!ret) { |
| 62 | up(&kprobe_mutex); |
| 63 | p->ainsn.insn = get_insn_slot(); |
| 64 | down(&kprobe_mutex); |
| 65 | if (!p->ainsn.insn) |
| 66 | ret = -ENOMEM; |
| 67 | } |
Ananth N Mavinakayanahalli | 63224d1e8 | 2005-06-08 15:49:41 -0700 | [diff] [blame] | 68 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Prasanna S Panchamukhi | bb144a8 | 2005-09-06 15:19:29 -0700 | [diff] [blame^] | 71 | void __kprobes arch_copy_kprobe(struct kprobe *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
| 73 | 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] | 74 | p->opcode = *p->addr; |
| 75 | } |
| 76 | |
Prasanna S Panchamukhi | bb144a8 | 2005-09-06 15:19:29 -0700 | [diff] [blame^] | 77 | void __kprobes arch_arm_kprobe(struct kprobe *p) |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 78 | { |
| 79 | *p->addr = BREAKPOINT_INSTRUCTION; |
| 80 | flush_icache_range((unsigned long) p->addr, |
| 81 | (unsigned long) p->addr + sizeof(kprobe_opcode_t)); |
| 82 | } |
| 83 | |
Prasanna S Panchamukhi | bb144a8 | 2005-09-06 15:19:29 -0700 | [diff] [blame^] | 84 | void __kprobes arch_disarm_kprobe(struct kprobe *p) |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame] | 85 | { |
| 86 | *p->addr = p->opcode; |
| 87 | flush_icache_range((unsigned long) p->addr, |
| 88 | (unsigned long) p->addr + sizeof(kprobe_opcode_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Prasanna S Panchamukhi | bb144a8 | 2005-09-06 15:19:29 -0700 | [diff] [blame^] | 91 | void __kprobes arch_remove_kprobe(struct kprobe *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | { |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 93 | up(&kprobe_mutex); |
| 94 | free_insn_slot(p->ainsn.insn); |
| 95 | down(&kprobe_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs) |
| 99 | { |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 100 | kprobe_opcode_t insn = *p->ainsn.insn; |
| 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | regs->msr |= MSR_SE; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 103 | |
| 104 | /* single step inline if it is a trap variant */ |
| 105 | if (IS_TW(insn) || IS_TD(insn) || IS_TWI(insn) || IS_TDI(insn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | regs->nip = (unsigned long)p->addr; |
| 107 | else |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 108 | regs->nip = (unsigned long)p->ainsn.insn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Prasanna S Panchamukhi | 42cc206 | 2005-06-23 00:09:38 -0700 | [diff] [blame] | 111 | static inline void save_previous_kprobe(void) |
| 112 | { |
| 113 | kprobe_prev = current_kprobe; |
| 114 | kprobe_status_prev = kprobe_status; |
| 115 | kprobe_saved_msr_prev = kprobe_saved_msr; |
| 116 | } |
| 117 | |
| 118 | static inline void restore_previous_kprobe(void) |
| 119 | { |
| 120 | current_kprobe = kprobe_prev; |
| 121 | kprobe_status = kprobe_status_prev; |
| 122 | kprobe_saved_msr = kprobe_saved_msr_prev; |
| 123 | } |
| 124 | |
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; |
| 148 | |
| 149 | /* Check we're not actually recursing */ |
| 150 | if (kprobe_running()) { |
| 151 | /* We *are* holding lock here, so this is safe. |
| 152 | Disarm the probe we just hit, and ignore it. */ |
| 153 | p = get_kprobe(addr); |
| 154 | if (p) { |
| 155 | if (kprobe_status == KPROBE_HIT_SS) { |
| 156 | regs->msr &= ~MSR_SE; |
| 157 | regs->msr |= kprobe_saved_msr; |
| 158 | unlock_kprobes(); |
| 159 | goto no_kprobe; |
| 160 | } |
Prasanna S Panchamukhi | 42cc206 | 2005-06-23 00:09:38 -0700 | [diff] [blame] | 161 | /* We have reentered the kprobe_handler(), since |
| 162 | * another probe was hit while within the handler. |
| 163 | * We here save the original kprobes variables and |
| 164 | * just single step on the instruction of the new probe |
| 165 | * without calling any user handlers. |
| 166 | */ |
| 167 | save_previous_kprobe(); |
| 168 | current_kprobe = p; |
| 169 | kprobe_saved_msr = regs->msr; |
| 170 | p->nmissed++; |
| 171 | prepare_singlestep(p, regs); |
| 172 | kprobe_status = KPROBE_REENTER; |
| 173 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } else { |
| 175 | p = current_kprobe; |
| 176 | if (p->break_handler && p->break_handler(p, regs)) { |
| 177 | goto ss_probe; |
| 178 | } |
| 179 | } |
| 180 | /* If it's not ours, can't be delete race, (we hold lock). */ |
| 181 | goto no_kprobe; |
| 182 | } |
| 183 | |
| 184 | lock_kprobes(); |
| 185 | p = get_kprobe(addr); |
| 186 | if (!p) { |
| 187 | unlock_kprobes(); |
| 188 | if (*addr != BREAKPOINT_INSTRUCTION) { |
| 189 | /* |
| 190 | * PowerPC has multiple variants of the "trap" |
| 191 | * instruction. If the current instruction is a |
| 192 | * trap variant, it could belong to someone else |
| 193 | */ |
| 194 | kprobe_opcode_t cur_insn = *addr; |
| 195 | if (IS_TW(cur_insn) || IS_TD(cur_insn) || |
| 196 | IS_TWI(cur_insn) || IS_TDI(cur_insn)) |
| 197 | goto no_kprobe; |
| 198 | /* |
| 199 | * The breakpoint instruction was removed right |
| 200 | * after we hit it. Another cpu has removed |
| 201 | * either a probepoint or a debugger breakpoint |
| 202 | * at this address. In either case, no further |
| 203 | * handling of this interrupt is appropriate. |
| 204 | */ |
| 205 | ret = 1; |
| 206 | } |
| 207 | /* Not one of ours: let kernel handle it */ |
| 208 | goto no_kprobe; |
| 209 | } |
| 210 | |
| 211 | kprobe_status = KPROBE_HIT_ACTIVE; |
| 212 | current_kprobe = p; |
| 213 | kprobe_saved_msr = regs->msr; |
| 214 | if (p->pre_handler && p->pre_handler(p, regs)) |
| 215 | /* handler has already set things up, so skip ss setup */ |
| 216 | return 1; |
| 217 | |
| 218 | ss_probe: |
| 219 | prepare_singlestep(p, regs); |
| 220 | kprobe_status = KPROBE_HIT_SS; |
| 221 | /* |
| 222 | * This preempt_disable() matches the preempt_enable_no_resched() |
| 223 | * in post_kprobe_handler(). |
| 224 | */ |
| 225 | preempt_disable(); |
| 226 | return 1; |
| 227 | |
| 228 | no_kprobe: |
| 229 | return ret; |
| 230 | } |
| 231 | |
| 232 | /* |
Rusty Lynch | 97f7943 | 2005-06-27 15:17:15 -0700 | [diff] [blame] | 233 | * Function return probe trampoline: |
| 234 | * - init_kprobes() establishes a probepoint here |
| 235 | * - When the probed function returns, this probe |
| 236 | * causes the handlers to fire |
| 237 | */ |
| 238 | void kretprobe_trampoline_holder(void) |
| 239 | { |
| 240 | asm volatile(".global kretprobe_trampoline\n" |
| 241 | "kretprobe_trampoline:\n" |
| 242 | "nop\n"); |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * Called when the probe at kretprobe trampoline is hit |
| 247 | */ |
Prasanna S Panchamukhi | bb144a8 | 2005-09-06 15:19:29 -0700 | [diff] [blame^] | 248 | int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs) |
Rusty Lynch | 97f7943 | 2005-06-27 15:17:15 -0700 | [diff] [blame] | 249 | { |
| 250 | struct kretprobe_instance *ri = NULL; |
| 251 | struct hlist_head *head; |
| 252 | struct hlist_node *node, *tmp; |
| 253 | unsigned long orig_ret_address = 0; |
| 254 | unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline; |
| 255 | |
| 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 | |
| 294 | unlock_kprobes(); |
| 295 | |
| 296 | /* |
| 297 | * By returning a non-zero value, we are telling |
| 298 | * kprobe_handler() that we have handled unlocking |
| 299 | * and re-enabling preemption. |
| 300 | */ |
| 301 | return 1; |
| 302 | } |
| 303 | |
| 304 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | * Called after single-stepping. p->addr is the address of the |
| 306 | * instruction whose first byte has been replaced by the "breakpoint" |
| 307 | * instruction. To avoid the SMP problems that can occur when we |
| 308 | * temporarily put back the original opcode to single-step, we |
| 309 | * single-stepped a copy of the instruction. The address of this |
| 310 | * copy is p->ainsn.insn. |
| 311 | */ |
Prasanna S Panchamukhi | bb144a8 | 2005-09-06 15:19:29 -0700 | [diff] [blame^] | 312 | static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | { |
| 314 | int ret; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 315 | unsigned int insn = *p->ainsn.insn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
| 317 | regs->nip = (unsigned long)p->addr; |
Ananth N Mavinakayanahalli | 9ec4b1f | 2005-06-27 15:17:01 -0700 | [diff] [blame] | 318 | ret = emulate_step(regs, insn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | if (ret == 0) |
| 320 | regs->nip = (unsigned long)p->addr + 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | static inline int post_kprobe_handler(struct pt_regs *regs) |
| 324 | { |
| 325 | if (!kprobe_running()) |
| 326 | return 0; |
| 327 | |
Prasanna S Panchamukhi | 42cc206 | 2005-06-23 00:09:38 -0700 | [diff] [blame] | 328 | if ((kprobe_status != KPROBE_REENTER) && current_kprobe->post_handler) { |
| 329 | kprobe_status = KPROBE_HIT_SSDONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | current_kprobe->post_handler(current_kprobe, regs, 0); |
Prasanna S Panchamukhi | 42cc206 | 2005-06-23 00:09:38 -0700 | [diff] [blame] | 331 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
| 333 | resume_execution(current_kprobe, regs); |
| 334 | regs->msr |= kprobe_saved_msr; |
| 335 | |
Prasanna S Panchamukhi | 42cc206 | 2005-06-23 00:09:38 -0700 | [diff] [blame] | 336 | /*Restore back the original saved kprobes variables and continue. */ |
| 337 | if (kprobe_status == KPROBE_REENTER) { |
| 338 | restore_previous_kprobe(); |
| 339 | goto out; |
| 340 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | unlock_kprobes(); |
Prasanna S Panchamukhi | 42cc206 | 2005-06-23 00:09:38 -0700 | [diff] [blame] | 342 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | preempt_enable_no_resched(); |
| 344 | |
| 345 | /* |
| 346 | * if somebody else is singlestepping across a probe point, msr |
| 347 | * will have SE set, in which case, continue the remaining processing |
| 348 | * of do_debug, as if this is not a probe hit. |
| 349 | */ |
| 350 | if (regs->msr & MSR_SE) |
| 351 | return 0; |
| 352 | |
| 353 | return 1; |
| 354 | } |
| 355 | |
| 356 | /* Interrupts disabled, kprobe_lock held. */ |
| 357 | static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr) |
| 358 | { |
| 359 | if (current_kprobe->fault_handler |
| 360 | && current_kprobe->fault_handler(current_kprobe, regs, trapnr)) |
| 361 | return 1; |
| 362 | |
| 363 | if (kprobe_status & KPROBE_HIT_SS) { |
| 364 | resume_execution(current_kprobe, regs); |
Ananth N Mavinakayanahalli | f829fd2 | 2005-06-08 15:50:00 -0700 | [diff] [blame] | 365 | regs->msr &= ~MSR_SE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | regs->msr |= kprobe_saved_msr; |
| 367 | |
| 368 | unlock_kprobes(); |
| 369 | preempt_enable_no_resched(); |
| 370 | } |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | /* |
| 375 | * Wrapper routine to for handling exceptions. |
| 376 | */ |
Prasanna S Panchamukhi | bb144a8 | 2005-09-06 15:19:29 -0700 | [diff] [blame^] | 377 | int __kprobes kprobe_exceptions_notify(struct notifier_block *self, |
| 378 | unsigned long val, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | { |
| 380 | struct die_args *args = (struct die_args *)data; |
| 381 | int ret = NOTIFY_DONE; |
| 382 | |
| 383 | /* |
| 384 | * Interrupts are not disabled here. We need to disable |
| 385 | * preemption, because kprobe_running() uses smp_processor_id(). |
| 386 | */ |
| 387 | preempt_disable(); |
| 388 | switch (val) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | case DIE_BPT: |
| 390 | if (kprobe_handler(args->regs)) |
| 391 | ret = NOTIFY_STOP; |
| 392 | break; |
| 393 | case DIE_SSTEP: |
| 394 | if (post_kprobe_handler(args->regs)) |
| 395 | ret = NOTIFY_STOP; |
| 396 | break; |
| 397 | case DIE_GPF: |
| 398 | case DIE_PAGE_FAULT: |
| 399 | if (kprobe_running() && |
| 400 | kprobe_fault_handler(args->regs, args->trapnr)) |
| 401 | ret = NOTIFY_STOP; |
| 402 | break; |
| 403 | default: |
| 404 | break; |
| 405 | } |
| 406 | preempt_enable(); |
| 407 | return ret; |
| 408 | } |
| 409 | |
Prasanna S Panchamukhi | bb144a8 | 2005-09-06 15:19:29 -0700 | [diff] [blame^] | 410 | int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | { |
| 412 | struct jprobe *jp = container_of(p, struct jprobe, kp); |
| 413 | |
| 414 | memcpy(&jprobe_saved_regs, regs, sizeof(struct pt_regs)); |
| 415 | |
| 416 | /* setup return addr to the jprobe handler routine */ |
| 417 | regs->nip = (unsigned long)(((func_descr_t *)jp->entry)->entry); |
| 418 | regs->gpr[2] = (unsigned long)(((func_descr_t *)jp->entry)->toc); |
| 419 | |
| 420 | return 1; |
| 421 | } |
| 422 | |
Prasanna S Panchamukhi | bb144a8 | 2005-09-06 15:19:29 -0700 | [diff] [blame^] | 423 | void __kprobes jprobe_return(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | { |
| 425 | asm volatile("trap" ::: "memory"); |
| 426 | } |
| 427 | |
Prasanna S Panchamukhi | bb144a8 | 2005-09-06 15:19:29 -0700 | [diff] [blame^] | 428 | void __kprobes jprobe_return_end(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | { |
| 430 | }; |
| 431 | |
Prasanna S Panchamukhi | bb144a8 | 2005-09-06 15:19:29 -0700 | [diff] [blame^] | 432 | int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | { |
| 434 | /* |
| 435 | * FIXME - we should ideally be validating that we got here 'cos |
| 436 | * of the "trap" in jprobe_return() above, before restoring the |
| 437 | * saved regs... |
| 438 | */ |
| 439 | memcpy(regs, &jprobe_saved_regs, sizeof(struct pt_regs)); |
| 440 | return 1; |
| 441 | } |
Rusty Lynch | 97f7943 | 2005-06-27 15:17:15 -0700 | [diff] [blame] | 442 | |
| 443 | static struct kprobe trampoline_p = { |
| 444 | .addr = (kprobe_opcode_t *) &kretprobe_trampoline, |
| 445 | .pre_handler = trampoline_probe_handler |
| 446 | }; |
| 447 | |
Rusty Lynch | 6772926 | 2005-07-05 18:54:50 -0700 | [diff] [blame] | 448 | int __init arch_init_kprobes(void) |
Rusty Lynch | 97f7943 | 2005-06-27 15:17:15 -0700 | [diff] [blame] | 449 | { |
| 450 | return register_kprobe(&trampoline_p); |
| 451 | } |