Skip to contentSkip to conversation

MIPS COP1X (and related) instructions

Qemu-devel Richard Sandiford 2 participants 5 messages Dec 28, 2007 – Dec 30, 2007

lore
Share & tools
lore
#0 lore raw

All MIPS COP1X instructions currently require the FPU to be in 64-bit mode. My understanding is that this is too restrictive, and that the base conditions are different for different revisions of the ISA:

MIPS IV:
COP1X instructions are available when the XX (CU3) bit of the status register is set. This bit can be set independently of UX and FR, and controls the core MIPS IV instructions as well as the FPU ones.

Revision 1 of MIPS64:
COP1X instructions are available when 64-bit operations (as opposed to 64-bit floating-point operations) are enabled.

Revision 2 of MIPS32 and MIPS64:
COP1X instructions are available when a 64-bit FPU is present (i.e. if FCR0.F64 is set).

The same conditions apply to some non-COP1X instructions too, such as RECIP.fmt and RSQRT.fmt. Of course, some of the affected instructions really do require 64-bit mode, such as the paired-single ones.

The attached patch is one attempt to fix this. It's wrong if the description above is wrong.

Because the conditions are different for different ISAs, I thought it was worth adding a MIPS_HFLAG_* to say whether COP1X (and related) instructions are available. This flag is directly associated with the nabla symbol in the MIPS32 and MIPS64 opcode tables.

Any instructions that requires FR to be set (as enforced by check_cp1_64bitmode) is also affected by the nabla behaviour, so I've made check_cp1_64bitmode depend on both flags.

I've added nabla checks for RECIP.S and RSQRT.S. My understanding is that the *.D forms are unpredictable when FR is clear, and that QEMU generally uses check_cp1_64bitmode for such instructions. I've therefore replaced the existing "even register" checks with check_cp1_64bitmode, rather than add a separate nabla test.

I've replaced the single check_cp1_64bitmode calls in gen_flt3_ldst gen_flt3_arith with case-by-case checks. I've also made CABS.[SD] use nabla checks instead of check_cp1_64bitmode and added nabla checks to BC1ANY[24].

I noticed all this because a 3-ABI sysroot built with a --with-arch=mips64 GCC uses things like MADD in the o32 libm. There were lots of o32 GCC failures when testing against that libm before the patch, but the results after the patch are on a par with the MIPS32 ones. Please install if OK.

Richard

Index: target-mips/cpu.h
===================================================================
RCS file: /sources/qemu/qemu/target-mips/cpu.h,v
retrieving revision 1.55

cpu.h +11 -7
diff -u -p -r1.55 cpu.h
@@ -417,7 +417,7 @@ struct CPUMIPSState {
     int user_mode_only; /* user mode only simulation */
     uint32_t hflags;    /* CPU State */
     /* TMASK defines different execution modes */
-#define MIPS_HFLAG_TMASK  0x00FF
+#define MIPS_HFLAG_TMASK  0x01FF
 #define MIPS_HFLAG_MODE   0x0007 /* execution modes                    */
     /* The KSU flags must be the lowest bits in hflags. The flag order
        must be the same as defined for CP0 Status. This allows to use
@@ -431,16 +431,20 @@ struct CPUMIPSState {
 #define MIPS_HFLAG_CP0    0x0010 /* CP0 enabled                        */
 #define MIPS_HFLAG_FPU    0x0020 /* FPU enabled                        */
 #define MIPS_HFLAG_F64    0x0040 /* 64-bit FPU enabled                 */
-#define MIPS_HFLAG_RE     0x0080 /* Reversed endianness                */
+    /* True if the MIPS IV COP1X instructions can be used.  This also
+       controls the non-COP1X instructions RECIP.S, RECIP.D, RSQRT.S
+       and RSQRT.D.  */
+#define MIPS_HFLAG_COP1X  0x0080 /* COP1X instructions enabled         */
+#define MIPS_HFLAG_RE     0x0100 /* Reversed endianness                */
     /* If translation is interrupted between the branch instruction and
      * the delay slot, record what type of branch it is so that we can
      * resume translation properly.  It might be possible to reduce
      * this from three bits to two.  */
-#define MIPS_HFLAG_BMASK  0x0700
-#define MIPS_HFLAG_B      0x0100 /* Unconditional branch               */
-#define MIPS_HFLAG_BC     0x0200 /* Conditional branch                 */
-#define MIPS_HFLAG_BL     0x0300 /* Likely branch                      */
-#define MIPS_HFLAG_BR     0x0400 /* branch to register (can't link TB) */
+#define MIPS_HFLAG_BMASK  0x0e00
+#define MIPS_HFLAG_B      0x0200 /* Unconditional branch               */
+#define MIPS_HFLAG_BC     0x0400 /* Conditional branch                 */
+#define MIPS_HFLAG_BL     0x0600 /* Likely branch                      */
+#define MIPS_HFLAG_BR     0x0800 /* branch to register (can't link TB) */
     target_ulong btarget;        /* Jump / branch target               */
     int bcond;                   /* Branch condition (if needed)       */
 

Index: target-mips/exec.h
===================================================================
RCS file: /sources/qemu/qemu/target-mips/exec.h,v
retrieving revision 1.44

exec.h +12 -2
diff -u -p -r1.44 exec.h
@@ -237,8 +237,8 @@ static always_inline int cpu_halted(CPUS
 
 static always_inline void compute_hflags(CPUState *env)
 {
-    env->hflags &= ~(MIPS_HFLAG_64 | MIPS_HFLAG_CP0 | MIPS_HFLAG_F64 |
-                     MIPS_HFLAG_FPU | MIPS_HFLAG_KSU);
+    env->hflags &= ~(MIPS_HFLAG_COP1X | MIPS_HFLAG_64 | MIPS_HFLAG_CP0 |
+                     MIPS_HFLAG_F64 | MIPS_HFLAG_FPU | MIPS_HFLAG_KSU);
     if (!(env->CP0_Status & (1 << CP0St_EXL)) &&
         !(env->CP0_Status & (1 << CP0St_ERL)) &&
         !(env->hflags & MIPS_HFLAG_DM)) {
@@ -257,6 +257,16 @@ static always_inline void compute_hflags
         env->hflags |= MIPS_HFLAG_FPU;
     if (env->CP0_Status & (1 << CP0St_FR))
         env->hflags |= MIPS_HFLAG_F64;
+    if (env->insn_flags & ISA_MIPS32R2) {
+        if (env->fpu->fcr0 & FCR0_F64)
+            env->hflags |= MIPS_HFLAG_COP1X;
+    } else if (env->insn_flags & ISA_MIPS32) {
+        if (env->hflags & MIPS_HFLAG_64)
+            env->hflags |= MIPS_HFLAG_COP1X;
+    } else if (env->insn_flags & ISA_MIPS4) {
+        if (env->CP0_Status & (1 << CP0St_CU3))
+            env->hflags |= MIPS_HFLAG_COP1X;
+    }
 }
 
 #endif /* !defined(__QEMU_MIPS_EXEC_H__) */

Index: target-mips/translate.c
===================================================================
RCS file: /sources/qemu/qemu/target-mips/translate.c,v retrieving revision 1.119

translate.c +47 -9

225 lines not on this page · raw message

#1 lore raw
Quoted 9 lines · Richard Sandiford

All MIPS COP1X instructions currently require the FPU to be in 64-bit mode. My understanding is that this is too restrictive, and that the base conditions are different for different revisions of the ISA:

MIPS IV:
COP1X instructions are available when the XX (CU3) bit of the status register is set. This bit can be set independently of UX and FR, and controls the core MIPS IV instructions as well as the FPU ones.

This part is, sadly, not fully correct. It depends on the CPU implementation what effect, the CU3 bit has. IIRC it behaves on some CPUs as you describe, while it is a nop on others.

(I don't know offhand which CPU did what there.)

Quoted 11 lines

Revision 1 of MIPS64:
COP1X instructions are available when 64-bit operations (as opposed to 64-bit floating-point operations) are enabled.

Revision 2 of MIPS32 and MIPS64:
COP1X instructions are available when a 64-bit FPU is present (i.e. if FCR0.F64 is set).

The same conditions apply to some non-COP1X instructions too, such as RECIP.fmt and RSQRT.fmt. Of course, some of the affected instructions really do require 64-bit mode, such as the paired-single ones.

This part is AFAIK correct.

Quoted 18 lines

The attached patch is one attempt to fix this. It's wrong if the description above is wrong.

Because the conditions are different for different ISAs, I thought it was worth adding a MIPS_HFLAG_* to say whether COP1X (and related) instructions are available. This flag is directly associated with the nabla symbol in the MIPS32 and MIPS64 opcode tables.

Any instructions that requires FR to be set (as enforced by check_cp1_64bitmode) is also affected by the nabla behaviour, so I've made check_cp1_64bitmode depend on both flags.

I've added nabla checks for RECIP.S and RSQRT.S. My understanding is that the *.D forms are unpredictable when FR is clear, and that QEMU generally uses check_cp1_64bitmode for such instructions. I've therefore replaced the existing "even register" checks with check_cp1_64bitmode, rather than add a separate nabla test.

Quoted 4 lines

I've replaced the single check_cp1_64bitmode calls in gen_flt3_ldst gen_flt3_arith with case-by-case checks. I've also made CABS.[SD] use nabla checks instead of check_cp1_64bitmode and added nabla checks to BC1ANY[24].

Looks reasonable to me, apart from that one misassumption.

Thiemo

#2 lore raw
Quoted 11 lines · Thiemo Seufer
Quoted 9 lines · Richard Sandiford

All MIPS COP1X instructions currently require the FPU to be in 64-bit mode. My understanding is that this is too restrictive, and that the base conditions are different for different revisions of the ISA:

MIPS IV:
COP1X instructions are available when the XX (CU3) bit of the status register is set. This bit can be set independently of UX and FR, and controls the core MIPS IV instructions as well as the FPU ones.

Quoted 3 lines

This part is, sadly, not fully correct. It depends on the CPU implementation what effect, the CU3 bit has. IIRC it behaves on some CPUs as you describe, while it is a nop on others.

Sorry. I'll take your word for it.

Quoted 1 line

(I don't know offhand which CPU did what there.)

(FWIW, the r10k and VR5500 do as described, and I'm pretty sure the RM7000 and RM9000 did too.)

Quoted 1 line

Looks reasonable to me, apart from that one misassumption.

What should the patch do instead for MIPS IV? Enable them unconditionally?

Richard

#3 lore raw
Quoted 18 lines · Richard Sandiford
Quoted 11 lines · Thiemo Seufer
Quoted 9 lines · Richard Sandiford

All MIPS COP1X instructions currently require the FPU to be in 64-bit mode. My understanding is that this is too restrictive, and that the base conditions are different for different revisions of the ISA:

MIPS IV:
COP1X instructions are available when the XX (CU3) bit of the status register is set. This bit can be set independently of UX and FR, and controls the core MIPS IV instructions as well as the FPU ones.

Quoted 3 lines

This part is, sadly, not fully correct. It depends on the CPU implementation what effect, the CU3 bit has. IIRC it behaves on some CPUs as you describe, while it is a nop on others.

Sorry. I'll take your word for it.

Quoted 8 lines
Quoted 1 line

(I don't know offhand which CPU did what there.)

(FWIW, the r10k and VR5500 do as described, and I'm pretty sure the RM7000 and RM9000 did too.)

Quoted 1 line

Looks reasonable to me, apart from that one misassumption.

What should the patch do instead for MIPS IV? Enable them unconditionally?

Given that it is currently theoretical, as the only MIPS IV CPU supported is the VR5432: Add a comment to the MIPS IV test that it is too restrictive for some CPUs.

Thiemo

#4 lore raw
Quoted 6 lines · Thiemo Seufer
Quoted 1 line · Richard Sandiford

What should the patch do instead for MIPS IV? Enable them unconditionally?

Given that it is currently theoretical, as the only MIPS IV CPU supported is the VR5432: Add a comment to the MIPS IV test that it is too restrictive for some CPUs.

Thanks, sounds good. Here's the patch with a comment added.

Richard

Index: target-mips/cpu.h
===================================================================
RCS file: /sources/qemu/qemu/target-mips/cpu.h,v
retrieving revision 1.55

cpu.h +11 -7
diff -u -p -r1.55 cpu.h
@@ -417,7 +417,7 @@ struct CPUMIPSState {
     int user_mode_only; /* user mode only simulation */
     uint32_t hflags;    /* CPU State */
     /* TMASK defines different execution modes */
-#define MIPS_HFLAG_TMASK  0x00FF
+#define MIPS_HFLAG_TMASK  0x01FF
 #define MIPS_HFLAG_MODE   0x0007 /* execution modes                    */
     /* The KSU flags must be the lowest bits in hflags. The flag order
        must be the same as defined for CP0 Status. This allows to use
@@ -431,16 +431,20 @@ struct CPUMIPSState {
 #define MIPS_HFLAG_CP0    0x0010 /* CP0 enabled                        */
 #define MIPS_HFLAG_FPU    0x0020 /* FPU enabled                        */
 #define MIPS_HFLAG_F64    0x0040 /* 64-bit FPU enabled                 */
-#define MIPS_HFLAG_RE     0x0080 /* Reversed endianness                */
+    /* True if the MIPS IV COP1X instructions can be used.  This also
+       controls the non-COP1X instructions RECIP.S, RECIP.D, RSQRT.S
+       and RSQRT.D.  */
+#define MIPS_HFLAG_COP1X  0x0080 /* COP1X instructions enabled         */
+#define MIPS_HFLAG_RE     0x0100 /* Reversed endianness                */
     /* If translation is interrupted between the branch instruction and
      * the delay slot, record what type of branch it is so that we can
      * resume translation properly.  It might be possible to reduce
      * this from three bits to two.  */
-#define MIPS_HFLAG_BMASK  0x0700
-#define MIPS_HFLAG_B      0x0100 /* Unconditional branch               */
-#define MIPS_HFLAG_BC     0x0200 /* Conditional branch                 */
-#define MIPS_HFLAG_BL     0x0300 /* Likely branch                      */
-#define MIPS_HFLAG_BR     0x0400 /* branch to register (can't link TB) */
+#define MIPS_HFLAG_BMASK  0x0e00
+#define MIPS_HFLAG_B      0x0200 /* Unconditional branch               */
+#define MIPS_HFLAG_BC     0x0400 /* Conditional branch                 */
+#define MIPS_HFLAG_BL     0x0600 /* Likely branch                      */
+#define MIPS_HFLAG_BR     0x0800 /* branch to register (can't link TB) */
     target_ulong btarget;        /* Jump / branch target               */
     int bcond;                   /* Branch condition (if needed)       */
 

Index: target-mips/exec.h
===================================================================
RCS file: /sources/qemu/qemu/target-mips/exec.h,v
retrieving revision 1.44

exec.h +16 -2
diff -u -p -r1.44 exec.h
@@ -237,8 +237,8 @@ static always_inline int cpu_halted(CPUS
 
 static always_inline void compute_hflags(CPUState *env)
 {
-    env->hflags &= ~(MIPS_HFLAG_64 | MIPS_HFLAG_CP0 | MIPS_HFLAG_F64 |
-                     MIPS_HFLAG_FPU | MIPS_HFLAG_KSU);
+    env->hflags &= ~(MIPS_HFLAG_COP1X | MIPS_HFLAG_64 | MIPS_HFLAG_CP0 |
+                     MIPS_HFLAG_F64 | MIPS_HFLAG_FPU | MIPS_HFLAG_KSU);
     if (!(env->CP0_Status & (1 << CP0St_EXL)) &&
         !(env->CP0_Status & (1 << CP0St_ERL)) &&
         !(env->hflags & MIPS_HFLAG_DM)) {
@@ -257,6 +257,20 @@ static always_inline void compute_hflags
         env->hflags |= MIPS_HFLAG_FPU;
     if (env->CP0_Status & (1 << CP0St_FR))
         env->hflags |= MIPS_HFLAG_F64;
+    if (env->insn_flags & ISA_MIPS32R2) {
+        if (env->fpu->fcr0 & FCR0_F64)
+            env->hflags |= MIPS_HFLAG_COP1X;
+    } else if (env->insn_flags & ISA_MIPS32) {
+        if (env->hflags & MIPS_HFLAG_64)
+            env->hflags |= MIPS_HFLAG_COP1X;
+    } else if (env->insn_flags & ISA_MIPS4) {
+        /* All supported MIPS IV CPUs use the XX (CU3) to enable
+           and disable the MIPS IV extensions to the MIPS III ISA.
+           Some other MIPS IV CPUs ignore the bit, so the check here
+           would be too restrictive for them.  */
+        if (env->CP0_Status & (1 << CP0St_CU3))
+            env->hflags |= MIPS_HFLAG_COP1X;
+    }
 }
 
 #endif /* !defined(__QEMU_MIPS_EXEC_H__) */

Index: target-mips/translate.c
===================================================================
RCS file: /sources/qemu/qemu/target-mips/translate.c,v retrieving revision 1.119

translate.c +47 -9

225 lines not on this page · raw message