From: Igor Mammedov Subject: Re: [Qemu-devel] [PATCH qom-cpu-next v3 3/4] target-i386: Slim conversion to X86CPU subclasses Date: Mon, 4 Feb 2013 17:05:01 +0100 Message-ID: <20130204170501.20ddb78f@nial.usersys.redhat.com> References: <1359765428-27805-1-git-send-email-afaerber@suse.de> <1359765428-27805-4-git-send-email-afaerber@suse.de> <20130204120856.571533d5@nial.usersys.redhat.com> <510FAF10.3050505@suse.de> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: qemu-devel@nongnu.org, ehabkost@redhat.com, Gleb Natapov , Marcelo Tosatti , blauwirbel@gmail.com, anthony@codemonkey.ws, X86 To: Andreas =?ISO-8859-1?B?RuRyYmVy?= Return-path: Received: from mx1.redhat.com ([209.132.183.28]:48664 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754177Ab3BDQF6 convert rfc822-to-8bit (ORCPT ); Mon, 4 Feb 2013 11:05:58 -0500 In-Reply-To: <510FAF10.3050505@suse.de> Sender: kvm-owner@vger.kernel.org List-ID: On Mon, 04 Feb 2013 13:52:32 +0100 Andreas F=E4rber wrote: > Am 04.02.2013 12:08, schrieb Igor Mammedov: > > On Sat, 2 Feb 2013 01:37:07 +0100 > > Andreas F=E4rber wrote: > >=20 > >> Move x86_def_t definition to header and embed into X86CPUClass. > >> Register types per built-in model definition. > >> > >> Move version initialization from x86_cpudef_setup() to class_init. > >> > >> Inline cpu_x86_register() into the X86CPU initfn. > >> Since instance_init cannot reports errors, drop error handling. > >> > >> Replace cpu_x86_find_by_name() with x86_cpu_class_by_name(). > >> Move KVM host vendor override from cpu_x86_find_by_name() to the i= nitfn. > >> > >> Register host-{i386,x86_64}-cpu type from KVM code to avoid #ifdef= s. > >> Make kvm_cpu_fill_host() a class_init and inline cpu_x86_fill_mode= l_id(). > >> > >> Let kvm_check_features_against_host() obtain host-{i386,86_64}-cpu= for > >> comparison. > >> > >> Signed-off-by: Andreas F=E4rber > >> --- > >> target-i386/cpu-qom.h | 24 ++++ > >> target-i386/cpu.c | 324 > >> +++++++++++++++++-------------------------------- target-i386/cpu.= h > >> | 2 - target-i386/kvm.c | 93 ++++++++++++++ > >> 4 Dateien ge=E4ndert, 228 Zeilen hinzugef=FCgt(+), 215 Zeilen ent= fernt(-) > >> > >> diff --git a/target-i386/cpu-qom.h b/target-i386/cpu-qom.h > >> index 48e6b54..80bf72d 100644 > >> --- a/target-i386/cpu-qom.h > >> +++ b/target-i386/cpu-qom.h [...] > >> * An x86 CPU model or family. > >> */ > >> @@ -51,6 +73,8 @@ typedef struct X86CPUClass { > >> =20 > >> DeviceRealize parent_realize; > >> void (*parent_reset)(CPUState *cpu); > >> + > >> + x86_def_t info; > > Is it necessary to embed it here, could pointer to corresponding st= atic > > array be used here? > > That way one could avoid extra memcpy in class_init(). >=20 > That would require an allocation for -cpu host, which is undesired. not necessary, it could be local static x86_def_t in target-i386/kvm.c = in host class_init(). Any way change is cosmetic and I do not insist on it= =2E > x86_def_t is supposed to die, forgot to add a TODO comment like on pp= c. > Patches to do that are being being polished on qom-cpu-ppc. +1 >=20 > >> } X86CPUClass; > >> =20 > >> /** [...] > >> *name) +static ObjectClass *x86_cpu_class_by_name(const char *name= ) > >> { > >> - x86_def_t *def; > >> - int i; > >> + ObjectClass *oc; > >> + char *typename; > >> =20 > >> if (name =3D=3D NULL) { > >> - return -1; > >> - } > >> - if (kvm_enabled() && strcmp(name, "host") =3D=3D 0) { > >> - kvm_cpu_fill_host(x86_cpu_def); > >> - return 0; > >> + return NULL; > >> } > >> =20 > >> - for (i =3D 0; i < ARRAY_SIZE(builtin_x86_defs); i++) { > >> - def =3D &builtin_x86_defs[i]; > >> - if (strcmp(name, def->name) =3D=3D 0) { > >> - memcpy(x86_cpu_def, def, sizeof(*def)); > >> - /* sysenter isn't supported in compatibility mode on = AMD, > >> - * syscall isn't supported in compatibility mode on I= ntel. > >> - * Normally we advertise the actual CPU vendor, but y= ou can > >> - * override this using the 'vendor' property if you w= ant to > >> use > >> - * KVM's sysenter/syscall emulation in compatibility = mode > >> and > >> - * when doing cross vendor migration > >> - */ > >> - if (kvm_enabled()) { > >> - uint32_t ebx =3D 0, ecx =3D 0, edx =3D 0; > >> - host_cpuid(0, 0, NULL, &ebx, &ecx, &edx); > >> - x86_cpu_vendor_words2str(x86_cpu_def->vendor, ebx= , edx, > >> ecx); > >> - } > >> - return 0; > >> + if (strcmp(name, "host") =3D=3D 0) { > >> + if (kvm_enabled()) { > >> + return object_class_by_name(TYPE_HOST_X86_CPU); > >> } > >> + return NULL; > >> } > > block is not necessary, the following block could yield the same re= sult. >=20 > No, it doesn't in the !kvm_enabled() case. I tripped over this when I > left the kvm_enabled() check where it was before, falling back to the > below lookup, leading to -cpu host being instantiated for TCG and > originally not asserting (that's why I added the -cpu host initfn). Maybe TYPE_HOST_X86_CPU shouldn't be registered at all if kvm_enabled()= =3D=3D 0. >=20 > Symptoms were x86_64 openSUSE .iso claiming no support for 486(?) > processors. >=20 > >> =20 > >> - return -1; > >> + typename =3D g_strdup_printf("%s-" TYPE_X86_CPU, name); > >> + oc =3D object_class_by_name(typename); > >> + g_free(typename); > >> + if (oc !=3D NULL && (!object_class_dynamic_cast(oc, TYPE_X86_= CPU) || > >> + object_class_is_abstract(oc))) { > >> + oc =3D NULL; > >> + } > >> + return oc; > >> } > >> =20 [...] > >> @@ -1580,13 +1424,13 @@ X86CPU *cpu_x86_init(const char *cpu_model= ) > >> name =3D model_pieces[0]; > >> features =3D model_pieces[1]; > >> =20 > >> - cpu =3D X86_CPU(object_new(TYPE_X86_CPU)); > >> - env =3D &cpu->env; > >> - env->cpu_model_str =3D cpu_model; > >> - > >> - if (cpu_x86_register(cpu, name) < 0) { > >> + oc =3D x86_cpu_class_by_name(name); > >> + if (oc =3D=3D NULL) { > > make sure error is reported when cpu is not found: > > error_setg(&error, "Can't find CPU: %s", name); >=20 > Why? The callers do that when NULL is returned. The callers do not exactly know why NULL is returned, wouldn't it bette= r to specify error message at the point of origin. >=20 > >> goto error; > >> } > >> + cpu =3D X86_CPU(object_new(object_class_get_name(oc))); > >> + env =3D &cpu->env; > >> + env->cpu_model_str =3D cpu_model; > >> =20 > >> cpu_x86_parse_featurestr(cpu, features, &error); > >> if (error) { [...] > >> @@ -2198,6 +2018,8 @@ static void x86_cpu_initfn(Object *obj) > >> CPUState *cs =3D CPU(obj); > >> X86CPU *cpu =3D X86_CPU(obj); > >> CPUX86State *env =3D &cpu->env; > >> + X86CPUClass *xcc =3D X86_CPU_GET_CLASS(obj); > >> + const x86_def_t *def =3D &xcc->info; > >> static int inited; > >> =20 > >> cpu_exec_init(env); > >> @@ -2227,6 +2049,41 @@ static void x86_cpu_initfn(Object *obj) > >> x86_cpuid_get_tsc_freq, > >> x86_cpuid_set_tsc_freq, NULL, NULL, NULL)= ; > >> =20 > >> + /* sysenter isn't supported in compatibility mode on AMD, > >> + * syscall isn't supported in compatibility mode on Intel. > >> + * Normally we advertise the actual CPU vendor, but you can > >> + * override this using the 'vendor' property if you want to u= se > >> + * KVM's sysenter/syscall emulation in compatibility mode and > >> + * when doing cross vendor migration > >> + */ > >> + if (kvm_enabled()) { > >> + host_cpuid(0, 0, NULL, &env->cpuid_vendor1, &env->cpuid_v= endor2, > >> + &env->cpuid_vendor3); > > This is not per instance specific, this override would be better do= ne to > > sub-classes in kvm_arch_init(). >=20 > Hmm... I think the issue I addresses this way was that kvm.c didn't h= ave > access to your static vendor_words2str() helper. Writing the words in= to > the word fields is more direct than converting them to a string and > writing them back into the words. you have these correct vendor words ready after you called kvm_host_cpu_class_init() in kvm_arch_init(), so just direct copy from = host class would do. >=20 > > As bonus we would get actual 'vendor' when > > doing class introspection provided it's done after kvm_init() is ca= lled. >=20 > Doing that would mean that we force class_init of every CPU class to = run > every time we start with KVM enabled. I'd ask if there is any particular downside in calling every class_init= ()? > Is there any practical downside to > doing it at instance_init time? Properties are set afterwards, so it > doesn't override anything. Where would we want to inspect an X86CPUCl= ass > to have an AMD model say Intel or vice versa? -cpu ? doesn't look at = it > today, nor does QMP. Presently nothing changes if we do it in kvm_init() or initfn() time si= nce none of users displays actual vendor value, but it looks more like a bu= g.=20 Downside of doing it initfn() is that static properties defaults and gl= obal properties are set by device_initfn() before cpu_initfn() is called, so it renders all that was set via it overridden by a later x86_cpu_initfn= (). So if there is no really big drawback in calling every x86 cpu class_in= it(), I'd prefer to utilize features provided by static properties, than hack= ing class wide vendor override per each cpu instance. As alternative x86 cpu could have a hook that is called from x86_cpu_class_init() that does kvm specific overrides and set by kvm_arch_init(). Then there is no need to initialize all types in kvm_arch_init(). The problem with it is if class is initialized before kvm_init(), then override won't be applied ever. >=20 > >> + } else { > >> + object_property_set_str(OBJECT(cpu), def->vendor, "vendor= ", > >> NULL); > >> + } > >> + > >> + object_property_set_int(OBJECT(cpu), def->level, "level", NUL= L); > >> + object_property_set_int(OBJECT(cpu), def->family, "family", N= ULL); > >> + object_property_set_int(OBJECT(cpu), def->model, "model", NUL= L); > >> + object_property_set_int(OBJECT(cpu), def->stepping, "stepping= ", > >> NULL); > >> + env->cpuid_features =3D def->features; > >> + env->cpuid_ext_features =3D def->ext_features; > >> + env->cpuid_ext_features |=3D CPUID_EXT_HYPERVISOR; > >> + env->cpuid_ext2_features =3D def->ext2_features; > >> + env->cpuid_ext3_features =3D def->ext3_features; > >> + object_property_set_int(OBJECT(cpu), def->xlevel, "xlevel", N= ULL); > >> + env->cpuid_kvm_features =3D def->kvm_features; > >> + if (kvm_enabled()) { > >> + env->cpuid_kvm_features |=3D kvm_default_features; > >> + } > >> + env->cpuid_svm_features =3D def->svm_features; > >> + env->cpuid_ext4_features =3D def->ext4_features; > >> + env->cpuid_7_0_ebx_features =3D def->cpuid_7_0_ebx_features; > >> + env->cpuid_xlevel2 =3D def->xlevel2; > >> + > >> + object_property_set_str(OBJECT(cpu), def->model_id, "model-id= ", > >> NULL); > > All this feature initialization in initfn() is not compatible with > > static/global properties because they are set in device_initfn(). B= ut I > > can remove properties/features from here as they are converted to s= tatic > > properties and incrementally move their defaults initialization int= o > > new x86_cpu_def_class_init(). >=20 > Would it help to call the old registration function from here? Or wha= t > exactly would you need on top? Nope, see previous answer to why setting defaults here is not desired. After conversion, defaults will be set via static properties defaults a= nd then on top of it, global properties will be applied in device_initfn(). Eventually -cpu xxx,foo should set global properties for cpu type and c= pu type name for machine to use. no manual property setting between object= _new() and realize() unless it is instance specific. [...] > >> diff --git a/target-i386/kvm.c b/target-i386/kvm.c > >> index 9ebf181..dbfa670 100644 > >> --- a/target-i386/kvm.c > >> +++ b/target-i386/kvm.c > >> @@ -735,6 +735,80 @@ static int kvm_get_supported_msrs(KVMState *s= ) > >> return ret; > >> } > >> =20 > >> +static void kvm_host_cpu_initfn(Object *obj) > >> +{ > >> + assert(kvm_enabled()); > >> +} > >> + > >> +static bool kvm_host_cpu_needs_class_init; > >> + > >> +static void kvm_host_cpu_class_init(ObjectClass *oc, void *data) > >> +{ > >> + X86CPUClass *xcc =3D X86_CPU_CLASS(oc); > >> + x86_def_t *x86_cpu_def =3D &xcc->info; > >> + KVMState *s =3D kvm_state; > >> + uint32_t eax =3D 0, ebx =3D 0, ecx =3D 0, edx =3D 0; > >> + int i; > >> + > >> + if (!kvm_enabled()) { > >> + kvm_host_cpu_needs_class_init =3D true; > >> + return; > >> + } > >> + > >> + xcc->info.name =3D "host"; > >> + > >> + /* Vendor will be set in initfn if kvm_enabled() */ > >> + > >> + host_cpuid(0x1, 0, &eax, &ebx, &ecx, &edx); > >> + x86_cpu_def->family =3D ((eax >> 8) & 0x0F) + ((eax >> 20) & = 0xFF); > >> + x86_cpu_def->model =3D ((eax >> 4) & 0x0F) | ((eax & 0xF0000)= >> 12); > >> + x86_cpu_def->stepping =3D eax & 0x0F; > >> + > >> + x86_cpu_def->level =3D kvm_arch_get_supported_cpuid(s, 0x0, 0= , R_EAX); > >> + x86_cpu_def->features =3D kvm_arch_get_supported_cpuid(s, 0x1= , 0, > >> R_EDX); > >> + x86_cpu_def->ext_features =3D kvm_arch_get_supported_cpuid(s,= 0x1, 0, > >> R_ECX); + > >> + if (x86_cpu_def->level >=3D 7) { > >> + x86_cpu_def->cpuid_7_0_ebx_features =3D > >> + kvm_arch_get_supported_cpuid(s, 0x7, 0, R_EBX= ); > >> + } else { > >> + x86_cpu_def->cpuid_7_0_ebx_features =3D 0; > >> + } > >> + > >> + x86_cpu_def->xlevel =3D kvm_arch_get_supported_cpuid(s, 0x800= 00000, 0, > >> R_EAX); > >> + x86_cpu_def->ext2_features =3D > >> + kvm_arch_get_supported_cpuid(s, 0x80000001, 0, R_= EDX); > >> + x86_cpu_def->ext3_features =3D > >> + kvm_arch_get_supported_cpuid(s, 0x80000001, 0, R_= ECX); > >> + > >> + for (i =3D 0; i < 3; i++) { > >> + host_cpuid(0x80000002 + i, 0, &eax, &ebx, &ecx, &edx); > >> + memcpy(xcc->info.model_id + i * 16 + 0, &eax, 4); > >> + memcpy(xcc->info.model_id + i * 16 + 4, &ebx, 4); > >> + memcpy(xcc->info.model_id + i * 16 + 8, &ecx, 4); > >> + memcpy(xcc->info.model_id + i * 16 + 12, &edx, 4); > >> + } > >> + > >> + /* Call Centaur's CPUID instruction. */ > >> + if (!strcmp(x86_cpu_def->vendor, CPUID_VENDOR_VIA)) { > >> + host_cpuid(0xC0000000, 0, &eax, &ebx, &ecx, &edx); > >> + eax =3D kvm_arch_get_supported_cpuid(s, 0xC0000000, 0, R_= EAX); > >> + if (eax >=3D 0xC0000001) { > >> + /* Support VIA max extended level */ > >> + x86_cpu_def->xlevel2 =3D eax; > >> + host_cpuid(0xC0000001, 0, &eax, &ebx, &ecx, &edx); > >> + x86_cpu_def->ext4_features =3D > >> + kvm_arch_get_supported_cpuid(s, 0xC0000001, 0= , > >> R_EDX); > >> + } > >> + } > >> + > >> + /* Other KVM-specific feature fields: */ > >> + x86_cpu_def->svm_features =3D > >> + kvm_arch_get_supported_cpuid(s, 0x8000000A, 0, R_EDX); > >> + x86_cpu_def->kvm_features =3D > >> + kvm_arch_get_supported_cpuid(s, KVM_CPUID_FEATURES, 0, R_= EAX); > >> +} > >> + > >> int kvm_arch_init(KVMState *s) > >> { > >> QemuOptsList *list =3D qemu_find_opts("machine"); > >> @@ -797,6 +871,11 @@ int kvm_arch_init(KVMState *s) > >> } > >> } > >> } > >> + > >> + if (kvm_host_cpu_needs_class_init) { > >> + kvm_host_cpu_class_init(object_class_by_name(TYPE_HOST_X8= 6_CPU), > >> NULL); > >> + } > > kvm_host_cpu_needs_class_init is set to true in kvm_host_cpu_class_= init() > > so block is nop. >=20 > Nope. It's a question of initialization ordering: Yep, I see now, type_register_static(&host_x86_cpu_type_info) creates fake(uninitialized) host class and then it is initialized when kvm_arch_init() is called. >=20 > > Why kvm_host_cpu_needs_class_init is needed anyway, why not just ca= ll > > kvm_host_cpu_class_init() here? >=20 > i) class_init may be called before kvm_enabled() evaluates to true, s= uch > as for -cpu ?, so we need to handle that case gracefully. We do not print host cpu type at -cpu ? now, it's implied that host cpu= is available if started with --enable-kvm. >=20 > ii) The regular case is that the class_init is run in response to > object_new() from pc.c, in which case kvm_enabled() evaluates to true > and the needs_class_init remains false. could we register host type in kvm_arch_init() time, and avoid calling kvm_host_cpu_class_init() twice? It also would allow to remove host look-up hack in x86_cpu_class_by_nam= e(). >=20 > Thus, both cases are used in practice. >=20 > Regards, > Andreas >=20 > >> + > >> return 0; > >> } > >> =20 > >> @@ -2330,3 +2409,17 @@ int kvm_device_msix_deassign(KVMState *s, u= int32_t > >> dev_id) return kvm_deassign_irq_internal(s, dev_id, > >> KVM_DEV_IRQ_GUEST_MSIX | KVM_DEV_IRQ_HOST_MSIX); > >> } > >> + > >> +static const TypeInfo host_x86_cpu_type_info =3D { > >> + .name =3D TYPE_HOST_X86_CPU, > >> + .parent =3D TYPE_X86_CPU, > >> + .instance_init =3D kvm_host_cpu_initfn, > >> + .class_init =3D kvm_host_cpu_class_init, > >> +}; > >> + > >> +static void kvm_register_types(void) > >> +{ > >> + type_register_static(&host_x86_cpu_type_info); > >> +} > >> + > >> +type_init(kvm_register_types) >=20