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