On 17/11/26 20:26, Richard Henderson wrote:
On 11/13/26 08:11, Philippe Mathieu-Daudé wrote:
BNEC (rs == rt), BLTC (rs == rt != 0) and BLTUC (rs == rt) are NOPs,
but flagged the next insn as a forbidden slot. A following branch
then raised RI, and a fault there got a wrong EPC/Cause.BD. nanoMIPS
has neither delay nor forbidden slots (MD01247 p.12), so just fall
through.

Fixes: 11d0fc10b7ef ("target/mips: Add emulation of nanoMIPS 32-bit branch")
Fixes: 14668cfaaf4d ("target/mips: Fix emulation of nanoMIPS BNEC[32]")
Signed-off-by: Philippe Mathieu-Daudé <[email protected]>
---
  target/mips/tcg/nanomips_translate.c.inc | 18 ++++++------------
  1 file changed, 6 insertions(+), 12 deletions(-)

The problem would appear to be elsewhere: Why would gen_compute_branch_nm *ever* set forbidden slot, given that nanomips doesn't have them?

forbidden slots is not set in the branch helpers path but in the
decoder, see for example commit 14668cfaaf4d in the NOP path:

--- a/target/mips/tcg/nanomips_translate.c.inc
+++ b/target/mips/tcg/nanomips_translate.c.inc
@@ -4528,7 +4528,12 @@ static int decode_nanomips_32_48_opc(CPUMIPSState *env, DisasContext *ctx)
             switch (extract32(ctx->opcode, 14, 2)) {
             case NM_BNEC:
                 check_nms(ctx);
-                gen_compute_branch_nm(ctx, OPC_BNE, 4, rs, rt, s);
+                if (rs == rt) {
+                    /* NOP */
+                    ctx->hflags |= MIPS_HFLAG_FBNSLOT;
+                } else {
+                    gen_compute_branch_nm(ctx, OPC_BNE, 4, rs, rt, s);
+                }
                 break;

I'll reword for clarity as:

  The decoder NOP special-cases for BNEC/BLTC/BLTUC (rs == rt) set
  MIPS_HFLAG_FBNSLOT, an idiom imported from the R6 compact-branch
  decoder; nanoMIPS has neither delay nor forbidden slots (MD01247
  p.12). With the flag set, the next branch hit gen_branch()'s default
  case and raised RI, wrongly reported with Cause.BD set and EPC at
  the NOP branch (stale hflags snapshotted by save_cpu_state()).

  Fall through instead: no nanoMIPS path sets MIPS_HFLAG_FBNSLOT
  anymore.



r~


diff --git a/target/mips/tcg/nanomips_translate.c.inc b/target/mips/ tcg/nanomips_translate.c.inc
index 68e9ca9cdb03..72d72f6f84a0 100644
--- a/target/mips/tcg/nanomips_translate.c.inc
+++ b/target/mips/tcg/nanomips_translate.c.inc
@@ -4370,26 +4370,20 @@ static int decode_nanomips_32_48_opc(CPUMIPSState *env, DisasContext *ctx)
              switch (extract32(ctx->opcode, 14, 2)) {
              case NM_BNEC:
                  check_nms(ctx);
-                if (rs == rt) {
-                    /* NOP */
-                    ctx->hflags |= MIPS_HFLAG_FBNSLOT;
-                } else {
+                /* rs == rt is never taken: NOP */
+                if (rs != rt) {
                      gen_compute_branch_nm(ctx, OPC_BNE, 4, rs, rt, s);
                  }
                  break;
              case NM_BLTC:
-                if (rs != 0 && rt != 0 && rs == rt) {
-                    /* NOP */
-                    ctx->hflags |= MIPS_HFLAG_FBNSLOT;
-                } else {
+                /* rs == rt != 0 is never taken: NOP */
+                if (rs == 0 || rt == 0 || rs != rt) {
                      gen_compute_compact_branch_nm(ctx, OPC_BLTC, rs, rt, s);
                  }
                  break;
              case NM_BLTUC:
-                if (rs == rt) {
-                    /* NOP */
-                    ctx->hflags |= MIPS_HFLAG_FBNSLOT;
-                } else {
+                /* rs == rt is never taken: NOP */
+                if (rs != rt) {
                      gen_compute_compact_branch_nm(ctx, OPC_BLTUC, rs, rt, s);
                  }
                  break;




Reply via email to