1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup RTEMSScoreStack |
---|
7 | * |
---|
8 | * @brief This header file provides interfaces of the |
---|
9 | * @ref RTEMSScoreStack which are only used by the implementation. |
---|
10 | */ |
---|
11 | |
---|
12 | /* |
---|
13 | * COPYRIGHT (c) 1989-2006. |
---|
14 | * On-Line Applications Research Corporation (OAR). |
---|
15 | * |
---|
16 | * Redistribution and use in source and binary forms, with or without |
---|
17 | * modification, are permitted provided that the following conditions |
---|
18 | * are met: |
---|
19 | * 1. Redistributions of source code must retain the above copyright |
---|
20 | * notice, this list of conditions and the following disclaimer. |
---|
21 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
22 | * notice, this list of conditions and the following disclaimer in the |
---|
23 | * documentation and/or other materials provided with the distribution. |
---|
24 | * |
---|
25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
29 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
30 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
31 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
33 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
34 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
35 | * POSSIBILITY OF SUCH DAMAGE. |
---|
36 | */ |
---|
37 | |
---|
38 | #ifndef _RTEMS_SCORE_STACKIMPL_H |
---|
39 | #define _RTEMS_SCORE_STACKIMPL_H |
---|
40 | |
---|
41 | #include <rtems/score/stack.h> |
---|
42 | #include <rtems/score/context.h> |
---|
43 | #include <rtems/score/tls.h> |
---|
44 | |
---|
45 | #ifdef __cplusplus |
---|
46 | extern "C" { |
---|
47 | #endif |
---|
48 | |
---|
49 | /** |
---|
50 | * @addtogroup RTEMSScoreStack |
---|
51 | * |
---|
52 | * @{ |
---|
53 | */ |
---|
54 | |
---|
55 | /** |
---|
56 | * @brief Initializes stack with the given starting address and size. |
---|
57 | * |
---|
58 | * This routine initializes the_stack record to indicate that |
---|
59 | * size bytes of memory starting at starting_address have been |
---|
60 | * reserved for a stack. |
---|
61 | * |
---|
62 | * @param[out] the_stack The stack to initialize. |
---|
63 | * @param starting_address The starting_address for the new stack. |
---|
64 | * @param size The size of the stack in bytes. |
---|
65 | */ |
---|
66 | static inline void _Stack_Initialize ( |
---|
67 | Stack_Control *the_stack, |
---|
68 | void *starting_address, |
---|
69 | size_t size |
---|
70 | ) |
---|
71 | { |
---|
72 | the_stack->area = starting_address; |
---|
73 | the_stack->size = size; |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * @brief Returns the minimum stack size. |
---|
78 | * |
---|
79 | * This function returns the minimum stack size configured |
---|
80 | * for this application. |
---|
81 | * |
---|
82 | * @return The minimum stack size. |
---|
83 | */ |
---|
84 | static inline uint32_t _Stack_Minimum (void) |
---|
85 | { |
---|
86 | return rtems_minimum_stack_size; |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * @brief Checks if the size is enough for a valid stack area on this processor. |
---|
91 | * |
---|
92 | * This function returns true if size bytes is enough memory for |
---|
93 | * a valid stack area on this processor, and false otherwise. |
---|
94 | * |
---|
95 | * @param size The stack size to check. |
---|
96 | * @param is_fp Indicates if the stack is for a floating-point thread. |
---|
97 | * |
---|
98 | * @retval true @a size is large enough. |
---|
99 | * @retval false @a size is not large enough. |
---|
100 | */ |
---|
101 | static inline bool _Stack_Is_enough( |
---|
102 | size_t size, |
---|
103 | bool is_fp |
---|
104 | ) |
---|
105 | { |
---|
106 | size_t minimum; |
---|
107 | |
---|
108 | minimum = _TLS_Get_allocation_size(); |
---|
109 | minimum += _Stack_Minimum(); |
---|
110 | |
---|
111 | #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) |
---|
112 | if ( is_fp ) { |
---|
113 | minimum += CONTEXT_FP_SIZE; |
---|
114 | } |
---|
115 | #endif |
---|
116 | |
---|
117 | return ( size >= minimum ); |
---|
118 | } |
---|
119 | |
---|
120 | /** |
---|
121 | * @brief Returns the appropriate stack size for the requested size. |
---|
122 | * |
---|
123 | * This function returns the appropriate stack size given the requested |
---|
124 | * size. If the requested size is below the minimum, then the minimum |
---|
125 | * configured stack size is returned. |
---|
126 | * |
---|
127 | * @param size The stack size to check. |
---|
128 | * |
---|
129 | * @return The appropriate stack size. |
---|
130 | */ |
---|
131 | static inline size_t _Stack_Ensure_minimum ( |
---|
132 | size_t size |
---|
133 | ) |
---|
134 | { |
---|
135 | if ( size >= _Stack_Minimum() ) |
---|
136 | return size; |
---|
137 | return _Stack_Minimum(); |
---|
138 | } |
---|
139 | |
---|
140 | /** |
---|
141 | * @brief Extends the stack size to account for additional data structures |
---|
142 | * allocated in the thread storage area. |
---|
143 | * |
---|
144 | * @param stack_size is the stack size. |
---|
145 | * |
---|
146 | * @param is_fp shall be true, if the stack is for a floating-point thread, |
---|
147 | * otherwise it shall be false. |
---|
148 | * |
---|
149 | * @return Returns the extended stack size. |
---|
150 | */ |
---|
151 | static inline size_t _Stack_Extend_size( |
---|
152 | size_t stack_size, |
---|
153 | bool is_fp |
---|
154 | ) |
---|
155 | { |
---|
156 | size_t extra_size; |
---|
157 | size_t allocator_overhead; |
---|
158 | |
---|
159 | extra_size = _TLS_Get_allocation_size(); |
---|
160 | |
---|
161 | #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE ) |
---|
162 | if ( is_fp ) { |
---|
163 | /* This addition cannot overflow since the TLS size cannot be that large */ |
---|
164 | extra_size += CONTEXT_FP_SIZE; |
---|
165 | } |
---|
166 | #else |
---|
167 | (void) is_fp; |
---|
168 | #endif |
---|
169 | |
---|
170 | /* |
---|
171 | * In order to make sure that a user-provided stack size is the minimum which |
---|
172 | * can be allocated for the stack, we have to align it up to the next stack |
---|
173 | * boundary. |
---|
174 | */ |
---|
175 | extra_size += CPU_STACK_ALIGNMENT - 1; |
---|
176 | |
---|
177 | /* |
---|
178 | * If the heap allocator does not meet the stack alignment requirement, then |
---|
179 | * we have to do the stack alignment manually in _Thread_Initialize() and |
---|
180 | * need to allocate extra space for this. |
---|
181 | */ |
---|
182 | allocator_overhead = CPU_STACK_ALIGNMENT - CPU_HEAP_ALIGNMENT; |
---|
183 | |
---|
184 | if ( stack_size > SIZE_MAX - extra_size - allocator_overhead ) { |
---|
185 | /* |
---|
186 | * In case of an unsigned integer overflow, saturate at the maximum value. |
---|
187 | */ |
---|
188 | return SIZE_MAX; |
---|
189 | } |
---|
190 | |
---|
191 | stack_size += extra_size; |
---|
192 | stack_size = RTEMS_ALIGN_DOWN( stack_size, CPU_STACK_ALIGNMENT ); |
---|
193 | |
---|
194 | return stack_size + allocator_overhead; |
---|
195 | } |
---|
196 | |
---|
197 | /** |
---|
198 | * @brief Allocate the requested stack space. |
---|
199 | * |
---|
200 | * @param stack_size The stack space that is requested. |
---|
201 | * |
---|
202 | * @retval stack_area The allocated stack area. |
---|
203 | * @retval NULL The allocation failed. |
---|
204 | */ |
---|
205 | void *_Stack_Allocate( size_t stack_size ); |
---|
206 | |
---|
207 | /** |
---|
208 | * @brief Free the stack area allocated by _Stack_Allocate(). |
---|
209 | * |
---|
210 | * Do nothing if the stack area is NULL. |
---|
211 | * |
---|
212 | * @param stack_area The stack area to free, or NULL. |
---|
213 | */ |
---|
214 | void _Stack_Free( void *stack_area ); |
---|
215 | |
---|
216 | /** @} */ |
---|
217 | |
---|
218 | #ifdef __cplusplus |
---|
219 | } |
---|
220 | #endif |
---|
221 | |
---|
222 | #endif |
---|
223 | /* end of include file */ |
---|