vectors.S 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * arch/xtensa/kernel/vectors.S
  3. *
  4. * This file contains all exception vectors (user, kernel, and double),
  5. * as well as the window vectors (overflow and underflow), and the debug
  6. * vector. These are the primary vectors executed by the processor if an
  7. * exception occurs.
  8. *
  9. * This file is subject to the terms and conditions of the GNU General
  10. * Public License. See the file "COPYING" in the main directory of
  11. * this archive for more details.
  12. *
  13. * Copyright (C) 2005 Tensilica, Inc.
  14. *
  15. * Chris Zankel <chris@zankel.net>
  16. *
  17. */
  18. /*
  19. * We use a two-level table approach. The user and kernel exception vectors
  20. * use a first-level dispatch table to dispatch the exception to a registered
  21. * fast handler or the default handler, if no fast handler was registered.
  22. * The default handler sets up a C-stack and dispatches the exception to a
  23. * registerd C handler in the second-level dispatch table.
  24. *
  25. * Fast handler entry condition:
  26. *
  27. * a0: trashed, original value saved on stack (PT_AREG0)
  28. * a1: a1
  29. * a2: new stack pointer, original value in depc
  30. * a3: dispatch table
  31. * depc: a2, original value saved on stack (PT_DEPC)
  32. * excsave_1: a3
  33. *
  34. * The value for PT_DEPC saved to stack also functions as a boolean to
  35. * indicate that the exception is either a double or a regular exception:
  36. *
  37. * PT_DEPC >= VALID_DOUBLE_EXCEPTION_ADDRESS: double exception
  38. * < VALID_DOUBLE_EXCEPTION_ADDRESS: regular exception
  39. *
  40. * Note: Neither the kernel nor the user exception handler generate literals.
  41. *
  42. */
  43. #include <linux/linkage.h>
  44. #include <asm/ptrace.h>
  45. #include <asm/current.h>
  46. #include <asm/asm-offsets.h>
  47. #include <asm/pgtable.h>
  48. #include <asm/processor.h>
  49. #include <asm/page.h>
  50. #include <asm/thread_info.h>
  51. #define WINDOW_VECTORS_SIZE 0x180
  52. /*
  53. * User exception vector. (Exceptions with PS.UM == 1, PS.EXCM == 0)
  54. *
  55. * We get here when an exception occurred while we were in userland.
  56. * We switch to the kernel stack and jump to the first level handler
  57. * associated to the exception cause.
  58. *
  59. * Note: the saved kernel stack pointer (EXC_TABLE_KSTK) is already
  60. * decremented by PT_USER_SIZE.
  61. */
  62. .section .UserExceptionVector.text, "ax"
  63. ENTRY(_UserExceptionVector)
  64. xsr a3, EXCSAVE_1 # save a3 and get dispatch table
  65. wsr a2, DEPC # save a2
  66. l32i a2, a3, EXC_TABLE_KSTK # load kernel stack to a2
  67. s32i a0, a2, PT_AREG0 # save a0 to ESF
  68. rsr a0, EXCCAUSE # retrieve exception cause
  69. s32i a0, a2, PT_DEPC # mark it as a regular exception
  70. addx4 a0, a0, a3 # find entry in table
  71. l32i a0, a0, EXC_TABLE_FAST_USER # load handler
  72. jx a0
  73. /*
  74. * Kernel exception vector. (Exceptions with PS.UM == 0, PS.EXCM == 0)
  75. *
  76. * We get this exception when we were already in kernel space.
  77. * We decrement the current stack pointer (kernel) by PT_SIZE and
  78. * jump to the first-level handler associated with the exception cause.
  79. *
  80. * Note: we need to preserve space for the spill region.
  81. */
  82. .section .KernelExceptionVector.text, "ax"
  83. ENTRY(_KernelExceptionVector)
  84. xsr a3, EXCSAVE_1 # save a3, and get dispatch table
  85. wsr a2, DEPC # save a2
  86. addi a2, a1, -16-PT_SIZE # adjust stack pointer
  87. s32i a0, a2, PT_AREG0 # save a0 to ESF
  88. rsr a0, EXCCAUSE # retrieve exception cause
  89. s32i a0, a2, PT_DEPC # mark it as a regular exception
  90. addx4 a0, a0, a3 # find entry in table
  91. l32i a0, a0, EXC_TABLE_FAST_KERNEL # load handler address
  92. jx a0
  93. /*
  94. * Double exception vector (Exceptions with PS.EXCM == 1)
  95. * We get this exception when another exception occurs while were are
  96. * already in an exception, such as window overflow/underflow exception,
  97. * or 'expected' exceptions, for example memory exception when we were trying
  98. * to read data from an invalid address in user space.
  99. *
  100. * Note that this vector is never invoked for level-1 interrupts, because such
  101. * interrupts are disabled (masked) when PS.EXCM is set.
  102. *
  103. * We decode the exception and take the appropriate action. However, the
  104. * double exception vector is much more careful, because a lot more error
  105. * cases go through the double exception vector than through the user and
  106. * kernel exception vectors.
  107. *
  108. * Occasionally, the kernel expects a double exception to occur. This usually
  109. * happens when accessing user-space memory with the user's permissions
  110. * (l32e/s32e instructions). The kernel state, though, is not always suitable
  111. * for immediate transfer of control to handle_double, where "normal" exception
  112. * processing occurs. Also in kernel mode, TLB misses can occur if accessing
  113. * vmalloc memory, possibly requiring repair in a double exception handler.
  114. *
  115. * The variable at TABLE_FIXUP offset from the pointer in EXCSAVE_1 doubles as
  116. * a boolean variable and a pointer to a fixup routine. If the variable
  117. * EXC_TABLE_FIXUP is non-zero, this handler jumps to that address. A value of
  118. * zero indicates to use the default kernel/user exception handler.
  119. * There is only one exception, when the value is identical to the exc_table
  120. * label, the kernel is in trouble. This mechanism is used to protect critical
  121. * sections, mainly when the handler writes to the stack to assert the stack
  122. * pointer is valid. Once the fixup/default handler leaves that area, the
  123. * EXC_TABLE_FIXUP variable is reset to the fixup handler or zero.
  124. *
  125. * Procedures wishing to use this mechanism should set EXC_TABLE_FIXUP to the
  126. * nonzero address of a fixup routine before it could cause a double exception
  127. * and reset it before it returns.
  128. *
  129. * Some other things to take care of when a fast exception handler doesn't
  130. * specify a particular fixup handler but wants to use the default handlers:
  131. *
  132. * - The original stack pointer (in a1) must not be modified. The fast
  133. * exception handler should only use a2 as the stack pointer.
  134. *
  135. * - If the fast handler manipulates the stack pointer (in a2), it has to
  136. * register a valid fixup handler and cannot use the default handlers.
  137. *
  138. * - The handler can use any other generic register from a3 to a15, but it
  139. * must save the content of these registers to stack (PT_AREG3...PT_AREGx)
  140. *
  141. * - These registers must be saved before a double exception can occur.
  142. *
  143. * - If we ever implement handling signals while in double exceptions, the
  144. * number of registers a fast handler has saved (excluding a0 and a1) must
  145. * be written to PT_AREG1. (1 if only a3 is used, 2 for a3 and a4, etc. )
  146. *
  147. * The fixup handlers are special handlers:
  148. *
  149. * - Fixup entry conditions differ from regular exceptions:
  150. *
  151. * a0: DEPC
  152. * a1: a1
  153. * a2: trashed, original value in EXC_TABLE_DOUBLE_A2
  154. * a3: exctable
  155. * depc: a0
  156. * excsave_1: a3
  157. *
  158. * - When the kernel enters the fixup handler, it still assumes it is in a
  159. * critical section, so EXC_TABLE_FIXUP variable is set to exc_table.
  160. * The fixup handler, therefore, has to re-register itself as the fixup
  161. * handler before it returns from the double exception.
  162. *
  163. * - Fixup handler can share the same exception frame with the fast handler.
  164. * The kernel stack pointer is not changed when entering the fixup handler.
  165. *
  166. * - Fixup handlers can jump to the default kernel and user exception
  167. * handlers. Before it jumps, though, it has to setup a exception frame
  168. * on stack. Because the default handler resets the register fixup handler
  169. * the fixup handler must make sure that the default handler returns to
  170. * it instead of the exception address, so it can re-register itself as
  171. * the fixup handler.
  172. *
  173. * In case of a critical condition where the kernel cannot recover, we jump
  174. * to unrecoverable_exception with the following entry conditions.
  175. * All registers a0...a15 are unchanged from the last exception, except:
  176. *
  177. * a0: last address before we jumped to the unrecoverable_exception.
  178. * excsave_1: a0
  179. *
  180. *
  181. * See the handle_alloca_user and spill_registers routines for example clients.
  182. *
  183. * FIXME: Note: we currently don't allow signal handling coming from a double
  184. * exception, so the item markt with (*) is not required.
  185. */
  186. .section .DoubleExceptionVector.text, "ax"
  187. .begin literal_prefix .DoubleExceptionVector
  188. ENTRY(_DoubleExceptionVector)
  189. /* Deliberately destroy excsave (don't assume it's value was valid). */
  190. wsr a3, EXCSAVE_1 # save a3
  191. /* Check for kernel double exception (usually fatal). */
  192. rsr a3, PS
  193. _bbci.l a3, PS_UM_BIT, .Lksp
  194. /* Check if we are currently handling a window exception. */
  195. /* Note: We don't need to indicate that we enter a critical section. */
  196. xsr a0, DEPC # get DEPC, save a0
  197. movi a3, XCHAL_WINDOW_VECTORS_VADDR
  198. _bltu a0, a3, .Lfixup
  199. addi a3, a3, WINDOW_VECTORS_SIZE
  200. _bgeu a0, a3, .Lfixup
  201. /* Window overflow/underflow exception. Get stack pointer. */
  202. mov a3, a2
  203. movi a2, exc_table
  204. l32i a2, a2, EXC_TABLE_KSTK
  205. /* Check for overflow/underflow exception, jump if overflow. */
  206. _bbci.l a0, 6, .Lovfl
  207. /* a0: depc, a1: a1, a2: kstk, a3: a2, depc: a0, excsave: a3 */
  208. /* Restart window underflow exception.
  209. * We return to the instruction in user space that caused the window
  210. * underflow exception. Therefore, we change window base to the value
  211. * before we entered the window underflow exception and prepare the
  212. * registers to return as if we were coming from a regular exception
  213. * by changing depc (in a0).
  214. * Note: We can trash the current window frame (a0...a3) and depc!
  215. */
  216. wsr a2, DEPC # save stack pointer temporarily
  217. rsr a0, PS
  218. extui a0, a0, PS_OWB_SHIFT, 4
  219. wsr a0, WINDOWBASE
  220. rsync
  221. /* We are now in the previous window frame. Save registers again. */
  222. xsr a2, DEPC # save a2 and get stack pointer
  223. s32i a0, a2, PT_AREG0
  224. wsr a3, EXCSAVE_1 # save a3
  225. movi a3, exc_table
  226. rsr a0, EXCCAUSE
  227. s32i a0, a2, PT_DEPC # mark it as a regular exception
  228. addx4 a0, a0, a3
  229. l32i a0, a0, EXC_TABLE_FAST_USER
  230. jx a0
  231. .Lfixup:/* Check for a fixup handler or if we were in a critical section. */
  232. /* a0: depc, a1: a1, a2: a2, a3: trashed, depc: a0, excsave1: a3 */
  233. movi a3, exc_table
  234. s32i a2, a3, EXC_TABLE_DOUBLE_SAVE # temporary variable
  235. /* Enter critical section. */
  236. l32i a2, a3, EXC_TABLE_FIXUP
  237. s32i a3, a3, EXC_TABLE_FIXUP
  238. beq a2, a3, .Lunrecoverable_fixup # critical!
  239. beqz a2, .Ldflt # no handler was registered
  240. /* a0: depc, a1: a1, a2: trash, a3: exctable, depc: a0, excsave: a3 */
  241. jx a2
  242. .Ldflt: /* Get stack pointer. */
  243. l32i a3, a3, EXC_TABLE_DOUBLE_SAVE
  244. addi a2, a3, -PT_USER_SIZE
  245. .Lovfl: /* Jump to default handlers. */
  246. /* a0: depc, a1: a1, a2: kstk, a3: a2, depc: a0, excsave: a3 */
  247. xsr a3, DEPC
  248. s32i a0, a2, PT_DEPC
  249. s32i a3, a2, PT_AREG0
  250. /* a0: avail, a1: a1, a2: kstk, a3: avail, depc: a2, excsave: a3 */
  251. movi a3, exc_table
  252. rsr a0, EXCCAUSE
  253. addx4 a0, a0, a3
  254. l32i a0, a0, EXC_TABLE_FAST_USER
  255. jx a0
  256. /*
  257. * We only allow the ITLB miss exception if we are in kernel space.
  258. * All other exceptions are unexpected and thus unrecoverable!
  259. */
  260. #ifdef CONFIG_MMU
  261. .extern fast_second_level_miss_double_kernel
  262. .Lksp: /* a0: a0, a1: a1, a2: a2, a3: trashed, depc: depc, excsave: a3 */
  263. rsr a3, EXCCAUSE
  264. beqi a3, EXCCAUSE_ITLB_MISS, 1f
  265. addi a3, a3, -EXCCAUSE_DTLB_MISS
  266. bnez a3, .Lunrecoverable
  267. 1: movi a3, fast_second_level_miss_double_kernel
  268. jx a3
  269. #else
  270. .equ .Lksp, .Lunrecoverable
  271. #endif
  272. /* Critical! We can't handle this situation. PANIC! */
  273. .extern unrecoverable_exception
  274. .Lunrecoverable_fixup:
  275. l32i a2, a3, EXC_TABLE_DOUBLE_SAVE
  276. xsr a0, DEPC
  277. .Lunrecoverable:
  278. rsr a3, EXCSAVE_1
  279. wsr a0, EXCSAVE_1
  280. movi a0, unrecoverable_exception
  281. callx0 a0
  282. .end literal_prefix
  283. /*
  284. * Debug interrupt vector
  285. *
  286. * There is not much space here, so simply jump to another handler.
  287. * EXCSAVE[DEBUGLEVEL] has been set to that handler.
  288. */
  289. .section .DebugInterruptVector.text, "ax"
  290. ENTRY(_DebugInterruptVector)
  291. xsr a0, EXCSAVE + XCHAL_DEBUGLEVEL
  292. jx a0
  293. /* Window overflow and underflow handlers.
  294. * The handlers must be 64 bytes apart, first starting with the underflow
  295. * handlers underflow-4 to underflow-12, then the overflow handlers
  296. * overflow-4 to overflow-12.
  297. *
  298. * Note: We rerun the underflow handlers if we hit an exception, so
  299. * we try to access any page that would cause a page fault early.
  300. */
  301. .section .WindowVectors.text, "ax"
  302. /* 4-Register Window Overflow Vector (Handler) */
  303. .align 64
  304. .global _WindowOverflow4
  305. _WindowOverflow4:
  306. s32e a0, a5, -16
  307. s32e a1, a5, -12
  308. s32e a2, a5, -8
  309. s32e a3, a5, -4
  310. rfwo
  311. /* 4-Register Window Underflow Vector (Handler) */
  312. .align 64
  313. .global _WindowUnderflow4
  314. _WindowUnderflow4:
  315. l32e a0, a5, -16
  316. l32e a1, a5, -12
  317. l32e a2, a5, -8
  318. l32e a3, a5, -4
  319. rfwu
  320. /* 8-Register Window Overflow Vector (Handler) */
  321. .align 64
  322. .global _WindowOverflow8
  323. _WindowOverflow8:
  324. s32e a0, a9, -16
  325. l32e a0, a1, -12
  326. s32e a2, a9, -8
  327. s32e a1, a9, -12
  328. s32e a3, a9, -4
  329. s32e a4, a0, -32
  330. s32e a5, a0, -28
  331. s32e a6, a0, -24
  332. s32e a7, a0, -20
  333. rfwo
  334. /* 8-Register Window Underflow Vector (Handler) */
  335. .align 64
  336. .global _WindowUnderflow8
  337. _WindowUnderflow8:
  338. l32e a1, a9, -12
  339. l32e a0, a9, -16
  340. l32e a7, a1, -12
  341. l32e a2, a9, -8
  342. l32e a4, a7, -32
  343. l32e a3, a9, -4
  344. l32e a5, a7, -28
  345. l32e a6, a7, -24
  346. l32e a7, a7, -20
  347. rfwu
  348. /* 12-Register Window Overflow Vector (Handler) */
  349. .align 64
  350. .global _WindowOverflow12
  351. _WindowOverflow12:
  352. s32e a0, a13, -16
  353. l32e a0, a1, -12
  354. s32e a1, a13, -12
  355. s32e a2, a13, -8
  356. s32e a3, a13, -4
  357. s32e a4, a0, -48
  358. s32e a5, a0, -44
  359. s32e a6, a0, -40
  360. s32e a7, a0, -36
  361. s32e a8, a0, -32
  362. s32e a9, a0, -28
  363. s32e a10, a0, -24
  364. s32e a11, a0, -20
  365. rfwo
  366. /* 12-Register Window Underflow Vector (Handler) */
  367. .align 64
  368. .global _WindowUnderflow12
  369. _WindowUnderflow12:
  370. l32e a1, a13, -12
  371. l32e a0, a13, -16
  372. l32e a11, a1, -12
  373. l32e a2, a13, -8
  374. l32e a4, a11, -48
  375. l32e a8, a11, -32
  376. l32e a3, a13, -4
  377. l32e a5, a11, -44
  378. l32e a6, a11, -40
  379. l32e a7, a11, -36
  380. l32e a9, a11, -28
  381. l32e a10, a11, -24
  382. l32e a11, a11, -20
  383. rfwu
  384. .text