blob: 7db60ba508a83be319d827f9655795f0723cd138 [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
Summer Qin373feb12020-03-27 15:35:33 +080050/*********************** Connection handle conversion APIs *******************/
51
Summer Qin373feb12020-03-27 15:35:33 +080052#define CONVERSION_FACTOR_BITOFFSET 3
53#define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET)
54/* Set 32 as the maximum */
55#define CONVERSION_FACTOR_VALUE_MAX 0x20
56
57#if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX
58#error "CONVERSION FACTOR OUT OF RANGE"
59#endif
60
61static uint32_t loop_index;
62
63/*
64 * A handle instance psa_handle_t allocated inside SPM is actually a memory
65 * address among the handle pool. Return this handle to the client directly
66 * exposes information of secure memory address. In this case, converting the
67 * handle into another value does not represent the memory address to avoid
68 * exposing secure memory directly to clients.
69 *
70 * This function converts the handle instance into another value by scaling the
71 * handle in pool offset, the converted value is named as a user handle.
72 *
73 * The formula:
74 * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE +
75 * CLIENT_HANDLE_VALUE_MIN + loop_index
76 * where:
77 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
78 * exceed CONVERSION_FACTOR_VALUE_MAX.
79 *
80 * handle_instance in RANGE[POOL_START, POOL_END]
81 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
82 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
83 *
84 * note:
85 * loop_index is used to promise same handle instance is converted into
86 * different user handles in short time.
87 */
Ken Liu505b1702020-05-29 13:19:58 +080088psa_handle_t tfm_spm_to_user_handle(struct tfm_conn_handle_t *handle_instance)
Summer Qin373feb12020-03-27 15:35:33 +080089{
90 psa_handle_t user_handle;
91
92 loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE;
93 user_handle = (psa_handle_t)((((uintptr_t)handle_instance -
94 (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) +
95 CLIENT_HANDLE_VALUE_MIN + loop_index);
96
97 return user_handle;
98}
99
100/*
101 * This function converts a user handle into a corresponded handle instance.
102 * The converted value is validated before returning, an invalid handle instance
103 * is returned as NULL.
104 *
105 * The formula:
106 * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) /
107 * CONVERSION_FACTOR_VALUE) + POOL_START
108 * where:
109 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
110 * exceed CONVERSION_FACTOR_VALUE_MAX.
111 *
112 * handle_instance in RANGE[POOL_START, POOL_END]
113 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
114 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
115 */
116struct tfm_conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle)
117{
118 struct tfm_conn_handle_t *handle_instance;
119
120 if (user_handle == PSA_NULL_HANDLE) {
121 return NULL;
122 }
123
124 handle_instance = (struct tfm_conn_handle_t *)((((uintptr_t)user_handle -
125 CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) +
126 (uintptr_t)conn_handle_pool);
127
128 return handle_instance;
129}
130
Edison Ai764d41f2018-09-21 15:56:36 +0800131/* Service handle management functions */
Mingyang Sun783a59b2021-04-20 15:52:18 +0800132struct tfm_conn_handle_t *tfm_spm_create_conn_handle(struct service_t *service,
133 int32_t client_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800134{
Edison Ai9cc26242019-08-06 11:28:04 +0800135 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800136
Ken Liuf250b8b2019-12-27 16:31:24 +0800137 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800138
139 /* Get buffer for handle list structure from handle pool */
Edison Ai9cc26242019-08-06 11:28:04 +0800140 p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
141 if (!p_handle) {
Summer Qin630c76b2020-05-20 10:32:58 +0800142 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800143 }
144
Ken Liuf39d8eb2021-10-07 12:55:33 +0800145 p_handle->internal_msg.service = service;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800146 p_handle->status = TFM_HANDLE_STATUS_IDLE;
Summer Qin1ce712a2019-10-14 18:04:05 +0800147 p_handle->client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800148
149 /* Add handle node to list for next psa functions */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800150 BI_LIST_INSERT_BEFORE(&service->handle_list, &p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800151
Summer Qin630c76b2020-05-20 10:32:58 +0800152 return p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800153}
154
Summer Qin630c76b2020-05-20 10:32:58 +0800155int32_t tfm_spm_validate_conn_handle(
156 const struct tfm_conn_handle_t *conn_handle,
157 int32_t client_id)
Summer Qin1ce712a2019-10-14 18:04:05 +0800158{
159 /* Check the handle address is validated */
160 if (is_valid_chunk_data_in_pool(conn_handle_pool,
161 (uint8_t *)conn_handle) != true) {
Ken Liubcae38b2021-01-20 15:47:44 +0800162 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800163 }
164
165 /* Check the handle caller is correct */
Summer Qin630c76b2020-05-20 10:32:58 +0800166 if (conn_handle->client_id != client_id) {
Ken Liubcae38b2021-01-20 15:47:44 +0800167 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800168 }
169
Ken Liubcae38b2021-01-20 15:47:44 +0800170 return SPM_SUCCESS;
Summer Qin1ce712a2019-10-14 18:04:05 +0800171}
172
Mingyang Sun783a59b2021-04-20 15:52:18 +0800173int32_t tfm_spm_free_conn_handle(struct service_t *service,
Summer Qin02f7f072020-08-24 16:02:54 +0800174 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800175{
Ken Liuf250b8b2019-12-27 16:31:24 +0800176 TFM_CORE_ASSERT(service);
Summer Qin630c76b2020-05-20 10:32:58 +0800177 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800178
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200179 /* Clear magic as the handler is not used anymore */
Summer Qin630c76b2020-05-20 10:32:58 +0800180 conn_handle->internal_msg.magic = 0;
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200181
Edison Ai764d41f2018-09-21 15:56:36 +0800182 /* Remove node from handle list */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800183 BI_LIST_REMOVE_NODE(&conn_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800184
185 /* Back handle buffer to pool */
Ken Liu66ca6132021-02-24 08:49:51 +0800186 tfm_pool_free(conn_handle_pool, conn_handle);
Ken Liubcae38b2021-01-20 15:47:44 +0800187 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800188}
189
Mingyang Sun783a59b2021-04-20 15:52:18 +0800190int32_t tfm_spm_set_rhandle(struct service_t *service,
Summer Qin02f7f072020-08-24 16:02:54 +0800191 struct tfm_conn_handle_t *conn_handle,
192 void *rhandle)
Edison Ai764d41f2018-09-21 15:56:36 +0800193{
Ken Liuf250b8b2019-12-27 16:31:24 +0800194 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800195 /* Set reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800196 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800197
Summer Qin630c76b2020-05-20 10:32:58 +0800198 conn_handle->rhandle = rhandle;
Ken Liubcae38b2021-01-20 15:47:44 +0800199 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800200}
201
Mingyang Sund44522a2020-01-16 16:48:37 +0800202/**
Xinyu Zhang3a453242021-04-16 17:57:09 +0800203 * \brief Get reverse handle value from connection handle.
Mingyang Sund44522a2020-01-16 16:48:37 +0800204 *
205 * \param[in] service Target service context pointer
206 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800207 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800208 *
209 * \retval void * Success
210 * \retval "Does not return" Panic for those:
211 * service pointer are NULL
Xinyu Zhang3a453242021-04-16 17:57:09 +0800212 * handle is \ref PSA_NULL_HANDLE
Mingyang Sund44522a2020-01-16 16:48:37 +0800213 * handle node does not be found
214 */
Mingyang Sun783a59b2021-04-20 15:52:18 +0800215static void *tfm_spm_get_rhandle(struct service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800216 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800217{
Ken Liuf250b8b2019-12-27 16:31:24 +0800218 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800219 /* Get reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800220 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800221
Summer Qin630c76b2020-05-20 10:32:58 +0800222 return conn_handle->rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800223}
224
225/* Partition management functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800226
Summer Qin02f7f072020-08-24 16:02:54 +0800227struct tfm_msg_body_t *tfm_spm_get_msg_by_signal(struct partition_t *partition,
228 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800229{
Ken Liu2c47f7f2021-01-22 11:06:04 +0800230 struct bi_list_node_t *node, *head;
Mingyang Sun73056b62020-07-03 15:18:46 +0800231 struct tfm_msg_body_t *tmp_msg, *msg = NULL;
Ken Liu92ede9f2021-10-20 09:35:00 +0800232 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Edison Ai764d41f2018-09-21 15:56:36 +0800233
Ken Liuf250b8b2019-12-27 16:31:24 +0800234 TFM_CORE_ASSERT(partition);
Edison Ai764d41f2018-09-21 15:56:36 +0800235
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800236 head = &partition->msg_list;
Mingyang Sun73056b62020-07-03 15:18:46 +0800237
Ken Liu2c47f7f2021-01-22 11:06:04 +0800238 if (BI_LIST_IS_EMPTY(head)) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800239 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800240 }
241
Mingyang Sun73056b62020-07-03 15:18:46 +0800242 /*
243 * There may be multiple messages for this RoT Service signal, do not clear
244 * partition mask until no remaining message. Search may be optimized.
245 */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800246 BI_LIST_FOR_EACH(node, head) {
Ken Liuc25558c2021-05-20 15:31:28 +0800247 tmp_msg = TO_CONTAINER(node, struct tfm_msg_body_t, msg_node);
Ken Liuacd2a572021-05-12 16:19:04 +0800248 if (tmp_msg->service->p_ldinf->signal == signal && msg) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800249 return msg;
Ken Liuacd2a572021-05-12 16:19:04 +0800250 } else if (tmp_msg->service->p_ldinf->signal == signal) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800251 msg = tmp_msg;
Ken Liu2c47f7f2021-01-22 11:06:04 +0800252 BI_LIST_REMOVE_NODE(node);
Edison Ai764d41f2018-09-21 15:56:36 +0800253 }
254 }
Mingyang Sun73056b62020-07-03 15:18:46 +0800255
Ken Liu92ede9f2021-10-20 09:35:00 +0800256 CRITICAL_SECTION_ENTER(cs_assert);
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800257 partition->signals_asserted &= ~signal;
Ken Liu92ede9f2021-10-20 09:35:00 +0800258 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sun73056b62020-07-03 15:18:46 +0800259
260 return msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800261}
262
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800263uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
264{
Kevin Pengbf9a97e2021-06-18 16:34:12 +0800265#if TFM_LVL == 1
266 return TFM_PARTITION_PRIVILEGED_MODE;
267#else /* TFM_LVL == 1 */
Shawn Shana3657e82021-10-18 14:37:09 +0800268 if (partition_flags & PARTITION_MODEL_PSA_ROT) {
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800269 return TFM_PARTITION_PRIVILEGED_MODE;
270 } else {
271 return TFM_PARTITION_UNPRIVILEGED_MODE;
272 }
Kevin Pengbf9a97e2021-06-18 16:34:12 +0800273#endif /* TFM_LVL == 1 */
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800274}
275
Mingyang Sun783a59b2021-04-20 15:52:18 +0800276struct service_t *tfm_spm_get_service_by_sid(uint32_t sid)
Edison Ai764d41f2018-09-21 15:56:36 +0800277{
Feder.Liangaeb5a792021-08-10 16:12:56 +0800278 struct service_t *p_prev, *p_curr;
Edison Ai764d41f2018-09-21 15:56:36 +0800279
Feder.Liangaeb5a792021-08-10 16:12:56 +0800280 UNI_LIST_FOR_EACH_PREV(p_prev, p_curr, &services_listhead) {
281 if (p_curr->p_ldinf->sid == sid) {
282 UNI_LIST_MOVE_AFTER(&services_listhead, p_prev, p_curr);
283 return p_curr;
Mingyang Sunef42f442021-06-11 15:07:58 +0800284 }
285 }
286
Ken Liuea45b0d2021-05-22 17:41:25 +0800287 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800288}
289
Mingyang Sund44522a2020-01-16 16:48:37 +0800290/**
291 * \brief Get the partition context by partition ID.
292 *
293 * \param[in] partition_id Partition identity
294 *
295 * \retval NULL Failed
296 * \retval "Not NULL" Target partition context pointer,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800297 * \ref partition_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800298 */
Kevin Pengeca45b92021-02-09 14:46:50 +0800299struct partition_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800300{
Ken Liuea45b0d2021-05-22 17:41:25 +0800301 struct partition_t *p_part;
Edison Ai764d41f2018-09-21 15:56:36 +0800302
Mingyang Sundeae45d2021-09-06 15:31:07 +0800303 UNI_LIST_FOR_EACH(p_part, PARTITION_LIST_ADDR) {
Ken Liuea45b0d2021-05-22 17:41:25 +0800304 if (p_part->p_ldinf->pid == partition_id) {
305 return p_part;
306 }
Edison Ai764d41f2018-09-21 15:56:36 +0800307 }
Ken Liuea45b0d2021-05-22 17:41:25 +0800308
Edison Ai764d41f2018-09-21 15:56:36 +0800309 return NULL;
310}
311
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800312struct partition_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800313{
Ken Liuf39d8eb2021-10-07 12:55:33 +0800314 return GET_CURRENT_COMPONENT();
Edison Ai764d41f2018-09-21 15:56:36 +0800315}
316
Mingyang Sun783a59b2021-04-20 15:52:18 +0800317int32_t tfm_spm_check_client_version(struct service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530318 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800319{
Ken Liuf250b8b2019-12-27 16:31:24 +0800320 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800321
Ken Liuacd2a572021-05-12 16:19:04 +0800322 switch (SERVICE_GET_VERSION_POLICY(service->p_ldinf->flags)) {
Ken Liub3b2cb62021-05-22 00:39:28 +0800323 case SERVICE_VERSION_POLICY_RELAXED:
Ken Liuacd2a572021-05-12 16:19:04 +0800324 if (version > service->p_ldinf->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800325 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800326 }
327 break;
Ken Liub3b2cb62021-05-22 00:39:28 +0800328 case SERVICE_VERSION_POLICY_STRICT:
Ken Liuacd2a572021-05-12 16:19:04 +0800329 if (version != service->p_ldinf->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800330 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800331 }
332 break;
333 default:
Ken Liubcae38b2021-01-20 15:47:44 +0800334 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800335 }
Ken Liubcae38b2021-01-20 15:47:44 +0800336 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800337}
338
Edison Aie728fbf2019-11-13 09:37:12 +0800339int32_t tfm_spm_check_authorization(uint32_t sid,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800340 struct service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800341 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800342{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800343 struct partition_t *partition = NULL;
Mingyang Sun2b352662021-04-21 11:35:43 +0800344 uint32_t *dep;
Edison Aie728fbf2019-11-13 09:37:12 +0800345 int32_t i;
346
Ken Liuf250b8b2019-12-27 16:31:24 +0800347 TFM_CORE_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800348
349 if (ns_caller) {
Ken Liuacd2a572021-05-12 16:19:04 +0800350 if (!SERVICE_IS_NS_ACCESSIBLE(service->p_ldinf->flags)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800351 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800352 }
353 } else {
354 partition = tfm_spm_get_running_partition();
355 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800356 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800357 }
358
Ken Liuacd2a572021-05-12 16:19:04 +0800359 dep = (uint32_t *)LOAD_INFO_DEPS(partition->p_ldinf);
360 for (i = 0; i < partition->p_ldinf->ndeps; i++) {
Mingyang Sun2b352662021-04-21 11:35:43 +0800361 if (dep[i] == sid) {
Edison Aie728fbf2019-11-13 09:37:12 +0800362 break;
363 }
364 }
365
Ken Liuacd2a572021-05-12 16:19:04 +0800366 if (i == partition->p_ldinf->ndeps) {
Ken Liubcae38b2021-01-20 15:47:44 +0800367 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800368 }
369 }
Ken Liubcae38b2021-01-20 15:47:44 +0800370 return SPM_SUCCESS;
Edison Aie728fbf2019-11-13 09:37:12 +0800371}
372
Edison Ai764d41f2018-09-21 15:56:36 +0800373/* Message functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800374
Summer Qin02f7f072020-08-24 16:02:54 +0800375struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800376{
377 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200378 * The message handler passed by the caller is considered invalid in the
379 * following cases:
380 * 1. Not a valid message handle. (The address of a message is not the
381 * address of a possible handle from the pool
382 * 2. Handle not belongs to the caller partition (The handle is either
383 * unused, or owned by anither partition)
384 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800385 */
Ken Liu505b1702020-05-29 13:19:58 +0800386 struct tfm_msg_body_t *p_msg;
Kevin Pengeec41a82021-08-18 13:56:23 +0800387 int32_t partition_id;
Ken Liu505b1702020-05-29 13:19:58 +0800388 struct tfm_conn_handle_t *p_conn_handle =
Ken Liu5d73c872021-08-19 19:23:17 +0800389 tfm_spm_to_handle_instance(msg_handle);
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200390
391 if (is_valid_chunk_data_in_pool(
Ken Liu505b1702020-05-29 13:19:58 +0800392 conn_handle_pool, (uint8_t *)p_conn_handle) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800393 return NULL;
394 }
395
Ken Liu505b1702020-05-29 13:19:58 +0800396 p_msg = &p_conn_handle->internal_msg;
397
Edison Ai764d41f2018-09-21 15:56:36 +0800398 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200399 * Check that the magic number is correct. This proves that the message
400 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800401 */
Ken Liu505b1702020-05-29 13:19:58 +0800402 if (p_msg->magic != TFM_MSG_MAGIC) {
Edison Ai764d41f2018-09-21 15:56:36 +0800403 return NULL;
404 }
405
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200406 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800407 partition_id = tfm_spm_partition_get_running_partition_id();
Ken Liuacd2a572021-05-12 16:19:04 +0800408 if (partition_id != p_msg->service->partition->p_ldinf->pid) {
Edison Ai764d41f2018-09-21 15:56:36 +0800409 return NULL;
410 }
411
Ken Liu505b1702020-05-29 13:19:58 +0800412 return p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800413}
414
Kevin Pengdf6aa292021-03-11 17:58:50 +0800415struct tfm_msg_body_t *
416 tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle)
417{
418 TFM_CORE_ASSERT(conn_handle != NULL);
419
420 return &(conn_handle->internal_msg);
421}
422
Edison Ai97115822019-08-01 14:22:19 +0800423void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800424 struct service_t *service,
Ken Liu505b1702020-05-29 13:19:58 +0800425 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800426 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800427 psa_invec *invec, size_t in_len,
428 psa_outvec *outvec, size_t out_len,
429 psa_outvec *caller_outvec)
430{
Edison Ai764d41f2018-09-21 15:56:36 +0800431 uint32_t i;
Ken Liu505b1702020-05-29 13:19:58 +0800432 struct tfm_conn_handle_t *conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800433
Ken Liuf250b8b2019-12-27 16:31:24 +0800434 TFM_CORE_ASSERT(msg);
435 TFM_CORE_ASSERT(service);
436 TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
437 TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
438 TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
439 TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
440 TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800441
Edison Ai764d41f2018-09-21 15:56:36 +0800442 /* Clear message buffer before using it */
Ken Liuf39d8eb2021-10-07 12:55:33 +0800443 spm_memset(&msg->msg, 0, sizeof(psa_msg_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800444
Ken Liu5d73c872021-08-19 19:23:17 +0800445 THRD_SYNC_INIT(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800446 msg->magic = TFM_MSG_MAGIC;
447 msg->service = service;
Ken Liuf39d8eb2021-10-07 12:55:33 +0800448 msg->p_client = GET_CURRENT_COMPONENT();
Edison Ai764d41f2018-09-21 15:56:36 +0800449 msg->caller_outvec = caller_outvec;
Summer Qin1ce712a2019-10-14 18:04:05 +0800450 msg->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800451
452 /* Copy contents */
453 msg->msg.type = type;
454
455 for (i = 0; i < in_len; i++) {
456 msg->msg.in_size[i] = invec[i].len;
457 msg->invec[i].base = invec[i].base;
458 }
459
460 for (i = 0; i < out_len; i++) {
461 msg->msg.out_size[i] = outvec[i].len;
462 msg->outvec[i].base = outvec[i].base;
463 /* Out len is used to record the writed number, set 0 here again */
464 msg->outvec[i].len = 0;
465 }
466
Ken Liu505b1702020-05-29 13:19:58 +0800467 /* Use the user connect handle as the message handle */
468 msg->msg.handle = handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800469
Ken Liu505b1702020-05-29 13:19:58 +0800470 conn_handle = tfm_spm_to_handle_instance(handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800471 /* For connected handle, set rhandle to every message */
Ken Liu505b1702020-05-29 13:19:58 +0800472 if (conn_handle) {
473 msg->msg.rhandle = tfm_spm_get_rhandle(service, conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800474 }
David Hu46603dd2019-12-11 18:05:16 +0800475
476 /* Set the private data of NSPE client caller in multi-core topology */
477 if (TFM_CLIENT_ID_IS_NS(client_id)) {
478 tfm_rpc_set_caller_data(msg, client_id);
479 }
Edison Ai764d41f2018-09-21 15:56:36 +0800480}
481
Kevin Pengeec41a82021-08-18 13:56:23 +0800482int32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800483{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800484 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800485
Kevin Peng79c2bda2020-07-24 16:31:12 +0800486 partition = tfm_spm_get_running_partition();
Ken Liuacd2a572021-05-12 16:19:04 +0800487 if (partition && partition->p_ldinf) {
488 return partition->p_ldinf->pid;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800489 } else {
490 return INVALID_PARTITION_ID;
491 }
Edison Ai764d41f2018-09-21 15:56:36 +0800492}
493
Summer Qin43c185d2019-10-10 15:44:42 +0800494int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800495 enum tfm_memory_access_e access,
496 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800497{
Mingyang Sund1ed6732020-08-26 15:52:21 +0800498 enum tfm_hal_status_t err;
499 uint32_t attr = 0;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800500
501 /* If len is zero, this indicates an empty buffer and base is ignored */
502 if (len == 0) {
Ken Liubcae38b2021-01-20 15:47:44 +0800503 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800504 }
505
506 if (!buffer) {
Ken Liubcae38b2021-01-20 15:47:44 +0800507 return SPM_ERROR_BAD_PARAMETERS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800508 }
509
510 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800511 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800512 }
513
Summer Qin424d4db2019-03-25 14:09:51 +0800514 if (access == TFM_MEMORY_ACCESS_RW) {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800515 attr |= (TFM_HAL_ACCESS_READABLE | TFM_HAL_ACCESS_WRITABLE);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800516 } else {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800517 attr |= TFM_HAL_ACCESS_READABLE;
Summer Qin424d4db2019-03-25 14:09:51 +0800518 }
Mingyang Sund1ed6732020-08-26 15:52:21 +0800519
520 if (privileged == TFM_PARTITION_UNPRIVILEGED_MODE) {
521 attr |= TFM_HAL_ACCESS_UNPRIVILEGED;
522 } else {
523 attr &= ~TFM_HAL_ACCESS_UNPRIVILEGED;
524 }
525
526 if (ns_caller) {
527 attr |= TFM_HAL_ACCESS_NS;
528 }
529
530 err = tfm_hal_memory_has_access((uintptr_t)buffer, len, attr);
531
532 if (err == TFM_HAL_SUCCESS) {
Ken Liubcae38b2021-01-20 15:47:44 +0800533 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800534 }
535
Ken Liubcae38b2021-01-20 15:47:44 +0800536 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800537}
538
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800539bool tfm_spm_is_ns_caller(void)
540{
541#if defined(TFM_MULTI_CORE_TOPOLOGY) || defined(FORWARD_PROT_MSG)
542 /* Multi-core NS PSA API request is processed by pendSV. */
543 return (__get_active_exc_num() == EXC_NUM_PENDSV);
544#else
545 struct partition_t *partition = tfm_spm_get_running_partition();
Mingyang Sune529e3b2021-07-12 14:46:30 +0800546
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800547 if (!partition) {
548 tfm_core_panic();
549 }
550
551 return (partition->p_ldinf->pid == TFM_SP_NON_SECURE_ID);
552#endif
553}
554
Mingyang Sune529e3b2021-07-12 14:46:30 +0800555uint32_t tfm_spm_get_caller_privilege_mode(void)
556{
557 struct partition_t *partition;
558
559#if defined(TFM_MULTI_CORE_TOPOLOGY) || defined(FORWARD_PROT_MSG)
560 /*
561 * In multi-core topology, if PSA request is from mailbox, the client
562 * is unprivileged.
563 */
564 if (__get_active_exc_num() == EXC_NUM_PENDSV) {
565 return TFM_PARTITION_UNPRIVILEGED_MODE;
566 }
567#endif
568 partition = tfm_spm_get_running_partition();
569 if (!partition) {
570 tfm_core_panic();
571 }
572
573 return tfm_spm_partition_get_privileged_mode(partition->p_ldinf->flags);
574}
575
Kevin Peng385fda82021-08-18 10:41:19 +0800576int32_t tfm_spm_get_client_id(bool ns_caller)
577{
578 int32_t client_id;
579
580 if (ns_caller) {
581 client_id = tfm_nspm_get_current_client_id();
582 } else {
583 client_id = tfm_spm_partition_get_running_partition_id();
584 }
585
586 if (ns_caller != (client_id < 0)) {
587 /* NS client ID must be negative and Secure ID must >= 0 */
588 tfm_core_panic();
589 }
590
591 return client_id;
592}
593
Ken Liuce2692d2020-02-11 12:39:36 +0800594uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800595{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800596 struct partition_t *partition;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800597 const struct partition_load_info_t *p_pldi;
598 uint32_t service_setting = 0;
Ken Liuce58bfc2021-05-12 17:54:48 +0800599
David Huf07e97d2021-02-15 22:05:40 +0800600#ifdef TFM_FIH_PROFILE_ON
601 fih_int fih_rc = FIH_FAILURE;
602#endif
Edison Ai764d41f2018-09-21 15:56:36 +0800603
604 tfm_pool_init(conn_handle_pool,
605 POOL_BUFFER_SIZE(conn_handle_pool),
606 sizeof(struct tfm_conn_handle_t),
607 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800608
Mingyang Sundeae45d2021-09-06 15:31:07 +0800609 UNI_LISI_INIT_HEAD(PARTITION_LIST_ADDR);
Ken Liuea45b0d2021-05-22 17:41:25 +0800610 UNI_LISI_INIT_HEAD(&services_listhead);
611
Sherry Zhnag482b88b2021-08-19 17:51:47 +0800612 /* Init the nonsecure context. */
613#ifndef TFM_MULTI_CORE_TOPOLOGY
614 tfm_nspm_ctx_init();
615#endif
616
Ken Liuacd2a572021-05-12 16:19:04 +0800617 while (1) {
Mingyang Sundeae45d2021-09-06 15:31:07 +0800618 partition = load_a_partition_assuredly(PARTITION_LIST_ADDR);
Shawn Shan23331062021-09-01 14:58:21 +0800619 if (partition == NO_MORE_PARTITION) {
Ken Liuacd2a572021-05-12 16:19:04 +0800620 break;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800621 }
622
Mingyang Sundeae45d2021-09-06 15:31:07 +0800623 p_pldi = partition->p_ldinf;
Mingyang Sun2b352662021-04-21 11:35:43 +0800624
Mingyang Sundeae45d2021-09-06 15:31:07 +0800625 if (p_pldi->nservices) {
626 service_setting = load_services_assuredly(
627 partition,
628 &services_listhead,
629 stateless_services_ref_tbl,
630 sizeof(stateless_services_ref_tbl));
Kevin Peng27e42272021-05-24 17:58:53 +0800631 }
632
Mingyang Sundeae45d2021-09-06 15:31:07 +0800633 if (p_pldi->nirqs) {
Kevin Peng27e42272021-05-24 17:58:53 +0800634 load_irqs_assuredly(partition);
635 }
636
Mingyang Sundeae45d2021-09-06 15:31:07 +0800637 /* Bind the partition with platform. */
Ken Liuce58bfc2021-05-12 17:54:48 +0800638#if TFM_FIH_PROFILE_ON
639 FIH_CALL(tfm_hal_bind_boundaries, fih_rc, partition->p_ldinf,
Mingyang Sundeae45d2021-09-06 15:31:07 +0800640 &partition->p_boundaries);
Ken Liuce58bfc2021-05-12 17:54:48 +0800641 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
642 tfm_core_panic();
Mingyang Sun61f8fbc2021-06-04 17:49:56 +0800643 }
Ken Liu86686282021-04-27 11:11:15 +0800644#else /* TFM_FIH_PROFILE_ON */
Ken Liuce58bfc2021-05-12 17:54:48 +0800645 if (tfm_hal_bind_boundaries(partition->p_ldinf,
Mingyang Sundeae45d2021-09-06 15:31:07 +0800646 &partition->p_boundaries)
647 != TFM_HAL_SUCCESS) {
Ken Liuce58bfc2021-05-12 17:54:48 +0800648 tfm_core_panic();
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100649 }
Ken Liuce58bfc2021-05-12 17:54:48 +0800650#endif /* TFM_FIH_PROFILE_ON */
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100651
Mingyang Sundeae45d2021-09-06 15:31:07 +0800652 backend_instance.comp_init_assuredly(partition, service_setting);
Edison Ai764d41f2018-09-21 15:56:36 +0800653 }
654
Mingyang Sundeae45d2021-09-06 15:31:07 +0800655 return backend_instance.system_run();
Edison Ai764d41f2018-09-21 15:56:36 +0800656}
Ken Liu2d175172019-03-21 17:08:41 +0800657
Ken Liu5d73c872021-08-19 19:23:17 +0800658/*
659 * Return both current and next context to assembly via AAPCS trick:
660 * - Returning a 64 bit integer by 32-bit R0 and R1.
661 *
662 * This is architecture-specific, hence the scheduler entry and this
663 * 'do_schedule' MAY be different on another architecture.
664 */
665union returning_contexts_t {
666 struct {
667 uint32_t curr;
668 uint32_t next;
669 } ctx;
Ken Liu2d175172019-03-21 17:08:41 +0800670
Ken Liu5d73c872021-08-19 19:23:17 +0800671 uint64_t curr_next_ctxs;
672};
673
674uint64_t do_schedule(void)
675{
676 union returning_contexts_t ret;
677 struct partition_t *p_part_curr, *p_part_next;
678 struct thread_t *pth_next = thrd_next();
679
Mingyang Sun4f08f4d2021-09-10 17:41:28 +0800680 p_part_curr = GET_THRD_OWNER(CURRENT_THREAD);
681 p_part_next = GET_THRD_OWNER(pth_next);
Ken Liu5d73c872021-08-19 19:23:17 +0800682
683 if (pth_next != NULL && p_part_curr != p_part_next) {
684 /* Check if there is enough room on stack to save more context */
685 if ((p_part_curr->ctx_ctrl.sp_limit +
686 sizeof(struct tfm_additional_context_t)) > __get_PSP()) {
687 tfm_core_panic();
688 }
Mate Toth-Palc430b992019-05-09 21:01:14 +0200689
Ken Liuce58bfc2021-05-12 17:54:48 +0800690 /*
691 * If required, let the platform update boundary based on its
692 * implementation. Change privilege, MPU or other configurations.
693 */
694 if (p_part_curr->p_boundaries != p_part_next->p_boundaries) {
695 if (tfm_hal_update_boundaries(p_part_next->p_ldinf,
696 p_part_next->p_boundaries)
697 != TFM_HAL_SUCCESS) {
698 tfm_core_panic();
699 }
700 }
Feder Liang42f5b562021-09-10 17:38:36 +0800701 ARCH_FLUSH_FP_CONTEXT();
Ken Liu2d175172019-03-21 17:08:41 +0800702 }
David Hufb38d562019-09-23 15:58:34 +0800703
704 /*
705 * Handle pending mailbox message from NS in multi-core topology.
706 * Empty operation on single Armv8-M platform.
707 */
708 tfm_rpc_client_call_handler();
Ken Liu5d73c872021-08-19 19:23:17 +0800709
Mingyang Sun4f08f4d2021-09-10 17:41:28 +0800710 ret.ctx.curr = (uint32_t)CURRENT_THREAD->p_context_ctrl;
Ken Liu5d73c872021-08-19 19:23:17 +0800711 ret.ctx.next = (uint32_t)pth_next->p_context_ctrl;
712
Mingyang Sun4f08f4d2021-09-10 17:41:28 +0800713 CURRENT_THREAD = pth_next;
Ken Liu5d73c872021-08-19 19:23:17 +0800714
715 return ret.curr_next_ctxs;
Ken Liu2d175172019-03-21 17:08:41 +0800716}
Mingyang Sund44522a2020-01-16 16:48:37 +0800717
Summer Qin02f7f072020-08-24 16:02:54 +0800718void update_caller_outvec_len(struct tfm_msg_body_t *msg)
Mingyang Sund44522a2020-01-16 16:48:37 +0800719{
720 uint32_t i;
721
722 /*
723 * FixeMe: abstract these part into dedicated functions to avoid
724 * accessing thread context in psa layer
725 */
Ken Liuf39d8eb2021-10-07 12:55:33 +0800726 /*
727 * If it is a NS request via RPC, the owner of this message is not set.
728 * Or if it is a SFN message, it does not have owner thread state either.
729 */
730 if ((!is_tfm_rpc_msg(msg)) && (msg->sfn_magic != TFM_MSG_MAGIC_SFN)) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800731 TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK);
732 }
733
734 for (i = 0; i < PSA_MAX_IOVEC; i++) {
735 if (msg->msg.out_size[i] == 0) {
736 continue;
737 }
738
739 TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base);
740
741 msg->caller_outvec[i].len = msg->outvec[i].len;
742 }
743}
744
Ken Liu5d73c872021-08-19 19:23:17 +0800745void spm_assert_signal(void *p_pt, psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800746{
Ken Liu92ede9f2021-10-20 09:35:00 +0800747 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Ken Liu5d73c872021-08-19 19:23:17 +0800748 struct partition_t *partition = (struct partition_t *)p_pt;
Mingyang Sund44522a2020-01-16 16:48:37 +0800749
Mingyang Sund44522a2020-01-16 16:48:37 +0800750 if (!partition) {
751 tfm_core_panic();
752 }
753
Ken Liu92ede9f2021-10-20 09:35:00 +0800754 CRITICAL_SECTION_ENTER(cs_assert);
755
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800756 partition->signals_asserted |= signal;
Mingyang Sund44522a2020-01-16 16:48:37 +0800757
Kevin Peng8dac6102021-03-09 16:44:00 +0800758 if (partition->signals_waiting & signal) {
Ken Liu5d73c872021-08-19 19:23:17 +0800759 thrd_wake_up(&partition->waitobj,
760 partition->signals_asserted & partition->signals_waiting);
Kevin Peng8dac6102021-03-09 16:44:00 +0800761 partition->signals_waiting &= ~signal;
762 }
Ken Liu92ede9f2021-10-20 09:35:00 +0800763
764 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sund44522a2020-01-16 16:48:37 +0800765}
766
Kevin Pengeca45b92021-02-09 14:46:50 +0800767__attribute__((naked))
Ken Liu5d73c872021-08-19 19:23:17 +0800768static psa_flih_result_t tfm_flih_deprivileged_handling(void *p_pt,
Kevin Pengec239bb2021-09-03 15:40:27 +0800769 uintptr_t fn_flih,
Ken Liu5d73c872021-08-19 19:23:17 +0800770 void *p_context_ctrl)
Mingyang Sund44522a2020-01-16 16:48:37 +0800771{
Kevin Pengeca45b92021-02-09 14:46:50 +0800772 __ASM volatile("SVC %0 \n"
773 "BX LR \n"
774 : : "I" (TFM_SVC_PREPARE_DEPRIV_FLIH));
775}
Kevin Pengdc791882021-03-12 10:57:12 +0800776
Kevin Pengec239bb2021-09-03 15:40:27 +0800777void spm_handle_interrupt(void *p_pt, struct irq_load_info_t *p_ildi)
Kevin Pengeca45b92021-02-09 14:46:50 +0800778{
Kevin Pengeca45b92021-02-09 14:46:50 +0800779 psa_flih_result_t flih_result;
Kevin Pengec239bb2021-09-03 15:40:27 +0800780 struct partition_t *p_part;
Kevin Pengdc791882021-03-12 10:57:12 +0800781
Kevin Pengec239bb2021-09-03 15:40:27 +0800782 if (!p_pt || !p_ildi) {
Ken Liu5d73c872021-08-19 19:23:17 +0800783 tfm_core_panic();
784 }
Kevin Pengeca45b92021-02-09 14:46:50 +0800785
Kevin Pengec239bb2021-09-03 15:40:27 +0800786 p_part = (struct partition_t *)p_pt;
787
788 if (p_ildi->pid != p_part->p_ldinf->pid) {
789 tfm_core_panic();
790 }
791
792 if (p_ildi->flih_func == NULL) {
Kevin Pengeca45b92021-02-09 14:46:50 +0800793 /* SLIH Model Handling */
Kevin Pengec239bb2021-09-03 15:40:27 +0800794 tfm_hal_irq_disable(p_ildi->source);
Ken Liu5d73c872021-08-19 19:23:17 +0800795 flih_result = PSA_FLIH_SIGNAL;
796 } else {
797 /* FLIH Model Handling */
Kevin Pengec239bb2021-09-03 15:40:27 +0800798 if (tfm_spm_partition_get_privileged_mode(p_part->p_ldinf->flags) ==
Ken Liu5d73c872021-08-19 19:23:17 +0800799 TFM_PARTITION_PRIVILEGED_MODE) {
Kevin Pengec239bb2021-09-03 15:40:27 +0800800 flih_result = p_ildi->flih_func();
Ken Liu5d73c872021-08-19 19:23:17 +0800801 } else {
802 flih_result = tfm_flih_deprivileged_handling(
Mingyang Sun4f08f4d2021-09-10 17:41:28 +0800803 p_part,
804 (uintptr_t)p_ildi->flih_func,
805 CURRENT_THREAD->p_context_ctrl);
Ken Liu5d73c872021-08-19 19:23:17 +0800806 }
Kevin Pengeca45b92021-02-09 14:46:50 +0800807 }
808
Ken Liu5d73c872021-08-19 19:23:17 +0800809 if (flih_result == PSA_FLIH_SIGNAL) {
Kevin Pengec239bb2021-09-03 15:40:27 +0800810 spm_assert_signal(p_pt, p_ildi->signal);
Kevin Pengeca45b92021-02-09 14:46:50 +0800811 }
Mingyang Sund44522a2020-01-16 16:48:37 +0800812}
813
Kevin Peng27e42272021-05-24 17:58:53 +0800814struct irq_load_info_t *get_irq_info_for_signal(
815 const struct partition_load_info_t *p_ldinf,
816 psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800817{
818 size_t i;
Kevin Peng27e42272021-05-24 17:58:53 +0800819 struct irq_load_info_t *irq_info;
Mingyang Sund44522a2020-01-16 16:48:37 +0800820
Ken Liu24dffb22021-02-10 11:03:58 +0800821 if (!IS_ONLY_ONE_BIT_IN_UINT32(signal)) {
Kevin Peng27e42272021-05-24 17:58:53 +0800822 return NULL;
Kevin Peng410bee52021-01-13 16:27:17 +0800823 }
824
Kevin Peng27e42272021-05-24 17:58:53 +0800825 irq_info = (struct irq_load_info_t *)LOAD_INFO_IRQ(p_ldinf);
826 for (i = 0; i < p_ldinf->nirqs; i++) {
827 if (irq_info[i].signal == signal) {
828 return &irq_info[i];
Mingyang Sund44522a2020-01-16 16:48:37 +0800829 }
830 }
Kevin Penga20b5af2021-01-11 11:20:52 +0800831
Kevin Peng27e42272021-05-24 17:58:53 +0800832 return NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800833}