blob: 7c37575d0db9fe117bc81f9cfd1c32906de0d313 [file] [log] [blame]
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +01001/*
2 * Copyright (c) 2021, Nordic Semiconductor ASA. All rights reserved.
Dávid Házibe7f0de2023-07-19 11:25:38 +02003 * Copyright (c) 2023, Arm Limited. All rights reserved.
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +01004 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8#ifndef __EXCEPTION_INFO_H__
9#define __EXCEPTION_INFO_H__
10
11#include <stdint.h>
12
13#if defined(__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
14#define TRUSTZONE_PRESENT
15#endif
16
17#if defined(__ARM_ARCH_8_1M_MAIN__) || defined(__ARM_ARCH_8M_MAIN__) \
18 || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7EM__)
19#define FAULT_STATUS_PRESENT
20#endif
21
22/* Arguments to EXCEPTION_INFO() */
23#define EXCEPTION_TYPE_SECUREFAULT 0
24#define EXCEPTION_TYPE_HARDFAULT 1
25#define EXCEPTION_TYPE_MEMFAULT 2
26#define EXCEPTION_TYPE_BUSFAULT 3
27#define EXCEPTION_TYPE_USAGEFAULT 4
Joakim Anderssona4066d82022-04-06 16:32:09 +020028#define EXCEPTION_TYPE_PLATFORM 5
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +010029
30/* This level of indirection is needed to fully resolve exception info when it's
31 * a macro
32 */
Dávid Házibe7f0de2023-07-19 11:25:38 +020033#define __STRINGIFY(exception_info) #exception_info
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +010034
35/* Store context for an exception, and print an error message with the context.
36 *
37 * @param[in] exception_type One of the EXCEPTION_TYPE_* values defined above. Any
38 * other value will result in printing "Unknown".
39 */
40#ifdef TFM_EXCEPTION_INFO_DUMP
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +010041
Chris Coleman9dd58c92023-10-08 13:49:23 -040042struct exception_info_t {
43 uint32_t EXC_RETURN; /* EXC_RETURN value in LR. */
44 uint32_t MSP; /* (Secure) MSP. */
45 uint32_t PSP; /* (Secure) PSP. */
46 uint32_t *EXC_FRAME; /* Exception frame on stack. */
47 uint32_t EXC_FRAME_COPY[8]; /* Copy of the basic exception frame. */
48 uint32_t xPSR; /* Program Status Registers. */
49
50#ifdef FAULT_STATUS_PRESENT
51 uint32_t CFSR; /* Configurable Fault Status Register. */
52 uint32_t HFSR; /* Hard Fault Status Register. */
53 uint32_t BFAR; /* Bus Fault address register. */
54 uint32_t BFARVALID; /* Whether BFAR contains a valid address. */
55 uint32_t MMFAR; /* MemManage Fault address register. */
56 uint32_t MMARVALID; /* Whether MMFAR contains a valid address. */
57#ifdef TRUSTZONE_PRESENT
58 uint32_t SFSR; /* SecureFault Status Register. */
59 uint32_t SFAR; /* SecureFault Address Register. */
60 uint32_t SFARVALID; /* Whether SFAR contains a valid address. */
61#endif
62#endif
63};
64
65/**
66 * \brief Get a pointer to the current exception_info_t context
67 *
68 * \return A pointer to the exception_info_t context or NULL if no context
69 * has been stored
70 */
71void tfm_exception_info_get_context(struct exception_info_t *ctx);
72
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +010073/* Store context for an exception, then print the info.
74 * Call EXCEPTION_INFO() instead of calling this directly.
75 */
76void store_and_dump_context(uint32_t LR_in, uint32_t MSP_in, uint32_t PSP_in,
77 uint32_t exception_type);
Dávid Házibe7f0de2023-07-19 11:25:38 +020078
79/* IAR Specific */
80#if defined(__ICCARM__)
81#pragma required = store_and_dump_context
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +010082#endif
83
Dávid Házibe7f0de2023-07-19 11:25:38 +020084#define EXCEPTION_INFO(exception_type) \
85 __ASM volatile( \
86 "MOV r0, lr\n" \
87 "MRS r1, MSP\n" \
88 "MRS r2, PSP\n" \
89 "MOVS r3, #" __STRINGIFY(exception_type) "\n"\
90 "BL store_and_dump_context\n" \
91 )
92
93#else /* TFM_EXCEPTION_INFO_DUMP */
94#define EXCEPTION_INFO(exception_type)
95#endif /* TFM_EXCEPTION_INFO_DUMP */
96
Øyvind Rønningstadf2c8dad2021-01-15 15:33:33 +010097#endif /* __EXCEPTION_INFO_H__ */