PageRenderTime 1815ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/arch/xtensa/kernel/traps.c

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