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