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
);
59 spin_unlock_irq(&die_lock
);
62 panic("Fatal exception in interrupt");
65 panic("Fatal exception");
70 void _exception(long signr
, struct pt_regs
*regs
, int code
,
76 die("Unhandled exception in kernel mode", regs
, signr
);
78 memset(&info
, 0, sizeof(info
));
79 info
.si_signo
= signr
;
81 info
.si_addr
= (void __user
*)addr
;
82 force_sig_info(signr
, &info
, current
);
85 * Init gets no signals that it doesn't have a handler for.
86 * That's all very well, but if it has caused a synchronous
87 * exception and we ignore the resulting signal, it will just
88 * generate the same exception over and over again and we get
89 * nowhere. Better to kill it and let the kernel panic.
91 if (is_init(current
)) {
92 __sighandler_t handler
;
94 spin_lock_irq(¤t
->sighand
->siglock
);
95 handler
= current
->sighand
->action
[signr
-1].sa
.sa_handler
;
96 spin_unlock_irq(¤t
->sighand
->siglock
);
97 if (handler
== SIG_DFL
) {
98 /* init has generated a synchronous exception
99 and it doesn't have a handler for the signal */
100 printk(KERN_CRIT
"init has generated signal %ld "
101 "but has no handler for it\n", signr
);
107 asmlinkage
void do_nmi(unsigned long ecr
, struct pt_regs
*regs
)
109 printk(KERN_ALERT
"Got Non-Maskable Interrupt, dumping regs\n");
110 show_regs_log_lvl(regs
, KERN_ALERT
);
111 show_stack_log_lvl(current
, regs
->sp
, regs
, KERN_ALERT
);
114 asmlinkage
void do_critical_exception(unsigned long ecr
, struct pt_regs
*regs
)
116 die("Critical exception", regs
, SIGKILL
);
119 asmlinkage
void do_address_exception(unsigned long ecr
, struct pt_regs
*regs
)
121 _exception(SIGBUS
, regs
, BUS_ADRALN
, regs
->pc
);
124 /* This way of handling undefined instructions is stolen from ARM */
125 static LIST_HEAD(undef_hook
);
126 static DEFINE_SPINLOCK(undef_lock
);
128 void register_undef_hook(struct undef_hook
*hook
)
130 spin_lock_irq(&undef_lock
);
131 list_add(&hook
->node
, &undef_hook
);
132 spin_unlock_irq(&undef_lock
);
135 void unregister_undef_hook(struct undef_hook
*hook
)
137 spin_lock_irq(&undef_lock
);
138 list_del(&hook
->node
);
139 spin_unlock_irq(&undef_lock
);
142 static int do_cop_absent(u32 insn
)
147 if ((insn
& 0xfdf00000) == 0xf1900000)
151 cop_nr
= (insn
>> 13) & 0x7;
153 /* Try enabling the coprocessor */
154 cpucr
= sysreg_read(CPUCR
);
155 cpucr
|= (1 << (24 + cop_nr
));
156 sysreg_write(CPUCR
, cpucr
);
158 cpucr
= sysreg_read(CPUCR
);
159 if (!(cpucr
& (1 << (24 + cop_nr
))))
165 int is_valid_bugaddr(unsigned long pc
)
167 unsigned short opcode
;
169 if (pc
< PAGE_OFFSET
)
171 if (probe_kernel_address((u16
*)pc
, opcode
))
174 return opcode
== AVR32_BUG_OPCODE
;
177 asmlinkage
void do_illegal_opcode(unsigned long ecr
, struct pt_regs
*regs
)
180 struct undef_hook
*hook
;
184 if (!user_mode(regs
) && (ecr
== ECR_ILLEGAL_OPCODE
)) {
185 enum bug_trap_type type
;
187 type
= report_bug(regs
->pc
);
189 case BUG_TRAP_TYPE_NONE
:
191 case BUG_TRAP_TYPE_WARN
:
194 case BUG_TRAP_TYPE_BUG
:
195 die("Kernel BUG", regs
, SIGKILL
);
201 if (user_mode(regs
)) {
202 pc
= (void __user
*)instruction_pointer(regs
);
203 if (get_user(insn
, (u32 __user
*)pc
))
206 if (ecr
== ECR_COPROC_ABSENT
&& !do_cop_absent(insn
))
209 spin_lock_irq(&undef_lock
);
210 list_for_each_entry(hook
, &undef_hook
, node
) {
211 if ((insn
& hook
->insn_mask
) == hook
->insn_val
) {
212 if (hook
->fn(regs
, insn
) == 0) {
213 spin_unlock_irq(&undef_lock
);
218 spin_unlock_irq(&undef_lock
);
222 case ECR_PRIVILEGE_VIOLATION
:
225 case ECR_COPROC_ABSENT
:
233 _exception(SIGILL
, regs
, code
, regs
->pc
);
237 _exception(SIGSEGV
, regs
, SEGV_MAPERR
, regs
->pc
);
240 asmlinkage
void do_fpe(unsigned long ecr
, struct pt_regs
*regs
)
242 /* We have no FPU yet */
243 _exception(SIGILL
, regs
, ILL_COPROC
, regs
->pc
);
247 void __init
trap_init(void)