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