[PATCH 00/19] target/mips: Mixed bag of fixes

Philippe Mathieu-Daudé posted 19 patches 1 week, 2 days ago
Maintainers: "Philippe Mathieu-Daudé" <philmd@oss.qualcomm.com>, Aurelien Jarno <aurelien@aurel32.net>, Jiaxun Yang <jiaxun.yang@flygoat.com>, Aleksandar Rikalo <arikalo@gmail.com>
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(-)
[PATCH 00/19] target/mips: Mixed bag of fixes
Posted by Philippe Mathieu-Daudé 1 week, 2 days ago
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(-)

-- 
2.55.0


Re: [PATCH 00/19] target/mips: Mixed bag of fixes
Posted by Peter Maydell 1 week, 2 days ago
On Fri, 13 Nov 2026 at 16:13, Philippe Mathieu-Daudé
<philmd@oss.qualcomm.com> wrote:
>
> 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
Re: [PATCH 00/19] target/mips: Mixed bag of fixes
Posted by Philippe Mathieu-Daudé 6 days, 19 hours ago
Hi Peter,

On 13/11/26 17:34, Peter Maydell wrote:
> On Fri, 13 Nov 2026 at 16:13, Philippe Mathieu-Daudé
> <philmd@oss.qualcomm.com> wrote:
>>
>> 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.

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 --
diff --git a/target/mips/tcg/dsp_helper.c b/target/mips/tcg/dsp_helper.c
index 73934cab7786..1d4d54897dd6 100644
--- a/target/mips/tcg/dsp_helper.c
+++ b/target/mips/tcg/dsp_helper.c
@@ -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 --
diff --git a/target/mips/tcg/dsp_helper.c b/target/mips/tcg/dsp_helper.c
index 73934cab7786..82a9a29c0213 100644
--- a/target/mips/tcg/dsp_helper.c
+++ b/target/mips/tcg/dsp_helper.c
@@ -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.

Re: [PATCH 00/19] target/mips: Mixed bag of fixes
Posted by Peter Maydell 6 days, 18 hours ago
On Mon, 16 Nov 2026 at 08:37, Philippe Mathieu-Daudé
<philmd@oss.qualcomm.com> wrote:
> On 13/11/26 17:34, Peter Maydell wrote:
> > 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 --
> diff --git a/target/mips/tcg/dsp_helper.c b/target/mips/tcg/dsp_helper.c
> index 73934cab7786..82a9a29c0213 100644
> --- a/target/mips/tcg/dsp_helper.c
> +++ b/target/mips/tcg/dsp_helper.c
> @@ -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?

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