2 * linux/arch/arm/kernel/traps.c
4 * Copyright (C) 1995-2009 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/signal.h>
16 #include <linux/personality.h>
17 #include <linux/kallsyms.h>
18 #include <linux/spinlock.h>
19 #include <linux/uaccess.h>
20 #include <linux/hardirq.h>
21 #include <linux/kdebug.h>
22 #include <linux/module.h>
23 #include <linux/kexec.h>
24 #include <linux/delay.h>
25 #include <linux/init.h>
27 #include <asm/atomic.h>
28 #include <asm/cacheflush.h>
29 #include <asm/system.h>
30 #include <asm/unistd.h>
31 #include <asm/traps.h>
32 #include <asm/unwind.h>
38 static const char *handler
[]= { "prefetch abort", "data abort", "address exception", "interrupt" };
40 #ifdef CONFIG_DEBUG_USER
41 unsigned int user_debug
;
43 static int __init
user_debug_setup(char *str
)
45 get_option(&str
, &user_debug
);
48 __setup("user_debug=", user_debug_setup
);
51 static void dump_mem(const char *, const char *, unsigned long, unsigned long);
53 void dump_backtrace_entry(unsigned long where
, unsigned long from
, unsigned long frame
)
55 #ifdef CONFIG_KALLSYMS
56 char sym1
[KSYM_SYMBOL_LEN
], sym2
[KSYM_SYMBOL_LEN
];
57 sprint_symbol(sym1
, where
);
58 sprint_symbol(sym2
, from
);
59 printk("[<%08lx>] (%s) from [<%08lx>] (%s)\n", where
, sym1
, from
, sym2
);
61 printk("Function entered at [<%08lx>] from [<%08lx>]\n", where
, from
);
64 if (in_exception_text(where
))
65 dump_mem("", "Exception stack", frame
+ 4, frame
+ 4 + sizeof(struct pt_regs
));
68 #ifndef CONFIG_ARM_UNWIND
70 * Stack pointers should always be within the kernels view of
71 * physical memory. If it is not there, then we can't dump
72 * out any information relating to the stack.
74 static int verify_stack(unsigned long sp
)
76 if (sp
< PAGE_OFFSET
||
77 (sp
> (unsigned long)high_memory
&& high_memory
!= NULL
))
85 * Dump out the contents of some memory nicely...
87 static void dump_mem(const char *lvl
, const char *str
, unsigned long bottom
,
95 * We need to switch to kernel mode so that we can use __get_user
96 * to safely read from kernel space. Note that we now dump the
97 * code first, just in case the backtrace kills us.
102 printk("%s%s(0x%08lx to 0x%08lx)\n", lvl
, str
, bottom
, top
);
104 for (first
= bottom
& ~31; first
< top
; first
+= 32) {
106 char str
[sizeof(" 12345678") * 8 + 1];
108 memset(str
, ' ', sizeof(str
));
109 str
[sizeof(str
) - 1] = '\0';
111 for (p
= first
, i
= 0; i
< 8 && p
< top
; i
++, p
+= 4) {
112 if (p
>= bottom
&& p
< top
) {
114 if (__get_user(val
, (unsigned long *)p
) == 0)
115 sprintf(str
+ i
* 9, " %08lx", val
);
117 sprintf(str
+ i
* 9, " ????????");
120 printk("%s%04lx:%s\n", lvl
, first
& 0xffff, str
);
126 static void dump_instr(const char *lvl
, struct pt_regs
*regs
)
128 unsigned long addr
= instruction_pointer(regs
);
129 const int thumb
= thumb_mode(regs
);
130 const int width
= thumb
? 4 : 8;
132 char str
[sizeof("00000000 ") * 5 + 2 + 1], *p
= str
;
136 * We need to switch to kernel mode so that we can use __get_user
137 * to safely read from kernel space. Note that we now dump the
138 * code first, just in case the backtrace kills us.
143 for (i
= -4; i
< 1; i
++) {
144 unsigned int val
, bad
;
147 bad
= __get_user(val
, &((u16
*)addr
)[i
]);
149 bad
= __get_user(val
, &((u32
*)addr
)[i
]);
152 p
+= sprintf(p
, i
== 0 ? "(%0*x) " : "%0*x ",
155 p
+= sprintf(p
, "bad PC value");
159 printk("%sCode: %s\n", lvl
, str
);
164 #ifdef CONFIG_ARM_UNWIND
165 static inline void dump_backtrace(struct pt_regs
*regs
, struct task_struct
*tsk
)
167 unwind_backtrace(regs
, tsk
);
170 static void dump_backtrace(struct pt_regs
*regs
, struct task_struct
*tsk
)
172 unsigned int fp
, mode
;
175 printk("Backtrace: ");
182 mode
= processor_mode(regs
);
183 } else if (tsk
!= current
) {
184 fp
= thread_saved_fp(tsk
);
187 asm("mov %0, fp" : "=r" (fp
) : : "cc");
192 printk("no frame pointer");
194 } else if (verify_stack(fp
)) {
195 printk("invalid frame pointer 0x%08x", fp
);
197 } else if (fp
< (unsigned long)end_of_stack(tsk
))
198 printk("frame pointer underflow");
202 c_backtrace(fp
, mode
);
206 void dump_stack(void)
208 dump_backtrace(NULL
, NULL
);
211 EXPORT_SYMBOL(dump_stack
);
213 void show_stack(struct task_struct
*tsk
, unsigned long *sp
)
215 dump_backtrace(NULL
, tsk
);
219 #ifdef CONFIG_PREEMPT
220 #define S_PREEMPT " PREEMPT"
230 static int __die(const char *str
, int err
, struct thread_info
*thread
, struct pt_regs
*regs
)
232 struct task_struct
*tsk
= thread
->task
;
233 static int die_counter
;
236 printk(KERN_EMERG
"Internal error: %s: %x [#%d]" S_PREEMPT S_SMP
"\n",
237 str
, err
, ++die_counter
);
238 sysfs_printk_last_file();
240 /* trap and error numbers are mostly meaningless on ARM */
241 ret
= notify_die(DIE_OOPS
, str
, regs
, err
, tsk
->thread
.trap_no
, SIGSEGV
);
242 if (ret
== NOTIFY_STOP
)
247 printk(KERN_EMERG
"Process %.*s (pid: %d, stack limit = 0x%p)\n",
248 TASK_COMM_LEN
, tsk
->comm
, task_pid_nr(tsk
), thread
+ 1);
250 if (!user_mode(regs
) || in_interrupt()) {
251 dump_mem(KERN_EMERG
, "Stack: ", regs
->ARM_sp
,
252 THREAD_SIZE
+ (unsigned long)task_stack_page(tsk
));
253 dump_backtrace(regs
, tsk
);
254 dump_instr(KERN_EMERG
, regs
);
260 DEFINE_SPINLOCK(die_lock
);
263 * This function is protected against re-entrancy.
265 void die(const char *str
, struct pt_regs
*regs
, int err
)
267 struct thread_info
*thread
= current_thread_info();
272 spin_lock_irq(&die_lock
);
275 ret
= __die(str
, err
, thread
, regs
);
277 if (regs
&& kexec_should_crash(thread
->task
))
281 add_taint(TAINT_DIE
);
282 spin_unlock_irq(&die_lock
);
286 panic("Fatal exception in interrupt");
288 panic("Fatal exception");
289 if (ret
!= NOTIFY_STOP
)
293 void arm_notify_die(const char *str
, struct pt_regs
*regs
,
294 struct siginfo
*info
, unsigned long err
, unsigned long trap
)
296 if (user_mode(regs
)) {
297 current
->thread
.error_code
= err
;
298 current
->thread
.trap_no
= trap
;
300 force_sig_info(info
->si_signo
, info
, current
);
306 static LIST_HEAD(undef_hook
);
307 static DEFINE_SPINLOCK(undef_lock
);
309 void register_undef_hook(struct undef_hook
*hook
)
313 spin_lock_irqsave(&undef_lock
, flags
);
314 list_add(&hook
->node
, &undef_hook
);
315 spin_unlock_irqrestore(&undef_lock
, flags
);
318 void unregister_undef_hook(struct undef_hook
*hook
)
322 spin_lock_irqsave(&undef_lock
, flags
);
323 list_del(&hook
->node
);
324 spin_unlock_irqrestore(&undef_lock
, flags
);
327 static int call_undef_hook(struct pt_regs
*regs
, unsigned int instr
)
329 struct undef_hook
*hook
;
331 int (*fn
)(struct pt_regs
*regs
, unsigned int instr
) = NULL
;
333 spin_lock_irqsave(&undef_lock
, flags
);
334 list_for_each_entry(hook
, &undef_hook
, node
)
335 if ((instr
& hook
->instr_mask
) == hook
->instr_val
&&
336 (regs
->ARM_cpsr
& hook
->cpsr_mask
) == hook
->cpsr_val
)
338 spin_unlock_irqrestore(&undef_lock
, flags
);
340 return fn
? fn(regs
, instr
) : 1;
343 asmlinkage
void __exception
do_undefinstr(struct pt_regs
*regs
)
345 unsigned int correction
= thumb_mode(regs
) ? 2 : 4;
351 * According to the ARM ARM, PC is 2 or 4 bytes ahead,
352 * depending whether we're in Thumb mode or not.
353 * Correct this offset.
355 regs
->ARM_pc
-= correction
;
357 pc
= (void __user
*)instruction_pointer(regs
);
359 if (processor_mode(regs
) == SVC_MODE
) {
361 } else if (thumb_mode(regs
)) {
362 get_user(instr
, (u16 __user
*)pc
);
364 get_user(instr
, (u32 __user
*)pc
);
367 if (call_undef_hook(regs
, instr
) == 0)
370 #ifdef CONFIG_DEBUG_USER
371 if (user_debug
& UDBG_UNDEFINED
) {
372 printk(KERN_INFO
"%s (%d): undefined instruction: pc=%p\n",
373 current
->comm
, task_pid_nr(current
), pc
);
374 dump_instr(KERN_INFO
, regs
);
378 info
.si_signo
= SIGILL
;
380 info
.si_code
= ILL_ILLOPC
;
383 arm_notify_die("Oops - undefined instruction", regs
, &info
, 0, 6);
386 asmlinkage
void do_unexp_fiq (struct pt_regs
*regs
)
388 printk("Hmm. Unexpected FIQ received, but trying to continue\n");
389 printk("You may have a hardware problem...\n");
393 * bad_mode handles the impossible case in the vectors. If you see one of
394 * these, then it's extremely serious, and could mean you have buggy hardware.
395 * It never returns, and never tries to sync. We hope that we can at least
396 * dump out some state information...
398 asmlinkage
void bad_mode(struct pt_regs
*regs
, int reason
)
402 printk(KERN_CRIT
"Bad mode in %s handler detected\n", handler
[reason
]);
404 die("Oops - bad mode", regs
, 0);
409 static int bad_syscall(int n
, struct pt_regs
*regs
)
411 struct thread_info
*thread
= current_thread_info();
414 if (current
->personality
!= PER_LINUX
&&
415 current
->personality
!= PER_LINUX_32BIT
&&
416 thread
->exec_domain
->handler
) {
417 thread
->exec_domain
->handler(n
, regs
);
421 #ifdef CONFIG_DEBUG_USER
422 if (user_debug
& UDBG_SYSCALL
) {
423 printk(KERN_ERR
"[%d] %s: obsolete system call %08x.\n",
424 task_pid_nr(current
), current
->comm
, n
);
425 dump_instr(KERN_ERR
, regs
);
429 info
.si_signo
= SIGILL
;
431 info
.si_code
= ILL_ILLTRP
;
432 info
.si_addr
= (void __user
*)instruction_pointer(regs
) -
433 (thumb_mode(regs
) ? 2 : 4);
435 arm_notify_die("Oops - bad syscall", regs
, &info
, n
, 0);
441 do_cache_op(unsigned long start
, unsigned long end
, int flags
)
443 struct mm_struct
*mm
= current
->active_mm
;
444 struct vm_area_struct
*vma
;
446 if (end
< start
|| flags
)
449 down_read(&mm
->mmap_sem
);
450 vma
= find_vma(mm
, start
);
451 if (vma
&& vma
->vm_start
< end
) {
452 if (start
< vma
->vm_start
)
453 start
= vma
->vm_start
;
454 if (end
> vma
->vm_end
)
457 flush_cache_user_range(vma
, start
, end
);
459 up_read(&mm
->mmap_sem
);
463 * Handle all unrecognised system calls.
464 * 0x9f0000 - 0x9fffff are some more esoteric system calls
466 #define NR(x) ((__ARM_NR_##x) - __ARM_NR_BASE)
467 asmlinkage
int arm_syscall(int no
, struct pt_regs
*regs
)
469 struct thread_info
*thread
= current_thread_info();
472 if ((no
>> 16) != (__ARM_NR_BASE
>> 16))
473 return bad_syscall(no
, regs
);
475 switch (no
& 0xffff) {
476 case 0: /* branch through 0 */
477 info
.si_signo
= SIGSEGV
;
479 info
.si_code
= SEGV_MAPERR
;
482 arm_notify_die("branch through zero", regs
, &info
, 0, 0);
485 case NR(breakpoint
): /* SWI BREAK_POINT */
486 regs
->ARM_pc
-= thumb_mode(regs
) ? 2 : 4;
487 ptrace_break(current
, regs
);
491 * Flush a region from virtual address 'r0' to virtual address 'r1'
492 * _exclusive_. There is no alignment requirement on either address;
493 * user space does not need to know the hardware cache layout.
495 * r2 contains flags. It should ALWAYS be passed as ZERO until it
496 * is defined to be something else. For now we ignore it, but may
497 * the fires of hell burn in your belly if you break this rule. ;)
499 * (at a later date, we may want to allow this call to not flush
500 * various aspects of the cache. Passing '0' will guarantee that
501 * everything necessary gets flushed to maintain consistency in
502 * the specified region).
505 do_cache_op(regs
->ARM_r0
, regs
->ARM_r1
, regs
->ARM_r2
);
509 if (!(elf_hwcap
& HWCAP_26BIT
))
511 regs
->ARM_cpsr
&= ~MODE32_BIT
;
515 if (!(elf_hwcap
& HWCAP_26BIT
))
517 regs
->ARM_cpsr
|= MODE32_BIT
;
521 thread
->tp_value
= regs
->ARM_r0
;
525 asm ("mcr p15, 0, %0, c13, c0, 3"
526 : : "r" (regs
->ARM_r0
));
529 * User space must never try to access this directly.
530 * Expect your app to break eventually if you do so.
531 * The user helper at 0xffff0fe0 must be used instead.
532 * (see entry-armv.S for details)
534 *((unsigned int *)0xffff0ff0) = regs
->ARM_r0
;
538 #ifdef CONFIG_NEEDS_SYSCALL_FOR_CMPXCHG
540 * Atomically store r1 in *r2 if *r2 is equal to r0 for user space.
541 * Return zero in r0 if *MEM was changed or non-zero if no exchange
542 * happened. Also set the user C flag accordingly.
543 * If access permissions have to be fixed up then non-zero is
544 * returned and the operation has to be re-attempted.
546 * *NOTE*: This is a ghost syscall private to the kernel. Only the
547 * __kuser_cmpxchg code in entry-armv.S should be aware of its
548 * existence. Don't ever use this from user code.
552 extern void do_DataAbort(unsigned long addr
, unsigned int fsr
,
553 struct pt_regs
*regs
);
555 unsigned long addr
= regs
->ARM_r2
;
556 struct mm_struct
*mm
= current
->mm
;
557 pgd_t
*pgd
; pmd_t
*pmd
; pte_t
*pte
;
560 regs
->ARM_cpsr
&= ~PSR_C_BIT
;
561 down_read(&mm
->mmap_sem
);
562 pgd
= pgd_offset(mm
, addr
);
563 if (!pgd_present(*pgd
))
565 pmd
= pmd_offset(pgd
, addr
);
566 if (!pmd_present(*pmd
))
568 pte
= pte_offset_map_lock(mm
, pmd
, addr
, &ptl
);
569 if (!pte_present(*pte
) || !pte_dirty(*pte
)) {
570 pte_unmap_unlock(pte
, ptl
);
573 val
= *(unsigned long *)addr
;
576 *(unsigned long *)addr
= regs
->ARM_r1
;
577 regs
->ARM_cpsr
|= PSR_C_BIT
;
579 pte_unmap_unlock(pte
, ptl
);
580 up_read(&mm
->mmap_sem
);
584 up_read(&mm
->mmap_sem
);
585 /* simulate a write access fault */
586 do_DataAbort(addr
, 15 + (1 << 11), regs
);
591 /* Calls 9f00xx..9f07ff are defined to return -ENOSYS
592 if not implemented, rather than raising SIGILL. This
593 way the calling program can gracefully determine whether
594 a feature is supported. */
595 if ((no
& 0xffff) <= 0x7ff)
599 #ifdef CONFIG_DEBUG_USER
601 * experience shows that these seem to indicate that
602 * something catastrophic has happened
604 if (user_debug
& UDBG_SYSCALL
) {
605 printk("[%d] %s: arm syscall %d\n",
606 task_pid_nr(current
), current
->comm
, no
);
607 dump_instr("", regs
);
608 if (user_mode(regs
)) {
610 c_backtrace(regs
->ARM_fp
, processor_mode(regs
));
614 info
.si_signo
= SIGILL
;
616 info
.si_code
= ILL_ILLTRP
;
617 info
.si_addr
= (void __user
*)instruction_pointer(regs
) -
618 (thumb_mode(regs
) ? 2 : 4);
620 arm_notify_die("Oops - bad syscall(2)", regs
, &info
, no
, 0);
624 #ifdef CONFIG_TLS_REG_EMUL
627 * We might be running on an ARMv6+ processor which should have the TLS
628 * register but for some reason we can't use it, or maybe an SMP system
629 * using a pre-ARMv6 processor (there are apparently a few prototypes like
630 * that in existence) and therefore access to that register must be
634 static int get_tp_trap(struct pt_regs
*regs
, unsigned int instr
)
636 int reg
= (instr
>> 12) & 15;
639 regs
->uregs
[reg
] = current_thread_info()->tp_value
;
644 static struct undef_hook arm_mrc_hook
= {
645 .instr_mask
= 0x0fff0fff,
646 .instr_val
= 0x0e1d0f70,
647 .cpsr_mask
= PSR_T_BIT
,
652 static int __init
arm_mrc_hook_init(void)
654 register_undef_hook(&arm_mrc_hook
);
658 late_initcall(arm_mrc_hook_init
);
662 void __bad_xchg(volatile void *ptr
, int size
)
664 printk("xchg: bad data size: pc 0x%p, ptr 0x%p, size %d\n",
665 __builtin_return_address(0), ptr
, size
);
668 EXPORT_SYMBOL(__bad_xchg
);
671 * A data abort trap was taken, but we did not handle the instruction.
672 * Try to abort the user program, or panic if it was the kernel.
675 baddataabort(int code
, unsigned long instr
, struct pt_regs
*regs
)
677 unsigned long addr
= instruction_pointer(regs
);
680 #ifdef CONFIG_DEBUG_USER
681 if (user_debug
& UDBG_BADABORT
) {
682 printk(KERN_ERR
"[%d] %s: bad data abort: code %d instr 0x%08lx\n",
683 task_pid_nr(current
), current
->comm
, code
, instr
);
684 dump_instr(KERN_ERR
, regs
);
685 show_pte(current
->mm
, addr
);
689 info
.si_signo
= SIGILL
;
691 info
.si_code
= ILL_ILLOPC
;
692 info
.si_addr
= (void __user
*)addr
;
694 arm_notify_die("unknown data abort code", regs
, &info
, instr
, 0);
697 void __attribute__((noreturn
)) __bug(const char *file
, int line
)
699 printk(KERN_CRIT
"kernel BUG at %s:%d!\n", file
, line
);
702 /* Avoid "noreturn function does return" */
705 EXPORT_SYMBOL(__bug
);
707 void __readwrite_bug(const char *fn
)
709 printk("%s called, but not implemented\n", fn
);
712 EXPORT_SYMBOL(__readwrite_bug
);
714 void __pte_error(const char *file
, int line
, unsigned long val
)
716 printk("%s:%d: bad pte %08lx.\n", file
, line
, val
);
719 void __pmd_error(const char *file
, int line
, unsigned long val
)
721 printk("%s:%d: bad pmd %08lx.\n", file
, line
, val
);
724 void __pgd_error(const char *file
, int line
, unsigned long val
)
726 printk("%s:%d: bad pgd %08lx.\n", file
, line
, val
);
729 asmlinkage
void __div0(void)
731 printk("Division by zero in kernel.\n");
734 EXPORT_SYMBOL(__div0
);
740 /* if that doesn't kill us, halt */
741 panic("Oops failed to kill thread");
743 EXPORT_SYMBOL(abort
);
745 void __init
trap_init(void)
750 static void __init
kuser_get_tls_init(unsigned long vectors
)
753 * vectors + 0xfe0 = __kuser_get_tls
754 * vectors + 0xfe8 = hardware TLS instruction at 0xffff0fe8
756 if (tls_emu
|| has_tls_reg
)
757 memcpy((void *)vectors
+ 0xfe0, (void *)vectors
+ 0xfe8, 4);
760 void __init
early_trap_init(void)
762 unsigned long vectors
= CONFIG_VECTORS_BASE
;
763 extern char __stubs_start
[], __stubs_end
[];
764 extern char __vectors_start
[], __vectors_end
[];
765 extern char __kuser_helper_start
[], __kuser_helper_end
[];
766 int kuser_sz
= __kuser_helper_end
- __kuser_helper_start
;
769 * Copy the vectors, stubs and kuser helpers (in entry-armv.S)
770 * into the vector page, mapped at 0xffff0000, and ensure these
771 * are visible to the instruction stream.
773 memcpy((void *)vectors
, __vectors_start
, __vectors_end
- __vectors_start
);
774 memcpy((void *)vectors
+ 0x200, __stubs_start
, __stubs_end
- __stubs_start
);
775 memcpy((void *)vectors
+ 0x1000 - kuser_sz
, __kuser_helper_start
, kuser_sz
);
778 * Do processor specific fixups for the kuser helpers
780 kuser_get_tls_init(vectors
);
783 * Copy signal return handlers into the vector page, and
784 * set sigreturn to be a pointer to these.
786 memcpy((void *)KERN_SIGRETURN_CODE
, sigreturn_codes
,
787 sizeof(sigreturn_codes
));
788 memcpy((void *)KERN_RESTART_CODE
, syscall_restart_code
,
789 sizeof(syscall_restart_code
));
791 flush_icache_range(vectors
, vectors
+ PAGE_SIZE
);
792 modify_domain(DOMAIN_USER
, DOMAIN_CLIENT
);