1 /*
2  * Copyright (c) 2021, Nordic Semiconductor ASA. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef __EXCEPTION_INFO_H__
8 #define __EXCEPTION_INFO_H__
9 
10 #include <stdint.h>
11 
12 #if defined(__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
13 #define TRUSTZONE_PRESENT
14 #endif
15 
16 #if defined(__ARM_ARCH_8_1M_MAIN__) || defined(__ARM_ARCH_8M_MAIN__) \
17     || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__)
18 #define FAULT_STATUS_PRESENT
19 #endif
20 
21 /* Arguments to EXCEPTION_INFO() */
22 #define EXCEPTION_TYPE_SECUREFAULT 0
23 #define EXCEPTION_TYPE_HARDFAULT   1
24 #define EXCEPTION_TYPE_MEMFAULT    2
25 #define EXCEPTION_TYPE_BUSFAULT    3
26 #define EXCEPTION_TYPE_USAGEFAULT  4
27 #define EXCEPTION_TYPE_PLATFORM    5
28 
29 /* This level of indirection is needed to fully resolve exception info when it's
30  * a macro
31  */
32 #define _STRINGIFY(exception_info) #exception_info
33 
34 /* Store context for an exception, and print an error message with the context.
35  *
36  * @param[in]  exception_type  One of the EXCEPTION_TYPE_* values defined above. Any
37  *                             other value will result in printing "Unknown".
38  */
39 #ifdef TFM_EXCEPTION_INFO_DUMP
40 #define EXCEPTION_INFO(exception_type)                  \
41     __ASM volatile(                                     \
42         "MOV     r0, lr\n"                              \
43         "MRS     r1, MSP\n"                             \
44         "MRS     r2, PSP\n"                             \
45         "MOVS    r3, #" _STRINGIFY(exception_type) "\n" \
46         "BL      store_and_dump_context\n"              \
47     )
48 
49 /* Store context for an exception, then print the info.
50  * Call EXCEPTION_INFO() instead of calling this directly.
51  */
52 void store_and_dump_context(uint32_t LR_in, uint32_t MSP_in, uint32_t PSP_in,
53                             uint32_t exception_type);
54 #else
55 #define EXCEPTION_INFO(exception_type)
56 #endif
57 
58 #endif /* __EXCEPTION_INFO_H__ */
59