target/riscv: implement query-cpu-definitions
Subsystems (2)
cover target/riscv: implement query-cpu-definitions
Hi,
This series implements one of the most common QMP CPU commands, query-cpu-definitions.
libvirt and others uses this command to inspect the CPUs available in the QEMU binary, with the option to further inspect their properties using device-list-properties.
The design decision made here is to consider all generic (i.e. not named) CPUs as not static, which the API semantics considers to be the same as migration-unsafe. The reason why I took this decision is because generic CPUs can have their extensions changed via command line. Today this is the case for all CPUs, but we're going to change that in the near future after this series.
Patches were based on top of Alistair's riscv-to-apply.next.
Daniel Henrique Barboza (4):
target/riscv: add CPU QOM header
target/riscv: add query-cpy-definitions support target/riscv: add 'static' attribute of query-cpu-definitions target/riscv: make generic cpus not static
qapi/machine-target.json | 6 ++- target/riscv/cpu-qom.h | 73 +++++++++++++++++++++++++++++++++++ target/riscv/cpu.c | 54 ++++++++++++++++++++++++-- target/riscv/cpu.h | 46 +--------------------- target/riscv/meson.build | 3 +- target/riscv/riscv-qmp-cmds.c | 55 ++++++++++++++++++++++++++ 6 files changed, 185 insertions(+), 52 deletions(-) create mode 100644 target/riscv/cpu-qom.h create mode 100644 target/riscv/riscv-qmp-cmds.c
Signature · 1 line
2.39.2
PATCH 1/4 target/riscv: add CPU QOM header
QMP CPU commands are usually implemented by a separated file, <arch>-qmp-cmds.c, to allow them to be build only for softmmu targets. This file uses a CPU QOM header with basic QOM declarations for the arch.
We'll introduce query-cpu-definitions for RISC-V CPUs in the next patch, but first we need a cpu-qom.h header with the definitions of TYPE_RISCV_CPU and RISCVCPUClass declarations. These were moved from cpu.h to the new file, and cpu.h now includes "cpu-qom.h".
--- target/riscv/cpu-qom.h | 70 ++++++++++++++++++++++++++++++++++++++++++ target/riscv/cpu.h | 46 +-------------------------- 2 files changed, 71 insertions(+), 45 deletions(-) create mode 100644 target/riscv/cpu-qom.h
target/riscv/cpu-qom.h new file +70 -0
@@ -0,0 +1,70 @@ +/* +* QEMU RISC-V CPU QOM header +* +* Copyright (c) 2023 Ventana Micro Systems Inc. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2 or later, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef RISCV_CPU_QOM_H +#define RISCV_CPU_QOM_H + +#include "hw/core/cpu.h" +#include "qom/object.h" + +#define TYPE_RISCV_CPU "riscv-cpu" + +#define RISCV_CPU_TYPE_SUFFIX "-" TYPE_RISCV_CPU +#define RISCV_CPU_TYPE_NAME (name) (name RISCV_CPU_TYPE_SUFFIX) +#define CPU_RESOLVING_TYPE TYPE_RISCV_CPU + +#define TYPE_RISCV_CPU_ANY RISCV_CPU_TYPE_NAME("any") +#define TYPE_RISCV_CPU_BASE32 RISCV_CPU_TYPE_NAME("rv32") +#define TYPE_RISCV_CPU_BASE64 RISCV_CPU_TYPE_NAME("rv64") +#define TYPE_RISCV_CPU_BASE128 RISCV_CPU_TYPE_NAME("x-rv128") +#define TYPE_RISCV_CPU_IBEX RISCV_CPU_TYPE_NAME("lowrisc-ibex") +#define TYPE_RISCV_CPU_SHAKTI_C RISCV_CPU_TYPE_NAME("shakti-c") +#define TYPE_RISCV_CPU_SIFIVE_E31 RISCV_CPU_TYPE_NAME("sifive-e31") +#define TYPE_RISCV_CPU_SIFIVE_E34 RISCV_CPU_TYPE_NAME("sifive-e34") +#define TYPE_RISCV_CPU_SIFIVE_E51 RISCV_CPU_TYPE_NAME("sifive-e51") +#define TYPE_RISCV_CPU_SIFIVE_U34 RISCV_CPU_TYPE_NAME("sifive-u34") +#define TYPE_RISCV_CPU_SIFIVE_U54 RISCV_CPU_TYPE_NAME("sifive-u54") +#define TYPE_RISCV_CPU_THEAD_C906 RISCV_CPU_TYPE_NAME("thead-c906") +#define TYPE_RISCV_CPU_HOST RISCV_CPU_TYPE_NAME("host") + +#if defined(TARGET_RISCV32 ) +# define TYPE_RISCV_CPU_BASE TYPE_RISCV_CPU_BASE32 +#elif defined(TARGET_RISCV64 ) +# define TYPE_RISCV_CPU_BASE TYPE_RISCV_CPU_BASE64 +#endif + +typedef struct CPUArchState CPURISCVState ; + +OBJECT_DECLARE_CPU_TYPE (RISCVCPU , RISCVCPUClass,RISCV_CPU ) + +/** +* RISCVCPUClass: +* @parent_realize: The parent class' realize handler. +* @parent_phases: The parent class' reset phase handlers. +* +* A RISCV CPU model. + */ +struct RISCVCPUClass { +/*< private >*/ +CPUClass parent_class; +/*< public >*/ +DeviceRealize parent_realize; +ResettablePhases parent_phases; +}; + +#endif /* RISCV_CPU_QOM_H */
target/riscv/cpu.h +1 -45
70 lines not on this page · raw message
Signature · 1 line
2.39.2
PATCH 3/4 target/riscv: add 'static' attribute of query-cpu-definitions
In reply to Daniel Henrique Barboza'static' is defined in the QMP doc as:
"whether a CPU definition is static and will not change depending on QEMU version, machine type, machine options and accelerator options. A static model is always migration-safe."
For RISC-V we'll consider all named CPUs as static since their extensions can't be changed by user input. Generic CPUs will be considered non-static.
We aren't ready to make the change for generic CPUs yet because we're using the same class init for every CPU. We'll deal with it next.
--- target/riscv/cpu-qom.h | 3 +++ target/riscv/cpu.c | 6 ++++++ target/riscv/riscv-qmp-cmds.c | 2 ++ 3 files changed, 11 insertions(+)
target/riscv/cpu-qom.h +3 -0
@@ -54,6 +54,7 @@ OBJECT_DECLARE_CPU_TYPE(RISCVCPU, RISCVCPUClass, RISCV_CPU)/** * RISCVCPUClass: +* @static_model: See CpuDefinitionInfo::static * @parent_realize: The parent class' realize handler. * @parent_phases: The parent class' reset phase handlers. *
@@ -65,6 +66,8 @@ struct RISCVCPUClass {/*< public >*/ DeviceRealize parent_realize;ResettablePhases parent_phases; + + bool static_model; };#endif /* RISCV_CPU_QOM_H */
target/riscv/cpu.c +6 -0
@@ -1679,6 +1679,12 @@ static void riscv_cpu_class_init(ObjectClass *c, void *data)resettable_class_set_parent_phases (rc,NULL , riscv_cpu_reset_hold,NULL , &mcc->parent_phases); +/* +* Consider all models to be static. Each CPU is free to +* set it to false if needed. +*/ + mcc->static_model = true; + cc->class_by_name = riscv_cpu_class_by_name; cc->has_work = riscv_cpu_has_work; cc->dump_state = riscv_cpu_dump_state;
target/riscv/riscv-qmp-cmds.c +2 -0
@@ -30,6 +30,7 @@static void riscv_cpu_add_definition (gpointer data,gpointer user_data) {ObjectClass *oc = data; +RISCVCPUClass *cc =RISCV_CPU_CLASS (oc);CpuDefinitionInfoList **cpu_list = user_data;CpuDefinitionInfo *info =g_malloc0 (sizeof (*info));const char *typename =object_class_get_name (oc);
@@ -37,6 +38,7 @@ static void riscv_cpu_add_definition(gpointer data, gpointer user_data) info->name =g_strndup (typename,strlen (typename) -strlen ("-" TYPE_RISCV_CPU )); info->q_typename =g_strdup (typename); + info->q_static = cc->static_model;QAPI_LIST_PREPEND (*cpu_list, info); }
Signature · 1 line
2.39.2
PATCH 2/4 target/riscv: add query-cpy-definitions support
In reply to Daniel Henrique BarbozaThis command is used by tooling like libvirt to retrieve a list of supported CPUs. Each entry returns a CpuDefinitionInfo object that contains more information about each CPU.
This initial support includes only the name of the CPU and its typename. Here's what the command produces for the riscv64 target:
$ ./build/qemu-system-riscv64 -S -M virt -display none -qmp stdio
{"QMP": {"version": (...)}
{"execute": "qmp_capabilities", "arguments": {"enable": ["oob"]}}
{"return": {}}
{"execute": "query-cpu-definitions"}
{"return": [
{"name": "rv64", "typename": "rv64-riscv-cpu", "static": false, "deprecated": false},
{"name": "sifive-e51", "typename": "sifive-e51-riscv-cpu", "static": false, "deprecated": false},
{"name": "any", "typename": "any-riscv-cpu", "static": false, "deprecated": false},
{"name": "x-rv128", "typename": "x-rv128-riscv-cpu", "static": false, "deprecated": false},
{"name": "shakti-c", "typename": "shakti-c-riscv-cpu", "static": false, "deprecated": false},
{"name": "thead-c906", "typename": "thead-c906-riscv-cpu", "static": false, "deprecated": false},
{"name": "sifive-u54", "typename": "sifive-u54-riscv-cpu", "static": false, "deprecated": false}]
}Next patches will implement the 'static' attribute of CpuDefinitionInfo.
--- qapi/machine-target.json | 6 ++-- target/riscv/meson.build | 3 +- target/riscv/riscv-qmp-cmds.c | 53 +++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 target/riscv/riscv-qmp-cmds.c
qapi/machine-target.json +4 -2
@@ -324,7 +324,8 @@ 'TARGET_I386', 'TARGET_S390X', 'TARGET_MIPS', - 'TARGET_LOONGARCH64' ] } } + 'TARGET_LOONGARCH64', + 'TARGET_RISCV' ] } } ## # @query-cpu-definitions:
@@ -341,4 +342,5 @@ 'TARGET_I386', 'TARGET_S390X', 'TARGET_MIPS', - 'TARGET_LOONGARCH64' ] } } + 'TARGET_LOONGARCH64', + 'TARGET_RISCV' ] } }
target/riscv/meson.build +2 -1
@@ -32,7 +32,8 @@ riscv_softmmu_ss.add(files( 'monitor.c', 'machine.c', 'pmu.c', - 'time_helper.c' + 'time_helper.c', + 'riscv-qmp-cmds.c', )) target_arch += {'riscv': riscv_ss}
target/riscv/riscv-qmp-cmds.c new file +53 -0
53 lines not on this page · raw message
Signature · 1 line
2.39.2
PATCH 4/4 target/riscv: make generic cpus not static
In reply to Daniel Henrique BarbozaA CPU is declared static or not by changing the class attribute 'static'. For now the base class is defining every CPU as static via riscv_cpu_class_init().
To change this setting for generic CPUs we'll need a different class init for them. Then we'll ned a macro that allows us to set a different .class_init implementation for the CPU. With all that we're now able to set 'static' as false for the 'any', 'rv32', 'rv64' and 'x-rv128' CPUs. For the riscv64 target:
$ ./build/qemu-system-riscv64 -S -M virt -display none -qmp stdio
{"QMP": {"version": (...) }
{"execute": "qmp_capabilities", "arguments": {"enable": ["oob"]}}
{"return": {}}
{"execute": "query-cpu-definitions"}
{"return": [
{"name": "rv64", "typename": "rv64-riscv-cpu", "static": false, "deprecated": false},
{"name": "sifive-e51", "typename": "sifive-e51-riscv-cpu", "static": true, "deprecated": false},
{"name": "any", "typename": "any-riscv-cpu", "static": false, "deprecated": false},
{"name": "x-rv128", "typename": "x-rv128-riscv-cpu", "static": false, "deprecated": false},
{"name": "shakti-c", "typename": "shakti-c-riscv-cpu", "static": true, "deprecated": false},
{"name": "thead-c906", "typename": "thead-c906-riscv-cpu", "static": true, "deprecated": false},
{"name": "sifive-u54", "typename": "sifive-u54-riscv-cpu", "static": true, "deprecated": false}]
}--- target/riscv/cpu.c | 48 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-)
target/riscv/cpu.c +44 -4
@@ -340,6 +340,13 @@ static void set_satp_mode_default_map(RISCVCPU *cpu) }#endif +static void riscv_any_cpu_class_init (ObjectClass *c,void *data) +{ +RISCVCPUClass *mcc =RISCV_CPU_CLASS (c); + + mcc->static_model = false; +} +static void riscv_any_cpu_init (Object *obj) {CPURISCVState *env = &RISCV_CPU (obj)->env;
@@ -360,6 +367,13 @@ static void riscv_any_cpu_init(Object *obj) }#if defined(TARGET_RISCV64 ) +static void rv64_base_cpu_class_init (ObjectClass *c,void *data) +{ +RISCVCPUClass *mcc =RISCV_CPU_CLASS (c); + + mcc->static_model = false; +} +static void rv64_base_cpu_init (Object *obj) {CPURISCVState *env = &RISCV_CPU (obj)->env;
@@ -430,6 +444,13 @@ static void rv64_thead_c906_cpu_init(Object *obj)#endif } +static void rv128_base_cpu_class_init (ObjectClass *c,void *data) +{ +RISCVCPUClass *mcc =RISCV_CPU_CLASS (c); + + mcc->static_model = false; +} +static void rv128_base_cpu_init (Object *obj) {if (qemu_tcg_mttcg_enabled ()) {
@@ -449,6 +470,13 @@ static void rv128_base_cpu_init(Object *obj)#endif }#else +static void rv32_base_cpu_class_init (ObjectClass *c,void *data) +{ +RISCVCPUClass *mcc =RISCV_CPU_CLASS (c); + + mcc->static_model = false; +} +static void rv32_base_cpu_init (Object *obj) {CPURISCVState *env = &RISCV_CPU (obj)->env;
@@ -1779,6 +1807,14 @@ void riscv_cpu_list(void) .instance_init = initfn \ } +#define DEFINE_CPU_WITH_CLASSFN (type_name, initfn, classfn) \ + { \ + .name = type_name, \ + .parent = TYPE_RISCV_CPU, \ + .instance_init = initfn, \ + .class_init = classfn \ + } +static const TypeInfo riscv_cpu_type_infos[] = { { .name =TYPE_RISCV_CPU ,
@@ -1790,23 +1826,27 @@ static const TypeInfo riscv_cpu_type_infos[] = { .class_size = sizeof(RISCVCPUClass), .class_init = riscv_cpu_class_init, }, - DEFINE_CPU(TYPE_RISCV_CPU_ANY, riscv_any_cpu_init), + DEFINE_CPU_WITH_CLASSFN(TYPE_RISCV_CPU_ANY, riscv_any_cpu_init, + riscv_any_cpu_class_init),#if defined(CONFIG_KVM ) DEFINE_CPU(TYPE_RISCV_CPU_HOST, riscv_host_cpu_init),#endif #if defined(TARGET_RISCV32 ) - DEFINE_CPU(TYPE_RISCV_CPU_BASE32, rv32_base_cpu_init), + DEFINE_CPU_WITH_CLASSFN(TYPE_RISCV_CPU_BASE32, rv32_base_cpu_init, + rv32_base_cpu_class_init), DEFINE_CPU(TYPE_RISCV_CPU_IBEX, rv32_ibex_cpu_init), DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E31, rv32_sifive_e_cpu_init), DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E34, rv32_imafcu_nommu_cpu_init), DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_U34, rv32_sifive_u_cpu_init), #elif defined(TARGET_RISCV64) -DEFINE_CPU (TYPE_RISCV_CPU_BASE64 , rv64_base_cpu_init), +DEFINE_CPU_WITH_CLASSFN (TYPE_RISCV_CPU_BASE64 , rv64_base_cpu_init, + rv64_base_cpu_class_init), +DEFINE_CPU_WITH_CLASSFN (TYPE_RISCV_CPU_BASE128 , rv128_base_cpu_init, + rv128_base_cpu_class_init),DEFINE_CPU (TYPE_RISCV_CPU_SIFIVE_E51 , rv64_sifive_e_cpu_init),DEFINE_CPU (TYPE_RISCV_CPU_SIFIVE_U54 , rv64_sifive_u_cpu_init),DEFINE_CPU (TYPE_RISCV_CPU_SHAKTI_C , rv64_sifive_u_cpu_init), DEFINE_CPU(TYPE_RISCV_CPU_THEAD_C906, rv64_thead_c906_cpu_init), - DEFINE_CPU(TYPE_RISCV_CPU_BASE128, rv128_base_cpu_init), #endif };
Signature · 1 line
2.39.2
Re: [PATCH 4/4] target/riscv: make generic cpus not static
Quoted 49 lines · Daniel Henrique Barboza
A CPU is declared static or not by changing the class attribute 'static'. For now the base class is defining every CPU as static via
riscv_cpu_class_init().To change this setting for generic CPUs we'll need a different class init for them. Then we'll ned a macro that allows us to set a different .class_init implementation for the CPU. With all that we're now able to set 'static' as false for the 'any', 'rv32', 'rv64' and 'x-rv128' CPUs. For the riscv64 target:
$ ./build/qemu-system-riscv64 -S -M virt -display none -qmp stdio {"QMP": {"version": (...) } {"execute": "qmp_capabilities", "arguments": {"enable": ["oob"]}} {"return": {}} {"execute": "query-cpu-definitions"} {"return": [ {"name": "rv64", "typename": "rv64-riscv-cpu", "static": false, "deprecated": false}, {"name": "sifive-e51", "typename": "sifive-e51-riscv-cpu", "static": true, "deprecated": false}, {"name": "any", "typename": "any-riscv-cpu", "static": false, "deprecated": false}, {"name": "x-rv128", "typename": "x-rv128-riscv-cpu", "static": false, "deprecated": false}, {"name": "shakti-c", "typename": "shakti-c-riscv-cpu", "static": true, "deprecated": false}, {"name": "thead-c906", "typename": "thead-c906-riscv-cpu", "static": true, "deprecated": false}, {"name": "sifive-u54", "typename": "sifive-u54-riscv-cpu", "static": true, "deprecated": false}] }Signed-off-by Daniel Henrique Barboza <[email protected]>--- target/riscv/cpu.c | 48 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-)target/riscv/cpu.c +7
-0@@ -340,6 +340,13 @@ static void set_satp_mode_default_map(RISCVCPU *cpu) }#endif +static void riscv_any_cpu_class_init (ObjectClass *c,void *data) +{ +RISCVCPUClass *mcc =RISCV_CPU_CLASS (c); + + mcc->static_model = false; +} +static void riscv_any_cpu_init (Object *obj) {CPURISCVState *env = &RISCV_CPU (obj)->env;
target/riscv/cpu.c static void riscv_any_cpu_init(Object *obj) · line 376
Quoted 10 lines
target/riscv/cpu.c +7
-0@@ -360,6 +367,13 @@ static void riscv_any_cpu_init(Object *obj) }#if defined(TARGET_RISCV64 ) +static void rv64_base_cpu_class_init (ObjectClass *c,void *data) +{ +RISCVCPUClass *mcc =RISCV_CPU_CLASS (c); + + mcc->static_model = false; +} +
We needn't duplicate the similar function. They can share a special class_init for general cpus.
Regards,
Weiwei li
Quoted 78 lines
target/riscv/cpu.c +30
-4static void rv64_base_cpu_init (Object *obj) {CPURISCVState *env = &RISCV_CPU (obj)->env;@@ -430,6 +444,13 @@ static void rv64_thead_c906_cpu_init(Object *obj)#endif } +static void rv128_base_cpu_class_init (ObjectClass *c,void *data) +{ +RISCVCPUClass *mcc =RISCV_CPU_CLASS (c); + + mcc->static_model = false; +} +static void rv128_base_cpu_init (Object *obj) {if (qemu_tcg_mttcg_enabled ()) {@@ -449,6 +470,13 @@ static void rv128_base_cpu_init(Object *obj)#endif }#else +static void rv32_base_cpu_class_init (ObjectClass *c,void *data) +{ +RISCVCPUClass *mcc =RISCV_CPU_CLASS (c); + + mcc->static_model = false; +} +static void rv32_base_cpu_init (Object *obj) {CPURISCVState *env = &RISCV_CPU (obj)->env;@@ -1779,6 +1807,14 @@ void riscv_cpu_list(void) .instance_init = initfn \ } +#define DEFINE_CPU_WITH_CLASSFN (type_name, initfn, classfn) \ + { \ + .name = type_name, \ + .parent = TYPE_RISCV_CPU, \ + .instance_init = initfn, \ + .class_init = classfn \ + } +static const TypeInfo riscv_cpu_type_infos[] = { { .name =TYPE_RISCV_CPU ,@@ -1790,23 +1826,27 @@ static const TypeInfo riscv_cpu_type_infos[] = { .class_size = sizeof(RISCVCPUClass), .class_init = riscv_cpu_class_init, }, - DEFINE_CPU(TYPE_RISCV_CPU_ANY, riscv_any_cpu_init), + DEFINE_CPU_WITH_CLASSFN(TYPE_RISCV_CPU_ANY, riscv_any_cpu_init, + riscv_any_cpu_class_init),#if defined(CONFIG_KVM ) DEFINE_CPU(TYPE_RISCV_CPU_HOST, riscv_host_cpu_init),#endif #if defined(TARGET_RISCV32 ) - DEFINE_CPU(TYPE_RISCV_CPU_BASE32, rv32_base_cpu_init), + DEFINE_CPU_WITH_CLASSFN(TYPE_RISCV_CPU_BASE32, rv32_base_cpu_init, + rv32_base_cpu_class_init), DEFINE_CPU(TYPE_RISCV_CPU_IBEX, rv32_ibex_cpu_init), DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E31, rv32_sifive_e_cpu_init), DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E34, rv32_imafcu_nommu_cpu_init), DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_U34, rv32_sifive_u_cpu_init), #elif defined(TARGET_RISCV64) -DEFINE_CPU (TYPE_RISCV_CPU_BASE64 , rv64_base_cpu_init), +DEFINE_CPU_WITH_CLASSFN (TYPE_RISCV_CPU_BASE64 , rv64_base_cpu_init, + rv64_base_cpu_class_init), +DEFINE_CPU_WITH_CLASSFN (TYPE_RISCV_CPU_BASE128 , rv128_base_cpu_init, + rv128_base_cpu_class_init),DEFINE_CPU (TYPE_RISCV_CPU_SIFIVE_E51 , rv64_sifive_e_cpu_init),DEFINE_CPU (TYPE_RISCV_CPU_SIFIVE_U54 , rv64_sifive_u_cpu_init),DEFINE_CPU (TYPE_RISCV_CPU_SHAKTI_C , rv64_sifive_u_cpu_init), DEFINE_CPU(TYPE_RISCV_CPU_THEAD_C906, rv64_thead_c906_cpu_init), - DEFINE_CPU(TYPE_RISCV_CPU_BASE128, rv128_base_cpu_init), #endif };