blob: c6a26bf51f5bae636dfd9c0cf5c161141675624b [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"
Kevin Pengb20b0322022-02-09 16:47:19 +080022#include "tfm_arch.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080023#include "tfm_secure_api.h"
24#include "tfm_memory_utils.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"
Mingyang Sund44522a2020-01-16 16:48:37 +080030#include "tfm_core_utils.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
Mingyang Sun620c8562021-11-10 11:44:58 +080057extern uint32_t scheduler_lock;
58
Summer Qin373feb12020-03-27 15:35:33 +080059/*********************** Connection handle conversion APIs *******************/
60
Summer Qin373feb12020-03-27 15:35:33 +080061#define CONVERSION_FACTOR_BITOFFSET 3
62#define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET)
63/* Set 32 as the maximum */
64#define CONVERSION_FACTOR_VALUE_MAX 0x20
65
66#if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX
67#error "CONVERSION FACTOR OUT OF RANGE"
68#endif
69
70static uint32_t loop_index;
71
72/*
73 * A handle instance psa_handle_t allocated inside SPM is actually a memory
74 * address among the handle pool. Return this handle to the client directly
75 * exposes information of secure memory address. In this case, converting the
76 * handle into another value does not represent the memory address to avoid
77 * exposing secure memory directly to clients.
78 *
79 * This function converts the handle instance into another value by scaling the
80 * handle in pool offset, the converted value is named as a user handle.
81 *
82 * The formula:
83 * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE +
84 * CLIENT_HANDLE_VALUE_MIN + loop_index
85 * where:
86 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
87 * exceed CONVERSION_FACTOR_VALUE_MAX.
88 *
89 * handle_instance in RANGE[POOL_START, POOL_END]
90 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
91 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
92 *
93 * note:
94 * loop_index is used to promise same handle instance is converted into
95 * different user handles in short time.
96 */
Ken Liu0bed7e02022-02-10 12:38:07 +080097psa_handle_t tfm_spm_to_user_handle(struct conn_handle_t *handle_instance)
Summer Qin373feb12020-03-27 15:35:33 +080098{
99 psa_handle_t user_handle;
100
101 loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE;
102 user_handle = (psa_handle_t)((((uintptr_t)handle_instance -
103 (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) +
104 CLIENT_HANDLE_VALUE_MIN + loop_index);
105
106 return user_handle;
107}
108
109/*
110 * This function converts a user handle into a corresponded handle instance.
111 * The converted value is validated before returning, an invalid handle instance
112 * is returned as NULL.
113 *
114 * The formula:
115 * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) /
116 * CONVERSION_FACTOR_VALUE) + POOL_START
117 * where:
118 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
119 * exceed CONVERSION_FACTOR_VALUE_MAX.
120 *
121 * handle_instance in RANGE[POOL_START, POOL_END]
122 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
123 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
124 */
Ken Liu0bed7e02022-02-10 12:38:07 +0800125struct conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle)
Summer Qin373feb12020-03-27 15:35:33 +0800126{
Ken Liu0bed7e02022-02-10 12:38:07 +0800127 struct conn_handle_t *handle_instance;
Summer Qin373feb12020-03-27 15:35:33 +0800128
129 if (user_handle == PSA_NULL_HANDLE) {
130 return NULL;
131 }
132
Ken Liu0bed7e02022-02-10 12:38:07 +0800133 handle_instance = (struct conn_handle_t *)((((uintptr_t)user_handle -
Summer Qin373feb12020-03-27 15:35:33 +0800134 CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) +
135 (uintptr_t)conn_handle_pool);
136
137 return handle_instance;
138}
139
Edison Ai764d41f2018-09-21 15:56:36 +0800140/* Service handle management functions */
Ken Liu0bed7e02022-02-10 12:38:07 +0800141struct conn_handle_t *tfm_spm_create_conn_handle(struct service_t *service,
142 int32_t client_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800143{
Ken Liu0bed7e02022-02-10 12:38:07 +0800144 struct conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800145
Ken Liuf250b8b2019-12-27 16:31:24 +0800146 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800147
148 /* Get buffer for handle list structure from handle pool */
Ken Liu0bed7e02022-02-10 12:38:07 +0800149 p_handle = (struct conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
Edison Ai9cc26242019-08-06 11:28:04 +0800150 if (!p_handle) {
Summer Qin630c76b2020-05-20 10:32:58 +0800151 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800152 }
153
Kevin Peng2cc25722021-11-18 11:46:02 +0800154 spm_memset(p_handle, 0, sizeof(*p_handle));
155
Ken Liu0bed7e02022-02-10 12:38:07 +0800156 p_handle->service = service;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800157 p_handle->status = TFM_HANDLE_STATUS_IDLE;
Summer Qin1ce712a2019-10-14 18:04:05 +0800158 p_handle->client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800159
Summer Qin630c76b2020-05-20 10:32:58 +0800160 return p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800161}
162
Ken Liu0bed7e02022-02-10 12:38:07 +0800163int32_t tfm_spm_validate_conn_handle(const struct 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,
Ken Liu0bed7e02022-02-10 12:38:07 +0800181 struct 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 */
Ken Liu0bed7e02022-02-10 12:38:07 +0800189 conn_handle->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
Edison Ai764d41f2018-09-21 15:56:36 +0800200/* Partition management functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800201
Ken Liu0bed7e02022-02-10 12:38:07 +0800202struct conn_handle_t *spm_get_handle_by_signal(struct partition_t *p_ptn,
Ken Liu5a28da32022-01-19 14:37:05 +0800203 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800204{
Ken Liu0bed7e02022-02-10 12:38:07 +0800205 struct conn_handle_t *p_msg_iter;
206 struct conn_handle_t **pr_msg_iter, **last_found_msg_holder = NULL;
Ken Liu92ede9f2021-10-20 09:35:00 +0800207 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Ken Liu5a28da32022-01-19 14:37:05 +0800208 uint32_t nr_found_msgs = 0;
Edison Ai764d41f2018-09-21 15:56:36 +0800209
Mingyang Sun620c8562021-11-10 11:44:58 +0800210 CRITICAL_SECTION_ENTER(cs_assert);
Ken Liu5a28da32022-01-19 14:37:05 +0800211
212 /* Return the last found message which applies a FIFO mechanism. */
Ken Liu0bed7e02022-02-10 12:38:07 +0800213 UNI_LIST_FOREACH_NODE_PNODE(pr_msg_iter, p_msg_iter, p_ptn, p_handles) {
Ken Liu5a28da32022-01-19 14:37:05 +0800214 if (p_msg_iter->service->p_ldinf->signal == signal) {
215 last_found_msg_holder = pr_msg_iter;
216 nr_found_msgs++;
Edison Ai764d41f2018-09-21 15:56:36 +0800217 }
218 }
Mingyang Sun73056b62020-07-03 15:18:46 +0800219
Ken Liu5a28da32022-01-19 14:37:05 +0800220 if (last_found_msg_holder) {
221 p_msg_iter = *last_found_msg_holder;
Ken Liu0bed7e02022-02-10 12:38:07 +0800222 UNI_LIST_REMOVE_NODE_BY_PNODE(last_found_msg_holder, p_handles);
Ken Liu5a28da32022-01-19 14:37:05 +0800223
Ken Liu5a28da32022-01-19 14:37:05 +0800224 if (nr_found_msgs == 1) {
225 p_ptn->signals_asserted &= ~signal;
226 }
227 }
228
Ken Liu92ede9f2021-10-20 09:35:00 +0800229 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sun73056b62020-07-03 15:18:46 +0800230
Ken Liu5a28da32022-01-19 14:37:05 +0800231 return p_msg_iter;
Edison Ai764d41f2018-09-21 15:56:36 +0800232}
233
Mingyang Sun783a59b2021-04-20 15:52:18 +0800234struct service_t *tfm_spm_get_service_by_sid(uint32_t sid)
Edison Ai764d41f2018-09-21 15:56:36 +0800235{
Feder.Liangaeb5a792021-08-10 16:12:56 +0800236 struct service_t *p_prev, *p_curr;
Edison Ai764d41f2018-09-21 15:56:36 +0800237
Ken Liu5a28da32022-01-19 14:37:05 +0800238 UNI_LIST_FOREACH_NODE_PREV(p_prev, p_curr, &services_listhead, next) {
Feder.Liangaeb5a792021-08-10 16:12:56 +0800239 if (p_curr->p_ldinf->sid == sid) {
Ken Liu5a28da32022-01-19 14:37:05 +0800240 UNI_LIST_MOVE_AFTER(&services_listhead, p_prev, p_curr, next);
Feder.Liangaeb5a792021-08-10 16:12:56 +0800241 return p_curr;
Mingyang Sunef42f442021-06-11 15:07:58 +0800242 }
243 }
244
Ken Liuea45b0d2021-05-22 17:41:25 +0800245 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800246}
247
Mingyang Sund44522a2020-01-16 16:48:37 +0800248/**
249 * \brief Get the partition context by partition ID.
250 *
251 * \param[in] partition_id Partition identity
252 *
253 * \retval NULL Failed
254 * \retval "Not NULL" Target partition context pointer,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800255 * \ref partition_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800256 */
Kevin Pengeca45b92021-02-09 14:46:50 +0800257struct partition_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800258{
Ken Liuea45b0d2021-05-22 17:41:25 +0800259 struct partition_t *p_part;
Edison Ai764d41f2018-09-21 15:56:36 +0800260
Ken Liu5a28da32022-01-19 14:37:05 +0800261 UNI_LIST_FOREACH(p_part, PARTITION_LIST_ADDR, next) {
Ken Liuea45b0d2021-05-22 17:41:25 +0800262 if (p_part->p_ldinf->pid == partition_id) {
263 return p_part;
264 }
Edison Ai764d41f2018-09-21 15:56:36 +0800265 }
Ken Liuea45b0d2021-05-22 17:41:25 +0800266
Edison Ai764d41f2018-09-21 15:56:36 +0800267 return NULL;
268}
269
Mingyang Sun783a59b2021-04-20 15:52:18 +0800270int32_t tfm_spm_check_client_version(struct service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530271 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800272{
Ken Liuf250b8b2019-12-27 16:31:24 +0800273 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800274
Ken Liuacd2a572021-05-12 16:19:04 +0800275 switch (SERVICE_GET_VERSION_POLICY(service->p_ldinf->flags)) {
Ken Liub3b2cb62021-05-22 00:39:28 +0800276 case SERVICE_VERSION_POLICY_RELAXED:
Ken Liuacd2a572021-05-12 16:19:04 +0800277 if (version > service->p_ldinf->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800278 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800279 }
280 break;
Ken Liub3b2cb62021-05-22 00:39:28 +0800281 case SERVICE_VERSION_POLICY_STRICT:
Ken Liuacd2a572021-05-12 16:19:04 +0800282 if (version != service->p_ldinf->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800283 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800284 }
285 break;
286 default:
Ken Liubcae38b2021-01-20 15:47:44 +0800287 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800288 }
Ken Liubcae38b2021-01-20 15:47:44 +0800289 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800290}
291
Edison Aie728fbf2019-11-13 09:37:12 +0800292int32_t tfm_spm_check_authorization(uint32_t sid,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800293 struct service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800294 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800295{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800296 struct partition_t *partition = NULL;
Mingyang Sun2b352662021-04-21 11:35:43 +0800297 uint32_t *dep;
Edison Aie728fbf2019-11-13 09:37:12 +0800298 int32_t i;
299
Ken Liuf250b8b2019-12-27 16:31:24 +0800300 TFM_CORE_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800301
302 if (ns_caller) {
Ken Liuacd2a572021-05-12 16:19:04 +0800303 if (!SERVICE_IS_NS_ACCESSIBLE(service->p_ldinf->flags)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800304 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800305 }
306 } else {
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800307 partition = GET_CURRENT_COMPONENT();
Edison Aie728fbf2019-11-13 09:37:12 +0800308 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800309 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800310 }
311
Ken Liuacd2a572021-05-12 16:19:04 +0800312 dep = (uint32_t *)LOAD_INFO_DEPS(partition->p_ldinf);
313 for (i = 0; i < partition->p_ldinf->ndeps; i++) {
Mingyang Sun2b352662021-04-21 11:35:43 +0800314 if (dep[i] == sid) {
Edison Aie728fbf2019-11-13 09:37:12 +0800315 break;
316 }
317 }
318
Ken Liuacd2a572021-05-12 16:19:04 +0800319 if (i == partition->p_ldinf->ndeps) {
Ken Liubcae38b2021-01-20 15:47:44 +0800320 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800321 }
322 }
Ken Liubcae38b2021-01-20 15:47:44 +0800323 return SPM_SUCCESS;
Edison Aie728fbf2019-11-13 09:37:12 +0800324}
325
Edison Ai764d41f2018-09-21 15:56:36 +0800326/* Message functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800327
Ken Liu0bed7e02022-02-10 12:38:07 +0800328struct conn_handle_t *spm_get_handle_by_user_handle(psa_handle_t msg_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800329{
330 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200331 * The message handler passed by the caller is considered invalid in the
332 * following cases:
333 * 1. Not a valid message handle. (The address of a message is not the
334 * address of a possible handle from the pool
335 * 2. Handle not belongs to the caller partition (The handle is either
336 * unused, or owned by anither partition)
337 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800338 */
Kevin Pengeec41a82021-08-18 13:56:23 +0800339 int32_t partition_id;
Ken Liu0bed7e02022-02-10 12:38:07 +0800340 struct conn_handle_t *p_conn_handle =
Ken Liu5d73c872021-08-19 19:23:17 +0800341 tfm_spm_to_handle_instance(msg_handle);
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200342
343 if (is_valid_chunk_data_in_pool(
Ken Liu0bed7e02022-02-10 12:38:07 +0800344 conn_handle_pool, (uint8_t *)p_conn_handle) != true) {
Edison Ai764d41f2018-09-21 15:56:36 +0800345 return NULL;
346 }
347
348 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200349 * Check that the magic number is correct. This proves that the message
350 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800351 */
Ken Liu0bed7e02022-02-10 12:38:07 +0800352 if (p_conn_handle->magic != TFM_MSG_MAGIC) {
Edison Ai764d41f2018-09-21 15:56:36 +0800353 return NULL;
354 }
355
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200356 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800357 partition_id = tfm_spm_partition_get_running_partition_id();
Ken Liu0bed7e02022-02-10 12:38:07 +0800358 if (partition_id != p_conn_handle->service->partition->p_ldinf->pid) {
Edison Ai764d41f2018-09-21 15:56:36 +0800359 return NULL;
360 }
361
Ken Liu0bed7e02022-02-10 12:38:07 +0800362 return p_conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800363}
364
Ken Liu0bed7e02022-02-10 12:38:07 +0800365void spm_fill_message(struct conn_handle_t *hdl,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800366 struct service_t *service,
Ken Liu505b1702020-05-29 13:19:58 +0800367 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800368 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800369 psa_invec *invec, size_t in_len,
370 psa_outvec *outvec, size_t out_len,
371 psa_outvec *caller_outvec)
372{
Edison Ai764d41f2018-09-21 15:56:36 +0800373 uint32_t i;
374
Ken Liu0bed7e02022-02-10 12:38:07 +0800375 TFM_CORE_ASSERT(hdl);
Ken Liuf250b8b2019-12-27 16:31:24 +0800376 TFM_CORE_ASSERT(service);
377 TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
378 TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
379 TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
380 TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
381 TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800382
Edison Ai764d41f2018-09-21 15:56:36 +0800383 /* Clear message buffer before using it */
Ken Liu0bed7e02022-02-10 12:38:07 +0800384 spm_memset(&hdl->msg, 0, sizeof(psa_msg_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800385
Ken Liu0bed7e02022-02-10 12:38:07 +0800386 THRD_SYNC_INIT(&hdl->ack_evnt);
387 hdl->magic = TFM_MSG_MAGIC;
388 hdl->service = service;
389 hdl->p_client = GET_CURRENT_COMPONENT();
390 hdl->caller_outvec = caller_outvec;
391 hdl->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800392
393 /* Copy contents */
Ken Liu0bed7e02022-02-10 12:38:07 +0800394 hdl->msg.type = type;
Edison Ai764d41f2018-09-21 15:56:36 +0800395
396 for (i = 0; i < in_len; i++) {
Ken Liu0bed7e02022-02-10 12:38:07 +0800397 hdl->msg.in_size[i] = invec[i].len;
398 hdl->invec[i].base = invec[i].base;
Edison Ai764d41f2018-09-21 15:56:36 +0800399 }
400
401 for (i = 0; i < out_len; i++) {
Ken Liu0bed7e02022-02-10 12:38:07 +0800402 hdl->msg.out_size[i] = outvec[i].len;
403 hdl->outvec[i].base = outvec[i].base;
404 /* Out len is used to record the wrote number, set 0 here again */
405 hdl->outvec[i].len = 0;
Edison Ai764d41f2018-09-21 15:56:36 +0800406 }
407
Ken Liu505b1702020-05-29 13:19:58 +0800408 /* Use the user connect handle as the message handle */
Ken Liu0bed7e02022-02-10 12:38:07 +0800409 hdl->msg.handle = handle;
410 hdl->msg.rhandle = hdl->rhandle;
David Hu46603dd2019-12-11 18:05:16 +0800411
412 /* Set the private data of NSPE client caller in multi-core topology */
413 if (TFM_CLIENT_ID_IS_NS(client_id)) {
Ken Liu0bed7e02022-02-10 12:38:07 +0800414 tfm_rpc_set_caller_data(hdl, client_id);
David Hu46603dd2019-12-11 18:05:16 +0800415 }
Edison Ai764d41f2018-09-21 15:56:36 +0800416}
417
Kevin Pengeec41a82021-08-18 13:56:23 +0800418int32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800419{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800420 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800421
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800422 partition = GET_CURRENT_COMPONENT();
Ken Liuacd2a572021-05-12 16:19:04 +0800423 if (partition && partition->p_ldinf) {
424 return partition->p_ldinf->pid;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800425 } else {
426 return INVALID_PARTITION_ID;
427 }
Edison Ai764d41f2018-09-21 15:56:36 +0800428}
429
Summer Qin43c185d2019-10-10 15:44:42 +0800430int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800431 enum tfm_memory_access_e access,
432 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800433{
Mingyang Sund1ed6732020-08-26 15:52:21 +0800434 enum tfm_hal_status_t err;
435 uint32_t attr = 0;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800436
437 /* If len is zero, this indicates an empty buffer and base is ignored */
438 if (len == 0) {
Ken Liubcae38b2021-01-20 15:47:44 +0800439 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800440 }
441
442 if (!buffer) {
Ken Liubcae38b2021-01-20 15:47:44 +0800443 return SPM_ERROR_BAD_PARAMETERS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800444 }
445
446 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800447 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800448 }
449
Summer Qin424d4db2019-03-25 14:09:51 +0800450 if (access == TFM_MEMORY_ACCESS_RW) {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800451 attr |= (TFM_HAL_ACCESS_READABLE | TFM_HAL_ACCESS_WRITABLE);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800452 } else {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800453 attr |= TFM_HAL_ACCESS_READABLE;
Summer Qin424d4db2019-03-25 14:09:51 +0800454 }
Mingyang Sund1ed6732020-08-26 15:52:21 +0800455
456 if (privileged == TFM_PARTITION_UNPRIVILEGED_MODE) {
457 attr |= TFM_HAL_ACCESS_UNPRIVILEGED;
458 } else {
459 attr &= ~TFM_HAL_ACCESS_UNPRIVILEGED;
460 }
461
462 if (ns_caller) {
463 attr |= TFM_HAL_ACCESS_NS;
464 }
465
466 err = tfm_hal_memory_has_access((uintptr_t)buffer, len, attr);
467
468 if (err == TFM_HAL_SUCCESS) {
Ken Liubcae38b2021-01-20 15:47:44 +0800469 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800470 }
471
Ken Liubcae38b2021-01-20 15:47:44 +0800472 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800473}
474
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800475bool tfm_spm_is_ns_caller(void)
476{
Kevin Pengcdf3ae22022-01-19 14:53:52 +0800477 struct partition_t *partition = GET_CURRENT_COMPONENT();
Ken Liu897e8f12022-02-10 03:21:17 +0100478
479 if (!partition) {
480 tfm_core_panic();
481 }
482
483 return (partition->p_ldinf->pid == TFM_SP_NON_SECURE_ID);
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800484}
485
Kevin Peng385fda82021-08-18 10:41:19 +0800486int32_t tfm_spm_get_client_id(bool ns_caller)
487{
488 int32_t client_id;
489
490 if (ns_caller) {
491 client_id = tfm_nspm_get_current_client_id();
492 } else {
493 client_id = tfm_spm_partition_get_running_partition_id();
494 }
495
496 if (ns_caller != (client_id < 0)) {
497 /* NS client ID must be negative and Secure ID must >= 0 */
498 tfm_core_panic();
499 }
500
501 return client_id;
502}
503
Ken Liuce2692d2020-02-11 12:39:36 +0800504uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800505{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800506 struct partition_t *partition;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800507 const struct partition_load_info_t *p_pldi;
508 uint32_t service_setting = 0;
Ken Liuce58bfc2021-05-12 17:54:48 +0800509
David Huf07e97d2021-02-15 22:05:40 +0800510#ifdef TFM_FIH_PROFILE_ON
511 fih_int fih_rc = FIH_FAILURE;
512#endif
Edison Ai764d41f2018-09-21 15:56:36 +0800513
514 tfm_pool_init(conn_handle_pool,
515 POOL_BUFFER_SIZE(conn_handle_pool),
Ken Liu0bed7e02022-02-10 12:38:07 +0800516 sizeof(struct conn_handle_t),
Sherry Zhang86a71c62022-01-12 18:19:02 +0800517 CONFIG_TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800518
Ken Liu5a28da32022-01-19 14:37:05 +0800519 UNI_LISI_INIT_NODE(PARTITION_LIST_ADDR, next);
520 UNI_LISI_INIT_NODE(&services_listhead, next);
Ken Liuea45b0d2021-05-22 17:41:25 +0800521
Sherry Zhnag482b88b2021-08-19 17:51:47 +0800522 /* Init the nonsecure context. */
Chris Brand56b4d0c2021-12-17 16:15:58 -0800523 tfm_nspm_ctx_init();
Sherry Zhnag482b88b2021-08-19 17:51:47 +0800524
Ken Liuacd2a572021-05-12 16:19:04 +0800525 while (1) {
Mingyang Sundeae45d2021-09-06 15:31:07 +0800526 partition = load_a_partition_assuredly(PARTITION_LIST_ADDR);
Shawn Shan23331062021-09-01 14:58:21 +0800527 if (partition == NO_MORE_PARTITION) {
Ken Liuacd2a572021-05-12 16:19:04 +0800528 break;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800529 }
530
Mingyang Sundeae45d2021-09-06 15:31:07 +0800531 p_pldi = partition->p_ldinf;
Mingyang Sun2b352662021-04-21 11:35:43 +0800532
Mingyang Sundeae45d2021-09-06 15:31:07 +0800533 if (p_pldi->nservices) {
534 service_setting = load_services_assuredly(
535 partition,
536 &services_listhead,
537 stateless_services_ref_tbl,
538 sizeof(stateless_services_ref_tbl));
Kevin Peng27e42272021-05-24 17:58:53 +0800539 }
540
Mingyang Sundeae45d2021-09-06 15:31:07 +0800541 if (p_pldi->nirqs) {
Kevin Peng27e42272021-05-24 17:58:53 +0800542 load_irqs_assuredly(partition);
543 }
544
Mingyang Sundeae45d2021-09-06 15:31:07 +0800545 /* Bind the partition with platform. */
Ken Liuce58bfc2021-05-12 17:54:48 +0800546#if TFM_FIH_PROFILE_ON
547 FIH_CALL(tfm_hal_bind_boundaries, fih_rc, partition->p_ldinf,
Mingyang Sundeae45d2021-09-06 15:31:07 +0800548 &partition->p_boundaries);
Ken Liuce58bfc2021-05-12 17:54:48 +0800549 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
550 tfm_core_panic();
Mingyang Sun61f8fbc2021-06-04 17:49:56 +0800551 }
Ken Liu86686282021-04-27 11:11:15 +0800552#else /* TFM_FIH_PROFILE_ON */
Ken Liuce58bfc2021-05-12 17:54:48 +0800553 if (tfm_hal_bind_boundaries(partition->p_ldinf,
Mingyang Sundeae45d2021-09-06 15:31:07 +0800554 &partition->p_boundaries)
555 != TFM_HAL_SUCCESS) {
Ken Liuce58bfc2021-05-12 17:54:48 +0800556 tfm_core_panic();
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100557 }
Ken Liuce58bfc2021-05-12 17:54:48 +0800558#endif /* TFM_FIH_PROFILE_ON */
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100559
Mingyang Sundeae45d2021-09-06 15:31:07 +0800560 backend_instance.comp_init_assuredly(partition, service_setting);
Edison Ai764d41f2018-09-21 15:56:36 +0800561 }
562
Mingyang Sundeae45d2021-09-06 15:31:07 +0800563 return backend_instance.system_run();
Edison Ai764d41f2018-09-21 15:56:36 +0800564}
Ken Liu2d175172019-03-21 17:08:41 +0800565
Ken Liu5d73c872021-08-19 19:23:17 +0800566uint64_t do_schedule(void)
567{
Ken Liu086b32c2021-12-27 14:44:52 +0800568 AAPCS_DUAL_U32_T ctx_ctrls;
Ken Liu5d73c872021-08-19 19:23:17 +0800569 struct partition_t *p_part_curr, *p_part_next;
Kevin Pengb20b0322022-02-09 16:47:19 +0800570 struct context_ctrl_t *p_curr_ctx;
Ken Liu5d73c872021-08-19 19:23:17 +0800571 struct thread_t *pth_next = thrd_next();
Kevin Pengca59ec02021-12-09 14:35:50 +0800572 struct critical_section_t cs = CRITICAL_SECTION_STATIC_INIT;
Ken Liu5d73c872021-08-19 19:23:17 +0800573
Kevin Pengb20b0322022-02-09 16:47:19 +0800574 p_curr_ctx = (struct context_ctrl_t *)(CURRENT_THREAD->p_context_ctrl);
Ken Liu086b32c2021-12-27 14:44:52 +0800575
Kevin Pengb20b0322022-02-09 16:47:19 +0800576 AAPCS_DUAL_U32_SET(ctx_ctrls, (uint32_t)p_curr_ctx, (uint32_t)p_curr_ctx);
577
578 p_part_curr = GET_CURRENT_COMPONENT();
Mingyang Sun4f08f4d2021-09-10 17:41:28 +0800579 p_part_next = GET_THRD_OWNER(pth_next);
Ken Liu5d73c872021-08-19 19:23:17 +0800580
Mingyang Sun620c8562021-11-10 11:44:58 +0800581 if (scheduler_lock != SCHEDULER_LOCKED && pth_next != NULL &&
582 p_part_curr != p_part_next) {
Ken Liu5d73c872021-08-19 19:23:17 +0800583 /* Check if there is enough room on stack to save more context */
Kevin Pengb20b0322022-02-09 16:47:19 +0800584 if ((p_curr_ctx->sp_limit +
585 sizeof(struct tfm_additional_context_t)) > __get_PSP()) {
Ken Liu5d73c872021-08-19 19:23:17 +0800586 tfm_core_panic();
587 }
Mate Toth-Palc430b992019-05-09 21:01:14 +0200588
Kevin Pengca59ec02021-12-09 14:35:50 +0800589 CRITICAL_SECTION_ENTER(cs);
Ken Liuce58bfc2021-05-12 17:54:48 +0800590 /*
591 * If required, let the platform update boundary based on its
592 * implementation. Change privilege, MPU or other configurations.
593 */
594 if (p_part_curr->p_boundaries != p_part_next->p_boundaries) {
595 if (tfm_hal_update_boundaries(p_part_next->p_ldinf,
596 p_part_next->p_boundaries)
597 != TFM_HAL_SUCCESS) {
598 tfm_core_panic();
599 }
600 }
Feder Liang42f5b562021-09-10 17:38:36 +0800601 ARCH_FLUSH_FP_CONTEXT();
Mingyang Sun620c8562021-11-10 11:44:58 +0800602
Ken Liu086b32c2021-12-27 14:44:52 +0800603 AAPCS_DUAL_U32_SET_A1(ctx_ctrls, (uint32_t)pth_next->p_context_ctrl);
604
Mingyang Sun620c8562021-11-10 11:44:58 +0800605 CURRENT_THREAD = pth_next;
Kevin Pengca59ec02021-12-09 14:35:50 +0800606 CRITICAL_SECTION_LEAVE(cs);
Ken Liu2d175172019-03-21 17:08:41 +0800607 }
David Hufb38d562019-09-23 15:58:34 +0800608
Ken Liu086b32c2021-12-27 14:44:52 +0800609 return AAPCS_DUAL_U32_AS_U64(ctx_ctrls);
Ken Liu2d175172019-03-21 17:08:41 +0800610}
Mingyang Sund44522a2020-01-16 16:48:37 +0800611
Ken Liu0bed7e02022-02-10 12:38:07 +0800612void update_caller_outvec_len(struct conn_handle_t *hdl)
Mingyang Sund44522a2020-01-16 16:48:37 +0800613{
614 uint32_t i;
615
616 /*
617 * FixeMe: abstract these part into dedicated functions to avoid
618 * accessing thread context in psa layer
619 */
Ken Liuf39d8eb2021-10-07 12:55:33 +0800620 /*
621 * If it is a NS request via RPC, the owner of this message is not set.
622 * Or if it is a SFN message, it does not have owner thread state either.
623 */
Ken Liu0bed7e02022-02-10 12:38:07 +0800624 if ((!is_tfm_rpc_msg(hdl)) && (hdl->sfn_magic != TFM_MSG_MAGIC_SFN)) {
625 TFM_CORE_ASSERT(hdl->ack_evnt.owner->state == THRD_STATE_BLOCK);
Mingyang Sund44522a2020-01-16 16:48:37 +0800626 }
627
628 for (i = 0; i < PSA_MAX_IOVEC; i++) {
Ken Liu0bed7e02022-02-10 12:38:07 +0800629 if (hdl->msg.out_size[i] == 0) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800630 continue;
631 }
632
Ken Liu0bed7e02022-02-10 12:38:07 +0800633 TFM_CORE_ASSERT(hdl->caller_outvec[i].base == hdl->outvec[i].base);
Mingyang Sund44522a2020-01-16 16:48:37 +0800634
Ken Liu0bed7e02022-02-10 12:38:07 +0800635 hdl->caller_outvec[i].len = hdl->outvec[i].len;
Mingyang Sund44522a2020-01-16 16:48:37 +0800636 }
637}
638
Ken Liu5d73c872021-08-19 19:23:17 +0800639void spm_assert_signal(void *p_pt, psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800640{
Ken Liu92ede9f2021-10-20 09:35:00 +0800641 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Ken Liu5d73c872021-08-19 19:23:17 +0800642 struct partition_t *partition = (struct partition_t *)p_pt;
Mingyang Sund44522a2020-01-16 16:48:37 +0800643
Mingyang Sund44522a2020-01-16 16:48:37 +0800644 if (!partition) {
645 tfm_core_panic();
646 }
647
Ken Liu92ede9f2021-10-20 09:35:00 +0800648 CRITICAL_SECTION_ENTER(cs_assert);
649
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800650 partition->signals_asserted |= signal;
Mingyang Sund44522a2020-01-16 16:48:37 +0800651
Kevin Peng8dac6102021-03-09 16:44:00 +0800652 if (partition->signals_waiting & signal) {
Ken Liu5d73c872021-08-19 19:23:17 +0800653 thrd_wake_up(&partition->waitobj,
654 partition->signals_asserted & partition->signals_waiting);
Kevin Peng8dac6102021-03-09 16:44:00 +0800655 partition->signals_waiting &= ~signal;
656 }
Ken Liu92ede9f2021-10-20 09:35:00 +0800657
658 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sund44522a2020-01-16 16:48:37 +0800659}