[PATCH v3 4/5] KVM: SVM: Add MSR_IA32_XSS to the GHCB for hypervisor kernel

John Allen posted 5 patches 1 month, 4 weeks ago
There is a newer version of this series
[PATCH v3 4/5] KVM: SVM: Add MSR_IA32_XSS to the GHCB for hypervisor kernel
Posted by John Allen 1 month, 4 weeks ago
When a guest issues a cpuid instruction for Fn0000000D_x0B
(CetUserOffset), KVM will intercept and need to access the guest
MSR_IA32_XSS value. For SEV-ES, this is encrypted and needs to be
included in the GHCB to be visible to the hypervisor.

Signed-off-by: John Allen <john.allen@amd.com>
---
v2:
  - Omit passing through XSS as this has already been properly
    implemented in a26b7cd22546 ("KVM: SEV: Do not intercept
    accesses to MSR_IA32_XSS for SEV-ES guests")
v3:
  - Move guest kernel GHCB_ACCESSORS definition to new series.
---
 arch/x86/kvm/svm/sev.c | 9 +++++++--
 arch/x86/kvm/svm/svm.h | 1 +
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index 3f20f6eb1ef6..2905a62e7bf2 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -3239,8 +3239,13 @@ static void sev_es_sync_from_ghcb(struct vcpu_svm *svm)
 
 	svm->vmcb->save.cpl = kvm_ghcb_get_cpl_if_valid(svm, ghcb);
 
-	if (kvm_ghcb_xcr0_is_valid(svm)) {
-		vcpu->arch.xcr0 = ghcb_get_xcr0(ghcb);
+	if (kvm_ghcb_xcr0_is_valid(svm) || kvm_ghcb_xss_is_valid(svm)) {
+		if (kvm_ghcb_xcr0_is_valid(svm))
+			vcpu->arch.xcr0 = ghcb_get_xcr0(ghcb);
+
+		if (kvm_ghcb_xss_is_valid(svm))
+			vcpu->arch.ia32_xss = ghcb_get_xss(ghcb);
+
 		vcpu->arch.cpuid_dynamic_bits_dirty = true;
 	}
 
diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h
index dabd69d6fd15..b189647d8389 100644
--- a/arch/x86/kvm/svm/svm.h
+++ b/arch/x86/kvm/svm/svm.h
@@ -925,5 +925,6 @@ DEFINE_KVM_GHCB_ACCESSORS(sw_exit_info_1)
 DEFINE_KVM_GHCB_ACCESSORS(sw_exit_info_2)
 DEFINE_KVM_GHCB_ACCESSORS(sw_scratch)
 DEFINE_KVM_GHCB_ACCESSORS(xcr0)
+DEFINE_KVM_GHCB_ACCESSORS(xss)
 
 #endif
-- 
2.34.1
Re: [PATCH v3 4/5] KVM: SVM: Add MSR_IA32_XSS to the GHCB for hypervisor kernel
Posted by Chao Gao 1 month, 1 week ago
On Wed, Aug 06, 2025 at 08:45:09PM +0000, John Allen wrote:
>When a guest issues a cpuid instruction for Fn0000000D_x0B
>(CetUserOffset), KVM will intercept and need to access the guest
>MSR_IA32_XSS value. For SEV-ES, this is encrypted and needs to be
>included in the GHCB to be visible to the hypervisor.
>
>Signed-off-by: John Allen <john.allen@amd.com>
>---
>v2:
>  - Omit passing through XSS as this has already been properly
>    implemented in a26b7cd22546 ("KVM: SEV: Do not intercept
>    accesses to MSR_IA32_XSS for SEV-ES guests")
>v3:
>  - Move guest kernel GHCB_ACCESSORS definition to new series.
>---
> arch/x86/kvm/svm/sev.c | 9 +++++++--
> arch/x86/kvm/svm/svm.h | 1 +
> 2 files changed, 8 insertions(+), 2 deletions(-)
>
>diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
>index 3f20f6eb1ef6..2905a62e7bf2 100644
>--- a/arch/x86/kvm/svm/sev.c
>+++ b/arch/x86/kvm/svm/sev.c
>@@ -3239,8 +3239,13 @@ static void sev_es_sync_from_ghcb(struct vcpu_svm *svm)
> 
> 	svm->vmcb->save.cpl = kvm_ghcb_get_cpl_if_valid(svm, ghcb);
> 
>-	if (kvm_ghcb_xcr0_is_valid(svm)) {
>-		vcpu->arch.xcr0 = ghcb_get_xcr0(ghcb);
>+	if (kvm_ghcb_xcr0_is_valid(svm) || kvm_ghcb_xss_is_valid(svm)) {
>+		if (kvm_ghcb_xcr0_is_valid(svm))
>+			vcpu->arch.xcr0 = ghcb_get_xcr0(ghcb);
>+
>+		if (kvm_ghcb_xss_is_valid(svm))
>+			vcpu->arch.ia32_xss = ghcb_get_xss(ghcb);
>+
> 		vcpu->arch.cpuid_dynamic_bits_dirty = true;

It seems a bit odd to me. How about:

	if (kvm_ghcb_xcr0_is_valid(svm)) {
		vcpu->arch.xcr0 = ghcb_get_xcr0(ghcb);
		vcpu->arch.cpuid_dynamic_bits_dirty = true;
	}

	if (kvm_ghcb_xss_is_valid(svm)) {
		vcpu->arch.xss = ghcb_get_xss(ghcb);
		vcpu->arch.cpuid_dynamic_bits_dirty = true;
	}

This looks better because it has less indentation and reduces the number
of "if" statements by one.

> 	}
> 
>diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h
>index dabd69d6fd15..b189647d8389 100644
>--- a/arch/x86/kvm/svm/svm.h
>+++ b/arch/x86/kvm/svm/svm.h
>@@ -925,5 +925,6 @@ DEFINE_KVM_GHCB_ACCESSORS(sw_exit_info_1)
> DEFINE_KVM_GHCB_ACCESSORS(sw_exit_info_2)
> DEFINE_KVM_GHCB_ACCESSORS(sw_scratch)
> DEFINE_KVM_GHCB_ACCESSORS(xcr0)
>+DEFINE_KVM_GHCB_ACCESSORS(xss)
> 
> #endif
>-- 
>2.34.1
>