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