Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 1 | /* |
Sherry Zhang | dcab286 | 2022-01-10 10:43:31 +0800 | [diff] [blame] | 2 | * Copyright (c) 2018-2022, Arm Limited. All rights reserved. |
Chris Brand | 56b4d0c | 2021-12-17 16:15:58 -0800 | [diff] [blame] | 3 | * Copyright (c) 2021, Cypress Semiconductor Corporation. All rights reserved. |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 4 | * |
| 5 | * SPDX-License-Identifier: BSD-3-Clause |
| 6 | * |
| 7 | */ |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 8 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 9 | #include <inttypes.h> |
| 10 | #include <stdbool.h> |
Ken Liu | 086b32c | 2021-12-27 14:44:52 +0800 | [diff] [blame] | 11 | #include "aapcs_local.h" |
Ken Liu | 24dffb2 | 2021-02-10 11:03:58 +0800 | [diff] [blame] | 12 | #include "bitops.h" |
Mingyang Sun | 4609ac7 | 2022-02-08 18:56:35 +0800 | [diff] [blame] | 13 | #include "config_impl.h" |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 14 | #include "critical_section.h" |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 15 | #include "current.h" |
David Hu | f07e97d | 2021-02-15 22:05:40 +0800 | [diff] [blame] | 16 | #include "fih.h" |
Jamie Fox | cc31d40 | 2019-01-28 17:13:52 +0000 | [diff] [blame] | 17 | #include "psa/client.h" |
| 18 | #include "psa/service.h" |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 19 | #include "thread.h" |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 20 | #include "internal_errors.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 21 | #include "tfm_spm_hal.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 22 | #include "tfm_api.h" |
Kevin Peng | b20b032 | 2022-02-09 16:47:19 +0800 | [diff] [blame] | 23 | #include "tfm_arch.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 24 | #include "tfm_secure_api.h" |
| 25 | #include "tfm_memory_utils.h" |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 26 | #include "tfm_hal_defs.h" |
Kevin Peng | d399a1f | 2021-09-08 15:33:14 +0800 | [diff] [blame] | 27 | #include "tfm_hal_interrupt.h" |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 28 | #include "tfm_hal_isolation.h" |
Mingyang Sun | 7397b4f | 2020-06-17 15:07:45 +0800 | [diff] [blame] | 29 | #include "spm_ipc.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 30 | #include "tfm_peripherals_def.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 31 | #include "tfm_core_utils.h" |
Kevin Peng | 385fda8 | 2021-08-18 10:41:19 +0800 | [diff] [blame] | 32 | #include "tfm_nspm.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 33 | #include "tfm_rpc.h" |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 34 | #include "tfm_core_trustzone.h" |
Ken Liu | 24dffb2 | 2021-02-10 11:03:58 +0800 | [diff] [blame] | 35 | #include "lists.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 36 | #include "tfm_pools.h" |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 37 | #include "region.h" |
Mingyang Sun | 7397b4f | 2020-06-17 15:07:45 +0800 | [diff] [blame] | 38 | #include "psa_manifest/pid.h" |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 39 | #include "ffm/backend.h" |
Mingyang Sun | 00df235 | 2021-04-15 15:46:08 +0800 | [diff] [blame] | 40 | #include "load/partition_defs.h" |
| 41 | #include "load/service_defs.h" |
Ken Liu | 8668628 | 2021-04-27 11:11:15 +0800 | [diff] [blame] | 42 | #include "load/asset_defs.h" |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 43 | #include "load/spm_load_api.h" |
Sherry Zhnag | 482b88b | 2021-08-19 17:51:47 +0800 | [diff] [blame] | 44 | #include "tfm_nspm.h" |
Summer Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 45 | #if defined(CONFIG_TFM_PARTITION_META) |
| 46 | #include "tfm_hal_memory_symbols.h" |
| 47 | #endif |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 48 | |
Sherry Zhang | 86a71c6 | 2022-01-12 18:19:02 +0800 | [diff] [blame] | 49 | #if !(defined CONFIG_TFM_CONN_HANDLE_MAX_NUM) || (CONFIG_TFM_CONN_HANDLE_MAX_NUM == 0) |
| 50 | #error "CONFIG_TFM_CONN_HANDLE_MAX_NUM must be defined and not zero." |
| 51 | #endif |
| 52 | |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 53 | /* Partition and service runtime data list head/runtime data table */ |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 54 | static struct service_head_t services_listhead; |
Ken Liu | b3b2cb6 | 2021-05-22 00:39:28 +0800 | [diff] [blame] | 55 | struct service_t *stateless_services_ref_tbl[STATIC_HANDLE_NUM_LIMIT]; |
Summer Qin | d99509f | 2019-08-02 17:36:58 +0800 | [diff] [blame] | 56 | |
Summer Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 57 | #if defined(CONFIG_TFM_PARTITION_META) |
| 58 | /* Indicator point to the partition meta */ |
| 59 | static uintptr_t *partition_meta_indicator_pos = NULL; |
| 60 | #endif |
| 61 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 62 | /* Pools */ |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 63 | TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct conn_handle_t), |
Sherry Zhang | 86a71c6 | 2022-01-12 18:19:02 +0800 | [diff] [blame] | 64 | CONFIG_TFM_CONN_HANDLE_MAX_NUM); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 65 | |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 66 | extern uint32_t scheduler_lock; |
| 67 | |
Summer Qin | 373feb1 | 2020-03-27 15:35:33 +0800 | [diff] [blame] | 68 | /*********************** Connection handle conversion APIs *******************/ |
| 69 | |
Summer Qin | 373feb1 | 2020-03-27 15:35:33 +0800 | [diff] [blame] | 70 | #define CONVERSION_FACTOR_BITOFFSET 3 |
| 71 | #define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET) |
| 72 | /* Set 32 as the maximum */ |
| 73 | #define CONVERSION_FACTOR_VALUE_MAX 0x20 |
| 74 | |
| 75 | #if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX |
| 76 | #error "CONVERSION FACTOR OUT OF RANGE" |
| 77 | #endif |
| 78 | |
| 79 | static uint32_t loop_index; |
| 80 | |
| 81 | /* |
| 82 | * A handle instance psa_handle_t allocated inside SPM is actually a memory |
| 83 | * address among the handle pool. Return this handle to the client directly |
| 84 | * exposes information of secure memory address. In this case, converting the |
| 85 | * handle into another value does not represent the memory address to avoid |
| 86 | * exposing secure memory directly to clients. |
| 87 | * |
| 88 | * This function converts the handle instance into another value by scaling the |
| 89 | * handle in pool offset, the converted value is named as a user handle. |
| 90 | * |
| 91 | * The formula: |
| 92 | * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE + |
| 93 | * CLIENT_HANDLE_VALUE_MIN + loop_index |
| 94 | * where: |
| 95 | * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not |
| 96 | * exceed CONVERSION_FACTOR_VALUE_MAX. |
| 97 | * |
| 98 | * handle_instance in RANGE[POOL_START, POOL_END] |
| 99 | * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF] |
| 100 | * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1] |
| 101 | * |
| 102 | * note: |
| 103 | * loop_index is used to promise same handle instance is converted into |
| 104 | * different user handles in short time. |
| 105 | */ |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 106 | psa_handle_t tfm_spm_to_user_handle(struct conn_handle_t *handle_instance) |
Summer Qin | 373feb1 | 2020-03-27 15:35:33 +0800 | [diff] [blame] | 107 | { |
| 108 | psa_handle_t user_handle; |
| 109 | |
| 110 | loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE; |
| 111 | user_handle = (psa_handle_t)((((uintptr_t)handle_instance - |
| 112 | (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) + |
| 113 | CLIENT_HANDLE_VALUE_MIN + loop_index); |
| 114 | |
| 115 | return user_handle; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * This function converts a user handle into a corresponded handle instance. |
| 120 | * The converted value is validated before returning, an invalid handle instance |
| 121 | * is returned as NULL. |
| 122 | * |
| 123 | * The formula: |
| 124 | * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) / |
| 125 | * CONVERSION_FACTOR_VALUE) + POOL_START |
| 126 | * where: |
| 127 | * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not |
| 128 | * exceed CONVERSION_FACTOR_VALUE_MAX. |
| 129 | * |
| 130 | * handle_instance in RANGE[POOL_START, POOL_END] |
| 131 | * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF] |
| 132 | * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1] |
| 133 | */ |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 134 | struct conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle) |
Summer Qin | 373feb1 | 2020-03-27 15:35:33 +0800 | [diff] [blame] | 135 | { |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 136 | struct conn_handle_t *handle_instance; |
Summer Qin | 373feb1 | 2020-03-27 15:35:33 +0800 | [diff] [blame] | 137 | |
| 138 | if (user_handle == PSA_NULL_HANDLE) { |
| 139 | return NULL; |
| 140 | } |
| 141 | |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 142 | handle_instance = (struct conn_handle_t *)((((uintptr_t)user_handle - |
Summer Qin | 373feb1 | 2020-03-27 15:35:33 +0800 | [diff] [blame] | 143 | CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) + |
| 144 | (uintptr_t)conn_handle_pool); |
| 145 | |
| 146 | return handle_instance; |
| 147 | } |
| 148 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 149 | /* Service handle management functions */ |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 150 | struct conn_handle_t *tfm_spm_create_conn_handle(void) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 151 | { |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 152 | struct conn_handle_t *p_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 153 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 154 | /* Get buffer for handle list structure from handle pool */ |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 155 | p_handle = (struct conn_handle_t *)tfm_pool_alloc(conn_handle_pool); |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 156 | if (!p_handle) { |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 157 | return NULL; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 158 | } |
| 159 | |
Kevin Peng | 2cc2572 | 2021-11-18 11:46:02 +0800 | [diff] [blame] | 160 | spm_memset(p_handle, 0, sizeof(*p_handle)); |
| 161 | |
Shawn Shan | cc39fcb | 2019-11-13 15:38:16 +0800 | [diff] [blame] | 162 | p_handle->status = TFM_HANDLE_STATUS_IDLE; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 163 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 164 | return p_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 165 | } |
| 166 | |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 167 | int32_t tfm_spm_validate_conn_handle(const struct conn_handle_t *conn_handle) |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 168 | { |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 169 | /* Check the handle address is valid */ |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 170 | if (is_valid_chunk_data_in_pool(conn_handle_pool, |
| 171 | (uint8_t *)conn_handle) != true) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 172 | return SPM_ERROR_GENERIC; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 173 | } |
| 174 | |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 175 | return SPM_SUCCESS; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 176 | } |
| 177 | |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 178 | int32_t tfm_spm_free_conn_handle(struct conn_handle_t *conn_handle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 179 | { |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 180 | struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT; |
| 181 | |
Summer Qin | 630c76b | 2020-05-20 10:32:58 +0800 | [diff] [blame] | 182 | TFM_CORE_ASSERT(conn_handle != NULL); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 183 | |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 184 | CRITICAL_SECTION_ENTER(cs_assert); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 185 | /* Back handle buffer to pool */ |
Ken Liu | 66ca613 | 2021-02-24 08:49:51 +0800 | [diff] [blame] | 186 | tfm_pool_free(conn_handle_pool, conn_handle); |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 187 | CRITICAL_SECTION_LEAVE(cs_assert); |
| 188 | |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 189 | return SPM_SUCCESS; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 190 | } |
| 191 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 192 | /* Partition management functions */ |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 193 | |
Mingyang Sun | 4609ac7 | 2022-02-08 18:56:35 +0800 | [diff] [blame] | 194 | /* This API is only used in IPC backend. */ |
| 195 | #if CONFIG_TFM_SPM_BACKEND_IPC == 1 |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 196 | struct conn_handle_t *spm_get_handle_by_signal(struct partition_t *p_ptn, |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame] | 197 | psa_signal_t signal) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 198 | { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 199 | struct conn_handle_t *p_handle_iter; |
| 200 | struct conn_handle_t **pr_handle_iter, **last_found_handle_holder = NULL; |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 201 | struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT; |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame] | 202 | uint32_t nr_found_msgs = 0; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 203 | |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 204 | CRITICAL_SECTION_ENTER(cs_assert); |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame] | 205 | |
| 206 | /* Return the last found message which applies a FIFO mechanism. */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 207 | UNI_LIST_FOREACH_NODE_PNODE(pr_handle_iter, p_handle_iter, |
| 208 | p_ptn, p_handles) { |
| 209 | if (p_handle_iter->service->p_ldinf->signal == signal) { |
| 210 | last_found_handle_holder = pr_handle_iter; |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame] | 211 | nr_found_msgs++; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 212 | } |
| 213 | } |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 214 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 215 | if (last_found_handle_holder) { |
| 216 | p_handle_iter = *last_found_handle_holder; |
| 217 | UNI_LIST_REMOVE_NODE_BY_PNODE(last_found_handle_holder, p_handles); |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame] | 218 | |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame] | 219 | if (nr_found_msgs == 1) { |
| 220 | p_ptn->signals_asserted &= ~signal; |
| 221 | } |
| 222 | } |
| 223 | |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 224 | CRITICAL_SECTION_LEAVE(cs_assert); |
Mingyang Sun | 73056b6 | 2020-07-03 15:18:46 +0800 | [diff] [blame] | 225 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 226 | return p_handle_iter; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 227 | } |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 228 | #endif /* CONFIG_TFM_SPM_BACKEND_IPC == 1 */ |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 229 | |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 230 | struct service_t *tfm_spm_get_service_by_sid(uint32_t sid) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 231 | { |
Feder.Liang | aeb5a79 | 2021-08-10 16:12:56 +0800 | [diff] [blame] | 232 | struct service_t *p_prev, *p_curr; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 233 | |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame] | 234 | UNI_LIST_FOREACH_NODE_PREV(p_prev, p_curr, &services_listhead, next) { |
Feder.Liang | aeb5a79 | 2021-08-10 16:12:56 +0800 | [diff] [blame] | 235 | if (p_curr->p_ldinf->sid == sid) { |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame] | 236 | UNI_LIST_MOVE_AFTER(&services_listhead, p_prev, p_curr, next); |
Feder.Liang | aeb5a79 | 2021-08-10 16:12:56 +0800 | [diff] [blame] | 237 | return p_curr; |
Mingyang Sun | ef42f44 | 2021-06-11 15:07:58 +0800 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 241 | return NULL; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 242 | } |
| 243 | |
Kevin Peng | 613b417 | 2022-02-15 14:41:44 +0800 | [diff] [blame] | 244 | #if CONFIG_TFM_DOORBELL_API == 1 |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 245 | /** |
| 246 | * \brief Get the partition context by partition ID. |
| 247 | * |
| 248 | * \param[in] partition_id Partition identity |
| 249 | * |
| 250 | * \retval NULL Failed |
| 251 | * \retval "Not NULL" Target partition context pointer, |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 252 | * \ref partition_t structures |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 253 | */ |
Kevin Peng | eca45b9 | 2021-02-09 14:46:50 +0800 | [diff] [blame] | 254 | 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] | 255 | { |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 256 | struct partition_t *p_part; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 257 | |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame] | 258 | UNI_LIST_FOREACH(p_part, PARTITION_LIST_ADDR, next) { |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 259 | if (p_part->p_ldinf->pid == partition_id) { |
| 260 | return p_part; |
| 261 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 262 | } |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 263 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 264 | return NULL; |
| 265 | } |
Kevin Peng | 613b417 | 2022-02-15 14:41:44 +0800 | [diff] [blame] | 266 | #endif /* CONFIG_TFM_DOORBELL_API == 1 */ |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 267 | |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 268 | int32_t tfm_spm_check_client_version(struct service_t *service, |
Jaykumar Pitambarbhai Patel | 3a98602 | 2019-10-08 17:37:15 +0530 | [diff] [blame] | 269 | uint32_t version) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 270 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 271 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 272 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 273 | switch (SERVICE_GET_VERSION_POLICY(service->p_ldinf->flags)) { |
Ken Liu | b3b2cb6 | 2021-05-22 00:39:28 +0800 | [diff] [blame] | 274 | case SERVICE_VERSION_POLICY_RELAXED: |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 275 | if (version > service->p_ldinf->version) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 276 | return SPM_ERROR_VERSION; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 277 | } |
| 278 | break; |
Ken Liu | b3b2cb6 | 2021-05-22 00:39:28 +0800 | [diff] [blame] | 279 | case SERVICE_VERSION_POLICY_STRICT: |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 280 | if (version != service->p_ldinf->version) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 281 | return SPM_ERROR_VERSION; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 282 | } |
| 283 | break; |
| 284 | default: |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 285 | return SPM_ERROR_VERSION; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 286 | } |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 287 | return SPM_SUCCESS; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 288 | } |
| 289 | |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 290 | int32_t tfm_spm_check_authorization(uint32_t sid, |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 291 | struct service_t *service, |
Summer Qin | 618e8c3 | 2019-12-09 10:47:20 +0800 | [diff] [blame] | 292 | bool ns_caller) |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 293 | { |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 294 | struct partition_t *partition = NULL; |
Mingyang Sun | 2b35266 | 2021-04-21 11:35:43 +0800 | [diff] [blame] | 295 | uint32_t *dep; |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 296 | int32_t i; |
| 297 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 298 | TFM_CORE_ASSERT(service); |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 299 | |
| 300 | if (ns_caller) { |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 301 | if (!SERVICE_IS_NS_ACCESSIBLE(service->p_ldinf->flags)) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 302 | return SPM_ERROR_GENERIC; |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 303 | } |
| 304 | } else { |
Kevin Peng | cdf3ae2 | 2022-01-19 14:53:52 +0800 | [diff] [blame] | 305 | partition = GET_CURRENT_COMPONENT(); |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 306 | if (!partition) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 307 | tfm_core_panic(); |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 308 | } |
| 309 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 310 | dep = (uint32_t *)LOAD_INFO_DEPS(partition->p_ldinf); |
| 311 | for (i = 0; i < partition->p_ldinf->ndeps; i++) { |
Mingyang Sun | 2b35266 | 2021-04-21 11:35:43 +0800 | [diff] [blame] | 312 | if (dep[i] == sid) { |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 313 | break; |
| 314 | } |
| 315 | } |
| 316 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 317 | if (i == partition->p_ldinf->ndeps) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 318 | return SPM_ERROR_GENERIC; |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 319 | } |
| 320 | } |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 321 | return SPM_SUCCESS; |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 322 | } |
| 323 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 324 | /* Message functions */ |
Summer Qin | 401cf81 | 2022-04-02 09:46:34 +0800 | [diff] [blame^] | 325 | #if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1 |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 326 | struct conn_handle_t *spm_get_handle_by_client_handle(psa_handle_t handle, |
| 327 | int32_t client_id) |
| 328 | { |
| 329 | struct conn_handle_t *p_conn_handle = tfm_spm_to_handle_instance(handle); |
| 330 | |
| 331 | if (tfm_spm_validate_conn_handle(p_conn_handle) != SPM_SUCCESS) { |
| 332 | return NULL; |
| 333 | } |
| 334 | |
| 335 | /* Validate the caller id in the connection handle equals client_id. */ |
| 336 | if (p_conn_handle->msg.client_id != client_id) { |
| 337 | return NULL; |
| 338 | } |
| 339 | |
| 340 | return p_conn_handle; |
| 341 | } |
Summer Qin | 401cf81 | 2022-04-02 09:46:34 +0800 | [diff] [blame^] | 342 | #endif |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 343 | |
| 344 | struct conn_handle_t *spm_get_handle_by_msg_handle(psa_handle_t msg_handle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 345 | { |
| 346 | /* |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 347 | * The message handler passed by the caller is considered invalid in the |
| 348 | * following cases: |
| 349 | * 1. Not a valid message handle. (The address of a message is not the |
| 350 | * address of a possible handle from the pool |
| 351 | * 2. Handle not belongs to the caller partition (The handle is either |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 352 | * unused, or owned by another partition) |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 353 | * Check the conditions above |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 354 | */ |
Kevin Peng | eec41a8 | 2021-08-18 13:56:23 +0800 | [diff] [blame] | 355 | int32_t partition_id; |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 356 | struct conn_handle_t *p_conn_handle = |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 357 | tfm_spm_to_handle_instance(msg_handle); |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 358 | |
Kevin Peng | e61e705 | 2022-01-27 14:57:06 +0800 | [diff] [blame] | 359 | if (tfm_spm_validate_conn_handle(p_conn_handle) != SPM_SUCCESS) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 360 | return NULL; |
| 361 | } |
| 362 | |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 363 | /* Check that the running partition owns the message */ |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 364 | partition_id = tfm_spm_partition_get_running_partition_id(); |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 365 | if (partition_id != p_conn_handle->service->partition->p_ldinf->pid) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 366 | return NULL; |
| 367 | } |
| 368 | |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 369 | return p_conn_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 370 | } |
| 371 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 372 | void spm_fill_message(struct conn_handle_t *conn_handle, |
Mingyang Sun | 783a59b | 2021-04-20 15:52:18 +0800 | [diff] [blame] | 373 | struct service_t *service, |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 374 | psa_handle_t handle, |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 375 | int32_t type, int32_t client_id, |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 376 | psa_invec *invec, size_t in_len, |
| 377 | psa_outvec *outvec, size_t out_len, |
| 378 | psa_outvec *caller_outvec) |
| 379 | { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 380 | uint32_t i; |
| 381 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 382 | TFM_CORE_ASSERT(conn_handle); |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 383 | TFM_CORE_ASSERT(service); |
| 384 | TFM_CORE_ASSERT(!(invec == NULL && in_len != 0)); |
| 385 | TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0)); |
| 386 | TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC); |
| 387 | TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC); |
| 388 | TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 389 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 390 | /* Clear message buffer before using it */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 391 | spm_memset(&conn_handle->msg, 0, sizeof(psa_msg_t)); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 392 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 393 | THRD_SYNC_INIT(&conn_handle->ack_evnt); |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 394 | conn_handle->service = service; |
| 395 | conn_handle->p_client = GET_CURRENT_COMPONENT(); |
| 396 | conn_handle->caller_outvec = caller_outvec; |
| 397 | conn_handle->msg.client_id = client_id; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 398 | |
| 399 | /* Copy contents */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 400 | conn_handle->msg.type = type; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 401 | |
| 402 | for (i = 0; i < in_len; i++) { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 403 | conn_handle->msg.in_size[i] = invec[i].len; |
| 404 | conn_handle->invec[i].base = invec[i].base; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | for (i = 0; i < out_len; i++) { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 408 | conn_handle->msg.out_size[i] = outvec[i].len; |
| 409 | conn_handle->outvec[i].base = outvec[i].base; |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 410 | /* Out len is used to record the wrote number, set 0 here again */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 411 | conn_handle->outvec[i].len = 0; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 412 | } |
| 413 | |
Ken Liu | 505b170 | 2020-05-29 13:19:58 +0800 | [diff] [blame] | 414 | /* Use the user connect handle as the message handle */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 415 | conn_handle->msg.handle = handle; |
| 416 | conn_handle->msg.rhandle = conn_handle->rhandle; |
David Hu | 46603dd | 2019-12-11 18:05:16 +0800 | [diff] [blame] | 417 | |
| 418 | /* Set the private data of NSPE client caller in multi-core topology */ |
| 419 | if (TFM_CLIENT_ID_IS_NS(client_id)) { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 420 | tfm_rpc_set_caller_data(conn_handle, client_id); |
David Hu | 46603dd | 2019-12-11 18:05:16 +0800 | [diff] [blame] | 421 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 422 | } |
| 423 | |
Kevin Peng | eec41a8 | 2021-08-18 13:56:23 +0800 | [diff] [blame] | 424 | int32_t tfm_spm_partition_get_running_partition_id(void) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 425 | { |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 426 | struct partition_t *partition; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 427 | |
Kevin Peng | cdf3ae2 | 2022-01-19 14:53:52 +0800 | [diff] [blame] | 428 | partition = GET_CURRENT_COMPONENT(); |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 429 | if (partition && partition->p_ldinf) { |
| 430 | return partition->p_ldinf->pid; |
Kevin Peng | 79c2bda | 2020-07-24 16:31:12 +0800 | [diff] [blame] | 431 | } else { |
| 432 | return INVALID_PARTITION_ID; |
| 433 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 434 | } |
| 435 | |
Summer Qin | 43c185d | 2019-10-10 15:44:42 +0800 | [diff] [blame] | 436 | 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] | 437 | enum tfm_memory_access_e access, |
| 438 | uint32_t privileged) |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 439 | { |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 440 | enum tfm_hal_status_t err; |
| 441 | uint32_t attr = 0; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 442 | |
| 443 | /* If len is zero, this indicates an empty buffer and base is ignored */ |
| 444 | if (len == 0) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 445 | return SPM_SUCCESS; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | if (!buffer) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 449 | return SPM_ERROR_BAD_PARAMETERS; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | if ((uintptr_t)buffer > (UINTPTR_MAX - len)) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 453 | return SPM_ERROR_MEMORY_CHECK; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 454 | } |
| 455 | |
Summer Qin | 424d4db | 2019-03-25 14:09:51 +0800 | [diff] [blame] | 456 | if (access == TFM_MEMORY_ACCESS_RW) { |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 457 | attr |= (TFM_HAL_ACCESS_READABLE | TFM_HAL_ACCESS_WRITABLE); |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 458 | } else { |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 459 | attr |= TFM_HAL_ACCESS_READABLE; |
Summer Qin | 424d4db | 2019-03-25 14:09:51 +0800 | [diff] [blame] | 460 | } |
Mingyang Sun | d1ed673 | 2020-08-26 15:52:21 +0800 | [diff] [blame] | 461 | |
| 462 | if (privileged == TFM_PARTITION_UNPRIVILEGED_MODE) { |
| 463 | attr |= TFM_HAL_ACCESS_UNPRIVILEGED; |
| 464 | } else { |
| 465 | attr &= ~TFM_HAL_ACCESS_UNPRIVILEGED; |
| 466 | } |
| 467 | |
| 468 | if (ns_caller) { |
| 469 | attr |= TFM_HAL_ACCESS_NS; |
| 470 | } |
| 471 | |
| 472 | err = tfm_hal_memory_has_access((uintptr_t)buffer, len, attr); |
| 473 | |
| 474 | if (err == TFM_HAL_SUCCESS) { |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 475 | return SPM_SUCCESS; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 476 | } |
| 477 | |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 478 | return SPM_ERROR_MEMORY_CHECK; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 479 | } |
| 480 | |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 481 | bool tfm_spm_is_ns_caller(void) |
| 482 | { |
Kevin Peng | cdf3ae2 | 2022-01-19 14:53:52 +0800 | [diff] [blame] | 483 | struct partition_t *partition = GET_CURRENT_COMPONENT(); |
Ken Liu | 897e8f1 | 2022-02-10 03:21:17 +0100 | [diff] [blame] | 484 | |
| 485 | if (!partition) { |
| 486 | tfm_core_panic(); |
| 487 | } |
| 488 | |
| 489 | return (partition->p_ldinf->pid == TFM_SP_NON_SECURE_ID); |
Mingyang Sun | 22a3faf | 2021-07-09 15:32:47 +0800 | [diff] [blame] | 490 | } |
| 491 | |
Kevin Peng | 385fda8 | 2021-08-18 10:41:19 +0800 | [diff] [blame] | 492 | int32_t tfm_spm_get_client_id(bool ns_caller) |
| 493 | { |
| 494 | int32_t client_id; |
| 495 | |
| 496 | if (ns_caller) { |
| 497 | client_id = tfm_nspm_get_current_client_id(); |
| 498 | } else { |
| 499 | client_id = tfm_spm_partition_get_running_partition_id(); |
| 500 | } |
| 501 | |
| 502 | if (ns_caller != (client_id < 0)) { |
| 503 | /* NS client ID must be negative and Secure ID must >= 0 */ |
| 504 | tfm_core_panic(); |
| 505 | } |
| 506 | |
| 507 | return client_id; |
| 508 | } |
| 509 | |
Ken Liu | ce2692d | 2020-02-11 12:39:36 +0800 | [diff] [blame] | 510 | uint32_t tfm_spm_init(void) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 511 | { |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 512 | struct partition_t *partition; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 513 | const struct partition_load_info_t *p_pldi; |
| 514 | uint32_t service_setting = 0; |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 515 | |
David Hu | f07e97d | 2021-02-15 22:05:40 +0800 | [diff] [blame] | 516 | #ifdef TFM_FIH_PROFILE_ON |
| 517 | fih_int fih_rc = FIH_FAILURE; |
| 518 | #endif |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 519 | |
| 520 | tfm_pool_init(conn_handle_pool, |
| 521 | POOL_BUFFER_SIZE(conn_handle_pool), |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 522 | sizeof(struct conn_handle_t), |
Sherry Zhang | 86a71c6 | 2022-01-12 18:19:02 +0800 | [diff] [blame] | 523 | CONFIG_TFM_CONN_HANDLE_MAX_NUM); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 524 | |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame] | 525 | UNI_LISI_INIT_NODE(PARTITION_LIST_ADDR, next); |
| 526 | UNI_LISI_INIT_NODE(&services_listhead, next); |
Ken Liu | ea45b0d | 2021-05-22 17:41:25 +0800 | [diff] [blame] | 527 | |
Sherry Zhnag | 482b88b | 2021-08-19 17:51:47 +0800 | [diff] [blame] | 528 | /* Init the nonsecure context. */ |
Chris Brand | 56b4d0c | 2021-12-17 16:15:58 -0800 | [diff] [blame] | 529 | tfm_nspm_ctx_init(); |
Sherry Zhnag | 482b88b | 2021-08-19 17:51:47 +0800 | [diff] [blame] | 530 | |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 531 | while (1) { |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 532 | partition = load_a_partition_assuredly(PARTITION_LIST_ADDR); |
Shawn Shan | 2333106 | 2021-09-01 14:58:21 +0800 | [diff] [blame] | 533 | if (partition == NO_MORE_PARTITION) { |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame] | 534 | break; |
Kevin Peng | 79c2bda | 2020-07-24 16:31:12 +0800 | [diff] [blame] | 535 | } |
| 536 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 537 | p_pldi = partition->p_ldinf; |
Mingyang Sun | 2b35266 | 2021-04-21 11:35:43 +0800 | [diff] [blame] | 538 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 539 | if (p_pldi->nservices) { |
| 540 | service_setting = load_services_assuredly( |
| 541 | partition, |
| 542 | &services_listhead, |
| 543 | stateless_services_ref_tbl, |
| 544 | sizeof(stateless_services_ref_tbl)); |
Kevin Peng | 27e4227 | 2021-05-24 17:58:53 +0800 | [diff] [blame] | 545 | } |
| 546 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 547 | if (p_pldi->nirqs) { |
Kevin Peng | 27e4227 | 2021-05-24 17:58:53 +0800 | [diff] [blame] | 548 | load_irqs_assuredly(partition); |
| 549 | } |
| 550 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 551 | /* Bind the partition with platform. */ |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 552 | #if TFM_FIH_PROFILE_ON |
| 553 | FIH_CALL(tfm_hal_bind_boundaries, fih_rc, partition->p_ldinf, |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 554 | &partition->p_boundaries); |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 555 | if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) { |
| 556 | tfm_core_panic(); |
Mingyang Sun | 61f8fbc | 2021-06-04 17:49:56 +0800 | [diff] [blame] | 557 | } |
Ken Liu | 8668628 | 2021-04-27 11:11:15 +0800 | [diff] [blame] | 558 | #else /* TFM_FIH_PROFILE_ON */ |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 559 | if (tfm_hal_bind_boundaries(partition->p_ldinf, |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 560 | &partition->p_boundaries) |
| 561 | != TFM_HAL_SUCCESS) { |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 562 | tfm_core_panic(); |
Mate Toth-Pal | 8ac98a7 | 2019-11-21 17:30:10 +0100 | [diff] [blame] | 563 | } |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 564 | #endif /* TFM_FIH_PROFILE_ON */ |
Mate Toth-Pal | 8ac98a7 | 2019-11-21 17:30:10 +0100 | [diff] [blame] | 565 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 566 | backend_instance.comp_init_assuredly(partition, service_setting); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 567 | } |
| 568 | |
Summer Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 569 | #if defined(CONFIG_TFM_PARTITION_META) |
| 570 | partition_meta_indicator_pos = (uintptr_t *)hal_mem_sp_meta_start; |
| 571 | #endif |
| 572 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 573 | return backend_instance.system_run(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 574 | } |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 575 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 576 | uint64_t do_schedule(void) |
| 577 | { |
Ken Liu | 086b32c | 2021-12-27 14:44:52 +0800 | [diff] [blame] | 578 | AAPCS_DUAL_U32_T ctx_ctrls; |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 579 | struct partition_t *p_part_curr, *p_part_next; |
Kevin Peng | b20b032 | 2022-02-09 16:47:19 +0800 | [diff] [blame] | 580 | struct context_ctrl_t *p_curr_ctx; |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 581 | struct thread_t *pth_next = thrd_next(); |
Kevin Peng | ca59ec0 | 2021-12-09 14:35:50 +0800 | [diff] [blame] | 582 | struct critical_section_t cs = CRITICAL_SECTION_STATIC_INIT; |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 583 | |
Kevin Peng | b20b032 | 2022-02-09 16:47:19 +0800 | [diff] [blame] | 584 | p_curr_ctx = (struct context_ctrl_t *)(CURRENT_THREAD->p_context_ctrl); |
Ken Liu | 086b32c | 2021-12-27 14:44:52 +0800 | [diff] [blame] | 585 | |
Kevin Peng | b20b032 | 2022-02-09 16:47:19 +0800 | [diff] [blame] | 586 | AAPCS_DUAL_U32_SET(ctx_ctrls, (uint32_t)p_curr_ctx, (uint32_t)p_curr_ctx); |
| 587 | |
| 588 | p_part_curr = GET_CURRENT_COMPONENT(); |
Mingyang Sun | 4f08f4d | 2021-09-10 17:41:28 +0800 | [diff] [blame] | 589 | p_part_next = GET_THRD_OWNER(pth_next); |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 590 | |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 591 | if (scheduler_lock != SCHEDULER_LOCKED && pth_next != NULL && |
| 592 | p_part_curr != p_part_next) { |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 593 | /* Check if there is enough room on stack to save more context */ |
Kevin Peng | b20b032 | 2022-02-09 16:47:19 +0800 | [diff] [blame] | 594 | if ((p_curr_ctx->sp_limit + |
| 595 | sizeof(struct tfm_additional_context_t)) > __get_PSP()) { |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 596 | tfm_core_panic(); |
| 597 | } |
Mate Toth-Pal | c430b99 | 2019-05-09 21:01:14 +0200 | [diff] [blame] | 598 | |
Kevin Peng | ca59ec0 | 2021-12-09 14:35:50 +0800 | [diff] [blame] | 599 | CRITICAL_SECTION_ENTER(cs); |
Ken Liu | ce58bfc | 2021-05-12 17:54:48 +0800 | [diff] [blame] | 600 | /* |
| 601 | * If required, let the platform update boundary based on its |
| 602 | * implementation. Change privilege, MPU or other configurations. |
| 603 | */ |
| 604 | if (p_part_curr->p_boundaries != p_part_next->p_boundaries) { |
| 605 | if (tfm_hal_update_boundaries(p_part_next->p_ldinf, |
| 606 | p_part_next->p_boundaries) |
| 607 | != TFM_HAL_SUCCESS) { |
| 608 | tfm_core_panic(); |
| 609 | } |
| 610 | } |
Feder Liang | 42f5b56 | 2021-09-10 17:38:36 +0800 | [diff] [blame] | 611 | ARCH_FLUSH_FP_CONTEXT(); |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 612 | |
Ken Liu | 086b32c | 2021-12-27 14:44:52 +0800 | [diff] [blame] | 613 | AAPCS_DUAL_U32_SET_A1(ctx_ctrls, (uint32_t)pth_next->p_context_ctrl); |
| 614 | |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 615 | CURRENT_THREAD = pth_next; |
Kevin Peng | ca59ec0 | 2021-12-09 14:35:50 +0800 | [diff] [blame] | 616 | CRITICAL_SECTION_LEAVE(cs); |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 617 | } |
David Hu | fb38d56 | 2019-09-23 15:58:34 +0800 | [diff] [blame] | 618 | |
Summer Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 619 | #if defined(CONFIG_TFM_PARTITION_META) |
| 620 | /* Update meta indicator */ |
| 621 | if (partition_meta_indicator_pos && (p_part_next->p_metadata)) { |
| 622 | *partition_meta_indicator_pos = (uintptr_t)(p_part_next->p_metadata); |
| 623 | } |
| 624 | #endif |
| 625 | |
Ken Liu | 086b32c | 2021-12-27 14:44:52 +0800 | [diff] [blame] | 626 | return AAPCS_DUAL_U32_AS_U64(ctx_ctrls); |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 627 | } |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 628 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 629 | void update_caller_outvec_len(struct conn_handle_t *handle) |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 630 | { |
| 631 | uint32_t i; |
| 632 | |
| 633 | /* |
| 634 | * FixeMe: abstract these part into dedicated functions to avoid |
| 635 | * accessing thread context in psa layer |
| 636 | */ |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 637 | /* |
| 638 | * If it is a NS request via RPC, the owner of this message is not set. |
| 639 | * Or if it is a SFN message, it does not have owner thread state either. |
| 640 | */ |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 641 | if ((!is_tfm_rpc_msg(handle)) && (handle->sfn_magic != TFM_MSG_MAGIC_SFN)) { |
| 642 | TFM_CORE_ASSERT(handle->ack_evnt.owner->state == THRD_STATE_BLOCK); |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | for (i = 0; i < PSA_MAX_IOVEC; i++) { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 646 | if (handle->msg.out_size[i] == 0) { |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 647 | continue; |
| 648 | } |
| 649 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 650 | TFM_CORE_ASSERT( |
| 651 | handle->caller_outvec[i].base == handle->outvec[i].base); |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 652 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 653 | handle->caller_outvec[i].len = handle->outvec[i].len; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 657 | void spm_assert_signal(void *p_pt, psa_signal_t signal) |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 658 | { |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 659 | struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT; |
Ken Liu | 5d73c87 | 2021-08-19 19:23:17 +0800 | [diff] [blame] | 660 | struct partition_t *partition = (struct partition_t *)p_pt; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 661 | |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 662 | if (!partition) { |
| 663 | tfm_core_panic(); |
| 664 | } |
| 665 | |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 666 | CRITICAL_SECTION_ENTER(cs_assert); |
| 667 | |
Mingyang Sun | af22ffa | 2020-07-09 17:48:37 +0800 | [diff] [blame] | 668 | partition->signals_asserted |= signal; |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 669 | |
Kevin Peng | 8dac610 | 2021-03-09 16:44:00 +0800 | [diff] [blame] | 670 | if (partition->signals_waiting & signal) { |
Mingyang Sun | 09328bd | 2022-03-25 11:55:13 +0800 | [diff] [blame] | 671 | backend_instance.wake_up(partition); |
Kevin Peng | 8dac610 | 2021-03-09 16:44:00 +0800 | [diff] [blame] | 672 | } |
Ken Liu | 92ede9f | 2021-10-20 09:35:00 +0800 | [diff] [blame] | 673 | |
| 674 | CRITICAL_SECTION_LEAVE(cs_assert); |
Mingyang Sun | d44522a | 2020-01-16 16:48:37 +0800 | [diff] [blame] | 675 | } |