traps.c 14 KB

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