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