blob: 2354e2b2974ac50cd784d6927b8b74877d526287 [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 Brandb4c2b002022-07-21 12:54:00 -07003 * Copyright (c) 2021-2022 Cypress Semiconductor Corporation (an Infineon
4 * company) or an affiliate of Cypress Semiconductor Corporation. All rights
5 * reserved.
Edison Ai764d41f2018-09-21 15:56:36 +08006 *
7 * SPDX-License-Identifier: BSD-3-Clause
8 *
9 */
Mingyang Sunda01a972019-07-12 17:32:59 +080010
Edison Ai764d41f2018-09-21 15:56:36 +080011#include <inttypes.h>
12#include <stdbool.h>
Summer Qin22c301a2022-04-27 17:36:00 +080013#include <stdint.h>
Ken Liu24dffb22021-02-10 11:03:58 +080014#include "bitops.h"
Mingyang Sun4609ac72022-02-08 18:56:35 +080015#include "config_impl.h"
Xinyu Zhangcdbe3622022-10-31 14:34:25 +080016#include "config_spm.h"
Ken Liu92ede9f2021-10-20 09:35:00 +080017#include "critical_section.h"
Ken Liuf39d8eb2021-10-07 12:55:33 +080018#include "current.h"
David Huf07e97d2021-02-15 22:05:40 +080019#include "fih.h"
Jamie Foxcc31d402019-01-28 17:13:52 +000020#include "psa/client.h"
21#include "psa/service.h"
Ken Liu5d73c872021-08-19 19:23:17 +080022#include "thread.h"
Ken Liubcae38b2021-01-20 15:47:44 +080023#include "internal_errors.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080024#include "tfm_api.h"
Kevin Pengb20b0322022-02-09 16:47:19 +080025#include "tfm_arch.h"
Mingyang Sund1ed6732020-08-26 15:52:21 +080026#include "tfm_hal_defs.h"
Kevin Pengd399a1f2021-09-08 15:33:14 +080027#include "tfm_hal_interrupt.h"
Mingyang Sund1ed6732020-08-26 15:52:21 +080028#include "tfm_hal_isolation.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080029#include "spm_ipc.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080030#include "tfm_peripherals_def.h"
Kevin Peng385fda82021-08-18 10:41:19 +080031#include "tfm_nspm.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080032#include "tfm_rpc.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080033#include "tfm_core_trustzone.h"
Ken Liu24dffb22021-02-10 11:03:58 +080034#include "lists.h"
Edison Ai764d41f2018-09-21 15:56:36 +080035#include "tfm_pools.h"
Mingyang Sun22a3faf2021-07-09 15:32:47 +080036#include "region.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080037#include "psa_manifest/pid.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080038#include "ffm/backend.h"
Mingyang Sun00df2352021-04-15 15:46:08 +080039#include "load/partition_defs.h"
40#include "load/service_defs.h"
Ken Liu86686282021-04-27 11:11:15 +080041#include "load/asset_defs.h"
Ken Liuacd2a572021-05-12 16:19:04 +080042#include "load/spm_load_api.h"
Sherry Zhnag482b88b2021-08-19 17:51:47 +080043#include "tfm_nspm.h"
Edison Ai764d41f2018-09-21 15:56:36 +080044
Sherry Zhang86a71c62022-01-12 18:19:02 +080045#if !(defined CONFIG_TFM_CONN_HANDLE_MAX_NUM) || (CONFIG_TFM_CONN_HANDLE_MAX_NUM == 0)
46#error "CONFIG_TFM_CONN_HANDLE_MAX_NUM must be defined and not zero."
47#endif
48
Ken Liuea45b0d2021-05-22 17:41:25 +080049/* Partition and service runtime data list head/runtime data table */
Ken Liuea45b0d2021-05-22 17:41:25 +080050static struct service_head_t services_listhead;
Ken Liub3b2cb62021-05-22 00:39:28 +080051struct service_t *stateless_services_ref_tbl[STATIC_HANDLE_NUM_LIMIT];
Summer Qind99509f2019-08-02 17:36:58 +080052
Edison Ai764d41f2018-09-21 15:56:36 +080053/* Pools */
Ken Liu0bed7e02022-02-10 12:38:07 +080054TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct conn_handle_t),
Sherry Zhang86a71c62022-01-12 18:19:02 +080055 CONFIG_TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +080056
Summer Qin373feb12020-03-27 15:35:33 +080057/*********************** Connection handle conversion APIs *******************/
58
Summer Qin373feb12020-03-27 15:35:33 +080059#define CONVERSION_FACTOR_BITOFFSET 3
60#define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET)
61/* Set 32 as the maximum */
62#define CONVERSION_FACTOR_VALUE_MAX 0x20
63
64#if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX
65#error "CONVERSION FACTOR OUT OF RANGE"
66#endif
67
68static uint32_t loop_index;
69
70/*
71 * A handle instance psa_handle_t allocated inside SPM is actually a memory
72 * address among the handle pool. Return this handle to the client directly
73 * exposes information of secure memory address. In this case, converting the
74 * handle into another value does not represent the memory address to avoid
75 * exposing secure memory directly to clients.
76 *
77 * This function converts the handle instance into another value by scaling the
78 * handle in pool offset, the converted value is named as a user handle.
79 *
80 * The formula:
81 * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE +
82 * CLIENT_HANDLE_VALUE_MIN + loop_index
83 * where:
84 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
85 * exceed CONVERSION_FACTOR_VALUE_MAX.
86 *
87 * handle_instance in RANGE[POOL_START, POOL_END]
88 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
89 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
90 *
91 * note:
92 * loop_index is used to promise same handle instance is converted into
93 * different user handles in short time.
94 */
Ken Liu0bed7e02022-02-10 12:38:07 +080095psa_handle_t tfm_spm_to_user_handle(struct conn_handle_t *handle_instance)
Summer Qin373feb12020-03-27 15:35:33 +080096{
97 psa_handle_t user_handle;
98
99 loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE;
100 user_handle = (psa_handle_t)((((uintptr_t)handle_instance -
101 (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) +
102 CLIENT_HANDLE_VALUE_MIN + loop_index);
103
104 return user_handle;
105}
106
107/*
108 * This function converts a user handle into a corresponded handle instance.
109 * The converted value is validated before returning, an invalid handle instance
110 * is returned as NULL.
111 *
112 * The formula:
113 * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) /
114 * CONVERSION_FACTOR_VALUE) + POOL_START
115 * where:
116 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
117 * exceed CONVERSION_FACTOR_VALUE_MAX.
118 *
119 * handle_instance in RANGE[POOL_START, POOL_END]
120 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
121 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
122 */
Ken Liu0bed7e02022-02-10 12:38:07 +0800123struct conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle)
Summer Qin373feb12020-03-27 15:35:33 +0800124{
Ken Liu0bed7e02022-02-10 12:38:07 +0800125 struct conn_handle_t *handle_instance;
Summer Qin373feb12020-03-27 15:35:33 +0800126
127 if (user_handle == PSA_NULL_HANDLE) {
128 return NULL;
129 }
130
Ken Liu0bed7e02022-02-10 12:38:07 +0800131 handle_instance = (struct conn_handle_t *)((((uintptr_t)user_handle -
Summer Qin373feb12020-03-27 15:35:33 +0800132 CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) +
133 (uintptr_t)conn_handle_pool);
134
135 return handle_instance;
136}
137
Edison Ai764d41f2018-09-21 15:56:36 +0800138/* Service handle management functions */
Kevin Penge61e7052022-01-27 14:57:06 +0800139struct conn_handle_t *tfm_spm_create_conn_handle(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800140{
Ken Liu0bed7e02022-02-10 12:38:07 +0800141 struct conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800142
Edison Ai764d41f2018-09-21 15:56:36 +0800143 /* Get buffer for handle list structure from handle pool */
Ken Liu0bed7e02022-02-10 12:38:07 +0800144 p_handle = (struct conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
Edison Ai9cc26242019-08-06 11:28:04 +0800145 if (!p_handle) {
Summer Qin630c76b2020-05-20 10:32:58 +0800146 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800147 }
148
Kevin Peng2cc25722021-11-18 11:46:02 +0800149 spm_memset(p_handle, 0, sizeof(*p_handle));
150
Shawn Shancc39fcb2019-11-13 15:38:16 +0800151 p_handle->status = TFM_HANDLE_STATUS_IDLE;
Edison Ai764d41f2018-09-21 15:56:36 +0800152
Summer Qin630c76b2020-05-20 10:32:58 +0800153 return p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800154}
155
Ken Liuad3f3e62022-03-04 22:23:52 +0800156psa_status_t tfm_spm_validate_conn_handle(const struct conn_handle_t *handle)
Summer Qin1ce712a2019-10-14 18:04:05 +0800157{
Kevin Penge61e7052022-01-27 14:57:06 +0800158 /* Check the handle address is valid */
Summer Qin1ce712a2019-10-14 18:04:05 +0800159 if (is_valid_chunk_data_in_pool(conn_handle_pool,
Ken Liuad3f3e62022-03-04 22:23:52 +0800160 (uint8_t *)handle) != true) {
Ken Liubcae38b2021-01-20 15:47:44 +0800161 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800162 }
163
Ken Liuad3f3e62022-03-04 22:23:52 +0800164 return PSA_SUCCESS;
Summer Qin1ce712a2019-10-14 18:04:05 +0800165}
166
Ken Liuad3f3e62022-03-04 22:23:52 +0800167void tfm_spm_free_conn_handle(struct conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800168{
Mingyang Sun620c8562021-11-10 11:44:58 +0800169 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
170
Summer Qin1056d1c2022-10-19 16:07:15 +0800171 SPM_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800172
Mingyang Sun620c8562021-11-10 11:44:58 +0800173 CRITICAL_SECTION_ENTER(cs_assert);
Edison Ai764d41f2018-09-21 15:56:36 +0800174 /* Back handle buffer to pool */
Ken Liu66ca6132021-02-24 08:49:51 +0800175 tfm_pool_free(conn_handle_pool, conn_handle);
Mingyang Sun620c8562021-11-10 11:44:58 +0800176 CRITICAL_SECTION_LEAVE(cs_assert);
Edison Ai764d41f2018-09-21 15:56:36 +0800177}
178
Edison Ai764d41f2018-09-21 15:56:36 +0800179/* Partition management functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800180
Mingyang Sun4609ac72022-02-08 18:56:35 +0800181/* This API is only used in IPC backend. */
182#if CONFIG_TFM_SPM_BACKEND_IPC == 1
Ken Liu0bed7e02022-02-10 12:38:07 +0800183struct conn_handle_t *spm_get_handle_by_signal(struct partition_t *p_ptn,
Ken Liu5a28da32022-01-19 14:37:05 +0800184 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800185{
Mingyang Suna09adda2022-02-16 18:11:33 +0800186 struct conn_handle_t *p_handle_iter;
187 struct conn_handle_t **pr_handle_iter, **last_found_handle_holder = NULL;
Ken Liu92ede9f2021-10-20 09:35:00 +0800188 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Ken Liu5a28da32022-01-19 14:37:05 +0800189 uint32_t nr_found_msgs = 0;
Edison Ai764d41f2018-09-21 15:56:36 +0800190
Mingyang Sun620c8562021-11-10 11:44:58 +0800191 CRITICAL_SECTION_ENTER(cs_assert);
Ken Liu5a28da32022-01-19 14:37:05 +0800192
193 /* Return the last found message which applies a FIFO mechanism. */
Mingyang Suna09adda2022-02-16 18:11:33 +0800194 UNI_LIST_FOREACH_NODE_PNODE(pr_handle_iter, p_handle_iter,
195 p_ptn, p_handles) {
196 if (p_handle_iter->service->p_ldinf->signal == signal) {
197 last_found_handle_holder = pr_handle_iter;
Ken Liu5a28da32022-01-19 14:37:05 +0800198 nr_found_msgs++;
Edison Ai764d41f2018-09-21 15:56:36 +0800199 }
200 }
Mingyang Sun73056b62020-07-03 15:18:46 +0800201
Mingyang Suna09adda2022-02-16 18:11:33 +0800202 if (last_found_handle_holder) {
203 p_handle_iter = *last_found_handle_holder;
204 UNI_LIST_REMOVE_NODE_BY_PNODE(last_found_handle_holder, p_handles);
Ken Liu5a28da32022-01-19 14:37:05 +0800205
Ken Liu5a28da32022-01-19 14:37:05 +0800206 if (nr_found_msgs == 1) {
207 p_ptn->signals_asserted &= ~signal;
208 }
209 }
210
Ken Liu92ede9f2021-10-20 09:35:00 +0800211 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sun73056b62020-07-03 15:18:46 +0800212
Mingyang Suna09adda2022-02-16 18:11:33 +0800213 return p_handle_iter;
Edison Ai764d41f2018-09-21 15:56:36 +0800214}
Mingyang Suna09adda2022-02-16 18:11:33 +0800215#endif /* CONFIG_TFM_SPM_BACKEND_IPC == 1 */
Edison Ai764d41f2018-09-21 15:56:36 +0800216
Mingyang Sun783a59b2021-04-20 15:52:18 +0800217struct service_t *tfm_spm_get_service_by_sid(uint32_t sid)
Edison Ai764d41f2018-09-21 15:56:36 +0800218{
Feder.Liangaeb5a792021-08-10 16:12:56 +0800219 struct service_t *p_prev, *p_curr;
Edison Ai764d41f2018-09-21 15:56:36 +0800220
Ken Liu5a28da32022-01-19 14:37:05 +0800221 UNI_LIST_FOREACH_NODE_PREV(p_prev, p_curr, &services_listhead, next) {
Feder.Liangaeb5a792021-08-10 16:12:56 +0800222 if (p_curr->p_ldinf->sid == sid) {
Ken Liu5a28da32022-01-19 14:37:05 +0800223 UNI_LIST_MOVE_AFTER(&services_listhead, p_prev, p_curr, next);
Feder.Liangaeb5a792021-08-10 16:12:56 +0800224 return p_curr;
Mingyang Sunef42f442021-06-11 15:07:58 +0800225 }
226 }
227
Ken Liuea45b0d2021-05-22 17:41:25 +0800228 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800229}
230
Kevin Peng613b4172022-02-15 14:41:44 +0800231#if CONFIG_TFM_DOORBELL_API == 1
Mingyang Sund44522a2020-01-16 16:48:37 +0800232/**
233 * \brief Get the partition context by partition ID.
234 *
235 * \param[in] partition_id Partition identity
236 *
237 * \retval NULL Failed
238 * \retval "Not NULL" Target partition context pointer,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800239 * \ref partition_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800240 */
Kevin Pengeca45b92021-02-09 14:46:50 +0800241struct partition_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800242{
Ken Liuea45b0d2021-05-22 17:41:25 +0800243 struct partition_t *p_part;
Edison Ai764d41f2018-09-21 15:56:36 +0800244
Ken Liu5a28da32022-01-19 14:37:05 +0800245 UNI_LIST_FOREACH(p_part, PARTITION_LIST_ADDR, next) {
Ken Liuea45b0d2021-05-22 17:41:25 +0800246 if (p_part->p_ldinf->pid == partition_id) {
247 return p_part;
248 }
Edison Ai764d41f2018-09-21 15:56:36 +0800249 }
Ken Liuea45b0d2021-05-22 17:41:25 +0800250
Edison Ai764d41f2018-09-21 15:56:36 +0800251 return NULL;
252}
Kevin Peng613b4172022-02-15 14:41:44 +0800253#endif /* CONFIG_TFM_DOORBELL_API == 1 */
Edison Ai764d41f2018-09-21 15:56:36 +0800254
Mingyang Sun783a59b2021-04-20 15:52:18 +0800255int32_t tfm_spm_check_client_version(struct service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530256 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800257{
Summer Qin1056d1c2022-10-19 16:07:15 +0800258 SPM_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800259
Ken Liuacd2a572021-05-12 16:19:04 +0800260 switch (SERVICE_GET_VERSION_POLICY(service->p_ldinf->flags)) {
Ken Liub3b2cb62021-05-22 00:39:28 +0800261 case SERVICE_VERSION_POLICY_RELAXED:
Ken Liuacd2a572021-05-12 16:19:04 +0800262 if (version > service->p_ldinf->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800263 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800264 }
265 break;
Ken Liub3b2cb62021-05-22 00:39:28 +0800266 case SERVICE_VERSION_POLICY_STRICT:
Ken Liuacd2a572021-05-12 16:19:04 +0800267 if (version != service->p_ldinf->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800268 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800269 }
270 break;
271 default:
Ken Liubcae38b2021-01-20 15:47:44 +0800272 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800273 }
Ken Liuad3f3e62022-03-04 22:23:52 +0800274 return PSA_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800275}
276
Edison Aie728fbf2019-11-13 09:37:12 +0800277int32_t tfm_spm_check_authorization(uint32_t sid,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800278 struct service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800279 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800280{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800281 struct partition_t *partition = NULL;
Chris Brand1fb796d2022-10-18 16:54:25 -0700282 const uint32_t *dep;
Edison Aie728fbf2019-11-13 09:37:12 +0800283 int32_t i;
284
Summer Qin1056d1c2022-10-19 16:07:15 +0800285 SPM_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800286
287 if (ns_caller) {
Ken Liuacd2a572021-05-12 16:19:04 +0800288 if (!SERVICE_IS_NS_ACCESSIBLE(service->p_ldinf->flags)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800289 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800290 }
291 } else {
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800292 partition = GET_CURRENT_COMPONENT();
Edison Aie728fbf2019-11-13 09:37:12 +0800293 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800294 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800295 }
296
Chris Brand1fb796d2022-10-18 16:54:25 -0700297 dep = LOAD_INFO_DEPS(partition->p_ldinf);
Ken Liuacd2a572021-05-12 16:19:04 +0800298 for (i = 0; i < partition->p_ldinf->ndeps; i++) {
Mingyang Sun2b352662021-04-21 11:35:43 +0800299 if (dep[i] == sid) {
Edison Aie728fbf2019-11-13 09:37:12 +0800300 break;
301 }
302 }
303
Ken Liuacd2a572021-05-12 16:19:04 +0800304 if (i == partition->p_ldinf->ndeps) {
Ken Liubcae38b2021-01-20 15:47:44 +0800305 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800306 }
307 }
Ken Liuad3f3e62022-03-04 22:23:52 +0800308 return PSA_SUCCESS;
Edison Aie728fbf2019-11-13 09:37:12 +0800309}
310
Edison Ai764d41f2018-09-21 15:56:36 +0800311/* Message functions */
Summer Qin401cf812022-04-02 09:46:34 +0800312#if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1
Kevin Penge61e7052022-01-27 14:57:06 +0800313struct conn_handle_t *spm_get_handle_by_client_handle(psa_handle_t handle,
314 int32_t client_id)
315{
316 struct conn_handle_t *p_conn_handle = tfm_spm_to_handle_instance(handle);
317
Ken Liuad3f3e62022-03-04 22:23:52 +0800318 if (tfm_spm_validate_conn_handle(p_conn_handle) != PSA_SUCCESS) {
Kevin Penge61e7052022-01-27 14:57:06 +0800319 return NULL;
320 }
321
322 /* Validate the caller id in the connection handle equals client_id. */
323 if (p_conn_handle->msg.client_id != client_id) {
324 return NULL;
325 }
326
327 return p_conn_handle;
328}
Summer Qin401cf812022-04-02 09:46:34 +0800329#endif
Kevin Penge61e7052022-01-27 14:57:06 +0800330
331struct conn_handle_t *spm_get_handle_by_msg_handle(psa_handle_t msg_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800332{
333 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200334 * The message handler passed by the caller is considered invalid in the
335 * following cases:
336 * 1. Not a valid message handle. (The address of a message is not the
337 * address of a possible handle from the pool
338 * 2. Handle not belongs to the caller partition (The handle is either
Mingyang Suna09adda2022-02-16 18:11:33 +0800339 * unused, or owned by another partition)
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200340 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800341 */
Kevin Pengeec41a82021-08-18 13:56:23 +0800342 int32_t partition_id;
Ken Liu0bed7e02022-02-10 12:38:07 +0800343 struct conn_handle_t *p_conn_handle =
Ken Liu5d73c872021-08-19 19:23:17 +0800344 tfm_spm_to_handle_instance(msg_handle);
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200345
Ken Liuad3f3e62022-03-04 22:23:52 +0800346 if (tfm_spm_validate_conn_handle(p_conn_handle) != PSA_SUCCESS) {
Edison Ai764d41f2018-09-21 15:56:36 +0800347 return NULL;
348 }
349
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200350 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800351 partition_id = tfm_spm_partition_get_running_partition_id();
Ken Liu0bed7e02022-02-10 12:38:07 +0800352 if (partition_id != p_conn_handle->service->partition->p_ldinf->pid) {
Edison Ai764d41f2018-09-21 15:56:36 +0800353 return NULL;
354 }
355
Ken Liu0bed7e02022-02-10 12:38:07 +0800356 return p_conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800357}
358
Mingyang Suna09adda2022-02-16 18:11:33 +0800359void spm_fill_message(struct conn_handle_t *conn_handle,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800360 struct service_t *service,
Ken Liu505b1702020-05-29 13:19:58 +0800361 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800362 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800363 psa_invec *invec, size_t in_len,
364 psa_outvec *outvec, size_t out_len,
365 psa_outvec *caller_outvec)
366{
Edison Ai764d41f2018-09-21 15:56:36 +0800367 uint32_t i;
368
Summer Qin1056d1c2022-10-19 16:07:15 +0800369 SPM_ASSERT(conn_handle);
370 SPM_ASSERT(service);
371 SPM_ASSERT(!(invec == NULL && in_len != 0));
372 SPM_ASSERT(!(outvec == NULL && out_len != 0));
373 SPM_ASSERT(in_len <= SIZE_MAX - out_len);
374 SPM_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800375
Edison Ai764d41f2018-09-21 15:56:36 +0800376 /* Clear message buffer before using it */
Mingyang Suna09adda2022-02-16 18:11:33 +0800377 spm_memset(&conn_handle->msg, 0, sizeof(psa_msg_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800378
Mingyang Suna09adda2022-02-16 18:11:33 +0800379 THRD_SYNC_INIT(&conn_handle->ack_evnt);
Mingyang Suna09adda2022-02-16 18:11:33 +0800380 conn_handle->service = service;
381 conn_handle->p_client = GET_CURRENT_COMPONENT();
382 conn_handle->caller_outvec = caller_outvec;
383 conn_handle->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800384
385 /* Copy contents */
Mingyang Suna09adda2022-02-16 18:11:33 +0800386 conn_handle->msg.type = type;
Edison Ai764d41f2018-09-21 15:56:36 +0800387
388 for (i = 0; i < in_len; i++) {
Mingyang Suna09adda2022-02-16 18:11:33 +0800389 conn_handle->msg.in_size[i] = invec[i].len;
390 conn_handle->invec[i].base = invec[i].base;
Edison Ai764d41f2018-09-21 15:56:36 +0800391 }
392
393 for (i = 0; i < out_len; i++) {
Mingyang Suna09adda2022-02-16 18:11:33 +0800394 conn_handle->msg.out_size[i] = outvec[i].len;
395 conn_handle->outvec[i].base = outvec[i].base;
Ken Liu0bed7e02022-02-10 12:38:07 +0800396 /* Out len is used to record the wrote number, set 0 here again */
Mingyang Suna09adda2022-02-16 18:11:33 +0800397 conn_handle->outvec[i].len = 0;
Edison Ai764d41f2018-09-21 15:56:36 +0800398 }
399
Ken Liu505b1702020-05-29 13:19:58 +0800400 /* Use the user connect handle as the message handle */
Mingyang Suna09adda2022-02-16 18:11:33 +0800401 conn_handle->msg.handle = handle;
402 conn_handle->msg.rhandle = conn_handle->rhandle;
David Hu46603dd2019-12-11 18:05:16 +0800403
404 /* Set the private data of NSPE client caller in multi-core topology */
405 if (TFM_CLIENT_ID_IS_NS(client_id)) {
Mingyang Suna09adda2022-02-16 18:11:33 +0800406 tfm_rpc_set_caller_data(conn_handle, client_id);
David Hu46603dd2019-12-11 18:05:16 +0800407 }
Edison Ai764d41f2018-09-21 15:56:36 +0800408}
409
Kevin Pengeec41a82021-08-18 13:56:23 +0800410int32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800411{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800412 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800413
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800414 partition = GET_CURRENT_COMPONENT();
Ken Liuacd2a572021-05-12 16:19:04 +0800415 if (partition && partition->p_ldinf) {
416 return partition->p_ldinf->pid;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800417 } else {
418 return INVALID_PARTITION_ID;
419 }
Edison Ai764d41f2018-09-21 15:56:36 +0800420}
421
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800422bool tfm_spm_is_ns_caller(void)
423{
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800424 struct partition_t *partition = GET_CURRENT_COMPONENT();
Ken Liu897e8f12022-02-10 03:21:17 +0100425
426 if (!partition) {
427 tfm_core_panic();
428 }
429
Chris Brandb4c2b002022-07-21 12:54:00 -0700430 return IS_PARTITION_NS_AGENT(partition->p_ldinf);
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800431}
432
Kevin Peng385fda82021-08-18 10:41:19 +0800433int32_t tfm_spm_get_client_id(bool ns_caller)
434{
435 int32_t client_id;
436
437 if (ns_caller) {
438 client_id = tfm_nspm_get_current_client_id();
439 } else {
440 client_id = tfm_spm_partition_get_running_partition_id();
441 }
442
443 if (ns_caller != (client_id < 0)) {
444 /* NS client ID must be negative and Secure ID must >= 0 */
445 tfm_core_panic();
446 }
447
448 return client_id;
449}
450
Ken Liuce2692d2020-02-11 12:39:36 +0800451uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800452{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800453 struct partition_t *partition;
Ruchika Gupta0b70b062022-06-15 12:03:43 +0530454 uint32_t service_setting;
David Huf07e97d2021-02-15 22:05:40 +0800455 fih_int fih_rc = FIH_FAILURE;
Edison Ai764d41f2018-09-21 15:56:36 +0800456
457 tfm_pool_init(conn_handle_pool,
458 POOL_BUFFER_SIZE(conn_handle_pool),
Ken Liu0bed7e02022-02-10 12:38:07 +0800459 sizeof(struct conn_handle_t),
Sherry Zhang86a71c62022-01-12 18:19:02 +0800460 CONFIG_TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800461
Ken Liu5a28da32022-01-19 14:37:05 +0800462 UNI_LISI_INIT_NODE(PARTITION_LIST_ADDR, next);
463 UNI_LISI_INIT_NODE(&services_listhead, next);
Ken Liuea45b0d2021-05-22 17:41:25 +0800464
Sherry Zhnag482b88b2021-08-19 17:51:47 +0800465 /* Init the nonsecure context. */
Chris Brand56b4d0c2021-12-17 16:15:58 -0800466 tfm_nspm_ctx_init();
Sherry Zhnag482b88b2021-08-19 17:51:47 +0800467
Ken Liuacd2a572021-05-12 16:19:04 +0800468 while (1) {
Mingyang Sundeae45d2021-09-06 15:31:07 +0800469 partition = load_a_partition_assuredly(PARTITION_LIST_ADDR);
Shawn Shan23331062021-09-01 14:58:21 +0800470 if (partition == NO_MORE_PARTITION) {
Ken Liuacd2a572021-05-12 16:19:04 +0800471 break;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800472 }
473
Ruchika Gupta0b70b062022-06-15 12:03:43 +0530474 service_setting = load_services_assuredly(
Mingyang Sundeae45d2021-09-06 15:31:07 +0800475 partition,
476 &services_listhead,
477 stateless_services_ref_tbl,
478 sizeof(stateless_services_ref_tbl));
Kevin Peng27e42272021-05-24 17:58:53 +0800479
Ruchika Gupta0b70b062022-06-15 12:03:43 +0530480 load_irqs_assuredly(partition);
Kevin Peng27e42272021-05-24 17:58:53 +0800481
Mingyang Sundeae45d2021-09-06 15:31:07 +0800482 /* Bind the partition with platform. */
Ken Liu967ffa92022-05-25 15:13:34 +0800483 FIH_CALL(tfm_hal_bind_boundary, fih_rc, partition->p_ldinf,
484 &partition->boundary);
Ken Liuce58bfc2021-05-12 17:54:48 +0800485 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
486 tfm_core_panic();
Mingyang Sun61f8fbc2021-06-04 17:49:56 +0800487 }
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100488
Ken Liu995a9742022-05-18 19:28:30 +0800489 backend_init_comp_assuredly(partition, service_setting);
Edison Ai764d41f2018-09-21 15:56:36 +0800490 }
491
Ken Liu995a9742022-05-18 19:28:30 +0800492 return backend_system_run();
Edison Ai764d41f2018-09-21 15:56:36 +0800493}
Ken Liu2d175172019-03-21 17:08:41 +0800494
Mingyang Suna09adda2022-02-16 18:11:33 +0800495void update_caller_outvec_len(struct conn_handle_t *handle)
Mingyang Sund44522a2020-01-16 16:48:37 +0800496{
497 uint32_t i;
498
499 /*
500 * FixeMe: abstract these part into dedicated functions to avoid
501 * accessing thread context in psa layer
502 */
Ken Liuf39d8eb2021-10-07 12:55:33 +0800503 /*
504 * If it is a NS request via RPC, the owner of this message is not set.
505 * Or if it is a SFN message, it does not have owner thread state either.
506 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800507 if ((!is_tfm_rpc_msg(handle)) && (handle->sfn_magic != TFM_MSG_MAGIC_SFN)) {
Summer Qin1056d1c2022-10-19 16:07:15 +0800508 SPM_ASSERT(handle->ack_evnt.owner->state == THRD_STATE_BLOCK);
Mingyang Sund44522a2020-01-16 16:48:37 +0800509 }
510
511 for (i = 0; i < PSA_MAX_IOVEC; i++) {
Mingyang Suna09adda2022-02-16 18:11:33 +0800512 if (handle->msg.out_size[i] == 0) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800513 continue;
514 }
515
Summer Qin1056d1c2022-10-19 16:07:15 +0800516 SPM_ASSERT(handle->caller_outvec[i].base == handle->outvec[i].base);
Mingyang Sund44522a2020-01-16 16:48:37 +0800517
Mingyang Suna09adda2022-02-16 18:11:33 +0800518 handle->caller_outvec[i].len = handle->outvec[i].len;
Mingyang Sund44522a2020-01-16 16:48:37 +0800519 }
520}
521
Ken Liu5d73c872021-08-19 19:23:17 +0800522void spm_assert_signal(void *p_pt, psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800523{
Ken Liu92ede9f2021-10-20 09:35:00 +0800524 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Ken Liu5d73c872021-08-19 19:23:17 +0800525 struct partition_t *partition = (struct partition_t *)p_pt;
Mingyang Sund44522a2020-01-16 16:48:37 +0800526
Mingyang Sund44522a2020-01-16 16:48:37 +0800527 if (!partition) {
528 tfm_core_panic();
529 }
530
Ken Liu92ede9f2021-10-20 09:35:00 +0800531 CRITICAL_SECTION_ENTER(cs_assert);
532
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800533 partition->signals_asserted |= signal;
Mingyang Sund44522a2020-01-16 16:48:37 +0800534
Kevin Peng8dac6102021-03-09 16:44:00 +0800535 if (partition->signals_waiting & signal) {
Ken Liu995a9742022-05-18 19:28:30 +0800536 backend_wake_up(partition);
Kevin Peng8dac6102021-03-09 16:44:00 +0800537 }
Ken Liu92ede9f2021-10-20 09:35:00 +0800538
539 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sund44522a2020-01-16 16:48:37 +0800540}