blob: 245ce43297f8b6a4f2d41cc7d1356619f9cb1b59 [file] [log] [blame]
Edison Ai764d41f2018-09-21 15:56:36 +08001/*
Kevin Penga20b5af2021-01-11 11:20:52 +08002 * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
Chris Brand56b4d0c2021-12-17 16:15:58 -08003 * Copyright (c) 2021, Cypress Semiconductor Corporation. All rights reserved.
Edison Ai764d41f2018-09-21 15:56:36 +08004 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 *
7 */
Mingyang Sunda01a972019-07-12 17:32:59 +08008
Edison Ai764d41f2018-09-21 15:56:36 +08009#include <inttypes.h>
10#include <stdbool.h>
Ken Liu24dffb22021-02-10 11:03:58 +080011#include "bitops.h"
Ken Liu92ede9f2021-10-20 09:35:00 +080012#include "critical_section.h"
Ken Liuf39d8eb2021-10-07 12:55:33 +080013#include "current.h"
David Huf07e97d2021-02-15 22:05:40 +080014#include "fih.h"
Jamie Foxcc31d402019-01-28 17:13:52 +000015#include "psa/client.h"
16#include "psa/service.h"
Ken Liu5d73c872021-08-19 19:23:17 +080017#include "thread.h"
Ken Liubcae38b2021-01-20 15:47:44 +080018#include "internal_errors.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080019#include "tfm_spm_hal.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080020#include "tfm_api.h"
21#include "tfm_secure_api.h"
22#include "tfm_memory_utils.h"
Mingyang Sund1ed6732020-08-26 15:52:21 +080023#include "tfm_hal_defs.h"
Kevin Pengd399a1f2021-09-08 15:33:14 +080024#include "tfm_hal_interrupt.h"
Mingyang Sund1ed6732020-08-26 15:52:21 +080025#include "tfm_hal_isolation.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080026#include "spm_ipc.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080027#include "tfm_peripherals_def.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080028#include "tfm_core_utils.h"
Kevin Peng385fda82021-08-18 10:41:19 +080029#include "tfm_nspm.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080030#include "tfm_rpc.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080031#include "tfm_core_trustzone.h"
Ken Liu24dffb22021-02-10 11:03:58 +080032#include "lists.h"
Edison Ai764d41f2018-09-21 15:56:36 +080033#include "tfm_pools.h"
Mingyang Sun22a3faf2021-07-09 15:32:47 +080034#include "region.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080035#include "psa_manifest/pid.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080036#include "ffm/backend.h"
Mingyang Sun00df2352021-04-15 15:46:08 +080037#include "load/partition_defs.h"
38#include "load/service_defs.h"
Ken Liu86686282021-04-27 11:11:15 +080039#include "load/asset_defs.h"
Ken Liuacd2a572021-05-12 16:19:04 +080040#include "load/spm_load_api.h"
Sherry Zhnag482b88b2021-08-19 17:51:47 +080041#include "tfm_nspm.h"
Edison Ai764d41f2018-09-21 15:56:36 +080042
Ken Liuea45b0d2021-05-22 17:41:25 +080043/* Partition and service runtime data list head/runtime data table */
Ken Liuea45b0d2021-05-22 17:41:25 +080044static struct service_head_t services_listhead;
Ken Liub3b2cb62021-05-22 00:39:28 +080045struct service_t *stateless_services_ref_tbl[STATIC_HANDLE_NUM_LIMIT];
Summer Qind99509f2019-08-02 17:36:58 +080046
Edison Ai764d41f2018-09-21 15:56:36 +080047/* Pools */
48TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t),
49 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +080050
Mingyang Sun620c8562021-11-10 11:44:58 +080051extern uint32_t scheduler_lock;
52
Summer Qin373feb12020-03-27 15:35:33 +080053/*********************** Connection handle conversion APIs *******************/
54
Summer Qin373feb12020-03-27 15:35:33 +080055#define CONVERSION_FACTOR_BITOFFSET 3
56#define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET)
57/* Set 32 as the maximum */
58#define CONVERSION_FACTOR_VALUE_MAX 0x20
59
60#if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX
61#error "CONVERSION FACTOR OUT OF RANGE"
62#endif
63
64static uint32_t loop_index;
65
66/*
67 * A handle instance psa_handle_t allocated inside SPM is actually a memory
68 * address among the handle pool. Return this handle to the client directly
69 * exposes information of secure memory address. In this case, converting the
70 * handle into another value does not represent the memory address to avoid
71 * exposing secure memory directly to clients.
72 *
73 * This function converts the handle instance into another value by scaling the
74 * handle in pool offset, the converted value is named as a user handle.
75 *
76 * The formula:
77 * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE +
78 * CLIENT_HANDLE_VALUE_MIN + loop_index
79 * where:
80 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
81 * exceed CONVERSION_FACTOR_VALUE_MAX.
82 *
83 * handle_instance in RANGE[POOL_START, POOL_END]
84 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
85 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
86 *
87 * note:
88 * loop_index is used to promise same handle instance is converted into
89 * different user handles in short time.
90 */
Ken Liu505b1702020-05-29 13:19:58 +080091psa_handle_t tfm_spm_to_user_handle(struct tfm_conn_handle_t *handle_instance)
Summer Qin373feb12020-03-27 15:35:33 +080092{
93 psa_handle_t user_handle;
94
95 loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE;
96 user_handle = (psa_handle_t)((((uintptr_t)handle_instance -
97 (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) +
98 CLIENT_HANDLE_VALUE_MIN + loop_index);
99
100 return user_handle;
101}
102
103/*
104 * This function converts a user handle into a corresponded handle instance.
105 * The converted value is validated before returning, an invalid handle instance
106 * is returned as NULL.
107 *
108 * The formula:
109 * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) /
110 * CONVERSION_FACTOR_VALUE) + POOL_START
111 * where:
112 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
113 * exceed CONVERSION_FACTOR_VALUE_MAX.
114 *
115 * handle_instance in RANGE[POOL_START, POOL_END]
116 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
117 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
118 */
119struct tfm_conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle)
120{
121 struct tfm_conn_handle_t *handle_instance;
122
123 if (user_handle == PSA_NULL_HANDLE) {
124 return NULL;
125 }
126
127 handle_instance = (struct tfm_conn_handle_t *)((((uintptr_t)user_handle -
128 CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) +
129 (uintptr_t)conn_handle_pool);
130
131 return handle_instance;
132}
133
Edison Ai764d41f2018-09-21 15:56:36 +0800134/* Service handle management functions */
Mingyang Sun783a59b2021-04-20 15:52:18 +0800135struct tfm_conn_handle_t *tfm_spm_create_conn_handle(struct service_t *service,
136 int32_t client_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800137{
Edison Ai9cc26242019-08-06 11:28:04 +0800138 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800139
Ken Liuf250b8b2019-12-27 16:31:24 +0800140 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800141
142 /* Get buffer for handle list structure from handle pool */
Edison Ai9cc26242019-08-06 11:28:04 +0800143 p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
144 if (!p_handle) {
Summer Qin630c76b2020-05-20 10:32:58 +0800145 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800146 }
147
Kevin Peng2cc25722021-11-18 11:46:02 +0800148 spm_memset(p_handle, 0, sizeof(*p_handle));
149
Ken Liuf39d8eb2021-10-07 12:55:33 +0800150 p_handle->internal_msg.service = service;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800151 p_handle->status = TFM_HANDLE_STATUS_IDLE;
Summer Qin1ce712a2019-10-14 18:04:05 +0800152 p_handle->client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800153
154 /* Add handle node to list for next psa functions */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800155 BI_LIST_INSERT_BEFORE(&service->handle_list, &p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800156
Summer Qin630c76b2020-05-20 10:32:58 +0800157 return p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800158}
159
Summer Qin630c76b2020-05-20 10:32:58 +0800160int32_t tfm_spm_validate_conn_handle(
161 const struct tfm_conn_handle_t *conn_handle,
162 int32_t client_id)
Summer Qin1ce712a2019-10-14 18:04:05 +0800163{
164 /* Check the handle address is validated */
165 if (is_valid_chunk_data_in_pool(conn_handle_pool,
166 (uint8_t *)conn_handle) != true) {
Ken Liubcae38b2021-01-20 15:47:44 +0800167 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800168 }
169
170 /* Check the handle caller is correct */
Summer Qin630c76b2020-05-20 10:32:58 +0800171 if (conn_handle->client_id != client_id) {
Ken Liubcae38b2021-01-20 15:47:44 +0800172 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800173 }
174
Ken Liubcae38b2021-01-20 15:47:44 +0800175 return SPM_SUCCESS;
Summer Qin1ce712a2019-10-14 18:04:05 +0800176}
177
Mingyang Sun783a59b2021-04-20 15:52:18 +0800178int32_t tfm_spm_free_conn_handle(struct service_t *service,
Summer Qin02f7f072020-08-24 16:02:54 +0800179 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800180{
Mingyang Sun620c8562021-11-10 11:44:58 +0800181 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
182
Ken Liuf250b8b2019-12-27 16:31:24 +0800183 TFM_CORE_ASSERT(service);
Summer Qin630c76b2020-05-20 10:32:58 +0800184 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800185
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200186 /* Clear magic as the handler is not used anymore */
Summer Qin630c76b2020-05-20 10:32:58 +0800187 conn_handle->internal_msg.magic = 0;
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200188
Mingyang Sun620c8562021-11-10 11:44:58 +0800189 CRITICAL_SECTION_ENTER(cs_assert);
Edison Ai764d41f2018-09-21 15:56:36 +0800190 /* Remove node from handle list */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800191 BI_LIST_REMOVE_NODE(&conn_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800192
193 /* Back handle buffer to pool */
Ken Liu66ca6132021-02-24 08:49:51 +0800194 tfm_pool_free(conn_handle_pool, conn_handle);
Mingyang Sun620c8562021-11-10 11:44:58 +0800195 CRITICAL_SECTION_LEAVE(cs_assert);
196
Ken Liubcae38b2021-01-20 15:47:44 +0800197 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800198}
199
Mingyang Sun783a59b2021-04-20 15:52:18 +0800200int32_t tfm_spm_set_rhandle(struct service_t *service,
Summer Qin02f7f072020-08-24 16:02:54 +0800201 struct tfm_conn_handle_t *conn_handle,
202 void *rhandle)
Edison Ai764d41f2018-09-21 15:56:36 +0800203{
Ken Liuf250b8b2019-12-27 16:31:24 +0800204 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800205 /* Set reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800206 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800207
Summer Qin630c76b2020-05-20 10:32:58 +0800208 conn_handle->rhandle = rhandle;
Ken Liubcae38b2021-01-20 15:47:44 +0800209 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800210}
211
Mingyang Sund44522a2020-01-16 16:48:37 +0800212/**
Xinyu Zhang3a453242021-04-16 17:57:09 +0800213 * \brief Get reverse handle value from connection handle.
Mingyang Sund44522a2020-01-16 16:48:37 +0800214 *
215 * \param[in] service Target service context pointer
216 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800217 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800218 *
219 * \retval void * Success
220 * \retval "Does not return" Panic for those:
221 * service pointer are NULL
Xinyu Zhang3a453242021-04-16 17:57:09 +0800222 * handle is \ref PSA_NULL_HANDLE
Mingyang Sund44522a2020-01-16 16:48:37 +0800223 * handle node does not be found
224 */
Mingyang Sun783a59b2021-04-20 15:52:18 +0800225static void *tfm_spm_get_rhandle(struct service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800226 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800227{
Ken Liuf250b8b2019-12-27 16:31:24 +0800228 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800229 /* Get reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800230 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800231
Summer Qin630c76b2020-05-20 10:32:58 +0800232 return conn_handle->rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800233}
234
235/* Partition management functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800236
Summer Qin02f7f072020-08-24 16:02:54 +0800237struct tfm_msg_body_t *tfm_spm_get_msg_by_signal(struct partition_t *partition,
238 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800239{
Ken Liu2c47f7f2021-01-22 11:06:04 +0800240 struct bi_list_node_t *node, *head;
Mingyang Sun73056b62020-07-03 15:18:46 +0800241 struct tfm_msg_body_t *tmp_msg, *msg = NULL;
Ken Liu92ede9f2021-10-20 09:35:00 +0800242 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Edison Ai764d41f2018-09-21 15:56:36 +0800243
Ken Liuf250b8b2019-12-27 16:31:24 +0800244 TFM_CORE_ASSERT(partition);
Edison Ai764d41f2018-09-21 15:56:36 +0800245
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800246 head = &partition->msg_list;
Mingyang Sun73056b62020-07-03 15:18:46 +0800247
Ken Liu2c47f7f2021-01-22 11:06:04 +0800248 if (BI_LIST_IS_EMPTY(head)) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800249 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800250 }
251
Mingyang Sun73056b62020-07-03 15:18:46 +0800252 /*
253 * There may be multiple messages for this RoT Service signal, do not clear
254 * partition mask until no remaining message. Search may be optimized.
255 */
Mingyang Sun620c8562021-11-10 11:44:58 +0800256 CRITICAL_SECTION_ENTER(cs_assert);
Ken Liu2c47f7f2021-01-22 11:06:04 +0800257 BI_LIST_FOR_EACH(node, head) {
Ken Liuc25558c2021-05-20 15:31:28 +0800258 tmp_msg = TO_CONTAINER(node, struct tfm_msg_body_t, msg_node);
Ken Liuacd2a572021-05-12 16:19:04 +0800259 if (tmp_msg->service->p_ldinf->signal == signal && msg) {
Mingyang Sun620c8562021-11-10 11:44:58 +0800260 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sun73056b62020-07-03 15:18:46 +0800261 return msg;
Ken Liuacd2a572021-05-12 16:19:04 +0800262 } else if (tmp_msg->service->p_ldinf->signal == signal) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800263 msg = tmp_msg;
Ken Liu2c47f7f2021-01-22 11:06:04 +0800264 BI_LIST_REMOVE_NODE(node);
Edison Ai764d41f2018-09-21 15:56:36 +0800265 }
266 }
Mingyang Sun73056b62020-07-03 15:18:46 +0800267
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800268 partition->signals_asserted &= ~signal;
Ken Liu92ede9f2021-10-20 09:35:00 +0800269 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sun73056b62020-07-03 15:18:46 +0800270
271 return msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800272}
273
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800274uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
275{
Kevin Pengbf9a97e2021-06-18 16:34:12 +0800276#if TFM_LVL == 1
277 return TFM_PARTITION_PRIVILEGED_MODE;
278#else /* TFM_LVL == 1 */
Shawn Shana3657e82021-10-18 14:37:09 +0800279 if (partition_flags & PARTITION_MODEL_PSA_ROT) {
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800280 return TFM_PARTITION_PRIVILEGED_MODE;
281 } else {
282 return TFM_PARTITION_UNPRIVILEGED_MODE;
283 }
Kevin Pengbf9a97e2021-06-18 16:34:12 +0800284#endif /* TFM_LVL == 1 */
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800285}
286
Mingyang Sun783a59b2021-04-20 15:52:18 +0800287struct service_t *tfm_spm_get_service_by_sid(uint32_t sid)
Edison Ai764d41f2018-09-21 15:56:36 +0800288{
Feder.Liangaeb5a792021-08-10 16:12:56 +0800289 struct service_t *p_prev, *p_curr;
Edison Ai764d41f2018-09-21 15:56:36 +0800290
Feder.Liangaeb5a792021-08-10 16:12:56 +0800291 UNI_LIST_FOR_EACH_PREV(p_prev, p_curr, &services_listhead) {
292 if (p_curr->p_ldinf->sid == sid) {
293 UNI_LIST_MOVE_AFTER(&services_listhead, p_prev, p_curr);
294 return p_curr;
Mingyang Sunef42f442021-06-11 15:07:58 +0800295 }
296 }
297
Ken Liuea45b0d2021-05-22 17:41:25 +0800298 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800299}
300
Mingyang Sund44522a2020-01-16 16:48:37 +0800301/**
302 * \brief Get the partition context by partition ID.
303 *
304 * \param[in] partition_id Partition identity
305 *
306 * \retval NULL Failed
307 * \retval "Not NULL" Target partition context pointer,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800308 * \ref partition_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800309 */
Kevin Pengeca45b92021-02-09 14:46:50 +0800310struct partition_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800311{
Ken Liuea45b0d2021-05-22 17:41:25 +0800312 struct partition_t *p_part;
Edison Ai764d41f2018-09-21 15:56:36 +0800313
Mingyang Sundeae45d2021-09-06 15:31:07 +0800314 UNI_LIST_FOR_EACH(p_part, PARTITION_LIST_ADDR) {
Ken Liuea45b0d2021-05-22 17:41:25 +0800315 if (p_part->p_ldinf->pid == partition_id) {
316 return p_part;
317 }
Edison Ai764d41f2018-09-21 15:56:36 +0800318 }
Ken Liuea45b0d2021-05-22 17:41:25 +0800319
Edison Ai764d41f2018-09-21 15:56:36 +0800320 return NULL;
321}
322
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800323struct partition_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800324{
Ken Liuf39d8eb2021-10-07 12:55:33 +0800325 return GET_CURRENT_COMPONENT();
Edison Ai764d41f2018-09-21 15:56:36 +0800326}
327
Mingyang Sun783a59b2021-04-20 15:52:18 +0800328int32_t tfm_spm_check_client_version(struct service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530329 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800330{
Ken Liuf250b8b2019-12-27 16:31:24 +0800331 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800332
Ken Liuacd2a572021-05-12 16:19:04 +0800333 switch (SERVICE_GET_VERSION_POLICY(service->p_ldinf->flags)) {
Ken Liub3b2cb62021-05-22 00:39:28 +0800334 case SERVICE_VERSION_POLICY_RELAXED:
Ken Liuacd2a572021-05-12 16:19:04 +0800335 if (version > service->p_ldinf->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800336 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800337 }
338 break;
Ken Liub3b2cb62021-05-22 00:39:28 +0800339 case SERVICE_VERSION_POLICY_STRICT:
Ken Liuacd2a572021-05-12 16:19:04 +0800340 if (version != service->p_ldinf->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800341 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800342 }
343 break;
344 default:
Ken Liubcae38b2021-01-20 15:47:44 +0800345 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800346 }
Ken Liubcae38b2021-01-20 15:47:44 +0800347 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800348}
349
Edison Aie728fbf2019-11-13 09:37:12 +0800350int32_t tfm_spm_check_authorization(uint32_t sid,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800351 struct service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800352 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800353{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800354 struct partition_t *partition = NULL;
Mingyang Sun2b352662021-04-21 11:35:43 +0800355 uint32_t *dep;
Edison Aie728fbf2019-11-13 09:37:12 +0800356 int32_t i;
357
Ken Liuf250b8b2019-12-27 16:31:24 +0800358 TFM_CORE_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800359
360 if (ns_caller) {
Ken Liuacd2a572021-05-12 16:19:04 +0800361 if (!SERVICE_IS_NS_ACCESSIBLE(service->p_ldinf->flags)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800362 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800363 }
364 } else {
365 partition = tfm_spm_get_running_partition();
366 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800367 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800368 }
369
Ken Liuacd2a572021-05-12 16:19:04 +0800370 dep = (uint32_t *)LOAD_INFO_DEPS(partition->p_ldinf);
371 for (i = 0; i < partition->p_ldinf->ndeps; i++) {
Mingyang Sun2b352662021-04-21 11:35:43 +0800372 if (dep[i] == sid) {
Edison Aie728fbf2019-11-13 09:37:12 +0800373 break;
374 }
375 }
376
Ken Liuacd2a572021-05-12 16:19:04 +0800377 if (i == partition->p_ldinf->ndeps) {
Ken Liubcae38b2021-01-20 15:47:44 +0800378 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800379 }
380 }
Ken Liubcae38b2021-01-20 15:47:44 +0800381 return SPM_SUCCESS;
Edison Aie728fbf2019-11-13 09:37:12 +0800382}
383
Edison Ai764d41f2018-09-21 15:56:36 +0800384/* Message functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800385
Summer Qin02f7f072020-08-24 16:02:54 +0800386struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800387{
388 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200389 * The message handler passed by the caller is considered invalid in the
390 * following cases:
391 * 1. Not a valid message handle. (The address of a message is not the
392 * address of a possible handle from the pool
393 * 2. Handle not belongs to the caller partition (The handle is either
394 * unused, or owned by anither partition)
395 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800396 */
Ken Liu505b1702020-05-29 13:19:58 +0800397 struct tfm_msg_body_t *p_msg;
Kevin Pengeec41a82021-08-18 13:56:23 +0800398 int32_t partition_id;
Ken Liu505b1702020-05-29 13:19:58 +0800399 struct tfm_conn_handle_t *p_conn_handle =
Ken Liu5d73c872021-08-19 19:23:17 +0800400 tfm_spm_to_handle_instance(msg_handle);
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200401
402 if (is_valid_chunk_data_in_pool(
Ken Liu505b1702020-05-29 13:19:58 +0800403 conn_handle_pool, (uint8_t *)p_conn_handle) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800404 return NULL;
405 }
406
Ken Liu505b1702020-05-29 13:19:58 +0800407 p_msg = &p_conn_handle->internal_msg;
408
Edison Ai764d41f2018-09-21 15:56:36 +0800409 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200410 * Check that the magic number is correct. This proves that the message
411 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800412 */
Ken Liu505b1702020-05-29 13:19:58 +0800413 if (p_msg->magic != TFM_MSG_MAGIC) {
Edison Ai764d41f2018-09-21 15:56:36 +0800414 return NULL;
415 }
416
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200417 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800418 partition_id = tfm_spm_partition_get_running_partition_id();
Ken Liuacd2a572021-05-12 16:19:04 +0800419 if (partition_id != p_msg->service->partition->p_ldinf->pid) {
Edison Ai764d41f2018-09-21 15:56:36 +0800420 return NULL;
421 }
422
Ken Liu505b1702020-05-29 13:19:58 +0800423 return p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800424}
425
Kevin Pengdf6aa292021-03-11 17:58:50 +0800426struct tfm_msg_body_t *
427 tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle)
428{
429 TFM_CORE_ASSERT(conn_handle != NULL);
430
431 return &(conn_handle->internal_msg);
432}
433
Edison Ai97115822019-08-01 14:22:19 +0800434void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800435 struct service_t *service,
Ken Liu505b1702020-05-29 13:19:58 +0800436 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800437 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800438 psa_invec *invec, size_t in_len,
439 psa_outvec *outvec, size_t out_len,
440 psa_outvec *caller_outvec)
441{
Edison Ai764d41f2018-09-21 15:56:36 +0800442 uint32_t i;
Ken Liu505b1702020-05-29 13:19:58 +0800443 struct tfm_conn_handle_t *conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800444
Ken Liuf250b8b2019-12-27 16:31:24 +0800445 TFM_CORE_ASSERT(msg);
446 TFM_CORE_ASSERT(service);
447 TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
448 TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
449 TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
450 TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
451 TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800452
Edison Ai764d41f2018-09-21 15:56:36 +0800453 /* Clear message buffer before using it */
Ken Liuf39d8eb2021-10-07 12:55:33 +0800454 spm_memset(&msg->msg, 0, sizeof(psa_msg_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800455
Ken Liu5d73c872021-08-19 19:23:17 +0800456 THRD_SYNC_INIT(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800457 msg->magic = TFM_MSG_MAGIC;
458 msg->service = service;
Ken Liuf39d8eb2021-10-07 12:55:33 +0800459 msg->p_client = GET_CURRENT_COMPONENT();
Edison Ai764d41f2018-09-21 15:56:36 +0800460 msg->caller_outvec = caller_outvec;
Summer Qin1ce712a2019-10-14 18:04:05 +0800461 msg->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800462
463 /* Copy contents */
464 msg->msg.type = type;
465
466 for (i = 0; i < in_len; i++) {
467 msg->msg.in_size[i] = invec[i].len;
468 msg->invec[i].base = invec[i].base;
469 }
470
471 for (i = 0; i < out_len; i++) {
472 msg->msg.out_size[i] = outvec[i].len;
473 msg->outvec[i].base = outvec[i].base;
474 /* Out len is used to record the writed number, set 0 here again */
475 msg->outvec[i].len = 0;
476 }
477
Ken Liu505b1702020-05-29 13:19:58 +0800478 /* Use the user connect handle as the message handle */
479 msg->msg.handle = handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800480
Ken Liu505b1702020-05-29 13:19:58 +0800481 conn_handle = tfm_spm_to_handle_instance(handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800482 /* For connected handle, set rhandle to every message */
Ken Liu505b1702020-05-29 13:19:58 +0800483 if (conn_handle) {
484 msg->msg.rhandle = tfm_spm_get_rhandle(service, conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800485 }
David Hu46603dd2019-12-11 18:05:16 +0800486
487 /* Set the private data of NSPE client caller in multi-core topology */
488 if (TFM_CLIENT_ID_IS_NS(client_id)) {
489 tfm_rpc_set_caller_data(msg, client_id);
490 }
Edison Ai764d41f2018-09-21 15:56:36 +0800491}
492
Kevin Pengeec41a82021-08-18 13:56:23 +0800493int32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800494{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800495 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800496
Kevin Peng79c2bda2020-07-24 16:31:12 +0800497 partition = tfm_spm_get_running_partition();
Ken Liuacd2a572021-05-12 16:19:04 +0800498 if (partition && partition->p_ldinf) {
499 return partition->p_ldinf->pid;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800500 } else {
501 return INVALID_PARTITION_ID;
502 }
Edison Ai764d41f2018-09-21 15:56:36 +0800503}
504
Summer Qin43c185d2019-10-10 15:44:42 +0800505int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800506 enum tfm_memory_access_e access,
507 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800508{
Mingyang Sund1ed6732020-08-26 15:52:21 +0800509 enum tfm_hal_status_t err;
510 uint32_t attr = 0;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800511
512 /* If len is zero, this indicates an empty buffer and base is ignored */
513 if (len == 0) {
Ken Liubcae38b2021-01-20 15:47:44 +0800514 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800515 }
516
517 if (!buffer) {
Ken Liubcae38b2021-01-20 15:47:44 +0800518 return SPM_ERROR_BAD_PARAMETERS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800519 }
520
521 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800522 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800523 }
524
Summer Qin424d4db2019-03-25 14:09:51 +0800525 if (access == TFM_MEMORY_ACCESS_RW) {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800526 attr |= (TFM_HAL_ACCESS_READABLE | TFM_HAL_ACCESS_WRITABLE);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800527 } else {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800528 attr |= TFM_HAL_ACCESS_READABLE;
Summer Qin424d4db2019-03-25 14:09:51 +0800529 }
Mingyang Sund1ed6732020-08-26 15:52:21 +0800530
531 if (privileged == TFM_PARTITION_UNPRIVILEGED_MODE) {
532 attr |= TFM_HAL_ACCESS_UNPRIVILEGED;
533 } else {
534 attr &= ~TFM_HAL_ACCESS_UNPRIVILEGED;
535 }
536
537 if (ns_caller) {
538 attr |= TFM_HAL_ACCESS_NS;
539 }
540
541 err = tfm_hal_memory_has_access((uintptr_t)buffer, len, attr);
542
543 if (err == TFM_HAL_SUCCESS) {
Ken Liubcae38b2021-01-20 15:47:44 +0800544 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800545 }
546
Ken Liubcae38b2021-01-20 15:47:44 +0800547 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800548}
549
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800550bool tfm_spm_is_ns_caller(void)
551{
Mingyang Sunc1805432021-11-22 18:56:59 +0800552#if defined(TFM_MULTI_CORE_TOPOLOGY)
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800553 /* Multi-core NS PSA API request is processed by pendSV. */
554 return (__get_active_exc_num() == EXC_NUM_PENDSV);
555#else
556 struct partition_t *partition = tfm_spm_get_running_partition();
Mingyang Sune529e3b2021-07-12 14:46:30 +0800557
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800558 if (!partition) {
559 tfm_core_panic();
560 }
561
562 return (partition->p_ldinf->pid == TFM_SP_NON_SECURE_ID);
563#endif
564}
565
Mingyang Sune529e3b2021-07-12 14:46:30 +0800566uint32_t tfm_spm_get_caller_privilege_mode(void)
567{
568 struct partition_t *partition;
569
570#if defined(TFM_MULTI_CORE_TOPOLOGY) || defined(FORWARD_PROT_MSG)
571 /*
572 * In multi-core topology, if PSA request is from mailbox, the client
573 * is unprivileged.
574 */
575 if (__get_active_exc_num() == EXC_NUM_PENDSV) {
576 return TFM_PARTITION_UNPRIVILEGED_MODE;
577 }
578#endif
579 partition = tfm_spm_get_running_partition();
580 if (!partition) {
581 tfm_core_panic();
582 }
583
584 return tfm_spm_partition_get_privileged_mode(partition->p_ldinf->flags);
585}
586
Kevin Peng385fda82021-08-18 10:41:19 +0800587int32_t tfm_spm_get_client_id(bool ns_caller)
588{
589 int32_t client_id;
590
591 if (ns_caller) {
592 client_id = tfm_nspm_get_current_client_id();
593 } else {
594 client_id = tfm_spm_partition_get_running_partition_id();
595 }
596
597 if (ns_caller != (client_id < 0)) {
598 /* NS client ID must be negative and Secure ID must >= 0 */
599 tfm_core_panic();
600 }
601
602 return client_id;
603}
604
Ken Liuce2692d2020-02-11 12:39:36 +0800605uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800606{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800607 struct partition_t *partition;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800608 const struct partition_load_info_t *p_pldi;
609 uint32_t service_setting = 0;
Ken Liuce58bfc2021-05-12 17:54:48 +0800610
David Huf07e97d2021-02-15 22:05:40 +0800611#ifdef TFM_FIH_PROFILE_ON
612 fih_int fih_rc = FIH_FAILURE;
613#endif
Edison Ai764d41f2018-09-21 15:56:36 +0800614
615 tfm_pool_init(conn_handle_pool,
616 POOL_BUFFER_SIZE(conn_handle_pool),
617 sizeof(struct tfm_conn_handle_t),
618 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800619
Mingyang Sundeae45d2021-09-06 15:31:07 +0800620 UNI_LISI_INIT_HEAD(PARTITION_LIST_ADDR);
Ken Liuea45b0d2021-05-22 17:41:25 +0800621 UNI_LISI_INIT_HEAD(&services_listhead);
622
Sherry Zhnag482b88b2021-08-19 17:51:47 +0800623 /* Init the nonsecure context. */
Chris Brand56b4d0c2021-12-17 16:15:58 -0800624 tfm_nspm_ctx_init();
Sherry Zhnag482b88b2021-08-19 17:51:47 +0800625
Ken Liuacd2a572021-05-12 16:19:04 +0800626 while (1) {
Mingyang Sundeae45d2021-09-06 15:31:07 +0800627 partition = load_a_partition_assuredly(PARTITION_LIST_ADDR);
Shawn Shan23331062021-09-01 14:58:21 +0800628 if (partition == NO_MORE_PARTITION) {
Ken Liuacd2a572021-05-12 16:19:04 +0800629 break;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800630 }
631
Mingyang Sundeae45d2021-09-06 15:31:07 +0800632 p_pldi = partition->p_ldinf;
Mingyang Sun2b352662021-04-21 11:35:43 +0800633
Mingyang Sundeae45d2021-09-06 15:31:07 +0800634 if (p_pldi->nservices) {
635 service_setting = load_services_assuredly(
636 partition,
637 &services_listhead,
638 stateless_services_ref_tbl,
639 sizeof(stateless_services_ref_tbl));
Kevin Peng27e42272021-05-24 17:58:53 +0800640 }
641
Mingyang Sundeae45d2021-09-06 15:31:07 +0800642 if (p_pldi->nirqs) {
Kevin Peng27e42272021-05-24 17:58:53 +0800643 load_irqs_assuredly(partition);
644 }
645
Mingyang Sundeae45d2021-09-06 15:31:07 +0800646 /* Bind the partition with platform. */
Ken Liuce58bfc2021-05-12 17:54:48 +0800647#if TFM_FIH_PROFILE_ON
648 FIH_CALL(tfm_hal_bind_boundaries, fih_rc, partition->p_ldinf,
Mingyang Sundeae45d2021-09-06 15:31:07 +0800649 &partition->p_boundaries);
Ken Liuce58bfc2021-05-12 17:54:48 +0800650 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
651 tfm_core_panic();
Mingyang Sun61f8fbc2021-06-04 17:49:56 +0800652 }
Ken Liu86686282021-04-27 11:11:15 +0800653#else /* TFM_FIH_PROFILE_ON */
Ken Liuce58bfc2021-05-12 17:54:48 +0800654 if (tfm_hal_bind_boundaries(partition->p_ldinf,
Mingyang Sundeae45d2021-09-06 15:31:07 +0800655 &partition->p_boundaries)
656 != TFM_HAL_SUCCESS) {
Ken Liuce58bfc2021-05-12 17:54:48 +0800657 tfm_core_panic();
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100658 }
Ken Liuce58bfc2021-05-12 17:54:48 +0800659#endif /* TFM_FIH_PROFILE_ON */
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100660
Mingyang Sundeae45d2021-09-06 15:31:07 +0800661 backend_instance.comp_init_assuredly(partition, service_setting);
Edison Ai764d41f2018-09-21 15:56:36 +0800662 }
663
Mingyang Sundeae45d2021-09-06 15:31:07 +0800664 return backend_instance.system_run();
Edison Ai764d41f2018-09-21 15:56:36 +0800665}
Ken Liu2d175172019-03-21 17:08:41 +0800666
Ken Liu5d73c872021-08-19 19:23:17 +0800667/*
668 * Return both current and next context to assembly via AAPCS trick:
669 * - Returning a 64 bit integer by 32-bit R0 and R1.
670 *
671 * This is architecture-specific, hence the scheduler entry and this
672 * 'do_schedule' MAY be different on another architecture.
673 */
674union returning_contexts_t {
675 struct {
676 uint32_t curr;
677 uint32_t next;
678 } ctx;
Ken Liu2d175172019-03-21 17:08:41 +0800679
Ken Liu5d73c872021-08-19 19:23:17 +0800680 uint64_t curr_next_ctxs;
681};
682
683uint64_t do_schedule(void)
684{
Mingyang Sun620c8562021-11-10 11:44:58 +0800685 union returning_contexts_t ret_ctx;
Ken Liu5d73c872021-08-19 19:23:17 +0800686 struct partition_t *p_part_curr, *p_part_next;
687 struct thread_t *pth_next = thrd_next();
Kevin Pengca59ec02021-12-09 14:35:50 +0800688 struct critical_section_t cs = CRITICAL_SECTION_STATIC_INIT;
Ken Liu5d73c872021-08-19 19:23:17 +0800689
Mingyang Sun620c8562021-11-10 11:44:58 +0800690 ret_ctx.ctx.curr = (uint32_t)CURRENT_THREAD->p_context_ctrl;
691 ret_ctx.ctx.next = (uint32_t)CURRENT_THREAD->p_context_ctrl;
Mingyang Sun4f08f4d2021-09-10 17:41:28 +0800692 p_part_curr = GET_THRD_OWNER(CURRENT_THREAD);
693 p_part_next = GET_THRD_OWNER(pth_next);
Ken Liu5d73c872021-08-19 19:23:17 +0800694
Mingyang Sun620c8562021-11-10 11:44:58 +0800695 if (scheduler_lock != SCHEDULER_LOCKED && pth_next != NULL &&
696 p_part_curr != p_part_next) {
Ken Liu5d73c872021-08-19 19:23:17 +0800697 /* Check if there is enough room on stack to save more context */
698 if ((p_part_curr->ctx_ctrl.sp_limit +
699 sizeof(struct tfm_additional_context_t)) > __get_PSP()) {
700 tfm_core_panic();
701 }
Mate Toth-Palc430b992019-05-09 21:01:14 +0200702
Kevin Pengca59ec02021-12-09 14:35:50 +0800703 CRITICAL_SECTION_ENTER(cs);
Ken Liuce58bfc2021-05-12 17:54:48 +0800704 /*
705 * If required, let the platform update boundary based on its
706 * implementation. Change privilege, MPU or other configurations.
707 */
708 if (p_part_curr->p_boundaries != p_part_next->p_boundaries) {
709 if (tfm_hal_update_boundaries(p_part_next->p_ldinf,
710 p_part_next->p_boundaries)
711 != TFM_HAL_SUCCESS) {
712 tfm_core_panic();
713 }
714 }
Feder Liang42f5b562021-09-10 17:38:36 +0800715 ARCH_FLUSH_FP_CONTEXT();
Mingyang Sun620c8562021-11-10 11:44:58 +0800716
717 ret_ctx.ctx.next = (uint32_t)pth_next->p_context_ctrl;
718 CURRENT_THREAD = pth_next;
Kevin Pengca59ec02021-12-09 14:35:50 +0800719 CRITICAL_SECTION_LEAVE(cs);
Ken Liu2d175172019-03-21 17:08:41 +0800720 }
David Hufb38d562019-09-23 15:58:34 +0800721
722 /*
723 * Handle pending mailbox message from NS in multi-core topology.
724 * Empty operation on single Armv8-M platform.
725 */
726 tfm_rpc_client_call_handler();
Ken Liu5d73c872021-08-19 19:23:17 +0800727
Mingyang Sun620c8562021-11-10 11:44:58 +0800728 return ret_ctx.curr_next_ctxs;
Ken Liu2d175172019-03-21 17:08:41 +0800729}
Mingyang Sund44522a2020-01-16 16:48:37 +0800730
Summer Qin02f7f072020-08-24 16:02:54 +0800731void update_caller_outvec_len(struct tfm_msg_body_t *msg)
Mingyang Sund44522a2020-01-16 16:48:37 +0800732{
733 uint32_t i;
734
735 /*
736 * FixeMe: abstract these part into dedicated functions to avoid
737 * accessing thread context in psa layer
738 */
Ken Liuf39d8eb2021-10-07 12:55:33 +0800739 /*
740 * If it is a NS request via RPC, the owner of this message is not set.
741 * Or if it is a SFN message, it does not have owner thread state either.
742 */
743 if ((!is_tfm_rpc_msg(msg)) && (msg->sfn_magic != TFM_MSG_MAGIC_SFN)) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800744 TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK);
745 }
746
747 for (i = 0; i < PSA_MAX_IOVEC; i++) {
748 if (msg->msg.out_size[i] == 0) {
749 continue;
750 }
751
752 TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base);
753
754 msg->caller_outvec[i].len = msg->outvec[i].len;
755 }
756}
757
Ken Liu5d73c872021-08-19 19:23:17 +0800758void spm_assert_signal(void *p_pt, psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800759{
Ken Liu92ede9f2021-10-20 09:35:00 +0800760 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Ken Liu5d73c872021-08-19 19:23:17 +0800761 struct partition_t *partition = (struct partition_t *)p_pt;
Mingyang Sund44522a2020-01-16 16:48:37 +0800762
Mingyang Sund44522a2020-01-16 16:48:37 +0800763 if (!partition) {
764 tfm_core_panic();
765 }
766
Ken Liu92ede9f2021-10-20 09:35:00 +0800767 CRITICAL_SECTION_ENTER(cs_assert);
768
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800769 partition->signals_asserted |= signal;
Mingyang Sund44522a2020-01-16 16:48:37 +0800770
Kevin Peng8dac6102021-03-09 16:44:00 +0800771 if (partition->signals_waiting & signal) {
Ken Liu5d73c872021-08-19 19:23:17 +0800772 thrd_wake_up(&partition->waitobj,
773 partition->signals_asserted & partition->signals_waiting);
Kevin Peng8dac6102021-03-09 16:44:00 +0800774 partition->signals_waiting &= ~signal;
775 }
Ken Liu92ede9f2021-10-20 09:35:00 +0800776
777 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sund44522a2020-01-16 16:48:37 +0800778}