target/riscv: implement query-cpu-definitions
Subsystems (2)
v2 cover target/riscv: implement query-cpu-definitions
Hi,
This v2 contains a change suggested by Weiwei Li in patch 4. No functional changes made from the previous version.
Changes from v1:
- patch 4:
- use a common class_init() fn instead of one class fn per generic CPU
- v1 link: https://lists.gnu.org/archive/html/qemu-devel/2023-04/msg01266.html
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 | 33 ++++++++++++++-- target/riscv/cpu.h | 46 +--------------------- target/riscv/meson.build | 3 +- target/riscv/riscv-qmp-cmds.c | 55 ++++++++++++++++++++++++++ 6 files changed, 164 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 v2 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 v2 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 v2 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 v2 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 | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-)
target/riscv/cpu.c +23 -4
@@ -340,6 +340,13 @@ static void set_satp_mode_default_map(RISCVCPU *cpu) }#endif +static void riscv_generic_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;
@@ -1779,6 +1786,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 +1805,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_generic_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, + riscv_generic_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, + riscv_generic_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), +DEFINE_CPU_WITH_CLASSFN (TYPE_RISCV_CPU_BASE128 , rv128_base_cpu_init, + riscv_generic_cpu_class_init), #endif };
Signature · 1 line
2.39.2
Re: [PATCH v2 1/4] target/riscv: add CPU QOM header
In reply to Daniel Henrique BarbozaQuoted 10 lines · Daniel Henrique Barboza
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.hheader with the definitions ofTYPE_RISCV_CPUand RISCVCPUClass declarations. These were moved fromcpu.hto the new file, andcpu.hnow includes "cpu-qom.h".
Quoted 6 lines
Signed-off-by Daniel Henrique Barboza<[email protected]>--- 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
r~
Re: [PATCH v2 2/4] target/riscv: add query-cpy-definitions support
In reply to Daniel Henrique BarbozaQuoted 24 lines · Daniel Henrique Barboza
This 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.
Quoted 7 lines
Signed-off-by Daniel Henrique Barboza<[email protected]>--- 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
r~
Re: [PATCH v2 3/4] target/riscv: add 'static' attribute of query-cpu-definitions
In reply to Daniel Henrique BarbozaQuoted 13 lines · 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.
Quoted 6 lines
Signed-off-by Daniel Henrique Barboza<[email protected]>--- target/riscv/cpu-qom.h | 3 +++ target/riscv/cpu.c | 6 ++++++ target/riscv/riscv-qmp-cmds.c | 2 ++ 3 files changed, 11 insertions(+)
Is 'static = true' really what you want as default?
Perhaps 'dynamic = false' (considering zero initialization) would be better? Do you want an attribute that can be changed at all?
You could plausibly implement this via class inheritance instead. E.g.
static const TypeInfo dynamic_cpu_type_info = {
.name = TYPE_RISCV_DYN_CPU,
.parent = TYPE_RISCV_CPU,
.abstract = true,
...
};
and then the dynamic cpus inherit from that. Your dynamic attribute becomes object_dynamic_cast(OBJECT(cpu), TYPE_RISCV_DYN_CPU) != NULL.
r~
Re: [PATCH v2 3/4] target/riscv: add 'static' attribute of query-cpu-definitions
Quoted 21 lines · Richard Henderson
Quoted 13 lines · 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.
Quoted 6 lines
Signed-off-by Daniel Henrique Barboza<[email protected]>---
target/riscv/cpu-qom.h | 3 +++
target/riscv/cpu.c | 6 ++++++
target/riscv/riscv-qmp-cmds.c| 2 ++3 files changed, 11 insertions(+)
Quoted 3 lines
Is 'static = true' really what you want as default?
Perhaps 'dynamic = false' (considering zero initialization) would be better? Do you want an attribute that can be changed at all?
I don't think there's a precedence in QEMU of a CPU that starts as static and then becomes non-static during runtime. If the CPU has the capability of changing its attributes/extensions during runtime then I'd call it non-static all the time.
Quoted 4 lines
You could plausibly implement this via class inheritance instead. E.g.
Quoted 8 lines
static const TypeInfo
dynamic_cpu_type_info= {
.name =TYPE_RISCV_DYN_CPU,
.parent =TYPE_RISCV_CPU,
.abstract = true,
...
};and then the dynamic cpus inherit from that. Your dynamic attribute becomes
object_dynamic_cast(OBJECT(cpu), TYPE_RISCV_DYN_CPU)!= NULL.
Sounds good. We'll avoid adding an extra flag in RISCVCPUClass too.
Thanks,
Daniel
Quoted 3 lines
r~