blob: 71f7ab7264b80362b5b02707cc04ffbec96f6705 [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__)
48 /* PSP is used only if SPSEL is set, and we came from thread mode. */
49 return ((lr & EXC_RETURN_SPSEL) && is_return_thread_mode(lr));
50#else
51 return (lr == EXC_RETURN_THREAD_PSP);
52#endif
53}
54
55/**
56 * \brief Get a pointer to the current exception frame
57 *
58 * \param[in] lr LR register containing the EXC_RETURN value.
59 * \param[in] msp The MSP at the start of the exception handler.
60 * \param[in] psp The PSP at the start of the exception handler.
61 *
62 * \return A pointer to the current exception frame.
63 */
64__STATIC_INLINE
65uint32_t *get_exception_frame(uint32_t lr, uint32_t msp, uint32_t psp)
66{
67#if defined(__ARM_ARCH_8M_BASE__) || defined(__ARM_ARCH_8M_MAIN__) \
68 || defined(__ARM_ARCH_8_1M_MAIN__)
69 bool is_psp = is_return_psp(lr);
70
71 return (uint32_t *)(is_return_secure_stack(lr)
72 ? (is_psp ? psp : msp)
73 : (is_psp ? __TZ_get_PSP_NS() : __TZ_get_MSP_NS()));
74#else
75 return (uint32_t *)(is_return_psp(lr) ? psp : msp);
76#endif
77}
78
Joakim Anderssona4066d82022-04-06 16:32:09 +020079static void dump_exception_info(bool stack_error,
80 struct exception_info_t *ctx)
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +010081{
Joakim Andersson53831432021-11-03 15:44:28 +010082 SPMLOG_DBGMSG("Here is some context for the exception:\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +010083 SPMLOG_DBGMSGVAL(" EXC_RETURN (LR): ", ctx->EXC_RETURN);
84 SPMLOG_DBGMSG(" Exception came from");
85#ifdef TRUSTZONE_PRESENT
86 if (is_return_secure_stack(ctx->EXC_RETURN)) {
87 SPMLOG_DBGMSG(" secure FW in");
88 } else {
89 SPMLOG_DBGMSG(" non-secure FW in");
90 }
91#endif
92
93 if (is_return_thread_mode(ctx->EXC_RETURN)) {
Joakim Andersson53831432021-11-03 15:44:28 +010094 SPMLOG_DBGMSG(" thread mode.\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +010095 } else {
Joakim Andersson53831432021-11-03 15:44:28 +010096 SPMLOG_DBGMSG(" handler mode.\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +010097 }
98 SPMLOG_DBGMSGVAL(" xPSR: ", ctx->xPSR);
99 SPMLOG_DBGMSGVAL(" MSP: ", ctx->MSP);
100 SPMLOG_DBGMSGVAL(" PSP: ", ctx->PSP);
101#ifdef TRUSTZONE_PRESENT
102 SPMLOG_DBGMSGVAL(" MSP_NS: ", __TZ_get_MSP_NS());
103 SPMLOG_DBGMSGVAL(" PSP_NS: ", __TZ_get_PSP_NS());
104#endif
105
106 SPMLOG_DBGMSGVAL(" Exception frame at: ", (uint32_t)ctx->EXC_FRAME);
107 if (stack_error) {
108 SPMLOG_DBGMSG(
Joakim Andersson53831432021-11-03 15:44:28 +0100109 " (Note that the exception frame may be corrupted for this type of error.)\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100110 }
111 SPMLOG_DBGMSGVAL(" R0: ", ctx->EXC_FRAME_COPY[0]);
112 SPMLOG_DBGMSGVAL(" R1: ", ctx->EXC_FRAME_COPY[1]);
113 SPMLOG_DBGMSGVAL(" R2: ", ctx->EXC_FRAME_COPY[2]);
114 SPMLOG_DBGMSGVAL(" R3: ", ctx->EXC_FRAME_COPY[3]);
115 SPMLOG_DBGMSGVAL(" R12: ", ctx->EXC_FRAME_COPY[4]);
116 SPMLOG_DBGMSGVAL(" LR: ", ctx->EXC_FRAME_COPY[5]);
117 SPMLOG_DBGMSGVAL(" PC: ", ctx->EXC_FRAME_COPY[6]);
118 SPMLOG_DBGMSGVAL(" xPSR: ", ctx->EXC_FRAME_COPY[7]);
119
120#ifdef FAULT_STATUS_PRESENT
121 SPMLOG_DBGMSGVAL(" CFSR: ", ctx->CFSR);
122 SPMLOG_DBGMSGVAL(" BFSR: ",
123 (ctx->CFSR & SCB_CFSR_BUSFAULTSR_Msk) >> SCB_CFSR_BUSFAULTSR_Pos);
124 if (ctx->BFARVALID) {
125 SPMLOG_DBGMSGVAL(" BFAR: ", ctx->BFAR);
126 } else {
Joakim Andersson53831432021-11-03 15:44:28 +0100127 SPMLOG_DBGMSG(" BFAR: Not Valid\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100128 }
129 SPMLOG_DBGMSGVAL(" MMFSR: ",
130 (ctx->CFSR & SCB_CFSR_MEMFAULTSR_Msk) >> SCB_CFSR_MEMFAULTSR_Pos);
131 if (ctx->MMARVALID) {
132 SPMLOG_DBGMSGVAL(" MMFAR: ", ctx->MMFAR);
133 } else {
Joakim Andersson53831432021-11-03 15:44:28 +0100134 SPMLOG_DBGMSG(" MMFAR: Not Valid\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100135 }
136 SPMLOG_DBGMSGVAL(" UFSR: ",
137 (ctx->CFSR & SCB_CFSR_USGFAULTSR_Msk) >> SCB_CFSR_USGFAULTSR_Pos);
138 SPMLOG_DBGMSGVAL(" HFSR: ", ctx->HFSR);
139#ifdef TRUSTZONE_PRESENT
140 SPMLOG_DBGMSGVAL(" SFSR: ", ctx->SFSR);
141 if (ctx->SFARVALID) {
142 SPMLOG_DBGMSGVAL(" SFAR: ", ctx->SFAR);
143 } else {
Joakim Andersson53831432021-11-03 15:44:28 +0100144 SPMLOG_DBGMSG(" SFAR: Not Valid\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100145 }
146#endif
147
148#endif
149}
150
151static void dump_error(uint32_t error_type)
152{
153 bool stack_error = false;
154
Joakim Andersson3d3c4452021-11-11 14:29:25 +0100155 SPMLOG_ERRMSG("FATAL ERROR: ");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100156 switch (error_type) {
157 case EXCEPTION_TYPE_SECUREFAULT:
Joakim Andersson3d3c4452021-11-11 14:29:25 +0100158 SPMLOG_ERRMSG("SecureFault\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100159 break;
160 case EXCEPTION_TYPE_HARDFAULT:
Joakim Andersson3d3c4452021-11-11 14:29:25 +0100161 SPMLOG_ERRMSG("HardFault\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100162 break;
163 case EXCEPTION_TYPE_MEMFAULT:
Joakim Andersson3d3c4452021-11-11 14:29:25 +0100164 SPMLOG_ERRMSG("MemManage fault\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100165 stack_error = true;
166 break;
167 case EXCEPTION_TYPE_BUSFAULT:
Joakim Andersson3d3c4452021-11-11 14:29:25 +0100168 SPMLOG_ERRMSG("BusFault\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100169 stack_error = true;
170 break;
171 case EXCEPTION_TYPE_USAGEFAULT:
Joakim Andersson3d3c4452021-11-11 14:29:25 +0100172 SPMLOG_ERRMSG("UsageFault\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100173 stack_error = true;
174 break;
Joakim Anderssona4066d82022-04-06 16:32:09 +0200175 case EXCEPTION_TYPE_PLATFORM:
176 SPMLOG_ERRMSG("Platform Exception\r\n");
177 /* Depends on the platform, assume it may cause stack error */
178 stack_error = true;
179 break;
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100180 default:
Joakim Andersson3d3c4452021-11-11 14:29:25 +0100181 SPMLOG_ERRMSG("Unknown\r\n");
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100182 break;
183 }
Joakim Anderssona4066d82022-04-06 16:32:09 +0200184
185 dump_exception_info(stack_error, &exception_info);
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100186}
187
Chris Coleman9dd58c92023-10-08 13:49:23 -0400188void tfm_exception_info_get_context(struct exception_info_t *ctx)
189{
190 memcpy(ctx, &exception_info, sizeof(exception_info));
191}
192
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100193void store_and_dump_context(uint32_t LR_in, uint32_t MSP_in, uint32_t PSP_in,
194 uint32_t exception_type)
195{
196 struct exception_info_t *ctx = &exception_info;
197
198 ctx->xPSR = __get_xPSR();
199 ctx->EXC_RETURN = LR_in;
200 ctx->MSP = MSP_in;
201 ctx->PSP = PSP_in;
202 ctx->EXC_FRAME = get_exception_frame(ctx->EXC_RETURN, ctx->MSP, ctx->PSP);
Ken Liub671d682022-05-12 20:39:29 +0800203 memcpy(ctx->EXC_FRAME_COPY, ctx->EXC_FRAME, sizeof(ctx->EXC_FRAME_COPY));
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +0100204
205#ifdef FAULT_STATUS_PRESENT
206 ctx->CFSR = SCB->CFSR;
207 ctx->HFSR = SCB->HFSR;
208 ctx->BFAR = SCB->BFAR;
209 ctx->BFARVALID = ctx->CFSR & SCB_CFSR_BFARVALID_Msk;
210 ctx->MMFAR = SCB->MMFAR;
211 ctx->MMARVALID = ctx->CFSR & SCB_CFSR_MMARVALID_Msk;
212 SCB->CFSR = ctx->CFSR; /* Clear bits. CFSR is write-one-to-clear. */
213 SCB->HFSR = ctx->HFSR; /* Clear bits. HFSR is write-one-to-clear. */
214#ifdef TRUSTZONE_PRESENT
215 ctx->SFSR = SAU->SFSR;
216 ctx->SFAR = SAU->SFAR;
217 ctx->SFARVALID = ctx->SFSR & SAU_SFSR_SFARVALID_Msk;
218 SAU->SFSR = ctx->SFSR; /* Clear bits. SFSR is write-one-to-clear. */
219#endif
220#endif
221
222 dump_error(exception_type);
223}