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/kdebug.h>
13 #include <linux/module.h>
14 #include <linux/notifier.h>
15 #include <linux/sched.h>
16 #include <linux/uaccess.h>
18 #include <asm/addrspace.h>
19 #include <asm/mmu_context.h>
21 #include <asm/sysreg.h>
22 #include <asm/traps.h>
24 static DEFINE_SPINLOCK(die_lock
);
26 void NORET_TYPE
die(const char *str
, struct pt_regs
*regs
, long err
)
28 static int die_counter
;
31 spin_lock_irq(&die_lock
);
34 printk(KERN_ALERT
"Oops: %s, sig: %ld [#%d]\n" KERN_EMERG
,
35 str
, err
, ++die_counter
);
39 #ifdef CONFIG_FRAME_POINTER
40 printk("FRAME_POINTER ");
42 if (current_cpu_data
.features
& AVR32_FEATURE_OCD
) {
43 unsigned long did
= ocd_read(DID
);
44 printk("chip: 0x%03lx:0x%04lx rev %lu\n",
49 printk("cpu: arch %u r%u / core %u r%u\n",
50 current_cpu_data
.arch_type
,
51 current_cpu_data
.arch_revision
,
52 current_cpu_data
.cpu_type
,
53 current_cpu_data
.cpu_revision
);
57 show_regs_log_lvl(regs
, KERN_EMERG
);
58 show_stack_log_lvl(current
, regs
->sp
, regs
, KERN_EMERG
);
61 spin_unlock_irq(&die_lock
);
64 panic("Fatal exception in interrupt");
67 panic("Fatal exception");
72 void _exception(long signr
, struct pt_regs
*regs
, int code
,
78 die("Unhandled exception in kernel mode", regs
, signr
);
80 memset(&info
, 0, sizeof(info
));
81 info
.si_signo
= signr
;
83 info
.si_addr
= (void __user
*)addr
;
84 force_sig_info(signr
, &info
, current
);
87 * Init gets no signals that it doesn't have a handler for.
88 * That's all very well, but if it has caused a synchronous
89 * exception and we ignore the resulting signal, it will just
90 * generate the same exception over and over again and we get
91 * nowhere. Better to kill it and let the kernel panic.
93 if (is_global_init(current
)) {
94 __sighandler_t handler
;
96 spin_lock_irq(¤t
->sighand
->siglock
);
97 handler
= current
->sighand
->action
[signr
-1].sa
.sa_handler
;
98 spin_unlock_irq(¤t
->sighand
->siglock
);
99 if (handler
== SIG_DFL
) {
100 /* init has generated a synchronous exception
101 and it doesn't have a handler for the signal */
102 printk(KERN_CRIT
"init has generated signal %ld "
103 "but has no handler for it\n", signr
);
109 asmlinkage
void do_nmi(unsigned long ecr
, struct pt_regs
*regs
)
115 ret
= notify_die(DIE_NMI
, "NMI", regs
, 0, ecr
, SIGINT
);
121 die("Fatal Non-Maskable Interrupt", regs
, SIGINT
);
126 printk(KERN_ALERT
"Got NMI, but nobody cared. Disabling...\n");
130 asmlinkage
void do_critical_exception(unsigned long ecr
, struct pt_regs
*regs
)
132 die("Critical exception", regs
, SIGKILL
);
135 asmlinkage
void do_address_exception(unsigned long ecr
, struct pt_regs
*regs
)
137 _exception(SIGBUS
, regs
, BUS_ADRALN
, regs
->pc
);
140 /* This way of handling undefined instructions is stolen from ARM */
141 static LIST_HEAD(undef_hook
);
142 static DEFINE_SPINLOCK(undef_lock
);
144 void register_undef_hook(struct undef_hook
*hook
)
146 spin_lock_irq(&undef_lock
);
147 list_add(&hook
->node
, &undef_hook
);
148 spin_unlock_irq(&undef_lock
);
151 void unregister_undef_hook(struct undef_hook
*hook
)
153 spin_lock_irq(&undef_lock
);
154 list_del(&hook
->node
);
155 spin_unlock_irq(&undef_lock
);
158 static int do_cop_absent(u32 insn
)
163 if ((insn
& 0xfdf00000) == 0xf1900000)
167 cop_nr
= (insn
>> 13) & 0x7;
169 /* Try enabling the coprocessor */
170 cpucr
= sysreg_read(CPUCR
);
171 cpucr
|= (1 << (24 + cop_nr
));
172 sysreg_write(CPUCR
, cpucr
);
174 cpucr
= sysreg_read(CPUCR
);
175 if (!(cpucr
& (1 << (24 + cop_nr
))))
182 int is_valid_bugaddr(unsigned long pc
)
184 unsigned short opcode
;
186 if (pc
< PAGE_OFFSET
)
188 if (probe_kernel_address((u16
*)pc
, opcode
))
191 return opcode
== AVR32_BUG_OPCODE
;
195 asmlinkage
void do_illegal_opcode(unsigned long ecr
, struct pt_regs
*regs
)
198 struct undef_hook
*hook
;
203 if (!user_mode(regs
) && (ecr
== ECR_ILLEGAL_OPCODE
)) {
204 enum bug_trap_type type
;
206 type
= report_bug(regs
->pc
, regs
);
208 case BUG_TRAP_TYPE_NONE
:
210 case BUG_TRAP_TYPE_WARN
:
213 case BUG_TRAP_TYPE_BUG
:
214 die("Kernel BUG", regs
, SIGKILL
);
221 if (user_mode(regs
)) {
222 pc
= (void __user
*)instruction_pointer(regs
);
223 if (get_user(insn
, (u32 __user
*)pc
))
226 if (ecr
== ECR_COPROC_ABSENT
&& !do_cop_absent(insn
))
229 spin_lock_irq(&undef_lock
);
230 list_for_each_entry(hook
, &undef_hook
, node
) {
231 if ((insn
& hook
->insn_mask
) == hook
->insn_val
) {
232 if (hook
->fn(regs
, insn
) == 0) {
233 spin_unlock_irq(&undef_lock
);
238 spin_unlock_irq(&undef_lock
);
242 case ECR_PRIVILEGE_VIOLATION
:
245 case ECR_COPROC_ABSENT
:
253 _exception(SIGILL
, regs
, code
, regs
->pc
);
257 _exception(SIGSEGV
, regs
, SEGV_MAPERR
, regs
->pc
);
260 asmlinkage
void do_fpe(unsigned long ecr
, struct pt_regs
*regs
)
262 /* We have no FPU yet */
263 _exception(SIGILL
, regs
, ILL_COPROC
, regs
->pc
);
267 void __init
trap_init(void)