blob: 7ec94602f0d59d7b30d32d882963999e22615d27 [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"
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +01009#include "tfm_spm_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{
Joakim Andersson53831432021-11-03 15:44:28 +010088 SPMLOG_DBGMSG("Here is some context for the exception:\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +010089 SPMLOG_DBGMSGVAL(" EXC_RETURN (LR): ", ctx->EXC_RETURN);
90 SPMLOG_DBGMSG(" Exception came from");
91#ifdef TRUSTZONE_PRESENT
92 if (is_return_secure_stack(ctx->EXC_RETURN)) {
93 SPMLOG_DBGMSG(" secure FW in");
94 } else {
95 SPMLOG_DBGMSG(" non-secure FW in");
96 }
97#endif
98
99 if (is_return_thread_mode(ctx->EXC_RETURN)) {
Joakim Andersson53831432021-11-03 15:44:28 +0100100 SPMLOG_DBGMSG(" thread mode.\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100101 } else {
Joakim Andersson53831432021-11-03 15:44:28 +0100102 SPMLOG_DBGMSG(" handler mode.\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100103 }
104 SPMLOG_DBGMSGVAL(" xPSR: ", ctx->xPSR);
105 SPMLOG_DBGMSGVAL(" MSP: ", ctx->MSP);
106 SPMLOG_DBGMSGVAL(" PSP: ", ctx->PSP);
107#ifdef TRUSTZONE_PRESENT
108 SPMLOG_DBGMSGVAL(" MSP_NS: ", __TZ_get_MSP_NS());
109 SPMLOG_DBGMSGVAL(" PSP_NS: ", __TZ_get_PSP_NS());
110#endif
111
112 SPMLOG_DBGMSGVAL(" Exception frame at: ", (uint32_t)ctx->EXC_FRAME);
113 if (stack_error) {
114 SPMLOG_DBGMSG(
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 }
117 SPMLOG_DBGMSGVAL(" R0: ", ctx->EXC_FRAME_COPY[0]);
118 SPMLOG_DBGMSGVAL(" R1: ", ctx->EXC_FRAME_COPY[1]);
119 SPMLOG_DBGMSGVAL(" R2: ", ctx->EXC_FRAME_COPY[2]);
120 SPMLOG_DBGMSGVAL(" R3: ", ctx->EXC_FRAME_COPY[3]);
121 SPMLOG_DBGMSGVAL(" R12: ", ctx->EXC_FRAME_COPY[4]);
122 SPMLOG_DBGMSGVAL(" LR: ", ctx->EXC_FRAME_COPY[5]);
123 SPMLOG_DBGMSGVAL(" PC: ", ctx->EXC_FRAME_COPY[6]);
124 SPMLOG_DBGMSGVAL(" xPSR: ", ctx->EXC_FRAME_COPY[7]);
125
Bohdan Hunkoe8aa0d72024-10-31 16:35:27 +0200126 SPMLOG_DBGMSG(" Callee saved register state:\r\n");
Joakim Anderssondbdcfa02023-11-14 14:49:26 +0100127 SPMLOG_DBGMSGVAL(" R4: ", ctx->CALLEE_SAVED_COPY[0]);
128 SPMLOG_DBGMSGVAL(" R5: ", ctx->CALLEE_SAVED_COPY[1]);
129 SPMLOG_DBGMSGVAL(" R6: ", ctx->CALLEE_SAVED_COPY[2]);
130 SPMLOG_DBGMSGVAL(" R7: ", ctx->CALLEE_SAVED_COPY[3]);
131 SPMLOG_DBGMSGVAL(" R8: ", ctx->CALLEE_SAVED_COPY[4]);
132 SPMLOG_DBGMSGVAL(" R9: ", ctx->CALLEE_SAVED_COPY[5]);
133 SPMLOG_DBGMSGVAL(" R10: ", ctx->CALLEE_SAVED_COPY[6]);
134 SPMLOG_DBGMSGVAL(" R11: ", ctx->CALLEE_SAVED_COPY[7]);
135
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100136#ifdef FAULT_STATUS_PRESENT
137 SPMLOG_DBGMSGVAL(" CFSR: ", ctx->CFSR);
138 SPMLOG_DBGMSGVAL(" BFSR: ",
139 (ctx->CFSR & SCB_CFSR_BUSFAULTSR_Msk) >> SCB_CFSR_BUSFAULTSR_Pos);
140 if (ctx->BFARVALID) {
141 SPMLOG_DBGMSGVAL(" BFAR: ", ctx->BFAR);
142 } else {
Joakim Andersson53831432021-11-03 15:44:28 +0100143 SPMLOG_DBGMSG(" BFAR: Not Valid\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100144 }
145 SPMLOG_DBGMSGVAL(" MMFSR: ",
146 (ctx->CFSR & SCB_CFSR_MEMFAULTSR_Msk) >> SCB_CFSR_MEMFAULTSR_Pos);
147 if (ctx->MMARVALID) {
148 SPMLOG_DBGMSGVAL(" MMFAR: ", ctx->MMFAR);
149 } else {
Joakim Andersson53831432021-11-03 15:44:28 +0100150 SPMLOG_DBGMSG(" MMFAR: Not Valid\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100151 }
152 SPMLOG_DBGMSGVAL(" UFSR: ",
153 (ctx->CFSR & SCB_CFSR_USGFAULTSR_Msk) >> SCB_CFSR_USGFAULTSR_Pos);
154 SPMLOG_DBGMSGVAL(" HFSR: ", ctx->HFSR);
155#ifdef TRUSTZONE_PRESENT
156 SPMLOG_DBGMSGVAL(" SFSR: ", ctx->SFSR);
157 if (ctx->SFARVALID) {
158 SPMLOG_DBGMSGVAL(" SFAR: ", ctx->SFAR);
159 } else {
Joakim Andersson53831432021-11-03 15:44:28 +0100160 SPMLOG_DBGMSG(" SFAR: Not Valid\r\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
Joakim Andersson3d3c4452021-11-11 14:29:25 +0100171 SPMLOG_ERRMSG("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:
Joakim Andersson3d3c4452021-11-11 14:29:25 +0100174 SPMLOG_ERRMSG("HardFault\r\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:
Joakim Andersson3d3c4452021-11-11 14:29:25 +0100178 SPMLOG_ERRMSG("MemManage fault\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100179 stack_error = true;
180 break;
181 case EXCEPTION_TYPE_BUSFAULT:
Joakim Andersson3d3c4452021-11-11 14:29:25 +0100182 SPMLOG_ERRMSG("BusFault\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100183 stack_error = true;
184 break;
185 case EXCEPTION_TYPE_USAGEFAULT:
Joakim Andersson3d3c4452021-11-11 14:29:25 +0100186 SPMLOG_ERRMSG("UsageFault\r\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:
191 SPMLOG_ERRMSG("SecureFault\r\n");
192 break;
193#endif
194#endif
195 /* Platform specific external interrupt secure handler. */
196 default:
197 if (ctx->VECTACTIVE < 16) {
198 SPMLOG_ERRMSGVAL("Reserved Exception ", ctx->VECTACTIVE);
199 } else {
200 SPMLOG_ERRMSGVAL("Platform external interrupt (IRQn): ", ctx->VECTACTIVE - 16);
201 }
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}