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