Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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