2 * linux/arch/arm/kernel/traps.c
4 * Copyright (C) 1995-2002 Russell King
5 * Fragments that appear the same as linux/arch/i386/kernel/traps.c (C) Linus Torvalds
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.
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.
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>
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>
33 static const char *handler[]= { "prefetch abort", "data abort", "address exception", "interrupt" };
35 #ifdef CONFIG_DEBUG_USER
36 unsigned int user_debug;
38 static int __init user_debug_setup(char *str)
40 get_option(&str, &user_debug);
43 __setup("user_debug=", user_debug_setup);
46 static void dump_mem(const char *str, unsigned long bottom, unsigned long top);
48 void dump_backtrace_entry(unsigned long where, unsigned long from, unsigned long frame)
50 #ifdef CONFIG_KALLSYMS
51 printk("[<%08lx>] ", where);
52 print_symbol("(%s) ", where);
53 printk("from [<%08lx>] ", from);
54 print_symbol("(%s)\n", from);
56 printk("Function entered at [<%08lx>] from [<%08lx>]\n", where, from);
59 if (in_exception_text(where))
60 dump_mem("Exception stack", frame + 4, frame + 4 + sizeof(struct pt_regs));
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.
68 static int verify_stack(unsigned long sp)
70 if (sp < PAGE_OFFSET ||
71 (sp > (unsigned long)high_memory && high_memory != NULL))
78 * Dump out the contents of some memory nicely...
80 static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
82 unsigned long p = bottom & ~31;
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.
94 printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
96 for (p = bottom & ~31; p < top;) {
97 printk("%04lx: ", p & 0xffff);
99 for (i = 0; i < 8; i++, p += 4) {
102 if (p < bottom || p >= top)
105 __get_user(val, (unsigned long *)p);
106 printk("%08x ", val);
115 static void dump_instr(struct pt_regs *regs)
117 unsigned long addr = instruction_pointer(regs);
118 const int thumb = thumb_mode(regs);
119 const int width = thumb ? 4 : 8;
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.
132 for (i = -4; i < 1; i++) {
133 unsigned int val, bad;
136 bad = __get_user(val, &((u16 *)addr)[i]);
138 bad = __get_user(val, &((u32 *)addr)[i]);
141 printk(i == 0 ? "(%0*x) " : "%0*x ", width, val);
143 printk("bad PC value.");
152 static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
157 printk("Backtrace: ");
160 printk("no frame pointer");
162 } else if (verify_stack(fp)) {
163 printk("invalid frame pointer 0x%08x", fp);
165 } else if (fp < (unsigned long)end_of_stack(tsk))
166 printk("frame pointer underflow");
170 c_backtrace(fp, processor_mode(regs));
173 void dump_stack(void)
178 EXPORT_SYMBOL(dump_stack);
180 void show_stack(struct task_struct *tsk, unsigned long *sp)
188 fp = thread_saved_fp(tsk);
190 asm("mov %0, fp" : "=r" (fp) : : "cc");
192 c_backtrace(fp, 0x10);
196 #ifdef CONFIG_PREEMPT
197 #define S_PREEMPT " PREEMPT"
207 static void __die(const char *str, int err, struct thread_info *thread, struct pt_regs *regs)
209 struct task_struct *tsk = thread->task;
210 static int die_counter;
212 printk("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n",
213 str, err, ++die_counter);
216 printk("Process %s (pid: %d, stack limit = 0x%p)\n",
217 tsk->comm, task_pid_nr(tsk), thread + 1);
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);
227 DEFINE_SPINLOCK(die_lock);
230 * This function is protected against re-entrancy.
232 NORET_TYPE void die(const char *str, struct pt_regs *regs, int err)
234 struct thread_info *thread = current_thread_info();
239 spin_lock_irq(&die_lock);
241 __die(str, err, thread, regs);
243 add_taint(TAINT_DIE);
244 spin_unlock_irq(&die_lock);
247 panic("Fatal exception in interrupt");
250 panic("Fatal exception");
256 void arm_notify_die(const char *str, struct pt_regs *regs,
257 struct siginfo *info, unsigned long err, unsigned long trap)
259 if (user_mode(regs)) {
260 current->thread.error_code = err;
261 current->thread.trap_no = trap;
263 force_sig_info(info->si_signo, info, current);
269 static LIST_HEAD(undef_hook);
270 static DEFINE_SPINLOCK(undef_lock);
272 void register_undef_hook(struct undef_hook *hook)
276 spin_lock_irqsave(&undef_lock, flags);
277 list_add(&hook->node, &undef_hook);
278 spin_unlock_irqrestore(&undef_lock, flags);
281 void unregister_undef_hook(struct undef_hook *hook)
285 spin_lock_irqsave(&undef_lock, flags);
286 list_del(&hook->node);
287 spin_unlock_irqrestore(&undef_lock, flags);
290 static int call_undef_hook(struct pt_regs *regs, unsigned int instr)
292 struct undef_hook *hook;
294 int (*fn)(struct pt_regs *regs, unsigned int instr) = NULL;
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)
301 spin_unlock_irqrestore(&undef_lock, flags);
303 return fn ? fn(regs, instr) : 1;
306 asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
308 unsigned int correction = thumb_mode(regs) ? 2 : 4;
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.
318 regs->ARM_pc -= correction;
320 pc = (void __user *)instruction_pointer(regs);
322 if (processor_mode(regs) == SVC_MODE) {
324 } else if (thumb_mode(regs)) {
325 get_user(instr, (u16 __user *)pc);
327 get_user(instr, (u32 __user *)pc);
330 if (call_undef_hook(regs, instr) == 0)
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);
341 info.si_signo = SIGILL;
343 info.si_code = ILL_ILLOPC;
346 arm_notify_die("Oops - undefined instruction", regs, &info, 0, 6);
349 asmlinkage void do_unexp_fiq (struct pt_regs *regs)
351 printk("Hmm. Unexpected FIQ received, but trying to continue\n");
352 printk("You may have a hardware problem...\n");
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...
361 asmlinkage void bad_mode(struct pt_regs *regs, int reason)
365 printk(KERN_CRIT "Bad mode in %s handler detected\n", handler[reason]);
367 die("Oops - bad mode", regs, 0);
372 static int bad_syscall(int n, struct pt_regs *regs)
374 struct thread_info *thread = current_thread_info();
377 if (current->personality != PER_LINUX &&
378 current->personality != PER_LINUX_32BIT &&
379 thread->exec_domain->handler) {
380 thread->exec_domain->handler(n, regs);
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);
392 info.si_signo = SIGILL;
394 info.si_code = ILL_ILLTRP;
395 info.si_addr = (void __user *)instruction_pointer(regs) -
396 (thumb_mode(regs) ? 2 : 4);
398 arm_notify_die("Oops - bad syscall", regs, &info, n, 0);
404 do_cache_op(unsigned long start, unsigned long end, int flags)
406 struct vm_area_struct *vma;
408 if (end < start || flags)
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)
418 flush_cache_user_range(vma, start, end);
423 * Handle all unrecognised system calls.
424 * 0x9f0000 - 0x9fffff are some more esoteric system calls
426 #define NR(x) ((__ARM_NR_##x) - __ARM_NR_BASE)
427 asmlinkage int arm_syscall(int no, struct pt_regs *regs)
429 struct thread_info *thread = current_thread_info();
432 if ((no >> 16) != (__ARM_NR_BASE>> 16))
433 return bad_syscall(no, regs);
435 switch (no & 0xffff) {
436 case 0: /* branch through 0 */
437 info.si_signo = SIGSEGV;
439 info.si_code = SEGV_MAPERR;
442 arm_notify_die("branch through zero", regs, &info, 0, 0);
445 case NR(breakpoint): /* SWI BREAK_POINT */
446 regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
447 ptrace_break(current, regs);
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.
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. ;)
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).
465 do_cache_op(regs->ARM_r0, regs->ARM_r1, regs->ARM_r2);
469 if (!(elf_hwcap & HWCAP_26BIT))
471 regs->ARM_cpsr &= ~MODE32_BIT;
475 if (!(elf_hwcap & HWCAP_26BIT))
477 regs->ARM_cpsr |= MODE32_BIT;
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)
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)
491 *((unsigned int *)0xffff0ff0) = regs->ARM_r0;
495 #ifdef CONFIG_NEEDS_SYSCALL_FOR_CMPXCHG
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.
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.
509 extern void do_DataAbort(unsigned long addr, unsigned int fsr,
510 struct pt_regs *regs);
512 unsigned long addr = regs->ARM_r2;
513 struct mm_struct *mm = current->mm;
514 pgd_t *pgd; pmd_t *pmd; pte_t *pte;
517 regs->ARM_cpsr &= ~PSR_C_BIT;
518 down_read(&mm->mmap_sem);
519 pgd = pgd_offset(mm, addr);
520 if (!pgd_present(*pgd))
522 pmd = pmd_offset(pgd, addr);
523 if (!pmd_present(*pmd))
525 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
526 if (!pte_present(*pte) || !pte_dirty(*pte)) {
527 pte_unmap_unlock(pte, ptl);
530 val = *(unsigned long *)addr;
533 *(unsigned long *)addr = regs->ARM_r1;
534 regs->ARM_cpsr |= PSR_C_BIT;
536 pte_unmap_unlock(pte, ptl);
537 up_read(&mm->mmap_sem);
541 up_read(&mm->mmap_sem);
542 /* simulate a write access fault */
543 do_DataAbort(addr, 15 + (1 << 11), regs);
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. */
556 #ifdef CONFIG_DEBUG_USER
558 * experience shows that these seem to indicate that
559 * something catastrophic has happened
561 if (user_debug & UDBG_SYSCALL) {
562 printk("[%d] %s: arm syscall %d\n",
563 task_pid_nr(current), current->comm, no);
565 if (user_mode(regs)) {
567 c_backtrace(regs->ARM_fp, processor_mode(regs));
571 info.si_signo = SIGILL;
573 info.si_code = ILL_ILLTRP;
574 info.si_addr = (void __user *)instruction_pointer(regs) -
575 (thumb_mode(regs) ? 2 : 4);
577 arm_notify_die("Oops - bad syscall(2)", regs, &info, no, 0);
581 #ifdef CONFIG_TLS_REG_EMUL
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
591 static int get_tp_trap(struct pt_regs *regs, unsigned int instr)
593 int reg = (instr >> 12) & 15;
596 regs->uregs[reg] = current_thread_info()->tp_value;
601 static struct undef_hook arm_mrc_hook = {
602 .instr_mask = 0x0fff0fff,
603 .instr_val = 0x0e1d0f70,
604 .cpsr_mask = PSR_T_BIT,
609 static int __init arm_mrc_hook_init(void)
611 register_undef_hook(&arm_mrc_hook);
615 late_initcall(arm_mrc_hook_init);
619 void __bad_xchg(volatile void *ptr, int size)
621 printk("xchg: bad data size: pc 0x%p, ptr 0x%p, size %d\n",
622 __builtin_return_address(0), ptr, size);
625 EXPORT_SYMBOL(__bad_xchg);
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.
632 baddataabort(int code, unsigned long instr, struct pt_regs *regs)
634 unsigned long addr = instruction_pointer(regs);
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);
642 show_pte(current->mm, addr);
646 info.si_signo = SIGILL;
648 info.si_code = ILL_ILLOPC;
649 info.si_addr = (void __user *)addr;
651 arm_notify_die("unknown data abort code", regs, &info, instr, 0);
654 void __attribute__((noreturn)) __bug(const char *file, int line)
656 printk(KERN_CRIT"kernel BUG at %s:%d!\n", file, line);
659 /* Avoid "noreturn function does return" */
662 EXPORT_SYMBOL(__bug);
664 void __readwrite_bug(const char *fn)
666 printk("%s called, but not implemented\n", fn);
669 EXPORT_SYMBOL(__readwrite_bug);
671 void __pte_error(const char *file, int line, unsigned long val)
673 printk("%s:%d: bad pte %08lx.\n", file, line, val);
676 void __pmd_error(const char *file, int line, unsigned long val)
678 printk("%s:%d: bad pmd %08lx.\n", file, line, val);
681 void __pgd_error(const char *file, int line, unsigned long val)
683 printk("%s:%d: bad pgd %08lx.\n", file, line, val);
686 asmlinkage void __div0(void)
688 printk("Division by zero in kernel.\n");
691 EXPORT_SYMBOL(__div0);
697 /* if that doesn't kill us, halt */
698 panic("Oops failed to kill thread");
700 EXPORT_SYMBOL(abort);
702 void __init trap_init(void)
707 void __init early_trap_init(void)
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;
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.
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);
725 * Copy signal return handlers into the vector page, and
726 * set sigreturn to be a pointer to these.
728 memcpy((void *)KERN_SIGRETURN_CODE, sigreturn_codes,
729 sizeof(sigreturn_codes));
731 flush_icache_range(vectors, vectors + PAGE_SIZE);
732 modify_domain(DOMAIN_USER, DOMAIN_CLIENT);