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