2 * Copyright (C) 2004-2006 Atmel Corporation
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.
9 #include <linux/sched.h>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/kallsyms.h>
13 #include <linux/notifier.h>
15 #include <asm/traps.h>
16 #include <asm/sysreg.h>
17 #include <asm/addrspace.h>
19 #include <asm/mmu_context.h>
20 #include <asm/uaccess.h>
22 static void dump_mem(const char *str
, unsigned long bottom
, unsigned long top
)
27 printk("%s(0x%08lx to 0x%08lx)\n", str
, bottom
, top
);
29 for (p
= bottom
& ~31; p
< top
; ) {
30 printk("%04lx: ", p
& 0xffff);
32 for (i
= 0; i
< 8; i
++, p
+= 4) {
35 if (p
< bottom
|| p
>= top
)
38 if (__get_user(val
, (unsigned int __user
*)p
)) {
52 #ifdef CONFIG_FRAME_POINTER
53 static inline void __show_trace(struct task_struct
*tsk
, unsigned long *sp
,
56 unsigned long __user
*fp
;
57 unsigned long __user
*last_fp
= NULL
;
60 fp
= (unsigned long __user
*)regs
->r7
;
61 } else if (tsk
== current
) {
62 register unsigned long __user
*real_fp
__asm__("r7");
65 fp
= (unsigned long __user
*)tsk
->thread
.cpu_context
.r7
;
69 * Walk the stack until (a) we get an exception, (b) the frame
70 * pointer becomes zero, or (c) the frame pointer gets stuck
73 while (fp
&& fp
!= last_fp
) {
74 unsigned long lr
, new_fp
= 0;
77 if (__get_user(lr
, fp
))
79 if (fp
&& __get_user(new_fp
, fp
+ 1))
81 fp
= (unsigned long __user
*)new_fp
;
83 printk(" [<%08lx>] ", lr
);
84 print_symbol("%s\n", lr
);
89 static inline void __show_trace(struct task_struct
*tsk
, unsigned long *sp
,
94 while (!kstack_end(sp
)) {
96 if (kernel_text_address(addr
)) {
97 printk(" [<%08lx>] ", addr
);
98 print_symbol("%s\n", addr
);
104 void show_trace(struct task_struct
*tsk
, unsigned long *sp
,
105 struct pt_regs
*regs
)
108 (((regs
->sr
& MODE_MASK
) == MODE_EXCEPTION
) ||
109 ((regs
->sr
& MODE_MASK
) == MODE_USER
)))
112 printk ("Call trace:");
113 #ifdef CONFIG_KALLSYMS
117 __show_trace(tsk
, sp
, regs
);
121 void show_stack(struct task_struct
*tsk
, unsigned long *sp
)
128 if (tsk
== current
) {
129 register unsigned long *real_sp
__asm__("sp");
132 sp
= (unsigned long *)tsk
->thread
.cpu_context
.ksp
;
136 stack
= (unsigned long)sp
;
137 dump_mem("Stack: ", stack
,
138 THREAD_SIZE
+ (unsigned long)tsk
->thread_info
);
139 show_trace(tsk
, sp
, NULL
);
142 void dump_stack(void)
144 show_stack(NULL
, NULL
);
146 EXPORT_SYMBOL(dump_stack
);
148 ATOMIC_NOTIFIER_HEAD(avr32_die_chain
);
150 int register_die_notifier(struct notifier_block
*nb
)
152 pr_debug("register_die_notifier: %p\n", nb
);
154 return atomic_notifier_chain_register(&avr32_die_chain
, nb
);
156 EXPORT_SYMBOL(register_die_notifier
);
158 int unregister_die_notifier(struct notifier_block
*nb
)
160 return atomic_notifier_chain_unregister(&avr32_die_chain
, nb
);
162 EXPORT_SYMBOL(unregister_die_notifier
);
164 static DEFINE_SPINLOCK(die_lock
);
166 void __die(const char *str
, struct pt_regs
*regs
, unsigned long err
,
167 const char *file
, const char *func
, unsigned long line
)
169 struct task_struct
*tsk
= current
;
170 static int die_counter
;
173 spin_lock_irq(&die_lock
);
176 printk(KERN_ALERT
"%s", str
);
178 printk(" in %s:%s, line %ld", file
, func
, line
);
179 printk("[#%d]:\n", ++die_counter
);
182 printk("Process %s (pid: %d, stack limit = 0x%p)\n",
183 tsk
->comm
, tsk
->pid
, tsk
->thread_info
+ 1);
185 if (!user_mode(regs
) || in_interrupt()) {
186 dump_mem("Stack: ", regs
->sp
,
187 THREAD_SIZE
+ (unsigned long)tsk
->thread_info
);
191 spin_unlock_irq(&die_lock
);
195 void __die_if_kernel(const char *str
, struct pt_regs
*regs
, unsigned long err
,
196 const char *file
, const char *func
, unsigned long line
)
198 if (!user_mode(regs
))
199 __die(str
, regs
, err
, file
, func
, line
);
202 asmlinkage
void do_nmi(unsigned long ecr
, struct pt_regs
*regs
)
204 #ifdef CONFIG_SUBARCH_AVR32B
206 * The exception entry always saves RSR_EX. For NMI, this is
207 * wrong; it should be RSR_NMI
209 regs
->sr
= sysreg_read(RSR_NMI
);
212 printk("NMI taken!!!!\n");
213 die("NMI", regs
, ecr
);
217 asmlinkage
void do_critical_exception(unsigned long ecr
, struct pt_regs
*regs
)
219 printk("Unable to handle critical exception %lu at pc = %08lx!\n",
221 die("Oops", regs
, ecr
);
225 asmlinkage
void do_address_exception(unsigned long ecr
, struct pt_regs
*regs
)
229 die_if_kernel("Oops: Address exception in kernel mode", regs
, ecr
);
232 if (ecr
== ECR_ADDR_ALIGN_X
)
233 pr_debug("Instruction Address Exception at pc = %08lx\n",
235 else if (ecr
== ECR_ADDR_ALIGN_R
)
236 pr_debug("Data Address Exception (Read) at pc = %08lx\n",
238 else if (ecr
== ECR_ADDR_ALIGN_W
)
239 pr_debug("Data Address Exception (Write) at pc = %08lx\n",
247 info
.si_signo
= SIGBUS
;
249 info
.si_code
= BUS_ADRALN
;
250 info
.si_addr
= (void __user
*)regs
->pc
;
252 force_sig_info(SIGBUS
, &info
, current
);
255 /* This way of handling undefined instructions is stolen from ARM */
256 static LIST_HEAD(undef_hook
);
257 static spinlock_t undef_lock
= SPIN_LOCK_UNLOCKED
;
259 void register_undef_hook(struct undef_hook
*hook
)
261 spin_lock_irq(&undef_lock
);
262 list_add(&hook
->node
, &undef_hook
);
263 spin_unlock_irq(&undef_lock
);
266 void unregister_undef_hook(struct undef_hook
*hook
)
268 spin_lock_irq(&undef_lock
);
269 list_del(&hook
->node
);
270 spin_unlock_irq(&undef_lock
);
273 static int do_cop_absent(u32 insn
)
277 if ( (insn
& 0xfdf00000) == 0xf1900000 )
281 cop_nr
= (insn
>> 13) & 0x7;
283 /* Try enabling the coprocessor */
284 cpucr
= sysreg_read(CPUCR
);
285 cpucr
|= (1 << (24 + cop_nr
));
286 sysreg_write(CPUCR
, cpucr
);
288 cpucr
= sysreg_read(CPUCR
);
289 if ( !(cpucr
& (1 << (24 + cop_nr
))) ){
290 printk("Coprocessor #%i not found!\n", cop_nr
);
298 #ifdef CONFIG_DEBUG_BUGVERBOSE
299 static inline void do_bug_verbose(struct pt_regs
*regs
, u32 insn
)
305 if (__get_user(line
, (u16 __user
*)(regs
->pc
+ 2)))
307 if (__get_user(file
, (char * __user
*)(regs
->pc
+ 4))
308 || (unsigned long)file
< PAGE_OFFSET
309 || __get_user(c
, file
))
310 file
= "<bad filename>";
312 printk(KERN_ALERT
"kernel BUG at %s:%d!\n", file
, line
);
315 static inline void do_bug_verbose(struct pt_regs
*regs
, u32 insn
)
322 asmlinkage
void do_illegal_opcode(unsigned long ecr
, struct pt_regs
*regs
)
325 struct undef_hook
*hook
;
329 if (!user_mode(regs
))
334 pc
= (void __user
*)instruction_pointer(regs
);
335 if (__get_user(insn
, (u32 __user
*)pc
))
338 if (ecr
== ECR_COPROC_ABSENT
) {
339 if (do_cop_absent(insn
) == 0)
343 spin_lock_irq(&undef_lock
);
344 list_for_each_entry(hook
, &undef_hook
, node
) {
345 if ((insn
& hook
->insn_mask
) == hook
->insn_val
) {
346 if (hook
->fn(regs
, insn
) == 0) {
347 spin_unlock_irq(&undef_lock
);
352 spin_unlock_irq(&undef_lock
);
357 printk("Illegal instruction at pc = %08lx\n", regs
->pc
);
358 if (regs
->pc
< TASK_SIZE
) {
359 unsigned long ptbr
, pgd
, pte
, *p
;
361 ptbr
= sysreg_read(PTBR
);
362 p
= (unsigned long *)ptbr
;
363 pgd
= p
[regs
->pc
>> 22];
364 p
= (unsigned long *)((pgd
& 0x1ffff000) | 0x80000000);
365 pte
= p
[(regs
->pc
>> 12) & 0x3ff];
366 printk("page table: 0x%08lx -> 0x%08lx -> 0x%08lx\n", ptbr
, pgd
, pte
);
370 info
.si_signo
= SIGILL
;
372 info
.si_addr
= (void __user
*)regs
->pc
;
374 case ECR_ILLEGAL_OPCODE
:
375 case ECR_UNIMPL_INSTRUCTION
:
376 info
.si_code
= ILL_ILLOPC
;
378 case ECR_PRIVILEGE_VIOLATION
:
379 info
.si_code
= ILL_PRVOPC
;
381 case ECR_COPROC_ABSENT
:
382 info
.si_code
= ILL_COPROC
;
388 force_sig_info(SIGILL
, &info
, current
);
393 if (__kernel_text_address(instruction_pointer(regs
))) {
394 insn
= *(u16
*)instruction_pointer(regs
);
395 if (insn
== AVR32_BUG_OPCODE
) {
396 do_bug_verbose(regs
, insn
);
397 die("Kernel BUG", regs
, 0);
403 die("Oops: Illegal instruction in kernel code", regs
, ecr
);
406 asmlinkage
void do_fpe(unsigned long ecr
, struct pt_regs
*regs
)
410 printk("Floating-point exception at pc = %08lx\n", regs
->pc
);
412 /* We have no FPU... */
413 info
.si_signo
= SIGILL
;
415 info
.si_addr
= (void __user
*)regs
->pc
;
416 info
.si_code
= ILL_COPROC
;
418 force_sig_info(SIGILL
, &info
, current
);
422 void __init
trap_init(void)