1 /*
2  * Copyright (c) 2024 Marvell.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #include <kernel_internal.h>
7 #include <zephyr/tc_util.h>
8 #include <zephyr/ztest.h>
9 
10 static volatile ZTEST_BMEM bool valid_fault;
11 
k_sys_fatal_error_handler(unsigned int reason,const struct arch_esf * pEsf)12 void k_sys_fatal_error_handler(unsigned int reason, const struct arch_esf *pEsf)
13 {
14 	int rv = TC_PASS;
15 
16 	TC_PRINT("Caught system error -- reason %d %d\n", reason, valid_fault);
17 	if (!valid_fault) {
18 		TC_PRINT("Fatal error was unexpected, aborting...\n");
19 		rv = TC_FAIL;
20 	}
21 	TC_END_RESULT_CUSTOM(rv, "test_pmp");
22 	TC_END_REPORT(rv);
23 	arch_system_halt(reason);
24 }
25 
26 #ifdef CONFIG_PMP_STACK_GUARD
check_isr_stack_guard(void)27 static void check_isr_stack_guard(void)
28 {
29 	char *isr_stack = (char *)z_interrupt_stacks;
30 
31 	valid_fault = true;
32 	*isr_stack = 42;
33 }
34 
check_main_stack_guard(void)35 static void check_main_stack_guard(void)
36 {
37 	char *main_stack = (char *)z_main_stack;
38 
39 	valid_fault = true;
40 	*main_stack = 42;
41 }
42 
43 #else
44 
check_isr_stack_guard(void)45 static void check_isr_stack_guard(void)
46 {
47 	ztest_test_skip();
48 }
49 
check_main_stack_guard(void)50 static void check_main_stack_guard(void)
51 {
52 	ztest_test_skip();
53 }
54 
55 #endif /* CONFIG_PMP_STACK_GUARD */
56 
57 typedef void (*pmp_test_func_t)(void);
58 
59 static const pmp_test_func_t pmp_test_func[] = {
60 	check_isr_stack_guard,
61 	check_main_stack_guard,
62 };
63 
64 /**
65  * @brief Verify RISC-V specific PMP stack guard regions.
66  * @details Manually write to the protected stack region to trigger fatal error.
67  */
ZTEST(riscv_pmp_no_mt,test_pmp)68 ZTEST(riscv_pmp_no_mt, test_pmp)
69 {
70 #ifndef PMP_TEST_FUNC_IDX
71 #define PMP_TEST_FUNC_IDX 0
72 #endif
73 	pmp_test_func[PMP_TEST_FUNC_IDX]();
74 
75 	zassert_unreachable("Write to stack guard did not fault");
76 	TC_END_REPORT(TC_FAIL);
77 }
78 
79 ZTEST_SUITE(riscv_pmp_no_mt, NULL, NULL, NULL, NULL, NULL);
80