Skip to content

Commit 035d688

Browse files
committed
Verify memory access permissions in PMP regions
Checks if a memory access falls within a configured region by comparing the requested address and size against the region boundaries. When a matching region is found, validates that the region's permissions match the requested operation type.
1 parent 7aa7573 commit 035d688

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

arch/riscv/pmp.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,41 @@ int32_t pmp_get_region(const pmp_config_t *config, uint8_t region_idx,
263263

264264
return ERR_OK;
265265
}
266+
267+
int32_t pmp_check_access(const pmp_config_t *config, uint32_t addr,
268+
uint32_t size, uint8_t is_write, uint8_t is_execute)
269+
{
270+
if (!config)
271+
return ERR_PMP_INVALID_REGION;
272+
273+
uint32_t access_end = addr + size;
274+
275+
/* In TOR mode, check all regions in priority order */
276+
for (uint8_t i = 0; i < config->region_count; i++) {
277+
const pmp_region_t *region = &config->regions[i];
278+
279+
/* Skip disabled regions */
280+
if (region->addr_start == 0 && region->addr_end == 0)
281+
continue;
282+
283+
/* Check if access falls within this region */
284+
if (addr >= region->addr_start && access_end <= region->addr_end) {
285+
/* Verify permissions match access type */
286+
uint8_t required_perm = 0;
287+
if (is_write)
288+
required_perm |= PMPCFG_W;
289+
if (is_execute)
290+
required_perm |= PMPCFG_X;
291+
if (!is_write && !is_execute)
292+
required_perm = PMPCFG_R;
293+
294+
if ((region->permissions & required_perm) == required_perm)
295+
return 1; /* Access allowed */
296+
else
297+
return 0; /* Access denied */
298+
}
299+
}
300+
301+
/* Access not covered by any region */
302+
return 0;
303+
}

0 commit comments

Comments
 (0)