blob: 0ec79d87c4d8a679b4f28dfc1793d677b93fbce1 [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"
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +01009#include "exception_info.h"
Summer Qin90602de2020-08-04 10:23:39 +080010#include "tfm_secure_api.h"
Summer Qin90602de2020-08-04 10:23:39 +080011#include "tfm/tfm_spm_services.h"
12
TTornblom18b3bf02020-09-03 17:42:11 +020013#if defined(__ICCARM__)
Kevin Peng414523f2021-03-04 14:00:34 +080014uint32_t tfm_core_svc_handler(uint32_t *msp, uint32_t *psp, uint32_t exc_return);
TTornblom18b3bf02020-09-03 17:42:11 +020015#pragma required=tfm_core_svc_handler
16#endif
17
Summer Qin90602de2020-08-04 10:23:39 +080018nsfptr_t ns_entry;
19
20void jump_to_ns_code(void)
21{
22 /* Calls the non-secure Reset_Handler to jump to the non-secure binary */
23 ns_entry();
Ken Liu92e46a32020-07-25 22:58:00 +080024
25 tfm_core_panic();
Summer Qin90602de2020-08-04 10:23:39 +080026}
27
28__attribute__((naked))
29int32_t tfm_core_get_caller_client_id(int32_t *caller_client_id)
30{
31 __ASM volatile(
32 "SVC %0\n"
33 "BX LR\n"
34 : : "I" (TFM_SVC_GET_CALLER_CLIENT_ID));
35}
36
37__attribute__((naked))
Mark Horvath4924cf82020-08-05 15:38:17 +020038static int32_t tfm_spm_request(int32_t request_type)
Summer Qin90602de2020-08-04 10:23:39 +080039{
40 __ASM volatile(
41 "SVC %0\n"
42 "BX lr\n"
43 : : "I" (TFM_SVC_SPM_REQUEST));
44}
45
Summer Qin90602de2020-08-04 10:23:39 +080046int32_t tfm_spm_request_reset_vote(void)
47{
Mark Horvath4924cf82020-08-05 15:38:17 +020048 return tfm_spm_request((int32_t)TFM_SPM_REQUEST_RESET_VOTE);
Summer Qin90602de2020-08-04 10:23:39 +080049}
50
51__attribute__((naked))
52void tfm_enable_irq(psa_signal_t irq_signal)
53{
54 __ASM("SVC %0\n"
55 "BX LR\n"
56 : : "I" (TFM_SVC_ENABLE_IRQ));
57}
58
59__attribute__((naked))
60void tfm_disable_irq(psa_signal_t irq_signal)
61{
62 __ASM("SVC %0\n"
63 "BX LR\n"
64 : : "I" (TFM_SVC_DISABLE_IRQ));
65}
66
67__attribute__((naked))
68static psa_signal_t psa_wait_internal(psa_signal_t signal_mask,
69 uint32_t timeout)
70{
71 __ASM("SVC %0\n"
72 "BX LR\n"
73 : : "I" (TFM_SVC_PSA_WAIT));
74}
75
76psa_signal_t psa_wait(psa_signal_t signal_mask, uint32_t timeout)
77{
78 /* FIXME: By using the 'WFI' instruction this function blocks until an
79 * interrupt happens. It is necessary to do this here as tfm_core_psa_wait
80 * runs with the priority of the SVC, so it cannot be interrupted, so
81 * waiting in it for the required interrupt to happen is not an option.
82 */
83 psa_signal_t actual_signal_mask;
84
85 while (1) {
86 actual_signal_mask = psa_wait_internal(signal_mask, timeout);
87 if ((actual_signal_mask & signal_mask) != 0) {
88 return actual_signal_mask;
89 }
90 __WFI();
91 }
92}
93
94__attribute__((naked))
95void psa_eoi(psa_signal_t irq_signal)
96{
97 __ASM("SVC %0\n"
98 "BX LR\n"
99 : : "I" (TFM_SVC_PSA_EOI));
100}
101
102#if defined(__ARM_ARCH_8_1M_MAIN__) || defined(__ARM_ARCH_8M_MAIN__)
103__attribute__((section("SFN"), naked))
104int32_t tfm_core_sfn_request(const struct tfm_sfn_req_s *desc_ptr)
105{
106 __ASM volatile(
107 "PUSH {r4-r12, lr} \n"
108 "SVC %[SVC_REQ] \n"
109 "MOV r4, #0 \n"
110 "MOV r5, r4 \n"
111 "MOV r6, r4 \n"
112 "MOV r7, r4 \n"
113 "MOV r8, r4 \n"
114 "MOV r9, r4 \n"
115 "MOV r10, r4 \n"
116 "MOV r11, r4 \n"
117 "BLX lr \n"
118 "SVC %[SVC_RET] \n"
119 "POP {r4-r12, pc} \n"
120 : : [SVC_REQ] "I" (TFM_SVC_SFN_REQUEST),
121 [SVC_RET] "I" (TFM_SVC_SFN_RETURN)
122 );
123}
124
125__attribute__((section("SFN"), naked))
126void priv_irq_handler_main(uint32_t partition_id, uint32_t unpriv_handler,
127 uint32_t irq_signal, uint32_t irq_line)
128{
129 __ASM(
130 /* Save the callee saved registers*/
131 "PUSH {r4-r12, lr} \n"
132 /* Request SVC to configure environment for the unpriv IRQ handler */
133 "SVC %[SVC_REQ] \n"
134 /* clear the callee saved registers to prevent information leak */
135 "MOV r4, #0 \n"
136 "MOV r5, r4 \n"
137 "MOV r6, r4 \n"
138 "MOV r7, r4 \n"
139 "MOV r8, r4 \n"
140 "MOV r9, r4 \n"
141 "MOV r10, r4 \n"
142 "MOV r11, r4 \n"
143 /* Branch to the unprivileged handler */
144 "BLX lr \n"
145 /* Request SVC to reconfigure the environment of the interrupted
146 * partition
147 */
148 "SVC %[SVC_RET] \n"
149 /* restore callee saved registers and return */
150 "POP {r4-r12, pc} \n"
151 : : [SVC_REQ] "I" (TFM_SVC_DEPRIV_REQ)
152 , [SVC_RET] "I" (TFM_SVC_DEPRIV_RET)
153 );
154}
155#elif defined(__ARM_ARCH_8M_BASE__)
156__attribute__((section("SFN"), naked))
157int32_t tfm_core_sfn_request(const struct tfm_sfn_req_s *desc_ptr)
158{
159 __ASM volatile(
160 "PUSH {lr} \n"
161 "PUSH {r4-r7} \n"
162 "MOV r4, r8 \n"
163 "MOV r5, r9 \n"
164 "MOV r6, r10 \n"
165 "MOV r7, r11 \n"
166 "PUSH {r4-r7} \n"
167 "MOV r4, r12 \n"
168 "PUSH {r4} \n"
169 "SVC %[SVC_REQ] \n"
170 "MOVS r4, #0 \n"
171 "MOV r5, r4 \n"
172 "MOV r6, r4 \n"
173 "MOV r7, r4 \n"
174 "MOV r8, r4 \n"
175 "MOV r9, r4 \n"
176 "MOV r10, r4 \n"
177 "MOV r11, r4 \n"
178 "BLX lr \n"
179 "SVC %[SVC_RET] \n"
180 "POP {r4} \n"
181 "MOV r12, r4 \n"
182 "POP {r4-r7} \n"
183 "MOV r8, r4 \n"
184 "MOV r9, r5 \n"
185 "MOV r10, r6 \n"
186 "MOV r11, r7 \n"
187 "POP {r4-r7} \n"
188 "POP {pc} \n"
189 : : [SVC_REQ] "I" (TFM_SVC_SFN_REQUEST),
190 [SVC_RET] "I" (TFM_SVC_SFN_RETURN)
191 );
192}
193
194__attribute__((section("SFN"), naked))
195void priv_irq_handler_main(uint32_t partition_id, uint32_t unpriv_handler,
196 uint32_t irq_signal, uint32_t irq_line)
197{
198 __ASM(
199 /* Save the callee saved registers*/
200 "PUSH {r4-r7, lr} \n"
201 "MOV r4, r8 \n"
202 "MOV r5, r9 \n"
203 "MOV r6, r10 \n"
204 "MOV r7, r11 \n"
205 "PUSH {r4-r7} \n"
206 "MOV r4, r12 \n"
207 "PUSH {r4} \n"
208 /* Request SVC to configure environment for the unpriv IRQ handler */
209 "SVC %[SVC_REQ] \n"
210 /* clear the callee saved registers to prevent information leak */
211 "MOVS r4, #0 \n"
212 "MOV r5, r4 \n"
213 "MOV r6, r4 \n"
214 "MOV r7, r4 \n"
215 "MOV r8, r4 \n"
216 "MOV r9, r4 \n"
217 "MOV r10, r4 \n"
218 "MOV r11, r4 \n"
219 /* Branch to the unprivileged handler */
220 "BLX lr \n"
221 /* Request SVC to reconfigure the environment of the interrupted
222 * partition
223 */
224 "SVC %[SVC_RET] \n"
225 /* restore callee saved registers and return */
226 "POP {r4} \n"
227 "MOV r12, r4 \n"
228 "POP {r4-r7} \n"
229 "MOV r8, r4 \n"
230 "MOV r9, r5 \n"
231 "MOV r10, r6 \n"
232 "MOV r11, r7 \n"
233 "POP {r4-r7, pc} \n"
234 : : [SVC_REQ] "I" (TFM_SVC_DEPRIV_REQ)
235 , [SVC_RET] "I" (TFM_SVC_DEPRIV_RET)
236 );
237}
238#endif
239
240#if defined(__ARM_ARCH_8_1M_MAIN__) || \
241 defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
Ken Liu50e21092020-10-14 16:42:15 +0800242void tfm_arch_set_secure_exception_priorities(void)
Summer Qin90602de2020-08-04 10:23:39 +0800243{
244 uint32_t VECTKEY;
245 SCB_Type *scb = SCB;
246 uint32_t AIRCR;
247
248 /* Set PRIS flag in AIRCR */
249 AIRCR = scb->AIRCR;
250 VECTKEY = (~AIRCR & SCB_AIRCR_VECTKEYSTAT_Msk);
251 scb->AIRCR = SCB_AIRCR_PRIS_Msk |
252 VECTKEY |
253 (AIRCR & ~SCB_AIRCR_VECTKEY_Msk);
Summer Qin90602de2020-08-04 10:23:39 +0800254
Ken Liu50e21092020-10-14 16:42:15 +0800255#ifndef __ARM_ARCH_8M_BASE__
Jamie Fox3ede9712020-09-28 23:14:54 +0100256 NVIC_SetPriority(MemoryManagement_IRQn, 0);
257 NVIC_SetPriority(BusFault_IRQn, 0);
Jamie Fox3ede9712020-09-28 23:14:54 +0100258 NVIC_SetPriority(SecureFault_IRQn, 0);
259#endif
Ken Liu50e21092020-10-14 16:42:15 +0800260
261 /*
262 * Function based model needs no PendSV for scheduling,
263 * set its priority just higher than thread mode.
264 */
265 NVIC_SetPriority(SVCall_IRQn, 0);
266 NVIC_SetPriority(PendSV_IRQn, (1 << __NVIC_PRIO_BITS) - 1);
Jamie Fox3ede9712020-09-28 23:14:54 +0100267}
Ken Liu50e21092020-10-14 16:42:15 +0800268#else
269#error Function based model works on V8M series only.
270#endif
Jamie Fox3ede9712020-09-28 23:14:54 +0100271
Summer Qindea1f2c2021-01-11 14:46:34 +0800272void tfm_arch_config_extensions(void)
Jamie Fox45587672020-08-17 18:31:14 +0100273{
274#if defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)
275 /* Configure Secure access to the FPU only if the secure image is being
276 * built with the FPU in use. This avoids introducing extra interrupt
277 * latency when the FPU is not used by the SPE.
278 */
279#if defined (__FPU_USED) && (__FPU_USED == 1U)
280 /* Enable Secure privileged and unprivilged access to the FP Extension */
281 SCB->CPACR |= (3U << 10U*2U) /* enable CP10 full access */
282 | (3U << 11U*2U); /* enable CP11 full access */
283
284#if defined(__ARM_ARCH_8_1M_MAIN__) || defined(__ARM_ARCH_8M_MAIN__)
285 /* If the SPE will ever use the floating-point registers for sensitive data,
286 * then FPCCR.TS, FPCCR.CLRONRET and FPCCR.CLRONRETS must be set at
287 * initialisation and not changed again afterwards.
288 */
289 FPU->FPCCR |= FPU_FPCCR_TS_Msk
290 | FPU_FPCCR_CLRONRET_Msk
291 | FPU_FPCCR_CLRONRETS_Msk;
292#endif
293#endif
294
295#if defined(__ARM_ARCH_8_1M_MAIN__) || defined(__ARM_ARCH_8M_MAIN__)
296 /* Permit Non-secure access to the Floating-point Extension.
297 * Note: It is still necessary to set CPACR_NS to enable the FP Extension in
298 * the NSPE. This configuration is left to NS privileged software.
299 */
300 SCB->NSACR |= SCB_NSACR_CP10_Msk | SCB_NSACR_CP11_Msk;
301#endif
Summer Qindea1f2c2021-01-11 14:46:34 +0800302
303#if defined(__ARM_ARCH_8_1M_MAIN__)
304 SCB->CCR |= SCB_CCR_TRD_Msk;
305#endif
Jamie Fox45587672020-08-17 18:31:14 +0100306#endif
307}
308
Ken Liue0af44c2020-07-25 22:51:30 +0800309#if defined(__ARM_ARCH_8M_BASE__) || defined(__ARM_ARCH_8_1M_MAIN__) || defined(__ARM_ARCH_8M_MAIN__)
Summer Qin90602de2020-08-04 10:23:39 +0800310__attribute__((naked)) void SVC_Handler(void)
311{
312 __ASM volatile(
Ken Liue0af44c2020-07-25 22:51:30 +0800313#if !defined(__ICCARM__)
314 ".syntax unified \n"
315#endif
Kevin Peng414523f2021-03-04 14:00:34 +0800316 "MRS r0, MSP \n"
317 "MOV r2, lr \n"
318 "MOVS r3, #8 \n"
319 "TST r2, r3 \n"
Ken Liue0af44c2020-07-25 22:51:30 +0800320 "BNE from_thread \n"
321 /*
322 * This branch is taken when the code is being invoked from handler mode.
323 * This happens when a de-privileged interrupt handler is to be run. Seal
324 * the stack before de-privileging.
325 */
Kevin Peng414523f2021-03-04 14:00:34 +0800326 "LDR r1, =0xFEF5EDA5 \n"
327 "MOVS r3, r1 \n"
328 "PUSH {r1, r3} \n"
Ken Liue0af44c2020-07-25 22:51:30 +0800329 "from_thread: \n"
Kevin Peng414523f2021-03-04 14:00:34 +0800330 "MRS r1, PSP \n"
Summer Qin90602de2020-08-04 10:23:39 +0800331 "BL tfm_core_svc_handler \n"
Kevin Peng414523f2021-03-04 14:00:34 +0800332 "MOVS r1, #8 \n"
Ken Liue0af44c2020-07-25 22:51:30 +0800333 "TST r1, r0 \n"
334 "BNE to_thread \n"
335 /*
336 * This branch is taken when the code is going to return to handler mode.
337 * This happens after a de-privileged interrupt handler had been run. Pop
338 * the sealing from the stack.
339 */
340 "POP {r1, r2} \n"
341 "to_thread: \n"
Summer Qin90602de2020-08-04 10:23:39 +0800342 "BX r0 \n"
343 );
344}
345#elif defined(__ARM_ARCH_6M__) || defined(__ARM_ARCH_7M__) || \
346 defined(__ARM_ARCH_7EM__)
347__attribute__((naked)) void SVC_Handler(void)
348{
349 __ASM volatile(
Kevin Peng414523f2021-03-04 14:00:34 +0800350 "MRS r0, MSP \n"
351 "MRS r1, PSP \n"
352 "MOV r2, lr \n"
353 "BL tfm_core_svc_handler \n"
354 "BX r0 \n"
Summer Qin90602de2020-08-04 10:23:39 +0800355 );
356}
357#endif
Jamie Foxb78795a2020-09-28 20:39:06 +0100358
359__attribute__((naked)) void HardFault_Handler(void)
360{
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100361 EXCEPTION_INFO(EXCEPTION_TYPE_HARDFAULT);
362
Jamie Foxb78795a2020-09-28 20:39:06 +0100363 /* A HardFault may indicate corruption of secure state, so it is essential
364 * that Non-secure code does not regain control after one is raised.
365 * Returning from this exception could allow a pending NS exception to be
366 * taken, so the current solution is not to return.
367 */
368 __ASM volatile("b .");
369}
370
371__attribute__((naked)) void MemManage_Handler(void)
372{
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100373 EXCEPTION_INFO(EXCEPTION_TYPE_MEMFAULT);
374
Jamie Foxb78795a2020-09-28 20:39:06 +0100375 /* A MemManage fault may indicate corruption of secure state, so it is
376 * essential that Non-secure code does not regain control after one is
377 * raised. Returning from this exception could allow a pending NS exception
378 * to be taken, so the current solution is not to return.
379 */
380 __ASM volatile("b .");
381}
382
383__attribute__((naked)) void BusFault_Handler(void)
384{
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100385 EXCEPTION_INFO(EXCEPTION_TYPE_BUSFAULT);
386
Jamie Foxb78795a2020-09-28 20:39:06 +0100387 /* A BusFault may indicate corruption of secure state, so it is essential
388 * that Non-secure code does not regain control after one is raised.
389 * Returning from this exception could allow a pending NS exception to be
390 * taken, so the current solution is not to return.
391 */
392 __ASM volatile("b .");
393}
394
395__attribute__((naked)) void SecureFault_Handler(void)
396{
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100397 EXCEPTION_INFO(EXCEPTION_TYPE_SECUREFAULT);
398
Jamie Foxb78795a2020-09-28 20:39:06 +0100399 /* A SecureFault may indicate corruption of secure state, so it is essential
400 * that Non-secure code does not regain control after one is raised.
401 * Returning from this exception could allow a pending NS exception to be
402 * taken, so the current solution is not to return.
403 */
404 __ASM volatile("b .");
405}
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100406
407__attribute__((naked)) void UsageFault_Handler(void)
408{
409 EXCEPTION_INFO(EXCEPTION_TYPE_USAGEFAULT);
410 __ASM volatile("b .");
411}