From 8344a5a63cc795e3654ae10bdb7bbe7cc4a8c67c Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Tue, 20 Mar 2018 12:26:44 -0400 Subject: [PATCH] pc: Parse CPU features only once Considering that features are converted to global properties and global properties are automatically applied to every new instance of created CPU (at object_new() time), there is no point in parsing cpu_model string every time a CPU created. So move parsing outside CPU creation loop and do it only once. Parsing also should be done before any CPU is created so that features would affect the first CPU a well. Backports commit 6aff24c6a61c6fec31e555c7748ba6085b7b2c06 from qemu --- qemu/hw/i386/pc.c | 29 +++++++++++++++++++++------- qemu/qom/cpu.c | 2 -- qemu/target/i386/cpu.c | 44 ------------------------------------------ qemu/target/i386/cpu.h | 1 - 4 files changed, 22 insertions(+), 54 deletions(-) diff --git a/qemu/hw/i386/pc.c b/qemu/hw/i386/pc.c index 12a35d40..e901356b 100644 --- a/qemu/hw/i386/pc.c +++ b/qemu/hw/i386/pc.c @@ -97,17 +97,13 @@ DeviceState *cpu_get_current_apic(struct uc_struct *uc) } } -static X86CPU *pc_new_cpu(struct uc_struct *uc, const char *cpu_model, int64_t apic_id, +static X86CPU *pc_new_cpu(struct uc_struct *uc, const char *typename, int64_t apic_id, Error **errp) { X86CPU *cpu; Error *local_err = NULL; - cpu = cpu_x86_create(uc, cpu_model, &local_err); - if (local_err != NULL) { - error_propagate(errp, local_err); - return NULL; - } + cpu = X86_CPU(uc, object_new(uc, typename)); object_property_set_int(uc, OBJECT(cpu), apic_id, "apic-id", &local_err); object_property_set_bool(uc, OBJECT(cpu), true, "realized", &local_err); @@ -123,6 +119,10 @@ static X86CPU *pc_new_cpu(struct uc_struct *uc, const char *cpu_model, int64_t a int pc_cpus_init(struct uc_struct *uc, const char *cpu_model) { int i; + CPUClass *cc; + ObjectClass *oc; + const char *typename; + gchar **model_pieces; Error *error = NULL; /* init CPUs */ @@ -134,8 +134,23 @@ int pc_cpus_init(struct uc_struct *uc, const char *cpu_model) #endif } + model_pieces = g_strsplit(machine->cpu_model, ",", 2); + if (!model_pieces[0]) { + fprintf(stderr, "Invalid/empty CPU model name"); + return -1; + } + + oc = cpu_class_by_name(uc, TYPE_X86_CPU, model_pieces[0]); + if (oc == NULL) { + fprintf(stderr, "Unable to find CPU definition: %s", model_pieces[0]); + return -1; + } + typename = object_class_get_name(oc); + cc = CPU_CLASS(uc, oc); + cc->parse_features(uc, typename, model_pieces[1], &error_fatal); + g_strfreev(model_pieces); for (i = 0; i < smp_cpus; i++) { - uc->cpu = (CPUState *)pc_new_cpu(uc, cpu_model, x86_cpu_apic_id_from_index(i), &error); + uc->cpu = (CPUState *)pc_new_cpu(uc, typename, x86_cpu_apic_id_from_index(i), &error); if (error) { //error_report("%s", error_get_pretty(error)); error_free(error); diff --git a/qemu/qom/cpu.c b/qemu/qom/cpu.c index b9f4b395..7211f699 100644 --- a/qemu/qom/cpu.c +++ b/qemu/qom/cpu.c @@ -230,9 +230,7 @@ static void cpu_common_parse_features(struct uc_struct *uc, const char *typename * call it only once, so we can remove this check (or change it * to assert(!cpu_globals_initialized). * Current callers of ->parse_features() are: - * - machvirt_init() * - cpu_generic_init() - * - cpu_x86_create() */ if (uc->cpu_globals_initialized) { return; diff --git a/qemu/target/i386/cpu.c b/qemu/target/i386/cpu.c index d56f20d8..71f59bee 100644 --- a/qemu/target/i386/cpu.c +++ b/qemu/target/i386/cpu.c @@ -3173,50 +3173,6 @@ static void x86_cpu_load_def(X86CPU *cpu, X86CPUDefinition *def, Error **errp) object_property_set_str(env->uc, OBJECT(cpu), vendor, "vendor", errp); } -X86CPU *cpu_x86_create(struct uc_struct *uc, const char *cpu_model, Error **errp) -{ - X86CPU *cpu = NULL; - ObjectClass *oc; - CPUClass *cc; - gchar **model_pieces; - char *name, *features; - Error *error = NULL; - const char *typename; - - model_pieces = g_strsplit(cpu_model, ",", 2); - if (!model_pieces[0]) { - error_setg(&error, "Invalid/empty CPU model name"); - goto out; - } - name = model_pieces[0]; - features = model_pieces[1]; - - oc = x86_cpu_class_by_name(uc, name); - if (oc == NULL) { - error_setg(&error, "Unable to find CPU definition: %s", name); - goto out; - } - cc = CPU_CLASS(uc, oc); - typename = object_class_get_name(oc); - - cc->parse_features(uc, typename, features, &error); - cpu = X86_CPU(uc, object_new(uc, typename)); - if (error) { - goto out; - } - -out: - if (error != NULL) { - error_propagate(errp, error); - if (cpu) { - object_unref(uc, OBJECT(cpu)); - cpu = NULL; - } - } - g_strfreev(model_pieces); - return cpu; -} - static void x86_cpu_cpudef_class_init(struct uc_struct *uc, ObjectClass *oc, void *data) { X86CPUDefinition *cpudef = data; diff --git a/qemu/target/i386/cpu.h b/qemu/target/i386/cpu.h index bfdcfb83..0fbfb194 100644 --- a/qemu/target/i386/cpu.h +++ b/qemu/target/i386/cpu.h @@ -1343,7 +1343,6 @@ int x86_cpu_gdb_write_register(CPUState *cpu, uint8_t *buf, int reg); void x86_cpu_exec_enter(CPUState *cpu); void x86_cpu_exec_exit(CPUState *cpu); -X86CPU *cpu_x86_create(struct uc_struct *uc, const char *cpu_model, Error **errp); int cpu_x86_support_mca_broadcast(CPUX86State *env); int cpu_get_pic_interrupt(CPUX86State *s);