Skip to contentSkip to conversation

support subsets of virtual memory extension

PATCH Superseded Weiwei Li 4 participants 15 messages Jan 25, 2022 – Jan 28, 2022 RISC-V

v6 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
author
#11 lore raw

PATCH v6 3/5 target/riscv: add support for svnapot extension

In reply to Weiwei Li

- add PTE_N bit
- add PTE_N bit check for inner PTE
- update address translation to support 64KiB continuous region (napot_bits = 4)

---
 target/riscv/cpu.c        |  2 ++
 target/riscv/cpu_bits.h   |  1 +
 target/riscv/cpu_helper.c | 17 ++++++++++++++---
 3 files changed, 17 insertions(+), 3 deletions(-)
target/riscv/cpu.c +2 -0
index 1cb0436187..8752fa1544 100644
@@ -729,6 +729,8 @@ static Property riscv_cpu_properties[] = {
     DEFINE_PROP_UINT16("vlen", RISCVCPU, cfg.vlen, 128),
     DEFINE_PROP_UINT16("elen", RISCVCPU, cfg.elen, 64),
 
+    DEFINE_PROP_BOOL("svnapot", RISCVCPU, cfg.ext_svnapot, false),
+
     DEFINE_PROP_BOOL("zba", RISCVCPU, cfg.ext_zba, true),
     DEFINE_PROP_BOOL("zbb", RISCVCPU, cfg.ext_zbb, true),
     DEFINE_PROP_BOOL("zbc", RISCVCPU, cfg.ext_zbc, true),
target/riscv/cpu_bits.h +1 -0
index 37b622fbfa..41190ce985 100644
@@ -489,6 +489,7 @@ typedef enum {
 #define PTE_A               0x040 /* Accessed */
 #define PTE_D               0x080 /* Dirty */
 #define PTE_SOFT            0x300 /* Reserved for Software */
+#define PTE_N               0x8000000000000000 /* NAPOT translation */
 
 /* Page table PPN shift amount */
 #define PTE_PPN_SHIFT       10
target/riscv/cpu_helper.c +14 -3
index a5bf07ccb6..ef7d9b07bd 100644
@@ -641,7 +641,7 @@ restart:
             return TRANSLATE_FAIL;
         } else if (!(pte & (PTE_R | PTE_W | PTE_X))) {
             /* Inner PTE, continue walking */
-            if (pte & (PTE_D | PTE_A | PTE_U)) {
+            if (pte & (target_ulong)(PTE_D | PTE_A | PTE_U | PTE_N)) {
                 return TRANSLATE_FAIL;
             }
             base = ppn << PGSHIFT;
@@ -717,8 +717,19 @@ restart:
             /* for superpage mappings, make a fake leaf PTE for the TLB's
                benefit. */
             target_ulong vpn = addr >> PGSHIFT;
-            *physical = ((ppn | (vpn & ((1L << ptshift) - 1))) << PGSHIFT) |
-                        (addr & ~TARGET_PAGE_MASK);
+
+            int napot_bits = 0;
+            if (cpu->cfg.ext_svnapot && (pte & (target_ulong)PTE_N)) {
+                napot_bits = ctzl(ppn) + 1;
+                if ((i != (levels - 1)) || (napot_bits != 4)) {
+                    return TRANSLATE_FAIL;
+                }
+            }
+
+            *physical = (((ppn & ~(((target_ulong)1 << napot_bits) - 1)) |
+                          (vpn & (((target_ulong)1 << napot_bits) - 1)) |
+                          (vpn & (((target_ulong)1 << ptshift) - 1))
+                        ) << PGSHIFT) | (addr & ~TARGET_PAGE_MASK);
 
             /* set permissions on the TLB entry */
             if ((pte & PTE_R) || ((pte & PTE_X) && mxr)) {
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH v6 1/5 target/riscv: Ignore reserved bits in PTE for RV64

In reply to Weiwei Li

From: Guo Ren <[email protected]>

Highest bits of PTE has been used for svpbmt, ref: [1], [2], so we need to ignore them. They cannot be a part of ppn.

1: The RISC-V Instruction Set Manual, Volume II: Privileged Architecture
4.4 Sv39: Page-Based 39-bit Virtual-Memory System
4.5 Sv48: Page-Based 48-bit Virtual-Memory System

2: https://github.com/riscv/virtual-memory/blob/main/specs/663-Svpbmt-diff.pdf

Cc Liu Zhiwei <[email protected]>
Cc Bin Meng <[email protected]>
Cc Alistair Francis <[email protected]>
---
 target/riscv/cpu.h        | 13 +++++++++++++
 target/riscv/cpu_bits.h   |  7 +++++++
 target/riscv/cpu_helper.c | 14 +++++++++++++-
 3 files changed, 33 insertions(+), 1 deletion(-)
target/riscv/cpu.h +13 -0
index 55635d68d5..45de8faaca 100644
@@ -341,6 +341,8 @@ struct RISCVCPU {
         bool ext_counters;
         bool ext_ifencei;
         bool ext_icsr;
+        bool ext_svnapot;
+        bool ext_svpbmt;
         bool ext_zfh;
         bool ext_zfhmin;
         bool ext_zve32f;
@@ -495,6 +497,17 @@ static inline int riscv_cpu_xlen(CPURISCVState *env)
     return 16 << env->xl;
 }
 
+#ifndef CONFIG_USER_ONLY
+#ifdef TARGET_RISCV32
+#define riscv_cpu_sxl(env)  ((void)(env), MXL_RV32)
+#else
+static inline RISCVMXL riscv_cpu_sxl(CPURISCVState *env)
+{
+    return get_field(env->mstatus, MSTATUS64_SXL);
+}
+#endif
+#endif
+
 /*
  * Encode LMUL to lmul as follows:
  *     LMUL    vlmul    lmul
target/riscv/cpu_bits.h +7 -0
index 7c87433645..37b622fbfa 100644
@@ -493,6 +493,13 @@ typedef enum {
 /* Page table PPN shift amount */
 #define PTE_PPN_SHIFT       10
 
+/* Page table PPN mask */
+#if defined(TARGET_RISCV32)
+#define PTE_PPN_MASK        0xFFFFFC00UL
+#elif defined(TARGET_RISCV64)
+#define PTE_PPN_MASK        0x3FFFFFFFFFFC00ULL
+#endif
+
 /* Leaf page shift amount */
 #define PGSHIFT             12
 
target/riscv/cpu_helper.c +13 -1
index 327a2c4f1d..2a921bedfd 100644
@@ -622,7 +622,19 @@ restart:
             return TRANSLATE_FAIL;
         }
 
-        hwaddr ppn = pte >> PTE_PPN_SHIFT;
+        hwaddr ppn;
+        RISCVCPU *cpu = env_archcpu(env);
+
+        if (riscv_cpu_sxl(env) == MXL_RV32) {
+            ppn = pte >> PTE_PPN_SHIFT;
+        } else if (cpu->cfg.ext_svpbmt || cpu->cfg.ext_svnapot) {
+            ppn = (pte & PTE_PPN_MASK) >> PTE_PPN_SHIFT;
+        } else {
+            ppn = pte >> PTE_PPN_SHIFT;
+            if ((pte & ~PTE_PPN_MASK) >> PTE_PPN_SHIFT) {
+                return TRANSLATE_FAIL;
+            }
+        }
 
         if (!(pte & PTE_V)) {
             /* Invalid PTE */
Signature · 1 line
2.17.1

Weiwei Li

author
#0 lore raw

v6 cover support subsets of virtual memory extension

This patchset implements virtual memory related RISC-V extensions: Svnapot version 1.0, Svinval vesion 1.0, Svpbmt version 1.0.

Specification:
https://github.com/riscv/virtual-memory/tree/main/specs

The port is available here:
https://github.com/plctlab/plct-qemu/tree/plct-virtmem-upstream-v6

To test this implementation, specify cpu argument with 'svinval=true,svnapot=true,svpbmt=true'.

This implementation can pass the riscv-tests for rv64ssvnapot.

v6:
* select ppn mask base on sxl

v5:
* merge patch https://lore.kernel.org/qemu-devel/[email protected]/
* relax pte attribute check

v4:
* fix encodings for hinval_vvma and hinval_gvma
* partition inner PTE check into several steps
* improve commit messages to describe changes

v3:
* drop "x-" in exposed properties

v2:
* add extension check for svnapot and svpbmt

Guo Ren (1):
target/riscv: Ignore reserved bits in PTE for RV64

Weiwei Li (4):
target/riscv: add PTE_A/PTE_D/PTE_U bits check for inner PTE target/riscv: add support for svnapot extension target/riscv: add support for svinval extension target/riscv: add support for svpbmt extension

 target/riscv/cpu.c                          |  4 ++
 target/riscv/cpu.h                          | 14 ++++
 target/riscv/cpu_bits.h                     | 10 +++
 target/riscv/cpu_helper.c                   | 34 +++++++++-
 target/riscv/insn32.decode                  |  7 ++
 target/riscv/insn_trans/trans_svinval.c.inc | 75 +++++++++++++++++++++
 target/riscv/translate.c                    |  1 +
 7 files changed, 142 insertions(+), 3 deletions(-)
 create mode 100644 target/riscv/insn_trans/trans_svinval.c.inc
Signature · 1 line
2.17.1
author
#8 lore raw

PATCH v6 2/5 target/riscv: add PTE_A/PTE_D/PTE_U bits check for inner PTE

---
 target/riscv/cpu_helper.c | 3 +++
 1 file changed, 3 insertions(+)
target/riscv/cpu_helper.c +3 -0
index 2a921bedfd..a5bf07ccb6 100644
@@ -641,6 +641,9 @@ restart:
             return TRANSLATE_FAIL;
         } else if (!(pte & (PTE_R | PTE_W | PTE_X))) {
             /* Inner PTE, continue walking */
+            if (pte & (PTE_D | PTE_A | PTE_U)) {
+                return TRANSLATE_FAIL;
+            }
             base = ppn << PGSHIFT;
         } else if ((pte & (PTE_R | PTE_W | PTE_X)) == PTE_W) {
             /* Reserved leaf PTE flags: PTE_W */
Signature · 1 line
2.17.1
author
#12 lore raw

PATCH v6 4/5 target/riscv: add support for svinval extension

In reply to Weiwei Li

- sinval.vma, hinval.vvma and hinval.gvma do the same as sfence.vma, hfence.vvma and hfence.gvma except extension check
- do nothing other than extension check for sfence.w.inval and sfence.inval.ir

---
 target/riscv/cpu.c                          |  1 +
 target/riscv/cpu.h                          |  1 +
 target/riscv/insn32.decode                  |  7 ++
 target/riscv/insn_trans/trans_svinval.c.inc | 75 +++++++++++++++++++++
 target/riscv/translate.c                    |  1 +
 5 files changed, 85 insertions(+)
 create mode 100644 target/riscv/insn_trans/trans_svinval.c.inc
target/riscv/cpu.c +1 -0
index 8752fa1544..4efdc16780 100644
@@ -729,6 +729,7 @@ static Property riscv_cpu_properties[] = {
     DEFINE_PROP_UINT16("vlen", RISCVCPU, cfg.vlen, 128),
     DEFINE_PROP_UINT16("elen", RISCVCPU, cfg.elen, 64),
 
+    DEFINE_PROP_BOOL("svinval", RISCVCPU, cfg.ext_svinval, false),
     DEFINE_PROP_BOOL("svnapot", RISCVCPU, cfg.ext_svnapot, false),
 
     DEFINE_PROP_BOOL("zba", RISCVCPU, cfg.ext_zba, true),
target/riscv/cpu.h +1 -0
index 45de8faaca..2efe987248 100644
@@ -341,6 +341,7 @@ struct RISCVCPU {
         bool ext_counters;
         bool ext_ifencei;
         bool ext_icsr;
+        bool ext_svinval;
         bool ext_svnapot;
         bool ext_svpbmt;
         bool ext_zfh;
target/riscv/insn32.decode +7 -0
index 5bbedc254c..1d3ff1efe1 100644
@@ -809,3 +809,10 @@ fcvt_l_h   1100010  00010 ..... ... ..... 1010011 @r2_rm
 fcvt_lu_h  1100010  00011 ..... ... ..... 1010011 @r2_rm
 fcvt_h_l   1101010  00010 ..... ... ..... 1010011 @r2_rm
 fcvt_h_lu  1101010  00011 ..... ... ..... 1010011 @r2_rm
+
+# *** Svinval Standard Extension ***
+sinval_vma        0001011 ..... ..... 000 00000 1110011 @sfence_vma
+sfence_w_inval    0001100 00000 00000 000 00000 1110011
+sfence_inval_ir   0001100 00001 00000 000 00000 1110011
+hinval_vvma       0010011 ..... ..... 000 00000 1110011 @hfence_vvma
+hinval_gvma       0110011 ..... ..... 000 00000 1110011 @hfence_gvma
target/riscv/insn_trans/trans_svinval.c.inc new file +75 -0

75 lines not on this page · raw message

target/riscv/translate.c +1 -0
index f0bbe80875..cbf3b43348 100644
@@ -855,6 +855,7 @@ static uint32_t opcode_at(DisasContextBase *dcbase, target_ulong pc)
 #include "insn_trans/trans_rvb.c.inc"
 #include "insn_trans/trans_rvzfh.c.inc"
 #include "insn_trans/trans_privileged.c.inc"
+#include "insn_trans/trans_svinval.c.inc"
 
 /* Include the auto-generated decoder for 16 bit insn */
 #include "decode-insn16.c.inc"
Signature · 1 line
2.17.1
author
#13 lore raw

PATCH v6 5/5 target/riscv: add support for svpbmt extension

In reply to Weiwei Li

- add PTE_PBMT bits: It uses two PTE bits, but otherwise has no effect on QEMU, since QEMU is sequentially consistent and doesn't model PMAs currently
- add PTE_PBMT bit check for inner PTE

---
 target/riscv/cpu.c        | 1 +
 target/riscv/cpu_bits.h   | 2 ++
 target/riscv/cpu_helper.c | 4 +++-
 3 files changed, 6 insertions(+), 1 deletion(-)
target/riscv/cpu.c +1 -0
index 4efdc16780..44c8229d3a 100644
@@ -731,6 +731,7 @@ static Property riscv_cpu_properties[] = {
 
     DEFINE_PROP_BOOL("svinval", RISCVCPU, cfg.ext_svinval, false),
     DEFINE_PROP_BOOL("svnapot", RISCVCPU, cfg.ext_svnapot, false),
+    DEFINE_PROP_BOOL("svpbmt", RISCVCPU, cfg.ext_svpbmt, false),
 
     DEFINE_PROP_BOOL("zba", RISCVCPU, cfg.ext_zba, true),
     DEFINE_PROP_BOOL("zbb", RISCVCPU, cfg.ext_zbb, true),
target/riscv/cpu_bits.h +2 -0
index 41190ce985..e3d0425f7f 100644
@@ -489,7 +489,9 @@ typedef enum {
 #define PTE_A               0x040 /* Accessed */
 #define PTE_D               0x080 /* Dirty */
 #define PTE_SOFT            0x300 /* Reserved for Software */
+#define PTE_PBMT            0x6000000000000000 /* Page-based memory types */
 #define PTE_N               0x8000000000000000 /* NAPOT translation */
+#define PTE_ATTR            (PTE_N | PTE_PBMT) /* All attributes bits */
 
 /* Page table PPN shift amount */
 #define PTE_PPN_SHIFT       10
target/riscv/cpu_helper.c +3 -1
index ef7d9b07bd..22ac2504ff 100644
@@ -639,9 +639,11 @@ restart:
         if (!(pte & PTE_V)) {
             /* Invalid PTE */
             return TRANSLATE_FAIL;
+        } else if (!cpu->cfg.ext_svpbmt && (pte & (target_ulong)PTE_PBMT)) {
+            return TRANSLATE_FAIL;
         } else if (!(pte & (PTE_R | PTE_W | PTE_X))) {
             /* Inner PTE, continue walking */
-            if (pte & (target_ulong)(PTE_D | PTE_A | PTE_U | PTE_N)) {
+            if (pte & (target_ulong)(PTE_D | PTE_A | PTE_U | PTE_ATTR)) {
                 return TRANSLATE_FAIL;
             }
             base = ppn << PGSHIFT;
Signature · 1 line
2.17.1
Reviewed-by on v6 1/5
#2 lore raw

Re: [PATCH v6 1/5] target/riscv: Ignore reserved bits in PTE for RV64

In reply to Weiwei Li
Quoted 39 lines · Weiwei Li

From: Guo Ren <[email protected]>

Highest bits of PTE has been used for svpbmt, ref: [1], [2], so we need to ignore them. They cannot be a part of ppn.

1: The RISC-V Instruction Set Manual, Volume II: Privileged Architecture
4.4 Sv39: Page-Based 39-bit Virtual-Memory System
4.5 Sv48: Page-Based 48-bit Virtual-Memory System

2: https://github.com/riscv/virtual-memory/blob/main/specs/663-Svpbmt-diff.pdf

Cc Liu Zhiwei <[email protected]>
Cc Bin Meng <[email protected]>
Cc Alistair Francis <[email protected]>
---
  target/riscv/cpu.h        | 13 +++++++++++++
  target/riscv/cpu_bits.h   |  7 +++++++
  target/riscv/cpu_helper.c | 14 +++++++++++++-
  3 files changed, 33 insertions(+), 1 deletion(-)
target/riscv/cpu.h +3 -0
index 55635d68d5..45de8faaca 100644
@@ -341,6 +341,8 @@ struct RISCVCPU {
          bool ext_counters;
          bool ext_ifencei;
          bool ext_icsr;
+        bool ext_svnapot;
+        bool ext_svpbmt;
          bool ext_zfh;
          bool ext_zfhmin;
          bool ext_zve32f;
@@ -495,6 +497,17 @@ static inline int riscv_cpu_xlen(CPURISCVState *env)
      return 16 << env->xl;
  }
 
+#ifndef CONFIG_USER_ONLY

target/riscv/cpu.h static inline int riscv_cpu_xlen(CPURISCVState *env) · line 510

Quoted 10 lines
target/riscv/cpu.h +10 -0
+#ifdef TARGET_RISCV32
+#define riscv_cpu_sxl(env)  ((void)(env), MXL_RV32)
+#else
+static inline RISCVMXL riscv_cpu_sxl(CPURISCVState *env)
+{
+    return get_field(env->mstatus, MSTATUS64_SXL);
+}
+#endif
+#endif
+

Perhaps an interface also works for user mode is better.

+#ifdef TARGET_RISCV32
+#define riscv_cpu_sxl(env) ((void)(env), MXL_RV32) +#else
+static inline RISCVMXL riscv_cpu_sxl(CPURISCVState *env) +{
+#ifdef CONFIG_USER_ONLY
+ return env->misa_mxl;
+#else
+ return get_field(env->mstatus, MSTATUS64_SXL);
+#endif
+}
+#endif
+

Quoted 8 lines
target/riscv/cpu.h +0 -0
  /*
   * Encode LMUL to lmul as follows:
   *     LMUL    vlmul    lmul
target/riscv/cpu_bits.h +0 -0
index 7c87433645..37b622fbfa 100644

target/riscv/cpu_bits.h typedef enum { · line 502

Quoted 10 lines
target/riscv/cpu_bits.h +7 -0
@@ -493,6 +493,13 @@ typedef enum {
  /* Page table PPN shift amount */
  #define PTE_PPN_SHIFT       10
 
+/* Page table PPN mask */
+#if defined(TARGET_RISCV32)
+#define PTE_PPN_MASK        0xFFFFFC00UL
+#elif defined(TARGET_RISCV64)
+#define PTE_PPN_MASK        0x3FFFFFFFFFFC00ULL
+#endif
+

No need to define PTE_PPN_MASK for TARGET_RISCV32.

Quoted 18 lines
target/riscv/cpu_bits.h +0 -0
  /* Leaf page shift amount */
  #define PGSHIFT             12
 
target/riscv/cpu_helper.c +6 -1
index 327a2c4f1d..2a921bedfd 100644
@@ -622,7 +622,19 @@ restart:
              return TRANSLATE_FAIL;
          }
 
-        hwaddr ppn = pte >> PTE_PPN_SHIFT;
+        hwaddr ppn;
+        RISCVCPU *cpu = env_archcpu(env);
+
+        if (riscv_cpu_sxl(env) == MXL_RV32) {
+            ppn = pte >> PTE_PPN_SHIFT;
+        } else if (cpu->cfg.ext_svpbmt || cpu->cfg.ext_svnapot) {

target/riscv/cpu_helper.c restart: · line 640

Quoted 10 lines
target/riscv/cpu_helper.c +7 -0
+            ppn = (pte & PTE_PPN_MASK) >> PTE_PPN_SHIFT;
+        } else {
+            ppn = pte >> PTE_PPN_SHIFT;
+            if ((pte & ~PTE_PPN_MASK) >> PTE_PPN_SHIFT) {
+                return TRANSLATE_FAIL;
+            }
+        }
 
          if (!(pte & PTE_V)) {
              /* Invalid PTE */

Otherwise,

Thanks,
Zhiwei

#3 lore raw

Re: [PATCH v6 1/5] target/riscv: Ignore reserved bits in PTE for RV64

Quoted 80 lines · LIU Zhiwei
Quoted 39 lines · Weiwei Li

From: Guo Ren <[email protected]>

Highest bits of PTE has been used for svpbmt, ref: [1], [2], so we need to ignore them. They cannot be a part of ppn.

1: The RISC-V Instruction Set Manual, Volume II: Privileged Architecture
4.4 Sv39: Page-Based 39-bit Virtual-Memory System
4.5 Sv48: Page-Based 48-bit Virtual-Memory System

2: https://github.com/riscv/virtual-memory/blob/main/specs/663-Svpbmt-diff.pdf

Cc Liu Zhiwei <[email protected]>
Cc Bin Meng <[email protected]>
Cc Alistair Francis <[email protected]>
---
  target/riscv/cpu.h        | 13 +++++++++++++
  target/riscv/cpu_bits.h   |  7 +++++++
  target/riscv/cpu_helper.c | 14 +++++++++++++-
  3 files changed, 33 insertions(+), 1 deletion(-)
target/riscv/cpu.h +3 -0
index 55635d68d5..45de8faaca 100644
@@ -341,6 +341,8 @@ struct RISCVCPU {
          bool ext_counters;
          bool ext_ifencei;
          bool ext_icsr;
+        bool ext_svnapot;
+        bool ext_svpbmt;
          bool ext_zfh;
          bool ext_zfhmin;
          bool ext_zve32f;
@@ -495,6 +497,17 @@ static inline int riscv_cpu_xlen(CPURISCVState *env)
      return 16 << env->xl;
  }
 
+#ifndef CONFIG_USER_ONLY

target/riscv/cpu.h static inline int riscv_cpu_xlen(CPURISCVState *env) · line 510

Quoted 10 lines
target/riscv/cpu.h +10 -0
+#ifdef TARGET_RISCV32
+#define riscv_cpu_sxl(env)  ((void)(env), MXL_RV32)
+#else
+static inline RISCVMXL riscv_cpu_sxl(CPURISCVState *env)
+{
+    return get_field(env->mstatus, MSTATUS64_SXL);
+}
+#endif
+#endif
+

Perhaps an interface also works for user mode is better.

target/riscv/cpu.h +13 -0
+#ifdef TARGET_RISCV32
+#define riscv_cpu_sxl(env)  ((void)(env), MXL_RV32)
+#else
+static inline RISCVMXL riscv_cpu_sxl(CPURISCVState *env)
+{
+#ifdef CONFIG_USER_ONLY
+    return env->misa_mxl;
+#else
+    return get_field(env->mstatus, MSTATUS64_SXL);
+#endif
+}
+#endif
+
Quoted 12 lines
target/riscv/cpu.h +0 -0
  /*
   * Encode LMUL to lmul as follows:
   *     LMUL    vlmul    lmul
target/riscv/cpu_bits.h +0 -0
index 7c87433645..37b622fbfa 100644
@@ -493,6 +493,13 @@ typedef enum {
  /* Page table PPN shift amount */
  #define PTE_PPN_SHIFT       10
 
Quoted 9 lines

target/riscv/cpu_bits.h typedef enum { · line 502

Quoted 7 lines
target/riscv/cpu_bits.h +7 -0
+/* Page table PPN mask */
+#if defined(TARGET_RISCV32)
+#define PTE_PPN_MASK        0xFFFFFC00UL
+#elif defined(TARGET_RISCV64)
+#define PTE_PPN_MASK        0x3FFFFFFFFFFC00ULL
+#endif
+

No need to define PTE_PPN_MASK for TARGET_RISCV32.

ppn = (pte & PTE_PPN_MASK) >> PTE_PPN_SHIFT;

pte is target_ulong, so types are different.

TARGET_RISCV32: is 32bit.
TARGET_RISCV64: is 64bit.

Quoted 39 lines
Quoted 19 lines
target/riscv/cpu_bits.h +0 -0
  /* Leaf page shift amount */
  #define PGSHIFT             12
 
target/riscv/cpu_helper.c +6 -1
index 327a2c4f1d..2a921bedfd 100644
@@ -622,7 +622,19 @@ restart:
              return TRANSLATE_FAIL;
          }
 
-        hwaddr ppn = pte >> PTE_PPN_SHIFT;
+        hwaddr ppn;
+        RISCVCPU *cpu = env_archcpu(env);
+
+        if (riscv_cpu_sxl(env) == MXL_RV32) {
+            ppn = pte >> PTE_PPN_SHIFT;
+        } else if (cpu->cfg.ext_svpbmt || cpu->cfg.ext_svnapot) {

target/riscv/cpu_helper.c restart: · line 640

Quoted 10 lines
target/riscv/cpu_helper.c +7 -0
+            ppn = (pte & PTE_PPN_MASK) >> PTE_PPN_SHIFT;
+        } else {
+            ppn = pte >> PTE_PPN_SHIFT;
+            if ((pte & ~PTE_PPN_MASK) >> PTE_PPN_SHIFT) {
+                return TRANSLATE_FAIL;
+            }
+        }
 
          if (!(pte & PTE_V)) {
              /* Invalid PTE */

Otherwise,

Thanks,
Zhiwei

Signature · 4 lines
Best Regards
 Guo Ren

ML: https://lore.kernel.org/linux-csky/
Tested-by on the v6 series
#14 lore raw
In reply to Weiwei Li
Quoted 50 lines · Weiwei Li

This patchset implements virtual memory related RISC-V extensions: Svnapot version 1.0, Svinval vesion 1.0, Svpbmt version 1.0.

Specification:
https://github.com/riscv/virtual-memory/tree/main/specs

The port is available here:
https://github.com/plctlab/plct-qemu/tree/plct-virtmem-upstream-v6

To test this implementation, specify cpu argument with 'svinval=true,svnapot=true,svpbmt=true'.

This implementation can pass the riscv-tests for rv64ssvnapot.

v6:
* select ppn mask base on sxl

v5:
* merge patch https://lore.kernel.org/qemu-devel/[email protected]/
* relax pte attribute check

v4:
* fix encodings for hinval_vvma and hinval_gvma
* partition inner PTE check into several steps
* improve commit messages to describe changes

v3:
* drop "x-" in exposed properties

v2:
* add extension check for svnapot and svpbmt

Guo Ren (1):
target/riscv: Ignore reserved bits in PTE for RV64

Weiwei Li (4):
target/riscv: add PTE_A/PTE_D/PTE_U bits check for inner PTE target/riscv: add support for svnapot extension target/riscv: add support for svinval extension target/riscv: add support for svpbmt extension

 target/riscv/cpu.c                          |  4 ++
 target/riscv/cpu.h                          | 14 ++++
 target/riscv/cpu_bits.h                     | 10 +++
 target/riscv/cpu_helper.c                   | 34 +++++++++-
 target/riscv/insn32.decode                  |  7 ++
 target/riscv/insn_trans/trans_svinval.c.inc | 75 +++++++++++++++++++++
 target/riscv/translate.c                    |  1 +
 7 files changed, 142 insertions(+), 3 deletions(-)
 create mode 100644 target/riscv/insn_trans/trans_svinval.c.inc
Quoted 4 lines

--
2.17.1

Signature · 4 lines
Best Regards
 Guo Ren

ML: https://lore.kernel.org/linux-csky/
#4 lore raw

Re: [PATCH v6 1/5] target/riscv: Ignore reserved bits in PTE for RV64

In reply to Guo Ren
Quoted 87 lines · Guo Ren
Quoted 78 lines · LIU Zhiwei
Quoted 39 lines · Weiwei Li

From: Guo Ren <[email protected]>

Highest bits of PTE has been used for svpbmt, ref: [1], [2], so we need to ignore them. They cannot be a part of ppn.

1: The RISC-V Instruction Set Manual, Volume II: Privileged Architecture
4.4 Sv39: Page-Based 39-bit Virtual-Memory System
4.5 Sv48: Page-Based 48-bit Virtual-Memory System

2: https://github.com/riscv/virtual-memory/blob/main/specs/663-Svpbmt-diff.pdf

Cc Liu Zhiwei <[email protected]>
Cc Bin Meng <[email protected]>
Cc Alistair Francis <[email protected]>
---
   target/riscv/cpu.h        | 13 +++++++++++++
   target/riscv/cpu_bits.h   |  7 +++++++
   target/riscv/cpu_helper.c | 14 +++++++++++++-
   3 files changed, 33 insertions(+), 1 deletion(-)
target/riscv/cpu.h +3 -0
index 55635d68d5..45de8faaca 100644
@@ -341,6 +341,8 @@ struct RISCVCPU {
           bool ext_counters;
           bool ext_ifencei;
           bool ext_icsr;
+        bool ext_svnapot;
+        bool ext_svpbmt;
           bool ext_zfh;
           bool ext_zfhmin;
           bool ext_zve32f;
@@ -495,6 +497,17 @@ static inline int riscv_cpu_xlen(CPURISCVState *env)
       return 16 << env->xl;
   }
 
+#ifndef CONFIG_USER_ONLY

target/riscv/cpu.h static inline int riscv_cpu_xlen(CPURISCVState *env) · line 510

Quoted 10 lines
target/riscv/cpu.h +10 -0
+#ifdef TARGET_RISCV32
+#define riscv_cpu_sxl(env)  ((void)(env), MXL_RV32)
+#else
+static inline RISCVMXL riscv_cpu_sxl(CPURISCVState *env)
+{
+    return get_field(env->mstatus, MSTATUS64_SXL);
+}
+#endif
+#endif
+

Perhaps an interface also works for user mode is better.

target/riscv/cpu.h +13 -0
+#ifdef TARGET_RISCV32
+#define riscv_cpu_sxl(env)  ((void)(env), MXL_RV32)
+#else
+static inline RISCVMXL riscv_cpu_sxl(CPURISCVState *env)
+{
+#ifdef CONFIG_USER_ONLY
+    return env->misa_mxl;
+#else
+    return get_field(env->mstatus, MSTATUS64_SXL);
+#endif
+}
+#endif
+
Quoted 12 lines
target/riscv/cpu.h +0 -0
   /*
    * Encode LMUL to lmul as follows:
    *     LMUL    vlmul    lmul
target/riscv/cpu_bits.h +0 -0
index 7c87433645..37b622fbfa 100644
@@ -493,6 +493,13 @@ typedef enum {
   /* Page table PPN shift amount */
   #define PTE_PPN_SHIFT       10
 
Quoted 8 lines

target/riscv/cpu_bits.h typedef enum { · line 502

Quoted 7 lines
target/riscv/cpu_bits.h +7 -0
+/* Page table PPN mask */
+#if defined(TARGET_RISCV32)
+#define PTE_PPN_MASK        0xFFFFFC00UL
+#elif defined(TARGET_RISCV64)
+#define PTE_PPN_MASK        0x3FFFFFFFFFFC00ULL
+#endif
+

No need to define PTE_PPN_MASK for TARGET_RISCV32.

Quoted 7 lines

ppn = (pte & PTE_PPN_MASK) >> PTE_PPN_SHIFT;

pte is target_ulong, so types are different.

TARGET_RISCV32: is 32bit.
TARGET_RISCV64: is 64bit.

I should make it more clear.  You will not use PTE_PPN_MASK on TARGET_RISCV32.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
target/riscv/cpu_bits.h +0 -0
   /* Leaf page shift amount */
   #define PGSHIFT             12
 
target/riscv/cpu_helper.c +0 -0
index 327a2c4f1d..2a921bedfd 100644
@@ -622,7 +622,19 @@ restart:
               return TRANSLATE_FAIL;
           }
 
Quoted 6 lines
Quoted 6 lines

target/riscv/cpu_helper.c restart: · line 629

Quoted 6 lines
target/riscv/cpu_helper.c +5 -1
-        hwaddr ppn = pte >> PTE_PPN_SHIFT;
+        hwaddr ppn;
+        RISCVCPU *cpu = env_archcpu(env);
+
+        if (riscv_cpu_sxl(env) == MXL_RV32) {
+            ppn = pte >> PTE_PPN_SHIFT;

TARGET_RISCV32 will always come here. So no need to define PTE_PPN_MASK for TARGET_RISCV32.

Thanks,
Zhiwei

Quoted 21 lines
Quoted 20 lines

target/riscv/cpu_helper.c restart: · line 640

Quoted 11 lines
target/riscv/cpu_helper.c +8 -0
+        } else if (cpu->cfg.ext_svpbmt || cpu->cfg.ext_svnapot) {
+            ppn = (pte & PTE_PPN_MASK) >> PTE_PPN_SHIFT;
+        } else {
+            ppn = pte >> PTE_PPN_SHIFT;
+            if ((pte & ~PTE_PPN_MASK) >> PTE_PPN_SHIFT) {
+                return TRANSLATE_FAIL;
+            }
+        }
 
           if (!(pte & PTE_V)) {
               /* Invalid PTE */

Otherwise,

Thanks,
Zhiwei

#5 lore raw

Re: [PATCH v6 1/5] target/riscv: Ignore reserved bits in PTE for RV64

Quoted 110 lines · LIU Zhiwei
Quoted 87 lines · Guo Ren
Quoted 78 lines · LIU Zhiwei
Quoted 39 lines · Weiwei Li

From: Guo Ren <[email protected]>

Highest bits of PTE has been used for svpbmt, ref: [1], [2], so we need to ignore them. They cannot be a part of ppn.

1: The RISC-V Instruction Set Manual, Volume II: Privileged Architecture
4.4 Sv39: Page-Based 39-bit Virtual-Memory System
4.5 Sv48: Page-Based 48-bit Virtual-Memory System

2: https://github.com/riscv/virtual-memory/blob/main/specs/663-Svpbmt-diff.pdf

Cc Liu Zhiwei <[email protected]>
Cc Bin Meng <[email protected]>
Cc Alistair Francis <[email protected]>
---
   target/riscv/cpu.h        | 13 +++++++++++++
   target/riscv/cpu_bits.h   |  7 +++++++
   target/riscv/cpu_helper.c | 14 +++++++++++++-
   3 files changed, 33 insertions(+), 1 deletion(-)
target/riscv/cpu.h +3 -0
index 55635d68d5..45de8faaca 100644
@@ -341,6 +341,8 @@ struct RISCVCPU {
           bool ext_counters;
           bool ext_ifencei;
           bool ext_icsr;
+        bool ext_svnapot;
+        bool ext_svpbmt;
           bool ext_zfh;
           bool ext_zfhmin;
           bool ext_zve32f;
@@ -495,6 +497,17 @@ static inline int riscv_cpu_xlen(CPURISCVState *env)
       return 16 << env->xl;
   }
 
+#ifndef CONFIG_USER_ONLY

target/riscv/cpu.h static inline int riscv_cpu_xlen(CPURISCVState *env) · line 510

Quoted 10 lines
target/riscv/cpu.h +10 -0
+#ifdef TARGET_RISCV32
+#define riscv_cpu_sxl(env)  ((void)(env), MXL_RV32)
+#else
+static inline RISCVMXL riscv_cpu_sxl(CPURISCVState *env)
+{
+    return get_field(env->mstatus, MSTATUS64_SXL);
+}
+#endif
+#endif
+

Perhaps an interface also works for user mode is better.

target/riscv/cpu.h +13 -0
+#ifdef TARGET_RISCV32
+#define riscv_cpu_sxl(env)  ((void)(env), MXL_RV32)
+#else
+static inline RISCVMXL riscv_cpu_sxl(CPURISCVState *env)
+{
+#ifdef CONFIG_USER_ONLY
+    return env->misa_mxl;
+#else
+    return get_field(env->mstatus, MSTATUS64_SXL);
+#endif
+}
+#endif
+
Quoted 12 lines
target/riscv/cpu.h +0 -0
   /*
    * Encode LMUL to lmul as follows:
    *     LMUL    vlmul    lmul
target/riscv/cpu_bits.h +0 -0
index 7c87433645..37b622fbfa 100644
@@ -493,6 +493,13 @@ typedef enum {
   /* Page table PPN shift amount */
   #define PTE_PPN_SHIFT       10
 
Quoted 8 lines

target/riscv/cpu_bits.h typedef enum { · line 502

Quoted 7 lines
target/riscv/cpu_bits.h +7 -0
+/* Page table PPN mask */
+#if defined(TARGET_RISCV32)
+#define PTE_PPN_MASK        0xFFFFFC00UL
+#elif defined(TARGET_RISCV64)
+#define PTE_PPN_MASK        0x3FFFFFFFFFFC00ULL
+#endif
+

No need to define PTE_PPN_MASK for TARGET_RISCV32.

Quoted 7 lines

ppn = (pte & PTE_PPN_MASK) >> PTE_PPN_SHIFT;

pte is target_ulong, so types are different.

TARGET_RISCV32: is 32bit.
TARGET_RISCV64: is 64bit.

I should make it more clear. You will not use PTE_PPN_MASK on TARGET_RISCV32.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
target/riscv/cpu_bits.h +0 -0
   /* Leaf page shift amount */
   #define PGSHIFT             12
 
target/riscv/cpu_helper.c +0 -0
index 327a2c4f1d..2a921bedfd 100644
@@ -622,7 +622,19 @@ restart:
               return TRANSLATE_FAIL;
           }
 
Quoted 9 lines
Quoted 6 lines
Quoted 6 lines

target/riscv/cpu_helper.c restart: · line 629

Quoted 6 lines
target/riscv/cpu_helper.c +5 -1
-        hwaddr ppn = pte >> PTE_PPN_SHIFT;
+        hwaddr ppn;
+        RISCVCPU *cpu = env_archcpu(env);
+
+        if (riscv_cpu_sxl(env) == MXL_RV32) {
+            ppn = pte >> PTE_PPN_SHIFT;

TARGET_RISCV32 will always come here. So no need to define PTE_PPN_MASK for TARGET_RISCV32.

Oops, maybe we should use TARGET_LONG_SIZE == 4

#if TARGET_LONG_SIZE == 4
typedef int32_t target_long;
typedef uint32_t target_ulong;
#define TARGET_FMT_lx "%08x"
#define TARGET_FMT_ld "%d"
#define TARGET_FMT_lu "%u"
#elif TARGET_LONG_SIZE == 8
typedef int64_t target_long;
typedef uint64_t target_ulong;
#define TARGET_FMT_lx "%016" PRIx64
#define TARGET_FMT_ld "%" PRId64
#define TARGET_FMT_lu "%" PRIu64
#else
#error TARGET_LONG_SIZE undefined
#endif

Quoted 25 lines

Thanks,
Zhiwei

Quoted 21 lines
Quoted 20 lines

target/riscv/cpu_helper.c restart: · line 640

Quoted 11 lines
target/riscv/cpu_helper.c +8 -0
+        } else if (cpu->cfg.ext_svpbmt || cpu->cfg.ext_svnapot) {
+            ppn = (pte & PTE_PPN_MASK) >> PTE_PPN_SHIFT;
+        } else {
+            ppn = pte >> PTE_PPN_SHIFT;
+            if ((pte & ~PTE_PPN_MASK) >> PTE_PPN_SHIFT) {
+                return TRANSLATE_FAIL;
+            }
+        }
 
           if (!(pte & PTE_V)) {
               /* Invalid PTE */

Otherwise,

Thanks,
Zhiwei

Signature · 4 lines
Best Regards
 Guo Ren

ML: https://lore.kernel.org/linux-csky/
author
#6 lore raw

Re: [PATCH v6 1/5] target/riscv: Ignore reserved bits in PTE for RV64

Quoted 119 lines · Guo Ren
Quoted 108 lines · LIU Zhiwei
Quoted 86 lines · Guo Ren
Quoted 77 lines · LIU Zhiwei
Quoted 39 lines · Weiwei Li

From: Guo Ren <[email protected]>

Highest bits of PTE has been used for svpbmt, ref: [1], [2], so we need to ignore them. They cannot be a part of ppn.

1: The RISC-V Instruction Set Manual, Volume II: Privileged Architecture
4.4 Sv39: Page-Based 39-bit Virtual-Memory System
4.5 Sv48: Page-Based 48-bit Virtual-Memory System

2: https://github.com/riscv/virtual-memory/blob/main/specs/663-Svpbmt-diff.pdf

Cc Liu Zhiwei <[email protected]>
Cc Bin Meng <[email protected]>
Cc Alistair Francis <[email protected]>
---
    target/riscv/cpu.h        | 13 +++++++++++++
    target/riscv/cpu_bits.h   |  7 +++++++
    target/riscv/cpu_helper.c | 14 +++++++++++++-
    3 files changed, 33 insertions(+), 1 deletion(-)
target/riscv/cpu.h +3 -0
index 55635d68d5..45de8faaca 100644
@@ -341,6 +341,8 @@ struct RISCVCPU {
            bool ext_counters;
            bool ext_ifencei;
            bool ext_icsr;
+        bool ext_svnapot;
+        bool ext_svpbmt;
            bool ext_zfh;
            bool ext_zfhmin;
            bool ext_zve32f;
@@ -495,6 +497,17 @@ static inline int riscv_cpu_xlen(CPURISCVState *env)
        return 16 << env->xl;
    }
 
+#ifndef CONFIG_USER_ONLY

target/riscv/cpu.h static inline int riscv_cpu_xlen(CPURISCVState *env) · line 510

Quoted 10 lines
target/riscv/cpu.h +10 -0
+#ifdef TARGET_RISCV32
+#define riscv_cpu_sxl(env)  ((void)(env), MXL_RV32)
+#else
+static inline RISCVMXL riscv_cpu_sxl(CPURISCVState *env)
+{
+    return get_field(env->mstatus, MSTATUS64_SXL);
+}
+#endif
+#endif
+

Perhaps an interface also works for user mode is better.

target/riscv/cpu.h +13 -0
+#ifdef TARGET_RISCV32
+#define riscv_cpu_sxl(env)  ((void)(env), MXL_RV32)
+#else
+static inline RISCVMXL riscv_cpu_sxl(CPURISCVState *env)
+{
+#ifdef CONFIG_USER_ONLY
+    return env->misa_mxl;
+#else
+    return get_field(env->mstatus, MSTATUS64_SXL);
+#endif
+}
+#endif
+
Quoted 12 lines
target/riscv/cpu.h +0 -0
    /*
     * Encode LMUL to lmul as follows:
     *     LMUL    vlmul    lmul
target/riscv/cpu_bits.h +0 -0
index 7c87433645..37b622fbfa 100644
@@ -493,6 +493,13 @@ typedef enum {
    /* Page table PPN shift amount */
    #define PTE_PPN_SHIFT       10
 
Quoted 8 lines

target/riscv/cpu_bits.h typedef enum { · line 502

Quoted 7 lines
target/riscv/cpu_bits.h +7 -0
+/* Page table PPN mask */
+#if defined(TARGET_RISCV32)
+#define PTE_PPN_MASK        0xFFFFFC00UL
+#elif defined(TARGET_RISCV64)
+#define PTE_PPN_MASK        0x3FFFFFFFFFFC00ULL
+#endif
+

No need to define PTE_PPN_MASK for TARGET_RISCV32.

Quoted 7 lines

ppn = (pte & PTE_PPN_MASK) >> PTE_PPN_SHIFT;

pte is target_ulong, so types are different.

TARGET_RISCV32: is 32bit.
TARGET_RISCV64: is 64bit.

I should make it more clear. You will not use PTE_PPN_MASK on TARGET_RISCV32.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
target/riscv/cpu_bits.h +0 -0
    /* Leaf page shift amount */
    #define PGSHIFT             12
 
target/riscv/cpu_helper.c +0 -0
index 327a2c4f1d..2a921bedfd 100644
@@ -622,7 +622,19 @@ restart:
                return TRANSLATE_FAIL;
            }
 
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines

target/riscv/cpu_helper.c restart: · line 629

Quoted 6 lines
target/riscv/cpu_helper.c +5 -1
-        hwaddr ppn = pte >> PTE_PPN_SHIFT;
+        hwaddr ppn;
+        RISCVCPU *cpu = env_archcpu(env);
+
+        if (riscv_cpu_sxl(env) == MXL_RV32) {
+            ppn = pte >> PTE_PPN_SHIFT;

TARGET_RISCV32 will always come here. So no need to define PTE_PPN_MASK for TARGET_RISCV32.

Oops, maybe we should use TARGET_LONG_SIZE == 4

Quoted 16 lines

#if TARGET_LONG_SIZE == 4
typedef int32_t target_long;
typedef uint32_t target_ulong;
#define TARGET_FMT_lx "%08x"
#define TARGET_FMT_ld "%d"
#define TARGET_FMT_lu "%u"
#elif TARGET_LONG_SIZE == 8
typedef int64_t target_long;
typedef uint64_t target_ulong;
#define TARGET_FMT_lx "%016" PRIx64
#define TARGET_FMT_ld "%" PRId64
#define TARGET_FMT_lu "%" PRIu64
#else
#error TARGET_LONG_SIZE undefined
#endif

TARGET_LONG_SIZE is related to TARGET_RISCV32 and TARGET_RISCV64.

In RV32, the code will truely not reach there when executing. However the code itself have different types for pte and PTE_PPN_MASK, and may cause compiler warning.

So if we only define PTE_PPN_MASK for RV64, maybe we can take type casting here:

  ppn = (pte & (target_ulong)PTE_PPN_MASK) >> PTE_PPN_SHIFT;

Regards,
Weiwei Li

Quoted 25 lines
Quoted 23 lines

Thanks,
Zhiwei

Quoted 20 lines
Quoted 20 lines

target/riscv/cpu_helper.c restart: · line 640

Quoted 11 lines
target/riscv/cpu_helper.c +8 -0
+        } else if (cpu->cfg.ext_svpbmt || cpu->cfg.ext_svnapot) {
+            ppn = (pte & PTE_PPN_MASK) >> PTE_PPN_SHIFT;
+        } else {
+            ppn = pte >> PTE_PPN_SHIFT;
+            if ((pte & ~PTE_PPN_MASK) >> PTE_PPN_SHIFT) {
+                return TRANSLATE_FAIL;
+            }
+        }
 
            if (!(pte & PTE_V)) {
                /* Invalid PTE */

Otherwise,

Thanks,
Zhiwei

#7 lore raw

Re: [PATCH v6 1/5] target/riscv: Ignore reserved bits in PTE for RV64

Quoted 138 lines · Weiwei Li
Quoted 119 lines · Guo Ren
Quoted 108 lines · LIU Zhiwei
Quoted 86 lines · Guo Ren
Quoted 77 lines · LIU Zhiwei
Quoted 39 lines · Weiwei Li

From: Guo Ren <[email protected]>

Highest bits of PTE has been used for svpbmt, ref: [1], [2], so we need to ignore them. They cannot be a part of ppn.

1: The RISC-V Instruction Set Manual, Volume II: Privileged Architecture
4.4 Sv39: Page-Based 39-bit Virtual-Memory System
4.5 Sv48: Page-Based 48-bit Virtual-Memory System

2: https://github.com/riscv/virtual-memory/blob/main/specs/663-Svpbmt-diff.pdf

Cc Liu Zhiwei <[email protected]>
Cc Bin Meng <[email protected]>
Cc Alistair Francis <[email protected]>
---
    target/riscv/cpu.h        | 13 +++++++++++++
    target/riscv/cpu_bits.h   |  7 +++++++
    target/riscv/cpu_helper.c | 14 +++++++++++++-
    3 files changed, 33 insertions(+), 1 deletion(-)
target/riscv/cpu.h +3 -0
index 55635d68d5..45de8faaca 100644
@@ -341,6 +341,8 @@ struct RISCVCPU {
            bool ext_counters;
            bool ext_ifencei;
            bool ext_icsr;
+        bool ext_svnapot;
+        bool ext_svpbmt;
            bool ext_zfh;
            bool ext_zfhmin;
            bool ext_zve32f;
@@ -495,6 +497,17 @@ static inline int riscv_cpu_xlen(CPURISCVState *env)
        return 16 << env->xl;
    }
 
+#ifndef CONFIG_USER_ONLY

target/riscv/cpu.h static inline int riscv_cpu_xlen(CPURISCVState *env) · line 510

Quoted 10 lines
target/riscv/cpu.h +10 -0
+#ifdef TARGET_RISCV32
+#define riscv_cpu_sxl(env)  ((void)(env), MXL_RV32)
+#else
+static inline RISCVMXL riscv_cpu_sxl(CPURISCVState *env)
+{
+    return get_field(env->mstatus, MSTATUS64_SXL);
+}
+#endif
+#endif
+

Perhaps an interface also works for user mode is better.

target/riscv/cpu.h +13 -0
+#ifdef TARGET_RISCV32
+#define riscv_cpu_sxl(env)  ((void)(env), MXL_RV32)
+#else
+static inline RISCVMXL riscv_cpu_sxl(CPURISCVState *env)
+{
+#ifdef CONFIG_USER_ONLY
+    return env->misa_mxl;
+#else
+    return get_field(env->mstatus, MSTATUS64_SXL);
+#endif
+}
+#endif
+
Quoted 12 lines
target/riscv/cpu.h +0 -0
    /*
     * Encode LMUL to lmul as follows:
     *     LMUL    vlmul    lmul
target/riscv/cpu_bits.h +0 -0
index 7c87433645..37b622fbfa 100644
@@ -493,6 +493,13 @@ typedef enum {
    /* Page table PPN shift amount */
    #define PTE_PPN_SHIFT       10
 
Quoted 8 lines

target/riscv/cpu_bits.h typedef enum { · line 502

Quoted 7 lines
target/riscv/cpu_bits.h +7 -0
+/* Page table PPN mask */
+#if defined(TARGET_RISCV32)
+#define PTE_PPN_MASK        0xFFFFFC00UL
+#elif defined(TARGET_RISCV64)
+#define PTE_PPN_MASK        0x3FFFFFFFFFFC00ULL
+#endif
+

No need to define PTE_PPN_MASK for TARGET_RISCV32.

Quoted 7 lines

ppn = (pte & PTE_PPN_MASK) >> PTE_PPN_SHIFT;

pte is target_ulong, so types are different.

TARGET_RISCV32: is 32bit.
TARGET_RISCV64: is 64bit.

I should make it more clear. You will not use PTE_PPN_MASK on TARGET_RISCV32.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
target/riscv/cpu_bits.h +0 -0
    /* Leaf page shift amount */
    #define PGSHIFT             12
 
target/riscv/cpu_helper.c +0 -0
index 327a2c4f1d..2a921bedfd 100644
@@ -622,7 +622,19 @@ restart:
                return TRANSLATE_FAIL;
            }
 
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines

target/riscv/cpu_helper.c restart: · line 629

Quoted 6 lines
target/riscv/cpu_helper.c +5 -1
-        hwaddr ppn = pte >> PTE_PPN_SHIFT;
+        hwaddr ppn;
+        RISCVCPU *cpu = env_archcpu(env);
+
+        if (riscv_cpu_sxl(env) == MXL_RV32) {
+            ppn = pte >> PTE_PPN_SHIFT;

TARGET_RISCV32 will always come here. So no need to define PTE_PPN_MASK for TARGET_RISCV32.

Oops, maybe we should use TARGET_LONG_SIZE == 4

Quoted 16 lines

#if TARGET_LONG_SIZE == 4
typedef int32_t target_long;
typedef uint32_t target_ulong;
#define TARGET_FMT_lx "%08x"
#define TARGET_FMT_ld "%d"
#define TARGET_FMT_lu "%u"
#elif TARGET_LONG_SIZE == 8
typedef int64_t target_long;
typedef uint64_t target_ulong;
#define TARGET_FMT_lx "%016" PRIx64
#define TARGET_FMT_ld "%" PRId64
#define TARGET_FMT_lu "%" PRIu64
#else
#error TARGET_LONG_SIZE undefined
#endif

Quoted 7 lines

TARGET_LONG_SIZE is related to TARGET_RISCV32 and TARGET_RISCV64.

In RV32, the code will truely not reach there when executing. However the code itself have different types for pte and PTE_PPN_MASK, and may cause compiler warning.

So if we only define PTE_PPN_MASK for RV64, maybe we can take type casting here:

   ppn = (pte & (target_ulong)PTE_PPN_MASK) >> PTE_PPN_SHIFT;

(target_ulong)PTE_PPN_MASK seems (u32) 0x3FFFFFFFFFFC00ULL = 0xFFFFFC00UL

I'm okay with the above.

Quoted 31 lines

Regards,
Weiwei Li

Quoted 25 lines
Quoted 23 lines

Thanks,
Zhiwei

Quoted 20 lines
Quoted 20 lines

target/riscv/cpu_helper.c restart: · line 640

Quoted 11 lines
target/riscv/cpu_helper.c +8 -0
+        } else if (cpu->cfg.ext_svpbmt || cpu->cfg.ext_svnapot) {
+            ppn = (pte & PTE_PPN_MASK) >> PTE_PPN_SHIFT;
+        } else {
+            ppn = pte >> PTE_PPN_SHIFT;
+            if ((pte & ~PTE_PPN_MASK) >> PTE_PPN_SHIFT) {
+                return TRANSLATE_FAIL;
+            }
+        }
 
            if (!(pte & PTE_V)) {
                /* Invalid PTE */

Otherwise,

Thanks,
Zhiwei

Signature · 4 lines
Best Regards
 Guo Ren

ML: https://lore.kernel.org/linux-csky/
#9 lore raw

Re: [PATCH v6 2/5] target/riscv: add PTE_A/PTE_D/PTE_U bits check for inner PTE

In reply to Weiwei Li
Quoted 4 lines · Weiwei Li

Could you please add a commit message to this patch?

Alistair

Quoted 22 lines
---
 target/riscv/cpu_helper.c | 3 +++
 1 file changed, 3 insertions(+)
target/riscv/cpu_helper.c +3 -1
index 2a921bedfd..a5bf07ccb6 100644
@@ -641,6 +641,9 @@ restart:
             return TRANSLATE_FAIL;
         } else if (!(pte & (PTE_R | PTE_W | PTE_X))) {
             /* Inner PTE, continue walking */
+            if (pte & (PTE_D | PTE_A | PTE_U)) {
+                return TRANSLATE_FAIL;
+            }
             base = ppn << PGSHIFT;
         } else if ((pte & (PTE_R | PTE_W | PTE_X)) == PTE_W) {
             /* Reserved leaf PTE flags: PTE_W */
--

2.17.1

author
#10 lore raw

Re: [PATCH v6 2/5] target/riscv: add PTE_A/PTE_D/PTE_U bits check for inner PTE

Quoted 7 lines · Alistair Francis
Quoted 3 lines · Weiwei Li

Could you please add a commit message to this patch?

Alistair

OK. I'll add it.

Regards,

Weiwei Li

Quoted 22 lines
Quoted 22 lines
---
  target/riscv/cpu_helper.c | 3 +++
  1 file changed, 3 insertions(+)
target/riscv/cpu_helper.c +3 -1
index 2a921bedfd..a5bf07ccb6 100644
@@ -641,6 +641,9 @@ restart:
              return TRANSLATE_FAIL;
          } else if (!(pte & (PTE_R | PTE_W | PTE_X))) {
              /* Inner PTE, continue walking */
+            if (pte & (PTE_D | PTE_A | PTE_U)) {
+                return TRANSLATE_FAIL;
+            }
              base = ppn << PGSHIFT;
          } else if ((pte & (PTE_R | PTE_W | PTE_X)) == PTE_W) {
              /* Reserved leaf PTE flags: PTE_W */
--

2.17.1