Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 1 | /* |
Sherry Zhang | dcab286 | 2022-01-10 10:43:31 +0800 | [diff] [blame] | 2 | * Copyright (c) 2018-2022, Arm Limited. All rights reserved. |
Chris Brand | 56b4d0c | 2021-12-17 16:15:58 -0800 | [diff] [blame] | 3 | * Copyright (c) 2021, Cypress Semiconductor Corporation. All rights reserved. |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 4 | * |
| 5 | * SPDX-License-Identifier: BSD-3-Clause |
| 6 | * |
| 7 | */ |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 8 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 9 | #include <inttypes.h> |
| 10 | #include <stdbool.h> |
Ken Liu | 086b32c | 2021-12-27 14:44:52 +0800 | [diff] [blame] | 11 | #include "aapcs_local.h" |
Ken Liu | 24dffb2 | 2021-02-10 11:03:58 +0800 | [diff] [blame] | 12 | #include "bitops.h" |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 13 | #include "critical_section.h" |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 14 | #include "current.h" |
David Hu | f07e97d | 2021-02-15 22:05:40 +0800 | [diff] [blame] | 15 | #include "fih.h" |
Jamie Fox | cc31d40 | 2019-01-28 17:13:52 +0000 | [diff] [blame] | 16 | #include "psa/client.h" |
| 17 | #include "psa/service.h" |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 18 | #include "thread.h" |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 19 | #include "internal_errors.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 20 | #include "tfm_spm_hal.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 21 | #include "tfm_api.h" |
| 22 | #include "tfm_secure_api.h" |
| 23 | #include "tfm_memory_utils.h" |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 24 | #include "tfm_hal_defs.h" |
Kevin Peng | d399a1f | 2021-09-08 15:33:14 +0800 | [diff] [blame] | 25 | #include "tfm_hal_interrupt.h" |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 26 | #include "tfm_hal_isolation.h" |
Mingyang Sun | 7397b4f | 2020-06-17 15:07:45 +0800 | [diff] [blame] | 27 | #include "spm_ipc.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 28 | #include "tfm_peripherals_def.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 29 | #include "tfm_core_utils.h" |
Kevin Peng | 385fda8 | 2021-08-18 10:41:19 +0800 | [diff] [blame] | 30 | #include "tfm_nspm.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 31 | #include "tfm_rpc.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 32 | #include "tfm_core_trustzone.h" |
Ken Liu | 24dffb2 | 2021-02-10 11:03:58 +0800 | [diff] [blame] | 33 | #include "lists.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 34 | #include "tfm_pools.h" |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 35 | #include "region.h" |
Mingyang Sun | 7397b4f | 2020-06-17 15:07:45 +0800 | [diff] [blame] | 36 | #include "psa_manifest/pid.h" |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 37 | #include "ffm/backend.h" |
Mingyang Sun | 00df235 | 2021-04-15 15:46:08 +0800 | [diff] [blame] | 38 | #include "load/partition_defs.h" |
| 39 | #include "load/service_defs.h" |
Ken Liu | 8668628 | 2021-04-27 11:11:15 +0800 | [diff] [blame] | 40 | #include "load/asset_defs.h" |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 41 | #include "load/spm_load_api.h" |
Sherry Zhnag | 482b88b | 2021-08-19 17:51:47 +0800 | [diff] [blame] | 42 | #include "tfm_nspm.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 43 | |
Sherry Zhang | 86a71c6 | 2022-01-12 18:19:02 +0800 | [diff] [blame] | 44 | #if !(defined CONFIG_TFM_CONN_HANDLE_MAX_NUM) || (CONFIG_TFM_CONN_HANDLE_MAX_NUM == 0) |
| 45 | #error "CONFIG_TFM_CONN_HANDLE_MAX_NUM must be defined and not zero." |
| 46 | #endif |
| 47 | |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 48 | /* Partition and service runtime data list head/runtime data table */ |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 49 | static struct service_head_t services_listhead; |
Ken Liu | b3b2cb6 | 2021-05-22 00:39:28 +0800 | [diff] [blame] | 50 | struct service_t *stateless_services_ref_tbl[STATIC_HANDLE_NUM_LIMIT]; |
Summer Qin | d99509f | 2019-08-02 17:36:58 +0800 | [diff] [blame] | 51 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 52 | /* Pools */ |
| 53 | TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t), |
Sherry Zhang | 86a71c6 | 2022-01-12 18:19:02 +0800 | [diff] [blame] | 54 | CONFIG_TFM_CONN_HANDLE_MAX_NUM); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 55 | |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 56 | extern uint32_t scheduler_lock; |
| 57 | |
Summer Qin | 373feb1 | 2020-03-27 15:35:33 +0800 | [diff] [blame] | 58 | /*********************** Connection handle conversion APIs *******************/ |
| 59 | |
Summer Qin | 373feb1 | 2020-03-27 15:35:33 +0800 | [diff] [blame] | 60 | #define CONVERSION_FACTOR_BITOFFSET 3 |
| 61 | #define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET) |
| 62 | /* Set 32 as the maximum */ |
| 63 | #define CONVERSION_FACTOR_VALUE_MAX 0x20 |
| 64 | |
| 65 | #if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX |
| 66 | #error "CONVERSION FACTOR OUT OF RANGE" |
| 67 | #endif |
| 68 | |
| 69 | static uint32_t loop_index; |
| 70 | |
| 71 | /* |
| 72 | * A handle instance psa_handle_t allocated inside SPM is actually a memory |
| 73 | * address among the handle pool. Return this handle to the client directly |
| 74 | * exposes information of secure memory address. In this case, converting the |
| 75 | * handle into another value does not represent the memory address to avoid |
| 76 | * exposing secure memory directly to clients. |
| 77 | * |
| 78 | * This function converts the handle instance into another value by scaling the |
| 79 | * handle in pool offset, the converted value is named as a user handle. |
| 80 | * |
| 81 | * The formula: |
| 82 | * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE + |
| 83 | * CLIENT_HANDLE_VALUE_MIN + loop_index |
| 84 | * where: |
| 85 | * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not |
| 86 | * exceed CONVERSION_FACTOR_VALUE_MAX. |
| 87 | * |
| 88 | * handle_instance in RANGE[POOL_START, POOL_END] |
| 89 | * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF] |
| 90 | * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1] |
| 91 | * |
| 92 | * note: |
| 93 | * loop_index is used to promise same handle instance is converted into |
| 94 | * different user handles in short time. |
| 95 | */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 96 | psa_handle_t tfm_spm_to_user_handle(struct tfm_conn_handle_t *handle_instance) |
Summer Qin | 373feb1 | 2020-03-27 15:35:33 +0800 | [diff] [blame] | 97 | { |
| 98 | psa_handle_t user_handle; |
| 99 | |
| 100 | loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE; |
| 101 | user_handle = (psa_handle_t)((((uintptr_t)handle_instance - |
| 102 | (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) + |
| 103 | CLIENT_HANDLE_VALUE_MIN + loop_index); |
| 104 | |
| 105 | return user_handle; |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * This function converts a user handle into a corresponded handle instance. |
| 110 | * The converted value is validated before returning, an invalid handle instance |
| 111 | * is returned as NULL. |
| 112 | * |
| 113 | * The formula: |
| 114 | * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) / |
| 115 | * CONVERSION_FACTOR_VALUE) + POOL_START |
| 116 | * where: |
| 117 | * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not |
| 118 | * exceed CONVERSION_FACTOR_VALUE_MAX. |
| 119 | * |
| 120 | * handle_instance in RANGE[POOL_START, POOL_END] |
| 121 | * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF] |
| 122 | * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1] |
| 123 | */ |
| 124 | struct tfm_conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle) |
| 125 | { |
| 126 | struct tfm_conn_handle_t *handle_instance; |
| 127 | |
| 128 | if (user_handle == PSA_NULL_HANDLE) { |
| 129 | return NULL; |
| 130 | } |
| 131 | |
| 132 | handle_instance = (struct tfm_conn_handle_t *)((((uintptr_t)user_handle - |
| 133 | CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) + |
| 134 | (uintptr_t)conn_handle_pool); |
| 135 | |
| 136 | return handle_instance; |
| 137 | } |
| 138 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 139 | /* Service handle management functions */ |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 140 | struct tfm_conn_handle_t *tfm_spm_create_conn_handle(struct service_t *service, |
| 141 | int32_t client_id) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 142 | { |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 143 | struct tfm_conn_handle_t *p_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 144 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 145 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 146 | |
| 147 | /* Get buffer for handle list structure from handle pool */ |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 148 | p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool); |
| 149 | if (!p_handle) { |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 150 | return NULL; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 151 | } |
| 152 | |
Kevin Peng | 2cc2572 | 2021-11-18 11:46:02 +0800 | [diff] [blame] | 153 | spm_memset(p_handle, 0, sizeof(*p_handle)); |
| 154 | |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 155 | p_handle->internal_msg.service = service; |
Shawn Shan | cc39fcb | 2019-11-13 15:38:16 +0800 | [diff] [blame] | 156 | p_handle->status = TFM_HANDLE_STATUS_IDLE; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 157 | p_handle->client_id = client_id; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 158 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 159 | return p_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 160 | } |
| 161 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 162 | int32_t tfm_spm_validate_conn_handle( |
| 163 | const struct tfm_conn_handle_t *conn_handle, |
| 164 | int32_t client_id) |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 165 | { |
| 166 | /* Check the handle address is validated */ |
| 167 | if (is_valid_chunk_data_in_pool(conn_handle_pool, |
| 168 | (uint8_t *)conn_handle) != true) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 169 | return SPM_ERROR_GENERIC; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /* Check the handle caller is correct */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 173 | if (conn_handle->client_id != client_id) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 174 | return SPM_ERROR_GENERIC; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 175 | } |
| 176 | |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 177 | return SPM_SUCCESS; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 178 | } |
| 179 | |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 180 | int32_t tfm_spm_free_conn_handle(struct service_t *service, |
Summer Qin | 02f7f07 | 2020-08-24 16:02:54 +0800 | [diff] [blame] | 181 | struct tfm_conn_handle_t *conn_handle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 182 | { |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 183 | struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT; |
| 184 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 185 | TFM_CORE_ASSERT(service); |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 186 | TFM_CORE_ASSERT(conn_handle != NULL); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 187 | |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 188 | /* Clear magic as the handler is not used anymore */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 189 | conn_handle->internal_msg.magic = 0; |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 190 | |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 191 | CRITICAL_SECTION_ENTER(cs_assert); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 192 | |
| 193 | /* Back handle buffer to pool */ |
Ken Liu | 66ca613 | 2021-02-24 08:49:51 +0800 | [diff] [blame] | 194 | tfm_pool_free(conn_handle_pool, conn_handle); |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 195 | CRITICAL_SECTION_LEAVE(cs_assert); |
| 196 | |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 197 | return SPM_SUCCESS; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 198 | } |
| 199 | |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 200 | int32_t tfm_spm_set_rhandle(struct service_t *service, |
Summer Qin | 02f7f07 | 2020-08-24 16:02:54 +0800 | [diff] [blame] | 201 | struct tfm_conn_handle_t *conn_handle, |
| 202 | void *rhandle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 203 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 204 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 205 | /* Set reverse handle value only be allowed for a connected handle */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 206 | TFM_CORE_ASSERT(conn_handle != NULL); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 207 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 208 | conn_handle->rhandle = rhandle; |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 209 | return SPM_SUCCESS; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 210 | } |
| 211 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 212 | /** |
Xinyu Zhang | 3a45324 | 2021-04-16 17:57:09 +0800 | [diff] [blame] | 213 | * \brief Get reverse handle value from connection handle. |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 214 | * |
| 215 | * \param[in] service Target service context pointer |
| 216 | * \param[in] conn_handle Connection handle created by |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 217 | * tfm_spm_create_conn_handle() |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 218 | * |
| 219 | * \retval void * Success |
| 220 | * \retval "Does not return" Panic for those: |
| 221 | * service pointer are NULL |
Xinyu Zhang | 3a45324 | 2021-04-16 17:57:09 +0800 | [diff] [blame] | 222 | * handle is \ref PSA_NULL_HANDLE |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 223 | * handle node does not be found |
| 224 | */ |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 225 | static void *tfm_spm_get_rhandle(struct service_t *service, |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 226 | struct tfm_conn_handle_t *conn_handle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 227 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 228 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 229 | /* Get reverse handle value only be allowed for a connected handle */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 230 | TFM_CORE_ASSERT(conn_handle != NULL); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 231 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 232 | return conn_handle->rhandle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /* Partition management functions */ |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 236 | |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame^] | 237 | struct tfm_msg_body_t *spm_get_msg_with_signal(struct partition_t *p_ptn, |
| 238 | psa_signal_t signal) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 239 | { |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame^] | 240 | struct tfm_msg_body_t *p_msg_iter; |
| 241 | struct tfm_msg_body_t **pr_msg_iter, **last_found_msg_holder = NULL; |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 242 | struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT; |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame^] | 243 | uint32_t nr_found_msgs = 0; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 244 | |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 245 | CRITICAL_SECTION_ENTER(cs_assert); |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame^] | 246 | |
| 247 | /* Return the last found message which applies a FIFO mechanism. */ |
| 248 | UNI_LIST_FOREACH_NODE_PNODE(pr_msg_iter, p_msg_iter, p_ptn, p_messages) { |
| 249 | if (p_msg_iter->service->p_ldinf->signal == signal) { |
| 250 | last_found_msg_holder = pr_msg_iter; |
| 251 | nr_found_msgs++; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 252 | } |
| 253 | } |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 254 | |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame^] | 255 | if (last_found_msg_holder) { |
| 256 | p_msg_iter = *last_found_msg_holder; |
| 257 | UNI_LIST_REMOVE_NODE_BY_PNODE(last_found_msg_holder, p_messages); |
| 258 | |
| 259 | /* Clear the signal bit since the only message is removed. */ |
| 260 | if (nr_found_msgs == 1) { |
| 261 | p_ptn->signals_asserted &= ~signal; |
| 262 | } |
| 263 | } |
| 264 | |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 265 | CRITICAL_SECTION_LEAVE(cs_assert); |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 266 | |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame^] | 267 | return p_msg_iter; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 268 | } |
| 269 | |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 270 | struct service_t *tfm_spm_get_service_by_sid(uint32_t sid) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 271 | { |
Feder.Liang | aeb5a79 | 2021-08-10 16:12:56 +0800 | [diff] [blame] | 272 | struct service_t *p_prev, *p_curr; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 273 | |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame^] | 274 | UNI_LIST_FOREACH_NODE_PREV(p_prev, p_curr, &services_listhead, next) { |
Feder.Liang | aeb5a79 | 2021-08-10 16:12:56 +0800 | [diff] [blame] | 275 | if (p_curr->p_ldinf->sid == sid) { |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame^] | 276 | UNI_LIST_MOVE_AFTER(&services_listhead, p_prev, p_curr, next); |
Feder.Liang | aeb5a79 | 2021-08-10 16:12:56 +0800 | [diff] [blame] | 277 | return p_curr; |
Mingyang Sun | ef42f44 | 2021-06-11 15:07:58 +0800 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 281 | return NULL; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 282 | } |
| 283 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 284 | /** |
| 285 | * \brief Get the partition context by partition ID. |
| 286 | * |
| 287 | * \param[in] partition_id Partition identity |
| 288 | * |
| 289 | * \retval NULL Failed |
| 290 | * \retval "Not NULL" Target partition context pointer, |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 291 | * \ref partition_t structures |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 292 | */ |
Kevin Peng | eca45b9 | 2021-02-09 14:46:50 +0800 | [diff] [blame] | 293 | struct partition_t *tfm_spm_get_partition_by_id(int32_t partition_id) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 294 | { |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 295 | struct partition_t *p_part; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 296 | |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame^] | 297 | UNI_LIST_FOREACH(p_part, PARTITION_LIST_ADDR, next) { |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 298 | if (p_part->p_ldinf->pid == partition_id) { |
| 299 | return p_part; |
| 300 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 301 | } |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 302 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 303 | return NULL; |
| 304 | } |
| 305 | |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 306 | int32_t tfm_spm_check_client_version(struct service_t *service, |
Jaykumar Pitambarbhai Patel | 3a98602 | 2019-10-08 17:37:15 +0530 | [diff] [blame] | 307 | uint32_t version) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 308 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 309 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 310 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 311 | switch (SERVICE_GET_VERSION_POLICY(service->p_ldinf->flags)) { |
Ken Liu | b3b2cb6 | 2021-05-22 00:39:28 +0800 | [diff] [blame] | 312 | case SERVICE_VERSION_POLICY_RELAXED: |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 313 | if (version > service->p_ldinf->version) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 314 | return SPM_ERROR_VERSION; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 315 | } |
| 316 | break; |
Ken Liu | b3b2cb6 | 2021-05-22 00:39:28 +0800 | [diff] [blame] | 317 | case SERVICE_VERSION_POLICY_STRICT: |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 318 | if (version != service->p_ldinf->version) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 319 | return SPM_ERROR_VERSION; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 320 | } |
| 321 | break; |
| 322 | default: |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 323 | return SPM_ERROR_VERSION; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 324 | } |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 325 | return SPM_SUCCESS; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 326 | } |
| 327 | |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 328 | int32_t tfm_spm_check_authorization(uint32_t sid, |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 329 | struct service_t *service, |
Summer Qin | 618e8c3 | 2019-12-09 10:47:20 +0800 | [diff] [blame] | 330 | bool ns_caller) |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 331 | { |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 332 | struct partition_t *partition = NULL; |
Mingyang Sun | 2b35266 | 2021-04-21 11:35:43 +0800 | [diff] [blame] | 333 | uint32_t *dep; |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 334 | int32_t i; |
| 335 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 336 | TFM_CORE_ASSERT(service); |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 337 | |
| 338 | if (ns_caller) { |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 339 | if (!SERVICE_IS_NS_ACCESSIBLE(service->p_ldinf->flags)) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 340 | return SPM_ERROR_GENERIC; |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 341 | } |
| 342 | } else { |
Kevin Peng | cdf3ae2 | 2022-01-19 14:53:52 +0800 | [diff] [blame] | 343 | partition = GET_CURRENT_COMPONENT(); |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 344 | if (!partition) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 345 | tfm_core_panic(); |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 346 | } |
| 347 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 348 | dep = (uint32_t *)LOAD_INFO_DEPS(partition->p_ldinf); |
| 349 | for (i = 0; i < partition->p_ldinf->ndeps; i++) { |
Mingyang Sun | 2b35266 | 2021-04-21 11:35:43 +0800 | [diff] [blame] | 350 | if (dep[i] == sid) { |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 351 | break; |
| 352 | } |
| 353 | } |
| 354 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 355 | if (i == partition->p_ldinf->ndeps) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 356 | return SPM_ERROR_GENERIC; |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 357 | } |
| 358 | } |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 359 | return SPM_SUCCESS; |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 360 | } |
| 361 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 362 | /* Message functions */ |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 363 | |
Summer Qin | 02f7f07 | 2020-08-24 16:02:54 +0800 | [diff] [blame] | 364 | struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 365 | { |
| 366 | /* |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 367 | * The message handler passed by the caller is considered invalid in the |
| 368 | * following cases: |
| 369 | * 1. Not a valid message handle. (The address of a message is not the |
| 370 | * address of a possible handle from the pool |
| 371 | * 2. Handle not belongs to the caller partition (The handle is either |
| 372 | * unused, or owned by anither partition) |
| 373 | * Check the conditions above |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 374 | */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 375 | struct tfm_msg_body_t *p_msg; |
Kevin Peng | eec41a8 | 2021-08-18 13:56:23 +0800 | [diff] [blame] | 376 | int32_t partition_id; |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 377 | struct tfm_conn_handle_t *p_conn_handle = |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 378 | tfm_spm_to_handle_instance(msg_handle); |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 379 | |
| 380 | if (is_valid_chunk_data_in_pool( |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 381 | conn_handle_pool, (uint8_t *)p_conn_handle) != 1) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 382 | return NULL; |
| 383 | } |
| 384 | |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 385 | p_msg = &p_conn_handle->internal_msg; |
| 386 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 387 | /* |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 388 | * Check that the magic number is correct. This proves that the message |
| 389 | * structure contains an active message. |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 390 | */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 391 | if (p_msg->magic != TFM_MSG_MAGIC) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 392 | return NULL; |
| 393 | } |
| 394 | |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 395 | /* Check that the running partition owns the message */ |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 396 | partition_id = tfm_spm_partition_get_running_partition_id(); |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 397 | if (partition_id != p_msg->service->partition->p_ldinf->pid) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 398 | return NULL; |
| 399 | } |
| 400 | |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 401 | return p_msg; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 402 | } |
| 403 | |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 404 | void tfm_spm_fill_msg(struct tfm_msg_body_t *msg, |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 405 | struct service_t *service, |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 406 | psa_handle_t handle, |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 407 | int32_t type, int32_t client_id, |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 408 | psa_invec *invec, size_t in_len, |
| 409 | psa_outvec *outvec, size_t out_len, |
| 410 | psa_outvec *caller_outvec) |
| 411 | { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 412 | uint32_t i; |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 413 | struct tfm_conn_handle_t *conn_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 414 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 415 | TFM_CORE_ASSERT(msg); |
| 416 | TFM_CORE_ASSERT(service); |
| 417 | TFM_CORE_ASSERT(!(invec == NULL && in_len != 0)); |
| 418 | TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0)); |
| 419 | TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC); |
| 420 | TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC); |
| 421 | TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 422 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 423 | /* Clear message buffer before using it */ |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 424 | spm_memset(&msg->msg, 0, sizeof(psa_msg_t)); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 425 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 426 | THRD_SYNC_INIT(&msg->ack_evnt); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 427 | msg->magic = TFM_MSG_MAGIC; |
| 428 | msg->service = service; |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 429 | msg->p_client = GET_CURRENT_COMPONENT(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 430 | msg->caller_outvec = caller_outvec; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 431 | msg->msg.client_id = client_id; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 432 | |
| 433 | /* Copy contents */ |
| 434 | msg->msg.type = type; |
| 435 | |
| 436 | for (i = 0; i < in_len; i++) { |
| 437 | msg->msg.in_size[i] = invec[i].len; |
| 438 | msg->invec[i].base = invec[i].base; |
| 439 | } |
| 440 | |
| 441 | for (i = 0; i < out_len; i++) { |
| 442 | msg->msg.out_size[i] = outvec[i].len; |
| 443 | msg->outvec[i].base = outvec[i].base; |
| 444 | /* Out len is used to record the writed number, set 0 here again */ |
| 445 | msg->outvec[i].len = 0; |
| 446 | } |
| 447 | |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 448 | /* Use the user connect handle as the message handle */ |
| 449 | msg->msg.handle = handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 450 | |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 451 | conn_handle = tfm_spm_to_handle_instance(handle); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 452 | /* For connected handle, set rhandle to every message */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 453 | if (conn_handle) { |
| 454 | msg->msg.rhandle = tfm_spm_get_rhandle(service, conn_handle); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 455 | } |
David Hu | 46603dd | 2019-12-11 18:05:16 +0800 | [diff] [blame] | 456 | |
| 457 | /* Set the private data of NSPE client caller in multi-core topology */ |
| 458 | if (TFM_CLIENT_ID_IS_NS(client_id)) { |
| 459 | tfm_rpc_set_caller_data(msg, client_id); |
| 460 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 461 | } |
| 462 | |
Kevin Peng | eec41a8 | 2021-08-18 13:56:23 +0800 | [diff] [blame] | 463 | int32_t tfm_spm_partition_get_running_partition_id(void) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 464 | { |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 465 | struct partition_t *partition; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 466 | |
Kevin Peng | cdf3ae2 | 2022-01-19 14:53:52 +0800 | [diff] [blame] | 467 | partition = GET_CURRENT_COMPONENT(); |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 468 | if (partition && partition->p_ldinf) { |
| 469 | return partition->p_ldinf->pid; |
Kevin Peng | 79c2bda | 2020-07-24 16:31:12 +0800 | [diff] [blame] | 470 | } else { |
| 471 | return INVALID_PARTITION_ID; |
| 472 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 473 | } |
| 474 | |
Summer Qin | 43c185d | 2019-10-10 15:44:42 +0800 | [diff] [blame] | 475 | int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller, |
Summer Qin | eb537e5 | 2019-03-29 09:57:10 +0800 | [diff] [blame] | 476 | enum tfm_memory_access_e access, |
| 477 | uint32_t privileged) |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 478 | { |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 479 | enum tfm_hal_status_t err; |
| 480 | uint32_t attr = 0; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 481 | |
| 482 | /* If len is zero, this indicates an empty buffer and base is ignored */ |
| 483 | if (len == 0) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 484 | return SPM_SUCCESS; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | if (!buffer) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 488 | return SPM_ERROR_BAD_PARAMETERS; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | if ((uintptr_t)buffer > (UINTPTR_MAX - len)) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 492 | return SPM_ERROR_MEMORY_CHECK; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 493 | } |
| 494 | |
Summer Qin | 424d4db | 2019-03-25 14:09:51 +0800 | [diff] [blame] | 495 | if (access == TFM_MEMORY_ACCESS_RW) { |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 496 | attr |= (TFM_HAL_ACCESS_READABLE | TFM_HAL_ACCESS_WRITABLE); |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 497 | } else { |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 498 | attr |= TFM_HAL_ACCESS_READABLE; |
Summer Qin | 424d4db | 2019-03-25 14:09:51 +0800 | [diff] [blame] | 499 | } |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 500 | |
| 501 | if (privileged == TFM_PARTITION_UNPRIVILEGED_MODE) { |
| 502 | attr |= TFM_HAL_ACCESS_UNPRIVILEGED; |
| 503 | } else { |
| 504 | attr &= ~TFM_HAL_ACCESS_UNPRIVILEGED; |
| 505 | } |
| 506 | |
| 507 | if (ns_caller) { |
| 508 | attr |= TFM_HAL_ACCESS_NS; |
| 509 | } |
| 510 | |
| 511 | err = tfm_hal_memory_has_access((uintptr_t)buffer, len, attr); |
| 512 | |
| 513 | if (err == TFM_HAL_SUCCESS) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 514 | return SPM_SUCCESS; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 515 | } |
| 516 | |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 517 | return SPM_ERROR_MEMORY_CHECK; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 518 | } |
| 519 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 520 | bool tfm_spm_is_ns_caller(void) |
| 521 | { |
Kevin Peng | cdf3ae2 | 2022-01-19 14:53:52 +0800 | [diff] [blame] | 522 | struct partition_t *partition = GET_CURRENT_COMPONENT(); |
Mingyang Sun | e529e3b | 2021-07-12 14:46:30 +0800 | [diff] [blame] | 523 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 524 | if (!partition) { |
| 525 | tfm_core_panic(); |
| 526 | } |
| 527 | |
| 528 | return (partition->p_ldinf->pid == TFM_SP_NON_SECURE_ID); |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 529 | } |
| 530 | |
Kevin Peng | 385fda8 | 2021-08-18 10:41:19 +0800 | [diff] [blame] | 531 | int32_t tfm_spm_get_client_id(bool ns_caller) |
| 532 | { |
| 533 | int32_t client_id; |
| 534 | |
| 535 | if (ns_caller) { |
| 536 | client_id = tfm_nspm_get_current_client_id(); |
| 537 | } else { |
| 538 | client_id = tfm_spm_partition_get_running_partition_id(); |
| 539 | } |
| 540 | |
| 541 | if (ns_caller != (client_id < 0)) { |
| 542 | /* NS client ID must be negative and Secure ID must >= 0 */ |
| 543 | tfm_core_panic(); |
| 544 | } |
| 545 | |
| 546 | return client_id; |
| 547 | } |
| 548 | |
Ken Liu | ce2692d | 2020-02-11 12:39:36 +0800 | [diff] [blame] | 549 | uint32_t tfm_spm_init(void) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 550 | { |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 551 | struct partition_t *partition; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 552 | const struct partition_load_info_t *p_pldi; |
| 553 | uint32_t service_setting = 0; |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 554 | |
David Hu | f07e97d | 2021-02-15 22:05:40 +0800 | [diff] [blame] | 555 | #ifdef TFM_FIH_PROFILE_ON |
| 556 | fih_int fih_rc = FIH_FAILURE; |
| 557 | #endif |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 558 | |
| 559 | tfm_pool_init(conn_handle_pool, |
| 560 | POOL_BUFFER_SIZE(conn_handle_pool), |
| 561 | sizeof(struct tfm_conn_handle_t), |
Sherry Zhang | 86a71c6 | 2022-01-12 18:19:02 +0800 | [diff] [blame] | 562 | CONFIG_TFM_CONN_HANDLE_MAX_NUM); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 563 | |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame^] | 564 | UNI_LISI_INIT_NODE(PARTITION_LIST_ADDR, next); |
| 565 | UNI_LISI_INIT_NODE(&services_listhead, next); |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 566 | |
Sherry Zhnag | 482b88b | 2021-08-19 17:51:47 +0800 | [diff] [blame] | 567 | /* Init the nonsecure context. */ |
Chris Brand | 56b4d0c | 2021-12-17 16:15:58 -0800 | [diff] [blame] | 568 | tfm_nspm_ctx_init(); |
Sherry Zhnag | 482b88b | 2021-08-19 17:51:47 +0800 | [diff] [blame] | 569 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 570 | while (1) { |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 571 | partition = load_a_partition_assuredly(PARTITION_LIST_ADDR); |
Shawn Shan | 2333106 | 2021-09-01 14:58:21 +0800 | [diff] [blame] | 572 | if (partition == NO_MORE_PARTITION) { |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 573 | break; |
Kevin Peng | 79c2bda | 2020-07-24 16:31:12 +0800 | [diff] [blame] | 574 | } |
| 575 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 576 | p_pldi = partition->p_ldinf; |
Mingyang Sun | 2b35266 | 2021-04-21 11:35:43 +0800 | [diff] [blame] | 577 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 578 | if (p_pldi->nservices) { |
| 579 | service_setting = load_services_assuredly( |
| 580 | partition, |
| 581 | &services_listhead, |
| 582 | stateless_services_ref_tbl, |
| 583 | sizeof(stateless_services_ref_tbl)); |
Kevin Peng | 27e4227 | 2021-05-24 17:58:53 +0800 | [diff] [blame] | 584 | } |
| 585 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 586 | if (p_pldi->nirqs) { |
Kevin Peng | 27e4227 | 2021-05-24 17:58:53 +0800 | [diff] [blame] | 587 | load_irqs_assuredly(partition); |
| 588 | } |
| 589 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 590 | /* Bind the partition with platform. */ |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 591 | #if TFM_FIH_PROFILE_ON |
| 592 | FIH_CALL(tfm_hal_bind_boundaries, fih_rc, partition->p_ldinf, |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 593 | &partition->p_boundaries); |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 594 | if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) { |
| 595 | tfm_core_panic(); |
Mingyang Sun | 61f8fbc | 2021-06-04 17:49:56 +0800 | [diff] [blame] | 596 | } |
Ken Liu | 8668628 | 2021-04-27 11:11:15 +0800 | [diff] [blame] | 597 | #else /* TFM_FIH_PROFILE_ON */ |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 598 | if (tfm_hal_bind_boundaries(partition->p_ldinf, |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 599 | &partition->p_boundaries) |
| 600 | != TFM_HAL_SUCCESS) { |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 601 | tfm_core_panic(); |
Mate Toth-Pal | 8ac98a7 | 2019-11-21 17:30:10 +0100 | [diff] [blame] | 602 | } |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 603 | #endif /* TFM_FIH_PROFILE_ON */ |
Mate Toth-Pal | 8ac98a7 | 2019-11-21 17:30:10 +0100 | [diff] [blame] | 604 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 605 | backend_instance.comp_init_assuredly(partition, service_setting); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 606 | } |
| 607 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 608 | return backend_instance.system_run(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 609 | } |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 610 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 611 | uint64_t do_schedule(void) |
| 612 | { |
Ken Liu | 086b32c | 2021-12-27 14:44:52 +0800 | [diff] [blame] | 613 | AAPCS_DUAL_U32_T ctx_ctrls; |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 614 | struct partition_t *p_part_curr, *p_part_next; |
| 615 | struct thread_t *pth_next = thrd_next(); |
Kevin Peng | ca59ec0 | 2021-12-09 14:35:50 +0800 | [diff] [blame] | 616 | struct critical_section_t cs = CRITICAL_SECTION_STATIC_INIT; |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 617 | |
Ken Liu | 086b32c | 2021-12-27 14:44:52 +0800 | [diff] [blame] | 618 | AAPCS_DUAL_U32_SET(ctx_ctrls, (uint32_t)CURRENT_THREAD->p_context_ctrl, |
| 619 | (uint32_t)CURRENT_THREAD->p_context_ctrl); |
| 620 | |
Mingyang Sun | 4f08f4d | 2021-09-10 17:41:28 +0800 | [diff] [blame] | 621 | p_part_curr = GET_THRD_OWNER(CURRENT_THREAD); |
| 622 | p_part_next = GET_THRD_OWNER(pth_next); |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 623 | |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 624 | if (scheduler_lock != SCHEDULER_LOCKED && pth_next != NULL && |
| 625 | p_part_curr != p_part_next) { |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 626 | /* Check if there is enough room on stack to save more context */ |
| 627 | if ((p_part_curr->ctx_ctrl.sp_limit + |
| 628 | sizeof(struct tfm_additional_context_t)) > __get_PSP()) { |
| 629 | tfm_core_panic(); |
| 630 | } |
Mate Toth-Pal | c430b99 | 2019-05-09 21:01:14 +0200 | [diff] [blame] | 631 | |
Kevin Peng | ca59ec0 | 2021-12-09 14:35:50 +0800 | [diff] [blame] | 632 | CRITICAL_SECTION_ENTER(cs); |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 633 | /* |
| 634 | * If required, let the platform update boundary based on its |
| 635 | * implementation. Change privilege, MPU or other configurations. |
| 636 | */ |
| 637 | if (p_part_curr->p_boundaries != p_part_next->p_boundaries) { |
| 638 | if (tfm_hal_update_boundaries(p_part_next->p_ldinf, |
| 639 | p_part_next->p_boundaries) |
| 640 | != TFM_HAL_SUCCESS) { |
| 641 | tfm_core_panic(); |
| 642 | } |
| 643 | } |
Feder Liang | 42f5b56 | 2021-09-10 17:38:36 +0800 | [diff] [blame] | 644 | ARCH_FLUSH_FP_CONTEXT(); |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 645 | |
Ken Liu | 086b32c | 2021-12-27 14:44:52 +0800 | [diff] [blame] | 646 | AAPCS_DUAL_U32_SET_A1(ctx_ctrls, (uint32_t)pth_next->p_context_ctrl); |
| 647 | |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 648 | CURRENT_THREAD = pth_next; |
Kevin Peng | ca59ec0 | 2021-12-09 14:35:50 +0800 | [diff] [blame] | 649 | CRITICAL_SECTION_LEAVE(cs); |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 650 | } |
David Hu | fb38d56 | 2019-09-23 15:58:34 +0800 | [diff] [blame] | 651 | |
Ken Liu | 086b32c | 2021-12-27 14:44:52 +0800 | [diff] [blame] | 652 | return AAPCS_DUAL_U32_AS_U64(ctx_ctrls); |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 653 | } |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 654 | |
Summer Qin | 02f7f07 | 2020-08-24 16:02:54 +0800 | [diff] [blame] | 655 | void update_caller_outvec_len(struct tfm_msg_body_t *msg) |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 656 | { |
| 657 | uint32_t i; |
| 658 | |
| 659 | /* |
| 660 | * FixeMe: abstract these part into dedicated functions to avoid |
| 661 | * accessing thread context in psa layer |
| 662 | */ |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 663 | /* |
| 664 | * If it is a NS request via RPC, the owner of this message is not set. |
| 665 | * Or if it is a SFN message, it does not have owner thread state either. |
| 666 | */ |
| 667 | if ((!is_tfm_rpc_msg(msg)) && (msg->sfn_magic != TFM_MSG_MAGIC_SFN)) { |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 668 | TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK); |
| 669 | } |
| 670 | |
| 671 | for (i = 0; i < PSA_MAX_IOVEC; i++) { |
| 672 | if (msg->msg.out_size[i] == 0) { |
| 673 | continue; |
| 674 | } |
| 675 | |
| 676 | TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base); |
| 677 | |
| 678 | msg->caller_outvec[i].len = msg->outvec[i].len; |
| 679 | } |
| 680 | } |
| 681 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 682 | void spm_assert_signal(void *p_pt, psa_signal_t signal) |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 683 | { |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 684 | struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT; |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 685 | struct partition_t *partition = (struct partition_t *)p_pt; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 686 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 687 | if (!partition) { |
| 688 | tfm_core_panic(); |
| 689 | } |
| 690 | |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 691 | CRITICAL_SECTION_ENTER(cs_assert); |
| 692 | |
Mingyang Sun | af22ffa | 2020-07-09 17:48:37 +0800 | [diff] [blame] | 693 | partition->signals_asserted |= signal; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 694 | |
Kevin Peng | 8dac610 | 2021-03-09 16:44:00 +0800 | [diff] [blame] | 695 | if (partition->signals_waiting & signal) { |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 696 | thrd_wake_up(&partition->waitobj, |
| 697 | partition->signals_asserted & partition->signals_waiting); |
Kevin Peng | 8dac610 | 2021-03-09 16:44:00 +0800 | [diff] [blame] | 698 | partition->signals_waiting &= ~signal; |
| 699 | } |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 700 | |
| 701 | CRITICAL_SECTION_LEAVE(cs_assert); |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 702 | } |