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 | */ |
| 7 | #include <inttypes.h> |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 8 | #include <limits.h> |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 9 | #include <stdbool.h> |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 10 | #include <stdlib.h> |
Jamie Fox | cc31d40 | 2019-01-28 17:13:52 +0000 | [diff] [blame] | 11 | #include "psa/client.h" |
| 12 | #include "psa/service.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 13 | #include "tfm_utils.h" |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame^] | 14 | #include "tfm_spm_hal.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 15 | #include "spm_api.h" |
| 16 | #include "spm_db.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 17 | #include "tfm_internal_defines.h" |
| 18 | #include "tfm_wait.h" |
| 19 | #include "tfm_message_queue.h" |
| 20 | #include "tfm_list.h" |
| 21 | #include "tfm_pools.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 22 | #include "tfm_thread.h" |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 23 | #include "region_defs.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 24 | #include "tfm_nspm.h" |
Edison Ai | 807fedb | 2019-03-07 11:22:03 +0800 | [diff] [blame] | 25 | #include "tfm_memory_utils.h" |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 26 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 27 | /* Extern SPM variable */ |
| 28 | extern struct spm_partition_db_t g_spm_partition_db; |
| 29 | |
Mate Toth-Pal | c430b99 | 2019-05-09 21:01:14 +0200 | [diff] [blame] | 30 | /* Extern secure lock variable */ |
| 31 | extern int32_t tfm_secure_lock; |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame^] | 32 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 33 | /* Pools */ |
| 34 | TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t), |
| 35 | TFM_CONN_HANDLE_MAX_NUM); |
| 36 | TFM_POOL_DECLARE(spm_service_pool, sizeof(struct tfm_spm_service_t), |
| 37 | TFM_SPM_MAX_ROT_SERV_NUM); |
| 38 | TFM_POOL_DECLARE(msg_db_pool, sizeof(struct tfm_msg_body_t), |
| 39 | TFM_MSG_QUEUE_MAX_MSG_NUM); |
| 40 | |
| 41 | static struct tfm_spm_service_db_t g_spm_service_db[] = { |
| 42 | #include "secure_fw/services/tfm_service_list.inc" |
| 43 | }; |
| 44 | |
| 45 | /********************** SPM functions for handler mode ***********************/ |
| 46 | |
| 47 | /* Service handle management functions */ |
| 48 | psa_handle_t tfm_spm_create_conn_handle(struct tfm_spm_service_t *service) |
| 49 | { |
| 50 | struct tfm_conn_handle_t *node; |
| 51 | |
| 52 | TFM_ASSERT(service); |
| 53 | |
| 54 | /* Get buffer for handle list structure from handle pool */ |
| 55 | node = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool); |
| 56 | if (!node) { |
| 57 | return PSA_NULL_HANDLE; |
| 58 | } |
| 59 | |
| 60 | /* Global unique handle, use handle buffer address directly */ |
| 61 | node->handle = (psa_handle_t)node; |
| 62 | |
| 63 | /* Add handle node to list for next psa functions */ |
| 64 | tfm_list_add_tail(&service->handle_list, &node->list); |
| 65 | |
| 66 | return node->handle; |
| 67 | } |
| 68 | |
| 69 | static struct tfm_conn_handle_t * |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 70 | tfm_spm_find_conn_handle_node(struct tfm_spm_service_t *service, |
| 71 | psa_handle_t conn_handle) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 72 | { |
| 73 | struct tfm_conn_handle_t *handle_node; |
| 74 | struct tfm_list_node_t *node, *head; |
| 75 | |
| 76 | TFM_ASSERT(service); |
| 77 | |
| 78 | head = &service->handle_list; |
| 79 | TFM_LIST_FOR_EACH(node, head) { |
| 80 | handle_node = TFM_GET_CONTAINER_PTR(node, struct tfm_conn_handle_t, |
| 81 | list); |
| 82 | if (handle_node->handle == conn_handle) { |
| 83 | return handle_node; |
| 84 | } |
| 85 | } |
| 86 | return NULL; |
| 87 | } |
| 88 | |
| 89 | int32_t tfm_spm_free_conn_handle(struct tfm_spm_service_t *service, |
| 90 | psa_handle_t conn_handle) |
| 91 | { |
| 92 | struct tfm_conn_handle_t *node; |
| 93 | |
| 94 | TFM_ASSERT(service); |
| 95 | |
| 96 | /* There are many handles for each RoT Service */ |
| 97 | node = tfm_spm_find_conn_handle_node(service, conn_handle); |
| 98 | if (!node) { |
| 99 | tfm_panic(); |
| 100 | } |
| 101 | |
| 102 | /* Remove node from handle list */ |
| 103 | tfm_list_del_node(&node->list); |
| 104 | |
| 105 | /* Back handle buffer to pool */ |
| 106 | tfm_pool_free(node); |
| 107 | return IPC_SUCCESS; |
| 108 | } |
| 109 | |
| 110 | int32_t tfm_spm_set_rhandle(struct tfm_spm_service_t *service, |
| 111 | psa_handle_t conn_handle, |
| 112 | void *rhandle) |
| 113 | { |
| 114 | struct tfm_conn_handle_t *node; |
| 115 | |
| 116 | TFM_ASSERT(service); |
| 117 | /* Set reverse handle value only be allowed for a connected handle */ |
| 118 | TFM_ASSERT(conn_handle != PSA_NULL_HANDLE); |
| 119 | |
| 120 | /* There are many handles for each RoT Service */ |
| 121 | node = tfm_spm_find_conn_handle_node(service, conn_handle); |
| 122 | if (!node) { |
| 123 | tfm_panic(); |
| 124 | } |
| 125 | |
| 126 | node->rhandle = rhandle; |
| 127 | return IPC_SUCCESS; |
| 128 | } |
| 129 | |
| 130 | void *tfm_spm_get_rhandle(struct tfm_spm_service_t *service, |
| 131 | psa_handle_t conn_handle) |
| 132 | { |
| 133 | struct tfm_conn_handle_t *node; |
| 134 | |
| 135 | TFM_ASSERT(service); |
| 136 | /* Get reverse handle value only be allowed for a connected handle */ |
| 137 | TFM_ASSERT(conn_handle != PSA_NULL_HANDLE); |
| 138 | |
| 139 | /* There are many handles for each RoT Service */ |
| 140 | node = tfm_spm_find_conn_handle_node(service, conn_handle); |
| 141 | if (!node) { |
| 142 | tfm_panic(); |
| 143 | } |
| 144 | |
| 145 | return node->rhandle; |
| 146 | } |
| 147 | |
| 148 | /* Partition management functions */ |
| 149 | struct tfm_spm_service_t * |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame^] | 150 | tfm_spm_get_service_by_signal(struct spm_partition_desc_t *partition, |
| 151 | psa_signal_t signal) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 152 | { |
| 153 | struct tfm_list_node_t *node, *head; |
| 154 | struct tfm_spm_service_t *service; |
| 155 | |
| 156 | TFM_ASSERT(partition); |
| 157 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 158 | if (tfm_list_is_empty(&partition->runtime_data.service_list)) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 159 | tfm_panic(); |
| 160 | } |
| 161 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 162 | head = &partition->runtime_data.service_list; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 163 | TFM_LIST_FOR_EACH(node, head) { |
| 164 | service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t, list); |
| 165 | if (service->service_db->signal == signal) { |
| 166 | return service; |
| 167 | } |
| 168 | } |
| 169 | return NULL; |
| 170 | } |
| 171 | |
| 172 | struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid) |
| 173 | { |
| 174 | uint32_t i; |
| 175 | struct tfm_list_node_t *node, *head; |
| 176 | struct tfm_spm_service_t *service; |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 177 | struct spm_partition_desc_t *partition; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 178 | |
Mate Toth-Pal | 3ad2e3e | 2019-07-11 21:43:37 +0200 | [diff] [blame] | 179 | for (i = 0; i < g_spm_partition_db.partition_count; i++) { |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 180 | partition = &g_spm_partition_db.partitions[i]; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 181 | /* Skip partition without IPC flag */ |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame^] | 182 | if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 183 | continue; |
| 184 | } |
| 185 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 186 | if (tfm_list_is_empty(&partition->runtime_data.service_list)) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 187 | continue; |
| 188 | } |
| 189 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 190 | head = &partition->runtime_data.service_list; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 191 | TFM_LIST_FOR_EACH(node, head) { |
| 192 | service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t, |
| 193 | list); |
| 194 | if (service->service_db->sid == sid) { |
| 195 | return service; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | return NULL; |
| 200 | } |
| 201 | |
| 202 | struct tfm_spm_service_t * |
| 203 | tfm_spm_get_service_by_handle(psa_handle_t conn_handle) |
| 204 | { |
| 205 | uint32_t i; |
| 206 | struct tfm_conn_handle_t *handle; |
| 207 | struct tfm_list_node_t *service_node, *service_head; |
| 208 | struct tfm_list_node_t *handle_node, *handle_head; |
| 209 | struct tfm_spm_service_t *service; |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 210 | struct spm_partition_desc_t *partition; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 211 | |
Mate Toth-Pal | 3ad2e3e | 2019-07-11 21:43:37 +0200 | [diff] [blame] | 212 | for (i = 0; i < g_spm_partition_db.partition_count; i++) { |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 213 | partition = &g_spm_partition_db.partitions[i]; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 214 | /* Skip partition without IPC flag */ |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame^] | 215 | if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 216 | continue; |
| 217 | } |
| 218 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 219 | if (tfm_list_is_empty(&partition->runtime_data.service_list)) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 220 | continue; |
| 221 | } |
| 222 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 223 | service_head = &partition->runtime_data.service_list; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 224 | TFM_LIST_FOR_EACH(service_node, service_head) { |
| 225 | service = TFM_GET_CONTAINER_PTR(service_node, |
| 226 | struct tfm_spm_service_t, list); |
| 227 | handle_head = &service->handle_list; |
| 228 | TFM_LIST_FOR_EACH(handle_node, handle_head) { |
| 229 | handle = TFM_GET_CONTAINER_PTR(handle_node, |
| 230 | struct tfm_conn_handle_t, list); |
| 231 | if (handle->handle == conn_handle) { |
| 232 | return service; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | return NULL; |
| 238 | } |
| 239 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 240 | 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] | 241 | { |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 242 | uint32_t idx = get_partition_idx(partition_id); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 243 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 244 | if (idx != SPM_INVALID_PARTITION_IDX) { |
| 245 | return &(g_spm_partition_db.partitions[idx]); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 246 | } |
| 247 | return NULL; |
| 248 | } |
| 249 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 250 | struct spm_partition_desc_t *tfm_spm_get_running_partition(void) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 251 | { |
| 252 | uint32_t spid; |
| 253 | |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame^] | 254 | spid = tfm_spm_partition_get_running_partition_id(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 255 | |
| 256 | return tfm_spm_get_partition_by_id(spid); |
| 257 | } |
| 258 | |
| 259 | int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service, |
| 260 | uint32_t minor_version) |
| 261 | { |
| 262 | TFM_ASSERT(service); |
| 263 | |
| 264 | switch (service->service_db->minor_policy) { |
| 265 | case TFM_VERSION_POLICY_RELAXED: |
Oren Cohen | 665d832 | 2019-04-28 14:24:40 +0300 | [diff] [blame] | 266 | if (minor_version > service->service_db->minor_version) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 267 | return IPC_ERROR_VERSION; |
| 268 | } |
| 269 | break; |
| 270 | case TFM_VERSION_POLICY_STRICT: |
| 271 | if (minor_version != service->service_db->minor_version) { |
| 272 | return IPC_ERROR_VERSION; |
| 273 | } |
| 274 | break; |
| 275 | default: |
| 276 | return IPC_ERROR_VERSION; |
| 277 | } |
| 278 | return IPC_SUCCESS; |
| 279 | } |
| 280 | |
| 281 | /* Message functions */ |
| 282 | struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle) |
| 283 | { |
| 284 | /* |
| 285 | * There may be one error handle passed by the caller in two conditions: |
| 286 | * 1. Not a valid message handle. |
| 287 | * 2. Handle between different Partitions. Partition A passes one handle |
| 288 | * belong to other Partitions and tries to access other's data. |
| 289 | * So, need do necessary checking to prevent those conditions. |
| 290 | */ |
| 291 | struct tfm_msg_body_t *msg; |
| 292 | uint32_t partition_id; |
| 293 | |
| 294 | msg = (struct tfm_msg_body_t *)msg_handle; |
| 295 | if (!msg) { |
| 296 | return NULL; |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * FixMe: For condition 1: using a magic number to define it's a message. |
| 301 | * It needs to be an enhancement to check the handle belong to service. |
| 302 | */ |
| 303 | if (msg->magic != TFM_MSG_MAGIC) { |
| 304 | return NULL; |
| 305 | } |
| 306 | |
| 307 | /* For condition 2: check if the partition ID is same */ |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame^] | 308 | partition_id = tfm_spm_partition_get_running_partition_id(); |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 309 | if (partition_id != msg->service->partition->static_data.partition_id) { |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 310 | return NULL; |
| 311 | } |
| 312 | |
| 313 | return msg; |
| 314 | } |
| 315 | |
| 316 | struct tfm_msg_body_t *tfm_spm_create_msg(struct tfm_spm_service_t *service, |
| 317 | psa_handle_t handle, |
Summer Qin | 632b3e0 | 2019-07-29 15:34:38 +0800 | [diff] [blame] | 318 | int32_t type, int32_t ns_caller, |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 319 | psa_invec *invec, size_t in_len, |
| 320 | psa_outvec *outvec, size_t out_len, |
| 321 | psa_outvec *caller_outvec) |
| 322 | { |
| 323 | struct tfm_msg_body_t *msg = NULL; |
| 324 | uint32_t i; |
| 325 | |
| 326 | TFM_ASSERT(service); |
| 327 | TFM_ASSERT(!(invec == NULL && in_len != 0)); |
| 328 | TFM_ASSERT(!(outvec == NULL && out_len != 0)); |
| 329 | TFM_ASSERT(in_len <= PSA_MAX_IOVEC); |
| 330 | TFM_ASSERT(out_len <= PSA_MAX_IOVEC); |
| 331 | TFM_ASSERT(in_len + out_len <= PSA_MAX_IOVEC); |
| 332 | |
| 333 | /* Get message buffer from message pool */ |
| 334 | msg = (struct tfm_msg_body_t *)tfm_pool_alloc(msg_db_pool); |
| 335 | if (!msg) { |
| 336 | return NULL; |
| 337 | } |
| 338 | |
| 339 | /* Clear message buffer before using it */ |
| 340 | tfm_memset(msg, 0, sizeof(struct tfm_msg_body_t)); |
| 341 | |
Ken Liu | 35f8939 | 2019-03-14 14:51:05 +0800 | [diff] [blame] | 342 | tfm_event_init(&msg->ack_evnt); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 343 | msg->magic = TFM_MSG_MAGIC; |
| 344 | msg->service = service; |
| 345 | msg->handle = handle; |
| 346 | msg->caller_outvec = caller_outvec; |
| 347 | /* Get current partition id */ |
| 348 | if (ns_caller) { |
| 349 | msg->msg.client_id = tfm_nspm_get_current_client_id(); |
| 350 | } else { |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame^] | 351 | msg->msg.client_id = tfm_spm_partition_get_running_partition_id(); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /* Copy contents */ |
| 355 | msg->msg.type = type; |
| 356 | |
| 357 | for (i = 0; i < in_len; i++) { |
| 358 | msg->msg.in_size[i] = invec[i].len; |
| 359 | msg->invec[i].base = invec[i].base; |
| 360 | } |
| 361 | |
| 362 | for (i = 0; i < out_len; i++) { |
| 363 | msg->msg.out_size[i] = outvec[i].len; |
| 364 | msg->outvec[i].base = outvec[i].base; |
| 365 | /* Out len is used to record the writed number, set 0 here again */ |
| 366 | msg->outvec[i].len = 0; |
| 367 | } |
| 368 | |
| 369 | /* Use message address as handle */ |
| 370 | msg->msg.handle = (psa_handle_t)msg; |
| 371 | |
| 372 | /* For connected handle, set rhandle to every message */ |
| 373 | if (handle != PSA_NULL_HANDLE) { |
| 374 | msg->msg.rhandle = tfm_spm_get_rhandle(service, handle); |
| 375 | } |
| 376 | |
| 377 | return msg; |
| 378 | } |
| 379 | |
| 380 | void tfm_spm_free_msg(struct tfm_msg_body_t *msg) |
| 381 | { |
| 382 | tfm_pool_free(msg); |
| 383 | } |
| 384 | |
| 385 | int32_t tfm_spm_send_event(struct tfm_spm_service_t *service, |
| 386 | struct tfm_msg_body_t *msg) |
| 387 | { |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 388 | struct spm_partition_runtime_data_t *p_runtime_data = |
| 389 | &service->partition->runtime_data; |
| 390 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 391 | TFM_ASSERT(service); |
| 392 | TFM_ASSERT(msg); |
| 393 | |
| 394 | /* Enqueue message to service message queue */ |
| 395 | if (tfm_msg_enqueue(&service->msg_queue, msg) != IPC_SUCCESS) { |
| 396 | return IPC_ERROR_GENERIC; |
| 397 | } |
| 398 | |
| 399 | /* Messages put. Update signals */ |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 400 | p_runtime_data->signals |= service->service_db->signal; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 401 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 402 | tfm_event_wake(&p_runtime_data->signal_evnt, (p_runtime_data->signals & |
| 403 | p_runtime_data->signal_mask)); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 404 | |
Ken Liu | 35f8939 | 2019-03-14 14:51:05 +0800 | [diff] [blame] | 405 | tfm_event_wait(&msg->ack_evnt); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 406 | |
| 407 | return IPC_SUCCESS; |
| 408 | } |
| 409 | |
| 410 | /* SPM extend functions */ |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame^] | 411 | uint32_t tfm_spm_partition_get_running_partition_id(void) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 412 | { |
| 413 | struct tfm_thrd_ctx *pth = tfm_thrd_curr_thread(); |
| 414 | struct spm_partition_desc_t *partition; |
| 415 | |
| 416 | partition = TFM_GET_CONTAINER_PTR(pth, struct spm_partition_desc_t, |
| 417 | sp_thrd); |
| 418 | return partition->static_data.partition_id; |
| 419 | } |
| 420 | |
| 421 | static struct tfm_thrd_ctx * |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame^] | 422 | tfm_spm_partition_get_thread_info(uint32_t partition_idx) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 423 | { |
| 424 | return &g_spm_partition_db.partitions[partition_idx].sp_thrd; |
| 425 | } |
| 426 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 427 | static tfm_thrd_func_t |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame^] | 428 | tfm_spm_partition_get_init_func(uint32_t partition_idx) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 429 | { |
| 430 | return (tfm_thrd_func_t)(g_spm_partition_db.partitions[partition_idx]. |
| 431 | static_data.partition_init); |
| 432 | } |
| 433 | |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame^] | 434 | static uint32_t tfm_spm_partition_get_priority(uint32_t partition_idx) |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 435 | { |
| 436 | return g_spm_partition_db.partitions[partition_idx].static_data. |
| 437 | partition_priority; |
| 438 | } |
| 439 | |
Summer Qin | 424d4db | 2019-03-25 14:09:51 +0800 | [diff] [blame] | 440 | int32_t tfm_memory_check(void *buffer, size_t len, int32_t ns_caller, |
Summer Qin | eb537e5 | 2019-03-29 09:57:10 +0800 | [diff] [blame] | 441 | enum tfm_memory_access_e access, |
| 442 | uint32_t privileged) |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 443 | { |
Hugues de Valon | 9957856 | 2019-06-18 16:08:51 +0100 | [diff] [blame] | 444 | enum tfm_status_e err; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 445 | |
| 446 | /* If len is zero, this indicates an empty buffer and base is ignored */ |
| 447 | if (len == 0) { |
| 448 | return IPC_SUCCESS; |
| 449 | } |
| 450 | |
| 451 | if (!buffer) { |
| 452 | return IPC_ERROR_BAD_PARAMETERS; |
| 453 | } |
| 454 | |
| 455 | if ((uintptr_t)buffer > (UINTPTR_MAX - len)) { |
| 456 | return IPC_ERROR_MEMORY_CHECK; |
| 457 | } |
| 458 | |
Summer Qin | 424d4db | 2019-03-25 14:09:51 +0800 | [diff] [blame] | 459 | if (access == TFM_MEMORY_ACCESS_RW) { |
Summer Qin | eb537e5 | 2019-03-29 09:57:10 +0800 | [diff] [blame] | 460 | err = tfm_core_has_write_access_to_region(buffer, len, ns_caller, |
| 461 | privileged); |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 462 | } else { |
Summer Qin | eb537e5 | 2019-03-29 09:57:10 +0800 | [diff] [blame] | 463 | err = tfm_core_has_read_access_to_region(buffer, len, ns_caller, |
| 464 | privileged); |
Summer Qin | 424d4db | 2019-03-25 14:09:51 +0800 | [diff] [blame] | 465 | } |
Summer Qin | 0fc3f59 | 2019-04-11 16:00:10 +0800 | [diff] [blame] | 466 | if (err == TFM_SUCCESS) { |
Summer Qin | 424d4db | 2019-03-25 14:09:51 +0800 | [diff] [blame] | 467 | return IPC_SUCCESS; |
Summer Qin | 2bfd2a0 | 2018-09-26 17:10:41 +0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | return IPC_ERROR_MEMORY_CHECK; |
| 471 | } |
| 472 | |
Summer Qin | eb537e5 | 2019-03-29 09:57:10 +0800 | [diff] [blame] | 473 | uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_idx) |
| 474 | { |
| 475 | if (tfm_spm_partition_get_flags(partition_idx) & SPM_PART_FLAG_PSA_ROT) { |
| 476 | return TFM_PARTITION_PRIVILEGED_MODE; |
| 477 | } else { |
| 478 | return TFM_PARTITION_UNPRIVILEGED_MODE; |
| 479 | } |
| 480 | } |
| 481 | |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 482 | /********************** SPM functions for thread mode ************************/ |
| 483 | |
| 484 | void tfm_spm_init(void) |
| 485 | { |
| 486 | uint32_t i, num; |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 487 | struct spm_partition_desc_t *partition; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 488 | struct tfm_spm_service_t *service; |
Ken Liu | 483f5da | 2019-04-24 10:45:21 +0800 | [diff] [blame] | 489 | struct tfm_thrd_ctx *pth, this_thrd; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 490 | |
| 491 | tfm_pool_init(conn_handle_pool, |
| 492 | POOL_BUFFER_SIZE(conn_handle_pool), |
| 493 | sizeof(struct tfm_conn_handle_t), |
| 494 | TFM_CONN_HANDLE_MAX_NUM); |
| 495 | tfm_pool_init(spm_service_pool, POOL_BUFFER_SIZE(spm_service_pool), |
| 496 | sizeof(struct tfm_spm_service_t), |
| 497 | TFM_SPM_MAX_ROT_SERV_NUM); |
| 498 | tfm_pool_init(msg_db_pool, POOL_BUFFER_SIZE(msg_db_pool), |
| 499 | sizeof(struct tfm_msg_body_t), |
| 500 | TFM_MSG_QUEUE_MAX_MSG_NUM); |
| 501 | |
| 502 | /* Init partition first for it will be used when init service */ |
Mate Toth-Pal | 3ad2e3e | 2019-07-11 21:43:37 +0200 | [diff] [blame] | 503 | for (i = 0; i < g_spm_partition_db.partition_count; i++) { |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 504 | partition = &g_spm_partition_db.partitions[i]; |
| 505 | tfm_spm_hal_configure_default_isolation(partition->platform_data); |
| 506 | partition->static_data.index = i; |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 507 | if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) { |
| 508 | continue; |
| 509 | } |
Ken Liu | 35f8939 | 2019-03-14 14:51:05 +0800 | [diff] [blame] | 510 | |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 511 | tfm_event_init(&partition->runtime_data.signal_evnt); |
| 512 | tfm_list_init(&partition->runtime_data.service_list); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 513 | |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame^] | 514 | pth = tfm_spm_partition_get_thread_info(i); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 515 | if (!pth) { |
| 516 | tfm_panic(); |
| 517 | } |
| 518 | |
| 519 | tfm_thrd_init(pth, |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame^] | 520 | tfm_spm_partition_get_init_func(i), |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 521 | NULL, |
Edison Ai | 788bae2 | 2019-02-18 17:38:59 +0800 | [diff] [blame] | 522 | (uint8_t *)tfm_spm_partition_get_stack_top(i), |
| 523 | (uint8_t *)tfm_spm_partition_get_stack_bottom(i)); |
Edison Ai | 788bae2 | 2019-02-18 17:38:59 +0800 | [diff] [blame] | 524 | |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame^] | 525 | pth->prior = tfm_spm_partition_get_priority(i); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 526 | |
| 527 | /* Kick off */ |
| 528 | if (tfm_thrd_start(pth) != THRD_SUCCESS) { |
| 529 | tfm_panic(); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | /* Init Service */ |
| 534 | num = sizeof(g_spm_service_db) / sizeof(struct tfm_spm_service_db_t); |
| 535 | for (i = 0; i < num; i++) { |
| 536 | partition = |
| 537 | tfm_spm_get_partition_by_id(g_spm_service_db[i].partition_id); |
| 538 | if (!partition) { |
| 539 | tfm_panic(); |
| 540 | } |
| 541 | service = (struct tfm_spm_service_t *)tfm_pool_alloc(spm_service_pool); |
| 542 | if (!service) { |
| 543 | tfm_panic(); |
| 544 | } |
| 545 | service->service_db = &g_spm_service_db[i]; |
| 546 | service->partition = partition; |
| 547 | tfm_list_init(&service->handle_list); |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 548 | tfm_list_add_tail(&partition->runtime_data.service_list, |
| 549 | &service->list); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 550 | } |
| 551 | |
Ken Liu | 483f5da | 2019-04-24 10:45:21 +0800 | [diff] [blame] | 552 | /* |
| 553 | * All threads initialized, start the scheduler. |
| 554 | * |
| 555 | * NOTE: |
| 556 | * Here is the booting privileged thread mode, and will never |
| 557 | * return to this place after scheduler is started. The start |
| 558 | * function has to save current runtime context to act as a |
| 559 | * 'current thread' to avoid repeating NULL 'current thread' |
| 560 | * checking while context switching. This saved context is worthy |
| 561 | * of being saved somewhere if there are potential usage purpose. |
| 562 | * Let's save this context in a local variable 'this_thrd' at |
| 563 | * current since there is no usage for it. |
Mate Toth-Pal | c430b99 | 2019-05-09 21:01:14 +0200 | [diff] [blame] | 564 | * Also set tfm_nspm_thread_entry as pfn for this thread to |
| 565 | * use in detecting NS/S thread scheduling changes. |
Ken Liu | 483f5da | 2019-04-24 10:45:21 +0800 | [diff] [blame] | 566 | */ |
Mate Toth-Pal | c430b99 | 2019-05-09 21:01:14 +0200 | [diff] [blame] | 567 | this_thrd.pfn = (tfm_thrd_func_t)tfm_nspm_thread_entry; |
Ken Liu | 483f5da | 2019-04-24 10:45:21 +0800 | [diff] [blame] | 568 | tfm_thrd_start_scheduler(&this_thrd); |
Edison Ai | 764d41f | 2018-09-21 15:56:36 +0800 | [diff] [blame] | 569 | } |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 570 | |
| 571 | void tfm_pendsv_do_schedule(struct tfm_state_context_ext *ctxb) |
| 572 | { |
| 573 | #if TFM_LVL == 2 |
| 574 | struct spm_partition_desc_t *p_next_partition; |
| 575 | uint32_t is_privileged; |
| 576 | #endif |
| 577 | struct tfm_thrd_ctx *pth_next = tfm_thrd_next_thread(); |
| 578 | struct tfm_thrd_ctx *pth_curr = tfm_thrd_curr_thread(); |
| 579 | |
Mate Toth-Pal | 32b2ccd | 2019-04-26 10:00:16 +0200 | [diff] [blame] | 580 | if (pth_next != NULL && pth_curr != pth_next) { |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 581 | #if TFM_LVL == 2 |
| 582 | p_next_partition = TFM_GET_CONTAINER_PTR(pth_next, |
| 583 | struct spm_partition_desc_t, |
| 584 | sp_thrd); |
| 585 | |
| 586 | if (p_next_partition->static_data.partition_flags & |
| 587 | SPM_PART_FLAG_PSA_ROT) { |
| 588 | is_privileged = TFM_PARTITION_PRIVILEGED_MODE; |
| 589 | } else { |
| 590 | is_privileged = TFM_PARTITION_UNPRIVILEGED_MODE; |
| 591 | } |
| 592 | |
| 593 | tfm_spm_partition_change_privilege(is_privileged); |
| 594 | #endif |
Mate Toth-Pal | c430b99 | 2019-05-09 21:01:14 +0200 | [diff] [blame] | 595 | /* Increase the secure lock, if we enter secure from non-secure */ |
| 596 | if ((void *)pth_curr->pfn == (void *)tfm_nspm_thread_entry) { |
| 597 | ++tfm_secure_lock; |
| 598 | } |
| 599 | /* Decrease the secure lock, if we return from secure to non-secure */ |
| 600 | if ((void *)pth_next->pfn == (void *)tfm_nspm_thread_entry) { |
| 601 | --tfm_secure_lock; |
| 602 | } |
| 603 | |
Ken Liu | 2d17517 | 2019-03-21 17:08:41 +0800 | [diff] [blame] | 604 | tfm_thrd_context_switch(ctxb, pth_curr, pth_next); |
| 605 | } |
| 606 | } |