From 555eeb412014cc9d39169a8e644e8cb9486f370e Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Tue, 20 Mar 2018 13:08:07 -0400 Subject: [PATCH] qom: cpus: split cpu_generic_init() on feature parsing and cpu creation parts it would allow to reuse feature parsing part in various machines that have CPU features instead of re-implementing the same feature parsing each time. Backports commit 3c72234c98004a01d79a24f78b07053cfebd0f22 from qemu --- qemu/include/qom/cpu.h | 21 ++++++++++++++++++++ qemu/qom/cpu.c | 45 +++++++++++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/qemu/include/qom/cpu.h b/qemu/include/qom/cpu.h index 461832bf..339ffb40 100644 --- a/qemu/include/qom/cpu.h +++ b/qemu/include/qom/cpu.h @@ -527,6 +527,27 @@ void cpu_reset(CPUState *cpu); */ ObjectClass *cpu_class_by_name(struct uc_struct *uc, const char *typename, const char *cpu_model); +/** + * cpu_create: + * @typename: The CPU type. + * + * Instantiates a CPU and realizes the CPU. + * + * Returns: A #CPUState or %NULL if an error occurred. + */ +CPUState *cpu_create(struct uc_struct *uc, const char *typename); + +/** + * cpu_parse_cpu_model: + * @typename: The CPU base type or CPU type. + * @cpu_model: The model string including optional parameters. + * + * processes optional parameters and registers them as global properties + * + * Returns: type of CPU to create or %NULL if an error occurred. + */ +const char *cpu_parse_cpu_model(struct uc_struct *uc, const char *typename, const char *cpu_model); + /** * cpu_generic_init: * @typename: The CPU base type. diff --git a/qemu/qom/cpu.c b/qemu/qom/cpu.c index ef6c228d..0946f5ce 100644 --- a/qemu/qom/cpu.c +++ b/qemu/qom/cpu.c @@ -41,13 +41,25 @@ bool cpu_exists(struct uc_struct *uc, int64_t id) return !!cpu_by_arch_id(uc, id); } -CPUState *cpu_generic_init(struct uc_struct *uc, const char *typename, const char *cpu_model) +CPUState *cpu_create(struct uc_struct *uc, const char *typename) +{ + Error *err = NULL; + CPUState *cpu = CPU(object_new(uc, typename)); + object_property_set_bool(uc, OBJECT(cpu), true, "realized", &err); + if (err != NULL) { + object_unref(uc, OBJECT(cpu)); + return NULL; + } + return cpu; +} + +const char *cpu_parse_cpu_model(struct uc_struct *uc, const char *typename, const char *cpu_model) { - CPUState *cpu = NULL; ObjectClass *oc; CPUClass *cc; Error *err = NULL; gchar **model_pieces; + const char *cpu_type; model_pieces = g_strsplit(cpu_model, ",", 2); @@ -57,27 +69,28 @@ CPUState *cpu_generic_init(struct uc_struct *uc, const char *typename, const cha return NULL; } + cpu_type = object_class_get_name(oc); cc = CPU_CLASS(uc, oc); - /* TODO: all callers of cpu_generic_init() need to be converted to - * call parse_features() only once, before calling cpu_generic_init(). - */ - cc->parse_features(uc, object_class_get_name(oc), model_pieces[1], &err); + cc->parse_features(uc, cpu_type, model_pieces[1], &err); g_strfreev(model_pieces); - if (err != NULL) { - goto out; - } - - cpu = CPU(object_new(uc, object_class_get_name(oc))); - object_property_set_bool(uc, OBJECT(cpu), true, "realized", &err); - -out: if (err != NULL) { error_free(err); - object_unref(uc, OBJECT(cpu)); return NULL; } + return cpu_type; +} - return cpu; +CPUState *cpu_generic_init(struct uc_struct *uc, const char *typename, const char *cpu_model) +{ + /* TODO: all callers of cpu_generic_init() need to be converted to + * call cpu_parse_features() only once, before calling cpu_generic_init(). + */ + const char *cpu_type = cpu_parse_cpu_model(uc, typename, cpu_model); + + if (cpu_type) { + return cpu_create(uc, cpu_type); + } + return NULL; } bool cpu_paging_enabled(const CPUState *cpu)