Hi Eric,

On Thu, Oct 29, 2026 at 10:20:20AM +0100, Eric Auger wrote:
> Factorize the code that initializes the kvm init features from the
> cpu settings in a helper. Use it kvm_arch_init_vcpu(). Soon it will
> be used by another function.
> 
> The only change in the code relates to the check of kvm_state. The
> future caller won't have the VM created when calling the helper and
> for this caller it is OK ignoring PSCI version setting.
> 
> Signed-off-by: Eric Auger <[email protected]>
> ---
>  target/arm/kvm.c | 50 ++++++++++++++++++++++++++++--------------------
>  1 file changed, 29 insertions(+), 21 deletions(-)
> 
> diff --git a/target/arm/kvm.c b/target/arm/kvm.c
> index 2d37c2bfb4..1c5be9ecf4 100644
> --- a/target/arm/kvm.c
> +++ b/target/arm/kvm.c
> @@ -2015,34 +2015,18 @@ int kvm_arch_pre_create_vcpu(CPUState *cpu, Error 
> **errp)
>      return 0;
>  }
>  
> -int kvm_arch_init_vcpu(CPUState *cs, Error **errp)
> +/* Determine init features for this CPU */
> +static void kvm_arm_vcpu_prepare_init_features(ARMCPU *cpu)
>  {
> -    int ret;
> -    uint64_t mpidr;
> -    ARMCPU *cpu = ARM_CPU(cs);
> +    CPUState *cs = CPU(cpu);
>      CPUARMState *env = &cpu->env;
> -    uint64_t psciver;
>  
> -    if (cpu->kvm_target == QEMU_KVM_ARM_TARGET_NONE) {
> -        error_setg(errp, "KVM is not supported for this guest CPU type");
> -        return -EINVAL;
> -    }
> -
> -    if (cpu->kvm_target == QEMU_KVM_ARM_TARGET_CORTEX_A53 ||
> -        cpu->kvm_target == QEMU_KVM_ARM_TARGET_CORTEX_A57) {
> -        warn_report_once("Using the cortex-a53 and cortex-a57 CPU types "
> -                         "with KVM is deprecated. Use the max or host CPU "
> -                         "types instead.");
> -    }
> -
> -    qemu_add_vm_change_state_handler(kvm_arm_vm_state_change, cpu);
> -
> -    /* Determine init features for this CPU */
>      memset(cpu->kvm_init_features, 0, sizeof(cpu->kvm_init_features));
> +
>      if (cs->start_powered_off) {
>          cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_POWER_OFF;
>      }
> -    if (cpu->psci_version != QEMU_PSCI_VERSION_0_1 &&
> +    if (cpu->psci_version != QEMU_PSCI_VERSION_0_1 && cs->kvm_state &&
>          kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_PSCI_0_2)) {
>          /*
>           * Versions >= v0.2 are backward compatible with v0.2
> @@ -2066,6 +2050,30 @@ int kvm_arch_init_vcpu(CPUState *cs, Error **errp)
>      if (cpu->has_el2 && kvm_arm_el2_supported()) {
>          cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_HAS_EL2;
>      }
> +}
> +
> +int kvm_arch_init_vcpu(CPUState *cs, Error **errp)
> +{
> +    int ret;
> +    uint64_t mpidr;
> +    ARMCPU *cpu = ARM_CPU(cs);
> +    uint64_t psciver;
> +
> +    if (cpu->kvm_target == QEMU_KVM_ARM_TARGET_NONE) {
> +        error_setg(errp, "KVM is not supported for this guest CPU type");
> +        return -EINVAL;
> +    }
> +
> +    if (cpu->kvm_target == QEMU_KVM_ARM_TARGET_CORTEX_A53 ||
> +        cpu->kvm_target == QEMU_KVM_ARM_TARGET_CORTEX_A57) {
> +        warn_report_once("Using the cortex-a53 and cortex-a57 CPU types "
> +                         "with KVM is deprecated. Use the max or host CPU "
> +                         "types instead.");
> +    }
> +
> +    qemu_add_vm_change_state_handler(kvm_arm_vm_state_change, cpu);
> +
> +    kvm_arm_vcpu_prepare_init_features(cpu);
>  
>      /* Do KVM_ARM_VCPU_INIT ioctl */
>      ret = kvm_arm_vcpu_init(cpu);
> -- 
> 2.53.0
> 
> 

Reviewed-by: Gustavo Romero <[email protected]>


Cheers,
Gustavo


Reply via email to