blob: a873a4f365955ba12bff37ba98cfe01387bb1a17 [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 Pengb42ed862022-08-08 14:44:02 +080012#include "svc_num.h"
Kevin Peng3f67b2e2021-10-18 17:47:27 +080013#include "tfm_arch.h"
14#include "tfm_hal_interrupt.h"
15#include "tfm_hal_isolation.h"
16#include "thread.h"
17#include "utilities.h"
18
19#include "load/spm_load_api.h"
20
Sherry Zhangd6dbe512022-03-23 16:42:32 +080021#if TFM_LVL != 1
22extern void tfm_flih_func_return(psa_flih_result_t result);
23
Kevin Peng3f67b2e2021-10-18 17:47:27 +080024__attribute__((naked))
25static psa_flih_result_t tfm_flih_deprivileged_handling(void *p_pt,
26 uintptr_t fn_flih,
Kevin Pengca59ec02021-12-09 14:35:50 +080027 void *curr_component)
Kevin Peng3f67b2e2021-10-18 17:47:27 +080028{
Sherry Zhang9714d962022-03-02 10:52:46 +080029 __ASM volatile("SVC "M2S(TFM_SVC_PREPARE_DEPRIV_FLIH)" \n"
30 "BX LR \n"
31 );
Kevin Peng3f67b2e2021-10-18 17:47:27 +080032}
33
Kevin Pengf7a20d82021-12-13 14:38:37 +080034uint32_t tfm_flih_prepare_depriv_flih(struct partition_t *p_owner_sp,
35 uintptr_t flih_func)
Kevin Peng3f67b2e2021-10-18 17:47:27 +080036{
37 struct partition_t *p_curr_sp;
Kevin Pengca59ec02021-12-09 14:35:50 +080038 uintptr_t sp_base, sp_limit, curr_stack, ctx_stack;
Kevin Peng3f67b2e2021-10-18 17:47:27 +080039 struct context_ctrl_t flih_ctx_ctrl;
40
41 /* Come too early before runtime setup, should not happen. */
42 if (!CURRENT_THREAD) {
43 tfm_core_panic();
44 }
45
Kevin Pengca59ec02021-12-09 14:35:50 +080046 p_curr_sp = GET_CURRENT_COMPONENT();
47 sp_base = LOAD_ALLOCED_STACK_ADDR(p_owner_sp->p_ldinf)
48 + p_owner_sp->p_ldinf->stack_size;
49 sp_limit = LOAD_ALLOCED_STACK_ADDR(p_owner_sp->p_ldinf);
Kevin Peng3f67b2e2021-10-18 17:47:27 +080050
Kevin Pengca59ec02021-12-09 14:35:50 +080051 curr_stack = (uintptr_t)__get_PSP();
52 if (curr_stack < sp_base && curr_stack > sp_limit) {
53 /* The IRQ Partition's stack is being used */
54 ctx_stack = curr_stack;
Kevin Peng3f67b2e2021-10-18 17:47:27 +080055 } else {
Kevin Pengca59ec02021-12-09 14:35:50 +080056 ctx_stack =
57 ((struct context_ctrl_t *)p_owner_sp->thrd.p_context_ctrl)->sp;
Kevin Peng3f67b2e2021-10-18 17:47:27 +080058 }
59
Ken Liu967ffa92022-05-25 15:13:34 +080060 if (p_owner_sp->boundary != p_curr_sp->boundary) {
61 tfm_hal_activate_boundary(p_owner_sp->p_ldinf,
62 p_owner_sp->boundary);
Kevin Pengca59ec02021-12-09 14:35:50 +080063 }
64
65 /*
66 * The CURRENT_COMPONENT has been stored on MSP by the SVC call, safe to
67 * update it.
68 */
69 SET_CURRENT_COMPONENT(p_owner_sp);
70
Ken Liubf4681f2022-02-11 11:15:03 +080071 flih_ctx_ctrl.sp_limit = sp_limit;
72 flih_ctx_ctrl.sp = ctx_stack;
73
Kevin Peng3f67b2e2021-10-18 17:47:27 +080074 tfm_arch_init_context(&flih_ctx_ctrl,
Kevin Pengf7a20d82021-12-13 14:38:37 +080075 flih_func, NULL,
Ken Liubf4681f2022-02-11 11:15:03 +080076 (uintptr_t)tfm_flih_func_return);
Kevin Peng3f67b2e2021-10-18 17:47:27 +080077
78 (void)tfm_arch_refresh_hardware_context(&flih_ctx_ctrl);
79
80 return flih_ctx_ctrl.exc_ret;
81}
82
83/* Go back to ISR from FLIH functions */
Kevin Pengf7a20d82021-12-13 14:38:37 +080084uint32_t tfm_flih_return_to_isr(psa_flih_result_t result,
85 struct context_flih_ret_t *p_ctx_flih_ret)
Kevin Peng3f67b2e2021-10-18 17:47:27 +080086{
87 struct partition_t *p_prev_sp, *p_owner_sp;
Kevin Peng3f67b2e2021-10-18 17:47:27 +080088
Kevin Pengca59ec02021-12-09 14:35:50 +080089 p_prev_sp = (struct partition_t *)(p_ctx_flih_ret->state_ctx.r2);
90 p_owner_sp = GET_CURRENT_COMPONENT();
Kevin Peng3f67b2e2021-10-18 17:47:27 +080091
Ken Liu967ffa92022-05-25 15:13:34 +080092 if (p_owner_sp->boundary != p_prev_sp->boundary) {
93 tfm_hal_activate_boundary(p_prev_sp->p_ldinf,
94 p_prev_sp->boundary);
Kevin Peng3f67b2e2021-10-18 17:47:27 +080095 }
96
Kevin Pengca59ec02021-12-09 14:35:50 +080097 /* Restore current component */
98 SET_CURRENT_COMPONENT(p_prev_sp);
Kevin Peng3f67b2e2021-10-18 17:47:27 +080099
Kevin Pengca59ec02021-12-09 14:35:50 +0800100 tfm_arch_set_psplim(p_ctx_flih_ret->psplim);
Kevin Peng3f67b2e2021-10-18 17:47:27 +0800101 __set_PSP(p_ctx_flih_ret->psp);
102
103 /* Set FLIH result to the ISR */
104 p_ctx_flih_ret->state_ctx.r0 = (uint32_t)result;
105
Kevin Pengca59ec02021-12-09 14:35:50 +0800106 return EXC_RETURN_HANDLER_S_MSP;
Kevin Peng3f67b2e2021-10-18 17:47:27 +0800107}
Sherry Zhangd6dbe512022-03-23 16:42:32 +0800108#endif
109
110struct irq_load_info_t *get_irq_info_for_signal(
111 const struct partition_load_info_t *p_ldinf,
112 psa_signal_t signal)
113{
114 size_t i;
115 struct irq_load_info_t *irq_info;
116
117 if (!IS_ONLY_ONE_BIT_IN_UINT32(signal)) {
118 return NULL;
119 }
120
121 irq_info = (struct irq_load_info_t *)LOAD_INFO_IRQ(p_ldinf);
122 for (i = 0; i < p_ldinf->nirqs; i++) {
123 if (irq_info[i].signal == signal) {
124 return &irq_info[i];
125 }
126 }
127
128 return NULL;
129}
Kevin Peng3f67b2e2021-10-18 17:47:27 +0800130
131void spm_handle_interrupt(void *p_pt, struct irq_load_info_t *p_ildi)
132{
133 psa_flih_result_t flih_result;
134 struct partition_t *p_part;
135
136 if (!p_pt || !p_ildi) {
137 tfm_core_panic();
138 }
139
140 p_part = (struct partition_t *)p_pt;
141
142 if (p_ildi->pid != p_part->p_ldinf->pid) {
143 tfm_core_panic();
144 }
145
146 if (p_ildi->flih_func == NULL) {
147 /* SLIH Model Handling */
148 tfm_hal_irq_disable(p_ildi->source);
149 flih_result = PSA_FLIH_SIGNAL;
150 } else {
151 /* FLIH Model Handling */
Sherry Zhangd6dbe512022-03-23 16:42:32 +0800152#if TFM_LVL == 1
153 flih_result = p_ildi->flih_func();
154#else
Kevin Penga40d29f2022-01-19 14:44:34 +0800155 if (GET_PARTITION_PRIVILEGED_MODE(p_part->p_ldinf) ==
Kevin Peng3f67b2e2021-10-18 17:47:27 +0800156 TFM_PARTITION_PRIVILEGED_MODE) {
157 flih_result = p_ildi->flih_func();
158 } else {
159 flih_result = tfm_flih_deprivileged_handling(
160 p_part,
161 (uintptr_t)p_ildi->flih_func,
Kevin Pengca59ec02021-12-09 14:35:50 +0800162 GET_CURRENT_COMPONENT());
Kevin Peng3f67b2e2021-10-18 17:47:27 +0800163 }
Sherry Zhangd6dbe512022-03-23 16:42:32 +0800164#endif
Kevin Peng3f67b2e2021-10-18 17:47:27 +0800165 }
166
167 if (flih_result == PSA_FLIH_SIGNAL) {
168 spm_assert_signal(p_pt, p_ildi->signal);
Sherry Zhang049733e2022-04-20 21:37:51 +0800169 /* In SFN backend, there is only one thread, no thread switch. */
170#if CONFIG_TFM_SPM_BACKEND_SFN != 1
Kevin Peng8a579692021-12-15 13:44:42 +0800171 if (THRD_EXPECTING_SCHEDULE()) {
172 tfm_arch_trigger_pendsv();
173 }
Sherry Zhang049733e2022-04-20 21:37:51 +0800174#endif
Kevin Peng3f67b2e2021-10-18 17:47:27 +0800175 }
176}