blob: 377508247f432b777f42c27f83943bebc3a35326 [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"
Ken Liu92ede9f2021-10-20 09:35:00 +080016#include "critical_section.h"
Ken Liuf39d8eb2021-10-07 12:55:33 +080017#include "current.h"
David Huf07e97d2021-02-15 22:05:40 +080018#include "fih.h"
Jamie Foxcc31d402019-01-28 17:13:52 +000019#include "psa/client.h"
20#include "psa/service.h"
Ken Liu5d73c872021-08-19 19:23:17 +080021#include "thread.h"
Ken Liubcae38b2021-01-20 15:47:44 +080022#include "internal_errors.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080023#include "tfm_api.h"
Kevin Pengb20b0322022-02-09 16:47:19 +080024#include "tfm_arch.h"
Mingyang Sund1ed6732020-08-26 15:52:21 +080025#include "tfm_hal_defs.h"
Kevin Pengd399a1f2021-09-08 15:33:14 +080026#include "tfm_hal_interrupt.h"
Mingyang Sund1ed6732020-08-26 15:52:21 +080027#include "tfm_hal_isolation.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080028#include "spm_ipc.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080029#include "tfm_peripherals_def.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 */
Ken Liu0bed7e02022-02-10 12:38:07 +080053TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct 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
Summer Qin373feb12020-03-27 15:35:33 +080056/*********************** Connection handle conversion APIs *******************/
57
Summer Qin373feb12020-03-27 15:35:33 +080058#define CONVERSION_FACTOR_BITOFFSET 3
59#define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET)
60/* Set 32 as the maximum */
61#define CONVERSION_FACTOR_VALUE_MAX 0x20
62
63#if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX
64#error "CONVERSION FACTOR OUT OF RANGE"
65#endif
66
67static uint32_t loop_index;
68
69/*
70 * A handle instance psa_handle_t allocated inside SPM is actually a memory
71 * address among the handle pool. Return this handle to the client directly
72 * exposes information of secure memory address. In this case, converting the
73 * handle into another value does not represent the memory address to avoid
74 * exposing secure memory directly to clients.
75 *
76 * This function converts the handle instance into another value by scaling the
77 * handle in pool offset, the converted value is named as a user handle.
78 *
79 * The formula:
80 * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE +
81 * CLIENT_HANDLE_VALUE_MIN + loop_index
82 * where:
83 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
84 * exceed CONVERSION_FACTOR_VALUE_MAX.
85 *
86 * handle_instance in RANGE[POOL_START, POOL_END]
87 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
88 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
89 *
90 * note:
91 * loop_index is used to promise same handle instance is converted into
92 * different user handles in short time.
93 */
Ken Liu0bed7e02022-02-10 12:38:07 +080094psa_handle_t tfm_spm_to_user_handle(struct conn_handle_t *handle_instance)
Summer Qin373feb12020-03-27 15:35:33 +080095{
96 psa_handle_t user_handle;
97
98 loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE;
99 user_handle = (psa_handle_t)((((uintptr_t)handle_instance -
100 (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) +
101 CLIENT_HANDLE_VALUE_MIN + loop_index);
102
103 return user_handle;
104}
105
106/*
107 * This function converts a user handle into a corresponded handle instance.
108 * The converted value is validated before returning, an invalid handle instance
109 * is returned as NULL.
110 *
111 * The formula:
112 * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) /
113 * CONVERSION_FACTOR_VALUE) + POOL_START
114 * where:
115 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
116 * exceed CONVERSION_FACTOR_VALUE_MAX.
117 *
118 * handle_instance in RANGE[POOL_START, POOL_END]
119 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
120 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
121 */
Ken Liu0bed7e02022-02-10 12:38:07 +0800122struct conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle)
Summer Qin373feb12020-03-27 15:35:33 +0800123{
Ken Liu0bed7e02022-02-10 12:38:07 +0800124 struct conn_handle_t *handle_instance;
Summer Qin373feb12020-03-27 15:35:33 +0800125
126 if (user_handle == PSA_NULL_HANDLE) {
127 return NULL;
128 }
129
Ken Liu0bed7e02022-02-10 12:38:07 +0800130 handle_instance = (struct conn_handle_t *)((((uintptr_t)user_handle -
Summer Qin373feb12020-03-27 15:35:33 +0800131 CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) +
132 (uintptr_t)conn_handle_pool);
133
134 return handle_instance;
135}
136
Edison Ai764d41f2018-09-21 15:56:36 +0800137/* Service handle management functions */
Kevin Penge61e7052022-01-27 14:57:06 +0800138struct conn_handle_t *tfm_spm_create_conn_handle(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800139{
Ken Liu0bed7e02022-02-10 12:38:07 +0800140 struct conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800141
Edison Ai764d41f2018-09-21 15:56:36 +0800142 /* Get buffer for handle list structure from handle pool */
Ken Liu0bed7e02022-02-10 12:38:07 +0800143 p_handle = (struct conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
Edison Ai9cc26242019-08-06 11:28:04 +0800144 if (!p_handle) {
Summer Qin630c76b2020-05-20 10:32:58 +0800145 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800146 }
147
Kevin Peng2cc25722021-11-18 11:46:02 +0800148 spm_memset(p_handle, 0, sizeof(*p_handle));
149
Shawn Shancc39fcb2019-11-13 15:38:16 +0800150 p_handle->status = TFM_HANDLE_STATUS_IDLE;
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
Ken Liuad3f3e62022-03-04 22:23:52 +0800155psa_status_t tfm_spm_validate_conn_handle(const struct conn_handle_t *handle)
Summer Qin1ce712a2019-10-14 18:04:05 +0800156{
Kevin Penge61e7052022-01-27 14:57:06 +0800157 /* Check the handle address is valid */
Summer Qin1ce712a2019-10-14 18:04:05 +0800158 if (is_valid_chunk_data_in_pool(conn_handle_pool,
Ken Liuad3f3e62022-03-04 22:23:52 +0800159 (uint8_t *)handle) != true) {
Ken Liubcae38b2021-01-20 15:47:44 +0800160 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800161 }
162
Ken Liuad3f3e62022-03-04 22:23:52 +0800163 return PSA_SUCCESS;
Summer Qin1ce712a2019-10-14 18:04:05 +0800164}
165
Ken Liuad3f3e62022-03-04 22:23:52 +0800166void tfm_spm_free_conn_handle(struct conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800167{
Mingyang Sun620c8562021-11-10 11:44:58 +0800168 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
169
Summer Qin1056d1c2022-10-19 16:07:15 +0800170 SPM_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800171
Mingyang Sun620c8562021-11-10 11:44:58 +0800172 CRITICAL_SECTION_ENTER(cs_assert);
Edison Ai764d41f2018-09-21 15:56:36 +0800173 /* Back handle buffer to pool */
Ken Liu66ca6132021-02-24 08:49:51 +0800174 tfm_pool_free(conn_handle_pool, conn_handle);
Mingyang Sun620c8562021-11-10 11:44:58 +0800175 CRITICAL_SECTION_LEAVE(cs_assert);
Edison Ai764d41f2018-09-21 15:56:36 +0800176}
177
Edison Ai764d41f2018-09-21 15:56:36 +0800178/* Partition management functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800179
Mingyang Sun4609ac72022-02-08 18:56:35 +0800180/* This API is only used in IPC backend. */
181#if CONFIG_TFM_SPM_BACKEND_IPC == 1
Ken Liu0bed7e02022-02-10 12:38:07 +0800182struct conn_handle_t *spm_get_handle_by_signal(struct partition_t *p_ptn,
Ken Liu5a28da32022-01-19 14:37:05 +0800183 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800184{
Mingyang Suna09adda2022-02-16 18:11:33 +0800185 struct conn_handle_t *p_handle_iter;
186 struct conn_handle_t **pr_handle_iter, **last_found_handle_holder = NULL;
Ken Liu92ede9f2021-10-20 09:35:00 +0800187 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Ken Liu5a28da32022-01-19 14:37:05 +0800188 uint32_t nr_found_msgs = 0;
Edison Ai764d41f2018-09-21 15:56:36 +0800189
Mingyang Sun620c8562021-11-10 11:44:58 +0800190 CRITICAL_SECTION_ENTER(cs_assert);
Ken Liu5a28da32022-01-19 14:37:05 +0800191
192 /* Return the last found message which applies a FIFO mechanism. */
Mingyang Suna09adda2022-02-16 18:11:33 +0800193 UNI_LIST_FOREACH_NODE_PNODE(pr_handle_iter, p_handle_iter,
194 p_ptn, p_handles) {
195 if (p_handle_iter->service->p_ldinf->signal == signal) {
196 last_found_handle_holder = pr_handle_iter;
Ken Liu5a28da32022-01-19 14:37:05 +0800197 nr_found_msgs++;
Edison Ai764d41f2018-09-21 15:56:36 +0800198 }
199 }
Mingyang Sun73056b62020-07-03 15:18:46 +0800200
Mingyang Suna09adda2022-02-16 18:11:33 +0800201 if (last_found_handle_holder) {
202 p_handle_iter = *last_found_handle_holder;
203 UNI_LIST_REMOVE_NODE_BY_PNODE(last_found_handle_holder, p_handles);
Ken Liu5a28da32022-01-19 14:37:05 +0800204
Ken Liu5a28da32022-01-19 14:37:05 +0800205 if (nr_found_msgs == 1) {
206 p_ptn->signals_asserted &= ~signal;
207 }
208 }
209
Ken Liu92ede9f2021-10-20 09:35:00 +0800210 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sun73056b62020-07-03 15:18:46 +0800211
Mingyang Suna09adda2022-02-16 18:11:33 +0800212 return p_handle_iter;
Edison Ai764d41f2018-09-21 15:56:36 +0800213}
Mingyang Suna09adda2022-02-16 18:11:33 +0800214#endif /* CONFIG_TFM_SPM_BACKEND_IPC == 1 */
Edison Ai764d41f2018-09-21 15:56:36 +0800215
Mingyang Sun783a59b2021-04-20 15:52:18 +0800216struct service_t *tfm_spm_get_service_by_sid(uint32_t sid)
Edison Ai764d41f2018-09-21 15:56:36 +0800217{
Feder.Liangaeb5a792021-08-10 16:12:56 +0800218 struct service_t *p_prev, *p_curr;
Edison Ai764d41f2018-09-21 15:56:36 +0800219
Ken Liu5a28da32022-01-19 14:37:05 +0800220 UNI_LIST_FOREACH_NODE_PREV(p_prev, p_curr, &services_listhead, next) {
Feder.Liangaeb5a792021-08-10 16:12:56 +0800221 if (p_curr->p_ldinf->sid == sid) {
Ken Liu5a28da32022-01-19 14:37:05 +0800222 UNI_LIST_MOVE_AFTER(&services_listhead, p_prev, p_curr, next);
Feder.Liangaeb5a792021-08-10 16:12:56 +0800223 return p_curr;
Mingyang Sunef42f442021-06-11 15:07:58 +0800224 }
225 }
226
Ken Liuea45b0d2021-05-22 17:41:25 +0800227 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800228}
229
Kevin Peng613b4172022-02-15 14:41:44 +0800230#if CONFIG_TFM_DOORBELL_API == 1
Mingyang Sund44522a2020-01-16 16:48:37 +0800231/**
232 * \brief Get the partition context by partition ID.
233 *
234 * \param[in] partition_id Partition identity
235 *
236 * \retval NULL Failed
237 * \retval "Not NULL" Target partition context pointer,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800238 * \ref partition_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800239 */
Kevin Pengeca45b92021-02-09 14:46:50 +0800240struct partition_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800241{
Ken Liuea45b0d2021-05-22 17:41:25 +0800242 struct partition_t *p_part;
Edison Ai764d41f2018-09-21 15:56:36 +0800243
Ken Liu5a28da32022-01-19 14:37:05 +0800244 UNI_LIST_FOREACH(p_part, PARTITION_LIST_ADDR, next) {
Ken Liuea45b0d2021-05-22 17:41:25 +0800245 if (p_part->p_ldinf->pid == partition_id) {
246 return p_part;
247 }
Edison Ai764d41f2018-09-21 15:56:36 +0800248 }
Ken Liuea45b0d2021-05-22 17:41:25 +0800249
Edison Ai764d41f2018-09-21 15:56:36 +0800250 return NULL;
251}
Kevin Peng613b4172022-02-15 14:41:44 +0800252#endif /* CONFIG_TFM_DOORBELL_API == 1 */
Edison Ai764d41f2018-09-21 15:56:36 +0800253
Mingyang Sun783a59b2021-04-20 15:52:18 +0800254int32_t tfm_spm_check_client_version(struct service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530255 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800256{
Summer Qin1056d1c2022-10-19 16:07:15 +0800257 SPM_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800258
Ken Liuacd2a572021-05-12 16:19:04 +0800259 switch (SERVICE_GET_VERSION_POLICY(service->p_ldinf->flags)) {
Ken Liub3b2cb62021-05-22 00:39:28 +0800260 case SERVICE_VERSION_POLICY_RELAXED:
Ken Liuacd2a572021-05-12 16:19:04 +0800261 if (version > service->p_ldinf->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800262 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800263 }
264 break;
Ken Liub3b2cb62021-05-22 00:39:28 +0800265 case SERVICE_VERSION_POLICY_STRICT:
Ken Liuacd2a572021-05-12 16:19:04 +0800266 if (version != service->p_ldinf->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800267 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800268 }
269 break;
270 default:
Ken Liubcae38b2021-01-20 15:47:44 +0800271 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800272 }
Ken Liuad3f3e62022-03-04 22:23:52 +0800273 return PSA_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800274}
275
Edison Aie728fbf2019-11-13 09:37:12 +0800276int32_t tfm_spm_check_authorization(uint32_t sid,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800277 struct service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800278 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800279{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800280 struct partition_t *partition = NULL;
Mingyang Sun2b352662021-04-21 11:35:43 +0800281 uint32_t *dep;
Edison Aie728fbf2019-11-13 09:37:12 +0800282 int32_t i;
283
Summer Qin1056d1c2022-10-19 16:07:15 +0800284 SPM_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800285
286 if (ns_caller) {
Ken Liuacd2a572021-05-12 16:19:04 +0800287 if (!SERVICE_IS_NS_ACCESSIBLE(service->p_ldinf->flags)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800288 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800289 }
290 } else {
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800291 partition = GET_CURRENT_COMPONENT();
Edison Aie728fbf2019-11-13 09:37:12 +0800292 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800293 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800294 }
295
Ken Liuacd2a572021-05-12 16:19:04 +0800296 dep = (uint32_t *)LOAD_INFO_DEPS(partition->p_ldinf);
297 for (i = 0; i < partition->p_ldinf->ndeps; i++) {
Mingyang Sun2b352662021-04-21 11:35:43 +0800298 if (dep[i] == sid) {
Edison Aie728fbf2019-11-13 09:37:12 +0800299 break;
300 }
301 }
302
Ken Liuacd2a572021-05-12 16:19:04 +0800303 if (i == partition->p_ldinf->ndeps) {
Ken Liubcae38b2021-01-20 15:47:44 +0800304 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800305 }
306 }
Ken Liuad3f3e62022-03-04 22:23:52 +0800307 return PSA_SUCCESS;
Edison Aie728fbf2019-11-13 09:37:12 +0800308}
309
Edison Ai764d41f2018-09-21 15:56:36 +0800310/* Message functions */
Summer Qin401cf812022-04-02 09:46:34 +0800311#if CONFIG_TFM_CONNECTION_BASED_SERVICE_API == 1
Kevin Penge61e7052022-01-27 14:57:06 +0800312struct conn_handle_t *spm_get_handle_by_client_handle(psa_handle_t handle,
313 int32_t client_id)
314{
315 struct conn_handle_t *p_conn_handle = tfm_spm_to_handle_instance(handle);
316
Ken Liuad3f3e62022-03-04 22:23:52 +0800317 if (tfm_spm_validate_conn_handle(p_conn_handle) != PSA_SUCCESS) {
Kevin Penge61e7052022-01-27 14:57:06 +0800318 return NULL;
319 }
320
321 /* Validate the caller id in the connection handle equals client_id. */
322 if (p_conn_handle->msg.client_id != client_id) {
323 return NULL;
324 }
325
326 return p_conn_handle;
327}
Summer Qin401cf812022-04-02 09:46:34 +0800328#endif
Kevin Penge61e7052022-01-27 14:57:06 +0800329
330struct conn_handle_t *spm_get_handle_by_msg_handle(psa_handle_t msg_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800331{
332 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200333 * The message handler passed by the caller is considered invalid in the
334 * following cases:
335 * 1. Not a valid message handle. (The address of a message is not the
336 * address of a possible handle from the pool
337 * 2. Handle not belongs to the caller partition (The handle is either
Mingyang Suna09adda2022-02-16 18:11:33 +0800338 * unused, or owned by another partition)
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200339 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800340 */
Kevin Pengeec41a82021-08-18 13:56:23 +0800341 int32_t partition_id;
Ken Liu0bed7e02022-02-10 12:38:07 +0800342 struct conn_handle_t *p_conn_handle =
Ken Liu5d73c872021-08-19 19:23:17 +0800343 tfm_spm_to_handle_instance(msg_handle);
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200344
Ken Liuad3f3e62022-03-04 22:23:52 +0800345 if (tfm_spm_validate_conn_handle(p_conn_handle) != PSA_SUCCESS) {
Edison Ai764d41f2018-09-21 15:56:36 +0800346 return NULL;
347 }
348
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200349 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800350 partition_id = tfm_spm_partition_get_running_partition_id();
Ken Liu0bed7e02022-02-10 12:38:07 +0800351 if (partition_id != p_conn_handle->service->partition->p_ldinf->pid) {
Edison Ai764d41f2018-09-21 15:56:36 +0800352 return NULL;
353 }
354
Ken Liu0bed7e02022-02-10 12:38:07 +0800355 return p_conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800356}
357
Mingyang Suna09adda2022-02-16 18:11:33 +0800358void spm_fill_message(struct conn_handle_t *conn_handle,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800359 struct service_t *service,
Ken Liu505b1702020-05-29 13:19:58 +0800360 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800361 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800362 psa_invec *invec, size_t in_len,
363 psa_outvec *outvec, size_t out_len,
364 psa_outvec *caller_outvec)
365{
Edison Ai764d41f2018-09-21 15:56:36 +0800366 uint32_t i;
367
Summer Qin1056d1c2022-10-19 16:07:15 +0800368 SPM_ASSERT(conn_handle);
369 SPM_ASSERT(service);
370 SPM_ASSERT(!(invec == NULL && in_len != 0));
371 SPM_ASSERT(!(outvec == NULL && out_len != 0));
372 SPM_ASSERT(in_len <= SIZE_MAX - out_len);
373 SPM_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800374
Edison Ai764d41f2018-09-21 15:56:36 +0800375 /* Clear message buffer before using it */
Mingyang Suna09adda2022-02-16 18:11:33 +0800376 spm_memset(&conn_handle->msg, 0, sizeof(psa_msg_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800377
Mingyang Suna09adda2022-02-16 18:11:33 +0800378 THRD_SYNC_INIT(&conn_handle->ack_evnt);
Mingyang Suna09adda2022-02-16 18:11:33 +0800379 conn_handle->service = service;
380 conn_handle->p_client = GET_CURRENT_COMPONENT();
381 conn_handle->caller_outvec = caller_outvec;
382 conn_handle->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800383
384 /* Copy contents */
Mingyang Suna09adda2022-02-16 18:11:33 +0800385 conn_handle->msg.type = type;
Edison Ai764d41f2018-09-21 15:56:36 +0800386
387 for (i = 0; i < in_len; i++) {
Mingyang Suna09adda2022-02-16 18:11:33 +0800388 conn_handle->msg.in_size[i] = invec[i].len;
389 conn_handle->invec[i].base = invec[i].base;
Edison Ai764d41f2018-09-21 15:56:36 +0800390 }
391
392 for (i = 0; i < out_len; i++) {
Mingyang Suna09adda2022-02-16 18:11:33 +0800393 conn_handle->msg.out_size[i] = outvec[i].len;
394 conn_handle->outvec[i].base = outvec[i].base;
Ken Liu0bed7e02022-02-10 12:38:07 +0800395 /* Out len is used to record the wrote number, set 0 here again */
Mingyang Suna09adda2022-02-16 18:11:33 +0800396 conn_handle->outvec[i].len = 0;
Edison Ai764d41f2018-09-21 15:56:36 +0800397 }
398
Ken Liu505b1702020-05-29 13:19:58 +0800399 /* Use the user connect handle as the message handle */
Mingyang Suna09adda2022-02-16 18:11:33 +0800400 conn_handle->msg.handle = handle;
401 conn_handle->msg.rhandle = conn_handle->rhandle;
David Hu46603dd2019-12-11 18:05:16 +0800402
403 /* Set the private data of NSPE client caller in multi-core topology */
404 if (TFM_CLIENT_ID_IS_NS(client_id)) {
Mingyang Suna09adda2022-02-16 18:11:33 +0800405 tfm_rpc_set_caller_data(conn_handle, client_id);
David Hu46603dd2019-12-11 18:05:16 +0800406 }
Edison Ai764d41f2018-09-21 15:56:36 +0800407}
408
Kevin Pengeec41a82021-08-18 13:56:23 +0800409int32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800410{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800411 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800412
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800413 partition = GET_CURRENT_COMPONENT();
Ken Liuacd2a572021-05-12 16:19:04 +0800414 if (partition && partition->p_ldinf) {
415 return partition->p_ldinf->pid;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800416 } else {
417 return INVALID_PARTITION_ID;
418 }
Edison Ai764d41f2018-09-21 15:56:36 +0800419}
420
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800421bool tfm_spm_is_ns_caller(void)
422{
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800423 struct partition_t *partition = GET_CURRENT_COMPONENT();
Ken Liu897e8f12022-02-10 03:21:17 +0100424
425 if (!partition) {
426 tfm_core_panic();
427 }
428
Chris Brandb4c2b002022-07-21 12:54:00 -0700429 return IS_PARTITION_NS_AGENT(partition->p_ldinf);
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800430}
431
Kevin Peng385fda82021-08-18 10:41:19 +0800432int32_t tfm_spm_get_client_id(bool ns_caller)
433{
434 int32_t client_id;
435
436 if (ns_caller) {
437 client_id = tfm_nspm_get_current_client_id();
438 } else {
439 client_id = tfm_spm_partition_get_running_partition_id();
440 }
441
442 if (ns_caller != (client_id < 0)) {
443 /* NS client ID must be negative and Secure ID must >= 0 */
444 tfm_core_panic();
445 }
446
447 return client_id;
448}
449
Ken Liuce2692d2020-02-11 12:39:36 +0800450uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800451{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800452 struct partition_t *partition;
Ruchika Gupta0b70b062022-06-15 12:03:43 +0530453 uint32_t service_setting;
David Huf07e97d2021-02-15 22:05:40 +0800454 fih_int fih_rc = FIH_FAILURE;
Edison Ai764d41f2018-09-21 15:56:36 +0800455
456 tfm_pool_init(conn_handle_pool,
457 POOL_BUFFER_SIZE(conn_handle_pool),
Ken Liu0bed7e02022-02-10 12:38:07 +0800458 sizeof(struct conn_handle_t),
Sherry Zhang86a71c62022-01-12 18:19:02 +0800459 CONFIG_TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800460
Ken Liu5a28da32022-01-19 14:37:05 +0800461 UNI_LISI_INIT_NODE(PARTITION_LIST_ADDR, next);
462 UNI_LISI_INIT_NODE(&services_listhead, next);
Ken Liuea45b0d2021-05-22 17:41:25 +0800463
Sherry Zhnag482b88b2021-08-19 17:51:47 +0800464 /* Init the nonsecure context. */
Chris Brand56b4d0c2021-12-17 16:15:58 -0800465 tfm_nspm_ctx_init();
Sherry Zhnag482b88b2021-08-19 17:51:47 +0800466
Ken Liuacd2a572021-05-12 16:19:04 +0800467 while (1) {
Mingyang Sundeae45d2021-09-06 15:31:07 +0800468 partition = load_a_partition_assuredly(PARTITION_LIST_ADDR);
Shawn Shan23331062021-09-01 14:58:21 +0800469 if (partition == NO_MORE_PARTITION) {
Ken Liuacd2a572021-05-12 16:19:04 +0800470 break;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800471 }
472
Ruchika Gupta0b70b062022-06-15 12:03:43 +0530473 service_setting = load_services_assuredly(
Mingyang Sundeae45d2021-09-06 15:31:07 +0800474 partition,
475 &services_listhead,
476 stateless_services_ref_tbl,
477 sizeof(stateless_services_ref_tbl));
Kevin Peng27e42272021-05-24 17:58:53 +0800478
Ruchika Gupta0b70b062022-06-15 12:03:43 +0530479 load_irqs_assuredly(partition);
Kevin Peng27e42272021-05-24 17:58:53 +0800480
Mingyang Sundeae45d2021-09-06 15:31:07 +0800481 /* Bind the partition with platform. */
Ken Liu967ffa92022-05-25 15:13:34 +0800482 FIH_CALL(tfm_hal_bind_boundary, fih_rc, partition->p_ldinf,
483 &partition->boundary);
Ken Liuce58bfc2021-05-12 17:54:48 +0800484 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
485 tfm_core_panic();
Mingyang Sun61f8fbc2021-06-04 17:49:56 +0800486 }
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100487
Ken Liu995a9742022-05-18 19:28:30 +0800488 backend_init_comp_assuredly(partition, service_setting);
Edison Ai764d41f2018-09-21 15:56:36 +0800489 }
490
Ken Liu995a9742022-05-18 19:28:30 +0800491 return backend_system_run();
Edison Ai764d41f2018-09-21 15:56:36 +0800492}
Ken Liu2d175172019-03-21 17:08:41 +0800493
Mingyang Suna09adda2022-02-16 18:11:33 +0800494void update_caller_outvec_len(struct conn_handle_t *handle)
Mingyang Sund44522a2020-01-16 16:48:37 +0800495{
496 uint32_t i;
497
498 /*
499 * FixeMe: abstract these part into dedicated functions to avoid
500 * accessing thread context in psa layer
501 */
Ken Liuf39d8eb2021-10-07 12:55:33 +0800502 /*
503 * If it is a NS request via RPC, the owner of this message is not set.
504 * Or if it is a SFN message, it does not have owner thread state either.
505 */
Mingyang Suna09adda2022-02-16 18:11:33 +0800506 if ((!is_tfm_rpc_msg(handle)) && (handle->sfn_magic != TFM_MSG_MAGIC_SFN)) {
Summer Qin1056d1c2022-10-19 16:07:15 +0800507 SPM_ASSERT(handle->ack_evnt.owner->state == THRD_STATE_BLOCK);
Mingyang Sund44522a2020-01-16 16:48:37 +0800508 }
509
510 for (i = 0; i < PSA_MAX_IOVEC; i++) {
Mingyang Suna09adda2022-02-16 18:11:33 +0800511 if (handle->msg.out_size[i] == 0) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800512 continue;
513 }
514
Summer Qin1056d1c2022-10-19 16:07:15 +0800515 SPM_ASSERT(handle->caller_outvec[i].base == handle->outvec[i].base);
Mingyang Sund44522a2020-01-16 16:48:37 +0800516
Mingyang Suna09adda2022-02-16 18:11:33 +0800517 handle->caller_outvec[i].len = handle->outvec[i].len;
Mingyang Sund44522a2020-01-16 16:48:37 +0800518 }
519}
520
Ken Liu5d73c872021-08-19 19:23:17 +0800521void spm_assert_signal(void *p_pt, psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800522{
Ken Liu92ede9f2021-10-20 09:35:00 +0800523 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Ken Liu5d73c872021-08-19 19:23:17 +0800524 struct partition_t *partition = (struct partition_t *)p_pt;
Mingyang Sund44522a2020-01-16 16:48:37 +0800525
Mingyang Sund44522a2020-01-16 16:48:37 +0800526 if (!partition) {
527 tfm_core_panic();
528 }
529
Ken Liu92ede9f2021-10-20 09:35:00 +0800530 CRITICAL_SECTION_ENTER(cs_assert);
531
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800532 partition->signals_asserted |= signal;
Mingyang Sund44522a2020-01-16 16:48:37 +0800533
Kevin Peng8dac6102021-03-09 16:44:00 +0800534 if (partition->signals_waiting & signal) {
Ken Liu995a9742022-05-18 19:28:30 +0800535 backend_wake_up(partition);
Kevin Peng8dac6102021-03-09 16:44:00 +0800536 }
Ken Liu92ede9f2021-10-20 09:35:00 +0800537
538 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sund44522a2020-01-16 16:48:37 +0800539}