2 * arch/xtensa/kernel/traps.c
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.
12 * Essentially rewritten for the Xtensa architecture port.
14 * Copyright (C) 2001 - 2013 Tensilica Inc.
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>
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
26 #include <linux/kernel.h>
27 #include <linux/sched/signal.h>
28 #include <linux/sched/debug.h>
29 #include <linux/sched/task_stack.h>
30 #include <linux/init.h>
31 #include <linux/module.h>
32 #include <linux/stringify.h>
33 #include <linux/kallsyms.h>
34 #include <linux/delay.h>
35 #include <linux/hardirq.h>
36 #include <linux/ratelimit.h>
37 #include <linux/pgtable.h>
39 #include <asm/stacktrace.h>
40 #include <asm/ptrace.h>
41 #include <asm/timex.h>
42 #include <linux/uaccess.h>
43 #include <asm/processor.h>
44 #include <asm/traps.h>
45 #include <asm/hw_breakpoint.h>
48 * Machine specific interrupt handlers
51 static void do_illegal_instruction(struct pt_regs *regs);
52 static void do_div0(struct pt_regs *regs);
53 static void do_interrupt(struct pt_regs *regs);
55 static void do_nmi(struct pt_regs *regs);
57 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
58 static void do_unaligned_user(struct pt_regs *regs);
60 static void do_multihit(struct pt_regs *regs);
61 #if XTENSA_HAVE_COPROCESSORS
62 static void do_coprocessor(struct pt_regs *regs);
64 static void do_debug(struct pt_regs *regs);
67 * The vector table must be preceded by a save area (which
68 * implies it must be in RAM, unless one places RAM immediately
69 * before a ROM and puts the vector at the start of the ROM (!))
75 #define COPROCESSOR(x) \
76 { EXCCAUSE_COPROCESSOR ## x ## _DISABLED, USER|KRNL, fast_coprocessor },\
77 { EXCCAUSE_COPROCESSOR ## x ## _DISABLED, 0, do_coprocessor }
83 } dispatch_init_table_t;
85 static dispatch_init_table_t __initdata dispatch_init_table[] = {
87 #ifdef CONFIG_USER_ABI_CALL0_PROBE
88 { EXCCAUSE_ILLEGAL_INSTRUCTION, USER, fast_illegal_instruction_user },
90 { EXCCAUSE_ILLEGAL_INSTRUCTION, 0, do_illegal_instruction},
91 { EXCCAUSE_SYSTEM_CALL, USER, fast_syscall_user },
92 { EXCCAUSE_SYSTEM_CALL, 0, system_call },
93 /* EXCCAUSE_INSTRUCTION_FETCH unhandled */
94 /* EXCCAUSE_LOAD_STORE_ERROR unhandled*/
95 { EXCCAUSE_LEVEL1_INTERRUPT, 0, do_interrupt },
96 #ifdef SUPPORT_WINDOWED
97 { EXCCAUSE_ALLOCA, USER|KRNL, fast_alloca },
99 { EXCCAUSE_INTEGER_DIVIDE_BY_ZERO, 0, do_div0 },
100 /* EXCCAUSE_PRIVILEGED unhandled */
101 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
102 #ifdef CONFIG_XTENSA_UNALIGNED_USER
103 { EXCCAUSE_UNALIGNED, USER, fast_unaligned },
105 { EXCCAUSE_UNALIGNED, 0, do_unaligned_user },
106 { EXCCAUSE_UNALIGNED, KRNL, fast_unaligned },
109 { EXCCAUSE_ITLB_MISS, 0, do_page_fault },
110 { EXCCAUSE_ITLB_MISS, USER|KRNL, fast_second_level_miss},
111 { EXCCAUSE_DTLB_MISS, USER|KRNL, fast_second_level_miss},
112 { EXCCAUSE_DTLB_MISS, 0, do_page_fault },
113 { EXCCAUSE_STORE_CACHE_ATTRIBUTE, USER|KRNL, fast_store_prohibited },
114 #endif /* CONFIG_MMU */
116 { EXCCAUSE_ITLB_MULTIHIT, 0, do_multihit },
117 { EXCCAUSE_ITLB_PRIVILEGE, 0, do_page_fault },
118 { EXCCAUSE_FETCH_CACHE_ATTRIBUTE, 0, do_page_fault },
119 { EXCCAUSE_DTLB_MULTIHIT, 0, do_multihit },
120 { EXCCAUSE_DTLB_PRIVILEGE, 0, do_page_fault },
121 { EXCCAUSE_STORE_CACHE_ATTRIBUTE, 0, do_page_fault },
122 { EXCCAUSE_LOAD_CACHE_ATTRIBUTE, 0, do_page_fault },
124 /* XCCHAL_EXCCAUSE_FLOATING_POINT unhandled */
125 #if XTENSA_HAVE_COPROCESSOR(0)
128 #if XTENSA_HAVE_COPROCESSOR(1)
131 #if XTENSA_HAVE_COPROCESSOR(2)
134 #if XTENSA_HAVE_COPROCESSOR(3)
137 #if XTENSA_HAVE_COPROCESSOR(4)
140 #if XTENSA_HAVE_COPROCESSOR(5)
143 #if XTENSA_HAVE_COPROCESSOR(6)
146 #if XTENSA_HAVE_COPROCESSOR(7)
150 { EXCCAUSE_MAPPED_NMI, 0, do_nmi },
152 { EXCCAUSE_MAPPED_DEBUG, 0, do_debug },
157 /* The exception table <exc_table> serves two functions:
158 * 1. it contains three dispatch tables (fast_user, fast_kernel, default-c)
159 * 2. it is a temporary memory buffer for the exception handlers.
162 DEFINE_PER_CPU(struct exc_table, exc_table);
163 DEFINE_PER_CPU(struct debug_table, debug_table);
165 void die(const char*, struct pt_regs*, long);
168 __die_if_kernel(const char *str, struct pt_regs *regs, long err)
170 if (!user_mode(regs))
175 * Unhandled Exceptions. Kill user task or panic if in kernel space.
178 void do_unhandled(struct pt_regs *regs)
180 __die_if_kernel("Caught unhandled exception - should not happen",
183 /* If in user mode, send SIGILL signal to current process */
184 pr_info_ratelimited("Caught unhandled exception in '%s' "
185 "(pid = %d, pc = %#010lx) - should not happen\n"
186 "\tEXCCAUSE is %ld\n",
187 current->comm, task_pid_nr(current), regs->pc,
193 * Multi-hit exception. This if fatal!
196 static void do_multihit(struct pt_regs *regs)
198 die("Caught multihit exception", regs, SIGKILL);
207 #define IS_POW2(v) (((v) & ((v) - 1)) == 0)
209 #if !(PROFILING_INTLEVEL == XCHAL_EXCM_LEVEL && \
210 IS_POW2(XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL)))
211 #warning "Fake NMI is requested for PMM, but there are other IRQs at or above its level."
212 #warning "Fake NMI will be used, but there will be a bugcheck if one of those IRQs fire."
214 static inline void check_valid_nmi(void)
216 unsigned intread = xtensa_get_sr(interrupt);
217 unsigned intenable = xtensa_get_sr(intenable);
219 BUG_ON(intread & intenable &
220 ~(XTENSA_INTLEVEL_ANDBELOW_MASK(PROFILING_INTLEVEL) ^
221 XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL) ^
222 BIT(XCHAL_PROFILING_INTERRUPT)));
227 static inline void check_valid_nmi(void)
233 irqreturn_t xtensa_pmu_irq_handler(int irq, void *dev_id);
235 DEFINE_PER_CPU(unsigned long, nmi_count);
237 static void do_nmi(struct pt_regs *regs)
239 struct pt_regs *old_regs = set_irq_regs(regs);
242 ++*this_cpu_ptr(&nmi_count);
244 xtensa_pmu_irq_handler(0, NULL);
246 set_irq_regs(old_regs);
250 static void do_interrupt(struct pt_regs *regs)
252 static const unsigned int_level_mask[] = {
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,
262 struct pt_regs *old_regs = set_irq_regs(regs);
263 unsigned unhandled = ~0u;
268 unsigned intread = xtensa_get_sr(interrupt);
269 unsigned intenable = xtensa_get_sr(intenable);
270 unsigned int_at_level = intread & intenable;
273 for (level = LOCKLEVEL; level > 0; --level) {
274 if (int_at_level & int_level_mask[level]) {
275 int_at_level &= int_level_mask[level];
276 if (int_at_level & unhandled)
277 int_at_level &= unhandled;
279 unhandled |= int_level_mask[level];
287 /* clear lowest pending irq in the unhandled mask */
288 unhandled ^= (int_at_level & -int_at_level);
289 do_IRQ(__ffs(int_at_level), regs);
293 set_irq_regs(old_regs);
296 static bool check_div0(struct pt_regs *regs)
298 static const u8 pattern[] = {'D', 'I', 'V', '0'};
302 if (user_mode(regs)) {
303 if (copy_from_user(buf, (void __user *)regs->pc + 2, 5))
307 p = (const u8 *)regs->pc + 2;
310 return memcmp(p, pattern, sizeof(pattern)) == 0 ||
311 memcmp(p + 1, pattern, sizeof(pattern)) == 0;
315 * Illegal instruction. Fatal if in kernel space.
318 static void do_illegal_instruction(struct pt_regs *regs)
320 #ifdef CONFIG_USER_ABI_CALL0_PROBE
322 * When call0 application encounters an illegal instruction fast
323 * exception handler will attempt to set PS.WOE and retry failing
325 * If we get here we know that that instruction is also illegal
326 * with PS.WOE set, so it's not related to the windowed option
327 * hence PS.WOE may be cleared.
329 if (regs->pc == current_thread_info()->ps_woe_fix_addr)
330 regs->ps &= ~PS_WOE_MASK;
332 if (check_div0(regs)) {
337 __die_if_kernel("Illegal instruction in kernel", regs, SIGKILL);
339 /* If in user mode, send SIGILL signal to current process. */
341 pr_info_ratelimited("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n",
342 current->comm, task_pid_nr(current), regs->pc);
346 static void do_div0(struct pt_regs *regs)
348 __die_if_kernel("Unhandled division by 0 in kernel", regs, SIGKILL);
349 force_sig_fault(SIGFPE, FPE_INTDIV, (void __user *)regs->pc);
353 * Handle unaligned memory accesses from user space. Kill task.
355 * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory
356 * accesses causes from user space.
359 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
360 static void do_unaligned_user(struct pt_regs *regs)
362 __die_if_kernel("Unhandled unaligned exception in kernel",
365 current->thread.bad_vaddr = regs->excvaddr;
366 current->thread.error_code = -3;
367 pr_info_ratelimited("Unaligned memory access to %08lx in '%s' "
368 "(pid = %d, pc = %#010lx)\n",
369 regs->excvaddr, current->comm,
370 task_pid_nr(current), regs->pc);
371 force_sig_fault(SIGBUS, BUS_ADRALN, (void *) regs->excvaddr);
375 #if XTENSA_HAVE_COPROCESSORS
376 static void do_coprocessor(struct pt_regs *regs)
378 coprocessor_flush_release_all(current_thread_info());
382 /* Handle debug events.
383 * When CONFIG_HAVE_HW_BREAKPOINT is on this handler is called with
384 * preemption disabled to avoid rescheduling and keep mapping of hardware
385 * breakpoint structures to debug registers intact, so that
386 * DEBUGCAUSE.DBNUM could be used in case of data breakpoint hit.
388 static void do_debug(struct pt_regs *regs)
390 #ifdef CONFIG_HAVE_HW_BREAKPOINT
391 int ret = check_hw_breakpoint(regs);
397 __die_if_kernel("Breakpoint in kernel", regs, SIGKILL);
399 /* If in user mode, send SIGTRAP signal to current process */
405 #define set_handler(type, cause, handler) \
409 for_each_possible_cpu(cpu) \
410 per_cpu(exc_table, cpu).type[cause] = (handler);\
413 /* Set exception C handler - for temporary use when probing exceptions */
415 xtensa_exception_handler *
416 __init trap_set_handler(int cause, xtensa_exception_handler *handler)
418 void *previous = per_cpu(exc_table, 0).default_handler[cause];
420 set_handler(default_handler, cause, handler);
425 static void trap_init_excsave(void)
427 xtensa_set_sr(this_cpu_ptr(&exc_table), excsave1);
430 static void trap_init_debug(void)
432 unsigned long debugsave = (unsigned long)this_cpu_ptr(&debug_table);
434 this_cpu_ptr(&debug_table)->debug_exception = debug_exception;
435 __asm__ __volatile__("wsr %0, excsave" __stringify(XCHAL_DEBUGLEVEL)
440 * Initialize dispatch tables.
442 * The exception vectors are stored compressed the __init section in the
443 * dispatch_init_table. This function initializes the following three tables
444 * from that compressed table:
445 * - fast user first dispatch table for user exceptions
446 * - fast kernel first dispatch table for kernel exceptions
447 * - default C-handler C-handler called by the default fast handler.
449 * See vectors.S for more details.
452 void __init trap_init(void)
456 /* Setup default vectors. */
458 for (i = 0; i < EXCCAUSE_N; i++) {
459 set_handler(fast_user_handler, i, user_exception);
460 set_handler(fast_kernel_handler, i, kernel_exception);
461 set_handler(default_handler, i, do_unhandled);
464 /* Setup specific handlers. */
466 for(i = 0; dispatch_init_table[i].cause >= 0; i++) {
467 int fast = dispatch_init_table[i].fast;
468 int cause = dispatch_init_table[i].cause;
469 void *handler = dispatch_init_table[i].handler;
472 set_handler(default_handler, cause, handler);
473 if ((fast & USER) != 0)
474 set_handler(fast_user_handler, cause, handler);
475 if ((fast & KRNL) != 0)
476 set_handler(fast_kernel_handler, cause, handler);
479 /* Initialize EXCSAVE_1 to hold the address of the exception table. */
485 void secondary_trap_init(void)
493 * This function dumps the current valid window frame and other base registers.
496 void show_regs(struct pt_regs * regs)
500 show_regs_print_info(KERN_DEFAULT);
502 for (i = 0; i < 16; i++) {
504 pr_info("a%02d:", i);
505 pr_cont(" %08lx", regs->areg[i]);
508 pr_info("pc: %08lx, ps: %08lx, depc: %08lx, excvaddr: %08lx\n",
509 regs->pc, regs->ps, regs->depc, regs->excvaddr);
510 pr_info("lbeg: %08lx, lend: %08lx lcount: %08lx, sar: %08lx\n",
511 regs->lbeg, regs->lend, regs->lcount, regs->sar);
513 pr_cont("wb: %08lx, ws: %08lx, wmask: %08lx, syscall: %ld\n",
514 regs->windowbase, regs->windowstart, regs->wmask,
518 static int show_trace_cb(struct stackframe *frame, void *data)
520 const char *loglvl = data;
522 if (kernel_text_address(frame->pc))
523 printk("%s [<%08lx>] %pB\n",
524 loglvl, frame->pc, (void *)frame->pc);
528 static void show_trace(struct task_struct *task, unsigned long *sp,
532 sp = stack_pointer(task);
534 printk("%sCall Trace:\n", loglvl);
535 walk_stackframe(sp, show_trace_cb, (void *)loglvl);
538 #define STACK_DUMP_ENTRY_SIZE 4
539 #define STACK_DUMP_LINE_SIZE 32
540 static size_t kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
542 void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl)
547 sp = stack_pointer(task);
549 len = min((-(size_t)sp) & (THREAD_SIZE - STACK_DUMP_ENTRY_SIZE),
550 kstack_depth_to_print * STACK_DUMP_ENTRY_SIZE);
552 printk("%sStack:\n", loglvl);
553 print_hex_dump(loglvl, " ", DUMP_PREFIX_NONE,
554 STACK_DUMP_LINE_SIZE, STACK_DUMP_ENTRY_SIZE,
556 show_trace(task, sp, loglvl);
559 DEFINE_SPINLOCK(die_lock);
561 void __noreturn die(const char * str, struct pt_regs * regs, long err)
563 static int die_counter;
566 if (IS_ENABLED(CONFIG_PREEMPTION))
567 pr = IS_ENABLED(CONFIG_PREEMPT_RT) ? " PREEMPT_RT" : " PREEMPT";
570 spin_lock_irq(&die_lock);
572 pr_info("%s: sig: %ld [#%d]%s\n", str, err, ++die_counter, pr);
574 if (!user_mode(regs))
575 show_stack(NULL, (unsigned long *)regs->areg[1], KERN_INFO);
577 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
578 spin_unlock_irq(&die_lock);
581 panic("Fatal exception in interrupt");
584 panic("Fatal exception");