core-util: Fix detection when running in a VM
authorArun Raghavan <arun@arunraghavan.net>
Mon, 1 Jul 2019 01:52:55 +0000 (07:22 +0530)
committerArun Raghavan <arun@arunraghavan.net>
Thu, 4 Jul 2019 07:13:51 +0000 (07:13 +0000)
The original code that was written was trying to detect what hypervisor
we were running under, rather than testing the presence bit first. We
don't really need the former, so let's use the more comprehensive latter
instead.

Fixes: https://gitlab.freedesktop.org/pulseaudio/pulseaudio/issues/684

src/pulsecore/core-util.c

index f2748ae..270acd8 100644 (file)
@@ -3518,11 +3518,7 @@ bool pa_running_in_vm(void) {
     /* Both CPUID and DMI are x86 specific interfaces... */
 
 #ifdef HAVE_CPUID_H
-    uint32_t eax;
-    union {
-        uint32_t sig32[3];
-        char text[13];
-    } sig;
+    unsigned int eax, ebx, ecx, edx;
 #endif
 
 #ifdef __linux__
@@ -3559,24 +3555,14 @@ bool pa_running_in_vm(void) {
 
 #ifdef HAVE_CPUID_H
 
-    /* Hypervisors provide their signature on the 0x40000000 cpuid leaf.
-     * http://lwn.net/Articles/301888/
-     *
-     * XXX: Why are we checking a list of signatures instead of the
-     * "hypervisor present bit"? According to the LWN article, the "hypervisor
-     * present bit" would be available on bit 31 of ECX on leaf 0x1, and that
-     * bit would tell us directly whether we're in a virtual machine or not. */
-    pa_zero(sig);
-    if (__get_cpuid(0x40000000, &eax, &sig.sig32[0], &sig.sig32[1], &sig.sig32[2]) == 0)
+    /* Hypervisors provide presence on 0x1 cpuid leaf.
+     * http://lwn.net/Articles/301888/ */
+    if (__get_cpuid(1, &eax, &ebx, &ecx, &edx) == 0)
         return false;
 
-    if (pa_streq(sig.text, "XenVMMXenVMM") ||
-        pa_streq(sig.text, "KVMKVMKVM") ||
-        /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */
-        pa_streq(sig.text, "VMwareVMware") ||
-        /* http://msdn.microsoft.com/en-us/library/bb969719.aspx */
-        pa_streq(sig.text, "Microsoft Hv"))
+    if (ecx & 0x80000000)
         return true;
+
 #endif /* HAVE_CPUID_H */
 
 #endif /* defined(__i386__) || defined(__x86_64__) */