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