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