Skip to content

Commit 8cc7c11

Browse files
committed
test: 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 8cc7c11

File tree

1 file changed

+82
-0
lines changed
  • tests/arch/riscv/pmp/mem-attr-entries/src

1 file changed

+82
-0
lines changed

tests/arch/riscv/pmp/mem-attr-entries/src/main.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,86 @@ ZTEST(riscv_pmp_memattr_entries, test_dt_pmp_perm_conversion)
124124
PMP_R | PMP_W | PMP_X, result);
125125
}
126126

127+
ZTEST(riscv_pmp_memattr_entries, test_pmp_tor_index_0)
128+
{
129+
unsigned long pmp_addr[] = {
130+
0x10000000 >> 2,
131+
0,
132+
};
133+
uint8_t cfg = PMP_TOR;
134+
unsigned int index = 0;
135+
unsigned long start_addr, end_addr;
136+
137+
unsigned long expected_end = 0x10000000 - 1;
138+
139+
pmp_decode_region(cfg, pmp_addr, index, &start_addr, &end_addr);
140+
141+
zassert_equal(0, start_addr, "TOR index 0 start address mismatch");
142+
zassert_equal(expected_end, end_addr, "TOR index 0 end address mismatch");
143+
}
144+
145+
ZTEST(riscv_pmp_memattr_entries, test_pmp_tor_index_n)
146+
{
147+
unsigned long pmp_addr[] = {
148+
0x10000000 >> 2,
149+
0x20000000 >> 2,
150+
};
151+
uint8_t cfg = PMP_TOR;
152+
unsigned int index = 1;
153+
unsigned long start_addr, end_addr;
154+
155+
unsigned long expected_start = 0x10000000;
156+
unsigned long expected_end = 0x20000000 - 1;
157+
158+
pmp_decode_region(cfg, pmp_addr, index, &start_addr, &end_addr);
159+
160+
zassert_equal(expected_start, start_addr, "TOR index n start address mismatch");
161+
zassert_equal(expected_end, end_addr, "TOR index n end address mismatch");
162+
}
163+
164+
ZTEST(riscv_pmp_memattr_entries, test_pmp_na4)
165+
{
166+
unsigned long pmp_addr[] = {
167+
0xADBEEF00 >> 2,
168+
};
169+
uint8_t cfg = PMP_NA4;
170+
unsigned int index = 0;
171+
unsigned long start_addr, end_addr;
172+
173+
unsigned long expected_start = 0xADBEEF00;
174+
unsigned long expected_end = 0xADBEEF00 + 3;
175+
176+
pmp_decode_region(cfg, pmp_addr, index, &start_addr, &end_addr);
177+
178+
zassert_equal(expected_start, start_addr, "NA4 start address mismatch");
179+
zassert_equal(expected_end, end_addr, "NA4 end address mismatch");
180+
}
181+
182+
ZTEST(riscv_pmp_memattr_entries, test_pmp_napot)
183+
{
184+
unsigned long pmp_addr[] = {0x20000000 >> 2};
185+
uint8_t cfg = PMP_NAPOT;
186+
unsigned int index = 0;
187+
unsigned long start_addr, end_addr;
188+
189+
unsigned long expected_start = 0x20000000;
190+
unsigned long expected_end = 0x20000007;
191+
192+
pmp_decode_region(cfg, pmp_addr, index, &start_addr, &end_addr);
193+
zassert_equal(expected_start, start_addr, "NAPOT 8-byte start address mismatch");
194+
zassert_equal(expected_end, end_addr, "NAPOT 8-byte end address mismatch");
195+
}
196+
197+
ZTEST(riscv_pmp_memattr_entries, test_pmp_default_disabled)
198+
{
199+
unsigned long pmp_addr[] = {0x12345678 >> 2};
200+
uint8_t cfg = 0x00;
201+
unsigned int index = 0;
202+
unsigned long start_addr, end_addr;
203+
204+
pmp_decode_region(cfg, pmp_addr, index, &start_addr, &end_addr);
205+
zassert_equal(0, start_addr, "Default start address mismatch");
206+
zassert_equal(0, end_addr, "Default end address mismatch");
207+
}
208+
127209
ZTEST_SUITE(riscv_pmp_memattr_entries, NULL, NULL, NULL, NULL, NULL);

0 commit comments

Comments
 (0)