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/config.h>
16 #include <linux/module.h>
17 #include <linux/signal.h>
18 #include <linux/spinlock.h>
19 #include <linux/personality.h>
20 #include <linux/ptrace.h>
21 #include <linux/kallsyms.h>
22 #include <linux/init.h>
24 #include <asm/atomic.h>
25 #include <asm/cacheflush.h>
27 #include <asm/system.h>
28 #include <asm/uaccess.h>
29 #include <asm/unistd.h>
30 #include <asm/traps.h>
34 extern void c_backtrace (unsigned long fp
, int pmode
);
35 extern void show_pte(struct mm_struct
*mm
, unsigned long addr
);
37 const char *processor_modes
[]=
38 { "USER_26", "FIQ_26" , "IRQ_26" , "SVC_26" , "UK4_26" , "UK5_26" , "UK6_26" , "UK7_26" ,
39 "UK8_26" , "UK9_26" , "UK10_26", "UK11_26", "UK12_26", "UK13_26", "UK14_26", "UK15_26",
40 "USER_32", "FIQ_32" , "IRQ_32" , "SVC_32" , "UK4_32" , "UK5_32" , "UK6_32" , "ABT_32" ,
41 "UK8_32" , "UK9_32" , "UK10_32", "UND_32" , "UK12_32", "UK13_32", "UK14_32", "SYS_32"
44 static const char *handler
[]= { "prefetch abort", "data abort", "address exception", "interrupt" };
46 #ifdef CONFIG_DEBUG_USER
47 unsigned int user_debug
;
49 static int __init
user_debug_setup(char *str
)
51 get_option(&str
, &user_debug
);
54 __setup("user_debug=", user_debug_setup
);
57 void dump_backtrace_entry(unsigned long where
, unsigned long from
)
59 #ifdef CONFIG_KALLSYMS
60 printk("[<%08lx>] ", where
);
61 print_symbol("(%s) ", where
);
62 printk("from [<%08lx>] ", from
);
63 print_symbol("(%s)\n", from
);
65 printk("Function entered at [<%08lx>] from [<%08lx>]\n", where
, from
);
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
|| (sp
> (unsigned long)high_memory
&& high_memory
!= 0))
83 * Dump out the contents of some memory nicely...
85 static void dump_mem(const char *str
, unsigned long bottom
, unsigned long top
)
87 unsigned long p
= bottom
& ~31;
92 * We need to switch to kernel mode so that we can use __get_user
93 * to safely read from kernel space. Note that we now dump the
94 * code first, just in case the backtrace kills us.
99 printk("%s(0x%08lx to 0x%08lx)\n", str
, bottom
, top
);
101 for (p
= bottom
& ~31; p
< top
;) {
102 printk("%04lx: ", p
& 0xffff);
104 for (i
= 0; i
< 8; i
++, p
+= 4) {
107 if (p
< bottom
|| p
>= top
)
110 __get_user(val
, (unsigned long *)p
);
111 printk("%08x ", val
);
120 static void dump_instr(struct pt_regs
*regs
)
122 unsigned long addr
= instruction_pointer(regs
);
123 const int thumb
= thumb_mode(regs
);
124 const int width
= thumb
? 4 : 8;
129 * We need to switch to kernel mode so that we can use __get_user
130 * to safely read from kernel space. Note that we now dump the
131 * code first, just in case the backtrace kills us.
137 for (i
= -4; i
< 1; i
++) {
138 unsigned int val
, bad
;
141 bad
= __get_user(val
, &((u16
*)addr
)[i
]);
143 bad
= __get_user(val
, &((u32
*)addr
)[i
]);
146 printk(i
== 0 ? "(%0*x) " : "%0*x ", width
, val
);
148 printk("bad PC value.");
157 static void dump_backtrace(struct pt_regs
*regs
, struct task_struct
*tsk
)
162 printk("Backtrace: ");
165 printk("no frame pointer");
167 } else if (verify_stack(fp
)) {
168 printk("invalid frame pointer 0x%08x", fp
);
170 } else if (fp
< (unsigned long)(tsk
->thread_info
+ 1))
171 printk("frame pointer underflow");
175 c_backtrace(fp
, processor_mode(regs
));
178 void dump_stack(void)
180 #ifdef CONFIG_DEBUG_ERRORS
185 EXPORT_SYMBOL(dump_stack
);
187 void show_stack(struct task_struct
*tsk
, unsigned long *sp
)
195 fp
= thread_saved_fp(tsk
);
197 asm("mov%? %0, fp" : "=r" (fp
));
199 c_backtrace(fp
, 0x10);
203 spinlock_t die_lock
= SPIN_LOCK_UNLOCKED
;
206 * This function is protected against re-entrancy.
208 NORET_TYPE
void die(const char *str
, struct pt_regs
*regs
, int err
)
210 struct task_struct
*tsk
= current
;
211 static int die_counter
;
214 spin_lock_irq(&die_lock
);
217 printk("Internal error: %s: %x [#%d]\n", str
, err
, ++die_counter
);
219 printk("CPU: %d\n", smp_processor_id());
221 printk("Process %s (pid: %d, stack limit = 0x%p)\n",
222 tsk
->comm
, tsk
->pid
, tsk
->thread_info
+ 1);
224 if (!user_mode(regs
) || in_interrupt()) {
225 dump_mem("Stack: ", regs
->ARM_sp
, 8192+(unsigned long)tsk
->thread_info
);
226 dump_backtrace(regs
, tsk
);
231 spin_unlock_irq(&die_lock
);
235 void die_if_kernel(const char *str
, struct pt_regs
*regs
, int err
)
243 static LIST_HEAD(undef_hook
);
244 static spinlock_t undef_lock
= SPIN_LOCK_UNLOCKED
;
246 void register_undef_hook(struct undef_hook
*hook
)
248 spin_lock_irq(&undef_lock
);
249 list_add(&hook
->node
, &undef_hook
);
250 spin_unlock_irq(&undef_lock
);
253 void unregister_undef_hook(struct undef_hook
*hook
)
255 spin_lock_irq(&undef_lock
);
256 list_del(&hook
->node
);
257 spin_unlock_irq(&undef_lock
);
260 asmlinkage
void do_undefinstr(struct pt_regs
*regs
)
262 unsigned int correction
= thumb_mode(regs
) ? 2 : 4;
264 struct undef_hook
*hook
;
269 * According to the ARM ARM, PC is 2 or 4 bytes ahead,
270 * depending whether we're in Thumb mode or not.
271 * Correct this offset.
273 regs
->ARM_pc
-= correction
;
275 pc
= (void __user
*)instruction_pointer(regs
);
276 if (thumb_mode(regs
)) {
277 get_user(instr
, (u16 __user
*)pc
);
279 get_user(instr
, (u32 __user
*)pc
);
282 spin_lock_irq(&undef_lock
);
283 list_for_each_entry(hook
, &undef_hook
, node
) {
284 if ((instr
& hook
->instr_mask
) == hook
->instr_val
&&
285 (regs
->ARM_cpsr
& hook
->cpsr_mask
) == hook
->cpsr_val
) {
286 if (hook
->fn(regs
, instr
) == 0) {
287 spin_unlock_irq(&undef_lock
);
292 spin_unlock_irq(&undef_lock
);
294 #ifdef CONFIG_DEBUG_USER
295 if (user_debug
& UDBG_UNDEFINED
) {
296 printk(KERN_INFO
"%s (%d): undefined instruction: pc=%p\n",
297 current
->comm
, current
->pid
, pc
);
302 current
->thread
.error_code
= 0;
303 current
->thread
.trap_no
= 6;
305 info
.si_signo
= SIGILL
;
307 info
.si_code
= ILL_ILLOPC
;
310 force_sig_info(SIGILL
, &info
, current
);
312 die_if_kernel("Oops - undefined instruction", regs
, 0);
315 asmlinkage
void do_unexp_fiq (struct pt_regs
*regs
)
317 #ifndef CONFIG_IGNORE_FIQ
318 printk("Hmm. Unexpected FIQ received, but trying to continue\n");
319 printk("You may have a hardware problem...\n");
324 * bad_mode handles the impossible case in the vectors. If you see one of
325 * these, then it's extremely serious, and could mean you have buggy hardware.
326 * It never returns, and never tries to sync. We hope that we can at least
327 * dump out some state information...
329 asmlinkage
void bad_mode(struct pt_regs
*regs
, int reason
, int proc_mode
)
331 unsigned int vectors
= vectors_base();
335 printk(KERN_CRIT
"Bad mode in %s handler detected: mode %s\n",
336 handler
[reason
], processor_modes
[proc_mode
]);
339 * Dump out the vectors and stub routines. Maybe a better solution
340 * would be to dump them out only if we detect that they are corrupted.
342 dump_mem(KERN_CRIT
"Vectors: ", vectors
, vectors
+ 0x40);
343 dump_mem(KERN_CRIT
"Stubs: ", vectors
+ 0x200, vectors
+ 0x4b8);
345 die("Oops - bad mode", regs
, 0);
350 static int bad_syscall(int n
, struct pt_regs
*regs
)
352 struct thread_info
*thread
= current_thread_info();
355 if (current
->personality
!= PER_LINUX
&& thread
->exec_domain
->handler
) {
356 thread
->exec_domain
->handler(n
, regs
);
360 #ifdef CONFIG_DEBUG_USER
361 if (user_debug
& UDBG_SYSCALL
) {
362 printk(KERN_ERR
"[%d] %s: obsolete system call %08x.\n",
363 current
->pid
, current
->comm
, n
);
368 info
.si_signo
= SIGILL
;
370 info
.si_code
= ILL_ILLTRP
;
371 info
.si_addr
= (void __user
*)instruction_pointer(regs
) -
372 (thumb_mode(regs
) ? 2 : 4);
374 force_sig_info(SIGILL
, &info
, current
);
375 die_if_kernel("Oops - bad syscall", regs
, n
);
380 do_cache_op(unsigned long start
, unsigned long end
, int flags
)
382 struct vm_area_struct
*vma
;
384 if (end
< start
|| flags
)
387 vma
= find_vma(current
->active_mm
, start
);
388 if (vma
&& vma
->vm_start
< end
) {
389 if (start
< vma
->vm_start
)
390 start
= vma
->vm_start
;
391 if (end
> vma
->vm_end
)
394 flush_cache_user_range(vma
, start
, end
);
399 * Handle all unrecognised system calls.
400 * 0x9f0000 - 0x9fffff are some more esoteric system calls
402 #define NR(x) ((__ARM_NR_##x) - __ARM_NR_BASE)
403 asmlinkage
int arm_syscall(int no
, struct pt_regs
*regs
)
407 if ((no
>> 16) != 0x9f)
408 return bad_syscall(no
, regs
);
410 switch (no
& 0xffff) {
411 case 0: /* branch through 0 */
412 info
.si_signo
= SIGSEGV
;
414 info
.si_code
= SEGV_MAPERR
;
417 force_sig_info(SIGSEGV
, &info
, current
);
419 die_if_kernel("branch through zero", regs
, 0);
422 case NR(breakpoint
): /* SWI BREAK_POINT */
423 regs
->ARM_pc
-= thumb_mode(regs
) ? 2 : 4;
424 ptrace_break(current
, regs
);
428 * Flush a region from virtual address 'r0' to virtual address 'r1'
429 * _exclusive_. There is no alignment requirement on either address;
430 * user space does not need to know the hardware cache layout.
432 * r2 contains flags. It should ALWAYS be passed as ZERO until it
433 * is defined to be something else. For now we ignore it, but may
434 * the fires of hell burn in your belly if you break this rule. ;)
436 * (at a later date, we may want to allow this call to not flush
437 * various aspects of the cache. Passing '0' will guarantee that
438 * everything necessary gets flushed to maintain consistency in
439 * the specified region).
442 do_cache_op(regs
->ARM_r0
, regs
->ARM_r1
, regs
->ARM_r2
);
446 if (!(elf_hwcap
& HWCAP_26BIT
))
448 regs
->ARM_cpsr
&= ~MODE32_BIT
;
452 if (!(elf_hwcap
& HWCAP_26BIT
))
454 regs
->ARM_cpsr
|= MODE32_BIT
;
458 /* Calls 9f00xx..9f07ff are defined to return -ENOSYS
459 if not implemented, rather than raising SIGILL. This
460 way the calling program can gracefully determine whether
461 a feature is supported. */
466 #ifdef CONFIG_DEBUG_USER
468 * experience shows that these seem to indicate that
469 * something catastrophic has happened
471 if (user_debug
& UDBG_SYSCALL
) {
472 printk("[%d] %s: arm syscall %d\n",
473 current
->pid
, current
->comm
, no
);
475 if (user_mode(regs
)) {
477 c_backtrace(regs
->ARM_fp
, processor_mode(regs
));
481 info
.si_signo
= SIGILL
;
483 info
.si_code
= ILL_ILLTRP
;
484 info
.si_addr
= (void __user
*)instruction_pointer(regs
) -
485 (thumb_mode(regs
) ? 2 : 4);
487 force_sig_info(SIGILL
, &info
, current
);
488 die_if_kernel("Oops - bad syscall(2)", regs
, no
);
492 void __bad_xchg(volatile void *ptr
, int size
)
494 printk("xchg: bad data size: pc 0x%p, ptr 0x%p, size %d\n",
495 __builtin_return_address(0), ptr
, size
);
498 EXPORT_SYMBOL(__bad_xchg
);
501 * A data abort trap was taken, but we did not handle the instruction.
502 * Try to abort the user program, or panic if it was the kernel.
505 baddataabort(int code
, unsigned long instr
, struct pt_regs
*regs
)
507 unsigned long addr
= instruction_pointer(regs
);
510 #ifdef CONFIG_DEBUG_USER
511 if (user_debug
& UDBG_BADABORT
) {
512 printk(KERN_ERR
"[%d] %s: bad data abort: code %d instr 0x%08lx\n",
513 current
->pid
, current
->comm
, code
, instr
);
515 show_pte(current
->mm
, addr
);
519 info
.si_signo
= SIGILL
;
521 info
.si_code
= ILL_ILLOPC
;
522 info
.si_addr
= (void __user
*)addr
;
524 force_sig_info(SIGILL
, &info
, current
);
525 die_if_kernel("unknown data abort code", regs
, instr
);
528 volatile void __bug(const char *file
, int line
, void *data
)
530 printk(KERN_CRIT
"kernel BUG at %s:%d!", file
, line
);
532 printk(" - extra data = %p", data
);
536 EXPORT_SYMBOL(__bug
);
538 void __readwrite_bug(const char *fn
)
540 printk("%s called, but not implemented", fn
);
543 EXPORT_SYMBOL(__readwrite_bug
);
545 void __pte_error(const char *file
, int line
, unsigned long val
)
547 printk("%s:%d: bad pte %08lx.\n", file
, line
, val
);
550 void __pmd_error(const char *file
, int line
, unsigned long val
)
552 printk("%s:%d: bad pmd %08lx.\n", file
, line
, val
);
555 void __pgd_error(const char *file
, int line
, unsigned long val
)
557 printk("%s:%d: bad pgd %08lx.\n", file
, line
, val
);
560 asmlinkage
void __div0(void)
562 printk("Division by zero in kernel.\n");
565 EXPORT_SYMBOL(__div0
);
571 /* if that doesn't kill us, halt */
572 panic("Oops failed to kill thread");
574 EXPORT_SYMBOL(abort
);
576 void __init
trap_init(void)
578 extern void __trap_init(unsigned long);
579 unsigned long base
= vectors_base();
582 flush_icache_range(base
, base
+ PAGE_SIZE
);
584 printk(KERN_DEBUG
"Relocating machine vectors to 0x%08lx\n",
586 modify_domain(DOMAIN_USER
, DOMAIN_CLIENT
);