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