When the setter is called, the final vcpu is not created yet. Applying the override on vcpu.isar initialized with the max scratch vcpu would not be correct because the final vcpu may have some features turned off (for instance virtualization) while the scratch vcpu features all available ones. So we need to postpone the actual override until the final vcpu has been initialized.
Set values are recorded in a hash table and will be applied to the guest in a subsequent patch. At this point the only checks that can be done on the input value are checking against field length and arch enum value if any. With regards to the getter, if the sysreg prop was set, we return the set value, otherwise, we return the value in cpu->isar.idregs[]. Signed-off-by: Eric Auger <[email protected]> Reviewed-by: Khushit Shah <[email protected]> --- v9 -> v10: - renamed MAKE_IDREG_KEY into IDREG_FIELD_KEY - rebase on new implementation in cpu64.c --- target/arm/cpu.h | 2 ++ target/arm/cpu.c | 5 ++-- target/arm/cpu64.c | 65 ++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/target/arm/cpu.h b/target/arm/cpu.h index 639be0713e..bef100c02c 100644 --- a/target/arm/cpu.h +++ b/target/arm/cpu.h @@ -968,6 +968,8 @@ struct ArchCPU { uint64_t *cpreg_vmstate_values; int32_t cpreg_vmstate_array_len; + GHashTable *sysreg_props; + DynamicGDBFeatureInfo dyn_sysreg_feature; DynamicGDBFeatureInfo dyn_svereg_feature; DynamicGDBFeatureInfo dyn_smereg_feature; diff --git a/target/arm/cpu.c b/target/arm/cpu.c index bee74c0f94..d2755d77ca 100644 --- a/target/arm/cpu.c +++ b/target/arm/cpu.c @@ -1238,8 +1238,8 @@ static void arm_cpu_initfn(Object *obj) { ARMCPU *cpu = ARM_CPU(obj); - cpu->cp_regs = g_hash_table_new_full(g_direct_hash, g_direct_equal, - NULL, g_free); + cpu->cp_regs = g_hash_table_new(g_direct_hash, g_direct_equal); + cpu->sysreg_props = g_hash_table_new(g_direct_hash, g_direct_equal); QLIST_INIT(&cpu->pre_el_change_hooks); QLIST_INIT(&cpu->el_change_hooks); @@ -1739,6 +1739,7 @@ static void arm_cpu_finalizefn(Object *obj) ARMCPRegMigTolerance *t, *n; g_hash_table_destroy(cpu->cp_regs); + g_hash_table_destroy(cpu->sysreg_props); QLIST_FOREACH_SAFE(hook, &cpu->pre_el_change_hooks, node, next) { QLIST_REMOVE(hook, node); diff --git a/target/arm/cpu64.c b/target/arm/cpu64.c index b1738c9be3..9662608675 100644 --- a/target/arm/cpu64.c +++ b/target/arm/cpu64.c @@ -801,18 +801,79 @@ static void kvm_arm_set_cpreg_mig_tolerances(ARMCPU *cpu) UINT64_MAX, 0, ToleranceOnlySrcTestValue); } +#define IDREG_FIELD_KEY(reg_idx, field_shift) \ + (((uint64_t)(reg_idx) << 8) | ((uint64_t)(field_shift) & 0xFF)) + static void set_sysreg_prop(Object *obj, Visitor *v, const char *name, void *opaque, Error **errp) { - error_setg(errp, "%s setter not yet implemented", name); + ARM64SysRegField *field = (ARM64SysRegField *)opaque; + ARMCPU *cpu = ARM_CPU(obj); + int index = field->index; + int lower = field->shift; + int length = field->length; + uint64_t key, value; + + if (!visit_type_uint64(v, name, &value, errp)) { + return; + } + + if (length < 64 && value > ((1ULL << length) - 1)) { + error_setg(errp, + "idreg %s set value (0x%" PRIx64 ") exceeds length of " + "field (%d)!", name, value, length); + return; + } + + if (field->arch_vals) { + /* this field has some enum values */ + for (int i = 0; i < field->arch_vals_count; i++) { + if (value == field->arch_vals[i]) { + goto valid; + } + } + error_setg(errp, + "idreg %s set value (0x%" PRIx64 ") does not match any " + "arch valid enum value", name, value); + return; + } + +valid: + + key = IDREG_FIELD_KEY(index, lower); + g_hash_table_insert(cpu->sysreg_props, GUINT_TO_POINTER(key), + GUINT_TO_POINTER(value)); } static void get_sysreg_prop(Object *obj, Visitor *v, const char *name, void *opaque, Error **errp) { - error_setg(errp, "%s getter not yet implemented", name); + ARM64SysRegField *field = (ARM64SysRegField *)opaque; + ARMCPU *cpu = ARM_CPU(obj); + int index = field->index; + int lower = field->shift; + int length = field->length; + gpointer value_ptr; + uint64_t value; + uint64_t key = IDREG_FIELD_KEY(index, lower); + gboolean exists; + + exists = g_hash_table_lookup_extended(cpu->sysreg_props, + GUINT_TO_POINTER(key), NULL, + &value_ptr); + if (exists) { + value = GPOINTER_TO_UINT(value_ptr); + } else { + uint64_t mask = MAKE_64BIT_MASK(lower, length); + uint64_t *idregs = cpu->isar.idregs; + + value = (idregs[index] & mask) >> lower; + } + + visit_type_uint64(v, name, &value, errp); + trace_get_sysreg_prop(name, value); } /** -- 2.53.0
