Skip to contentSkip to conversation

target/riscv: Implement SMSDID and SMMPT extension

RFC PATCH Superseded LIU Zhiwei 2 participants 17 messages Sep 9, 2025 – Sep 18, 2025

Subsystems (2)

v1 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

LIU Zhiwei

author
#0 lore raw

cover target/riscv: Implement SMSDID and SMMPT extension

This patch set introduces support for the RISC-V Smmpt (Supervisor Domain Access Protection) extension. It only includes two sub-extensions:
SMSDID and SMMPT.

This patch set implements the v0.3.4 version of Smmpt
(https://github.com/riscv/riscv-smmtt/releases/tag/v0.3.4).

As there are newer SMMPT specification versions, this patch set is not intend for merging.

The implementation is broken down into a series of logical steps:

Patch 1 adds the fundamental definitions for the Smmpt extension, including
new CSRs (mmpt, msdcfg), their bit-field layouts, and the corresponding CPU
configuration flags (ext_smmpt, ext_smsdid).

Patch 2 introduces the core logic for Memory Protection Table (MPT) lookups.
It includes a new file, riscv_smmpt.c, which implements the multi-level table walk to determine permissions for a given physical address.

Patch 3 integrates the MPT permission checks into the main MMU and TLB handling pathways. This ensures that both page table walks and final data accesses are subject to Smmpt protection rules.

Patch 4 adds support for the new fence instructions defined by the Smmpt extension, specifically mfence.spa and minval.spa.

Patch 5 enables smmpt and smsdia extendion.

With this series, QEMU can now model systems that utilize the Smmpt extension for enhanced memory security.

LIU Zhiwei (5):
target/riscv: Add basic definitions and CSRs for SMMPT target/riscv: Implement core SMMPT lookup logic target/riscv: Integrate SMMPT checks into MMU and TLB fill target/riscv: Implement SMMPT fence instructions target/riscv: Enable SMMPT extension

 target/riscv/cpu.c                            |   4 +
 target/riscv/cpu.h                            |   9 +-
 target/riscv/cpu_bits.h                       |  27 ++
 target/riscv/cpu_cfg_fields.h.inc             |   2 +
 target/riscv/cpu_helper.c                     |  81 +++++-
 target/riscv/csr.c                            |  83 ++++++
 target/riscv/insn32.decode                    |   2 +
 .../riscv/insn_trans/trans_privileged.c.inc   |  30 ++
 target/riscv/meson.build                      |   1 +
 target/riscv/pmp.h                            |   3 +
 target/riscv/riscv_smmpt.c                    | 273 ++++++++++++++++++
 target/riscv/riscv_smmpt.h                    |  38 +++
 12 files changed, 548 insertions(+), 5 deletions(-)
 create mode 100644 target/riscv/riscv_smmpt.c
 create mode 100644 target/riscv/riscv_smmpt.h
Signature · 1 line
2.25.1
author
#1 lore raw

RFC PATCH 1/5 target/riscv: Add basic definitions and CSRs for SMMPT

This patch lays the groundwork for the SMMPT (Supervisor Domains Access Protection) extension by introducing its fundamental components.

It adds:
- New CPU configuration flags, ext_smmpt and ext_smsdid, to enable the extension.
- Bit-field definitions for the mmpt CSR in cpu_bits.h.
- The mmpt and msdcfg CSR numbers and their read/write handlers in csr.c.
- New fields in CPUArchState to store the state of these new CSRs.
- A new translation failure reason TRANSLATE_MPT_FAIL.

This provides the necessary infrastructure for the core MPT logic and MMU integration that will follow.

---
 target/riscv/cpu.h                |  9 +++-
 target/riscv/cpu_bits.h           | 27 ++++++++++
 target/riscv/cpu_cfg_fields.h.inc |  2 +
 target/riscv/csr.c                | 83 +++++++++++++++++++++++++++++++
 4 files changed, 120 insertions(+), 1 deletion(-)
target/riscv/cpu.h +8 -1
index 4a862da615..fa7b804cb3 100644
@@ -130,7 +130,8 @@ enum {
     TRANSLATE_SUCCESS,
     TRANSLATE_FAIL,
     TRANSLATE_PMP_FAIL,
-    TRANSLATE_G_STAGE_FAIL
+    TRANSLATE_G_STAGE_FAIL,
+    TRANSLATE_MPT_FAIL
 };
 
 /* Extension context status */
@@ -180,6 +181,7 @@ extern RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[];
 #if !defined(CONFIG_USER_ONLY)
 #include "pmp.h"
 #include "debug.h"
+#include "riscv_smmpt.h"
 #endif
 
 #define RV_VLEN_MAX 1024
@@ -486,6 +488,11 @@ struct CPUArchState {
     uint64_t hstateen[SMSTATEEN_MAX_COUNT];
     uint64_t sstateen[SMSTATEEN_MAX_COUNT];
     uint64_t henvcfg;
+    /* Smsdid */
+    uint32_t mptmode;
+    uint32_t sdid;
+    uint64_t mptppn;
+    uint32_t msdcfg;
 #endif
 
     /* Fields from here on are preserved across CPU reset. */
target/riscv/cpu_bits.h +27 -0
index b62dd82fe7..c6a34863d1 100644
@@ -1164,4 +1164,31 @@ typedef enum CTRType {
 #define MCONTEXT64                         0x0000000000001FFFULL
 #define MCONTEXT32_HCONTEXT                0x0000007F
 #define MCONTEXT64_HCONTEXT                0x0000000000003FFFULL
+
+/* Smsdid */
+#define CSR_MMPT        0xbc0
+#define CSR_MSDCFG      0xbd1
+
+#define MMPT_MODE_MASK_32   0xC0000000
+#define MMPT_SDID_MASK_32   0x3F000000
+#define MMPT_PPN_MASK_32    0x003FFFFF
+
+#define MMPT_MODE_SHIFT_32  30
+#define MMPT_SDID_SHIFT_32  24
+
+#define MMPT_MODE_MASK_64   0xF000000000000000ULL
+#define MMPT_SDID_MASK_64   0x0FC0000000000000ULL
+#define MMPT_PPN_MASK_64    0x000FFFFFFFFFFFFFULL
+
+#define MPTE_L3_VALID       0x0000100000000000ULL
+#define MPTE_L3_RESERVED    0xFFFFE00000000000ULL
+
+#define MPTE_L2_RESERVED_64    0xFFFF800000000000ULL
+#define MPTE_L2_RESERVED_32    0xFE000000
+
+#define MPTE_L1_RESERVED_64    0xFFFFFFFF00000000ULL
+#define MPTE_L1_RESERVED_32    0xFFFF0000
+
+#define MMPT_MODE_SHIFT_64  60
+#define MMPT_SDID_SHIFT_64  54
 #endif
target/riscv/cpu_cfg_fields.h.inc +2 -0
index e2d116f0df..8c8a4ac236 100644
@@ -60,6 +60,8 @@ BOOL_FIELD(ext_svpbmt)
 BOOL_FIELD(ext_svrsw60t59b)
 BOOL_FIELD(ext_svvptc)
 BOOL_FIELD(ext_svukte)
+BOOL_FIELD(ext_smmpt)
+BOOL_FIELD(ext_smsdid)
 BOOL_FIELD(ext_zdinx)
 BOOL_FIELD(ext_zaamo)
 BOOL_FIELD(ext_zacas)
target/riscv/csr.c +83 -0

101 lines not on this page · raw message

Signature · 1 line
2.25.1
#2 lore raw

Re: [RFC PATCH 1/5] target/riscv: Add basic definitions and CSRs for SMMPT

Quoted 207 lines · LIU Zhiwei

This patch lays the groundwork for the SMMPT (Supervisor Domains Access Protection) extension by introducing its fundamental components.

It adds:
- New CPU configuration flags, ext_smmpt and ext_smsdid, to enable the extension.
- Bit-field definitions for the mmpt CSR in cpu_bits.h.
- The mmpt and msdcfg CSR numbers and their read/write handlers in csr.c.
- New fields in CPUArchState to store the state of these new CSRs.
- A new translation failure reason TRANSLATE_MPT_FAIL.

This provides the necessary infrastructure for the core MPT logic and MMU integration that will follow.

---
  target/riscv/cpu.h                |  9 +++-
  target/riscv/cpu_bits.h           | 27 ++++++++++
  target/riscv/cpu_cfg_fields.h.inc |  2 +
  target/riscv/csr.c                | 83 +++++++++++++++++++++++++++++++
  4 files changed, 120 insertions(+), 1 deletion(-)
target/riscv/cpu.h +8 -1
index 4a862da615..fa7b804cb3 100644
@@ -130,7 +130,8 @@ enum {
      TRANSLATE_SUCCESS,
      TRANSLATE_FAIL,
      TRANSLATE_PMP_FAIL,
-    TRANSLATE_G_STAGE_FAIL
+    TRANSLATE_G_STAGE_FAIL,
+    TRANSLATE_MPT_FAIL
  };
 
  /* Extension context status */
@@ -180,6 +181,7 @@ extern RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[];
  #if !defined(CONFIG_USER_ONLY)
  #include "pmp.h"
  #include "debug.h"
+#include "riscv_smmpt.h"
  #endif
 
  #define RV_VLEN_MAX 1024
@@ -486,6 +488,11 @@ struct CPUArchState {
      uint64_t hstateen[SMSTATEEN_MAX_COUNT];
      uint64_t sstateen[SMSTATEEN_MAX_COUNT];
      uint64_t henvcfg;
+    /* Smsdid */
+    uint32_t mptmode;
+    uint32_t sdid;
+    uint64_t mptppn;
+    uint32_t msdcfg;
  #endif
 
      /* Fields from here on are preserved across CPU reset. */
target/riscv/cpu_bits.h +27 -0
index b62dd82fe7..c6a34863d1 100644
@@ -1164,4 +1164,31 @@ typedef enum CTRType {
  #define MCONTEXT64                         0x0000000000001FFFULL
  #define MCONTEXT32_HCONTEXT                0x0000007F
  #define MCONTEXT64_HCONTEXT                0x0000000000003FFFULL
+
+/* Smsdid */
+#define CSR_MMPT        0xbc0
+#define CSR_MSDCFG      0xbd1
+
+#define MMPT_MODE_MASK_32   0xC0000000
+#define MMPT_SDID_MASK_32   0x3F000000
+#define MMPT_PPN_MASK_32    0x003FFFFF
+
+#define MMPT_MODE_SHIFT_32  30
+#define MMPT_SDID_SHIFT_32  24
+
+#define MMPT_MODE_MASK_64   0xF000000000000000ULL
+#define MMPT_SDID_MASK_64   0x0FC0000000000000ULL
+#define MMPT_PPN_MASK_64    0x000FFFFFFFFFFFFFULL
+
+#define MPTE_L3_VALID       0x0000100000000000ULL
+#define MPTE_L3_RESERVED    0xFFFFE00000000000ULL
+
+#define MPTE_L2_RESERVED_64    0xFFFF800000000000ULL
+#define MPTE_L2_RESERVED_32    0xFE000000
+
+#define MPTE_L1_RESERVED_64    0xFFFFFFFF00000000ULL
+#define MPTE_L1_RESERVED_32    0xFFFF0000
+
+#define MMPT_MODE_SHIFT_64  60
+#define MMPT_SDID_SHIFT_64  54
  #endif
target/riscv/cpu_cfg_fields.h.inc +2 -0
index e2d116f0df..8c8a4ac236 100644
@@ -60,6 +60,8 @@ BOOL_FIELD(ext_svpbmt)
  BOOL_FIELD(ext_svrsw60t59b)
  BOOL_FIELD(ext_svvptc)
  BOOL_FIELD(ext_svukte)
+BOOL_FIELD(ext_smmpt)
+BOOL_FIELD(ext_smsdid)
  BOOL_FIELD(ext_zdinx)
  BOOL_FIELD(ext_zaamo)
  BOOL_FIELD(ext_zacas)
target/riscv/csr.c +80 -0
index 8842e07a73..77bc596ed3 100644
@@ -793,6 +793,15 @@ static RISCVException rnmi(CPURISCVState *env, int csrno)
 
      return RISCV_EXCP_ILLEGAL_INST;
  }
+
+static RISCVException smsdid(CPURISCVState *env, int csrno)
+{
+    if (riscv_cpu_cfg(env)->ext_smsdid) {
+        return RISCV_EXCP_NONE;
+    }
+
+    return RISCV_EXCP_ILLEGAL_INST;
+}
  #endif
 
  static RISCVException seed(CPURISCVState *env, int csrno)
@@ -5470,6 +5479,77 @@ static RISCVException write_mnstatus(CPURISCVState *env, int csrno,
      return RISCV_EXCP_NONE;
  }
 
+static RISCVException read_mmpt(CPURISCVState *env, int csrno,
+                                target_ulong *val)
+{
+    if (riscv_cpu_xlen(env) == 32) {
+        uint32_t value = 0;
+        value |= env->mptmode << MMPT_MODE_SHIFT_32;
+        value |= (env->sdid << MMPT_SDID_SHIFT_32) & MMPT_SDID_MASK_32;
+        value |= env->mptppn & MMPT_PPN_MASK_32;
+        *val = value;
+    } else if (riscv_cpu_xlen(env) == 64) {
+        uint64_t value_64 = 0;
+        uint32_t mode_value = env->mptmode;
+        /* mpt_mode_t convert to mmpt.mode value */
+        if (mode_value) {
+            mode_value -= SMMTT43 - SMMTT34;
+        }
+        value_64 |= (uint64_t)mode_value << MMPT_MODE_SHIFT_64;
+        value_64 |= ((uint64_t)env->sdid << MMPT_SDID_SHIFT_64)
+                    & MMPT_SDID_MASK_64;
+        value_64 |= (uint64_t)env->mptppn & MMPT_PPN_MASK_64;
+        *val = value_64;
+    } else {
+        return RISCV_EXCP_ILLEGAL_INST;
+    }
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_mmpt(CPURISCVState *env, int csrno,
+                                 target_ulong val, uintptr_t ra)
+{
+    /* Fixme: if mode is bare, the remaining fields in mmpt must be zero */
+    if (riscv_cpu_xlen(env) == 32) {
+        /* Only write the legal value */
+        uint32_t mode_value = (val & MMPT_MODE_MASK_32) >> MMPT_MODE_SHIFT_32;
+        if (mode_value <= SMMTT34) {
+            env->mptmode = mode_value;
+        }
+        env->sdid = (val & MMPT_SDID_MASK_32) >> MMPT_SDID_SHIFT_32;
+        env->mptppn = val & MMPT_PPN_MASK_32;
+    } else if (riscv_cpu_xlen(env) == 64) {
+        uint32_t mode_value = (val & MMPT_MODE_MASK_64) >> MMPT_MODE_SHIFT_64;
+        /* check legal value */
+        if (mode_value < SMMTTMAX) {
+            /* convert to mpt_mode_t */
+            if (mode_value) {
+                mode_value += SMMTT43 - SMMTT34;
+            }
+            env->mptmode = mode_value;
+        }
+        env->sdid = (val & MMPT_SDID_MASK_64) >> MMPT_SDID_SHIFT_64;
+        env->mptppn = val & MMPT_PPN_MASK_64;
+    } else {
+        return RISCV_EXCP_ILLEGAL_INST;
+    }
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException read_msdcfg(CPURISCVState *env, int csrno,
+                                   target_ulong *val)
+{
+    *val = env->msdcfg;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_msdcfg(CPURISCVState *env, int csrno,
+                                    target_ulong val, uintptr_t ra)
+{
+    env->msdcfg = val;
+    return RISCV_EXCP_NONE;
+}
+
  #endif
 
  /* Crypto Extension */

target/riscv/csr.c riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 6751

Quoted 6 lines
target/riscv/csr.c +3 -0
@@ -6666,6 +6746,9 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
                               write_mhpmcounterh                         },
      [CSR_SCOUNTOVF]      = { "scountovf", sscofpmf,  read_scountovf,
                               .min_priv_ver = PRIV_VERSION_1_12_0 },
+    /* Supervisor Domain Identifier and Protection Registers */
+    [CSR_MMPT] =    { "mmpt",   smsdid,  read_mmpt,   write_mmpt   },
+    [CSR_MSDCFG] =  { "msdcfg", smsdid,  read_msdcfg, write_msdcfg },

I see that you added ext_smmpt but ended up not using in this patch. Shouldn't read_mmpt and write_mmpt dependent on ext_smmpt?

Thanks,

Daniel

Quoted 3 lines
target/riscv/csr.c +0 -0
  #endif /* !CONFIG_USER_ONLY */
  };
author
#3 lore raw

Re: [RFC PATCH 1/5] target/riscv: Add basic definitions and CSRs for SMMPT

Quoted 210 lines · Daniel Henrique Barboza
Quoted 207 lines · LIU Zhiwei

This patch lays the groundwork for the SMMPT (Supervisor Domains Access Protection) extension by introducing its fundamental components.

It adds:
- New CPU configuration flags, ext_smmpt and ext_smsdid, to enable the extension.
- Bit-field definitions for the mmpt CSR in cpu_bits.h.
- The mmpt and msdcfg CSR numbers and their read/write handlers in csr.c.
- New fields in CPUArchState to store the state of these new CSRs.
- A new translation failure reason TRANSLATE_MPT_FAIL.

This provides the necessary infrastructure for the core MPT logic and MMU integration that will follow.

---
  target/riscv/cpu.h                |  9 +++- target/riscv/cpu_bits.h           | 27 ++++++++++ target/riscv/cpu_cfg_fields.h.inc |  2 + target/riscv/csr.c                | 83 +++++++++++++++++++++++++++++++

  4 files changed, 120 insertions(+), 1 deletion(-)
target/riscv/cpu.h +8 -1
index 4a862da615..fa7b804cb3 100644
@@ -130,7 +130,8 @@ enum {
       TRANSLATE_SUCCESS,
       TRANSLATE_FAIL,
       TRANSLATE_PMP_FAIL,
-    TRANSLATE_G_STAGE_FAIL
+    TRANSLATE_G_STAGE_FAIL,
+    TRANSLATE_MPT_FAIL
   };
     /* Extension context status */
@@ -180,6 +181,7 @@ extern RISCVCPUImpliedExtsRule 
 *riscv_multi_ext_implied_rules[];
   #if !defined(CONFIG_USER_ONLY)
   #include "pmp.h"
   #include "debug.h"
+#include "riscv_smmpt.h"
   #endif
     #define RV_VLEN_MAX 1024
@@ -486,6 +488,11 @@ struct CPUArchState {
       uint64_t hstateen[SMSTATEEN_MAX_COUNT];
       uint64_t sstateen[SMSTATEEN_MAX_COUNT];
       uint64_t henvcfg;
+    /* Smsdid */
+    uint32_t mptmode;
+    uint32_t sdid;
+    uint64_t mptppn;
+    uint32_t msdcfg;
   #endif
         /* Fields from here on are preserved across CPU reset. */
target/riscv/cpu_bits.h +27 -0
index b62dd82fe7..c6a34863d1 100644
@@ -1164,4 +1164,31 @@ typedef enum CTRType {
   #define MCONTEXT64 0x0000000000001FFFULL
   #define MCONTEXT32_HCONTEXT                0x0000007F
   #define MCONTEXT64_HCONTEXT 0x0000000000003FFFULL
+
+/* Smsdid */
+#define CSR_MMPT        0xbc0
+#define CSR_MSDCFG      0xbd1
+
+#define MMPT_MODE_MASK_32   0xC0000000
+#define MMPT_SDID_MASK_32   0x3F000000
+#define MMPT_PPN_MASK_32    0x003FFFFF
+
+#define MMPT_MODE_SHIFT_32  30
+#define MMPT_SDID_SHIFT_32  24
+
+#define MMPT_MODE_MASK_64   0xF000000000000000ULL
+#define MMPT_SDID_MASK_64   0x0FC0000000000000ULL
+#define MMPT_PPN_MASK_64    0x000FFFFFFFFFFFFFULL
+
+#define MPTE_L3_VALID       0x0000100000000000ULL
+#define MPTE_L3_RESERVED    0xFFFFE00000000000ULL
+
+#define MPTE_L2_RESERVED_64    0xFFFF800000000000ULL
+#define MPTE_L2_RESERVED_32    0xFE000000
+
+#define MPTE_L1_RESERVED_64    0xFFFFFFFF00000000ULL
+#define MPTE_L1_RESERVED_32    0xFFFF0000
+
+#define MMPT_MODE_SHIFT_64  60
+#define MMPT_SDID_SHIFT_64  54
   #endif
target/riscv/cpu_cfg_fields.h.inc +2 -0
index e2d116f0df..8c8a4ac236 100644
@@ -60,6 +60,8 @@ BOOL_FIELD(ext_svpbmt)
   BOOL_FIELD(ext_svrsw60t59b)
   BOOL_FIELD(ext_svvptc)
   BOOL_FIELD(ext_svukte)
+BOOL_FIELD(ext_smmpt)
+BOOL_FIELD(ext_smsdid)
   BOOL_FIELD(ext_zdinx)
   BOOL_FIELD(ext_zaamo)
   BOOL_FIELD(ext_zacas)
target/riscv/csr.c +39 -0
index 8842e07a73..77bc596ed3 100644
@@ -793,6 +793,15 @@ static RISCVException rnmi(CPURISCVState *env, 
 int csrno)
         return RISCV_EXCP_ILLEGAL_INST;
   }
+
+static RISCVException smsdid(CPURISCVState *env, int csrno)
+{
+    if (riscv_cpu_cfg(env)->ext_smsdid) {
+        return RISCV_EXCP_NONE;
+    }
+
+    return RISCV_EXCP_ILLEGAL_INST;
+}
   #endif
     static RISCVException seed(CPURISCVState *env, int csrno)
@@ -5470,6 +5479,77 @@ static RISCVException 
 write_mnstatus(CPURISCVState *env, int csrno,
       return RISCV_EXCP_NONE;
   }
   +static RISCVException read_mmpt(CPURISCVState *env, int csrno,
+                                target_ulong *val)
+{
+    if (riscv_cpu_xlen(env) == 32) {
+        uint32_t value = 0;
+        value |= env->mptmode << MMPT_MODE_SHIFT_32;
+        value |= (env->sdid << MMPT_SDID_SHIFT_32) & MMPT_SDID_MASK_32;
+        value |= env->mptppn & MMPT_PPN_MASK_32;
+        *val = value;
+    } else if (riscv_cpu_xlen(env) == 64) {
+        uint64_t value_64 = 0;
+        uint32_t mode_value = env->mptmode;
+        /* mpt_mode_t convert to mmpt.mode value */
+        if (mode_value) {
+            mode_value -= SMMTT43 - SMMTT34;
+        }
+        value_64 |= (uint64_t)mode_value << MMPT_MODE_SHIFT_64;
+        value_64 |= ((uint64_t)env->sdid << MMPT_SDID_SHIFT_64)
+                    & MMPT_SDID_MASK_64;
+        value_64 |= (uint64_t)env->mptppn & MMPT_PPN_MASK_64;
+        *val = value_64;
+    } else {
+        return RISCV_EXCP_ILLEGAL_INST;
+    }
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_mmpt(CPURISCVState *env, int csrno,
+                                 target_ulong val, uintptr_t ra)
+{
+    /* Fixme: if mode is bare, the remaining fields in mmpt must be 

zero */
+    if (riscv_cpu_xlen(env) == 32) {
+        /* Only write the legal value */
+        uint32_t mode_value = (val & MMPT_MODE_MASK_32) >> MMPT_MODE_SHIFT_32;
+        if (mode_value <= SMMTT34) {
+            env->mptmode = mode_value;
+        }
+        env->sdid = (val & MMPT_SDID_MASK_32) >> MMPT_SDID_SHIFT_32; +        env->mptppn = val & MMPT_PPN_MASK_32;
+    } else if (riscv_cpu_xlen(env) == 64) {
+        uint32_t mode_value = (val & MMPT_MODE_MASK_64) >> MMPT_MODE_SHIFT_64;
+        /* check legal value */
+        if (mode_value < SMMTTMAX) {
+            /* convert to mpt_mode_t */
+            if (mode_value) {
+                mode_value += SMMTT43 - SMMTT34;
+            }
+            env->mptmode = mode_value;
+        }
+        env->sdid = (val & MMPT_SDID_MASK_64) >> MMPT_SDID_SHIFT_64; +        env->mptppn = val & MMPT_PPN_MASK_64;
+    } else {
+        return RISCV_EXCP_ILLEGAL_INST;
+    }
+    return RISCV_EXCP_NONE;

target/riscv/csr.c +16 -0
+}
+
+static RISCVException read_msdcfg(CPURISCVState *env, int csrno,
+                                   target_ulong *val)
+{
+    *val = env->msdcfg;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_msdcfg(CPURISCVState *env, int csrno,
+                                    target_ulong val, uintptr_t ra)
+{
+    env->msdcfg = val;
+    return RISCV_EXCP_NONE;
+}
+

  #endif
    /* Crypto Extension */

target/riscv/csr.c +0 -0
@@ -6666,6 +6746,9 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
 write_mhpmcounterh                         },
Quoted 8 lines

target/riscv/csr.c riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 6751

Quoted 5 lines
target/riscv/csr.c +3 -0
       [CSR_SCOUNTOVF]      = { "scountovf", sscofpmf, read_scountovf,
                                .min_priv_ver = PRIV_VERSION_1_12_0 },
+    /* Supervisor Domain Identifier and Protection Registers */
+    [CSR_MMPT] =    { "mmpt",   smsdid,  read_mmpt, write_mmpt   },
+    [CSR_MSDCFG] =  { "msdcfg", smsdid,  read_msdcfg, write_msdcfg },

I see that you added ext_smmpt but ended up not using in this patch. Shouldn't read_mmpt and write_mmpt dependent on ext_smmpt?

Hi Daniel,

Great question—thanks for raising it.

From the straightforward reading of the spec, mmpt is part of the M-mode programming interface defined by Smsdid, not Smmpt. Quoting the spec: “Smsdid defines an interface to program the configuration for supervisor-domain-related extensions. The interface consists of M-mode CSRs msdcfg and mmpt.”

From another angle, we can mirror how satp relates to the MMU to get a cleaner design. As the privileged spec says, “When SXLEN=64, all satp encodings corresponding to MODE=Bare are reserved for future standard use.” Based on that analogy, a better approach is to let ext_smmpt constrain the legal mmpt settings:

* Keep mmpt under ext_smsdid (CSR presence/decoding).
* Make mmpt WARL: when ext_smmpt is absent, only MODE=Bare is legal and SDID remains readable/writable; writes to other fields are masked to zero and reads return zero.
* When ext_smmpt is present, enable the full mmpt mode set and fields.

Concretely, I can:

* Keep read_mmpt/write_mmpt gated by ext_smsdid, and
* Add an access mask that depends on ext_smmpt as above (non-Bare modes are WARL-masked when ext_smmpt=0), with a comment referencing the spec.

If that sounds good, I’ll update this patch accordingly. Thanks,
Zhiwei

Quoted 9 lines

Thanks,

Daniel

Quoted 2 lines
target/riscv/csr.c +0 -0
     #endif /* !CONFIG_USER_ONLY */
   };
author
#4 lore raw

RFC PATCH 2/5 target/riscv: Implement core SMMPT lookup logic

In reply to LIU Zhiwei

This patch introduces the core implementation for the Memory Protection Table (MPT) walk, which is the central mechanism of the SMMPT extension.

A new file, riscv_smmpt.c, is added to encapsulate the MPT logic. It implements the smmpt_lookup() function, which performs a multi-level page table-like walk starting from the physical address specified in the mptppn CSR field. This walk determines the access permissions (read, write, execute) for a given physical address.

The implementation supports various SMMPT modes (SMMTT34, SMMTT43, etc.) and correctly handles leaf and non-leaf entries, including reserved bit checks. Helper functions for parsing MPT entries and converting access permissions are also included in the new riscv_smmpt.h header.

---
 target/riscv/meson.build   |   1 +
 target/riscv/pmp.h         |   3 +
 target/riscv/riscv_smmpt.c | 273 +++++++++++++++++++++++++++++++++++++
 target/riscv/riscv_smmpt.h |  38 ++++++
 4 files changed, 315 insertions(+)
 create mode 100644 target/riscv/riscv_smmpt.c
 create mode 100644 target/riscv/riscv_smmpt.h
target/riscv/meson.build +1 -0
index a4bd61e52a..e85b534a64 100644
@@ -29,6 +29,7 @@ riscv_system_ss = ss.source_set()
 riscv_system_ss.add(files(
   'arch_dump.c',
   'pmp.c',
+  'riscv_smmpt.c',
   'debug.c',
   'monitor.c',
   'machine.c',
target/riscv/pmp.h +3 -0
index 271cf24169..d9c5e74345 100644
@@ -85,6 +85,9 @@ void pmp_update_rule_nums(CPURISCVState *env);
 uint32_t pmp_get_num_rules(CPURISCVState *env);
 int pmp_priv_to_page_prot(pmp_priv_t pmp_priv);
 void pmp_unlock_entries(CPURISCVState *env);
+int get_physical_address_pmp(CPURISCVState *env, int *prot, hwaddr addr,
+                             int size, MMUAccessType access_type,
+                             int mode);
 
 #define MSECCFG_MML_ISSET(env) get_field(env->mseccfg, MSECCFG_MML)
 #define MSECCFG_MMWP_ISSET(env) get_field(env->mseccfg, MSECCFG_MMWP)
target/riscv/riscv_smmpt.c new file +273 -0

273 lines not on this page · raw message

target/riscv/riscv_smmpt.h new file +38 -0
new file mode 100644
index 0000000000..d5797c6168
@@ -0,0 +1,38 @@
+/*
+ * QEMU RISC-V Smmpt (Memory Protection Table)
+ *
+ * Copyright (c) 2024 Alibaba Group. All rights reserved.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef RISCV_SMPT_H
+#define RISCV_SMPT_H
+
+#include "cpu.h"
+#include "exec/mmu-access-type.h"
+
+typedef enum {
+    SMMTTBARE = 0,
+    SMMTT34   = 1,
+    SMMTT43   = 2,
+    SMMTT52   = 3,
+    SMMTT64   = 4,
+    SMMTTMAX
+} mpt_mode_t;
+
+typedef enum {
+    ACCESS_ALLOW_R = 0b001,
+    ACCESS_ALLOW_X = 0b100,
+    ACCESS_ALLOW_RX = 0b101 ,
+    ACCESS_ALLOW_RW = 0b011,
+    ACCESS_ALLOW_RWX = 0b111,
+} mpt_access_t;
+
+
+int smmpt_access_to_page_prot(mpt_access_t mpt_access);
+bool smmpt_check_access(CPURISCVState *env, hwaddr addr,
+                        mpt_access_t *allowed_access,
+                        MMUAccessType access_type);
+
+#endif
Signature · 1 line
2.25.1
#5 lore raw

Re: [RFC PATCH 2/5] target/riscv: Implement core SMMPT lookup logic

Quoted 130 lines · LIU Zhiwei

This patch introduces the core implementation for the Memory Protection Table (MPT) walk, which is the central mechanism of the SMMPT extension.

A new file, riscv_smmpt.c, is added to encapsulate the MPT logic. It implements the smmpt_lookup() function, which performs a multi-level page table-like walk starting from the physical address specified in the mptppn CSR field. This walk determines the access permissions (read, write, execute) for a given physical address.

The implementation supports various SMMPT modes (SMMTT34, SMMTT43, etc.) and correctly handles leaf and non-leaf entries, including reserved bit checks. Helper functions for parsing MPT entries and converting access permissions are also included in the new riscv_smmpt.h header.

---
  target/riscv/meson.build   |   1 +
  target/riscv/pmp.h         |   3 +
  target/riscv/riscv_smmpt.c | 273 +++++++++++++++++++++++++++++++++++++
  target/riscv/riscv_smmpt.h |  38 ++++++
  4 files changed, 315 insertions(+)

create mode 100644 target/riscv/riscv_smmpt.c
create mode 100644 target/riscv/riscv_smmpt.h

target/riscv/meson.build +1 -0
index a4bd61e52a..e85b534a64 100644
@@ -29,6 +29,7 @@ riscv_system_ss = ss.source_set()
  riscv_system_ss.add(files(
    'arch_dump.c',
    'pmp.c',
+  'riscv_smmpt.c',
    'debug.c',
    'monitor.c',
    'machine.c',
target/riscv/pmp.h +3 -0
index 271cf24169..d9c5e74345 100644
@@ -85,6 +85,9 @@ void pmp_update_rule_nums(CPURISCVState *env);
  uint32_t pmp_get_num_rules(CPURISCVState *env);
  int pmp_priv_to_page_prot(pmp_priv_t pmp_priv);
  void pmp_unlock_entries(CPURISCVState *env);
+int get_physical_address_pmp(CPURISCVState *env, int *prot, hwaddr addr,
+                             int size, MMUAccessType access_type,
+                             int mode);
 
  #define MSECCFG_MML_ISSET(env) get_field(env->mseccfg, MSECCFG_MML)
  #define MSECCFG_MMWP_ISSET(env) get_field(env->mseccfg, MSECCFG_MMWP)
target/riscv/riscv_smmpt.c new file +72 -0
new file mode 100644
index 0000000000..09e3c66c7f
@@ -0,0 +1,273 @@
+/*
+ * QEMU RISC-V Smmpt (Memory Protection Table)
+ *
+ * Copyright (c) 2024 Alibaba Group. All rights reserved.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "riscv_smmpt.h"
+#include "pmp.h"
+#include "system/memory.h"
+
+typedef uint64_t load_entry_fn(AddressSpace *, hwaddr,
+                               MemTxAttrs, MemTxResult *);
+
+static uint64_t load_entry_32(AddressSpace *as, hwaddr addr,
+                              MemTxAttrs attrs, MemTxResult *result)
+{
+    return address_space_ldl(as, addr, attrs, result);
+}
+
+static uint64_t load_entry_64(AddressSpace *as, hwaddr addr,
+                              MemTxAttrs attrs, MemTxResult *result)
+{
+    return address_space_ldq(as, addr, attrs, result);
+}
+
+typedef union {
+    uint64_t raw;
+    struct {
+        uint32_t v:1;
+        uint32_t l:1;
+        uint32_t rsv1:5;
+        uint32_t perms:24;
+        uint32_t n:1;
+    } leaf32;
+    struct {
+        uint32_t v:1;
+        uint32_t l:1;
+        uint32_t rsv1:8;
+        uint32_t ppn:22;
+    } nonleaf32;
+    struct {
+        uint64_t v:1;
+        uint64_t l:1;
+        uint64_t rsv1:8;
+        uint64_t perms:48;
+        uint64_t rsv2:5;
+        uint64_t n:1;
+    } leaf64;
+    struct {
+        uint64_t v:1;
+        uint64_t l:1;
+        uint64_t rsv1:8;
+        uint64_t ppn:52;
+        uint64_t rsv2:1;
+        uint64_t n:1;
+    } nonleaf64;
+} mpte_union_t;
+
+static inline bool mpte_is_leaf(uint64_t mpte)
+{
+   return mpte & 0x2;
+}
+
+static inline bool mpte_is_valid(uint64_t mpte)
+{
+    return mpte & 0x1;
+}
+
+static uint64_t mpte_get_rsv(CPURISCVState *env, uint64_t mpte)

target/riscv/riscv_smmpt.c line 82

Quoted 10 lines
target/riscv/riscv_smmpt.c new file +10 -0
+{
+    RISCVMXL mxl = riscv_cpu_mxl(env);
+    bool leaf = mpte_is_leaf(mpte);
+    mpte_union_t *u = (mpte_union_t *)&mpte;
+
+    return (mxl == MXL_RV32)
+        ? (leaf ? u->leaf32.rsv1 : u->nonleaf32.rsv1)
+        : (leaf ? (u->leaf64.rsv1 << 5) | u->leaf64.rsv2
+                : (u->nonleaf64.rsv1 << 1) | u->nonleaf64.rsv2);
+}

I suggest going a little easier on the ternary nesting:

if (mxl == MXL_RV32) {
    return leaf ? u->leaf32.rsv1 : u->nonleaf32.rsv1;
}

return leaf ? (u->leaf64.rsv1 << 5) | u->leaf64.rsv2

            : (u->nonleaf64.rsv1 << 1) | u->nonleaf64.rsv2);

Everything else LGTM. Thanks,

Daniel

Quoted 235 lines
target/riscv/riscv_smmpt.c new file +191 -0
+
+static uint64_t mpte_get_perms(CPURISCVState *env, uint64_t mpte)
+{
+    RISCVMXL mxl = riscv_cpu_mxl(env);
+    mpte_union_t *u = (mpte_union_t *)&mpte;
+
+    return (mxl == MXL_RV32) ? u->leaf32.perms : u->leaf64.perms;
+}
+
+static bool mpte_check_nlnapot(CPURISCVState *env, uint64_t mpte, bool *nlnapot)
+{
+    RISCVMXL mxl = riscv_cpu_mxl(env);
+    mpte_union_t *u = (mpte_union_t *)&mpte;
+    if (mxl == MXL_RV32) {
+        *nlnapot = false;
+        return true;
+    }
+    *nlnapot = u->nonleaf64.n;
+    return u->nonleaf64.n ? (u->nonleaf64.ppn & 0x1ff) == 0x100 : true;
+}
+
+static uint64_t mpte_get_ppn(CPURISCVState *env, uint64_t mpte, int pn,
+                             bool nlnapot)
+{
+    RISCVMXL mxl = riscv_cpu_mxl(env);
+    mpte_union_t *u = (mpte_union_t *)&mpte;
+
+    if (nlnapot) {
+        return deposit64(u->nonleaf64.ppn, 0, 9, pn & 0x1ff);
+    }
+    return (mxl == MXL_RV32) ? u->nonleaf32.ppn : u->nonleaf64.ppn;
+}
+
+/* Caller should assert i before call this interface */
+static int mpt_get_pn(hwaddr addr, int i, mpt_mode_t mode)
+{
+    if (mode == SMMTT34) {
+        return i == 0
+            ? extract64(addr, 15, 10)
+            : extract64(addr, 25, 9);
+    } else {
+        int offset = 16 + i * 9;
+        if ((mode == SMMTT64) && (i == 4)) {
+            return extract64(addr, offset, 12);
+        } else {
+            return extract64(addr, offset, 9);
+        }
+    }
+}
+
+/* Caller should assert i before call this interface */
+static int mpt_get_pi(hwaddr addr, int i, mpt_mode_t mode)
+{
+    if (mode == SMMTT34) {
+        return i == 0
+            ? extract64(addr, 12, 3)
+            : extract64(addr, 22, 3);
+    } else {
+        int offset = 16 + i * 9;
+        return extract64(addr, offset - 4, 4);
+    }
+}
+
+static bool smmpt_lookup(CPURISCVState *env, hwaddr addr, mpt_mode_t mode,
+                         mpt_access_t *allowed_access,
+                         MMUAccessType access_type)
+{
+    MemTxResult res;
+    MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED;
+    CPUState *cs = env_cpu(env);
+    hwaddr mpte_addr, base = (hwaddr)env->mptppn << PGSHIFT;
+    load_entry_fn *load_entry;
+    uint32_t mptesize, levels, xwr;
+    int pn, pi, pmp_prot, pmp_ret;
+    uint64_t mpte, perms;
+
+    switch (mode) {
+    case SMMTT34:
+        load_entry = &load_entry_32; levels = 2; mptesize = 4; break;
+    case SMMTT43:
+        load_entry = &load_entry_64; levels = 3; mptesize = 8; break;
+        break;
+    case SMMTT52:
+        load_entry = &load_entry_64; levels = 4; mptesize = 8; break;
+    case SMMTT64:
+        load_entry = &load_entry_64; levels = 5; mptesize = 8; break;
+    case SMMTTBARE:
+        *allowed_access = ACCESS_ALLOW_RWX;
+        return true;
+    default:
+        g_assert_not_reached();
+        break;
+    }
+    for (int i = levels - 1; i >= 0 ; i--) {
+        /* 1. Get pn[i] as the mpt index */
+        pn = mpt_get_pn(addr, i, mode);
+        /* 2. Get mpte address and get mpte */
+        mpte_addr = base + pn * mptesize;
+        pmp_ret = get_physical_address_pmp(env, &pmp_prot, mpte_addr,
+                                           mptesize, MMU_DATA_LOAD, PRV_M);
+        if (pmp_ret != TRANSLATE_SUCCESS) {
+            return false;
+        }
+        mpte = load_entry(cs->as, mpte_addr, attrs, &res);
+        /* 3. Check valid bit and reserve bits of mpte */
+        if (!mpte_is_valid(mpte) || mpte_get_rsv(env, mpte)) {
+            return false;
+        }
+
+        /* 4. Process non-leaf node */
+        if (!mpte_is_leaf(mpte)) {
+            bool nlnapot = false;
+            if (i == 0) {
+                return false;
+            }
+            if (!mpte_check_nlnapot(env, mpte, &nlnapot)) {
+                return false;
+            }
+            base = mpte_get_ppn(env, mpte, pn, nlnapot) << PGSHIFT;
+            continue;
+        }
+
+        /* 5. Process leaf node */
+        pi = mpt_get_pi(addr, i, mode);
+        perms = mpte_get_perms(env, mpte);
+        xwr = (perms >> (pi * 3)) & 0x7;
+        switch (xwr) {
+        case ACCESS_ALLOW_R:
+            *allowed_access = ACCESS_ALLOW_R;
+            return access_type == MMU_DATA_LOAD;
+        case ACCESS_ALLOW_X:
+            *allowed_access = ACCESS_ALLOW_X;
+            return access_type == MMU_INST_FETCH;
+        case ACCESS_ALLOW_RX:
+            *allowed_access = ACCESS_ALLOW_R;
+            return (access_type == MMU_DATA_LOAD ||
+                    access_type == MMU_INST_FETCH);
+        case ACCESS_ALLOW_RW:
+            *allowed_access = ACCESS_ALLOW_RW;
+            return (access_type == MMU_DATA_LOAD ||
+                    access_type == MMU_DATA_STORE);
+        case ACCESS_ALLOW_RWX:
+            *allowed_access = ACCESS_ALLOW_RWX;
+            return true;
+        default:
+            return false;
+        }
+    }
+    return false;
+}
+
+bool smmpt_check_access(CPURISCVState *env, hwaddr addr,
+                        mpt_access_t *allowed_access, MMUAccessType access_type)
+{
+    bool mpt_has_access;
+    mpt_mode_t mode = env->mptmode;
+
+    mpt_has_access = smmpt_lookup(env, addr, mode,
+                                  allowed_access, access_type);
+    return mpt_has_access;
+}
+
+/*
+ * Convert MPT access to TLB page privilege.
+ */
+int smmpt_access_to_page_prot(mpt_access_t mpt_access)
+{
+    int prot;
+    switch (mpt_access) {
+    case ACCESS_ALLOW_R:
+        prot = PAGE_READ;
+        break;
+    case ACCESS_ALLOW_X:
+        prot = PAGE_EXEC;
+        break;
+    case ACCESS_ALLOW_RX:
+        prot = PAGE_READ | PAGE_EXEC;
+        break;
+    case ACCESS_ALLOW_RW:
+        prot = PAGE_READ | PAGE_WRITE;
+        break;
+    case ACCESS_ALLOW_RWX:
+        prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
+        break;
+    default:
+        prot = 0;
+        break;
+    }
+
+    return prot;
+}
target/riscv/riscv_smmpt.h new file +38 -0
new file mode 100644
index 0000000000..d5797c6168
@@ -0,0 +1,38 @@
+/*
+ * QEMU RISC-V Smmpt (Memory Protection Table)
+ *
+ * Copyright (c) 2024 Alibaba Group. All rights reserved.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef RISCV_SMPT_H
+#define RISCV_SMPT_H
+
+#include "cpu.h"
+#include "exec/mmu-access-type.h"
+
+typedef enum {
+    SMMTTBARE = 0,
+    SMMTT34   = 1,
+    SMMTT43   = 2,
+    SMMTT52   = 3,
+    SMMTT64   = 4,
+    SMMTTMAX
+} mpt_mode_t;
+
+typedef enum {
+    ACCESS_ALLOW_R = 0b001,
+    ACCESS_ALLOW_X = 0b100,
+    ACCESS_ALLOW_RX = 0b101 ,
+    ACCESS_ALLOW_RW = 0b011,
+    ACCESS_ALLOW_RWX = 0b111,
+} mpt_access_t;
+
+
+int smmpt_access_to_page_prot(mpt_access_t mpt_access);
+bool smmpt_check_access(CPURISCVState *env, hwaddr addr,
+                        mpt_access_t *allowed_access,
+                        MMUAccessType access_type);
+
+#endif
author
#6 lore raw

RFC PATCH 3/5 target/riscv: Integrate SMMPT checks into MMU and TLB fill

In reply to LIU Zhiwei

With the core MPT lookup logic in place, this patch integrates the permission checks into QEMU's main MMU processing functions.

A new helper, get_physical_address_mpt, is introduced to check the permissions for a given physical address against the MPT. This helper is then called at two critical points:

1. During page table walks (get_physical_address): The physical address of the Page Table Entry (PTE) itself is checked to ensure the supervisor has permission to read it.

2. After successful address translation (riscv_cpu_tlb_fill): The final guest-physical address is checked against the MPT before the access is allowed to proceed.

This ensures that SMMPT protection is enforced for both the translation process and the final memory access, as required by the specification.

---
 target/riscv/cpu_helper.c | 81 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 77 insertions(+), 4 deletions(-)
target/riscv/cpu_helper.c +77 -4
index 3479a62cc7..f8ca74ef61 100644
@@ -1089,9 +1089,8 @@ void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv, bool virt_en)
  * @access_type: The type of MMU access
  * @mode: Indicates current privilege level.
  */
-static int get_physical_address_pmp(CPURISCVState *env, int *prot, hwaddr addr,
-                                    int size, MMUAccessType access_type,
-                                    int mode)
+int get_physical_address_pmp(CPURISCVState *env, int *prot, hwaddr addr,
+                             int size, MMUAccessType access_type, int mode)
 {
     pmp_priv_t pmp_priv;
     bool pmp_has_privs;
@@ -1162,6 +1161,60 @@ static bool check_svukte_addr(CPURISCVState *env, vaddr addr)
     return !high_bit;
 }
 
+/*
+ * get_physical_address_mpt - check mpt permission for this physical address
+ *
+ * Lookup the Memory Protection Table and check permission for this
+ * physical address. Returns 0 if the permission checking was successful
+ *
+ * @env: CPURISCVState
+ * @prot: The returned protection attributes
+ * @addr: The physical address to be checked permission
+ * @access_type: The type of MMU access
+ * @mode: Indicates current privilege level.
+ */
+static int get_physical_address_mpt(CPURISCVState *env, int *prot, hwaddr addr,
+                                    MMUAccessType access_type, int mode)
+{
+    mpt_access_t mpt_access;
+    bool mpt_has_access;
+
+    /*
+     * If the extension is not supported or the mmpt.mode is Bare,
+     * there is no protection, return success.
+     */
+    if (!riscv_cpu_cfg(env)->ext_smmpt || env->mptmode == 0) {
+        *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
+        return TRANSLATE_SUCCESS;
+    }
+
+    /*
+     * MPT is checked for all accesses to physical memory, unless the
+     * effective privilege mode is M.
+     *
+     * Data accesses in M-mode when the MPRV bit in mstatus is set and
+     * the MPP field in mstatus contains S or U are subject to MPT checks.
+     *
+     * In riscv_env_mmu_index, The MPRV and MPP bits are already checked and
+     * encoded to mmu_idx, So we do not need to check it here.
+     */
+    if (mode == PRV_M) {
+        *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
+        return TRANSLATE_SUCCESS;
+    }
+
+    mpt_has_access = smmpt_check_access(env, addr,
+                                      &mpt_access, access_type);
+    if (!mpt_has_access) {
+        *prot = 0;
+        return TRANSLATE_MPT_FAIL;
+    }
+
+    *prot = smmpt_access_to_page_prot(mpt_access);
+
+    return TRANSLATE_SUCCESS;
+}
+
 /*
  * get_physical_address - get the physical address for this virtual address
  *
@@ -1356,6 +1409,13 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,
             pte_addr = base + idx * ptesize;
         }
 
+        int mpt_prot;
+        int mpt_ret = get_physical_address_mpt(env, &mpt_prot, pte_addr,
+                                               MMU_DATA_LOAD, PRV_S);
+        if (mpt_ret != TRANSLATE_SUCCESS) {
+            return TRANSLATE_MPT_FAIL;
+        }
+
         int pmp_prot;
         int pmp_ret = get_physical_address_pmp(env, &pmp_prot, pte_addr,
                                                sxlen_bytes,
@@ -1766,7 +1826,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
     CPURISCVState *env = &cpu->env;
     vaddr im_address;
     hwaddr pa = 0;
-    int prot, prot2, prot_pmp;
+    int prot, prot2, prot_pmp, mpt_prot;
     bool pmp_violation = false;
     bool first_stage_error = true;
     bool two_stage_lookup = mmuidx_2stage(mmu_idx);
@@ -1820,6 +1880,13 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
             prot &= prot2;
 
             if (ret == TRANSLATE_SUCCESS) {
+                ret = get_physical_address_mpt(env, &mpt_prot, pa,
+                                               access_type, mode);
+                qemu_log_mask(CPU_LOG_MMU,
+                              "%s MPT address=" HWADDR_FMT_plx " ret %d prot"
+                              " %d\n",
+                              __func__, pa, ret, mpt_prot);
+                prot &= mpt_prot;
                 ret = get_physical_address_pmp(env, &prot_pmp, pa,
                                                size, access_type, mode);
                 tlb_size = pmp_get_tlb_size(env, pa);
@@ -1855,6 +1922,12 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
                       __func__, address, ret, pa, prot);
 
         if (ret == TRANSLATE_SUCCESS) {
+            ret = get_physical_address_mpt(env, &mpt_prot, pa,
+                                           access_type, mode);
+            qemu_log_mask(CPU_LOG_MMU,
+                          "%s MPT address=" HWADDR_FMT_plx " ret %d prot %d\n",
+                          __func__, pa, ret, mpt_prot);
+            prot &= mpt_prot;
             ret = get_physical_address_pmp(env, &prot_pmp, pa,
                                            size, access_type, mode);
             tlb_size = pmp_get_tlb_size(env, pa);
Signature · 1 line
2.25.1
Reviewed-by on v1 3/5
#7 lore raw

Re: [RFC PATCH 3/5] target/riscv: Integrate SMMPT checks into MMU and TLB fill

Quoted 15 lines · LIU Zhiwei

With the core MPT lookup logic in place, this patch integrates the permission checks into QEMU's main MMU processing functions.

A new helper, get_physical_address_mpt, is introduced to check the permissions for a given physical address against the MPT. This helper is then called at two critical points:

1. During page table walks (get_physical_address): The physical address of the Page Table Entry (PTE) itself is checked to ensure the supervisor has permission to read it.

2. After successful address translation (riscv_cpu_tlb_fill): The final guest-physical address is checked against the MPT before the access is allowed to proceed.

Quoted 7 lines

This ensures that SMMPT protection is enforced for both the translation process and the final memory access, as required by the specification.

---

Quoted 130 lines
  target/riscv/cpu_helper.c | 81 +++++++++++++++++++++++++++++++++++++--
  1 file changed, 77 insertions(+), 4 deletions(-)
target/riscv/cpu_helper.c +77 -4
index 3479a62cc7..f8ca74ef61 100644
@@ -1089,9 +1089,8 @@ void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv, bool virt_en)
   * @access_type: The type of MMU access
   * @mode: Indicates current privilege level.
   */
-static int get_physical_address_pmp(CPURISCVState *env, int *prot, hwaddr addr,
-                                    int size, MMUAccessType access_type,
-                                    int mode)
+int get_physical_address_pmp(CPURISCVState *env, int *prot, hwaddr addr,
+                             int size, MMUAccessType access_type, int mode)
  {
      pmp_priv_t pmp_priv;
      bool pmp_has_privs;
@@ -1162,6 +1161,60 @@ static bool check_svukte_addr(CPURISCVState *env, vaddr addr)
      return !high_bit;
  }
 
+/*
+ * get_physical_address_mpt - check mpt permission for this physical address
+ *
+ * Lookup the Memory Protection Table and check permission for this
+ * physical address. Returns 0 if the permission checking was successful
+ *
+ * @env: CPURISCVState
+ * @prot: The returned protection attributes
+ * @addr: The physical address to be checked permission
+ * @access_type: The type of MMU access
+ * @mode: Indicates current privilege level.
+ */
+static int get_physical_address_mpt(CPURISCVState *env, int *prot, hwaddr addr,
+                                    MMUAccessType access_type, int mode)
+{
+    mpt_access_t mpt_access;
+    bool mpt_has_access;
+
+    /*
+     * If the extension is not supported or the mmpt.mode is Bare,
+     * there is no protection, return success.
+     */
+    if (!riscv_cpu_cfg(env)->ext_smmpt || env->mptmode == 0) {
+        *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
+        return TRANSLATE_SUCCESS;
+    }
+
+    /*
+     * MPT is checked for all accesses to physical memory, unless the
+     * effective privilege mode is M.
+     *
+     * Data accesses in M-mode when the MPRV bit in mstatus is set and
+     * the MPP field in mstatus contains S or U are subject to MPT checks.
+     *
+     * In riscv_env_mmu_index, The MPRV and MPP bits are already checked and
+     * encoded to mmu_idx, So we do not need to check it here.
+     */
+    if (mode == PRV_M) {
+        *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
+        return TRANSLATE_SUCCESS;
+    }
+
+    mpt_has_access = smmpt_check_access(env, addr,
+                                      &mpt_access, access_type);
+    if (!mpt_has_access) {
+        *prot = 0;
+        return TRANSLATE_MPT_FAIL;
+    }
+
+    *prot = smmpt_access_to_page_prot(mpt_access);
+
+    return TRANSLATE_SUCCESS;
+}
+
  /*
   * get_physical_address - get the physical address for this virtual address
   *
@@ -1356,6 +1409,13 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,
              pte_addr = base + idx * ptesize;
          }
 
+        int mpt_prot;
+        int mpt_ret = get_physical_address_mpt(env, &mpt_prot, pte_addr,
+                                               MMU_DATA_LOAD, PRV_S);
+        if (mpt_ret != TRANSLATE_SUCCESS) {
+            return TRANSLATE_MPT_FAIL;
+        }
+
          int pmp_prot;
          int pmp_ret = get_physical_address_pmp(env, &pmp_prot, pte_addr,
                                                 sxlen_bytes,
@@ -1766,7 +1826,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
      CPURISCVState *env = &cpu->env;
      vaddr im_address;
      hwaddr pa = 0;
-    int prot, prot2, prot_pmp;
+    int prot, prot2, prot_pmp, mpt_prot;
      bool pmp_violation = false;
      bool first_stage_error = true;
      bool two_stage_lookup = mmuidx_2stage(mmu_idx);
@@ -1820,6 +1880,13 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
              prot &= prot2;
 
              if (ret == TRANSLATE_SUCCESS) {
+                ret = get_physical_address_mpt(env, &mpt_prot, pa,
+                                               access_type, mode);
+                qemu_log_mask(CPU_LOG_MMU,
+                              "%s MPT address=" HWADDR_FMT_plx " ret %d prot"
+                              " %d\n",
+                              __func__, pa, ret, mpt_prot);
+                prot &= mpt_prot;
                  ret = get_physical_address_pmp(env, &prot_pmp, pa,
                                                 size, access_type, mode);
                  tlb_size = pmp_get_tlb_size(env, pa);
@@ -1855,6 +1922,12 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
                        __func__, address, ret, pa, prot);
 
          if (ret == TRANSLATE_SUCCESS) {
+            ret = get_physical_address_mpt(env, &mpt_prot, pa,
+                                           access_type, mode);
+            qemu_log_mask(CPU_LOG_MMU,
+                          "%s MPT address=" HWADDR_FMT_plx " ret %d prot %d\n",
+                          __func__, pa, ret, mpt_prot);
+            prot &= mpt_prot;
              ret = get_physical_address_pmp(env, &prot_pmp, pa,
                                             size, access_type, mode);
              tlb_size = pmp_get_tlb_size(env, pa);
author
#8 lore raw

RFC PATCH 4/5 target/riscv: Implement SMMPT fence instructions

In reply to LIU Zhiwei

This patch completes the SMMPT implementation by adding support for the new fence instructions: mfence.spa and minval.spa.

According to the specification, these instructions act as memory ordering fences for MPT updates. In QEMU's TCG model, this is conservatively implemented by flushing the entire TLB, which ensures that any subsequent memory accesses will re-evaluate permissions and see the effects of any prior MPT modifications.

The instructions are privileged and will cause an illegal instruction exception if executed outside of M-mode.

---
 target/riscv/insn32.decode                    |  2 ++
 .../riscv/insn_trans/trans_privileged.c.inc   | 30 +++++++++++++++++++
 2 files changed, 32 insertions(+)
target/riscv/insn32.decode +2 -0
index cd23b1f3a9..cf58f1beee 100644
@@ -120,6 +120,8 @@ sret        0001000    00010 00000 000 00000 1110011
 mret        0011000    00010 00000 000 00000 1110011
 wfi         0001000    00101 00000 000 00000 1110011
 sfence_vma  0001001    ..... ..... 000 00000 1110011 @sfence_vma
+mfence_spa  1000011    ..... ..... 000 00000 1110011 @sfence_vma
+minval_spa  0000011    ..... ..... 000 00000 1110011 @sfence_vma
 
 # *** NMI ***
 mnret       0111000    00010 00000 000 00000 1110011
target/riscv/insn_trans/trans_privileged.c.inc +30 -0
index 8a62b4cfcd..5ec6bf5991 100644
@@ -160,3 +160,33 @@ static bool trans_sfence_vma(DisasContext *ctx, arg_sfence_vma *a)
 #endif
     return false;
 }
+
+#define REQUIRE_SMSDID(ctx) do {          \
+    if (!ctx->cfg_ptr->ext_smsdid) {      \
+        return false;                     \
+    }                                     \
+} while (0)
+
+static bool do_mfence_spa(DisasContext *ctx)
+{
+#ifndef CONFIG_USER_ONLY
+    REQUIRE_SMSDID(ctx);
+    if (ctx->priv != PRV_M) {
+        return false;
+    }
+    decode_save_opc(ctx, 0);
+    gen_helper_tlb_flush_all(tcg_env);
+    return true;
+#endif
+    return false;
+}
+
+static bool trans_mfence_spa(DisasContext *ctx, arg_mfence_spa *a)
+{
+    return do_mfence_spa(ctx);
+}
+
+static bool trans_minval_spa(DisasContext *ctx, arg_minval_spa *a)
+{
+    return do_mfence_spa(ctx);
+}
Signature · 1 line
2.25.1
Reviewed-by on v1 4/5
#9 lore raw

Re: [RFC PATCH 4/5] target/riscv: Implement SMMPT fence instructions

Quoted 9 lines · LIU Zhiwei

This patch completes the SMMPT implementation by adding support for the new fence instructions: mfence.spa and minval.spa.

According to the specification, these instructions act as memory ordering fences for MPT updates. In QEMU's TCG model, this is conservatively implemented by flushing the entire TLB, which ensures that any subsequent memory accesses will re-evaluate permissions and see the effects of any prior MPT modifications.

Quoted 7 lines

The instructions are privileged and will cause an illegal instruction exception if executed outside of M-mode.

---

Quoted 55 lines
  target/riscv/insn32.decode                    |  2 ++
  .../riscv/insn_trans/trans_privileged.c.inc   | 30 +++++++++++++++++++
  2 files changed, 32 insertions(+)
target/riscv/insn32.decode +2 -0
index cd23b1f3a9..cf58f1beee 100644
@@ -120,6 +120,8 @@ sret        0001000    00010 00000 000 00000 1110011
  mret        0011000    00010 00000 000 00000 1110011
  wfi         0001000    00101 00000 000 00000 1110011
  sfence_vma  0001001    ..... ..... 000 00000 1110011 @sfence_vma
+mfence_spa  1000011    ..... ..... 000 00000 1110011 @sfence_vma
+minval_spa  0000011    ..... ..... 000 00000 1110011 @sfence_vma
 
  # *** NMI ***
  mnret       0111000    00010 00000 000 00000 1110011
target/riscv/insn_trans/trans_privileged.c.inc +30 -0
index 8a62b4cfcd..5ec6bf5991 100644
@@ -160,3 +160,33 @@ static bool trans_sfence_vma(DisasContext *ctx, arg_sfence_vma *a)
  #endif
      return false;
  }
+
+#define REQUIRE_SMSDID(ctx) do {          \
+    if (!ctx->cfg_ptr->ext_smsdid) {      \
+        return false;                     \
+    }                                     \
+} while (0)
+
+static bool do_mfence_spa(DisasContext *ctx)
+{
+#ifndef CONFIG_USER_ONLY
+    REQUIRE_SMSDID(ctx);
+    if (ctx->priv != PRV_M) {
+        return false;
+    }
+    decode_save_opc(ctx, 0);
+    gen_helper_tlb_flush_all(tcg_env);
+    return true;
+#endif
+    return false;
+}
+
+static bool trans_mfence_spa(DisasContext *ctx, arg_mfence_spa *a)
+{
+    return do_mfence_spa(ctx);
+}
+
+static bool trans_minval_spa(DisasContext *ctx, arg_minval_spa *a)
+{
+    return do_mfence_spa(ctx);
+}
author
#10 lore raw

RFC PATCH 5/5 target/riscv: Enable SMMPT extension

In reply to LIU Zhiwei
---
 target/riscv/cpu.c | 4 ++++
 1 file changed, 4 insertions(+)
target/riscv/cpu.c +4 -0
index d055ddf462..eea0942cf5 100644
@@ -204,7 +204,9 @@ const RISCVIsaExtData isa_edata_arr[] = {
     ISA_EXT_DATA_ENTRY(smdbltrp, PRIV_VERSION_1_13_0, ext_smdbltrp),
     ISA_EXT_DATA_ENTRY(smepmp, PRIV_VERSION_1_12_0, ext_smepmp),
     ISA_EXT_DATA_ENTRY(smrnmi, PRIV_VERSION_1_12_0, ext_smrnmi),
+    ISA_EXT_DATA_ENTRY(smsdid, PRIV_VERSION_1_13_0, ext_smsdid),
     ISA_EXT_DATA_ENTRY(smmpm, PRIV_VERSION_1_13_0, ext_smmpm),
+    ISA_EXT_DATA_ENTRY(smmpt, PRIV_VERSION_1_13_0, ext_smmpt),
     ISA_EXT_DATA_ENTRY(smnpm, PRIV_VERSION_1_13_0, ext_smnpm),
     ISA_EXT_DATA_ENTRY(smstateen, PRIV_VERSION_1_12_0, ext_smstateen),
     ISA_EXT_DATA_ENTRY(ssaia, PRIV_VERSION_1_12_0, ext_ssaia),
@@ -1279,6 +1281,8 @@ const RISCVCPUMultiExtConfig riscv_cpu_extensions[] = {
     MULTI_EXT_CFG_BOOL("smmpm", ext_smmpm, false),
     MULTI_EXT_CFG_BOOL("smnpm", ext_smnpm, false),
     MULTI_EXT_CFG_BOOL("smstateen", ext_smstateen, false),
+    MULTI_EXT_CFG_BOOL("smsdid", ext_smsdid, false),
+    MULTI_EXT_CFG_BOOL("smmpt", ext_smmpt, false),
     MULTI_EXT_CFG_BOOL("ssaia", ext_ssaia, false),
     MULTI_EXT_CFG_BOOL("ssdbltrp", ext_ssdbltrp, false),
     MULTI_EXT_CFG_BOOL("svade", ext_svade, false),
Signature · 1 line
2.25.1
#11 lore raw

Re: [RFC PATCH 5/5] target/riscv: Enable SMMPT extension

Quoted 12 lines · LIU Zhiwei
---
  target/riscv/cpu.c | 4 ++++
  1 file changed, 4 insertions(+)
target/riscv/cpu.c +0 -0
index d055ddf462..eea0942cf5 100644

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

Quoted 6 lines
target/riscv/cpu.c +2 -0
@@ -204,7 +204,9 @@ const RISCVIsaExtData isa_edata_arr[] = {
      ISA_EXT_DATA_ENTRY(smdbltrp, PRIV_VERSION_1_13_0, ext_smdbltrp),
      ISA_EXT_DATA_ENTRY(smepmp, PRIV_VERSION_1_12_0, ext_smepmp),
      ISA_EXT_DATA_ENTRY(smrnmi, PRIV_VERSION_1_12_0, ext_smrnmi),
+    ISA_EXT_DATA_ENTRY(smsdid, PRIV_VERSION_1_13_0, ext_smsdid),
      ISA_EXT_DATA_ENTRY(smmpm, PRIV_VERSION_1_13_0, ext_smmpm),
+    ISA_EXT_DATA_ENTRY(smmpt, PRIV_VERSION_1_13_0, ext_smmpt),

smmpt should be before smsdid. The ordering of Z extensions has that weird priority ordering, but S extensions should be ordered alphabetically.

Yes, there are some extensions that are already out of order (smmpm, smnpm ...). We'll have to patch them back to order in a separated patch, but for now let's not add to the problem hehe

Thanks,

Daniel

Quoted 12 lines
target/riscv/cpu.c +2 -0
      ISA_EXT_DATA_ENTRY(smnpm, PRIV_VERSION_1_13_0, ext_smnpm),
      ISA_EXT_DATA_ENTRY(smstateen, PRIV_VERSION_1_12_0, ext_smstateen),
      ISA_EXT_DATA_ENTRY(ssaia, PRIV_VERSION_1_12_0, ext_ssaia),
@@ -1279,6 +1281,8 @@ const RISCVCPUMultiExtConfig riscv_cpu_extensions[] = {
      MULTI_EXT_CFG_BOOL("smmpm", ext_smmpm, false),
      MULTI_EXT_CFG_BOOL("smnpm", ext_smnpm, false),
      MULTI_EXT_CFG_BOOL("smstateen", ext_smstateen, false),
+    MULTI_EXT_CFG_BOOL("smsdid", ext_smsdid, false),
+    MULTI_EXT_CFG_BOOL("smmpt", ext_smmpt, false),
      MULTI_EXT_CFG_BOOL("ssaia", ext_ssaia, false),
      MULTI_EXT_CFG_BOOL("ssdbltrp", ext_ssdbltrp, false),
      MULTI_EXT_CFG_BOOL("svade", ext_svade, false),
author
#12 lore raw

Re: [RFC PATCH 5/5] target/riscv: Enable SMMPT extension

Quoted 10 lines · Daniel Henrique Barboza
Quoted 7 lines · LIU Zhiwei

---
  target/riscv/cpu.c | 4 ++++

  1 file changed, 4 insertions(+)
Quoted 13 lines

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

Quoted 11 lines
target/riscv/cpu.c +2 -0
index d055ddf462..eea0942cf5 100644
@@ -204,7 +204,9 @@ const RISCVIsaExtData isa_edata_arr[] = {
       ISA_EXT_DATA_ENTRY(smdbltrp, PRIV_VERSION_1_13_0, ext_smdbltrp),
       ISA_EXT_DATA_ENTRY(smepmp, PRIV_VERSION_1_12_0, ext_smepmp),
       ISA_EXT_DATA_ENTRY(smrnmi, PRIV_VERSION_1_12_0, ext_smrnmi),
+    ISA_EXT_DATA_ENTRY(smsdid, PRIV_VERSION_1_13_0, ext_smsdid),
       ISA_EXT_DATA_ENTRY(smmpm, PRIV_VERSION_1_13_0, ext_smmpm),
+    ISA_EXT_DATA_ENTRY(smmpt, PRIV_VERSION_1_13_0, ext_smmpt),

smmpt should be before smsdid.

OK. I miss it.  I will also make it a experiment extension.

Thanks,

Zhiwei

Quoted 26 lines

The ordering of Z extensions has that weird priority ordering, but S extensions should be ordered alphabetically.

Yes, there are some extensions that are already out of order (smmpm, smnpm ...). We'll have to patch them back to order in a separated patch, but for now let's not add to the problem hehe

Thanks,

Daniel

Quoted 13 lines
target/riscv/cpu.c +2 -0
       ISA_EXT_DATA_ENTRY(smnpm, PRIV_VERSION_1_13_0, ext_smnpm),
       ISA_EXT_DATA_ENTRY(smstateen, PRIV_VERSION_1_12_0, ext_smstateen),
       ISA_EXT_DATA_ENTRY(ssaia, PRIV_VERSION_1_12_0, ext_ssaia),
@@ -1279,6 +1281,8 @@ const RISCVCPUMultiExtConfig 
 riscv_cpu_extensions[] = {
       MULTI_EXT_CFG_BOOL("smmpm", ext_smmpm, false),
       MULTI_EXT_CFG_BOOL("smnpm", ext_smnpm, false),
       MULTI_EXT_CFG_BOOL("smstateen", ext_smstateen, false),
+    MULTI_EXT_CFG_BOOL("smsdid", ext_smsdid, false),
+    MULTI_EXT_CFG_BOOL("smmpt", ext_smmpt, false),
       MULTI_EXT_CFG_BOOL("ssaia", ext_ssaia, false),
       MULTI_EXT_CFG_BOOL("ssdbltrp", ext_ssdbltrp, false),

      MULTI_EXT_CFG_BOOL("svade", ext_svade, false),

#13 lore raw
In reply to LIU Zhiwei

Hi!

Quoted 9 lines · LIU Zhiwei

This patch set introduces support for the RISC-V Smmpt (Supervisor Domain Access Protection) extension. It only includes two sub-extensions:
SMSDID and SMMPT.

This patch set implements the v0.3.4 version of Smmpt
(https://github.com/riscv/riscv-smmtt/releases/tag/v0.3.4).

As there are newer SMMPT specification versions, this patch set is not intend for merging.

I'm not sure I understood. Do you mean this patch set isn't supposed to be merged?

Daniel

Quoted 49 lines

The implementation is broken down into a series of logical steps:

Patch 1 adds the fundamental definitions for the Smmpt extension, including
new CSRs (mmpt, msdcfg), their bit-field layouts, and the corresponding CPU
configuration flags (ext_smmpt, ext_smsdid).

Patch 2 introduces the core logic for Memory Protection Table (MPT) lookups.
It includes a new file, riscv_smmpt.c, which implements the multi-level table walk to determine permissions for a given physical address.

Patch 3 integrates the MPT permission checks into the main MMU and TLB handling pathways. This ensures that both page table walks and final data accesses are subject to Smmpt protection rules.

Patch 4 adds support for the new fence instructions defined by the Smmpt extension, specifically mfence.spa and minval.spa.

Patch 5 enables smmpt and smsdia extendion.

With this series, QEMU can now model systems that utilize the Smmpt extension for enhanced memory security.

LIU Zhiwei (5):
target/riscv: Add basic definitions and CSRs for SMMPT target/riscv: Implement core SMMPT lookup logic target/riscv: Integrate SMMPT checks into MMU and TLB fill target/riscv: Implement SMMPT fence instructions target/riscv: Enable SMMPT extension

  target/riscv/cpu.c                            |   4 +
  target/riscv/cpu.h                            |   9 +-
  target/riscv/cpu_bits.h                       |  27 ++
  target/riscv/cpu_cfg_fields.h.inc             |   2 +
  target/riscv/cpu_helper.c                     |  81 +++++-
  target/riscv/csr.c                            |  83 ++++++
  target/riscv/insn32.decode                    |   2 +
  .../riscv/insn_trans/trans_privileged.c.inc   |  30 ++
  target/riscv/meson.build                      |   1 +
  target/riscv/pmp.h                            |   3 +
  target/riscv/riscv_smmpt.c                    | 273 ++++++++++++++++++
  target/riscv/riscv_smmpt.h                    |  38 +++
  12 files changed, 548 insertions(+), 5 deletions(-)

create mode 100644 target/riscv/riscv_smmpt.c
create mode 100644 target/riscv/riscv_smmpt.h

#14 lore raw
In reply to LIU Zhiwei

Hi!

Quoted 9 lines · LIU Zhiwei

This patch set introduces support for the RISC-V Smmpt (Supervisor Domain Access Protection) extension. It only includes two sub-extensions:
SMSDID and SMMPT.

This patch set implements the v0.3.4 version of Smmpt
(https://github.com/riscv/riscv-smmtt/releases/tag/v0.3.4).

As there are newer SMMPT specification versions, this patch set is not intend for merging.

I'm not sure I understood. Do you mean this patch set isn't supposed to be merged even after review?

Daniel

Quoted 49 lines

The implementation is broken down into a series of logical steps:

Patch 1 adds the fundamental definitions for the Smmpt extension, including
new CSRs (mmpt, msdcfg), their bit-field layouts, and the corresponding CPU
configuration flags (ext_smmpt, ext_smsdid).

Patch 2 introduces the core logic for Memory Protection Table (MPT) lookups.
It includes a new file, riscv_smmpt.c, which implements the multi-level table walk to determine permissions for a given physical address.

Patch 3 integrates the MPT permission checks into the main MMU and TLB handling pathways. This ensures that both page table walks and final data accesses are subject to Smmpt protection rules.

Patch 4 adds support for the new fence instructions defined by the Smmpt extension, specifically mfence.spa and minval.spa.

Patch 5 enables smmpt and smsdia extendion.

With this series, QEMU can now model systems that utilize the Smmpt extension for enhanced memory security.

LIU Zhiwei (5):
target/riscv: Add basic definitions and CSRs for SMMPT target/riscv: Implement core SMMPT lookup logic target/riscv: Integrate SMMPT checks into MMU and TLB fill target/riscv: Implement SMMPT fence instructions target/riscv: Enable SMMPT extension

  target/riscv/cpu.c                            |   4 +
  target/riscv/cpu.h                            |   9 +-
  target/riscv/cpu_bits.h                       |  27 ++
  target/riscv/cpu_cfg_fields.h.inc             |   2 +
  target/riscv/cpu_helper.c                     |  81 +++++-
  target/riscv/csr.c                            |  83 ++++++
  target/riscv/insn32.decode                    |   2 +
  .../riscv/insn_trans/trans_privileged.c.inc   |  30 ++
  target/riscv/meson.build                      |   1 +
  target/riscv/pmp.h                            |   3 +
  target/riscv/riscv_smmpt.c                    | 273 ++++++++++++++++++
  target/riscv/riscv_smmpt.h                    |  38 +++
  12 files changed, 548 insertions(+), 5 deletions(-)

create mode 100644 target/riscv/riscv_smmpt.c
create mode 100644 target/riscv/riscv_smmpt.h

author
#15 lore raw
Quoted 7 lines · Daniel Henrique Barboza

Hi!

Quoted 4 lines · LIU Zhiwei

This patch set introduces support for the RISC-V Smmpt (Supervisor Domain Access Protection) extension. It only includes two sub-extensions:
SMSDID and SMMPT.

Quoted 9 lines
Quoted 5 lines

This patch set implements the v0.3.4 version of Smmpt
(https://github.com/riscv/riscv-smmtt/releases/tag/v0.3.4).

As there are newer SMMPT specification versions, this patch set is not intend for merging.

I'm not sure I understood. Do you mean this patch set isn't supposed to be merged even after review?

Hi Daniel,

Thanks for the follow-up.

My primary goal is to have this feature merged eventually. I marked this version as "not for merging" because I'm aware it's based on an older specification, and I plan to update it to the latest one.

However, if the community finds this specific version (v0.3.4) useful for current development, such as for CoVE or other ongoing work, I am open to having it merged as is.

In any case, I will continue to work on an implementation aligned with the newest specification. I would greatly appreciate any review comments or feedback on the current approach.

Best regards,
Zhiwei

Quoted 53 lines

Daniel

Quoted 49 lines

The implementation is broken down into a series of logical steps:

Patch 1 adds the fundamental definitions for the Smmpt extension, including
new CSRs (mmpt, msdcfg), their bit-field layouts, and the corresponding CPU
configuration flags (ext_smmpt, ext_smsdid).

Patch 2 introduces the core logic for Memory Protection Table (MPT) lookups.
It includes a new file, riscv_smmpt.c, which implements the multi-level table walk to determine permissions for a given physical address.

Patch 3 integrates the MPT permission checks into the main MMU and TLB handling pathways. This ensures that both page table walks and final data accesses are subject to Smmpt protection rules.

Patch 4 adds support for the new fence instructions defined by the Smmpt extension, specifically mfence.spa and minval.spa.

Patch 5 enables smmpt and smsdia extendion.

With this series, QEMU can now model systems that utilize the Smmpt extension for enhanced memory security.

LIU Zhiwei (5):
   target/riscv: Add basic definitions and CSRs for SMMPT target/riscv: Implement core SMMPT lookup logic target/riscv: Integrate SMMPT checks into MMU and TLB fill target/riscv: Implement SMMPT fence instructions target/riscv: Enable SMMPT extension

  target/riscv/cpu.c                            |   4 + target/riscv/cpu.h                            |   9 +- target/riscv/cpu_bits.h                       |  27 ++ target/riscv/cpu_cfg_fields.h.inc             |   2 + target/riscv/cpu_helper.c                     |  81 +++++- target/riscv/csr.c                            |  83 ++++++ target/riscv/insn32.decode                    |   2 + .../riscv/insn_trans/trans_privileged.c.inc   |  30 ++ target/riscv/meson.build                      |   1 + target/riscv/pmp.h                            |   3 + target/riscv/riscv_smmpt.c                    | 273 ++++++++++++++++++ target/riscv/riscv_smmpt.h                    |  38 +++

  12 files changed, 548 insertions(+), 5 deletions(-)

  create mode 100644 target/riscv/riscv_smmpt.c
  create mode 100644 target/riscv/riscv_smmpt.h

#16 lore raw
Quoted 19 lines · LIU Zhiwei
Quoted 7 lines · Daniel Henrique Barboza

Hi!

Quoted 4 lines · LIU Zhiwei

This patch set introduces support for the RISC-V Smmpt (Supervisor Domain Access Protection) extension. It only includes two sub-extensions:
SMSDID and SMMPT.

Quoted 9 lines
Quoted 5 lines

This patch set implements the v0.3.4 version of Smmpt
(https://github.com/riscv/riscv-smmtt/releases/tag/v0.3.4).

As there are newer SMMPT specification versions, this patch set is not intend for merging.

I'm not sure I understood. Do you mean this patch set isn't supposed to be merged even after review?

Quoted 7 lines

Hi Daniel,

Thanks for the follow-up.

My primary goal is to have this feature merged eventually. I marked this version as "not for merging" because I'm aware it's based on an older specification, and I plan to update it to the latest one.

However, if the community finds this specific version (v0.3.4) useful for current development, such as for CoVE or other ongoing work, I am open to having it merged as is.

Having any implementation merged is better than no implementation. As long as we document it properly I don't see any problems. Let people use it for their own work and we can update it to a newer version as we go along.

Thanks,

Daniel

Quoted 59 lines

In any case, I will continue to work on an implementation aligned with the newest specification. I would greatly appreciate any review comments or feedback on the current approach.

Best regards,
Zhiwei

Quoted 53 lines

Daniel

Quoted 49 lines

The implementation is broken down into a series of logical steps:

Patch 1 adds the fundamental definitions for the Smmpt extension, including
new CSRs (mmpt, msdcfg), their bit-field layouts, and the corresponding CPU
configuration flags (ext_smmpt, ext_smsdid).

Patch 2 introduces the core logic for Memory Protection Table (MPT) lookups.
It includes a new file, riscv_smmpt.c, which implements the multi-level table walk to determine permissions for a given physical address.

Patch 3 integrates the MPT permission checks into the main MMU and TLB handling pathways. This ensures that both page table walks and final data accesses are subject to Smmpt protection rules.

Patch 4 adds support for the new fence instructions defined by the Smmpt extension, specifically mfence.spa and minval.spa.

Patch 5 enables smmpt and smsdia extendion.

With this series, QEMU can now model systems that utilize the Smmpt extension for enhanced memory security.

LIU Zhiwei (5):
   target/riscv: Add basic definitions and CSRs for SMMPT target/riscv: Implement core SMMPT lookup logic target/riscv: Integrate SMMPT checks into MMU and TLB fill target/riscv: Implement SMMPT fence instructions target/riscv: Enable SMMPT extension

  target/riscv/cpu.c                            |   4 + target/riscv/cpu.h                            |   9 +- target/riscv/cpu_bits.h                       |  27 ++ target/riscv/cpu_cfg_fields.h.inc             |   2 + target/riscv/cpu_helper.c                     |  81 +++++- target/riscv/csr.c                            |  83 ++++++ target/riscv/insn32.decode                    |   2 + .../riscv/insn_trans/trans_privileged.c.inc   |  30 ++ target/riscv/meson.build                      |   1 + target/riscv/pmp.h                            |   3 + target/riscv/riscv_smmpt.c                    | 273 ++++++++++++++++++ target/riscv/riscv_smmpt.h                    |  38 +++

  12 files changed, 548 insertions(+), 5 deletions(-)

  create mode 100644 target/riscv/riscv_smmpt.c
  create mode 100644 target/riscv/riscv_smmpt.h