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