blob: e7e7a13affd1d1bccc03dbb9027300c696a169f5 [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"
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +00009#include "tfm_log.h"
Dávid Házibe7f0de2023-07-19 11:25:38 +020010/* "exception_info.h" must be the last include because of the IAR pragma */
11#include "exception_info.h"
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +010012
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +010013static struct exception_info_t exception_info;
14
15/**
16 * \brief Check whether the exception was triggered in thread or handler mode.
17 *
18 * \param[in] lr LR register containing the EXC_RETURN value.
19 *
20 * \retval true The exception will return to thread mode.
21 */
22__STATIC_INLINE bool is_return_thread_mode(uint32_t lr)
23{
24#if defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__)
25 return !((lr == EXC_RETURN_HANDLER) || (lr == EXC_RETURN_HANDLER_FPU));
26#elif defined(__ARM_ARCH_8M_BASE__) || defined(__ARM_ARCH_8M_MAIN__) \
27 || defined(__ARM_ARCH_8_1M_MAIN__)
28 return (lr & EXC_RETURN_MODE);
29#else
30 return !(lr == EXC_RETURN_HANDLER);
31#endif
32}
33
34/**
35 * \brief Check whether the PSP or MSP is used to restore stack frame on
36 * exception return.
37 *
38 * \param[in] lr LR register containing the EXC_RETURN value.
39 *
40 * \retval true The exception frame is on the PSP
41 */
42__STATIC_INLINE bool is_return_psp(uint32_t lr)
43{
44#if defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__)
45 return ((lr == EXC_RETURN_THREAD_PSP) || (lr == EXC_RETURN_THREAD_PSP_FPU));
46#elif defined(__ARM_ARCH_8M_BASE__) || defined(__ARM_ARCH_8M_MAIN__) \
47 || defined(__ARM_ARCH_8_1M_MAIN__)
Roman Mazuraka780d102024-02-05 21:38:34 +020048 if (is_return_secure_stack(lr)) {
49 /* PSP is used only if SPSEL is set, and we came from thread mode. */
50 return ((lr & EXC_RETURN_SPSEL) && is_return_thread_mode(lr));
51 } else {
52 /* PSP is used only if CONTROL_NS.SPSEL is set, and we came from thread mode. */
53 bool sp_sel = _FLD2VAL(CONTROL_SPSEL, __TZ_get_CONTROL_NS()) != 0;
54 return (sp_sel && is_return_thread_mode(lr));
55 }
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +010056#else
57 return (lr == EXC_RETURN_THREAD_PSP);
58#endif
59}
60
61/**
62 * \brief Get a pointer to the current exception frame
63 *
64 * \param[in] lr LR register containing the EXC_RETURN value.
65 * \param[in] msp The MSP at the start of the exception handler.
66 * \param[in] psp The PSP at the start of the exception handler.
67 *
68 * \return A pointer to the current exception frame.
69 */
70__STATIC_INLINE
71uint32_t *get_exception_frame(uint32_t lr, uint32_t msp, uint32_t psp)
72{
73#if defined(__ARM_ARCH_8M_BASE__) || defined(__ARM_ARCH_8M_MAIN__) \
74 || defined(__ARM_ARCH_8_1M_MAIN__)
75 bool is_psp = is_return_psp(lr);
76
77 return (uint32_t *)(is_return_secure_stack(lr)
78 ? (is_psp ? psp : msp)
79 : (is_psp ? __TZ_get_PSP_NS() : __TZ_get_MSP_NS()));
80#else
81 return (uint32_t *)(is_return_psp(lr) ? psp : msp);
82#endif
83}
84
Joakim Anderssona4066d82022-04-06 16:32:09 +020085static void dump_exception_info(bool stack_error,
Joakim Andersson90e0c062023-11-13 13:48:08 +010086 const struct exception_info_t *ctx)
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +010087{
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +000088 VERBOSE_RAW("Here is some context for the exception:\n");
89 VERBOSE_RAW(" EXC_RETURN (LR): 0x%08x\n", ctx->EXC_RETURN);
90 VERBOSE_RAW(" Exception came from");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +010091#ifdef TRUSTZONE_PRESENT
92 if (is_return_secure_stack(ctx->EXC_RETURN)) {
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +000093 VERBOSE_RAW(" secure FW in");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +010094 } else {
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +000095 VERBOSE_RAW(" non-secure FW in");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +010096 }
97#endif
98
99 if (is_return_thread_mode(ctx->EXC_RETURN)) {
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000100 VERBOSE_RAW(" thread mode.\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100101 } else {
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000102 VERBOSE_RAW(" handler mode.\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100103 }
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000104 VERBOSE_RAW(" xPSR: 0x%08x\n", ctx->xPSR);
105 VERBOSE_RAW(" MSP: 0x%08x\n", ctx->MSP);
106 VERBOSE_RAW(" PSP: 0x%08x\n", ctx->PSP);
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100107#ifdef TRUSTZONE_PRESENT
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000108 VERBOSE_RAW(" MSP_NS: 0x%08x\n", __TZ_get_MSP_NS());
109 VERBOSE_RAW(" PSP_NS: 0x%08x\n", __TZ_get_PSP_NS());
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100110#endif
111
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000112 VERBOSE_RAW(" Exception frame at: 0x%08x\n", (uint32_t)ctx->EXC_FRAME);
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100113 if (stack_error) {
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000114 VERBOSE_RAW(
Joakim Andersson53831432021-11-03 15:44:28 +0100115 " (Note that the exception frame may be corrupted for this type of error.)\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100116 }
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000117 VERBOSE_RAW(" R0: 0x%08x\n", ctx->EXC_FRAME_COPY[0]);
118 VERBOSE_RAW(" R1: 0x%08x\n", ctx->EXC_FRAME_COPY[1]);
119 VERBOSE_RAW(" R2: 0x%08x\n", ctx->EXC_FRAME_COPY[2]);
120 VERBOSE_RAW(" R3: 0x%08x\n", ctx->EXC_FRAME_COPY[3]);
121 VERBOSE_RAW(" R12: 0x%08x\n", ctx->EXC_FRAME_COPY[4]);
122 VERBOSE_RAW(" LR: 0x%08x\n", ctx->EXC_FRAME_COPY[5]);
123 VERBOSE_RAW(" PC: 0x%08x\n", ctx->EXC_FRAME_COPY[6]);
124 VERBOSE_RAW(" xPSR: 0x%08x\n", ctx->EXC_FRAME_COPY[7]);
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100125
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000126 VERBOSE_RAW(" Callee saved register state:\n");
127 VERBOSE_RAW(" R4: 0x%08x\n", ctx->CALLEE_SAVED_COPY[0]);
128 VERBOSE_RAW(" R5: 0x%08x\n", ctx->CALLEE_SAVED_COPY[1]);
129 VERBOSE_RAW(" R6: 0x%08x\n", ctx->CALLEE_SAVED_COPY[2]);
130 VERBOSE_RAW(" R7: 0x%08x\n", ctx->CALLEE_SAVED_COPY[3]);
131 VERBOSE_RAW(" R8: 0x%08x\n", ctx->CALLEE_SAVED_COPY[4]);
132 VERBOSE_RAW(" R9: 0x%08x\n", ctx->CALLEE_SAVED_COPY[5]);
133 VERBOSE_RAW(" R10: 0x%08x\n", ctx->CALLEE_SAVED_COPY[6]);
134 VERBOSE_RAW(" R11: 0x%08x\n", ctx->CALLEE_SAVED_COPY[7]);
Joakim Anderssondbdcfa02023-11-14 14:49:26 +0100135
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100136#ifdef FAULT_STATUS_PRESENT
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000137 VERBOSE_RAW(" CFSR: 0x%08x\n", ctx->CFSR);
138 VERBOSE_RAW(" BFSR: ",
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100139 (ctx->CFSR & SCB_CFSR_BUSFAULTSR_Msk) >> SCB_CFSR_BUSFAULTSR_Pos);
140 if (ctx->BFARVALID) {
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000141 VERBOSE_RAW(" BFAR: 0x%08x\n", ctx->BFAR);
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100142 } else {
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000143 VERBOSE_RAW(" BFAR: Not Valid\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100144 }
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000145 VERBOSE_RAW(" MMFSR: ",
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100146 (ctx->CFSR & SCB_CFSR_MEMFAULTSR_Msk) >> SCB_CFSR_MEMFAULTSR_Pos);
147 if (ctx->MMARVALID) {
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000148 VERBOSE_RAW(" MMFAR: 0x%08x\n", ctx->MMFAR);
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100149 } else {
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000150 VERBOSE_RAW(" MMFAR: Not Valid\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100151 }
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000152 VERBOSE_RAW(" UFSR: 0x%08x\n",
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100153 (ctx->CFSR & SCB_CFSR_USGFAULTSR_Msk) >> SCB_CFSR_USGFAULTSR_Pos);
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000154 VERBOSE_RAW(" HFSR: 0x%08x\n", ctx->HFSR);
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100155#ifdef TRUSTZONE_PRESENT
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000156 VERBOSE_RAW(" SFSR: 0x%08x\n", ctx->SFSR);
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100157 if (ctx->SFARVALID) {
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000158 VERBOSE_RAW(" SFAR: 0x%08x\n", ctx->SFAR);
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100159 } else {
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000160 VERBOSE_RAW(" SFAR: Not Valid\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100161 }
162#endif
163
164#endif
165}
166
Joakim Andersson90e0c062023-11-13 13:48:08 +0100167static void dump_error(const struct exception_info_t *ctx)
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100168{
169 bool stack_error = false;
170
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000171 ERROR_RAW("FATAL ERROR: ");
Joakim Andersson90e0c062023-11-13 13:48:08 +0100172 switch (ctx->VECTACTIVE) {
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100173 case EXCEPTION_TYPE_HARDFAULT:
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000174 ERROR_RAW("HardFault\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100175 break;
Joakim Andersson90e0c062023-11-13 13:48:08 +0100176#ifdef FAULT_STATUS_PRESENT
177 case EXCEPTION_TYPE_MEMMANAGEFAULT:
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000178 ERROR_RAW("MemManage fault\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100179 stack_error = true;
180 break;
181 case EXCEPTION_TYPE_BUSFAULT:
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000182 ERROR_RAW("BusFault\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100183 stack_error = true;
184 break;
185 case EXCEPTION_TYPE_USAGEFAULT:
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000186 ERROR_RAW("UsageFault\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100187 stack_error = true;
188 break;
Joakim Andersson90e0c062023-11-13 13:48:08 +0100189#ifdef TRUSTZONE_PRESENT
190 case EXCEPTION_TYPE_SECUREFAULT:
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000191 ERROR_RAW("SecureFault\n");
Joakim Andersson90e0c062023-11-13 13:48:08 +0100192 break;
193#endif
194#endif
195 /* Platform specific external interrupt secure handler. */
196 default:
197 if (ctx->VECTACTIVE < 16) {
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000198 ERROR_RAW("Reserved Exception 0x%08x\n", ctx->VECTACTIVE);
Joakim Andersson90e0c062023-11-13 13:48:08 +0100199 } else {
Jackson Cooper-Driveraf546022025-03-13 15:14:27 +0000200 ERROR_RAW("Platform external interrupt (IRQn): 0x%08x\n", ctx->VECTACTIVE - 16);
Joakim Andersson90e0c062023-11-13 13:48:08 +0100201 }
Joakim Anderssona4066d82022-04-06 16:32:09 +0200202 /* Depends on the platform, assume it may cause stack error */
203 stack_error = true;
204 break;
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100205 }
Joakim Anderssona4066d82022-04-06 16:32:09 +0200206
Joakim Andersson90e0c062023-11-13 13:48:08 +0100207 dump_exception_info(stack_error, ctx);
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100208}
209
Chris Coleman9dd58c92023-10-08 13:49:23 -0400210void tfm_exception_info_get_context(struct exception_info_t *ctx)
211{
212 memcpy(ctx, &exception_info, sizeof(exception_info));
213}
214
Joakim Anderssondbdcfa02023-11-14 14:49:26 +0100215void store_and_dump_context(uint32_t MSP_in, uint32_t PSP_in, uint32_t LR_in,
216 uint32_t *callee_saved)
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100217{
218 struct exception_info_t *ctx = &exception_info;
219
Joakim Andersson90e0c062023-11-13 13:48:08 +0100220 ctx->VECTACTIVE = SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk;
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100221 ctx->xPSR = __get_xPSR();
222 ctx->EXC_RETURN = LR_in;
223 ctx->MSP = MSP_in;
224 ctx->PSP = PSP_in;
225 ctx->EXC_FRAME = get_exception_frame(ctx->EXC_RETURN, ctx->MSP, ctx->PSP);
Ken Liub671d682022-05-12 20:39:29 +0800226 memcpy(ctx->EXC_FRAME_COPY, ctx->EXC_FRAME, sizeof(ctx->EXC_FRAME_COPY));
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100227
Joakim Anderssondbdcfa02023-11-14 14:49:26 +0100228 if (callee_saved) {
229 memcpy(ctx->CALLEE_SAVED_COPY, callee_saved, sizeof(ctx->CALLEE_SAVED_COPY));
230 }
231
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100232#ifdef FAULT_STATUS_PRESENT
233 ctx->CFSR = SCB->CFSR;
234 ctx->HFSR = SCB->HFSR;
235 ctx->BFAR = SCB->BFAR;
236 ctx->BFARVALID = ctx->CFSR & SCB_CFSR_BFARVALID_Msk;
237 ctx->MMFAR = SCB->MMFAR;
238 ctx->MMARVALID = ctx->CFSR & SCB_CFSR_MMARVALID_Msk;
239 SCB->CFSR = ctx->CFSR; /* Clear bits. CFSR is write-one-to-clear. */
240 SCB->HFSR = ctx->HFSR; /* Clear bits. HFSR is write-one-to-clear. */
241#ifdef TRUSTZONE_PRESENT
242 ctx->SFSR = SAU->SFSR;
243 ctx->SFAR = SAU->SFAR;
244 ctx->SFARVALID = ctx->SFSR & SAU_SFSR_SFARVALID_Msk;
245 SAU->SFSR = ctx->SFSR; /* Clear bits. SFSR is write-one-to-clear. */
246#endif
247#endif
248
Joakim Andersson90e0c062023-11-13 13:48:08 +0100249 dump_error(ctx);
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100250}