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 | |
| 39 | /* kprobe_status settings */ |
| 40 | #define KPROBE_HIT_ACTIVE 0x00000001 |
| 41 | #define KPROBE_HIT_SS 0x00000002 |
| 42 | |
| 43 | static struct kprobe *current_kprobe; |
| 44 | static unsigned long kprobe_status, kprobe_saved_msr; |
| 45 | static struct pt_regs jprobe_saved_regs; |
| 46 | |
| 47 | int arch_prepare_kprobe(struct kprobe *p) |
| 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 | } |
| 59 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void arch_copy_kprobe(struct kprobe *p) |
| 63 | { |
| 64 | 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^] | 65 | p->opcode = *p->addr; |
| 66 | } |
| 67 | |
| 68 | void arch_arm_kprobe(struct kprobe *p) |
| 69 | { |
| 70 | *p->addr = BREAKPOINT_INSTRUCTION; |
| 71 | flush_icache_range((unsigned long) p->addr, |
| 72 | (unsigned long) p->addr + sizeof(kprobe_opcode_t)); |
| 73 | } |
| 74 | |
| 75 | void arch_disarm_kprobe(struct kprobe *p) |
| 76 | { |
| 77 | *p->addr = p->opcode; |
| 78 | flush_icache_range((unsigned long) p->addr, |
| 79 | (unsigned long) p->addr + sizeof(kprobe_opcode_t)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void arch_remove_kprobe(struct kprobe *p) |
| 83 | { |
| 84 | } |
| 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs) |
| 87 | { |
| 88 | regs->msr |= MSR_SE; |
| 89 | /*single step inline if it a breakpoint instruction*/ |
| 90 | if (p->opcode == BREAKPOINT_INSTRUCTION) |
| 91 | regs->nip = (unsigned long)p->addr; |
| 92 | else |
| 93 | regs->nip = (unsigned long)&p->ainsn.insn; |
| 94 | } |
| 95 | |
| 96 | static inline int kprobe_handler(struct pt_regs *regs) |
| 97 | { |
| 98 | struct kprobe *p; |
| 99 | int ret = 0; |
| 100 | unsigned int *addr = (unsigned int *)regs->nip; |
| 101 | |
| 102 | /* Check we're not actually recursing */ |
| 103 | if (kprobe_running()) { |
| 104 | /* We *are* holding lock here, so this is safe. |
| 105 | Disarm the probe we just hit, and ignore it. */ |
| 106 | p = get_kprobe(addr); |
| 107 | if (p) { |
| 108 | if (kprobe_status == KPROBE_HIT_SS) { |
| 109 | regs->msr &= ~MSR_SE; |
| 110 | regs->msr |= kprobe_saved_msr; |
| 111 | unlock_kprobes(); |
| 112 | goto no_kprobe; |
| 113 | } |
Rusty Lynch | 7e1048b | 2005-06-23 00:09:25 -0700 | [diff] [blame^] | 114 | arch_disarm_kprobe(p); |
| 115 | regs->nip = (unsigned long)p->addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | ret = 1; |
| 117 | } else { |
| 118 | p = current_kprobe; |
| 119 | if (p->break_handler && p->break_handler(p, regs)) { |
| 120 | goto ss_probe; |
| 121 | } |
| 122 | } |
| 123 | /* If it's not ours, can't be delete race, (we hold lock). */ |
| 124 | goto no_kprobe; |
| 125 | } |
| 126 | |
| 127 | lock_kprobes(); |
| 128 | p = get_kprobe(addr); |
| 129 | if (!p) { |
| 130 | unlock_kprobes(); |
| 131 | if (*addr != BREAKPOINT_INSTRUCTION) { |
| 132 | /* |
| 133 | * PowerPC has multiple variants of the "trap" |
| 134 | * instruction. If the current instruction is a |
| 135 | * trap variant, it could belong to someone else |
| 136 | */ |
| 137 | kprobe_opcode_t cur_insn = *addr; |
| 138 | if (IS_TW(cur_insn) || IS_TD(cur_insn) || |
| 139 | IS_TWI(cur_insn) || IS_TDI(cur_insn)) |
| 140 | goto no_kprobe; |
| 141 | /* |
| 142 | * The breakpoint instruction was removed right |
| 143 | * after we hit it. Another cpu has removed |
| 144 | * either a probepoint or a debugger breakpoint |
| 145 | * at this address. In either case, no further |
| 146 | * handling of this interrupt is appropriate. |
| 147 | */ |
| 148 | ret = 1; |
| 149 | } |
| 150 | /* Not one of ours: let kernel handle it */ |
| 151 | goto no_kprobe; |
| 152 | } |
| 153 | |
| 154 | kprobe_status = KPROBE_HIT_ACTIVE; |
| 155 | current_kprobe = p; |
| 156 | kprobe_saved_msr = regs->msr; |
| 157 | if (p->pre_handler && p->pre_handler(p, regs)) |
| 158 | /* handler has already set things up, so skip ss setup */ |
| 159 | return 1; |
| 160 | |
| 161 | ss_probe: |
| 162 | prepare_singlestep(p, regs); |
| 163 | kprobe_status = KPROBE_HIT_SS; |
| 164 | /* |
| 165 | * This preempt_disable() matches the preempt_enable_no_resched() |
| 166 | * in post_kprobe_handler(). |
| 167 | */ |
| 168 | preempt_disable(); |
| 169 | return 1; |
| 170 | |
| 171 | no_kprobe: |
| 172 | return ret; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Called after single-stepping. p->addr is the address of the |
| 177 | * instruction whose first byte has been replaced by the "breakpoint" |
| 178 | * instruction. To avoid the SMP problems that can occur when we |
| 179 | * temporarily put back the original opcode to single-step, we |
| 180 | * single-stepped a copy of the instruction. The address of this |
| 181 | * copy is p->ainsn.insn. |
| 182 | */ |
| 183 | static void resume_execution(struct kprobe *p, struct pt_regs *regs) |
| 184 | { |
| 185 | int ret; |
| 186 | |
| 187 | regs->nip = (unsigned long)p->addr; |
| 188 | ret = emulate_step(regs, p->ainsn.insn[0]); |
| 189 | if (ret == 0) |
| 190 | regs->nip = (unsigned long)p->addr + 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static inline int post_kprobe_handler(struct pt_regs *regs) |
| 194 | { |
| 195 | if (!kprobe_running()) |
| 196 | return 0; |
| 197 | |
| 198 | if (current_kprobe->post_handler) |
| 199 | current_kprobe->post_handler(current_kprobe, regs, 0); |
| 200 | |
| 201 | resume_execution(current_kprobe, regs); |
| 202 | regs->msr |= kprobe_saved_msr; |
| 203 | |
| 204 | unlock_kprobes(); |
| 205 | preempt_enable_no_resched(); |
| 206 | |
| 207 | /* |
| 208 | * if somebody else is singlestepping across a probe point, msr |
| 209 | * will have SE set, in which case, continue the remaining processing |
| 210 | * of do_debug, as if this is not a probe hit. |
| 211 | */ |
| 212 | if (regs->msr & MSR_SE) |
| 213 | return 0; |
| 214 | |
| 215 | return 1; |
| 216 | } |
| 217 | |
| 218 | /* Interrupts disabled, kprobe_lock held. */ |
| 219 | static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr) |
| 220 | { |
| 221 | if (current_kprobe->fault_handler |
| 222 | && current_kprobe->fault_handler(current_kprobe, regs, trapnr)) |
| 223 | return 1; |
| 224 | |
| 225 | if (kprobe_status & KPROBE_HIT_SS) { |
| 226 | resume_execution(current_kprobe, regs); |
Ananth N Mavinakayanahalli | f829fd2 | 2005-06-08 15:50:00 -0700 | [diff] [blame] | 227 | regs->msr &= ~MSR_SE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | regs->msr |= kprobe_saved_msr; |
| 229 | |
| 230 | unlock_kprobes(); |
| 231 | preempt_enable_no_resched(); |
| 232 | } |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * Wrapper routine to for handling exceptions. |
| 238 | */ |
| 239 | int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val, |
| 240 | void *data) |
| 241 | { |
| 242 | struct die_args *args = (struct die_args *)data; |
| 243 | int ret = NOTIFY_DONE; |
| 244 | |
| 245 | /* |
| 246 | * Interrupts are not disabled here. We need to disable |
| 247 | * preemption, because kprobe_running() uses smp_processor_id(). |
| 248 | */ |
| 249 | preempt_disable(); |
| 250 | switch (val) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | case DIE_BPT: |
| 252 | if (kprobe_handler(args->regs)) |
| 253 | ret = NOTIFY_STOP; |
| 254 | break; |
| 255 | case DIE_SSTEP: |
| 256 | if (post_kprobe_handler(args->regs)) |
| 257 | ret = NOTIFY_STOP; |
| 258 | break; |
| 259 | case DIE_GPF: |
| 260 | case DIE_PAGE_FAULT: |
| 261 | if (kprobe_running() && |
| 262 | kprobe_fault_handler(args->regs, args->trapnr)) |
| 263 | ret = NOTIFY_STOP; |
| 264 | break; |
| 265 | default: |
| 266 | break; |
| 267 | } |
| 268 | preempt_enable(); |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs) |
| 273 | { |
| 274 | struct jprobe *jp = container_of(p, struct jprobe, kp); |
| 275 | |
| 276 | memcpy(&jprobe_saved_regs, regs, sizeof(struct pt_regs)); |
| 277 | |
| 278 | /* setup return addr to the jprobe handler routine */ |
| 279 | regs->nip = (unsigned long)(((func_descr_t *)jp->entry)->entry); |
| 280 | regs->gpr[2] = (unsigned long)(((func_descr_t *)jp->entry)->toc); |
| 281 | |
| 282 | return 1; |
| 283 | } |
| 284 | |
| 285 | void jprobe_return(void) |
| 286 | { |
| 287 | asm volatile("trap" ::: "memory"); |
| 288 | } |
| 289 | |
| 290 | void jprobe_return_end(void) |
| 291 | { |
| 292 | }; |
| 293 | |
| 294 | int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) |
| 295 | { |
| 296 | /* |
| 297 | * FIXME - we should ideally be validating that we got here 'cos |
| 298 | * of the "trap" in jprobe_return() above, before restoring the |
| 299 | * saved regs... |
| 300 | */ |
| 301 | memcpy(regs, &jprobe_saved_regs, sizeof(struct pt_regs)); |
| 302 | return 1; |
| 303 | } |