xref: /dpdk/lib/eal/x86/rte_hypervisor.c (revision 2454b37b3345c7cc05e7cf9faa727add1c259ceb)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright 2017 Mellanox Technologies, Ltd
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #include "rte_hypervisor.h"
699a2dd95SBruce Richardson 
799a2dd95SBruce Richardson #include <stdint.h>
899a2dd95SBruce Richardson #include <string.h>
999a2dd95SBruce Richardson 
1099a2dd95SBruce Richardson #include "rte_cpuflags.h"
1199a2dd95SBruce Richardson #include "rte_cpuid.h"
1299a2dd95SBruce Richardson 
1399a2dd95SBruce Richardson /* See http://lwn.net/Articles/301888/ */
1499a2dd95SBruce Richardson #define HYPERVISOR_INFO_LEAF 0x40000000
1599a2dd95SBruce Richardson 
1699a2dd95SBruce Richardson enum rte_hypervisor
rte_hypervisor_get(void)1799a2dd95SBruce Richardson rte_hypervisor_get(void)
1899a2dd95SBruce Richardson {
1999a2dd95SBruce Richardson 	cpuid_registers_t regs;
2099a2dd95SBruce Richardson 	int reg;
2199a2dd95SBruce Richardson 	char name[13];
2299a2dd95SBruce Richardson 
2399a2dd95SBruce Richardson 	if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_HYPERVISOR))
2499a2dd95SBruce Richardson 		return RTE_HYPERVISOR_NONE;
2599a2dd95SBruce Richardson 
26*2454b37bSTyler Retzlaff #ifdef RTE_TOOLCHAIN_MSVC
27*2454b37bSTyler Retzlaff 	__cpuid(regs, HYPERVISOR_INFO_LEAF);
28*2454b37bSTyler Retzlaff #else
2999a2dd95SBruce Richardson 	__cpuid(HYPERVISOR_INFO_LEAF,
3099a2dd95SBruce Richardson 			regs[RTE_REG_EAX], regs[RTE_REG_EBX],
3199a2dd95SBruce Richardson 			regs[RTE_REG_ECX], regs[RTE_REG_EDX]);
32*2454b37bSTyler Retzlaff #endif
3399a2dd95SBruce Richardson 	for (reg = 1; reg < 4; reg++)
3499a2dd95SBruce Richardson 		memcpy(name + (reg - 1) * 4, &regs[reg], 4);
3599a2dd95SBruce Richardson 	name[12] = '\0';
3699a2dd95SBruce Richardson 
3799a2dd95SBruce Richardson 	if (strcmp("KVMKVMKVM", name) == 0)
3899a2dd95SBruce Richardson 		return RTE_HYPERVISOR_KVM;
3999a2dd95SBruce Richardson 	if (strcmp("Microsoft Hv", name) == 0)
4099a2dd95SBruce Richardson 		return RTE_HYPERVISOR_HYPERV;
4199a2dd95SBruce Richardson 	if (strcmp("VMwareVMware", name) == 0)
4299a2dd95SBruce Richardson 		return RTE_HYPERVISOR_VMWARE;
4399a2dd95SBruce Richardson 	return RTE_HYPERVISOR_UNKNOWN;
4499a2dd95SBruce Richardson }
45