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