blob: 923db5c15278fc530bc593fa321781a01ecd1c37 [file] [log] [blame]
Chris Zankel5a0015d2005-06-23 22:01:16 -07001/*
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 *
14 * Copyright (C) 2001 - 2005 Tensilica Inc.
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>
Nishanth Aravamudan5c888d52005-07-12 13:58:26 -070032#include <linux/delay.h>
Alexey Dobriyan5a891ed2009-03-10 12:55:49 -070033#include <linux/hardirq.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070034
35#include <asm/ptrace.h>
36#include <asm/timex.h>
37#include <asm/uaccess.h>
38#include <asm/pgtable.h>
39#include <asm/processor.h>
Max Filippov2d6f82f2013-02-03 05:39:22 +040040#include <asm/traps.h>
Chris Zankel5a0015d2005-06-23 22:01:16 -070041
42#ifdef CONFIG_KGDB
43extern int gdb_enter;
44extern int return_from_debug_flag;
45#endif
46
47/*
48 * Machine specific interrupt handlers
49 */
50
51extern void kernel_exception(void);
52extern void user_exception(void);
53
54extern void fast_syscall_kernel(void);
55extern void fast_syscall_user(void);
56extern void fast_alloca(void);
57extern void fast_unaligned(void);
58extern void fast_second_level_miss(void);
59extern void fast_store_prohibited(void);
60extern void fast_coprocessor(void);
61
62extern void do_illegal_instruction (struct pt_regs*);
63extern void do_interrupt (struct pt_regs*);
64extern void do_unaligned_user (struct pt_regs*);
65extern void do_multihit (struct pt_regs*, unsigned long);
66extern void do_page_fault (struct pt_regs*, unsigned long);
67extern void do_debug (struct pt_regs*);
68extern void system_call (struct pt_regs*);
69
70/*
71 * The vector table must be preceded by a save area (which
72 * implies it must be in RAM, unless one places RAM immediately
73 * before a ROM and puts the vector at the start of the ROM (!))
74 */
75
76#define KRNL 0x01
77#define USER 0x02
78
79#define COPROCESSOR(x) \
Chris Zankel173d6682006-12-10 02:18:48 -080080{ EXCCAUSE_COPROCESSOR ## x ## _DISABLED, USER, fast_coprocessor }
Chris Zankel5a0015d2005-06-23 22:01:16 -070081
82typedef struct {
83 int cause;
84 int fast;
85 void* handler;
86} dispatch_init_table_t;
87
Chris Zankelb91dc332007-08-03 15:54:36 -070088static dispatch_init_table_t __initdata dispatch_init_table[] = {
Chris Zankel5a0015d2005-06-23 22:01:16 -070089
Chris Zankel173d6682006-12-10 02:18:48 -080090{ EXCCAUSE_ILLEGAL_INSTRUCTION, 0, do_illegal_instruction},
91{ EXCCAUSE_SYSTEM_CALL, KRNL, fast_syscall_kernel },
92{ EXCCAUSE_SYSTEM_CALL, USER, fast_syscall_user },
93{ EXCCAUSE_SYSTEM_CALL, 0, system_call },
94/* EXCCAUSE_INSTRUCTION_FETCH unhandled */
95/* EXCCAUSE_LOAD_STORE_ERROR unhandled*/
96{ EXCCAUSE_LEVEL1_INTERRUPT, 0, do_interrupt },
97{ EXCCAUSE_ALLOCA, USER|KRNL, fast_alloca },
98/* EXCCAUSE_INTEGER_DIVIDE_BY_ZERO unhandled */
99/* EXCCAUSE_PRIVILEGED unhandled */
Chris Zankel5a0015d2005-06-23 22:01:16 -0700100#if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
Max Filippov4ded6282012-10-15 21:23:02 +0400101#ifdef CONFIG_XTENSA_UNALIGNED_USER
Chris Zankel173d6682006-12-10 02:18:48 -0800102{ EXCCAUSE_UNALIGNED, USER, fast_unaligned },
Chris Zankel5a0015d2005-06-23 22:01:16 -0700103#else
Chris Zankel173d6682006-12-10 02:18:48 -0800104{ EXCCAUSE_UNALIGNED, 0, do_unaligned_user },
Chris Zankel5a0015d2005-06-23 22:01:16 -0700105#endif
Chris Zankel173d6682006-12-10 02:18:48 -0800106{ EXCCAUSE_UNALIGNED, KRNL, fast_unaligned },
Chris Zankel5a0015d2005-06-23 22:01:16 -0700107#endif
Johannes Weinere5083a62009-03-04 16:21:31 +0100108#ifdef CONFIG_MMU
Chris Zankel173d6682006-12-10 02:18:48 -0800109{ EXCCAUSE_ITLB_MISS, 0, do_page_fault },
110{ EXCCAUSE_ITLB_MISS, USER|KRNL, fast_second_level_miss},
111{ EXCCAUSE_ITLB_MULTIHIT, 0, do_multihit },
112{ EXCCAUSE_ITLB_PRIVILEGE, 0, do_page_fault },
113/* EXCCAUSE_SIZE_RESTRICTION unhandled */
114{ EXCCAUSE_FETCH_CACHE_ATTRIBUTE, 0, do_page_fault },
115{ EXCCAUSE_DTLB_MISS, USER|KRNL, fast_second_level_miss},
116{ EXCCAUSE_DTLB_MISS, 0, do_page_fault },
117{ EXCCAUSE_DTLB_MULTIHIT, 0, do_multihit },
118{ EXCCAUSE_DTLB_PRIVILEGE, 0, do_page_fault },
119/* EXCCAUSE_DTLB_SIZE_RESTRICTION unhandled */
120{ EXCCAUSE_STORE_CACHE_ATTRIBUTE, USER|KRNL, fast_store_prohibited },
121{ EXCCAUSE_STORE_CACHE_ATTRIBUTE, 0, do_page_fault },
122{ EXCCAUSE_LOAD_CACHE_ATTRIBUTE, 0, do_page_fault },
Johannes Weinere5083a62009-03-04 16:21:31 +0100123#endif /* CONFIG_MMU */
Chris Zankel5a0015d2005-06-23 22:01:16 -0700124/* XCCHAL_EXCCAUSE_FLOATING_POINT unhandled */
Chris Zankelc658eac2008-02-12 13:17:07 -0800125#if XTENSA_HAVE_COPROCESSOR(0)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700126COPROCESSOR(0),
127#endif
Chris Zankelc658eac2008-02-12 13:17:07 -0800128#if XTENSA_HAVE_COPROCESSOR(1)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700129COPROCESSOR(1),
130#endif
Chris Zankelc658eac2008-02-12 13:17:07 -0800131#if XTENSA_HAVE_COPROCESSOR(2)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700132COPROCESSOR(2),
133#endif
Chris Zankelc658eac2008-02-12 13:17:07 -0800134#if XTENSA_HAVE_COPROCESSOR(3)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700135COPROCESSOR(3),
136#endif
Chris Zankelc658eac2008-02-12 13:17:07 -0800137#if XTENSA_HAVE_COPROCESSOR(4)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700138COPROCESSOR(4),
139#endif
Chris Zankelc658eac2008-02-12 13:17:07 -0800140#if XTENSA_HAVE_COPROCESSOR(5)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700141COPROCESSOR(5),
142#endif
Chris Zankelc658eac2008-02-12 13:17:07 -0800143#if XTENSA_HAVE_COPROCESSOR(6)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700144COPROCESSOR(6),
145#endif
Chris Zankelc658eac2008-02-12 13:17:07 -0800146#if XTENSA_HAVE_COPROCESSOR(7)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700147COPROCESSOR(7),
148#endif
149{ EXCCAUSE_MAPPED_DEBUG, 0, do_debug },
150{ -1, -1, 0 }
151
152};
153
154/* The exception table <exc_table> serves two functions:
155 * 1. it contains three dispatch tables (fast_user, fast_kernel, default-c)
156 * 2. it is a temporary memory buffer for the exception handlers.
157 */
158
159unsigned long exc_table[EXC_TABLE_SIZE/4];
160
161void die(const char*, struct pt_regs*, long);
162
163static inline void
164__die_if_kernel(const char *str, struct pt_regs *regs, long err)
165{
166 if (!user_mode(regs))
167 die(str, regs, err);
168}
169
170/*
171 * Unhandled Exceptions. Kill user task or panic if in kernel space.
172 */
173
174void do_unhandled(struct pt_regs *regs, unsigned long exccause)
175{
176 __die_if_kernel("Caught unhandled exception - should not happen",
177 regs, SIGKILL);
178
179 /* If in user mode, send SIGILL signal to current process */
180 printk("Caught unhandled exception in '%s' "
181 "(pid = %d, pc = %#010lx) - should not happen\n"
182 "\tEXCCAUSE is %ld\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700183 current->comm, task_pid_nr(current), regs->pc, exccause);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700184 force_sig(SIGILL, current);
185}
186
187/*
188 * Multi-hit exception. This if fatal!
189 */
190
191void do_multihit(struct pt_regs *regs, unsigned long exccause)
192{
193 die("Caught multihit exception", regs, SIGKILL);
194}
195
196/*
Marc Gauthier2d1c6452013-01-05 04:57:17 +0400197 * IRQ handler.
198 * PS.INTLEVEL is the current IRQ priority level.
Chris Zankel5a0015d2005-06-23 22:01:16 -0700199 */
200
Chris Zankel5a0015d2005-06-23 22:01:16 -0700201extern void do_IRQ(int, struct pt_regs *);
202
Marc Gauthier2d1c6452013-01-05 04:57:17 +0400203void do_interrupt(struct pt_regs *regs)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700204{
Marc Gauthier2d1c6452013-01-05 04:57:17 +0400205 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 };
215 unsigned level = get_sr(ps) & PS_INTLEVEL_MASK;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700216
Marc Gauthier2d1c6452013-01-05 04:57:17 +0400217 if (WARN_ON_ONCE(level >= ARRAY_SIZE(int_level_mask)))
218 return;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700219
Marc Gauthier2d1c6452013-01-05 04:57:17 +0400220 for (;;) {
221 unsigned intread = get_sr(interrupt);
222 unsigned intenable = get_sr(intenable);
223 unsigned int_at_level = intread & intenable &
224 int_level_mask[level];
225
226 if (!int_at_level)
227 return;
228
229 /*
230 * Clear the interrupt before processing, in case it's
231 * edge-triggered or software-generated
232 */
233 while (int_at_level) {
234 unsigned i = __ffs(int_at_level);
235 unsigned mask = 1 << i;
236
237 int_at_level ^= mask;
238 set_sr(mask, intclear);
239 do_IRQ(i, regs);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700240 }
241 }
242}
243
244/*
245 * Illegal instruction. Fatal if in kernel space.
246 */
247
248void
249do_illegal_instruction(struct pt_regs *regs)
250{
251 __die_if_kernel("Illegal instruction in kernel", regs, SIGKILL);
252
253 /* If in user mode, send SIGILL signal to current process. */
254
255 printk("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700256 current->comm, task_pid_nr(current), regs->pc);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700257 force_sig(SIGILL, current);
258}
259
260
261/*
262 * Handle unaligned memory accesses from user space. Kill task.
263 *
264 * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory
265 * accesses causes from user space.
266 */
267
268#if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
Max Filippov4ded6282012-10-15 21:23:02 +0400269#ifndef CONFIG_XTENSA_UNALIGNED_USER
Chris Zankel5a0015d2005-06-23 22:01:16 -0700270void
271do_unaligned_user (struct pt_regs *regs)
272{
273 siginfo_t info;
274
275 __die_if_kernel("Unhandled unaligned exception in kernel",
276 regs, SIGKILL);
277
278 current->thread.bad_vaddr = regs->excvaddr;
279 current->thread.error_code = -3;
280 printk("Unaligned memory access to %08lx in '%s' "
281 "(pid = %d, pc = %#010lx)\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700282 regs->excvaddr, current->comm, task_pid_nr(current), regs->pc);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700283 info.si_signo = SIGBUS;
284 info.si_errno = 0;
285 info.si_code = BUS_ADRALN;
286 info.si_addr = (void *) regs->excvaddr;
287 force_sig_info(SIGSEGV, &info, current);
288
289}
290#endif
291#endif
292
293void
294do_debug(struct pt_regs *regs)
295{
296#ifdef CONFIG_KGDB
297 /* If remote debugging is configured AND enabled, we give control to
298 * kgdb. Otherwise, we fall through, perhaps giving control to the
299 * native debugger.
300 */
301
302 if (gdb_enter) {
303 extern void gdb_handle_exception(struct pt_regs *);
304 gdb_handle_exception(regs);
305 return_from_debug_flag = 1;
306 return;
307 }
308#endif
309
310 __die_if_kernel("Breakpoint in kernel", regs, SIGKILL);
311
312 /* If in user mode, send SIGTRAP signal to current process */
313
314 force_sig(SIGTRAP, current);
315}
316
317
Max Filippov28570e82012-11-19 08:30:15 +0400318/* Set exception C handler - for temporary use when probing exceptions */
319
320void * __init trap_set_handler(int cause, void *handler)
321{
322 unsigned long *entry = &exc_table[EXC_TABLE_DEFAULT / 4 + cause];
323 void *previous = (void *)*entry;
324 *entry = (unsigned long)handler;
325 return previous;
326}
327
328
Chris Zankel5a0015d2005-06-23 22:01:16 -0700329/*
330 * Initialize dispatch tables.
331 *
332 * The exception vectors are stored compressed the __init section in the
333 * dispatch_init_table. This function initializes the following three tables
334 * from that compressed table:
335 * - fast user first dispatch table for user exceptions
336 * - fast kernel first dispatch table for kernel exceptions
337 * - default C-handler C-handler called by the default fast handler.
338 *
339 * See vectors.S for more details.
340 */
341
342#define set_handler(idx,handler) (exc_table[idx] = (unsigned long) (handler))
343
Chris Zankelb91dc332007-08-03 15:54:36 -0700344void __init trap_init(void)
Chris Zankel5a0015d2005-06-23 22:01:16 -0700345{
346 int i;
347
348 /* Setup default vectors. */
349
350 for(i = 0; i < 64; i++) {
351 set_handler(EXC_TABLE_FAST_USER/4 + i, user_exception);
352 set_handler(EXC_TABLE_FAST_KERNEL/4 + i, kernel_exception);
353 set_handler(EXC_TABLE_DEFAULT/4 + i, do_unhandled);
354 }
355
356 /* Setup specific handlers. */
357
358 for(i = 0; dispatch_init_table[i].cause >= 0; i++) {
359
360 int fast = dispatch_init_table[i].fast;
361 int cause = dispatch_init_table[i].cause;
362 void *handler = dispatch_init_table[i].handler;
363
364 if (fast == 0)
365 set_handler (EXC_TABLE_DEFAULT/4 + cause, handler);
366 if (fast && fast & USER)
367 set_handler (EXC_TABLE_FAST_USER/4 + cause, handler);
368 if (fast && fast & KRNL)
369 set_handler (EXC_TABLE_FAST_KERNEL/4 + cause, handler);
370 }
371
372 /* Initialize EXCSAVE_1 to hold the address of the exception table. */
373
374 i = (unsigned long)exc_table;
Max Filippovbc5378f2012-10-15 03:55:38 +0400375 __asm__ __volatile__("wsr %0, excsave1\n" : : "a" (i));
Chris Zankel5a0015d2005-06-23 22:01:16 -0700376}
377
378/*
379 * This function dumps the current valid window frame and other base registers.
380 */
381
382void show_regs(struct pt_regs * regs)
383{
384 int i, wmask;
385
386 wmask = regs->wmask & ~1;
387
Chris Zankel8d7e8242008-02-12 11:55:32 -0800388 for (i = 0; i < 16; i++) {
Chris Zankel5a0015d2005-06-23 22:01:16 -0700389 if ((i % 8) == 0)
Joe Perchesad361c92009-07-06 13:05:40 -0700390 printk(KERN_INFO "a%02d:", i);
391 printk(KERN_CONT " %08lx", regs->areg[i]);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700392 }
Joe Perchesad361c92009-07-06 13:05:40 -0700393 printk(KERN_CONT "\n");
Chris Zankel5a0015d2005-06-23 22:01:16 -0700394
395 printk("pc: %08lx, ps: %08lx, depc: %08lx, excvaddr: %08lx\n",
396 regs->pc, regs->ps, regs->depc, regs->excvaddr);
397 printk("lbeg: %08lx, lend: %08lx lcount: %08lx, sar: %08lx\n",
398 regs->lbeg, regs->lend, regs->lcount, regs->sar);
399 if (user_mode(regs))
400 printk("wb: %08lx, ws: %08lx, wmask: %08lx, syscall: %ld\n",
401 regs->windowbase, regs->windowstart, regs->wmask,
402 regs->syscall);
403}
404
Johannes Weiner586411d2009-05-11 15:43:33 +0200405static __always_inline unsigned long *stack_pointer(struct task_struct *task)
406{
407 unsigned long *sp;
408
409 if (!task || task == current)
410 __asm__ __volatile__ ("mov %0, a1\n" : "=a"(sp));
411 else
412 sp = (unsigned long *)task->thread.sp;
413
414 return sp;
415}
416
Chris Zankel5a0015d2005-06-23 22:01:16 -0700417void show_trace(struct task_struct *task, unsigned long *sp)
418{
419 unsigned long a0, a1, pc;
420 unsigned long sp_start, sp_end;
421
Johannes Weiner28a0ce72009-03-04 16:21:29 +0100422 if (sp)
423 a1 = (unsigned long)sp;
424 else
Johannes Weiner586411d2009-05-11 15:43:33 +0200425 a1 = (unsigned long)stack_pointer(task);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700426
427 sp_start = a1 & ~(THREAD_SIZE-1);
428 sp_end = sp_start + THREAD_SIZE;
429
430 printk("Call Trace:");
431#ifdef CONFIG_KALLSYMS
432 printk("\n");
433#endif
434 spill_registers();
435
436 while (a1 > sp_start && a1 < sp_end) {
437 sp = (unsigned long*)a1;
438
439 a0 = *(sp - 4);
440 a1 = *(sp - 3);
441
442 if (a1 <= (unsigned long) sp)
443 break;
444
445 pc = MAKE_PC_FROM_RA(a0, a1);
446
447 if (kernel_text_address(pc)) {
448 printk(" [<%08lx>] ", pc);
449 print_symbol("%s\n", pc);
450 }
451 }
452 printk("\n");
453}
454
455/*
456 * This routine abuses get_user()/put_user() to reference pointers
457 * with at least a bit of error checking ...
458 */
459
460static int kstack_depth_to_print = 24;
461
462void show_stack(struct task_struct *task, unsigned long *sp)
463{
464 int i = 0;
465 unsigned long *stack;
466
Johannes Weiner28a0ce72009-03-04 16:21:29 +0100467 if (!sp)
Johannes Weiner586411d2009-05-11 15:43:33 +0200468 sp = stack_pointer(task);
Chris Zankelc4c45942012-11-28 16:53:51 -0800469 stack = sp;
Chris Zankel5a0015d2005-06-23 22:01:16 -0700470
471 printk("\nStack: ");
472
473 for (i = 0; i < kstack_depth_to_print; i++) {
474 if (kstack_end(sp))
475 break;
476 if (i && ((i % 8) == 0))
477 printk("\n ");
478 printk("%08lx ", *sp++);
479 }
480 printk("\n");
481 show_trace(task, stack);
482}
483
484void dump_stack(void)
485{
486 show_stack(current, NULL);
487}
488
489EXPORT_SYMBOL(dump_stack);
490
491
492void show_code(unsigned int *pc)
493{
494 long i;
495
496 printk("\nCode:");
497
498 for(i = -3 ; i < 6 ; i++) {
499 unsigned long insn;
500 if (__get_user(insn, pc + i)) {
501 printk(" (Bad address in pc)\n");
502 break;
503 }
504 printk("%c%08lx%c",(i?' ':'<'),insn,(i?' ':'>'));
505 }
506}
507
Ingo Molnar34af9462006-06-27 02:53:55 -0700508DEFINE_SPINLOCK(die_lock);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700509
510void die(const char * str, struct pt_regs * regs, long err)
511{
512 static int die_counter;
513 int nl = 0;
514
515 console_verbose();
516 spin_lock_irq(&die_lock);
517
518 printk("%s: sig: %ld [#%d]\n", str, err, ++die_counter);
519#ifdef CONFIG_PREEMPT
520 printk("PREEMPT ");
521 nl = 1;
522#endif
523 if (nl)
524 printk("\n");
525 show_regs(regs);
526 if (!user_mode(regs))
527 show_stack(NULL, (unsigned long*)regs->areg[1]);
528
Rusty Russell373d4d092013-01-21 17:17:39 +1030529 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
Chris Zankel5a0015d2005-06-23 22:01:16 -0700530 spin_unlock_irq(&die_lock);
531
532 if (in_interrupt())
533 panic("Fatal exception in interrupt");
534
Hormscea6a4b2006-07-30 03:03:34 -0700535 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700536 panic("Fatal exception");
Hormscea6a4b2006-07-30 03:03:34 -0700537
Chris Zankel5a0015d2005-06-23 22:01:16 -0700538 do_exit(err);
539}