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.
10 #include <linux/init.h>
11 #include <linux/kallsyms.h>
12 #include <linux/module.h>
13 #include <linux/notifier.h>
14 #include <linux/sched.h>
15 #include <linux/uaccess.h>
17 #include <asm/addrspace.h>
18 #include <asm/mmu_context.h>
20 #include <asm/sysreg.h>
21 #include <asm/traps.h>
23 static DEFINE_SPINLOCK(die_lock);
25 void NORET_TYPE die(const char *str, struct pt_regs *regs, long err)
27 static int die_counter;
30 spin_lock_irq(&die_lock);
33 printk(KERN_ALERT "Oops: %s, sig: %ld [#%d]\n" KERN_EMERG,
34 str, err, ++die_counter);
38 #ifdef CONFIG_FRAME_POINTER
39 printk("FRAME_POINTER ");
41 if (current_cpu_data.features & AVR32_FEATURE_OCD) {
42 unsigned long did = __mfdr(DBGREG_DID);
43 printk("chip: 0x%03lx:0x%04lx rev %lu\n",
48 printk("cpu: arch %u r%u / core %u r%u\n",
49 current_cpu_data.arch_type,
50 current_cpu_data.arch_revision,
51 current_cpu_data.cpu_type,
52 current_cpu_data.cpu_revision);
56 show_regs_log_lvl(regs, KERN_EMERG);
57 show_stack_log_lvl(current, regs->sp, regs, KERN_EMERG);
60 spin_unlock_irq(&die_lock);
63 panic("Fatal exception in interrupt");
66 panic("Fatal exception");
71 void _exception(long signr, struct pt_regs *regs, int code,
77 die("Unhandled exception in kernel mode", regs, signr);
79 memset(&info, 0, sizeof(info));
80 info.si_signo = signr;
82 info.si_addr = (void __user *)addr;
83 force_sig_info(signr, &info, current);
86 * Init gets no signals that it doesn't have a handler for.
87 * That's all very well, but if it has caused a synchronous
88 * exception and we ignore the resulting signal, it will just
89 * generate the same exception over and over again and we get
90 * nowhere. Better to kill it and let the kernel panic.
92 if (is_global_init(current)) {
93 __sighandler_t handler;
95 spin_lock_irq(¤t->sighand->siglock);
96 handler = current->sighand->action[signr-1].sa.sa_handler;
97 spin_unlock_irq(¤t->sighand->siglock);
98 if (handler == SIG_DFL) {
99 /* init has generated a synchronous exception
100 and it doesn't have a handler for the signal */
101 printk(KERN_CRIT "init has generated signal %ld "
102 "but has no handler for it\n", signr);
108 asmlinkage void do_nmi(unsigned long ecr, struct pt_regs *regs)
110 printk(KERN_ALERT "Got Non-Maskable Interrupt, dumping regs\n");
111 show_regs_log_lvl(regs, KERN_ALERT);
112 show_stack_log_lvl(current, regs->sp, regs, KERN_ALERT);
115 asmlinkage void do_critical_exception(unsigned long ecr, struct pt_regs *regs)
117 die("Critical exception", regs, SIGKILL);
120 asmlinkage void do_address_exception(unsigned long ecr, struct pt_regs *regs)
122 _exception(SIGBUS, regs, BUS_ADRALN, regs->pc);
125 /* This way of handling undefined instructions is stolen from ARM */
126 static LIST_HEAD(undef_hook);
127 static DEFINE_SPINLOCK(undef_lock);
129 void register_undef_hook(struct undef_hook *hook)
131 spin_lock_irq(&undef_lock);
132 list_add(&hook->node, &undef_hook);
133 spin_unlock_irq(&undef_lock);
136 void unregister_undef_hook(struct undef_hook *hook)
138 spin_lock_irq(&undef_lock);
139 list_del(&hook->node);
140 spin_unlock_irq(&undef_lock);
143 static int do_cop_absent(u32 insn)
148 if ((insn & 0xfdf00000) == 0xf1900000)
152 cop_nr = (insn >> 13) & 0x7;
154 /* Try enabling the coprocessor */
155 cpucr = sysreg_read(CPUCR);
156 cpucr |= (1 << (24 + cop_nr));
157 sysreg_write(CPUCR, cpucr);
159 cpucr = sysreg_read(CPUCR);
160 if (!(cpucr & (1 << (24 + cop_nr))))
166 int is_valid_bugaddr(unsigned long pc)
168 unsigned short opcode;
170 if (pc < PAGE_OFFSET)
172 if (probe_kernel_address((u16 *)pc, opcode))
175 return opcode == AVR32_BUG_OPCODE;
178 asmlinkage void do_illegal_opcode(unsigned long ecr, struct pt_regs *regs)
181 struct undef_hook *hook;
185 if (!user_mode(regs) && (ecr == ECR_ILLEGAL_OPCODE)) {
186 enum bug_trap_type type;
188 type = report_bug(regs->pc, regs);
190 case BUG_TRAP_TYPE_NONE:
192 case BUG_TRAP_TYPE_WARN:
195 case BUG_TRAP_TYPE_BUG:
196 die("Kernel BUG", regs, SIGKILL);
202 if (user_mode(regs)) {
203 pc = (void __user *)instruction_pointer(regs);
204 if (get_user(insn, (u32 __user *)pc))
207 if (ecr == ECR_COPROC_ABSENT && !do_cop_absent(insn))
210 spin_lock_irq(&undef_lock);
211 list_for_each_entry(hook, &undef_hook, node) {
212 if ((insn & hook->insn_mask) == hook->insn_val) {
213 if (hook->fn(regs, insn) == 0) {
214 spin_unlock_irq(&undef_lock);
219 spin_unlock_irq(&undef_lock);
223 case ECR_PRIVILEGE_VIOLATION:
226 case ECR_COPROC_ABSENT:
234 _exception(SIGILL, regs, code, regs->pc);
238 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->pc);
241 asmlinkage void do_fpe(unsigned long ecr, struct pt_regs *regs)
243 /* We have no FPU yet */
244 _exception(SIGILL, regs, ILL_COPROC, regs->pc);
248 void __init trap_init(void)