1 /*
2  * Copyright (c) 2021 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/sys/printk.h>
9 #include <zephyr/sys/reboot.h>
10 #include <cmsis_core.h>
11 #include <zephyr/ztest.h>
12 #include <zephyr/tc_util.h>
13 
14 static volatile int expected_reason = -1;
15 
k_sys_fatal_error_handler(unsigned int reason,const struct arch_esf * pEsf)16 void k_sys_fatal_error_handler(unsigned int reason, const struct arch_esf *pEsf)
17 {
18 	static bool triggered_synchronous_svc;
19 
20 	TC_PRINT("Caught system error -- reason %d\n", reason);
21 
22 	if (expected_reason == -1) {
23 		printk("Was not expecting a crash\n");
24 		k_fatal_halt(reason);
25 	}
26 
27 	if (reason != expected_reason) {
28 		printk("Wrong crash type got %d expected %d\n", reason,
29 			expected_reason);
30 		k_fatal_halt(reason);
31 	}
32 
33 	/* Fault validation succeeded */
34 
35 	if (triggered_synchronous_svc == false) {
36 		triggered_synchronous_svc = true;
37 
38 		/* Trigger a new CPU runtime error from
39 		 * inside the current runtime error
40 		 */
41 		expected_reason = K_ERR_KERNEL_PANIC;
42 		__ASSERT(0,
43 		"Assert occurring inside kernel panic");
44 	}
45 
46 	expected_reason = -1;
47 }
48 
ZTEST(arm_hardfault_validation,test_arm_hardfault)49 ZTEST(arm_hardfault_validation, test_arm_hardfault)
50 {
51 	expected_reason = K_ERR_KERNEL_PANIC;
52 
53 	k_panic();
54 }
55