Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018-2019, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 7 | |
| 8 | /* All the APIs defined in this file are used for IPC model. */ |
| 9 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 10 | #include <inttypes.h> |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 11 | #include <limits.h> |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 12 | #include <stdbool.h> |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 13 | #include <stdlib.h> |
Jamie Fox | cc31d40 | 2019-01-28 17:13:52 +0000 | [diff] [blame] | 14 | #include "psa/client.h" |
| 15 | #include "psa/service.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 16 | #include "tfm_utils.h" |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 17 | #include "tfm_spm_hal.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 18 | #include "spm_api.h" |
| 19 | #include "spm_db.h" |
David Hu | 326f397 | 2019-08-06 14:16:51 +0800 | [diff] [blame] | 20 | #include "tfm_core_mem_check.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 21 | #include "tfm_internal_defines.h" |
| 22 | #include "tfm_wait.h" |
| 23 | #include "tfm_message_queue.h" |
| 24 | #include "tfm_list.h" |
| 25 | #include "tfm_pools.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 26 | #include "tfm_thread.h" |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 27 | #include "region_defs.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 28 | #include "tfm_nspm.h" |
Edison Ai | 807fedb | 2019-03-07 11:22:03 +0800 | [diff] [blame] | 29 | #include "tfm_memory_utils.h" |
Mingyang Sun | 94b1b41 | 2019-09-20 15:11:14 +0800 | [diff] [blame] | 30 | #include "tfm_core_utils.h" |
David Hu | fb38d56 | 2019-09-23 15:58:34 +0800 | [diff] [blame] | 31 | #include "tfm_rpc.h" |
Shawn Shan | 9b0e0c7 | 2019-10-22 13:43:07 +0800 | [diff] [blame] | 32 | #include "tfm_irq_list.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 33 | |
Summer Qin | d99509f | 2019-08-02 17:36:58 +0800 | [diff] [blame] | 34 | #include "secure_fw/services/tfm_service_list.inc" |
| 35 | |
| 36 | /* Extern service variable */ |
| 37 | extern struct tfm_spm_service_t service[]; |
Summer Qin | e578c5b | 2019-08-16 16:42:16 +0800 | [diff] [blame] | 38 | extern const struct tfm_spm_service_db_t service_db[]; |
Summer Qin | d99509f | 2019-08-02 17:36:58 +0800 | [diff] [blame] | 39 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 40 | /* Extern SPM variable */ |
| 41 | extern struct spm_partition_db_t g_spm_partition_db; |
| 42 | |
Mate Toth-Pal | c430b99 | 2019-05-09 21:01:14 +0200 | [diff] [blame] | 43 | /* Extern secure lock variable */ |
| 44 | extern int32_t tfm_secure_lock; |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 45 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 46 | /* Pools */ |
| 47 | TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t), |
| 48 | TFM_CONN_HANDLE_MAX_NUM); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 49 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 50 | /********************** SPM functions for handler mode ***********************/ |
| 51 | |
| 52 | /* Service handle management functions */ |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 53 | psa_handle_t tfm_spm_create_conn_handle(struct tfm_spm_service_t *service, |
| 54 | int32_t client_id) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 55 | { |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 56 | struct tfm_conn_handle_t *p_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 57 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 58 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 59 | |
| 60 | /* Get buffer for handle list structure from handle pool */ |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 61 | p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool); |
| 62 | if (!p_handle) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 63 | return PSA_NULL_HANDLE; |
| 64 | } |
| 65 | |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 66 | p_handle->service = service; |
Shawn Shan | cc39fcb | 2019-11-13 15:38:16 +0800 | [diff] [blame] | 67 | p_handle->status = TFM_HANDLE_STATUS_IDLE; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 68 | p_handle->client_id = client_id; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 69 | |
| 70 | /* Add handle node to list for next psa functions */ |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 71 | tfm_list_add_tail(&service->handle_list, &p_handle->list); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 72 | |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 73 | return (psa_handle_t)p_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 74 | } |
| 75 | |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 76 | int32_t tfm_spm_validate_conn_handle(psa_handle_t conn_handle, |
| 77 | int32_t client_id) |
| 78 | { |
| 79 | /* Check the handle address is validated */ |
| 80 | if (is_valid_chunk_data_in_pool(conn_handle_pool, |
| 81 | (uint8_t *)conn_handle) != true) { |
| 82 | return IPC_ERROR_GENERIC; |
| 83 | } |
| 84 | |
| 85 | /* Check the handle caller is correct */ |
| 86 | if (((struct tfm_conn_handle_t *)conn_handle)->client_id != client_id) { |
| 87 | return IPC_ERROR_GENERIC; |
| 88 | } |
| 89 | |
| 90 | return IPC_SUCCESS; |
| 91 | } |
| 92 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 93 | static struct tfm_conn_handle_t * |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 94 | tfm_spm_find_conn_handle_node(struct tfm_spm_service_t *service, |
| 95 | psa_handle_t conn_handle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 96 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 97 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 98 | |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 99 | return (struct tfm_conn_handle_t *)conn_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | int32_t tfm_spm_free_conn_handle(struct tfm_spm_service_t *service, |
| 103 | psa_handle_t conn_handle) |
| 104 | { |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 105 | struct tfm_conn_handle_t *p_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 106 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 107 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 108 | |
| 109 | /* There are many handles for each RoT Service */ |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 110 | p_handle = tfm_spm_find_conn_handle_node(service, conn_handle); |
| 111 | if (!p_handle) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 112 | tfm_core_panic(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 113 | } |
| 114 | |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 115 | /* Clear magic as the handler is not used anymore */ |
| 116 | p_handle->internal_msg.magic = 0; |
| 117 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 118 | /* Remove node from handle list */ |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 119 | tfm_list_del_node(&p_handle->list); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 120 | |
| 121 | /* Back handle buffer to pool */ |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 122 | tfm_pool_free(p_handle); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 123 | return IPC_SUCCESS; |
| 124 | } |
| 125 | |
| 126 | int32_t tfm_spm_set_rhandle(struct tfm_spm_service_t *service, |
| 127 | psa_handle_t conn_handle, |
| 128 | void *rhandle) |
| 129 | { |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 130 | struct tfm_conn_handle_t *p_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 131 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 132 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 133 | /* Set reverse handle value only be allowed for a connected handle */ |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 134 | TFM_CORE_ASSERT(conn_handle != PSA_NULL_HANDLE); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 135 | |
| 136 | /* There are many handles for each RoT Service */ |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 137 | p_handle = tfm_spm_find_conn_handle_node(service, conn_handle); |
| 138 | if (!p_handle) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 139 | tfm_core_panic(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 140 | } |
| 141 | |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 142 | p_handle->rhandle = rhandle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 143 | return IPC_SUCCESS; |
| 144 | } |
| 145 | |
| 146 | void *tfm_spm_get_rhandle(struct tfm_spm_service_t *service, |
| 147 | psa_handle_t conn_handle) |
| 148 | { |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 149 | struct tfm_conn_handle_t *p_handle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 150 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 151 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 152 | /* Get reverse handle value only be allowed for a connected handle */ |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 153 | TFM_CORE_ASSERT(conn_handle != PSA_NULL_HANDLE); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 154 | |
| 155 | /* There are many handles for each RoT Service */ |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 156 | p_handle = tfm_spm_find_conn_handle_node(service, conn_handle); |
| 157 | if (!p_handle) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 158 | tfm_core_panic(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 159 | } |
| 160 | |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 161 | return p_handle->rhandle; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* Partition management functions */ |
| 165 | struct tfm_spm_service_t * |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 166 | tfm_spm_get_service_by_signal(struct spm_partition_desc_t *partition, |
| 167 | psa_signal_t signal) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 168 | { |
| 169 | struct tfm_list_node_t *node, *head; |
| 170 | struct tfm_spm_service_t *service; |
| 171 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 172 | TFM_CORE_ASSERT(partition); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 173 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 174 | if (tfm_list_is_empty(&partition->runtime_data.service_list)) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 175 | tfm_core_panic(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 176 | } |
| 177 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 178 | head = &partition->runtime_data.service_list; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 179 | TFM_LIST_FOR_EACH(node, head) { |
| 180 | service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t, list); |
Summer Qin | e578c5b | 2019-08-16 16:42:16 +0800 | [diff] [blame] | 181 | if (service->service_db->signal == signal) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 182 | return service; |
| 183 | } |
| 184 | } |
| 185 | return NULL; |
| 186 | } |
| 187 | |
| 188 | struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid) |
| 189 | { |
| 190 | uint32_t i; |
| 191 | struct tfm_list_node_t *node, *head; |
| 192 | struct tfm_spm_service_t *service; |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 193 | struct spm_partition_desc_t *partition; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 194 | |
Mate Toth-Pal | 3ad2e3e | 2019-07-11 21:43:37 +0200 | [diff] [blame] | 195 | for (i = 0; i < g_spm_partition_db.partition_count; i++) { |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 196 | partition = &g_spm_partition_db.partitions[i]; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 197 | /* Skip partition without IPC flag */ |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 198 | if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 199 | continue; |
| 200 | } |
| 201 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 202 | if (tfm_list_is_empty(&partition->runtime_data.service_list)) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 203 | continue; |
| 204 | } |
| 205 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 206 | head = &partition->runtime_data.service_list; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 207 | TFM_LIST_FOR_EACH(node, head) { |
| 208 | service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t, |
| 209 | list); |
Summer Qin | e578c5b | 2019-08-16 16:42:16 +0800 | [diff] [blame] | 210 | if (service->service_db->sid == sid) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 211 | return service; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | return NULL; |
| 216 | } |
| 217 | |
| 218 | struct tfm_spm_service_t * |
| 219 | tfm_spm_get_service_by_handle(psa_handle_t conn_handle) |
| 220 | { |
Edison Ai | 9cc2624 | 2019-08-06 11:28:04 +0800 | [diff] [blame] | 221 | return ((struct tfm_conn_handle_t *)conn_handle)->service; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 222 | } |
| 223 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 224 | struct spm_partition_desc_t *tfm_spm_get_partition_by_id(int32_t partition_id) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 225 | { |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 226 | uint32_t idx = get_partition_idx(partition_id); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 227 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 228 | if (idx != SPM_INVALID_PARTITION_IDX) { |
| 229 | return &(g_spm_partition_db.partitions[idx]); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 230 | } |
| 231 | return NULL; |
| 232 | } |
| 233 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 234 | struct spm_partition_desc_t *tfm_spm_get_running_partition(void) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 235 | { |
| 236 | uint32_t spid; |
| 237 | |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 238 | spid = tfm_spm_partition_get_running_partition_id(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 239 | |
| 240 | return tfm_spm_get_partition_by_id(spid); |
| 241 | } |
| 242 | |
| 243 | int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service, |
Jaykumar Pitambarbhai Patel | 3a98602 | 2019-10-08 17:37:15 +0530 | [diff] [blame] | 244 | uint32_t version) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 245 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 246 | TFM_CORE_ASSERT(service); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 247 | |
Jaykumar Pitambarbhai Patel | 3a98602 | 2019-10-08 17:37:15 +0530 | [diff] [blame] | 248 | switch (service->service_db->version_policy) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 249 | case TFM_VERSION_POLICY_RELAXED: |
Jaykumar Pitambarbhai Patel | 3a98602 | 2019-10-08 17:37:15 +0530 | [diff] [blame] | 250 | if (version > service->service_db->version) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 251 | return IPC_ERROR_VERSION; |
| 252 | } |
| 253 | break; |
| 254 | case TFM_VERSION_POLICY_STRICT: |
Jaykumar Pitambarbhai Patel | 3a98602 | 2019-10-08 17:37:15 +0530 | [diff] [blame] | 255 | if (version != service->service_db->version) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 256 | return IPC_ERROR_VERSION; |
| 257 | } |
| 258 | break; |
| 259 | default: |
| 260 | return IPC_ERROR_VERSION; |
| 261 | } |
| 262 | return IPC_SUCCESS; |
| 263 | } |
| 264 | |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 265 | int32_t tfm_spm_check_authorization(uint32_t sid, |
| 266 | struct tfm_spm_service_t *service, |
Summer Qin | 618e8c3 | 2019-12-09 10:47:20 +0800 | [diff] [blame] | 267 | bool ns_caller) |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 268 | { |
| 269 | struct spm_partition_desc_t *partition = NULL; |
| 270 | int32_t i; |
| 271 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 272 | TFM_CORE_ASSERT(service); |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 273 | |
| 274 | if (ns_caller) { |
| 275 | if (!service->service_db->non_secure_client) { |
| 276 | return IPC_ERROR_GENERIC; |
| 277 | } |
| 278 | } else { |
| 279 | partition = tfm_spm_get_running_partition(); |
| 280 | if (!partition) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 281 | tfm_core_panic(); |
Edison Ai | e728fbf | 2019-11-13 09:37:12 +0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | for (i = 0; i < partition->static_data->dependencies_num; i++) { |
| 285 | if (partition->static_data->p_dependencies[i] == sid) { |
| 286 | break; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | if (i == partition->static_data->dependencies_num) { |
| 291 | return IPC_ERROR_GENERIC; |
| 292 | } |
| 293 | } |
| 294 | return IPC_SUCCESS; |
| 295 | } |
| 296 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 297 | /* Message functions */ |
| 298 | struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle) |
| 299 | { |
| 300 | /* |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 301 | * The message handler passed by the caller is considered invalid in the |
| 302 | * following cases: |
| 303 | * 1. Not a valid message handle. (The address of a message is not the |
| 304 | * address of a possible handle from the pool |
| 305 | * 2. Handle not belongs to the caller partition (The handle is either |
| 306 | * unused, or owned by anither partition) |
| 307 | * Check the conditions above |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 308 | */ |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 309 | struct tfm_conn_handle_t *connection_handle_address; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 310 | struct tfm_msg_body_t *msg; |
| 311 | uint32_t partition_id; |
| 312 | |
| 313 | msg = (struct tfm_msg_body_t *)msg_handle; |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 314 | |
| 315 | connection_handle_address = |
| 316 | TFM_GET_CONTAINER_PTR(msg, struct tfm_conn_handle_t, internal_msg); |
| 317 | |
| 318 | if (is_valid_chunk_data_in_pool( |
| 319 | conn_handle_pool, (uint8_t *)connection_handle_address) != 1) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 320 | return NULL; |
| 321 | } |
| 322 | |
| 323 | /* |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 324 | * Check that the magic number is correct. This proves that the message |
| 325 | * structure contains an active message. |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 326 | */ |
| 327 | if (msg->magic != TFM_MSG_MAGIC) { |
| 328 | return NULL; |
| 329 | } |
| 330 | |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 331 | /* Check that the running partition owns the message */ |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 332 | partition_id = tfm_spm_partition_get_running_partition_id(); |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 333 | if (partition_id != msg->service->partition->static_data->partition_id) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 334 | return NULL; |
| 335 | } |
| 336 | |
Mate Toth-Pal | a4b5d24 | 2019-09-23 09:14:47 +0200 | [diff] [blame] | 337 | /* |
| 338 | * FixMe: For condition 1 it should be checked whether the message belongs |
| 339 | * to the service. Skipping this check isn't a security risk as even if the |
| 340 | * message belongs to another service, the handle belongs to the calling |
| 341 | * partition. |
| 342 | */ |
| 343 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 344 | return msg; |
| 345 | } |
| 346 | |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 347 | struct tfm_msg_body_t * |
| 348 | tfm_spm_get_msg_buffer_from_conn_handle(psa_handle_t conn_handle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 349 | { |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 350 | TFM_CORE_ASSERT(conn_handle != PSA_NULL_HANDLE); |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 351 | |
| 352 | return &(((struct tfm_conn_handle_t *)conn_handle)->internal_msg); |
| 353 | } |
| 354 | |
| 355 | void tfm_spm_fill_msg(struct tfm_msg_body_t *msg, |
| 356 | struct tfm_spm_service_t *service, |
| 357 | psa_handle_t handle, |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 358 | int32_t type, int32_t client_id, |
Edison Ai | 9711582 | 2019-08-01 14:22:19 +0800 | [diff] [blame] | 359 | psa_invec *invec, size_t in_len, |
| 360 | psa_outvec *outvec, size_t out_len, |
| 361 | psa_outvec *caller_outvec) |
| 362 | { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 363 | uint32_t i; |
| 364 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 365 | TFM_CORE_ASSERT(msg); |
| 366 | TFM_CORE_ASSERT(service); |
| 367 | TFM_CORE_ASSERT(!(invec == NULL && in_len != 0)); |
| 368 | TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0)); |
| 369 | TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC); |
| 370 | TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC); |
| 371 | TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 372 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 373 | /* Clear message buffer before using it */ |
Mingyang Sun | 94b1b41 | 2019-09-20 15:11:14 +0800 | [diff] [blame] | 374 | tfm_core_util_memset(msg, 0, sizeof(struct tfm_msg_body_t)); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 375 | |
Ken Liu | 35f8939 | 2019-03-14 14:51:05 +0800 | [diff] [blame] | 376 | tfm_event_init(&msg->ack_evnt); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 377 | msg->magic = TFM_MSG_MAGIC; |
| 378 | msg->service = service; |
| 379 | msg->handle = handle; |
| 380 | msg->caller_outvec = caller_outvec; |
Summer Qin | 1ce712a | 2019-10-14 18:04:05 +0800 | [diff] [blame] | 381 | msg->msg.client_id = client_id; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 382 | |
| 383 | /* Copy contents */ |
| 384 | msg->msg.type = type; |
| 385 | |
| 386 | for (i = 0; i < in_len; i++) { |
| 387 | msg->msg.in_size[i] = invec[i].len; |
| 388 | msg->invec[i].base = invec[i].base; |
| 389 | } |
| 390 | |
| 391 | for (i = 0; i < out_len; i++) { |
| 392 | msg->msg.out_size[i] = outvec[i].len; |
| 393 | msg->outvec[i].base = outvec[i].base; |
| 394 | /* Out len is used to record the writed number, set 0 here again */ |
| 395 | msg->outvec[i].len = 0; |
| 396 | } |
| 397 | |
| 398 | /* Use message address as handle */ |
| 399 | msg->msg.handle = (psa_handle_t)msg; |
| 400 | |
| 401 | /* For connected handle, set rhandle to every message */ |
| 402 | if (handle != PSA_NULL_HANDLE) { |
| 403 | msg->msg.rhandle = tfm_spm_get_rhandle(service, handle); |
| 404 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | int32_t tfm_spm_send_event(struct tfm_spm_service_t *service, |
| 408 | struct tfm_msg_body_t *msg) |
| 409 | { |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 410 | struct spm_partition_runtime_data_t *p_runtime_data = |
| 411 | &service->partition->runtime_data; |
| 412 | |
Ken Liu | f250b8b | 2019-12-27 16:31:24 +0800 | [diff] [blame] | 413 | TFM_CORE_ASSERT(service); |
| 414 | TFM_CORE_ASSERT(msg); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 415 | |
| 416 | /* Enqueue message to service message queue */ |
| 417 | if (tfm_msg_enqueue(&service->msg_queue, msg) != IPC_SUCCESS) { |
| 418 | return IPC_ERROR_GENERIC; |
| 419 | } |
| 420 | |
| 421 | /* Messages put. Update signals */ |
Summer Qin | e578c5b | 2019-08-16 16:42:16 +0800 | [diff] [blame] | 422 | p_runtime_data->signals |= service->service_db->signal; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 423 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 424 | tfm_event_wake(&p_runtime_data->signal_evnt, (p_runtime_data->signals & |
| 425 | p_runtime_data->signal_mask)); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 426 | |
David Hu | fb38d56 | 2019-09-23 15:58:34 +0800 | [diff] [blame] | 427 | /* |
| 428 | * If it is a NS request via RPC, it is unnecessary to block current |
| 429 | * thread. |
| 430 | */ |
| 431 | if (!is_tfm_rpc_msg(msg)) { |
| 432 | tfm_event_wait(&msg->ack_evnt); |
| 433 | } |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 434 | |
| 435 | return IPC_SUCCESS; |
| 436 | } |
| 437 | |
Edison Ai | 7aff9e8 | 2019-07-11 14:56:46 +0800 | [diff] [blame] | 438 | uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx) |
| 439 | { |
| 440 | return g_spm_partition_db.partitions[partition_idx]. |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 441 | memory_data->stack_bottom; |
Edison Ai | 7aff9e8 | 2019-07-11 14:56:46 +0800 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx) |
| 445 | { |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 446 | return g_spm_partition_db.partitions[partition_idx].memory_data->stack_top; |
Edison Ai | 7aff9e8 | 2019-07-11 14:56:46 +0800 | [diff] [blame] | 447 | } |
| 448 | |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 449 | uint32_t tfm_spm_partition_get_running_partition_id(void) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 450 | { |
| 451 | struct tfm_thrd_ctx *pth = tfm_thrd_curr_thread(); |
| 452 | struct spm_partition_desc_t *partition; |
Summer Qin | b5da9cc | 2019-08-26 15:19:45 +0800 | [diff] [blame] | 453 | struct spm_partition_runtime_data_t *r_data; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 454 | |
Summer Qin | b5da9cc | 2019-08-26 15:19:45 +0800 | [diff] [blame] | 455 | r_data = TFM_GET_CONTAINER_PTR(pth, struct spm_partition_runtime_data_t, |
| 456 | sp_thrd); |
| 457 | partition = TFM_GET_CONTAINER_PTR(r_data, struct spm_partition_desc_t, |
| 458 | runtime_data); |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 459 | return partition->static_data->partition_id; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | static struct tfm_thrd_ctx * |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 463 | tfm_spm_partition_get_thread_info(uint32_t partition_idx) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 464 | { |
Summer Qin | b5da9cc | 2019-08-26 15:19:45 +0800 | [diff] [blame] | 465 | return &g_spm_partition_db.partitions[partition_idx].runtime_data.sp_thrd; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 466 | } |
| 467 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 468 | static tfm_thrd_func_t |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 469 | tfm_spm_partition_get_init_func(uint32_t partition_idx) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 470 | { |
| 471 | return (tfm_thrd_func_t)(g_spm_partition_db.partitions[partition_idx]. |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 472 | static_data->partition_init); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 473 | } |
| 474 | |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 475 | static uint32_t tfm_spm_partition_get_priority(uint32_t partition_idx) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 476 | { |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 477 | return g_spm_partition_db.partitions[partition_idx].static_data-> |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 478 | partition_priority; |
| 479 | } |
| 480 | |
Summer Qin | 43c185d | 2019-10-10 15:44:42 +0800 | [diff] [blame] | 481 | 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] | 482 | enum tfm_memory_access_e access, |
| 483 | uint32_t privileged) |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 484 | { |
Hugues de Valon | 9957856 | 2019-06-18 16:08:51 +0100 | [diff] [blame] | 485 | enum tfm_status_e err; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 486 | |
| 487 | /* If len is zero, this indicates an empty buffer and base is ignored */ |
| 488 | if (len == 0) { |
| 489 | return IPC_SUCCESS; |
| 490 | } |
| 491 | |
| 492 | if (!buffer) { |
| 493 | return IPC_ERROR_BAD_PARAMETERS; |
| 494 | } |
| 495 | |
| 496 | if ((uintptr_t)buffer > (UINTPTR_MAX - len)) { |
| 497 | return IPC_ERROR_MEMORY_CHECK; |
| 498 | } |
| 499 | |
Summer Qin | 424d4db | 2019-03-25 14:09:51 +0800 | [diff] [blame] | 500 | if (access == TFM_MEMORY_ACCESS_RW) { |
Summer Qin | eb537e5 | 2019-03-29 09:57:10 +0800 | [diff] [blame] | 501 | err = tfm_core_has_write_access_to_region(buffer, len, ns_caller, |
| 502 | privileged); |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 503 | } else { |
Summer Qin | eb537e5 | 2019-03-29 09:57:10 +0800 | [diff] [blame] | 504 | err = tfm_core_has_read_access_to_region(buffer, len, ns_caller, |
| 505 | privileged); |
Summer Qin | 424d4db | 2019-03-25 14:09:51 +0800 | [diff] [blame] | 506 | } |
Summer Qin | 0fc3f59 | 2019-04-11 16:00:10 +0800 | [diff] [blame] | 507 | if (err == TFM_SUCCESS) { |
Summer Qin | 424d4db | 2019-03-25 14:09:51 +0800 | [diff] [blame] | 508 | return IPC_SUCCESS; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | return IPC_ERROR_MEMORY_CHECK; |
| 512 | } |
| 513 | |
Summer Qin | 75f0d75 | 2019-08-27 14:38:20 +0800 | [diff] [blame] | 514 | uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags) |
Summer Qin | eb537e5 | 2019-03-29 09:57:10 +0800 | [diff] [blame] | 515 | { |
Summer Qin | 75f0d75 | 2019-08-27 14:38:20 +0800 | [diff] [blame] | 516 | if (partition_flags & SPM_PART_FLAG_PSA_ROT) { |
Summer Qin | eb537e5 | 2019-03-29 09:57:10 +0800 | [diff] [blame] | 517 | return TFM_PARTITION_PRIVILEGED_MODE; |
| 518 | } else { |
| 519 | return TFM_PARTITION_UNPRIVILEGED_MODE; |
| 520 | } |
| 521 | } |
| 522 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 523 | /********************** SPM functions for thread mode ************************/ |
| 524 | |
| 525 | void tfm_spm_init(void) |
| 526 | { |
Shawn Shan | 9b0e0c7 | 2019-10-22 13:43:07 +0800 | [diff] [blame] | 527 | uint32_t i, j, num; |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 528 | struct spm_partition_desc_t *partition; |
Ken Liu | 490281d | 2019-12-30 15:55:26 +0800 | [diff] [blame^] | 529 | struct tfm_thrd_ctx *pth, *p_ns_entry_thread = NULL; |
Mate Toth-Pal | 8ac98a7 | 2019-11-21 17:30:10 +0100 | [diff] [blame] | 530 | const struct tfm_spm_partition_platform_data_t **platform_data_p; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 531 | |
| 532 | tfm_pool_init(conn_handle_pool, |
| 533 | POOL_BUFFER_SIZE(conn_handle_pool), |
| 534 | sizeof(struct tfm_conn_handle_t), |
| 535 | TFM_CONN_HANDLE_MAX_NUM); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 536 | |
| 537 | /* Init partition first for it will be used when init service */ |
Mate Toth-Pal | 3ad2e3e | 2019-07-11 21:43:37 +0200 | [diff] [blame] | 538 | for (i = 0; i < g_spm_partition_db.partition_count; i++) { |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 539 | partition = &g_spm_partition_db.partitions[i]; |
Edison Ai | f050170 | 2019-10-11 14:36:42 +0800 | [diff] [blame] | 540 | |
| 541 | /* Check if the PSA framework version matches. */ |
| 542 | if (partition->static_data->psa_framework_version != |
| 543 | PSA_FRAMEWORK_VERSION) { |
| 544 | ERROR_MSG("Warning: PSA Framework Verison is not matched!"); |
| 545 | continue; |
| 546 | } |
| 547 | |
Mate Toth-Pal | 8ac98a7 | 2019-11-21 17:30:10 +0100 | [diff] [blame] | 548 | platform_data_p = partition->platform_data_list; |
| 549 | if (platform_data_p != NULL) { |
| 550 | while ((*platform_data_p) != NULL) { |
| 551 | tfm_spm_hal_configure_default_isolation(*platform_data_p); |
| 552 | ++platform_data_p; |
| 553 | } |
| 554 | } |
| 555 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 556 | if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) { |
| 557 | continue; |
| 558 | } |
Ken Liu | 35f8939 | 2019-03-14 14:51:05 +0800 | [diff] [blame] | 559 | |
Shawn Shan | c7dda0e | 2019-12-23 14:45:09 +0800 | [diff] [blame] | 560 | /* Add PSA_DOORBELL signal to assigned_signals */ |
| 561 | partition->runtime_data.assigned_signals |= PSA_DOORBELL; |
| 562 | |
Shawn Shan | 9b0e0c7 | 2019-10-22 13:43:07 +0800 | [diff] [blame] | 563 | /* TODO: This can be optimized by generating the assigned signal |
| 564 | * in code generation time. |
| 565 | */ |
| 566 | for (j = 0; j < tfm_core_irq_signals_count; ++j) { |
| 567 | if (tfm_core_irq_signals[j].partition_id == |
| 568 | partition->static_data->partition_id) { |
| 569 | partition->runtime_data.assigned_signals |= |
| 570 | tfm_core_irq_signals[j].signal_value; |
| 571 | } |
| 572 | } |
| 573 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 574 | tfm_event_init(&partition->runtime_data.signal_evnt); |
| 575 | tfm_list_init(&partition->runtime_data.service_list); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 576 | |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 577 | pth = tfm_spm_partition_get_thread_info(i); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 578 | if (!pth) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 579 | tfm_core_panic(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | tfm_thrd_init(pth, |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 583 | tfm_spm_partition_get_init_func(i), |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 584 | NULL, |
Ken Liu | 5a2b905 | 2019-08-15 19:03:29 +0800 | [diff] [blame] | 585 | (uintptr_t)tfm_spm_partition_get_stack_top(i), |
| 586 | (uintptr_t)tfm_spm_partition_get_stack_bottom(i)); |
Edison Ai | 788bae2 | 2019-02-18 17:38:59 +0800 | [diff] [blame] | 587 | |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 588 | pth->prior = tfm_spm_partition_get_priority(i); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 589 | |
Ken Liu | 490281d | 2019-12-30 15:55:26 +0800 | [diff] [blame^] | 590 | if (partition->static_data->partition_id == TFM_SP_NON_SECURE_ID) { |
| 591 | p_ns_entry_thread = pth; |
| 592 | } |
| 593 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 594 | /* Kick off */ |
| 595 | if (tfm_thrd_start(pth) != THRD_SUCCESS) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 596 | tfm_core_panic(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
| 600 | /* Init Service */ |
Summer Qin | d99509f | 2019-08-02 17:36:58 +0800 | [diff] [blame] | 601 | num = sizeof(service) / sizeof(struct tfm_spm_service_t); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 602 | for (i = 0; i < num; i++) { |
Summer Qin | e578c5b | 2019-08-16 16:42:16 +0800 | [diff] [blame] | 603 | service[i].service_db = &service_db[i]; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 604 | partition = |
Summer Qin | e578c5b | 2019-08-16 16:42:16 +0800 | [diff] [blame] | 605 | tfm_spm_get_partition_by_id(service[i].service_db->partition_id); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 606 | if (!partition) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 607 | tfm_core_panic(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 608 | } |
Summer Qin | d99509f | 2019-08-02 17:36:58 +0800 | [diff] [blame] | 609 | service[i].partition = partition; |
Shawn Shan | 9b0e0c7 | 2019-10-22 13:43:07 +0800 | [diff] [blame] | 610 | partition->runtime_data.assigned_signals |= service->service_db->signal; |
| 611 | |
Summer Qin | d99509f | 2019-08-02 17:36:58 +0800 | [diff] [blame] | 612 | tfm_list_init(&service[i].handle_list); |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 613 | tfm_list_add_tail(&partition->runtime_data.service_list, |
Summer Qin | d99509f | 2019-08-02 17:36:58 +0800 | [diff] [blame] | 614 | &service[i].list); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 615 | } |
| 616 | |
Ken Liu | 483f5da | 2019-04-24 10:45:21 +0800 | [diff] [blame] | 617 | /* |
| 618 | * All threads initialized, start the scheduler. |
| 619 | * |
| 620 | * NOTE: |
Ken Liu | 490281d | 2019-12-30 15:55:26 +0800 | [diff] [blame^] | 621 | * It is worthy to give the thread object to scheduler if the background |
| 622 | * context belongs to one of the threads. Here the background thread is the |
| 623 | * initialization thread who calls SPM SVC, which re-uses the non-secure |
| 624 | * entry thread's stack. After SPM initialization is done, this stack is |
| 625 | * cleaned up and the background context is never going to return. Tell |
| 626 | * the scheduler that the current thread is non-secure entry thread. |
Ken Liu | 483f5da | 2019-04-24 10:45:21 +0800 | [diff] [blame] | 627 | */ |
Ken Liu | 490281d | 2019-12-30 15:55:26 +0800 | [diff] [blame^] | 628 | tfm_thrd_start_scheduler(p_ns_entry_thread); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 629 | } |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 630 | |
| 631 | void tfm_pendsv_do_schedule(struct tfm_state_context_ext *ctxb) |
| 632 | { |
| 633 | #if TFM_LVL == 2 |
| 634 | struct spm_partition_desc_t *p_next_partition; |
Summer Qin | b5da9cc | 2019-08-26 15:19:45 +0800 | [diff] [blame] | 635 | struct spm_partition_runtime_data_t *r_data; |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 636 | uint32_t is_privileged; |
| 637 | #endif |
| 638 | struct tfm_thrd_ctx *pth_next = tfm_thrd_next_thread(); |
| 639 | struct tfm_thrd_ctx *pth_curr = tfm_thrd_curr_thread(); |
| 640 | |
Mate Toth-Pal | 32b2ccd | 2019-04-26 10:00:16 +0200 | [diff] [blame] | 641 | if (pth_next != NULL && pth_curr != pth_next) { |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 642 | #if TFM_LVL == 2 |
Summer Qin | b5da9cc | 2019-08-26 15:19:45 +0800 | [diff] [blame] | 643 | r_data = TFM_GET_CONTAINER_PTR(pth_next, |
| 644 | struct spm_partition_runtime_data_t, |
| 645 | sp_thrd); |
| 646 | p_next_partition = TFM_GET_CONTAINER_PTR(r_data, |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 647 | struct spm_partition_desc_t, |
Summer Qin | b5da9cc | 2019-08-26 15:19:45 +0800 | [diff] [blame] | 648 | runtime_data); |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 649 | |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 650 | if (p_next_partition->static_data->partition_flags & |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 651 | SPM_PART_FLAG_PSA_ROT) { |
| 652 | is_privileged = TFM_PARTITION_PRIVILEGED_MODE; |
| 653 | } else { |
| 654 | is_privileged = TFM_PARTITION_UNPRIVILEGED_MODE; |
| 655 | } |
| 656 | |
| 657 | tfm_spm_partition_change_privilege(is_privileged); |
| 658 | #endif |
Mate Toth-Pal | c430b99 | 2019-05-09 21:01:14 +0200 | [diff] [blame] | 659 | /* Increase the secure lock, if we enter secure from non-secure */ |
| 660 | if ((void *)pth_curr->pfn == (void *)tfm_nspm_thread_entry) { |
| 661 | ++tfm_secure_lock; |
| 662 | } |
| 663 | /* Decrease the secure lock, if we return from secure to non-secure */ |
| 664 | if ((void *)pth_next->pfn == (void *)tfm_nspm_thread_entry) { |
| 665 | --tfm_secure_lock; |
| 666 | } |
| 667 | |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 668 | tfm_thrd_context_switch(ctxb, pth_curr, pth_next); |
| 669 | } |
David Hu | fb38d56 | 2019-09-23 15:58:34 +0800 | [diff] [blame] | 670 | |
| 671 | /* |
| 672 | * Handle pending mailbox message from NS in multi-core topology. |
| 673 | * Empty operation on single Armv8-M platform. |
| 674 | */ |
| 675 | tfm_rpc_client_call_handler(); |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 676 | } |