Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 1 | /* |
shejia01 | a0ea10c | 2022-06-27 13:56:00 +0800 | [diff] [blame] | 2 | * Copyright (c) 2021-2023, Arm Limited. All rights reserved. |
BohdanHunko | 749c9a5 | 2023-02-16 16:14:47 +0200 | [diff] [blame] | 3 | * Copyright (c) 2021-2023 Cypress Semiconductor Corporation (an Infineon |
Chris Brand | b4c2b00 | 2022-07-21 12:54:00 -0700 | [diff] [blame] | 4 | * company) or an affiliate of Cypress Semiconductor Corporation. All rights |
| 5 | * reserved. |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 6 | * |
| 7 | * SPDX-License-Identifier: BSD-3-Clause |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | #include <stdint.h> |
Sherry Zhang | 049733e | 2022-04-20 21:37:51 +0800 | [diff] [blame] | 12 | #include "aapcs_local.h" |
Mingyang Sun | 620c856 | 2021-11-10 11:44:58 +0800 | [diff] [blame] | 13 | #include "critical_section.h" |
Ken Liu | e07c3b7 | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 14 | #include "compiler_ext_defs.h" |
Xinyu Zhang | cdbe362 | 2022-10-31 14:34:25 +0800 | [diff] [blame] | 15 | #include "config_spm.h" |
Summer Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 16 | #include "runtime_defs.h" |
Chris Brand | 30106ba | 2022-01-13 13:48:50 -0800 | [diff] [blame] | 17 | #include "ffm/stack_watermark.h" |
Sherry Zhang | c714702 | 2023-02-03 11:21:10 +0800 | [diff] [blame] | 18 | #include "spm.h" |
Ken Liu | 62bae59 | 2021-10-19 22:15:43 +0800 | [diff] [blame] | 19 | #include "tfm_hal_isolation.h" |
Kevin Peng | b288c52 | 2021-09-26 16:18:23 +0800 | [diff] [blame] | 20 | #include "tfm_hal_platform.h" |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 21 | #include "tfm_rpc.h" |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 22 | #include "ffm/backend.h" |
Ken Liu | 62bae59 | 2021-10-19 22:15:43 +0800 | [diff] [blame] | 23 | #include "utilities.h" |
Ken Liu | 71a79fe | 2023-03-01 16:27:23 +0800 | [diff] [blame^] | 24 | #include "cmsis_psa/memory_symbols.h" |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 25 | #include "load/partition_defs.h" |
| 26 | #include "load/service_defs.h" |
| 27 | #include "load/spm_load_api.h" |
| 28 | #include "psa/error.h" |
Jianliang Shen | 9e38935 | 2023-02-09 16:58:08 +0800 | [diff] [blame] | 29 | #include "internal_status_code.h" |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 30 | |
| 31 | /* Declare the global component list */ |
| 32 | struct partition_head_t partition_listhead; |
| 33 | |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame] | 34 | #if CONFIG_TFM_PSA_API_CROSS_CALL == 1 |
Ken Liu | 63a176b | 2022-06-09 22:36:56 +0800 | [diff] [blame] | 35 | /* Instance for SPM_THREAD_CONTEXT */ |
Ken Liu | e07c3b7 | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 36 | |
Chris Brand | fe5adca | 2022-11-08 17:44:07 -0800 | [diff] [blame] | 37 | #ifdef CONFIG_TFM_USE_TRUSTZONE |
| 38 | struct 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 Liu | 63a176b | 2022-06-09 22:36:56 +0800 | [diff] [blame] | 41 | static uint8_t spm_thread_stack[CONFIG_TFM_SPM_THREAD_STACK_SIZE] __aligned(8); |
| 42 | ARCH_CLAIM_CTXCTRL_INSTANCE(spm_thread_context, |
| 43 | spm_thread_stack, |
| 44 | sizeof(spm_thread_stack)); |
| 45 | |
Ken Liu | e07c3b7 | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 46 | struct context_ctrl_t *p_spm_thread_context = &spm_thread_context; |
Ken Liu | e07c3b7 | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 47 | #endif |
| 48 | |
| 49 | #endif |
| 50 | |
Sherry Zhang | 049733e | 2022-04-20 21:37:51 +0800 | [diff] [blame] | 51 | /* Indicator point to the partition meta */ |
| 52 | uintptr_t *partition_meta_indicator_pos; |
| 53 | |
| 54 | extern uint32_t scheduler_lock; |
| 55 | |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 56 | /* |
| 57 | * Query the state of current thread. |
| 58 | */ |
| 59 | static 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 Zhang | ef49b1d | 2023-02-07 14:08:40 +0800 | [diff] [blame] | 105 | extern struct psa_api_tbl_t psa_api_cross; |
| 106 | extern struct psa_api_tbl_t psa_api_svc; |
| 107 | |
Summer Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 108 | static void prv_process_metadata(struct partition_t *p_pt) |
| 109 | { |
Kevin Peng | 43160d5 | 2022-02-11 13:35:56 +0800 | [diff] [blame] | 110 | 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 Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 116 | |
Kevin Peng | 43160d5 | 2022-02-11 13:35:56 +0800 | [diff] [blame] | 117 | p_pt_ldi = p_pt->p_ldinf; |
Chris Brand | 1fb796d | 2022-10-18 16:54:25 -0700 | [diff] [blame] | 118 | p_srv_ldi = LOAD_INFO_SERVICE(p_pt_ldi); |
Kevin Peng | 43160d5 | 2022-02-11 13:35:56 +0800 | [diff] [blame] | 119 | 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 Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 127 | } |
| 128 | |
Kevin Peng | 43160d5 | 2022-02-11 13:35:56 +0800 | [diff] [blame] | 129 | 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 Zhang | ef49b1d | 2023-02-07 14:08:40 +0800 | [diff] [blame] | 134 | #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 Peng | 43160d5 | 2022-02-11 13:35:56 +0800 | [diff] [blame] | 140 | 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 Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 153 | } |
| 154 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 155 | /* |
| 156 | * Send message and wake up the SP who is waiting on message queue, block the |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 157 | * current thread and trigger scheduler. |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 158 | */ |
Ken Liu | 995a974 | 2022-05-18 19:28:30 +0800 | [diff] [blame] | 159 | psa_status_t backend_messaging(struct service_t *service, |
Ken Liu | c9313eb | 2023-02-22 15:45:54 +0800 | [diff] [blame] | 160 | struct connection_t *handle) |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 161 | { |
| 162 | struct partition_t *p_owner = NULL; |
| 163 | psa_signal_t signal = 0; |
Jianliang Shen | 9e38935 | 2023-02-09 16:58:08 +0800 | [diff] [blame] | 164 | psa_status_t ret = PSA_SUCCESS; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 165 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 166 | if (!handle || !service || !service->p_ldinf || !service->partition) { |
Mingyang Sun | bb4a42a | 2021-12-14 15:18:52 +0800 | [diff] [blame] | 167 | return PSA_ERROR_PROGRAMMER_ERROR; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | p_owner = service->partition; |
| 171 | signal = service->p_ldinf->signal; |
| 172 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 173 | UNI_LIST_INSERT_AFTER(p_owner, handle, p_handles); |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 174 | |
| 175 | /* Messages put. Update signals */ |
Jianliang Shen | 9e38935 | 2023-02-09 16:58:08 +0800 | [diff] [blame] | 176 | ret = backend_assert_signal(p_owner, signal); |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 177 | |
| 178 | /* |
| 179 | * If it is a NS request via RPC, it is unnecessary to block current |
| 180 | * thread. |
| 181 | */ |
| 182 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 183 | if (!is_tfm_rpc_msg(handle)) { |
Jianliang Shen | 9e38935 | 2023-02-09 16:58:08 +0800 | [diff] [blame] | 184 | ret = backend_wait_signals(handle->p_client, TFM_IPC_REPLY_SIGNAL); |
| 185 | } else { |
| 186 | ret = PSA_SUCCESS; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 187 | } |
| 188 | |
Mingyang Sun | aeca8e0 | 2022-02-24 14:47:56 +0800 | [diff] [blame] | 189 | handle->status = TFM_HANDLE_STATUS_ACTIVE; |
| 190 | |
Jianliang Shen | 9e38935 | 2023-02-09 16:58:08 +0800 | [diff] [blame] | 191 | return ret; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 192 | } |
| 193 | |
Ken Liu | c9313eb | 2023-02-22 15:45:54 +0800 | [diff] [blame] | 194 | psa_status_t backend_replying(struct connection_t *handle, int32_t status) |
Ken Liu | 802a370 | 2021-10-15 12:09:56 +0800 | [diff] [blame] | 195 | { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 196 | if (is_tfm_rpc_msg(handle)) { |
| 197 | tfm_rpc_client_call_reply(handle, status); |
Ken Liu | 802a370 | 2021-10-15 12:09:56 +0800 | [diff] [blame] | 198 | } else { |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 199 | handle->p_client->reply_value = (uintptr_t)status; |
Jianliang Shen | 9e38935 | 2023-02-09 16:58:08 +0800 | [diff] [blame] | 200 | return backend_assert_signal(handle->p_client, TFM_IPC_REPLY_SIGNAL); |
Ken Liu | 802a370 | 2021-10-15 12:09:56 +0800 | [diff] [blame] | 201 | } |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 202 | |
| 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 Liu | 802a370 | 2021-10-15 12:09:56 +0800 | [diff] [blame] | 209 | } |
| 210 | |
BohdanHunko | 749c9a5 | 2023-02-16 16:14:47 +0200 | [diff] [blame] | 211 | extern void common_sfn_thread(void); |
Summer Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 212 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 213 | /* Parameters are treated as assuredly */ |
Ken Liu | 995a974 | 2022-05-18 19:28:30 +0800 | [diff] [blame] | 214 | void backend_init_comp_assuredly(struct partition_t *p_pt, |
| 215 | uint32_t service_setting) |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 216 | { |
| 217 | const struct partition_load_info_t *p_pldi = p_pt->p_ldinf; |
BohdanHunko | 749c9a5 | 2023-02-16 16:14:47 +0200 | [diff] [blame] | 218 | thrd_fn_t thrd_entry; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 219 | |
Kevin Peng | 613b417 | 2022-02-15 14:41:44 +0800 | [diff] [blame] | 220 | #if CONFIG_TFM_DOORBELL_API == 1 |
| 221 | p_pt->signals_allowed |= PSA_DOORBELL; |
| 222 | #endif /* CONFIG_TFM_DOORBELL_API == 1 */ |
| 223 | |
| 224 | p_pt->signals_allowed |= service_setting; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 225 | |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 226 | UNI_LISI_INIT_NODE(p_pt, p_handles); |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 227 | |
Ken Liu | bf4681f | 2022-02-11 11:15:03 +0800 | [diff] [blame] | 228 | ARCH_CTXCTRL_INIT(&p_pt->ctx_ctrl, |
| 229 | LOAD_ALLOCED_STACK_ADDR(p_pldi), |
| 230 | p_pldi->stack_size); |
| 231 | |
Chris Brand | 30106ba | 2022-01-13 13:48:50 -0800 | [diff] [blame] | 232 | watermark_stack(p_pt); |
| 233 | |
Summer Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 234 | prv_process_metadata(p_pt); |
| 235 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 236 | THRD_INIT(&p_pt->thrd, &p_pt->ctx_ctrl, |
| 237 | TO_THREAD_PRIORITY(PARTITION_PRIORITY(p_pldi->flags))); |
| 238 | |
Chris Brand | fe5adca | 2022-11-08 17:44:07 -0800 | [diff] [blame] | 239 | #if (CONFIG_TFM_PSA_API_CROSS_CALL == 1) && defined(CONFIG_TFM_USE_TRUSTZONE) |
Chris Brand | b4c2b00 | 2022-07-21 12:54:00 -0700 | [diff] [blame] | 240 | if (IS_PARTITION_NS_AGENT(p_pldi)) { |
Chris Brand | fe5adca | 2022-11-08 17:44:07 -0800 | [diff] [blame] | 241 | /* Get the context from ns_agent_tz */ |
| 242 | if (p_pldi->pid == 0) { |
| 243 | SPM_THREAD_CONTEXT = &p_pt->ctx_ctrl; |
| 244 | } |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 245 | } |
Summer Qin | 9544482 | 2022-01-27 11:22:00 +0800 | [diff] [blame] | 246 | #endif |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 247 | |
BohdanHunko | 749c9a5 | 2023-02-16 16:14:47 +0200 | [diff] [blame] | 248 | if (IS_PARTITION_IPC_MODEL(p_pldi)) { |
| 249 | /* IPC Partition */ |
| 250 | thrd_entry = POSITION_TO_ENTRY(p_pldi->entry, thrd_fn_t); |
| 251 | } else { |
| 252 | /* SFN Partition */ |
| 253 | thrd_entry = POSITION_TO_ENTRY(common_sfn_thread, thrd_fn_t); |
| 254 | } |
| 255 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 256 | thrd_start(&p_pt->thrd, |
BohdanHunko | 749c9a5 | 2023-02-16 16:14:47 +0200 | [diff] [blame] | 257 | thrd_entry, |
Ken Liu | bf4681f | 2022-02-11 11:15:03 +0800 | [diff] [blame] | 258 | THRD_GENERAL_EXIT); |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 259 | } |
| 260 | |
Ken Liu | 995a974 | 2022-05-18 19:28:30 +0800 | [diff] [blame] | 261 | uint32_t backend_system_run(void) |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 262 | { |
Ken Liu | 62bae59 | 2021-10-19 22:15:43 +0800 | [diff] [blame] | 263 | uint32_t control; |
| 264 | struct partition_t *p_cur_pt; |
Xinyu Zhang | 6ad0703 | 2022-08-10 14:45:56 +0800 | [diff] [blame] | 265 | fih_int fih_rc = FIH_FAILURE; |
Ken Liu | 62bae59 | 2021-10-19 22:15:43 +0800 | [diff] [blame] | 266 | |
Sherry Zhang | 6868164 | 2022-06-24 13:36:33 +0800 | [diff] [blame] | 267 | #if CONFIG_TFM_PSA_API_CROSS_CALL == 1 |
Summer Qin | 1056d1c | 2022-10-19 16:07:15 +0800 | [diff] [blame] | 268 | SPM_ASSERT(SPM_THREAD_CONTEXT); |
Chris Brand | 3778bc1 | 2021-12-15 17:01:05 -0800 | [diff] [blame] | 269 | #endif |
| 270 | |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 271 | /* Init thread callback function. */ |
| 272 | thrd_set_query_callback(query_state); |
| 273 | |
Ken Liu | 71a79fe | 2023-03-01 16:27:23 +0800 | [diff] [blame^] | 274 | partition_meta_indicator_pos = (uintptr_t *)PART_LOCAL_STORAGE_PTR_POS; |
Ken Liu | 62bae59 | 2021-10-19 22:15:43 +0800 | [diff] [blame] | 275 | control = thrd_start_scheduler(&CURRENT_THREAD); |
| 276 | |
| 277 | p_cur_pt = TO_CONTAINER(CURRENT_THREAD->p_context_ctrl, |
| 278 | struct partition_t, ctx_ctrl); |
| 279 | |
Xinyu Zhang | 6ad0703 | 2022-08-10 14:45:56 +0800 | [diff] [blame] | 280 | FIH_CALL(tfm_hal_activate_boundary, fih_rc, p_cur_pt->p_ldinf, p_cur_pt->boundary); |
| 281 | if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) { |
Ken Liu | 62bae59 | 2021-10-19 22:15:43 +0800 | [diff] [blame] | 282 | tfm_core_panic(); |
| 283 | } |
| 284 | |
| 285 | return control; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 286 | } |
| 287 | |
Jianliang Shen | 9e38935 | 2023-02-09 16:58:08 +0800 | [diff] [blame] | 288 | psa_status_t backend_wait_signals(struct partition_t *p_pt, psa_signal_t signals) |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 289 | { |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 290 | struct critical_section_t cs_signal = CRITICAL_SECTION_STATIC_INIT; |
Jianliang Shen | 9e38935 | 2023-02-09 16:58:08 +0800 | [diff] [blame] | 291 | psa_status_t ret = PSA_SUCCESS; |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 292 | |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 293 | if (!p_pt) { |
| 294 | tfm_core_panic(); |
Mingyang Sun | 5c9529f | 2022-03-15 17:51:56 +0800 | [diff] [blame] | 295 | } |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 296 | |
| 297 | CRITICAL_SECTION_ENTER(cs_signal); |
| 298 | |
Jianliang Shen | 9e38935 | 2023-02-09 16:58:08 +0800 | [diff] [blame] | 299 | ret = p_pt->signals_asserted & signals; |
| 300 | if (ret == 0) { |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 301 | p_pt->signals_waiting = signals; |
Jianliang Shen | 9e38935 | 2023-02-09 16:58:08 +0800 | [diff] [blame] | 302 | ret = STATUS_NEED_SCHEDULE; |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | CRITICAL_SECTION_LEAVE(cs_signal); |
Mingyang Sun | 5c9529f | 2022-03-15 17:51:56 +0800 | [diff] [blame] | 306 | |
Jianliang Shen | 9e38935 | 2023-02-09 16:58:08 +0800 | [diff] [blame] | 307 | return ret; |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 308 | } |
| 309 | |
Jianliang Shen | 9e38935 | 2023-02-09 16:58:08 +0800 | [diff] [blame] | 310 | psa_status_t backend_assert_signal(struct partition_t *p_pt, psa_signal_t signal) |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 311 | { |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 312 | struct critical_section_t cs_signal = CRITICAL_SECTION_STATIC_INIT; |
Jianliang Shen | 9e38935 | 2023-02-09 16:58:08 +0800 | [diff] [blame] | 313 | psa_status_t ret = PSA_SUCCESS; |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 314 | |
| 315 | if (!p_pt) { |
| 316 | tfm_core_panic(); |
shejia01 | a0ea10c | 2022-06-27 13:56:00 +0800 | [diff] [blame] | 317 | } |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 318 | |
| 319 | CRITICAL_SECTION_ENTER(cs_signal); |
| 320 | p_pt->signals_asserted |= signal; |
Jianliang Shen | 9e38935 | 2023-02-09 16:58:08 +0800 | [diff] [blame] | 321 | |
| 322 | if (p_pt->signals_asserted & p_pt->signals_waiting) { |
| 323 | ret = STATUS_NEED_SCHEDULE; |
| 324 | } |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 325 | CRITICAL_SECTION_LEAVE(cs_signal); |
| 326 | |
Jianliang Shen | 9e38935 | 2023-02-09 16:58:08 +0800 | [diff] [blame] | 327 | return ret; |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 328 | } |
| 329 | |
Sherry Zhang | 049733e | 2022-04-20 21:37:51 +0800 | [diff] [blame] | 330 | uint64_t ipc_schedule(void) |
| 331 | { |
Xinyu Zhang | 6ad0703 | 2022-08-10 14:45:56 +0800 | [diff] [blame] | 332 | fih_int fih_rc = FIH_FAILURE; |
Sherry Zhang | 049733e | 2022-04-20 21:37:51 +0800 | [diff] [blame] | 333 | AAPCS_DUAL_U32_T ctx_ctrls; |
| 334 | struct partition_t *p_part_curr, *p_part_next; |
| 335 | struct context_ctrl_t *p_curr_ctx; |
| 336 | struct thread_t *pth_next = thrd_next(); |
| 337 | struct critical_section_t cs = CRITICAL_SECTION_STATIC_INIT; |
| 338 | |
| 339 | p_curr_ctx = (struct context_ctrl_t *)(CURRENT_THREAD->p_context_ctrl); |
| 340 | |
| 341 | AAPCS_DUAL_U32_SET(ctx_ctrls, (uint32_t)p_curr_ctx, (uint32_t)p_curr_ctx); |
| 342 | |
| 343 | p_part_curr = GET_CURRENT_COMPONENT(); |
| 344 | p_part_next = GET_THRD_OWNER(pth_next); |
| 345 | |
| 346 | if (scheduler_lock != SCHEDULER_LOCKED && pth_next != NULL && |
| 347 | p_part_curr != p_part_next) { |
| 348 | /* Check if there is enough room on stack to save more context */ |
| 349 | if ((p_curr_ctx->sp_limit + |
| 350 | sizeof(struct tfm_additional_context_t)) > __get_PSP()) { |
| 351 | tfm_core_panic(); |
| 352 | } |
| 353 | |
| 354 | CRITICAL_SECTION_ENTER(cs); |
| 355 | /* |
| 356 | * If required, let the platform update boundary based on its |
| 357 | * implementation. Change privilege, MPU or other configurations. |
| 358 | */ |
Chendi Sun | 0f7d282 | 2022-10-28 12:24:12 +0800 | [diff] [blame] | 359 | if (tfm_hal_boundary_need_switch(p_part_curr->boundary, |
| 360 | p_part_next->boundary)) { |
Xinyu Zhang | 6ad0703 | 2022-08-10 14:45:56 +0800 | [diff] [blame] | 361 | FIH_CALL(tfm_hal_activate_boundary, fih_rc, |
| 362 | p_part_next->p_ldinf, p_part_next->boundary); |
| 363 | if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) { |
Sherry Zhang | 049733e | 2022-04-20 21:37:51 +0800 | [diff] [blame] | 364 | tfm_core_panic(); |
| 365 | } |
| 366 | } |
| 367 | ARCH_FLUSH_FP_CONTEXT(); |
| 368 | |
| 369 | AAPCS_DUAL_U32_SET_A1(ctx_ctrls, (uint32_t)pth_next->p_context_ctrl); |
| 370 | |
| 371 | CURRENT_THREAD = pth_next; |
| 372 | CRITICAL_SECTION_LEAVE(cs); |
| 373 | } |
| 374 | |
| 375 | /* Update meta indicator */ |
| 376 | if (partition_meta_indicator_pos && (p_part_next->p_metadata)) { |
| 377 | *partition_meta_indicator_pos = (uintptr_t)(p_part_next->p_metadata); |
| 378 | } |
| 379 | return AAPCS_DUAL_U32_AS_U64(ctx_ctrls); |
| 380 | } |