blob: 67a28c3a36e069ddc6b49c490ab1274ab15d0453 [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.
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
Edison Ai764d41f2018-09-21 15:56:36 +08008#include <inttypes.h>
9#include <stdbool.h>
Ken Liu24dffb22021-02-10 11:03:58 +080010#include "bitops.h"
Ken Liu92ede9f2021-10-20 09:35:00 +080011#include "critical_section.h"
Ken Liuf39d8eb2021-10-07 12:55:33 +080012#include "current.h"
David Huf07e97d2021-02-15 22:05:40 +080013#include "fih.h"
Jamie Foxcc31d402019-01-28 17:13:52 +000014#include "psa/client.h"
15#include "psa/service.h"
Ken Liu5d73c872021-08-19 19:23:17 +080016#include "thread.h"
Ken Liubcae38b2021-01-20 15:47:44 +080017#include "internal_errors.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080018#include "tfm_spm_hal.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080019#include "tfm_api.h"
20#include "tfm_secure_api.h"
21#include "tfm_memory_utils.h"
Mingyang Sund1ed6732020-08-26 15:52:21 +080022#include "tfm_hal_defs.h"
Kevin Pengd399a1f2021-09-08 15:33:14 +080023#include "tfm_hal_interrupt.h"
Mingyang Sund1ed6732020-08-26 15:52:21 +080024#include "tfm_hal_isolation.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080025#include "spm_ipc.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080026#include "tfm_peripherals_def.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080027#include "tfm_core_utils.h"
Kevin Peng385fda82021-08-18 10:41:19 +080028#include "tfm_nspm.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080029#include "tfm_rpc.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080030#include "tfm_core_trustzone.h"
Ken Liu24dffb22021-02-10 11:03:58 +080031#include "lists.h"
Edison Ai764d41f2018-09-21 15:56:36 +080032#include "tfm_pools.h"
Mingyang Sun22a3faf2021-07-09 15:32:47 +080033#include "region.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080034#include "psa_manifest/pid.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080035#include "ffm/backend.h"
Mingyang Sun00df2352021-04-15 15:46:08 +080036#include "load/partition_defs.h"
37#include "load/service_defs.h"
Ken Liu86686282021-04-27 11:11:15 +080038#include "load/asset_defs.h"
Ken Liuacd2a572021-05-12 16:19:04 +080039#include "load/spm_load_api.h"
Sherry Zhnag482b88b2021-08-19 17:51:47 +080040#include "tfm_nspm.h"
Edison Ai764d41f2018-09-21 15:56:36 +080041
Ken Liuea45b0d2021-05-22 17:41:25 +080042/* Partition and service runtime data list head/runtime data table */
Ken Liuea45b0d2021-05-22 17:41:25 +080043static struct service_head_t services_listhead;
Ken Liub3b2cb62021-05-22 00:39:28 +080044struct service_t *stateless_services_ref_tbl[STATIC_HANDLE_NUM_LIMIT];
Summer Qind99509f2019-08-02 17:36:58 +080045
Edison Ai764d41f2018-09-21 15:56:36 +080046/* Pools */
47TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t),
48 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +080049
Mingyang Sun620c8562021-11-10 11:44:58 +080050extern uint32_t scheduler_lock;
51
Summer Qin373feb12020-03-27 15:35:33 +080052/*********************** Connection handle conversion APIs *******************/
53
Summer Qin373feb12020-03-27 15:35:33 +080054#define CONVERSION_FACTOR_BITOFFSET 3
55#define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET)
56/* Set 32 as the maximum */
57#define CONVERSION_FACTOR_VALUE_MAX 0x20
58
59#if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX
60#error "CONVERSION FACTOR OUT OF RANGE"
61#endif
62
63static uint32_t loop_index;
64
65/*
66 * A handle instance psa_handle_t allocated inside SPM is actually a memory
67 * address among the handle pool. Return this handle to the client directly
68 * exposes information of secure memory address. In this case, converting the
69 * handle into another value does not represent the memory address to avoid
70 * exposing secure memory directly to clients.
71 *
72 * This function converts the handle instance into another value by scaling the
73 * handle in pool offset, the converted value is named as a user handle.
74 *
75 * The formula:
76 * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE +
77 * CLIENT_HANDLE_VALUE_MIN + loop_index
78 * where:
79 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
80 * exceed CONVERSION_FACTOR_VALUE_MAX.
81 *
82 * handle_instance in RANGE[POOL_START, POOL_END]
83 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
84 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
85 *
86 * note:
87 * loop_index is used to promise same handle instance is converted into
88 * different user handles in short time.
89 */
Ken Liu505b1702020-05-29 13:19:58 +080090psa_handle_t tfm_spm_to_user_handle(struct tfm_conn_handle_t *handle_instance)
Summer Qin373feb12020-03-27 15:35:33 +080091{
92 psa_handle_t user_handle;
93
94 loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE;
95 user_handle = (psa_handle_t)((((uintptr_t)handle_instance -
96 (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) +
97 CLIENT_HANDLE_VALUE_MIN + loop_index);
98
99 return user_handle;
100}
101
102/*
103 * This function converts a user handle into a corresponded handle instance.
104 * The converted value is validated before returning, an invalid handle instance
105 * is returned as NULL.
106 *
107 * The formula:
108 * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) /
109 * CONVERSION_FACTOR_VALUE) + POOL_START
110 * where:
111 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
112 * exceed CONVERSION_FACTOR_VALUE_MAX.
113 *
114 * handle_instance in RANGE[POOL_START, POOL_END]
115 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
116 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
117 */
118struct tfm_conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle)
119{
120 struct tfm_conn_handle_t *handle_instance;
121
122 if (user_handle == PSA_NULL_HANDLE) {
123 return NULL;
124 }
125
126 handle_instance = (struct tfm_conn_handle_t *)((((uintptr_t)user_handle -
127 CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) +
128 (uintptr_t)conn_handle_pool);
129
130 return handle_instance;
131}
132
Edison Ai764d41f2018-09-21 15:56:36 +0800133/* Service handle management functions */
Mingyang Sun783a59b2021-04-20 15:52:18 +0800134struct tfm_conn_handle_t *tfm_spm_create_conn_handle(struct service_t *service,
135 int32_t client_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800136{
Edison Ai9cc26242019-08-06 11:28:04 +0800137 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800138
Ken Liuf250b8b2019-12-27 16:31:24 +0800139 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800140
141 /* Get buffer for handle list structure from handle pool */
Edison Ai9cc26242019-08-06 11:28:04 +0800142 p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
143 if (!p_handle) {
Summer Qin630c76b2020-05-20 10:32:58 +0800144 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800145 }
146
Kevin Peng2cc25722021-11-18 11:46:02 +0800147 spm_memset(p_handle, 0, sizeof(*p_handle));
148
Ken Liuf39d8eb2021-10-07 12:55:33 +0800149 p_handle->internal_msg.service = service;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800150 p_handle->status = TFM_HANDLE_STATUS_IDLE;
Summer Qin1ce712a2019-10-14 18:04:05 +0800151 p_handle->client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800152
153 /* Add handle node to list for next psa functions */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800154 BI_LIST_INSERT_BEFORE(&service->handle_list, &p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800155
Summer Qin630c76b2020-05-20 10:32:58 +0800156 return p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800157}
158
Summer Qin630c76b2020-05-20 10:32:58 +0800159int32_t tfm_spm_validate_conn_handle(
160 const struct tfm_conn_handle_t *conn_handle,
161 int32_t client_id)
Summer Qin1ce712a2019-10-14 18:04:05 +0800162{
163 /* Check the handle address is validated */
164 if (is_valid_chunk_data_in_pool(conn_handle_pool,
165 (uint8_t *)conn_handle) != true) {
Ken Liubcae38b2021-01-20 15:47:44 +0800166 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800167 }
168
169 /* Check the handle caller is correct */
Summer Qin630c76b2020-05-20 10:32:58 +0800170 if (conn_handle->client_id != client_id) {
Ken Liubcae38b2021-01-20 15:47:44 +0800171 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800172 }
173
Ken Liubcae38b2021-01-20 15:47:44 +0800174 return SPM_SUCCESS;
Summer Qin1ce712a2019-10-14 18:04:05 +0800175}
176
Mingyang Sun783a59b2021-04-20 15:52:18 +0800177int32_t tfm_spm_free_conn_handle(struct service_t *service,
Summer Qin02f7f072020-08-24 16:02:54 +0800178 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800179{
Mingyang Sun620c8562021-11-10 11:44:58 +0800180 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
181
Ken Liuf250b8b2019-12-27 16:31:24 +0800182 TFM_CORE_ASSERT(service);
Summer Qin630c76b2020-05-20 10:32:58 +0800183 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800184
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200185 /* Clear magic as the handler is not used anymore */
Summer Qin630c76b2020-05-20 10:32:58 +0800186 conn_handle->internal_msg.magic = 0;
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200187
Mingyang Sun620c8562021-11-10 11:44:58 +0800188 CRITICAL_SECTION_ENTER(cs_assert);
Edison Ai764d41f2018-09-21 15:56:36 +0800189 /* Remove node from handle list */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800190 BI_LIST_REMOVE_NODE(&conn_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800191
192 /* Back handle buffer to pool */
Ken Liu66ca6132021-02-24 08:49:51 +0800193 tfm_pool_free(conn_handle_pool, conn_handle);
Mingyang Sun620c8562021-11-10 11:44:58 +0800194 CRITICAL_SECTION_LEAVE(cs_assert);
195
Ken Liubcae38b2021-01-20 15:47:44 +0800196 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800197}
198
Mingyang Sun783a59b2021-04-20 15:52:18 +0800199int32_t tfm_spm_set_rhandle(struct service_t *service,
Summer Qin02f7f072020-08-24 16:02:54 +0800200 struct tfm_conn_handle_t *conn_handle,
201 void *rhandle)
Edison Ai764d41f2018-09-21 15:56:36 +0800202{
Ken Liuf250b8b2019-12-27 16:31:24 +0800203 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800204 /* Set reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800205 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800206
Summer Qin630c76b2020-05-20 10:32:58 +0800207 conn_handle->rhandle = rhandle;
Ken Liubcae38b2021-01-20 15:47:44 +0800208 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800209}
210
Mingyang Sund44522a2020-01-16 16:48:37 +0800211/**
Xinyu Zhang3a453242021-04-16 17:57:09 +0800212 * \brief Get reverse handle value from connection handle.
Mingyang Sund44522a2020-01-16 16:48:37 +0800213 *
214 * \param[in] service Target service context pointer
215 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800216 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800217 *
218 * \retval void * Success
219 * \retval "Does not return" Panic for those:
220 * service pointer are NULL
Xinyu Zhang3a453242021-04-16 17:57:09 +0800221 * handle is \ref PSA_NULL_HANDLE
Mingyang Sund44522a2020-01-16 16:48:37 +0800222 * handle node does not be found
223 */
Mingyang Sun783a59b2021-04-20 15:52:18 +0800224static void *tfm_spm_get_rhandle(struct service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800225 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800226{
Ken Liuf250b8b2019-12-27 16:31:24 +0800227 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800228 /* Get reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800229 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800230
Summer Qin630c76b2020-05-20 10:32:58 +0800231 return conn_handle->rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800232}
233
234/* Partition management functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800235
Summer Qin02f7f072020-08-24 16:02:54 +0800236struct tfm_msg_body_t *tfm_spm_get_msg_by_signal(struct partition_t *partition,
237 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800238{
Ken Liu2c47f7f2021-01-22 11:06:04 +0800239 struct bi_list_node_t *node, *head;
Mingyang Sun73056b62020-07-03 15:18:46 +0800240 struct tfm_msg_body_t *tmp_msg, *msg = NULL;
Ken Liu92ede9f2021-10-20 09:35:00 +0800241 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Edison Ai764d41f2018-09-21 15:56:36 +0800242
Ken Liuf250b8b2019-12-27 16:31:24 +0800243 TFM_CORE_ASSERT(partition);
Edison Ai764d41f2018-09-21 15:56:36 +0800244
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800245 head = &partition->msg_list;
Mingyang Sun73056b62020-07-03 15:18:46 +0800246
Ken Liu2c47f7f2021-01-22 11:06:04 +0800247 if (BI_LIST_IS_EMPTY(head)) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800248 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800249 }
250
Mingyang Sun73056b62020-07-03 15:18:46 +0800251 /*
252 * There may be multiple messages for this RoT Service signal, do not clear
253 * partition mask until no remaining message. Search may be optimized.
254 */
Mingyang Sun620c8562021-11-10 11:44:58 +0800255 CRITICAL_SECTION_ENTER(cs_assert);
Ken Liu2c47f7f2021-01-22 11:06:04 +0800256 BI_LIST_FOR_EACH(node, head) {
Ken Liuc25558c2021-05-20 15:31:28 +0800257 tmp_msg = TO_CONTAINER(node, struct tfm_msg_body_t, msg_node);
Ken Liuacd2a572021-05-12 16:19:04 +0800258 if (tmp_msg->service->p_ldinf->signal == signal && msg) {
Mingyang Sun620c8562021-11-10 11:44:58 +0800259 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sun73056b62020-07-03 15:18:46 +0800260 return msg;
Ken Liuacd2a572021-05-12 16:19:04 +0800261 } else if (tmp_msg->service->p_ldinf->signal == signal) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800262 msg = tmp_msg;
Ken Liu2c47f7f2021-01-22 11:06:04 +0800263 BI_LIST_REMOVE_NODE(node);
Edison Ai764d41f2018-09-21 15:56:36 +0800264 }
265 }
Mingyang Sun73056b62020-07-03 15:18:46 +0800266
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800267 partition->signals_asserted &= ~signal;
Ken Liu92ede9f2021-10-20 09:35:00 +0800268 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sun73056b62020-07-03 15:18:46 +0800269
270 return msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800271}
272
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800273uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
274{
Kevin Pengbf9a97e2021-06-18 16:34:12 +0800275#if TFM_LVL == 1
276 return TFM_PARTITION_PRIVILEGED_MODE;
277#else /* TFM_LVL == 1 */
Shawn Shana3657e82021-10-18 14:37:09 +0800278 if (partition_flags & PARTITION_MODEL_PSA_ROT) {
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800279 return TFM_PARTITION_PRIVILEGED_MODE;
280 } else {
281 return TFM_PARTITION_UNPRIVILEGED_MODE;
282 }
Kevin Pengbf9a97e2021-06-18 16:34:12 +0800283#endif /* TFM_LVL == 1 */
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800284}
285
Mingyang Sun783a59b2021-04-20 15:52:18 +0800286struct service_t *tfm_spm_get_service_by_sid(uint32_t sid)
Edison Ai764d41f2018-09-21 15:56:36 +0800287{
Feder.Liangaeb5a792021-08-10 16:12:56 +0800288 struct service_t *p_prev, *p_curr;
Edison Ai764d41f2018-09-21 15:56:36 +0800289
Feder.Liangaeb5a792021-08-10 16:12:56 +0800290 UNI_LIST_FOR_EACH_PREV(p_prev, p_curr, &services_listhead) {
291 if (p_curr->p_ldinf->sid == sid) {
292 UNI_LIST_MOVE_AFTER(&services_listhead, p_prev, p_curr);
293 return p_curr;
Mingyang Sunef42f442021-06-11 15:07:58 +0800294 }
295 }
296
Ken Liuea45b0d2021-05-22 17:41:25 +0800297 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800298}
299
Mingyang Sund44522a2020-01-16 16:48:37 +0800300/**
301 * \brief Get the partition context by partition ID.
302 *
303 * \param[in] partition_id Partition identity
304 *
305 * \retval NULL Failed
306 * \retval "Not NULL" Target partition context pointer,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800307 * \ref partition_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800308 */
Kevin Pengeca45b92021-02-09 14:46:50 +0800309struct partition_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800310{
Ken Liuea45b0d2021-05-22 17:41:25 +0800311 struct partition_t *p_part;
Edison Ai764d41f2018-09-21 15:56:36 +0800312
Mingyang Sundeae45d2021-09-06 15:31:07 +0800313 UNI_LIST_FOR_EACH(p_part, PARTITION_LIST_ADDR) {
Ken Liuea45b0d2021-05-22 17:41:25 +0800314 if (p_part->p_ldinf->pid == partition_id) {
315 return p_part;
316 }
Edison Ai764d41f2018-09-21 15:56:36 +0800317 }
Ken Liuea45b0d2021-05-22 17:41:25 +0800318
Edison Ai764d41f2018-09-21 15:56:36 +0800319 return NULL;
320}
321
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800322struct partition_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800323{
Ken Liuf39d8eb2021-10-07 12:55:33 +0800324 return GET_CURRENT_COMPONENT();
Edison Ai764d41f2018-09-21 15:56:36 +0800325}
326
Mingyang Sun783a59b2021-04-20 15:52:18 +0800327int32_t tfm_spm_check_client_version(struct service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530328 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800329{
Ken Liuf250b8b2019-12-27 16:31:24 +0800330 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800331
Ken Liuacd2a572021-05-12 16:19:04 +0800332 switch (SERVICE_GET_VERSION_POLICY(service->p_ldinf->flags)) {
Ken Liub3b2cb62021-05-22 00:39:28 +0800333 case SERVICE_VERSION_POLICY_RELAXED:
Ken Liuacd2a572021-05-12 16:19:04 +0800334 if (version > service->p_ldinf->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800335 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800336 }
337 break;
Ken Liub3b2cb62021-05-22 00:39:28 +0800338 case SERVICE_VERSION_POLICY_STRICT:
Ken Liuacd2a572021-05-12 16:19:04 +0800339 if (version != service->p_ldinf->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800340 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800341 }
342 break;
343 default:
Ken Liubcae38b2021-01-20 15:47:44 +0800344 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800345 }
Ken Liubcae38b2021-01-20 15:47:44 +0800346 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800347}
348
Edison Aie728fbf2019-11-13 09:37:12 +0800349int32_t tfm_spm_check_authorization(uint32_t sid,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800350 struct service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800351 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800352{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800353 struct partition_t *partition = NULL;
Mingyang Sun2b352662021-04-21 11:35:43 +0800354 uint32_t *dep;
Edison Aie728fbf2019-11-13 09:37:12 +0800355 int32_t i;
356
Ken Liuf250b8b2019-12-27 16:31:24 +0800357 TFM_CORE_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800358
359 if (ns_caller) {
Ken Liuacd2a572021-05-12 16:19:04 +0800360 if (!SERVICE_IS_NS_ACCESSIBLE(service->p_ldinf->flags)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800361 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800362 }
363 } else {
364 partition = tfm_spm_get_running_partition();
365 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800366 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800367 }
368
Ken Liuacd2a572021-05-12 16:19:04 +0800369 dep = (uint32_t *)LOAD_INFO_DEPS(partition->p_ldinf);
370 for (i = 0; i < partition->p_ldinf->ndeps; i++) {
Mingyang Sun2b352662021-04-21 11:35:43 +0800371 if (dep[i] == sid) {
Edison Aie728fbf2019-11-13 09:37:12 +0800372 break;
373 }
374 }
375
Ken Liuacd2a572021-05-12 16:19:04 +0800376 if (i == partition->p_ldinf->ndeps) {
Ken Liubcae38b2021-01-20 15:47:44 +0800377 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800378 }
379 }
Ken Liubcae38b2021-01-20 15:47:44 +0800380 return SPM_SUCCESS;
Edison Aie728fbf2019-11-13 09:37:12 +0800381}
382
Edison Ai764d41f2018-09-21 15:56:36 +0800383/* Message functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800384
Summer Qin02f7f072020-08-24 16:02:54 +0800385struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800386{
387 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200388 * The message handler passed by the caller is considered invalid in the
389 * following cases:
390 * 1. Not a valid message handle. (The address of a message is not the
391 * address of a possible handle from the pool
392 * 2. Handle not belongs to the caller partition (The handle is either
393 * unused, or owned by anither partition)
394 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800395 */
Ken Liu505b1702020-05-29 13:19:58 +0800396 struct tfm_msg_body_t *p_msg;
Kevin Pengeec41a82021-08-18 13:56:23 +0800397 int32_t partition_id;
Ken Liu505b1702020-05-29 13:19:58 +0800398 struct tfm_conn_handle_t *p_conn_handle =
Ken Liu5d73c872021-08-19 19:23:17 +0800399 tfm_spm_to_handle_instance(msg_handle);
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200400
401 if (is_valid_chunk_data_in_pool(
Ken Liu505b1702020-05-29 13:19:58 +0800402 conn_handle_pool, (uint8_t *)p_conn_handle) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800403 return NULL;
404 }
405
Ken Liu505b1702020-05-29 13:19:58 +0800406 p_msg = &p_conn_handle->internal_msg;
407
Edison Ai764d41f2018-09-21 15:56:36 +0800408 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200409 * Check that the magic number is correct. This proves that the message
410 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800411 */
Ken Liu505b1702020-05-29 13:19:58 +0800412 if (p_msg->magic != TFM_MSG_MAGIC) {
Edison Ai764d41f2018-09-21 15:56:36 +0800413 return NULL;
414 }
415
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200416 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800417 partition_id = tfm_spm_partition_get_running_partition_id();
Ken Liuacd2a572021-05-12 16:19:04 +0800418 if (partition_id != p_msg->service->partition->p_ldinf->pid) {
Edison Ai764d41f2018-09-21 15:56:36 +0800419 return NULL;
420 }
421
Ken Liu505b1702020-05-29 13:19:58 +0800422 return p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800423}
424
Kevin Pengdf6aa292021-03-11 17:58:50 +0800425struct tfm_msg_body_t *
426 tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle)
427{
428 TFM_CORE_ASSERT(conn_handle != NULL);
429
430 return &(conn_handle->internal_msg);
431}
432
Edison Ai97115822019-08-01 14:22:19 +0800433void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800434 struct service_t *service,
Ken Liu505b1702020-05-29 13:19:58 +0800435 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800436 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800437 psa_invec *invec, size_t in_len,
438 psa_outvec *outvec, size_t out_len,
439 psa_outvec *caller_outvec)
440{
Edison Ai764d41f2018-09-21 15:56:36 +0800441 uint32_t i;
Ken Liu505b1702020-05-29 13:19:58 +0800442 struct tfm_conn_handle_t *conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800443
Ken Liuf250b8b2019-12-27 16:31:24 +0800444 TFM_CORE_ASSERT(msg);
445 TFM_CORE_ASSERT(service);
446 TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
447 TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
448 TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
449 TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
450 TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800451
Edison Ai764d41f2018-09-21 15:56:36 +0800452 /* Clear message buffer before using it */
Ken Liuf39d8eb2021-10-07 12:55:33 +0800453 spm_memset(&msg->msg, 0, sizeof(psa_msg_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800454
Ken Liu5d73c872021-08-19 19:23:17 +0800455 THRD_SYNC_INIT(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800456 msg->magic = TFM_MSG_MAGIC;
457 msg->service = service;
Ken Liuf39d8eb2021-10-07 12:55:33 +0800458 msg->p_client = GET_CURRENT_COMPONENT();
Edison Ai764d41f2018-09-21 15:56:36 +0800459 msg->caller_outvec = caller_outvec;
Summer Qin1ce712a2019-10-14 18:04:05 +0800460 msg->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800461
462 /* Copy contents */
463 msg->msg.type = type;
464
465 for (i = 0; i < in_len; i++) {
466 msg->msg.in_size[i] = invec[i].len;
467 msg->invec[i].base = invec[i].base;
468 }
469
470 for (i = 0; i < out_len; i++) {
471 msg->msg.out_size[i] = outvec[i].len;
472 msg->outvec[i].base = outvec[i].base;
473 /* Out len is used to record the writed number, set 0 here again */
474 msg->outvec[i].len = 0;
475 }
476
Ken Liu505b1702020-05-29 13:19:58 +0800477 /* Use the user connect handle as the message handle */
478 msg->msg.handle = handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800479
Ken Liu505b1702020-05-29 13:19:58 +0800480 conn_handle = tfm_spm_to_handle_instance(handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800481 /* For connected handle, set rhandle to every message */
Ken Liu505b1702020-05-29 13:19:58 +0800482 if (conn_handle) {
483 msg->msg.rhandle = tfm_spm_get_rhandle(service, conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800484 }
David Hu46603dd2019-12-11 18:05:16 +0800485
486 /* Set the private data of NSPE client caller in multi-core topology */
487 if (TFM_CLIENT_ID_IS_NS(client_id)) {
488 tfm_rpc_set_caller_data(msg, client_id);
489 }
Edison Ai764d41f2018-09-21 15:56:36 +0800490}
491
Kevin Pengeec41a82021-08-18 13:56:23 +0800492int32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800493{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800494 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800495
Kevin Peng79c2bda2020-07-24 16:31:12 +0800496 partition = tfm_spm_get_running_partition();
Ken Liuacd2a572021-05-12 16:19:04 +0800497 if (partition && partition->p_ldinf) {
498 return partition->p_ldinf->pid;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800499 } else {
500 return INVALID_PARTITION_ID;
501 }
Edison Ai764d41f2018-09-21 15:56:36 +0800502}
503
Summer Qin43c185d2019-10-10 15:44:42 +0800504int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800505 enum tfm_memory_access_e access,
506 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800507{
Mingyang Sund1ed6732020-08-26 15:52:21 +0800508 enum tfm_hal_status_t err;
509 uint32_t attr = 0;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800510
511 /* If len is zero, this indicates an empty buffer and base is ignored */
512 if (len == 0) {
Ken Liubcae38b2021-01-20 15:47:44 +0800513 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800514 }
515
516 if (!buffer) {
Ken Liubcae38b2021-01-20 15:47:44 +0800517 return SPM_ERROR_BAD_PARAMETERS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800518 }
519
520 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800521 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800522 }
523
Summer Qin424d4db2019-03-25 14:09:51 +0800524 if (access == TFM_MEMORY_ACCESS_RW) {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800525 attr |= (TFM_HAL_ACCESS_READABLE | TFM_HAL_ACCESS_WRITABLE);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800526 } else {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800527 attr |= TFM_HAL_ACCESS_READABLE;
Summer Qin424d4db2019-03-25 14:09:51 +0800528 }
Mingyang Sund1ed6732020-08-26 15:52:21 +0800529
530 if (privileged == TFM_PARTITION_UNPRIVILEGED_MODE) {
531 attr |= TFM_HAL_ACCESS_UNPRIVILEGED;
532 } else {
533 attr &= ~TFM_HAL_ACCESS_UNPRIVILEGED;
534 }
535
536 if (ns_caller) {
537 attr |= TFM_HAL_ACCESS_NS;
538 }
539
540 err = tfm_hal_memory_has_access((uintptr_t)buffer, len, attr);
541
542 if (err == TFM_HAL_SUCCESS) {
Ken Liubcae38b2021-01-20 15:47:44 +0800543 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800544 }
545
Ken Liubcae38b2021-01-20 15:47:44 +0800546 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800547}
548
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800549bool tfm_spm_is_ns_caller(void)
550{
551#if defined(TFM_MULTI_CORE_TOPOLOGY) || defined(FORWARD_PROT_MSG)
552 /* Multi-core NS PSA API request is processed by pendSV. */
553 return (__get_active_exc_num() == EXC_NUM_PENDSV);
554#else
555 struct partition_t *partition = tfm_spm_get_running_partition();
Mingyang Sune529e3b2021-07-12 14:46:30 +0800556
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800557 if (!partition) {
558 tfm_core_panic();
559 }
560
561 return (partition->p_ldinf->pid == TFM_SP_NON_SECURE_ID);
562#endif
563}
564
Mingyang Sune529e3b2021-07-12 14:46:30 +0800565uint32_t tfm_spm_get_caller_privilege_mode(void)
566{
567 struct partition_t *partition;
568
569#if defined(TFM_MULTI_CORE_TOPOLOGY) || defined(FORWARD_PROT_MSG)
570 /*
571 * In multi-core topology, if PSA request is from mailbox, the client
572 * is unprivileged.
573 */
574 if (__get_active_exc_num() == EXC_NUM_PENDSV) {
575 return TFM_PARTITION_UNPRIVILEGED_MODE;
576 }
577#endif
578 partition = tfm_spm_get_running_partition();
579 if (!partition) {
580 tfm_core_panic();
581 }
582
583 return tfm_spm_partition_get_privileged_mode(partition->p_ldinf->flags);
584}
585
Kevin Peng385fda82021-08-18 10:41:19 +0800586int32_t tfm_spm_get_client_id(bool ns_caller)
587{
588 int32_t client_id;
589
590 if (ns_caller) {
591 client_id = tfm_nspm_get_current_client_id();
592 } else {
593 client_id = tfm_spm_partition_get_running_partition_id();
594 }
595
596 if (ns_caller != (client_id < 0)) {
597 /* NS client ID must be negative and Secure ID must >= 0 */
598 tfm_core_panic();
599 }
600
601 return client_id;
602}
603
Ken Liuce2692d2020-02-11 12:39:36 +0800604uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800605{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800606 struct partition_t *partition;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800607 const struct partition_load_info_t *p_pldi;
608 uint32_t service_setting = 0;
Ken Liuce58bfc2021-05-12 17:54:48 +0800609
David Huf07e97d2021-02-15 22:05:40 +0800610#ifdef TFM_FIH_PROFILE_ON
611 fih_int fih_rc = FIH_FAILURE;
612#endif
Edison Ai764d41f2018-09-21 15:56:36 +0800613
614 tfm_pool_init(conn_handle_pool,
615 POOL_BUFFER_SIZE(conn_handle_pool),
616 sizeof(struct tfm_conn_handle_t),
617 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800618
Mingyang Sundeae45d2021-09-06 15:31:07 +0800619 UNI_LISI_INIT_HEAD(PARTITION_LIST_ADDR);
Ken Liuea45b0d2021-05-22 17:41:25 +0800620 UNI_LISI_INIT_HEAD(&services_listhead);
621
Sherry Zhnag482b88b2021-08-19 17:51:47 +0800622 /* Init the nonsecure context. */
623#ifndef TFM_MULTI_CORE_TOPOLOGY
624 tfm_nspm_ctx_init();
625#endif
626
Ken Liuacd2a572021-05-12 16:19:04 +0800627 while (1) {
Mingyang Sundeae45d2021-09-06 15:31:07 +0800628 partition = load_a_partition_assuredly(PARTITION_LIST_ADDR);
Shawn Shan23331062021-09-01 14:58:21 +0800629 if (partition == NO_MORE_PARTITION) {
Ken Liuacd2a572021-05-12 16:19:04 +0800630 break;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800631 }
632
Mingyang Sundeae45d2021-09-06 15:31:07 +0800633 p_pldi = partition->p_ldinf;
Mingyang Sun2b352662021-04-21 11:35:43 +0800634
Mingyang Sundeae45d2021-09-06 15:31:07 +0800635 if (p_pldi->nservices) {
636 service_setting = load_services_assuredly(
637 partition,
638 &services_listhead,
639 stateless_services_ref_tbl,
640 sizeof(stateless_services_ref_tbl));
Kevin Peng27e42272021-05-24 17:58:53 +0800641 }
642
Mingyang Sundeae45d2021-09-06 15:31:07 +0800643 if (p_pldi->nirqs) {
Kevin Peng27e42272021-05-24 17:58:53 +0800644 load_irqs_assuredly(partition);
645 }
646
Mingyang Sundeae45d2021-09-06 15:31:07 +0800647 /* Bind the partition with platform. */
Ken Liuce58bfc2021-05-12 17:54:48 +0800648#if TFM_FIH_PROFILE_ON
649 FIH_CALL(tfm_hal_bind_boundaries, fih_rc, partition->p_ldinf,
Mingyang Sundeae45d2021-09-06 15:31:07 +0800650 &partition->p_boundaries);
Ken Liuce58bfc2021-05-12 17:54:48 +0800651 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
652 tfm_core_panic();
Mingyang Sun61f8fbc2021-06-04 17:49:56 +0800653 }
Ken Liu86686282021-04-27 11:11:15 +0800654#else /* TFM_FIH_PROFILE_ON */
Ken Liuce58bfc2021-05-12 17:54:48 +0800655 if (tfm_hal_bind_boundaries(partition->p_ldinf,
Mingyang Sundeae45d2021-09-06 15:31:07 +0800656 &partition->p_boundaries)
657 != TFM_HAL_SUCCESS) {
Ken Liuce58bfc2021-05-12 17:54:48 +0800658 tfm_core_panic();
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100659 }
Ken Liuce58bfc2021-05-12 17:54:48 +0800660#endif /* TFM_FIH_PROFILE_ON */
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100661
Mingyang Sundeae45d2021-09-06 15:31:07 +0800662 backend_instance.comp_init_assuredly(partition, service_setting);
Edison Ai764d41f2018-09-21 15:56:36 +0800663 }
664
Mingyang Sundeae45d2021-09-06 15:31:07 +0800665 return backend_instance.system_run();
Edison Ai764d41f2018-09-21 15:56:36 +0800666}
Ken Liu2d175172019-03-21 17:08:41 +0800667
Ken Liu5d73c872021-08-19 19:23:17 +0800668/*
669 * Return both current and next context to assembly via AAPCS trick:
670 * - Returning a 64 bit integer by 32-bit R0 and R1.
671 *
672 * This is architecture-specific, hence the scheduler entry and this
673 * 'do_schedule' MAY be different on another architecture.
674 */
675union returning_contexts_t {
676 struct {
677 uint32_t curr;
678 uint32_t next;
679 } ctx;
Ken Liu2d175172019-03-21 17:08:41 +0800680
Ken Liu5d73c872021-08-19 19:23:17 +0800681 uint64_t curr_next_ctxs;
682};
683
684uint64_t do_schedule(void)
685{
Mingyang Sun620c8562021-11-10 11:44:58 +0800686 union returning_contexts_t ret_ctx;
Ken Liu5d73c872021-08-19 19:23:17 +0800687 struct partition_t *p_part_curr, *p_part_next;
688 struct thread_t *pth_next = thrd_next();
689
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
Ken Liuce58bfc2021-05-12 17:54:48 +0800703 /*
704 * If required, let the platform update boundary based on its
705 * implementation. Change privilege, MPU or other configurations.
706 */
707 if (p_part_curr->p_boundaries != p_part_next->p_boundaries) {
708 if (tfm_hal_update_boundaries(p_part_next->p_ldinf,
709 p_part_next->p_boundaries)
710 != TFM_HAL_SUCCESS) {
711 tfm_core_panic();
712 }
713 }
Feder Liang42f5b562021-09-10 17:38:36 +0800714 ARCH_FLUSH_FP_CONTEXT();
Mingyang Sun620c8562021-11-10 11:44:58 +0800715
716 ret_ctx.ctx.next = (uint32_t)pth_next->p_context_ctrl;
717 CURRENT_THREAD = pth_next;
Ken Liu2d175172019-03-21 17:08:41 +0800718 }
David Hufb38d562019-09-23 15:58:34 +0800719
720 /*
721 * Handle pending mailbox message from NS in multi-core topology.
722 * Empty operation on single Armv8-M platform.
723 */
724 tfm_rpc_client_call_handler();
Ken Liu5d73c872021-08-19 19:23:17 +0800725
Mingyang Sun620c8562021-11-10 11:44:58 +0800726 return ret_ctx.curr_next_ctxs;
Ken Liu2d175172019-03-21 17:08:41 +0800727}
Mingyang Sund44522a2020-01-16 16:48:37 +0800728
Summer Qin02f7f072020-08-24 16:02:54 +0800729void update_caller_outvec_len(struct tfm_msg_body_t *msg)
Mingyang Sund44522a2020-01-16 16:48:37 +0800730{
731 uint32_t i;
732
733 /*
734 * FixeMe: abstract these part into dedicated functions to avoid
735 * accessing thread context in psa layer
736 */
Ken Liuf39d8eb2021-10-07 12:55:33 +0800737 /*
738 * If it is a NS request via RPC, the owner of this message is not set.
739 * Or if it is a SFN message, it does not have owner thread state either.
740 */
741 if ((!is_tfm_rpc_msg(msg)) && (msg->sfn_magic != TFM_MSG_MAGIC_SFN)) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800742 TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK);
743 }
744
745 for (i = 0; i < PSA_MAX_IOVEC; i++) {
746 if (msg->msg.out_size[i] == 0) {
747 continue;
748 }
749
750 TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base);
751
752 msg->caller_outvec[i].len = msg->outvec[i].len;
753 }
754}
755
Ken Liu5d73c872021-08-19 19:23:17 +0800756void spm_assert_signal(void *p_pt, psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800757{
Ken Liu92ede9f2021-10-20 09:35:00 +0800758 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Ken Liu5d73c872021-08-19 19:23:17 +0800759 struct partition_t *partition = (struct partition_t *)p_pt;
Mingyang Sund44522a2020-01-16 16:48:37 +0800760
Mingyang Sund44522a2020-01-16 16:48:37 +0800761 if (!partition) {
762 tfm_core_panic();
763 }
764
Ken Liu92ede9f2021-10-20 09:35:00 +0800765 CRITICAL_SECTION_ENTER(cs_assert);
766
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800767 partition->signals_asserted |= signal;
Mingyang Sund44522a2020-01-16 16:48:37 +0800768
Kevin Peng8dac6102021-03-09 16:44:00 +0800769 if (partition->signals_waiting & signal) {
Ken Liu5d73c872021-08-19 19:23:17 +0800770 thrd_wake_up(&partition->waitobj,
771 partition->signals_asserted & partition->signals_waiting);
Kevin Peng8dac6102021-03-09 16:44:00 +0800772 partition->signals_waiting &= ~signal;
773 }
Ken Liu92ede9f2021-10-20 09:35:00 +0800774
775 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sund44522a2020-01-16 16:48:37 +0800776}
777
Kevin Pengeca45b92021-02-09 14:46:50 +0800778__attribute__((naked))
Ken Liu5d73c872021-08-19 19:23:17 +0800779static psa_flih_result_t tfm_flih_deprivileged_handling(void *p_pt,
Kevin Pengec239bb2021-09-03 15:40:27 +0800780 uintptr_t fn_flih,
Ken Liu5d73c872021-08-19 19:23:17 +0800781 void *p_context_ctrl)
Mingyang Sund44522a2020-01-16 16:48:37 +0800782{
Kevin Pengeca45b92021-02-09 14:46:50 +0800783 __ASM volatile("SVC %0 \n"
784 "BX LR \n"
785 : : "I" (TFM_SVC_PREPARE_DEPRIV_FLIH));
786}
Kevin Pengdc791882021-03-12 10:57:12 +0800787
Kevin Pengec239bb2021-09-03 15:40:27 +0800788void spm_handle_interrupt(void *p_pt, struct irq_load_info_t *p_ildi)
Kevin Pengeca45b92021-02-09 14:46:50 +0800789{
Kevin Pengeca45b92021-02-09 14:46:50 +0800790 psa_flih_result_t flih_result;
Kevin Pengec239bb2021-09-03 15:40:27 +0800791 struct partition_t *p_part;
Kevin Pengdc791882021-03-12 10:57:12 +0800792
Kevin Pengec239bb2021-09-03 15:40:27 +0800793 if (!p_pt || !p_ildi) {
Ken Liu5d73c872021-08-19 19:23:17 +0800794 tfm_core_panic();
795 }
Kevin Pengeca45b92021-02-09 14:46:50 +0800796
Kevin Pengec239bb2021-09-03 15:40:27 +0800797 p_part = (struct partition_t *)p_pt;
798
799 if (p_ildi->pid != p_part->p_ldinf->pid) {
800 tfm_core_panic();
801 }
802
803 if (p_ildi->flih_func == NULL) {
Kevin Pengeca45b92021-02-09 14:46:50 +0800804 /* SLIH Model Handling */
Kevin Pengec239bb2021-09-03 15:40:27 +0800805 tfm_hal_irq_disable(p_ildi->source);
Ken Liu5d73c872021-08-19 19:23:17 +0800806 flih_result = PSA_FLIH_SIGNAL;
807 } else {
808 /* FLIH Model Handling */
Kevin Pengec239bb2021-09-03 15:40:27 +0800809 if (tfm_spm_partition_get_privileged_mode(p_part->p_ldinf->flags) ==
Ken Liu5d73c872021-08-19 19:23:17 +0800810 TFM_PARTITION_PRIVILEGED_MODE) {
Kevin Pengec239bb2021-09-03 15:40:27 +0800811 flih_result = p_ildi->flih_func();
Ken Liu5d73c872021-08-19 19:23:17 +0800812 } else {
813 flih_result = tfm_flih_deprivileged_handling(
Mingyang Sun4f08f4d2021-09-10 17:41:28 +0800814 p_part,
815 (uintptr_t)p_ildi->flih_func,
816 CURRENT_THREAD->p_context_ctrl);
Ken Liu5d73c872021-08-19 19:23:17 +0800817 }
Kevin Pengeca45b92021-02-09 14:46:50 +0800818 }
819
Ken Liu5d73c872021-08-19 19:23:17 +0800820 if (flih_result == PSA_FLIH_SIGNAL) {
Kevin Pengec239bb2021-09-03 15:40:27 +0800821 spm_assert_signal(p_pt, p_ildi->signal);
Kevin Pengeca45b92021-02-09 14:46:50 +0800822 }
Mingyang Sund44522a2020-01-16 16:48:37 +0800823}
824
Kevin Peng27e42272021-05-24 17:58:53 +0800825struct irq_load_info_t *get_irq_info_for_signal(
826 const struct partition_load_info_t *p_ldinf,
827 psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800828{
829 size_t i;
Kevin Peng27e42272021-05-24 17:58:53 +0800830 struct irq_load_info_t *irq_info;
Mingyang Sund44522a2020-01-16 16:48:37 +0800831
Ken Liu24dffb22021-02-10 11:03:58 +0800832 if (!IS_ONLY_ONE_BIT_IN_UINT32(signal)) {
Kevin Peng27e42272021-05-24 17:58:53 +0800833 return NULL;
Kevin Peng410bee52021-01-13 16:27:17 +0800834 }
835
Kevin Peng27e42272021-05-24 17:58:53 +0800836 irq_info = (struct irq_load_info_t *)LOAD_INFO_IRQ(p_ldinf);
837 for (i = 0; i < p_ldinf->nirqs; i++) {
838 if (irq_info[i].signal == signal) {
839 return &irq_info[i];
Mingyang Sund44522a2020-01-16 16:48:37 +0800840 }
841 }
Kevin Penga20b5af2021-01-11 11:20:52 +0800842
Kevin Peng27e42272021-05-24 17:58:53 +0800843 return NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800844}