blob: d383fa4ee3884cb0f48e242fd6dfb74d46e9cb75 [file] [log] [blame]
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +01001/*
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"
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +010011
12struct 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
36static 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 */
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 */
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
88uint32_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
Joakim Anderssona4066d82022-04-06 16:32:09 +0200102static void dump_exception_info(bool stack_error,
103 struct exception_info_t *ctx)
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100104{
Joakim Andersson53831432021-11-03 15:44:28 +0100105 SPMLOG_DBGMSG("Here is some context for the exception:\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100106 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)) {
Joakim Andersson53831432021-11-03 15:44:28 +0100117 SPMLOG_DBGMSG(" thread mode.\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100118 } else {
Joakim Andersson53831432021-11-03 15:44:28 +0100119 SPMLOG_DBGMSG(" handler mode.\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100120 }
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(
Joakim Andersson53831432021-11-03 15:44:28 +0100132 " (Note that the exception frame may be corrupted for this type of error.)\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100133 }
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 {
Joakim Andersson53831432021-11-03 15:44:28 +0100150 SPMLOG_DBGMSG(" BFAR: Not Valid\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100151 }
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 {
Joakim Andersson53831432021-11-03 15:44:28 +0100157 SPMLOG_DBGMSG(" MMFAR: Not Valid\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100158 }
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 {
Joakim Andersson53831432021-11-03 15:44:28 +0100167 SPMLOG_DBGMSG(" SFAR: Not Valid\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100168 }
169#endif
170
171#endif
172}
173
174static void dump_error(uint32_t error_type)
175{
176 bool stack_error = false;
177
Joakim Andersson3d3c4452021-11-11 14:29:25 +0100178 SPMLOG_ERRMSG("FATAL ERROR: ");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100179 switch (error_type) {
180 case EXCEPTION_TYPE_SECUREFAULT:
Joakim Andersson3d3c4452021-11-11 14:29:25 +0100181 SPMLOG_ERRMSG("SecureFault\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100182 break;
183 case EXCEPTION_TYPE_HARDFAULT:
Joakim Andersson3d3c4452021-11-11 14:29:25 +0100184 SPMLOG_ERRMSG("HardFault\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100185 break;
186 case EXCEPTION_TYPE_MEMFAULT:
Joakim Andersson3d3c4452021-11-11 14:29:25 +0100187 SPMLOG_ERRMSG("MemManage fault\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100188 stack_error = true;
189 break;
190 case EXCEPTION_TYPE_BUSFAULT:
Joakim Andersson3d3c4452021-11-11 14:29:25 +0100191 SPMLOG_ERRMSG("BusFault\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100192 stack_error = true;
193 break;
194 case EXCEPTION_TYPE_USAGEFAULT:
Joakim Andersson3d3c4452021-11-11 14:29:25 +0100195 SPMLOG_ERRMSG("UsageFault\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100196 stack_error = true;
197 break;
Joakim Anderssona4066d82022-04-06 16:32:09 +0200198 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;
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100203 default:
Joakim Andersson3d3c4452021-11-11 14:29:25 +0100204 SPMLOG_ERRMSG("Unknown\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100205 break;
206 }
Joakim Anderssona4066d82022-04-06 16:32:09 +0200207
208 dump_exception_info(stack_error, &exception_info);
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100209}
210
211void 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 spm_memcpy(ctx->EXC_FRAME_COPY, ctx->EXC_FRAME,
222 sizeof(ctx->EXC_FRAME_COPY));
223
224#ifdef FAULT_STATUS_PRESENT
225 ctx->CFSR = SCB->CFSR;
226 ctx->HFSR = SCB->HFSR;
227 ctx->BFAR = SCB->BFAR;
228 ctx->BFARVALID = ctx->CFSR & SCB_CFSR_BFARVALID_Msk;
229 ctx->MMFAR = SCB->MMFAR;
230 ctx->MMARVALID = ctx->CFSR & SCB_CFSR_MMARVALID_Msk;
231 SCB->CFSR = ctx->CFSR; /* Clear bits. CFSR is write-one-to-clear. */
232 SCB->HFSR = ctx->HFSR; /* Clear bits. HFSR is write-one-to-clear. */
233#ifdef TRUSTZONE_PRESENT
234 ctx->SFSR = SAU->SFSR;
235 ctx->SFAR = SAU->SFAR;
236 ctx->SFARVALID = ctx->SFSR & SAU_SFSR_SFARVALID_Msk;
237 SAU->SFSR = ctx->SFSR; /* Clear bits. SFSR is write-one-to-clear. */
238#endif
239#endif
240
241 dump_error(exception_type);
242}