blob: 9e49a838f9e66b74c7999cec2150fb42c2287355 [file] [log] [blame]
Edison Ai764d41f2018-09-21 15:56:36 +08001/*
Sherry Zhangdcab2862022-01-10 10:43:31 +08002 * Copyright (c) 2018-2022, 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 Liu086b32c2021-12-27 14:44:52 +080011#include "aapcs_local.h"
Ken Liu24dffb22021-02-10 11:03:58 +080012#include "bitops.h"
Ken Liu92ede9f2021-10-20 09:35:00 +080013#include "critical_section.h"
Ken Liuf39d8eb2021-10-07 12:55:33 +080014#include "current.h"
David Huf07e97d2021-02-15 22:05:40 +080015#include "fih.h"
Jamie Foxcc31d402019-01-28 17:13:52 +000016#include "psa/client.h"
17#include "psa/service.h"
Ken Liu5d73c872021-08-19 19:23:17 +080018#include "thread.h"
Ken Liubcae38b2021-01-20 15:47:44 +080019#include "internal_errors.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080020#include "tfm_spm_hal.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080021#include "tfm_api.h"
22#include "tfm_secure_api.h"
23#include "tfm_memory_utils.h"
Mingyang Sund1ed6732020-08-26 15:52:21 +080024#include "tfm_hal_defs.h"
Kevin Pengd399a1f2021-09-08 15:33:14 +080025#include "tfm_hal_interrupt.h"
Mingyang Sund1ed6732020-08-26 15:52:21 +080026#include "tfm_hal_isolation.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080027#include "spm_ipc.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080028#include "tfm_peripherals_def.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080029#include "tfm_core_utils.h"
Kevin Peng385fda82021-08-18 10:41:19 +080030#include "tfm_nspm.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080031#include "tfm_rpc.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080032#include "tfm_core_trustzone.h"
Ken Liu24dffb22021-02-10 11:03:58 +080033#include "lists.h"
Edison Ai764d41f2018-09-21 15:56:36 +080034#include "tfm_pools.h"
Mingyang Sun22a3faf2021-07-09 15:32:47 +080035#include "region.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080036#include "psa_manifest/pid.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080037#include "ffm/backend.h"
Mingyang Sun00df2352021-04-15 15:46:08 +080038#include "load/partition_defs.h"
39#include "load/service_defs.h"
Ken Liu86686282021-04-27 11:11:15 +080040#include "load/asset_defs.h"
Ken Liuacd2a572021-05-12 16:19:04 +080041#include "load/spm_load_api.h"
Sherry Zhnag482b88b2021-08-19 17:51:47 +080042#include "tfm_nspm.h"
Edison Ai764d41f2018-09-21 15:56:36 +080043
Ken Liuea45b0d2021-05-22 17:41:25 +080044/* Partition and service runtime data list head/runtime data table */
Ken Liuea45b0d2021-05-22 17:41:25 +080045static struct service_head_t services_listhead;
Ken Liub3b2cb62021-05-22 00:39:28 +080046struct service_t *stateless_services_ref_tbl[STATIC_HANDLE_NUM_LIMIT];
Summer Qind99509f2019-08-02 17:36:58 +080047
Edison Ai764d41f2018-09-21 15:56:36 +080048/* Pools */
49TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t),
50 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +080051
Mingyang Sun620c8562021-11-10 11:44:58 +080052extern uint32_t scheduler_lock;
53
Summer Qin373feb12020-03-27 15:35:33 +080054/*********************** Connection handle conversion APIs *******************/
55
Summer Qin373feb12020-03-27 15:35:33 +080056#define CONVERSION_FACTOR_BITOFFSET 3
57#define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET)
58/* Set 32 as the maximum */
59#define CONVERSION_FACTOR_VALUE_MAX 0x20
60
61#if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX
62#error "CONVERSION FACTOR OUT OF RANGE"
63#endif
64
65static uint32_t loop_index;
66
67/*
68 * A handle instance psa_handle_t allocated inside SPM is actually a memory
69 * address among the handle pool. Return this handle to the client directly
70 * exposes information of secure memory address. In this case, converting the
71 * handle into another value does not represent the memory address to avoid
72 * exposing secure memory directly to clients.
73 *
74 * This function converts the handle instance into another value by scaling the
75 * handle in pool offset, the converted value is named as a user handle.
76 *
77 * The formula:
78 * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE +
79 * CLIENT_HANDLE_VALUE_MIN + loop_index
80 * where:
81 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
82 * exceed CONVERSION_FACTOR_VALUE_MAX.
83 *
84 * handle_instance in RANGE[POOL_START, POOL_END]
85 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
86 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
87 *
88 * note:
89 * loop_index is used to promise same handle instance is converted into
90 * different user handles in short time.
91 */
Ken Liu505b1702020-05-29 13:19:58 +080092psa_handle_t tfm_spm_to_user_handle(struct tfm_conn_handle_t *handle_instance)
Summer Qin373feb12020-03-27 15:35:33 +080093{
94 psa_handle_t user_handle;
95
96 loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE;
97 user_handle = (psa_handle_t)((((uintptr_t)handle_instance -
98 (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) +
99 CLIENT_HANDLE_VALUE_MIN + loop_index);
100
101 return user_handle;
102}
103
104/*
105 * This function converts a user handle into a corresponded handle instance.
106 * The converted value is validated before returning, an invalid handle instance
107 * is returned as NULL.
108 *
109 * The formula:
110 * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) /
111 * CONVERSION_FACTOR_VALUE) + POOL_START
112 * where:
113 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
114 * exceed CONVERSION_FACTOR_VALUE_MAX.
115 *
116 * handle_instance in RANGE[POOL_START, POOL_END]
117 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
118 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
119 */
120struct tfm_conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle)
121{
122 struct tfm_conn_handle_t *handle_instance;
123
124 if (user_handle == PSA_NULL_HANDLE) {
125 return NULL;
126 }
127
128 handle_instance = (struct tfm_conn_handle_t *)((((uintptr_t)user_handle -
129 CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) +
130 (uintptr_t)conn_handle_pool);
131
132 return handle_instance;
133}
134
Edison Ai764d41f2018-09-21 15:56:36 +0800135/* Service handle management functions */
Mingyang Sun783a59b2021-04-20 15:52:18 +0800136struct tfm_conn_handle_t *tfm_spm_create_conn_handle(struct service_t *service,
137 int32_t client_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800138{
Edison Ai9cc26242019-08-06 11:28:04 +0800139 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800140
Ken Liuf250b8b2019-12-27 16:31:24 +0800141 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800142
143 /* Get buffer for handle list structure from handle pool */
Edison Ai9cc26242019-08-06 11:28:04 +0800144 p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
145 if (!p_handle) {
Summer Qin630c76b2020-05-20 10:32:58 +0800146 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800147 }
148
Kevin Peng2cc25722021-11-18 11:46:02 +0800149 spm_memset(p_handle, 0, sizeof(*p_handle));
150
Ken Liuf39d8eb2021-10-07 12:55:33 +0800151 p_handle->internal_msg.service = service;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800152 p_handle->status = TFM_HANDLE_STATUS_IDLE;
Summer Qin1ce712a2019-10-14 18:04:05 +0800153 p_handle->client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800154
Summer Qin630c76b2020-05-20 10:32:58 +0800155 return p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800156}
157
Summer Qin630c76b2020-05-20 10:32:58 +0800158int32_t tfm_spm_validate_conn_handle(
159 const struct tfm_conn_handle_t *conn_handle,
160 int32_t client_id)
Summer Qin1ce712a2019-10-14 18:04:05 +0800161{
162 /* Check the handle address is validated */
163 if (is_valid_chunk_data_in_pool(conn_handle_pool,
164 (uint8_t *)conn_handle) != true) {
Ken Liubcae38b2021-01-20 15:47:44 +0800165 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800166 }
167
168 /* Check the handle caller is correct */
Summer Qin630c76b2020-05-20 10:32:58 +0800169 if (conn_handle->client_id != client_id) {
Ken Liubcae38b2021-01-20 15:47:44 +0800170 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800171 }
172
Ken Liubcae38b2021-01-20 15:47:44 +0800173 return SPM_SUCCESS;
Summer Qin1ce712a2019-10-14 18:04:05 +0800174}
175
Mingyang Sun783a59b2021-04-20 15:52:18 +0800176int32_t tfm_spm_free_conn_handle(struct service_t *service,
Summer Qin02f7f072020-08-24 16:02:54 +0800177 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800178{
Mingyang Sun620c8562021-11-10 11:44:58 +0800179 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
180
Ken Liuf250b8b2019-12-27 16:31:24 +0800181 TFM_CORE_ASSERT(service);
Summer Qin630c76b2020-05-20 10:32:58 +0800182 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800183
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200184 /* Clear magic as the handler is not used anymore */
Summer Qin630c76b2020-05-20 10:32:58 +0800185 conn_handle->internal_msg.magic = 0;
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200186
Mingyang Sun620c8562021-11-10 11:44:58 +0800187 CRITICAL_SECTION_ENTER(cs_assert);
Edison Ai764d41f2018-09-21 15:56:36 +0800188
189 /* Back handle buffer to pool */
Ken Liu66ca6132021-02-24 08:49:51 +0800190 tfm_pool_free(conn_handle_pool, conn_handle);
Mingyang Sun620c8562021-11-10 11:44:58 +0800191 CRITICAL_SECTION_LEAVE(cs_assert);
192
Ken Liubcae38b2021-01-20 15:47:44 +0800193 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800194}
195
Mingyang Sun783a59b2021-04-20 15:52:18 +0800196int32_t tfm_spm_set_rhandle(struct service_t *service,
Summer Qin02f7f072020-08-24 16:02:54 +0800197 struct tfm_conn_handle_t *conn_handle,
198 void *rhandle)
Edison Ai764d41f2018-09-21 15:56:36 +0800199{
Ken Liuf250b8b2019-12-27 16:31:24 +0800200 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800201 /* Set reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800202 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800203
Summer Qin630c76b2020-05-20 10:32:58 +0800204 conn_handle->rhandle = rhandle;
Ken Liubcae38b2021-01-20 15:47:44 +0800205 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800206}
207
Mingyang Sund44522a2020-01-16 16:48:37 +0800208/**
Xinyu Zhang3a453242021-04-16 17:57:09 +0800209 * \brief Get reverse handle value from connection handle.
Mingyang Sund44522a2020-01-16 16:48:37 +0800210 *
211 * \param[in] service Target service context pointer
212 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800213 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800214 *
215 * \retval void * Success
216 * \retval "Does not return" Panic for those:
217 * service pointer are NULL
Xinyu Zhang3a453242021-04-16 17:57:09 +0800218 * handle is \ref PSA_NULL_HANDLE
Mingyang Sund44522a2020-01-16 16:48:37 +0800219 * handle node does not be found
220 */
Mingyang Sun783a59b2021-04-20 15:52:18 +0800221static void *tfm_spm_get_rhandle(struct service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800222 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800223{
Ken Liuf250b8b2019-12-27 16:31:24 +0800224 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800225 /* Get reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800226 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800227
Summer Qin630c76b2020-05-20 10:32:58 +0800228 return conn_handle->rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800229}
230
231/* Partition management functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800232
Summer Qin02f7f072020-08-24 16:02:54 +0800233struct tfm_msg_body_t *tfm_spm_get_msg_by_signal(struct partition_t *partition,
234 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800235{
Ken Liu2c47f7f2021-01-22 11:06:04 +0800236 struct bi_list_node_t *node, *head;
Mingyang Sun73056b62020-07-03 15:18:46 +0800237 struct tfm_msg_body_t *tmp_msg, *msg = NULL;
Ken Liu92ede9f2021-10-20 09:35:00 +0800238 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Edison Ai764d41f2018-09-21 15:56:36 +0800239
Ken Liuf250b8b2019-12-27 16:31:24 +0800240 TFM_CORE_ASSERT(partition);
Edison Ai764d41f2018-09-21 15:56:36 +0800241
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800242 head = &partition->msg_list;
Mingyang Sun73056b62020-07-03 15:18:46 +0800243
Ken Liu2c47f7f2021-01-22 11:06:04 +0800244 if (BI_LIST_IS_EMPTY(head)) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800245 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800246 }
247
Mingyang Sun73056b62020-07-03 15:18:46 +0800248 /*
249 * There may be multiple messages for this RoT Service signal, do not clear
250 * partition mask until no remaining message. Search may be optimized.
251 */
Mingyang Sun620c8562021-11-10 11:44:58 +0800252 CRITICAL_SECTION_ENTER(cs_assert);
Ken Liu2c47f7f2021-01-22 11:06:04 +0800253 BI_LIST_FOR_EACH(node, head) {
Ken Liuc25558c2021-05-20 15:31:28 +0800254 tmp_msg = TO_CONTAINER(node, struct tfm_msg_body_t, msg_node);
Ken Liuacd2a572021-05-12 16:19:04 +0800255 if (tmp_msg->service->p_ldinf->signal == signal && msg) {
Mingyang Sun620c8562021-11-10 11:44:58 +0800256 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sun73056b62020-07-03 15:18:46 +0800257 return msg;
Ken Liuacd2a572021-05-12 16:19:04 +0800258 } else if (tmp_msg->service->p_ldinf->signal == signal) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800259 msg = tmp_msg;
Ken Liu2c47f7f2021-01-22 11:06:04 +0800260 BI_LIST_REMOVE_NODE(node);
Edison Ai764d41f2018-09-21 15:56:36 +0800261 }
262 }
Mingyang Sun73056b62020-07-03 15:18:46 +0800263
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800264 partition->signals_asserted &= ~signal;
Ken Liu92ede9f2021-10-20 09:35:00 +0800265 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sun73056b62020-07-03 15:18:46 +0800266
267 return msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800268}
269
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800270uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
271{
Kevin Pengbf9a97e2021-06-18 16:34:12 +0800272#if TFM_LVL == 1
273 return TFM_PARTITION_PRIVILEGED_MODE;
274#else /* TFM_LVL == 1 */
Shawn Shana3657e82021-10-18 14:37:09 +0800275 if (partition_flags & PARTITION_MODEL_PSA_ROT) {
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800276 return TFM_PARTITION_PRIVILEGED_MODE;
277 } else {
278 return TFM_PARTITION_UNPRIVILEGED_MODE;
279 }
Kevin Pengbf9a97e2021-06-18 16:34:12 +0800280#endif /* TFM_LVL == 1 */
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800281}
282
Mingyang Sun783a59b2021-04-20 15:52:18 +0800283struct service_t *tfm_spm_get_service_by_sid(uint32_t sid)
Edison Ai764d41f2018-09-21 15:56:36 +0800284{
Feder.Liangaeb5a792021-08-10 16:12:56 +0800285 struct service_t *p_prev, *p_curr;
Edison Ai764d41f2018-09-21 15:56:36 +0800286
Feder.Liangaeb5a792021-08-10 16:12:56 +0800287 UNI_LIST_FOR_EACH_PREV(p_prev, p_curr, &services_listhead) {
288 if (p_curr->p_ldinf->sid == sid) {
289 UNI_LIST_MOVE_AFTER(&services_listhead, p_prev, p_curr);
290 return p_curr;
Mingyang Sunef42f442021-06-11 15:07:58 +0800291 }
292 }
293
Ken Liuea45b0d2021-05-22 17:41:25 +0800294 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800295}
296
Mingyang Sund44522a2020-01-16 16:48:37 +0800297/**
298 * \brief Get the partition context by partition ID.
299 *
300 * \param[in] partition_id Partition identity
301 *
302 * \retval NULL Failed
303 * \retval "Not NULL" Target partition context pointer,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800304 * \ref partition_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800305 */
Kevin Pengeca45b92021-02-09 14:46:50 +0800306struct partition_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800307{
Ken Liuea45b0d2021-05-22 17:41:25 +0800308 struct partition_t *p_part;
Edison Ai764d41f2018-09-21 15:56:36 +0800309
Mingyang Sundeae45d2021-09-06 15:31:07 +0800310 UNI_LIST_FOR_EACH(p_part, PARTITION_LIST_ADDR) {
Ken Liuea45b0d2021-05-22 17:41:25 +0800311 if (p_part->p_ldinf->pid == partition_id) {
312 return p_part;
313 }
Edison Ai764d41f2018-09-21 15:56:36 +0800314 }
Ken Liuea45b0d2021-05-22 17:41:25 +0800315
Edison Ai764d41f2018-09-21 15:56:36 +0800316 return NULL;
317}
318
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800319struct partition_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800320{
Ken Liuf39d8eb2021-10-07 12:55:33 +0800321 return GET_CURRENT_COMPONENT();
Edison Ai764d41f2018-09-21 15:56:36 +0800322}
323
Mingyang Sun783a59b2021-04-20 15:52:18 +0800324int32_t tfm_spm_check_client_version(struct service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530325 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800326{
Ken Liuf250b8b2019-12-27 16:31:24 +0800327 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800328
Ken Liuacd2a572021-05-12 16:19:04 +0800329 switch (SERVICE_GET_VERSION_POLICY(service->p_ldinf->flags)) {
Ken Liub3b2cb62021-05-22 00:39:28 +0800330 case SERVICE_VERSION_POLICY_RELAXED:
Ken Liuacd2a572021-05-12 16:19:04 +0800331 if (version > service->p_ldinf->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800332 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800333 }
334 break;
Ken Liub3b2cb62021-05-22 00:39:28 +0800335 case SERVICE_VERSION_POLICY_STRICT:
Ken Liuacd2a572021-05-12 16:19:04 +0800336 if (version != service->p_ldinf->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800337 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800338 }
339 break;
340 default:
Ken Liubcae38b2021-01-20 15:47:44 +0800341 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800342 }
Ken Liubcae38b2021-01-20 15:47:44 +0800343 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800344}
345
Edison Aie728fbf2019-11-13 09:37:12 +0800346int32_t tfm_spm_check_authorization(uint32_t sid,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800347 struct service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800348 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800349{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800350 struct partition_t *partition = NULL;
Mingyang Sun2b352662021-04-21 11:35:43 +0800351 uint32_t *dep;
Edison Aie728fbf2019-11-13 09:37:12 +0800352 int32_t i;
353
Ken Liuf250b8b2019-12-27 16:31:24 +0800354 TFM_CORE_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800355
356 if (ns_caller) {
Ken Liuacd2a572021-05-12 16:19:04 +0800357 if (!SERVICE_IS_NS_ACCESSIBLE(service->p_ldinf->flags)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800358 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800359 }
360 } else {
361 partition = tfm_spm_get_running_partition();
362 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800363 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800364 }
365
Ken Liuacd2a572021-05-12 16:19:04 +0800366 dep = (uint32_t *)LOAD_INFO_DEPS(partition->p_ldinf);
367 for (i = 0; i < partition->p_ldinf->ndeps; i++) {
Mingyang Sun2b352662021-04-21 11:35:43 +0800368 if (dep[i] == sid) {
Edison Aie728fbf2019-11-13 09:37:12 +0800369 break;
370 }
371 }
372
Ken Liuacd2a572021-05-12 16:19:04 +0800373 if (i == partition->p_ldinf->ndeps) {
Ken Liubcae38b2021-01-20 15:47:44 +0800374 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800375 }
376 }
Ken Liubcae38b2021-01-20 15:47:44 +0800377 return SPM_SUCCESS;
Edison Aie728fbf2019-11-13 09:37:12 +0800378}
379
Edison Ai764d41f2018-09-21 15:56:36 +0800380/* Message functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800381
Summer Qin02f7f072020-08-24 16:02:54 +0800382struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800383{
384 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200385 * The message handler passed by the caller is considered invalid in the
386 * following cases:
387 * 1. Not a valid message handle. (The address of a message is not the
388 * address of a possible handle from the pool
389 * 2. Handle not belongs to the caller partition (The handle is either
390 * unused, or owned by anither partition)
391 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800392 */
Ken Liu505b1702020-05-29 13:19:58 +0800393 struct tfm_msg_body_t *p_msg;
Kevin Pengeec41a82021-08-18 13:56:23 +0800394 int32_t partition_id;
Ken Liu505b1702020-05-29 13:19:58 +0800395 struct tfm_conn_handle_t *p_conn_handle =
Ken Liu5d73c872021-08-19 19:23:17 +0800396 tfm_spm_to_handle_instance(msg_handle);
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200397
398 if (is_valid_chunk_data_in_pool(
Ken Liu505b1702020-05-29 13:19:58 +0800399 conn_handle_pool, (uint8_t *)p_conn_handle) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800400 return NULL;
401 }
402
Ken Liu505b1702020-05-29 13:19:58 +0800403 p_msg = &p_conn_handle->internal_msg;
404
Edison Ai764d41f2018-09-21 15:56:36 +0800405 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200406 * Check that the magic number is correct. This proves that the message
407 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800408 */
Ken Liu505b1702020-05-29 13:19:58 +0800409 if (p_msg->magic != TFM_MSG_MAGIC) {
Edison Ai764d41f2018-09-21 15:56:36 +0800410 return NULL;
411 }
412
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200413 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800414 partition_id = tfm_spm_partition_get_running_partition_id();
Ken Liuacd2a572021-05-12 16:19:04 +0800415 if (partition_id != p_msg->service->partition->p_ldinf->pid) {
Edison Ai764d41f2018-09-21 15:56:36 +0800416 return NULL;
417 }
418
Ken Liu505b1702020-05-29 13:19:58 +0800419 return p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800420}
421
Kevin Pengdf6aa292021-03-11 17:58:50 +0800422struct tfm_msg_body_t *
423 tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle)
424{
425 TFM_CORE_ASSERT(conn_handle != NULL);
426
427 return &(conn_handle->internal_msg);
428}
429
Edison Ai97115822019-08-01 14:22:19 +0800430void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800431 struct service_t *service,
Ken Liu505b1702020-05-29 13:19:58 +0800432 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800433 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800434 psa_invec *invec, size_t in_len,
435 psa_outvec *outvec, size_t out_len,
436 psa_outvec *caller_outvec)
437{
Edison Ai764d41f2018-09-21 15:56:36 +0800438 uint32_t i;
Ken Liu505b1702020-05-29 13:19:58 +0800439 struct tfm_conn_handle_t *conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800440
Ken Liuf250b8b2019-12-27 16:31:24 +0800441 TFM_CORE_ASSERT(msg);
442 TFM_CORE_ASSERT(service);
443 TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
444 TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
445 TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
446 TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
447 TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800448
Edison Ai764d41f2018-09-21 15:56:36 +0800449 /* Clear message buffer before using it */
Ken Liuf39d8eb2021-10-07 12:55:33 +0800450 spm_memset(&msg->msg, 0, sizeof(psa_msg_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800451
Ken Liu5d73c872021-08-19 19:23:17 +0800452 THRD_SYNC_INIT(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800453 msg->magic = TFM_MSG_MAGIC;
454 msg->service = service;
Ken Liuf39d8eb2021-10-07 12:55:33 +0800455 msg->p_client = GET_CURRENT_COMPONENT();
Edison Ai764d41f2018-09-21 15:56:36 +0800456 msg->caller_outvec = caller_outvec;
Summer Qin1ce712a2019-10-14 18:04:05 +0800457 msg->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800458
459 /* Copy contents */
460 msg->msg.type = type;
461
462 for (i = 0; i < in_len; i++) {
463 msg->msg.in_size[i] = invec[i].len;
464 msg->invec[i].base = invec[i].base;
465 }
466
467 for (i = 0; i < out_len; i++) {
468 msg->msg.out_size[i] = outvec[i].len;
469 msg->outvec[i].base = outvec[i].base;
470 /* Out len is used to record the writed number, set 0 here again */
471 msg->outvec[i].len = 0;
472 }
473
Ken Liu505b1702020-05-29 13:19:58 +0800474 /* Use the user connect handle as the message handle */
475 msg->msg.handle = handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800476
Ken Liu505b1702020-05-29 13:19:58 +0800477 conn_handle = tfm_spm_to_handle_instance(handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800478 /* For connected handle, set rhandle to every message */
Ken Liu505b1702020-05-29 13:19:58 +0800479 if (conn_handle) {
480 msg->msg.rhandle = tfm_spm_get_rhandle(service, conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800481 }
David Hu46603dd2019-12-11 18:05:16 +0800482
483 /* Set the private data of NSPE client caller in multi-core topology */
484 if (TFM_CLIENT_ID_IS_NS(client_id)) {
485 tfm_rpc_set_caller_data(msg, client_id);
486 }
Edison Ai764d41f2018-09-21 15:56:36 +0800487}
488
Kevin Pengeec41a82021-08-18 13:56:23 +0800489int32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800490{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800491 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800492
Kevin Peng79c2bda2020-07-24 16:31:12 +0800493 partition = tfm_spm_get_running_partition();
Ken Liuacd2a572021-05-12 16:19:04 +0800494 if (partition && partition->p_ldinf) {
495 return partition->p_ldinf->pid;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800496 } else {
497 return INVALID_PARTITION_ID;
498 }
Edison Ai764d41f2018-09-21 15:56:36 +0800499}
500
Summer Qin43c185d2019-10-10 15:44:42 +0800501int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800502 enum tfm_memory_access_e access,
503 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800504{
Mingyang Sund1ed6732020-08-26 15:52:21 +0800505 enum tfm_hal_status_t err;
506 uint32_t attr = 0;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800507
508 /* If len is zero, this indicates an empty buffer and base is ignored */
509 if (len == 0) {
Ken Liubcae38b2021-01-20 15:47:44 +0800510 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800511 }
512
513 if (!buffer) {
Ken Liubcae38b2021-01-20 15:47:44 +0800514 return SPM_ERROR_BAD_PARAMETERS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800515 }
516
517 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800518 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800519 }
520
Summer Qin424d4db2019-03-25 14:09:51 +0800521 if (access == TFM_MEMORY_ACCESS_RW) {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800522 attr |= (TFM_HAL_ACCESS_READABLE | TFM_HAL_ACCESS_WRITABLE);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800523 } else {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800524 attr |= TFM_HAL_ACCESS_READABLE;
Summer Qin424d4db2019-03-25 14:09:51 +0800525 }
Mingyang Sund1ed6732020-08-26 15:52:21 +0800526
527 if (privileged == TFM_PARTITION_UNPRIVILEGED_MODE) {
528 attr |= TFM_HAL_ACCESS_UNPRIVILEGED;
529 } else {
530 attr &= ~TFM_HAL_ACCESS_UNPRIVILEGED;
531 }
532
533 if (ns_caller) {
534 attr |= TFM_HAL_ACCESS_NS;
535 }
536
537 err = tfm_hal_memory_has_access((uintptr_t)buffer, len, attr);
538
539 if (err == TFM_HAL_SUCCESS) {
Ken Liubcae38b2021-01-20 15:47:44 +0800540 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800541 }
542
Ken Liubcae38b2021-01-20 15:47:44 +0800543 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800544}
545
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800546bool tfm_spm_is_ns_caller(void)
547{
Mingyang Sunc1805432021-11-22 18:56:59 +0800548#if defined(TFM_MULTI_CORE_TOPOLOGY)
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800549 /* Multi-core NS PSA API request is processed by pendSV. */
550 return (__get_active_exc_num() == EXC_NUM_PENDSV);
551#else
552 struct partition_t *partition = tfm_spm_get_running_partition();
Mingyang Sune529e3b2021-07-12 14:46:30 +0800553
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800554 if (!partition) {
555 tfm_core_panic();
556 }
557
558 return (partition->p_ldinf->pid == TFM_SP_NON_SECURE_ID);
559#endif
560}
561
Mingyang Sune529e3b2021-07-12 14:46:30 +0800562uint32_t tfm_spm_get_caller_privilege_mode(void)
563{
564 struct partition_t *partition;
565
566#if defined(TFM_MULTI_CORE_TOPOLOGY) || defined(FORWARD_PROT_MSG)
567 /*
568 * In multi-core topology, if PSA request is from mailbox, the client
569 * is unprivileged.
570 */
571 if (__get_active_exc_num() == EXC_NUM_PENDSV) {
572 return TFM_PARTITION_UNPRIVILEGED_MODE;
573 }
574#endif
575 partition = tfm_spm_get_running_partition();
576 if (!partition) {
577 tfm_core_panic();
578 }
579
580 return tfm_spm_partition_get_privileged_mode(partition->p_ldinf->flags);
581}
582
Kevin Peng385fda82021-08-18 10:41:19 +0800583int32_t tfm_spm_get_client_id(bool ns_caller)
584{
585 int32_t client_id;
586
587 if (ns_caller) {
588 client_id = tfm_nspm_get_current_client_id();
589 } else {
590 client_id = tfm_spm_partition_get_running_partition_id();
591 }
592
593 if (ns_caller != (client_id < 0)) {
594 /* NS client ID must be negative and Secure ID must >= 0 */
595 tfm_core_panic();
596 }
597
598 return client_id;
599}
600
Ken Liuce2692d2020-02-11 12:39:36 +0800601uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800602{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800603 struct partition_t *partition;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800604 const struct partition_load_info_t *p_pldi;
605 uint32_t service_setting = 0;
Ken Liuce58bfc2021-05-12 17:54:48 +0800606
David Huf07e97d2021-02-15 22:05:40 +0800607#ifdef TFM_FIH_PROFILE_ON
608 fih_int fih_rc = FIH_FAILURE;
609#endif
Edison Ai764d41f2018-09-21 15:56:36 +0800610
611 tfm_pool_init(conn_handle_pool,
612 POOL_BUFFER_SIZE(conn_handle_pool),
613 sizeof(struct tfm_conn_handle_t),
614 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800615
Mingyang Sundeae45d2021-09-06 15:31:07 +0800616 UNI_LISI_INIT_HEAD(PARTITION_LIST_ADDR);
Ken Liuea45b0d2021-05-22 17:41:25 +0800617 UNI_LISI_INIT_HEAD(&services_listhead);
618
Sherry Zhnag482b88b2021-08-19 17:51:47 +0800619 /* Init the nonsecure context. */
Chris Brand56b4d0c2021-12-17 16:15:58 -0800620 tfm_nspm_ctx_init();
Sherry Zhnag482b88b2021-08-19 17:51:47 +0800621
Ken Liuacd2a572021-05-12 16:19:04 +0800622 while (1) {
Mingyang Sundeae45d2021-09-06 15:31:07 +0800623 partition = load_a_partition_assuredly(PARTITION_LIST_ADDR);
Shawn Shan23331062021-09-01 14:58:21 +0800624 if (partition == NO_MORE_PARTITION) {
Ken Liuacd2a572021-05-12 16:19:04 +0800625 break;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800626 }
627
Mingyang Sundeae45d2021-09-06 15:31:07 +0800628 p_pldi = partition->p_ldinf;
Mingyang Sun2b352662021-04-21 11:35:43 +0800629
Mingyang Sundeae45d2021-09-06 15:31:07 +0800630 if (p_pldi->nservices) {
631 service_setting = load_services_assuredly(
632 partition,
633 &services_listhead,
634 stateless_services_ref_tbl,
635 sizeof(stateless_services_ref_tbl));
Kevin Peng27e42272021-05-24 17:58:53 +0800636 }
637
Mingyang Sundeae45d2021-09-06 15:31:07 +0800638 if (p_pldi->nirqs) {
Kevin Peng27e42272021-05-24 17:58:53 +0800639 load_irqs_assuredly(partition);
640 }
641
Mingyang Sundeae45d2021-09-06 15:31:07 +0800642 /* Bind the partition with platform. */
Ken Liuce58bfc2021-05-12 17:54:48 +0800643#if TFM_FIH_PROFILE_ON
644 FIH_CALL(tfm_hal_bind_boundaries, fih_rc, partition->p_ldinf,
Mingyang Sundeae45d2021-09-06 15:31:07 +0800645 &partition->p_boundaries);
Ken Liuce58bfc2021-05-12 17:54:48 +0800646 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
647 tfm_core_panic();
Mingyang Sun61f8fbc2021-06-04 17:49:56 +0800648 }
Ken Liu86686282021-04-27 11:11:15 +0800649#else /* TFM_FIH_PROFILE_ON */
Ken Liuce58bfc2021-05-12 17:54:48 +0800650 if (tfm_hal_bind_boundaries(partition->p_ldinf,
Mingyang Sundeae45d2021-09-06 15:31:07 +0800651 &partition->p_boundaries)
652 != TFM_HAL_SUCCESS) {
Ken Liuce58bfc2021-05-12 17:54:48 +0800653 tfm_core_panic();
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100654 }
Ken Liuce58bfc2021-05-12 17:54:48 +0800655#endif /* TFM_FIH_PROFILE_ON */
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100656
Mingyang Sundeae45d2021-09-06 15:31:07 +0800657 backend_instance.comp_init_assuredly(partition, service_setting);
Edison Ai764d41f2018-09-21 15:56:36 +0800658 }
659
Mingyang Sundeae45d2021-09-06 15:31:07 +0800660 return backend_instance.system_run();
Edison Ai764d41f2018-09-21 15:56:36 +0800661}
Ken Liu2d175172019-03-21 17:08:41 +0800662
Ken Liu5d73c872021-08-19 19:23:17 +0800663uint64_t do_schedule(void)
664{
Ken Liu086b32c2021-12-27 14:44:52 +0800665 AAPCS_DUAL_U32_T ctx_ctrls;
Ken Liu5d73c872021-08-19 19:23:17 +0800666 struct partition_t *p_part_curr, *p_part_next;
667 struct thread_t *pth_next = thrd_next();
Kevin Pengca59ec02021-12-09 14:35:50 +0800668 struct critical_section_t cs = CRITICAL_SECTION_STATIC_INIT;
Ken Liu5d73c872021-08-19 19:23:17 +0800669
Ken Liu086b32c2021-12-27 14:44:52 +0800670 AAPCS_DUAL_U32_SET(ctx_ctrls, (uint32_t)CURRENT_THREAD->p_context_ctrl,
671 (uint32_t)CURRENT_THREAD->p_context_ctrl);
672
Mingyang Sun4f08f4d2021-09-10 17:41:28 +0800673 p_part_curr = GET_THRD_OWNER(CURRENT_THREAD);
674 p_part_next = GET_THRD_OWNER(pth_next);
Ken Liu5d73c872021-08-19 19:23:17 +0800675
Mingyang Sun620c8562021-11-10 11:44:58 +0800676 if (scheduler_lock != SCHEDULER_LOCKED && pth_next != NULL &&
677 p_part_curr != p_part_next) {
Ken Liu5d73c872021-08-19 19:23:17 +0800678 /* Check if there is enough room on stack to save more context */
679 if ((p_part_curr->ctx_ctrl.sp_limit +
680 sizeof(struct tfm_additional_context_t)) > __get_PSP()) {
681 tfm_core_panic();
682 }
Mate Toth-Palc430b992019-05-09 21:01:14 +0200683
Kevin Pengca59ec02021-12-09 14:35:50 +0800684 CRITICAL_SECTION_ENTER(cs);
Ken Liuce58bfc2021-05-12 17:54:48 +0800685 /*
686 * If required, let the platform update boundary based on its
687 * implementation. Change privilege, MPU or other configurations.
688 */
689 if (p_part_curr->p_boundaries != p_part_next->p_boundaries) {
690 if (tfm_hal_update_boundaries(p_part_next->p_ldinf,
691 p_part_next->p_boundaries)
692 != TFM_HAL_SUCCESS) {
693 tfm_core_panic();
694 }
695 }
Feder Liang42f5b562021-09-10 17:38:36 +0800696 ARCH_FLUSH_FP_CONTEXT();
Mingyang Sun620c8562021-11-10 11:44:58 +0800697
Ken Liu086b32c2021-12-27 14:44:52 +0800698 AAPCS_DUAL_U32_SET_A1(ctx_ctrls, (uint32_t)pth_next->p_context_ctrl);
699
Mingyang Sun620c8562021-11-10 11:44:58 +0800700 CURRENT_THREAD = pth_next;
Kevin Pengca59ec02021-12-09 14:35:50 +0800701 CRITICAL_SECTION_LEAVE(cs);
Ken Liu2d175172019-03-21 17:08:41 +0800702 }
David Hufb38d562019-09-23 15:58:34 +0800703
704 /*
705 * Handle pending mailbox message from NS in multi-core topology.
706 * Empty operation on single Armv8-M platform.
707 */
708 tfm_rpc_client_call_handler();
Ken Liu5d73c872021-08-19 19:23:17 +0800709
Ken Liu086b32c2021-12-27 14:44:52 +0800710 return AAPCS_DUAL_U32_AS_U64(ctx_ctrls);
Ken Liu2d175172019-03-21 17:08:41 +0800711}
Mingyang Sund44522a2020-01-16 16:48:37 +0800712
Summer Qin02f7f072020-08-24 16:02:54 +0800713void update_caller_outvec_len(struct tfm_msg_body_t *msg)
Mingyang Sund44522a2020-01-16 16:48:37 +0800714{
715 uint32_t i;
716
717 /*
718 * FixeMe: abstract these part into dedicated functions to avoid
719 * accessing thread context in psa layer
720 */
Ken Liuf39d8eb2021-10-07 12:55:33 +0800721 /*
722 * If it is a NS request via RPC, the owner of this message is not set.
723 * Or if it is a SFN message, it does not have owner thread state either.
724 */
725 if ((!is_tfm_rpc_msg(msg)) && (msg->sfn_magic != TFM_MSG_MAGIC_SFN)) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800726 TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK);
727 }
728
729 for (i = 0; i < PSA_MAX_IOVEC; i++) {
730 if (msg->msg.out_size[i] == 0) {
731 continue;
732 }
733
734 TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base);
735
736 msg->caller_outvec[i].len = msg->outvec[i].len;
737 }
738}
739
Ken Liu5d73c872021-08-19 19:23:17 +0800740void spm_assert_signal(void *p_pt, psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800741{
Ken Liu92ede9f2021-10-20 09:35:00 +0800742 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Ken Liu5d73c872021-08-19 19:23:17 +0800743 struct partition_t *partition = (struct partition_t *)p_pt;
Mingyang Sund44522a2020-01-16 16:48:37 +0800744
Mingyang Sund44522a2020-01-16 16:48:37 +0800745 if (!partition) {
746 tfm_core_panic();
747 }
748
Ken Liu92ede9f2021-10-20 09:35:00 +0800749 CRITICAL_SECTION_ENTER(cs_assert);
750
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800751 partition->signals_asserted |= signal;
Mingyang Sund44522a2020-01-16 16:48:37 +0800752
Kevin Peng8dac6102021-03-09 16:44:00 +0800753 if (partition->signals_waiting & signal) {
Ken Liu5d73c872021-08-19 19:23:17 +0800754 thrd_wake_up(&partition->waitobj,
755 partition->signals_asserted & partition->signals_waiting);
Kevin Peng8dac6102021-03-09 16:44:00 +0800756 partition->signals_waiting &= ~signal;
757 }
Ken Liu92ede9f2021-10-20 09:35:00 +0800758
759 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sund44522a2020-01-16 16:48:37 +0800760}