Skip to contentSkip to conversation

Implements RISC-V S-mode Physical Memory Protection (SPMP) and its extensions

RFC PATCH In review luisccc 2 participants 13 messages Mar 18 – Jun 12

Subsystems (2)
lore
Share & tools
lore

luisccc

author
#0 lore raw

cover Implements RISC-V S-mode Physical Memory Protection (SPMP) and its extensions

This patchset adds initial support for RISC-V S-mode Physical Memory Protection (SPMP) to QEMU.

Implemented version of the specification:
https://github.com/riscv/riscv-isa-manual/tree/d539cf2fe65b02a734bf97f210474f82bd5e9393

The implementation introduces the SPMP checks to the core, CSR support, delegation handling, and CPU properties to enable SPMP and corresponding extensions.

SPMP can be enabled via:
-cpu <model>,spmp=true
Note: smpmpdeleg, sscsrind, and smcsrind also enabled.

The sspmpen extension can be enabled via:
-cpu <model>,sspmpen=true
Note: SPMP and its depedencies also enabled.

luisccc (6):
target/riscv: Add SPMP infrastructure
target/riscv: Add SPMP CSR configuration
target/riscv: Implement extension for Sharing Hardware Resources
(Smpmpdeleg)
target/riscv: Implement extension for Optimizing Context Switching
(Sspmpen)
target/riscv: Enable SPMP support in CPU
target/riscv: Add CPU properties for SPMP and sspmpen

 target/riscv/cpu.c                |  88 +++++
 target/riscv/cpu.h                |   9 +
 target/riscv/cpu_bits.h           |   9 +
 target/riscv/cpu_cfg_fields.h.inc |   3 +
 target/riscv/cpu_helper.c         | 139 ++++++--
 target/riscv/csr.c                | 167 ++++++++++
 target/riscv/meson.build          |   1 +
 target/riscv/pmp.c                |  14 +-
 target/riscv/pmp.h                |   1 +
 target/riscv/spmp.c               | 516 ++++++++++++++++++++++++++++++
 target/riscv/spmp.h               |  83 +++++
 11 files changed, 1005 insertions(+), 25 deletions(-)
 create mode 100644 target/riscv/spmp.c
 create mode 100644 target/riscv/spmp.h
Signature · 1 line
2.43.0
author
#1 lore raw

RFC PATCH 1/6 target/riscv: Add SPMP infrastructure

Introduce the initial infrastructure required for the RISC-V S-mode Physical Memory Protection (SPMP) implementation.

This patch adds the SPMP source files, integrates them into the build system, and introduces the CPU state and configuration fields required by subsequent patches. No SPMP checks are enforced yet.

---
 target/riscv/cpu.h                |   6 +
 target/riscv/cpu_cfg_fields.h.inc |   1 +
 target/riscv/meson.build          |   1 +
 target/riscv/spmp.c               | 512 ++++++++++++++++++++++++++++++
 target/riscv/spmp.h               |  83 +++++
 5 files changed, 603 insertions(+)
 create mode 100644 target/riscv/spmp.c
 create mode 100644 target/riscv/spmp.h
target/riscv/cpu.h +6 -0
index 35d1f6362c..c265098324 100644
@@ -178,8 +178,11 @@ extern RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[];
 #define OLD_MAX_RISCV_PMPS (16)
 #define MIN_RISCV_PMP_GRANULARITY 4
 
+#define MAX_RISCV_SPMPS (64)
+
 #if !defined(CONFIG_USER_ONLY)
 #include "pmp.h"
+#include "spmp.h"
 #include "debug.h"
 #endif
 
@@ -442,6 +445,9 @@ struct CPUArchState {
     pmp_table_t pmp_state;
     target_ulong mseccfg;
 
+    /* S-mode Physical Memory Protection */
+    spmp_table_t spmp_state;
+
     /* trigger module */
     target_ulong trigger_cur;
     target_ulong tdata1[RV_MAX_TRIGGERS];
target/riscv/cpu_cfg_fields.h.inc +1 -0
index 70ec650abf..72d417c241 100644
@@ -156,6 +156,7 @@ BOOL_FIELD(ext_xmipslsp)
 
 BOOL_FIELD(mmu)
 BOOL_FIELD(pmp)
+BOOL_FIELD(spmp)
 BOOL_FIELD(debug)
 BOOL_FIELD(misa_w)
 
target/riscv/meson.build +1 -0
index 3842c7c1a8..d03f3a7826 100644
@@ -34,6 +34,7 @@ riscv_system_ss = ss.source_set()
 riscv_system_ss.add(files(
   'arch_dump.c',
   'pmp.c',
+  'spmp.c',
   'debug.c',
   'monitor.c',
   'machine.c',
target/riscv/spmp.c new file +512 -0

512 lines not on this page · raw message

target/riscv/spmp.h new file +83 -0

83 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Re: [RFC PATCH 1/6] target/riscv: Add SPMP infrastructure

Hi Luisccc,
Thank you for this patch. I have two main questions about the implementation:

The SPMP CSR write functions don't flush the TLB after modifying entries:
- spmpcfg_csr_write()
- spmpaddr_csr_write()
- sspmpen_csr_write()

PMP implements granularity handling for address matching (masking low bits in TOR mode, reinterpreting NA4 as NAPOT, etc.), but SPMP doesn't.
https://lists.nongnu.org/archive/html/qemu-riscv/2025-10/msg00416.html
<https://lists.nongnu.org/archive/html/qemu-riscv/2025-10/msg00416.html>

+/*

target/riscv/spmp.c line 38

Quoted 7 lines

+ * Accessor method to extract address matching type 'a field' from cfg reg
+ */

target/riscv/spmp.c new file +5 -0
+uint8_t spmp_get_a_field(uint8_t cfg)
+{
+    uint8_t a = cfg >> 3;
+    return a & 0x3;
+}

+

target/riscv/spmp.c line 50

Quoted 12 lines
target/riscv/spmp.c new file +11 -0
+/*
+ * Check whether mstatus.sum is set.
+ */
+static inline int sum_is_set(CPURISCVState *env)
+{
+    if (env->mstatus & MSTATUS_SUM) {
+        return 1;
+    }
+
+    return 0;
+}

These two helper functions could be simplified:
uint8_t spmp_get_a_field(uint8_t cfg)

{
    return (cfg & SPMP_AMATCH) >> 3;
}

static inline bool sum_is_set(CPURISCVState *env)

{
    return !!(env->mstatus & MSTATUS_SUM);
}

+static void spmp_update_rule_addr(CPURISCVState *env, uint32_t spmp_index)

Quoted 30 lines
target/riscv/spmp.c new file +30 -0
+{
+    uint8_t this_cfg = env->spmp_state.spmp[spmp_index].cfg_reg;
+    target_ulong this_addr = env->spmp_state.spmp[spmp_index].addr_reg;
+    target_ulong prev_addr = 0u;
+    target_ulong sa = 0u;
+    target_ulong ea = 0u;
+
+    if (spmp_index >= 1u) {
+        prev_addr = env->spmp_state.spmp[spmp_index - 1].addr_reg;
+    }
+
+    switch (spmp_get_a_field(this_cfg)) {
+    case SPMP_AMATCH_OFF:
+        sa = 0u;
+        ea = -1;
+        break;
+
+    case SPMP_AMATCH_TOR:
+        sa = prev_addr << 2; /* shift up from [xx:0] to [xx+2:2] */
+        ea = (this_addr << 2) - 1u;
+        break;
+
+    case SPMP_AMATCH_NA4:
+        sa = this_addr << 2; /* shift up from [xx:0] to [xx+2:2] */
+        ea = (sa + 4u) - 1u;
+        break;
+
+    case SPMP_AMATCH_NAPOT:
+        spmp_decode_napot(this_addr, &sa, &ea);
+        break;

target/riscv/spmp.c line 109

Quoted 10 lines
target/riscv/spmp.c new file +10 -0
+
+    default:
+        sa = 0u;
+        ea = 0u;
+        break;
+    }
+
+    env->spmp_state.addr[spmp_index].sa = sa;
+    env->spmp_state.addr[spmp_index].ea = ea;
+}

The current SPMP implementation doesn't handle granularity configuration like PMP does.

int g = spmp_get_granularity_g(env);
case SPMP_AMATCH_TOR:

    /* Bits spmpaddr[G-1:0] do not affect the TOR address-matching logic. */
    if (g >= 1) {
        prev_addr &= ~((1ULL << g) - 1ULL);
        this_addr &= ~((1ULL << g) - 1ULL);
    }
    if (prev_addr >= this_addr) {
        sa = ea = 0u;
        break;
    }
case SPMP_AMATCH_NAPOT:
    /* Align to pmp_granularity */
    if (g >= 2) {
        this_addr |= ((1ul << (g - 1ul)) - 1ul);
    }
    spmp_decode_napot(this_addr, &sa, &ea);
Quoted 5 lines
target/riscv/spmp.c new file +4 -0
+
+static void spmp_update_rule_nums(CPURISCVState *env)
+{
+    int i;

target/riscv/spmp.c line 123

Quoted 10 lines
target/riscv/spmp.c new file +10 -0
+
+    env->spmp_state.num_active_rules = 0;
+    for (i = 0; i < MAX_RISCV_SPMPS; i++) {
+        const uint8_t a_field =
+            spmp_get_a_field(env->spmp_state.spmp[i].cfg_reg);
+        if (SPMP_AMATCH_OFF != a_field) {
+            env->spmp_state.num_active_rules++;
+        }
+    }
+}

Optimize spmp_update_rule_nums() to O(1)
Currently this function scans all 64 entries on every config write. We can optimize it to O(1) by incrementally updating the counter:

static void spmp_update_rule_nums(CPURISCVState *env, uint32_t spmp_index,
uint8_t old_a_field)

{
    uint8_t new_a_field =
        spmp_get_a_field(env->spmp_state.spmp[spmp_index].cfg_reg);

    if (old_a_field == SPMP_AMATCH_OFF && new_a_field != SPMP_AMATCH_OFF) {
        env->spmp_state.num_active_rules++;
    } else if (old_a_field != SPMP_AMATCH_OFF
               && new_a_field == SPMP_AMATCH_OFF) {
        env->spmp_state.num_active_rules--;
    }
}

target/riscv/spmp.c line 147

Quoted 13 lines
target/riscv/spmp.c new file +12 -0
+
+static uint8_t spmp_is_in_range(CPURISCVState *env, int spmp_index,
+                                target_ulong addr)
+{
+    if ((addr >= env->spmp_state.addr[spmp_index].sa)
+        && (addr <= env->spmp_state.addr[spmp_index].ea)) {
+        return 1;
+    }
+
+    return 0;
+}
+

It would be clearer to use bool instead of uint8_t for the return type. return (addr >= env->spmp_state.addr[spmp_index].sa) &&

           (addr <= env->spmp_state.addr[spmp_index].ea);
Quoted 1 line

+static bool spmp_hart_has_privs_default(CPURISCVState *env, target_ulong

addr,

target/riscv/spmp.c line 163

Quoted 7 lines
target/riscv/spmp.c new file +7 -0
+                                        spmp_priv_t *allowed_privs,
+                                        target_ulong mode)
+{
+    bool ret;
+    mode = env->virt_enabled ? PRV_U : mode;
+
+    if ((!riscv_cpu_cfg(env)->spmp) || !(mode == PRV_U)) {

Can we remove mode = env->virt_enabled ? PRV_U : mode; here ?

+/*

Quoted 26 lines

+ * Check if the address has required RWX privs to complete desired operation
+ */

target/riscv/spmp.c new file +23 -0
+bool spmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
+    target_ulong size, spmp_priv_t privs, spmp_priv_t *allowed_privs,
+    target_ulong mode)
+{
+    int i = 0;
+    int ret = -1;
+    int spmp_size = 0;
+    target_ulong s = 0;
+    target_ulong e = 0;
+    bool spmpen_en = false;
+
+    /* If it is either VS or VU mode, we treat it as U mode */
+    mode = env->virt_enabled ? PRV_U : mode;
+
+    /* Short cut for M-mode access */
+    if (mode == PRV_M) {
+        *allowed_privs = SPMP_READ | SPMP_WRITE | SPMP_EXEC;
+        return true;
+    }
+
+    /* Short cut if no rules */
+    if (env->spmp_state.num_active_rules == 0) {
+        return spmp_hart_has_privs_default(env, addr, allowed_privs,
Quoted 3 lines

mode);
+ }

We need to check this, num_active_rules == 0 doesn't mean no implemented entries (OFF).
SEPC : If the effective privilege mode of the access is S/U and no SPMP entry matches, but at least one
SPMP entry is implemented, the access is denied.

Quoted 27 lines
target/riscv/spmp.c new file +25 -0
+
+    if (size == 0) {
+        if (riscv_cpu_cfg(env)->mmu) {
+            /*
+             * If size is unknown (0), assume that all bytes
+             * from addr to the end of the page will be accessed.
+             */
+            spmp_size = -(addr | TARGET_PAGE_MASK);
+        } else {
+            spmp_size = sizeof(target_ulong);
+        }
+    } else {
+        spmp_size = size;
+    }
+
+    /* It depends on mpmpdeleg */
+    for (i = 0; i < env->spmp_state.num_deleg_rules; i++) {
+        s = spmp_is_in_range(env, i, addr);
+        e = spmp_is_in_range(env, i, addr + spmp_size - 1);
+        spmpen_en = spmp_get_spmpen_bit(env, i);
+
+        /* partially inside */
+        if ((s + e) == 1) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: spmp violation - access is partially

inside\n",

target/riscv/spmp.c line 247

Quoted 4 lines
target/riscv/spmp.c new file +4 -0
+                          __func__);
+            ret = 0;
+            break;
+        }

return false directly ?

Quoted 68 lines
target/riscv/spmp.c new file +11 -0
+
+        /* fully inside */
+        const uint8_t a_field =
+            spmp_get_a_field(env->spmp_state.spmp[i].cfg_reg);
+
+        /*
+         * Convert the SPMP permissions to match the truth table in the
+         * SPMP spec.
+         */
+        const uint8_t spmp_operation =
+                                (env->spmp_state.spmp[i].cfg_reg &

SPMP_EXEC)
+ | (env->spmp_state.spmp[i].cfg_reg &
SPMP_WRITE)
+ | (env->spmp_state.spmp[i].cfg_reg &
SPMP_READ);

target/riscv/spmp.c new file +4 -0
+
+        if (((s + e) == 2) && (SPMP_AMATCH_OFF != a_field) && spmpen_en) {
+            /*
+             * If the SPMP entry is not off, spmpen bit is set, and the

address

target/riscv/spmp.c new file +14 -0
+             * is in range, do the priv check
+             */
+
+            /* Shared not set */
+            if (!(env->spmp_state.spmp[i].cfg_reg & SPMP_SHARED)) {
+                /*
+                 *   Deny if:
+                 *   S mode access, with SUM not set, and UMODE set.
+                 *   U mode access, with UMODE not set.
+                 */
+                if ((mode == PRV_S && !sum_is_set(env) &&
+                    (env->spmp_state.spmp[i].cfg_reg & SPMP_UMODE)) ||
+                    (mode == PRV_U &&
+                        !(env->spmp_state.spmp[i].cfg_reg & SPMP_UMODE)))

{

target/riscv/spmp.c new file +6 -0
+                    *allowed_privs = 0;
+                }
+                /*
+                 *   EnforceNoX if:
+                 *   S mode access, with SUM set, and UMODE set.
+                 *   Note: The specification has the table in RWX, the

oposite

target/riscv/spmp.c new file +24 -0
+                 *   of the order in the cfg reg.
+                 */
+                else if (mode == PRV_S && sum_is_set(env) &&
+                        (env->spmp_state.spmp[i].cfg_reg & SPMP_UMODE)) {
+                    switch (spmp_operation) {
+                    case 0:
+                    case 2:
+                    case 4:
+                    case 6:
+                        *allowed_privs = 0;
+                        break;
+                    case 1:
+                    case 5:
+                        *allowed_privs = SPMP_READ;
+                        break;
+                    case 3:
+                    case 7:
+                        *allowed_privs = SPMP_READ | SPMP_WRITE;
+                        break;
+                    default:
+                        g_assert_not_reached();
+                    }
+                } else {
+                    /* Check for reserved configs */

target/riscv/spmp.c line 318

Quoted 10 lines
target/riscv/spmp.c new file +10 -0
+                    if (spmp_operation == 2 || spmp_operation == 6) {
+                        *allowed_privs = 0;
+                    } else {
+                        /* U mode falls here - Enforce */
+                        *allowed_privs = spmp_operation & 0x7;
+                    }
+                }
+            }
+            /* Set Shared bit */
+            else {

Lack of spmpcfg.U == 0.

if (!(env->spmp_state.spmp[i].cfg_reg & SPMP_UMODE)) {
                    *allowed_privs = 0;
                }
                else if (mode == PRV_S) {

+/*

Quoted 28 lines

+ * Accessor to set the cfg reg for a specific SPMP/HART
+ * Bounds checks.
+ */

target/riscv/spmp.c new file +7 -0
+void spmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index,
+    target_ulong val, bool m_mode_access)
+{
+    bool locked = m_mode_access ? false : is_entry_locked(env, reg_index);
+
+    /* If within bounds and not locked */
+    if (reg_index < env->spmp_state.num_deleg_rules && !locked) {

No blank

(quoted diff) +2 -0
+        env->spmp_state.spmp[reg_index].cfg_reg = val;
+        /* Storing this allows for faster switching with the sspmpen ext

*/

(quoted diff) +2 -0
+        env->spmp_state.locked_rules |=
+                                ((val & SPMP_LOCK) >> 7 & 0x1) <<

reg_index;

target/riscv/spmp.c new file +6 -0
+
+        spmp_update_rule(env, reg_index);
+    } else {
+        if (locked) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                    "%s: ignoring spmpcfg write - locked entry\n",

__func__);

target/riscv/spmp.c new file +3 -0
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                    "%s: ignoring spmpcfg write - out of bounds\n",

__func__);

target/riscv/spmp.c line 50

Quoted 3 lines
target/riscv/spmp.c new file +3 -0
+        }
+    }
+}

1. SPMP configuration changes require TLB flush (currently missing) When SPMP entries are modified, cached TLB entries may still contain old permission information
2. NA4 mode should be reinterpreted as NAPOT when granularity > 4 bytes

     if (reg_index < env->spmp_state.num_deleg_rules && !locked) {
        if (env->spmp_state.spmp[reg_index].cfg_reg != new_val) {
            uint8_t old_a_field =
                spmp_get_a_field(env->spmp_state.spmp[reg_index].cfg_reg);
            uint8_t a_field = spmp_get_a_field(new_val);
            /*
             * When granularity g >= 1 (i.e., granularity > 4 bytes),
             * the NA4 (Naturally Aligned 4-byte) mode is not selectable.
             * In this case, an NA4 setting is reinterpreted as a NAPOT
mode.
             */
            if ((riscv_cpu_cfg(env)->pmp_granularity >
                MIN_RISCV_PMP_GRANULARITY) && (a_field == SPMP_AMATCH_NA4))
{
                    new_val |= SPMP_AMATCH;
            }
            env->spmp_state.spmp[reg_index].cfg_reg = new_val;
            spmp_update_rule(env, reg_index, old_a_field);
            tlb_flush(env_cpu(env));
Quoted 2 lines

+/*

Quoted 22 lines

+ * Handle a write to a spmpaddr CSR
+ */

target/riscv/spmp.c new file +4 -0
+void spmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
+    target_ulong val, bool m_mode_access)
+{
+    bool locked = m_mode_access ? false : is_entry_locked(env,

addr_index);

target/riscv/spmp.c new file +10 -0
+
+    /* If within bounds and not locked */
+    if (addr_index < env->spmp_state.num_deleg_rules && !locked) {
+
+        env->spmp_state.spmp[addr_index].addr_reg = val;
+        spmp_update_rule(env, addr_index);
+    } else {
+        if (locked) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                    "%s: ignoring spmpaddr write - locked entry\n",

__func__);

target/riscv/spmp.c new file +3 -0
+        } else {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                    "%s: ignoring spmpaddr write - out of bounds\n",

__func__);

target/riscv/spmp.c line 50

Quoted 3 lines
target/riscv/spmp.c new file +3 -0
+        }
+    }
+}

We need to handle flush here

        if (env->spmp_state.spmp[addr_index].addr_reg != new_val) {
            uint8_t old_a_field =
                spmp_get_a_field(env->spmp_state.spmp[addr_index].cfg_reg);
            env->spmp_state.spmp[addr_index].addr_reg = new_val;
            spmp_update_rule(env, addr_index, old_a_field);
            tlb_flush(env_cpu(env));
        }

target/riscv/spmp.c line 459

Quoted 9 lines
target/riscv/spmp.c new file +9 -0
+/*
+ * Handle a read from a spmpaddr CSR
+ */
+target_ulong spmpaddr_csr_read(CPURISCVState *env, uint32_t addr_index)
+{
+    target_ulong val = 0;
+
+    if (addr_index < env->spmp_state.num_deleg_rules) {
+        val = env->spmp_state.spmp[addr_index].addr_reg;

Handle granularity

        int g = spmp_get_granularity_g(env);
        switch (spmp_get_a_field(env->spmp_state.spmp[addr_index].cfg_reg))
{
        case SPMP_AMATCH_OFF:
            /* fallthrough */
        case SPMP_AMATCH_TOR:
            /* Bit [g-1:0] read all zero */
            if (g >= 1 && g < TARGET_LONG_BITS) {
                val = deposit64(val, 0, g, 0);
            }
            break;
        case SPMP_AMATCH_NAPOT:
            /* Bit [g-2:0] read all one */
            if (g >= 2 && g < TARGET_LONG_BITS) {
                val = deposit64(val, 0, g - 1, -1ULL);
            }
            break;
        default:
            break;
        }
Quoted 14 lines
target/riscv/spmp.c new file +7 -0
+/*
+ * Handle a write to the sspmpen CSR
+ */
+void sspmpen_csr_write(CPURISCVState *env, uint64_t new_val)
+{
+    uint64_t mask = (env->spmp_state.num_deleg_rules == MAX_RISCV_SPMPS) ?
+                    ~0ULL : ((1ULL << env->spmp_state.num_deleg_rules) -

1);

target/riscv/spmp.c new file +4 -0
+
+    /* If the rule is locked, the bit cannot be changed */
+    env->spmp_state.spmpen =
+                    (env->spmp_state.spmpen &

env->spmp_state.locked_rules) |

target/riscv/spmp.c line 481

Quoted 3 lines
target/riscv/spmp.c new file +3 -0
+                    (new_val & ~env->spmp_state.locked_rules);
+    env->spmp_state.spmpen &= mask;
+}

flush()

Quoted 11 lines
target/riscv/spmp.c new file +10 -0
+
+/*
+ * Convert SPMP privilege to TLB page privilege.
+ */
+int spmp_priv_to_page_prot(spmp_priv_t spmp_priv)
+{
+    int prot = 0;
+
+    if (spmp_priv & SPMP_READ) {
+        prot |= PAGE_READ;

target/riscv/spmp.c line 501

Quoted 10 lines
target/riscv/spmp.c new file +10 -0
+    }
+    if (spmp_priv & SPMP_WRITE) {
+        prot |= PAGE_WRITE;
+    }
+    if (spmp_priv & SPMP_EXEC) {
+        prot |= PAGE_EXEC;
+    }
+
+    return prot;
+}

int spmp_priv_to_page_prot(spmp_priv_t spmp_priv)

{
    return spmp_priv & PAGE_RWX;
}

target/riscv/spmp.c line 508

Quoted 8 lines
target/riscv/spmp.c new file +7 -0
+
+void spmp_unlock_entries(CPURISCVState *env)
+{
+    /* Reset everything */
+    for (int i = 0; i < MAX_RISCV_SPMPS; i++) {
+        env->spmp_state.spmp[i].cfg_reg &= ~(SPMP_LOCK | SPMP_AMATCH);
+    }

Maybe we can clear it all ?
void spmp_unlock_entries(CPURISCVState *env)

{
    /* Reset everything */
    memset(&env->spmp_state, 0, sizeof(env->spmp_state));
}
Quoted 7 lines
target/riscv/spmp.c new file +4 -0
+
+    env->spmp_state.locked_rules = 0;
+    env->spmp_state.num_active_rules = 0;
+}
author
#3 lore raw

RFC PATCH 2/6 target/riscv: Add SPMP CSR configuration

In reply to luisccc

Introduce the CSR definitions and handlers to configure S-mode Physical Memory Protection (SPMP).
This patch adds the SPMP CSR bit definitions. The implementation allows software to configure SPMP state but does not yet enforce memory access checks.

---
 target/riscv/cpu_bits.h |  3 ++
 target/riscv/csr.c      | 73 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+)
target/riscv/cpu_bits.h +3 -0
index b62dd82fe7..9b7ab28bba 100644
@@ -1049,6 +1049,9 @@ typedef enum CTRType {
 #define ISELECT_CD_LAST                    0x5f
 #define ISELECT_MASK_SXCSRIND              0xfff
 
+/* SPMP Values for Indirect CSR Access Extension */
+#define ISELECT_SPMP_BASE                  0x100
+
 /* Dummy [M|S|VS]ISELECT value for emulating [M|S|VS]TOPEI CSRs */
 #define ISELECT_IMSIC_TOPEI                (ISELECT_MASK_AIA + 1)
 
target/riscv/csr.c +73 -0

103 lines not on this page · raw message

Signature · 1 line
2.43.0
#4 lore raw

Re: [RFC PATCH 2/6] target/riscv: Add SPMP CSR configuration

target/riscv/csr.c static bool xiselect_ctr_range(int csrno, target_ulong isel) · line 2471

Quoted 7 lines · luisccc
target/riscv/csr.c +5 -0
+static bool xiselect_spmp_range(target_ulong isel)
+{
+    return (ISELECT_SPMP_BASE <= isel &&
+            isel <= ISELECT_SPMP_BASE + MAX_RISCV_SPMPS);
+}

0 <= isel <= 64 indicates that there are 65 valid entries in total.

target/riscv/csr.c static RISCVException write_pmpaddr(CPURISCVState *env, int csrno, · line 5331

Quoted 11 lines
target/riscv/csr.c +10 -0
+static int rmw_xireg_spmp(CPURISCVState *env, int csrno,
+                        target_ulong isel, target_ulong *val,
+                        target_ulong new_val, target_ulong wr_mask)
+{
+    int index = 0;
+    bool m_mode_access = false;
+
+    /* Read 0 and write ignore if no rules are delegated */
+    if (env->spmp_state.num_deleg_rules == 0) {
+        *val = 0;

Maybe we should avoid dereferencing NULL
if (val) {
*val = 0;
}

Quoted 14 lines
target/riscv/csr.c +13 -0
+        return 0;
+    }
+
+    index = isel - ISELECT_SPMP_BASE;
+
+    switch (csrno) {
+    case CSR_MIREG:
+        /* If M mode, signal it */
+        m_mode_access = true;
+        [[fallthrough]];
+    case CSR_SIREG:
+        if (val) {
+            *val = spmpaddr_csr_read(env, index);

target/riscv/csr.c static RISCVException write_pmpaddr(CPURISCVState *env, int csrno, · line 5354

Quoted 10 lines
target/riscv/csr.c +10 -0
+        }
+
+        spmpaddr_csr_write(env, index, new_val & wr_mask, m_mode_access);
+        break;
+    case CSR_MIREG2:
+        /* If M mode, signal it */
+        m_mode_access = true;
+        [[fallthrough]];
+    case CSR_SIREG2:
+        index = isel - ISELECT_SPMP_BASE;

Duplicate setting index ?

Spec says : The mireg3 through mireg6 are reserved. Maybe

    /* Reserved as WPRI - read returns 0, write ignored */
    case CSR_SIREG3:
        /* fallthrough */
    case CSR_SIREG4 ... CSR_SIREG6:
        /* fallthrough */
    case CSR_MIREG3:
        /* fallthrough */
    case CSR_MIREG4 ... CSR_MIREG6:
        if (val) {
            *val = 0;
        }
        break;
Quoted 4 lines

2.43.0

author
#5 lore raw

RFC PATCH 3/6 target/riscv: Implement extension for Sharing Hardware Resources (Smpmpdeleg)

In reply to luisccc
---
 target/riscv/cpu.c                |  8 ++++++++
 target/riscv/cpu.h                |  2 ++
 target/riscv/cpu_bits.h           |  2 ++
 target/riscv/cpu_cfg_fields.h.inc |  1 +
 target/riscv/csr.c                | 27 +++++++++++++++++++++++++++
 target/riscv/pmp.c                | 14 +++++++++++++-
 target/riscv/pmp.h                |  1 +
 7 files changed, 54 insertions(+), 1 deletion(-)
target/riscv/cpu.c +8 -0
index e56470a374..5383e07dbd 100644
@@ -760,6 +760,14 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type)
     }
 
     pmp_unlock_entries(env);
+
+    /* Is SPMP enabled? */
+    if (riscv_cpu_cfg(env)->spmp) {
+        env->mpmpdeleg = MPMP_DELEG_DEFAULT;
+        env->spmp_state.num_deleg_rules = 64 - MPMP_DELEG_DEFAULT;
+
+        spmp_unlock_entries(env);
+    }
 #else
     env->priv = PRV_U;
     env->senvcfg = 0;
target/riscv/cpu.h +2 -0
index c265098324..e19c1216ea 100644
@@ -179,6 +179,7 @@ extern RISCVCPUImpliedExtsRule *riscv_multi_ext_implied_rules[];
 #define MIN_RISCV_PMP_GRANULARITY 4
 
 #define MAX_RISCV_SPMPS (64)
+#define MPMP_DELEG_DEFAULT (64)
 
 #if !defined(CONFIG_USER_ONLY)
 #include "pmp.h"
@@ -447,6 +448,7 @@ struct CPUArchState {
 
     /* S-mode Physical Memory Protection */
     spmp_table_t spmp_state;
+    uint16_t     mpmpdeleg;
 
     /* trigger module */
     target_ulong trigger_cur;
target/riscv/cpu_bits.h +2 -0
index 9b7ab28bba..36f7c1e873 100644
@@ -449,6 +449,8 @@
 #define CSR_PMPADDR62       0x3ee
 #define CSR_PMPADDR63       0x3ef
 
+#define CSR_MPMPDELEG       0x316
+
 /* RNMI */
 #define CSR_MNSCRATCH       0x740
 #define CSR_MNEPC           0x741
target/riscv/cpu_cfg_fields.h.inc +1 -0
index 72d417c241..772cc176d0 100644
@@ -119,6 +119,7 @@ BOOL_FIELD(rvv_ta_all_1s)
 BOOL_FIELD(rvv_ma_all_1s)
 BOOL_FIELD(rvv_vl_half_avl)
 BOOL_FIELD(rvv_vsetvl_x0_vill)
+BOOL_FIELD(ext_smpmpdeleg)
 /* Named features  */
 BOOL_FIELD(ext_svade)
 BOOL_FIELD(ext_zic64b)
target/riscv/csr.c +27 -0
index 167d84d92a..4b9fa11540 100644
@@ -5365,6 +5365,30 @@ static int rmw_xireg_spmp(CPURISCVState *env, int csrno,
     return 0;
 }
 
+/* S-mode Physical Memory Protection */
+static RISCVException rmw_mpmpdeleg(CPURISCVState *env, int csrno,
+                                    target_ulong *ret_val,
+                                    target_ulong new_val, target_ulong wr_mask)
+{
+    uint16_t new_mpmpdeleg = (env->mpmpdeleg & ~wr_mask) | (new_val & wr_mask);
+
+    if (ret_val) {
+        *ret_val = env->mpmpdeleg;
+    }
+
+    /*
+     * pmpnum is locked if MSECCFG_MML is set
+     * and if new_mpmpdeleg is higher than last locked rule
+     */
+    if (!(env->mseccfg & MSECCFG_MML) &&
+        (new_mpmpdeleg & 0x7F) > env->pmp_state.last_locked_rule) {
+        env->mpmpdeleg = new_mpmpdeleg & 0x7F;
+    }
+
+    env->spmp_state.num_deleg_rules = MPMP_DELEG_DEFAULT - env->mpmpdeleg;
+    return RISCV_EXCP_NONE;
+}
+
 static RISCVException read_tselect(CPURISCVState *env, int csrno,
                                    target_ulong *val)
 {
@@ -6362,6 +6386,9 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
     [CSR_PMPADDR63]  = { "pmpaddr63", pmp, read_pmpaddr, write_pmpaddr,
                          .min_priv_ver = PRIV_VERSION_1_12_0           },
 
+    /* S-mode Physical Memory Protection */
+    [CSR_MPMPDELEG]   = { "mpmpdeleg", spmp, NULL, NULL, rmw_mpmpdeleg },
+
     /* Debug CSRs */
     [CSR_TSELECT]   =  { "tselect",  debug, read_tselect,  write_tselect  },
     [CSR_TDATA1]    =  { "tdata1",   debug, read_tdata,    write_tdata    },
target/riscv/pmp.c +13 -1
index 3ef62d26ad..22d6f1f5b3 100644
@@ -174,6 +174,12 @@ static bool pmp_write_cfg(CPURISCVState *env, uint32_t pmp_index, uint8_t val)
                     return false;
             }
             env->pmp_state.pmp[pmp_index].cfg_reg = val;
+
+            if ((val & PMP_LOCK) &&
+                env->pmp_state.last_locked_rule < (signed int)pmp_index) {
+                env->pmp_state.last_locked_rule = pmp_index;
+            }
+
             pmp_update_rule_addr(env, pmp_index);
             return true;
         }
@@ -193,6 +199,8 @@ void pmp_unlock_entries(CPURISCVState *env)
     for (i = 0; i < pmp_num; i++) {
         env->pmp_state.pmp[i].cfg_reg &= ~(PMP_LOCK | PMP_AMATCH);
     }
+
+    env->pmp_state.last_locked_rule = -1;
 }
 
 static void pmp_decode_napot(hwaddr a, hwaddr *sa, hwaddr *ea)
@@ -385,11 +393,15 @@ bool pmp_hart_has_privs(CPURISCVState *env, hwaddr addr,
         pmp_size = size;
     }
 
+    /* If SPMP is enabled, use the MPMP delegation */
+    uint8_t max_pmp_index =
+        (riscv_cpu_cfg(env)->spmp) ? (env->mpmpdeleg & 0x7F) : pmp_regions;
+
     /*
      * 1.10 draft priv spec states there is an implicit order
      * from low to high
      */
-    for (i = 0; i < pmp_regions; i++) {
+    for (i = 0; i < max_pmp_index; i++) {
         s = pmp_is_in_range(env, i, addr);
         e = pmp_is_in_range(env, i, addr + pmp_size - 1);
 
target/riscv/pmp.h +1 -0
index 271cf24169..3194c6b531 100644
@@ -63,6 +63,7 @@ typedef struct {
     pmp_entry_t pmp[MAX_RISCV_PMPS];
     pmp_addr_t  addr[MAX_RISCV_PMPS];
     uint32_t num_rules;
+    signed char last_locked_rule;
 } pmp_table_t;
 
 void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index,
Signature · 1 line
2.43.0
#6 lore raw

Re: [RFC PATCH 3/6] target/riscv: Implement extension for Sharing Hardware Resources (Smpmpdeleg)

target/riscv/cpu.c static void riscv_cpu_reset_hold(Object *obj, ResetType type) · line 767

Quoted 8 lines
     pmp_unlock_entries(env);
target/riscv/cpu.c +5 -0
+
+    /* Is SPMP enabled? */
+    if (riscv_cpu_cfg(env)->spmp) {
+        env->mpmpdeleg = MPMP_DELEG_DEFAULT;
+        env->spmp_state.num_deleg_rules = 64 - MPMP_DELEG_DEFAULT;

Can we replace the const number with a macro?

+ spmp_unlock_entries(env);

Quoted 9 lines
target/riscv/cpu.c +1 -0
+    }
 #else
     env->priv = PRV_U;
     env->senvcfg = 0;
target/riscv/cpu.h +0 -0
index c265098324..e19c1216ea 100644

target/riscv/cpu.h extern RISCVCPUImpliedExtsRule · line 184

Quoted 6 lines
target/riscv/cpu.h +1 -0
@@ -179,6 +179,7 @@ extern RISCVCPUImpliedExtsRule
 *riscv_multi_ext_implied_rules[];
 #define MIN_RISCV_PMP_GRANULARITY 4
 
 #define MAX_RISCV_SPMPS (64)
+#define MPMP_DELEG_DEFAULT (64)
 

Why do we need MPMP_DELEG_DEFAULT?
Since SPMP and PMP share the same PMP entries, maybe we can directly use MAX_RISCV_PMPS instead?

+/* S-mode Physical Memory Protection */

Quoted 16 lines
target/riscv/cpu.h +3 -0
+static RISCVException rmw_mpmpdeleg(CPURISCVState *env, int csrno,
+                                    target_ulong *ret_val,
+                                    target_ulong new_val, target_ulong

wr_mask)

target/riscv/cpu.h +2 -0
+{
+    uint16_t new_mpmpdeleg = (env->mpmpdeleg & ~wr_mask) | (new_val &

wr_mask);

target/riscv/cpu.h +8 -0
+
+    if (ret_val) {
+        *ret_val = env->mpmpdeleg;
+    }
+
+    /*
+     * pmpnum is locked if MSECCFG_MML is set
+     * and if new_mpmpdeleg is higher than last locked rule

target/riscv/cpu.h

Quoted 10 lines
target/riscv/cpu.h +10 -0
+     */
+    if (!(env->mseccfg & MSECCFG_MML) &&
+        (new_mpmpdeleg & 0x7F) > env->pmp_state.last_locked_rule) {
+        env->mpmpdeleg = new_mpmpdeleg & 0x7F;
+    }
+
+    env->spmp_state.num_deleg_rules = MPMP_DELEG_DEFAULT - env->mpmpdeleg;
+    return RISCV_EXCP_NONE;
+}
+

Do we need to flush tlb here ?

author
#7 lore raw

RFC PATCH 4/6 target/riscv: Implement extension for Optimizing Context Switching (Sspmpen)

In reply to luisccc
---
 target/riscv/cpu_bits.h           |  4 ++
 target/riscv/cpu_cfg_fields.h.inc |  1 +
 target/riscv/csr.c                | 67 +++++++++++++++++++++++++++++++
 target/riscv/spmp.c               |  6 ++-
 4 files changed, 77 insertions(+), 1 deletion(-)
target/riscv/cpu_bits.h +4 -0
index 36f7c1e873..0a8cfedc5d 100644
@@ -255,6 +255,10 @@
 #define CSR_SCTRSTATUS      0x14f
 #define CSR_SCTRDEPTH       0x15f
 
+/* S-mode Physical Memory Protection (SPMP) */
+#define CSR_SPMPEN          0x183
+#define CSR_SPMPENH         0x193
+
 /* VS-Level Control transfer records CSRs */
 #define CSR_VSCTRCTL        0x24e
 
target/riscv/cpu_cfg_fields.h.inc +1 -0
index 772cc176d0..88edac21ca 100644
@@ -120,6 +120,7 @@ BOOL_FIELD(rvv_ma_all_1s)
 BOOL_FIELD(rvv_vl_half_avl)
 BOOL_FIELD(rvv_vsetvl_x0_vill)
 BOOL_FIELD(ext_smpmpdeleg)
+BOOL_FIELD(ext_sspmpen)
 /* Named features  */
 BOOL_FIELD(ext_svade)
 BOOL_FIELD(ext_zic64b)
target/riscv/csr.c +67 -0

85 lines not on this page · raw message

target/riscv/spmp.c +5 -1
index 85c5259b33..1615c207df 100644
@@ -147,7 +147,11 @@ static uint8_t spmp_is_in_range(CPURISCVState *env, int spmp_index,
 
 static bool spmp_get_spmpen_bit(CPURISCVState *env, int index)
 {
-    return true;
+    if (!riscv_cpu_cfg(env)->ext_sspmpen) {
+        return true;
+    }
+
+    return (env->spmp_state.spmpen >> index) & 0x1;
 }
 
 /*
Signature · 1 line
2.43.0
Reviewed-by on v1 4/6
#8 lore raw

Re: [RFC PATCH 4/6] target/riscv: Implement extension for Optimizing Context Switching (Sspmpen)

Thanks

Quoted 155 lines · luisccc
---
 target/riscv/cpu_bits.h           |  4 ++
 target/riscv/cpu_cfg_fields.h.inc |  1 +
 target/riscv/csr.c                | 67 +++++++++++++++++++++++++++++++
 target/riscv/spmp.c               |  6 ++-
 4 files changed, 77 insertions(+), 1 deletion(-)
target/riscv/cpu_bits.h +4 -0
index 36f7c1e873..0a8cfedc5d 100644
@@ -255,6 +255,10 @@
 #define CSR_SCTRSTATUS      0x14f
 #define CSR_SCTRDEPTH       0x15f
 
+/* S-mode Physical Memory Protection (SPMP) */
+#define CSR_SPMPEN          0x183
+#define CSR_SPMPENH         0x193
+
 /* VS-Level Control transfer records CSRs */
 #define CSR_VSCTRCTL        0x24e
 
target/riscv/cpu_cfg_fields.h.inc +1 -0
index 772cc176d0..88edac21ca 100644
@@ -120,6 +120,7 @@ BOOL_FIELD(rvv_ma_all_1s)
 BOOL_FIELD(rvv_vl_half_avl)
 BOOL_FIELD(rvv_vsetvl_x0_vill)
 BOOL_FIELD(ext_smpmpdeleg)
+BOOL_FIELD(ext_sspmpen)
 /* Named features  */
 BOOL_FIELD(ext_svade)
 BOOL_FIELD(ext_zic64b)
target/riscv/csr.c +10 -0
index 4b9fa11540..a11d7e6704 100644
@@ -769,6 +769,16 @@ static RISCVException spmp(CPURISCVState *env, int
 csrno)
     return smode(env, csrno);
 }
 
+static RISCVException sspmpen(CPURISCVState *env, int csrno)
+{
+    if (!riscv_cpu_cfg(env)->ext_sspmpen) {
+        return RISCV_EXCP_ILLEGAL_INST;
+    }
+
+    /* SSPMPEN can only exist, if spmp exists */
+    return spmp(env, csrno);
+}
+
 static RISCVException have_mseccfg(CPURISCVState *env, int csrno)
 {
     if (riscv_cpu_cfg(env)->ext_smepmp) {
target/riscv/csr.c +55 -0
@@ -5389,6 +5399,61 @@ static RISCVException rmw_mpmpdeleg(CPURISCVState
 *env, int csrno,
     return RISCV_EXCP_NONE;
 }
 
+static RISCVException rmw_spmpen64(CPURISCVState *env, int csrno,
+                                    uint64_t *ret_val,
+                                    uint64_t new_val, uint64_t wr_mask)
+{
+    uint64_t new_spmpen = (env->spmp_state.spmpen & ~wr_mask) |
+                            (new_val & wr_mask);
+
+    if (env->spmp_state.num_deleg_rules == 0) {
+        if (ret_val) {
+            *ret_val = 0;
+        }
+
+        return RISCV_EXCP_NONE;
+    }
+
+    if (ret_val) {
+        *ret_val = env->spmp_state.spmpen;
+    }
+
+    sspmpen_csr_write(env, new_spmpen);
+
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException rmw_spmpen(CPURISCVState *env, int csrno,
+                                  target_ulong *ret_val,
+                                  target_ulong new_val, target_ulong
 wr_mask)
+{
+    uint64_t rval = 0;
+    RISCVException ret;
+    ret = rmw_spmpen64(env, csrno, &rval, new_val, wr_mask);
+    if (ret_val) {
+        *ret_val = rval;
+    }
+
+    return ret;
+}
+
+static RISCVException rmw_spmpenh(CPURISCVState *env, int csrno,
+                                   target_ulong *ret_val,
+                                   target_ulong new_val,
+                                   target_ulong wr_mask)
+{
+    uint64_t rval = 0;
+    RISCVException ret;
+
+    ret = rmw_spmpen64(env, csrno, &rval,
+        ((uint64_t)new_val) << 32, ((uint64_t)wr_mask) << 32);
+    if (ret_val) {
+        *ret_val = rval >> 32;
+    }
+
+    return ret;
+}
+
 static RISCVException read_tselect(CPURISCVState *env, int csrno,

target_ulong *val)
{

target/riscv/csr.c +2 -0
@@ -6388,6 +6453,8 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
 
     /* S-mode Physical Memory Protection */
     [CSR_MPMPDELEG]   = { "mpmpdeleg", spmp, NULL, NULL, rmw_mpmpdeleg },
+    [CSR_SPMPEN]  = { "spmpen", sspmpen, NULL, NULL, rmw_spmpen },
+    [CSR_SPMPENH] = { "spmpenh", sspmpen, NULL, NULL, rmw_spmpenh },
 
     /* Debug CSRs */
     [CSR_TSELECT]   =  { "tselect",  debug, read_tselect,  write_tselect

},

target/riscv/spmp.c +5 -1
index 85c5259b33..1615c207df 100644
@@ -147,7 +147,11 @@ static uint8_t spmp_is_in_range(CPURISCVState *env,
 int spmp_index,
 
 static bool spmp_get_spmpen_bit(CPURISCVState *env, int index)
 {
-    return true;
+    if (!riscv_cpu_cfg(env)->ext_sspmpen) {
+        return true;
+    }
+
+    return (env->spmp_state.spmpen >> index) & 0x1;
 }
 

/*
--
2.43.0

author
#9 lore raw

RFC PATCH 5/6 target/riscv: Enable SPMP support in CPU

In reply to luisccc

Integrate the SPMP implementation into the RISC-V CPU model.Memory access checks are updated to take SPMP configuration into account when the extension is active.

---
 target/riscv/cpu.h        |   1 +
 target/riscv/cpu_helper.c | 139 +++++++++++++++++++++++++++++++-------
 2 files changed, 116 insertions(+), 24 deletions(-)
target/riscv/cpu.h +1 -0
index e19c1216ea..bf05ab66ce 100644
@@ -130,6 +130,7 @@ enum {
     TRANSLATE_SUCCESS,
     TRANSLATE_FAIL,
     TRANSLATE_PMP_FAIL,
+    TRANSLATE_SPMP_FAIL,
     TRANSLATE_G_STAGE_FAIL
 };
 
target/riscv/cpu_helper.c +115 -24

204 lines not on this page · raw message

Signature · 1 line
2.43.0
#10 lore raw

Re: [RFC PATCH 5/6] target/riscv: Enable SPMP support in CPU

Quoted 16 lines

static int get_physical_address_spmp(CPURISCVState *env, int *prot, hwaddr addr,

target/riscv/cpu_helper.c +11 -0
+                                    int size, int access_type,
+                                    int mode)
+{
+    spmp_priv_t spmp_priv;
+
+    if (!riscv_cpu_cfg(env)->spmp) {
+        *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
+        return TRANSLATE_SUCCESS;
+    }
+
+    if (!spmp_hart_has_privs(env, addr, size, 1 << access_type,

&spmp_priv,

target/riscv/cpu_helper.c +1 -0
+                            mode)) {

target/riscv/cpu_helper.c static int get_physical_address_pmp(CPURISCVState *env, int *prot, hwaddr addr, · line 1152

Quoted 10 lines
target/riscv/cpu_helper.c +10 -0
+        *prot = 0;
+        return TRANSLATE_SPMP_FAIL;
+    }
+
+    *prot = spmp_priv_to_page_prot(spmp_priv);
+
+    return TRANSLATE_SUCCESS;
+}
+
+

There is one extra blank

target/riscv/cpu_helper.c static void raise_mmu_exception(CPURISCVState *env, · line 1677

Quoted 9 lines
target/riscv/cpu_helper.c +2 -1
@@ -1634,7 +1671,8 @@ static void raise_mmu_exception(CPURISCVState *env,
 target_ulong address,
     case MMU_DATA_LOAD:
         if (pmp_violation) {
             cs->exception_index = RISCV_EXCP_LOAD_ACCESS_FAULT;
-        } else if (two_stage && !first_stage) {
+        } else if ((two_stage && !first_stage) ||
+                    (env->virt_enabled && spmp_violation)) {
 

Nit: align the parameters.

target/riscv/cpu_helper.c static void raise_mmu_exception(CPURISCVState *env, · line 1687

Quoted 12 lines
target/riscv/cpu_helper.c +0 -0
             cs->exception_index = RISCV_EXCP_LOAD_GUEST_ACCESS_FAULT;
         } else {
             cs->exception_index = RISCV_EXCP_LOAD_PAGE_FAULT;
target/riscv/cpu_helper.c +2 -1
@@ -1643,7 +1681,8 @@ static void raise_mmu_exception(CPURISCVState *env,
 target_ulong address,
     case MMU_DATA_STORE:
         if (pmp_violation) {
             cs->exception_index = RISCV_EXCP_STORE_AMO_ACCESS_FAULT;
-        } else if (two_stage && !first_stage) {
+        } else if ((two_stage && !first_stage) ||
+                    (env->virt_enabled && spmp_violation)) {
 

Nit: align the parameters.

author
#11 lore raw

RFC PATCH 6/6 target/riscv: Add CPU properties for SPMP and sspmpen

In reply to luisccc

Add CPU properties to control the SPMP and sspmpen extensions from the QEMU command line.

SPMP can be enabled with:
-cpu <model>,spmp=true
Note: This automatically enables smpmpdeleg, sscsrind, and smcsrind.

The sspmpen extension can be enabled with:
-cpu <model>,sspmpen=true
Note: This automatically enables SPMP and its depedencies.

---
 target/riscv/cpu.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 80 insertions(+)
target/riscv/cpu.c +80 -0
index 5383e07dbd..29bfce6581 100644
@@ -1656,6 +1656,84 @@ static const PropertyInfo prop_pmp_granularity = {
     .set = prop_pmp_granularity_set,
 };
 
+static void prop_spmp_set(Object *obj, Visitor *v, const char *name,
+                         void *opaque, Error **errp)
+{
+    RISCVCPU *cpu = RISCV_CPU(obj);
+    bool value;
+
+    visit_type_bool(v, name, &value, errp);
+
+    if (cpu->cfg.spmp != value && riscv_cpu_is_vendor(obj)) {
+        cpu_set_prop_err(cpu, name, errp);
+        return;
+    }
+
+    cpu_option_add_user_setting(name, value);
+    cpu->cfg.spmp = value;
+    cpu->cfg.ext_smpmpdeleg = value;
+
+    /* Enable necessary extensions */
+    if (value) {
+        cpu->cfg.ext_sscsrind = true;
+        cpu->cfg.ext_smcsrind = true;
+    }
+}
+
+static void prop_spmp_get(Object *obj, Visitor *v, const char *name,
+                         void *opaque, Error **errp)
+{
+    bool value = RISCV_CPU(obj)->cfg.spmp;
+
+    visit_type_bool(v, name, &value, errp);
+}
+
+static const PropertyInfo prop_spmp = {
+    .type = "bool",
+    .description = "spmp",
+    .get = prop_spmp_get,
+    .set = prop_spmp_set,
+};
+
+static void prop_sspmpen_set(Object *obj, Visitor *v, const char *name,
+                         void *opaque, Error **errp)
+{
+    RISCVCPU *cpu = RISCV_CPU(obj);
+    bool value;
+
+    visit_type_bool(v, name, &value, errp);
+
+    if (cpu->cfg.spmp != value && riscv_cpu_is_vendor(obj)) {
+        cpu_set_prop_err(cpu, name, errp);
+        return;
+    }
+
+    cpu_option_add_user_setting(name, value);
+    cpu->cfg.ext_sspmpen = value;
+
+    /* Enable necessary extensions */
+    if (value) {
+        cpu->cfg.spmp = true;
+        cpu->cfg.ext_sscsrind = true;
+        cpu->cfg.ext_smcsrind = true;
+    }
+}
+
+static void prop_sspmpen_get(Object *obj, Visitor *v, const char *name,
+                         void *opaque, Error **errp)
+{
+    bool value = RISCV_CPU(obj)->cfg.ext_sspmpen;
+
+    visit_type_bool(v, name, &value, errp);
+}
+
+static const PropertyInfo prop_ext_sspmpen = {
+    .type = "bool",
+    .description = "ext_sspmpen",
+    .get = prop_sspmpen_get,
+    .set = prop_sspmpen_set,
+};
+
 static int priv_spec_from_str(const char *priv_spec_str)
 {
     int priv_version = -1;
@@ -2657,6 +2735,8 @@ static const Property riscv_cpu_properties[] = {
     {.name = "pmp", .info = &prop_pmp},
     {.name = "num-pmp-regions", .info = &prop_num_pmp_regions},
     {.name = "pmp-granularity", .info = &prop_pmp_granularity},
+    {.name = "spmp", .info = &prop_spmp},
+    {.name = "sspmpen", .info = &prop_ext_sspmpen},
 
     {.name = "priv_spec", .info = &prop_priv_spec},
     {.name = "vext_spec", .info = &prop_vext_spec},
Signature · 1 line
2.43.0
#12 lore raw

Re: [RFC PATCH 6/6] target/riscv: Add CPU properties for SPMP and sspmpen

Quoted 14 lines
target/riscv/cpu.c +13 -0
+static void prop_sspmpen_set(Object *obj, Visitor *v, const char *name,
+                         void *opaque, Error **errp)
+{
+    RISCVCPU *cpu = RISCV_CPU(obj);
+    bool value;
+
+    visit_type_bool(v, name, &value, errp);
+
+    if (cpu->cfg.spmp != value && riscv_cpu_is_vendor(obj)) {
+        cpu_set_prop_err(cpu, name, errp);
+        return;
+    }
+

target/riscv/cpu.c static const PropertyInfo prop_pmp_granularity = { · line 1720

Quoted 10 lines
target/riscv/cpu.c +10 -0
+    cpu_option_add_user_setting(name, value);
+    cpu->cfg.ext_sspmpen = value;
+
+    /* Enable necessary extensions */
+    if (value) {
+        cpu->cfg.spmp = true;
+        cpu->cfg.ext_sscsrind = true;
+        cpu->cfg.ext_smcsrind = true;
+    }
+}

Why do we need to set it again ? So if we enable sspmpen means enable spmp
at the same time?