15a0015d6SChris Zankel /* 25a0015d6SChris Zankel * arch/xtensa/kernel/traps.c 35a0015d6SChris Zankel * 45a0015d6SChris Zankel * Exception handling. 55a0015d6SChris Zankel * 65a0015d6SChris Zankel * Derived from code with the following copyrights: 75a0015d6SChris Zankel * Copyright (C) 1994 - 1999 by Ralf Baechle 85a0015d6SChris Zankel * Modified for R3000 by Paul M. Antoine, 1995, 1996 95a0015d6SChris Zankel * Complete output from die() by Ulf Carlsson, 1998 105a0015d6SChris Zankel * Copyright (C) 1999 Silicon Graphics, Inc. 115a0015d6SChris Zankel * 125a0015d6SChris Zankel * Essentially rewritten for the Xtensa architecture port. 135a0015d6SChris Zankel * 143e4196a5SMax Filippov * Copyright (C) 2001 - 2013 Tensilica Inc. 155a0015d6SChris Zankel * 165a0015d6SChris Zankel * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com> 175a0015d6SChris Zankel * Chris Zankel <chris@zankel.net> 185a0015d6SChris Zankel * Marc Gauthier<marc@tensilica.com, marc@alumni.uwaterloo.ca> 195a0015d6SChris Zankel * Kevin Chea 205a0015d6SChris Zankel * 215a0015d6SChris Zankel * This file is subject to the terms and conditions of the GNU General Public 225a0015d6SChris Zankel * License. See the file "COPYING" in the main directory of this archive 235a0015d6SChris Zankel * for more details. 245a0015d6SChris Zankel */ 255a0015d6SChris Zankel 265a0015d6SChris Zankel #include <linux/kernel.h> 273f07c014SIngo Molnar #include <linux/sched/signal.h> 28b17b0153SIngo Molnar #include <linux/sched/debug.h> 293f8c2452SIngo Molnar #include <linux/sched/task_stack.h> 305a0015d6SChris Zankel #include <linux/init.h> 315a0015d6SChris Zankel #include <linux/module.h> 325a0015d6SChris Zankel #include <linux/stringify.h> 335a0015d6SChris Zankel #include <linux/kallsyms.h> 345c888d53SNishanth Aravamudan #include <linux/delay.h> 355a891ed5SAlexey Dobriyan #include <linux/hardirq.h> 36c130d3beSMax Filippov #include <linux/ratelimit.h> 3765fddcfcSMike Rapoport #include <linux/pgtable.h> 385a0015d6SChris Zankel 393e4196a5SMax Filippov #include <asm/stacktrace.h> 405a0015d6SChris Zankel #include <asm/ptrace.h> 415a0015d6SChris Zankel #include <asm/timex.h> 427c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 435a0015d6SChris Zankel #include <asm/processor.h> 442d6f82feSMax Filippov #include <asm/traps.h> 45c91e02bdSMax Filippov #include <asm/hw_breakpoint.h> 465a0015d6SChris Zankel 475a0015d6SChris Zankel /* 485a0015d6SChris Zankel * Machine specific interrupt handlers 495a0015d6SChris Zankel */ 505a0015d6SChris Zankel 51db0d07faSMax Filippov static void do_illegal_instruction(struct pt_regs *regs); 52408b1d3cSMax Filippov static void do_div0(struct pt_regs *regs); 53db0d07faSMax Filippov static void do_interrupt(struct pt_regs *regs); 54db0d07faSMax Filippov #if XTENSA_FAKE_NMI 55db0d07faSMax Filippov static void do_nmi(struct pt_regs *regs); 56db0d07faSMax Filippov #endif 57db0d07faSMax Filippov #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION 58db0d07faSMax Filippov static void do_unaligned_user(struct pt_regs *regs); 59db0d07faSMax Filippov #endif 60db0d07faSMax Filippov static void do_multihit(struct pt_regs *regs); 6111e969bcSMax Filippov #if XTENSA_HAVE_COPROCESSORS 6211e969bcSMax Filippov static void do_coprocessor(struct pt_regs *regs); 6311e969bcSMax Filippov #endif 64db0d07faSMax Filippov static void do_debug(struct pt_regs *regs); 655a0015d6SChris Zankel 665a0015d6SChris Zankel /* 675a0015d6SChris Zankel * The vector table must be preceded by a save area (which 685a0015d6SChris Zankel * implies it must be in RAM, unless one places RAM immediately 695a0015d6SChris Zankel * before a ROM and puts the vector at the start of the ROM (!)) 705a0015d6SChris Zankel */ 715a0015d6SChris Zankel 725a0015d6SChris Zankel #define KRNL 0x01 735a0015d6SChris Zankel #define USER 0x02 745a0015d6SChris Zankel 755a0015d6SChris Zankel #define COPROCESSOR(x) \ 7611e969bcSMax Filippov { EXCCAUSE_COPROCESSOR ## x ## _DISABLED, USER|KRNL, fast_coprocessor },\ 7711e969bcSMax Filippov { EXCCAUSE_COPROCESSOR ## x ## _DISABLED, 0, do_coprocessor } 785a0015d6SChris Zankel 795a0015d6SChris Zankel typedef struct { 805a0015d6SChris Zankel int cause; 815a0015d6SChris Zankel int fast; 825a0015d6SChris Zankel void* handler; 835a0015d6SChris Zankel } dispatch_init_table_t; 845a0015d6SChris Zankel 85b91dc336SChris Zankel static dispatch_init_table_t __initdata dispatch_init_table[] = { 865a0015d6SChris Zankel 8709f8a6dbSMax Filippov #ifdef CONFIG_USER_ABI_CALL0_PROBE 8809f8a6dbSMax Filippov { EXCCAUSE_ILLEGAL_INSTRUCTION, USER, fast_illegal_instruction_user }, 8909f8a6dbSMax Filippov #endif 90173d6681SChris Zankel { EXCCAUSE_ILLEGAL_INSTRUCTION, 0, do_illegal_instruction}, 91173d6681SChris Zankel { EXCCAUSE_SYSTEM_CALL, USER, fast_syscall_user }, 92173d6681SChris Zankel { EXCCAUSE_SYSTEM_CALL, 0, system_call }, 93173d6681SChris Zankel /* EXCCAUSE_INSTRUCTION_FETCH unhandled */ 94173d6681SChris Zankel /* EXCCAUSE_LOAD_STORE_ERROR unhandled*/ 95173d6681SChris Zankel { EXCCAUSE_LEVEL1_INTERRUPT, 0, do_interrupt }, 96da0a4e5cSMax Filippov #ifdef SUPPORT_WINDOWED 97173d6681SChris Zankel { EXCCAUSE_ALLOCA, USER|KRNL, fast_alloca }, 98da0a4e5cSMax Filippov #endif 99408b1d3cSMax Filippov { EXCCAUSE_INTEGER_DIVIDE_BY_ZERO, 0, do_div0 }, 100173d6681SChris Zankel /* EXCCAUSE_PRIVILEGED unhandled */ 1015a0015d6SChris Zankel #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION 1024ded6282SMax Filippov #ifdef CONFIG_XTENSA_UNALIGNED_USER 103173d6681SChris Zankel { EXCCAUSE_UNALIGNED, USER, fast_unaligned }, 1045a0015d6SChris Zankel #endif 1053cfc096eSMax Filippov { EXCCAUSE_UNALIGNED, 0, do_unaligned_user }, 106173d6681SChris Zankel { EXCCAUSE_UNALIGNED, KRNL, fast_unaligned }, 1075a0015d6SChris Zankel #endif 108e5083a63SJohannes Weiner #ifdef CONFIG_MMU 109173d6681SChris Zankel { EXCCAUSE_ITLB_MISS, 0, do_page_fault }, 110173d6681SChris Zankel { EXCCAUSE_ITLB_MISS, USER|KRNL, fast_second_level_miss}, 111173d6681SChris Zankel { EXCCAUSE_DTLB_MISS, USER|KRNL, fast_second_level_miss}, 112173d6681SChris Zankel { EXCCAUSE_DTLB_MISS, 0, do_page_fault }, 113a8f0c31fSMax Filippov { EXCCAUSE_STORE_CACHE_ATTRIBUTE, USER|KRNL, fast_store_prohibited }, 114a8f0c31fSMax Filippov #endif /* CONFIG_MMU */ 115a8f0c31fSMax Filippov #ifdef CONFIG_PFAULT 116a8f0c31fSMax Filippov { EXCCAUSE_ITLB_MULTIHIT, 0, do_multihit }, 117a8f0c31fSMax Filippov { EXCCAUSE_ITLB_PRIVILEGE, 0, do_page_fault }, 118a8f0c31fSMax Filippov { EXCCAUSE_FETCH_CACHE_ATTRIBUTE, 0, do_page_fault }, 119173d6681SChris Zankel { EXCCAUSE_DTLB_MULTIHIT, 0, do_multihit }, 120173d6681SChris Zankel { EXCCAUSE_DTLB_PRIVILEGE, 0, do_page_fault }, 121173d6681SChris Zankel { EXCCAUSE_STORE_CACHE_ATTRIBUTE, 0, do_page_fault }, 122173d6681SChris Zankel { EXCCAUSE_LOAD_CACHE_ATTRIBUTE, 0, do_page_fault }, 123a8f0c31fSMax Filippov #endif 1245a0015d6SChris Zankel /* XCCHAL_EXCCAUSE_FLOATING_POINT unhandled */ 125c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(0) 1265a0015d6SChris Zankel COPROCESSOR(0), 1275a0015d6SChris Zankel #endif 128c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(1) 1295a0015d6SChris Zankel COPROCESSOR(1), 1305a0015d6SChris Zankel #endif 131c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(2) 1325a0015d6SChris Zankel COPROCESSOR(2), 1335a0015d6SChris Zankel #endif 134c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(3) 1355a0015d6SChris Zankel COPROCESSOR(3), 1365a0015d6SChris Zankel #endif 137c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(4) 1385a0015d6SChris Zankel COPROCESSOR(4), 1395a0015d6SChris Zankel #endif 140c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(5) 1415a0015d6SChris Zankel COPROCESSOR(5), 1425a0015d6SChris Zankel #endif 143c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(6) 1445a0015d6SChris Zankel COPROCESSOR(6), 1455a0015d6SChris Zankel #endif 146c658eac6SChris Zankel #if XTENSA_HAVE_COPROCESSOR(7) 1475a0015d6SChris Zankel COPROCESSOR(7), 1485a0015d6SChris Zankel #endif 14938fef73cSMax Filippov #if XTENSA_FAKE_NMI 15038fef73cSMax Filippov { EXCCAUSE_MAPPED_NMI, 0, do_nmi }, 15138fef73cSMax Filippov #endif 1525a0015d6SChris Zankel { EXCCAUSE_MAPPED_DEBUG, 0, do_debug }, 1535a0015d6SChris Zankel { -1, -1, 0 } 1545a0015d6SChris Zankel 1555a0015d6SChris Zankel }; 1565a0015d6SChris Zankel 1575a0015d6SChris Zankel /* The exception table <exc_table> serves two functions: 1585a0015d6SChris Zankel * 1. it contains three dispatch tables (fast_user, fast_kernel, default-c) 1595a0015d6SChris Zankel * 2. it is a temporary memory buffer for the exception handlers. 1605a0015d6SChris Zankel */ 1615a0015d6SChris Zankel 162f21a79caSMax Filippov DEFINE_PER_CPU(struct exc_table, exc_table); 1636ec7026aSMax Filippov DEFINE_PER_CPU(struct debug_table, debug_table); 1646ec7026aSMax Filippov 1655a0015d6SChris Zankel void die(const char*, struct pt_regs*, long); 1665a0015d6SChris Zankel 1675a0015d6SChris Zankel static inline void 1685a0015d6SChris Zankel __die_if_kernel(const char *str, struct pt_regs *regs, long err) 1695a0015d6SChris Zankel { 1705a0015d6SChris Zankel if (!user_mode(regs)) 1715a0015d6SChris Zankel die(str, regs, err); 1725a0015d6SChris Zankel } 1735a0015d6SChris Zankel 1745a0015d6SChris Zankel /* 1755a0015d6SChris Zankel * Unhandled Exceptions. Kill user task or panic if in kernel space. 1765a0015d6SChris Zankel */ 1775a0015d6SChris Zankel 178fc55402bSMax Filippov void do_unhandled(struct pt_regs *regs) 1795a0015d6SChris Zankel { 1805a0015d6SChris Zankel __die_if_kernel("Caught unhandled exception - should not happen", 1815a0015d6SChris Zankel regs, SIGKILL); 1825a0015d6SChris Zankel 1835a0015d6SChris Zankel /* If in user mode, send SIGILL signal to current process */ 184c130d3beSMax Filippov pr_info_ratelimited("Caught unhandled exception in '%s' " 1855a0015d6SChris Zankel "(pid = %d, pc = %#010lx) - should not happen\n" 1865a0015d6SChris Zankel "\tEXCCAUSE is %ld\n", 187c130d3beSMax Filippov current->comm, task_pid_nr(current), regs->pc, 188fc55402bSMax Filippov regs->exccause); 1893cf5d076SEric W. Biederman force_sig(SIGILL); 1905a0015d6SChris Zankel } 1915a0015d6SChris Zankel 1925a0015d6SChris Zankel /* 1935a0015d6SChris Zankel * Multi-hit exception. This if fatal! 1945a0015d6SChris Zankel */ 1955a0015d6SChris Zankel 196db0d07faSMax Filippov static void do_multihit(struct pt_regs *regs) 1975a0015d6SChris Zankel { 1985a0015d6SChris Zankel die("Caught multihit exception", regs, SIGKILL); 1995a0015d6SChris Zankel } 2005a0015d6SChris Zankel 2015a0015d6SChris Zankel /* 2022d1c645cSMarc Gauthier * IRQ handler. 2035a0015d6SChris Zankel */ 2045a0015d6SChris Zankel 20538fef73cSMax Filippov #if XTENSA_FAKE_NMI 20638fef73cSMax Filippov 207e4629194SMax Filippov #define IS_POW2(v) (((v) & ((v) - 1)) == 0) 208e4629194SMax Filippov 209e4629194SMax Filippov #if !(PROFILING_INTLEVEL == XCHAL_EXCM_LEVEL && \ 210e4629194SMax Filippov IS_POW2(XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL))) 211e4629194SMax Filippov #warning "Fake NMI is requested for PMM, but there are other IRQs at or above its level." 212e4629194SMax Filippov #warning "Fake NMI will be used, but there will be a bugcheck if one of those IRQs fire." 213e4629194SMax Filippov 214e4629194SMax Filippov static inline void check_valid_nmi(void) 215e4629194SMax Filippov { 216cad6fadeSMax Filippov unsigned intread = xtensa_get_sr(interrupt); 217cad6fadeSMax Filippov unsigned intenable = xtensa_get_sr(intenable); 218e4629194SMax Filippov 219e4629194SMax Filippov BUG_ON(intread & intenable & 220e4629194SMax Filippov ~(XTENSA_INTLEVEL_ANDBELOW_MASK(PROFILING_INTLEVEL) ^ 221e4629194SMax Filippov XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL) ^ 222e4629194SMax Filippov BIT(XCHAL_PROFILING_INTERRUPT))); 223e4629194SMax Filippov } 224e4629194SMax Filippov 225e4629194SMax Filippov #else 226e4629194SMax Filippov 227e4629194SMax Filippov static inline void check_valid_nmi(void) 228e4629194SMax Filippov { 229e4629194SMax Filippov } 230e4629194SMax Filippov 231e4629194SMax Filippov #endif 232e4629194SMax Filippov 23338fef73cSMax Filippov irqreturn_t xtensa_pmu_irq_handler(int irq, void *dev_id); 23438fef73cSMax Filippov 23538fef73cSMax Filippov DEFINE_PER_CPU(unsigned long, nmi_count); 23638fef73cSMax Filippov 237db0d07faSMax Filippov static void do_nmi(struct pt_regs *regs) 23838fef73cSMax Filippov { 239de4415d0SMax Filippov struct pt_regs *old_regs = set_irq_regs(regs); 24038fef73cSMax Filippov 24138fef73cSMax Filippov nmi_enter(); 24238fef73cSMax Filippov ++*this_cpu_ptr(&nmi_count); 243e4629194SMax Filippov check_valid_nmi(); 24438fef73cSMax Filippov xtensa_pmu_irq_handler(0, NULL); 24538fef73cSMax Filippov nmi_exit(); 24638fef73cSMax Filippov set_irq_regs(old_regs); 24738fef73cSMax Filippov } 24838fef73cSMax Filippov #endif 24938fef73cSMax Filippov 250db0d07faSMax Filippov static void do_interrupt(struct pt_regs *regs) 2515a0015d6SChris Zankel { 2522d1c645cSMarc Gauthier static const unsigned int_level_mask[] = { 2532d1c645cSMarc Gauthier 0, 2542d1c645cSMarc Gauthier XCHAL_INTLEVEL1_MASK, 2552d1c645cSMarc Gauthier XCHAL_INTLEVEL2_MASK, 2562d1c645cSMarc Gauthier XCHAL_INTLEVEL3_MASK, 2572d1c645cSMarc Gauthier XCHAL_INTLEVEL4_MASK, 2582d1c645cSMarc Gauthier XCHAL_INTLEVEL5_MASK, 2592d1c645cSMarc Gauthier XCHAL_INTLEVEL6_MASK, 2602d1c645cSMarc Gauthier XCHAL_INTLEVEL7_MASK, 2612d1c645cSMarc Gauthier }; 262de4415d0SMax Filippov struct pt_regs *old_regs = set_irq_regs(regs); 26343ba2237SMax Filippov unsigned unhandled = ~0u; 26499623239SMax Filippov 26599623239SMax Filippov irq_enter(); 2662d1c645cSMarc Gauthier 2672d1c645cSMarc Gauthier for (;;) { 268cad6fadeSMax Filippov unsigned intread = xtensa_get_sr(interrupt); 269cad6fadeSMax Filippov unsigned intenable = xtensa_get_sr(intenable); 270895666a9SMax Filippov unsigned int_at_level = intread & intenable; 271895666a9SMax Filippov unsigned level; 2722d1c645cSMarc Gauthier 273895666a9SMax Filippov for (level = LOCKLEVEL; level > 0; --level) { 274895666a9SMax Filippov if (int_at_level & int_level_mask[level]) { 275895666a9SMax Filippov int_at_level &= int_level_mask[level]; 27643ba2237SMax Filippov if (int_at_level & unhandled) 27743ba2237SMax Filippov int_at_level &= unhandled; 27843ba2237SMax Filippov else 27943ba2237SMax Filippov unhandled |= int_level_mask[level]; 280895666a9SMax Filippov break; 281895666a9SMax Filippov } 282895666a9SMax Filippov } 283895666a9SMax Filippov 284895666a9SMax Filippov if (level == 0) 28599623239SMax Filippov break; 2862d1c645cSMarc Gauthier 28743ba2237SMax Filippov /* clear lowest pending irq in the unhandled mask */ 28843ba2237SMax Filippov unhandled ^= (int_at_level & -int_at_level); 28999623239SMax Filippov do_IRQ(__ffs(int_at_level), regs); 29099623239SMax Filippov } 2915a0015d6SChris Zankel 29299623239SMax Filippov irq_exit(); 29399623239SMax Filippov set_irq_regs(old_regs); 2945a0015d6SChris Zankel } 2955a0015d6SChris Zankel 296*d7486200SMax Filippov static bool check_div0(struct pt_regs *regs) 297*d7486200SMax Filippov { 298*d7486200SMax Filippov static const u8 pattern[] = {'D', 'I', 'V', '0'}; 299*d7486200SMax Filippov const u8 *p; 300*d7486200SMax Filippov u8 buf[5]; 301*d7486200SMax Filippov 302*d7486200SMax Filippov if (user_mode(regs)) { 303*d7486200SMax Filippov if (copy_from_user(buf, (void __user *)regs->pc + 2, 5)) 304*d7486200SMax Filippov return 0; 305*d7486200SMax Filippov p = buf; 306*d7486200SMax Filippov } else { 307*d7486200SMax Filippov p = (const u8 *)regs->pc + 2; 308*d7486200SMax Filippov } 309*d7486200SMax Filippov 310*d7486200SMax Filippov return memcmp(p, pattern, sizeof(pattern)) == 0 || 311*d7486200SMax Filippov memcmp(p + 1, pattern, sizeof(pattern)) == 0; 312*d7486200SMax Filippov } 313*d7486200SMax Filippov 3145a0015d6SChris Zankel /* 3155a0015d6SChris Zankel * Illegal instruction. Fatal if in kernel space. 3165a0015d6SChris Zankel */ 3175a0015d6SChris Zankel 318db0d07faSMax Filippov static void do_illegal_instruction(struct pt_regs *regs) 3195a0015d6SChris Zankel { 320*d7486200SMax Filippov if (check_div0(regs)) { 321*d7486200SMax Filippov do_div0(regs); 322*d7486200SMax Filippov return; 323*d7486200SMax Filippov } 324*d7486200SMax Filippov 3255a0015d6SChris Zankel __die_if_kernel("Illegal instruction in kernel", regs, SIGKILL); 3265a0015d6SChris Zankel 3275a0015d6SChris Zankel /* If in user mode, send SIGILL signal to current process. */ 3285a0015d6SChris Zankel 329c130d3beSMax Filippov pr_info_ratelimited("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n", 33019c5870cSAlexey Dobriyan current->comm, task_pid_nr(current), regs->pc); 3313cf5d076SEric W. Biederman force_sig(SIGILL); 3325a0015d6SChris Zankel } 3335a0015d6SChris Zankel 334408b1d3cSMax Filippov static void do_div0(struct pt_regs *regs) 335408b1d3cSMax Filippov { 336408b1d3cSMax Filippov __die_if_kernel("Unhandled division by 0 in kernel", regs, SIGKILL); 337408b1d3cSMax Filippov force_sig_fault(SIGFPE, FPE_INTDIV, (void __user *)regs->pc); 338408b1d3cSMax Filippov } 3395a0015d6SChris Zankel 3405a0015d6SChris Zankel /* 3415a0015d6SChris Zankel * Handle unaligned memory accesses from user space. Kill task. 3425a0015d6SChris Zankel * 3435a0015d6SChris Zankel * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory 3445a0015d6SChris Zankel * accesses causes from user space. 3455a0015d6SChris Zankel */ 3465a0015d6SChris Zankel 3475a0015d6SChris Zankel #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION 348db0d07faSMax Filippov static void do_unaligned_user(struct pt_regs *regs) 3495a0015d6SChris Zankel { 3505a0015d6SChris Zankel __die_if_kernel("Unhandled unaligned exception in kernel", 3515a0015d6SChris Zankel regs, SIGKILL); 3525a0015d6SChris Zankel 3535a0015d6SChris Zankel current->thread.bad_vaddr = regs->excvaddr; 3545a0015d6SChris Zankel current->thread.error_code = -3; 355c130d3beSMax Filippov pr_info_ratelimited("Unaligned memory access to %08lx in '%s' " 3565a0015d6SChris Zankel "(pid = %d, pc = %#010lx)\n", 357c130d3beSMax Filippov regs->excvaddr, current->comm, 358c130d3beSMax Filippov task_pid_nr(current), regs->pc); 3592e1661d2SEric W. Biederman force_sig_fault(SIGBUS, BUS_ADRALN, (void *) regs->excvaddr); 3605a0015d6SChris Zankel } 3615a0015d6SChris Zankel #endif 3625a0015d6SChris Zankel 36311e969bcSMax Filippov #if XTENSA_HAVE_COPROCESSORS 36411e969bcSMax Filippov static void do_coprocessor(struct pt_regs *regs) 36511e969bcSMax Filippov { 36611e969bcSMax Filippov coprocessor_flush_release_all(current_thread_info()); 36711e969bcSMax Filippov } 36811e969bcSMax Filippov #endif 36911e969bcSMax Filippov 370c91e02bdSMax Filippov /* Handle debug events. 371c91e02bdSMax Filippov * When CONFIG_HAVE_HW_BREAKPOINT is on this handler is called with 372c91e02bdSMax Filippov * preemption disabled to avoid rescheduling and keep mapping of hardware 373c91e02bdSMax Filippov * breakpoint structures to debug registers intact, so that 374c91e02bdSMax Filippov * DEBUGCAUSE.DBNUM could be used in case of data breakpoint hit. 375c91e02bdSMax Filippov */ 376db0d07faSMax Filippov static void do_debug(struct pt_regs *regs) 3775a0015d6SChris Zankel { 378c91e02bdSMax Filippov #ifdef CONFIG_HAVE_HW_BREAKPOINT 379c91e02bdSMax Filippov int ret = check_hw_breakpoint(regs); 380c91e02bdSMax Filippov 381c91e02bdSMax Filippov preempt_enable(); 382c91e02bdSMax Filippov if (ret == 0) 383c91e02bdSMax Filippov return; 384c91e02bdSMax Filippov #endif 3855a0015d6SChris Zankel __die_if_kernel("Breakpoint in kernel", regs, SIGKILL); 3865a0015d6SChris Zankel 3875a0015d6SChris Zankel /* If in user mode, send SIGTRAP signal to current process */ 3885a0015d6SChris Zankel 3893cf5d076SEric W. Biederman force_sig(SIGTRAP); 3905a0015d6SChris Zankel } 3915a0015d6SChris Zankel 3925a0015d6SChris Zankel 393f21a79caSMax Filippov #define set_handler(type, cause, handler) \ 394f21a79caSMax Filippov do { \ 395f21a79caSMax Filippov unsigned int cpu; \ 396f21a79caSMax Filippov \ 397f21a79caSMax Filippov for_each_possible_cpu(cpu) \ 398f21a79caSMax Filippov per_cpu(exc_table, cpu).type[cause] = (handler);\ 399f21a79caSMax Filippov } while (0) 400f615136cSMax Filippov 40128570e8dSMax Filippov /* Set exception C handler - for temporary use when probing exceptions */ 40228570e8dSMax Filippov 403fc55402bSMax Filippov xtensa_exception_handler * 404fc55402bSMax Filippov __init trap_set_handler(int cause, xtensa_exception_handler *handler) 40528570e8dSMax Filippov { 406f21a79caSMax Filippov void *previous = per_cpu(exc_table, 0).default_handler[cause]; 407f21a79caSMax Filippov 408f21a79caSMax Filippov set_handler(default_handler, cause, handler); 40928570e8dSMax Filippov return previous; 41028570e8dSMax Filippov } 41128570e8dSMax Filippov 41228570e8dSMax Filippov 41349b424feSMax Filippov static void trap_init_excsave(void) 414f615136cSMax Filippov { 4159fa8c59fSMax Filippov xtensa_set_sr(this_cpu_ptr(&exc_table), excsave1); 416f615136cSMax Filippov } 417f615136cSMax Filippov 4186ec7026aSMax Filippov static void trap_init_debug(void) 4196ec7026aSMax Filippov { 4206ec7026aSMax Filippov unsigned long debugsave = (unsigned long)this_cpu_ptr(&debug_table); 4216ec7026aSMax Filippov 4226ec7026aSMax Filippov this_cpu_ptr(&debug_table)->debug_exception = debug_exception; 4236ec7026aSMax Filippov __asm__ __volatile__("wsr %0, excsave" __stringify(XCHAL_DEBUGLEVEL) 4246ec7026aSMax Filippov :: "a"(debugsave)); 4256ec7026aSMax Filippov } 4266ec7026aSMax Filippov 4275a0015d6SChris Zankel /* 4285a0015d6SChris Zankel * Initialize dispatch tables. 4295a0015d6SChris Zankel * 4305a0015d6SChris Zankel * The exception vectors are stored compressed the __init section in the 4315a0015d6SChris Zankel * dispatch_init_table. This function initializes the following three tables 4325a0015d6SChris Zankel * from that compressed table: 4335a0015d6SChris Zankel * - fast user first dispatch table for user exceptions 4345a0015d6SChris Zankel * - fast kernel first dispatch table for kernel exceptions 4355a0015d6SChris Zankel * - default C-handler C-handler called by the default fast handler. 4365a0015d6SChris Zankel * 4375a0015d6SChris Zankel * See vectors.S for more details. 4385a0015d6SChris Zankel */ 4395a0015d6SChris Zankel 440b91dc336SChris Zankel void __init trap_init(void) 4415a0015d6SChris Zankel { 4425a0015d6SChris Zankel int i; 4435a0015d6SChris Zankel 4445a0015d6SChris Zankel /* Setup default vectors. */ 4455a0015d6SChris Zankel 446f21a79caSMax Filippov for (i = 0; i < EXCCAUSE_N; i++) { 447f21a79caSMax Filippov set_handler(fast_user_handler, i, user_exception); 448f21a79caSMax Filippov set_handler(fast_kernel_handler, i, kernel_exception); 449f21a79caSMax Filippov set_handler(default_handler, i, do_unhandled); 4505a0015d6SChris Zankel } 4515a0015d6SChris Zankel 4525a0015d6SChris Zankel /* Setup specific handlers. */ 4535a0015d6SChris Zankel 4545a0015d6SChris Zankel for(i = 0; dispatch_init_table[i].cause >= 0; i++) { 4555a0015d6SChris Zankel int fast = dispatch_init_table[i].fast; 4565a0015d6SChris Zankel int cause = dispatch_init_table[i].cause; 4575a0015d6SChris Zankel void *handler = dispatch_init_table[i].handler; 4585a0015d6SChris Zankel 4595a0015d6SChris Zankel if (fast == 0) 460f21a79caSMax Filippov set_handler(default_handler, cause, handler); 46160deebe6SMax Filippov if ((fast & USER) != 0) 462f21a79caSMax Filippov set_handler(fast_user_handler, cause, handler); 46360deebe6SMax Filippov if ((fast & KRNL) != 0) 464f21a79caSMax Filippov set_handler(fast_kernel_handler, cause, handler); 4655a0015d6SChris Zankel } 4665a0015d6SChris Zankel 4675a0015d6SChris Zankel /* Initialize EXCSAVE_1 to hold the address of the exception table. */ 468f615136cSMax Filippov trap_init_excsave(); 4696ec7026aSMax Filippov trap_init_debug(); 4705a0015d6SChris Zankel } 4715a0015d6SChris Zankel 472f615136cSMax Filippov #ifdef CONFIG_SMP 47349b424feSMax Filippov void secondary_trap_init(void) 474f615136cSMax Filippov { 475f615136cSMax Filippov trap_init_excsave(); 4766ec7026aSMax Filippov trap_init_debug(); 477f615136cSMax Filippov } 478f615136cSMax Filippov #endif 479f615136cSMax Filippov 4805a0015d6SChris Zankel /* 4815a0015d6SChris Zankel * This function dumps the current valid window frame and other base registers. 4825a0015d6SChris Zankel */ 4835a0015d6SChris Zankel 4845a0015d6SChris Zankel void show_regs(struct pt_regs * regs) 4855a0015d6SChris Zankel { 486431d1a34SMax Filippov int i; 4875a0015d6SChris Zankel 488a43cb95dSTejun Heo show_regs_print_info(KERN_DEFAULT); 489a43cb95dSTejun Heo 4908d7e8240SChris Zankel for (i = 0; i < 16; i++) { 4915a0015d6SChris Zankel if ((i % 8) == 0) 492d4eccafcSMax Filippov pr_info("a%02d:", i); 493d4eccafcSMax Filippov pr_cont(" %08lx", regs->areg[i]); 4945a0015d6SChris Zankel } 495d4eccafcSMax Filippov pr_cont("\n"); 496d4eccafcSMax Filippov pr_info("pc: %08lx, ps: %08lx, depc: %08lx, excvaddr: %08lx\n", 4975a0015d6SChris Zankel regs->pc, regs->ps, regs->depc, regs->excvaddr); 498d4eccafcSMax Filippov pr_info("lbeg: %08lx, lend: %08lx lcount: %08lx, sar: %08lx\n", 4995a0015d6SChris Zankel regs->lbeg, regs->lend, regs->lcount, regs->sar); 5005a0015d6SChris Zankel if (user_mode(regs)) 501d4eccafcSMax Filippov pr_cont("wb: %08lx, ws: %08lx, wmask: %08lx, syscall: %ld\n", 5025a0015d6SChris Zankel regs->windowbase, regs->windowstart, regs->wmask, 5035a0015d6SChris Zankel regs->syscall); 5045a0015d6SChris Zankel } 5055a0015d6SChris Zankel 5063e4196a5SMax Filippov static int show_trace_cb(struct stackframe *frame, void *data) 507586411dcSJohannes Weiner { 50847fb7029SDmitry Safonov const char *loglvl = data; 50947fb7029SDmitry Safonov 510e640cc30SMax Filippov if (kernel_text_address(frame->pc)) 51147fb7029SDmitry Safonov printk("%s [<%08lx>] %pB\n", 51247fb7029SDmitry Safonov loglvl, frame->pc, (void *)frame->pc); 5133e4196a5SMax Filippov return 0; 514586411dcSJohannes Weiner } 515586411dcSJohannes Weiner 51647fb7029SDmitry Safonov static void show_trace(struct task_struct *task, unsigned long *sp, 51747fb7029SDmitry Safonov const char *loglvl) 5185a0015d6SChris Zankel { 5193e4196a5SMax Filippov if (!sp) 5203e4196a5SMax Filippov sp = stack_pointer(task); 5215a0015d6SChris Zankel 52247fb7029SDmitry Safonov printk("%sCall Trace:\n", loglvl); 52347fb7029SDmitry Safonov walk_stackframe(sp, show_trace_cb, (void *)loglvl); 5245a0015d6SChris Zankel } 5255a0015d6SChris Zankel 526c5fccebcSMax Filippov #define STACK_DUMP_ENTRY_SIZE 4 527c5fccebcSMax Filippov #define STACK_DUMP_LINE_SIZE 32 5288951eb15SMax Filippov static size_t kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH; 5295a0015d6SChris Zankel 5309cb8f069SDmitry Safonov void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl) 5315a0015d6SChris Zankel { 532c5fccebcSMax Filippov size_t len; 5335a0015d6SChris Zankel 53428a0ce7fSJohannes Weiner if (!sp) 535586411dcSJohannes Weiner sp = stack_pointer(task); 536c5fccebcSMax Filippov 537c5fccebcSMax Filippov len = min((-(size_t)sp) & (THREAD_SIZE - STACK_DUMP_ENTRY_SIZE), 538c5fccebcSMax Filippov kstack_depth_to_print * STACK_DUMP_ENTRY_SIZE); 5395a0015d6SChris Zankel 54020da1e8bSDmitry Safonov printk("%sStack:\n", loglvl); 54120da1e8bSDmitry Safonov print_hex_dump(loglvl, " ", DUMP_PREFIX_NONE, 542c5fccebcSMax Filippov STACK_DUMP_LINE_SIZE, STACK_DUMP_ENTRY_SIZE, 543c5fccebcSMax Filippov sp, len, false); 54420da1e8bSDmitry Safonov show_trace(task, sp, loglvl); 54520da1e8bSDmitry Safonov } 54620da1e8bSDmitry Safonov 54734af946aSIngo Molnar DEFINE_SPINLOCK(die_lock); 5485a0015d6SChris Zankel 5499fd5a04dSEric W. Biederman void __noreturn die(const char * str, struct pt_regs * regs, long err) 5505a0015d6SChris Zankel { 5515a0015d6SChris Zankel static int die_counter; 5526c5260d7SThomas Gleixner const char *pr = ""; 5536c5260d7SThomas Gleixner 5546c5260d7SThomas Gleixner if (IS_ENABLED(CONFIG_PREEMPTION)) 5556c5260d7SThomas Gleixner pr = IS_ENABLED(CONFIG_PREEMPT_RT) ? " PREEMPT_RT" : " PREEMPT"; 5565a0015d6SChris Zankel 5575a0015d6SChris Zankel console_verbose(); 5585a0015d6SChris Zankel spin_lock_irq(&die_lock); 5595a0015d6SChris Zankel 5606c5260d7SThomas Gleixner pr_info("%s: sig: %ld [#%d]%s\n", str, err, ++die_counter, pr); 5615a0015d6SChris Zankel show_regs(regs); 5625a0015d6SChris Zankel if (!user_mode(regs)) 5639cb8f069SDmitry Safonov show_stack(NULL, (unsigned long *)regs->areg[1], KERN_INFO); 5645a0015d6SChris Zankel 565373d4d09SRusty Russell add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE); 5665a0015d6SChris Zankel spin_unlock_irq(&die_lock); 5675a0015d6SChris Zankel 5685a0015d6SChris Zankel if (in_interrupt()) 5695a0015d6SChris Zankel panic("Fatal exception in interrupt"); 5705a0015d6SChris Zankel 571cea6a4baSHorms if (panic_on_oops) 572012c437dSHorms panic("Fatal exception"); 573cea6a4baSHorms 5740e25498fSEric W. Biederman make_task_dead(err); 5755a0015d6SChris Zankel } 576