]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/xtensa/kernel/traps.c
xtensa: implement CPU hotplug
[mirror_ubuntu-artful-kernel.git] / arch / xtensa / kernel / traps.c
CommitLineData
5a0015d6
CZ
1/*
2 * arch/xtensa/kernel/traps.c
3 *
4 * Exception handling.
5 *
6 * Derived from code with the following copyrights:
7 * Copyright (C) 1994 - 1999 by Ralf Baechle
8 * Modified for R3000 by Paul M. Antoine, 1995, 1996
9 * Complete output from die() by Ulf Carlsson, 1998
10 * Copyright (C) 1999 Silicon Graphics, Inc.
11 *
12 * Essentially rewritten for the Xtensa architecture port.
13 *
3e4196a5 14 * Copyright (C) 2001 - 2013 Tensilica Inc.
5a0015d6
CZ
15 *
16 * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
17 * Chris Zankel <chris@zankel.net>
18 * Marc Gauthier<marc@tensilica.com, marc@alumni.uwaterloo.ca>
19 * Kevin Chea
20 *
21 * This file is subject to the terms and conditions of the GNU General Public
22 * License. See the file "COPYING" in the main directory of this archive
23 * for more details.
24 */
25
26#include <linux/kernel.h>
27#include <linux/sched.h>
28#include <linux/init.h>
29#include <linux/module.h>
30#include <linux/stringify.h>
31#include <linux/kallsyms.h>
5c888d53 32#include <linux/delay.h>
5a891ed5 33#include <linux/hardirq.h>
5a0015d6 34
3e4196a5 35#include <asm/stacktrace.h>
5a0015d6
CZ
36#include <asm/ptrace.h>
37#include <asm/timex.h>
38#include <asm/uaccess.h>
39#include <asm/pgtable.h>
40#include <asm/processor.h>
2d6f82fe 41#include <asm/traps.h>
5a0015d6
CZ
42
43#ifdef CONFIG_KGDB
44extern int gdb_enter;
45extern int return_from_debug_flag;
46#endif
47
48/*
49 * Machine specific interrupt handlers
50 */
51
52extern void kernel_exception(void);
53extern void user_exception(void);
54
55extern void fast_syscall_kernel(void);
56extern void fast_syscall_user(void);
57extern void fast_alloca(void);
58extern void fast_unaligned(void);
59extern void fast_second_level_miss(void);
60extern void fast_store_prohibited(void);
61extern void fast_coprocessor(void);
62
63extern void do_illegal_instruction (struct pt_regs*);
64extern void do_interrupt (struct pt_regs*);
65extern void do_unaligned_user (struct pt_regs*);
66extern void do_multihit (struct pt_regs*, unsigned long);
67extern void do_page_fault (struct pt_regs*, unsigned long);
68extern void do_debug (struct pt_regs*);
69extern void system_call (struct pt_regs*);
70
71/*
72 * The vector table must be preceded by a save area (which
73 * implies it must be in RAM, unless one places RAM immediately
74 * before a ROM and puts the vector at the start of the ROM (!))
75 */
76
77#define KRNL 0x01
78#define USER 0x02
79
80#define COPROCESSOR(x) \
173d6681 81{ EXCCAUSE_COPROCESSOR ## x ## _DISABLED, USER, fast_coprocessor }
5a0015d6
CZ
82
83typedef struct {
84 int cause;
85 int fast;
86 void* handler;
87} dispatch_init_table_t;
88
b91dc336 89static dispatch_init_table_t __initdata dispatch_init_table[] = {
5a0015d6 90
173d6681
CZ
91{ EXCCAUSE_ILLEGAL_INSTRUCTION, 0, do_illegal_instruction},
92{ EXCCAUSE_SYSTEM_CALL, KRNL, fast_syscall_kernel },
93{ EXCCAUSE_SYSTEM_CALL, USER, fast_syscall_user },
94{ EXCCAUSE_SYSTEM_CALL, 0, system_call },
95/* EXCCAUSE_INSTRUCTION_FETCH unhandled */
96/* EXCCAUSE_LOAD_STORE_ERROR unhandled*/
97{ EXCCAUSE_LEVEL1_INTERRUPT, 0, do_interrupt },
98{ EXCCAUSE_ALLOCA, USER|KRNL, fast_alloca },
99/* EXCCAUSE_INTEGER_DIVIDE_BY_ZERO unhandled */
100/* EXCCAUSE_PRIVILEGED unhandled */
5a0015d6 101#if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
4ded6282 102#ifdef CONFIG_XTENSA_UNALIGNED_USER
173d6681 103{ EXCCAUSE_UNALIGNED, USER, fast_unaligned },
5a0015d6 104#else
173d6681 105{ EXCCAUSE_UNALIGNED, 0, do_unaligned_user },
5a0015d6 106#endif
173d6681 107{ EXCCAUSE_UNALIGNED, KRNL, fast_unaligned },
5a0015d6 108#endif
e5083a63 109#ifdef CONFIG_MMU
173d6681
CZ
110{ EXCCAUSE_ITLB_MISS, 0, do_page_fault },
111{ EXCCAUSE_ITLB_MISS, USER|KRNL, fast_second_level_miss},
112{ EXCCAUSE_ITLB_MULTIHIT, 0, do_multihit },
113{ EXCCAUSE_ITLB_PRIVILEGE, 0, do_page_fault },
114/* EXCCAUSE_SIZE_RESTRICTION unhandled */
115{ EXCCAUSE_FETCH_CACHE_ATTRIBUTE, 0, do_page_fault },
116{ EXCCAUSE_DTLB_MISS, USER|KRNL, fast_second_level_miss},
117{ EXCCAUSE_DTLB_MISS, 0, do_page_fault },
118{ EXCCAUSE_DTLB_MULTIHIT, 0, do_multihit },
119{ EXCCAUSE_DTLB_PRIVILEGE, 0, do_page_fault },
120/* EXCCAUSE_DTLB_SIZE_RESTRICTION unhandled */
121{ EXCCAUSE_STORE_CACHE_ATTRIBUTE, USER|KRNL, fast_store_prohibited },
122{ EXCCAUSE_STORE_CACHE_ATTRIBUTE, 0, do_page_fault },
123{ EXCCAUSE_LOAD_CACHE_ATTRIBUTE, 0, do_page_fault },
e5083a63 124#endif /* CONFIG_MMU */
5a0015d6 125/* XCCHAL_EXCCAUSE_FLOATING_POINT unhandled */
c658eac6 126#if XTENSA_HAVE_COPROCESSOR(0)
5a0015d6
CZ
127COPROCESSOR(0),
128#endif
c658eac6 129#if XTENSA_HAVE_COPROCESSOR(1)
5a0015d6
CZ
130COPROCESSOR(1),
131#endif
c658eac6 132#if XTENSA_HAVE_COPROCESSOR(2)
5a0015d6
CZ
133COPROCESSOR(2),
134#endif
c658eac6 135#if XTENSA_HAVE_COPROCESSOR(3)
5a0015d6
CZ
136COPROCESSOR(3),
137#endif
c658eac6 138#if XTENSA_HAVE_COPROCESSOR(4)
5a0015d6
CZ
139COPROCESSOR(4),
140#endif
c658eac6 141#if XTENSA_HAVE_COPROCESSOR(5)
5a0015d6
CZ
142COPROCESSOR(5),
143#endif
c658eac6 144#if XTENSA_HAVE_COPROCESSOR(6)
5a0015d6
CZ
145COPROCESSOR(6),
146#endif
c658eac6 147#if XTENSA_HAVE_COPROCESSOR(7)
5a0015d6
CZ
148COPROCESSOR(7),
149#endif
150{ EXCCAUSE_MAPPED_DEBUG, 0, do_debug },
151{ -1, -1, 0 }
152
153};
154
155/* The exception table <exc_table> serves two functions:
156 * 1. it contains three dispatch tables (fast_user, fast_kernel, default-c)
157 * 2. it is a temporary memory buffer for the exception handlers.
158 */
159
f615136c 160DEFINE_PER_CPU(unsigned long, exc_table[EXC_TABLE_SIZE/4]);
5a0015d6
CZ
161
162void die(const char*, struct pt_regs*, long);
163
164static inline void
165__die_if_kernel(const char *str, struct pt_regs *regs, long err)
166{
167 if (!user_mode(regs))
168 die(str, regs, err);
169}
170
171/*
172 * Unhandled Exceptions. Kill user task or panic if in kernel space.
173 */
174
175void do_unhandled(struct pt_regs *regs, unsigned long exccause)
176{
177 __die_if_kernel("Caught unhandled exception - should not happen",
178 regs, SIGKILL);
179
180 /* If in user mode, send SIGILL signal to current process */
181 printk("Caught unhandled exception in '%s' "
182 "(pid = %d, pc = %#010lx) - should not happen\n"
183 "\tEXCCAUSE is %ld\n",
19c5870c 184 current->comm, task_pid_nr(current), regs->pc, exccause);
5a0015d6
CZ
185 force_sig(SIGILL, current);
186}
187
188/*
189 * Multi-hit exception. This if fatal!
190 */
191
192void do_multihit(struct pt_regs *regs, unsigned long exccause)
193{
194 die("Caught multihit exception", regs, SIGKILL);
195}
196
197/*
2d1c645c 198 * IRQ handler.
5a0015d6
CZ
199 */
200
5a0015d6
CZ
201extern void do_IRQ(int, struct pt_regs *);
202
2d1c645c 203void do_interrupt(struct pt_regs *regs)
5a0015d6 204{
2d1c645c
MG
205 static const unsigned int_level_mask[] = {
206 0,
207 XCHAL_INTLEVEL1_MASK,
208 XCHAL_INTLEVEL2_MASK,
209 XCHAL_INTLEVEL3_MASK,
210 XCHAL_INTLEVEL4_MASK,
211 XCHAL_INTLEVEL5_MASK,
212 XCHAL_INTLEVEL6_MASK,
213 XCHAL_INTLEVEL7_MASK,
214 };
99623239
MF
215 struct pt_regs *old_regs = set_irq_regs(regs);
216
217 irq_enter();
5a0015d6 218
2d1c645c
MG
219 for (;;) {
220 unsigned intread = get_sr(interrupt);
221 unsigned intenable = get_sr(intenable);
895666a9
MF
222 unsigned int_at_level = intread & intenable;
223 unsigned level;
224
225 for (level = LOCKLEVEL; level > 0; --level) {
226 if (int_at_level & int_level_mask[level]) {
227 int_at_level &= int_level_mask[level];
228 break;
229 }
230 }
2d1c645c 231
895666a9 232 if (level == 0)
99623239
MF
233 break;
234
235 do_IRQ(__ffs(int_at_level), regs);
5a0015d6 236 }
99623239
MF
237
238 irq_exit();
239 set_irq_regs(old_regs);
5a0015d6
CZ
240}
241
242/*
243 * Illegal instruction. Fatal if in kernel space.
244 */
245
246void
247do_illegal_instruction(struct pt_regs *regs)
248{
249 __die_if_kernel("Illegal instruction in kernel", regs, SIGKILL);
250
251 /* If in user mode, send SIGILL signal to current process. */
252
253 printk("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n",
19c5870c 254 current->comm, task_pid_nr(current), regs->pc);
5a0015d6
CZ
255 force_sig(SIGILL, current);
256}
257
258
259/*
260 * Handle unaligned memory accesses from user space. Kill task.
261 *
262 * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory
263 * accesses causes from user space.
264 */
265
266#if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
4ded6282 267#ifndef CONFIG_XTENSA_UNALIGNED_USER
5a0015d6
CZ
268void
269do_unaligned_user (struct pt_regs *regs)
270{
271 siginfo_t info;
272
273 __die_if_kernel("Unhandled unaligned exception in kernel",
274 regs, SIGKILL);
275
276 current->thread.bad_vaddr = regs->excvaddr;
277 current->thread.error_code = -3;
278 printk("Unaligned memory access to %08lx in '%s' "
279 "(pid = %d, pc = %#010lx)\n",
19c5870c 280 regs->excvaddr, current->comm, task_pid_nr(current), regs->pc);
5a0015d6
CZ
281 info.si_signo = SIGBUS;
282 info.si_errno = 0;
283 info.si_code = BUS_ADRALN;
284 info.si_addr = (void *) regs->excvaddr;
285 force_sig_info(SIGSEGV, &info, current);
286
287}
288#endif
289#endif
290
291void
292do_debug(struct pt_regs *regs)
293{
294#ifdef CONFIG_KGDB
295 /* If remote debugging is configured AND enabled, we give control to
296 * kgdb. Otherwise, we fall through, perhaps giving control to the
297 * native debugger.
298 */
299
300 if (gdb_enter) {
301 extern void gdb_handle_exception(struct pt_regs *);
302 gdb_handle_exception(regs);
303 return_from_debug_flag = 1;
304 return;
305 }
306#endif
307
308 __die_if_kernel("Breakpoint in kernel", regs, SIGKILL);
309
310 /* If in user mode, send SIGTRAP signal to current process */
311
312 force_sig(SIGTRAP, current);
313}
314
315
f615136c
MF
316static void set_handler(int idx, void *handler)
317{
318 unsigned int cpu;
319
320 for_each_possible_cpu(cpu)
321 per_cpu(exc_table, cpu)[idx] = (unsigned long)handler;
322}
323
28570e8d
MF
324/* Set exception C handler - for temporary use when probing exceptions */
325
326void * __init trap_set_handler(int cause, void *handler)
327{
f615136c
MF
328 void *previous = (void *)per_cpu(exc_table, 0)[
329 EXC_TABLE_DEFAULT / 4 + cause];
330 set_handler(EXC_TABLE_DEFAULT / 4 + cause, handler);
28570e8d
MF
331 return previous;
332}
333
334
49b424fe 335static void trap_init_excsave(void)
f615136c
MF
336{
337 unsigned long excsave1 = (unsigned long)this_cpu_ptr(exc_table);
338 __asm__ __volatile__("wsr %0, excsave1\n" : : "a" (excsave1));
339}
340
5a0015d6
CZ
341/*
342 * Initialize dispatch tables.
343 *
344 * The exception vectors are stored compressed the __init section in the
345 * dispatch_init_table. This function initializes the following three tables
346 * from that compressed table:
347 * - fast user first dispatch table for user exceptions
348 * - fast kernel first dispatch table for kernel exceptions
349 * - default C-handler C-handler called by the default fast handler.
350 *
351 * See vectors.S for more details.
352 */
353
b91dc336 354void __init trap_init(void)
5a0015d6
CZ
355{
356 int i;
357
358 /* Setup default vectors. */
359
360 for(i = 0; i < 64; i++) {
361 set_handler(EXC_TABLE_FAST_USER/4 + i, user_exception);
362 set_handler(EXC_TABLE_FAST_KERNEL/4 + i, kernel_exception);
363 set_handler(EXC_TABLE_DEFAULT/4 + i, do_unhandled);
364 }
365
366 /* Setup specific handlers. */
367
368 for(i = 0; dispatch_init_table[i].cause >= 0; i++) {
369
370 int fast = dispatch_init_table[i].fast;
371 int cause = dispatch_init_table[i].cause;
372 void *handler = dispatch_init_table[i].handler;
373
374 if (fast == 0)
375 set_handler (EXC_TABLE_DEFAULT/4 + cause, handler);
376 if (fast && fast & USER)
377 set_handler (EXC_TABLE_FAST_USER/4 + cause, handler);
378 if (fast && fast & KRNL)
379 set_handler (EXC_TABLE_FAST_KERNEL/4 + cause, handler);
380 }
381
382 /* Initialize EXCSAVE_1 to hold the address of the exception table. */
f615136c
MF
383 trap_init_excsave();
384}
5a0015d6 385
f615136c 386#ifdef CONFIG_SMP
49b424fe 387void secondary_trap_init(void)
f615136c
MF
388{
389 trap_init_excsave();
5a0015d6 390}
f615136c 391#endif
5a0015d6
CZ
392
393/*
394 * This function dumps the current valid window frame and other base registers.
395 */
396
397void show_regs(struct pt_regs * regs)
398{
399 int i, wmask;
400
a43cb95d
TH
401 show_regs_print_info(KERN_DEFAULT);
402
5a0015d6
CZ
403 wmask = regs->wmask & ~1;
404
8d7e8240 405 for (i = 0; i < 16; i++) {
5a0015d6 406 if ((i % 8) == 0)
ad361c98
JP
407 printk(KERN_INFO "a%02d:", i);
408 printk(KERN_CONT " %08lx", regs->areg[i]);
5a0015d6 409 }
ad361c98 410 printk(KERN_CONT "\n");
5a0015d6
CZ
411
412 printk("pc: %08lx, ps: %08lx, depc: %08lx, excvaddr: %08lx\n",
413 regs->pc, regs->ps, regs->depc, regs->excvaddr);
414 printk("lbeg: %08lx, lend: %08lx lcount: %08lx, sar: %08lx\n",
415 regs->lbeg, regs->lend, regs->lcount, regs->sar);
416 if (user_mode(regs))
417 printk("wb: %08lx, ws: %08lx, wmask: %08lx, syscall: %ld\n",
418 regs->windowbase, regs->windowstart, regs->wmask,
419 regs->syscall);
420}
421
3e4196a5 422static int show_trace_cb(struct stackframe *frame, void *data)
586411dc 423{
3e4196a5
MF
424 if (kernel_text_address(frame->pc)) {
425 printk(" [<%08lx>] ", frame->pc);
426 print_symbol("%s\n", frame->pc);
427 }
428 return 0;
586411dc
JW
429}
430
5a0015d6
CZ
431void show_trace(struct task_struct *task, unsigned long *sp)
432{
3e4196a5
MF
433 if (!sp)
434 sp = stack_pointer(task);
5a0015d6
CZ
435
436 printk("Call Trace:");
437#ifdef CONFIG_KALLSYMS
438 printk("\n");
439#endif
3e4196a5 440 walk_stackframe(sp, show_trace_cb, NULL);
5a0015d6
CZ
441 printk("\n");
442}
443
444/*
445 * This routine abuses get_user()/put_user() to reference pointers
446 * with at least a bit of error checking ...
447 */
448
449static int kstack_depth_to_print = 24;
450
451void show_stack(struct task_struct *task, unsigned long *sp)
452{
453 int i = 0;
454 unsigned long *stack;
455
28a0ce7f 456 if (!sp)
586411dc 457 sp = stack_pointer(task);
c4c4594b 458 stack = sp;
5a0015d6
CZ
459
460 printk("\nStack: ");
461
462 for (i = 0; i < kstack_depth_to_print; i++) {
463 if (kstack_end(sp))
464 break;
465 if (i && ((i % 8) == 0))
466 printk("\n ");
467 printk("%08lx ", *sp++);
468 }
469 printk("\n");
470 show_trace(task, stack);
471}
472
5a0015d6
CZ
473void show_code(unsigned int *pc)
474{
475 long i;
476
477 printk("\nCode:");
478
479 for(i = -3 ; i < 6 ; i++) {
480 unsigned long insn;
481 if (__get_user(insn, pc + i)) {
482 printk(" (Bad address in pc)\n");
483 break;
484 }
485 printk("%c%08lx%c",(i?' ':'<'),insn,(i?' ':'>'));
486 }
487}
488
34af946a 489DEFINE_SPINLOCK(die_lock);
5a0015d6
CZ
490
491void die(const char * str, struct pt_regs * regs, long err)
492{
493 static int die_counter;
494 int nl = 0;
495
496 console_verbose();
497 spin_lock_irq(&die_lock);
498
499 printk("%s: sig: %ld [#%d]\n", str, err, ++die_counter);
500#ifdef CONFIG_PREEMPT
501 printk("PREEMPT ");
502 nl = 1;
503#endif
504 if (nl)
505 printk("\n");
506 show_regs(regs);
507 if (!user_mode(regs))
508 show_stack(NULL, (unsigned long*)regs->areg[1]);
509
373d4d09 510 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
5a0015d6
CZ
511 spin_unlock_irq(&die_lock);
512
513 if (in_interrupt())
514 panic("Fatal exception in interrupt");
515
cea6a4ba 516 if (panic_on_oops)
012c437d 517 panic("Fatal exception");
cea6a4ba 518
5a0015d6
CZ
519 do_exit(err);
520}