On Sun, Apr 6, 2025 at 5:03 PM Paolo Bonzini <pbonzini@redhat.com> wrote:
>
> Allow using RISCVCPUDef to replicate all the logic of custom .instance_init
> functions. To simulate inheritance, merge the child's RISCVCPUDef with
> the parent and then finally move it to the CPUState at the end of
> TYPE_RISCV_CPU's own instance_init function.
>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Alistair
> ---
> target/riscv/cpu.h | 4 ++++
> target/riscv/cpu.c | 42 +++++++++++++++++++++++++++++++++++++-
> target/riscv/kvm/kvm-cpu.c | 6 ++++++
> 3 files changed, 51 insertions(+), 1 deletion(-)
>
> diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h
> index 9bbfdcf6758..acaa49b979c 100644
> --- a/target/riscv/cpu.h
> +++ b/target/riscv/cpu.h
> @@ -543,6 +543,10 @@ struct ArchCPU {
>
> typedef struct RISCVCPUDef {
> RISCVMXL misa_mxl_max; /* max mxl for this cpu */
> + uint32_t misa_ext;
> + int priv_spec;
> + int32_t vext_spec;
> + RISCVCPUConfig cfg;
> } RISCVCPUDef;
>
> /**
> diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c
> index 2c2a6a4b44a..620641fbed6 100644
> --- a/target/riscv/cpu.c
> +++ b/target/riscv/cpu.c
> @@ -74,6 +74,13 @@ bool riscv_cpu_option_set(const char *optname)
> return g_hash_table_contains(general_user_opts, optname);
> }
>
> +static void riscv_cpu_cfg_merge(RISCVCPUConfig *dest, const RISCVCPUConfig *src)
> +{
> +#define BOOL_FIELD(x) dest->x |= src->x;
> +#define TYPED_FIELD(type, x, default_) if (src->x != default_) dest->x = src->x;
> +#include "cpu_cfg_fields.h.inc"
> +}
> +
> #define ISA_EXT_DATA_ENTRY(_name, _min_ver, _prop) \
> {#_name, _min_ver, CPU_CFG_OFFSET(_prop)}
>
> @@ -435,7 +442,7 @@ const char *satp_mode_str(uint8_t satp_mode, bool is_32_bit)
> }
>
> static void set_satp_mode_max_supported(RISCVCPU *cpu,
> - uint8_t satp_mode)
> + int satp_mode)
> {
> bool rv32 = riscv_cpu_mxl(&cpu->env) == MXL_RV32;
> const bool *valid_vm = rv32 ? valid_vm_1_10_32 : valid_vm_1_10_64;
> @@ -1487,6 +1494,16 @@ static void riscv_cpu_init(Object *obj)
> cpu->cfg.cboz_blocksize = 64;
> cpu->env.vext_ver = VEXT_VERSION_1_00_0;
> cpu->cfg.max_satp_mode = -1;
> +
> + env->misa_ext_mask = env->misa_ext = mcc->def->misa_ext;
> + riscv_cpu_cfg_merge(&cpu->cfg, &mcc->def->cfg);
> +
> + if (mcc->def->priv_spec != RISCV_PROFILE_ATTR_UNUSED) {
> + cpu->env.priv_ver = mcc->def->priv_spec;
> + }
> + if (mcc->def->vext_spec != RISCV_PROFILE_ATTR_UNUSED) {
> + cpu->env.vext_ver = mcc->def->vext_spec;
> + }
> }
>
> static void riscv_bare_cpu_init(Object *obj)
> @@ -3093,6 +3110,17 @@ static void riscv_cpu_class_base_init(ObjectClass *c, void *data)
> assert(def->misa_mxl_max <= MXL_RV128);
> mcc->def->misa_mxl_max = def->misa_mxl_max;
> }
> + if (def->priv_spec != RISCV_PROFILE_ATTR_UNUSED) {
> + assert(def->priv_spec <= PRIV_VERSION_LATEST);
> + mcc->def->priv_spec = def->priv_spec;
> + }
> + if (def->vext_spec != RISCV_PROFILE_ATTR_UNUSED) {
> + assert(def->vext_spec != 0);
> + mcc->def->vext_spec = def->vext_spec;
> + }
> + mcc->def->misa_ext |= def->misa_ext;
> +
> + riscv_cpu_cfg_merge(&mcc->def->cfg, &def->cfg);
> }
>
> if (!object_class_is_abstract(c)) {
> @@ -3199,6 +3227,9 @@ void riscv_isa_write_fdt(RISCVCPU *cpu, void *fdt, char *nodename)
> .instance_init = (initfn), \
> .class_data = (void*) &((const RISCVCPUDef) { \
> .misa_mxl_max = (misa_mxl_max_), \
> + .priv_spec = RISCV_PROFILE_ATTR_UNUSED, \
> + .vext_spec = RISCV_PROFILE_ATTR_UNUSED, \
> + .cfg.max_satp_mode = -1, \
> }), \
> }
>
> @@ -3209,6 +3240,9 @@ void riscv_isa_write_fdt(RISCVCPU *cpu, void *fdt, char *nodename)
> .instance_init = (initfn), \
> .class_data = (void*) &((const RISCVCPUDef) { \
> .misa_mxl_max = (misa_mxl_max_), \
> + .priv_spec = RISCV_PROFILE_ATTR_UNUSED, \
> + .vext_spec = RISCV_PROFILE_ATTR_UNUSED, \
> + .cfg.max_satp_mode = -1, \
> }), \
> }
>
> @@ -3219,6 +3253,9 @@ void riscv_isa_write_fdt(RISCVCPU *cpu, void *fdt, char *nodename)
> .instance_init = (initfn), \
> .class_data = (void*) &((const RISCVCPUDef) { \
> .misa_mxl_max = (misa_mxl_max_), \
> + .priv_spec = RISCV_PROFILE_ATTR_UNUSED, \
> + .vext_spec = RISCV_PROFILE_ATTR_UNUSED, \
> + .cfg.max_satp_mode = -1, \
> }), \
> }
>
> @@ -3229,6 +3266,9 @@ void riscv_isa_write_fdt(RISCVCPU *cpu, void *fdt, char *nodename)
> .instance_init = (initfn), \
> .class_data = (void*) &((const RISCVCPUDef) { \
> .misa_mxl_max = (misa_mxl_max_), \
> + .priv_spec = RISCV_PROFILE_ATTR_UNUSED, \
> + .vext_spec = RISCV_PROFILE_ATTR_UNUSED, \
> + .cfg.max_satp_mode = -1, \
> }), \
> }
>
> diff --git a/target/riscv/kvm/kvm-cpu.c b/target/riscv/kvm/kvm-cpu.c
> index d7e6970a670..02bcb72cbb3 100644
> --- a/target/riscv/kvm/kvm-cpu.c
> +++ b/target/riscv/kvm/kvm-cpu.c
> @@ -2004,10 +2004,16 @@ static const TypeInfo riscv_kvm_cpu_type_infos[] = {
> #if defined(TARGET_RISCV32)
> .class_data = &((const RISCVCPUDef) {
> .misa_mxl_max = MXL_RV32,
> + .priv_spec = RISCV_PROFILE_ATTR_UNUSED,
> + .vext_spec = RISCV_PROFILE_ATTR_UNUSED,
> + .cfg.max_satp_mode = -1,
> },
> #elif defined(TARGET_RISCV64)
> .class_data = &((const RISCVCPUDef) {
> .misa_mxl_max = MXL_RV64,
> + .priv_spec = RISCV_PROFILE_ATTR_UNUSED,
> + .vext_spec = RISCV_PROFILE_ATTR_UNUSED,
> + .cfg.max_satp_mode = -1,
> },
> #endif
> }
> --
> 2.49.0
>