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 | |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 44 | /* Partition and service runtime data list head/runtime data table */ |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 45 | static struct service_head_t services_listhead; |
Ken Liu | b3b2cb6 | 2021-05-22 00:39:28 +0800 | [diff] [blame] | 46 | struct service_t *stateless_services_ref_tbl[STATIC_HANDLE_NUM_LIMIT]; |
Summer Qin | d99509f | 2019-08-02 17:36:58 +0800 | [diff] [blame] | 47 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 48 | /* Pools */ |
| 49 | TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t), |
| 50 | TFM_CONN_HANDLE_MAX_NUM); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 51 | |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 52 | extern uint32_t scheduler_lock; |
| 53 | |
Summer Qin | 373feb1 | 2020-03-27 15:35:33 +0800 | [diff] [blame] | 54 | /*********************** Connection handle conversion APIs *******************/ |
| 55 | |
Summer Qin | 373feb1 | 2020-03-27 15:35:33 +0800 | [diff] [blame] | 56 | #define CONVERSION_FACTOR_BITOFFSET 3 |
| 57 | #define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET) |
| 58 | /* Set 32 as the maximum */ |
| 59 | #define CONVERSION_FACTOR_VALUE_MAX 0x20 |
| 60 | |
| 61 | #if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX |
| 62 | #error "CONVERSION FACTOR OUT OF RANGE" |
| 63 | #endif |
| 64 | |
| 65 | static uint32_t loop_index; |
| 66 | |
| 67 | /* |
| 68 | * A handle instance psa_handle_t allocated inside SPM is actually a memory |
| 69 | * address among the handle pool. Return this handle to the client directly |
| 70 | * exposes information of secure memory address. In this case, converting the |
| 71 | * handle into another value does not represent the memory address to avoid |
| 72 | * exposing secure memory directly to clients. |
| 73 | * |
| 74 | * This function converts the handle instance into another value by scaling the |
| 75 | * handle in pool offset, the converted value is named as a user handle. |
| 76 | * |
| 77 | * The formula: |
| 78 | * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE + |
| 79 | * CLIENT_HANDLE_VALUE_MIN + loop_index |
| 80 | * where: |
| 81 | * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not |
| 82 | * exceed CONVERSION_FACTOR_VALUE_MAX. |
| 83 | * |
| 84 | * handle_instance in RANGE[POOL_START, POOL_END] |
| 85 | * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF] |
| 86 | * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1] |
| 87 | * |
| 88 | * note: |
| 89 | * loop_index is used to promise same handle instance is converted into |
| 90 | * different user handles in short time. |
| 91 | */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 92 | 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] | 93 | { |
| 94 | psa_handle_t user_handle; |
| 95 | |
| 96 | loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE; |
| 97 | user_handle = (psa_handle_t)((((uintptr_t)handle_instance - |
| 98 | (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) + |
| 99 | CLIENT_HANDLE_VALUE_MIN + loop_index); |
| 100 | |
| 101 | return user_handle; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * This function converts a user handle into a corresponded handle instance. |
| 106 | * The converted value is validated before returning, an invalid handle instance |
| 107 | * is returned as NULL. |
| 108 | * |
| 109 | * The formula: |
| 110 | * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) / |
| 111 | * CONVERSION_FACTOR_VALUE) + POOL_START |
| 112 | * where: |
| 113 | * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not |
| 114 | * exceed CONVERSION_FACTOR_VALUE_MAX. |
| 115 | * |
| 116 | * handle_instance in RANGE[POOL_START, POOL_END] |
| 117 | * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF] |
| 118 | * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1] |
| 119 | */ |
| 120 | struct tfm_conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle) |
| 121 | { |
| 122 | struct tfm_conn_handle_t *handle_instance; |
| 123 | |
| 124 | if (user_handle == PSA_NULL_HANDLE) { |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | handle_instance = (struct tfm_conn_handle_t *)((((uintptr_t)user_handle - |
| 129 | CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) + |
| 130 | (uintptr_t)conn_handle_pool); |
| 131 | |
| 132 | return handle_instance; |
| 133 | } |
| 134 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 135 | /* Service handle management functions */ |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 136 | struct tfm_conn_handle_t *tfm_spm_create_conn_handle(struct service_t *service, |
| 137 | int32_t client_id) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 138 | { |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 139 | struct tfm_conn_handle_t *p_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 140 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 141 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 142 | |
| 143 | /* Get buffer for handle list structure from handle pool */ |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 144 | p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool); |
| 145 | if (!p_handle) { |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 146 | return NULL; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 147 | } |
| 148 | |
Kevin Peng | 2cc2572 | 2021-11-18 11:46:02 +0800 | [diff] [blame] | 149 | spm_memset(p_handle, 0, sizeof(*p_handle)); |
| 150 | |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 151 | p_handle->internal_msg.service = service; |
Shawn Shan | cc39fcb | 2019-11-13 15:38:16 +0800 | [diff] [blame] | 152 | p_handle->status = TFM_HANDLE_STATUS_IDLE; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 153 | p_handle->client_id = client_id; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 154 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 155 | return p_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 156 | } |
| 157 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 158 | int32_t tfm_spm_validate_conn_handle( |
| 159 | const struct tfm_conn_handle_t *conn_handle, |
| 160 | int32_t client_id) |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 161 | { |
| 162 | /* Check the handle address is validated */ |
| 163 | if (is_valid_chunk_data_in_pool(conn_handle_pool, |
| 164 | (uint8_t *)conn_handle) != true) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 165 | return SPM_ERROR_GENERIC; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* Check the handle caller is correct */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 169 | if (conn_handle->client_id != client_id) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 170 | return SPM_ERROR_GENERIC; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 171 | } |
| 172 | |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 173 | return SPM_SUCCESS; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 174 | } |
| 175 | |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 176 | int32_t tfm_spm_free_conn_handle(struct service_t *service, |
Summer Qin | 02f7f07 | 2020-08-24 16:02:54 +0800 | [diff] [blame] | 177 | struct tfm_conn_handle_t *conn_handle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 178 | { |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 179 | struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT; |
| 180 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 181 | TFM_CORE_ASSERT(service); |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 182 | TFM_CORE_ASSERT(conn_handle != NULL); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 183 | |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 184 | /* Clear magic as the handler is not used anymore */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 185 | conn_handle->internal_msg.magic = 0; |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 186 | |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 187 | CRITICAL_SECTION_ENTER(cs_assert); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 188 | |
| 189 | /* Back handle buffer to pool */ |
Ken Liu | 66ca613 | 2021-02-24 08:49:51 +0800 | [diff] [blame] | 190 | tfm_pool_free(conn_handle_pool, conn_handle); |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 191 | CRITICAL_SECTION_LEAVE(cs_assert); |
| 192 | |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 193 | return SPM_SUCCESS; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 194 | } |
| 195 | |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 196 | int32_t tfm_spm_set_rhandle(struct service_t *service, |
Summer Qin | 02f7f07 | 2020-08-24 16:02:54 +0800 | [diff] [blame] | 197 | struct tfm_conn_handle_t *conn_handle, |
| 198 | void *rhandle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 199 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 200 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 201 | /* Set reverse handle value only be allowed for a connected handle */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 202 | TFM_CORE_ASSERT(conn_handle != NULL); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 203 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 204 | conn_handle->rhandle = rhandle; |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 205 | return SPM_SUCCESS; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 206 | } |
| 207 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 208 | /** |
Xinyu Zhang | 3a45324 | 2021-04-16 17:57:09 +0800 | [diff] [blame] | 209 | * \brief Get reverse handle value from connection handle. |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 210 | * |
| 211 | * \param[in] service Target service context pointer |
| 212 | * \param[in] conn_handle Connection handle created by |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 213 | * tfm_spm_create_conn_handle() |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 214 | * |
| 215 | * \retval void * Success |
| 216 | * \retval "Does not return" Panic for those: |
| 217 | * service pointer are NULL |
Xinyu Zhang | 3a45324 | 2021-04-16 17:57:09 +0800 | [diff] [blame] | 218 | * handle is \ref PSA_NULL_HANDLE |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 219 | * handle node does not be found |
| 220 | */ |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 221 | static void *tfm_spm_get_rhandle(struct service_t *service, |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 222 | struct tfm_conn_handle_t *conn_handle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 223 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 224 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 225 | /* Get reverse handle value only be allowed for a connected handle */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 226 | TFM_CORE_ASSERT(conn_handle != NULL); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 227 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 228 | return conn_handle->rhandle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /* Partition management functions */ |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 232 | |
Summer Qin | 02f7f07 | 2020-08-24 16:02:54 +0800 | [diff] [blame] | 233 | struct tfm_msg_body_t *tfm_spm_get_msg_by_signal(struct partition_t *partition, |
| 234 | psa_signal_t signal) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 235 | { |
Ken Liu | 2c47f7f | 2021-01-22 11:06:04 +0800 | [diff] [blame] | 236 | struct bi_list_node_t *node, *head; |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 237 | struct tfm_msg_body_t *tmp_msg, *msg = NULL; |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 238 | struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 239 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 240 | TFM_CORE_ASSERT(partition); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 241 | |
Mingyang Sun | af22ffa | 2020-07-09 17:48:37 +0800 | [diff] [blame] | 242 | head = &partition->msg_list; |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 243 | |
Ken Liu | 2c47f7f | 2021-01-22 11:06:04 +0800 | [diff] [blame] | 244 | if (BI_LIST_IS_EMPTY(head)) { |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 245 | return NULL; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 246 | } |
| 247 | |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 248 | /* |
| 249 | * There may be multiple messages for this RoT Service signal, do not clear |
| 250 | * partition mask until no remaining message. Search may be optimized. |
| 251 | */ |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 252 | CRITICAL_SECTION_ENTER(cs_assert); |
Ken Liu | 2c47f7f | 2021-01-22 11:06:04 +0800 | [diff] [blame] | 253 | BI_LIST_FOR_EACH(node, head) { |
Ken Liu | c25558c | 2021-05-20 15:31:28 +0800 | [diff] [blame] | 254 | tmp_msg = TO_CONTAINER(node, struct tfm_msg_body_t, msg_node); |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 255 | if (tmp_msg->service->p_ldinf->signal == signal && msg) { |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 256 | CRITICAL_SECTION_LEAVE(cs_assert); |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 257 | return msg; |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 258 | } else if (tmp_msg->service->p_ldinf->signal == signal) { |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 259 | msg = tmp_msg; |
Ken Liu | 2c47f7f | 2021-01-22 11:06:04 +0800 | [diff] [blame] | 260 | BI_LIST_REMOVE_NODE(node); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 261 | } |
| 262 | } |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 263 | |
Mingyang Sun | af22ffa | 2020-07-09 17:48:37 +0800 | [diff] [blame] | 264 | partition->signals_asserted &= ~signal; |
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 | |
| 267 | return msg; |
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 | |
Feder.Liang | aeb5a79 | 2021-08-10 16:12:56 +0800 | [diff] [blame] | 274 | UNI_LIST_FOR_EACH_PREV(p_prev, p_curr, &services_listhead) { |
| 275 | if (p_curr->p_ldinf->sid == sid) { |
| 276 | UNI_LIST_MOVE_AFTER(&services_listhead, p_prev, p_curr); |
| 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 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 297 | UNI_LIST_FOR_EACH(p_part, PARTITION_LIST_ADDR) { |
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 | |
Kevin Peng | df6aa29 | 2021-03-11 17:58:50 +0800 | [diff] [blame] | 404 | struct tfm_msg_body_t * |
| 405 | tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle) |
| 406 | { |
| 407 | TFM_CORE_ASSERT(conn_handle != NULL); |
| 408 | |
| 409 | return &(conn_handle->internal_msg); |
| 410 | } |
| 411 | |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 412 | void tfm_spm_fill_msg(struct tfm_msg_body_t *msg, |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 413 | struct service_t *service, |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 414 | psa_handle_t handle, |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 415 | int32_t type, int32_t client_id, |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 416 | psa_invec *invec, size_t in_len, |
| 417 | psa_outvec *outvec, size_t out_len, |
| 418 | psa_outvec *caller_outvec) |
| 419 | { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 420 | uint32_t i; |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 421 | struct tfm_conn_handle_t *conn_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 422 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 423 | TFM_CORE_ASSERT(msg); |
| 424 | TFM_CORE_ASSERT(service); |
| 425 | TFM_CORE_ASSERT(!(invec == NULL && in_len != 0)); |
| 426 | TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0)); |
| 427 | TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC); |
| 428 | TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC); |
| 429 | TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 430 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 431 | /* Clear message buffer before using it */ |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 432 | spm_memset(&msg->msg, 0, sizeof(psa_msg_t)); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 433 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 434 | THRD_SYNC_INIT(&msg->ack_evnt); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 435 | msg->magic = TFM_MSG_MAGIC; |
| 436 | msg->service = service; |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 437 | msg->p_client = GET_CURRENT_COMPONENT(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 438 | msg->caller_outvec = caller_outvec; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 439 | msg->msg.client_id = client_id; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 440 | |
| 441 | /* Copy contents */ |
| 442 | msg->msg.type = type; |
| 443 | |
| 444 | for (i = 0; i < in_len; i++) { |
| 445 | msg->msg.in_size[i] = invec[i].len; |
| 446 | msg->invec[i].base = invec[i].base; |
| 447 | } |
| 448 | |
| 449 | for (i = 0; i < out_len; i++) { |
| 450 | msg->msg.out_size[i] = outvec[i].len; |
| 451 | msg->outvec[i].base = outvec[i].base; |
| 452 | /* Out len is used to record the writed number, set 0 here again */ |
| 453 | msg->outvec[i].len = 0; |
| 454 | } |
| 455 | |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 456 | /* Use the user connect handle as the message handle */ |
| 457 | msg->msg.handle = handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 458 | |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 459 | conn_handle = tfm_spm_to_handle_instance(handle); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 460 | /* For connected handle, set rhandle to every message */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 461 | if (conn_handle) { |
| 462 | msg->msg.rhandle = tfm_spm_get_rhandle(service, conn_handle); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 463 | } |
David Hu | 46603dd | 2019-12-11 18:05:16 +0800 | [diff] [blame] | 464 | |
| 465 | /* Set the private data of NSPE client caller in multi-core topology */ |
| 466 | if (TFM_CLIENT_ID_IS_NS(client_id)) { |
| 467 | tfm_rpc_set_caller_data(msg, client_id); |
| 468 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 469 | } |
| 470 | |
Kevin Peng | eec41a8 | 2021-08-18 13:56:23 +0800 | [diff] [blame] | 471 | int32_t tfm_spm_partition_get_running_partition_id(void) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 472 | { |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 473 | struct partition_t *partition; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 474 | |
Kevin Peng | cdf3ae2 | 2022-01-19 14:53:52 +0800 | [diff] [blame^] | 475 | partition = GET_CURRENT_COMPONENT(); |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 476 | if (partition && partition->p_ldinf) { |
| 477 | return partition->p_ldinf->pid; |
Kevin Peng | 79c2bda | 2020-07-24 16:31:12 +0800 | [diff] [blame] | 478 | } else { |
| 479 | return INVALID_PARTITION_ID; |
| 480 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 481 | } |
| 482 | |
Summer Qin | 43c185d | 2019-10-10 15:44:42 +0800 | [diff] [blame] | 483 | 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] | 484 | enum tfm_memory_access_e access, |
| 485 | uint32_t privileged) |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 486 | { |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 487 | enum tfm_hal_status_t err; |
| 488 | uint32_t attr = 0; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 489 | |
| 490 | /* If len is zero, this indicates an empty buffer and base is ignored */ |
| 491 | if (len == 0) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 492 | return SPM_SUCCESS; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | if (!buffer) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 496 | return SPM_ERROR_BAD_PARAMETERS; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | if ((uintptr_t)buffer > (UINTPTR_MAX - len)) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 500 | return SPM_ERROR_MEMORY_CHECK; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 501 | } |
| 502 | |
Summer Qin | 424d4db | 2019-03-25 14:09:51 +0800 | [diff] [blame] | 503 | if (access == TFM_MEMORY_ACCESS_RW) { |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 504 | attr |= (TFM_HAL_ACCESS_READABLE | TFM_HAL_ACCESS_WRITABLE); |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 505 | } else { |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 506 | attr |= TFM_HAL_ACCESS_READABLE; |
Summer Qin | 424d4db | 2019-03-25 14:09:51 +0800 | [diff] [blame] | 507 | } |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 508 | |
| 509 | if (privileged == TFM_PARTITION_UNPRIVILEGED_MODE) { |
| 510 | attr |= TFM_HAL_ACCESS_UNPRIVILEGED; |
| 511 | } else { |
| 512 | attr &= ~TFM_HAL_ACCESS_UNPRIVILEGED; |
| 513 | } |
| 514 | |
| 515 | if (ns_caller) { |
| 516 | attr |= TFM_HAL_ACCESS_NS; |
| 517 | } |
| 518 | |
| 519 | err = tfm_hal_memory_has_access((uintptr_t)buffer, len, attr); |
| 520 | |
| 521 | if (err == TFM_HAL_SUCCESS) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 522 | return SPM_SUCCESS; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 523 | } |
| 524 | |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 525 | return SPM_ERROR_MEMORY_CHECK; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 526 | } |
| 527 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 528 | bool tfm_spm_is_ns_caller(void) |
| 529 | { |
Kevin Peng | cdf3ae2 | 2022-01-19 14:53:52 +0800 | [diff] [blame^] | 530 | struct partition_t *partition = GET_CURRENT_COMPONENT(); |
Mingyang Sun | e529e3b | 2021-07-12 14:46:30 +0800 | [diff] [blame] | 531 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 532 | if (!partition) { |
| 533 | tfm_core_panic(); |
| 534 | } |
| 535 | |
| 536 | return (partition->p_ldinf->pid == TFM_SP_NON_SECURE_ID); |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 537 | } |
| 538 | |
Kevin Peng | 385fda8 | 2021-08-18 10:41:19 +0800 | [diff] [blame] | 539 | int32_t tfm_spm_get_client_id(bool ns_caller) |
| 540 | { |
| 541 | int32_t client_id; |
| 542 | |
| 543 | if (ns_caller) { |
| 544 | client_id = tfm_nspm_get_current_client_id(); |
| 545 | } else { |
| 546 | client_id = tfm_spm_partition_get_running_partition_id(); |
| 547 | } |
| 548 | |
| 549 | if (ns_caller != (client_id < 0)) { |
| 550 | /* NS client ID must be negative and Secure ID must >= 0 */ |
| 551 | tfm_core_panic(); |
| 552 | } |
| 553 | |
| 554 | return client_id; |
| 555 | } |
| 556 | |
Ken Liu | ce2692d | 2020-02-11 12:39:36 +0800 | [diff] [blame] | 557 | uint32_t tfm_spm_init(void) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 558 | { |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 559 | struct partition_t *partition; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 560 | const struct partition_load_info_t *p_pldi; |
| 561 | uint32_t service_setting = 0; |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 562 | |
David Hu | f07e97d | 2021-02-15 22:05:40 +0800 | [diff] [blame] | 563 | #ifdef TFM_FIH_PROFILE_ON |
| 564 | fih_int fih_rc = FIH_FAILURE; |
| 565 | #endif |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 566 | |
| 567 | tfm_pool_init(conn_handle_pool, |
| 568 | POOL_BUFFER_SIZE(conn_handle_pool), |
| 569 | sizeof(struct tfm_conn_handle_t), |
| 570 | TFM_CONN_HANDLE_MAX_NUM); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 571 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 572 | UNI_LISI_INIT_HEAD(PARTITION_LIST_ADDR); |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 573 | UNI_LISI_INIT_HEAD(&services_listhead); |
| 574 | |
Sherry Zhnag | 482b88b | 2021-08-19 17:51:47 +0800 | [diff] [blame] | 575 | /* Init the nonsecure context. */ |
Chris Brand | 56b4d0c | 2021-12-17 16:15:58 -0800 | [diff] [blame] | 576 | tfm_nspm_ctx_init(); |
Sherry Zhnag | 482b88b | 2021-08-19 17:51:47 +0800 | [diff] [blame] | 577 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 578 | while (1) { |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 579 | partition = load_a_partition_assuredly(PARTITION_LIST_ADDR); |
Shawn Shan | 2333106 | 2021-09-01 14:58:21 +0800 | [diff] [blame] | 580 | if (partition == NO_MORE_PARTITION) { |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 581 | break; |
Kevin Peng | 79c2bda | 2020-07-24 16:31:12 +0800 | [diff] [blame] | 582 | } |
| 583 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 584 | p_pldi = partition->p_ldinf; |
Mingyang Sun | 2b35266 | 2021-04-21 11:35:43 +0800 | [diff] [blame] | 585 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 586 | if (p_pldi->nservices) { |
| 587 | service_setting = load_services_assuredly( |
| 588 | partition, |
| 589 | &services_listhead, |
| 590 | stateless_services_ref_tbl, |
| 591 | sizeof(stateless_services_ref_tbl)); |
Kevin Peng | 27e4227 | 2021-05-24 17:58:53 +0800 | [diff] [blame] | 592 | } |
| 593 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 594 | if (p_pldi->nirqs) { |
Kevin Peng | 27e4227 | 2021-05-24 17:58:53 +0800 | [diff] [blame] | 595 | load_irqs_assuredly(partition); |
| 596 | } |
| 597 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 598 | /* Bind the partition with platform. */ |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 599 | #if TFM_FIH_PROFILE_ON |
| 600 | FIH_CALL(tfm_hal_bind_boundaries, fih_rc, partition->p_ldinf, |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 601 | &partition->p_boundaries); |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 602 | if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) { |
| 603 | tfm_core_panic(); |
Mingyang Sun | 61f8fbc | 2021-06-04 17:49:56 +0800 | [diff] [blame] | 604 | } |
Ken Liu | 8668628 | 2021-04-27 11:11:15 +0800 | [diff] [blame] | 605 | #else /* TFM_FIH_PROFILE_ON */ |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 606 | if (tfm_hal_bind_boundaries(partition->p_ldinf, |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 607 | &partition->p_boundaries) |
| 608 | != TFM_HAL_SUCCESS) { |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 609 | tfm_core_panic(); |
Mate Toth-Pal | 8ac98a7 | 2019-11-21 17:30:10 +0100 | [diff] [blame] | 610 | } |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 611 | #endif /* TFM_FIH_PROFILE_ON */ |
Mate Toth-Pal | 8ac98a7 | 2019-11-21 17:30:10 +0100 | [diff] [blame] | 612 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 613 | backend_instance.comp_init_assuredly(partition, service_setting); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 614 | } |
| 615 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 616 | return backend_instance.system_run(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 617 | } |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 618 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 619 | uint64_t do_schedule(void) |
| 620 | { |
Ken Liu | 086b32c | 2021-12-27 14:44:52 +0800 | [diff] [blame] | 621 | AAPCS_DUAL_U32_T ctx_ctrls; |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 622 | struct partition_t *p_part_curr, *p_part_next; |
| 623 | struct thread_t *pth_next = thrd_next(); |
Kevin Peng | ca59ec0 | 2021-12-09 14:35:50 +0800 | [diff] [blame] | 624 | struct critical_section_t cs = CRITICAL_SECTION_STATIC_INIT; |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 625 | |
Ken Liu | 086b32c | 2021-12-27 14:44:52 +0800 | [diff] [blame] | 626 | AAPCS_DUAL_U32_SET(ctx_ctrls, (uint32_t)CURRENT_THREAD->p_context_ctrl, |
| 627 | (uint32_t)CURRENT_THREAD->p_context_ctrl); |
| 628 | |
Mingyang Sun | 4f08f4d | 2021-09-10 17:41:28 +0800 | [diff] [blame] | 629 | p_part_curr = GET_THRD_OWNER(CURRENT_THREAD); |
| 630 | p_part_next = GET_THRD_OWNER(pth_next); |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 631 | |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 632 | if (scheduler_lock != SCHEDULER_LOCKED && pth_next != NULL && |
| 633 | p_part_curr != p_part_next) { |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 634 | /* Check if there is enough room on stack to save more context */ |
| 635 | if ((p_part_curr->ctx_ctrl.sp_limit + |
| 636 | sizeof(struct tfm_additional_context_t)) > __get_PSP()) { |
| 637 | tfm_core_panic(); |
| 638 | } |
Mate Toth-Pal | c430b99 | 2019-05-09 21:01:14 +0200 | [diff] [blame] | 639 | |
Kevin Peng | ca59ec0 | 2021-12-09 14:35:50 +0800 | [diff] [blame] | 640 | CRITICAL_SECTION_ENTER(cs); |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 641 | /* |
| 642 | * If required, let the platform update boundary based on its |
| 643 | * implementation. Change privilege, MPU or other configurations. |
| 644 | */ |
| 645 | if (p_part_curr->p_boundaries != p_part_next->p_boundaries) { |
| 646 | if (tfm_hal_update_boundaries(p_part_next->p_ldinf, |
| 647 | p_part_next->p_boundaries) |
| 648 | != TFM_HAL_SUCCESS) { |
| 649 | tfm_core_panic(); |
| 650 | } |
| 651 | } |
Feder Liang | 42f5b56 | 2021-09-10 17:38:36 +0800 | [diff] [blame] | 652 | ARCH_FLUSH_FP_CONTEXT(); |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 653 | |
Ken Liu | 086b32c | 2021-12-27 14:44:52 +0800 | [diff] [blame] | 654 | AAPCS_DUAL_U32_SET_A1(ctx_ctrls, (uint32_t)pth_next->p_context_ctrl); |
| 655 | |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 656 | CURRENT_THREAD = pth_next; |
Kevin Peng | ca59ec0 | 2021-12-09 14:35:50 +0800 | [diff] [blame] | 657 | CRITICAL_SECTION_LEAVE(cs); |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 658 | } |
David Hu | fb38d56 | 2019-09-23 15:58:34 +0800 | [diff] [blame] | 659 | |
Ken Liu | 086b32c | 2021-12-27 14:44:52 +0800 | [diff] [blame] | 660 | return AAPCS_DUAL_U32_AS_U64(ctx_ctrls); |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 661 | } |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 662 | |
Summer Qin | 02f7f07 | 2020-08-24 16:02:54 +0800 | [diff] [blame] | 663 | void update_caller_outvec_len(struct tfm_msg_body_t *msg) |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 664 | { |
| 665 | uint32_t i; |
| 666 | |
| 667 | /* |
| 668 | * FixeMe: abstract these part into dedicated functions to avoid |
| 669 | * accessing thread context in psa layer |
| 670 | */ |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 671 | /* |
| 672 | * If it is a NS request via RPC, the owner of this message is not set. |
| 673 | * Or if it is a SFN message, it does not have owner thread state either. |
| 674 | */ |
| 675 | 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] | 676 | TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK); |
| 677 | } |
| 678 | |
| 679 | for (i = 0; i < PSA_MAX_IOVEC; i++) { |
| 680 | if (msg->msg.out_size[i] == 0) { |
| 681 | continue; |
| 682 | } |
| 683 | |
| 684 | TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base); |
| 685 | |
| 686 | msg->caller_outvec[i].len = msg->outvec[i].len; |
| 687 | } |
| 688 | } |
| 689 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 690 | void spm_assert_signal(void *p_pt, psa_signal_t signal) |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 691 | { |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 692 | struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT; |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 693 | struct partition_t *partition = (struct partition_t *)p_pt; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 694 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 695 | if (!partition) { |
| 696 | tfm_core_panic(); |
| 697 | } |
| 698 | |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 699 | CRITICAL_SECTION_ENTER(cs_assert); |
| 700 | |
Mingyang Sun | af22ffa | 2020-07-09 17:48:37 +0800 | [diff] [blame] | 701 | partition->signals_asserted |= signal; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 702 | |
Kevin Peng | 8dac610 | 2021-03-09 16:44:00 +0800 | [diff] [blame] | 703 | if (partition->signals_waiting & signal) { |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 704 | thrd_wake_up(&partition->waitobj, |
| 705 | partition->signals_asserted & partition->signals_waiting); |
Kevin Peng | 8dac610 | 2021-03-09 16:44:00 +0800 | [diff] [blame] | 706 | partition->signals_waiting &= ~signal; |
| 707 | } |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 708 | |
| 709 | CRITICAL_SECTION_LEAVE(cs_assert); |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 710 | } |