Re: [PATCH v9 03/20] KVM: x86: Support EGPRs on VCPU RESET/INIT events

From: Jim Mattson

Date: Tue Nov 17 2026 - 23:44:36 EST


On Tue, Nov 17, 2026 at 10:31 AM Chang S. Bae <chang.seok.bae@xxxxxxxxx> wrote:
>
> EGPRs are not zeroed on CPU INIT. Handle each register based on index to
> support this EGPR behavior while preserving the existing behavior for
> legacy GPRs.
>
> Refactor the rest of GPR handling together as the reset comment mentions it.
>
> Signed-off-by: Chang S. Bae <chang.seok.bae@xxxxxxxxx>
> ---
> V8 -> V9: New patch (Jim Mattson)
> ---
> arch/x86/kvm/x86.c | 40 ++++++++++++++++++++++++++--------------
> 1 file changed, 26 insertions(+), 14 deletions(-)
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 2cd9ee316aad..73bb1d25931c 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -9601,9 +9601,33 @@ static void kvm_xstate_reset(struct kvm_vcpu *vcpu, bool init_event)
> kvm_load_guest_fpu(vcpu);
> }
>
> -void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
> +static void kvm_vcpu_regs_reset(struct kvm_vcpu *vcpu, bool init_event)
> {
> struct kvm_cpuid_entry2 *cpuid_0x1;
> + int idx;
> +
> + /*
> + * All legacy GPRs except RDX (handled below) are zeroed on RESET/INIT.
> + * EGPRs (R16-R31), however, retain their values on INIT.
> + */
> + for (idx = VCPU_REGS_RAX; idx < NR_VCPU_GENERAL_PURPOSE_REGS; idx++)
> + vcpu->arch.regs[idx] = idx > 15 && init_event ? vcpu->arch.regs[idx] : 0;

Nit: I'd rather not see a bare 15 here, though I realize that
VCPU_REGS_R16 doesn't exist when CONFIG_KVM_APX=n. Perhaps define the
number of legacy GPRs in kvm_host.h:

#ifdef CONFIG_X86_64
#define NR_VCPU_LEGACY_GPRS (VCPU_REGS_R15 + 1)
#else
#define NR_VCPU_LEGACY_GPRS (VCPU_REGS_RDI + 1)
#endif

and then this becomes:

/*
* All legacy GPRs except RDX (handled below) are zeroed on
* RESET/INIT. EGPRs (R16-R31) are zeroed on RESET, but are
* unchanged on INIT.
*/
memset(vcpu->arch.regs, 0,
init_event ? NR_VCPU_LEGACY_GPRS * sizeof(vcpu->arch.regs[0]) :
sizeof(vcpu->arch.regs));

With that change,

Reviewed-by: Jim Mattson <jmattson@xxxxxxxxxx>