sched: make early bootup sched_clock() use safer
[wrt350n-kernel.git] / arch / avr32 / kernel / traps.c
blobcf6f686d9b0b4c97647316cd93a890930f100990
1 /*
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.
7 */
9 #include <linux/bug.h>
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>
20 #include <asm/ocd.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;
30 console_verbose();
31 spin_lock_irq(&die_lock);
32 bust_spinlocks(1);
34 printk(KERN_ALERT "Oops: %s, sig: %ld [#%d]\n" KERN_EMERG,
35 str, err, ++die_counter);
36 #ifdef CONFIG_PREEMPT
37 printk("PREEMPT ");
38 #endif
39 #ifdef CONFIG_FRAME_POINTER
40 printk("FRAME_POINTER ");
41 #endif
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",
45 (did >> 1) & 0x7ff,
46 (did >> 12) & 0x7fff,
47 (did >> 28) & 0xf);
48 } else {
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);
56 print_modules();
57 show_regs_log_lvl(regs, KERN_EMERG);
58 show_stack_log_lvl(current, regs->sp, regs, KERN_EMERG);
59 bust_spinlocks(0);
60 add_taint(TAINT_DIE);
61 spin_unlock_irq(&die_lock);
63 if (in_interrupt())
64 panic("Fatal exception in interrupt");
66 if (panic_on_oops)
67 panic("Fatal exception");
69 do_exit(err);
72 void _exception(long signr, struct pt_regs *regs, int code,
73 unsigned long addr)
75 siginfo_t info;
77 if (!user_mode(regs))
78 die("Unhandled exception in kernel mode", regs, signr);
80 memset(&info, 0, sizeof(info));
81 info.si_signo = signr;
82 info.si_code = code;
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(&current->sighand->siglock);
97 handler = current->sighand->action[signr-1].sa.sa_handler;
98 spin_unlock_irq(&current->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);
104 do_exit(signr);
109 asmlinkage void do_nmi(unsigned long ecr, struct pt_regs *regs)
111 int ret;
113 nmi_enter();
115 ret = notify_die(DIE_NMI, "NMI", regs, 0, ecr, SIGINT);
116 switch (ret) {
117 case NOTIFY_OK:
118 case NOTIFY_STOP:
119 return;
120 case NOTIFY_BAD:
121 die("Fatal Non-Maskable Interrupt", regs, SIGINT);
122 default:
123 break;
126 printk(KERN_ALERT "Got NMI, but nobody cared. Disabling...\n");
127 nmi_disable();
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)
160 int cop_nr;
161 u32 cpucr;
163 if ((insn & 0xfdf00000) == 0xf1900000)
164 /* LDC0 */
165 cop_nr = 0;
166 else
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))))
176 return -ENODEV;
178 return 0;
181 int is_valid_bugaddr(unsigned long pc)
183 unsigned short opcode;
185 if (pc < PAGE_OFFSET)
186 return 0;
187 if (probe_kernel_address((u16 *)pc, opcode))
188 return 0;
190 return opcode == AVR32_BUG_OPCODE;
193 asmlinkage void do_illegal_opcode(unsigned long ecr, struct pt_regs *regs)
195 u32 insn;
196 struct undef_hook *hook;
197 void __user *pc;
198 long code;
200 if (!user_mode(regs) && (ecr == ECR_ILLEGAL_OPCODE)) {
201 enum bug_trap_type type;
203 type = report_bug(regs->pc, regs);
204 switch (type) {
205 case BUG_TRAP_TYPE_NONE:
206 break;
207 case BUG_TRAP_TYPE_WARN:
208 regs->pc += 2;
209 return;
210 case BUG_TRAP_TYPE_BUG:
211 die("Kernel BUG", regs, SIGKILL);
215 local_irq_enable();
217 if (user_mode(regs)) {
218 pc = (void __user *)instruction_pointer(regs);
219 if (get_user(insn, (u32 __user *)pc))
220 goto invalid_area;
222 if (ecr == ECR_COPROC_ABSENT && !do_cop_absent(insn))
223 return;
225 spin_lock_irq(&undef_lock);
226 list_for_each_entry(hook, &undef_hook, node) {
227 if ((insn & hook->insn_mask) == hook->insn_val) {
228 if (hook->fn(regs, insn) == 0) {
229 spin_unlock_irq(&undef_lock);
230 return;
234 spin_unlock_irq(&undef_lock);
237 switch (ecr) {
238 case ECR_PRIVILEGE_VIOLATION:
239 code = ILL_PRVOPC;
240 break;
241 case ECR_COPROC_ABSENT:
242 code = ILL_COPROC;
243 break;
244 default:
245 code = ILL_ILLOPC;
246 break;
249 _exception(SIGILL, regs, code, regs->pc);
250 return;
252 invalid_area:
253 _exception(SIGSEGV, regs, SEGV_MAPERR, regs->pc);
256 asmlinkage void do_fpe(unsigned long ecr, struct pt_regs *regs)
258 /* We have no FPU yet */
259 _exception(SIGILL, regs, ILL_COPROC, regs->pc);
263 void __init trap_init(void)