2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
\r
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * https://www.FreeRTOS.org
\r
23 * https://github.com/FreeRTOS
\r
27 /* Secure context includes. */
\r
28 #include "secure_context.h"
\r
30 /* Secure heap includes. */
\r
31 #include "secure_heap.h"
\r
33 /* Secure port macros. */
\r
34 #include "secure_port_macros.h"
\r
37 * @brief CONTROL value for privileged tasks.
\r
39 * Bit[0] - 0 --> Thread mode is privileged.
\r
40 * Bit[1] - 1 --> Thread mode uses PSP.
\r
42 #define securecontextCONTROL_VALUE_PRIVILEGED 0x02
\r
45 * @brief CONTROL value for un-privileged tasks.
\r
47 * Bit[0] - 1 --> Thread mode is un-privileged.
\r
48 * Bit[1] - 1 --> Thread mode uses PSP.
\r
50 #define securecontextCONTROL_VALUE_UNPRIVILEGED 0x03
\r
51 /*-----------------------------------------------------------*/
\r
54 * @brief Structure to represent secure context.
\r
56 * @note Since stack grows down, pucStackStart is the highest address while
\r
57 * pucStackLimit is the first addess of the allocated memory.
\r
59 typedef struct SecureContext
\r
61 uint8_t * pucCurrentStackPointer; /**< Current value of stack pointer (PSP). */
\r
62 uint8_t * pucStackLimit; /**< Last location of the stack memory (PSPLIM). */
\r
63 uint8_t * pucStackStart; /**< First location of the stack memory. */
\r
65 /*-----------------------------------------------------------*/
\r
67 secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
\r
71 /* Read the Interrupt Program Status Register (IPSR) value. */
\r
72 secureportREAD_IPSR( ulIPSR );
\r
74 /* Do nothing if the processor is running in the Thread Mode. IPSR is zero
\r
75 * when the processor is running in the Thread Mode. */
\r
78 /* No stack for thread mode until a task's context is loaded. */
\r
79 secureportSET_PSPLIM( securecontextNO_STACK );
\r
80 secureportSET_PSP( securecontextNO_STACK );
\r
82 #if ( configENABLE_MPU == 1 )
\r
84 /* Configure thread mode to use PSP and to be unprivileged. */
\r
85 secureportSET_CONTROL( securecontextCONTROL_VALUE_UNPRIVILEGED );
\r
87 #else /* configENABLE_MPU */
\r
89 /* Configure thread mode to use PSP and to be privileged.. */
\r
90 secureportSET_CONTROL( securecontextCONTROL_VALUE_PRIVILEGED );
\r
92 #endif /* configENABLE_MPU */
\r
95 /*-----------------------------------------------------------*/
\r
97 #if ( configENABLE_MPU == 1 )
\r
98 secureportNON_SECURE_CALLABLE SecureContextHandle_t SecureContext_AllocateContext( uint32_t ulSecureStackSize,
\r
99 uint32_t ulIsTaskPrivileged )
\r
100 #else /* configENABLE_MPU */
\r
101 secureportNON_SECURE_CALLABLE SecureContextHandle_t SecureContext_AllocateContext( uint32_t ulSecureStackSize )
\r
102 #endif /* configENABLE_MPU */
\r
104 uint8_t * pucStackMemory = NULL;
\r
106 SecureContextHandle_t xSecureContextHandle = NULL;
\r
108 #if ( configENABLE_MPU == 1 )
\r
109 uint32_t * pulCurrentStackPointer = NULL;
\r
110 #endif /* configENABLE_MPU */
\r
112 /* Read the Interrupt Program Status Register (IPSR) value. */
\r
113 secureportREAD_IPSR( ulIPSR );
\r
115 /* Do nothing if the processor is running in the Thread Mode. IPSR is zero
\r
116 * when the processor is running in the Thread Mode. */
\r
119 /* Allocate the context structure. */
\r
120 xSecureContextHandle = ( SecureContextHandle_t ) pvPortMalloc( sizeof( SecureContext_t ) );
\r
122 if( xSecureContextHandle != NULL )
\r
124 /* Allocate the stack space. */
\r
125 pucStackMemory = pvPortMalloc( ulSecureStackSize );
\r
127 if( pucStackMemory != NULL )
\r
129 /* Since stack grows down, the starting point will be the last
\r
130 * location. Note that this location is next to the last
\r
131 * allocated byte because the hardware decrements the stack
\r
132 * pointer before writing i.e. if stack pointer is 0x2, a push
\r
133 * operation will decrement the stack pointer to 0x1 and then
\r
135 xSecureContextHandle->pucStackStart = pucStackMemory + ulSecureStackSize;
\r
137 /* The stack cannot go beyond this location. This value is
\r
138 * programmed in the PSPLIM register on context switch.*/
\r
139 xSecureContextHandle->pucStackLimit = pucStackMemory;
\r
141 #if ( configENABLE_MPU == 1 )
\r
143 /* Store the correct CONTROL value for the task on the stack.
\r
144 * This value is programmed in the CONTROL register on
\r
145 * context switch. */
\r
146 pulCurrentStackPointer = ( uint32_t * ) xSecureContextHandle->pucStackStart;
\r
147 pulCurrentStackPointer--;
\r
149 if( ulIsTaskPrivileged )
\r
151 *( pulCurrentStackPointer ) = securecontextCONTROL_VALUE_PRIVILEGED;
\r
155 *( pulCurrentStackPointer ) = securecontextCONTROL_VALUE_UNPRIVILEGED;
\r
158 /* Store the current stack pointer. This value is programmed in
\r
159 * the PSP register on context switch. */
\r
160 xSecureContextHandle->pucCurrentStackPointer = ( uint8_t * ) pulCurrentStackPointer;
\r
162 #else /* configENABLE_MPU */
\r
164 /* Current SP is set to the starting of the stack. This
\r
165 * value programmed in the PSP register on context switch. */
\r
166 xSecureContextHandle->pucCurrentStackPointer = xSecureContextHandle->pucStackStart;
\r
168 #endif /* configENABLE_MPU */
\r
172 /* Free the context to avoid memory leak and make sure to return
\r
173 * NULL to indicate failure. */
\r
174 vPortFree( xSecureContextHandle );
\r
175 xSecureContextHandle = NULL;
\r
180 return xSecureContextHandle;
\r
182 /*-----------------------------------------------------------*/
\r
184 secureportNON_SECURE_CALLABLE void SecureContext_FreeContext( SecureContextHandle_t xSecureContextHandle )
\r
188 /* Read the Interrupt Program Status Register (IPSR) value. */
\r
189 secureportREAD_IPSR( ulIPSR );
\r
191 /* Do nothing if the processor is running in the Thread Mode. IPSR is zero
\r
192 * when the processor is running in the Thread Mode. */
\r
195 /* Ensure that valid parameters are passed. */
\r
196 secureportASSERT( xSecureContextHandle != NULL );
\r
198 /* Free the stack space. */
\r
199 vPortFree( xSecureContextHandle->pucStackLimit );
\r
201 /* Free the context itself. */
\r
202 vPortFree( xSecureContextHandle );
\r
205 /*-----------------------------------------------------------*/
\r