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