blob: 4894fd2dd39a4acefa3650a58b60f189c23d4121 [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
20__attribute__((naked))
21static psa_flih_result_t tfm_flih_deprivileged_handling(void *p_pt,
22 uintptr_t fn_flih,
Kevin Pengca59ec02021-12-09 14:35:50 +080023 void *curr_component)
Kevin Peng3f67b2e2021-10-18 17:47:27 +080024{
Sherry Zhang9714d962022-03-02 10:52:46 +080025 __ASM volatile("SVC "M2S(TFM_SVC_PREPARE_DEPRIV_FLIH)" \n"
26 "BX LR \n"
27 );
Kevin Peng3f67b2e2021-10-18 17:47:27 +080028}
29
30struct irq_load_info_t *get_irq_info_for_signal(
31 const struct partition_load_info_t *p_ldinf,
32 psa_signal_t signal)
33{
34 size_t i;
35 struct irq_load_info_t *irq_info;
36
37 if (!IS_ONLY_ONE_BIT_IN_UINT32(signal)) {
38 return NULL;
39 }
40
41 irq_info = (struct irq_load_info_t *)LOAD_INFO_IRQ(p_ldinf);
42 for (i = 0; i < p_ldinf->nirqs; i++) {
43 if (irq_info[i].signal == signal) {
44 return &irq_info[i];
45 }
46 }
47
48 return NULL;
49}
50
51extern void tfm_flih_func_return(psa_flih_result_t result);
52
Kevin Pengf7a20d82021-12-13 14:38:37 +080053uint32_t tfm_flih_prepare_depriv_flih(struct partition_t *p_owner_sp,
54 uintptr_t flih_func)
Kevin Peng3f67b2e2021-10-18 17:47:27 +080055{
56 struct partition_t *p_curr_sp;
Kevin Pengca59ec02021-12-09 14:35:50 +080057 uintptr_t sp_base, sp_limit, curr_stack, ctx_stack;
Kevin Peng3f67b2e2021-10-18 17:47:27 +080058 struct context_ctrl_t flih_ctx_ctrl;
59
60 /* Come too early before runtime setup, should not happen. */
61 if (!CURRENT_THREAD) {
62 tfm_core_panic();
63 }
64
Kevin Pengca59ec02021-12-09 14:35:50 +080065 p_curr_sp = GET_CURRENT_COMPONENT();
66 sp_base = LOAD_ALLOCED_STACK_ADDR(p_owner_sp->p_ldinf)
67 + p_owner_sp->p_ldinf->stack_size;
68 sp_limit = LOAD_ALLOCED_STACK_ADDR(p_owner_sp->p_ldinf);
Kevin Peng3f67b2e2021-10-18 17:47:27 +080069
Kevin Pengca59ec02021-12-09 14:35:50 +080070 curr_stack = (uintptr_t)__get_PSP();
71 if (curr_stack < sp_base && curr_stack > sp_limit) {
72 /* The IRQ Partition's stack is being used */
73 ctx_stack = curr_stack;
Kevin Peng3f67b2e2021-10-18 17:47:27 +080074 } else {
Kevin Pengca59ec02021-12-09 14:35:50 +080075 ctx_stack =
76 ((struct context_ctrl_t *)p_owner_sp->thrd.p_context_ctrl)->sp;
Kevin Peng3f67b2e2021-10-18 17:47:27 +080077 }
78
Kevin Pengca59ec02021-12-09 14:35:50 +080079 if (p_owner_sp->p_boundaries != p_curr_sp->p_boundaries) {
80 tfm_hal_update_boundaries(p_owner_sp->p_ldinf,
81 p_owner_sp->p_boundaries);
82 }
83
84 /*
85 * The CURRENT_COMPONENT has been stored on MSP by the SVC call, safe to
86 * update it.
87 */
88 SET_CURRENT_COMPONENT(p_owner_sp);
89
Ken Liubf4681f2022-02-11 11:15:03 +080090 flih_ctx_ctrl.sp_limit = sp_limit;
91 flih_ctx_ctrl.sp = ctx_stack;
92
Kevin Peng3f67b2e2021-10-18 17:47:27 +080093 tfm_arch_init_context(&flih_ctx_ctrl,
Kevin Pengf7a20d82021-12-13 14:38:37 +080094 flih_func, NULL,
Ken Liubf4681f2022-02-11 11:15:03 +080095 (uintptr_t)tfm_flih_func_return);
Kevin Peng3f67b2e2021-10-18 17:47:27 +080096
97 (void)tfm_arch_refresh_hardware_context(&flih_ctx_ctrl);
98
99 return flih_ctx_ctrl.exc_ret;
100}
101
102/* Go back to ISR from FLIH functions */
Kevin Pengf7a20d82021-12-13 14:38:37 +0800103uint32_t tfm_flih_return_to_isr(psa_flih_result_t result,
104 struct context_flih_ret_t *p_ctx_flih_ret)
Kevin Peng3f67b2e2021-10-18 17:47:27 +0800105{
106 struct partition_t *p_prev_sp, *p_owner_sp;
Kevin Peng3f67b2e2021-10-18 17:47:27 +0800107
Kevin Pengca59ec02021-12-09 14:35:50 +0800108 p_prev_sp = (struct partition_t *)(p_ctx_flih_ret->state_ctx.r2);
109 p_owner_sp = GET_CURRENT_COMPONENT();
Kevin Peng3f67b2e2021-10-18 17:47:27 +0800110
111 if (p_owner_sp->p_boundaries != p_prev_sp->p_boundaries) {
112 tfm_hal_update_boundaries(p_prev_sp->p_ldinf,
113 p_prev_sp->p_boundaries);
114 }
115
Kevin Pengca59ec02021-12-09 14:35:50 +0800116 /* Restore current component */
117 SET_CURRENT_COMPONENT(p_prev_sp);
Kevin Peng3f67b2e2021-10-18 17:47:27 +0800118
Kevin Pengca59ec02021-12-09 14:35:50 +0800119 tfm_arch_set_psplim(p_ctx_flih_ret->psplim);
Kevin Peng3f67b2e2021-10-18 17:47:27 +0800120 __set_PSP(p_ctx_flih_ret->psp);
121
122 /* Set FLIH result to the ISR */
123 p_ctx_flih_ret->state_ctx.r0 = (uint32_t)result;
124
Kevin Pengca59ec02021-12-09 14:35:50 +0800125 return EXC_RETURN_HANDLER_S_MSP;
Kevin Peng3f67b2e2021-10-18 17:47:27 +0800126}
127
128void spm_handle_interrupt(void *p_pt, struct irq_load_info_t *p_ildi)
129{
130 psa_flih_result_t flih_result;
131 struct partition_t *p_part;
132
133 if (!p_pt || !p_ildi) {
134 tfm_core_panic();
135 }
136
137 p_part = (struct partition_t *)p_pt;
138
139 if (p_ildi->pid != p_part->p_ldinf->pid) {
140 tfm_core_panic();
141 }
142
143 if (p_ildi->flih_func == NULL) {
144 /* SLIH Model Handling */
145 tfm_hal_irq_disable(p_ildi->source);
146 flih_result = PSA_FLIH_SIGNAL;
147 } else {
148 /* FLIH Model Handling */
Kevin Penga40d29f2022-01-19 14:44:34 +0800149 if (GET_PARTITION_PRIVILEGED_MODE(p_part->p_ldinf) ==
Kevin Peng3f67b2e2021-10-18 17:47:27 +0800150 TFM_PARTITION_PRIVILEGED_MODE) {
151 flih_result = p_ildi->flih_func();
152 } else {
153 flih_result = tfm_flih_deprivileged_handling(
154 p_part,
155 (uintptr_t)p_ildi->flih_func,
Kevin Pengca59ec02021-12-09 14:35:50 +0800156 GET_CURRENT_COMPONENT());
Kevin Peng3f67b2e2021-10-18 17:47:27 +0800157 }
158 }
159
160 if (flih_result == PSA_FLIH_SIGNAL) {
161 spm_assert_signal(p_pt, p_ildi->signal);
Kevin Peng8a579692021-12-15 13:44:42 +0800162
163 if (THRD_EXPECTING_SCHEDULE()) {
164 tfm_arch_trigger_pendsv();
165 }
Kevin Peng3f67b2e2021-10-18 17:47:27 +0800166 }
167}