Skip to contentSkip to conversation

target/riscv: Support Smpmpmt extension

PATCH Superseded Jay Chang 2 participants 2 messages Nov 4, 2025 – Nov 6, 2025 RISC-V

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jay Chang

author
#0 lore raw

PATCH 1/1 target/riscv: Support Smpmpmt extension

The Smpmpmt extension provides a mechanism to control memory attributes at the granularity of PMP (Physical Memory Protection) registers, similar to how Svpbmt controls memory attributes at the page level.

Version 0.6
https://github.com/riscv/riscv-isa-manual/blob/smpmpmt/src/smpmpmt.adoc#svpbmt

---
 target/riscv/cpu.c                |  2 ++
 target/riscv/cpu_cfg_fields.h.inc |  1 +
 target/riscv/pmp.c                | 12 ++++++++++++
 target/riscv/pmp.h                |  1 +
 4 files changed, 16 insertions(+)
target/riscv/cpu.c +2 -0
index ae8b721e55..a0290f06f6 100644
@@ -230,6 +230,7 @@ const RISCVIsaExtData isa_edata_arr[] = {
     ISA_EXT_DATA_ENTRY(svinval, PRIV_VERSION_1_12_0, ext_svinval),
     ISA_EXT_DATA_ENTRY(svnapot, PRIV_VERSION_1_12_0, ext_svnapot),
     ISA_EXT_DATA_ENTRY(svpbmt, PRIV_VERSION_1_12_0, ext_svpbmt),
+    ISA_EXT_DATA_ENTRY(smpmpmt, PRIV_VERSION_1_12_0, ext_smpmpmt),
     ISA_EXT_DATA_ENTRY(svrsw60t59b, PRIV_VERSION_1_13_0, ext_svrsw60t59b),
     ISA_EXT_DATA_ENTRY(svukte, PRIV_VERSION_1_13_0, ext_svukte),
     ISA_EXT_DATA_ENTRY(svvptc, PRIV_VERSION_1_13_0, ext_svvptc),
@@ -1262,6 +1263,7 @@ const RISCVCPUMultiExtConfig riscv_cpu_extensions[] = {
     MULTI_EXT_CFG_BOOL("svinval", ext_svinval, false),
     MULTI_EXT_CFG_BOOL("svnapot", ext_svnapot, false),
     MULTI_EXT_CFG_BOOL("svpbmt", ext_svpbmt, false),
+    MULTI_EXT_CFG_BOOL("smpmpmt", ext_smpmpmt, false),
     MULTI_EXT_CFG_BOOL("svrsw60t59b", ext_svrsw60t59b, false),
     MULTI_EXT_CFG_BOOL("svvptc", ext_svvptc, true),
 
target/riscv/cpu_cfg_fields.h.inc +1 -0
index a154ecdc79..b1096da664 100644
@@ -57,6 +57,7 @@ BOOL_FIELD(ext_svadu)
 BOOL_FIELD(ext_svinval)
 BOOL_FIELD(ext_svnapot)
 BOOL_FIELD(ext_svpbmt)
+BOOL_FIELD(ext_smpmpmt)
 BOOL_FIELD(ext_svrsw60t59b)
 BOOL_FIELD(ext_svvptc)
 BOOL_FIELD(ext_svukte)
target/riscv/pmp.c +12 -0
index 3ef62d26ad..52a7677683 100644
@@ -165,6 +165,18 @@ static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)
                           "ignoring pmpcfg write - invalid\n");
         } else {
             uint8_t a_field = pmp_get_a_field(val);
+
+            if (!riscv_cpu_cfg(env)->ext_smpmpmt) {
+                /* If smpmpmt not supported, clear the MTMATCH bit */
+                val &= ~PMP_MTMATCH;
+            } else if ((val & PMP_MTMATCH) == PMP_MTMATCH) {
+                /*
+                 * If trying to set reserved value (0x3) for MT field,
+                 * preserve the original MT field from current config.
+                 */
+                val = (val & ~PMP_MTMATCH) |
+                    (env->pmp_state.pmp[pmp_index].cfg_reg & PMP_MTMATCH);
+            }
             /*
              * When granularity g >= 1 (i.e., granularity > 4 bytes),
              * the NA4 (Naturally Aligned 4-byte) mode is not selectable
target/riscv/pmp.h +1 -0
index 271cf24169..467fb6b4b1 100644
@@ -29,6 +29,7 @@ typedef enum {
     PMP_WRITE = 1 << 1,
     PMP_EXEC  = 1 << 2,
     PMP_AMATCH = (3 << 3),
+    PMP_MTMATCH = (3 << 5),
     PMP_LOCK  = 1 << 7
 } pmp_priv_t;
 
Signature · 1 line
2.48.1
#1 lore raw
Quoted 21 lines · Jay Chang

The Smpmpmt extension provides a mechanism to control memory attributes at the granularity of PMP (Physical Memory Protection) registers, similar to how Svpbmt controls memory attributes at the page level.

Version 0.6
https://github.com/riscv/riscv-isa-manual/blob/smpmpmt/src/smpmpmt.adoc#svpbmt

---
  target/riscv/cpu.c                |  2 ++
  target/riscv/cpu_cfg_fields.h.inc |  1 +
  target/riscv/pmp.c                | 12 ++++++++++++
  target/riscv/pmp.h                |  1 +
  4 files changed, 16 insertions(+)
target/riscv/cpu.c +0 -0
index ae8b721e55..a0290f06f6 100644

target/riscv/cpu.c const RISCVIsaExtData isa_edata_arr[] = { · line 233

Quoted 4 lines
target/riscv/cpu.c +1 -0
@@ -230,6 +230,7 @@ const RISCVIsaExtData isa_edata_arr[] = {
      ISA_EXT_DATA_ENTRY(svinval, PRIV_VERSION_1_12_0, ext_svinval),
      ISA_EXT_DATA_ENTRY(svnapot, PRIV_VERSION_1_12_0, ext_svnapot),
      ISA_EXT_DATA_ENTRY(svpbmt, PRIV_VERSION_1_12_0, ext_svpbmt),
+    ISA_EXT_DATA_ENTRY(smpmpmt, PRIV_VERSION_1_12_0, ext_smpmpmt),

We need to place smpmpmt in the correct riscv,isa order, i.e. right before smrnmi.

Otherwise LGTM. Thanks,

Daniel

Quoted 58 lines
target/riscv/cpu.c +1 -0
      ISA_EXT_DATA_ENTRY(svrsw60t59b, PRIV_VERSION_1_13_0, ext_svrsw60t59b),
      ISA_EXT_DATA_ENTRY(svukte, PRIV_VERSION_1_13_0, ext_svukte),
      ISA_EXT_DATA_ENTRY(svvptc, PRIV_VERSION_1_13_0, ext_svvptc),
@@ -1262,6 +1263,7 @@ const RISCVCPUMultiExtConfig riscv_cpu_extensions[] = {
      MULTI_EXT_CFG_BOOL("svinval", ext_svinval, false),
      MULTI_EXT_CFG_BOOL("svnapot", ext_svnapot, false),
      MULTI_EXT_CFG_BOOL("svpbmt", ext_svpbmt, false),
+    MULTI_EXT_CFG_BOOL("smpmpmt", ext_smpmpmt, false),
      MULTI_EXT_CFG_BOOL("svrsw60t59b", ext_svrsw60t59b, false),
      MULTI_EXT_CFG_BOOL("svvptc", ext_svvptc, true),
 
target/riscv/cpu_cfg_fields.h.inc +1 -0
index a154ecdc79..b1096da664 100644
@@ -57,6 +57,7 @@ BOOL_FIELD(ext_svadu)
  BOOL_FIELD(ext_svinval)
  BOOL_FIELD(ext_svnapot)
  BOOL_FIELD(ext_svpbmt)
+BOOL_FIELD(ext_smpmpmt)
  BOOL_FIELD(ext_svrsw60t59b)
  BOOL_FIELD(ext_svvptc)
  BOOL_FIELD(ext_svukte)
target/riscv/pmp.c +12 -0
index 3ef62d26ad..52a7677683 100644
@@ -165,6 +165,18 @@ static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)
                            "ignoring pmpcfg write - invalid\n");
          } else {
              uint8_t a_field = pmp_get_a_field(val);
+
+            if (!riscv_cpu_cfg(env)->ext_smpmpmt) {
+                /* If smpmpmt not supported, clear the MTMATCH bit */
+                val &= ~PMP_MTMATCH;
+            } else if ((val & PMP_MTMATCH) == PMP_MTMATCH) {
+                /*
+                 * If trying to set reserved value (0x3) for MT field,
+                 * preserve the original MT field from current config.
+                 */
+                val = (val & ~PMP_MTMATCH) |
+                    (env->pmp_state.pmp[pmp_index].cfg_reg & PMP_MTMATCH);
+            }
              /*
               * When granularity g >= 1 (i.e., granularity > 4 bytes),
               * the NA4 (Naturally Aligned 4-byte) mode is not selectable
target/riscv/pmp.h +1 -0
index 271cf24169..467fb6b4b1 100644
@@ -29,6 +29,7 @@ typedef enum {
      PMP_WRITE = 1 << 1,
      PMP_EXEC  = 1 << 2,
      PMP_AMATCH = (3 << 3),
+    PMP_MTMATCH = (3 << 5),
      PMP_LOCK  = 1 << 7
  } pmp_priv_t;