Skip to contentSkip to conversation

Incorrect handling of PPC64 rldcl insn

Qemu-devel Torbjorn Granlund 4 participants 21 messages May 6, 2013 – May 8, 2013

lore
Share & tools
lore
#0 lore raw

I could finally make Debian GNU/Linux install and run under qemu-system-ppc64. I used Debian 7.0.0 and qemu from the main git repo, updated a few days ago.

While Debian runs well and not too slowly, GMP fails badly under all ABIs, and in many different ways. I have isolated the first problem.

Test case:

#include <stdio.h>
int
main ()
{
unsigned long r;
asm ("rldcl\t%0, %1, %2, 0" : "=r" (r) : "r" (0xcafebabedeadbeeful), "r" (16)); printf ("%lx\n", r);
return 0;
}

Expected output:
babedeadbeefcafe

Output under qemu:
0

I have single stepped in gdb to determine that it is indeed rldcl that misbehaves.

Signature · 1 line
Torbjörn
#1 lore raw
Quoted 19 lines · Torbjorn Granlund

I could finally make Debian GNU/Linux install and run under qemu-system-ppc64. I used Debian 7.0.0 and qemu from the main git repo, updated a few days ago.

While Debian runs well and not too slowly, GMP fails badly under all ABIs, and in many different ways. I have isolated the first problem.

Test case:

#include<stdio.h>
int
main ()

{
   unsigned long r;
   asm ("rldcl\t%0, %1, %2, 0" : "=r" (r) : "r" (0xcafebabedeadbeeful), "r" (16));
   printf ("%lx\n", r);
   return 0;
}
Quoted 8 lines

Expected output:
babedeadbeefcafe

Output under qemu:
0

I have single stepped in gdb to determine that it is indeed rldcl that misbehaves.

Thanks a lot for the bug report and test case! Please CC qemu-ppc whenever you find issues or have patches for PPC. That makes filtering for important mails a lot easier.

Does the patch below fix the issue for you?

Alex

target-ppc/translate.c +0 -2
index 0886f4d..a018616 100644
@@ -1733,8 +1733,6 @@ static inline void gen_rldnm(DisasContext *ctx, uint32_t mb, uint32_t me)
 {
     TCGv t0;
 
-    mb = MB(ctx->opcode);
-    me = ME(ctx->opcode);
     t0 = tcg_temp_new();
     tcg_gen_andi_tl(t0, cpu_gpr[rB(ctx->opcode)], 0x3f);
     tcg_gen_rotl_tl(t0, cpu_gpr[rS(ctx->opcode)], t0);
#2 lore raw

Alexander Graf <[email protected]> writes:

Thanks a lot for the bug report and test case! Please CC qemu-ppc whenever you find issues or have patches for PPC. That makes filtering for important mails a lot easier.

Would that make my complaints be considered more or less important? :-)

Does the patch below fix the issue for you?

It indeed does. (I actually tried that already, but I cannot follow the data flow into these functions, so cannot tell if that patch is sufficient. This bug indicates complete non-testing status of these insns, which are mainstream enough to be generated by gcc. I suppose there will likely be more such fundamental errors if more instructions are also completely untested.)

Signature · 1 line
Torbjörn
#3 lore raw
Quoted 6 lines · Torbjorn Granlund

Alexander Graf <[email protected]> writes:

Thanks a lot for the bug report and test case! Please CC qemu-ppc whenever you find issues or have patches for PPC. That makes filtering for important mails a lot easier.

Quoted 7 lines

Would that make my complaints be considered more or less important? :-)

Does the patch below fix the issue for you?

It indeed does. (I actually tried that already, but I cannot follow the data flow into these functions, so cannot tell if that patch is sufficient.

Yes, it is. It's a leftover bug from converting the code to TCG I assume.

Quoted 4 lines

This bug indicates complete non-testing status of these insns, which are mainstream enough to be generated by gcc. I suppose there will likely be more such fundamental errors if more instructions are also completely untested.)

There's a certain chance that happens, yes. We don't have instruction test suites for the PPC target.

Alex

#4 lore raw
Quoted 11 lines · Alexander Graf
Quoted 8 lines · Torbjorn Granlund

Alexander Graf <[email protected]> writes:

Thanks a lot for the bug report and test case! Please CC qemu-ppc whenever you find issues or have patches for PPC. That makes filtering for important mails a lot easier.

Would that make my complaints be considered more or less important? :-)

Quoted 7 lines
Quoted 5 lines

Does the patch below fix the issue for you?

It indeed does. (I actually tried that already, but I cannot follow the data flow into these functions, so cannot tell if that patch is sufficient.

Yes, it is. It's a leftover bug from converting the code to TCG I assume.

Yes, looks like I am the culprit here.

Quoted 7 lines
Quoted 4 lines

This bug indicates complete non-testing status of these insns, which are mainstream enough to be generated by gcc. I suppose there will likely be more such fundamental errors if more instructions are also completely untested.)

There's a certain chance that happens, yes. We don't have instruction test suites for the PPC target.

We have the Gwenole Beauschene testsuite for the PPC32 target, even if it doesn't work when compiled on a recent distribution, one has to use the old binary. It currently passes, so the PPC32 and Altivec instructions should be fine.

On the contrary, the PPC64 instructions are untested, and there are likely a few bugs like this one left, especially on complex instructions.

Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#5 lore raw

[Qemu-devel] Incorrect handling of more PPC64 insns

In reply to Alexander Graf

Alexander Graf <[email protected]> writes:

There's a certain chance that happens, yes. We don't have instruction test suites for the PPC target.

There certainly are more bugs. GMP still crashes all over the place. I have semi-isolated one more.

Extracted stand-alone sources:

Asm code generated on gcc110 from the source file:

Generate executable and execute:

gcc -m32 -mpowerpc64 bug-qemu-ppc-again.s && ./a.out

This runs silently as it should on real hardware. Under qemu (from 2013-05-02 plus the rldcl patch) I incorrectly get the error message:

GMP_NUMB_CEIL_MAX_DIV3 too small

This seems reproducible every time, unlike most qemu bugs that hit GMP. I haven't isolated this bug to a single instruction, but if rldcl was untested, expecting all of there here used rldicl rldimi rlwinm to be tested is perhaps over-optimistic?

Signature · 1 line
Torbjörn
#6 lore raw

Re: [Qemu-devel] Incorrect handling of more PPC64 insns

Quoted 6 lines · Torbjorn Granlund

Alexander Graf <[email protected]> writes:

There's a certain chance that happens, yes. We don't have instruction test suites for the PPC target.

There certainly are more bugs. GMP still crashes all over the place.

If you want to more seriously test the PPC instructions it might be worth extending risu to cope with more than just the ARM architecture...
https://wiki.linaro.org/PeterMaydell/Risu
That would be a moderate chunk of work -- about 300 lines of C code in the client, plus refactoring the generator perl script to separate out the architecture-dependent bits and add ppc support. The advantage is that once you've done it it's very easy to add support for testing a single instruction, provided you have a known-good hardware implementation to be the reference.

(one day soon I will have to make it cope with aarch64)

-- PMM

#7 lore raw

Re: [Qemu-devel] Incorrect handling of more PPC64 insns

Peter Maydell <[email protected]> writes:

Quoted 6 lines · Torbjorn Granlund

Alexander Graf <[email protected]> writes:

There's a certain chance that happens, yes. We don't have instruction test suites for the PPC target.

There certainly are more bugs. GMP still crashes all over the place.

If you want to more seriously test the PPC instructions it might be worth extending risu to cope with more than just the ARM architecture...

I am an involuntary tester of lots of software, and a voluntary tester of my own software.

I think we should do testing of the software we write ourselves, since we else hurt the productivity of the community.

I waste half my hacking time on other hackers' plain bugs.

But no, I will not become a voluntary tester of the qemu hackers' untested code...

Signature · 1 line
Torbjörn
#8 lore raw

Re: [Qemu-devel] Incorrect handling of more PPC64 insns

Quoted 15 lines · Torbjorn Granlund

Peter Maydell <[email protected]> writes:

Quoted 5 lines · Torbjorn Granlund

Alexander Graf <[email protected]> writes:
There's a certain chance that happens, yes. We don't have instruction test suites for the PPC target.

There certainly are more bugs. GMP still crashes all over the place.

If you want to more seriously test the PPC instructions it might be worth extending risu to cope with more than just the ARM architecture...

I am an involuntary tester of lots of software, and a voluntary tester of my own software.

Quoted 7 lines

I think we should do testing of the software we write ourselves, since we else hurt the productivity of the community.

I waste half my hacking time on other hackers' plain bugs.

But no, I will not become a voluntary tester of the qemu hackers' untested code...

Sorry, I should have phrased myself more clearly. It was more meant as a suggestion for anybody reading qemu-ppc/qemu-devel who was interested.

-- PMM

#9 lore raw

Re: [Qemu-devel] Incorrect handling of more PPC64 insns (PATCH)

In reply to Torbjorn Granlund

OK, so took to reading some of translate to see how well it agrees with the PPC architecture definition.

I spotted a bug with cmp, which was repeated 4 times. Somebody decided that NARROW_MODE should affect the handling of cmp instructions, which is contrary to the ISA documentation.

The first hunk is just a comment about suspicious code. I don't suggest to apply that.

Incidentally, this patch makes GMP testing go a bit further, and the testcase bug-qemu-ppc-again.s works correctly.

target-ppc/translate.c +5 -4
index 1a84653..c44b96d 100644
@@ -665,6 +665,7 @@ static inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
 
 static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
 {
+// suspicious code -- tege
     if (NARROW_MODE(ctx)) {
         gen_op_cmpi32(reg, 0, 1, 0);
     } else {
@@ -675,7 +676,7 @@ static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
 /* cmp */
 static void gen_cmp(DisasContext *ctx)
 {
-    if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
+    if (!(ctx->opcode & 0x00200000)) {
         gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                      1, crfD(ctx->opcode));
     } else {
@@ -687,7 +688,7 @@ static void gen_cmp(DisasContext *ctx)
 /* cmpi */
 static void gen_cmpi(DisasContext *ctx)
 {
-    if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
+    if (!(ctx->opcode & 0x00200000)) {
         gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
                       1, crfD(ctx->opcode));
     } else {
@@ -699,7 +700,7 @@ static void gen_cmpi(DisasContext *ctx)
 /* cmpl */
 static void gen_cmpl(DisasContext *ctx)
 {
-    if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
+    if (!(ctx->opcode & 0x00200000)) {
         gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                      0, crfD(ctx->opcode));
     } else {
@@ -711,7 +712,7 @@ static void gen_cmpl(DisasContext *ctx)
 /* cmpli */
 static void gen_cmpli(DisasContext *ctx)
 {
-    if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
+    if (!(ctx->opcode & 0x00200000)) {
         gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
                       0, crfD(ctx->opcode));
     } else {
Signature · 1 line
Torbjörn
#10 lore raw

Re: [Qemu-devel] [Qemu-ppc] Incorrect handling of more PPC64 insns (PATCH)

Quoted 7 lines · Torbjorn Granlund

OK, so took to reading some of translate to see how well it agrees with the PPC architecture definition.

I spotted a bug with cmp, which was repeated 4 times. Somebody decided that NARROW_MODE should affect the handling of cmp instructions, which is contrary to the ISA documentation.

Quoted 5 lines

The first hunk is just a comment about suspicious code. I don't suggest to apply that.

Incidentally, this patch makes GMP testing go a bit further, and the testcase bug-qemu-ppc-again.s works correctly.

Richard, could you please proof read this?

Thanks!

Alex

Quoted 50 lines
target-ppc/translate.c +5 -4
index 1a84653..c44b96d 100644
@@ -665,6 +665,7 @@ static inline void gen_op_cmpi32(TCGv arg0, target_ulong arg1, int s, int crf)
 
  static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
  {
+// suspicious code -- tege
      if (NARROW_MODE(ctx)) {
          gen_op_cmpi32(reg, 0, 1, 0);
      } else {
@@ -675,7 +676,7 @@ static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
  /* cmp */
  static void gen_cmp(DisasContext *ctx)
  {
-    if (NARROW_MODE(ctx) || !(ctx->opcode&  0x00200000)) {
+    if (!(ctx->opcode&  0x00200000)) {
          gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                       1, crfD(ctx->opcode));
      } else {
@@ -687,7 +688,7 @@ static void gen_cmp(DisasContext *ctx)
  /* cmpi */
  static void gen_cmpi(DisasContext *ctx)
  {
-    if (NARROW_MODE(ctx) || !(ctx->opcode&  0x00200000)) {
+    if (!(ctx->opcode&  0x00200000)) {
          gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
                        1, crfD(ctx->opcode));
      } else {
@@ -699,7 +700,7 @@ static void gen_cmpi(DisasContext *ctx)
  /* cmpl */
  static void gen_cmpl(DisasContext *ctx)
  {
-    if (NARROW_MODE(ctx) || !(ctx->opcode&  0x00200000)) {
+    if (!(ctx->opcode&  0x00200000)) {
          gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                       0, crfD(ctx->opcode));
      } else {
@@ -711,7 +712,7 @@ static void gen_cmpl(DisasContext *ctx)
  /* cmpli */
  static void gen_cmpli(DisasContext *ctx)
  {
-    if (NARROW_MODE(ctx) || !(ctx->opcode&  0x00200000)) {
+    if (!(ctx->opcode&  0x00200000)) {
          gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
                        0, crfD(ctx->opcode));
      } else {
#11 lore raw

Re: [Qemu-devel] [Qemu-ppc] Incorrect handling of more PPC64 insns (PATCH)

Quoted 2 lines · Alexander Graf

The first hunk is just a comment about suspicious code. I don't suggest to apply that.

The "suspicious" code is correct. The Rc update should indeed be SF-mode dependent.

With the other 4 hunks, qemu-ppc64 is now able to execute GMP's testsuite to completion, using a shared lib build. (The static build has yet to complete.)

Signature · 1 line
Torbjörn
#12 lore raw

Re: [Qemu-devel] [Qemu-ppc] Incorrect handling of more PPC64 insns (PATCH)

I realised a possible problem with my suggested patch.

What about a 32-bit processor? Then NARROW_MODE macro is identical 0.

The pre-patch behaviour was then to ignore the L bit and decode both 32-bit and 64-bit instruction in the same way.

Apparently that is correct behaviour. (The manual is slightly vague, but I let hardware decide.)

With my patch, the bit is not ignored, and invalid code will be generated for 32-bit targets, if they'd set the L bit.

Here is an uglier but hopefully completely correct patch.

target-ppc/translate.c +20 -4
index 1a84653..69d684c 100644
@@ -675,49 +675,65 @@ static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
 /* cmp */
 static void gen_cmp(DisasContext *ctx)
 {
-    if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
+#if defined(TARGET_PPC64)
+    if (!(ctx->opcode & 0x00200000)) {
+#endif
         gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                      1, crfD(ctx->opcode));
+#if defined(TARGET_PPC64)
     } else {
         gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                    1, crfD(ctx->opcode));
     }
+#endif
 }
 
 /* cmpi */
 static void gen_cmpi(DisasContext *ctx)
 {
-    if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
+#if defined(TARGET_PPC64)
+    if (!(ctx->opcode & 0x00200000)) {
+#endif
         gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
                       1, crfD(ctx->opcode));
+#if defined(TARGET_PPC64)
     } else {
         gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
                     1, crfD(ctx->opcode));
     }
+#endif
 }
 
 /* cmpl */
 static void gen_cmpl(DisasContext *ctx)
 {
-    if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
+#if defined(TARGET_PPC64)
+    if (!(ctx->opcode & 0x00200000)) {
+#endif
         gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                      0, crfD(ctx->opcode));
+#if defined(TARGET_PPC64)
     } else {
         gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                    0, crfD(ctx->opcode));
     }
+#endif
 }
 
 /* cmpli */
 static void gen_cmpli(DisasContext *ctx)
 {
-    if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
+#if defined(TARGET_PPC64)
+    if (!(ctx->opcode & 0x00200000)) {
+#endif
         gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
                       0, crfD(ctx->opcode));
+#if defined(TARGET_PPC64)
     } else {
         gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
                     0, crfD(ctx->opcode));
     }
+#endif
 }
 
 /* isel (PowerPC 2.03 specification) */
Signature · 1 line
Torbjörn
#13 lore raw

Re: [Qemu-devel] [Qemu-ppc] Incorrect handling of more PPC64 insns (PATCH)

Am 07.05.2013 um 21:30 schrieb Torbjorn Granlund <[email protected]>:

Quoted 20 lines

I realised a possible problem with my suggested patch.

What about a 32-bit processor? Then NARROW_MODE macro is identical 0.

The pre-patch behaviour was then to ignore the L bit and decode both 32-bit and 64-bit instruction in the same way.

Apparently that is correct behaviour. (The manual is slightly vague, but I let hardware decide.)

With my patch, the bit is not ignored, and invalid code will be generated for 32-bit targets, if they'd set the L bit.

Here is an uglier but hopefully completely correct patch.

target-ppc/translate.c +0 -0
index 1a84653..69d684c 100644

target-ppc/translate.c static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg) · line 679

Quoted 6 lines
target-ppc/translate.c +2 -1
@@ -675,49 +675,65 @@ static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
 /* cmp */
 static void gen_cmp(DisasContext *ctx)
 {
-    if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
+#if defined(TARGET_PPC64)
+    if (!(ctx->opcode & 0x00200000)) {

The ppc64 target can also execute as ppc32 CPU if you pass in the correct -cpu value. So this one looks slightly bogus...

Alex

Quoted 66 lines
target-ppc/translate.c +18 -3
+#endif
        gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                     1, crfD(ctx->opcode));
+#if defined(TARGET_PPC64)
    } else {
        gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                   1, crfD(ctx->opcode));
    }
+#endif
 }
 
 /* cmpi */
 static void gen_cmpi(DisasContext *ctx)
 {
-    if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
+#if defined(TARGET_PPC64)
+    if (!(ctx->opcode & 0x00200000)) {
+#endif
        gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
                      1, crfD(ctx->opcode));
+#if defined(TARGET_PPC64)
    } else {
        gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
                    1, crfD(ctx->opcode));
    }
+#endif
 }
 
 /* cmpl */
 static void gen_cmpl(DisasContext *ctx)
 {
-    if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
+#if defined(TARGET_PPC64)
+    if (!(ctx->opcode & 0x00200000)) {
+#endif
        gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                     0, crfD(ctx->opcode));
+#if defined(TARGET_PPC64)
    } else {
        gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                   0, crfD(ctx->opcode));
    }
+#endif
 }
 
 /* cmpli */
 static void gen_cmpli(DisasContext *ctx)
 {
-    if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
+#if defined(TARGET_PPC64)
+    if (!(ctx->opcode & 0x00200000)) {
+#endif
        gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
                      0, crfD(ctx->opcode));
+#if defined(TARGET_PPC64)
    } else {
        gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
                    0, crfD(ctx->opcode));
    }
+#endif
 }
 
 /* isel (PowerPC 2.03 specification) */
Signature · 2 lines
-- 
Torbjörn
#14 lore raw

Re: [Qemu-devel] [Qemu-ppc] Incorrect handling of more PPC64 insns (PATCH)

In reply to Torbjorn Granlund
Quoted 26 lines · Torbjorn Granlund

I realised a possible problem with my suggested patch.

What about a 32-bit processor? Then NARROW_MODE macro is identical 0.

The pre-patch behaviour was then to ignore the L bit and decode both 32-bit and 64-bit instruction in the same way.

Apparently that is correct behaviour. (The manual is slightly vague, but I let hardware decide.)

With my patch, the bit is not ignored, and invalid code will be generated for 32-bit targets, if they'd set the L bit.

Here is an uglier but hopefully completely correct patch.

target-ppc/translate.c +2 -1
index 1a84653..69d684c 100644
@@ -675,49 +675,65 @@ static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
 /* cmp */
 static void gen_cmp(DisasContext *ctx)
 {
-    if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
+#if defined(TARGET_PPC64)
+    if (!(ctx->opcode & 0x00200000)) {

target-ppc/translate.c static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg) · line 689

Quoted 10 lines
target-ppc/translate.c +3 -0
+#endif
         gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                      1, crfD(ctx->opcode));
+#if defined(TARGET_PPC64)
     } else {
         gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                    1, crfD(ctx->opcode));
     }
+#endif
 }

I agree that there is a bug there, and that cmp32 should be used with when L=0. That said your code is not going to generate and invalid code on a 32-bit CPU with L=1, but instead just skip the instruction. Moreover as Alexander pointed, TARGET_PPC64 doesn't mean it's a 64-bit CPU, but that the resulting qemu binaries support 64-bit CPU.

What about the following patch (only lightly tested).

From: Aurelien Jarno <[email protected]>

target-ppc: fix cmp instructions on 64-bit CPUs

64-bit CPUs check for the L bit of comparison instruction to determine if the instruction is 32-bit wide, and not to the MSR SF bit.

L=1 on a 32-bit CPU should generate an invalid instruction exception.

---
 target-ppc/translate.c |   48 ++++++++++++++++++++++++++++++++----------------
 1 file changed, 32 insertions(+), 16 deletions(-)
target-ppc/translate.c +32 -16
index 0886f4d..ab41dc1 100644
@@ -675,48 +675,64 @@ static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
 /* cmp */
 static void gen_cmp(DisasContext *ctx)
 {
-    if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
+    if (ctx->opcode & 0x00200000) {
+        if (unlikely(!(ctx->insns_flags & PPC_64B))) {
+            gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
+        } else {
+            gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
+                       1, crfD(ctx->opcode));
+        }
+    } else {
         gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                      1, crfD(ctx->opcode));
-    } else {
-        gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
-                   1, crfD(ctx->opcode));
     }
 }
 
 /* cmpi */
 static void gen_cmpi(DisasContext *ctx)
 {
-    if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
+    if (ctx->opcode & 0x00200000) {
+        if (unlikely(!(ctx->insns_flags & PPC_64B))) {
+            gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
+        } else {
+            gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
+                        1, crfD(ctx->opcode));
+        }
+    } else {
         gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
                       1, crfD(ctx->opcode));
-    } else {
-        gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
-                    1, crfD(ctx->opcode));
     }
 }
 
 /* cmpl */
 static void gen_cmpl(DisasContext *ctx)
 {
-    if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
+    if (ctx->opcode & 0x00200000) {
+        if (unlikely(!(ctx->insns_flags & PPC_64B))) {
+            gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
+        } else {
+            gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
+                       0, crfD(ctx->opcode));
+        }
+    } else {
         gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                      0, crfD(ctx->opcode));
-    } else {
-        gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
-                   0, crfD(ctx->opcode));
     }
 }
 
 /* cmpli */
 static void gen_cmpli(DisasContext *ctx)
 {
-    if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
+    if (ctx->opcode & 0x00200000) {
+        if (unlikely(!(ctx->insns_flags & PPC_64B))) {
+            gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
+        } else {
+            gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
+                        0, crfD(ctx->opcode));
+        }
+    } else {
         gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
                       0, crfD(ctx->opcode));
-    } else {
-        gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
-                    0, crfD(ctx->opcode));
     }
 }
 
Signature · 7 lines
1.7.10.4

 

-- 
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#15 lore raw

Re: [Qemu-devel] [Qemu-ppc] Incorrect handling of more PPC64 insns (PATCH)

Quoted 67 lines · Aurelien Jarno
Quoted 26 lines · Torbjorn Granlund

I realised a possible problem with my suggested patch.

What about a 32-bit processor? Then NARROW_MODE macro is identical 0.

The pre-patch behaviour was then to ignore the L bit and decode both 32-bit and 64-bit instruction in the same way.

Apparently that is correct behaviour. (The manual is slightly vague, but I let hardware decide.)

With my patch, the bit is not ignored, and invalid code will be generated for 32-bit targets, if they'd set the L bit.

Here is an uglier but hopefully completely correct patch.

target-ppc/translate.c +2 -1
index 1a84653..69d684c 100644
@@ -675,49 +675,65 @@ static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
 /* cmp */
 static void gen_cmp(DisasContext *ctx)
 {
-    if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
+#if defined(TARGET_PPC64)
+    if (!(ctx->opcode & 0x00200000)) {

target-ppc/translate.c static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg) · line 689

Quoted 10 lines
target-ppc/translate.c +3 -0
+#endif
        gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                     1, crfD(ctx->opcode));
+#if defined(TARGET_PPC64)
    } else {
        gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                   1, crfD(ctx->opcode));
    }
+#endif
 }

I agree that there is a bug there, and that cmp32 should be used with when L=0. That said your code is not going to generate and invalid code on a 32-bit CPU with L=1, but instead just skip the instruction. Moreover as Alexander pointed, TARGET_PPC64 doesn't mean it's a 64-bit CPU, but that the resulting qemu binaries support 64-bit CPU.

What about the following patch (only lightly tested).

From: Aurelien Jarno <[email protected]>

target-ppc: fix cmp instructions on 64-bit CPUs

64-bit CPUs check for the L bit of comparison instruction to determine if the instruction is 32-bit wide, and not to the MSR SF bit.

L=1 on a 32-bit CPU should generate an invalid instruction exception.

---
target-ppc/translate.c | 48 ++++++++++++++++++++++++++++++++----------------

1 file changed, 32 insertions(+), 16 deletions(-)
target-ppc/translate.c +0 -0
index 0886f4d..ab41dc1 100644

target-ppc/translate.c static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg) · line 680

Quoted 7 lines
target-ppc/translate.c +3 -1
@@ -675,48 +675,64 @@ static inline void gen_set_Rc0(DisasContext *ctx, TCGv reg)
 /* cmp */
 static void gen_cmp(DisasContext *ctx)
 {
-    if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
+    if (ctx->opcode & 0x00200000) {
+        if (unlikely(!(ctx->insns_flags & PPC_64B))) {
+            gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);

Can't we handle this through the reserved bits in the instruction map?

Alex

Quoted 81 lines
target-ppc/translate.c +29 -15
+        } else {
+            gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
+                       1, crfD(ctx->opcode));
+        }
+    } else {
        gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                     1, crfD(ctx->opcode));
-    } else {
-        gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
-                   1, crfD(ctx->opcode));
    }
 }
 
 /* cmpi */
 static void gen_cmpi(DisasContext *ctx)
 {
-    if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
+    if (ctx->opcode & 0x00200000) {
+        if (unlikely(!(ctx->insns_flags & PPC_64B))) {
+            gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
+        } else {
+            gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
+                        1, crfD(ctx->opcode));
+        }
+    } else {
        gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
                      1, crfD(ctx->opcode));
-    } else {
-        gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], SIMM(ctx->opcode),
-                    1, crfD(ctx->opcode));
    }
 }
 
 /* cmpl */
 static void gen_cmpl(DisasContext *ctx)
 {
-    if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
+    if (ctx->opcode & 0x00200000) {
+        if (unlikely(!(ctx->insns_flags & PPC_64B))) {
+            gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
+        } else {
+            gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
+                       0, crfD(ctx->opcode));
+        }
+    } else {
        gen_op_cmp32(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
                     0, crfD(ctx->opcode));
-    } else {
-        gen_op_cmp(cpu_gpr[rA(ctx->opcode)], cpu_gpr[rB(ctx->opcode)],
-                   0, crfD(ctx->opcode));
    }
 }
 
 /* cmpli */
 static void gen_cmpli(DisasContext *ctx)
 {
-    if (NARROW_MODE(ctx) || !(ctx->opcode & 0x00200000)) {
+    if (ctx->opcode & 0x00200000) {
+        if (unlikely(!(ctx->insns_flags & PPC_64B))) {
+            gen_inval_exception(ctx, POWERPC_EXCP_INVAL_INVAL);
+        } else {
+            gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
+                        0, crfD(ctx->opcode));
+        }
+    } else {
        gen_op_cmpi32(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
                      0, crfD(ctx->opcode));
-    } else {
-        gen_op_cmpi(cpu_gpr[rA(ctx->opcode)], UIMM(ctx->opcode),
-                    0, crfD(ctx->opcode));
    }
 }
 
Signature · 7 lines
1.7.10.4



-- 
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#16 lore raw

Re: [Qemu-devel] [Qemu-ppc] Incorrect handling of more PPC64 insns (PATCH)

In reply to Aurelien Jarno

Aurelien Jarno <[email protected]> writes:

64-bit CPUs check for the L bit of comparison instruction to determine if the instruction is 32-bit wide, and not to the MSR SF bit.

L=1 on a 32-bit CPU should generate an invalid instruction exception.

No. See my previous post.

The L bit is to be ignored for 32-bit CPUs, just like the original code did.

Signature · 1 line
Torbjörn
#17 lore raw

Re: [Qemu-devel] [Qemu-ppc] Incorrect handling of more PPC64 insns (PATCH)

Quoted 11 lines · Torbjorn Granlund

Aurelien Jarno <[email protected]> writes:

64-bit CPUs check for the L bit of comparison instruction to determine if the instruction is 32-bit wide, and not to the MSR SF bit.

L=1 on a 32-bit CPU should generate an invalid instruction exception.

No. See my previous post.

The L bit is to be ignored for 32-bit CPUs, just like the original code did.

I see. So if the target is 64bit capable, then we distinguish by the instruction bit, but for 32bit targets we always call the 32bit variant regardless of the bit?

Alex

#18 lore raw

Re: [Qemu-devel] [Qemu-ppc] Incorrect handling of more PPC64 insns (PATCH)

Quoted 8 lines · Alexander Graf
Quoted 5 lines · Torbjorn Granlund

Aurelien Jarno <[email protected]> writes:

64-bit CPUs check for the L bit of comparison instruction to determine if the instruction is 32-bit wide, and not to the MSR SF bit.

Quoted 8 lines
Quoted 6 lines

L=1 on a 32-bit CPU should generate an invalid instruction exception.

No. See my previous post.

The L bit is to be ignored for 32-bit CPUs, just like the original code did.

I see. So if the target is 64bit capable, then we distinguish by the instruction bit, but for 32bit targets we always call the 32bit variant regardless of the bit?

Ok, so the real problem here is that NARROW_MODE is not set, but is used to differentiate whether to use the 32bit cmp only or not.

Richard, there are 2 ways out of this:

1) get rid of NARROW_MODE and always check ctx->sf
2) add a new 32bit only insns flag and create separate functions for 32bit cmp calls

I have a patch set ready for 2, but I think 1 would be the better alternative. The only cases I could spot where things could break is in the add/sub corner case. Let me try to cook up something.

Alex

#19 lore raw

Re: [Qemu-devel] [Qemu-ppc] Incorrect handling of more PPC64 insns (PATCH)

Alexander Graf <[email protected]> writes:

Ok, so the real problem here is that NARROW_MODE is not set, but is used to differentiate whether to use the 32bit cmp only or not.

Eh?

Richard, there are 2 ways out of this:

1) get rid of NARROW_MODE and always check ctx->sf

No!

The cmp insn with L set should NOT be affected by SF. That's the entire point of my change.

I reviewed the other uses of NARROW_MODE and didn't spot any errors. (But I must confess that I would need to red the PPC manuals better inn order to tell for sure.)

2) add a new 32bit only insns flag and create separate functions for 32bit cmp calls

Aurelien's patch looked promising, if one removes the exception casting.

Signature · 1 line
Torbjörn
#20 lore raw

Re: [Qemu-devel] [Qemu-ppc] Incorrect handling of more PPC64 insns (PATCH)

Quoted 7 lines · Torbjorn Granlund

Alexander Graf <[email protected]> writes:

Ok, so the real problem here is that NARROW_MODE is not set, but is used to differentiate whether to use the 32bit cmp only or not.

Eh?

Quoted 8 lines

Richard, there are 2 ways out of this:

1) get rid of NARROW_MODE and always check ctx->sf

No!

The cmp insn with L set should NOT be affected by SF. That's the entire point of my change.

You're right. I got confused there :).

Quoted 8 lines

I reviewed the other uses of NARROW_MODE and didn't spot any errors. (But I must confess that I would need to red the PPC manuals better inn order to tell for sure.)

2) add a new 32bit only insns flag and create separate functions for 32bit cmp calls

Aurelien's patch looked promising, if one removes the exception casting.

His exception casting is actually correct. You can use qemu-(system-)ppc64, but run it with a CPU definition that is 32bit only, like a G3. These old CPUs did not know the instruction with L yet, so they do throw an illegal instruction exception, which we have to model.

Alex