secure_context.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-License-Identifier: MIT
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. /* Secure context includes. */
  29. #include "secure_context.h"
  30. /* Secure heap includes. */
  31. #include "secure_heap.h"
  32. /* Secure port macros. */
  33. #include "secure_port_macros.h"
  34. /**
  35. * @brief CONTROL value for privileged tasks.
  36. *
  37. * Bit[0] - 0 --> Thread mode is privileged.
  38. * Bit[1] - 1 --> Thread mode uses PSP.
  39. */
  40. #define securecontextCONTROL_VALUE_PRIVILEGED 0x02
  41. /**
  42. * @brief CONTROL value for un-privileged tasks.
  43. *
  44. * Bit[0] - 1 --> Thread mode is un-privileged.
  45. * Bit[1] - 1 --> Thread mode uses PSP.
  46. */
  47. #define securecontextCONTROL_VALUE_UNPRIVILEGED 0x03
  48. /**
  49. * @brief Size of stack seal values in bytes.
  50. */
  51. #define securecontextSTACK_SEAL_SIZE 8
  52. /**
  53. * @brief Stack seal value as recommended by ARM.
  54. */
  55. #define securecontextSTACK_SEAL_VALUE 0xFEF5EDA5
  56. /**
  57. * @brief Maximum number of secure contexts.
  58. */
  59. #ifndef secureconfigMAX_SECURE_CONTEXTS
  60. #define secureconfigMAX_SECURE_CONTEXTS 8UL
  61. #endif
  62. /*-----------------------------------------------------------*/
  63. /**
  64. * @brief Pre-allocated array of secure contexts.
  65. */
  66. SecureContext_t xSecureContexts[ secureconfigMAX_SECURE_CONTEXTS ];
  67. /*-----------------------------------------------------------*/
  68. /**
  69. * @brief Get a free secure context for a task from the secure context pool (xSecureContexts).
  70. *
  71. * This function ensures that only one secure context is allocated for a task.
  72. *
  73. * @param[in] pvTaskHandle The task handle for which the secure context is allocated.
  74. *
  75. * @return Index of a free secure context in the xSecureContexts array.
  76. */
  77. static uint32_t ulGetSecureContext( void * pvTaskHandle );
  78. /**
  79. * @brief Return the secure context to the secure context pool (xSecureContexts).
  80. *
  81. * @param[in] ulSecureContextIndex Index of the context in the xSecureContexts array.
  82. */
  83. static void vReturnSecureContext( uint32_t ulSecureContextIndex );
  84. /* These are implemented in assembly. */
  85. extern void SecureContext_LoadContextAsm( SecureContext_t * pxSecureContext );
  86. extern void SecureContext_SaveContextAsm( SecureContext_t * pxSecureContext );
  87. /*-----------------------------------------------------------*/
  88. static uint32_t ulGetSecureContext( void * pvTaskHandle )
  89. {
  90. /* Start with invalid index. */
  91. uint32_t i, ulSecureContextIndex = secureconfigMAX_SECURE_CONTEXTS;
  92. for( i = 0; i < secureconfigMAX_SECURE_CONTEXTS; i++ )
  93. {
  94. if( ( xSecureContexts[ i ].pucCurrentStackPointer == NULL ) &&
  95. ( xSecureContexts[ i ].pucStackLimit == NULL ) &&
  96. ( xSecureContexts[ i ].pucStackStart == NULL ) &&
  97. ( xSecureContexts[ i ].pvTaskHandle == NULL ) &&
  98. ( ulSecureContextIndex == secureconfigMAX_SECURE_CONTEXTS ) )
  99. {
  100. ulSecureContextIndex = i;
  101. }
  102. else if( xSecureContexts[ i ].pvTaskHandle == pvTaskHandle )
  103. {
  104. /* A task can only have one secure context. Do not allocate a second
  105. * context for the same task. */
  106. ulSecureContextIndex = secureconfigMAX_SECURE_CONTEXTS;
  107. break;
  108. }
  109. }
  110. return ulSecureContextIndex;
  111. }
  112. /*-----------------------------------------------------------*/
  113. static void vReturnSecureContext( uint32_t ulSecureContextIndex )
  114. {
  115. xSecureContexts[ ulSecureContextIndex ].pucCurrentStackPointer = NULL;
  116. xSecureContexts[ ulSecureContextIndex ].pucStackLimit = NULL;
  117. xSecureContexts[ ulSecureContextIndex ].pucStackStart = NULL;
  118. xSecureContexts[ ulSecureContextIndex ].pvTaskHandle = NULL;
  119. }
  120. /*-----------------------------------------------------------*/
  121. secureportNON_SECURE_CALLABLE void SecureContext_Init( void )
  122. {
  123. uint32_t ulIPSR, i;
  124. static uint32_t ulSecureContextsInitialized = 0;
  125. /* Read the Interrupt Program Status Register (IPSR) value. */
  126. secureportREAD_IPSR( ulIPSR );
  127. /* Do nothing if the processor is running in the Thread Mode. IPSR is zero
  128. * when the processor is running in the Thread Mode. */
  129. if( ( ulIPSR != 0 ) && ( ulSecureContextsInitialized == 0 ) )
  130. {
  131. /* Ensure to initialize secure contexts only once. */
  132. ulSecureContextsInitialized = 1;
  133. /* No stack for thread mode until a task's context is loaded. */
  134. secureportSET_PSPLIM( securecontextNO_STACK );
  135. secureportSET_PSP( securecontextNO_STACK );
  136. /* Initialize all secure contexts. */
  137. for( i = 0; i < secureconfigMAX_SECURE_CONTEXTS; i++ )
  138. {
  139. xSecureContexts[ i ].pucCurrentStackPointer = NULL;
  140. xSecureContexts[ i ].pucStackLimit = NULL;
  141. xSecureContexts[ i ].pucStackStart = NULL;
  142. xSecureContexts[ i ].pvTaskHandle = NULL;
  143. }
  144. #if ( configENABLE_MPU == 1 )
  145. {
  146. /* Configure thread mode to use PSP and to be unprivileged. */
  147. secureportSET_CONTROL( securecontextCONTROL_VALUE_UNPRIVILEGED );
  148. }
  149. #else /* configENABLE_MPU */
  150. {
  151. /* Configure thread mode to use PSP and to be privileged. */
  152. secureportSET_CONTROL( securecontextCONTROL_VALUE_PRIVILEGED );
  153. }
  154. #endif /* configENABLE_MPU */
  155. }
  156. }
  157. /*-----------------------------------------------------------*/
  158. #if ( configENABLE_MPU == 1 )
  159. secureportNON_SECURE_CALLABLE SecureContextHandle_t SecureContext_AllocateContext( uint32_t ulSecureStackSize,
  160. uint32_t ulIsTaskPrivileged,
  161. void * pvTaskHandle )
  162. #else /* configENABLE_MPU */
  163. secureportNON_SECURE_CALLABLE SecureContextHandle_t SecureContext_AllocateContext( uint32_t ulSecureStackSize,
  164. void * pvTaskHandle )
  165. #endif /* configENABLE_MPU */
  166. {
  167. uint8_t * pucStackMemory = NULL;
  168. uint8_t * pucStackLimit;
  169. uint32_t ulIPSR, ulSecureContextIndex;
  170. SecureContextHandle_t xSecureContextHandle = securecontextINVALID_CONTEXT_ID;
  171. #if ( configENABLE_MPU == 1 )
  172. uint32_t * pulCurrentStackPointer = NULL;
  173. #endif /* configENABLE_MPU */
  174. /* Read the Interrupt Program Status Register (IPSR) and Process Stack Limit
  175. * Register (PSPLIM) value. */
  176. secureportREAD_IPSR( ulIPSR );
  177. secureportREAD_PSPLIM( pucStackLimit );
  178. /* Do nothing if the processor is running in the Thread Mode. IPSR is zero
  179. * when the processor is running in the Thread Mode.
  180. * Also do nothing, if a secure context us already loaded. PSPLIM is set to
  181. * securecontextNO_STACK when no secure context is loaded. */
  182. if( ( ulIPSR != 0 ) && ( pucStackLimit == securecontextNO_STACK ) )
  183. {
  184. /* Ontain a free secure context. */
  185. ulSecureContextIndex = ulGetSecureContext( pvTaskHandle );
  186. /* Were we able to get a free context? */
  187. if( ulSecureContextIndex < secureconfigMAX_SECURE_CONTEXTS )
  188. {
  189. /* Allocate the stack space. */
  190. pucStackMemory = pvPortMalloc( ulSecureStackSize + securecontextSTACK_SEAL_SIZE );
  191. if( pucStackMemory != NULL )
  192. {
  193. /* Since stack grows down, the starting point will be the last
  194. * location. Note that this location is next to the last
  195. * allocated byte for stack (excluding the space for seal values)
  196. * because the hardware decrements the stack pointer before
  197. * writing i.e. if stack pointer is 0x2, a push operation will
  198. * decrement the stack pointer to 0x1 and then write at 0x1. */
  199. xSecureContexts[ ulSecureContextIndex ].pucStackStart = pucStackMemory + ulSecureStackSize;
  200. /* Seal the created secure process stack. */
  201. *( uint32_t * )( pucStackMemory + ulSecureStackSize ) = securecontextSTACK_SEAL_VALUE;
  202. *( uint32_t * )( pucStackMemory + ulSecureStackSize + 4 ) = securecontextSTACK_SEAL_VALUE;
  203. /* The stack cannot go beyond this location. This value is
  204. * programmed in the PSPLIM register on context switch.*/
  205. xSecureContexts[ ulSecureContextIndex ].pucStackLimit = pucStackMemory;
  206. xSecureContexts[ ulSecureContextIndex ].pvTaskHandle = pvTaskHandle;
  207. #if ( configENABLE_MPU == 1 )
  208. {
  209. /* Store the correct CONTROL value for the task on the stack.
  210. * This value is programmed in the CONTROL register on
  211. * context switch. */
  212. pulCurrentStackPointer = ( uint32_t * ) xSecureContexts[ ulSecureContextIndex ].pucStackStart;
  213. pulCurrentStackPointer--;
  214. if( ulIsTaskPrivileged )
  215. {
  216. *( pulCurrentStackPointer ) = securecontextCONTROL_VALUE_PRIVILEGED;
  217. }
  218. else
  219. {
  220. *( pulCurrentStackPointer ) = securecontextCONTROL_VALUE_UNPRIVILEGED;
  221. }
  222. /* Store the current stack pointer. This value is programmed in
  223. * the PSP register on context switch. */
  224. xSecureContexts[ ulSecureContextIndex ].pucCurrentStackPointer = ( uint8_t * ) pulCurrentStackPointer;
  225. }
  226. #else /* configENABLE_MPU */
  227. {
  228. /* Current SP is set to the starting of the stack. This
  229. * value programmed in the PSP register on context switch. */
  230. xSecureContexts[ ulSecureContextIndex ].pucCurrentStackPointer = xSecureContexts[ ulSecureContextIndex ].pucStackStart;
  231. }
  232. #endif /* configENABLE_MPU */
  233. /* Ensure to never return 0 as a valid context handle. */
  234. xSecureContextHandle = ulSecureContextIndex + 1UL;
  235. }
  236. }
  237. }
  238. return xSecureContextHandle;
  239. }
  240. /*-----------------------------------------------------------*/
  241. secureportNON_SECURE_CALLABLE void SecureContext_FreeContext( SecureContextHandle_t xSecureContextHandle, void * pvTaskHandle )
  242. {
  243. uint32_t ulIPSR, ulSecureContextIndex;
  244. /* Read the Interrupt Program Status Register (IPSR) value. */
  245. secureportREAD_IPSR( ulIPSR );
  246. /* Do nothing if the processor is running in the Thread Mode. IPSR is zero
  247. * when the processor is running in the Thread Mode. */
  248. if( ulIPSR != 0 )
  249. {
  250. /* Only free if a valid context handle is passed. */
  251. if( ( xSecureContextHandle > 0UL ) && ( xSecureContextHandle <= secureconfigMAX_SECURE_CONTEXTS ) )
  252. {
  253. ulSecureContextIndex = xSecureContextHandle - 1UL;
  254. /* Ensure that the secure context being deleted is associated with
  255. * the task. */
  256. if( xSecureContexts[ ulSecureContextIndex ].pvTaskHandle == pvTaskHandle )
  257. {
  258. /* Free the stack space. */
  259. vPortFree( xSecureContexts[ ulSecureContextIndex ].pucStackLimit );
  260. /* Return the secure context back to the free secure contexts pool. */
  261. vReturnSecureContext( ulSecureContextIndex );
  262. }
  263. }
  264. }
  265. }
  266. /*-----------------------------------------------------------*/
  267. secureportNON_SECURE_CALLABLE void SecureContext_LoadContext( SecureContextHandle_t xSecureContextHandle, void * pvTaskHandle )
  268. {
  269. uint8_t * pucStackLimit;
  270. uint32_t ulSecureContextIndex;
  271. if( ( xSecureContextHandle > 0UL ) && ( xSecureContextHandle <= secureconfigMAX_SECURE_CONTEXTS ) )
  272. {
  273. ulSecureContextIndex = xSecureContextHandle - 1UL;
  274. secureportREAD_PSPLIM( pucStackLimit );
  275. /* Ensure that no secure context is loaded and the task is loading it's
  276. * own context. */
  277. if( ( pucStackLimit == securecontextNO_STACK ) &&
  278. ( xSecureContexts[ ulSecureContextIndex ].pvTaskHandle == pvTaskHandle ) )
  279. {
  280. SecureContext_LoadContextAsm( &( xSecureContexts[ ulSecureContextIndex ] ) );
  281. }
  282. }
  283. }
  284. /*-----------------------------------------------------------*/
  285. secureportNON_SECURE_CALLABLE void SecureContext_SaveContext( SecureContextHandle_t xSecureContextHandle, void * pvTaskHandle )
  286. {
  287. uint8_t * pucStackLimit;
  288. uint32_t ulSecureContextIndex;
  289. if( ( xSecureContextHandle > 0UL ) && ( xSecureContextHandle <= secureconfigMAX_SECURE_CONTEXTS ) )
  290. {
  291. ulSecureContextIndex = xSecureContextHandle - 1UL;
  292. secureportREAD_PSPLIM( pucStackLimit );
  293. /* Ensure that task's context is loaded and the task is saving it's own
  294. * context. */
  295. if( ( xSecureContexts[ ulSecureContextIndex ].pucStackLimit == pucStackLimit ) &&
  296. ( xSecureContexts[ ulSecureContextIndex ].pvTaskHandle == pvTaskHandle ) )
  297. {
  298. SecureContext_SaveContextAsm( &( xSecureContexts[ ulSecureContextIndex ] ) );
  299. }
  300. }
  301. }
  302. /*-----------------------------------------------------------*/