Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 1 | /* |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2018, Arm Limited. All rights reserved. |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 8 | /* This file contains the APIs exported by the SPM to tfm core */ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 9 | |
| 10 | #include <stdio.h> |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 11 | #include "spm_api.h" |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 12 | #include "spm_db.h" |
| 13 | #include "tfm_api.h" |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 14 | #include "mpu_armv8m_drv.h" |
| 15 | #include "region_defs.h" |
| 16 | #include "secure_fw/core/tfm_core.h" |
| 17 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 18 | struct spm_partition_db_t g_spm_partition_db = {0,}; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 19 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 20 | #define MPU_REGION_VENEERS 0 |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 21 | #define MPU_REGION_TFM_UNPRIV_CODE 1 |
| 22 | #define MPU_REGION_TFM_UNPRIV_DATA 2 |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 23 | #define MPU_REGION_NS_DATA 3 |
| 24 | #define PARTITION_REGION_RO 4 |
| 25 | #define PARTITION_REGION_RW_STACK 5 |
| 26 | #define PARTITION_REGION_PERIPH 6 |
| 27 | #define PARTITION_REGION_SHARE 7 |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 28 | |
| 29 | /* This should move to platform retarget */ |
| 30 | struct mpu_armv8m_dev_t dev_mpu_s = { MPU_BASE }; |
| 31 | |
| 32 | typedef enum { |
| 33 | TFM_INIT_FAILURE, |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 34 | } sp_error_type_t; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 35 | |
| 36 | /* |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 37 | * This function is called when a secure partition causes an error. |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 38 | * In case of an error in the error handling, a non-zero value have to be |
| 39 | * returned. |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 40 | */ |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 41 | static void tfm_spm_partition_err_handler( |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 42 | struct spm_partition_desc_t *partition, |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 43 | sp_error_type_t err_type, |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 44 | int32_t err_code) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 45 | { |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 46 | #ifdef TFM_CORE_DEBUG |
| 47 | if (err_type == TFM_INIT_FAILURE) { |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 48 | printf("Partition init failed for partition id 0x%08X\r\n", |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 49 | partition->static_data.partition_id); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 50 | } else { |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 51 | printf("Unknown partition error %d for partition id 0x%08X\r\n", |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 52 | err_type, partition->static_data.partition_id); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 53 | } |
| 54 | #endif |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 55 | tfm_spm_partition_set_state(partition->static_data.partition_id, |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 56 | SPM_PARTITION_STATE_CLOSED); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 59 | uint32_t get_partition_idx(uint32_t partition_id) |
| 60 | { |
| 61 | int i; |
| 62 | |
| 63 | if (partition_id == INVALID_PARTITION_ID) { |
| 64 | return SPM_INVALID_PARTITION_IDX; |
| 65 | } |
| 66 | |
| 67 | for (i = 0; i < g_spm_partition_db.partition_count; ++i) { |
| 68 | if (g_spm_partition_db.partitions[i].static_data.partition_id == |
| 69 | partition_id) { |
| 70 | return i; |
| 71 | } |
| 72 | } |
| 73 | return SPM_INVALID_PARTITION_IDX; |
| 74 | } |
| 75 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 76 | enum spm_err_t tfm_spm_db_init(void) |
| 77 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 78 | struct spm_partition_desc_t *part_ptr; |
| 79 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 80 | /* This function initialises partition db */ |
| 81 | g_spm_partition_db.is_init = 1; |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 82 | g_spm_partition_db.running_partition_idx = SPM_INVALID_PARTITION_IDX; |
| 83 | g_spm_partition_db.partition_count = 0; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 84 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 85 | /* There are a few partitions that are used by TF-M internally. |
| 86 | * These are explicitly added to the partition db here. |
| 87 | */ |
| 88 | |
| 89 | /* For the non secure Execution environment */ |
| 90 | if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) { |
| 91 | return SPM_ERR_INVALID_CONFIG; |
| 92 | } |
| 93 | part_ptr = &(g_spm_partition_db.partitions[ |
| 94 | g_spm_partition_db.partition_count]); |
| 95 | part_ptr->static_data.partition_id = TFM_SP_NON_SECURE_ID; |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame^] | 96 | part_ptr->static_data.partition_flags = 0; |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 97 | part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT; |
| 98 | ++g_spm_partition_db.partition_count; |
| 99 | |
| 100 | /* For the TF-M core environment itself */ |
| 101 | if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) { |
| 102 | return SPM_ERR_INVALID_CONFIG; |
| 103 | } |
| 104 | part_ptr = &(g_spm_partition_db.partitions[ |
| 105 | g_spm_partition_db.partition_count]); |
| 106 | part_ptr->static_data.partition_id = TFM_SP_CORE_ID; |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame^] | 107 | part_ptr->static_data.partition_flags = |
| 108 | SPM_PART_FLAG_SECURE | SPM_PART_FLAG_TRUSTED; |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 109 | part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT; |
| 110 | ++g_spm_partition_db.partition_count; |
| 111 | |
| 112 | /* Add user-defined secure partitions */ |
| 113 | #include "user_partition_defines.inc" |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 114 | |
| 115 | return SPM_ERR_OK; |
| 116 | } |
| 117 | |
| 118 | #if TFM_LVL != 1 |
| 119 | REGION_DECLARE(Image$$, TFM_UNPRIV_CODE, $$RO$$Base); |
| 120 | REGION_DECLARE(Image$$, TFM_UNPRIV_CODE, $$RO$$Limit); |
| 121 | REGION_DECLARE(Image$$, TFM_UNPRIV_RO_DATA, $$RW$$Base); |
| 122 | REGION_DECLARE(Image$$, TFM_UNPRIV_RO_DATA, $$ZI$$Limit); |
| 123 | REGION_DECLARE(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Base); |
| 124 | REGION_DECLARE(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Limit); |
| 125 | |
| 126 | enum spm_err_t tfm_spm_mpu_init(void) |
| 127 | { |
| 128 | mpu_armv8m_clean(&dev_mpu_s); |
| 129 | |
| 130 | struct mpu_armv8m_region_cfg_t region_cfg; |
| 131 | |
| 132 | /* Veneer region */ |
| 133 | region_cfg.region_nr = MPU_REGION_VENEERS; |
| 134 | region_cfg.region_base = CMSE_VENEER_REGION_START; |
| 135 | region_cfg.region_limit = CMSE_VENEER_REGION_LIMIT; |
| 136 | region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV; |
| 137 | region_cfg.attr_sh = MPU_ARMV8M_SH_NONE; |
| 138 | region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK; |
| 139 | mpu_armv8m_region_enable(&dev_mpu_s, ®ion_cfg); |
| 140 | |
| 141 | /* TFM Core unprivileged code region */ |
| 142 | region_cfg.region_nr = MPU_REGION_TFM_UNPRIV_CODE; |
| 143 | region_cfg.region_base = |
| 144 | (uint32_t)®ION_NAME(Image$$, TFM_UNPRIV_CODE, $$RO$$Base); |
| 145 | region_cfg.region_limit = |
| 146 | (uint32_t)®ION_NAME(Image$$, TFM_UNPRIV_CODE, $$RO$$Limit); |
| 147 | region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV; |
| 148 | region_cfg.attr_sh = MPU_ARMV8M_SH_NONE; |
| 149 | region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK; |
| 150 | mpu_armv8m_region_enable(&dev_mpu_s, ®ion_cfg); |
| 151 | |
| 152 | /* TFM Core unprivileged data region */ |
| 153 | region_cfg.region_nr = MPU_REGION_TFM_UNPRIV_DATA; |
| 154 | region_cfg.region_base = |
| 155 | (uint32_t)®ION_NAME(Image$$, TFM_UNPRIV_RO_DATA, $$RW$$Base); |
| 156 | region_cfg.region_limit = |
| 157 | (uint32_t)®ION_NAME(Image$$, TFM_UNPRIV_RO_DATA, $$ZI$$Limit); |
| 158 | region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV; |
| 159 | region_cfg.attr_sh = MPU_ARMV8M_SH_NONE; |
| 160 | region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER; |
| 161 | mpu_armv8m_region_enable(&dev_mpu_s, ®ion_cfg); |
| 162 | |
| 163 | /* TFM Core unprivileged non-secure data region */ |
| 164 | region_cfg.region_nr = MPU_REGION_NS_DATA; |
| 165 | region_cfg.region_base = NS_DATA_START; |
| 166 | region_cfg.region_limit = NS_DATA_LIMIT; |
| 167 | region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV; |
| 168 | region_cfg.attr_sh = MPU_ARMV8M_SH_NONE; |
| 169 | region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER; |
| 170 | mpu_armv8m_region_enable(&dev_mpu_s, ®ion_cfg); |
| 171 | |
| 172 | mpu_armv8m_enable(&dev_mpu_s, 1, 1); |
| 173 | |
| 174 | return SPM_ERR_OK; |
| 175 | } |
| 176 | |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 177 | /** |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 178 | * Set share region to which the partition needs access |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 179 | */ |
| 180 | static enum spm_err_t tfm_spm_set_share_region( |
| 181 | enum tfm_buffer_share_region_e share) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 182 | { |
| 183 | enum spm_err_t res = SPM_ERR_INVALID_CONFIG; |
| 184 | uint32_t scratch_base = |
| 185 | (uint32_t)®ION_NAME(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Base); |
| 186 | uint32_t scratch_limit = |
| 187 | (uint32_t)®ION_NAME(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Limit); |
| 188 | |
| 189 | mpu_armv8m_disable(&dev_mpu_s); |
| 190 | |
| 191 | if (share == TFM_BUFFER_SHARE_DISABLE) { |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 192 | mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_SHARE); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 193 | } else { |
| 194 | struct mpu_armv8m_region_cfg_t region_cfg; |
| 195 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 196 | region_cfg.region_nr = PARTITION_REGION_SHARE; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 197 | region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV; |
| 198 | region_cfg.attr_sh = MPU_ARMV8M_SH_NONE; |
| 199 | region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER; |
| 200 | switch (share) { |
| 201 | case TFM_BUFFER_SHARE_SCRATCH: |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 202 | /* Use scratch area for SP-to-SP data sharing */ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 203 | region_cfg.region_base = scratch_base; |
| 204 | region_cfg.region_limit = scratch_limit; |
| 205 | res = SPM_ERR_OK; |
| 206 | break; |
| 207 | case TFM_BUFFER_SHARE_NS_CODE: |
| 208 | region_cfg.region_base = NS_CODE_START; |
| 209 | region_cfg.region_limit = NS_CODE_LIMIT; |
| 210 | /* Only allow read access to NS code region and keep |
| 211 | * exec.never attribute |
| 212 | */ |
| 213 | region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV; |
| 214 | res = SPM_ERR_OK; |
| 215 | break; |
| 216 | default: |
| 217 | res = SPM_ERR_INVALID_CONFIG; |
| 218 | break; |
| 219 | } |
| 220 | if (res == SPM_ERR_OK) { |
| 221 | mpu_armv8m_region_enable(&dev_mpu_s, ®ion_cfg); |
| 222 | } |
| 223 | } |
| 224 | mpu_armv8m_enable(&dev_mpu_s, 1, 1); |
| 225 | |
| 226 | return res; |
| 227 | } |
| 228 | #endif |
| 229 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 230 | enum spm_err_t tfm_spm_partition_init(void) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 231 | { |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 232 | struct spm_partition_desc_t *part; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 233 | int32_t fail_cnt = 0; |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 234 | uint32_t idx; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 235 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 236 | /* Call the init function for each partition */ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 237 | /* FixMe: This implementation only fits level 1 isolation. |
| 238 | * On higher levels MPU (and PPC) configuration need to be in place to have |
| 239 | * proper isolation during init. |
| 240 | */ |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 241 | for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) { |
| 242 | part = &g_spm_partition_db.partitions[idx]; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 243 | if (part->static_data.periph_start) { |
| 244 | ppc_configure_to_secure(part->static_data.periph_ppc_bank, |
| 245 | part->static_data.periph_ppc_loc); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 246 | } |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 247 | if (part->static_data.partition_init == NULL) { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 248 | tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE); |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 249 | } else { |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 250 | int32_t ret = part->static_data.partition_init(); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 251 | |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 252 | if (ret == TFM_SUCCESS) { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 253 | tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE); |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 254 | } else { |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 255 | tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, ret); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 256 | fail_cnt++; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | if (fail_cnt == 0) { |
| 262 | return SPM_ERR_OK; |
| 263 | } else { |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 264 | return SPM_ERR_PARTITION_NOT_AVAILABLE; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
| 268 | #if TFM_LVL != 1 |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 269 | enum spm_err_t tfm_spm_partition_sandbox_config(uint32_t partition_idx) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 270 | { |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 271 | /* This function takes a partition id and enables the |
| 272 | * SPM partition for that partition |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 273 | */ |
| 274 | |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 275 | struct spm_partition_desc_t *part; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 276 | struct mpu_armv8m_region_cfg_t region_cfg; |
| 277 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 278 | if (!g_spm_partition_db.is_init) { |
| 279 | return SPM_ERR_PARTITION_DB_NOT_INIT; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /*brute force id*/ |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 283 | part = &g_spm_partition_db.partitions[partition_idx]; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 284 | |
| 285 | mpu_armv8m_disable(&dev_mpu_s); |
| 286 | |
| 287 | /* Configure Regions */ |
| 288 | |
| 289 | /* RO region*/ |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 290 | region_cfg.region_nr = PARTITION_REGION_RO; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 291 | region_cfg.region_base = part->static_data.ro_start; |
| 292 | region_cfg.region_limit = part->static_data.ro_limit; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 293 | region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV; |
| 294 | region_cfg.attr_sh = MPU_ARMV8M_SH_NONE; |
| 295 | region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK; |
| 296 | |
| 297 | mpu_armv8m_region_enable(&dev_mpu_s, ®ion_cfg); |
| 298 | |
| 299 | /* RW, ZI and stack as one region*/ |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 300 | region_cfg.region_nr = PARTITION_REGION_RW_STACK; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 301 | region_cfg.region_base = part->static_data.rw_start; |
| 302 | region_cfg.region_limit = part->static_data.stack_top; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 303 | region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV; |
| 304 | region_cfg.attr_sh = MPU_ARMV8M_SH_NONE; |
| 305 | region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER; |
| 306 | |
| 307 | mpu_armv8m_region_enable(&dev_mpu_s, ®ion_cfg); |
| 308 | |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 309 | if (part->static_data.periph_start) { |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 310 | /* Peripheral */ |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 311 | region_cfg.region_nr = PARTITION_REGION_PERIPH; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 312 | region_cfg.region_base = part->static_data.periph_start; |
| 313 | region_cfg.region_limit = part->static_data.periph_limit; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 314 | region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV; |
| 315 | region_cfg.attr_sh = MPU_ARMV8M_SH_NONE; |
| 316 | region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER; |
| 317 | mpu_armv8m_region_enable(&dev_mpu_s, ®ion_cfg); |
| 318 | |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 319 | ppc_en_secure_unpriv(part->static_data.periph_ppc_bank, |
| 320 | part->static_data.periph_ppc_loc); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | mpu_armv8m_enable(&dev_mpu_s, 1, 1); |
| 324 | |
| 325 | #ifndef UNPRIV_JUMP_TO_NS |
| 326 | /* FixMe: if jump_to_ns_code() from unprivileged is solved, |
| 327 | * this code can be removed |
| 328 | */ |
| 329 | /* Initialization is done, set thread mode to unprivileged. |
| 330 | */ |
| 331 | CONTROL_Type ctrl; |
| 332 | |
| 333 | ctrl.w = __get_CONTROL(); |
| 334 | ctrl.b.nPRIV = 1; |
| 335 | __set_CONTROL(ctrl.w); |
| 336 | __DSB(); |
| 337 | __ISB(); |
| 338 | #endif |
| 339 | |
| 340 | return SPM_ERR_OK; |
| 341 | } |
| 342 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 343 | enum spm_err_t tfm_spm_partition_sandbox_deconfig(uint32_t partition_idx) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 344 | { |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 345 | /* This function takes a partition id and disables the |
| 346 | * SPM partition for that partition |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 347 | */ |
| 348 | |
| 349 | #ifndef UNPRIV_JUMP_TO_NS |
| 350 | /* FixMe: if jump_to_ns_code() from unprivileged is solved, |
| 351 | * this code can be removed |
| 352 | */ |
| 353 | CONTROL_Type ctrl; |
| 354 | |
| 355 | ctrl.w = __get_CONTROL(); |
| 356 | ctrl.b.nPRIV = 0; |
| 357 | __set_CONTROL(ctrl.w); |
| 358 | __DSB(); |
| 359 | __ISB(); |
| 360 | #endif |
| 361 | |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 362 | struct spm_partition_desc_t *part; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 363 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 364 | part = &g_spm_partition_db.partitions[partition_idx]; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 365 | |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 366 | if (part->static_data.periph_start) { |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 367 | /* Peripheral */ |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 368 | ppc_clr_secure_unpriv(part->static_data.periph_ppc_bank, |
| 369 | part->static_data.periph_ppc_loc); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | mpu_armv8m_disable(&dev_mpu_s); |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 373 | mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_RO); |
| 374 | mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_RW_STACK); |
| 375 | mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_PERIPH); |
| 376 | mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_SHARE); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 377 | mpu_armv8m_enable(&dev_mpu_s, 1, 1); |
| 378 | |
| 379 | return SPM_ERR_OK; |
| 380 | } |
| 381 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 382 | uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 383 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 384 | return g_spm_partition_db.partitions[partition_idx]. |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 385 | static_data.stack_bottom; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 388 | uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 389 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 390 | return g_spm_partition_db.partitions[partition_idx].static_data.stack_top; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 393 | void tfm_spm_partition_set_stack(uint32_t partition_idx, uint32_t stack_ptr) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 394 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 395 | g_spm_partition_db.partitions[partition_idx]. |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 396 | runtime_data.stack_ptr = stack_ptr; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 397 | } |
| 398 | #endif |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 399 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 400 | uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx) |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 401 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 402 | return g_spm_partition_db.partitions[partition_idx].static_data. |
| 403 | partition_id; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 404 | } |
| 405 | |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame^] | 406 | uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx) |
| 407 | { |
| 408 | return g_spm_partition_db.partitions[partition_idx].static_data. |
| 409 | partition_flags; |
| 410 | } |
| 411 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 412 | const struct spm_partition_runtime_data_t * |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame^] | 413 | tfm_spm_partition_get_runtime_data(uint32_t partition_idx) |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 414 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 415 | return &(g_spm_partition_db.partitions[partition_idx].runtime_data); |
| 416 | } |
| 417 | |
| 418 | void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state) |
| 419 | { |
| 420 | g_spm_partition_db.partitions[partition_idx].runtime_data.partition_state = |
| 421 | state; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 422 | if (state == SPM_PARTITION_STATE_RUNNING) { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 423 | g_spm_partition_db.running_partition_idx = partition_idx; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 427 | void tfm_spm_partition_set_caller_partition_id(uint32_t partition_idx, |
| 428 | uint32_t caller_partition_idx) |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 429 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 430 | g_spm_partition_db.partitions[partition_idx].runtime_data. |
| 431 | caller_partition_idx = caller_partition_idx; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 432 | } |
| 433 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 434 | void tfm_spm_partition_set_orig_psp(uint32_t partition_idx, |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 435 | uint32_t orig_psp) |
| 436 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 437 | g_spm_partition_db.partitions[partition_idx].runtime_data.orig_psp = |
| 438 | orig_psp; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 439 | } |
| 440 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 441 | void tfm_spm_partition_set_orig_psplim(uint32_t partition_idx, |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 442 | uint32_t orig_psplim) |
| 443 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 444 | g_spm_partition_db.partitions[partition_idx].runtime_data.orig_psplim = |
| 445 | orig_psplim; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 446 | } |
| 447 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 448 | void tfm_spm_partition_set_orig_lr(uint32_t partition_idx, uint32_t orig_lr) |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 449 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 450 | g_spm_partition_db.partitions[partition_idx].runtime_data.orig_lr = orig_lr; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 451 | } |
| 452 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 453 | enum spm_err_t tfm_spm_partition_set_share(uint32_t partition_idx, |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 454 | uint32_t share) |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 455 | { |
| 456 | enum spm_err_t ret = SPM_ERR_OK; |
| 457 | |
| 458 | #if TFM_LVL != 1 |
| 459 | /* Only need to set configuration on levels higher than 1 */ |
| 460 | ret = tfm_spm_set_share_region(share); |
| 461 | #endif |
| 462 | |
| 463 | if (ret == SPM_ERR_OK) { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 464 | g_spm_partition_db.partitions[partition_idx].runtime_data.share = share; |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 465 | } |
| 466 | return ret; |
| 467 | } |
| 468 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 469 | uint32_t tfm_spm_partition_get_running_partition_idx(void) |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 470 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 471 | return g_spm_partition_db.running_partition_idx; |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 472 | } |
| 473 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 474 | void tfm_spm_partition_cleanup_context(uint32_t partition_idx) |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 475 | { |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 476 | struct spm_partition_desc_t *partition = |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 477 | &(g_spm_partition_db.partitions[partition_idx]); |
| 478 | partition->runtime_data.caller_partition_idx = SPM_INVALID_PARTITION_IDX; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 479 | partition->runtime_data.orig_psp = 0; |
| 480 | partition->runtime_data.orig_psplim = 0; |
| 481 | partition->runtime_data.orig_lr = 0; |
| 482 | partition->runtime_data.share = 0; |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 483 | } |