blob: 19f1d9a34d8bf81cf5be0f3909c2bc249148b40e [file] [log] [blame]
Edison Ai764d41f2018-09-21 15:56:36 +08001/*
Ken Liu5248af22019-12-29 12:47:13 +08002 * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
Edison Ai764d41f2018-09-21 15:56:36 +08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
Mingyang Sunda01a972019-07-12 17:32:59 +08007
8/* All the APIs defined in this file are used for IPC model. */
9
Edison Ai764d41f2018-09-21 15:56:36 +080010#include <inttypes.h>
Summer Qin2bfd2a02018-09-26 17:10:41 +080011#include <limits.h>
Edison Ai764d41f2018-09-21 15:56:36 +080012#include <stdbool.h>
Edison Ai764d41f2018-09-21 15:56:36 +080013#include <stdlib.h>
Jamie Foxcc31d402019-01-28 17:13:52 +000014#include "psa/client.h"
15#include "psa/service.h"
Edison Ai764d41f2018-09-21 15:56:36 +080016#include "tfm_utils.h"
Mingyang Sunf3d29892019-07-10 17:50:23 +080017#include "tfm_spm_hal.h"
Edison Ai764d41f2018-09-21 15:56:36 +080018#include "spm_api.h"
19#include "spm_db.h"
David Hu46603dd2019-12-11 18:05:16 +080020#include "tfm_api.h"
David Hu326f3972019-08-06 14:16:51 +080021#include "tfm_core_mem_check.h"
Edison Ai764d41f2018-09-21 15:56:36 +080022#include "tfm_internal_defines.h"
23#include "tfm_wait.h"
24#include "tfm_message_queue.h"
25#include "tfm_list.h"
26#include "tfm_pools.h"
Edison Ai764d41f2018-09-21 15:56:36 +080027#include "tfm_thread.h"
Summer Qin2bfd2a02018-09-26 17:10:41 +080028#include "region_defs.h"
Edison Ai764d41f2018-09-21 15:56:36 +080029#include "tfm_nspm.h"
Edison Ai807fedb2019-03-07 11:22:03 +080030#include "tfm_memory_utils.h"
Mingyang Sun94b1b412019-09-20 15:11:14 +080031#include "tfm_core_utils.h"
David Hufb38d562019-09-23 15:58:34 +080032#include "tfm_rpc.h"
Shawn Shan9b0e0c72019-10-22 13:43:07 +080033#include "tfm_irq_list.h"
Edison Ai764d41f2018-09-21 15:56:36 +080034
Summer Qind99509f2019-08-02 17:36:58 +080035#include "secure_fw/services/tfm_service_list.inc"
36
37/* Extern service variable */
38extern struct tfm_spm_service_t service[];
Summer Qine578c5b2019-08-16 16:42:16 +080039extern const struct tfm_spm_service_db_t service_db[];
Summer Qind99509f2019-08-02 17:36:58 +080040
Edison Ai764d41f2018-09-21 15:56:36 +080041/* Extern SPM variable */
42extern struct spm_partition_db_t g_spm_partition_db;
43
44/* Pools */
45TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t),
46 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +080047
Edison Ai764d41f2018-09-21 15:56:36 +080048/********************** SPM functions for handler mode ***********************/
49
50/* Service handle management functions */
Summer Qin1ce712a2019-10-14 18:04:05 +080051psa_handle_t tfm_spm_create_conn_handle(struct tfm_spm_service_t *service,
52 int32_t client_id)
Edison Ai764d41f2018-09-21 15:56:36 +080053{
Edison Ai9cc26242019-08-06 11:28:04 +080054 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +080055
Ken Liuf250b8b2019-12-27 16:31:24 +080056 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +080057
58 /* Get buffer for handle list structure from handle pool */
Edison Ai9cc26242019-08-06 11:28:04 +080059 p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
60 if (!p_handle) {
Edison Ai764d41f2018-09-21 15:56:36 +080061 return PSA_NULL_HANDLE;
62 }
63
Edison Ai9cc26242019-08-06 11:28:04 +080064 p_handle->service = service;
Shawn Shancc39fcb2019-11-13 15:38:16 +080065 p_handle->status = TFM_HANDLE_STATUS_IDLE;
Summer Qin1ce712a2019-10-14 18:04:05 +080066 p_handle->client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +080067
68 /* Add handle node to list for next psa functions */
Edison Ai9cc26242019-08-06 11:28:04 +080069 tfm_list_add_tail(&service->handle_list, &p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +080070
Edison Ai9cc26242019-08-06 11:28:04 +080071 return (psa_handle_t)p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +080072}
73
Summer Qin1ce712a2019-10-14 18:04:05 +080074int32_t tfm_spm_validate_conn_handle(psa_handle_t conn_handle,
75 int32_t client_id)
76{
77 /* Check the handle address is validated */
78 if (is_valid_chunk_data_in_pool(conn_handle_pool,
79 (uint8_t *)conn_handle) != true) {
80 return IPC_ERROR_GENERIC;
81 }
82
83 /* Check the handle caller is correct */
84 if (((struct tfm_conn_handle_t *)conn_handle)->client_id != client_id) {
85 return IPC_ERROR_GENERIC;
86 }
87
88 return IPC_SUCCESS;
89}
90
Edison Ai764d41f2018-09-21 15:56:36 +080091static struct tfm_conn_handle_t *
Mingyang Sun5e13aa72019-07-10 10:30:16 +080092 tfm_spm_find_conn_handle_node(struct tfm_spm_service_t *service,
93 psa_handle_t conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +080094{
Ken Liuf250b8b2019-12-27 16:31:24 +080095 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +080096
Edison Ai9cc26242019-08-06 11:28:04 +080097 return (struct tfm_conn_handle_t *)conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +080098}
99
100int32_t tfm_spm_free_conn_handle(struct tfm_spm_service_t *service,
101 psa_handle_t conn_handle)
102{
Edison Ai9cc26242019-08-06 11:28:04 +0800103 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800104
Ken Liuf250b8b2019-12-27 16:31:24 +0800105 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800106
107 /* There are many handles for each RoT Service */
Edison Ai9cc26242019-08-06 11:28:04 +0800108 p_handle = tfm_spm_find_conn_handle_node(service, conn_handle);
109 if (!p_handle) {
Edison Ai9059ea02019-11-28 13:46:14 +0800110 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800111 }
112
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200113 /* Clear magic as the handler is not used anymore */
114 p_handle->internal_msg.magic = 0;
115
Edison Ai764d41f2018-09-21 15:56:36 +0800116 /* Remove node from handle list */
Edison Ai9cc26242019-08-06 11:28:04 +0800117 tfm_list_del_node(&p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800118
119 /* Back handle buffer to pool */
Edison Ai9cc26242019-08-06 11:28:04 +0800120 tfm_pool_free(p_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800121 return IPC_SUCCESS;
122}
123
124int32_t tfm_spm_set_rhandle(struct tfm_spm_service_t *service,
125 psa_handle_t conn_handle,
126 void *rhandle)
127{
Edison Ai9cc26242019-08-06 11:28:04 +0800128 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800129
Ken Liuf250b8b2019-12-27 16:31:24 +0800130 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800131 /* Set reverse handle value only be allowed for a connected handle */
Ken Liuf250b8b2019-12-27 16:31:24 +0800132 TFM_CORE_ASSERT(conn_handle != PSA_NULL_HANDLE);
Edison Ai764d41f2018-09-21 15:56:36 +0800133
134 /* There are many handles for each RoT Service */
Edison Ai9cc26242019-08-06 11:28:04 +0800135 p_handle = tfm_spm_find_conn_handle_node(service, conn_handle);
136 if (!p_handle) {
Edison Ai9059ea02019-11-28 13:46:14 +0800137 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800138 }
139
Edison Ai9cc26242019-08-06 11:28:04 +0800140 p_handle->rhandle = rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800141 return IPC_SUCCESS;
142}
143
144void *tfm_spm_get_rhandle(struct tfm_spm_service_t *service,
145 psa_handle_t conn_handle)
146{
Edison Ai9cc26242019-08-06 11:28:04 +0800147 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800148
Ken Liuf250b8b2019-12-27 16:31:24 +0800149 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800150 /* Get reverse handle value only be allowed for a connected handle */
Ken Liuf250b8b2019-12-27 16:31:24 +0800151 TFM_CORE_ASSERT(conn_handle != PSA_NULL_HANDLE);
Edison Ai764d41f2018-09-21 15:56:36 +0800152
153 /* There are many handles for each RoT Service */
Edison Ai9cc26242019-08-06 11:28:04 +0800154 p_handle = tfm_spm_find_conn_handle_node(service, conn_handle);
155 if (!p_handle) {
Edison Ai9059ea02019-11-28 13:46:14 +0800156 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800157 }
158
Edison Ai9cc26242019-08-06 11:28:04 +0800159 return p_handle->rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800160}
161
162/* Partition management functions */
163struct tfm_spm_service_t *
Mingyang Sunf3d29892019-07-10 17:50:23 +0800164 tfm_spm_get_service_by_signal(struct spm_partition_desc_t *partition,
165 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800166{
167 struct tfm_list_node_t *node, *head;
168 struct tfm_spm_service_t *service;
169
Ken Liuf250b8b2019-12-27 16:31:24 +0800170 TFM_CORE_ASSERT(partition);
Edison Ai764d41f2018-09-21 15:56:36 +0800171
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800172 if (tfm_list_is_empty(&partition->runtime_data.service_list)) {
Edison Ai9059ea02019-11-28 13:46:14 +0800173 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800174 }
175
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800176 head = &partition->runtime_data.service_list;
Edison Ai764d41f2018-09-21 15:56:36 +0800177 TFM_LIST_FOR_EACH(node, head) {
178 service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t, list);
Summer Qine578c5b2019-08-16 16:42:16 +0800179 if (service->service_db->signal == signal) {
Edison Ai764d41f2018-09-21 15:56:36 +0800180 return service;
181 }
182 }
183 return NULL;
184}
185
186struct tfm_spm_service_t *tfm_spm_get_service_by_sid(uint32_t sid)
187{
188 uint32_t i;
189 struct tfm_list_node_t *node, *head;
190 struct tfm_spm_service_t *service;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800191 struct spm_partition_desc_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800192
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200193 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800194 partition = &g_spm_partition_db.partitions[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800195 /* Skip partition without IPC flag */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800196 if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) {
Edison Ai764d41f2018-09-21 15:56:36 +0800197 continue;
198 }
199
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800200 if (tfm_list_is_empty(&partition->runtime_data.service_list)) {
Edison Ai764d41f2018-09-21 15:56:36 +0800201 continue;
202 }
203
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800204 head = &partition->runtime_data.service_list;
Edison Ai764d41f2018-09-21 15:56:36 +0800205 TFM_LIST_FOR_EACH(node, head) {
206 service = TFM_GET_CONTAINER_PTR(node, struct tfm_spm_service_t,
207 list);
Summer Qine578c5b2019-08-16 16:42:16 +0800208 if (service->service_db->sid == sid) {
Edison Ai764d41f2018-09-21 15:56:36 +0800209 return service;
210 }
211 }
212 }
213 return NULL;
214}
215
216struct tfm_spm_service_t *
217 tfm_spm_get_service_by_handle(psa_handle_t conn_handle)
218{
Edison Ai9cc26242019-08-06 11:28:04 +0800219 return ((struct tfm_conn_handle_t *)conn_handle)->service;
Edison Ai764d41f2018-09-21 15:56:36 +0800220}
221
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800222struct spm_partition_desc_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800223{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800224 uint32_t idx = get_partition_idx(partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800225
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800226 if (idx != SPM_INVALID_PARTITION_IDX) {
227 return &(g_spm_partition_db.partitions[idx]);
Edison Ai764d41f2018-09-21 15:56:36 +0800228 }
229 return NULL;
230}
231
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800232struct spm_partition_desc_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800233{
234 uint32_t spid;
235
Mingyang Sunf3d29892019-07-10 17:50:23 +0800236 spid = tfm_spm_partition_get_running_partition_id();
Edison Ai764d41f2018-09-21 15:56:36 +0800237
238 return tfm_spm_get_partition_by_id(spid);
239}
240
241int32_t tfm_spm_check_client_version(struct tfm_spm_service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530242 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800243{
Ken Liuf250b8b2019-12-27 16:31:24 +0800244 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800245
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530246 switch (service->service_db->version_policy) {
Edison Ai764d41f2018-09-21 15:56:36 +0800247 case TFM_VERSION_POLICY_RELAXED:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530248 if (version > service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800249 return IPC_ERROR_VERSION;
250 }
251 break;
252 case TFM_VERSION_POLICY_STRICT:
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530253 if (version != service->service_db->version) {
Edison Ai764d41f2018-09-21 15:56:36 +0800254 return IPC_ERROR_VERSION;
255 }
256 break;
257 default:
258 return IPC_ERROR_VERSION;
259 }
260 return IPC_SUCCESS;
261}
262
Edison Aie728fbf2019-11-13 09:37:12 +0800263int32_t tfm_spm_check_authorization(uint32_t sid,
264 struct tfm_spm_service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800265 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800266{
267 struct spm_partition_desc_t *partition = NULL;
268 int32_t i;
269
Ken Liuf250b8b2019-12-27 16:31:24 +0800270 TFM_CORE_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800271
272 if (ns_caller) {
273 if (!service->service_db->non_secure_client) {
274 return IPC_ERROR_GENERIC;
275 }
276 } else {
277 partition = tfm_spm_get_running_partition();
278 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800279 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800280 }
281
282 for (i = 0; i < partition->static_data->dependencies_num; i++) {
283 if (partition->static_data->p_dependencies[i] == sid) {
284 break;
285 }
286 }
287
288 if (i == partition->static_data->dependencies_num) {
289 return IPC_ERROR_GENERIC;
290 }
291 }
292 return IPC_SUCCESS;
293}
294
Edison Ai764d41f2018-09-21 15:56:36 +0800295/* Message functions */
296struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
297{
298 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200299 * The message handler passed by the caller is considered invalid in the
300 * following cases:
301 * 1. Not a valid message handle. (The address of a message is not the
302 * address of a possible handle from the pool
303 * 2. Handle not belongs to the caller partition (The handle is either
304 * unused, or owned by anither partition)
305 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800306 */
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200307 struct tfm_conn_handle_t *connection_handle_address;
Edison Ai764d41f2018-09-21 15:56:36 +0800308 struct tfm_msg_body_t *msg;
309 uint32_t partition_id;
310
311 msg = (struct tfm_msg_body_t *)msg_handle;
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200312
313 connection_handle_address =
314 TFM_GET_CONTAINER_PTR(msg, struct tfm_conn_handle_t, internal_msg);
315
316 if (is_valid_chunk_data_in_pool(
317 conn_handle_pool, (uint8_t *)connection_handle_address) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800318 return NULL;
319 }
320
321 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200322 * Check that the magic number is correct. This proves that the message
323 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800324 */
325 if (msg->magic != TFM_MSG_MAGIC) {
326 return NULL;
327 }
328
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200329 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800330 partition_id = tfm_spm_partition_get_running_partition_id();
Summer Qin423dbef2019-08-22 15:59:35 +0800331 if (partition_id != msg->service->partition->static_data->partition_id) {
Edison Ai764d41f2018-09-21 15:56:36 +0800332 return NULL;
333 }
334
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200335 /*
336 * FixMe: For condition 1 it should be checked whether the message belongs
337 * to the service. Skipping this check isn't a security risk as even if the
338 * message belongs to another service, the handle belongs to the calling
339 * partition.
340 */
341
Edison Ai764d41f2018-09-21 15:56:36 +0800342 return msg;
343}
344
Edison Ai97115822019-08-01 14:22:19 +0800345struct tfm_msg_body_t *
346 tfm_spm_get_msg_buffer_from_conn_handle(psa_handle_t conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800347{
Ken Liuf250b8b2019-12-27 16:31:24 +0800348 TFM_CORE_ASSERT(conn_handle != PSA_NULL_HANDLE);
Edison Ai97115822019-08-01 14:22:19 +0800349
350 return &(((struct tfm_conn_handle_t *)conn_handle)->internal_msg);
351}
352
353void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
354 struct tfm_spm_service_t *service,
355 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800356 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800357 psa_invec *invec, size_t in_len,
358 psa_outvec *outvec, size_t out_len,
359 psa_outvec *caller_outvec)
360{
Edison Ai764d41f2018-09-21 15:56:36 +0800361 uint32_t i;
362
Ken Liuf250b8b2019-12-27 16:31:24 +0800363 TFM_CORE_ASSERT(msg);
364 TFM_CORE_ASSERT(service);
365 TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
366 TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
367 TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
368 TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
369 TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800370
Edison Ai764d41f2018-09-21 15:56:36 +0800371 /* Clear message buffer before using it */
Mingyang Sun94b1b412019-09-20 15:11:14 +0800372 tfm_core_util_memset(msg, 0, sizeof(struct tfm_msg_body_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800373
Ken Liu35f89392019-03-14 14:51:05 +0800374 tfm_event_init(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800375 msg->magic = TFM_MSG_MAGIC;
376 msg->service = service;
377 msg->handle = handle;
378 msg->caller_outvec = caller_outvec;
Summer Qin1ce712a2019-10-14 18:04:05 +0800379 msg->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800380
381 /* Copy contents */
382 msg->msg.type = type;
383
384 for (i = 0; i < in_len; i++) {
385 msg->msg.in_size[i] = invec[i].len;
386 msg->invec[i].base = invec[i].base;
387 }
388
389 for (i = 0; i < out_len; i++) {
390 msg->msg.out_size[i] = outvec[i].len;
391 msg->outvec[i].base = outvec[i].base;
392 /* Out len is used to record the writed number, set 0 here again */
393 msg->outvec[i].len = 0;
394 }
395
396 /* Use message address as handle */
397 msg->msg.handle = (psa_handle_t)msg;
398
399 /* For connected handle, set rhandle to every message */
400 if (handle != PSA_NULL_HANDLE) {
401 msg->msg.rhandle = tfm_spm_get_rhandle(service, handle);
402 }
David Hu46603dd2019-12-11 18:05:16 +0800403
404 /* Set the private data of NSPE client caller in multi-core topology */
405 if (TFM_CLIENT_ID_IS_NS(client_id)) {
406 tfm_rpc_set_caller_data(msg, client_id);
407 }
Edison Ai764d41f2018-09-21 15:56:36 +0800408}
409
410int32_t tfm_spm_send_event(struct tfm_spm_service_t *service,
411 struct tfm_msg_body_t *msg)
412{
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800413 struct spm_partition_runtime_data_t *p_runtime_data =
414 &service->partition->runtime_data;
415
Ken Liuf250b8b2019-12-27 16:31:24 +0800416 TFM_CORE_ASSERT(service);
417 TFM_CORE_ASSERT(msg);
Edison Ai764d41f2018-09-21 15:56:36 +0800418
419 /* Enqueue message to service message queue */
420 if (tfm_msg_enqueue(&service->msg_queue, msg) != IPC_SUCCESS) {
421 return IPC_ERROR_GENERIC;
422 }
423
424 /* Messages put. Update signals */
Summer Qine578c5b2019-08-16 16:42:16 +0800425 p_runtime_data->signals |= service->service_db->signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800426
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800427 tfm_event_wake(&p_runtime_data->signal_evnt, (p_runtime_data->signals &
428 p_runtime_data->signal_mask));
Edison Ai764d41f2018-09-21 15:56:36 +0800429
David Hufb38d562019-09-23 15:58:34 +0800430 /*
431 * If it is a NS request via RPC, it is unnecessary to block current
432 * thread.
433 */
434 if (!is_tfm_rpc_msg(msg)) {
435 tfm_event_wait(&msg->ack_evnt);
436 }
Edison Ai764d41f2018-09-21 15:56:36 +0800437
438 return IPC_SUCCESS;
439}
440
Edison Ai7aff9e82019-07-11 14:56:46 +0800441uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
442{
443 return g_spm_partition_db.partitions[partition_idx].
Summer Qin423dbef2019-08-22 15:59:35 +0800444 memory_data->stack_bottom;
Edison Ai7aff9e82019-07-11 14:56:46 +0800445}
446
447uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
448{
Summer Qin423dbef2019-08-22 15:59:35 +0800449 return g_spm_partition_db.partitions[partition_idx].memory_data->stack_top;
Edison Ai7aff9e82019-07-11 14:56:46 +0800450}
451
Mingyang Sunf3d29892019-07-10 17:50:23 +0800452uint32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800453{
Summer Qin66f1e032020-01-06 15:40:03 +0800454 struct tfm_core_thread_t *pth = tfm_core_thrd_get_curr_thread();
Edison Ai764d41f2018-09-21 15:56:36 +0800455 struct spm_partition_desc_t *partition;
Summer Qinb5da9cc2019-08-26 15:19:45 +0800456 struct spm_partition_runtime_data_t *r_data;
Edison Ai764d41f2018-09-21 15:56:36 +0800457
Summer Qinb5da9cc2019-08-26 15:19:45 +0800458 r_data = TFM_GET_CONTAINER_PTR(pth, struct spm_partition_runtime_data_t,
459 sp_thrd);
460 partition = TFM_GET_CONTAINER_PTR(r_data, struct spm_partition_desc_t,
461 runtime_data);
Summer Qin423dbef2019-08-22 15:59:35 +0800462 return partition->static_data->partition_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800463}
464
Summer Qin66f1e032020-01-06 15:40:03 +0800465static struct tfm_core_thread_t *
Mingyang Sunf3d29892019-07-10 17:50:23 +0800466 tfm_spm_partition_get_thread_info(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800467{
Summer Qinb5da9cc2019-08-26 15:19:45 +0800468 return &g_spm_partition_db.partitions[partition_idx].runtime_data.sp_thrd;
Edison Ai764d41f2018-09-21 15:56:36 +0800469}
470
Summer Qin66f1e032020-01-06 15:40:03 +0800471static tfm_core_thrd_entry_t
Mingyang Sunf3d29892019-07-10 17:50:23 +0800472 tfm_spm_partition_get_init_func(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800473{
Summer Qin66f1e032020-01-06 15:40:03 +0800474 return (tfm_core_thrd_entry_t)(g_spm_partition_db.partitions[partition_idx].
Summer Qin423dbef2019-08-22 15:59:35 +0800475 static_data->partition_init);
Edison Ai764d41f2018-09-21 15:56:36 +0800476}
477
Mingyang Sunf3d29892019-07-10 17:50:23 +0800478static uint32_t tfm_spm_partition_get_priority(uint32_t partition_idx)
Edison Ai764d41f2018-09-21 15:56:36 +0800479{
Summer Qin423dbef2019-08-22 15:59:35 +0800480 return g_spm_partition_db.partitions[partition_idx].static_data->
Edison Ai764d41f2018-09-21 15:56:36 +0800481 partition_priority;
482}
483
Summer Qin43c185d2019-10-10 15:44:42 +0800484int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800485 enum tfm_memory_access_e access,
486 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800487{
Hugues de Valon99578562019-06-18 16:08:51 +0100488 enum tfm_status_e err;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800489
490 /* If len is zero, this indicates an empty buffer and base is ignored */
491 if (len == 0) {
492 return IPC_SUCCESS;
493 }
494
495 if (!buffer) {
496 return IPC_ERROR_BAD_PARAMETERS;
497 }
498
499 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
500 return IPC_ERROR_MEMORY_CHECK;
501 }
502
Summer Qin424d4db2019-03-25 14:09:51 +0800503 if (access == TFM_MEMORY_ACCESS_RW) {
Summer Qineb537e52019-03-29 09:57:10 +0800504 err = tfm_core_has_write_access_to_region(buffer, len, ns_caller,
505 privileged);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800506 } else {
Summer Qineb537e52019-03-29 09:57:10 +0800507 err = tfm_core_has_read_access_to_region(buffer, len, ns_caller,
508 privileged);
Summer Qin424d4db2019-03-25 14:09:51 +0800509 }
Summer Qin0fc3f592019-04-11 16:00:10 +0800510 if (err == TFM_SUCCESS) {
Summer Qin424d4db2019-03-25 14:09:51 +0800511 return IPC_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800512 }
513
514 return IPC_ERROR_MEMORY_CHECK;
515}
516
Edison Ai764d41f2018-09-21 15:56:36 +0800517/********************** SPM functions for thread mode ************************/
518
Ken Liuce2692d2020-02-11 12:39:36 +0800519uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800520{
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800521 uint32_t i, j, num;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800522 struct spm_partition_desc_t *partition;
Summer Qin66f1e032020-01-06 15:40:03 +0800523 struct tfm_core_thread_t *pth, *p_ns_entry_thread = NULL;
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100524 const struct tfm_spm_partition_platform_data_t **platform_data_p;
Edison Ai764d41f2018-09-21 15:56:36 +0800525
526 tfm_pool_init(conn_handle_pool,
527 POOL_BUFFER_SIZE(conn_handle_pool),
528 sizeof(struct tfm_conn_handle_t),
529 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800530
531 /* Init partition first for it will be used when init service */
Mate Toth-Pal3ad2e3e2019-07-11 21:43:37 +0200532 for (i = 0; i < g_spm_partition_db.partition_count; i++) {
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800533 partition = &g_spm_partition_db.partitions[i];
Edison Aif0501702019-10-11 14:36:42 +0800534
535 /* Check if the PSA framework version matches. */
536 if (partition->static_data->psa_framework_version !=
537 PSA_FRAMEWORK_VERSION) {
538 ERROR_MSG("Warning: PSA Framework Verison is not matched!");
539 continue;
540 }
541
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100542 platform_data_p = partition->platform_data_list;
543 if (platform_data_p != NULL) {
544 while ((*platform_data_p) != NULL) {
Mate Toth-Pal5e6d0342019-11-22 11:43:20 +0100545 tfm_spm_hal_configure_default_isolation(i, *platform_data_p);
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100546 ++platform_data_p;
547 }
548 }
549
Edison Ai764d41f2018-09-21 15:56:36 +0800550 if ((tfm_spm_partition_get_flags(i) & SPM_PART_FLAG_IPC) == 0) {
551 continue;
552 }
Ken Liu35f89392019-03-14 14:51:05 +0800553
Shawn Shanc7dda0e2019-12-23 14:45:09 +0800554 /* Add PSA_DOORBELL signal to assigned_signals */
555 partition->runtime_data.assigned_signals |= PSA_DOORBELL;
556
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800557 /* TODO: This can be optimized by generating the assigned signal
558 * in code generation time.
559 */
560 for (j = 0; j < tfm_core_irq_signals_count; ++j) {
561 if (tfm_core_irq_signals[j].partition_id ==
562 partition->static_data->partition_id) {
563 partition->runtime_data.assigned_signals |=
564 tfm_core_irq_signals[j].signal_value;
565 }
566 }
567
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800568 tfm_event_init(&partition->runtime_data.signal_evnt);
569 tfm_list_init(&partition->runtime_data.service_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800570
Mingyang Sunf3d29892019-07-10 17:50:23 +0800571 pth = tfm_spm_partition_get_thread_info(i);
Edison Ai764d41f2018-09-21 15:56:36 +0800572 if (!pth) {
Edison Ai9059ea02019-11-28 13:46:14 +0800573 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800574 }
575
Summer Qin66f1e032020-01-06 15:40:03 +0800576 tfm_core_thrd_init(pth,
577 tfm_spm_partition_get_init_func(i),
578 NULL,
579 (uintptr_t)tfm_spm_partition_get_stack_top(i),
580 (uintptr_t)tfm_spm_partition_get_stack_bottom(i));
Edison Ai788bae22019-02-18 17:38:59 +0800581
Mingyang Sunf3d29892019-07-10 17:50:23 +0800582 pth->prior = tfm_spm_partition_get_priority(i);
Edison Ai764d41f2018-09-21 15:56:36 +0800583
Ken Liu490281d2019-12-30 15:55:26 +0800584 if (partition->static_data->partition_id == TFM_SP_NON_SECURE_ID) {
585 p_ns_entry_thread = pth;
Ken Liu5248af22019-12-29 12:47:13 +0800586 pth->param = (void *)tfm_spm_hal_get_ns_entry_point();
Ken Liu490281d2019-12-30 15:55:26 +0800587 }
588
Edison Ai764d41f2018-09-21 15:56:36 +0800589 /* Kick off */
Summer Qin66f1e032020-01-06 15:40:03 +0800590 if (tfm_core_thrd_start(pth) != THRD_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800591 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800592 }
593 }
594
595 /* Init Service */
Summer Qind99509f2019-08-02 17:36:58 +0800596 num = sizeof(service) / sizeof(struct tfm_spm_service_t);
Edison Ai764d41f2018-09-21 15:56:36 +0800597 for (i = 0; i < num; i++) {
Summer Qine578c5b2019-08-16 16:42:16 +0800598 service[i].service_db = &service_db[i];
Edison Ai764d41f2018-09-21 15:56:36 +0800599 partition =
Summer Qine578c5b2019-08-16 16:42:16 +0800600 tfm_spm_get_partition_by_id(service[i].service_db->partition_id);
Edison Ai764d41f2018-09-21 15:56:36 +0800601 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800602 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800603 }
Summer Qind99509f2019-08-02 17:36:58 +0800604 service[i].partition = partition;
Jaykumar Pitambarbhai Patel0c7a0382020-01-09 15:25:58 +0530605 partition->runtime_data.assigned_signals |= service[i].service_db->signal;
Shawn Shan9b0e0c72019-10-22 13:43:07 +0800606
Summer Qind99509f2019-08-02 17:36:58 +0800607 tfm_list_init(&service[i].handle_list);
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800608 tfm_list_add_tail(&partition->runtime_data.service_list,
Summer Qind99509f2019-08-02 17:36:58 +0800609 &service[i].list);
Edison Ai764d41f2018-09-21 15:56:36 +0800610 }
611
Ken Liu483f5da2019-04-24 10:45:21 +0800612 /*
613 * All threads initialized, start the scheduler.
614 *
615 * NOTE:
Ken Liu490281d2019-12-30 15:55:26 +0800616 * It is worthy to give the thread object to scheduler if the background
617 * context belongs to one of the threads. Here the background thread is the
618 * initialization thread who calls SPM SVC, which re-uses the non-secure
619 * entry thread's stack. After SPM initialization is done, this stack is
620 * cleaned up and the background context is never going to return. Tell
621 * the scheduler that the current thread is non-secure entry thread.
Ken Liu483f5da2019-04-24 10:45:21 +0800622 */
Summer Qin66f1e032020-01-06 15:40:03 +0800623 tfm_core_thrd_start_scheduler(p_ns_entry_thread);
Ken Liuce2692d2020-02-11 12:39:36 +0800624
Summer Qind2ad7e72020-01-06 18:16:35 +0800625 return p_ns_entry_thread->arch_ctx.lr;
Edison Ai764d41f2018-09-21 15:56:36 +0800626}
Ken Liu2d175172019-03-21 17:08:41 +0800627
Summer Qind2ad7e72020-01-06 18:16:35 +0800628void tfm_pendsv_do_schedule(struct tfm_arch_ctx_t *p_actx)
Ken Liu2d175172019-03-21 17:08:41 +0800629{
630#if TFM_LVL == 2
631 struct spm_partition_desc_t *p_next_partition;
Summer Qinb5da9cc2019-08-26 15:19:45 +0800632 struct spm_partition_runtime_data_t *r_data;
Ken Liu2d175172019-03-21 17:08:41 +0800633 uint32_t is_privileged;
634#endif
Summer Qin66f1e032020-01-06 15:40:03 +0800635 struct tfm_core_thread_t *pth_next = tfm_core_thrd_get_next_thread();
636 struct tfm_core_thread_t *pth_curr = tfm_core_thrd_get_curr_thread();
Ken Liu2d175172019-03-21 17:08:41 +0800637
Mate Toth-Pal32b2ccd2019-04-26 10:00:16 +0200638 if (pth_next != NULL && pth_curr != pth_next) {
Ken Liu2d175172019-03-21 17:08:41 +0800639#if TFM_LVL == 2
Summer Qinb5da9cc2019-08-26 15:19:45 +0800640 r_data = TFM_GET_CONTAINER_PTR(pth_next,
641 struct spm_partition_runtime_data_t,
642 sp_thrd);
643 p_next_partition = TFM_GET_CONTAINER_PTR(r_data,
Ken Liu2d175172019-03-21 17:08:41 +0800644 struct spm_partition_desc_t,
Summer Qinb5da9cc2019-08-26 15:19:45 +0800645 runtime_data);
Ken Liu2d175172019-03-21 17:08:41 +0800646
Summer Qin423dbef2019-08-22 15:59:35 +0800647 if (p_next_partition->static_data->partition_flags &
Ken Liu2d175172019-03-21 17:08:41 +0800648 SPM_PART_FLAG_PSA_ROT) {
649 is_privileged = TFM_PARTITION_PRIVILEGED_MODE;
650 } else {
651 is_privileged = TFM_PARTITION_UNPRIVILEGED_MODE;
652 }
653
654 tfm_spm_partition_change_privilege(is_privileged);
655#endif
Mate Toth-Palc430b992019-05-09 21:01:14 +0200656
Summer Qind2ad7e72020-01-06 18:16:35 +0800657 tfm_core_thrd_switch_context(p_actx, pth_curr, pth_next);
Ken Liu2d175172019-03-21 17:08:41 +0800658 }
David Hufb38d562019-09-23 15:58:34 +0800659
660 /*
661 * Handle pending mailbox message from NS in multi-core topology.
662 * Empty operation on single Armv8-M platform.
663 */
664 tfm_rpc_client_call_handler();
Ken Liu2d175172019-03-21 17:08:41 +0800665}