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