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