2 * FreeRTOS SMP Kernel V202110.00
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * https://www.FreeRTOS.org
23 * https://github.com/FreeRTOS
28 /* Secure context includes. */
29 #include "secure_context.h"
31 /* Secure heap includes. */
32 #include "secure_heap.h"
34 /* Secure port macros. */
35 #include "secure_port_macros.h"
38 * @brief CONTROL value for privileged tasks.
40 * Bit[0] - 0 --> Thread mode is privileged.
41 * Bit[1] - 1 --> Thread mode uses PSP.
43 #define securecontextCONTROL_VALUE_PRIVILEGED 0x02
46 * @brief CONTROL value for un-privileged tasks.
48 * Bit[0] - 1 --> Thread mode is un-privileged.
49 * Bit[1] - 1 --> Thread mode uses PSP.
51 #define securecontextCONTROL_VALUE_UNPRIVILEGED 0x03
52 /*-----------------------------------------------------------*/
55 * @brief Structure to represent secure context.
57 * @note Since stack grows down, pucStackStart is the highest address while
58 * pucStackLimit is the first addess of the allocated memory.
60 typedef struct SecureContext
62 uint8_t * pucCurrentStackPointer; /**< Current value of stack pointer (PSP). */
63 uint8_t * pucStackLimit; /**< Last location of the stack memory (PSPLIM). */
64 uint8_t * pucStackStart; /**< First location of the stack memory. */
66 /*-----------------------------------------------------------*/
68 secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
72 /* Read the Interrupt Program Status Register (IPSR) value. */
73 secureportREAD_IPSR( ulIPSR );
75 /* Do nothing if the processor is running in the Thread Mode. IPSR is zero
76 * when the processor is running in the Thread Mode. */
79 /* No stack for thread mode until a task's context is loaded. */
80 secureportSET_PSPLIM( securecontextNO_STACK );
81 secureportSET_PSP( securecontextNO_STACK );
83 #if ( configENABLE_MPU == 1 )
85 /* Configure thread mode to use PSP and to be unprivileged. */
86 secureportSET_CONTROL( securecontextCONTROL_VALUE_UNPRIVILEGED );
88 #else /* configENABLE_MPU */
90 /* Configure thread mode to use PSP and to be privileged.. */
91 secureportSET_CONTROL( securecontextCONTROL_VALUE_PRIVILEGED );
93 #endif /* configENABLE_MPU */
96 /*-----------------------------------------------------------*/
98 #if ( configENABLE_MPU == 1 )
99 secureportNON_SECURE_CALLABLE SecureContextHandle_t SecureContext_AllocateContext( uint32_t ulSecureStackSize,
100 uint32_t ulIsTaskPrivileged )
101 #else /* configENABLE_MPU */
102 secureportNON_SECURE_CALLABLE SecureContextHandle_t SecureContext_AllocateContext( uint32_t ulSecureStackSize )
103 #endif /* configENABLE_MPU */
105 uint8_t * pucStackMemory = NULL;
107 SecureContextHandle_t xSecureContextHandle = NULL;
109 #if ( configENABLE_MPU == 1 )
110 uint32_t * pulCurrentStackPointer = NULL;
111 #endif /* configENABLE_MPU */
113 /* Read the Interrupt Program Status Register (IPSR) value. */
114 secureportREAD_IPSR( ulIPSR );
116 /* Do nothing if the processor is running in the Thread Mode. IPSR is zero
117 * when the processor is running in the Thread Mode. */
120 /* Allocate the context structure. */
121 xSecureContextHandle = ( SecureContextHandle_t ) pvPortMalloc( sizeof( SecureContext_t ) );
123 if( xSecureContextHandle != NULL )
125 /* Allocate the stack space. */
126 pucStackMemory = pvPortMalloc( ulSecureStackSize );
128 if( pucStackMemory != NULL )
130 /* Since stack grows down, the starting point will be the last
131 * location. Note that this location is next to the last
132 * allocated byte because the hardware decrements the stack
133 * pointer before writing i.e. if stack pointer is 0x2, a push
134 * operation will decrement the stack pointer to 0x1 and then
136 xSecureContextHandle->pucStackStart = pucStackMemory + ulSecureStackSize;
138 /* The stack cannot go beyond this location. This value is
139 * programmed in the PSPLIM register on context switch.*/
140 xSecureContextHandle->pucStackLimit = pucStackMemory;
142 #if ( configENABLE_MPU == 1 )
144 /* Store the correct CONTROL value for the task on the stack.
145 * This value is programmed in the CONTROL register on
147 pulCurrentStackPointer = ( uint32_t * ) xSecureContextHandle->pucStackStart;
148 pulCurrentStackPointer--;
150 if( ulIsTaskPrivileged )
152 *( pulCurrentStackPointer ) = securecontextCONTROL_VALUE_PRIVILEGED;
156 *( pulCurrentStackPointer ) = securecontextCONTROL_VALUE_UNPRIVILEGED;
159 /* Store the current stack pointer. This value is programmed in
160 * the PSP register on context switch. */
161 xSecureContextHandle->pucCurrentStackPointer = ( uint8_t * ) pulCurrentStackPointer;
163 #else /* configENABLE_MPU */
165 /* Current SP is set to the starting of the stack. This
166 * value programmed in the PSP register on context switch. */
167 xSecureContextHandle->pucCurrentStackPointer = xSecureContextHandle->pucStackStart;
169 #endif /* configENABLE_MPU */
173 /* Free the context to avoid memory leak and make sure to return
174 * NULL to indicate failure. */
175 vPortFree( xSecureContextHandle );
176 xSecureContextHandle = NULL;
181 return xSecureContextHandle;
183 /*-----------------------------------------------------------*/
185 secureportNON_SECURE_CALLABLE void SecureContext_FreeContext( SecureContextHandle_t xSecureContextHandle )
189 /* Read the Interrupt Program Status Register (IPSR) value. */
190 secureportREAD_IPSR( ulIPSR );
192 /* Do nothing if the processor is running in the Thread Mode. IPSR is zero
193 * when the processor is running in the Thread Mode. */
196 /* Ensure that valid parameters are passed. */
197 secureportASSERT( xSecureContextHandle != NULL );
199 /* Free the stack space. */
200 vPortFree( xSecureContextHandle->pucStackLimit );
202 /* Free the context itself. */
203 vPortFree( xSecureContextHandle );
206 /*-----------------------------------------------------------*/