Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1ogUKI-0002Ha-Ag for mharc-qemu-riscv@gnu.org; Thu, 06 Oct 2022 13:06:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43252) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ogUKD-000263-Mt for qemu-riscv@nongnu.org; Thu, 06 Oct 2022 13:06:22 -0400 Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]:43748) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ogUKA-0006A3-FU for qemu-riscv@nongnu.org; Thu, 06 Oct 2022 13:06:20 -0400 Received: by mail-pj1-x1031.google.com with SMTP id g1-20020a17090a708100b00203c1c66ae3so2372637pjk.2 for ; Thu, 06 Oct 2022 10:06:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; h=mime-version:user-agent:references:in-reply-to:date:cc:to:from :subject:message-id:from:to:cc:subject:date:message-id:reply-to; bh=yKaD/R3/C1Hpn4Pj7UJP5JQHTfh9voBLDqH5MOY2HxI=; b=lsmcZ2APG2YWpQQti5q93NEO0chaEiyGR2reaUrKhYtdnD114OTXMQD5A8CkG2jz6E YRazu7l4YkrDtPQ0/HBf1uQbGjBQjx66VTmhFJIhq6ot0bxPSMgi7URCNw7XjbH9mJcB qA+fkitjJsFBfeRSndj9YZ9e3zB2fsRN2cLtnqwHVheS+q+v3KI5HMInMT62ImLAEmPu cYN66muHK006nOQLgCRZxoew1RLGl/TewJ0QMrWIaPxTmMKxe1rYm7P7We0f5S/laA09 lQWg1pr8xUxozgMIqcWM3A5suIrFgIjiLUhd+hEuHa3ipGaNCYMObUnyMmG+SySVQu6y L2Vg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:user-agent:references:in-reply-to:date:cc:to:from :subject:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=yKaD/R3/C1Hpn4Pj7UJP5JQHTfh9voBLDqH5MOY2HxI=; b=PnJDPOgub1/hvJu+m+wN3IuDvGXSl+lQbeW2os9DLR8zJ4t0+Q+FNRoEqAg1uK2Rl/ qKeRtYguqIsbcLtCbUDhx2T4SOvcVYW0FqVntlDpDQk5uXFkdCgtKcnW6hvOTHCdZM3t V1HwvjlAzl1hHHXxJmy3zjB1nOl2Ur3Tc5a9L28y9+2qn+evbS+BSLdCVdog5wvM/tWc YGLGJt6bCEiLRX4fhvyeEBWT4RIhhhRWL8VIIJVSE0xKuCk4DpB3F9UiPhSFHffKYES9 W+AHMFbxFHkYsleMbhwJJS91qAc8jBsrizuDbK8y7TUwW+3GxRBxoE18nLEpjo0y4u+B 8qfQ== X-Gm-Message-State: ACrzQf3isxFYygLVGEOdYnnxsF85XPz5ylw0Rp2VcpFN7Zek0xepeVVS 9zoDu+Rgb3vB5LdUFkS9gWQtSA== X-Google-Smtp-Source: AMsMyM7CU2pbnU96zAHuwNg9el0ULZSFjtPEONPwiM0Ir8auB4XQhnCTGQWdACDbOYYkvJ0yV8jUzA== X-Received: by 2002:a17:90b:3144:b0:202:e2cd:2d2a with SMTP id ip4-20020a17090b314400b00202e2cd2d2amr676102pjb.39.1665075976427; Thu, 06 Oct 2022 10:06:16 -0700 (PDT) Received: from [192.168.1.5] ([103.97.165.210]) by smtp.gmail.com with ESMTPSA id p21-20020a170902ead500b001788ccecbf5sm1541290pld.31.2022.10.06.10.06.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Oct 2022 10:06:15 -0700 (PDT) Message-ID: Subject: Re: [PATCH v10 4/5] target/riscv: smstateen check for fcsr From: mchitale@ventanamicro.com To: weiwei , qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: alistair.francis@wdc.com Date: Thu, 06 Oct 2022 22:36:10 +0530 In-Reply-To: References: <20221003114718.30659-1-mchitale@ventanamicro.com> <20221003114718.30659-5-mchitale@ventanamicro.com> <404205f5-9626-23b3-d388-b2288293c7d1@iscas.ac.cn> Content-Type: multipart/alternative; boundary="=-ok35VPksaYy2IEbOXLos" User-Agent: Evolution 3.36.5-0ubuntu1 MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1031; envelope-from=mchitale@ventanamicro.com; helo=mail-pj1-x1031.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-riscv@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 06 Oct 2022 17:06:22 -0000 --=-ok35VPksaYy2IEbOXLos Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit On Tue, 2022-10-04 at 21:23 +0800, weiwei wrote: > > > > > On 2022/10/4 14:51, > mchitale@ventanamicro.com wrote: > > > > > > On Mon, 2022-10-03 at 21:02 +0800, weiwei wrote: > > > > > On 2022/10/3 19:47, Mayuresh Chitale wrote: > > > > > > > If smstateen is implemented and sstateen0.fcsr is > > > > clear then thefloating pointoperations must return illegal > > > > instruction exception or virtualinstructiontrap, if relevant. > > > > Signed-off-by: Mayuresh Chitale > > > > --- target/riscv/csr.c | 23 > > > > ++++++++++++ target/riscv/insn_trans/trans_rvf.c.inc | > > > > 43+++++++++++++++++++++ > > > > -- target/riscv/insn_trans/trans_rvzfh.c.inc | 12 +++++++ 3 > > > > files changed, 75 insertions(+), 3 deletions(-) > > > > diff --git a/target/riscv/csr.c b/target/riscv/csr.cindex > > > > 71236f2b5d..8b25f885ec 100644--- a/target/riscv/csr.c+++ > > > > b/target/riscv/csr.c@@ -84,6 +84,10 @@ static RISCVException > > > > fs(CPURISCVState *env, > > > > intcsrno) !RISCV_CPU(env_cpu(env))->cfg.ext_zfinx) > > > > { return RISCV_EXCP_ILLEGAL_INST; }++ if > > > > (!env->debugger && !riscv_cpu_fp_enabled(env)) {+ return > > > > smstateen_acc_ok(env, 0, > > > > SMSTATEEN0_FCSR);+ } #endif return > > > > RISCV_EXCP_NONE; }@@ -2023,6 +2027,9 @@ static > > > > RISCVExceptionwrite_mstateen0(CPURISCVState *env, int > > > > csrno, target_ulong > > > > new_val) { uint64_t wr_mask = SMSTATEEN_STATEEN | > > > > SMSTATEEN0_HSENVCFG;+ if (!riscv_has_ext(env, RVF)) > > > > {+ wr_mask |= SMSTATEEN0_FCSR;+ } return > > > > write_mstateen(env, csrno, wr_mask, new_val); }@@ -2059,6 > > > > +2066,10 @@ static RISCVExceptionwrite_mstateen0h(CPURISCVState > > > > *env, int csrno, { uint64_t wr_mask = SMSTATEEN_STATEEN | > > > > SMSTATEEN0_HSENVCFG; + if (!riscv_has_ext(env, RVF)) > > > > {+ wr_mask |= SMSTATEEN0_FCSR;+ }+ return > > > > write_mstateenh(env, csrno, wr_mask, new_val); } @@ -2096,6 > > > > +2107,10 @@ static RISCVExceptionwrite_hstateen0(CPURISCVState > > > > *env, int csrno, { uint64_t wr_mask = SMSTATEEN_STATEEN | > > > > SMSTATEEN0_HSENVCFG; + if (!riscv_has_ext(env, RVF)) > > > > {+ wr_mask |= SMSTATEEN0_FCSR;+ }+ return > > > > write_hstateen(env, csrno, wr_mask, new_val); } @@ -2135,6 > > > > +2150,10 @@ static RISCVExceptionwrite_hstateen0h(CPURISCVState > > > > *env, int csrno, { uint64_t wr_mask = SMSTATEEN_STATEEN | > > > > SMSTATEEN0_HSENVCFG; + if (!riscv_has_ext(env, RVF)) > > > > {+ wr_mask |= SMSTATEEN0_FCSR;+ }+ return > > > > write_hstateenh(env, csrno, wr_mask, new_val); } @@ -2182,6 > > > > +2201,10 @@ static RISCVExceptionwrite_sstateen0(CPURISCVState > > > > *env, int csrno, { uint64_t wr_mask = SMSTATEEN_STATEEN | > > > > SMSTATEEN0_HSENVCFG; + if (!riscv_has_ext(env, RVF)) > > > > {+ wr_mask |= SMSTATEEN0_FCSR;+ }+ return > > > > write_sstateen(env, csrno, wr_mask, new_val); } diff --git > > > > a/target/riscv/insn_trans/trans_rvf.c.incb/target/riscv/insn_tr > > > > ans/trans_rvf.c.incindex a1d3eb52ad..93657680c6 100644--- > > > > a/target/riscv/insn_trans/trans_rvf.c.inc+++ > > > > b/target/riscv/insn_trans/trans_rvf.c.inc@@ -24,9 +24,46 > > > > @@ return false; \ } while (0) -#define > > > > REQUIRE_ZFINX_OR_F(ctx) do {\- if (!ctx->cfg_ptr->ext_zfinx) > > > > { \- REQUIRE_EXT(ctx, RVF); \+#ifndef > > > > CONFIG_USER_ONLY+static inline bool > > > > smstateen_fcsr_check(DisasContext *ctx, > > > > intindex)+{+ CPUState *cpu = ctx->cs;+ CPURISCVState *env > > > > = cpu->env_ptr;+ uint64_t stateen = env- > > > > >mstateen[index];++ if (!ctx->cfg_ptr->ext_smstateen || env- > > > > >priv == PRV_M) {+ return true;+ }++ if (ctx- > > > > >virt_enabled) {+ stateen &= env- > > > > >hstateen[index];+ }++ if (env->priv == PRV_U && > > > > has_ext(ctx, RVS)) {+ stateen &= env- > > > > >sstateen[index];+ }++ if (!(stateen & SMSTATEEN0_FCSR)) > > > > {+ if (ctx->virt_enabled) {+ ctx- > > > > >virt_inst_excp = true;+ } > > > > > > > > > > Are there any considerations for adding virt_inst_excp > > > instead ofdirectly > > > "generate_exception(ctx, RISCV_EXCP_VIRT_INSTRUCTION_FAULT)" > > > here? > > > Regards, > > > Weiwei Li > > > > > > > I had considered it but I think this is a simpler approach as > > the restof the code path stays the same as generating an illegal > > instructionexception, for e.g setting the bins value (tval). > > > > OK, we did need to set bins value for virt instruction > exception. However I prefer directly call a > > > > new gen_exception_virt function(similar to gen_exception_illegal) > here. > > > > Also we need toreturn true from all the caller trans_* > > functions even if the smstateencheck has failed. > > > > False is returned when smstateen check fails currently. Yes, however if we make this change then should return true if the check fails so that the decode_opc doesnt fall through and generate another exception. It can be done but it would be contrary to the general convention. > Regards, > Weiwei Li > > > > > > > > > > > > > > > > > + return false;+ }++ return > > > > true;+}+#else+#define smstateen_fcsr_check(ctx, index) > > > > (true)+#endif++#define REQUIRE_ZFINX_OR_F(ctx) do { \+ if > > > > (!has_ext(ctx, RVF)) { \+ if (!ctx->cfg_ptr->ext_zfinx) > > > > { \+ return false; \+ } \+ if > > > > (!smstateen_fcsr_check(ctx, 0)) { \+ return false; > > > > \+ } \ } \ } while (0) diff --git > > > > a/target/riscv/insn_trans/trans_rvzfh.c.incb/target/riscv/insn_ > > > > trans/trans_rvzfh.c.incindex 5d07150cd0..6c2e338c0a 100644--- > > > > a/target/riscv/insn_trans/trans_rvzfh.c.inc+++ > > > > b/target/riscv/insn_trans/trans_rvzfh.c.inc@@ -20,18 +20,27 > > > > @@ if (!ctx->cfg_ptr->ext_zfh) { \ return > > > > false; \ } \+ if > > > > (!smstateen_fcsr_check(ctx, 0)) { \+ return false; > > > > \+ } \ } while (0) #define REQUIRE_ZHINX_OR_ZFH(ctx) do > > > > { \ if (!ctx->cfg_ptr->ext_zhinx && !ctx->cfg_ptr- > > > > >ext_zfh) { \ return > > > > false; \ } > > > > \+ if (!smstateen_fcsr_check(ctx, 0)) { \+ return > > > > false; \+ } \ } while (0) #define > > > > REQUIRE_ZFH_OR_ZFHMIN(ctx) do { \ if (!(ctx- > > > > >cfg_ptr->ext_zfh || ctx->cfg_ptr->ext_zfhmin)) { > > > > \ return > > > > false; \ } > > > > \+ if (!smstateen_fcsr_check(ctx, 0)) { > > > > \+ return false; \+ } \ } while (0) #define > > > > REQUIRE_ZFH_OR_ZFHMIN_OR_ZHINX_OR_ZHINXMIN(ctx) do { \@@ -39,6 > > > > +48,9 @@ ctx->cfg_ptr->ext_zhinx || ctx->cfg_ptr- > > > > >ext_zhinxmin)) { \ return > > > > false; \ } > > > > \+ if > > > > (!smstateen_fcsr_check(ctx, 0)) { \+ return false; > > > > \+ } \ } while (0) static bool trans_flh(DisasContext > > > > *ctx, arg_flh *a) > > > > > > > > > > > > > > > > > --=-ok35VPksaYy2IEbOXLos Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable
On Tue, 2022-10-04 a= t 21:23 +0800, weiwei wrote:


On 2022/10/4 14:51, mchitale@ventanamicro.com wrote:
On Mon, 2022-10-03 at 21:02 +0800, weiwei wrote:
On 2022/10/3 19:47, Mayuresh Chitale wrote:
If smstateen is implemented and sstateen0.fcsr is clear then=
 the
floating point
operations must return illegal inst=
ruction exception or virtual
instruction
trap, if relev=
ant.

Signed-off-by: Mayuresh Chitale 
=
<mchitale@ventanamicro.com>

---
target/riscv/csr.c | 23 ++++++++++++
 =
 target/riscv/insn_trans/trans_rvf.c.inc   | 43
+++++++++++++++++=
++++--
  target/riscv/insn_trans/trans_rvzfh.c.inc | 12 +++++++
  3 files changed, 75 insertions(+), 3 deletions(-)

=
diff --git a/target/riscv/csr.c b/target/riscv/csr.c
i=
ndex 71236f2b5d..8b25f885ec 100644
--- a/target/riscv/csr.c
=
+++ b/target/riscv/csr.c
@@ -84,6 +84,10 @@ static RISCVExce=
ption fs(CPURISCVState *env, int
csrno)
          !RISC=
V_CPU(env_cpu(env))->cfg.ext_zfinx) {
          return RISCV_E=
XCP_ILLEGAL_INST;
      }
+
+    if (!env->=
;debugger && !riscv_cpu_fp_enabled(env)) {
+        retur=
n smstateen_acc_ok(env, 0, SMSTATEEN0_FCSR);
+    }
  #=
endif
      return RISCV_EXCP_NONE;
  }
@@ -2=
023,6 +2027,9 @@ static RISCVException
write_mstateen0(CPURISCVSt=
ate *env, int csrno,
                                        targ=
et_ulong new_val)
  {
      uint64_t wr_mask =3D SMSTAT=
EEN_STATEEN | SMSTATEEN0_HSENVCFG;
+    if (!riscv_has_ext(env, R=
VF)) {
+        wr_mask |=3D SMSTATEEN0_FCSR;
+    }
  
      return write_mstateen(env, csrno, wr_mask, new_v=
al);
  }
@@ -2059,6 +2066,10 @@ static RISCVException
write_mstateen0h(CPURISCVState *env, int csrno,
  {
      uint64_t wr_mask =3D SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
  
+    if (!riscv_has_ext(env, RVF)) {
+     =
   wr_mask |=3D SMSTATEEN0_FCSR;
+    }
+
   =
   return write_mstateenh(env, csrno, wr_mask, new_val);
  }
  
@@ -2096,6 +2107,10 @@ static RISCVException
w=
rite_hstateen0(CPURISCVState *env, int csrno,
  {
     =
 uint64_t wr_mask =3D SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
  =
+    if (!riscv_has_ext(env, RVF)) {
+        wr_mask =
|=3D SMSTATEEN0_FCSR;
+    }
+
      return w=
rite_hstateen(env, csrno, wr_mask, new_val);
  }
  
@@ -2135,6 +2150,10 @@ static RISCVException
write_hstatee=
n0h(CPURISCVState *env, int csrno,
  {
      uint64_t w=
r_mask =3D SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
  
=
+    if (!riscv_has_ext(env, RVF)) {
+        wr_mask |=3D SMSTAT=
EEN0_FCSR;
+    }
+
      return write_hstate=
enh(env, csrno, wr_mask, new_val);
  }
  
@@ =
-2182,6 +2201,10 @@ static RISCVException
write_sstateen0(CPURISC=
VState *env, int csrno,
  {
      uint64_t wr_mask =3D =
SMSTATEEN_STATEEN | SMSTATEEN0_HSENVCFG;
  
+    if (!r=
iscv_has_ext(env, RVF)) {
+        wr_mask |=3D SMSTATEEN0_FCSR;<=
/pre>
+    }
+
      return write_sstateen(env, csr=
no, wr_mask, new_val);
  }
  
diff --git a/ta=
rget/riscv/insn_trans/trans_rvf.c.inc
b/target/riscv/insn_trans/t=
rans_rvf.c.inc
index a1d3eb52ad..93657680c6 100644
--- =
a/target/riscv/insn_trans/trans_rvf.c.inc
+++ b/target/riscv/insn=
_trans/trans_rvf.c.inc
@@ -24,9 +24,46 @@
             =
 return false; \
  } while (0)
  
-#define RE=
QUIRE_ZFINX_OR_F(ctx) do {\
-    if (!ctx->cfg_ptr->ext_zfi=
nx) { \
-        REQUIRE_EXT(ctx, RVF); \
+#ifndef CONF=
IG_USER_ONLY
+static inline bool smstateen_fcsr_check(DisasContex=
t *ctx, int
index)
+{
+    CPUState *cpu =3D =
ctx->cs;
+    CPURISCVState *env =3D cpu->env_ptr;
+ uint64_t stateen =3D env->mstateen[index];
+
=
+    if (!ctx->cfg_ptr->ext_smstateen || env->priv =3D=3D PRV_M) {=
+        return true;
+    }
+
+  =
  if (ctx->virt_enabled) {
+        stateen &=3D env->h=
stateen[index];
+    }
+
+    if (env->pri=
v =3D=3D PRV_U && has_ext(ctx, RVS)) {
+        stateen &=
amp;=3D env->sstateen[index];
+    }
+
+  =
  if (!(stateen & SMSTATEEN0_FCSR)) {
+        if (ctx->vi=
rt_enabled) {
+            ctx->virt_inst_excp =3D true;
=
+        }
Are there any considerations for adding  virt_inst_excp instea=
d of
directly

"generate_exception(ctx, R=
ISCV_EXCP_VIRT_INSTRUCTION_FAULT)" here?

Regards,<=
/pre>

Weiwei Li
I had considered it but I think this is a simpler approach as th=
e rest
of the code path stays the same as generating an illegal i=
nstruction
exception, for e.g setting the bins value (tval).
    

OK,  we did need to set bins value for virt instruction exception. However I prefer directly call a

new gen_exception_virt function(similar to gen_exception_illegal) here.

 Also we need to
return true from all the caller trans=
_* functions even if the smstateen
check has failed.

False is returned when smstateen check fails currently.

Yes, however if we make this change then should return true if the= check fails so that the decode_opc doesnt fall through and generate anothe= r exception. It can be done but it would be contrary to the general convent= ion.

Regards,

Weiwei Li

=20
=20
+        return false;
+    }
+
+ return true;
+}
+#else
+#define smstat=
een_fcsr_check(ctx, index) (true)
+#endif
+
+=
#define REQUIRE_ZFINX_OR_F(ctx) do { \
+    if (!has_ext(ctx, RVF=
)) { \
+        if (!ctx->cfg_ptr->ext_zfinx) { \
+ return false; \
+        } \
+        if =
(!smstateen_fcsr_check(ctx, 0)) { \
+            return false; \<=
/pre>
+        } \
      } \
  } while (0)
diff --git a/target/riscv/insn_trans/trans_rvzfh.c.inc
=
b/target/riscv/insn_trans/trans_rvzfh.c.inc
index 5d07150cd0=
..6c2e338c0a 100644
--- a/target/riscv/insn_trans/trans_rvzfh.c.i=
nc
+++ b/target/riscv/insn_trans/trans_rvzfh.c.inc
@@ -=
20,18 +20,27 @@
      if (!ctx->cfg_ptr->ext_zfh) {      \<=
/pre>
          return false;         \
      }              =
           \
+    if (!smstateen_fcsr_check(ctx, 0)) { \
+ return false; \
+    } \
  } while (0)
=
  
  #define REQUIRE_ZHINX_OR_ZFH(ctx) do { \
    =
  if (!ctx->cfg_ptr->ext_zhinx && !ctx->cfg_ptr->ext_zf=
h) { \
          return false;                  \
     =
 }                                  \
+    if (!smstateen_fcsr_ch=
eck(ctx, 0)) { \
+        return false; \
+    } \
  } while (0)
  
  #define REQUIRE_ZFH_OR_ZFHMIN(=
ctx) do {       \
      if (!(ctx->cfg_ptr->ext_zfh || ctx-=
>cfg_ptr->ext_zfhmin)) { \
          return false;         =
                \
      }                                        =
 \
+    if (!smstateen_fcsr_check(ctx, 0)) { \
+       =
 return false; \
+    } \
  } while (0)
  
  #define REQUIRE_ZFH_OR_ZFHMIN_OR_ZHINX_OR_ZHINXMIN(ctx) do { \
@@ -39,6 +48,9 @@
            ctx->cfg_ptr->ext_zhi=
nx || ctx->cfg_ptr->ext_zhinxmin)) 
{     \
     =
     return false;                                        \
     =
 }                                                        \
+    =
if (!smstateen_fcsr_check(ctx, 0)) { \
+        return false; \
+    } \
  } while (0)
  
  static b=
ool trans_flh(DisasContext *ctx, arg_flh *a)
=20
--=-ok35VPksaYy2IEbOXLos--