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