Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1 | /* |
Kevin Peng | a20b5af | 2021-01-11 11:20:52 +0800 | [diff] [blame] | 2 | * Copyright (c) 2017-2021, Arm Limited. All rights reserved. |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 8 | #include <stdint.h> |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 9 | #include <stdbool.h> |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 10 | #include <arm_cmse.h> |
Antonio de Angelis | 00667fc | 2021-05-05 22:40:39 +0100 | [diff] [blame] | 11 | #include "arch.h" |
Ken Liu | 24dffb2 | 2021-02-10 11:03:58 +0800 | [diff] [blame] | 12 | #include "bitops.h" |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 13 | #include "fih.h" |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 14 | #include "tfm_nspm.h" |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 15 | #include "tfm_api.h" |
| 16 | #include "tfm_arch.h" |
| 17 | #include "tfm_irq_list.h" |
| 18 | #include "psa/service.h" |
| 19 | #include "tfm_core_mem_check.h" |
Mingyang Sun | bd7ceb5 | 2020-06-11 16:53:03 +0800 | [diff] [blame] | 20 | #include "tfm_peripherals_def.h" |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 21 | #include "tfm_secure_api.h" |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 22 | #include "tfm_spm_hal.h" |
Soby Mathew | 960521a | 2020-09-29 12:48:50 +0100 | [diff] [blame] | 23 | #include "tfm_core_trustzone.h" |
Mingyang Sun | 7397b4f | 2020-06-17 15:07:45 +0800 | [diff] [blame] | 24 | #include "spm_func.h" |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 25 | #include "region_defs.h" |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 26 | #include "region.h" |
Mingyang Sun | 7397b4f | 2020-06-17 15:07:45 +0800 | [diff] [blame] | 27 | #include "spm_partition_defs.h" |
| 28 | #include "psa_manifest/pid.h" |
Summer Qin | 5fdcf63 | 2020-06-22 16:49:24 +0800 | [diff] [blame] | 29 | #include "tfm/tfm_spm_services.h" |
Mingyang Sun | bd7ceb5 | 2020-06-11 16:53:03 +0800 | [diff] [blame] | 30 | #include "tfm_spm_db_func.inc" |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 31 | |
David Hu | 5da82de | 2020-12-02 16:41:30 +0800 | [diff] [blame] | 32 | /* Structure to temporarily save iovec parameters from PSA client */ |
| 33 | struct iovec_params_t { |
| 34 | psa_invec in_vec[PSA_MAX_IOVEC]; |
| 35 | size_t in_len; |
| 36 | psa_outvec out_vec[PSA_MAX_IOVEC]; |
| 37 | size_t out_len; |
| 38 | |
| 39 | psa_outvec *orig_outvec; |
| 40 | }; |
| 41 | |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 42 | #define EXC_RETURN_SECURE_FUNCTION 0xFFFFFFFD |
| 43 | #define EXC_RETURN_SECURE_HANDLER 0xFFFFFFF1 |
| 44 | |
| 45 | #ifndef TFM_LVL |
| 46 | #error TFM_LVL is not defined! |
| 47 | #endif |
| 48 | |
Soby Mathew | 960521a | 2020-09-29 12:48:50 +0100 | [diff] [blame] | 49 | #ifdef TFM_MULTI_CORE_TOPOLOGY |
| 50 | #error Multi core is not supported by Function mode |
| 51 | #endif |
| 52 | |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 53 | REGION_DECLARE_T(Image$$, TFM_SECURE_STACK, $$ZI$$Base, uint32_t); |
| 54 | REGION_DECLARE_T(Image$$, TFM_SECURE_STACK, $$ZI$$Limit, struct iovec_args_t)[]; |
| 55 | |
Soby Mathew | 960521a | 2020-09-29 12:48:50 +0100 | [diff] [blame] | 56 | static uint32_t *tfm_secure_stack_seal = |
| 57 | ((uint32_t *)®ION_NAME(Image$$, TFM_SECURE_STACK, $$ZI$$Limit)[-1]) - 2; |
| 58 | |
shejia01 | 01a497c | 2021-04-21 10:04:07 +0800 | [diff] [blame] | 59 | REGION_DECLARE_T(Image$$, ARM_LIB_STACK_SEAL, $$ZI$$Base, uint32_t)[]; |
Soby Mathew | 960521a | 2020-09-29 12:48:50 +0100 | [diff] [blame] | 60 | |
| 61 | /* |
| 62 | * Function to seal the psp stacks for Function model of TF-M. |
| 63 | */ |
| 64 | void tfm_spm_seal_psp_stacks(void) |
| 65 | { |
| 66 | /* |
| 67 | * The top of TFM_SECURE_STACK is used for iovec parameters, we need to |
| 68 | * place the seal between iovec parameters and partition stack. |
| 69 | * |
| 70 | * Image$$TFM_SECURE_STACK$$ZI$$Limit-> +-------------------------+ |
| 71 | * | | |
| 72 | * | iovec parameters for | |
| 73 | * | partition | |
| 74 | * (Image$$TFM_SECURE_STACK$$ZI$$Limit -| | |
| 75 | * sizeof(iovec_args_t)) -> +-------------------------+ |
| 76 | * | Stack Seal | |
| 77 | * +-------------------------+ |
| 78 | * | | |
| 79 | * | Partition stack | |
| 80 | * | | |
| 81 | * Image$$TFM_SECURE_STACK$$ZI$$Base-> +-------------------------+ |
| 82 | */ |
| 83 | *(tfm_secure_stack_seal) = TFM_STACK_SEAL_VALUE; |
| 84 | *(tfm_secure_stack_seal + 1) = TFM_STACK_SEAL_VALUE; |
| 85 | |
| 86 | /* |
| 87 | * Seal the ARM_LIB_STACK by writing the seal value to the reserved |
| 88 | * region. |
| 89 | */ |
shejia01 | 01a497c | 2021-04-21 10:04:07 +0800 | [diff] [blame] | 90 | uint32_t *arm_lib_stck_seal_base = |
| 91 | ((uint32_t *)®ION_NAME(Image$$, ARM_LIB_STACK_SEAL, $$ZI$$Base)[-1]) - 2; |
Soby Mathew | 960521a | 2020-09-29 12:48:50 +0100 | [diff] [blame] | 92 | |
| 93 | *(arm_lib_stck_seal_base) = TFM_STACK_SEAL_VALUE; |
| 94 | *(arm_lib_stck_seal_base + 1) = TFM_STACK_SEAL_VALUE; |
| 95 | } |
| 96 | |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 97 | /* |
| 98 | * This is the "Big Lock" on the secure side, to guarantee single entry |
| 99 | * to SPE |
| 100 | */ |
Summer Qin | 5fdcf63 | 2020-06-22 16:49:24 +0800 | [diff] [blame] | 101 | static int32_t tfm_secure_lock; |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 102 | static int32_t tfm_secure_api_initializing = 1; |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 103 | |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 104 | static uint32_t *prepare_partition_iovec_ctx( |
| 105 | const struct tfm_state_context_t *svc_ctx, |
| 106 | const struct tfm_sfn_req_s *desc_ptr, |
| 107 | const struct iovec_args_t *iovec_args, |
| 108 | uint32_t *dst) |
| 109 | { |
| 110 | /* XPSR = as was when called, but make sure it's thread mode */ |
| 111 | *(--dst) = svc_ctx->xpsr & 0xFFFFFE00U; |
| 112 | /* ReturnAddress = resume veneer in new context */ |
| 113 | *(--dst) = svc_ctx->ra; |
| 114 | /* LR = sfn address */ |
| 115 | *(--dst) = (uint32_t)desc_ptr->sfn; |
| 116 | /* R12 = don't care */ |
| 117 | *(--dst) = 0U; |
| 118 | |
| 119 | /* R0-R3 = sfn arguments */ |
| 120 | *(--dst) = iovec_args->out_len; |
| 121 | *(--dst) = (uint32_t)iovec_args->out_vec; |
| 122 | *(--dst) = iovec_args->in_len; |
| 123 | *(--dst) = (uint32_t)iovec_args->in_vec; |
| 124 | |
| 125 | return dst; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * \brief Create a stack frame that sets the execution environment to thread |
| 130 | * mode on exception return. |
| 131 | * |
| 132 | * \param[in] svc_ctx The stacked SVC context |
| 133 | * \param[in] unpriv_handler The unprivileged IRQ handler to be called |
| 134 | * \param[in] dst A pointer where the context is to be created. (the |
| 135 | * pointer is considered to be a stack pointer, and |
| 136 | * the frame is created below it) |
| 137 | * |
| 138 | * \return A pointer pointing at the created stack frame. |
| 139 | */ |
| 140 | static int32_t *prepare_partition_irq_ctx( |
| 141 | const struct tfm_state_context_t *svc_ctx, |
| 142 | sfn_t unpriv_handler, |
| 143 | int32_t *dst) |
| 144 | { |
| 145 | int i; |
| 146 | |
| 147 | /* XPSR = as was when called, but make sure it's thread mode */ |
| 148 | *(--dst) = svc_ctx->xpsr & 0xFFFFFE00; |
| 149 | /* ReturnAddress = resume to the privileged handler code, but execute it |
| 150 | * unprivileged. |
| 151 | */ |
| 152 | *(--dst) = svc_ctx->ra; |
| 153 | /* LR = start address */ |
| 154 | *(--dst) = (int32_t)unpriv_handler; |
| 155 | |
| 156 | /* R12, R0-R3 unused arguments */ |
| 157 | for (i = 0; i < 5; ++i) { |
| 158 | *(--dst) = 0; |
| 159 | } |
| 160 | |
| 161 | return dst; |
| 162 | } |
| 163 | |
| 164 | static void restore_caller_ctx(const struct tfm_state_context_t *svc_ctx, |
| 165 | struct tfm_state_context_t *target_ctx) |
| 166 | { |
| 167 | /* ReturnAddress = resume veneer after second SVC */ |
| 168 | target_ctx->ra = svc_ctx->ra; |
| 169 | |
| 170 | /* R0 = function return value */ |
| 171 | target_ctx->r0 = svc_ctx->r0; |
| 172 | |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * \brief Check whether the iovec parameters are valid, and the memory ranges |
| 178 | * are in the possession of the calling partition. |
| 179 | * |
David Hu | 5da82de | 2020-12-02 16:41:30 +0800 | [diff] [blame] | 180 | * \param[in] desc_ptr The secure function request descriptor |
| 181 | * \param[out] iovec_ptr The local buffer to store iovec arguments |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 182 | * |
| 183 | * \return Return /ref TFM_SUCCESS if the iovec parameters are valid, error code |
| 184 | * otherwise as in /ref tfm_status_e |
| 185 | */ |
| 186 | static enum tfm_status_e tfm_core_check_sfn_parameters( |
David Hu | 5da82de | 2020-12-02 16:41:30 +0800 | [diff] [blame] | 187 | const struct tfm_sfn_req_s *desc_ptr, |
| 188 | struct iovec_params_t *iovec_ptr) |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 189 | { |
| 190 | struct psa_invec *in_vec = (psa_invec *)desc_ptr->args[0]; |
| 191 | size_t in_len; |
| 192 | struct psa_outvec *out_vec = (psa_outvec *)desc_ptr->args[2]; |
| 193 | size_t out_len; |
| 194 | uint32_t i; |
Kevin Peng | 1f6f5af | 2020-08-27 15:07:58 +0800 | [diff] [blame] | 195 | uint32_t privileged_mode = TFM_PARTITION_UNPRIVILEGED_MODE; |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 196 | |
| 197 | if ((desc_ptr->args[1] < 0) || (desc_ptr->args[3] < 0)) { |
| 198 | return TFM_ERROR_INVALID_PARAMETER; |
| 199 | } |
| 200 | |
| 201 | in_len = (size_t)(desc_ptr->args[1]); |
| 202 | out_len = (size_t)(desc_ptr->args[3]); |
| 203 | |
Kevin Peng | 1f6f5af | 2020-08-27 15:07:58 +0800 | [diff] [blame] | 204 | /* |
| 205 | * Get caller's privileged mode: |
| 206 | * The privileged mode of NS Secure Service caller will be decided by the |
| 207 | * tfm_core_has_xxx_access_to_region functions. |
| 208 | * Secure caller can be only privileged mode because the whole SPE is |
| 209 | * running under privileged mode |
| 210 | */ |
| 211 | if (!desc_ptr->ns_caller) { |
| 212 | privileged_mode = TFM_PARTITION_PRIVILEGED_MODE; |
| 213 | } |
| 214 | |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 215 | /* The number of vectors are within range. Extra checks to avoid overflow */ |
| 216 | if ((in_len > PSA_MAX_IOVEC) || (out_len > PSA_MAX_IOVEC) || |
| 217 | (in_len + out_len > PSA_MAX_IOVEC)) { |
| 218 | return TFM_ERROR_INVALID_PARAMETER; |
| 219 | } |
| 220 | |
| 221 | /* Check whether the caller partition has at write access to the iovec |
| 222 | * structures themselves. Use the TT instruction for this. |
| 223 | */ |
| 224 | if (in_len > 0) { |
| 225 | if ((in_vec == NULL) || |
| 226 | (tfm_core_has_write_access_to_region(in_vec, |
| 227 | sizeof(psa_invec)*in_len, desc_ptr->ns_caller, |
Kevin Peng | 1f6f5af | 2020-08-27 15:07:58 +0800 | [diff] [blame] | 228 | privileged_mode) != TFM_SUCCESS)) { |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 229 | return TFM_ERROR_INVALID_PARAMETER; |
| 230 | } |
| 231 | } else { |
| 232 | if (in_vec != NULL) { |
| 233 | return TFM_ERROR_INVALID_PARAMETER; |
| 234 | } |
| 235 | } |
| 236 | if (out_len > 0) { |
| 237 | if ((out_vec == NULL) || |
| 238 | (tfm_core_has_write_access_to_region(out_vec, |
| 239 | sizeof(psa_outvec)*out_len, desc_ptr->ns_caller, |
Kevin Peng | 1f6f5af | 2020-08-27 15:07:58 +0800 | [diff] [blame] | 240 | privileged_mode) != TFM_SUCCESS)) { |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 241 | return TFM_ERROR_INVALID_PARAMETER; |
| 242 | } |
| 243 | } else { |
| 244 | if (out_vec != NULL) { |
| 245 | return TFM_ERROR_INVALID_PARAMETER; |
| 246 | } |
| 247 | } |
| 248 | |
David Hu | 5da82de | 2020-12-02 16:41:30 +0800 | [diff] [blame] | 249 | /* Copy iovec parameters into a local buffer before validating them */ |
| 250 | iovec_ptr->in_len = in_len; |
| 251 | for (i = 0; i < in_len; ++i) { |
| 252 | iovec_ptr->in_vec[i].base = in_vec[i].base; |
| 253 | iovec_ptr->in_vec[i].len = in_vec[i].len; |
| 254 | } |
| 255 | iovec_ptr->out_len = out_len; |
| 256 | for (i = 0; i < out_len; ++i) { |
| 257 | iovec_ptr->out_vec[i].base = out_vec[i].base; |
| 258 | iovec_ptr->out_vec[i].len = out_vec[i].len; |
| 259 | } |
| 260 | iovec_ptr->orig_outvec = out_vec; |
| 261 | |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 262 | /* Check whether the caller partition has access to the data inside the |
| 263 | * iovecs |
| 264 | */ |
| 265 | for (i = 0; i < in_len; ++i) { |
David Hu | 5da82de | 2020-12-02 16:41:30 +0800 | [diff] [blame] | 266 | if (iovec_ptr->in_vec[i].len > 0) { |
| 267 | if ((iovec_ptr->in_vec[i].base == NULL) || |
| 268 | (tfm_core_has_read_access_to_region(iovec_ptr->in_vec[i].base, |
| 269 | iovec_ptr->in_vec[i].len, desc_ptr->ns_caller, |
Kevin Peng | 1f6f5af | 2020-08-27 15:07:58 +0800 | [diff] [blame] | 270 | privileged_mode) != TFM_SUCCESS)) { |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 271 | return TFM_ERROR_INVALID_PARAMETER; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | for (i = 0; i < out_len; ++i) { |
David Hu | 5da82de | 2020-12-02 16:41:30 +0800 | [diff] [blame] | 276 | if (iovec_ptr->out_vec[i].len > 0) { |
| 277 | if ((iovec_ptr->out_vec[i].base == NULL) || |
| 278 | (tfm_core_has_write_access_to_region(iovec_ptr->out_vec[i].base, |
| 279 | iovec_ptr->out_vec[i].len, desc_ptr->ns_caller, |
Kevin Peng | 1f6f5af | 2020-08-27 15:07:58 +0800 | [diff] [blame] | 280 | privileged_mode) != TFM_SUCCESS)) { |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 281 | return TFM_ERROR_INVALID_PARAMETER; |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | return TFM_SUCCESS; |
| 287 | } |
| 288 | |
| 289 | static void tfm_copy_iovec_parameters(struct iovec_args_t *target, |
| 290 | const struct iovec_args_t *source) |
| 291 | { |
| 292 | size_t i; |
| 293 | |
| 294 | /* The vectors have been sanity checked already, and since then the |
| 295 | * interrupts have been kept disabled. So we can be sure that the |
| 296 | * vectors haven't been tampered with since the check. So it is safe to pass |
| 297 | * it to the called partition. |
| 298 | */ |
| 299 | |
| 300 | target->in_len = source->in_len; |
| 301 | for (i = 0; i < source->in_len; ++i) { |
| 302 | target->in_vec[i].base = source->in_vec[i].base; |
| 303 | target->in_vec[i].len = source->in_vec[i].len; |
| 304 | } |
| 305 | target->out_len = source->out_len; |
| 306 | for (i = 0; i < source->out_len; ++i) { |
| 307 | target->out_vec[i].base = source->out_vec[i].base; |
| 308 | target->out_vec[i].len = source->out_vec[i].len; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | static void tfm_clear_iovec_parameters(struct iovec_args_t *args) |
| 313 | { |
| 314 | int i; |
| 315 | |
| 316 | args->in_len = 0; |
| 317 | for (i = 0; i < PSA_MAX_IOVEC; ++i) { |
| 318 | args->in_vec[i].base = NULL; |
| 319 | args->in_vec[i].len = 0; |
| 320 | } |
| 321 | args->out_len = 0; |
| 322 | for (i = 0; i < PSA_MAX_IOVEC; ++i) { |
| 323 | args->out_vec[i].base = NULL; |
| 324 | args->out_vec[i].len = 0; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * \brief Check whether the partitions for the secure function call are in a |
| 330 | * proper state. |
| 331 | * |
| 332 | * \param[in] curr_partition_state State of the partition to be called |
| 333 | * \param[in] caller_partition_state State of the caller partition |
| 334 | * |
| 335 | * \return \ref TFM_SUCCESS if the check passes, error otherwise. |
| 336 | */ |
| 337 | static enum tfm_status_e check_partition_state(uint32_t curr_partition_state, |
| 338 | uint32_t caller_partition_state) |
| 339 | { |
| 340 | if (caller_partition_state != SPM_PARTITION_STATE_RUNNING) { |
| 341 | /* Calling partition from non-running state (e.g. during handling IRQ) |
| 342 | * is not allowed. |
| 343 | */ |
| 344 | return TFM_ERROR_INVALID_EXC_MODE; |
| 345 | } |
| 346 | |
| 347 | if (curr_partition_state == SPM_PARTITION_STATE_RUNNING || |
| 348 | curr_partition_state == SPM_PARTITION_STATE_HANDLING_IRQ || |
| 349 | curr_partition_state == SPM_PARTITION_STATE_SUSPENDED || |
| 350 | curr_partition_state == SPM_PARTITION_STATE_BLOCKED) { |
| 351 | /* Active partitions cannot be called! */ |
| 352 | return TFM_ERROR_PARTITION_NON_REENTRANT; |
| 353 | } else if (curr_partition_state != SPM_PARTITION_STATE_IDLE) { |
| 354 | /* The partition to be called is not in a proper state */ |
| 355 | return TFM_SECURE_LOCK_FAILED; |
| 356 | } |
| 357 | return TFM_SUCCESS; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * \brief Check whether the partitions for the secure function call of irq are |
| 362 | * in a proper state. |
| 363 | * |
| 364 | * \param[in] called_partition_state State of the partition to be called |
| 365 | * |
| 366 | * \return \ref TFM_SUCCESS if the check passes, error otherwise. |
| 367 | */ |
| 368 | static enum tfm_status_e check_irq_partition_state( |
| 369 | uint32_t called_partition_state) |
| 370 | { |
| 371 | if (called_partition_state == SPM_PARTITION_STATE_IDLE || |
| 372 | called_partition_state == SPM_PARTITION_STATE_RUNNING || |
| 373 | called_partition_state == SPM_PARTITION_STATE_HANDLING_IRQ || |
| 374 | called_partition_state == SPM_PARTITION_STATE_SUSPENDED || |
| 375 | called_partition_state == SPM_PARTITION_STATE_BLOCKED) { |
| 376 | return TFM_SUCCESS; |
| 377 | } |
| 378 | return TFM_SECURE_LOCK_FAILED; |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * \brief Calculate the address where the iovec parameters are to be saved for |
| 383 | * the called partition. |
| 384 | * |
| 385 | * \param[in] partition_idx The index of the partition to be called. |
| 386 | * |
| 387 | * \return The address where the iovec parameters should be saved. |
| 388 | */ |
| 389 | static struct iovec_args_t *get_iovec_args_stack_address(uint32_t partition_idx) |
| 390 | { |
| 391 | /* Save the iovecs on the common stack. */ |
TTornblom | 99f0be2 | 2019-12-17 16:22:38 +0100 | [diff] [blame] | 392 | return ®ION_NAME(Image$$, TFM_SECURE_STACK, $$ZI$$Limit)[-1]; |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 393 | } |
| 394 | |
Mingyang Sun | da30f1e | 2020-07-13 17:20:32 +0800 | [diff] [blame] | 395 | /** |
| 396 | * \brief Returns the index of the partition with the given partition ID. |
| 397 | * |
| 398 | * \param[in] partition_id Partition id |
| 399 | * |
| 400 | * \return the partition idx if partition_id is valid, |
| 401 | * \ref SPM_INVALID_PARTITION_IDX othervise |
| 402 | */ |
| 403 | static uint32_t get_partition_idx(uint32_t partition_id) |
| 404 | { |
| 405 | uint32_t i; |
| 406 | |
| 407 | if (partition_id == INVALID_PARTITION_ID) { |
| 408 | return SPM_INVALID_PARTITION_IDX; |
| 409 | } |
| 410 | |
| 411 | for (i = 0; i < g_spm_partition_db.partition_count; ++i) { |
| 412 | if (g_spm_partition_db.partitions[i].static_data->partition_id == |
| 413 | partition_id) { |
| 414 | return i; |
| 415 | } |
| 416 | } |
| 417 | return SPM_INVALID_PARTITION_IDX; |
| 418 | } |
| 419 | |
| 420 | /** |
David Hu | 5da82de | 2020-12-02 16:41:30 +0800 | [diff] [blame] | 421 | * \brief Set the iovec parameters for the partition |
| 422 | * |
| 423 | * \param[in] partition_idx Partition index |
| 424 | * \param[in] iovec_ptr The arguments of the secure function |
| 425 | * |
| 426 | * \return Error code \ref spm_err_t |
| 427 | * |
| 428 | * \note This function doesn't check if partition_idx is valid. |
| 429 | * \note This function assumes that the iovecs that are passed in iovec_ptr are |
| 430 | * valid, and does no sanity check on them at all. |
| 431 | */ |
| 432 | static enum spm_err_t tfm_spm_partition_set_iovec(uint32_t partition_idx, |
| 433 | const struct iovec_params_t *iovec_ptr) |
| 434 | { |
| 435 | struct spm_partition_runtime_data_t *runtime_data = |
| 436 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 437 | size_t i; |
| 438 | |
| 439 | if ((iovec_ptr->in_len < 0) || (iovec_ptr->out_len < 0)) { |
| 440 | return SPM_ERR_INVALID_PARAMETER; |
| 441 | } |
| 442 | |
| 443 | runtime_data->iovec_args.in_len = iovec_ptr->in_len; |
| 444 | for (i = 0U; i < runtime_data->iovec_args.in_len; ++i) { |
| 445 | runtime_data->iovec_args.in_vec[i].base = iovec_ptr->in_vec[i].base; |
| 446 | runtime_data->iovec_args.in_vec[i].len = iovec_ptr->in_vec[i].len; |
| 447 | } |
| 448 | runtime_data->iovec_args.out_len = iovec_ptr->out_len; |
| 449 | for (i = 0U; i < runtime_data->iovec_args.out_len; ++i) { |
| 450 | runtime_data->iovec_args.out_vec[i].base = iovec_ptr->out_vec[i].base; |
| 451 | runtime_data->iovec_args.out_vec[i].len = iovec_ptr->out_vec[i].len; |
| 452 | } |
| 453 | runtime_data->orig_outvec = iovec_ptr->orig_outvec; |
| 454 | |
| 455 | return SPM_ERR_OK; |
| 456 | } |
| 457 | |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 458 | static enum tfm_status_e tfm_start_partition( |
David Hu | 5da82de | 2020-12-02 16:41:30 +0800 | [diff] [blame] | 459 | const struct tfm_sfn_req_s *desc_ptr, |
| 460 | const struct iovec_params_t *iovec_ptr, |
| 461 | uint32_t excReturn) |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 462 | { |
Xinyu Zhang | 3a45324 | 2021-04-16 17:57:09 +0800 | [diff] [blame] | 463 | register uint32_t partition_idx; |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 464 | enum tfm_status_e res; |
| 465 | uint32_t caller_partition_idx = desc_ptr->caller_part_idx; |
| 466 | const struct spm_partition_runtime_data_t *curr_part_data; |
| 467 | const struct spm_partition_runtime_data_t *caller_part_data; |
| 468 | uint32_t caller_flags; |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 469 | uint32_t psp; |
| 470 | uint32_t partition_psp, partition_psplim; |
| 471 | uint32_t partition_state; |
| 472 | uint32_t caller_partition_state; |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 473 | struct tfm_state_context_t *svc_ctx; |
| 474 | uint32_t caller_partition_id; |
| 475 | int32_t client_id; |
| 476 | struct iovec_args_t *iovec_args; |
| 477 | |
| 478 | psp = __get_PSP(); |
| 479 | svc_ctx = (struct tfm_state_context_t *)psp; |
| 480 | caller_flags = tfm_spm_partition_get_flags(caller_partition_idx); |
| 481 | |
| 482 | /* Check partition state consistency */ |
| 483 | if (((caller_flags & SPM_PART_FLAG_APP_ROT) != 0) |
| 484 | != (!desc_ptr->ns_caller)) { |
| 485 | /* Partition state inconsistency detected */ |
| 486 | return TFM_SECURE_LOCK_FAILED; |
| 487 | } |
| 488 | |
| 489 | partition_idx = get_partition_idx(desc_ptr->sp_id); |
| 490 | |
| 491 | curr_part_data = tfm_spm_partition_get_runtime_data(partition_idx); |
| 492 | caller_part_data = tfm_spm_partition_get_runtime_data(caller_partition_idx); |
| 493 | partition_state = curr_part_data->partition_state; |
| 494 | caller_partition_state = caller_part_data->partition_state; |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 495 | caller_partition_id = tfm_spm_partition_get_partition_id( |
| 496 | caller_partition_idx); |
| 497 | |
| 498 | if (!tfm_secure_api_initializing) { |
| 499 | res = check_partition_state(partition_state, caller_partition_state); |
| 500 | if (res != TFM_SUCCESS) { |
| 501 | return res; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | /* Prepare switch to shared secure partition stack */ |
| 506 | /* In case the call is coming from the non-secure world, we save the iovecs |
Soby Mathew | 960521a | 2020-09-29 12:48:50 +0100 | [diff] [blame] | 507 | * on the stop of the stack. Also the stack seal is present below this region. |
| 508 | * So the memory area, that can actually be used as stack by the partitions |
| 509 | * starts at a lower address. |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 510 | */ |
Soby Mathew | 960521a | 2020-09-29 12:48:50 +0100 | [diff] [blame] | 511 | partition_psp = (uint32_t) tfm_secure_stack_seal; |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 512 | partition_psplim = |
| 513 | (uint32_t)®ION_NAME(Image$$, TFM_SECURE_STACK, $$ZI$$Base); |
| 514 | |
| 515 | /* Store the context for the partition call */ |
| 516 | tfm_spm_partition_set_caller_partition_idx(partition_idx, |
| 517 | caller_partition_idx); |
| 518 | tfm_spm_partition_store_context(caller_partition_idx, psp, excReturn); |
| 519 | |
| 520 | if ((caller_flags & SPM_PART_FLAG_APP_ROT)) { |
| 521 | tfm_spm_partition_set_caller_client_id(partition_idx, |
| 522 | caller_partition_id); |
| 523 | } else { |
| 524 | client_id = tfm_nspm_get_current_client_id(); |
| 525 | if (client_id >= 0) { |
| 526 | return TFM_SECURE_LOCK_FAILED; |
| 527 | } |
| 528 | tfm_spm_partition_set_caller_client_id(partition_idx, client_id); |
| 529 | } |
| 530 | |
| 531 | /* In level one, only switch context and return from exception if in |
| 532 | * handler mode |
| 533 | */ |
| 534 | if ((desc_ptr->ns_caller) || (tfm_secure_api_initializing)) { |
David Hu | 5da82de | 2020-12-02 16:41:30 +0800 | [diff] [blame] | 535 | if (tfm_spm_partition_set_iovec(partition_idx, iovec_ptr) != |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 536 | SPM_ERR_OK) { |
| 537 | return TFM_ERROR_GENERIC; |
| 538 | } |
| 539 | iovec_args = get_iovec_args_stack_address(partition_idx); |
| 540 | tfm_copy_iovec_parameters(iovec_args, &(curr_part_data->iovec_args)); |
| 541 | |
| 542 | /* Prepare the partition context, update stack ptr */ |
| 543 | psp = (uint32_t)prepare_partition_iovec_ctx(svc_ctx, desc_ptr, |
| 544 | iovec_args, |
| 545 | (uint32_t *)partition_psp); |
| 546 | __set_PSP(psp); |
| 547 | tfm_arch_set_psplim(partition_psplim); |
| 548 | } |
| 549 | |
| 550 | tfm_spm_partition_set_state(caller_partition_idx, |
| 551 | SPM_PARTITION_STATE_BLOCKED); |
| 552 | tfm_spm_partition_set_state(partition_idx, SPM_PARTITION_STATE_RUNNING); |
| 553 | tfm_secure_lock++; |
| 554 | |
| 555 | return TFM_SUCCESS; |
| 556 | } |
| 557 | |
| 558 | static enum tfm_status_e tfm_start_partition_for_irq_handling( |
| 559 | uint32_t excReturn, |
| 560 | struct tfm_state_context_t *svc_ctx) |
| 561 | { |
| 562 | uint32_t handler_partition_id = svc_ctx->r0; |
| 563 | sfn_t unpriv_handler = (sfn_t)svc_ctx->r1; |
| 564 | uint32_t irq_signal = svc_ctx->r2; |
Kevin Peng | a20b5af | 2021-01-11 11:20:52 +0800 | [diff] [blame] | 565 | uint32_t irq_line = svc_ctx->r3; |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 566 | enum tfm_status_e res; |
| 567 | uint32_t psp = __get_PSP(); |
| 568 | uint32_t handler_partition_psp; |
| 569 | uint32_t handler_partition_state; |
| 570 | uint32_t interrupted_partition_idx = |
| 571 | tfm_spm_partition_get_running_partition_idx(); |
| 572 | const struct spm_partition_runtime_data_t *handler_part_data; |
| 573 | uint32_t handler_partition_idx; |
| 574 | |
| 575 | handler_partition_idx = get_partition_idx(handler_partition_id); |
| 576 | handler_part_data = tfm_spm_partition_get_runtime_data( |
| 577 | handler_partition_idx); |
| 578 | handler_partition_state = handler_part_data->partition_state; |
| 579 | |
| 580 | res = check_irq_partition_state(handler_partition_state); |
| 581 | if (res != TFM_SUCCESS) { |
| 582 | return res; |
| 583 | } |
| 584 | |
| 585 | /* set mask for the partition */ |
| 586 | tfm_spm_partition_set_signal_mask( |
| 587 | handler_partition_idx, |
| 588 | handler_part_data->signal_mask | irq_signal); |
| 589 | |
| 590 | tfm_spm_hal_disable_irq(irq_line); |
| 591 | |
| 592 | /* save the current context of the interrupted partition */ |
| 593 | tfm_spm_partition_push_interrupted_ctx(interrupted_partition_idx); |
| 594 | |
| 595 | handler_partition_psp = psp; |
| 596 | |
| 597 | /* save the current context of the handler partition */ |
| 598 | tfm_spm_partition_push_handler_ctx(handler_partition_idx); |
| 599 | |
| 600 | /* Store caller for the partition */ |
| 601 | tfm_spm_partition_set_caller_partition_idx(handler_partition_idx, |
| 602 | interrupted_partition_idx); |
| 603 | |
| 604 | psp = (uint32_t)prepare_partition_irq_ctx(svc_ctx, unpriv_handler, |
| 605 | (int32_t *)handler_partition_psp); |
| 606 | __set_PSP(psp); |
| 607 | |
| 608 | tfm_spm_partition_set_state(interrupted_partition_idx, |
| 609 | SPM_PARTITION_STATE_SUSPENDED); |
| 610 | tfm_spm_partition_set_state(handler_partition_idx, |
| 611 | SPM_PARTITION_STATE_HANDLING_IRQ); |
| 612 | |
| 613 | return TFM_SUCCESS; |
| 614 | } |
| 615 | |
| 616 | static enum tfm_status_e tfm_return_from_partition(uint32_t *excReturn) |
| 617 | { |
| 618 | uint32_t current_partition_idx = |
| 619 | tfm_spm_partition_get_running_partition_idx(); |
| 620 | const struct spm_partition_runtime_data_t *curr_part_data, *ret_part_data; |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 621 | uint32_t return_partition_idx; |
| 622 | uint32_t return_partition_flags; |
| 623 | uint32_t psp = __get_PSP(); |
| 624 | size_t i; |
| 625 | struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)psp; |
| 626 | struct iovec_args_t *iovec_args; |
| 627 | |
| 628 | if (current_partition_idx == SPM_INVALID_PARTITION_IDX) { |
| 629 | return TFM_SECURE_UNLOCK_FAILED; |
| 630 | } |
| 631 | |
| 632 | curr_part_data = tfm_spm_partition_get_runtime_data(current_partition_idx); |
| 633 | return_partition_idx = curr_part_data->caller_partition_idx; |
| 634 | |
| 635 | if (return_partition_idx == SPM_INVALID_PARTITION_IDX) { |
| 636 | return TFM_SECURE_UNLOCK_FAILED; |
| 637 | } |
| 638 | |
| 639 | ret_part_data = tfm_spm_partition_get_runtime_data(return_partition_idx); |
| 640 | |
| 641 | return_partition_flags = tfm_spm_partition_get_flags(return_partition_idx); |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 642 | |
| 643 | tfm_secure_lock--; |
| 644 | |
| 645 | if (!(return_partition_flags & SPM_PART_FLAG_APP_ROT) || |
| 646 | (tfm_secure_api_initializing)) { |
| 647 | /* In TFM level 1 context restore is only done when |
| 648 | * returning to NS or after initialization |
| 649 | */ |
| 650 | /* Restore caller context */ |
| 651 | restore_caller_ctx(svc_ctx, |
| 652 | (struct tfm_state_context_t *)ret_part_data->stack_ptr); |
| 653 | *excReturn = ret_part_data->lr; |
| 654 | __set_PSP(ret_part_data->stack_ptr); |
TTornblom | 99f0be2 | 2019-12-17 16:22:38 +0100 | [diff] [blame] | 655 | REGION_DECLARE_T(Image$$, ARM_LIB_STACK, $$ZI$$Base, uint32_t)[]; |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 656 | uint32_t psp_stack_bottom = |
| 657 | (uint32_t)REGION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Base); |
| 658 | tfm_arch_set_psplim(psp_stack_bottom); |
| 659 | |
TTornblom | 99f0be2 | 2019-12-17 16:22:38 +0100 | [diff] [blame] | 660 | iovec_args = ®ION_NAME(Image$$, TFM_SECURE_STACK, $$ZI$$Limit)[-1]; |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 661 | |
| 662 | for (i = 0; i < curr_part_data->iovec_args.out_len; ++i) { |
| 663 | curr_part_data->orig_outvec[i].len = iovec_args->out_vec[i].len; |
| 664 | } |
| 665 | tfm_clear_iovec_parameters(iovec_args); |
| 666 | } |
| 667 | |
| 668 | tfm_spm_partition_cleanup_context(current_partition_idx); |
| 669 | |
| 670 | tfm_spm_partition_set_state(current_partition_idx, |
| 671 | SPM_PARTITION_STATE_IDLE); |
| 672 | tfm_spm_partition_set_state(return_partition_idx, |
| 673 | SPM_PARTITION_STATE_RUNNING); |
| 674 | |
| 675 | return TFM_SUCCESS; |
| 676 | } |
| 677 | |
| 678 | static enum tfm_status_e tfm_return_from_partition_irq_handling( |
| 679 | uint32_t *excReturn) |
| 680 | { |
| 681 | uint32_t handler_partition_idx = |
| 682 | tfm_spm_partition_get_running_partition_idx(); |
| 683 | const struct spm_partition_runtime_data_t *handler_part_data; |
| 684 | uint32_t interrupted_partition_idx; |
| 685 | uint32_t psp = __get_PSP(); |
| 686 | struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)psp; |
| 687 | |
| 688 | if (handler_partition_idx == SPM_INVALID_PARTITION_IDX) { |
| 689 | return TFM_SECURE_UNLOCK_FAILED; |
| 690 | } |
| 691 | |
| 692 | handler_part_data = tfm_spm_partition_get_runtime_data( |
| 693 | handler_partition_idx); |
| 694 | interrupted_partition_idx = handler_part_data->caller_partition_idx; |
| 695 | |
| 696 | if (interrupted_partition_idx == SPM_INVALID_PARTITION_IDX) { |
| 697 | return TFM_SECURE_UNLOCK_FAILED; |
| 698 | } |
| 699 | |
| 700 | /* For level 1, modify PSP, so that the SVC stack frame disappears, |
| 701 | * and return to the privileged handler using the stack frame still on the |
| 702 | * MSP stack. |
| 703 | */ |
| 704 | *excReturn = svc_ctx->ra; |
| 705 | psp += sizeof(struct tfm_state_context_t); |
| 706 | |
| 707 | tfm_spm_partition_pop_handler_ctx(handler_partition_idx); |
| 708 | tfm_spm_partition_pop_interrupted_ctx(interrupted_partition_idx); |
| 709 | |
| 710 | __set_PSP(psp); |
| 711 | |
| 712 | return TFM_SUCCESS; |
| 713 | } |
| 714 | |
| 715 | static enum tfm_status_e tfm_check_sfn_req_integrity( |
| 716 | const struct tfm_sfn_req_s *desc_ptr) |
| 717 | { |
| 718 | if ((desc_ptr == NULL) || |
| 719 | (desc_ptr->sp_id == 0) || |
| 720 | (desc_ptr->sfn == NULL)) { |
| 721 | /* invalid parameter */ |
| 722 | return TFM_ERROR_INVALID_PARAMETER; |
| 723 | } |
| 724 | return TFM_SUCCESS; |
| 725 | } |
| 726 | |
| 727 | static enum tfm_status_e tfm_core_check_sfn_req_rules( |
| 728 | const struct tfm_sfn_req_s *desc_ptr) |
| 729 | { |
| 730 | /* Check partition idx validity */ |
| 731 | if (desc_ptr->caller_part_idx == SPM_INVALID_PARTITION_IDX) { |
| 732 | return TFM_ERROR_NO_ACTIVE_PARTITION; |
| 733 | } |
| 734 | |
| 735 | if ((desc_ptr->ns_caller) && (tfm_secure_lock != 0)) { |
| 736 | /* Secure domain is already locked! |
| 737 | * This should only happen if caller is secure partition! |
| 738 | */ |
| 739 | /* This scenario is a potential security breach. |
| 740 | * Error is handled in caller. |
| 741 | */ |
| 742 | return TFM_ERROR_SECURE_DOMAIN_LOCKED; |
| 743 | } |
| 744 | |
| 745 | if (tfm_secure_api_initializing) { |
| 746 | int32_t id = |
| 747 | tfm_spm_partition_get_partition_id(desc_ptr->caller_part_idx); |
| 748 | |
| 749 | if ((id != TFM_SP_CORE_ID) || (tfm_secure_lock != 0)) { |
| 750 | /* Invalid request during system initialization */ |
Antonio de Angelis | fa5d460 | 2021-06-09 16:11:11 +0100 | [diff] [blame] | 751 | SPMLOG_ERRMSG("Invalid service request during initialization!\r\n"); |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 752 | return TFM_ERROR_NOT_INITIALIZED; |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | return TFM_SUCCESS; |
| 757 | } |
| 758 | |
Antonio de Angelis | 5b64bb7 | 2021-04-27 08:37:14 +0100 | [diff] [blame] | 759 | uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx) |
| 760 | { |
| 761 | return g_spm_partition_db.partitions[partition_idx].static_data-> |
| 762 | partition_flags; |
| 763 | } |
| 764 | |
Mingyang Sun | da30f1e | 2020-07-13 17:20:32 +0800 | [diff] [blame] | 765 | uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx) |
| 766 | { |
| 767 | return g_spm_partition_db.partitions[partition_idx].static_data-> |
| 768 | partition_id; |
| 769 | } |
| 770 | |
| 771 | uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags) |
| 772 | { |
| 773 | if (partition_flags & SPM_PART_FLAG_PSA_ROT) { |
| 774 | return TFM_PARTITION_PRIVILEGED_MODE; |
| 775 | } else { |
| 776 | return TFM_PARTITION_UNPRIVILEGED_MODE; |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | bool tfm_is_partition_privileged(uint32_t partition_idx) |
| 781 | { |
| 782 | uint32_t flags = tfm_spm_partition_get_flags(partition_idx); |
| 783 | |
| 784 | return tfm_spm_partition_get_privileged_mode(flags) == |
| 785 | TFM_PARTITION_PRIVILEGED_MODE; |
| 786 | } |
| 787 | |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 788 | void tfm_spm_secure_api_init_done(void) |
| 789 | { |
| 790 | tfm_secure_api_initializing = 0; |
| 791 | } |
| 792 | |
Antonio de Angelis | 00667fc | 2021-05-05 22:40:39 +0100 | [diff] [blame] | 793 | static enum tfm_status_e tfm_spm_sfn_request_handler( |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 794 | struct tfm_sfn_req_s *desc_ptr, uint32_t excReturn) |
| 795 | { |
| 796 | enum tfm_status_e res; |
shejia01 | 01a497c | 2021-04-21 10:04:07 +0800 | [diff] [blame] | 797 | struct iovec_params_t iovecs = {0}; |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 798 | |
| 799 | res = tfm_check_sfn_req_integrity(desc_ptr); |
| 800 | if (res != TFM_SUCCESS) { |
Antonio de Angelis | fa5d460 | 2021-06-09 16:11:11 +0100 | [diff] [blame] | 801 | SPMLOG_ERRMSG("Invalid service request!\r\n"); |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 802 | tfm_secure_api_error_handler(); |
| 803 | } |
| 804 | |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 805 | desc_ptr->caller_part_idx = tfm_spm_partition_get_running_partition_idx(); |
| 806 | |
David Hu | 5da82de | 2020-12-02 16:41:30 +0800 | [diff] [blame] | 807 | res = tfm_core_check_sfn_parameters(desc_ptr, &iovecs); |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 808 | if (res != TFM_SUCCESS) { |
| 809 | /* The sanity check of iovecs failed. */ |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 810 | tfm_secure_api_error_handler(); |
| 811 | } |
| 812 | |
| 813 | res = tfm_core_check_sfn_req_rules(desc_ptr); |
| 814 | if (res != TFM_SUCCESS) { |
| 815 | /* FixMe: error compartmentalization TBD */ |
| 816 | tfm_spm_partition_set_state( |
| 817 | desc_ptr->caller_part_idx, SPM_PARTITION_STATE_CLOSED); |
Antonio de Angelis | fa5d460 | 2021-06-09 16:11:11 +0100 | [diff] [blame] | 818 | SPMLOG_ERRMSG("Unauthorized service request!\r\n"); |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 819 | tfm_secure_api_error_handler(); |
| 820 | } |
| 821 | |
David Hu | 5da82de | 2020-12-02 16:41:30 +0800 | [diff] [blame] | 822 | res = tfm_start_partition(desc_ptr, &iovecs, excReturn); |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 823 | if (res != TFM_SUCCESS) { |
| 824 | /* FixMe: consider possible fault scenarios */ |
Antonio de Angelis | fa5d460 | 2021-06-09 16:11:11 +0100 | [diff] [blame] | 825 | SPMLOG_ERRMSG("Failed to process service request!\r\n"); |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 826 | tfm_secure_api_error_handler(); |
| 827 | } |
| 828 | |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 829 | return res; |
| 830 | } |
| 831 | |
| 832 | int32_t tfm_spm_sfn_request_thread_mode(struct tfm_sfn_req_s *desc_ptr) |
| 833 | { |
| 834 | enum tfm_status_e res; |
| 835 | int32_t *args; |
| 836 | int32_t retVal; |
David Hu | 5da82de | 2020-12-02 16:41:30 +0800 | [diff] [blame] | 837 | struct iovec_params_t iovecs; |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 838 | |
David Hu | 5da82de | 2020-12-02 16:41:30 +0800 | [diff] [blame] | 839 | res = tfm_core_check_sfn_parameters(desc_ptr, &iovecs); |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 840 | if (res != TFM_SUCCESS) { |
| 841 | /* The sanity check of iovecs failed. */ |
| 842 | return (int32_t)res; |
| 843 | } |
| 844 | |
| 845 | /* No excReturn value is needed as no exception handling is used */ |
| 846 | res = tfm_spm_sfn_request_handler(desc_ptr, 0); |
| 847 | |
| 848 | if (res != TFM_SUCCESS) { |
| 849 | tfm_secure_api_error_handler(); |
| 850 | } |
| 851 | |
| 852 | /* Secure partition to secure partition call in TFM level 1 */ |
| 853 | args = desc_ptr->args; |
| 854 | retVal = desc_ptr->sfn(args[0], args[1], args[2], args[3]); |
| 855 | |
| 856 | /* return handler should restore original exc_return value... */ |
| 857 | res = tfm_return_from_partition(NULL); |
| 858 | if (res == TFM_SUCCESS) { |
| 859 | /* If unlock successful, pass SS return value to caller */ |
| 860 | return retVal; |
| 861 | } else { |
| 862 | /* Unlock errors indicate ctx database corruption or unknown |
| 863 | * anomalies. Halt execution |
| 864 | */ |
Antonio de Angelis | fa5d460 | 2021-06-09 16:11:11 +0100 | [diff] [blame] | 865 | SPMLOG_ERRMSG("Secure API error during unlock!\r\n"); |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 866 | tfm_secure_api_error_handler(); |
| 867 | } |
| 868 | return (int32_t)res; |
| 869 | } |
| 870 | |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 871 | int32_t tfm_spm_check_buffer_access(uint32_t partition_idx, |
| 872 | void *start_addr, |
| 873 | size_t len, |
| 874 | uint32_t alignment) |
| 875 | { |
| 876 | uintptr_t start_addr_value = (uintptr_t)start_addr; |
| 877 | uintptr_t end_addr_value = (uintptr_t)start_addr + len; |
| 878 | uintptr_t alignment_mask; |
| 879 | |
| 880 | alignment_mask = (((uintptr_t)1) << alignment) - 1; |
| 881 | |
Antonio de Angelis | f8564cb | 2021-04-28 13:52:13 +0100 | [diff] [blame] | 882 | /* Check pointer alignment and protect against overflow and zero len */ |
| 883 | if (!(start_addr_value & alignment_mask) && |
| 884 | (end_addr_value > start_addr_value)) { |
| 885 | /* Check that the range is in S_DATA */ |
| 886 | if ((start_addr_value >= S_DATA_START) && |
| 887 | (end_addr_value <= (S_DATA_START + S_DATA_SIZE))) { |
| 888 | return TFM_SUCCESS; |
| 889 | } else { |
| 890 | return TFM_ERROR_NOT_IN_RANGE; |
| 891 | } |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 892 | } |
| 893 | |
Antonio de Angelis | f8564cb | 2021-04-28 13:52:13 +0100 | [diff] [blame] | 894 | return TFM_ERROR_INVALID_PARAMETER; |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 895 | } |
| 896 | |
Antonio de Angelis | 00667fc | 2021-05-05 22:40:39 +0100 | [diff] [blame] | 897 | static void tfm_spm_partition_requests_thread(struct tfm_sfn_req_s *desc_ptr, |
| 898 | uint32_t exc_return, |
| 899 | uint32_t is_return, |
| 900 | uintptr_t msp) |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 901 | { |
Antonio de Angelis | 00667fc | 2021-05-05 22:40:39 +0100 | [diff] [blame] | 902 | enum tfm_status_e res; |
| 903 | uint32_t exc_ret; |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 904 | |
Antonio de Angelis | 00667fc | 2021-05-05 22:40:39 +0100 | [diff] [blame] | 905 | if (!is_return) { |
| 906 | res = tfm_spm_sfn_request_handler(desc_ptr, exc_return); |
| 907 | exc_ret = EXC_RETURN_SECURE_FUNCTION; |
| 908 | } else { |
| 909 | res = tfm_return_from_partition(&exc_return); |
| 910 | exc_ret = exc_return; |
| 911 | } |
| 912 | /* Reset MSP at top of stack and do TFM_SVC_SFN_COMPLETION */ |
| 913 | tfm_sfn_completion(res, exc_ret, msp); |
| 914 | } |
| 915 | |
| 916 | /* This SVC handler is called if veneer is running in thread mode */ |
| 917 | void tfm_spm_partition_request_return_handler( |
| 918 | const uint32_t *svc_ctx, uint32_t exc_return, uint32_t *msp) |
| 919 | { |
| 920 | if (!(exc_return & EXC_RETURN_STACK_PROCESS)) { |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 921 | /* Service request SVC called with MSP active. |
| 922 | * Either invalid configuration for Thread mode or SVC called |
| 923 | * from Handler mode, which is not supported. |
| 924 | * FixMe: error severity TBD |
| 925 | */ |
Antonio de Angelis | fa5d460 | 2021-06-09 16:11:11 +0100 | [diff] [blame] | 926 | SPMLOG_ERRMSG("Service request SVC called with MSP active!\r\n"); |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 927 | tfm_secure_api_error_handler(); |
| 928 | } |
| 929 | |
Antonio de Angelis | 00667fc | 2021-05-05 22:40:39 +0100 | [diff] [blame] | 930 | /* Setup a context on the stack to trigger exception return */ |
| 931 | struct tfm_state_context_t ctx = {0}; |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 932 | |
Antonio de Angelis | 00667fc | 2021-05-05 22:40:39 +0100 | [diff] [blame] | 933 | ctx.r0 = svc_ctx ? svc_ctx[0] : (uintptr_t) NULL; |
| 934 | ctx.r1 = exc_return; |
| 935 | ctx.r2 = svc_ctx ? 0 : 1; |
| 936 | ctx.r3 = (uintptr_t) msp; |
| 937 | ctx.xpsr = XPSR_T32; |
| 938 | ctx.ra = (uint32_t) tfm_spm_partition_requests_thread & ~0x1UL; |
| 939 | |
| 940 | __set_MSP((uint32_t)&ctx); |
| 941 | |
| 942 | tfm_arch_trigger_exc_return(EXC_RETURN_THREAD_S_MSP); |
| 943 | } |
| 944 | |
| 945 | void tfm_spm_partition_completion_handler(enum tfm_status_e res, uint32_t exc_return, uint32_t *msp) |
| 946 | { |
| 947 | if (res != TFM_SUCCESS) { |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 948 | tfm_secure_api_error_handler(); |
| 949 | } |
| 950 | |
Antonio de Angelis | 00667fc | 2021-05-05 22:40:39 +0100 | [diff] [blame] | 951 | __set_MSP((uint32_t)msp + sizeof(struct tfm_state_context_t)); |
| 952 | |
| 953 | tfm_arch_trigger_exc_return(exc_return); |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | /* This SVC handler is called, if a thread mode execution environment is to |
| 957 | * be set up, to run an unprivileged IRQ handler |
| 958 | */ |
| 959 | uint32_t tfm_spm_depriv_req_handler(uint32_t *svc_args, uint32_t excReturn) |
| 960 | { |
| 961 | struct tfm_state_context_t *svc_ctx = |
| 962 | (struct tfm_state_context_t *)svc_args; |
| 963 | |
| 964 | enum tfm_status_e res; |
| 965 | |
| 966 | if (excReturn & EXC_RETURN_STACK_PROCESS) { |
| 967 | /* FixMe: error severity TBD */ |
Antonio de Angelis | fa5d460 | 2021-06-09 16:11:11 +0100 | [diff] [blame] | 968 | SPMLOG_ERRMSG("Partition request SVC called with PSP active!\r\n"); |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 969 | tfm_secure_api_error_handler(); |
| 970 | } |
| 971 | |
| 972 | res = tfm_start_partition_for_irq_handling(excReturn, svc_ctx); |
| 973 | if (res != TFM_SUCCESS) { |
| 974 | /* The partition is in an invalid state (UNINIT or CLOSED), so none of |
| 975 | * its code can be run |
| 976 | */ |
| 977 | /* FixMe: For now this case is handled with TF-M panic, however it would |
| 978 | * be possible to skip the execution of the interrupt handler, and |
| 979 | * resume the execution of the interrupted code. |
| 980 | */ |
| 981 | tfm_secure_api_error_handler(); |
| 982 | } |
| 983 | return EXC_RETURN_SECURE_FUNCTION; |
| 984 | } |
| 985 | |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 986 | /* This SVC handler is called if a deprivileged IRQ handler was executed, and |
| 987 | * the execution environment is to be set back for the privileged handler mode |
| 988 | */ |
| 989 | uint32_t tfm_spm_depriv_return_handler(uint32_t *irq_svc_args, uint32_t lr) |
| 990 | { |
| 991 | enum tfm_status_e res; |
Ken Liu | e0af44c | 2020-07-25 22:51:30 +0800 | [diff] [blame] | 992 | struct tfm_state_context_t *irq_svc_ctx; |
| 993 | |
| 994 | /* Take into account the sealed stack*/ |
| 995 | irq_svc_args += 2; |
| 996 | |
| 997 | irq_svc_ctx = (struct tfm_state_context_t *)irq_svc_args; |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 998 | |
| 999 | if (!(lr & EXC_RETURN_STACK_PROCESS)) { |
| 1000 | /* Partition request SVC called with MSP active. |
| 1001 | * FixMe: error severity TBD |
| 1002 | */ |
Antonio de Angelis | fa5d460 | 2021-06-09 16:11:11 +0100 | [diff] [blame] | 1003 | SPMLOG_ERRMSG("Partition request SVC called with MSP active!\r\n"); |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 1004 | tfm_secure_api_error_handler(); |
| 1005 | } |
| 1006 | |
| 1007 | res = tfm_return_from_partition_irq_handling(&lr); |
| 1008 | if (res != TFM_SUCCESS) { |
| 1009 | /* Unlock errors indicate ctx database corruption or unknown anomalies |
| 1010 | * Halt execution |
| 1011 | */ |
Antonio de Angelis | fa5d460 | 2021-06-09 16:11:11 +0100 | [diff] [blame] | 1012 | SPMLOG_ERRMSG("Secure API error during unlock!\r\n"); |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 1013 | tfm_secure_api_error_handler(); |
| 1014 | } |
| 1015 | |
| 1016 | irq_svc_ctx->ra = lr; |
| 1017 | |
| 1018 | return EXC_RETURN_SECURE_HANDLER; |
| 1019 | } |
| 1020 | |
| 1021 | /* FIXME: get_irq_line_for_signal is also implemented in the ipc folder. */ |
| 1022 | /** |
| 1023 | * \brief Return the IRQ line number associated with a signal |
| 1024 | * |
| 1025 | * \param[in] partition_id The ID of the partition in which we look for the |
| 1026 | * signal |
| 1027 | * \param[in] signal The signal we do the query for |
| 1028 | * |
| 1029 | * \retval >=0 The IRQ line number associated with a signal in the partition |
| 1030 | * \retval <0 error |
| 1031 | */ |
Kevin Peng | a20b5af | 2021-01-11 11:20:52 +0800 | [diff] [blame] | 1032 | static int32_t get_irq_line_for_signal(int32_t partition_id, |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 1033 | psa_signal_t signal) |
| 1034 | { |
| 1035 | size_t i; |
| 1036 | |
Ken Liu | 24dffb2 | 2021-02-10 11:03:58 +0800 | [diff] [blame] | 1037 | if (!IS_ONLY_ONE_BIT_IN_UINT32(signal)) { |
Kevin Peng | 410bee5 | 2021-01-13 16:27:17 +0800 | [diff] [blame] | 1038 | return -1; |
| 1039 | } |
| 1040 | |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 1041 | for (i = 0; i < tfm_core_irq_signals_count; ++i) { |
| 1042 | if (tfm_core_irq_signals[i].partition_id == partition_id && |
| 1043 | tfm_core_irq_signals[i].signal_value == signal) { |
| 1044 | return tfm_core_irq_signals[i].irq_line; |
| 1045 | } |
| 1046 | } |
Kevin Peng | a20b5af | 2021-01-11 11:20:52 +0800 | [diff] [blame] | 1047 | return -1; |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | void tfm_spm_enable_irq_handler(uint32_t *svc_args) |
| 1051 | { |
| 1052 | struct tfm_state_context_t *svc_ctx = |
| 1053 | (struct tfm_state_context_t *)svc_args; |
| 1054 | psa_signal_t irq_signal = svc_ctx->r0; |
| 1055 | uint32_t running_partition_idx = |
| 1056 | tfm_spm_partition_get_running_partition_idx(); |
| 1057 | uint32_t running_partition_id = |
| 1058 | tfm_spm_partition_get_partition_id(running_partition_idx); |
Kevin Peng | a20b5af | 2021-01-11 11:20:52 +0800 | [diff] [blame] | 1059 | int32_t irq_line; |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 1060 | |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 1061 | irq_line = get_irq_line_for_signal(running_partition_id, irq_signal); |
| 1062 | |
| 1063 | if (irq_line < 0) { |
| 1064 | /* FixMe: error severity TBD */ |
| 1065 | tfm_secure_api_error_handler(); |
| 1066 | } |
| 1067 | |
| 1068 | tfm_spm_hal_enable_irq(irq_line); |
| 1069 | } |
| 1070 | |
| 1071 | void tfm_spm_disable_irq_handler(uint32_t *svc_args) |
| 1072 | { |
| 1073 | struct tfm_state_context_t *svc_ctx = |
| 1074 | (struct tfm_state_context_t *)svc_args; |
| 1075 | psa_signal_t irq_signal = svc_ctx->r0; |
| 1076 | uint32_t running_partition_idx = |
| 1077 | tfm_spm_partition_get_running_partition_idx(); |
| 1078 | uint32_t running_partition_id = |
| 1079 | tfm_spm_partition_get_partition_id(running_partition_idx); |
Kevin Peng | a20b5af | 2021-01-11 11:20:52 +0800 | [diff] [blame] | 1080 | int32_t irq_line; |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 1081 | |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 1082 | irq_line = get_irq_line_for_signal(running_partition_id, irq_signal); |
| 1083 | |
| 1084 | if (irq_line < 0) { |
| 1085 | /* FixMe: error severity TBD */ |
| 1086 | tfm_secure_api_error_handler(); |
| 1087 | } |
| 1088 | |
| 1089 | tfm_spm_hal_disable_irq(irq_line); |
| 1090 | } |
| 1091 | |
| 1092 | void tfm_spm_psa_wait(uint32_t *svc_args) |
| 1093 | { |
| 1094 | /* Look for partition that is ready for run */ |
| 1095 | struct tfm_state_context_t *svc_ctx = |
| 1096 | (struct tfm_state_context_t *)svc_args; |
| 1097 | uint32_t running_partition_idx; |
| 1098 | const struct spm_partition_runtime_data_t *curr_part_data; |
| 1099 | |
| 1100 | psa_signal_t signal_mask = svc_ctx->r0; |
| 1101 | uint32_t timeout = svc_ctx->r1; |
| 1102 | |
| 1103 | /* |
| 1104 | * Timeout[30:0] are reserved for future use. |
| 1105 | * SPM must ignore the value of RES. |
| 1106 | */ |
| 1107 | timeout &= PSA_TIMEOUT_MASK; |
| 1108 | |
| 1109 | running_partition_idx = tfm_spm_partition_get_running_partition_idx(); |
| 1110 | curr_part_data = tfm_spm_partition_get_runtime_data(running_partition_idx); |
| 1111 | |
| 1112 | if (timeout == PSA_BLOCK) { |
| 1113 | /* FIXME: Scheduling is not available in library model, and busy wait is |
| 1114 | * also not possible as this code is running in SVC context, and it |
| 1115 | * cannot be pre-empted by interrupts. So do nothing here for now |
| 1116 | */ |
| 1117 | (void) signal_mask; |
| 1118 | } |
| 1119 | |
| 1120 | svc_ctx->r0 = curr_part_data->signal_mask; |
| 1121 | } |
| 1122 | |
| 1123 | void tfm_spm_psa_eoi(uint32_t *svc_args) |
| 1124 | { |
| 1125 | struct tfm_state_context_t *svc_ctx = |
| 1126 | (struct tfm_state_context_t *)svc_args; |
| 1127 | psa_signal_t irq_signal = svc_ctx->r0; |
| 1128 | uint32_t signal_mask; |
| 1129 | uint32_t running_partition_idx; |
| 1130 | uint32_t running_partition_id; |
| 1131 | const struct spm_partition_runtime_data_t *curr_part_data; |
Kevin Peng | a20b5af | 2021-01-11 11:20:52 +0800 | [diff] [blame] | 1132 | int32_t irq_line; |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 1133 | |
| 1134 | running_partition_idx = tfm_spm_partition_get_running_partition_idx(); |
| 1135 | running_partition_id = |
| 1136 | tfm_spm_partition_get_partition_id(running_partition_idx); |
| 1137 | curr_part_data = tfm_spm_partition_get_runtime_data(running_partition_idx); |
| 1138 | |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 1139 | irq_line = get_irq_line_for_signal(running_partition_id, irq_signal); |
| 1140 | |
| 1141 | if (irq_line < 0) { |
| 1142 | /* FixMe: error severity TBD */ |
| 1143 | tfm_secure_api_error_handler(); |
| 1144 | } |
| 1145 | |
| 1146 | tfm_spm_hal_clear_pending_irq(irq_line); |
| 1147 | tfm_spm_hal_enable_irq(irq_line); |
| 1148 | |
| 1149 | signal_mask = curr_part_data->signal_mask & ~irq_signal; |
| 1150 | tfm_spm_partition_set_signal_mask(running_partition_idx, signal_mask); |
| 1151 | } |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1152 | |
| 1153 | /* |
| 1154 | * This function is called when a secure partition causes an error. |
| 1155 | * In case of an error in the error handling, a non-zero value have to be |
| 1156 | * returned. |
| 1157 | */ |
Xinyu Zhang | a8820de | 2021-01-25 16:41:50 +0800 | [diff] [blame] | 1158 | static void tfm_spm_partition_err_handler(const uint32_t idx, int32_t errcode) |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1159 | { |
Xinyu Zhang | a8820de | 2021-01-25 16:41:50 +0800 | [diff] [blame] | 1160 | (void)errcode; |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 1161 | |
Xinyu Zhang | a8820de | 2021-01-25 16:41:50 +0800 | [diff] [blame] | 1162 | tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_CLOSED); |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1163 | } |
| 1164 | |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 1165 | fih_int tfm_spm_partition_init(void) |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1166 | { |
| 1167 | struct spm_partition_desc_t *part; |
| 1168 | struct tfm_sfn_req_s desc; |
| 1169 | int32_t args[4] = {0}; |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 1170 | fih_int fail_cnt = FIH_INT_INIT(0); |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1171 | uint32_t idx; |
Mingyang Sun | 61f8fbc | 2021-06-04 17:49:56 +0800 | [diff] [blame] | 1172 | bool privileged; |
Ken Liu | 172f1e3 | 2021-02-05 16:31:03 +0800 | [diff] [blame] | 1173 | const struct platform_data_t **platform_data_p; |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 1174 | #ifdef TFM_FIH_PROFILE_ON |
| 1175 | fih_int fih_rc = FIH_FAILURE; |
| 1176 | #endif |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1177 | |
| 1178 | /* Call the init function for each partition */ |
| 1179 | for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) { |
| 1180 | part = &g_spm_partition_db.partitions[idx]; |
Mate Toth-Pal | 8ac98a7 | 2019-11-21 17:30:10 +0100 | [diff] [blame] | 1181 | platform_data_p = part->platform_data_list; |
| 1182 | if (platform_data_p != NULL) { |
| 1183 | while ((*platform_data_p) != NULL) { |
Mingyang Sun | 61f8fbc | 2021-06-04 17:49:56 +0800 | [diff] [blame] | 1184 | if (tfm_is_partition_privileged(idx)) { |
| 1185 | privileged = true; |
| 1186 | } else { |
| 1187 | privileged = false; |
| 1188 | } |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 1189 | #ifdef TFM_FIH_PROFILE_ON |
Mingyang Sun | 61f8fbc | 2021-06-04 17:49:56 +0800 | [diff] [blame] | 1190 | FIH_CALL(tfm_spm_hal_configure_default_isolation, fih_rc, |
| 1191 | privileged, *platform_data_p); |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 1192 | if (fih_not_eq(fih_rc, fih_int_encode(TFM_PLAT_ERR_SUCCESS))) { |
| 1193 | fail_cnt = fih_int_encode(fih_int_decode(fail_cnt) + 1); |
| 1194 | } |
| 1195 | #else /* TFM_FIH_PROFILE_ON */ |
Mingyang Sun | 61f8fbc | 2021-06-04 17:49:56 +0800 | [diff] [blame] | 1196 | if (tfm_spm_hal_configure_default_isolation(privileged, |
Edison Ai | 6be3df1 | 2020-02-14 22:14:33 +0800 | [diff] [blame] | 1197 | *platform_data_p) != TFM_PLAT_ERR_SUCCESS) { |
| 1198 | fail_cnt++; |
| 1199 | } |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 1200 | #endif /* TFM_FIH_PROFILE_ON */ |
Mate Toth-Pal | 8ac98a7 | 2019-11-21 17:30:10 +0100 | [diff] [blame] | 1201 | ++platform_data_p; |
| 1202 | } |
| 1203 | } |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 1204 | if (part->static_data->partition_init == NULL) { |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1205 | tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE); |
| 1206 | tfm_spm_partition_set_caller_partition_idx(idx, |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 1207 | SPM_INVALID_PARTITION_IDX); |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1208 | } else { |
| 1209 | int32_t res; |
| 1210 | |
| 1211 | desc.args = args; |
Summer Qin | 43c185d | 2019-10-10 15:44:42 +0800 | [diff] [blame] | 1212 | desc.ns_caller = false; |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 1213 | desc.sfn = (sfn_t)part->static_data->partition_init; |
| 1214 | desc.sp_id = part->static_data->partition_id; |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1215 | res = tfm_core_sfn_request(&desc); |
| 1216 | if (res == TFM_SUCCESS) { |
| 1217 | tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE); |
| 1218 | } else { |
Xinyu Zhang | a8820de | 2021-01-25 16:41:50 +0800 | [diff] [blame] | 1219 | tfm_spm_partition_err_handler(idx, res); |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 1220 | fail_cnt = fih_int_encode(fih_int_decode(fail_cnt) + 1); |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1221 | } |
| 1222 | } |
| 1223 | } |
| 1224 | |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 1225 | tfm_spm_secure_api_init_done(); |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1226 | |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 1227 | fih_int_validate(fail_cnt); |
| 1228 | if (fih_eq(fail_cnt, fih_int_encode(0))) { |
| 1229 | FIH_RET(fih_int_encode(SPM_ERR_OK)); |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1230 | } |
David Hu | 9804b6a | 2021-02-15 21:23:06 +0800 | [diff] [blame] | 1231 | |
| 1232 | FIH_RET(fih_int_encode(SPM_ERR_PARTITION_NOT_AVAILABLE)); |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | void tfm_spm_partition_push_interrupted_ctx(uint32_t partition_idx) |
| 1236 | { |
| 1237 | struct spm_partition_runtime_data_t *runtime_data = |
| 1238 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 1239 | struct interrupted_ctx_stack_frame_t *stack_frame = |
Edison Ai | 7aff9e8 | 2019-07-11 14:56:46 +0800 | [diff] [blame] | 1240 | (struct interrupted_ctx_stack_frame_t *)runtime_data->ctx_stack_ptr; |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1241 | |
| 1242 | stack_frame->partition_state = runtime_data->partition_state; |
Matt | 463ed58 | 2019-12-20 12:31:25 +0800 | [diff] [blame] | 1243 | |
| 1244 | runtime_data->ctx_stack_ptr += |
| 1245 | sizeof(struct interrupted_ctx_stack_frame_t) / sizeof(uint32_t); |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | void tfm_spm_partition_pop_interrupted_ctx(uint32_t partition_idx) |
| 1249 | { |
| 1250 | struct spm_partition_runtime_data_t *runtime_data = |
| 1251 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 1252 | struct interrupted_ctx_stack_frame_t *stack_frame; |
| 1253 | |
Matt | 463ed58 | 2019-12-20 12:31:25 +0800 | [diff] [blame] | 1254 | runtime_data->ctx_stack_ptr -= |
| 1255 | sizeof(struct interrupted_ctx_stack_frame_t) / sizeof(uint32_t); |
| 1256 | |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1257 | stack_frame = (struct interrupted_ctx_stack_frame_t *) |
| 1258 | runtime_data->ctx_stack_ptr; |
| 1259 | tfm_spm_partition_set_state(partition_idx, stack_frame->partition_state); |
| 1260 | stack_frame->partition_state = 0; |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | void tfm_spm_partition_push_handler_ctx(uint32_t partition_idx) |
| 1264 | { |
| 1265 | struct spm_partition_runtime_data_t *runtime_data = |
| 1266 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 1267 | struct handler_ctx_stack_frame_t *stack_frame = |
| 1268 | (struct handler_ctx_stack_frame_t *) |
| 1269 | runtime_data->ctx_stack_ptr; |
| 1270 | |
| 1271 | stack_frame->partition_state = runtime_data->partition_state; |
| 1272 | stack_frame->caller_partition_idx = runtime_data->caller_partition_idx; |
| 1273 | |
| 1274 | runtime_data->ctx_stack_ptr += |
| 1275 | sizeof(struct handler_ctx_stack_frame_t) / sizeof(uint32_t); |
| 1276 | } |
| 1277 | |
| 1278 | void tfm_spm_partition_pop_handler_ctx(uint32_t partition_idx) |
| 1279 | { |
| 1280 | struct spm_partition_runtime_data_t *runtime_data = |
| 1281 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
| 1282 | struct handler_ctx_stack_frame_t *stack_frame; |
| 1283 | |
| 1284 | runtime_data->ctx_stack_ptr -= |
| 1285 | sizeof(struct handler_ctx_stack_frame_t) / sizeof(uint32_t); |
| 1286 | |
| 1287 | stack_frame = (struct handler_ctx_stack_frame_t *) |
| 1288 | runtime_data->ctx_stack_ptr; |
| 1289 | |
| 1290 | tfm_spm_partition_set_state(partition_idx, stack_frame->partition_state); |
| 1291 | stack_frame->partition_state = 0; |
| 1292 | tfm_spm_partition_set_caller_partition_idx( |
| 1293 | partition_idx, stack_frame->caller_partition_idx); |
| 1294 | stack_frame->caller_partition_idx = 0; |
| 1295 | } |
| 1296 | |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1297 | void tfm_spm_partition_store_context(uint32_t partition_idx, |
| 1298 | uint32_t stack_ptr, uint32_t lr) |
| 1299 | { |
| 1300 | g_spm_partition_db.partitions[partition_idx]. |
| 1301 | runtime_data.stack_ptr = stack_ptr; |
| 1302 | g_spm_partition_db.partitions[partition_idx]. |
| 1303 | runtime_data.lr = lr; |
| 1304 | } |
| 1305 | |
| 1306 | const struct spm_partition_runtime_data_t * |
| 1307 | tfm_spm_partition_get_runtime_data(uint32_t partition_idx) |
| 1308 | { |
| 1309 | return &(g_spm_partition_db.partitions[partition_idx].runtime_data); |
| 1310 | } |
| 1311 | |
| 1312 | void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state) |
| 1313 | { |
| 1314 | g_spm_partition_db.partitions[partition_idx].runtime_data.partition_state = |
| 1315 | state; |
| 1316 | if (state == SPM_PARTITION_STATE_RUNNING || |
| 1317 | state == SPM_PARTITION_STATE_HANDLING_IRQ) { |
| 1318 | g_spm_partition_db.running_partition_idx = partition_idx; |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | void tfm_spm_partition_set_caller_partition_idx(uint32_t partition_idx, |
| 1323 | uint32_t caller_partition_idx) |
| 1324 | { |
| 1325 | g_spm_partition_db.partitions[partition_idx].runtime_data. |
| 1326 | caller_partition_idx = caller_partition_idx; |
| 1327 | } |
| 1328 | |
| 1329 | void tfm_spm_partition_set_signal_mask(uint32_t partition_idx, |
| 1330 | uint32_t signal_mask) |
| 1331 | { |
| 1332 | g_spm_partition_db.partitions[partition_idx].runtime_data. |
| 1333 | signal_mask = signal_mask; |
| 1334 | } |
| 1335 | |
| 1336 | void tfm_spm_partition_set_caller_client_id(uint32_t partition_idx, |
| 1337 | int32_t caller_client_id) |
| 1338 | { |
| 1339 | g_spm_partition_db.partitions[partition_idx].runtime_data. |
| 1340 | caller_client_id = caller_client_id; |
| 1341 | } |
| 1342 | |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1343 | uint32_t tfm_spm_partition_get_running_partition_idx(void) |
| 1344 | { |
| 1345 | return g_spm_partition_db.running_partition_idx; |
| 1346 | } |
| 1347 | |
| 1348 | void tfm_spm_partition_cleanup_context(uint32_t partition_idx) |
| 1349 | { |
| 1350 | struct spm_partition_desc_t *partition = |
| 1351 | &(g_spm_partition_db.partitions[partition_idx]); |
| 1352 | int32_t i; |
| 1353 | |
| 1354 | partition->runtime_data.caller_partition_idx = SPM_INVALID_PARTITION_IDX; |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 1355 | partition->runtime_data.iovec_args.in_len = 0; |
| 1356 | for (i = 0; i < PSA_MAX_IOVEC; ++i) { |
| 1357 | partition->runtime_data.iovec_args.in_vec[i].base = 0; |
| 1358 | partition->runtime_data.iovec_args.in_vec[i].len = 0; |
| 1359 | } |
| 1360 | partition->runtime_data.iovec_args.out_len = 0; |
| 1361 | for (i = 0; i < PSA_MAX_IOVEC; ++i) { |
| 1362 | partition->runtime_data.iovec_args.out_vec[i].base = 0; |
| 1363 | partition->runtime_data.iovec_args.out_vec[i].len = 0; |
| 1364 | } |
| 1365 | partition->runtime_data.orig_outvec = 0; |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 1366 | } |
Summer Qin | 830c554 | 2020-02-14 13:44:20 +0800 | [diff] [blame] | 1367 | |
| 1368 | void tfm_spm_request_handler(const struct tfm_state_context_t *svc_ctx) |
| 1369 | { |
| 1370 | uint32_t *res_ptr = (uint32_t *)&svc_ctx->r0; |
| 1371 | uint32_t running_partition_flags = 0; |
| 1372 | uint32_t running_partition_idx; |
| 1373 | |
| 1374 | /* Check permissions on request type basis */ |
| 1375 | |
| 1376 | switch (svc_ctx->r0) { |
| 1377 | case TFM_SPM_REQUEST_RESET_VOTE: |
| 1378 | running_partition_idx = |
| 1379 | tfm_spm_partition_get_running_partition_idx(); |
| 1380 | running_partition_flags = tfm_spm_partition_get_flags( |
| 1381 | running_partition_idx); |
| 1382 | |
| 1383 | /* Currently only PSA Root of Trust services are allowed to make Reset |
| 1384 | * vote request |
| 1385 | */ |
| 1386 | if ((running_partition_flags & SPM_PART_FLAG_PSA_ROT) == 0) { |
| 1387 | *res_ptr = (uint32_t)TFM_ERROR_GENERIC; |
| 1388 | } |
| 1389 | |
| 1390 | /* FixMe: this is a placeholder for checks to be performed before |
| 1391 | * allowing execution of reset |
| 1392 | */ |
| 1393 | *res_ptr = (uint32_t)TFM_SUCCESS; |
| 1394 | |
| 1395 | break; |
| 1396 | default: |
| 1397 | *res_ptr = (uint32_t)TFM_ERROR_INVALID_PARAMETER; |
| 1398 | } |
| 1399 | } |
Mingyang Sun | bd7ceb5 | 2020-06-11 16:53:03 +0800 | [diff] [blame] | 1400 | |
| 1401 | enum spm_err_t tfm_spm_db_init(void) |
| 1402 | { |
| 1403 | uint32_t i; |
| 1404 | |
| 1405 | /* This function initialises partition db */ |
| 1406 | |
| 1407 | /* For the non secure Execution environment */ |
| 1408 | tfm_nspm_configure_clients(); |
| 1409 | |
| 1410 | for (i = 0; i < g_spm_partition_db.partition_count; i++) { |
| 1411 | g_spm_partition_db.partitions[i].runtime_data.partition_state = |
| 1412 | SPM_PARTITION_STATE_UNINIT; |
| 1413 | g_spm_partition_db.partitions[i].runtime_data.caller_partition_idx = |
| 1414 | SPM_INVALID_PARTITION_IDX; |
| 1415 | g_spm_partition_db.partitions[i].runtime_data.caller_client_id = |
| 1416 | TFM_INVALID_CLIENT_ID; |
| 1417 | g_spm_partition_db.partitions[i].runtime_data.ctx_stack_ptr = |
| 1418 | ctx_stack_list[i]; |
| 1419 | g_spm_partition_db.partitions[i].static_data = &static_data_list[i]; |
| 1420 | g_spm_partition_db.partitions[i].platform_data_list = |
| 1421 | platform_data_list_list[i]; |
| 1422 | } |
| 1423 | g_spm_partition_db.is_init = 1; |
| 1424 | |
| 1425 | return SPM_ERR_OK; |
| 1426 | } |