blob: cd24c2f3e7f41b3ea2ec0bf29766509e9f2a9cc6 [file] [log] [blame]
Kevin Peng3f67b2e2021-10-18 17:47:27 +08001/*
Kevin Penga40d29f2022-01-19 14:44:34 +08002 * Copyright (c) 2021-2022, Arm Limited. All rights reserved.
Kevin Peng3f67b2e2021-10-18 17:47:27 +08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include "interrupt.h"
9
10#include "bitops.h"
Kevin Pengca59ec02021-12-09 14:35:50 +080011#include "current.h"
Kevin Peng3f67b2e2021-10-18 17:47:27 +080012#include "tfm_arch.h"
13#include "tfm_hal_interrupt.h"
14#include "tfm_hal_isolation.h"
15#include "thread.h"
16#include "utilities.h"
17
18#include "load/spm_load_api.h"
19
Sherry Zhangd6dbe512022-03-23 16:42:32 +080020#if TFM_LVL != 1
21extern void tfm_flih_func_return(psa_flih_result_t result);
22
Kevin Peng3f67b2e2021-10-18 17:47:27 +080023__attribute__((naked))
24static psa_flih_result_t tfm_flih_deprivileged_handling(void *p_pt,
25 uintptr_t fn_flih,
Kevin Pengca59ec02021-12-09 14:35:50 +080026 void *curr_component)
Kevin Peng3f67b2e2021-10-18 17:47:27 +080027{
Sherry Zhang9714d962022-03-02 10:52:46 +080028 __ASM volatile("SVC "M2S(TFM_SVC_PREPARE_DEPRIV_FLIH)" \n"
29 "BX LR \n"
30 );
Kevin Peng3f67b2e2021-10-18 17:47:27 +080031}
32
Kevin Pengf7a20d82021-12-13 14:38:37 +080033uint32_t tfm_flih_prepare_depriv_flih(struct partition_t *p_owner_sp,
34 uintptr_t flih_func)
Kevin Peng3f67b2e2021-10-18 17:47:27 +080035{
36 struct partition_t *p_curr_sp;
Kevin Pengca59ec02021-12-09 14:35:50 +080037 uintptr_t sp_base, sp_limit, curr_stack, ctx_stack;
Kevin Peng3f67b2e2021-10-18 17:47:27 +080038 struct context_ctrl_t flih_ctx_ctrl;
39
40 /* Come too early before runtime setup, should not happen. */
41 if (!CURRENT_THREAD) {
42 tfm_core_panic();
43 }
44
Kevin Pengca59ec02021-12-09 14:35:50 +080045 p_curr_sp = GET_CURRENT_COMPONENT();
46 sp_base = LOAD_ALLOCED_STACK_ADDR(p_owner_sp->p_ldinf)
47 + p_owner_sp->p_ldinf->stack_size;
48 sp_limit = LOAD_ALLOCED_STACK_ADDR(p_owner_sp->p_ldinf);
Kevin Peng3f67b2e2021-10-18 17:47:27 +080049
Kevin Pengca59ec02021-12-09 14:35:50 +080050 curr_stack = (uintptr_t)__get_PSP();
51 if (curr_stack < sp_base && curr_stack > sp_limit) {
52 /* The IRQ Partition's stack is being used */
53 ctx_stack = curr_stack;
Kevin Peng3f67b2e2021-10-18 17:47:27 +080054 } else {
Kevin Pengca59ec02021-12-09 14:35:50 +080055 ctx_stack =
56 ((struct context_ctrl_t *)p_owner_sp->thrd.p_context_ctrl)->sp;
Kevin Peng3f67b2e2021-10-18 17:47:27 +080057 }
58
Ken Liu967ffa92022-05-25 15:13:34 +080059 if (p_owner_sp->boundary != p_curr_sp->boundary) {
60 tfm_hal_activate_boundary(p_owner_sp->p_ldinf,
61 p_owner_sp->boundary);
Kevin Pengca59ec02021-12-09 14:35:50 +080062 }
63
64 /*
65 * The CURRENT_COMPONENT has been stored on MSP by the SVC call, safe to
66 * update it.
67 */
68 SET_CURRENT_COMPONENT(p_owner_sp);
69
Ken Liubf4681f2022-02-11 11:15:03 +080070 flih_ctx_ctrl.sp_limit = sp_limit;
71 flih_ctx_ctrl.sp = ctx_stack;
72
Kevin Peng3f67b2e2021-10-18 17:47:27 +080073 tfm_arch_init_context(&flih_ctx_ctrl,
Kevin Pengf7a20d82021-12-13 14:38:37 +080074 flih_func, NULL,
Ken Liubf4681f2022-02-11 11:15:03 +080075 (uintptr_t)tfm_flih_func_return);
Kevin Peng3f67b2e2021-10-18 17:47:27 +080076
77 (void)tfm_arch_refresh_hardware_context(&flih_ctx_ctrl);
78
79 return flih_ctx_ctrl.exc_ret;
80}
81
82/* Go back to ISR from FLIH functions */
Kevin Pengf7a20d82021-12-13 14:38:37 +080083uint32_t tfm_flih_return_to_isr(psa_flih_result_t result,
84 struct context_flih_ret_t *p_ctx_flih_ret)
Kevin Peng3f67b2e2021-10-18 17:47:27 +080085{
86 struct partition_t *p_prev_sp, *p_owner_sp;
Kevin Peng3f67b2e2021-10-18 17:47:27 +080087
Kevin Pengca59ec02021-12-09 14:35:50 +080088 p_prev_sp = (struct partition_t *)(p_ctx_flih_ret->state_ctx.r2);
89 p_owner_sp = GET_CURRENT_COMPONENT();
Kevin Peng3f67b2e2021-10-18 17:47:27 +080090
Ken Liu967ffa92022-05-25 15:13:34 +080091 if (p_owner_sp->boundary != p_prev_sp->boundary) {
92 tfm_hal_activate_boundary(p_prev_sp->p_ldinf,
93 p_prev_sp->boundary);
Kevin Peng3f67b2e2021-10-18 17:47:27 +080094 }
95
Kevin Pengca59ec02021-12-09 14:35:50 +080096 /* Restore current component */
97 SET_CURRENT_COMPONENT(p_prev_sp);
Kevin Peng3f67b2e2021-10-18 17:47:27 +080098
Kevin Pengca59ec02021-12-09 14:35:50 +080099 tfm_arch_set_psplim(p_ctx_flih_ret->psplim);
Kevin Peng3f67b2e2021-10-18 17:47:27 +0800100 __set_PSP(p_ctx_flih_ret->psp);
101
102 /* Set FLIH result to the ISR */
103 p_ctx_flih_ret->state_ctx.r0 = (uint32_t)result;
104
Kevin Pengca59ec02021-12-09 14:35:50 +0800105 return EXC_RETURN_HANDLER_S_MSP;
Kevin Peng3f67b2e2021-10-18 17:47:27 +0800106}
Sherry Zhangd6dbe512022-03-23 16:42:32 +0800107#endif
108
109struct irq_load_info_t *get_irq_info_for_signal(
110 const struct partition_load_info_t *p_ldinf,
111 psa_signal_t signal)
112{
113 size_t i;
114 struct irq_load_info_t *irq_info;
115
116 if (!IS_ONLY_ONE_BIT_IN_UINT32(signal)) {
117 return NULL;
118 }
119
120 irq_info = (struct irq_load_info_t *)LOAD_INFO_IRQ(p_ldinf);
121 for (i = 0; i < p_ldinf->nirqs; i++) {
122 if (irq_info[i].signal == signal) {
123 return &irq_info[i];
124 }
125 }
126
127 return NULL;
128}
Kevin Peng3f67b2e2021-10-18 17:47:27 +0800129
130void spm_handle_interrupt(void *p_pt, struct irq_load_info_t *p_ildi)
131{
132 psa_flih_result_t flih_result;
133 struct partition_t *p_part;
134
135 if (!p_pt || !p_ildi) {
136 tfm_core_panic();
137 }
138
139 p_part = (struct partition_t *)p_pt;
140
141 if (p_ildi->pid != p_part->p_ldinf->pid) {
142 tfm_core_panic();
143 }
144
145 if (p_ildi->flih_func == NULL) {
146 /* SLIH Model Handling */
147 tfm_hal_irq_disable(p_ildi->source);
148 flih_result = PSA_FLIH_SIGNAL;
149 } else {
150 /* FLIH Model Handling */
Sherry Zhangd6dbe512022-03-23 16:42:32 +0800151#if TFM_LVL == 1
152 flih_result = p_ildi->flih_func();
153#else
Kevin Penga40d29f2022-01-19 14:44:34 +0800154 if (GET_PARTITION_PRIVILEGED_MODE(p_part->p_ldinf) ==
Kevin Peng3f67b2e2021-10-18 17:47:27 +0800155 TFM_PARTITION_PRIVILEGED_MODE) {
156 flih_result = p_ildi->flih_func();
157 } else {
158 flih_result = tfm_flih_deprivileged_handling(
159 p_part,
160 (uintptr_t)p_ildi->flih_func,
Kevin Pengca59ec02021-12-09 14:35:50 +0800161 GET_CURRENT_COMPONENT());
Kevin Peng3f67b2e2021-10-18 17:47:27 +0800162 }
Sherry Zhangd6dbe512022-03-23 16:42:32 +0800163#endif
Kevin Peng3f67b2e2021-10-18 17:47:27 +0800164 }
165
166 if (flih_result == PSA_FLIH_SIGNAL) {
167 spm_assert_signal(p_pt, p_ildi->signal);
Sherry Zhang049733e2022-04-20 21:37:51 +0800168 /* In SFN backend, there is only one thread, no thread switch. */
169#if CONFIG_TFM_SPM_BACKEND_SFN != 1
Kevin Peng8a579692021-12-15 13:44:42 +0800170 if (THRD_EXPECTING_SCHEDULE()) {
171 tfm_arch_trigger_pendsv();
172 }
Sherry Zhang049733e2022-04-20 21:37:51 +0800173#endif
Kevin Peng3f67b2e2021-10-18 17:47:27 +0800174 }
175}