blob: 71a4afd1ce5edefe5359454c9f1a91e00424fa26 [file] [log] [blame]
Edison Ai764d41f2018-09-21 15:56:36 +08001/*
Kevin Penga20b5af2021-01-11 11:20:52 +08002 * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
Edison Ai764d41f2018-09-21 15:56:36 +08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
Mingyang Sunda01a972019-07-12 17:32:59 +08007
Edison Ai764d41f2018-09-21 15:56:36 +08008#include <inttypes.h>
9#include <stdbool.h>
Ken Liu24dffb22021-02-10 11:03:58 +080010#include "bitops.h"
David Huf07e97d2021-02-15 22:05:40 +080011#include "fih.h"
Jamie Foxcc31d402019-01-28 17:13:52 +000012#include "psa/client.h"
13#include "psa/service.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080014#include "tfm_thread.h"
Edison Ai764d41f2018-09-21 15:56:36 +080015#include "tfm_wait.h"
Ken Liubcae38b2021-01-20 15:47:44 +080016#include "internal_errors.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080017#include "tfm_spm_hal.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080018#include "tfm_api.h"
19#include "tfm_secure_api.h"
20#include "tfm_memory_utils.h"
Mingyang Sund1ed6732020-08-26 15:52:21 +080021#include "tfm_hal_defs.h"
22#include "tfm_hal_isolation.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080023#include "spm_ipc.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080024#include "tfm_peripherals_def.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080025#include "tfm_core_utils.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080026#include "tfm_rpc.h"
Mingyang Sund44522a2020-01-16 16:48:37 +080027#include "tfm_core_trustzone.h"
Ken Liu24dffb22021-02-10 11:03:58 +080028#include "lists.h"
Edison Ai764d41f2018-09-21 15:56:36 +080029#include "tfm_pools.h"
Mingyang Sun22a3faf2021-07-09 15:32:47 +080030#include "region.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080031#include "psa_manifest/pid.h"
Mingyang Sun00df2352021-04-15 15:46:08 +080032#include "load/partition_defs.h"
33#include "load/service_defs.h"
Ken Liu86686282021-04-27 11:11:15 +080034#include "load/asset_defs.h"
Ken Liuacd2a572021-05-12 16:19:04 +080035#include "load/spm_load_api.h"
Kevin Peng27e42272021-05-24 17:58:53 +080036#include "load/irq_defs.h"
Edison Ai764d41f2018-09-21 15:56:36 +080037
Ken Liuea45b0d2021-05-22 17:41:25 +080038/* Partition and service runtime data list head/runtime data table */
39static struct partition_head_t partitions_listhead;
40static struct service_head_t services_listhead;
Ken Liub3b2cb62021-05-22 00:39:28 +080041struct service_t *stateless_services_ref_tbl[STATIC_HANDLE_NUM_LIMIT];
Summer Qind99509f2019-08-02 17:36:58 +080042
Edison Ai764d41f2018-09-21 15:56:36 +080043/* Pools */
44TFM_POOL_DECLARE(conn_handle_pool, sizeof(struct tfm_conn_handle_t),
45 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +080046
Mingyang Sun22a3faf2021-07-09 15:32:47 +080047/* The veneer section names come from the scatter file */
48REGION_DECLARE(Image$$, TFM_UNPRIV_CODE, $$RO$$Base);
49REGION_DECLARE(Image$$, TFM_UNPRIV_CODE, $$RO$$Limit);
50
Kevin Pengeca45b92021-02-09 14:46:50 +080051void spm_interrupt_handler(struct partition_load_info_t *p_ldinf,
52 psa_signal_t signal,
53 uint32_t irq_line,
54 psa_flih_func flih_func);
Mingyang Sund44522a2020-01-16 16:48:37 +080055
56#include "tfm_secure_irq_handlers_ipc.inc"
Edison Ai764d41f2018-09-21 15:56:36 +080057
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
Edison Ai9cc26242019-08-06 11:28:04 +0800153 p_handle->service = service;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800154 p_handle->status = TFM_HANDLE_STATUS_IDLE;
Summer Qin1ce712a2019-10-14 18:04:05 +0800155 p_handle->client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800156
157 /* Add handle node to list for next psa functions */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800158 BI_LIST_INSERT_BEFORE(&service->handle_list, &p_handle->list);
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
Summer Qin630c76b2020-05-20 10:32:58 +0800163int32_t tfm_spm_validate_conn_handle(
164 const struct tfm_conn_handle_t *conn_handle,
165 int32_t client_id)
Summer Qin1ce712a2019-10-14 18:04:05 +0800166{
167 /* Check the handle address is validated */
168 if (is_valid_chunk_data_in_pool(conn_handle_pool,
169 (uint8_t *)conn_handle) != true) {
Ken Liubcae38b2021-01-20 15:47:44 +0800170 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800171 }
172
173 /* Check the handle caller is correct */
Summer Qin630c76b2020-05-20 10:32:58 +0800174 if (conn_handle->client_id != client_id) {
Ken Liubcae38b2021-01-20 15:47:44 +0800175 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800176 }
177
Ken Liubcae38b2021-01-20 15:47:44 +0800178 return SPM_SUCCESS;
Summer Qin1ce712a2019-10-14 18:04:05 +0800179}
180
Mingyang Sun783a59b2021-04-20 15:52:18 +0800181int32_t tfm_spm_free_conn_handle(struct service_t *service,
Summer Qin02f7f072020-08-24 16:02:54 +0800182 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800183{
Ken Liuf250b8b2019-12-27 16:31:24 +0800184 TFM_CORE_ASSERT(service);
Summer Qin630c76b2020-05-20 10:32:58 +0800185 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800186
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200187 /* Clear magic as the handler is not used anymore */
Summer Qin630c76b2020-05-20 10:32:58 +0800188 conn_handle->internal_msg.magic = 0;
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200189
Edison Ai764d41f2018-09-21 15:56:36 +0800190 /* Remove node from handle list */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800191 BI_LIST_REMOVE_NODE(&conn_handle->list);
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);
Ken Liubcae38b2021-01-20 15:47:44 +0800195 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800196}
197
Mingyang Sun783a59b2021-04-20 15:52:18 +0800198int32_t tfm_spm_set_rhandle(struct service_t *service,
Summer Qin02f7f072020-08-24 16:02:54 +0800199 struct tfm_conn_handle_t *conn_handle,
200 void *rhandle)
Edison Ai764d41f2018-09-21 15:56:36 +0800201{
Ken Liuf250b8b2019-12-27 16:31:24 +0800202 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800203 /* Set reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800204 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800205
Summer Qin630c76b2020-05-20 10:32:58 +0800206 conn_handle->rhandle = rhandle;
Ken Liubcae38b2021-01-20 15:47:44 +0800207 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800208}
209
Mingyang Sund44522a2020-01-16 16:48:37 +0800210/**
Xinyu Zhang3a453242021-04-16 17:57:09 +0800211 * \brief Get reverse handle value from connection handle.
Mingyang Sund44522a2020-01-16 16:48:37 +0800212 *
213 * \param[in] service Target service context pointer
214 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800215 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800216 *
217 * \retval void * Success
218 * \retval "Does not return" Panic for those:
219 * service pointer are NULL
Xinyu Zhang3a453242021-04-16 17:57:09 +0800220 * handle is \ref PSA_NULL_HANDLE
Mingyang Sund44522a2020-01-16 16:48:37 +0800221 * handle node does not be found
222 */
Mingyang Sun783a59b2021-04-20 15:52:18 +0800223static void *tfm_spm_get_rhandle(struct service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800224 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800225{
Ken Liuf250b8b2019-12-27 16:31:24 +0800226 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800227 /* Get reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800228 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800229
Summer Qin630c76b2020-05-20 10:32:58 +0800230 return conn_handle->rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800231}
232
233/* Partition management functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800234
Summer Qin02f7f072020-08-24 16:02:54 +0800235struct tfm_msg_body_t *tfm_spm_get_msg_by_signal(struct partition_t *partition,
236 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800237{
Ken Liu2c47f7f2021-01-22 11:06:04 +0800238 struct bi_list_node_t *node, *head;
Mingyang Sun73056b62020-07-03 15:18:46 +0800239 struct tfm_msg_body_t *tmp_msg, *msg = NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800240
Ken Liuf250b8b2019-12-27 16:31:24 +0800241 TFM_CORE_ASSERT(partition);
Edison Ai764d41f2018-09-21 15:56:36 +0800242
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800243 head = &partition->msg_list;
Mingyang Sun73056b62020-07-03 15:18:46 +0800244
Ken Liu2c47f7f2021-01-22 11:06:04 +0800245 if (BI_LIST_IS_EMPTY(head)) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800246 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800247 }
248
Mingyang Sun73056b62020-07-03 15:18:46 +0800249 /*
250 * There may be multiple messages for this RoT Service signal, do not clear
251 * partition mask until no remaining message. Search may be optimized.
252 */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800253 BI_LIST_FOR_EACH(node, head) {
Ken Liuc25558c2021-05-20 15:31:28 +0800254 tmp_msg = TO_CONTAINER(node, struct tfm_msg_body_t, msg_node);
Ken Liuacd2a572021-05-12 16:19:04 +0800255 if (tmp_msg->service->p_ldinf->signal == signal && msg) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800256 return msg;
Ken Liuacd2a572021-05-12 16:19:04 +0800257 } else if (tmp_msg->service->p_ldinf->signal == signal) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800258 msg = tmp_msg;
Ken Liu2c47f7f2021-01-22 11:06:04 +0800259 BI_LIST_REMOVE_NODE(node);
Edison Ai764d41f2018-09-21 15:56:36 +0800260 }
261 }
Mingyang Sun73056b62020-07-03 15:18:46 +0800262
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800263 partition->signals_asserted &= ~signal;
Mingyang Sun73056b62020-07-03 15:18:46 +0800264
265 return msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800266}
267
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800268#if TFM_LVL != 1
269/**
270 * \brief Change the privilege mode for partition thread mode.
271 *
272 * \param[in] privileged Privileged mode,
273 * \ref TFM_PARTITION_PRIVILEGED_MODE
274 * and \ref TFM_PARTITION_UNPRIVILEGED_MODE
275 *
276 * \note Barrier instructions are not called by this function, and if
277 * it is called in thread mode, it might be necessary to call
278 * them after this function returns.
279 */
280static void tfm_spm_partition_change_privilege(uint32_t privileged)
281{
282 CONTROL_Type ctrl;
283
284 ctrl.w = __get_CONTROL();
285
286 if (privileged == TFM_PARTITION_PRIVILEGED_MODE) {
287 ctrl.b.nPRIV = 0;
288 } else {
289 ctrl.b.nPRIV = 1;
290 }
291
292 __set_CONTROL(ctrl.w);
293}
294#endif /* if(TFM_LVL != 1) */
295
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800296uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
297{
Kevin Pengbf9a97e2021-06-18 16:34:12 +0800298#if TFM_LVL == 1
299 return TFM_PARTITION_PRIVILEGED_MODE;
300#else /* TFM_LVL == 1 */
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800301 if (partition_flags & SPM_PART_FLAG_PSA_ROT) {
302 return TFM_PARTITION_PRIVILEGED_MODE;
303 } else {
304 return TFM_PARTITION_UNPRIVILEGED_MODE;
305 }
Kevin Pengbf9a97e2021-06-18 16:34:12 +0800306#endif /* TFM_LVL == 1 */
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800307}
308
Mingyang Sun783a59b2021-04-20 15:52:18 +0800309struct service_t *tfm_spm_get_service_by_sid(uint32_t sid)
Edison Ai764d41f2018-09-21 15:56:36 +0800310{
Ken Liuea45b0d2021-05-22 17:41:25 +0800311 struct service_t *p_serv;
Edison Ai764d41f2018-09-21 15:56:36 +0800312
Ken Liuea45b0d2021-05-22 17:41:25 +0800313 UNI_LIST_FOR_EACH(p_serv, &services_listhead) {
314 if (p_serv->p_ldinf->sid == sid) {
315 return p_serv;
Mingyang Sunef42f442021-06-11 15:07:58 +0800316 }
317 }
318
Ken Liuea45b0d2021-05-22 17:41:25 +0800319 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800320}
321
Mingyang Sund44522a2020-01-16 16:48:37 +0800322/**
323 * \brief Get the partition context by partition ID.
324 *
325 * \param[in] partition_id Partition identity
326 *
327 * \retval NULL Failed
328 * \retval "Not NULL" Target partition context pointer,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800329 * \ref partition_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800330 */
Kevin Pengeca45b92021-02-09 14:46:50 +0800331struct partition_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800332{
Ken Liuea45b0d2021-05-22 17:41:25 +0800333 struct partition_t *p_part;
Edison Ai764d41f2018-09-21 15:56:36 +0800334
Ken Liuea45b0d2021-05-22 17:41:25 +0800335 UNI_LIST_FOR_EACH(p_part, &partitions_listhead) {
336 if (p_part->p_ldinf->pid == partition_id) {
337 return p_part;
338 }
Edison Ai764d41f2018-09-21 15:56:36 +0800339 }
Ken Liuea45b0d2021-05-22 17:41:25 +0800340
Edison Ai764d41f2018-09-21 15:56:36 +0800341 return NULL;
342}
343
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800344struct partition_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800345{
Kevin Peng82fbca52021-03-09 13:48:48 +0800346 struct tfm_core_thread_t *pth = tfm_core_thrd_get_curr();
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800347 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800348
Ken Liuc25558c2021-05-20 15:31:28 +0800349 partition = TO_CONTAINER(pth, struct partition_t, sp_thread);
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800350
Kevin Peng79c2bda2020-07-24 16:31:12 +0800351 return partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800352}
353
Mingyang Sun783a59b2021-04-20 15:52:18 +0800354int32_t tfm_spm_check_client_version(struct service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530355 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800356{
Ken Liuf250b8b2019-12-27 16:31:24 +0800357 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800358
Ken Liuacd2a572021-05-12 16:19:04 +0800359 switch (SERVICE_GET_VERSION_POLICY(service->p_ldinf->flags)) {
Ken Liub3b2cb62021-05-22 00:39:28 +0800360 case SERVICE_VERSION_POLICY_RELAXED:
Ken Liuacd2a572021-05-12 16:19:04 +0800361 if (version > service->p_ldinf->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800362 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800363 }
364 break;
Ken Liub3b2cb62021-05-22 00:39:28 +0800365 case SERVICE_VERSION_POLICY_STRICT:
Ken Liuacd2a572021-05-12 16:19:04 +0800366 if (version != service->p_ldinf->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800367 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800368 }
369 break;
370 default:
Ken Liubcae38b2021-01-20 15:47:44 +0800371 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800372 }
Ken Liubcae38b2021-01-20 15:47:44 +0800373 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800374}
375
Edison Aie728fbf2019-11-13 09:37:12 +0800376int32_t tfm_spm_check_authorization(uint32_t sid,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800377 struct service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800378 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800379{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800380 struct partition_t *partition = NULL;
Mingyang Sun2b352662021-04-21 11:35:43 +0800381 uint32_t *dep;
Edison Aie728fbf2019-11-13 09:37:12 +0800382 int32_t i;
383
Ken Liuf250b8b2019-12-27 16:31:24 +0800384 TFM_CORE_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800385
386 if (ns_caller) {
Ken Liuacd2a572021-05-12 16:19:04 +0800387 if (!SERVICE_IS_NS_ACCESSIBLE(service->p_ldinf->flags)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800388 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800389 }
390 } else {
391 partition = tfm_spm_get_running_partition();
392 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800393 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800394 }
395
Ken Liuacd2a572021-05-12 16:19:04 +0800396 dep = (uint32_t *)LOAD_INFO_DEPS(partition->p_ldinf);
397 for (i = 0; i < partition->p_ldinf->ndeps; i++) {
Mingyang Sun2b352662021-04-21 11:35:43 +0800398 if (dep[i] == sid) {
Edison Aie728fbf2019-11-13 09:37:12 +0800399 break;
400 }
401 }
402
Ken Liuacd2a572021-05-12 16:19:04 +0800403 if (i == partition->p_ldinf->ndeps) {
Ken Liubcae38b2021-01-20 15:47:44 +0800404 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800405 }
406 }
Ken Liubcae38b2021-01-20 15:47:44 +0800407 return SPM_SUCCESS;
Edison Aie728fbf2019-11-13 09:37:12 +0800408}
409
Edison Ai764d41f2018-09-21 15:56:36 +0800410/* Message functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800411
Summer Qin02f7f072020-08-24 16:02:54 +0800412struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800413{
414 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200415 * The message handler passed by the caller is considered invalid in the
416 * following cases:
417 * 1. Not a valid message handle. (The address of a message is not the
418 * address of a possible handle from the pool
419 * 2. Handle not belongs to the caller partition (The handle is either
420 * unused, or owned by anither partition)
421 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800422 */
Ken Liu505b1702020-05-29 13:19:58 +0800423 struct tfm_msg_body_t *p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800424 uint32_t partition_id;
Ken Liu505b1702020-05-29 13:19:58 +0800425 struct tfm_conn_handle_t *p_conn_handle =
426 tfm_spm_to_handle_instance(msg_handle);
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200427
428 if (is_valid_chunk_data_in_pool(
Ken Liu505b1702020-05-29 13:19:58 +0800429 conn_handle_pool, (uint8_t *)p_conn_handle) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800430 return NULL;
431 }
432
Ken Liu505b1702020-05-29 13:19:58 +0800433 p_msg = &p_conn_handle->internal_msg;
434
Edison Ai764d41f2018-09-21 15:56:36 +0800435 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200436 * Check that the magic number is correct. This proves that the message
437 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800438 */
Ken Liu505b1702020-05-29 13:19:58 +0800439 if (p_msg->magic != TFM_MSG_MAGIC) {
Edison Ai764d41f2018-09-21 15:56:36 +0800440 return NULL;
441 }
442
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200443 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800444 partition_id = tfm_spm_partition_get_running_partition_id();
Ken Liuacd2a572021-05-12 16:19:04 +0800445 if (partition_id != p_msg->service->partition->p_ldinf->pid) {
Edison Ai764d41f2018-09-21 15:56:36 +0800446 return NULL;
447 }
448
Ken Liu505b1702020-05-29 13:19:58 +0800449 return p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800450}
451
Kevin Pengdf6aa292021-03-11 17:58:50 +0800452struct tfm_msg_body_t *
453 tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle)
454{
455 TFM_CORE_ASSERT(conn_handle != NULL);
456
457 return &(conn_handle->internal_msg);
458}
459
Edison Ai97115822019-08-01 14:22:19 +0800460void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800461 struct service_t *service,
Ken Liu505b1702020-05-29 13:19:58 +0800462 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800463 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800464 psa_invec *invec, size_t in_len,
465 psa_outvec *outvec, size_t out_len,
466 psa_outvec *caller_outvec)
467{
Edison Ai764d41f2018-09-21 15:56:36 +0800468 uint32_t i;
Ken Liu505b1702020-05-29 13:19:58 +0800469 struct tfm_conn_handle_t *conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800470
Ken Liuf250b8b2019-12-27 16:31:24 +0800471 TFM_CORE_ASSERT(msg);
472 TFM_CORE_ASSERT(service);
473 TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
474 TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
475 TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
476 TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
477 TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800478
Edison Ai764d41f2018-09-21 15:56:36 +0800479 /* Clear message buffer before using it */
Summer Qinf24dbb52020-07-23 14:53:54 +0800480 spm_memset(msg, 0, sizeof(struct tfm_msg_body_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800481
Ken Liu35f89392019-03-14 14:51:05 +0800482 tfm_event_init(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800483 msg->magic = TFM_MSG_MAGIC;
484 msg->service = service;
Edison Ai764d41f2018-09-21 15:56:36 +0800485 msg->caller_outvec = caller_outvec;
Summer Qin1ce712a2019-10-14 18:04:05 +0800486 msg->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800487
488 /* Copy contents */
489 msg->msg.type = type;
490
491 for (i = 0; i < in_len; i++) {
492 msg->msg.in_size[i] = invec[i].len;
493 msg->invec[i].base = invec[i].base;
494 }
495
496 for (i = 0; i < out_len; i++) {
497 msg->msg.out_size[i] = outvec[i].len;
498 msg->outvec[i].base = outvec[i].base;
499 /* Out len is used to record the writed number, set 0 here again */
500 msg->outvec[i].len = 0;
501 }
502
Ken Liu505b1702020-05-29 13:19:58 +0800503 /* Use the user connect handle as the message handle */
504 msg->msg.handle = handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800505
Ken Liu505b1702020-05-29 13:19:58 +0800506 conn_handle = tfm_spm_to_handle_instance(handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800507 /* For connected handle, set rhandle to every message */
Ken Liu505b1702020-05-29 13:19:58 +0800508 if (conn_handle) {
509 msg->msg.rhandle = tfm_spm_get_rhandle(service, conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800510 }
David Hu46603dd2019-12-11 18:05:16 +0800511
512 /* Set the private data of NSPE client caller in multi-core topology */
513 if (TFM_CLIENT_ID_IS_NS(client_id)) {
514 tfm_rpc_set_caller_data(msg, client_id);
515 }
Edison Ai764d41f2018-09-21 15:56:36 +0800516}
517
Mingyang Sun783a59b2021-04-20 15:52:18 +0800518void tfm_spm_send_event(struct service_t *service,
Kevin Peng8dac6102021-03-09 16:44:00 +0800519 struct tfm_msg_body_t *msg)
Edison Ai764d41f2018-09-21 15:56:36 +0800520{
Kevin Peng8dac6102021-03-09 16:44:00 +0800521 struct partition_t *partition = NULL;
522 psa_signal_t signal = 0;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800523
Ken Liuacd2a572021-05-12 16:19:04 +0800524 if (!msg || !service || !service->p_ldinf || !service->partition) {
Kevin Peng8dac6102021-03-09 16:44:00 +0800525 tfm_core_panic();
526 }
527
528 partition = service->partition;
Ken Liuacd2a572021-05-12 16:19:04 +0800529 signal = service->p_ldinf->signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800530
Mingyang Sun73056b62020-07-03 15:18:46 +0800531 /* Add message to partition message list tail */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800532 BI_LIST_INSERT_BEFORE(&partition->msg_list, &msg->msg_node);
Edison Ai764d41f2018-09-21 15:56:36 +0800533
534 /* Messages put. Update signals */
Kevin Peng8dac6102021-03-09 16:44:00 +0800535 partition->signals_asserted |= signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800536
Kevin Peng8dac6102021-03-09 16:44:00 +0800537 if (partition->signals_waiting & signal) {
538 tfm_event_wake(
539 &partition->event,
540 (partition->signals_asserted & partition->signals_waiting));
541 partition->signals_waiting &= ~signal;
542 }
Edison Ai764d41f2018-09-21 15:56:36 +0800543
David Hufb38d562019-09-23 15:58:34 +0800544 /*
545 * If it is a NS request via RPC, it is unnecessary to block current
546 * thread.
547 */
548 if (!is_tfm_rpc_msg(msg)) {
549 tfm_event_wait(&msg->ack_evnt);
550 }
Edison Ai764d41f2018-09-21 15:56:36 +0800551}
552
Mingyang Sunf3d29892019-07-10 17:50:23 +0800553uint32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800554{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800555 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800556
Kevin Peng79c2bda2020-07-24 16:31:12 +0800557 partition = tfm_spm_get_running_partition();
Ken Liuacd2a572021-05-12 16:19:04 +0800558 if (partition && partition->p_ldinf) {
559 return partition->p_ldinf->pid;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800560 } else {
561 return INVALID_PARTITION_ID;
562 }
Edison Ai764d41f2018-09-21 15:56:36 +0800563}
564
Summer Qin43c185d2019-10-10 15:44:42 +0800565int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800566 enum tfm_memory_access_e access,
567 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800568{
Mingyang Sund1ed6732020-08-26 15:52:21 +0800569 enum tfm_hal_status_t err;
570 uint32_t attr = 0;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800571
572 /* If len is zero, this indicates an empty buffer and base is ignored */
573 if (len == 0) {
Ken Liubcae38b2021-01-20 15:47:44 +0800574 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800575 }
576
577 if (!buffer) {
Ken Liubcae38b2021-01-20 15:47:44 +0800578 return SPM_ERROR_BAD_PARAMETERS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800579 }
580
581 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800582 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800583 }
584
Summer Qin424d4db2019-03-25 14:09:51 +0800585 if (access == TFM_MEMORY_ACCESS_RW) {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800586 attr |= (TFM_HAL_ACCESS_READABLE | TFM_HAL_ACCESS_WRITABLE);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800587 } else {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800588 attr |= TFM_HAL_ACCESS_READABLE;
Summer Qin424d4db2019-03-25 14:09:51 +0800589 }
Mingyang Sund1ed6732020-08-26 15:52:21 +0800590
591 if (privileged == TFM_PARTITION_UNPRIVILEGED_MODE) {
592 attr |= TFM_HAL_ACCESS_UNPRIVILEGED;
593 } else {
594 attr &= ~TFM_HAL_ACCESS_UNPRIVILEGED;
595 }
596
597 if (ns_caller) {
598 attr |= TFM_HAL_ACCESS_NS;
599 }
600
601 err = tfm_hal_memory_has_access((uintptr_t)buffer, len, attr);
602
603 if (err == TFM_HAL_SUCCESS) {
Ken Liubcae38b2021-01-20 15:47:44 +0800604 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800605 }
606
Ken Liubcae38b2021-01-20 15:47:44 +0800607 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800608}
609
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800610bool tfm_spm_is_ns_caller(void)
611{
612#if defined(TFM_MULTI_CORE_TOPOLOGY) || defined(FORWARD_PROT_MSG)
613 /* Multi-core NS PSA API request is processed by pendSV. */
614 return (__get_active_exc_num() == EXC_NUM_PENDSV);
615#else
616 struct partition_t *partition = tfm_spm_get_running_partition();
Mingyang Sune529e3b2021-07-12 14:46:30 +0800617
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800618 if (!partition) {
619 tfm_core_panic();
620 }
621
622 return (partition->p_ldinf->pid == TFM_SP_NON_SECURE_ID);
623#endif
624}
625
Mingyang Sune529e3b2021-07-12 14:46:30 +0800626uint32_t tfm_spm_get_caller_privilege_mode(void)
627{
628 struct partition_t *partition;
629
630#if defined(TFM_MULTI_CORE_TOPOLOGY) || defined(FORWARD_PROT_MSG)
631 /*
632 * In multi-core topology, if PSA request is from mailbox, the client
633 * is unprivileged.
634 */
635 if (__get_active_exc_num() == EXC_NUM_PENDSV) {
636 return TFM_PARTITION_UNPRIVILEGED_MODE;
637 }
638#endif
639 partition = tfm_spm_get_running_partition();
640 if (!partition) {
641 tfm_core_panic();
642 }
643
644 return tfm_spm_partition_get_privileged_mode(partition->p_ldinf->flags);
645}
646
Ken Liuce2692d2020-02-11 12:39:36 +0800647uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800648{
Kevin Peng54d47fb2021-06-15 16:40:08 +0800649 uint32_t i;
Mingyang Sun61f8fbc2021-06-04 17:49:56 +0800650 bool privileged;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800651 struct partition_t *partition;
Summer Qin66f1e032020-01-06 15:40:03 +0800652 struct tfm_core_thread_t *pth, *p_ns_entry_thread = NULL;
Ken Liu86686282021-04-27 11:11:15 +0800653 const struct platform_data_t *platform_data_p;
Mingyang Sun8d004f72021-06-01 10:46:26 +0800654 const struct partition_load_info_t *p_ldinf;
Ken Liu4520ce32021-05-11 22:49:10 +0800655 struct asset_desc_t *p_asset_load;
David Huf07e97d2021-02-15 22:05:40 +0800656#ifdef TFM_FIH_PROFILE_ON
657 fih_int fih_rc = FIH_FAILURE;
658#endif
Edison Ai764d41f2018-09-21 15:56:36 +0800659
660 tfm_pool_init(conn_handle_pool,
661 POOL_BUFFER_SIZE(conn_handle_pool),
662 sizeof(struct tfm_conn_handle_t),
663 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800664
Ken Liuea45b0d2021-05-22 17:41:25 +0800665 UNI_LISI_INIT_HEAD(&partitions_listhead);
666 UNI_LISI_INIT_HEAD(&services_listhead);
667
Ken Liuacd2a572021-05-12 16:19:04 +0800668 while (1) {
Ken Liuea45b0d2021-05-22 17:41:25 +0800669 partition = load_a_partition_assuredly(&partitions_listhead);
Ken Liuacd2a572021-05-12 16:19:04 +0800670 if (partition == NULL) {
671 break;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800672 }
673
Mingyang Sun8d004f72021-06-01 10:46:26 +0800674 p_ldinf = partition->p_ldinf;
Mingyang Sun2b352662021-04-21 11:35:43 +0800675
Kevin Peng27e42272021-05-24 17:58:53 +0800676 if (p_ldinf->nservices) {
Ken Liuea45b0d2021-05-22 17:41:25 +0800677 load_services_assuredly(partition, &services_listhead,
Kevin Peng27e42272021-05-24 17:58:53 +0800678 stateless_services_ref_tbl,
679 sizeof(stateless_services_ref_tbl));
680 }
681
682 if (p_ldinf->nirqs) {
683 load_irqs_assuredly(partition);
684 }
685
Ken Liuacd2a572021-05-12 16:19:04 +0800686 /* Init mmio assets */
Mingyang Sun8d004f72021-06-01 10:46:26 +0800687 if (p_ldinf->nassets > 0) {
688 if (tfm_spm_partition_get_privileged_mode(p_ldinf->flags) ==
Mingyang Sun61f8fbc2021-06-04 17:49:56 +0800689 TFM_PARTITION_PRIVILEGED_MODE) {
690 privileged = true;
691 } else {
692 privileged = false;
693 }
694 }
695
Mingyang Sun8d004f72021-06-01 10:46:26 +0800696 p_asset_load = (struct asset_desc_t *)LOAD_INFO_ASSET(p_ldinf);
697 for (i = 0; i < p_ldinf->nassets; i++) {
Ken Liu86686282021-04-27 11:11:15 +0800698 /* Skip the memory-based asset */
Ken Liu4520ce32021-05-11 22:49:10 +0800699 if (!(p_asset_load[i].attr & ASSET_DEV_REF_BIT)) {
Ken Liu86686282021-04-27 11:11:15 +0800700 continue;
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100701 }
Ken Liu86686282021-04-27 11:11:15 +0800702
Ken Liuacd2a572021-05-12 16:19:04 +0800703 platform_data_p = REFERENCE_TO_PTR(p_asset_load[i].dev.addr_ref,
704 struct platform_data_t *);
Ken Liu25e09c72021-05-24 15:46:46 +0800705
706 /*
707 * TODO: some partitions declare MMIO not exist on specific
708 * platforms, and the platform defines a dummy NULL reference
709 * for these MMIO items, which cause 'nassets' to contain several
710 * NULL items. Skip these NULL items initialization temporarily to
711 * avoid HAL API panic.
712 * Eventually, these platform-specific partitions need to be moved
713 * into a platform-specific folder. Then this workaround can be
714 * removed.
715 */
716 if (!platform_data_p) {
717 continue;
718 }
719
Ken Liu86686282021-04-27 11:11:15 +0800720#ifdef TFM_FIH_PROFILE_ON
Mingyang Sun61f8fbc2021-06-04 17:49:56 +0800721 FIH_CALL(tfm_spm_hal_configure_default_isolation, fih_rc,
722 privileged, platform_data_p);
Ken Liu86686282021-04-27 11:11:15 +0800723 if (fih_not_eq(fih_rc, fih_int_encode(TFM_PLAT_ERR_SUCCESS))) {
724 tfm_core_panic();
725 }
726#else /* TFM_FIH_PROFILE_ON */
Mingyang Sun61f8fbc2021-06-04 17:49:56 +0800727 if (tfm_spm_hal_configure_default_isolation(privileged,
Ken Liu86686282021-04-27 11:11:15 +0800728 platform_data_p) != TFM_PLAT_ERR_SUCCESS) {
729 tfm_core_panic();
730 }
731#endif /* TFM_FIH_PROFILE_ON */
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100732 }
733
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800734 partition->signals_allowed |= PSA_DOORBELL;
Shawn Shanc7dda0e2019-12-23 14:45:09 +0800735
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800736 tfm_event_init(&partition->event);
Ken Liu2c47f7f2021-01-22 11:06:04 +0800737 BI_LIST_INIT_NODE(&partition->msg_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800738
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800739 pth = &partition->sp_thread;
Edison Ai764d41f2018-09-21 15:56:36 +0800740 if (!pth) {
Edison Ai9059ea02019-11-28 13:46:14 +0800741 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800742 }
743
Mingyang Sun8d004f72021-06-01 10:46:26 +0800744 /* Extendable partition load info is right after p_ldinf. */
Mingyang Sun2b352662021-04-21 11:35:43 +0800745 tfm_core_thrd_init(
746 pth,
Mingyang Sun8d004f72021-06-01 10:46:26 +0800747 POSITION_TO_ENTRY(p_ldinf->entry, tfm_core_thrd_entry_t),
Mingyang Sun2b352662021-04-21 11:35:43 +0800748 NULL,
Mingyang Sun8d004f72021-06-01 10:46:26 +0800749 LOAD_ALLOCED_STACK_ADDR(p_ldinf) + p_ldinf->stack_size,
750 LOAD_ALLOCED_STACK_ADDR(p_ldinf));
Edison Ai788bae22019-02-18 17:38:59 +0800751
Mingyang Sun8d004f72021-06-01 10:46:26 +0800752 pth->prior = TO_THREAD_PRIORITY(PARTITION_PRIORITY(p_ldinf->flags));
Edison Ai764d41f2018-09-21 15:56:36 +0800753
Mingyang Sun8d004f72021-06-01 10:46:26 +0800754 if (p_ldinf->pid == TFM_SP_NON_SECURE_ID) {
Ken Liu490281d2019-12-30 15:55:26 +0800755 p_ns_entry_thread = pth;
Ken Liu5248af22019-12-29 12:47:13 +0800756 pth->param = (void *)tfm_spm_hal_get_ns_entry_point();
Ken Liu490281d2019-12-30 15:55:26 +0800757 }
758
Edison Ai764d41f2018-09-21 15:56:36 +0800759 /* Kick off */
Summer Qin66f1e032020-01-06 15:40:03 +0800760 if (tfm_core_thrd_start(pth) != THRD_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800761 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800762 }
Edison Ai764d41f2018-09-21 15:56:36 +0800763 }
764
Ken Liu483f5da2019-04-24 10:45:21 +0800765 /*
766 * All threads initialized, start the scheduler.
767 *
768 * NOTE:
Ken Liu490281d2019-12-30 15:55:26 +0800769 * It is worthy to give the thread object to scheduler if the background
770 * context belongs to one of the threads. Here the background thread is the
771 * initialization thread who calls SPM SVC, which re-uses the non-secure
772 * entry thread's stack. After SPM initialization is done, this stack is
773 * cleaned up and the background context is never going to return. Tell
774 * the scheduler that the current thread is non-secure entry thread.
Ken Liu483f5da2019-04-24 10:45:21 +0800775 */
Summer Qin66f1e032020-01-06 15:40:03 +0800776 tfm_core_thrd_start_scheduler(p_ns_entry_thread);
Ken Liuce2692d2020-02-11 12:39:36 +0800777
Summer Qind2ad7e72020-01-06 18:16:35 +0800778 return p_ns_entry_thread->arch_ctx.lr;
Edison Ai764d41f2018-09-21 15:56:36 +0800779}
Ken Liu2d175172019-03-21 17:08:41 +0800780
Kevin Peng25b190b2020-10-30 17:10:45 +0800781#if TFM_LVL != 1
Kevin Peng64011942021-05-25 11:19:07 +0800782static void set_up_boundary(const struct partition_load_info_t *p_ldinf)
783{
784#if TFM_LVL == 3
David Huf07e97d2021-02-15 22:05:40 +0800785#if defined(TFM_FIH_PROFILE_ON) && (TFM_LVL == 3)
786 fih_int fih_rc = FIH_FAILURE;
787#endif
Kevin Peng64011942021-05-25 11:19:07 +0800788 /*
789 * FIXME: To implement isolations among partitions in isolation level 3,
790 * each partition needs to run in unprivileged mode. Currently some
791 * PRoTs cannot work in unprivileged mode, make them privileged now.
792 */
793 if (!(p_ldinf->flags & SPM_PART_FLAG_PSA_ROT)) {
794 struct asset_desc_t *p_asset =
795 (struct asset_desc_t *)LOAD_INFO_ASSET(p_ldinf);
796 /* Partition must have private data as the first asset in LVL3 */
797 if (p_ldinf->nassets == 0) {
798 tfm_core_panic();
799 }
800 if (p_asset->attr & ASSET_DEV_REF_BIT) {
801 tfm_core_panic();
802 }
803 /* FIXME: only MPU-based implementations are supported currently */
804#ifdef TFM_FIH_PROFILE_ON
805 FIH_CALL(tfm_hal_mpu_update_partition_boundary, fih_rc,
806 p_asset->mem.addr_x, p_asset->mem.addr_y);
807 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
808 tfm_core_panic();
809 }
810#else /* TFM_FIH_PROFILE_ON */
811 if (tfm_hal_mpu_update_partition_boundary(p_asset->mem.addr_x,
812 p_asset->mem.addr_y)
813 != TFM_HAL_SUCCESS) {
814 tfm_core_panic();
815 }
816#endif /* TFM_FIH_PROFILE_ON */
817 }
818#else /* TFM_LVL == 3 */
819 (void)p_ldinf;
820#endif /* TFM_LVL == 3 */
821}
822#endif /* TFM_LVL != 1 */
823
824void tfm_set_up_isolation_boundary(const struct partition_t *partition)
825{
826#if TFM_LVL != 1
827 const struct partition_load_info_t *p_ldinf;
828 uint32_t is_privileged;
829
830 p_ldinf = partition->p_ldinf;
831 is_privileged = p_ldinf->flags & SPM_PART_FLAG_PSA_ROT ?
832 TFM_PARTITION_PRIVILEGED_MODE :
833 TFM_PARTITION_UNPRIVILEGED_MODE;
834
835 tfm_spm_partition_change_privilege(is_privileged);
836
837 set_up_boundary(p_ldinf);
838#else /* TFM_LVL != 1 */
839 (void)partition;
840#endif /* TFM_LVL != 1 */
841}
842
843void tfm_pendsv_do_schedule(struct tfm_arch_ctx_t *p_actx)
844{
845 struct partition_t *p_next_partition;
846 struct tfm_core_thread_t *pth_next = tfm_core_thrd_get_next();
847 struct tfm_core_thread_t *pth_curr = tfm_core_thrd_get_curr();
Ken Liu2d175172019-03-21 17:08:41 +0800848
Mate Toth-Pal32b2ccd2019-04-26 10:00:16 +0200849 if (pth_next != NULL && pth_curr != pth_next) {
Ken Liuc25558c2021-05-20 15:31:28 +0800850 p_next_partition = TO_CONTAINER(pth_next,
851 struct partition_t,
852 sp_thread);
Kevin Peng64011942021-05-25 11:19:07 +0800853 tfm_set_up_isolation_boundary(p_next_partition);
Mate Toth-Palc430b992019-05-09 21:01:14 +0200854
Summer Qind2ad7e72020-01-06 18:16:35 +0800855 tfm_core_thrd_switch_context(p_actx, pth_curr, pth_next);
Ken Liu2d175172019-03-21 17:08:41 +0800856 }
David Hufb38d562019-09-23 15:58:34 +0800857
858 /*
859 * Handle pending mailbox message from NS in multi-core topology.
860 * Empty operation on single Armv8-M platform.
861 */
862 tfm_rpc_client_call_handler();
Ken Liu2d175172019-03-21 17:08:41 +0800863}
Mingyang Sund44522a2020-01-16 16:48:37 +0800864
Summer Qin02f7f072020-08-24 16:02:54 +0800865void update_caller_outvec_len(struct tfm_msg_body_t *msg)
Mingyang Sund44522a2020-01-16 16:48:37 +0800866{
867 uint32_t i;
868
869 /*
870 * FixeMe: abstract these part into dedicated functions to avoid
871 * accessing thread context in psa layer
872 */
873 /* If it is a NS request via RPC, the owner of this message is not set */
874 if (!is_tfm_rpc_msg(msg)) {
875 TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK);
876 }
877
878 for (i = 0; i < PSA_MAX_IOVEC; i++) {
879 if (msg->msg.out_size[i] == 0) {
880 continue;
881 }
882
883 TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base);
884
885 msg->caller_outvec[i].len = msg->outvec[i].len;
886 }
887}
888
Summer Qin02f7f072020-08-24 16:02:54 +0800889void notify_with_signal(int32_t partition_id, psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800890{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800891 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800892
893 /*
894 * The value of partition_id must be greater than zero as the target of
895 * notification must be a Secure Partition, providing a Non-secure
896 * Partition ID is a fatal error.
897 */
898 if (!TFM_CLIENT_ID_IS_S(partition_id)) {
899 tfm_core_panic();
900 }
901
902 /*
903 * It is a fatal error if partition_id does not correspond to a Secure
904 * Partition.
905 */
906 partition = tfm_spm_get_partition_by_id(partition_id);
907 if (!partition) {
908 tfm_core_panic();
909 }
910
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800911 partition->signals_asserted |= signal;
Mingyang Sund44522a2020-01-16 16:48:37 +0800912
Kevin Peng8dac6102021-03-09 16:44:00 +0800913 if (partition->signals_waiting & signal) {
914 tfm_event_wake(
915 &partition->event,
916 partition->signals_asserted & partition->signals_waiting);
917 partition->signals_waiting &= ~signal;
918 }
Mingyang Sund44522a2020-01-16 16:48:37 +0800919}
920
Kevin Pengeca45b92021-02-09 14:46:50 +0800921__attribute__((naked))
922static void tfm_flih_deprivileged_handling(uint32_t p_ldinf,
923 psa_flih_func flih_func,
924 psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800925{
Kevin Pengeca45b92021-02-09 14:46:50 +0800926 __ASM volatile("SVC %0 \n"
927 "BX LR \n"
928 : : "I" (TFM_SVC_PREPARE_DEPRIV_FLIH));
929}
Kevin Pengdc791882021-03-12 10:57:12 +0800930
Kevin Pengeca45b92021-02-09 14:46:50 +0800931void spm_interrupt_handler(struct partition_load_info_t *p_ldinf,
932 psa_signal_t signal,
933 uint32_t irq_line,
934 psa_flih_func flih_func)
935{
936 uint32_t pid;
937 psa_flih_result_t flih_result;
Kevin Pengdc791882021-03-12 10:57:12 +0800938
Kevin Pengeca45b92021-02-09 14:46:50 +0800939 pid = p_ldinf->pid;
940
941 if (flih_func == NULL) {
942 /* SLIH Model Handling */
943 __disable_irq();
944 tfm_spm_hal_disable_irq(irq_line);
945 notify_with_signal(pid, signal);
946 __enable_irq();
947 return;
948 }
949
950 /* FLIH Model Handling */
951 if (tfm_spm_partition_get_privileged_mode(p_ldinf->flags) ==
952 TFM_PARTITION_PRIVILEGED_MODE) {
953 flih_result = flih_func();
954 if (flih_result == PSA_FLIH_SIGNAL) {
955 __disable_irq();
956 notify_with_signal(pid, signal);
957 __enable_irq();
958 } else if (flih_result != PSA_FLIH_NO_SIGNAL) {
959 /*
960 * Nothing needed to do for PSA_FLIH_NO_SIGNAL
961 * But if the flih_result is invalid, should panic.
962 */
963 tfm_core_panic();
964 }
965 } else {
966 tfm_flih_deprivileged_handling((uint32_t)p_ldinf, flih_func, signal);
967 }
Mingyang Sund44522a2020-01-16 16:48:37 +0800968}
969
Kevin Peng27e42272021-05-24 17:58:53 +0800970struct irq_load_info_t *get_irq_info_for_signal(
971 const struct partition_load_info_t *p_ldinf,
972 psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800973{
974 size_t i;
Kevin Peng27e42272021-05-24 17:58:53 +0800975 struct irq_load_info_t *irq_info;
Mingyang Sund44522a2020-01-16 16:48:37 +0800976
Ken Liu24dffb22021-02-10 11:03:58 +0800977 if (!IS_ONLY_ONE_BIT_IN_UINT32(signal)) {
Kevin Peng27e42272021-05-24 17:58:53 +0800978 return NULL;
Kevin Peng410bee52021-01-13 16:27:17 +0800979 }
980
Kevin Peng27e42272021-05-24 17:58:53 +0800981 irq_info = (struct irq_load_info_t *)LOAD_INFO_IRQ(p_ldinf);
982 for (i = 0; i < p_ldinf->nirqs; i++) {
983 if (irq_info[i].signal == signal) {
984 return &irq_info[i];
Mingyang Sund44522a2020-01-16 16:48:37 +0800985 }
986 }
Kevin Penga20b5af2021-01-11 11:20:52 +0800987
Kevin Peng27e42272021-05-24 17:58:53 +0800988 return NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800989}
990
Summer Qindea1f2c2021-01-11 14:46:34 +0800991#if !defined(__ARM_ARCH_8_1M_MAIN__)
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800992void tfm_spm_validate_caller(uint32_t *p_ctx, uint32_t exc_return)
Mingyang Sund44522a2020-01-16 16:48:37 +0800993{
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800994 /*
995 * TODO: the reentrant detection mechanism needs to be changed when there
996 * is no boundaries.
997 */
Mingyang Sund44522a2020-01-16 16:48:37 +0800998 uintptr_t stacked_ctx_pos;
Mingyang Sun22a3faf2021-07-09 15:32:47 +0800999 bool ns_caller = false;
1000 struct partition_t *p_cur_sp = tfm_spm_get_running_partition();
1001 uint32_t veneer_base =
1002 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_CODE, $$RO$$Base);
1003 uint32_t veneer_limit =
1004 (uint32_t)&REGION_NAME(Image$$, TFM_UNPRIV_CODE, $$RO$$Limit);
1005
1006 if (!p_cur_sp) {
1007 tfm_core_panic();
1008 }
1009
1010 /*
1011 * The caller security attribute detection bases on LR of state context.
1012 * However, if SP calls PSA APIs based on its customized SVC, the LR may be
1013 * occupied by general purpose value while calling SVC.
1014 * Check if caller comes from non-secure: return address (p_ctx[6]) belongs
1015 * to veneer section, and the bit0 of LR (p_ctx[5]) is zero.
1016 */
1017 if (p_ctx[6] >= veneer_base && p_ctx[6] < veneer_limit &&
1018 !(p_ctx[5] & TFM_VENEER_LR_BIT0_MASK)) {
1019 ns_caller = true;
1020 }
1021
1022 /* If called from ns, partition ID should be TFM_SP_NON_SECURE_ID. */
1023 if ((ns_caller == true) !=
1024 (p_cur_sp->p_ldinf->pid == TFM_SP_NON_SECURE_ID)) {
1025 tfm_core_panic();
1026 }
Mingyang Sund44522a2020-01-16 16:48:37 +08001027
1028 if (ns_caller) {
1029 /*
1030 * The background IRQ can't be supported, since if SP is executing,
1031 * the preempted context of SP can be different with the one who
Mingyang Sun22a3faf2021-07-09 15:32:47 +08001032 * preempts veneer. Check if veneer stack contains multiple contexts.
Mingyang Sund44522a2020-01-16 16:48:37 +08001033 */
1034 stacked_ctx_pos = (uintptr_t)p_ctx +
1035 sizeof(struct tfm_state_context_t) +
Ken Liu05e13ba2020-07-25 10:31:33 +08001036 TFM_STACK_SEALED_SIZE;
Mingyang Sund44522a2020-01-16 16:48:37 +08001037
1038 if (is_stack_alloc_fp_space(exc_return)) {
Xinyu Zhang3a453242021-04-16 17:57:09 +08001039#if defined(__FPU_USED) && (__FPU_USED == 1U)
Mingyang Sund44522a2020-01-16 16:48:37 +08001040 if (FPU->FPCCR & FPU_FPCCR_TS_Msk) {
1041 stacked_ctx_pos += TFM_ADDTIONAL_FP_CONTEXT_WORDS *
1042 sizeof(uint32_t);
1043 }
1044#endif
1045 stacked_ctx_pos += TFM_BASIC_FP_CONTEXT_WORDS * sizeof(uint32_t);
1046 }
1047
Mingyang Sunaf22ffa2020-07-09 17:48:37 +08001048 if (stacked_ctx_pos != p_cur_sp->sp_thread.stk_top) {
Mingyang Sund44522a2020-01-16 16:48:37 +08001049 tfm_core_panic();
1050 }
Mingyang Sund44522a2020-01-16 16:48:37 +08001051 }
1052}
Summer Qindea1f2c2021-01-11 14:46:34 +08001053#endif