traps.c 5.9 KB
Newer Older
H
Haavard Skinnemoen 已提交
1 2 3 4 5 6 7
/*
 * Copyright (C) 2004-2006 Atmel Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
8 9

#include <linux/bug.h>
10
#include <linux/hardirq.h>
H
Haavard Skinnemoen 已提交
11 12
#include <linux/init.h>
#include <linux/kallsyms.h>
H
Haavard Skinnemoen 已提交
13
#include <linux/kdebug.h>
14
#include <linux/module.h>
H
Haavard Skinnemoen 已提交
15
#include <linux/notifier.h>
16 17
#include <linux/sched.h>
#include <linux/uaccess.h>
H
Haavard Skinnemoen 已提交
18 19 20

#include <asm/addrspace.h>
#include <asm/mmu_context.h>
21 22 23
#include <asm/ocd.h>
#include <asm/sysreg.h>
#include <asm/traps.h>
H
Haavard Skinnemoen 已提交
24 25 26

static DEFINE_SPINLOCK(die_lock);

27
void NORET_TYPE die(const char *str, struct pt_regs *regs, long err)
H
Haavard Skinnemoen 已提交
28 29 30 31 32 33 34
{
	static int die_counter;

	console_verbose();
	spin_lock_irq(&die_lock);
	bust_spinlocks(1);

35 36 37 38 39 40 41 42 43
	printk(KERN_ALERT "Oops: %s, sig: %ld [#%d]\n" KERN_EMERG,
	       str, err, ++die_counter);
#ifdef CONFIG_PREEMPT
	printk("PREEMPT ");
#endif
#ifdef CONFIG_FRAME_POINTER
	printk("FRAME_POINTER ");
#endif
	if (current_cpu_data.features & AVR32_FEATURE_OCD) {
44
		unsigned long did = ocd_read(DID);
45 46 47 48 49 50 51 52 53 54
		printk("chip: 0x%03lx:0x%04lx rev %lu\n",
		       (did >> 1) & 0x7ff,
		       (did >> 12) & 0x7fff,
		       (did >> 28) & 0xf);
	} else {
		printk("cpu: arch %u r%u / core %u r%u\n",
		       current_cpu_data.arch_type,
		       current_cpu_data.arch_revision,
		       current_cpu_data.cpu_type,
		       current_cpu_data.cpu_revision);
H
Haavard Skinnemoen 已提交
55 56
	}

57 58 59
	print_modules();
	show_regs_log_lvl(regs, KERN_EMERG);
	show_stack_log_lvl(current, regs->sp, regs, KERN_EMERG);
H
Haavard Skinnemoen 已提交
60
	bust_spinlocks(0);
61
	add_taint(TAINT_DIE);
H
Haavard Skinnemoen 已提交
62
	spin_unlock_irq(&die_lock);
63 64 65 66 67 68 69 70

	if (in_interrupt())
		panic("Fatal exception in interrupt");

	if (panic_on_oops)
		panic("Fatal exception");

	do_exit(err);
H
Haavard Skinnemoen 已提交
71 72
}

73 74
void _exception(long signr, struct pt_regs *regs, int code,
		unsigned long addr)
H
Haavard Skinnemoen 已提交
75
{
76 77
	siginfo_t info;

H
Haavard Skinnemoen 已提交
78
	if (!user_mode(regs))
79 80 81 82 83 84 85
		die("Unhandled exception in kernel mode", regs, signr);

	memset(&info, 0, sizeof(info));
	info.si_signo = signr;
	info.si_code = code;
	info.si_addr = (void __user *)addr;
	force_sig_info(signr, &info, current);
H
Haavard Skinnemoen 已提交
86 87

	/*
88 89 90 91 92
	 * Init gets no signals that it doesn't have a handler for.
	 * That's all very well, but if it has caused a synchronous
	 * exception and we ignore the resulting signal, it will just
	 * generate the same exception over and over again and we get
	 * nowhere.  Better to kill it and let the kernel panic.
H
Haavard Skinnemoen 已提交
93
	 */
94
	if (is_global_init(current)) {
95 96 97 98 99 100 101 102 103 104 105 106 107 108
		__sighandler_t handler;

		spin_lock_irq(&current->sighand->siglock);
		handler = current->sighand->action[signr-1].sa.sa_handler;
		spin_unlock_irq(&current->sighand->siglock);
		if (handler == SIG_DFL) {
			/* init has generated a synchronous exception
			   and it doesn't have a handler for the signal */
			printk(KERN_CRIT "init has generated signal %ld "
			       "but has no handler for it\n", signr);
			do_exit(signr);
		}
	}
}
H
Haavard Skinnemoen 已提交
109

110 111
asmlinkage void do_nmi(unsigned long ecr, struct pt_regs *regs)
{
H
Haavard Skinnemoen 已提交
112 113 114 115 116 117 118 119
	int ret;

	nmi_enter();

	ret = notify_die(DIE_NMI, "NMI", regs, 0, ecr, SIGINT);
	switch (ret) {
	case NOTIFY_OK:
	case NOTIFY_STOP:
120
		break;
H
Haavard Skinnemoen 已提交
121 122 123
	case NOTIFY_BAD:
		die("Fatal Non-Maskable Interrupt", regs, SIGINT);
	default:
124 125
		printk(KERN_ALERT "Got NMI, but nobody cared. Disabling...\n");
		nmi_disable();
H
Haavard Skinnemoen 已提交
126 127
		break;
	}
128
	nmi_exit();
H
Haavard Skinnemoen 已提交
129 130 131 132
}

asmlinkage void do_critical_exception(unsigned long ecr, struct pt_regs *regs)
{
133
	die("Critical exception", regs, SIGKILL);
H
Haavard Skinnemoen 已提交
134 135 136 137
}

asmlinkage void do_address_exception(unsigned long ecr, struct pt_regs *regs)
{
138
	_exception(SIGBUS, regs, BUS_ADRALN, regs->pc);
H
Haavard Skinnemoen 已提交
139 140 141 142
}

/* This way of handling undefined instructions is stolen from ARM */
static LIST_HEAD(undef_hook);
143
static DEFINE_SPINLOCK(undef_lock);
H
Haavard Skinnemoen 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

void register_undef_hook(struct undef_hook *hook)
{
	spin_lock_irq(&undef_lock);
	list_add(&hook->node, &undef_hook);
	spin_unlock_irq(&undef_lock);
}

void unregister_undef_hook(struct undef_hook *hook)
{
	spin_lock_irq(&undef_lock);
	list_del(&hook->node);
	spin_unlock_irq(&undef_lock);
}

static int do_cop_absent(u32 insn)
{
	int cop_nr;
	u32 cpucr;
163 164

	if ((insn & 0xfdf00000) == 0xf1900000)
H
Haavard Skinnemoen 已提交
165 166 167 168 169 170 171 172 173 174 175
		/* LDC0 */
		cop_nr = 0;
	else
		cop_nr = (insn >> 13) & 0x7;

	/* Try enabling the coprocessor */
	cpucr = sysreg_read(CPUCR);
	cpucr |= (1 << (24 + cop_nr));
	sysreg_write(CPUCR, cpucr);

	cpucr = sysreg_read(CPUCR);
176 177
	if (!(cpucr & (1 << (24 + cop_nr))))
		return -ENODEV;
H
Haavard Skinnemoen 已提交
178 179 180 181

	return 0;
}

182
#ifdef CONFIG_BUG
183
int is_valid_bugaddr(unsigned long pc)
H
Haavard Skinnemoen 已提交
184
{
185 186 187 188 189 190
	unsigned short opcode;

	if (pc < PAGE_OFFSET)
		return 0;
	if (probe_kernel_address((u16 *)pc, opcode))
		return 0;
H
Haavard Skinnemoen 已提交
191

192
	return opcode == AVR32_BUG_OPCODE;
H
Haavard Skinnemoen 已提交
193
}
194
#endif
H
Haavard Skinnemoen 已提交
195 196 197 198 199 200

asmlinkage void do_illegal_opcode(unsigned long ecr, struct pt_regs *regs)
{
	u32 insn;
	struct undef_hook *hook;
	void __user *pc;
201
	long code;
H
Haavard Skinnemoen 已提交
202

203
#ifdef CONFIG_BUG
204 205 206
	if (!user_mode(regs) && (ecr == ECR_ILLEGAL_OPCODE)) {
		enum bug_trap_type type;

207
		type = report_bug(regs->pc, regs);
208 209 210 211 212 213 214 215 216 217
		switch (type) {
		case BUG_TRAP_TYPE_NONE:
			break;
		case BUG_TRAP_TYPE_WARN:
			regs->pc += 2;
			return;
		case BUG_TRAP_TYPE_BUG:
			die("Kernel BUG", regs, SIGKILL);
		}
	}
218
#endif
H
Haavard Skinnemoen 已提交
219 220 221

	local_irq_enable();

222 223 224 225
	if (user_mode(regs)) {
		pc = (void __user *)instruction_pointer(regs);
		if (get_user(insn, (u32 __user *)pc))
			goto invalid_area;
H
Haavard Skinnemoen 已提交
226

227
		if (ecr == ECR_COPROC_ABSENT && !do_cop_absent(insn))
H
Haavard Skinnemoen 已提交
228 229
			return;

230 231 232 233 234 235 236
		spin_lock_irq(&undef_lock);
		list_for_each_entry(hook, &undef_hook, node) {
			if ((insn & hook->insn_mask) == hook->insn_val) {
				if (hook->fn(regs, insn) == 0) {
					spin_unlock_irq(&undef_lock);
					return;
				}
H
Haavard Skinnemoen 已提交
237 238
			}
		}
239
		spin_unlock_irq(&undef_lock);
H
Haavard Skinnemoen 已提交
240 241 242 243
	}

	switch (ecr) {
	case ECR_PRIVILEGE_VIOLATION:
244
		code = ILL_PRVOPC;
H
Haavard Skinnemoen 已提交
245 246
		break;
	case ECR_COPROC_ABSENT:
247
		code = ILL_COPROC;
H
Haavard Skinnemoen 已提交
248 249
		break;
	default:
250 251
		code = ILL_ILLOPC;
		break;
H
Haavard Skinnemoen 已提交
252 253
	}

254
	_exception(SIGILL, regs, code, regs->pc);
H
Haavard Skinnemoen 已提交
255 256
	return;

257 258
invalid_area:
	_exception(SIGSEGV, regs, SEGV_MAPERR, regs->pc);
H
Haavard Skinnemoen 已提交
259 260 261 262
}

asmlinkage void do_fpe(unsigned long ecr, struct pt_regs *regs)
{
263 264
	/* We have no FPU yet */
	_exception(SIGILL, regs, ILL_COPROC, regs->pc);
H
Haavard Skinnemoen 已提交
265 266 267 268 269 270 271
}


void __init trap_init(void)
{

}