Knowledge Base

|
Mechanisms to determine if software is running in a VMware virtual machine (1009458)
Details
Solution
Detecting when software is running in a VMware virtual machine relies on two mechanisms:
- Testing the CPUID hypervisor present bit
- Testing the virtual BIOS DMI information and the hypervisor port
Testing the CPUID hypervisor present bit
Sample code
int cpuid_check()
{
unsigned int eax, ebx, ecx, edx;
char hyper_vendor_id[13];
cpuid(0x1, &eax, &ebx, &ecx, &edx);
if (bit 31 of ecx is set) {
cpuid(0x40000000, &eax, &ebx, &ecx, &edx);
memcpy(hyper_vendor_id + 0, &ebx, 4);
memcpy(hyper_vendor_id + 4, &ecx, 4);
memcpy(hyper_vendor_id + 8, &edx, 4);
hyper_vendor_id[12] = '\0';
if (!strcmp(hyper_vendor_id, "VMwareVMware"))
return 1; // Success - running under VMware
}
return 0;
}
Testing the virtual BIOS DMI information and the hypervisor port
Apart from the CPUID-based method for VMware virtual machine detection, VMware also provides a fallback mechanism for the following reasons:
- This CPUID-based technique will not work for guest code running at CPL3 when VT/AMD-V is not available or not enabled.
- The hypervisor present bit and hypervisor information leaf are only defined for products based on VMware hardware version 7.
Virtual BIOS DMI information
Sample code
int dmi_check(void)
{
char string[10];
GET_BIOS_SERIAL(string);
if (!memcmp(string, "VMware-", 7) || !memcmp(string, "VMW", 3))
return 1; // DMI contains VMware specific string.
else
return 0;
}
Performing just the DMI check is insufficient because the BIOS' serial number might, by chance, contain the string "VMware-" or "VMW". You should also test the hypervisor port.
Hypervisor port
VMware implements an I/O port that programs can query to detect if software is running in a VMware hypervisor. This hypervisor port behaves differently depending on magic values in certain registers and modifies some registers as a side effect. VMware hypervisor is detected by performing an IN operation to port 0x5658 (the VMware hypervisor port).
eax = 0x564D5868 (VMware hypervisor magic value)
ebx = 0xFFFFFFFF (UINT_MAX)
ecx = 10 (Getversion command identifier)
edx = 0x5658 (hypervisor port number)
On VMware, this operation modifies the value of register ebx to 0x564D5868 (the VMware hypervisor magic value).
Sample code
#define VMWARE_HYPERVISOR_MAGIC 0x564D5868
#define VMWARE_HYPERVISOR_PORT 0x5658
#define VMWARE_PORT_CMD_GETVERSION 10
#define VMWARE_PORT(cmd, eax, ebx, ecx, edx) \
__asm__("inl (%%dx)" : \
"=a"(eax), "=c"(ecx), "=d"(edx), "=b"(ebx) : \
"0"(VMWARE_HYPERVISOR_MAGIC), \
"1"(VMWARE_PORT_CMD_##cmd), \
"2"(VMWARE_HYPERVISOR_PORT), "3"(UINT_MAX) : \
"memory");
int hypervisor_port_check(void)
{
uint32_t eax, ebx, ecx, edx;
VMWARE_PORT(GETVERSION, eax, ebx, ecx, edx);
if (ebx == VMWARE_HYPERVISOR_MAGIC)
return 1; // Success - running under VMware
else
return 0;
}
Recommended code
int Detect_VMware(void)
{
if (cpuid_check())
return 1; // Success running under VMware.
else if (dmi_check() && hypervisor_port_check())
return 1;
return 0;
}
Additional Information
For translated versions of this article, see:Tags
Request a Product Feature
- Updated:
- Categories:
- Languages:
- Product Family:
- Product(s):
- Product Version(s):

