public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/3] KVM: x86: Virtualize AMD CPUID faulting
@ 2026-04-02 22:30 Jim Mattson
  2026-04-02 22:31 ` [PATCH 1/3] KVM: x86: Rename supports_cpuid_fault to supports_intel_cpuid_fault Jim Mattson
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Jim Mattson @ 2026-04-02 22:30 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Shuah Khan,
	kvm, linux-kernel, linux-kselftest, Clayton Pence
  Cc: Jim Mattson

AMD's "disable CPUID in usermode" feature is analogous to Intel's "CPUID
faulting" feature, but it is advertised and activated differently.  The AMD
feature is advertised via CPUID.80000021H:EAX.CpuidUserDis[bit 17] and
activated by setting HWCR.CpuidUserDis[bit 35].

Add virtualization support for the AMD feature.

Jim Mattson (3):
  KVM: x86: Rename supports_cpuid_fault to supports_intel_cpuid_fault
  KVM: x86: Virtualize AMD CPUID faulting
  KVM: selftests: Update hwcr_msr_test for CPUID faulting bit

 arch/x86/kvm/cpuid.c                          |  2 +-
 arch/x86/kvm/cpuid.h                          |  8 ++++---
 arch/x86/kvm/emulate.c                        | 14 +++++++------
 arch/x86/kvm/x86.c                            | 21 ++++++++++++-------
 .../testing/selftests/kvm/x86/hwcr_msr_test.c |  2 +-
 5 files changed, 29 insertions(+), 18 deletions(-)


base-commit: 3d6cdcc8883b5726513d245eef0e91cabfc397f7
-- 
2.53.0.1213.gd9a14994de-goog


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/3] KVM: x86: Rename supports_cpuid_fault to supports_intel_cpuid_fault
  2026-04-02 22:30 [PATCH 0/3] KVM: x86: Virtualize AMD CPUID faulting Jim Mattson
@ 2026-04-02 22:31 ` Jim Mattson
  2026-04-10 22:21   ` Sean Christopherson
  2026-04-02 22:31 ` [PATCH 2/3] KVM: x86: Virtualize AMD CPUID faulting Jim Mattson
  2026-04-02 22:31 ` [PATCH 3/3] KVM: selftests: Update hwcr_msr_test for CPUID faulting bit Jim Mattson
  2 siblings, 1 reply; 6+ messages in thread
From: Jim Mattson @ 2026-04-02 22:31 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Shuah Khan,
	kvm, linux-kernel, linux-kselftest, Clayton Pence
  Cc: Jim Mattson

The function, supports_cpuid_fault(), tests for guest support of Intel's
CPUID faulting feature. Rename the function to supports_intel_cpuid_fault()
for clarity.

Signed-off-by: Jim Mattson <jmattson@google.com>
---
 arch/x86/kvm/cpuid.h | 2 +-
 arch/x86/kvm/x86.c   | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kvm/cpuid.h b/arch/x86/kvm/cpuid.h
index 039b8e6f40ba..51cbe67c992a 100644
--- a/arch/x86/kvm/cpuid.h
+++ b/arch/x86/kvm/cpuid.h
@@ -181,7 +181,7 @@ static inline int guest_cpuid_stepping(struct kvm_vcpu *vcpu)
 	return x86_stepping(best->eax);
 }
 
-static inline bool supports_cpuid_fault(struct kvm_vcpu *vcpu)
+static inline bool supports_intel_cpuid_fault(struct kvm_vcpu *vcpu)
 {
 	return vcpu->arch.msr_platform_info & MSR_PLATFORM_INFO_CPUID_FAULT;
 }
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 0b5d48e75b65..9768f001011d 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -4264,7 +4264,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	case MSR_MISC_FEATURES_ENABLES:
 		if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ||
 		    (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
-		     !supports_cpuid_fault(vcpu)))
+		     !supports_intel_cpuid_fault(vcpu)))
 			return 1;
 		vcpu->arch.msr_misc_features_enables = data;
 		break;
-- 
2.53.0.1213.gd9a14994de-goog


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/3] KVM: x86: Virtualize AMD CPUID faulting
  2026-04-02 22:30 [PATCH 0/3] KVM: x86: Virtualize AMD CPUID faulting Jim Mattson
  2026-04-02 22:31 ` [PATCH 1/3] KVM: x86: Rename supports_cpuid_fault to supports_intel_cpuid_fault Jim Mattson
@ 2026-04-02 22:31 ` Jim Mattson
  2026-04-02 22:31 ` [PATCH 3/3] KVM: selftests: Update hwcr_msr_test for CPUID faulting bit Jim Mattson
  2 siblings, 0 replies; 6+ messages in thread
From: Jim Mattson @ 2026-04-02 22:31 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Shuah Khan,
	kvm, linux-kernel, linux-kselftest, Clayton Pence
  Cc: Jim Mattson

On AMD CPUs, CPUID faulting support is advertised via
CPUID.80000021H:EAX.CpuidUserDis[bit 17] and enabled by setting
HWCR.CpuidUserDis[bit 35].

Advertise the feature to userspace regardless of host CPU support. Allow
writes to HWCR to set bit 35 when the guest CPUID advertises
CpuidUserDis. Update cpuid_fault_enabled() and em_cpuid() to check
HWCR.CpuidUserDis[bit 35] as well as
MSR_FEATURE_ENABLES.CPUID_GP_ON_CPL_GT_0[bit 0].

Signed-off-by: Jim Mattson <jmattson@google.com>
---
 arch/x86/kvm/cpuid.c   |  2 +-
 arch/x86/kvm/cpuid.h   |  6 ++++--
 arch/x86/kvm/emulate.c | 14 ++++++++------
 arch/x86/kvm/x86.c     | 19 +++++++++++++------
 4 files changed, 26 insertions(+), 15 deletions(-)

diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index e69156b54cff..db54fac61da9 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -1248,7 +1248,7 @@ void kvm_initialize_cpu_caps(void)
 		F(AUTOIBRS),
 		EMULATED_F(NO_SMM_CTL_MSR),
 		/* PrefetchCtlMsr */
-		/* GpOnUserCpuid */
+		EMULATED_F(GP_ON_USER_CPUID),
 		/* EPSF */
 		F(PREFETCHI),
 		F(AVX512_BMM),
diff --git a/arch/x86/kvm/cpuid.h b/arch/x86/kvm/cpuid.h
index 51cbe67c992a..cb285962a956 100644
--- a/arch/x86/kvm/cpuid.h
+++ b/arch/x86/kvm/cpuid.h
@@ -188,8 +188,10 @@ static inline bool supports_intel_cpuid_fault(struct kvm_vcpu *vcpu)
 
 static inline bool cpuid_fault_enabled(struct kvm_vcpu *vcpu)
 {
-	return vcpu->arch.msr_misc_features_enables &
-		  MSR_MISC_FEATURES_ENABLES_CPUID_FAULT;
+	return (vcpu->arch.msr_misc_features_enables &
+		MSR_MISC_FEATURES_ENABLES_CPUID_FAULT) ||
+		(vcpu->arch.msr_hwcr &
+		 BIT_ULL(MSR_K7_HWCR_CPUID_USER_DIS_BIT));
 }
 
 static __always_inline void kvm_cpu_cap_clear(unsigned int x86_feature)
diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index 6145dac4a605..41d6c3ffa6e7 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -3593,13 +3593,15 @@ static int em_sti(struct x86_emulate_ctxt *ctxt)
 static int em_cpuid(struct x86_emulate_ctxt *ctxt)
 {
 	u32 eax, ebx, ecx, edx;
-	u64 msr = 0;
+	u64 msr[2] = {};
 
-	ctxt->ops->get_msr(ctxt, MSR_MISC_FEATURES_ENABLES, &msr);
-	if (!ctxt->ops->is_smm(ctxt) &&
-	    (msr & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT) &&
-	    ctxt->ops->cpl(ctxt))
-		return emulate_gp(ctxt, 0);
+	if (!ctxt->ops->is_smm(ctxt) && ctxt->ops->cpl(ctxt)) {
+		ctxt->ops->get_msr(ctxt, MSR_MISC_FEATURES_ENABLES, &msr[0]);
+		ctxt->ops->get_msr(ctxt, MSR_K7_HWCR, &msr[1]);
+		if ((msr[0] & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT) ||
+		    (msr[1] & BIT_ULL(MSR_K7_HWCR_CPUID_USER_DIS_BIT)))
+			return emulate_gp(ctxt, 0);
+	}
 
 	eax = reg_read(ctxt, VCPU_REGS_RAX);
 	ecx = reg_read(ctxt, VCPU_REGS_RCX);
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 9768f001011d..430e583c5e62 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -4002,22 +4002,29 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		break;
 	case MSR_EFER:
 		return set_efer(vcpu, msr_info);
-	case MSR_K7_HWCR:
-		data &= ~(u64)0x40;	/* ignore flush filter disable */
-		data &= ~(u64)0x100;	/* ignore ignne emulation enable */
-		data &= ~(u64)0x8;	/* ignore TLB cache disable */
-
+	case MSR_K7_HWCR: {
 		/*
 		 * Allow McStatusWrEn and TscFreqSel. (Linux guests from v3.2
 		 * through at least v6.6 whine if TscFreqSel is clear,
 		 * depending on F/M/S.
 		 */
-		if (data & ~(BIT_ULL(18) | BIT_ULL(24))) {
+		u64 valid = BIT_ULL(18) | BIT_ULL(24);
+
+		data &= ~(u64)0x40;	/* ignore flush filter disable */
+		data &= ~(u64)0x100;	/* ignore ignne emulation enable */
+		data &= ~(u64)0x8;	/* ignore TLB cache disable */
+
+		if (guest_cpu_cap_has(vcpu, X86_FEATURE_GP_ON_USER_CPUID))
+			valid |= BIT_ULL(MSR_K7_HWCR_CPUID_USER_DIS_BIT);
+
+
+		if (data & ~valid) {
 			kvm_pr_unimpl_wrmsr(vcpu, msr, data);
 			return 1;
 		}
 		vcpu->arch.msr_hwcr = data;
 		break;
+	}
 	case MSR_FAM10H_MMIO_CONF_BASE:
 		if (data != 0) {
 			kvm_pr_unimpl_wrmsr(vcpu, msr, data);
-- 
2.53.0.1213.gd9a14994de-goog


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/3] KVM: selftests: Update hwcr_msr_test for CPUID faulting bit
  2026-04-02 22:30 [PATCH 0/3] KVM: x86: Virtualize AMD CPUID faulting Jim Mattson
  2026-04-02 22:31 ` [PATCH 1/3] KVM: x86: Rename supports_cpuid_fault to supports_intel_cpuid_fault Jim Mattson
  2026-04-02 22:31 ` [PATCH 2/3] KVM: x86: Virtualize AMD CPUID faulting Jim Mattson
@ 2026-04-02 22:31 ` Jim Mattson
  2 siblings, 0 replies; 6+ messages in thread
From: Jim Mattson @ 2026-04-02 22:31 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, Shuah Khan,
	kvm, linux-kernel, linux-kselftest, Clayton Pence
  Cc: Jim Mattson

Add BIT_ULL(35) (CpuidUserDis) to the valid mask in hwcr_msr_test, now that
KVM accepts writes to this bit when the guest CPUID advertises
CpuidUserDis.

Signed-off-by: Jim Mattson <jmattson@google.com>
---
 tools/testing/selftests/kvm/x86/hwcr_msr_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/testing/selftests/kvm/x86/hwcr_msr_test.c b/tools/testing/selftests/kvm/x86/hwcr_msr_test.c
index 10b1b0ba374e..5357281e6e4e 100644
--- a/tools/testing/selftests/kvm/x86/hwcr_msr_test.c
+++ b/tools/testing/selftests/kvm/x86/hwcr_msr_test.c
@@ -11,7 +11,7 @@
 void test_hwcr_bit(struct kvm_vcpu *vcpu, unsigned int bit)
 {
 	const uint64_t ignored = BIT_ULL(3) | BIT_ULL(6) | BIT_ULL(8);
-	const uint64_t valid = BIT_ULL(18) | BIT_ULL(24);
+	const uint64_t valid = BIT_ULL(18) | BIT_ULL(24) | BIT_ULL(35);
 	const uint64_t legal = ignored | valid;
 	uint64_t val = BIT_ULL(bit);
 	uint64_t actual;
-- 
2.53.0.1213.gd9a14994de-goog


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/3] KVM: x86: Rename supports_cpuid_fault to supports_intel_cpuid_fault
  2026-04-02 22:31 ` [PATCH 1/3] KVM: x86: Rename supports_cpuid_fault to supports_intel_cpuid_fault Jim Mattson
@ 2026-04-10 22:21   ` Sean Christopherson
  2026-04-10 22:22     ` Jim Mattson
  0 siblings, 1 reply; 6+ messages in thread
From: Sean Christopherson @ 2026-04-10 22:21 UTC (permalink / raw)
  To: Jim Mattson
  Cc: Paolo Bonzini, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Shuah Khan, kvm, linux-kernel,
	linux-kselftest, Clayton Pence

On Thu, Apr 02, 2026, Jim Mattson wrote:
> The function, supports_cpuid_fault(), tests for guest support of Intel's
> CPUID faulting feature. Rename the function to supports_intel_cpuid_fault()
> for clarity.
> 
> Signed-off-by: Jim Mattson <jmattson@google.com>
> ---
>  arch/x86/kvm/cpuid.h | 2 +-
>  arch/x86/kvm/x86.c   | 2 +-
>  2 files changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/x86/kvm/cpuid.h b/arch/x86/kvm/cpuid.h
> index 039b8e6f40ba..51cbe67c992a 100644
> --- a/arch/x86/kvm/cpuid.h
> +++ b/arch/x86/kvm/cpuid.h
> @@ -181,7 +181,7 @@ static inline int guest_cpuid_stepping(struct kvm_vcpu *vcpu)
>  	return x86_stepping(best->eax);
>  }
>  
> -static inline bool supports_cpuid_fault(struct kvm_vcpu *vcpu)
> +static inline bool supports_intel_cpuid_fault(struct kvm_vcpu *vcpu)

*sigh*

I was going to suggest supports_misc_enables_cpuid_fault(), since KVM emulates
the MSR for all CPU vendors, but the enable is in MISC_FEATURE_ENABLES, not
IA32_MISC_ENABLES.

What if we just drop the helper?  There's only one user.  I.e.

diff --git a/arch/x86/kvm/cpuid.h b/arch/x86/kvm/cpuid.h
index 039b8e6f40ba..8b64d863e19c 100644
--- a/arch/x86/kvm/cpuid.h
+++ b/arch/x86/kvm/cpuid.h
@@ -181,11 +181,6 @@ static inline int guest_cpuid_stepping(struct kvm_vcpu *vcpu)
        return x86_stepping(best->eax);
 }
 
-static inline bool supports_cpuid_fault(struct kvm_vcpu *vcpu)
-{
-       return vcpu->arch.msr_platform_info & MSR_PLATFORM_INFO_CPUID_FAULT;
-}
-
 static inline bool cpuid_fault_enabled(struct kvm_vcpu *vcpu)
 {
        return vcpu->arch.msr_misc_features_enables &
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 2410401c57d8..c72ea99422ca 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -4262,7 +4262,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
        case MSR_MISC_FEATURES_ENABLES:
                if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ||
                    (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
-                    !supports_cpuid_fault(vcpu)))
+                    !(vcpu->arch.msr_platform_info & MSR_PLATFORM_INFO_CPUID_FAULT)))
                        return 1;
                vcpu->arch.msr_misc_features_enables = data;
                break;

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/3] KVM: x86: Rename supports_cpuid_fault to supports_intel_cpuid_fault
  2026-04-10 22:21   ` Sean Christopherson
@ 2026-04-10 22:22     ` Jim Mattson
  0 siblings, 0 replies; 6+ messages in thread
From: Jim Mattson @ 2026-04-10 22:22 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: Paolo Bonzini, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, x86, H. Peter Anvin, Shuah Khan, kvm, linux-kernel,
	linux-kselftest, Clayton Pence

On Fri, Apr 10, 2026 at 3:21 PM Sean Christopherson <seanjc@google.com> wrote:
>
> On Thu, Apr 02, 2026, Jim Mattson wrote:
> > The function, supports_cpuid_fault(), tests for guest support of Intel's
> > CPUID faulting feature. Rename the function to supports_intel_cpuid_fault()
> > for clarity.
> >
> > Signed-off-by: Jim Mattson <jmattson@google.com>
> > ---
> >  arch/x86/kvm/cpuid.h | 2 +-
> >  arch/x86/kvm/x86.c   | 2 +-
> >  2 files changed, 2 insertions(+), 2 deletions(-)
> >
> > diff --git a/arch/x86/kvm/cpuid.h b/arch/x86/kvm/cpuid.h
> > index 039b8e6f40ba..51cbe67c992a 100644
> > --- a/arch/x86/kvm/cpuid.h
> > +++ b/arch/x86/kvm/cpuid.h
> > @@ -181,7 +181,7 @@ static inline int guest_cpuid_stepping(struct kvm_vcpu *vcpu)
> >       return x86_stepping(best->eax);
> >  }
> >
> > -static inline bool supports_cpuid_fault(struct kvm_vcpu *vcpu)
> > +static inline bool supports_intel_cpuid_fault(struct kvm_vcpu *vcpu)
>
> *sigh*
>
> I was going to suggest supports_misc_enables_cpuid_fault(), since KVM emulates
> the MSR for all CPU vendors, but the enable is in MISC_FEATURE_ENABLES, not
> IA32_MISC_ENABLES.
>
> What if we just drop the helper?  There's only one user.  I.e.

My thoughts exactly. :)

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-04-10 22:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-02 22:30 [PATCH 0/3] KVM: x86: Virtualize AMD CPUID faulting Jim Mattson
2026-04-02 22:31 ` [PATCH 1/3] KVM: x86: Rename supports_cpuid_fault to supports_intel_cpuid_fault Jim Mattson
2026-04-10 22:21   ` Sean Christopherson
2026-04-10 22:22     ` Jim Mattson
2026-04-02 22:31 ` [PATCH 2/3] KVM: x86: Virtualize AMD CPUID faulting Jim Mattson
2026-04-02 22:31 ` [PATCH 3/3] KVM: selftests: Update hwcr_msr_test for CPUID faulting bit Jim Mattson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox