| Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Kernel Probes (KProbes) | 
|  | 3 | * | 
|  | 4 | * Copyright (C) 2005-2006 Atmel Corporation | 
|  | 5 | * | 
|  | 6 | * Based on arch/ppc64/kernel/kprobes.c | 
|  | 7 | *  Copyright (C) IBM Corporation, 2002, 2004 | 
|  | 8 | * | 
|  | 9 | * This program is free software; you can redistribute it and/or modify | 
|  | 10 | * it under the terms of the GNU General Public License version 2 as | 
|  | 11 | * published by the Free Software Foundation. | 
|  | 12 | */ | 
|  | 13 |  | 
|  | 14 | #include <linux/kprobes.h> | 
|  | 15 | #include <linux/ptrace.h> | 
|  | 16 |  | 
|  | 17 | #include <asm/cacheflush.h> | 
| Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 18 | #include <linux/kdebug.h> | 
| Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 19 | #include <asm/ocd.h> | 
|  | 20 |  | 
|  | 21 | DEFINE_PER_CPU(struct kprobe *, current_kprobe); | 
|  | 22 | static unsigned long kprobe_status; | 
|  | 23 | static struct pt_regs jprobe_saved_regs; | 
|  | 24 |  | 
|  | 25 | int __kprobes arch_prepare_kprobe(struct kprobe *p) | 
|  | 26 | { | 
|  | 27 | int ret = 0; | 
|  | 28 |  | 
|  | 29 | if ((unsigned long)p->addr & 0x01) { | 
|  | 30 | printk("Attempt to register kprobe at an unaligned address\n"); | 
|  | 31 | ret = -EINVAL; | 
|  | 32 | } | 
|  | 33 |  | 
|  | 34 | /* XXX: Might be a good idea to check if p->addr is a valid | 
|  | 35 | * kernel address as well... */ | 
|  | 36 |  | 
|  | 37 | if (!ret) { | 
|  | 38 | pr_debug("copy kprobe at %p\n", p->addr); | 
|  | 39 | memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t)); | 
|  | 40 | p->opcode = *p->addr; | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | return ret; | 
|  | 44 | } | 
|  | 45 |  | 
|  | 46 | void __kprobes arch_arm_kprobe(struct kprobe *p) | 
|  | 47 | { | 
|  | 48 | pr_debug("arming kprobe at %p\n", p->addr); | 
|  | 49 | *p->addr = BREAKPOINT_INSTRUCTION; | 
|  | 50 | flush_icache_range((unsigned long)p->addr, | 
|  | 51 | (unsigned long)p->addr + sizeof(kprobe_opcode_t)); | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | void __kprobes arch_disarm_kprobe(struct kprobe *p) | 
|  | 55 | { | 
|  | 56 | pr_debug("disarming kprobe at %p\n", p->addr); | 
|  | 57 | *p->addr = p->opcode; | 
|  | 58 | flush_icache_range((unsigned long)p->addr, | 
|  | 59 | (unsigned long)p->addr + sizeof(kprobe_opcode_t)); | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs) | 
|  | 63 | { | 
|  | 64 | unsigned long dc; | 
|  | 65 |  | 
|  | 66 | pr_debug("preparing to singlestep over %p (PC=%08lx)\n", | 
|  | 67 | p->addr, regs->pc); | 
|  | 68 |  | 
|  | 69 | BUG_ON(!(sysreg_read(SR) & SYSREG_BIT(SR_D))); | 
|  | 70 |  | 
|  | 71 | dc = __mfdr(DBGREG_DC); | 
|  | 72 | dc |= DC_SS; | 
|  | 73 | __mtdr(DBGREG_DC, dc); | 
|  | 74 |  | 
|  | 75 | /* | 
|  | 76 | * We must run the instruction from its original location | 
|  | 77 | * since it may actually reference PC. | 
|  | 78 | * | 
|  | 79 | * TODO: Do the instruction replacement directly in icache. | 
|  | 80 | */ | 
|  | 81 | *p->addr = p->opcode; | 
|  | 82 | flush_icache_range((unsigned long)p->addr, | 
|  | 83 | (unsigned long)p->addr + sizeof(kprobe_opcode_t)); | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | static void __kprobes resume_execution(struct kprobe *p, struct pt_regs *regs) | 
|  | 87 | { | 
|  | 88 | unsigned long dc; | 
|  | 89 |  | 
|  | 90 | pr_debug("resuming execution at PC=%08lx\n", regs->pc); | 
|  | 91 |  | 
|  | 92 | dc = __mfdr(DBGREG_DC); | 
|  | 93 | dc &= ~DC_SS; | 
|  | 94 | __mtdr(DBGREG_DC, dc); | 
|  | 95 |  | 
|  | 96 | *p->addr = BREAKPOINT_INSTRUCTION; | 
|  | 97 | flush_icache_range((unsigned long)p->addr, | 
|  | 98 | (unsigned long)p->addr + sizeof(kprobe_opcode_t)); | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | static void __kprobes set_current_kprobe(struct kprobe *p) | 
|  | 102 | { | 
|  | 103 | __get_cpu_var(current_kprobe) = p; | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | static int __kprobes kprobe_handler(struct pt_regs *regs) | 
|  | 107 | { | 
|  | 108 | struct kprobe *p; | 
|  | 109 | void *addr = (void *)regs->pc; | 
|  | 110 | int ret = 0; | 
|  | 111 |  | 
| Haavard Skinnemoen | 6ea850b | 2006-10-24 10:12:40 +0200 | [diff] [blame] | 112 | pr_debug("kprobe_handler: kprobe_running=%p\n", | 
| Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 113 | kprobe_running()); | 
|  | 114 |  | 
|  | 115 | /* | 
|  | 116 | * We don't want to be preempted for the entire | 
|  | 117 | * duration of kprobe processing | 
|  | 118 | */ | 
|  | 119 | preempt_disable(); | 
|  | 120 |  | 
|  | 121 | /* Check that we're not recursing */ | 
|  | 122 | if (kprobe_running()) { | 
|  | 123 | p = get_kprobe(addr); | 
|  | 124 | if (p) { | 
|  | 125 | if (kprobe_status == KPROBE_HIT_SS) { | 
|  | 126 | printk("FIXME: kprobe hit while single-stepping!\n"); | 
|  | 127 | goto no_kprobe; | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | printk("FIXME: kprobe hit while handling another kprobe\n"); | 
|  | 131 | goto no_kprobe; | 
|  | 132 | } else { | 
|  | 133 | p = kprobe_running(); | 
|  | 134 | if (p->break_handler && p->break_handler(p, regs)) | 
|  | 135 | goto ss_probe; | 
|  | 136 | } | 
|  | 137 | /* If it's not ours, can't be delete race, (we hold lock). */ | 
|  | 138 | goto no_kprobe; | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | p = get_kprobe(addr); | 
|  | 142 | if (!p) | 
|  | 143 | goto no_kprobe; | 
|  | 144 |  | 
|  | 145 | kprobe_status = KPROBE_HIT_ACTIVE; | 
|  | 146 | set_current_kprobe(p); | 
|  | 147 | if (p->pre_handler && p->pre_handler(p, regs)) | 
|  | 148 | /* handler has already set things up, so skip ss setup */ | 
|  | 149 | return 1; | 
|  | 150 |  | 
|  | 151 | ss_probe: | 
|  | 152 | prepare_singlestep(p, regs); | 
|  | 153 | kprobe_status = KPROBE_HIT_SS; | 
|  | 154 | return 1; | 
|  | 155 |  | 
|  | 156 | no_kprobe: | 
| Paul Mundt | 4d3eeea | 2006-12-06 20:33:54 -0800 | [diff] [blame] | 157 | preempt_enable_no_resched(); | 
| Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 158 | return ret; | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | static int __kprobes post_kprobe_handler(struct pt_regs *regs) | 
|  | 162 | { | 
|  | 163 | struct kprobe *cur = kprobe_running(); | 
|  | 164 |  | 
|  | 165 | pr_debug("post_kprobe_handler, cur=%p\n", cur); | 
|  | 166 |  | 
|  | 167 | if (!cur) | 
|  | 168 | return 0; | 
|  | 169 |  | 
|  | 170 | if (cur->post_handler) { | 
|  | 171 | kprobe_status = KPROBE_HIT_SSDONE; | 
|  | 172 | cur->post_handler(cur, regs, 0); | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | resume_execution(cur, regs); | 
|  | 176 | reset_current_kprobe(); | 
|  | 177 | preempt_enable_no_resched(); | 
|  | 178 |  | 
|  | 179 | return 1; | 
|  | 180 | } | 
|  | 181 |  | 
| Christoph Hellwig | 9caebec | 2007-05-12 17:56:11 +0200 | [diff] [blame] | 182 | int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr) | 
| Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 183 | { | 
|  | 184 | struct kprobe *cur = kprobe_running(); | 
|  | 185 |  | 
|  | 186 | pr_debug("kprobe_fault_handler: trapnr=%d\n", trapnr); | 
|  | 187 |  | 
|  | 188 | if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr)) | 
|  | 189 | return 1; | 
|  | 190 |  | 
|  | 191 | if (kprobe_status & KPROBE_HIT_SS) { | 
|  | 192 | resume_execution(cur, regs); | 
|  | 193 | preempt_enable_no_resched(); | 
|  | 194 | } | 
|  | 195 | return 0; | 
|  | 196 | } | 
|  | 197 |  | 
|  | 198 | /* | 
|  | 199 | * Wrapper routine to for handling exceptions. | 
|  | 200 | */ | 
|  | 201 | int __kprobes kprobe_exceptions_notify(struct notifier_block *self, | 
|  | 202 | unsigned long val, void *data) | 
|  | 203 | { | 
|  | 204 | struct die_args *args = (struct die_args *)data; | 
|  | 205 | int ret = NOTIFY_DONE; | 
|  | 206 |  | 
|  | 207 | pr_debug("kprobe_exceptions_notify: val=%lu, data=%p\n", | 
|  | 208 | val, data); | 
|  | 209 |  | 
|  | 210 | switch (val) { | 
|  | 211 | case DIE_BREAKPOINT: | 
|  | 212 | if (kprobe_handler(args->regs)) | 
|  | 213 | ret = NOTIFY_STOP; | 
|  | 214 | break; | 
|  | 215 | case DIE_SSTEP: | 
|  | 216 | if (post_kprobe_handler(args->regs)) | 
|  | 217 | ret = NOTIFY_STOP; | 
|  | 218 | break; | 
| Haavard Skinnemoen | 5f97f7f | 2006-09-25 23:32:13 -0700 | [diff] [blame] | 219 | default: | 
|  | 220 | break; | 
|  | 221 | } | 
|  | 222 |  | 
|  | 223 | return ret; | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs) | 
|  | 227 | { | 
|  | 228 | struct jprobe *jp = container_of(p, struct jprobe, kp); | 
|  | 229 |  | 
|  | 230 | memcpy(&jprobe_saved_regs, regs, sizeof(struct pt_regs)); | 
|  | 231 |  | 
|  | 232 | /* | 
|  | 233 | * TODO: We should probably save some of the stack here as | 
|  | 234 | * well, since gcc may pass arguments on the stack for certain | 
|  | 235 | * functions (lots of arguments, large aggregates, varargs) | 
|  | 236 | */ | 
|  | 237 |  | 
|  | 238 | /* setup return addr to the jprobe handler routine */ | 
|  | 239 | regs->pc = (unsigned long)jp->entry; | 
|  | 240 | return 1; | 
|  | 241 | } | 
|  | 242 |  | 
|  | 243 | void __kprobes jprobe_return(void) | 
|  | 244 | { | 
|  | 245 | asm volatile("breakpoint" ::: "memory"); | 
|  | 246 | } | 
|  | 247 |  | 
|  | 248 | int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) | 
|  | 249 | { | 
|  | 250 | /* | 
|  | 251 | * FIXME - we should ideally be validating that we got here 'cos | 
|  | 252 | * of the "trap" in jprobe_return() above, before restoring the | 
|  | 253 | * saved regs... | 
|  | 254 | */ | 
|  | 255 | memcpy(regs, &jprobe_saved_regs, sizeof(struct pt_regs)); | 
|  | 256 | return 1; | 
|  | 257 | } | 
|  | 258 |  | 
|  | 259 | int __init arch_init_kprobes(void) | 
|  | 260 | { | 
|  | 261 | printk("KPROBES: Enabling monitor mode (MM|DBE)...\n"); | 
|  | 262 | __mtdr(DBGREG_DC, DC_MM | DC_DBE); | 
|  | 263 |  | 
|  | 264 | /* TODO: Register kretprobe trampoline */ | 
|  | 265 | return 0; | 
|  | 266 | } |