Skip to content

Commit 9bf5a2f

Browse files
committed
debug: enhance set_pmp_deny to support alignment checks
- set size considering the minimum PMP granularity supported by the hart. Signed-off-by: Hiroo HAYASHI <24754036+hirooih@users.noreply.github.com>
1 parent fd4e6cd commit 9bf5a2f

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

debug/gdbserver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ class MemTestReadInvalid(SimpleMemoryTest):
279279
def test(self):
280280
bad_address = self.hart.bad_address
281281
if self.target.support_set_pmp_deny:
282-
self.set_pmp_deny(bad_address)
282+
self.set_pmp_deny(bad_address, 4)
283283
self.gdb.command("monitor riscv set_mem_access progbuf abstract")
284284
good_address = self.hart.ram + 0x80
285285

@@ -2205,7 +2205,7 @@ def test(self):
22052205
# Use NULL if a known-bad address is not provided.
22062206
bad_address = self.hart.bad_address or 0
22072207
if self.target.support_set_pmp_deny:
2208-
self.set_pmp_deny(bad_address)
2208+
self.set_pmp_deny(bad_address, 1)
22092209
self.gdb.command("monitor riscv set_mem_access progbuf abstract")
22102210
self.gdb.p(f"fox=(char*)0x{bad_address:08x}")
22112211
output = self.gdb.c()

debug/testlib.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,9 +1488,32 @@ def parkOtherHarts(self, symbol=None):
14881488
self.gdb.select_hart(self.hart)
14891489
self.gdb.command(f"monitor targets {self.hart.id}")
14901490

1491-
def set_pmp_deny(self, address, size=4 * 1024):
1491+
def ctz(self, i):
1492+
# count trailing zeros
1493+
return (i & -i).bit_length() - 1
1494+
1495+
def get_minimum_pmp_granularity(self):
1496+
# Determine the minimum PMP granularity supported by this hart.
1497+
# cf. RISC-V Privileged Architecture, 3.7.1.1. Address Matching
1498+
self.gdb.p("$pmpcfg0=0") # Null region
1499+
self.gdb.p("$pmpaddr0=-1") # All ones
1500+
readback = self.gdb.p("$pmpaddr0")
1501+
return 2**(self.ctz(readback) + 2)
1502+
1503+
def set_pmp_deny(self, address, size):
14921504
# Enable physical memory protection, no permission to access specific
1493-
# address range (default 4KB).
1505+
# address range. The size must be a power of two and the address must be
1506+
# naturally aligned to the size.
1507+
1508+
# PMP requires size to be at least the minimum granularity.
1509+
# The minimum granularity for NAPOT mode is 8 bytes.
1510+
size = max(size, self.get_minimum_pmp_granularity(), 8)
1511+
1512+
if 2**self.ctz(address) < size:
1513+
raise TestNotApplicable(
1514+
f"address 0x{address:x} should be naturally aligned to "
1515+
f"0x{size:x}.")
1516+
14941517
self.gdb.p("$mseccfg=0x4") # RLB
14951518
self.gdb.p("$pmpcfg0=0x98") # L, NAPOT, !R, !W, !X
14961519
self.gdb.p("$pmpaddr0="

0 commit comments

Comments
 (0)