traps.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. #ifdef CONFIG_KGDB
  41. extern int gdb_enter;
  42. extern int return_from_debug_flag;
  43. #endif
  44. /*
  45. * Machine specific interrupt handlers
  46. */
  47. extern void kernel_exception(void);
  48. extern void user_exception(void);
  49. extern void fast_syscall_kernel(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_unaligned_user (struct pt_regs*);
  59. extern void do_multihit (struct pt_regs*, unsigned long);
  60. extern void do_page_fault (struct pt_regs*, unsigned long);
  61. extern void do_debug (struct pt_regs*);
  62. extern void system_call (struct pt_regs*);
  63. /*
  64. * The vector table must be preceded by a save area (which
  65. * implies it must be in RAM, unless one places RAM immediately
  66. * before a ROM and puts the vector at the start of the ROM (!))
  67. */
  68. #define KRNL 0x01
  69. #define USER 0x02
  70. #define COPROCESSOR(x) \
  71. { EXCCAUSE_COPROCESSOR ## x ## _DISABLED, USER, fast_coprocessor }
  72. typedef struct {
  73. int cause;
  74. int fast;
  75. void* handler;
  76. } dispatch_init_table_t;
  77. static dispatch_init_table_t __initdata dispatch_init_table[] = {
  78. { EXCCAUSE_ILLEGAL_INSTRUCTION, 0, do_illegal_instruction},
  79. { EXCCAUSE_SYSTEM_CALL, KRNL, fast_syscall_kernel },
  80. { EXCCAUSE_SYSTEM_CALL, USER, fast_syscall_user },
  81. { EXCCAUSE_SYSTEM_CALL, 0, system_call },
  82. /* EXCCAUSE_INSTRUCTION_FETCH unhandled */
  83. /* EXCCAUSE_LOAD_STORE_ERROR unhandled*/
  84. { EXCCAUSE_LEVEL1_INTERRUPT, 0, do_interrupt },
  85. { EXCCAUSE_ALLOCA, USER|KRNL, fast_alloca },
  86. /* EXCCAUSE_INTEGER_DIVIDE_BY_ZERO unhandled */
  87. /* EXCCAUSE_PRIVILEGED unhandled */
  88. #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
  89. #ifdef CONFIG_XTENSA_UNALIGNED_USER
  90. { EXCCAUSE_UNALIGNED, USER, fast_unaligned },
  91. #else
  92. { EXCCAUSE_UNALIGNED, 0, do_unaligned_user },
  93. #endif
  94. { EXCCAUSE_UNALIGNED, KRNL, fast_unaligned },
  95. #endif
  96. #ifdef CONFIG_MMU
  97. { EXCCAUSE_ITLB_MISS, 0, do_page_fault },
  98. { EXCCAUSE_ITLB_MISS, USER|KRNL, fast_second_level_miss},
  99. { EXCCAUSE_ITLB_MULTIHIT, 0, do_multihit },
  100. { EXCCAUSE_ITLB_PRIVILEGE, 0, do_page_fault },
  101. /* EXCCAUSE_SIZE_RESTRICTION unhandled */
  102. { EXCCAUSE_FETCH_CACHE_ATTRIBUTE, 0, do_page_fault },
  103. { EXCCAUSE_DTLB_MISS, USER|KRNL, fast_second_level_miss},
  104. { EXCCAUSE_DTLB_MISS, 0, do_page_fault },
  105. { EXCCAUSE_DTLB_MULTIHIT, 0, do_multihit },
  106. { EXCCAUSE_DTLB_PRIVILEGE, 0, do_page_fault },
  107. /* EXCCAUSE_DTLB_SIZE_RESTRICTION unhandled */
  108. { EXCCAUSE_STORE_CACHE_ATTRIBUTE, USER|KRNL, fast_store_prohibited },
  109. { EXCCAUSE_STORE_CACHE_ATTRIBUTE, 0, do_page_fault },
  110. { EXCCAUSE_LOAD_CACHE_ATTRIBUTE, 0, do_page_fault },
  111. #endif /* CONFIG_MMU */
  112. /* XCCHAL_EXCCAUSE_FLOATING_POINT unhandled */
  113. #if XTENSA_HAVE_COPROCESSOR(0)
  114. COPROCESSOR(0),
  115. #endif
  116. #if XTENSA_HAVE_COPROCESSOR(1)
  117. COPROCESSOR(1),
  118. #endif
  119. #if XTENSA_HAVE_COPROCESSOR(2)
  120. COPROCESSOR(2),
  121. #endif
  122. #if XTENSA_HAVE_COPROCESSOR(3)
  123. COPROCESSOR(3),
  124. #endif
  125. #if XTENSA_HAVE_COPROCESSOR(4)
  126. COPROCESSOR(4),
  127. #endif
  128. #if XTENSA_HAVE_COPROCESSOR(5)
  129. COPROCESSOR(5),
  130. #endif
  131. #if XTENSA_HAVE_COPROCESSOR(6)
  132. COPROCESSOR(6),
  133. #endif
  134. #if XTENSA_HAVE_COPROCESSOR(7)
  135. COPROCESSOR(7),
  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. void die(const char*, struct pt_regs*, long);
  146. static inline void
  147. __die_if_kernel(const char *str, struct pt_regs *regs, long err)
  148. {
  149. if (!user_mode(regs))
  150. die(str, regs, err);
  151. }
  152. /*
  153. * Unhandled Exceptions. Kill user task or panic if in kernel space.
  154. */
  155. void do_unhandled(struct pt_regs *regs, unsigned long exccause)
  156. {
  157. __die_if_kernel("Caught unhandled exception - should not happen",
  158. regs, SIGKILL);
  159. /* If in user mode, send SIGILL signal to current process */
  160. printk("Caught unhandled exception in '%s' "
  161. "(pid = %d, pc = %#010lx) - should not happen\n"
  162. "\tEXCCAUSE is %ld\n",
  163. current->comm, task_pid_nr(current), regs->pc, exccause);
  164. force_sig(SIGILL, current);
  165. }
  166. /*
  167. * Multi-hit exception. This if fatal!
  168. */
  169. void do_multihit(struct pt_regs *regs, unsigned long exccause)
  170. {
  171. die("Caught multihit exception", regs, SIGKILL);
  172. }
  173. /*
  174. * IRQ handler.
  175. */
  176. extern void do_IRQ(int, struct pt_regs *);
  177. void do_interrupt(struct pt_regs *regs)
  178. {
  179. static const unsigned int_level_mask[] = {
  180. 0,
  181. XCHAL_INTLEVEL1_MASK,
  182. XCHAL_INTLEVEL2_MASK,
  183. XCHAL_INTLEVEL3_MASK,
  184. XCHAL_INTLEVEL4_MASK,
  185. XCHAL_INTLEVEL5_MASK,
  186. XCHAL_INTLEVEL6_MASK,
  187. XCHAL_INTLEVEL7_MASK,
  188. };
  189. struct pt_regs *old_regs = set_irq_regs(regs);
  190. irq_enter();
  191. for (;;) {
  192. unsigned intread = get_sr(interrupt);
  193. unsigned intenable = get_sr(intenable);
  194. unsigned int_at_level = intread & intenable;
  195. unsigned level;
  196. for (level = LOCKLEVEL; level > 0; --level) {
  197. if (int_at_level & int_level_mask[level]) {
  198. int_at_level &= int_level_mask[level];
  199. break;
  200. }
  201. }
  202. if (level == 0)
  203. break;
  204. do_IRQ(__ffs(int_at_level), regs);
  205. }
  206. irq_exit();
  207. set_irq_regs(old_regs);
  208. }
  209. /*
  210. * Illegal instruction. Fatal if in kernel space.
  211. */
  212. void
  213. do_illegal_instruction(struct pt_regs *regs)
  214. {
  215. __die_if_kernel("Illegal instruction in kernel", regs, SIGKILL);
  216. /* If in user mode, send SIGILL signal to current process. */
  217. printk("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n",
  218. current->comm, task_pid_nr(current), regs->pc);
  219. force_sig(SIGILL, current);
  220. }
  221. /*
  222. * Handle unaligned memory accesses from user space. Kill task.
  223. *
  224. * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory
  225. * accesses causes from user space.
  226. */
  227. #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
  228. #ifndef CONFIG_XTENSA_UNALIGNED_USER
  229. void
  230. do_unaligned_user (struct pt_regs *regs)
  231. {
  232. siginfo_t info;
  233. __die_if_kernel("Unhandled unaligned exception in kernel",
  234. regs, SIGKILL);
  235. current->thread.bad_vaddr = regs->excvaddr;
  236. current->thread.error_code = -3;
  237. printk("Unaligned memory access to %08lx in '%s' "
  238. "(pid = %d, pc = %#010lx)\n",
  239. regs->excvaddr, current->comm, task_pid_nr(current), regs->pc);
  240. info.si_signo = SIGBUS;
  241. info.si_errno = 0;
  242. info.si_code = BUS_ADRALN;
  243. info.si_addr = (void *) regs->excvaddr;
  244. force_sig_info(SIGSEGV, &info, current);
  245. }
  246. #endif
  247. #endif
  248. void
  249. do_debug(struct pt_regs *regs)
  250. {
  251. #ifdef CONFIG_KGDB
  252. /* If remote debugging is configured AND enabled, we give control to
  253. * kgdb. Otherwise, we fall through, perhaps giving control to the
  254. * native debugger.
  255. */
  256. if (gdb_enter) {
  257. extern void gdb_handle_exception(struct pt_regs *);
  258. gdb_handle_exception(regs);
  259. return_from_debug_flag = 1;
  260. return;
  261. }
  262. #endif
  263. __die_if_kernel("Breakpoint in kernel", regs, SIGKILL);
  264. /* If in user mode, send SIGTRAP signal to current process */
  265. force_sig(SIGTRAP, current);
  266. }
  267. static void set_handler(int idx, void *handler)
  268. {
  269. unsigned int cpu;
  270. for_each_possible_cpu(cpu)
  271. per_cpu(exc_table, cpu)[idx] = (unsigned long)handler;
  272. }
  273. /* Set exception C handler - for temporary use when probing exceptions */
  274. void * __init trap_set_handler(int cause, void *handler)
  275. {
  276. void *previous = (void *)per_cpu(exc_table, 0)[
  277. EXC_TABLE_DEFAULT / 4 + cause];
  278. set_handler(EXC_TABLE_DEFAULT / 4 + cause, handler);
  279. return previous;
  280. }
  281. static void trap_init_excsave(void)
  282. {
  283. unsigned long excsave1 = (unsigned long)this_cpu_ptr(exc_table);
  284. __asm__ __volatile__("wsr %0, excsave1\n" : : "a" (excsave1));
  285. }
  286. /*
  287. * Initialize dispatch tables.
  288. *
  289. * The exception vectors are stored compressed the __init section in the
  290. * dispatch_init_table. This function initializes the following three tables
  291. * from that compressed table:
  292. * - fast user first dispatch table for user exceptions
  293. * - fast kernel first dispatch table for kernel exceptions
  294. * - default C-handler C-handler called by the default fast handler.
  295. *
  296. * See vectors.S for more details.
  297. */
  298. void __init trap_init(void)
  299. {
  300. int i;
  301. /* Setup default vectors. */
  302. for(i = 0; i < 64; i++) {
  303. set_handler(EXC_TABLE_FAST_USER/4 + i, user_exception);
  304. set_handler(EXC_TABLE_FAST_KERNEL/4 + i, kernel_exception);
  305. set_handler(EXC_TABLE_DEFAULT/4 + i, do_unhandled);
  306. }
  307. /* Setup specific handlers. */
  308. for(i = 0; dispatch_init_table[i].cause >= 0; i++) {
  309. int fast = dispatch_init_table[i].fast;
  310. int cause = dispatch_init_table[i].cause;
  311. void *handler = dispatch_init_table[i].handler;
  312. if (fast == 0)
  313. set_handler (EXC_TABLE_DEFAULT/4 + cause, handler);
  314. if (fast && fast & USER)
  315. set_handler (EXC_TABLE_FAST_USER/4 + cause, handler);
  316. if (fast && fast & KRNL)
  317. set_handler (EXC_TABLE_FAST_KERNEL/4 + cause, handler);
  318. }
  319. /* Initialize EXCSAVE_1 to hold the address of the exception table. */
  320. trap_init_excsave();
  321. }
  322. #ifdef CONFIG_SMP
  323. void secondary_trap_init(void)
  324. {
  325. trap_init_excsave();
  326. }
  327. #endif
  328. /*
  329. * This function dumps the current valid window frame and other base registers.
  330. */
  331. void show_regs(struct pt_regs * regs)
  332. {
  333. int i, wmask;
  334. show_regs_print_info(KERN_DEFAULT);
  335. wmask = regs->wmask & ~1;
  336. for (i = 0; i < 16; i++) {
  337. if ((i % 8) == 0)
  338. printk(KERN_INFO "a%02d:", i);
  339. printk(KERN_CONT " %08lx", regs->areg[i]);
  340. }
  341. printk(KERN_CONT "\n");
  342. printk("pc: %08lx, ps: %08lx, depc: %08lx, excvaddr: %08lx\n",
  343. regs->pc, regs->ps, regs->depc, regs->excvaddr);
  344. printk("lbeg: %08lx, lend: %08lx lcount: %08lx, sar: %08lx\n",
  345. regs->lbeg, regs->lend, regs->lcount, regs->sar);
  346. if (user_mode(regs))
  347. printk("wb: %08lx, ws: %08lx, wmask: %08lx, syscall: %ld\n",
  348. regs->windowbase, regs->windowstart, regs->wmask,
  349. regs->syscall);
  350. }
  351. static int show_trace_cb(struct stackframe *frame, void *data)
  352. {
  353. if (kernel_text_address(frame->pc)) {
  354. printk(" [<%08lx>] ", frame->pc);
  355. print_symbol("%s\n", frame->pc);
  356. }
  357. return 0;
  358. }
  359. void show_trace(struct task_struct *task, unsigned long *sp)
  360. {
  361. if (!sp)
  362. sp = stack_pointer(task);
  363. printk("Call Trace:");
  364. #ifdef CONFIG_KALLSYMS
  365. printk("\n");
  366. #endif
  367. walk_stackframe(sp, show_trace_cb, NULL);
  368. printk("\n");
  369. }
  370. /*
  371. * This routine abuses get_user()/put_user() to reference pointers
  372. * with at least a bit of error checking ...
  373. */
  374. static int kstack_depth_to_print = 24;
  375. void show_stack(struct task_struct *task, unsigned long *sp)
  376. {
  377. int i = 0;
  378. unsigned long *stack;
  379. if (!sp)
  380. sp = stack_pointer(task);
  381. stack = sp;
  382. printk("\nStack: ");
  383. for (i = 0; i < kstack_depth_to_print; i++) {
  384. if (kstack_end(sp))
  385. break;
  386. if (i && ((i % 8) == 0))
  387. printk("\n ");
  388. printk("%08lx ", *sp++);
  389. }
  390. printk("\n");
  391. show_trace(task, stack);
  392. }
  393. void show_code(unsigned int *pc)
  394. {
  395. long i;
  396. printk("\nCode:");
  397. for(i = -3 ; i < 6 ; i++) {
  398. unsigned long insn;
  399. if (__get_user(insn, pc + i)) {
  400. printk(" (Bad address in pc)\n");
  401. break;
  402. }
  403. printk("%c%08lx%c",(i?' ':'<'),insn,(i?' ':'>'));
  404. }
  405. }
  406. DEFINE_SPINLOCK(die_lock);
  407. void die(const char * str, struct pt_regs * regs, long err)
  408. {
  409. static int die_counter;
  410. int nl = 0;
  411. console_verbose();
  412. spin_lock_irq(&die_lock);
  413. printk("%s: sig: %ld [#%d]\n", str, err, ++die_counter);
  414. #ifdef CONFIG_PREEMPT
  415. printk("PREEMPT ");
  416. nl = 1;
  417. #endif
  418. if (nl)
  419. printk("\n");
  420. show_regs(regs);
  421. if (!user_mode(regs))
  422. show_stack(NULL, (unsigned long*)regs->areg[1]);
  423. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  424. spin_unlock_irq(&die_lock);
  425. if (in_interrupt())
  426. panic("Fatal exception in interrupt");
  427. if (panic_on_oops)
  428. panic("Fatal exception");
  429. do_exit(err);
  430. }