Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 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 | * |
Max Filippov | 3e4196a | 2013-04-15 09:20:48 +0400 | [diff] [blame] | 14 | * Copyright (C) 2001 - 2013 Tensilica Inc. |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 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> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 27 | #include <linux/sched/signal.h> |
Ingo Molnar | b17b015 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 28 | #include <linux/sched/debug.h> |
Ingo Molnar | 3f8c245 | 2017-02-05 14:31:22 +0100 | [diff] [blame] | 29 | #include <linux/sched/task_stack.h> |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 30 | #include <linux/init.h> |
| 31 | #include <linux/module.h> |
| 32 | #include <linux/stringify.h> |
| 33 | #include <linux/kallsyms.h> |
Nishanth Aravamudan | 5c888d5 | 2005-07-12 13:58:26 -0700 | [diff] [blame] | 34 | #include <linux/delay.h> |
Alexey Dobriyan | 5a891ed | 2009-03-10 12:55:49 -0700 | [diff] [blame] | 35 | #include <linux/hardirq.h> |
Max Filippov | c130d3b | 2017-12-15 12:00:30 -0800 | [diff] [blame] | 36 | #include <linux/ratelimit.h> |
Mike Rapoport | 65fddcf | 2020-06-08 21:32:42 -0700 | [diff] [blame] | 37 | #include <linux/pgtable.h> |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 38 | |
Max Filippov | 3e4196a | 2013-04-15 09:20:48 +0400 | [diff] [blame] | 39 | #include <asm/stacktrace.h> |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 40 | #include <asm/ptrace.h> |
| 41 | #include <asm/timex.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 42 | #include <linux/uaccess.h> |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 43 | #include <asm/processor.h> |
Max Filippov | 2d6f82f | 2013-02-03 05:39:22 +0400 | [diff] [blame] | 44 | #include <asm/traps.h> |
Max Filippov | c91e02b | 2016-01-24 10:32:10 +0300 | [diff] [blame] | 45 | #include <asm/hw_breakpoint.h> |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 46 | |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 47 | /* |
| 48 | * Machine specific interrupt handlers |
| 49 | */ |
| 50 | |
| 51 | extern void kernel_exception(void); |
| 52 | extern void user_exception(void); |
| 53 | |
Max Filippov | 09f8a6d | 2015-01-12 09:44:44 +0300 | [diff] [blame] | 54 | extern void fast_illegal_instruction_user(void); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 55 | extern void fast_syscall_user(void); |
| 56 | extern void fast_alloca(void); |
| 57 | extern void fast_unaligned(void); |
| 58 | extern void fast_second_level_miss(void); |
| 59 | extern void fast_store_prohibited(void); |
| 60 | extern void fast_coprocessor(void); |
| 61 | |
| 62 | extern void do_illegal_instruction (struct pt_regs*); |
| 63 | extern void do_interrupt (struct pt_regs*); |
Max Filippov | 38fef73 | 2015-07-16 10:37:31 +0300 | [diff] [blame] | 64 | extern void do_nmi(struct pt_regs *); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 65 | extern void do_unaligned_user (struct pt_regs*); |
| 66 | extern void do_multihit (struct pt_regs*, unsigned long); |
| 67 | extern void do_page_fault (struct pt_regs*, unsigned long); |
| 68 | extern void do_debug (struct pt_regs*); |
| 69 | extern void system_call (struct pt_regs*); |
| 70 | |
| 71 | /* |
| 72 | * The vector table must be preceded by a save area (which |
| 73 | * implies it must be in RAM, unless one places RAM immediately |
| 74 | * before a ROM and puts the vector at the start of the ROM (!)) |
| 75 | */ |
| 76 | |
| 77 | #define KRNL 0x01 |
| 78 | #define USER 0x02 |
| 79 | |
| 80 | #define COPROCESSOR(x) \ |
Chris Zankel | 173d668 | 2006-12-10 02:18:48 -0800 | [diff] [blame] | 81 | { EXCCAUSE_COPROCESSOR ## x ## _DISABLED, USER, fast_coprocessor } |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 82 | |
| 83 | typedef struct { |
| 84 | int cause; |
| 85 | int fast; |
| 86 | void* handler; |
| 87 | } dispatch_init_table_t; |
| 88 | |
Chris Zankel | b91dc33 | 2007-08-03 15:54:36 -0700 | [diff] [blame] | 89 | static dispatch_init_table_t __initdata dispatch_init_table[] = { |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 90 | |
Max Filippov | 09f8a6d | 2015-01-12 09:44:44 +0300 | [diff] [blame] | 91 | #ifdef CONFIG_USER_ABI_CALL0_PROBE |
| 92 | { EXCCAUSE_ILLEGAL_INSTRUCTION, USER, fast_illegal_instruction_user }, |
| 93 | #endif |
Chris Zankel | 173d668 | 2006-12-10 02:18:48 -0800 | [diff] [blame] | 94 | { EXCCAUSE_ILLEGAL_INSTRUCTION, 0, do_illegal_instruction}, |
Chris Zankel | 173d668 | 2006-12-10 02:18:48 -0800 | [diff] [blame] | 95 | { EXCCAUSE_SYSTEM_CALL, USER, fast_syscall_user }, |
| 96 | { EXCCAUSE_SYSTEM_CALL, 0, system_call }, |
| 97 | /* EXCCAUSE_INSTRUCTION_FETCH unhandled */ |
| 98 | /* EXCCAUSE_LOAD_STORE_ERROR unhandled*/ |
| 99 | { EXCCAUSE_LEVEL1_INTERRUPT, 0, do_interrupt }, |
Max Filippov | da0a4e5 | 2021-07-26 07:32:55 -0700 | [diff] [blame] | 100 | #ifdef SUPPORT_WINDOWED |
Chris Zankel | 173d668 | 2006-12-10 02:18:48 -0800 | [diff] [blame] | 101 | { EXCCAUSE_ALLOCA, USER|KRNL, fast_alloca }, |
Max Filippov | da0a4e5 | 2021-07-26 07:32:55 -0700 | [diff] [blame] | 102 | #endif |
Chris Zankel | 173d668 | 2006-12-10 02:18:48 -0800 | [diff] [blame] | 103 | /* EXCCAUSE_INTEGER_DIVIDE_BY_ZERO unhandled */ |
| 104 | /* EXCCAUSE_PRIVILEGED unhandled */ |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 105 | #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION |
Max Filippov | 4ded628 | 2012-10-15 21:23:02 +0400 | [diff] [blame] | 106 | #ifdef CONFIG_XTENSA_UNALIGNED_USER |
Chris Zankel | 173d668 | 2006-12-10 02:18:48 -0800 | [diff] [blame] | 107 | { EXCCAUSE_UNALIGNED, USER, fast_unaligned }, |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 108 | #endif |
Max Filippov | 3cfc096 | 2014-08-07 01:03:01 +0400 | [diff] [blame] | 109 | { EXCCAUSE_UNALIGNED, 0, do_unaligned_user }, |
Chris Zankel | 173d668 | 2006-12-10 02:18:48 -0800 | [diff] [blame] | 110 | { EXCCAUSE_UNALIGNED, KRNL, fast_unaligned }, |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 111 | #endif |
Johannes Weiner | e5083a6 | 2009-03-04 16:21:31 +0100 | [diff] [blame] | 112 | #ifdef CONFIG_MMU |
Chris Zankel | 173d668 | 2006-12-10 02:18:48 -0800 | [diff] [blame] | 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 | /* EXCCAUSE_SIZE_RESTRICTION unhandled */ |
| 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 | /* EXCCAUSE_DTLB_SIZE_RESTRICTION unhandled */ |
| 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 }, |
Johannes Weiner | e5083a6 | 2009-03-04 16:21:31 +0100 | [diff] [blame] | 127 | #endif /* CONFIG_MMU */ |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 128 | /* XCCHAL_EXCCAUSE_FLOATING_POINT unhandled */ |
Chris Zankel | c658eac | 2008-02-12 13:17:07 -0800 | [diff] [blame] | 129 | #if XTENSA_HAVE_COPROCESSOR(0) |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 130 | COPROCESSOR(0), |
| 131 | #endif |
Chris Zankel | c658eac | 2008-02-12 13:17:07 -0800 | [diff] [blame] | 132 | #if XTENSA_HAVE_COPROCESSOR(1) |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 133 | COPROCESSOR(1), |
| 134 | #endif |
Chris Zankel | c658eac | 2008-02-12 13:17:07 -0800 | [diff] [blame] | 135 | #if XTENSA_HAVE_COPROCESSOR(2) |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 136 | COPROCESSOR(2), |
| 137 | #endif |
Chris Zankel | c658eac | 2008-02-12 13:17:07 -0800 | [diff] [blame] | 138 | #if XTENSA_HAVE_COPROCESSOR(3) |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 139 | COPROCESSOR(3), |
| 140 | #endif |
Chris Zankel | c658eac | 2008-02-12 13:17:07 -0800 | [diff] [blame] | 141 | #if XTENSA_HAVE_COPROCESSOR(4) |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 142 | COPROCESSOR(4), |
| 143 | #endif |
Chris Zankel | c658eac | 2008-02-12 13:17:07 -0800 | [diff] [blame] | 144 | #if XTENSA_HAVE_COPROCESSOR(5) |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 145 | COPROCESSOR(5), |
| 146 | #endif |
Chris Zankel | c658eac | 2008-02-12 13:17:07 -0800 | [diff] [blame] | 147 | #if XTENSA_HAVE_COPROCESSOR(6) |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 148 | COPROCESSOR(6), |
| 149 | #endif |
Chris Zankel | c658eac | 2008-02-12 13:17:07 -0800 | [diff] [blame] | 150 | #if XTENSA_HAVE_COPROCESSOR(7) |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 151 | COPROCESSOR(7), |
| 152 | #endif |
Max Filippov | 38fef73 | 2015-07-16 10:37:31 +0300 | [diff] [blame] | 153 | #if XTENSA_FAKE_NMI |
| 154 | { EXCCAUSE_MAPPED_NMI, 0, do_nmi }, |
| 155 | #endif |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 156 | { EXCCAUSE_MAPPED_DEBUG, 0, do_debug }, |
| 157 | { -1, -1, 0 } |
| 158 | |
| 159 | }; |
| 160 | |
| 161 | /* The exception table <exc_table> serves two functions: |
| 162 | * 1. it contains three dispatch tables (fast_user, fast_kernel, default-c) |
| 163 | * 2. it is a temporary memory buffer for the exception handlers. |
| 164 | */ |
| 165 | |
Max Filippov | f21a79c | 2017-12-15 16:08:16 -0800 | [diff] [blame] | 166 | DEFINE_PER_CPU(struct exc_table, exc_table); |
Max Filippov | 6ec7026 | 2016-03-07 01:36:33 +0300 | [diff] [blame] | 167 | DEFINE_PER_CPU(struct debug_table, debug_table); |
| 168 | |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 169 | void die(const char*, struct pt_regs*, long); |
| 170 | |
| 171 | static 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 | * Unhandled Exceptions. Kill user task or panic if in kernel space. |
| 180 | */ |
| 181 | |
| 182 | void do_unhandled(struct pt_regs *regs, unsigned long exccause) |
| 183 | { |
| 184 | __die_if_kernel("Caught unhandled exception - should not happen", |
Max Filippov | c130d3b | 2017-12-15 12:00:30 -0800 | [diff] [blame] | 185 | regs, SIGKILL); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 186 | |
| 187 | /* If in user mode, send SIGILL signal to current process */ |
Max Filippov | c130d3b | 2017-12-15 12:00:30 -0800 | [diff] [blame] | 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); |
Eric W. Biederman | 3cf5d07 | 2019-05-23 10:17:27 -0500 | [diff] [blame] | 193 | force_sig(SIGILL); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Multi-hit exception. This if fatal! |
| 198 | */ |
| 199 | |
| 200 | void do_multihit(struct pt_regs *regs, unsigned long exccause) |
| 201 | { |
| 202 | die("Caught multihit exception", regs, SIGKILL); |
| 203 | } |
| 204 | |
| 205 | /* |
Marc Gauthier | 2d1c645 | 2013-01-05 04:57:17 +0400 | [diff] [blame] | 206 | * IRQ handler. |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 207 | */ |
| 208 | |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 209 | extern void do_IRQ(int, struct pt_regs *); |
| 210 | |
Max Filippov | 38fef73 | 2015-07-16 10:37:31 +0300 | [diff] [blame] | 211 | #if XTENSA_FAKE_NMI |
| 212 | |
Max Filippov | e462919 | 2015-11-27 16:26:41 +0300 | [diff] [blame] | 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 | |
| 220 | static inline void check_valid_nmi(void) |
| 221 | { |
Max Filippov | cad6fad | 2018-11-27 16:27:47 -0800 | [diff] [blame] | 222 | unsigned intread = xtensa_get_sr(interrupt); |
| 223 | unsigned intenable = xtensa_get_sr(intenable); |
Max Filippov | e462919 | 2015-11-27 16:26:41 +0300 | [diff] [blame] | 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 | |
| 233 | static inline void check_valid_nmi(void) |
| 234 | { |
| 235 | } |
| 236 | |
| 237 | #endif |
| 238 | |
Max Filippov | 38fef73 | 2015-07-16 10:37:31 +0300 | [diff] [blame] | 239 | irqreturn_t xtensa_pmu_irq_handler(int irq, void *dev_id); |
| 240 | |
| 241 | DEFINE_PER_CPU(unsigned long, nmi_count); |
| 242 | |
| 243 | void 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); |
Max Filippov | e462919 | 2015-11-27 16:26:41 +0300 | [diff] [blame] | 253 | check_valid_nmi(); |
Max Filippov | 38fef73 | 2015-07-16 10:37:31 +0300 | [diff] [blame] | 254 | xtensa_pmu_irq_handler(0, NULL); |
| 255 | nmi_exit(); |
| 256 | set_irq_regs(old_regs); |
| 257 | } |
| 258 | #endif |
| 259 | |
Marc Gauthier | 2d1c645 | 2013-01-05 04:57:17 +0400 | [diff] [blame] | 260 | void do_interrupt(struct pt_regs *regs) |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 261 | { |
Marc Gauthier | 2d1c645 | 2013-01-05 04:57:17 +0400 | [diff] [blame] | 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 | }; |
Max Filippov | 7d5f6a9 | 2015-07-16 06:18:46 +0300 | [diff] [blame] | 272 | struct pt_regs *old_regs; |
Max Filippov | 43ba223 | 2021-07-09 04:13:23 -0700 | [diff] [blame] | 273 | unsigned unhandled = ~0u; |
Max Filippov | 9962323 | 2013-10-17 02:42:23 +0400 | [diff] [blame] | 274 | |
Max Filippov | 7d5f6a9 | 2015-07-16 06:18:46 +0300 | [diff] [blame] | 275 | trace_hardirqs_off(); |
| 276 | |
| 277 | old_regs = set_irq_regs(regs); |
Max Filippov | 9962323 | 2013-10-17 02:42:23 +0400 | [diff] [blame] | 278 | irq_enter(); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 279 | |
Marc Gauthier | 2d1c645 | 2013-01-05 04:57:17 +0400 | [diff] [blame] | 280 | for (;;) { |
Max Filippov | cad6fad | 2018-11-27 16:27:47 -0800 | [diff] [blame] | 281 | unsigned intread = xtensa_get_sr(interrupt); |
| 282 | unsigned intenable = xtensa_get_sr(intenable); |
Max Filippov | 895666a | 2013-03-26 02:51:43 +0400 | [diff] [blame] | 283 | unsigned int_at_level = intread & intenable; |
| 284 | unsigned level; |
Marc Gauthier | 2d1c645 | 2013-01-05 04:57:17 +0400 | [diff] [blame] | 285 | |
Max Filippov | 895666a | 2013-03-26 02:51:43 +0400 | [diff] [blame] | 286 | for (level = LOCKLEVEL; level > 0; --level) { |
| 287 | if (int_at_level & int_level_mask[level]) { |
| 288 | int_at_level &= int_level_mask[level]; |
Max Filippov | 43ba223 | 2021-07-09 04:13:23 -0700 | [diff] [blame] | 289 | if (int_at_level & unhandled) |
| 290 | int_at_level &= unhandled; |
| 291 | else |
| 292 | unhandled |= int_level_mask[level]; |
Max Filippov | 895666a | 2013-03-26 02:51:43 +0400 | [diff] [blame] | 293 | break; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | if (level == 0) |
Max Filippov | 9962323 | 2013-10-17 02:42:23 +0400 | [diff] [blame] | 298 | break; |
Marc Gauthier | 2d1c645 | 2013-01-05 04:57:17 +0400 | [diff] [blame] | 299 | |
Max Filippov | 43ba223 | 2021-07-09 04:13:23 -0700 | [diff] [blame] | 300 | /* clear lowest pending irq in the unhandled mask */ |
| 301 | unhandled ^= (int_at_level & -int_at_level); |
Max Filippov | 9962323 | 2013-10-17 02:42:23 +0400 | [diff] [blame] | 302 | do_IRQ(__ffs(int_at_level), regs); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 303 | } |
Max Filippov | 9962323 | 2013-10-17 02:42:23 +0400 | [diff] [blame] | 304 | |
| 305 | irq_exit(); |
| 306 | set_irq_regs(old_regs); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Illegal instruction. Fatal if in kernel space. |
| 311 | */ |
| 312 | |
| 313 | void |
| 314 | do_illegal_instruction(struct pt_regs *regs) |
| 315 | { |
| 316 | __die_if_kernel("Illegal instruction in kernel", regs, SIGKILL); |
| 317 | |
| 318 | /* If in user mode, send SIGILL signal to current process. */ |
| 319 | |
Max Filippov | c130d3b | 2017-12-15 12:00:30 -0800 | [diff] [blame] | 320 | pr_info_ratelimited("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n", |
| 321 | current->comm, task_pid_nr(current), regs->pc); |
Eric W. Biederman | 3cf5d07 | 2019-05-23 10:17:27 -0500 | [diff] [blame] | 322 | force_sig(SIGILL); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | |
| 326 | /* |
| 327 | * Handle unaligned memory accesses from user space. Kill task. |
| 328 | * |
| 329 | * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory |
| 330 | * accesses causes from user space. |
| 331 | */ |
| 332 | |
| 333 | #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 334 | void |
| 335 | do_unaligned_user (struct pt_regs *regs) |
| 336 | { |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 337 | __die_if_kernel("Unhandled unaligned exception in kernel", |
Max Filippov | c130d3b | 2017-12-15 12:00:30 -0800 | [diff] [blame] | 338 | regs, SIGKILL); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 339 | |
| 340 | current->thread.bad_vaddr = regs->excvaddr; |
| 341 | current->thread.error_code = -3; |
Max Filippov | c130d3b | 2017-12-15 12:00:30 -0800 | [diff] [blame] | 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); |
Eric W. Biederman | 2e1661d2 | 2019-05-23 11:04:24 -0500 | [diff] [blame] | 346 | force_sig_fault(SIGBUS, BUS_ADRALN, (void *) regs->excvaddr); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 347 | } |
| 348 | #endif |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 349 | |
Max Filippov | c91e02b | 2016-01-24 10:32:10 +0300 | [diff] [blame] | 350 | /* Handle debug events. |
| 351 | * When CONFIG_HAVE_HW_BREAKPOINT is on this handler is called with |
| 352 | * preemption disabled to avoid rescheduling and keep mapping of hardware |
| 353 | * breakpoint structures to debug registers intact, so that |
| 354 | * DEBUGCAUSE.DBNUM could be used in case of data breakpoint hit. |
| 355 | */ |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 356 | void |
| 357 | do_debug(struct pt_regs *regs) |
| 358 | { |
Max Filippov | c91e02b | 2016-01-24 10:32:10 +0300 | [diff] [blame] | 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 |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 366 | __die_if_kernel("Breakpoint in kernel", regs, SIGKILL); |
| 367 | |
| 368 | /* If in user mode, send SIGTRAP signal to current process */ |
| 369 | |
Eric W. Biederman | 3cf5d07 | 2019-05-23 10:17:27 -0500 | [diff] [blame] | 370 | force_sig(SIGTRAP); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | |
Max Filippov | f21a79c | 2017-12-15 16:08:16 -0800 | [diff] [blame] | 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) |
Max Filippov | f615136 | 2013-10-17 02:42:26 +0400 | [diff] [blame] | 381 | |
Max Filippov | 28570e8 | 2012-11-19 08:30:15 +0400 | [diff] [blame] | 382 | /* Set exception C handler - for temporary use when probing exceptions */ |
| 383 | |
| 384 | void * __init trap_set_handler(int cause, void *handler) |
| 385 | { |
Max Filippov | f21a79c | 2017-12-15 16:08:16 -0800 | [diff] [blame] | 386 | void *previous = per_cpu(exc_table, 0).default_handler[cause]; |
| 387 | |
| 388 | set_handler(default_handler, cause, handler); |
Max Filippov | 28570e8 | 2012-11-19 08:30:15 +0400 | [diff] [blame] | 389 | return previous; |
| 390 | } |
| 391 | |
| 392 | |
Max Filippov | 49b424f | 2013-10-17 02:42:28 +0400 | [diff] [blame] | 393 | static void trap_init_excsave(void) |
Max Filippov | f615136 | 2013-10-17 02:42:26 +0400 | [diff] [blame] | 394 | { |
Max Filippov | f21a79c | 2017-12-15 16:08:16 -0800 | [diff] [blame] | 395 | unsigned long excsave1 = (unsigned long)this_cpu_ptr(&exc_table); |
Max Filippov | f615136 | 2013-10-17 02:42:26 +0400 | [diff] [blame] | 396 | __asm__ __volatile__("wsr %0, excsave1\n" : : "a" (excsave1)); |
| 397 | } |
| 398 | |
Max Filippov | 6ec7026 | 2016-03-07 01:36:33 +0300 | [diff] [blame] | 399 | static 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 | |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 408 | /* |
| 409 | * Initialize dispatch tables. |
| 410 | * |
| 411 | * The exception vectors are stored compressed the __init section in the |
| 412 | * dispatch_init_table. This function initializes the following three tables |
| 413 | * from that compressed table: |
| 414 | * - fast user first dispatch table for user exceptions |
| 415 | * - fast kernel first dispatch table for kernel exceptions |
| 416 | * - default C-handler C-handler called by the default fast handler. |
| 417 | * |
| 418 | * See vectors.S for more details. |
| 419 | */ |
| 420 | |
Chris Zankel | b91dc33 | 2007-08-03 15:54:36 -0700 | [diff] [blame] | 421 | void __init trap_init(void) |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 422 | { |
| 423 | int i; |
| 424 | |
| 425 | /* Setup default vectors. */ |
| 426 | |
Max Filippov | f21a79c | 2017-12-15 16:08:16 -0800 | [diff] [blame] | 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); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /* Setup specific handlers. */ |
| 434 | |
| 435 | for(i = 0; dispatch_init_table[i].cause >= 0; i++) { |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 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) |
Max Filippov | f21a79c | 2017-12-15 16:08:16 -0800 | [diff] [blame] | 441 | set_handler(default_handler, cause, handler); |
Max Filippov | 60deebe | 2019-01-02 02:19:30 -0800 | [diff] [blame] | 442 | if ((fast & USER) != 0) |
Max Filippov | f21a79c | 2017-12-15 16:08:16 -0800 | [diff] [blame] | 443 | set_handler(fast_user_handler, cause, handler); |
Max Filippov | 60deebe | 2019-01-02 02:19:30 -0800 | [diff] [blame] | 444 | if ((fast & KRNL) != 0) |
Max Filippov | f21a79c | 2017-12-15 16:08:16 -0800 | [diff] [blame] | 445 | set_handler(fast_kernel_handler, cause, handler); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /* Initialize EXCSAVE_1 to hold the address of the exception table. */ |
Max Filippov | f615136 | 2013-10-17 02:42:26 +0400 | [diff] [blame] | 449 | trap_init_excsave(); |
Max Filippov | 6ec7026 | 2016-03-07 01:36:33 +0300 | [diff] [blame] | 450 | trap_init_debug(); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Max Filippov | f615136 | 2013-10-17 02:42:26 +0400 | [diff] [blame] | 453 | #ifdef CONFIG_SMP |
Max Filippov | 49b424f | 2013-10-17 02:42:28 +0400 | [diff] [blame] | 454 | void secondary_trap_init(void) |
Max Filippov | f615136 | 2013-10-17 02:42:26 +0400 | [diff] [blame] | 455 | { |
| 456 | trap_init_excsave(); |
Max Filippov | 6ec7026 | 2016-03-07 01:36:33 +0300 | [diff] [blame] | 457 | trap_init_debug(); |
Max Filippov | f615136 | 2013-10-17 02:42:26 +0400 | [diff] [blame] | 458 | } |
| 459 | #endif |
| 460 | |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 461 | /* |
| 462 | * This function dumps the current valid window frame and other base registers. |
| 463 | */ |
| 464 | |
| 465 | void show_regs(struct pt_regs * regs) |
| 466 | { |
Max Filippov | 431d1a3 | 2021-08-22 21:28:01 -0700 | [diff] [blame] | 467 | int i; |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 468 | |
Tejun Heo | a43cb95 | 2013-04-30 15:27:17 -0700 | [diff] [blame] | 469 | show_regs_print_info(KERN_DEFAULT); |
| 470 | |
Chris Zankel | 8d7e824 | 2008-02-12 11:55:32 -0800 | [diff] [blame] | 471 | for (i = 0; i < 16; i++) { |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 472 | if ((i % 8) == 0) |
Max Filippov | d4eccaf | 2016-11-04 14:45:08 -0700 | [diff] [blame] | 473 | pr_info("a%02d:", i); |
| 474 | pr_cont(" %08lx", regs->areg[i]); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 475 | } |
Max Filippov | d4eccaf | 2016-11-04 14:45:08 -0700 | [diff] [blame] | 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); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 481 | if (user_mode(regs)) |
Max Filippov | d4eccaf | 2016-11-04 14:45:08 -0700 | [diff] [blame] | 482 | pr_cont("wb: %08lx, ws: %08lx, wmask: %08lx, syscall: %ld\n", |
| 483 | regs->windowbase, regs->windowstart, regs->wmask, |
| 484 | regs->syscall); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Max Filippov | 3e4196a | 2013-04-15 09:20:48 +0400 | [diff] [blame] | 487 | static int show_trace_cb(struct stackframe *frame, void *data) |
Johannes Weiner | 586411d | 2009-05-11 15:43:33 +0200 | [diff] [blame] | 488 | { |
Dmitry Safonov | 47fb702 | 2020-06-08 21:32:04 -0700 | [diff] [blame] | 489 | const char *loglvl = data; |
| 490 | |
Max Filippov | e640cc3 | 2017-03-31 15:58:40 -0700 | [diff] [blame] | 491 | if (kernel_text_address(frame->pc)) |
Dmitry Safonov | 47fb702 | 2020-06-08 21:32:04 -0700 | [diff] [blame] | 492 | printk("%s [<%08lx>] %pB\n", |
| 493 | loglvl, frame->pc, (void *)frame->pc); |
Max Filippov | 3e4196a | 2013-04-15 09:20:48 +0400 | [diff] [blame] | 494 | return 0; |
Johannes Weiner | 586411d | 2009-05-11 15:43:33 +0200 | [diff] [blame] | 495 | } |
| 496 | |
Dmitry Safonov | 47fb702 | 2020-06-08 21:32:04 -0700 | [diff] [blame] | 497 | static void show_trace(struct task_struct *task, unsigned long *sp, |
| 498 | const char *loglvl) |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 499 | { |
Max Filippov | 3e4196a | 2013-04-15 09:20:48 +0400 | [diff] [blame] | 500 | if (!sp) |
| 501 | sp = stack_pointer(task); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 502 | |
Dmitry Safonov | 47fb702 | 2020-06-08 21:32:04 -0700 | [diff] [blame] | 503 | printk("%sCall Trace:\n", loglvl); |
| 504 | walk_stackframe(sp, show_trace_cb, (void *)loglvl); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Max Filippov | c5fcceb | 2019-11-12 08:47:48 -0800 | [diff] [blame] | 507 | #define STACK_DUMP_ENTRY_SIZE 4 |
| 508 | #define STACK_DUMP_LINE_SIZE 32 |
Max Filippov | 8951eb15 | 2019-11-12 08:43:25 -0800 | [diff] [blame] | 509 | static size_t kstack_depth_to_print = CONFIG_PRINT_STACK_DEPTH; |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 510 | |
Dmitry Safonov | 9cb8f06 | 2020-06-08 21:32:29 -0700 | [diff] [blame] | 511 | void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl) |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 512 | { |
Max Filippov | c5fcceb | 2019-11-12 08:47:48 -0800 | [diff] [blame] | 513 | size_t len; |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 514 | |
Johannes Weiner | 28a0ce7 | 2009-03-04 16:21:29 +0100 | [diff] [blame] | 515 | if (!sp) |
Johannes Weiner | 586411d | 2009-05-11 15:43:33 +0200 | [diff] [blame] | 516 | sp = stack_pointer(task); |
Max Filippov | c5fcceb | 2019-11-12 08:47:48 -0800 | [diff] [blame] | 517 | |
| 518 | len = min((-(size_t)sp) & (THREAD_SIZE - STACK_DUMP_ENTRY_SIZE), |
| 519 | kstack_depth_to_print * STACK_DUMP_ENTRY_SIZE); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 520 | |
Dmitry Safonov | 20da1e8 | 2020-06-08 21:32:07 -0700 | [diff] [blame] | 521 | printk("%sStack:\n", loglvl); |
| 522 | print_hex_dump(loglvl, " ", DUMP_PREFIX_NONE, |
Max Filippov | c5fcceb | 2019-11-12 08:47:48 -0800 | [diff] [blame] | 523 | STACK_DUMP_LINE_SIZE, STACK_DUMP_ENTRY_SIZE, |
| 524 | sp, len, false); |
Dmitry Safonov | 20da1e8 | 2020-06-08 21:32:07 -0700 | [diff] [blame] | 525 | show_trace(task, sp, loglvl); |
| 526 | } |
| 527 | |
Ingo Molnar | 34af946 | 2006-06-27 02:53:55 -0700 | [diff] [blame] | 528 | DEFINE_SPINLOCK(die_lock); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 529 | |
Eric W. Biederman | 9fd5a04 | 2021-10-20 12:43:48 -0500 | [diff] [blame] | 530 | void __noreturn die(const char * str, struct pt_regs * regs, long err) |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 531 | { |
| 532 | static int die_counter; |
Thomas Gleixner | 6c5260d | 2019-10-15 21:18:07 +0200 | [diff] [blame] | 533 | const char *pr = ""; |
| 534 | |
| 535 | if (IS_ENABLED(CONFIG_PREEMPTION)) |
| 536 | pr = IS_ENABLED(CONFIG_PREEMPT_RT) ? " PREEMPT_RT" : " PREEMPT"; |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 537 | |
| 538 | console_verbose(); |
| 539 | spin_lock_irq(&die_lock); |
| 540 | |
Thomas Gleixner | 6c5260d | 2019-10-15 21:18:07 +0200 | [diff] [blame] | 541 | pr_info("%s: sig: %ld [#%d]%s\n", str, err, ++die_counter, pr); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 542 | show_regs(regs); |
| 543 | if (!user_mode(regs)) |
Dmitry Safonov | 9cb8f06 | 2020-06-08 21:32:29 -0700 | [diff] [blame] | 544 | show_stack(NULL, (unsigned long *)regs->areg[1], KERN_INFO); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 545 | |
Rusty Russell | 373d4d0 | 2013-01-21 17:17:39 +1030 | [diff] [blame] | 546 | add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE); |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 547 | spin_unlock_irq(&die_lock); |
| 548 | |
| 549 | if (in_interrupt()) |
| 550 | panic("Fatal exception in interrupt"); |
| 551 | |
Horms | cea6a4b | 2006-07-30 03:03:34 -0700 | [diff] [blame] | 552 | if (panic_on_oops) |
Horms | 012c437 | 2006-08-13 23:24:22 -0700 | [diff] [blame] | 553 | panic("Fatal exception"); |
Horms | cea6a4b | 2006-07-30 03:03:34 -0700 | [diff] [blame] | 554 | |
Chris Zankel | 5a0015d | 2005-06-23 22:01:16 -0700 | [diff] [blame] | 555 | do_exit(err); |
| 556 | } |