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. |
Chris Brand | b4c2b00 | 2022-07-21 12:54:00 -0700 | [diff] [blame] | 3 | * Copyright (c) 2021-2022 Cypress Semiconductor Corporation (an Infineon |
| 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" |
Sherry Zhang | 049733e | 2022-04-20 21:37:51 +0800 | [diff] [blame] | 19 | #include "tfm_hal_memory_symbols.h" |
Ken Liu | 62bae59 | 2021-10-19 22:15:43 +0800 | [diff] [blame] | 20 | #include "tfm_hal_isolation.h" |
Kevin Peng | b288c52 | 2021-09-26 16:18:23 +0800 | [diff] [blame] | 21 | #include "tfm_hal_platform.h" |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 22 | #include "tfm_rpc.h" |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 23 | #include "ffm/backend.h" |
Ken Liu | 62bae59 | 2021-10-19 22:15:43 +0800 | [diff] [blame] | 24 | #include "utilities.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" |
| 29 | |
| 30 | /* Declare the global component list */ |
| 31 | struct partition_head_t partition_listhead; |
| 32 | |
Kevin Peng | 9f1a754 | 2022-02-07 16:32:27 +0800 | [diff] [blame] | 33 | #if CONFIG_TFM_PSA_API_CROSS_CALL == 1 |
Ken Liu | 63a176b | 2022-06-09 22:36:56 +0800 | [diff] [blame] | 34 | /* Instance for SPM_THREAD_CONTEXT */ |
Ken Liu | e07c3b7 | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 35 | |
Chris Brand | fe5adca | 2022-11-08 17:44:07 -0800 | [diff] [blame] | 36 | #ifdef CONFIG_TFM_USE_TRUSTZONE |
| 37 | struct 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 Liu | 63a176b | 2022-06-09 22:36:56 +0800 | [diff] [blame] | 40 | static uint8_t spm_thread_stack[CONFIG_TFM_SPM_THREAD_STACK_SIZE] __aligned(8); |
| 41 | ARCH_CLAIM_CTXCTRL_INSTANCE(spm_thread_context, |
| 42 | spm_thread_stack, |
| 43 | sizeof(spm_thread_stack)); |
| 44 | |
Ken Liu | e07c3b7 | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 45 | struct context_ctrl_t *p_spm_thread_context = &spm_thread_context; |
Ken Liu | e07c3b7 | 2021-10-14 16:19:13 +0800 | [diff] [blame] | 46 | #endif |
| 47 | |
| 48 | #endif |
| 49 | |
Sherry Zhang | 049733e | 2022-04-20 21:37:51 +0800 | [diff] [blame] | 50 | /* Indicator point to the partition meta */ |
| 51 | uintptr_t *partition_meta_indicator_pos; |
| 52 | |
| 53 | extern uint32_t scheduler_lock; |
| 54 | |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 55 | /* |
| 56 | * Query the state of current thread. |
| 57 | */ |
| 58 | static 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 | |
Summer Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 104 | static void prv_process_metadata(struct partition_t *p_pt) |
| 105 | { |
Kevin Peng | 43160d5 | 2022-02-11 13:35:56 +0800 | [diff] [blame] | 106 | const struct partition_load_info_t *p_pt_ldi; |
| 107 | const struct service_load_info_t *p_srv_ldi; |
| 108 | struct context_ctrl_t *ctx_ctrl; |
| 109 | struct runtime_metadata_t *p_rt_meta; |
| 110 | service_fn_t *p_sfn_table; |
| 111 | uint32_t allocate_size; |
Summer Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 112 | |
Kevin Peng | 43160d5 | 2022-02-11 13:35:56 +0800 | [diff] [blame] | 113 | p_pt_ldi = p_pt->p_ldinf; |
Chris Brand | 1fb796d | 2022-10-18 16:54:25 -0700 | [diff] [blame] | 114 | p_srv_ldi = LOAD_INFO_SERVICE(p_pt_ldi); |
Kevin Peng | 43160d5 | 2022-02-11 13:35:56 +0800 | [diff] [blame] | 115 | ctx_ctrl = &p_pt->ctx_ctrl; |
| 116 | |
| 117 | /* common runtime metadata */ |
| 118 | allocate_size = sizeof(*p_rt_meta); |
| 119 | |
| 120 | if (!IS_PARTITION_IPC_MODEL(p_pt_ldi)) { |
| 121 | /* SFN specific metadata - SFN function table */ |
| 122 | allocate_size += sizeof(service_fn_t) * p_pt_ldi->nservices; |
Summer Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 123 | } |
| 124 | |
Kevin Peng | 43160d5 | 2022-02-11 13:35:56 +0800 | [diff] [blame] | 125 | ARCH_CTXCTRL_ALLOCATE_STACK(ctx_ctrl, allocate_size); |
| 126 | p_rt_meta = (struct runtime_metadata_t *) |
| 127 | ARCH_CTXCTRL_ALLOCATED_PTR(ctx_ctrl); |
| 128 | |
| 129 | p_rt_meta->entry = p_pt_ldi->entry; |
| 130 | p_rt_meta->n_sfn = 0; |
| 131 | p_sfn_table = p_rt_meta->sfn_table; |
| 132 | |
| 133 | if (!IS_PARTITION_IPC_MODEL(p_pt_ldi)) { |
| 134 | /* SFN table. The signal bit of the service is the same index of SFN. */ |
| 135 | for (int i = 0; i < p_pt_ldi->nservices; i++) { |
| 136 | p_sfn_table[i] = (service_fn_t)p_srv_ldi[i].sfn; |
| 137 | } |
| 138 | |
| 139 | p_rt_meta->n_sfn = p_pt_ldi->nservices; |
| 140 | } |
| 141 | |
| 142 | p_pt->p_metadata = (void *)p_rt_meta; |
Summer Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 143 | } |
| 144 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 145 | /* |
| 146 | * 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] | 147 | * current thread and trigger scheduler. |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 148 | */ |
Ken Liu | 995a974 | 2022-05-18 19:28:30 +0800 | [diff] [blame] | 149 | psa_status_t backend_messaging(struct service_t *service, |
| 150 | struct conn_handle_t *handle) |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 151 | { |
| 152 | struct partition_t *p_owner = NULL; |
| 153 | psa_signal_t signal = 0; |
| 154 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 155 | if (!handle || !service || !service->p_ldinf || !service->partition) { |
Mingyang Sun | bb4a42a | 2021-12-14 15:18:52 +0800 | [diff] [blame] | 156 | return PSA_ERROR_PROGRAMMER_ERROR; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | p_owner = service->partition; |
| 160 | signal = service->p_ldinf->signal; |
| 161 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 162 | UNI_LIST_INSERT_AFTER(p_owner, handle, p_handles); |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 163 | |
| 164 | /* Messages put. Update signals */ |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 165 | backend_assert_signal(p_owner, signal); |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | * If it is a NS request via RPC, it is unnecessary to block current |
| 169 | * thread. |
| 170 | */ |
| 171 | |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 172 | if (!is_tfm_rpc_msg(handle)) { |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 173 | backend_wait_signals(handle->p_client, TFM_IPC_REPLY_SIGNAL); |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 174 | } |
| 175 | |
Mingyang Sun | aeca8e0 | 2022-02-24 14:47:56 +0800 | [diff] [blame] | 176 | handle->status = TFM_HANDLE_STATUS_ACTIVE; |
| 177 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 178 | return PSA_SUCCESS; |
| 179 | } |
| 180 | |
Ken Liu | 995a974 | 2022-05-18 19:28:30 +0800 | [diff] [blame] | 181 | psa_status_t backend_replying(struct conn_handle_t *handle, int32_t status) |
Ken Liu | 802a370 | 2021-10-15 12:09:56 +0800 | [diff] [blame] | 182 | { |
Mingyang Sun | a09adda | 2022-02-16 18:11:33 +0800 | [diff] [blame] | 183 | if (is_tfm_rpc_msg(handle)) { |
| 184 | tfm_rpc_client_call_reply(handle, status); |
Ken Liu | 802a370 | 2021-10-15 12:09:56 +0800 | [diff] [blame] | 185 | } else { |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 186 | handle->p_client->reply_value = (uintptr_t)status; |
| 187 | backend_assert_signal(handle->p_client, TFM_IPC_REPLY_SIGNAL); |
Ken Liu | 802a370 | 2021-10-15 12:09:56 +0800 | [diff] [blame] | 188 | } |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 189 | |
| 190 | /* |
| 191 | * 'psa_reply' exists in IPC model only and returns 'void'. Return |
| 192 | * 'PSA_SUCCESS' here always since SPM does not forward the status |
| 193 | * to the caller. |
| 194 | */ |
| 195 | return PSA_SUCCESS; |
Ken Liu | 802a370 | 2021-10-15 12:09:56 +0800 | [diff] [blame] | 196 | } |
| 197 | |
Summer Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 198 | extern void sprt_main(void); |
| 199 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 200 | /* Parameters are treated as assuredly */ |
Ken Liu | 995a974 | 2022-05-18 19:28:30 +0800 | [diff] [blame] | 201 | void backend_init_comp_assuredly(struct partition_t *p_pt, |
| 202 | uint32_t service_setting) |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 203 | { |
| 204 | const struct partition_load_info_t *p_pldi = p_pt->p_ldinf; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 205 | |
Kevin Peng | 613b417 | 2022-02-15 14:41:44 +0800 | [diff] [blame] | 206 | #if CONFIG_TFM_DOORBELL_API == 1 |
| 207 | p_pt->signals_allowed |= PSA_DOORBELL; |
| 208 | #endif /* CONFIG_TFM_DOORBELL_API == 1 */ |
| 209 | |
| 210 | p_pt->signals_allowed |= service_setting; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 211 | |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 212 | UNI_LISI_INIT_NODE(p_pt, p_handles); |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 213 | |
Ken Liu | bf4681f | 2022-02-11 11:15:03 +0800 | [diff] [blame] | 214 | ARCH_CTXCTRL_INIT(&p_pt->ctx_ctrl, |
| 215 | LOAD_ALLOCED_STACK_ADDR(p_pldi), |
| 216 | p_pldi->stack_size); |
| 217 | |
Chris Brand | 30106ba | 2022-01-13 13:48:50 -0800 | [diff] [blame] | 218 | watermark_stack(p_pt); |
| 219 | |
Summer Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 220 | prv_process_metadata(p_pt); |
| 221 | |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 222 | THRD_INIT(&p_pt->thrd, &p_pt->ctx_ctrl, |
| 223 | TO_THREAD_PRIORITY(PARTITION_PRIORITY(p_pldi->flags))); |
| 224 | |
Chris Brand | fe5adca | 2022-11-08 17:44:07 -0800 | [diff] [blame] | 225 | #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] | 226 | if (IS_PARTITION_NS_AGENT(p_pldi)) { |
Chris Brand | fe5adca | 2022-11-08 17:44:07 -0800 | [diff] [blame] | 227 | /* Get the context from ns_agent_tz */ |
| 228 | if (p_pldi->pid == 0) { |
| 229 | SPM_THREAD_CONTEXT = &p_pt->ctx_ctrl; |
| 230 | } |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 231 | } |
Summer Qin | 9544482 | 2022-01-27 11:22:00 +0800 | [diff] [blame] | 232 | #endif |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 233 | |
| 234 | thrd_start(&p_pt->thrd, |
Summer Qin | 596f555 | 2022-01-27 18:04:06 +0800 | [diff] [blame] | 235 | POSITION_TO_ENTRY(sprt_main, thrd_fn_t), |
Ken Liu | bf4681f | 2022-02-11 11:15:03 +0800 | [diff] [blame] | 236 | THRD_GENERAL_EXIT); |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 237 | } |
| 238 | |
Ken Liu | 995a974 | 2022-05-18 19:28:30 +0800 | [diff] [blame] | 239 | uint32_t backend_system_run(void) |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 240 | { |
Ken Liu | 62bae59 | 2021-10-19 22:15:43 +0800 | [diff] [blame] | 241 | uint32_t control; |
| 242 | struct partition_t *p_cur_pt; |
Xinyu Zhang | 6ad0703 | 2022-08-10 14:45:56 +0800 | [diff] [blame] | 243 | fih_int fih_rc = FIH_FAILURE; |
Ken Liu | 62bae59 | 2021-10-19 22:15:43 +0800 | [diff] [blame] | 244 | |
Sherry Zhang | 6868164 | 2022-06-24 13:36:33 +0800 | [diff] [blame] | 245 | #if CONFIG_TFM_PSA_API_CROSS_CALL == 1 |
Summer Qin | 1056d1c | 2022-10-19 16:07:15 +0800 | [diff] [blame] | 246 | SPM_ASSERT(SPM_THREAD_CONTEXT); |
Chris Brand | 3778bc1 | 2021-12-15 17:01:05 -0800 | [diff] [blame] | 247 | #endif |
| 248 | |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 249 | /* Init thread callback function. */ |
| 250 | thrd_set_query_callback(query_state); |
| 251 | |
Sherry Zhang | 049733e | 2022-04-20 21:37:51 +0800 | [diff] [blame] | 252 | partition_meta_indicator_pos = (uintptr_t *)hal_mem_sp_meta_start; |
Ken Liu | 62bae59 | 2021-10-19 22:15:43 +0800 | [diff] [blame] | 253 | control = thrd_start_scheduler(&CURRENT_THREAD); |
| 254 | |
| 255 | p_cur_pt = TO_CONTAINER(CURRENT_THREAD->p_context_ctrl, |
| 256 | struct partition_t, ctx_ctrl); |
| 257 | |
Xinyu Zhang | 6ad0703 | 2022-08-10 14:45:56 +0800 | [diff] [blame] | 258 | FIH_CALL(tfm_hal_activate_boundary, fih_rc, p_cur_pt->p_ldinf, p_cur_pt->boundary); |
| 259 | if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) { |
Ken Liu | 62bae59 | 2021-10-19 22:15:43 +0800 | [diff] [blame] | 260 | tfm_core_panic(); |
| 261 | } |
| 262 | |
| 263 | return control; |
Mingyang Sun | deae45d | 2021-09-06 15:31:07 +0800 | [diff] [blame] | 264 | } |
| 265 | |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 266 | psa_signal_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] | 267 | { |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 268 | struct critical_section_t cs_signal = CRITICAL_SECTION_STATIC_INIT; |
Mingyang Sun | 5c9529f | 2022-03-15 17:51:56 +0800 | [diff] [blame] | 269 | psa_signal_t ret_signal; |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 270 | |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 271 | if (!p_pt) { |
| 272 | tfm_core_panic(); |
Mingyang Sun | 5c9529f | 2022-03-15 17:51:56 +0800 | [diff] [blame] | 273 | } |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 274 | |
| 275 | CRITICAL_SECTION_ENTER(cs_signal); |
| 276 | |
| 277 | ret_signal = p_pt->signals_asserted & signals; |
| 278 | if (ret_signal == 0) { |
| 279 | p_pt->signals_waiting = signals; |
| 280 | } |
| 281 | |
| 282 | CRITICAL_SECTION_LEAVE(cs_signal); |
Mingyang Sun | 5c9529f | 2022-03-15 17:51:56 +0800 | [diff] [blame] | 283 | |
| 284 | return ret_signal; |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 285 | } |
| 286 | |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 287 | uint32_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] | 288 | { |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 289 | struct critical_section_t cs_signal = CRITICAL_SECTION_STATIC_INIT; |
| 290 | |
| 291 | if (!p_pt) { |
| 292 | tfm_core_panic(); |
shejia01 | a0ea10c | 2022-06-27 13:56:00 +0800 | [diff] [blame] | 293 | } |
shejia01 | 95a88bc | 2023-01-16 15:44:46 +0800 | [diff] [blame] | 294 | |
| 295 | CRITICAL_SECTION_ENTER(cs_signal); |
| 296 | p_pt->signals_asserted |= signal; |
| 297 | CRITICAL_SECTION_LEAVE(cs_signal); |
| 298 | |
| 299 | return PSA_SUCCESS; |
Kevin Peng | def92de | 2021-11-10 16:14:48 +0800 | [diff] [blame] | 300 | } |
| 301 | |
Sherry Zhang | 049733e | 2022-04-20 21:37:51 +0800 | [diff] [blame] | 302 | uint64_t ipc_schedule(void) |
| 303 | { |
Xinyu Zhang | 6ad0703 | 2022-08-10 14:45:56 +0800 | [diff] [blame] | 304 | fih_int fih_rc = FIH_FAILURE; |
Sherry Zhang | 049733e | 2022-04-20 21:37:51 +0800 | [diff] [blame] | 305 | AAPCS_DUAL_U32_T ctx_ctrls; |
| 306 | struct partition_t *p_part_curr, *p_part_next; |
| 307 | struct context_ctrl_t *p_curr_ctx; |
| 308 | struct thread_t *pth_next = thrd_next(); |
| 309 | struct critical_section_t cs = CRITICAL_SECTION_STATIC_INIT; |
| 310 | |
| 311 | p_curr_ctx = (struct context_ctrl_t *)(CURRENT_THREAD->p_context_ctrl); |
| 312 | |
| 313 | AAPCS_DUAL_U32_SET(ctx_ctrls, (uint32_t)p_curr_ctx, (uint32_t)p_curr_ctx); |
| 314 | |
| 315 | p_part_curr = GET_CURRENT_COMPONENT(); |
| 316 | p_part_next = GET_THRD_OWNER(pth_next); |
| 317 | |
| 318 | if (scheduler_lock != SCHEDULER_LOCKED && pth_next != NULL && |
| 319 | p_part_curr != p_part_next) { |
| 320 | /* Check if there is enough room on stack to save more context */ |
| 321 | if ((p_curr_ctx->sp_limit + |
| 322 | sizeof(struct tfm_additional_context_t)) > __get_PSP()) { |
| 323 | tfm_core_panic(); |
| 324 | } |
| 325 | |
| 326 | CRITICAL_SECTION_ENTER(cs); |
| 327 | /* |
| 328 | * If required, let the platform update boundary based on its |
| 329 | * implementation. Change privilege, MPU or other configurations. |
| 330 | */ |
Chendi Sun | 0f7d282 | 2022-10-28 12:24:12 +0800 | [diff] [blame] | 331 | if (tfm_hal_boundary_need_switch(p_part_curr->boundary, |
| 332 | p_part_next->boundary)) { |
Xinyu Zhang | 6ad0703 | 2022-08-10 14:45:56 +0800 | [diff] [blame] | 333 | FIH_CALL(tfm_hal_activate_boundary, fih_rc, |
| 334 | p_part_next->p_ldinf, p_part_next->boundary); |
| 335 | if (fih_not_eq(fih_rc, fih_int_encode(TFM_HAL_SUCCESS))) { |
Sherry Zhang | 049733e | 2022-04-20 21:37:51 +0800 | [diff] [blame] | 336 | tfm_core_panic(); |
| 337 | } |
| 338 | } |
| 339 | ARCH_FLUSH_FP_CONTEXT(); |
| 340 | |
| 341 | AAPCS_DUAL_U32_SET_A1(ctx_ctrls, (uint32_t)pth_next->p_context_ctrl); |
| 342 | |
| 343 | CURRENT_THREAD = pth_next; |
| 344 | CRITICAL_SECTION_LEAVE(cs); |
| 345 | } |
| 346 | |
| 347 | /* Update meta indicator */ |
| 348 | if (partition_meta_indicator_pos && (p_part_next->p_metadata)) { |
| 349 | *partition_meta_indicator_pos = (uintptr_t)(p_part_next->p_metadata); |
| 350 | } |
| 351 | return AAPCS_DUAL_U32_AS_U64(ctx_ctrls); |
| 352 | } |