vectors.S 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 - 2008 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. #include <asm/vectors.h>
  52. #define WINDOW_VECTORS_SIZE 0x180
  53. /*
  54. * User exception vector. (Exceptions with PS.UM == 1, PS.EXCM == 0)
  55. *
  56. * We get here when an exception occurred while we were in userland.
  57. * We switch to the kernel stack and jump to the first level handler
  58. * associated to the exception cause.
  59. *
  60. * Note: the saved kernel stack pointer (EXC_TABLE_KSTK) is already
  61. * decremented by PT_USER_SIZE.
  62. */
  63. .section .UserExceptionVector.text, "ax"
  64. ENTRY(_UserExceptionVector)
  65. xsr a3, excsave1 # save a3 and get dispatch table
  66. wsr a2, depc # save a2
  67. l32i a2, a3, EXC_TABLE_KSTK # load kernel stack to a2
  68. s32i a0, a2, PT_AREG0 # save a0 to ESF
  69. rsr a0, exccause # retrieve exception cause
  70. s32i a0, a2, PT_DEPC # mark it as a regular exception
  71. addx4 a0, a0, a3 # find entry in table
  72. l32i a0, a0, EXC_TABLE_FAST_USER # load handler
  73. jx a0
  74. ENDPROC(_UserExceptionVector)
  75. /*
  76. * Kernel exception vector. (Exceptions with PS.UM == 0, PS.EXCM == 0)
  77. *
  78. * We get this exception when we were already in kernel space.
  79. * We decrement the current stack pointer (kernel) by PT_SIZE and
  80. * jump to the first-level handler associated with the exception cause.
  81. *
  82. * Note: we need to preserve space for the spill region.
  83. */
  84. .section .KernelExceptionVector.text, "ax"
  85. ENTRY(_KernelExceptionVector)
  86. xsr a3, excsave1 # save a3, and get dispatch table
  87. wsr a2, depc # save a2
  88. addi a2, a1, -16-PT_SIZE # adjust stack pointer
  89. s32i a0, a2, PT_AREG0 # save a0 to ESF
  90. rsr a0, exccause # retrieve exception cause
  91. s32i a0, a2, PT_DEPC # mark it as a regular exception
  92. addx4 a0, a0, a3 # find entry in table
  93. l32i a0, a0, EXC_TABLE_FAST_KERNEL # load handler address
  94. jx a0
  95. ENDPROC(_KernelExceptionVector)
  96. /*
  97. * Double exception vector (Exceptions with PS.EXCM == 1)
  98. * We get this exception when another exception occurs while were are
  99. * already in an exception, such as window overflow/underflow exception,
  100. * or 'expected' exceptions, for example memory exception when we were trying
  101. * to read data from an invalid address in user space.
  102. *
  103. * Note that this vector is never invoked for level-1 interrupts, because such
  104. * interrupts are disabled (masked) when PS.EXCM is set.
  105. *
  106. * We decode the exception and take the appropriate action. However, the
  107. * double exception vector is much more careful, because a lot more error
  108. * cases go through the double exception vector than through the user and
  109. * kernel exception vectors.
  110. *
  111. * Occasionally, the kernel expects a double exception to occur. This usually
  112. * happens when accessing user-space memory with the user's permissions
  113. * (l32e/s32e instructions). The kernel state, though, is not always suitable
  114. * for immediate transfer of control to handle_double, where "normal" exception
  115. * processing occurs. Also in kernel mode, TLB misses can occur if accessing
  116. * vmalloc memory, possibly requiring repair in a double exception handler.
  117. *
  118. * The variable at TABLE_FIXUP offset from the pointer in EXCSAVE_1 doubles as
  119. * a boolean variable and a pointer to a fixup routine. If the variable
  120. * EXC_TABLE_FIXUP is non-zero, this handler jumps to that address. A value of
  121. * zero indicates to use the default kernel/user exception handler.
  122. * There is only one exception, when the value is identical to the exc_table
  123. * label, the kernel is in trouble. This mechanism is used to protect critical
  124. * sections, mainly when the handler writes to the stack to assert the stack
  125. * pointer is valid. Once the fixup/default handler leaves that area, the
  126. * EXC_TABLE_FIXUP variable is reset to the fixup handler or zero.
  127. *
  128. * Procedures wishing to use this mechanism should set EXC_TABLE_FIXUP to the
  129. * nonzero address of a fixup routine before it could cause a double exception
  130. * and reset it before it returns.
  131. *
  132. * Some other things to take care of when a fast exception handler doesn't
  133. * specify a particular fixup handler but wants to use the default handlers:
  134. *
  135. * - The original stack pointer (in a1) must not be modified. The fast
  136. * exception handler should only use a2 as the stack pointer.
  137. *
  138. * - If the fast handler manipulates the stack pointer (in a2), it has to
  139. * register a valid fixup handler and cannot use the default handlers.
  140. *
  141. * - The handler can use any other generic register from a3 to a15, but it
  142. * must save the content of these registers to stack (PT_AREG3...PT_AREGx)
  143. *
  144. * - These registers must be saved before a double exception can occur.
  145. *
  146. * - If we ever implement handling signals while in double exceptions, the
  147. * number of registers a fast handler has saved (excluding a0 and a1) must
  148. * be written to PT_AREG1. (1 if only a3 is used, 2 for a3 and a4, etc. )
  149. *
  150. * The fixup handlers are special handlers:
  151. *
  152. * - Fixup entry conditions differ from regular exceptions:
  153. *
  154. * a0: DEPC
  155. * a1: a1
  156. * a2: trashed, original value in EXC_TABLE_DOUBLE_A2
  157. * a3: exctable
  158. * depc: a0
  159. * excsave_1: a3
  160. *
  161. * - When the kernel enters the fixup handler, it still assumes it is in a
  162. * critical section, so EXC_TABLE_FIXUP variable is set to exc_table.
  163. * The fixup handler, therefore, has to re-register itself as the fixup
  164. * handler before it returns from the double exception.
  165. *
  166. * - Fixup handler can share the same exception frame with the fast handler.
  167. * The kernel stack pointer is not changed when entering the fixup handler.
  168. *
  169. * - Fixup handlers can jump to the default kernel and user exception
  170. * handlers. Before it jumps, though, it has to setup a exception frame
  171. * on stack. Because the default handler resets the register fixup handler
  172. * the fixup handler must make sure that the default handler returns to
  173. * it instead of the exception address, so it can re-register itself as
  174. * the fixup handler.
  175. *
  176. * In case of a critical condition where the kernel cannot recover, we jump
  177. * to unrecoverable_exception with the following entry conditions.
  178. * All registers a0...a15 are unchanged from the last exception, except:
  179. *
  180. * a0: last address before we jumped to the unrecoverable_exception.
  181. * excsave_1: a0
  182. *
  183. *
  184. * See the handle_alloca_user and spill_registers routines for example clients.
  185. *
  186. * FIXME: Note: we currently don't allow signal handling coming from a double
  187. * exception, so the item markt with (*) is not required.
  188. */
  189. .section .DoubleExceptionVector.text, "ax"
  190. .begin literal_prefix .DoubleExceptionVector
  191. ENTRY(_DoubleExceptionVector)
  192. /* Deliberately destroy excsave (don't assume it's value was valid). */
  193. wsr a3, excsave1 # save a3
  194. /* Check for kernel double exception (usually fatal). */
  195. rsr a3, ps
  196. _bbci.l a3, PS_UM_BIT, .Lksp
  197. /* Check if we are currently handling a window exception. */
  198. /* Note: We don't need to indicate that we enter a critical section. */
  199. xsr a0, depc # get DEPC, save a0
  200. movi a3, WINDOW_VECTORS_VADDR
  201. _bltu a0, a3, .Lfixup
  202. addi a3, a3, WINDOW_VECTORS_SIZE
  203. _bgeu a0, a3, .Lfixup
  204. /* Window overflow/underflow exception. Get stack pointer. */
  205. mov a3, a2
  206. /* This explicit literal and the following references to it are made
  207. * in order to fit DoubleExceptionVector.literals into the available
  208. * 16-byte gap before DoubleExceptionVector.text in the absence of
  209. * link time relaxation. See kernel/vmlinux.lds.S
  210. */
  211. .literal .Lexc_table, exc_table
  212. l32r a2, .Lexc_table
  213. l32i a2, a2, EXC_TABLE_KSTK
  214. /* Check for overflow/underflow exception, jump if overflow. */
  215. _bbci.l a0, 6, .Lovfl
  216. /* a0: depc, a1: a1, a2: kstk, a3: a2, depc: a0, excsave: a3 */
  217. /* Restart window underflow exception.
  218. * We return to the instruction in user space that caused the window
  219. * underflow exception. Therefore, we change window base to the value
  220. * before we entered the window underflow exception and prepare the
  221. * registers to return as if we were coming from a regular exception
  222. * by changing depc (in a0).
  223. * Note: We can trash the current window frame (a0...a3) and depc!
  224. */
  225. wsr a2, depc # save stack pointer temporarily
  226. rsr a0, ps
  227. extui a0, a0, PS_OWB_SHIFT, 4
  228. wsr a0, windowbase
  229. rsync
  230. /* We are now in the previous window frame. Save registers again. */
  231. xsr a2, depc # save a2 and get stack pointer
  232. s32i a0, a2, PT_AREG0
  233. wsr a3, excsave1 # save a3
  234. l32r a3, .Lexc_table
  235. rsr a0, exccause
  236. s32i a0, a2, PT_DEPC # mark it as a regular exception
  237. addx4 a0, a0, a3
  238. l32i a0, a0, EXC_TABLE_FAST_USER
  239. jx a0
  240. .Lfixup:/* Check for a fixup handler or if we were in a critical section. */
  241. /* a0: depc, a1: a1, a2: a2, a3: trashed, depc: a0, excsave1: a3 */
  242. l32r a3, .Lexc_table
  243. s32i a2, a3, EXC_TABLE_DOUBLE_SAVE # temporary variable
  244. /* Enter critical section. */
  245. l32i a2, a3, EXC_TABLE_FIXUP
  246. s32i a3, a3, EXC_TABLE_FIXUP
  247. beq a2, a3, .Lunrecoverable_fixup # critical!
  248. beqz a2, .Ldflt # no handler was registered
  249. /* a0: depc, a1: a1, a2: trash, a3: exctable, depc: a0, excsave: a3 */
  250. jx a2
  251. .Ldflt: /* Get stack pointer. */
  252. l32i a3, a3, EXC_TABLE_DOUBLE_SAVE
  253. addi a2, a3, -PT_USER_SIZE
  254. .Lovfl: /* Jump to default handlers. */
  255. /* a0: depc, a1: a1, a2: kstk, a3: a2, depc: a0, excsave: a3 */
  256. xsr a3, depc
  257. s32i a0, a2, PT_DEPC
  258. s32i a3, a2, PT_AREG0
  259. /* a0: avail, a1: a1, a2: kstk, a3: avail, depc: a2, excsave: a3 */
  260. l32r a3, .Lexc_table
  261. rsr a0, exccause
  262. addx4 a0, a0, a3
  263. l32i a0, a0, EXC_TABLE_FAST_USER
  264. jx a0
  265. /*
  266. * We only allow the ITLB miss exception if we are in kernel space.
  267. * All other exceptions are unexpected and thus unrecoverable!
  268. */
  269. #ifdef CONFIG_MMU
  270. .extern fast_second_level_miss_double_kernel
  271. .Lksp: /* a0: a0, a1: a1, a2: a2, a3: trashed, depc: depc, excsave: a3 */
  272. rsr a3, exccause
  273. beqi a3, EXCCAUSE_ITLB_MISS, 1f
  274. addi a3, a3, -EXCCAUSE_DTLB_MISS
  275. bnez a3, .Lunrecoverable
  276. 1: movi a3, fast_second_level_miss_double_kernel
  277. jx a3
  278. #else
  279. .equ .Lksp, .Lunrecoverable
  280. #endif
  281. /* Critical! We can't handle this situation. PANIC! */
  282. .extern unrecoverable_exception
  283. .Lunrecoverable_fixup:
  284. l32i a2, a3, EXC_TABLE_DOUBLE_SAVE
  285. xsr a0, depc
  286. .Lunrecoverable:
  287. rsr a3, excsave1
  288. wsr a0, excsave1
  289. movi a0, unrecoverable_exception
  290. callx0 a0
  291. .end literal_prefix
  292. ENDPROC(_DoubleExceptionVector)
  293. /*
  294. * Debug interrupt vector
  295. *
  296. * There is not much space here, so simply jump to another handler.
  297. * EXCSAVE[DEBUGLEVEL] has been set to that handler.
  298. */
  299. .section .DebugInterruptVector.text, "ax"
  300. ENTRY(_DebugInterruptVector)
  301. xsr a0, SREG_EXCSAVE + XCHAL_DEBUGLEVEL
  302. jx a0
  303. ENDPROC(_DebugInterruptVector)
  304. /*
  305. * Medium priority level interrupt vectors
  306. *
  307. * Each takes less than 16 (0x10) bytes, no literals, by placing
  308. * the extra 8 bytes that would otherwise be required in the window
  309. * vectors area where there is space. With relocatable vectors,
  310. * all vectors are within ~ 4 kB range of each other, so we can
  311. * simply jump (J) to another vector without having to use JX.
  312. *
  313. * common_exception code gets current IRQ level in PS.INTLEVEL
  314. * and preserves it for the IRQ handling time.
  315. */
  316. .macro irq_entry_level level
  317. .if XCHAL_EXCM_LEVEL >= \level
  318. .section .Level\level\()InterruptVector.text, "ax"
  319. ENTRY(_Level\level\()InterruptVector)
  320. wsr a0, excsave2
  321. rsr a0, epc\level
  322. wsr a0, epc1
  323. movi a0, EXCCAUSE_LEVEL1_INTERRUPT
  324. wsr a0, exccause
  325. rsr a0, eps\level
  326. # branch to user or kernel vector
  327. j _SimulateUserKernelVectorException
  328. .endif
  329. .endm
  330. irq_entry_level 2
  331. irq_entry_level 3
  332. irq_entry_level 4
  333. irq_entry_level 5
  334. irq_entry_level 6
  335. /* Window overflow and underflow handlers.
  336. * The handlers must be 64 bytes apart, first starting with the underflow
  337. * handlers underflow-4 to underflow-12, then the overflow handlers
  338. * overflow-4 to overflow-12.
  339. *
  340. * Note: We rerun the underflow handlers if we hit an exception, so
  341. * we try to access any page that would cause a page fault early.
  342. */
  343. #define ENTRY_ALIGN64(name) \
  344. .globl name; \
  345. .align 64; \
  346. name:
  347. .section .WindowVectors.text, "ax"
  348. /* 4-Register Window Overflow Vector (Handler) */
  349. ENTRY_ALIGN64(_WindowOverflow4)
  350. s32e a0, a5, -16
  351. s32e a1, a5, -12
  352. s32e a2, a5, -8
  353. s32e a3, a5, -4
  354. rfwo
  355. ENDPROC(_WindowOverflow4)
  356. #if XCHAL_EXCM_LEVEL >= 2
  357. /* Not a window vector - but a convenient location
  358. * (where we know there's space) for continuation of
  359. * medium priority interrupt dispatch code.
  360. * On entry here, a0 contains PS, and EPC2 contains saved a0:
  361. */
  362. .align 4
  363. _SimulateUserKernelVectorException:
  364. addi a0, a0, (1 << PS_EXCM_BIT)
  365. wsr a0, ps
  366. bbsi.l a0, PS_UM_BIT, 1f # branch if user mode
  367. rsr a0, excsave2 # restore a0
  368. j _KernelExceptionVector # simulate kernel vector exception
  369. 1: rsr a0, excsave2 # restore a0
  370. j _UserExceptionVector # simulate user vector exception
  371. #endif
  372. /* 4-Register Window Underflow Vector (Handler) */
  373. ENTRY_ALIGN64(_WindowUnderflow4)
  374. l32e a0, a5, -16
  375. l32e a1, a5, -12
  376. l32e a2, a5, -8
  377. l32e a3, a5, -4
  378. rfwu
  379. ENDPROC(_WindowUnderflow4)
  380. /* 8-Register Window Overflow Vector (Handler) */
  381. ENTRY_ALIGN64(_WindowOverflow8)
  382. s32e a0, a9, -16
  383. l32e a0, a1, -12
  384. s32e a2, a9, -8
  385. s32e a1, a9, -12
  386. s32e a3, a9, -4
  387. s32e a4, a0, -32
  388. s32e a5, a0, -28
  389. s32e a6, a0, -24
  390. s32e a7, a0, -20
  391. rfwo
  392. ENDPROC(_WindowOverflow8)
  393. /* 8-Register Window Underflow Vector (Handler) */
  394. ENTRY_ALIGN64(_WindowUnderflow8)
  395. l32e a1, a9, -12
  396. l32e a0, a9, -16
  397. l32e a7, a1, -12
  398. l32e a2, a9, -8
  399. l32e a4, a7, -32
  400. l32e a3, a9, -4
  401. l32e a5, a7, -28
  402. l32e a6, a7, -24
  403. l32e a7, a7, -20
  404. rfwu
  405. ENDPROC(_WindowUnderflow8)
  406. /* 12-Register Window Overflow Vector (Handler) */
  407. ENTRY_ALIGN64(_WindowOverflow12)
  408. s32e a0, a13, -16
  409. l32e a0, a1, -12
  410. s32e a1, a13, -12
  411. s32e a2, a13, -8
  412. s32e a3, a13, -4
  413. s32e a4, a0, -48
  414. s32e a5, a0, -44
  415. s32e a6, a0, -40
  416. s32e a7, a0, -36
  417. s32e a8, a0, -32
  418. s32e a9, a0, -28
  419. s32e a10, a0, -24
  420. s32e a11, a0, -20
  421. rfwo
  422. ENDPROC(_WindowOverflow12)
  423. /* 12-Register Window Underflow Vector (Handler) */
  424. ENTRY_ALIGN64(_WindowUnderflow12)
  425. l32e a1, a13, -12
  426. l32e a0, a13, -16
  427. l32e a11, a1, -12
  428. l32e a2, a13, -8
  429. l32e a4, a11, -48
  430. l32e a8, a11, -32
  431. l32e a3, a13, -4
  432. l32e a5, a11, -44
  433. l32e a6, a11, -40
  434. l32e a7, a11, -36
  435. l32e a9, a11, -28
  436. l32e a10, a11, -24
  437. l32e a11, a11, -20
  438. rfwu
  439. ENDPROC(_WindowUnderflow12)
  440. .text