2 * FreeRTOS Kernel V10.4.3
\r
3 * Copyright (C) 2020 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
25 * 1 tab == 4 spaces!
\r
28 /* Secure context includes. */
\r
29 #include "secure_context.h"
\r
31 /* Secure heap includes. */
\r
32 #include "secure_heap.h"
\r
34 /* Secure port macros. */
\r
35 #include "secure_port_macros.h"
\r
38 * @brief CONTROL value for privileged tasks.
\r
40 * Bit[0] - 0 --> Thread mode is privileged.
\r
41 * Bit[1] - 1 --> Thread mode uses PSP.
\r
43 #define securecontextCONTROL_VALUE_PRIVILEGED 0x02
\r
46 * @brief CONTROL value for un-privileged tasks.
\r
48 * Bit[0] - 1 --> Thread mode is un-privileged.
\r
49 * Bit[1] - 1 --> Thread mode uses PSP.
\r
51 #define securecontextCONTROL_VALUE_UNPRIVILEGED 0x03
\r
52 /*-----------------------------------------------------------*/
\r
55 * @brief Structure to represent secure context.
\r
57 * @note Since stack grows down, pucStackStart is the highest address while
\r
58 * pucStackLimit is the first addess of the allocated memory.
\r
60 typedef struct SecureContext
\r
62 uint8_t * pucCurrentStackPointer; /**< Current value of stack pointer (PSP). */
\r
63 uint8_t * pucStackLimit; /**< Last location of the stack memory (PSPLIM). */
\r
64 uint8_t * pucStackStart; /**< First location of the stack memory. */
\r
66 /*-----------------------------------------------------------*/
\r
68 secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
\r
72 /* Read the Interrupt Program Status Register (IPSR) value. */
\r
73 secureportREAD_IPSR( ulIPSR );
\r
75 /* Do nothing if the processor is running in the Thread Mode. IPSR is zero
\r
76 * when the processor is running in the Thread Mode. */
\r
79 /* No stack for thread mode until a task's context is loaded. */
\r
80 secureportSET_PSPLIM( securecontextNO_STACK );
\r
81 secureportSET_PSP( securecontextNO_STACK );
\r
83 #if ( configENABLE_MPU == 1 )
\r
85 /* Configure thread mode to use PSP and to be unprivileged. */
\r
86 secureportSET_CONTROL( securecontextCONTROL_VALUE_UNPRIVILEGED );
\r
88 #else /* configENABLE_MPU */
\r
90 /* Configure thread mode to use PSP and to be privileged.. */
\r
91 secureportSET_CONTROL( securecontextCONTROL_VALUE_PRIVILEGED );
\r
93 #endif /* configENABLE_MPU */
\r
96 /*-----------------------------------------------------------*/
\r
98 #if ( configENABLE_MPU == 1 )
\r
99 secureportNON_SECURE_CALLABLE SecureContextHandle_t SecureContext_AllocateContext( uint32_t ulSecureStackSize,
\r
100 uint32_t ulIsTaskPrivileged )
\r
101 #else /* configENABLE_MPU */
\r
102 secureportNON_SECURE_CALLABLE SecureContextHandle_t SecureContext_AllocateContext( uint32_t ulSecureStackSize )
\r
103 #endif /* configENABLE_MPU */
\r
105 uint8_t * pucStackMemory = NULL;
\r
107 SecureContextHandle_t xSecureContextHandle = NULL;
\r
109 #if ( configENABLE_MPU == 1 )
\r
110 uint32_t * pulCurrentStackPointer = NULL;
\r
111 #endif /* configENABLE_MPU */
\r
113 /* Read the Interrupt Program Status Register (IPSR) value. */
\r
114 secureportREAD_IPSR( ulIPSR );
\r
116 /* Do nothing if the processor is running in the Thread Mode. IPSR is zero
\r
117 * when the processor is running in the Thread Mode. */
\r
120 /* Allocate the context structure. */
\r
121 xSecureContextHandle = ( SecureContextHandle_t ) pvPortMalloc( sizeof( SecureContext_t ) );
\r
123 if( xSecureContextHandle != NULL )
\r
125 /* Allocate the stack space. */
\r
126 pucStackMemory = pvPortMalloc( ulSecureStackSize );
\r
128 if( pucStackMemory != NULL )
\r
130 /* Since stack grows down, the starting point will be the last
\r
131 * location. Note that this location is next to the last
\r
132 * allocated byte because the hardware decrements the stack
\r
133 * pointer before writing i.e. if stack pointer is 0x2, a push
\r
134 * operation will decrement the stack pointer to 0x1 and then
\r
136 xSecureContextHandle->pucStackStart = pucStackMemory + ulSecureStackSize;
\r
138 /* The stack cannot go beyond this location. This value is
\r
139 * programmed in the PSPLIM register on context switch.*/
\r
140 xSecureContextHandle->pucStackLimit = pucStackMemory;
\r
142 #if ( configENABLE_MPU == 1 )
\r
144 /* Store the correct CONTROL value for the task on the stack.
\r
145 * This value is programmed in the CONTROL register on
\r
146 * context switch. */
\r
147 pulCurrentStackPointer = ( uint32_t * ) xSecureContextHandle->pucStackStart;
\r
148 pulCurrentStackPointer--;
\r
150 if( ulIsTaskPrivileged )
\r
152 *( pulCurrentStackPointer ) = securecontextCONTROL_VALUE_PRIVILEGED;
\r
156 *( pulCurrentStackPointer ) = securecontextCONTROL_VALUE_UNPRIVILEGED;
\r
159 /* Store the current stack pointer. This value is programmed in
\r
160 * the PSP register on context switch. */
\r
161 xSecureContextHandle->pucCurrentStackPointer = ( uint8_t * ) pulCurrentStackPointer;
\r
163 #else /* configENABLE_MPU */
\r
165 /* Current SP is set to the starting of the stack. This
\r
166 * value programmed in the PSP register on context switch. */
\r
167 xSecureContextHandle->pucCurrentStackPointer = xSecureContextHandle->pucStackStart;
\r
169 #endif /* configENABLE_MPU */
\r
173 /* Free the context to avoid memory leak and make sure to return
\r
174 * NULL to indicate failure. */
\r
175 vPortFree( xSecureContextHandle );
\r
176 xSecureContextHandle = NULL;
\r
181 return xSecureContextHandle;
\r
183 /*-----------------------------------------------------------*/
\r
185 secureportNON_SECURE_CALLABLE void SecureContext_FreeContext( SecureContextHandle_t xSecureContextHandle )
\r
189 /* Read the Interrupt Program Status Register (IPSR) value. */
\r
190 secureportREAD_IPSR( ulIPSR );
\r
192 /* Do nothing if the processor is running in the Thread Mode. IPSR is zero
\r
193 * when the processor is running in the Thread Mode. */
\r
196 /* Ensure that valid parameters are passed. */
\r
197 secureportASSERT( xSecureContextHandle != NULL );
\r
199 /* Free the stack space. */
\r
200 vPortFree( xSecureContextHandle->pucStackLimit );
\r
202 /* Free the context itself. */
\r
203 vPortFree( xSecureContextHandle );
\r
206 /*-----------------------------------------------------------*/
\r