tz_context.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #include "RTE_Components.h"
  25. #include CMSIS_device_header
  26. #include "tz_context.h"
  27. /// Number of process slots (threads may call secure library code)
  28. #ifndef TZ_PROCESS_STACK_SLOTS
  29. #define TZ_PROCESS_STACK_SLOTS 8U
  30. #endif
  31. /// Stack size of the secure library code
  32. #ifndef TZ_PROCESS_STACK_SIZE
  33. #define TZ_PROCESS_STACK_SIZE 256U
  34. #endif
  35. typedef struct {
  36. uint32_t sp_top; // stack space top
  37. uint32_t sp_limit; // stack space limit
  38. uint32_t sp; // current stack pointer
  39. } stack_info_t;
  40. static stack_info_t ProcessStackInfo [TZ_PROCESS_STACK_SLOTS];
  41. static uint64_t ProcessStackMemory[TZ_PROCESS_STACK_SLOTS][TZ_PROCESS_STACK_SIZE/8U];
  42. static uint32_t ProcessStackFreeSlot = 0xFFFFFFFFU;
  43. /// Initialize secure context memory system
  44. /// \return execution status (1: success, 0: error)
  45. __attribute__((cmse_nonsecure_entry))
  46. uint32_t TZ_InitContextSystem_S (void) {
  47. uint32_t n;
  48. if (__get_IPSR() == 0U) {
  49. return 0U; // Thread Mode
  50. }
  51. for (n = 0U; n < TZ_PROCESS_STACK_SLOTS; n++) {
  52. ProcessStackInfo[n].sp = 0U;
  53. ProcessStackInfo[n].sp_limit = (uint32_t)&ProcessStackMemory[n];
  54. ProcessStackInfo[n].sp_top = (uint32_t)&ProcessStackMemory[n] + TZ_PROCESS_STACK_SIZE;
  55. *((uint32_t *)ProcessStackMemory[n]) = n + 1U;
  56. }
  57. *((uint32_t *)ProcessStackMemory[--n]) = 0xFFFFFFFFU;
  58. ProcessStackFreeSlot = 0U;
  59. // Default process stack pointer and stack limit
  60. __set_PSPLIM((uint32_t)ProcessStackMemory);
  61. __set_PSP ((uint32_t)ProcessStackMemory);
  62. // Privileged Thread Mode using PSP
  63. __set_CONTROL(0x02U);
  64. return 1U; // Success
  65. }
  66. /// Allocate context memory for calling secure software modules in TrustZone
  67. /// \param[in] module identifies software modules called from non-secure mode
  68. /// \return value != 0 id TrustZone memory slot identifier
  69. /// \return value 0 no memory available or internal error
  70. __attribute__((cmse_nonsecure_entry))
  71. TZ_MemoryId_t TZ_AllocModuleContext_S (TZ_ModuleId_t module) {
  72. uint32_t slot;
  73. (void)module; // Ignore (fixed Stack size)
  74. if (__get_IPSR() == 0U) {
  75. return 0U; // Thread Mode
  76. }
  77. if (ProcessStackFreeSlot == 0xFFFFFFFFU) {
  78. return 0U; // No slot available
  79. }
  80. slot = ProcessStackFreeSlot;
  81. ProcessStackFreeSlot = *((uint32_t *)ProcessStackMemory[slot]);
  82. ProcessStackInfo[slot].sp = ProcessStackInfo[slot].sp_top;
  83. return (slot + 1U);
  84. }
  85. /// Free context memory that was previously allocated with \ref TZ_AllocModuleContext_S
  86. /// \param[in] id TrustZone memory slot identifier
  87. /// \return execution status (1: success, 0: error)
  88. __attribute__((cmse_nonsecure_entry))
  89. uint32_t TZ_FreeModuleContext_S (TZ_MemoryId_t id) {
  90. uint32_t slot;
  91. if (__get_IPSR() == 0U) {
  92. return 0U; // Thread Mode
  93. }
  94. if ((id == 0U) || (id > TZ_PROCESS_STACK_SLOTS)) {
  95. return 0U; // Invalid ID
  96. }
  97. slot = id - 1U;
  98. if (ProcessStackInfo[slot].sp == 0U) {
  99. return 0U; // Inactive slot
  100. }
  101. ProcessStackInfo[slot].sp = 0U;
  102. *((uint32_t *)ProcessStackMemory[slot]) = ProcessStackFreeSlot;
  103. ProcessStackFreeSlot = slot;
  104. return 1U; // Success
  105. }
  106. /// Load secure context (called on RTOS thread context switch)
  107. /// \param[in] id TrustZone memory slot identifier
  108. /// \return execution status (1: success, 0: error)
  109. __attribute__((cmse_nonsecure_entry))
  110. uint32_t TZ_LoadContext_S (TZ_MemoryId_t id) {
  111. uint32_t slot;
  112. if ((__get_IPSR() == 0U) || ((__get_CONTROL() & 2U) == 0U)) {
  113. return 0U; // Thread Mode or using Main Stack for threads
  114. }
  115. if ((id == 0U) || (id > TZ_PROCESS_STACK_SLOTS)) {
  116. return 0U; // Invalid ID
  117. }
  118. slot = id - 1U;
  119. if (ProcessStackInfo[slot].sp == 0U) {
  120. return 0U; // Inactive slot
  121. }
  122. // Setup process stack pointer and stack limit
  123. __set_PSPLIM(ProcessStackInfo[slot].sp_limit);
  124. __set_PSP (ProcessStackInfo[slot].sp);
  125. return 1U; // Success
  126. }
  127. /// Store secure context (called on RTOS thread context switch)
  128. /// \param[in] id TrustZone memory slot identifier
  129. /// \return execution status (1: success, 0: error)
  130. __attribute__((cmse_nonsecure_entry))
  131. uint32_t TZ_StoreContext_S (TZ_MemoryId_t id) {
  132. uint32_t slot;
  133. uint32_t sp;
  134. if ((__get_IPSR() == 0U) || ((__get_CONTROL() & 2U) == 0U)) {
  135. return 0U; // Thread Mode or using Main Stack for threads
  136. }
  137. if ((id == 0U) || (id > TZ_PROCESS_STACK_SLOTS)) {
  138. return 0U; // Invalid ID
  139. }
  140. slot = id - 1U;
  141. if (ProcessStackInfo[slot].sp == 0U) {
  142. return 0U; // Inactive slot
  143. }
  144. sp = __get_PSP();
  145. if ((sp < ProcessStackInfo[slot].sp_limit) ||
  146. (sp > ProcessStackInfo[slot].sp_top)) {
  147. return 0U; // SP out of range
  148. }
  149. ProcessStackInfo[slot].sp = sp;
  150. // Default process stack pointer and stack limit
  151. __set_PSPLIM((uint32_t)ProcessStackMemory);
  152. __set_PSP ((uint32_t)ProcessStackMemory);
  153. return 1U; // Success
  154. }