blob: 5b6065c57be7ad12e0e1db7042b2963b518aeb7b [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 Sun7397b4f2020-06-17 15:07:45 +080030#include "psa_manifest/pid.h"
Summer Qin5fdcf632020-06-22 16:49:24 +080031#include "tfm/tfm_spm_services.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
Kevin Pengeca45b92021-02-09 14:46:50 +080047void spm_interrupt_handler(struct partition_load_info_t *p_ldinf,
48 psa_signal_t signal,
49 uint32_t irq_line,
50 psa_flih_func flih_func);
Mingyang Sund44522a2020-01-16 16:48:37 +080051
52#include "tfm_secure_irq_handlers_ipc.inc"
Edison Ai764d41f2018-09-21 15:56:36 +080053
Summer Qin373feb12020-03-27 15:35:33 +080054/*********************** Connection handle conversion APIs *******************/
55
Summer Qin373feb12020-03-27 15:35:33 +080056#define CONVERSION_FACTOR_BITOFFSET 3
57#define CONVERSION_FACTOR_VALUE (1 << CONVERSION_FACTOR_BITOFFSET)
58/* Set 32 as the maximum */
59#define CONVERSION_FACTOR_VALUE_MAX 0x20
60
61#if CONVERSION_FACTOR_VALUE > CONVERSION_FACTOR_VALUE_MAX
62#error "CONVERSION FACTOR OUT OF RANGE"
63#endif
64
65static uint32_t loop_index;
66
67/*
68 * A handle instance psa_handle_t allocated inside SPM is actually a memory
69 * address among the handle pool. Return this handle to the client directly
70 * exposes information of secure memory address. In this case, converting the
71 * handle into another value does not represent the memory address to avoid
72 * exposing secure memory directly to clients.
73 *
74 * This function converts the handle instance into another value by scaling the
75 * handle in pool offset, the converted value is named as a user handle.
76 *
77 * The formula:
78 * user_handle = (handle_instance - POOL_START) * CONVERSION_FACTOR_VALUE +
79 * CLIENT_HANDLE_VALUE_MIN + loop_index
80 * where:
81 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
82 * exceed CONVERSION_FACTOR_VALUE_MAX.
83 *
84 * handle_instance in RANGE[POOL_START, POOL_END]
85 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
86 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
87 *
88 * note:
89 * loop_index is used to promise same handle instance is converted into
90 * different user handles in short time.
91 */
Ken Liu505b1702020-05-29 13:19:58 +080092psa_handle_t tfm_spm_to_user_handle(struct tfm_conn_handle_t *handle_instance)
Summer Qin373feb12020-03-27 15:35:33 +080093{
94 psa_handle_t user_handle;
95
96 loop_index = (loop_index + 1) % CONVERSION_FACTOR_VALUE;
97 user_handle = (psa_handle_t)((((uintptr_t)handle_instance -
98 (uintptr_t)conn_handle_pool) << CONVERSION_FACTOR_BITOFFSET) +
99 CLIENT_HANDLE_VALUE_MIN + loop_index);
100
101 return user_handle;
102}
103
104/*
105 * This function converts a user handle into a corresponded handle instance.
106 * The converted value is validated before returning, an invalid handle instance
107 * is returned as NULL.
108 *
109 * The formula:
110 * handle_instance = ((user_handle - CLIENT_HANDLE_VALUE_MIN) /
111 * CONVERSION_FACTOR_VALUE) + POOL_START
112 * where:
113 * CONVERSION_FACTOR_VALUE = 1 << CONVERSION_FACTOR_BITOFFSET, and should not
114 * exceed CONVERSION_FACTOR_VALUE_MAX.
115 *
116 * handle_instance in RANGE[POOL_START, POOL_END]
117 * user_handle in RANGE[CLIENT_HANDLE_VALUE_MIN, 0x3FFFFFFF]
118 * loop_index in RANGE[0, CONVERSION_FACTOR_VALUE - 1]
119 */
120struct tfm_conn_handle_t *tfm_spm_to_handle_instance(psa_handle_t user_handle)
121{
122 struct tfm_conn_handle_t *handle_instance;
123
124 if (user_handle == PSA_NULL_HANDLE) {
125 return NULL;
126 }
127
128 handle_instance = (struct tfm_conn_handle_t *)((((uintptr_t)user_handle -
129 CLIENT_HANDLE_VALUE_MIN) >> CONVERSION_FACTOR_BITOFFSET) +
130 (uintptr_t)conn_handle_pool);
131
132 return handle_instance;
133}
134
Edison Ai764d41f2018-09-21 15:56:36 +0800135/* Service handle management functions */
Mingyang Sun783a59b2021-04-20 15:52:18 +0800136struct tfm_conn_handle_t *tfm_spm_create_conn_handle(struct service_t *service,
137 int32_t client_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800138{
Edison Ai9cc26242019-08-06 11:28:04 +0800139 struct tfm_conn_handle_t *p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800140
Ken Liuf250b8b2019-12-27 16:31:24 +0800141 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800142
143 /* Get buffer for handle list structure from handle pool */
Edison Ai9cc26242019-08-06 11:28:04 +0800144 p_handle = (struct tfm_conn_handle_t *)tfm_pool_alloc(conn_handle_pool);
145 if (!p_handle) {
Summer Qin630c76b2020-05-20 10:32:58 +0800146 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800147 }
148
Edison Ai9cc26242019-08-06 11:28:04 +0800149 p_handle->service = service;
Shawn Shancc39fcb2019-11-13 15:38:16 +0800150 p_handle->status = TFM_HANDLE_STATUS_IDLE;
Summer Qin1ce712a2019-10-14 18:04:05 +0800151 p_handle->client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800152
153 /* Add handle node to list for next psa functions */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800154 BI_LIST_INSERT_BEFORE(&service->handle_list, &p_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800155
Summer Qin630c76b2020-05-20 10:32:58 +0800156 return p_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800157}
158
Summer Qin630c76b2020-05-20 10:32:58 +0800159int32_t tfm_spm_validate_conn_handle(
160 const struct tfm_conn_handle_t *conn_handle,
161 int32_t client_id)
Summer Qin1ce712a2019-10-14 18:04:05 +0800162{
163 /* Check the handle address is validated */
164 if (is_valid_chunk_data_in_pool(conn_handle_pool,
165 (uint8_t *)conn_handle) != true) {
Ken Liubcae38b2021-01-20 15:47:44 +0800166 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800167 }
168
169 /* Check the handle caller is correct */
Summer Qin630c76b2020-05-20 10:32:58 +0800170 if (conn_handle->client_id != client_id) {
Ken Liubcae38b2021-01-20 15:47:44 +0800171 return SPM_ERROR_GENERIC;
Summer Qin1ce712a2019-10-14 18:04:05 +0800172 }
173
Ken Liubcae38b2021-01-20 15:47:44 +0800174 return SPM_SUCCESS;
Summer Qin1ce712a2019-10-14 18:04:05 +0800175}
176
Mingyang Sun783a59b2021-04-20 15:52:18 +0800177int32_t tfm_spm_free_conn_handle(struct service_t *service,
Summer Qin02f7f072020-08-24 16:02:54 +0800178 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800179{
Ken Liuf250b8b2019-12-27 16:31:24 +0800180 TFM_CORE_ASSERT(service);
Summer Qin630c76b2020-05-20 10:32:58 +0800181 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800182
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200183 /* Clear magic as the handler is not used anymore */
Summer Qin630c76b2020-05-20 10:32:58 +0800184 conn_handle->internal_msg.magic = 0;
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200185
Edison Ai764d41f2018-09-21 15:56:36 +0800186 /* Remove node from handle list */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800187 BI_LIST_REMOVE_NODE(&conn_handle->list);
Edison Ai764d41f2018-09-21 15:56:36 +0800188
189 /* Back handle buffer to pool */
Ken Liu66ca6132021-02-24 08:49:51 +0800190 tfm_pool_free(conn_handle_pool, conn_handle);
Ken Liubcae38b2021-01-20 15:47:44 +0800191 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800192}
193
Mingyang Sun783a59b2021-04-20 15:52:18 +0800194int32_t tfm_spm_set_rhandle(struct service_t *service,
Summer Qin02f7f072020-08-24 16:02:54 +0800195 struct tfm_conn_handle_t *conn_handle,
196 void *rhandle)
Edison Ai764d41f2018-09-21 15:56:36 +0800197{
Ken Liuf250b8b2019-12-27 16:31:24 +0800198 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800199 /* Set reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800200 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800201
Summer Qin630c76b2020-05-20 10:32:58 +0800202 conn_handle->rhandle = rhandle;
Ken Liubcae38b2021-01-20 15:47:44 +0800203 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800204}
205
Mingyang Sund44522a2020-01-16 16:48:37 +0800206/**
Xinyu Zhang3a453242021-04-16 17:57:09 +0800207 * \brief Get reverse handle value from connection handle.
Mingyang Sund44522a2020-01-16 16:48:37 +0800208 *
209 * \param[in] service Target service context pointer
210 * \param[in] conn_handle Connection handle created by
Summer Qin630c76b2020-05-20 10:32:58 +0800211 * tfm_spm_create_conn_handle()
Mingyang Sund44522a2020-01-16 16:48:37 +0800212 *
213 * \retval void * Success
214 * \retval "Does not return" Panic for those:
215 * service pointer are NULL
Xinyu Zhang3a453242021-04-16 17:57:09 +0800216 * handle is \ref PSA_NULL_HANDLE
Mingyang Sund44522a2020-01-16 16:48:37 +0800217 * handle node does not be found
218 */
Mingyang Sun783a59b2021-04-20 15:52:18 +0800219static void *tfm_spm_get_rhandle(struct service_t *service,
Summer Qin630c76b2020-05-20 10:32:58 +0800220 struct tfm_conn_handle_t *conn_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800221{
Ken Liuf250b8b2019-12-27 16:31:24 +0800222 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800223 /* Get reverse handle value only be allowed for a connected handle */
Summer Qin630c76b2020-05-20 10:32:58 +0800224 TFM_CORE_ASSERT(conn_handle != NULL);
Edison Ai764d41f2018-09-21 15:56:36 +0800225
Summer Qin630c76b2020-05-20 10:32:58 +0800226 return conn_handle->rhandle;
Edison Ai764d41f2018-09-21 15:56:36 +0800227}
228
229/* Partition management functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800230
Summer Qin02f7f072020-08-24 16:02:54 +0800231struct tfm_msg_body_t *tfm_spm_get_msg_by_signal(struct partition_t *partition,
232 psa_signal_t signal)
Edison Ai764d41f2018-09-21 15:56:36 +0800233{
Ken Liu2c47f7f2021-01-22 11:06:04 +0800234 struct bi_list_node_t *node, *head;
Mingyang Sun73056b62020-07-03 15:18:46 +0800235 struct tfm_msg_body_t *tmp_msg, *msg = NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800236
Ken Liuf250b8b2019-12-27 16:31:24 +0800237 TFM_CORE_ASSERT(partition);
Edison Ai764d41f2018-09-21 15:56:36 +0800238
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800239 head = &partition->msg_list;
Mingyang Sun73056b62020-07-03 15:18:46 +0800240
Ken Liu2c47f7f2021-01-22 11:06:04 +0800241 if (BI_LIST_IS_EMPTY(head)) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800242 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800243 }
244
Mingyang Sun73056b62020-07-03 15:18:46 +0800245 /*
246 * There may be multiple messages for this RoT Service signal, do not clear
247 * partition mask until no remaining message. Search may be optimized.
248 */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800249 BI_LIST_FOR_EACH(node, head) {
Ken Liuc25558c2021-05-20 15:31:28 +0800250 tmp_msg = TO_CONTAINER(node, struct tfm_msg_body_t, msg_node);
Ken Liuacd2a572021-05-12 16:19:04 +0800251 if (tmp_msg->service->p_ldinf->signal == signal && msg) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800252 return msg;
Ken Liuacd2a572021-05-12 16:19:04 +0800253 } else if (tmp_msg->service->p_ldinf->signal == signal) {
Mingyang Sun73056b62020-07-03 15:18:46 +0800254 msg = tmp_msg;
Ken Liu2c47f7f2021-01-22 11:06:04 +0800255 BI_LIST_REMOVE_NODE(node);
Edison Ai764d41f2018-09-21 15:56:36 +0800256 }
257 }
Mingyang Sun73056b62020-07-03 15:18:46 +0800258
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800259 partition->signals_asserted &= ~signal;
Mingyang Sun73056b62020-07-03 15:18:46 +0800260
261 return msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800262}
263
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800264#if TFM_LVL != 1
265/**
266 * \brief Change the privilege mode for partition thread mode.
267 *
268 * \param[in] privileged Privileged mode,
269 * \ref TFM_PARTITION_PRIVILEGED_MODE
270 * and \ref TFM_PARTITION_UNPRIVILEGED_MODE
271 *
272 * \note Barrier instructions are not called by this function, and if
273 * it is called in thread mode, it might be necessary to call
274 * them after this function returns.
275 */
276static void tfm_spm_partition_change_privilege(uint32_t privileged)
277{
278 CONTROL_Type ctrl;
279
280 ctrl.w = __get_CONTROL();
281
282 if (privileged == TFM_PARTITION_PRIVILEGED_MODE) {
283 ctrl.b.nPRIV = 0;
284 } else {
285 ctrl.b.nPRIV = 1;
286 }
287
288 __set_CONTROL(ctrl.w);
289}
290#endif /* if(TFM_LVL != 1) */
291
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800292uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags)
293{
Kevin Pengbf9a97e2021-06-18 16:34:12 +0800294#if TFM_LVL == 1
295 return TFM_PARTITION_PRIVILEGED_MODE;
296#else /* TFM_LVL == 1 */
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800297 if (partition_flags & SPM_PART_FLAG_PSA_ROT) {
298 return TFM_PARTITION_PRIVILEGED_MODE;
299 } else {
300 return TFM_PARTITION_UNPRIVILEGED_MODE;
301 }
Kevin Pengbf9a97e2021-06-18 16:34:12 +0800302#endif /* TFM_LVL == 1 */
Mingyang Sunda30f1e2020-07-13 17:20:32 +0800303}
304
Mingyang Sun783a59b2021-04-20 15:52:18 +0800305struct service_t *tfm_spm_get_service_by_sid(uint32_t sid)
Edison Ai764d41f2018-09-21 15:56:36 +0800306{
Ken Liuea45b0d2021-05-22 17:41:25 +0800307 struct service_t *p_serv;
Edison Ai764d41f2018-09-21 15:56:36 +0800308
Ken Liuea45b0d2021-05-22 17:41:25 +0800309 UNI_LIST_FOR_EACH(p_serv, &services_listhead) {
310 if (p_serv->p_ldinf->sid == sid) {
311 return p_serv;
Mingyang Sunef42f442021-06-11 15:07:58 +0800312 }
313 }
314
Ken Liuea45b0d2021-05-22 17:41:25 +0800315 return NULL;
Edison Ai764d41f2018-09-21 15:56:36 +0800316}
317
Mingyang Sund44522a2020-01-16 16:48:37 +0800318/**
319 * \brief Get the partition context by partition ID.
320 *
321 * \param[in] partition_id Partition identity
322 *
323 * \retval NULL Failed
324 * \retval "Not NULL" Target partition context pointer,
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800325 * \ref partition_t structures
Mingyang Sund44522a2020-01-16 16:48:37 +0800326 */
Kevin Pengeca45b92021-02-09 14:46:50 +0800327struct partition_t *tfm_spm_get_partition_by_id(int32_t partition_id)
Edison Ai764d41f2018-09-21 15:56:36 +0800328{
Ken Liuea45b0d2021-05-22 17:41:25 +0800329 struct partition_t *p_part;
Edison Ai764d41f2018-09-21 15:56:36 +0800330
Ken Liuea45b0d2021-05-22 17:41:25 +0800331 UNI_LIST_FOR_EACH(p_part, &partitions_listhead) {
332 if (p_part->p_ldinf->pid == partition_id) {
333 return p_part;
334 }
Edison Ai764d41f2018-09-21 15:56:36 +0800335 }
Ken Liuea45b0d2021-05-22 17:41:25 +0800336
Edison Ai764d41f2018-09-21 15:56:36 +0800337 return NULL;
338}
339
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800340struct partition_t *tfm_spm_get_running_partition(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800341{
Kevin Peng82fbca52021-03-09 13:48:48 +0800342 struct tfm_core_thread_t *pth = tfm_core_thrd_get_curr();
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800343 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800344
Ken Liuc25558c2021-05-20 15:31:28 +0800345 partition = TO_CONTAINER(pth, struct partition_t, sp_thread);
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800346
Kevin Peng79c2bda2020-07-24 16:31:12 +0800347 return partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800348}
349
Mingyang Sun783a59b2021-04-20 15:52:18 +0800350int32_t tfm_spm_check_client_version(struct service_t *service,
Jaykumar Pitambarbhai Patel3a986022019-10-08 17:37:15 +0530351 uint32_t version)
Edison Ai764d41f2018-09-21 15:56:36 +0800352{
Ken Liuf250b8b2019-12-27 16:31:24 +0800353 TFM_CORE_ASSERT(service);
Edison Ai764d41f2018-09-21 15:56:36 +0800354
Ken Liuacd2a572021-05-12 16:19:04 +0800355 switch (SERVICE_GET_VERSION_POLICY(service->p_ldinf->flags)) {
Ken Liub3b2cb62021-05-22 00:39:28 +0800356 case SERVICE_VERSION_POLICY_RELAXED:
Ken Liuacd2a572021-05-12 16:19:04 +0800357 if (version > service->p_ldinf->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800358 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800359 }
360 break;
Ken Liub3b2cb62021-05-22 00:39:28 +0800361 case SERVICE_VERSION_POLICY_STRICT:
Ken Liuacd2a572021-05-12 16:19:04 +0800362 if (version != service->p_ldinf->version) {
Ken Liubcae38b2021-01-20 15:47:44 +0800363 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800364 }
365 break;
366 default:
Ken Liubcae38b2021-01-20 15:47:44 +0800367 return SPM_ERROR_VERSION;
Edison Ai764d41f2018-09-21 15:56:36 +0800368 }
Ken Liubcae38b2021-01-20 15:47:44 +0800369 return SPM_SUCCESS;
Edison Ai764d41f2018-09-21 15:56:36 +0800370}
371
Edison Aie728fbf2019-11-13 09:37:12 +0800372int32_t tfm_spm_check_authorization(uint32_t sid,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800373 struct service_t *service,
Summer Qin618e8c32019-12-09 10:47:20 +0800374 bool ns_caller)
Edison Aie728fbf2019-11-13 09:37:12 +0800375{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800376 struct partition_t *partition = NULL;
Mingyang Sun2b352662021-04-21 11:35:43 +0800377 uint32_t *dep;
Edison Aie728fbf2019-11-13 09:37:12 +0800378 int32_t i;
379
Ken Liuf250b8b2019-12-27 16:31:24 +0800380 TFM_CORE_ASSERT(service);
Edison Aie728fbf2019-11-13 09:37:12 +0800381
382 if (ns_caller) {
Ken Liuacd2a572021-05-12 16:19:04 +0800383 if (!SERVICE_IS_NS_ACCESSIBLE(service->p_ldinf->flags)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800384 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800385 }
386 } else {
387 partition = tfm_spm_get_running_partition();
388 if (!partition) {
Edison Ai9059ea02019-11-28 13:46:14 +0800389 tfm_core_panic();
Edison Aie728fbf2019-11-13 09:37:12 +0800390 }
391
Ken Liuacd2a572021-05-12 16:19:04 +0800392 dep = (uint32_t *)LOAD_INFO_DEPS(partition->p_ldinf);
393 for (i = 0; i < partition->p_ldinf->ndeps; i++) {
Mingyang Sun2b352662021-04-21 11:35:43 +0800394 if (dep[i] == sid) {
Edison Aie728fbf2019-11-13 09:37:12 +0800395 break;
396 }
397 }
398
Ken Liuacd2a572021-05-12 16:19:04 +0800399 if (i == partition->p_ldinf->ndeps) {
Ken Liubcae38b2021-01-20 15:47:44 +0800400 return SPM_ERROR_GENERIC;
Edison Aie728fbf2019-11-13 09:37:12 +0800401 }
402 }
Ken Liubcae38b2021-01-20 15:47:44 +0800403 return SPM_SUCCESS;
Edison Aie728fbf2019-11-13 09:37:12 +0800404}
405
Edison Ai764d41f2018-09-21 15:56:36 +0800406/* Message functions */
Mingyang Sund44522a2020-01-16 16:48:37 +0800407
Summer Qin02f7f072020-08-24 16:02:54 +0800408struct tfm_msg_body_t *tfm_spm_get_msg_from_handle(psa_handle_t msg_handle)
Edison Ai764d41f2018-09-21 15:56:36 +0800409{
410 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200411 * The message handler passed by the caller is considered invalid in the
412 * following cases:
413 * 1. Not a valid message handle. (The address of a message is not the
414 * address of a possible handle from the pool
415 * 2. Handle not belongs to the caller partition (The handle is either
416 * unused, or owned by anither partition)
417 * Check the conditions above
Edison Ai764d41f2018-09-21 15:56:36 +0800418 */
Ken Liu505b1702020-05-29 13:19:58 +0800419 struct tfm_msg_body_t *p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800420 uint32_t partition_id;
Ken Liu505b1702020-05-29 13:19:58 +0800421 struct tfm_conn_handle_t *p_conn_handle =
422 tfm_spm_to_handle_instance(msg_handle);
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200423
424 if (is_valid_chunk_data_in_pool(
Ken Liu505b1702020-05-29 13:19:58 +0800425 conn_handle_pool, (uint8_t *)p_conn_handle) != 1) {
Edison Ai764d41f2018-09-21 15:56:36 +0800426 return NULL;
427 }
428
Ken Liu505b1702020-05-29 13:19:58 +0800429 p_msg = &p_conn_handle->internal_msg;
430
Edison Ai764d41f2018-09-21 15:56:36 +0800431 /*
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200432 * Check that the magic number is correct. This proves that the message
433 * structure contains an active message.
Edison Ai764d41f2018-09-21 15:56:36 +0800434 */
Ken Liu505b1702020-05-29 13:19:58 +0800435 if (p_msg->magic != TFM_MSG_MAGIC) {
Edison Ai764d41f2018-09-21 15:56:36 +0800436 return NULL;
437 }
438
Mate Toth-Pala4b5d242019-09-23 09:14:47 +0200439 /* Check that the running partition owns the message */
Mingyang Sunf3d29892019-07-10 17:50:23 +0800440 partition_id = tfm_spm_partition_get_running_partition_id();
Ken Liuacd2a572021-05-12 16:19:04 +0800441 if (partition_id != p_msg->service->partition->p_ldinf->pid) {
Edison Ai764d41f2018-09-21 15:56:36 +0800442 return NULL;
443 }
444
Ken Liu505b1702020-05-29 13:19:58 +0800445 return p_msg;
Edison Ai764d41f2018-09-21 15:56:36 +0800446}
447
Kevin Pengdf6aa292021-03-11 17:58:50 +0800448struct tfm_msg_body_t *
449 tfm_spm_get_msg_buffer_from_conn_handle(struct tfm_conn_handle_t *conn_handle)
450{
451 TFM_CORE_ASSERT(conn_handle != NULL);
452
453 return &(conn_handle->internal_msg);
454}
455
Edison Ai97115822019-08-01 14:22:19 +0800456void tfm_spm_fill_msg(struct tfm_msg_body_t *msg,
Mingyang Sun783a59b2021-04-20 15:52:18 +0800457 struct service_t *service,
Ken Liu505b1702020-05-29 13:19:58 +0800458 psa_handle_t handle,
Summer Qin1ce712a2019-10-14 18:04:05 +0800459 int32_t type, int32_t client_id,
Edison Ai97115822019-08-01 14:22:19 +0800460 psa_invec *invec, size_t in_len,
461 psa_outvec *outvec, size_t out_len,
462 psa_outvec *caller_outvec)
463{
Edison Ai764d41f2018-09-21 15:56:36 +0800464 uint32_t i;
Ken Liu505b1702020-05-29 13:19:58 +0800465 struct tfm_conn_handle_t *conn_handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800466
Ken Liuf250b8b2019-12-27 16:31:24 +0800467 TFM_CORE_ASSERT(msg);
468 TFM_CORE_ASSERT(service);
469 TFM_CORE_ASSERT(!(invec == NULL && in_len != 0));
470 TFM_CORE_ASSERT(!(outvec == NULL && out_len != 0));
471 TFM_CORE_ASSERT(in_len <= PSA_MAX_IOVEC);
472 TFM_CORE_ASSERT(out_len <= PSA_MAX_IOVEC);
473 TFM_CORE_ASSERT(in_len + out_len <= PSA_MAX_IOVEC);
Edison Ai764d41f2018-09-21 15:56:36 +0800474
Edison Ai764d41f2018-09-21 15:56:36 +0800475 /* Clear message buffer before using it */
Summer Qinf24dbb52020-07-23 14:53:54 +0800476 spm_memset(msg, 0, sizeof(struct tfm_msg_body_t));
Edison Ai764d41f2018-09-21 15:56:36 +0800477
Ken Liu35f89392019-03-14 14:51:05 +0800478 tfm_event_init(&msg->ack_evnt);
Edison Ai764d41f2018-09-21 15:56:36 +0800479 msg->magic = TFM_MSG_MAGIC;
480 msg->service = service;
Edison Ai764d41f2018-09-21 15:56:36 +0800481 msg->caller_outvec = caller_outvec;
Summer Qin1ce712a2019-10-14 18:04:05 +0800482 msg->msg.client_id = client_id;
Edison Ai764d41f2018-09-21 15:56:36 +0800483
484 /* Copy contents */
485 msg->msg.type = type;
486
487 for (i = 0; i < in_len; i++) {
488 msg->msg.in_size[i] = invec[i].len;
489 msg->invec[i].base = invec[i].base;
490 }
491
492 for (i = 0; i < out_len; i++) {
493 msg->msg.out_size[i] = outvec[i].len;
494 msg->outvec[i].base = outvec[i].base;
495 /* Out len is used to record the writed number, set 0 here again */
496 msg->outvec[i].len = 0;
497 }
498
Ken Liu505b1702020-05-29 13:19:58 +0800499 /* Use the user connect handle as the message handle */
500 msg->msg.handle = handle;
Edison Ai764d41f2018-09-21 15:56:36 +0800501
Ken Liu505b1702020-05-29 13:19:58 +0800502 conn_handle = tfm_spm_to_handle_instance(handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800503 /* For connected handle, set rhandle to every message */
Ken Liu505b1702020-05-29 13:19:58 +0800504 if (conn_handle) {
505 msg->msg.rhandle = tfm_spm_get_rhandle(service, conn_handle);
Edison Ai764d41f2018-09-21 15:56:36 +0800506 }
David Hu46603dd2019-12-11 18:05:16 +0800507
508 /* Set the private data of NSPE client caller in multi-core topology */
509 if (TFM_CLIENT_ID_IS_NS(client_id)) {
510 tfm_rpc_set_caller_data(msg, client_id);
511 }
Edison Ai764d41f2018-09-21 15:56:36 +0800512}
513
Mingyang Sun783a59b2021-04-20 15:52:18 +0800514void tfm_spm_send_event(struct service_t *service,
Kevin Peng8dac6102021-03-09 16:44:00 +0800515 struct tfm_msg_body_t *msg)
Edison Ai764d41f2018-09-21 15:56:36 +0800516{
Kevin Peng8dac6102021-03-09 16:44:00 +0800517 struct partition_t *partition = NULL;
518 psa_signal_t signal = 0;
Mingyang Sun5e13aa72019-07-10 10:30:16 +0800519
Ken Liuacd2a572021-05-12 16:19:04 +0800520 if (!msg || !service || !service->p_ldinf || !service->partition) {
Kevin Peng8dac6102021-03-09 16:44:00 +0800521 tfm_core_panic();
522 }
523
524 partition = service->partition;
Ken Liuacd2a572021-05-12 16:19:04 +0800525 signal = service->p_ldinf->signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800526
Mingyang Sun73056b62020-07-03 15:18:46 +0800527 /* Add message to partition message list tail */
Ken Liu2c47f7f2021-01-22 11:06:04 +0800528 BI_LIST_INSERT_BEFORE(&partition->msg_list, &msg->msg_node);
Edison Ai764d41f2018-09-21 15:56:36 +0800529
530 /* Messages put. Update signals */
Kevin Peng8dac6102021-03-09 16:44:00 +0800531 partition->signals_asserted |= signal;
Edison Ai764d41f2018-09-21 15:56:36 +0800532
Kevin Peng8dac6102021-03-09 16:44:00 +0800533 if (partition->signals_waiting & signal) {
534 tfm_event_wake(
535 &partition->event,
536 (partition->signals_asserted & partition->signals_waiting));
537 partition->signals_waiting &= ~signal;
538 }
Edison Ai764d41f2018-09-21 15:56:36 +0800539
David Hufb38d562019-09-23 15:58:34 +0800540 /*
541 * If it is a NS request via RPC, it is unnecessary to block current
542 * thread.
543 */
544 if (!is_tfm_rpc_msg(msg)) {
545 tfm_event_wait(&msg->ack_evnt);
546 }
Edison Ai764d41f2018-09-21 15:56:36 +0800547}
548
Mingyang Sunf3d29892019-07-10 17:50:23 +0800549uint32_t tfm_spm_partition_get_running_partition_id(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800550{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800551 struct partition_t *partition;
Edison Ai764d41f2018-09-21 15:56:36 +0800552
Kevin Peng79c2bda2020-07-24 16:31:12 +0800553 partition = tfm_spm_get_running_partition();
Ken Liuacd2a572021-05-12 16:19:04 +0800554 if (partition && partition->p_ldinf) {
555 return partition->p_ldinf->pid;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800556 } else {
557 return INVALID_PARTITION_ID;
558 }
Edison Ai764d41f2018-09-21 15:56:36 +0800559}
560
Summer Qin43c185d2019-10-10 15:44:42 +0800561int32_t tfm_memory_check(const void *buffer, size_t len, bool ns_caller,
Summer Qineb537e52019-03-29 09:57:10 +0800562 enum tfm_memory_access_e access,
563 uint32_t privileged)
Summer Qin2bfd2a02018-09-26 17:10:41 +0800564{
Mingyang Sund1ed6732020-08-26 15:52:21 +0800565 enum tfm_hal_status_t err;
566 uint32_t attr = 0;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800567
568 /* If len is zero, this indicates an empty buffer and base is ignored */
569 if (len == 0) {
Ken Liubcae38b2021-01-20 15:47:44 +0800570 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800571 }
572
573 if (!buffer) {
Ken Liubcae38b2021-01-20 15:47:44 +0800574 return SPM_ERROR_BAD_PARAMETERS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800575 }
576
577 if ((uintptr_t)buffer > (UINTPTR_MAX - len)) {
Ken Liubcae38b2021-01-20 15:47:44 +0800578 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800579 }
580
Summer Qin424d4db2019-03-25 14:09:51 +0800581 if (access == TFM_MEMORY_ACCESS_RW) {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800582 attr |= (TFM_HAL_ACCESS_READABLE | TFM_HAL_ACCESS_WRITABLE);
Summer Qin2bfd2a02018-09-26 17:10:41 +0800583 } else {
Mingyang Sund1ed6732020-08-26 15:52:21 +0800584 attr |= TFM_HAL_ACCESS_READABLE;
Summer Qin424d4db2019-03-25 14:09:51 +0800585 }
Mingyang Sund1ed6732020-08-26 15:52:21 +0800586
587 if (privileged == TFM_PARTITION_UNPRIVILEGED_MODE) {
588 attr |= TFM_HAL_ACCESS_UNPRIVILEGED;
589 } else {
590 attr &= ~TFM_HAL_ACCESS_UNPRIVILEGED;
591 }
592
593 if (ns_caller) {
594 attr |= TFM_HAL_ACCESS_NS;
595 }
596
597 err = tfm_hal_memory_has_access((uintptr_t)buffer, len, attr);
598
599 if (err == TFM_HAL_SUCCESS) {
Ken Liubcae38b2021-01-20 15:47:44 +0800600 return SPM_SUCCESS;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800601 }
602
Ken Liubcae38b2021-01-20 15:47:44 +0800603 return SPM_ERROR_MEMORY_CHECK;
Summer Qin2bfd2a02018-09-26 17:10:41 +0800604}
605
Ken Liuce2692d2020-02-11 12:39:36 +0800606uint32_t tfm_spm_init(void)
Edison Ai764d41f2018-09-21 15:56:36 +0800607{
Kevin Peng54d47fb2021-06-15 16:40:08 +0800608 uint32_t i;
Mingyang Sun61f8fbc2021-06-04 17:49:56 +0800609 bool privileged;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800610 struct partition_t *partition;
Summer Qin66f1e032020-01-06 15:40:03 +0800611 struct tfm_core_thread_t *pth, *p_ns_entry_thread = NULL;
Ken Liu86686282021-04-27 11:11:15 +0800612 const struct platform_data_t *platform_data_p;
Mingyang Sun8d004f72021-06-01 10:46:26 +0800613 const struct partition_load_info_t *p_ldinf;
Ken Liu4520ce32021-05-11 22:49:10 +0800614 struct asset_desc_t *p_asset_load;
David Huf07e97d2021-02-15 22:05:40 +0800615#ifdef TFM_FIH_PROFILE_ON
616 fih_int fih_rc = FIH_FAILURE;
617#endif
Edison Ai764d41f2018-09-21 15:56:36 +0800618
619 tfm_pool_init(conn_handle_pool,
620 POOL_BUFFER_SIZE(conn_handle_pool),
621 sizeof(struct tfm_conn_handle_t),
622 TFM_CONN_HANDLE_MAX_NUM);
Edison Ai764d41f2018-09-21 15:56:36 +0800623
Ken Liuea45b0d2021-05-22 17:41:25 +0800624 UNI_LISI_INIT_HEAD(&partitions_listhead);
625 UNI_LISI_INIT_HEAD(&services_listhead);
626
Ken Liuacd2a572021-05-12 16:19:04 +0800627 while (1) {
Ken Liuea45b0d2021-05-22 17:41:25 +0800628 partition = load_a_partition_assuredly(&partitions_listhead);
Ken Liuacd2a572021-05-12 16:19:04 +0800629 if (partition == NULL) {
630 break;
Kevin Peng79c2bda2020-07-24 16:31:12 +0800631 }
632
Mingyang Sun8d004f72021-06-01 10:46:26 +0800633 p_ldinf = partition->p_ldinf;
Mingyang Sun2b352662021-04-21 11:35:43 +0800634
Kevin Peng27e42272021-05-24 17:58:53 +0800635 if (p_ldinf->nservices) {
Ken Liuea45b0d2021-05-22 17:41:25 +0800636 load_services_assuredly(partition, &services_listhead,
Kevin Peng27e42272021-05-24 17:58:53 +0800637 stateless_services_ref_tbl,
638 sizeof(stateless_services_ref_tbl));
639 }
640
641 if (p_ldinf->nirqs) {
642 load_irqs_assuredly(partition);
643 }
644
Ken Liuacd2a572021-05-12 16:19:04 +0800645 /* Init mmio assets */
Mingyang Sun8d004f72021-06-01 10:46:26 +0800646 if (p_ldinf->nassets > 0) {
647 if (tfm_spm_partition_get_privileged_mode(p_ldinf->flags) ==
Mingyang Sun61f8fbc2021-06-04 17:49:56 +0800648 TFM_PARTITION_PRIVILEGED_MODE) {
649 privileged = true;
650 } else {
651 privileged = false;
652 }
653 }
654
Mingyang Sun8d004f72021-06-01 10:46:26 +0800655 p_asset_load = (struct asset_desc_t *)LOAD_INFO_ASSET(p_ldinf);
656 for (i = 0; i < p_ldinf->nassets; i++) {
Ken Liu86686282021-04-27 11:11:15 +0800657 /* Skip the memory-based asset */
Ken Liu4520ce32021-05-11 22:49:10 +0800658 if (!(p_asset_load[i].attr & ASSET_DEV_REF_BIT)) {
Ken Liu86686282021-04-27 11:11:15 +0800659 continue;
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100660 }
Ken Liu86686282021-04-27 11:11:15 +0800661
Ken Liuacd2a572021-05-12 16:19:04 +0800662 platform_data_p = REFERENCE_TO_PTR(p_asset_load[i].dev.addr_ref,
663 struct platform_data_t *);
Ken Liu25e09c72021-05-24 15:46:46 +0800664
665 /*
666 * TODO: some partitions declare MMIO not exist on specific
667 * platforms, and the platform defines a dummy NULL reference
668 * for these MMIO items, which cause 'nassets' to contain several
669 * NULL items. Skip these NULL items initialization temporarily to
670 * avoid HAL API panic.
671 * Eventually, these platform-specific partitions need to be moved
672 * into a platform-specific folder. Then this workaround can be
673 * removed.
674 */
675 if (!platform_data_p) {
676 continue;
677 }
678
Ken Liu86686282021-04-27 11:11:15 +0800679#ifdef TFM_FIH_PROFILE_ON
Mingyang Sun61f8fbc2021-06-04 17:49:56 +0800680 FIH_CALL(tfm_spm_hal_configure_default_isolation, fih_rc,
681 privileged, platform_data_p);
Ken Liu86686282021-04-27 11:11:15 +0800682 if (fih_not_eq(fih_rc, fih_int_encode(TFM_PLAT_ERR_SUCCESS))) {
683 tfm_core_panic();
684 }
685#else /* TFM_FIH_PROFILE_ON */
Mingyang Sun61f8fbc2021-06-04 17:49:56 +0800686 if (tfm_spm_hal_configure_default_isolation(privileged,
Ken Liu86686282021-04-27 11:11:15 +0800687 platform_data_p) != TFM_PLAT_ERR_SUCCESS) {
688 tfm_core_panic();
689 }
690#endif /* TFM_FIH_PROFILE_ON */
Mate Toth-Pal8ac98a72019-11-21 17:30:10 +0100691 }
692
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800693 partition->signals_allowed |= PSA_DOORBELL;
Shawn Shanc7dda0e2019-12-23 14:45:09 +0800694
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800695 tfm_event_init(&partition->event);
Ken Liu2c47f7f2021-01-22 11:06:04 +0800696 BI_LIST_INIT_NODE(&partition->msg_list);
Edison Ai764d41f2018-09-21 15:56:36 +0800697
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800698 pth = &partition->sp_thread;
Edison Ai764d41f2018-09-21 15:56:36 +0800699 if (!pth) {
Edison Ai9059ea02019-11-28 13:46:14 +0800700 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800701 }
702
Mingyang Sun8d004f72021-06-01 10:46:26 +0800703 /* Extendable partition load info is right after p_ldinf. */
Mingyang Sun2b352662021-04-21 11:35:43 +0800704 tfm_core_thrd_init(
705 pth,
Mingyang Sun8d004f72021-06-01 10:46:26 +0800706 POSITION_TO_ENTRY(p_ldinf->entry, tfm_core_thrd_entry_t),
Mingyang Sun2b352662021-04-21 11:35:43 +0800707 NULL,
Mingyang Sun8d004f72021-06-01 10:46:26 +0800708 LOAD_ALLOCED_STACK_ADDR(p_ldinf) + p_ldinf->stack_size,
709 LOAD_ALLOCED_STACK_ADDR(p_ldinf));
Edison Ai788bae22019-02-18 17:38:59 +0800710
Mingyang Sun8d004f72021-06-01 10:46:26 +0800711 pth->prior = TO_THREAD_PRIORITY(PARTITION_PRIORITY(p_ldinf->flags));
Edison Ai764d41f2018-09-21 15:56:36 +0800712
Mingyang Sun8d004f72021-06-01 10:46:26 +0800713 if (p_ldinf->pid == TFM_SP_NON_SECURE_ID) {
Ken Liu490281d2019-12-30 15:55:26 +0800714 p_ns_entry_thread = pth;
Ken Liu5248af22019-12-29 12:47:13 +0800715 pth->param = (void *)tfm_spm_hal_get_ns_entry_point();
Ken Liu490281d2019-12-30 15:55:26 +0800716 }
717
Edison Ai764d41f2018-09-21 15:56:36 +0800718 /* Kick off */
Summer Qin66f1e032020-01-06 15:40:03 +0800719 if (tfm_core_thrd_start(pth) != THRD_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800720 tfm_core_panic();
Edison Ai764d41f2018-09-21 15:56:36 +0800721 }
Edison Ai764d41f2018-09-21 15:56:36 +0800722 }
723
Ken Liu483f5da2019-04-24 10:45:21 +0800724 /*
725 * All threads initialized, start the scheduler.
726 *
727 * NOTE:
Ken Liu490281d2019-12-30 15:55:26 +0800728 * It is worthy to give the thread object to scheduler if the background
729 * context belongs to one of the threads. Here the background thread is the
730 * initialization thread who calls SPM SVC, which re-uses the non-secure
731 * entry thread's stack. After SPM initialization is done, this stack is
732 * cleaned up and the background context is never going to return. Tell
733 * the scheduler that the current thread is non-secure entry thread.
Ken Liu483f5da2019-04-24 10:45:21 +0800734 */
Summer Qin66f1e032020-01-06 15:40:03 +0800735 tfm_core_thrd_start_scheduler(p_ns_entry_thread);
Ken Liuce2692d2020-02-11 12:39:36 +0800736
Summer Qind2ad7e72020-01-06 18:16:35 +0800737 return p_ns_entry_thread->arch_ctx.lr;
Edison Ai764d41f2018-09-21 15:56:36 +0800738}
Ken Liu2d175172019-03-21 17:08:41 +0800739
Kevin Peng25b190b2020-10-30 17:10:45 +0800740#if TFM_LVL != 1
Kevin Peng64011942021-05-25 11:19:07 +0800741static void set_up_boundary(const struct partition_load_info_t *p_ldinf)
742{
743#if TFM_LVL == 3
David Huf07e97d2021-02-15 22:05:40 +0800744#if defined(TFM_FIH_PROFILE_ON) && (TFM_LVL == 3)
745 fih_int fih_rc = FIH_FAILURE;
746#endif
Kevin Peng64011942021-05-25 11:19:07 +0800747 /*
748 * FIXME: To implement isolations among partitions in isolation level 3,
749 * each partition needs to run in unprivileged mode. Currently some
750 * PRoTs cannot work in unprivileged mode, make them privileged now.
751 */
752 if (!(p_ldinf->flags & SPM_PART_FLAG_PSA_ROT)) {
753 struct asset_desc_t *p_asset =
754 (struct asset_desc_t *)LOAD_INFO_ASSET(p_ldinf);
755 /* Partition must have private data as the first asset in LVL3 */
756 if (p_ldinf->nassets == 0) {
757 tfm_core_panic();
758 }
759 if (p_asset->attr & ASSET_DEV_REF_BIT) {
760 tfm_core_panic();
761 }
762 /* FIXME: only MPU-based implementations are supported currently */
763#ifdef TFM_FIH_PROFILE_ON
764 FIH_CALL(tfm_hal_mpu_update_partition_boundary, fih_rc,
765 p_asset->mem.addr_x, p_asset->mem.addr_y);
766 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
767 tfm_core_panic();
768 }
769#else /* TFM_FIH_PROFILE_ON */
770 if (tfm_hal_mpu_update_partition_boundary(p_asset->mem.addr_x,
771 p_asset->mem.addr_y)
772 != TFM_HAL_SUCCESS) {
773 tfm_core_panic();
774 }
775#endif /* TFM_FIH_PROFILE_ON */
776 }
777#else /* TFM_LVL == 3 */
778 (void)p_ldinf;
779#endif /* TFM_LVL == 3 */
780}
781#endif /* TFM_LVL != 1 */
782
783void tfm_set_up_isolation_boundary(const struct partition_t *partition)
784{
785#if TFM_LVL != 1
786 const struct partition_load_info_t *p_ldinf;
787 uint32_t is_privileged;
788
789 p_ldinf = partition->p_ldinf;
790 is_privileged = p_ldinf->flags & SPM_PART_FLAG_PSA_ROT ?
791 TFM_PARTITION_PRIVILEGED_MODE :
792 TFM_PARTITION_UNPRIVILEGED_MODE;
793
794 tfm_spm_partition_change_privilege(is_privileged);
795
796 set_up_boundary(p_ldinf);
797#else /* TFM_LVL != 1 */
798 (void)partition;
799#endif /* TFM_LVL != 1 */
800}
801
802void tfm_pendsv_do_schedule(struct tfm_arch_ctx_t *p_actx)
803{
804 struct partition_t *p_next_partition;
805 struct tfm_core_thread_t *pth_next = tfm_core_thrd_get_next();
806 struct tfm_core_thread_t *pth_curr = tfm_core_thrd_get_curr();
Ken Liu2d175172019-03-21 17:08:41 +0800807
Mate Toth-Pal32b2ccd2019-04-26 10:00:16 +0200808 if (pth_next != NULL && pth_curr != pth_next) {
Ken Liuc25558c2021-05-20 15:31:28 +0800809 p_next_partition = TO_CONTAINER(pth_next,
810 struct partition_t,
811 sp_thread);
Kevin Peng64011942021-05-25 11:19:07 +0800812 tfm_set_up_isolation_boundary(p_next_partition);
Mate Toth-Palc430b992019-05-09 21:01:14 +0200813
Summer Qind2ad7e72020-01-06 18:16:35 +0800814 tfm_core_thrd_switch_context(p_actx, pth_curr, pth_next);
Ken Liu2d175172019-03-21 17:08:41 +0800815 }
David Hufb38d562019-09-23 15:58:34 +0800816
817 /*
818 * Handle pending mailbox message from NS in multi-core topology.
819 * Empty operation on single Armv8-M platform.
820 */
821 tfm_rpc_client_call_handler();
Ken Liu2d175172019-03-21 17:08:41 +0800822}
Mingyang Sund44522a2020-01-16 16:48:37 +0800823
Summer Qin02f7f072020-08-24 16:02:54 +0800824void update_caller_outvec_len(struct tfm_msg_body_t *msg)
Mingyang Sund44522a2020-01-16 16:48:37 +0800825{
826 uint32_t i;
827
828 /*
829 * FixeMe: abstract these part into dedicated functions to avoid
830 * accessing thread context in psa layer
831 */
832 /* If it is a NS request via RPC, the owner of this message is not set */
833 if (!is_tfm_rpc_msg(msg)) {
834 TFM_CORE_ASSERT(msg->ack_evnt.owner->state == THRD_STATE_BLOCK);
835 }
836
837 for (i = 0; i < PSA_MAX_IOVEC; i++) {
838 if (msg->msg.out_size[i] == 0) {
839 continue;
840 }
841
842 TFM_CORE_ASSERT(msg->caller_outvec[i].base == msg->outvec[i].base);
843
844 msg->caller_outvec[i].len = msg->outvec[i].len;
845 }
846}
847
Summer Qin02f7f072020-08-24 16:02:54 +0800848void notify_with_signal(int32_t partition_id, psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800849{
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800850 struct partition_t *partition = NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800851
852 /*
853 * The value of partition_id must be greater than zero as the target of
854 * notification must be a Secure Partition, providing a Non-secure
855 * Partition ID is a fatal error.
856 */
857 if (!TFM_CLIENT_ID_IS_S(partition_id)) {
858 tfm_core_panic();
859 }
860
861 /*
862 * It is a fatal error if partition_id does not correspond to a Secure
863 * Partition.
864 */
865 partition = tfm_spm_get_partition_by_id(partition_id);
866 if (!partition) {
867 tfm_core_panic();
868 }
869
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800870 partition->signals_asserted |= signal;
Mingyang Sund44522a2020-01-16 16:48:37 +0800871
Kevin Peng8dac6102021-03-09 16:44:00 +0800872 if (partition->signals_waiting & signal) {
873 tfm_event_wake(
874 &partition->event,
875 partition->signals_asserted & partition->signals_waiting);
876 partition->signals_waiting &= ~signal;
877 }
Mingyang Sund44522a2020-01-16 16:48:37 +0800878}
879
Kevin Pengeca45b92021-02-09 14:46:50 +0800880__attribute__((naked))
881static void tfm_flih_deprivileged_handling(uint32_t p_ldinf,
882 psa_flih_func flih_func,
883 psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800884{
Kevin Pengeca45b92021-02-09 14:46:50 +0800885 __ASM volatile("SVC %0 \n"
886 "BX LR \n"
887 : : "I" (TFM_SVC_PREPARE_DEPRIV_FLIH));
888}
Kevin Pengdc791882021-03-12 10:57:12 +0800889
Kevin Pengeca45b92021-02-09 14:46:50 +0800890void spm_interrupt_handler(struct partition_load_info_t *p_ldinf,
891 psa_signal_t signal,
892 uint32_t irq_line,
893 psa_flih_func flih_func)
894{
895 uint32_t pid;
896 psa_flih_result_t flih_result;
Kevin Pengdc791882021-03-12 10:57:12 +0800897
Kevin Pengeca45b92021-02-09 14:46:50 +0800898 pid = p_ldinf->pid;
899
900 if (flih_func == NULL) {
901 /* SLIH Model Handling */
902 __disable_irq();
903 tfm_spm_hal_disable_irq(irq_line);
904 notify_with_signal(pid, signal);
905 __enable_irq();
906 return;
907 }
908
909 /* FLIH Model Handling */
910 if (tfm_spm_partition_get_privileged_mode(p_ldinf->flags) ==
911 TFM_PARTITION_PRIVILEGED_MODE) {
912 flih_result = flih_func();
913 if (flih_result == PSA_FLIH_SIGNAL) {
914 __disable_irq();
915 notify_with_signal(pid, signal);
916 __enable_irq();
917 } else if (flih_result != PSA_FLIH_NO_SIGNAL) {
918 /*
919 * Nothing needed to do for PSA_FLIH_NO_SIGNAL
920 * But if the flih_result is invalid, should panic.
921 */
922 tfm_core_panic();
923 }
924 } else {
925 tfm_flih_deprivileged_handling((uint32_t)p_ldinf, flih_func, signal);
926 }
Mingyang Sund44522a2020-01-16 16:48:37 +0800927}
928
Kevin Peng27e42272021-05-24 17:58:53 +0800929struct irq_load_info_t *get_irq_info_for_signal(
930 const struct partition_load_info_t *p_ldinf,
931 psa_signal_t signal)
Mingyang Sund44522a2020-01-16 16:48:37 +0800932{
933 size_t i;
Kevin Peng27e42272021-05-24 17:58:53 +0800934 struct irq_load_info_t *irq_info;
Mingyang Sund44522a2020-01-16 16:48:37 +0800935
Ken Liu24dffb22021-02-10 11:03:58 +0800936 if (!IS_ONLY_ONE_BIT_IN_UINT32(signal)) {
Kevin Peng27e42272021-05-24 17:58:53 +0800937 return NULL;
Kevin Peng410bee52021-01-13 16:27:17 +0800938 }
939
Kevin Peng27e42272021-05-24 17:58:53 +0800940 irq_info = (struct irq_load_info_t *)LOAD_INFO_IRQ(p_ldinf);
941 for (i = 0; i < p_ldinf->nirqs; i++) {
942 if (irq_info[i].signal == signal) {
943 return &irq_info[i];
Mingyang Sund44522a2020-01-16 16:48:37 +0800944 }
945 }
Kevin Penga20b5af2021-01-11 11:20:52 +0800946
Kevin Peng27e42272021-05-24 17:58:53 +0800947 return NULL;
Mingyang Sund44522a2020-01-16 16:48:37 +0800948}
949
Summer Qindea1f2c2021-01-11 14:46:34 +0800950#if !defined(__ARM_ARCH_8_1M_MAIN__)
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800951void tfm_spm_validate_caller(struct partition_t *p_cur_sp, uint32_t *p_ctx,
952 uint32_t exc_return, bool ns_caller)
Mingyang Sund44522a2020-01-16 16:48:37 +0800953{
954 uintptr_t stacked_ctx_pos;
955
956 if (ns_caller) {
957 /*
958 * The background IRQ can't be supported, since if SP is executing,
959 * the preempted context of SP can be different with the one who
960 * preempts veneer.
961 */
Ken Liuacd2a572021-05-12 16:19:04 +0800962 if (p_cur_sp->p_ldinf->pid != TFM_SP_NON_SECURE_ID) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800963 tfm_core_panic();
964 }
965
966 /*
967 * It is non-secure caller, check if veneer stack contains
968 * multiple contexts.
969 */
970 stacked_ctx_pos = (uintptr_t)p_ctx +
971 sizeof(struct tfm_state_context_t) +
Ken Liu05e13ba2020-07-25 10:31:33 +0800972 TFM_STACK_SEALED_SIZE;
Mingyang Sund44522a2020-01-16 16:48:37 +0800973
974 if (is_stack_alloc_fp_space(exc_return)) {
Xinyu Zhang3a453242021-04-16 17:57:09 +0800975#if defined(__FPU_USED) && (__FPU_USED == 1U)
Mingyang Sund44522a2020-01-16 16:48:37 +0800976 if (FPU->FPCCR & FPU_FPCCR_TS_Msk) {
977 stacked_ctx_pos += TFM_ADDTIONAL_FP_CONTEXT_WORDS *
978 sizeof(uint32_t);
979 }
980#endif
981 stacked_ctx_pos += TFM_BASIC_FP_CONTEXT_WORDS * sizeof(uint32_t);
982 }
983
Mingyang Sunaf22ffa2020-07-09 17:48:37 +0800984 if (stacked_ctx_pos != p_cur_sp->sp_thread.stk_top) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800985 tfm_core_panic();
986 }
Ken Liuacd2a572021-05-12 16:19:04 +0800987 } else if (p_cur_sp->p_ldinf->pid <= 0) {
Mingyang Sund44522a2020-01-16 16:48:37 +0800988 tfm_core_panic();
989 }
990}
Summer Qindea1f2c2021-01-11 14:46:34 +0800991#endif
Summer Qin830c5542020-02-14 13:44:20 +0800992
993void tfm_spm_request_handler(const struct tfm_state_context_t *svc_ctx)
994{
995 uint32_t *res_ptr = (uint32_t *)&svc_ctx->r0;
996 uint32_t running_partition_flags = 0;
Mingyang Sunae70d8d2020-06-30 15:56:05 +0800997 const struct partition_t *partition = NULL;
Summer Qin830c5542020-02-14 13:44:20 +0800998
999 /* Check permissions on request type basis */
1000
1001 switch (svc_ctx->r0) {
1002 case TFM_SPM_REQUEST_RESET_VOTE:
1003 partition = tfm_spm_get_running_partition();
1004 if (!partition) {
1005 tfm_core_panic();
1006 }
Ken Liuacd2a572021-05-12 16:19:04 +08001007 running_partition_flags = partition->p_ldinf->flags;
Summer Qin830c5542020-02-14 13:44:20 +08001008
1009 /* Currently only PSA Root of Trust services are allowed to make Reset
1010 * vote request
1011 */
1012 if ((running_partition_flags & SPM_PART_FLAG_PSA_ROT) == 0) {
1013 *res_ptr = (uint32_t)TFM_ERROR_GENERIC;
1014 }
1015
1016 /* FixMe: this is a placeholder for checks to be performed before
1017 * allowing execution of reset
1018 */
1019 *res_ptr = (uint32_t)TFM_SUCCESS;
1020
1021 break;
1022 default:
1023 *res_ptr = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
1024 }
1025}