xtensa: remove remaining non-functional KGDB bits
[linux-2.6-block.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 42
5a0015d6
CZ
43/*
44 * Machine specific interrupt handlers
45 */
46
47extern void kernel_exception(void);
48extern void user_exception(void);
49
50extern void fast_syscall_kernel(void);
51extern void fast_syscall_user(void);
52extern void fast_alloca(void);
53extern void fast_unaligned(void);
54extern void fast_second_level_miss(void);
55extern void fast_store_prohibited(void);
56extern void fast_coprocessor(void);
57
58extern void do_illegal_instruction (struct pt_regs*);
59extern void do_interrupt (struct pt_regs*);
38fef73c 60extern void do_nmi(struct pt_regs *);
5a0015d6
CZ
61extern void do_unaligned_user (struct pt_regs*);
62extern void do_multihit (struct pt_regs*, unsigned long);
63extern void do_page_fault (struct pt_regs*, unsigned long);
64extern void do_debug (struct pt_regs*);
65extern void system_call (struct pt_regs*);
66
67/*
68 * The vector table must be preceded by a save area (which
69 * implies it must be in RAM, unless one places RAM immediately
70 * before a ROM and puts the vector at the start of the ROM (!))
71 */
72
73#define KRNL 0x01
74#define USER 0x02
75
76#define COPROCESSOR(x) \
173d6681 77{ EXCCAUSE_COPROCESSOR ## x ## _DISABLED, USER, fast_coprocessor }
5a0015d6
CZ
78
79typedef struct {
80 int cause;
81 int fast;
82 void* handler;
83} dispatch_init_table_t;
84
b91dc336 85static dispatch_init_table_t __initdata dispatch_init_table[] = {
5a0015d6 86
173d6681
CZ
87{ EXCCAUSE_ILLEGAL_INSTRUCTION, 0, do_illegal_instruction},
88{ EXCCAUSE_SYSTEM_CALL, KRNL, fast_syscall_kernel },
89{ EXCCAUSE_SYSTEM_CALL, USER, fast_syscall_user },
90{ EXCCAUSE_SYSTEM_CALL, 0, system_call },
91/* EXCCAUSE_INSTRUCTION_FETCH unhandled */
92/* EXCCAUSE_LOAD_STORE_ERROR unhandled*/
93{ EXCCAUSE_LEVEL1_INTERRUPT, 0, do_interrupt },
94{ EXCCAUSE_ALLOCA, USER|KRNL, fast_alloca },
95/* EXCCAUSE_INTEGER_DIVIDE_BY_ZERO unhandled */
96/* EXCCAUSE_PRIVILEGED unhandled */
5a0015d6 97#if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
4ded6282 98#ifdef CONFIG_XTENSA_UNALIGNED_USER
173d6681 99{ EXCCAUSE_UNALIGNED, USER, fast_unaligned },
5a0015d6 100#endif
3cfc096e 101{ EXCCAUSE_UNALIGNED, 0, do_unaligned_user },
173d6681 102{ EXCCAUSE_UNALIGNED, KRNL, fast_unaligned },
5a0015d6 103#endif
e5083a63 104#ifdef CONFIG_MMU
173d6681
CZ
105{ EXCCAUSE_ITLB_MISS, 0, do_page_fault },
106{ EXCCAUSE_ITLB_MISS, USER|KRNL, fast_second_level_miss},
107{ EXCCAUSE_ITLB_MULTIHIT, 0, do_multihit },
108{ EXCCAUSE_ITLB_PRIVILEGE, 0, do_page_fault },
109/* EXCCAUSE_SIZE_RESTRICTION unhandled */
110{ EXCCAUSE_FETCH_CACHE_ATTRIBUTE, 0, do_page_fault },
111{ EXCCAUSE_DTLB_MISS, USER|KRNL, fast_second_level_miss},
112{ EXCCAUSE_DTLB_MISS, 0, do_page_fault },
113{ EXCCAUSE_DTLB_MULTIHIT, 0, do_multihit },
114{ EXCCAUSE_DTLB_PRIVILEGE, 0, do_page_fault },
115/* EXCCAUSE_DTLB_SIZE_RESTRICTION unhandled */
116{ EXCCAUSE_STORE_CACHE_ATTRIBUTE, USER|KRNL, fast_store_prohibited },
117{ EXCCAUSE_STORE_CACHE_ATTRIBUTE, 0, do_page_fault },
118{ EXCCAUSE_LOAD_CACHE_ATTRIBUTE, 0, do_page_fault },
e5083a63 119#endif /* CONFIG_MMU */
5a0015d6 120/* XCCHAL_EXCCAUSE_FLOATING_POINT unhandled */
c658eac6 121#if XTENSA_HAVE_COPROCESSOR(0)
5a0015d6
CZ
122COPROCESSOR(0),
123#endif
c658eac6 124#if XTENSA_HAVE_COPROCESSOR(1)
5a0015d6
CZ
125COPROCESSOR(1),
126#endif
c658eac6 127#if XTENSA_HAVE_COPROCESSOR(2)
5a0015d6
CZ
128COPROCESSOR(2),
129#endif
c658eac6 130#if XTENSA_HAVE_COPROCESSOR(3)
5a0015d6
CZ
131COPROCESSOR(3),
132#endif
c658eac6 133#if XTENSA_HAVE_COPROCESSOR(4)
5a0015d6
CZ
134COPROCESSOR(4),
135#endif
c658eac6 136#if XTENSA_HAVE_COPROCESSOR(5)
5a0015d6
CZ
137COPROCESSOR(5),
138#endif
c658eac6 139#if XTENSA_HAVE_COPROCESSOR(6)
5a0015d6
CZ
140COPROCESSOR(6),
141#endif
c658eac6 142#if XTENSA_HAVE_COPROCESSOR(7)
5a0015d6
CZ
143COPROCESSOR(7),
144#endif
38fef73c
MF
145#if XTENSA_FAKE_NMI
146{ EXCCAUSE_MAPPED_NMI, 0, do_nmi },
147#endif
5a0015d6
CZ
148{ EXCCAUSE_MAPPED_DEBUG, 0, do_debug },
149{ -1, -1, 0 }
150
151};
152
153/* The exception table <exc_table> serves two functions:
154 * 1. it contains three dispatch tables (fast_user, fast_kernel, default-c)
155 * 2. it is a temporary memory buffer for the exception handlers.
156 */
157
f615136c 158DEFINE_PER_CPU(unsigned long, exc_table[EXC_TABLE_SIZE/4]);
5a0015d6
CZ
159
160void die(const char*, struct pt_regs*, long);
161
162static inline void
163__die_if_kernel(const char *str, struct pt_regs *regs, long err)
164{
165 if (!user_mode(regs))
166 die(str, regs, err);
167}
168
169/*
170 * Unhandled Exceptions. Kill user task or panic if in kernel space.
171 */
172
173void do_unhandled(struct pt_regs *regs, unsigned long exccause)
174{
175 __die_if_kernel("Caught unhandled exception - should not happen",
176 regs, SIGKILL);
177
178 /* If in user mode, send SIGILL signal to current process */
179 printk("Caught unhandled exception in '%s' "
180 "(pid = %d, pc = %#010lx) - should not happen\n"
181 "\tEXCCAUSE is %ld\n",
19c5870c 182 current->comm, task_pid_nr(current), regs->pc, exccause);
5a0015d6
CZ
183 force_sig(SIGILL, current);
184}
185
186/*
187 * Multi-hit exception. This if fatal!
188 */
189
190void do_multihit(struct pt_regs *regs, unsigned long exccause)
191{
192 die("Caught multihit exception", regs, SIGKILL);
193}
194
195/*
2d1c645c 196 * IRQ handler.
5a0015d6
CZ
197 */
198
5a0015d6
CZ
199extern void do_IRQ(int, struct pt_regs *);
200
38fef73c
MF
201#if XTENSA_FAKE_NMI
202
e4629194
MF
203#define IS_POW2(v) (((v) & ((v) - 1)) == 0)
204
205#if !(PROFILING_INTLEVEL == XCHAL_EXCM_LEVEL && \
206 IS_POW2(XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL)))
207#warning "Fake NMI is requested for PMM, but there are other IRQs at or above its level."
208#warning "Fake NMI will be used, but there will be a bugcheck if one of those IRQs fire."
209
210static inline void check_valid_nmi(void)
211{
212 unsigned intread = get_sr(interrupt);
213 unsigned intenable = get_sr(intenable);
214
215 BUG_ON(intread & intenable &
216 ~(XTENSA_INTLEVEL_ANDBELOW_MASK(PROFILING_INTLEVEL) ^
217 XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL) ^
218 BIT(XCHAL_PROFILING_INTERRUPT)));
219}
220
221#else
222
223static inline void check_valid_nmi(void)
224{
225}
226
227#endif
228
38fef73c
MF
229irqreturn_t xtensa_pmu_irq_handler(int irq, void *dev_id);
230
231DEFINE_PER_CPU(unsigned long, nmi_count);
232
233void do_nmi(struct pt_regs *regs)
234{
235 struct pt_regs *old_regs;
236
237 if ((regs->ps & PS_INTLEVEL_MASK) < LOCKLEVEL)
238 trace_hardirqs_off();
239
240 old_regs = set_irq_regs(regs);
241 nmi_enter();
242 ++*this_cpu_ptr(&nmi_count);
e4629194 243 check_valid_nmi();
38fef73c
MF
244 xtensa_pmu_irq_handler(0, NULL);
245 nmi_exit();
246 set_irq_regs(old_regs);
247}
248#endif
249
2d1c645c 250void do_interrupt(struct pt_regs *regs)
5a0015d6 251{
2d1c645c
MG
252 static const unsigned int_level_mask[] = {
253 0,
254 XCHAL_INTLEVEL1_MASK,
255 XCHAL_INTLEVEL2_MASK,
256 XCHAL_INTLEVEL3_MASK,
257 XCHAL_INTLEVEL4_MASK,
258 XCHAL_INTLEVEL5_MASK,
259 XCHAL_INTLEVEL6_MASK,
260 XCHAL_INTLEVEL7_MASK,
261 };
7d5f6a9a 262 struct pt_regs *old_regs;
99623239 263
7d5f6a9a
MF
264 trace_hardirqs_off();
265
266 old_regs = set_irq_regs(regs);
99623239 267 irq_enter();
5a0015d6 268
2d1c645c
MG
269 for (;;) {
270 unsigned intread = get_sr(interrupt);
271 unsigned intenable = get_sr(intenable);
895666a9
MF
272 unsigned int_at_level = intread & intenable;
273 unsigned level;
274
275 for (level = LOCKLEVEL; level > 0; --level) {
276 if (int_at_level & int_level_mask[level]) {
277 int_at_level &= int_level_mask[level];
278 break;
279 }
280 }
2d1c645c 281
895666a9 282 if (level == 0)
99623239
MF
283 break;
284
285 do_IRQ(__ffs(int_at_level), regs);
5a0015d6 286 }
99623239
MF
287
288 irq_exit();
289 set_irq_regs(old_regs);
5a0015d6
CZ
290}
291
292/*
293 * Illegal instruction. Fatal if in kernel space.
294 */
295
296void
297do_illegal_instruction(struct pt_regs *regs)
298{
299 __die_if_kernel("Illegal instruction in kernel", regs, SIGKILL);
300
301 /* If in user mode, send SIGILL signal to current process. */
302
303 printk("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n",
19c5870c 304 current->comm, task_pid_nr(current), regs->pc);
5a0015d6
CZ
305 force_sig(SIGILL, current);
306}
307
308
309/*
310 * Handle unaligned memory accesses from user space. Kill task.
311 *
312 * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory
313 * accesses causes from user space.
314 */
315
316#if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
5a0015d6
CZ
317void
318do_unaligned_user (struct pt_regs *regs)
319{
320 siginfo_t info;
321
322 __die_if_kernel("Unhandled unaligned exception in kernel",
323 regs, SIGKILL);
324
325 current->thread.bad_vaddr = regs->excvaddr;
326 current->thread.error_code = -3;
327 printk("Unaligned memory access to %08lx in '%s' "
328 "(pid = %d, pc = %#010lx)\n",
19c5870c 329 regs->excvaddr, current->comm, task_pid_nr(current), regs->pc);
5a0015d6
CZ
330 info.si_signo = SIGBUS;
331 info.si_errno = 0;
332 info.si_code = BUS_ADRALN;
333 info.si_addr = (void *) regs->excvaddr;
334 force_sig_info(SIGSEGV, &info, current);
335
336}
337#endif
5a0015d6
CZ
338
339void
340do_debug(struct pt_regs *regs)
341{
5a0015d6
CZ
342 __die_if_kernel("Breakpoint in kernel", regs, SIGKILL);
343
344 /* If in user mode, send SIGTRAP signal to current process */
345
346 force_sig(SIGTRAP, current);
347}
348
349
f615136c
MF
350static void set_handler(int idx, void *handler)
351{
352 unsigned int cpu;
353
354 for_each_possible_cpu(cpu)
355 per_cpu(exc_table, cpu)[idx] = (unsigned long)handler;
356}
357
28570e8d
MF
358/* Set exception C handler - for temporary use when probing exceptions */
359
360void * __init trap_set_handler(int cause, void *handler)
361{
f615136c
MF
362 void *previous = (void *)per_cpu(exc_table, 0)[
363 EXC_TABLE_DEFAULT / 4 + cause];
364 set_handler(EXC_TABLE_DEFAULT / 4 + cause, handler);
28570e8d
MF
365 return previous;
366}
367
368
49b424fe 369static void trap_init_excsave(void)
f615136c
MF
370{
371 unsigned long excsave1 = (unsigned long)this_cpu_ptr(exc_table);
372 __asm__ __volatile__("wsr %0, excsave1\n" : : "a" (excsave1));
373}
374
5a0015d6
CZ
375/*
376 * Initialize dispatch tables.
377 *
378 * The exception vectors are stored compressed the __init section in the
379 * dispatch_init_table. This function initializes the following three tables
380 * from that compressed table:
381 * - fast user first dispatch table for user exceptions
382 * - fast kernel first dispatch table for kernel exceptions
383 * - default C-handler C-handler called by the default fast handler.
384 *
385 * See vectors.S for more details.
386 */
387
b91dc336 388void __init trap_init(void)
5a0015d6
CZ
389{
390 int i;
391
392 /* Setup default vectors. */
393
394 for(i = 0; i < 64; i++) {
395 set_handler(EXC_TABLE_FAST_USER/4 + i, user_exception);
396 set_handler(EXC_TABLE_FAST_KERNEL/4 + i, kernel_exception);
397 set_handler(EXC_TABLE_DEFAULT/4 + i, do_unhandled);
398 }
399
400 /* Setup specific handlers. */
401
402 for(i = 0; dispatch_init_table[i].cause >= 0; i++) {
403
404 int fast = dispatch_init_table[i].fast;
405 int cause = dispatch_init_table[i].cause;
406 void *handler = dispatch_init_table[i].handler;
407
408 if (fast == 0)
409 set_handler (EXC_TABLE_DEFAULT/4 + cause, handler);
410 if (fast && fast & USER)
411 set_handler (EXC_TABLE_FAST_USER/4 + cause, handler);
412 if (fast && fast & KRNL)
413 set_handler (EXC_TABLE_FAST_KERNEL/4 + cause, handler);
414 }
415
416 /* Initialize EXCSAVE_1 to hold the address of the exception table. */
f615136c
MF
417 trap_init_excsave();
418}
5a0015d6 419
f615136c 420#ifdef CONFIG_SMP
49b424fe 421void secondary_trap_init(void)
f615136c
MF
422{
423 trap_init_excsave();
5a0015d6 424}
f615136c 425#endif
5a0015d6
CZ
426
427/*
428 * This function dumps the current valid window frame and other base registers.
429 */
430
431void show_regs(struct pt_regs * regs)
432{
433 int i, wmask;
434
a43cb95d
TH
435 show_regs_print_info(KERN_DEFAULT);
436
5a0015d6
CZ
437 wmask = regs->wmask & ~1;
438
8d7e8240 439 for (i = 0; i < 16; i++) {
5a0015d6 440 if ((i % 8) == 0)
ad361c98
JP
441 printk(KERN_INFO "a%02d:", i);
442 printk(KERN_CONT " %08lx", regs->areg[i]);
5a0015d6 443 }
ad361c98 444 printk(KERN_CONT "\n");
5a0015d6
CZ
445
446 printk("pc: %08lx, ps: %08lx, depc: %08lx, excvaddr: %08lx\n",
447 regs->pc, regs->ps, regs->depc, regs->excvaddr);
448 printk("lbeg: %08lx, lend: %08lx lcount: %08lx, sar: %08lx\n",
449 regs->lbeg, regs->lend, regs->lcount, regs->sar);
450 if (user_mode(regs))
451 printk("wb: %08lx, ws: %08lx, wmask: %08lx, syscall: %ld\n",
452 regs->windowbase, regs->windowstart, regs->wmask,
453 regs->syscall);
454}
455
3e4196a5 456static int show_trace_cb(struct stackframe *frame, void *data)
586411dc 457{
3e4196a5
MF
458 if (kernel_text_address(frame->pc)) {
459 printk(" [<%08lx>] ", frame->pc);
460 print_symbol("%s\n", frame->pc);
461 }
462 return 0;
586411dc
JW
463}
464
5a0015d6
CZ
465void show_trace(struct task_struct *task, unsigned long *sp)
466{
3e4196a5
MF
467 if (!sp)
468 sp = stack_pointer(task);
5a0015d6
CZ
469
470 printk("Call Trace:");
471#ifdef CONFIG_KALLSYMS
472 printk("\n");
473#endif
3e4196a5 474 walk_stackframe(sp, show_trace_cb, NULL);
5a0015d6
CZ
475 printk("\n");
476}
477
478/*
479 * This routine abuses get_user()/put_user() to reference pointers
480 * with at least a bit of error checking ...
481 */
482
483static int kstack_depth_to_print = 24;
484
485void show_stack(struct task_struct *task, unsigned long *sp)
486{
487 int i = 0;
488 unsigned long *stack;
489
28a0ce7f 490 if (!sp)
586411dc 491 sp = stack_pointer(task);
c4c4594b 492 stack = sp;
5a0015d6
CZ
493
494 printk("\nStack: ");
495
496 for (i = 0; i < kstack_depth_to_print; i++) {
497 if (kstack_end(sp))
498 break;
499 if (i && ((i % 8) == 0))
500 printk("\n ");
501 printk("%08lx ", *sp++);
502 }
503 printk("\n");
504 show_trace(task, stack);
505}
506
5a0015d6
CZ
507void show_code(unsigned int *pc)
508{
509 long i;
510
511 printk("\nCode:");
512
513 for(i = -3 ; i < 6 ; i++) {
514 unsigned long insn;
515 if (__get_user(insn, pc + i)) {
516 printk(" (Bad address in pc)\n");
517 break;
518 }
519 printk("%c%08lx%c",(i?' ':'<'),insn,(i?' ':'>'));
520 }
521}
522
34af946a 523DEFINE_SPINLOCK(die_lock);
5a0015d6
CZ
524
525void die(const char * str, struct pt_regs * regs, long err)
526{
527 static int die_counter;
528 int nl = 0;
529
530 console_verbose();
531 spin_lock_irq(&die_lock);
532
533 printk("%s: sig: %ld [#%d]\n", str, err, ++die_counter);
534#ifdef CONFIG_PREEMPT
535 printk("PREEMPT ");
536 nl = 1;
537#endif
538 if (nl)
539 printk("\n");
540 show_regs(regs);
541 if (!user_mode(regs))
542 show_stack(NULL, (unsigned long*)regs->areg[1]);
543
373d4d09 544 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
5a0015d6
CZ
545 spin_unlock_irq(&die_lock);
546
547 if (in_interrupt())
548 panic("Fatal exception in interrupt");
549
cea6a4ba 550 if (panic_on_oops)
012c437d 551 panic("Fatal exception");
cea6a4ba 552
5a0015d6
CZ
553 do_exit(err);
554}