TF-M Reference Manual  1.2.0
TrustedFirmware-M
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
tfm_arch_v8m.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 #ifndef __TFM_ARCH_V8M_H__
8 #define __TFM_ARCH_V8M_H__
9 
10 #include <stdint.h>
11 #include <stdbool.h>
12 
13 #include "cmsis_compiler.h"
14 #include "tfm_core_trustzone.h"
15 #include "utilities.h"
16 
17 #define EXC_RETURN_INDICATOR (0xFF << 24)
18 #define EXC_RETURN_RES1 (0x1FFFF << 7)
19 #define EXC_RETURN_SECURE_STACK (1 << 6)
20 #define EXC_RETURN_STACK_RULE (1 << 5)
21 #define EXC_RETURN_FPU_FRAME_BASIC (1 << 4)
22 #define EXC_RETURN_MODE_THREAD (1 << 3)
23 #define EXC_RETURN_STACK_PROCESS (1 << 2)
24 #define EXC_RETURN_RES0 (0 << 1)
25 #define EXC_RETURN_EXC_SECURE (1)
26 
27 /* Initial EXC_RETURN value in LR when a thread is loaded at the first time */
28 #define EXC_RETURN_THREAD_S_PSP \
29  EXC_RETURN_INDICATOR | EXC_RETURN_RES1 | \
30  EXC_RETURN_SECURE_STACK | EXC_RETURN_STACK_RULE | \
31  EXC_RETURN_FPU_FRAME_BASIC | EXC_RETURN_MODE_THREAD | \
32  EXC_RETURN_STACK_PROCESS | EXC_RETURN_RES0 | \
33  EXC_RETURN_EXC_SECURE
34 
35 #if defined(__ARM_ARCH_8_1M_MAIN__) || defined(__ARM_ARCH_8M_MAIN__)
36 struct tfm_arch_ctx_t {
37  uint32_t r4;
38  uint32_t r5;
39  uint32_t r6;
40  uint32_t r7;
41  uint32_t r8;
42  uint32_t r9;
43  uint32_t r10;
44  uint32_t r11;
45  uint32_t sp;
46  uint32_t sp_limit;
47  uint32_t dummy;
48  uint32_t lr;
49 };
50 #elif defined(__ARM_ARCH_8M_BASE__)
51 struct tfm_arch_ctx_t {
52  uint32_t r8;
53  uint32_t r9;
54  uint32_t r10;
55  uint32_t r11;
56  uint32_t r4;
57  uint32_t r5;
58  uint32_t r6;
59  uint32_t r7;
60  uint32_t sp;
61  uint32_t sp_limit;
62  uint32_t dummy;
63  uint32_t lr;
64 };
65 #endif
66 
67 /* Disable NS exceptions by setting NS PRIMASK to 1 */
68 #define TFM_NS_EXC_DISABLE() __TZ_set_PRIMASK_NS(1)
69 /* Enable NS exceptions by setting NS PRIMASK to 0 */
70 #define TFM_NS_EXC_ENABLE() __TZ_set_PRIMASK_NS(0)
71 
83 __STATIC_INLINE bool is_return_secure_stack(uint32_t lr)
84 {
85  return (lr & EXC_RETURN_SECURE_STACK);
86 }
87 
97 __STATIC_INLINE bool is_stack_alloc_fp_space(uint32_t lr)
98 {
99  return (lr & EXC_RETURN_FPU_FRAME_BASIC) ? false : true;
100 }
101 
107 __STATIC_INLINE void tfm_arch_set_psplim(uint32_t psplim)
108 {
109  __set_PSPLIM(psplim);
110 }
111 
121 __STATIC_INLINE uintptr_t tfm_arch_seal_thread_stack(uintptr_t stk)
122 {
123  TFM_CORE_ASSERT((stk & 0x7) == 0);
124  stk -= TFM_STACK_SEALED_SIZE;
125 
126  *((uint32_t *)stk) = TFM_STACK_SEAL_VALUE;
127  *((uint32_t *)(stk + 4)) = TFM_STACK_SEAL_VALUE;
128 
129  return stk;
130 }
131 
137 __STATIC_INLINE void tfm_arch_update_ctx(struct tfm_arch_ctx_t *p_actx)
138 {
139  __set_PSP(p_actx->sp);
140  __set_PSPLIM(p_actx->sp_limit);
141 }
142 
151 __STATIC_INLINE void tfm_arch_init_secure_msp(uint32_t msplim)
152 {
153  uint32_t mstk_adr = __get_MSP();
154 
155  /*
156  * Seal the main stack and update MSP to point below the stack seal.
157  * Set MSPLIM. As the initial 'main()' code is running under privileged PSP
158  * manipulating MSP works here.
159  */
160  TFM_CORE_ASSERT((mstk_adr & 0x7) == 0);
161  mstk_adr -= TFM_STACK_SEALED_SIZE;
162 
163  *((uint32_t *)mstk_adr) = TFM_STACK_SEAL_VALUE;
164  *((uint32_t *)(mstk_adr + 4)) = TFM_STACK_SEAL_VALUE;
165 
166  __set_MSP(mstk_adr);
167  __set_MSPLIM(msplim);
168 }
169 
170 #endif
__STATIC_INLINE bool is_stack_alloc_fp_space(uint32_t lr)
Check whether the stack frame for this exception has space allocated for Floating Point(FP) state inf...
Definition: tfm_arch_v8m.h:97
__STATIC_INLINE uintptr_t tfm_arch_seal_thread_stack(uintptr_t stk)
Seal the thread stack.
Definition: tfm_arch_v8m.h:121
#define TFM_STACK_SEALED_SIZE
__STATIC_INLINE void tfm_arch_update_ctx(struct tfm_arch_ctx_t *p_actx)
Update architecture context value into hardware.
Definition: tfm_arch_v8m.h:137
#define EXC_RETURN_FPU_FRAME_BASIC
Definition: tfm_arch_v8m.h:21
__STATIC_INLINE void tfm_arch_init_secure_msp(uint32_t msplim)
Set MSPLIM register and seal the MSP.
Definition: tfm_arch_v8m.h:151
#define TFM_CORE_ASSERT(cond)
Definition: utilities.h:21
__STATIC_INLINE bool is_return_secure_stack(uint32_t lr)
Check whether Secure or Non-secure stack is used to restore stack frame on exception return...
Definition: tfm_arch_v8m.h:83
#define TFM_STACK_SEAL_VALUE
__STATIC_INLINE void tfm_arch_set_psplim(uint32_t psplim)
Set PSPLIM register.
Definition: tfm_arch_v8m.h:107
#define EXC_RETURN_SECURE_STACK
Definition: tfm_arch_v8m.h:19