Skip to contentSkip to conversation

target-mips: fix incorrect behaviour for EXTP

PATCH Applied Petar Jovanovic 4 participants 7 messages May 13, 2013 – May 18, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 target-mips: fix incorrect behaviour for EXTP

From: Petar Jovanovic <[email protected]>

The mask for EXTP instruction when size=31 has not been correctly calculated.

The test (mips32-dsp/extp.c) has been extended to include the case that triggers the issue.

---
 target-mips/dsp_helper.c         |    3 +--
 tests/tcg/mips/mips32-dsp/extp.c |   18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 2 deletions(-)
target-mips/dsp_helper.c +1 -2
index 9212789..a55f866 100644
@@ -3415,8 +3415,7 @@ target_ulong helper_extp(target_ulong ac, target_ulong size, CPUMIPSState *env)
     if (sub >= -1) {
         acc = ((uint64_t)env->active_tc.HI[ac] << 32) |
               ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO);
-        temp = (acc >> (start_pos - size)) &
-               (((uint32_t)0x01 << (size + 1)) - 1);
+        temp = (acc >> (start_pos - size)) & (~0U >> (31 - size));
         set_DSPControl_efi(0, env);
     } else {
         set_DSPControl_efi(1, env);
tests/tcg/mips/mips32-dsp/extp.c +18 -0
index 21a67af..b18bdb3 100644
@@ -40,5 +40,23 @@ int main()
     dsp = (dsp >> 14) & 0x01;
     assert(dsp == 1);
 
+    ach = 0;
+    acl = 0x80000001;
+    dsp = 0x1F;
+    result = 0x80000001;
+
+    __asm
+        ("wrdsp %1\n\t"
+         "mthi %2, $ac2\n\t"
+         "mtlo %3, $ac2\n\t"
+         "extp %0, $ac2, 0x1F\n\t"
+         "rddsp %1\n\t"
+         : "=r"(rt), "+r"(dsp)
+         : "r"(ach), "r"(acl)
+        );
+    dsp = (dsp >> 14) & 0x01;
+    assert(dsp == 0);
+    assert(result == rt);
+
     return 0;
 }
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH 2/2 target-mips: fix EXTPDP and setting up pos field in the DSPControl reg

From: Petar Jovanovic <[email protected]>

This change makes sure that modifications of pos field in the DSPControl register do not trash other bits in the register. This bug can be triggered with the additional test case in mips32-dsp/extpdp.c in this commit.

In addition to this, this change corrects incorrect calculation of the mask for EXTPDP.

---
 target-mips/dsp_helper.c           |    9 ++++-----
 tests/tcg/mips/mips32-dsp/extpdp.c |   18 ++++++++++++++++++
 2 files changed, 22 insertions(+), 5 deletions(-)
target-mips/dsp_helper.c +4 -5
index a55f866..27283ed 100644
@@ -90,10 +90,10 @@ static inline void set_DSPControl_pos(uint32_t pos, CPUMIPSState *env)
     dspc = env->active_tc.DSPControl;
 #ifndef TARGET_MIPS64
     dspc = dspc & 0xFFFFFFC0;
-    dspc |= pos;
+    dspc |= (pos & 0x3F);
 #else
     dspc = dspc & 0xFFFFFF80;
-    dspc |= pos;
+    dspc |= (pos & 0x7F);
 #endif
     env->active_tc.DSPControl = dspc;
 }
@@ -3439,10 +3439,9 @@ target_ulong helper_extpdp(target_ulong ac, target_ulong size,
     if (sub >= -1) {
         acc  = ((uint64_t)env->active_tc.HI[ac] << 32) |
                ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO);
-        temp = (acc >> (start_pos - size)) &
-               (((uint32_t)0x01 << (size + 1)) - 1);
+        temp = (acc >> (start_pos - size)) & (~0U >> (31 - size));
 
-        set_DSPControl_pos(start_pos - (size + 1), env);
+        set_DSPControl_pos(sub & 0x3F, env);
         set_DSPControl_efi(0, env);
     } else {
         set_DSPControl_efi(1, env);
tests/tcg/mips/mips32-dsp/extpdp.c +18 -0
index 15ba082..79ee16e 100644
@@ -42,5 +42,23 @@ int main()
     efi = (dsp >> 14) & 0x01;
     assert(efi == 1);
 
+
+    ach = 0;
+    acl = 0;
+    dsp = 0;
+    result = 0;
+
+    __asm
+        ("wrdsp %1\n\t"
+         "mthi %2, $ac1\n\t"
+         "mtlo %3, $ac1\n\t"
+         "extpdp %0, $ac1, 0x00\n\t"
+         "rddsp %1\n\t"
+         : "=r"(rt), "+r"(dsp)
+         : "r"(ach), "r"(acl)
+        );
+    assert(dsp == 0x3F);
+    assert(result == rt);
+
     return 0;
 }
Signature · 1 line
1.7.9.5
#2 lore raw

Re: [Qemu-devel] [PATCH 2/2] target-mips: fix EXTPDP and setting up pos field in the DSPControl reg

Quoted 33 lines · Petar Jovanovic

From: Petar Jovanovic <[email protected]>

This change makes sure that modifications of pos field in the DSPControl register do not trash other bits in the register. This bug can be triggered with the additional test case in mips32-dsp/extpdp.c in this commit.

In addition to this, this change corrects incorrect calculation of the mask for EXTPDP.

---
 target-mips/dsp_helper.c           |    9 ++++-----
 tests/tcg/mips/mips32-dsp/extpdp.c |   18 ++++++++++++++++++
 2 files changed, 22 insertions(+), 5 deletions(-)
target-mips/dsp_helper.c +2 -2
index a55f866..27283ed 100644
@@ -90,10 +90,10 @@ static inline void set_DSPControl_pos(uint32_t pos, CPUMIPSState *env)
     dspc = env->active_tc.DSPControl;
 #ifndef TARGET_MIPS64
     dspc = dspc & 0xFFFFFFC0;
-    dspc |= pos;
+    dspc |= (pos & 0x3F);
 #else
     dspc = dspc & 0xFFFFFF80;
-    dspc |= pos;
+    dspc |= (pos & 0x7F);
 #endif
     env->active_tc.DSPControl = dspc;
 }

target-mips/dsp_helper.c target_ulong helper_extpdp(target_ulong ac, target_ulong size, · line 3444

Quoted 9 lines
target-mips/dsp_helper.c +2 -3
@@ -3439,10 +3439,9 @@ target_ulong helper_extpdp(target_ulong ac, target_ulong size,
     if (sub >= -1) {
         acc  = ((uint64_t)env->active_tc.HI[ac] << 32) |
                ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO);
-        temp = (acc >> (start_pos - size)) &
-               (((uint32_t)0x01 << (size + 1)) - 1);
+        temp = (acc >> (start_pos - size)) & (~0U >> (31 - size));
 
-        set_DSPControl_pos(start_pos - (size + 1), env);
+        set_DSPControl_pos(sub & 0x3F, env);

I am not sure it is correct there for the 64-bit mode.

The value that should be written should be DSPcontrolpos6..0 - (size
+ 1), therefore keeeping only last 6 bits doesn't seems correct to me. Given the change above, I think it should simply be set_DSPControl_pos(sub, env);

Quoted 35 lines
target-mips/dsp_helper.c +0 -0
         set_DSPControl_efi(0, env);
     } else {
         set_DSPControl_efi(1, env);
tests/tcg/mips/mips32-dsp/extpdp.c +18 -0
index 15ba082..79ee16e 100644
@@ -42,5 +42,23 @@ int main()
     efi = (dsp >> 14) & 0x01;
     assert(efi == 1);
 
+
+    ach = 0;
+    acl = 0;
+    dsp = 0;
+    result = 0;
+
+    __asm
+        ("wrdsp %1\n\t"
+         "mthi %2, $ac1\n\t"
+         "mtlo %3, $ac1\n\t"
+         "extpdp %0, $ac1, 0x00\n\t"
+         "rddsp %1\n\t"
+         : "=r"(rt), "+r"(dsp)
+         : "r"(ach), "r"(acl)
+        );
+    assert(dsp == 0x3F);
+    assert(result == rt);
+
     return 0;
 }
Signature · 1 line
1.7.9.5
Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
#3 lore raw

Re: [Qemu-devel] [PATCH 2/2] target-mips: fix EXTPDP and setting up pos field in the DSPControl reg

________________________________________
From: Aurelien Jarno [[email protected]]
Sent: Friday, May 17, 2013 7:34 PM
To: Petar Jovanovic
Cc: [email protected]; Petar Jovanovic
Subject: Re: [PATCH 2/2] target-mips: fix EXTPDP and setting up pos field in the DSPControl reg

I am not sure it is correct there for the 64-bit mode.

The value that should be written should be DSPcontrolpos6..0 - (size
+ 1), therefore keeeping only last 6 bits doesn't seems correct to me. Given the change above, I think it should simply be set_DSPControl_pos(sub, env);

----------

You are correct. I have submitted a second version of the patch.

Petar

#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] target-mips: fix EXTPDP and setting up pos field in the DSPControl reg

In reply to Petar Jovanovic

target-mips/dsp_helper.c target_ulong helper_extpdp(target_ulong ac, target_ulong size, · line 3442

Quoted 7 lines · Petar Jovanovic
target-mips/dsp_helper.c +1 -2
@@ -3439,10 +3439,9 @@ target_ulong helper_extpdp(target_ulong ac, target_ulong size,
     if (sub >= -1) {
         acc  = ((uint64_t)env->active_tc.HI[ac] << 32) |
                ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO);
-        temp = (acc >> (start_pos - size)) &
-               (((uint32_t)0x01 << (size + 1)) - 1);
+        temp = (acc >> (start_pos - size)) & (~0U >> (31 - size));
    temp = extract64(acc, start_pos - size, size + 1);

I think?

thanks
-- PMM

#5 lore raw

Re: [Qemu-devel] [PATCH 2/2] target-mips: fix EXTPDP and setting up pos field in the DSPControl reg

________________________________________
From: Peter Maydell [[email protected]]
Sent: Friday, May 17, 2013 8:01 PM
To: Petar Jovanovic
Cc: [email protected]; Petar Jovanovic; [email protected]
Subject: Re: [Qemu-devel] [PATCH 2/2] target-mips: fix EXTPDP and setting up pos field in the DSPControl reg

target-mips/dsp_helper.c target_ulong helper_extpdp(target_ulong ac, target_ulong size, · line 3442

Quoted 7 lines · Petar Jovanovic
target-mips/dsp_helper.c +1 -2
@@ -3439,10 +3439,9 @@ target_ulong helper_extpdp(target_ulong ac, target_ulong size,
     if (sub >= -1) {
         acc  = ((uint64_t)env->active_tc.HI[ac] << 32) |
                ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO);
-        temp = (acc >> (start_pos - size)) &
-               (((uint32_t)0x01 << (size + 1)) - 1);
+        temp = (acc >> (start_pos - size)) & (~0U >> (31 - size));
    temp = extract64(acc, start_pos - size, size + 1);

I think?

thanks
-- PMM

________________________________________

Thanks Peter, this is indeed more readable. I have just uploaded another patch set with the use of extract64 helper.

http://patchwork.ozlabs.org/patch/244721/

Note to Aurelien -
the #include "qemu/bitops.h" will cause a conflict if the patch "target-mips: clean-up in BIT_INSV" is applied first, since both patches add that line at the top of the file.

Petar

#6 lore raw
In reply to Petar Jovanovic
Quoted 46 lines · Petar Jovanovic

From: Petar Jovanovic <[email protected]>

The mask for EXTP instruction when size=31 has not been correctly calculated.

The test (mips32-dsp/extp.c) has been extended to include the case that triggers the issue.

---
 target-mips/dsp_helper.c         |    3 +--
 tests/tcg/mips/mips32-dsp/extp.c |   18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 2 deletions(-)
target-mips/dsp_helper.c +1 -2
index 9212789..a55f866 100644
@@ -3415,8 +3415,7 @@ target_ulong helper_extp(target_ulong ac, target_ulong size, CPUMIPSState *env)
     if (sub >= -1) {
         acc = ((uint64_t)env->active_tc.HI[ac] << 32) |
               ((uint64_t)env->active_tc.LO[ac] & MIPSDSP_LLO);
-        temp = (acc >> (start_pos - size)) &
-               (((uint32_t)0x01 << (size + 1)) - 1);
+        temp = (acc >> (start_pos - size)) & (~0U >> (31 - size));
         set_DSPControl_efi(0, env);
     } else {
         set_DSPControl_efi(1, env);
tests/tcg/mips/mips32-dsp/extp.c +10 -0
index 21a67af..b18bdb3 100644
@@ -40,5 +40,23 @@ int main()
     dsp = (dsp >> 14) & 0x01;
     assert(dsp == 1);
 
+    ach = 0;
+    acl = 0x80000001;
+    dsp = 0x1F;
+    result = 0x80000001;
+
+    __asm
+        ("wrdsp %1\n\t"
+         "mthi %2, $ac2\n\t"
+         "mtlo %3, $ac2\n\t"
+         "extp %0, $ac2, 0x1F\n\t"

tests/tcg/mips/mips32-dsp/extp.c int main() · line 62

Quoted 10 lines
tests/tcg/mips/mips32-dsp/extp.c +8 -0
+         "rddsp %1\n\t"
+         : "=r"(rt), "+r"(dsp)
+         : "r"(ach), "r"(acl)
+        );
+    dsp = (dsp >> 14) & 0x01;
+    assert(dsp == 0);
+    assert(result == rt);
+
     return 0;
 }

Thanks, applied.

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