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; |
| 96 | part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT; |
| 97 | ++g_spm_partition_db.partition_count; |
| 98 | |
| 99 | /* For the TF-M core environment itself */ |
| 100 | if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) { |
| 101 | return SPM_ERR_INVALID_CONFIG; |
| 102 | } |
| 103 | part_ptr = &(g_spm_partition_db.partitions[ |
| 104 | g_spm_partition_db.partition_count]); |
| 105 | part_ptr->static_data.partition_id = TFM_SP_CORE_ID; |
| 106 | part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT; |
| 107 | ++g_spm_partition_db.partition_count; |
| 108 | |
| 109 | /* Add user-defined secure partitions */ |
| 110 | #include "user_partition_defines.inc" |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 111 | |
| 112 | return SPM_ERR_OK; |
| 113 | } |
| 114 | |
| 115 | #if TFM_LVL != 1 |
| 116 | REGION_DECLARE(Image$$, TFM_UNPRIV_CODE, $$RO$$Base); |
| 117 | REGION_DECLARE(Image$$, TFM_UNPRIV_CODE, $$RO$$Limit); |
| 118 | REGION_DECLARE(Image$$, TFM_UNPRIV_RO_DATA, $$RW$$Base); |
| 119 | REGION_DECLARE(Image$$, TFM_UNPRIV_RO_DATA, $$ZI$$Limit); |
| 120 | REGION_DECLARE(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Base); |
| 121 | REGION_DECLARE(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Limit); |
| 122 | |
| 123 | enum spm_err_t tfm_spm_mpu_init(void) |
| 124 | { |
| 125 | mpu_armv8m_clean(&dev_mpu_s); |
| 126 | |
| 127 | struct mpu_armv8m_region_cfg_t region_cfg; |
| 128 | |
| 129 | /* Veneer region */ |
| 130 | region_cfg.region_nr = MPU_REGION_VENEERS; |
| 131 | region_cfg.region_base = CMSE_VENEER_REGION_START; |
| 132 | region_cfg.region_limit = CMSE_VENEER_REGION_LIMIT; |
| 133 | region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV; |
| 134 | region_cfg.attr_sh = MPU_ARMV8M_SH_NONE; |
| 135 | region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK; |
| 136 | mpu_armv8m_region_enable(&dev_mpu_s, ®ion_cfg); |
| 137 | |
| 138 | /* TFM Core unprivileged code region */ |
| 139 | region_cfg.region_nr = MPU_REGION_TFM_UNPRIV_CODE; |
| 140 | region_cfg.region_base = |
| 141 | (uint32_t)®ION_NAME(Image$$, TFM_UNPRIV_CODE, $$RO$$Base); |
| 142 | region_cfg.region_limit = |
| 143 | (uint32_t)®ION_NAME(Image$$, TFM_UNPRIV_CODE, $$RO$$Limit); |
| 144 | region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV; |
| 145 | region_cfg.attr_sh = MPU_ARMV8M_SH_NONE; |
| 146 | region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK; |
| 147 | mpu_armv8m_region_enable(&dev_mpu_s, ®ion_cfg); |
| 148 | |
| 149 | /* TFM Core unprivileged data region */ |
| 150 | region_cfg.region_nr = MPU_REGION_TFM_UNPRIV_DATA; |
| 151 | region_cfg.region_base = |
| 152 | (uint32_t)®ION_NAME(Image$$, TFM_UNPRIV_RO_DATA, $$RW$$Base); |
| 153 | region_cfg.region_limit = |
| 154 | (uint32_t)®ION_NAME(Image$$, TFM_UNPRIV_RO_DATA, $$ZI$$Limit); |
| 155 | region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV; |
| 156 | region_cfg.attr_sh = MPU_ARMV8M_SH_NONE; |
| 157 | region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER; |
| 158 | mpu_armv8m_region_enable(&dev_mpu_s, ®ion_cfg); |
| 159 | |
| 160 | /* TFM Core unprivileged non-secure data region */ |
| 161 | region_cfg.region_nr = MPU_REGION_NS_DATA; |
| 162 | region_cfg.region_base = NS_DATA_START; |
| 163 | region_cfg.region_limit = NS_DATA_LIMIT; |
| 164 | region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV; |
| 165 | region_cfg.attr_sh = MPU_ARMV8M_SH_NONE; |
| 166 | region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER; |
| 167 | mpu_armv8m_region_enable(&dev_mpu_s, ®ion_cfg); |
| 168 | |
| 169 | mpu_armv8m_enable(&dev_mpu_s, 1, 1); |
| 170 | |
| 171 | return SPM_ERR_OK; |
| 172 | } |
| 173 | |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 174 | /** |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 175 | * Set share region to which the partition needs access |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 176 | */ |
| 177 | static enum spm_err_t tfm_spm_set_share_region( |
| 178 | enum tfm_buffer_share_region_e share) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 179 | { |
| 180 | enum spm_err_t res = SPM_ERR_INVALID_CONFIG; |
| 181 | uint32_t scratch_base = |
| 182 | (uint32_t)®ION_NAME(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Base); |
| 183 | uint32_t scratch_limit = |
| 184 | (uint32_t)®ION_NAME(Image$$, TFM_UNPRIV_SCRATCH, $$ZI$$Limit); |
| 185 | |
| 186 | mpu_armv8m_disable(&dev_mpu_s); |
| 187 | |
| 188 | if (share == TFM_BUFFER_SHARE_DISABLE) { |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 189 | mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_SHARE); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 190 | } else { |
| 191 | struct mpu_armv8m_region_cfg_t region_cfg; |
| 192 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 193 | region_cfg.region_nr = PARTITION_REGION_SHARE; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 194 | region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV; |
| 195 | region_cfg.attr_sh = MPU_ARMV8M_SH_NONE; |
| 196 | region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER; |
| 197 | switch (share) { |
| 198 | case TFM_BUFFER_SHARE_SCRATCH: |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 199 | /* Use scratch area for SP-to-SP data sharing */ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 200 | region_cfg.region_base = scratch_base; |
| 201 | region_cfg.region_limit = scratch_limit; |
| 202 | res = SPM_ERR_OK; |
| 203 | break; |
| 204 | case TFM_BUFFER_SHARE_NS_CODE: |
| 205 | region_cfg.region_base = NS_CODE_START; |
| 206 | region_cfg.region_limit = NS_CODE_LIMIT; |
| 207 | /* Only allow read access to NS code region and keep |
| 208 | * exec.never attribute |
| 209 | */ |
| 210 | region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV; |
| 211 | res = SPM_ERR_OK; |
| 212 | break; |
| 213 | default: |
| 214 | res = SPM_ERR_INVALID_CONFIG; |
| 215 | break; |
| 216 | } |
| 217 | if (res == SPM_ERR_OK) { |
| 218 | mpu_armv8m_region_enable(&dev_mpu_s, ®ion_cfg); |
| 219 | } |
| 220 | } |
| 221 | mpu_armv8m_enable(&dev_mpu_s, 1, 1); |
| 222 | |
| 223 | return res; |
| 224 | } |
| 225 | #endif |
| 226 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 227 | enum spm_err_t tfm_spm_partition_init(void) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 228 | { |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 229 | struct spm_partition_desc_t *part; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 230 | int32_t fail_cnt = 0; |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 231 | uint32_t idx; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 232 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 233 | /* Call the init function for each partition */ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 234 | /* FixMe: This implementation only fits level 1 isolation. |
| 235 | * On higher levels MPU (and PPC) configuration need to be in place to have |
| 236 | * proper isolation during init. |
| 237 | */ |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 238 | for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) { |
| 239 | part = &g_spm_partition_db.partitions[idx]; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 240 | if (part->static_data.periph_start) { |
| 241 | ppc_configure_to_secure(part->static_data.periph_ppc_bank, |
| 242 | part->static_data.periph_ppc_loc); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 243 | } |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 244 | if (part->static_data.partition_init == NULL) { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 245 | tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE); |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 246 | } else { |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 247 | int32_t ret = part->static_data.partition_init(); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 248 | |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 249 | if (ret == TFM_SUCCESS) { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 250 | tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE); |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 251 | } else { |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 252 | tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, ret); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 253 | fail_cnt++; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | if (fail_cnt == 0) { |
| 259 | return SPM_ERR_OK; |
| 260 | } else { |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 261 | return SPM_ERR_PARTITION_NOT_AVAILABLE; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
| 265 | #if TFM_LVL != 1 |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 266 | 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] | 267 | { |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 268 | /* This function takes a partition id and enables the |
| 269 | * SPM partition for that partition |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 270 | */ |
| 271 | |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 272 | struct spm_partition_desc_t *part; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 273 | struct mpu_armv8m_region_cfg_t region_cfg; |
| 274 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 275 | if (!g_spm_partition_db.is_init) { |
| 276 | return SPM_ERR_PARTITION_DB_NOT_INIT; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /*brute force id*/ |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 280 | part = &g_spm_partition_db.partitions[partition_idx]; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 281 | |
| 282 | mpu_armv8m_disable(&dev_mpu_s); |
| 283 | |
| 284 | /* Configure Regions */ |
| 285 | |
| 286 | /* RO region*/ |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 287 | region_cfg.region_nr = PARTITION_REGION_RO; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 288 | region_cfg.region_base = part->static_data.ro_start; |
| 289 | region_cfg.region_limit = part->static_data.ro_limit; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 290 | region_cfg.attr_access = MPU_ARMV8M_AP_RO_PRIV_UNPRIV; |
| 291 | region_cfg.attr_sh = MPU_ARMV8M_SH_NONE; |
| 292 | region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_OK; |
| 293 | |
| 294 | mpu_armv8m_region_enable(&dev_mpu_s, ®ion_cfg); |
| 295 | |
| 296 | /* RW, ZI and stack as one region*/ |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 297 | region_cfg.region_nr = PARTITION_REGION_RW_STACK; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 298 | region_cfg.region_base = part->static_data.rw_start; |
| 299 | region_cfg.region_limit = part->static_data.stack_top; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 300 | region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV; |
| 301 | region_cfg.attr_sh = MPU_ARMV8M_SH_NONE; |
| 302 | region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER; |
| 303 | |
| 304 | mpu_armv8m_region_enable(&dev_mpu_s, ®ion_cfg); |
| 305 | |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 306 | if (part->static_data.periph_start) { |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 307 | /* Peripheral */ |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 308 | region_cfg.region_nr = PARTITION_REGION_PERIPH; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 309 | region_cfg.region_base = part->static_data.periph_start; |
| 310 | region_cfg.region_limit = part->static_data.periph_limit; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 311 | region_cfg.attr_access = MPU_ARMV8M_AP_RW_PRIV_UNPRIV; |
| 312 | region_cfg.attr_sh = MPU_ARMV8M_SH_NONE; |
| 313 | region_cfg.attr_exec = MPU_ARMV8M_XN_EXEC_NEVER; |
| 314 | mpu_armv8m_region_enable(&dev_mpu_s, ®ion_cfg); |
| 315 | |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 316 | ppc_en_secure_unpriv(part->static_data.periph_ppc_bank, |
| 317 | part->static_data.periph_ppc_loc); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | mpu_armv8m_enable(&dev_mpu_s, 1, 1); |
| 321 | |
| 322 | #ifndef UNPRIV_JUMP_TO_NS |
| 323 | /* FixMe: if jump_to_ns_code() from unprivileged is solved, |
| 324 | * this code can be removed |
| 325 | */ |
| 326 | /* Initialization is done, set thread mode to unprivileged. |
| 327 | */ |
| 328 | CONTROL_Type ctrl; |
| 329 | |
| 330 | ctrl.w = __get_CONTROL(); |
| 331 | ctrl.b.nPRIV = 1; |
| 332 | __set_CONTROL(ctrl.w); |
| 333 | __DSB(); |
| 334 | __ISB(); |
| 335 | #endif |
| 336 | |
| 337 | return SPM_ERR_OK; |
| 338 | } |
| 339 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 340 | 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] | 341 | { |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 342 | /* This function takes a partition id and disables the |
| 343 | * SPM partition for that partition |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 344 | */ |
| 345 | |
| 346 | #ifndef UNPRIV_JUMP_TO_NS |
| 347 | /* FixMe: if jump_to_ns_code() from unprivileged is solved, |
| 348 | * this code can be removed |
| 349 | */ |
| 350 | CONTROL_Type ctrl; |
| 351 | |
| 352 | ctrl.w = __get_CONTROL(); |
| 353 | ctrl.b.nPRIV = 0; |
| 354 | __set_CONTROL(ctrl.w); |
| 355 | __DSB(); |
| 356 | __ISB(); |
| 357 | #endif |
| 358 | |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 359 | struct spm_partition_desc_t *part; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 360 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 361 | part = &g_spm_partition_db.partitions[partition_idx]; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 362 | |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 363 | if (part->static_data.periph_start) { |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 364 | /* Peripheral */ |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 365 | ppc_clr_secure_unpriv(part->static_data.periph_ppc_bank, |
| 366 | part->static_data.periph_ppc_loc); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | mpu_armv8m_disable(&dev_mpu_s); |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 370 | mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_RO); |
| 371 | mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_RW_STACK); |
| 372 | mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_PERIPH); |
| 373 | mpu_armv8m_region_disable(&dev_mpu_s, PARTITION_REGION_SHARE); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 374 | mpu_armv8m_enable(&dev_mpu_s, 1, 1); |
| 375 | |
| 376 | return SPM_ERR_OK; |
| 377 | } |
| 378 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 379 | uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 380 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 381 | return g_spm_partition_db.partitions[partition_idx]. |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 382 | static_data.stack_bottom; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 385 | uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 386 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 387 | return g_spm_partition_db.partitions[partition_idx].static_data.stack_top; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 390 | 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] | 391 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 392 | g_spm_partition_db.partitions[partition_idx]. |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 393 | runtime_data.stack_ptr = stack_ptr; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 394 | } |
| 395 | #endif |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 396 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 397 | 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] | 398 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 399 | return g_spm_partition_db.partitions[partition_idx].static_data. |
| 400 | partition_id; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 401 | } |
| 402 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 403 | const struct spm_partition_runtime_data_t * |
| 404 | tfm_spm_partition_get_runtime_data(uint32_t partition_idx) |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 405 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 406 | return &(g_spm_partition_db.partitions[partition_idx].runtime_data); |
| 407 | } |
| 408 | |
| 409 | void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state) |
| 410 | { |
| 411 | g_spm_partition_db.partitions[partition_idx].runtime_data.partition_state = |
| 412 | state; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 413 | if (state == SPM_PARTITION_STATE_RUNNING) { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 414 | g_spm_partition_db.running_partition_idx = partition_idx; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 418 | void tfm_spm_partition_set_caller_partition_id(uint32_t partition_idx, |
| 419 | uint32_t caller_partition_idx) |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 420 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 421 | g_spm_partition_db.partitions[partition_idx].runtime_data. |
| 422 | caller_partition_idx = caller_partition_idx; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 423 | } |
| 424 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 425 | void tfm_spm_partition_set_orig_psp(uint32_t partition_idx, |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 426 | uint32_t orig_psp) |
| 427 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 428 | g_spm_partition_db.partitions[partition_idx].runtime_data.orig_psp = |
| 429 | orig_psp; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 430 | } |
| 431 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 432 | void tfm_spm_partition_set_orig_psplim(uint32_t partition_idx, |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 433 | uint32_t orig_psplim) |
| 434 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 435 | g_spm_partition_db.partitions[partition_idx].runtime_data.orig_psplim = |
| 436 | orig_psplim; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 437 | } |
| 438 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 439 | 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] | 440 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 441 | 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] | 442 | } |
| 443 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 444 | 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] | 445 | uint32_t share) |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 446 | { |
| 447 | enum spm_err_t ret = SPM_ERR_OK; |
| 448 | |
| 449 | #if TFM_LVL != 1 |
| 450 | /* Only need to set configuration on levels higher than 1 */ |
| 451 | ret = tfm_spm_set_share_region(share); |
| 452 | #endif |
| 453 | |
| 454 | if (ret == SPM_ERR_OK) { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 455 | g_spm_partition_db.partitions[partition_idx].runtime_data.share = share; |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 456 | } |
| 457 | return ret; |
| 458 | } |
| 459 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 460 | uint32_t tfm_spm_partition_get_running_partition_idx(void) |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 461 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 462 | return g_spm_partition_db.running_partition_idx; |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 463 | } |
| 464 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 465 | void tfm_spm_partition_cleanup_context(uint32_t partition_idx) |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 466 | { |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 467 | struct spm_partition_desc_t *partition = |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame^] | 468 | &(g_spm_partition_db.partitions[partition_idx]); |
| 469 | partition->runtime_data.caller_partition_idx = SPM_INVALID_PARTITION_IDX; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 470 | partition->runtime_data.orig_psp = 0; |
| 471 | partition->runtime_data.orig_psplim = 0; |
| 472 | partition->runtime_data.orig_lr = 0; |
| 473 | partition->runtime_data.share = 0; |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 474 | } |