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