Blame SOURCES/valgrind-3.15.0-avx-rdrand-f16c.patch

058efa
commit 791fe5ecf909d573bcbf353b677b9404f9da0ed4
058efa
Author: Mark Wielaard <mark@klomp.org>
058efa
Date:   Mon May 27 22:19:27 2019 +0200
058efa
058efa
    Expose rdrand and f16c through cpuid also if the host only has avx.
058efa
    
058efa
    The amd64 CPUID dirtyhelpers are mostly static since they emulate some
058efa
    existing CPU "family". The avx2 ("i7-4910MQ") CPUID variant however
058efa
    can "dynamicly" enable rdrand and/or f16c if the host supports them.
058efa
    Do the same for the avx_and_cx16 ("i5-2300") CPUID variant.
058efa
    
058efa
    https://bugs.kde.org/show_bug.cgi?id=408009
058efa
058efa
diff --git a/VEX/priv/guest_amd64_defs.h b/VEX/priv/guest_amd64_defs.h
058efa
index 4f34b41..a5de527 100644
058efa
--- a/VEX/priv/guest_amd64_defs.h
058efa
+++ b/VEX/priv/guest_amd64_defs.h
058efa
@@ -165,7 +165,9 @@ extern void  amd64g_dirtyhelper_storeF80le ( Addr/*addr*/, ULong/*data*/ );
058efa
 extern void  amd64g_dirtyhelper_CPUID_baseline ( VexGuestAMD64State* st );
058efa
 extern void  amd64g_dirtyhelper_CPUID_sse3_and_cx16 ( VexGuestAMD64State* st );
058efa
 extern void  amd64g_dirtyhelper_CPUID_sse42_and_cx16 ( VexGuestAMD64State* st );
058efa
-extern void  amd64g_dirtyhelper_CPUID_avx_and_cx16 ( VexGuestAMD64State* st );
058efa
+extern void  amd64g_dirtyhelper_CPUID_avx_and_cx16 ( VexGuestAMD64State* st,
058efa
+                                                     ULong hasF16C,
058efa
+                                                     ULong hasRDRAND );
058efa
 extern void  amd64g_dirtyhelper_CPUID_avx2 ( VexGuestAMD64State* st,
058efa
                                              ULong hasF16C, ULong hasRDRAND );
058efa
 
058efa
diff --git a/VEX/priv/guest_amd64_helpers.c b/VEX/priv/guest_amd64_helpers.c
058efa
index e4cf7e2..182bae0 100644
058efa
--- a/VEX/priv/guest_amd64_helpers.c
058efa
+++ b/VEX/priv/guest_amd64_helpers.c
058efa
@@ -3141,8 +3141,11 @@ void amd64g_dirtyhelper_CPUID_sse42_and_cx16 ( VexGuestAMD64State* st )
058efa
    address sizes   : 36 bits physical, 48 bits virtual
058efa
    power management:
058efa
 */
058efa
-void amd64g_dirtyhelper_CPUID_avx_and_cx16 ( VexGuestAMD64State* st )
058efa
+void amd64g_dirtyhelper_CPUID_avx_and_cx16 ( VexGuestAMD64State* st,
058efa
+                                             ULong hasF16C, ULong hasRDRAND )
058efa
 {
058efa
+   vassert((hasF16C >> 1) == 0ULL);
058efa
+   vassert((hasRDRAND >> 1) == 0ULL);
058efa
 #  define SET_ABCD(_a,_b,_c,_d)                \
058efa
       do { st->guest_RAX = (ULong)(_a);        \
058efa
            st->guest_RBX = (ULong)(_b);        \
058efa
@@ -3157,9 +3160,14 @@ void amd64g_dirtyhelper_CPUID_avx_and_cx16 ( VexGuestAMD64State* st )
058efa
       case 0x00000000:
058efa
          SET_ABCD(0x0000000d, 0x756e6547, 0x6c65746e, 0x49656e69);
058efa
          break;
058efa
-      case 0x00000001:
058efa
-         SET_ABCD(0x000206a7, 0x00100800, 0x1f9ae3bf, 0xbfebfbff);
058efa
+      case 0x00000001: {
058efa
+         // As a baseline, advertise neither F16C (ecx:29) nor RDRAND (ecx:30),
058efa
+         // but patch in support for them as directed by the caller.
058efa
+         UInt ecx_extra
058efa
+            = (hasF16C ? (1U << 29) : 0) | (hasRDRAND ? (1U << 30) : 0);
058efa
+         SET_ABCD(0x000206a7, 0x00100800, (0x1f9ae3bf | ecx_extra), 0xbfebfbff);
058efa
          break;
058efa
+      }
058efa
       case 0x00000002:
058efa
          SET_ABCD(0x76035a01, 0x00f0b0ff, 0x00000000, 0x00ca0000);
058efa
          break;
058efa
diff --git a/VEX/priv/guest_amd64_toIR.c b/VEX/priv/guest_amd64_toIR.c
058efa
index 56e992c..96dee38 100644
058efa
--- a/VEX/priv/guest_amd64_toIR.c
058efa
+++ b/VEX/priv/guest_amd64_toIR.c
058efa
@@ -22007,7 +22007,8 @@ Long dis_ESC_0F (
058efa
 
058efa
       vassert(fName); vassert(fAddr);
058efa
       IRExpr** args = NULL;
058efa
-      if (fAddr == &amd64g_dirtyhelper_CPUID_avx2) {
058efa
+      if (fAddr == &amd64g_dirtyhelper_CPUID_avx2
058efa
+          || fAddr == &amd64g_dirtyhelper_CPUID_avx_and_cx16) {
058efa
          Bool hasF16C   = (archinfo->hwcaps & VEX_HWCAPS_AMD64_F16C) != 0;
058efa
          Bool hasRDRAND = (archinfo->hwcaps & VEX_HWCAPS_AMD64_RDRAND) != 0;
058efa
          args = mkIRExprVec_3(IRExpr_GSPTR(),
058efa
diff --git a/coregrind/m_machine.c b/coregrind/m_machine.c
058efa
index 3536e57..56a28d1 100644
058efa
--- a/coregrind/m_machine.c
058efa
+++ b/coregrind/m_machine.c
058efa
@@ -1076,10 +1076,10 @@ Bool VG_(machine_get_hwcaps)( void )
058efa
         have_avx2 = (ebx & (1<<5)) != 0; /* True => have AVX2 */
058efa
      }
058efa
 
058efa
-     /* Sanity check for RDRAND and F16C.  These don't actually *need* AVX2, but
058efa
-        it's convenient to restrict them to the AVX2 case since the simulated
058efa
-        CPUID we'll offer them on has AVX2 as a base. */
058efa
-     if (!have_avx2) {
058efa
+     /* Sanity check for RDRAND and F16C.  These don't actually *need* AVX, but
058efa
+        it's convenient to restrict them to the AVX case since the simulated
058efa
+        CPUID we'll offer them on has AVX as a base. */
058efa
+     if (!have_avx) {
058efa
         have_f16c   = False;
058efa
         have_rdrand = False;
058efa
      }