Re: [PATCH RFC 4/8] target/riscv: Support generic CSR indirect access

2024-06-05 Thread Jason Chien
The predicate functions should contain the access control by the 
state-enable CSRs, which is not presented in this patch. Do you mind 
that I take over the indirect CSR access control part? The Signed-off-by 
will be kept.


Atish Patra 於 2024/2/17 上午 08:01 寫道:

From: Kaiwen Xue 

This adds the indirect access registers required by sscsrind/smcsrind
and the operations on them. Note that xiselect and xireg are used for
both AIA and sxcsrind, and the behavior of accessing them depends on
whether each extension is enabled and the value stored in xiselect.

Co-developed-by: Atish Patra 
Signed-off-by: Atish Patra 
Signed-off-by: Kaiwen Xue 
---
  target/riscv/cpu_bits.h |  28 +++-
  target/riscv/csr.c  | 146 +++-
  2 files changed, 169 insertions(+), 5 deletions(-)

diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h
index 0ee91e502e8f..3a66f83009b5 100644
--- a/target/riscv/cpu_bits.h
+++ b/target/riscv/cpu_bits.h
@@ -176,6 +176,13 @@
  #define CSR_MISELECT0x350
  #define CSR_MIREG   0x351
  
+/* Machine Indirect Register Alias */

+#define CSR_MIREG2  0x352
+#define CSR_MIREG3  0x353
+#define CSR_MIREG4  0x355
+#define CSR_MIREG5  0x356
+#define CSR_MIREG6  0x357
+
  /* Machine-Level Interrupts (AIA) */
  #define CSR_MTOPEI  0x35c
  #define CSR_MTOPI   0xfb0
@@ -225,6 +232,13 @@
  #define CSR_SISELECT0x150
  #define CSR_SIREG   0x151
  
+/* Supervisor Indirect Register Alias */

+#define CSR_SIREG2  0x152
+#define CSR_SIREG3  0x153
+#define CSR_SIREG4  0x155
+#define CSR_SIREG5  0x156
+#define CSR_SIREG6  0x157
+
  /* Supervisor-Level Interrupts (AIA) */
  #define CSR_STOPEI  0x15c
  #define CSR_STOPI   0xdb0
@@ -291,6 +305,13 @@
  #define CSR_VSISELECT   0x250
  #define CSR_VSIREG  0x251
  
+/* Virtual Supervisor Indirect Alias */

+#define CSR_VSIREG2 0x252
+#define CSR_VSIREG3 0x253
+#define CSR_VSIREG4 0x255
+#define CSR_VSIREG5 0x256
+#define CSR_VSIREG6 0x257
+
  /* VS-Level Interrupts (H-extension with AIA) */
  #define CSR_VSTOPEI 0x25c
  #define CSR_VSTOPI  0xeb0
@@ -847,10 +868,13 @@ typedef enum RISCVException {
  #define ISELECT_IMSIC_EIE630xff
  #define ISELECT_IMSIC_FIRSTISELECT_IMSIC_EIDELIVERY
  #define ISELECT_IMSIC_LAST ISELECT_IMSIC_EIE63
-#define ISELECT_MASK   0x1ff
+#define ISELECT_MASK_AIA   0x1ff
+
+/* MISELECT, SISELECT, and VSISELECT bits (AIA) */
+#define ISELECT_MASK_SXCSRIND  0xfff
Could you rename ISELECT_MASK_SXCSRIND to ISELECT_MASK_CSRIND to keep 
the naming consistency with ISELECT_MASK_AIA?
  
  /* Dummy [M|S|VS]ISELECT value for emulating [M|S|VS]TOPEI CSRs */

-#define ISELECT_IMSIC_TOPEI(ISELECT_MASK + 1)
+#define ISELECT_IMSIC_TOPEI(ISELECT_MASK_AIA + 1)
  
  /* IMSIC bits (AIA) */

  #define IMSIC_TOPEI_IID_SHIFT  16
diff --git a/target/riscv/csr.c b/target/riscv/csr.c
index 89a1325a02a5..a1c10f1d010a 100644
--- a/target/riscv/csr.c
+++ b/target/riscv/csr.c
@@ -287,6 +287,17 @@ static int aia_any32(CPURISCVState *env, int csrno)
  return any32(env, csrno);
  }
  
+static RISCVException sxcsrind_any(CPURISCVState *env, int csrno)
Could you rename sxcsrind_any() to csrind_any() to keep naming 
consistency with aia_any()?

+{
+RISCVCPU *cpu = env_archcpu(env);
+
+if (!cpu->cfg.ext_smcsrind) {
+return RISCV_EXCP_ILLEGAL_INST;
+}
+
+return RISCV_EXCP_NONE;
+}
+
  static int sxcsrind_or_aia_any(CPURISCVState *env, int csrno)
  {
  if (!riscv_cpu_cfg(env)->ext_smaia && !riscv_cpu_cfg(env)->ext_smcsrind) {
@@ -355,6 +366,17 @@ static int aia_smode32(CPURISCVState *env, int csrno)
  return smode32(env, csrno);
  }
  
+static RISCVException sxcsrind_smode(CPURISCVState *env, int csrno)
Could you rename sxcsrind_smode() to csrind_smode() to keep naming 
consistency with aia_smode()?

+{
+RISCVCPU *cpu = env_archcpu(env);
+
+if (!cpu->cfg.ext_sscsrind) {
S-mode CSRs are defined in Smcsrind as well. If both Smcsrind and 
Sscsrind are disabled, return RISCV_EXCP_ILLEGAL_INST.

+return RISCV_EXCP_ILLEGAL_INST;
+}
+
A virtual instruction exception should be raised here for attempts from 
VU-mode to access siselect or sireg*.

+return smode(env, csrno);
+}
+
  static int sxcsrind_or_aia_smode(CPURISCVState *env, int csrno)
  {
  if (!riscv_cpu_cfg(env)->ext_ssaia && !riscv_cpu_cfg(env)->ext_sscsrind) {
@@ -383,6 +405,17 @@ static RISCVException hmode32(CPURISCVState *env, int 
csrno)
  
  }
  
+static RISCVException sxcsrind_hmode(CPURISCVState *env, int csrno)
Could you rename sxcsrind_hmode() to csrind_hmode() to keep naming 
consistency with aia_hmode()?

+{
+RISCVCPU *cpu = env_archcpu(env);
+
+if 

[PATCH RFC 4/8] target/riscv: Support generic CSR indirect access

2024-02-16 Thread Atish Patra
From: Kaiwen Xue 

This adds the indirect access registers required by sscsrind/smcsrind
and the operations on them. Note that xiselect and xireg are used for
both AIA and sxcsrind, and the behavior of accessing them depends on
whether each extension is enabled and the value stored in xiselect.

Co-developed-by: Atish Patra 
Signed-off-by: Atish Patra 
Signed-off-by: Kaiwen Xue 
---
 target/riscv/cpu_bits.h |  28 +++-
 target/riscv/csr.c  | 146 +++-
 2 files changed, 169 insertions(+), 5 deletions(-)

diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h
index 0ee91e502e8f..3a66f83009b5 100644
--- a/target/riscv/cpu_bits.h
+++ b/target/riscv/cpu_bits.h
@@ -176,6 +176,13 @@
 #define CSR_MISELECT0x350
 #define CSR_MIREG   0x351
 
+/* Machine Indirect Register Alias */
+#define CSR_MIREG2  0x352
+#define CSR_MIREG3  0x353
+#define CSR_MIREG4  0x355
+#define CSR_MIREG5  0x356
+#define CSR_MIREG6  0x357
+
 /* Machine-Level Interrupts (AIA) */
 #define CSR_MTOPEI  0x35c
 #define CSR_MTOPI   0xfb0
@@ -225,6 +232,13 @@
 #define CSR_SISELECT0x150
 #define CSR_SIREG   0x151
 
+/* Supervisor Indirect Register Alias */
+#define CSR_SIREG2  0x152
+#define CSR_SIREG3  0x153
+#define CSR_SIREG4  0x155
+#define CSR_SIREG5  0x156
+#define CSR_SIREG6  0x157
+
 /* Supervisor-Level Interrupts (AIA) */
 #define CSR_STOPEI  0x15c
 #define CSR_STOPI   0xdb0
@@ -291,6 +305,13 @@
 #define CSR_VSISELECT   0x250
 #define CSR_VSIREG  0x251
 
+/* Virtual Supervisor Indirect Alias */
+#define CSR_VSIREG2 0x252
+#define CSR_VSIREG3 0x253
+#define CSR_VSIREG4 0x255
+#define CSR_VSIREG5 0x256
+#define CSR_VSIREG6 0x257
+
 /* VS-Level Interrupts (H-extension with AIA) */
 #define CSR_VSTOPEI 0x25c
 #define CSR_VSTOPI  0xeb0
@@ -847,10 +868,13 @@ typedef enum RISCVException {
 #define ISELECT_IMSIC_EIE630xff
 #define ISELECT_IMSIC_FIRSTISELECT_IMSIC_EIDELIVERY
 #define ISELECT_IMSIC_LAST ISELECT_IMSIC_EIE63
-#define ISELECT_MASK   0x1ff
+#define ISELECT_MASK_AIA   0x1ff
+
+/* MISELECT, SISELECT, and VSISELECT bits (AIA) */
+#define ISELECT_MASK_SXCSRIND  0xfff
 
 /* Dummy [M|S|VS]ISELECT value for emulating [M|S|VS]TOPEI CSRs */
-#define ISELECT_IMSIC_TOPEI(ISELECT_MASK + 1)
+#define ISELECT_IMSIC_TOPEI(ISELECT_MASK_AIA + 1)
 
 /* IMSIC bits (AIA) */
 #define IMSIC_TOPEI_IID_SHIFT  16
diff --git a/target/riscv/csr.c b/target/riscv/csr.c
index 89a1325a02a5..a1c10f1d010a 100644
--- a/target/riscv/csr.c
+++ b/target/riscv/csr.c
@@ -287,6 +287,17 @@ static int aia_any32(CPURISCVState *env, int csrno)
 return any32(env, csrno);
 }
 
+static RISCVException sxcsrind_any(CPURISCVState *env, int csrno)
+{
+RISCVCPU *cpu = env_archcpu(env);
+
+if (!cpu->cfg.ext_smcsrind) {
+return RISCV_EXCP_ILLEGAL_INST;
+}
+
+return RISCV_EXCP_NONE;
+}
+
 static int sxcsrind_or_aia_any(CPURISCVState *env, int csrno)
 {
 if (!riscv_cpu_cfg(env)->ext_smaia && !riscv_cpu_cfg(env)->ext_smcsrind) {
@@ -355,6 +366,17 @@ static int aia_smode32(CPURISCVState *env, int csrno)
 return smode32(env, csrno);
 }
 
+static RISCVException sxcsrind_smode(CPURISCVState *env, int csrno)
+{
+RISCVCPU *cpu = env_archcpu(env);
+
+if (!cpu->cfg.ext_sscsrind) {
+return RISCV_EXCP_ILLEGAL_INST;
+}
+
+return smode(env, csrno);
+}
+
 static int sxcsrind_or_aia_smode(CPURISCVState *env, int csrno)
 {
 if (!riscv_cpu_cfg(env)->ext_ssaia && !riscv_cpu_cfg(env)->ext_sscsrind) {
@@ -383,6 +405,17 @@ static RISCVException hmode32(CPURISCVState *env, int 
csrno)
 
 }
 
+static RISCVException sxcsrind_hmode(CPURISCVState *env, int csrno)
+{
+RISCVCPU *cpu = env_archcpu(env);
+
+if (!cpu->cfg.ext_sscsrind) {
+return RISCV_EXCP_ILLEGAL_INST;
+}
+
+return hmode(env, csrno);
+}
+
 static int sxcsrind_or_aia_hmode(CPURISCVState *env, int csrno)
 {
 if (!riscv_cpu_cfg(env)->ext_ssaia && !riscv_cpu_cfg(env)->ext_sscsrind) {
@@ -1926,7 +1959,12 @@ static int rmw_xiselect(CPURISCVState *env, int csrno, 
target_ulong *val,
 *val = *iselect;
 }
 
-wr_mask &= ISELECT_MASK;
+if (riscv_cpu_cfg(env)->ext_smcsrind || riscv_cpu_cfg(env)->ext_sscsrind) {
+wr_mask &= ISELECT_MASK_SXCSRIND;
+} else {
+wr_mask &= ISELECT_MASK_AIA;
+}
+
 if (wr_mask) {
 *iselect = (*iselect & ~wr_mask) | (new_val & wr_mask);
 }
@@ -2065,6 +2103,59 @@ done:
 return RISCV_EXCP_NONE;
 }
 
+/*
+ * rmw_xireg_sxcsrind: Perform indirect access to xireg and xireg2-xireg6
+ *
+ * Perform indirect access to xireg and xireg2-xireg6.
+ * This is a generic interface for all xireg CSRs. Apart from