Skip to contentSkip to conversation

target/mips: Mixed bag of fixes

PATCH In review Philippe Mathieu-Daudé 3 participants 52 messages Nov 13 – Nov 19

lore
Share & tools
lore
#0 lore raw

cover target/mips: Mixed bag of fixes

Various individual fixes noted while reviewing manuals during decodetree conversion, and using a RISU prototype.

Philippe Mathieu-Daudé (19):
target/mips: Check for MIPS II when decoding BLEZL/BGTZL target/mips: Require rt == 0 when decoding BLEZL/BGTZL target/mips: Require the MT module for MFTR/MTTR target/mips: Reject the PMON encoding on Release 6 target/mips: Enforce alignment and 64-bit CPU for the DSP LX loads target/mips: Fix reset into microMIPS mode when Config3.ISA is 3 target/mips: Fix microMIPS BC2NEZC POOL32I minor opcode target/mips: Fix microMIPS CACHE offset used for ITC tag access target/mips: Add the NMS check to the nanoMIPS CACHE instruction target/mips: Fix nanoMIPS BPOSGE32C branch offset sign extension target/mips: Pass the right instruction size for nanoMIPS RESTORE.JRC target/mips: Fix nanoMIPS BC1EQZC/BC1NEZC always raising RI target/mips: Fix nanoMIPS BEQC16/BNEC16 corner cases target/mips: Fix nanoMIPS BLTUC with rs == $zero target/mips: Pass the nanoMIPS BREAK code to
generate_exception_break()
target/mips: Decode 6-bit bit number of nanoMIPS BBEQZC/BBNEZC target/mips: Don't set forbidden slot on never-taken nanoMIPS branches target/mips: Do not sign-extend partial LWR results on Octeon target/mips: Do not negate NaN results in NMADD.fmt and NMSUB.fmt

 target/mips/cpu.c                         |  2 +-
 target/mips/tcg/fpu_helper.c              | 39 ++++++++---
 target/mips/tcg/translate.c               | 39 +++++++++--
 target/mips/tcg/micromips_translate.c.inc |  4 +-
 target/mips/tcg/nanomips_translate.c.inc  | 81 +++++++----------------
 5 files changed, 90 insertions(+), 75 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/19 target/mips: Check for MIPS II when decoding BLEZL/BGTZL

Branch-likely instructions were introduced in MIPS II; on MIPS I they signal Reserved Instruction. BEQL/BNEL and the REGIMM likely forms already check ISA_MIPS2, but BLEZL and BGTZL (non-R6) did not.

Fixes d9224450208e ("target-mips: Tighten ISA level checks")
---
 target/mips/tcg/translate.c | 2 ++
 1 file changed, 2 insertions(+)
target/mips/tcg/translate.c +2 -0
index 10811db827c9..f521e0ec5bd0 100644
@@ -14621,6 +14621,7 @@ static bool decode_opc_legacy(CPUMIPSState *env, DisasContext *ctx)
             gen_compute_compact_branch(ctx, op, rs, rt, imm << 2);
         } else {
             /* OPC_BLEZL */
+            check_insn(ctx, ISA_MIPS2);
             gen_compute_branch(ctx, op, 4, rs, rt, imm << 2, 4);
         }
         break;
@@ -14634,6 +14635,7 @@ static bool decode_opc_legacy(CPUMIPSState *env, DisasContext *ctx)
             gen_compute_compact_branch(ctx, op, rs, rt, imm << 2);
         } else {
             /* OPC_BGTZL */
+            check_insn(ctx, ISA_MIPS2);
             gen_compute_branch(ctx, op, 4, rs, rt, imm << 2, 4);
         }
         break;
Signature · 1 line
2.55.0
Reviewed-by on v1 1/19
#2 lore raw

Re: [PATCH 01/19] target/mips: Check for MIPS II when decoding BLEZL/BGTZL

Quoted 23 lines · Philippe Mathieu-Daudé

Branch-likely instructions were introduced in MIPS II; on MIPS I they signal Reserved Instruction. BEQL/BNEL and the REGIMM likely forms already check ISA_MIPS2, but BLEZL and BGTZL (non-R6) did not.

Fixes d9224450208e ("target-mips: Tighten ISA level checks")
---
  target/mips/tcg/translate.c | 2 ++
  1 file changed, 2 insertions(+)
target/mips/tcg/translate.c +1 -0
index 10811db827c9..f521e0ec5bd0 100644
@@ -14621,6 +14621,7 @@ static bool decode_opc_legacy(CPUMIPSState *env, DisasContext *ctx)
              gen_compute_compact_branch(ctx, op, rs, rt, imm << 2);
          } else {
              /* OPC_BLEZL */
+            check_insn(ctx, ISA_MIPS2);
              gen_compute_branch(ctx, op, 4, rs, rt, imm << 2, 4);
          }
          break;

target/mips/tcg/translate.c static bool decode_opc_legacy(CPUMIPSState *env, DisasContext *ctx) · line 14641

Quoted 7 lines
target/mips/tcg/translate.c +1 -0
@@ -14634,6 +14635,7 @@ static bool decode_opc_legacy(CPUMIPSState *env, DisasContext *ctx)
              gen_compute_compact_branch(ctx, op, rs, rt, imm << 2);
          } else {
              /* OPC_BGTZL */
+            check_insn(ctx, ISA_MIPS2);
              gen_compute_branch(ctx, op, 4, rs, rt, imm << 2, 4);
          }
          break;

r~

#3 lore raw

PATCH 2/19 target/mips: Require rt == 0 when decoding BLEZL/BGTZL

In reply to Philippe Mathieu-Daudé

The BLEZL and BGTZL encodings fix rt to 00000. The legacy decoder ignored rt on non-R6 CPUs, executing reserved encodings as BLEZL/BGTZL instead of raising Reserved Instruction. On R6 these opcodes are the compact branches, which already reject rt == 0.

---
 target/mips/tcg/translate.c | 8 ++++++++
 1 file changed, 8 insertions(+)
target/mips/tcg/translate.c +8 -0
index f521e0ec5bd0..6e7e1a0f758e 100644
@@ -14621,6 +14621,10 @@ static bool decode_opc_legacy(CPUMIPSState *env, DisasContext *ctx)
             gen_compute_compact_branch(ctx, op, rs, rt, imm << 2);
         } else {
             /* OPC_BLEZL */
+            if (rt != 0) {
+                gen_reserved_instruction(ctx);
+                break;
+            }
             check_insn(ctx, ISA_MIPS2);
             gen_compute_branch(ctx, op, 4, rs, rt, imm << 2, 4);
         }
@@ -14635,6 +14639,10 @@ static bool decode_opc_legacy(CPUMIPSState *env, DisasContext *ctx)
             gen_compute_compact_branch(ctx, op, rs, rt, imm << 2);
         } else {
             /* OPC_BGTZL */
+            if (rt != 0) {
+                gen_reserved_instruction(ctx);
+                break;
+            }
             check_insn(ctx, ISA_MIPS2);
             gen_compute_branch(ctx, op, 4, rs, rt, imm << 2, 4);
         }
Signature · 1 line
2.55.0
Reviewed-by on v1 2/19
#4 lore raw

Re: [PATCH 02/19] target/mips: Require rt == 0 when decoding BLEZL/BGTZL

Quoted 10 lines · Philippe Mathieu-Daudé

The BLEZL and BGTZL encodings fix rt to 00000. The legacy decoder ignored rt on non-R6 CPUs, executing reserved encodings as BLEZL/BGTZL instead of raising Reserved Instruction. On R6 these opcodes are the compact branches, which already reject rt == 0.

---
  target/mips/tcg/translate.c | 8 ++++++++
  1 file changed, 8 insertions(+)

r~

#5 lore raw

PATCH 3/19 target/mips: Require the MT module for MFTR/MTTR

In reply to Philippe Mathieu-Daudé

MFTR/MTTR must raise Reserved Instruction on processors not implementing the MT module (MD00378 pp.46/50, Config3.MT:
table 6.21). Use check_cp0_mt(), as done for the other MT instructions, instead of only check_cp0_enabled().

Fixes ead9360e2fbc ("Partial support for 34K multithreading")
---
 target/mips/tcg/translate.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/mips/tcg/translate.c +2 -2
index 6e7e1a0f758e..c434da714b0c 100644
@@ -8588,7 +8588,7 @@ static void gen_cp0(CPUMIPSState *env, DisasContext *ctx, uint32_t opc,
         opn = "mthc0";
         break;
     case OPC_MFTR:
-        check_cp0_enabled(ctx);
+        check_cp0_mt(ctx);
         if (rd == 0) {
             /* Treat as NOP. */
             return;
@@ -8598,7 +8598,7 @@ static void gen_cp0(CPUMIPSState *env, DisasContext *ctx, uint32_t opc,
         opn = "mftr";
         break;
     case OPC_MTTR:
-        check_cp0_enabled(ctx);
+        check_cp0_mt(ctx);
         gen_mttr(env, ctx, rd, rt, (ctx->opcode >> 5) & 1,
                  ctx->opcode & 0x7, (ctx->opcode >> 4) & 1);
         opn = "mttr";
Signature · 1 line
2.55.0
#6 lore raw

Re: [PATCH 03/19] target/mips: Require the MT module for MFTR/MTTR

Quoted 10 lines · Philippe Mathieu-Daudé

MFTR/MTTR must raise Reserved Instruction on processors not implementing the MT module (MD00378 pp.46/50, Config3.MT:
table 6.21). Use check_cp0_mt(), as done for the other MT instructions, instead of only check_cp0_enabled().

Fixes ead9360e2fbc ("Partial support for 34K multithreading")
---
  target/mips/tcg/translate.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)

Surely check_cp0_mt must be enabled for user-only, not sit behind an ifdef.

r~

Quoted 23 lines
target/mips/tcg/translate.c +2 -2
index 6e7e1a0f758e..c434da714b0c 100644
@@ -8588,7 +8588,7 @@ static void gen_cp0(CPUMIPSState *env, DisasContext *ctx, uint32_t opc,
          opn = "mthc0";
          break;
      case OPC_MFTR:
-        check_cp0_enabled(ctx);
+        check_cp0_mt(ctx);
          if (rd == 0) {
              /* Treat as NOP. */
              return;
@@ -8598,7 +8598,7 @@ static void gen_cp0(CPUMIPSState *env, DisasContext *ctx, uint32_t opc,
          opn = "mftr";
          break;
      case OPC_MTTR:
-        check_cp0_enabled(ctx);
+        check_cp0_mt(ctx);
          gen_mttr(env, ctx, rd, rt, (ctx->opcode >> 5) & 1,
                   ctx->opcode & 0x7, (ctx->opcode >> 4) & 1);
          opn = "mttr";
#7 lore raw

Re: [PATCH 03/19] target/mips: Require the MT module for MFTR/MTTR

Quoted 6 lines · Richard Henderson
Quoted 5 lines · Philippe Mathieu-Daudé

MFTR/MTTR must raise Reserved Instruction on processors not implementing the MT module (MD00378 pp.46/50, Config3.MT:
table 6.21). Use check_cp0_mt(), as done for the other MT instructions, instead of only check_cp0_enabled().

Quoted 8 lines
Quoted 5 lines
Fixes ead9360e2fbc ("Partial support for 34K multithreading")

---
  target/mips/tcg/translate.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)

Surely check_cp0_mt must be enabled for user-only, not sit behind an ifdef.

I suppose I got lucky, MIPS_HFLAG_CP0 is never set in user-mode so the OPC_CP0 dispatch already raises CpU before any MT check could apply.

I'll remove the #ifdef (and fix the Config5/Config3 typo in comment), thanks!

Quoted 3 lines

r~

#8 lore raw

Re: [PATCH 03/19] target/mips: Require the MT module for MFTR/MTTR

Quoted 13 lines · Philippe Mathieu-Daudé
Quoted 12 lines · Richard Henderson
Quoted 10 lines · Philippe Mathieu-Daudé

MFTR/MTTR must raise Reserved Instruction on processors not implementing the MT module (MD00378 pp.46/50, Config3.MT:
table 6.21). Use check_cp0_mt(), as done for the other MT instructions, instead of only check_cp0_enabled().

Fixes ead9360e2fbc ("Partial support for 34K multithreading")

---
  target/mips/tcg/translate.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 8 lines
Quoted 2 lines

Surely check_cp0_mt must be enabled for user-only, not sit behind an ifdef.

I suppose I got lucky, MIPS_HFLAG_CP0 is never set in user-mode so the OPC_CP0 dispatch already raises CpU before any MT check could apply.

I'll remove the #ifdef (and fix the Config5/Config3 typo in comment), thanks!

I meant, check_cp0_mt is undefined for user-only.
How does this compile?

r~

#9 lore raw

Re: [PATCH 03/19] target/mips: Require the MT module for MFTR/MTTR

Quoted 17 lines · Richard Henderson
Quoted 16 lines · Philippe Mathieu-Daudé
Quoted 14 lines · Richard Henderson
Quoted 10 lines · Philippe Mathieu-Daudé

MFTR/MTTR must raise Reserved Instruction on processors not implementing the MT module (MD00378 pp.46/50, Config3.MT:
table 6.21). Use check_cp0_mt(), as done for the other MT instructions, instead of only check_cp0_enabled().

Fixes ead9360e2fbc ("Partial support for 34K multithreading")

---
  target/mips/tcg/translate.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)

Surely check_cp0_mt must be enabled for user-only, not sit behind an ifdef.

Quoted 7 lines
Quoted 5 lines

I suppose I got lucky, MIPS_HFLAG_CP0 is never set in user-mode so the OPC_CP0 dispatch already raises CpU before any MT check could apply.

I'll remove the #ifdef (and fix the Config5/Config3 typo in comment), thanks!

I meant, check_cp0_mt is undefined for user-only.
How does this compile?

It doesn't have to :) gen_cp0() is itself compiled out for CONFIG_USER_ONLY -- the #ifndef CONFIG_USER_ONLY region holding the CP0 helpers (opened at translate.c:4875) is closed right after gen_cp0() at :8717 -- so the new call sites only exist in system builds, matching check_cp0_mt()'s own #ifndef.

Still, check_mt() is available to all builds and there is no technical reason to hide check_cp0_mt() (its body doesn't use anything system-only), so I'm moving it out of the #ifdef as announced, along with the Config5 -> Config3 comment fix.

#10 lore raw

PATCH 4/19 target/mips: Reject the PMON encoding on Release 6

In reply to Philippe Mathieu-Daudé

On R6, SPECIAL funct 0x05 is LSA, which decodetree matches only when bits 10:8 are zero. Other encodings fell through to the non-standard PMON hook instead of raising Reserved Instruction.

Fixes 0e9524af2dd5 ("target/mips: Remove unreachable [D]LSA code")
---
 target/mips/tcg/translate.c | 4 ++++
 1 file changed, 4 insertions(+)
target/mips/tcg/translate.c +4 -0
index c434da714b0c..d1be32bfd9f5 100644
@@ -13301,6 +13301,10 @@ static void decode_opc_special(CPUMIPSState *env, DisasContext *ctx)
         MIPS_INVAL("PMON / selsl");
         gen_reserved_instruction(ctx);
 #else
+        if (ctx->insn_flags & ISA_MIPS_R6) {
+            gen_reserved_instruction(ctx);
+            break;
+        }
         gen_helper_pmon(tcg_env, tcg_constant_i32(sa));
 #endif
         break;
Signature · 1 line
2.55.0
Reviewed-by on v1 4/19
#11 lore raw

Re: [PATCH 04/19] target/mips: Reject the PMON encoding on Release 6

Quoted 15 lines · Philippe Mathieu-Daudé

On R6, SPECIAL funct 0x05 is LSA, which decodetree matches only when bits 10:8 are zero. Other encodings fell through to the non-standard PMON hook instead of raising Reserved Instruction.

Fixes 0e9524af2dd5 ("target/mips: Remove unreachable [D]LSA code")
---
  target/mips/tcg/translate.c | 4 ++++
  1 file changed, 4 insertions(+)
target/mips/tcg/translate.c +0 -0
index c434da714b0c..d1be32bfd9f5 100644

target/mips/tcg/translate.c static void decode_opc_special(CPUMIPSState *env, DisasContext *ctx) · line 13310

Quoted 10 lines
target/mips/tcg/translate.c +4 -0
@@ -13301,6 +13301,10 @@ static void decode_opc_special(CPUMIPSState *env, DisasContext *ctx)
          MIPS_INVAL("PMON / selsl");
          gen_reserved_instruction(ctx);
  #else
+        if (ctx->insn_flags & ISA_MIPS_R6) {
+            gen_reserved_instruction(ctx);
+            break;
+        }
          gen_helper_pmon(tcg_env, tcg_constant_i32(sa));
  #endif
          break;

Depending on how long this lives, perhaps better as a REMOVED line in rel6.decode to catch this instead?

Anyway,

r~

#12 lore raw

PATCH 5/19 target/mips: Enforce alignment and 64-bit CPU for the DSP LX loads

In reply to Philippe Mathieu-Daudé

The DSP LHX, LWX and LDX loads require a naturally aligned address and raise Address Error otherwise (MD00374 r3.01 p.130-131, MD00375 r3.02 p.145). Use the default memop alignment mask for them (strict, except under linux-user FIXADE); LBUX is a byte access. The Octeon LX forms share gen_lx() and are unchanged.

LDX belongs to the MIPS64 DSP module only, but was decoded on any DSP CPU in a 64-bit build, including 32-bit ones such as the 24KE or 74K. Call check_mips_64() after the DSP Disabled check to raise Reserved Instruction there.

Fixes 9b1a1d68d041 ("target-mips: Add ASE DSP load instructions")
---
 target/mips/tcg/translate.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
target/mips/tcg/translate.c +4 -3
index d1be32bfd9f5..6966b7e56abb 100644
@@ -13653,17 +13653,18 @@ static void decode_opc_special3_legacy(CPUMIPSState *env, DisasContext *ctx)
         switch (op2) {
 #if defined(TARGET_MIPS64)
         case OPC_LDX:
-            gen_lx(ctx, rd, rs, rt, MO_UQ);
+            check_mips_64(ctx);
+            gen_lx(ctx, rd, rs, rt, MO_UQ | ctx->default_tcg_memop_mask);
             break;
 #endif
         case OPC_LBUX:
             gen_lx(ctx, rd, rs, rt, MO_UB);
             break;
         case OPC_LHX:
-            gen_lx(ctx, rd, rs, rt, MO_SW);
+            gen_lx(ctx, rd, rs, rt, MO_SW | ctx->default_tcg_memop_mask);
             break;
         case OPC_LWX:
-            gen_lx(ctx, rd, rs, rt, MO_SL);
+            gen_lx(ctx, rd, rs, rt, MO_SL | ctx->default_tcg_memop_mask);
             break;
         default:            /* Invalid */
             MIPS_INVAL("MASK LX");
Signature · 1 line
2.55.0
Reviewed-by on v1 5/19
#13 lore raw

Re: [PATCH 05/19] target/mips: Enforce alignment and 64-bit CPU for the DSP LX loads

Quoted 11 lines · Philippe Mathieu-Daudé

The DSP LHX, LWX and LDX loads require a naturally aligned address and raise Address Error otherwise (MD00374 r3.01 p.130-131, MD00375 r3.02 p.145). Use the default memop alignment mask for them (strict, except under linux-user FIXADE); LBUX is a byte access. The Octeon LX forms share gen_lx() and are unchanged.

LDX belongs to the MIPS64 DSP module only, but was decoded on any DSP CPU in a 64-bit build, including 32-bit ones such as the 24KE or 74K. Call check_mips_64() after the DSP Disabled check to raise Reserved Instruction there.

Quoted 5 lines
Fixes 9b1a1d68d041 ("target-mips: Add ASE DSP load instructions")
---
  target/mips/tcg/translate.c | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)

r~

#14 lore raw

PATCH 6/19 target/mips: Fix reset into microMIPS mode when Config3.ISA is 3

In reply to Philippe Mathieu-Daudé

The Config3.ISA == 3 test ANDs bit 14 and bit 15 as two separate constants, so it is always false and no CPU resets into microMIPS mode. Test the 2-bit field instead.

Config3.ISA == 3: both MIPS32 and microMIPS32 are implemented, with microMIPS32 used out of reset (MD00090 rev 6.02, Table 9.67, p. 248).

Fixes 0bbc0396809f ("target/mips: Prevent switching mode Config3 ISA bit nanoMIPS")
---
 target/mips/cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/mips/cpu.c +1 -1
index 8ad5e49ade41..324e5989cca1 100644
@@ -459,7 +459,7 @@ static void mips_cpu_reset_hold(Object *obj, ResetType type)
         env->CP0_PWField = 0x02;
     }
 
-    if (env->CP0_Config3 & (1 << CP0C3_ISA) & (1 << (CP0C3_ISA + 1))) {
+    if (extract32(env->CP0_Config3, CP0C3_ISA, 2) == 3) {
         /*  microMIPS on reset when Config3.ISA is 3 */
         env->hflags |= MIPS_HFLAG_M16;
     }
Signature · 1 line
2.55.0
Reviewed-by on v1 6/19
#15 lore raw

Re: [PATCH 06/19] target/mips: Fix reset into microMIPS mode when Config3.ISA is 3

Quoted 4 lines · Philippe Mathieu-Daudé

The Config3.ISA == 3 test ANDs bit 14 and bit 15 as two separate constants, so it is always false and no CPU resets into microMIPS mode. Test the 2-bit field instead.

Quoted 8 lines

Config3.ISA == 3: both MIPS32 and microMIPS32 are implemented, with microMIPS32 used out of reset (MD00090 rev 6.02, Table 9.67, p. 248).

Fixes 0bbc0396809f ("target/mips: Prevent switching mode Config3 ISA bit nanoMIPS")
---
  target/mips/cpu.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

r~

#16 lore raw

PATCH 7/19 target/mips: Fix microMIPS BC2NEZC POOL32I minor opcode

In reply to Philippe Mathieu-Daudé

BC2NEZC is POOL32I minor opcode 0x0b (bits 25:21 = 01011); 0x0a is BC2EQZC. See MD00582 rev 6.04 p. 131 and MD00594 rev 6.04 p. 145.

Both constants are unreferenced, so there is no functional change.

Fixes 3a1f426828cd ("target-mips: add microMIPS32 R6 opcode enum")
---
 target/mips/tcg/micromips_translate.c.inc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/mips/tcg/micromips_translate.c.inc +1 -1
index d6a5c2263b56..4ad2b699615e 100644
@@ -513,7 +513,7 @@ enum {
     TLTIU = 0x0a,
     BC2EQZC = 0x0a,
     TGEIU = 0x0b,
-    BC2NEZC = 0x0a,
+    BC2NEZC = 0x0b,
     TNEI = 0x0c,
     R6_SYNCI = 0x0c,
     LUI = 0x0d,
Signature · 1 line
2.55.0
Reviewed-by on v1 7/19
#17 lore raw

Re: [PATCH 07/19] target/mips: Fix microMIPS BC2NEZC POOL32I minor opcode

Quoted 11 lines · Philippe Mathieu-Daudé

BC2NEZC is POOL32I minor opcode 0x0b (bits 25:21 = 01011); 0x0a is BC2EQZC. See MD00582 rev 6.04 p. 131 and MD00594 rev 6.04 p. 145.

Both constants are unreferenced, so there is no functional change.

Fixes 3a1f426828cd ("target-mips: add microMIPS32 R6 opcode enum")
---
  target/mips/tcg/micromips_translate.c.inc | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

r~

#18 lore raw

PATCH 8/19 target/mips: Fix microMIPS CACHE offset used for ITC tag access

In reply to Philippe Mathieu-Daudé

CACHE is encoded in POOL32B with a 9-bit signed offset in bits 8:0 (MD00582 rev 6.04, p. 144). The decoder passed the generic 'imm' (the low 16 bits of the opcode), which also holds the minor opcode and reserved bits 15:9, so the ITC tag access used a wrong effective address. Extract the 9-bit offset, as done for CACHEE.

Fixes 0d74a222c27e ("target-mips: make ITC Configuration Tags accessible")
---
 target/mips/tcg/micromips_translate.c.inc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/mips/tcg/micromips_translate.c.inc +1 -1
index 4ad2b699615e..21374e3ec60f 100644
@@ -1820,7 +1820,7 @@ static void decode_micromips32_opc(CPUMIPSState *env, DisasContext *ctx)
         case CACHE:
             check_cp0_enabled(ctx);
             if (ctx->hflags & MIPS_HFLAG_ITC_CACHE) {
-                gen_cache_operation(ctx, rt, rs, imm);
+                gen_cache_operation(ctx, rt, rs, SIMM(ctx->opcode, 0, 9));
             }
             break;
         case LWC2:
Signature · 1 line
2.55.0
Reviewed-by on v1 8/19
#19 lore raw

Re: [PATCH 08/19] target/mips: Fix microMIPS CACHE offset used for ITC tag access

Quoted 11 lines · Philippe Mathieu-Daudé

CACHE is encoded in POOL32B with a 9-bit signed offset in bits 8:0 (MD00582 rev 6.04, p. 144). The decoder passed the generic 'imm' (the low 16 bits of the opcode), which also holds the minor opcode and reserved bits 15:9, so the ITC tag access used a wrong effective address. Extract the 9-bit offset, as done for CACHEE.

Fixes 0d74a222c27e ("target-mips: make ITC Configuration Tags accessible")
---
  target/mips/tcg/micromips_translate.c.inc | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

r~

#20 lore raw

PATCH 9/19 target/mips: Add the NMS check to the nanoMIPS CACHE instruction

In reply to Philippe Mathieu-Daudé

CACHE and CACHEE share pseudo-code raising Reserved Instruction on NMS cores without caches, but only CACHEE performed that check. See MD01247 rev 01.01, "CACHE/CACHEE" (printed page 63).

Fixes 8f1d9b6d6397 ("target/mips: Add emulation of nanoMIPS 32-bit LD/ST")
---
 target/mips/tcg/nanomips_translate.c.inc | 1 +
 1 file changed, 1 insertion(+)
target/mips/tcg/nanomips_translate.c.inc +1 -0
index c883b5514f1c..20e96f2ad9be 100644
@@ -4131,6 +4131,7 @@ static int decode_nanomips_32_48_opc(CPUMIPSState *env, DisasContext *ctx)
                     }
                     break;
                 case NM_CACHE:
+                    check_nms_dl_il_sl_tl_l2c(ctx);
                     check_cp0_enabled(ctx);
                     if (ctx->hflags & MIPS_HFLAG_ITC_CACHE) {
                         gen_cache_operation(ctx, rt, rs, s);
Signature · 1 line
2.55.0
Reviewed-by on v1 9/19
#21 lore raw

Re: [PATCH 09/19] target/mips: Add the NMS check to the nanoMIPS CACHE instruction

Quoted 15 lines · Philippe Mathieu-Daudé

CACHE and CACHEE share pseudo-code raising Reserved Instruction on NMS cores without caches, but only CACHEE performed that check. See MD01247 rev 01.01, "CACHE/CACHEE" (printed page 63).

Fixes 8f1d9b6d6397 ("target/mips: Add emulation of nanoMIPS 32-bit LD/ST")
---
  target/mips/tcg/nanomips_translate.c.inc | 1 +
  1 file changed, 1 insertion(+)
target/mips/tcg/nanomips_translate.c.inc +0 -0
index c883b5514f1c..20e96f2ad9be 100644

target/mips/tcg/nanomips_translate.c.inc static int decode_nanomips_32_48_opc(CPUMIPSState *env, DisasContext *ctx) · line 4137

Quoted 7 lines
target/mips/tcg/nanomips_translate.c.inc +1 -0
@@ -4131,6 +4131,7 @@ static int decode_nanomips_32_48_opc(CPUMIPSState *env, DisasContext *ctx)
                      }
                      break;
                  case NM_CACHE:
+                    check_nms_dl_il_sl_tl_l2c(ctx);
                      check_cp0_enabled(ctx);
                      if (ctx->hflags & MIPS_HFLAG_ITC_CACHE) {
                          gen_cache_operation(ctx, rt, rs, s);

Also missing check_eva.

Otherwise,

r~

#22 lore raw

Re: [PATCH 09/19] target/mips: Add the NMS check to the nanoMIPS CACHE instruction

Quoted 11 lines · Richard Henderson
Quoted 10 lines · Philippe Mathieu-Daudé

CACHE and CACHEE share pseudo-code raising Reserved Instruction on NMS cores without caches, but only CACHEE performed that check. See MD01247 rev 01.01, "CACHE/CACHEE" (printed page 63).

Fixes 8f1d9b6d6397 ("target/mips: Add emulation of nanoMIPS 32-bit LD/ST")

---
  target/mips/tcg/nanomips_translate.c.inc | 1 +

  1 file changed, 1 insertion(+)
Quoted 15 lines
Quoted 8 lines
target/mips/tcg/nanomips_translate.c.inc +0 -0
index c883b5514f1c..20e96f2ad9be 100644
@@ -4131,6 +4131,7 @@ static int 
 decode_nanomips_32_48_opc(CPUMIPSState *env, DisasContext *ctx)
                       }
                       break;
Quoted 5 lines

                  case NM_CACHE:
+                    check_nms_dl_il_sl_tl_l2c(ctx); check_cp0_enabled(ctx);
                      if (ctx->hflags & MIPS_HFLAG_ITC_CACHE) { gen_cache_operation(ctx, rt, rs, s);

Also missing check_eva.

Hmm looking further commit 84c2fdc397b6 ("target/mips: Fix CACHEE opcode (CACHE using EVA addressing)") added the check at an invalid position; we check EVA -> CP0 -> NMS; the spec checks NMS -> EVA -> CP0 (in MD01247 rev 01.01 p.63 pseudo-code block). On a core with NMS=1, no caches, EVA=1 and CP0 disabled, the spec says RI while we deliver Coprocessor Unusable.

#23 lore raw

Re: [PATCH 09/19] target/mips: Add the NMS check to the nanoMIPS CACHE instruction

Quoted 12 lines · Philippe Mathieu-Daudé
Quoted 11 lines · Richard Henderson
Quoted 10 lines · Philippe Mathieu-Daudé

CACHE and CACHEE share pseudo-code raising Reserved Instruction on NMS cores without caches, but only CACHEE performed that check. See MD01247 rev 01.01, "CACHE/CACHEE" (printed page 63).

Fixes 8f1d9b6d6397 ("target/mips: Add emulation of nanoMIPS 32-bit LD/ST")

---
  target/mips/tcg/nanomips_translate.c.inc | 1 +

  1 file changed, 1 insertion(+)
Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
target/mips/tcg/nanomips_translate.c.inc +0 -0
index c883b5514f1c..20e96f2ad9be 100644
@@ -4131,6 +4131,7 @@ static int 
 decode_nanomips_32_48_opc(CPUMIPSState *env, DisasContext *ctx)
                       }
Quoted 8 lines
Quoted 6 lines
target/mips/tcg/nanomips_translate.c.inc +0 -0
                       break;

                  case NM_CACHE:
+                    check_nms_dl_il_sl_tl_l2c(ctx); check_cp0_enabled(ctx);
                      if (ctx->hflags & MIPS_HFLAG_ITC_CACHE) { gen_cache_operation(ctx, rt, rs, s);

Also missing check_eva.

The EVA check in the shared pseudo-code is gated by the per-format is_eva flag -- CACHE sets is_eva = False, CACHEE sets is_eva = True (MD01247 rev 01.01 p.63): "if is_eva and not C0.Config5.EVA: raise exception('RI')". For plain CACHE it is dead (never raises RI), and check_eva() would wrongly raise RI on cores without Config5.EVA.

I'll post the NM_CACHEE reorder as a follow-up.

Quoted 7 lines

Hmm looking further commit 84c2fdc397b6 ("target/mips: Fix CACHEE opcode (CACHE using EVA addressing)") added the check at an invalid position; we check EVA -> CP0 -> NMS; the spec checks NMS -> EVA -> CP0 (in MD01247 rev 01.01 p.63 pseudo-code block). On a core with NMS=1, no caches, EVA=1 and CP0 disabled, the spec says RI while we deliver Coprocessor Unusable.

#24 lore raw

PATCH 10/19 target/mips: Fix nanoMIPS BPOSGE32C branch offset sign extension

In reply to Philippe Mathieu-Daudé

The BPOSGE32C offset is a signed value s[14:1], with s[14] stored in instruction bit 0 (MD01249 rev 0.04, "BPOSGE32C", printed page 88). The decoder zero-extended it, so backward branches went to the wrong target.

Reuse the sign-extended offset already computed by the P.BR3A pool.

Fixes 6d033ca7513d ("target/mips: Add emulation of DSP ASE for nanoMIPS")
---
 target/mips/tcg/nanomips_translate.c.inc | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
target/mips/tcg/nanomips_translate.c.inc +1 -7
index 20e96f2ad9be..3e326afd0827 100644
@@ -4365,13 +4365,7 @@ static int decode_nanomips_32_48_opc(CPUMIPSState *env, DisasContext *ctx)
                     break;
                 case NM_BPOSGE32C:
                     check_dsp_r3(ctx);
-                    {
-                        imm = extract32(ctx->opcode, 1, 13)
-                            | extract32(ctx->opcode, 0, 1) << 13;
-
-                        gen_compute_branch_nm(ctx, OPC_BPOSGE32, 4, -1, -2,
-                                              imm << 1);
-                    }
+                    gen_compute_branch_nm(ctx, OPC_BPOSGE32, 4, -1, -2, s);
                     break;
                 default:
                     gen_reserved_instruction(ctx);
Signature · 1 line
2.55.0
Reviewed-by on v1 10/19
#25 lore raw

Re: [PATCH 10/19] target/mips: Fix nanoMIPS BPOSGE32C branch offset sign extension

Quoted 5 lines · Philippe Mathieu-Daudé

The BPOSGE32C offset is a signed value s[14:1], with s[14] stored in instruction bit 0 (MD01249 rev 0.04, "BPOSGE32C", printed page 88). The decoder zero-extended it, so backward branches went to the wrong target.

Quoted 7 lines

Reuse the sign-extended offset already computed by the P.BR3A pool.

Fixes 6d033ca7513d ("target/mips: Add emulation of DSP ASE for nanoMIPS")
---
  target/mips/tcg/nanomips_translate.c.inc | 8 +-------
  1 file changed, 1 insertion(+), 7 deletions(-)

r~

#26 lore raw

PATCH 11/19 target/mips: Pass the right instruction size for nanoMIPS RESTORE.JRC

In reply to Philippe Mathieu-Daudé

RESTORE.JRC[32] is a 32-bit instruction (MD01247, printed page 155), but gen_compute_branch_nm() was called with an instruction size of 2 (copy/paste from the 16-bit form), which wrongly set MIPS_HFLAG_B16.

No functional effect: gen_branch() clears MIPS_HFLAG_B16 before anything reads it.

Fixes c02809830346 ("target/mips: Add emulation of some nanoMIPS 32-bit instr")
---
 target/mips/tcg/nanomips_translate.c.inc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/mips/tcg/nanomips_translate.c.inc +1 -1
index 3e326afd0827..5941889b617b 100644
@@ -3745,7 +3745,7 @@ static int decode_nanomips_32_48_opc(CPUMIPSState *env, DisasContext *ctx)
                                 extract32(ctx->opcode, 2, 1),
                                 extract32(ctx->opcode, 3, 9) << 3);
                     if ((ctx->opcode & 3) == NM_RESTORE_JRC) {
-                        gen_compute_branch_nm(ctx, OPC_JR, 2, 31, 0, 0);
+                        gen_compute_branch_nm(ctx, OPC_JR, 4, 31, 0, 0);
                     }
                     break;
                 default:
Signature · 1 line
2.55.0
Reviewed-by on v1 11/19
#27 lore raw

Re: [PATCH 11/19] target/mips: Pass the right instruction size for nanoMIPS RESTORE.JRC

Quoted 4 lines · Philippe Mathieu-Daudé

RESTORE.JRC[32] is a 32-bit instruction (MD01247, printed page 155), but gen_compute_branch_nm() was called with an instruction size of 2 (copy/paste from the 16-bit form), which wrongly set MIPS_HFLAG_B16.

Quoted 8 lines

No functional effect: gen_branch() clears MIPS_HFLAG_B16 before anything reads it.

Fixes c02809830346 ("target/mips: Add emulation of some nanoMIPS 32-bit instr")
---
  target/mips/tcg/nanomips_translate.c.inc | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

r~

#28 lore raw

PATCH 12/19 target/mips: Fix nanoMIPS BC1EQZC/BC1NEZC always raising RI

In reply to Philippe Mathieu-Daudé

The P.BR3A decoder passes OPC_BC1EQZ / OPC_BC1NEZ (the MIPS CP1 opcodes) to gen_compute_branch_cp1_nm(), but the function switches on the nanoMIPS pool values NM_BC1EQZC (0) and NM_BC1NEZC (1). No call could match, so both instructions raised Reserved Instruction.

The P.BR3A encoding is in MD01247 rev 01.01, printed page 288. Switch on the values the callers pass.

Fixes 11d0fc10b7ef ("target/mips: Add emulation of nanoMIPS 32-bit branch")
---
 target/mips/tcg/nanomips_translate.c.inc | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/mips/tcg/nanomips_translate.c.inc +2 -2
index 5941889b617b..f9dc573dbb01 100644
@@ -2558,11 +2558,11 @@ static void gen_compute_branch_cp1_nm(DisasContext *ctx, uint32_t op,
     btarget = addr_add(ctx, ctx->base.pc_next + 4, offset);
 
     switch (op) {
-    case NM_BC1EQZC:
+    case OPC_BC1EQZ:
         tcg_gen_xori_i64(t0, t0, 1);
         ctx->hflags |= MIPS_HFLAG_BC;
         break;
-    case NM_BC1NEZC:
+    case OPC_BC1NEZ:
         /* t0 already set */
         ctx->hflags |= MIPS_HFLAG_BC;
         break;
Signature · 1 line
2.55.0
Reviewed-by on v1 12/19
#29 lore raw

Re: [PATCH 12/19] target/mips: Fix nanoMIPS BC1EQZC/BC1NEZC always raising RI

Quoted 5 lines · Philippe Mathieu-Daudé

The P.BR3A decoder passes OPC_BC1EQZ / OPC_BC1NEZ (the MIPS CP1 opcodes) to gen_compute_branch_cp1_nm(), but the function switches on the nanoMIPS pool values NM_BC1EQZC (0) and NM_BC1NEZC (1). No call could match, so both instructions raised Reserved Instruction.

Quoted 8 lines

The P.BR3A encoding is in MD01247 rev 01.01, printed page 288. Switch on the values the callers pass.

Fixes 11d0fc10b7ef ("target/mips: Add emulation of nanoMIPS 32-bit branch")
---
  target/mips/tcg/nanomips_translate.c.inc | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)

r~

#30 lore raw

PATCH 13/19 target/mips: Fix nanoMIPS BEQC16/BNEC16 corner cases

In reply to Philippe Mathieu-Daudé

BNEC[16] with identical registers is never taken, so it is a NOP. gen_compute_branch_nm() instead wrote $ra and skipped the next 4 bytes as if it were a MIPS branch-and-link-likely.

Also raise Reserved Instruction on NMS cores, where the 16-bit forms are not available.

Fixes 764371d24c51 ("target/mips: Add emulation of nanoMIPS 16-bit branch")
---
 target/mips/tcg/nanomips_translate.c.inc | 4 ++++
 1 file changed, 4 insertions(+)
target/mips/tcg/nanomips_translate.c.inc +4 -0
index f9dc573dbb01..25d57e75a0b0 100644
@@ -4756,6 +4756,10 @@ static int decode_isa_nanomips(CPUMIPSState *env, DisasContext *ctx)
                 /* P16.BRI */
                 uint32_t opc = extract32(ctx->opcode, 4, 3) <
                                extract32(ctx->opcode, 7, 3) ? OPC_BEQ : OPC_BNE;
+                check_nms(ctx);
+                if (rs == rt) {
+                    break;
+                }
                 gen_compute_branch_nm(ctx, opc, 2, rs, rt,
                                       extract32(ctx->opcode, 0, 4) << 1);
             }
Signature · 1 line
2.55.0
#31 lore raw

Re: [PATCH 13/19] target/mips: Fix nanoMIPS BEQC16/BNEC16 corner cases

Quoted 18 lines · Philippe Mathieu-Daudé

BNEC[16] with identical registers is never taken, so it is a NOP. gen_compute_branch_nm() instead wrote $ra and skipped the next 4 bytes as if it were a MIPS branch-and-link-likely.

Also raise Reserved Instruction on NMS cores, where the 16-bit forms are not available.

Fixes 764371d24c51 ("target/mips: Add emulation of nanoMIPS 16-bit branch")
---
  target/mips/tcg/nanomips_translate.c.inc | 4 ++++
  1 file changed, 4 insertions(+)
target/mips/tcg/nanomips_translate.c.inc +0 -0
index f9dc573dbb01..25d57e75a0b0 100644

target/mips/tcg/nanomips_translate.c.inc static int decode_isa_nanomips(CPUMIPSState *env, DisasContext *ctx) · line 4765

Quoted 10 lines
target/mips/tcg/nanomips_translate.c.inc +4 -0
@@ -4756,6 +4756,10 @@ static int decode_isa_nanomips(CPUMIPSState *env, DisasContext *ctx)
                  /* P16.BRI */
                  uint32_t opc = extract32(ctx->opcode, 4, 3) <
                                 extract32(ctx->opcode, 7, 3) ? OPC_BEQ : OPC_BNE;
+                check_nms(ctx);
+                if (rs == rt) {
+                    break;
+                }
                  gen_compute_branch_nm(ctx, opc, 2, rs, rt,
                                        extract32(ctx->opcode, 0, 4) << 1);
              }

Surely not. BNE x,x is never taken, but BEQ x,x is.

r~

#32 lore raw

Re: [PATCH 13/19] target/mips: Fix nanoMIPS BEQC16/BNEC16 corner cases

Quoted 24 lines · Richard Henderson
Quoted 23 lines · Philippe Mathieu-Daudé

BNEC[16] with identical registers is never taken, so it is a NOP. gen_compute_branch_nm() instead wrote $ra and skipped the next 4 bytes as if it were a MIPS branch-and-link-likely.

Also raise Reserved Instruction on NMS cores, where the 16-bit forms are not available.

Fixes 764371d24c51 ("target/mips: Add emulation of nanoMIPS 16-bit branch")

---
  target/mips/tcg/nanomips_translate.c.inc | 4 ++++

  1 file changed, 4 insertions(+)
target/mips/tcg/nanomips_translate.c.inc +0 -0
index f9dc573dbb01..25d57e75a0b0 100644
@@ -4756,6 +4756,10 @@ static int decode_isa_nanomips(CPUMIPSState 
 *env, DisasContext *ctx)
                   /* P16.BRI */

                  uint32_t opc = extract32(ctx->opcode, 4, 3) < extract32(ctx->opcode, 7, 3) ? OPC_BEQ : OPC_BNE; +                check_nms(ctx);

Quoted 8 lines
Quoted 6 lines

+                if (rs == rt) {
+                    break;
+                }
                  gen_compute_branch_nm(ctx, opc, 2, rs, rt, extract32(ctx->opcode, 0, 4) << 1); }

Surely not.  BNE x,x is never taken, but BEQ x,x is.

Right... Updated as:

- if (rs == rt) {
+ if (opc == OPC_BNE && rs == rt) {

#33 lore raw

PATCH 14/19 target/mips: Fix nanoMIPS BLTUC with rs == $zero

In reply to Philippe Mathieu-Daudé

BLTUC rs, rt branches if unsigned(rs) < unsigned(rt) (MD01247 p.55). With rs == $zero it is taken when rt != 0, but the decoder treated it as a NOP. Only skip the branch when rs == rt (never taken).

Reaching the generic path exposes the R6 BGTZALC/BLTZALC-like special cases in gen_compute_compact_branch_nm(), which set $ra and compare as signed; nanoMIPS has neither. Drop them from the unsigned branches (they were unreachable for BGEUC, which the decoder maps to BC/BEQZC).

Fixes 11d0fc10b7ef ("target/mips: Add emulation of nanoMIPS 32-bit branch")
---
 target/mips/tcg/nanomips_translate.c.inc | 34 +++---------------------
 1 file changed, 3 insertions(+), 31 deletions(-)
target/mips/tcg/nanomips_translate.c.inc +3 -31
index 25d57e75a0b0..524972b24a98 100644
@@ -2419,18 +2419,8 @@ static void gen_compute_compact_branch_nm(DisasContext *ctx, uint32_t opc,
     /* compact branch */
     case OPC_BGEC:
     case OPC_BLTC:
-        gen_load_gpr(t0, rs);
-        gen_load_gpr(t1, rt);
-        bcond_compute = 1;
-        ctx->btarget = addr_add(ctx, ctx->base.pc_next + 4, offset);
-        break;
     case OPC_BGEUC:
     case OPC_BLTUC:
-        if (rs == 0 || rs == rt) {
-            /* OPC_BLEZALC, OPC_BGEZALC */
-            /* OPC_BGTZALC, OPC_BLTZALC */
-            tcg_gen_movi_tl(cpu_gpr[31], ctx->base.pc_next + 4);
-        }
         gen_load_gpr(t0, rs);
         gen_load_gpr(t1, rt);
         bcond_compute = 1;
@@ -2476,28 +2466,10 @@ static void gen_compute_compact_branch_nm(DisasContext *ctx, uint32_t opc,
 
         switch (opc) {
         case OPC_BGEUC:
-            if (rs == 0 && rt != 0) {
-                /* OPC_BLEZALC */
-                tcg_gen_brcondi_tl(tcg_invert_cond(TCG_COND_LE), t1, 0, fs);
-            } else if (rs != 0 && rt != 0 && rs == rt) {
-                /* OPC_BGEZALC */
-                tcg_gen_brcondi_tl(tcg_invert_cond(TCG_COND_GE), t1, 0, fs);
-            } else {
-                /* OPC_BGEUC */
-                tcg_gen_brcond_tl(tcg_invert_cond(TCG_COND_GEU), t0, t1, fs);
-            }
+            tcg_gen_brcond_tl(tcg_invert_cond(TCG_COND_GEU), t0, t1, fs);
             break;
         case OPC_BLTUC:
-            if (rs == 0 && rt != 0) {
-                /* OPC_BGTZALC */
-                tcg_gen_brcondi_tl(tcg_invert_cond(TCG_COND_GT), t1, 0, fs);
-            } else if (rs != 0 && rt != 0 && rs == rt) {
-                /* OPC_BLTZALC */
-                tcg_gen_brcondi_tl(tcg_invert_cond(TCG_COND_LT), t1, 0, fs);
-            } else {
-                /* OPC_BLTUC */
-                tcg_gen_brcond_tl(tcg_invert_cond(TCG_COND_LTU), t0, t1, fs);
-            }
+            tcg_gen_brcond_tl(tcg_invert_cond(TCG_COND_LTU), t0, t1, fs);
             break;
         case OPC_BGEC:
             if (rs == 0 && rt != 0) {
@@ -4414,7 +4386,7 @@ static int decode_nanomips_32_48_opc(CPUMIPSState *env, DisasContext *ctx)
                 }
                 break;
             case NM_BLTUC:
-                if (rs == 0 || rs == rt) {
+                if (rs == rt) {
                     /* NOP */
                     ctx->hflags |= MIPS_HFLAG_FBNSLOT;
                 } else {
Signature · 1 line
2.55.0
#34 lore raw

Re: [PATCH 14/19] target/mips: Fix nanoMIPS BLTUC with rs == $zero

target/mips/tcg/nanomips_translate.c.inc static void gen_compute_compact_branch_nm(DisasContext *ctx, uint32_t opc, · line 2426

Quoted 8 lines · Philippe Mathieu-Daudé
      /* compact branch */
      case OPC_BGEC:
      case OPC_BLTC:
target/mips/tcg/nanomips_translate.c.inc +0 -5
-        gen_load_gpr(t0, rs);
-        gen_load_gpr(t1, rt);
-        bcond_compute = 1;
-        ctx->btarget = addr_add(ctx, ctx->base.pc_next + 4, offset);
-        break;

I don't understand why you're discarding bcond_compute?

r~

#35 lore raw

Re: [PATCH 14/19] target/mips: Fix nanoMIPS BLTUC with rs == $zero

Quoted 11 lines · Richard Henderson
Quoted 8 lines · Philippe Mathieu-Daudé

      /* compact branch */
      case OPC_BGEC:
      case OPC_BLTC:
-        gen_load_gpr(t0, rs);
-        gen_load_gpr(t1, rt);
-        bcond_compute = 1;
-        ctx->btarget = addr_add(ctx, ctx->base.pc_next + 4, offset); -        break;

I don't understand why you're discarding bcond_compute?

Restoring context:

target/mips/tcg/nanomips_translate.c.inc +0 -10
index 25d57e75a0b0..524972b24a98 100644
@@ -2419,18 +2419,8 @@ static void gen_compute_compact_branch_nm(DisasContext *ctx, uint32_t opc,
     /* compact branch */
     case OPC_BGEC:
     case OPC_BLTC:
-        gen_load_gpr(t0, rs);
-        gen_load_gpr(t1, rt);
-        bcond_compute = 1;
-        ctx->btarget = addr_add(ctx, ctx->base.pc_next + 4, offset);
-        break;
 
         // now fall thru ...
 
     case OPC_BGEUC:
     case OPC_BLTUC:
-        if (rs == 0 || rs == rt) {
-            /* OPC_BLEZALC, OPC_BGEZALC */
-            /* OPC_BGTZALC, OPC_BLTZALC */
-            tcg_gen_movi_tl(cpu_gpr[31], ctx->base.pc_next + 4);
-        }
         // ... here

         gen_load_gpr(t0, rs);
         gen_load_gpr(t1, rt);
         bcond_compute = 1;

I'll amend to description for clarity:

The deleted BGEC/BLTC lines are duplicated verbatim by the BGEUC/BLTUC block they now fall through into, so bcond_compute/btarget handling is unchanged.

#36 lore raw

PATCH 15/19 target/mips: Pass the nanoMIPS BREAK code to generate_exception_break()

In reply to Philippe Mathieu-Daudé

The nanoMIPS BREAK[32] and BREAK[16] decoders call generate_exception_end() and drop the break code, leaving a stale env->error_code for linux-user. Pass the encoded 'code' 19 bits for BREAK[32], 3 bits for BREAK[16] (MD01247 rev 01.01, p.59).

Complete 6f3533dd1b6a ("target/mips: Extract break code into env->error_code").

Fixes c02809830346 ("target/mips: Add emulation of some nanoMIPS 32-bit")
Fixes 8869ad02bfd9 ("target/mips: Add emulation of nanoMIPS 16-bit misc")
---
 target/mips/tcg/nanomips_translate.c.inc | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/mips/tcg/nanomips_translate.c.inc +2 -2
index 524972b24a98..3612c68561fd 100644
@@ -3545,7 +3545,7 @@ static int decode_nanomips_32_48_opc(CPUMIPSState *env, DisasContext *ctx)
                 }
                 break;
             case NM_BREAK:
-                generate_exception_end(ctx, EXCP_BREAK);
+                generate_exception_break(ctx, extract32(ctx->opcode, 0, 19));
                 break;
             case NM_SDBBP:
                 if (is_uhi(ctx, extract32(ctx->opcode, 0, 19))) {
@@ -4481,7 +4481,7 @@ static int decode_isa_nanomips(CPUMIPSState *env, DisasContext *ctx)
                 }
                 break;
             case NM_BREAK16:
-                generate_exception_end(ctx, EXCP_BREAK);
+                generate_exception_break(ctx, extract32(ctx->opcode, 0, 3));
                 break;
             case NM_SDBBP16:
                 if (is_uhi(ctx, extract32(ctx->opcode, 0, 3))) {
Signature · 1 line
2.55.0
Reviewed-by on v1 15/19
#37 lore raw

Re: [PATCH 15/19] target/mips: Pass the nanoMIPS BREAK code to generate_exception_break()

Quoted 9 lines · Philippe Mathieu-Daudé

The nanoMIPS BREAK[32] and BREAK[16] decoders call generate_exception_end() and drop the break code, leaving a stale env->error_code for linux-user. Pass the encoded 'code' 19 bits for BREAK[32], 3 bits for BREAK[16] (MD01247 rev 01.01, p.59).

Complete 6f3533dd1b6a ("target/mips: Extract break code into env->error_code").

Quoted 6 lines
Fixes c02809830346 ("target/mips: Add emulation of some nanoMIPS 32-bit")
Fixes 8869ad02bfd9 ("target/mips: Add emulation of nanoMIPS 16-bit misc")
---
  target/mips/tcg/nanomips_translate.c.inc | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)

r~

#38 lore raw

PATCH 16/19 target/mips: Decode 6-bit bit number of nanoMIPS BBEQZC/BBNEZC

In reply to Philippe Mathieu-Daudé

BBEQZC (MD01247 p.38) and BBNEZC (p.39) encode the bit number in bits 16:11; bit 17 is 'x' (don't care). The decoder used the 7-bit immediate (17:11) of the other P.BRI branches, so a set bit 17 gave a bit number >= 64 and raised Reserved Instruction on cores without 64-bit operations. Decode only 6 bits for these two; BEQIC and the other P.BRI branches really have a 7-bit u field.

Fixes 11d0fc10b7ef ("target/mips: Add emulation of nanoMIPS 32-bit branch")
---
 target/mips/tcg/nanomips_translate.c.inc | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
target/mips/tcg/nanomips_translate.c.inc +5 -3
index 3612c68561fd..68e9ca9cdb03 100644
@@ -4403,10 +4403,12 @@ static int decode_nanomips_32_48_opc(CPUMIPSState *env, DisasContext *ctx)
         {
             int32_t s = sextract32(ctx->opcode, 0, 1) << 11 |
                         extract32(ctx->opcode, 1, 10) << 1;
-            uint32_t u = extract32(ctx->opcode, 11, 7);
+            uint32_t opc = extract32(ctx->opcode, 18, 3);
+            uint32_t u = extract32(ctx->opcode, 11,
+                                   (opc == NM_BBEQZC ||
+                                    opc == NM_BBNEZC) ? 6 : 7);
 
-            gen_compute_imm_branch(ctx, extract32(ctx->opcode, 18, 3),
-                                   rt, u, s);
+            gen_compute_imm_branch(ctx, opc, rt, u, s);
         }
         break;
     default:
Signature · 1 line
2.55.0
Reviewed-by on v1 16/19
#39 lore raw

Re: [PATCH 16/19] target/mips: Decode 6-bit bit number of nanoMIPS BBEQZC/BBNEZC

Quoted 7 lines · Philippe Mathieu-Daudé

BBEQZC (MD01247 p.38) and BBNEZC (p.39) encode the bit number in bits 16:11; bit 17 is 'x' (don't care). The decoder used the 7-bit immediate (17:11) of the other P.BRI branches, so a set bit 17 gave a bit number >= 64 and raised Reserved Instruction on cores without 64-bit operations. Decode only 6 bits for these two; BEQIC and the other P.BRI branches really have a 7-bit u field.

Quoted 5 lines
Fixes 11d0fc10b7ef ("target/mips: Add emulation of nanoMIPS 32-bit branch")
---
  target/mips/tcg/nanomips_translate.c.inc | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)

r~

#40 lore raw

PATCH 17/19 target/mips: Don't set forbidden slot on never-taken nanoMIPS branches

In reply to Philippe Mathieu-Daudé

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]")
---
 target/mips/tcg/nanomips_translate.c.inc | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
target/mips/tcg/nanomips_translate.c.inc +6 -12
index 68e9ca9cdb03..72d72f6f84a0 100644
@@ -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;
Signature · 1 line
2.55.0
#41 lore raw

Re: [PATCH 17/19] target/mips: Don't set forbidden slot on never-taken nanoMIPS branches

Quoted 6 lines · Philippe Mathieu-Daudé

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.

Quoted 6 lines
Fixes 11d0fc10b7ef ("target/mips: Add emulation of nanoMIPS 32-bit branch")
Fixes 14668cfaaf4d ("target/mips: Fix emulation of nanoMIPS BNEC[32]")
---
  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?

r~

Quoted 38 lines
target/mips/tcg/nanomips_translate.c.inc +6 -12
index 68e9ca9cdb03..72d72f6f84a0 100644
@@ -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;
#42 lore raw

Re: [PATCH 17/19] target/mips: Don't set forbidden slot on never-taken nanoMIPS branches

Quoted 7 lines · Richard Henderson
Quoted 6 lines · Philippe Mathieu-Daudé

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.

Quoted 8 lines
Quoted 6 lines
Fixes 11d0fc10b7ef ("target/mips: Add emulation of nanoMIPS 32-bit branch")
Fixes 14668cfaaf4d ("target/mips: Fix emulation of nanoMIPS BNEC[32]")

---
  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:

target/mips/tcg/nanomips_translate.c.inc +6 -1
@@ -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.

Quoted 45 lines

r~

Quoted 39 lines
target/mips/tcg/nanomips_translate.c.inc +0 -0
index 68e9ca9cdb03..72d72f6f84a0 100644
@@ -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;

#43 lore raw

PATCH 18/19 target/mips: Do not sign-extend partial LWR results on Octeon

In reply to Philippe Mathieu-Daudé

The MIPS64 Release 6.05 manual (LWR description) says that when bit 31 is not loaded, bits 63..32 of the destination are implementation
dependent: either unchanged or a copy of bit 31. Both behaviours are architecturally allowed.

Octeon III CN71XX hardware leaves bits 63..32 unchanged unless the whole word is loaded (vAddr[1:0] = 3 for big-endian, 0 for little-endian). QEMU always sign-extended the merged low word.

Seen replaying a risu trace recorded on a Cavium Octeon III CN71XX:
"lwr s7,26334(a2)" left r23 = 0004114d368db793 on the hardware where QEMU had 00000000368db793.

Only change the behaviour for Octeon CPUs (INSN_OCTEON); all other CPUs keep sign-extending the result. LWL always loads bit 31 and is unchanged.

---
 target/mips/tcg/translate.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
target/mips/tcg/translate.c +12 -2
index 6966b7e56abb..607384292633 100644
@@ -2063,7 +2063,7 @@ void gen_lx(DisasContext *ctx, int rd, int base, int index, MemOp mop)
 static void gen_ld(DisasContext *ctx, uint32_t opc,
                    int rt, int base, int offset)
 {
-    TCGv t0, t1;
+    TCGv t0, t1, t2, t3;
     int mem_idx = ctx->mem_idx;
 
     if (rt == 0 && ctx->insn_flags & (INSN_LOONGSON2E | INSN_LOONGSON2F |
@@ -2176,7 +2176,17 @@ static void gen_ld(DisasContext *ctx, uint32_t opc,
         t1 = tcg_temp_new();
         gen_load_gpr(t1, rt);
         gen_lxr(ctx, t1, t0, mem_idx, mo_endian(ctx) | MO_UL);
-        tcg_gen_ext32s_tl(t1, t1);
+        if (ctx->insn_flags & INSN_OCTEON) {
+            t2 = tcg_temp_new();
+            t3 = tcg_temp_new();
+            tcg_gen_ext32s_tl(t3, t1);
+            tcg_gen_andi_tl(t2, t0, 3);
+            tcg_gen_movcond_tl(TCG_COND_EQ, t1, t2,
+                               tcg_constant_tl(disas_is_bigendian(ctx) ? 3 : 0),
+                               t3, t1);
+        } else {
+            tcg_gen_ext32s_tl(t1, t1);
+        }
         gen_store_gpr(t1, rt);
         break;
     case OPC_LLE:
Signature · 1 line
2.55.0
Acked-by on v1 18/19
#44 lore raw

Re: [PATCH 18/19] target/mips: Do not sign-extend partial LWR results on Octeon

Quoted 13 lines · Philippe Mathieu-Daudé

The MIPS64 Release 6.05 manual (LWR description) says that when bit 31 is not loaded, bits 63..32 of the destination are implementation
dependent: either unchanged or a copy of bit 31. Both behaviours are architecturally allowed.

Octeon III CN71XX hardware leaves bits 63..32 unchanged unless the whole word is loaded (vAddr[1:0] = 3 for big-endian, 0 for little-endian). QEMU always sign-extended the merged low word.

Seen replaying a risu trace recorded on a Cavium Octeon III CN71XX:
"lwr s7,26334(a2)" left r23 = 0004114d368db793 on the hardware where QEMU had 00000000368db793.

Quoted 8 lines

Only change the behaviour for Octeon CPUs (INSN_OCTEON); all other CPUs keep sign-extending the result. LWL always loads bit 31 and is unchanged.

---
  target/mips/tcg/translate.c | 14 ++++++++++++--
  1 file changed, 12 insertions(+), 2 deletions(-)

Horrid, but,

r~

#45 lore raw

PATCH 19/19 target/mips: Do not negate NaN results in NMADD.fmt and NMSUB.fmt

In reply to Philippe Mathieu-Daudé

The final negation of NMADD.fmt and NMSUB.fmt is arithmetic. With the legacy NaN encoding (FCSR.NAN2008 = 0) QEMU returns the default NaN (0x7ff7ffffffffffff for double precision, positive) for a NaN result, then flipped its sign with float{32,64}_chs().

Observed on a Cavium Octeon III V0.2 (FPU V0.0, Linux 4.9.79) running
risu: "nmsub.d $f7,$f27,$f23,$f15" with $f27 = 0x7fffffffffffffff (a signaling NaN in the legacy encoding), $f23 = 0 and
$f15 = 0x472fa6f57f408dcc gives 0x7ff7ffffffffffff on the hardware, where QEMU gave 0xfff7ffffffffffff. The same was seen for the single precision "nmsub.s $f1,$f18,$f22,$f21".

Do not change the sign of a NaN result when NAN2008 is 0. Non-NaN results and the NAN2008 = 1 behavior are unchanged. NMADD.fmt, which shares the code, was not exercised on the hardware.

The hardware (or its kernel FPU emulator) also returns a different NaN than QEMU for "mul.d" with a signaling NaN operand and a denormal
operand: this is not addressed here.

---
 target/mips/tcg/fpu_helper.c | 39 ++++++++++++++++++++++++++++--------
 1 file changed, 31 insertions(+), 8 deletions(-)
target/mips/tcg/fpu_helper.c +31 -8
index d6595da6482f..e085aa5669f0 100644
@@ -1627,12 +1627,35 @@ uint64_t HELPER(float_msub_ps)(CPUMIPSState *env, uint64_t fdt0,
     return ((uint64_t)fsth0 << 32) | fstl0;
 }
 
+/*
+ * The final negation of NMADD.fmt and NMSUB.fmt is arithmetic: with the
+ * legacy NaN encoding (FCSR.NAN2008 = 0) the sign of a NaN result is not
+ * changed.
+ */
+static inline float64 nm_negate_d(CPUMIPSState *env, float64 a)
+{
+    if (!(env->active_fpu.fcr31 & (1 << FCR31_NAN2008)) &&
+        float64_is_any_nan(a)) {
+        return a;
+    }
+    return float64_chs(a);
+}
+
+static inline float32 nm_negate_s(CPUMIPSState *env, float32 a)
+{
+    if (!(env->active_fpu.fcr31 & (1 << FCR31_NAN2008)) &&
+        float32_is_any_nan(a)) {
+        return a;
+    }
+    return float32_chs(a);
+}
+
 uint64_t HELPER(float_nmadd_d)(CPUMIPSState *env, uint64_t fst0,
                              uint64_t fst1, uint64_t fst2)
 {
     fst0 = float64_mul(fst0, fst1, &env->active_fpu.fp_status);
     fst0 = float64_add(fst0, fst2, &env->active_fpu.fp_status);
-    fst0 = float64_chs(fst0);
+    fst0 = nm_negate_d(env, fst0);
 
     update_fcr31(env, GETPC());
     return fst0;
@@ -1643,7 +1666,7 @@ uint32_t HELPER(float_nmadd_s)(CPUMIPSState *env, uint32_t fst0,
 {
     fst0 = float32_mul(fst0, fst1, &env->active_fpu.fp_status);
     fst0 = float32_add(fst0, fst2, &env->active_fpu.fp_status);
-    fst0 = float32_chs(fst0);
+    fst0 = nm_negate_s(env, fst0);
 
     update_fcr31(env, GETPC());
     return fst0;
@@ -1661,10 +1684,10 @@ uint64_t HELPER(float_nmadd_ps)(CPUMIPSState *env, uint64_t fdt0,
 
     fstl0 = float32_mul(fstl0, fstl1, &env->active_fpu.fp_status);
     fstl0 = float32_add(fstl0, fstl2, &env->active_fpu.fp_status);
-    fstl0 = float32_chs(fstl0);
+    fstl0 = nm_negate_s(env, fstl0);
     fsth0 = float32_mul(fsth0, fsth1, &env->active_fpu.fp_status);
     fsth0 = float32_add(fsth0, fsth2, &env->active_fpu.fp_status);
-    fsth0 = float32_chs(fsth0);
+    fsth0 = nm_negate_s(env, fsth0);
 
     update_fcr31(env, GETPC());
     return ((uint64_t)fsth0 << 32) | fstl0;
@@ -1675,7 +1698,7 @@ uint64_t HELPER(float_nmsub_d)(CPUMIPSState *env, uint64_t fst0,
 {
     fst0 = float64_mul(fst0, fst1, &env->active_fpu.fp_status);
     fst0 = float64_sub(fst0, fst2, &env->active_fpu.fp_status);
-    fst0 = float64_chs(fst0);
+    fst0 = nm_negate_d(env, fst0);
 
     update_fcr31(env, GETPC());
     return fst0;
@@ -1686,7 +1709,7 @@ uint32_t HELPER(float_nmsub_s)(CPUMIPSState *env, uint32_t fst0,
 {
     fst0 = float32_mul(fst0, fst1, &env->active_fpu.fp_status);
     fst0 = float32_sub(fst0, fst2, &env->active_fpu.fp_status);
-    fst0 = float32_chs(fst0);
+    fst0 = nm_negate_s(env, fst0);
 
     update_fcr31(env, GETPC());
     return fst0;
@@ -1704,10 +1727,10 @@ uint64_t HELPER(float_nmsub_ps)(CPUMIPSState *env, uint64_t fdt0,
 
     fstl0 = float32_mul(fstl0, fstl1, &env->active_fpu.fp_status);
     fstl0 = float32_sub(fstl0, fstl2, &env->active_fpu.fp_status);
-    fstl0 = float32_chs(fstl0);
+    fstl0 = nm_negate_s(env, fstl0);
     fsth0 = float32_mul(fsth0, fsth1, &env->active_fpu.fp_status);
     fsth0 = float32_sub(fsth0, fsth2, &env->active_fpu.fp_status);
-    fsth0 = float32_chs(fsth0);
+    fsth0 = nm_negate_s(env, fsth0);
 
     update_fcr31(env, GETPC());
     return ((uint64_t)fsth0 << 32) | fstl0;
Signature · 1 line
2.55.0
#46 lore raw

Re: [PATCH 19/19] target/mips: Do not negate NaN results in NMADD.fmt and NMSUB.fmt

Quoted 16 lines · Philippe Mathieu-Daudé

The final negation of NMADD.fmt and NMSUB.fmt is arithmetic. With the legacy NaN encoding (FCSR.NAN2008 = 0) QEMU returns the default NaN (0x7ff7ffffffffffff for double precision, positive) for a NaN result, then flipped its sign with float{32,64}_chs().

Observed on a Cavium Octeon III V0.2 (FPU V0.0, Linux 4.9.79) running
risu: "nmsub.d $f7,$f27,$f23,$f15" with $f27 = 0x7fffffffffffffff (a signaling NaN in the legacy encoding), $f23 = 0 and
$f15 = 0x472fa6f57f408dcc gives 0x7ff7ffffffffffff on the hardware, where QEMU gave 0xfff7ffffffffffff. The same was seen for the single precision "nmsub.s $f1,$f18,$f22,$f21".

Do not change the sign of a NaN result when NAN2008 is 0. Non-NaN results and the NAN2008 = 1 behavior are unchanged. NMADD.fmt, which shares the code, was not exercised on the hardware.

Quoted 8 lines

The hardware (or its kernel FPU emulator) also returns a different NaN than QEMU for "mul.d" with a signaling NaN operand and a denormal
operand: this is not addressed here.

---
  target/mips/tcg/fpu_helper.c | 39 ++++++++++++++++++++++++++++--------
  1 file changed, 31 insertions(+), 8 deletions(-)

Isn't the larger problem that this should be using a fused multiply-add via float64_muladd, and thus the negation via float_muladd_negate_result (which will not affect NaN results).

Certainly MD00087 says the product uses infinite precision.

r~

Quoted 95 lines
target/mips/tcg/fpu_helper.c +31 -8
index d6595da6482f..e085aa5669f0 100644
@@ -1627,12 +1627,35 @@ uint64_t HELPER(float_msub_ps)(CPUMIPSState *env, uint64_t fdt0,
      return ((uint64_t)fsth0 << 32) | fstl0;
  }
 
+/*
+ * The final negation of NMADD.fmt and NMSUB.fmt is arithmetic: with the
+ * legacy NaN encoding (FCSR.NAN2008 = 0) the sign of a NaN result is not
+ * changed.
+ */
+static inline float64 nm_negate_d(CPUMIPSState *env, float64 a)
+{
+    if (!(env->active_fpu.fcr31 & (1 << FCR31_NAN2008)) &&
+        float64_is_any_nan(a)) {
+        return a;
+    }
+    return float64_chs(a);
+}
+
+static inline float32 nm_negate_s(CPUMIPSState *env, float32 a)
+{
+    if (!(env->active_fpu.fcr31 & (1 << FCR31_NAN2008)) &&
+        float32_is_any_nan(a)) {
+        return a;
+    }
+    return float32_chs(a);
+}
+
  uint64_t HELPER(float_nmadd_d)(CPUMIPSState *env, uint64_t fst0,
                               uint64_t fst1, uint64_t fst2)
  {
      fst0 = float64_mul(fst0, fst1, &env->active_fpu.fp_status);
      fst0 = float64_add(fst0, fst2, &env->active_fpu.fp_status);
-    fst0 = float64_chs(fst0);
+    fst0 = nm_negate_d(env, fst0);
 
      update_fcr31(env, GETPC());
      return fst0;
@@ -1643,7 +1666,7 @@ uint32_t HELPER(float_nmadd_s)(CPUMIPSState *env, uint32_t fst0,
  {
      fst0 = float32_mul(fst0, fst1, &env->active_fpu.fp_status);
      fst0 = float32_add(fst0, fst2, &env->active_fpu.fp_status);
-    fst0 = float32_chs(fst0);
+    fst0 = nm_negate_s(env, fst0);
 
      update_fcr31(env, GETPC());
      return fst0;
@@ -1661,10 +1684,10 @@ uint64_t HELPER(float_nmadd_ps)(CPUMIPSState *env, uint64_t fdt0,
 
      fstl0 = float32_mul(fstl0, fstl1, &env->active_fpu.fp_status);
      fstl0 = float32_add(fstl0, fstl2, &env->active_fpu.fp_status);
-    fstl0 = float32_chs(fstl0);
+    fstl0 = nm_negate_s(env, fstl0);
      fsth0 = float32_mul(fsth0, fsth1, &env->active_fpu.fp_status);
      fsth0 = float32_add(fsth0, fsth2, &env->active_fpu.fp_status);
-    fsth0 = float32_chs(fsth0);
+    fsth0 = nm_negate_s(env, fsth0);
 
      update_fcr31(env, GETPC());
      return ((uint64_t)fsth0 << 32) | fstl0;
@@ -1675,7 +1698,7 @@ uint64_t HELPER(float_nmsub_d)(CPUMIPSState *env, uint64_t fst0,
  {
      fst0 = float64_mul(fst0, fst1, &env->active_fpu.fp_status);
      fst0 = float64_sub(fst0, fst2, &env->active_fpu.fp_status);
-    fst0 = float64_chs(fst0);
+    fst0 = nm_negate_d(env, fst0);
 
      update_fcr31(env, GETPC());
      return fst0;
@@ -1686,7 +1709,7 @@ uint32_t HELPER(float_nmsub_s)(CPUMIPSState *env, uint32_t fst0,
  {
      fst0 = float32_mul(fst0, fst1, &env->active_fpu.fp_status);
      fst0 = float32_sub(fst0, fst2, &env->active_fpu.fp_status);
-    fst0 = float32_chs(fst0);
+    fst0 = nm_negate_s(env, fst0);
 
      update_fcr31(env, GETPC());
      return fst0;
@@ -1704,10 +1727,10 @@ uint64_t HELPER(float_nmsub_ps)(CPUMIPSState *env, uint64_t fdt0,
 
      fstl0 = float32_mul(fstl0, fstl1, &env->active_fpu.fp_status);
      fstl0 = float32_sub(fstl0, fstl2, &env->active_fpu.fp_status);
-    fstl0 = float32_chs(fstl0);
+    fstl0 = nm_negate_s(env, fstl0);
      fsth0 = float32_mul(fsth0, fsth1, &env->active_fpu.fp_status);
      fsth0 = float32_sub(fsth0, fsth2, &env->active_fpu.fp_status);
-    fsth0 = float32_chs(fsth0);
+    fsth0 = nm_negate_s(env, fsth0);
 
      update_fcr31(env, GETPC());
      return ((uint64_t)fsth0 << 32) | fstl0;
#47 lore raw

Re: [PATCH 19/19] target/mips: Do not negate NaN results in NMADD.fmt and NMSUB.fmt

Quoted 21 lines · Richard Henderson
Quoted 20 lines · Philippe Mathieu-Daudé

The final negation of NMADD.fmt and NMSUB.fmt is arithmetic. With the legacy NaN encoding (FCSR.NAN2008 = 0) QEMU returns the default NaN (0x7ff7ffffffffffff for double precision, positive) for a NaN result, then flipped its sign with float{32,64}_chs().

Observed on a Cavium Octeon III V0.2 (FPU V0.0, Linux 4.9.79) running
risu: "nmsub.d $f7,$f27,$f23,$f15" with $f27 = 0x7fffffffffffffff (a signaling NaN in the legacy encoding), $f23 = 0 and
$f15 = 0x472fa6f57f408dcc gives 0x7ff7ffffffffffff on the hardware, where QEMU gave 0xfff7ffffffffffff. The same was seen for the single precision "nmsub.s $f1,$f18,$f22,$f21".

Do not change the sign of a NaN result when NAN2008 is 0. Non-NaN results and the NAN2008 = 1 behavior are unchanged. NMADD.fmt, which shares the code, was not exercised on the hardware.

The hardware (or its kernel FPU emulator) also returns a different NaN than QEMU for "mul.d" with a signaling NaN operand and a denormal
operand: this is not addressed here.

Quoted 8 lines
Quoted 4 lines

---
  target/mips/tcg/fpu_helper.c | 39 ++++++++++++++++++++++++++++--------

  1 file changed, 31 insertions(+), 8 deletions(-)

Isn't the larger problem that this should be using a fused multiply-add via float64_muladd, and thus the negation via float_muladd_negate_result (which will not affect NaN results).

Certainly MD00087 says the product uses infinite precision.

MD00087 says the opposite for NMADD/NMSUB... Vol II-A rev 6.06, NMADD.fmt (p.376) and NMSUB.fmt (p.378):

"The intermediate product is rounded according to the current
rounding mode in FCSR [...] The results and flags are as if separate floating-point multiply and add and negate instructions were executed"

The infinite-precision-product wording is from the Release 6 MADDF.fmt/MSUBF.fmt (p.324), which the same manual describes as replacing the pre-R6 instructions that were "unfused multiply-add, with an intermediate rounding".
Those are already implemented with float_muladd (float_maddf_*/ float_msubf_*); using it for NMADD/NMSUB would remove the required intermediate rounding and change Inexact in double-rounding cases.

Reviewed-by
#48 lore raw

Re: [PATCH 19/19] target/mips: Do not negate NaN results in NMADD.fmt and NMSUB.fmt

Quoted 13 lines · Philippe Mathieu-Daudé
Quoted 3 lines · Richard Henderson

Isn't the larger problem that this should be using a fused multiply- add via float64_muladd, and thus the negation via float_muladd_negate_result (which will not affect NaN results).

Certainly MD00087 says the product uses infinite precision.

MD00087 says the opposite for NMADD/NMSUB... Vol II-A rev 6.06, NMADD.fmt (p.376) and NMSUB.fmt (p.378):

  "The intermediate product is rounded according to the current rounding mode in FCSR [...] The results and flags are as if separate floating-point multiply and add and negate instructions were executed"

Quoted 7 lines

The infinite-precision-product wording is from the Release 6 MADDF.fmt/MSUBF.fmt (p.324), which the same manual describes as replacing the pre-R6 instructions that were "unfused multiply-add, with an intermediate rounding".
Those are already implemented with float_muladd (float_maddf_*/ float_msubf_*); using it for NMADD/NMSUB would remove the required intermediate rounding and change Inexact in double-rounding cases.

Weird.

MD00087 rev 2.00, p269:

The value in FPR fs is multiplied by the value in FPR ft to produce an intermediate product. The value in FPR fr is added to the product. The result sum is calculated to infinite precision, rounded according to the current rounding mode in FCSR, negated by changing the sign bit, and placed into FPR fd. The operands and result are values in format fmt.

Hmph. rev 5 adds the language that rounds the intermediate product. Oh well.

r~

#49 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 3 lines · Philippe Mathieu-Daudé

Various individual fixes noted while reviewing manuals during decodetree conversion, and using a RISU prototype.

If you have a little time for MIPS could I get you to have a look at Coverity issue 1689817 ? It's not urgent, but Coverity complains about what it thinks is some dead code in the dextr_s_h helper:

    } else if ((temp127 == 1) &&
            (temp[1] < 0xFFFFFFFFFFFFFFFFll
             || temp[0] < 0xFFFFFFFFFFFF1000ll)) {

The compare with temp[1] is very odd here. The constant has the "ll" suffix indicating that the author thought this would be signed, but the value is 0xFFFF_FFFF_FFFF_FFFF so it doesn't fit in a signed 64-bit integer type. So the C spec says it has type "unsigned long long" (because it's a hex constant). That means that the comparison is int64_t against uint64_t, which causes temp[1] to be cast to unsigned and we do an unsigned comparison.

The upshot is that "temp[1] < 0xFFFFFFFFFFFFFFFFll" is a funny way of writing "temp[1] != -1". So I think Coverity is wrong about there being dead code, but it's confused because the code is written in a confusing and possibly wrong way.

The comparison with temp[] is also 0xFFFF_FFFF_FFFF_1000 with the LL suffix, so it's also not really a signed value or comparison.

I don't know what this instruction is supposed to be doing, but it would be helpful if somebody who does could have a look and rewrite the code to either do the right thing if the current code is wrong, or else to rephrase it so it is clearer about what it's doing if it's getting the right result in a weird way.

thanks
-- PMM

#50 lore raw

Hi Peter,

Quoted 27 lines · Peter Maydell
Quoted 3 lines · Philippe Mathieu-Daudé

Various individual fixes noted while reviewing manuals during decodetree conversion, and using a RISU prototype.

If you have a little time for MIPS could I get you to have a look at Coverity issue 1689817 ? It's not urgent, but Coverity complains about what it thinks is some dead code in the dextr_s_h helper:

     } else if ((temp127 == 1) &&
             (temp[1] < 0xFFFFFFFFFFFFFFFFll
              || temp[0] < 0xFFFFFFFFFFFF1000ll)) {

The compare with temp[1] is very odd here. The constant has the "ll" suffix indicating that the author thought this would be signed, but the value is 0xFFFF_FFFF_FFFF_FFFF so it doesn't fit in a signed 64-bit integer type. So the C spec says it has type "unsigned long long" (because it's a hex constant). That means that the comparison is int64_t against uint64_t, which causes temp[1] to be cast to unsigned and we do an unsigned comparison.

The upshot is that "temp[1] < 0xFFFFFFFFFFFFFFFFll" is a funny way of writing "temp[1] != -1". So I think Coverity is wrong about there being dead code, but it's confused because the code is written in a confusing and possibly wrong way.

Quoted 8 lines

The comparison with temp[] is also 0xFFFF_FFFF_FFFF_1000 with the LL suffix, so it's also not really a signed value or comparison.

I don't know what this instruction is supposed to be doing, but it would be helpful if somebody who does could have a look and rewrite the code to either do the right thing if the current code is wrong, or else to rephrase it so it is clearer about what it's doing if it's getting the right result in a weird way.

Although MD00375 (MIPS64 DSP) and MD00765 (microMIPS64 DSP) manuals list the DEXTP* or DEXTR* instructions encoding tables, they do not contains these instruction descriptions. We can infer from the MIPS32 EXTR_S.H semantics which saturates to [-0x8000, 0x7FFF], sign-extend to 64 bits and apply to the 128-bit HI:LO.

0xFFFF_FFFF_FFFF_1000 is a typo, should be 0xFFFF_FFFF_FFFF_8000.

Fix using uint64_t and explicit comparisons (positive values with bit 63 set in the low half were not saturated, as that half was compared as signed):

-- >8 --

target/mips/tcg/dsp_helper.c +5 -6
index 73934cab7786..1d4d54897dd6 100644
@@ -3379,22 +3379,21 @@ target_ulong HELPER(extr_s_h)(target_ulong ac, target_ulong shift,
 target_ulong HELPER(dextr_s_h)(target_ulong ac, target_ulong shift,
                               CPUMIPSState *env)
 {
-    int64_t temp[2];
+    uint64_t temp[2];
     uint32_t temp127;
 
     shift = shift & 0x1F;
 
-    mipsdsp_rashift_acc((uint64_t *)temp, ac, shift, env);
+    mipsdsp_rashift_acc(temp, ac, shift, env);
 
     temp127 = (temp[1] >> 63) & 0x01;
 
-    if ((temp127 == 0) && (temp[1] > 0 || temp[0] > 32767)) {
+    if (temp127 == 0 && (temp[1] != 0 || temp[0] > 0x7FFF)) {
         temp[0] &= 0xFFFF0000;
         temp[0] |= 0x00007FFF;
         set_DSPControl_overflow_flag(1, 23, env);
-    } else if ((temp127 == 1) &&
-            (temp[1] < 0xFFFFFFFFFFFFFFFFll
-             || temp[0] < 0xFFFFFFFFFFFF1000ll)) {
+    } else if (temp127 == 1 &&
+               (temp[1] != ~0ull || temp[0] < 0xFFFFFFFFFFFF8000ull)) {
         temp[0] &= 0xFFFF0000;
         temp[0] |= 0x00008000;
         set_DSPControl_overflow_flag(1, 23, env);

---

But this is a good opportunity to rewrite in a more readable style (I'm not a fan of complex condition checks in particular when 'else if' is involved, I rather use local self-descriptive booleans); the same fix becomes:

-- >8 --

target/mips/tcg/dsp_helper.c +12 -17
index 73934cab7786..82a9a29c0213 100644
@@ -3379,28 +3379,25 @@ target_ulong HELPER(extr_s_h)(target_ulong ac, target_ulong shift,
 target_ulong HELPER(dextr_s_h)(target_ulong ac, target_ulong shift,
                               CPUMIPSState *env)
 {
-    int64_t temp[2];
-    uint32_t temp127;
+    uint64_t temp[2];
+    int64_t lo;
+    bool negative, fits_s64, fits_s16;
 
     shift = shift & 0x1F;
 
-    mipsdsp_rashift_acc((uint64_t *)temp, ac, shift, env);
+    mipsdsp_rashift_acc(temp, ac, shift, env);
 
-    temp127 = (temp[1] >> 63) & 0x01;
+    lo = temp[0];
+    negative = temp[1] >> 63;
+    fits_s64 = temp[1] == (uint64_t)(lo >> 63);
+    fits_s16 = lo == (int16_t)lo;
 
-    if ((temp127 == 0) && (temp[1] > 0 || temp[0] > 32767)) {
-        temp[0] &= 0xFFFF0000;
-        temp[0] |= 0x00007FFF;
-        set_DSPControl_overflow_flag(1, 23, env);
-    } else if ((temp127 == 1) &&
-            (temp[1] < 0xFFFFFFFFFFFFFFFFll
-             || temp[0] < 0xFFFFFFFFFFFF1000ll)) {
-        temp[0] &= 0xFFFF0000;
-        temp[0] |= 0x00008000;
-        set_DSPControl_overflow_flag(1, 23, env);
+    if (fits_s64 && fits_s16) {
+        return lo;
     }
-
-    return (int64_t)(int16_t)(temp[0] & MIPSDSP_LO);
+    set_DSPControl_overflow_flag(1, 23, env);
+    return negative ? INT16_MIN : INT16_MAX;
 }
 
---

Is it reviewable-enough to be a single fix patch or do you rather that split in 1 fix and 1 refactor?

Thanks,

Phil.

#51 lore raw
Quoted 71 lines · Philippe Mathieu-Daudé
Quoted 9 lines · Peter Maydell

If you have a little time for MIPS could I get you to have a look at Coverity issue 1689817 ? It's not urgent, but Coverity complains about what it thinks is some dead code in the dextr_s_h helper:

I don't know what this instruction is supposed to be doing, but it would be helpful if somebody who does could have a look and rewrite the code to either do the right thing if the current code is wrong, or else to rephrase it so it is clearer about what it's doing if it's getting the right result in a weird way.

Although MD00375 (MIPS64 DSP) and MD00765 (microMIPS64 DSP) manuals list the DEXTP* or DEXTR* instructions encoding tables, they do not contains these instruction descriptions. We can infer from the MIPS32 EXTR_S.H semantics which saturates to [-0x8000, 0x7FFF], sign-extend to 64 bits and apply to the 128-bit HI:LO.

But this is a good opportunity to rewrite in a more readable style (I'm not a fan of complex condition checks in particular when 'else if' is involved, I rather use local self-descriptive booleans); the same fix becomes:

-- >8 --

target/mips/tcg/dsp_helper.c +12 -17
index 73934cab7786..82a9a29c0213 100644
@@ -3379,28 +3379,25 @@ target_ulong HELPER(extr_s_h)(target_ulong ac,
 target_ulong shift,
  target_ulong HELPER(dextr_s_h)(target_ulong ac, target_ulong shift,
                                CPUMIPSState *env)
  {
-    int64_t temp[2];
-    uint32_t temp127;
+    uint64_t temp[2];
+    int64_t lo;
+    bool negative, fits_s64, fits_s16;
 
      shift = shift & 0x1F;
 
-    mipsdsp_rashift_acc((uint64_t *)temp, ac, shift, env);
+    mipsdsp_rashift_acc(temp, ac, shift, env);
 
-    temp127 = (temp[1] >> 63) & 0x01;
+    lo = temp[0];
+    negative = temp[1] >> 63;
+    fits_s64 = temp[1] == (uint64_t)(lo >> 63);
+    fits_s16 = lo == (int16_t)lo;
 
-    if ((temp127 == 0) && (temp[1] > 0 || temp[0] > 32767)) {
-        temp[0] &= 0xFFFF0000;
-        temp[0] |= 0x00007FFF;
-        set_DSPControl_overflow_flag(1, 23, env);
-    } else if ((temp127 == 1) &&
-            (temp[1] < 0xFFFFFFFFFFFFFFFFll
-             || temp[0] < 0xFFFFFFFFFFFF1000ll)) {
-        temp[0] &= 0xFFFF0000;
-        temp[0] |= 0x00008000;
-        set_DSPControl_overflow_flag(1, 23, env);
+    if (fits_s64 && fits_s16) {
+        return lo;
      }
-
-    return (int64_t)(int16_t)(temp[0] & MIPSDSP_LO);
+    set_DSPControl_overflow_flag(1, 23, env);
+    return negative ? INT16_MIN : INT16_MAX;
  }
 
---
Quoted 2 lines

Is it reviewable-enough to be a single fix patch or do you rather that split in 1 fix and 1 refactor?

I think that's OK as a single patch if you explain in the commit message which cases the old code was getting wrong and what the intent of the code is.

-- PMM