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