Skip to contentSkip to conversation

target/riscv: Implement Smsdid and Smmpt extension

PATCH Superseded LIU Zhiwei 8 participants 37 messages Apr 8 – May 12

Subsystems (2)

v5 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

LIU Zhiwei

author
#0 lore raw

v5 cover target/riscv: Implement Smsdid and Smmpt extension

This patch set introduces support for the RISC-V Smmpt (Supervisor Memory-tracking and Protection Table) extension. Smmpt provides a hardware mechanism for fine-grained memory protection, checked after address translation, which is particularly useful for supervisor-level sandboxing and security monitoring.

The rfc patch set:
https://mail.gnu.org/archive/html/qemu-riscv/2025-09/msg00216.html

v4->v5:
1. Rebase to master.
v3->v4:
1. Add missing review tags.
v2->v3:
1. Fix build error in patch 2.
2. Rebase to master.

rfc->v2:
1. When ext_smmpt is false or BARE mode, make other fields in mmpt CSR zero.
2. Add patch 5 to fix smrnmi ISA string order.
3. Fix patch 6 smmpt and smsdid ISA string order.
4. Make smmpt and smsdid experiment extensions.
5. Add review tags.

LIU Zhiwei (6):
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: Fix smrnmi isa alphabetical order target/riscv: Enable SMMPT extension

 target/riscv/cpu.c                            |   6 +-
 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                            |  95 ++++++
 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                    | 274 ++++++++++++++++++
 target/riscv/riscv_smmpt.h                    |  36 +++
 12 files changed, 560 insertions(+), 6 deletions(-)
 create mode 100644 target/riscv/riscv_smmpt.c
 create mode 100644 target/riscv/riscv_smmpt.h
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v5 1/6 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                | 95 +++++++++++++++++++++++++++++++
 target/riscv/riscv_smmpt.h        | 21 +++++++
 5 files changed, 153 insertions(+), 1 deletion(-)
 create mode 100644 target/riscv/riscv_smmpt.h
target/riscv/cpu.h +8 -1
index 35d1f6362c..f8265c2cc2 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 */
@@ -181,6 +182,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
@@ -481,6 +483,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 cd1a5ec56b..80644948dd 100644
@@ -63,6 +63,8 @@ BOOL_FIELD(ext_smpmpmt)
 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 +95 -0

113 lines not on this page · raw message

target/riscv/riscv_smmpt.h new file +21 -0
new file mode 100644
index 0000000000..74dcccf4be
@@ -0,0 +1,21 @@
+/*
+ * 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_SMMPT_H
+#define RISCV_SMMPT_H
+
+typedef enum {
+    SMMPTBARE = 0,
+    SMMPT34   = 1,
+    SMMPT43   = 2,
+    SMMPT52   = 3,
+    SMMPT64   = 4,
+    SMMPTMAX
+} mpt_mode_t;
+
+#endif
Signature · 1 line
2.43.0
Acked-by on v5 1/6
#2 lore raw

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

Quoted 13 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.

Quoted 7 lines

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

Alistair

Quoted 242 lines
---
 target/riscv/cpu.h                |  9 ++-
 target/riscv/cpu_bits.h           | 27 +++++++++
 target/riscv/cpu_cfg_fields.h.inc |  2 +
 target/riscv/csr.c                | 95 +++++++++++++++++++++++++++++++
 target/riscv/riscv_smmpt.h        | 21 +++++++
 5 files changed, 153 insertions(+), 1 deletion(-)
 create mode 100644 target/riscv/riscv_smmpt.h
target/riscv/cpu.h +8 -1
index 35d1f6362c..f8265c2cc2 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 */
@@ -181,6 +182,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
@@ -481,6 +483,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 cd1a5ec56b..80644948dd 100644
@@ -63,6 +63,8 @@ BOOL_FIELD(ext_smpmpmt)
 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 +95 -0
index a75281539b..081d2fc2c3 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)
@@ -5446,6 +5455,89 @@ 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 -= SMMPT43 - SMMPT34;
+        }
+        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)
+{
+    uint32_t mode_value = 0;
+    if (!riscv_cpu_cfg(env)->ext_smmpt) {
+        goto set_remaining_fields_zero;
+    }
+
+    if (riscv_cpu_xlen(env) == 32) {
+        mode_value = (val & MMPT_MODE_MASK_32) >> MMPT_MODE_SHIFT_32;
+        /* If mode is bare, the remaining fields in mmpt must be zero */
+        if (mode_value == SMMPTBARE) {
+            goto set_remaining_fields_zero;
+        } else if (mode_value <= SMMPT34) {
+            /* Only write the legal value */
+            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) {
+        mode_value = (val & MMPT_MODE_MASK_64) >> MMPT_MODE_SHIFT_64;
+        if (mode_value == SMMPTBARE) {
+            goto set_remaining_fields_zero;
+        } else if (mode_value < SMMPTMAX) {
+            /* convert to mpt_mode_t */
+            mode_value += SMMPT43 - SMMPT34;
+            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;
+
+set_remaining_fields_zero:
+    env->sdid = 0;
+    env->mptmode = SMMPTBARE;
+    env->mptppn = 0;
+    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 */
@@ -6660,6 +6752,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 },
 
 #endif /* !CONFIG_USER_ONLY */
 };
target/riscv/riscv_smmpt.h new file +21 -1
new file mode 100644
index 0000000000..74dcccf4be
@@ -0,0 +1,21 @@
+/*
+ * 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_SMMPT_H
+#define RISCV_SMMPT_H
+
+typedef enum {
+    SMMPTBARE = 0,
+    SMMPT34   = 1,
+    SMMPT43   = 2,
+    SMMPT52   = 3,
+    SMMPT64   = 4,
+    SMMPTMAX
+} mpt_mode_t;
+
+#endif
--

2.43.0

Reviewed-by on v5 1/6
#3 lore raw

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

In reply to LIU Zhiwei

LIU Zhiwei <[email protected]> 於 2026年4月9日週四 上午3:39寫道:

Quoted 85 lines

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                | 95 +++++++++++++++++++++++++++++++
 target/riscv/riscv_smmpt.h        | 21 +++++++
 5 files changed, 153 insertions(+), 1 deletion(-)
 create mode 100644 target/riscv/riscv_smmpt.h
target/riscv/cpu.h +8 -1
index 35d1f6362c..f8265c2cc2 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 */
@@ -181,6 +182,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
@@ -481,6 +483,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 +14 -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

target/riscv/cpu_bits.h typedef enum CTRType { · line 1190

Quoted 10 lines
target/riscv/cpu_bits.h +10 -0
+#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

Hi Zhiwei,

MPTE_* fields are not used in the pachset.

Otherwise,

Quoted 168 lines
target/riscv/cpu_bits.h +3 -0
+
+#define MMPT_MODE_SHIFT_64  60
+#define MMPT_SDID_SHIFT_64  54
 #endif
target/riscv/cpu_cfg_fields.h.inc +2 -0
index cd1a5ec56b..80644948dd 100644
@@ -63,6 +63,8 @@ BOOL_FIELD(ext_smpmpmt)
 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 +95 -0
index a75281539b..081d2fc2c3 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)
@@ -5446,6 +5455,89 @@ 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 -= SMMPT43 - SMMPT34;
+        }
+        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)
+{
+    uint32_t mode_value = 0;
+    if (!riscv_cpu_cfg(env)->ext_smmpt) {
+        goto set_remaining_fields_zero;
+    }
+
+    if (riscv_cpu_xlen(env) == 32) {
+        mode_value = (val & MMPT_MODE_MASK_32) >> MMPT_MODE_SHIFT_32;
+        /* If mode is bare, the remaining fields in mmpt must be zero */
+        if (mode_value == SMMPTBARE) {
+            goto set_remaining_fields_zero;
+        } else if (mode_value <= SMMPT34) {
+            /* Only write the legal value */
+            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) {
+        mode_value = (val & MMPT_MODE_MASK_64) >> MMPT_MODE_SHIFT_64;
+        if (mode_value == SMMPTBARE) {
+            goto set_remaining_fields_zero;
+        } else if (mode_value < SMMPTMAX) {
+            /* convert to mpt_mode_t */
+            mode_value += SMMPT43 - SMMPT34;
+            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;
+
+set_remaining_fields_zero:
+    env->sdid = 0;
+    env->mptmode = SMMPTBARE;
+    env->mptppn = 0;
+    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 */
@@ -6660,6 +6752,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 },
 
 #endif /* !CONFIG_USER_ONLY */
 };
target/riscv/riscv_smmpt.h new file +21 -1
new file mode 100644
index 0000000000..74dcccf4be
@@ -0,0 +1,21 @@
+/*
+ * 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_SMMPT_H
+#define RISCV_SMMPT_H
+
+typedef enum {
+    SMMPTBARE = 0,
+    SMMPT34   = 1,
+    SMMPT43   = 2,
+    SMMPT52   = 3,
+    SMMPT64   = 4,
+    SMMPTMAX
+} mpt_mode_t;
+
+#endif
--

2.43.0

Reviewed-by on v5 1/6
#4 lore raw

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

In reply to LIU Zhiwei
Quoted 19 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.

Quoted 8 lines
---
  target/riscv/cpu.h                |  9 ++-
  target/riscv/cpu_bits.h           | 27 +++++++++
  target/riscv/cpu_cfg_fields.h.inc |  2 +
  target/riscv/csr.c                | 95 +++++++++++++++++++++++++++++++
  target/riscv/riscv_smmpt.h        | 21 +++++++
  5 files changed, 153 insertions(+), 1 deletion(-)

create mode 100644 target/riscv/riscv_smmpt.h

Thanks,
Nutty

author
#5 lore raw

PATCH v5 2/6 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 (SMMPT34, SMMPT43, 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/cpu_helper.c  |   5 +-
 target/riscv/meson.build   |   1 +
 target/riscv/pmp.h         |   3 +
 target/riscv/riscv_smmpt.c | 274 +++++++++++++++++++++++++++++++++++++
 target/riscv/riscv_smmpt.h |  15 ++
 5 files changed, 295 insertions(+), 3 deletions(-)
 create mode 100644 target/riscv/riscv_smmpt.c
target/riscv/cpu_helper.c +2 -3
index dd6c861a90..e3361fadae 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;
target/riscv/meson.build +1 -0
index 3842c7c1a8..bd1c2e8fad 100644
@@ -34,6 +34,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 467fb6b4b1..1ed574227c 100644
@@ -86,6 +86,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 +274 -0

274 lines not on this page · raw message

target/riscv/riscv_smmpt.h +15 -0
index 74dcccf4be..0d0597f8eb 100644
@@ -9,6 +9,9 @@
 #ifndef RISCV_SMMPT_H
 #define RISCV_SMMPT_H
 
+#include "cpu.h"
+#include "exec/mmu-access-type.h"
+
 typedef enum {
     SMMPTBARE = 0,
     SMMPT34   = 1,
@@ -18,4 +21,16 @@ typedef enum {
     SMMPTMAX
 } 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.43.0
#6 lore raw

Re: [PATCH v5 2/6] target/riscv: Implement core SMMPT lookup logic

Quoted 126 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 (SMMPT34, SMMPT43, 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/cpu_helper.c  |   5 +-
 target/riscv/meson.build   |   1 +
 target/riscv/pmp.h         |   3 +
 target/riscv/riscv_smmpt.c | 274 +++++++++++++++++++++++++++++++++++++
 target/riscv/riscv_smmpt.h |  15 ++
 5 files changed, 295 insertions(+), 3 deletions(-)
 create mode 100644 target/riscv/riscv_smmpt.c
target/riscv/cpu_helper.c +2 -3
index dd6c861a90..e3361fadae 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;
target/riscv/meson.build +1 -0
index 3842c7c1a8..bd1c2e8fad 100644
@@ -34,6 +34,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 467fb6b4b1..1ed574227c 100644
@@ -86,6 +86,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 +50 -0
new file mode 100644
index 0000000000..b7b47c5ae1
@@ -0,0 +1,274 @@
+/*
+ * 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;

target/riscv/riscv_smmpt.c line 60

Quoted 10 lines
target/riscv/riscv_smmpt.c new file +10 -0
+    } 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;

There is no guarantee in C that these bit fields are packed and laid out like you are expecting

Quoted 6 lines
target/riscv/riscv_smmpt.c new file +6 -0
+
+static inline bool mpte_is_leaf(uint64_t mpte)
+{
+   return mpte & 0x2;
+}
+

target/riscv/riscv_smmpt.c line 76

Quoted 10 lines
target/riscv/riscv_smmpt.c new file +10 -0
+static inline bool mpte_is_valid(uint64_t mpte)
+{
+    return mpte & 0x1;
+}
+
+static uint64_t mpte_get_rsv(CPURISCVState *env, uint64_t mpte)
+{
+    RISCVMXL mxl = riscv_cpu_mxl(env);
+    bool leaf = mpte_is_leaf(mpte);
+    mpte_union_t *u = (mpte_union_t *)&mpte;

So this cast

target/riscv/riscv_smmpt.c line 79

Quoted 3 lines
target/riscv/riscv_smmpt.c new file +3 -0
+
+    if (mxl == MXL_RV32) {
+        return leaf ? u->leaf32.rsv1 : u->nonleaf32.rsv1;

and these accesses are not valid.

On some platforms this will work, but on others it won't

See the notes here
https://en.cppreference.com/w/cpp/language/bit_field.html for more details

Alistair

#7 lore raw

Re: [PATCH v5 2/6] target/riscv: Implement core SMMPT lookup logic

Quoted 10 lines · Alistair Francis
Quoted 9 lines · LIU Zhiwei
target/riscv/riscv_smmpt.c new file +9 -0
+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;

...

Quoted 2 lines

There is no guarantee in C that these bit fields are packed and laid out like you are expecting

...

Quoted 7 lines

and these accesses are not valid.

On some platforms this will work, but on others it won't

See the notes here
https://en.cppreference.com/w/cpp/language/bit_field.html for more details

Indeed. Use hw/core/registerfields.h macros.

r~

author
#8 lore raw

Re: [PATCH v5 2/6] target/riscv: Implement core SMMPT lookup logic

In reply to Alistair Francis
Quoted 160 lines · Alistair Francis
Quoted 125 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 (SMMPT34, SMMPT43, 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/cpu_helper.c  |   5 +-
  target/riscv/meson.build   |   1 +
  target/riscv/pmp.h         |   3 +
  target/riscv/riscv_smmpt.c | 274 +++++++++++++++++++++++++++++++++++++
  target/riscv/riscv_smmpt.h |  15 ++
  5 files changed, 295 insertions(+), 3 deletions(-)

create mode 100644 target/riscv/riscv_smmpt.c

target/riscv/cpu_helper.c +2 -3
index dd6c861a90..e3361fadae 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;
target/riscv/meson.build +1 -0
index 3842c7c1a8..bd1c2e8fad 100644
@@ -34,6 +34,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 467fb6b4b1..1ed574227c 100644
@@ -86,6 +86,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 +50 -0
new file mode 100644
index 0000000000..b7b47c5ae1
@@ -0,0 +1,274 @@
+/*
+ * 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;

target/riscv/riscv_smmpt.c line 60

Quoted 10 lines
target/riscv/riscv_smmpt.c new file +10 -0
+    } 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;

There is no guarantee in C that these bit fields are packed and laid out like you are expecting

Quoted 6 lines
target/riscv/riscv_smmpt.c new file +6 -0
+
+static inline bool mpte_is_leaf(uint64_t mpte)
+{
+   return mpte & 0x2;
+}
+

target/riscv/riscv_smmpt.c line 76

Quoted 10 lines
target/riscv/riscv_smmpt.c new file +10 -0
+static inline bool mpte_is_valid(uint64_t mpte)
+{
+    return mpte & 0x1;
+}
+
+static uint64_t mpte_get_rsv(CPURISCVState *env, uint64_t mpte)
+{
+    RISCVMXL mxl = riscv_cpu_mxl(env);
+    bool leaf = mpte_is_leaf(mpte);
+    mpte_union_t *u = (mpte_union_t *)&mpte;

So this cast

target/riscv/riscv_smmpt.c line 79

Quoted 3 lines
target/riscv/riscv_smmpt.c new file +3 -0
+
+    if (mxl == MXL_RV32) {
+        return leaf ? u->leaf32.rsv1 : u->nonleaf32.rsv1;
Quoted 7 lines

and these accesses are not valid.

On some platforms this will work, but on others it won't

See the notes here
https://en.cppreference.com/w/cpp/language/bit_field.html for more details

Good point, thanks. You're right that using C bit-fields here is not portable and relies on implementation-defined layout. I'll replace the union/bit-field parsing with explicit bit extraction helpers based on extract64(), so the MPTE format is decoded in a host-independent way.

Thanks,
Zhiwei

Quoted 2 lines

Alistair

author
#9 lore raw

Re: [PATCH v5 2/6] target/riscv: Implement core SMMPT lookup logic

In reply to Alistair Francis

Hi Alistair,

Quoted 130 lines · Alistair Francis
Quoted 129 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 (SMMPT34, SMMPT43, 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/cpu_helper.c  |   5 +-
  target/riscv/meson.build   |   1 +
  target/riscv/pmp.h         |   3 +
  target/riscv/riscv_smmpt.c | 274 +++++++++++++++++++++++++++++++++++++
  target/riscv/riscv_smmpt.h |  15 ++
  5 files changed, 295 insertions(+), 3 deletions(-)

create mode 100644 target/riscv/riscv_smmpt.c

target/riscv/cpu_helper.c +2 -3
index dd6c861a90..e3361fadae 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;
target/riscv/meson.build +1 -0
index 3842c7c1a8..bd1c2e8fad 100644
@@ -34,6 +34,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 467fb6b4b1..1ed574227c 100644
@@ -86,6 +86,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 +54 -0
new file mode 100644
index 0000000000..b7b47c5ae1
@@ -0,0 +1,274 @@
+/*
+ * 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;
Quoted 8 lines

target/riscv/riscv_smmpt.c line 60

Quoted 6 lines
target/riscv/riscv_smmpt.c new file +6 -0
+        uint64_t rsv1:8;
+        uint64_t ppn:52;
+        uint64_t rsv2:1;
+        uint64_t n:1;
+    } nonleaf64;
+} mpte_union_t;

There is no guarantee in C that these bit fields are packed and laid out like you are expecting

Good catch! Thanks for pointing it out. I have used the explicit extraction method for bit fields in mpte in v6 patch set.

Thanks,
Zhiwei

Quoted 31 lines
Quoted 7 lines
target/riscv/riscv_smmpt.c new file +6 -0
+
+static inline bool mpte_is_leaf(uint64_t mpte)
+{
+   return mpte & 0x2;
+}
+

target/riscv/riscv_smmpt.c line 76

Quoted 10 lines
target/riscv/riscv_smmpt.c new file +10 -0
+static inline bool mpte_is_valid(uint64_t mpte)
+{
+    return mpte & 0x1;
+}
+
+static uint64_t mpte_get_rsv(CPURISCVState *env, uint64_t mpte)
+{
+    RISCVMXL mxl = riscv_cpu_mxl(env);
+    bool leaf = mpte_is_leaf(mpte);
+    mpte_union_t *u = (mpte_union_t *)&mpte;

So this cast

target/riscv/riscv_smmpt.c line 79

Quoted 3 lines
target/riscv/riscv_smmpt.c new file +3 -0
+
+    if (mxl == MXL_RV32) {
+        return leaf ? u->leaf32.rsv1 : u->nonleaf32.rsv1;

and these accesses are not valid.

On some platforms this will work, but on others it won't

See the notes here
https://en.cppreference.com/w/cpp/language/bit_field.html for more details

Alistair

#10 lore raw

Re: [PATCH v5 2/6] target/riscv: Implement core SMMPT lookup logic

In reply to LIU Zhiwei

Hi Zhiwei,

LIU Zhiwei <[email protected]> 於 2026年4月9日週四 上午3:24寫道:

Quoted 365 lines

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 (SMMPT34, SMMPT43, 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/cpu_helper.c  |   5 +-
 target/riscv/meson.build   |   1 +
 target/riscv/pmp.h         |   3 +
 target/riscv/riscv_smmpt.c | 274 +++++++++++++++++++++++++++++++++++++
 target/riscv/riscv_smmpt.h |  15 ++
 5 files changed, 295 insertions(+), 3 deletions(-)
 create mode 100644 target/riscv/riscv_smmpt.c
target/riscv/cpu_helper.c +2 -3
index dd6c861a90..e3361fadae 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;
target/riscv/meson.build +1 -0
index 3842c7c1a8..bd1c2e8fad 100644
@@ -34,6 +34,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 467fb6b4b1..1ed574227c 100644
@@ -86,6 +86,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 +274 -0
new file mode 100644
index 0000000000..b7b47c5ae1
@@ -0,0 +1,274 @@
+/*
+ * 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)
+{
+    RISCVMXL mxl = riscv_cpu_mxl(env);
+    bool leaf = mpte_is_leaf(mpte);
+    mpte_union_t *u = (mpte_union_t *)&mpte;
+
+    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;
+}
+
+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 == SMMPT34) {
+        return i == 0
+            ? extract64(addr, 15, 10)
+            : extract64(addr, 25, 9);
+    } else {
+        int offset = 16 + i * 9;
+        if ((mode == SMMPT64) && (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 == SMMPT34) {
+        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 SMMPT34:
+        load_entry = &load_entry_32; levels = 2; mptesize = 4; break;
+    case SMMPT43:
+        load_entry = &load_entry_64; levels = 3; mptesize = 8; break;
+        break;
+    case SMMPT52:
+        load_entry = &load_entry_64; levels = 4; mptesize = 8; break;
+    case SMMPT64:
+        load_entry = &load_entry_64; levels = 5; mptesize = 8; break;
+    case SMMPTBARE:
+        *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 +3 -0
index 74dcccf4be..0d0597f8eb 100644
@@ -9,6 +9,9 @@
 #ifndef RISCV_SMMPT_H
 #define RISCV_SMMPT_H
 
+#include "cpu.h"
+#include "exec/mmu-access-type.h"
+
 typedef enum {
     SMMPTBARE = 0,
     SMMPT34   = 1,

target/riscv/riscv_smmpt.h typedef enum { · line 30

Quoted 10 lines
target/riscv/riscv_smmpt.h +7 -0
@@ -18,4 +21,16 @@ typedef enum {
     SMMPTMAX
 } 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;

The definitions of ACCESS_ALLOW_R and ACCESS_ALLOW_X are identical to PAGE_READ and PAGE_EXEC.
I wonder if we really need to define an additional access type of mpt_access_t.

We can also save smmpt_access_to_page_prot() call if smmpt_check_access() returns
PAGE_READ, PAGE_WRITE, and PAGE_EXEC.

Regards,
Frank Chang

Quoted 10 lines
target/riscv/riscv_smmpt.h +5 -1
+
+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
--

2.43.0

author
#11 lore raw

Re: [PATCH v5 2/6] target/riscv: Implement core SMMPT lookup logic

Hi Frank,

Quoted 381 lines · Frank Chang

Hi Zhiwei,

LIU Zhiwei <[email protected]> 於 2026年4月9日週四 上午3:24寫道:

Quoted 364 lines

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 (SMMPT34, SMMPT43, 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/cpu_helper.c  |   5 +-
  target/riscv/meson.build   |   1 +
  target/riscv/pmp.h         |   3 +
  target/riscv/riscv_smmpt.c | 274 +++++++++++++++++++++++++++++++++++++
  target/riscv/riscv_smmpt.h |  15 ++
  5 files changed, 295 insertions(+), 3 deletions(-)

create mode 100644 target/riscv/riscv_smmpt.c

target/riscv/cpu_helper.c +2 -3
index dd6c861a90..e3361fadae 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;
target/riscv/meson.build +1 -0
index 3842c7c1a8..bd1c2e8fad 100644
@@ -34,6 +34,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 467fb6b4b1..1ed574227c 100644
@@ -86,6 +86,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 +274 -0
new file mode 100644
index 0000000000..b7b47c5ae1
@@ -0,0 +1,274 @@
+/*
+ * 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)
+{
+    RISCVMXL mxl = riscv_cpu_mxl(env);
+    bool leaf = mpte_is_leaf(mpte);
+    mpte_union_t *u = (mpte_union_t *)&mpte;
+
+    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;
+}
+
+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 == SMMPT34) {
+        return i == 0
+            ? extract64(addr, 15, 10)
+            : extract64(addr, 25, 9);
+    } else {
+        int offset = 16 + i * 9;
+        if ((mode == SMMPT64) && (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 == SMMPT34) {
+        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 SMMPT34:
+        load_entry = &load_entry_32; levels = 2; mptesize = 4; break;
+    case SMMPT43:
+        load_entry = &load_entry_64; levels = 3; mptesize = 8; break;
+        break;
+    case SMMPT52:
+        load_entry = &load_entry_64; levels = 4; mptesize = 8; break;
+    case SMMPT64:
+        load_entry = &load_entry_64; levels = 5; mptesize = 8; break;
+    case SMMPTBARE:
+        *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 +3 -0
index 74dcccf4be..0d0597f8eb 100644
@@ -9,6 +9,9 @@
  #ifndef RISCV_SMMPT_H
  #define RISCV_SMMPT_H
 
+#include "cpu.h"
+#include "exec/mmu-access-type.h"
+
  typedef enum {
      SMMPTBARE = 0,
      SMMPT34   = 1,

target/riscv/riscv_smmpt.h typedef enum { · line 30

Quoted 10 lines
target/riscv/riscv_smmpt.h +7 -0
@@ -18,4 +21,16 @@ typedef enum {
      SMMPTMAX
  } 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;

The definitions of ACCESS_ALLOW_R and ACCESS_ALLOW_X are identical to PAGE_READ and PAGE_EXEC.
I wonder if we really need to define an additional access type of mpt_access_t.

Quoted 3 lines

We can also save smmpt_access_to_page_prot() call if smmpt_check_access() returns
PAGE_READ, PAGE_WRITE, and PAGE_EXEC.

Good catch! Done as you advise in v6 patch set.

Thanks,
Zhiwei

Quoted 14 lines

Regards,
Frank Chang

Quoted 10 lines
target/riscv/riscv_smmpt.h +5 -1
+
+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
--

2.43.0

#12 lore raw

Re: [PATCH v5 2/6] target/riscv: Implement core SMMPT lookup logic

In reply to LIU Zhiwei

Hi Zhiwei,

Quoted 93 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 (SMMPT34, SMMPT43, 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/cpu_helper.c  |   5 +-
  target/riscv/meson.build   |   1 +
  target/riscv/pmp.h         |   3 +
  target/riscv/riscv_smmpt.c | 274 +++++++++++++++++++++++++++++++++++++
  target/riscv/riscv_smmpt.h |  15 ++
  5 files changed, 295 insertions(+), 3 deletions(-)

create mode 100644 target/riscv/riscv_smmpt.c

target/riscv/cpu_helper.c +2 -3
index dd6c861a90..e3361fadae 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;
target/riscv/meson.build +1 -0
index 3842c7c1a8..bd1c2e8fad 100644
@@ -34,6 +34,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 467fb6b4b1..1ed574227c 100644
@@ -86,6 +86,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 +18 -0
new file mode 100644
index 0000000000..b7b47c5ae1
@@ -0,0 +1,274 @@
+/*
+ * 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)

target/riscv/riscv_smmpt.c line 28

Quoted 10 lines
target/riscv/riscv_smmpt.c new file +10 -0
+{
+    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);
+}
+

So it seems like we have upstream changes that impacted this code.

(Phil, feel free to chime in).

When building this series I face this error:

FAILED: libqemu-riscv64-softmmu.a.p/target_riscv_riscv_smmpt.c.o
cc -Ilibqemu-riscv64-softmmu.a.p -I. -I.. -Itarget/riscv -I../target/riscv -Isubprojects/libvduse -I../subprojects/libvduse -Iqapi -Itrace -Iui -Iui/shader -I/usr/include/pixman-1 -I/usr/include/glib-2.0 -I/usr/lib/aarch64-linux-gnu/glib-2.0/include -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/gio-unix-2.0 -I/usr/include/slirp -fdiagnostics-color=auto -Wall -Winvalid-pch -Werror -std=gnu11 -O0 -g -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fno-common -fwrapv -ftrivial-auto-var-init=zero -fzero-call-used-regs=used-gpr -fstack-protector-strong -Wempty-body -Wendif-labels -Wexpansion-to-defined -Wformat-overflow=2 -Wformat-security -Wformat-y2k -Wignored-qualifiers -Wimplicit-fallthrough=2 -Winit-self -Wmissing-format-attribute -Wmissing-prototypes -Wnested-externs -Wold-style-declaration -Wold-style-definition -Wredundant-decls -Wshadow=local -Wstrict-prototypes -Wtype-limits -Wundef -Wvla -Wwrite-strings -Wno-missing-include-dirs -Wno-psabi -Wno-shift-negative-value -isystem /home/dbarboza/work/qemu/linux-headers -isystem linux-headers -iquote . -iquote /home/dbarboza/work/qemu -iquote /home/dbarboza/work/qemu/include -iquote /home/dbarboza/work/qemu/host/include/aarch64 -iquote /home/dbarboza/work/qemu/host/include/generic -iquote /home/dbarboza/work/qemu/tcg/aarch64 -pthread -fPIE -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=600 -DNCURSES_WIDECHAR=1 -isystem../linux-headers -isystemlinux-headers -DCOMPILING_PER_TARGET '-DCONFIG_TARGET="riscv64-softmmu-config-target.h"' '-DCONFIG_DEVICES="riscv64-softmmu-config-devices.h"' -MD -MQ libqemu-riscv64-softmmu.a.p/target_riscv_riscv_smmpt.c.o -MF libqemu-riscv64-softmmu.a.p/target_riscv_riscv_smmpt.c.o.d -o libqemu-riscv64-softmmu.a.p/target_riscv_riscv_smmpt.c.o -c ../target/riscv/riscv_smmpt.c ../target/riscv/riscv_smmpt.c: In function ‘load_entry_32’:
../target/riscv/riscv_smmpt.c:20:12: error: implicit declaration of function ‘address_space_ldl’; did you mean ‘address_space_ldub’? [-Werror=implicit-function-declaration]

   20 |     return address_space_ldl(as, addr, attrs, result);
      |            ^~~~~~~~~~~~~~~~~
      |            address_space_ldub
../target/riscv/riscv_smmpt.c:20:12: error: nested extern declaration of ‘address_space_ldl’ [-Werror=nested-externs]
../target/riscv/riscv_smmpt.c: In function ‘load_entry_64’:
../target/riscv/riscv_smmpt.c:26:12: error: implicit declaration of function ‘address_space_ldq’; did you mean ‘address_space_ldub’? [-Werror=implicit-function-declaration]
   26 |     return address_space_ldq(as, addr, attrs, result);
      |            ^~~~~~~~~~~~~~~~~
      |            address_space_ldub

../target/riscv/riscv_smmpt.c:26:12: error: nested extern declaration of ‘address_space_ldq’ [-Werror=nested-externs]
cc1: all warnings being treated as errors
[1638/2623] Generating docs/QEMU manual with a custom command
ninja: build stopped: subcommand failed.

I'm not sure why that is since the (seemly) appropriate header system/memmory.h is included.

With this change I can get it to build:

$ git diff
target/riscv/riscv_smmpt.c +2 -2
index b7b47c5ae1..ddc8cd9932 100644
@@ -17,13 +17,13 @@ typedef uint64_t load_entry_fn(AddressSpace *, hwaddr,
 static uint64_t load_entry_32(AddressSpace *as, hwaddr addr,
                               MemTxAttrs attrs, MemTxResult *result)
 {
-    return address_space_ldl(as, addr, attrs, result);
+    return address_space_ldl_le(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);
+    return address_space_ldq_le(as, addr, attrs, result);
 }
 
 typedef union {

I ***think*** this is a fair change since we're reading/writing memory using the same endianess, and our memory operations are usually LE, e.g. in get_physical_address:

        if (riscv_cpu_mxl(env) == MXL_RV32) {
            pte = address_space_ldl_le(cs->as, pte_addr, attrs, &res);
        } else {
            pte = address_space_ldq_le(cs->as, pte_addr, attrs, &res);
        }

I'll defer to you what we should do here. Thanks,

Daniel

Quoted 277 lines
target/riscv/riscv_smmpt.c new file +246 -0
+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)
+{
+    RISCVMXL mxl = riscv_cpu_mxl(env);
+    bool leaf = mpte_is_leaf(mpte);
+    mpte_union_t *u = (mpte_union_t *)&mpte;
+
+    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;
+}
+
+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 == SMMPT34) {
+        return i == 0
+            ? extract64(addr, 15, 10)
+            : extract64(addr, 25, 9);
+    } else {
+        int offset = 16 + i * 9;
+        if ((mode == SMMPT64) && (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 == SMMPT34) {
+        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 SMMPT34:
+        load_entry = &load_entry_32; levels = 2; mptesize = 4; break;
+    case SMMPT43:
+        load_entry = &load_entry_64; levels = 3; mptesize = 8; break;
+        break;
+    case SMMPT52:
+        load_entry = &load_entry_64; levels = 4; mptesize = 8; break;
+    case SMMPT64:
+        load_entry = &load_entry_64; levels = 5; mptesize = 8; break;
+    case SMMPTBARE:
+        *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 +15 -0
index 74dcccf4be..0d0597f8eb 100644
@@ -9,6 +9,9 @@
  #ifndef RISCV_SMMPT_H
  #define RISCV_SMMPT_H
 
+#include "cpu.h"
+#include "exec/mmu-access-type.h"
+
  typedef enum {
      SMMPTBARE = 0,
      SMMPT34   = 1,
@@ -18,4 +21,16 @@ typedef enum {
      SMMPTMAX
  } 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
#13 lore raw

Re: [PATCH v5 2/6] target/riscv: Implement core SMMPT lookup logic

Hi Daniel,

Quoted 196 lines · Daniel Henrique Barboza

Hi Zhiwei,

Quoted 98 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 (SMMPT34, SMMPT43, 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/cpu_helper.c  |   5 +- target/riscv/meson.build   |   1 +
  target/riscv/pmp.h         |   3 + target/riscv/riscv_smmpt.c | 274 +++++++++++++++++++++++++++++++++++++ target/riscv/riscv_smmpt.h |  15 ++

  5 files changed, 295 insertions(+), 3 deletions(-)

  create mode 100644 target/riscv/riscv_smmpt.c

target/riscv/cpu_helper.c +1 -3
index dd6c861a90..e3361fadae 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;

target/riscv/meson.build +0 -0
index 3842c7c1a8..bd1c2e8fad 100644
@@ -34,6 +34,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 467fb6b4b1..1ed574227c 100644
@@ -86,6 +86,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 +18 -0
new file mode 100644
index 0000000000..b7b47c5ae1
@@ -0,0 +1,274 @@
+/*
+ * 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)

target/riscv/riscv_smmpt.c line 28

Quoted 10 lines
target/riscv/riscv_smmpt.c new file +10 -0
+{
+    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);
+}
+

So it seems like we have upstream changes that impacted this code.

(Phil, feel free to chime in).

When building this series I face this error:

FAILED: libqemu-riscv64-softmmu.a.p/target_riscv_riscv_smmpt.c.o cc -Ilibqemu-riscv64-softmmu.a.p -I. -I.. -Itarget/riscv

(quoted diff) +0 -18
-I../target/riscv -Isubprojects/libvduse -I../subprojects/libvduse 
-Iqapi -Itrace -Iui -Iui/shader -I/usr/include/pixman-1 
-I/usr/include/glib-2.0 -I/usr/lib/aarch64-linux-gnu/glib-2.0/include 
-I/usr/include/libmount -I/usr/include/blkid 
-I/usr/include/gio-unix-2.0 -I/usr/include/slirp 
-fdiagnostics-color=auto -Wall -Winvalid-pch -Werror -std=gnu11 -O0 -g 
-D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE 
-fno-strict-aliasing -fno-common -fwrapv -ftrivial-auto-var-init=zero 
-fzero-call-used-regs=used-gpr -fstack-protector-strong -Wempty-body 
-Wendif-labels -Wexpansion-to-defined -Wformat-overflow=2 
-Wformat-security -Wformat-y2k -Wignored-qualifiers 
-Wimplicit-fallthrough=2 -Winit-self -Wmissing-format-attribute 
-Wmissing-prototypes -Wnested-externs -Wold-style-declaration 
-Wold-style-definition -Wredundant-decls -Wshadow=local 
-Wstrict-prototypes -Wtype-limits -Wundef -Wvla -Wwrite-strings 
-Wno-missing-include-dirs -Wno-psabi -Wno-shift-negative-value 
-isystem /home/dbarboza/work/qemu/linux-headers -isystem linux-headers 
-iquote . -iquote /home/dbarboza/work/qemu -iquote 

/home/dbarboza/work/qemu/include -iquote /home/dbarboza/work/qemu/host/include/aarch64 -iquote /home/dbarboza/work/qemu/host/include/generic -iquote /home/dbarboza/work/qemu/tcg/aarch64 -pthread -fPIE -D_DEFAULT_SOURCE

(quoted diff) +0 -2
-D_XOPEN_SOURCE=600 -DNCURSES_WIDECHAR=1 -isystem../linux-headers 
-isystemlinux-headers -DCOMPILING_PER_TARGET 

'-DCONFIG_TARGET="riscv64-softmmu-config-target.h"' '-DCONFIG_DEVICES="riscv64-softmmu-config-devices.h"' -MD -MQ libqemu-riscv64-softmmu.a.p/target_riscv_riscv_smmpt.c.o -MF libqemu-riscv64-softmmu.a.p/target_riscv_riscv_smmpt.c.o.d -o libqemu-riscv64-softmmu.a.p/target_riscv_riscv_smmpt.c.o -c ../target/riscv/riscv_smmpt.c ../target/riscv/riscv_smmpt.c: In function ‘load_entry_32’:
../target/riscv/riscv_smmpt.c:20:12: error: implicit declaration of function ‘address_space_ldl’; did you mean ‘address_space_ldub’? [-Werror=implicit-function-declaration]
   20 |     return address_space_ldl(as, addr, attrs, result);
      |            ^~~~~~~~~~~~~~~~~
      |            address_space_ldub
../target/riscv/riscv_smmpt.c:20:12: error: nested extern declaration of ‘address_space_ldl’ [-Werror=nested-externs]
../target/riscv/riscv_smmpt.c: In function ‘load_entry_64’:
../target/riscv/riscv_smmpt.c:26:12: error: implicit declaration of function ‘address_space_ldq’; did you mean ‘address_space_ldub’? [-Werror=implicit-function-declaration]
   26 |     return address_space_ldq(as, addr, attrs, result);
      |            ^~~~~~~~~~~~~~~~~
      |            address_space_ldub
../target/riscv/riscv_smmpt.c:26:12: error: nested extern declaration of ‘address_space_ldq’ [-Werror=nested-externs]
cc1: all warnings being treated as errors
[1638/2623] Generating docs/QEMU manual with a custom command
ninja: build stopped: subcommand failed.

I'm not sure why that is since the (seemly) appropriate header system/memmory.h is included.

With this change I can get it to build:

$ git diff
target/riscv/riscv_smmpt.c +2 -2
index b7b47c5ae1..ddc8cd9932 100644
@@ -17,13 +17,13 @@ typedef uint64_t load_entry_fn(AddressSpace *, 
 hwaddr,
  static uint64_t load_entry_32(AddressSpace *as, hwaddr addr,
                                MemTxAttrs attrs, MemTxResult *result)
  {
-    return address_space_ldl(as, addr, attrs, result);
+    return address_space_ldl_le(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);
+    return address_space_ldq_le(as, addr, attrs, result);
  }
 

 typedef union {

I ***think*** this is a fair change since we're reading/writing memory using the same endianess, and our memory operations are usually LE, e.g. in get_physical_address:

Quoted 8 lines

        if (riscv_cpu_mxl(env) == MXL_RV32) {
            pte = address_space_ldl_le(cs->as, pte_addr, attrs, &res); } else {
            pte = address_space_ldq_le(cs->as, pte_addr, attrs, &res); }

I'll defer to you what we should do here.  Thanks,

I see the same build issue based on the community master branch. And fixed as you suggested in V6 patch set.

Thanks,
Zhiwei

Quoted 283 lines

Daniel

Quoted 279 lines
target/riscv/riscv_smmpt.c new file +65 -0
+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)
+{
+    RISCVMXL mxl = riscv_cpu_mxl(env);
+    bool leaf = mpte_is_leaf(mpte);
+    mpte_union_t *u = (mpte_union_t *)&mpte;
+
+    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;
+}
+
+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)

target/riscv/riscv_smmpt.c new file +54 -0
+{
+    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 == SMMPT34) {
+        return i == 0
+            ? extract64(addr, 15, 10)
+            : extract64(addr, 25, 9);
+    } else {
+        int offset = 16 + i * 9;
+        if ((mode == SMMPT64) && (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 == SMMPT34) {
+        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)

target/riscv/riscv_smmpt.c new file +34 -0
+{
+    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 SMMPT34:
+        load_entry = &load_entry_32; levels = 2; mptesize = 4; break;
+    case SMMPT43:
+        load_entry = &load_entry_64; levels = 3; mptesize = 8; break;
+        break;
+    case SMMPT52:
+        load_entry = &load_entry_64; levels = 4; mptesize = 8; break;
+    case SMMPT64:
+        load_entry = &load_entry_64; levels = 5; mptesize = 8; break;
+    case SMMPTBARE:
+        *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;
+        }

target/riscv/riscv_smmpt.c new file +45 -0
+
+        /* 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)

target/riscv/riscv_smmpt.c new file +38 -0
+{
+    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 +0 -0
index 74dcccf4be..0d0597f8eb 100644
@@ -9,6 +9,9 @@
   #ifndef RISCV_SMMPT_H
   #define RISCV_SMMPT_H

  +#include "cpu.h"

target/riscv/riscv_smmpt.h +2 -0
+#include "exec/mmu-access-type.h"
+

  typedef enum {
      SMMPTBARE = 0,
      SMMPT34   = 1,

target/riscv/riscv_smmpt.h +0 -0
@@ -18,4 +21,16 @@ typedef enum {

      SMMPTMAX
  } 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,

target/riscv/riscv_smmpt.h +6 -0
+} 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
#14 lore raw

PATCH v5 3/6 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 | 76 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 75 insertions(+), 1 deletion(-)
target/riscv/cpu_helper.c +75 -1
index e3361fadae..2fab00e9b4 100644
@@ -1161,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
  *
@@ -1355,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,
@@ -1765,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);
@@ -1819,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);
@@ -1854,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.43.0
#15 lore raw

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

Quoted 116 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.

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 | 76 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 75 insertions(+), 1 deletion(-)
target/riscv/cpu_helper.c +62 -1
index e3361fadae..2fab00e9b4 100644
@@ -1161,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
  *
@@ -1355,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,
@@ -1765,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);

target/riscv/cpu_helper.c bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, · line 1888

Quoted 9 lines
target/riscv/cpu_helper.c +6 -0
@@ -1819,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);

We should only log if the extension is enabled

Alistair

author
#16 lore raw

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

Hi Alistair,

Quoted 118 lines · Alistair Francis
Quoted 117 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.

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 | 76 ++++++++++++++++++++++++++++++++++++++-
  1 file changed, 75 insertions(+), 1 deletion(-)
target/riscv/cpu_helper.c +62 -1
index e3361fadae..2fab00e9b4 100644
@@ -1161,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
   *
@@ -1355,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,
@@ -1765,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);
@@ -1819,6 +1880,13 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
              prot &= prot2;
 
Quoted 8 lines

target/riscv/cpu_helper.c bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, · line 1888

Quoted 7 lines
target/riscv/cpu_helper.c +6 -0
              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);

We should only log if the extension is enabled

Yes. Output it under the condition of riscv_cpu_cfg(env)->ext_smmpt in v6 patch set.

Thanks,
Zhiwei

Quoted 2 lines

Alistair

#17 lore raw

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

In reply to LIU Zhiwei

Hi Zhiwei,

LIU Zhiwei <[email protected]> 於 2026年4月9日週四 上午3:12寫道:

Quoted 48 lines

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 | 76 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 75 insertions(+), 1 deletion(-)
target/riscv/cpu_helper.c +13 -0
index e3361fadae..2fab00e9b4 100644
@@ -1161,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,

target/riscv/cpu_helper.c static bool check_svukte_addr(CPURISCVState *env, vaddr addr) · line 1186

Quoted 10 lines
target/riscv/cpu_helper.c +10 -0
+                                    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) {

Nit: env->mptmode == SMMPTBARE

Quoted 35 lines
target/riscv/cpu_helper.c +31 -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
  *

target/riscv/cpu_helper.c static int get_physical_address(CPURISCVState *env, hwaddr *physical, · line 1416

Quoted 8 lines
target/riscv/cpu_helper.c +5 -0
@@ -1355,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;

Nit: Just return mpt_ret;

Quoted 15 lines
target/riscv/cpu_helper.c +3 -1
+        }
+
         int pmp_prot;
         int pmp_ret = get_physical_address_pmp(env, &pmp_prot, pte_addr,
                                                sxlen_bytes,
@@ -1765,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);

target/riscv/cpu_helper.c bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, · line 1889

Quoted 10 lines
target/riscv/cpu_helper.c +7 -0
@@ -1819,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;

We didn't check the return value of get_physical_address_mpt(). So it might that get_physical_address_mpt() returns TRANSLATE_MPT_FAIL, but get_physical_address_pmp() returns TRANSLATE_SUCCESS. The final ret will become: TRANSLATE_SUCCESS.

Quoted 20 lines
target/riscv/cpu_helper.c +6 -1
                 ret = get_physical_address_pmp(env, &prot_pmp, pa,
                                                size, access_type, mode);
                 tlb_size = pmp_get_tlb_size(env, pa);
@@ -1854,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);
--

2.43.0

author
#18 lore raw

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

Hi Frank,

Quoted 53 lines · Frank Chang

Hi Zhiwei,

LIU Zhiwei <[email protected]> 於 2026年4月9日週四 上午3:12寫道:

Quoted 50 lines

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 | 76 ++++++++++++++++++++++++++++++++++++++-
  1 file changed, 75 insertions(+), 1 deletion(-)
target/riscv/cpu_helper.c +16 -0
index e3361fadae..2fab00e9b4 100644
@@ -1161,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;
Quoted 8 lines

target/riscv/cpu_helper.c static bool check_svukte_addr(CPURISCVState *env, vaddr addr) · line 1186

Quoted 7 lines
target/riscv/cpu_helper.c +7 -0
+    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) {

Nit: env->mptmode == SMMPTBARE

Done.

Quoted 39 lines
Quoted 39 lines
target/riscv/cpu_helper.c +31 -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
   *
@@ -1355,6 +1409,13 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical,
              pte_addr = base + idx * ptesize;
          }
 
Quoted 6 lines

target/riscv/cpu_helper.c static int get_physical_address(CPURISCVState *env, hwaddr *physical, · line 1416

Quoted 5 lines
target/riscv/cpu_helper.c +5 -0
+        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;

Nit: Just return mpt_ret;

Done.

Quoted 18 lines
Quoted 18 lines
target/riscv/cpu_helper.c +3 -1
+        }
+
          int pmp_prot;
          int pmp_ret = get_physical_address_pmp(env, &pmp_prot, pte_addr,
                                                 sxlen_bytes,
@@ -1765,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);
@@ -1819,6 +1880,13 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
              prot &= prot2;
 
Quoted 12 lines

target/riscv/cpu_helper.c bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, · line 1889

Quoted 8 lines
target/riscv/cpu_helper.c +7 -0
              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;

We didn't check the return value of get_physical_address_mpt(). So it might that get_physical_address_mpt() returns TRANSLATE_MPT_FAIL, but get_physical_address_pmp() returns TRANSLATE_SUCCESS. The final ret will become: TRANSLATE_SUCCESS.

Good catch!  Besides this, I found there are other places that we should handle TRANSLATE_MPT_FAIL as TRANSLATE_PMP_FAIL in the same way for exceptions. I fix them togather in v6 patch set.

Thanks,
Zhiwei

Quoted 21 lines
Quoted 21 lines
target/riscv/cpu_helper.c +6 -1
                  ret = get_physical_address_pmp(env, &prot_pmp, pa,
                                                 size, access_type, mode);
                  tlb_size = pmp_get_tlb_size(env, pa);
@@ -1854,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);
--

2.43.0

author
#19 lore raw

PATCH v5 4/6 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 6e35c4b1e6..84cec57daf 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.43.0
Reviewed-by on v5 4/6
#20 lore raw

Re: [PATCH v5 4/6] target/riscv: Implement SMMPT fence instructions

Quoted 10 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.

Alistair

Quoted 59 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 6e35c4b1e6..84cec57daf 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 -1
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);
+}
--

2.43.0

#21 lore raw

Re: [PATCH v5 4/6] target/riscv: Implement SMMPT fence instructions

In reply to LIU Zhiwei

Hi Zhiwei,

LIU Zhiwei <[email protected]> 於 2026年4月9日週四 上午3:24寫道:

Quoted 63 lines

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 6e35c4b1e6..84cec57daf 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 +20 -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;
+}

target/riscv/insn_trans/trans_privileged.c.inc static bool trans_sfence_vma(DisasContext *ctx, arg_sfence_vma *a) · line 192

Quoted 10 lines
target/riscv/insn_trans/trans_privileged.c.inc +10 -0
+
+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);
+}

The spec says that:
"When Svinval is implemented with Smsdid, the MINVAL.SPA instruction must be implemented to
support such fine-granular invalidation of physical memory access-permission caches."

So I think we should call REQUIRE_SVINVAL() for MINVAL.SPA. However, trans_privileged.c.inc is included before trans_svinval.c.inc. So it might be better to separate these instructions into trans_smmpt.c.inc and include it after trans_svinval.c.inc.

Regards,
Frank Chang

Quoted 4 lines

--
2.43.0

author
#22 lore raw

Re: [PATCH v5 4/6] target/riscv: Implement SMMPT fence instructions

Hi Frank,

Quoted 72 lines · Frank Chang

Hi Zhiwei,

LIU Zhiwei <[email protected]> 於 2026年4月9日週四 上午3:24寫道:

Quoted 69 lines

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 6e35c4b1e6..84cec57daf 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 +27 -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)
Quoted 8 lines

target/riscv/insn_trans/trans_privileged.c.inc static bool trans_sfence_vma(DisasContext *ctx, arg_sfence_vma *a) · line 187

Quoted 3 lines
target/riscv/insn_trans/trans_privileged.c.inc +3 -0
+{
+    return do_mfence_spa(ctx);
+}

The spec says that:
"When Svinval is implemented with Smsdid, the MINVAL.SPA instruction must be implemented to
support such fine-granular invalidation of physical memory access-permission caches."

I see it is just "when", not explicitly requires Svinval. Another reason is that we  implement minval.spa on QEMU in a not such fine-granular invalidation way. Thus I don't make it require Svinval in v6 patch set. If you have other comments, let me know.

Thanks,
Zhiwei

Quoted 13 lines

So I think we should call REQUIRE_SVINVAL() for MINVAL.SPA. However, trans_privileged.c.inc is included before trans_svinval.c.inc. So it might be better to separate these instructions into trans_smmpt.c.inc and include it after trans_svinval.c.inc.

Regards,
Frank Chang

Quoted 4 lines

--
2.43.0

#23 lore raw

Re: [PATCH v5 4/6] target/riscv: Implement SMMPT fence instructions

In reply to LIU Zhiwei

Hi Zhiwei

Quoted 27 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.

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 +0 -0
index 6e35c4b1e6..84cec57daf 100644

target/riscv/insn32.decode sret 0001000 00010 00000 000 00000 1110011 · line 125

Quoted 6 lines
target/riscv/insn32.decode +2 -0
@@ -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
 

SMMTT spec version 0.49 mentions them as mfence.pa and minval.pa

Thanks
Rahul

author
#24 lore raw

Re: [PATCH v5 4/6] target/riscv: Implement SMMPT fence instructions

Hi Rahul,

Quoted 24 lines · Rahul Pathak

Hi Zhiwei

Quoted 21 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.

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(+)
Quoted 12 lines

target/riscv/insn32.decode sret 0001000 00010 00000 000 00000 1110011 · line 125

Quoted 11 lines
target/riscv/insn32.decode +2 -0
index 6e35c4b1e6..84cec57daf 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
 

SMMTT spec version 0.49 mentions them as mfence.pa and minval.pa

This is still on v0.3.4. I still make it consistent in v6 patch set. Maybe we can update to other new versions in the future.

Thanks,
Zhiwei

Quoted 3 lines

Thanks
Rahul

author
#25 lore raw

PATCH v5 5/6 target/riscv: Fix smrnmi isa alphabetical order

In reply to LIU Zhiwei
---
 target/riscv/cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/cpu.c +1 -1
index 8ac935ac06..cb4ccd001d 100644
@@ -224,9 +224,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(smpmpmt, PRIV_VERSION_1_12_0, ext_smpmpmt),
-    ISA_EXT_DATA_ENTRY(smrnmi, PRIV_VERSION_1_12_0, ext_smrnmi),
     ISA_EXT_DATA_ENTRY(smmpm, PRIV_VERSION_1_13_0, ext_smmpm),
     ISA_EXT_DATA_ENTRY(smnpm, PRIV_VERSION_1_13_0, ext_smnpm),
+    ISA_EXT_DATA_ENTRY(smrnmi, PRIV_VERSION_1_12_0, ext_smrnmi),
     ISA_EXT_DATA_ENTRY(smstateen, PRIV_VERSION_1_12_0, ext_smstateen),
     ISA_EXT_DATA_ENTRY(ssaia, PRIV_VERSION_1_12_0, ext_ssaia),
     ISA_EXT_DATA_ENTRY(ssccfg, PRIV_VERSION_1_13_0, ext_ssccfg),
Signature · 1 line
2.43.0
Reviewed-by on v5 5/6
#26 lore raw

Re: [PATCH v5 5/6] target/riscv: Fix smrnmi isa alphabetical order

Quoted 4 lines · LIU Zhiwei

Alistair

Quoted 22 lines
---
 target/riscv/cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/cpu.c +1 -2
index 8ac935ac06..cb4ccd001d 100644
@@ -224,9 +224,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(smpmpmt, PRIV_VERSION_1_12_0, ext_smpmpmt),
-    ISA_EXT_DATA_ENTRY(smrnmi, PRIV_VERSION_1_12_0, ext_smrnmi),
     ISA_EXT_DATA_ENTRY(smmpm, PRIV_VERSION_1_13_0, ext_smmpm),
     ISA_EXT_DATA_ENTRY(smnpm, PRIV_VERSION_1_13_0, ext_smnpm),
+    ISA_EXT_DATA_ENTRY(smrnmi, PRIV_VERSION_1_12_0, ext_smrnmi),
     ISA_EXT_DATA_ENTRY(smstateen, PRIV_VERSION_1_12_0, ext_smstateen),
     ISA_EXT_DATA_ENTRY(ssaia, PRIV_VERSION_1_12_0, ext_ssaia),
     ISA_EXT_DATA_ENTRY(ssccfg, PRIV_VERSION_1_13_0, ext_ssccfg),
--

2.43.0

Reviewed-by on v5 5/6
#27 lore raw

Re: [PATCH v5 5/6] target/riscv: Fix smrnmi isa alphabetical order

In reply to LIU Zhiwei

LIU Zhiwei <[email protected]> 於 2026年4月9日週四 上午2:36寫道:

Quoted 27 lines
---
 target/riscv/cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/cpu.c +1 -2
index 8ac935ac06..cb4ccd001d 100644
@@ -224,9 +224,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(smpmpmt, PRIV_VERSION_1_12_0, ext_smpmpmt),
-    ISA_EXT_DATA_ENTRY(smrnmi, PRIV_VERSION_1_12_0, ext_smrnmi),
     ISA_EXT_DATA_ENTRY(smmpm, PRIV_VERSION_1_13_0, ext_smmpm),
     ISA_EXT_DATA_ENTRY(smnpm, PRIV_VERSION_1_13_0, ext_smnpm),
+    ISA_EXT_DATA_ENTRY(smrnmi, PRIV_VERSION_1_12_0, ext_smrnmi),
     ISA_EXT_DATA_ENTRY(smstateen, PRIV_VERSION_1_12_0, ext_smstateen),
     ISA_EXT_DATA_ENTRY(ssaia, PRIV_VERSION_1_12_0, ext_ssaia),
     ISA_EXT_DATA_ENTRY(ssccfg, PRIV_VERSION_1_13_0, ext_ssccfg),
--

2.43.0

author
#28 lore raw

PATCH v5 6/6 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 cb4ccd001d..1e49465eeb 100644
@@ -225,8 +225,10 @@ const RISCVIsaExtData isa_edata_arr[] = {
     ISA_EXT_DATA_ENTRY(smepmp, PRIV_VERSION_1_12_0, ext_smepmp),
     ISA_EXT_DATA_ENTRY(smpmpmt, PRIV_VERSION_1_12_0, ext_smpmpmt),
     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(smrnmi, PRIV_VERSION_1_12_0, ext_smrnmi),
+    ISA_EXT_DATA_ENTRY(smsdid, PRIV_VERSION_1_13_0, ext_smsdid),
     ISA_EXT_DATA_ENTRY(smstateen, PRIV_VERSION_1_12_0, ext_smstateen),
     ISA_EXT_DATA_ENTRY(ssaia, PRIV_VERSION_1_12_0, ext_ssaia),
     ISA_EXT_DATA_ENTRY(ssccfg, PRIV_VERSION_1_13_0, ext_ssccfg),
@@ -1379,6 +1381,8 @@ const RISCVCPUMultiExtConfig riscv_cpu_vendor_exts[] = {
 
 /* These are experimental so mark with 'x-' */
 const RISCVCPUMultiExtConfig riscv_cpu_experimental_exts[] = {
+    MULTI_EXT_CFG_BOOL("x-smmpt", ext_smmpt, false),
+    MULTI_EXT_CFG_BOOL("x-smsdid", ext_smsdid, false),
     MULTI_EXT_CFG_BOOL("x-svukte", ext_svukte, false),
 
     { },
Signature · 1 line
2.43.0
#29 lore raw

Re: [PATCH v5 6/6] target/riscv: Enable SMMPT extension

On Thu, Apr 9, 2026 at 12:09 AM LIU Zhiwei <[email protected]> wrote:

Can we add a link in this commit to the spec version please?

Alistair

Quoted 36 lines
---
 target/riscv/cpu.c | 4 ++++
 1 file changed, 4 insertions(+)
target/riscv/cpu.c +4 -1
index cb4ccd001d..1e49465eeb 100644
@@ -225,8 +225,10 @@ const RISCVIsaExtData isa_edata_arr[] = {
     ISA_EXT_DATA_ENTRY(smepmp, PRIV_VERSION_1_12_0, ext_smepmp),
     ISA_EXT_DATA_ENTRY(smpmpmt, PRIV_VERSION_1_12_0, ext_smpmpmt),
     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(smrnmi, PRIV_VERSION_1_12_0, ext_smrnmi),
+    ISA_EXT_DATA_ENTRY(smsdid, PRIV_VERSION_1_13_0, ext_smsdid),
     ISA_EXT_DATA_ENTRY(smstateen, PRIV_VERSION_1_12_0, ext_smstateen),
     ISA_EXT_DATA_ENTRY(ssaia, PRIV_VERSION_1_12_0, ext_ssaia),
     ISA_EXT_DATA_ENTRY(ssccfg, PRIV_VERSION_1_13_0, ext_ssccfg),
@@ -1379,6 +1381,8 @@ const RISCVCPUMultiExtConfig riscv_cpu_vendor_exts[] = {
 
 /* These are experimental so mark with 'x-' */
 const RISCVCPUMultiExtConfig riscv_cpu_experimental_exts[] = {
+    MULTI_EXT_CFG_BOOL("x-smmpt", ext_smmpt, false),
+    MULTI_EXT_CFG_BOOL("x-smsdid", ext_smsdid, false),
     MULTI_EXT_CFG_BOOL("x-svukte", ext_svukte, false),
 
     { },
--

2.43.0

author
#30 lore raw

Re: [PATCH v5 6/6] target/riscv: Enable SMMPT extension

Hi Alistaire

Quoted 3 lines · Alistair Francis

On Thu, Apr 9, 2026 at 12:09 AM LIU Zhiwei <[email protected]> wrote:

Can we add a link in this commit to the spec version please?

OK. Done in v6 patch set.

Thanks,
Zhiwei

Quoted 38 lines

Alistair

Quoted 35 lines
---
  target/riscv/cpu.c | 4 ++++
  1 file changed, 4 insertions(+)
target/riscv/cpu.c +4 -1
index cb4ccd001d..1e49465eeb 100644
@@ -225,8 +225,10 @@ const RISCVIsaExtData isa_edata_arr[] = {
      ISA_EXT_DATA_ENTRY(smepmp, PRIV_VERSION_1_12_0, ext_smepmp),
      ISA_EXT_DATA_ENTRY(smpmpmt, PRIV_VERSION_1_12_0, ext_smpmpmt),
      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(smrnmi, PRIV_VERSION_1_12_0, ext_smrnmi),
+    ISA_EXT_DATA_ENTRY(smsdid, PRIV_VERSION_1_13_0, ext_smsdid),
      ISA_EXT_DATA_ENTRY(smstateen, PRIV_VERSION_1_12_0, ext_smstateen),
      ISA_EXT_DATA_ENTRY(ssaia, PRIV_VERSION_1_12_0, ext_ssaia),
      ISA_EXT_DATA_ENTRY(ssccfg, PRIV_VERSION_1_13_0, ext_ssccfg),
@@ -1379,6 +1381,8 @@ const RISCVCPUMultiExtConfig riscv_cpu_vendor_exts[] = {
 
  /* These are experimental so mark with 'x-' */
  const RISCVCPUMultiExtConfig riscv_cpu_experimental_exts[] = {
+    MULTI_EXT_CFG_BOOL("x-smmpt", ext_smmpt, false),
+    MULTI_EXT_CFG_BOOL("x-smsdid", ext_smsdid, false),
      MULTI_EXT_CFG_BOOL("x-svukte", ext_svukte, false),
 
      { },
--

2.43.0

Reviewed-by on v5 6/6
#31 lore raw

Re: [PATCH v5 6/6] target/riscv: Enable SMMPT extension

In reply to LIU Zhiwei

LIU Zhiwei <[email protected]> 於 2026年4月9日週四 上午3:29寫道:

Quoted 5 lines
Quoted 32 lines
---
 target/riscv/cpu.c | 4 ++++
 1 file changed, 4 insertions(+)
target/riscv/cpu.c +4 -1
index cb4ccd001d..1e49465eeb 100644
@@ -225,8 +225,10 @@ const RISCVIsaExtData isa_edata_arr[] = {
     ISA_EXT_DATA_ENTRY(smepmp, PRIV_VERSION_1_12_0, ext_smepmp),
     ISA_EXT_DATA_ENTRY(smpmpmt, PRIV_VERSION_1_12_0, ext_smpmpmt),
     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(smrnmi, PRIV_VERSION_1_12_0, ext_smrnmi),
+    ISA_EXT_DATA_ENTRY(smsdid, PRIV_VERSION_1_13_0, ext_smsdid),
     ISA_EXT_DATA_ENTRY(smstateen, PRIV_VERSION_1_12_0, ext_smstateen),
     ISA_EXT_DATA_ENTRY(ssaia, PRIV_VERSION_1_12_0, ext_ssaia),
     ISA_EXT_DATA_ENTRY(ssccfg, PRIV_VERSION_1_13_0, ext_ssccfg),
@@ -1379,6 +1381,8 @@ const RISCVCPUMultiExtConfig riscv_cpu_vendor_exts[] = {
 
 /* These are experimental so mark with 'x-' */
 const RISCVCPUMultiExtConfig riscv_cpu_experimental_exts[] = {
+    MULTI_EXT_CFG_BOOL("x-smmpt", ext_smmpt, false),
+    MULTI_EXT_CFG_BOOL("x-smsdid", ext_smsdid, false),
     MULTI_EXT_CFG_BOOL("x-svukte", ext_svukte, false),
 
     { },
--

2.43.0

#32 lore raw

Re: [PATCH v5 6/6] target/riscv: Enable SMMPT extension

In reply to LIU Zhiwei

Hi Zhiwei,

It was brought to my attention that, as per spec 0.49, smmpt is dependent on smsdid. In other words, using our current implicit rules framework, enabling smmpt would also enable smsdid. I checked myself, and on doc version 0.49, start of Chapter 4:

https://github.com/riscv/riscv-smmtt/releases/tag/v0.49

"The following MPT extensions are defined:

• Smmpt34 - Page-based 34-bit memory protection system (RV32 only)
• Smmpt43 - Page-based 43-bit memory protection system (RV64 only)
• Smmpt52 - Page-based 52-bit memory protection system (RV64 only)
• Smmpt64 - Page-based 64-bit memory protection system (RV64 only)

These MPT extensions depend on Smsdid. "

Given that a v6 is going to happen anyway might as well change this patch to add this implicit rule too.

Cheers,
Daniel

Quoted 32 lines · LIU Zhiwei
---
  target/riscv/cpu.c | 4 ++++
  1 file changed, 4 insertions(+)
target/riscv/cpu.c +4 -0
index cb4ccd001d..1e49465eeb 100644
@@ -225,8 +225,10 @@ const RISCVIsaExtData isa_edata_arr[] = {
      ISA_EXT_DATA_ENTRY(smepmp, PRIV_VERSION_1_12_0, ext_smepmp),
      ISA_EXT_DATA_ENTRY(smpmpmt, PRIV_VERSION_1_12_0, ext_smpmpmt),
      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(smrnmi, PRIV_VERSION_1_12_0, ext_smrnmi),
+    ISA_EXT_DATA_ENTRY(smsdid, PRIV_VERSION_1_13_0, ext_smsdid),
      ISA_EXT_DATA_ENTRY(smstateen, PRIV_VERSION_1_12_0, ext_smstateen),
      ISA_EXT_DATA_ENTRY(ssaia, PRIV_VERSION_1_12_0, ext_ssaia),
      ISA_EXT_DATA_ENTRY(ssccfg, PRIV_VERSION_1_13_0, ext_ssccfg),
@@ -1379,6 +1381,8 @@ const RISCVCPUMultiExtConfig riscv_cpu_vendor_exts[] = {
 
  /* These are experimental so mark with 'x-' */
  const RISCVCPUMultiExtConfig riscv_cpu_experimental_exts[] = {
+    MULTI_EXT_CFG_BOOL("x-smmpt", ext_smmpt, false),
+    MULTI_EXT_CFG_BOOL("x-smsdid", ext_smsdid, false),
      MULTI_EXT_CFG_BOOL("x-svukte", ext_svukte, false),
 
      { },
author
#33 lore raw

Re: [PATCH v5 6/6] target/riscv: Enable SMMPT extension

Hi Daniel,

Quoted 17 lines · Daniel Henrique Barboza

Hi Zhiwei,

It was brought to my attention that, as per spec 0.49, smmpt is dependent on smsdid.  In other words, using our current implicit rules framework, enabling smmpt would also enable smsdid.  I checked myself, and on doc version 0.49, start of Chapter 4:

https://github.com/riscv/riscv-smmtt/releases/tag/v0.49

"The following MPT extensions are defined:

• Smmpt34 - Page-based 34-bit memory protection system (RV32 only)
• Smmpt43 - Page-based 43-bit memory protection system (RV64 only)
• Smmpt52 - Page-based 52-bit memory protection system (RV64 only)
• Smmpt64 - Page-based 64-bit memory protection system (RV64 only)

Quoted 5 lines

These MPT extensions depend on Smsdid. "

Given that a v6 is going to happen anyway might as well change this patch to add this implicit rule too.

Yes. I have added this implicit rule in v6 patch set.

Thanks,
Zhiwei

Quoted 39 lines

Cheers,
Daniel

Quoted 31 lines · LIU Zhiwei

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

  1 file changed, 4 insertions(+)
target/riscv/cpu.c +4 -0
index cb4ccd001d..1e49465eeb 100644
@@ -225,8 +225,10 @@ const RISCVIsaExtData isa_edata_arr[] = {
       ISA_EXT_DATA_ENTRY(smepmp, PRIV_VERSION_1_12_0, ext_smepmp),
       ISA_EXT_DATA_ENTRY(smpmpmt, PRIV_VERSION_1_12_0, ext_smpmpmt),
       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(smrnmi, PRIV_VERSION_1_12_0, ext_smrnmi),
+    ISA_EXT_DATA_ENTRY(smsdid, PRIV_VERSION_1_13_0, ext_smsdid),
       ISA_EXT_DATA_ENTRY(smstateen, PRIV_VERSION_1_12_0, ext_smstateen),
       ISA_EXT_DATA_ENTRY(ssaia, PRIV_VERSION_1_12_0, ext_ssaia),
       ISA_EXT_DATA_ENTRY(ssccfg, PRIV_VERSION_1_13_0, ext_ssccfg),
@@ -1379,6 +1381,8 @@ const RISCVCPUMultiExtConfig 
 riscv_cpu_vendor_exts[] = {
     /* These are experimental so mark with 'x-' */
   const RISCVCPUMultiExtConfig riscv_cpu_experimental_exts[] = {
+    MULTI_EXT_CFG_BOOL("x-smmpt", ext_smmpt, false),
+    MULTI_EXT_CFG_BOOL("x-smsdid", ext_smsdid, false),
       MULTI_EXT_CFG_BOOL("x-svukte", ext_svukte, false),
         { },
Reviewed-by on the v5 series
#34 lore raw
In reply to LIU Zhiwei

Hi Zhiwei,

Quoted 9 lines · LIU Zhiwei

This patch set introduces support for the RISC-V Smmpt (Supervisor Memory-tracking and Protection Table) extension. Smmpt provides a hardware mechanism for fine-grained memory protection, checked after address translation, which is particularly useful for supervisor-level sandboxing and security monitoring.

The rfc patch set:
https://mail.gnu.org/archive/html/qemu-riscv/2025-09/msg00216.html

My review comments align with Daniel's, LGTM.

Thanks,
Chao

Quoted 44 lines

v4->v5:
1. Rebase to master.
v3->v4:
1. Add missing review tags.
v2->v3:
1. Fix build error in patch 2.
2. Rebase to master.

rfc->v2:
1. When ext_smmpt is false or BARE mode, make other fields in mmpt CSR zero.
2. Add patch 5 to fix smrnmi ISA string order.
3. Fix patch 6 smmpt and smsdid ISA string order.
4. Make smmpt and smsdid experiment extensions.
5. Add review tags.

LIU Zhiwei (6):
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: Fix smrnmi isa alphabetical order target/riscv: Enable SMMPT extension

 target/riscv/cpu.c                            |   6 +-
 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                            |  95 ++++++
 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                    | 274 ++++++++++++++++++
 target/riscv/riscv_smmpt.h                    |  36 +++
 12 files changed, 560 insertions(+), 6 deletions(-)
 create mode 100644 target/riscv/riscv_smmpt.c
 create mode 100644 target/riscv/riscv_smmpt.h
Signature · 1 line
2.43.0
#35 lore raw
In reply to LIU Zhiwei

Hi Zhiwei,

I was re-reading the code and the version history and I realized that these patches seems to be based on version v0.34. At least this is what is mentioned on the v1 cover:

"This patch set implements the v0.3.4 version of Smmpt"
(https://github.com/riscv/riscv-smmtt/releases/tag/v0.3.4).
https://lore.kernel.org/qemu-devel/[email protected]/

Seeing the changelog from 0.34 to the current version (0.49) there's a lot of differences, including opcode values.

I think it's worth bumping the patches to the latest spec version in v6. Even if v6 takes more time (there were some IOMPT register changes that landed on v0.42). Latest version 0.49 is staled since January 26, ratification target set to Q32026:

https://riscv.atlassian.net/browse/RVS-1345

Thus I think we're in good shape implementing v0.49 it without the risk of changing behind our backs.

Thanks,
Daniel

Quoted 50 lines · LIU Zhiwei

This patch set introduces support for the RISC-V Smmpt (Supervisor Memory-tracking and Protection Table) extension. Smmpt provides a hardware mechanism for fine-grained memory protection, checked after address translation, which is particularly useful for supervisor-level sandboxing and security monitoring.

The rfc patch set:
https://mail.gnu.org/archive/html/qemu-riscv/2025-09/msg00216.html

v4->v5:
1. Rebase to master.
v3->v4:
1. Add missing review tags.
v2->v3:
1. Fix build error in patch 2.
2. Rebase to master.

rfc->v2:
1. When ext_smmpt is false or BARE mode, make other fields in mmpt CSR zero.
2. Add patch 5 to fix smrnmi ISA string order.
3. Fix patch 6 smmpt and smsdid ISA string order.
4. Make smmpt and smsdid experiment extensions.
5. Add review tags.

LIU Zhiwei (6):
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: Fix smrnmi isa alphabetical order target/riscv: Enable SMMPT extension

  target/riscv/cpu.c                            |   6 +-
  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                            |  95 ++++++
  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                    | 274 ++++++++++++++++++
  target/riscv/riscv_smmpt.h                    |  36 +++
  12 files changed, 560 insertions(+), 6 deletions(-)

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

author
#36 lore raw

Hi Daniel,

Quoted 20 lines · Daniel Henrique Barboza

Hi Zhiwei,

I was re-reading the code and the version history and I realized that these patches seems to be based on version v0.34.  At least this is what is mentioned on the v1 cover:

"This patch set implements the v0.3.4 version of Smmpt"
(https://github.com/riscv/riscv-smmtt/releases/tag/v0.3.4).
https://lore.kernel.org/qemu-devel/[email protected]/

Seeing the changelog from 0.34 to the current version (0.49) there's a lot of differences, including opcode values.

I think it's worth bumping the patches to the latest spec version in v6.  Even if v6 takes more time (there were some IOMPT register changes that landed on v0.42).  Latest version 0.49 is staled since January 26, ratification target set to Q32026:

Quoted 4 lines

https://riscv.atlassian.net/browse/RVS-1345

Thus I think we're in good shape implementing v0.49 it without the risk of changing behind our backs.

As SMMPT extension have many other sub extensions, I'd like to submit what we have now(aonther patch set about Smsdia  is also depend on v0.3.4) to make a base for future develop.

Thanks,
Zhiwei

Quoted 58 lines

Thanks,
Daniel

Quoted 50 lines · LIU Zhiwei

This patch set introduces support for the RISC-V Smmpt (Supervisor Memory-tracking and Protection Table) extension. Smmpt provides a hardware mechanism for fine-grained memory protection, checked after address translation, which is particularly useful for supervisor-level sandboxing and security monitoring.

The rfc patch set:
https://mail.gnu.org/archive/html/qemu-riscv/2025-09/msg00216.html

v4->v5:
     1. Rebase to master.
v3->v4:
     1. Add missing review tags.
v2->v3:
     1. Fix build error in patch 2.
     2. Rebase to master.

rfc->v2:
     1. When ext_smmpt is false or BARE mode, make other fields in mmpt CSR zero.
     2. Add patch 5 to fix smrnmi ISA string order.
     3. Fix patch 6 smmpt and smsdid ISA string order.
     4. Make smmpt and smsdid experiment extensions.
     5. Add review tags.

LIU Zhiwei (6):
   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: Fix smrnmi isa alphabetical order target/riscv: Enable SMMPT extension

  target/riscv/cpu.c                            |   6 +- 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                            |  95 ++++++ 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                    | 274 ++++++++++++++++++ target/riscv/riscv_smmpt.h                    |  36 +++

  12 files changed, 560 insertions(+), 6 deletions(-)

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