Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions arch/riscv/core/pmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,34 +104,53 @@ static void print_pmp_entries(unsigned int pmp_start, unsigned int pmp_end,
}
}

static void dump_pmp_regs(const char *banner)
/**
* @brief Reads the PMP configuration CSRs (pmpcfgX) based on architecture and slot count.
*
* This helper function abstracts the logic required to read the correct CSRs
* (pmpcfg0, pmpcfg1, pmpcfg2, pmpcfg3) depending on whether the system is
* 32-bit or 64-bit and the total number of PMP slots configured.
*
* @param pmp_cfg Pointer to the array where the CSR contents will be stored.
* @param pmp_cfg_size The size of the pmp_cfg array, measured in unsigned long entries.
*/
static inline void z_riscv_pmp_read_config(unsigned long *pmp_cfg, size_t pmp_cfg_size)
{
unsigned long pmp_addr[CONFIG_PMP_SLOTS];
unsigned long pmp_cfg[CONFIG_PMP_SLOTS / PMPCFG_STRIDE];

#define PMPADDR_READ(x) pmp_addr[x] = csr_read(pmpaddr##x)

FOR_EACH(PMPADDR_READ, (;), 0, 1, 2, 3, 4, 5, 6, 7);
#if CONFIG_PMP_SLOTS > 8
FOR_EACH(PMPADDR_READ, (;), 8, 9, 10, 11, 12, 13, 14, 15);
#endif

#undef PMPADDR_READ
__ASSERT(pmp_cfg_size == (size_t)(CONFIG_PMP_SLOTS / PMPCFG_STRIDE),
"Invalid PMP config array size");

#ifdef CONFIG_64BIT
/* RV64: pmpcfg0 holds entries 0-7; pmpcfg2 holds entries 8-15. */
pmp_cfg[0] = csr_read(pmpcfg0);
#if CONFIG_PMP_SLOTS > 8
pmp_cfg[1] = csr_read(pmpcfg2);
#endif
#else
/* RV32: Each pmpcfg register holds 4 entries. */
pmp_cfg[0] = csr_read(pmpcfg0);
pmp_cfg[1] = csr_read(pmpcfg1);
#if CONFIG_PMP_SLOTS > 8
pmp_cfg[2] = csr_read(pmpcfg2);
pmp_cfg[3] = csr_read(pmpcfg3);
#endif
#endif
}

static void dump_pmp_regs(const char *banner)
{
unsigned long pmp_addr[CONFIG_PMP_SLOTS];
unsigned long pmp_cfg[CONFIG_PMP_SLOTS / PMPCFG_STRIDE];

#define PMPADDR_READ(x) pmp_addr[x] = csr_read(pmpaddr##x)

FOR_EACH(PMPADDR_READ, (;), 0, 1, 2, 3, 4, 5, 6, 7);
#if CONFIG_PMP_SLOTS > 8
FOR_EACH(PMPADDR_READ, (;), 8, 9, 10, 11, 12, 13, 14, 15);
#endif

#undef PMPADDR_READ

z_riscv_pmp_read_config(pmp_cfg, (size_t)(CONFIG_PMP_SLOTS / PMPCFG_STRIDE));
print_pmp_entries(0, CONFIG_PMP_SLOTS, pmp_addr, pmp_cfg, banner);
}

Expand Down