From a3774fd51a716ffafeddfb017f9c25a046013132 Mon Sep 17 00:00:00 2001 From: Jordan Yates Date: Thu, 26 Jan 2023 10:10:57 +1000 Subject: [PATCH] arch: option to generate simplified error codes Add an option to generate simplified error codes instead of more specific architecture specific error codes. Enable this by default in tests to make exception tests more generic across hardware. Fixes #54053. Signed-off-by: Jordan Yates --- arch/Kconfig | 12 ++++++++++++ arch/arm/core/aarch32/cortex_a_r/fault.c | 16 +++++++++++++++- arch/arm/core/aarch32/cortex_m/fault.c | 4 ++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/arch/Kconfig b/arch/Kconfig index 36e7ae844c8..10ae341a378 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -501,6 +501,18 @@ config EXTRA_EXCEPTION_INFO register state, when a fault occurs. This information can be useful to collect for post-mortem analysis and debug of issues. +config SIMPLIFIED_EXCEPTION_CODES + bool "Convert arch specific exception codes to K_ERR_CPU_EXCEPTION" + default y if ZTEST + help + The same piece of faulty code (NULL dereference, etc) can result in + a multitude of potential exception codes at the CPU level, depending + upon whether addresses exist, an MPU is configured, the particular + implementation of the CPU or any number of other reasons. Enabling + this option collapses all the architecture specific exception codes + down to the generic K_ERR_CPU_EXCEPTION, which makes testing code + much more portable. + endmenu # Interrupt configuration config INIT_ARCH_HW_AT_BOOT diff --git a/arch/arm/core/aarch32/cortex_a_r/fault.c b/arch/arm/core/aarch32/cortex_a_r/fault.c index 1b92c7037a6..8d7cc6ded32 100644 --- a/arch/arm/core/aarch32/cortex_a_r/fault.c +++ b/arch/arm/core/aarch32/cortex_a_r/fault.c @@ -194,8 +194,12 @@ bool z_arm_fault_undef_instruction(z_arch_esf_t *esf) /* Print fault information */ LOG_ERR("***** UNDEFINED INSTRUCTION ABORT *****"); + uint32_t reason = IS_ENABLED(CONFIG_SIMPLIFIED_EXCEPTION_CODES) ? + K_ERR_CPU_EXCEPTION : + K_ERR_ARM_UNDEFINED_INSTRUCTION; + /* Invoke kernel fatal exception handler */ - z_arm_fatal_error(K_ERR_ARM_UNDEFINED_INSTRUCTION, esf); + z_arm_fatal_error(reason, esf); /* All undefined instructions are treated as fatal for now */ return true; @@ -223,6 +227,11 @@ bool z_arm_fault_prefetch(z_arch_esf_t *esf) reason = dump_fault(fs, ifar); } + /* Simplify exception codes if requested */ + if (IS_ENABLED(CONFIG_SIMPLIFIED_EXCEPTION_CODES) && (reason >= K_ERR_ARCH_START)) { + reason = K_ERR_CPU_EXCEPTION; + } + /* Invoke kernel fatal exception handler */ z_arm_fatal_error(reason, esf); @@ -290,6 +299,11 @@ bool z_arm_fault_data(z_arch_esf_t *esf) reason = dump_fault(fs, dfar); } + /* Simplify exception codes if requested */ + if (IS_ENABLED(CONFIG_SIMPLIFIED_EXCEPTION_CODES) && (reason >= K_ERR_ARCH_START)) { + reason = K_ERR_CPU_EXCEPTION; + } + /* Invoke kernel fatal exception handler */ z_arm_fatal_error(reason, esf); diff --git a/arch/arm/core/aarch32/cortex_m/fault.c b/arch/arm/core/aarch32/cortex_m/fault.c index 93277a1b9d8..cece1d722ce 100644 --- a/arch/arm/core/aarch32/cortex_m/fault.c +++ b/arch/arm/core/aarch32/cortex_m/fault.c @@ -1124,6 +1124,10 @@ void z_arm_fault(uint32_t msp, uint32_t psp, uint32_t exc_return, esf_copy.basic.xpsr &= ~(IPSR_ISR_Msk); } + if (IS_ENABLED(CONFIG_SIMPLIFIED_EXCEPTION_CODES) && (reason >= K_ERR_ARCH_START)) { + reason = K_ERR_CPU_EXCEPTION; + } + z_arm_fatal_error(reason, &esf_copy); }