1 /*
2 * Copyright (c) 2021, Nordic Semiconductor ASA. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <string.h>
8 #include "tfm_arch.h"
9 #include "exception_info.h"
10 #include "tfm_spm_log.h"
11
12 struct exception_info_t {
13 uint32_t EXC_RETURN; /* EXC_RETURN value in LR. */
14 uint32_t MSP; /* (Secure) MSP. */
15 uint32_t PSP; /* (Secure) PSP. */
16 uint32_t *EXC_FRAME; /* Exception frame on stack. */
17 uint32_t EXC_FRAME_COPY[8]; /* Copy of the basic exception frame. */
18 uint32_t xPSR; /* Program Status Registers. */
19
20 #ifdef FAULT_STATUS_PRESENT
21 uint32_t CFSR; /* Configurable Fault Status Register. */
22 uint32_t HFSR; /* Hard Fault Status Register. */
23 uint32_t BFAR; /* Bus Fault address register. */
24 uint32_t BFARVALID; /* Whether BFAR contains a valid address. */
25 uint32_t MMFAR; /* MemManage Fault address register. */
26 uint32_t MMARVALID; /* Whether MMFAR contains a valid address. */
27 #ifdef TRUSTZONE_PRESENT
28 uint32_t SFSR; /* SecureFault Status Register. */
29 uint32_t SFAR; /* SecureFault Address Register. */
30 uint32_t SFARVALID; /* Whether SFAR contains a valid address. */
31 #endif
32
33 #endif
34 };
35
36 static struct exception_info_t exception_info;
37
38 /**
39 * \brief Check whether the exception was triggered in thread or handler mode.
40 *
41 * \param[in] lr LR register containing the EXC_RETURN value.
42 *
43 * \retval true The exception will return to thread mode.
44 */
is_return_thread_mode(uint32_t lr)45 __STATIC_INLINE bool is_return_thread_mode(uint32_t lr)
46 {
47 #if defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__)
48 return !((lr == EXC_RETURN_HANDLER) || (lr == EXC_RETURN_HANDLER_FPU));
49 #elif defined(__ARM_ARCH_8M_BASE__) || defined(__ARM_ARCH_8M_MAIN__) \
50 || defined(__ARM_ARCH_8_1M_MAIN__)
51 return (lr & EXC_RETURN_MODE);
52 #else
53 return !(lr == EXC_RETURN_HANDLER);
54 #endif
55 }
56
57 /**
58 * \brief Check whether the PSP or MSP is used to restore stack frame on
59 * exception return.
60 *
61 * \param[in] lr LR register containing the EXC_RETURN value.
62 *
63 * \retval true The exception frame is on the PSP
64 */
is_return_psp(uint32_t lr)65 __STATIC_INLINE bool is_return_psp(uint32_t lr)
66 {
67 #if defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__)
68 return ((lr == EXC_RETURN_THREAD_PSP) || (lr == EXC_RETURN_THREAD_PSP_FPU));
69 #elif defined(__ARM_ARCH_8M_BASE__) || defined(__ARM_ARCH_8M_MAIN__) \
70 || defined(__ARM_ARCH_8_1M_MAIN__)
71 /* PSP is used only if SPSEL is set, and we came from thread mode. */
72 return ((lr & EXC_RETURN_SPSEL) && is_return_thread_mode(lr));
73 #else
74 return (lr == EXC_RETURN_THREAD_PSP);
75 #endif
76 }
77
78 /**
79 * \brief Get a pointer to the current exception frame
80 *
81 * \param[in] lr LR register containing the EXC_RETURN value.
82 * \param[in] msp The MSP at the start of the exception handler.
83 * \param[in] psp The PSP at the start of the exception handler.
84 *
85 * \return A pointer to the current exception frame.
86 */
87 __STATIC_INLINE
get_exception_frame(uint32_t lr,uint32_t msp,uint32_t psp)88 uint32_t *get_exception_frame(uint32_t lr, uint32_t msp, uint32_t psp)
89 {
90 #if defined(__ARM_ARCH_8M_BASE__) || defined(__ARM_ARCH_8M_MAIN__) \
91 || defined(__ARM_ARCH_8_1M_MAIN__)
92 bool is_psp = is_return_psp(lr);
93
94 return (uint32_t *)(is_return_secure_stack(lr)
95 ? (is_psp ? psp : msp)
96 : (is_psp ? __TZ_get_PSP_NS() : __TZ_get_MSP_NS()));
97 #else
98 return (uint32_t *)(is_return_psp(lr) ? psp : msp);
99 #endif
100 }
101
dump_exception_info(bool stack_error,struct exception_info_t * ctx)102 static void dump_exception_info(bool stack_error,
103 struct exception_info_t *ctx)
104 {
105 SPMLOG_DBGMSG("Here is some context for the exception:\r\n");
106 SPMLOG_DBGMSGVAL(" EXC_RETURN (LR): ", ctx->EXC_RETURN);
107 SPMLOG_DBGMSG(" Exception came from");
108 #ifdef TRUSTZONE_PRESENT
109 if (is_return_secure_stack(ctx->EXC_RETURN)) {
110 SPMLOG_DBGMSG(" secure FW in");
111 } else {
112 SPMLOG_DBGMSG(" non-secure FW in");
113 }
114 #endif
115
116 if (is_return_thread_mode(ctx->EXC_RETURN)) {
117 SPMLOG_DBGMSG(" thread mode.\r\n");
118 } else {
119 SPMLOG_DBGMSG(" handler mode.\r\n");
120 }
121 SPMLOG_DBGMSGVAL(" xPSR: ", ctx->xPSR);
122 SPMLOG_DBGMSGVAL(" MSP: ", ctx->MSP);
123 SPMLOG_DBGMSGVAL(" PSP: ", ctx->PSP);
124 #ifdef TRUSTZONE_PRESENT
125 SPMLOG_DBGMSGVAL(" MSP_NS: ", __TZ_get_MSP_NS());
126 SPMLOG_DBGMSGVAL(" PSP_NS: ", __TZ_get_PSP_NS());
127 #endif
128
129 SPMLOG_DBGMSGVAL(" Exception frame at: ", (uint32_t)ctx->EXC_FRAME);
130 if (stack_error) {
131 SPMLOG_DBGMSG(
132 " (Note that the exception frame may be corrupted for this type of error.)\r\n");
133 }
134 SPMLOG_DBGMSGVAL(" R0: ", ctx->EXC_FRAME_COPY[0]);
135 SPMLOG_DBGMSGVAL(" R1: ", ctx->EXC_FRAME_COPY[1]);
136 SPMLOG_DBGMSGVAL(" R2: ", ctx->EXC_FRAME_COPY[2]);
137 SPMLOG_DBGMSGVAL(" R3: ", ctx->EXC_FRAME_COPY[3]);
138 SPMLOG_DBGMSGVAL(" R12: ", ctx->EXC_FRAME_COPY[4]);
139 SPMLOG_DBGMSGVAL(" LR: ", ctx->EXC_FRAME_COPY[5]);
140 SPMLOG_DBGMSGVAL(" PC: ", ctx->EXC_FRAME_COPY[6]);
141 SPMLOG_DBGMSGVAL(" xPSR: ", ctx->EXC_FRAME_COPY[7]);
142
143 #ifdef FAULT_STATUS_PRESENT
144 SPMLOG_DBGMSGVAL(" CFSR: ", ctx->CFSR);
145 SPMLOG_DBGMSGVAL(" BFSR: ",
146 (ctx->CFSR & SCB_CFSR_BUSFAULTSR_Msk) >> SCB_CFSR_BUSFAULTSR_Pos);
147 if (ctx->BFARVALID) {
148 SPMLOG_DBGMSGVAL(" BFAR: ", ctx->BFAR);
149 } else {
150 SPMLOG_DBGMSG(" BFAR: Not Valid\r\n");
151 }
152 SPMLOG_DBGMSGVAL(" MMFSR: ",
153 (ctx->CFSR & SCB_CFSR_MEMFAULTSR_Msk) >> SCB_CFSR_MEMFAULTSR_Pos);
154 if (ctx->MMARVALID) {
155 SPMLOG_DBGMSGVAL(" MMFAR: ", ctx->MMFAR);
156 } else {
157 SPMLOG_DBGMSG(" MMFAR: Not Valid\r\n");
158 }
159 SPMLOG_DBGMSGVAL(" UFSR: ",
160 (ctx->CFSR & SCB_CFSR_USGFAULTSR_Msk) >> SCB_CFSR_USGFAULTSR_Pos);
161 SPMLOG_DBGMSGVAL(" HFSR: ", ctx->HFSR);
162 #ifdef TRUSTZONE_PRESENT
163 SPMLOG_DBGMSGVAL(" SFSR: ", ctx->SFSR);
164 if (ctx->SFARVALID) {
165 SPMLOG_DBGMSGVAL(" SFAR: ", ctx->SFAR);
166 } else {
167 SPMLOG_DBGMSG(" SFAR: Not Valid\r\n");
168 }
169 #endif
170
171 #endif
172 }
173
dump_error(uint32_t error_type)174 static void dump_error(uint32_t error_type)
175 {
176 bool stack_error = false;
177
178 SPMLOG_ERRMSG("FATAL ERROR: ");
179 switch (error_type) {
180 case EXCEPTION_TYPE_SECUREFAULT:
181 SPMLOG_ERRMSG("SecureFault\r\n");
182 break;
183 case EXCEPTION_TYPE_HARDFAULT:
184 SPMLOG_ERRMSG("HardFault\r\n");
185 break;
186 case EXCEPTION_TYPE_MEMFAULT:
187 SPMLOG_ERRMSG("MemManage fault\r\n");
188 stack_error = true;
189 break;
190 case EXCEPTION_TYPE_BUSFAULT:
191 SPMLOG_ERRMSG("BusFault\r\n");
192 stack_error = true;
193 break;
194 case EXCEPTION_TYPE_USAGEFAULT:
195 SPMLOG_ERRMSG("UsageFault\r\n");
196 stack_error = true;
197 break;
198 case EXCEPTION_TYPE_PLATFORM:
199 SPMLOG_ERRMSG("Platform Exception\r\n");
200 /* Depends on the platform, assume it may cause stack error */
201 stack_error = true;
202 break;
203 default:
204 SPMLOG_ERRMSG("Unknown\r\n");
205 break;
206 }
207
208 dump_exception_info(stack_error, &exception_info);
209 }
210
store_and_dump_context(uint32_t LR_in,uint32_t MSP_in,uint32_t PSP_in,uint32_t exception_type)211 void store_and_dump_context(uint32_t LR_in, uint32_t MSP_in, uint32_t PSP_in,
212 uint32_t exception_type)
213 {
214 struct exception_info_t *ctx = &exception_info;
215
216 ctx->xPSR = __get_xPSR();
217 ctx->EXC_RETURN = LR_in;
218 ctx->MSP = MSP_in;
219 ctx->PSP = PSP_in;
220 ctx->EXC_FRAME = get_exception_frame(ctx->EXC_RETURN, ctx->MSP, ctx->PSP);
221 memcpy(ctx->EXC_FRAME_COPY, ctx->EXC_FRAME, sizeof(ctx->EXC_FRAME_COPY));
222
223 #ifdef FAULT_STATUS_PRESENT
224 ctx->CFSR = SCB->CFSR;
225 ctx->HFSR = SCB->HFSR;
226 ctx->BFAR = SCB->BFAR;
227 ctx->BFARVALID = ctx->CFSR & SCB_CFSR_BFARVALID_Msk;
228 ctx->MMFAR = SCB->MMFAR;
229 ctx->MMARVALID = ctx->CFSR & SCB_CFSR_MMARVALID_Msk;
230 SCB->CFSR = ctx->CFSR; /* Clear bits. CFSR is write-one-to-clear. */
231 SCB->HFSR = ctx->HFSR; /* Clear bits. HFSR is write-one-to-clear. */
232 #ifdef TRUSTZONE_PRESENT
233 ctx->SFSR = SAU->SFSR;
234 ctx->SFAR = SAU->SFAR;
235 ctx->SFARVALID = ctx->SFSR & SAU_SFSR_SFARVALID_Msk;
236 SAU->SFSR = ctx->SFSR; /* Clear bits. SFSR is write-one-to-clear. */
237 #endif
238 #endif
239
240 dump_error(exception_type);
241 }
242