Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 1 | /* |
Ken Liu | 5248af2 | 2019-12-29 12:47:13 +0800 | [diff] [blame] | 2 | * Copyright (c) 2018-2020, 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> |
Jamie Fox | cc31d40 | 2019-01-28 17:13:52 +0000 | [diff] [blame] | 10 | #include "psa/client.h" |
| 11 | #include "psa/service.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 12 | #include "psa/lifecycle.h" |
| 13 | #include "tfm_thread.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 14 | #include "tfm_wait.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 15 | #include "tfm_utils.h" |
| 16 | #include "tfm_internal_defines.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 17 | #include "tfm_message_queue.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 18 | #include "tfm_spm_hal.h" |
| 19 | #include "tfm_irq_list.h" |
| 20 | #include "tfm_api.h" |
| 21 | #include "tfm_secure_api.h" |
| 22 | #include "tfm_memory_utils.h" |
Mingyang Sun | c3123ec | 2020-06-11 17:43:58 +0800 | [diff] [blame] | 23 | #include "spm_api.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 24 | #include "tfm_peripherals_def.h" |
Mingyang Sun | c3123ec | 2020-06-11 17:43:58 +0800 | [diff] [blame] | 25 | #include "spm_db.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 26 | #include "tfm_core_utils.h" |
| 27 | #include "spm_psa_client_call.h" |
| 28 | #include "tfm_rpc.h" |
| 29 | #include "tfm_internal.h" |
| 30 | #include "tfm_core_trustzone.h" |
| 31 | #include "tfm_core_mem_check.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 32 | #include "tfm_list.h" |
| 33 | #include "tfm_pools.h" |
Mingyang Sun | bd7ceb5 | 2020-06-11 16:53:03 +0800 | [diff] [blame] | 34 | #include "region.h" |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 35 | #include "region_defs.h" |
Ken Liu | 1f345b0 | 2020-05-30 21:11:05 +0800 | [diff] [blame] | 36 | #include "tfm/tfm_spm_services_api.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 37 | |
Ken Liu | 1f345b0 | 2020-05-30 21:11:05 +0800 | [diff] [blame] | 38 | #include "secure_fw/partitions/tfm_service_list.inc" |
Mingyang Sun | bd7ceb5 | 2020-06-11 16:53:03 +0800 | [diff] [blame] | 39 | #include "tfm_spm_db_ipc.inc" |
Summer Qin | d99509f | 2019-08-02 17:36:58 +0800 | [diff] [blame] | 40 | |
| 41 | /* Extern service variable */ |
| 42 | extern struct tfm_spm_service_t service[]; |
Summer Qin | e578c5b | 2019-08-16 16:42:16 +0800 | [diff] [blame] | 43 | extern const struct tfm_spm_service_db_t service_db[]; |
Summer Qin | d99509f | 2019-08-02 17:36:58 +0800 | [diff] [blame] | 44 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 45 | /* Pools */ |
| 46 | TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t), |
| 47 | TFM_CONN_HANDLE_MAX_NUM); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 48 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 49 | void tfm_irq_handler(uint32_t partition_id, psa_signal_t signal, |
TTornblom | faf74f5 | 2020-03-04 17:56:27 +0100 | [diff] [blame] | 50 | IRQn_Type irq_line); |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 51 | |
| 52 | #include "tfm_secure_irq_handlers_ipc.inc" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 53 | |
Summer Qin | 373feb1 | 2020-03-27 15:35:33 +0800 | [diff] [blame] | 54 | /*********************** Connection handle conversion APIs *******************/ |
| 55 | |
| 56 | /* Set a minimal value here for feature expansion. */ |
| 57 | #define CLIENT_HANDLE_VALUE_MIN 32 |
| 58 | |
| 59 | #define CONVERSION_FACTOR_BITOFFSET 3 |
| 60 | #define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET) |
| 61 | /* Set 32 as the maximum */ |
| 62 | #define CONVERSION_FACTOR_VALUE_MAX 0x20 |
| 63 | |
| 64 | #if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX |
| 65 | #error "CONVERSION FACTOR OUT OF RANGE" |
| 66 | #endif |
| 67 | |
| 68 | static uint32_t loop_index; |
| 69 | |
| 70 | /* |
| 71 | * A handle instance psa_handle_t allocated inside SPM is actually a memory |
| 72 | * address among the handle pool. Return this handle to the client directly |
| 73 | * exposes information of secure memory address. In this case, converting the |
| 74 | * handle into another value does not represent the memory address to avoid |
| 75 | * exposing secure memory directly to clients. |
| 76 | * |
| 77 | * This function converts the handle instance into another value by scaling the |
| 78 | * handle in pool offset, the converted value is named as a user handle. |
| 79 | * |
| 80 | * The formula: |
| 81 | * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE + |
| 82 | * CLIENT_HANDLE_VALUE_MIN + loop_index |
| 83 | * where: |
| 84 | * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not |
| 85 | * exceed CONVERSION_FACTOR_VALUE_MAX. |
| 86 | * |
| 87 | * handle_instance in RANGE[POOL_START, POOL_END] |
| 88 | * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF] |
| 89 | * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1] |
| 90 | * |
| 91 | * note: |
| 92 | * loop_index is used to promise same handle instance is converted into |
| 93 | * different user handles in short time. |
| 94 | */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 95 | 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] | 96 | { |
| 97 | psa_handle_t user_handle; |
| 98 | |
| 99 | loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE; |
| 100 | user_handle = (psa_handle_t)((((uintptr_t)handle_instance - |
| 101 | (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) + |
| 102 | CLIENT_HANDLE_VALUE_MIN + loop_index); |
| 103 | |
| 104 | return user_handle; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * This function converts a user handle into a corresponded handle instance. |
| 109 | * The converted value is validated before returning, an invalid handle instance |
| 110 | * is returned as NULL. |
| 111 | * |
| 112 | * The formula: |
| 113 | * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) / |
| 114 | * CONVERSION_FACTOR_VALUE) + POOL_START |
| 115 | * where: |
| 116 | * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not |
| 117 | * exceed CONVERSION_FACTOR_VALUE_MAX. |
| 118 | * |
| 119 | * handle_instance in RANGE[POOL_START, POOL_END] |
| 120 | * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF] |
| 121 | * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1] |
| 122 | */ |
| 123 | struct tfm_conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle) |
| 124 | { |
| 125 | struct tfm_conn_handle_t *handle_instance; |
| 126 | |
| 127 | if (user_handle == PSA_NULL_HANDLE) { |
| 128 | return NULL; |
| 129 | } |
| 130 | |
| 131 | handle_instance = (struct tfm_conn_handle_t *)((((uintptr_t)user_handle - |
| 132 | CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) + |
| 133 | (uintptr_t)conn_handle_pool); |
| 134 | |
| 135 | return handle_instance; |
| 136 | } |
| 137 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 138 | /* Service handle management functions */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 139 | struct tfm_conn_handle_t *tfm_spm_create_conn_handle( |
| 140 | struct tfm_spm_service_t *service, |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 141 | int32_t client_id) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 142 | { |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 143 | struct tfm_conn_handle_t *p_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 144 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 145 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 146 | |
| 147 | /* Get buffer for handle list structure from handle pool */ |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 148 | p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool); |
| 149 | if (!p_handle) { |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 150 | return NULL; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 151 | } |
| 152 | |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 153 | p_handle->service = service; |
Shawn Shan | cc39fcb | 2019-11-13 15:38:16 +0800 | [diff] [blame] | 154 | p_handle->status = TFM_HANDLE_STATUS_IDLE; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 155 | p_handle->client_id = client_id; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 156 | |
| 157 | /* Add handle node to list for next psa functions */ |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 158 | tfm_list_add_tail(&service->handle_list, &p_handle->list); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 159 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 160 | return p_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 161 | } |
| 162 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 163 | int32_t tfm_spm_validate_conn_handle( |
| 164 | const struct tfm_conn_handle_t *conn_handle, |
| 165 | int32_t client_id) |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 166 | { |
| 167 | /* Check the handle address is validated */ |
| 168 | if (is_valid_chunk_data_in_pool(conn_handle_pool, |
| 169 | (uint8_t *)conn_handle) != true) { |
| 170 | return IPC_ERROR_GENERIC; |
| 171 | } |
| 172 | |
| 173 | /* Check the handle caller is correct */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 174 | if (conn_handle->client_id != client_id) { |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 175 | return IPC_ERROR_GENERIC; |
| 176 | } |
| 177 | |
| 178 | return IPC_SUCCESS; |
| 179 | } |
| 180 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 181 | /** |
| 182 | * \brief Free connection handle which not used anymore. |
| 183 | * |
| 184 | * \param[in] service Target service context pointer |
| 185 | * \param[in] conn_handle Connection handle created by |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 186 | * tfm_spm_create_conn_handle() |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 187 | * |
| 188 | * \retval IPC_SUCCESS Success |
| 189 | * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input |
| 190 | * \retval "Does not return" Panic for not find service by handle |
| 191 | */ |
| 192 | static int32_t tfm_spm_free_conn_handle(struct tfm_spm_service_t *service, |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 193 | struct tfm_conn_handle_t *conn_handle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 194 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 195 | TFM_CORE_ASSERT(service); |
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 | |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 198 | /* Clear magic as the handler is not used anymore */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 199 | conn_handle->internal_msg.magic = 0; |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 200 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 201 | /* Remove node from handle list */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 202 | tfm_list_del_node(&conn_handle->list); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 203 | |
| 204 | /* Back handle buffer to pool */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 205 | tfm_pool_free(conn_handle); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 206 | return IPC_SUCCESS; |
| 207 | } |
| 208 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 209 | /** |
| 210 | * \brief Set reverse handle value for connection. |
| 211 | * |
| 212 | * \param[in] service Target service context pointer |
| 213 | * \param[in] conn_handle Connection handle created by |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 214 | * tfm_spm_create_conn_handle() |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 215 | * \param[in] rhandle rhandle need to save |
| 216 | * |
| 217 | * \retval IPC_SUCCESS Success |
| 218 | * \retval IPC_ERROR_BAD_PARAMETERS Bad parameters input |
| 219 | * \retval "Does not return" Panic for not find handle node |
| 220 | */ |
| 221 | static int32_t tfm_spm_set_rhandle(struct tfm_spm_service_t *service, |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 222 | struct tfm_conn_handle_t *conn_handle, |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 223 | void *rhandle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 224 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 225 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 226 | /* Set reverse handle value only be allowed for a connected handle */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 227 | TFM_CORE_ASSERT(conn_handle != NULL); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 228 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 229 | conn_handle->rhandle = rhandle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 230 | return IPC_SUCCESS; |
| 231 | } |
| 232 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 233 | /** |
| 234 | * \brief Get reverse handle value from connection hanlde. |
| 235 | * |
| 236 | * \param[in] service Target service context pointer |
| 237 | * \param[in] conn_handle Connection handle created by |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 238 | * tfm_spm_create_conn_handle() |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 239 | * |
| 240 | * \retval void * Success |
| 241 | * \retval "Does not return" Panic for those: |
| 242 | * service pointer are NULL |
| 243 | * hanlde is \ref PSA_NULL_HANDLE |
| 244 | * handle node does not be found |
| 245 | */ |
| 246 | static void *tfm_spm_get_rhandle(struct tfm_spm_service_t *service, |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 247 | struct tfm_conn_handle_t *conn_handle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 248 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 249 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 250 | /* Get reverse handle value only be allowed for a connected handle */ |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 251 | TFM_CORE_ASSERT(conn_handle != NULL); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 252 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 253 | return conn_handle->rhandle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /* Partition management functions */ |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 257 | |
| 258 | /** |
| 259 | * \brief Get the service context by signal. |
| 260 | * |
| 261 | * \param[in] partition Partition context pointer |
| 262 | * \ref spm_partition_desc_t structures |
| 263 | * \param[in] signal Signal associated with inputs to the Secure |
| 264 | * Partition, \ref psa_signal_t |
| 265 | * |
| 266 | * \retval NULL Failed |
| 267 | * \retval "Not NULL" Target service context pointer, |
| 268 | * \ref tfm_spm_service_t structures |
| 269 | */ |
| 270 | static struct tfm_spm_service_t * |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 271 | tfm_spm_get_service_by_signal(struct spm_partition_desc_t *partition, |
| 272 | psa_signal_t signal) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 273 | { |
| 274 | struct tfm_list_node_t *node, *head; |
| 275 | struct tfm_spm_service_t *service; |
| 276 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 277 | TFM_CORE_ASSERT(partition); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 278 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 279 | if (tfm_list_is_empty(&partition->runtime_data.service_list)) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 280 | tfm_core_panic(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 281 | } |
| 282 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 283 | head = &partition->runtime_data.service_list; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 284 | TFM_LIST_FOR_EACH(node, head) { |
| 285 | service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t, list); |
Summer Qin | e578c5b | 2019-08-16 16:42:16 +0800 | [diff] [blame] | 286 | if (service->service_db->signal == signal) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 287 | return service; |
| 288 | } |
| 289 | } |
| 290 | return NULL; |
| 291 | } |
| 292 | |
| 293 | struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid) |
| 294 | { |
Summer Qin | 2fca1c8 | 2020-03-20 14:37:55 +0800 | [diff] [blame] | 295 | uint32_t i, num; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 296 | |
Summer Qin | 2fca1c8 | 2020-03-20 14:37:55 +0800 | [diff] [blame] | 297 | num = sizeof(service) / sizeof(struct tfm_spm_service_t); |
| 298 | for (i = 0; i < num; i++) { |
| 299 | if (service[i].service_db->sid == sid) { |
| 300 | return &service[i]; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 301 | } |
| 302 | } |
Summer Qin | 2fca1c8 | 2020-03-20 14:37:55 +0800 | [diff] [blame] | 303 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 304 | return NULL; |
| 305 | } |
| 306 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 307 | /** |
| 308 | * \brief Get the partition context by partition ID. |
| 309 | * |
| 310 | * \param[in] partition_id Partition identity |
| 311 | * |
| 312 | * \retval NULL Failed |
| 313 | * \retval "Not NULL" Target partition context pointer, |
| 314 | * \ref spm_partition_desc_t structures |
| 315 | */ |
| 316 | static struct spm_partition_desc_t * |
| 317 | tfm_spm_get_partition_by_id(int32_t partition_id) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 318 | { |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 319 | uint32_t idx = get_partition_idx(partition_id); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 320 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 321 | if (idx != SPM_INVALID_PARTITION_IDX) { |
| 322 | return &(g_spm_partition_db.partitions[idx]); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 323 | } |
| 324 | return NULL; |
| 325 | } |
| 326 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 327 | struct spm_partition_desc_t *tfm_spm_get_running_partition(void) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 328 | { |
| 329 | uint32_t spid; |
| 330 | |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 331 | spid = tfm_spm_partition_get_running_partition_id(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 332 | |
| 333 | return tfm_spm_get_partition_by_id(spid); |
| 334 | } |
| 335 | |
| 336 | 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] | 337 | uint32_t version) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 338 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 339 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 340 | |
Jaykumar Pitambarbhai Patel | 3a98602 | 2019-10-08 17:37:15 +0530 | [diff] [blame] | 341 | switch (service->service_db->version_policy) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 342 | case TFM_VERSION_POLICY_RELAXED: |
Jaykumar Pitambarbhai Patel | 3a98602 | 2019-10-08 17:37:15 +0530 | [diff] [blame] | 343 | if (version > service->service_db->version) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 344 | return IPC_ERROR_VERSION; |
| 345 | } |
| 346 | break; |
| 347 | case TFM_VERSION_POLICY_STRICT: |
Jaykumar Pitambarbhai Patel | 3a98602 | 2019-10-08 17:37:15 +0530 | [diff] [blame] | 348 | if (version != service->service_db->version) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 349 | return IPC_ERROR_VERSION; |
| 350 | } |
| 351 | break; |
| 352 | default: |
| 353 | return IPC_ERROR_VERSION; |
| 354 | } |
| 355 | return IPC_SUCCESS; |
| 356 | } |
| 357 | |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 358 | int32_t tfm_spm_check_authorization(uint32_t sid, |
| 359 | struct tfm_spm_service_t *service, |
Summer Qin | 618e8c3 | 2019-12-09 10:47:20 +0800 | [diff] [blame] | 360 | bool ns_caller) |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 361 | { |
| 362 | struct spm_partition_desc_t *partition = NULL; |
| 363 | int32_t i; |
| 364 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 365 | TFM_CORE_ASSERT(service); |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 366 | |
| 367 | if (ns_caller) { |
| 368 | if (!service->service_db->non_secure_client) { |
| 369 | return IPC_ERROR_GENERIC; |
| 370 | } |
| 371 | } else { |
| 372 | partition = tfm_spm_get_running_partition(); |
| 373 | if (!partition) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 374 | tfm_core_panic(); |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | for (i = 0; i < partition->static_data->dependencies_num; i++) { |
| 378 | if (partition->static_data->p_dependencies[i] == sid) { |
| 379 | break; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | if (i == partition->static_data->dependencies_num) { |
| 384 | return IPC_ERROR_GENERIC; |
| 385 | } |
| 386 | } |
| 387 | return IPC_SUCCESS; |
| 388 | } |
| 389 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 390 | /* Message functions */ |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 391 | |
| 392 | /** |
| 393 | * \brief Get message context by message handle. |
| 394 | * |
| 395 | * \param[in] msg_handle Message handle which is a reference generated |
| 396 | * by the SPM to a specific message. |
| 397 | * |
| 398 | * \return The message body context pointer |
| 399 | * \ref tfm_msg_body_t structures |
| 400 | */ |
| 401 | static struct tfm_msg_body_t * |
| 402 | tfm_spm_get_msg_from_handle(psa_handle_t msg_handle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 403 | { |
| 404 | /* |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 405 | * The message handler passed by the caller is considered invalid in the |
| 406 | * following cases: |
| 407 | * 1. Not a valid message handle. (The address of a message is not the |
| 408 | * address of a possible handle from the pool |
| 409 | * 2. Handle not belongs to the caller partition (The handle is either |
| 410 | * unused, or owned by anither partition) |
| 411 | * Check the conditions above |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 412 | */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 413 | struct tfm_msg_body_t *p_msg; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 414 | uint32_t partition_id; |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 415 | struct tfm_conn_handle_t *p_conn_handle = |
| 416 | tfm_spm_to_handle_instance(msg_handle); |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 417 | |
| 418 | if (is_valid_chunk_data_in_pool( |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 419 | conn_handle_pool, (uint8_t *)p_conn_handle) != 1) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 420 | return NULL; |
| 421 | } |
| 422 | |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 423 | p_msg = &p_conn_handle->internal_msg; |
| 424 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 425 | /* |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 426 | * Check that the magic number is correct. This proves that the message |
| 427 | * structure contains an active message. |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 428 | */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 429 | if (p_msg->magic != TFM_MSG_MAGIC) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 430 | return NULL; |
| 431 | } |
| 432 | |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 433 | /* Check that the running partition owns the message */ |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 434 | partition_id = tfm_spm_partition_get_running_partition_id(); |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 435 | if (partition_id != p_msg->service->partition->static_data->partition_id) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 436 | return NULL; |
| 437 | } |
| 438 | |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 439 | return p_msg; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 440 | } |
| 441 | |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 442 | struct tfm_msg_body_t * |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 443 | tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 444 | { |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 445 | TFM_CORE_ASSERT(conn_handle != NULL); |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 446 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 447 | return &(conn_handle->internal_msg); |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | void tfm_spm_fill_msg(struct tfm_msg_body_t *msg, |
| 451 | struct tfm_spm_service_t *service, |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 452 | psa_handle_t handle, |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 453 | int32_t type, int32_t client_id, |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 454 | psa_invec *invec, size_t in_len, |
| 455 | psa_outvec *outvec, size_t out_len, |
| 456 | psa_outvec *caller_outvec) |
| 457 | { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 458 | uint32_t i; |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 459 | struct tfm_conn_handle_t *conn_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 460 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 461 | TFM_CORE_ASSERT(msg); |
| 462 | TFM_CORE_ASSERT(service); |
| 463 | TFM_CORE_ASSERT(!(invec == NULL && in_len != 0)); |
| 464 | TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0)); |
| 465 | TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC); |
| 466 | TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC); |
| 467 | TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 468 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 469 | /* Clear message buffer before using it */ |
Mingyang Sun | 94b1b41 | 2019-09-20 15:11:14 +0800 | [diff] [blame] | 470 | tfm_core_util_memset(msg, 0, sizeof(struct tfm_msg_body_t)); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 471 | |
Ken Liu | 35f8939 | 2019-03-14 14:51:05 +0800 | [diff] [blame] | 472 | tfm_event_init(&msg->ack_evnt); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 473 | msg->magic = TFM_MSG_MAGIC; |
| 474 | msg->service = service; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 475 | msg->caller_outvec = caller_outvec; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 476 | msg->msg.client_id = client_id; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 477 | |
| 478 | /* Copy contents */ |
| 479 | msg->msg.type = type; |
| 480 | |
| 481 | for (i = 0; i < in_len; i++) { |
| 482 | msg->msg.in_size[i] = invec[i].len; |
| 483 | msg->invec[i].base = invec[i].base; |
| 484 | } |
| 485 | |
| 486 | for (i = 0; i < out_len; i++) { |
| 487 | msg->msg.out_size[i] = outvec[i].len; |
| 488 | msg->outvec[i].base = outvec[i].base; |
| 489 | /* Out len is used to record the writed number, set 0 here again */ |
| 490 | msg->outvec[i].len = 0; |
| 491 | } |
| 492 | |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 493 | /* Use the user connect handle as the message handle */ |
| 494 | msg->msg.handle = handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 495 | |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 496 | conn_handle = tfm_spm_to_handle_instance(handle); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 497 | /* For connected handle, set rhandle to every message */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 498 | if (conn_handle) { |
| 499 | msg->msg.rhandle = tfm_spm_get_rhandle(service, conn_handle); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 500 | } |
David Hu | 46603dd | 2019-12-11 18:05:16 +0800 | [diff] [blame] | 501 | |
| 502 | /* Set the private data of NSPE client caller in multi-core topology */ |
| 503 | if (TFM_CLIENT_ID_IS_NS(client_id)) { |
| 504 | tfm_rpc_set_caller_data(msg, client_id); |
| 505 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | int32_t tfm_spm_send_event(struct tfm_spm_service_t *service, |
| 509 | struct tfm_msg_body_t *msg) |
| 510 | { |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 511 | struct spm_partition_runtime_data_t *p_runtime_data = |
| 512 | &service->partition->runtime_data; |
| 513 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 514 | TFM_CORE_ASSERT(service); |
| 515 | TFM_CORE_ASSERT(msg); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 516 | |
| 517 | /* Enqueue message to service message queue */ |
| 518 | if (tfm_msg_enqueue(&service->msg_queue, msg) != IPC_SUCCESS) { |
| 519 | return IPC_ERROR_GENERIC; |
| 520 | } |
| 521 | |
| 522 | /* Messages put. Update signals */ |
Summer Qin | e578c5b | 2019-08-16 16:42:16 +0800 | [diff] [blame] | 523 | p_runtime_data->signals |= service->service_db->signal; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 524 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 525 | tfm_event_wake(&p_runtime_data->signal_evnt, (p_runtime_data->signals & |
| 526 | p_runtime_data->signal_mask)); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 527 | |
David Hu | fb38d56 | 2019-09-23 15:58:34 +0800 | [diff] [blame] | 528 | /* |
| 529 | * If it is a NS request via RPC, it is unnecessary to block current |
| 530 | * thread. |
| 531 | */ |
| 532 | if (!is_tfm_rpc_msg(msg)) { |
| 533 | tfm_event_wait(&msg->ack_evnt); |
| 534 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 535 | |
| 536 | return IPC_SUCCESS; |
| 537 | } |
| 538 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 539 | /** |
| 540 | * \brief Get bottom of stack region for a partition |
| 541 | * |
| 542 | * \param[in] partition_idx Partition index |
| 543 | * |
| 544 | * \return Stack region bottom value |
| 545 | * |
| 546 | * \note This function doesn't check if partition_idx is valid. |
| 547 | */ |
| 548 | static uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx) |
Edison Ai | 7aff9e8 | 2019-07-11 14:56:46 +0800 | [diff] [blame] | 549 | { |
| 550 | return g_spm_partition_db.partitions[partition_idx]. |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 551 | memory_data->stack_bottom; |
Edison Ai | 7aff9e8 | 2019-07-11 14:56:46 +0800 | [diff] [blame] | 552 | } |
| 553 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 554 | /** |
| 555 | * \brief Get top of stack region for a partition |
| 556 | * |
| 557 | * \param[in] partition_idx Partition index |
| 558 | * |
| 559 | * \return Stack region top value |
| 560 | * |
| 561 | * \note This function doesn't check if partition_idx is valid. |
| 562 | */ |
| 563 | static uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx) |
Edison Ai | 7aff9e8 | 2019-07-11 14:56:46 +0800 | [diff] [blame] | 564 | { |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 565 | return g_spm_partition_db.partitions[partition_idx].memory_data->stack_top; |
Edison Ai | 7aff9e8 | 2019-07-11 14:56:46 +0800 | [diff] [blame] | 566 | } |
| 567 | |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 568 | uint32_t tfm_spm_partition_get_running_partition_id(void) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 569 | { |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 570 | struct tfm_core_thread_t *pth = tfm_core_thrd_get_curr_thread(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 571 | struct spm_partition_desc_t *partition; |
Summer Qin | b5da9cc | 2019-08-26 15:19:45 +0800 | [diff] [blame] | 572 | struct spm_partition_runtime_data_t *r_data; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 573 | |
Summer Qin | b5da9cc | 2019-08-26 15:19:45 +0800 | [diff] [blame] | 574 | r_data = TFM_GET_CONTAINER_PTR(pth, struct spm_partition_runtime_data_t, |
| 575 | sp_thrd); |
| 576 | partition = TFM_GET_CONTAINER_PTR(r_data, struct spm_partition_desc_t, |
| 577 | runtime_data); |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 578 | return partition->static_data->partition_id; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 579 | } |
| 580 | |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 581 | static struct tfm_core_thread_t * |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 582 | tfm_spm_partition_get_thread_info(uint32_t partition_idx) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 583 | { |
Summer Qin | b5da9cc | 2019-08-26 15:19:45 +0800 | [diff] [blame] | 584 | return &g_spm_partition_db.partitions[partition_idx].runtime_data.sp_thrd; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 585 | } |
| 586 | |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 587 | static tfm_core_thrd_entry_t |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 588 | tfm_spm_partition_get_init_func(uint32_t partition_idx) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 589 | { |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 590 | return (tfm_core_thrd_entry_t)(g_spm_partition_db.partitions[partition_idx]. |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 591 | static_data->partition_init); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 592 | } |
| 593 | |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 594 | static uint32_t tfm_spm_partition_get_priority(uint32_t partition_idx) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 595 | { |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 596 | return g_spm_partition_db.partitions[partition_idx].static_data-> |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 597 | partition_priority; |
| 598 | } |
| 599 | |
Summer Qin | 43c185d | 2019-10-10 15:44:42 +0800 | [diff] [blame] | 600 | 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] | 601 | enum tfm_memory_access_e access, |
| 602 | uint32_t privileged) |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 603 | { |
Hugues de Valon | 9957856 | 2019-06-18 16:08:51 +0100 | [diff] [blame] | 604 | enum tfm_status_e err; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 605 | |
| 606 | /* If len is zero, this indicates an empty buffer and base is ignored */ |
| 607 | if (len == 0) { |
| 608 | return IPC_SUCCESS; |
| 609 | } |
| 610 | |
| 611 | if (!buffer) { |
| 612 | return IPC_ERROR_BAD_PARAMETERS; |
| 613 | } |
| 614 | |
| 615 | if ((uintptr_t)buffer > (UINTPTR_MAX - len)) { |
| 616 | return IPC_ERROR_MEMORY_CHECK; |
| 617 | } |
| 618 | |
Summer Qin | 424d4db | 2019-03-25 14:09:51 +0800 | [diff] [blame] | 619 | if (access == TFM_MEMORY_ACCESS_RW) { |
Summer Qin | eb537e5 | 2019-03-29 09:57:10 +0800 | [diff] [blame] | 620 | err = tfm_core_has_write_access_to_region(buffer, len, ns_caller, |
| 621 | privileged); |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 622 | } else { |
Summer Qin | eb537e5 | 2019-03-29 09:57:10 +0800 | [diff] [blame] | 623 | err = tfm_core_has_read_access_to_region(buffer, len, ns_caller, |
| 624 | privileged); |
Summer Qin | 424d4db | 2019-03-25 14:09:51 +0800 | [diff] [blame] | 625 | } |
Summer Qin | 0fc3f59 | 2019-04-11 16:00:10 +0800 | [diff] [blame] | 626 | if (err == TFM_SUCCESS) { |
Summer Qin | 424d4db | 2019-03-25 14:09:51 +0800 | [diff] [blame] | 627 | return IPC_SUCCESS; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | return IPC_ERROR_MEMORY_CHECK; |
| 631 | } |
| 632 | |
Ken Liu | ce2692d | 2020-02-11 12:39:36 +0800 | [diff] [blame] | 633 | uint32_t tfm_spm_init(void) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 634 | { |
Shawn Shan | 9b0e0c7 | 2019-10-22 13:43:07 +0800 | [diff] [blame] | 635 | uint32_t i, j, num; |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 636 | struct spm_partition_desc_t *partition; |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 637 | struct tfm_core_thread_t *pth, *p_ns_entry_thread = NULL; |
Mate Toth-Pal | 8ac98a7 | 2019-11-21 17:30:10 +0100 | [diff] [blame] | 638 | const struct tfm_spm_partition_platform_data_t **platform_data_p; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 639 | |
| 640 | tfm_pool_init(conn_handle_pool, |
| 641 | POOL_BUFFER_SIZE(conn_handle_pool), |
| 642 | sizeof(struct tfm_conn_handle_t), |
| 643 | TFM_CONN_HANDLE_MAX_NUM); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 644 | |
| 645 | /* Init partition first for it will be used when init service */ |
Mate Toth-Pal | 3ad2e3e | 2019-07-11 21:43:37 +0200 | [diff] [blame] | 646 | for (i = 0; i < g_spm_partition_db.partition_count; i++) { |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 647 | partition = &g_spm_partition_db.partitions[i]; |
Edison Ai | f050170 | 2019-10-11 14:36:42 +0800 | [diff] [blame] | 648 | |
| 649 | /* Check if the PSA framework version matches. */ |
| 650 | if (partition->static_data->psa_framework_version != |
| 651 | PSA_FRAMEWORK_VERSION) { |
| 652 | ERROR_MSG("Warning: PSA Framework Verison is not matched!"); |
| 653 | continue; |
| 654 | } |
| 655 | |
Mate Toth-Pal | 8ac98a7 | 2019-11-21 17:30:10 +0100 | [diff] [blame] | 656 | platform_data_p = partition->platform_data_list; |
| 657 | if (platform_data_p != NULL) { |
| 658 | while ((*platform_data_p) != NULL) { |
Edison Ai | 6be3df1 | 2020-02-14 22:14:33 +0800 | [diff] [blame] | 659 | if (tfm_spm_hal_configure_default_isolation(i, |
| 660 | *platform_data_p) != TFM_PLAT_ERR_SUCCESS) { |
| 661 | tfm_core_panic(); |
| 662 | } |
Mate Toth-Pal | 8ac98a7 | 2019-11-21 17:30:10 +0100 | [diff] [blame] | 663 | ++platform_data_p; |
| 664 | } |
| 665 | } |
| 666 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 667 | if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) { |
| 668 | continue; |
| 669 | } |
Ken Liu | 35f8939 | 2019-03-14 14:51:05 +0800 | [diff] [blame] | 670 | |
Shawn Shan | c7dda0e | 2019-12-23 14:45:09 +0800 | [diff] [blame] | 671 | /* Add PSA_DOORBELL signal to assigned_signals */ |
| 672 | partition->runtime_data.assigned_signals |= PSA_DOORBELL; |
| 673 | |
Shawn Shan | 9b0e0c7 | 2019-10-22 13:43:07 +0800 | [diff] [blame] | 674 | /* TODO: This can be optimized by generating the assigned signal |
| 675 | * in code generation time. |
| 676 | */ |
| 677 | for (j = 0; j < tfm_core_irq_signals_count; ++j) { |
| 678 | if (tfm_core_irq_signals[j].partition_id == |
| 679 | partition->static_data->partition_id) { |
| 680 | partition->runtime_data.assigned_signals |= |
| 681 | tfm_core_irq_signals[j].signal_value; |
| 682 | } |
| 683 | } |
| 684 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 685 | tfm_event_init(&partition->runtime_data.signal_evnt); |
| 686 | tfm_list_init(&partition->runtime_data.service_list); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 687 | |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 688 | pth = tfm_spm_partition_get_thread_info(i); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 689 | if (!pth) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 690 | tfm_core_panic(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 691 | } |
| 692 | |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 693 | tfm_core_thrd_init(pth, |
| 694 | tfm_spm_partition_get_init_func(i), |
| 695 | NULL, |
| 696 | (uintptr_t)tfm_spm_partition_get_stack_top(i), |
| 697 | (uintptr_t)tfm_spm_partition_get_stack_bottom(i)); |
Edison Ai | 788bae2 | 2019-02-18 17:38:59 +0800 | [diff] [blame] | 698 | |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 699 | pth->prior = tfm_spm_partition_get_priority(i); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 700 | |
Ken Liu | 490281d | 2019-12-30 15:55:26 +0800 | [diff] [blame] | 701 | if (partition->static_data->partition_id == TFM_SP_NON_SECURE_ID) { |
| 702 | p_ns_entry_thread = pth; |
Ken Liu | 5248af2 | 2019-12-29 12:47:13 +0800 | [diff] [blame] | 703 | pth->param = (void *)tfm_spm_hal_get_ns_entry_point(); |
Ken Liu | 490281d | 2019-12-30 15:55:26 +0800 | [diff] [blame] | 704 | } |
| 705 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 706 | /* Kick off */ |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 707 | if (tfm_core_thrd_start(pth) != THRD_SUCCESS) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 708 | tfm_core_panic(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
| 712 | /* Init Service */ |
Summer Qin | d99509f | 2019-08-02 17:36:58 +0800 | [diff] [blame] | 713 | num = sizeof(service) / sizeof(struct tfm_spm_service_t); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 714 | for (i = 0; i < num; i++) { |
Summer Qin | e578c5b | 2019-08-16 16:42:16 +0800 | [diff] [blame] | 715 | service[i].service_db = &service_db[i]; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 716 | partition = |
Summer Qin | e578c5b | 2019-08-16 16:42:16 +0800 | [diff] [blame] | 717 | tfm_spm_get_partition_by_id(service[i].service_db->partition_id); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 718 | if (!partition) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 719 | tfm_core_panic(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 720 | } |
Summer Qin | d99509f | 2019-08-02 17:36:58 +0800 | [diff] [blame] | 721 | service[i].partition = partition; |
Jaykumar Pitambarbhai Patel | 0c7a038 | 2020-01-09 15:25:58 +0530 | [diff] [blame] | 722 | partition->runtime_data.assigned_signals |= service[i].service_db->signal; |
Shawn Shan | 9b0e0c7 | 2019-10-22 13:43:07 +0800 | [diff] [blame] | 723 | |
Summer Qin | d99509f | 2019-08-02 17:36:58 +0800 | [diff] [blame] | 724 | tfm_list_init(&service[i].handle_list); |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 725 | tfm_list_add_tail(&partition->runtime_data.service_list, |
Summer Qin | d99509f | 2019-08-02 17:36:58 +0800 | [diff] [blame] | 726 | &service[i].list); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 727 | } |
| 728 | |
Ken Liu | 483f5da | 2019-04-24 10:45:21 +0800 | [diff] [blame] | 729 | /* |
| 730 | * All threads initialized, start the scheduler. |
| 731 | * |
| 732 | * NOTE: |
Ken Liu | 490281d | 2019-12-30 15:55:26 +0800 | [diff] [blame] | 733 | * It is worthy to give the thread object to scheduler if the background |
| 734 | * context belongs to one of the threads. Here the background thread is the |
| 735 | * initialization thread who calls SPM SVC, which re-uses the non-secure |
| 736 | * entry thread's stack. After SPM initialization is done, this stack is |
| 737 | * cleaned up and the background context is never going to return. Tell |
| 738 | * the scheduler that the current thread is non-secure entry thread. |
Ken Liu | 483f5da | 2019-04-24 10:45:21 +0800 | [diff] [blame] | 739 | */ |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 740 | tfm_core_thrd_start_scheduler(p_ns_entry_thread); |
Ken Liu | ce2692d | 2020-02-11 12:39:36 +0800 | [diff] [blame] | 741 | |
Summer Qin | d2ad7e7 | 2020-01-06 18:16:35 +0800 | [diff] [blame] | 742 | return p_ns_entry_thread->arch_ctx.lr; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 743 | } |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 744 | |
Summer Qin | d2ad7e7 | 2020-01-06 18:16:35 +0800 | [diff] [blame] | 745 | void tfm_pendsv_do_schedule(struct tfm_arch_ctx_t *p_actx) |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 746 | { |
| 747 | #if TFM_LVL == 2 |
| 748 | struct spm_partition_desc_t *p_next_partition; |
Summer Qin | b5da9cc | 2019-08-26 15:19:45 +0800 | [diff] [blame] | 749 | struct spm_partition_runtime_data_t *r_data; |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 750 | uint32_t is_privileged; |
| 751 | #endif |
Summer Qin | 66f1e03 | 2020-01-06 15:40:03 +0800 | [diff] [blame] | 752 | struct tfm_core_thread_t *pth_next = tfm_core_thrd_get_next_thread(); |
| 753 | 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] | 754 | |
Mate Toth-Pal | 32b2ccd | 2019-04-26 10:00:16 +0200 | [diff] [blame] | 755 | if (pth_next != NULL && pth_curr != pth_next) { |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 756 | #if TFM_LVL == 2 |
Summer Qin | b5da9cc | 2019-08-26 15:19:45 +0800 | [diff] [blame] | 757 | r_data = TFM_GET_CONTAINER_PTR(pth_next, |
| 758 | struct spm_partition_runtime_data_t, |
| 759 | sp_thrd); |
| 760 | p_next_partition = TFM_GET_CONTAINER_PTR(r_data, |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 761 | struct spm_partition_desc_t, |
Summer Qin | b5da9cc | 2019-08-26 15:19:45 +0800 | [diff] [blame] | 762 | runtime_data); |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 763 | |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 764 | if (p_next_partition->static_data->partition_flags & |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 765 | SPM_PART_FLAG_PSA_ROT) { |
| 766 | is_privileged = TFM_PARTITION_PRIVILEGED_MODE; |
| 767 | } else { |
| 768 | is_privileged = TFM_PARTITION_UNPRIVILEGED_MODE; |
| 769 | } |
| 770 | |
| 771 | tfm_spm_partition_change_privilege(is_privileged); |
| 772 | #endif |
Mate Toth-Pal | c430b99 | 2019-05-09 21:01:14 +0200 | [diff] [blame] | 773 | |
Summer Qin | d2ad7e7 | 2020-01-06 18:16:35 +0800 | [diff] [blame] | 774 | tfm_core_thrd_switch_context(p_actx, pth_curr, pth_next); |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 775 | } |
David Hu | fb38d56 | 2019-09-23 15:58:34 +0800 | [diff] [blame] | 776 | |
| 777 | /* |
| 778 | * Handle pending mailbox message from NS in multi-core topology. |
| 779 | * Empty operation on single Armv8-M platform. |
| 780 | */ |
| 781 | tfm_rpc_client_call_handler(); |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 782 | } |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 783 | |
| 784 | /*********************** SPM functions for PSA Client APIs *******************/ |
| 785 | |
| 786 | uint32_t tfm_spm_psa_framework_version(void) |
| 787 | { |
| 788 | return tfm_spm_client_psa_framework_version(); |
| 789 | } |
| 790 | |
| 791 | uint32_t tfm_spm_psa_version(uint32_t *args, bool ns_caller) |
| 792 | { |
| 793 | uint32_t sid; |
| 794 | |
| 795 | TFM_CORE_ASSERT(args != NULL); |
| 796 | sid = (uint32_t)args[0]; |
| 797 | |
| 798 | return tfm_spm_client_psa_version(sid, ns_caller); |
| 799 | } |
| 800 | |
| 801 | psa_status_t tfm_spm_psa_connect(uint32_t *args, bool ns_caller) |
| 802 | { |
| 803 | uint32_t sid; |
| 804 | uint32_t version; |
| 805 | |
| 806 | TFM_CORE_ASSERT(args != NULL); |
| 807 | sid = (uint32_t)args[0]; |
| 808 | version = (uint32_t)args[1]; |
| 809 | |
| 810 | return tfm_spm_client_psa_connect(sid, version, ns_caller); |
| 811 | } |
| 812 | |
| 813 | psa_status_t tfm_spm_psa_call(uint32_t *args, bool ns_caller, uint32_t lr) |
| 814 | { |
| 815 | psa_handle_t handle; |
| 816 | psa_invec *inptr; |
| 817 | psa_outvec *outptr; |
| 818 | size_t in_num, out_num; |
| 819 | struct spm_partition_desc_t *partition = NULL; |
| 820 | uint32_t privileged; |
| 821 | int32_t type; |
| 822 | struct tfm_control_parameter_t ctrl_param; |
| 823 | |
| 824 | TFM_CORE_ASSERT(args != NULL); |
| 825 | handle = (psa_handle_t)args[0]; |
| 826 | |
| 827 | partition = tfm_spm_get_running_partition(); |
| 828 | if (!partition) { |
| 829 | tfm_core_panic(); |
| 830 | } |
| 831 | privileged = tfm_spm_partition_get_privileged_mode( |
| 832 | partition->static_data->partition_flags); |
| 833 | |
| 834 | /* |
| 835 | * Read parameters from the arguments. It is a fatal error if the |
| 836 | * memory reference for buffer is invalid or not readable. |
| 837 | */ |
| 838 | if (tfm_memory_check((const void *)args[1], |
| 839 | sizeof(struct tfm_control_parameter_t), ns_caller, |
| 840 | TFM_MEMORY_ACCESS_RW, privileged) != IPC_SUCCESS) { |
| 841 | tfm_core_panic(); |
| 842 | } |
| 843 | |
| 844 | tfm_core_util_memcpy(&ctrl_param, |
| 845 | (const void *)args[1], |
| 846 | sizeof(ctrl_param)); |
| 847 | |
| 848 | type = ctrl_param.type; |
| 849 | in_num = ctrl_param.in_len; |
| 850 | out_num = ctrl_param.out_len; |
| 851 | inptr = (psa_invec *)args[2]; |
| 852 | outptr = (psa_outvec *)args[3]; |
| 853 | |
| 854 | /* The request type must be zero or positive. */ |
| 855 | if (type < 0) { |
| 856 | tfm_core_panic(); |
| 857 | } |
| 858 | |
| 859 | return tfm_spm_client_psa_call(handle, type, inptr, in_num, outptr, out_num, |
| 860 | ns_caller, privileged); |
| 861 | } |
| 862 | |
| 863 | void tfm_spm_psa_close(uint32_t *args, bool ns_caller) |
| 864 | { |
| 865 | psa_handle_t handle; |
| 866 | |
| 867 | TFM_CORE_ASSERT(args != NULL); |
| 868 | handle = args[0]; |
| 869 | |
| 870 | tfm_spm_client_psa_close(handle, ns_caller); |
| 871 | } |
| 872 | |
| 873 | uint32_t tfm_spm_get_lifecycle_state(void) |
| 874 | { |
| 875 | /* |
| 876 | * FixMe: return PSA_LIFECYCLE_UNKNOWN to the caller directly. It will be |
| 877 | * implemented in the future. |
| 878 | */ |
| 879 | return PSA_LIFECYCLE_UNKNOWN; |
| 880 | } |
| 881 | |
| 882 | /********************* SPM functions for PSA Service APIs ********************/ |
| 883 | |
| 884 | psa_signal_t tfm_spm_psa_wait(uint32_t *args) |
| 885 | { |
| 886 | psa_signal_t signal_mask; |
| 887 | uint32_t timeout; |
| 888 | struct spm_partition_desc_t *partition = NULL; |
| 889 | |
| 890 | TFM_CORE_ASSERT(args != NULL); |
| 891 | signal_mask = (psa_signal_t)args[0]; |
| 892 | timeout = args[1]; |
| 893 | |
| 894 | /* |
| 895 | * Timeout[30:0] are reserved for future use. |
| 896 | * SPM must ignore the value of RES. |
| 897 | */ |
| 898 | timeout &= PSA_TIMEOUT_MASK; |
| 899 | |
| 900 | partition = tfm_spm_get_running_partition(); |
| 901 | if (!partition) { |
| 902 | tfm_core_panic(); |
| 903 | } |
| 904 | |
| 905 | /* |
| 906 | * It is a PROGRAMMER ERROR if the signal_mask does not include any assigned |
| 907 | * signals. |
| 908 | */ |
| 909 | if ((partition->runtime_data.assigned_signals & signal_mask) == 0) { |
| 910 | tfm_core_panic(); |
| 911 | } |
| 912 | |
| 913 | /* |
| 914 | * Expected signals are included in signal wait mask, ignored signals |
| 915 | * should not be set and affect caller thread state. Save this mask for |
| 916 | * further checking while signals are ready to be set. |
| 917 | */ |
| 918 | partition->runtime_data.signal_mask = signal_mask; |
| 919 | |
| 920 | /* |
| 921 | * tfm_event_wait() blocks the caller thread if no signals are available. |
| 922 | * In this case, the return value of this function is temporary set into |
| 923 | * runtime context. After new signal(s) are available, the return value |
| 924 | * is updated with the available signal(s) and blocked thread gets to run. |
| 925 | */ |
| 926 | if (timeout == PSA_BLOCK && |
| 927 | (partition->runtime_data.signals & signal_mask) == 0) { |
| 928 | tfm_event_wait(&partition->runtime_data.signal_evnt); |
| 929 | } |
| 930 | |
| 931 | return partition->runtime_data.signals & signal_mask; |
| 932 | } |
| 933 | |
| 934 | psa_status_t tfm_spm_psa_get(uint32_t *args) |
| 935 | { |
| 936 | psa_signal_t signal; |
| 937 | psa_msg_t *msg = NULL; |
| 938 | struct tfm_spm_service_t *service = NULL; |
| 939 | struct tfm_msg_body_t *tmp_msg = NULL; |
| 940 | struct spm_partition_desc_t *partition = NULL; |
| 941 | uint32_t privileged; |
| 942 | |
| 943 | TFM_CORE_ASSERT(args != NULL); |
| 944 | signal = (psa_signal_t)args[0]; |
| 945 | msg = (psa_msg_t *)args[1]; |
| 946 | |
| 947 | /* |
| 948 | * Only one message could be retrieved every time for psa_get(). It is a |
| 949 | * fatal error if the input signal has more than a signal bit set. |
| 950 | */ |
Ken Liu | 410ada5 | 2020-01-08 11:37:27 +0800 | [diff] [blame] | 951 | if (!tfm_is_one_bit_set(signal)) { |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 952 | tfm_core_panic(); |
| 953 | } |
| 954 | |
| 955 | partition = tfm_spm_get_running_partition(); |
| 956 | if (!partition) { |
| 957 | tfm_core_panic(); |
| 958 | } |
| 959 | privileged = tfm_spm_partition_get_privileged_mode( |
| 960 | partition->static_data->partition_flags); |
| 961 | |
| 962 | /* |
| 963 | * Write the message to the service buffer. It is a fatal error if the |
| 964 | * input msg pointer is not a valid memory reference or not read-write. |
| 965 | */ |
| 966 | if (tfm_memory_check(msg, sizeof(psa_msg_t), false, TFM_MEMORY_ACCESS_RW, |
| 967 | privileged) != IPC_SUCCESS) { |
| 968 | tfm_core_panic(); |
| 969 | } |
| 970 | |
| 971 | /* |
| 972 | * It is a fatal error if the caller call psa_get() when no message has |
| 973 | * been set. The caller must call this function after an RoT Service signal |
| 974 | * is returned by psa_wait(). |
| 975 | */ |
| 976 | if (partition->runtime_data.signals == 0) { |
| 977 | tfm_core_panic(); |
| 978 | } |
| 979 | |
| 980 | /* |
| 981 | * It is a fatal error if the RoT Service signal is not currently asserted. |
| 982 | */ |
| 983 | if ((partition->runtime_data.signals & signal) == 0) { |
| 984 | tfm_core_panic(); |
| 985 | } |
| 986 | |
| 987 | /* |
| 988 | * Get RoT service by signal from partition. It is a fatal error if getting |
| 989 | * failed, which means the input signal is not correspond to an RoT service. |
| 990 | */ |
| 991 | service = tfm_spm_get_service_by_signal(partition, signal); |
| 992 | if (!service) { |
| 993 | tfm_core_panic(); |
| 994 | } |
| 995 | |
| 996 | tmp_msg = tfm_msg_dequeue(&service->msg_queue); |
| 997 | if (!tmp_msg) { |
| 998 | return PSA_ERROR_DOES_NOT_EXIST; |
| 999 | } |
| 1000 | |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 1001 | (TFM_GET_CONTAINER_PTR(tmp_msg, |
| 1002 | struct tfm_conn_handle_t, |
| 1003 | internal_msg))->status = TFM_HANDLE_STATUS_ACTIVE; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 1004 | |
| 1005 | tfm_core_util_memcpy(msg, &tmp_msg->msg, sizeof(psa_msg_t)); |
| 1006 | |
| 1007 | /* |
| 1008 | * There may be multiple messages for this RoT Service signal, do not clear |
| 1009 | * its mask until no remaining message. |
| 1010 | */ |
| 1011 | if (tfm_msg_queue_is_empty(&service->msg_queue)) { |
| 1012 | partition->runtime_data.signals &= ~signal; |
| 1013 | } |
| 1014 | |
| 1015 | return PSA_SUCCESS; |
| 1016 | } |
| 1017 | |
| 1018 | void tfm_spm_psa_set_rhandle(uint32_t *args) |
| 1019 | { |
| 1020 | psa_handle_t msg_handle; |
| 1021 | void *rhandle = NULL; |
| 1022 | struct tfm_msg_body_t *msg = NULL; |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 1023 | struct tfm_conn_handle_t *conn_handle; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 1024 | |
| 1025 | TFM_CORE_ASSERT(args != NULL); |
| 1026 | msg_handle = (psa_handle_t)args[0]; |
| 1027 | rhandle = (void *)args[1]; |
| 1028 | |
| 1029 | /* It is a fatal error if message handle is invalid */ |
| 1030 | msg = tfm_spm_get_msg_from_handle(msg_handle); |
| 1031 | if (!msg) { |
| 1032 | tfm_core_panic(); |
| 1033 | } |
| 1034 | |
| 1035 | msg->msg.rhandle = rhandle; |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 1036 | conn_handle = tfm_spm_to_handle_instance(msg_handle); |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 1037 | |
| 1038 | /* Store reverse handle for following client calls. */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 1039 | tfm_spm_set_rhandle(msg->service, conn_handle, rhandle); |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | size_t tfm_spm_psa_read(uint32_t *args) |
| 1043 | { |
| 1044 | psa_handle_t msg_handle; |
| 1045 | uint32_t invec_idx; |
| 1046 | void *buffer = NULL; |
| 1047 | size_t num_bytes; |
| 1048 | size_t bytes; |
| 1049 | struct tfm_msg_body_t *msg = NULL; |
| 1050 | uint32_t privileged; |
| 1051 | struct spm_partition_desc_t *partition = NULL; |
| 1052 | |
| 1053 | TFM_CORE_ASSERT(args != NULL); |
| 1054 | msg_handle = (psa_handle_t)args[0]; |
| 1055 | invec_idx = args[1]; |
| 1056 | buffer = (void *)args[2]; |
| 1057 | num_bytes = (size_t)args[3]; |
| 1058 | |
| 1059 | /* It is a fatal error if message handle is invalid */ |
| 1060 | msg = tfm_spm_get_msg_from_handle(msg_handle); |
| 1061 | if (!msg) { |
| 1062 | tfm_core_panic(); |
| 1063 | } |
| 1064 | |
| 1065 | partition = msg->service->partition; |
| 1066 | privileged = tfm_spm_partition_get_privileged_mode( |
| 1067 | partition->static_data->partition_flags); |
| 1068 | |
| 1069 | /* |
| 1070 | * It is a fatal error if message handle does not refer to a request |
| 1071 | * message |
| 1072 | */ |
| 1073 | if (msg->msg.type < PSA_IPC_CALL) { |
| 1074 | tfm_core_panic(); |
| 1075 | } |
| 1076 | |
| 1077 | /* |
| 1078 | * It is a fatal error if invec_idx is equal to or greater than |
| 1079 | * PSA_MAX_IOVEC |
| 1080 | */ |
| 1081 | if (invec_idx >= PSA_MAX_IOVEC) { |
| 1082 | tfm_core_panic(); |
| 1083 | } |
| 1084 | |
| 1085 | /* There was no remaining data in this input vector */ |
| 1086 | if (msg->msg.in_size[invec_idx] == 0) { |
| 1087 | return 0; |
| 1088 | } |
| 1089 | |
| 1090 | /* |
| 1091 | * Copy the client data to the service buffer. It is a fatal error |
| 1092 | * if the memory reference for buffer is invalid or not read-write. |
| 1093 | */ |
| 1094 | if (tfm_memory_check(buffer, num_bytes, false, |
| 1095 | TFM_MEMORY_ACCESS_RW, privileged) != IPC_SUCCESS) { |
| 1096 | tfm_core_panic(); |
| 1097 | } |
| 1098 | |
| 1099 | bytes = num_bytes > msg->msg.in_size[invec_idx] ? |
| 1100 | msg->msg.in_size[invec_idx] : num_bytes; |
| 1101 | |
| 1102 | tfm_core_util_memcpy(buffer, msg->invec[invec_idx].base, bytes); |
| 1103 | |
| 1104 | /* There maybe some remaining data */ |
| 1105 | msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base + bytes; |
| 1106 | msg->msg.in_size[invec_idx] -= bytes; |
| 1107 | |
| 1108 | return bytes; |
| 1109 | } |
| 1110 | |
| 1111 | size_t tfm_spm_psa_skip(uint32_t *args) |
| 1112 | { |
| 1113 | psa_handle_t msg_handle; |
| 1114 | uint32_t invec_idx; |
| 1115 | size_t num_bytes; |
| 1116 | struct tfm_msg_body_t *msg = NULL; |
| 1117 | |
| 1118 | TFM_CORE_ASSERT(args != NULL); |
| 1119 | msg_handle = (psa_handle_t)args[0]; |
| 1120 | invec_idx = args[1]; |
| 1121 | num_bytes = (size_t)args[2]; |
| 1122 | |
| 1123 | /* It is a fatal error if message handle is invalid */ |
| 1124 | msg = tfm_spm_get_msg_from_handle(msg_handle); |
| 1125 | if (!msg) { |
| 1126 | tfm_core_panic(); |
| 1127 | } |
| 1128 | |
| 1129 | /* |
| 1130 | * It is a fatal error if message handle does not refer to a request |
| 1131 | * message |
| 1132 | */ |
| 1133 | if (msg->msg.type < PSA_IPC_CALL) { |
| 1134 | tfm_core_panic(); |
| 1135 | } |
| 1136 | |
| 1137 | /* |
| 1138 | * It is a fatal error if invec_idx is equal to or greater than |
| 1139 | * PSA_MAX_IOVEC |
| 1140 | */ |
| 1141 | if (invec_idx >= PSA_MAX_IOVEC) { |
| 1142 | tfm_core_panic(); |
| 1143 | } |
| 1144 | |
| 1145 | /* There was no remaining data in this input vector */ |
| 1146 | if (msg->msg.in_size[invec_idx] == 0) { |
| 1147 | return 0; |
| 1148 | } |
| 1149 | |
| 1150 | /* |
| 1151 | * If num_bytes is greater than the remaining size of the input vector then |
| 1152 | * the remaining size of the input vector is used. |
| 1153 | */ |
| 1154 | if (num_bytes > msg->msg.in_size[invec_idx]) { |
| 1155 | num_bytes = msg->msg.in_size[invec_idx]; |
| 1156 | } |
| 1157 | |
| 1158 | /* There maybe some remaining data */ |
| 1159 | msg->invec[invec_idx].base = (char *)msg->invec[invec_idx].base + |
| 1160 | num_bytes; |
| 1161 | msg->msg.in_size[invec_idx] -= num_bytes; |
| 1162 | |
| 1163 | return num_bytes; |
| 1164 | } |
| 1165 | |
| 1166 | void tfm_spm_psa_write(uint32_t *args) |
| 1167 | { |
| 1168 | psa_handle_t msg_handle; |
| 1169 | uint32_t outvec_idx; |
| 1170 | void *buffer = NULL; |
| 1171 | size_t num_bytes; |
| 1172 | struct tfm_msg_body_t *msg = NULL; |
| 1173 | uint32_t privileged; |
| 1174 | struct spm_partition_desc_t *partition = NULL; |
| 1175 | |
| 1176 | TFM_CORE_ASSERT(args != NULL); |
| 1177 | msg_handle = (psa_handle_t)args[0]; |
| 1178 | outvec_idx = args[1]; |
| 1179 | buffer = (void *)args[2]; |
| 1180 | num_bytes = (size_t)args[3]; |
| 1181 | |
| 1182 | /* It is a fatal error if message handle is invalid */ |
| 1183 | msg = tfm_spm_get_msg_from_handle(msg_handle); |
| 1184 | if (!msg) { |
| 1185 | tfm_core_panic(); |
| 1186 | } |
| 1187 | |
| 1188 | partition = msg->service->partition; |
| 1189 | privileged = tfm_spm_partition_get_privileged_mode( |
| 1190 | partition->static_data->partition_flags); |
| 1191 | |
| 1192 | /* |
| 1193 | * It is a fatal error if message handle does not refer to a request |
| 1194 | * message |
| 1195 | */ |
| 1196 | if (msg->msg.type < PSA_IPC_CALL) { |
| 1197 | tfm_core_panic(); |
| 1198 | } |
| 1199 | |
| 1200 | /* |
| 1201 | * It is a fatal error if outvec_idx is equal to or greater than |
| 1202 | * PSA_MAX_IOVEC |
| 1203 | */ |
| 1204 | if (outvec_idx >= PSA_MAX_IOVEC) { |
| 1205 | tfm_core_panic(); |
| 1206 | } |
| 1207 | |
| 1208 | /* |
| 1209 | * It is a fatal error if the call attempts to write data past the end of |
| 1210 | * the client output vector |
| 1211 | */ |
| 1212 | if (num_bytes > msg->msg.out_size[outvec_idx] - |
| 1213 | msg->outvec[outvec_idx].len) { |
| 1214 | tfm_core_panic(); |
| 1215 | } |
| 1216 | |
| 1217 | /* |
| 1218 | * Copy the service buffer to client outvecs. It is a fatal error |
| 1219 | * if the memory reference for buffer is invalid or not readable. |
| 1220 | */ |
| 1221 | if (tfm_memory_check(buffer, num_bytes, false, |
| 1222 | TFM_MEMORY_ACCESS_RO, privileged) != IPC_SUCCESS) { |
| 1223 | tfm_core_panic(); |
| 1224 | } |
| 1225 | |
| 1226 | tfm_core_util_memcpy((char *)msg->outvec[outvec_idx].base + |
| 1227 | msg->outvec[outvec_idx].len, buffer, num_bytes); |
| 1228 | |
| 1229 | /* Update the write number */ |
| 1230 | msg->outvec[outvec_idx].len += num_bytes; |
| 1231 | } |
| 1232 | |
| 1233 | static void update_caller_outvec_len(struct tfm_msg_body_t *msg) |
| 1234 | { |
| 1235 | uint32_t i; |
| 1236 | |
| 1237 | /* |
| 1238 | * FixeMe: abstract these part into dedicated functions to avoid |
| 1239 | * accessing thread context in psa layer |
| 1240 | */ |
| 1241 | /* If it is a NS request via RPC, the owner of this message is not set */ |
| 1242 | if (!is_tfm_rpc_msg(msg)) { |
| 1243 | TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK); |
| 1244 | } |
| 1245 | |
| 1246 | for (i = 0; i < PSA_MAX_IOVEC; i++) { |
| 1247 | if (msg->msg.out_size[i] == 0) { |
| 1248 | continue; |
| 1249 | } |
| 1250 | |
| 1251 | TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base); |
| 1252 | |
| 1253 | msg->caller_outvec[i].len = msg->outvec[i].len; |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | void tfm_spm_psa_reply(uint32_t *args) |
| 1258 | { |
| 1259 | psa_handle_t msg_handle; |
| 1260 | psa_status_t status; |
| 1261 | struct tfm_spm_service_t *service = NULL; |
| 1262 | struct tfm_msg_body_t *msg = NULL; |
| 1263 | int32_t ret = PSA_SUCCESS; |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 1264 | struct tfm_conn_handle_t *conn_handle; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 1265 | |
| 1266 | TFM_CORE_ASSERT(args != NULL); |
| 1267 | msg_handle = (psa_handle_t)args[0]; |
| 1268 | status = (psa_status_t)args[1]; |
| 1269 | |
| 1270 | /* It is a fatal error if message handle is invalid */ |
| 1271 | msg = tfm_spm_get_msg_from_handle(msg_handle); |
| 1272 | if (!msg) { |
| 1273 | tfm_core_panic(); |
| 1274 | } |
| 1275 | |
| 1276 | /* |
| 1277 | * RoT Service information is needed in this function, stored it in message |
| 1278 | * body structure. Only two parameters are passed in this function: handle |
| 1279 | * and status, so it is useful and simply to do like this. |
| 1280 | */ |
| 1281 | service = msg->service; |
| 1282 | if (!service) { |
| 1283 | tfm_core_panic(); |
| 1284 | } |
| 1285 | |
| 1286 | /* |
| 1287 | * Three type of message are passed in this function: CONNECTION, REQUEST, |
| 1288 | * DISCONNECTION. It needs to process differently for each type. |
| 1289 | */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 1290 | conn_handle = tfm_spm_to_handle_instance(msg_handle); |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 1291 | switch (msg->msg.type) { |
| 1292 | case PSA_IPC_CONNECT: |
| 1293 | /* |
| 1294 | * Reply to PSA_IPC_CONNECT message. Connect handle is returned if the |
| 1295 | * input status is PSA_SUCCESS. Others return values are based on the |
| 1296 | * input status. |
| 1297 | */ |
| 1298 | if (status == PSA_SUCCESS) { |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 1299 | ret = msg_handle; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 1300 | } else if (status == PSA_ERROR_CONNECTION_REFUSED) { |
| 1301 | /* Refuse the client connection, indicating a permanent error. */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 1302 | tfm_spm_free_conn_handle(service, conn_handle); |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 1303 | ret = PSA_ERROR_CONNECTION_REFUSED; |
| 1304 | } else if (status == PSA_ERROR_CONNECTION_BUSY) { |
| 1305 | /* Fail the client connection, indicating a transient error. */ |
| 1306 | ret = PSA_ERROR_CONNECTION_BUSY; |
| 1307 | } else { |
| 1308 | tfm_core_panic(); |
| 1309 | } |
| 1310 | break; |
| 1311 | case PSA_IPC_DISCONNECT: |
| 1312 | /* Service handle is not used anymore */ |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 1313 | tfm_spm_free_conn_handle(service, conn_handle); |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 1314 | |
| 1315 | /* |
| 1316 | * If the message type is PSA_IPC_DISCONNECT, then the status code is |
| 1317 | * ignored |
| 1318 | */ |
| 1319 | break; |
| 1320 | default: |
| 1321 | if (msg->msg.type >= PSA_IPC_CALL) { |
| 1322 | /* Reply to a request message. Return values are based on status */ |
| 1323 | ret = status; |
| 1324 | /* |
| 1325 | * The total number of bytes written to a single parameter must be |
| 1326 | * reported to the client by updating the len member of the |
| 1327 | * psa_outvec structure for the parameter before returning from |
| 1328 | * psa_call(). |
| 1329 | */ |
| 1330 | update_caller_outvec_len(msg); |
| 1331 | } else { |
| 1332 | tfm_core_panic(); |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | if (ret == PSA_ERROR_PROGRAMMER_ERROR) { |
| 1337 | /* |
| 1338 | * If the source of the programmer error is a Secure Partition, the SPM |
| 1339 | * must panic the Secure Partition in response to a PROGRAMMER ERROR. |
| 1340 | */ |
| 1341 | if (TFM_CLIENT_ID_IS_NS(msg->msg.client_id)) { |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 1342 | conn_handle->status = TFM_HANDLE_STATUS_CONNECT_ERROR; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 1343 | } else { |
| 1344 | tfm_core_panic(); |
| 1345 | } |
| 1346 | } else { |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame^] | 1347 | conn_handle->status = TFM_HANDLE_STATUS_IDLE; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | if (is_tfm_rpc_msg(msg)) { |
| 1351 | tfm_rpc_client_call_reply(msg, ret); |
| 1352 | } else { |
| 1353 | tfm_event_wake(&msg->ack_evnt, ret); |
| 1354 | } |
| 1355 | } |
| 1356 | |
| 1357 | /** |
| 1358 | * \brief notify the partition with the signal. |
| 1359 | * |
| 1360 | * \param[in] partition_id The ID of the partition to be notified. |
| 1361 | * \param[in] signal The signal that the partition is to be notified |
| 1362 | * with. |
| 1363 | * |
| 1364 | * \retval void Success. |
| 1365 | * \retval "Does not return" If partition_id is invalid. |
| 1366 | */ |
| 1367 | static void notify_with_signal(int32_t partition_id, psa_signal_t signal) |
| 1368 | { |
| 1369 | struct spm_partition_desc_t *partition = NULL; |
| 1370 | |
| 1371 | /* |
| 1372 | * The value of partition_id must be greater than zero as the target of |
| 1373 | * notification must be a Secure Partition, providing a Non-secure |
| 1374 | * Partition ID is a fatal error. |
| 1375 | */ |
| 1376 | if (!TFM_CLIENT_ID_IS_S(partition_id)) { |
| 1377 | tfm_core_panic(); |
| 1378 | } |
| 1379 | |
| 1380 | /* |
| 1381 | * It is a fatal error if partition_id does not correspond to a Secure |
| 1382 | * Partition. |
| 1383 | */ |
| 1384 | partition = tfm_spm_get_partition_by_id(partition_id); |
| 1385 | if (!partition) { |
| 1386 | tfm_core_panic(); |
| 1387 | } |
| 1388 | |
| 1389 | partition->runtime_data.signals |= signal; |
| 1390 | |
| 1391 | /* |
| 1392 | * The target partition may be blocked with waiting for signals after |
| 1393 | * called psa_wait(). Set the return value with the available signals |
| 1394 | * before wake it up with tfm_event_signal(). |
| 1395 | */ |
| 1396 | tfm_event_wake(&partition->runtime_data.signal_evnt, |
| 1397 | partition->runtime_data.signals & |
| 1398 | partition->runtime_data.signal_mask); |
| 1399 | } |
| 1400 | |
| 1401 | void tfm_spm_psa_notify(uint32_t *args) |
| 1402 | { |
| 1403 | int32_t partition_id; |
| 1404 | |
| 1405 | TFM_CORE_ASSERT(args != NULL); |
| 1406 | partition_id = (int32_t)args[0]; |
| 1407 | |
| 1408 | notify_with_signal(partition_id, PSA_DOORBELL); |
| 1409 | } |
| 1410 | |
| 1411 | /** |
| 1412 | * \brief assert signal for a given IRQ line. |
| 1413 | * |
| 1414 | * \param[in] partition_id The ID of the partition which handles this IRQ |
| 1415 | * \param[in] signal The signal associated with this IRQ |
| 1416 | * \param[in] irq_line The number of the IRQ line |
| 1417 | * |
| 1418 | * \retval void Success. |
| 1419 | * \retval "Does not return" Partition ID is invalid |
| 1420 | */ |
| 1421 | void tfm_irq_handler(uint32_t partition_id, psa_signal_t signal, |
TTornblom | faf74f5 | 2020-03-04 17:56:27 +0100 | [diff] [blame] | 1422 | IRQn_Type irq_line) |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 1423 | { |
| 1424 | tfm_spm_hal_disable_irq(irq_line); |
| 1425 | notify_with_signal(partition_id, signal); |
| 1426 | } |
| 1427 | |
| 1428 | void tfm_spm_psa_clear(void) |
| 1429 | { |
| 1430 | struct spm_partition_desc_t *partition = NULL; |
| 1431 | |
| 1432 | partition = tfm_spm_get_running_partition(); |
| 1433 | if (!partition) { |
| 1434 | tfm_core_panic(); |
| 1435 | } |
| 1436 | |
| 1437 | /* |
| 1438 | * It is a fatal error if the Secure Partition's doorbell signal is not |
| 1439 | * currently asserted. |
| 1440 | */ |
| 1441 | if ((partition->runtime_data.signals & PSA_DOORBELL) == 0) { |
| 1442 | tfm_core_panic(); |
| 1443 | } |
| 1444 | partition->runtime_data.signals &= ~PSA_DOORBELL; |
| 1445 | } |
| 1446 | |
| 1447 | void tfm_spm_psa_panic(void) |
| 1448 | { |
| 1449 | /* |
| 1450 | * PSA FF recommends that the SPM causes the system to restart when a secure |
| 1451 | * partition panics. |
| 1452 | */ |
| 1453 | tfm_spm_hal_system_reset(); |
| 1454 | } |
| 1455 | |
| 1456 | /** |
| 1457 | * \brief Return the IRQ line number associated with a signal |
| 1458 | * |
| 1459 | * \param[in] partition_id The ID of the partition in which we look for |
| 1460 | * the signal. |
| 1461 | * \param[in] signal The signal we do the query for. |
| 1462 | * \param[out] irq_line The irq line associated with signal |
| 1463 | * |
| 1464 | * \retval IPC_SUCCESS Execution successful, irq_line contains a valid |
| 1465 | * value. |
| 1466 | * \retval IPC_ERROR_GENERIC There was an error finding the IRQ line for the |
| 1467 | * signal. irq_line is unchanged. |
| 1468 | */ |
| 1469 | static int32_t get_irq_line_for_signal(int32_t partition_id, |
| 1470 | psa_signal_t signal, |
TTornblom | faf74f5 | 2020-03-04 17:56:27 +0100 | [diff] [blame] | 1471 | IRQn_Type *irq_line) |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 1472 | { |
| 1473 | size_t i; |
| 1474 | |
| 1475 | for (i = 0; i < tfm_core_irq_signals_count; ++i) { |
| 1476 | if (tfm_core_irq_signals[i].partition_id == partition_id && |
| 1477 | tfm_core_irq_signals[i].signal_value == signal) { |
| 1478 | *irq_line = tfm_core_irq_signals[i].irq_line; |
| 1479 | return IPC_SUCCESS; |
| 1480 | } |
| 1481 | } |
| 1482 | return IPC_ERROR_GENERIC; |
| 1483 | } |
| 1484 | |
| 1485 | void tfm_spm_psa_eoi(uint32_t *args) |
| 1486 | { |
| 1487 | psa_signal_t irq_signal; |
TTornblom | faf74f5 | 2020-03-04 17:56:27 +0100 | [diff] [blame] | 1488 | IRQn_Type irq_line = (IRQn_Type) 0; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 1489 | int32_t ret; |
| 1490 | struct spm_partition_desc_t *partition = NULL; |
| 1491 | |
| 1492 | TFM_CORE_ASSERT(args != NULL); |
| 1493 | irq_signal = (psa_signal_t)args[0]; |
| 1494 | |
| 1495 | /* It is a fatal error if passed signal indicates more than one signals. */ |
| 1496 | if (!tfm_is_one_bit_set(irq_signal)) { |
| 1497 | tfm_core_panic(); |
| 1498 | } |
| 1499 | |
| 1500 | partition = tfm_spm_get_running_partition(); |
| 1501 | if (!partition) { |
| 1502 | tfm_core_panic(); |
| 1503 | } |
| 1504 | |
| 1505 | ret = get_irq_line_for_signal(partition->static_data->partition_id, |
| 1506 | irq_signal, &irq_line); |
| 1507 | /* It is a fatal error if passed signal is not an interrupt signal. */ |
| 1508 | if (ret != IPC_SUCCESS) { |
| 1509 | tfm_core_panic(); |
| 1510 | } |
| 1511 | |
| 1512 | /* It is a fatal error if passed signal is not currently asserted */ |
| 1513 | if ((partition->runtime_data.signals & irq_signal) == 0) { |
| 1514 | tfm_core_panic(); |
| 1515 | } |
| 1516 | |
| 1517 | partition->runtime_data.signals &= ~irq_signal; |
| 1518 | |
| 1519 | tfm_spm_hal_clear_pending_irq(irq_line); |
| 1520 | tfm_spm_hal_enable_irq(irq_line); |
| 1521 | } |
| 1522 | |
| 1523 | void tfm_spm_enable_irq(uint32_t *args) |
| 1524 | { |
| 1525 | struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)args; |
| 1526 | psa_signal_t irq_signal = svc_ctx->r0; |
TTornblom | faf74f5 | 2020-03-04 17:56:27 +0100 | [diff] [blame] | 1527 | IRQn_Type irq_line = (IRQn_Type) 0; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 1528 | int32_t ret; |
| 1529 | struct spm_partition_desc_t *partition = NULL; |
| 1530 | |
| 1531 | /* It is a fatal error if passed signal indicates more than one signals. */ |
| 1532 | if (!tfm_is_one_bit_set(irq_signal)) { |
| 1533 | tfm_core_panic(); |
| 1534 | } |
| 1535 | |
| 1536 | partition = tfm_spm_get_running_partition(); |
| 1537 | if (!partition) { |
| 1538 | tfm_core_panic(); |
| 1539 | } |
| 1540 | |
| 1541 | ret = get_irq_line_for_signal(partition->static_data->partition_id, |
| 1542 | irq_signal, &irq_line); |
| 1543 | /* It is a fatal error if passed signal is not an interrupt signal. */ |
| 1544 | if (ret != IPC_SUCCESS) { |
| 1545 | tfm_core_panic(); |
| 1546 | } |
| 1547 | |
| 1548 | tfm_spm_hal_enable_irq(irq_line); |
| 1549 | } |
| 1550 | |
| 1551 | void tfm_spm_disable_irq(uint32_t *args) |
| 1552 | { |
| 1553 | struct tfm_state_context_t *svc_ctx = (struct tfm_state_context_t *)args; |
| 1554 | psa_signal_t irq_signal = svc_ctx->r0; |
TTornblom | faf74f5 | 2020-03-04 17:56:27 +0100 | [diff] [blame] | 1555 | IRQn_Type irq_line = (IRQn_Type) 0; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 1556 | int32_t ret; |
| 1557 | struct spm_partition_desc_t *partition = NULL; |
| 1558 | |
| 1559 | /* It is a fatal error if passed signal indicates more than one signals. */ |
| 1560 | if (!tfm_is_one_bit_set(irq_signal)) { |
| 1561 | tfm_core_panic(); |
| 1562 | } |
| 1563 | |
| 1564 | partition = tfm_spm_get_running_partition(); |
| 1565 | if (!partition) { |
| 1566 | tfm_core_panic(); |
| 1567 | } |
| 1568 | |
| 1569 | ret = get_irq_line_for_signal(partition->static_data->partition_id, |
| 1570 | irq_signal, &irq_line); |
| 1571 | /* It is a fatal error if passed signal is not an interrupt signal. */ |
| 1572 | if (ret != IPC_SUCCESS) { |
| 1573 | tfm_core_panic(); |
| 1574 | } |
| 1575 | |
| 1576 | tfm_spm_hal_disable_irq(irq_line); |
| 1577 | } |
| 1578 | |
| 1579 | void tfm_spm_validate_caller(struct spm_partition_desc_t *p_cur_sp, |
| 1580 | uint32_t *p_ctx, uint32_t exc_return, |
| 1581 | bool ns_caller) |
| 1582 | { |
| 1583 | uintptr_t stacked_ctx_pos; |
| 1584 | |
| 1585 | if (ns_caller) { |
| 1586 | /* |
| 1587 | * The background IRQ can't be supported, since if SP is executing, |
| 1588 | * the preempted context of SP can be different with the one who |
| 1589 | * preempts veneer. |
| 1590 | */ |
| 1591 | if (p_cur_sp->static_data->partition_id != TFM_SP_NON_SECURE_ID) { |
| 1592 | tfm_core_panic(); |
| 1593 | } |
| 1594 | |
| 1595 | /* |
| 1596 | * It is non-secure caller, check if veneer stack contains |
| 1597 | * multiple contexts. |
| 1598 | */ |
| 1599 | stacked_ctx_pos = (uintptr_t)p_ctx + |
| 1600 | sizeof(struct tfm_state_context_t) + |
| 1601 | TFM_VENEER_STACK_GUARD_SIZE; |
| 1602 | |
| 1603 | if (is_stack_alloc_fp_space(exc_return)) { |
| 1604 | #if defined (__FPU_USED) && (__FPU_USED == 1U) |
| 1605 | if (FPU->FPCCR & FPU_FPCCR_TS_Msk) { |
| 1606 | stacked_ctx_pos += TFM_ADDTIONAL_FP_CONTEXT_WORDS * |
| 1607 | sizeof(uint32_t); |
| 1608 | } |
| 1609 | #endif |
| 1610 | stacked_ctx_pos += TFM_BASIC_FP_CONTEXT_WORDS * sizeof(uint32_t); |
| 1611 | } |
| 1612 | |
| 1613 | if (stacked_ctx_pos != p_cur_sp->runtime_data.sp_thrd.stk_top) { |
| 1614 | tfm_core_panic(); |
| 1615 | } |
| 1616 | } else if (p_cur_sp->static_data->partition_id <= 0) { |
| 1617 | tfm_core_panic(); |
| 1618 | } |
| 1619 | } |
Summer Qin | 830c554 | 2020-02-14 13:44:20 +0800 | [diff] [blame] | 1620 | |
| 1621 | void tfm_spm_request_handler(const struct tfm_state_context_t *svc_ctx) |
| 1622 | { |
| 1623 | uint32_t *res_ptr = (uint32_t *)&svc_ctx->r0; |
| 1624 | uint32_t running_partition_flags = 0; |
| 1625 | const struct spm_partition_desc_t *partition = NULL; |
| 1626 | |
| 1627 | /* Check permissions on request type basis */ |
| 1628 | |
| 1629 | switch (svc_ctx->r0) { |
| 1630 | case TFM_SPM_REQUEST_RESET_VOTE: |
| 1631 | partition = tfm_spm_get_running_partition(); |
| 1632 | if (!partition) { |
| 1633 | tfm_core_panic(); |
| 1634 | } |
| 1635 | running_partition_flags = partition->static_data->partition_flags; |
| 1636 | |
| 1637 | /* Currently only PSA Root of Trust services are allowed to make Reset |
| 1638 | * vote request |
| 1639 | */ |
| 1640 | if ((running_partition_flags & SPM_PART_FLAG_PSA_ROT) == 0) { |
| 1641 | *res_ptr = (uint32_t)TFM_ERROR_GENERIC; |
| 1642 | } |
| 1643 | |
| 1644 | /* FixMe: this is a placeholder for checks to be performed before |
| 1645 | * allowing execution of reset |
| 1646 | */ |
| 1647 | *res_ptr = (uint32_t)TFM_SUCCESS; |
| 1648 | |
| 1649 | break; |
| 1650 | default: |
| 1651 | *res_ptr = (uint32_t)TFM_ERROR_INVALID_PARAMETER; |
| 1652 | } |
| 1653 | } |
Mingyang Sun | bd7ceb5 | 2020-06-11 16:53:03 +0800 | [diff] [blame] | 1654 | |
| 1655 | enum spm_err_t tfm_spm_db_init(void) |
| 1656 | { |
| 1657 | uint32_t i; |
| 1658 | |
| 1659 | /* This function initialises partition db */ |
| 1660 | |
| 1661 | for (i = 0; i < g_spm_partition_db.partition_count; i++) { |
| 1662 | g_spm_partition_db.partitions[i].static_data = &static_data_list[i]; |
| 1663 | g_spm_partition_db.partitions[i].platform_data_list = |
| 1664 | platform_data_list_list[i]; |
| 1665 | g_spm_partition_db.partitions[i].memory_data = &memory_data_list[i]; |
| 1666 | } |
| 1667 | g_spm_partition_db.is_init = 1; |
| 1668 | |
| 1669 | return SPM_ERR_OK; |
| 1670 | } |