Ø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 | |
| 42 | /* Store context for an exception, then print the info. |
| 43 | * Call EXCEPTION_INFO() instead of calling this directly. |
| 44 | */ |
| 45 | void store_and_dump_context(uint32_t LR_in, uint32_t MSP_in, uint32_t PSP_in, |
| 46 | uint32_t exception_type); |
Dávid Házi | be7f0de | 2023-07-19 11:25:38 +0200 | [diff] [blame^] | 47 | |
| 48 | /* IAR Specific */ |
| 49 | #if defined(__ICCARM__) |
| 50 | #pragma required = store_and_dump_context |
Øyvind Rønningstad | f2c8dad | 2021-01-15 15:33:33 +0100 | [diff] [blame] | 51 | #endif |
| 52 | |
Dávid Házi | be7f0de | 2023-07-19 11:25:38 +0200 | [diff] [blame^] | 53 | #define EXCEPTION_INFO(exception_type) \ |
| 54 | __ASM volatile( \ |
| 55 | "MOV r0, lr\n" \ |
| 56 | "MRS r1, MSP\n" \ |
| 57 | "MRS r2, PSP\n" \ |
| 58 | "MOVS r3, #" __STRINGIFY(exception_type) "\n"\ |
| 59 | "BL store_and_dump_context\n" \ |
| 60 | ) |
| 61 | |
| 62 | #else /* TFM_EXCEPTION_INFO_DUMP */ |
| 63 | #define EXCEPTION_INFO(exception_type) |
| 64 | #endif /* TFM_EXCEPTION_INFO_DUMP */ |
| 65 | |
Øyvind Rønningstad | f2c8dad | 2021-01-15 15:33:33 +0100 | [diff] [blame] | 66 | #endif /* __EXCEPTION_INFO_H__ */ |