xtensa_context.h 17 KB

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