blob: 729374d2092e50706a902ed8e1f7b5db619274b4 [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"
David Huf07e97d2021-02-15 22:05:40 +080011#include "fih.h"
Jamie Foxcc31d402019-01-28 17:13:52 +000012#include "psa/client.h"
13#include "psa/service.h"
Ken Liu5d73c872021-08-19 19:23:17 +080014#include "thread.h"
Ken Liubcae38b2021-01-20 15:47:44 +080015#include "internal_errors.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080016#include "tfm_spm_hal.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080017#include "tfm_api.h"
18#include "tfm_secure_api.h"
19#include "tfm_memory_utils.h"
Mingyang Sund1ed6732020-08-26 15:52:21 +080020#include "tfm_hal_defs.h"
Kevin Pengd399a1f2021-09-08 15:33:14 +080021#include "tfm_hal_interrupt.h"
Mingyang Sund1ed6732020-08-26 15:52:21 +080022#include "tfm_hal_isolation.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080023#include "spm_ipc.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080024#include "tfm_peripherals_def.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080025#include "tfm_core_utils.h"
Kevin Peng385fda82021-08-18 10:41:19 +080026#include "tfm_nspm.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080027#include "tfm_rpc.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080028#include "tfm_core_trustzone.h"
Ken Liu24dffb22021-02-10 11:03:58 +080029#include "lists.h"
Edison Ai764d41f2018-09-21 15:56:36 +080030#include "tfm_pools.h"
Mingyang Sun22a3faf2021-07-09 15:32:47 +080031#include "region.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080032#include "psa_manifest/pid.h"
Mingyang Sun00df2352021-04-15 15:46:08 +080033#include "load/partition_defs.h"
34#include "load/service_defs.h"
Ken Liu86686282021-04-27 11:11:15 +080035#include "load/asset_defs.h"
Ken Liuacd2a572021-05-12 16:19:04 +080036#include "load/spm_load_api.h"
Edison Ai764d41f2018-09-21 15:56:36 +080037
Ken Liuea45b0d2021-05-22 17:41:25 +080038/* Partition and service runtime data list head/runtime data table */
39static struct partition_head_t partitions_listhead;
40static struct service_head_t services_listhead;
Ken Liub3b2cb62021-05-22 00:39:28 +080041struct service_t *stateless_services_ref_tbl[STATIC_HANDLE_NUM_LIMIT];
Summer Qind99509f2019-08-02 17:36:58 +080042
Ken Liu5d73c872021-08-19 19:23:17 +080043struct thread_t *pth_curr;
44
Edison Ai764d41f2018-09-21 15:56:36 +080045/* Pools */
46TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t),
47 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +080048
Summer Qin373feb12020-03-27 15:35:33 +080049/*********************** Connection handle conversion APIs *******************/
50
Summer Qin373feb12020-03-27 15:35:33 +080051#define CONVERSION_FACTOR_BITOFFSET 3
52#define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET)
53/* Set 32 as the maximum */
54#define CONVERSION_FACTOR_VALUE_MAX 0x20
55
56#if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX
57#error "CONVERSION FACTOR OUT OF RANGE"
58#endif
59
60static uint32_t loop_index;
61
62/*
63 * A handle instance psa_handle_t allocated inside SPM is actually a memory
64 * address among the handle pool. Return this handle to the client directly
65 * exposes information of secure memory address. In this case, converting the
66 * handle into another value does not represent the memory address to avoid
67 * exposing secure memory directly to clients.
68 *
69 * This function converts the handle instance into another value by scaling the
70 * handle in pool offset, the converted value is named as a user handle.
71 *
72 * The formula:
73 * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE +
74 * CLIENT_HANDLE_VALUE_MIN + loop_index
75 * where:
76 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
77 * exceed CONVERSION_FACTOR_VALUE_MAX.
78 *
79 * handle_instance in RANGE[POOL_START, POOL_END]
80 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
81 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
82 *
83 * note:
84 * loop_index is used to promise same handle instance is converted into
85 * different user handles in short time.
86 */
Ken Liu505b1702020-05-29 13:19:58 +080087psa_handle_t tfm_spm_to_user_handle(struct tfm_conn_handle_t *handle_instance)
Summer Qin373feb12020-03-27 15:35:33 +080088{
89 psa_handle_t user_handle;
90
91 loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE;
92 user_handle = (psa_handle_t)((((uintptr_t)handle_instance -
93 (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) +
94 CLIENT_HANDLE_VALUE_MIN + loop_index);
95
96 return user_handle;
97}
98
99/*
100 * This function converts a user handle into a corresponded handle instance.
101 * The converted value is validated before returning, an invalid handle instance
102 * is returned as NULL.
103 *
104 * The formula:
105 * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) /
106 * CONVERSION_FACTOR_VALUE) + POOL_START
107 * where:
108 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
109 * exceed CONVERSION_FACTOR_VALUE_MAX.
110 *
111 * handle_instance in RANGE[POOL_START, POOL_END]
112 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
113 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
114 */
115struct tfm_conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle)
116{
117 struct tfm_conn_handle_t *handle_instance;
118
119 if (user_handle == PSA_NULL_HANDLE) {
120 return NULL;
121 }
122
123 handle_instance = (struct tfm_conn_handle_t *)((((uintptr_t)user_handle -
124 CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) +
125 (uintptr_t)conn_handle_pool);
126
127 return handle_instance;
128}
129
Edison Ai764d41f2018-09-21 15:56:36 +0800130/* Service handle management functions */
Mingyang Sun783a59b2021-04-20 15:52:18 +0800131struct tfm_conn_handle_t *tfm_spm_create_conn_handle(struct service_t *service,
132 int32_t client_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800133{
Edison Ai9cc26242019-08-06 11:28:04 +0800134 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800135
Ken Liuf250b8b2019-12-27 16:31:24 +0800136 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800137
138 /* Get buffer for handle list structure from handle pool */
Edison Ai9cc26242019-08-06 11:28:04 +0800139 p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
140 if (!p_handle) {
Summer Qin630c76b2020-05-20 10:32:58 +0800141 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800142 }
143
Edison Ai9cc26242019-08-06 11:28:04 +0800144 p_handle->service = service;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800145 p_handle->status = TFM_HANDLE_STATUS_IDLE;
Summer Qin1ce712a2019-10-14 18:04:05 +0800146 p_handle->client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800147
148 /* Add handle node to list for next psa functions */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800149 BI_LIST_INSERT_BEFORE(&service->handle_list, &p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800150
Summer Qin630c76b2020-05-20 10:32:58 +0800151 return p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800152}
153
Summer Qin630c76b2020-05-20 10:32:58 +0800154int32_t tfm_spm_validate_conn_handle(
155 const struct tfm_conn_handle_t *conn_handle,
156 int32_t client_id)
Summer Qin1ce712a2019-10-14 18:04:05 +0800157{
158 /* Check the handle address is validated */
159 if (is_valid_chunk_data_in_pool(conn_handle_pool,
160 (uint8_t *)conn_handle) != true) {
Ken Liubcae38b2021-01-20 15:47:44 +0800161 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800162 }
163
164 /* Check the handle caller is correct */
Summer Qin630c76b2020-05-20 10:32:58 +0800165 if (conn_handle->client_id != client_id) {
Ken Liubcae38b2021-01-20 15:47:44 +0800166 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800167 }
168
Ken Liubcae38b2021-01-20 15:47:44 +0800169 return SPM_SUCCESS;
Summer Qin1ce712a2019-10-14 18:04:05 +0800170}
171
Mingyang Sun783a59b2021-04-20 15:52:18 +0800172int32_t tfm_spm_free_conn_handle(struct service_t *service,
Summer Qin02f7f072020-08-24 16:02:54 +0800173 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800174{
Ken Liuf250b8b2019-12-27 16:31:24 +0800175 TFM_CORE_ASSERT(service);
Summer Qin630c76b2020-05-20 10:32:58 +0800176 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800177
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200178 /* Clear magic as the handler is not used anymore */
Summer Qin630c76b2020-05-20 10:32:58 +0800179 conn_handle->internal_msg.magic = 0;
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200180
Edison Ai764d41f2018-09-21 15:56:36 +0800181 /* Remove node from handle list */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800182 BI_LIST_REMOVE_NODE(&conn_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800183
184 /* Back handle buffer to pool */
Ken Liu66ca6132021-02-24 08:49:51 +0800185 tfm_pool_free(conn_handle_pool, conn_handle);
Ken Liubcae38b2021-01-20 15:47:44 +0800186 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800187}
188
Mingyang Sun783a59b2021-04-20 15:52:18 +0800189int32_t tfm_spm_set_rhandle(struct service_t *service,
Summer Qin02f7f072020-08-24 16:02:54 +0800190 struct tfm_conn_handle_t *conn_handle,
191 void *rhandle)
Edison Ai764d41f2018-09-21 15:56:36 +0800192{
Ken Liuf250b8b2019-12-27 16:31:24 +0800193 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800194 /* Set reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800195 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800196
Summer Qin630c76b2020-05-20 10:32:58 +0800197 conn_handle->rhandle = rhandle;
Ken Liubcae38b2021-01-20 15:47:44 +0800198 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800199}
200
Mingyang Sund44522a2020-01-16 16:48:37 +0800201/**
Xinyu Zhang3a453242021-04-16 17:57:09 +0800202 * \brief Get reverse handle value from connection handle.
Mingyang Sund44522a2020-01-16 16:48:37 +0800203 *
204 * \param[in] service Target service context pointer
205 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800206 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800207 *
208 * \retval void * Success
209 * \retval "Does not return" Panic for those:
210 * service pointer are NULL
Xinyu Zhang3a453242021-04-16 17:57:09 +0800211 * handle is \ref PSA_NULL_HANDLE
Mingyang Sund44522a2020-01-16 16:48:37 +0800212 * handle node does not be found
213 */
Mingyang Sun783a59b2021-04-20 15:52:18 +0800214static void *tfm_spm_get_rhandle(struct service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800215 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800216{
Ken Liuf250b8b2019-12-27 16:31:24 +0800217 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800218 /* Get reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800219 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800220
Summer Qin630c76b2020-05-20 10:32:58 +0800221 return conn_handle->rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800222}
223
224/* Partition management functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800225
Summer Qin02f7f072020-08-24 16:02:54 +0800226struct tfm_msg_body_t *tfm_spm_get_msg_by_signal(struct partition_t *partition,
227 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800228{
Ken Liu2c47f7f2021-01-22 11:06:04 +0800229 struct bi_list_node_t *node, *head;
Mingyang Sun73056b62020-07-03 15:18:46 +0800230 struct tfm_msg_body_t *tmp_msg, *msg = NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800231
Ken Liuf250b8b2019-12-27 16:31:24 +0800232 TFM_CORE_ASSERT(partition);
Edison Ai764d41f2018-09-21 15:56:36 +0800233
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800234 head = &partition->msg_list;
Mingyang Sun73056b62020-07-03 15:18:46 +0800235
Ken Liu2c47f7f2021-01-22 11:06:04 +0800236 if (BI_LIST_IS_EMPTY(head)) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800237 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800238 }
239
Mingyang Sun73056b62020-07-03 15:18:46 +0800240 /*
241 * There may be multiple messages for this RoT Service signal, do not clear
242 * partition mask until no remaining message. Search may be optimized.
243 */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800244 BI_LIST_FOR_EACH(node, head) {
Ken Liuc25558c2021-05-20 15:31:28 +0800245 tmp_msg = TO_CONTAINER(node, struct tfm_msg_body_t, msg_node);
Ken Liuacd2a572021-05-12 16:19:04 +0800246 if (tmp_msg->service->p_ldinf->signal == signal && msg) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800247 return msg;
Ken Liuacd2a572021-05-12 16:19:04 +0800248 } else if (tmp_msg->service->p_ldinf->signal == signal) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800249 msg = tmp_msg;
Ken Liu2c47f7f2021-01-22 11:06:04 +0800250 BI_LIST_REMOVE_NODE(node);
Edison Ai764d41f2018-09-21 15:56:36 +0800251 }
252 }
Mingyang Sun73056b62020-07-03 15:18:46 +0800253
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800254 partition->signals_asserted &= ~signal;
Mingyang Sun73056b62020-07-03 15:18:46 +0800255
256 return msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800257}
258
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800259uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
260{
Kevin Pengbf9a97e2021-06-18 16:34:12 +0800261#if TFM_LVL == 1
262 return TFM_PARTITION_PRIVILEGED_MODE;
263#else /* TFM_LVL == 1 */
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800264 if (partition_flags & SPM_PART_FLAG_PSA_ROT) {
265 return TFM_PARTITION_PRIVILEGED_MODE;
266 } else {
267 return TFM_PARTITION_UNPRIVILEGED_MODE;
268 }
Kevin Pengbf9a97e2021-06-18 16:34:12 +0800269#endif /* TFM_LVL == 1 */
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800270}
271
Mingyang Sun783a59b2021-04-20 15:52:18 +0800272struct service_t *tfm_spm_get_service_by_sid(uint32_t sid)
Edison Ai764d41f2018-09-21 15:56:36 +0800273{
Feder.Liangaeb5a792021-08-10 16:12:56 +0800274 struct service_t *p_prev, *p_curr;
Edison Ai764d41f2018-09-21 15:56:36 +0800275
Feder.Liangaeb5a792021-08-10 16:12:56 +0800276 UNI_LIST_FOR_EACH_PREV(p_prev, p_curr, &services_listhead) {
277 if (p_curr->p_ldinf->sid == sid) {
278 UNI_LIST_MOVE_AFTER(&services_listhead, p_prev, p_curr);
279 return p_curr;
Mingyang Sunef42f442021-06-11 15:07:58 +0800280 }
281 }
282
Ken Liuea45b0d2021-05-22 17:41:25 +0800283 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800284}
285
Mingyang Sund44522a2020-01-16 16:48:37 +0800286/**
287 * \brief Get the partition context by partition ID.
288 *
289 * \param[in] partition_id Partition identity
290 *
291 * \retval NULL Failed
292 * \retval "Not NULL" Target partition context pointer,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800293 * \ref partition_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800294 */
Kevin Pengeca45b92021-02-09 14:46:50 +0800295struct partition_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800296{
Ken Liuea45b0d2021-05-22 17:41:25 +0800297 struct partition_t *p_part;
Edison Ai764d41f2018-09-21 15:56:36 +0800298
Ken Liuea45b0d2021-05-22 17:41:25 +0800299 UNI_LIST_FOR_EACH(p_part, &partitions_listhead) {
300 if (p_part->p_ldinf->pid == partition_id) {
301 return p_part;
302 }
Edison Ai764d41f2018-09-21 15:56:36 +0800303 }
Ken Liuea45b0d2021-05-22 17:41:25 +0800304
Edison Ai764d41f2018-09-21 15:56:36 +0800305 return NULL;
306}
307
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800308struct partition_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800309{
Ken Liu5d73c872021-08-19 19:23:17 +0800310 return TO_CONTAINER(pth_curr, struct partition_t, thrd);
Edison Ai764d41f2018-09-21 15:56:36 +0800311}
312
Mingyang Sun783a59b2021-04-20 15:52:18 +0800313int32_t tfm_spm_check_client_version(struct service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530314 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800315{
Ken Liuf250b8b2019-12-27 16:31:24 +0800316 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800317
Ken Liuacd2a572021-05-12 16:19:04 +0800318 switch (SERVICE_GET_VERSION_POLICY(service->p_ldinf->flags)) {
Ken Liub3b2cb62021-05-22 00:39:28 +0800319 case SERVICE_VERSION_POLICY_RELAXED:
Ken Liuacd2a572021-05-12 16:19:04 +0800320 if (version > service->p_ldinf->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800321 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800322 }
323 break;
Ken Liub3b2cb62021-05-22 00:39:28 +0800324 case SERVICE_VERSION_POLICY_STRICT:
Ken Liuacd2a572021-05-12 16:19:04 +0800325 if (version != service->p_ldinf->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800326 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800327 }
328 break;
329 default:
Ken Liubcae38b2021-01-20 15:47:44 +0800330 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800331 }
Ken Liubcae38b2021-01-20 15:47:44 +0800332 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800333}
334
Edison Aie728fbf2019-11-13 09:37:12 +0800335int32_t tfm_spm_check_authorization(uint32_t sid,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800336 struct service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800337 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800338{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800339 struct partition_t *partition = NULL;
Mingyang Sun2b352662021-04-21 11:35:43 +0800340 uint32_t *dep;
Edison Aie728fbf2019-11-13 09:37:12 +0800341 int32_t i;
342
Ken Liuf250b8b2019-12-27 16:31:24 +0800343 TFM_CORE_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800344
345 if (ns_caller) {
Ken Liuacd2a572021-05-12 16:19:04 +0800346 if (!SERVICE_IS_NS_ACCESSIBLE(service->p_ldinf->flags)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800347 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800348 }
349 } else {
350 partition = tfm_spm_get_running_partition();
351 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800352 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800353 }
354
Ken Liuacd2a572021-05-12 16:19:04 +0800355 dep = (uint32_t *)LOAD_INFO_DEPS(partition->p_ldinf);
356 for (i = 0; i < partition->p_ldinf->ndeps; i++) {
Mingyang Sun2b352662021-04-21 11:35:43 +0800357 if (dep[i] == sid) {
Edison Aie728fbf2019-11-13 09:37:12 +0800358 break;
359 }
360 }
361
Ken Liuacd2a572021-05-12 16:19:04 +0800362 if (i == partition->p_ldinf->ndeps) {
Ken Liubcae38b2021-01-20 15:47:44 +0800363 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800364 }
365 }
Ken Liubcae38b2021-01-20 15:47:44 +0800366 return SPM_SUCCESS;
Edison Aie728fbf2019-11-13 09:37:12 +0800367}
368
Edison Ai764d41f2018-09-21 15:56:36 +0800369/* Message functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800370
Summer Qin02f7f072020-08-24 16:02:54 +0800371struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800372{
373 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200374 * The message handler passed by the caller is considered invalid in the
375 * following cases:
376 * 1. Not a valid message handle. (The address of a message is not the
377 * address of a possible handle from the pool
378 * 2. Handle not belongs to the caller partition (The handle is either
379 * unused, or owned by anither partition)
380 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800381 */
Ken Liu505b1702020-05-29 13:19:58 +0800382 struct tfm_msg_body_t *p_msg;
Kevin Pengeec41a82021-08-18 13:56:23 +0800383 int32_t partition_id;
Ken Liu505b1702020-05-29 13:19:58 +0800384 struct tfm_conn_handle_t *p_conn_handle =
Ken Liu5d73c872021-08-19 19:23:17 +0800385 tfm_spm_to_handle_instance(msg_handle);
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200386
387 if (is_valid_chunk_data_in_pool(
Ken Liu505b1702020-05-29 13:19:58 +0800388 conn_handle_pool, (uint8_t *)p_conn_handle) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800389 return NULL;
390 }
391
Ken Liu505b1702020-05-29 13:19:58 +0800392 p_msg = &p_conn_handle->internal_msg;
393
Edison Ai764d41f2018-09-21 15:56:36 +0800394 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200395 * Check that the magic number is correct. This proves that the message
396 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800397 */
Ken Liu505b1702020-05-29 13:19:58 +0800398 if (p_msg->magic != TFM_MSG_MAGIC) {
Edison Ai764d41f2018-09-21 15:56:36 +0800399 return NULL;
400 }
401
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200402 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800403 partition_id = tfm_spm_partition_get_running_partition_id();
Ken Liuacd2a572021-05-12 16:19:04 +0800404 if (partition_id != p_msg->service->partition->p_ldinf->pid) {
Edison Ai764d41f2018-09-21 15:56:36 +0800405 return NULL;
406 }
407
Ken Liu505b1702020-05-29 13:19:58 +0800408 return p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800409}
410
Kevin Pengdf6aa292021-03-11 17:58:50 +0800411struct tfm_msg_body_t *
412 tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle)
413{
414 TFM_CORE_ASSERT(conn_handle != NULL);
415
416 return &(conn_handle->internal_msg);
417}
418
Edison Ai97115822019-08-01 14:22:19 +0800419void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800420 struct service_t *service,
Ken Liu505b1702020-05-29 13:19:58 +0800421 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800422 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800423 psa_invec *invec, size_t in_len,
424 psa_outvec *outvec, size_t out_len,
425 psa_outvec *caller_outvec)
426{
Edison Ai764d41f2018-09-21 15:56:36 +0800427 uint32_t i;
Ken Liu505b1702020-05-29 13:19:58 +0800428 struct tfm_conn_handle_t *conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800429
Ken Liuf250b8b2019-12-27 16:31:24 +0800430 TFM_CORE_ASSERT(msg);
431 TFM_CORE_ASSERT(service);
432 TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
433 TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
434 TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
435 TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
436 TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800437
Edison Ai764d41f2018-09-21 15:56:36 +0800438 /* Clear message buffer before using it */
Summer Qinf24dbb52020-07-23 14:53:54 +0800439 spm_memset(msg, 0, sizeof(struct tfm_msg_body_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800440
Ken Liu5d73c872021-08-19 19:23:17 +0800441 THRD_SYNC_INIT(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800442 msg->magic = TFM_MSG_MAGIC;
443 msg->service = service;
Edison Ai764d41f2018-09-21 15:56:36 +0800444 msg->caller_outvec = caller_outvec;
Summer Qin1ce712a2019-10-14 18:04:05 +0800445 msg->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800446
447 /* Copy contents */
448 msg->msg.type = type;
449
450 for (i = 0; i < in_len; i++) {
451 msg->msg.in_size[i] = invec[i].len;
452 msg->invec[i].base = invec[i].base;
453 }
454
455 for (i = 0; i < out_len; i++) {
456 msg->msg.out_size[i] = outvec[i].len;
457 msg->outvec[i].base = outvec[i].base;
458 /* Out len is used to record the writed number, set 0 here again */
459 msg->outvec[i].len = 0;
460 }
461
Ken Liu505b1702020-05-29 13:19:58 +0800462 /* Use the user connect handle as the message handle */
463 msg->msg.handle = handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800464
Ken Liu505b1702020-05-29 13:19:58 +0800465 conn_handle = tfm_spm_to_handle_instance(handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800466 /* For connected handle, set rhandle to every message */
Ken Liu505b1702020-05-29 13:19:58 +0800467 if (conn_handle) {
468 msg->msg.rhandle = tfm_spm_get_rhandle(service, conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800469 }
David Hu46603dd2019-12-11 18:05:16 +0800470
471 /* Set the private data of NSPE client caller in multi-core topology */
472 if (TFM_CLIENT_ID_IS_NS(client_id)) {
473 tfm_rpc_set_caller_data(msg, client_id);
474 }
Edison Ai764d41f2018-09-21 15:56:36 +0800475}
476
Mingyang Sun783a59b2021-04-20 15:52:18 +0800477void tfm_spm_send_event(struct service_t *service,
Kevin Peng8dac6102021-03-09 16:44:00 +0800478 struct tfm_msg_body_t *msg)
Edison Ai764d41f2018-09-21 15:56:36 +0800479{
Kevin Peng8dac6102021-03-09 16:44:00 +0800480 struct partition_t *partition = NULL;
481 psa_signal_t signal = 0;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800482
Ken Liuacd2a572021-05-12 16:19:04 +0800483 if (!msg || !service || !service->p_ldinf || !service->partition) {
Kevin Peng8dac6102021-03-09 16:44:00 +0800484 tfm_core_panic();
485 }
486
487 partition = service->partition;
Ken Liuacd2a572021-05-12 16:19:04 +0800488 signal = service->p_ldinf->signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800489
Mingyang Sun73056b62020-07-03 15:18:46 +0800490 /* Add message to partition message list tail */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800491 BI_LIST_INSERT_BEFORE(&partition->msg_list, &msg->msg_node);
Edison Ai764d41f2018-09-21 15:56:36 +0800492
493 /* Messages put. Update signals */
Kevin Peng8dac6102021-03-09 16:44:00 +0800494 partition->signals_asserted |= signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800495
Kevin Peng8dac6102021-03-09 16:44:00 +0800496 if (partition->signals_waiting & signal) {
Ken Liu5d73c872021-08-19 19:23:17 +0800497 thrd_wake_up(
498 &partition->waitobj,
Kevin Peng8dac6102021-03-09 16:44:00 +0800499 (partition->signals_asserted & partition->signals_waiting));
500 partition->signals_waiting &= ~signal;
501 }
Edison Ai764d41f2018-09-21 15:56:36 +0800502
David Hufb38d562019-09-23 15:58:34 +0800503 /*
504 * If it is a NS request via RPC, it is unnecessary to block current
505 * thread.
506 */
507 if (!is_tfm_rpc_msg(msg)) {
Ken Liu5d73c872021-08-19 19:23:17 +0800508 thrd_wait_on(&msg->ack_evnt, pth_curr);
David Hufb38d562019-09-23 15:58:34 +0800509 }
Edison Ai764d41f2018-09-21 15:56:36 +0800510}
511
Kevin Pengeec41a82021-08-18 13:56:23 +0800512int32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800513{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800514 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800515
Kevin Peng79c2bda2020-07-24 16:31:12 +0800516 partition = tfm_spm_get_running_partition();
Ken Liuacd2a572021-05-12 16:19:04 +0800517 if (partition && partition->p_ldinf) {
518 return partition->p_ldinf->pid;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800519 } else {
520 return INVALID_PARTITION_ID;
521 }
Edison Ai764d41f2018-09-21 15:56:36 +0800522}
523
Summer Qin43c185d2019-10-10 15:44:42 +0800524int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800525 enum tfm_memory_access_e access,
526 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800527{
Mingyang Sund1ed6732020-08-26 15:52:21 +0800528 enum tfm_hal_status_t err;
529 uint32_t attr = 0;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800530
531 /* If len is zero, this indicates an empty buffer and base is ignored */
532 if (len == 0) {
Ken Liubcae38b2021-01-20 15:47:44 +0800533 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800534 }
535
536 if (!buffer) {
Ken Liubcae38b2021-01-20 15:47:44 +0800537 return SPM_ERROR_BAD_PARAMETERS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800538 }
539
540 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800541 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800542 }
543
Summer Qin424d4db2019-03-25 14:09:51 +0800544 if (access == TFM_MEMORY_ACCESS_RW) {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800545 attr |= (TFM_HAL_ACCESS_READABLE | TFM_HAL_ACCESS_WRITABLE);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800546 } else {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800547 attr |= TFM_HAL_ACCESS_READABLE;
Summer Qin424d4db2019-03-25 14:09:51 +0800548 }
Mingyang Sund1ed6732020-08-26 15:52:21 +0800549
550 if (privileged == TFM_PARTITION_UNPRIVILEGED_MODE) {
551 attr |= TFM_HAL_ACCESS_UNPRIVILEGED;
552 } else {
553 attr &= ~TFM_HAL_ACCESS_UNPRIVILEGED;
554 }
555
556 if (ns_caller) {
557 attr |= TFM_HAL_ACCESS_NS;
558 }
559
560 err = tfm_hal_memory_has_access((uintptr_t)buffer, len, attr);
561
562 if (err == TFM_HAL_SUCCESS) {
Ken Liubcae38b2021-01-20 15:47:44 +0800563 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800564 }
565
Ken Liubcae38b2021-01-20 15:47:44 +0800566 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800567}
568
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800569bool tfm_spm_is_ns_caller(void)
570{
571#if defined(TFM_MULTI_CORE_TOPOLOGY) || defined(FORWARD_PROT_MSG)
572 /* Multi-core NS PSA API request is processed by pendSV. */
573 return (__get_active_exc_num() == EXC_NUM_PENDSV);
574#else
575 struct partition_t *partition = tfm_spm_get_running_partition();
Mingyang Sune529e3b2021-07-12 14:46:30 +0800576
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800577 if (!partition) {
578 tfm_core_panic();
579 }
580
581 return (partition->p_ldinf->pid == TFM_SP_NON_SECURE_ID);
582#endif
583}
584
Mingyang Sune529e3b2021-07-12 14:46:30 +0800585uint32_t tfm_spm_get_caller_privilege_mode(void)
586{
587 struct partition_t *partition;
588
589#if defined(TFM_MULTI_CORE_TOPOLOGY) || defined(FORWARD_PROT_MSG)
590 /*
591 * In multi-core topology, if PSA request is from mailbox, the client
592 * is unprivileged.
593 */
594 if (__get_active_exc_num() == EXC_NUM_PENDSV) {
595 return TFM_PARTITION_UNPRIVILEGED_MODE;
596 }
597#endif
598 partition = tfm_spm_get_running_partition();
599 if (!partition) {
600 tfm_core_panic();
601 }
602
603 return tfm_spm_partition_get_privileged_mode(partition->p_ldinf->flags);
604}
605
Kevin Peng385fda82021-08-18 10:41:19 +0800606int32_t tfm_spm_get_client_id(bool ns_caller)
607{
608 int32_t client_id;
609
610 if (ns_caller) {
611 client_id = tfm_nspm_get_current_client_id();
612 } else {
613 client_id = tfm_spm_partition_get_running_partition_id();
614 }
615
616 if (ns_caller != (client_id < 0)) {
617 /* NS client ID must be negative and Secure ID must >= 0 */
618 tfm_core_panic();
619 }
620
621 return client_id;
622}
623
Ken Liuce2692d2020-02-11 12:39:36 +0800624uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800625{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800626 struct partition_t *partition;
Mingyang Sun8d004f72021-06-01 10:46:26 +0800627 const struct partition_load_info_t *p_ldinf;
Ken Liu5d73c872021-08-19 19:23:17 +0800628 void *p_param, *p_boundaries = NULL;
Ken Liuce58bfc2021-05-12 17:54:48 +0800629
David Huf07e97d2021-02-15 22:05:40 +0800630#ifdef TFM_FIH_PROFILE_ON
631 fih_int fih_rc = FIH_FAILURE;
632#endif
Edison Ai764d41f2018-09-21 15:56:36 +0800633
634 tfm_pool_init(conn_handle_pool,
635 POOL_BUFFER_SIZE(conn_handle_pool),
636 sizeof(struct tfm_conn_handle_t),
637 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800638
Ken Liuea45b0d2021-05-22 17:41:25 +0800639 UNI_LISI_INIT_HEAD(&partitions_listhead);
640 UNI_LISI_INIT_HEAD(&services_listhead);
641
Ken Liuacd2a572021-05-12 16:19:04 +0800642 while (1) {
Ken Liuea45b0d2021-05-22 17:41:25 +0800643 partition = load_a_partition_assuredly(&partitions_listhead);
Shawn Shan23331062021-09-01 14:58:21 +0800644 if (partition == NO_MORE_PARTITION) {
Ken Liuacd2a572021-05-12 16:19:04 +0800645 break;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800646 }
647
Mingyang Sun8d004f72021-06-01 10:46:26 +0800648 p_ldinf = partition->p_ldinf;
Mingyang Sun2b352662021-04-21 11:35:43 +0800649
Kevin Peng27e42272021-05-24 17:58:53 +0800650 if (p_ldinf->nservices) {
Ken Liuea45b0d2021-05-22 17:41:25 +0800651 load_services_assuredly(partition, &services_listhead,
Kevin Peng27e42272021-05-24 17:58:53 +0800652 stateless_services_ref_tbl,
653 sizeof(stateless_services_ref_tbl));
654 }
655
656 if (p_ldinf->nirqs) {
657 load_irqs_assuredly(partition);
658 }
659
Ken Liuce58bfc2021-05-12 17:54:48 +0800660 /* Bind the partition with plaform. */
661#if TFM_FIH_PROFILE_ON
662 FIH_CALL(tfm_hal_bind_boundaries, fih_rc, partition->p_ldinf,
663 &p_boundaries);
664 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
665 tfm_core_panic();
Mingyang Sun61f8fbc2021-06-04 17:49:56 +0800666 }
Ken Liu86686282021-04-27 11:11:15 +0800667#else /* TFM_FIH_PROFILE_ON */
Ken Liuce58bfc2021-05-12 17:54:48 +0800668 if (tfm_hal_bind_boundaries(partition->p_ldinf,
669 &p_boundaries) != TFM_HAL_SUCCESS) {
670 tfm_core_panic();
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100671 }
Ken Liuce58bfc2021-05-12 17:54:48 +0800672#endif /* TFM_FIH_PROFILE_ON */
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100673
Ken Liuce58bfc2021-05-12 17:54:48 +0800674 partition->p_boundaries = p_boundaries;
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800675 partition->signals_allowed |= PSA_DOORBELL;
Shawn Shanc7dda0e2019-12-23 14:45:09 +0800676
Ken Liu5d73c872021-08-19 19:23:17 +0800677 THRD_SYNC_INIT(&partition->waitobj);
Ken Liu2c47f7f2021-01-22 11:06:04 +0800678 BI_LIST_INIT_NODE(&partition->msg_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800679
Ken Liu5d73c872021-08-19 19:23:17 +0800680 THRD_INIT(&partition->thrd, &partition->ctx_ctrl,
681 TO_THREAD_PRIORITY(PARTITION_PRIORITY(p_ldinf->flags)));
Edison Ai764d41f2018-09-21 15:56:36 +0800682
Ken Liu5d73c872021-08-19 19:23:17 +0800683 p_param = NULL;
Mingyang Sun8d004f72021-06-01 10:46:26 +0800684 if (p_ldinf->pid == TFM_SP_NON_SECURE_ID) {
Ken Liu5d73c872021-08-19 19:23:17 +0800685 p_param = (void *)tfm_spm_hal_get_ns_entry_point();
Ken Liu490281d2019-12-30 15:55:26 +0800686 }
687
Ken Liu5d73c872021-08-19 19:23:17 +0800688 thrd_start(&partition->thrd,
689 POSITION_TO_ENTRY(p_ldinf->entry, thrd_fn_t), p_param,
690 LOAD_ALLOCED_STACK_ADDR(p_ldinf),
691 LOAD_ALLOCED_STACK_ADDR(p_ldinf) + p_ldinf->stack_size);
Edison Ai764d41f2018-09-21 15:56:36 +0800692 }
693
Ken Liu5d73c872021-08-19 19:23:17 +0800694 return thrd_start_scheduler(&pth_curr);
Edison Ai764d41f2018-09-21 15:56:36 +0800695}
Ken Liu2d175172019-03-21 17:08:41 +0800696
Ken Liu5d73c872021-08-19 19:23:17 +0800697/*
698 * Return both current and next context to assembly via AAPCS trick:
699 * - Returning a 64 bit integer by 32-bit R0 and R1.
700 *
701 * This is architecture-specific, hence the scheduler entry and this
702 * 'do_schedule' MAY be different on another architecture.
703 */
704union returning_contexts_t {
705 struct {
706 uint32_t curr;
707 uint32_t next;
708 } ctx;
Ken Liu2d175172019-03-21 17:08:41 +0800709
Ken Liu5d73c872021-08-19 19:23:17 +0800710 uint64_t curr_next_ctxs;
711};
712
713uint64_t do_schedule(void)
714{
715 union returning_contexts_t ret;
716 struct partition_t *p_part_curr, *p_part_next;
717 struct thread_t *pth_next = thrd_next();
718
719 p_part_curr = TO_CONTAINER(pth_curr, struct partition_t, thrd);
720 p_part_next = TO_CONTAINER(pth_next, struct partition_t, thrd);
721
722 if (pth_next != NULL && p_part_curr != p_part_next) {
723 /* Check if there is enough room on stack to save more context */
724 if ((p_part_curr->ctx_ctrl.sp_limit +
725 sizeof(struct tfm_additional_context_t)) > __get_PSP()) {
726 tfm_core_panic();
727 }
Mate Toth-Palc430b992019-05-09 21:01:14 +0200728
Ken Liuce58bfc2021-05-12 17:54:48 +0800729 /*
730 * If required, let the platform update boundary based on its
731 * implementation. Change privilege, MPU or other configurations.
732 */
733 if (p_part_curr->p_boundaries != p_part_next->p_boundaries) {
734 if (tfm_hal_update_boundaries(p_part_next->p_ldinf,
735 p_part_next->p_boundaries)
736 != TFM_HAL_SUCCESS) {
737 tfm_core_panic();
738 }
739 }
Ken Liu2d175172019-03-21 17:08:41 +0800740 }
David Hufb38d562019-09-23 15:58:34 +0800741
742 /*
743 * Handle pending mailbox message from NS in multi-core topology.
744 * Empty operation on single Armv8-M platform.
745 */
746 tfm_rpc_client_call_handler();
Ken Liu5d73c872021-08-19 19:23:17 +0800747
748 ret.ctx.curr = (uint32_t)pth_curr->p_context_ctrl;
749 ret.ctx.next = (uint32_t)pth_next->p_context_ctrl;
750
751 pth_curr = pth_next;
752
753 return ret.curr_next_ctxs;
Ken Liu2d175172019-03-21 17:08:41 +0800754}
Mingyang Sund44522a2020-01-16 16:48:37 +0800755
Summer Qin02f7f072020-08-24 16:02:54 +0800756void update_caller_outvec_len(struct tfm_msg_body_t *msg)
Mingyang Sund44522a2020-01-16 16:48:37 +0800757{
758 uint32_t i;
759
760 /*
761 * FixeMe: abstract these part into dedicated functions to avoid
762 * accessing thread context in psa layer
763 */
764 /* If it is a NS request via RPC, the owner of this message is not set */
765 if (!is_tfm_rpc_msg(msg)) {
766 TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK);
767 }
768
769 for (i = 0; i < PSA_MAX_IOVEC; i++) {
770 if (msg->msg.out_size[i] == 0) {
771 continue;
772 }
773
774 TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base);
775
776 msg->caller_outvec[i].len = msg->outvec[i].len;
777 }
778}
779
Ken Liu5d73c872021-08-19 19:23:17 +0800780void spm_assert_signal(void *p_pt, psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800781{
Ken Liu5d73c872021-08-19 19:23:17 +0800782 struct partition_t *partition = (struct partition_t *)p_pt;
Mingyang Sund44522a2020-01-16 16:48:37 +0800783
Mingyang Sund44522a2020-01-16 16:48:37 +0800784 if (!partition) {
785 tfm_core_panic();
786 }
787
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800788 partition->signals_asserted |= signal;
Mingyang Sund44522a2020-01-16 16:48:37 +0800789
Kevin Peng8dac6102021-03-09 16:44:00 +0800790 if (partition->signals_waiting & signal) {
Ken Liu5d73c872021-08-19 19:23:17 +0800791 thrd_wake_up(&partition->waitobj,
792 partition->signals_asserted & partition->signals_waiting);
Kevin Peng8dac6102021-03-09 16:44:00 +0800793 partition->signals_waiting &= ~signal;
794 }
Mingyang Sund44522a2020-01-16 16:48:37 +0800795}
796
Kevin Pengeca45b92021-02-09 14:46:50 +0800797__attribute__((naked))
Ken Liu5d73c872021-08-19 19:23:17 +0800798static psa_flih_result_t tfm_flih_deprivileged_handling(void *p_pt,
Kevin Pengec239bb2021-09-03 15:40:27 +0800799 uintptr_t fn_flih,
Ken Liu5d73c872021-08-19 19:23:17 +0800800 void *p_context_ctrl)
Mingyang Sund44522a2020-01-16 16:48:37 +0800801{
Kevin Pengeca45b92021-02-09 14:46:50 +0800802 __ASM volatile("SVC %0 \n"
803 "BX LR \n"
804 : : "I" (TFM_SVC_PREPARE_DEPRIV_FLIH));
805}
Kevin Pengdc791882021-03-12 10:57:12 +0800806
Kevin Pengec239bb2021-09-03 15:40:27 +0800807void spm_handle_interrupt(void *p_pt, struct irq_load_info_t *p_ildi)
Kevin Pengeca45b92021-02-09 14:46:50 +0800808{
Kevin Pengeca45b92021-02-09 14:46:50 +0800809 psa_flih_result_t flih_result;
Kevin Pengec239bb2021-09-03 15:40:27 +0800810 struct partition_t *p_part;
Kevin Pengdc791882021-03-12 10:57:12 +0800811
Kevin Pengec239bb2021-09-03 15:40:27 +0800812 if (!p_pt || !p_ildi) {
Ken Liu5d73c872021-08-19 19:23:17 +0800813 tfm_core_panic();
814 }
Kevin Pengeca45b92021-02-09 14:46:50 +0800815
Kevin Pengec239bb2021-09-03 15:40:27 +0800816 p_part = (struct partition_t *)p_pt;
817
818 if (p_ildi->pid != p_part->p_ldinf->pid) {
819 tfm_core_panic();
820 }
821
822 if (p_ildi->flih_func == NULL) {
Kevin Pengeca45b92021-02-09 14:46:50 +0800823 /* SLIH Model Handling */
Kevin Pengec239bb2021-09-03 15:40:27 +0800824 tfm_hal_irq_disable(p_ildi->source);
Ken Liu5d73c872021-08-19 19:23:17 +0800825 flih_result = PSA_FLIH_SIGNAL;
826 } else {
827 /* FLIH Model Handling */
Kevin Pengec239bb2021-09-03 15:40:27 +0800828 if (tfm_spm_partition_get_privileged_mode(p_part->p_ldinf->flags) ==
Ken Liu5d73c872021-08-19 19:23:17 +0800829 TFM_PARTITION_PRIVILEGED_MODE) {
Kevin Pengec239bb2021-09-03 15:40:27 +0800830 flih_result = p_ildi->flih_func();
Ken Liu5d73c872021-08-19 19:23:17 +0800831 } else {
832 flih_result = tfm_flih_deprivileged_handling(
Kevin Pengec239bb2021-09-03 15:40:27 +0800833 p_part,
834 (uintptr_t)p_ildi->flih_func,
835 pth_curr->p_context_ctrl);
Ken Liu5d73c872021-08-19 19:23:17 +0800836 }
Kevin Pengeca45b92021-02-09 14:46:50 +0800837 }
838
Ken Liu5d73c872021-08-19 19:23:17 +0800839 if (flih_result == PSA_FLIH_SIGNAL) {
840 __disable_irq();
Kevin Pengec239bb2021-09-03 15:40:27 +0800841 spm_assert_signal(p_pt, p_ildi->signal);
Ken Liu5d73c872021-08-19 19:23:17 +0800842 __enable_irq();
Kevin Pengeca45b92021-02-09 14:46:50 +0800843 }
Mingyang Sund44522a2020-01-16 16:48:37 +0800844}
845
Kevin Peng27e42272021-05-24 17:58:53 +0800846struct irq_load_info_t *get_irq_info_for_signal(
847 const struct partition_load_info_t *p_ldinf,
848 psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800849{
850 size_t i;
Kevin Peng27e42272021-05-24 17:58:53 +0800851 struct irq_load_info_t *irq_info;
Mingyang Sund44522a2020-01-16 16:48:37 +0800852
Ken Liu24dffb22021-02-10 11:03:58 +0800853 if (!IS_ONLY_ONE_BIT_IN_UINT32(signal)) {
Kevin Peng27e42272021-05-24 17:58:53 +0800854 return NULL;
Kevin Peng410bee52021-01-13 16:27:17 +0800855 }
856
Kevin Peng27e42272021-05-24 17:58:53 +0800857 irq_info = (struct irq_load_info_t *)LOAD_INFO_IRQ(p_ldinf);
858 for (i = 0; i < p_ldinf->nirqs; i++) {
859 if (irq_info[i].signal == signal) {
860 return &irq_info[i];
Mingyang Sund44522a2020-01-16 16:48:37 +0800861 }
862 }
Kevin Penga20b5af2021-01-11 11:20:52 +0800863
Kevin Peng27e42272021-05-24 17:58:53 +0800864 return NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800865}