mbed_tz_context.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /******************************************************************************
  2. * @file tz_context.c
  3. * @brief Context Management for Armv8-M TrustZone - Sample implementation
  4. * @version V1.1.1
  5. * @date 10. January 2018
  6. ******************************************************************************/
  7. /*
  8. * Copyright (c) 2016-2018 Arm Limited. All rights reserved.
  9. *
  10. * SPDX-License-Identifier: Apache-2.0
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the License); you may
  13. * not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  20. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. *
  24. * ----------------------------------------------------------------------------
  25. *
  26. * $Date: 15. October 2016
  27. * $Revision: 1.1.0
  28. *
  29. * Project: TrustZone for ARMv8-M
  30. * Title: Context Management for ARMv8-M TrustZone - Sample implementation
  31. *
  32. *---------------------------------------------------------------------------*/
  33. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  34. #include "RTE_Components.h"
  35. #include CMSIS_device_header
  36. #include "tz_context.h"
  37. /// Number of process slots (threads may call secure library code)
  38. #ifndef TZ_PROCESS_STACK_SLOTS
  39. #define TZ_PROCESS_STACK_SLOTS 8U
  40. #endif
  41. /// Stack size of the secure library code
  42. #ifndef TZ_PROCESS_STACK_SIZE
  43. #define TZ_PROCESS_STACK_SIZE 256U
  44. #endif
  45. typedef struct {
  46. uint32_t sp_top; // stack space top
  47. uint32_t sp_limit; // stack space limit
  48. uint32_t sp; // current stack pointer
  49. } stack_info_t;
  50. static stack_info_t ProcessStackInfo [TZ_PROCESS_STACK_SLOTS];
  51. static uint64_t ProcessStackMemory[TZ_PROCESS_STACK_SLOTS][TZ_PROCESS_STACK_SIZE/8U];
  52. static uint32_t ProcessStackFreeSlot = 0xFFFFFFFFU;
  53. /// Initialize secure context memory system
  54. /// \return execution status (1: success, 0: error)
  55. __attribute__((cmse_nonsecure_entry))
  56. uint32_t TZ_InitContextSystem_S (void) {
  57. uint32_t n;
  58. if (__get_IPSR() == 0U) {
  59. return 0U; // Thread Mode
  60. }
  61. for (n = 0U; n < TZ_PROCESS_STACK_SLOTS; n++) {
  62. ProcessStackInfo[n].sp = 0U;
  63. ProcessStackInfo[n].sp_limit = (uint32_t)&ProcessStackMemory[n];
  64. ProcessStackInfo[n].sp_top = (uint32_t)&ProcessStackMemory[n] + TZ_PROCESS_STACK_SIZE;
  65. *((uint32_t *)ProcessStackMemory[n]) = n + 1U;
  66. }
  67. *((uint32_t *)ProcessStackMemory[--n]) = 0xFFFFFFFFU;
  68. ProcessStackFreeSlot = 0U;
  69. // Default process stack pointer and stack limit
  70. __set_PSPLIM((uint32_t)ProcessStackMemory);
  71. __set_PSP ((uint32_t)ProcessStackMemory);
  72. // Privileged Thread Mode using PSP
  73. __set_CONTROL(0x02U);
  74. return 1U; // Success
  75. }
  76. /// Allocate context memory for calling secure software modules in TrustZone
  77. /// \param[in] module identifies software modules called from non-secure mode
  78. /// \return value != 0 id TrustZone memory slot identifier
  79. /// \return value 0 no memory available or internal error
  80. __attribute__((cmse_nonsecure_entry))
  81. TZ_MemoryId_t TZ_AllocModuleContext_S (TZ_ModuleId_t module) {
  82. uint32_t slot;
  83. (void)module; // Ignore (fixed Stack size)
  84. if (__get_IPSR() == 0U) {
  85. return 0U; // Thread Mode
  86. }
  87. if (ProcessStackFreeSlot == 0xFFFFFFFFU) {
  88. return 0U; // No slot available
  89. }
  90. slot = ProcessStackFreeSlot;
  91. ProcessStackFreeSlot = *((uint32_t *)ProcessStackMemory[slot]);
  92. ProcessStackInfo[slot].sp = ProcessStackInfo[slot].sp_top;
  93. return (slot + 1U);
  94. }
  95. /// Free context memory that was previously allocated with \ref TZ_AllocModuleContext_S
  96. /// \param[in] id TrustZone memory slot identifier
  97. /// \return execution status (1: success, 0: error)
  98. __attribute__((cmse_nonsecure_entry))
  99. uint32_t TZ_FreeModuleContext_S (TZ_MemoryId_t id) {
  100. uint32_t slot;
  101. if (__get_IPSR() == 0U) {
  102. return 0U; // Thread Mode
  103. }
  104. if ((id == 0U) || (id > TZ_PROCESS_STACK_SLOTS)) {
  105. return 0U; // Invalid ID
  106. }
  107. slot = id - 1U;
  108. if (ProcessStackInfo[slot].sp == 0U) {
  109. return 0U; // Inactive slot
  110. }
  111. ProcessStackInfo[slot].sp = 0U;
  112. *((uint32_t *)ProcessStackMemory[slot]) = ProcessStackFreeSlot;
  113. ProcessStackFreeSlot = slot;
  114. return 1U; // Success
  115. }
  116. /// Load secure context (called on RTOS thread context switch)
  117. /// \param[in] id TrustZone memory slot identifier
  118. /// \return execution status (1: success, 0: error)
  119. __attribute__((cmse_nonsecure_entry))
  120. uint32_t TZ_LoadContext_S (TZ_MemoryId_t id) {
  121. uint32_t slot;
  122. if ((__get_IPSR() == 0U) || ((__get_CONTROL() & 2U) == 0U)) {
  123. return 0U; // Thread Mode or using Main Stack for threads
  124. }
  125. if ((id == 0U) || (id > TZ_PROCESS_STACK_SLOTS)) {
  126. return 0U; // Invalid ID
  127. }
  128. slot = id - 1U;
  129. if (ProcessStackInfo[slot].sp == 0U) {
  130. return 0U; // Inactive slot
  131. }
  132. // Setup process stack pointer and stack limit
  133. __set_PSPLIM(ProcessStackInfo[slot].sp_limit);
  134. __set_PSP (ProcessStackInfo[slot].sp);
  135. return 1U; // Success
  136. }
  137. /// Store secure context (called on RTOS thread context switch)
  138. /// \param[in] id TrustZone memory slot identifier
  139. /// \return execution status (1: success, 0: error)
  140. __attribute__((cmse_nonsecure_entry))
  141. uint32_t TZ_StoreContext_S (TZ_MemoryId_t id) {
  142. uint32_t slot;
  143. uint32_t sp;
  144. if ((__get_IPSR() == 0U) || ((__get_CONTROL() & 2U) == 0U)) {
  145. return 0U; // Thread Mode or using Main Stack for threads
  146. }
  147. if ((id == 0U) || (id > TZ_PROCESS_STACK_SLOTS)) {
  148. return 0U; // Invalid ID
  149. }
  150. slot = id - 1U;
  151. if (ProcessStackInfo[slot].sp == 0U) {
  152. return 0U; // Inactive slot
  153. }
  154. sp = __get_PSP();
  155. if ((sp < ProcessStackInfo[slot].sp_limit) ||
  156. (sp > ProcessStackInfo[slot].sp_top)) {
  157. return 0U; // SP out of range
  158. }
  159. ProcessStackInfo[slot].sp = sp;
  160. // Default process stack pointer and stack limit
  161. __set_PSPLIM((uint32_t)ProcessStackMemory);
  162. __set_PSP ((uint32_t)ProcessStackMemory);
  163. return 1U; // Success
  164. }
  165. #endif