Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Kernel Probes (KProbes) |
| 3 | * arch/ia64/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 | * Copyright (C) Intel Corporation, 2005 |
| 21 | * |
| 22 | * 2005-Apr Rusty Lynch <rusty.lynch@intel.com> and Anil S Keshavamurthy |
| 23 | * <anil.s.keshavamurthy@intel.com> adapted from i386 |
| 24 | */ |
| 25 | |
| 26 | #include <linux/config.h> |
| 27 | #include <linux/kprobes.h> |
| 28 | #include <linux/ptrace.h> |
| 29 | #include <linux/spinlock.h> |
| 30 | #include <linux/string.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <linux/preempt.h> |
| 33 | #include <linux/moduleloader.h> |
| 34 | |
| 35 | #include <asm/pgtable.h> |
| 36 | #include <asm/kdebug.h> |
| 37 | |
Anil S Keshavamurthy | b2761dc | 2005-06-23 00:09:28 -0700 | [diff] [blame^] | 38 | extern void jprobe_inst_return(void); |
| 39 | |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 40 | /* kprobe_status settings */ |
| 41 | #define KPROBE_HIT_ACTIVE 0x00000001 |
| 42 | #define KPROBE_HIT_SS 0x00000002 |
| 43 | |
| 44 | static struct kprobe *current_kprobe; |
| 45 | static unsigned long kprobe_status; |
Anil S Keshavamurthy | b2761dc | 2005-06-23 00:09:28 -0700 | [diff] [blame^] | 46 | static struct pt_regs jprobe_saved_regs; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 47 | |
| 48 | enum instruction_type {A, I, M, F, B, L, X, u}; |
| 49 | static enum instruction_type bundle_encoding[32][3] = { |
| 50 | { M, I, I }, /* 00 */ |
| 51 | { M, I, I }, /* 01 */ |
| 52 | { M, I, I }, /* 02 */ |
| 53 | { M, I, I }, /* 03 */ |
| 54 | { M, L, X }, /* 04 */ |
| 55 | { M, L, X }, /* 05 */ |
| 56 | { u, u, u }, /* 06 */ |
| 57 | { u, u, u }, /* 07 */ |
| 58 | { M, M, I }, /* 08 */ |
| 59 | { M, M, I }, /* 09 */ |
| 60 | { M, M, I }, /* 0A */ |
| 61 | { M, M, I }, /* 0B */ |
| 62 | { M, F, I }, /* 0C */ |
| 63 | { M, F, I }, /* 0D */ |
| 64 | { M, M, F }, /* 0E */ |
| 65 | { M, M, F }, /* 0F */ |
| 66 | { M, I, B }, /* 10 */ |
| 67 | { M, I, B }, /* 11 */ |
| 68 | { M, B, B }, /* 12 */ |
| 69 | { M, B, B }, /* 13 */ |
| 70 | { u, u, u }, /* 14 */ |
| 71 | { u, u, u }, /* 15 */ |
| 72 | { B, B, B }, /* 16 */ |
| 73 | { B, B, B }, /* 17 */ |
| 74 | { M, M, B }, /* 18 */ |
| 75 | { M, M, B }, /* 19 */ |
| 76 | { u, u, u }, /* 1A */ |
| 77 | { u, u, u }, /* 1B */ |
| 78 | { M, F, B }, /* 1C */ |
| 79 | { M, F, B }, /* 1D */ |
| 80 | { u, u, u }, /* 1E */ |
| 81 | { u, u, u }, /* 1F */ |
| 82 | }; |
| 83 | |
| 84 | int arch_prepare_kprobe(struct kprobe *p) |
| 85 | { |
| 86 | unsigned long addr = (unsigned long) p->addr; |
| 87 | unsigned long bundle_addr = addr & ~0xFULL; |
| 88 | unsigned long slot = addr & 0xf; |
| 89 | bundle_t bundle; |
| 90 | unsigned long template; |
| 91 | |
| 92 | /* |
| 93 | * TODO: Verify that a probe is not being inserted |
| 94 | * in sensitive regions of code |
| 95 | * TODO: Verify that the memory holding the probe is rwx |
| 96 | * TODO: verify this is a kernel address |
| 97 | */ |
| 98 | memcpy(&bundle, (unsigned long *)bundle_addr, sizeof(bundle_t)); |
| 99 | template = bundle.quad0.template; |
| 100 | if (((bundle_encoding[template][1] == L) && slot > 1) || (slot > 2)) { |
| 101 | printk(KERN_WARNING "Attempting to insert unaligned kprobe at 0x%lx\n", addr); |
| 102 | return -EINVAL; |
| 103 | } |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | void arch_copy_kprobe(struct kprobe *p) |
| 108 | { |
| 109 | unsigned long addr = (unsigned long)p->addr; |
| 110 | unsigned long bundle_addr = addr & ~0xFULL; |
| 111 | |
| 112 | memcpy(&p->ainsn.insn.bundle, (unsigned long *)bundle_addr, |
| 113 | sizeof(bundle_t)); |
| 114 | memcpy(&p->opcode.bundle, &p->ainsn.insn.bundle, sizeof(bundle_t)); |
| 115 | } |
| 116 | |
| 117 | void arch_arm_kprobe(struct kprobe *p) |
| 118 | { |
| 119 | unsigned long addr = (unsigned long)p->addr; |
| 120 | unsigned long arm_addr = addr & ~0xFULL; |
| 121 | unsigned long slot = addr & 0xf; |
| 122 | unsigned long template; |
| 123 | bundle_t bundle; |
| 124 | |
| 125 | memcpy(&bundle, &p->ainsn.insn.bundle, sizeof(bundle_t)); |
| 126 | |
| 127 | template = bundle.quad0.template; |
| 128 | if (slot == 1 && bundle_encoding[template][1] == L) |
| 129 | slot = 2; |
| 130 | switch (slot) { |
| 131 | case 0: |
| 132 | bundle.quad0.slot0 = BREAK_INST; |
| 133 | break; |
| 134 | case 1: |
| 135 | bundle.quad0.slot1_p0 = BREAK_INST; |
| 136 | bundle.quad1.slot1_p1 = (BREAK_INST >> (64-46)); |
| 137 | break; |
| 138 | case 2: |
| 139 | bundle.quad1.slot2 = BREAK_INST; |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | /* Flush icache for the instruction at the emulated address */ |
| 144 | flush_icache_range((unsigned long)&p->ainsn.insn.bundle, |
| 145 | (unsigned long)&p->ainsn.insn.bundle + |
| 146 | sizeof(bundle_t)); |
| 147 | /* |
| 148 | * Patch the original instruction with the probe instruction |
| 149 | * and flush the instruction cache |
| 150 | */ |
| 151 | memcpy((char *) arm_addr, (char *) &bundle, sizeof(bundle_t)); |
| 152 | flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t)); |
| 153 | } |
| 154 | |
| 155 | void arch_disarm_kprobe(struct kprobe *p) |
| 156 | { |
| 157 | unsigned long addr = (unsigned long)p->addr; |
| 158 | unsigned long arm_addr = addr & ~0xFULL; |
| 159 | |
| 160 | /* p->opcode contains the original unaltered bundle */ |
| 161 | memcpy((char *) arm_addr, (char *) &p->opcode.bundle, sizeof(bundle_t)); |
| 162 | flush_icache_range(arm_addr, arm_addr + sizeof(bundle_t)); |
| 163 | } |
| 164 | |
| 165 | void arch_remove_kprobe(struct kprobe *p) |
| 166 | { |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * We are resuming execution after a single step fault, so the pt_regs |
| 171 | * structure reflects the register state after we executed the instruction |
| 172 | * located in the kprobe (p->ainsn.insn.bundle). We still need to adjust |
| 173 | * the ip to point back to the original stack address, and if we see that |
| 174 | * the slot has incremented back to zero, then we need to point to the next |
| 175 | * slot location. |
| 176 | */ |
| 177 | static void resume_execution(struct kprobe *p, struct pt_regs *regs) |
| 178 | { |
| 179 | unsigned long bundle = (unsigned long)p->addr & ~0xFULL; |
| 180 | |
| 181 | /* |
| 182 | * TODO: Handle cases where kprobe was inserted on a branch instruction |
| 183 | */ |
| 184 | |
| 185 | if (!ia64_psr(regs)->ri) |
| 186 | regs->cr_iip = bundle + 0x10; |
| 187 | else |
| 188 | regs->cr_iip = bundle; |
| 189 | |
| 190 | ia64_psr(regs)->ss = 0; |
| 191 | } |
| 192 | |
| 193 | static void prepare_ss(struct kprobe *p, struct pt_regs *regs) |
| 194 | { |
| 195 | unsigned long bundle_addr = (unsigned long) &p->ainsn.insn.bundle; |
| 196 | unsigned long slot = (unsigned long)p->addr & 0xf; |
| 197 | |
| 198 | /* Update instruction pointer (IIP) and slot number (IPSR.ri) */ |
| 199 | regs->cr_iip = bundle_addr & ~0xFULL; |
| 200 | |
| 201 | if (slot > 2) |
| 202 | slot = 0; |
| 203 | |
| 204 | ia64_psr(regs)->ri = slot; |
| 205 | |
| 206 | /* turn on single stepping */ |
| 207 | ia64_psr(regs)->ss = 1; |
| 208 | } |
| 209 | |
| 210 | static int pre_kprobes_handler(struct pt_regs *regs) |
| 211 | { |
| 212 | struct kprobe *p; |
| 213 | int ret = 0; |
| 214 | kprobe_opcode_t *addr = (kprobe_opcode_t *)instruction_pointer(regs); |
| 215 | |
| 216 | preempt_disable(); |
| 217 | |
| 218 | /* Handle recursion cases */ |
| 219 | if (kprobe_running()) { |
| 220 | p = get_kprobe(addr); |
| 221 | if (p) { |
| 222 | if (kprobe_status == KPROBE_HIT_SS) { |
| 223 | unlock_kprobes(); |
| 224 | goto no_kprobe; |
| 225 | } |
| 226 | arch_disarm_kprobe(p); |
| 227 | ret = 1; |
| 228 | } else { |
| 229 | /* |
| 230 | * jprobe instrumented function just completed |
| 231 | */ |
| 232 | p = current_kprobe; |
| 233 | if (p->break_handler && p->break_handler(p, regs)) { |
| 234 | goto ss_probe; |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | lock_kprobes(); |
| 240 | p = get_kprobe(addr); |
| 241 | if (!p) { |
| 242 | unlock_kprobes(); |
| 243 | goto no_kprobe; |
| 244 | } |
| 245 | |
| 246 | kprobe_status = KPROBE_HIT_ACTIVE; |
| 247 | current_kprobe = p; |
| 248 | |
| 249 | if (p->pre_handler && p->pre_handler(p, regs)) |
| 250 | /* |
| 251 | * Our pre-handler is specifically requesting that we just |
| 252 | * do a return. This is handling the case where the |
| 253 | * pre-handler is really our special jprobe pre-handler. |
| 254 | */ |
| 255 | return 1; |
| 256 | |
| 257 | ss_probe: |
| 258 | prepare_ss(p, regs); |
| 259 | kprobe_status = KPROBE_HIT_SS; |
| 260 | return 1; |
| 261 | |
| 262 | no_kprobe: |
| 263 | preempt_enable_no_resched(); |
| 264 | return ret; |
| 265 | } |
| 266 | |
| 267 | static int post_kprobes_handler(struct pt_regs *regs) |
| 268 | { |
| 269 | if (!kprobe_running()) |
| 270 | return 0; |
| 271 | |
| 272 | if (current_kprobe->post_handler) |
| 273 | current_kprobe->post_handler(current_kprobe, regs, 0); |
| 274 | |
| 275 | resume_execution(current_kprobe, regs); |
| 276 | |
| 277 | unlock_kprobes(); |
| 278 | preempt_enable_no_resched(); |
| 279 | return 1; |
| 280 | } |
| 281 | |
| 282 | static int kprobes_fault_handler(struct pt_regs *regs, int trapnr) |
| 283 | { |
| 284 | if (!kprobe_running()) |
| 285 | return 0; |
| 286 | |
| 287 | if (current_kprobe->fault_handler && |
| 288 | current_kprobe->fault_handler(current_kprobe, regs, trapnr)) |
| 289 | return 1; |
| 290 | |
| 291 | if (kprobe_status & KPROBE_HIT_SS) { |
| 292 | resume_execution(current_kprobe, regs); |
| 293 | unlock_kprobes(); |
| 294 | preempt_enable_no_resched(); |
| 295 | } |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val, |
| 301 | void *data) |
| 302 | { |
| 303 | struct die_args *args = (struct die_args *)data; |
| 304 | switch(val) { |
| 305 | case DIE_BREAK: |
| 306 | if (pre_kprobes_handler(args->regs)) |
| 307 | return NOTIFY_STOP; |
| 308 | break; |
| 309 | case DIE_SS: |
| 310 | if (post_kprobes_handler(args->regs)) |
| 311 | return NOTIFY_STOP; |
| 312 | break; |
| 313 | case DIE_PAGE_FAULT: |
| 314 | if (kprobes_fault_handler(args->regs, args->trapnr)) |
| 315 | return NOTIFY_STOP; |
| 316 | default: |
| 317 | break; |
| 318 | } |
| 319 | return NOTIFY_DONE; |
| 320 | } |
| 321 | |
| 322 | int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs) |
| 323 | { |
Anil S Keshavamurthy | b2761dc | 2005-06-23 00:09:28 -0700 | [diff] [blame^] | 324 | struct jprobe *jp = container_of(p, struct jprobe, kp); |
| 325 | unsigned long addr = ((struct fnptr *)(jp->entry))->ip; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 326 | |
Anil S Keshavamurthy | b2761dc | 2005-06-23 00:09:28 -0700 | [diff] [blame^] | 327 | /* save architectural state */ |
| 328 | jprobe_saved_regs = *regs; |
| 329 | |
| 330 | /* after rfi, execute the jprobe instrumented function */ |
| 331 | regs->cr_iip = addr & ~0xFULL; |
| 332 | ia64_psr(regs)->ri = addr & 0xf; |
| 333 | regs->r1 = ((struct fnptr *)(jp->entry))->gp; |
| 334 | |
| 335 | /* |
| 336 | * fix the return address to our jprobe_inst_return() function |
| 337 | * in the jprobes.S file |
| 338 | */ |
| 339 | regs->b0 = ((struct fnptr *)(jprobe_inst_return))->ip; |
| 340 | |
| 341 | return 1; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs) |
| 345 | { |
Anil S Keshavamurthy | b2761dc | 2005-06-23 00:09:28 -0700 | [diff] [blame^] | 346 | *regs = jprobe_saved_regs; |
| 347 | return 1; |
Anil S Keshavamurthy | fd7b231 | 2005-06-23 00:09:28 -0700 | [diff] [blame] | 348 | } |