traps.c 13 KB

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