1  /*
2  * FreeRTOS Kernel V11.1.0
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  * Configuration-specific information for Xtensa build. This file must be
32  * included in FreeRTOSConfig.h to properly set up the config-dependent
33  * parameters correctly.
34  *
35  * NOTE: To enable thread-safe C library support, XT_USE_THREAD_SAFE_CLIB must
36  * be defined to be > 0 somewhere above or on the command line.
37  */
38 
39 #ifndef XTENSA_CONFIG_H
40 #define XTENSA_CONFIG_H
41 
42 /* *INDENT-OFF* */
43 #ifdef __cplusplus
44     extern "C" {
45 #endif
46 /* *INDENT-ON* */
47 
48 #include <xtensa/hal.h>
49 #include <xtensa/config/core.h>
50 #include <xtensa/config/system.h>   /* required for XSHAL_CLIB */
51 
52 #include "xtensa_context.h"
53 
54 
55 /*-----------------------------------------------------------------------------
56 *                                 STACK REQUIREMENTS
57 *
58 * This section defines the minimum stack size, and the extra space required to
59 * be allocated for saving coprocessor state and/or C library state information
60 * (if thread safety is enabled for the C library). The sizes are in bytes.
61 *
62 * Stack sizes for individual tasks should be derived from these minima based on
63 * the maximum call depth of the task and the maximum level of interrupt nesting.
64 * A minimum stack size is defined by XT_STACK_MIN_SIZE. This minimum is based
65 * on the requirement for a task that calls nothing else but can be interrupted.
66 * This assumes that interrupt handlers do not call more than a few levels deep.
67 * If this is not true, i.e. one or more interrupt handlers make deep calls then
68 * the minimum must be increased.
69 *
70 * If the Xtensa processor configuration includes coprocessors, then space is
71 * allocated to save the coprocessor state on the stack.
72 *
73 * If thread safety is enabled for the C runtime library, (XT_USE_THREAD_SAFE_CLIB
74 * is defined) then space is allocated to save the C library context in the TCB.
75 *
76 * Allocating insufficient stack space is a common source of hard-to-find errors.
77 * During development, it is best to enable the FreeRTOS stack checking features.
78 *
79 * Usage:
80 *
81 * XT_USE_THREAD_SAFE_CLIB -- Define this to a nonzero value to enable thread-safe
82 *                            use of the C library. This will require extra stack
83 *                            space to be allocated for tasks that use the C library
84 *                            reentrant functions. See below for more information.
85 *
86 * NOTE: The Xtensa toolchain supports multiple C libraries and not all of them
87 * support thread safety. Check your core configuration to see which C library
88 * was chosen for your system.
89 *
90 * XT_STACK_MIN_SIZE       -- The minimum stack size for any task. It is recommended
91 *                            that you do not use a stack smaller than this for any
92 *                            task. In case you want to use stacks smaller than this
93 *                            size, you must verify that the smaller size(s) will work
94 *                            under all operating conditions.
95 *
96 * XT_STACK_EXTRA          -- The amount of extra stack space to allocate for a task
97 *                            that does not make C library reentrant calls. Add this
98 *                            to the amount of stack space required by the task itself.
99 *
100 * XT_STACK_EXTRA_CLIB     -- The amount of space to allocate for C library state.
101 *
102 -----------------------------------------------------------------------------*/
103 
104 /* Extra space required for interrupt/exception hooks. */
105 #ifdef XT_INTEXC_HOOKS
106   #ifdef __XTENSA_CALL0_ABI__
107     #define STK_INTEXC_EXTRA        0x200
108   #else
109     #define STK_INTEXC_EXTRA        0x180
110   #endif
111 #else
112   #define STK_INTEXC_EXTRA          0
113 #endif
114 
115 /* Check C library thread safety support and compute size of C library save area.
116    For the supported libraries, we enable thread safety by default, and this can
117    be overridden from the compiler/make command line. */
118 #if (XSHAL_CLIB == XTHAL_CLIB_NEWLIB) || (XSHAL_CLIB == XTHAL_CLIB_XCLIB)
119   #ifndef XT_USE_THREAD_SAFE_CLIB
120     #define XT_USE_THREAD_SAFE_CLIB         1
121   #endif
122 #else
123   #define XT_USE_THREAD_SAFE_CLIB           0
124 #endif
125 
126 #if XT_USE_THREAD_SAFE_CLIB > 0u
127   #if XSHAL_CLIB == XTHAL_CLIB_XCLIB
128     #define XT_HAVE_THREAD_SAFE_CLIB        1
129     #if !defined __ASSEMBLER__
130       #include <sys/reent.h>
131       #define XT_CLIB_CONTEXT_AREA_SIZE     ((sizeof(struct _reent) + 15) + (-16))
132       #define XT_CLIB_GLOBAL_PTR            _reent_ptr
133       #define _REENT_INIT_PTR               _init_reent
134       #define _impure_ptr                   _reent_ptr
135 
136       void _reclaim_reent(void * ptr);
137     #endif
138   #elif XSHAL_CLIB == XTHAL_CLIB_NEWLIB
139     #define XT_HAVE_THREAD_SAFE_CLIB        1
140     #if !defined __ASSEMBLER__
141       #include <sys/reent.h>
142       #define XT_CLIB_CONTEXT_AREA_SIZE     ((sizeof(struct _reent) + 15) + (-16))
143       #define XT_CLIB_GLOBAL_PTR            _impure_ptr
144     #endif
145   #else
146     #define XT_HAVE_THREAD_SAFE_CLIB        0
147     #error The selected C runtime library is not thread safe.
148   #endif
149 #else
150   #define XT_CLIB_CONTEXT_AREA_SIZE         0
151 #endif
152 
153 /*------------------------------------------------------------------------------
154   Extra size -- interrupt frame plus coprocessor save area plus hook space.
155   NOTE: Make sure XT_INTEXC_HOOKS is undefined unless you really need the hooks.
156 ------------------------------------------------------------------------------*/
157 #ifdef __XTENSA_CALL0_ABI__
158   #define XT_XTRA_SIZE            (XT_STK_FRMSZ + STK_INTEXC_EXTRA + 0x10 + XT_CP_SIZE)
159 #else
160   #define XT_XTRA_SIZE            (XT_STK_FRMSZ + STK_INTEXC_EXTRA + 0x20 + XT_CP_SIZE)
161 #endif
162 
163 /*------------------------------------------------------------------------------
164   Space allocated for user code -- function calls and local variables.
165   NOTE: This number can be adjusted to suit your needs. You must verify that the
166   amount of space you reserve is adequate for the worst-case conditions in your
167   application.
168   NOTE: The windowed ABI requires more stack, since space has to be reserved
169   for spilling register windows.
170 ------------------------------------------------------------------------------*/
171 #ifdef __XTENSA_CALL0_ABI__
172   #define XT_USER_SIZE            0x200
173 #else
174   #define XT_USER_SIZE            0x400
175 #endif
176 
177 /* Minimum recommended stack size. */
178 #define XT_STACK_MIN_SIZE         ((XT_XTRA_SIZE + XT_USER_SIZE) / sizeof(unsigned char))
179 
180 /* OS overhead with and without C library thread context. */
181 #define XT_STACK_EXTRA              (XT_XTRA_SIZE)
182 #define XT_STACK_EXTRA_CLIB         (XT_XTRA_SIZE + XT_CLIB_CONTEXT_AREA_SIZE)
183 
184 
185 /* *INDENT-OFF* */
186 #ifdef __cplusplus
187     }
188 #endif
189 /* *INDENT-ON* */
190 
191 #endif /* XTENSA_CONFIG_H */
192