Add -cpu host CPU type
cover Add -cpu host CPU type
Hi,
this patchset adds a new CPU type called "host" which mimics the host CPU in many ways. It's main usecase is to reveal most of the CPU features to KVM guests.
KVM provides in the kernel a feature to trim the guest's CPU bits to be not more capable than the host's one. This is currently broken in userspace (both qemu.git and qemu-kvm.git), so fix this on the way, since without it most guests will crash.
Additionally allow control over the hypervisor bit, which is somewhat special, because it's the only bit which is not present in the host, but should be set in the guest.
Patch 1-3 should apply easily to qemu-kvm.git, Patch 4 needs a little tweak. Patch 5 and 6 are entirely different, although it's broken there in a similar way. If you need the patchset against qemu-kvm.git, drop me a note.
Regards,
Andre.
PATCH 1/6 allow hypervisor CPUID bit to be overriden
KVM defaults to the hypervisor CPUID bit to be set, whereas pure QEMU clears it. On some occasions one wants to set or clear it the other way round (for instance to get HyperV running inside a guest).
Move the bit-set to be done before the command line parsing and enable it by default. One can disable it by using: -cpu qemu64,-hypervisor Fix some whitespace damage on the way.
--- target-i386/helper.c | 23 +++++++++++------------ 1 files changed, 11 insertions(+), 12 deletions(-)
target-i386/helper.c +11 -12
@@ -44,7 +44,7 @@ static const char *ext_feature_name[] = {"pni" /* Intel,AMD sse3 */ ,NULL ,NULL ,"monitor" ,"ds_cpl" ,"vmx" ,NULL /* Linux smx */ ,"est" ,"tm2" ,"ssse3" ,"cid" ,NULL ,NULL ,"cx16" ,"xtpr" ,NULL ,NULL ,NULL ,"dca" ,NULL ,NULL ,NULL ,NULL ,"popcnt" , - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, NULL, NULL, NULL, "hypervisor", };static const char *ext2_feature_name[] = { "fpu", "vme", "de", "pse", "tsc", "msr", "pae", "mce",
@@ -59,30 +59,30 @@ static const char *ext3_feature_name[] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, }; -static void add_flagname_to_bitmaps (char *flagname,uint32_t *features, -uint32_t *ext_features, -uint32_t *ext2_features, +static void add_flagname_to_bitmaps (const char *flagname,uint32_t *features, +uint32_t *ext_features, +uint32_t *ext2_features,uint32_t *ext3_features) {int i;int found =0 ; -for ( i =0 ; i <32 ; i++ ) +for ( i =0 ; i <32 ; i++ )if (feature_name[i] && !strcmp (flagname, feature_name[i])) { *features |=1 << i; found =1 ; } -for ( i =0 ; i <32 ; i++ ) +for ( i =0 ; i <32 ; i++ )if (ext_feature_name[i] && !strcmp (flagname, ext_feature_name[i])) { *ext_features |=1 << i; found =1 ; } -for ( i =0 ; i <32 ; i++ ) +for ( i =0 ; i <32 ; i++ )if (ext2_feature_name[i] && !strcmp (flagname, ext2_feature_name[i])) { *ext2_features |=1 << i; found =1 ; } -for ( i =0 ; i <32 ; i++ ) +for ( i =0 ; i <32 ; i++ )if (ext3_feature_name[i] && !strcmp (flagname, ext3_feature_name[i])) { *ext3_features |=1 << i; found =1 ;
@@ -330,6 +330,9 @@ static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model) goto error;memcpy (x86_cpu_def, def,sizeof (*def)); +add_flagname_to_bitmaps ("hypervisor" , &plus_features, + &plus_ext_features, &plus_ext2_features, &plus_ext3_features); + featurestr =strtok (NULL ,"," );while (featurestr) {
@@ -1523,10 +1526,6 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *ebx = (env->cpuid_apic_id <<24 ) |8 <<8 ;/* CLFLUSH size in quad words, Linux wants it. */ *ecx = env->cpuid_ext_features; *edx = env->cpuid_features; - -/* "Hypervisor present" bit required for Microsoft SVVP */ -if (kvm_enabled ()) - *ecx |= (1 <<31 );break ;case 2 :/* cache info: needed for Pentium Pro compatibility */
Signature · 1 line
1.6.1.3
PATCH 5/6 remove CPUID host hacks
In reply to Andre PrzywaraKVM provides an in-kernel feature to disable CPUID bits that are not present in the current host. So there is no need here to duplicate this work. Additionally allows 3DNow! on capable processors, since the restriction seems to apply to QEMU/TCG only.
--- target-i386/helper.c | 27 +++++---------------------- 1 files changed, 5 insertions(+), 22 deletions(-)
target-i386/helper.c +5 -22
@@ -1643,28 +1643,11 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *edx = env->cpuid_ext2_features;if (kvm_enabled ()) { -uint32_t h_eax, h_edx; - -host_cpuid (index,0 , &h_eax,NULL ,NULL , &h_edx); - -/* disable CPU features that the host does not support */ - -/* long mode */ -if ((h_edx &0x20000000 ) ==0 /* || !lm_capable_kernel */ ) - *edx &= ~0x20000000 ; -/* syscall */ -if ((h_edx &0x00000800 ) ==0 ) - *edx &= ~0x00000800 ; -/* nx */ -if ((h_edx &0x00100000 ) ==0 ) - *edx &= ~0x00100000 ; - -/* disable CPU features that KVM cannot support */ - -/* svm */ - *ecx &= ~4UL ; -/* 3dnow */ - *edx &= ~0xc0000000 ; +/* Nested SVM not yet supported in KVM */ + *ecx &= ~CPUID_EXT3_SVM ; + }else { +/* AMD 3DNow! is not supported in QEMU */ + *edx &= ~(CPUID_EXT2_3DNOW |CPUID_EXT2_3DNOWEXT ); }break ;case 0x80000002 :
Signature · 1 line
1.6.1.3
PATCH 3/6 CPUID Fn8000_0001.EAX is family/model/stepping, not features
In reply to Andre Przywara--- target-i386/helper.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
target-i386/helper.c +1 -1
@@ -1651,7 +1651,7 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *ecx = env->cpuid_vendor3;break ;case 0x80000001 : - *eax = env->cpuid_features; + *eax = env->cpuid_version; *ebx =0 ; *ecx = env->cpuid_ext3_features; *edx = env->cpuid_ext2_features;
Signature · 1 line
1.6.1.3
PATCH 4/6 fix KVMs GET_SUPPORTED_CPUID feature usage
In reply to Andre PrzywaraIf we want to trim the user provided CPUID bits for KVM to be not greater than that of the host, we should not remove the bits _after_ we sent them to the kernel.
This fixes the masking of features that are not present on the host by moving the trim function and it's call from helper.c to kvm.c. It helps to use -cpu host.
--- target-i386/helper.c | 29 ----------------------------- target-i386/kvm.c | 22 ++++++++++++++++++++++ 2 files changed, 22 insertions(+), 29 deletions(-)
target-i386/helper.c +0 -29
@@ -92,20 +92,6 @@ static void add_flagname_to_bitmaps(const char *flagname, uint32_t *features, } } -static void kvm_trim_features (uint32_t *features,uint32_t supported, -const char *names[]) -{ -int i; -uint32_t mask; - -for (i =0 ; i <32 ; ++i) { - mask =1U << i; -if ((*features & mask) && !(supported & mask)) { - *features &= ~mask; - } - } -} - typedef struct x86_def_t { const char *name; uint32_t level;
@@ -1773,21 +1759,6 @@ CPUX86State *cpu_x86_init(const char *cpu_model)qemu_init_vcpu (env); -if (kvm_enabled ()) { -kvm_trim_features (&env->cpuid_features, -kvm_arch_get_supported_cpuid (env,1 ,R_EDX ), - feature_name); -kvm_trim_features (&env->cpuid_ext_features, -kvm_arch_get_supported_cpuid (env,1 ,R_ECX ), - ext_feature_name); -kvm_trim_features (&env->cpuid_ext2_features, -kvm_arch_get_supported_cpuid (env,0x80000001 ,R_EDX ), - ext2_feature_name); -kvm_trim_features (&env->cpuid_ext3_features, -kvm_arch_get_supported_cpuid (env,0x80000001 ,R_ECX ), - ext3_feature_name); - } -return env; }
target-i386/kvm.c +22 -0
@@ -117,6 +117,19 @@ uint32_t kvm_arch_get_supported_cpuid(CPUState *env, uint32_t function, int reg)#endif +static void kvm_trim_features (uint32_t *features,uint32_t supported) +{ +int i; +uint32_t mask; + +for (i =0 ; i <32 ; ++i) { + mask =1U << i; +if ((*features & mask) && !(supported & mask)) { + *features &= ~mask; + } + } +} +int kvm_arch_init_vcpu (CPUState *env) {struct {
@@ -128,6 +141,15 @@ int kvm_arch_init_vcpu(CPUState *env) env->mp_state =KVM_MP_STATE_RUNNABLE ; +kvm_trim_features (&env->cpuid_features, +kvm_arch_get_supported_cpuid (env,1 ,R_EDX )); +kvm_trim_features (&env->cpuid_ext_features, +kvm_arch_get_supported_cpuid (env,1 ,R_ECX )); +kvm_trim_features (&env->cpuid_ext2_features, +kvm_arch_get_supported_cpuid (env,0x80000001 ,R_EDX )); +kvm_trim_features (&env->cpuid_ext3_features, +kvm_arch_get_supported_cpuid (env,0x80000001 ,R_ECX )); + cpuid_i =0 ;cpu_x86_cpuid (env,0 ,0 , &limit, &unused, &unused, &unused);
Signature · 1 line
1.6.1.3
PATCH 6/6 preserve the hypervisor bit while KVM trims the CPUID bits
In reply to Andre PrzywaraThe KVM kernel will disable all bits in CPUID which are not present in the host. As this is mostly true for the hypervisor bit (1.ecx), preserve its value before the trim and restore it afterwards.
--- target-i386/cpu.h | 1 + target-i386/kvm.c | 4 ++++ 2 files changed, 5 insertions(+), 0 deletions(-)
target-i386/cpu.h +1 -0
@@ -362,6 +362,7 @@#define CPUID_EXT_POPCNT (1 << 23)#define CPUID_EXT_XSAVE (1 << 26)#define CPUID_EXT_OSXSAVE (1 << 27) +#define CPUID_EXT_HYPERVISOR (1 << 31)#define CPUID_EXT2_SYSCALL (1 << 11)#define CPUID_EXT2_MP (1 << 19)
target-i386/kvm.c +4 -0
@@ -143,8 +143,12 @@ int kvm_arch_init_vcpu(CPUState *env)kvm_trim_features (&env->cpuid_features,kvm_arch_get_supported_cpuid (env,1 ,R_EDX )); + + i = env->cpuid_ext_features &CPUID_EXT_HYPERVISOR ;kvm_trim_features (&env->cpuid_ext_features,kvm_arch_get_supported_cpuid (env,1 ,R_ECX )); + env->cpuid_ext_features |= i; +kvm_trim_features (&env->cpuid_ext2_features,kvm_arch_get_supported_cpuid (env,0x80000001 ,R_EDX )); kvm_trim_features(&env->cpuid_ext3_features,
Signature · 1 line
1.6.1.3
PATCH 2/6 introduce -cpu host target
In reply to Andre PrzywaraAlthough the guest's CPUID bits can be controlled in a fine grained way in QEMU, a simple way to inject the host CPU is missing. This is handy for KVM desktop virtualization, where one wants the guest to support the full host feature set.
Introduce another CPU type called 'host', which will propagate the host's CPUID bits to the guest. Unwanted bits can still be turned off by using the existing syntax (-cpu host,-skinit)
--- target-i386/helper.c | 65 +++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 59 insertions(+), 6 deletions(-)
target-i386/helper.c +59 -6
@@ -273,9 +273,9 @@ static x86_def_t x86_defs[] = { { .name = "athlon", .level = 2, - .vendor1 = 0x68747541, /* "Auth" */ - .vendor2 = 0x69746e65, /* "enti" */ - .vendor3 = 0x444d4163, /* "cAMD" */ + .vendor1 = CPUID_VENDOR_AMD_1, + .vendor2 = CPUID_VENDOR_AMD_2, + .vendor3 = CPUID_VENDOR_AMD_3, .family = 6, .model = 2, .stepping = 3,
@@ -308,6 +308,54 @@ static x86_def_t x86_defs[] = { }, }; +static void host_cpuid (uint32_t function,uint32_t count,uint32_t *eax, +uint32_t *ebx,uint32_t *ecx,uint32_t *edx); + +static int cpu_x86_fill_model_id (char *str) +{ +uint32_t eax, ebx, ecx, edx; +int i; + +for (i =0 ; i <3 ; i++) { +host_cpuid (0x80000002 + i,0 , &eax, &ebx, &ecx, &edx); +memcpy (str + i *16 +0 , &eax,4 ); +memcpy (str + i *16 +4 , &ebx,4 ); +memcpy (str + i *16 +8 , &ecx,4 ); +memcpy (str + i *16 +12 , &edx,4 ); + } +return 0 ; +} + +static int cpu_x86_fill_host (x86_def_t *x86_cpu_def) +{ +uint32_t eax, ebx, ecx, edx; + + x86_cpu_def->name ="host" ; +host_cpuid (0x0 ,0 , &eax, &ebx, &ecx, &edx); + x86_cpu_def->level = eax; + x86_cpu_def->vendor1 = ebx; + x86_cpu_def->vendor2 = edx; + x86_cpu_def->vendor3 = ecx; + +host_cpuid (0x1 ,0 , &eax, &ebx, &ecx, &edx); + x86_cpu_def->family = ((eax >>8 ) &0x0F ) + ((eax >>20 ) &0xFF ); + x86_cpu_def->model = ((eax >>4 ) &0x0F ) | ((eax &0xF0000 ) >>12 ); + x86_cpu_def->stepping = eax &0x0F ; + x86_cpu_def->ext_features = ecx; + x86_cpu_def->features = edx; + +host_cpuid (0x80000000 ,0 , &eax, &ebx, &ecx, &edx); + x86_cpu_def->xlevel = eax; + +host_cpuid (0x80000001 ,0 , &eax, &ebx, &ecx, &edx); + x86_cpu_def->ext2_features = edx; + x86_cpu_def->ext3_features = ecx; +cpu_x86_fill_model_id (x86_cpu_def->model_id); + x86_cpu_def->vendor_override =0 ; + +return 0 ; +} +static int cpu_x86_find_by_name (x86_def_t *x86_cpu_def,const char *cpu_model) {unsigned int i;
@@ -326,9 +374,14 @@ static int cpu_x86_find_by_name(x86_def_t *x86_cpu_def, const char *cpu_model)break ; } } -if (!def) - goto error; -memcpy (x86_cpu_def, def,sizeof (*def)); +if (!def) { +if (strcmp (name,"host" ) !=0 ) { + goto error; + } +cpu_x86_fill_host (x86_cpu_def); + }else { +memcpy (x86_cpu_def, def,sizeof (*def)); + }add_flagname_to_bitmaps ("hypervisor" , &plus_features, &plus_ext_features, &plus_ext2_features, &plus_ext3_features);
Signature · 1 line
1.6.1.3