blob: b237e1637cd6b0eff55d1a0bc07fb20c690411be [file] [log] [blame]
Summer Qin90602de2020-08-04 10:23:39 +08001/*
2 * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Summer Qin9c1fba12020-08-12 15:49:12 +08008#include "arch.h"
Summer Qin90602de2020-08-04 10:23:39 +08009#include "tfm_secure_api.h"
Summer Qin90602de2020-08-04 10:23:39 +080010#include "tfm/tfm_spm_services.h"
11
TTornblom18b3bf02020-09-03 17:42:11 +020012#if defined(__ICCARM__)
13uint32_t tfm_core_svc_handler(uint32_t *svc_args, uint32_t lr, uint32_t *msp);
14#pragma required=tfm_core_svc_handler
15#endif
16
Summer Qin90602de2020-08-04 10:23:39 +080017nsfptr_t ns_entry;
18
19void jump_to_ns_code(void)
20{
21 /* Calls the non-secure Reset_Handler to jump to the non-secure binary */
22 ns_entry();
23}
24
25__attribute__((naked))
26int32_t tfm_core_get_caller_client_id(int32_t *caller_client_id)
27{
28 __ASM volatile(
29 "SVC %0\n"
30 "BX LR\n"
31 : : "I" (TFM_SVC_GET_CALLER_CLIENT_ID));
32}
33
34__attribute__((naked))
Mark Horvath4924cf82020-08-05 15:38:17 +020035static int32_t tfm_spm_request(int32_t request_type)
Summer Qin90602de2020-08-04 10:23:39 +080036{
37 __ASM volatile(
38 "SVC %0\n"
39 "BX lr\n"
40 : : "I" (TFM_SVC_SPM_REQUEST));
41}
42
Summer Qin90602de2020-08-04 10:23:39 +080043int32_t tfm_spm_request_reset_vote(void)
44{
Mark Horvath4924cf82020-08-05 15:38:17 +020045 return tfm_spm_request((int32_t)TFM_SPM_REQUEST_RESET_VOTE);
Summer Qin90602de2020-08-04 10:23:39 +080046}
47
48__attribute__((naked))
49void tfm_enable_irq(psa_signal_t irq_signal)
50{
51 __ASM("SVC %0\n"
52 "BX LR\n"
53 : : "I" (TFM_SVC_ENABLE_IRQ));
54}
55
56__attribute__((naked))
57void tfm_disable_irq(psa_signal_t irq_signal)
58{
59 __ASM("SVC %0\n"
60 "BX LR\n"
61 : : "I" (TFM_SVC_DISABLE_IRQ));
62}
63
64__attribute__((naked))
65static psa_signal_t psa_wait_internal(psa_signal_t signal_mask,
66 uint32_t timeout)
67{
68 __ASM("SVC %0\n"
69 "BX LR\n"
70 : : "I" (TFM_SVC_PSA_WAIT));
71}
72
73psa_signal_t psa_wait(psa_signal_t signal_mask, uint32_t timeout)
74{
75 /* FIXME: By using the 'WFI' instruction this function blocks until an
76 * interrupt happens. It is necessary to do this here as tfm_core_psa_wait
77 * runs with the priority of the SVC, so it cannot be interrupted, so
78 * waiting in it for the required interrupt to happen is not an option.
79 */
80 psa_signal_t actual_signal_mask;
81
82 while (1) {
83 actual_signal_mask = psa_wait_internal(signal_mask, timeout);
84 if ((actual_signal_mask & signal_mask) != 0) {
85 return actual_signal_mask;
86 }
87 __WFI();
88 }
89}
90
91__attribute__((naked))
92void psa_eoi(psa_signal_t irq_signal)
93{
94 __ASM("SVC %0\n"
95 "BX LR\n"
96 : : "I" (TFM_SVC_PSA_EOI));
97}
98
99#if defined(__ARM_ARCH_8_1M_MAIN__) || defined(__ARM_ARCH_8M_MAIN__)
100__attribute__((section("SFN"), naked))
101int32_t tfm_core_sfn_request(const struct tfm_sfn_req_s *desc_ptr)
102{
103 __ASM volatile(
104 "PUSH {r4-r12, lr} \n"
105 "SVC %[SVC_REQ] \n"
106 "MOV r4, #0 \n"
107 "MOV r5, r4 \n"
108 "MOV r6, r4 \n"
109 "MOV r7, r4 \n"
110 "MOV r8, r4 \n"
111 "MOV r9, r4 \n"
112 "MOV r10, r4 \n"
113 "MOV r11, r4 \n"
114 "BLX lr \n"
115 "SVC %[SVC_RET] \n"
116 "POP {r4-r12, pc} \n"
117 : : [SVC_REQ] "I" (TFM_SVC_SFN_REQUEST),
118 [SVC_RET] "I" (TFM_SVC_SFN_RETURN)
119 );
120}
121
122__attribute__((section("SFN"), naked))
123void priv_irq_handler_main(uint32_t partition_id, uint32_t unpriv_handler,
124 uint32_t irq_signal, uint32_t irq_line)
125{
126 __ASM(
127 /* Save the callee saved registers*/
128 "PUSH {r4-r12, lr} \n"
129 /* Request SVC to configure environment for the unpriv IRQ handler */
130 "SVC %[SVC_REQ] \n"
131 /* clear the callee saved registers to prevent information leak */
132 "MOV r4, #0 \n"
133 "MOV r5, r4 \n"
134 "MOV r6, r4 \n"
135 "MOV r7, r4 \n"
136 "MOV r8, r4 \n"
137 "MOV r9, r4 \n"
138 "MOV r10, r4 \n"
139 "MOV r11, r4 \n"
140 /* Branch to the unprivileged handler */
141 "BLX lr \n"
142 /* Request SVC to reconfigure the environment of the interrupted
143 * partition
144 */
145 "SVC %[SVC_RET] \n"
146 /* restore callee saved registers and return */
147 "POP {r4-r12, pc} \n"
148 : : [SVC_REQ] "I" (TFM_SVC_DEPRIV_REQ)
149 , [SVC_RET] "I" (TFM_SVC_DEPRIV_RET)
150 );
151}
152#elif defined(__ARM_ARCH_8M_BASE__)
153__attribute__((section("SFN"), naked))
154int32_t tfm_core_sfn_request(const struct tfm_sfn_req_s *desc_ptr)
155{
156 __ASM volatile(
157 "PUSH {lr} \n"
158 "PUSH {r4-r7} \n"
159 "MOV r4, r8 \n"
160 "MOV r5, r9 \n"
161 "MOV r6, r10 \n"
162 "MOV r7, r11 \n"
163 "PUSH {r4-r7} \n"
164 "MOV r4, r12 \n"
165 "PUSH {r4} \n"
166 "SVC %[SVC_REQ] \n"
167 "MOVS r4, #0 \n"
168 "MOV r5, r4 \n"
169 "MOV r6, r4 \n"
170 "MOV r7, r4 \n"
171 "MOV r8, r4 \n"
172 "MOV r9, r4 \n"
173 "MOV r10, r4 \n"
174 "MOV r11, r4 \n"
175 "BLX lr \n"
176 "SVC %[SVC_RET] \n"
177 "POP {r4} \n"
178 "MOV r12, r4 \n"
179 "POP {r4-r7} \n"
180 "MOV r8, r4 \n"
181 "MOV r9, r5 \n"
182 "MOV r10, r6 \n"
183 "MOV r11, r7 \n"
184 "POP {r4-r7} \n"
185 "POP {pc} \n"
186 : : [SVC_REQ] "I" (TFM_SVC_SFN_REQUEST),
187 [SVC_RET] "I" (TFM_SVC_SFN_RETURN)
188 );
189}
190
191__attribute__((section("SFN"), naked))
192void priv_irq_handler_main(uint32_t partition_id, uint32_t unpriv_handler,
193 uint32_t irq_signal, uint32_t irq_line)
194{
195 __ASM(
196 /* Save the callee saved registers*/
197 "PUSH {r4-r7, lr} \n"
198 "MOV r4, r8 \n"
199 "MOV r5, r9 \n"
200 "MOV r6, r10 \n"
201 "MOV r7, r11 \n"
202 "PUSH {r4-r7} \n"
203 "MOV r4, r12 \n"
204 "PUSH {r4} \n"
205 /* Request SVC to configure environment for the unpriv IRQ handler */
206 "SVC %[SVC_REQ] \n"
207 /* clear the callee saved registers to prevent information leak */
208 "MOVS r4, #0 \n"
209 "MOV r5, r4 \n"
210 "MOV r6, r4 \n"
211 "MOV r7, r4 \n"
212 "MOV r8, r4 \n"
213 "MOV r9, r4 \n"
214 "MOV r10, r4 \n"
215 "MOV r11, r4 \n"
216 /* Branch to the unprivileged handler */
217 "BLX lr \n"
218 /* Request SVC to reconfigure the environment of the interrupted
219 * partition
220 */
221 "SVC %[SVC_RET] \n"
222 /* restore callee saved registers and return */
223 "POP {r4} \n"
224 "MOV r12, r4 \n"
225 "POP {r4-r7} \n"
226 "MOV r8, r4 \n"
227 "MOV r9, r5 \n"
228 "MOV r10, r6 \n"
229 "MOV r11, r7 \n"
230 "POP {r4-r7, pc} \n"
231 : : [SVC_REQ] "I" (TFM_SVC_DEPRIV_REQ)
232 , [SVC_RET] "I" (TFM_SVC_DEPRIV_RET)
233 );
234}
235#endif
236
237#if defined(__ARM_ARCH_8_1M_MAIN__) || \
238 defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
Ken Liu50e21092020-10-14 16:42:15 +0800239void tfm_arch_set_secure_exception_priorities(void)
Summer Qin90602de2020-08-04 10:23:39 +0800240{
241 uint32_t VECTKEY;
242 SCB_Type *scb = SCB;
243 uint32_t AIRCR;
244
245 /* Set PRIS flag in AIRCR */
246 AIRCR = scb->AIRCR;
247 VECTKEY = (~AIRCR & SCB_AIRCR_VECTKEYSTAT_Msk);
248 scb->AIRCR = SCB_AIRCR_PRIS_Msk |
249 VECTKEY |
250 (AIRCR & ~SCB_AIRCR_VECTKEY_Msk);
Summer Qin90602de2020-08-04 10:23:39 +0800251
Ken Liu50e21092020-10-14 16:42:15 +0800252#ifndef __ARM_ARCH_8M_BASE__
Jamie Fox3ede9712020-09-28 23:14:54 +0100253 NVIC_SetPriority(MemoryManagement_IRQn, 0);
254 NVIC_SetPriority(BusFault_IRQn, 0);
Jamie Fox3ede9712020-09-28 23:14:54 +0100255 NVIC_SetPriority(SecureFault_IRQn, 0);
256#endif
Ken Liu50e21092020-10-14 16:42:15 +0800257
258 /*
259 * Function based model needs no PendSV for scheduling,
260 * set its priority just higher than thread mode.
261 */
262 NVIC_SetPriority(SVCall_IRQn, 0);
263 NVIC_SetPriority(PendSV_IRQn, (1 << __NVIC_PRIO_BITS) - 1);
Jamie Fox3ede9712020-09-28 23:14:54 +0100264}
Ken Liu50e21092020-10-14 16:42:15 +0800265#else
266#error Function based model works on V8M series only.
267#endif
Jamie Fox3ede9712020-09-28 23:14:54 +0100268
Jamie Fox45587672020-08-17 18:31:14 +0100269void tfm_arch_configure_coprocessors(void)
270{
271#if defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)
272 /* Configure Secure access to the FPU only if the secure image is being
273 * built with the FPU in use. This avoids introducing extra interrupt
274 * latency when the FPU is not used by the SPE.
275 */
276#if defined (__FPU_USED) && (__FPU_USED == 1U)
277 /* Enable Secure privileged and unprivilged access to the FP Extension */
278 SCB->CPACR |= (3U << 10U*2U) /* enable CP10 full access */
279 | (3U << 11U*2U); /* enable CP11 full access */
280
281#if defined(__ARM_ARCH_8_1M_MAIN__) || defined(__ARM_ARCH_8M_MAIN__)
282 /* If the SPE will ever use the floating-point registers for sensitive data,
283 * then FPCCR.TS, FPCCR.CLRONRET and FPCCR.CLRONRETS must be set at
284 * initialisation and not changed again afterwards.
285 */
286 FPU->FPCCR |= FPU_FPCCR_TS_Msk
287 | FPU_FPCCR_CLRONRET_Msk
288 | FPU_FPCCR_CLRONRETS_Msk;
289#endif
290#endif
291
292#if defined(__ARM_ARCH_8_1M_MAIN__) || defined(__ARM_ARCH_8M_MAIN__)
293 /* Permit Non-secure access to the Floating-point Extension.
294 * Note: It is still necessary to set CPACR_NS to enable the FP Extension in
295 * the NSPE. This configuration is left to NS privileged software.
296 */
297 SCB->NSACR |= SCB_NSACR_CP10_Msk | SCB_NSACR_CP11_Msk;
298#endif
299#endif
300}
301
Ken Liue0af44c2020-07-25 22:51:30 +0800302#if defined(__ARM_ARCH_8M_BASE__) || defined(__ARM_ARCH_8_1M_MAIN__) || defined(__ARM_ARCH_8M_MAIN__)
Summer Qin90602de2020-08-04 10:23:39 +0800303__attribute__((naked)) void SVC_Handler(void)
304{
305 __ASM volatile(
Ken Liue0af44c2020-07-25 22:51:30 +0800306#if !defined(__ICCARM__)
307 ".syntax unified \n"
308#endif
309 "MRS r0, PSP \n"
Summer Qin90602de2020-08-04 10:23:39 +0800310 "MRS r2, MSP \n"
311 "MOVS r1, #4 \n"
312 "MOV r3, lr \n"
Summer Qin90602de2020-08-04 10:23:39 +0800313 "TST r1, r3 \n"
Ken Liue0af44c2020-07-25 22:51:30 +0800314 "BNE from_thread \n"
315 /*
316 * This branch is taken when the code is being invoked from handler mode.
317 * This happens when a de-privileged interrupt handler is to be run. Seal
318 * the stack before de-privileging.
319 */
320 "LDR r0, =0xFEF5EDA5 \n"
321 "MOVS r3, r0 \n"
322 "PUSH {r0, r3} \n"
323 /* Overwrite r0 with MSP */
324 "MOV r0, r2 \n"
325 "from_thread: \n"
Summer Qin90602de2020-08-04 10:23:39 +0800326 "MOV r1, lr \n"
327 "BL tfm_core_svc_handler \n"
Ken Liue0af44c2020-07-25 22:51:30 +0800328 "MOVS r1, #4 \n"
329 "TST r1, r0 \n"
330 "BNE to_thread \n"
331 /*
332 * This branch is taken when the code is going to return to handler mode.
333 * This happens after a de-privileged interrupt handler had been run. Pop
334 * the sealing from the stack.
335 */
336 "POP {r1, r2} \n"
337 "to_thread: \n"
Summer Qin90602de2020-08-04 10:23:39 +0800338 "BX r0 \n"
339 );
340}
341#elif defined(__ARM_ARCH_6M__) || defined(__ARM_ARCH_7M__) || \
342 defined(__ARM_ARCH_7EM__)
343__attribute__((naked)) void SVC_Handler(void)
344{
345 __ASM volatile(
346 "MOVS r0, #4 \n" /* Check store SP in thread mode to r0 */
347 "MOV r1, lr \n"
348 "TST r0, r1 \n"
349 "BEQ handler \n"
350 "MRS r0, PSP \n" /* Coming from thread mode */
351 "B sp_stored \n"
352 "handler: \n"
353 "BX lr \n" /* Coming from handler mode */
354 "sp_stored: \n"
355 "MOV r1, lr \n"
356 "BL tfm_core_svc_handler \n"
357 "BX r0 \n"
358 );
359}
360#endif
Jamie Foxb78795a2020-09-28 20:39:06 +0100361
362__attribute__((naked)) void HardFault_Handler(void)
363{
364 /* A HardFault may indicate corruption of secure state, so it is essential
365 * that Non-secure code does not regain control after one is raised.
366 * Returning from this exception could allow a pending NS exception to be
367 * taken, so the current solution is not to return.
368 */
369 __ASM volatile("b .");
370}
371
372__attribute__((naked)) void MemManage_Handler(void)
373{
374 /* A MemManage fault may indicate corruption of secure state, so it is
375 * essential that Non-secure code does not regain control after one is
376 * raised. Returning from this exception could allow a pending NS exception
377 * to be taken, so the current solution is not to return.
378 */
379 __ASM volatile("b .");
380}
381
382__attribute__((naked)) void BusFault_Handler(void)
383{
384 /* A BusFault may indicate corruption of secure state, so it is essential
385 * that Non-secure code does not regain control after one is raised.
386 * Returning from this exception could allow a pending NS exception to be
387 * taken, so the current solution is not to return.
388 */
389 __ASM volatile("b .");
390}
391
392__attribute__((naked)) void SecureFault_Handler(void)
393{
394 /* A SecureFault may indicate corruption of secure state, so it is essential
395 * that Non-secure code does not regain control after one is raised.
396 * Returning from this exception could allow a pending NS exception to be
397 * taken, so the current solution is not to return.
398 */
399 __ASM volatile("b .");
400}