1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
49
50
51extern void kernel_exception(void);
52extern void user_exception(void);
53
54extern void fast_illegal_instruction_user(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_nmi(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
73
74
75
76
77#define KRNL 0x01
78#define USER 0x02
79
80#define COPROCESSOR(x) \
81{ EXCCAUSE_COPROCESSOR ## x ## _DISABLED, USER, fast_coprocessor }
82
83typedef struct {
84 int cause;
85 int fast;
86 void* handler;
87} dispatch_init_table_t;
88
89static 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
98
99{ EXCCAUSE_LEVEL1_INTERRUPT, 0, do_interrupt },
100#ifdef SUPPORT_WINDOWED
101{ EXCCAUSE_ALLOCA, USER|KRNL, fast_alloca },
102#endif
103
104
105#if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
106#ifdef CONFIG_XTENSA_UNALIGNED_USER
107{ EXCCAUSE_UNALIGNED, USER, fast_unaligned },
108#endif
109{ EXCCAUSE_UNALIGNED, 0, do_unaligned_user },
110{ EXCCAUSE_UNALIGNED, KRNL, fast_unaligned },
111#endif
112#ifdef CONFIG_MMU
113{ EXCCAUSE_ITLB_MISS, 0, do_page_fault },
114{ EXCCAUSE_ITLB_MISS, USER|KRNL, fast_second_level_miss},
115{ EXCCAUSE_ITLB_MULTIHIT, 0, do_multihit },
116{ EXCCAUSE_ITLB_PRIVILEGE, 0, do_page_fault },
117
118{ EXCCAUSE_FETCH_CACHE_ATTRIBUTE, 0, do_page_fault },
119{ EXCCAUSE_DTLB_MISS, USER|KRNL, fast_second_level_miss},
120{ EXCCAUSE_DTLB_MISS, 0, do_page_fault },
121{ EXCCAUSE_DTLB_MULTIHIT, 0, do_multihit },
122{ EXCCAUSE_DTLB_PRIVILEGE, 0, do_page_fault },
123
124{ EXCCAUSE_STORE_CACHE_ATTRIBUTE, USER|KRNL, fast_store_prohibited },
125{ EXCCAUSE_STORE_CACHE_ATTRIBUTE, 0, do_page_fault },
126{ EXCCAUSE_LOAD_CACHE_ATTRIBUTE, 0, do_page_fault },
127#endif
128
129#if XTENSA_HAVE_COPROCESSOR(0)
130COPROCESSOR(0),
131#endif
132#if XTENSA_HAVE_COPROCESSOR(1)
133COPROCESSOR(1),
134#endif
135#if XTENSA_HAVE_COPROCESSOR(2)
136COPROCESSOR(2),
137#endif
138#if XTENSA_HAVE_COPROCESSOR(3)
139COPROCESSOR(3),
140#endif
141#if XTENSA_HAVE_COPROCESSOR(4)
142COPROCESSOR(4),
143#endif
144#if XTENSA_HAVE_COPROCESSOR(5)
145COPROCESSOR(5),
146#endif
147#if XTENSA_HAVE_COPROCESSOR(6)
148COPROCESSOR(6),
149#endif
150#if XTENSA_HAVE_COPROCESSOR(7)
151COPROCESSOR(7),
152#endif
153#if XTENSA_FAKE_NMI
154{ EXCCAUSE_MAPPED_NMI, 0, do_nmi },
155#endif
156{ EXCCAUSE_MAPPED_DEBUG, 0, do_debug },
157{ -1, -1, 0 }
158
159};
160
161
162
163
164
165
166DEFINE_PER_CPU(struct exc_table, exc_table);
167DEFINE_PER_CPU(struct debug_table, debug_table);
168
169void die(const char*, struct pt_regs*, long);
170
171static inline void
172__die_if_kernel(const char *str, struct pt_regs *regs, long err)
173{
174 if (!user_mode(regs))
175 die(str, regs, err);
176}
177
178
179
180
181
182void do_unhandled(struct pt_regs *regs, unsigned long exccause)
183{
184 __die_if_kernel("Caught unhandled exception - should not happen",
185 regs, SIGKILL);
186
187
188 pr_info_ratelimited("Caught unhandled exception in '%s' "
189 "(pid = %d, pc = %#010lx) - should not happen\n"
190 "\tEXCCAUSE is %ld\n",
191 current->comm, task_pid_nr(current), regs->pc,
192 exccause);
193 force_sig(SIGILL);
194}
195
196
197
198
199
200void do_multihit(struct pt_regs *regs, unsigned long exccause)
201{
202 die("Caught multihit exception", regs, SIGKILL);
203}
204
205
206
207
208
209extern void do_IRQ(int, struct pt_regs *);
210
211#if XTENSA_FAKE_NMI
212
213#define IS_POW2(v) (((v) & ((v) - 1)) == 0)
214
215#if !(PROFILING_INTLEVEL == XCHAL_EXCM_LEVEL && \
216 IS_POW2(XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL)))
217#warning "Fake NMI is requested for PMM, but there are other IRQs at or above its level."
218#warning "Fake NMI will be used, but there will be a bugcheck if one of those IRQs fire."
219
220static inline void check_valid_nmi(void)
221{
222 unsigned intread = xtensa_get_sr(interrupt);
223 unsigned intenable = xtensa_get_sr(intenable);
224
225 BUG_ON(intread & intenable &
226 ~(XTENSA_INTLEVEL_ANDBELOW_MASK(PROFILING_INTLEVEL) ^
227 XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL) ^
228 BIT(XCHAL_PROFILING_INTERRUPT)));
229}
230
231#else
232
233static inline void check_valid_nmi(void)
234{
235}
236
237#endif
238
239irqreturn_t xtensa_pmu_irq_handler(int irq, void *dev_id);
240
241DEFINE_PER_CPU(unsigned long, nmi_count);
242
243void do_nmi(struct pt_regs *regs)
244{
245 struct pt_regs *old_regs;
246
247 if ((regs->ps & PS_INTLEVEL_MASK) < LOCKLEVEL)
248 trace_hardirqs_off();
249
250 old_regs = set_irq_regs(regs);
251 nmi_enter();
252 ++*this_cpu_ptr(&nmi_count);
253 check_valid_nmi();
254 xtensa_pmu_irq_handler(0, NULL);
255 nmi_exit();
256 set_irq_regs(old_regs);
257}
258#endif
259
260void do_interrupt(struct pt_regs *regs)
261{
262 static const unsigned int_level_mask[] = {
263 0,
264 XCHAL_INTLEVEL1_MASK,
265 XCHAL_INTLEVEL2_MASK,
266 XCHAL_INTLEVEL3_MASK,
267 XCHAL_INTLEVEL4_MASK,
268 XCHAL_INTLEVEL5_MASK,
269 XCHAL_INTLEVEL6_MASK,
270 XCHAL_INTLEVEL7_MASK,
271 };
272 struct pt_regs *old_regs;
273 unsigned unhandled = ~0u;
274
275 trace_hardirqs_off();
276
277 old_regs = set_irq_regs(regs);
278 irq_enter();
279
280 for (;;) {
281 unsigned intread = xtensa_get_sr(interrupt);
282 unsigned intenable = xtensa_get_sr(intenable);
283 unsigned int_at_level = intread & intenable;
284 unsigned level;
285
286 for (level = LOCKLEVEL; level > 0; --level) {
287 if (int_at_level & int_level_mask[level]) {
288 int_at_level &= int_level_mask[level];
289 if (int_at_level & unhandled)
290 int_at_level &= unhandled;
291 else
292 unhandled |= int_level_mask[level];
293 break;
294 }
295 }
296
297 if (level == 0)
298 break;
299
300
301 unhandled ^= (int_at_level & -int_at_level);
302 do_IRQ(__ffs(int_at_level), regs);
303 }
304
305 irq_exit();
306 set_irq_regs(old_regs);
307}
308
309
310
311
312
313void
314do_illegal_instruction(struct pt_regs *regs)
315{
316 __die_if_kernel("Illegal instruction in kernel", regs, SIGKILL);
317
318
319
320 pr_info_ratelimited("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n",
321 current->comm, task_pid_nr(current), regs->pc);
322 force_sig(SIGILL);
323}
324
325
326
327
328
329
330
331
332
333#if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
334void
335do_unaligned_user (struct pt_regs *regs)
336{
337 __die_if_kernel("Unhandled unaligned exception in kernel",
338 regs, SIGKILL);
339
340 current->thread.bad_vaddr = regs->excvaddr;
341 current->thread.error_code = -3;
342 pr_info_ratelimited("Unaligned memory access to %08lx in '%s' "
343 "(pid = %d, pc = %#010lx)\n",
344 regs->excvaddr, current->comm,
345 task_pid_nr(current), regs->pc);
346 force_sig_fault(SIGBUS, BUS_ADRALN, (void *) regs->excvaddr);
347}
348#endif
349
350
351
352
353
354
355
356void
357do_debug(struct pt_regs *regs)
358{
359#ifdef CONFIG_HAVE_HW_BREAKPOINT
360 int ret = check_hw_breakpoint(regs);
361
362 preempt_enable();
363 if (ret == 0)
364 return;
365#endif
366 __die_if_kernel("Breakpoint in kernel", regs, SIGKILL);
367
368
369
370 force_sig(SIGTRAP);
371}
372
373
374#define set_handler(type, cause, handler) \
375 do { \
376 unsigned int cpu; \
377 \
378 for_each_possible_cpu(cpu) \
379 per_cpu(exc_table, cpu).type[cause] = (handler);\
380 } while (0)
381
382
383
384void * __init trap_set_handler(int cause, void *handler)
385{
386 void *previous = per_cpu(exc_table, 0).default_handler[cause];
387
388 set_handler(default_handler, cause, handler);
389 return previous;
390}
391
392
393static void trap_init_excsave(void)
394{
395 unsigned long excsave1 = (unsigned long)this_cpu_ptr(&exc_table);
396 __asm__ __volatile__("wsr %0, excsave1\n" : : "a" (excsave1));
397}
398
399static void trap_init_debug(void)
400{
401 unsigned long debugsave = (unsigned long)this_cpu_ptr(&debug_table);
402
403 this_cpu_ptr(&debug_table)->debug_exception = debug_exception;
404 __asm__ __volatile__("wsr %0, excsave" __stringify(XCHAL_DEBUGLEVEL)
405 :: "a"(debugsave));
406}
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421void __init trap_init(void)
422{
423 int i;
424
425
426
427 for (i = 0; i < EXCCAUSE_N; i++) {
428 set_handler(fast_user_handler, i, user_exception);
429 set_handler(fast_kernel_handler, i, kernel_exception);
430 set_handler(default_handler, i, do_unhandled);
431 }
432
433
434
435 for(i = 0; dispatch_init_table[i].cause >= 0; i++) {
436 int fast = dispatch_init_table[i].fast;
437 int cause = dispatch_init_table[i].cause;
438 void *handler = dispatch_init_table[i].handler;
439
440 if (fast == 0)
441 set_handler(default_handler, cause, handler);
442 if ((fast & USER) != 0)
443 set_handler(fast_user_handler, cause, handler);
444 if ((fast & KRNL) != 0)
445 set_handler(fast_kernel_handler, cause, handler);
446 }
447
448
449 trap_init_excsave();
450 trap_init_debug();
451}
452
453#ifdef CONFIG_SMP
454void secondary_trap_init(void)
455{
456 trap_init_excsave();
457 trap_init_debug();
458}
459#endif
460
461
462
463
464
465void show_regs(struct pt_regs * regs)
466{
467 int i;
468
469 show_regs_print_info(KERN_DEFAULT);
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
487static 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
497static 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
509static size_t kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH;
510
511void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl)
512{
513 size_t len;
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 print_hex_dump(loglvl, " ", DUMP_PREFIX_NONE,
523 STACK_DUMP_LINE_SIZE, STACK_DUMP_ENTRY_SIZE,
524 sp, len, false);
525 show_trace(task, sp, loglvl);
526}
527
528DEFINE_SPINLOCK(die_lock);
529
530void __noreturn die(const char * str, struct pt_regs * regs, long err)
531{
532 static int die_counter;
533 const char *pr = "";
534
535 if (IS_ENABLED(CONFIG_PREEMPTION))
536 pr = IS_ENABLED(CONFIG_PREEMPT_RT) ? " PREEMPT_RT" : " PREEMPT";
537
538 console_verbose();
539 spin_lock_irq(&die_lock);
540
541 pr_info("%s: sig: %ld [#%d]%s\n", str, err, ++die_counter, pr);
542 show_regs(regs);
543 if (!user_mode(regs))
544 show_stack(NULL, (unsigned long *)regs->areg[1], KERN_INFO);
545
546 add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
547 spin_unlock_irq(&die_lock);
548
549 if (in_interrupt())
550 panic("Fatal exception in interrupt");
551
552 if (panic_on_oops)
553 panic("Fatal exception");
554
555 make_task_dead(err);
556}
557