Skip to content

Commit

Permalink
Set PMP to create invalid address
Browse files Browse the repository at this point in the history
Signed-off-by: liangzhen <zhen.liang@spacemit.com>
Change-Id: I707281626521d321aadb6cee5aea457fc5c0424d
  • Loading branch information
lz-bro committed Feb 4, 2024
1 parent 67e7759 commit 188a89e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
8 changes: 8 additions & 0 deletions debug/gdbserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ def test(self):
class MemTestReadInvalid(SimpleMemoryTest):
def test(self):
bad_address = self.hart.bad_address
if self.target.support_set_pmp_deny:
self.set_pmp_deny(bad_address)
good_address = self.hart.ram + 0x80

self.write_nop_program(2)
Expand All @@ -279,6 +281,8 @@ def test(self):
self.gdb.stepi() # Don't let gdb cache register read
assertEqual(self.gdb.p(f"*((int*)0x{good_address:x})"), 0xabcdef)
assertEqual(self.gdb.p("$s0"), 0x12345678)
if self.target.support_set_pmp_deny:
self.reset_pmp_deny()

#class MemTestWriteInvalid(SimpleMemoryTest):
# def test(self):
Expand Down Expand Up @@ -2106,6 +2110,8 @@ def setup(self):
self.gdb.b("handle_trap")

def test(self):
if self.target.support_set_pmp_deny:
self.set_pmp_deny(0)
# Set trigger on Load access fault
self.gdb.command("monitor riscv etrigger set m 0x20")
# Set fox to a null pointer so we'll get a load access exception later.
Expand All @@ -2117,6 +2123,8 @@ def test(self):
# actual exception handler.
assertIn("breakpoint", output)
assertIn("trap_entry", self.gdb.where())
if self.target.support_set_pmp_deny:
self.reset_pmp_deny()

class IcountTest(DebugTest):
compile_args = ("programs/infinite_loop.S", )
Expand Down
3 changes: 3 additions & 0 deletions debug/targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ class Target:
# Instruction count limit
icount_limit = 4

# Support set_pmp_deny to create invalid addresses.
support_set_pmp_deny = False

# Internal variables:
directory = None
temporary_files = []
Expand Down
24 changes: 24 additions & 0 deletions debug/testlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,30 @@ def parkOtherHarts(self, symbol=None):
self.gdb.select_hart(self.hart)
self.gdb.command(f"monitor targets {self.hart.id}")

def set_pmp_deny(self, address, size=4 * 1024):
# Enable physical memory protection, no permission to access specific
# address range (default 4KB).
try:
self.gdb.p("$mseccfg=0x4") # RLB
self.gdb.p("$pmpcfg0=0x98") # L, NAPOT, !R, !W, !X
self.gdb.p("$pmpaddr0="
f"0x{((address >> 2) | ((size - 1) >> 3)):x}")
# PMP changes require an sfence.vma, 0x12000073 is sfence.vma
self.gdb.command("monitor riscv exec_progbuf 0x12000073")
except CouldNotFetch:
# PMP registers are optional
pass

def reset_pmp_deny(self):
try:
self.gdb.p("$pmpcfg0=0")
self.gdb.p("$pmpaddr0=0")
# PMP changes require an sfence.vma, 0x12000073 is sfence.vma
self.gdb.command("monitor riscv exec_progbuf 0x12000073")
except CouldNotFetch:
# PMP registers are optional
pass

def disable_pmp(self):
# Disable physical memory protection by allowing U mode access to all
# memory.
Expand Down

0 comments on commit 188a89e

Please sign in to comment.