blob: 6fe40dbd957c5ae172f30761819da9cd57046e28 [file] [log] [blame]
Summer Qin90602de2020-08-04 10:23:39 +08001/*
Summer Qindea1f2c2021-01-11 14:46:34 +08002 * Copyright (c) 2018-2021, Arm Limited. All rights reserved.
Summer Qin90602de2020-08-04 10:23:39 +08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Summer Qin9c1fba12020-08-12 15:49:12 +08008#include "arch.h"
Xinyu Zhang3a453242021-04-16 17:57:09 +08009#include "compiler_ext_defs.h"
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +010010#include "exception_info.h"
Summer Qin90602de2020-08-04 10:23:39 +080011#include "tfm_secure_api.h"
Summer Qin90602de2020-08-04 10:23:39 +080012#include "tfm/tfm_spm_services.h"
13
TTornblom18b3bf02020-09-03 17:42:11 +020014#if defined(__ICCARM__)
Kevin Peng414523f2021-03-04 14:00:34 +080015uint32_t tfm_core_svc_handler(uint32_t *msp, uint32_t *psp, uint32_t exc_return);
Xinyu Zhang3a453242021-04-16 17:57:09 +080016#pragma required = tfm_core_svc_handler
TTornblom18b3bf02020-09-03 17:42:11 +020017#endif
18
Summer Qin90602de2020-08-04 10:23:39 +080019nsfptr_t ns_entry;
20
21void jump_to_ns_code(void)
22{
23 /* Calls the non-secure Reset_Handler to jump to the non-secure binary */
24 ns_entry();
Ken Liu92e46a32020-07-25 22:58:00 +080025
26 tfm_core_panic();
Summer Qin90602de2020-08-04 10:23:39 +080027}
28
29__attribute__((naked))
30int32_t tfm_core_get_caller_client_id(int32_t *caller_client_id)
31{
32 __ASM volatile(
33 "SVC %0\n"
34 "BX LR\n"
35 : : "I" (TFM_SVC_GET_CALLER_CLIENT_ID));
36}
37
38__attribute__((naked))
Mark Horvath4924cf82020-08-05 15:38:17 +020039static int32_t tfm_spm_request(int32_t request_type)
Summer Qin90602de2020-08-04 10:23:39 +080040{
41 __ASM volatile(
42 "SVC %0\n"
43 "BX lr\n"
44 : : "I" (TFM_SVC_SPM_REQUEST));
45}
46
Summer Qin90602de2020-08-04 10:23:39 +080047int32_t tfm_spm_request_reset_vote(void)
48{
Mark Horvath4924cf82020-08-05 15:38:17 +020049 return tfm_spm_request((int32_t)TFM_SPM_REQUEST_RESET_VOTE);
Summer Qin90602de2020-08-04 10:23:39 +080050}
51
52__attribute__((naked))
53void tfm_enable_irq(psa_signal_t irq_signal)
54{
55 __ASM("SVC %0\n"
56 "BX LR\n"
57 : : "I" (TFM_SVC_ENABLE_IRQ));
58}
59
60__attribute__((naked))
61void tfm_disable_irq(psa_signal_t irq_signal)
62{
63 __ASM("SVC %0\n"
64 "BX LR\n"
65 : : "I" (TFM_SVC_DISABLE_IRQ));
66}
67
68__attribute__((naked))
69static psa_signal_t psa_wait_internal(psa_signal_t signal_mask,
70 uint32_t timeout)
71{
72 __ASM("SVC %0\n"
73 "BX LR\n"
74 : : "I" (TFM_SVC_PSA_WAIT));
75}
76
77psa_signal_t psa_wait(psa_signal_t signal_mask, uint32_t timeout)
78{
79 /* FIXME: By using the 'WFI' instruction this function blocks until an
80 * interrupt happens. It is necessary to do this here as tfm_core_psa_wait
81 * runs with the priority of the SVC, so it cannot be interrupted, so
82 * waiting in it for the required interrupt to happen is not an option.
83 */
84 psa_signal_t actual_signal_mask;
85
86 while (1) {
87 actual_signal_mask = psa_wait_internal(signal_mask, timeout);
88 if ((actual_signal_mask & signal_mask) != 0) {
89 return actual_signal_mask;
90 }
91 __WFI();
92 }
93}
94
95__attribute__((naked))
96void psa_eoi(psa_signal_t irq_signal)
97{
98 __ASM("SVC %0\n"
99 "BX LR\n"
100 : : "I" (TFM_SVC_PSA_EOI));
101}
102
103#if defined(__ARM_ARCH_8_1M_MAIN__) || defined(__ARM_ARCH_8M_MAIN__)
Xinyu Zhang3a453242021-04-16 17:57:09 +0800104__section("SFN") __naked
Summer Qin90602de2020-08-04 10:23:39 +0800105int32_t tfm_core_sfn_request(const struct tfm_sfn_req_s *desc_ptr)
106{
107 __ASM volatile(
108 "PUSH {r4-r12, lr} \n"
109 "SVC %[SVC_REQ] \n"
110 "MOV r4, #0 \n"
111 "MOV r5, r4 \n"
112 "MOV r6, r4 \n"
113 "MOV r7, r4 \n"
114 "MOV r8, r4 \n"
115 "MOV r9, r4 \n"
116 "MOV r10, r4 \n"
117 "MOV r11, r4 \n"
118 "BLX lr \n"
119 "SVC %[SVC_RET] \n"
120 "POP {r4-r12, pc} \n"
121 : : [SVC_REQ] "I" (TFM_SVC_SFN_REQUEST),
122 [SVC_RET] "I" (TFM_SVC_SFN_RETURN)
123 );
124}
125
Xinyu Zhang3a453242021-04-16 17:57:09 +0800126__section("SFN") __naked
Summer Qin90602de2020-08-04 10:23:39 +0800127void priv_irq_handler_main(uint32_t partition_id, uint32_t unpriv_handler,
128 uint32_t irq_signal, uint32_t irq_line)
129{
130 __ASM(
131 /* Save the callee saved registers*/
132 "PUSH {r4-r12, lr} \n"
133 /* Request SVC to configure environment for the unpriv IRQ handler */
134 "SVC %[SVC_REQ] \n"
135 /* clear the callee saved registers to prevent information leak */
136 "MOV r4, #0 \n"
137 "MOV r5, r4 \n"
138 "MOV r6, r4 \n"
139 "MOV r7, r4 \n"
140 "MOV r8, r4 \n"
141 "MOV r9, r4 \n"
142 "MOV r10, r4 \n"
143 "MOV r11, r4 \n"
144 /* Branch to the unprivileged handler */
145 "BLX lr \n"
146 /* Request SVC to reconfigure the environment of the interrupted
147 * partition
148 */
149 "SVC %[SVC_RET] \n"
150 /* restore callee saved registers and return */
151 "POP {r4-r12, pc} \n"
152 : : [SVC_REQ] "I" (TFM_SVC_DEPRIV_REQ)
153 , [SVC_RET] "I" (TFM_SVC_DEPRIV_RET)
154 );
155}
156#elif defined(__ARM_ARCH_8M_BASE__)
Xinyu Zhang3a453242021-04-16 17:57:09 +0800157__section("SFN") __naked
Summer Qin90602de2020-08-04 10:23:39 +0800158int32_t tfm_core_sfn_request(const struct tfm_sfn_req_s *desc_ptr)
159{
160 __ASM volatile(
161 "PUSH {lr} \n"
162 "PUSH {r4-r7} \n"
163 "MOV r4, r8 \n"
164 "MOV r5, r9 \n"
165 "MOV r6, r10 \n"
166 "MOV r7, r11 \n"
167 "PUSH {r4-r7} \n"
168 "MOV r4, r12 \n"
169 "PUSH {r4} \n"
170 "SVC %[SVC_REQ] \n"
171 "MOVS r4, #0 \n"
172 "MOV r5, r4 \n"
173 "MOV r6, r4 \n"
174 "MOV r7, r4 \n"
175 "MOV r8, r4 \n"
176 "MOV r9, r4 \n"
177 "MOV r10, r4 \n"
178 "MOV r11, r4 \n"
179 "BLX lr \n"
180 "SVC %[SVC_RET] \n"
181 "POP {r4} \n"
182 "MOV r12, r4 \n"
183 "POP {r4-r7} \n"
184 "MOV r8, r4 \n"
185 "MOV r9, r5 \n"
186 "MOV r10, r6 \n"
187 "MOV r11, r7 \n"
188 "POP {r4-r7} \n"
189 "POP {pc} \n"
190 : : [SVC_REQ] "I" (TFM_SVC_SFN_REQUEST),
191 [SVC_RET] "I" (TFM_SVC_SFN_RETURN)
192 );
193}
194
Xinyu Zhang3a453242021-04-16 17:57:09 +0800195__section("SFN") __naked
Summer Qin90602de2020-08-04 10:23:39 +0800196void priv_irq_handler_main(uint32_t partition_id, uint32_t unpriv_handler,
197 uint32_t irq_signal, uint32_t irq_line)
198{
199 __ASM(
200 /* Save the callee saved registers*/
201 "PUSH {r4-r7, lr} \n"
202 "MOV r4, r8 \n"
203 "MOV r5, r9 \n"
204 "MOV r6, r10 \n"
205 "MOV r7, r11 \n"
206 "PUSH {r4-r7} \n"
207 "MOV r4, r12 \n"
208 "PUSH {r4} \n"
209 /* Request SVC to configure environment for the unpriv IRQ handler */
210 "SVC %[SVC_REQ] \n"
211 /* clear the callee saved registers to prevent information leak */
212 "MOVS r4, #0 \n"
213 "MOV r5, r4 \n"
214 "MOV r6, r4 \n"
215 "MOV r7, r4 \n"
216 "MOV r8, r4 \n"
217 "MOV r9, r4 \n"
218 "MOV r10, r4 \n"
219 "MOV r11, r4 \n"
220 /* Branch to the unprivileged handler */
221 "BLX lr \n"
222 /* Request SVC to reconfigure the environment of the interrupted
223 * partition
224 */
225 "SVC %[SVC_RET] \n"
226 /* restore callee saved registers and return */
227 "POP {r4} \n"
228 "MOV r12, r4 \n"
229 "POP {r4-r7} \n"
230 "MOV r8, r4 \n"
231 "MOV r9, r5 \n"
232 "MOV r10, r6 \n"
233 "MOV r11, r7 \n"
234 "POP {r4-r7, pc} \n"
235 : : [SVC_REQ] "I" (TFM_SVC_DEPRIV_REQ)
236 , [SVC_RET] "I" (TFM_SVC_DEPRIV_RET)
237 );
238}
239#endif
240
241#if defined(__ARM_ARCH_8_1M_MAIN__) || \
242 defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
Ken Liu50e21092020-10-14 16:42:15 +0800243void tfm_arch_set_secure_exception_priorities(void)
Summer Qin90602de2020-08-04 10:23:39 +0800244{
245 uint32_t VECTKEY;
246 SCB_Type *scb = SCB;
247 uint32_t AIRCR;
248
249 /* Set PRIS flag in AIRCR */
250 AIRCR = scb->AIRCR;
251 VECTKEY = (~AIRCR & SCB_AIRCR_VECTKEYSTAT_Msk);
252 scb->AIRCR = SCB_AIRCR_PRIS_Msk |
253 VECTKEY |
254 (AIRCR & ~SCB_AIRCR_VECTKEY_Msk);
Summer Qin90602de2020-08-04 10:23:39 +0800255
Ken Liu50e21092020-10-14 16:42:15 +0800256#ifndef __ARM_ARCH_8M_BASE__
Jamie Fox3ede9712020-09-28 23:14:54 +0100257 NVIC_SetPriority(MemoryManagement_IRQn, 0);
258 NVIC_SetPriority(BusFault_IRQn, 0);
Jamie Fox3ede9712020-09-28 23:14:54 +0100259 NVIC_SetPriority(SecureFault_IRQn, 0);
260#endif
Ken Liu50e21092020-10-14 16:42:15 +0800261
262 /*
263 * Function based model needs no PendSV for scheduling,
264 * set its priority just higher than thread mode.
265 */
266 NVIC_SetPriority(SVCall_IRQn, 0);
267 NVIC_SetPriority(PendSV_IRQn, (1 << __NVIC_PRIO_BITS) - 1);
Jamie Fox3ede9712020-09-28 23:14:54 +0100268}
Ken Liu50e21092020-10-14 16:42:15 +0800269#else
270#error Function based model works on V8M series only.
271#endif
Jamie Fox3ede9712020-09-28 23:14:54 +0100272
Summer Qindea1f2c2021-01-11 14:46:34 +0800273void tfm_arch_config_extensions(void)
Jamie Fox45587672020-08-17 18:31:14 +0100274{
Xinyu Zhang3a453242021-04-16 17:57:09 +0800275#if defined(__FPU_PRESENT) && (__FPU_PRESENT == 1U)
Jamie Fox45587672020-08-17 18:31:14 +0100276 /* Configure Secure access to the FPU only if the secure image is being
277 * built with the FPU in use. This avoids introducing extra interrupt
278 * latency when the FPU is not used by the SPE.
279 */
Xinyu Zhang3a453242021-04-16 17:57:09 +0800280#if defined(__FPU_USED) && (__FPU_USED == 1U)
Jamie Fox45587672020-08-17 18:31:14 +0100281 /* Enable Secure privileged and unprivilged access to the FP Extension */
282 SCB->CPACR |= (3U << 10U*2U) /* enable CP10 full access */
283 | (3U << 11U*2U); /* enable CP11 full access */
284
285#if defined(__ARM_ARCH_8_1M_MAIN__) || defined(__ARM_ARCH_8M_MAIN__)
286 /* If the SPE will ever use the floating-point registers for sensitive data,
287 * then FPCCR.TS, FPCCR.CLRONRET and FPCCR.CLRONRETS must be set at
288 * initialisation and not changed again afterwards.
289 */
290 FPU->FPCCR |= FPU_FPCCR_TS_Msk
291 | FPU_FPCCR_CLRONRET_Msk
292 | FPU_FPCCR_CLRONRETS_Msk;
293#endif
294#endif
295
296#if defined(__ARM_ARCH_8_1M_MAIN__) || defined(__ARM_ARCH_8M_MAIN__)
297 /* Permit Non-secure access to the Floating-point Extension.
298 * Note: It is still necessary to set CPACR_NS to enable the FP Extension in
299 * the NSPE. This configuration is left to NS privileged software.
300 */
301 SCB->NSACR |= SCB_NSACR_CP10_Msk | SCB_NSACR_CP11_Msk;
302#endif
Summer Qindea1f2c2021-01-11 14:46:34 +0800303
304#if defined(__ARM_ARCH_8_1M_MAIN__)
305 SCB->CCR |= SCB_CCR_TRD_Msk;
306#endif
Jamie Fox45587672020-08-17 18:31:14 +0100307#endif
308}
309
Xinyu Zhang3a453242021-04-16 17:57:09 +0800310#if defined(__ARM_ARCH_8M_BASE__) || \
311 defined(__ARM_ARCH_8_1M_MAIN__) || \
312 defined(__ARM_ARCH_8M_MAIN__)
Summer Qin90602de2020-08-04 10:23:39 +0800313__attribute__((naked)) void SVC_Handler(void)
314{
315 __ASM volatile(
Ken Liue0af44c2020-07-25 22:51:30 +0800316#if !defined(__ICCARM__)
317 ".syntax unified \n"
318#endif
Kevin Peng414523f2021-03-04 14:00:34 +0800319 "MRS r0, MSP \n"
320 "MOV r2, lr \n"
321 "MOVS r3, #8 \n"
322 "TST r2, r3 \n"
Ken Liue0af44c2020-07-25 22:51:30 +0800323 "BNE from_thread \n"
324 /*
325 * This branch is taken when the code is being invoked from handler mode.
326 * This happens when a de-privileged interrupt handler is to be run. Seal
327 * the stack before de-privileging.
328 */
Kevin Peng414523f2021-03-04 14:00:34 +0800329 "LDR r1, =0xFEF5EDA5 \n"
330 "MOVS r3, r1 \n"
331 "PUSH {r1, r3} \n"
Ken Liue0af44c2020-07-25 22:51:30 +0800332 "from_thread: \n"
Kevin Peng414523f2021-03-04 14:00:34 +0800333 "MRS r1, PSP \n"
Summer Qin90602de2020-08-04 10:23:39 +0800334 "BL tfm_core_svc_handler \n"
Kevin Peng414523f2021-03-04 14:00:34 +0800335 "MOVS r1, #8 \n"
Ken Liue0af44c2020-07-25 22:51:30 +0800336 "TST r1, r0 \n"
337 "BNE to_thread \n"
338 /*
339 * This branch is taken when the code is going to return to handler mode.
340 * This happens after a de-privileged interrupt handler had been run. Pop
341 * the sealing from the stack.
342 */
343 "POP {r1, r2} \n"
344 "to_thread: \n"
Summer Qin90602de2020-08-04 10:23:39 +0800345 "BX r0 \n"
346 );
347}
348#elif defined(__ARM_ARCH_6M__) || defined(__ARM_ARCH_7M__) || \
349 defined(__ARM_ARCH_7EM__)
350__attribute__((naked)) void SVC_Handler(void)
351{
352 __ASM volatile(
Kevin Peng414523f2021-03-04 14:00:34 +0800353 "MRS r0, MSP \n"
354 "MRS r1, PSP \n"
355 "MOV r2, lr \n"
356 "BL tfm_core_svc_handler \n"
357 "BX r0 \n"
Summer Qin90602de2020-08-04 10:23:39 +0800358 );
359}
360#endif
Jamie Foxb78795a2020-09-28 20:39:06 +0100361
362__attribute__((naked)) void HardFault_Handler(void)
363{
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100364 EXCEPTION_INFO(EXCEPTION_TYPE_HARDFAULT);
365
Jamie Foxb78795a2020-09-28 20:39:06 +0100366 /* A HardFault may indicate corruption of secure state, so it is essential
367 * that Non-secure code does not regain control after one is raised.
368 * Returning from this exception could allow a pending NS exception to be
369 * taken, so the current solution is not to return.
370 */
371 __ASM volatile("b .");
372}
373
374__attribute__((naked)) void MemManage_Handler(void)
375{
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100376 EXCEPTION_INFO(EXCEPTION_TYPE_MEMFAULT);
377
Jamie Foxb78795a2020-09-28 20:39:06 +0100378 /* A MemManage fault may indicate corruption of secure state, so it is
379 * essential that Non-secure code does not regain control after one is
380 * raised. Returning from this exception could allow a pending NS exception
381 * to be taken, so the current solution is not to return.
382 */
383 __ASM volatile("b .");
384}
385
386__attribute__((naked)) void BusFault_Handler(void)
387{
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100388 EXCEPTION_INFO(EXCEPTION_TYPE_BUSFAULT);
389
Jamie Foxb78795a2020-09-28 20:39:06 +0100390 /* A BusFault may indicate corruption of secure state, so it is essential
391 * that Non-secure code does not regain control after one is raised.
392 * Returning from this exception could allow a pending NS exception to be
393 * taken, so the current solution is not to return.
394 */
395 __ASM volatile("b .");
396}
397
398__attribute__((naked)) void SecureFault_Handler(void)
399{
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100400 EXCEPTION_INFO(EXCEPTION_TYPE_SECUREFAULT);
401
Jamie Foxb78795a2020-09-28 20:39:06 +0100402 /* A SecureFault may indicate corruption of secure state, so it is essential
403 * that Non-secure code does not regain control after one is raised.
404 * Returning from this exception could allow a pending NS exception to be
405 * taken, so the current solution is not to return.
406 */
407 __ASM volatile("b .");
408}
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100409
410__attribute__((naked)) void UsageFault_Handler(void)
411{
412 EXCEPTION_INFO(EXCEPTION_TYPE_USAGEFAULT);
413 __ASM volatile("b .");
414}