blob: 31524c71f485681ca8c14a9acd9af010e398dff9 [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.
BohdanHunko749c9a52023-02-16 16:14:47 +02003 * Copyright (c) 2021-2023 Cypress Semiconductor Corporation (an Infineon
Chris Brandb4c2b002022-07-21 12:54:00 -07004 * 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"
Ken Liu62bae592021-10-19 22:15:43 +080019#include "tfm_hal_isolation.h"
Kevin Pengb288c522021-09-26 16:18:23 +080020#include "tfm_hal_platform.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080021#include "tfm_rpc.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080022#include "ffm/backend.h"
Ken Liu62bae592021-10-19 22:15:43 +080023#include "utilities.h"
Ken Liu71a79fe2023-03-01 16:27:23 +080024#include "cmsis_psa/memory_symbols.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"
29
30/* Declare the global component list */
31struct partition_head_t partition_listhead;
32
Kevin Peng9f1a7542022-02-07 16:32:27 +080033#if CONFIG_TFM_PSA_API_CROSS_CALL == 1
Ken Liu63a176b2022-06-09 22:36:56 +080034/* Instance for SPM_THREAD_CONTEXT */
Ken Liue07c3b72021-10-14 16:19:13 +080035
Chris Brandfe5adca2022-11-08 17:44:07 -080036#ifdef CONFIG_TFM_USE_TRUSTZONE
37struct context_ctrl_t *p_spm_thread_context;
38#else
39/* If ns_agent_tz isn't used, we need to provide a stack for SPM to use */
Ken Liu63a176b2022-06-09 22:36:56 +080040static uint8_t spm_thread_stack[CONFIG_TFM_SPM_THREAD_STACK_SIZE] __aligned(8);
41ARCH_CLAIM_CTXCTRL_INSTANCE(spm_thread_context,
42 spm_thread_stack,
43 sizeof(spm_thread_stack));
44
Ken Liue07c3b72021-10-14 16:19:13 +080045struct context_ctrl_t *p_spm_thread_context = &spm_thread_context;
Ken Liue07c3b72021-10-14 16:19:13 +080046#endif
47
48#endif
49
Sherry Zhang049733e2022-04-20 21:37:51 +080050/* Indicator point to the partition meta */
51uintptr_t *partition_meta_indicator_pos;
52
53extern uint32_t scheduler_lock;
54
shejia0195a88bc2023-01-16 15:44:46 +080055/*
56 * Query the state of current thread.
57 */
58static uint32_t query_state(struct thread_t *p_thrd, uint32_t *p_retval)
59{
60 struct critical_section_t cs_signal = CRITICAL_SECTION_STATIC_INIT;
61 struct partition_t *p_pt = NULL;
62 uint32_t state = p_thrd->state;
63 psa_signal_t signal_ret = 0;
64
65 /* Get current partition of thread. */
66 p_pt = TO_CONTAINER(p_thrd->p_context_ctrl,
67 struct partition_t, ctx_ctrl);
68
69 CRITICAL_SECTION_ENTER(cs_signal);
70
71 signal_ret = p_pt->signals_waiting & p_pt->signals_asserted;
72
73 if (signal_ret) {
74 /*
75 * If the partition is waiting some signals and any of them is asserted,
76 * change thread to be THRD_STATE_RET_VAL_AVAIL and fill the retval. If
77 * the waiting signal is TFM_IPC_REPLY_SIGNAL, it means the Secure
78 * Partition is waiting for the services to be fulfilled, then the
79 * return value comes from the backend_replying() by the server
80 * Partition. For other waiting signals by psa_wait(), the return value
81 * is just the signal.
82 */
83 if (signal_ret == TFM_IPC_REPLY_SIGNAL) {
84 p_pt->signals_asserted &= ~TFM_IPC_REPLY_SIGNAL;
85 *p_retval = (uint32_t)p_pt->reply_value;
86 } else {
87 *p_retval = signal_ret;
88 }
89
90 p_pt->signals_waiting = 0;
91 state = THRD_STATE_RET_VAL_AVAIL;
92 } else if (p_pt->signals_waiting != 0) {
93 /*
94 * If the thread is waiting some signals but none of them is asserted,
95 * block the thread.
96 */
97 state = THRD_STATE_BLOCK;
98 }
99
100 CRITICAL_SECTION_LEAVE(cs_signal);
101 return state;
102}
103
Sherry Zhangef49b1d2023-02-07 14:08:40 +0800104extern struct psa_api_tbl_t psa_api_cross;
105extern struct psa_api_tbl_t psa_api_svc;
106
Summer Qin596f5552022-01-27 18:04:06 +0800107static void prv_process_metadata(struct partition_t *p_pt)
108{
Kevin Peng43160d52022-02-11 13:35:56 +0800109 const struct partition_load_info_t *p_pt_ldi;
110 const struct service_load_info_t *p_srv_ldi;
111 struct context_ctrl_t *ctx_ctrl;
112 struct runtime_metadata_t *p_rt_meta;
113 service_fn_t *p_sfn_table;
114 uint32_t allocate_size;
Summer Qin596f5552022-01-27 18:04:06 +0800115
Kevin Peng43160d52022-02-11 13:35:56 +0800116 p_pt_ldi = p_pt->p_ldinf;
Chris Brand1fb796d2022-10-18 16:54:25 -0700117 p_srv_ldi = LOAD_INFO_SERVICE(p_pt_ldi);
Kevin Peng43160d52022-02-11 13:35:56 +0800118 ctx_ctrl = &p_pt->ctx_ctrl;
119
120 /* common runtime metadata */
121 allocate_size = sizeof(*p_rt_meta);
122
Summer Qin2ead4fc2023-02-24 14:09:34 +0800123 if (!IS_IPC_MODEL(p_pt_ldi)) {
Kevin Peng43160d52022-02-11 13:35:56 +0800124 /* SFN specific metadata - SFN function table */
125 allocate_size += sizeof(service_fn_t) * p_pt_ldi->nservices;
Summer Qin596f5552022-01-27 18:04:06 +0800126 }
127
Kevin Peng43160d52022-02-11 13:35:56 +0800128 ARCH_CTXCTRL_ALLOCATE_STACK(ctx_ctrl, allocate_size);
129 p_rt_meta = (struct runtime_metadata_t *)
130 ARCH_CTXCTRL_ALLOCATED_PTR(ctx_ctrl);
131
132 p_rt_meta->entry = p_pt_ldi->entry;
Sherry Zhangef49b1d2023-02-07 14:08:40 +0800133#if TFM_LVL == 1
134 p_rt_meta->psa_fns = &psa_api_cross;
135#else
136 /* TODO: ABI for PRoT partitions needs to be updated based on implementations. */
137 p_rt_meta->psa_fns = &psa_api_svc;
138#endif
Kevin Peng43160d52022-02-11 13:35:56 +0800139 p_rt_meta->n_sfn = 0;
140 p_sfn_table = p_rt_meta->sfn_table;
141
Summer Qin2ead4fc2023-02-24 14:09:34 +0800142 if (!IS_IPC_MODEL(p_pt_ldi)) {
Kevin Peng43160d52022-02-11 13:35:56 +0800143 /* SFN table. The signal bit of the service is the same index of SFN. */
144 for (int i = 0; i < p_pt_ldi->nservices; i++) {
145 p_sfn_table[i] = (service_fn_t)p_srv_ldi[i].sfn;
146 }
147
148 p_rt_meta->n_sfn = p_pt_ldi->nservices;
149 }
150
151 p_pt->p_metadata = (void *)p_rt_meta;
Summer Qin596f5552022-01-27 18:04:06 +0800152}
153
Mingyang Sundeae45d2021-09-06 15:31:07 +0800154/*
155 * Send message and wake up the SP who is waiting on message queue, block the
Ken Liuf39d8eb2021-10-07 12:55:33 +0800156 * current thread and trigger scheduler.
Mingyang Sundeae45d2021-09-06 15:31:07 +0800157 */
Ken Liu995a9742022-05-18 19:28:30 +0800158psa_status_t backend_messaging(struct service_t *service,
Ken Liuc9313eb2023-02-22 15:45:54 +0800159 struct connection_t *handle)
Mingyang Sundeae45d2021-09-06 15:31:07 +0800160{
161 struct partition_t *p_owner = NULL;
162 psa_signal_t signal = 0;
163
Mingyang Suna09adda2022-02-16 18:11:33 +0800164 if (!handle || !service || !service->p_ldinf || !service->partition) {
Mingyang Sunbb4a42a2021-12-14 15:18:52 +0800165 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800166 }
167
168 p_owner = service->partition;
169 signal = service->p_ldinf->signal;
170
Mingyang Suna09adda2022-02-16 18:11:33 +0800171 UNI_LIST_INSERT_AFTER(p_owner, handle, p_handles);
Mingyang Sundeae45d2021-09-06 15:31:07 +0800172
173 /* Messages put. Update signals */
Jianliang Shenbd8c7c92023-03-03 16:07:42 +0100174 backend_assert_signal(p_owner, signal);
Mingyang Sundeae45d2021-09-06 15:31:07 +0800175
176 /*
177 * If it is a NS request via RPC, it is unnecessary to block current
178 * thread.
179 */
180
Mingyang Suna09adda2022-02-16 18:11:33 +0800181 if (!is_tfm_rpc_msg(handle)) {
Jianliang Shenbd8c7c92023-03-03 16:07:42 +0100182 backend_wait_signals(handle->p_client, TFM_IPC_REPLY_SIGNAL);
Mingyang Sundeae45d2021-09-06 15:31:07 +0800183 }
184
Mingyang Sunaeca8e02022-02-24 14:47:56 +0800185 handle->status = TFM_HANDLE_STATUS_ACTIVE;
186
Jianliang Shenbd8c7c92023-03-03 16:07:42 +0100187 return PSA_SUCCESS;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800188}
189
Ken Liuc9313eb2023-02-22 15:45:54 +0800190psa_status_t backend_replying(struct connection_t *handle, int32_t status)
Ken Liu802a3702021-10-15 12:09:56 +0800191{
Mingyang Suna09adda2022-02-16 18:11:33 +0800192 if (is_tfm_rpc_msg(handle)) {
193 tfm_rpc_client_call_reply(handle, status);
Ken Liu802a3702021-10-15 12:09:56 +0800194 } else {
shejia0195a88bc2023-01-16 15:44:46 +0800195 handle->p_client->reply_value = (uintptr_t)status;
Jianliang Shenbd8c7c92023-03-03 16:07:42 +0100196 backend_assert_signal(handle->p_client, TFM_IPC_REPLY_SIGNAL);
Ken Liu802a3702021-10-15 12:09:56 +0800197 }
Ken Liuf39d8eb2021-10-07 12:55:33 +0800198
199 /*
200 * 'psa_reply' exists in IPC model only and returns 'void'. Return
201 * 'PSA_SUCCESS' here always since SPM does not forward the status
202 * to the caller.
203 */
204 return PSA_SUCCESS;
Ken Liu802a3702021-10-15 12:09:56 +0800205}
206
BohdanHunkofdd3f1d2023-02-17 13:10:32 +0200207extern void common_sfn_thread(void *param);
Summer Qin596f5552022-01-27 18:04:06 +0800208
Mingyang Sundeae45d2021-09-06 15:31:07 +0800209/* Parameters are treated as assuredly */
Ken Liu995a9742022-05-18 19:28:30 +0800210void backend_init_comp_assuredly(struct partition_t *p_pt,
211 uint32_t service_setting)
Mingyang Sundeae45d2021-09-06 15:31:07 +0800212{
213 const struct partition_load_info_t *p_pldi = p_pt->p_ldinf;
BohdanHunko749c9a52023-02-16 16:14:47 +0200214 thrd_fn_t thrd_entry;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800215
Kevin Peng613b4172022-02-15 14:41:44 +0800216#if CONFIG_TFM_DOORBELL_API == 1
217 p_pt->signals_allowed |= PSA_DOORBELL;
218#endif /* CONFIG_TFM_DOORBELL_API == 1 */
219
220 p_pt->signals_allowed |= service_setting;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800221
Ken Liu0bed7e02022-02-10 12:38:07 +0800222 UNI_LISI_INIT_NODE(p_pt, p_handles);
Mingyang Sundeae45d2021-09-06 15:31:07 +0800223
Ken Liubf4681f2022-02-11 11:15:03 +0800224 ARCH_CTXCTRL_INIT(&p_pt->ctx_ctrl,
225 LOAD_ALLOCED_STACK_ADDR(p_pldi),
226 p_pldi->stack_size);
227
Chris Brand30106ba2022-01-13 13:48:50 -0800228 watermark_stack(p_pt);
229
Summer Qin596f5552022-01-27 18:04:06 +0800230 prv_process_metadata(p_pt);
231
Mingyang Sundeae45d2021-09-06 15:31:07 +0800232 THRD_INIT(&p_pt->thrd, &p_pt->ctx_ctrl,
233 TO_THREAD_PRIORITY(PARTITION_PRIORITY(p_pldi->flags)));
234
Chris Brandfe5adca2022-11-08 17:44:07 -0800235#if (CONFIG_TFM_PSA_API_CROSS_CALL == 1) && defined(CONFIG_TFM_USE_TRUSTZONE)
Summer Qin2ead4fc2023-02-24 14:09:34 +0800236 if (IS_NS_AGENT(p_pldi)) {
Chris Brandfe5adca2022-11-08 17:44:07 -0800237 /* Get the context from ns_agent_tz */
238 if (p_pldi->pid == 0) {
239 SPM_THREAD_CONTEXT = &p_pt->ctx_ctrl;
240 }
Mingyang Sundeae45d2021-09-06 15:31:07 +0800241 }
Summer Qin95444822022-01-27 11:22:00 +0800242#endif
Mingyang Sundeae45d2021-09-06 15:31:07 +0800243
Summer Qin2ead4fc2023-02-24 14:09:34 +0800244 if (IS_IPC_MODEL(p_pldi)) {
BohdanHunko749c9a52023-02-16 16:14:47 +0200245 /* IPC Partition */
246 thrd_entry = POSITION_TO_ENTRY(p_pldi->entry, thrd_fn_t);
247 } else {
248 /* SFN Partition */
249 thrd_entry = POSITION_TO_ENTRY(common_sfn_thread, thrd_fn_t);
250 }
251
Mingyang Sundeae45d2021-09-06 15:31:07 +0800252 thrd_start(&p_pt->thrd,
BohdanHunko749c9a52023-02-16 16:14:47 +0200253 thrd_entry,
BohdanHunkofdd3f1d2023-02-17 13:10:32 +0200254 THRD_GENERAL_EXIT,
255 NULL);
Mingyang Sundeae45d2021-09-06 15:31:07 +0800256}
257
Ken Liu995a9742022-05-18 19:28:30 +0800258uint32_t backend_system_run(void)
Mingyang Sundeae45d2021-09-06 15:31:07 +0800259{
Ken Liu62bae592021-10-19 22:15:43 +0800260 uint32_t control;
261 struct partition_t *p_cur_pt;
Xinyu Zhang6ad07032022-08-10 14:45:56 +0800262 fih_int fih_rc = FIH_FAILURE;
Ken Liu62bae592021-10-19 22:15:43 +0800263
Sherry Zhang68681642022-06-24 13:36:33 +0800264#if CONFIG_TFM_PSA_API_CROSS_CALL == 1
Summer Qin1056d1c2022-10-19 16:07:15 +0800265 SPM_ASSERT(SPM_THREAD_CONTEXT);
Chris Brand3778bc12021-12-15 17:01:05 -0800266#endif
267
shejia0195a88bc2023-01-16 15:44:46 +0800268 /* Init thread callback function. */
269 thrd_set_query_callback(query_state);
270
Ken Liu71a79fe2023-03-01 16:27:23 +0800271 partition_meta_indicator_pos = (uintptr_t *)PART_LOCAL_STORAGE_PTR_POS;
Ken Liu62bae592021-10-19 22:15:43 +0800272 control = thrd_start_scheduler(&CURRENT_THREAD);
273
274 p_cur_pt = TO_CONTAINER(CURRENT_THREAD->p_context_ctrl,
275 struct partition_t, ctx_ctrl);
276
Xinyu Zhang6ad07032022-08-10 14:45:56 +0800277 FIH_CALL(tfm_hal_activate_boundary, fih_rc, p_cur_pt->p_ldinf, p_cur_pt->boundary);
278 if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) {
Ken Liu62bae592021-10-19 22:15:43 +0800279 tfm_core_panic();
280 }
281
282 return control;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800283}
284
Jianliang Shenbd8c7c92023-03-03 16:07:42 +0100285psa_signal_t backend_wait_signals(struct partition_t *p_pt, psa_signal_t signals)
Kevin Pengdef92de2021-11-10 16:14:48 +0800286{
shejia0195a88bc2023-01-16 15:44:46 +0800287 struct critical_section_t cs_signal = CRITICAL_SECTION_STATIC_INIT;
Jianliang Shenbd8c7c92023-03-03 16:07:42 +0100288 psa_signal_t ret_signal;
Kevin Pengdef92de2021-11-10 16:14:48 +0800289
shejia0195a88bc2023-01-16 15:44:46 +0800290 if (!p_pt) {
291 tfm_core_panic();
Mingyang Sun5c9529f2022-03-15 17:51:56 +0800292 }
shejia0195a88bc2023-01-16 15:44:46 +0800293
294 CRITICAL_SECTION_ENTER(cs_signal);
295
Jianliang Shenbd8c7c92023-03-03 16:07:42 +0100296 ret_signal = p_pt->signals_asserted & signals;
297 if (ret_signal == 0) {
shejia0195a88bc2023-01-16 15:44:46 +0800298 p_pt->signals_waiting = signals;
299 }
300
301 CRITICAL_SECTION_LEAVE(cs_signal);
Mingyang Sun5c9529f2022-03-15 17:51:56 +0800302
Jianliang Shenbd8c7c92023-03-03 16:07:42 +0100303 return ret_signal;
Kevin Pengdef92de2021-11-10 16:14:48 +0800304}
305
Jianliang Shenbd8c7c92023-03-03 16:07:42 +0100306uint32_t backend_assert_signal(struct partition_t *p_pt, psa_signal_t signal)
Kevin Pengdef92de2021-11-10 16:14:48 +0800307{
shejia0195a88bc2023-01-16 15:44:46 +0800308 struct critical_section_t cs_signal = CRITICAL_SECTION_STATIC_INIT;
309
310 if (!p_pt) {
311 tfm_core_panic();
shejia01a0ea10c2022-06-27 13:56:00 +0800312 }
shejia0195a88bc2023-01-16 15:44:46 +0800313
314 CRITICAL_SECTION_ENTER(cs_signal);
315 p_pt->signals_asserted |= signal;
316 CRITICAL_SECTION_LEAVE(cs_signal);
317
Jianliang Shenbd8c7c92023-03-03 16:07:42 +0100318 return PSA_SUCCESS;
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}