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" |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 14 | #include "thread.h" |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 15 | #include "internal_errors.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 16 | #include "tfm_spm_hal.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 17 | #include "tfm_api.h" |
| 18 | #include "tfm_secure_api.h" |
| 19 | #include "tfm_memory_utils.h" |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 20 | #include "tfm_hal_defs.h" |
Kevin Peng | d399a1f | 2021-09-08 15:33:14 +0800 | [diff] [blame^] | 21 | #include "tfm_hal_interrupt.h" |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 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" |
Kevin Peng | 385fda8 | 2021-08-18 10:41:19 +0800 | [diff] [blame] | 26 | #include "tfm_nspm.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 27 | #include "tfm_rpc.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 28 | #include "tfm_core_trustzone.h" |
Ken Liu | 24dffb2 | 2021-02-10 11:03:58 +0800 | [diff] [blame] | 29 | #include "lists.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 30 | #include "tfm_pools.h" |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 31 | #include "region.h" |
Mingyang Sun | 7397b4f | 2020-06-17 15:07:45 +0800 | [diff] [blame] | 32 | #include "psa_manifest/pid.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" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 37 | |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 38 | /* Partition and service runtime data list head/runtime data table */ |
| 39 | static struct partition_head_t partitions_listhead; |
| 40 | static struct service_head_t services_listhead; |
Ken Liu | b3b2cb6 | 2021-05-22 00:39:28 +0800 | [diff] [blame] | 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 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 43 | struct thread_t *pth_curr; |
| 44 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 45 | /* Pools */ |
| 46 | TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t), |
| 47 | TFM_CONN_HANDLE_MAX_NUM); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 48 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 49 | #include "tfm_secure_irq_handlers_ipc.inc" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 50 | |
Summer Qin | 373feb1 | 2020-03-27 15:35:33 +0800 | [diff] [blame] | 51 | /*********************** Connection handle conversion APIs *******************/ |
| 52 | |
Summer Qin | 373feb1 | 2020-03-27 15:35:33 +0800 | [diff] [blame] | 53 | #define CONVERSION_FACTOR_BITOFFSET 3 |
| 54 | #define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET) |
| 55 | /* Set 32 as the maximum */ |
| 56 | #define CONVERSION_FACTOR_VALUE_MAX 0x20 |
| 57 | |
| 58 | #if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX |
| 59 | #error "CONVERSION FACTOR OUT OF RANGE" |
| 60 | #endif |
| 61 | |
| 62 | static uint32_t loop_index; |
| 63 | |
| 64 | /* |
| 65 | * A handle instance psa_handle_t allocated inside SPM is actually a memory |
| 66 | * address among the handle pool. Return this handle to the client directly |
| 67 | * exposes information of secure memory address. In this case, converting the |
| 68 | * handle into another value does not represent the memory address to avoid |
| 69 | * exposing secure memory directly to clients. |
| 70 | * |
| 71 | * This function converts the handle instance into another value by scaling the |
| 72 | * handle in pool offset, the converted value is named as a user handle. |
| 73 | * |
| 74 | * The formula: |
| 75 | * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE + |
| 76 | * CLIENT_HANDLE_VALUE_MIN + loop_index |
| 77 | * where: |
| 78 | * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not |
| 79 | * exceed CONVERSION_FACTOR_VALUE_MAX. |
| 80 | * |
| 81 | * handle_instance in RANGE[POOL_START, POOL_END] |
| 82 | * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF] |
| 83 | * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1] |
| 84 | * |
| 85 | * note: |
| 86 | * loop_index is used to promise same handle instance is converted into |
| 87 | * different user handles in short time. |
| 88 | */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 89 | 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] | 90 | { |
| 91 | psa_handle_t user_handle; |
| 92 | |
| 93 | loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE; |
| 94 | user_handle = (psa_handle_t)((((uintptr_t)handle_instance - |
| 95 | (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) + |
| 96 | CLIENT_HANDLE_VALUE_MIN + loop_index); |
| 97 | |
| 98 | return user_handle; |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * This function converts a user handle into a corresponded handle instance. |
| 103 | * The converted value is validated before returning, an invalid handle instance |
| 104 | * is returned as NULL. |
| 105 | * |
| 106 | * The formula: |
| 107 | * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) / |
| 108 | * CONVERSION_FACTOR_VALUE) + POOL_START |
| 109 | * where: |
| 110 | * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not |
| 111 | * exceed CONVERSION_FACTOR_VALUE_MAX. |
| 112 | * |
| 113 | * handle_instance in RANGE[POOL_START, POOL_END] |
| 114 | * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF] |
| 115 | * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1] |
| 116 | */ |
| 117 | struct tfm_conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle) |
| 118 | { |
| 119 | struct tfm_conn_handle_t *handle_instance; |
| 120 | |
| 121 | if (user_handle == PSA_NULL_HANDLE) { |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | handle_instance = (struct tfm_conn_handle_t *)((((uintptr_t)user_handle - |
| 126 | CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) + |
| 127 | (uintptr_t)conn_handle_pool); |
| 128 | |
| 129 | return handle_instance; |
| 130 | } |
| 131 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 132 | /* Service handle management functions */ |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 133 | struct tfm_conn_handle_t *tfm_spm_create_conn_handle(struct service_t *service, |
| 134 | int32_t client_id) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 135 | { |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 136 | struct tfm_conn_handle_t *p_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 137 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 138 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 139 | |
| 140 | /* Get buffer for handle list structure from handle pool */ |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 141 | p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool); |
| 142 | if (!p_handle) { |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 143 | return NULL; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 144 | } |
| 145 | |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 146 | p_handle->service = service; |
Shawn Shan | cc39fcb | 2019-11-13 15:38:16 +0800 | [diff] [blame] | 147 | p_handle->status = TFM_HANDLE_STATUS_IDLE; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 148 | p_handle->client_id = client_id; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 149 | |
| 150 | /* Add handle node to list for next psa functions */ |
Ken Liu | 2c47f7f | 2021-01-22 11:06:04 +0800 | [diff] [blame] | 151 | BI_LIST_INSERT_BEFORE(&service->handle_list, &p_handle->list); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 152 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 153 | return p_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 154 | } |
| 155 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 156 | int32_t tfm_spm_validate_conn_handle( |
| 157 | const struct tfm_conn_handle_t *conn_handle, |
| 158 | int32_t client_id) |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 159 | { |
| 160 | /* Check the handle address is validated */ |
| 161 | if (is_valid_chunk_data_in_pool(conn_handle_pool, |
| 162 | (uint8_t *)conn_handle) != true) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 163 | return SPM_ERROR_GENERIC; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /* Check the handle caller is correct */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 167 | if (conn_handle->client_id != client_id) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 168 | return SPM_ERROR_GENERIC; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 169 | } |
| 170 | |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 171 | return SPM_SUCCESS; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 172 | } |
| 173 | |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 174 | int32_t tfm_spm_free_conn_handle(struct service_t *service, |
Summer Qin | 02f7f07 | 2020-08-24 16:02:54 +0800 | [diff] [blame] | 175 | struct tfm_conn_handle_t *conn_handle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 176 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 177 | TFM_CORE_ASSERT(service); |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 178 | TFM_CORE_ASSERT(conn_handle != NULL); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 179 | |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 180 | /* Clear magic as the handler is not used anymore */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 181 | conn_handle->internal_msg.magic = 0; |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 182 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 183 | /* Remove node from handle list */ |
Ken Liu | 2c47f7f | 2021-01-22 11:06:04 +0800 | [diff] [blame] | 184 | BI_LIST_REMOVE_NODE(&conn_handle->list); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 185 | |
| 186 | /* Back handle buffer to pool */ |
Ken Liu | 66ca613 | 2021-02-24 08:49:51 +0800 | [diff] [blame] | 187 | tfm_pool_free(conn_handle_pool, conn_handle); |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 188 | return SPM_SUCCESS; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 189 | } |
| 190 | |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 191 | int32_t tfm_spm_set_rhandle(struct service_t *service, |
Summer Qin | 02f7f07 | 2020-08-24 16:02:54 +0800 | [diff] [blame] | 192 | struct tfm_conn_handle_t *conn_handle, |
| 193 | void *rhandle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 194 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 195 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 196 | /* Set reverse handle value only be allowed for a connected handle */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 197 | TFM_CORE_ASSERT(conn_handle != NULL); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 198 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 199 | conn_handle->rhandle = rhandle; |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 200 | return SPM_SUCCESS; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 201 | } |
| 202 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 203 | /** |
Xinyu Zhang | 3a45324 | 2021-04-16 17:57:09 +0800 | [diff] [blame] | 204 | * \brief Get reverse handle value from connection handle. |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 205 | * |
| 206 | * \param[in] service Target service context pointer |
| 207 | * \param[in] conn_handle Connection handle created by |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 208 | * tfm_spm_create_conn_handle() |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 209 | * |
| 210 | * \retval void * Success |
| 211 | * \retval "Does not return" Panic for those: |
| 212 | * service pointer are NULL |
Xinyu Zhang | 3a45324 | 2021-04-16 17:57:09 +0800 | [diff] [blame] | 213 | * handle is \ref PSA_NULL_HANDLE |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 214 | * handle node does not be found |
| 215 | */ |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 216 | static void *tfm_spm_get_rhandle(struct service_t *service, |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 217 | struct tfm_conn_handle_t *conn_handle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 218 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 219 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 220 | /* Get reverse handle value only be allowed for a connected handle */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 221 | TFM_CORE_ASSERT(conn_handle != NULL); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 222 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 223 | return conn_handle->rhandle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* Partition management functions */ |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 227 | |
Summer Qin | 02f7f07 | 2020-08-24 16:02:54 +0800 | [diff] [blame] | 228 | struct tfm_msg_body_t *tfm_spm_get_msg_by_signal(struct partition_t *partition, |
| 229 | psa_signal_t signal) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 230 | { |
Ken Liu | 2c47f7f | 2021-01-22 11:06:04 +0800 | [diff] [blame] | 231 | struct bi_list_node_t *node, *head; |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 232 | struct tfm_msg_body_t *tmp_msg, *msg = NULL; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 233 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 234 | TFM_CORE_ASSERT(partition); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 235 | |
Mingyang Sun | af22ffa | 2020-07-09 17:48:37 +0800 | [diff] [blame] | 236 | head = &partition->msg_list; |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 237 | |
Ken Liu | 2c47f7f | 2021-01-22 11:06:04 +0800 | [diff] [blame] | 238 | if (BI_LIST_IS_EMPTY(head)) { |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 239 | return NULL; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 240 | } |
| 241 | |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 242 | /* |
| 243 | * There may be multiple messages for this RoT Service signal, do not clear |
| 244 | * partition mask until no remaining message. Search may be optimized. |
| 245 | */ |
Ken Liu | 2c47f7f | 2021-01-22 11:06:04 +0800 | [diff] [blame] | 246 | BI_LIST_FOR_EACH(node, head) { |
Ken Liu | c25558c | 2021-05-20 15:31:28 +0800 | [diff] [blame] | 247 | tmp_msg = TO_CONTAINER(node, struct tfm_msg_body_t, msg_node); |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 248 | if (tmp_msg->service->p_ldinf->signal == signal && msg) { |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 249 | return msg; |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 250 | } else if (tmp_msg->service->p_ldinf->signal == signal) { |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 251 | msg = tmp_msg; |
Ken Liu | 2c47f7f | 2021-01-22 11:06:04 +0800 | [diff] [blame] | 252 | BI_LIST_REMOVE_NODE(node); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 253 | } |
| 254 | } |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 255 | |
Mingyang Sun | af22ffa | 2020-07-09 17:48:37 +0800 | [diff] [blame] | 256 | partition->signals_asserted &= ~signal; |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 257 | |
| 258 | return msg; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 259 | } |
| 260 | |
Mingyang Sun | da30f1e | 2020-07-13 17:20:32 +0800 | [diff] [blame] | 261 | uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags) |
| 262 | { |
Kevin Peng | bf9a97e | 2021-06-18 16:34:12 +0800 | [diff] [blame] | 263 | #if TFM_LVL == 1 |
| 264 | return TFM_PARTITION_PRIVILEGED_MODE; |
| 265 | #else /* TFM_LVL == 1 */ |
Mingyang Sun | da30f1e | 2020-07-13 17:20:32 +0800 | [diff] [blame] | 266 | if (partition_flags & SPM_PART_FLAG_PSA_ROT) { |
| 267 | return TFM_PARTITION_PRIVILEGED_MODE; |
| 268 | } else { |
| 269 | return TFM_PARTITION_UNPRIVILEGED_MODE; |
| 270 | } |
Kevin Peng | bf9a97e | 2021-06-18 16:34:12 +0800 | [diff] [blame] | 271 | #endif /* TFM_LVL == 1 */ |
Mingyang Sun | da30f1e | 2020-07-13 17:20:32 +0800 | [diff] [blame] | 272 | } |
| 273 | |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 274 | struct service_t *tfm_spm_get_service_by_sid(uint32_t sid) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 275 | { |
Feder.Liang | aeb5a79 | 2021-08-10 16:12:56 +0800 | [diff] [blame] | 276 | struct service_t *p_prev, *p_curr; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 277 | |
Feder.Liang | aeb5a79 | 2021-08-10 16:12:56 +0800 | [diff] [blame] | 278 | UNI_LIST_FOR_EACH_PREV(p_prev, p_curr, &services_listhead) { |
| 279 | if (p_curr->p_ldinf->sid == sid) { |
| 280 | UNI_LIST_MOVE_AFTER(&services_listhead, p_prev, p_curr); |
| 281 | return p_curr; |
Mingyang Sun | ef42f44 | 2021-06-11 15:07:58 +0800 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 285 | return NULL; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 286 | } |
| 287 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 288 | /** |
| 289 | * \brief Get the partition context by partition ID. |
| 290 | * |
| 291 | * \param[in] partition_id Partition identity |
| 292 | * |
| 293 | * \retval NULL Failed |
| 294 | * \retval "Not NULL" Target partition context pointer, |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 295 | * \ref partition_t structures |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 296 | */ |
Kevin Peng | eca45b9 | 2021-02-09 14:46:50 +0800 | [diff] [blame] | 297 | 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] | 298 | { |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 299 | struct partition_t *p_part; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 300 | |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 301 | UNI_LIST_FOR_EACH(p_part, &partitions_listhead) { |
| 302 | if (p_part->p_ldinf->pid == partition_id) { |
| 303 | return p_part; |
| 304 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 305 | } |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 306 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 307 | return NULL; |
| 308 | } |
| 309 | |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 310 | struct partition_t *tfm_spm_get_running_partition(void) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 311 | { |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 312 | return TO_CONTAINER(pth_curr, struct partition_t, thrd); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 313 | } |
| 314 | |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 315 | int32_t tfm_spm_check_client_version(struct service_t *service, |
Jaykumar Pitambarbhai Patel | 3a98602 | 2019-10-08 17:37:15 +0530 | [diff] [blame] | 316 | uint32_t version) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 317 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 318 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 319 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 320 | switch (SERVICE_GET_VERSION_POLICY(service->p_ldinf->flags)) { |
Ken Liu | b3b2cb6 | 2021-05-22 00:39:28 +0800 | [diff] [blame] | 321 | case SERVICE_VERSION_POLICY_RELAXED: |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 322 | if (version > service->p_ldinf->version) { |
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 | } |
| 325 | break; |
Ken Liu | b3b2cb6 | 2021-05-22 00:39:28 +0800 | [diff] [blame] | 326 | case SERVICE_VERSION_POLICY_STRICT: |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 327 | if (version != service->p_ldinf->version) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 328 | return SPM_ERROR_VERSION; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 329 | } |
| 330 | break; |
| 331 | default: |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 332 | return SPM_ERROR_VERSION; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 333 | } |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 334 | return SPM_SUCCESS; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 335 | } |
| 336 | |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 337 | int32_t tfm_spm_check_authorization(uint32_t sid, |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 338 | struct service_t *service, |
Summer Qin | 618e8c3 | 2019-12-09 10:47:20 +0800 | [diff] [blame] | 339 | bool ns_caller) |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 340 | { |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 341 | struct partition_t *partition = NULL; |
Mingyang Sun | 2b35266 | 2021-04-21 11:35:43 +0800 | [diff] [blame] | 342 | uint32_t *dep; |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 343 | int32_t i; |
| 344 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 345 | TFM_CORE_ASSERT(service); |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 346 | |
| 347 | if (ns_caller) { |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 348 | if (!SERVICE_IS_NS_ACCESSIBLE(service->p_ldinf->flags)) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 349 | return SPM_ERROR_GENERIC; |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 350 | } |
| 351 | } else { |
| 352 | partition = tfm_spm_get_running_partition(); |
| 353 | if (!partition) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 354 | tfm_core_panic(); |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 355 | } |
| 356 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 357 | dep = (uint32_t *)LOAD_INFO_DEPS(partition->p_ldinf); |
| 358 | for (i = 0; i < partition->p_ldinf->ndeps; i++) { |
Mingyang Sun | 2b35266 | 2021-04-21 11:35:43 +0800 | [diff] [blame] | 359 | if (dep[i] == sid) { |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 360 | break; |
| 361 | } |
| 362 | } |
| 363 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 364 | if (i == partition->p_ldinf->ndeps) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 365 | return SPM_ERROR_GENERIC; |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 366 | } |
| 367 | } |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 368 | return SPM_SUCCESS; |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 369 | } |
| 370 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 371 | /* Message functions */ |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 372 | |
Summer Qin | 02f7f07 | 2020-08-24 16:02:54 +0800 | [diff] [blame] | 373 | 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] | 374 | { |
| 375 | /* |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 376 | * The message handler passed by the caller is considered invalid in the |
| 377 | * following cases: |
| 378 | * 1. Not a valid message handle. (The address of a message is not the |
| 379 | * address of a possible handle from the pool |
| 380 | * 2. Handle not belongs to the caller partition (The handle is either |
| 381 | * unused, or owned by anither partition) |
| 382 | * Check the conditions above |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 383 | */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 384 | struct tfm_msg_body_t *p_msg; |
Kevin Peng | eec41a8 | 2021-08-18 13:56:23 +0800 | [diff] [blame] | 385 | int32_t partition_id; |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 386 | struct tfm_conn_handle_t *p_conn_handle = |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 387 | tfm_spm_to_handle_instance(msg_handle); |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 388 | |
| 389 | if (is_valid_chunk_data_in_pool( |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 390 | conn_handle_pool, (uint8_t *)p_conn_handle) != 1) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 391 | return NULL; |
| 392 | } |
| 393 | |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 394 | p_msg = &p_conn_handle->internal_msg; |
| 395 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 396 | /* |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 397 | * Check that the magic number is correct. This proves that the message |
| 398 | * structure contains an active message. |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 399 | */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 400 | if (p_msg->magic != TFM_MSG_MAGIC) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 401 | return NULL; |
| 402 | } |
| 403 | |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 404 | /* Check that the running partition owns the message */ |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 405 | partition_id = tfm_spm_partition_get_running_partition_id(); |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 406 | if (partition_id != p_msg->service->partition->p_ldinf->pid) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 407 | return NULL; |
| 408 | } |
| 409 | |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 410 | return p_msg; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 411 | } |
| 412 | |
Kevin Peng | df6aa29 | 2021-03-11 17:58:50 +0800 | [diff] [blame] | 413 | struct tfm_msg_body_t * |
| 414 | tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle) |
| 415 | { |
| 416 | TFM_CORE_ASSERT(conn_handle != NULL); |
| 417 | |
| 418 | return &(conn_handle->internal_msg); |
| 419 | } |
| 420 | |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 421 | void tfm_spm_fill_msg(struct tfm_msg_body_t *msg, |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 422 | struct service_t *service, |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 423 | psa_handle_t handle, |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 424 | int32_t type, int32_t client_id, |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 425 | psa_invec *invec, size_t in_len, |
| 426 | psa_outvec *outvec, size_t out_len, |
| 427 | psa_outvec *caller_outvec) |
| 428 | { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 429 | uint32_t i; |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 430 | struct tfm_conn_handle_t *conn_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 431 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 432 | TFM_CORE_ASSERT(msg); |
| 433 | TFM_CORE_ASSERT(service); |
| 434 | TFM_CORE_ASSERT(!(invec == NULL && in_len != 0)); |
| 435 | TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0)); |
| 436 | TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC); |
| 437 | TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC); |
| 438 | TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 439 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 440 | /* Clear message buffer before using it */ |
Summer Qin | f24dbb5 | 2020-07-23 14:53:54 +0800 | [diff] [blame] | 441 | spm_memset(msg, 0, sizeof(struct tfm_msg_body_t)); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 442 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 443 | THRD_SYNC_INIT(&msg->ack_evnt); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 444 | msg->magic = TFM_MSG_MAGIC; |
| 445 | msg->service = service; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 446 | msg->caller_outvec = caller_outvec; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 447 | msg->msg.client_id = client_id; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 448 | |
| 449 | /* Copy contents */ |
| 450 | msg->msg.type = type; |
| 451 | |
| 452 | for (i = 0; i < in_len; i++) { |
| 453 | msg->msg.in_size[i] = invec[i].len; |
| 454 | msg->invec[i].base = invec[i].base; |
| 455 | } |
| 456 | |
| 457 | for (i = 0; i < out_len; i++) { |
| 458 | msg->msg.out_size[i] = outvec[i].len; |
| 459 | msg->outvec[i].base = outvec[i].base; |
| 460 | /* Out len is used to record the writed number, set 0 here again */ |
| 461 | msg->outvec[i].len = 0; |
| 462 | } |
| 463 | |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 464 | /* Use the user connect handle as the message handle */ |
| 465 | msg->msg.handle = handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 466 | |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 467 | conn_handle = tfm_spm_to_handle_instance(handle); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 468 | /* For connected handle, set rhandle to every message */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 469 | if (conn_handle) { |
| 470 | msg->msg.rhandle = tfm_spm_get_rhandle(service, conn_handle); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 471 | } |
David Hu | 46603dd | 2019-12-11 18:05:16 +0800 | [diff] [blame] | 472 | |
| 473 | /* Set the private data of NSPE client caller in multi-core topology */ |
| 474 | if (TFM_CLIENT_ID_IS_NS(client_id)) { |
| 475 | tfm_rpc_set_caller_data(msg, client_id); |
| 476 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 477 | } |
| 478 | |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 479 | void tfm_spm_send_event(struct service_t *service, |
Kevin Peng | 8dac610 | 2021-03-09 16:44:00 +0800 | [diff] [blame] | 480 | struct tfm_msg_body_t *msg) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 481 | { |
Kevin Peng | 8dac610 | 2021-03-09 16:44:00 +0800 | [diff] [blame] | 482 | struct partition_t *partition = NULL; |
| 483 | psa_signal_t signal = 0; |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 484 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 485 | if (!msg || !service || !service->p_ldinf || !service->partition) { |
Kevin Peng | 8dac610 | 2021-03-09 16:44:00 +0800 | [diff] [blame] | 486 | tfm_core_panic(); |
| 487 | } |
| 488 | |
| 489 | partition = service->partition; |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 490 | signal = service->p_ldinf->signal; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 491 | |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 492 | /* Add message to partition message list tail */ |
Ken Liu | 2c47f7f | 2021-01-22 11:06:04 +0800 | [diff] [blame] | 493 | BI_LIST_INSERT_BEFORE(&partition->msg_list, &msg->msg_node); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 494 | |
| 495 | /* Messages put. Update signals */ |
Kevin Peng | 8dac610 | 2021-03-09 16:44:00 +0800 | [diff] [blame] | 496 | partition->signals_asserted |= signal; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 497 | |
Kevin Peng | 8dac610 | 2021-03-09 16:44:00 +0800 | [diff] [blame] | 498 | if (partition->signals_waiting & signal) { |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 499 | thrd_wake_up( |
| 500 | &partition->waitobj, |
Kevin Peng | 8dac610 | 2021-03-09 16:44:00 +0800 | [diff] [blame] | 501 | (partition->signals_asserted & partition->signals_waiting)); |
| 502 | partition->signals_waiting &= ~signal; |
| 503 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 504 | |
David Hu | fb38d56 | 2019-09-23 15:58:34 +0800 | [diff] [blame] | 505 | /* |
| 506 | * If it is a NS request via RPC, it is unnecessary to block current |
| 507 | * thread. |
| 508 | */ |
| 509 | if (!is_tfm_rpc_msg(msg)) { |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 510 | thrd_wait_on(&msg->ack_evnt, pth_curr); |
David Hu | fb38d56 | 2019-09-23 15:58:34 +0800 | [diff] [blame] | 511 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 512 | } |
| 513 | |
Kevin Peng | eec41a8 | 2021-08-18 13:56:23 +0800 | [diff] [blame] | 514 | int32_t tfm_spm_partition_get_running_partition_id(void) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 515 | { |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 516 | struct partition_t *partition; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 517 | |
Kevin Peng | 79c2bda | 2020-07-24 16:31:12 +0800 | [diff] [blame] | 518 | partition = tfm_spm_get_running_partition(); |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 519 | if (partition && partition->p_ldinf) { |
| 520 | return partition->p_ldinf->pid; |
Kevin Peng | 79c2bda | 2020-07-24 16:31:12 +0800 | [diff] [blame] | 521 | } else { |
| 522 | return INVALID_PARTITION_ID; |
| 523 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 524 | } |
| 525 | |
Summer Qin | 43c185d | 2019-10-10 15:44:42 +0800 | [diff] [blame] | 526 | 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] | 527 | enum tfm_memory_access_e access, |
| 528 | uint32_t privileged) |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 529 | { |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 530 | enum tfm_hal_status_t err; |
| 531 | uint32_t attr = 0; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 532 | |
| 533 | /* If len is zero, this indicates an empty buffer and base is ignored */ |
| 534 | if (len == 0) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 535 | return SPM_SUCCESS; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | if (!buffer) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 539 | return SPM_ERROR_BAD_PARAMETERS; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | if ((uintptr_t)buffer > (UINTPTR_MAX - len)) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 543 | return SPM_ERROR_MEMORY_CHECK; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 544 | } |
| 545 | |
Summer Qin | 424d4db | 2019-03-25 14:09:51 +0800 | [diff] [blame] | 546 | if (access == TFM_MEMORY_ACCESS_RW) { |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 547 | attr |= (TFM_HAL_ACCESS_READABLE | TFM_HAL_ACCESS_WRITABLE); |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 548 | } else { |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 549 | attr |= TFM_HAL_ACCESS_READABLE; |
Summer Qin | 424d4db | 2019-03-25 14:09:51 +0800 | [diff] [blame] | 550 | } |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 551 | |
| 552 | if (privileged == TFM_PARTITION_UNPRIVILEGED_MODE) { |
| 553 | attr |= TFM_HAL_ACCESS_UNPRIVILEGED; |
| 554 | } else { |
| 555 | attr &= ~TFM_HAL_ACCESS_UNPRIVILEGED; |
| 556 | } |
| 557 | |
| 558 | if (ns_caller) { |
| 559 | attr |= TFM_HAL_ACCESS_NS; |
| 560 | } |
| 561 | |
| 562 | err = tfm_hal_memory_has_access((uintptr_t)buffer, len, attr); |
| 563 | |
| 564 | if (err == TFM_HAL_SUCCESS) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 565 | return SPM_SUCCESS; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 566 | } |
| 567 | |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 568 | return SPM_ERROR_MEMORY_CHECK; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 569 | } |
| 570 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 571 | bool tfm_spm_is_ns_caller(void) |
| 572 | { |
| 573 | #if defined(TFM_MULTI_CORE_TOPOLOGY) || defined(FORWARD_PROT_MSG) |
| 574 | /* Multi-core NS PSA API request is processed by pendSV. */ |
| 575 | return (__get_active_exc_num() == EXC_NUM_PENDSV); |
| 576 | #else |
| 577 | struct partition_t *partition = tfm_spm_get_running_partition(); |
Mingyang Sun | e529e3b | 2021-07-12 14:46:30 +0800 | [diff] [blame] | 578 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 579 | if (!partition) { |
| 580 | tfm_core_panic(); |
| 581 | } |
| 582 | |
| 583 | return (partition->p_ldinf->pid == TFM_SP_NON_SECURE_ID); |
| 584 | #endif |
| 585 | } |
| 586 | |
Mingyang Sun | e529e3b | 2021-07-12 14:46:30 +0800 | [diff] [blame] | 587 | uint32_t tfm_spm_get_caller_privilege_mode(void) |
| 588 | { |
| 589 | struct partition_t *partition; |
| 590 | |
| 591 | #if defined(TFM_MULTI_CORE_TOPOLOGY) || defined(FORWARD_PROT_MSG) |
| 592 | /* |
| 593 | * In multi-core topology, if PSA request is from mailbox, the client |
| 594 | * is unprivileged. |
| 595 | */ |
| 596 | if (__get_active_exc_num() == EXC_NUM_PENDSV) { |
| 597 | return TFM_PARTITION_UNPRIVILEGED_MODE; |
| 598 | } |
| 599 | #endif |
| 600 | partition = tfm_spm_get_running_partition(); |
| 601 | if (!partition) { |
| 602 | tfm_core_panic(); |
| 603 | } |
| 604 | |
| 605 | return tfm_spm_partition_get_privileged_mode(partition->p_ldinf->flags); |
| 606 | } |
| 607 | |
Kevin Peng | 385fda8 | 2021-08-18 10:41:19 +0800 | [diff] [blame] | 608 | int32_t tfm_spm_get_client_id(bool ns_caller) |
| 609 | { |
| 610 | int32_t client_id; |
| 611 | |
| 612 | if (ns_caller) { |
| 613 | client_id = tfm_nspm_get_current_client_id(); |
| 614 | } else { |
| 615 | client_id = tfm_spm_partition_get_running_partition_id(); |
| 616 | } |
| 617 | |
| 618 | if (ns_caller != (client_id < 0)) { |
| 619 | /* NS client ID must be negative and Secure ID must >= 0 */ |
| 620 | tfm_core_panic(); |
| 621 | } |
| 622 | |
| 623 | return client_id; |
| 624 | } |
| 625 | |
Ken Liu | ce2692d | 2020-02-11 12:39:36 +0800 | [diff] [blame] | 626 | uint32_t tfm_spm_init(void) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 627 | { |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 628 | struct partition_t *partition; |
Mingyang Sun | 8d004f7 | 2021-06-01 10:46:26 +0800 | [diff] [blame] | 629 | const struct partition_load_info_t *p_ldinf; |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 630 | void *p_param, *p_boundaries = NULL; |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 631 | |
David Hu | f07e97d | 2021-02-15 22:05:40 +0800 | [diff] [blame] | 632 | #ifdef TFM_FIH_PROFILE_ON |
| 633 | fih_int fih_rc = FIH_FAILURE; |
| 634 | #endif |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 635 | |
| 636 | tfm_pool_init(conn_handle_pool, |
| 637 | POOL_BUFFER_SIZE(conn_handle_pool), |
| 638 | sizeof(struct tfm_conn_handle_t), |
| 639 | TFM_CONN_HANDLE_MAX_NUM); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 640 | |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 641 | UNI_LISI_INIT_HEAD(&partitions_listhead); |
| 642 | UNI_LISI_INIT_HEAD(&services_listhead); |
| 643 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 644 | while (1) { |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 645 | partition = load_a_partition_assuredly(&partitions_listhead); |
Shawn Shan | 2333106 | 2021-09-01 14:58:21 +0800 | [diff] [blame] | 646 | if (partition == NO_MORE_PARTITION) { |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 647 | break; |
Kevin Peng | 79c2bda | 2020-07-24 16:31:12 +0800 | [diff] [blame] | 648 | } |
| 649 | |
Mingyang Sun | 8d004f7 | 2021-06-01 10:46:26 +0800 | [diff] [blame] | 650 | p_ldinf = partition->p_ldinf; |
Mingyang Sun | 2b35266 | 2021-04-21 11:35:43 +0800 | [diff] [blame] | 651 | |
Kevin Peng | 27e4227 | 2021-05-24 17:58:53 +0800 | [diff] [blame] | 652 | if (p_ldinf->nservices) { |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 653 | load_services_assuredly(partition, &services_listhead, |
Kevin Peng | 27e4227 | 2021-05-24 17:58:53 +0800 | [diff] [blame] | 654 | stateless_services_ref_tbl, |
| 655 | sizeof(stateless_services_ref_tbl)); |
| 656 | } |
| 657 | |
| 658 | if (p_ldinf->nirqs) { |
| 659 | load_irqs_assuredly(partition); |
| 660 | } |
| 661 | |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 662 | /* Bind the partition with plaform. */ |
| 663 | #if TFM_FIH_PROFILE_ON |
| 664 | FIH_CALL(tfm_hal_bind_boundaries, fih_rc, partition->p_ldinf, |
| 665 | &p_boundaries); |
| 666 | if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) { |
| 667 | tfm_core_panic(); |
Mingyang Sun | 61f8fbc | 2021-06-04 17:49:56 +0800 | [diff] [blame] | 668 | } |
Ken Liu | 8668628 | 2021-04-27 11:11:15 +0800 | [diff] [blame] | 669 | #else /* TFM_FIH_PROFILE_ON */ |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 670 | if (tfm_hal_bind_boundaries(partition->p_ldinf, |
| 671 | &p_boundaries) != TFM_HAL_SUCCESS) { |
| 672 | tfm_core_panic(); |
Mate Toth-Pal | 8ac98a7 | 2019-11-21 17:30:10 +0100 | [diff] [blame] | 673 | } |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 674 | #endif /* TFM_FIH_PROFILE_ON */ |
Mate Toth-Pal | 8ac98a7 | 2019-11-21 17:30:10 +0100 | [diff] [blame] | 675 | |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 676 | partition->p_boundaries = p_boundaries; |
Mingyang Sun | af22ffa | 2020-07-09 17:48:37 +0800 | [diff] [blame] | 677 | partition->signals_allowed |= PSA_DOORBELL; |
Shawn Shan | c7dda0e | 2019-12-23 14:45:09 +0800 | [diff] [blame] | 678 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 679 | THRD_SYNC_INIT(&partition->waitobj); |
Ken Liu | 2c47f7f | 2021-01-22 11:06:04 +0800 | [diff] [blame] | 680 | BI_LIST_INIT_NODE(&partition->msg_list); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 681 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 682 | THRD_INIT(&partition->thrd, &partition->ctx_ctrl, |
| 683 | TO_THREAD_PRIORITY(PARTITION_PRIORITY(p_ldinf->flags))); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 684 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 685 | p_param = NULL; |
Mingyang Sun | 8d004f7 | 2021-06-01 10:46:26 +0800 | [diff] [blame] | 686 | if (p_ldinf->pid == TFM_SP_NON_SECURE_ID) { |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 687 | p_param = (void *)tfm_spm_hal_get_ns_entry_point(); |
Ken Liu | 490281d | 2019-12-30 15:55:26 +0800 | [diff] [blame] | 688 | } |
| 689 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 690 | thrd_start(&partition->thrd, |
| 691 | POSITION_TO_ENTRY(p_ldinf->entry, thrd_fn_t), p_param, |
| 692 | LOAD_ALLOCED_STACK_ADDR(p_ldinf), |
| 693 | LOAD_ALLOCED_STACK_ADDR(p_ldinf) + p_ldinf->stack_size); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 694 | } |
| 695 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 696 | return thrd_start_scheduler(&pth_curr); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 697 | } |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 698 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 699 | /* |
| 700 | * Return both current and next context to assembly via AAPCS trick: |
| 701 | * - Returning a 64 bit integer by 32-bit R0 and R1. |
| 702 | * |
| 703 | * This is architecture-specific, hence the scheduler entry and this |
| 704 | * 'do_schedule' MAY be different on another architecture. |
| 705 | */ |
| 706 | union returning_contexts_t { |
| 707 | struct { |
| 708 | uint32_t curr; |
| 709 | uint32_t next; |
| 710 | } ctx; |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 711 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 712 | uint64_t curr_next_ctxs; |
| 713 | }; |
| 714 | |
| 715 | uint64_t do_schedule(void) |
| 716 | { |
| 717 | union returning_contexts_t ret; |
| 718 | struct partition_t *p_part_curr, *p_part_next; |
| 719 | struct thread_t *pth_next = thrd_next(); |
| 720 | |
| 721 | p_part_curr = TO_CONTAINER(pth_curr, struct partition_t, thrd); |
| 722 | p_part_next = TO_CONTAINER(pth_next, struct partition_t, thrd); |
| 723 | |
| 724 | if (pth_next != NULL && p_part_curr != p_part_next) { |
| 725 | /* Check if there is enough room on stack to save more context */ |
| 726 | if ((p_part_curr->ctx_ctrl.sp_limit + |
| 727 | sizeof(struct tfm_additional_context_t)) > __get_PSP()) { |
| 728 | tfm_core_panic(); |
| 729 | } |
Mate Toth-Pal | c430b99 | 2019-05-09 21:01:14 +0200 | [diff] [blame] | 730 | |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 731 | /* |
| 732 | * If required, let the platform update boundary based on its |
| 733 | * implementation. Change privilege, MPU or other configurations. |
| 734 | */ |
| 735 | if (p_part_curr->p_boundaries != p_part_next->p_boundaries) { |
| 736 | if (tfm_hal_update_boundaries(p_part_next->p_ldinf, |
| 737 | p_part_next->p_boundaries) |
| 738 | != TFM_HAL_SUCCESS) { |
| 739 | tfm_core_panic(); |
| 740 | } |
| 741 | } |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 742 | } |
David Hu | fb38d56 | 2019-09-23 15:58:34 +0800 | [diff] [blame] | 743 | |
| 744 | /* |
| 745 | * Handle pending mailbox message from NS in multi-core topology. |
| 746 | * Empty operation on single Armv8-M platform. |
| 747 | */ |
| 748 | tfm_rpc_client_call_handler(); |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 749 | |
| 750 | ret.ctx.curr = (uint32_t)pth_curr->p_context_ctrl; |
| 751 | ret.ctx.next = (uint32_t)pth_next->p_context_ctrl; |
| 752 | |
| 753 | pth_curr = pth_next; |
| 754 | |
| 755 | return ret.curr_next_ctxs; |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 756 | } |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 757 | |
Summer Qin | 02f7f07 | 2020-08-24 16:02:54 +0800 | [diff] [blame] | 758 | void update_caller_outvec_len(struct tfm_msg_body_t *msg) |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 759 | { |
| 760 | uint32_t i; |
| 761 | |
| 762 | /* |
| 763 | * FixeMe: abstract these part into dedicated functions to avoid |
| 764 | * accessing thread context in psa layer |
| 765 | */ |
| 766 | /* If it is a NS request via RPC, the owner of this message is not set */ |
| 767 | if (!is_tfm_rpc_msg(msg)) { |
| 768 | TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK); |
| 769 | } |
| 770 | |
| 771 | for (i = 0; i < PSA_MAX_IOVEC; i++) { |
| 772 | if (msg->msg.out_size[i] == 0) { |
| 773 | continue; |
| 774 | } |
| 775 | |
| 776 | TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base); |
| 777 | |
| 778 | msg->caller_outvec[i].len = msg->outvec[i].len; |
| 779 | } |
| 780 | } |
| 781 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 782 | void spm_assert_signal(void *p_pt, psa_signal_t signal) |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 783 | { |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 784 | struct partition_t *partition = (struct partition_t *)p_pt; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 785 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 786 | if (!partition) { |
| 787 | tfm_core_panic(); |
| 788 | } |
| 789 | |
Mingyang Sun | af22ffa | 2020-07-09 17:48:37 +0800 | [diff] [blame] | 790 | partition->signals_asserted |= signal; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 791 | |
Kevin Peng | 8dac610 | 2021-03-09 16:44:00 +0800 | [diff] [blame] | 792 | if (partition->signals_waiting & signal) { |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 793 | thrd_wake_up(&partition->waitobj, |
| 794 | partition->signals_asserted & partition->signals_waiting); |
Kevin Peng | 8dac610 | 2021-03-09 16:44:00 +0800 | [diff] [blame] | 795 | partition->signals_waiting &= ~signal; |
| 796 | } |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 797 | } |
| 798 | |
Kevin Peng | eca45b9 | 2021-02-09 14:46:50 +0800 | [diff] [blame] | 799 | __attribute__((naked)) |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 800 | static psa_flih_result_t tfm_flih_deprivileged_handling(void *p_pt, |
| 801 | psa_flih_func fn_flih, |
| 802 | void *p_context_ctrl) |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 803 | { |
Kevin Peng | eca45b9 | 2021-02-09 14:46:50 +0800 | [diff] [blame] | 804 | __ASM volatile("SVC %0 \n" |
| 805 | "BX LR \n" |
| 806 | : : "I" (TFM_SVC_PREPARE_DEPRIV_FLIH)); |
| 807 | } |
Kevin Peng | dc79188 | 2021-03-12 10:57:12 +0800 | [diff] [blame] | 808 | |
Kevin Peng | eca45b9 | 2021-02-09 14:46:50 +0800 | [diff] [blame] | 809 | void spm_interrupt_handler(struct partition_load_info_t *p_ldinf, |
| 810 | psa_signal_t signal, |
| 811 | uint32_t irq_line, |
| 812 | psa_flih_func flih_func) |
| 813 | { |
Kevin Peng | eca45b9 | 2021-02-09 14:46:50 +0800 | [diff] [blame] | 814 | psa_flih_result_t flih_result; |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 815 | struct partition_t *p_pt; |
Kevin Peng | dc79188 | 2021-03-12 10:57:12 +0800 | [diff] [blame] | 816 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 817 | p_pt = tfm_spm_get_partition_by_id(p_ldinf->pid); |
| 818 | if (!p_pt) { |
| 819 | tfm_core_panic(); |
| 820 | } |
Kevin Peng | eca45b9 | 2021-02-09 14:46:50 +0800 | [diff] [blame] | 821 | |
| 822 | if (flih_func == NULL) { |
| 823 | /* SLIH Model Handling */ |
Kevin Peng | d399a1f | 2021-09-08 15:33:14 +0800 | [diff] [blame^] | 824 | tfm_hal_irq_disable(irq_line); |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 825 | flih_result = PSA_FLIH_SIGNAL; |
| 826 | } else { |
| 827 | /* FLIH Model Handling */ |
| 828 | if (tfm_spm_partition_get_privileged_mode(p_ldinf->flags) == |
| 829 | TFM_PARTITION_PRIVILEGED_MODE) { |
| 830 | flih_result = flih_func(); |
| 831 | } else { |
| 832 | flih_result = tfm_flih_deprivileged_handling( |
| 833 | p_pt, flih_func, |
| 834 | pth_curr->p_context_ctrl); |
| 835 | } |
Kevin Peng | eca45b9 | 2021-02-09 14:46:50 +0800 | [diff] [blame] | 836 | } |
| 837 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 838 | if (flih_result == PSA_FLIH_SIGNAL) { |
| 839 | __disable_irq(); |
| 840 | spm_assert_signal(p_pt, signal); |
| 841 | __enable_irq(); |
Kevin Peng | eca45b9 | 2021-02-09 14:46:50 +0800 | [diff] [blame] | 842 | } |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 843 | } |
| 844 | |
Kevin Peng | 27e4227 | 2021-05-24 17:58:53 +0800 | [diff] [blame] | 845 | struct irq_load_info_t *get_irq_info_for_signal( |
| 846 | const struct partition_load_info_t *p_ldinf, |
| 847 | psa_signal_t signal) |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 848 | { |
| 849 | size_t i; |
Kevin Peng | 27e4227 | 2021-05-24 17:58:53 +0800 | [diff] [blame] | 850 | struct irq_load_info_t *irq_info; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 851 | |
Ken Liu | 24dffb2 | 2021-02-10 11:03:58 +0800 | [diff] [blame] | 852 | if (!IS_ONLY_ONE_BIT_IN_UINT32(signal)) { |
Kevin Peng | 27e4227 | 2021-05-24 17:58:53 +0800 | [diff] [blame] | 853 | return NULL; |
Kevin Peng | 410bee5 | 2021-01-13 16:27:17 +0800 | [diff] [blame] | 854 | } |
| 855 | |
Kevin Peng | 27e4227 | 2021-05-24 17:58:53 +0800 | [diff] [blame] | 856 | irq_info = (struct irq_load_info_t *)LOAD_INFO_IRQ(p_ldinf); |
| 857 | for (i = 0; i < p_ldinf->nirqs; i++) { |
| 858 | if (irq_info[i].signal == signal) { |
| 859 | return &irq_info[i]; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 860 | } |
| 861 | } |
Kevin Peng | a20b5af | 2021-01-11 11:20:52 +0800 | [diff] [blame] | 862 | |
Kevin Peng | 27e4227 | 2021-05-24 17:58:53 +0800 | [diff] [blame] | 863 | return NULL; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 864 | } |