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