Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 1 | /* |
Kevin Peng | a20b5af | 2021-01-11 11:20:52 +0800 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, Arm Limited. All rights reserved. |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 7 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 8 | #include <inttypes.h> |
| 9 | #include <stdbool.h> |
Ken Liu | 24dffb2 | 2021-02-10 11:03:58 +0800 | [diff] [blame] | 10 | #include "bitops.h" |
David Hu | f07e97d | 2021-02-15 22:05:40 +0800 | [diff] [blame] | 11 | #include "fih.h" |
Jamie Fox | cc31d40 | 2019-01-28 17:13:52 +0000 | [diff] [blame] | 12 | #include "psa/client.h" |
| 13 | #include "psa/service.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 14 | #include "tfm_thread.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 15 | #include "tfm_wait.h" |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 16 | #include "internal_errors.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 17 | #include "tfm_spm_hal.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 18 | #include "tfm_api.h" |
| 19 | #include "tfm_secure_api.h" |
| 20 | #include "tfm_memory_utils.h" |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 21 | #include "tfm_hal_defs.h" |
| 22 | #include "tfm_hal_isolation.h" |
Mingyang Sun | 7397b4f | 2020-06-17 15:07:45 +0800 | [diff] [blame] | 23 | #include "spm_ipc.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 24 | #include "tfm_peripherals_def.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 25 | #include "tfm_core_utils.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 26 | #include "tfm_rpc.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 27 | #include "tfm_core_trustzone.h" |
Ken Liu | 24dffb2 | 2021-02-10 11:03:58 +0800 | [diff] [blame] | 28 | #include "lists.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 29 | #include "tfm_pools.h" |
Mingyang Sun | 7397b4f | 2020-06-17 15:07:45 +0800 | [diff] [blame] | 30 | #include "spm_partition_defs.h" |
| 31 | #include "psa_manifest/pid.h" |
Summer Qin | 5fdcf63 | 2020-06-22 16:49:24 +0800 | [diff] [blame] | 32 | #include "tfm/tfm_spm_services.h" |
Mingyang Sun | 00df235 | 2021-04-15 15:46:08 +0800 | [diff] [blame] | 33 | #include "load/partition_defs.h" |
| 34 | #include "load/service_defs.h" |
Ken Liu | 8668628 | 2021-04-27 11:11:15 +0800 | [diff] [blame] | 35 | #include "load/asset_defs.h" |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 36 | #include "load/spm_load_api.h" |
Kevin Peng | 27e4227 | 2021-05-24 17:58:53 +0800 | [diff] [blame] | 37 | #include "load/irq_defs.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 38 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 39 | extern struct spm_partition_db_t g_spm_partition_db; |
Ken Liu | b3b2cb6 | 2021-05-22 00:39:28 +0800 | [diff] [blame] | 40 | static struct service_t *connection_services_listhead; |
| 41 | struct service_t *stateless_services_ref_tbl[STATIC_HANDLE_NUM_LIMIT]; |
Summer Qin | d99509f | 2019-08-02 17:36:58 +0800 | [diff] [blame] | 42 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 43 | /* Pools */ |
| 44 | TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t), |
| 45 | TFM_CONN_HANDLE_MAX_NUM); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 46 | |
Kevin Peng | dc79188 | 2021-03-12 10:57:12 +0800 | [diff] [blame] | 47 | void tfm_set_irq_signal(uint32_t partition_id, psa_signal_t signal, |
| 48 | uint32_t irq_line); |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 49 | |
| 50 | #include "tfm_secure_irq_handlers_ipc.inc" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 51 | |
Summer Qin | 373feb1 | 2020-03-27 15:35:33 +0800 | [diff] [blame] | 52 | /*********************** Connection handle conversion APIs *******************/ |
| 53 | |
Summer Qin | 373feb1 | 2020-03-27 15:35:33 +0800 | [diff] [blame] | 54 | #define CONVERSION_FACTOR_BITOFFSET 3 |
| 55 | #define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET) |
| 56 | /* Set 32 as the maximum */ |
| 57 | #define CONVERSION_FACTOR_VALUE_MAX 0x20 |
| 58 | |
| 59 | #if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX |
| 60 | #error "CONVERSION FACTOR OUT OF RANGE" |
| 61 | #endif |
| 62 | |
| 63 | static uint32_t loop_index; |
| 64 | |
| 65 | /* |
| 66 | * A handle instance psa_handle_t allocated inside SPM is actually a memory |
| 67 | * address among the handle pool. Return this handle to the client directly |
| 68 | * exposes information of secure memory address. In this case, converting the |
| 69 | * handle into another value does not represent the memory address to avoid |
| 70 | * exposing secure memory directly to clients. |
| 71 | * |
| 72 | * This function converts the handle instance into another value by scaling the |
| 73 | * handle in pool offset, the converted value is named as a user handle. |
| 74 | * |
| 75 | * The formula: |
| 76 | * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE + |
| 77 | * CLIENT_HANDLE_VALUE_MIN + loop_index |
| 78 | * where: |
| 79 | * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not |
| 80 | * exceed CONVERSION_FACTOR_VALUE_MAX. |
| 81 | * |
| 82 | * handle_instance in RANGE[POOL_START, POOL_END] |
| 83 | * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF] |
| 84 | * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1] |
| 85 | * |
| 86 | * note: |
| 87 | * loop_index is used to promise same handle instance is converted into |
| 88 | * different user handles in short time. |
| 89 | */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 90 | 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] | 91 | { |
| 92 | psa_handle_t user_handle; |
| 93 | |
| 94 | loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE; |
| 95 | user_handle = (psa_handle_t)((((uintptr_t)handle_instance - |
| 96 | (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) + |
| 97 | CLIENT_HANDLE_VALUE_MIN + loop_index); |
| 98 | |
| 99 | return user_handle; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * This function converts a user handle into a corresponded handle instance. |
| 104 | * The converted value is validated before returning, an invalid handle instance |
| 105 | * is returned as NULL. |
| 106 | * |
| 107 | * The formula: |
| 108 | * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) / |
| 109 | * CONVERSION_FACTOR_VALUE) + POOL_START |
| 110 | * where: |
| 111 | * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not |
| 112 | * exceed CONVERSION_FACTOR_VALUE_MAX. |
| 113 | * |
| 114 | * handle_instance in RANGE[POOL_START, POOL_END] |
| 115 | * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF] |
| 116 | * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1] |
| 117 | */ |
| 118 | struct tfm_conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle) |
| 119 | { |
| 120 | struct tfm_conn_handle_t *handle_instance; |
| 121 | |
| 122 | if (user_handle == PSA_NULL_HANDLE) { |
| 123 | return NULL; |
| 124 | } |
| 125 | |
| 126 | handle_instance = (struct tfm_conn_handle_t *)((((uintptr_t)user_handle - |
| 127 | CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) + |
| 128 | (uintptr_t)conn_handle_pool); |
| 129 | |
| 130 | return handle_instance; |
| 131 | } |
| 132 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 133 | /* Service handle management functions */ |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 134 | struct tfm_conn_handle_t *tfm_spm_create_conn_handle(struct service_t *service, |
| 135 | int32_t client_id) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 136 | { |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 137 | struct tfm_conn_handle_t *p_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 138 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 139 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 140 | |
| 141 | /* Get buffer for handle list structure from handle pool */ |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 142 | p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool); |
| 143 | if (!p_handle) { |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 144 | return NULL; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 145 | } |
| 146 | |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 147 | p_handle->service = service; |
Shawn Shan | cc39fcb | 2019-11-13 15:38:16 +0800 | [diff] [blame] | 148 | p_handle->status = TFM_HANDLE_STATUS_IDLE; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 149 | p_handle->client_id = client_id; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 150 | |
| 151 | /* Add handle node to list for next psa functions */ |
Ken Liu | 2c47f7f | 2021-01-22 11:06:04 +0800 | [diff] [blame] | 152 | BI_LIST_INSERT_BEFORE(&service->handle_list, &p_handle->list); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 153 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 154 | return p_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 155 | } |
| 156 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 157 | int32_t tfm_spm_validate_conn_handle( |
| 158 | const struct tfm_conn_handle_t *conn_handle, |
| 159 | int32_t client_id) |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 160 | { |
| 161 | /* Check the handle address is validated */ |
| 162 | if (is_valid_chunk_data_in_pool(conn_handle_pool, |
| 163 | (uint8_t *)conn_handle) != true) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 164 | return SPM_ERROR_GENERIC; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /* Check the handle caller is correct */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 168 | if (conn_handle->client_id != client_id) { |
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 | |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 172 | return SPM_SUCCESS; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 173 | } |
| 174 | |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 175 | int32_t tfm_spm_free_conn_handle(struct service_t *service, |
Summer Qin | 02f7f07 | 2020-08-24 16:02:54 +0800 | [diff] [blame] | 176 | struct tfm_conn_handle_t *conn_handle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 177 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 178 | TFM_CORE_ASSERT(service); |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 179 | TFM_CORE_ASSERT(conn_handle != NULL); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 180 | |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 181 | /* Clear magic as the handler is not used anymore */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 182 | conn_handle->internal_msg.magic = 0; |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 183 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 184 | /* Remove node from handle list */ |
Ken Liu | 2c47f7f | 2021-01-22 11:06:04 +0800 | [diff] [blame] | 185 | BI_LIST_REMOVE_NODE(&conn_handle->list); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 186 | |
| 187 | /* Back handle buffer to pool */ |
Ken Liu | 66ca613 | 2021-02-24 08:49:51 +0800 | [diff] [blame] | 188 | tfm_pool_free(conn_handle_pool, conn_handle); |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 189 | return SPM_SUCCESS; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 190 | } |
| 191 | |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 192 | int32_t tfm_spm_set_rhandle(struct service_t *service, |
Summer Qin | 02f7f07 | 2020-08-24 16:02:54 +0800 | [diff] [blame] | 193 | struct tfm_conn_handle_t *conn_handle, |
| 194 | void *rhandle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 195 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 196 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 197 | /* Set reverse handle value only be allowed for a connected handle */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 198 | TFM_CORE_ASSERT(conn_handle != NULL); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 199 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 200 | conn_handle->rhandle = rhandle; |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 201 | return SPM_SUCCESS; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 202 | } |
| 203 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 204 | /** |
Xinyu Zhang | 3a45324 | 2021-04-16 17:57:09 +0800 | [diff] [blame] | 205 | * \brief Get reverse handle value from connection handle. |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 206 | * |
| 207 | * \param[in] service Target service context pointer |
| 208 | * \param[in] conn_handle Connection handle created by |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 209 | * tfm_spm_create_conn_handle() |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 210 | * |
| 211 | * \retval void * Success |
| 212 | * \retval "Does not return" Panic for those: |
| 213 | * service pointer are NULL |
Xinyu Zhang | 3a45324 | 2021-04-16 17:57:09 +0800 | [diff] [blame] | 214 | * handle is \ref PSA_NULL_HANDLE |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 215 | * handle node does not be found |
| 216 | */ |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 217 | static void *tfm_spm_get_rhandle(struct service_t *service, |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 218 | struct tfm_conn_handle_t *conn_handle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 219 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 220 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 221 | /* Get reverse handle value only be allowed for a connected handle */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 222 | TFM_CORE_ASSERT(conn_handle != NULL); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 223 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 224 | return conn_handle->rhandle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* Partition management functions */ |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 228 | |
Summer Qin | 02f7f07 | 2020-08-24 16:02:54 +0800 | [diff] [blame] | 229 | struct tfm_msg_body_t *tfm_spm_get_msg_by_signal(struct partition_t *partition, |
| 230 | psa_signal_t signal) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 231 | { |
Ken Liu | 2c47f7f | 2021-01-22 11:06:04 +0800 | [diff] [blame] | 232 | struct bi_list_node_t *node, *head; |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 233 | struct tfm_msg_body_t *tmp_msg, *msg = NULL; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 234 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 235 | TFM_CORE_ASSERT(partition); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 236 | |
Mingyang Sun | af22ffa | 2020-07-09 17:48:37 +0800 | [diff] [blame] | 237 | head = &partition->msg_list; |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 238 | |
Ken Liu | 2c47f7f | 2021-01-22 11:06:04 +0800 | [diff] [blame] | 239 | if (BI_LIST_IS_EMPTY(head)) { |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 240 | return NULL; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 241 | } |
| 242 | |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 243 | /* |
| 244 | * There may be multiple messages for this RoT Service signal, do not clear |
| 245 | * partition mask until no remaining message. Search may be optimized. |
| 246 | */ |
Ken Liu | 2c47f7f | 2021-01-22 11:06:04 +0800 | [diff] [blame] | 247 | BI_LIST_FOR_EACH(node, head) { |
Ken Liu | c25558c | 2021-05-20 15:31:28 +0800 | [diff] [blame] | 248 | tmp_msg = TO_CONTAINER(node, struct tfm_msg_body_t, msg_node); |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 249 | if (tmp_msg->service->p_ldinf->signal == signal && msg) { |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 250 | return msg; |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 251 | } else if (tmp_msg->service->p_ldinf->signal == signal) { |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 252 | msg = tmp_msg; |
Ken Liu | 2c47f7f | 2021-01-22 11:06:04 +0800 | [diff] [blame] | 253 | BI_LIST_REMOVE_NODE(node); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 254 | } |
| 255 | } |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 256 | |
Mingyang Sun | af22ffa | 2020-07-09 17:48:37 +0800 | [diff] [blame] | 257 | partition->signals_asserted &= ~signal; |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 258 | |
| 259 | return msg; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 260 | } |
| 261 | |
Mingyang Sun | da30f1e | 2020-07-13 17:20:32 +0800 | [diff] [blame] | 262 | /** |
| 263 | * \brief Returns the index of the partition with the given partition ID. |
| 264 | * |
| 265 | * \param[in] partition_id Partition id |
| 266 | * |
| 267 | * \return the partition idx if partition_id is valid, |
| 268 | * \ref SPM_INVALID_PARTITION_IDX othervise |
| 269 | */ |
| 270 | static uint32_t get_partition_idx(uint32_t partition_id) |
| 271 | { |
| 272 | uint32_t i; |
| 273 | |
| 274 | if (partition_id == INVALID_PARTITION_ID) { |
| 275 | return SPM_INVALID_PARTITION_IDX; |
| 276 | } |
| 277 | |
| 278 | for (i = 0; i < g_spm_partition_db.partition_count; ++i) { |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 279 | if (g_spm_partition_db.partitions[i].p_ldinf->pid == partition_id) { |
Mingyang Sun | da30f1e | 2020-07-13 17:20:32 +0800 | [diff] [blame] | 280 | return i; |
| 281 | } |
| 282 | } |
| 283 | return SPM_INVALID_PARTITION_IDX; |
| 284 | } |
| 285 | |
Mingyang Sun | da30f1e | 2020-07-13 17:20:32 +0800 | [diff] [blame] | 286 | #if TFM_LVL != 1 |
| 287 | /** |
| 288 | * \brief Change the privilege mode for partition thread mode. |
| 289 | * |
| 290 | * \param[in] privileged Privileged mode, |
| 291 | * \ref TFM_PARTITION_PRIVILEGED_MODE |
| 292 | * and \ref TFM_PARTITION_UNPRIVILEGED_MODE |
| 293 | * |
| 294 | * \note Barrier instructions are not called by this function, and if |
| 295 | * it is called in thread mode, it might be necessary to call |
| 296 | * them after this function returns. |
| 297 | */ |
| 298 | static void tfm_spm_partition_change_privilege(uint32_t privileged) |
| 299 | { |
| 300 | CONTROL_Type ctrl; |
| 301 | |
| 302 | ctrl.w = __get_CONTROL(); |
| 303 | |
| 304 | if (privileged == TFM_PARTITION_PRIVILEGED_MODE) { |
| 305 | ctrl.b.nPRIV = 0; |
| 306 | } else { |
| 307 | ctrl.b.nPRIV = 1; |
| 308 | } |
| 309 | |
| 310 | __set_CONTROL(ctrl.w); |
| 311 | } |
| 312 | #endif /* if(TFM_LVL != 1) */ |
| 313 | |
Mingyang Sun | da30f1e | 2020-07-13 17:20:32 +0800 | [diff] [blame] | 314 | uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags) |
| 315 | { |
Kevin Peng | bf9a97e | 2021-06-18 16:34:12 +0800 | [diff] [blame^] | 316 | #if TFM_LVL == 1 |
| 317 | return TFM_PARTITION_PRIVILEGED_MODE; |
| 318 | #else /* TFM_LVL == 1 */ |
Mingyang Sun | da30f1e | 2020-07-13 17:20:32 +0800 | [diff] [blame] | 319 | if (partition_flags & SPM_PART_FLAG_PSA_ROT) { |
| 320 | return TFM_PARTITION_PRIVILEGED_MODE; |
| 321 | } else { |
| 322 | return TFM_PARTITION_UNPRIVILEGED_MODE; |
| 323 | } |
Kevin Peng | bf9a97e | 2021-06-18 16:34:12 +0800 | [diff] [blame^] | 324 | #endif /* TFM_LVL == 1 */ |
Mingyang Sun | da30f1e | 2020-07-13 17:20:32 +0800 | [diff] [blame] | 325 | } |
| 326 | |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 327 | struct service_t *tfm_spm_get_service_by_sid(uint32_t sid) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 328 | { |
Mingyang Sun | ef42f44 | 2021-06-11 15:07:58 +0800 | [diff] [blame] | 329 | uint32_t i = 0; |
Ken Liu | b3b2cb6 | 2021-05-22 00:39:28 +0800 | [diff] [blame] | 330 | struct service_t *p_serv = connection_services_listhead; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 331 | |
Mingyang Sun | ef42f44 | 2021-06-11 15:07:58 +0800 | [diff] [blame] | 332 | for (i = 0; i < STATIC_HANDLE_NUM_LIMIT; i++) { |
| 333 | if (stateless_services_ref_tbl[i]->p_ldinf->sid == sid) { |
| 334 | return stateless_services_ref_tbl[i]; |
| 335 | } |
| 336 | } |
| 337 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 338 | while (p_serv && p_serv->p_ldinf->sid != sid) { |
| 339 | p_serv = TO_CONTAINER(BI_LIST_NEXT_NODE(&p_serv->list), |
| 340 | struct service_t, list); |
Ken Liu | b3b2cb6 | 2021-05-22 00:39:28 +0800 | [diff] [blame] | 341 | if (p_serv == connection_services_listhead) |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 342 | return NULL; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 343 | } |
Summer Qin | 2fca1c8 | 2020-03-20 14:37:55 +0800 | [diff] [blame] | 344 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 345 | return p_serv; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 346 | } |
| 347 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 348 | /** |
| 349 | * \brief Get the partition context by partition ID. |
| 350 | * |
| 351 | * \param[in] partition_id Partition identity |
| 352 | * |
| 353 | * \retval NULL Failed |
| 354 | * \retval "Not NULL" Target partition context pointer, |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 355 | * \ref partition_t structures |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 356 | */ |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 357 | static 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] | 358 | { |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 359 | uint32_t idx = get_partition_idx(partition_id); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 360 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 361 | if (idx != SPM_INVALID_PARTITION_IDX) { |
| 362 | return &(g_spm_partition_db.partitions[idx]); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 363 | } |
| 364 | return NULL; |
| 365 | } |
| 366 | |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 367 | struct partition_t *tfm_spm_get_running_partition(void) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 368 | { |
Kevin Peng | 82fbca5 | 2021-03-09 13:48:48 +0800 | [diff] [blame] | 369 | struct tfm_core_thread_t *pth = tfm_core_thrd_get_curr(); |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 370 | struct partition_t *partition; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 371 | |
Ken Liu | c25558c | 2021-05-20 15:31:28 +0800 | [diff] [blame] | 372 | partition = TO_CONTAINER(pth, struct partition_t, sp_thread); |
Mingyang Sun | af22ffa | 2020-07-09 17:48:37 +0800 | [diff] [blame] | 373 | |
Kevin Peng | 79c2bda | 2020-07-24 16:31:12 +0800 | [diff] [blame] | 374 | return partition; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 375 | } |
| 376 | |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 377 | int32_t tfm_spm_check_client_version(struct service_t *service, |
Jaykumar Pitambarbhai Patel | 3a98602 | 2019-10-08 17:37:15 +0530 | [diff] [blame] | 378 | uint32_t version) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 379 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 380 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 381 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 382 | switch (SERVICE_GET_VERSION_POLICY(service->p_ldinf->flags)) { |
Ken Liu | b3b2cb6 | 2021-05-22 00:39:28 +0800 | [diff] [blame] | 383 | case SERVICE_VERSION_POLICY_RELAXED: |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 384 | if (version > service->p_ldinf->version) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 385 | return SPM_ERROR_VERSION; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 386 | } |
| 387 | break; |
Ken Liu | b3b2cb6 | 2021-05-22 00:39:28 +0800 | [diff] [blame] | 388 | case SERVICE_VERSION_POLICY_STRICT: |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 389 | if (version != service->p_ldinf->version) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 390 | return SPM_ERROR_VERSION; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 391 | } |
| 392 | break; |
| 393 | default: |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 394 | return SPM_ERROR_VERSION; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 395 | } |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 396 | return SPM_SUCCESS; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 397 | } |
| 398 | |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 399 | int32_t tfm_spm_check_authorization(uint32_t sid, |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 400 | struct service_t *service, |
Summer Qin | 618e8c3 | 2019-12-09 10:47:20 +0800 | [diff] [blame] | 401 | bool ns_caller) |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 402 | { |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 403 | struct partition_t *partition = NULL; |
Mingyang Sun | 2b35266 | 2021-04-21 11:35:43 +0800 | [diff] [blame] | 404 | uint32_t *dep; |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 405 | int32_t i; |
| 406 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 407 | TFM_CORE_ASSERT(service); |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 408 | |
| 409 | if (ns_caller) { |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 410 | if (!SERVICE_IS_NS_ACCESSIBLE(service->p_ldinf->flags)) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 411 | return SPM_ERROR_GENERIC; |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 412 | } |
| 413 | } else { |
| 414 | partition = tfm_spm_get_running_partition(); |
| 415 | if (!partition) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 416 | tfm_core_panic(); |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 417 | } |
| 418 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 419 | dep = (uint32_t *)LOAD_INFO_DEPS(partition->p_ldinf); |
| 420 | for (i = 0; i < partition->p_ldinf->ndeps; i++) { |
Mingyang Sun | 2b35266 | 2021-04-21 11:35:43 +0800 | [diff] [blame] | 421 | if (dep[i] == sid) { |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 422 | break; |
| 423 | } |
| 424 | } |
| 425 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 426 | if (i == partition->p_ldinf->ndeps) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 427 | return SPM_ERROR_GENERIC; |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 428 | } |
| 429 | } |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 430 | return SPM_SUCCESS; |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 431 | } |
| 432 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 433 | /* Message functions */ |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 434 | |
Summer Qin | 02f7f07 | 2020-08-24 16:02:54 +0800 | [diff] [blame] | 435 | 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] | 436 | { |
| 437 | /* |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 438 | * The message handler passed by the caller is considered invalid in the |
| 439 | * following cases: |
| 440 | * 1. Not a valid message handle. (The address of a message is not the |
| 441 | * address of a possible handle from the pool |
| 442 | * 2. Handle not belongs to the caller partition (The handle is either |
| 443 | * unused, or owned by anither partition) |
| 444 | * Check the conditions above |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 445 | */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 446 | struct tfm_msg_body_t *p_msg; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 447 | uint32_t partition_id; |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 448 | struct tfm_conn_handle_t *p_conn_handle = |
| 449 | tfm_spm_to_handle_instance(msg_handle); |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 450 | |
| 451 | if (is_valid_chunk_data_in_pool( |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 452 | conn_handle_pool, (uint8_t *)p_conn_handle) != 1) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 453 | return NULL; |
| 454 | } |
| 455 | |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 456 | p_msg = &p_conn_handle->internal_msg; |
| 457 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 458 | /* |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 459 | * Check that the magic number is correct. This proves that the message |
| 460 | * structure contains an active message. |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 461 | */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 462 | if (p_msg->magic != TFM_MSG_MAGIC) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 463 | return NULL; |
| 464 | } |
| 465 | |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 466 | /* Check that the running partition owns the message */ |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 467 | partition_id = tfm_spm_partition_get_running_partition_id(); |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 468 | if (partition_id != p_msg->service->partition->p_ldinf->pid) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 469 | return NULL; |
| 470 | } |
| 471 | |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 472 | return p_msg; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 473 | } |
| 474 | |
Kevin Peng | df6aa29 | 2021-03-11 17:58:50 +0800 | [diff] [blame] | 475 | struct tfm_msg_body_t * |
| 476 | tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle) |
| 477 | { |
| 478 | TFM_CORE_ASSERT(conn_handle != NULL); |
| 479 | |
| 480 | return &(conn_handle->internal_msg); |
| 481 | } |
| 482 | |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 483 | void tfm_spm_fill_msg(struct tfm_msg_body_t *msg, |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 484 | struct service_t *service, |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 485 | psa_handle_t handle, |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 486 | int32_t type, int32_t client_id, |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 487 | psa_invec *invec, size_t in_len, |
| 488 | psa_outvec *outvec, size_t out_len, |
| 489 | psa_outvec *caller_outvec) |
| 490 | { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 491 | uint32_t i; |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 492 | struct tfm_conn_handle_t *conn_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 493 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 494 | TFM_CORE_ASSERT(msg); |
| 495 | TFM_CORE_ASSERT(service); |
| 496 | TFM_CORE_ASSERT(!(invec == NULL && in_len != 0)); |
| 497 | TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0)); |
| 498 | TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC); |
| 499 | TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC); |
| 500 | TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 501 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 502 | /* Clear message buffer before using it */ |
Summer Qin | f24dbb5 | 2020-07-23 14:53:54 +0800 | [diff] [blame] | 503 | spm_memset(msg, 0, sizeof(struct tfm_msg_body_t)); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 504 | |
Ken Liu | 35f8939 | 2019-03-14 14:51:05 +0800 | [diff] [blame] | 505 | tfm_event_init(&msg->ack_evnt); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 506 | msg->magic = TFM_MSG_MAGIC; |
| 507 | msg->service = service; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 508 | msg->caller_outvec = caller_outvec; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 509 | msg->msg.client_id = client_id; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 510 | |
| 511 | /* Copy contents */ |
| 512 | msg->msg.type = type; |
| 513 | |
| 514 | for (i = 0; i < in_len; i++) { |
| 515 | msg->msg.in_size[i] = invec[i].len; |
| 516 | msg->invec[i].base = invec[i].base; |
| 517 | } |
| 518 | |
| 519 | for (i = 0; i < out_len; i++) { |
| 520 | msg->msg.out_size[i] = outvec[i].len; |
| 521 | msg->outvec[i].base = outvec[i].base; |
| 522 | /* Out len is used to record the writed number, set 0 here again */ |
| 523 | msg->outvec[i].len = 0; |
| 524 | } |
| 525 | |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 526 | /* Use the user connect handle as the message handle */ |
| 527 | msg->msg.handle = handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 528 | |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 529 | conn_handle = tfm_spm_to_handle_instance(handle); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 530 | /* For connected handle, set rhandle to every message */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 531 | if (conn_handle) { |
| 532 | msg->msg.rhandle = tfm_spm_get_rhandle(service, conn_handle); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 533 | } |
David Hu | 46603dd | 2019-12-11 18:05:16 +0800 | [diff] [blame] | 534 | |
| 535 | /* Set the private data of NSPE client caller in multi-core topology */ |
| 536 | if (TFM_CLIENT_ID_IS_NS(client_id)) { |
| 537 | tfm_rpc_set_caller_data(msg, client_id); |
| 538 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 539 | } |
| 540 | |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 541 | void tfm_spm_send_event(struct service_t *service, |
Kevin Peng | 8dac610 | 2021-03-09 16:44:00 +0800 | [diff] [blame] | 542 | struct tfm_msg_body_t *msg) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 543 | { |
Kevin Peng | 8dac610 | 2021-03-09 16:44:00 +0800 | [diff] [blame] | 544 | struct partition_t *partition = NULL; |
| 545 | psa_signal_t signal = 0; |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 546 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 547 | if (!msg || !service || !service->p_ldinf || !service->partition) { |
Kevin Peng | 8dac610 | 2021-03-09 16:44:00 +0800 | [diff] [blame] | 548 | tfm_core_panic(); |
| 549 | } |
| 550 | |
| 551 | partition = service->partition; |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 552 | signal = service->p_ldinf->signal; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 553 | |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 554 | /* Add message to partition message list tail */ |
Ken Liu | 2c47f7f | 2021-01-22 11:06:04 +0800 | [diff] [blame] | 555 | BI_LIST_INSERT_BEFORE(&partition->msg_list, &msg->msg_node); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 556 | |
| 557 | /* Messages put. Update signals */ |
Kevin Peng | 8dac610 | 2021-03-09 16:44:00 +0800 | [diff] [blame] | 558 | partition->signals_asserted |= signal; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 559 | |
Kevin Peng | 8dac610 | 2021-03-09 16:44:00 +0800 | [diff] [blame] | 560 | if (partition->signals_waiting & signal) { |
| 561 | tfm_event_wake( |
| 562 | &partition->event, |
| 563 | (partition->signals_asserted & partition->signals_waiting)); |
| 564 | partition->signals_waiting &= ~signal; |
| 565 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 566 | |
David Hu | fb38d56 | 2019-09-23 15:58:34 +0800 | [diff] [blame] | 567 | /* |
| 568 | * If it is a NS request via RPC, it is unnecessary to block current |
| 569 | * thread. |
| 570 | */ |
| 571 | if (!is_tfm_rpc_msg(msg)) { |
| 572 | tfm_event_wait(&msg->ack_evnt); |
| 573 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 574 | } |
| 575 | |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 576 | uint32_t tfm_spm_partition_get_running_partition_id(void) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 577 | { |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 578 | struct partition_t *partition; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 579 | |
Kevin Peng | 79c2bda | 2020-07-24 16:31:12 +0800 | [diff] [blame] | 580 | partition = tfm_spm_get_running_partition(); |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 581 | if (partition && partition->p_ldinf) { |
| 582 | return partition->p_ldinf->pid; |
Kevin Peng | 79c2bda | 2020-07-24 16:31:12 +0800 | [diff] [blame] | 583 | } else { |
| 584 | return INVALID_PARTITION_ID; |
| 585 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 586 | } |
| 587 | |
Summer Qin | 43c185d | 2019-10-10 15:44:42 +0800 | [diff] [blame] | 588 | 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] | 589 | enum tfm_memory_access_e access, |
| 590 | uint32_t privileged) |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 591 | { |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 592 | enum tfm_hal_status_t err; |
| 593 | uint32_t attr = 0; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 594 | |
| 595 | /* If len is zero, this indicates an empty buffer and base is ignored */ |
| 596 | if (len == 0) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 597 | return SPM_SUCCESS; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | if (!buffer) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 601 | return SPM_ERROR_BAD_PARAMETERS; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | if ((uintptr_t)buffer > (UINTPTR_MAX - len)) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 605 | return SPM_ERROR_MEMORY_CHECK; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 606 | } |
| 607 | |
Summer Qin | 424d4db | 2019-03-25 14:09:51 +0800 | [diff] [blame] | 608 | if (access == TFM_MEMORY_ACCESS_RW) { |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 609 | attr |= (TFM_HAL_ACCESS_READABLE | TFM_HAL_ACCESS_WRITABLE); |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 610 | } else { |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 611 | attr |= TFM_HAL_ACCESS_READABLE; |
Summer Qin | 424d4db | 2019-03-25 14:09:51 +0800 | [diff] [blame] | 612 | } |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 613 | |
| 614 | if (privileged == TFM_PARTITION_UNPRIVILEGED_MODE) { |
| 615 | attr |= TFM_HAL_ACCESS_UNPRIVILEGED; |
| 616 | } else { |
| 617 | attr &= ~TFM_HAL_ACCESS_UNPRIVILEGED; |
| 618 | } |
| 619 | |
| 620 | if (ns_caller) { |
| 621 | attr |= TFM_HAL_ACCESS_NS; |
| 622 | } |
| 623 | |
| 624 | err = tfm_hal_memory_has_access((uintptr_t)buffer, len, attr); |
| 625 | |
| 626 | if (err == TFM_HAL_SUCCESS) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 627 | return SPM_SUCCESS; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 628 | } |
| 629 | |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 630 | return SPM_ERROR_MEMORY_CHECK; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 631 | } |
| 632 | |
Ken Liu | ce2692d | 2020-02-11 12:39:36 +0800 | [diff] [blame] | 633 | uint32_t tfm_spm_init(void) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 634 | { |
Kevin Peng | 54d47fb | 2021-06-15 16:40:08 +0800 | [diff] [blame] | 635 | uint32_t i; |
Mingyang Sun | 61f8fbc | 2021-06-04 17:49:56 +0800 | [diff] [blame] | 636 | bool privileged; |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 637 | struct partition_t *partition; |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 638 | struct tfm_core_thread_t *pth, *p_ns_entry_thread = NULL; |
Ken Liu | 8668628 | 2021-04-27 11:11:15 +0800 | [diff] [blame] | 639 | const struct platform_data_t *platform_data_p; |
Mingyang Sun | 8d004f7 | 2021-06-01 10:46:26 +0800 | [diff] [blame] | 640 | const struct partition_load_info_t *p_ldinf; |
Ken Liu | 4520ce3 | 2021-05-11 22:49:10 +0800 | [diff] [blame] | 641 | struct asset_desc_t *p_asset_load; |
David Hu | f07e97d | 2021-02-15 22:05:40 +0800 | [diff] [blame] | 642 | #ifdef TFM_FIH_PROFILE_ON |
| 643 | fih_int fih_rc = FIH_FAILURE; |
| 644 | #endif |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 645 | |
| 646 | tfm_pool_init(conn_handle_pool, |
| 647 | POOL_BUFFER_SIZE(conn_handle_pool), |
| 648 | sizeof(struct tfm_conn_handle_t), |
| 649 | TFM_CONN_HANDLE_MAX_NUM); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 650 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 651 | while (1) { |
| 652 | partition = load_a_partition_assuredly(); |
| 653 | if (partition == NULL) { |
| 654 | break; |
Kevin Peng | 79c2bda | 2020-07-24 16:31:12 +0800 | [diff] [blame] | 655 | } |
| 656 | |
Mingyang Sun | 8d004f7 | 2021-06-01 10:46:26 +0800 | [diff] [blame] | 657 | p_ldinf = partition->p_ldinf; |
Mingyang Sun | 2b35266 | 2021-04-21 11:35:43 +0800 | [diff] [blame] | 658 | |
Kevin Peng | 27e4227 | 2021-05-24 17:58:53 +0800 | [diff] [blame] | 659 | if (p_ldinf->nservices) { |
| 660 | load_services_assuredly(partition, &connection_services_listhead, |
| 661 | stateless_services_ref_tbl, |
| 662 | sizeof(stateless_services_ref_tbl)); |
| 663 | } |
| 664 | |
| 665 | if (p_ldinf->nirqs) { |
| 666 | load_irqs_assuredly(partition); |
| 667 | } |
| 668 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 669 | /* Init mmio assets */ |
Mingyang Sun | 8d004f7 | 2021-06-01 10:46:26 +0800 | [diff] [blame] | 670 | if (p_ldinf->nassets > 0) { |
| 671 | if (tfm_spm_partition_get_privileged_mode(p_ldinf->flags) == |
Mingyang Sun | 61f8fbc | 2021-06-04 17:49:56 +0800 | [diff] [blame] | 672 | TFM_PARTITION_PRIVILEGED_MODE) { |
| 673 | privileged = true; |
| 674 | } else { |
| 675 | privileged = false; |
| 676 | } |
| 677 | } |
| 678 | |
Mingyang Sun | 8d004f7 | 2021-06-01 10:46:26 +0800 | [diff] [blame] | 679 | p_asset_load = (struct asset_desc_t *)LOAD_INFO_ASSET(p_ldinf); |
| 680 | for (i = 0; i < p_ldinf->nassets; i++) { |
Ken Liu | 8668628 | 2021-04-27 11:11:15 +0800 | [diff] [blame] | 681 | /* Skip the memory-based asset */ |
Ken Liu | 4520ce3 | 2021-05-11 22:49:10 +0800 | [diff] [blame] | 682 | if (!(p_asset_load[i].attr & ASSET_DEV_REF_BIT)) { |
Ken Liu | 8668628 | 2021-04-27 11:11:15 +0800 | [diff] [blame] | 683 | continue; |
Mate Toth-Pal | 8ac98a7 | 2019-11-21 17:30:10 +0100 | [diff] [blame] | 684 | } |
Ken Liu | 8668628 | 2021-04-27 11:11:15 +0800 | [diff] [blame] | 685 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 686 | platform_data_p = REFERENCE_TO_PTR(p_asset_load[i].dev.addr_ref, |
| 687 | struct platform_data_t *); |
Ken Liu | 25e09c7 | 2021-05-24 15:46:46 +0800 | [diff] [blame] | 688 | |
| 689 | /* |
| 690 | * TODO: some partitions declare MMIO not exist on specific |
| 691 | * platforms, and the platform defines a dummy NULL reference |
| 692 | * for these MMIO items, which cause 'nassets' to contain several |
| 693 | * NULL items. Skip these NULL items initialization temporarily to |
| 694 | * avoid HAL API panic. |
| 695 | * Eventually, these platform-specific partitions need to be moved |
| 696 | * into a platform-specific folder. Then this workaround can be |
| 697 | * removed. |
| 698 | */ |
| 699 | if (!platform_data_p) { |
| 700 | continue; |
| 701 | } |
| 702 | |
Ken Liu | 8668628 | 2021-04-27 11:11:15 +0800 | [diff] [blame] | 703 | #ifdef TFM_FIH_PROFILE_ON |
Mingyang Sun | 61f8fbc | 2021-06-04 17:49:56 +0800 | [diff] [blame] | 704 | FIH_CALL(tfm_spm_hal_configure_default_isolation, fih_rc, |
| 705 | privileged, platform_data_p); |
Ken Liu | 8668628 | 2021-04-27 11:11:15 +0800 | [diff] [blame] | 706 | if (fih_not_eq(fih_rc, fih_int_encode(TFM_PLAT_ERR_SUCCESS))) { |
| 707 | tfm_core_panic(); |
| 708 | } |
| 709 | #else /* TFM_FIH_PROFILE_ON */ |
Mingyang Sun | 61f8fbc | 2021-06-04 17:49:56 +0800 | [diff] [blame] | 710 | if (tfm_spm_hal_configure_default_isolation(privileged, |
Ken Liu | 8668628 | 2021-04-27 11:11:15 +0800 | [diff] [blame] | 711 | platform_data_p) != TFM_PLAT_ERR_SUCCESS) { |
| 712 | tfm_core_panic(); |
| 713 | } |
| 714 | #endif /* TFM_FIH_PROFILE_ON */ |
Mate Toth-Pal | 8ac98a7 | 2019-11-21 17:30:10 +0100 | [diff] [blame] | 715 | } |
| 716 | |
Mingyang Sun | af22ffa | 2020-07-09 17:48:37 +0800 | [diff] [blame] | 717 | partition->signals_allowed |= PSA_DOORBELL; |
Shawn Shan | c7dda0e | 2019-12-23 14:45:09 +0800 | [diff] [blame] | 718 | |
Mingyang Sun | af22ffa | 2020-07-09 17:48:37 +0800 | [diff] [blame] | 719 | tfm_event_init(&partition->event); |
Ken Liu | 2c47f7f | 2021-01-22 11:06:04 +0800 | [diff] [blame] | 720 | BI_LIST_INIT_NODE(&partition->msg_list); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 721 | |
Mingyang Sun | af22ffa | 2020-07-09 17:48:37 +0800 | [diff] [blame] | 722 | pth = &partition->sp_thread; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 723 | if (!pth) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 724 | tfm_core_panic(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 725 | } |
| 726 | |
Mingyang Sun | 8d004f7 | 2021-06-01 10:46:26 +0800 | [diff] [blame] | 727 | /* Extendable partition load info is right after p_ldinf. */ |
Mingyang Sun | 2b35266 | 2021-04-21 11:35:43 +0800 | [diff] [blame] | 728 | tfm_core_thrd_init( |
| 729 | pth, |
Mingyang Sun | 8d004f7 | 2021-06-01 10:46:26 +0800 | [diff] [blame] | 730 | POSITION_TO_ENTRY(p_ldinf->entry, tfm_core_thrd_entry_t), |
Mingyang Sun | 2b35266 | 2021-04-21 11:35:43 +0800 | [diff] [blame] | 731 | NULL, |
Mingyang Sun | 8d004f7 | 2021-06-01 10:46:26 +0800 | [diff] [blame] | 732 | LOAD_ALLOCED_STACK_ADDR(p_ldinf) + p_ldinf->stack_size, |
| 733 | LOAD_ALLOCED_STACK_ADDR(p_ldinf)); |
Edison Ai | 788bae2 | 2019-02-18 17:38:59 +0800 | [diff] [blame] | 734 | |
Mingyang Sun | 8d004f7 | 2021-06-01 10:46:26 +0800 | [diff] [blame] | 735 | pth->prior = TO_THREAD_PRIORITY(PARTITION_PRIORITY(p_ldinf->flags)); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 736 | |
Mingyang Sun | 8d004f7 | 2021-06-01 10:46:26 +0800 | [diff] [blame] | 737 | if (p_ldinf->pid == TFM_SP_NON_SECURE_ID) { |
Ken Liu | 490281d | 2019-12-30 15:55:26 +0800 | [diff] [blame] | 738 | p_ns_entry_thread = pth; |
Ken Liu | 5248af2 | 2019-12-29 12:47:13 +0800 | [diff] [blame] | 739 | pth->param = (void *)tfm_spm_hal_get_ns_entry_point(); |
Ken Liu | 490281d | 2019-12-30 15:55:26 +0800 | [diff] [blame] | 740 | } |
| 741 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 742 | /* Kick off */ |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 743 | if (tfm_core_thrd_start(pth) != THRD_SUCCESS) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 744 | tfm_core_panic(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 745 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 746 | } |
| 747 | |
Ken Liu | 483f5da | 2019-04-24 10:45:21 +0800 | [diff] [blame] | 748 | /* |
| 749 | * All threads initialized, start the scheduler. |
| 750 | * |
| 751 | * NOTE: |
Ken Liu | 490281d | 2019-12-30 15:55:26 +0800 | [diff] [blame] | 752 | * It is worthy to give the thread object to scheduler if the background |
| 753 | * context belongs to one of the threads. Here the background thread is the |
| 754 | * initialization thread who calls SPM SVC, which re-uses the non-secure |
| 755 | * entry thread's stack. After SPM initialization is done, this stack is |
| 756 | * cleaned up and the background context is never going to return. Tell |
| 757 | * the scheduler that the current thread is non-secure entry thread. |
Ken Liu | 483f5da | 2019-04-24 10:45:21 +0800 | [diff] [blame] | 758 | */ |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 759 | tfm_core_thrd_start_scheduler(p_ns_entry_thread); |
Ken Liu | ce2692d | 2020-02-11 12:39:36 +0800 | [diff] [blame] | 760 | |
Summer Qin | d2ad7e7 | 2020-01-06 18:16:35 +0800 | [diff] [blame] | 761 | return p_ns_entry_thread->arch_ctx.lr; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 762 | } |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 763 | |
Summer Qin | d2ad7e7 | 2020-01-06 18:16:35 +0800 | [diff] [blame] | 764 | void tfm_pendsv_do_schedule(struct tfm_arch_ctx_t *p_actx) |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 765 | { |
Kevin Peng | 25b190b | 2020-10-30 17:10:45 +0800 | [diff] [blame] | 766 | #if TFM_LVL != 1 |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 767 | struct partition_t *p_next_partition; |
Ken Liu | 4520ce3 | 2021-05-11 22:49:10 +0800 | [diff] [blame] | 768 | const struct partition_load_info_t *p_part_static; |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 769 | uint32_t is_privileged; |
| 770 | #endif |
Kevin Peng | 82fbca5 | 2021-03-09 13:48:48 +0800 | [diff] [blame] | 771 | struct tfm_core_thread_t *pth_next = tfm_core_thrd_get_next(); |
| 772 | struct tfm_core_thread_t *pth_curr = tfm_core_thrd_get_curr(); |
David Hu | f07e97d | 2021-02-15 22:05:40 +0800 | [diff] [blame] | 773 | #if defined(TFM_FIH_PROFILE_ON) && (TFM_LVL == 3) |
| 774 | fih_int fih_rc = FIH_FAILURE; |
| 775 | #endif |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 776 | |
Mate Toth-Pal | 32b2ccd | 2019-04-26 10:00:16 +0200 | [diff] [blame] | 777 | if (pth_next != NULL && pth_curr != pth_next) { |
Kevin Peng | 25b190b | 2020-10-30 17:10:45 +0800 | [diff] [blame] | 778 | #if TFM_LVL != 1 |
Ken Liu | c25558c | 2021-05-20 15:31:28 +0800 | [diff] [blame] | 779 | p_next_partition = TO_CONTAINER(pth_next, |
| 780 | struct partition_t, |
| 781 | sp_thread); |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 782 | p_part_static = p_next_partition->p_ldinf; |
Ken Liu | 8668628 | 2021-04-27 11:11:15 +0800 | [diff] [blame] | 783 | if (p_part_static->flags & SPM_PART_FLAG_PSA_ROT) { |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 784 | is_privileged = TFM_PARTITION_PRIVILEGED_MODE; |
| 785 | } else { |
| 786 | is_privileged = TFM_PARTITION_UNPRIVILEGED_MODE; |
| 787 | } |
| 788 | |
| 789 | tfm_spm_partition_change_privilege(is_privileged); |
Kevin Peng | 25b190b | 2020-10-30 17:10:45 +0800 | [diff] [blame] | 790 | #if TFM_LVL == 3 |
| 791 | /* |
| 792 | * FIXME: To implement isolations among partitions in isolation level 3, |
| 793 | * each partition needs to run in unprivileged mode. Currently some |
| 794 | * PRoTs cannot work in unprivileged mode, make them privileged now. |
| 795 | */ |
| 796 | if (is_privileged == TFM_PARTITION_UNPRIVILEGED_MODE) { |
Ken Liu | 8668628 | 2021-04-27 11:11:15 +0800 | [diff] [blame] | 797 | struct asset_desc_t *p_asset = |
Ken Liu | 4520ce3 | 2021-05-11 22:49:10 +0800 | [diff] [blame] | 798 | (struct asset_desc_t *)LOAD_INFO_ASSET(p_part_static); |
Ken Liu | 8668628 | 2021-04-27 11:11:15 +0800 | [diff] [blame] | 799 | /* Partition must have private data as the first asset in LVL3 */ |
| 800 | if (p_part_static->nassets == 0) { |
| 801 | tfm_core_panic(); |
| 802 | } |
| 803 | if (p_asset->attr & ASSET_DEV_REF_BIT) { |
| 804 | tfm_core_panic(); |
| 805 | } |
Kevin Peng | 25b190b | 2020-10-30 17:10:45 +0800 | [diff] [blame] | 806 | /* FIXME: only MPU-based implementations are supported currently */ |
David Hu | f07e97d | 2021-02-15 22:05:40 +0800 | [diff] [blame] | 807 | #ifdef TFM_FIH_PROFILE_ON |
| 808 | FIH_CALL(tfm_hal_mpu_update_partition_boundary, fih_rc, |
Ken Liu | 8668628 | 2021-04-27 11:11:15 +0800 | [diff] [blame] | 809 | p_asset->mem.addr_x, p_asset->mem.addr_y); |
David Hu | f07e97d | 2021-02-15 22:05:40 +0800 | [diff] [blame] | 810 | if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) { |
| 811 | tfm_core_panic(); |
| 812 | } |
| 813 | #else /* TFM_FIH_PROFILE_ON */ |
Ken Liu | 8668628 | 2021-04-27 11:11:15 +0800 | [diff] [blame] | 814 | if (tfm_hal_mpu_update_partition_boundary(p_asset->mem.addr_x, |
| 815 | p_asset->mem.addr_y) |
Kevin Peng | 25b190b | 2020-10-30 17:10:45 +0800 | [diff] [blame] | 816 | != TFM_HAL_SUCCESS) { |
| 817 | tfm_core_panic(); |
| 818 | } |
David Hu | f07e97d | 2021-02-15 22:05:40 +0800 | [diff] [blame] | 819 | #endif /* TFM_FIH_PROFILE_ON */ |
Kevin Peng | 25b190b | 2020-10-30 17:10:45 +0800 | [diff] [blame] | 820 | } |
| 821 | #endif /* TFM_LVL == 3 */ |
| 822 | #endif /* TFM_LVL != 1 */ |
Mate Toth-Pal | c430b99 | 2019-05-09 21:01:14 +0200 | [diff] [blame] | 823 | |
Summer Qin | d2ad7e7 | 2020-01-06 18:16:35 +0800 | [diff] [blame] | 824 | tfm_core_thrd_switch_context(p_actx, pth_curr, pth_next); |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 825 | } |
David Hu | fb38d56 | 2019-09-23 15:58:34 +0800 | [diff] [blame] | 826 | |
| 827 | /* |
| 828 | * Handle pending mailbox message from NS in multi-core topology. |
| 829 | * Empty operation on single Armv8-M platform. |
| 830 | */ |
| 831 | tfm_rpc_client_call_handler(); |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 832 | } |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 833 | |
Summer Qin | 02f7f07 | 2020-08-24 16:02:54 +0800 | [diff] [blame] | 834 | void update_caller_outvec_len(struct tfm_msg_body_t *msg) |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 835 | { |
| 836 | uint32_t i; |
| 837 | |
| 838 | /* |
| 839 | * FixeMe: abstract these part into dedicated functions to avoid |
| 840 | * accessing thread context in psa layer |
| 841 | */ |
| 842 | /* If it is a NS request via RPC, the owner of this message is not set */ |
| 843 | if (!is_tfm_rpc_msg(msg)) { |
| 844 | TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK); |
| 845 | } |
| 846 | |
| 847 | for (i = 0; i < PSA_MAX_IOVEC; i++) { |
| 848 | if (msg->msg.out_size[i] == 0) { |
| 849 | continue; |
| 850 | } |
| 851 | |
| 852 | TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base); |
| 853 | |
| 854 | msg->caller_outvec[i].len = msg->outvec[i].len; |
| 855 | } |
| 856 | } |
| 857 | |
Summer Qin | 02f7f07 | 2020-08-24 16:02:54 +0800 | [diff] [blame] | 858 | void notify_with_signal(int32_t partition_id, psa_signal_t signal) |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 859 | { |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 860 | struct partition_t *partition = NULL; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 861 | |
| 862 | /* |
| 863 | * The value of partition_id must be greater than zero as the target of |
| 864 | * notification must be a Secure Partition, providing a Non-secure |
| 865 | * Partition ID is a fatal error. |
| 866 | */ |
| 867 | if (!TFM_CLIENT_ID_IS_S(partition_id)) { |
| 868 | tfm_core_panic(); |
| 869 | } |
| 870 | |
| 871 | /* |
| 872 | * It is a fatal error if partition_id does not correspond to a Secure |
| 873 | * Partition. |
| 874 | */ |
| 875 | partition = tfm_spm_get_partition_by_id(partition_id); |
| 876 | if (!partition) { |
| 877 | tfm_core_panic(); |
| 878 | } |
| 879 | |
Mingyang Sun | af22ffa | 2020-07-09 17:48:37 +0800 | [diff] [blame] | 880 | partition->signals_asserted |= signal; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 881 | |
Kevin Peng | 8dac610 | 2021-03-09 16:44:00 +0800 | [diff] [blame] | 882 | if (partition->signals_waiting & signal) { |
| 883 | tfm_event_wake( |
| 884 | &partition->event, |
| 885 | partition->signals_asserted & partition->signals_waiting); |
| 886 | partition->signals_waiting &= ~signal; |
| 887 | } |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 888 | } |
| 889 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 890 | /** |
Kevin Peng | dc79188 | 2021-03-12 10:57:12 +0800 | [diff] [blame] | 891 | * \brief Sets signal to partition for Second-Level Interrupt Handling mode IRQ |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 892 | * |
| 893 | * \param[in] partition_id The ID of the partition which handles this IRQ |
| 894 | * \param[in] signal The signal associated with this IRQ |
| 895 | * \param[in] irq_line The number of the IRQ line |
| 896 | * |
| 897 | * \retval void Success. |
| 898 | * \retval "Does not return" Partition ID is invalid |
| 899 | */ |
Kevin Peng | dc79188 | 2021-03-12 10:57:12 +0800 | [diff] [blame] | 900 | void tfm_set_irq_signal(uint32_t partition_id, psa_signal_t signal, |
| 901 | uint32_t irq_line) |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 902 | { |
Kevin Peng | dc79188 | 2021-03-12 10:57:12 +0800 | [diff] [blame] | 903 | __disable_irq(); |
| 904 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 905 | tfm_spm_hal_disable_irq(irq_line); |
| 906 | notify_with_signal(partition_id, signal); |
Kevin Peng | dc79188 | 2021-03-12 10:57:12 +0800 | [diff] [blame] | 907 | |
| 908 | __enable_irq(); |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 909 | } |
| 910 | |
Kevin Peng | 27e4227 | 2021-05-24 17:58:53 +0800 | [diff] [blame] | 911 | struct irq_load_info_t *get_irq_info_for_signal( |
| 912 | const struct partition_load_info_t *p_ldinf, |
| 913 | psa_signal_t signal) |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 914 | { |
| 915 | size_t i; |
Kevin Peng | 27e4227 | 2021-05-24 17:58:53 +0800 | [diff] [blame] | 916 | struct irq_load_info_t *irq_info; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 917 | |
Ken Liu | 24dffb2 | 2021-02-10 11:03:58 +0800 | [diff] [blame] | 918 | if (!IS_ONLY_ONE_BIT_IN_UINT32(signal)) { |
Kevin Peng | 27e4227 | 2021-05-24 17:58:53 +0800 | [diff] [blame] | 919 | return NULL; |
Kevin Peng | 410bee5 | 2021-01-13 16:27:17 +0800 | [diff] [blame] | 920 | } |
| 921 | |
Kevin Peng | 27e4227 | 2021-05-24 17:58:53 +0800 | [diff] [blame] | 922 | irq_info = (struct irq_load_info_t *)LOAD_INFO_IRQ(p_ldinf); |
| 923 | for (i = 0; i < p_ldinf->nirqs; i++) { |
| 924 | if (irq_info[i].signal == signal) { |
| 925 | return &irq_info[i]; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 926 | } |
| 927 | } |
Kevin Peng | a20b5af | 2021-01-11 11:20:52 +0800 | [diff] [blame] | 928 | |
Kevin Peng | 27e4227 | 2021-05-24 17:58:53 +0800 | [diff] [blame] | 929 | return NULL; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 930 | } |
| 931 | |
Summer Qin | dea1f2c | 2021-01-11 14:46:34 +0800 | [diff] [blame] | 932 | #if !defined(__ARM_ARCH_8_1M_MAIN__) |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 933 | void tfm_spm_validate_caller(struct partition_t *p_cur_sp, uint32_t *p_ctx, |
| 934 | uint32_t exc_return, bool ns_caller) |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 935 | { |
| 936 | uintptr_t stacked_ctx_pos; |
| 937 | |
| 938 | if (ns_caller) { |
| 939 | /* |
| 940 | * The background IRQ can't be supported, since if SP is executing, |
| 941 | * the preempted context of SP can be different with the one who |
| 942 | * preempts veneer. |
| 943 | */ |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 944 | if (p_cur_sp->p_ldinf->pid != TFM_SP_NON_SECURE_ID) { |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 945 | tfm_core_panic(); |
| 946 | } |
| 947 | |
| 948 | /* |
| 949 | * It is non-secure caller, check if veneer stack contains |
| 950 | * multiple contexts. |
| 951 | */ |
| 952 | stacked_ctx_pos = (uintptr_t)p_ctx + |
| 953 | sizeof(struct tfm_state_context_t) + |
Ken Liu | 05e13ba | 2020-07-25 10:31:33 +0800 | [diff] [blame] | 954 | TFM_STACK_SEALED_SIZE; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 955 | |
| 956 | if (is_stack_alloc_fp_space(exc_return)) { |
Xinyu Zhang | 3a45324 | 2021-04-16 17:57:09 +0800 | [diff] [blame] | 957 | #if defined(__FPU_USED) && (__FPU_USED == 1U) |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 958 | if (FPU->FPCCR & FPU_FPCCR_TS_Msk) { |
| 959 | stacked_ctx_pos += TFM_ADDTIONAL_FP_CONTEXT_WORDS * |
| 960 | sizeof(uint32_t); |
| 961 | } |
| 962 | #endif |
| 963 | stacked_ctx_pos += TFM_BASIC_FP_CONTEXT_WORDS * sizeof(uint32_t); |
| 964 | } |
| 965 | |
Mingyang Sun | af22ffa | 2020-07-09 17:48:37 +0800 | [diff] [blame] | 966 | if (stacked_ctx_pos != p_cur_sp->sp_thread.stk_top) { |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 967 | tfm_core_panic(); |
| 968 | } |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 969 | } else if (p_cur_sp->p_ldinf->pid <= 0) { |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 970 | tfm_core_panic(); |
| 971 | } |
| 972 | } |
Summer Qin | dea1f2c | 2021-01-11 14:46:34 +0800 | [diff] [blame] | 973 | #endif |
Summer Qin | 830c554 | 2020-02-14 13:44:20 +0800 | [diff] [blame] | 974 | |
| 975 | void tfm_spm_request_handler(const struct tfm_state_context_t *svc_ctx) |
| 976 | { |
| 977 | uint32_t *res_ptr = (uint32_t *)&svc_ctx->r0; |
| 978 | uint32_t running_partition_flags = 0; |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 979 | const struct partition_t *partition = NULL; |
Summer Qin | 830c554 | 2020-02-14 13:44:20 +0800 | [diff] [blame] | 980 | |
| 981 | /* Check permissions on request type basis */ |
| 982 | |
| 983 | switch (svc_ctx->r0) { |
| 984 | case TFM_SPM_REQUEST_RESET_VOTE: |
| 985 | partition = tfm_spm_get_running_partition(); |
| 986 | if (!partition) { |
| 987 | tfm_core_panic(); |
| 988 | } |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 989 | running_partition_flags = partition->p_ldinf->flags; |
Summer Qin | 830c554 | 2020-02-14 13:44:20 +0800 | [diff] [blame] | 990 | |
| 991 | /* Currently only PSA Root of Trust services are allowed to make Reset |
| 992 | * vote request |
| 993 | */ |
| 994 | if ((running_partition_flags & SPM_PART_FLAG_PSA_ROT) == 0) { |
| 995 | *res_ptr = (uint32_t)TFM_ERROR_GENERIC; |
| 996 | } |
| 997 | |
| 998 | /* FixMe: this is a placeholder for checks to be performed before |
| 999 | * allowing execution of reset |
| 1000 | */ |
| 1001 | *res_ptr = (uint32_t)TFM_SUCCESS; |
| 1002 | |
| 1003 | break; |
| 1004 | default: |
| 1005 | *res_ptr = (uint32_t)TFM_ERROR_INVALID_PARAMETER; |
| 1006 | } |
| 1007 | } |