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