traps.c 14 KB

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