blob: 07b01f56004a1da8e250ea327cdba42d327b814d [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
Summer Qin596f5552022-01-27 18:04:06 +0800105static void prv_process_metadata(struct partition_t *p_pt)
106{
Kevin Peng43160d52022-02-11 13:35:56 +0800107 const struct partition_load_info_t *p_pt_ldi;
108 const struct service_load_info_t *p_srv_ldi;
109 struct context_ctrl_t *ctx_ctrl;
110 struct runtime_metadata_t *p_rt_meta;
111 service_fn_t *p_sfn_table;
112 uint32_t allocate_size;
Summer Qin596f5552022-01-27 18:04:06 +0800113
Kevin Peng43160d52022-02-11 13:35:56 +0800114 p_pt_ldi = p_pt->p_ldinf;
Chris Brand1fb796d2022-10-18 16:54:25 -0700115 p_srv_ldi = LOAD_INFO_SERVICE(p_pt_ldi);
Kevin Peng43160d52022-02-11 13:35:56 +0800116 ctx_ctrl = &p_pt->ctx_ctrl;
117
118 /* common runtime metadata */
119 allocate_size = sizeof(*p_rt_meta);
120
121 if (!IS_PARTITION_IPC_MODEL(p_pt_ldi)) {
122 /* SFN specific metadata - SFN function table */
123 allocate_size += sizeof(service_fn_t) * p_pt_ldi->nservices;
Summer Qin596f5552022-01-27 18:04:06 +0800124 }
125
Kevin Peng43160d52022-02-11 13:35:56 +0800126 ARCH_CTXCTRL_ALLOCATE_STACK(ctx_ctrl, allocate_size);
127 p_rt_meta = (struct runtime_metadata_t *)
128 ARCH_CTXCTRL_ALLOCATED_PTR(ctx_ctrl);
129
130 p_rt_meta->entry = p_pt_ldi->entry;
131 p_rt_meta->n_sfn = 0;
132 p_sfn_table = p_rt_meta->sfn_table;
133
134 if (!IS_PARTITION_IPC_MODEL(p_pt_ldi)) {
135 /* SFN table. The signal bit of the service is the same index of SFN. */
136 for (int i = 0; i < p_pt_ldi->nservices; i++) {
137 p_sfn_table[i] = (service_fn_t)p_srv_ldi[i].sfn;
138 }
139
140 p_rt_meta->n_sfn = p_pt_ldi->nservices;
141 }
142
143 p_pt->p_metadata = (void *)p_rt_meta;
Summer Qin596f5552022-01-27 18:04:06 +0800144}
145
Mingyang Sundeae45d2021-09-06 15:31:07 +0800146/*
147 * Send message and wake up the SP who is waiting on message queue, block the
Ken Liuf39d8eb2021-10-07 12:55:33 +0800148 * current thread and trigger scheduler.
Mingyang Sundeae45d2021-09-06 15:31:07 +0800149 */
Ken Liu995a9742022-05-18 19:28:30 +0800150psa_status_t backend_messaging(struct service_t *service,
Ken Liuc9313eb2023-02-22 15:45:54 +0800151 struct connection_t *handle)
Mingyang Sundeae45d2021-09-06 15:31:07 +0800152{
153 struct partition_t *p_owner = NULL;
154 psa_signal_t signal = 0;
Jianliang Shen9e389352023-02-09 16:58:08 +0800155 psa_status_t ret = PSA_SUCCESS;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800156
Mingyang Suna09adda2022-02-16 18:11:33 +0800157 if (!handle || !service || !service->p_ldinf || !service->partition) {
Mingyang Sunbb4a42a2021-12-14 15:18:52 +0800158 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800159 }
160
161 p_owner = service->partition;
162 signal = service->p_ldinf->signal;
163
Mingyang Suna09adda2022-02-16 18:11:33 +0800164 UNI_LIST_INSERT_AFTER(p_owner, handle, p_handles);
Mingyang Sundeae45d2021-09-06 15:31:07 +0800165
166 /* Messages put. Update signals */
Jianliang Shen9e389352023-02-09 16:58:08 +0800167 ret = backend_assert_signal(p_owner, signal);
Mingyang Sundeae45d2021-09-06 15:31:07 +0800168
169 /*
170 * If it is a NS request via RPC, it is unnecessary to block current
171 * thread.
172 */
173
Mingyang Suna09adda2022-02-16 18:11:33 +0800174 if (!is_tfm_rpc_msg(handle)) {
Jianliang Shen9e389352023-02-09 16:58:08 +0800175 ret = backend_wait_signals(handle->p_client, TFM_IPC_REPLY_SIGNAL);
176 } else {
177 ret = PSA_SUCCESS;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800178 }
179
Mingyang Sunaeca8e02022-02-24 14:47:56 +0800180 handle->status = TFM_HANDLE_STATUS_ACTIVE;
181
Jianliang Shen9e389352023-02-09 16:58:08 +0800182 return ret;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800183}
184
Ken Liuc9313eb2023-02-22 15:45:54 +0800185psa_status_t backend_replying(struct connection_t *handle, int32_t status)
Ken Liu802a3702021-10-15 12:09:56 +0800186{
Mingyang Suna09adda2022-02-16 18:11:33 +0800187 if (is_tfm_rpc_msg(handle)) {
188 tfm_rpc_client_call_reply(handle, status);
Ken Liu802a3702021-10-15 12:09:56 +0800189 } else {
shejia0195a88bc2023-01-16 15:44:46 +0800190 handle->p_client->reply_value = (uintptr_t)status;
Jianliang Shen9e389352023-02-09 16:58:08 +0800191 return backend_assert_signal(handle->p_client, TFM_IPC_REPLY_SIGNAL);
Ken Liu802a3702021-10-15 12:09:56 +0800192 }
Ken Liuf39d8eb2021-10-07 12:55:33 +0800193
194 /*
195 * 'psa_reply' exists in IPC model only and returns 'void'. Return
196 * 'PSA_SUCCESS' here always since SPM does not forward the status
197 * to the caller.
198 */
199 return PSA_SUCCESS;
Ken Liu802a3702021-10-15 12:09:56 +0800200}
201
Summer Qin596f5552022-01-27 18:04:06 +0800202extern void sprt_main(void);
203
Mingyang Sundeae45d2021-09-06 15:31:07 +0800204/* Parameters are treated as assuredly */
Ken Liu995a9742022-05-18 19:28:30 +0800205void backend_init_comp_assuredly(struct partition_t *p_pt,
206 uint32_t service_setting)
Mingyang Sundeae45d2021-09-06 15:31:07 +0800207{
208 const struct partition_load_info_t *p_pldi = p_pt->p_ldinf;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800209
Kevin Peng613b4172022-02-15 14:41:44 +0800210#if CONFIG_TFM_DOORBELL_API == 1
211 p_pt->signals_allowed |= PSA_DOORBELL;
212#endif /* CONFIG_TFM_DOORBELL_API == 1 */
213
214 p_pt->signals_allowed |= service_setting;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800215
Ken Liu0bed7e02022-02-10 12:38:07 +0800216 UNI_LISI_INIT_NODE(p_pt, p_handles);
Mingyang Sundeae45d2021-09-06 15:31:07 +0800217
Ken Liubf4681f2022-02-11 11:15:03 +0800218 ARCH_CTXCTRL_INIT(&p_pt->ctx_ctrl,
219 LOAD_ALLOCED_STACK_ADDR(p_pldi),
220 p_pldi->stack_size);
221
Chris Brand30106ba2022-01-13 13:48:50 -0800222 watermark_stack(p_pt);
223
Summer Qin596f5552022-01-27 18:04:06 +0800224 prv_process_metadata(p_pt);
225
Mingyang Sundeae45d2021-09-06 15:31:07 +0800226 THRD_INIT(&p_pt->thrd, &p_pt->ctx_ctrl,
227 TO_THREAD_PRIORITY(PARTITION_PRIORITY(p_pldi->flags)));
228
Chris Brandfe5adca2022-11-08 17:44:07 -0800229#if (CONFIG_TFM_PSA_API_CROSS_CALL == 1) && defined(CONFIG_TFM_USE_TRUSTZONE)
Chris Brandb4c2b002022-07-21 12:54:00 -0700230 if (IS_PARTITION_NS_AGENT(p_pldi)) {
Chris Brandfe5adca2022-11-08 17:44:07 -0800231 /* Get the context from ns_agent_tz */
232 if (p_pldi->pid == 0) {
233 SPM_THREAD_CONTEXT = &p_pt->ctx_ctrl;
234 }
Mingyang Sundeae45d2021-09-06 15:31:07 +0800235 }
Summer Qin95444822022-01-27 11:22:00 +0800236#endif
Mingyang Sundeae45d2021-09-06 15:31:07 +0800237
238 thrd_start(&p_pt->thrd,
Summer Qin596f5552022-01-27 18:04:06 +0800239 POSITION_TO_ENTRY(sprt_main, thrd_fn_t),
Ken Liubf4681f2022-02-11 11:15:03 +0800240 THRD_GENERAL_EXIT);
Mingyang Sundeae45d2021-09-06 15:31:07 +0800241}
242
Ken Liu995a9742022-05-18 19:28:30 +0800243uint32_t backend_system_run(void)
Mingyang Sundeae45d2021-09-06 15:31:07 +0800244{
Ken Liu62bae592021-10-19 22:15:43 +0800245 uint32_t control;
246 struct partition_t *p_cur_pt;
Xinyu Zhang6ad07032022-08-10 14:45:56 +0800247 fih_int fih_rc = FIH_FAILURE;
Ken Liu62bae592021-10-19 22:15:43 +0800248
Sherry Zhang68681642022-06-24 13:36:33 +0800249#if CONFIG_TFM_PSA_API_CROSS_CALL == 1
Summer Qin1056d1c2022-10-19 16:07:15 +0800250 SPM_ASSERT(SPM_THREAD_CONTEXT);
Chris Brand3778bc12021-12-15 17:01:05 -0800251#endif
252
shejia0195a88bc2023-01-16 15:44:46 +0800253 /* Init thread callback function. */
254 thrd_set_query_callback(query_state);
255
Sherry Zhang049733e2022-04-20 21:37:51 +0800256 partition_meta_indicator_pos = (uintptr_t *)hal_mem_sp_meta_start;
Ken Liu62bae592021-10-19 22:15:43 +0800257 control = thrd_start_scheduler(&CURRENT_THREAD);
258
259 p_cur_pt = TO_CONTAINER(CURRENT_THREAD->p_context_ctrl,
260 struct partition_t, ctx_ctrl);
261
Xinyu Zhang6ad07032022-08-10 14:45:56 +0800262 FIH_CALL(tfm_hal_activate_boundary, fih_rc, p_cur_pt->p_ldinf, p_cur_pt->boundary);
263 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
Ken Liu62bae592021-10-19 22:15:43 +0800264 tfm_core_panic();
265 }
266
267 return control;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800268}
269
Jianliang Shen9e389352023-02-09 16:58:08 +0800270psa_status_t backend_wait_signals(struct partition_t *p_pt, psa_signal_t signals)
Kevin Pengdef92de2021-11-10 16:14:48 +0800271{
shejia0195a88bc2023-01-16 15:44:46 +0800272 struct critical_section_t cs_signal = CRITICAL_SECTION_STATIC_INIT;
Jianliang Shen9e389352023-02-09 16:58:08 +0800273 psa_status_t ret = PSA_SUCCESS;
Kevin Pengdef92de2021-11-10 16:14:48 +0800274
shejia0195a88bc2023-01-16 15:44:46 +0800275 if (!p_pt) {
276 tfm_core_panic();
Mingyang Sun5c9529f2022-03-15 17:51:56 +0800277 }
shejia0195a88bc2023-01-16 15:44:46 +0800278
279 CRITICAL_SECTION_ENTER(cs_signal);
280
Jianliang Shen9e389352023-02-09 16:58:08 +0800281 ret = p_pt->signals_asserted & signals;
282 if (ret == 0) {
shejia0195a88bc2023-01-16 15:44:46 +0800283 p_pt->signals_waiting = signals;
Jianliang Shen9e389352023-02-09 16:58:08 +0800284 ret = STATUS_NEED_SCHEDULE;
shejia0195a88bc2023-01-16 15:44:46 +0800285 }
286
287 CRITICAL_SECTION_LEAVE(cs_signal);
Mingyang Sun5c9529f2022-03-15 17:51:56 +0800288
Jianliang Shen9e389352023-02-09 16:58:08 +0800289 return ret;
Kevin Pengdef92de2021-11-10 16:14:48 +0800290}
291
Jianliang Shen9e389352023-02-09 16:58:08 +0800292psa_status_t backend_assert_signal(struct partition_t *p_pt, psa_signal_t signal)
Kevin Pengdef92de2021-11-10 16:14:48 +0800293{
shejia0195a88bc2023-01-16 15:44:46 +0800294 struct critical_section_t cs_signal = CRITICAL_SECTION_STATIC_INIT;
Jianliang Shen9e389352023-02-09 16:58:08 +0800295 psa_status_t ret = PSA_SUCCESS;
shejia0195a88bc2023-01-16 15:44:46 +0800296
297 if (!p_pt) {
298 tfm_core_panic();
shejia01a0ea10c2022-06-27 13:56:00 +0800299 }
shejia0195a88bc2023-01-16 15:44:46 +0800300
301 CRITICAL_SECTION_ENTER(cs_signal);
302 p_pt->signals_asserted |= signal;
Jianliang Shen9e389352023-02-09 16:58:08 +0800303
304 if (p_pt->signals_asserted & p_pt->signals_waiting) {
305 ret = STATUS_NEED_SCHEDULE;
306 }
shejia0195a88bc2023-01-16 15:44:46 +0800307 CRITICAL_SECTION_LEAVE(cs_signal);
308
Jianliang Shen9e389352023-02-09 16:58:08 +0800309 return ret;
Kevin Pengdef92de2021-11-10 16:14:48 +0800310}
311
Sherry Zhang049733e2022-04-20 21:37:51 +0800312uint64_t ipc_schedule(void)
313{
Xinyu Zhang6ad07032022-08-10 14:45:56 +0800314 fih_int fih_rc = FIH_FAILURE;
Sherry Zhang049733e2022-04-20 21:37:51 +0800315 AAPCS_DUAL_U32_T ctx_ctrls;
316 struct partition_t *p_part_curr, *p_part_next;
317 struct context_ctrl_t *p_curr_ctx;
318 struct thread_t *pth_next = thrd_next();
319 struct critical_section_t cs = CRITICAL_SECTION_STATIC_INIT;
320
321 p_curr_ctx = (struct context_ctrl_t *)(CURRENT_THREAD->p_context_ctrl);
322
323 AAPCS_DUAL_U32_SET(ctx_ctrls, (uint32_t)p_curr_ctx, (uint32_t)p_curr_ctx);
324
325 p_part_curr = GET_CURRENT_COMPONENT();
326 p_part_next = GET_THRD_OWNER(pth_next);
327
328 if (scheduler_lock != SCHEDULER_LOCKED && pth_next != NULL &&
329 p_part_curr != p_part_next) {
330 /* Check if there is enough room on stack to save more context */
331 if ((p_curr_ctx->sp_limit +
332 sizeof(struct tfm_additional_context_t)) > __get_PSP()) {
333 tfm_core_panic();
334 }
335
336 CRITICAL_SECTION_ENTER(cs);
337 /*
338 * If required, let the platform update boundary based on its
339 * implementation. Change privilege, MPU or other configurations.
340 */
Chendi Sun0f7d2822022-10-28 12:24:12 +0800341 if (tfm_hal_boundary_need_switch(p_part_curr->boundary,
342 p_part_next->boundary)) {
Xinyu Zhang6ad07032022-08-10 14:45:56 +0800343 FIH_CALL(tfm_hal_activate_boundary, fih_rc,
344 p_part_next->p_ldinf, p_part_next->boundary);
345 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
Sherry Zhang049733e2022-04-20 21:37:51 +0800346 tfm_core_panic();
347 }
348 }
349 ARCH_FLUSH_FP_CONTEXT();
350
351 AAPCS_DUAL_U32_SET_A1(ctx_ctrls, (uint32_t)pth_next->p_context_ctrl);
352
353 CURRENT_THREAD = pth_next;
354 CRITICAL_SECTION_LEAVE(cs);
355 }
356
357 /* Update meta indicator */
358 if (partition_meta_indicator_pos && (p_part_next->p_metadata)) {
359 *partition_meta_indicator_pos = (uintptr_t)(p_part_next->p_metadata);
360 }
361 return AAPCS_DUAL_U32_AS_U64(ctx_ctrls);
362}