i386: Add `machine` parameter to query-cpu-definitions
v2 cover i386: Add `machine` parameter to query-cpu-definitions
Changes v1 -> v2:
* Rewrite documentation, with suggestions from Markus
* Try to reduce churn and keep the existing default_cpu_version static variable
* Replace x86_cpu_class_get_alias_of() with x86_cpu_model_resolve_alias()
Link to v1:
https://lore.kernel.org/qemu-devel/[email protected]
We had introduced versioned CPU models in QEMU 4.1, including a method for querying for CPU model versions using query-cpu-definitions. This solution had one problem: fetching CPU alias information for multiple machine types required restarting QEMU for each machine being queried.
This series adds a new machine parameter to query-cpu-definitions, that can be used to query CPU model alias information for multiple machines without restarting QEMU.
About CPU model versioning vs query-cpu-model-expansion
-------------------------------------------------------
There had been some discussion[1] about using a mechanism for this similar to what's done in s390x, based on query-cpu-model-expansion. I had considered that approach in the beginning, but this would require
query-cpu-model-expansion type=static
to resolve to a useful base CPU model that is static.
Unfortunately, we have no useful static CPU model in x86 yet, except for "base", because of the complex accelerator-dependent rules that affect expansion of CPU models. Maybe in the future we'll be able to define static versions of each CPU model, and unify the CPU model versioning approaches in all architectures.
While we don't have this, we need to work with what we have. What we have today in x86 are CPU models that are not static and not accelerator-independent.
[1] https://lore.kernel.org/qemu-devel/[email protected]/
Eduardo Habkost (7):
machine: machine_find_class() function
i386: Add X86CPUModel.alias_of field
i386: Replace x86_cpu_class_get_alias_of() with
x86_cpu_model_resolve_alias()
i386: Add default_version parameter to CPU version functions
i386: Wrap QMP code in !CONFIG_USER_ONLY
i386: Don't use default_cpu_version inside x86_cpu_definition_entry()
cpu: Add machine parameter to query-cpu-definitions
qapi/machine-target.json | 14 ++- include/hw/boards.h | 2 + hw/core/machine.c | 16 ++++ softmmu/vl.c | 17 +--- target/arm/helper.c | 4 +- target/i386/cpu.c | 105 +++++++++++++++------ target/mips/helper.c | 4 +- target/s390x/cpu_models.c | 4 +- target/ppc/translate_init.c.inc | 4 +- tests/acceptance/x86_cpu_model_versions.py | 48 ++++++++++ 10 files changed, 167 insertions(+), 51 deletions(-)
Signature · 1 line
2.28.0
PATCH v2 1/7 machine: machine_find_class() function
Move find_machine() from vl.c to core/machine.c and rename it to machine_find_class(), so it can be reused by other code.
The function won't reuse the results of the previous object_class_get_list() call like it did in vl.c, but this shouldn't be a problem because the function is expected to be called only once during regular QEMU usage.
--- include/hw/boards.h | 2 ++ hw/core/machine.c | 16 ++++++++++++++++ softmmu/vl.c | 17 +---------------- 3 files changed, 19 insertions(+), 16 deletions(-)
include/hw/boards.h +2 -0
@@ -43,6 +43,8 @@ void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type);MemoryRegion *machine_consume_memdev (MachineState *machine,HostMemoryBackend *backend); +MachineClass *machine_find_class (const char *name); +/** * CPUArchId: * @arch_id - architecture-dependent CPU ID of present or possible CPU
hw/core/machine.c +16 -0
@@ -1136,6 +1136,22 @@ void machine_run_board_init(MachineState *machine) machine_class->init (machine); } +MachineClass *machine_find_class (const char *name) +{ +g_autoptr (GSList) machines =object_class_get_list (TYPE_MACHINE , false); +GSList *el; + +for (el = machines; el; el = el->next) { +MachineClass *mc = el->data; + +if (!strcmp (mc->name, name) || !g_strcmp0 (mc->alias, name)) { +return mc; + } + } + +return NULL ; +} +static const TypeInfo machine_info = { .name =TYPE_MACHINE , .parent =TYPE_OBJECT ,
softmmu/vl.c +1 -16
@@ -1159,21 +1159,6 @@ static int usb_parse(const char *cmdline)MachineState *current_machine; -static MachineClass *find_machine (const char *name,GSList *machines) -{ -GSList *el; - -for (el = machines; el; el = el->next) { -MachineClass *mc = el->data; - -if (!strcmp (mc->name, name) || !g_strcmp0 (mc->alias, name)) { -return mc; - } - } - -return NULL ; -} -static MachineClass *find_default_machine (GSList *machines) {GSList *el;
@@ -2341,7 +2326,7 @@ static MachineClass *machine_parse(const char *name, GSList *machines)exit (0 ); } - mc =find_machine (name, machines); + mc =machine_find_class (name);if (!mc) {error_report ("unsupported machine type" );error_printf ("Use -machine help to list supported machines\n" );
Signature · 1 line
2.28.0
PATCH v2 2/7 i386: Add X86CPUModel.alias_of field
In reply to Eduardo HabkostInstead of calling x86_cpu_class_get_alias_of(), just save the actual CPU model name in X86CPUModel and use it in -cpu help.
--- target/i386/cpu.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
target/i386/cpu.c +11 -4
@@ -1647,6 +1647,12 @@ struct X86CPUModel {* This matters only for "-cpu help" and query-cpu-definitions */ bool is_alias; +/* +* Valid only if is_alias is true. +* If NULL, actual alias depend on machine type. +* If not NULL, name of actual CPU model this is an alias to. +*/ +const char *alias_of; };/* Get full model name for CPU version */
@@ -4927,14 +4933,13 @@ static void x86_cpu_list_entry(gpointer data, gpointer user_data)X86CPUClass *cc =X86_CPU_CLASS (oc); g_autofree char *name = x86_cpu_class_get_model_name(cc); g_autofree char *desc = g_strdup(cc->model_description); - g_autofree char *alias_of = x86_cpu_class_get_alias_of(cc); g_autofree char *model_id = x86_cpu_class_get_model_id(cc); -if (!desc && alias_of) { -if (cc->model && cc->model->version ==CPU_VERSION_AUTO ) { +if (!desc && cc->model && cc->model->is_alias) { +if (!cc->model->alias_of) { desc =g_strdup ("(alias configured by machine type)" ); }else { - desc =g_strdup_printf ("(alias of %s)" , alias_of); + desc =g_strdup_printf ("(alias of %s)" , cc->model->alias_of); } }if (!desc && cc->model && cc->model->note) {
@@ -5418,6 +5423,7 @@ static void x86_register_cpudef_types(X86CPUDefinition *def) m->cpudef = def; m->version =CPU_VERSION_AUTO ; m->is_alias = true; + m->alias_of =NULL ;/* depends on machine type */ x86_register_cpu_model_type (def->name, m);/* Versioned models: */
@@ -5436,6 +5442,7 @@ static void x86_register_cpudef_types(X86CPUDefinition *def) am->cpudef = def; am->version = vdef->version; am->is_alias = true; + am->alias_of =g_strdup (name);x86_register_cpu_model_type (vdef->alias, am); } }
Signature · 1 line
2.28.0
PATCH v2 3/7 i386: Replace x86_cpu_class_get_alias_of() with x86_cpu_model_resolve_alias()
In reply to Eduardo HabkostNew function is very similar to the old function, but it just takes a X86CPUModel as argument, and its name makes its purpose clearer.
--- target/i386/cpu.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-)
target/i386/cpu.c +17 -16
@@ -4170,6 +4170,21 @@ static X86CPUVersion x86_cpu_model_resolve_version(const X86CPUModel *model)return v; } +/* Resolve CPU model alias to actual CPU model name */ +static char *x86_cpu_model_resolve_alias (const X86CPUModel *model) +{ +X86CPUVersion version; + +if (!model->is_alias) { +return NULL ; + } + version =x86_cpu_model_resolve_version (model); +if (version <=0 ) { +return NULL ; + } +return x86_cpu_versioned_model_name (model->cpudef, version); +} +void x86_cpu_change_kvm_default (const char *prop,const char *value) {PropValue *pv;
@@ -4913,20 +4928,6 @@ static char *x86_cpu_class_get_model_id(X86CPUClass *xc)return r; } -static char *x86_cpu_class_get_alias_of (X86CPUClass *cc) -{ -X86CPUVersion version; - -if (!cc->model || !cc->model->is_alias) { -return NULL ; - } - version =x86_cpu_model_resolve_version (cc->model); -if (version <=0 ) { -return NULL ; - } -return x86_cpu_versioned_model_name (cc->model->cpudef, version); -} -static void x86_cpu_list_entry (gpointer data,gpointer user_data) {ObjectClass *oc = data;
@@ -5002,8 +5003,8 @@ static void x86_cpu_definition_entry(gpointer data, gpointer user_data)* Old machine types won't report aliases, so that alias translation * doesn't break compatibility with previous QEMU versions. */ -if (default_cpu_version !=CPU_VERSION_LEGACY ) { - info->alias_of =x86_cpu_class_get_alias_of (cc); +if (cc->model && default_cpu_version !=CPU_VERSION_LEGACY ) { + info->alias_of =x86_cpu_model_resolve_alias (cc->model); info->has_alias_of = !!info->alias_of; }
Signature · 1 line
2.28.0
PATCH v2 4/7 i386: Add default_version parameter to CPU version functions
In reply to Eduardo HabkostCurrently, the functions that resolve CPU model versions (x86_cpu_model_resolve_version(), x86_cpu_model_resolve_alias()) need a machine to be initialized first. Get rid of this requirement by making those functions get an explicit default_version argument.
No behavior changes are introduced by this patch, as all callers are being changed to use default_cpu_version as argument.
--- target/i386/cpu.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
target/i386/cpu.c +10 -6
@@ -4158,11 +4158,12 @@ static X86CPUVersion x86_cpu_model_last_version(const X86CPUModel *model) }/* Return the actual version being used for a specific CPU model */ -static X86CPUVersion x86_cpu_model_resolve_version (const X86CPUModel *model) +static X86CPUVersion x86_cpu_model_resolve_version (const X86CPUModel *model, +X86CPUVersion default_version) {X86CPUVersion v = model->version;if (v ==CPU_VERSION_AUTO ) { - v = default_cpu_version; + v = default_version; }if (v ==CPU_VERSION_LATEST ) {return x86_cpu_model_last_version (model);
@@ -4171,14 +4172,15 @@ static X86CPUVersion x86_cpu_model_resolve_version(const X86CPUModel *model) }/* Resolve CPU model alias to actual CPU model name */ -static char *x86_cpu_model_resolve_alias (const X86CPUModel *model) +static char *x86_cpu_model_resolve_alias (const X86CPUModel *model, +X86CPUVersion default_version) {X86CPUVersion version;if (!model->is_alias) {return NULL ; } - version =x86_cpu_model_resolve_version (model); + version =x86_cpu_model_resolve_version (model, default_version);if (version <=0 ) {return NULL ; }
@@ -5004,7 +5006,8 @@ static void x86_cpu_definition_entry(gpointer data, gpointer user_data)* doesn't break compatibility with previous QEMU versions. */ if (cc->model && default_cpu_version !=CPU_VERSION_LEGACY ) { - info->alias_of =x86_cpu_model_resolve_alias (cc->model); + info->alias_of =x86_cpu_model_resolve_alias (cc->model, + default_cpu_version); info->has_alias_of = !!info->alias_of; }
@@ -5075,7 +5078,8 @@ static void x86_cpu_apply_props(X86CPU *cpu, PropValue *props)static void x86_cpu_apply_version_props (X86CPU *cpu,X86CPUModel *model) {const X86CPUVersionDefinition *vdef; -X86CPUVersion version =x86_cpu_model_resolve_version (model); +X86CPUVersion version =x86_cpu_model_resolve_version (model, + default_cpu_version);if (version ==CPU_VERSION_LEGACY ) {return ;
Signature · 1 line
2.28.0
PATCH v2 5/7 i386: Wrap QMP code in !CONFIG_USER_ONLY
In reply to Eduardo HabkostThe QMP code was never called by *-user, and we will add machine-type-specific code there, so add #ifdefs to make it safe.
--- target/i386/cpu.c | 6 ++++++ 1 file changed, 6 insertions(+)
target/i386/cpu.c +6 -0
@@ -4171,6 +4171,7 @@ static X86CPUVersion x86_cpu_model_resolve_version(const X86CPUModel *model,return v; } +#ifndef CONFIG_USER_ONLY /* Resolve CPU model alias to actual CPU model name */ static char *x86_cpu_model_resolve_alias (const X86CPUModel *model,X86CPUVersion default_version)
@@ -4186,6 +4187,7 @@ static char *x86_cpu_model_resolve_alias(const X86CPUModel *model, }return x86_cpu_versioned_model_name (model->cpudef, version); } +#endif void x86_cpu_change_kvm_default (const char *prop,const char *value) {
@@ -4838,6 +4840,7 @@ static void x86_cpu_get_unavailable_features(Object *obj, Visitor *v,visit_type_strList (v,"unavailable-features" , &result, errp); } +#ifndef CONFIG_USER_ONLY /* Check for missing features that may prevent the CPU class from * running using the current machine and accelerator. */
@@ -4876,6 +4879,7 @@ static void x86_cpu_class_check_missing_features(X86CPUClass *xcc, object_unref(OBJECT(xc)); } +#endif /* Print all cpuid feature names in featureset */
@@ -4985,6 +4989,7 @@ void x86_cpu_list(void)g_list_free (names); } +#ifndef CONFIG_USER_ONLY static void x86_cpu_definition_entry (gpointer data,gpointer user_data) {ObjectClass *oc = data;
@@ -5025,6 +5030,7 @@ CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)g_slist_free (list); return cpu_list; } +#endif static uint64_t x86_cpu_get_supported_feature_word (FeatureWord w,bool migratable_only)
Signature · 1 line
2.28.0
PATCH v2 6/7 i386: Don't use default_cpu_version inside x86_cpu_definition_entry()
In reply to Eduardo HabkostWe will change query-cpu-definitions to have a new machine parameter. Move the code that reads default_cpu_version to qmp_query_cpu_definitions() to make that easier to implement.
This patch shouldn't introduce any behavior change. Results of query-cpu-definition will be exactly the same.
--- target/i386/cpu.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-)
target/i386/cpu.c +15 -8
@@ -4990,11 +4990,16 @@ void x86_cpu_list(void) }#ifndef CONFIG_USER_ONLY +typedef struct X86CPUDefinitionArgs { +CpuDefinitionInfoList *cpu_list; +X86CPUVersion default_version; +}X86CPUDefinitionArgs ; +static void x86_cpu_definition_entry (gpointer data,gpointer user_data) {ObjectClass *oc = data;X86CPUClass *cc =X86_CPU_CLASS (oc); -CpuDefinitionInfoList **cpu_list = user_data; +X86CPUDefinitionArgs *args = user_data;CpuDefinitionInfoList *entry;CpuDefinitionInfo *info;
@@ -5010,25 +5015,27 @@ static void x86_cpu_definition_entry(gpointer data, gpointer user_data)* Old machine types won't report aliases, so that alias translation * doesn't break compatibility with previous QEMU versions. */ -if (cc->model && default_cpu_version !=CPU_VERSION_LEGACY ) { +if (cc->model && args->default_version !=CPU_VERSION_LEGACY ) { info->alias_of =x86_cpu_model_resolve_alias (cc->model, - default_cpu_version); + args->default_version); info->has_alias_of = !!info->alias_of; } entry =g_malloc0 (sizeof (*entry)); entry->value = info; - entry->next = *cpu_list; - *cpu_list = entry; + entry->next = args->cpu_list; + args->cpu_list = entry; }CpuDefinitionInfoList *qmp_query_cpu_definitions (Error **errp) { -CpuDefinitionInfoList *cpu_list =NULL ; +X86CPUDefinitionArgs args = { .cpu_list =NULL };GSList *list =get_sorted_cpu_model_list (); -g_slist_foreach (list, x86_cpu_definition_entry, &cpu_list); + + args.default_version = default_cpu_version; +g_slist_foreach (list, x86_cpu_definition_entry, &args);g_slist_free (list); -return cpu_list; +return args.cpu_list; }#endif
Signature · 1 line
2.28.0
PATCH v2 7/7 cpu: Add `machine` parameter to query-cpu-definitions
In reply to Eduardo HabkostThe new parameter can be used by management software to query for CPU model alias information for multiple machines without restarting QEMU.
---
Changes v1 -> v2:
* Rewrite documentation, with suggestions from Markus
--- qapi/machine-target.json | 14 ++++++- target/arm/helper.c | 4 +- target/i386/cpu.c | 24 ++++++++++- target/mips/helper.c | 4 +- target/s390x/cpu_models.c | 4 +- target/ppc/translate_init.c.inc | 4 +- tests/acceptance/x86_cpu_model_versions.py | 48 ++++++++++++++++++++++ 7 files changed, 95 insertions(+), 7 deletions(-)
qapi/machine-target.json +13 -1
@@ -282,6 +282,10 @@ # # @alias-of: Name of CPU model this model is an alias for. The target of the # CPU model alias may change depending on the machine type. +# If the @machine argument was provided to query-cpu-definitions, +# alias information for that machine type will be returned. +# If @machine is not provided, alias information for +# the current machine will be returned. # Management software is supposed to translate CPU model aliases # in the VM configuration, because aliases may stop being # migration-safe in the future (since 4.1)
@@ -318,9 +322,17 @@ # # Return a list of supported virtual CPU definitions # +# @machine: Name of machine type. The value of the @alias-of field +# is machine-specific. This overrides the machine type +# used to look up that information. This can be used +# to query machine-specific CPU model aliases without +# restarting QEMU (since 5.2) +# # Returns: a list of CpuDefInfo # # Since: 1.2.0 ## -{ 'command': 'query-cpu-definitions', 'returns': ['CpuDefinitionInfo'], +{ 'command': 'query-cpu-definitions', + 'data': { '*machine': 'str' }, + 'returns': ['CpuDefinitionInfo'], 'if': 'defined(TARGET_PPC) || defined(TARGET_ARM) || defined(TARGET_I386) || defined(TARGET_S390X) || defined(TARGET_MIPS)' }
target/arm/helper.c +3 -1
@@ -8276,7 +8276,9 @@ static void arm_cpu_add_definition(gpointer data, gpointer user_data) *cpu_list = entry; } -CpuDefinitionInfoList *qmp_query_cpu_definitions (Error **errp) +CpuDefinitionInfoList *qmp_query_cpu_definitions (bool has_machine, +const char *machine, +Error **errp) {CpuDefinitionInfoList *cpu_list =NULL ;GSList *list;
target/i386/cpu.c +22 -2
@@ -56,6 +56,7 @@#ifndef CONFIG_USER_ONLY #include "exec/address-spaces.h" #include "hw/i386/apic_internal.h" +#include "hw/i386/pc.h" #include "hw/boards.h" #endif
@@ -4187,6 +4188,13 @@ static char *x86_cpu_model_resolve_alias(const X86CPUModel *model, }return x86_cpu_versioned_model_name (model->cpudef, version); } + +static X86CPUVersion default_cpu_version_for_machine (MachineClass *mc) +{ +PCMachineClass *pcmc = + (PCMachineClass *)object_class_dynamic_cast (OBJECT_CLASS (mc),TYPE_PC_MACHINE ); +return pcmc ? pcmc->default_cpu_version :1 ; +}#endif void x86_cpu_change_kvm_default (const char *prop,const char *value)
@@ -5027,12 +5035,24 @@ static void x86_cpu_definition_entry(gpointer data, gpointer user_data) args->cpu_list = entry; } -CpuDefinitionInfoList *qmp_query_cpu_definitions (Error **errp) +CpuDefinitionInfoList *qmp_query_cpu_definitions (bool has_machine, +const char *machine, +Error **errp) {X86CPUDefinitionArgs args = { .cpu_list =NULL };GSList *list =get_sorted_cpu_model_list (); - args.default_version = default_cpu_version; +if (!has_machine) { + args.default_version = default_cpu_version; + }else { +MachineClass *mc =machine_find_class (machine); +if (!mc) { +error_setg (errp,"Machine type '%s' not found" , machine); +return NULL ; + } + args.default_version =default_cpu_version_for_machine (mc); + } +g_slist_foreach (list, x86_cpu_definition_entry, &args);g_slist_free (list);return args.cpu_list;
target/mips/helper.c +3 -1
@@ -1518,7 +1518,9 @@ static void mips_cpu_add_definition(gpointer data, gpointer user_data) *cpu_list = entry; } -CpuDefinitionInfoList *qmp_query_cpu_definitions (Error **errp) +CpuDefinitionInfoList *qmp_query_cpu_definitions (bool has_machine, +const char *machine, +Error **errp) {CpuDefinitionInfoList *cpu_list =NULL ;GSList *list;
target/s390x/cpu_models.c +3 -1
@@ -456,7 +456,9 @@ static void create_cpu_model_list(ObjectClass *klass, void *opaque) *cpu_list = entry; } -CpuDefinitionInfoList *qmp_query_cpu_definitions (Error **errp) +CpuDefinitionInfoList *qmp_query_cpu_definitions (bool has_machine, +const char *machine, +Error **errp) {struct CpuDefinitionInfoListData list_data = { .list =NULL ,
target/ppc/translate_init.c.inc +3 -1
@@ -10635,7 +10635,9 @@ static void ppc_cpu_defs_entry(gpointer data, gpointer user_data) *first = entry; } -CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp) +CpuDefinitionInfoList *qmp_query_cpu_definitions(bool has_machine, + const char *machine, + Error **errp) { CpuDefinitionInfoList *cpu_list = NULL; GSList *list;
tests/acceptance/x86_cpu_model_versions.py +48 -0
54 lines not on this page · raw message
Signature · 1 line
2.28.0