blob: 0d8b087d714314498de679417fedfc1d16935637 [file] [log] [blame]
Edison Ai764d41f2018-09-21 15:56:36 +08001/*
Sherry Zhangdcab2862022-01-10 10:43:31 +08002 * Copyright (c) 2018-2022, Arm Limited. All rights reserved.
Chris Brand56b4d0c2021-12-17 16:15:58 -08003 * Copyright (c) 2021, Cypress Semiconductor Corporation. All rights reserved.
Edison Ai764d41f2018-09-21 15:56:36 +08004 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 *
7 */
Mingyang Sunda01a972019-07-12 17:32:59 +08008
Edison Ai764d41f2018-09-21 15:56:36 +08009#include <inttypes.h>
10#include <stdbool.h>
Ken Liu086b32c2021-12-27 14:44:52 +080011#include "aapcs_local.h"
Ken Liu24dffb22021-02-10 11:03:58 +080012#include "bitops.h"
Ken Liu92ede9f2021-10-20 09:35:00 +080013#include "critical_section.h"
Ken Liuf39d8eb2021-10-07 12:55:33 +080014#include "current.h"
David Huf07e97d2021-02-15 22:05:40 +080015#include "fih.h"
Jamie Foxcc31d402019-01-28 17:13:52 +000016#include "psa/client.h"
17#include "psa/service.h"
Ken Liu5d73c872021-08-19 19:23:17 +080018#include "thread.h"
Ken Liubcae38b2021-01-20 15:47:44 +080019#include "internal_errors.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080020#include "tfm_spm_hal.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080021#include "tfm_api.h"
22#include "tfm_secure_api.h"
23#include "tfm_memory_utils.h"
Mingyang Sund1ed6732020-08-26 15:52:21 +080024#include "tfm_hal_defs.h"
Kevin Pengd399a1f2021-09-08 15:33:14 +080025#include "tfm_hal_interrupt.h"
Mingyang Sund1ed6732020-08-26 15:52:21 +080026#include "tfm_hal_isolation.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080027#include "spm_ipc.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080028#include "tfm_peripherals_def.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080029#include "tfm_core_utils.h"
Kevin Peng385fda82021-08-18 10:41:19 +080030#include "tfm_nspm.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080031#include "tfm_rpc.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080032#include "tfm_core_trustzone.h"
Ken Liu24dffb22021-02-10 11:03:58 +080033#include "lists.h"
Edison Ai764d41f2018-09-21 15:56:36 +080034#include "tfm_pools.h"
Mingyang Sun22a3faf2021-07-09 15:32:47 +080035#include "region.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080036#include "psa_manifest/pid.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080037#include "ffm/backend.h"
Mingyang Sun00df2352021-04-15 15:46:08 +080038#include "load/partition_defs.h"
39#include "load/service_defs.h"
Ken Liu86686282021-04-27 11:11:15 +080040#include "load/asset_defs.h"
Ken Liuacd2a572021-05-12 16:19:04 +080041#include "load/spm_load_api.h"
Sherry Zhnag482b88b2021-08-19 17:51:47 +080042#include "tfm_nspm.h"
Edison Ai764d41f2018-09-21 15:56:36 +080043
Sherry Zhang86a71c62022-01-12 18:19:02 +080044#if !(defined CONFIG_TFM_CONN_HANDLE_MAX_NUM) || (CONFIG_TFM_CONN_HANDLE_MAX_NUM == 0)
45#error "CONFIG_TFM_CONN_HANDLE_MAX_NUM must be defined and not zero."
46#endif
47
Ken Liuea45b0d2021-05-22 17:41:25 +080048/* Partition and service runtime data list head/runtime data table */
Ken Liuea45b0d2021-05-22 17:41:25 +080049static struct service_head_t services_listhead;
Ken Liub3b2cb62021-05-22 00:39:28 +080050struct service_t *stateless_services_ref_tbl[STATIC_HANDLE_NUM_LIMIT];
Summer Qind99509f2019-08-02 17:36:58 +080051
Edison Ai764d41f2018-09-21 15:56:36 +080052/* Pools */
53TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t),
Sherry Zhang86a71c62022-01-12 18:19:02 +080054 CONFIG_TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +080055
Mingyang Sun620c8562021-11-10 11:44:58 +080056extern uint32_t scheduler_lock;
57
Summer Qin373feb12020-03-27 15:35:33 +080058/*********************** Connection handle conversion APIs *******************/
59
Summer Qin373feb12020-03-27 15:35:33 +080060#define CONVERSION_FACTOR_BITOFFSET 3
61#define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET)
62/* Set 32 as the maximum */
63#define CONVERSION_FACTOR_VALUE_MAX 0x20
64
65#if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX
66#error "CONVERSION FACTOR OUT OF RANGE"
67#endif
68
69static uint32_t loop_index;
70
71/*
72 * A handle instance psa_handle_t allocated inside SPM is actually a memory
73 * address among the handle pool. Return this handle to the client directly
74 * exposes information of secure memory address. In this case, converting the
75 * handle into another value does not represent the memory address to avoid
76 * exposing secure memory directly to clients.
77 *
78 * This function converts the handle instance into another value by scaling the
79 * handle in pool offset, the converted value is named as a user handle.
80 *
81 * The formula:
82 * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE +
83 * CLIENT_HANDLE_VALUE_MIN + loop_index
84 * where:
85 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
86 * exceed CONVERSION_FACTOR_VALUE_MAX.
87 *
88 * handle_instance in RANGE[POOL_START, POOL_END]
89 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
90 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
91 *
92 * note:
93 * loop_index is used to promise same handle instance is converted into
94 * different user handles in short time.
95 */
Ken Liu505b1702020-05-29 13:19:58 +080096psa_handle_t tfm_spm_to_user_handle(struct tfm_conn_handle_t *handle_instance)
Summer Qin373feb12020-03-27 15:35:33 +080097{
98 psa_handle_t user_handle;
99
100 loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE;
101 user_handle = (psa_handle_t)((((uintptr_t)handle_instance -
102 (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) +
103 CLIENT_HANDLE_VALUE_MIN + loop_index);
104
105 return user_handle;
106}
107
108/*
109 * This function converts a user handle into a corresponded handle instance.
110 * The converted value is validated before returning, an invalid handle instance
111 * is returned as NULL.
112 *
113 * The formula:
114 * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) /
115 * CONVERSION_FACTOR_VALUE) + POOL_START
116 * where:
117 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
118 * exceed CONVERSION_FACTOR_VALUE_MAX.
119 *
120 * handle_instance in RANGE[POOL_START, POOL_END]
121 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
122 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
123 */
124struct tfm_conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle)
125{
126 struct tfm_conn_handle_t *handle_instance;
127
128 if (user_handle == PSA_NULL_HANDLE) {
129 return NULL;
130 }
131
132 handle_instance = (struct tfm_conn_handle_t *)((((uintptr_t)user_handle -
133 CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) +
134 (uintptr_t)conn_handle_pool);
135
136 return handle_instance;
137}
138
Edison Ai764d41f2018-09-21 15:56:36 +0800139/* Service handle management functions */
Mingyang Sun783a59b2021-04-20 15:52:18 +0800140struct tfm_conn_handle_t *tfm_spm_create_conn_handle(struct service_t *service,
141 int32_t client_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800142{
Edison Ai9cc26242019-08-06 11:28:04 +0800143 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800144
Ken Liuf250b8b2019-12-27 16:31:24 +0800145 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800146
147 /* Get buffer for handle list structure from handle pool */
Edison Ai9cc26242019-08-06 11:28:04 +0800148 p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
149 if (!p_handle) {
Summer Qin630c76b2020-05-20 10:32:58 +0800150 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800151 }
152
Kevin Peng2cc25722021-11-18 11:46:02 +0800153 spm_memset(p_handle, 0, sizeof(*p_handle));
154
Ken Liuf39d8eb2021-10-07 12:55:33 +0800155 p_handle->internal_msg.service = service;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800156 p_handle->status = TFM_HANDLE_STATUS_IDLE;
Summer Qin1ce712a2019-10-14 18:04:05 +0800157 p_handle->client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800158
Summer Qin630c76b2020-05-20 10:32:58 +0800159 return p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800160}
161
Summer Qin630c76b2020-05-20 10:32:58 +0800162int32_t tfm_spm_validate_conn_handle(
163 const struct tfm_conn_handle_t *conn_handle,
164 int32_t client_id)
Summer Qin1ce712a2019-10-14 18:04:05 +0800165{
166 /* Check the handle address is validated */
167 if (is_valid_chunk_data_in_pool(conn_handle_pool,
168 (uint8_t *)conn_handle) != true) {
Ken Liubcae38b2021-01-20 15:47:44 +0800169 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800170 }
171
172 /* Check the handle caller is correct */
Summer Qin630c76b2020-05-20 10:32:58 +0800173 if (conn_handle->client_id != client_id) {
Ken Liubcae38b2021-01-20 15:47:44 +0800174 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800175 }
176
Ken Liubcae38b2021-01-20 15:47:44 +0800177 return SPM_SUCCESS;
Summer Qin1ce712a2019-10-14 18:04:05 +0800178}
179
Mingyang Sun783a59b2021-04-20 15:52:18 +0800180int32_t tfm_spm_free_conn_handle(struct service_t *service,
Summer Qin02f7f072020-08-24 16:02:54 +0800181 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800182{
Mingyang Sun620c8562021-11-10 11:44:58 +0800183 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
184
Ken Liuf250b8b2019-12-27 16:31:24 +0800185 TFM_CORE_ASSERT(service);
Summer Qin630c76b2020-05-20 10:32:58 +0800186 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800187
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200188 /* Clear magic as the handler is not used anymore */
Summer Qin630c76b2020-05-20 10:32:58 +0800189 conn_handle->internal_msg.magic = 0;
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200190
Mingyang Sun620c8562021-11-10 11:44:58 +0800191 CRITICAL_SECTION_ENTER(cs_assert);
Edison Ai764d41f2018-09-21 15:56:36 +0800192
193 /* Back handle buffer to pool */
Ken Liu66ca6132021-02-24 08:49:51 +0800194 tfm_pool_free(conn_handle_pool, conn_handle);
Mingyang Sun620c8562021-11-10 11:44:58 +0800195 CRITICAL_SECTION_LEAVE(cs_assert);
196
Ken Liubcae38b2021-01-20 15:47:44 +0800197 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800198}
199
Mingyang Sun783a59b2021-04-20 15:52:18 +0800200int32_t tfm_spm_set_rhandle(struct service_t *service,
Summer Qin02f7f072020-08-24 16:02:54 +0800201 struct tfm_conn_handle_t *conn_handle,
202 void *rhandle)
Edison Ai764d41f2018-09-21 15:56:36 +0800203{
Ken Liuf250b8b2019-12-27 16:31:24 +0800204 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800205 /* Set reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800206 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800207
Summer Qin630c76b2020-05-20 10:32:58 +0800208 conn_handle->rhandle = rhandle;
Ken Liubcae38b2021-01-20 15:47:44 +0800209 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800210}
211
Mingyang Sund44522a2020-01-16 16:48:37 +0800212/**
Xinyu Zhang3a453242021-04-16 17:57:09 +0800213 * \brief Get reverse handle value from connection handle.
Mingyang Sund44522a2020-01-16 16:48:37 +0800214 *
215 * \param[in] service Target service context pointer
216 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800217 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800218 *
219 * \retval void * Success
220 * \retval "Does not return" Panic for those:
221 * service pointer are NULL
Xinyu Zhang3a453242021-04-16 17:57:09 +0800222 * handle is \ref PSA_NULL_HANDLE
Mingyang Sund44522a2020-01-16 16:48:37 +0800223 * handle node does not be found
224 */
Mingyang Sun783a59b2021-04-20 15:52:18 +0800225static void *tfm_spm_get_rhandle(struct service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800226 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800227{
Ken Liuf250b8b2019-12-27 16:31:24 +0800228 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800229 /* Get reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800230 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800231
Summer Qin630c76b2020-05-20 10:32:58 +0800232 return conn_handle->rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800233}
234
235/* Partition management functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800236
Ken Liu5a28da32022-01-19 14:37:05 +0800237struct tfm_msg_body_t *spm_get_msg_with_signal(struct partition_t *p_ptn,
238 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800239{
Ken Liu5a28da32022-01-19 14:37:05 +0800240 struct tfm_msg_body_t *p_msg_iter;
241 struct tfm_msg_body_t **pr_msg_iter, **last_found_msg_holder = NULL;
Ken Liu92ede9f2021-10-20 09:35:00 +0800242 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Ken Liu5a28da32022-01-19 14:37:05 +0800243 uint32_t nr_found_msgs = 0;
Edison Ai764d41f2018-09-21 15:56:36 +0800244
Mingyang Sun620c8562021-11-10 11:44:58 +0800245 CRITICAL_SECTION_ENTER(cs_assert);
Ken Liu5a28da32022-01-19 14:37:05 +0800246
247 /* Return the last found message which applies a FIFO mechanism. */
248 UNI_LIST_FOREACH_NODE_PNODE(pr_msg_iter, p_msg_iter, p_ptn, p_messages) {
249 if (p_msg_iter->service->p_ldinf->signal == signal) {
250 last_found_msg_holder = pr_msg_iter;
251 nr_found_msgs++;
Edison Ai764d41f2018-09-21 15:56:36 +0800252 }
253 }
Mingyang Sun73056b62020-07-03 15:18:46 +0800254
Ken Liu5a28da32022-01-19 14:37:05 +0800255 if (last_found_msg_holder) {
256 p_msg_iter = *last_found_msg_holder;
257 UNI_LIST_REMOVE_NODE_BY_PNODE(last_found_msg_holder, p_messages);
258
259 /* Clear the signal bit since the only message is removed. */
260 if (nr_found_msgs == 1) {
261 p_ptn->signals_asserted &= ~signal;
262 }
263 }
264
Ken Liu92ede9f2021-10-20 09:35:00 +0800265 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sun73056b62020-07-03 15:18:46 +0800266
Ken Liu5a28da32022-01-19 14:37:05 +0800267 return p_msg_iter;
Edison Ai764d41f2018-09-21 15:56:36 +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
Ken Liu5a28da32022-01-19 14:37:05 +0800274 UNI_LIST_FOREACH_NODE_PREV(p_prev, p_curr, &services_listhead, next) {
Feder.Liangaeb5a792021-08-10 16:12:56 +0800275 if (p_curr->p_ldinf->sid == sid) {
Ken Liu5a28da32022-01-19 14:37:05 +0800276 UNI_LIST_MOVE_AFTER(&services_listhead, p_prev, p_curr, next);
Feder.Liangaeb5a792021-08-10 16:12:56 +0800277 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 Liu5a28da32022-01-19 14:37:05 +0800297 UNI_LIST_FOREACH(p_part, PARTITION_LIST_ADDR, next) {
Ken Liuea45b0d2021-05-22 17:41:25 +0800298 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 Sun783a59b2021-04-20 15:52:18 +0800306int32_t tfm_spm_check_client_version(struct service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530307 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800308{
Ken Liuf250b8b2019-12-27 16:31:24 +0800309 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800310
Ken Liuacd2a572021-05-12 16:19:04 +0800311 switch (SERVICE_GET_VERSION_POLICY(service->p_ldinf->flags)) {
Ken Liub3b2cb62021-05-22 00:39:28 +0800312 case SERVICE_VERSION_POLICY_RELAXED:
Ken Liuacd2a572021-05-12 16:19:04 +0800313 if (version > service->p_ldinf->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800314 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800315 }
316 break;
Ken Liub3b2cb62021-05-22 00:39:28 +0800317 case SERVICE_VERSION_POLICY_STRICT:
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;
322 default:
Ken Liubcae38b2021-01-20 15:47:44 +0800323 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800324 }
Ken Liubcae38b2021-01-20 15:47:44 +0800325 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800326}
327
Edison Aie728fbf2019-11-13 09:37:12 +0800328int32_t tfm_spm_check_authorization(uint32_t sid,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800329 struct service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800330 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800331{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800332 struct partition_t *partition = NULL;
Mingyang Sun2b352662021-04-21 11:35:43 +0800333 uint32_t *dep;
Edison Aie728fbf2019-11-13 09:37:12 +0800334 int32_t i;
335
Ken Liuf250b8b2019-12-27 16:31:24 +0800336 TFM_CORE_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800337
338 if (ns_caller) {
Ken Liuacd2a572021-05-12 16:19:04 +0800339 if (!SERVICE_IS_NS_ACCESSIBLE(service->p_ldinf->flags)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800340 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800341 }
342 } else {
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800343 partition = GET_CURRENT_COMPONENT();
Edison Aie728fbf2019-11-13 09:37:12 +0800344 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800345 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800346 }
347
Ken Liuacd2a572021-05-12 16:19:04 +0800348 dep = (uint32_t *)LOAD_INFO_DEPS(partition->p_ldinf);
349 for (i = 0; i < partition->p_ldinf->ndeps; i++) {
Mingyang Sun2b352662021-04-21 11:35:43 +0800350 if (dep[i] == sid) {
Edison Aie728fbf2019-11-13 09:37:12 +0800351 break;
352 }
353 }
354
Ken Liuacd2a572021-05-12 16:19:04 +0800355 if (i == partition->p_ldinf->ndeps) {
Ken Liubcae38b2021-01-20 15:47:44 +0800356 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800357 }
358 }
Ken Liubcae38b2021-01-20 15:47:44 +0800359 return SPM_SUCCESS;
Edison Aie728fbf2019-11-13 09:37:12 +0800360}
361
Edison Ai764d41f2018-09-21 15:56:36 +0800362/* Message functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800363
Summer Qin02f7f072020-08-24 16:02:54 +0800364struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800365{
366 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200367 * The message handler passed by the caller is considered invalid in the
368 * following cases:
369 * 1. Not a valid message handle. (The address of a message is not the
370 * address of a possible handle from the pool
371 * 2. Handle not belongs to the caller partition (The handle is either
372 * unused, or owned by anither partition)
373 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800374 */
Ken Liu505b1702020-05-29 13:19:58 +0800375 struct tfm_msg_body_t *p_msg;
Kevin Pengeec41a82021-08-18 13:56:23 +0800376 int32_t partition_id;
Ken Liu505b1702020-05-29 13:19:58 +0800377 struct tfm_conn_handle_t *p_conn_handle =
Ken Liu5d73c872021-08-19 19:23:17 +0800378 tfm_spm_to_handle_instance(msg_handle);
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200379
380 if (is_valid_chunk_data_in_pool(
Ken Liu505b1702020-05-29 13:19:58 +0800381 conn_handle_pool, (uint8_t *)p_conn_handle) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800382 return NULL;
383 }
384
Ken Liu505b1702020-05-29 13:19:58 +0800385 p_msg = &p_conn_handle->internal_msg;
386
Edison Ai764d41f2018-09-21 15:56:36 +0800387 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200388 * Check that the magic number is correct. This proves that the message
389 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800390 */
Ken Liu505b1702020-05-29 13:19:58 +0800391 if (p_msg->magic != TFM_MSG_MAGIC) {
Edison Ai764d41f2018-09-21 15:56:36 +0800392 return NULL;
393 }
394
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200395 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800396 partition_id = tfm_spm_partition_get_running_partition_id();
Ken Liuacd2a572021-05-12 16:19:04 +0800397 if (partition_id != p_msg->service->partition->p_ldinf->pid) {
Edison Ai764d41f2018-09-21 15:56:36 +0800398 return NULL;
399 }
400
Ken Liu505b1702020-05-29 13:19:58 +0800401 return p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800402}
403
Edison Ai97115822019-08-01 14:22:19 +0800404void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800405 struct service_t *service,
Ken Liu505b1702020-05-29 13:19:58 +0800406 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800407 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800408 psa_invec *invec, size_t in_len,
409 psa_outvec *outvec, size_t out_len,
410 psa_outvec *caller_outvec)
411{
Edison Ai764d41f2018-09-21 15:56:36 +0800412 uint32_t i;
Ken Liu505b1702020-05-29 13:19:58 +0800413 struct tfm_conn_handle_t *conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800414
Ken Liuf250b8b2019-12-27 16:31:24 +0800415 TFM_CORE_ASSERT(msg);
416 TFM_CORE_ASSERT(service);
417 TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
418 TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
419 TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
420 TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
421 TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800422
Edison Ai764d41f2018-09-21 15:56:36 +0800423 /* Clear message buffer before using it */
Ken Liuf39d8eb2021-10-07 12:55:33 +0800424 spm_memset(&msg->msg, 0, sizeof(psa_msg_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800425
Ken Liu5d73c872021-08-19 19:23:17 +0800426 THRD_SYNC_INIT(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800427 msg->magic = TFM_MSG_MAGIC;
428 msg->service = service;
Ken Liuf39d8eb2021-10-07 12:55:33 +0800429 msg->p_client = GET_CURRENT_COMPONENT();
Edison Ai764d41f2018-09-21 15:56:36 +0800430 msg->caller_outvec = caller_outvec;
Summer Qin1ce712a2019-10-14 18:04:05 +0800431 msg->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800432
433 /* Copy contents */
434 msg->msg.type = type;
435
436 for (i = 0; i < in_len; i++) {
437 msg->msg.in_size[i] = invec[i].len;
438 msg->invec[i].base = invec[i].base;
439 }
440
441 for (i = 0; i < out_len; i++) {
442 msg->msg.out_size[i] = outvec[i].len;
443 msg->outvec[i].base = outvec[i].base;
444 /* Out len is used to record the writed number, set 0 here again */
445 msg->outvec[i].len = 0;
446 }
447
Ken Liu505b1702020-05-29 13:19:58 +0800448 /* Use the user connect handle as the message handle */
449 msg->msg.handle = handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800450
Ken Liu505b1702020-05-29 13:19:58 +0800451 conn_handle = tfm_spm_to_handle_instance(handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800452 /* For connected handle, set rhandle to every message */
Ken Liu505b1702020-05-29 13:19:58 +0800453 if (conn_handle) {
454 msg->msg.rhandle = tfm_spm_get_rhandle(service, conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800455 }
David Hu46603dd2019-12-11 18:05:16 +0800456
457 /* Set the private data of NSPE client caller in multi-core topology */
458 if (TFM_CLIENT_ID_IS_NS(client_id)) {
459 tfm_rpc_set_caller_data(msg, client_id);
460 }
Edison Ai764d41f2018-09-21 15:56:36 +0800461}
462
Kevin Pengeec41a82021-08-18 13:56:23 +0800463int32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800464{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800465 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800466
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800467 partition = GET_CURRENT_COMPONENT();
Ken Liuacd2a572021-05-12 16:19:04 +0800468 if (partition && partition->p_ldinf) {
469 return partition->p_ldinf->pid;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800470 } else {
471 return INVALID_PARTITION_ID;
472 }
Edison Ai764d41f2018-09-21 15:56:36 +0800473}
474
Summer Qin43c185d2019-10-10 15:44:42 +0800475int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800476 enum tfm_memory_access_e access,
477 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800478{
Mingyang Sund1ed6732020-08-26 15:52:21 +0800479 enum tfm_hal_status_t err;
480 uint32_t attr = 0;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800481
482 /* If len is zero, this indicates an empty buffer and base is ignored */
483 if (len == 0) {
Ken Liubcae38b2021-01-20 15:47:44 +0800484 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800485 }
486
487 if (!buffer) {
Ken Liubcae38b2021-01-20 15:47:44 +0800488 return SPM_ERROR_BAD_PARAMETERS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800489 }
490
491 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800492 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800493 }
494
Summer Qin424d4db2019-03-25 14:09:51 +0800495 if (access == TFM_MEMORY_ACCESS_RW) {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800496 attr |= (TFM_HAL_ACCESS_READABLE | TFM_HAL_ACCESS_WRITABLE);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800497 } else {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800498 attr |= TFM_HAL_ACCESS_READABLE;
Summer Qin424d4db2019-03-25 14:09:51 +0800499 }
Mingyang Sund1ed6732020-08-26 15:52:21 +0800500
501 if (privileged == TFM_PARTITION_UNPRIVILEGED_MODE) {
502 attr |= TFM_HAL_ACCESS_UNPRIVILEGED;
503 } else {
504 attr &= ~TFM_HAL_ACCESS_UNPRIVILEGED;
505 }
506
507 if (ns_caller) {
508 attr |= TFM_HAL_ACCESS_NS;
509 }
510
511 err = tfm_hal_memory_has_access((uintptr_t)buffer, len, attr);
512
513 if (err == TFM_HAL_SUCCESS) {
Ken Liubcae38b2021-01-20 15:47:44 +0800514 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800515 }
516
Ken Liubcae38b2021-01-20 15:47:44 +0800517 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800518}
519
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800520bool tfm_spm_is_ns_caller(void)
521{
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800522 struct partition_t *partition = GET_CURRENT_COMPONENT();
Mingyang Sune529e3b2021-07-12 14:46:30 +0800523
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800524 if (!partition) {
525 tfm_core_panic();
526 }
527
528 return (partition->p_ldinf->pid == TFM_SP_NON_SECURE_ID);
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800529}
530
Kevin Peng385fda82021-08-18 10:41:19 +0800531int32_t tfm_spm_get_client_id(bool ns_caller)
532{
533 int32_t client_id;
534
535 if (ns_caller) {
536 client_id = tfm_nspm_get_current_client_id();
537 } else {
538 client_id = tfm_spm_partition_get_running_partition_id();
539 }
540
541 if (ns_caller != (client_id < 0)) {
542 /* NS client ID must be negative and Secure ID must >= 0 */
543 tfm_core_panic();
544 }
545
546 return client_id;
547}
548
Ken Liuce2692d2020-02-11 12:39:36 +0800549uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800550{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800551 struct partition_t *partition;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800552 const struct partition_load_info_t *p_pldi;
553 uint32_t service_setting = 0;
Ken Liuce58bfc2021-05-12 17:54:48 +0800554
David Huf07e97d2021-02-15 22:05:40 +0800555#ifdef TFM_FIH_PROFILE_ON
556 fih_int fih_rc = FIH_FAILURE;
557#endif
Edison Ai764d41f2018-09-21 15:56:36 +0800558
559 tfm_pool_init(conn_handle_pool,
560 POOL_BUFFER_SIZE(conn_handle_pool),
561 sizeof(struct tfm_conn_handle_t),
Sherry Zhang86a71c62022-01-12 18:19:02 +0800562 CONFIG_TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800563
Ken Liu5a28da32022-01-19 14:37:05 +0800564 UNI_LISI_INIT_NODE(PARTITION_LIST_ADDR, next);
565 UNI_LISI_INIT_NODE(&services_listhead, next);
Ken Liuea45b0d2021-05-22 17:41:25 +0800566
Sherry Zhnag482b88b2021-08-19 17:51:47 +0800567 /* Init the nonsecure context. */
Chris Brand56b4d0c2021-12-17 16:15:58 -0800568 tfm_nspm_ctx_init();
Sherry Zhnag482b88b2021-08-19 17:51:47 +0800569
Ken Liuacd2a572021-05-12 16:19:04 +0800570 while (1) {
Mingyang Sundeae45d2021-09-06 15:31:07 +0800571 partition = load_a_partition_assuredly(PARTITION_LIST_ADDR);
Shawn Shan23331062021-09-01 14:58:21 +0800572 if (partition == NO_MORE_PARTITION) {
Ken Liuacd2a572021-05-12 16:19:04 +0800573 break;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800574 }
575
Mingyang Sundeae45d2021-09-06 15:31:07 +0800576 p_pldi = partition->p_ldinf;
Mingyang Sun2b352662021-04-21 11:35:43 +0800577
Mingyang Sundeae45d2021-09-06 15:31:07 +0800578 if (p_pldi->nservices) {
579 service_setting = load_services_assuredly(
580 partition,
581 &services_listhead,
582 stateless_services_ref_tbl,
583 sizeof(stateless_services_ref_tbl));
Kevin Peng27e42272021-05-24 17:58:53 +0800584 }
585
Mingyang Sundeae45d2021-09-06 15:31:07 +0800586 if (p_pldi->nirqs) {
Kevin Peng27e42272021-05-24 17:58:53 +0800587 load_irqs_assuredly(partition);
588 }
589
Mingyang Sundeae45d2021-09-06 15:31:07 +0800590 /* Bind the partition with platform. */
Ken Liuce58bfc2021-05-12 17:54:48 +0800591#if TFM_FIH_PROFILE_ON
592 FIH_CALL(tfm_hal_bind_boundaries, fih_rc, partition->p_ldinf,
Mingyang Sundeae45d2021-09-06 15:31:07 +0800593 &partition->p_boundaries);
Ken Liuce58bfc2021-05-12 17:54:48 +0800594 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
595 tfm_core_panic();
Mingyang Sun61f8fbc2021-06-04 17:49:56 +0800596 }
Ken Liu86686282021-04-27 11:11:15 +0800597#else /* TFM_FIH_PROFILE_ON */
Ken Liuce58bfc2021-05-12 17:54:48 +0800598 if (tfm_hal_bind_boundaries(partition->p_ldinf,
Mingyang Sundeae45d2021-09-06 15:31:07 +0800599 &partition->p_boundaries)
600 != TFM_HAL_SUCCESS) {
Ken Liuce58bfc2021-05-12 17:54:48 +0800601 tfm_core_panic();
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100602 }
Ken Liuce58bfc2021-05-12 17:54:48 +0800603#endif /* TFM_FIH_PROFILE_ON */
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100604
Mingyang Sundeae45d2021-09-06 15:31:07 +0800605 backend_instance.comp_init_assuredly(partition, service_setting);
Edison Ai764d41f2018-09-21 15:56:36 +0800606 }
607
Mingyang Sundeae45d2021-09-06 15:31:07 +0800608 return backend_instance.system_run();
Edison Ai764d41f2018-09-21 15:56:36 +0800609}
Ken Liu2d175172019-03-21 17:08:41 +0800610
Ken Liu5d73c872021-08-19 19:23:17 +0800611uint64_t do_schedule(void)
612{
Ken Liu086b32c2021-12-27 14:44:52 +0800613 AAPCS_DUAL_U32_T ctx_ctrls;
Ken Liu5d73c872021-08-19 19:23:17 +0800614 struct partition_t *p_part_curr, *p_part_next;
615 struct thread_t *pth_next = thrd_next();
Kevin Pengca59ec02021-12-09 14:35:50 +0800616 struct critical_section_t cs = CRITICAL_SECTION_STATIC_INIT;
Ken Liu5d73c872021-08-19 19:23:17 +0800617
Ken Liu086b32c2021-12-27 14:44:52 +0800618 AAPCS_DUAL_U32_SET(ctx_ctrls, (uint32_t)CURRENT_THREAD->p_context_ctrl,
619 (uint32_t)CURRENT_THREAD->p_context_ctrl);
620
Mingyang Sun4f08f4d2021-09-10 17:41:28 +0800621 p_part_curr = GET_THRD_OWNER(CURRENT_THREAD);
622 p_part_next = GET_THRD_OWNER(pth_next);
Ken Liu5d73c872021-08-19 19:23:17 +0800623
Mingyang Sun620c8562021-11-10 11:44:58 +0800624 if (scheduler_lock != SCHEDULER_LOCKED && pth_next != NULL &&
625 p_part_curr != p_part_next) {
Ken Liu5d73c872021-08-19 19:23:17 +0800626 /* Check if there is enough room on stack to save more context */
627 if ((p_part_curr->ctx_ctrl.sp_limit +
628 sizeof(struct tfm_additional_context_t)) > __get_PSP()) {
629 tfm_core_panic();
630 }
Mate Toth-Palc430b992019-05-09 21:01:14 +0200631
Kevin Pengca59ec02021-12-09 14:35:50 +0800632 CRITICAL_SECTION_ENTER(cs);
Ken Liuce58bfc2021-05-12 17:54:48 +0800633 /*
634 * If required, let the platform update boundary based on its
635 * implementation. Change privilege, MPU or other configurations.
636 */
637 if (p_part_curr->p_boundaries != p_part_next->p_boundaries) {
638 if (tfm_hal_update_boundaries(p_part_next->p_ldinf,
639 p_part_next->p_boundaries)
640 != TFM_HAL_SUCCESS) {
641 tfm_core_panic();
642 }
643 }
Feder Liang42f5b562021-09-10 17:38:36 +0800644 ARCH_FLUSH_FP_CONTEXT();
Mingyang Sun620c8562021-11-10 11:44:58 +0800645
Ken Liu086b32c2021-12-27 14:44:52 +0800646 AAPCS_DUAL_U32_SET_A1(ctx_ctrls, (uint32_t)pth_next->p_context_ctrl);
647
Mingyang Sun620c8562021-11-10 11:44:58 +0800648 CURRENT_THREAD = pth_next;
Kevin Pengca59ec02021-12-09 14:35:50 +0800649 CRITICAL_SECTION_LEAVE(cs);
Ken Liu2d175172019-03-21 17:08:41 +0800650 }
David Hufb38d562019-09-23 15:58:34 +0800651
Ken Liu086b32c2021-12-27 14:44:52 +0800652 return AAPCS_DUAL_U32_AS_U64(ctx_ctrls);
Ken Liu2d175172019-03-21 17:08:41 +0800653}
Mingyang Sund44522a2020-01-16 16:48:37 +0800654
Summer Qin02f7f072020-08-24 16:02:54 +0800655void update_caller_outvec_len(struct tfm_msg_body_t *msg)
Mingyang Sund44522a2020-01-16 16:48:37 +0800656{
657 uint32_t i;
658
659 /*
660 * FixeMe: abstract these part into dedicated functions to avoid
661 * accessing thread context in psa layer
662 */
Ken Liuf39d8eb2021-10-07 12:55:33 +0800663 /*
664 * If it is a NS request via RPC, the owner of this message is not set.
665 * Or if it is a SFN message, it does not have owner thread state either.
666 */
667 if ((!is_tfm_rpc_msg(msg)) && (msg->sfn_magic != TFM_MSG_MAGIC_SFN)) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800668 TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK);
669 }
670
671 for (i = 0; i < PSA_MAX_IOVEC; i++) {
672 if (msg->msg.out_size[i] == 0) {
673 continue;
674 }
675
676 TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base);
677
678 msg->caller_outvec[i].len = msg->outvec[i].len;
679 }
680}
681
Ken Liu5d73c872021-08-19 19:23:17 +0800682void spm_assert_signal(void *p_pt, psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800683{
Ken Liu92ede9f2021-10-20 09:35:00 +0800684 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Ken Liu5d73c872021-08-19 19:23:17 +0800685 struct partition_t *partition = (struct partition_t *)p_pt;
Mingyang Sund44522a2020-01-16 16:48:37 +0800686
Mingyang Sund44522a2020-01-16 16:48:37 +0800687 if (!partition) {
688 tfm_core_panic();
689 }
690
Ken Liu92ede9f2021-10-20 09:35:00 +0800691 CRITICAL_SECTION_ENTER(cs_assert);
692
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800693 partition->signals_asserted |= signal;
Mingyang Sund44522a2020-01-16 16:48:37 +0800694
Kevin Peng8dac6102021-03-09 16:44:00 +0800695 if (partition->signals_waiting & signal) {
Ken Liu5d73c872021-08-19 19:23:17 +0800696 thrd_wake_up(&partition->waitobj,
697 partition->signals_asserted & partition->signals_waiting);
Kevin Peng8dac6102021-03-09 16:44:00 +0800698 partition->signals_waiting &= ~signal;
699 }
Ken Liu92ede9f2021-10-20 09:35:00 +0800700
701 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sund44522a2020-01-16 16:48:37 +0800702}