thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone
ed5979
From d49e5d193dfccf6f5cfa98ccce5bd491478d563d Mon Sep 17 00:00:00 2001
ed5979
From: Richard Henderson <richard.henderson@linaro.org>
ed5979
Date: Sat, 14 Jan 2023 13:32:06 -1000
ed5979
Subject: [PATCH 7/8] target/i386: Fix BZHI instruction
ed5979
ed5979
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
ed5979
RH-MergeRequest: 154: target/i386: fix bugs in emulation of BMI instructions
ed5979
RH-Bugzilla: 2173590
ed5979
RH-Acked-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
ed5979
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
ed5979
RH-Acked-by: Bandan Das <None>
ed5979
RH-Commit: [7/7] ad6b343c09c0304ac32cc68670c49d1fc12d8cf8 (bonzini/rhel-qemu-kvm)
ed5979
ed5979
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2173590
ed5979
Upstream-Status: merged
ed5979
ed5979
We did not correctly handle N >= operand size.
ed5979
ed5979
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1374
ed5979
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
ed5979
Message-Id: <20230114233206.3118472-1-richard.henderson@linaro.org>
ed5979
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
ed5979
(cherry picked from commit 9ad2ba6e8e7fc195d0dd0b76ab38bd2fceb1bdd4)
ed5979
---
ed5979
 target/i386/tcg/emit.c.inc      | 14 +++++++-------
ed5979
 tests/tcg/i386/test-i386-bmi2.c |  3 +++
ed5979
 2 files changed, 10 insertions(+), 7 deletions(-)
ed5979
ed5979
diff --git a/target/i386/tcg/emit.c.inc b/target/i386/tcg/emit.c.inc
ed5979
index e61ae9a2e9..0d01e13002 100644
ed5979
--- a/target/i386/tcg/emit.c.inc
ed5979
+++ b/target/i386/tcg/emit.c.inc
ed5979
@@ -1147,20 +1147,20 @@ static void gen_BLSR(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
ed5979
 static void gen_BZHI(DisasContext *s, CPUX86State *env, X86DecodedInsn *decode)
ed5979
 {
ed5979
     MemOp ot = decode->op[0].ot;
ed5979
-    TCGv bound;
ed5979
+    TCGv bound = tcg_constant_tl(ot == MO_64 ? 63 : 31);
ed5979
+    TCGv zero = tcg_constant_tl(0);
ed5979
+    TCGv mone = tcg_constant_tl(-1);
ed5979
 
ed5979
-    tcg_gen_ext8u_tl(s->T1, cpu_regs[s->vex_v]);
ed5979
-    bound = tcg_constant_tl(ot == MO_64 ? 63 : 31);
ed5979
+    tcg_gen_ext8u_tl(s->T1, s->T1);
ed5979
 
ed5979
     /*
ed5979
      * Note that since we're using BMILG (in order to get O
ed5979
      * cleared) we need to store the inverse into C.
ed5979
      */
ed5979
-    tcg_gen_setcond_tl(TCG_COND_LT, cpu_cc_src, s->T1, bound);
ed5979
-    tcg_gen_movcond_tl(TCG_COND_GT, s->T1, s->T1, bound, bound, s->T1);
ed5979
+    tcg_gen_setcond_tl(TCG_COND_LEU, cpu_cc_src, s->T1, bound);
ed5979
 
ed5979
-    tcg_gen_movi_tl(s->A0, -1);
ed5979
-    tcg_gen_shl_tl(s->A0, s->A0, s->T1);
ed5979
+    tcg_gen_shl_tl(s->A0, mone, s->T1);
ed5979
+    tcg_gen_movcond_tl(TCG_COND_LEU, s->A0, s->T1, bound, s->A0, zero);
ed5979
     tcg_gen_andc_tl(s->T0, s->T0, s->A0);
ed5979
 
ed5979
     gen_op_update1_cc(s);
ed5979
diff --git a/tests/tcg/i386/test-i386-bmi2.c b/tests/tcg/i386/test-i386-bmi2.c
ed5979
index 982d4abda4..0244df7987 100644
ed5979
--- a/tests/tcg/i386/test-i386-bmi2.c
ed5979
+++ b/tests/tcg/i386/test-i386-bmi2.c
ed5979
@@ -123,6 +123,9 @@ int main(int argc, char *argv[]) {
ed5979
     result = bzhiq(mask, 0x1f);
ed5979
     assert(result == (mask & ~(-1 << 30)));
ed5979
 
ed5979
+    result = bzhiq(mask, 0x40);
ed5979
+    assert(result == mask);
ed5979
+
ed5979
     result = rorxq(0x2132435465768798, 8);
ed5979
     assert(result == 0x9821324354657687);
ed5979
 
ed5979
-- 
ed5979
2.39.1
ed5979