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