Skip to contentSkip to conversation

Extend and configure PMP region count

PATCH Applied Jay Chang 3 participants 8 messages May 22, 2025 – Jun 5, 2025 RISC-V

v4 of 5 Applied Latest is v5 →

lore
Share & tools
lore

Jay Chang

author
#0 lore raw

v4 cover Extend and configure PMP region count

The first patch extends the number of PMP regions supported up to 64, following the RISC-V Privileged Specification (version >1.12), where RV32 can have up to 64 PMP regions configured through 16 CSRs.

The second patch makes the PMP region count configurable via a new CPU parameter num-pmp-regions. This allows platforms to adjust the number of PMP regions without relying on a fixed default value. If unspecified, the default remains 16 to preserve compatibility.

Change log:
V4:
* Changed pmp_regions type from uint16_t to uint8_t
* Introduced OLD_MAX_RISCV_PMPS macro

Jay Chang (2):
target/riscv: Extend PMP region up to 64 target/riscv: Make PMP region count configurable

 target/riscv/cpu.c                |  54 ++++++++++++-
 target/riscv/cpu.h                |   3 +-
 target/riscv/cpu_bits.h           |  60 ++++++++++++++
 target/riscv/cpu_cfg_fields.h.inc |   1 +
 target/riscv/csr.c                | 129 +++++++++++++++++++++++++++++-
 target/riscv/machine.c            |   3 +-
 target/riscv/pmp.c                |  28 ++++---
 7 files changed, 262 insertions(+), 16 deletions(-)
Signature · 1 line
2.48.1
author
#1 lore raw

PATCH v4 1/2 target/riscv: Extend PMP region up to 64

According to the RISC-V Privileged Specification (version >1.12), RV32 supports 16 CSRs (pmpcfg0–pmpcfg15) to configure 64 PMP regions (pmpaddr0–pmpaddr63).

---
 target/riscv/cpu_bits.h |  60 +++++++++++++++++++
 target/riscv/csr.c      | 124 +++++++++++++++++++++++++++++++++++++++-
 2 files changed, 182 insertions(+), 2 deletions(-)
target/riscv/cpu_bits.h +60 -0
index a30317c617..e6b3e28386 100644
@@ -372,6 +372,18 @@
 #define CSR_PMPCFG1         0x3a1
 #define CSR_PMPCFG2         0x3a2
 #define CSR_PMPCFG3         0x3a3
+#define CSR_PMPCFG4         0x3a4
+#define CSR_PMPCFG5         0x3a5
+#define CSR_PMPCFG6         0x3a6
+#define CSR_PMPCFG7         0x3a7
+#define CSR_PMPCFG8         0x3a8
+#define CSR_PMPCFG9         0x3a9
+#define CSR_PMPCFG10        0x3aa
+#define CSR_PMPCFG11        0x3ab
+#define CSR_PMPCFG12        0x3ac
+#define CSR_PMPCFG13        0x3ad
+#define CSR_PMPCFG14        0x3ae
+#define CSR_PMPCFG15        0x3af
 #define CSR_PMPADDR0        0x3b0
 #define CSR_PMPADDR1        0x3b1
 #define CSR_PMPADDR2        0x3b2
@@ -388,6 +400,54 @@
 #define CSR_PMPADDR13       0x3bd
 #define CSR_PMPADDR14       0x3be
 #define CSR_PMPADDR15       0x3bf
+#define CSR_PMPADDR16       0x3c0
+#define CSR_PMPADDR17       0x3c1
+#define CSR_PMPADDR18       0x3c2
+#define CSR_PMPADDR19       0x3c3
+#define CSR_PMPADDR20       0x3c4
+#define CSR_PMPADDR21       0x3c5
+#define CSR_PMPADDR22       0x3c6
+#define CSR_PMPADDR23       0x3c7
+#define CSR_PMPADDR24       0x3c8
+#define CSR_PMPADDR25       0x3c9
+#define CSR_PMPADDR26       0x3ca
+#define CSR_PMPADDR27       0x3cb
+#define CSR_PMPADDR28       0x3cc
+#define CSR_PMPADDR29       0x3cd
+#define CSR_PMPADDR30       0x3ce
+#define CSR_PMPADDR31       0x3cf
+#define CSR_PMPADDR32       0x3d0
+#define CSR_PMPADDR33       0x3d1
+#define CSR_PMPADDR34       0x3d2
+#define CSR_PMPADDR35       0x3d3
+#define CSR_PMPADDR36       0x3d4
+#define CSR_PMPADDR37       0x3d5
+#define CSR_PMPADDR38       0x3d6
+#define CSR_PMPADDR39       0x3d7
+#define CSR_PMPADDR40       0x3d8
+#define CSR_PMPADDR41       0x3d9
+#define CSR_PMPADDR42       0x3da
+#define CSR_PMPADDR43       0x3db
+#define CSR_PMPADDR44       0x3dc
+#define CSR_PMPADDR45       0x3dd
+#define CSR_PMPADDR46       0x3de
+#define CSR_PMPADDR47       0x3df
+#define CSR_PMPADDR48       0x3e0
+#define CSR_PMPADDR49       0x3e1
+#define CSR_PMPADDR50       0x3e2
+#define CSR_PMPADDR51       0x3e3
+#define CSR_PMPADDR52       0x3e4
+#define CSR_PMPADDR53       0x3e5
+#define CSR_PMPADDR54       0x3e6
+#define CSR_PMPADDR55       0x3e7
+#define CSR_PMPADDR56       0x3e8
+#define CSR_PMPADDR57       0x3e9
+#define CSR_PMPADDR58       0x3ea
+#define CSR_PMPADDR59       0x3eb
+#define CSR_PMPADDR60       0x3ec
+#define CSR_PMPADDR61       0x3ed
+#define CSR_PMPADDR62       0x3ee
+#define CSR_PMPADDR63       0x3ef
 
 /* RNMI */
 #define CSR_MNSCRATCH       0x740
target/riscv/csr.c +122 -2

136 lines not on this page · raw message

Signature · 1 line
2.48.1
author
#2 lore raw

PATCH v4 2/2 target/riscv: Make PMP region count configurable

In reply to Jay Chang

Previously, the number of PMP regions was hardcoded to 16 in QEMU. This patch replaces the fixed value with a new pmp_regions field, allowing platforms to configure the number of PMP regions.

If no specific value is provided, the default number of PMP regions remains 16 to preserve the existing behavior.

A new CPU parameter num-pmp-regions has been introduced to the QEMU command line. For example:

	-cpu rv64, g=true, c=true, pmp=true, num-pmp-regions=8
---
 target/riscv/cpu.c                | 54 +++++++++++++++++++++++++++++--
 target/riscv/cpu.h                |  3 +-
 target/riscv/cpu_cfg_fields.h.inc |  1 +
 target/riscv/csr.c                |  5 ++-
 target/riscv/machine.c            |  3 +-
 target/riscv/pmp.c                | 28 ++++++++++------
 6 files changed, 80 insertions(+), 14 deletions(-)
target/riscv/cpu.c +52 -2
index 629ac37501..8e32252c11 100644
@@ -1117,6 +1117,7 @@ static void riscv_cpu_init(Object *obj)
     cpu->cfg.cbom_blocksize = 64;
     cpu->cfg.cbop_blocksize = 64;
     cpu->cfg.cboz_blocksize = 64;
+    cpu->cfg.pmp_regions = 16;
     cpu->env.vext_ver = VEXT_VERSION_1_00_0;
     cpu->cfg.max_satp_mode = -1;
 
@@ -1568,6 +1569,46 @@ static const PropertyInfo prop_pmp = {
     .set = prop_pmp_set,
 };
 
+static void prop_num_pmp_regions_set(Object *obj, Visitor *v, const char *name,
+                                     void *opaque, Error **errp)
+{
+    RISCVCPU *cpu = RISCV_CPU(obj);
+    uint8_t value;
+
+    visit_type_uint8(v, name, &value, errp);
+
+    if (cpu->cfg.pmp_regions != value && riscv_cpu_is_vendor(obj)) {
+        cpu_set_prop_err(cpu, name, errp);
+        return;
+    }
+
+    if (cpu->env.priv_ver < PRIV_VERSION_1_12_0 && value > OLD_MAX_RISCV_PMPS) {
+        error_setg(errp, "Number of PMP regions exceeds maximum available");
+        return;
+    } else if (value > MAX_RISCV_PMPS) {
+        error_setg(errp, "Number of PMP regions exceeds maximum available");
+        return;
+    }
+
+    cpu_option_add_user_setting(name, value);
+    cpu->cfg.pmp_regions = value;
+}
+
+static void prop_num_pmp_regions_get(Object *obj, Visitor *v, const char *name,
+                                     void *opaque, Error **errp)
+{
+    uint8_t value = RISCV_CPU(obj)->cfg.pmp_regions;
+
+    visit_type_uint8(v, name, &value, errp);
+}
+
+static const PropertyInfo prop_num_pmp_regions = {
+    .type = "uint8",
+    .description = "num-pmp-regions",
+    .get = prop_num_pmp_regions_get,
+    .set = prop_num_pmp_regions_set,
+};
+
 static int priv_spec_from_str(const char *priv_spec_str)
 {
     int priv_version = -1;
@@ -2567,6 +2608,7 @@ static const Property riscv_cpu_properties[] = {
 
     {.name = "mmu", .info = &prop_mmu},
     {.name = "pmp", .info = &prop_pmp},
+    {.name = "num-pmp-regions", .info = &prop_num_pmp_regions},
 
     {.name = "priv_spec", .info = &prop_priv_spec},
     {.name = "vext_spec", .info = &prop_vext_spec},
@@ -2891,6 +2933,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
     DEFINE_ABSTRACT_RISCV_CPU(TYPE_RISCV_DYNAMIC_CPU, TYPE_RISCV_CPU,
         .cfg.mmu = true,
         .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
         .priv_spec = PRIV_VERSION_LATEST,
     ),
 
@@ -2937,7 +2980,8 @@ static const TypeInfo riscv_cpu_type_infos[] = {
         .cfg.max_satp_mode = VM_1_10_MBARE,
         .cfg.ext_zifencei = true,
         .cfg.ext_zicsr = true,
-        .cfg.pmp = true
+        .cfg.pmp = true,
+        .cfg.pmp_regions = 8
     ),
 
     DEFINE_ABSTRACT_RISCV_CPU(TYPE_RISCV_CPU_SIFIVE_U, TYPE_RISCV_VENDOR_CPU,
@@ -2948,7 +2992,8 @@ static const TypeInfo riscv_cpu_type_infos[] = {
         .cfg.ext_zifencei = true,
         .cfg.ext_zicsr = true,
         .cfg.mmu = true,
-        .cfg.pmp = true
+        .cfg.pmp = true,
+        .cfg.pmp_regions = 8
     ),
 
 #if defined(TARGET_RISCV32) || \
@@ -2966,6 +3011,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
         .cfg.ext_zifencei = true,
         .cfg.ext_zicsr = true,
         .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
         .cfg.ext_smepmp = true,
 
         .cfg.ext_zba = true,
@@ -3040,6 +3086,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
         .cfg.ext_xtheadmempair = true,
         .cfg.ext_xtheadsync = true,
         .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
 
         .cfg.mvendorid = THEAD_VENDOR_ID,
 
@@ -3063,6 +3110,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
         .cfg.rvv_ta_all_1s = true,
         .cfg.misa_w = true,
         .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
         .cfg.cbom_blocksize = 64,
         .cfg.cbop_blocksize = 64,
         .cfg.cboz_blocksize = 64,
@@ -3119,6 +3167,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
         .cfg.ext_zifencei = true,
         .cfg.ext_zicsr = true,
         .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
         .cfg.ext_zicbom = true,
         .cfg.cbom_blocksize = 64,
         .cfg.cboz_blocksize = 64,
@@ -3163,6 +3212,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
 
         .cfg.mmu = true,
         .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
 
         .cfg.max_satp_mode = VM_1_10_SV39,
     ),
target/riscv/cpu.h +2 -1
index 229ade9ed9..67323a7d9d 100644
@@ -159,7 +159,8 @@ extern RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[];
 
 #define MMU_USER_IDX 3
 
-#define MAX_RISCV_PMPS (16)
+#define MAX_RISCV_PMPS (64)
+#define OLD_MAX_RISCV_PMPS (16)
 
 #if !defined(CONFIG_USER_ONLY)
 #include "pmp.h"
target/riscv/cpu_cfg_fields.h.inc +1 -0
index 59f134a419..33c4f9bac8 100644
@@ -163,6 +163,7 @@ TYPED_FIELD(uint16_t, elen, 0)
 TYPED_FIELD(uint16_t, cbom_blocksize, 0)
 TYPED_FIELD(uint16_t, cbop_blocksize, 0)
 TYPED_FIELD(uint16_t, cboz_blocksize, 0)
+TYPED_FIELD(uint8_t,  pmp_regions, 0)
 
 TYPED_FIELD(int8_t, max_satp_mode, -1)
 
target/riscv/csr.c +4 -1
index d6cd441133..6296ecd1e1 100644
@@ -738,7 +738,10 @@ static RISCVException dbltrp_hmode(CPURISCVState *env, int csrno)
 static RISCVException pmp(CPURISCVState *env, int csrno)
 {
     if (riscv_cpu_cfg(env)->pmp) {
-        if (csrno <= CSR_PMPCFG3) {
+        int max_pmpcfg = (env->priv_ver >= PRIV_VERSION_1_12_0) ?
++                              CSR_PMPCFG15 : CSR_PMPCFG3;
+
+        if (csrno <= max_pmpcfg) {
             uint32_t reg_index = csrno - CSR_PMPCFG0;
 
             /* TODO: RV128 restriction check */
target/riscv/machine.c +2 -1
index c97e9ce9df..1600ec44f0 100644
@@ -36,8 +36,9 @@ static int pmp_post_load(void *opaque, int version_id)
     RISCVCPU *cpu = opaque;
     CPURISCVState *env = &cpu->env;
     int i;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
-    for (i = 0; i < MAX_RISCV_PMPS; i++) {
+    for (i = 0; i < pmp_regions; i++) {
         pmp_update_rule_addr(env, i);
     }
     pmp_update_rule_nums(env);
target/riscv/pmp.c +19 -9
index 5af295e410..3540327c9a 100644
@@ -122,7 +122,9 @@ uint32_t pmp_get_num_rules(CPURISCVState *env)
  */
 static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index)
 {
-    if (pmp_index < MAX_RISCV_PMPS) {
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
+
+    if (pmp_index < pmp_regions) {
         return env->pmp_state.pmp[pmp_index].cfg_reg;
     }
 
@@ -136,7 +138,9 @@ static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index)
  */
 static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)
 {
-    if (pmp_index < MAX_RISCV_PMPS) {
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
+
+    if (pmp_index < pmp_regions) {
         if (env->pmp_state.pmp[pmp_index].cfg_reg == val) {
             /* no change */
             return false;
@@ -236,9 +240,10 @@ void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index)
 void pmp_update_rule_nums(CPURISCVState *env)
 {
     int i;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
     env->pmp_state.num_rules = 0;
-    for (i = 0; i < MAX_RISCV_PMPS; i++) {
+    for (i = 0; i < pmp_regions; i++) {
         const uint8_t a_field =
             pmp_get_a_field(env->pmp_state.pmp[i].cfg_reg);
         if (PMP_AMATCH_OFF != a_field) {
@@ -332,6 +337,7 @@ bool pmp_hart_has_privs(CPURISCVState *env, hwaddr addr,
     int pmp_size = 0;
     hwaddr s = 0;
     hwaddr e = 0;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
     /* Short cut if no rules */
     if (0 == pmp_get_num_rules(env)) {
@@ -356,7 +362,7 @@ bool pmp_hart_has_privs(CPURISCVState *env, hwaddr addr,
      * 1.10 draft priv spec states there is an implicit order
      * from low to high
      */
-    for (i = 0; i < MAX_RISCV_PMPS; i++) {
+    for (i = 0; i < pmp_regions; i++) {
         s = pmp_is_in_range(env, i, addr);
         e = pmp_is_in_range(env, i, addr + pmp_size - 1);
 
@@ -527,8 +533,9 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
 {
     trace_pmpaddr_csr_write(env->mhartid, addr_index, val);
     bool is_next_cfg_tor = false;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
-    if (addr_index < MAX_RISCV_PMPS) {
+    if (addr_index < pmp_regions) {
         if (env->pmp_state.pmp[addr_index].addr_reg == val) {
             /* no change */
             return;
@@ -538,7 +545,7 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
          * In TOR mode, need to check the lock bit of the next pmp
          * (if there is a next).
          */
-        if (addr_index + 1 < MAX_RISCV_PMPS) {
+        if (addr_index + 1 < pmp_regions) {
             uint8_t pmp_cfg = env->pmp_state.pmp[addr_index + 1].cfg_reg;
             is_next_cfg_tor = PMP_AMATCH_TOR == pmp_get_a_field(pmp_cfg);
 
@@ -573,8 +580,9 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
 target_ulong pmpaddr_csr_read(CPURISCVState *env, uint32_t addr_index)
 {
     target_ulong val = 0;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
-    if (addr_index < MAX_RISCV_PMPS) {
+    if (addr_index < pmp_regions) {
         val = env->pmp_state.pmp[addr_index].addr_reg;
         trace_pmpaddr_csr_read(env->mhartid, addr_index, val);
     } else {
@@ -592,6 +600,7 @@ void mseccfg_csr_write(CPURISCVState *env, target_ulong val)
 {
     int i;
     uint64_t mask = MSECCFG_MMWP | MSECCFG_MML;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
     /* Update PMM field only if the value is valid according to Zjpm v1.0 */
     if (riscv_cpu_cfg(env)->ext_smmpm &&
         riscv_cpu_mxl(env) == MXL_RV64 &&
@@ -603,7 +612,7 @@ void mseccfg_csr_write(CPURISCVState *env, target_ulong val)
 
     /* RLB cannot be enabled if it's already 0 and if any regions are locked */
     if (!MSECCFG_RLB_ISSET(env)) {
-        for (i = 0; i < MAX_RISCV_PMPS; i++) {
+        for (i = 0; i < pmp_regions; i++) {
             if (pmp_is_locked(env, i)) {
                 val &= ~MSECCFG_RLB;
                 break;
@@ -659,6 +668,7 @@ target_ulong pmp_get_tlb_size(CPURISCVState *env, hwaddr addr)
     hwaddr tlb_sa = addr & ~(TARGET_PAGE_SIZE - 1);
     hwaddr tlb_ea = tlb_sa + TARGET_PAGE_SIZE - 1;
     int i;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
     /*
      * If PMP is not supported or there are no PMP rules, the TLB page will not
@@ -669,7 +679,7 @@ target_ulong pmp_get_tlb_size(CPURISCVState *env, hwaddr addr)
         return TARGET_PAGE_SIZE;
     }
 
-    for (i = 0; i < MAX_RISCV_PMPS; i++) {
+    for (i = 0; i < pmp_regions; i++) {
         if (pmp_get_a_field(env->pmp_state.pmp[i].cfg_reg) == PMP_AMATCH_OFF) {
             continue;
         }
Signature · 1 line
2.48.1
Reviewed-by on v4 2/2
#3 lore raw

Re: [PATCH v4 2/2] target/riscv: Make PMP region count configurable

Quoted 8 lines · Jay Chang

Previously, the number of PMP regions was hardcoded to 16 in QEMU. This patch replaces the fixed value with a new pmp_regions field, allowing platforms to configure the number of PMP regions.

If no specific value is provided, the default number of PMP regions remains 16 to preserve the existing behavior.

Quoted 7 lines

A new CPU parameter num-pmp-regions has been introduced to the QEMU command line. For example:

        -cpu rv64, g=true, c=true, pmp=true, num-pmp-regions=8

Alistair

Quoted 325 lines
---
 target/riscv/cpu.c                | 54 +++++++++++++++++++++++++++++--
 target/riscv/cpu.h                |  3 +-
 target/riscv/cpu_cfg_fields.h.inc |  1 +
 target/riscv/csr.c                |  5 ++-
 target/riscv/machine.c            |  3 +-
 target/riscv/pmp.c                | 28 ++++++++++------
 6 files changed, 80 insertions(+), 14 deletions(-)
target/riscv/cpu.c +52 -2
index 629ac37501..8e32252c11 100644
@@ -1117,6 +1117,7 @@ static void riscv_cpu_init(Object *obj)
     cpu->cfg.cbom_blocksize = 64;
     cpu->cfg.cbop_blocksize = 64;
     cpu->cfg.cboz_blocksize = 64;
+    cpu->cfg.pmp_regions = 16;
     cpu->env.vext_ver = VEXT_VERSION_1_00_0;
     cpu->cfg.max_satp_mode = -1;
 
@@ -1568,6 +1569,46 @@ static const PropertyInfo prop_pmp = {
     .set = prop_pmp_set,
 };
 
+static void prop_num_pmp_regions_set(Object *obj, Visitor *v, const char *name,
+                                     void *opaque, Error **errp)
+{
+    RISCVCPU *cpu = RISCV_CPU(obj);
+    uint8_t value;
+
+    visit_type_uint8(v, name, &value, errp);
+
+    if (cpu->cfg.pmp_regions != value && riscv_cpu_is_vendor(obj)) {
+        cpu_set_prop_err(cpu, name, errp);
+        return;
+    }
+
+    if (cpu->env.priv_ver < PRIV_VERSION_1_12_0 && value > OLD_MAX_RISCV_PMPS) {
+        error_setg(errp, "Number of PMP regions exceeds maximum available");
+        return;
+    } else if (value > MAX_RISCV_PMPS) {
+        error_setg(errp, "Number of PMP regions exceeds maximum available");
+        return;
+    }
+
+    cpu_option_add_user_setting(name, value);
+    cpu->cfg.pmp_regions = value;
+}
+
+static void prop_num_pmp_regions_get(Object *obj, Visitor *v, const char *name,
+                                     void *opaque, Error **errp)
+{
+    uint8_t value = RISCV_CPU(obj)->cfg.pmp_regions;
+
+    visit_type_uint8(v, name, &value, errp);
+}
+
+static const PropertyInfo prop_num_pmp_regions = {
+    .type = "uint8",
+    .description = "num-pmp-regions",
+    .get = prop_num_pmp_regions_get,
+    .set = prop_num_pmp_regions_set,
+};
+
 static int priv_spec_from_str(const char *priv_spec_str)
 {
     int priv_version = -1;
@@ -2567,6 +2608,7 @@ static const Property riscv_cpu_properties[] = {
 
     {.name = "mmu", .info = &prop_mmu},
     {.name = "pmp", .info = &prop_pmp},
+    {.name = "num-pmp-regions", .info = &prop_num_pmp_regions},
 
     {.name = "priv_spec", .info = &prop_priv_spec},
     {.name = "vext_spec", .info = &prop_vext_spec},
@@ -2891,6 +2933,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
     DEFINE_ABSTRACT_RISCV_CPU(TYPE_RISCV_DYNAMIC_CPU, TYPE_RISCV_CPU,
         .cfg.mmu = true,
         .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
         .priv_spec = PRIV_VERSION_LATEST,
     ),
 
@@ -2937,7 +2980,8 @@ static const TypeInfo riscv_cpu_type_infos[] = {
         .cfg.max_satp_mode = VM_1_10_MBARE,
         .cfg.ext_zifencei = true,
         .cfg.ext_zicsr = true,
-        .cfg.pmp = true
+        .cfg.pmp = true,
+        .cfg.pmp_regions = 8
     ),
 
     DEFINE_ABSTRACT_RISCV_CPU(TYPE_RISCV_CPU_SIFIVE_U, TYPE_RISCV_VENDOR_CPU,
@@ -2948,7 +2992,8 @@ static const TypeInfo riscv_cpu_type_infos[] = {
         .cfg.ext_zifencei = true,
         .cfg.ext_zicsr = true,
         .cfg.mmu = true,
-        .cfg.pmp = true
+        .cfg.pmp = true,
+        .cfg.pmp_regions = 8
     ),
 
 #if defined(TARGET_RISCV32) || \
@@ -2966,6 +3011,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
         .cfg.ext_zifencei = true,
         .cfg.ext_zicsr = true,
         .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
         .cfg.ext_smepmp = true,
 
         .cfg.ext_zba = true,
@@ -3040,6 +3086,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
         .cfg.ext_xtheadmempair = true,
         .cfg.ext_xtheadsync = true,
         .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
 
         .cfg.mvendorid = THEAD_VENDOR_ID,
 
@@ -3063,6 +3110,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
         .cfg.rvv_ta_all_1s = true,
         .cfg.misa_w = true,
         .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
         .cfg.cbom_blocksize = 64,
         .cfg.cbop_blocksize = 64,
         .cfg.cboz_blocksize = 64,
@@ -3119,6 +3167,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
         .cfg.ext_zifencei = true,
         .cfg.ext_zicsr = true,
         .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
         .cfg.ext_zicbom = true,
         .cfg.cbom_blocksize = 64,
         .cfg.cboz_blocksize = 64,
@@ -3163,6 +3212,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
 
         .cfg.mmu = true,
         .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
 
         .cfg.max_satp_mode = VM_1_10_SV39,
     ),
target/riscv/cpu.h +2 -1
index 229ade9ed9..67323a7d9d 100644
@@ -159,7 +159,8 @@ extern RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[];
 
 #define MMU_USER_IDX 3
 
-#define MAX_RISCV_PMPS (16)
+#define MAX_RISCV_PMPS (64)
+#define OLD_MAX_RISCV_PMPS (16)
 
 #if !defined(CONFIG_USER_ONLY)
 #include "pmp.h"
target/riscv/cpu_cfg_fields.h.inc +1 -0
index 59f134a419..33c4f9bac8 100644
@@ -163,6 +163,7 @@ TYPED_FIELD(uint16_t, elen, 0)
 TYPED_FIELD(uint16_t, cbom_blocksize, 0)
 TYPED_FIELD(uint16_t, cbop_blocksize, 0)
 TYPED_FIELD(uint16_t, cboz_blocksize, 0)
+TYPED_FIELD(uint8_t,  pmp_regions, 0)
 
 TYPED_FIELD(int8_t, max_satp_mode, -1)
 
target/riscv/csr.c +4 -1
index d6cd441133..6296ecd1e1 100644
@@ -738,7 +738,10 @@ static RISCVException dbltrp_hmode(CPURISCVState *env, int csrno)
 static RISCVException pmp(CPURISCVState *env, int csrno)
 {
     if (riscv_cpu_cfg(env)->pmp) {
-        if (csrno <= CSR_PMPCFG3) {
+        int max_pmpcfg = (env->priv_ver >= PRIV_VERSION_1_12_0) ?
++                              CSR_PMPCFG15 : CSR_PMPCFG3;
+
+        if (csrno <= max_pmpcfg) {
             uint32_t reg_index = csrno - CSR_PMPCFG0;
 
             /* TODO: RV128 restriction check */
target/riscv/machine.c +2 -1
index c97e9ce9df..1600ec44f0 100644
@@ -36,8 +36,9 @@ static int pmp_post_load(void *opaque, int version_id)
     RISCVCPU *cpu = opaque;
     CPURISCVState *env = &cpu->env;
     int i;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
-    for (i = 0; i < MAX_RISCV_PMPS; i++) {
+    for (i = 0; i < pmp_regions; i++) {
         pmp_update_rule_addr(env, i);
     }
     pmp_update_rule_nums(env);
target/riscv/pmp.c +19 -10
index 5af295e410..3540327c9a 100644
@@ -122,7 +122,9 @@ uint32_t pmp_get_num_rules(CPURISCVState *env)
  */
 static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index)
 {
-    if (pmp_index < MAX_RISCV_PMPS) {
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
+
+    if (pmp_index < pmp_regions) {
         return env->pmp_state.pmp[pmp_index].cfg_reg;
     }
 
@@ -136,7 +138,9 @@ static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index)
  */
 static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)
 {
-    if (pmp_index < MAX_RISCV_PMPS) {
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
+
+    if (pmp_index < pmp_regions) {
         if (env->pmp_state.pmp[pmp_index].cfg_reg == val) {
             /* no change */
             return false;
@@ -236,9 +240,10 @@ void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index)
 void pmp_update_rule_nums(CPURISCVState *env)
 {
     int i;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
     env->pmp_state.num_rules = 0;
-    for (i = 0; i < MAX_RISCV_PMPS; i++) {
+    for (i = 0; i < pmp_regions; i++) {
         const uint8_t a_field =
             pmp_get_a_field(env->pmp_state.pmp[i].cfg_reg);
         if (PMP_AMATCH_OFF != a_field) {
@@ -332,6 +337,7 @@ bool pmp_hart_has_privs(CPURISCVState *env, hwaddr addr,
     int pmp_size = 0;
     hwaddr s = 0;
     hwaddr e = 0;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
     /* Short cut if no rules */
     if (0 == pmp_get_num_rules(env)) {
@@ -356,7 +362,7 @@ bool pmp_hart_has_privs(CPURISCVState *env, hwaddr addr,
      * 1.10 draft priv spec states there is an implicit order
      * from low to high
      */
-    for (i = 0; i < MAX_RISCV_PMPS; i++) {
+    for (i = 0; i < pmp_regions; i++) {
         s = pmp_is_in_range(env, i, addr);
         e = pmp_is_in_range(env, i, addr + pmp_size - 1);
 
@@ -527,8 +533,9 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
 {
     trace_pmpaddr_csr_write(env->mhartid, addr_index, val);
     bool is_next_cfg_tor = false;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
-    if (addr_index < MAX_RISCV_PMPS) {
+    if (addr_index < pmp_regions) {
         if (env->pmp_state.pmp[addr_index].addr_reg == val) {
             /* no change */
             return;
@@ -538,7 +545,7 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
          * In TOR mode, need to check the lock bit of the next pmp
          * (if there is a next).
          */
-        if (addr_index + 1 < MAX_RISCV_PMPS) {
+        if (addr_index + 1 < pmp_regions) {
             uint8_t pmp_cfg = env->pmp_state.pmp[addr_index + 1].cfg_reg;
             is_next_cfg_tor = PMP_AMATCH_TOR == pmp_get_a_field(pmp_cfg);
 
@@ -573,8 +580,9 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
 target_ulong pmpaddr_csr_read(CPURISCVState *env, uint32_t addr_index)
 {
     target_ulong val = 0;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
-    if (addr_index < MAX_RISCV_PMPS) {
+    if (addr_index < pmp_regions) {
         val = env->pmp_state.pmp[addr_index].addr_reg;
         trace_pmpaddr_csr_read(env->mhartid, addr_index, val);
     } else {
@@ -592,6 +600,7 @@ void mseccfg_csr_write(CPURISCVState *env, target_ulong val)
 {
     int i;
     uint64_t mask = MSECCFG_MMWP | MSECCFG_MML;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
     /* Update PMM field only if the value is valid according to Zjpm v1.0 */
     if (riscv_cpu_cfg(env)->ext_smmpm &&
         riscv_cpu_mxl(env) == MXL_RV64 &&
@@ -603,7 +612,7 @@ void mseccfg_csr_write(CPURISCVState *env, target_ulong val)
 
     /* RLB cannot be enabled if it's already 0 and if any regions are locked */
     if (!MSECCFG_RLB_ISSET(env)) {
-        for (i = 0; i < MAX_RISCV_PMPS; i++) {
+        for (i = 0; i < pmp_regions; i++) {
             if (pmp_is_locked(env, i)) {
                 val &= ~MSECCFG_RLB;
                 break;
@@ -659,6 +668,7 @@ target_ulong pmp_get_tlb_size(CPURISCVState *env, hwaddr addr)
     hwaddr tlb_sa = addr & ~(TARGET_PAGE_SIZE - 1);
     hwaddr tlb_ea = tlb_sa + TARGET_PAGE_SIZE - 1;
     int i;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
     /*
      * If PMP is not supported or there are no PMP rules, the TLB page will not
@@ -669,7 +679,7 @@ target_ulong pmp_get_tlb_size(CPURISCVState *env, hwaddr addr)
         return TARGET_PAGE_SIZE;
     }
 
-    for (i = 0; i < MAX_RISCV_PMPS; i++) {
+    for (i = 0; i < pmp_regions; i++) {
         if (pmp_get_a_field(env->pmp_state.pmp[i].cfg_reg) == PMP_AMATCH_OFF) {
             continue;
         }
--

2.48.1

#4 lore raw

Re: [PATCH v4 2/2] target/riscv: Make PMP region count configurable

In reply to Jay Chang

It seems like this patch is breaking 'make check-functional':

Quoted 1 line
Quoted 1 line
Quoted 1 line

UBSAN_OPTIONS=halt_on_error=1:abort_on_error=1:print_summary=1:print_stacktrace=1 RUST_BACKTRACE=1 LD_LIBRARY_PATH=/home/danielhb/work/qemu/build/contrib/plugins:/home/danielhb/work/qemu/build/tests/tcg/plugins MSAN_OPTIONS=halt_on_error=1:abort_on_error=1:print_summary=1:print_stacktrace=1 QEMU_BUILD_ROOT=/home/danielhb/work/qemu/build QEMU_TEST_QEMU_IMG=/home/danielhb/work/qemu/build/qemu-img PYTHONPATH=/home/danielhb/work/qemu/python:/home/danielhb/work/qemu/tests/functional G_TEST_SLOW=1 ASAN_OPTIONS=halt_on_error=1:abort_on_error=1:print_summary=1 MALLOC_PERTURB_=229 MESON_TEST_ITERATION=1 QEMU_TEST_QEMU_BINARY=/home/danielhb/work/qemu/build/qemu-system-riscv64 /home/danielhb/work/qemu/build/pyvenv/bin/python3 /home/danielhb/work/qemu/tests/functional/test_riscv64_tuxrun.py

Summary of Failures:

11/12 qemu:func-thorough+func-riscv32-thorough+thorough / func-riscv32-riscv32_tuxrun TIMEOUT 90.02s killed by signal 15 SIGTERM
12/12 qemu:func-thorough+func-riscv64-thorough+thorough / func-riscv64-riscv64_tuxrun TIMEOUT 120.13s killed by signal 15 SIGTERM

Going through the code:

Quoted 28 lines · Jay Chang

Previously, the number of PMP regions was hardcoded to 16 in QEMU. This patch replaces the fixed value with a new pmp_regions field, allowing platforms to configure the number of PMP regions.

If no specific value is provided, the default number of PMP regions remains 16 to preserve the existing behavior.

A new CPU parameter num-pmp-regions has been introduced to the QEMU command line. For example:

	-cpu rv64, g=true, c=true, pmp=true, num-pmp-regions=8
---
  target/riscv/cpu.c                | 54 +++++++++++++++++++++++++++++--
  target/riscv/cpu.h                |  3 +-
  target/riscv/cpu_cfg_fields.h.inc |  1 +
  target/riscv/csr.c                |  5 ++-
  target/riscv/machine.c            |  3 +-
  target/riscv/pmp.c                | 28 ++++++++++------
  6 files changed, 80 insertions(+), 14 deletions(-)
target/riscv/cpu.c +0 -0
index 629ac37501..8e32252c11 100644

target/riscv/cpu.c static void riscv_cpu_init(Object *obj) · line 1120

Quoted 4 lines
target/riscv/cpu.c +1 -0
@@ -1117,6 +1117,7 @@ static void riscv_cpu_init(Object *obj)
      cpu->cfg.cbom_blocksize = 64;
      cpu->cfg.cbop_blocksize = 64;
      cpu->cfg.cboz_blocksize = 64;
+    cpu->cfg.pmp_regions = 16;

pmp_regions is set to 16 in the base class, which is what we want to keep the default intact. But then:

Quoted 67 lines
target/riscv/cpu.c +42 -0
      cpu->env.vext_ver = VEXT_VERSION_1_00_0;
      cpu->cfg.max_satp_mode = -1;
 
@@ -1568,6 +1569,46 @@ static const PropertyInfo prop_pmp = {
      .set = prop_pmp_set,
  };
 
+static void prop_num_pmp_regions_set(Object *obj, Visitor *v, const char *name,
+                                     void *opaque, Error **errp)
+{
+    RISCVCPU *cpu = RISCV_CPU(obj);
+    uint8_t value;
+
+    visit_type_uint8(v, name, &value, errp);
+
+    if (cpu->cfg.pmp_regions != value && riscv_cpu_is_vendor(obj)) {
+        cpu_set_prop_err(cpu, name, errp);
+        return;
+    }
+
+    if (cpu->env.priv_ver < PRIV_VERSION_1_12_0 && value > OLD_MAX_RISCV_PMPS) {
+        error_setg(errp, "Number of PMP regions exceeds maximum available");
+        return;
+    } else if (value > MAX_RISCV_PMPS) {
+        error_setg(errp, "Number of PMP regions exceeds maximum available");
+        return;
+    }
+
+    cpu_option_add_user_setting(name, value);
+    cpu->cfg.pmp_regions = value;
+}
+
+static void prop_num_pmp_regions_get(Object *obj, Visitor *v, const char *name,
+                                     void *opaque, Error **errp)
+{
+    uint8_t value = RISCV_CPU(obj)->cfg.pmp_regions;
+
+    visit_type_uint8(v, name, &value, errp);
+}
+
+static const PropertyInfo prop_num_pmp_regions = {
+    .type = "uint8",
+    .description = "num-pmp-regions",
+    .get = prop_num_pmp_regions_get,
+    .set = prop_num_pmp_regions_set,
+};
+
  static int priv_spec_from_str(const char *priv_spec_str)
  {
      int priv_version = -1;
@@ -2567,6 +2608,7 @@ static const Property riscv_cpu_properties[] = {
 
      {.name = "mmu", .info = &prop_mmu},
      {.name = "pmp", .info = &prop_pmp},
+    {.name = "num-pmp-regions", .info = &prop_num_pmp_regions},
 
      {.name = "priv_spec", .info = &prop_priv_spec},
      {.name = "vext_spec", .info = &prop_vext_spec},
@@ -2891,6 +2933,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
      DEFINE_ABSTRACT_RISCV_CPU(TYPE_RISCV_DYNAMIC_CPU, TYPE_RISCV_CPU,
          .cfg.mmu = true,
          .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
          .priv_spec = PRIV_VERSION_LATEST,
      ),
 

target/riscv/cpu.c static const TypeInfo riscv_cpu_type_infos[] = { · line 2984

Quoted 6 lines
target/riscv/cpu.c +2 -1
@@ -2937,7 +2980,8 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.max_satp_mode = VM_1_10_MBARE,
          .cfg.ext_zifencei = true,
          .cfg.ext_zicsr = true,
-        .cfg.pmp = true
+        .cfg.pmp = true,
+        .cfg.pmp_regions = 8

We can't set pmp_regions = 8 in the CPU definitions too. This will overwrite the pmp_regions = 16 from the parent object. In short, we're changing the default val for a lot of CPUs.

This diff fixes the test:

target/riscv/cpu.c +0 -6
index d49c02dfe4..b84f08e582 100644
@@ -2933,7 +2933,6 @@ static const TypeInfo riscv_cpu_type_infos[] = {
     DEFINE_ABSTRACT_RISCV_CPU(TYPE_RISCV_DYNAMIC_CPU, TYPE_RISCV_CPU,
         .cfg.mmu = true,
         .cfg.pmp = true,
-        .cfg.pmp_regions = 8,
         .priv_spec = PRIV_VERSION_LATEST,
     ),
 
@@ -3011,7 +3010,6 @@ static const TypeInfo riscv_cpu_type_infos[] = {
         .cfg.ext_zifencei = true,
         .cfg.ext_zicsr = true,
         .cfg.pmp = true,
-        .cfg.pmp_regions = 8,
         .cfg.ext_smepmp = true,
 
         .cfg.ext_zba = true,
@@ -3086,7 +3084,6 @@ static const TypeInfo riscv_cpu_type_infos[] = {
         .cfg.ext_xtheadmempair = true,
         .cfg.ext_xtheadsync = true,
         .cfg.pmp = true,
-        .cfg.pmp_regions = 8,
 
         .cfg.mvendorid = THEAD_VENDOR_ID,
 
@@ -3110,7 +3107,6 @@ static const TypeInfo riscv_cpu_type_infos[] = {
         .cfg.rvv_ta_all_1s = true,
         .cfg.misa_w = true,
         .cfg.pmp = true,
-        .cfg.pmp_regions = 8,
         .cfg.cbom_blocksize = 64,
         .cfg.cbop_blocksize = 64,
         .cfg.cboz_blocksize = 64,
@@ -3167,7 +3163,6 @@ static const TypeInfo riscv_cpu_type_infos[] = {
         .cfg.ext_zifencei = true,
         .cfg.ext_zicsr = true,
         .cfg.pmp = true,
-        .cfg.pmp_regions = 8,
         .cfg.ext_zicbom = true,
         .cfg.cbom_blocksize = 64,
         .cfg.cboz_blocksize = 64,
@@ -3212,7 +3207,6 @@ static const TypeInfo riscv_cpu_type_infos[] = {
 
         .cfg.mmu = true,
         .cfg.pmp = true,
-        .cfg.pmp_regions = 8,
 
         .cfg.max_satp_mode = VM_1_10_SV39,
     ),

I removed all "pmp_regions = 8" assignments but the Si-five ones. The generic CPUs should retain the default pmp regions value, and every other vendor CPU should set its own default if they want.

Thanks,

Daniel

Quoted 230 lines
target/riscv/cpu.c +7 -1
      ),
 
      DEFINE_ABSTRACT_RISCV_CPU(TYPE_RISCV_CPU_SIFIVE_U, TYPE_RISCV_VENDOR_CPU,
@@ -2948,7 +2992,8 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.ext_zifencei = true,
          .cfg.ext_zicsr = true,
          .cfg.mmu = true,
-        .cfg.pmp = true
+        .cfg.pmp = true,
+        .cfg.pmp_regions = 8
      ),
 
  #if defined(TARGET_RISCV32) || \
@@ -2966,6 +3011,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.ext_zifencei = true,
          .cfg.ext_zicsr = true,
          .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
          .cfg.ext_smepmp = true,
 
          .cfg.ext_zba = true,
@@ -3040,6 +3086,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.ext_xtheadmempair = true,
          .cfg.ext_xtheadsync = true,
          .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
 
          .cfg.mvendorid = THEAD_VENDOR_ID,
 
@@ -3063,6 +3110,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.rvv_ta_all_1s = true,
          .cfg.misa_w = true,
          .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
          .cfg.cbom_blocksize = 64,
          .cfg.cbop_blocksize = 64,
          .cfg.cboz_blocksize = 64,
@@ -3119,6 +3167,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.ext_zifencei = true,
          .cfg.ext_zicsr = true,
          .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
          .cfg.ext_zicbom = true,
          .cfg.cbom_blocksize = 64,
          .cfg.cboz_blocksize = 64,
@@ -3163,6 +3212,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
 
          .cfg.mmu = true,
          .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
 
          .cfg.max_satp_mode = VM_1_10_SV39,
      ),
target/riscv/cpu.h +2 -1
index 229ade9ed9..67323a7d9d 100644
@@ -159,7 +159,8 @@ extern RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[];
 
  #define MMU_USER_IDX 3
 
-#define MAX_RISCV_PMPS (16)
+#define MAX_RISCV_PMPS (64)
+#define OLD_MAX_RISCV_PMPS (16)
 
  #if !defined(CONFIG_USER_ONLY)
  #include "pmp.h"
target/riscv/cpu_cfg_fields.h.inc +1 -0
index 59f134a419..33c4f9bac8 100644
@@ -163,6 +163,7 @@ TYPED_FIELD(uint16_t, elen, 0)
  TYPED_FIELD(uint16_t, cbom_blocksize, 0)
  TYPED_FIELD(uint16_t, cbop_blocksize, 0)
  TYPED_FIELD(uint16_t, cboz_blocksize, 0)
+TYPED_FIELD(uint8_t,  pmp_regions, 0)
 
  TYPED_FIELD(int8_t, max_satp_mode, -1)
 
target/riscv/csr.c +4 -1
index d6cd441133..6296ecd1e1 100644
@@ -738,7 +738,10 @@ static RISCVException dbltrp_hmode(CPURISCVState *env, int csrno)
  static RISCVException pmp(CPURISCVState *env, int csrno)
  {
      if (riscv_cpu_cfg(env)->pmp) {
-        if (csrno <= CSR_PMPCFG3) {
+        int max_pmpcfg = (env->priv_ver >= PRIV_VERSION_1_12_0) ?
++                              CSR_PMPCFG15 : CSR_PMPCFG3;
+
+        if (csrno <= max_pmpcfg) {
              uint32_t reg_index = csrno - CSR_PMPCFG0;
 
              /* TODO: RV128 restriction check */
target/riscv/machine.c +2 -1
index c97e9ce9df..1600ec44f0 100644
@@ -36,8 +36,9 @@ static int pmp_post_load(void *opaque, int version_id)
      RISCVCPU *cpu = opaque;
      CPURISCVState *env = &cpu->env;
      int i;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
-    for (i = 0; i < MAX_RISCV_PMPS; i++) {
+    for (i = 0; i < pmp_regions; i++) {
          pmp_update_rule_addr(env, i);
      }
      pmp_update_rule_nums(env);
target/riscv/pmp.c +19 -9
index 5af295e410..3540327c9a 100644
@@ -122,7 +122,9 @@ uint32_t pmp_get_num_rules(CPURISCVState *env)
   */
  static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index)
  {
-    if (pmp_index < MAX_RISCV_PMPS) {
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
+
+    if (pmp_index < pmp_regions) {
          return env->pmp_state.pmp[pmp_index].cfg_reg;
      }
 
@@ -136,7 +138,9 @@ static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index)
   */
  static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)
  {
-    if (pmp_index < MAX_RISCV_PMPS) {
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
+
+    if (pmp_index < pmp_regions) {
          if (env->pmp_state.pmp[pmp_index].cfg_reg == val) {
              /* no change */
              return false;
@@ -236,9 +240,10 @@ void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index)
  void pmp_update_rule_nums(CPURISCVState *env)
  {
      int i;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
      env->pmp_state.num_rules = 0;
-    for (i = 0; i < MAX_RISCV_PMPS; i++) {
+    for (i = 0; i < pmp_regions; i++) {
          const uint8_t a_field =
              pmp_get_a_field(env->pmp_state.pmp[i].cfg_reg);
          if (PMP_AMATCH_OFF != a_field) {
@@ -332,6 +337,7 @@ bool pmp_hart_has_privs(CPURISCVState *env, hwaddr addr,
      int pmp_size = 0;
      hwaddr s = 0;
      hwaddr e = 0;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
      /* Short cut if no rules */
      if (0 == pmp_get_num_rules(env)) {
@@ -356,7 +362,7 @@ bool pmp_hart_has_privs(CPURISCVState *env, hwaddr addr,
       * 1.10 draft priv spec states there is an implicit order
       * from low to high
       */
-    for (i = 0; i < MAX_RISCV_PMPS; i++) {
+    for (i = 0; i < pmp_regions; i++) {
          s = pmp_is_in_range(env, i, addr);
          e = pmp_is_in_range(env, i, addr + pmp_size - 1);
 
@@ -527,8 +533,9 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
  {
      trace_pmpaddr_csr_write(env->mhartid, addr_index, val);
      bool is_next_cfg_tor = false;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
-    if (addr_index < MAX_RISCV_PMPS) {
+    if (addr_index < pmp_regions) {
          if (env->pmp_state.pmp[addr_index].addr_reg == val) {
              /* no change */
              return;
@@ -538,7 +545,7 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
           * In TOR mode, need to check the lock bit of the next pmp
           * (if there is a next).
           */
-        if (addr_index + 1 < MAX_RISCV_PMPS) {
+        if (addr_index + 1 < pmp_regions) {
              uint8_t pmp_cfg = env->pmp_state.pmp[addr_index + 1].cfg_reg;
              is_next_cfg_tor = PMP_AMATCH_TOR == pmp_get_a_field(pmp_cfg);
 
@@ -573,8 +580,9 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
  target_ulong pmpaddr_csr_read(CPURISCVState *env, uint32_t addr_index)
  {
      target_ulong val = 0;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
-    if (addr_index < MAX_RISCV_PMPS) {
+    if (addr_index < pmp_regions) {
          val = env->pmp_state.pmp[addr_index].addr_reg;
          trace_pmpaddr_csr_read(env->mhartid, addr_index, val);
      } else {
@@ -592,6 +600,7 @@ void mseccfg_csr_write(CPURISCVState *env, target_ulong val)
  {
      int i;
      uint64_t mask = MSECCFG_MMWP | MSECCFG_MML;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
      /* Update PMM field only if the value is valid according to Zjpm v1.0 */
      if (riscv_cpu_cfg(env)->ext_smmpm &&
          riscv_cpu_mxl(env) == MXL_RV64 &&
@@ -603,7 +612,7 @@ void mseccfg_csr_write(CPURISCVState *env, target_ulong val)
 
      /* RLB cannot be enabled if it's already 0 and if any regions are locked */
      if (!MSECCFG_RLB_ISSET(env)) {
-        for (i = 0; i < MAX_RISCV_PMPS; i++) {
+        for (i = 0; i < pmp_regions; i++) {
              if (pmp_is_locked(env, i)) {
                  val &= ~MSECCFG_RLB;
                  break;
@@ -659,6 +668,7 @@ target_ulong pmp_get_tlb_size(CPURISCVState *env, hwaddr addr)
      hwaddr tlb_sa = addr & ~(TARGET_PAGE_SIZE - 1);
      hwaddr tlb_ea = tlb_sa + TARGET_PAGE_SIZE - 1;
      int i;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
      /*
       * If PMP is not supported or there are no PMP rules, the TLB page will not
@@ -669,7 +679,7 @@ target_ulong pmp_get_tlb_size(CPURISCVState *env, hwaddr addr)
          return TARGET_PAGE_SIZE;
      }
 
-    for (i = 0; i < MAX_RISCV_PMPS; i++) {
+    for (i = 0; i < pmp_regions; i++) {
          if (pmp_get_a_field(env->pmp_state.pmp[i].cfg_reg) == PMP_AMATCH_OFF) {
              continue;
          }
author
#5 lore raw

Re: [PATCH v4 2/2] target/riscv: Make PMP region count configurable

*Hi Daniel,*

You're absolutely right — thanks for pointing it out and providing the fix!

Would you like me to send out a v5 patch incorporating your changes?

Best Regards
Jay Chang

Quoted 477 lines · Daniel Henrique Barboza

It seems like this patch is breaking 'make check-functional':

Quoted 1 line
Quoted 1 line
Quoted 1 line

UBSAN_OPTIONS=halt_on_error=1:abort_on_error=1:print_summary=1:print_stacktrace=1 RUST_BACKTRACE=1 LD_LIBRARY_PATH=/home/danielhb/work/qemu/build/contrib/plugins:/home/danielhb/work/qemu/build/tests/tcg/plugins MSAN_OPTIONS=halt_on_error=1:abort_on_error=1:print_summary=1:print_stacktrace=1 QEMU_BUILD_ROOT=/home/danielhb/work/qemu/build QEMU_TEST_QEMU_IMG=/home/danielhb/work/qemu/build/qemu-img PYTHONPATH=/home/danielhb/work/qemu/python:/home/danielhb/work/qemu/tests/functional G_TEST_SLOW=1 ASAN_OPTIONS=halt_on_error=1:abort_on_error=1:print_summary=1 MALLOC_PERTURB_=229 MESON_TEST_ITERATION=1 QEMU_TEST_QEMU_BINARY=/home/danielhb/work/qemu/build/qemu-system-riscv64 /home/danielhb/work/qemu/build/pyvenv/bin/python3 /home/danielhb/work/qemu/tests/functional/test_riscv64_tuxrun.py

Summary of Failures:

11/12 qemu:func-thorough+func-riscv32-thorough+thorough /
func-riscv32-riscv32_tuxrun TIMEOUT 90.02s killed by signal 15
SIGTERM
12/12 qemu:func-thorough+func-riscv64-thorough+thorough /
func-riscv64-riscv64_tuxrun TIMEOUT 120.13s killed by signal 15
SIGTERM

Going through the code:

Quoted 28 lines · Jay Chang

Previously, the number of PMP regions was hardcoded to 16 in QEMU. This patch replaces the fixed value with a new pmp_regions field, allowing platforms to configure the number of PMP regions.

If no specific value is provided, the default number of PMP regions remains 16 to preserve the existing behavior.

A new CPU parameter num-pmp-regions has been introduced to the QEMU command line. For example:

      -cpu rv64, g=true, c=true, pmp=true, num-pmp-regions=8
---
  target/riscv/cpu.c                | 54 +++++++++++++++++++++++++++++--
  target/riscv/cpu.h                |  3 +-
  target/riscv/cpu_cfg_fields.h.inc |  1 +
  target/riscv/csr.c                |  5 ++-
  target/riscv/machine.c            |  3 +-
  target/riscv/pmp.c                | 28 ++++++++++------
  6 files changed, 80 insertions(+), 14 deletions(-)
target/riscv/cpu.c +0 -0
index 629ac37501..8e32252c11 100644

target/riscv/cpu.c static void riscv_cpu_init(Object *obj) · line 1120

Quoted 4 lines
target/riscv/cpu.c +1 -0
@@ -1117,6 +1117,7 @@ static void riscv_cpu_init(Object *obj)
      cpu->cfg.cbom_blocksize = 64;
      cpu->cfg.cbop_blocksize = 64;
      cpu->cfg.cboz_blocksize = 64;
+    cpu->cfg.pmp_regions = 16;

pmp_regions is set to 16 in the base class, which is what we want to keep the default intact. But then:

target/riscv/cpu.c static const PropertyInfo prop_pmp = { · line 1572

Quoted 8 lines
target/riscv/cpu.c +1 -0
      cpu->env.vext_ver = VEXT_VERSION_1_00_0;
      cpu->cfg.max_satp_mode = -1;
 
@@ -1568,6 +1569,46 @@ static const PropertyInfo prop_pmp = {
      .set = prop_pmp_set,
  };
 
+static void prop_num_pmp_regions_set(Object *obj, Visitor *v, const

char *name,

target/riscv/cpu.c static const PropertyInfo prop_pmp = { · line 1585

Quoted 13 lines
target/riscv/cpu.c +13 -0
+                                     void *opaque, Error **errp)
+{
+    RISCVCPU *cpu = RISCV_CPU(obj);
+    uint8_t value;
+
+    visit_type_uint8(v, name, &value, errp);
+
+    if (cpu->cfg.pmp_regions != value && riscv_cpu_is_vendor(obj)) {
+        cpu_set_prop_err(cpu, name, errp);
+        return;
+    }
+
+    if (cpu->env.priv_ver < PRIV_VERSION_1_12_0 && value >

OLD_MAX_RISCV_PMPS) {

target/riscv/cpu.c static const PropertyInfo prop_pmp = { · line 1586

Quoted 1 line
target/riscv/cpu.c +1 -0
+        error_setg(errp, "Number of PMP regions exceeds maximum

available");

target/riscv/cpu.c static const PropertyInfo prop_pmp = { · line 1589

Quoted 3 lines
target/riscv/cpu.c +3 -0
+        return;
+    } else if (value > MAX_RISCV_PMPS) {
+        error_setg(errp, "Number of PMP regions exceeds maximum

available");

target/riscv/cpu.c static const PropertyInfo prop_pmp = { · line 1597

Quoted 8 lines
target/riscv/cpu.c +8 -0
+        return;
+    }
+
+    cpu_option_add_user_setting(name, value);
+    cpu->cfg.pmp_regions = value;
+}
+
+static void prop_num_pmp_regions_get(Object *obj, Visitor *v, const

char *name,

Quoted 34 lines
target/riscv/cpu.c +16 -0
+                                     void *opaque, Error **errp)
+{
+    uint8_t value = RISCV_CPU(obj)->cfg.pmp_regions;
+
+    visit_type_uint8(v, name, &value, errp);
+}
+
+static const PropertyInfo prop_num_pmp_regions = {
+    .type = "uint8",
+    .description = "num-pmp-regions",
+    .get = prop_num_pmp_regions_get,
+    .set = prop_num_pmp_regions_set,
+};
+
  static int priv_spec_from_str(const char *priv_spec_str)
  {
      int priv_version = -1;
@@ -2567,6 +2608,7 @@ static const Property riscv_cpu_properties[] = {
 
      {.name = "mmu", .info = &prop_mmu},
      {.name = "pmp", .info = &prop_pmp},
+    {.name = "num-pmp-regions", .info = &prop_num_pmp_regions},
 
      {.name = "priv_spec", .info = &prop_priv_spec},
      {.name = "vext_spec", .info = &prop_vext_spec},
@@ -2891,6 +2933,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
      DEFINE_ABSTRACT_RISCV_CPU(TYPE_RISCV_DYNAMIC_CPU, TYPE_RISCV_CPU,
          .cfg.mmu = true,
          .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
          .priv_spec = PRIV_VERSION_LATEST,
      ),
 

target/riscv/cpu.c static const TypeInfo riscv_cpu_type_infos[] = { · line 2984

Quoted 6 lines
target/riscv/cpu.c +2 -1
@@ -2937,7 +2980,8 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.max_satp_mode = VM_1_10_MBARE,
          .cfg.ext_zifencei = true,
          .cfg.ext_zicsr = true,
-        .cfg.pmp = true
+        .cfg.pmp = true,
+        .cfg.pmp_regions = 8

We can't set pmp_regions = 8 in the CPU definitions too. This will overwrite the
pmp_regions = 16 from the parent object. In short, we're changing the default
val for a lot of CPUs.

This diff fixes the test:

target/riscv/cpu.c +0 -6
index d49c02dfe4..b84f08e582 100644
@@ -2933,7 +2933,6 @@ static const TypeInfo riscv_cpu_type_infos[] = {
      DEFINE_ABSTRACT_RISCV_CPU(TYPE_RISCV_DYNAMIC_CPU, TYPE_RISCV_CPU,
          .cfg.mmu = true,
          .cfg.pmp = true,
-        .cfg.pmp_regions = 8,
          .priv_spec = PRIV_VERSION_LATEST,
      ),
 
@@ -3011,7 +3010,6 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.ext_zifencei = true,
          .cfg.ext_zicsr = true,
          .cfg.pmp = true,
-        .cfg.pmp_regions = 8,
          .cfg.ext_smepmp = true,
 
          .cfg.ext_zba = true,
@@ -3086,7 +3084,6 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.ext_xtheadmempair = true,
          .cfg.ext_xtheadsync = true,
          .cfg.pmp = true,
-        .cfg.pmp_regions = 8,
 
          .cfg.mvendorid = THEAD_VENDOR_ID,
 
@@ -3110,7 +3107,6 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.rvv_ta_all_1s = true,
          .cfg.misa_w = true,
          .cfg.pmp = true,
-        .cfg.pmp_regions = 8,
          .cfg.cbom_blocksize = 64,
          .cfg.cbop_blocksize = 64,
          .cfg.cboz_blocksize = 64,
@@ -3167,7 +3163,6 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.ext_zifencei = true,
          .cfg.ext_zicsr = true,
          .cfg.pmp = true,
-        .cfg.pmp_regions = 8,
          .cfg.ext_zicbom = true,
          .cfg.cbom_blocksize = 64,
          .cfg.cboz_blocksize = 64,
@@ -3212,7 +3207,6 @@ static const TypeInfo riscv_cpu_type_infos[] = {
 
          .cfg.mmu = true,
          .cfg.pmp = true,
-        .cfg.pmp_regions = 8,
 
          .cfg.max_satp_mode = VM_1_10_SV39,
      ),

I removed all "pmp_regions = 8" assignments but the Si-five ones. The generic
CPUs should retain the default pmp regions value, and every other vendor CPU
should set its own default if they want.

Thanks,

Daniel

target/riscv/cpu.c static const TypeInfo riscv_cpu_type_infos[] = { · line 2987

Quoted 3 lines
target/riscv/cpu.c +0 -0
      ),
 
      DEFINE_ABSTRACT_RISCV_CPU(TYPE_RISCV_CPU_SIFIVE_U,

TYPE_RISCV_VENDOR_CPU,

target/riscv/cpu.h extern RISCVCPUImpliedExtsRule

Quoted 55 lines
target/riscv/cpu.c +7 -1
@@ -2948,7 +2992,8 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.ext_zifencei = true,
          .cfg.ext_zicsr = true,
          .cfg.mmu = true,
-        .cfg.pmp = true
+        .cfg.pmp = true,
+        .cfg.pmp_regions = 8
      ),
 
  #if defined(TARGET_RISCV32) || \
@@ -2966,6 +3011,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.ext_zifencei = true,
          .cfg.ext_zicsr = true,
          .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
          .cfg.ext_smepmp = true,
 
          .cfg.ext_zba = true,
@@ -3040,6 +3086,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.ext_xtheadmempair = true,
          .cfg.ext_xtheadsync = true,
          .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
 
          .cfg.mvendorid = THEAD_VENDOR_ID,
 
@@ -3063,6 +3110,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.rvv_ta_all_1s = true,
          .cfg.misa_w = true,
          .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
          .cfg.cbom_blocksize = 64,
          .cfg.cbop_blocksize = 64,
          .cfg.cboz_blocksize = 64,
@@ -3119,6 +3167,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.ext_zifencei = true,
          .cfg.ext_zicsr = true,
          .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
          .cfg.ext_zicbom = true,
          .cfg.cbom_blocksize = 64,
          .cfg.cboz_blocksize = 64,
@@ -3163,6 +3212,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
 
          .cfg.mmu = true,
          .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
 
          .cfg.max_satp_mode = VM_1_10_SV39,
      ),
target/riscv/cpu.h +0 -0
index 229ade9ed9..67323a7d9d 100644
@@ -159,7 +159,8 @@ extern RISCVCPUImpliedExtsRule

*riscv_multi_ext_implied_rules[];

Review

Quoted 10 lines
target/riscv/cpu.h +2 -1
  #define MMU_USER_IDX 3
 
-#define MAX_RISCV_PMPS (16)
+#define MAX_RISCV_PMPS (64)
+#define OLD_MAX_RISCV_PMPS (16)
 
  #if !defined(CONFIG_USER_ONLY)
  #include "pmp.h"
(quoted diff) +0 -0

b/target/riscv/cpu_cfg_fields.h.inc

target/riscv/csr.c static RISCVException dbltrp_hmode(CPURISCVState

Quoted 16 lines

index 59f134a419..33c4f9bac8 100644

target/riscv/cpu_cfg_fields.h.inc +1 -0
@@ -163,6 +163,7 @@ TYPED_FIELD(uint16_t, elen, 0)
  TYPED_FIELD(uint16_t, cbom_blocksize, 0)
  TYPED_FIELD(uint16_t, cbop_blocksize, 0)
  TYPED_FIELD(uint16_t, cboz_blocksize, 0)
+TYPED_FIELD(uint8_t,  pmp_regions, 0)
 
  TYPED_FIELD(int8_t, max_satp_mode, -1)
 
target/riscv/csr.c +0 -0
index d6cd441133..6296ecd1e1 100644
@@ -738,7 +738,10 @@ static RISCVException dbltrp_hmode(CPURISCVState

*env, int csrno)

Quoted 31 lines
target/riscv/csr.c +4 -1
  static RISCVException pmp(CPURISCVState *env, int csrno)
  {
      if (riscv_cpu_cfg(env)->pmp) {
-        if (csrno <= CSR_PMPCFG3) {
+        int max_pmpcfg = (env->priv_ver >= PRIV_VERSION_1_12_0) ?
++                              CSR_PMPCFG15 : CSR_PMPCFG3;
+
+        if (csrno <= max_pmpcfg) {
              uint32_t reg_index = csrno - CSR_PMPCFG0;
 
              /* TODO: RV128 restriction check */
target/riscv/machine.c +2 -1
index c97e9ce9df..1600ec44f0 100644
@@ -36,8 +36,9 @@ static int pmp_post_load(void *opaque, int version_id)
      RISCVCPU *cpu = opaque;
      CPURISCVState *env = &cpu->env;
      int i;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
-    for (i = 0; i < MAX_RISCV_PMPS; i++) {
+    for (i = 0; i < pmp_regions; i++) {
          pmp_update_rule_addr(env, i);
      }
      pmp_update_rule_nums(env);
target/riscv/pmp.c +0 -0
index 5af295e410..3540327c9a 100644

target/riscv/pmp.c uint32_t pmp_get_num_rules(CPURISCVState *env) · line 123

Quoted 2 lines
target/riscv/pmp.c +0 -0
@@ -122,7 +122,9 @@ uint32_t pmp_get_num_rules(CPURISCVState *env)
   */
  static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t

pmp_index)

target/riscv/pmp.c static inline uint8_t pmp_read_cfg(CPURISCVState

Quoted 9 lines
target/riscv/pmp.c +3 -1
  {
-    if (pmp_index < MAX_RISCV_PMPS) {
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
+
+    if (pmp_index < pmp_regions) {
          return env->pmp_state.pmp[pmp_index].cfg_reg;
      }
 
@@ -136,7 +138,9 @@ static inline uint8_t pmp_read_cfg(CPURISCVState

*env, uint32_t pmp_index)

target/riscv/pmp.c static inline uint8_t pmp_read_cfg(CPURISCVState · line 139

Quoted 2 lines
target/riscv/pmp.c +0 -0
   */
  static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index,

uint8_t val)

target/riscv/pmp.c void pmp_update_rule_addr(CPURISCVState *env,

Quoted 9 lines
target/riscv/pmp.c +3 -1
  {
-    if (pmp_index < MAX_RISCV_PMPS) {
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
+
+    if (pmp_index < pmp_regions) {
          if (env->pmp_state.pmp[pmp_index].cfg_reg == val) {
              /* no change */
              return false;
@@ -236,9 +240,10 @@ void pmp_update_rule_addr(CPURISCVState *env,

uint32_t pmp_index)

target/riscv/pmp.c bool pmp_hart_has_privs(CPURISCVState *env, hwaddr

Quoted 12 lines
target/riscv/pmp.c +2 -1
  void pmp_update_rule_nums(CPURISCVState *env)
  {
      int i;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
      env->pmp_state.num_rules = 0;
-    for (i = 0; i < MAX_RISCV_PMPS; i++) {
+    for (i = 0; i < pmp_regions; i++) {
          const uint8_t a_field =
              pmp_get_a_field(env->pmp_state.pmp[i].cfg_reg);
          if (PMP_AMATCH_OFF != a_field) {
@@ -332,6 +337,7 @@ bool pmp_hart_has_privs(CPURISCVState *env, hwaddr

addr,

target/riscv/pmp.c bool pmp_hart_has_privs(CPURISCVState *env, hwaddr

Quoted 8 lines
target/riscv/pmp.c +1 -0
      int pmp_size = 0;
      hwaddr s = 0;
      hwaddr e = 0;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
      /* Short cut if no rules */
      if (0 == pmp_get_num_rules(env)) {
@@ -356,7 +362,7 @@ bool pmp_hart_has_privs(CPURISCVState *env, hwaddr

addr,

target/riscv/pmp.c void pmpaddr_csr_write(CPURISCVState *env, uint32_t

Quoted 9 lines
target/riscv/pmp.c +1 -1
       * 1.10 draft priv spec states there is an implicit order
       * from low to high
       */
-    for (i = 0; i < MAX_RISCV_PMPS; i++) {
+    for (i = 0; i < pmp_regions; i++) {
          s = pmp_is_in_range(env, i, addr);
          e = pmp_is_in_range(env, i, addr + pmp_size - 1);
 
@@ -527,8 +533,9 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t

addr_index,

target/riscv/pmp.c void pmpaddr_csr_write(CPURISCVState *env, uint32_t

Quoted 11 lines
target/riscv/pmp.c +2 -1
  {
      trace_pmpaddr_csr_write(env->mhartid, addr_index, val);
      bool is_next_cfg_tor = false;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
-    if (addr_index < MAX_RISCV_PMPS) {
+    if (addr_index < pmp_regions) {
          if (env->pmp_state.pmp[addr_index].addr_reg == val) {
              /* no change */
              return;
@@ -538,7 +545,7 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t

addr_index,

target/riscv/pmp.c void pmpaddr_csr_write(CPURISCVState *env, uint32_t · line 549

Quoted 6 lines
target/riscv/pmp.c +1 -1
           * In TOR mode, need to check the lock bit of the next pmp
           * (if there is a next).
           */
-        if (addr_index + 1 < MAX_RISCV_PMPS) {
+        if (addr_index + 1 < pmp_regions) {
              uint8_t pmp_cfg = env->pmp_state.pmp[addr_index +

1].cfg_reg;

target/riscv/pmp.c void pmpaddr_csr_write(CPURISCVState *env, uint32_t · line 550

Quoted 1 line
target/riscv/pmp.c +0 -0
              is_next_cfg_tor = PMP_AMATCH_TOR ==

pmp_get_a_field(pmp_cfg);

target/riscv/pmp.c void pmpaddr_csr_write(CPURISCVState *env, uint32_t

Quoted 2 lines
target/riscv/pmp.c +0 -0
@@ -573,8 +580,9 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t

addr_index,

target/riscv/pmp.c void mseccfg_csr_write(CPURISCVState *env,

Quoted 11 lines
target/riscv/pmp.c +2 -1
  target_ulong pmpaddr_csr_read(CPURISCVState *env, uint32_t addr_index)
  {
      target_ulong val = 0;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
-    if (addr_index < MAX_RISCV_PMPS) {
+    if (addr_index < pmp_regions) {
          val = env->pmp_state.pmp[addr_index].addr_reg;
          trace_pmpaddr_csr_read(env->mhartid, addr_index, val);
      } else {
@@ -592,6 +600,7 @@ void mseccfg_csr_write(CPURISCVState *env,

target_ulong val)

target/riscv/pmp.c void mseccfg_csr_write(CPURISCVState *env, · line 604

Quoted 5 lines
target/riscv/pmp.c +1 -0
  {
      int i;
      uint64_t mask = MSECCFG_MMWP | MSECCFG_MML;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
      /* Update PMM field only if the value is valid according to Zjpm

v1.0 */

target/riscv/pmp.c void mseccfg_csr_write(CPURISCVState *env,

Quoted 3 lines
target/riscv/pmp.c +0 -0
      if (riscv_cpu_cfg(env)->ext_smmpm &&
          riscv_cpu_mxl(env) == MXL_RV64 &&
@@ -603,7 +612,7 @@ void mseccfg_csr_write(CPURISCVState *env,

target_ulong val)

target/riscv/pmp.c void mseccfg_csr_write(CPURISCVState *env, · line 612

Quoted 2 lines
target/riscv/pmp.c +0 -0
      /* RLB cannot be enabled if it's already 0 and if any regions are

locked */

target/riscv/pmp.c target_ulong pmp_get_tlb_size(CPURISCVState *env,

Quoted 7 lines
target/riscv/pmp.c +1 -1
      if (!MSECCFG_RLB_ISSET(env)) {
-        for (i = 0; i < MAX_RISCV_PMPS; i++) {
+        for (i = 0; i < pmp_regions; i++) {
              if (pmp_is_locked(env, i)) {
                  val &= ~MSECCFG_RLB;
                  break;
@@ -659,6 +668,7 @@ target_ulong pmp_get_tlb_size(CPURISCVState *env,

hwaddr addr)

target/riscv/pmp.c target_ulong pmp_get_tlb_size(CPURISCVState *env, · line 674

Quoted 7 lines
target/riscv/pmp.c +1 -0
      hwaddr tlb_sa = addr & ~(TARGET_PAGE_SIZE - 1);
      hwaddr tlb_ea = tlb_sa + TARGET_PAGE_SIZE - 1;
      int i;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
      /*
       * If PMP is not supported or there are no PMP rules, the TLB page

will not

target/riscv/pmp.c target_ulong pmp_get_tlb_size(CPURISCVState *env,

Quoted 1 line
target/riscv/pmp.c +0 -0
@@ -669,7 +679,7 @@ target_ulong pmp_get_tlb_size(CPURISCVState *env,

hwaddr addr)

target/riscv/pmp.c target_ulong pmp_get_tlb_size(CPURISCVState *env, · line 683

Quoted 6 lines
target/riscv/pmp.c +1 -1
          return TARGET_PAGE_SIZE;
      }
 
-    for (i = 0; i < MAX_RISCV_PMPS; i++) {
+    for (i = 0; i < pmp_regions; i++) {
          if (pmp_get_a_field(env->pmp_state.pmp[i].cfg_reg) ==

PMP_AMATCH_OFF) {

Quoted 2 lines
target/riscv/pmp.c +0 -0
              continue;
          }
#6 lore raw

Re: [PATCH v4 2/2] target/riscv: Make PMP region count configurable

Quoted 6 lines · Jay Chang

Hi Daniel,

You're absolutely right — thanks for pointing it out and providing the fix!

Would you like me to send out a v5 patch incorporating your changes?

Yes please

Alistair

Quoted 436 lines

Best Regards
Jay Chang

Quoted 429 lines · Daniel Henrique Barboza

It seems like this patch is breaking 'make check-functional':

Quoted 1 line
Quoted 1 line
Quoted 1 line

UBSAN_OPTIONS=halt_on_error=1:abort_on_error=1:print_summary=1:print_stacktrace=1 RUST_BACKTRACE=1 LD_LIBRARY_PATH=/home/danielhb/work/qemu/build/contrib/plugins:/home/danielhb/work/qemu/build/tests/tcg/plugins MSAN_OPTIONS=halt_on_error=1:abort_on_error=1:print_summary=1:print_stacktrace=1 QEMU_BUILD_ROOT=/home/danielhb/work/qemu/build QEMU_TEST_QEMU_IMG=/home/danielhb/work/qemu/build/qemu-img PYTHONPATH=/home/danielhb/work/qemu/python:/home/danielhb/work/qemu/tests/functional G_TEST_SLOW=1 ASAN_OPTIONS=halt_on_error=1:abort_on_error=1:print_summary=1 MALLOC_PERTURB_=229 MESON_TEST_ITERATION=1 QEMU_TEST_QEMU_BINARY=/home/danielhb/work/qemu/build/qemu-system-riscv64 /home/danielhb/work/qemu/build/pyvenv/bin/python3 /home/danielhb/work/qemu/tests/functional/test_riscv64_tuxrun.py

Summary of Failures:

11/12 qemu:func-thorough+func-riscv32-thorough+thorough / func-riscv32-riscv32_tuxrun TIMEOUT 90.02s killed by signal 15 SIGTERM
12/12 qemu:func-thorough+func-riscv64-thorough+thorough / func-riscv64-riscv64_tuxrun TIMEOUT 120.13s killed by signal 15 SIGTERM

Going through the code:

Quoted 28 lines · Jay Chang

Previously, the number of PMP regions was hardcoded to 16 in QEMU. This patch replaces the fixed value with a new pmp_regions field, allowing platforms to configure the number of PMP regions.

If no specific value is provided, the default number of PMP regions remains 16 to preserve the existing behavior.

A new CPU parameter num-pmp-regions has been introduced to the QEMU command line. For example:

      -cpu rv64, g=true, c=true, pmp=true, num-pmp-regions=8
---
  target/riscv/cpu.c                | 54 +++++++++++++++++++++++++++++--
  target/riscv/cpu.h                |  3 +-
  target/riscv/cpu_cfg_fields.h.inc |  1 +
  target/riscv/csr.c                |  5 ++-
  target/riscv/machine.c            |  3 +-
  target/riscv/pmp.c                | 28 ++++++++++------
  6 files changed, 80 insertions(+), 14 deletions(-)
target/riscv/cpu.c +0 -0
index 629ac37501..8e32252c11 100644

target/riscv/cpu.c static void riscv_cpu_init(Object *obj) · line 1120

Quoted 4 lines
target/riscv/cpu.c +1 -0
@@ -1117,6 +1117,7 @@ static void riscv_cpu_init(Object *obj)
      cpu->cfg.cbom_blocksize = 64;
      cpu->cfg.cbop_blocksize = 64;
      cpu->cfg.cboz_blocksize = 64;
+    cpu->cfg.pmp_regions = 16;

pmp_regions is set to 16 in the base class, which is what we want to keep the default intact. But then:

Quoted 67 lines
target/riscv/cpu.c +42 -0
      cpu->env.vext_ver = VEXT_VERSION_1_00_0;
      cpu->cfg.max_satp_mode = -1;
 
@@ -1568,6 +1569,46 @@ static const PropertyInfo prop_pmp = {
      .set = prop_pmp_set,
  };
 
+static void prop_num_pmp_regions_set(Object *obj, Visitor *v, const char *name,
+                                     void *opaque, Error **errp)
+{
+    RISCVCPU *cpu = RISCV_CPU(obj);
+    uint8_t value;
+
+    visit_type_uint8(v, name, &value, errp);
+
+    if (cpu->cfg.pmp_regions != value && riscv_cpu_is_vendor(obj)) {
+        cpu_set_prop_err(cpu, name, errp);
+        return;
+    }
+
+    if (cpu->env.priv_ver < PRIV_VERSION_1_12_0 && value > OLD_MAX_RISCV_PMPS) {
+        error_setg(errp, "Number of PMP regions exceeds maximum available");
+        return;
+    } else if (value > MAX_RISCV_PMPS) {
+        error_setg(errp, "Number of PMP regions exceeds maximum available");
+        return;
+    }
+
+    cpu_option_add_user_setting(name, value);
+    cpu->cfg.pmp_regions = value;
+}
+
+static void prop_num_pmp_regions_get(Object *obj, Visitor *v, const char *name,
+                                     void *opaque, Error **errp)
+{
+    uint8_t value = RISCV_CPU(obj)->cfg.pmp_regions;
+
+    visit_type_uint8(v, name, &value, errp);
+}
+
+static const PropertyInfo prop_num_pmp_regions = {
+    .type = "uint8",
+    .description = "num-pmp-regions",
+    .get = prop_num_pmp_regions_get,
+    .set = prop_num_pmp_regions_set,
+};
+
  static int priv_spec_from_str(const char *priv_spec_str)
  {
      int priv_version = -1;
@@ -2567,6 +2608,7 @@ static const Property riscv_cpu_properties[] = {
 
      {.name = "mmu", .info = &prop_mmu},
      {.name = "pmp", .info = &prop_pmp},
+    {.name = "num-pmp-regions", .info = &prop_num_pmp_regions},
 
      {.name = "priv_spec", .info = &prop_priv_spec},
      {.name = "vext_spec", .info = &prop_vext_spec},
@@ -2891,6 +2933,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
      DEFINE_ABSTRACT_RISCV_CPU(TYPE_RISCV_DYNAMIC_CPU, TYPE_RISCV_CPU,
          .cfg.mmu = true,
          .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
          .priv_spec = PRIV_VERSION_LATEST,
      ),
 

target/riscv/cpu.c static const TypeInfo riscv_cpu_type_infos[] = { · line 2984

Quoted 6 lines
target/riscv/cpu.c +2 -1
@@ -2937,7 +2980,8 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.max_satp_mode = VM_1_10_MBARE,
          .cfg.ext_zifencei = true,
          .cfg.ext_zicsr = true,
-        .cfg.pmp = true
+        .cfg.pmp = true,
+        .cfg.pmp_regions = 8

We can't set pmp_regions = 8 in the CPU definitions too. This will overwrite the pmp_regions = 16 from the parent object. In short, we're changing the default val for a lot of CPUs.

This diff fixes the test:

target/riscv/cpu.c +0 -6
index d49c02dfe4..b84f08e582 100644
@@ -2933,7 +2933,6 @@ static const TypeInfo riscv_cpu_type_infos[] = {
      DEFINE_ABSTRACT_RISCV_CPU(TYPE_RISCV_DYNAMIC_CPU, TYPE_RISCV_CPU,
          .cfg.mmu = true,
          .cfg.pmp = true,
-        .cfg.pmp_regions = 8,
          .priv_spec = PRIV_VERSION_LATEST,
      ),
 
@@ -3011,7 +3010,6 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.ext_zifencei = true,
          .cfg.ext_zicsr = true,
          .cfg.pmp = true,
-        .cfg.pmp_regions = 8,
          .cfg.ext_smepmp = true,
 
          .cfg.ext_zba = true,
@@ -3086,7 +3084,6 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.ext_xtheadmempair = true,
          .cfg.ext_xtheadsync = true,
          .cfg.pmp = true,
-        .cfg.pmp_regions = 8,
 
          .cfg.mvendorid = THEAD_VENDOR_ID,
 
@@ -3110,7 +3107,6 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.rvv_ta_all_1s = true,
          .cfg.misa_w = true,
          .cfg.pmp = true,
-        .cfg.pmp_regions = 8,
          .cfg.cbom_blocksize = 64,
          .cfg.cbop_blocksize = 64,
          .cfg.cboz_blocksize = 64,
@@ -3167,7 +3163,6 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.ext_zifencei = true,
          .cfg.ext_zicsr = true,
          .cfg.pmp = true,
-        .cfg.pmp_regions = 8,
          .cfg.ext_zicbom = true,
          .cfg.cbom_blocksize = 64,
          .cfg.cboz_blocksize = 64,
@@ -3212,7 +3207,6 @@ static const TypeInfo riscv_cpu_type_infos[] = {
 
          .cfg.mmu = true,
          .cfg.pmp = true,
-        .cfg.pmp_regions = 8,
 
          .cfg.max_satp_mode = VM_1_10_SV39,
      ),

I removed all "pmp_regions = 8" assignments but the Si-five ones. The generic CPUs should retain the default pmp regions value, and every other vendor CPU should set its own default if they want.

Thanks,

Daniel

Quoted 230 lines
target/riscv/cpu.c +7 -1
      ),
 
      DEFINE_ABSTRACT_RISCV_CPU(TYPE_RISCV_CPU_SIFIVE_U, TYPE_RISCV_VENDOR_CPU,
@@ -2948,7 +2992,8 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.ext_zifencei = true,
          .cfg.ext_zicsr = true,
          .cfg.mmu = true,
-        .cfg.pmp = true
+        .cfg.pmp = true,
+        .cfg.pmp_regions = 8
      ),
 
  #if defined(TARGET_RISCV32) || \
@@ -2966,6 +3011,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.ext_zifencei = true,
          .cfg.ext_zicsr = true,
          .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
          .cfg.ext_smepmp = true,
 
          .cfg.ext_zba = true,
@@ -3040,6 +3086,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.ext_xtheadmempair = true,
          .cfg.ext_xtheadsync = true,
          .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
 
          .cfg.mvendorid = THEAD_VENDOR_ID,
 
@@ -3063,6 +3110,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.rvv_ta_all_1s = true,
          .cfg.misa_w = true,
          .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
          .cfg.cbom_blocksize = 64,
          .cfg.cbop_blocksize = 64,
          .cfg.cboz_blocksize = 64,
@@ -3119,6 +3167,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
          .cfg.ext_zifencei = true,
          .cfg.ext_zicsr = true,
          .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
          .cfg.ext_zicbom = true,
          .cfg.cbom_blocksize = 64,
          .cfg.cboz_blocksize = 64,
@@ -3163,6 +3212,7 @@ static const TypeInfo riscv_cpu_type_infos[] = {
 
          .cfg.mmu = true,
          .cfg.pmp = true,
+        .cfg.pmp_regions = 8,
 
          .cfg.max_satp_mode = VM_1_10_SV39,
      ),
target/riscv/cpu.h +2 -1
index 229ade9ed9..67323a7d9d 100644
@@ -159,7 +159,8 @@ extern RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[];
 
  #define MMU_USER_IDX 3
 
-#define MAX_RISCV_PMPS (16)
+#define MAX_RISCV_PMPS (64)
+#define OLD_MAX_RISCV_PMPS (16)
 
  #if !defined(CONFIG_USER_ONLY)
  #include "pmp.h"
target/riscv/cpu_cfg_fields.h.inc +1 -0
index 59f134a419..33c4f9bac8 100644
@@ -163,6 +163,7 @@ TYPED_FIELD(uint16_t, elen, 0)
  TYPED_FIELD(uint16_t, cbom_blocksize, 0)
  TYPED_FIELD(uint16_t, cbop_blocksize, 0)
  TYPED_FIELD(uint16_t, cboz_blocksize, 0)
+TYPED_FIELD(uint8_t,  pmp_regions, 0)
 
  TYPED_FIELD(int8_t, max_satp_mode, -1)
 
target/riscv/csr.c +4 -1
index d6cd441133..6296ecd1e1 100644
@@ -738,7 +738,10 @@ static RISCVException dbltrp_hmode(CPURISCVState *env, int csrno)
  static RISCVException pmp(CPURISCVState *env, int csrno)
  {
      if (riscv_cpu_cfg(env)->pmp) {
-        if (csrno <= CSR_PMPCFG3) {
+        int max_pmpcfg = (env->priv_ver >= PRIV_VERSION_1_12_0) ?
++                              CSR_PMPCFG15 : CSR_PMPCFG3;
+
+        if (csrno <= max_pmpcfg) {
              uint32_t reg_index = csrno - CSR_PMPCFG0;
 
              /* TODO: RV128 restriction check */
target/riscv/machine.c +2 -1
index c97e9ce9df..1600ec44f0 100644
@@ -36,8 +36,9 @@ static int pmp_post_load(void *opaque, int version_id)
      RISCVCPU *cpu = opaque;
      CPURISCVState *env = &cpu->env;
      int i;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
-    for (i = 0; i < MAX_RISCV_PMPS; i++) {
+    for (i = 0; i < pmp_regions; i++) {
          pmp_update_rule_addr(env, i);
      }
      pmp_update_rule_nums(env);
target/riscv/pmp.c +19 -9
index 5af295e410..3540327c9a 100644
@@ -122,7 +122,9 @@ uint32_t pmp_get_num_rules(CPURISCVState *env)
   */
  static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index)
  {
-    if (pmp_index < MAX_RISCV_PMPS) {
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
+
+    if (pmp_index < pmp_regions) {
          return env->pmp_state.pmp[pmp_index].cfg_reg;
      }
 
@@ -136,7 +138,9 @@ static inline uint8_t pmp_read_cfg(CPURISCVState *env, uint32_t pmp_index)
   */
  static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)
  {
-    if (pmp_index < MAX_RISCV_PMPS) {
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
+
+    if (pmp_index < pmp_regions) {
          if (env->pmp_state.pmp[pmp_index].cfg_reg == val) {
              /* no change */
              return false;
@@ -236,9 +240,10 @@ void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index)
  void pmp_update_rule_nums(CPURISCVState *env)
  {
      int i;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
      env->pmp_state.num_rules = 0;
-    for (i = 0; i < MAX_RISCV_PMPS; i++) {
+    for (i = 0; i < pmp_regions; i++) {
          const uint8_t a_field =
              pmp_get_a_field(env->pmp_state.pmp[i].cfg_reg);
          if (PMP_AMATCH_OFF != a_field) {
@@ -332,6 +337,7 @@ bool pmp_hart_has_privs(CPURISCVState *env, hwaddr addr,
      int pmp_size = 0;
      hwaddr s = 0;
      hwaddr e = 0;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
      /* Short cut if no rules */
      if (0 == pmp_get_num_rules(env)) {
@@ -356,7 +362,7 @@ bool pmp_hart_has_privs(CPURISCVState *env, hwaddr addr,
       * 1.10 draft priv spec states there is an implicit order
       * from low to high
       */
-    for (i = 0; i < MAX_RISCV_PMPS; i++) {
+    for (i = 0; i < pmp_regions; i++) {
          s = pmp_is_in_range(env, i, addr);
          e = pmp_is_in_range(env, i, addr + pmp_size - 1);
 
@@ -527,8 +533,9 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
  {
      trace_pmpaddr_csr_write(env->mhartid, addr_index, val);
      bool is_next_cfg_tor = false;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
-    if (addr_index < MAX_RISCV_PMPS) {
+    if (addr_index < pmp_regions) {
          if (env->pmp_state.pmp[addr_index].addr_reg == val) {
              /* no change */
              return;
@@ -538,7 +545,7 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
           * In TOR mode, need to check the lock bit of the next pmp
           * (if there is a next).
           */
-        if (addr_index + 1 < MAX_RISCV_PMPS) {
+        if (addr_index + 1 < pmp_regions) {
              uint8_t pmp_cfg = env->pmp_state.pmp[addr_index + 1].cfg_reg;
              is_next_cfg_tor = PMP_AMATCH_TOR == pmp_get_a_field(pmp_cfg);
 
@@ -573,8 +580,9 @@ void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
  target_ulong pmpaddr_csr_read(CPURISCVState *env, uint32_t addr_index)
  {
      target_ulong val = 0;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
-    if (addr_index < MAX_RISCV_PMPS) {
+    if (addr_index < pmp_regions) {
          val = env->pmp_state.pmp[addr_index].addr_reg;
          trace_pmpaddr_csr_read(env->mhartid, addr_index, val);
      } else {
@@ -592,6 +600,7 @@ void mseccfg_csr_write(CPURISCVState *env, target_ulong val)
  {
      int i;
      uint64_t mask = MSECCFG_MMWP | MSECCFG_MML;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
      /* Update PMM field only if the value is valid according to Zjpm v1.0 */
      if (riscv_cpu_cfg(env)->ext_smmpm &&
          riscv_cpu_mxl(env) == MXL_RV64 &&
@@ -603,7 +612,7 @@ void mseccfg_csr_write(CPURISCVState *env, target_ulong val)
 
      /* RLB cannot be enabled if it's already 0 and if any regions are locked */
      if (!MSECCFG_RLB_ISSET(env)) {
-        for (i = 0; i < MAX_RISCV_PMPS; i++) {
+        for (i = 0; i < pmp_regions; i++) {
              if (pmp_is_locked(env, i)) {
                  val &= ~MSECCFG_RLB;
                  break;
@@ -659,6 +668,7 @@ target_ulong pmp_get_tlb_size(CPURISCVState *env, hwaddr addr)
      hwaddr tlb_sa = addr & ~(TARGET_PAGE_SIZE - 1);
      hwaddr tlb_ea = tlb_sa + TARGET_PAGE_SIZE - 1;
      int i;
+    uint8_t pmp_regions = riscv_cpu_cfg(env)->pmp_regions;
 
      /*
       * If PMP is not supported or there are no PMP rules, the TLB page will not
@@ -669,7 +679,7 @@ target_ulong pmp_get_tlb_size(CPURISCVState *env, hwaddr addr)
          return TARGET_PAGE_SIZE;
      }
 
-    for (i = 0; i < MAX_RISCV_PMPS; i++) {
+    for (i = 0; i < pmp_regions; i++) {
          if (pmp_get_a_field(env->pmp_state.pmp[i].cfg_reg) == PMP_AMATCH_OFF) {
              continue;
          }
#7 lore raw
In reply to Jay Chang
Quoted 10 lines · Jay Chang

The first patch extends the number of PMP regions supported up to 64, following the RISC-V Privileged Specification (version >1.12), where RV32 can have up to 64 PMP regions configured through 16 CSRs.

The second patch makes the PMP region count configurable via a new CPU parameter num-pmp-regions. This allows platforms to adjust the number of PMP regions without relying on a fixed default value. If unspecified, the default remains 16 to preserve compatibility.

Quoted 8 lines

Change log:
V4:
* Changed pmp_regions type from uint16_t to uint8_t
* Introduced OLD_MAX_RISCV_PMPS macro

Jay Chang (2):
target/riscv: Extend PMP region up to 64 target/riscv: Make PMP region count configurable

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 14 lines
 target/riscv/cpu.c                |  54 ++++++++++++-
 target/riscv/cpu.h                |   3 +-
 target/riscv/cpu_bits.h           |  60 ++++++++++++++
 target/riscv/cpu_cfg_fields.h.inc |   1 +
 target/riscv/csr.c                | 129 +++++++++++++++++++++++++++++-
 target/riscv/machine.c            |   3 +-
 target/riscv/pmp.c                |  28 ++++---
 7 files changed, 262 insertions(+), 16 deletions(-)

--
2.48.1