Vineet Gupta | 4d86dfb | 2013-01-22 17:03:59 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/kprobes.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/module.h> |
Vineet Gupta | 4d86dfb | 2013-01-22 17:03:59 +0530 | [diff] [blame] | 13 | #include <linux/kdebug.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/uaccess.h> |
| 16 | #include <asm/cacheflush.h> |
| 17 | #include <asm/current.h> |
| 18 | #include <asm/disasm.h> |
| 19 | |
| 20 | #define MIN_STACK_SIZE(addr) min((unsigned long)MAX_STACK_SIZE, \ |
| 21 | (unsigned long)current_thread_info() + THREAD_SIZE - (addr)) |
| 22 | |
| 23 | DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL; |
| 24 | DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk); |
| 25 | |
| 26 | int __kprobes arch_prepare_kprobe(struct kprobe *p) |
| 27 | { |
| 28 | /* Attempt to probe at unaligned address */ |
| 29 | if ((unsigned long)p->addr & 0x01) |
| 30 | return -EINVAL; |
| 31 | |
| 32 | /* Address should not be in exception handling code */ |
| 33 | |
| 34 | p->ainsn.is_short = is_short_instr((unsigned long)p->addr); |
| 35 | p->opcode = *p->addr; |
| 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | void __kprobes arch_arm_kprobe(struct kprobe *p) |
| 41 | { |
| 42 | *p->addr = UNIMP_S_INSTRUCTION; |
| 43 | |
| 44 | flush_icache_range((unsigned long)p->addr, |
| 45 | (unsigned long)p->addr + sizeof(kprobe_opcode_t)); |
| 46 | } |
| 47 | |
| 48 | void __kprobes arch_disarm_kprobe(struct kprobe *p) |
| 49 | { |
| 50 | *p->addr = p->opcode; |
| 51 | |
| 52 | flush_icache_range((unsigned long)p->addr, |
| 53 | (unsigned long)p->addr + sizeof(kprobe_opcode_t)); |
| 54 | } |
| 55 | |
| 56 | void __kprobes arch_remove_kprobe(struct kprobe *p) |
| 57 | { |
| 58 | arch_disarm_kprobe(p); |
| 59 | |
| 60 | /* Can we remove the kprobe in the middle of kprobe handling? */ |
| 61 | if (p->ainsn.t1_addr) { |
| 62 | *(p->ainsn.t1_addr) = p->ainsn.t1_opcode; |
| 63 | |
| 64 | flush_icache_range((unsigned long)p->ainsn.t1_addr, |
| 65 | (unsigned long)p->ainsn.t1_addr + |
| 66 | sizeof(kprobe_opcode_t)); |
| 67 | |
| 68 | p->ainsn.t1_addr = NULL; |
| 69 | } |
| 70 | |
| 71 | if (p->ainsn.t2_addr) { |
| 72 | *(p->ainsn.t2_addr) = p->ainsn.t2_opcode; |
| 73 | |
| 74 | flush_icache_range((unsigned long)p->ainsn.t2_addr, |
| 75 | (unsigned long)p->ainsn.t2_addr + |
| 76 | sizeof(kprobe_opcode_t)); |
| 77 | |
| 78 | p->ainsn.t2_addr = NULL; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb) |
| 83 | { |
| 84 | kcb->prev_kprobe.kp = kprobe_running(); |
| 85 | kcb->prev_kprobe.status = kcb->kprobe_status; |
| 86 | } |
| 87 | |
| 88 | static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb) |
| 89 | { |
Christoph Lameter | 6855e95 | 2013-08-28 19:48:15 +0000 | [diff] [blame] | 90 | __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp); |
Vineet Gupta | 4d86dfb | 2013-01-22 17:03:59 +0530 | [diff] [blame] | 91 | kcb->kprobe_status = kcb->prev_kprobe.status; |
| 92 | } |
| 93 | |
| 94 | static inline void __kprobes set_current_kprobe(struct kprobe *p) |
| 95 | { |
Christoph Lameter | 6855e95 | 2013-08-28 19:48:15 +0000 | [diff] [blame] | 96 | __this_cpu_write(current_kprobe, p); |
Vineet Gupta | 4d86dfb | 2013-01-22 17:03:59 +0530 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static void __kprobes resume_execution(struct kprobe *p, unsigned long addr, |
| 100 | struct pt_regs *regs) |
| 101 | { |
| 102 | /* Remove the trap instructions inserted for single step and |
| 103 | * restore the original instructions |
| 104 | */ |
| 105 | if (p->ainsn.t1_addr) { |
| 106 | *(p->ainsn.t1_addr) = p->ainsn.t1_opcode; |
| 107 | |
| 108 | flush_icache_range((unsigned long)p->ainsn.t1_addr, |
| 109 | (unsigned long)p->ainsn.t1_addr + |
| 110 | sizeof(kprobe_opcode_t)); |
| 111 | |
| 112 | p->ainsn.t1_addr = NULL; |
| 113 | } |
| 114 | |
| 115 | if (p->ainsn.t2_addr) { |
| 116 | *(p->ainsn.t2_addr) = p->ainsn.t2_opcode; |
| 117 | |
| 118 | flush_icache_range((unsigned long)p->ainsn.t2_addr, |
| 119 | (unsigned long)p->ainsn.t2_addr + |
| 120 | sizeof(kprobe_opcode_t)); |
| 121 | |
| 122 | p->ainsn.t2_addr = NULL; |
| 123 | } |
| 124 | |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | static void __kprobes setup_singlestep(struct kprobe *p, struct pt_regs *regs) |
| 129 | { |
| 130 | unsigned long next_pc; |
| 131 | unsigned long tgt_if_br = 0; |
| 132 | int is_branch; |
| 133 | unsigned long bta; |
| 134 | |
| 135 | /* Copy the opcode back to the kprobe location and execute the |
| 136 | * instruction. Because of this we will not be able to get into the |
| 137 | * same kprobe until this kprobe is done |
| 138 | */ |
| 139 | *(p->addr) = p->opcode; |
| 140 | |
| 141 | flush_icache_range((unsigned long)p->addr, |
| 142 | (unsigned long)p->addr + sizeof(kprobe_opcode_t)); |
| 143 | |
| 144 | /* Now we insert the trap at the next location after this instruction to |
| 145 | * single step. If it is a branch we insert the trap at possible branch |
| 146 | * targets |
| 147 | */ |
| 148 | |
| 149 | bta = regs->bta; |
| 150 | |
| 151 | if (regs->status32 & 0x40) { |
| 152 | /* We are in a delay slot with the branch taken */ |
| 153 | |
| 154 | next_pc = bta & ~0x01; |
| 155 | |
| 156 | if (!p->ainsn.is_short) { |
| 157 | if (bta & 0x01) |
| 158 | regs->blink += 2; |
| 159 | else { |
| 160 | /* Branch not taken */ |
| 161 | next_pc += 2; |
| 162 | |
| 163 | /* next pc is taken from bta after executing the |
| 164 | * delay slot instruction |
| 165 | */ |
| 166 | regs->bta += 2; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | is_branch = 0; |
| 171 | } else |
| 172 | is_branch = |
| 173 | disasm_next_pc((unsigned long)p->addr, regs, |
| 174 | (struct callee_regs *) current->thread.callee_reg, |
| 175 | &next_pc, &tgt_if_br); |
| 176 | |
| 177 | p->ainsn.t1_addr = (kprobe_opcode_t *) next_pc; |
| 178 | p->ainsn.t1_opcode = *(p->ainsn.t1_addr); |
| 179 | *(p->ainsn.t1_addr) = TRAP_S_2_INSTRUCTION; |
| 180 | |
| 181 | flush_icache_range((unsigned long)p->ainsn.t1_addr, |
| 182 | (unsigned long)p->ainsn.t1_addr + |
| 183 | sizeof(kprobe_opcode_t)); |
| 184 | |
| 185 | if (is_branch) { |
| 186 | p->ainsn.t2_addr = (kprobe_opcode_t *) tgt_if_br; |
| 187 | p->ainsn.t2_opcode = *(p->ainsn.t2_addr); |
| 188 | *(p->ainsn.t2_addr) = TRAP_S_2_INSTRUCTION; |
| 189 | |
| 190 | flush_icache_range((unsigned long)p->ainsn.t2_addr, |
| 191 | (unsigned long)p->ainsn.t2_addr + |
| 192 | sizeof(kprobe_opcode_t)); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | int __kprobes arc_kprobe_handler(unsigned long addr, struct pt_regs *regs) |
| 197 | { |
| 198 | struct kprobe *p; |
| 199 | struct kprobe_ctlblk *kcb; |
| 200 | |
| 201 | preempt_disable(); |
| 202 | |
| 203 | kcb = get_kprobe_ctlblk(); |
| 204 | p = get_kprobe((unsigned long *)addr); |
| 205 | |
| 206 | if (p) { |
| 207 | /* |
| 208 | * We have reentered the kprobe_handler, since another kprobe |
| 209 | * was hit while within the handler, we save the original |
| 210 | * kprobes and single step on the instruction of the new probe |
| 211 | * without calling any user handlers to avoid recursive |
| 212 | * kprobes. |
| 213 | */ |
| 214 | if (kprobe_running()) { |
| 215 | save_previous_kprobe(kcb); |
| 216 | set_current_kprobe(p); |
| 217 | kprobes_inc_nmissed_count(p); |
| 218 | setup_singlestep(p, regs); |
| 219 | kcb->kprobe_status = KPROBE_REENTER; |
| 220 | return 1; |
| 221 | } |
| 222 | |
| 223 | set_current_kprobe(p); |
| 224 | kcb->kprobe_status = KPROBE_HIT_ACTIVE; |
| 225 | |
| 226 | /* If we have no pre-handler or it returned 0, we continue with |
| 227 | * normal processing. If we have a pre-handler and it returned |
Masami Hiramatsu | e00f199 | 2018-06-20 01:06:04 +0900 | [diff] [blame^] | 228 | * non-zero - which means user handler setup registers to exit |
| 229 | * to another instruction, we must skip the single stepping. |
Vineet Gupta | 4d86dfb | 2013-01-22 17:03:59 +0530 | [diff] [blame] | 230 | */ |
| 231 | if (!p->pre_handler || !p->pre_handler(p, regs)) { |
| 232 | setup_singlestep(p, regs); |
| 233 | kcb->kprobe_status = KPROBE_HIT_SS; |
| 234 | } |
| 235 | |
| 236 | return 1; |
| 237 | } else if (kprobe_running()) { |
Christoph Lameter | 6855e95 | 2013-08-28 19:48:15 +0000 | [diff] [blame] | 238 | p = __this_cpu_read(current_kprobe); |
Vineet Gupta | 4d86dfb | 2013-01-22 17:03:59 +0530 | [diff] [blame] | 239 | if (p->break_handler && p->break_handler(p, regs)) { |
| 240 | setup_singlestep(p, regs); |
| 241 | kcb->kprobe_status = KPROBE_HIT_SS; |
| 242 | return 1; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /* no_kprobe: */ |
| 247 | preempt_enable_no_resched(); |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | static int __kprobes arc_post_kprobe_handler(unsigned long addr, |
| 252 | struct pt_regs *regs) |
| 253 | { |
| 254 | struct kprobe *cur = kprobe_running(); |
| 255 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 256 | |
| 257 | if (!cur) |
| 258 | return 0; |
| 259 | |
| 260 | resume_execution(cur, addr, regs); |
| 261 | |
| 262 | /* Rearm the kprobe */ |
| 263 | arch_arm_kprobe(cur); |
| 264 | |
| 265 | /* |
| 266 | * When we return from trap instruction we go to the next instruction |
| 267 | * We restored the actual instruction in resume_exectuiont and we to |
| 268 | * return to the same address and execute it |
| 269 | */ |
| 270 | regs->ret = addr; |
| 271 | |
| 272 | if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) { |
| 273 | kcb->kprobe_status = KPROBE_HIT_SSDONE; |
| 274 | cur->post_handler(cur, regs, 0); |
| 275 | } |
| 276 | |
| 277 | if (kcb->kprobe_status == KPROBE_REENTER) { |
| 278 | restore_previous_kprobe(kcb); |
| 279 | goto out; |
| 280 | } |
| 281 | |
| 282 | reset_current_kprobe(); |
| 283 | |
| 284 | out: |
| 285 | preempt_enable_no_resched(); |
| 286 | return 1; |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Fault can be for the instruction being single stepped or for the |
| 291 | * pre/post handlers in the module. |
| 292 | * This is applicable for applications like user probes, where we have the |
| 293 | * probe in user space and the handlers in the kernel |
| 294 | */ |
| 295 | |
| 296 | int __kprobes kprobe_fault_handler(struct pt_regs *regs, unsigned long trapnr) |
| 297 | { |
| 298 | struct kprobe *cur = kprobe_running(); |
| 299 | struct kprobe_ctlblk *kcb = get_kprobe_ctlblk(); |
| 300 | |
| 301 | switch (kcb->kprobe_status) { |
| 302 | case KPROBE_HIT_SS: |
| 303 | case KPROBE_REENTER: |
| 304 | /* |
| 305 | * We are here because the instruction being single stepped |
| 306 | * caused the fault. We reset the current kprobe and allow the |
| 307 | * exception handler as if it is regular exception. In our |
| 308 | * case it doesn't matter because the system will be halted |
| 309 | */ |
| 310 | resume_execution(cur, (unsigned long)cur->addr, regs); |
| 311 | |
| 312 | if (kcb->kprobe_status == KPROBE_REENTER) |
| 313 | restore_previous_kprobe(kcb); |
| 314 | else |
| 315 | reset_current_kprobe(); |
| 316 | |
| 317 | preempt_enable_no_resched(); |
| 318 | break; |
| 319 | |
| 320 | case KPROBE_HIT_ACTIVE: |
| 321 | case KPROBE_HIT_SSDONE: |
| 322 | /* |
| 323 | * We are here because the instructions in the pre/post handler |
| 324 | * caused the fault. |
| 325 | */ |
| 326 | |
| 327 | /* We increment the nmissed count for accounting, |
Anoop Thomas Mathew | 23d6d3d | 2013-09-20 09:25:41 +0530 | [diff] [blame] | 328 | * we can also use npre/npostfault count for accounting |
Vineet Gupta | 4d86dfb | 2013-01-22 17:03:59 +0530 | [diff] [blame] | 329 | * these specific fault cases. |
| 330 | */ |
| 331 | kprobes_inc_nmissed_count(cur); |
| 332 | |
| 333 | /* |
| 334 | * We come here because instructions in the pre/post |
| 335 | * handler caused the page_fault, this could happen |
| 336 | * if handler tries to access user space by |
| 337 | * copy_from_user(), get_user() etc. Let the |
| 338 | * user-specified handler try to fix it first. |
| 339 | */ |
| 340 | if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr)) |
| 341 | return 1; |
| 342 | |
| 343 | /* |
| 344 | * In case the user-specified fault handler returned zero, |
| 345 | * try to fix up. |
| 346 | */ |
| 347 | if (fixup_exception(regs)) |
| 348 | return 1; |
| 349 | |
| 350 | /* |
| 351 | * fixup_exception() could not handle it, |
| 352 | * Let do_page_fault() fix it. |
| 353 | */ |
| 354 | break; |
| 355 | |
| 356 | default: |
| 357 | break; |
| 358 | } |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | int __kprobes kprobe_exceptions_notify(struct notifier_block *self, |
| 363 | unsigned long val, void *data) |
| 364 | { |
| 365 | struct die_args *args = data; |
| 366 | unsigned long addr = args->err; |
| 367 | int ret = NOTIFY_DONE; |
| 368 | |
| 369 | switch (val) { |
| 370 | case DIE_IERR: |
| 371 | if (arc_kprobe_handler(addr, args->regs)) |
| 372 | return NOTIFY_STOP; |
| 373 | break; |
| 374 | |
| 375 | case DIE_TRAP: |
| 376 | if (arc_post_kprobe_handler(addr, args->regs)) |
| 377 | return NOTIFY_STOP; |
| 378 | break; |
| 379 | |
| 380 | default: |
| 381 | break; |
| 382 | } |
| 383 | |
| 384 | return ret; |
| 385 | } |
| 386 | |
Vineet Gupta | 4d86dfb | 2013-01-22 17:03:59 +0530 | [diff] [blame] | 387 | static void __used kretprobe_trampoline_holder(void) |
| 388 | { |
| 389 | __asm__ __volatile__(".global kretprobe_trampoline\n" |
| 390 | "kretprobe_trampoline:\n" "nop\n"); |
| 391 | } |
| 392 | |
| 393 | void __kprobes arch_prepare_kretprobe(struct kretprobe_instance *ri, |
| 394 | struct pt_regs *regs) |
| 395 | { |
| 396 | |
| 397 | ri->ret_addr = (kprobe_opcode_t *) regs->blink; |
| 398 | |
| 399 | /* Replace the return addr with trampoline addr */ |
| 400 | regs->blink = (unsigned long)&kretprobe_trampoline; |
| 401 | } |
| 402 | |
| 403 | static int __kprobes trampoline_probe_handler(struct kprobe *p, |
| 404 | struct pt_regs *regs) |
| 405 | { |
| 406 | struct kretprobe_instance *ri = NULL; |
| 407 | struct hlist_head *head, empty_rp; |
Vineet Gupta | 7f85e5e | 2013-02-08 12:10:17 +0530 | [diff] [blame] | 408 | struct hlist_node *tmp; |
Vineet Gupta | 4d86dfb | 2013-01-22 17:03:59 +0530 | [diff] [blame] | 409 | unsigned long flags, orig_ret_address = 0; |
| 410 | unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline; |
| 411 | |
| 412 | INIT_HLIST_HEAD(&empty_rp); |
| 413 | kretprobe_hash_lock(current, &head, &flags); |
| 414 | |
| 415 | /* |
| 416 | * It is possible to have multiple instances associated with a given |
| 417 | * task either because an multiple functions in the call path |
| 418 | * have a return probe installed on them, and/or more than one return |
| 419 | * return probe was registered for a target function. |
| 420 | * |
| 421 | * We can handle this because: |
| 422 | * - instances are always inserted at the head of the list |
| 423 | * - when multiple return probes are registered for the same |
| 424 | * function, the first instance's ret_addr will point to the |
| 425 | * real return address, and all the rest will point to |
| 426 | * kretprobe_trampoline |
| 427 | */ |
Vineet Gupta | 7f85e5e | 2013-02-08 12:10:17 +0530 | [diff] [blame] | 428 | hlist_for_each_entry_safe(ri, tmp, head, hlist) { |
Vineet Gupta | 4d86dfb | 2013-01-22 17:03:59 +0530 | [diff] [blame] | 429 | if (ri->task != current) |
| 430 | /* another task is sharing our hash bucket */ |
| 431 | continue; |
| 432 | |
| 433 | if (ri->rp && ri->rp->handler) |
| 434 | ri->rp->handler(ri, regs); |
| 435 | |
| 436 | orig_ret_address = (unsigned long)ri->ret_addr; |
| 437 | recycle_rp_inst(ri, &empty_rp); |
| 438 | |
| 439 | if (orig_ret_address != trampoline_address) { |
| 440 | /* |
| 441 | * This is the real return address. Any other |
| 442 | * instances associated with this task are for |
| 443 | * other calls deeper on the call stack |
| 444 | */ |
| 445 | break; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | kretprobe_assert(ri, orig_ret_address, trampoline_address); |
| 450 | regs->ret = orig_ret_address; |
| 451 | |
| 452 | reset_current_kprobe(); |
| 453 | kretprobe_hash_unlock(current, &flags); |
| 454 | preempt_enable_no_resched(); |
| 455 | |
Vineet Gupta | 7f85e5e | 2013-02-08 12:10:17 +0530 | [diff] [blame] | 456 | hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) { |
Vineet Gupta | 4d86dfb | 2013-01-22 17:03:59 +0530 | [diff] [blame] | 457 | hlist_del(&ri->hlist); |
| 458 | kfree(ri); |
| 459 | } |
| 460 | |
| 461 | /* By returning a non zero value, we are telling the kprobe handler |
| 462 | * that we don't want the post_handler to run |
| 463 | */ |
| 464 | return 1; |
| 465 | } |
| 466 | |
| 467 | static struct kprobe trampoline_p = { |
| 468 | .addr = (kprobe_opcode_t *) &kretprobe_trampoline, |
| 469 | .pre_handler = trampoline_probe_handler |
| 470 | }; |
| 471 | |
| 472 | int __init arch_init_kprobes(void) |
| 473 | { |
| 474 | /* Registering the trampoline code for the kret probe */ |
| 475 | return register_kprobe(&trampoline_p); |
| 476 | } |
| 477 | |
| 478 | int __kprobes arch_trampoline_kprobe(struct kprobe *p) |
| 479 | { |
| 480 | if (p->addr == (kprobe_opcode_t *) &kretprobe_trampoline) |
| 481 | return 1; |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
Vineet Gupta | 38a9ff6 | 2013-06-12 15:13:40 +0530 | [diff] [blame] | 486 | void trap_is_kprobe(unsigned long address, struct pt_regs *regs) |
Vineet Gupta | 4d86dfb | 2013-01-22 17:03:59 +0530 | [diff] [blame] | 487 | { |
Vineet Gupta | 38a9ff6 | 2013-06-12 15:13:40 +0530 | [diff] [blame] | 488 | notify_die(DIE_TRAP, "kprobe_trap", regs, address, 0, SIGTRAP); |
Vineet Gupta | 4d86dfb | 2013-01-22 17:03:59 +0530 | [diff] [blame] | 489 | } |