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