Skip to content

Commit 1310856

Browse files
f0rget-the-sadnashif
authored andcommitted
tests: riscv: test PMP stack guards
Test if PMP protected regions prevents write access. Signed-off-by: Volodymyr Fialko <[email protected]>
1 parent 9eee2ea commit 1310856

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(riscv_pmp)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})
9+
10+
target_include_directories(app PRIVATE
11+
${ZEPHYR_BASE}/kernel/include
12+
${ZEPHYR_BASE}/arch/${ARCH}/include
13+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_MULTITHREADING=n
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
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
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+
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+
45+
static void check_isr_stack_guard(void)
46+
{
47+
ztest_test_skip();
48+
}
49+
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+
*/
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);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
common:
2+
platform_allow:
3+
- qemu_riscv32
4+
- qemu_riscv32e
5+
- qemu_riscv64
6+
filter: CONFIG_RISCV_PMP
7+
ignore_faults: true
8+
9+
tests:
10+
arch.riscv.pmp.no-mt.isr-stack-guard:
11+
extra_args: EXTRA_CFLAGS=-DPMP_TEST_FUNC_IDX=0
12+
arch.riscv.pmp.no-mt.main-stack-guard:
13+
extra_args: EXTRA_CFLAGS=-DPMP_TEST_FUNC_IDX=1

0 commit comments

Comments
 (0)