xtensa_context.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  3. * Copyright (C) 2015-2019 Cadence Design Systems, Inc.
  4. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  5. *
  6. * SPDX-License-Identifier: MIT
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  9. * this software and associated documentation files (the "Software"), to deal in
  10. * the Software without restriction, including without limitation the rights to
  11. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  12. * the Software, and to permit persons to whom the Software is furnished to do so,
  13. * subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  20. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  21. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  22. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * https://www.FreeRTOS.org
  26. * https://github.com/FreeRTOS
  27. *
  28. */
  29. /*
  30. * XTENSA CONTEXT FRAMES AND MACROS FOR RTOS ASSEMBLER SOURCES
  31. *
  32. * This header contains definitions and macros for use primarily by Xtensa
  33. * RTOS assembly coded source files. It includes and uses the Xtensa hardware
  34. * abstraction layer (HAL) to deal with config specifics. It may also be
  35. * included in C source files.
  36. *
  37. * !! Supports only Xtensa Exception Architecture 2 (XEA2). XEA1 not supported. !!
  38. *
  39. * NOTE: The Xtensa architecture requires stack pointer alignment to 16 bytes.
  40. */
  41. #ifndef XTENSA_CONTEXT_H
  42. #define XTENSA_CONTEXT_H
  43. #ifdef __ASSEMBLER__
  44. #include <xtensa/coreasm.h>
  45. #endif
  46. #include <xtensa/config/tie.h>
  47. #include <xtensa/corebits.h>
  48. #include <xtensa/config/system.h>
  49. /* Align a value up to nearest n-byte boundary, where n is a power of 2. */
  50. #define ALIGNUP(n, val) (((val) + (n)-1) & -(n))
  51. /*
  52. -------------------------------------------------------------------------------
  53. Macros that help define structures for both C and assembler.
  54. -------------------------------------------------------------------------------
  55. */
  56. #if defined(_ASMLANGUAGE) || defined(__ASSEMBLER__)
  57. #define STRUCT_BEGIN .pushsection .text; .struct 0
  58. #define STRUCT_FIELD(ctype,size,asname,name) asname: .space size
  59. #define STRUCT_AFIELD(ctype,size,asname,name,n) asname: .space (size)*(n)
  60. #define STRUCT_END(sname) sname##Size:; .popsection
  61. #else
  62. #define STRUCT_BEGIN typedef struct {
  63. #define STRUCT_FIELD(ctype,size,asname,name) ctype name;
  64. #define STRUCT_AFIELD(ctype,size,asname,name,n) ctype name[n];
  65. #define STRUCT_END(sname) } sname;
  66. #endif //_ASMLANGUAGE || __ASSEMBLER__
  67. /*
  68. -------------------------------------------------------------------------------
  69. INTERRUPT/EXCEPTION STACK FRAME FOR A THREAD OR NESTED INTERRUPT
  70. A stack frame of this structure is allocated for any interrupt or exception.
  71. It goes on the current stack. If the RTOS has a system stack for handling
  72. interrupts, every thread stack must allow space for just one interrupt stack
  73. frame, then nested interrupt stack frames go on the system stack.
  74. The frame includes basic registers (explicit) and "extra" registers introduced
  75. by user TIE or the use of the MAC16 option in the user's Xtensa config.
  76. The frame size is minimized by omitting regs not applicable to user's config.
  77. For Windowed ABI, this stack frame includes the interruptee's base save area,
  78. another base save area to manage gcc nested functions, and a little temporary
  79. space to help manage the spilling of the register windows.
  80. -------------------------------------------------------------------------------
  81. */
  82. STRUCT_BEGIN
  83. STRUCT_FIELD (long, 4, XT_STK_EXIT, exit) /* exit point for dispatch */
  84. STRUCT_FIELD (long, 4, XT_STK_PC, pc) /* return PC */
  85. STRUCT_FIELD (long, 4, XT_STK_PS, ps) /* return PS */
  86. STRUCT_FIELD (long, 4, XT_STK_A0, a0)
  87. STRUCT_FIELD (long, 4, XT_STK_A1, a1) /* stack pointer before interrupt */
  88. STRUCT_FIELD (long, 4, XT_STK_A2, a2)
  89. STRUCT_FIELD (long, 4, XT_STK_A3, a3)
  90. STRUCT_FIELD (long, 4, XT_STK_A4, a4)
  91. STRUCT_FIELD (long, 4, XT_STK_A5, a5)
  92. STRUCT_FIELD (long, 4, XT_STK_A6, a6)
  93. STRUCT_FIELD (long, 4, XT_STK_A7, a7)
  94. STRUCT_FIELD (long, 4, XT_STK_A8, a8)
  95. STRUCT_FIELD (long, 4, XT_STK_A9, a9)
  96. STRUCT_FIELD (long, 4, XT_STK_A10, a10)
  97. STRUCT_FIELD (long, 4, XT_STK_A11, a11)
  98. STRUCT_FIELD (long, 4, XT_STK_A12, a12)
  99. STRUCT_FIELD (long, 4, XT_STK_A13, a13)
  100. STRUCT_FIELD (long, 4, XT_STK_A14, a14)
  101. STRUCT_FIELD (long, 4, XT_STK_A15, a15)
  102. STRUCT_FIELD (long, 4, XT_STK_SAR, sar)
  103. STRUCT_FIELD (long, 4, XT_STK_EXCCAUSE, exccause)
  104. STRUCT_FIELD (long, 4, XT_STK_EXCVADDR, excvaddr)
  105. #if XCHAL_HAVE_LOOPS
  106. STRUCT_FIELD (long, 4, XT_STK_LBEG, lbeg)
  107. STRUCT_FIELD (long, 4, XT_STK_LEND, lend)
  108. STRUCT_FIELD (long, 4, XT_STK_LCOUNT, lcount)
  109. #endif
  110. #ifndef __XTENSA_CALL0_ABI__
  111. /* Temporary space for saving stuff during window spill */
  112. STRUCT_FIELD (long, 4, XT_STK_TMP0, tmp0)
  113. STRUCT_FIELD (long, 4, XT_STK_TMP1, tmp1)
  114. STRUCT_FIELD (long, 4, XT_STK_TMP2, tmp2)
  115. #endif
  116. #ifdef XT_USE_SWPRI
  117. /* Storage for virtual priority mask */
  118. STRUCT_FIELD (long, 4, XT_STK_VPRI, vpri)
  119. #endif
  120. #ifdef XT_USE_OVLY
  121. /* Storage for overlay state */
  122. STRUCT_FIELD (long, 4, XT_STK_OVLY, ovly)
  123. #endif
  124. STRUCT_END(XtExcFrame)
  125. #if defined(_ASMLANGUAGE) || defined(__ASSEMBLER__)
  126. #define XT_STK_NEXT1 XtExcFrameSize
  127. #else
  128. #define XT_STK_NEXT1 sizeof(XtExcFrame)
  129. #endif
  130. /* Allocate extra storage if needed */
  131. #if XCHAL_EXTRA_SA_SIZE != 0
  132. #if XCHAL_EXTRA_SA_ALIGN <= 16
  133. #define XT_STK_EXTRA ALIGNUP(XCHAL_EXTRA_SA_ALIGN, XT_STK_NEXT1)
  134. #else
  135. /* If need more alignment than stack, add space for dynamic alignment */
  136. #define XT_STK_EXTRA (ALIGNUP(XCHAL_EXTRA_SA_ALIGN, XT_STK_NEXT1) + XCHAL_EXTRA_SA_ALIGN)
  137. #endif
  138. #define XT_STK_NEXT2 (XT_STK_EXTRA + XCHAL_EXTRA_SA_SIZE)
  139. #else
  140. #define XT_STK_NEXT2 XT_STK_NEXT1
  141. #endif
  142. /*
  143. -------------------------------------------------------------------------------
  144. This is the frame size. Add space for 4 registers (interruptee's base save
  145. area) and some space for gcc nested functions if any.
  146. -------------------------------------------------------------------------------
  147. */
  148. #define XT_STK_FRMSZ (ALIGNUP(0x10, XT_STK_NEXT2) + 0x20)
  149. /*
  150. -------------------------------------------------------------------------------
  151. SOLICITED STACK FRAME FOR A THREAD
  152. A stack frame of this structure is allocated whenever a thread enters the
  153. RTOS kernel intentionally (and synchronously) to submit to thread scheduling.
  154. It goes on the current thread's stack.
  155. The solicited frame only includes registers that are required to be preserved
  156. by the callee according to the compiler's ABI conventions, some space to save
  157. the return address for returning to the caller, and the caller's PS register.
  158. For Windowed ABI, this stack frame includes the caller's base save area.
  159. Note on XT_SOL_EXIT field:
  160. It is necessary to distinguish a solicited from an interrupt stack frame.
  161. This field corresponds to XT_STK_EXIT in the interrupt stack frame and is
  162. always at the same offset (0). It can be written with a code (usually 0)
  163. to distinguish a solicted frame from an interrupt frame. An RTOS port may
  164. opt to ignore this field if it has another way of distinguishing frames.
  165. -------------------------------------------------------------------------------
  166. */
  167. STRUCT_BEGIN
  168. #ifdef __XTENSA_CALL0_ABI__
  169. STRUCT_FIELD (long, 4, XT_SOL_EXIT, exit)
  170. STRUCT_FIELD (long, 4, XT_SOL_PC, pc)
  171. STRUCT_FIELD (long, 4, XT_SOL_PS, ps)
  172. STRUCT_FIELD (long, 4, XT_SOL_NEXT, next)
  173. STRUCT_FIELD (long, 4, XT_SOL_A12, a12) /* should be on 16-byte alignment */
  174. STRUCT_FIELD (long, 4, XT_SOL_A13, a13)
  175. STRUCT_FIELD (long, 4, XT_SOL_A14, a14)
  176. STRUCT_FIELD (long, 4, XT_SOL_A15, a15)
  177. #else
  178. STRUCT_FIELD (long, 4, XT_SOL_EXIT, exit)
  179. STRUCT_FIELD (long, 4, XT_SOL_PC, pc)
  180. STRUCT_FIELD (long, 4, XT_SOL_PS, ps)
  181. STRUCT_FIELD (long, 4, XT_SOL_NEXT, next)
  182. STRUCT_FIELD (long, 4, XT_SOL_A0, a0) /* should be on 16-byte alignment */
  183. STRUCT_FIELD (long, 4, XT_SOL_A1, a1)
  184. STRUCT_FIELD (long, 4, XT_SOL_A2, a2)
  185. STRUCT_FIELD (long, 4, XT_SOL_A3, a3)
  186. #endif
  187. STRUCT_END(XtSolFrame)
  188. /* Size of solicited stack frame */
  189. #define XT_SOL_FRMSZ ALIGNUP(0x10, XtSolFrameSize)
  190. /*
  191. -------------------------------------------------------------------------------
  192. CO-PROCESSOR STATE SAVE AREA FOR A THREAD
  193. The RTOS must provide an area per thread to save the state of co-processors
  194. when that thread does not have control. Co-processors are context-switched
  195. lazily (on demand) only when a new thread uses a co-processor instruction,
  196. otherwise a thread retains ownership of the co-processor even when it loses
  197. control of the processor. An Xtensa co-processor exception is triggered when
  198. any co-processor instruction is executed by a thread that is not the owner,
  199. and the context switch of that co-processor is then peformed by the handler.
  200. Ownership represents which thread's state is currently in the co-processor.
  201. Co-processors may not be used by interrupt or exception handlers. If an
  202. co-processor instruction is executed by an interrupt or exception handler,
  203. the co-processor exception handler will trigger a kernel panic and freeze.
  204. This restriction is introduced to reduce the overhead of saving and restoring
  205. co-processor state (which can be quite large) and in particular remove that
  206. overhead from interrupt handlers.
  207. The co-processor state save area may be in any convenient per-thread location
  208. such as in the thread control block or above the thread stack area. It need
  209. not be in the interrupt stack frame since interrupts don't use co-processors.
  210. Along with the save area for each co-processor, two bitmasks with flags per
  211. co-processor (laid out as in the CPENABLE reg) help manage context-switching
  212. co-processors as efficiently as possible:
  213. XT_CPENABLE
  214. The contents of a non-running thread's CPENABLE register.
  215. It represents the co-processors owned (and whose state is still needed)
  216. by the thread. When a thread is preempted, its CPENABLE is saved here.
  217. When a thread solicits a context-swtich, its CPENABLE is cleared - the
  218. compiler has saved the (caller-saved) co-proc state if it needs to.
  219. When a non-running thread loses ownership of a CP, its bit is cleared.
  220. When a thread runs, it's XT_CPENABLE is loaded into the CPENABLE reg.
  221. Avoids co-processor exceptions when no change of ownership is needed.
  222. XT_CPSTORED
  223. A bitmask with the same layout as CPENABLE, a bit per co-processor.
  224. Indicates whether the state of each co-processor is saved in the state
  225. save area. When a thread enters the kernel, only the state of co-procs
  226. still enabled in CPENABLE is saved. When the co-processor exception
  227. handler assigns ownership of a co-processor to a thread, it restores
  228. the saved state only if this bit is set, and clears this bit.
  229. XT_CP_CS_ST
  230. A bitmask with the same layout as CPENABLE, a bit per co-processor.
  231. Indicates whether callee-saved state is saved in the state save area.
  232. Callee-saved state is saved by itself on a solicited context switch,
  233. and restored when needed by the coprocessor exception handler.
  234. Unsolicited switches will cause the entire coprocessor to be saved
  235. when necessary.
  236. XT_CP_ASA
  237. Pointer to the aligned save area. Allows it to be aligned more than
  238. the overall save area (which might only be stack-aligned or TCB-aligned).
  239. Especially relevant for Xtensa cores configured with a very large data
  240. path that requires alignment greater than 16 bytes (ABI stack alignment).
  241. -------------------------------------------------------------------------------
  242. */
  243. #if XCHAL_CP_NUM > 0
  244. /* Offsets of each coprocessor save area within the 'aligned save area': */
  245. #define XT_CP0_SA 0
  246. #define XT_CP1_SA ALIGNUP(XCHAL_CP1_SA_ALIGN, XT_CP0_SA + XCHAL_CP0_SA_SIZE)
  247. #define XT_CP2_SA ALIGNUP(XCHAL_CP2_SA_ALIGN, XT_CP1_SA + XCHAL_CP1_SA_SIZE)
  248. #define XT_CP3_SA ALIGNUP(XCHAL_CP3_SA_ALIGN, XT_CP2_SA + XCHAL_CP2_SA_SIZE)
  249. #define XT_CP4_SA ALIGNUP(XCHAL_CP4_SA_ALIGN, XT_CP3_SA + XCHAL_CP3_SA_SIZE)
  250. #define XT_CP5_SA ALIGNUP(XCHAL_CP5_SA_ALIGN, XT_CP4_SA + XCHAL_CP4_SA_SIZE)
  251. #define XT_CP6_SA ALIGNUP(XCHAL_CP6_SA_ALIGN, XT_CP5_SA + XCHAL_CP5_SA_SIZE)
  252. #define XT_CP7_SA ALIGNUP(XCHAL_CP7_SA_ALIGN, XT_CP6_SA + XCHAL_CP6_SA_SIZE)
  253. #define XT_CP_SA_SIZE ALIGNUP(16, XT_CP7_SA + XCHAL_CP7_SA_SIZE)
  254. /* Offsets within the overall save area: */
  255. #define XT_CPENABLE 0 /* (2 bytes) coprocessors active for this thread */
  256. #define XT_CPSTORED 2 /* (2 bytes) coprocessors saved for this thread */
  257. #define XT_CP_CS_ST 4 /* (2 bytes) coprocessor callee-saved regs stored for this thread */
  258. #define XT_CP_ASA 8 /* (4 bytes) ptr to aligned save area */
  259. /* Overall size allows for dynamic alignment: */
  260. #define XT_CP_SIZE (12 + XT_CP_SA_SIZE + XCHAL_TOTAL_SA_ALIGN)
  261. #else
  262. #define XT_CP_SIZE 0
  263. #endif
  264. /*
  265. -------------------------------------------------------------------------------
  266. MACROS TO HANDLE ABI SPECIFICS OF FUNCTION ENTRY AND RETURN
  267. Convenient where the frame size requirements are the same for both ABIs.
  268. ENTRY(sz), RET(sz) are for framed functions (have locals or make calls).
  269. ENTRY0, RET0 are for frameless functions (no locals, no calls).
  270. where size = size of stack frame in bytes (must be >0 and aligned to 16).
  271. For framed functions the frame is created and the return address saved at
  272. base of frame (Call0 ABI) or as determined by hardware (Windowed ABI).
  273. For frameless functions, there is no frame and return address remains in a0.
  274. Note: Because CPP macros expand to a single line, macros requiring multi-line
  275. expansions are implemented as assembler macros.
  276. -------------------------------------------------------------------------------
  277. */
  278. #ifdef __ASSEMBLER__
  279. #ifdef __XTENSA_CALL0_ABI__
  280. /* Call0 */
  281. #define ENTRY(sz) entry1 sz
  282. .macro entry1 size=0x10
  283. addi sp, sp, -\size
  284. s32i a0, sp, 0
  285. .endm
  286. #define ENTRY0
  287. #define RET(sz) ret1 sz
  288. .macro ret1 size=0x10
  289. l32i a0, sp, 0
  290. addi sp, sp, \size
  291. ret
  292. .endm
  293. #define RET0 ret
  294. #else
  295. /* Windowed */
  296. #define ENTRY(sz) entry sp, sz
  297. #define ENTRY0 entry sp, 0x10
  298. #define RET(sz) retw
  299. #define RET0 retw
  300. #endif
  301. #endif
  302. #endif /* XTENSA_CONTEXT_H */