blob: 8950e54c178619d96c04626e75999b4a8a675173 [file] [log] [blame]
Mingyang Sundeae45d2021-09-06 15:31:07 +08001/*
shejia01a0ea10c2022-06-27 13:56:00 +08002 * Copyright (c) 2021-2023, Arm Limited. All rights reserved.
Chris Brandb4c2b002022-07-21 12:54:00 -07003 * Copyright (c) 2021-2022 Cypress Semiconductor Corporation (an Infineon
4 * company) or an affiliate of Cypress Semiconductor Corporation. All rights
5 * reserved.
Mingyang Sundeae45d2021-09-06 15:31:07 +08006 *
7 * SPDX-License-Identifier: BSD-3-Clause
8 *
9 */
10
11#include <stdint.h>
Sherry Zhang049733e2022-04-20 21:37:51 +080012#include "aapcs_local.h"
Mingyang Sun620c8562021-11-10 11:44:58 +080013#include "critical_section.h"
Ken Liue07c3b72021-10-14 16:19:13 +080014#include "compiler_ext_defs.h"
Xinyu Zhangcdbe3622022-10-31 14:34:25 +080015#include "config_spm.h"
Summer Qin596f5552022-01-27 18:04:06 +080016#include "runtime_defs.h"
Chris Brand30106ba2022-01-13 13:48:50 -080017#include "ffm/stack_watermark.h"
Sherry Zhangc7147022023-02-03 11:21:10 +080018#include "spm.h"
Sherry Zhang049733e2022-04-20 21:37:51 +080019#include "tfm_hal_memory_symbols.h"
Ken Liu62bae592021-10-19 22:15:43 +080020#include "tfm_hal_isolation.h"
Kevin Pengb288c522021-09-26 16:18:23 +080021#include "tfm_hal_platform.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080022#include "tfm_rpc.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080023#include "ffm/backend.h"
Ken Liu62bae592021-10-19 22:15:43 +080024#include "utilities.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080025#include "load/partition_defs.h"
26#include "load/service_defs.h"
27#include "load/spm_load_api.h"
28#include "psa/error.h"
Jianliang Shen9e389352023-02-09 16:58:08 +080029#include "internal_status_code.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080030
31/* Declare the global component list */
32struct partition_head_t partition_listhead;
33
Kevin Peng9f1a7542022-02-07 16:32:27 +080034#if CONFIG_TFM_PSA_API_CROSS_CALL == 1
Ken Liu63a176b2022-06-09 22:36:56 +080035/* Instance for SPM_THREAD_CONTEXT */
Ken Liue07c3b72021-10-14 16:19:13 +080036
Chris Brandfe5adca2022-11-08 17:44:07 -080037#ifdef CONFIG_TFM_USE_TRUSTZONE
38struct context_ctrl_t *p_spm_thread_context;
39#else
40/* If ns_agent_tz isn't used, we need to provide a stack for SPM to use */
Ken Liu63a176b2022-06-09 22:36:56 +080041static uint8_t spm_thread_stack[CONFIG_TFM_SPM_THREAD_STACK_SIZE] __aligned(8);
42ARCH_CLAIM_CTXCTRL_INSTANCE(spm_thread_context,
43 spm_thread_stack,
44 sizeof(spm_thread_stack));
45
Ken Liue07c3b72021-10-14 16:19:13 +080046struct context_ctrl_t *p_spm_thread_context = &spm_thread_context;
Ken Liue07c3b72021-10-14 16:19:13 +080047#endif
48
49#endif
50
Sherry Zhang049733e2022-04-20 21:37:51 +080051/* Indicator point to the partition meta */
52uintptr_t *partition_meta_indicator_pos;
53
54extern uint32_t scheduler_lock;
55
shejia0195a88bc2023-01-16 15:44:46 +080056/*
57 * Query the state of current thread.
58 */
59static uint32_t query_state(struct thread_t *p_thrd, uint32_t *p_retval)
60{
61 struct critical_section_t cs_signal = CRITICAL_SECTION_STATIC_INIT;
62 struct partition_t *p_pt = NULL;
63 uint32_t state = p_thrd->state;
64 psa_signal_t signal_ret = 0;
65
66 /* Get current partition of thread. */
67 p_pt = TO_CONTAINER(p_thrd->p_context_ctrl,
68 struct partition_t, ctx_ctrl);
69
70 CRITICAL_SECTION_ENTER(cs_signal);
71
72 signal_ret = p_pt->signals_waiting & p_pt->signals_asserted;
73
74 if (signal_ret) {
75 /*
76 * If the partition is waiting some signals and any of them is asserted,
77 * change thread to be THRD_STATE_RET_VAL_AVAIL and fill the retval. If
78 * the waiting signal is TFM_IPC_REPLY_SIGNAL, it means the Secure
79 * Partition is waiting for the services to be fulfilled, then the
80 * return value comes from the backend_replying() by the server
81 * Partition. For other waiting signals by psa_wait(), the return value
82 * is just the signal.
83 */
84 if (signal_ret == TFM_IPC_REPLY_SIGNAL) {
85 p_pt->signals_asserted &= ~TFM_IPC_REPLY_SIGNAL;
86 *p_retval = (uint32_t)p_pt->reply_value;
87 } else {
88 *p_retval = signal_ret;
89 }
90
91 p_pt->signals_waiting = 0;
92 state = THRD_STATE_RET_VAL_AVAIL;
93 } else if (p_pt->signals_waiting != 0) {
94 /*
95 * If the thread is waiting some signals but none of them is asserted,
96 * block the thread.
97 */
98 state = THRD_STATE_BLOCK;
99 }
100
101 CRITICAL_SECTION_LEAVE(cs_signal);
102 return state;
103}
104
Sherry Zhangef49b1d2023-02-07 14:08:40 +0800105extern struct psa_api_tbl_t psa_api_cross;
106extern struct psa_api_tbl_t psa_api_svc;
107
Summer Qin596f5552022-01-27 18:04:06 +0800108static void prv_process_metadata(struct partition_t *p_pt)
109{
Kevin Peng43160d52022-02-11 13:35:56 +0800110 const struct partition_load_info_t *p_pt_ldi;
111 const struct service_load_info_t *p_srv_ldi;
112 struct context_ctrl_t *ctx_ctrl;
113 struct runtime_metadata_t *p_rt_meta;
114 service_fn_t *p_sfn_table;
115 uint32_t allocate_size;
Summer Qin596f5552022-01-27 18:04:06 +0800116
Kevin Peng43160d52022-02-11 13:35:56 +0800117 p_pt_ldi = p_pt->p_ldinf;
Chris Brand1fb796d2022-10-18 16:54:25 -0700118 p_srv_ldi = LOAD_INFO_SERVICE(p_pt_ldi);
Kevin Peng43160d52022-02-11 13:35:56 +0800119 ctx_ctrl = &p_pt->ctx_ctrl;
120
121 /* common runtime metadata */
122 allocate_size = sizeof(*p_rt_meta);
123
124 if (!IS_PARTITION_IPC_MODEL(p_pt_ldi)) {
125 /* SFN specific metadata - SFN function table */
126 allocate_size += sizeof(service_fn_t) * p_pt_ldi->nservices;
Summer Qin596f5552022-01-27 18:04:06 +0800127 }
128
Kevin Peng43160d52022-02-11 13:35:56 +0800129 ARCH_CTXCTRL_ALLOCATE_STACK(ctx_ctrl, allocate_size);
130 p_rt_meta = (struct runtime_metadata_t *)
131 ARCH_CTXCTRL_ALLOCATED_PTR(ctx_ctrl);
132
133 p_rt_meta->entry = p_pt_ldi->entry;
Sherry Zhangef49b1d2023-02-07 14:08:40 +0800134#if TFM_LVL == 1
135 p_rt_meta->psa_fns = &psa_api_cross;
136#else
137 /* TODO: ABI for PRoT partitions needs to be updated based on implementations. */
138 p_rt_meta->psa_fns = &psa_api_svc;
139#endif
Kevin Peng43160d52022-02-11 13:35:56 +0800140 p_rt_meta->n_sfn = 0;
141 p_sfn_table = p_rt_meta->sfn_table;
142
143 if (!IS_PARTITION_IPC_MODEL(p_pt_ldi)) {
144 /* SFN table. The signal bit of the service is the same index of SFN. */
145 for (int i = 0; i < p_pt_ldi->nservices; i++) {
146 p_sfn_table[i] = (service_fn_t)p_srv_ldi[i].sfn;
147 }
148
149 p_rt_meta->n_sfn = p_pt_ldi->nservices;
150 }
151
152 p_pt->p_metadata = (void *)p_rt_meta;
Summer Qin596f5552022-01-27 18:04:06 +0800153}
154
Mingyang Sundeae45d2021-09-06 15:31:07 +0800155/*
156 * Send message and wake up the SP who is waiting on message queue, block the
Ken Liuf39d8eb2021-10-07 12:55:33 +0800157 * current thread and trigger scheduler.
Mingyang Sundeae45d2021-09-06 15:31:07 +0800158 */
Ken Liu995a9742022-05-18 19:28:30 +0800159psa_status_t backend_messaging(struct service_t *service,
Ken Liuc9313eb2023-02-22 15:45:54 +0800160 struct connection_t *handle)
Mingyang Sundeae45d2021-09-06 15:31:07 +0800161{
162 struct partition_t *p_owner = NULL;
163 psa_signal_t signal = 0;
Jianliang Shen9e389352023-02-09 16:58:08 +0800164 psa_status_t ret = PSA_SUCCESS;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800165
Mingyang Suna09adda2022-02-16 18:11:33 +0800166 if (!handle || !service || !service->p_ldinf || !service->partition) {
Mingyang Sunbb4a42a2021-12-14 15:18:52 +0800167 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800168 }
169
170 p_owner = service->partition;
171 signal = service->p_ldinf->signal;
172
Mingyang Suna09adda2022-02-16 18:11:33 +0800173 UNI_LIST_INSERT_AFTER(p_owner, handle, p_handles);
Mingyang Sundeae45d2021-09-06 15:31:07 +0800174
175 /* Messages put. Update signals */
Jianliang Shen9e389352023-02-09 16:58:08 +0800176 ret = backend_assert_signal(p_owner, signal);
Mingyang Sundeae45d2021-09-06 15:31:07 +0800177
178 /*
179 * If it is a NS request via RPC, it is unnecessary to block current
180 * thread.
181 */
182
Mingyang Suna09adda2022-02-16 18:11:33 +0800183 if (!is_tfm_rpc_msg(handle)) {
Jianliang Shen9e389352023-02-09 16:58:08 +0800184 ret = backend_wait_signals(handle->p_client, TFM_IPC_REPLY_SIGNAL);
185 } else {
186 ret = PSA_SUCCESS;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800187 }
188
Mingyang Sunaeca8e02022-02-24 14:47:56 +0800189 handle->status = TFM_HANDLE_STATUS_ACTIVE;
190
Jianliang Shen9e389352023-02-09 16:58:08 +0800191 return ret;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800192}
193
Ken Liuc9313eb2023-02-22 15:45:54 +0800194psa_status_t backend_replying(struct connection_t *handle, int32_t status)
Ken Liu802a3702021-10-15 12:09:56 +0800195{
Mingyang Suna09adda2022-02-16 18:11:33 +0800196 if (is_tfm_rpc_msg(handle)) {
197 tfm_rpc_client_call_reply(handle, status);
Ken Liu802a3702021-10-15 12:09:56 +0800198 } else {
shejia0195a88bc2023-01-16 15:44:46 +0800199 handle->p_client->reply_value = (uintptr_t)status;
Jianliang Shen9e389352023-02-09 16:58:08 +0800200 return backend_assert_signal(handle->p_client, TFM_IPC_REPLY_SIGNAL);
Ken Liu802a3702021-10-15 12:09:56 +0800201 }
Ken Liuf39d8eb2021-10-07 12:55:33 +0800202
203 /*
204 * 'psa_reply' exists in IPC model only and returns 'void'. Return
205 * 'PSA_SUCCESS' here always since SPM does not forward the status
206 * to the caller.
207 */
208 return PSA_SUCCESS;
Ken Liu802a3702021-10-15 12:09:56 +0800209}
210
Summer Qin596f5552022-01-27 18:04:06 +0800211extern void sprt_main(void);
212
Mingyang Sundeae45d2021-09-06 15:31:07 +0800213/* Parameters are treated as assuredly */
Ken Liu995a9742022-05-18 19:28:30 +0800214void backend_init_comp_assuredly(struct partition_t *p_pt,
215 uint32_t service_setting)
Mingyang Sundeae45d2021-09-06 15:31:07 +0800216{
217 const struct partition_load_info_t *p_pldi = p_pt->p_ldinf;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800218
Kevin Peng613b4172022-02-15 14:41:44 +0800219#if CONFIG_TFM_DOORBELL_API == 1
220 p_pt->signals_allowed |= PSA_DOORBELL;
221#endif /* CONFIG_TFM_DOORBELL_API == 1 */
222
223 p_pt->signals_allowed |= service_setting;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800224
Ken Liu0bed7e02022-02-10 12:38:07 +0800225 UNI_LISI_INIT_NODE(p_pt, p_handles);
Mingyang Sundeae45d2021-09-06 15:31:07 +0800226
Ken Liubf4681f2022-02-11 11:15:03 +0800227 ARCH_CTXCTRL_INIT(&p_pt->ctx_ctrl,
228 LOAD_ALLOCED_STACK_ADDR(p_pldi),
229 p_pldi->stack_size);
230
Chris Brand30106ba2022-01-13 13:48:50 -0800231 watermark_stack(p_pt);
232
Summer Qin596f5552022-01-27 18:04:06 +0800233 prv_process_metadata(p_pt);
234
Mingyang Sundeae45d2021-09-06 15:31:07 +0800235 THRD_INIT(&p_pt->thrd, &p_pt->ctx_ctrl,
236 TO_THREAD_PRIORITY(PARTITION_PRIORITY(p_pldi->flags)));
237
Chris Brandfe5adca2022-11-08 17:44:07 -0800238#if (CONFIG_TFM_PSA_API_CROSS_CALL == 1) && defined(CONFIG_TFM_USE_TRUSTZONE)
Chris Brandb4c2b002022-07-21 12:54:00 -0700239 if (IS_PARTITION_NS_AGENT(p_pldi)) {
Chris Brandfe5adca2022-11-08 17:44:07 -0800240 /* Get the context from ns_agent_tz */
241 if (p_pldi->pid == 0) {
242 SPM_THREAD_CONTEXT = &p_pt->ctx_ctrl;
243 }
Mingyang Sundeae45d2021-09-06 15:31:07 +0800244 }
Summer Qin95444822022-01-27 11:22:00 +0800245#endif
Mingyang Sundeae45d2021-09-06 15:31:07 +0800246
247 thrd_start(&p_pt->thrd,
Summer Qin596f5552022-01-27 18:04:06 +0800248 POSITION_TO_ENTRY(sprt_main, thrd_fn_t),
Ken Liubf4681f2022-02-11 11:15:03 +0800249 THRD_GENERAL_EXIT);
Mingyang Sundeae45d2021-09-06 15:31:07 +0800250}
251
Ken Liu995a9742022-05-18 19:28:30 +0800252uint32_t backend_system_run(void)
Mingyang Sundeae45d2021-09-06 15:31:07 +0800253{
Ken Liu62bae592021-10-19 22:15:43 +0800254 uint32_t control;
255 struct partition_t *p_cur_pt;
Xinyu Zhang6ad07032022-08-10 14:45:56 +0800256 fih_int fih_rc = FIH_FAILURE;
Ken Liu62bae592021-10-19 22:15:43 +0800257
Sherry Zhang68681642022-06-24 13:36:33 +0800258#if CONFIG_TFM_PSA_API_CROSS_CALL == 1
Summer Qin1056d1c2022-10-19 16:07:15 +0800259 SPM_ASSERT(SPM_THREAD_CONTEXT);
Chris Brand3778bc12021-12-15 17:01:05 -0800260#endif
261
shejia0195a88bc2023-01-16 15:44:46 +0800262 /* Init thread callback function. */
263 thrd_set_query_callback(query_state);
264
Sherry Zhang049733e2022-04-20 21:37:51 +0800265 partition_meta_indicator_pos = (uintptr_t *)hal_mem_sp_meta_start;
Ken Liu62bae592021-10-19 22:15:43 +0800266 control = thrd_start_scheduler(&CURRENT_THREAD);
267
268 p_cur_pt = TO_CONTAINER(CURRENT_THREAD->p_context_ctrl,
269 struct partition_t, ctx_ctrl);
270
Xinyu Zhang6ad07032022-08-10 14:45:56 +0800271 FIH_CALL(tfm_hal_activate_boundary, fih_rc, p_cur_pt->p_ldinf, p_cur_pt->boundary);
272 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
Ken Liu62bae592021-10-19 22:15:43 +0800273 tfm_core_panic();
274 }
275
276 return control;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800277}
278
Jianliang Shen9e389352023-02-09 16:58:08 +0800279psa_status_t backend_wait_signals(struct partition_t *p_pt, psa_signal_t signals)
Kevin Pengdef92de2021-11-10 16:14:48 +0800280{
shejia0195a88bc2023-01-16 15:44:46 +0800281 struct critical_section_t cs_signal = CRITICAL_SECTION_STATIC_INIT;
Jianliang Shen9e389352023-02-09 16:58:08 +0800282 psa_status_t ret = PSA_SUCCESS;
Kevin Pengdef92de2021-11-10 16:14:48 +0800283
shejia0195a88bc2023-01-16 15:44:46 +0800284 if (!p_pt) {
285 tfm_core_panic();
Mingyang Sun5c9529f2022-03-15 17:51:56 +0800286 }
shejia0195a88bc2023-01-16 15:44:46 +0800287
288 CRITICAL_SECTION_ENTER(cs_signal);
289
Jianliang Shen9e389352023-02-09 16:58:08 +0800290 ret = p_pt->signals_asserted & signals;
291 if (ret == 0) {
shejia0195a88bc2023-01-16 15:44:46 +0800292 p_pt->signals_waiting = signals;
Jianliang Shen9e389352023-02-09 16:58:08 +0800293 ret = STATUS_NEED_SCHEDULE;
shejia0195a88bc2023-01-16 15:44:46 +0800294 }
295
296 CRITICAL_SECTION_LEAVE(cs_signal);
Mingyang Sun5c9529f2022-03-15 17:51:56 +0800297
Jianliang Shen9e389352023-02-09 16:58:08 +0800298 return ret;
Kevin Pengdef92de2021-11-10 16:14:48 +0800299}
300
Jianliang Shen9e389352023-02-09 16:58:08 +0800301psa_status_t backend_assert_signal(struct partition_t *p_pt, psa_signal_t signal)
Kevin Pengdef92de2021-11-10 16:14:48 +0800302{
shejia0195a88bc2023-01-16 15:44:46 +0800303 struct critical_section_t cs_signal = CRITICAL_SECTION_STATIC_INIT;
Jianliang Shen9e389352023-02-09 16:58:08 +0800304 psa_status_t ret = PSA_SUCCESS;
shejia0195a88bc2023-01-16 15:44:46 +0800305
306 if (!p_pt) {
307 tfm_core_panic();
shejia01a0ea10c2022-06-27 13:56:00 +0800308 }
shejia0195a88bc2023-01-16 15:44:46 +0800309
310 CRITICAL_SECTION_ENTER(cs_signal);
311 p_pt->signals_asserted |= signal;
Jianliang Shen9e389352023-02-09 16:58:08 +0800312
313 if (p_pt->signals_asserted & p_pt->signals_waiting) {
314 ret = STATUS_NEED_SCHEDULE;
315 }
shejia0195a88bc2023-01-16 15:44:46 +0800316 CRITICAL_SECTION_LEAVE(cs_signal);
317
Jianliang Shen9e389352023-02-09 16:58:08 +0800318 return ret;
Kevin Pengdef92de2021-11-10 16:14:48 +0800319}
320
Sherry Zhang049733e2022-04-20 21:37:51 +0800321uint64_t ipc_schedule(void)
322{
Xinyu Zhang6ad07032022-08-10 14:45:56 +0800323 fih_int fih_rc = FIH_FAILURE;
Sherry Zhang049733e2022-04-20 21:37:51 +0800324 AAPCS_DUAL_U32_T ctx_ctrls;
325 struct partition_t *p_part_curr, *p_part_next;
326 struct context_ctrl_t *p_curr_ctx;
327 struct thread_t *pth_next = thrd_next();
328 struct critical_section_t cs = CRITICAL_SECTION_STATIC_INIT;
329
330 p_curr_ctx = (struct context_ctrl_t *)(CURRENT_THREAD->p_context_ctrl);
331
332 AAPCS_DUAL_U32_SET(ctx_ctrls, (uint32_t)p_curr_ctx, (uint32_t)p_curr_ctx);
333
334 p_part_curr = GET_CURRENT_COMPONENT();
335 p_part_next = GET_THRD_OWNER(pth_next);
336
337 if (scheduler_lock != SCHEDULER_LOCKED && pth_next != NULL &&
338 p_part_curr != p_part_next) {
339 /* Check if there is enough room on stack to save more context */
340 if ((p_curr_ctx->sp_limit +
341 sizeof(struct tfm_additional_context_t)) > __get_PSP()) {
342 tfm_core_panic();
343 }
344
345 CRITICAL_SECTION_ENTER(cs);
346 /*
347 * If required, let the platform update boundary based on its
348 * implementation. Change privilege, MPU or other configurations.
349 */
Chendi Sun0f7d2822022-10-28 12:24:12 +0800350 if (tfm_hal_boundary_need_switch(p_part_curr->boundary,
351 p_part_next->boundary)) {
Xinyu Zhang6ad07032022-08-10 14:45:56 +0800352 FIH_CALL(tfm_hal_activate_boundary, fih_rc,
353 p_part_next->p_ldinf, p_part_next->boundary);
354 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
Sherry Zhang049733e2022-04-20 21:37:51 +0800355 tfm_core_panic();
356 }
357 }
358 ARCH_FLUSH_FP_CONTEXT();
359
360 AAPCS_DUAL_U32_SET_A1(ctx_ctrls, (uint32_t)pth_next->p_context_ctrl);
361
362 CURRENT_THREAD = pth_next;
363 CRITICAL_SECTION_LEAVE(cs);
364 }
365
366 /* Update meta indicator */
367 if (partition_meta_indicator_pos && (p_part_next->p_metadata)) {
368 *partition_meta_indicator_pos = (uintptr_t)(p_part_next->p_metadata);
369 }
370 return AAPCS_DUAL_U32_AS_U64(ctx_ctrls);
371}