blob: 4e8960fdd070dfe533a282fb4f8b7063e2bf0026 [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
Edison Ai764d41f2018-09-21 15:56:36 +080043/* Pools */
44TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t),
45 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +080046
Summer Qin373feb12020-03-27 15:35:33 +080047/*********************** Connection handle conversion APIs *******************/
48
Summer Qin373feb12020-03-27 15:35:33 +080049#define CONVERSION_FACTOR_BITOFFSET 3
50#define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET)
51/* Set 32 as the maximum */
52#define CONVERSION_FACTOR_VALUE_MAX 0x20
53
54#if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX
55#error "CONVERSION FACTOR OUT OF RANGE"
56#endif
57
58static uint32_t loop_index;
59
60/*
61 * A handle instance psa_handle_t allocated inside SPM is actually a memory
62 * address among the handle pool. Return this handle to the client directly
63 * exposes information of secure memory address. In this case, converting the
64 * handle into another value does not represent the memory address to avoid
65 * exposing secure memory directly to clients.
66 *
67 * This function converts the handle instance into another value by scaling the
68 * handle in pool offset, the converted value is named as a user handle.
69 *
70 * The formula:
71 * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE +
72 * CLIENT_HANDLE_VALUE_MIN + loop_index
73 * where:
74 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
75 * exceed CONVERSION_FACTOR_VALUE_MAX.
76 *
77 * handle_instance in RANGE[POOL_START, POOL_END]
78 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
79 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
80 *
81 * note:
82 * loop_index is used to promise same handle instance is converted into
83 * different user handles in short time.
84 */
Ken Liu505b1702020-05-29 13:19:58 +080085psa_handle_t tfm_spm_to_user_handle(struct tfm_conn_handle_t *handle_instance)
Summer Qin373feb12020-03-27 15:35:33 +080086{
87 psa_handle_t user_handle;
88
89 loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE;
90 user_handle = (psa_handle_t)((((uintptr_t)handle_instance -
91 (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) +
92 CLIENT_HANDLE_VALUE_MIN + loop_index);
93
94 return user_handle;
95}
96
97/*
98 * This function converts a user handle into a corresponded handle instance.
99 * The converted value is validated before returning, an invalid handle instance
100 * is returned as NULL.
101 *
102 * The formula:
103 * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) /
104 * CONVERSION_FACTOR_VALUE) + POOL_START
105 * where:
106 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
107 * exceed CONVERSION_FACTOR_VALUE_MAX.
108 *
109 * handle_instance in RANGE[POOL_START, POOL_END]
110 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
111 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
112 */
113struct tfm_conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle)
114{
115 struct tfm_conn_handle_t *handle_instance;
116
117 if (user_handle == PSA_NULL_HANDLE) {
118 return NULL;
119 }
120
121 handle_instance = (struct tfm_conn_handle_t *)((((uintptr_t)user_handle -
122 CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) +
123 (uintptr_t)conn_handle_pool);
124
125 return handle_instance;
126}
127
Edison Ai764d41f2018-09-21 15:56:36 +0800128/* Service handle management functions */
Mingyang Sun783a59b2021-04-20 15:52:18 +0800129struct tfm_conn_handle_t *tfm_spm_create_conn_handle(struct service_t *service,
130 int32_t client_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800131{
Edison Ai9cc26242019-08-06 11:28:04 +0800132 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800133
Ken Liuf250b8b2019-12-27 16:31:24 +0800134 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800135
136 /* Get buffer for handle list structure from handle pool */
Edison Ai9cc26242019-08-06 11:28:04 +0800137 p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
138 if (!p_handle) {
Summer Qin630c76b2020-05-20 10:32:58 +0800139 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800140 }
141
Edison Ai9cc26242019-08-06 11:28:04 +0800142 p_handle->service = service;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800143 p_handle->status = TFM_HANDLE_STATUS_IDLE;
Summer Qin1ce712a2019-10-14 18:04:05 +0800144 p_handle->client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800145
146 /* Add handle node to list for next psa functions */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800147 BI_LIST_INSERT_BEFORE(&service->handle_list, &p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800148
Summer Qin630c76b2020-05-20 10:32:58 +0800149 return p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800150}
151
Summer Qin630c76b2020-05-20 10:32:58 +0800152int32_t tfm_spm_validate_conn_handle(
153 const struct tfm_conn_handle_t *conn_handle,
154 int32_t client_id)
Summer Qin1ce712a2019-10-14 18:04:05 +0800155{
156 /* Check the handle address is validated */
157 if (is_valid_chunk_data_in_pool(conn_handle_pool,
158 (uint8_t *)conn_handle) != true) {
Ken Liubcae38b2021-01-20 15:47:44 +0800159 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800160 }
161
162 /* Check the handle caller is correct */
Summer Qin630c76b2020-05-20 10:32:58 +0800163 if (conn_handle->client_id != client_id) {
Ken Liubcae38b2021-01-20 15:47:44 +0800164 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800165 }
166
Ken Liubcae38b2021-01-20 15:47:44 +0800167 return SPM_SUCCESS;
Summer Qin1ce712a2019-10-14 18:04:05 +0800168}
169
Mingyang Sun783a59b2021-04-20 15:52:18 +0800170int32_t tfm_spm_free_conn_handle(struct service_t *service,
Summer Qin02f7f072020-08-24 16:02:54 +0800171 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800172{
Ken Liuf250b8b2019-12-27 16:31:24 +0800173 TFM_CORE_ASSERT(service);
Summer Qin630c76b2020-05-20 10:32:58 +0800174 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800175
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200176 /* Clear magic as the handler is not used anymore */
Summer Qin630c76b2020-05-20 10:32:58 +0800177 conn_handle->internal_msg.magic = 0;
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200178
Edison Ai764d41f2018-09-21 15:56:36 +0800179 /* Remove node from handle list */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800180 BI_LIST_REMOVE_NODE(&conn_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800181
182 /* Back handle buffer to pool */
Ken Liu66ca6132021-02-24 08:49:51 +0800183 tfm_pool_free(conn_handle_pool, conn_handle);
Ken Liubcae38b2021-01-20 15:47:44 +0800184 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800185}
186
Mingyang Sun783a59b2021-04-20 15:52:18 +0800187int32_t tfm_spm_set_rhandle(struct service_t *service,
Summer Qin02f7f072020-08-24 16:02:54 +0800188 struct tfm_conn_handle_t *conn_handle,
189 void *rhandle)
Edison Ai764d41f2018-09-21 15:56:36 +0800190{
Ken Liuf250b8b2019-12-27 16:31:24 +0800191 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800192 /* Set reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800193 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800194
Summer Qin630c76b2020-05-20 10:32:58 +0800195 conn_handle->rhandle = rhandle;
Ken Liubcae38b2021-01-20 15:47:44 +0800196 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800197}
198
Mingyang Sund44522a2020-01-16 16:48:37 +0800199/**
Xinyu Zhang3a453242021-04-16 17:57:09 +0800200 * \brief Get reverse handle value from connection handle.
Mingyang Sund44522a2020-01-16 16:48:37 +0800201 *
202 * \param[in] service Target service context pointer
203 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800204 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800205 *
206 * \retval void * Success
207 * \retval "Does not return" Panic for those:
208 * service pointer are NULL
Xinyu Zhang3a453242021-04-16 17:57:09 +0800209 * handle is \ref PSA_NULL_HANDLE
Mingyang Sund44522a2020-01-16 16:48:37 +0800210 * handle node does not be found
211 */
Mingyang Sun783a59b2021-04-20 15:52:18 +0800212static void *tfm_spm_get_rhandle(struct service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800213 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800214{
Ken Liuf250b8b2019-12-27 16:31:24 +0800215 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800216 /* Get reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800217 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800218
Summer Qin630c76b2020-05-20 10:32:58 +0800219 return conn_handle->rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800220}
221
222/* Partition management functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800223
Summer Qin02f7f072020-08-24 16:02:54 +0800224struct tfm_msg_body_t *tfm_spm_get_msg_by_signal(struct partition_t *partition,
225 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800226{
Ken Liu2c47f7f2021-01-22 11:06:04 +0800227 struct bi_list_node_t *node, *head;
Mingyang Sun73056b62020-07-03 15:18:46 +0800228 struct tfm_msg_body_t *tmp_msg, *msg = NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800229
Ken Liuf250b8b2019-12-27 16:31:24 +0800230 TFM_CORE_ASSERT(partition);
Edison Ai764d41f2018-09-21 15:56:36 +0800231
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800232 head = &partition->msg_list;
Mingyang Sun73056b62020-07-03 15:18:46 +0800233
Ken Liu2c47f7f2021-01-22 11:06:04 +0800234 if (BI_LIST_IS_EMPTY(head)) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800235 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800236 }
237
Mingyang Sun73056b62020-07-03 15:18:46 +0800238 /*
239 * There may be multiple messages for this RoT Service signal, do not clear
240 * partition mask until no remaining message. Search may be optimized.
241 */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800242 BI_LIST_FOR_EACH(node, head) {
Ken Liuc25558c2021-05-20 15:31:28 +0800243 tmp_msg = TO_CONTAINER(node, struct tfm_msg_body_t, msg_node);
Ken Liuacd2a572021-05-12 16:19:04 +0800244 if (tmp_msg->service->p_ldinf->signal == signal && msg) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800245 return msg;
Ken Liuacd2a572021-05-12 16:19:04 +0800246 } else if (tmp_msg->service->p_ldinf->signal == signal) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800247 msg = tmp_msg;
Ken Liu2c47f7f2021-01-22 11:06:04 +0800248 BI_LIST_REMOVE_NODE(node);
Edison Ai764d41f2018-09-21 15:56:36 +0800249 }
250 }
Mingyang Sun73056b62020-07-03 15:18:46 +0800251
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800252 partition->signals_asserted &= ~signal;
Mingyang Sun73056b62020-07-03 15:18:46 +0800253
254 return msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800255}
256
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800257uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
258{
Kevin Pengbf9a97e2021-06-18 16:34:12 +0800259#if TFM_LVL == 1
260 return TFM_PARTITION_PRIVILEGED_MODE;
261#else /* TFM_LVL == 1 */
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800262 if (partition_flags & SPM_PART_FLAG_PSA_ROT) {
263 return TFM_PARTITION_PRIVILEGED_MODE;
264 } else {
265 return TFM_PARTITION_UNPRIVILEGED_MODE;
266 }
Kevin Pengbf9a97e2021-06-18 16:34:12 +0800267#endif /* TFM_LVL == 1 */
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800268}
269
Mingyang Sun783a59b2021-04-20 15:52:18 +0800270struct service_t *tfm_spm_get_service_by_sid(uint32_t sid)
Edison Ai764d41f2018-09-21 15:56:36 +0800271{
Feder.Liangaeb5a792021-08-10 16:12:56 +0800272 struct service_t *p_prev, *p_curr;
Edison Ai764d41f2018-09-21 15:56:36 +0800273
Feder.Liangaeb5a792021-08-10 16:12:56 +0800274 UNI_LIST_FOR_EACH_PREV(p_prev, p_curr, &services_listhead) {
275 if (p_curr->p_ldinf->sid == sid) {
276 UNI_LIST_MOVE_AFTER(&services_listhead, p_prev, p_curr);
277 return p_curr;
Mingyang Sunef42f442021-06-11 15:07:58 +0800278 }
279 }
280
Ken Liuea45b0d2021-05-22 17:41:25 +0800281 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800282}
283
Mingyang Sund44522a2020-01-16 16:48:37 +0800284/**
285 * \brief Get the partition context by partition ID.
286 *
287 * \param[in] partition_id Partition identity
288 *
289 * \retval NULL Failed
290 * \retval "Not NULL" Target partition context pointer,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800291 * \ref partition_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800292 */
Kevin Pengeca45b92021-02-09 14:46:50 +0800293struct partition_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800294{
Ken Liuea45b0d2021-05-22 17:41:25 +0800295 struct partition_t *p_part;
Edison Ai764d41f2018-09-21 15:56:36 +0800296
Ken Liuea45b0d2021-05-22 17:41:25 +0800297 UNI_LIST_FOR_EACH(p_part, &partitions_listhead) {
298 if (p_part->p_ldinf->pid == partition_id) {
299 return p_part;
300 }
Edison Ai764d41f2018-09-21 15:56:36 +0800301 }
Ken Liuea45b0d2021-05-22 17:41:25 +0800302
Edison Ai764d41f2018-09-21 15:56:36 +0800303 return NULL;
304}
305
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800306struct partition_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800307{
Mingyang Sun4f08f4d2021-09-10 17:41:28 +0800308 return GET_THRD_OWNER(CURRENT_THREAD);
Edison Ai764d41f2018-09-21 15:56:36 +0800309}
310
Mingyang Sun783a59b2021-04-20 15:52:18 +0800311int32_t tfm_spm_check_client_version(struct service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530312 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800313{
Ken Liuf250b8b2019-12-27 16:31:24 +0800314 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800315
Ken Liuacd2a572021-05-12 16:19:04 +0800316 switch (SERVICE_GET_VERSION_POLICY(service->p_ldinf->flags)) {
Ken Liub3b2cb62021-05-22 00:39:28 +0800317 case SERVICE_VERSION_POLICY_RELAXED:
Ken Liuacd2a572021-05-12 16:19:04 +0800318 if (version > service->p_ldinf->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800319 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800320 }
321 break;
Ken Liub3b2cb62021-05-22 00:39:28 +0800322 case SERVICE_VERSION_POLICY_STRICT:
Ken Liuacd2a572021-05-12 16:19:04 +0800323 if (version != service->p_ldinf->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800324 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800325 }
326 break;
327 default:
Ken Liubcae38b2021-01-20 15:47:44 +0800328 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800329 }
Ken Liubcae38b2021-01-20 15:47:44 +0800330 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800331}
332
Edison Aie728fbf2019-11-13 09:37:12 +0800333int32_t tfm_spm_check_authorization(uint32_t sid,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800334 struct service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800335 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800336{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800337 struct partition_t *partition = NULL;
Mingyang Sun2b352662021-04-21 11:35:43 +0800338 uint32_t *dep;
Edison Aie728fbf2019-11-13 09:37:12 +0800339 int32_t i;
340
Ken Liuf250b8b2019-12-27 16:31:24 +0800341 TFM_CORE_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800342
343 if (ns_caller) {
Ken Liuacd2a572021-05-12 16:19:04 +0800344 if (!SERVICE_IS_NS_ACCESSIBLE(service->p_ldinf->flags)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800345 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800346 }
347 } else {
348 partition = tfm_spm_get_running_partition();
349 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800350 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800351 }
352
Ken Liuacd2a572021-05-12 16:19:04 +0800353 dep = (uint32_t *)LOAD_INFO_DEPS(partition->p_ldinf);
354 for (i = 0; i < partition->p_ldinf->ndeps; i++) {
Mingyang Sun2b352662021-04-21 11:35:43 +0800355 if (dep[i] == sid) {
Edison Aie728fbf2019-11-13 09:37:12 +0800356 break;
357 }
358 }
359
Ken Liuacd2a572021-05-12 16:19:04 +0800360 if (i == partition->p_ldinf->ndeps) {
Ken Liubcae38b2021-01-20 15:47:44 +0800361 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800362 }
363 }
Ken Liubcae38b2021-01-20 15:47:44 +0800364 return SPM_SUCCESS;
Edison Aie728fbf2019-11-13 09:37:12 +0800365}
366
Edison Ai764d41f2018-09-21 15:56:36 +0800367/* Message functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800368
Summer Qin02f7f072020-08-24 16:02:54 +0800369struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800370{
371 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200372 * The message handler passed by the caller is considered invalid in the
373 * following cases:
374 * 1. Not a valid message handle. (The address of a message is not the
375 * address of a possible handle from the pool
376 * 2. Handle not belongs to the caller partition (The handle is either
377 * unused, or owned by anither partition)
378 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800379 */
Ken Liu505b1702020-05-29 13:19:58 +0800380 struct tfm_msg_body_t *p_msg;
Kevin Pengeec41a82021-08-18 13:56:23 +0800381 int32_t partition_id;
Ken Liu505b1702020-05-29 13:19:58 +0800382 struct tfm_conn_handle_t *p_conn_handle =
Ken Liu5d73c872021-08-19 19:23:17 +0800383 tfm_spm_to_handle_instance(msg_handle);
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200384
385 if (is_valid_chunk_data_in_pool(
Ken Liu505b1702020-05-29 13:19:58 +0800386 conn_handle_pool, (uint8_t *)p_conn_handle) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800387 return NULL;
388 }
389
Ken Liu505b1702020-05-29 13:19:58 +0800390 p_msg = &p_conn_handle->internal_msg;
391
Edison Ai764d41f2018-09-21 15:56:36 +0800392 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200393 * Check that the magic number is correct. This proves that the message
394 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800395 */
Ken Liu505b1702020-05-29 13:19:58 +0800396 if (p_msg->magic != TFM_MSG_MAGIC) {
Edison Ai764d41f2018-09-21 15:56:36 +0800397 return NULL;
398 }
399
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200400 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800401 partition_id = tfm_spm_partition_get_running_partition_id();
Ken Liuacd2a572021-05-12 16:19:04 +0800402 if (partition_id != p_msg->service->partition->p_ldinf->pid) {
Edison Ai764d41f2018-09-21 15:56:36 +0800403 return NULL;
404 }
405
Ken Liu505b1702020-05-29 13:19:58 +0800406 return p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800407}
408
Kevin Pengdf6aa292021-03-11 17:58:50 +0800409struct tfm_msg_body_t *
410 tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle)
411{
412 TFM_CORE_ASSERT(conn_handle != NULL);
413
414 return &(conn_handle->internal_msg);
415}
416
Edison Ai97115822019-08-01 14:22:19 +0800417void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800418 struct service_t *service,
Ken Liu505b1702020-05-29 13:19:58 +0800419 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800420 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800421 psa_invec *invec, size_t in_len,
422 psa_outvec *outvec, size_t out_len,
423 psa_outvec *caller_outvec)
424{
Edison Ai764d41f2018-09-21 15:56:36 +0800425 uint32_t i;
Ken Liu505b1702020-05-29 13:19:58 +0800426 struct tfm_conn_handle_t *conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800427
Ken Liuf250b8b2019-12-27 16:31:24 +0800428 TFM_CORE_ASSERT(msg);
429 TFM_CORE_ASSERT(service);
430 TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
431 TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
432 TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
433 TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
434 TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800435
Edison Ai764d41f2018-09-21 15:56:36 +0800436 /* Clear message buffer before using it */
Summer Qinf24dbb52020-07-23 14:53:54 +0800437 spm_memset(msg, 0, sizeof(struct tfm_msg_body_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800438
Ken Liu5d73c872021-08-19 19:23:17 +0800439 THRD_SYNC_INIT(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800440 msg->magic = TFM_MSG_MAGIC;
441 msg->service = service;
Edison Ai764d41f2018-09-21 15:56:36 +0800442 msg->caller_outvec = caller_outvec;
Summer Qin1ce712a2019-10-14 18:04:05 +0800443 msg->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800444
445 /* Copy contents */
446 msg->msg.type = type;
447
448 for (i = 0; i < in_len; i++) {
449 msg->msg.in_size[i] = invec[i].len;
450 msg->invec[i].base = invec[i].base;
451 }
452
453 for (i = 0; i < out_len; i++) {
454 msg->msg.out_size[i] = outvec[i].len;
455 msg->outvec[i].base = outvec[i].base;
456 /* Out len is used to record the writed number, set 0 here again */
457 msg->outvec[i].len = 0;
458 }
459
Ken Liu505b1702020-05-29 13:19:58 +0800460 /* Use the user connect handle as the message handle */
461 msg->msg.handle = handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800462
Ken Liu505b1702020-05-29 13:19:58 +0800463 conn_handle = tfm_spm_to_handle_instance(handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800464 /* For connected handle, set rhandle to every message */
Ken Liu505b1702020-05-29 13:19:58 +0800465 if (conn_handle) {
466 msg->msg.rhandle = tfm_spm_get_rhandle(service, conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800467 }
David Hu46603dd2019-12-11 18:05:16 +0800468
469 /* Set the private data of NSPE client caller in multi-core topology */
470 if (TFM_CLIENT_ID_IS_NS(client_id)) {
471 tfm_rpc_set_caller_data(msg, client_id);
472 }
Edison Ai764d41f2018-09-21 15:56:36 +0800473}
474
Mingyang Sun783a59b2021-04-20 15:52:18 +0800475void tfm_spm_send_event(struct service_t *service,
Kevin Peng8dac6102021-03-09 16:44:00 +0800476 struct tfm_msg_body_t *msg)
Edison Ai764d41f2018-09-21 15:56:36 +0800477{
Kevin Peng8dac6102021-03-09 16:44:00 +0800478 struct partition_t *partition = NULL;
479 psa_signal_t signal = 0;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800480
Ken Liuacd2a572021-05-12 16:19:04 +0800481 if (!msg || !service || !service->p_ldinf || !service->partition) {
Kevin Peng8dac6102021-03-09 16:44:00 +0800482 tfm_core_panic();
483 }
484
485 partition = service->partition;
Ken Liuacd2a572021-05-12 16:19:04 +0800486 signal = service->p_ldinf->signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800487
Mingyang Sun73056b62020-07-03 15:18:46 +0800488 /* Add message to partition message list tail */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800489 BI_LIST_INSERT_BEFORE(&partition->msg_list, &msg->msg_node);
Edison Ai764d41f2018-09-21 15:56:36 +0800490
491 /* Messages put. Update signals */
Kevin Peng8dac6102021-03-09 16:44:00 +0800492 partition->signals_asserted |= signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800493
Kevin Peng8dac6102021-03-09 16:44:00 +0800494 if (partition->signals_waiting & signal) {
Ken Liu5d73c872021-08-19 19:23:17 +0800495 thrd_wake_up(
496 &partition->waitobj,
Kevin Peng8dac6102021-03-09 16:44:00 +0800497 (partition->signals_asserted & partition->signals_waiting));
498 partition->signals_waiting &= ~signal;
499 }
Edison Ai764d41f2018-09-21 15:56:36 +0800500
David Hufb38d562019-09-23 15:58:34 +0800501 /*
502 * If it is a NS request via RPC, it is unnecessary to block current
503 * thread.
504 */
505 if (!is_tfm_rpc_msg(msg)) {
Mingyang Sun4f08f4d2021-09-10 17:41:28 +0800506 thrd_wait_on(&msg->ack_evnt, CURRENT_THREAD);
David Hufb38d562019-09-23 15:58:34 +0800507 }
Edison Ai764d41f2018-09-21 15:56:36 +0800508}
509
Kevin Pengeec41a82021-08-18 13:56:23 +0800510int32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800511{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800512 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800513
Kevin Peng79c2bda2020-07-24 16:31:12 +0800514 partition = tfm_spm_get_running_partition();
Ken Liuacd2a572021-05-12 16:19:04 +0800515 if (partition && partition->p_ldinf) {
516 return partition->p_ldinf->pid;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800517 } else {
518 return INVALID_PARTITION_ID;
519 }
Edison Ai764d41f2018-09-21 15:56:36 +0800520}
521
Summer Qin43c185d2019-10-10 15:44:42 +0800522int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800523 enum tfm_memory_access_e access,
524 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800525{
Mingyang Sund1ed6732020-08-26 15:52:21 +0800526 enum tfm_hal_status_t err;
527 uint32_t attr = 0;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800528
529 /* If len is zero, this indicates an empty buffer and base is ignored */
530 if (len == 0) {
Ken Liubcae38b2021-01-20 15:47:44 +0800531 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800532 }
533
534 if (!buffer) {
Ken Liubcae38b2021-01-20 15:47:44 +0800535 return SPM_ERROR_BAD_PARAMETERS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800536 }
537
538 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800539 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800540 }
541
Summer Qin424d4db2019-03-25 14:09:51 +0800542 if (access == TFM_MEMORY_ACCESS_RW) {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800543 attr |= (TFM_HAL_ACCESS_READABLE | TFM_HAL_ACCESS_WRITABLE);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800544 } else {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800545 attr |= TFM_HAL_ACCESS_READABLE;
Summer Qin424d4db2019-03-25 14:09:51 +0800546 }
Mingyang Sund1ed6732020-08-26 15:52:21 +0800547
548 if (privileged == TFM_PARTITION_UNPRIVILEGED_MODE) {
549 attr |= TFM_HAL_ACCESS_UNPRIVILEGED;
550 } else {
551 attr &= ~TFM_HAL_ACCESS_UNPRIVILEGED;
552 }
553
554 if (ns_caller) {
555 attr |= TFM_HAL_ACCESS_NS;
556 }
557
558 err = tfm_hal_memory_has_access((uintptr_t)buffer, len, attr);
559
560 if (err == TFM_HAL_SUCCESS) {
Ken Liubcae38b2021-01-20 15:47:44 +0800561 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800562 }
563
Ken Liubcae38b2021-01-20 15:47:44 +0800564 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800565}
566
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800567bool tfm_spm_is_ns_caller(void)
568{
569#if defined(TFM_MULTI_CORE_TOPOLOGY) || defined(FORWARD_PROT_MSG)
570 /* Multi-core NS PSA API request is processed by pendSV. */
571 return (__get_active_exc_num() == EXC_NUM_PENDSV);
572#else
573 struct partition_t *partition = tfm_spm_get_running_partition();
Mingyang Sune529e3b2021-07-12 14:46:30 +0800574
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800575 if (!partition) {
576 tfm_core_panic();
577 }
578
579 return (partition->p_ldinf->pid == TFM_SP_NON_SECURE_ID);
580#endif
581}
582
Mingyang Sune529e3b2021-07-12 14:46:30 +0800583uint32_t tfm_spm_get_caller_privilege_mode(void)
584{
585 struct partition_t *partition;
586
587#if defined(TFM_MULTI_CORE_TOPOLOGY) || defined(FORWARD_PROT_MSG)
588 /*
589 * In multi-core topology, if PSA request is from mailbox, the client
590 * is unprivileged.
591 */
592 if (__get_active_exc_num() == EXC_NUM_PENDSV) {
593 return TFM_PARTITION_UNPRIVILEGED_MODE;
594 }
595#endif
596 partition = tfm_spm_get_running_partition();
597 if (!partition) {
598 tfm_core_panic();
599 }
600
601 return tfm_spm_partition_get_privileged_mode(partition->p_ldinf->flags);
602}
603
Kevin Peng385fda82021-08-18 10:41:19 +0800604int32_t tfm_spm_get_client_id(bool ns_caller)
605{
606 int32_t client_id;
607
608 if (ns_caller) {
609 client_id = tfm_nspm_get_current_client_id();
610 } else {
611 client_id = tfm_spm_partition_get_running_partition_id();
612 }
613
614 if (ns_caller != (client_id < 0)) {
615 /* NS client ID must be negative and Secure ID must >= 0 */
616 tfm_core_panic();
617 }
618
619 return client_id;
620}
621
Ken Liuce2692d2020-02-11 12:39:36 +0800622uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800623{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800624 struct partition_t *partition;
Mingyang Sun8d004f72021-06-01 10:46:26 +0800625 const struct partition_load_info_t *p_ldinf;
Ken Liu5d73c872021-08-19 19:23:17 +0800626 void *p_param, *p_boundaries = NULL;
Ken Liuce58bfc2021-05-12 17:54:48 +0800627
David Huf07e97d2021-02-15 22:05:40 +0800628#ifdef TFM_FIH_PROFILE_ON
629 fih_int fih_rc = FIH_FAILURE;
630#endif
Edison Ai764d41f2018-09-21 15:56:36 +0800631
632 tfm_pool_init(conn_handle_pool,
633 POOL_BUFFER_SIZE(conn_handle_pool),
634 sizeof(struct tfm_conn_handle_t),
635 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800636
Ken Liuea45b0d2021-05-22 17:41:25 +0800637 UNI_LISI_INIT_HEAD(&partitions_listhead);
638 UNI_LISI_INIT_HEAD(&services_listhead);
639
Ken Liuacd2a572021-05-12 16:19:04 +0800640 while (1) {
Ken Liuea45b0d2021-05-22 17:41:25 +0800641 partition = load_a_partition_assuredly(&partitions_listhead);
Shawn Shan23331062021-09-01 14:58:21 +0800642 if (partition == NO_MORE_PARTITION) {
Ken Liuacd2a572021-05-12 16:19:04 +0800643 break;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800644 }
645
Mingyang Sun8d004f72021-06-01 10:46:26 +0800646 p_ldinf = partition->p_ldinf;
Mingyang Sun2b352662021-04-21 11:35:43 +0800647
Kevin Peng27e42272021-05-24 17:58:53 +0800648 if (p_ldinf->nservices) {
Ken Liuea45b0d2021-05-22 17:41:25 +0800649 load_services_assuredly(partition, &services_listhead,
Kevin Peng27e42272021-05-24 17:58:53 +0800650 stateless_services_ref_tbl,
651 sizeof(stateless_services_ref_tbl));
652 }
653
654 if (p_ldinf->nirqs) {
655 load_irqs_assuredly(partition);
656 }
657
Ken Liuce58bfc2021-05-12 17:54:48 +0800658 /* Bind the partition with plaform. */
659#if TFM_FIH_PROFILE_ON
660 FIH_CALL(tfm_hal_bind_boundaries, fih_rc, partition->p_ldinf,
661 &p_boundaries);
662 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
663 tfm_core_panic();
Mingyang Sun61f8fbc2021-06-04 17:49:56 +0800664 }
Ken Liu86686282021-04-27 11:11:15 +0800665#else /* TFM_FIH_PROFILE_ON */
Ken Liuce58bfc2021-05-12 17:54:48 +0800666 if (tfm_hal_bind_boundaries(partition->p_ldinf,
667 &p_boundaries) != TFM_HAL_SUCCESS) {
668 tfm_core_panic();
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100669 }
Ken Liuce58bfc2021-05-12 17:54:48 +0800670#endif /* TFM_FIH_PROFILE_ON */
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100671
Ken Liuce58bfc2021-05-12 17:54:48 +0800672 partition->p_boundaries = p_boundaries;
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800673 partition->signals_allowed |= PSA_DOORBELL;
Shawn Shanc7dda0e2019-12-23 14:45:09 +0800674
Ken Liu5d73c872021-08-19 19:23:17 +0800675 THRD_SYNC_INIT(&partition->waitobj);
Ken Liu2c47f7f2021-01-22 11:06:04 +0800676 BI_LIST_INIT_NODE(&partition->msg_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800677
Ken Liu5d73c872021-08-19 19:23:17 +0800678 THRD_INIT(&partition->thrd, &partition->ctx_ctrl,
679 TO_THREAD_PRIORITY(PARTITION_PRIORITY(p_ldinf->flags)));
Edison Ai764d41f2018-09-21 15:56:36 +0800680
Ken Liu5d73c872021-08-19 19:23:17 +0800681 p_param = NULL;
Mingyang Sun8d004f72021-06-01 10:46:26 +0800682 if (p_ldinf->pid == TFM_SP_NON_SECURE_ID) {
Ken Liu5d73c872021-08-19 19:23:17 +0800683 p_param = (void *)tfm_spm_hal_get_ns_entry_point();
Ken Liu490281d2019-12-30 15:55:26 +0800684 }
685
Ken Liu5d73c872021-08-19 19:23:17 +0800686 thrd_start(&partition->thrd,
687 POSITION_TO_ENTRY(p_ldinf->entry, thrd_fn_t), p_param,
688 LOAD_ALLOCED_STACK_ADDR(p_ldinf),
689 LOAD_ALLOCED_STACK_ADDR(p_ldinf) + p_ldinf->stack_size);
Edison Ai764d41f2018-09-21 15:56:36 +0800690 }
691
Mingyang Sun4f08f4d2021-09-10 17:41:28 +0800692 return thrd_start_scheduler(&CURRENT_THREAD);
Edison Ai764d41f2018-09-21 15:56:36 +0800693}
Ken Liu2d175172019-03-21 17:08:41 +0800694
Ken Liu5d73c872021-08-19 19:23:17 +0800695/*
696 * Return both current and next context to assembly via AAPCS trick:
697 * - Returning a 64 bit integer by 32-bit R0 and R1.
698 *
699 * This is architecture-specific, hence the scheduler entry and this
700 * 'do_schedule' MAY be different on another architecture.
701 */
702union returning_contexts_t {
703 struct {
704 uint32_t curr;
705 uint32_t next;
706 } ctx;
Ken Liu2d175172019-03-21 17:08:41 +0800707
Ken Liu5d73c872021-08-19 19:23:17 +0800708 uint64_t curr_next_ctxs;
709};
710
711uint64_t do_schedule(void)
712{
713 union returning_contexts_t ret;
714 struct partition_t *p_part_curr, *p_part_next;
715 struct thread_t *pth_next = thrd_next();
716
Mingyang Sun4f08f4d2021-09-10 17:41:28 +0800717 p_part_curr = GET_THRD_OWNER(CURRENT_THREAD);
718 p_part_next = GET_THRD_OWNER(pth_next);
Ken Liu5d73c872021-08-19 19:23:17 +0800719
720 if (pth_next != NULL && p_part_curr != p_part_next) {
721 /* Check if there is enough room on stack to save more context */
722 if ((p_part_curr->ctx_ctrl.sp_limit +
723 sizeof(struct tfm_additional_context_t)) > __get_PSP()) {
724 tfm_core_panic();
725 }
Mate Toth-Palc430b992019-05-09 21:01:14 +0200726
Ken Liuce58bfc2021-05-12 17:54:48 +0800727 /*
728 * If required, let the platform update boundary based on its
729 * implementation. Change privilege, MPU or other configurations.
730 */
731 if (p_part_curr->p_boundaries != p_part_next->p_boundaries) {
732 if (tfm_hal_update_boundaries(p_part_next->p_ldinf,
733 p_part_next->p_boundaries)
734 != TFM_HAL_SUCCESS) {
735 tfm_core_panic();
736 }
737 }
Ken Liu2d175172019-03-21 17:08:41 +0800738 }
David Hufb38d562019-09-23 15:58:34 +0800739
740 /*
741 * Handle pending mailbox message from NS in multi-core topology.
742 * Empty operation on single Armv8-M platform.
743 */
744 tfm_rpc_client_call_handler();
Ken Liu5d73c872021-08-19 19:23:17 +0800745
Mingyang Sun4f08f4d2021-09-10 17:41:28 +0800746 ret.ctx.curr = (uint32_t)CURRENT_THREAD->p_context_ctrl;
Ken Liu5d73c872021-08-19 19:23:17 +0800747 ret.ctx.next = (uint32_t)pth_next->p_context_ctrl;
748
Mingyang Sun4f08f4d2021-09-10 17:41:28 +0800749 CURRENT_THREAD = pth_next;
Ken Liu5d73c872021-08-19 19:23:17 +0800750
751 return ret.curr_next_ctxs;
Ken Liu2d175172019-03-21 17:08:41 +0800752}
Mingyang Sund44522a2020-01-16 16:48:37 +0800753
Summer Qin02f7f072020-08-24 16:02:54 +0800754void update_caller_outvec_len(struct tfm_msg_body_t *msg)
Mingyang Sund44522a2020-01-16 16:48:37 +0800755{
756 uint32_t i;
757
758 /*
759 * FixeMe: abstract these part into dedicated functions to avoid
760 * accessing thread context in psa layer
761 */
762 /* If it is a NS request via RPC, the owner of this message is not set */
763 if (!is_tfm_rpc_msg(msg)) {
764 TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK);
765 }
766
767 for (i = 0; i < PSA_MAX_IOVEC; i++) {
768 if (msg->msg.out_size[i] == 0) {
769 continue;
770 }
771
772 TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base);
773
774 msg->caller_outvec[i].len = msg->outvec[i].len;
775 }
776}
777
Ken Liu5d73c872021-08-19 19:23:17 +0800778void spm_assert_signal(void *p_pt, psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800779{
Ken Liu5d73c872021-08-19 19:23:17 +0800780 struct partition_t *partition = (struct partition_t *)p_pt;
Mingyang Sund44522a2020-01-16 16:48:37 +0800781
Mingyang Sund44522a2020-01-16 16:48:37 +0800782 if (!partition) {
783 tfm_core_panic();
784 }
785
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800786 partition->signals_asserted |= signal;
Mingyang Sund44522a2020-01-16 16:48:37 +0800787
Kevin Peng8dac6102021-03-09 16:44:00 +0800788 if (partition->signals_waiting & signal) {
Ken Liu5d73c872021-08-19 19:23:17 +0800789 thrd_wake_up(&partition->waitobj,
790 partition->signals_asserted & partition->signals_waiting);
Kevin Peng8dac6102021-03-09 16:44:00 +0800791 partition->signals_waiting &= ~signal;
792 }
Mingyang Sund44522a2020-01-16 16:48:37 +0800793}
794
Kevin Pengeca45b92021-02-09 14:46:50 +0800795__attribute__((naked))
Ken Liu5d73c872021-08-19 19:23:17 +0800796static psa_flih_result_t tfm_flih_deprivileged_handling(void *p_pt,
Kevin Pengec239bb2021-09-03 15:40:27 +0800797 uintptr_t fn_flih,
Ken Liu5d73c872021-08-19 19:23:17 +0800798 void *p_context_ctrl)
Mingyang Sund44522a2020-01-16 16:48:37 +0800799{
Kevin Pengeca45b92021-02-09 14:46:50 +0800800 __ASM volatile("SVC %0 \n"
801 "BX LR \n"
802 : : "I" (TFM_SVC_PREPARE_DEPRIV_FLIH));
803}
Kevin Pengdc791882021-03-12 10:57:12 +0800804
Kevin Pengec239bb2021-09-03 15:40:27 +0800805void spm_handle_interrupt(void *p_pt, struct irq_load_info_t *p_ildi)
Kevin Pengeca45b92021-02-09 14:46:50 +0800806{
Kevin Pengeca45b92021-02-09 14:46:50 +0800807 psa_flih_result_t flih_result;
Kevin Pengec239bb2021-09-03 15:40:27 +0800808 struct partition_t *p_part;
Kevin Pengdc791882021-03-12 10:57:12 +0800809
Kevin Pengec239bb2021-09-03 15:40:27 +0800810 if (!p_pt || !p_ildi) {
Ken Liu5d73c872021-08-19 19:23:17 +0800811 tfm_core_panic();
812 }
Kevin Pengeca45b92021-02-09 14:46:50 +0800813
Kevin Pengec239bb2021-09-03 15:40:27 +0800814 p_part = (struct partition_t *)p_pt;
815
816 if (p_ildi->pid != p_part->p_ldinf->pid) {
817 tfm_core_panic();
818 }
819
820 if (p_ildi->flih_func == NULL) {
Kevin Pengeca45b92021-02-09 14:46:50 +0800821 /* SLIH Model Handling */
Kevin Pengec239bb2021-09-03 15:40:27 +0800822 tfm_hal_irq_disable(p_ildi->source);
Ken Liu5d73c872021-08-19 19:23:17 +0800823 flih_result = PSA_FLIH_SIGNAL;
824 } else {
825 /* FLIH Model Handling */
Kevin Pengec239bb2021-09-03 15:40:27 +0800826 if (tfm_spm_partition_get_privileged_mode(p_part->p_ldinf->flags) ==
Ken Liu5d73c872021-08-19 19:23:17 +0800827 TFM_PARTITION_PRIVILEGED_MODE) {
Kevin Pengec239bb2021-09-03 15:40:27 +0800828 flih_result = p_ildi->flih_func();
Ken Liu5d73c872021-08-19 19:23:17 +0800829 } else {
830 flih_result = tfm_flih_deprivileged_handling(
Mingyang Sun4f08f4d2021-09-10 17:41:28 +0800831 p_part,
832 (uintptr_t)p_ildi->flih_func,
833 CURRENT_THREAD->p_context_ctrl);
Ken Liu5d73c872021-08-19 19:23:17 +0800834 }
Kevin Pengeca45b92021-02-09 14:46:50 +0800835 }
836
Ken Liu5d73c872021-08-19 19:23:17 +0800837 if (flih_result == PSA_FLIH_SIGNAL) {
838 __disable_irq();
Kevin Pengec239bb2021-09-03 15:40:27 +0800839 spm_assert_signal(p_pt, p_ildi->signal);
Ken Liu5d73c872021-08-19 19:23:17 +0800840 __enable_irq();
Kevin Pengeca45b92021-02-09 14:46:50 +0800841 }
Mingyang Sund44522a2020-01-16 16:48:37 +0800842}
843
Kevin Peng27e42272021-05-24 17:58:53 +0800844struct irq_load_info_t *get_irq_info_for_signal(
845 const struct partition_load_info_t *p_ldinf,
846 psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800847{
848 size_t i;
Kevin Peng27e42272021-05-24 17:58:53 +0800849 struct irq_load_info_t *irq_info;
Mingyang Sund44522a2020-01-16 16:48:37 +0800850
Ken Liu24dffb22021-02-10 11:03:58 +0800851 if (!IS_ONLY_ONE_BIT_IN_UINT32(signal)) {
Kevin Peng27e42272021-05-24 17:58:53 +0800852 return NULL;
Kevin Peng410bee52021-01-13 16:27:17 +0800853 }
854
Kevin Peng27e42272021-05-24 17:58:53 +0800855 irq_info = (struct irq_load_info_t *)LOAD_INFO_IRQ(p_ldinf);
856 for (i = 0; i < p_ldinf->nirqs; i++) {
857 if (irq_info[i].signal == signal) {
858 return &irq_info[i];
Mingyang Sund44522a2020-01-16 16:48:37 +0800859 }
860 }
Kevin Penga20b5af2021-01-11 11:20:52 +0800861
Kevin Peng27e42272021-05-24 17:58:53 +0800862 return NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800863}