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