Skip to content

Commit 8845cf6

Browse files
committed
tests: riscv: Add unit tests for riscv_pmp_decode_region
This commit introduces a suite of unit tests for the `pmp_decode_region` function using the Ztest framework. The tests validate the correct calculation of start and end addresses for various RISC-V PMP (Physical Memory Protection) entry configurations. The following scenarios are covered: - TOR (Top of Range) mode for index 0. - TOR (Top of Range) mode for index > 0. - NA4 (Naturally Aligned Four-byte) mode. - NAPOT (Naturally Aligned Power-of-Two) mode. - Default behavior for a disabled PMP entry. These tests ensure the PMP region decoding logic is accurate across different addressing modes. Signed-off-by: Firas Sammoura <[email protected]>
1 parent 6c83b9b commit 8845cf6

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_ZTEST=y
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) 2025 Google LLC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <kernel_internal.h>
8+
#include <zephyr/tc_util.h>
9+
#include <zephyr/ztest.h>
10+
11+
ZTEST(riscv_pmp_decode_region, test_pmp_tor_index_0)
12+
{
13+
unsigned long pmp_addr[] = {
14+
0x10000000 >> 2,
15+
0,
16+
};
17+
uint8_t cfg = PMP_TOR;
18+
unsigned int index = 0;
19+
unsigned long start_addr, end_addr;
20+
21+
unsigned long expected_end = 0x10000000 - 1;
22+
23+
pmp_decode_region(cfg, pmp_addr, index, &start_addr, &end_addr);
24+
25+
zassert_equal(0, start_addr, "TOR index 0 start address mismatch");
26+
zassert_equal(expected_end, end_addr, "TOR index 0 end address mismatch");
27+
}
28+
29+
ZTEST(riscv_pmp_decode_region, test_pmp_tor_index_n)
30+
{
31+
unsigned long pmp_addr[] = {
32+
0x10000000 >> 2,
33+
0x20000000 >> 2,
34+
};
35+
uint8_t cfg = PMP_TOR;
36+
unsigned int index = 1;
37+
unsigned long start_addr, end_addr;
38+
39+
unsigned long expected_start = 0x10000000;
40+
unsigned long expected_end = 0x20000000 - 1;
41+
42+
pmp_decode_region(cfg, pmp_addr, index, &start_addr, &end_addr);
43+
44+
zassert_equal(expected_start, start_addr, "TOR index n start address mismatch");
45+
zassert_equal(expected_end, end_addr, "TOR index n end address mismatch");
46+
}
47+
48+
ZTEST(riscv_pmp_decode_region, test_pmp_na4)
49+
{
50+
unsigned long pmp_addr[] = {
51+
0xADBEEF00 >> 2,
52+
};
53+
uint8_t cfg = PMP_NA4;
54+
unsigned int index = 0;
55+
unsigned long start_addr, end_addr;
56+
57+
unsigned long expected_start = 0xADBEEF00;
58+
unsigned long expected_end = 0xADBEEF00 + 3;
59+
60+
pmp_decode_region(cfg, pmp_addr, index, &start_addr, &end_addr);
61+
62+
zassert_equal(expected_start, start_addr, "NA4 start address mismatch");
63+
zassert_equal(expected_end, end_addr, "NA4 end address mismatch");
64+
}
65+
66+
ZTEST(riscv_pmp_decode_region, test_pmp_napot)
67+
{
68+
unsigned long pmp_addr[] = {0x20000000 >> 2};
69+
uint8_t cfg = PMP_NAPOT;
70+
unsigned int index = 0;
71+
unsigned long start_addr, end_addr;
72+
73+
unsigned long expected_start = 0x20000000;
74+
unsigned long expected_end = 0x20000007;
75+
76+
pmp_decode_region(cfg, pmp_addr, index, &start_addr, &end_addr);
77+
zassert_equal(expected_start, start_addr, "NAPOT 8-byte start address mismatch");
78+
zassert_equal(expected_end, end_addr, "NAPOT 8-byte end address mismatch");
79+
}
80+
81+
ZTEST(riscv_pmp_decode_region, test_pmp_default_disabled)
82+
{
83+
unsigned long pmp_addr[] = {0x12345678 >> 2};
84+
uint8_t cfg = 0x00;
85+
unsigned int index = 0;
86+
unsigned long start_addr, end_addr;
87+
88+
pmp_decode_region(cfg, pmp_addr, index, &start_addr, &end_addr);
89+
zassert_equal(0, start_addr, "Default start address mismatch");
90+
zassert_equal(0, end_addr, "Default end address mismatch");
91+
}
92+
93+
ZTEST_SUITE(riscv_pmp_decode_region, NULL, NULL, NULL, NULL, NULL);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
common:
2+
platform_allow:
3+
- qemu_riscv32
4+
- qemu_riscv32e
5+
- qemu_riscv64
6+
filter: CONFIG_RISCV_PMP
7+
8+
tests:
9+
arch.riscv.pmp.decode.region: {}

0 commit comments

Comments
 (0)