Skip to contentSkip to conversation

target/riscv: Implement Smsdid and Smmpt extension

PATCH In review LIU Zhiwei 5 participants 24 messages Jul 23 – Nov 9

Subsystems (2)
lore
Share & tools
lore

LIU Zhiwei

author
#0 lore raw

v8 cover target/riscv: Implement Smsdid and Smmpt extension

This patch set introduces support for the RISC-V Smsdid and Smmpt (Supervisor Domain Identifier and Memory Protection Table) extensions v0.4.9(https://github.com/riscv/riscv-smmtt/releases/tag/v0.4.9). 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

Rahul Pathak is working on SMMPT OpenSBI support -
https://lists.infradead.org/pipermail/opensbi/2026-July/010235.html

based on his QEMU SMMPT branch -
https://github.com/pathakraul/qemu/tree/rpathak_smmpt_v1

As we all agree -
https://patchew.org/QEMU/[email protected]/
, we will collaborate on this work and not duplicate efforts. Rahul will focus on OpenSBI upstream and I will update the QEMU patch set to v8.

Xiangyi will review the OpenSBI SMMPT patch set and may collaborate to upstream the other parts, such as SMSDIA and IOMPT.

v7->v8:
1. Update the mfence.pa and minval.pa instruction encodings to match the upstream riscv-opcodes definition
(https://github.com/riscv/riscv-opcodes/pull/389/): mfence.pa now uses funct7=0b0011001 and minval.pa uses funct7=0b0011011. The decoder, disassembler and the system test are updated accordingly.

v6->v7:
The implementation is updated from the v0.3.4 specification to v0.4.9. The main changes are:
1. Update the mmpt CSR number to 0x382 and msdcfg to 0x74E.
2. Update the mmpt register layout: the SDID and PPN field positions changed and the RV64 PPN is now 44 bits.
3. Rework the MPTE format: the N (NAPOT) bit now lives at bit 2, the non-leaf NAPOT form is removed, NAPOT leaf entries encode a single XWR tuple plus a G granularity field, and non-NAPOT leaf entries hold per-page XWR tuples.
4. Treat XWR=000 as "no access" and a non-leaf entry with N=1 as a fault.
5. Enforce the Smmpt64 root-table PPN alignment (low 3 bits zero).
6. Rename the fence instructions mfence.spa/minval.spa to mfence.pa/minval.pa.
7. Drop stale Reviewed-by tags on the substantially reworked patches.
8. Introduce the configurable "smmpt-sdidlen" CPU property.
9. Add disassembly support for the Smmpt.
10. Add bare-metal M-mode system tests.
11. Rebase to master.

v5->v6:
1. Use explicitly bit fields extract instead of mpte_union_t.
2. Use the same exception behavior for MPT valiation as PMP valiation.
3. Use PAGE_* instead of MPT_ACCESS_* as they have same value.
4. Use address_space_*_le instead of address_space_* for SMMPT.
5. Only print SMMPT address check log when SMMPT is enabled.
6. Add implied rule for SMMPT as it depends on SMSDID.
7. Rebase to master.

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 (11):
target/riscv: Add basic definitions and CSRs for SMMPT target/riscv: Add smmpt-sdidlen property for the mmpt SDID field 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: Add disassembly for Smmpt instructions and CSRs target/riscv: Enable SMMPT extension
target/riscv: Add system test for SMMPT extension target/riscv: Add system tests for Smmpt52 and Smmpt64 target/riscv: Add system test for Smmpt34

 disas/riscv.c                                 |   9 +-
 target/riscv/cpu.c                            |  60 +++-
 target/riscv/cpu.h                            |   8 +
 target/riscv/cpu_bits.h                       |  27 ++
 target/riscv/cpu_cfg_fields.h.inc             |   3 +
 target/riscv/insn32.decode                    |   2 +
 target/riscv/meson.build                      |   1 +
 target/riscv/riscv_smmpt.c                    | 339 ++++++++++++++++++
 target/riscv/riscv_smmpt.h                    |  26 ++
 target/riscv/tcg/cpu_helper.c                 | 119 +++++-
 target/riscv/tcg/csr.c                        | 106 ++++++
 .../tcg/insn_trans/trans_privileged.c.inc     |  30 ++
 target/riscv/tcg/pmp.h                        |   3 +
 tests/tcg/riscv64/Makefile.softmmu-target     |  12 +
 tests/tcg/riscv64/smmpt-common.S              | 257 +++++++++++++
 tests/tcg/riscv64/test-smmpt.S                |  85 +++++
 tests/tcg/riscv64/test-smmpt34.S              |  97 +++++
 tests/tcg/riscv64/test-smmpt52.S              |  91 +++++
 tests/tcg/riscv64/test-smmpt64.S              |  98 +++++
 19 files changed, 1354 insertions(+), 19 deletions(-)
 create mode 100644 target/riscv/riscv_smmpt.c
 create mode 100644 target/riscv/riscv_smmpt.h
 create mode 100644 tests/tcg/riscv64/smmpt-common.S
 create mode 100644 tests/tcg/riscv64/test-smmpt.S
 create mode 100644 tests/tcg/riscv64/test-smmpt34.S
 create mode 100644 tests/tcg/riscv64/test-smmpt52.S
 create mode 100644 tests/tcg/riscv64/test-smmpt64.S

base-commit: 499039798cdad7d86b787fec0eaf1da4151c0f05

Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v8 1/11 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                |  8 +++
 target/riscv/cpu_bits.h           | 24 ++++++++
 target/riscv/cpu_cfg_fields.h.inc |  2 +
 target/riscv/riscv_smmpt.h        | 21 +++++++
 target/riscv/tcg/csr.c            | 99 +++++++++++++++++++++++++++++++
 5 files changed, 154 insertions(+)
 create mode 100644 target/riscv/riscv_smmpt.h
target/riscv/cpu.h +8 -0
index c9dfa7daff..8825e1c186 100644
@@ -139,6 +139,7 @@ enum {
     TRANSLATE_PMP_FAIL,
     TRANSLATE_G_STAGE_FAIL,
     TRANSLATE_PMA_FAIL,
+    TRANSLATE_MPT_FAIL
 };
 
 /* Extension context status */
@@ -184,6 +185,7 @@ extern RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[];
 
 #if !defined(CONFIG_USER_ONLY)
 #include "tcg/pmp.h"
+#include "riscv_smmpt.h"
 #endif
 
 #define RV_VLEN_MAX 1024
@@ -521,6 +523,12 @@ struct CPUArchState {
     uint64_t rnmip;
     uint64_t rnmi_irqvec;
     uint64_t rnmi_excpvec;
+
+    /* 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 +24 -0
index 3f146a43fe..4ed4843e3d 100644
@@ -1166,4 +1166,28 @@ typedef enum CTRType {
 #define MCONTEXT64                         0x0000000000001FFFULL
 #define MCONTEXT32_HCONTEXT                0x0000007F
 #define MCONTEXT64_HCONTEXT                0x0000000000003FFFULL
+
+/* Smsdid */
+#define CSR_MMPT        0x382
+#define CSR_MSDCFG      0x74E
+
+/*
+ * MMPT register layout for MXLEN=32:
+ * MODE[31:30], 0[29:28], SDID[27:22], PPN[21:0]
+ */
+#define MMPT_MODE_MASK_32   0xC0000000
+#define MMPT_MODE_SHIFT_32  30
+#define MMPT_SDID_MASK_32   0x0FC00000
+#define MMPT_SDID_SHIFT_32  22
+#define MMPT_PPN_MASK_32    0x003FFFFF
+
+/*
+ * MMPT register layout for MXLEN=64:
+ * MODE[63:60], 0[59:58], SDID[57:52], 0[51:44], PPN[43:0]
+ */
+#define MMPT_MODE_MASK_64   0xF000000000000000ULL
+#define MMPT_MODE_SHIFT_64  60
+#define MMPT_SDID_MASK_64   0x03F0000000000000ULL
+#define MMPT_SDID_SHIFT_64  52
+#define MMPT_PPN_MASK_64    0x00000FFFFFFFFFFFULL
 #endif
target/riscv/cpu_cfg_fields.h.inc +2 -0
index 9eb47af0a7..6b616388be 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/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
target/riscv/tcg/csr.c +99 -0

117 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#3 lore raw

PATCH v8 2/11 target/riscv: Add smmpt-sdidlen property for the mmpt SDID field

In reply to LIU Zhiwei

The number of implemented SDID bits is UNSPECIFIED and may be zero. The number of implemented SDID bits, termed SDIDLEN, may be determined by writing one to every bit position in the SDID field, then reading back the value in mmpt to see which bit positions in the SDID field hold a one. The least-significant bits of SDID are implemented first: that is, if SDIDLEN > 0, SDID[SDIDLEN-1:0] is writable. The maximal value of SDIDLEN, termed SDIDMAX, is 6.

Model this by adding a configurable "smmpt-sdidlen" CPU property. The value defaults to SDIDMAX (6) to preserve the current behaviour. The mmpt write handler now keeps only the low SDIDLEN bits of the SDID field, making the SDIDLEN discovery sequence work as specified.

---
 target/riscv/cpu.c                | 45 +++++++++++++++++++++++++++++++
 target/riscv/cpu_bits.h           |  3 +++
 target/riscv/cpu_cfg_fields.h.inc |  1 +
 target/riscv/tcg/csr.c            | 11 ++++++--
 4 files changed, 58 insertions(+), 2 deletions(-)
target/riscv/cpu.c +45 -0
index 5a82e6563b..0f96e487fa 100644
@@ -1434,6 +1434,7 @@ static void riscv_cpu_init(Object *obj)
     cpu->cfg.pmu_mask = MAKE_64BIT_MASK(3, 16);
     cpu->cfg.vlenb = 128 >> 3;
     cpu->cfg.elen = 64;
+    cpu->cfg.sdidlen = MMPT_SDIDLEN_MAX;
     cpu->cfg.cbom_blocksize = 64;
     cpu->cfg.cbop_blocksize = 64;
     cpu->cfg.cboz_blocksize = 64;
@@ -1939,6 +1940,48 @@ static const PropertyInfo prop_vlen = {
     .set = prop_vlen_set,
 };
 
+static void prop_sdidlen_set(Object *obj, Visitor *v, const char *name,
+                             void *opaque, Error **errp)
+{
+    RISCVCPU *cpu = RISCV_CPU(obj);
+    uint8_t value;
+
+    if (!visit_type_uint8(v, name, &value, errp)) {
+        return;
+    }
+
+    if (value > MMPT_SDIDLEN_MAX) {
+        error_setg(errp, "smmpt-sdidlen must be between 0 and %d",
+                   MMPT_SDIDLEN_MAX);
+        return;
+    }
+
+    if (value != cpu->cfg.sdidlen && riscv_cpu_is_vendor(obj)) {
+        cpu_set_prop_err(cpu, name, errp);
+        error_append_hint(errp, "Current '%s' val: %u\n",
+                          name, cpu->cfg.sdidlen);
+        return;
+    }
+
+    cpu_option_add_user_setting(cpu, name);
+    cpu->cfg.sdidlen = value;
+}
+
+static void prop_sdidlen_get(Object *obj, Visitor *v, const char *name,
+                             void *opaque, Error **errp)
+{
+    uint8_t value = RISCV_CPU(obj)->cfg.sdidlen;
+
+    visit_type_uint8(v, name, &value, errp);
+}
+
+static const PropertyInfo prop_sdidlen = {
+    .type = "uint8",
+    .description = "smmpt-sdidlen",
+    .get = prop_sdidlen_get,
+    .set = prop_sdidlen_set,
+};
+
 static void prop_elen_set(Object *obj, Visitor *v, const char *name,
                          void *opaque, Error **errp)
 {
@@ -2853,6 +2896,8 @@ static const Property riscv_cpu_properties[] = {
     {.name = "vlen", .info = &prop_vlen},
     {.name = "elen", .info = &prop_elen},
 
+    {.name = "smmpt-sdidlen", .info = &prop_sdidlen},
+
     {.name = "cbom_blocksize", .info = &prop_cbom_blksize},
     {.name = "cbop_blocksize", .info = &prop_cbop_blksize},
     {.name = "cboz_blocksize", .info = &prop_cboz_blksize},
target/riscv/cpu_bits.h +3 -0
index 4ed4843e3d..642edf3369 100644
@@ -1171,6 +1171,9 @@ typedef enum CTRType {
 #define CSR_MMPT        0x382
 #define CSR_MSDCFG      0x74E
 
+/* Maximal number of implemented SDID bits (SDIDMAX) */
+#define MMPT_SDIDLEN_MAX    6
+
 /*
  * MMPT register layout for MXLEN=32:
  * MODE[31:30], 0[29:28], SDID[27:22], PPN[21:0]
target/riscv/cpu_cfg_fields.h.inc +1 -0
index 6b616388be..4a8c9b9e21 100644
@@ -174,6 +174,7 @@ TYPED_FIELD(uint64_t, mimpid, 0)
 TYPED_FIELD(uint32_t, pmu_mask, 0)
 TYPED_FIELD(uint16_t, vlenb, 0)
 TYPED_FIELD(uint16_t, elen, 0)
+TYPED_FIELD(uint8_t,  sdidlen, 0)
 TYPED_FIELD(uint16_t, cbom_blocksize, 0)
 TYPED_FIELD(uint16_t, cbop_blocksize, 0)
 TYPED_FIELD(uint16_t, cboz_blocksize, 0)
target/riscv/tcg/csr.c +9 -2
index 8ffe85caa9..8d38d7a972 100644
@@ -5608,6 +5608,11 @@ static RISCVException write_mmpt(CPURISCVState *env, int csrno,
                                  target_ulong val, uintptr_t ra)
 {
     uint32_t mode_value = 0;
+    /*
+     * Only the least-significant SDIDLEN bits of the SDID field are
+     * writable; the remaining bits are WARL and read as zero.
+     */
+    uint64_t sdid_mask = MAKE_64BIT_MASK(0, riscv_cpu_cfg(env)->sdidlen);
     if (!riscv_cpu_cfg(env)->ext_smmpt) {
         goto set_remaining_fields_zero;
     }
@@ -5621,7 +5626,8 @@ static RISCVException write_mmpt(CPURISCVState *env, int csrno,
             /* Only write the legal value */
             env->mptmode = mode_value;
         }
-        env->sdid = (val & MMPT_SDID_MASK_32) >> MMPT_SDID_SHIFT_32;
+        env->sdid = ((val & MMPT_SDID_MASK_32) >> MMPT_SDID_SHIFT_32)
+                    & sdid_mask;
         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;
@@ -5632,7 +5638,8 @@ static RISCVException write_mmpt(CPURISCVState *env, int csrno,
             mode_value += SMMPT43 - SMMPT34;
             env->mptmode = mode_value;
         }
-        env->sdid = (val & MMPT_SDID_MASK_64) >> MMPT_SDID_SHIFT_64;
+        env->sdid = (((uint64_t)val & MMPT_SDID_MASK_64) >> MMPT_SDID_SHIFT_64)
+                    & sdid_mask;
         env->mptppn = val & MMPT_PPN_MASK_64;
         /* Smmpt64: root table PPN bits 2:0 must be zero (32KiB alignment) */
         if (env->mptmode == SMMPT64) {
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v8 02/11] target/riscv: Add smmpt-sdidlen property for the mmpt SDID field

Hi Zhiwei,

Quoted 49 lines · LIU Zhiwei

The number of implemented SDID bits is UNSPECIFIED and may be zero. The number of implemented SDID bits, termed SDIDLEN, may be determined by writing one to every bit position in the SDID field, then reading back the value in mmpt to see which bit positions in the SDID field hold a one. The least-significant bits of SDID are implemented first: that is, if SDIDLEN > 0, SDID[SDIDLEN-1:0] is writable. The maximal value of SDIDLEN, termed SDIDMAX, is 6.

Model this by adding a configurable "smmpt-sdidlen" CPU property. The value defaults to SDIDMAX (6) to preserve the current behaviour. The mmpt write handler now keeps only the low SDIDLEN bits of the SDID field, making the SDIDLEN discovery sequence work as specified.

---
  target/riscv/cpu.c                | 45 +++++++++++++++++++++++++++++++
  target/riscv/cpu_bits.h           |  3 +++
  target/riscv/cpu_cfg_fields.h.inc |  1 +
  target/riscv/tcg/csr.c            | 11 ++++++--
  4 files changed, 58 insertions(+), 2 deletions(-)
target/riscv/cpu.c +13 -0
index 5a82e6563b..0f96e487fa 100644
@@ -1434,6 +1434,7 @@ static void riscv_cpu_init(Object *obj)
      cpu->cfg.pmu_mask = MAKE_64BIT_MASK(3, 16);
      cpu->cfg.vlenb = 128 >> 3;
      cpu->cfg.elen = 64;
+    cpu->cfg.sdidlen = MMPT_SDIDLEN_MAX;
      cpu->cfg.cbom_blocksize = 64;
      cpu->cfg.cbop_blocksize = 64;
      cpu->cfg.cboz_blocksize = 64;
@@ -1939,6 +1940,48 @@ static const PropertyInfo prop_vlen = {
      .set = prop_vlen_set,
  };
 
+static void prop_sdidlen_set(Object *obj, Visitor *v, const char *name,
+                             void *opaque, Error **errp)
+{
+    RISCVCPU *cpu = RISCV_CPU(obj);
+    uint8_t value;
+
+    if (!visit_type_uint8(v, name, &value, errp)) {
+        return;
+    }
+
+    if (value > MMPT_SDIDLEN_MAX) {
+        error_setg(errp, "smmpt-sdidlen must be between 0 and %d",

target/riscv/cpu.c static const PropertyInfo prop_vlen = { · line 1964

Quoted 10 lines
target/riscv/cpu.c +10 -0
+                   MMPT_SDIDLEN_MAX);
+        return;
+    }
+
+    if (value != cpu->cfg.sdidlen && riscv_cpu_is_vendor(obj)) {
+        cpu_set_prop_err(cpu, name, errp);
+        error_append_hint(errp, "Current '%s' val: %u\n",
+                          name, cpu->cfg.sdidlen);
+        return;
+    }

This is something I'll eventually get to but I think we're being too protective with vendor CPUs. If users wants to use a vendor CPU and then change stuff that would either misconfigure or mischaracterize it, we should just let them. It's a conscious decision in the command line to change the configuration of user CPUs, i.e. users that do that are liable to any consequences too.

All this to say that we should get rid of this particular check. Everything else LGTM.

Thanks,
Daniel

Quoted 94 lines
target/riscv/cpu.c +22 -0
+
+    cpu_option_add_user_setting(cpu, name);
+    cpu->cfg.sdidlen = value;
+}
+
+static void prop_sdidlen_get(Object *obj, Visitor *v, const char *name,
+                             void *opaque, Error **errp)
+{
+    uint8_t value = RISCV_CPU(obj)->cfg.sdidlen;
+
+    visit_type_uint8(v, name, &value, errp);
+}
+
+static const PropertyInfo prop_sdidlen = {
+    .type = "uint8",
+    .description = "smmpt-sdidlen",
+    .get = prop_sdidlen_get,
+    .set = prop_sdidlen_set,
+};
+
  static void prop_elen_set(Object *obj, Visitor *v, const char *name,
                           void *opaque, Error **errp)
  {
@@ -2853,6 +2896,8 @@ static const Property riscv_cpu_properties[] = {
      {.name = "vlen", .info = &prop_vlen},
      {.name = "elen", .info = &prop_elen},
 
+    {.name = "smmpt-sdidlen", .info = &prop_sdidlen},
+
      {.name = "cbom_blocksize", .info = &prop_cbom_blksize},
      {.name = "cbop_blocksize", .info = &prop_cbop_blksize},
      {.name = "cboz_blocksize", .info = &prop_cboz_blksize},
target/riscv/cpu_bits.h +3 -0
index 4ed4843e3d..642edf3369 100644
@@ -1171,6 +1171,9 @@ typedef enum CTRType {
  #define CSR_MMPT        0x382
  #define CSR_MSDCFG      0x74E
 
+/* Maximal number of implemented SDID bits (SDIDMAX) */
+#define MMPT_SDIDLEN_MAX    6
+
  /*
   * MMPT register layout for MXLEN=32:
   * MODE[31:30], 0[29:28], SDID[27:22], PPN[21:0]
target/riscv/cpu_cfg_fields.h.inc +1 -0
index 6b616388be..4a8c9b9e21 100644
@@ -174,6 +174,7 @@ TYPED_FIELD(uint64_t, mimpid, 0)
  TYPED_FIELD(uint32_t, pmu_mask, 0)
  TYPED_FIELD(uint16_t, vlenb, 0)
  TYPED_FIELD(uint16_t, elen, 0)
+TYPED_FIELD(uint8_t,  sdidlen, 0)
  TYPED_FIELD(uint16_t, cbom_blocksize, 0)
  TYPED_FIELD(uint16_t, cbop_blocksize, 0)
  TYPED_FIELD(uint16_t, cboz_blocksize, 0)
target/riscv/tcg/csr.c +9 -2
index 8ffe85caa9..8d38d7a972 100644
@@ -5608,6 +5608,11 @@ static RISCVException write_mmpt(CPURISCVState *env, int csrno,
                                   target_ulong val, uintptr_t ra)
  {
      uint32_t mode_value = 0;
+    /*
+     * Only the least-significant SDIDLEN bits of the SDID field are
+     * writable; the remaining bits are WARL and read as zero.
+     */
+    uint64_t sdid_mask = MAKE_64BIT_MASK(0, riscv_cpu_cfg(env)->sdidlen);
      if (!riscv_cpu_cfg(env)->ext_smmpt) {
          goto set_remaining_fields_zero;
      }
@@ -5621,7 +5626,8 @@ static RISCVException write_mmpt(CPURISCVState *env, int csrno,
              /* Only write the legal value */
              env->mptmode = mode_value;
          }
-        env->sdid = (val & MMPT_SDID_MASK_32) >> MMPT_SDID_SHIFT_32;
+        env->sdid = ((val & MMPT_SDID_MASK_32) >> MMPT_SDID_SHIFT_32)
+                    & sdid_mask;
          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;
@@ -5632,7 +5638,8 @@ static RISCVException write_mmpt(CPURISCVState *env, int csrno,
              mode_value += SMMPT43 - SMMPT34;
              env->mptmode = mode_value;
          }
-        env->sdid = (val & MMPT_SDID_MASK_64) >> MMPT_SDID_SHIFT_64;
+        env->sdid = (((uint64_t)val & MMPT_SDID_MASK_64) >> MMPT_SDID_SHIFT_64)
+                    & sdid_mask;
          env->mptppn = val & MMPT_PPN_MASK_64;
          /* Smmpt64: root table PPN bits 2:0 must be zero (32KiB alignment) */
          if (env->mptmode == SMMPT64) {
author
#5 lore raw

PATCH v8 3/11 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. Both non-NAPOT leaf entries (per-page XWR tuples) and NAPOT leaf entries (a single XWR tuple with a G granularity field) are supported, as defined by the v0.4.9 MPTE format. Helper functions for parsing MPT entries and converting access permissions are included in the new riscv_smmpt.h header.

---
 target/riscv/meson.build      |   1 +
 target/riscv/riscv_smmpt.c    | 339 ++++++++++++++++++++++++++++++++++
 target/riscv/riscv_smmpt.h    |   5 +
 target/riscv/tcg/cpu_helper.c |   6 +-
 target/riscv/tcg/pmp.h        |   3 +
 5 files changed, 351 insertions(+), 3 deletions(-)
 create mode 100644 target/riscv/riscv_smmpt.c
target/riscv/meson.build +1 -0
index 42d0f6d538..51257a8f3a 100644
@@ -22,6 +22,7 @@ riscv_ss.add(files(
 riscv_system_ss = ss.source_set()
 riscv_system_ss.add(files(
   'arch_dump.c',
+  'riscv_smmpt.c',
   'monitor.c',
   'machine.c',
   'time_helper.c',
target/riscv/riscv_smmpt.c new file +339 -0

339 lines not on this page · raw message

target/riscv/riscv_smmpt.h +5 -0
index 74dcccf4be..c15637036e 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,6 @@ typedef enum {
     SMMPTMAX
 } mpt_mode_t;
 
+bool smmpt_check_access(CPURISCVState *env, hwaddr addr,
+                        int *prot, MMUAccessType access_type);
 #endif
target/riscv/tcg/cpu_helper.c +3 -3
index 07d9222652..354506c565 100644
@@ -903,9 +903,9 @@ void riscv_cpu_set_mode(CPURISCVState *env, privilege_mode_t newpriv,
  * @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,
-                                    privilege_mode_t mode)
+int get_physical_address_pmp(CPURISCVState *env, int *prot, hwaddr addr,
+                             int size, MMUAccessType access_type,
+                             privilege_mode_t mode)
 {
     pmp_priv_t pmp_priv;
     bool pmp_has_privs;
target/riscv/tcg/pmp.h +3 -0
index 4c95c2767a..16c0db93c6 100644
@@ -80,6 +80,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,
+                             privilege_mode_t mode);
 
 #define MSECCFG_MML_ISSET(env) get_field(env->mseccfg, MSECCFG_MML)
 #define MSECCFG_MMWP_ISSET(env) get_field(env->mseccfg, MSECCFG_MMWP)
Signature · 1 line
2.43.0
Reviewed-by on v8 3/11
#6 lore raw

Re: [PATCH v8 03/11] target/riscv: Implement core SMMPT lookup logic

Quoted 313 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. Both non-NAPOT leaf entries (per-page XWR tuples) and NAPOT leaf entries (a single XWR tuple with a G granularity field) are supported, as defined by the v0.4.9 MPTE format. Helper functions for parsing MPT entries and converting access permissions are included in the new riscv_smmpt.h header.

---
  target/riscv/meson.build      |   1 +
  target/riscv/riscv_smmpt.c    | 339 ++++++++++++++++++++++++++++++++++
  target/riscv/riscv_smmpt.h    |   5 +
  target/riscv/tcg/cpu_helper.c |   6 +-
  target/riscv/tcg/pmp.h        |   3 +
  5 files changed, 351 insertions(+), 3 deletions(-)

create mode 100644 target/riscv/riscv_smmpt.c

target/riscv/meson.build +1 -0
index 42d0f6d538..51257a8f3a 100644
@@ -22,6 +22,7 @@ riscv_ss.add(files(
  riscv_system_ss = ss.source_set()
  riscv_system_ss.add(files(
    'arch_dump.c',
+  'riscv_smmpt.c',
    'monitor.c',
    'machine.c',
    'time_helper.c',
target/riscv/riscv_smmpt.c new file +266 -0
new file mode 100644
index 0000000000..5bdc474dbb
@@ -0,0 +1,339 @@
+/*
+ * QEMU RISC-V Smmpt (Memory Protection Table)
+ *
+ * Copyright (c) 2024 Alibaba Group. All rights reserved.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Implements the MPT lookup algorithm per SMMTT specification v0.4.9.
+ *
+ * MPTE format (v0.4.9):
+ *
+ * 32-bit non-leaf:  V[0], L=0[1], Reserved[9:2], PPN[31:10]
+ * 32-bit non-NAPOT leaf: V[0], L=1[1], N=0[2], Reserved[7:3], XWR[31:8]
+ * 32-bit NAPOT leaf: V[0], L=1[1], N=1[2], Reserved[7:3], XWR[10:8], 0[11],
+ *                    G[15:12], Reserved[31:16]
+ *
+ * 64-bit non-leaf:  V[0], L=0[1], Reserved[9:2], PPN[53:10], Reserved[63:54]
+ * 64-bit non-NAPOT leaf: V[0], L=1[1], N=0[2], Reserved[7:3], XWR[55:8],
+ *                         Reserved[63:56]
+ * 64-bit NAPOT leaf: V[0], L=1[1], N=1[2], Reserved[7:3], XWR[10:8], 0[11],
+ *                    G[15:12], Reserved[63:16]
+ */
+
+#include "qemu/osdep.h"
+#include "riscv_smmpt.h"
+#include "tcg/pmp.h"
+#include "exec/page-protection.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_le(as, addr, attrs, result);
+}
+
+static uint64_t load_entry_64(AddressSpace *as, hwaddr addr,
+                              MemTxAttrs attrs, MemTxResult *result)
+{
+    return address_space_ldq_le(as, addr, attrs, result);
+}
+
+static inline bool mpte_is_valid(uint64_t mpte)
+{
+    return mpte & 0x1;
+}
+
+static inline bool mpte_is_leaf(uint64_t mpte)
+{
+    return mpte & 0x2;
+}
+
+static inline bool mpte_get_n(uint64_t mpte)
+{
+    return (mpte >> 2) & 0x1;
+}
+
+/*
+ * Get reserved bits from MPTE. Returns non-zero if any reserved bit is set.
+ */
+static uint64_t mpte_get_rsv(CPURISCVState *env, uint64_t mpte)
+{
+    RISCVMXL mxl = riscv_cpu_mxl(env);
+    bool leaf = mpte_is_leaf(mpte);
+    bool napot = mpte_get_n(mpte);
+
+    if (mxl == MXL_RV32) {
+        if (!leaf) {
+            /* non-leaf32: Reserved = bits[9:2] */
+            return extract32(mpte, 2, 8);
+        }
+        if (!napot) {
+            /* non-NAPOT leaf32: Reserved = bits[7:3] (XWR fills [31:8]) */
+            return extract32(mpte, 3, 5);
+        }
+        /* NAPOT leaf32: Reserved = bits[7:3] | bit[11] (mbz) | bits[31:16] */
+        return extract32(mpte, 3, 5) | extract32(mpte, 11, 1) |
+               extract32(mpte, 16, 16);
+    }
+
+    /* RV64 */
+    if (!leaf) {
+        /* non-leaf64: Reserved = bits[9:2] | bits[63:54] */
+        return extract64(mpte, 2, 8) | extract64(mpte, 54, 10);
+    }
+    if (!napot) {
+        /* non-NAPOT leaf64: Reserved = bits[7:3] | bits[63:56] */
+        return extract64(mpte, 3, 5) | extract64(mpte, 56, 8);
+    }
+    /* NAPOT leaf64: Reserved = bits[7:3] | bit[11] (mbz) | bits[63:16] */
+    return extract64(mpte, 3, 5) | extract64(mpte, 11, 1) |
+           extract64(mpte, 16, 48);
+}
+
+/*
+ * Get PPN from a non-leaf MPTE.
+ * RV32 non-leaf: PPN = bits[31:10] (22 bits)
+ * RV64 non-leaf: PPN = bits[53:10] (44 bits)
+ */
+static uint64_t mpte_get_ppn(CPURISCVState *env, uint64_t mpte)
+{
+    RISCVMXL mxl = riscv_cpu_mxl(env);
+
+    if (mxl == MXL_RV32) {
+        return extract32(mpte, 10, 22);
+    }
+    return extract64(mpte, 10, 44);
+}
+
+/*
+ * Get XWR permission for a specific page index from a non-NAPOT leaf.
+ * The XWR base bit is 8 for both RV32 and RV64; only the number of
+ * entries differs (pi in 0..7 for RV32, 0..15 for RV64).
+ */
+static uint32_t mpte_get_xwr(CPURISCVState *env, uint64_t mpte, int pi)
+{
+    return extract64(mpte, 8 + pi * 3, 3);
+}
+
+/*
+ * Get single XWR from a NAPOT leaf.
+ * The NAPOT XWR base bit is 8 for both RV32 and RV64 (bits[10:8]).
+ */
+static uint32_t mpte_get_napot_xwr(CPURISCVState *env, uint64_t mpte)
+{
+    return extract64(mpte, 8, 3);
+}
+
+/*
+ * Get G field from a NAPOT leaf.
+ * The G base bit is 12 for both RV32 and RV64 (bits[15:12]).
+ */
+static uint32_t mpte_get_g(CPURISCVState *env, uint64_t mpte)
+{
+    return extract64(mpte, 12, 4);
+}
+
+/*
+ * Validate the G encoding for the given MPT mode.
+ * Smmpt34: only G=6 is valid
+ * Smmpt43/52/64: only G=4 is valid
+ */
+static bool mpte_validate_g(uint32_t g, mpt_mode_t mode)
+{
+    switch (mode) {
+    case SMMPT34:
+        return g == 6;
+    case SMMPT43:
+    case SMMPT52:
+    case SMMPT64:
+        return g == 4;
+    default:
+        return false;
+    }
+}
+
+/*
+ * Get page number index pn[i] from the supervisor physical address.
+ *
+ * Smmpt34 (34-bit SPA):
+ *   SPA layout: range_offset[14:0], pn[0][24:15] (10 bits),
+ *   pn[1][33:25] (9 bits)
+ *
+ * Smmpt43/52/64 (RV64 MPT):
+ *   SPA layout: range_offset[15:0], pn[0][24:16] (9 bits), ...
+ *   For Smmpt64, pn[4] (top level) is 12 bits.
+ */
+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);
+        }
+    }
+}
+
+/*
+ * Get the page index within a leaf MPTE.
+ *
+ * Smmpt34: pi = SPA[14:12] (3 bits) for level 0, SPA[24:22] for level 1
+ * Smmpt43/52/64: pi = SPA[offset-4 +: 4] (4 bits)
+ */
+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);
+    }
+}
+
+/*
+ * Check XWR permission bits against the access type.
+ * Returns true if access is allowed.
+ *
+ * The 3-bit XWR field uses the same bit order as RISC-V PTE permissions
+ * (bit0 = R, bit1 = W, bit2 = X), matching QEMU's PAGE_READ / PAGE_WRITE /
+ * PAGE_EXEC. As with PTEs, writable-but-not-readable encodings are reserved.
+ *
+ * XWR encoding (bit2=X, bit1=W, bit0=R):
+ *   000 = No access
+ *   001 = Read only
+ *   010 = Reserved (fault)
+ *   011 = Read + Write
+ *   100 = Execute only
+ *   101 = Read + Execute
+ *   110 = Reserved (fault)
+ *   111 = Read + Write + Execute
+ */
+static bool mpt_check_xwr(uint32_t xwr, int *prot, MMUAccessType access_type)
+{
+    switch (xwr) {
+    case 0: /* No access */
+        return false;
+    case PAGE_EXEC: /* 100: Execute only */
+        *prot = PAGE_EXEC;
+        return access_type == MMU_INST_FETCH;
+    case PAGE_READ | PAGE_EXEC: /* 101: Read + Execute */
+        *prot = PAGE_READ | PAGE_EXEC;
+        return (access_type == MMU_DATA_LOAD ||
+                access_type == MMU_INST_FETCH);
+    case PAGE_READ: /* 001: Read only */
+        *prot = PAGE_READ;
+        return access_type == MMU_DATA_LOAD;
+    case PAGE_READ | PAGE_WRITE: /* 011: Read + Write */
+        *prot = PAGE_READ | PAGE_WRITE;
+        return (access_type == MMU_DATA_LOAD ||
+                access_type == MMU_DATA_STORE);
+    case PAGE_READ | PAGE_WRITE | PAGE_EXEC: /* 111: R+W+X */
+        *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
+        return true;
+    default: /* 010, 110: Reserved - fault */
+        return false;
+    }
+}
+
+static bool smmpt_lookup(CPURISCVState *env, hwaddr addr, mpt_mode_t mode,
+                         int *prot, 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, g;
+    int pn, pi, pmp_prot, pmp_ret;
+    uint64_t mpte;
+
+    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;
+    case SMMPT52:

target/riscv/riscv_smmpt.c line 276

Quoted 10 lines
target/riscv/riscv_smmpt.c new file +10 -0
+        load_entry = &load_entry_64; levels = 4; mptesize = 8; break;
+    case SMMPT64:
+        load_entry = &load_entry_64; levels = 5; mptesize = 8; break;
+    case SMMPTBARE:
+        *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
+        return true;
+    default:
+        g_assert_not_reached();
+        break;
+    }

We don't need this 'break' after g_assert_not_reached() - if execution hits this assert the whole process will exit.

With the break removed:

Quoted 115 lines
target/riscv/riscv_smmpt.c new file +63 -0
+
+    for (int i = levels - 1; i >= 0; i--) {
+        /* Step 1: Get pn[i] as the MPT index */
+        pn = mpt_get_pn(addr, i, mode);
+
+        /* Step 2: Load MPTE from memory */
+        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);
+        if (res != MEMTX_OK) {
+            return false;
+        }
+
+        /* Step 3: Check valid bit and reserved bits */
+        if (!mpte_is_valid(mpte) || mpte_get_rsv(env, mpte)) {
+            return false;
+        }
+
+        /* Step 3 (cont): non-leaf with N=1 is a fault */
+        if (!mpte_is_leaf(mpte) && mpte_get_n(mpte)) {
+            return false;
+        }
+
+        /* Step 4: Process non-leaf node */
+        if (!mpte_is_leaf(mpte)) {
+            if (i == 0) {
+                return false;
+            }
+            base = mpte_get_ppn(env, mpte) << PGSHIFT;
+            continue;
+        }
+
+        /* Step 5 & 6: Process leaf node */
+        if (!mpte_get_n(mpte)) {
+            /* Step 5: Non-NAPOT leaf - get XWR[pi] */
+            pi = mpt_get_pi(addr, i, mode);
+            xwr = mpte_get_xwr(env, mpte, pi);
+        } else {
+            /* Step 6: NAPOT leaf - validate G, get single XWR */
+            g = mpte_get_g(env, mpte);
+            if (!mpte_validate_g(g, mode)) {
+                return false;
+            }
+            xwr = mpte_get_napot_xwr(env, mpte);
+        }
+
+        /* Step 7: Check permission */
+        return mpt_check_xwr(xwr, prot, access_type);
+    }
+    return false;
+}
+
+bool smmpt_check_access(CPURISCVState *env, hwaddr addr,
+                        int *prot, MMUAccessType access_type)
+{
+    mpt_mode_t mode = env->mptmode;
+
+    return smmpt_lookup(env, addr, mode, prot, access_type);
+}
target/riscv/riscv_smmpt.h +5 -0
index 74dcccf4be..c15637036e 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,6 @@ typedef enum {
      SMMPTMAX
  } mpt_mode_t;
 
+bool smmpt_check_access(CPURISCVState *env, hwaddr addr,
+                        int *prot, MMUAccessType access_type);
  #endif
target/riscv/tcg/cpu_helper.c +3 -3
index 07d9222652..354506c565 100644
@@ -903,9 +903,9 @@ void riscv_cpu_set_mode(CPURISCVState *env, privilege_mode_t newpriv,
   * @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,
-                                    privilege_mode_t mode)
+int get_physical_address_pmp(CPURISCVState *env, int *prot, hwaddr addr,
+                             int size, MMUAccessType access_type,
+                             privilege_mode_t mode)
  {
      pmp_priv_t pmp_priv;
      bool pmp_has_privs;
target/riscv/tcg/pmp.h +3 -0
index 4c95c2767a..16c0db93c6 100644
@@ -80,6 +80,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,
+                             privilege_mode_t mode);
 
  #define MSECCFG_MML_ISSET(env) get_field(env->mseccfg, MSECCFG_MML)
  #define MSECCFG_MMWP_ISSET(env) get_field(env->mseccfg, MSECCFG_MMWP)
author
#7 lore raw

PATCH v8 4/11 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/tcg/cpu_helper.c | 113 ++++++++++++++++++++++++++++++----
 1 file changed, 100 insertions(+), 13 deletions(-)
target/riscv/tcg/cpu_helper.c +100 -13
index 354506c565..e3aac26931 100644
@@ -976,6 +976,53 @@ 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)
+{
+    /*
+     * 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 == SMMPTBARE) {
+        *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;
+    }
+
+    if (!smmpt_check_access(env, addr, prot, access_type)) {
+        *prot = 0;
+        return TRANSLATE_MPT_FAIL;
+    }
+
+    return TRANSLATE_SUCCESS;
+}
+
 /*
  * get_physical_address - get the physical address for this virtual address
  *
@@ -1174,6 +1221,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 mpt_ret;
+        }
+
         int pmp_prot;
         int pmp_ret = get_physical_address_pmp(env, &pmp_prot, pte_addr,
                                                sxlen_bytes,
@@ -1657,7 +1711,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_pma_violation = false;
     bool first_stage_error = true;
     bool two_stage_lookup = mmuidx_2stage(mmu_idx);
@@ -1710,26 +1764,42 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
             prot &= prot2;
 
             if (ret == TRANSLATE_SUCCESS) {
-                ret = get_physical_address_pmp(env, &prot_pmp, pa,
-                                               size, access_type, mode);
-                tlb_size = pmp_get_tlb_size(env, pa);
+                ret = get_physical_address_mpt(env, &mpt_prot, pa,
+                                               access_type, mode);
+                if (riscv_cpu_cfg(env)->ext_smmpt) {
+                    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;
 
-                qemu_log_mask(CPU_LOG_MMU,
-                              "%s PMP address=" HWADDR_FMT_plx " ret %d prot"
-                              " %d tlb_size %" HWADDR_PRIu "\n",
-                              __func__, pa, ret, prot_pmp, tlb_size);
+                if (ret == TRANSLATE_SUCCESS) {
+                    ret = get_physical_address_pmp(env, &prot_pmp, pa,
+                                                   size, access_type, mode);
+                    tlb_size = pmp_get_tlb_size(env, pa);
+
+                    qemu_log_mask(CPU_LOG_MMU,
+                                  "%s PMP address=" HWADDR_FMT_plx
+                                  " ret %d prot %d tlb_size %"
+                                  HWADDR_PRIu "\n",
+                                  __func__, pa, ret, prot_pmp,
+                                  tlb_size);
 
-                prot &= prot_pmp;
+                    prot &= prot_pmp;
+                }
             } else {
                 /*
                  * Guest physical address translation failed, this is a HS
                  * level exception
                  */
                 first_stage_error = false;
-                if (ret != TRANSLATE_PMP_FAIL) {
+                if (ret != TRANSLATE_PMP_FAIL &&
+                    ret != TRANSLATE_MPT_FAIL) {
                     env->guest_phys_fault_addr = (im_address |
                                                   (address &
-                                                   (TARGET_PAGE_SIZE - 1))) >> 2;
+                                                   (TARGET_PAGE_SIZE - 1)))
+                                                 >> 2;
                 }
             }
         }
@@ -1744,6 +1814,18 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
                       HWADDR_FMT_plx " prot %d\n",
                       __func__, address, ret, pa, prot);
 
+        if (ret == TRANSLATE_SUCCESS) {
+            ret = get_physical_address_mpt(env, &mpt_prot, pa,
+                                           access_type, mode);
+            if (riscv_cpu_cfg(env)->ext_smmpt) {
+                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;
+        }
+
         if (ret == TRANSLATE_SUCCESS) {
             ret = get_physical_address_pmp(env, &prot_pmp, pa,
                                            size, access_type, mode);
@@ -1757,8 +1839,13 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
             prot &= prot_pmp;
         }
     }
-
-    if (ret == TRANSLATE_PMP_FAIL || ret == TRANSLATE_PMA_FAIL) {
+    /*
+     * Both MPT (Machine-level Memory Protection Table, Smmpt extension) and
+     * PMP (Physical Memory Protection) follow the same exception reporting
+     * rule when an access violation is detected
+     */
+    if (ret == TRANSLATE_PMP_FAIL || ret == TRANSLATE_PMA_FAIL ||
+        ret == TRANSLATE_MPT_FAIL) {
         pmp_pma_violation = true;
     }
 
Signature · 1 line
2.43.0
author
#8 lore raw

PATCH v8 5/11 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.pa and minval.pa.

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 ++
 .../tcg/insn_trans/trans_privileged.c.inc     | 30 +++++++++++++++++++
 2 files changed, 32 insertions(+)
target/riscv/insn32.decode +2 -0
index 21272fdb50..f0917b7696 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_pa   0011001    ..... ..... 000 00000 1110011 @sfence_vma
+minval_pa   0011011    ..... ..... 000 00000 1110011 @sfence_vma
 
 # *** NMI ***
 mnret       0111000    00010 00000 000 00000 1110011
target/riscv/tcg/insn_trans/trans_privileged.c.inc +30 -0
index a8eaccef67..67f2e56034 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_pa(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_pa(DisasContext *ctx, arg_mfence_pa *a)
+{
+    return do_mfence_pa(ctx);
+}
+
+static bool trans_minval_pa(DisasContext *ctx, arg_minval_pa *a)
+{
+    return do_mfence_pa(ctx);
+}
Signature · 1 line
2.43.0
Reviewed-by on v8 5/11
#9 lore raw

Re: [PATCH v8 05/11] target/riscv: Implement SMMPT fence instructions

Quoted 9 lines · LIU Zhiwei

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

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

Quoted 7 lines

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

---

Quoted 55 lines
  target/riscv/insn32.decode                    |  2 ++
  .../tcg/insn_trans/trans_privileged.c.inc     | 30 +++++++++++++++++++
  2 files changed, 32 insertions(+)
target/riscv/insn32.decode +2 -0
index 21272fdb50..f0917b7696 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_pa   0011001    ..... ..... 000 00000 1110011 @sfence_vma
+minval_pa   0011011    ..... ..... 000 00000 1110011 @sfence_vma
 
  # *** NMI ***
  mnret       0111000    00010 00000 000 00000 1110011
target/riscv/tcg/insn_trans/trans_privileged.c.inc +30 -0
index a8eaccef67..67f2e56034 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_pa(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_pa(DisasContext *ctx, arg_mfence_pa *a)
+{
+    return do_mfence_pa(ctx);
+}
+
+static bool trans_minval_pa(DisasContext *ctx, arg_minval_pa *a)
+{
+    return do_mfence_pa(ctx);
+}
author
#10 lore raw

PATCH v8 6/11 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 0f96e487fa..727b2924a6 100644
@@ -264,9 +264,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
author
#11 lore raw

PATCH v8 7/11 target/riscv: Add disassembly for Smmpt instructions and CSRs

In reply to LIU Zhiwei

Add disassembly support to the RISC-V disassembler for the machine CSRs and fence instructions introduced by the Smmpt/Smsdid series:

- CSRs: mmpt (0x382), msdcfg (0x74e)
- Instructions: mfence.pa, minval.pa

Note that CSR 0x382 was previously mapped to the obsolete "mibase" register from the removed base-and-bound draft; it is now reused by Smmpt for mmpt.

---
 disas/riscv.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
disas/riscv.c +8 -1
index 7f1b262773..8fdae54449 100644
@@ -990,6 +990,8 @@ typedef enum {
     rv_op_cbo_flush = 958,
     rv_op_cbo_zero = 959,
     rv_op_mnret = 960,
+    rv_op_mfence_pa = 961,
+    rv_op_minval_pa = 962,
 } rv_op;
 
 /* register names */
@@ -2265,6 +2267,8 @@ const rv_opcode_data rvi_opcode_data[] = {
    { "cbo.flush", rv_codec_r, rv_fmt_rs1, NULL, 0, 0, 0 },
    { "cbo.zero", rv_codec_r, rv_fmt_rs1, NULL, 0, 0, 0 },
    { "mnret", rv_codec_none, rv_fmt_none, NULL, 0, 0, 0 },
+   { "mfence.pa", rv_codec_r, rv_fmt_rs1_rs2, NULL, 0, 0, 0 },
+   { "minval.pa", rv_codec_r, rv_fmt_rs1_rs2, NULL, 0, 0, 0 },
 };
 
 /* CSR names */
@@ -2356,7 +2360,7 @@ static const char *csr_name(int csrno)
     case 0x0344: return "mip";
     case 0x0380: return "mbase";
     case 0x0381: return "mbound";
-    case 0x0382: return "mibase";
+    case 0x0382: return "mmpt";
     case 0x0383: return "mibound";
     case 0x0384: return "mdbase";
     case 0x0385: return "mdbound";
@@ -2440,6 +2444,7 @@ static const char *csr_name(int csrno)
     case 0x03ed: return "pmpaddr61";
     case 0x03ee: return "pmpaddr62";
     case 0x03ef: return "pmpaddr63";
+    case 0x074e: return "msdcfg";
     case 0x0780: return "mtohost";
     case 0x0781: return "mfromhost";
     case 0x0782: return "mreset";
@@ -4079,6 +4084,8 @@ static void decode_inst_opcode(rv_decode *dec, rv_isa isa)
                     case 64: op = rv_op_mret; break;
                     }
                     break;
+                case 800: op = rv_op_mfence_pa; break;
+                case 864: op = rv_op_minval_pa; break;
                 case 1792:
                     switch ((inst >> 15) & 0b1111111111) {
                     case 64: op = rv_op_mnret; break;
Signature · 1 line
2.43.0
author
#12 lore raw

PATCH v8 8/11 target/riscv: Enable SMMPT extension

In reply to LIU Zhiwei

This patch implements v0.4.9 SMMPT specification(https://github.com/riscv/riscv-smmtt/releases/tag/v0.4.9).

---
 target/riscv/cpu.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
target/riscv/cpu.c +12 -1
index 727b2924a6..5f8455dae2 100644
@@ -265,8 +265,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_EXPERIMENTAL_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_EXPERIMENTAL_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),
@@ -2852,6 +2854,15 @@ static RISCVCPUImpliedExtsRule ZVFBFA_IMPLIED = {
     },
 };
 
+static RISCVCPUImpliedExtsRule SMMPT_IMPLIED = {
+    .ext = CPU_CFG_OFFSET(ext_smmpt),
+    .implied_multi_exts = {
+        CPU_CFG_OFFSET(ext_smsdid),
+
+        RISCV_IMPLIED_EXTS_RULE_END
+    },
+};
+
 RISCVCPUImpliedExtsRule *riscv_misa_ext_implied_rules[] = {
     &RVA_IMPLIED, &RVD_IMPLIED, &RVF_IMPLIED,
     &RVM_IMPLIED, &RVV_IMPLIED, &RVG_IMPLIED,
@@ -2873,7 +2884,7 @@ RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[] = {
     &ZVKS_IMPLIED,  &ZVKSC_IMPLIED, &ZVKSG_IMPLIED, &SHA_IMPLIED,
     &SSCFG_IMPLIED, &SUPM_IMPLIED, &SSPM_IMPLIED, &SMCTR_IMPLIED,
     &SSCTR_IMPLIED, &SSSTATEEN_IMPLIED,
-    NULL
+    &SMMPT_IMPLIED, NULL
 };
 
 static const Property riscv_cpu_properties[] = {
Signature · 1 line
2.43.0
author
#13 lore raw

PATCH v8 9/11 target/riscv: Add system test for SMMPT extension

In reply to LIU Zhiwei

Add a bare-metal M-mode assembly test (test-smmpt.S) that validates the Smmpt43 MPT lookup and permission enforcement against SMMTT v0.4.9.

The test builds a 3-level Smmpt43 MPT in RAM and uses the MPRV trick (mstatus.MPRV=1, MPP=S, satp=Bare) so that data accesses are subject to MPT checks while instruction fetches remain in M-mode (which bypasses the MPT). PMP is configured to grant all permissions so that only the MPT gates the accesses.

Coverage: mfence.pa in M-mode; every XWR encoding (RW, no-access, R-only, R+X, RWX, the reserved 010/110 encodings and X-only) checked for both load and store, verifying the correct exception cause (load access fault = 5, store/AMO access fault = 7); an invalid (V=0) leaf; a leaf with a reserved bit set; and NAPOT leaves with a valid and a reserved G field.

The mode-independent harness (MPRV helpers, per-access check/verify subroutines, the M-mode trap handler and the semihosting exit) lives in a shared smmpt-common.S so the Smmpt52/Smmpt64/Smmpt34 tests can reuse it.

---
 tests/tcg/riscv64/Makefile.softmmu-target |   4 +
 tests/tcg/riscv64/smmpt-common.S          | 257 ++++++++++++++++++++++
 tests/tcg/riscv64/test-smmpt.S            |  85 +++++++
 3 files changed, 346 insertions(+)
 create mode 100644 tests/tcg/riscv64/smmpt-common.S
 create mode 100644 tests/tcg/riscv64/test-smmpt.S
tests/tcg/riscv64/Makefile.softmmu-target +4 -0
index 82be8a2c91..18da9c6299 100644
@@ -41,5 +41,9 @@ comma:= ,
 run-test-crc32: test-crc32
 	$(call run-test, $<, $(QEMU) -cpu rv64$(comma)xlrbr=true $(QEMU_OPTS)$<)
 
+EXTRA_RUNS += run-test-smmpt
+run-test-smmpt: test-smmpt
+	$(call run-test, $<, $(QEMU) -cpu rv64$(comma)x-smmpt=true $(QEMU_OPTS)$<)
+
 # We don't currently support the multiarch system tests
 undefine MULTIARCH_TESTS
tests/tcg/riscv64/smmpt-common.S new file +257 -0

257 lines not on this page · raw message

tests/tcg/riscv64/test-smmpt.S new file +85 -0

85 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#14 lore raw

PATCH v8 10/11 target/riscv: Add system tests for Smmpt52 and Smmpt64

In reply to LIU Zhiwei

Extend the SMMPT system-test coverage to the two larger RV64 memory protection schemes: Smmpt52 (4-level MPT, 52-bit SPA) and Smmpt64 (5-level MPT, 64-bit SPA with a 32 KiB / 32 KiB-aligned root table).

Both tests reuse the mode-independent harness in smmpt-common.S and only differ in the number of radix levels and, for Smmpt64, the root-table size and alignment. They exercise the same permission and structural checks (all XWR encodings for load and store, V=0 and reserved-bit leaves, and NAPOT leaves with a valid and a reserved G field) and are wired into the Makefile as run-test-smmpt52 and run-test-smmpt64.

---
 tests/tcg/riscv64/Makefile.softmmu-target |  8 ++
 tests/tcg/riscv64/test-smmpt52.S          | 91 +++++++++++++++++++++
 tests/tcg/riscv64/test-smmpt64.S          | 98 +++++++++++++++++++++++
 3 files changed, 197 insertions(+)
 create mode 100644 tests/tcg/riscv64/test-smmpt52.S
 create mode 100644 tests/tcg/riscv64/test-smmpt64.S
tests/tcg/riscv64/Makefile.softmmu-target +8 -0
index 18da9c6299..6bbec02ed2 100644
@@ -45,5 +45,13 @@ EXTRA_RUNS += run-test-smmpt
 run-test-smmpt: test-smmpt
 	$(call run-test, $<, $(QEMU) -cpu rv64$(comma)x-smmpt=true $(QEMU_OPTS)$<)
 
+EXTRA_RUNS += run-test-smmpt52
+run-test-smmpt52: test-smmpt52
+	$(call run-test, $<, $(QEMU) -cpu rv64$(comma)x-smmpt=true $(QEMU_OPTS)$<)
+
+EXTRA_RUNS += run-test-smmpt64
+run-test-smmpt64: test-smmpt64
+	$(call run-test, $<, $(QEMU) -cpu rv64$(comma)x-smmpt=true $(QEMU_OPTS)$<)
+
 # We don't currently support the multiarch system tests
 undefine MULTIARCH_TESTS
tests/tcg/riscv64/test-smmpt52.S new file +91 -0

91 lines not on this page · raw message

tests/tcg/riscv64/test-smmpt64.S new file +98 -0

98 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#15 lore raw

PATCH v8 11/11 target/riscv: Add system test for Smmpt34

In reply to LIU Zhiwei

Add SMMPT system-test coverage for Smmpt34, the RV32-only memory protection scheme (2-level MPT with 4-byte entries).

Smmpt34 requires a 32-bit CPU, but qemu-system-riscv64 can run a 32-bit CPU (-cpu rv32), so the test lives in tests/tcg/riscv64 alongside the RV64 Smmpt tests and shares the same harness (smmpt-common.S). It only differs in the table geometry: 4-byte MPTEs, two radix levels, a 10-bit level-0 index and the RV32 mmpt CSR layout (MODE in bits[31:30]). It exercises the same permission and structural checks as the RV64 tests (all XWR encodings for load and store, V=0 and reserved-bit leaves, and NAPOT leaves with a valid and a reserved G field), using the same access addresses. It is built for RV32 and run on qemu-system-riscv64 with -cpu rv32.

---
 tests/tcg/riscv64/test-smmpt34.S | 97 ++++++++++++++++++++++++++++++++
 1 file changed, 97 insertions(+)
 create mode 100644 tests/tcg/riscv64/test-smmpt34.S
tests/tcg/riscv64/test-smmpt34.S new file +97 -0
new file mode 100644
index 0000000000..92c8320999
@@ -0,0 +1,97 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Test for the Smmpt34 (Supervisor Memory Protection Table) extension,
+ * per SMMTT specification v0.4.9.  Smmpt34 is the RV32-only scheme.
+ *
+ * Although Smmpt34 requires a 32-bit CPU, the program is run on
+ * qemu-system-riscv64 with a 32-bit CPU (-cpu rv32), which lives in the
+ * same target as the RV64 Smmpt tests.  It is therefore kept here in
+ * tests/tcg/riscv64 alongside the other Smmpt tests and shares the same
+ * harness (smmpt-common.S).  Build it for RV32 and run it as:
+ *
+ *   $CC -march=rv32ima_zicsr -mabi=ilp32 -I tests/tcg/riscv64 \
+ *       tests/tcg/riscv64/test-smmpt34.S -Wa,--noexecstack -c -o smmpt34.o
+ *   $LD -m elf32lriscv -T tests/tcg/riscv64/semihost.ld smmpt34.o -o smmpt34
+ *   qemu-system-riscv64 -cpu rv32,x-smmpt=true -M virt -display none \
+ *       -semihosting -device loader,file=smmpt34
+ *
+ * Smmpt34 uses a 2-level MPT with 4-byte entries (8 pages per leaf,
+ * pi = SPA[14:12]).  The supervisor physical address is partitioned as
+ * pn[1] = SPA[33:25] (9-bit root index), pn[0] = SPA[24:15] (10-bit level-0
+ * index) and range offset SPA[14:0].  For the RAM addresses used here
+ * (0x8xxx_xxxx, < 4 GiB) pn[1] resolves to 64.  See smmpt-common.S for the
+ * MPRV trick, the shared address layout and the exit-code convention.
+ *
+ * This test reuses the same shared harness and the same access addresses as
+ * the RV64 tests; only the table geometry (4-byte entries, 2 levels, 10-bit
+ * level-0 index) and the RV32 mmpt layout differ.
+ *
+ * MPT layout (Smmpt34, 2 levels, 4-byte entries):
+ *
+ *   L1 (root) @ 0x8040_0000 : entry[64]  -> non-leaf, PPN(L0)=0x80410
+ *   L0        @ 0x8041_0000 :
+ *     entry[160] -> non-NAPOT leaf (LEAF_ALL_XWR)  covering 0x8050_0000+
+ *     entry[162] -> V=0 (invalid)                  covering 0x8051_0000
+ *     entry[164] -> leaf with a reserved bit set    covering 0x8052_0000
+ *     entry[192] -> NAPOT leaf, G=6, XWR=RW         covering 0x8060_0000
+ *     entry[256] -> NAPOT leaf, reserved G=0        covering 0x8080_0000
+ *
+ *   mmpt = MODE(1=Smmpt34)<<30 | PPN(0x80400)
+ */
+
+#include "smmpt-common.S"
+
+	.option	norvc
+
+	.text
+	.global	_start
+_start:
+	/* Install the M-mode trap handler */
+	lla	t0, mtrap
+	csrw	mtvec, t0
+	csrw	medeleg, zero
+
+	/* PMP entry 0: NAPOT covering the whole space, RWX. */
+	li	t0, -1
+	csrw	pmpaddr0, t0
+	li	t0, 0x1f
+	csrw	pmpcfg0, t0
+
+	/* Build the MPT tables (M-mode stores bypass the MPT). */
+	/* L1[64] = non-leaf -> L0 (PPN 0x80410): (0x80410 << 10) | V */
+	li	t0, 0x80400000
+	li	t1, 0x20104001
+	sw	t1, 0x100(t0)		/* 64 * 4 = 0x100 */
+
+	li	t0, 0x80410000
+	/* L0[160] = non-NAPOT leaf carrying all XWR encodings (pi0..pi7) */
+	li	t1, LEAF_ALL_XWR
+	sw	t1, 0x280(t0)		/* 160 * 4 = 0x280 */
+	/* L0[162] = 0: invalid entry (V=0) */
+	sw	x0, 0x288(t0)		/* 162 * 4 = 0x288 */
+	/* L0[164] = leaf with reserved bit 3 set (V|L|rsv|XWR[pi0]=RW) */
+	li	t1, 0x30B
+	sw	t1, 0x290(t0)		/* 164 * 4 = 0x290 */
+	/* L0[192] = NAPOT leaf: V|L|N | XWR=RW(0x300) | G=6(0x6000) */
+	li	t1, 0x6307
+	sw	t1, 0x300(t0)		/* 192 * 4 = 0x300 */
+	/* L0[256] = NAPOT leaf with reserved G=0: V|L|N | XWR=RW | G=0 */
+	li	t1, 0x307
+	sw	t1, 0x400(t0)		/* 256 * 4 = 0x400 */
+
+	/* Program mmpt: MODE=1 (Smmpt34) at bits[31:30], PPN = 0x80400 */
+	li	t0, 0x40080400
+	csrw	0x382, t0		/* CSR_MMPT */
+
+	/* mfence.pa must not fault in M-mode */
+	TEST_MFENCE_PA 1
+
+	/* Run the shared permission and structural checks */
+	RUN_LEAF_CHECKS
+
+	/* All tests passed */
+	li	a0, 0
+	j	_exit
+
+	SMMPT_HARNESS
Signature · 1 line
2.43.0
#16 lore raw

Re: [PATCH v8 11/11] target/riscv: Add system test for Smmpt34

Quoted 14 lines · LIU Zhiwei

Add SMMPT system-test coverage for Smmpt34, the RV32-only memory protection scheme (2-level MPT with 4-byte entries).

Smmpt34 requires a 32-bit CPU, but qemu-system-riscv64 can run a 32-bit CPU (-cpu rv32), so the test lives in tests/tcg/riscv64 alongside the RV64 Smmpt tests and shares the same harness (smmpt-common.S). It only differs in the table geometry: 4-byte MPTEs, two radix levels, a 10-bit level-0 index and the RV32 mmpt CSR layout (MODE in bits[31:30]). It exercises the same permission and structural checks as the RV64 tests (all XWR encodings for load and store, V=0 and reserved-bit leaves, and NAPOT leaves with a valid and a reserved G field), using the same access addresses. It is built for RV32 and run on qemu-system-riscv64 with -cpu rv32.

Quoted 3 lines
---
  tests/tcg/riscv64/test-smmpt34.S | 97 ++++++++++++++++++++++++++++++++

I believe we're missing the meson/Makefile entry for this test.

Since it's a 32-bit test only we want it in the tests/tcg/riscv32/ dir.

Moving the file, and adding the following tests/tcg/riscv32/system/meson.build entry:

tests += {

  'test-smmpt34.S': {
    'exe_name': 'test-smmpt34',
    'cflags': cflags + ['-march=rv32gcv'],
    'qemu_args': ['-cpu', 'rv32,x-smmpt=true', qemu_args],

}
}

We'll have the following error:

FAILED: tests/tcg/riscv32-softmmu-test-smmpt34.test
/usr/bin/riscv64-linux-gnu-gcc ../tests/tcg/riscv32/test-smmpt34.S -o tests/tcg/riscv32-softmmu-test-smmpt34.test -static -MMD -MF tests/tcg/riscv32-softmmu-test-smmpt34.d -Wall -Werror -O0 -g -fno-strict-aliasing -fno-stack-protector -march=rv32im_zicsr -mabi=ilp32 -nostdlib -ffreestanding -Wa,--noexecstack -Wl,-T ../tests/tcg/riscv32/system/../semihost.ld -march=rv32gcv ../tests/tcg/riscv32/test-smmpt34.S:43:10: fatal error: smmpt-common.S: No such file or directory
43 | #include "smmpt-common.S"

To fix that you'll need a tests/tcg/riscv32/smmpt-common.S symlink to the original tests/tcg/riscv64/smmpt-common.S file.

Doing all that will make the test run successfully with 'make check-tcg'.

Also, for the other riscv64 test files from patches 09 and 10, adding this entry in tests/tcg/riscv64/system/meson.build will enable all of them:

tests += {

  'test-smmpt.S': {
    'exe_name': 'test-smmpt',
    'cflags': cflags + ['-march=rv64gcv'],
    'qemu_args': ['-cpu', 'rv64,x-smmpt=true', qemu_args],

},

  'test-smmpt52.S': {
    'exe_name': 'test-smmpt52',
    'cflags': cflags + ['-march=rv64gcv'],
    'qemu_args': ['-cpu', 'rv64,x-smmpt=true', qemu_args],

},

  'test-smmpt64.S': {
    'exe_name': 'test-smmpt64',
    'cflags': cflags + ['-march=rv64gcv'],
    'qemu_args': ['-cpu', 'rv64,x-smmpt=true', qemu_args],

}
}

Thanks,
Daniel

Quoted 106 lines
  1 file changed, 97 insertions(+)

create mode 100644 tests/tcg/riscv64/test-smmpt34.S

tests/tcg/riscv64/test-smmpt34.S new file +97 -0
new file mode 100644
index 0000000000..92c8320999
@@ -0,0 +1,97 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Test for the Smmpt34 (Supervisor Memory Protection Table) extension,
+ * per SMMTT specification v0.4.9.  Smmpt34 is the RV32-only scheme.
+ *
+ * Although Smmpt34 requires a 32-bit CPU, the program is run on
+ * qemu-system-riscv64 with a 32-bit CPU (-cpu rv32), which lives in the
+ * same target as the RV64 Smmpt tests.  It is therefore kept here in
+ * tests/tcg/riscv64 alongside the other Smmpt tests and shares the same
+ * harness (smmpt-common.S).  Build it for RV32 and run it as:
+ *
+ *   $CC -march=rv32ima_zicsr -mabi=ilp32 -I tests/tcg/riscv64 \
+ *       tests/tcg/riscv64/test-smmpt34.S -Wa,--noexecstack -c -o smmpt34.o
+ *   $LD -m elf32lriscv -T tests/tcg/riscv64/semihost.ld smmpt34.o -o smmpt34
+ *   qemu-system-riscv64 -cpu rv32,x-smmpt=true -M virt -display none \
+ *       -semihosting -device loader,file=smmpt34
+ *
+ * Smmpt34 uses a 2-level MPT with 4-byte entries (8 pages per leaf,
+ * pi = SPA[14:12]).  The supervisor physical address is partitioned as
+ * pn[1] = SPA[33:25] (9-bit root index), pn[0] = SPA[24:15] (10-bit level-0
+ * index) and range offset SPA[14:0].  For the RAM addresses used here
+ * (0x8xxx_xxxx, < 4 GiB) pn[1] resolves to 64.  See smmpt-common.S for the
+ * MPRV trick, the shared address layout and the exit-code convention.
+ *
+ * This test reuses the same shared harness and the same access addresses as
+ * the RV64 tests; only the table geometry (4-byte entries, 2 levels, 10-bit
+ * level-0 index) and the RV32 mmpt layout differ.
+ *
+ * MPT layout (Smmpt34, 2 levels, 4-byte entries):
+ *
+ *   L1 (root) @ 0x8040_0000 : entry[64]  -> non-leaf, PPN(L0)=0x80410
+ *   L0        @ 0x8041_0000 :
+ *     entry[160] -> non-NAPOT leaf (LEAF_ALL_XWR)  covering 0x8050_0000+
+ *     entry[162] -> V=0 (invalid)                  covering 0x8051_0000
+ *     entry[164] -> leaf with a reserved bit set    covering 0x8052_0000
+ *     entry[192] -> NAPOT leaf, G=6, XWR=RW         covering 0x8060_0000
+ *     entry[256] -> NAPOT leaf, reserved G=0        covering 0x8080_0000
+ *
+ *   mmpt = MODE(1=Smmpt34)<<30 | PPN(0x80400)
+ */
+
+#include "smmpt-common.S"
+
+	.option	norvc
+
+	.text
+	.global	_start
+_start:
+	/* Install the M-mode trap handler */
+	lla	t0, mtrap
+	csrw	mtvec, t0
+	csrw	medeleg, zero
+
+	/* PMP entry 0: NAPOT covering the whole space, RWX. */
+	li	t0, -1
+	csrw	pmpaddr0, t0
+	li	t0, 0x1f
+	csrw	pmpcfg0, t0
+
+	/* Build the MPT tables (M-mode stores bypass the MPT). */
+	/* L1[64] = non-leaf -> L0 (PPN 0x80410): (0x80410 << 10) | V */
+	li	t0, 0x80400000
+	li	t1, 0x20104001
+	sw	t1, 0x100(t0)		/* 64 * 4 = 0x100 */
+
+	li	t0, 0x80410000
+	/* L0[160] = non-NAPOT leaf carrying all XWR encodings (pi0..pi7) */
+	li	t1, LEAF_ALL_XWR
+	sw	t1, 0x280(t0)		/* 160 * 4 = 0x280 */
+	/* L0[162] = 0: invalid entry (V=0) */
+	sw	x0, 0x288(t0)		/* 162 * 4 = 0x288 */
+	/* L0[164] = leaf with reserved bit 3 set (V|L|rsv|XWR[pi0]=RW) */
+	li	t1, 0x30B
+	sw	t1, 0x290(t0)		/* 164 * 4 = 0x290 */
+	/* L0[192] = NAPOT leaf: V|L|N | XWR=RW(0x300) | G=6(0x6000) */
+	li	t1, 0x6307
+	sw	t1, 0x300(t0)		/* 192 * 4 = 0x300 */
+	/* L0[256] = NAPOT leaf with reserved G=0: V|L|N | XWR=RW | G=0 */
+	li	t1, 0x307
+	sw	t1, 0x400(t0)		/* 256 * 4 = 0x400 */
+
+	/* Program mmpt: MODE=1 (Smmpt34) at bits[31:30], PPN = 0x80400 */
+	li	t0, 0x40080400
+	csrw	0x382, t0		/* CSR_MMPT */
+
+	/* mfence.pa must not fault in M-mode */
+	TEST_MFENCE_PA 1
+
+	/* Run the shared permission and structural checks */
+	RUN_LEAF_CHECKS
+
+	/* All tests passed */
+	li	a0, 0
+	j	_exit
+
+	SMMPT_HARNESS
#17 lore raw
In reply to LIU Zhiwei
Quoted 25 lines · LIU Zhiwei

This patch set introduces support for the RISC-V Smsdid and Smmpt (Supervisor Domain Identifier and Memory Protection Table) extensions v0.4.9(https://github.com/riscv/riscv-smmtt/releases/tag/v0.4.9). 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

Rahul Pathak is working on SMMPT OpenSBI support -
https://lists.infradead.org/pipermail/opensbi/2026-July/010235.html

based on his QEMU SMMPT branch -
https://github.com/pathakraul/qemu/tree/rpathak_smmpt_v1

As we all agree -
https://patchew.org/QEMU/[email protected]/
, we will collaborate on this work and not duplicate efforts. Rahul will focus on OpenSBI upstream and I will update the QEMU patch set to v8.

Xiangyi will review the OpenSBI SMMPT patch set and may collaborate to upstream the other parts, such as SMSDIA and IOMPT.

Quoted 6 lines

v7->v8:
1. Update the mfence.pa and minval.pa instruction encodings to match the upstream riscv-opcodes definition
(https://github.com/riscv/riscv-opcodes/pull/389/): mfence.pa now uses funct7=0b0011001 and minval.pa uses funct7=0b0011011. The decoder, disassembler and the system test are updated accordingly.

I have posted the updated OpenSBI SMSDID+SMMPT V2 series which aligns with this series version

https://lists.infradead.org/pipermail/opensbi/2026-July/010288.html

#18 lore raw
In reply to LIU Zhiwei

Hi Zhiwei,

Sorry from taking this long to take a look at this work. I am afraid I'll need to ask you to send a v9 due to the following upstream changes, aside from some review comments I made:

- upstream changed how tcg tests are invoked (we use meson now), i.e. you need to add changes in tests/tcg/riscv64/system/meson.build instead of Makefile.softmmu-target;

- upstream also changed how we declare things in disas/riscv.c.

If you send a v9 I'll prioritize it for review. We can get this done for 11.2.

Cheers,
Daniel

Quoted 121 lines · LIU Zhiwei

This patch set introduces support for the RISC-V Smsdid and Smmpt (Supervisor Domain Identifier and Memory Protection Table) extensions v0.4.9(https://github.com/riscv/riscv-smmtt/releases/tag/v0.4.9). 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

Rahul Pathak is working on SMMPT OpenSBI support -
https://lists.infradead.org/pipermail/opensbi/2026-July/010235.html

based on his QEMU SMMPT branch -
https://github.com/pathakraul/qemu/tree/rpathak_smmpt_v1

As we all agree -
https://patchew.org/QEMU/[email protected]/
, we will collaborate on this work and not duplicate efforts. Rahul will focus on OpenSBI upstream and I will update the QEMU patch set to v8.

Xiangyi will review the OpenSBI SMMPT patch set and may collaborate to upstream the other parts, such as SMSDIA and IOMPT.

v7->v8:
1. Update the mfence.pa and minval.pa instruction encodings to match the upstream riscv-opcodes definition
(https://github.com/riscv/riscv-opcodes/pull/389/): mfence.pa now uses funct7=0b0011001 and minval.pa uses funct7=0b0011011. The decoder, disassembler and the system test are updated accordingly.

v6->v7:
The implementation is updated from the v0.3.4 specification to v0.4.9. The main changes are:
1. Update the mmpt CSR number to 0x382 and msdcfg to 0x74E.
2. Update the mmpt register layout: the SDID and PPN field positions changed and the RV64 PPN is now 44 bits.
3. Rework the MPTE format: the N (NAPOT) bit now lives at bit 2, the non-leaf NAPOT form is removed, NAPOT leaf entries encode a single XWR tuple plus a G granularity field, and non-NAPOT leaf entries hold per-page XWR tuples.
4. Treat XWR=000 as "no access" and a non-leaf entry with N=1 as a fault.
5. Enforce the Smmpt64 root-table PPN alignment (low 3 bits zero).
6. Rename the fence instructions mfence.spa/minval.spa to mfence.pa/minval.pa.
7. Drop stale Reviewed-by tags on the substantially reworked patches.
8. Introduce the configurable "smmpt-sdidlen" CPU property.
9. Add disassembly support for the Smmpt.
10. Add bare-metal M-mode system tests.
11. Rebase to master.

v5->v6:
1. Use explicitly bit fields extract instead of mpte_union_t.
2. Use the same exception behavior for MPT valiation as PMP valiation.
3. Use PAGE_* instead of MPT_ACCESS_* as they have same value.
4. Use address_space_*_le instead of address_space_* for SMMPT.
5. Only print SMMPT address check log when SMMPT is enabled.
6. Add implied rule for SMMPT as it depends on SMSDID.
7. Rebase to master.

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 (11):
target/riscv: Add basic definitions and CSRs for SMMPT target/riscv: Add smmpt-sdidlen property for the mmpt SDID field 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: Add disassembly for Smmpt instructions and CSRs target/riscv: Enable SMMPT extension
target/riscv: Add system test for SMMPT extension target/riscv: Add system tests for Smmpt52 and Smmpt64 target/riscv: Add system test for Smmpt34

  disas/riscv.c                                 |   9 +-
  target/riscv/cpu.c                            |  60 +++-
  target/riscv/cpu.h                            |   8 +
  target/riscv/cpu_bits.h                       |  27 ++
  target/riscv/cpu_cfg_fields.h.inc             |   3 +
  target/riscv/insn32.decode                    |   2 +
  target/riscv/meson.build                      |   1 +
  target/riscv/riscv_smmpt.c                    | 339 ++++++++++++++++++
  target/riscv/riscv_smmpt.h                    |  26 ++
  target/riscv/tcg/cpu_helper.c                 | 119 +++++-
  target/riscv/tcg/csr.c                        | 106 ++++++
  .../tcg/insn_trans/trans_privileged.c.inc     |  30 ++
  target/riscv/tcg/pmp.h                        |   3 +
  tests/tcg/riscv64/Makefile.softmmu-target     |  12 +
  tests/tcg/riscv64/smmpt-common.S              | 257 +++++++++++++
  tests/tcg/riscv64/test-smmpt.S                |  85 +++++
  tests/tcg/riscv64/test-smmpt34.S              |  97 +++++
  tests/tcg/riscv64/test-smmpt52.S              |  91 +++++
  tests/tcg/riscv64/test-smmpt64.S              |  98 +++++
  19 files changed, 1354 insertions(+), 19 deletions(-)

create mode 100644 target/riscv/riscv_smmpt.c create mode 100644 target/riscv/riscv_smmpt.h create mode 100644 tests/tcg/riscv64/smmpt-common.S create mode 100644 tests/tcg/riscv64/test-smmpt.S create mode 100644 tests/tcg/riscv64/test-smmpt34.S create mode 100644 tests/tcg/riscv64/test-smmpt52.S create mode 100644 tests/tcg/riscv64/test-smmpt64.S

base-commit: 499039798cdad7d86b787fec0eaf1da4151c0f05

author
#19 lore raw
Quoted 11 lines · Daniel Henrique Barboza

Hi Zhiwei,

Sorry from taking this long to take a look at this work.  I am afraid I'll need to ask you to send a v9 due to the following upstream changes, aside from some review comments I made:

- upstream changed how tcg tests are invoked (we use meson now), i.e. you need to add changes in tests/tcg/riscv64/system/meson.build instead of Makefile.softmmu-target;

Quoted 6 lines

- upstream also changed how we declare things in disas/riscv.c.

If you send a v9 I'll prioritize it for review.  We can get this done for 11.2.

Hi Daniel,

Thank you for taking the time to review this! No worries about the delay.

I appreciate the heads-up on the upstream changes. I will update the patch accordingly:

Switch from Makefile.softmmu-target to tests/tcg/riscv64/system/meson.build for the TCG test invocation;
Update the declarations in disas/riscv.c to align with the latest upstream changes.
I will send out v9 as soon as possible. Really glad to hear we can target 11.2 for this — I'll make sure to prioritize it on my end as well.

Thanks again for the review and for keeping this moving!

Best regards,
Zhiwei

Quoted 128 lines

Cheers,
Daniel

Quoted 122 lines · LIU Zhiwei

This patch set introduces support for the RISC-V Smsdid and Smmpt (Supervisor Domain Identifier and Memory Protection Table) extensions v0.4.9(https://github.com/riscv/riscv-smmtt/releases/tag/v0.4.9). 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

Rahul Pathak is working on SMMPT OpenSBI support -
https://lists.infradead.org/pipermail/opensbi/2026-July/010235.html

based on his QEMU SMMPT branch -
https://github.com/pathakraul/qemu/tree/rpathak_smmpt_v1

As we all agree -
https://patchew.org/QEMU/[email protected]/

, we will collaborate on this work and not duplicate efforts. Rahul will focus on OpenSBI upstream and I will update the QEMU patch set to v8.

Xiangyi will review the OpenSBI SMMPT patch set and may collaborate to upstream the other parts, such as SMSDIA and IOMPT.

v7->v8:
     1. Update the mfence.pa and minval.pa instruction encodings to match the upstream riscv-opcodes definition
        (https://github.com/riscv/riscv-opcodes/pull/389/): mfence.pa now uses funct7=0b0011001 and minval.pa uses funct7=0b0011011. The decoder, disassembler and the system test are updated accordingly.

v6->v7:
     The implementation is updated from the v0.3.4 specification to v0.4.9. The main changes are:
     1. Update the mmpt CSR number to 0x382 and msdcfg to 0x74E.
     2. Update the mmpt register layout: the SDID and PPN field positions changed and the RV64 PPN is now 44 bits.
     3. Rework the MPTE format: the N (NAPOT) bit now lives at bit 2, the non-leaf NAPOT form is removed, NAPOT leaf entries encode a single XWR tuple plus a G granularity field, and non-NAPOT leaf entries hold per-page XWR tuples.
     4. Treat XWR=000 as "no access" and a non-leaf entry with N=1 as a fault.
     5. Enforce the Smmpt64 root-table PPN alignment (low 3 bits zero).
     6. Rename the fence instructions mfence.spa/minval.spa to mfence.pa/minval.pa.
     7. Drop stale Reviewed-by tags on the substantially reworked patches.
     8. Introduce the configurable "smmpt-sdidlen" CPU property.
     9. Add disassembly support for the Smmpt.
     10. Add bare-metal M-mode system tests.
     11. Rebase to master.

v5->v6:
     1. Use explicitly bit fields extract instead of mpte_union_t.
     2. Use the same exception behavior for MPT valiation as PMP valiation.
     3. Use PAGE_* instead of MPT_ACCESS_* as they have same value.
     4. Use address_space_*_le instead of address_space_* for SMMPT.
     5. Only print SMMPT address check log when SMMPT is enabled.
     6. Add implied rule for SMMPT as it depends on SMSDID.
     7. Rebase to master.

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 (11):
   target/riscv: Add basic definitions and CSRs for SMMPT target/riscv: Add smmpt-sdidlen property for the mmpt SDID field 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: Add disassembly for Smmpt instructions and CSRs target/riscv: Enable SMMPT extension
   target/riscv: Add system test for SMMPT extension target/riscv: Add system tests for Smmpt52 and Smmpt64 target/riscv: Add system test for Smmpt34

  disas/riscv.c                                 |   9 +- target/riscv/cpu.c                            |  60 +++- target/riscv/cpu.h                            |   8 + target/riscv/cpu_bits.h                       |  27 ++ target/riscv/cpu_cfg_fields.h.inc             |   3 + target/riscv/insn32.decode                    |   2 + target/riscv/meson.build                      |   1 + target/riscv/riscv_smmpt.c                    | 339 ++++++++++++++++++ target/riscv/riscv_smmpt.h                    |  26 ++ target/riscv/tcg/cpu_helper.c                 | 119 +++++- target/riscv/tcg/csr.c                        | 106 ++++++ .../tcg/insn_trans/trans_privileged.c.inc     |  30 ++ target/riscv/tcg/pmp.h                        |   3 + tests/tcg/riscv64/Makefile.softmmu-target     |  12 + tests/tcg/riscv64/smmpt-common.S              | 257 +++++++++++++ tests/tcg/riscv64/test-smmpt.S                |  85 +++++

  tests/tcg/riscv64/test-smmpt34.S              |  97 +++++
  tests/tcg/riscv64/test-smmpt52.S              |  91 +++++
  tests/tcg/riscv64/test-smmpt64.S              |  98 +++++
  19 files changed, 1354 insertions(+), 19 deletions(-)

  create mode 100644 target/riscv/riscv_smmpt.c create mode 100644 target/riscv/riscv_smmpt.h create mode 100644 tests/tcg/riscv64/smmpt-common.S create mode 100644 tests/tcg/riscv64/test-smmpt.S create mode 100644 tests/tcg/riscv64/test-smmpt34.S create mode 100644 tests/tcg/riscv64/test-smmpt52.S create mode 100644 tests/tcg/riscv64/test-smmpt64.S

base-commit: 499039798cdad7d86b787fec0eaf1da4151c0f05

#20 lore raw

Hi Zhiwei,

The SMMPT spec has been updated following the ARC comments
and the big change is the merging of both SMSDID and SMMPT sub extensions into one with new name "SMSD" which is much better now.

https://github.com/riscv/riscv-smmtt/commit/ff5069fa99fc1fdd97c9fb8d96329e13afa82712

Can you please update this series.

Thanks
Rahul

Quoted 174 lines · LIU Zhiwei
Quoted 12 lines · Daniel Henrique Barboza

Hi Zhiwei,

Sorry from taking this long to take a look at this work. I am afraid I'll
need to ask you to send a v9 due to the following upstream changes, aside from some review comments I made:

- upstream changed how tcg tests are invoked (we use meson now), i.e. you need to add changes in tests/tcg/riscv64/system/meson.build instead of Makefile.softmmu-target;

Quoted 6 lines

- upstream also changed how we declare things in disas/riscv.c.

If you send a v9 I'll prioritize it for review. We can get this done for 11.2.

Hi Daniel,

Thank you for taking the time to review this! No worries about the delay.

I appreciate the heads-up on the upstream changes. I will update the patch accordingly:

Switch from Makefile.softmmu-target to tests/tcg/riscv64/system/meson.build for the TCG test invocation; Update the declarations in disas/riscv.c to align with the latest upstream changes.
I will send out v9 as soon as possible. Really glad to hear we can target 11.2 for this — I'll make sure to prioritize it on my end as well.

Thanks again for the review and for keeping this moving!

Best regards,
Zhiwei

Quoted 135 lines

Cheers,
Daniel

Quoted 129 lines · LIU Zhiwei

This patch set introduces support for the RISC-V Smsdid and Smmpt (Supervisor Domain Identifier and Memory Protection Table) extensions v0.4.9(https://github.com/riscv/riscv-smmtt/releases/tag/v0.4.9). 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

Rahul Pathak is working on SMMPT OpenSBI support -
https://lists.infradead.org/pipermail/opensbi/2026-July/010235.html

based on his QEMU SMMPT branch -
https://github.com/pathakraul/qemu/tree/rpathak_smmpt_v1

As we all agree -
https://patchew.org/QEMU/[email protected]/

, we will collaborate on this work and not duplicate efforts. Rahul will focus on OpenSBI upstream and I will update the QEMU patch set to v8.

Xiangyi will review the OpenSBI SMMPT patch set and may collaborate to upstream the other parts, such as SMSDIA and IOMPT.

v7->v8:
1. Update the mfence.pa and minval.pa instruction encodings to
match the
upstream riscv-opcodes definition
(https://github.com/riscv/riscv-opcodes/pull/389/): mfence.pa
now uses

        funct7=0b0011001 and minval.pa uses funct7=0b0011011. The

decoder,
disassembler and the system test are updated accordingly.

v6->v7:
The implementation is updated from the v0.3.4 specification to v0.4.9. The main changes are:
1. Update the mmpt CSR number to 0x382 and msdcfg to 0x74E.
2. Update the mmpt register layout: the SDID and PPN field
positions
changed and the RV64 PPN is now 44 bits.
3. Rework the MPTE format: the N (NAPOT) bit now lives at bit 2,
the
non-leaf NAPOT form is removed, NAPOT leaf entries encode a
single
XWR tuple plus a G granularity field, and non-NAPOT leaf entries hold per-page XWR tuples.
4. Treat XWR=000 as "no access" and a non-leaf entry with N=1 as a fault.
5. Enforce the Smmpt64 root-table PPN alignment (low 3 bits zero).
6. Rename the fence instructions mfence.spa/minval.spa to mfence.pa/minval.pa.
7. Drop stale Reviewed-by tags on the substantially reworked
patches.
8. Introduce the configurable "smmpt-sdidlen" CPU property.
9. Add disassembly support for the Smmpt.
10. Add bare-metal M-mode system tests.
11. Rebase to master.

v5->v6:
1. Use explicitly bit fields extract instead of mpte_union_t.
2. Use the same exception behavior for MPT valiation as PMP valiation.
3. Use PAGE_* instead of MPT_ACCESS_* as they have same value.
4. Use address_space_*_le instead of address_space_* for SMMPT.
5. Only print SMMPT address check log when SMMPT is enabled.
6. Add implied rule for SMMPT as it depends on SMSDID.
7. Rebase to master.

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 (11):
target/riscv: Add basic definitions and CSRs for SMMPT target/riscv: Add smmpt-sdidlen property for the mmpt SDID field 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: Add disassembly for Smmpt instructions and CSRs target/riscv: Enable SMMPT extension
target/riscv: Add system test for SMMPT extension target/riscv: Add system tests for Smmpt52 and Smmpt64 target/riscv: Add system test for Smmpt34

  disas/riscv.c                                 |   9 +-
  target/riscv/cpu.c                            |  60 +++-
  target/riscv/cpu.h                            |   8 +
  target/riscv/cpu_bits.h                       |  27 ++
  target/riscv/cpu_cfg_fields.h.inc             |   3 +
  target/riscv/insn32.decode                    |   2 +
  target/riscv/meson.build                      |   1 +
  target/riscv/riscv_smmpt.c                    | 339 ++++++++++++++++++
  target/riscv/riscv_smmpt.h                    |  26 ++
  target/riscv/tcg/cpu_helper.c                 | 119 +++++-
  target/riscv/tcg/csr.c                        | 106 ++++++
  .../tcg/insn_trans/trans_privileged.c.inc     |  30 ++
  target/riscv/tcg/pmp.h                        |   3 +
  tests/tcg/riscv64/Makefile.softmmu-target     |  12 +
  tests/tcg/riscv64/smmpt-common.S              | 257 +++++++++++++
  tests/tcg/riscv64/test-smmpt.S                |  85 +++++
  tests/tcg/riscv64/test-smmpt34.S              |  97 +++++
  tests/tcg/riscv64/test-smmpt52.S              |  91 +++++
  tests/tcg/riscv64/test-smmpt64.S              |  98 +++++
  19 files changed, 1354 insertions(+), 19 deletions(-)

create mode 100644 target/riscv/riscv_smmpt.c create mode 100644 target/riscv/riscv_smmpt.h create mode 100644 tests/tcg/riscv64/smmpt-common.S create mode 100644 tests/tcg/riscv64/test-smmpt.S create mode 100644 tests/tcg/riscv64/test-smmpt34.S create mode 100644 tests/tcg/riscv64/test-smmpt52.S create mode 100644 tests/tcg/riscv64/test-smmpt64.S

base-commit: 499039798cdad7d86b787fec0eaf1da4151c0f05

#21 lore raw
Quoted 9 lines · Rahul Pathak

Hi Zhiwei,

The SMMPT spec has been updated following the ARC comments
and the big change is the merging of both SMSDID and SMMPT sub extensions into one with new name "SMSD" which is much better now.

https://github.com/riscv/riscv-smmtt/commit/ff5069fa99fc1fdd97c9fb8d96329e13afa82712

Can you please update this series.

And while you're at it can you please update the TCG tests to use the new meson infrastructure? It's a matter of changing patches 09, 10 and 11 to add entries in tests/tcg/riscv64/system/meson.build instead of Makefile.softmmu-target.

Cheers,
Daniel

Quoted 180 lines

Thanks
Rahul

Quoted 174 lines · LIU Zhiwei
Quoted 12 lines · Daniel Henrique Barboza

Hi Zhiwei,

Sorry from taking this long to take a look at this work. I am afraid I'll
need to ask you to send a v9 due to the following upstream changes, aside from some review comments I made:

- upstream changed how tcg tests are invoked (we use meson now), i.e. you need to add changes in tests/tcg/riscv64/system/meson.build instead of Makefile.softmmu-target;

Quoted 6 lines

- upstream also changed how we declare things in disas/riscv.c.

If you send a v9 I'll prioritize it for review. We can get this done for 11.2.

Hi Daniel,

Thank you for taking the time to review this! No worries about the delay.

I appreciate the heads-up on the upstream changes. I will update the patch accordingly:

Switch from Makefile.softmmu-target to tests/tcg/riscv64/system/meson.build for the TCG test invocation; Update the declarations in disas/riscv.c to align with the latest upstream changes.
I will send out v9 as soon as possible. Really glad to hear we can target 11.2 for this — I'll make sure to prioritize it on my end as well.

Thanks again for the review and for keeping this moving!

Best regards,
Zhiwei

Quoted 135 lines

Cheers,
Daniel

Quoted 129 lines · LIU Zhiwei

This patch set introduces support for the RISC-V Smsdid and Smmpt (Supervisor Domain Identifier and Memory Protection Table) extensions v0.4.9(https://github.com/riscv/riscv-smmtt/releases/tag/v0.4.9). 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

Rahul Pathak is working on SMMPT OpenSBI support -
https://lists.infradead.org/pipermail/opensbi/2026-July/010235.html

based on his QEMU SMMPT branch -
https://github.com/pathakraul/qemu/tree/rpathak_smmpt_v1

As we all agree -
https://patchew.org/QEMU/[email protected]/

, we will collaborate on this work and not duplicate efforts. Rahul will focus on OpenSBI upstream and I will update the QEMU patch set to v8.

Xiangyi will review the OpenSBI SMMPT patch set and may collaborate to upstream the other parts, such as SMSDIA and IOMPT.

v7->v8:
1. Update the mfence.pa and minval.pa instruction encodings to
match the
upstream riscv-opcodes definition
(https://github.com/riscv/riscv-opcodes/pull/389/): mfence.pa
now uses

         funct7=0b0011001 and minval.pa uses funct7=0b0011011. The

decoder,
disassembler and the system test are updated accordingly.

v6->v7:
The implementation is updated from the v0.3.4 specification to v0.4.9. The main changes are:
1. Update the mmpt CSR number to 0x382 and msdcfg to 0x74E.
2. Update the mmpt register layout: the SDID and PPN field
positions
changed and the RV64 PPN is now 44 bits.
3. Rework the MPTE format: the N (NAPOT) bit now lives at bit 2,
the
non-leaf NAPOT form is removed, NAPOT leaf entries encode a
single
XWR tuple plus a G granularity field, and non-NAPOT leaf entries hold per-page XWR tuples.
4. Treat XWR=000 as "no access" and a non-leaf entry with N=1 as a fault.
5. Enforce the Smmpt64 root-table PPN alignment (low 3 bits zero).
6. Rename the fence instructions mfence.spa/minval.spa to mfence.pa/minval.pa.
7. Drop stale Reviewed-by tags on the substantially reworked
patches.
8. Introduce the configurable "smmpt-sdidlen" CPU property.
9. Add disassembly support for the Smmpt.
10. Add bare-metal M-mode system tests.
11. Rebase to master.

v5->v6:
1. Use explicitly bit fields extract instead of mpte_union_t.
2. Use the same exception behavior for MPT valiation as PMP valiation.
3. Use PAGE_* instead of MPT_ACCESS_* as they have same value.
4. Use address_space_*_le instead of address_space_* for SMMPT.
5. Only print SMMPT address check log when SMMPT is enabled.
6. Add implied rule for SMMPT as it depends on SMSDID.
7. Rebase to master.

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 (11):
target/riscv: Add basic definitions and CSRs for SMMPT target/riscv: Add smmpt-sdidlen property for the mmpt SDID field 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: Add disassembly for Smmpt instructions and CSRs target/riscv: Enable SMMPT extension
target/riscv: Add system test for SMMPT extension target/riscv: Add system tests for Smmpt52 and Smmpt64 target/riscv: Add system test for Smmpt34

   disas/riscv.c                                 |   9 +-
   target/riscv/cpu.c                            |  60 +++-
   target/riscv/cpu.h                            |   8 +
   target/riscv/cpu_bits.h                       |  27 ++
   target/riscv/cpu_cfg_fields.h.inc             |   3 +
   target/riscv/insn32.decode                    |   2 +
   target/riscv/meson.build                      |   1 +
   target/riscv/riscv_smmpt.c                    | 339 ++++++++++++++++++
   target/riscv/riscv_smmpt.h                    |  26 ++
   target/riscv/tcg/cpu_helper.c                 | 119 +++++-
   target/riscv/tcg/csr.c                        | 106 ++++++
   .../tcg/insn_trans/trans_privileged.c.inc     |  30 ++
   target/riscv/tcg/pmp.h                        |   3 +
   tests/tcg/riscv64/Makefile.softmmu-target     |  12 +
   tests/tcg/riscv64/smmpt-common.S              | 257 +++++++++++++
   tests/tcg/riscv64/test-smmpt.S                |  85 +++++
   tests/tcg/riscv64/test-smmpt34.S              |  97 +++++
   tests/tcg/riscv64/test-smmpt52.S              |  91 +++++
   tests/tcg/riscv64/test-smmpt64.S              |  98 +++++
   19 files changed, 1354 insertions(+), 19 deletions(-)

create mode 100644 target/riscv/riscv_smmpt.c create mode 100644 target/riscv/riscv_smmpt.h create mode 100644 tests/tcg/riscv64/smmpt-common.S create mode 100644 tests/tcg/riscv64/test-smmpt.S create mode 100644 tests/tcg/riscv64/test-smmpt34.S create mode 100644 tests/tcg/riscv64/test-smmpt52.S create mode 100644 tests/tcg/riscv64/test-smmpt64.S

base-commit: 499039798cdad7d86b787fec0eaf1da4151c0f05

author
#22 lore raw
In reply to Rahul Pathak
Quoted 9 lines · Rahul Pathak

Hi Zhiwei,

The SMMPT spec has been updated following the ARC comments
and the big change is the merging of both SMSDID and SMMPT sub extensions into one with new name "SMSD" which is much better now.

https://github.com/riscv/riscv-smmtt/commit/ff5069fa99fc1fdd97c9fb8d96329e13afa82712

Can you please update this series.

Hi Rahul and Daniel,

I see the latest specification is v0.9.0 now. Supporting v0.4.9 is not necessary. I will update this patch set to v0.9.0 together with updated test cases.

Thanks,
Zhiwei

Quoted 177 lines

Thanks
Rahul

Quoted 171 lines · LIU Zhiwei
Quoted 12 lines · Daniel Henrique Barboza

Hi Zhiwei,

Sorry from taking this long to take a look at this work. I am afraid I'll
need to ask you to send a v9 due to the following upstream changes, aside from some review comments I made:

- upstream changed how tcg tests are invoked (we use meson now), i.e. you need to add changes in tests/tcg/riscv64/system/meson.build instead of Makefile.softmmu-target;

Quoted 6 lines

- upstream also changed how we declare things in disas/riscv.c.

If you send a v9 I'll prioritize it for review. We can get this done for 11.2.

Hi Daniel,

Thank you for taking the time to review this! No worries about the delay.

I appreciate the heads-up on the upstream changes. I will update the patch accordingly:

Switch from Makefile.softmmu-target to tests/tcg/riscv64/system/meson.build for the TCG test invocation; Update the declarations in disas/riscv.c to align with the latest upstream changes.
I will send out v9 as soon as possible. Really glad to hear we can target 11.2 for this — I'll make sure to prioritize it on my end as well.

Thanks again for the review and for keeping this moving!

Best regards,
Zhiwei

Quoted 133 lines

Cheers,
Daniel

Quoted 129 lines · LIU Zhiwei

This patch set introduces support for the RISC-V Smsdid and Smmpt (Supervisor Domain Identifier and Memory Protection Table) extensions v0.4.9(https://github.com/riscv/riscv-smmtt/releases/tag/v0.4.9). 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

Rahul Pathak is working on SMMPT OpenSBI support -
https://lists.infradead.org/pipermail/opensbi/2026-July/010235.html

based on his QEMU SMMPT branch -
https://github.com/pathakraul/qemu/tree/rpathak_smmpt_v1

As we all agree -
https://patchew.org/QEMU/[email protected]/

, we will collaborate on this work and not duplicate efforts. Rahul will focus on OpenSBI upstream and I will update the QEMU patch set to v8.

Xiangyi will review the OpenSBI SMMPT patch set and may collaborate to upstream the other parts, such as SMSDIA and IOMPT.

v7->v8:
1. Update the mfence.pa and minval.pa instruction encodings to
match the
upstream riscv-opcodes definition
(https://github.com/riscv/riscv-opcodes/pull/389/): mfence.pa
now uses

         funct7=0b0011001 and minval.pa uses funct7=0b0011011. The

decoder,
disassembler and the system test are updated accordingly.

v6->v7:
The implementation is updated from the v0.3.4 specification to v0.4.9. The main changes are:
1. Update the mmpt CSR number to 0x382 and msdcfg to 0x74E.
2. Update the mmpt register layout: the SDID and PPN field
positions
changed and the RV64 PPN is now 44 bits.
3. Rework the MPTE format: the N (NAPOT) bit now lives at bit 2,
the
non-leaf NAPOT form is removed, NAPOT leaf entries encode a
single
XWR tuple plus a G granularity field, and non-NAPOT leaf entries hold per-page XWR tuples.
4. Treat XWR=000 as "no access" and a non-leaf entry with N=1 as a fault.
5. Enforce the Smmpt64 root-table PPN alignment (low 3 bits zero).
6. Rename the fence instructions mfence.spa/minval.spa to mfence.pa/minval.pa.
7. Drop stale Reviewed-by tags on the substantially reworked
patches.
8. Introduce the configurable "smmpt-sdidlen" CPU property.
9. Add disassembly support for the Smmpt.
10. Add bare-metal M-mode system tests.
11. Rebase to master.

v5->v6:
1. Use explicitly bit fields extract instead of mpte_union_t.
2. Use the same exception behavior for MPT valiation as PMP valiation.
3. Use PAGE_* instead of MPT_ACCESS_* as they have same value.
4. Use address_space_*_le instead of address_space_* for SMMPT.
5. Only print SMMPT address check log when SMMPT is enabled.
6. Add implied rule for SMMPT as it depends on SMSDID.
7. Rebase to master.

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 (11):
target/riscv: Add basic definitions and CSRs for SMMPT target/riscv: Add smmpt-sdidlen property for the mmpt SDID field 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: Add disassembly for Smmpt instructions and CSRs target/riscv: Enable SMMPT extension
target/riscv: Add system test for SMMPT extension target/riscv: Add system tests for Smmpt52 and Smmpt64 target/riscv: Add system test for Smmpt34

   disas/riscv.c                                 |   9 +-
   target/riscv/cpu.c                            |  60 +++-
   target/riscv/cpu.h                            |   8 +
   target/riscv/cpu_bits.h                       |  27 ++
   target/riscv/cpu_cfg_fields.h.inc             |   3 +
   target/riscv/insn32.decode                    |   2 +
   target/riscv/meson.build                      |   1 +
   target/riscv/riscv_smmpt.c                    | 339 ++++++++++++++++++
   target/riscv/riscv_smmpt.h                    |  26 ++
   target/riscv/tcg/cpu_helper.c                 | 119 +++++-
   target/riscv/tcg/csr.c                        | 106 ++++++
   .../tcg/insn_trans/trans_privileged.c.inc     |  30 ++
   target/riscv/tcg/pmp.h                        |   3 +
   tests/tcg/riscv64/Makefile.softmmu-target     |  12 +
   tests/tcg/riscv64/smmpt-common.S              | 257 +++++++++++++
   tests/tcg/riscv64/test-smmpt.S                |  85 +++++
   tests/tcg/riscv64/test-smmpt34.S              |  97 +++++
   tests/tcg/riscv64/test-smmpt52.S              |  91 +++++
   tests/tcg/riscv64/test-smmpt64.S              |  98 +++++
   19 files changed, 1354 insertions(+), 19 deletions(-)

create mode 100644 target/riscv/riscv_smmpt.c create mode 100644 target/riscv/riscv_smmpt.h create mode 100644 tests/tcg/riscv64/smmpt-common.S create mode 100644 tests/tcg/riscv64/test-smmpt.S create mode 100644 tests/tcg/riscv64/test-smmpt34.S create mode 100644 tests/tcg/riscv64/test-smmpt52.S create mode 100644 tests/tcg/riscv64/test-smmpt64.S

base-commit: 499039798cdad7d86b787fec0eaf1da4151c0f05

#23 lore raw

Last week I have sent V7 of OpenSBI series which is based on V0.9 version of SMMTT(SPDA) spec.

https://patchwork.ozlabs.org/project/opensbi/list/?series=531089

Quoted 198 lines · LIU Zhiwei
Quoted 9 lines · Rahul Pathak

Hi Zhiwei,

The SMMPT spec has been updated following the ARC comments
and the big change is the merging of both SMSDID and SMMPT sub extensions into one with new name "SMSD" which is much better now.

https://github.com/riscv/riscv-smmtt/commit/ff5069fa99fc1fdd97c9fb8d96329e13afa82712

Can you please update this series.

Hi Rahul and Daniel,

I see the latest specification is v0.9.0 now. Supporting v0.4.9 is not necessary. I will update this patch set to v0.9.0 together with updated test cases.

Thanks,
Zhiwei

Quoted 177 lines

Thanks
Rahul

Quoted 171 lines · LIU Zhiwei
Quoted 12 lines · Daniel Henrique Barboza

Hi Zhiwei,

Sorry from taking this long to take a look at this work. I am afraid I'll
need to ask you to send a v9 due to the following upstream changes, aside from some review comments I made:

- upstream changed how tcg tests are invoked (we use meson now), i.e. you need to add changes in tests/tcg/riscv64/system/meson.build instead of Makefile.softmmu-target;

Quoted 6 lines

- upstream also changed how we declare things in disas/riscv.c.

If you send a v9 I'll prioritize it for review. We can get this done for 11.2.

Hi Daniel,

Thank you for taking the time to review this! No worries about the delay.

I appreciate the heads-up on the upstream changes. I will update the patch accordingly:

Switch from Makefile.softmmu-target to tests/tcg/riscv64/system/meson.build for the TCG test invocation; Update the declarations in disas/riscv.c to align with the latest upstream changes.
I will send out v9 as soon as possible. Really glad to hear we can target 11.2 for this — I'll make sure to prioritize it on my end as well.

Thanks again for the review and for keeping this moving!

Best regards,
Zhiwei

Quoted 133 lines

Cheers,
Daniel

Quoted 129 lines · LIU Zhiwei

This patch set introduces support for the RISC-V Smsdid and Smmpt (Supervisor Domain Identifier and Memory Protection Table) extensions v0.4.9(https://github.com/riscv/riscv-smmtt/releases/tag/v0.4.9). 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

Rahul Pathak is working on SMMPT OpenSBI support -
https://lists.infradead.org/pipermail/opensbi/2026-July/010235.html

based on his QEMU SMMPT branch -
https://github.com/pathakraul/qemu/tree/rpathak_smmpt_v1

As we all agree -
https://patchew.org/QEMU/[email protected]/

, we will collaborate on this work and not duplicate efforts. Rahul will focus on OpenSBI upstream and I will update the QEMU patch set to v8.

Xiangyi will review the OpenSBI SMMPT patch set and may collaborate to upstream the other parts, such as SMSDIA and IOMPT.

v7->v8:
1. Update the mfence.pa and minval.pa instruction encodings to
match the
upstream riscv-opcodes definition
(https://github.com/riscv/riscv-opcodes/pull/389/): mfence.pa
now uses

         funct7=0b0011001 and minval.pa uses funct7=0b0011011. The

decoder,
disassembler and the system test are updated accordingly.

v6->v7:
The implementation is updated from the v0.3.4 specification to v0.4.9. The main changes are:
1. Update the mmpt CSR number to 0x382 and msdcfg to 0x74E.
2. Update the mmpt register layout: the SDID and PPN field
positions
changed and the RV64 PPN is now 44 bits.
3. Rework the MPTE format: the N (NAPOT) bit now lives at bit 2,
the
non-leaf NAPOT form is removed, NAPOT leaf entries encode a
single
XWR tuple plus a G granularity field, and non-NAPOT leaf entries hold per-page XWR tuples.
4. Treat XWR=000 as "no access" and a non-leaf entry with N=1 as a fault.
5. Enforce the Smmpt64 root-table PPN alignment (low 3 bits zero).
6. Rename the fence instructions mfence.spa/minval.spa to mfence.pa/minval.pa.
7. Drop stale Reviewed-by tags on the substantially reworked
patches.
8. Introduce the configurable "smmpt-sdidlen" CPU property.
9. Add disassembly support for the Smmpt.
10. Add bare-metal M-mode system tests.
11. Rebase to master.

v5->v6:
1. Use explicitly bit fields extract instead of mpte_union_t.
2. Use the same exception behavior for MPT valiation as PMP valiation.
3. Use PAGE_* instead of MPT_ACCESS_* as they have same value.
4. Use address_space_*_le instead of address_space_* for SMMPT.
5. Only print SMMPT address check log when SMMPT is enabled.
6. Add implied rule for SMMPT as it depends on SMSDID.
7. Rebase to master.

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 (11):
target/riscv: Add basic definitions and CSRs for SMMPT target/riscv: Add smmpt-sdidlen property for the mmpt SDID field 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: Add disassembly for Smmpt instructions and CSRs target/riscv: Enable SMMPT extension
target/riscv: Add system test for SMMPT extension target/riscv: Add system tests for Smmpt52 and Smmpt64 target/riscv: Add system test for Smmpt34

   disas/riscv.c                                 |   9 +-
   target/riscv/cpu.c                            |  60 +++-
   target/riscv/cpu.h                            |   8 +
   target/riscv/cpu_bits.h                       |  27 ++
   target/riscv/cpu_cfg_fields.h.inc             |   3 +
   target/riscv/insn32.decode                    |   2 +
   target/riscv/meson.build                      |   1 +
   target/riscv/riscv_smmpt.c                    | 339 ++++++++++++++++++
   target/riscv/riscv_smmpt.h                    |  26 ++
   target/riscv/tcg/cpu_helper.c                 | 119 +++++-
   target/riscv/tcg/csr.c                        | 106 ++++++
   .../tcg/insn_trans/trans_privileged.c.inc     |  30 ++
   target/riscv/tcg/pmp.h                        |   3 +
   tests/tcg/riscv64/Makefile.softmmu-target     |  12 +
   tests/tcg/riscv64/smmpt-common.S              | 257 +++++++++++++
   tests/tcg/riscv64/test-smmpt.S                |  85 +++++
   tests/tcg/riscv64/test-smmpt34.S              |  97 +++++
   tests/tcg/riscv64/test-smmpt52.S              |  91 +++++
   tests/tcg/riscv64/test-smmpt64.S              |  98 +++++
   19 files changed, 1354 insertions(+), 19 deletions(-)

create mode 100644 target/riscv/riscv_smmpt.c create mode 100644 target/riscv/riscv_smmpt.h create mode 100644 tests/tcg/riscv64/smmpt-common.S create mode 100644 tests/tcg/riscv64/test-smmpt.S create mode 100644 tests/tcg/riscv64/test-smmpt34.S create mode 100644 tests/tcg/riscv64/test-smmpt52.S create mode 100644 tests/tcg/riscv64/test-smmpt64.S

base-commit: 499039798cdad7d86b787fec0eaf1da4151c0f05