1 /* 2 * linux/arch/arm/kernel/traps.c 3 * 4 * Copyright (C) 1995-2002 Russell King 5 * Fragments that appear the same as linux/arch/i386/kernel/traps.c (C) Linus Torvalds 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License version 2 as 9 * published by the Free Software Foundation. 10 * 11 * 'traps.c' handles hardware exceptions after we have saved some state in 12 * 'linux/arch/arm/lib/traps.S'. Mostly a debugging aid, but will probably 13 * kill the offending process. 14 */ 15 #include <linux/module.h> 16 #include <linux/signal.h> 17 #include <linux/spinlock.h> 18 #include <linux/personality.h> 19 #include <linux/kallsyms.h> 20 #include <linux/delay.h> 21 #include <linux/init.h> 22 #include <linux/uaccess.h> 23 24 #include <asm/atomic.h> 25 #include <asm/cacheflush.h> 26 #include <asm/system.h> 27 #include <asm/unistd.h> 28 #include <asm/traps.h> 29 30 #include "ptrace.h" 31 #include "signal.h" 32 33 static const char *handler[]= { "prefetch abort", "data abort", "address exception", "interrupt" }; 34 35 #ifdef CONFIG_DEBUG_USER 36 unsigned int user_debug; 37 38 static int __init user_debug_setup(char *str) 39 { 40 get_option(&str, &user_debug); 41 return 1; 42 } 43 __setup("user_debug=", user_debug_setup); 44 #endif 45 46 static void dump_mem(const char *str, unsigned long bottom, unsigned long top); 47 48 void dump_backtrace_entry(unsigned long where, unsigned long from, unsigned long frame) 49 { 50 #ifdef CONFIG_KALLSYMS 51 printk("[<%08lx>] ", where); 52 print_symbol("(%s) ", where); 53 printk("from [<%08lx>] ", from); 54 print_symbol("(%s)\n", from); 55 #else 56 printk("Function entered at [<%08lx>] from [<%08lx>]\n", where, from); 57 #endif 58 59 if (in_exception_text(where)) 60 dump_mem("Exception stack", frame + 4, frame + 4 + sizeof(struct pt_regs)); 61 } 62 63 /* 64 * Stack pointers should always be within the kernels view of 65 * physical memory. If it is not there, then we can't dump 66 * out any information relating to the stack. 67 */ 68 static int verify_stack(unsigned long sp) 69 { 70 if (sp < PAGE_OFFSET || 71 (sp > (unsigned long)high_memory && high_memory != NULL)) 72 return -EFAULT; 73 74 return 0; 75 } 76 77 /* 78 * Dump out the contents of some memory nicely... 79 */ 80 static void dump_mem(const char *str, unsigned long bottom, unsigned long top) 81 { 82 unsigned long p = bottom & ~31; 83 mm_segment_t fs; 84 int i; 85 86 /* 87 * We need to switch to kernel mode so that we can use __get_user 88 * to safely read from kernel space. Note that we now dump the 89 * code first, just in case the backtrace kills us. 90 */ 91 fs = get_fs(); 92 set_fs(KERNEL_DS); 93 94 printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top); 95 96 for (p = bottom & ~31; p < top;) { 97 printk("%04lx: ", p & 0xffff); 98 99 for (i = 0; i < 8; i++, p += 4) { 100 unsigned int val; 101 102 if (p < bottom || p >= top) 103 printk(" "); 104 else { 105 __get_user(val, (unsigned long *)p); 106 printk("%08x ", val); 107 } 108 } 109 printk ("\n"); 110 } 111 112 set_fs(fs); 113 } 114 115 static void dump_instr(struct pt_regs *regs) 116 { 117 unsigned long addr = instruction_pointer(regs); 118 const int thumb = thumb_mode(regs); 119 const int width = thumb ? 4 : 8; 120 mm_segment_t fs; 121 int i; 122 123 /* 124 * We need to switch to kernel mode so that we can use __get_user 125 * to safely read from kernel space. Note that we now dump the 126 * code first, just in case the backtrace kills us. 127 */ 128 fs = get_fs(); 129 set_fs(KERNEL_DS); 130 131 printk("Code: "); 132 for (i = -4; i < 1; i++) { 133 unsigned int val, bad; 134 135 if (thumb) 136 bad = __get_user(val, &((u16 *)addr)[i]); 137 else 138 bad = __get_user(val, &((u32 *)addr)[i]); 139 140 if (!bad) 141 printk(i == 0 ? "(%0*x) " : "%0*x ", width, val); 142 else { 143 printk("bad PC value."); 144 break; 145 } 146 } 147 printk("\n"); 148 149 set_fs(fs); 150 } 151 152 static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk) 153 { 154 unsigned int fp; 155 int ok = 1; 156 157 printk("Backtrace: "); 158 fp = regs->ARM_fp; 159 if (!fp) { 160 printk("no frame pointer"); 161 ok = 0; 162 } else if (verify_stack(fp)) { 163 printk("invalid frame pointer 0x%08x", fp); 164 ok = 0; 165 } else if (fp < (unsigned long)end_of_stack(tsk)) 166 printk("frame pointer underflow"); 167 printk("\n"); 168 169 if (ok) 170 c_backtrace(fp, processor_mode(regs)); 171 } 172 173 void dump_stack(void) 174 { 175 __backtrace(); 176 } 177 178 EXPORT_SYMBOL(dump_stack); 179 180 void show_stack(struct task_struct *tsk, unsigned long *sp) 181 { 182 unsigned long fp; 183 184 if (!tsk) 185 tsk = current; 186 187 if (tsk != current) 188 fp = thread_saved_fp(tsk); 189 else 190 asm("mov %0, fp" : "=r" (fp) : : "cc"); 191 192 c_backtrace(fp, 0x10); 193 barrier(); 194 } 195 196 #ifdef CONFIG_PREEMPT 197 #define S_PREEMPT " PREEMPT" 198 #else 199 #define S_PREEMPT "" 200 #endif 201 #ifdef CONFIG_SMP 202 #define S_SMP " SMP" 203 #else 204 #define S_SMP "" 205 #endif 206 207 static void __die(const char *str, int err, struct thread_info *thread, struct pt_regs *regs) 208 { 209 struct task_struct *tsk = thread->task; 210 static int die_counter; 211 212 printk("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n", 213 str, err, ++die_counter); 214 print_modules(); 215 __show_regs(regs); 216 printk("Process %s (pid: %d, stack limit = 0x%p)\n", 217 tsk->comm, task_pid_nr(tsk), thread + 1); 218 219 if (!user_mode(regs) || in_interrupt()) { 220 dump_mem("Stack: ", regs->ARM_sp, 221 THREAD_SIZE + (unsigned long)task_stack_page(tsk)); 222 dump_backtrace(regs, tsk); 223 dump_instr(regs); 224 } 225 } 226 227 DEFINE_SPINLOCK(die_lock); 228 229 /* 230 * This function is protected against re-entrancy. 231 */ 232 NORET_TYPE void die(const char *str, struct pt_regs *regs, int err) 233 { 234 struct thread_info *thread = current_thread_info(); 235 236 oops_enter(); 237 238 console_verbose(); 239 spin_lock_irq(&die_lock); 240 bust_spinlocks(1); 241 __die(str, err, thread, regs); 242 bust_spinlocks(0); 243 add_taint(TAINT_DIE); 244 spin_unlock_irq(&die_lock); 245 246 if (in_interrupt()) 247 panic("Fatal exception in interrupt"); 248 249 if (panic_on_oops) 250 panic("Fatal exception"); 251 252 oops_exit(); 253 do_exit(SIGSEGV); 254 } 255 256 void arm_notify_die(const char *str, struct pt_regs *regs, 257 struct siginfo *info, unsigned long err, unsigned long trap) 258 { 259 if (user_mode(regs)) { 260 current->thread.error_code = err; 261 current->thread.trap_no = trap; 262 263 force_sig_info(info->si_signo, info, current); 264 } else { 265 die(str, regs, err); 266 } 267 } 268 269 static LIST_HEAD(undef_hook); 270 static DEFINE_SPINLOCK(undef_lock); 271 272 void register_undef_hook(struct undef_hook *hook) 273 { 274 unsigned long flags; 275 276 spin_lock_irqsave(&undef_lock, flags); 277 list_add(&hook->node, &undef_hook); 278 spin_unlock_irqrestore(&undef_lock, flags); 279 } 280 281 void unregister_undef_hook(struct undef_hook *hook) 282 { 283 unsigned long flags; 284 285 spin_lock_irqsave(&undef_lock, flags); 286 list_del(&hook->node); 287 spin_unlock_irqrestore(&undef_lock, flags); 288 } 289 290 static int call_undef_hook(struct pt_regs *regs, unsigned int instr) 291 { 292 struct undef_hook *hook; 293 unsigned long flags; 294 int (*fn)(struct pt_regs *regs, unsigned int instr) = NULL; 295 296 spin_lock_irqsave(&undef_lock, flags); 297 list_for_each_entry(hook, &undef_hook, node) 298 if ((instr & hook->instr_mask) == hook->instr_val && 299 (regs->ARM_cpsr & hook->cpsr_mask) == hook->cpsr_val) 300 fn = hook->fn; 301 spin_unlock_irqrestore(&undef_lock, flags); 302 303 return fn ? fn(regs, instr) : 1; 304 } 305 306 asmlinkage void __exception do_undefinstr(struct pt_regs *regs) 307 { 308 unsigned int correction = thumb_mode(regs) ? 2 : 4; 309 unsigned int instr; 310 siginfo_t info; 311 void __user *pc; 312 313 /* 314 * According to the ARM ARM, PC is 2 or 4 bytes ahead, 315 * depending whether we're in Thumb mode or not. 316 * Correct this offset. 317 */ 318 regs->ARM_pc -= correction; 319 320 pc = (void __user *)instruction_pointer(regs); 321 322 if (processor_mode(regs) == SVC_MODE) { 323 instr = *(u32 *) pc; 324 } else if (thumb_mode(regs)) { 325 get_user(instr, (u16 __user *)pc); 326 } else { 327 get_user(instr, (u32 __user *)pc); 328 } 329 330 if (call_undef_hook(regs, instr) == 0) 331 return; 332 333 #ifdef CONFIG_DEBUG_USER 334 if (user_debug & UDBG_UNDEFINED) { 335 printk(KERN_INFO "%s (%d): undefined instruction: pc=%p\n", 336 current->comm, task_pid_nr(current), pc); 337 dump_instr(regs); 338 } 339 #endif 340 341 info.si_signo = SIGILL; 342 info.si_errno = 0; 343 info.si_code = ILL_ILLOPC; 344 info.si_addr = pc; 345 346 arm_notify_die("Oops - undefined instruction", regs, &info, 0, 6); 347 } 348 349 asmlinkage void do_unexp_fiq (struct pt_regs *regs) 350 { 351 printk("Hmm. Unexpected FIQ received, but trying to continue\n"); 352 printk("You may have a hardware problem...\n"); 353 } 354 355 /* 356 * bad_mode handles the impossible case in the vectors. If you see one of 357 * these, then it's extremely serious, and could mean you have buggy hardware. 358 * It never returns, and never tries to sync. We hope that we can at least 359 * dump out some state information... 360 */ 361 asmlinkage void bad_mode(struct pt_regs *regs, int reason) 362 { 363 console_verbose(); 364 365 printk(KERN_CRIT "Bad mode in %s handler detected\n", handler[reason]); 366 367 die("Oops - bad mode", regs, 0); 368 local_irq_disable(); 369 panic("bad mode"); 370 } 371 372 static int bad_syscall(int n, struct pt_regs *regs) 373 { 374 struct thread_info *thread = current_thread_info(); 375 siginfo_t info; 376 377 if (current->personality != PER_LINUX && 378 current->personality != PER_LINUX_32BIT && 379 thread->exec_domain->handler) { 380 thread->exec_domain->handler(n, regs); 381 return regs->ARM_r0; 382 } 383 384 #ifdef CONFIG_DEBUG_USER 385 if (user_debug & UDBG_SYSCALL) { 386 printk(KERN_ERR "[%d] %s: obsolete system call %08x.\n", 387 task_pid_nr(current), current->comm, n); 388 dump_instr(regs); 389 } 390 #endif 391 392 info.si_signo = SIGILL; 393 info.si_errno = 0; 394 info.si_code = ILL_ILLTRP; 395 info.si_addr = (void __user *)instruction_pointer(regs) - 396 (thumb_mode(regs) ? 2 : 4); 397 398 arm_notify_die("Oops - bad syscall", regs, &info, n, 0); 399 400 return regs->ARM_r0; 401 } 402 403 static inline void 404 do_cache_op(unsigned long start, unsigned long end, int flags) 405 { 406 struct vm_area_struct *vma; 407 408 if (end < start || flags) 409 return; 410 411 vma = find_vma(current->active_mm, start); 412 if (vma && vma->vm_start < end) { 413 if (start < vma->vm_start) 414 start = vma->vm_start; 415 if (end > vma->vm_end) 416 end = vma->vm_end; 417 418 flush_cache_user_range(vma, start, end); 419 } 420 } 421 422 /* 423 * Handle all unrecognised system calls. 424 * 0x9f0000 - 0x9fffff are some more esoteric system calls 425 */ 426 #define NR(x) ((__ARM_NR_##x) - __ARM_NR_BASE) 427 asmlinkage int arm_syscall(int no, struct pt_regs *regs) 428 { 429 struct thread_info *thread = current_thread_info(); 430 siginfo_t info; 431 432 if ((no >> 16) != (__ARM_NR_BASE>> 16)) 433 return bad_syscall(no, regs); 434 435 switch (no & 0xffff) { 436 case 0: /* branch through 0 */ 437 info.si_signo = SIGSEGV; 438 info.si_errno = 0; 439 info.si_code = SEGV_MAPERR; 440 info.si_addr = NULL; 441 442 arm_notify_die("branch through zero", regs, &info, 0, 0); 443 return 0; 444 445 case NR(breakpoint): /* SWI BREAK_POINT */ 446 regs->ARM_pc -= thumb_mode(regs) ? 2 : 4; 447 ptrace_break(current, regs); 448 return regs->ARM_r0; 449 450 /* 451 * Flush a region from virtual address 'r0' to virtual address 'r1' 452 * _exclusive_. There is no alignment requirement on either address; 453 * user space does not need to know the hardware cache layout. 454 * 455 * r2 contains flags. It should ALWAYS be passed as ZERO until it 456 * is defined to be something else. For now we ignore it, but may 457 * the fires of hell burn in your belly if you break this rule. ;) 458 * 459 * (at a later date, we may want to allow this call to not flush 460 * various aspects of the cache. Passing '0' will guarantee that 461 * everything necessary gets flushed to maintain consistency in 462 * the specified region). 463 */ 464 case NR(cacheflush): 465 do_cache_op(regs->ARM_r0, regs->ARM_r1, regs->ARM_r2); 466 return 0; 467 468 case NR(usr26): 469 if (!(elf_hwcap & HWCAP_26BIT)) 470 break; 471 regs->ARM_cpsr &= ~MODE32_BIT; 472 return regs->ARM_r0; 473 474 case NR(usr32): 475 if (!(elf_hwcap & HWCAP_26BIT)) 476 break; 477 regs->ARM_cpsr |= MODE32_BIT; 478 return regs->ARM_r0; 479 480 case NR(set_tls): 481 thread->tp_value = regs->ARM_r0; 482 #if defined(CONFIG_HAS_TLS_REG) 483 asm ("mcr p15, 0, %0, c13, c0, 3" : : "r" (regs->ARM_r0) ); 484 #elif !defined(CONFIG_TLS_REG_EMUL) 485 /* 486 * User space must never try to access this directly. 487 * Expect your app to break eventually if you do so. 488 * The user helper at 0xffff0fe0 must be used instead. 489 * (see entry-armv.S for details) 490 */ 491 *((unsigned int *)0xffff0ff0) = regs->ARM_r0; 492 #endif 493 return 0; 494 495 #ifdef CONFIG_NEEDS_SYSCALL_FOR_CMPXCHG 496 /* 497 * Atomically store r1 in *r2 if *r2 is equal to r0 for user space. 498 * Return zero in r0 if *MEM was changed or non-zero if no exchange 499 * happened. Also set the user C flag accordingly. 500 * If access permissions have to be fixed up then non-zero is 501 * returned and the operation has to be re-attempted. 502 * 503 * *NOTE*: This is a ghost syscall private to the kernel. Only the 504 * __kuser_cmpxchg code in entry-armv.S should be aware of its 505 * existence. Don't ever use this from user code. 506 */ 507 case 0xfff0: 508 for (;;) { 509 extern void do_DataAbort(unsigned long addr, unsigned int fsr, 510 struct pt_regs *regs); 511 unsigned long val; 512 unsigned long addr = regs->ARM_r2; 513 struct mm_struct *mm = current->mm; 514 pgd_t *pgd; pmd_t *pmd; pte_t *pte; 515 spinlock_t *ptl; 516 517 regs->ARM_cpsr &= ~PSR_C_BIT; 518 down_read(&mm->mmap_sem); 519 pgd = pgd_offset(mm, addr); 520 if (!pgd_present(*pgd)) 521 goto bad_access; 522 pmd = pmd_offset(pgd, addr); 523 if (!pmd_present(*pmd)) 524 goto bad_access; 525 pte = pte_offset_map_lock(mm, pmd, addr, &ptl); 526 if (!pte_present(*pte) || !pte_dirty(*pte)) { 527 pte_unmap_unlock(pte, ptl); 528 goto bad_access; 529 } 530 val = *(unsigned long *)addr; 531 val -= regs->ARM_r0; 532 if (val == 0) { 533 *(unsigned long *)addr = regs->ARM_r1; 534 regs->ARM_cpsr |= PSR_C_BIT; 535 } 536 pte_unmap_unlock(pte, ptl); 537 up_read(&mm->mmap_sem); 538 return val; 539 540 bad_access: 541 up_read(&mm->mmap_sem); 542 /* simulate a write access fault */ 543 do_DataAbort(addr, 15 + (1 << 11), regs); 544 } 545 #endif 546 547 default: 548 /* Calls 9f00xx..9f07ff are defined to return -ENOSYS 549 if not implemented, rather than raising SIGILL. This 550 way the calling program can gracefully determine whether 551 a feature is supported. */ 552 if (no <= 0x7ff) 553 return -ENOSYS; 554 break; 555 } 556 #ifdef CONFIG_DEBUG_USER 557 /* 558 * experience shows that these seem to indicate that 559 * something catastrophic has happened 560 */ 561 if (user_debug & UDBG_SYSCALL) { 562 printk("[%d] %s: arm syscall %d\n", 563 task_pid_nr(current), current->comm, no); 564 dump_instr(regs); 565 if (user_mode(regs)) { 566 __show_regs(regs); 567 c_backtrace(regs->ARM_fp, processor_mode(regs)); 568 } 569 } 570 #endif 571 info.si_signo = SIGILL; 572 info.si_errno = 0; 573 info.si_code = ILL_ILLTRP; 574 info.si_addr = (void __user *)instruction_pointer(regs) - 575 (thumb_mode(regs) ? 2 : 4); 576 577 arm_notify_die("Oops - bad syscall(2)", regs, &info, no, 0); 578 return 0; 579 } 580 581 #ifdef CONFIG_TLS_REG_EMUL 582 583 /* 584 * We might be running on an ARMv6+ processor which should have the TLS 585 * register but for some reason we can't use it, or maybe an SMP system 586 * using a pre-ARMv6 processor (there are apparently a few prototypes like 587 * that in existence) and therefore access to that register must be 588 * emulated. 589 */ 590 591 static int get_tp_trap(struct pt_regs *regs, unsigned int instr) 592 { 593 int reg = (instr >> 12) & 15; 594 if (reg == 15) 595 return 1; 596 regs->uregs[reg] = current_thread_info()->tp_value; 597 regs->ARM_pc += 4; 598 return 0; 599 } 600 601 static struct undef_hook arm_mrc_hook = { 602 .instr_mask = 0x0fff0fff, 603 .instr_val = 0x0e1d0f70, 604 .cpsr_mask = PSR_T_BIT, 605 .cpsr_val = 0, 606 .fn = get_tp_trap, 607 }; 608 609 static int __init arm_mrc_hook_init(void) 610 { 611 register_undef_hook(&arm_mrc_hook); 612 return 0; 613 } 614 615 late_initcall(arm_mrc_hook_init); 616 617 #endif 618 619 void __bad_xchg(volatile void *ptr, int size) 620 { 621 printk("xchg: bad data size: pc 0x%p, ptr 0x%p, size %d\n", 622 __builtin_return_address(0), ptr, size); 623 BUG(); 624 } 625 EXPORT_SYMBOL(__bad_xchg); 626 627 /* 628 * A data abort trap was taken, but we did not handle the instruction. 629 * Try to abort the user program, or panic if it was the kernel. 630 */ 631 asmlinkage void 632 baddataabort(int code, unsigned long instr, struct pt_regs *regs) 633 { 634 unsigned long addr = instruction_pointer(regs); 635 siginfo_t info; 636 637 #ifdef CONFIG_DEBUG_USER 638 if (user_debug & UDBG_BADABORT) { 639 printk(KERN_ERR "[%d] %s: bad data abort: code %d instr 0x%08lx\n", 640 task_pid_nr(current), current->comm, code, instr); 641 dump_instr(regs); 642 show_pte(current->mm, addr); 643 } 644 #endif 645 646 info.si_signo = SIGILL; 647 info.si_errno = 0; 648 info.si_code = ILL_ILLOPC; 649 info.si_addr = (void __user *)addr; 650 651 arm_notify_die("unknown data abort code", regs, &info, instr, 0); 652 } 653 654 void __attribute__((noreturn)) __bug(const char *file, int line) 655 { 656 printk(KERN_CRIT"kernel BUG at %s:%d!\n", file, line); 657 *(int *)0 = 0; 658 659 /* Avoid "noreturn function does return" */ 660 for (;;); 661 } 662 EXPORT_SYMBOL(__bug); 663 664 void __readwrite_bug(const char *fn) 665 { 666 printk("%s called, but not implemented\n", fn); 667 BUG(); 668 } 669 EXPORT_SYMBOL(__readwrite_bug); 670 671 void __pte_error(const char *file, int line, unsigned long val) 672 { 673 printk("%s:%d: bad pte %08lx.\n", file, line, val); 674 } 675 676 void __pmd_error(const char *file, int line, unsigned long val) 677 { 678 printk("%s:%d: bad pmd %08lx.\n", file, line, val); 679 } 680 681 void __pgd_error(const char *file, int line, unsigned long val) 682 { 683 printk("%s:%d: bad pgd %08lx.\n", file, line, val); 684 } 685 686 asmlinkage void __div0(void) 687 { 688 printk("Division by zero in kernel.\n"); 689 dump_stack(); 690 } 691 EXPORT_SYMBOL(__div0); 692 693 void abort(void) 694 { 695 BUG(); 696 697 /* if that doesn't kill us, halt */ 698 panic("Oops failed to kill thread"); 699 } 700 EXPORT_SYMBOL(abort); 701 702 void __init trap_init(void) 703 { 704 return; 705 } 706 707 void __init early_trap_init(void) 708 { 709 unsigned long vectors = CONFIG_VECTORS_BASE; 710 extern char __stubs_start[], __stubs_end[]; 711 extern char __vectors_start[], __vectors_end[]; 712 extern char __kuser_helper_start[], __kuser_helper_end[]; 713 int kuser_sz = __kuser_helper_end - __kuser_helper_start; 714 715 /* 716 * Copy the vectors, stubs and kuser helpers (in entry-armv.S) 717 * into the vector page, mapped at 0xffff0000, and ensure these 718 * are visible to the instruction stream. 719 */ 720 memcpy((void *)vectors, __vectors_start, __vectors_end - __vectors_start); 721 memcpy((void *)vectors + 0x200, __stubs_start, __stubs_end - __stubs_start); 722 memcpy((void *)vectors + 0x1000 - kuser_sz, __kuser_helper_start, kuser_sz); 723 724 /* 725 * Copy signal return handlers into the vector page, and 726 * set sigreturn to be a pointer to these. 727 */ 728 memcpy((void *)KERN_SIGRETURN_CODE, sigreturn_codes, 729 sizeof(sigreturn_codes)); 730 731 flush_icache_range(vectors, vectors + PAGE_SIZE); 732 modify_domain(DOMAIN_USER, DOMAIN_CLIENT); 733 } 734