Skip to content

Commit bbd4eaa

Browse files
authored
Merge pull request #2157 from riscv-software-src/pmp
Fix PMP checks for misaligned accesses
2 parents 3767c6e + 6872aab commit bbd4eaa

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

riscv/mmu.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -490,12 +490,15 @@ bool mmu_t::pmp_ok(reg_t addr, reg_t len, access_type type, reg_t mode, bool hlv
490490
if (!proc || proc->n_pmp == 0)
491491
return true;
492492

493+
reg_t gran = reg_t(1) << proc->lg_pmp_granularity;
494+
auto first_addr_aligned = addr & -gran;
495+
auto last_addr_aligned = (addr + len - 1) & -gran;
496+
493497
for (size_t i = 0; i < proc->n_pmp; i++) {
494-
// Check each 4-byte sector of the access
498+
// Check each PMP-granularity sector of the access
495499
bool any_match = false;
496500
bool all_match = true;
497-
for (reg_t offset = 0; offset < len; offset += 1 << PMP_SHIFT) {
498-
reg_t cur_addr = addr + offset;
501+
for (reg_t cur_addr = first_addr_aligned; cur_addr <= last_addr_aligned; cur_addr += gran) {
499502
bool match = proc->state.pmpaddr[i]->match4(cur_addr);
500503
any_match |= match;
501504
all_match &= match;

0 commit comments

Comments
 (0)