Skip to contentSkip to conversation

Privilege version update

RFC Superseded Atish Patra 8 participants 18 messages Jan 20, 2022 – Feb 3, 2022 RISC-V

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover Privilege version update

RISC-V International (RVI) has ratified many RISC-V ISA extensions recently[1]. The privileged specification version is also upgraded to v1.12. It means certain CSRs introduced in v1.12 should only be accessible only if the priv specification version supported is equal or greater than v1.12. Doing this check in predicate function is not scalable as there will be new CSRs introduced in the future versions of the privileged specification.

This series tries to address this problem by adding a field in the csr_ops which can be checked in csrrw function before invoking the predicate function. To keep the code churn to minimum, it is assumed that the minimum version of the privilege version supported for any CSR is v1.10 unless specified explicitly in the csr_ops table. Any new CSRs introduced in v1.12 have been updated accordingly.

This will work fine for any ratified extensions. However, it is bit unclear what should be done for the stable draft extensions. My suggestion is not to update the priv field in the CSR ops table until the extension is marked experimental (i.e. not frozen/ratified). Once the extension is ratified and graduated from experimental to available stage, the privileged spec version should be updated in the csr table if required. I am open to other suggestions as well.

[1] https://wiki.riscv.org/display/TECH/Recently+Ratified+Extensions

Atish Patra (5):
target/riscv: Add the privileged spec version 1.12.0 target/riscv: Introduce privilege version field in the CSR ops. target/riscv: Add support for mconfigptr target/riscv: Add *envcfg* CSRs support
target/riscv: Enable privileged spec version 1.12

target/riscv/cpu.c      |   8 +-
target/riscv/cpu.h      |  10 ++
target/riscv/cpu_bits.h |  32 +++++++
target/riscv/csr.c      | 199 +++++++++++++++++++++++++++++++++-------
target/riscv/machine.c  |  26 ++++++
5 files changed, 237 insertions(+), 38 deletions(-)

--
2.30.2

#1 lore raw

RFC 1/5 target/riscv: Add the privileged spec version 1.12.0

Add the definition for ratified privileged specification version v1.12

---
 target/riscv/cpu.h | 1 +
 1 file changed, 1 insertion(+)
target/riscv/cpu.h +1 -0
index 4d630867650a..671f65100b1a 100644
@@ -82,6 +82,7 @@ enum {
 
 #define PRIV_VERSION_1_10_0 0x00011000
 #define PRIV_VERSION_1_11_0 0x00011100
+#define PRIV_VERSION_1_12_0 0x00011200
 
 #define VEXT_VERSION_1_00_0 0x00010000
 
Signature · 1 line
2.30.2
#2 lore raw

Re: [RFC 1/5] target/riscv: Add the privileged spec version 1.12.0

Quoted 12 lines · Atish Patra

Add the definition for ratified privileged specification version v1.12

---
  target/riscv/cpu.h | 1 +
  1 file changed, 1 insertion(+)
target/riscv/cpu.h +0 -0
index 4d630867650a..671f65100b1a 100644

target/riscv/cpu.h enum { · line 85

Quoted 4 lines
target/riscv/cpu.h +1 -0
@@ -82,6 +82,7 @@ enum {
 
  #define PRIV_VERSION_1_10_0 0x00011000
  #define PRIV_VERSION_1_11_0 0x00011100
+#define PRIV_VERSION_1_12_0 0x00011200

Is there any good reason for defining things this way, as opposed to a simple enumeration? A simple enum would eliminate the need for

target/riscv/cpu.h enum {

Quoted 4 lines
target/riscv/cpu.h +4 -0
+    /* The default privilege specification version supported is 1.10 */
+    if (!csr_min_priv) {
+        csr_min_priv = PRIV_VERSION_1_10_0;
+    }

in patch 5.

r~

#3 lore raw

Re: [RFC 1/5] target/riscv: Add the privileged spec version 1.12.0

Quoted 14 lines · Richard Henderson
Quoted 13 lines · Atish Patra

Add the definition for ratified privileged specification version v1.12

---
  target/riscv/cpu.h | 1 +
  1 file changed, 1 insertion(+)
target/riscv/cpu.h +0 -0
index 4d630867650a..671f65100b1a 100644
@@ -82,6 +82,7 @@ enum {
 
Quoted 9 lines

target/riscv/cpu.h enum { · line 85

Quoted 3 lines
target/riscv/cpu.h +1 -0
  #define PRIV_VERSION_1_10_0 0x00011000
  #define PRIV_VERSION_1_11_0 0x00011100
+#define PRIV_VERSION_1_12_0 0x00011200

Is there any good reason for defining things this way, as opposed to a simple enumeration?
A simple enum would eliminate the need for

Agreed. A simple enum would be much nicer. I was just following the previous definition of
PRIV_VERSION_1_10_0 & PRIV_VERSION_1_11_0.

I am not sure about the reason behind this scheme.

@Alistair Francis <[email protected]> Is there any history behind this scheme ?
or Are you okay if I change it ?

Quoted 10 lines

target/riscv/cpu.h enum {

Quoted 4 lines
target/riscv/cpu.h +4 -0
+    /* The default privilege specification version supported is 1.10 */
+    if (!csr_min_priv) {
+        csr_min_priv = PRIV_VERSION_1_10_0;
+    }

in patch 5.

r~

#4 lore raw

Re: [RFC 1/5] target/riscv: Add the privileged spec version 1.12.0

Quoted 27 lines · Atish Kumar Patra
Quoted 15 lines · Richard Henderson
Quoted 13 lines · Atish Patra

Add the definition for ratified privileged specification version v1.12

---
  target/riscv/cpu.h | 1 +
  1 file changed, 1 insertion(+)
target/riscv/cpu.h +0 -0
index 4d630867650a..671f65100b1a 100644
@@ -82,6 +82,7 @@ enum {
 
Quoted 7 lines

target/riscv/cpu.h enum { · line 85

Quoted 3 lines
target/riscv/cpu.h +1 -0
  #define PRIV_VERSION_1_10_0 0x00011000
  #define PRIV_VERSION_1_11_0 0x00011100
+#define PRIV_VERSION_1_12_0 0x00011200

Is there any good reason for defining things this way, as opposed to a simple enumeration? A simple enum would eliminate the need for

Quoted 6 lines

Agreed. A simple enum would be much nicer. I was just following the previous definition of PRIV_VERSION_1_10_0 & PRIV_VERSION_1_11_0.

I am not sure about the reason behind this scheme.

@Alistair Francis Is there any history behind this scheme ?

I don't think so

Quoted 1 line

or Are you okay if I change it ?

Yep :)

Alistair

#5 lore raw

RFC 2/5 target/riscv: Introduce privilege version field in the CSR ops.

In reply to Atish Patra

To allow/disallow the CSR access based on the privilege spec, a new field in the csr_ops is introduced. It also adds the privileged specification version (v1.12) for the CSRs introduced in the v1.12. This includes the new ratified extensions such as Vector, Hypervisor and secconfig CSR.

---
 target/riscv/cpu.h |   1 +
 target/riscv/csr.c | 103 ++++++++++++++++++++++++++++++---------------
 2 files changed, 69 insertions(+), 35 deletions(-)
target/riscv/cpu.h +1 -0
index 671f65100b1a..7f87917204c5 100644
@@ -521,6 +521,7 @@ typedef struct {
     riscv_csr_op_fn op;
     riscv_csr_read128_fn read128;
     riscv_csr_write128_fn write128;
+    uint32_t min_priv_ver;
 } riscv_csr_operations;
 
 /* CSR function table constants */
target/riscv/csr.c +68 -35

117 lines not on this page · raw message

Signature · 1 line
2.30.2
#6 lore raw

Re: [RFC 2/5] target/riscv: Introduce privilege version field in the CSR ops.

Review

Quoted 2 lines · Atish Patra
(quoted diff) +2 -0
+    [CSR_VSTART]   = { "vstart",   vs,     read_vstart,  write_vstart, NULL,
+                                           NULL, NULL, PRIV_VERSION_1_12_0 },

I think adding all of these NULLs are hard to read. Better to just add

    .min_priv_ver = PRIV_VERSION_1_12_0

to the existing entry.

r~

#7 lore raw

Re: [RFC 2/5] target/riscv: Introduce privilege version field in the CSR ops.

Quoted 4 lines · Richard Henderson
Quoted 1 line · Atish Patra

+ [CSR_VSTART] = { "vstart", vs, read_vstart, write_vstart,

NULL,

Quoted 1 line

+ NULL, NULL,

Quoted 10 lines

PRIV_VERSION_1_12_0 },

I think adding all of these NULLs are hard to read. Better to just add

     .min_priv_ver = PRIV_VERSION_1_12_0

to the existing entry.

Yeah. That's better. Fixed it.

Quoted 3 lines

r~

#8 lore raw

RFC 3/5 target/riscv: Add support for mconfigptr

In reply to Atish Patra

RISC-V privileged specification v1.12 introduced a mconfigptr which will hold the physical address of a configuration data structure. As Qemu doesn't have a configuration data structure, is read as zero which is valid as per the priv spec.

---
 target/riscv/cpu_bits.h | 1 +
 target/riscv/csr.c      | 2 ++
 2 files changed, 3 insertions(+)
target/riscv/cpu_bits.h +1 -0
index 5a6d49aa64cc..f6f90b5cbd52 100644
@@ -148,6 +148,7 @@
 #define CSR_MARCHID         0xf12
 #define CSR_MIMPID          0xf13
 #define CSR_MHARTID         0xf14
+#define CSR_MCONFIGPTR      0xf15
 
 /* Machine Trap Setup */
 #define CSR_MSTATUS         0x300
target/riscv/csr.c +2 -0
index 762d3269b4a4..e66bf2201857 100644
@@ -2030,6 +2030,8 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
     [CSR_MARCHID]   = { "marchid",   any,   read_zero    },
     [CSR_MIMPID]    = { "mimpid",    any,   read_zero    },
     [CSR_MHARTID]   = { "mhartid",   any,   read_mhartid },
+    [CSR_MCONFIGPTR]   = { "mconfigptr", any, read_zero, NULL, NULL, NULL, NULL,
+                                         PRIV_VERSION_1_12_0},
 
     /* Machine Trap Setup */
     [CSR_MSTATUS]     = { "mstatus",    any,   read_mstatus,     write_mstatus, NULL,
Signature · 1 line
2.30.2
#9 lore raw

RFC 4/5 target/riscv: Add *envcfg* CSRs support

In reply to Atish Patra

The RISC-V privileged specification v1.12 defines few execution environment configuration CSRs that can be used enable/disable extensions per privilege levels.

Add the basic support for these CSRs.

---
 target/riscv/cpu.h      |  8 ++++
 target/riscv/cpu_bits.h | 31 +++++++++++++++
 target/riscv/csr.c      | 84 +++++++++++++++++++++++++++++++++++++++++
 target/riscv/machine.c  | 26 +++++++++++++
 4 files changed, 149 insertions(+)
target/riscv/cpu.h +8 -0
index 7f87917204c5..b9462300a472 100644
@@ -264,6 +264,14 @@ struct CPURISCVState {
     target_ulong spmbase;
     target_ulong upmmask;
     target_ulong upmbase;
+
+    /* CSRs for execution enviornment configuration */
+
+    target_ulong menvcfg;
+    target_ulong menvcfgh;
+    target_ulong senvcfg;
+    target_ulong henvcfg;
+    target_ulong henvcfgh;
 #endif
 
     float_status fp_status;
target/riscv/cpu_bits.h +31 -0
index f6f90b5cbd52..afb237c2313b 100644
@@ -177,6 +177,9 @@
 #define CSR_STVEC           0x105
 #define CSR_SCOUNTEREN      0x106
 
+/* Supervisor Configuration CSRs */
+#define CSR_SENVCFG         0x10A
+
 /* Supervisor Trap Handling */
 #define CSR_SSCRATCH        0x140
 #define CSR_SEPC            0x141
@@ -204,6 +207,10 @@
 #define CSR_HTIMEDELTA      0x605
 #define CSR_HTIMEDELTAH     0x615
 
+/* Hypervisor Configuration CSRs */
+#define CSR_HENVCFG         0x60A
+#define CSR_HENVCFGH        0x61A
+
 /* Virtual CSRs */
 #define CSR_VSSTATUS        0x200
 #define CSR_VSIE            0x204
@@ -218,6 +225,10 @@
 #define CSR_MTINST          0x34a
 #define CSR_MTVAL2          0x34b
 
+/* Machine Configuration CSRs */
+#define CSR_MENVCFG         0x30A
+#define CSR_MENVCFGH        0x31A
+
 /* Enhanced Physical Memory Protection (ePMP) */
 #define CSR_MSECCFG         0x747
 #define CSR_MSECCFGH        0x757
@@ -578,6 +589,26 @@ typedef enum RISCVException {
 #define PM_EXT_CLEAN    0x00000002ULL
 #define PM_EXT_DIRTY    0x00000003ULL
 
+/* Execution enviornment configuration bits */
+#define MENVCFG_FIOM                       (1 << 0)
+#define MENVCFG_CBE                        0x30000ULL
+#define MENVCFG_CBCFE                      (1 << 6)
+#define MENVCFG_CBZE                       (1 << 7)
+#define MENVCFG_PBMTE                      (1 << 62)
+#define MENVCFG_STCE                       (1 << 63)
+
+#define SENVCFG_FIOM                       MENVCFG_FIOM
+#define SENVCFG_CBE                        MENVCFG_CBE
+#define SENVCFG_CBCFE                      MENVCFG_CBCFE
+#define SENVCFG_CBZE                       MENVCFG_CBZE
+
+#define HENVCFG_FIOM                       MENVCFG_FIOM
+#define HENVCFG_CBE                        MENVCFG_CBE
+#define HENVCFG_CBCFE                      MENVCFG_CBCFE
+#define HENVCFG_CBZE                       MENVCFG_CBZE
+#define HENVCFG_PBMTE                      MENVCFG_PBMTE
+#define HENVCFG_STCE                       MENVCFG_STCE
+
 /* Offsets for every pair of control bits per each priv level */
 #define XS_OFFSET    0ULL
 #define U_OFFSET     2ULL
target/riscv/csr.c +84 -0

96 lines not on this page · raw message

target/riscv/machine.c +26 -0
index 13b9ab375b95..59479a999b87 100644
@@ -185,6 +185,31 @@ static const VMStateDescription vmstate_rv128 = {
     }
 };
 
+/* TODO: henvcfg need both hyper_needed & envcfg_needed */
+static bool envcfg_needed(void *opaque)
+{
+    RISCVCPU *cpu = opaque;
+    CPURISCVState *env = &cpu->env;
+
+    return (env->priv_ver >= PRIV_VERSION_1_12_0 ? 1 : 0);
+}
+
+static const VMStateDescription vmstate_envcfg = {
+    .name = "cpu/envcfg",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .needed = envcfg_needed,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINTTL(env.menvcfg, RISCVCPU),
+        VMSTATE_UINTTL(env.menvcfgh, RISCVCPU),
+        VMSTATE_UINTTL(env.senvcfg, RISCVCPU),
+        VMSTATE_UINTTL(env.henvcfg, RISCVCPU),
+        VMSTATE_UINTTL(env.henvcfgh, RISCVCPU),
+
+        VMSTATE_END_OF_LIST()
+    }
+};
+
 const VMStateDescription vmstate_riscv_cpu = {
     .name = "cpu",
     .version_id = 3,
@@ -240,6 +265,7 @@ const VMStateDescription vmstate_riscv_cpu = {
         &vmstate_vector,
         &vmstate_pointermasking,
         &vmstate_rv128,
+        &vmstate_envcfg,
         NULL
     }
 };
Signature · 1 line
2.30.2
#10 lore raw

Re: [RFC 4/5] target/riscv: Add *envcfg* CSRs support

Quoted 19 lines · Atish Patra

The RISC-V privileged specification v1.12 defines few execution environment configuration CSRs that can be used enable/disable extensions per privilege levels.

Add the basic support for these CSRs.

---
  target/riscv/cpu.h      |  8 ++++
  target/riscv/cpu_bits.h | 31 +++++++++++++++
  target/riscv/csr.c      | 84 +++++++++++++++++++++++++++++++++++++++++
  target/riscv/machine.c  | 26 +++++++++++++
  4 files changed, 149 insertions(+)
target/riscv/cpu.h +0 -0
index 7f87917204c5..b9462300a472 100644

target/riscv/cpu.h struct CPURISCVState { · line 271

Quoted 8 lines
target/riscv/cpu.h +5 -0
@@ -264,6 +264,14 @@ struct CPURISCVState {
      target_ulong spmbase;
      target_ulong upmmask;
      target_ulong upmbase;
+
+    /* CSRs for execution enviornment configuration */
+
+    target_ulong menvcfg;
+    target_ulong menvcfgh;

I think we needn't maintain seperate menvcfg and menvcfgh, just use "uint64_t menvcfg" as the way of mstatus.

Similar to  henvcfg and henvcfg.

Quoted 158 lines
target/riscv/cpu.h +3 -0
+    target_ulong senvcfg;
+    target_ulong henvcfg;
+    target_ulong henvcfgh;
  #endif
 
      float_status fp_status;
target/riscv/cpu_bits.h +31 -0
index f6f90b5cbd52..afb237c2313b 100644
@@ -177,6 +177,9 @@
  #define CSR_STVEC           0x105
  #define CSR_SCOUNTEREN      0x106
 
+/* Supervisor Configuration CSRs */
+#define CSR_SENVCFG         0x10A
+
  /* Supervisor Trap Handling */
  #define CSR_SSCRATCH        0x140
  #define CSR_SEPC            0x141
@@ -204,6 +207,10 @@
  #define CSR_HTIMEDELTA      0x605
  #define CSR_HTIMEDELTAH     0x615
 
+/* Hypervisor Configuration CSRs */
+#define CSR_HENVCFG         0x60A
+#define CSR_HENVCFGH        0x61A
+
  /* Virtual CSRs */
  #define CSR_VSSTATUS        0x200
  #define CSR_VSIE            0x204
@@ -218,6 +225,10 @@
  #define CSR_MTINST          0x34a
  #define CSR_MTVAL2          0x34b
 
+/* Machine Configuration CSRs */
+#define CSR_MENVCFG         0x30A
+#define CSR_MENVCFGH        0x31A
+
  /* Enhanced Physical Memory Protection (ePMP) */
  #define CSR_MSECCFG         0x747
  #define CSR_MSECCFGH        0x757
@@ -578,6 +589,26 @@ typedef enum RISCVException {
  #define PM_EXT_CLEAN    0x00000002ULL
  #define PM_EXT_DIRTY    0x00000003ULL
 
+/* Execution enviornment configuration bits */
+#define MENVCFG_FIOM                       (1 << 0)
+#define MENVCFG_CBE                        0x30000ULL
+#define MENVCFG_CBCFE                      (1 << 6)
+#define MENVCFG_CBZE                       (1 << 7)
+#define MENVCFG_PBMTE                      (1 << 62)
+#define MENVCFG_STCE                       (1 << 63)
+
+#define SENVCFG_FIOM                       MENVCFG_FIOM
+#define SENVCFG_CBE                        MENVCFG_CBE
+#define SENVCFG_CBCFE                      MENVCFG_CBCFE
+#define SENVCFG_CBZE                       MENVCFG_CBZE
+
+#define HENVCFG_FIOM                       MENVCFG_FIOM
+#define HENVCFG_CBE                        MENVCFG_CBE
+#define HENVCFG_CBCFE                      MENVCFG_CBCFE
+#define HENVCFG_CBZE                       MENVCFG_CBZE
+#define HENVCFG_PBMTE                      MENVCFG_PBMTE
+#define HENVCFG_STCE                       MENVCFG_STCE
+
  /* Offsets for every pair of control bits per each priv level */
  #define XS_OFFSET    0ULL
  #define U_OFFSET     2ULL
target/riscv/csr.c +74 -0
index e66bf2201857..a4bbae7a1bbd 100644
@@ -853,6 +853,77 @@ static RISCVException write_mtval(CPURISCVState *env, int csrno,
      return RISCV_EXCP_NONE;
  }
 
+/* Execution environment configuration setup */
+static RISCVException read_menvcfg(CPURISCVState *env, int csrno,
+                                 target_ulong *val)
+{
+    *val = env->menvcfg;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_menvcfg(CPURISCVState *env, int csrno,
+                                  target_ulong val)
+{
+    env->menvcfg = val;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException read_menvcfgh(CPURISCVState *env, int csrno,
+                                 target_ulong *val)
+{
+    *val = env->menvcfgh;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_menvcfgh(CPURISCVState *env, int csrno,
+                                  target_ulong val)
+{
+    env->menvcfgh = val;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException read_senvcfg(CPURISCVState *env, int csrno,
+                                 target_ulong *val)
+{
+    *val = env->senvcfg;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_senvcfg(CPURISCVState *env, int csrno,
+                                  target_ulong val)
+{
+    env->senvcfg = val;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException read_henvcfg(CPURISCVState *env, int csrno,
+                                 target_ulong *val)
+{
+    *val = env->henvcfg;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_henvcfg(CPURISCVState *env, int csrno,
+                                  target_ulong val)
+{
+    env->henvcfg = val;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException read_henvcfgh(CPURISCVState *env, int csrno,
+                                 target_ulong *val)
+{
+    *val = env->henvcfgh;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_henvcfgh(CPURISCVState *env, int csrno,
+                                  target_ulong val)
+{
+    env->henvcfgh = val;
+    return RISCV_EXCP_NONE;
+}
+
  static RISCVException rmw_mip(CPURISCVState *env, int csrno,
                                target_ulong *ret_value,
                                target_ulong new_value, target_ulong write_mask)
@@ -2054,6 +2125,19 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
      [CSR_MTVAL]    = { "mtval",    any,  read_mtval,    write_mtval    },
      [CSR_MIP]      = { "mip",      any,  NULL,    NULL, rmw_mip        },
 
+    /* Execution environment configuration */
+    [CSR_MENVCFG]  = { "menvcfg",  any,     read_menvcfg,  write_menvcfg, NULL,
+                                            NULL, NULL, PRIV_VERSION_1_12_0},

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

Quoted 10 lines
target/riscv/csr.c +10 -0
+    [CSR_MENVCFGH] = { "menvcfgh", any32,   read_menvcfgh, write_menvcfgh, NULL,
+                                            NULL, NULL, PRIV_VERSION_1_12_0},
+    [CSR_SENVCFG]  = { "senvcfg",  smode,   read_senvcfg,  write_senvcfg, NULL,
+                                            NULL, NULL, PRIV_VERSION_1_12_0},
+    [CSR_HENVCFG]  = { "henvcfg",  hmode,   read_henvcfg, write_henvcfg, NULL,
+                                            NULL, NULL, PRIV_VERSION_1_12_0},
+    [CSR_HENVCFGH] = { "henvcfgh", hmode32, read_henvcfgh, write_henvcfgh, NULL,
+                                            NULL, NULL, PRIV_VERSION_1_12_0},
+
+

Two new lines here.

Quoted 40 lines
target/riscv/csr.c +0 -0
      /* Supervisor Trap Setup */
      [CSR_SSTATUS]    = { "sstatus",    smode, read_sstatus,    write_sstatus, NULL,
                                                read_sstatus_i128                 },
target/riscv/machine.c +25 -0
index 13b9ab375b95..59479a999b87 100644
@@ -185,6 +185,31 @@ static const VMStateDescription vmstate_rv128 = {
      }
  };
 
+/* TODO: henvcfg need both hyper_needed & envcfg_needed */
+static bool envcfg_needed(void *opaque)
+{
+    RISCVCPU *cpu = opaque;
+    CPURISCVState *env = &cpu->env;
+
+    return (env->priv_ver >= PRIV_VERSION_1_12_0 ? 1 : 0);
+}
+
+static const VMStateDescription vmstate_envcfg = {
+    .name = "cpu/envcfg",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .needed = envcfg_needed,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINTTL(env.menvcfg, RISCVCPU),
+        VMSTATE_UINTTL(env.menvcfgh, RISCVCPU),
+        VMSTATE_UINTTL(env.senvcfg, RISCVCPU),
+        VMSTATE_UINTTL(env.henvcfg, RISCVCPU),
+        VMSTATE_UINTTL(env.henvcfgh, RISCVCPU),
+
+        VMSTATE_END_OF_LIST()
+    }
+};
+
  const VMStateDescription vmstate_riscv_cpu = {
      .name = "cpu",
      .version_id = 3,

target/riscv/machine.c const VMStateDescription vmstate_riscv_cpu = { · line 271

Quoted 7 lines
target/riscv/machine.c +1 -0
@@ -240,6 +265,7 @@ const VMStateDescription vmstate_riscv_cpu = {
          &vmstate_vector,
          &vmstate_pointermasking,
          &vmstate_rv128,
+        &vmstate_envcfg,
          NULL
      }
  };

Regards,

Weiwei Li

#11 lore raw

Re: [RFC 4/5] target/riscv: Add *envcfg* CSRs support

Quoted 16 lines · Weiwei Li
Quoted 14 lines · Atish Patra

The RISC-V privileged specification v1.12 defines few execution environment configuration CSRs that can be used enable/disable extensions per privilege levels.

Add the basic support for these CSRs.

---
  target/riscv/cpu.h      |  8 ++++
  target/riscv/cpu_bits.h | 31 +++++++++++++++
  target/riscv/csr.c      | 84 +++++++++++++++++++++++++++++++++++++++++
  target/riscv/machine.c  | 26 +++++++++++++
  4 files changed, 149 insertions(+)
Quoted 18 lines

target/riscv/cpu.h struct CPURISCVState { · line 271

Quoted 13 lines
target/riscv/cpu.h +5 -0
index 7f87917204c5..b9462300a472 100644
@@ -264,6 +264,14 @@ struct CPURISCVState {
      target_ulong spmbase;
      target_ulong upmmask;
      target_ulong upmbase;
+
+    /* CSRs for execution enviornment configuration */
+
+    target_ulong menvcfg;
+    target_ulong menvcfgh;

I think we needn't maintain seperate menvcfg and menvcfgh, just use "uint64_t menvcfg" as the way of mstatus.

unlike mstatush, menvcfgh/henvcfgh will be accessed directly to do runtime predicate for stimecmp/vstimecmp.

We have to do the 32 bit shifting during every check which makes the code hard to read
at the cost of 2 ulongs.

IMO, having separate variables is much simpler.

Similar to henvcfg and henvcfg.

Quoted 176 lines

target/riscv/csr.c static RISCVException write_mtval(CPURISCVState

Quoted 75 lines
target/riscv/cpu.h +3 -0
+    target_ulong senvcfg;
+    target_ulong henvcfg;
+    target_ulong henvcfgh;
  #endif
 
      float_status fp_status;
target/riscv/cpu_bits.h +31 -0
index f6f90b5cbd52..afb237c2313b 100644
@@ -177,6 +177,9 @@
  #define CSR_STVEC           0x105
  #define CSR_SCOUNTEREN      0x106
 
+/* Supervisor Configuration CSRs */
+#define CSR_SENVCFG         0x10A
+
  /* Supervisor Trap Handling */
  #define CSR_SSCRATCH        0x140
  #define CSR_SEPC            0x141
@@ -204,6 +207,10 @@
  #define CSR_HTIMEDELTA      0x605
  #define CSR_HTIMEDELTAH     0x615
 
+/* Hypervisor Configuration CSRs */
+#define CSR_HENVCFG         0x60A
+#define CSR_HENVCFGH        0x61A
+
  /* Virtual CSRs */
  #define CSR_VSSTATUS        0x200
  #define CSR_VSIE            0x204
@@ -218,6 +225,10 @@
  #define CSR_MTINST          0x34a
  #define CSR_MTVAL2          0x34b
 
+/* Machine Configuration CSRs */
+#define CSR_MENVCFG         0x30A
+#define CSR_MENVCFGH        0x31A
+
  /* Enhanced Physical Memory Protection (ePMP) */
  #define CSR_MSECCFG         0x747
  #define CSR_MSECCFGH        0x757
@@ -578,6 +589,26 @@ typedef enum RISCVException {
  #define PM_EXT_CLEAN    0x00000002ULL
  #define PM_EXT_DIRTY    0x00000003ULL
 
+/* Execution enviornment configuration bits */
+#define MENVCFG_FIOM                       (1 << 0)
+#define MENVCFG_CBE                        0x30000ULL
+#define MENVCFG_CBCFE                      (1 << 6)
+#define MENVCFG_CBZE                       (1 << 7)
+#define MENVCFG_PBMTE                      (1 << 62)
+#define MENVCFG_STCE                       (1 << 63)
+
+#define SENVCFG_FIOM                       MENVCFG_FIOM
+#define SENVCFG_CBE                        MENVCFG_CBE
+#define SENVCFG_CBCFE                      MENVCFG_CBCFE
+#define SENVCFG_CBZE                       MENVCFG_CBZE
+
+#define HENVCFG_FIOM                       MENVCFG_FIOM
+#define HENVCFG_CBE                        MENVCFG_CBE
+#define HENVCFG_CBCFE                      MENVCFG_CBCFE
+#define HENVCFG_CBZE                       MENVCFG_CBZE
+#define HENVCFG_PBMTE                      MENVCFG_PBMTE
+#define HENVCFG_STCE                       MENVCFG_STCE
+
  /* Offsets for every pair of control bits per each priv level */
  #define XS_OFFSET    0ULL
  #define U_OFFSET     2ULL
target/riscv/csr.c +0 -0
index e66bf2201857..a4bbae7a1bbd 100644
@@ -853,6 +853,77 @@ static RISCVException write_mtval(CPURISCVState

*env, int csrno,

Quoted 67 lines
target/riscv/csr.c +64 -0
      return RISCV_EXCP_NONE;
  }
 
+/* Execution environment configuration setup */
+static RISCVException read_menvcfg(CPURISCVState *env, int csrno,
+                                 target_ulong *val)
+{
+    *val = env->menvcfg;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_menvcfg(CPURISCVState *env, int csrno,
+                                  target_ulong val)
+{
+    env->menvcfg = val;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException read_menvcfgh(CPURISCVState *env, int csrno,
+                                 target_ulong *val)
+{
+    *val = env->menvcfgh;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_menvcfgh(CPURISCVState *env, int csrno,
+                                  target_ulong val)
+{
+    env->menvcfgh = val;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException read_senvcfg(CPURISCVState *env, int csrno,
+                                 target_ulong *val)
+{
+    *val = env->senvcfg;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_senvcfg(CPURISCVState *env, int csrno,
+                                  target_ulong val)
+{
+    env->senvcfg = val;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException read_henvcfg(CPURISCVState *env, int csrno,
+                                 target_ulong *val)
+{
+    *val = env->henvcfg;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_henvcfg(CPURISCVState *env, int csrno,
+                                  target_ulong val)
+{
+    env->henvcfg = val;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException read_henvcfgh(CPURISCVState *env, int csrno,
+                                 target_ulong *val)
+{
+    *val = env->henvcfgh;
+    return RISCV_EXCP_NONE;
+}
+

target/riscv/csr.c static RISCVException write_mtval(CPURISCVState *env, int csrno,

Quoted 10 lines
target/riscv/csr.c +7 -0
+static RISCVException write_henvcfgh(CPURISCVState *env, int csrno,
+                                  target_ulong val)
+{
+    env->henvcfgh = val;
+    return RISCV_EXCP_NONE;
+}
+
  static RISCVException rmw_mip(CPURISCVState *env, int csrno,
                                target_ulong *ret_value,
                                target_ulong new_value, target_ulong

write_mask)

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

Quoted 2 lines
target/riscv/csr.c +0 -0
@@ -2054,6 +2125,19 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
      [CSR_MTVAL]    = { "mtval",    any,  read_mtval,    write_mtval

},

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

Quoted 1 line
target/riscv/csr.c +0 -0
      [CSR_MIP]      = { "mip",      any,  NULL,    NULL, rmw_mip

},

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

Quoted 3 lines
target/riscv/csr.c +2 -0
+    /* Execution environment configuration */
+    [CSR_MENVCFG]  = { "menvcfg",  any,     read_menvcfg,

write_menvcfg, NULL,

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

Quoted 1 line
target/riscv/csr.c +1 -0
+                                            NULL, NULL,

PRIV_VERSION_1_12_0},

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

Quoted 1 line
target/riscv/csr.c +1 -0
+    [CSR_MENVCFGH] = { "menvcfgh", any32,   read_menvcfgh,

write_menvcfgh, NULL,

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

Quoted 1 line
target/riscv/csr.c +1 -0
+                                            NULL, NULL,

PRIV_VERSION_1_12_0},

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

Quoted 1 line
target/riscv/csr.c +1 -0
+    [CSR_SENVCFG]  = { "senvcfg",  smode,   read_senvcfg,

write_senvcfg, NULL,

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

Quoted 1 line
target/riscv/csr.c +1 -0
+                                            NULL, NULL,

PRIV_VERSION_1_12_0},

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

Quoted 1 line
target/riscv/csr.c +1 -0
+    [CSR_HENVCFG]  = { "henvcfg",  hmode,   read_henvcfg,

write_henvcfg, NULL,

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

Quoted 1 line
target/riscv/csr.c +1 -0
+                                            NULL, NULL,
Quoted 9 lines

PRIV_VERSION_1_12_0},

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

Quoted 1 line
target/riscv/csr.c +1 -0
+    [CSR_HENVCFGH] = { "henvcfgh", hmode32, read_henvcfgh,

write_henvcfgh, NULL,

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

Quoted 1 line
target/riscv/csr.c +1 -0
+                                            NULL, NULL,

PRIV_VERSION_1_12_0},

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

Quoted 2 lines
target/riscv/csr.c +2 -0
+
+

Two new lines here.

Fixed it. Thanks.

Quoted 56 lines

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

Quoted 2 lines
target/riscv/csr.c +0 -0
      /* Supervisor Trap Setup */
      [CSR_SSTATUS]    = { "sstatus",    smode, read_sstatus,

write_sstatus, NULL,

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

Quoted 1 line
target/riscv/csr.c +0 -0
                                                read_sstatus_i128
         },
Quoted 37 lines
target/riscv/machine.c +25 -0
index 13b9ab375b95..59479a999b87 100644
@@ -185,6 +185,31 @@ static const VMStateDescription vmstate_rv128 = {
      }
  };
 
+/* TODO: henvcfg need both hyper_needed & envcfg_needed */
+static bool envcfg_needed(void *opaque)
+{
+    RISCVCPU *cpu = opaque;
+    CPURISCVState *env = &cpu->env;
+
+    return (env->priv_ver >= PRIV_VERSION_1_12_0 ? 1 : 0);
+}
+
+static const VMStateDescription vmstate_envcfg = {
+    .name = "cpu/envcfg",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .needed = envcfg_needed,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINTTL(env.menvcfg, RISCVCPU),
+        VMSTATE_UINTTL(env.menvcfgh, RISCVCPU),
+        VMSTATE_UINTTL(env.senvcfg, RISCVCPU),
+        VMSTATE_UINTTL(env.henvcfg, RISCVCPU),
+        VMSTATE_UINTTL(env.henvcfgh, RISCVCPU),
+
+        VMSTATE_END_OF_LIST()
+    }
+};
+
  const VMStateDescription vmstate_riscv_cpu = {
      .name = "cpu",
      .version_id = 3,

target/riscv/machine.c const VMStateDescription vmstate_riscv_cpu = { · line 271

Quoted 7 lines
target/riscv/machine.c +1 -0
@@ -240,6 +265,7 @@ const VMStateDescription vmstate_riscv_cpu = {
          &vmstate_vector,
          &vmstate_pointermasking,
          &vmstate_rv128,
+        &vmstate_envcfg,
          NULL
      }
  };

Regards,

Weiwei Li

Signature · 2 lines
Regards,
Atish
#12 lore raw

Re: [RFC 4/5] target/riscv: Add *envcfg* CSRs support

Quoted 40 lines · Atish Patra

On Wed, Jan 26, 2022 at 12:37 AM Weiwei Li <[email protected] <mailto:[email protected]>> wrote:

    在 2022/1/21 上午4:07, Atish Patra 写道:
    > The RISC-V privileged specification v1.12 defines few execution
    > environment configuration CSRs that can be used enable/disable
    > extensions per privilege levels.
    >
    > Add the basic support for these CSRs.
    >
    > Signed-off-by: Atish Patra <[email protected]
    <mailto:[email protected]>>
    > ---
    >   target/riscv/cpu.h      |  8 ++++
    >   target/riscv/cpu_bits.h | 31 +++++++++++++++
    >   target/riscv/csr.c      | 84
    +++++++++++++++++++++++++++++++++++++++++
    >   target/riscv/machine.c  | 26 +++++++++++++
    >   4 files changed, 149 insertions(+)
    >
    > diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h
    > index 7f87917204c5..b9462300a472 100644
    > --- a/target/riscv/cpu.h
    > +++ b/target/riscv/cpu.h
    > @@ -264,6 +264,14 @@ struct CPURISCVState {
    >       target_ulong spmbase;
    >       target_ulong upmmask;
    >       target_ulong upmbase;
    > +
    > +    /* CSRs for execution enviornment configuration */
    > +
    > +    target_ulong menvcfg;
    > +    target_ulong menvcfgh;

    I think we needn't maintain seperate menvcfg and menvcfgh, just use
    "uint64_t menvcfg" as the way of mstatus.
Quoted 7 lines

unlike mstatush, menvcfgh/henvcfgh will be accessed directly to do runtime predicate for stimecmp/vstimecmp.

We have to do the 32 bit shifting during every check which makes the code hard to read at the cost of 2 ulongs.

IMO, having separate variables is much simpler.

Do you mean check STCE/VSTCE bit in menvcfg/henvcfg?

If so, I think use a simple "uint64_t menvcfg/henvcfg" may be better,  then we can only check the 63 bit of them.

Or we should decide where to get this bit from(mencvfg/henvcfg, or mencfgh/henvcfgh) based on the MXLEN/HSXLEN.

Regards,

Weiwei Li

Quoted 251 lines
    Similar to  henvcfg and henvcfg.

    > +    target_ulong senvcfg;
    > +    target_ulong henvcfg;
    > +    target_ulong henvcfgh;
    >   #endif
    >
    >       float_status fp_status;
    > diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h
    > index f6f90b5cbd52..afb237c2313b 100644
    > --- a/target/riscv/cpu_bits.h
    > +++ b/target/riscv/cpu_bits.h
    > @@ -177,6 +177,9 @@
    >   #define CSR_STVEC           0x105
    >   #define CSR_SCOUNTEREN      0x106
    >
    > +/* Supervisor Configuration CSRs */
    > +#define CSR_SENVCFG         0x10A
    > +
    >   /* Supervisor Trap Handling */
    >   #define CSR_SSCRATCH        0x140
    >   #define CSR_SEPC            0x141
    > @@ -204,6 +207,10 @@
    >   #define CSR_HTIMEDELTA      0x605
    >   #define CSR_HTIMEDELTAH     0x615
    >
    > +/* Hypervisor Configuration CSRs */
    > +#define CSR_HENVCFG         0x60A
    > +#define CSR_HENVCFGH        0x61A
    > +
    >   /* Virtual CSRs */
    >   #define CSR_VSSTATUS        0x200
    >   #define CSR_VSIE            0x204
    > @@ -218,6 +225,10 @@
    >   #define CSR_MTINST          0x34a
    >   #define CSR_MTVAL2          0x34b
    >
    > +/* Machine Configuration CSRs */
    > +#define CSR_MENVCFG         0x30A
    > +#define CSR_MENVCFGH        0x31A
    > +
    >   /* Enhanced Physical Memory Protection (ePMP) */
    >   #define CSR_MSECCFG         0x747
    >   #define CSR_MSECCFGH        0x757
    > @@ -578,6 +589,26 @@ typedef enum RISCVException {
    >   #define PM_EXT_CLEAN    0x00000002ULL
    >   #define PM_EXT_DIRTY    0x00000003ULL
    >
    > +/* Execution enviornment configuration bits */
    > +#define MENVCFG_FIOM                       (1 << 0)
    > +#define MENVCFG_CBE                        0x30000ULL
    > +#define MENVCFG_CBCFE                      (1 << 6)
    > +#define MENVCFG_CBZE                       (1 << 7)
    > +#define MENVCFG_PBMTE                      (1 << 62)
    > +#define MENVCFG_STCE                       (1 << 63)
    > +
    > +#define SENVCFG_FIOM  MENVCFG_FIOM
    > +#define SENVCFG_CBE                        MENVCFG_CBE
    > +#define SENVCFG_CBCFE MENVCFG_CBCFE
    > +#define SENVCFG_CBZE  MENVCFG_CBZE
    > +
    > +#define HENVCFG_FIOM  MENVCFG_FIOM
    > +#define HENVCFG_CBE                        MENVCFG_CBE
    > +#define HENVCFG_CBCFE MENVCFG_CBCFE
    > +#define HENVCFG_CBZE  MENVCFG_CBZE
    > +#define HENVCFG_PBMTE MENVCFG_PBMTE
    > +#define HENVCFG_STCE  MENVCFG_STCE
    > +
    >   /* Offsets for every pair of control bits per each priv level */
    >   #define XS_OFFSET    0ULL
    >   #define U_OFFSET     2ULL
    > diff --git a/target/riscv/csr.c b/target/riscv/csr.c
    > index e66bf2201857..a4bbae7a1bbd 100644
    > --- a/target/riscv/csr.c
    > +++ b/target/riscv/csr.c
    > @@ -853,6 +853,77 @@ static RISCVException
    write_mtval(CPURISCVState *env, int csrno,
    >       return RISCV_EXCP_NONE;
    >   }
    >
    > +/* Execution environment configuration setup */
    > +static RISCVException read_menvcfg(CPURISCVState *env, int csrno,
    > +                                 target_ulong *val)
    > +{
    > +    *val = env->menvcfg;
    > +    return RISCV_EXCP_NONE;
    > +}
    > +
    > +static RISCVException write_menvcfg(CPURISCVState *env, int csrno,
    > +                                  target_ulong val)
    > +{
    > +    env->menvcfg = val;
    > +    return RISCV_EXCP_NONE;
    > +}
    > +
    > +static RISCVException read_menvcfgh(CPURISCVState *env, int csrno,
    > +                                 target_ulong *val)
    > +{
    > +    *val = env->menvcfgh;
    > +    return RISCV_EXCP_NONE;
    > +}
    > +
    > +static RISCVException write_menvcfgh(CPURISCVState *env, int csrno,
    > +                                  target_ulong val)
    > +{
    > +    env->menvcfgh = val;
    > +    return RISCV_EXCP_NONE;
    > +}
    > +
    > +static RISCVException read_senvcfg(CPURISCVState *env, int csrno,
    > +                                 target_ulong *val)
    > +{
    > +    *val = env->senvcfg;
    > +    return RISCV_EXCP_NONE;
    > +}
    > +
    > +static RISCVException write_senvcfg(CPURISCVState *env, int csrno,
    > +                                  target_ulong val)
    > +{
    > +    env->senvcfg = val;
    > +    return RISCV_EXCP_NONE;
    > +}
    > +
    > +static RISCVException read_henvcfg(CPURISCVState *env, int csrno,
    > +                                 target_ulong *val)
    > +{
    > +    *val = env->henvcfg;
    > +    return RISCV_EXCP_NONE;
    > +}
    > +
    > +static RISCVException write_henvcfg(CPURISCVState *env, int csrno,
    > +                                  target_ulong val)
    > +{
    > +    env->henvcfg = val;
    > +    return RISCV_EXCP_NONE;
    > +}
    > +
    > +static RISCVException read_henvcfgh(CPURISCVState *env, int csrno,
    > +                                 target_ulong *val)
    > +{
    > +    *val = env->henvcfgh;
    > +    return RISCV_EXCP_NONE;
    > +}
    > +
    > +static RISCVException write_henvcfgh(CPURISCVState *env, int csrno,
    > +                                  target_ulong val)
    > +{
    > +    env->henvcfgh = val;
    > +    return RISCV_EXCP_NONE;
    > +}
    > +
    >   static RISCVException rmw_mip(CPURISCVState *env, int csrno,
    >                                 target_ulong *ret_value,
    >                                 target_ulong new_value,
    target_ulong write_mask)
    > @@ -2054,6 +2125,19 @@ riscv_csr_operations
    csr_ops[CSR_TABLE_SIZE] = {
    >       [CSR_MTVAL]    = { "mtval",    any,  read_mtval,  
    write_mtval    },
    >       [CSR_MIP]      = { "mip",      any,  NULL, NULL, rmw_mip 
          },
    >
    > +    /* Execution environment configuration */
    > +    [CSR_MENVCFG]  = { "menvcfg",  any,  read_menvcfg, 
    write_menvcfg, NULL,
    > +                                            NULL, NULL,
    PRIV_VERSION_1_12_0},
    > +    [CSR_MENVCFGH] = { "menvcfgh", any32,  read_menvcfgh,
    write_menvcfgh, NULL,
    > +                                            NULL, NULL,
    PRIV_VERSION_1_12_0},
    > +    [CSR_SENVCFG]  = { "senvcfg",  smode,  read_senvcfg, 
    write_senvcfg, NULL,
    > +                                            NULL, NULL,
    PRIV_VERSION_1_12_0},
    > +    [CSR_HENVCFG]  = { "henvcfg",  hmode,  read_henvcfg,
    write_henvcfg, NULL,
    > +                                            NULL, NULL,
    PRIV_VERSION_1_12_0},
    > +    [CSR_HENVCFGH] = { "henvcfgh", hmode32, read_henvcfgh,
    write_henvcfgh, NULL,
    > +                                            NULL, NULL,
    PRIV_VERSION_1_12_0},
    > +
    > +
    Two new lines here.

Fixed it. Thanks.

Quoted 2 lines

       /* Supervisor Trap Setup */
       [CSR_SSTATUS]    = { "sstatus",    smode, read_sstatus,

    write_sstatus, NULL,
    >  read_sstatus_i128                 },
    > diff --git a/target/riscv/machine.c b/target/riscv/machine.c
    > index 13b9ab375b95..59479a999b87 100644
    > --- a/target/riscv/machine.c
    > +++ b/target/riscv/machine.c
    > @@ -185,6 +185,31 @@ static const VMStateDescription
    vmstate_rv128 = {
    >       }
    >   };
    >
    > +/* TODO: henvcfg need both hyper_needed & envcfg_needed */
    > +static bool envcfg_needed(void *opaque)
    > +{
    > +    RISCVCPU *cpu = opaque;
    > +    CPURISCVState *env = &cpu->env;
    > +
    > +    return (env->priv_ver >= PRIV_VERSION_1_12_0 ? 1 : 0);
    > +}
    > +
    > +static const VMStateDescription vmstate_envcfg = {
    > +    .name = "cpu/envcfg",
    > +    .version_id = 1,
    > +    .minimum_version_id = 1,
    > +    .needed = envcfg_needed,
    > +    .fields = (VMStateField[]) {
    > +        VMSTATE_UINTTL(env.menvcfg, RISCVCPU),
    > +        VMSTATE_UINTTL(env.menvcfgh, RISCVCPU),
    > +        VMSTATE_UINTTL(env.senvcfg, RISCVCPU),
    > +        VMSTATE_UINTTL(env.henvcfg, RISCVCPU),
    > +        VMSTATE_UINTTL(env.henvcfgh, RISCVCPU),
    > +
    > +        VMSTATE_END_OF_LIST()
    > +    }
    > +};
    > +
    >   const VMStateDescription vmstate_riscv_cpu = {
    >       .name = "cpu",
    >       .version_id = 3,
    > @@ -240,6 +265,7 @@ const VMStateDescription vmstate_riscv_cpu = {
    >           &vmstate_vector,
    >           &vmstate_pointermasking,
    >           &vmstate_rv128,
    > +        &vmstate_envcfg,
    >           NULL
    >       }
    >   };

    Regards,

    Weiwei Li
Signature · 2 lines
Regards,
Atish
#13 lore raw

Re: [RFC 4/5] target/riscv: Add *envcfg* CSRs support

Quoted 48 lines · On Fri, Jan 28, 2022 at 5:50 PM angell1518 <[email protected]> wrote:

在 2022/1/29 上午9:28, Atish Patra 写道:

Quoted 16 lines · Weiwei Li
Quoted 14 lines · Atish Patra

The RISC-V privileged specification v1.12 defines few execution environment configuration CSRs that can be used enable/disable extensions per privilege levels.

Add the basic support for these CSRs.

---
  target/riscv/cpu.h      |  8 ++++
  target/riscv/cpu_bits.h | 31 +++++++++++++++
  target/riscv/csr.c      | 84 +++++++++++++++++++++++++++++++++++++++++
  target/riscv/machine.c  | 26 +++++++++++++
  4 files changed, 149 insertions(+)
Quoted 18 lines

target/riscv/cpu.h struct CPURISCVState { · line 271

Quoted 13 lines
target/riscv/cpu.h +5 -0
index 7f87917204c5..b9462300a472 100644
@@ -264,6 +264,14 @@ struct CPURISCVState {
      target_ulong spmbase;
      target_ulong upmmask;
      target_ulong upmbase;
+
+    /* CSRs for execution enviornment configuration */
+
+    target_ulong menvcfg;
+    target_ulong menvcfgh;

I think we needn't maintain seperate menvcfg and menvcfgh, just use "uint64_t menvcfg" as the way of mstatus.

unlike mstatush, menvcfgh/henvcfgh will be accessed directly to do runtime predicate for stimecmp/vstimecmp.

We have to do the 32 bit shifting during every check which makes the code hard to read
at the cost of 2 ulongs.

Quoted 7 lines

IMO, having separate variables is much simpler.

Do you mean check STCE/VSTCE bit in menvcfg/henvcfg?

If so, I think use a simple "uint64_t menvcfg/henvcfg" may be better, then we can only check the 63 bit of them.

Which is a bit confusing as the STCE bit in mencfgh/henvcfgh is 31 not 63. But that's my personal preference.

I will just leave a comment to clarify the confusion for now. I will send a patch with unified menvcfg and wait for others's feedback.

Quoted 260 lines

Or we should decide where to get this bit from(mencvfg/henvcfg, or mencfgh/henvcfgh) based on the MXLEN/HSXLEN.

Regards,

Weiwei Li

Similar to henvcfg and henvcfg.

Quoted 176 lines

target/riscv/csr.c static RISCVException write_mtval(CPURISCVState

Quoted 75 lines
target/riscv/cpu.h +3 -0
+    target_ulong senvcfg;
+    target_ulong henvcfg;
+    target_ulong henvcfgh;
  #endif
 
      float_status fp_status;
target/riscv/cpu_bits.h +31 -0
index f6f90b5cbd52..afb237c2313b 100644
@@ -177,6 +177,9 @@
  #define CSR_STVEC           0x105
  #define CSR_SCOUNTEREN      0x106
 
+/* Supervisor Configuration CSRs */
+#define CSR_SENVCFG         0x10A
+
  /* Supervisor Trap Handling */
  #define CSR_SSCRATCH        0x140
  #define CSR_SEPC            0x141
@@ -204,6 +207,10 @@
  #define CSR_HTIMEDELTA      0x605
  #define CSR_HTIMEDELTAH     0x615
 
+/* Hypervisor Configuration CSRs */
+#define CSR_HENVCFG         0x60A
+#define CSR_HENVCFGH        0x61A
+
  /* Virtual CSRs */
  #define CSR_VSSTATUS        0x200
  #define CSR_VSIE            0x204
@@ -218,6 +225,10 @@
  #define CSR_MTINST          0x34a
  #define CSR_MTVAL2          0x34b
 
+/* Machine Configuration CSRs */
+#define CSR_MENVCFG         0x30A
+#define CSR_MENVCFGH        0x31A
+
  /* Enhanced Physical Memory Protection (ePMP) */
  #define CSR_MSECCFG         0x747
  #define CSR_MSECCFGH        0x757
@@ -578,6 +589,26 @@ typedef enum RISCVException {
  #define PM_EXT_CLEAN    0x00000002ULL
  #define PM_EXT_DIRTY    0x00000003ULL
 
+/* Execution enviornment configuration bits */
+#define MENVCFG_FIOM                       (1 << 0)
+#define MENVCFG_CBE                        0x30000ULL
+#define MENVCFG_CBCFE                      (1 << 6)
+#define MENVCFG_CBZE                       (1 << 7)
+#define MENVCFG_PBMTE                      (1 << 62)
+#define MENVCFG_STCE                       (1 << 63)
+
+#define SENVCFG_FIOM                       MENVCFG_FIOM
+#define SENVCFG_CBE                        MENVCFG_CBE
+#define SENVCFG_CBCFE                      MENVCFG_CBCFE
+#define SENVCFG_CBZE                       MENVCFG_CBZE
+
+#define HENVCFG_FIOM                       MENVCFG_FIOM
+#define HENVCFG_CBE                        MENVCFG_CBE
+#define HENVCFG_CBCFE                      MENVCFG_CBCFE
+#define HENVCFG_CBZE                       MENVCFG_CBZE
+#define HENVCFG_PBMTE                      MENVCFG_PBMTE
+#define HENVCFG_STCE                       MENVCFG_STCE
+
  /* Offsets for every pair of control bits per each priv level */
  #define XS_OFFSET    0ULL
  #define U_OFFSET     2ULL
target/riscv/csr.c +0 -0
index e66bf2201857..a4bbae7a1bbd 100644
@@ -853,6 +853,77 @@ static RISCVException write_mtval(CPURISCVState

*env, int csrno,

Quoted 67 lines
target/riscv/csr.c +64 -0
      return RISCV_EXCP_NONE;
  }
 
+/* Execution environment configuration setup */
+static RISCVException read_menvcfg(CPURISCVState *env, int csrno,
+                                 target_ulong *val)
+{
+    *val = env->menvcfg;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_menvcfg(CPURISCVState *env, int csrno,
+                                  target_ulong val)
+{
+    env->menvcfg = val;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException read_menvcfgh(CPURISCVState *env, int csrno,
+                                 target_ulong *val)
+{
+    *val = env->menvcfgh;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_menvcfgh(CPURISCVState *env, int csrno,
+                                  target_ulong val)
+{
+    env->menvcfgh = val;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException read_senvcfg(CPURISCVState *env, int csrno,
+                                 target_ulong *val)
+{
+    *val = env->senvcfg;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_senvcfg(CPURISCVState *env, int csrno,
+                                  target_ulong val)
+{
+    env->senvcfg = val;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException read_henvcfg(CPURISCVState *env, int csrno,
+                                 target_ulong *val)
+{
+    *val = env->henvcfg;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException write_henvcfg(CPURISCVState *env, int csrno,
+                                  target_ulong val)
+{
+    env->henvcfg = val;
+    return RISCV_EXCP_NONE;
+}
+
+static RISCVException read_henvcfgh(CPURISCVState *env, int csrno,
+                                 target_ulong *val)
+{
+    *val = env->henvcfgh;
+    return RISCV_EXCP_NONE;
+}
+

target/riscv/csr.c static RISCVException write_mtval(CPURISCVState *env, int csrno,

Quoted 10 lines
target/riscv/csr.c +7 -0
+static RISCVException write_henvcfgh(CPURISCVState *env, int csrno,
+                                  target_ulong val)
+{
+    env->henvcfgh = val;
+    return RISCV_EXCP_NONE;
+}
+
  static RISCVException rmw_mip(CPURISCVState *env, int csrno,
                                target_ulong *ret_value,
                                target_ulong new_value, target_ulong

write_mask)

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

Quoted 2 lines
target/riscv/csr.c +0 -0
@@ -2054,6 +2125,19 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
      [CSR_MTVAL]    = { "mtval",    any,  read_mtval,    write_mtval

},

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

Quoted 1 line
target/riscv/csr.c +0 -0
      [CSR_MIP]      = { "mip",      any,  NULL,    NULL, rmw_mip

},

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

Quoted 3 lines
target/riscv/csr.c +2 -0
+    /* Execution environment configuration */
+    [CSR_MENVCFG]  = { "menvcfg",  any,     read_menvcfg,

write_menvcfg, NULL,

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

Quoted 1 line
target/riscv/csr.c +1 -0
+                                            NULL, NULL,

PRIV_VERSION_1_12_0},

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

Quoted 1 line
target/riscv/csr.c +1 -0
+    [CSR_MENVCFGH] = { "menvcfgh", any32,   read_menvcfgh,

write_menvcfgh, NULL,

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

Quoted 1 line
target/riscv/csr.c +1 -0
+                                            NULL, NULL,

PRIV_VERSION_1_12_0},

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

Quoted 1 line
target/riscv/csr.c +1 -0
+    [CSR_SENVCFG]  = { "senvcfg",  smode,   read_senvcfg,

write_senvcfg, NULL,

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

Quoted 1 line
target/riscv/csr.c +1 -0
+                                            NULL, NULL,

PRIV_VERSION_1_12_0},

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

Quoted 1 line
target/riscv/csr.c +1 -0
+    [CSR_HENVCFG]  = { "henvcfg",  hmode,   read_henvcfg,

write_henvcfg, NULL,

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

Quoted 1 line
target/riscv/csr.c +1 -0
+                                            NULL, NULL,
Quoted 9 lines

PRIV_VERSION_1_12_0},

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

Quoted 1 line
target/riscv/csr.c +1 -0
+    [CSR_HENVCFGH] = { "henvcfgh", hmode32, read_henvcfgh,

write_henvcfgh, NULL,

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

Quoted 1 line
target/riscv/csr.c +1 -0
+                                            NULL, NULL,

PRIV_VERSION_1_12_0},

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

Quoted 2 lines
target/riscv/csr.c +2 -0
+
+

Two new lines here.

Fixed it. Thanks.

Quoted 52 lines

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

Quoted 2 lines
target/riscv/csr.c +0 -0
      /* Supervisor Trap Setup */
      [CSR_SSTATUS]    = { "sstatus",    smode, read_sstatus,

write_sstatus, NULL,

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

Quoted 1 line
target/riscv/csr.c +0 -0
                                                read_sstatus_i128
           },
Quoted 37 lines
target/riscv/machine.c +25 -0
index 13b9ab375b95..59479a999b87 100644
@@ -185,6 +185,31 @@ static const VMStateDescription vmstate_rv128 = {
      }
  };
 
+/* TODO: henvcfg need both hyper_needed & envcfg_needed */
+static bool envcfg_needed(void *opaque)
+{
+    RISCVCPU *cpu = opaque;
+    CPURISCVState *env = &cpu->env;
+
+    return (env->priv_ver >= PRIV_VERSION_1_12_0 ? 1 : 0);
+}
+
+static const VMStateDescription vmstate_envcfg = {
+    .name = "cpu/envcfg",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .needed = envcfg_needed,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINTTL(env.menvcfg, RISCVCPU),
+        VMSTATE_UINTTL(env.menvcfgh, RISCVCPU),
+        VMSTATE_UINTTL(env.senvcfg, RISCVCPU),
+        VMSTATE_UINTTL(env.henvcfg, RISCVCPU),
+        VMSTATE_UINTTL(env.henvcfgh, RISCVCPU),
+
+        VMSTATE_END_OF_LIST()
+    }
+};
+
  const VMStateDescription vmstate_riscv_cpu = {
      .name = "cpu",
      .version_id = 3,

target/riscv/machine.c const VMStateDescription vmstate_riscv_cpu = { · line 271

Quoted 7 lines
target/riscv/machine.c +1 -0
@@ -240,6 +265,7 @@ const VMStateDescription vmstate_riscv_cpu = {
          &vmstate_vector,
          &vmstate_pointermasking,
          &vmstate_rv128,
+        &vmstate_envcfg,
          NULL
      }
  };

Regards,

Quoted 4 lines

Weiwei Li

--
Regards,
Atish

#14 lore raw

Re: [RFC 4/5] target/riscv: Add *envcfg* CSRs support

In reply to Atish Patra

Hi Atish,

Am Donnerstag, 20. Januar 2022, 21:07:34 CET schrieb Atish Patra:

Quoted 18 lines

The RISC-V privileged specification v1.12 defines few execution environment configuration CSRs that can be used enable/disable extensions per privilege levels.

Add the basic support for these CSRs.

---
 target/riscv/cpu.h      |  8 ++++
 target/riscv/cpu_bits.h | 31 +++++++++++++++
 target/riscv/csr.c      | 84 +++++++++++++++++++++++++++++++++++++++++
 target/riscv/machine.c  | 26 +++++++++++++
 4 files changed, 149 insertions(+)
target/riscv/cpu_bits.h +0 -0
index f6f90b5cbd52..afb237c2313b 100644

[...]

target/riscv/cpu_bits.h typedef enum RISCVException { · line 595

Quoted 8 lines
target/riscv/cpu_bits.h +3 -0
@@ -578,6 +589,26 @@ typedef enum RISCVException {
 #define PM_EXT_CLEAN    0x00000002ULL
 #define PM_EXT_DIRTY    0x00000003ULL
 
+/* Execution enviornment configuration bits */
+#define MENVCFG_FIOM                       (1 << 0)
 
+#define MENVCFG_CBE                        0x30000ULL

Looking both at the cmo spec as well as the most recent privileged spec (draft) the field is called CBIE it seems.

Also the shift looks wrong. Both cmo as well as privileged spec show it at bits [5:4] and _not_ [17:16].

Also wouldn't doing it like (_UL(3) << 4) be better to catch such things?

Quoted 19 lines
target/riscv/cpu_bits.h +17 -0
+#define MENVCFG_CBCFE                      (1 << 6)
+#define MENVCFG_CBZE                       (1 << 7)
+#define MENVCFG_PBMTE                      (1 << 62)
+#define MENVCFG_STCE                       (1 << 63)
+
+#define SENVCFG_FIOM                       MENVCFG_FIOM
+#define SENVCFG_CBE                        MENVCFG_CBE
+#define SENVCFG_CBCFE                      MENVCFG_CBCFE
+#define SENVCFG_CBZE                       MENVCFG_CBZE
+
+#define HENVCFG_FIOM                       MENVCFG_FIOM
+#define HENVCFG_CBE                        MENVCFG_CBE
+#define HENVCFG_CBCFE                      MENVCFG_CBCFE
+#define HENVCFG_CBZE                       MENVCFG_CBZE
+#define HENVCFG_PBMTE                      MENVCFG_PBMTE
+#define HENVCFG_STCE                       MENVCFG_STCE
+
 /* Offsets for every pair of control bits per each priv level */
 #define XS_OFFSET    0ULL
Quoted 1 line

#define U_OFFSET 2ULL

Heiko

#15 lore raw

Re: [RFC 4/5] target/riscv: Add *envcfg* CSRs support

Quoted 31 lines · Heiko Stübner

Hi Atish,

Am Donnerstag, 20. Januar 2022, 21:07:34 CET schrieb Atish Patra:

Quoted 18 lines

The RISC-V privileged specification v1.12 defines few execution environment configuration CSRs that can be used enable/disable extensions per privilege levels.

Add the basic support for these CSRs.

---
 target/riscv/cpu.h      |  8 ++++
 target/riscv/cpu_bits.h | 31 +++++++++++++++
 target/riscv/csr.c      | 84 +++++++++++++++++++++++++++++++++++++++++
 target/riscv/machine.c  | 26 +++++++++++++
 4 files changed, 149 insertions(+)
target/riscv/cpu_bits.h +0 -0
index f6f90b5cbd52..afb237c2313b 100644

[...]

Quoted 6 lines
target/riscv/cpu_bits.h +2 -0
@@ -578,6 +589,26 @@ typedef enum RISCVException {
 #define PM_EXT_CLEAN    0x00000002ULL
 #define PM_EXT_DIRTY    0x00000003ULL
 
+/* Execution enviornment configuration bits */
+#define MENVCFG_FIOM                       (1 << 0)
Quoted 9 lines

target/riscv/cpu_bits.h typedef enum RISCVException { · line 594

Quoted 1 line
target/riscv/cpu_bits.h +1 -0
+#define MENVCFG_CBE                        0x30000ULL

Looking both at the cmo spec as well as the most recent privileged spec (draft) the field is called CBIE it seems.

Also the shift looks wrong. Both cmo as well as privileged spec show it at bits [5:4] and _not_ [17:16].

This looks like a typo from my side. These bits are reserved in the spec! Apologies for such a silly mistake. Fixed it in v2.

Quoted 3 lines

Also wouldn't doing it like (_UL(3) << 4) be better to catch such things?

Of course.

Quoted 26 lines
Quoted 10 lines
target/riscv/cpu_bits.h +10 -0
+#define MENVCFG_CBCFE                      (1 << 6)
+#define MENVCFG_CBZE                       (1 << 7)
+#define MENVCFG_PBMTE                      (1 << 62)
+#define MENVCFG_STCE                       (1 << 63)
+
+#define SENVCFG_FIOM                       MENVCFG_FIOM
+#define SENVCFG_CBE                        MENVCFG_CBE
+#define SENVCFG_CBCFE                      MENVCFG_CBCFE
+#define SENVCFG_CBZE                       MENVCFG_CBZE
+

target/riscv/cpu_bits.h typedef enum RISCVException { · line 614

Quoted 10 lines
target/riscv/cpu_bits.h +7 -0
+#define HENVCFG_FIOM                       MENVCFG_FIOM
+#define HENVCFG_CBE                        MENVCFG_CBE
+#define HENVCFG_CBCFE                      MENVCFG_CBCFE
+#define HENVCFG_CBZE                       MENVCFG_CBZE
+#define HENVCFG_PBMTE                      MENVCFG_PBMTE
+#define HENVCFG_STCE                       MENVCFG_STCE
+
 /* Offsets for every pair of control bits per each priv level */
 #define XS_OFFSET    0ULL
 #define U_OFFSET     2ULL

Heiko

#16 lore raw

RFC 5/5 target/riscv: Enable privileged spec version 1.12

In reply to Atish Patra

Virt machine uses privileged specification version 1.12 now. All other machine continue to use the default one defined for that machine unless changed to 1.12 by the user explicitly.

---
 target/riscv/cpu.c |  8 +++++---
 target/riscv/csr.c | 10 ++++++++++
 2 files changed, 15 insertions(+), 3 deletions(-)
target/riscv/cpu.c +5 -3
index 9bc25d3055d4..cec5791151e7 100644
@@ -153,7 +153,7 @@ static void riscv_any_cpu_init(Object *obj)
 #elif defined(TARGET_RISCV64)
     set_misa(env, MXL_RV64, RVI | RVM | RVA | RVF | RVD | RVC | RVU);
 #endif
-    set_priv_version(env, PRIV_VERSION_1_11_0);
+    set_priv_version(env, PRIV_VERSION_1_12_0);
 }
 
 #if defined(TARGET_RISCV64)
@@ -439,7 +439,9 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)
     }
 
     if (cpu->cfg.priv_spec) {
-        if (!g_strcmp0(cpu->cfg.priv_spec, "v1.11.0")) {
+        if (!g_strcmp0(cpu->cfg.priv_spec, "v1.12.0")) {
+            priv_version = PRIV_VERSION_1_12_0;
+        } else if (!g_strcmp0(cpu->cfg.priv_spec, "v1.11.0")) {
             priv_version = PRIV_VERSION_1_11_0;
         } else if (!g_strcmp0(cpu->cfg.priv_spec, "v1.10.0")) {
             priv_version = PRIV_VERSION_1_10_0;
@@ -454,7 +456,7 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)
     if (priv_version) {
         set_priv_version(env, priv_version);
     } else if (!env->priv_ver) {
-        set_priv_version(env, PRIV_VERSION_1_11_0);
+        set_priv_version(env, PRIV_VERSION_1_12_0);
     }
 
     if (cpu->cfg.mmu) {
target/riscv/csr.c +10 -0
index a4bbae7a1bbd..62d429cc3f17 100644
@@ -1874,6 +1874,12 @@ static inline RISCVException riscv_csrrw_check(CPURISCVState *env,
     int read_only = get_field(csrno, 0xC00) == 3;
 #if !defined(CONFIG_USER_ONLY)
     int effective_priv = env->priv;
+    int csr_min_priv = csr_ops[csrno].min_priv_ver;
+
+    /* The default privilege specification version supported is 1.10 */
+    if (!csr_min_priv) {
+        csr_min_priv = PRIV_VERSION_1_10_0;
+    }
 
     if (riscv_has_ext(env, RVH) &&
         env->priv == PRV_S &&
@@ -1904,6 +1910,10 @@ static inline RISCVException riscv_csrrw_check(CPURISCVState *env,
         return RISCV_EXCP_ILLEGAL_INST;
     }
 
+    if (env->priv_ver < csr_min_priv) {
+        return RISCV_EXCP_ILLEGAL_INST;
+    }
+
     return csr_ops[csrno].predicate(env, csrno);
 }
 
Signature · 1 line
2.30.2
#17 lore raw

Re: [RFC 5/5] target/riscv: Enable privileged spec version 1.12

Quoted 64 lines · Atish Patra

Virt machine uses privileged specification version 1.12 now. All other machine continue to use the default one defined for that machine unless changed to 1.12 by the user explicitly.

---
 target/riscv/cpu.c |  8 +++++---
 target/riscv/csr.c | 10 ++++++++++
 2 files changed, 15 insertions(+), 3 deletions(-)
target/riscv/cpu.c +4 -2
index 9bc25d3055d4..cec5791151e7 100644
@@ -153,7 +153,7 @@ static void riscv_any_cpu_init(Object *obj)
 #elif defined(TARGET_RISCV64)
     set_misa(env, MXL_RV64, RVI | RVM | RVA | RVF | RVD | RVC | RVU);
 #endif
-    set_priv_version(env, PRIV_VERSION_1_11_0);
+    set_priv_version(env, PRIV_VERSION_1_12_0);
 }
 
 #if defined(TARGET_RISCV64)
@@ -439,7 +439,9 @@ static void riscv_cpu_realize(DeviceState *dev, Error
 **errp)
     }
 
     if (cpu->cfg.priv_spec) {
-        if (!g_strcmp0(cpu->cfg.priv_spec, "v1.11.0")) {
+        if (!g_strcmp0(cpu->cfg.priv_spec, "v1.12.0")) {
+            priv_version = PRIV_VERSION_1_12_0;
+        } else if (!g_strcmp0(cpu->cfg.priv_spec, "v1.11.0")) {
             priv_version = PRIV_VERSION_1_11_0;
         } else if (!g_strcmp0(cpu->cfg.priv_spec, "v1.10.0")) {
             priv_version = PRIV_VERSION_1_10_0;
target/riscv/cpu.c +1 -1
@@ -454,7 +456,7 @@ static void riscv_cpu_realize(DeviceState *dev, Error
 **errp)
     if (priv_version) {
         set_priv_version(env, priv_version);
     } else if (!env->priv_ver) {
-        set_priv_version(env, PRIV_VERSION_1_11_0);
+        set_priv_version(env, PRIV_VERSION_1_12_0);
     }
 
     if (cpu->cfg.mmu) {
target/riscv/csr.c +6 -0
index a4bbae7a1bbd..62d429cc3f17 100644
@@ -1874,6 +1874,12 @@ static inline RISCVException
 riscv_csrrw_check(CPURISCVState *env,
     int read_only = get_field(csrno, 0xC00) == 3;
 #if !defined(CONFIG_USER_ONLY)
     int effective_priv = env->priv;
+    int csr_min_priv = csr_ops[csrno].min_priv_ver;
+
+    /* The default privilege specification version supported is 1.10 */
+    if (!csr_min_priv) {
+        csr_min_priv = PRIV_VERSION_1_10_0;
+    }
 
     if (riscv_has_ext(env, RVH) &&
         env->priv == PRV_S &&

target/riscv/csr.c static inline RISCVException · line 1917

Quoted 8 lines
target/riscv/csr.c +3 -0
@@ -1904,6 +1910,10 @@ static inline RISCVException
 riscv_csrrw_check(CPURISCVState *env,
         return RISCV_EXCP_ILLEGAL_INST;
     }
 
+    if (env->priv_ver < csr_min_priv) {
+        return RISCV_EXCP_ILLEGAL_INST;
+    }
 

This does not compile if CONFIG_USER_ONLY is defined.

Quoted 8 lines
target/riscv/csr.c +1 -0
+
     return csr_ops[csrno].predicate(env, csrno);

}

--
2.30.2