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 <jordan.yates@data61.csiro.au>
diff --git a/arch/Kconfig b/arch/Kconfig
index 36e7ae8..10ae341 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -501,6 +501,18 @@
 	  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 1b92c70..8d7cc6d 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 @@
 	/* 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 @@
 		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 @@
 		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 93277a1..cece1d7 100644
--- a/arch/arm/core/aarch32/cortex_m/fault.c
+++ b/arch/arm/core/aarch32/cortex_m/fault.c
@@ -1124,6 +1124,10 @@
 		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);
 }