Øyvind Rønningstad | f2c8dad | 2021-01-15 15:33:33 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021, Nordic Semiconductor ASA. All rights reserved. |
Dávid Házi | be7f0de | 2023-07-19 11:25:38 +0200 | [diff] [blame] | 3 | * Copyright (c) 2023, Arm Limited. All rights reserved. |
Øyvind Rønningstad | f2c8dad | 2021-01-15 15:33:33 +0100 | [diff] [blame] | 4 | * |
| 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 Andersson | a4066d8 | 2022-04-06 16:32:09 +0200 | [diff] [blame] | 28 | #define EXCEPTION_TYPE_PLATFORM 5 |
Øyvind Rønningstad | f2c8dad | 2021-01-15 15:33:33 +0100 | [diff] [blame] | 29 | |
| 30 | /* This level of indirection is needed to fully resolve exception info when it's |
| 31 | * a macro |
| 32 | */ |
Dávid Házi | be7f0de | 2023-07-19 11:25:38 +0200 | [diff] [blame] | 33 | #define __STRINGIFY(exception_info) #exception_info |
Øyvind Rønningstad | f2c8dad | 2021-01-15 15:33:33 +0100 | [diff] [blame] | 34 | |
| 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ønningstad | f2c8dad | 2021-01-15 15:33:33 +0100 | [diff] [blame] | 41 | |
Chris Coleman | 9dd58c9 | 2023-10-08 13:49:23 -0400 | [diff] [blame^] | 42 | struct 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 | */ |
| 71 | void tfm_exception_info_get_context(struct exception_info_t *ctx); |
| 72 | |
Øyvind Rønningstad | f2c8dad | 2021-01-15 15:33:33 +0100 | [diff] [blame] | 73 | /* Store context for an exception, then print the info. |
| 74 | * Call EXCEPTION_INFO() instead of calling this directly. |
| 75 | */ |
| 76 | void store_and_dump_context(uint32_t LR_in, uint32_t MSP_in, uint32_t PSP_in, |
| 77 | uint32_t exception_type); |
Dávid Házi | be7f0de | 2023-07-19 11:25:38 +0200 | [diff] [blame] | 78 | |
| 79 | /* IAR Specific */ |
| 80 | #if defined(__ICCARM__) |
| 81 | #pragma required = store_and_dump_context |
Øyvind Rønningstad | f2c8dad | 2021-01-15 15:33:33 +0100 | [diff] [blame] | 82 | #endif |
| 83 | |
Dávid Házi | be7f0de | 2023-07-19 11:25:38 +0200 | [diff] [blame] | 84 | #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ønningstad | f2c8dad | 2021-01-15 15:33:33 +0100 | [diff] [blame] | 97 | #endif /* __EXCEPTION_INFO_H__ */ |