From nobody Sun Feb 8 02:56:30 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 733A0328B67; Mon, 10 Nov 2025 18:24:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799076; cv=none; b=i5ME0cDFGADv5jEmKduXVGW/+5IFii6RryAxp3OQUV2W4Y5S8gl1JS4S1OS6EmQuA3qZQZN+C7uRxZbFiuo0fd9kXh1+CwhisA6RNrE7vaEFIwgzn0m854t8A22M7FDJbGXBAehyUgZGBFMrpQwgixCEsF6B3SJ21+8jQYn8lsc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799076; c=relaxed/simple; bh=qKfymT4CzUPc+oFKR+qBDa5+aKxK1tf6clEXGCz4zPs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=h/81kPQALNt5tVezlqcP2tOtQUW9Oadap3xhDo/rYa0sB4zX3UVGtxLAxQR3dZZU4dqXlOacRETu8qbb/rN6UAGn6CpnvGM2B/0/rn2wUbaxaYLiq0iM+55s/5hwewysTVD2wpX8AjZcIHQGnLK5BCHE4fEB5rPFuNjej2/IBbI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=DAxqdRoa; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="DAxqdRoa" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1762799075; x=1794335075; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qKfymT4CzUPc+oFKR+qBDa5+aKxK1tf6clEXGCz4zPs=; b=DAxqdRoaKyQ/B8ui6Earw7J3wBMvtsuNljtQiO+GfQVOe7fsm0XPxx9Q LHweal+6WqMCTtn/np1SIpi9iagfGCxmHu5IWEoMmCJCTVHtHOtYSUExc AENkA9EOqrMruRjp7KySzGr9RJOgcAjiaFZBAIrXIw1paUHRRPCl5u75C Z1SUpXOjMkdyjKFDCIg8/0TYYu/uGdoq3eGlfQ/S9H84OECMRHlKMdUSO u5U0BkRbcJD0t/2OBusLfGt3lCM4SA1KS1yPA3raisnWfHN1rd+BYFi0/ v6GfreeBDuuDR8gQ03LMyWY4D9GoLBvrD+R9ZLMEZ5+KJLHYisjJT1QCR Q==; X-CSE-ConnectionGUID: Z/TNvu1XTfqELgWGimmxEQ== X-CSE-MsgGUID: VtUAwOomQh+98V9KLvvThQ== X-IronPort-AV: E=McAfee;i="6800,10657,11609"; a="76305479" X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="76305479" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2025 10:24:34 -0800 X-CSE-ConnectionGUID: GIKrYYquRKKm5am0rThq7A== X-CSE-MsgGUID: TugSciuUSACFpTB1LJKSNQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="219396031" Received: from chang-linux-3.sc.intel.com (HELO chang-linux-3) ([172.25.66.172]) by orviesa002.jf.intel.com with ESMTP; 10 Nov 2025 10:24:35 -0800 From: "Chang S. Bae" To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, chao.gao@intel.com, zhao1.liu@intel.com, chang.seok.bae@intel.com Subject: [PATCH RFC v1 01/20] KVM: x86: Rename register accessors to be GPR-specific Date: Mon, 10 Nov 2025 18:01:12 +0000 Message-ID: <20251110180131.28264-2-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251110180131.28264-1-chang.seok.bae@intel.com> References: <20251110180131.28264-1-chang.seok.bae@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Refactor the VCPU register state accessors to make them explicitly GPR-only. The existing register accessors operate on the cached VCPU register state. That cache holds GPRs and RIP. RIP has its own interface already. This renaming clarifies GPR access only. No functional changes intended. Signed-off-by: Chang S. Bae --- arch/x86/kvm/svm/svm.c | 8 ++++---- arch/x86/kvm/vmx/nested.c | 20 ++++++++++---------- arch/x86/kvm/vmx/vmx.c | 12 ++++++------ arch/x86/kvm/x86.c | 10 +++++----- arch/x86/kvm/x86.h | 5 ++--- arch/x86/kvm/xen.c | 2 +- 6 files changed, 28 insertions(+), 29 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 153c12dbf3eb..3aa2c37754ef 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -2473,7 +2473,7 @@ static int cr_interception(struct kvm_vcpu *vcpu) err =3D 0; if (cr >=3D 16) { /* mov to cr */ cr -=3D 16; - val =3D kvm_register_read(vcpu, reg); + val =3D kvm_gpr_read(vcpu, reg); trace_kvm_cr_write(cr, val); switch (cr) { case 0: @@ -2519,7 +2519,7 @@ static int cr_interception(struct kvm_vcpu *vcpu) kvm_queue_exception(vcpu, UD_VECTOR); return 1; } - kvm_register_write(vcpu, reg, val); + kvm_gpr_write(vcpu, reg, val); trace_kvm_cr_read(cr, val); } return kvm_complete_insn_gp(vcpu, err); @@ -2591,9 +2591,9 @@ static int dr_interception(struct kvm_vcpu *vcpu) dr =3D svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0; if (dr >=3D 16) { /* mov to DRn */ dr -=3D 16; - err =3D kvm_set_dr(vcpu, dr, kvm_register_read(vcpu, reg)); + err =3D kvm_set_dr(vcpu, dr, kvm_gpr_read(vcpu, reg)); } else { - kvm_register_write(vcpu, reg, kvm_get_dr(vcpu, dr)); + kvm_gpr_write(vcpu, reg, kvm_get_dr(vcpu, dr)); } =20 return kvm_complete_insn_gp(vcpu, err); diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 76271962cb70..47a941989787 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -5325,9 +5325,9 @@ int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsign= ed long exit_qualification, else if (addr_size =3D=3D 0) off =3D (gva_t)sign_extend64(off, 15); if (base_is_valid) - off +=3D kvm_register_read(vcpu, base_reg); + off +=3D kvm_gpr_read(vcpu, base_reg); if (index_is_valid) - off +=3D kvm_register_read(vcpu, index_reg) << scaling; + off +=3D kvm_gpr_read(vcpu, index_reg) << scaling; vmx_get_segment(vcpu, &s, seg_reg); =20 /* @@ -5719,7 +5719,7 @@ static int handle_vmread(struct kvm_vcpu *vcpu) return 1; =20 /* Decode instruction info and find the field to read */ - field =3D kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf)); + field =3D kvm_gpr_read(vcpu, (((instr_info) >> 28) & 0xf)); =20 if (!nested_vmx_is_evmptr12_valid(vmx)) { /* @@ -5768,7 +5768,7 @@ static int handle_vmread(struct kvm_vcpu *vcpu) * on the guest's mode (32 or 64 bit), not on the given field's length. */ if (instr_info & BIT(10)) { - kvm_register_write(vcpu, (((instr_info) >> 3) & 0xf), value); + kvm_gpr_write(vcpu, (((instr_info) >> 3) & 0xf), value); } else { len =3D is_64_bit_mode(vcpu) ? 8 : 4; if (get_vmx_mem_address(vcpu, exit_qualification, @@ -5842,7 +5842,7 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu) return nested_vmx_failInvalid(vcpu); =20 if (instr_info & BIT(10)) - value =3D kvm_register_read(vcpu, (((instr_info) >> 3) & 0xf)); + value =3D kvm_gpr_read(vcpu, (((instr_info) >> 3) & 0xf)); else { len =3D is_64_bit_mode(vcpu) ? 8 : 4; if (get_vmx_mem_address(vcpu, exit_qualification, @@ -5853,7 +5853,7 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu) return kvm_handle_memory_failure(vcpu, r, &e); } =20 - field =3D kvm_register_read(vcpu, (((instr_info) >> 28) & 0xf)); + field =3D kvm_gpr_read(vcpu, (((instr_info) >> 28) & 0xf)); =20 offset =3D get_vmcs12_field_offset(field); if (offset < 0) @@ -6051,7 +6051,7 @@ static int handle_invept(struct kvm_vcpu *vcpu) =20 vmx_instruction_info =3D vmcs_read32(VMX_INSTRUCTION_INFO); gpr_index =3D vmx_get_instr_info_reg2(vmx_instruction_info); - type =3D kvm_register_read(vcpu, gpr_index); + type =3D kvm_gpr_read(vcpu, gpr_index); =20 types =3D (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; =20 @@ -6132,7 +6132,7 @@ static int handle_invvpid(struct kvm_vcpu *vcpu) =20 vmx_instruction_info =3D vmcs_read32(VMX_INSTRUCTION_INFO); gpr_index =3D vmx_get_instr_info_reg2(vmx_instruction_info); - type =3D kvm_register_read(vcpu, gpr_index); + type =3D kvm_gpr_read(vcpu, gpr_index); =20 types =3D (vmx->nested.msrs.vpid_caps & VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; @@ -6406,7 +6406,7 @@ static bool nested_vmx_exit_handled_cr(struct kvm_vcp= u *vcpu, switch ((exit_qualification >> 4) & 3) { case 0: /* mov to cr */ reg =3D (exit_qualification >> 8) & 15; - val =3D kvm_register_read(vcpu, reg); + val =3D kvm_gpr_read(vcpu, reg); switch (cr) { case 0: if (vmcs12->cr0_guest_host_mask & @@ -6492,7 +6492,7 @@ static bool nested_vmx_exit_handled_vmcs_access(struc= t kvm_vcpu *vcpu, =20 /* Decode instruction info and find the field to access */ vmx_instruction_info =3D vmcs_read32(VMX_INSTRUCTION_INFO); - field =3D kvm_register_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); + field =3D kvm_gpr_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); =20 /* Out-of-range fields always cause a VM exit from L2 to L1 */ if (field >> 15) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index f87c216d976d..c7d38f7692cf 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5462,7 +5462,7 @@ static int handle_cr(struct kvm_vcpu *vcpu) reg =3D (exit_qualification >> 8) & 15; switch ((exit_qualification >> 4) & 3) { case 0: /* mov to cr */ - val =3D kvm_register_read(vcpu, reg); + val =3D kvm_gpr_read(vcpu, reg); trace_kvm_cr_write(cr, val); switch (cr) { case 0: @@ -5504,12 +5504,12 @@ static int handle_cr(struct kvm_vcpu *vcpu) WARN_ON_ONCE(enable_unrestricted_guest); =20 val =3D kvm_read_cr3(vcpu); - kvm_register_write(vcpu, reg, val); + kvm_gpr_write(vcpu, reg, val); trace_kvm_cr_read(cr, val); return kvm_skip_emulated_instruction(vcpu); case 8: val =3D kvm_get_cr8(vcpu); - kvm_register_write(vcpu, reg, val); + kvm_gpr_write(vcpu, reg, val); trace_kvm_cr_read(cr, val); return kvm_skip_emulated_instruction(vcpu); } @@ -5579,10 +5579,10 @@ static int handle_dr(struct kvm_vcpu *vcpu) =20 reg =3D DEBUG_REG_ACCESS_REG(exit_qualification); if (exit_qualification & TYPE_MOV_FROM_DR) { - kvm_register_write(vcpu, reg, kvm_get_dr(vcpu, dr)); + kvm_gpr_write(vcpu, reg, kvm_get_dr(vcpu, dr)); err =3D 0; } else { - err =3D kvm_set_dr(vcpu, dr, kvm_register_read(vcpu, reg)); + err =3D kvm_set_dr(vcpu, dr, kvm_gpr_read(vcpu, reg)); } =20 out: @@ -5941,7 +5941,7 @@ static int handle_invpcid(struct kvm_vcpu *vcpu) =20 vmx_instruction_info =3D vmcs_read32(VMX_INSTRUCTION_INFO); gpr_index =3D vmx_get_instr_info_reg2(vmx_instruction_info); - type =3D kvm_register_read(vcpu, gpr_index); + type =3D kvm_gpr_read(vcpu, gpr_index); =20 /* According to the Intel instruction reference, the memory operand * is read even if it isn't needed (e.g., for type=3D=3Dall) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index b4b5d2d09634..603057ea7421 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2084,8 +2084,8 @@ static int complete_fast_rdmsr(struct kvm_vcpu *vcpu) static int complete_fast_rdmsr_imm(struct kvm_vcpu *vcpu) { if (!vcpu->run->msr.error) - kvm_register_write(vcpu, vcpu->arch.cui_rdmsr_imm_reg, - vcpu->run->msr.data); + kvm_gpr_write(vcpu, vcpu->arch.cui_rdmsr_imm_reg, + vcpu->run->msr.data); =20 return complete_fast_msr_access(vcpu); } @@ -2139,7 +2139,7 @@ static int __kvm_emulate_rdmsr(struct kvm_vcpu *vcpu,= u32 msr, int reg, kvm_rax_write(vcpu, data & -1u); kvm_rdx_write(vcpu, (data >> 32) & -1u); } else { - kvm_register_write(vcpu, reg, data); + kvm_gpr_write(vcpu, reg, data); } } else { /* MSR read failed? See if we should ask user space */ @@ -2197,7 +2197,7 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_emulate_wrmsr); =20 int kvm_emulate_wrmsr_imm(struct kvm_vcpu *vcpu, u32 msr, int reg) { - return __kvm_emulate_wrmsr(vcpu, msr, kvm_register_read(vcpu, reg)); + return __kvm_emulate_wrmsr(vcpu, msr, kvm_gpr_read(vcpu, reg)); } EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_emulate_wrmsr_imm); =20 @@ -2301,7 +2301,7 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(handle_fastpath_wrmsr); =20 fastpath_t handle_fastpath_wrmsr_imm(struct kvm_vcpu *vcpu, u32 msr, int r= eg) { - return __handle_fastpath_wrmsr(vcpu, msr, kvm_register_read(vcpu, reg)); + return __handle_fastpath_wrmsr(vcpu, msr, kvm_gpr_read(vcpu, reg)); } EXPORT_SYMBOL_FOR_KVM_INTERNAL(handle_fastpath_wrmsr_imm); =20 diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index f3dc77f006f9..4edadd64d3d5 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -400,15 +400,14 @@ static inline bool vcpu_match_mmio_gpa(struct kvm_vcp= u *vcpu, gpa_t gpa) return false; } =20 -static inline unsigned long kvm_register_read(struct kvm_vcpu *vcpu, int r= eg) +static inline unsigned long kvm_gpr_read(struct kvm_vcpu *vcpu, int reg) { unsigned long val =3D kvm_register_read_raw(vcpu, reg); =20 return is_64_bit_mode(vcpu) ? val : (u32)val; } =20 -static inline void kvm_register_write(struct kvm_vcpu *vcpu, - int reg, unsigned long val) +static inline void kvm_gpr_write(struct kvm_vcpu *vcpu, int reg, unsigned = long val) { if (!is_64_bit_mode(vcpu)) val =3D (u32)val; diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index d6b2a665b499..c9700dc88bb1 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -1679,7 +1679,7 @@ int kvm_xen_hypercall(struct kvm_vcpu *vcpu) bool handled =3D false; u8 cpl; =20 - input =3D (u64)kvm_register_read(vcpu, VCPU_REGS_RAX); + input =3D (u64)kvm_gpr_read(vcpu, VCPU_REGS_RAX); =20 /* Hyper-V hypercalls get bit 31 set in EAX */ if ((input & 0x80000000) && --=20 2.51.0 From nobody Sun Feb 8 02:56:30 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2F96C32ED46; Mon, 10 Nov 2025 18:24:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799078; cv=none; b=f/MJw15nSPqeuqbdnCKKwGqf0CPdtjK82ciluLDtlbPxWUxIltpHIFe0Thj2i1VSb3IZLD55nhZYKca3MXcGYglPfrL9CEubEZ2AHSfmRFPLVIp2ABzJcrWXv/jm+uqfYIRGv8oXYRxV+M29m9rO6suN94zqTyxEcD/CKOG4g7o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799078; c=relaxed/simple; bh=NefA63Pt7iC0ongl/5CkrpdKXbIXnrDJwvgQ440aYxk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bVVYaMhr/OrpFiNNUmA65awRaNh4KSVUPRLseO6254wqc+gJ94amMdI+S+tqjraAYcgvry/Y90A4oF8t3YrEcerlq4nRHTSMLosAhkOG5RcHewkwm3HktPKMQrKcSf4PKsjLZkOjq/QiXutkeEZJW+uDNGuHMnRKZ1ysN+X/G1I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=nYep8g6h; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="nYep8g6h" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1762799077; x=1794335077; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=NefA63Pt7iC0ongl/5CkrpdKXbIXnrDJwvgQ440aYxk=; b=nYep8g6hYE+ASveaOMWEndlkEC++X2Ded60T7inufE3bBL29wzUAdxkk ztKOlEvqzhecfUzZk+lWh4JweFARnBEJA282omHr1N+LjVStAJhgaOeGy BJTXx3uo49FXgPLYTWeFFnlYbVaiVp4nMkdcUScPBj3+i8BQjgalcw7hn CCxmKxmllwVX0zhbrboUSl7WpQcTOyR0Lgr3cM+bbychRfj6qTNcQBFDl 7q77TG/NNAi6WX7Nt4xRUxBCK5fdl/nzwjx8OREPQDbYqWhznash/FFvQ yDR0hV7uyAzBFi7se2VdJsm1fClr+8zwHHx8v06MjEs6QQ7WlA1T+OaPP w==; X-CSE-ConnectionGUID: TmmsN5o2QQ2LhMjFGchO/Q== X-CSE-MsgGUID: rcsGcnKpSKyDrXSiQDY0QA== X-IronPort-AV: E=McAfee;i="6800,10657,11609"; a="76305481" X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="76305481" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2025 10:24:37 -0800 X-CSE-ConnectionGUID: oQA/fvitRviOmblbQ7Hu9A== X-CSE-MsgGUID: 4WqVmKFZSg6q5DtdP51mEA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="219396040" Received: from chang-linux-3.sc.intel.com (HELO chang-linux-3) ([172.25.66.172]) by orviesa002.jf.intel.com with ESMTP; 10 Nov 2025 10:24:37 -0800 From: "Chang S. Bae" To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, chao.gao@intel.com, zhao1.liu@intel.com, chang.seok.bae@intel.com Subject: [PATCH RFC v1 02/20] KVM: x86: Refactor GPR accessors to differentiate register access types Date: Mon, 10 Nov 2025 18:01:13 +0000 Message-ID: <20251110180131.28264-3-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251110180131.28264-1-chang.seok.bae@intel.com> References: <20251110180131.28264-1-chang.seok.bae@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Refactor the GPR accessors to introduce internal helpers to distinguish between legacy and extended registers. x86 CPUs introduce additional GPRs, but those registers will initially remain unused in the kernel and will not be saved in KVM register cache on every VM exit. Guest states are expected to remain live in hardware registers. This abstraction layer centralizes the selection of access methods, providing a unified interface. For now, the EGPR accessors are placeholders to be implemented later. Signed-off-by: Chang S. Bae --- arch/x86/include/asm/kvm_host.h | 18 ++++++++++++ arch/x86/include/asm/kvm_vcpu_regs.h | 16 ++++++++++ arch/x86/kvm/fpu.h | 6 ++++ arch/x86/kvm/x86.h | 44 ++++++++++++++++++++++++++-- 4 files changed, 82 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 48598d017d6f..940f83c121cf 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -212,6 +212,24 @@ enum { VCPU_SREG_GS, VCPU_SREG_TR, VCPU_SREG_LDTR, +#ifdef CONFIG_X86_64 + VCPU_XREG_R16 =3D __VCPU_XREG_R16, + VCPU_XREG_R17 =3D __VCPU_XREG_R17, + VCPU_XREG_R18 =3D __VCPU_XREG_R18, + VCPU_XREG_R19 =3D __VCPU_XREG_R19, + VCPU_XREG_R20 =3D __VCPU_XREG_R20, + VCPU_XREG_R21 =3D __VCPU_XREG_R21, + VCPU_XREG_R22 =3D __VCPU_XREG_R22, + VCPU_XREG_R23 =3D __VCPU_XREG_R23, + VCPU_XREG_R24 =3D __VCPU_XREG_R24, + VCPU_XREG_R25 =3D __VCPU_XREG_R25, + VCPU_XREG_R26 =3D __VCPU_XREG_R26, + VCPU_XREG_R27 =3D __VCPU_XREG_R27, + VCPU_XREG_R28 =3D __VCPU_XREG_R28, + VCPU_XREG_R29 =3D __VCPU_XREG_R29, + VCPU_XREG_R30 =3D __VCPU_XREG_R30, + VCPU_XREG_R31 =3D __VCPU_XREG_R31, +#endif }; =20 enum exit_fastpath_completion { diff --git a/arch/x86/include/asm/kvm_vcpu_regs.h b/arch/x86/include/asm/kv= m_vcpu_regs.h index 1af2cb59233b..dd0cc171f405 100644 --- a/arch/x86/include/asm/kvm_vcpu_regs.h +++ b/arch/x86/include/asm/kvm_vcpu_regs.h @@ -20,6 +20,22 @@ #define __VCPU_REGS_R13 13 #define __VCPU_REGS_R14 14 #define __VCPU_REGS_R15 15 +#define __VCPU_XREG_R16 16 +#define __VCPU_XREG_R17 17 +#define __VCPU_XREG_R18 18 +#define __VCPU_XREG_R19 19 +#define __VCPU_XREG_R20 20 +#define __VCPU_XREG_R21 21 +#define __VCPU_XREG_R22 22 +#define __VCPU_XREG_R23 23 +#define __VCPU_XREG_R24 24 +#define __VCPU_XREG_R25 25 +#define __VCPU_XREG_R26 26 +#define __VCPU_XREG_R27 27 +#define __VCPU_XREG_R28 28 +#define __VCPU_XREG_R29 29 +#define __VCPU_XREG_R30 30 +#define __VCPU_XREG_R31 31 #endif =20 #endif /* _ASM_X86_KVM_VCPU_REGS_H */ diff --git a/arch/x86/kvm/fpu.h b/arch/x86/kvm/fpu.h index 3ba12888bf66..159239b3a651 100644 --- a/arch/x86/kvm/fpu.h +++ b/arch/x86/kvm/fpu.h @@ -4,6 +4,7 @@ #define __KVM_FPU_H_ =20 #include +#include =20 typedef u32 __attribute__((vector_size(16))) sse128_t; #define __sse128_u union { sse128_t vec; u64 as_u64[2]; u32 as_u32[4]; } @@ -137,4 +138,9 @@ static inline void kvm_write_mmx_reg(int reg, const u64= *data) kvm_fpu_put(); } =20 +#ifdef CONFIG_X86_64 +static inline unsigned long kvm_read_egpr(int reg) { return 0; } +static inline void kvm_write_egpr(int reg, unsigned long data) { } +#endif + #endif diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index 4edadd64d3d5..74ae8f12b5a1 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -400,9 +400,49 @@ static inline bool vcpu_match_mmio_gpa(struct kvm_vcpu= *vcpu, gpa_t gpa) return false; } =20 +#ifdef CONFIG_X86_64 +static inline unsigned long _kvm_gpr_read(struct kvm_vcpu *vcpu, int reg) +{ + switch (reg) { + case VCPU_REGS_RAX ... VCPU_REGS_R15: + return kvm_register_read_raw(vcpu, reg); + case VCPU_XREG_R16 ... VCPU_XREG_R31: + return kvm_read_egpr(reg); + default: + WARN_ON_ONCE(1); + } + + return 0; +} + +static inline void _kvm_gpr_write(struct kvm_vcpu *vcpu, int reg, unsigned= long val) +{ + switch (reg) { + case VCPU_REGS_RAX ... VCPU_REGS_R15: + kvm_register_write_raw(vcpu, reg, val); + break; + case VCPU_XREG_R16 ... VCPU_XREG_R31: + kvm_write_egpr(reg, val); + break; + default: + WARN_ON_ONCE(1); + } +} +#else +static inline unsigned long _kvm_gpr_read(struct kvm_vcpu *vcpu, int reg) +{ + return kvm_register_read_raw(vcpu, reg); +} + +static inline void _kvm_gpr_write(struct kvm_vcpu *vcpu, int reg, unsigned= long val) +{ + kvm_register_write_raw(vcpu, reg, val); +} +#endif + static inline unsigned long kvm_gpr_read(struct kvm_vcpu *vcpu, int reg) { - unsigned long val =3D kvm_register_read_raw(vcpu, reg); + unsigned long val =3D _kvm_gpr_read(vcpu, reg); =20 return is_64_bit_mode(vcpu) ? val : (u32)val; } @@ -411,7 +451,7 @@ static inline void kvm_gpr_write(struct kvm_vcpu *vcpu,= int reg, unsigned long v { if (!is_64_bit_mode(vcpu)) val =3D (u32)val; - return kvm_register_write_raw(vcpu, reg, val); + _kvm_gpr_write(vcpu, reg, val); } =20 static inline bool kvm_check_has_quirk(struct kvm *kvm, u64 quirk) --=20 2.51.0 From nobody Sun Feb 8 02:56:30 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AFDF133030E; Mon, 10 Nov 2025 18:24:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799081; cv=none; b=jGpqc9DhTtMSwpC2fbxgcgnibbtc/u1tmAqD1gPBZMFIWhrXl/0COnAmnnbL5i0Ccw2cIIugITDXr2FKPrimHJ2J9XCNjLEs2f0wsC2gjOEn826mzKb6NgtVtE0PPFvh6HDkhqJEAXApCYenZudAwECPoSh/Jry+9BY4WgXDS+8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799081; c=relaxed/simple; bh=fjLFINRg3/6z0R1dBo+yQCUsxeF40Dkx+1fVSpT0dC8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=HeI7HNrpwwqA+VZkLyx7WPovwlgGRJF8WyiRlYknbszFU/gtjXXZxFZTubbyw9Uq4xAHCHUKM3JM+wCWizMTkklpmNi6GTFlenaUtW5PvtX2yH/X7aeEqbY3aaRK5eUmhYGrtOQX172SHnl3oozfDwmKXoJUrj2NmrZu4T8e3U4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Qqd9c6P3; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Qqd9c6P3" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1762799080; x=1794335080; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=fjLFINRg3/6z0R1dBo+yQCUsxeF40Dkx+1fVSpT0dC8=; b=Qqd9c6P3uCqln1UVXNAG/i6o279qN1niEQJYIRx+gm9QJjJ3eOSP4IcP blpkXhjqevTLGH71xS2BJ692JyK+ekPJI3N1BbDaBbSXtAtX78mQuP8fK /8G0bvC8Xq1Z1K713LUN/DesFMj2KEGGtSjfJMcPPeku3lHCQrQ0Ck1bA 26B4kNvm8GzrKkETjvoWPBivU8r5UImOr3AzdohKvaiFYRgkjsBuuGQU5 oOYAnw78IbPj2D99Yg6i1hXp4isH2/e0RIlBnLYh5o+bagl/+jLL8Kx9g s+EdW3vbfET0A8RH4uLkIsJpcsjyYrIoqYwTDnzx0OdNXbp+xuwKxntK1 Q==; X-CSE-ConnectionGUID: qNz2oky5Rb2eLuKM1W12Xg== X-CSE-MsgGUID: k+XIwtcLR/SnT52/RkkVdA== X-IronPort-AV: E=McAfee;i="6800,10657,11609"; a="76305486" X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="76305486" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2025 10:24:40 -0800 X-CSE-ConnectionGUID: IEzSgS/MSiKbfjVP7N8umw== X-CSE-MsgGUID: GqXqhK1HQSOZv9GaxwatSg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="219396076" Received: from chang-linux-3.sc.intel.com (HELO chang-linux-3) ([172.25.66.172]) by orviesa002.jf.intel.com with ESMTP; 10 Nov 2025 10:24:40 -0800 From: "Chang S. Bae" To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, chao.gao@intel.com, zhao1.liu@intel.com, chang.seok.bae@intel.com Subject: [PATCH RFC v1 03/20] KVM: x86: Implement accessors for extended GPRs Date: Mon, 10 Nov 2025 18:01:14 +0000 Message-ID: <20251110180131.28264-4-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251110180131.28264-1-chang.seok.bae@intel.com> References: <20251110180131.28264-1-chang.seok.bae@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Add helpers to directly read and write EGPRs (R16=E2=80=93R31). Unlike legacy GPRs, EGPRs are not cached in vcpu->arch.regs[]. Their contents remain live in hardware. If preempted, the EGPR state is preserved in the guest XSAVE buffer. The Advanced Performance Extentions (APX) feature introduces EGPRs as an XSAVE-managed state component. The new helpers access the registers directly between kvm_fpu_get() and kvm_fpu_put(). Callers should ensure that EGPRs are enabled before using these helpers. Signed-off-by: Chang S. Bae --- RFC note: There may be alternative options for EGPR access. If the EGPR state is saved in the guest fpstate, KVM could read or write it there instead. However, since EGPR-related VM exits are expected to be rare, adding extra complexity and overhead at this stage doesn=E2=80=99t seem worthwhile. --- arch/x86/kvm/fpu.h | 80 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/fpu.h b/arch/x86/kvm/fpu.h index 159239b3a651..aa35bdf1a073 100644 --- a/arch/x86/kvm/fpu.h +++ b/arch/x86/kvm/fpu.h @@ -96,6 +96,61 @@ static inline void _kvm_write_mmx_reg(int reg, const u64= *data) } } =20 +#ifdef CONFIG_X86_64 +/* + * Accessors for extended general-purpose registers. binutils >=3D 2.43 can + * recognize those register symbols. + */ + +static inline void _kvm_read_egpr(int reg, unsigned long *data) +{ + /* mov %r16..%r31, %rax */ + switch (reg) { + case __VCPU_XREG_R16: asm(".byte 0xd5, 0x48, 0x89, 0xc0" : "=3Da"(*data))= ; break; + case __VCPU_XREG_R17: asm(".byte 0xd5, 0x48, 0x89, 0xc8" : "=3Da"(*data))= ; break; + case __VCPU_XREG_R18: asm(".byte 0xd5, 0x48, 0x89, 0xd0" : "=3Da"(*data))= ; break; + case __VCPU_XREG_R19: asm(".byte 0xd5, 0x48, 0x89, 0xd8" : "=3Da"(*data))= ; break; + case __VCPU_XREG_R20: asm(".byte 0xd5, 0x48, 0x89, 0xe0" : "=3Da"(*data))= ; break; + case __VCPU_XREG_R21: asm(".byte 0xd5, 0x48, 0x89, 0xe8" : "=3Da"(*data))= ; break; + case __VCPU_XREG_R22: asm(".byte 0xd5, 0x48, 0x89, 0xf0" : "=3Da"(*data))= ; break; + case __VCPU_XREG_R23: asm(".byte 0xd5, 0x48, 0x89, 0xf8" : "=3Da"(*data))= ; break; + case __VCPU_XREG_R24: asm(".byte 0xd5, 0x4c, 0x89, 0xc0" : "=3Da"(*data))= ; break; + case __VCPU_XREG_R25: asm(".byte 0xd5, 0x4c, 0x89, 0xc8" : "=3Da"(*data))= ; break; + case __VCPU_XREG_R26: asm(".byte 0xd5, 0x4c, 0x89, 0xd0" : "=3Da"(*data))= ; break; + case __VCPU_XREG_R27: asm(".byte 0xd5, 0x4c, 0x89, 0xd8" : "=3Da"(*data))= ; break; + case __VCPU_XREG_R28: asm(".byte 0xd5, 0x4c, 0x89, 0xe0" : "=3Da"(*data))= ; break; + case __VCPU_XREG_R29: asm(".byte 0xd5, 0x4c, 0x89, 0xe8" : "=3Da"(*data))= ; break; + case __VCPU_XREG_R30: asm(".byte 0xd5, 0x4c, 0x89, 0xf0" : "=3Da"(*data))= ; break; + case __VCPU_XREG_R31: asm(".byte 0xd5, 0x4c, 0x89, 0xf8" : "=3Da"(*data))= ; break; + default: BUG(); + } +} + +static inline void _kvm_write_egpr(int reg, unsigned long *data) +{ + /* mov %rax, %r16...%r31*/ + switch (reg) { + case __VCPU_XREG_R16: asm(".byte 0xd5, 0x18, 0x89, 0xc0" : : "a"(*data));= break; + case __VCPU_XREG_R17: asm(".byte 0xd5, 0x18, 0x89, 0xc1" : : "a"(*data));= break; + case __VCPU_XREG_R18: asm(".byte 0xd5, 0x18, 0x89, 0xc2" : : "a"(*data));= break; + case __VCPU_XREG_R19: asm(".byte 0xd5, 0x18, 0x89, 0xc3" : : "a"(*data));= break; + case __VCPU_XREG_R20: asm(".byte 0xd5, 0x18, 0x89, 0xc4" : : "a"(*data));= break; + case __VCPU_XREG_R21: asm(".byte 0xd5, 0x18, 0x89, 0xc5" : : "a"(*data));= break; + case __VCPU_XREG_R22: asm(".byte 0xd5, 0x18, 0x89, 0xc6" : : "a"(*data));= break; + case __VCPU_XREG_R23: asm(".byte 0xd5, 0x18, 0x89, 0xc7" : : "a"(*data));= break; + case __VCPU_XREG_R24: asm(".byte 0xd5, 0x19, 0x89, 0xc0" : : "a"(*data));= break; + case __VCPU_XREG_R25: asm(".byte 0xd5, 0x19, 0x89, 0xc1" : : "a"(*data));= break; + case __VCPU_XREG_R26: asm(".byte 0xd5, 0x19, 0x89, 0xc2" : : "a"(*data));= break; + case __VCPU_XREG_R27: asm(".byte 0xd5, 0x19, 0x89, 0xc3" : : "a"(*data));= break; + case __VCPU_XREG_R28: asm(".byte 0xd5, 0x19, 0x89, 0xc4" : : "a"(*data));= break; + case __VCPU_XREG_R29: asm(".byte 0xd5, 0x19, 0x89, 0xc5" : : "a"(*data));= break; + case __VCPU_XREG_R30: asm(".byte 0xd5, 0x19, 0x89, 0xc6" : : "a"(*data));= break; + case __VCPU_XREG_R31: asm(".byte 0xd5, 0x19, 0x89, 0xc7" : : "a"(*data));= break; + default: BUG(); + } +} +#endif + static inline void kvm_fpu_get(void) { fpregs_lock(); @@ -139,8 +194,29 @@ static inline void kvm_write_mmx_reg(int reg, const u6= 4 *data) } =20 #ifdef CONFIG_X86_64 -static inline unsigned long kvm_read_egpr(int reg) { return 0; } -static inline void kvm_write_egpr(int reg, unsigned long data) { } +static inline unsigned long kvm_read_egpr(int reg) +{ + unsigned long data; + + if (WARN_ON_ONCE(!cpu_has_xfeatures(XFEATURE_MASK_APX, NULL))) + return 0; + + kvm_fpu_get(); + _kvm_read_egpr(reg, &data); + kvm_fpu_put(); + + return data; +} + +static inline void kvm_write_egpr(int reg, unsigned long data) +{ + if (WARN_ON_ONCE(!cpu_has_xfeatures(XFEATURE_MASK_APX, NULL))) + return; + + kvm_fpu_get(); + _kvm_write_egpr(reg, &data); + kvm_fpu_put(); +} #endif =20 #endif --=20 2.51.0 From nobody Sun Feb 8 02:56:30 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B25E2330B17; Mon, 10 Nov 2025 18:24:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799083; cv=none; b=stYEgJE9CQNheh+LwlijWJztRFl5T7Y6GhT+JGU60okPCaF0mOKGMK7OculAKl7k9u9ZJo4FUnd9stY4MGasVymtiHt7IGeo8UtTa1Abg3PD+h2nQfrztf3ymaXxV1YigZYbZrzVenvyRuP+HRslN7Z+WqcPBL7NFSjTN2vYObc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799083; c=relaxed/simple; bh=J56z5jm9hwNPN0i9sE+CDuzUkAEhWxjTGRWpBhhgLhk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GQEjqRqRzFyVq6BZ55fMXgJ7oYjHAsYZXSVjGZ3R01cKhc954obHYxG2Pqn8Ys3QhzkBB8P9gFilGG2V5v5iIClh34XM5Kje5Vn9cqj9K5XDl8oWOE/WxFv/R0eOGt9E142akpqqlafDxX1fgaf2vFiHsVya5Tr8n5BBAi5UG5M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=eNAuuxmx; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="eNAuuxmx" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1762799082; x=1794335082; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=J56z5jm9hwNPN0i9sE+CDuzUkAEhWxjTGRWpBhhgLhk=; b=eNAuuxmx6AkcE/UXoCcosAom9L6JpYLA1kisnixcsbR6S8oVq14gv9PM zcSgwqAorTjZz+5QFtpnxgFr6+z9sB9UfDiggjm4N50PyjwGkw7N/t3vr RQaEKWG9jBysi/xli5hTBbym4tHKnD+Zo+B5TP0TTFvL5aVDhCU650W3O AOBG9iw2RdSPMvwKESb99nOdqcLhEKull9MKyBA9DhD4lCWQFelUE3OO9 C9LYpb0lvLRxKp+d3gHEB4uyRN8JIyi19n0Om809kFvM/+rwjc9nZGhGT x8cR9cbOscUcTpxmMzQ/oxH6IyfqNhGyrVex7m16Gt8+xvEe5ubglF4cj g==; X-CSE-ConnectionGUID: 8/zTmDEMQpWoYePc072gsg== X-CSE-MsgGUID: epBIv7gaSJKVCwvTLQ4Kbw== X-IronPort-AV: E=McAfee;i="6800,10657,11609"; a="76305489" X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="76305489" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2025 10:24:42 -0800 X-CSE-ConnectionGUID: +94w+2mTQZaDsKMdmgTXsQ== X-CSE-MsgGUID: EhK3mx4bSy2X5EhpTazMzQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="219396095" Received: from chang-linux-3.sc.intel.com (HELO chang-linux-3) ([172.25.66.172]) by orviesa002.jf.intel.com with ESMTP; 10 Nov 2025 10:24:42 -0800 From: "Chang S. Bae" To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, chao.gao@intel.com, zhao1.liu@intel.com, chang.seok.bae@intel.com Subject: [PATCH RFC v1 04/20] KVM: VMX: Introduce unified instruction info structure Date: Mon, 10 Nov 2025 18:01:15 +0000 Message-ID: <20251110180131.28264-5-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251110180131.28264-1-chang.seok.bae@intel.com> References: <20251110180131.28264-1-chang.seok.bae@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Define a unified data structure that can represent both the legacy and extended VMX instruction information formats. VMX provides per-instruction metadata for VM exits to help decode the attributes of the instruction that triggered the exit. The legacy format, however, only supports up to 16 GPRs and thus cannot represent EGPRs. To support these new registers, VMX introduces an extended 64-bit layout. Instead of maintaining separate storage for each format, a single union structure makes the overall handling simple. The field names are consistent across both layouts. While the presence of certain fields depends on the instruction type, the offsets remain fixed within each format. Signed-off-by: Chang S. Bae --- arch/x86/kvm/vmx/vmx.h | 61 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index ea93121029f9..c358aca7253c 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -311,6 +311,67 @@ struct kvm_vmx { u64 *pid_table; }; =20 +/* + * 32-bit layout of the legacy instruction information field. This format + * supports the 16 legacy GPRs. + */ +struct base_insn_info { + u32 scale : 2; /* Scaling factor */ + u32 reserved1 : 1; + u32 reg1 : 4; /* First register index */ + u32 asize : 3; /* Address size */ + u32 is_reg : 1; /* 0: memory, 1: register */ + u32 osize : 2; /* Operand size */ + u32 reserved2 : 2; + u32 seg : 3; /* Segment register index */ + u32 index : 4; /* Index register index */ + u32 index_invalid : 1; /* 0: valid, 1: invalid */ + u32 base : 4; /* Base register index */ + u32 base_invalid : 1; /* 0: valid, 1: invalid */ + u32 reg2 : 4; /* Second register index */ +}; + +/* + * 64-bit layout of the extended instruction information field, which + * supports EGPRs. + */ +struct ext_insn_info { + u64 scale : 2; /* Scaling factor */ + u64 asize : 2; /* Address size */ + u64 is_reg : 1; /* 0: memory, 1: register */ + u64 osize : 2; /* Operand size */ + u64 seg : 3; /* Segment register index */ + u64 index_invalid : 1; /* 0: valid, 1: invalid */ + u64 base_invalid : 1; /* 0: valid, 1: invalid */ + u64 reserved1 : 4; + u64 reg1 : 5; /* First register index */ + u64 reserved2 : 3; + u64 index : 5; /* Index register index */ + u64 reserved3 : 3; + u64 base : 5; /* Base register index */ + u64 reserved4 : 3; + u64 reg2 : 5; /* Second register index */ + u64 reserved5 : 19; +}; + +/* Union for accessing either the legacy or extended format. */ +union insn_info { + struct base_insn_info base; + struct ext_insn_info ext; + u32 word; + u64 dword; +}; + +/* + * Wrapper structure combining the instruction info and a flag indicating + * whether the extended layout is in use. + */ +struct vmx_insn_info { + /* true if using the extended layout */ + bool extended; + union insn_info info; +}; + static __always_inline struct vcpu_vt *to_vt(struct kvm_vcpu *vcpu) { return &(container_of(vcpu, struct vcpu_vmx, vcpu)->vt); --=20 2.51.0 From nobody Sun Feb 8 02:56:30 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C2A673314B6; Mon, 10 Nov 2025 18:24:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799085; cv=none; b=g0hGaIoLe8vVWGn4KPPKb+GNf4igYvpvlPjKWkNqnKVB9pycHnZJ2HZdyuqGfrh8QaIRG9G5xgEYT3TkMiMJJ+hh6UnaIvFUecbxBThFndOV802ql2ILm+SCbMiH16uKR05/4ObiFZPzZ9ggvaPbN00xbnr+emtpxH7zmGYXsh8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799085; c=relaxed/simple; bh=rOAz+ywDBJAMB3ygOZJMiUcKS5Zy1jnq3CoNZHxvWyw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CjB0ZCT9SPTPTiVapnItl3ZxG4wmrg8hVJkxLTsx/Fxbrx+SC/fQJcr6ehbQIx1JuCqwWOcujL+9+LT7UN18TPkk7QcWPtvieLh6jibqogVvKah2tkDl6yHSvov0XeO3xvx4u6qmP7Hbdd1vPyRA24QcUzirBesFzlYr1JgAU1g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=hss9biar; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="hss9biar" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1762799084; x=1794335084; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rOAz+ywDBJAMB3ygOZJMiUcKS5Zy1jnq3CoNZHxvWyw=; b=hss9biarrV/BLHmPHmSpYbi5uuiKS3CTd+e26I1xzOCJ6gGmOlzH5rM4 HanwfS+9y3BPxgKnuVMjl43oYz0uYfmSIphz7drRt3zy9M2+EGSutOuYb cgZswH0hc1j0wtQp1KmNArM+YfcS3kQNTg4B3NfwGgJB0XX18bh8HW3mk F4AWIkme4hTes6ppVK9D7xbNYSvvkXR9o1sF7dJx+LYkSvLmXSFR73Kf2 rVak+ybxY1I1vF4SY1tkvyMVCvmp34Zdo2/RRNh9BCz7+kMBlnQimIZkz kQUPG6hDzsZvD39mLSrdtjAbLEenck0jXdl3amT52RhDXalmlH1od9e54 w==; X-CSE-ConnectionGUID: K8F+pzBEQOKJX99Jr2U65w== X-CSE-MsgGUID: G7wSg65FTvuWe1g8wjWF5g== X-IronPort-AV: E=McAfee;i="6800,10657,11609"; a="76305492" X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="76305492" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2025 10:24:44 -0800 X-CSE-ConnectionGUID: UvELXc6mTyC2S4DYueoUkQ== X-CSE-MsgGUID: pd1+b6rwSLG09jIWdRTcPg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="219396114" Received: from chang-linux-3.sc.intel.com (HELO chang-linux-3) ([172.25.66.172]) by orviesa002.jf.intel.com with ESMTP; 10 Nov 2025 10:24:44 -0800 From: "Chang S. Bae" To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, chao.gao@intel.com, zhao1.liu@intel.com, chang.seok.bae@intel.com Subject: [PATCH RFC v1 05/20] KVM: VMX: Refactor instruction information retrieval Date: Mon, 10 Nov 2025 18:01:16 +0000 Message-ID: <20251110180131.28264-6-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251110180131.28264-1-chang.seok.bae@intel.com> References: <20251110180131.28264-1-chang.seok.bae@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce helpers to convert and extract exited instruction attributes, preparing for EGPR support and deprecating some existing helpers. Previously, VMX exit handlers directly decoded the raw VMCS field, resulting in duplicated logic and assumption tied to the legacy layout. With the unified structure, handlers can convert raw data into a structure form and access each instruction attribute by field name. The helper will later determine the format based on the VCPU configuration. For now, there is no functional change since only the legacy layout is used. Signed-off-by: Chang S. Bae --- RFC note: Macro and variable naming may still evolve depending on maintainer/reviewer preferences. --- arch/x86/kvm/vmx/nested.c | 73 +++++++++++++++++++-------------------- arch/x86/kvm/vmx/nested.h | 2 +- arch/x86/kvm/vmx/vmx.c | 14 ++++---- arch/x86/kvm/vmx/vmx.h | 23 ++++++------ 4 files changed, 57 insertions(+), 55 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 47a941989787..4b883ded6c4b 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -5289,7 +5289,7 @@ static void nested_vmx_triple_fault(struct kvm_vcpu *= vcpu) * #UD, #GP, or #SS. */ int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualific= ation, - u32 vmx_instruction_info, bool wr, int len, gva_t *ret) + struct vmx_insn_info info, bool wr, int len, gva_t *ret) { gva_t off; bool exn; @@ -5303,14 +5303,14 @@ int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsi= gned long exit_qualification, * For how an actual address is calculated from all these components, * refer to Vol. 1, "Operand Addressing". */ - int scaling =3D vmx_instruction_info & 3; - int addr_size =3D (vmx_instruction_info >> 7) & 7; - bool is_reg =3D vmx_instruction_info & (1u << 10); - int seg_reg =3D (vmx_instruction_info >> 15) & 7; - int index_reg =3D (vmx_instruction_info >> 18) & 0xf; - bool index_is_valid =3D !(vmx_instruction_info & (1u << 22)); - int base_reg =3D (vmx_instruction_info >> 23) & 0xf; - bool base_is_valid =3D !(vmx_instruction_info & (1u << 27)); + int scaling =3D insn_attr(info, scale); + int addr_size =3D insn_attr(info, asize); + bool is_reg =3D insn_attr(info, is_reg); + int seg_reg =3D insn_attr(info, seg); + int index_reg =3D insn_attr(info, index); + bool index_is_valid =3D !insn_attr(info, index_invalid); + int base_reg =3D insn_attr(info, base); + bool base_is_valid =3D !insn_attr(info, base_invalid); =20 if (is_reg) { kvm_queue_exception(vcpu, UD_VECTOR); @@ -5421,7 +5421,7 @@ static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu= , gpa_t *vmpointer, int r; =20 if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), - vmcs_read32(VMX_INSTRUCTION_INFO), false, + vmx_get_insn_info(vcpu), false, sizeof(*vmpointer), &gva)) { *ret =3D 1; return -EINVAL; @@ -5706,7 +5706,7 @@ static int handle_vmread(struct kvm_vcpu *vcpu) struct vmcs12 *vmcs12 =3D is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu) : get_vmcs12(vcpu); unsigned long exit_qualification =3D vmx_get_exit_qual(vcpu); - u32 instr_info =3D vmcs_read32(VMX_INSTRUCTION_INFO); + struct vmx_insn_info info =3D vmx_get_insn_info(vcpu); struct vcpu_vmx *vmx =3D to_vmx(vcpu); struct x86_exception e; unsigned long field; @@ -5719,7 +5719,7 @@ static int handle_vmread(struct kvm_vcpu *vcpu) return 1; =20 /* Decode instruction info and find the field to read */ - field =3D kvm_gpr_read(vcpu, (((instr_info) >> 28) & 0xf)); + field =3D kvm_gpr_read(vcpu, insn_attr(info, reg2)); =20 if (!nested_vmx_is_evmptr12_valid(vmx)) { /* @@ -5767,12 +5767,12 @@ static int handle_vmread(struct kvm_vcpu *vcpu) * Note that the number of bits actually copied is 32 or 64 depending * on the guest's mode (32 or 64 bit), not on the given field's length. */ - if (instr_info & BIT(10)) { - kvm_gpr_write(vcpu, (((instr_info) >> 3) & 0xf), value); + if (insn_attr(info, is_reg)) { + kvm_gpr_write(vcpu, insn_attr(info, reg1), value); } else { len =3D is_64_bit_mode(vcpu) ? 8 : 4; if (get_vmx_mem_address(vcpu, exit_qualification, - instr_info, true, len, &gva)) + info, true, len, &gva)) return 1; /* _system ok, nested_vmx_check_permission has verified cpl=3D0 */ r =3D kvm_write_guest_virt_system(vcpu, gva, &value, len, &e); @@ -5812,7 +5812,7 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu) struct vmcs12 *vmcs12 =3D is_guest_mode(vcpu) ? get_shadow_vmcs12(vcpu) : get_vmcs12(vcpu); unsigned long exit_qualification =3D vmx_get_exit_qual(vcpu); - u32 instr_info =3D vmcs_read32(VMX_INSTRUCTION_INFO); + struct vmx_insn_info info =3D vmx_get_insn_info(vcpu); struct vcpu_vmx *vmx =3D to_vmx(vcpu); struct x86_exception e; unsigned long field; @@ -5841,19 +5841,19 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu) get_vmcs12(vcpu)->vmcs_link_pointer =3D=3D INVALID_GPA)) return nested_vmx_failInvalid(vcpu); =20 - if (instr_info & BIT(10)) - value =3D kvm_gpr_read(vcpu, (((instr_info) >> 3) & 0xf)); + if (insn_attr(info, is_reg)) + value =3D kvm_gpr_read(vcpu, insn_attr(info, reg1)); else { len =3D is_64_bit_mode(vcpu) ? 8 : 4; if (get_vmx_mem_address(vcpu, exit_qualification, - instr_info, false, len, &gva)) + info, false, len, &gva)) return 1; r =3D kvm_read_guest_virt(vcpu, gva, &value, len, &e); if (r !=3D X86EMUL_CONTINUE) return kvm_handle_memory_failure(vcpu, r, &e); } =20 - field =3D kvm_gpr_read(vcpu, (((instr_info) >> 28) & 0xf)); + field =3D kvm_gpr_read(vcpu, insn_attr(info, reg2)); =20 offset =3D get_vmcs12_field_offset(field); if (offset < 0) @@ -6001,7 +6001,7 @@ static int handle_vmptrld(struct kvm_vcpu *vcpu) static int handle_vmptrst(struct kvm_vcpu *vcpu) { unsigned long exit_qual =3D vmx_get_exit_qual(vcpu); - u32 instr_info =3D vmcs_read32(VMX_INSTRUCTION_INFO); + struct vmx_insn_info info =3D vmx_get_insn_info(vcpu); gpa_t current_vmptr =3D to_vmx(vcpu)->nested.current_vmptr; struct x86_exception e; gva_t gva; @@ -6013,7 +6013,7 @@ static int handle_vmptrst(struct kvm_vcpu *vcpu) if (unlikely(nested_vmx_is_evmptr12_valid(to_vmx(vcpu)))) return 1; =20 - if (get_vmx_mem_address(vcpu, exit_qual, instr_info, + if (get_vmx_mem_address(vcpu, exit_qual, info, true, sizeof(gpa_t), &gva)) return 1; /* *_system ok, nested_vmx_check_permission has verified cpl=3D0 */ @@ -6029,15 +6029,16 @@ static int handle_vmptrst(struct kvm_vcpu *vcpu) static int handle_invept(struct kvm_vcpu *vcpu) { struct vcpu_vmx *vmx =3D to_vmx(vcpu); - u32 vmx_instruction_info, types; unsigned long type, roots_to_free; + struct vmx_insn_info info; struct kvm_mmu *mmu; gva_t gva; struct x86_exception e; struct { u64 eptp, gpa; } operand; - int i, r, gpr_index; + u32 types; + int i, r; =20 if (!(vmx->nested.msrs.secondary_ctls_high & SECONDARY_EXEC_ENABLE_EPT) || @@ -6049,9 +6050,8 @@ static int handle_invept(struct kvm_vcpu *vcpu) if (!nested_vmx_check_permission(vcpu)) return 1; =20 - vmx_instruction_info =3D vmcs_read32(VMX_INSTRUCTION_INFO); - gpr_index =3D vmx_get_instr_info_reg2(vmx_instruction_info); - type =3D kvm_gpr_read(vcpu, gpr_index); + info =3D vmx_get_insn_info(vcpu); + type =3D kvm_gpr_read(vcpu, insn_attr(info, reg2)); =20 types =3D (vmx->nested.msrs.ept_caps >> VMX_EPT_EXTENT_SHIFT) & 6; =20 @@ -6062,7 +6062,7 @@ static int handle_invept(struct kvm_vcpu *vcpu) * operand is read even if it isn't needed (e.g., for type=3D=3Dglobal) */ if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), - vmx_instruction_info, false, sizeof(operand), &gva)) + info, false, sizeof(operand), &gva)) return 1; r =3D kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e); if (r !=3D X86EMUL_CONTINUE) @@ -6109,7 +6109,7 @@ static int handle_invept(struct kvm_vcpu *vcpu) static int handle_invvpid(struct kvm_vcpu *vcpu) { struct vcpu_vmx *vmx =3D to_vmx(vcpu); - u32 vmx_instruction_info; + struct vmx_insn_info info; unsigned long type, types; gva_t gva; struct x86_exception e; @@ -6118,7 +6118,7 @@ static int handle_invvpid(struct kvm_vcpu *vcpu) u64 gla; } operand; u16 vpid02; - int r, gpr_index; + int r; =20 if (!(vmx->nested.msrs.secondary_ctls_high & SECONDARY_EXEC_ENABLE_VPID) || @@ -6130,9 +6130,8 @@ static int handle_invvpid(struct kvm_vcpu *vcpu) if (!nested_vmx_check_permission(vcpu)) return 1; =20 - vmx_instruction_info =3D vmcs_read32(VMX_INSTRUCTION_INFO); - gpr_index =3D vmx_get_instr_info_reg2(vmx_instruction_info); - type =3D kvm_gpr_read(vcpu, gpr_index); + info =3D vmx_get_insn_info(vcpu); + type =3D kvm_gpr_read(vcpu, insn_attr(info, reg2)); =20 types =3D (vmx->nested.msrs.vpid_caps & VMX_VPID_EXTENT_SUPPORTED_MASK) >> 8; @@ -6145,7 +6144,7 @@ static int handle_invvpid(struct kvm_vcpu *vcpu) * operand is read even if it isn't needed (e.g., for type=3D=3Dglobal) */ if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), - vmx_instruction_info, false, sizeof(operand), &gva)) + info, false, sizeof(operand), &gva)) return 1; r =3D kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e); if (r !=3D X86EMUL_CONTINUE) @@ -6483,7 +6482,7 @@ static bool nested_vmx_exit_handled_encls(struct kvm_= vcpu *vcpu, static bool nested_vmx_exit_handled_vmcs_access(struct kvm_vcpu *vcpu, struct vmcs12 *vmcs12, gpa_t bitmap) { - u32 vmx_instruction_info; + struct vmx_insn_info info; unsigned long field; u8 b; =20 @@ -6491,8 +6490,8 @@ static bool nested_vmx_exit_handled_vmcs_access(struc= t kvm_vcpu *vcpu, return true; =20 /* Decode instruction info and find the field to access */ - vmx_instruction_info =3D vmcs_read32(VMX_INSTRUCTION_INFO); - field =3D kvm_gpr_read(vcpu, (((vmx_instruction_info) >> 28) & 0xf)); + info =3D vmx_get_insn_info(vcpu); + field =3D kvm_gpr_read(vcpu, insn_attr(info, reg2)); =20 /* Out-of-range fields always cause a VM exit from L2 to L1 */ if (field >> 15) diff --git a/arch/x86/kvm/vmx/nested.h b/arch/x86/kvm/vmx/nested.h index 983484d42ebf..e54f4e7b3664 100644 --- a/arch/x86/kvm/vmx/nested.h +++ b/arch/x86/kvm/vmx/nested.h @@ -50,7 +50,7 @@ void nested_sync_vmcs12_to_shadow(struct kvm_vcpu *vcpu); int vmx_set_vmx_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data); int vmx_get_vmx_msr(struct nested_vmx_msrs *msrs, u32 msr_index, u64 *pdat= a); int get_vmx_mem_address(struct kvm_vcpu *vcpu, unsigned long exit_qualific= ation, - u32 vmx_instruction_info, bool wr, int len, gva_t *ret); + struct vmx_insn_info info, bool wr, int len, gva_t *ret); void nested_mark_vmcs12_pages_dirty(struct kvm_vcpu *vcpu); bool nested_vmx_check_io_bitmaps(struct kvm_vcpu *vcpu, unsigned int port, int size); diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index c7d38f7692cf..dd8c9517c38c 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5925,29 +5925,27 @@ static int handle_monitor_trap(struct kvm_vcpu *vcp= u) =20 static int handle_invpcid(struct kvm_vcpu *vcpu) { - u32 vmx_instruction_info; + struct vmx_insn_info info; unsigned long type; gva_t gva; struct { u64 pcid; u64 gla; } operand; - int gpr_index; =20 if (!guest_cpu_cap_has(vcpu, X86_FEATURE_INVPCID)) { kvm_queue_exception(vcpu, UD_VECTOR); return 1; } =20 - vmx_instruction_info =3D vmcs_read32(VMX_INSTRUCTION_INFO); - gpr_index =3D vmx_get_instr_info_reg2(vmx_instruction_info); - type =3D kvm_gpr_read(vcpu, gpr_index); + info =3D vmx_get_insn_info(vcpu); + type =3D kvm_gpr_read(vcpu, insn_attr(info, reg2)); =20 /* According to the Intel instruction reference, the memory operand * is read even if it isn't needed (e.g., for type=3D=3Dall) */ if (get_vmx_mem_address(vcpu, vmx_get_exit_qual(vcpu), - vmx_instruction_info, false, + info, false, sizeof(operand), &gva)) return 1; =20 @@ -6084,7 +6082,9 @@ static int handle_notify(struct kvm_vcpu *vcpu) =20 static int vmx_get_msr_imm_reg(struct kvm_vcpu *vcpu) { - return vmx_get_instr_info_reg(vmcs_read32(VMX_INSTRUCTION_INFO)); + struct vmx_insn_info info =3D vmx_get_insn_info(vcpu); + + return insn_attr(info, reg1); } =20 static int handle_rdmsr_imm(struct kvm_vcpu *vcpu) diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index c358aca7253c..a58d9187ed1d 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -372,6 +372,19 @@ struct vmx_insn_info { union insn_info info; }; =20 +static inline struct vmx_insn_info vmx_get_insn_info(struct kvm_vcpu *vcpu= __maybe_unused) +{ + struct vmx_insn_info insn; + + insn.extended =3D false; + insn.info.word =3D vmcs_read32(VMX_INSTRUCTION_INFO); + + return insn; +} + +#define insn_attr(insn, attr) \ + ((insn).extended ? (insn).info.ext.attr : (insn).info.base.attr) + static __always_inline struct vcpu_vt *to_vt(struct kvm_vcpu *vcpu) { return &(container_of(vcpu, struct vcpu_vmx, vcpu)->vt); @@ -780,16 +793,6 @@ static inline bool vmx_guest_state_valid(struct kvm_vc= pu *vcpu) =20 void dump_vmcs(struct kvm_vcpu *vcpu); =20 -static inline int vmx_get_instr_info_reg(u32 vmx_instr_info) -{ - return (vmx_instr_info >> 3) & 0xf; -} - -static inline int vmx_get_instr_info_reg2(u32 vmx_instr_info) -{ - return (vmx_instr_info >> 28) & 0xf; -} - static inline bool vmx_can_use_ipiv(struct kvm_vcpu *vcpu) { return lapic_in_kernel(vcpu) && enable_ipiv; --=20 2.51.0 From nobody Sun Feb 8 02:56:30 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CF532331A71; Mon, 10 Nov 2025 18:24:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799087; cv=none; b=UHZtCqLCbyo0ppa8mfeMdo7ZrHJBAUrjh5onAxdWto2c2J4NGUydGkEpkSggjYwjd2YFw0DmwGCKqdrE78Z5cHyBgVr4OQTE/hEEdFSIOkewfDZ0X8s+hKmiC6IKI4FIOhC9B+ZPylxnr6MWnKA3EenUznkqliK1meluiv919ck= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799087; c=relaxed/simple; bh=JdEY9D1v3AeME/Vd8PH/gA211HPcNSlQZiY0XFrMMxE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=O/xwxyEmI40UD1177JJxxPlvBoztYft21FxjmIlntl4aXFpxwq6JZB338czePl3FZhl/HHp3uM1PI6e8KVBrJvmZYdzlxtOdpHi2gBIoSaQZw730DNSIvNieecTrk7QE6ctmOJWiArXW56LIETFB6rWoJNGnR+j2l+PU3TexaOg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Pm/pmTe+; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Pm/pmTe+" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1762799086; x=1794335086; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=JdEY9D1v3AeME/Vd8PH/gA211HPcNSlQZiY0XFrMMxE=; b=Pm/pmTe+vffVr3bOg+LZqeldAm5LYKMUp8/NRQ8W+DenNzj7HskXfECN NkJ3Aq1Wse61IBPhWeVm8jwBkZtXmdCUI7RFcQWKCWIUIPgHsWKENqJD+ LHhDnHPdkRxsq03+wyCQwCmm7tkAsbSnIc9tH7Nxy8L2PS/v2e5Ru7ltO fkR6JLnYpcJ87wqwT0LaGu4yGbcwzAP9mk0fjmDR9nvfspaXO3c5+Yklt 4z8+r/WeGSSLhleMTsOYVKWrsT7SEzMVFlKGwFsp2xf8L2O2peA4VFqDJ jptbvDxb1nkXpLdDzdwEolkGIbZiK1I/h1VGCedUt9Eke46AiVqY6VwjI g==; X-CSE-ConnectionGUID: zMGM3z3yRNiiyPpEhUZKyg== X-CSE-MsgGUID: w9kq7ceBRrakpLhh2SzVHQ== X-IronPort-AV: E=McAfee;i="6800,10657,11609"; a="76305499" X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="76305499" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2025 10:24:46 -0800 X-CSE-ConnectionGUID: 4plH02aOR9+0iwk26K4rxw== X-CSE-MsgGUID: SVBhU90PTa2iMVuKFyjnEw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="219396128" Received: from chang-linux-3.sc.intel.com (HELO chang-linux-3) ([172.25.66.172]) by orviesa002.jf.intel.com with ESMTP; 10 Nov 2025 10:24:46 -0800 From: "Chang S. Bae" To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, chao.gao@intel.com, zhao1.liu@intel.com, chang.seok.bae@intel.com Subject: [PATCH RFC v1 06/20] KVM: VMX: Refactor GPR index retrieval from exit qualification Date: Mon, 10 Nov 2025 18:01:17 +0000 Message-ID: <20251110180131.28264-7-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251110180131.28264-1-chang.seok.bae@intel.com> References: <20251110180131.28264-1-chang.seok.bae@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce a helper to extract the GPR index from the exit qualification field. Some VMX exit qualification, in addition to the VMX instruction info field, encode a GPR index. With the introduction of EGPRs, this field is extended by a previously reserved bit position. This refactoring centralizes the logic so that future updates can handle the extended GPR index without code duplication. Since the VMCS exit qualification is cached in VCPU state, it is safe for the helper to access it directly via the VCPU pointer. This argument will also be used later to determine EGPR availability. No functional change intended. Signed-off-by: Chang S. Bae --- arch/x86/kvm/vmx/nested.c | 2 +- arch/x86/kvm/vmx/vmx.c | 2 +- arch/x86/kvm/vmx/vmx.h | 5 +++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 4b883ded6c4b..97ec8e594155 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -6404,7 +6404,7 @@ static bool nested_vmx_exit_handled_cr(struct kvm_vcp= u *vcpu, =20 switch ((exit_qualification >> 4) & 3) { case 0: /* mov to cr */ - reg =3D (exit_qualification >> 8) & 15; + reg =3D vmx_get_exit_qual_gpr(vcpu); val =3D kvm_gpr_read(vcpu, reg); switch (cr) { case 0: diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index dd8c9517c38c..4405724cb874 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -5459,7 +5459,7 @@ static int handle_cr(struct kvm_vcpu *vcpu) =20 exit_qualification =3D vmx_get_exit_qual(vcpu); cr =3D exit_qualification & 15; - reg =3D (exit_qualification >> 8) & 15; + reg =3D vmx_get_exit_qual_gpr(vcpu); switch ((exit_qualification >> 4) & 3) { case 0: /* mov to cr */ val =3D kvm_gpr_read(vcpu, reg); diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index a58d9187ed1d..64a0772c883c 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -411,6 +411,11 @@ static __always_inline unsigned long vmx_get_exit_qual= (struct kvm_vcpu *vcpu) return vt->exit_qualification; } =20 +static inline int vmx_get_exit_qual_gpr(struct kvm_vcpu *vcpu) +{ + return (vmx_get_exit_qual(vcpu) >> 8) & 0xf; +} + static __always_inline u32 vmx_get_intr_info(struct kvm_vcpu *vcpu) { struct vcpu_vt *vt =3D to_vt(vcpu); --=20 2.51.0 From nobody Sun Feb 8 02:56:30 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F37523321D7; Mon, 10 Nov 2025 18:24:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799089; cv=none; b=hTv+eHay6rXzxkIkXYNpoadX47Nm4o1g1EptoMNXnCijQ/9ofYHvFWS65nhx/GCIZvS/+cVzEXrTawGEqzPzb46g+aNnM1xX1ShkJM9Ug1JBxHZ0kztv/xR6bxTtb5X8G5u4D1U0Np0yWorhsNIILkiywv6r1ilvmXZY9rCJchQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799089; c=relaxed/simple; bh=9vN9/9HY8bfnpcKi4t8REEnOTQVjCIro+TT1Dr7KppY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iCj9miOWLoO5f70q/2e6H1omXf+jNDTTAEfzHlIL2m82sYHz0581JwW9kyxCK2BmBOWBAwuhgPJ0YB2yUT1JS1yVPYKJIeqnC82/qIefLJoyU7nK4pbZuHEGkErkWE+9SYFU/K4S7GddKL9EBFwecETgJkVpHpYx7p+QjIsHOco= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=b1J/N0Iw; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="b1J/N0Iw" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1762799088; x=1794335088; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9vN9/9HY8bfnpcKi4t8REEnOTQVjCIro+TT1Dr7KppY=; b=b1J/N0IwI2TgTKM4ZJznzzV5WiwYoEZMF5xepiyTrQjFOhyJ4ZSEWdEd qpYRJBs+wSGvTQMf/MkhjHibAyZ6P6+aCq6f1Il/p45TVncUrvZOJTfdb +3La+UDF4jzoRY2mVa2cZuuJw4M0wI7rkchUwyQWCs+zVKBDba5krIsvr NCdkBWxamx/VHo0/Mn8OawCBxOeByxF9ejbXyfFcvhNC0YuIABl+58Z+B N33NGW6FLIWCGUji1vgePdJY9N4UzwRgCN9E3KVAKcLYEVn2DuHoGEr1Z aTGftjKttw1zMRqb/IQVrTmTpxlkhtLHEmL4n1OnVczZuV47qhDTZ/qfW Q==; X-CSE-ConnectionGUID: M48djxRzSvevGe5wRIdFXA== X-CSE-MsgGUID: N6JYxRmYSXiCHy8Qrlnmyg== X-IronPort-AV: E=McAfee;i="6800,10657,11609"; a="76305502" X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="76305502" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2025 10:24:48 -0800 X-CSE-ConnectionGUID: 2QxnF43PTXW32LZ0u1VtHw== X-CSE-MsgGUID: w1w8SdiiRO+hPuLi14TI+Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="219396141" Received: from chang-linux-3.sc.intel.com (HELO chang-linux-3) ([172.25.66.172]) by orviesa002.jf.intel.com with ESMTP; 10 Nov 2025 10:24:48 -0800 From: "Chang S. Bae" To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, chao.gao@intel.com, zhao1.liu@intel.com, chang.seok.bae@intel.com Subject: [PATCH RFC v1 07/20] KVM: nVMX: Support the extended instruction info field Date: Mon, 10 Nov 2025 18:01:18 +0000 Message-ID: <20251110180131.28264-8-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251110180131.28264-1-chang.seok.bae@intel.com> References: <20251110180131.28264-1-chang.seok.bae@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Define the VMCS field offset for the extended instruction information and handle it for nested VMX. When EGPRs are available, VMX provides a new 64-bit field to extend the legacy instruction information, allowing access to the higher register indices. Then, nested VMX needs to propagate this field between L1 and L2. The EGPR checker will be implemented later. Signed-off-by: Chang S. Bae --- RFC note: During the draft, I brought up the offset definition initially for non-nested VMX primarily. Then, I realized the switching helper affects nVMX code anyway. Due to this dependency, this change is placed first together with the offset definition. --- arch/x86/include/asm/vmx.h | 2 ++ arch/x86/kvm/vmx/nested.c | 2 ++ arch/x86/kvm/vmx/vmcs12.c | 1 + arch/x86/kvm/vmx/vmcs12.h | 3 ++- arch/x86/kvm/vmx/vmx.h | 2 ++ 5 files changed, 9 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/vmx.h b/arch/x86/include/asm/vmx.h index c85c50019523..ab0684948c56 100644 --- a/arch/x86/include/asm/vmx.h +++ b/arch/x86/include/asm/vmx.h @@ -264,6 +264,8 @@ enum vmcs_field { PID_POINTER_TABLE_HIGH =3D 0x00002043, GUEST_PHYSICAL_ADDRESS =3D 0x00002400, GUEST_PHYSICAL_ADDRESS_HIGH =3D 0x00002401, + EXTENDED_INSTRUCTION_INFO =3D 0x00002406, + EXTENDED_INSTRUCTION_INFO_HIGH =3D 0x00002407, VMCS_LINK_POINTER =3D 0x00002800, VMCS_LINK_POINTER_HIGH =3D 0x00002801, GUEST_IA32_DEBUGCTL =3D 0x00002802, diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 97ec8e594155..3442610a6b70 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -4798,6 +4798,8 @@ static void prepare_vmcs12(struct kvm_vcpu *vcpu, str= uct vmcs12 *vmcs12, vmcs12->vm_exit_intr_info =3D exit_intr_info; vmcs12->vm_exit_instruction_len =3D exit_insn_len; vmcs12->vmx_instruction_info =3D vmcs_read32(VMX_INSTRUCTION_INFO); + if (vmx_egpr_enabled(vcpu)) + vmcs12->extended_instruction_info =3D vmcs_read64(EXTENDED_INSTRUCTION_= INFO); =20 /* * According to spec, there's no need to store the guest's diff --git a/arch/x86/kvm/vmx/vmcs12.c b/arch/x86/kvm/vmx/vmcs12.c index 4233b5ca9461..ea2b690a419e 100644 --- a/arch/x86/kvm/vmx/vmcs12.c +++ b/arch/x86/kvm/vmx/vmcs12.c @@ -53,6 +53,7 @@ const unsigned short vmcs12_field_offsets[] =3D { FIELD64(XSS_EXIT_BITMAP, xss_exit_bitmap), FIELD64(ENCLS_EXITING_BITMAP, encls_exiting_bitmap), FIELD64(GUEST_PHYSICAL_ADDRESS, guest_physical_address), + FIELD64(EXTENDED_INSTRUCTION_INFO, extended_instruction_info), FIELD64(VMCS_LINK_POINTER, vmcs_link_pointer), FIELD64(GUEST_IA32_DEBUGCTL, guest_ia32_debugctl), FIELD64(GUEST_IA32_PAT, guest_ia32_pat), diff --git a/arch/x86/kvm/vmx/vmcs12.h b/arch/x86/kvm/vmx/vmcs12.h index 4ad6b16525b9..2146e45aaade 100644 --- a/arch/x86/kvm/vmx/vmcs12.h +++ b/arch/x86/kvm/vmx/vmcs12.h @@ -71,7 +71,7 @@ struct __packed vmcs12 { u64 pml_address; u64 encls_exiting_bitmap; u64 tsc_multiplier; - u64 padding64[1]; /* room for future expansion */ + u64 extended_instruction_info; /* * To allow migration of L1 (complete with its L2 guests) between * machines of different natural widths (32 or 64 bit), we cannot have @@ -261,6 +261,7 @@ static inline void vmx_check_vmcs12_offsets(void) CHECK_OFFSET(pml_address, 312); CHECK_OFFSET(encls_exiting_bitmap, 320); CHECK_OFFSET(tsc_multiplier, 328); + CHECK_OFFSET(extended_instruction_info, 336); CHECK_OFFSET(cr0_guest_host_mask, 344); CHECK_OFFSET(cr4_guest_host_mask, 352); CHECK_OFFSET(cr0_read_shadow, 360); diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index 64a0772c883c..b8da6ebc35dc 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -372,6 +372,8 @@ struct vmx_insn_info { union insn_info info; }; =20 +static inline bool vmx_egpr_enabled(struct kvm_vcpu *vcpu __maybe_unused) = { return false; } + static inline struct vmx_insn_info vmx_get_insn_info(struct kvm_vcpu *vcpu= __maybe_unused) { struct vmx_insn_info insn; --=20 2.51.0 From nobody Sun Feb 8 02:56:30 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9B0C033290C; Mon, 10 Nov 2025 18:24:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799091; cv=none; b=tYh4wWeeLyOQSIHbq8s3HQ5RlnVUqM1YPOK2ybVhhjt0R7jVus2P6s1v0uIetmltkjEK71wZ3VIgvZLYQt7SOmFyo1Q5xEr3ahRzEoPb/cRDP00j0flHazQvfqCCwv1Grf9XPnM8gHcyl8lD90WYIYPZeffaXGRbCfN99LvgS6o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799091; c=relaxed/simple; bh=O1ivPnUeRMjHpWyc8+K7OqTxpqzw1Cozcpk6Zyf1pRE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HkhLhvNfRAnO2X7ijaX6vfIEGa40Qw/jfUcDXCjxH4S4sRZecTKnGS/V8ooqcmGQQKlw0tS03w85emokxRVQzZ0rPUtDB7522vozKyvbsiCJiJ3mlpdLBuIRanLpO2HYRkXQ/B9weUGSX18NAEb/pIqzQNClTtcUMf6ykTJETfU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=ioLe45+0; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ioLe45+0" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1762799090; x=1794335090; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=O1ivPnUeRMjHpWyc8+K7OqTxpqzw1Cozcpk6Zyf1pRE=; b=ioLe45+0G1zQRd3yXCI35zv7D0wLtAXylcoB/zISjmMQPxtvjqeuaGAZ WlixTMVWpYvCrVvYr7stZJt+KFBukFRo0ftXc1kc3mIdL9quTa8LRBWUz C/l4DbZOyvKKVvc6H8Ucs8EEBygBxFo0qjGN43aysA3yG7J/dtvvXYLJ/ eKZkWXFz+I2t7dnHridBP1I3oU+2uLW3IzRrkjxVNlP1X4Clj1det+AMl NRju7FCzSdRvXuWYBoFGS3ppAk37kvjppc9V8ungPhXmhTl17wSeABxgc DZhxmLyqL/4oHU3IrUaNLw5wT6rfxFUFzb8jtuRV2gMdRz3v2HXYe48lm g==; X-CSE-ConnectionGUID: 9fCHG+FcShyAJatTGTyo0A== X-CSE-MsgGUID: Gf1pMDYdQcCBnndJJJyy0Q== X-IronPort-AV: E=McAfee;i="6800,10657,11609"; a="76305505" X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="76305505" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2025 10:24:50 -0800 X-CSE-ConnectionGUID: 9P1taJqKT/W+2e+t/rkGsg== X-CSE-MsgGUID: M951pyIKTrGyHaklOzahSw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="219396147" Received: from chang-linux-3.sc.intel.com (HELO chang-linux-3) ([172.25.66.172]) by orviesa002.jf.intel.com with ESMTP; 10 Nov 2025 10:24:50 -0800 From: "Chang S. Bae" To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, chao.gao@intel.com, zhao1.liu@intel.com, chang.seok.bae@intel.com Subject: [PATCH RFC v1 08/20] KVM: VMX: Support extended register index in exit handling Date: Mon, 10 Nov 2025 18:01:19 +0000 Message-ID: <20251110180131.28264-9-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251110180131.28264-1-chang.seok.bae@intel.com> References: <20251110180131.28264-1-chang.seok.bae@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Support to 5-bit register indices in VMCS fields when EGPRs are enabled. Signed-off-by: Chang S. Bae --- RFC note: The "chicken bit" (XCR0.APX) checker is intentionally deferred, as the emulator in the next series will do a similar check. Consolidating the XCR0 handling at the end keeps the logic clearer during the feature exposition. --- arch/x86/kvm/vmx/vmx.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index b8da6ebc35dc..6cf1eb739caf 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -374,12 +374,17 @@ struct vmx_insn_info { =20 static inline bool vmx_egpr_enabled(struct kvm_vcpu *vcpu __maybe_unused) = { return false; } =20 -static inline struct vmx_insn_info vmx_get_insn_info(struct kvm_vcpu *vcpu= __maybe_unused) +static inline struct vmx_insn_info vmx_get_insn_info(struct kvm_vcpu *vcpu) { struct vmx_insn_info insn; =20 - insn.extended =3D false; - insn.info.word =3D vmcs_read32(VMX_INSTRUCTION_INFO); + if (vmx_egpr_enabled(vcpu)) { + insn.extended =3D true; + insn.info.dword =3D vmcs_read64(EXTENDED_INSTRUCTION_INFO); + } else { + insn.extended =3D false; + insn.info.word =3D vmcs_read32(VMX_INSTRUCTION_INFO); + } =20 return insn; } @@ -415,7 +420,10 @@ static __always_inline unsigned long vmx_get_exit_qual= (struct kvm_vcpu *vcpu) =20 static inline int vmx_get_exit_qual_gpr(struct kvm_vcpu *vcpu) { - return (vmx_get_exit_qual(vcpu) >> 8) & 0xf; + if (vmx_egpr_enabled(vcpu)) + return (vmx_get_exit_qual(vcpu) >> 8) & 0x1f; + else + return (vmx_get_exit_qual(vcpu) >> 8) & 0xf; } =20 static __always_inline u32 vmx_get_intr_info(struct kvm_vcpu *vcpu) --=20 2.51.0 From nobody Sun Feb 8 02:56:30 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AF72A333753; Mon, 10 Nov 2025 18:24:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799093; cv=none; b=E4VXrOs9/DMsgzJlz2ENR0inruBSNAFjNGax+d7t+JHmBxzyJHqmIaoRM7aqk3EZS8rtndr+ZGXqdqxgwLnPia2hxnf+PROc0V7B5HXKz2EuBFMwwlgtZAybGJTsW+GsXDqcGW6vFzpyoIqjkXe76N1dp0LSuyNLFRyPehiw3E4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799093; c=relaxed/simple; bh=HiQEVHuWFzVpNQ0uf8gl/Zy8oB0Q8AomnhA0WkRcPck=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XVGFHhjaVN4VX+UVOjd42fbNgPqOMem/MJ5m6elo0dUPIaMdTFD3nHRYhb5Nrx+HFmT3PHQLV6tnnuTwgY5kJOLFUC2htQypnmpIQ1dDgiKN4Lv7kpvYiiVrcOIil60zKoYSkkWp73VetJqcSghyVi9WsoGD6urTzz1xkRF38yc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=NxE3HAFE; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="NxE3HAFE" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1762799092; x=1794335092; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HiQEVHuWFzVpNQ0uf8gl/Zy8oB0Q8AomnhA0WkRcPck=; b=NxE3HAFEmos54iI08O6jqktVOyKwr7mKrUrp70MKpH9y2MEZIzSm92nm BC3qYogQ1YwNcnM+mtA0hsdLZWcpksDu5a9TJiagHF5IKgt/Vs7wxUQ+X uouSIyZr0V+0vCuwnLopHuke7quCRNiEPFMbTWFYW9nnbjgZcwm9auuGm W4/LVkiQ+at5TAgPbk7UEkAMDDqQknFTpz75dE6Ckh9nyJh1jObcuzzUX zBIbddxl0J6SFxVE1y0yxhebPWMhp89ariuc0M5+v8k4hINcG1mRHLE0x Jy3MPfVO24q7PWFjskclNh5gxmdB57OR80op4IT5On49LwSmRqMn9x8bv g==; X-CSE-ConnectionGUID: MDjPeIv3S7eePEYBiqjcTQ== X-CSE-MsgGUID: HvJzp6GyRn6KwR4DDwu5bw== X-IronPort-AV: E=McAfee;i="6800,10657,11609"; a="76305506" X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="76305506" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2025 10:24:51 -0800 X-CSE-ConnectionGUID: p82M8Sg3S5mI+3F3fWp6LQ== X-CSE-MsgGUID: zVPpoz4IQs2N98b4zF4JCg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="219396156" Received: from chang-linux-3.sc.intel.com (HELO chang-linux-3) ([172.25.66.172]) by orviesa002.jf.intel.com with ESMTP; 10 Nov 2025 10:24:52 -0800 From: "Chang S. Bae" To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, chao.gao@intel.com, zhao1.liu@intel.com, chang.seok.bae@intel.com Subject: [PATCH RFC v1 09/20] KVM: x86: Support EGPR accessing and tracking for instruction emulation Date: Mon, 10 Nov 2025 18:01:20 +0000 Message-ID: <20251110180131.28264-10-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251110180131.28264-1-chang.seok.bae@intel.com> References: <20251110180131.28264-1-chang.seok.bae@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extend the emulator context and GPR accessors to handle EGPRs before adding support for REX2-prefixed instructions. Now the KVM GPR accessors can handle EGPRs. Then, the emulator can uniformly cache and track all GPRs without requiring separate handling. Signed-off-by: Chang S. Bae --- arch/x86/kvm/kvm_emulate.h | 10 +++++----- arch/x86/kvm/x86.c | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h index 7b5ddb787a25..153c70ea5561 100644 --- a/arch/x86/kvm/kvm_emulate.h +++ b/arch/x86/kvm/kvm_emulate.h @@ -105,13 +105,13 @@ struct x86_instruction_info { struct x86_emulate_ops { void (*vm_bugged)(struct x86_emulate_ctxt *ctxt); /* - * read_gpr: read a general purpose register (rax - r15) + * read_gpr: read a general purpose register (rax - r31) * * @reg: gpr number. */ ulong (*read_gpr)(struct x86_emulate_ctxt *ctxt, unsigned reg); /* - * write_gpr: write a general purpose register (rax - r15) + * write_gpr: write a general purpose register (rax - r31) * * @reg: gpr number. * @val: value to write. @@ -312,7 +312,7 @@ typedef void (*fastop_t)(struct fastop *); * a ModRM or SIB byte. */ #ifdef CONFIG_X86_64 -#define NR_EMULATOR_GPRS 16 +#define NR_EMULATOR_GPRS 32 #else #define NR_EMULATOR_GPRS 8 #endif @@ -361,9 +361,9 @@ struct x86_emulate_ctxt { u8 lock_prefix; u8 rep_prefix; /* bitmaps of registers in _regs[] that can be read */ - u16 regs_valid; + u32 regs_valid; /* bitmaps of registers in _regs[] that have been written */ - u16 regs_dirty; + u32 regs_dirty; /* modrm */ u8 modrm; u8 modrm_mod; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 603057ea7421..338986a5a3ae 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8812,12 +8812,12 @@ static bool emulator_guest_cpuid_is_intel_compatibl= e(struct x86_emulate_ctxt *ct =20 static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg) { - return kvm_register_read_raw(emul_to_vcpu(ctxt), reg); + return _kvm_gpr_read(emul_to_vcpu(ctxt), reg); } =20 static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg= , ulong val) { - kvm_register_write_raw(emul_to_vcpu(ctxt), reg, val); + _kvm_gpr_write(emul_to_vcpu(ctxt), reg, val); } =20 static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool mask= ed) --=20 2.51.0 From nobody Sun Feb 8 02:56:30 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 54E0B334687; Mon, 10 Nov 2025 18:24:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799094; cv=none; b=MQBW/1iTjnMR5zCkMjr5v2YHVaaXNp8HC1OqA4UvHvErc+0qhxZt9NuShQ1L+8P3TAX4DdCH5vywoLjHe7rOq0xWkke0RXUJNWbw00F8+qv/NXRxnB2vyUfaPxhf5+QabjqEQSk9QtYwmpunTiI/Sqbc8wmuENkvwoPGSlktL1w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799094; c=relaxed/simple; bh=5dYF4iad+H8uTWx9fKhN0f3/Ncx+rJF2lXMG350MeIE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RMPHg4S+qMhh1Ye2aM6pvkmuycYg5WR2qvXt+7v9gDMqHoOEIiwgHgMZHVXumjHwlNwmHPoEO2oTaiv7zLXdNDAvqD5VTSGDTvi9nZIU0A4uR8GiFEBoGTsS3WtIrkk53MAuQ0AwwH76h5h1klpnvLbrKnQj3iyHe74ISx/tUtQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=mzkXQhN9; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="mzkXQhN9" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1762799093; x=1794335093; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5dYF4iad+H8uTWx9fKhN0f3/Ncx+rJF2lXMG350MeIE=; b=mzkXQhN9DMO4C1yL4ENmXMCmyC8ETDbKHwDGAdYo0oUBJBqGKn8QuA29 v98wkj4ZeYOw+EVmQxljeJdcruLnDPHGdMOzUMqGWuRzMbeuR7SYtsJ4p ZX4j5BjUcb73KHMg4OGAEmN66+rEZqDT8fvUN7wj8scHPy+FJFQXJ/c/G VIJCguzYMUmekB4kjii9MAobLmzJTQq4fnuwU7KEGUyK32HBg+E+KFZjo pQC5nWeYJBzye6wfri/rc5sNcrln9IqUNMD5xZ0n5Di3hgxyyWqky+0G3 KFkWpv5mqye/gpqFaV6uEfDY02sY9s4dx+ppm/+7cJ/+wApQAZt9IheBs Q==; X-CSE-ConnectionGUID: 8s2WRLUsTwCrktcs4M89gQ== X-CSE-MsgGUID: EIkXouZPS4irYPvCEe6ZZA== X-IronPort-AV: E=McAfee;i="6800,10657,11609"; a="76305508" X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="76305508" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2025 10:24:53 -0800 X-CSE-ConnectionGUID: qpiy4uSiTOqX6PYOCuK4xw== X-CSE-MsgGUID: rRbWRPE8SKeq1MgqvdYS/g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="219396161" Received: from chang-linux-3.sc.intel.com (HELO chang-linux-3) ([172.25.66.172]) by orviesa002.jf.intel.com with ESMTP; 10 Nov 2025 10:24:53 -0800 From: "Chang S. Bae" To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, chao.gao@intel.com, zhao1.liu@intel.com, chang.seok.bae@intel.com Subject: [PATCH RFC v1 10/20] KVM: x86: Refactor REX prefix handling in instruction emulation Date: Mon, 10 Nov 2025 18:01:21 +0000 Message-ID: <20251110180131.28264-11-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251110180131.28264-1-chang.seok.bae@intel.com> References: <20251110180131.28264-1-chang.seok.bae@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Restructure how to represent and interpret REX fields. Specifically, * Repurpose the existing rex_prefix field to identify the prefix type * Introduce a new union to hold both REX and REX2 bitfields * Update decoder logic to interpret the unified data type Historically, REX used the upper four bits of a signle byte as a fixed identifier, with the lower bits encoded. REX2 extends this to two bytes. The first byte identifies the prefix, and the second encodes additional bits, preserving compatibility with legacy REX encoding. Previously, the emulator stored the REX byte as-is, which cannot capture REX2 semantics. This refactor prepares for REX2 decoding while preserving current behavior. No functional changes intended. Signed-off-by: Chang S. Bae --- arch/x86/kvm/emulate.c | 33 ++++++++++++++++++--------------- arch/x86/kvm/kvm_emulate.h | 31 ++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index 4e3da5b497b8..763fbd139242 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -924,7 +924,7 @@ static void *decode_register(struct x86_emulate_ctxt *c= txt, u8 modrm_reg, int byteop) { void *p; - int highbyte_regs =3D (ctxt->rex_prefix =3D=3D 0) && byteop; + int highbyte_regs =3D (ctxt->rex_prefix =3D=3D REX_NONE) && byteop; =20 if (highbyte_regs && modrm_reg >=3D 4 && modrm_reg < 8) p =3D (unsigned char *)reg_rmw(ctxt, modrm_reg & 3) + 1; @@ -1080,10 +1080,12 @@ static void decode_register_operand(struct x86_emul= ate_ctxt *ctxt, { unsigned int reg; =20 - if (ctxt->d & ModRM) + if (ctxt->d & ModRM) { reg =3D ctxt->modrm_reg; - else - reg =3D (ctxt->b & 7) | ((ctxt->rex_prefix & 1) << 3); + } else { + reg =3D (ctxt->b & 7) | + (ctxt->rex.bits.b3 * BIT(3)); + } =20 if (ctxt->d & Sse) { op->type =3D OP_XMM; @@ -1122,9 +1124,9 @@ static int decode_modrm(struct x86_emulate_ctxt *ctxt, int rc =3D X86EMUL_CONTINUE; ulong modrm_ea =3D 0; =20 - ctxt->modrm_reg =3D ((ctxt->rex_prefix << 1) & 8); /* REX.R */ - index_reg =3D (ctxt->rex_prefix << 2) & 8; /* REX.X */ - base_reg =3D (ctxt->rex_prefix << 3) & 8; /* REX.B */ + ctxt->modrm_reg =3D ctxt->rex.bits.r3 * BIT(3); + index_reg =3D ctxt->rex.bits.x3 * BIT(3); + base_reg =3D ctxt->rex.bits.b3 * BIT(3); =20 ctxt->modrm_mod =3D (ctxt->modrm & 0xc0) >> 6; ctxt->modrm_reg |=3D (ctxt->modrm & 0x38) >> 3; @@ -2466,7 +2468,7 @@ static int em_sysexit(struct x86_emulate_ctxt *ctxt) =20 setup_syscalls_segments(&cs, &ss); =20 - if ((ctxt->rex_prefix & 0x8) !=3D 0x0) + if (ctxt->rex.bits.w) usermode =3D X86EMUL_MODE_PROT64; else usermode =3D X86EMUL_MODE_PROT32; @@ -4851,7 +4853,8 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, vo= id *insn, int insn_len, int case 0x40 ... 0x4f: /* REX */ if (mode !=3D X86EMUL_MODE_PROT64) goto done_prefixes; - ctxt->rex_prefix =3D ctxt->b; + ctxt->rex_prefix =3D REX_PREFIX; + ctxt->rex.raw =3D 0x0f & ctxt->b; continue; case 0xf0: /* LOCK */ ctxt->lock_prefix =3D 1; @@ -4865,15 +4868,14 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, = void *insn, int insn_len, int } =20 /* Any legacy prefix after a REX prefix nullifies its effect. */ - - ctxt->rex_prefix =3D 0; + ctxt->rex_prefix =3D REX_NONE; + ctxt->rex.raw =3D 0; } =20 done_prefixes: =20 - /* REX prefix. */ - if (ctxt->rex_prefix & 8) - ctxt->op_bytes =3D 8; /* REX.W */ + if (ctxt->rex.bits.w) + ctxt->op_bytes =3D 8; =20 /* Opcode byte(s). */ opcode =3D opcode_table[ctxt->b]; @@ -5137,7 +5139,8 @@ void init_decode_cache(struct x86_emulate_ctxt *ctxt) { /* Clear fields that are set conditionally but read without a guard. */ ctxt->rip_relative =3D false; - ctxt->rex_prefix =3D 0; + ctxt->rex_prefix =3D REX_NONE; + ctxt->rex.raw =3D 0; ctxt->lock_prefix =3D 0; ctxt->rep_prefix =3D 0; ctxt->regs_valid =3D 0; diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h index 153c70ea5561..b285299ebfa4 100644 --- a/arch/x86/kvm/kvm_emulate.h +++ b/arch/x86/kvm/kvm_emulate.h @@ -317,6 +317,32 @@ typedef void (*fastop_t)(struct fastop *); #define NR_EMULATOR_GPRS 8 #endif =20 +/* + * REX prefix type to distinguish between no prefix, legacy REX, REX2, + * or an invalid REX2 sequence. + */ +enum rex_type { + REX_NONE, + REX_PREFIX, + REX2_PREFIX, + REX2_INVALID +}; + +/* Unified representation for REX/REX2 prefix bits */ +union rex_field { + struct { + u8 b3 :1, /* REX2.B3 or REX.B */ + x3 :1, /* REX2.X3 or REX.X */ + r3 :1, /* REX2.R3 or REX.R */ + w :1, /* REX2.W or REX.W */ + b4 :1, /* REX2.B4 */ + x4 :1, /* REX2.X4 */ + r4 :1, /* REX2.R4 */ + m0 :1; /* REX2.M0 */ + } bits; + u8 raw; +}; + struct x86_emulate_ctxt { void *vcpu; const struct x86_emulate_ops *ops; @@ -357,7 +383,10 @@ struct x86_emulate_ctxt { int (*check_perm)(struct x86_emulate_ctxt *ctxt); =20 bool rip_relative; - u8 rex_prefix; + /* Type of REX prefix (none, REX, REX2) */ + enum rex_type rex_prefix; + /* Rex bits */ + union rex_field rex; u8 lock_prefix; u8 rep_prefix; /* bitmaps of registers in _regs[] that can be read */ --=20 2.51.0 From nobody Sun Feb 8 02:56:30 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 48A4A334C13; Mon, 10 Nov 2025 18:24:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799096; cv=none; b=Dz2xf2k8k2fzMcwR/8C4j6Gnk5bGUi12ViwhykqMFtLCgRmdpbHSO5WCVMKPoK+PmN6iaIDNg/uihpY+h9Rn0Pi8xYEaiDB010zurtxjBCmV830/qSzKMLxn4NQTpcBeI1LFMNSb3hElrCJWJo0Q0He9c8pjdy5/GuFu3QaHBMA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799096; c=relaxed/simple; bh=iCND/GY/Tsl4VP6nvvL9lsH1G26oXP1yAWuJhJB9LJM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RanyF0aliczjFEg/XzxCph4TnU11wLtBvaMG1VWqvmZwbY5fTbLC1iJPfhtPba4BhoLLIh6X2i6lDvfwlOtSYICrHSbrml764Iwl3XZpLrar5PPdWEn5Cp1UbPJW1nvQZDXlmuGXekOCWRDzVSHqJUZb+SqxOXurFWyS1LWgljY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=fpE6kmuI; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="fpE6kmuI" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1762799095; x=1794335095; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=iCND/GY/Tsl4VP6nvvL9lsH1G26oXP1yAWuJhJB9LJM=; b=fpE6kmuIDyypZJ8ZiqEbUemFm+uBaSAs08SrwTejPy9tsAi9qDlAK8pN k+Y3Q7X5kao5gQYWptlAoTVXZ2PwJ4ItWH+t9JllebIYcCaO95AEnQ62h r5HmJ9H+0vLXmojDPkzhQgNzV0IfYP7wjkllSmwgH0F4jaxB7i+mujROx /Zr17vwUdpeVOkSn7te2LCRFdTFkuX0NBW4Ivm1rKpkZAVcNQy5GpHC63 S9vSG8u885pjg6KoUVKeQc/pJzOn/qNLRyAPfxSnxIFS1nGGiVD5gF00t Jk6RROT2tNWunnQ8GaFjrnpOmEF8J4QrqHEcX1EX5wt1tcoqk6wjXLEjm g==; X-CSE-ConnectionGUID: mV7rBniSQMWuqcw/ftrYCA== X-CSE-MsgGUID: 1gZym6G0SbakwT0zHfqPww== X-IronPort-AV: E=McAfee;i="6800,10657,11609"; a="76305509" X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="76305509" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2025 10:24:55 -0800 X-CSE-ConnectionGUID: hQXlYiacQGSI50wQClE6ag== X-CSE-MsgGUID: 3zChTSL8RC21vL6O8EuRJw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="219396171" Received: from chang-linux-3.sc.intel.com (HELO chang-linux-3) ([172.25.66.172]) by orviesa002.jf.intel.com with ESMTP; 10 Nov 2025 10:24:55 -0800 From: "Chang S. Bae" To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, chao.gao@intel.com, zhao1.liu@intel.com, chang.seok.bae@intel.com Subject: [PATCH RFC v1 11/20] KVM: x86: Refactor opcode table lookup in instruction emulation Date: Mon, 10 Nov 2025 18:01:22 +0000 Message-ID: <20251110180131.28264-12-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251110180131.28264-1-chang.seok.bae@intel.com> References: <20251110180131.28264-1-chang.seok.bae@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Refactor opcode lookup to clearly separate handling of different byte sequences and prefix types, in preparation for REX2 support. The decoder begins with a one-byte opcode table by default and falls through to other tables on escape bytes, but the logic is intertwined and hard to extend. REX2 introduces a dedicated bit in its payload byte to indicate which opcode table to use. To accommodate this mapping bit, the existing lookup path needs to be restructured. No functional changes intended. Signed-off-by: Chang S. Bae --- arch/x86/kvm/emulate.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index 763fbd139242..9c98843094a1 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -4773,7 +4773,6 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, vo= id *insn, int insn_len, int ctxt->_eip =3D ctxt->eip; ctxt->fetch.ptr =3D ctxt->fetch.data; ctxt->fetch.end =3D ctxt->fetch.data + insn_len; - ctxt->opcode_len =3D 1; ctxt->intercept =3D x86_intercept_none; if (insn_len > 0) memcpy(ctxt->fetch.data, insn, insn_len); @@ -4877,20 +4876,24 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, = void *insn, int insn_len, int if (ctxt->rex.bits.w) ctxt->op_bytes =3D 8; =20 - /* Opcode byte(s). */ - opcode =3D opcode_table[ctxt->b]; - /* Two-byte opcode? */ + /* Determine opcode byte(s): */ if (ctxt->b =3D=3D 0x0f) { - ctxt->opcode_len =3D 2; + /* Escape byte: start two-byte opcode sequence */ ctxt->b =3D insn_fetch(u8, ctxt); - opcode =3D twobyte_table[ctxt->b]; - - /* 0F_38 opcode map */ if (ctxt->b =3D=3D 0x38) { + /* Three-byte opcode */ ctxt->opcode_len =3D 3; ctxt->b =3D insn_fetch(u8, ctxt); opcode =3D opcode_map_0f_38[ctxt->b]; + } else { + /* Two-byte opcode */ + ctxt->opcode_len =3D 2; + opcode =3D twobyte_table[ctxt->b]; } + } else { + /* Single-byte opcode */ + ctxt->opcode_len =3D 1; + opcode =3D opcode_table[ctxt->b]; } ctxt->d =3D opcode.flags; =20 --=20 2.51.0 From nobody Sun Feb 8 02:56:30 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1EF04335073; Mon, 10 Nov 2025 18:24:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799098; cv=none; b=DwxI5RJ2kfZm9BU3D9AZ8K1qw6ciYJvp5ayFUdbNen0tjGZJ6bhUFcmgK+Jgt0nQBl6xhEIril1NXbhHLEGBua7n+AU3oMZvd1NHYmWwlIvIVQ36H/EJg+RHU/MxXv7clPkLI76DQiXfAV8zue2hfll3mYaps+LXD2WTg9CpXd0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799098; c=relaxed/simple; bh=jYFK422XN5sDK4Xy8kxd03KOHseSRDbyhejfWJIw0VI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PAAxNuCHmMdFoCkUHyRMu+YRbq5IpJf8PCCDNLJRrjSXojtE/thB/qoQ/YhY2y7CZFWwexzAn/eS4SFguZ3hmXPQYxhlWLEwfPz4mppGDvEODfd/5I00iDRnP5knerePToWXMqxg0v7m/SBS28EyfEkyVb7mLQSFyqAbNk8AJ0E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Vj1TaIsJ; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Vj1TaIsJ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1762799097; x=1794335097; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=jYFK422XN5sDK4Xy8kxd03KOHseSRDbyhejfWJIw0VI=; b=Vj1TaIsJ1jeOujevc6/6D7Z372lGzpXMR2ERdG31TldiBHoZG6iu+slB bOCGbUrdG26obtTLK3HS5+lEoxhQJT8C89PdhM0fSJpUAlB0edOaUaLnI 65+jKK41WDXdaTZJJz/AK8DWv70vvikchg6XSowbxZ8xtVP/gqNOtSQMJ pTlO16ycPake0Px2Dn9bPy451dRyCCqi+T7XsvB4Jo+BcGcdxq54y8oaV RsjCjnQt/UhCu96FgoeMGORk2K3IeVKf+o1XsEaPy/zA2AbTj7CbNdk83 CWP41IfsVHgI20r3bFLjt5HwuqZ+JWH9iv9V4qulBYLhd/C5g32m9ufn4 Q==; X-CSE-ConnectionGUID: tadtMYcHRruXTo3B6EROcg== X-CSE-MsgGUID: 9Kn2Gzf0St2bP5I6+oRTZA== X-IronPort-AV: E=McAfee;i="6800,10657,11609"; a="76305512" X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="76305512" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2025 10:24:57 -0800 X-CSE-ConnectionGUID: 4Up46sQJRluOi5ufeHeApQ== X-CSE-MsgGUID: 1xWoI8fFQnipwr6GO8/8ZA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="219396175" Received: from chang-linux-3.sc.intel.com (HELO chang-linux-3) ([172.25.66.172]) by orviesa002.jf.intel.com with ESMTP; 10 Nov 2025 10:24:57 -0800 From: "Chang S. Bae" To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, chao.gao@intel.com, zhao1.liu@intel.com, chang.seok.bae@intel.com Subject: [PATCH RFC v1 12/20] KVM: x86: Support REX2-extended register index in the decoder Date: Mon, 10 Nov 2025 18:01:23 +0000 Message-ID: <20251110180131.28264-13-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251110180131.28264-1-chang.seok.bae@intel.com> References: <20251110180131.28264-1-chang.seok.bae@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Update register index decoding to account for the additional bit fields introduced by the REX2 prefix. Both ModR/M and opcode register decoding paths now consider the extended index bits (R4, X4, B4) in addition to the legacy REX bits (R3, X3, B3). Signed-off-by: Chang S. Bae --- arch/x86/kvm/emulate.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index 9c98843094a1..ed3a8c0bca20 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -1084,7 +1084,8 @@ static void decode_register_operand(struct x86_emulat= e_ctxt *ctxt, reg =3D ctxt->modrm_reg; } else { reg =3D (ctxt->b & 7) | - (ctxt->rex.bits.b3 * BIT(3)); + (ctxt->rex.bits.b3 * BIT(3)) | + (ctxt->rex.bits.b4 * BIT(4)); } =20 if (ctxt->d & Sse) { @@ -1124,9 +1125,12 @@ static int decode_modrm(struct x86_emulate_ctxt *ctx= t, int rc =3D X86EMUL_CONTINUE; ulong modrm_ea =3D 0; =20 - ctxt->modrm_reg =3D ctxt->rex.bits.r3 * BIT(3); - index_reg =3D ctxt->rex.bits.x3 * BIT(3); - base_reg =3D ctxt->rex.bits.b3 * BIT(3); + ctxt->modrm_reg =3D (ctxt->rex.bits.r3 * BIT(3)) | + (ctxt->rex.bits.r4 * BIT(4)); + index_reg =3D (ctxt->rex.bits.x3 * BIT(3)) | + (ctxt->rex.bits.x4 * BIT(4)); + base_reg =3D (ctxt->rex.bits.b3 * BIT(3)) | + (ctxt->rex.bits.b4 * BIT(4)); =20 ctxt->modrm_mod =3D (ctxt->modrm & 0xc0) >> 6; ctxt->modrm_reg |=3D (ctxt->modrm & 0x38) >> 3; --=20 2.51.0 From nobody Sun Feb 8 02:56:30 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 07877335561; Mon, 10 Nov 2025 18:24:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799100; cv=none; b=Y577w9hNO9niBMtyfzSPkZqOGfTn5nHXcmAQ13K3hGXcK9URpmu+0wTPfyqGirw7y/Ve5D+Z6j8lF3o4Q7n53HzLgjpN8ALMWNiJWq5CPxUazrmuOZIEFLJod5NFzo61jM9FHyiqviBhaSJ3FBjd/+wj5NtKrBiwo0BhaqDkAOU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799100; c=relaxed/simple; bh=EzMDv1UxMuKuzGlYtOJ78agTEmG6+hpaQI8j2t+mVT0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SM45IdIoXkCEgzx3thM0grKZiLXb0FBVrnFLVdUDX2OP9yvN9v6v7lXcjnCiHC7Hv0waxl77FtqaBxBzB6RZBCXfZMnaZ+EUipq+QYNxldcCW8BpcXIFdND7Ya0oCcLV0Q4WVnWyZ4GfIKxdYHbr1E4/vmq0Y2MHMtmQ4nhTuPQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Iqb1AOMr; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Iqb1AOMr" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1762799099; x=1794335099; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=EzMDv1UxMuKuzGlYtOJ78agTEmG6+hpaQI8j2t+mVT0=; b=Iqb1AOMrv7nuadeUfzvcsxa00ywDILa6r1Hc9UyzWqG+V1JvWuGU2EV0 E8Gu+SL8uz9tZ6zu7zOg2a71XSnFZP9l+OoJmhdj202stSJSkItF3RuVI t8xXYHCcrd1TQzu9K5DGn8RIwi8ZjJCnM01mvF213i13bfMklr3YT8Aqb /FJv2LfutWhR8phe+qEOBjO3r81nKVbvk28RRyuKOKAhTiyDOTk5j5+zh nvcuUeligLVvvnPcg+xUkFTJg2iZS8rw57D8NK2Mnoctt4bMXLUIGFCW7 tg5BKji/XnQq6SlL/9r6lYeL0NVDP05N7EQG5mABZ8zbK7GbwXgbT5X5d A==; X-CSE-ConnectionGUID: WImzNt/bTLyfgWYEB+MmqQ== X-CSE-MsgGUID: oomtmbWhRX+ICnbkswCQ3Q== X-IronPort-AV: E=McAfee;i="6800,10657,11609"; a="76305514" X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="76305514" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2025 10:24:59 -0800 X-CSE-ConnectionGUID: e8mBaDGqR32p+KuNOIYqVg== X-CSE-MsgGUID: 2//SVxCvTUSxlESXbQDVLA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="219396179" Received: from chang-linux-3.sc.intel.com (HELO chang-linux-3) ([172.25.66.172]) by orviesa002.jf.intel.com with ESMTP; 10 Nov 2025 10:24:59 -0800 From: "Chang S. Bae" To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, chao.gao@intel.com, zhao1.liu@intel.com, chang.seok.bae@intel.com Subject: [PATCH RFC v1 13/20] KVM: x86: Add REX2 opcode tables to the instruction decoder Date: Mon, 10 Nov 2025 18:01:24 +0000 Message-ID: <20251110180131.28264-14-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251110180131.28264-1-chang.seok.bae@intel.com> References: <20251110180131.28264-1-chang.seok.bae@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extend the decoder to find REX2-prefixed opcodes by introducing dedicated REX2 opcode tables. During initialization, clone the legacy opcode tables and patch entries that differ under REX2. Although most REX2-prefixed opcodes follow the legacy tables, some differ for instructions that do not reference extended register bits or are newly introduced under REX2. Using separate tables simplifies the lookup logic and allows efficient patching of exceptions. The EGPR checker will be implemented later. Signed-off-by: Chang S. Bae --- RFC note: The lookup logic could be separated from the table population, but keeping the user of the tables close to their initialization helps clarify the purpose of the new table. If this becomes hard to follow, splitting the lookup separately can be an option. --- arch/x86/kvm/emulate.c | 73 +++++++++++++++++++++++++++++++++++++- arch/x86/kvm/kvm_emulate.h | 2 ++ arch/x86/kvm/x86.c | 1 + 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index ed3a8c0bca20..58879a31abcd 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -4475,6 +4475,19 @@ static const struct opcode opcode_map_0f_38[256] =3D= { N, N, X4(N), X8(N) }; =20 +/* + * REX2 opcode tables. + * + * REX2-prefixed opcodes mostly follow the legacy tables but differ slight= ly + * for instructions that do not use R/X/B register bits. Initialize the RE= X2 + * tables by copying the legacy ones, then mark mismatched rows as undefin= ed. + */ +static struct opcode rex2_opcode_table[256] __ro_after_init; +static struct opcode rex2_twobyte_table[256] __ro_after_init; + +static const struct opcode undefined =3D D(Undefined); +static const struct opcode notimpl =3D N; + #undef D #undef N #undef G @@ -4761,6 +4774,11 @@ static int decode_operand(struct x86_emulate_ctxt *c= txt, struct operand *op, return rc; } =20 +static inline bool emul_egpr_enabled(struct x86_emulate_ctxt *ctxt __maybe= _unused) +{ + return false; +} + int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_le= n, int emulation_type) { int rc =3D X86EMUL_CONTINUE; @@ -4881,7 +4899,24 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, v= oid *insn, int insn_len, int ctxt->op_bytes =3D 8; =20 /* Determine opcode byte(s): */ - if (ctxt->b =3D=3D 0x0f) { + if (ctxt->rex_prefix =3D=3D REX2_INVALID) { + /* + * A REX2 prefix was detected, but the prefix decoder + * found invalid byte sequence. + */ + opcode =3D undefined; + } else if (ctxt->rex_prefix =3D=3D REX2_PREFIX) { + /* REX2 prefix is only valid when EGPRs are enabled. */ + if (!emul_egpr_enabled(ctxt)) { + opcode =3D undefined; + } else if (ctxt->rex.bits.m0) { + ctxt->opcode_len =3D 2; + opcode =3D rex2_twobyte_table[ctxt->b]; + } else { + ctxt->opcode_len =3D 1; + opcode =3D rex2_opcode_table[ctxt->b]; + } + } else if (ctxt->b =3D=3D 0x0f) { /* Escape byte: start two-byte opcode sequence */ ctxt->b =3D insn_fetch(u8, ctxt); if (ctxt->b =3D=3D 0x38) { @@ -5526,3 +5561,39 @@ bool emulator_can_use_gpa(struct x86_emulate_ctxt *c= txt) =20 return true; } + +static void undefine_row(struct opcode *row) +{ + struct opcode *ptr =3D row; + int i; + + /* Clear 16 entries per row */ + for (i =3D 0; i < 0x10; i++, ptr++) + *ptr =3D undefined; +} + +/* + * Populate REX2 opcode table: + * + * REX2-prefixed opcodes mostly reuse the legacy layout, except for those = that + * neither reference extended register bits nor are newly introduced under= the + * REX2 prefix. Initialize both single- and two-byte tables by cloning the + * legacy versions, then patch the table for some exceptions. + */ +void __init kvm_init_rex2_opcode_table(void) +{ + /* Copy legacy tables: */ + memcpy(rex2_opcode_table, opcode_table, sizeof(opcode_table)); + memcpy(rex2_twobyte_table, twobyte_table, sizeof(twobyte_table)); + + /* Undefine reserved opcode ranges: */ + undefine_row(&rex2_opcode_table[0x40]); + undefine_row(&rex2_opcode_table[0x70]); + undefine_row(&rex2_opcode_table[0xa0]); + undefine_row(&rex2_opcode_table[0xe0]); + undefine_row(&rex2_twobyte_table[0x30]); + undefine_row(&rex2_twobyte_table[0x80]); + + /* Mark opcode not yet implemented: */ + rex2_opcode_table[0xa1] =3D notimpl; +} diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h index b285299ebfa4..cc16211d61f6 100644 --- a/arch/x86/kvm/kvm_emulate.h +++ b/arch/x86/kvm/kvm_emulate.h @@ -589,4 +589,6 @@ static inline ulong *reg_rmw(struct x86_emulate_ctxt *c= txt, unsigned nr) return reg_write(ctxt, nr); } =20 +void __init kvm_init_rex2_opcode_table(void); + #endif /* _ASM_X86_KVM_X86_EMULATE_H */ diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 338986a5a3ae..4c8c2fc3bda6 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -14354,6 +14354,7 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_rmp_fault); static int __init kvm_x86_init(void) { kvm_init_xstate_sizes(); + kvm_init_rex2_opcode_table(); =20 kvm_mmu_x86_module_init(); mitigate_smt_rsb &=3D boot_cpu_has_bug(X86_BUG_SMT_RSB) && cpu_smt_possib= le(); --=20 2.51.0 From nobody Sun Feb 8 02:56:30 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BFC143358C1; Mon, 10 Nov 2025 18:25:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799102; cv=none; b=DtnV6EQuxRVJte1wNJpjbEjHvBiTPqRFmIhg7jUyCTu9RHqFmKvLcKQuKWAR2auDNS3dqZ1YyQUMnsV+cA954PwxDC2iCc9hKLxoaj3UDz+JlMy1uQFA0beM/2fmVgA9AGQEV4CeWoSgGzMnj3BAsjev/QIB6uRPSuraRKCZI8I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799102; c=relaxed/simple; bh=OKF1FVbUobLfexLIDLKx2QfWN1Gg7JvJuzC6gMCI7cc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AMW3Cb1waiM88KdONYbgTTWCw3V0VACu2Bjb/GKo75vZzfW/ByHduav4xf8OKdMG0Cy/KfdRqYupp30L6+ca9o1TbLfuEevmgsAh2r3/x0UH8fre1ypyAZAY1dfF2FJJ1Gm2m1ObuAdIUIMiN8+J50NZaBavWV465+Rc5U0q6PU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=DPb9rnW1; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="DPb9rnW1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1762799101; x=1794335101; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=OKF1FVbUobLfexLIDLKx2QfWN1Gg7JvJuzC6gMCI7cc=; b=DPb9rnW1d4wyoPR8HZ1IoUf7lxz4Z1/n+RGvHB7gMNTV/+tgCjbPBxkv miPaJQCDllrR8vAk9rwq/k/42pcgCIThOhEUAvRJS14dRZPdVNc+l2DE6 SkPvjlBw4ysSQBPRVq+85/pnWHbiuTIXZnE916pgfJGASTa+KvIuqsp69 QPMmPcihH7jIqo9xmu7dQlPJhZhIU4ueN//bdtidCE7yzQcy9F9bjFdJM 1VJmnelNH6V/FK1aRgz/vRfcqWzsdQbvILoSPrATtyk3JW1E/kgfKfRm6 n9OCwHp4STqOzI8p+o3o39IcRT6ayUHZRpuvZXGUl/DzB0/lLh80Dpnyz Q==; X-CSE-ConnectionGUID: rNXvZYh/T/msRTpuFBABPA== X-CSE-MsgGUID: ArYNEKU1S9yjRfl5qo84hQ== X-IronPort-AV: E=McAfee;i="6800,10657,11609"; a="76305517" X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="76305517" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2025 10:25:01 -0800 X-CSE-ConnectionGUID: K2yW8NtuRD6FifplzeX6Fg== X-CSE-MsgGUID: DjKKLPLpT66baPklZroZBQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="219396191" Received: from chang-linux-3.sc.intel.com (HELO chang-linux-3) ([172.25.66.172]) by orviesa002.jf.intel.com with ESMTP; 10 Nov 2025 10:25:01 -0800 From: "Chang S. Bae" To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, chao.gao@intel.com, zhao1.liu@intel.com, chang.seok.bae@intel.com Subject: [PATCH RFC v1 14/20] KVM: x86: Emulate REX2-prefixed 64-bit absolute jump Date: Mon, 10 Nov 2025 18:01:25 +0000 Message-ID: <20251110180131.28264-15-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251110180131.28264-1-chang.seok.bae@intel.com> References: <20251110180131.28264-1-chang.seok.bae@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support for the new absolute jump, previously unimplemented. This instruction has an unusual quirk: the REX2.W bit uses inverted polarity. Unlike normal REX or REX2 semantics (where W=3D1 indicates a 64-bit operand size), this instruction uses W=3D0 to select an 8-byte operand size. The new InvertedWidthPolarity flag and its helper to interpret the W bit correctly, avoiding special-case hacks in the emulator logic. Since the ctxt->op_bytes depends on the instruction flags, the size should be determined after the instruction lookup. Signed-off-by: Chang S. Bae --- arch/x86/kvm/emulate.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index 58879a31abcd..03f8e007b14e 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -179,6 +179,7 @@ #define TwoMemOp ((u64)1 << 55) /* Instruction has two memory operand = */ #define IsBranch ((u64)1 << 56) /* Instruction is considered a branch.= */ #define ShadowStack ((u64)1 << 57) /* Instruction affects Shadow Stacks. = */ +#define InvertedWidthPolarity ((u64)1 << 58) /* Instruction uses inverted = REX2.W polarity */ =20 #define DstXacc (DstAccLo | SrcAccHi | SrcWrite) =20 @@ -993,6 +994,16 @@ EM_ASM_2W(btc); =20 EM_ASM_2R(cmp, cmp_r); =20 +static inline bool is_64bit_operand_size(struct x86_emulate_ctxt *ctxt) +{ + /* + * Most instructions interpret REX.W=3D1 as 64-bit operand size. + * Some REX2 opcodes invert this logic. + */ + return ctxt->d & InvertedWidthPolarity ? + ctxt->rex.bits.w =3D=3D 0 : ctxt->rex.bits.w =3D=3D 1; +} + static int em_bsf_c(struct x86_emulate_ctxt *ctxt) { /* If src is zero, do not writeback, but update flags */ @@ -2472,7 +2483,7 @@ static int em_sysexit(struct x86_emulate_ctxt *ctxt) =20 setup_syscalls_segments(&cs, &ss); =20 - if (ctxt->rex.bits.w) + if (is_64bit_operand_size(ctxt)) usermode =3D X86EMUL_MODE_PROT64; else usermode =3D X86EMUL_MODE_PROT32; @@ -4486,7 +4497,8 @@ static struct opcode rex2_opcode_table[256] __ro_aft= er_init; static struct opcode rex2_twobyte_table[256] __ro_after_init; =20 static const struct opcode undefined =3D D(Undefined); -static const struct opcode notimpl =3D N; +static const struct opcode pfx_d5_a1 =3D I(SrcImm64 | NearBranch | IsBranc= h | InvertedWidthPolarity, \ + em_jmp_abs); =20 #undef D #undef N @@ -4543,6 +4555,7 @@ static bool is_ibt_instruction(struct x86_emulate_ctx= t *ctxt) return true; case SrcNone: case SrcImm: + case SrcImm64: case SrcImmByte: /* * Note, ImmU16 is used only for the stack adjustment operand on ENTER @@ -4895,9 +4908,6 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, vo= id *insn, int insn_len, int =20 done_prefixes: =20 - if (ctxt->rex.bits.w) - ctxt->op_bytes =3D 8; - /* Determine opcode byte(s): */ if (ctxt->rex_prefix =3D=3D REX2_INVALID) { /* @@ -4936,6 +4946,9 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, vo= id *insn, int insn_len, int } ctxt->d =3D opcode.flags; =20 + if (is_64bit_operand_size(ctxt)) + ctxt->op_bytes =3D 8; + if (ctxt->d & ModRM) ctxt->modrm =3D insn_fetch(u8, ctxt); =20 @@ -5594,6 +5607,6 @@ void __init kvm_init_rex2_opcode_table(void) undefine_row(&rex2_twobyte_table[0x30]); undefine_row(&rex2_twobyte_table[0x80]); =20 - /* Mark opcode not yet implemented: */ - rex2_opcode_table[0xa1] =3D notimpl; + /* Define the REX2-specific absolute jump (0xA1) opcode */ + rex2_opcode_table[0xa1] =3D pfx_d5_a1; } --=20 2.51.0 From nobody Sun Feb 8 02:56:30 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8B85C336ED8; Mon, 10 Nov 2025 18:25:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799104; cv=none; b=DpPNUox/af9XQy4DJpUcqUZwGGvRu1fti/A+FYrD/igFoAIWRdnhoM0yZvPf2c8Nrol+nQs6bbXSSrtNygs8yW2IBokczAitPCHzQJe+BLLwGFvCiTKcDxB4F9nluPcpUnLv62REzbawKHWddfpweY9PRAGYg9QvdbsCiJCk4BQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799104; c=relaxed/simple; bh=tqLsilKH4UEbDrll02cVHYgerxD+VGQCU4DswF/lNkI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lJaIPv1oFJ1GIscdp8cOOaUzRYbKzmIPLR5ElKCJWMQFI6KN6pDicAaZjvi+vWoTxPZLMPO0HsCcgeYWlomAuF5w6c7Pb3pdaMg4n9KR5ZRFnj0MGFZPnnUZocC9CxWJkQK94OO0E3ezRq4UMI+hTwCKzYS7lVo3ojgdIgHUJ8k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=PoXDQOIp; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="PoXDQOIp" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1762799103; x=1794335103; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=tqLsilKH4UEbDrll02cVHYgerxD+VGQCU4DswF/lNkI=; b=PoXDQOIpLvcrY4mXia4H9rvui3XGsMZk9d3A1ro80nvMSn38xiZispO9 RAIY3FnGjKZj6WDE8qzebUCeekXHnd8nmSaYzIq4kczpVSJk912F9d/g3 Z8UEF9O6HNAi26RUcKwBvdZoCFfoc7JtwNfl0krAwFB9kUpv/IkcyhFQ+ 3EGJEEs6U8OF6EF0dFRPsnLXNg5zKkFGwFYUYkno1dJ2QBTpfaJSNbB04 FamwyZF8O90yPsw79CHYU+F9w0yDSiwDQf1o8XP4q4Wqgk2krp6PBKxwn w1nmv3fHpAgXfKTAsgR5cT5STnoyYQEPJOh4RN6gZllK642hjefjFtwur g==; X-CSE-ConnectionGUID: +Y0wD/YZT324Ydvcd7LxGw== X-CSE-MsgGUID: WYSieWSpR9KIFW/l7sI/wg== X-IronPort-AV: E=McAfee;i="6800,10657,11609"; a="76305520" X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="76305520" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2025 10:25:02 -0800 X-CSE-ConnectionGUID: 01SKmm4dQASedW0QvfwzYQ== X-CSE-MsgGUID: dt8VZuIiTHawoBgDxdjc5A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="219396202" Received: from chang-linux-3.sc.intel.com (HELO chang-linux-3) ([172.25.66.172]) by orviesa002.jf.intel.com with ESMTP; 10 Nov 2025 10:25:03 -0800 From: "Chang S. Bae" To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, chao.gao@intel.com, zhao1.liu@intel.com, chang.seok.bae@intel.com Subject: [PATCH RFC v1 15/20] KVM: x86: Reject EVEX-prefix instructions in the emulator Date: Mon, 10 Nov 2025 18:01:26 +0000 Message-ID: <20251110180131.28264-16-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251110180131.28264-1-chang.seok.bae@intel.com> References: <20251110180131.28264-1-chang.seok.bae@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Explicitly mark EVEX-prefixed opcodes (0x62) as unsupported, clarifying current decoding behavior. While new prefixes like REX2 extend GPR handling, EVEX emulation should be addressed separately once after VEX support is implemented. Signed-off-by: Chang S. Bae --- arch/x86/kvm/emulate.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index 03f8e007b14e..9bd61ea496e5 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -4952,8 +4952,8 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, vo= id *insn, int insn_len, int if (ctxt->d & ModRM) ctxt->modrm =3D insn_fetch(u8, ctxt); =20 - /* vex-prefix instructions are not implemented */ - if (ctxt->opcode_len =3D=3D 1 && (ctxt->b =3D=3D 0xc5 || ctxt->b =3D=3D 0= xc4) && + /* VEX and EVEX-prefixed instructions are not implemented */ + if (ctxt->opcode_len =3D=3D 1 && (ctxt->b =3D=3D 0xc5 || ctxt->b =3D=3D 0= xc4 || ctxt->b =3D=3D 0x62) && (mode =3D=3D X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) =3D=3D 0xc0)= ) { ctxt->d =3D NotImpl; } --=20 2.51.0 From nobody Sun Feb 8 02:56:30 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4577233710F; Mon, 10 Nov 2025 18:25:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799105; cv=none; b=FQQgwFxKsfvSZAHQ/zmoGNSxbVvx6V7owefja9tw4f7se57/OjxrVtzAOLtO1LMsa1fl4DUZN4zE1jBU4gGtj4E4jCF1EyW2GgIWStqDgp0CqllaflCAPcAwfPslGq0b8a7SnsB1m0CS8w34izg+m3fR3ta9WsG9bgk3wx2TylM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799105; c=relaxed/simple; bh=EkIlUTsAjCozM0tNyupgDOe46m8fi+dk3CanNhD+v2Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UdgCq2ljdRzZfO8V80Ja7XeUBcvCIccyo0cVaJIDR+8bhsPeD/vLxVDIa8dd8+Td/WLLIL8q5MLul0rNkc3d4wvOh5P3tZUWu02eXP5OoobMYo8YH71+PuSGhjdzgR8oBcTiaQ+k41/29A15wYfi6EEI/dLARBw1hIdAnXqWsRQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=AFvhdxSC; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="AFvhdxSC" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1762799104; x=1794335104; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=EkIlUTsAjCozM0tNyupgDOe46m8fi+dk3CanNhD+v2Q=; b=AFvhdxSCVutr+PB+LZipZMyMewoZxKsLCZhQmIxVFjOp24ccBCqEVDnM +PvTE9MzBFsZlObmVr2WC0vo2qDKoiQRzJT9d/dawFq9+U8iI+zy3B/Yn x1ALNtwmXwFCZE70p7l44pwm8bQ7WzcsVe3qU7tYek9Zt1WHoeyfqy9tR en3v7+H5H0PcWxTmO4RoZFhp7T0D1nO4gJXIyQqI97uNyhmWiV6Bu4ZHP +cRgNN9mDqTo9xBxp5tqT2MaTrWA7V/J27xlgzTjIs2m0DBb3ynis2kuE X0Oyn3f9HwX9S+dTUB5TAkMYsG5gja9Hnlf8uy9yCUnkkE3aJEiDnYJQJ Q==; X-CSE-ConnectionGUID: nX4SsH2fSAKzUYhfUyhE5w== X-CSE-MsgGUID: pgwRUCG9SOSNDlKUtJgP3A== X-IronPort-AV: E=McAfee;i="6800,10657,11609"; a="76305521" X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="76305521" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2025 10:25:04 -0800 X-CSE-ConnectionGUID: FxIUYRg+Tr2eJNmT/rxdsg== X-CSE-MsgGUID: QQsX7R7CR42UtpyQ9uljeA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="219396213" Received: from chang-linux-3.sc.intel.com (HELO chang-linux-3) ([172.25.66.172]) by orviesa002.jf.intel.com with ESMTP; 10 Nov 2025 10:25:04 -0800 From: "Chang S. Bae" To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, chao.gao@intel.com, zhao1.liu@intel.com, chang.seok.bae@intel.com Subject: [PATCH RFC v1 16/20] KVM: x86: Decode REX2 prefix in the emulator Date: Mon, 10 Nov 2025 18:01:27 +0000 Message-ID: <20251110180131.28264-17-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251110180131.28264-1-chang.seok.bae@intel.com> References: <20251110180131.28264-1-chang.seok.bae@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extend the instruction emulator to recognize and interpret the REX2 prefix byte. Also, detect and flag invalid prefix sequences after a REX2 prefix. In the existing prefix-decoding loop, * The loop exits when the first non-prefix byte is encountered. * Any non-REX prefix clears previously recorded REX information. For REX2, however, once a REX2 prefix is encountered, most subsequent prefixes are invalid. So, each subsequent prefix needs to be validated before continuing the loop. Signed-off-by: Chang S. Bae --- RFC note: The REX2 decoding itself is straightforward. The additional logic is mainly to detect and handle invalid prefix sequences. If this seems excessive, there is a chance to cut off this check since VMX would raise '#UD' on such cases anyway. --- arch/x86/kvm/emulate.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index 9bd61ea496e5..f9381a4055d6 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -4844,7 +4844,7 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, vo= id *insn, int insn_len, int ctxt->op_bytes =3D def_op_bytes; ctxt->ad_bytes =3D def_ad_bytes; =20 - /* Legacy prefixes. */ + /* Legacy and REX/REX2 prefixes. */ for (;;) { switch (ctxt->b =3D insn_fetch(u8, ctxt)) { case 0x66: /* operand-size override */ @@ -4887,9 +4887,20 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, v= oid *insn, int insn_len, int case 0x40 ... 0x4f: /* REX */ if (mode !=3D X86EMUL_MODE_PROT64) goto done_prefixes; + if (ctxt->rex_prefix =3D=3D REX2_PREFIX) + break; ctxt->rex_prefix =3D REX_PREFIX; ctxt->rex.raw =3D 0x0f & ctxt->b; continue; + case 0xd5: /* REX2 */ + if (mode !=3D X86EMUL_MODE_PROT64) + goto done_prefixes; + if (ctxt->rex_prefix =3D=3D REX2_PREFIX && + ctxt->rex.bits.m0 =3D=3D 0) + break; + ctxt->rex_prefix =3D REX2_PREFIX; + ctxt->rex.raw =3D insn_fetch(u8, ctxt); + continue; case 0xf0: /* LOCK */ ctxt->lock_prefix =3D 1; break; @@ -4901,6 +4912,17 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, v= oid *insn, int insn_len, int goto done_prefixes; } =20 + if (ctxt->rex_prefix =3D=3D REX2_PREFIX) { + /* + * A legacy or REX prefix following a REX2 prefix + * forms an invalid byte sequences. Likewise, + * a second REX2 prefix following a REX2 prefix + * with M0=3D0 is invalid. + */ + ctxt->rex_prefix =3D REX2_INVALID; + goto done_prefixes; + } + /* Any legacy prefix after a REX prefix nullifies its effect. */ ctxt->rex_prefix =3D REX_NONE; ctxt->rex.raw =3D 0; --=20 2.51.0 From nobody Sun Feb 8 02:56:30 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 44E7B337BA7; Mon, 10 Nov 2025 18:25:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799107; cv=none; b=envLFZQEDQcfV4v2enYQEDSpb4wjilD7md30zSw9IYiVb9KW9/rjJPLg5R66cuS+CSQl+d1o2YpuoG0+xt5eqIyNj2lw5/0N49wSE6S57cODfDiFnLPPfpKxGNl2drAZBPOBUJWQi+c6Hh/chartloEUsW7ioj33j4lW898j2eI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799107; c=relaxed/simple; bh=yv77DOMcvVXHx2A9/x6zUQan80ucLdQCNqzpepizjPU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PFA7tejqrYHt/L/KaJ/PPuq/JaWBFqa7hmxOBNBTnDNTAKWCY+HwlyTU/VZrmMYfR++zITUcY+c4v32+1G3aeMr1TTY2gi2tizgcVokZbHqz9vxXhTdvOdY2BgHu1ka4qSwWGepTdArOHa7VnrLHKx7YUe2uapGbZZeL2RHkhoI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=BZJ2Qr4o; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="BZJ2Qr4o" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1762799106; x=1794335106; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=yv77DOMcvVXHx2A9/x6zUQan80ucLdQCNqzpepizjPU=; b=BZJ2Qr4o3TH/a76tMWsl6fVmOeLQnTXvV8VE3gLkhyfSgsrNlZXnr5E0 oqPEgerHbJNtgYUHLhXhllWEX1A51ZxWd8Ximgx5X37XypY/NIDQKzCUx qWwLNCPqjWMk7BVOG1FL3CnfFHprdZW7biydnPb5cnWPLR/8gpn5fzrMm lmhJIqoKwX6ckCcBmNE0p0UTqWjR5PsvLA6jQ6PLTQR0aPjo7WLXJ0UNy xzGP/xInoNftQSvu9PK/2EKE1Iv5843aT+LbSvIEXwjq1QZPwcPSo9w++ 7mA/+DA/PVTOOM0yQG6Wq6YGSeqKBymMbYITrk1PctbSq5kleLoEpu6zE g==; X-CSE-ConnectionGUID: zH2AhCNTT/uwx6f2xBG0eg== X-CSE-MsgGUID: 802gRZkLQWiJ4oGn1e2wTw== X-IronPort-AV: E=McAfee;i="6800,10657,11609"; a="76305523" X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="76305523" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2025 10:25:06 -0800 X-CSE-ConnectionGUID: XSsQtCZATrG/HScYGLFBmQ== X-CSE-MsgGUID: VHm/Ym8GQU+P2xtd5K4AQQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="219396233" Received: from chang-linux-3.sc.intel.com (HELO chang-linux-3) ([172.25.66.172]) by orviesa002.jf.intel.com with ESMTP; 10 Nov 2025 10:25:06 -0800 From: "Chang S. Bae" To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, chao.gao@intel.com, zhao1.liu@intel.com, chang.seok.bae@intel.com Subject: [PATCH RFC v1 17/20] KVM: x86: Prepare APX state setting in XCR0 Date: Mon, 10 Nov 2025 18:01:28 +0000 Message-ID: <20251110180131.28264-18-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251110180131.28264-1-chang.seok.bae@intel.com> References: <20251110180131.28264-1-chang.seok.bae@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Prepare the APX state enabling in XCR0 by implementing the previous placeholders and ensuring its readiness. APX introduces EGPRs, tracked as XSTATE component 19. Like other XSAVE-managed states, EGPR availability is controlled through XCR0, and the registers are accessible only in 64-bit mode. At this point, only VMX supports EGPRs. SVM will require corresponding extensions to handle EGPR indices. The addition to the supported XCR0 mask should accompany guest CPUID exposure, which will be done separately. Signed-off-by: Chang S. Bae --- RFC note Not all callers may need to validate the XCR0 bit -- maybe a capability bit. However, every exit associated with EGPRs should already have that control bit set in the first place. Checking it explicitly does not charge additional cost, so I have this for consistency. --- arch/x86/kvm/emulate.c | 9 +++++++-- arch/x86/kvm/kvm_cache_regs.h | 1 + arch/x86/kvm/kvm_emulate.h | 1 + arch/x86/kvm/svm/svm.c | 7 ++++++- arch/x86/kvm/vmx/vmx.h | 9 ++++++++- arch/x86/kvm/x86.c | 11 +++++++++++ 6 files changed, 34 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index f9381a4055d6..ba3020e6f469 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -4787,9 +4787,14 @@ static int decode_operand(struct x86_emulate_ctxt *c= txt, struct operand *op, return rc; } =20 -static inline bool emul_egpr_enabled(struct x86_emulate_ctxt *ctxt __maybe= _unused) +/* EGPR availability is controlled by the APX feature bit in XCR0. */ +static inline bool emul_egpr_enabled(struct x86_emulate_ctxt *ctxt) { - return false; + u64 xcr0; + + ctxt->ops->get_xcr(ctxt, XCR_XFEATURE_ENABLED_MASK, &xcr0); + + return xcr0 & XFEATURE_MASK_APX; } =20 int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_le= n, int emulation_type) diff --git a/arch/x86/kvm/kvm_cache_regs.h b/arch/x86/kvm/kvm_cache_regs.h index 8ddb01191d6f..acdb3751317c 100644 --- a/arch/x86/kvm/kvm_cache_regs.h +++ b/arch/x86/kvm/kvm_cache_regs.h @@ -3,6 +3,7 @@ #define ASM_KVM_CACHE_REGS_H =20 #include +#include =20 #define KVM_POSSIBLE_CR0_GUEST_BITS (X86_CR0_TS | X86_CR0_WP) #define KVM_POSSIBLE_CR4_GUEST_BITS \ diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h index cc16211d61f6..673a82532c78 100644 --- a/arch/x86/kvm/kvm_emulate.h +++ b/arch/x86/kvm/kvm_emulate.h @@ -237,6 +237,7 @@ struct x86_emulate_ops { bool (*is_smm)(struct x86_emulate_ctxt *ctxt); int (*leave_smm)(struct x86_emulate_ctxt *ctxt); void (*triple_fault)(struct x86_emulate_ctxt *ctxt); + int (*get_xcr)(struct x86_emulate_ctxt *ctxt, u32 index, u64 *xcr); int (*set_xcr)(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr); =20 gva_t (*get_untagged_addr)(struct x86_emulate_ctxt *ctxt, gva_t addr, diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 3aa2c37754ef..e6a082686000 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -5288,8 +5288,13 @@ static __init int svm_hardware_setup(void) } kvm_enable_efer_bits(EFER_NX); =20 + /* + * APX introduces EGPRs, which require additional VMCB support. + * Disable APX until the necessary extensions are handled. + */ kvm_caps.supported_xcr0 &=3D ~(XFEATURE_MASK_BNDREGS | - XFEATURE_MASK_BNDCSR); + XFEATURE_MASK_BNDCSR | + XFEATURE_MASK_APX); =20 if (boot_cpu_has(X86_FEATURE_FXSR_OPT)) kvm_enable_efer_bits(EFER_FFXSR); diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index 6cf1eb739caf..784aa0504dce 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -372,7 +372,14 @@ struct vmx_insn_info { union insn_info info; }; =20 -static inline bool vmx_egpr_enabled(struct kvm_vcpu *vcpu __maybe_unused) = { return false; } +/* + * EGPR availability is controlled by the APX xfeature bit in XCR0 and is + * only accessible in 64-bit mode. + */ +static inline bool vmx_egpr_enabled(struct kvm_vcpu *vcpu) +{ + return vcpu->arch.xcr0 & XFEATURE_MASK_APX && is_64_bit_mode(vcpu); +} =20 static inline struct vmx_insn_info vmx_get_insn_info(struct kvm_vcpu *vcpu) { diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 4c8c2fc3bda6..e087db0f4153 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8843,6 +8843,16 @@ static void emulator_triple_fault(struct x86_emulate= _ctxt *ctxt) kvm_make_request(KVM_REQ_TRIPLE_FAULT, emul_to_vcpu(ctxt)); } =20 +static int emulator_get_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 = *xcr) +{ + /* Only support XCR_XFEATURE_ENABLED_MASK now */ + if (index !=3D XCR_XFEATURE_ENABLED_MASK) + return 1; + + *xcr =3D emul_to_vcpu(ctxt)->arch.xcr0; + return 0; +} + static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 = xcr) { return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr); @@ -8915,6 +8925,7 @@ static const struct x86_emulate_ops emulate_ops =3D { .is_smm =3D emulator_is_smm, .leave_smm =3D emulator_leave_smm, .triple_fault =3D emulator_triple_fault, + .get_xcr =3D emulator_get_xcr, .set_xcr =3D emulator_set_xcr, .get_untagged_addr =3D emulator_get_untagged_addr, .is_canonical_addr =3D emulator_is_canonical_addr, --=20 2.51.0 From nobody Sun Feb 8 02:56:30 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 36BA83385A1; Mon, 10 Nov 2025 18:25:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799109; cv=none; b=F3f/uHHWJNZFvzs8YkfLgqatBot/FJokgmKemP8qYbVgK+Tg2TAUauzuj7sJXzvs7P/isweLn/F2fVLDryg+5Jd27rvAZhlBNvMD4gimGoo7MRnwQOcQ6p6kqxeoMwHrAtdvXy1Mdf7DRbSEXztinuApDH8Il5ul+yBKHCdlTio= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799109; c=relaxed/simple; bh=YMofdhrpUU9PJUyQ+P3EJM5wMG7SVst7KrVlO53ZPUQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tGoCy4WWbli+Ueh6apfAwKwPD0oSdS6AA4+PtEIy5LgR7jBbXASVbkQ5FN7rX2jwaw0Jk0Z8XERhVYesiHo8y4T9pb6wJ+KfWikcx3GwrANTNRLzh6+TnLDlzj5snjhImr9MiV6BpxQEFEXGdBVAHiqXmTLOzh74M1QLtCarSvw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=AvwX9pj3; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="AvwX9pj3" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1762799108; x=1794335108; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=YMofdhrpUU9PJUyQ+P3EJM5wMG7SVst7KrVlO53ZPUQ=; b=AvwX9pj3xvXyqPM+iQ9xCA/jP6IWheYCmxq0BZ5l70A3LGJDhLSHqFp+ 5fhSw43jaoUWlmDcrSvt61tL42z/Y1UneLv45NqMgqbREMbQvXA54HAJA kvl7sa7Y+skng9wrosiz0nntksvHhN8bcZuIXqCM2FbUfBQW+qDrbMXkG Wquu5u+9X9hNkkkbFw6R5M3dKt4i0Rk4NgSAYMCVCc7rZTBIBNtP+TUMR 9HcvMbQm1fWoXKsKiUco+6yVBlyPc/1y58nk/heZHpx70NfbE9z9L6R+W T3NUy2dUVQJKuY7PkA7Ii+jyoqtKOC7WCqe4MShhC2lL+26Qk8OYrq798 Q==; X-CSE-ConnectionGUID: tCB/QqRCTku2gAWnAZfKaA== X-CSE-MsgGUID: ljXudI8oQeu0GMIhsFjbAA== X-IronPort-AV: E=McAfee;i="6800,10657,11609"; a="76305526" X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="76305526" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2025 10:25:08 -0800 X-CSE-ConnectionGUID: PJPNq06hRkO2B462uQn/9Q== X-CSE-MsgGUID: edr6w08QSsOF/EdmhPkXZA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="219396249" Received: from chang-linux-3.sc.intel.com (HELO chang-linux-3) ([172.25.66.172]) by orviesa002.jf.intel.com with ESMTP; 10 Nov 2025 10:25:08 -0800 From: "Chang S. Bae" To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, chao.gao@intel.com, zhao1.liu@intel.com, chang.seok.bae@intel.com, Peter Fang Subject: [PATCH RFC v1 18/20] KVM: x86: Expose APX foundational feature bit to guests Date: Mon, 10 Nov 2025 18:01:29 +0000 Message-ID: <20251110180131.28264-19-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251110180131.28264-1-chang.seok.bae@intel.com> References: <20251110180131.28264-1-chang.seok.bae@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Peter Fang Add the APX xfeature bit to the list of supported XCR0 components and expose the APX feature to guests. Define the APX CPUID feature bits and update the maximum supported CPUID leaf to 0x29 to include the APX leaf. On SVM systems, ensure that the feature is not advertised as EGPR support is not yet supported. No APX sub-features are enumerated yet. Those will be exposed in a separate patch. Signed-off-by: Peter Fang Signed-off-by: Chang S. Bae --- Peter had figured out establishing this change by spotting the CPUID maximum updates. --- arch/x86/kvm/cpuid.c | 8 +++++++- arch/x86/kvm/reverse_cpuid.h | 2 ++ arch/x86/kvm/svm/svm.c | 8 ++++++++ arch/x86/kvm/x86.c | 3 ++- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 52524e0ca97f..b90e58f2a42f 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -1031,6 +1031,7 @@ void kvm_set_cpu_caps(void) F(AVX_VNNI_INT16), F(PREFETCHITI), F(AVX10), + SCATTERED_F(APX), ); =20 kvm_cpu_cap_init(CPUID_7_2_EDX, @@ -1393,7 +1394,7 @@ static inline int __do_cpuid_func(struct kvm_cpuid_ar= ray *array, u32 function) switch (function) { case 0: /* Limited to the highest leaf implemented in KVM. */ - entry->eax =3D min(entry->eax, 0x24U); + entry->eax =3D min(entry->eax, 0x29U); break; case 1: cpuid_entry_override(entry, CPUID_1_EDX); @@ -1638,6 +1639,11 @@ static inline int __do_cpuid_func(struct kvm_cpuid_a= rray *array, u32 function) entry->edx =3D 0; break; } + case 0x29: { + /* No APX sub-features are supported yet */ + entry->eax =3D entry->ebx =3D entry->ecx =3D entry->edx =3D 0; + break; + } case KVM_CPUID_SIGNATURE: { const u32 *sigptr =3D (const u32 *)KVM_SIGNATURE; entry->eax =3D KVM_CPUID_FEATURES; diff --git a/arch/x86/kvm/reverse_cpuid.h b/arch/x86/kvm/reverse_cpuid.h index 743ab25ba787..e9d9fb4070ca 100644 --- a/arch/x86/kvm/reverse_cpuid.h +++ b/arch/x86/kvm/reverse_cpuid.h @@ -35,6 +35,7 @@ #define X86_FEATURE_AVX_VNNI_INT16 KVM_X86_FEATURE(CPUID_7_1_EDX, 10) #define X86_FEATURE_PREFETCHITI KVM_X86_FEATURE(CPUID_7_1_EDX, 14) #define X86_FEATURE_AVX10 KVM_X86_FEATURE(CPUID_7_1_EDX, 19) +#define KVM_X86_FEATURE_APX KVM_X86_FEATURE(CPUID_7_1_EDX, 21) =20 /* Intel-defined sub-features, CPUID level 0x00000007:2 (EDX) */ #define X86_FEATURE_INTEL_PSFD KVM_X86_FEATURE(CPUID_7_2_EDX, 0) @@ -126,6 +127,7 @@ static __always_inline u32 __feature_translate(int x86_= feature) KVM_X86_TRANSLATE_FEATURE(SGX1); KVM_X86_TRANSLATE_FEATURE(SGX2); KVM_X86_TRANSLATE_FEATURE(SGX_EDECCSSA); + KVM_X86_TRANSLATE_FEATURE(APX); KVM_X86_TRANSLATE_FEATURE(CONSTANT_TSC); KVM_X86_TRANSLATE_FEATURE(PERFMON_V2); KVM_X86_TRANSLATE_FEATURE(RRSBA_CTRL); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index e6a082686000..da57f7506f88 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -5271,6 +5271,14 @@ static __init void svm_set_cpu_caps(void) */ kvm_cpu_cap_clear(X86_FEATURE_BUS_LOCK_DETECT); kvm_cpu_cap_clear(X86_FEATURE_MSR_IMM); + + /* + * If the APX xfeature bit is not supported, meaning that VMCB + * support for EGPRs is unavailable, then the APX feature should + * not be exposed to the guest. + */ + if (!(kvm_caps.supported_xcr0 & XFEATURE_MASK_APX)) + kvm_cpu_cap_clear(X86_FEATURE_APX); } =20 static __init int svm_hardware_setup(void) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index e087db0f4153..bcf8e95d88dc 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -217,7 +217,8 @@ static struct kvm_user_return_msrs __percpu *user_retur= n_msrs; #define KVM_SUPPORTED_XCR0 (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \ | XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \ | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \ - | XFEATURE_MASK_PKRU | XFEATURE_MASK_XTILE) + | XFEATURE_MASK_PKRU | XFEATURE_MASK_XTILE \ + | XFEATURE_MASK_APX) =20 #define XFEATURE_MASK_CET_ALL (XFEATURE_MASK_CET_USER | XFEATURE_MASK_CET_= KERNEL) /* --=20 2.51.0 From nobody Sun Feb 8 02:56:30 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 15A5B338929; Mon, 10 Nov 2025 18:25:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799111; cv=none; b=eEpHjwUCb5PRSkowhIsW9vsYxL5ymxB4c/Wo1RzrNz6N6ycja5MZK7jzLiEP0axX8ibTkOI54Y+1lkVb8aXPxANF5PDcJVg60JDnk9R/f9IcaNDhciGd3ueoRCiCXpYGLdNxORSWfQ/f90ygJeq8t3Z8hfqsMIpbqylPqnZ2w9c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799111; c=relaxed/simple; bh=M90FqDepqn9vD16vcF4PCVp4pHvNqNNxEQXBCJXAYWI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kSq2UEdVyZiPeTOR6yPHie2ARypJjnoywnotdH7T07ceHih7Jz00m6qNQaiPTxmj5YOaXi36OKQs8N/HPSVEELiuAyQ9hqoDLstw53uRn90rCj/YGO2SErqUdC3upWPopDRzk23k3fix9QzF2QtbWI6gu1WpVXC5Y03hZaLgkac= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=HC6gNXIW; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="HC6gNXIW" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1762799110; x=1794335110; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=M90FqDepqn9vD16vcF4PCVp4pHvNqNNxEQXBCJXAYWI=; b=HC6gNXIWN1gPcjbNG8Ob5on+62LFDZRU2FTTEzrhclJVfqAjA5ijAENL yCd8bYfidmEdyEb3Sb0HSCTAbOCUWpD/6KIihbW6M12tzjZXeCVujDhn/ EDyD8VPWjeKhSEW6nrlU0K5BouovyGw0aX3ZUfm+f6SvLxcXDhxM81rYR UhJJTgh5jUWFVrwLRZJ5ww+8WeKWQtyPE4pGxJLl6XsmalodSbI+T6NmN 0DKH7lMGN8rtwg8ZMoguT8KY8Amnx23/6bDfSKpXGApzspn5JRIK9vCM+ wVUQGr0R5Ovyv0Gb3d2hJCb+Q0ENwwXAJSC+JNIbW+leOUzqc7bEJiJSd w==; X-CSE-ConnectionGUID: MvZy/K8WThCgbE7dTswcuQ== X-CSE-MsgGUID: d/UAvDbFQ4+uD45s5ttfvA== X-IronPort-AV: E=McAfee;i="6800,10657,11609"; a="76305532" X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="76305532" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2025 10:25:10 -0800 X-CSE-ConnectionGUID: dzQZpdE1SeWFykSb4QxwMw== X-CSE-MsgGUID: bYZux5neTfqIDgGSJYsvfg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="219396267" Received: from chang-linux-3.sc.intel.com (HELO chang-linux-3) ([172.25.66.172]) by orviesa002.jf.intel.com with ESMTP; 10 Nov 2025 10:25:10 -0800 From: "Chang S. Bae" To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, chao.gao@intel.com, zhao1.liu@intel.com, chang.seok.bae@intel.com Subject: [PATCH RFC v1 19/20] KVM: x86: Expose APX sub-features to guests Date: Mon, 10 Nov 2025 18:01:30 +0000 Message-ID: <20251110180131.28264-20-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251110180131.28264-1-chang.seok.bae@intel.com> References: <20251110180131.28264-1-chang.seok.bae@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add CPUID leaf 0x29 sub-leaf 0 to enumerate APX sub-features to guests. This leaf currently defines the following sub-features: * New Conditional Instructions (NCI) * New Data Destination (NDD) * Flags Suppression (NF) The CPUID leaf is only exposed if the APX feature is enabled. Signed-off-by: Chang S. Bae --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/cpuid.c | 10 ++++++++-- arch/x86/kvm/reverse_cpuid.h | 4 ++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 940f83c121cf..763872080c64 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -794,6 +794,7 @@ enum kvm_only_cpuid_leafs { CPUID_24_0_EBX, CPUID_8000_0021_ECX, CPUID_7_1_ECX, + CPUID_29_0_EBX, NR_KVM_CPU_CAPS, =20 NKVMCAPINTS =3D NR_KVM_CPU_CAPS - NCAPINTS, diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index b90e58f2a42f..95c25de641ca 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -1063,6 +1063,10 @@ void kvm_set_cpu_caps(void) F(AVX10_512), ); =20 + kvm_cpu_cap_init(CPUID_29_0_EBX, + F(APX_NCI_NDD_NF), + ); + kvm_cpu_cap_init(CPUID_8000_0001_ECX, F(LAHF_LM), F(CMP_LEGACY), @@ -1640,8 +1644,10 @@ static inline int __do_cpuid_func(struct kvm_cpuid_a= rray *array, u32 function) break; } case 0x29: { - /* No APX sub-features are supported yet */ - entry->eax =3D entry->ebx =3D entry->ecx =3D entry->edx =3D 0; + if (!(kvm_caps.supported_xcr0 & XFEATURE_MASK_APX)) { + entry->eax =3D entry->ebx =3D entry->ecx =3D entry->edx =3D 0; + break; + } break; } case KVM_CPUID_SIGNATURE: { diff --git a/arch/x86/kvm/reverse_cpuid.h b/arch/x86/kvm/reverse_cpuid.h index e9d9fb4070ca..a8eca23ee2d4 100644 --- a/arch/x86/kvm/reverse_cpuid.h +++ b/arch/x86/kvm/reverse_cpuid.h @@ -50,6 +50,9 @@ #define X86_FEATURE_AVX10_256 KVM_X86_FEATURE(CPUID_24_0_EBX, 17) #define X86_FEATURE_AVX10_512 KVM_X86_FEATURE(CPUID_24_0_EBX, 18) =20 +/* Intel-defined sub-features, CPUID level 0x00000029:0 (EBX) */ +#define X86_FEATURE_APX_NCI_NDD_NF KVM_X86_FEATURE(CPUID_29_0_EBX, 0) + /* CPUID level 0x80000007 (EDX). */ #define KVM_X86_FEATURE_CONSTANT_TSC KVM_X86_FEATURE(CPUID_8000_0007_EDX, = 8) =20 @@ -92,6 +95,7 @@ static const struct cpuid_reg reverse_cpuid[] =3D { [CPUID_24_0_EBX] =3D { 0x24, 0, CPUID_EBX}, [CPUID_8000_0021_ECX] =3D {0x80000021, 0, CPUID_ECX}, [CPUID_7_1_ECX] =3D { 7, 1, CPUID_ECX}, + [CPUID_29_0_EBX] =3D { 0x29, 0, CPUID_EBX}, }; =20 /* --=20 2.51.0 From nobody Sun Feb 8 02:56:30 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 055FD3396FE; Mon, 10 Nov 2025 18:25:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799113; cv=none; b=geHReUcoU1eVsEAnjuCUfRJ5yv9AtPH9RCP48cwOGEPg8iuBi566EN+2Sjp7bcmfy/2HmKnFNwW6zv3zU98/Nq1L8aBGgWhhGwLTlB6qbUXVKZVKmU/oM4Oi16bVqZd5vFlHPlLK9KwVd1fVPnr0M1kqrAxJC47alY/pI9L28Wg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762799113; c=relaxed/simple; bh=nttrs9kXJEruBtJDkTL/ZkXBCpbVD+Se2fR40WktCw4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=diplprWOjx7fqicQ7Q8ybqCjAj7vIMg9qopbXov/o6OHx0fC4zbQDuhqI7xUpRjXnEzY7nLG0z57Zow629Wntl38Fy4Qp514tf5L284i3UOucH2oXBqGAUgNnE+1AgEA7+2gFpl4x9zqVys5/ByCyQHbc/369NeDA5iwgWWf/xU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=b2OGkEzi; arc=none smtp.client-ip=198.175.65.12 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="b2OGkEzi" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1762799112; x=1794335112; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=nttrs9kXJEruBtJDkTL/ZkXBCpbVD+Se2fR40WktCw4=; b=b2OGkEziBuVCBmILT3O6HopGHpN86odXwZcQWIAJURL07xDbPMoALBQm 945vXUL1kjj9DYLrpsWEOgjSSA/Anx3Dbq7aK5VZeucyHRJ6cdCROxAv8 C8pSzs3BQA5ELXiKb+ZVf+D59ywvppv9tWmIzKL9YE6Lkj02aEUiRFkTb 7GeVA8hbVvfEo9/Ke4KIW8A/9wkIafRfy/c5LrUfRpMWCN2KoSVpy01xf zXYBBfmBAD1DXZUS3GFp+5pNObGMaZD4tZnbamubtuxNbZ/e9kGHrgN8c gnujfNI2SD0w4TGT2rp248R43Te/LRLwH5tQRr4hK1xDU/hiUDQL6CaHX g==; X-CSE-ConnectionGUID: Loa790ghSMaJfHPMpf34sw== X-CSE-MsgGUID: 7bGlQ0hKSX6HC817Ms9Y3A== X-IronPort-AV: E=McAfee;i="6800,10657,11609"; a="76305555" X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="76305555" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Nov 2025 10:25:12 -0800 X-CSE-ConnectionGUID: vJb1HY+jRJiswqjNK2PQzg== X-CSE-MsgGUID: DvjLWkxdSlqFhlv4UCNEBg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,294,1754982000"; d="scan'208";a="219396288" Received: from chang-linux-3.sc.intel.com (HELO chang-linux-3) ([172.25.66.172]) by orviesa002.jf.intel.com with ESMTP; 10 Nov 2025 10:25:12 -0800 From: "Chang S. Bae" To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, chao.gao@intel.com, zhao1.liu@intel.com, chang.seok.bae@intel.com Subject: [PATCH RFC v1 20/20] KVM: selftests: Add APX state handling and XCR0 sanity checks Date: Mon, 10 Nov 2025 18:01:31 +0000 Message-ID: <20251110180131.28264-21-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20251110180131.28264-1-chang.seok.bae@intel.com> References: <20251110180131.28264-1-chang.seok.bae@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that KVM exposes the APX feature to guests on APX-capable systems, extend the selftests to validate XCR0 configuration and state management. Since APX repurposes the XSAVE area previously used by MPX in the non-compacted format, add a check to ensure that MPX states are not set when APX is enabled. Also, load non-init APX state data in the guest so that XSTATE_BV[APX] is set, allowing validation of APX state testing. Signed-off-by: Chang S. Bae --- .../selftests/kvm/include/x86/processor.h | 1 + tools/testing/selftests/kvm/x86/state_test.c | 6 ++++++ .../selftests/kvm/x86/xcr0_cpuid_test.c | 20 +++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/te= sting/selftests/kvm/include/x86/processor.h index 51cd84b9ca66..dde7af40584e 100644 --- a/tools/testing/selftests/kvm/include/x86/processor.h +++ b/tools/testing/selftests/kvm/include/x86/processor.h @@ -88,6 +88,7 @@ struct xstate { #define XFEATURE_MASK_LBR BIT_ULL(15) #define XFEATURE_MASK_XTILE_CFG BIT_ULL(17) #define XFEATURE_MASK_XTILE_DATA BIT_ULL(18) +#define XFEATURE_MASK_APX BIT_ULL(19) =20 #define XFEATURE_MASK_AVX512 (XFEATURE_MASK_OPMASK | \ XFEATURE_MASK_ZMM_Hi256 | \ diff --git a/tools/testing/selftests/kvm/x86/state_test.c b/tools/testing/s= elftests/kvm/x86/state_test.c index 141b7fc0c965..6d1dc575b22b 100644 --- a/tools/testing/selftests/kvm/x86/state_test.c +++ b/tools/testing/selftests/kvm/x86/state_test.c @@ -167,6 +167,12 @@ static void __attribute__((__flatten__)) guest_code(vo= id *arg) asm volatile ("vmovupd %0, %%zmm16" :: "m" (buffer)); } =20 + if (supported_xcr0 & XFEATURE_MASK_APX) { + /* mov $0xcccccccc, %r16 */ + asm volatile (".byte 0xd5, 0x18, 0xb8, 0xcc, 0xcc," + "0xcc, 0xcc, 0x00, 0x00, 0x00, 0x00"); + } + if (this_cpu_has(X86_FEATURE_MPX)) { uint64_t bounds[2] =3D { 10, 0xffffffffull }; uint64_t output[2] =3D { }; diff --git a/tools/testing/selftests/kvm/x86/xcr0_cpuid_test.c b/tools/test= ing/selftests/kvm/x86/xcr0_cpuid_test.c index d038c1571729..6e4f2f83c831 100644 --- a/tools/testing/selftests/kvm/x86/xcr0_cpuid_test.c +++ b/tools/testing/selftests/kvm/x86/xcr0_cpuid_test.c @@ -46,6 +46,21 @@ do { \ __supported, (xfeatures)); \ } while (0) =20 +/* + * Verify that mutually exclusive architectural features do not overlap. + * For example, APX and MPX must never be reported as supported together. + */ +#define ASSERT_XFEATURE_CONFLICT(supported_xcr0, xfeatures, conflicts) \ +do { \ + uint64_t __supported =3D (supported_xcr0) & ((xfeatures) | (conflicts)); = \ + \ + __GUEST_ASSERT((__supported & (xfeatures)) !=3D (xfeatures) || \ + !(__supported & (conflicts)), \ + "supported =3D 0x%lx, xfeatures =3D 0x%llx, conflicts =3D 0x%llx"= , \ + __supported, (xfeatures), (conflicts)); \ +} while (0) + + static void guest_code(void) { uint64_t initial_xcr0; @@ -79,6 +94,11 @@ static void guest_code(void) ASSERT_ALL_OR_NONE_XFEATURE(supported_xcr0, XFEATURE_MASK_XTILE); =20 + /* Check APX by ensuring MPX is not exposed concurrently */ + ASSERT_XFEATURE_CONFLICT(supported_xcr0, + XFEATURE_MASK_APX, + XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR); + vector =3D xsetbv_safe(0, XFEATURE_MASK_FP); __GUEST_ASSERT(!vector, "Expected success on XSETBV(FP), got %s", --=20 2.51.0