traps.c 12 KB

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