2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
\r
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * SPDX-License-Identifier: MIT
\r
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
8 * this software and associated documentation files (the "Software"), to deal in
\r
9 * the Software without restriction, including without limitation the rights to
\r
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
11 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
12 * subject to the following conditions:
\r
14 * The above copyright notice and this permission notice shall be included in all
\r
15 * copies or substantial portions of the Software.
\r
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
24 * https://www.FreeRTOS.org
\r
25 * https://github.com/FreeRTOS
\r
29 /* Secure context includes. */
\r
30 #include "secure_context.h"
\r
32 /* Secure heap includes. */
\r
33 #include "secure_heap.h"
\r
35 /* Secure port macros. */
\r
36 #include "secure_port_macros.h"
\r
39 * @brief CONTROL value for privileged tasks.
\r
41 * Bit[0] - 0 --> Thread mode is privileged.
\r
42 * Bit[1] - 1 --> Thread mode uses PSP.
\r
44 #define securecontextCONTROL_VALUE_PRIVILEGED 0x02
\r
47 * @brief CONTROL value for un-privileged tasks.
\r
49 * Bit[0] - 1 --> Thread mode is un-privileged.
\r
50 * Bit[1] - 1 --> Thread mode uses PSP.
\r
52 #define securecontextCONTROL_VALUE_UNPRIVILEGED 0x03
\r
55 * @brief Invalid context ID.
\r
57 #define securecontextINVALID_CONTEXT_ID 0UL
\r
60 * @brief Maximum number of secure contexts.
\r
62 #ifndef secureconfigMAX_SECURE_CONTEXTS
\r
63 #define secureconfigMAX_SECURE_CONTEXTS 8UL
\r
65 /*-----------------------------------------------------------*/
\r
68 * @brief Pre-allocated array of secure contexts.
\r
70 SecureContext_t xSecureContexts[ secureconfigMAX_SECURE_CONTEXTS ];
\r
71 /*-----------------------------------------------------------*/
\r
74 * @brief Get a free context from the secure context pool (xSecureContexts).
\r
76 * @return Index of a free context in the xSecureContexts array.
\r
78 static uint32_t ulGetSecureContext( void );
\r
81 * @brief Return the secure context to the secure context pool (xSecureContexts).
\r
83 * @param[in] ulSecureContextIndex Index of the context in the xSecureContexts array.
\r
85 static void vReturnSecureContext( uint32_t ulSecureContextIndex );
\r
87 /* These are implemented in assembly. */
\r
88 extern void SecureContext_LoadContextAsm( SecureContext_t * pxSecureContext );
\r
89 extern void SecureContext_SaveContextAsm( SecureContext_t * pxSecureContext );
\r
90 /*-----------------------------------------------------------*/
\r
92 static uint32_t ulGetSecureContext( void )
\r
94 uint32_t ulSecureContextIndex;
\r
96 for( ulSecureContextIndex = 0; ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS; ulSecureContextIndex++ )
\r
98 if( ( xSecureContexts[ ulSecureContextIndex ].pucCurrentStackPointer == NULL ) &&
\r
99 ( xSecureContexts[ ulSecureContextIndex ].pucStackLimit == NULL ) &&
\r
100 ( xSecureContexts[ ulSecureContextIndex ].pucStackStart == NULL ) )
\r
106 return ulSecureContextIndex;
\r
108 /*-----------------------------------------------------------*/
\r
110 static void vReturnSecureContext( uint32_t ulSecureContextIndex )
\r
112 xSecureContexts[ ulSecureContextIndex ].pucCurrentStackPointer = NULL;
\r
113 xSecureContexts[ ulSecureContextIndex ].pucStackLimit = NULL;
\r
114 xSecureContexts[ ulSecureContextIndex ].pucStackStart = NULL;
\r
116 /*-----------------------------------------------------------*/
\r
118 secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
\r
120 uint32_t ulIPSR, i;
\r
122 /* Read the Interrupt Program Status Register (IPSR) value. */
\r
123 secureportREAD_IPSR( ulIPSR );
\r
125 /* Do nothing if the processor is running in the Thread Mode. IPSR is zero
\r
126 * when the processor is running in the Thread Mode. */
\r
129 /* No stack for thread mode until a task's context is loaded. */
\r
130 secureportSET_PSPLIM( securecontextNO_STACK );
\r
131 secureportSET_PSP( securecontextNO_STACK );
\r
133 /* Initialize all secure contexts. */
\r
134 for( i = 0; i < secureconfigMAX_SECURE_CONTEXTS; i++ )
\r
136 xSecureContexts[ i ].pucCurrentStackPointer = NULL;
\r
137 xSecureContexts[ i ].pucStackLimit = NULL;
\r
138 xSecureContexts[ i ].pucStackStart = NULL;
\r
141 #if ( configENABLE_MPU == 1 )
\r
143 /* Configure thread mode to use PSP and to be unprivileged. */
\r
144 secureportSET_CONTROL( securecontextCONTROL_VALUE_UNPRIVILEGED );
\r
146 #else /* configENABLE_MPU */
\r
148 /* Configure thread mode to use PSP and to be privileged. */
\r
149 secureportSET_CONTROL( securecontextCONTROL_VALUE_PRIVILEGED );
\r
151 #endif /* configENABLE_MPU */
\r
154 /*-----------------------------------------------------------*/
\r
156 #if ( configENABLE_MPU == 1 )
\r
157 secureportNON_SECURE_CALLABLE SecureContextHandle_t SecureContext_AllocateContext( uint32_t ulSecureStackSize,
\r
158 uint32_t ulIsTaskPrivileged )
\r
159 #else /* configENABLE_MPU */
\r
160 secureportNON_SECURE_CALLABLE SecureContextHandle_t SecureContext_AllocateContext( uint32_t ulSecureStackSize )
\r
161 #endif /* configENABLE_MPU */
\r
163 uint8_t * pucStackMemory = NULL;
\r
164 uint32_t ulIPSR, ulSecureContextIndex;
\r
165 SecureContextHandle_t xSecureContextHandle;
\r
167 #if ( configENABLE_MPU == 1 )
\r
168 uint32_t * pulCurrentStackPointer = NULL;
\r
169 #endif /* configENABLE_MPU */
\r
171 /* Read the Interrupt Program Status Register (IPSR) value. */
\r
172 secureportREAD_IPSR( ulIPSR );
\r
174 /* Do nothing if the processor is running in the Thread Mode. IPSR is zero
\r
175 * when the processor is running in the Thread Mode. */
\r
178 /* Ontain a free secure context. */
\r
179 ulSecureContextIndex = ulGetSecureContext();
\r
181 /* Were we able to get a free context? */
\r
182 if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
\r
184 /* Allocate the stack space. */
\r
185 pucStackMemory = pvPortMalloc( ulSecureStackSize );
\r
187 if( pucStackMemory != NULL )
\r
189 /* Since stack grows down, the starting point will be the last
\r
190 * location. Note that this location is next to the last
\r
191 * allocated byte because the hardware decrements the stack
\r
192 * pointer before writing i.e. if stack pointer is 0x2, a push
\r
193 * operation will decrement the stack pointer to 0x1 and then
\r
195 xSecureContexts[ ulSecureContextIndex ].pucStackStart = pucStackMemory + ulSecureStackSize;
\r
197 /* The stack cannot go beyond this location. This value is
\r
198 * programmed in the PSPLIM register on context switch.*/
\r
199 xSecureContexts[ ulSecureContextIndex ].pucStackLimit = pucStackMemory;
\r
201 #if ( configENABLE_MPU == 1 )
\r
203 /* Store the correct CONTROL value for the task on the stack.
\r
204 * This value is programmed in the CONTROL register on
\r
205 * context switch. */
\r
206 pulCurrentStackPointer = ( uint32_t * ) xSecureContexts[ ulSecureContextIndex ].pucStackStart;
\r
207 pulCurrentStackPointer--;
\r
209 if( ulIsTaskPrivileged )
\r
211 *( pulCurrentStackPointer ) = securecontextCONTROL_VALUE_PRIVILEGED;
\r
215 *( pulCurrentStackPointer ) = securecontextCONTROL_VALUE_UNPRIVILEGED;
\r
218 /* Store the current stack pointer. This value is programmed in
\r
219 * the PSP register on context switch. */
\r
220 xSecureContexts[ ulSecureContextIndex ].pucCurrentStackPointer = ( uint8_t * ) pulCurrentStackPointer;
\r
222 #else /* configENABLE_MPU */
\r
224 /* Current SP is set to the starting of the stack. This
\r
225 * value programmed in the PSP register on context switch. */
\r
226 xSecureContexts[ ulSecureContextIndex ].pucCurrentStackPointer = xSecureContexts[ ulSecureContextIndex ].pucStackStart;
\r
228 #endif /* configENABLE_MPU */
\r
230 /* Ensure to never return 0 as a valid context handle. */
\r
231 xSecureContextHandle = ulSecureContextIndex + 1UL;
\r
235 xSecureContextHandle = securecontextINVALID_CONTEXT_ID;
\r
240 return xSecureContextHandle;
\r
242 /*-----------------------------------------------------------*/
\r
244 secureportNON_SECURE_CALLABLE void SecureContext_FreeContext( SecureContextHandle_t xSecureContextHandle )
\r
246 uint32_t ulIPSR, ulSecureContextIndex;
\r
248 /* Read the Interrupt Program Status Register (IPSR) value. */
\r
249 secureportREAD_IPSR( ulIPSR );
\r
251 /* Do nothing if the processor is running in the Thread Mode. IPSR is zero
\r
252 * when the processor is running in the Thread Mode. */
\r
255 /* Only free if a valid context handle is passed. */
\r
256 if( ( xSecureContextHandle > 0UL ) && ( xSecureContextHandle <= secureconfigMAX_SECURE_CONTEXTS ) )
\r
258 ulSecureContextIndex = xSecureContextHandle - 1UL;
\r
260 /* Free the stack space. */
\r
261 vPortFree( xSecureContexts[ ulSecureContextIndex ].pucStackLimit );
\r
263 /* Return the context back to the free contexts pool. */
\r
264 vReturnSecureContext( ulSecureContextIndex );
\r
268 /*-----------------------------------------------------------*/
\r
270 secureportNON_SECURE_CALLABLE void SecureContext_LoadContext( SecureContextHandle_t xSecureContextHandle )
\r
272 uint32_t ulSecureContextIndex;
\r
274 if( ( xSecureContextHandle > 0UL ) && ( xSecureContextHandle <= secureconfigMAX_SECURE_CONTEXTS ) )
\r
276 ulSecureContextIndex = xSecureContextHandle - 1UL;
\r
278 SecureContext_LoadContextAsm( &( xSecureContexts[ ulSecureContextIndex ] ) );
\r
281 /*-----------------------------------------------------------*/
\r
283 secureportNON_SECURE_CALLABLE void SecureContext_SaveContext( SecureContextHandle_t xSecureContextHandle )
\r
285 uint32_t ulSecureContextIndex;
\r
287 if( ( xSecureContextHandle > 0UL ) && ( xSecureContextHandle <= secureconfigMAX_SECURE_CONTEXTS ) )
\r
289 ulSecureContextIndex = xSecureContextHandle - 1UL;
\r
291 SecureContext_SaveContextAsm( &( xSecureContexts[ ulSecureContextIndex ] ) );
\r
294 /*-----------------------------------------------------------*/
\r