Skip to contentSkip to conversation

Basic SEV-SNP Selftests

PATCH Superseded Pratik R. Sampat 3 participants 18 messages Feb 21, 2025 – Feb 25, 2025

Subsystems (7)
x86 KVM Kselftest Crypto API +3 more subsystems

v7 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v7 cover Basic SEV-SNP Selftests

This patch series extends the sev_init2 and the sev_smoke test to exercise the SEV-SNP VM launch workflow.

Primarily, it introduces the architectural defines, its support in the SEV library and extends the tests to interact with the SEV-SNP ioctl() wrappers.

Patch 1  - Do not advertize SNP on initialization failure
Patch 2  - SNP test for KVM_SEV_INIT2
Patch 3  - Add vmgexit helper
Patch 4  - Add SMT control interface helper
Patch 5  - Replace assert() with TEST_ASSERT_EQ()
Patch 6  - Introduce SEV+ VM type check
Patch 7  - SNP iotcl() plumbing for the SEV library
Patch 8  - Force set GUEST_MEMFD for SNP
Patch 9  - Cleanups of smoke test - Decouple policy from type

Patch 10 - SNP smoke test

The series is based on
git.kernel.org/pub/scm/virt/kvm/kvm.git next

v6..v7:
Based on comments from Sean -
* Replaced FW check with sev->snp_initialized
* Dropped the patch which removes SEV+ KVM advertizement if INIT fails. This should be now be resolved by the combination of the patches [1,2] from Ashish.
* Change vmgexit to an inline function
* Export SMT control parsing interface to kvm_util
Note: hyperv_cpuid KST only compile testeworkbench.editor.empty.hintd
* Replace assert() with TEST_ASSERT_EQ() within SEV library
* Define KVM_SEV_PAGE_TYPE_INVALID for SEV call of encrypt_region()
* Parameterize encrypt_region() to include privatize_region()
* Deduplication of sev test calls between SEV,SEV-ES and SNP
* Removed FW version tests for SNP
* Included testing of SNP_POLICY_DBG
* Dropped most tags from patches that have been changed or indirectly affected

[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/f78ddb64087df27e7bcb1ae0ab53f55aa0804fab.1739226950.git.ashish.kalra@amd.com

v5..v6:
https://lore.kernel.org/kvm/[email protected]/
* Rename is_sev_platform_init to sev_fw_initialized (Nikunj)
* Rename KVM CPU feature X86_FEATURE_SNP to X86_FEATURE_SEV_SNP (Nikunj)
* Collected Tags from Nikunj, Pankaj, Srikanth.

v4..v5:
https://lore.kernel.org/kvm/[email protected]/
* Introduced a check to disable advertising support for SEV, SEV-ES and SNP when platform initialization fails (Nikunj)
* Remove the redundant SNP check within is_sev_vm() (Nikunj)
* Cleanup of the encrypt_region flow for better readability (Nikunj)
* Refactor paths to use the canonical $(ARCH) to rebase for kvm/next

v3..v4:
https://lore.kernel.org/kvm/[email protected]/
* Remove SNP FW API version check in the test and ensure the KVM capability advertizes the presence of the feature. Retain the minimum version definitions to exercise these API versions in the smoke test
* Retained only the SNP smoke test and SNP_INIT2 test
* The SNP architectural defined merged with SNP_INIT2 test patch
* SNP shutdown merged with SNP smoke test patch
* Add SEV VM type check to abstract comparisons and reduce clutter
* Define a SNP default policy which sets bits based on the presence of SMT
* Decouple privatization and encryption for it to be SNP agnostic
* Assert for only positive tests using vm_ioctl()
* Dropped tested-by tags

In summary - based on comments from Sean, I have primarily reduced the scope of this patch series to focus on breaking down the SNP smoke test patch (v3 - patch2) to first introduce SEV-SNP support and use this interface to extend the sev_init2 and the sev_smoke test.

The rest of the v3 patchset that introduces ioctl, pre fault, fallocate and negative tests, will be re-worked and re-introduced subsequently in future patch series post addressing the issues discussed.

v2..v3:
https://lore.kernel.org/kvm/[email protected]/
* Remove the assignments for the prefault and fallocate test type
enums.
* Fix error message for sev launch measure and finish.
* Collect tested-by tags [Peter, Srikanth]](<This patch series extends the sev_init2 and the sev_smoke test to exercise the SEV-SNP VM launch workflow.

Primarily, it introduces the architectural defines, its support in the SEV library and extends the tests to interact with the SEV-SNP ioctl() wrappers.

Patch 1  - Do not advertize SNP on initialization failure
Patch 2  - SNP test for KVM_SEV_INIT2
Patch 3  - Add vmgexit helper
Patch 4  - Helper for SMT control interface
Patch 5  - Replace assert() with TEST_ASSERT_EQ()
Patch 6  - Introduce SEV+ VM type check
Patch 7  - SNP iotcl() plumbing for the SEV library
Patch 8  - Force set GUEST_MEMFD for SNP
Patch 9  - Cleanups of smoke test - Decouple policy from type

Patch 10 - SNP smoke test

The series is based on
git.kernel.org/pub/scm/virt/kvm/kvm.git next

v6..v7
Based on comments from Sean -
* Replaced FW check with sev-%3Esnp_initialized
* Dropped the patch which removes SEV+ KVM advertizement if INIT fails This should be resolved by the combination of [1][2] from Ashish:
* Change vmgexit to an inline function
* Export SMT control parsing interface to kvm_util
* Replace assert() with TEST_ASSERT_EQ() within SEV library
* Define KVM_SEV_PAGE_TYPE_INVALID for SEV to use it with encrypt_region()
* Parameterize encrypt_region() to include privatize_region() functionality
* Deduplication of sev test calls between SEV,SEV-ES and SNP
* Removed FW version tests for SNP
* Included testing of SNP_POLICY_DBG
* Dropped most tags from patches that have directly / indirectly changed.

[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/f78ddb64087df27e7bcb1ae0ab53f55aa0804fab.1739226950.git.ashish.kalra@amd.com

v5..v6
https://lore.kernel.org/kvm/[email protected]/
* Rename is_sev_platform_init to sev_fw_initialized (Nikunj)
* Rename KVM CPU feature X86_FEATURE_SNP to X86_FEATURE_SEV_SNP (Nikunj)
* Collected Tags from Nikunj, Pankaj, Srikanth.

v4..v5:
https://lore.kernel.org/kvm/[email protected]/
* Introduced a check to disable advertising support for SEV, SEV-ES and SNP when platform initialization fails (Nikunj)
* Remove the redundant SNP check within is_sev_vm() (Nikunj)
* Cleanup of the encrypt_region flow for better readability (Nikunj)
* Refactor paths to use the canonical $(ARCH) to rebase for kvm/next

v3..v4:
https://lore.kernel.org/kvm/[email protected]/
* Remove SNP FW API version check in the test and ensure the KVM capability advertizes the presence of the feature. Retain the minimum version definitions to exercise these API versions in the smoke test
* Retained only the SNP smoke test and SNP_INIT2 test
* The SNP architectural defined merged with SNP_INIT2 test patch
* SNP shutdown merged with SNP smoke test patch
* Add SEV VM type check to abstract comparisons and reduce clutter
* Define a SNP default policy which sets bits based on the presence of SMT
* Decouple privatization and encryption for it to be SNP agnostic
* Assert for only positive tests using vm_ioctl()
* Dropped tested-by tags

In summary - based on comments from Sean, I have primarily reduced the scope of this patch series to focus on breaking down the SNP smoke test patch (v3 - patch2) to first introduce SEV-SNP support and use this interface to extend the sev_init2 and the sev_smoke test.

The rest of the v3 patchset that introduces ioctl, pre fault, fallocate and negative tests, will be re-worked and re-introduced subsequently in future patch series post addressing the issues discussed.

v2..v3:
https://lore.kernel.org/kvm/[email protected]/
* Remove the assignments for the prefault and fallocate test type enums.
* Fix error message for sev launch measure and finish.
* Collect tested-by tags [Peter, Srikanth]

Pratik R. Sampat (10):
KVM: SEV: Disable SEV-SNP support on initialization failure
KVM: selftests: SEV-SNP test for KVM_SEV_INIT2
KVM: selftests: Add vmgexit helper
KVM: selftests: Add SMT control state helper
KVM: selftests: Replace assert() with TEST_ASSERT_EQ()
KVM: selftests: Introduce SEV VM type check
KVM: selftests: Add library support for interacting with SNP
KVM: selftests: Force GUEST_MEMFD flag for SNP VM type
KVM: selftests: Abstractions for SEV to decouple policy from type
KVM: selftests: Add a basic SEV-SNP smoke test

 arch/x86/include/uapi/asm/kvm.h               |  1 +
 arch/x86/kvm/svm/sev.c                        |  4 +-
 drivers/crypto/ccp/sev-dev.c                  |  8 ++
 include/linux/psp-sev.h                       |  3 +
 tools/arch/x86/include/uapi/asm/kvm.h         |  1 +
 .../testing/selftests/kvm/include/kvm_util.h  | 35 +++++++
 .../selftests/kvm/include/x86/processor.h     |  1 +
 tools/testing/selftests/kvm/include/x86/sev.h | 42 ++++++++-
 tools/testing/selftests/kvm/lib/kvm_util.c    |  7 +-
 .../testing/selftests/kvm/lib/x86/processor.c |  4 +-
 tools/testing/selftests/kvm/lib/x86/sev.c     | 93 +++++++++++++++++--
 .../testing/selftests/kvm/x86/hyperv_cpuid.c  | 19 ----
 .../selftests/kvm/x86/sev_init2_tests.c       | 13 +++
 .../selftests/kvm/x86/sev_smoke_test.c        | 75 +++++++++------
 14 files changed, 246 insertions(+), 60 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v7 1/10 KVM: SEV: Disable SEV-SNP support on initialization failure

During platform init, SNP initialization may fail for several reasons, such as firmware command failures and incompatible versions. However, the KVM capability may continue to advertise support for it. Export this information to KVM and withdraw SEV-SNP support if has not been successfully initialized.

Fixes 1dfe571c12cf ("KVM: SEV: Add initial SEV-SNP support")

---
v6..v7:

* Replace FW version check with sev->snp_initialized (Sean)

---
 arch/x86/kvm/svm/sev.c       | 4 +++-
 drivers/crypto/ccp/sev-dev.c | 8 ++++++++
 include/linux/psp-sev.h      | 3 +++
 3 files changed, 14 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/sev.c +3 -1
index 0dbb25442ec1..87b5d63a5817 100644
@@ -3050,7 +3050,9 @@ void __init sev_hardware_setup(void)
 	sev_es_asid_count = min_sev_asid - 1;
 	WARN_ON_ONCE(misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count));
 	sev_es_supported = true;
-	sev_snp_supported = sev_snp_enabled && cc_platform_has(CC_ATTR_HOST_SEV_SNP);
+	sev_snp_supported = (sev_snp_enabled &&
+			    cc_platform_has(CC_ATTR_HOST_SEV_SNP) &&
+			    snp_initialized());
 
 out:
 	if (boot_cpu_has(X86_FEATURE_SEV))
drivers/crypto/ccp/sev-dev.c +8 -0
index 2e87ca0e292a..8d2cf8552bc2 100644
@@ -1352,6 +1352,14 @@ int sev_platform_init(struct sev_platform_init_args *args)
 }
 EXPORT_SYMBOL_GPL(sev_platform_init);
 
+bool snp_initialized(void)
+{
+	struct sev_device *sev = psp_master->sev_data;
+
+	return sev->snp_initialized;
+}
+EXPORT_SYMBOL_GPL(snp_initialized);
+
 static int __sev_platform_shutdown_locked(int *error)
 {
 	struct psp_device *psp = psp_master;
include/linux/psp-sev.h +3 -0
index f3cad182d4ef..d34068c87a28 100644
@@ -954,6 +954,7 @@ int sev_do_cmd(int cmd, void *data, int *psp_ret);
 void *psp_copy_user_blob(u64 uaddr, u32 len);
 void *snp_alloc_firmware_page(gfp_t mask);
 void snp_free_firmware_page(void *addr);
+bool snp_initialized(void);
 
 #else	/* !CONFIG_CRYPTO_DEV_SP_PSP */
 
@@ -988,6 +989,8 @@ static inline void *snp_alloc_firmware_page(gfp_t mask)
 
 static inline void snp_free_firmware_page(void *addr) { }
 
+static inline bool snp_initialized(void) { return false; }
+
 #endif	/* CONFIG_CRYPTO_DEV_SP_PSP */
 
 #endif	/* __PSP_SEV_H__ */
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v7 01/10] KVM: SEV: Disable SEV-SNP support on initialization failure

Quoted 39 lines · Pratik R. Sampat

During platform init, SNP initialization may fail for several reasons, such as firmware command failures and incompatible versions. However, the KVM capability may continue to advertise support for it. Export this information to KVM and withdraw SEV-SNP support if has not been successfully initialized.

Fixes 1dfe571c12cf ("KVM: SEV: Add initial SEV-SNP support")

---
v6..v7:

* Replace FW version check with sev->snp_initialized (Sean)

---
  arch/x86/kvm/svm/sev.c       | 4 +++-
  drivers/crypto/ccp/sev-dev.c | 8 ++++++++
  include/linux/psp-sev.h      | 3 +++
  3 files changed, 14 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/sev.c +3 -1
index 0dbb25442ec1..87b5d63a5817 100644
@@ -3050,7 +3050,9 @@ void __init sev_hardware_setup(void)
  	sev_es_asid_count = min_sev_asid - 1;
  	WARN_ON_ONCE(misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count));
  	sev_es_supported = true;
-	sev_snp_supported = sev_snp_enabled && cc_platform_has(CC_ATTR_HOST_SEV_SNP);
+	sev_snp_supported = (sev_snp_enabled &&
+			    cc_platform_has(CC_ATTR_HOST_SEV_SNP) &&
+			    snp_initialized());
 
  out:
  	if (boot_cpu_has(X86_FEATURE_SEV))
drivers/crypto/ccp/sev-dev.c +0 -0
index 2e87ca0e292a..8d2cf8552bc2 100644

drivers/crypto/ccp/sev-dev.c int sev_platform_init(struct sev_platform_init_args *args) · line 1357

Quoted 6 lines
drivers/crypto/ccp/sev-dev.c +3 -0
@@ -1352,6 +1352,14 @@ int sev_platform_init(struct sev_platform_init_args *args)
  }
  EXPORT_SYMBOL_GPL(sev_platform_init);
 
+bool snp_initialized(void)
+{
+	struct sev_device *sev = psp_master->sev_data;

Should check psp_master isn't NULL before accessing just in case (particularly for future potential callers).

(e.g. see ccb88e9549e7 ("crypto: ccp - Fix null pointer dereference in __sev_platform_shutdown_locked")

Quoted 29 lines
drivers/crypto/ccp/sev-dev.c +5 -0
+
+	return sev->snp_initialized;
+}
+EXPORT_SYMBOL_GPL(snp_initialized);
+
  static int __sev_platform_shutdown_locked(int *error)
  {
  	struct psp_device *psp = psp_master;
include/linux/psp-sev.h +3 -0
index f3cad182d4ef..d34068c87a28 100644
@@ -954,6 +954,7 @@ int sev_do_cmd(int cmd, void *data, int *psp_ret);
  void *psp_copy_user_blob(u64 uaddr, u32 len);
  void *snp_alloc_firmware_page(gfp_t mask);
  void snp_free_firmware_page(void *addr);
+bool snp_initialized(void);
 
  #else	/* !CONFIG_CRYPTO_DEV_SP_PSP */
 
@@ -988,6 +989,8 @@ static inline void *snp_alloc_firmware_page(gfp_t mask)
 
  static inline void snp_free_firmware_page(void *addr) { }
 
+static inline bool snp_initialized(void) { return false; }
+
  #endif	/* CONFIG_CRYPTO_DEV_SP_PSP */
 
  #endif	/* __PSP_SEV_H__ */
#3 lore raw

Re: [PATCH v7 01/10] KVM: SEV: Disable SEV-SNP support on initialization failure

Hi Liam,

Thanks for review!

Quoted 54 lines · Liam Merwick
Quoted 40 lines · Pratik R. Sampat

During platform init, SNP initialization may fail for several reasons, such as firmware command failures and incompatible versions. However, the KVM capability may continue to advertise support for it. Export this information to KVM and withdraw SEV-SNP support if has not been successfully initialized.

Fixes 1dfe571c12cf ("KVM: SEV: Add initial SEV-SNP support")

---
v6..v7:

* Replace FW version check with sev->snp_initialized (Sean)
---
  arch/x86/kvm/svm/sev.c       | 4 +++- drivers/crypto/ccp/sev-dev.c | 8 ++++++++ include/linux/psp-sev.h      | 3 +++

  3 files changed, 14 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/sev.c +3 -1
index 0dbb25442ec1..87b5d63a5817 100644
@@ -3050,7 +3050,9 @@ void __init sev_hardware_setup(void)
       sev_es_asid_count = min_sev_asid - 1;
       WARN_ON_ONCE(misc_cg_set_capacity(MISC_CG_RES_SEV_ES,
 sev_es_asid_count));
       sev_es_supported = true;
-    sev_snp_supported = sev_snp_enabled &&
 cc_platform_has(CC_ATTR_HOST_SEV_SNP);
+    sev_snp_supported = (sev_snp_enabled &&
+                cc_platform_has(CC_ATTR_HOST_SEV_SNP) &&
+                snp_initialized());

    out:
      if (boot_cpu_has(X86_FEATURE_SEV))

drivers/crypto/ccp/sev-dev.c +0 -0
index 2e87ca0e292a..8d2cf8552bc2 100644

drivers/crypto/ccp/sev-dev.c int sev_platform_init(struct · line 1357

Quoted 6 lines
drivers/crypto/ccp/sev-dev.c +2 -0
@@ -1352,6 +1352,14 @@ int sev_platform_init(struct
 sev_platform_init_args *args)
   }
   EXPORT_SYMBOL_GPL(sev_platform_init);
   +bool snp_initialized(void)
+{
+    struct sev_device *sev = psp_master->sev_data;

Should check psp_master isn't NULL before accessing just in case (particularly for future potential callers).

Quoted 3 lines

(e.g. see ccb88e9549e7 ("crypto: ccp - Fix null pointer dereference in __sev_platform_shutdown_locked")

Thanks for pointing this out, if I end up using this interface, I'll put the NULL check in.

Thanks!
Pratik

Quoted 29 lines
Quoted 28 lines
drivers/crypto/ccp/sev-dev.c +5 -0
+
+    return sev->snp_initialized;
+}
+EXPORT_SYMBOL_GPL(snp_initialized);
+
   static int __sev_platform_shutdown_locked(int *error)
   {

      struct psp_device *psp = psp_master;

include/linux/psp-sev.h +1 -0
index f3cad182d4ef..d34068c87a28 100644
@@ -954,6 +954,7 @@ int sev_do_cmd(int cmd, void *data, int *psp_ret);
   void *psp_copy_user_blob(u64 uaddr, u32 len);
   void *snp_alloc_firmware_page(gfp_t mask);
   void snp_free_firmware_page(void *addr);
+bool snp_initialized(void);
     #else    /* !CONFIG_CRYPTO_DEV_SP_PSP */

  @@ -988,6 +989,8 @@ static inline void *snp_alloc_firmware_page(gfp_t mask)
    static inline void snp_free_firmware_page(void *addr) { } +static inline bool snp_initialized(void) { return false; } +
  #endif    /* CONFIG_CRYPTO_DEV_SP_PSP */
    #endif    /* __PSP_SEV_H__ */

maintainer
#4 lore raw

Re: [PATCH v7 01/10] KVM: SEV: Disable SEV-SNP support on initialization failure

In reply to Pratik R. Sampat
Quoted 5 lines · Pratik R. Sampat

During platform init, SNP initialization may fail for several reasons, such as firmware command failures and incompatible versions. However, the KVM capability may continue to advertise support for it. Export this information to KVM and withdraw SEV-SNP support if has not been successfully initialized.

Hmmm... rather than creating a new API, can you just issue an SNP_PLATFORM_STATUS command and see if the SNP is not in the UNINIT state?

Thanks,
Tom

Quoted 69 lines
Fixes 1dfe571c12cf ("KVM: SEV: Add initial SEV-SNP support")

---
v6..v7:

* Replace FW version check with sev->snp_initialized (Sean)

---
 arch/x86/kvm/svm/sev.c       | 4 +++-
 drivers/crypto/ccp/sev-dev.c | 8 ++++++++
 include/linux/psp-sev.h      | 3 +++
 3 files changed, 14 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/sev.c +3 -1
index 0dbb25442ec1..87b5d63a5817 100644
@@ -3050,7 +3050,9 @@ void __init sev_hardware_setup(void)
 	sev_es_asid_count = min_sev_asid - 1;
 	WARN_ON_ONCE(misc_cg_set_capacity(MISC_CG_RES_SEV_ES, sev_es_asid_count));
 	sev_es_supported = true;
-	sev_snp_supported = sev_snp_enabled && cc_platform_has(CC_ATTR_HOST_SEV_SNP);
+	sev_snp_supported = (sev_snp_enabled &&
+			    cc_platform_has(CC_ATTR_HOST_SEV_SNP) &&
+			    snp_initialized());
 
 out:
 	if (boot_cpu_has(X86_FEATURE_SEV))
drivers/crypto/ccp/sev-dev.c +8 -0
index 2e87ca0e292a..8d2cf8552bc2 100644
@@ -1352,6 +1352,14 @@ int sev_platform_init(struct sev_platform_init_args *args)
 }
 EXPORT_SYMBOL_GPL(sev_platform_init);
 
+bool snp_initialized(void)
+{
+	struct sev_device *sev = psp_master->sev_data;
+
+	return sev->snp_initialized;
+}
+EXPORT_SYMBOL_GPL(snp_initialized);
+
 static int __sev_platform_shutdown_locked(int *error)
 {
 	struct psp_device *psp = psp_master;
include/linux/psp-sev.h +3 -0
index f3cad182d4ef..d34068c87a28 100644
@@ -954,6 +954,7 @@ int sev_do_cmd(int cmd, void *data, int *psp_ret);
 void *psp_copy_user_blob(u64 uaddr, u32 len);
 void *snp_alloc_firmware_page(gfp_t mask);
 void snp_free_firmware_page(void *addr);
+bool snp_initialized(void);
 
 #else	/* !CONFIG_CRYPTO_DEV_SP_PSP */
 
@@ -988,6 +989,8 @@ static inline void *snp_alloc_firmware_page(gfp_t mask)
 
 static inline void snp_free_firmware_page(void *addr) { }
 
+static inline bool snp_initialized(void) { return false; }
+
 #endif	/* CONFIG_CRYPTO_DEV_SP_PSP */
 
 #endif	/* __PSP_SEV_H__ */
#5 lore raw

Re: [PATCH v7 01/10] KVM: SEV: Disable SEV-SNP support on initialization failure

Hi Tom,

Quoted 10 lines · Tom Lendacky
Quoted 5 lines · Pratik R. Sampat

During platform init, SNP initialization may fail for several reasons, such as firmware command failures and incompatible versions. However, the KVM capability may continue to advertise support for it. Export this information to KVM and withdraw SEV-SNP support if has not been successfully initialized.

Hmmm... rather than creating a new API, can you just issue an SNP_PLATFORM_STATUS command and see if the SNP is not in the UNINIT state?

Although reading sev->snp_initialized is probably cheaper to do, it is cleaner to query the platform status.

Querying SNP_PLATFORM_STATUS requires the pages to transition to firmware-owned and back, and the helpers for it are implemented within sev-dev.c. So, similar to sev_platform_status(), I'm thinking it is probably better to create the snp_platform_status() API as well and use that within KVM to check the state.

Thanks!
Pratik

#6 lore raw

Re: [PATCH v7 01/10] KVM: SEV: Disable SEV-SNP support on initialization failure

Quoted 14 lines · Pratik R. Sampat

Hi Tom,

Quoted 10 lines · Tom Lendacky
Quoted 5 lines · Pratik R. Sampat

During platform init, SNP initialization may fail for several reasons, such as firmware command failures and incompatible versions. However, the KVM capability may continue to advertise support for it. Export this information to KVM and withdraw SEV-SNP support if has not been successfully initialized.

Hmmm... rather than creating a new API, can you just issue an SNP_PLATFORM_STATUS command and see if the SNP is not in the UNINIT state?

Quoted 9 lines

Although reading sev->snp_initialized is probably cheaper to do, it is cleaner to query the platform status.

Querying SNP_PLATFORM_STATUS requires the pages to transition to firmware-owned and back, and the helpers for it are implemented within sev-dev.c. So, similar to sev_platform_status(), I'm thinking it is probably better to create the snp_platform_status() API as well and use that within KVM to check the state.

Although I am guessing the initial intent was to not have an API exposed at all from CCP and only make the SNP_PLATFORM_STATUS call instead?

Since that may not be cleanly possible (we have helpers for page state conversions such as rmp_mark_pages_firmware() in ccp) without duplicating functionality in KVM as well, I guess the question really boils down to whether we export the cheaper snp_initialized() or the snp_platform_status() API instead?

Thanks again!
Pratik

maintainer
#7 lore raw

Re: [PATCH v7 01/10] KVM: SEV: Disable SEV-SNP support on initialization failure

Quoted 25 lines · Pratik R. Sampat
Quoted 14 lines · Pratik R. Sampat

Hi Tom,

Quoted 10 lines · Tom Lendacky
Quoted 5 lines · Pratik R. Sampat

During platform init, SNP initialization may fail for several reasons, such as firmware command failures and incompatible versions. However, the KVM capability may continue to advertise support for it. Export this information to KVM and withdraw SEV-SNP support if has not been successfully initialized.

Hmmm... rather than creating a new API, can you just issue an SNP_PLATFORM_STATUS command and see if the SNP is not in the UNINIT state?

Quoted 9 lines

Although reading sev->snp_initialized is probably cheaper to do, it is cleaner to query the platform status.

Querying SNP_PLATFORM_STATUS requires the pages to transition to firmware-owned and back, and the helpers for it are implemented within sev-dev.c. So, similar to sev_platform_status(), I'm thinking it is probably better to create the snp_platform_status() API as well and use that within KVM to check the state.

Quoted 8 lines

Although I am guessing the initial intent was to not have an API exposed at all from CCP and only make the SNP_PLATFORM_STATUS call instead?

Since that may not be cleanly possible (we have helpers for page state conversions such as rmp_mark_pages_firmware() in ccp) without duplicating functionality in KVM as well, I guess the question really boils down to whether we export the cheaper snp_initialized() or the snp_platform_status() API instead?

Taking a closer look, we do already have APIs that KVM uses to allocate firmware pages (output pages for SNP APIs) that can be used:
snp_alloc_firmware_page() and snp_free_firmware_page().

I think that should be enough to use sev_do_cmd() to perform the SEV_CMD_SNP_PLATFORM_STATUS command without exposing a new API.

Thanks,
Tom

Quoted 3 lines

Thanks again!
Pratik

#8 lore raw

Re: [PATCH v7 01/10] KVM: SEV: Disable SEV-SNP support on initialization failure

Quoted 35 lines · Tom Lendacky
Quoted 25 lines · Pratik R. Sampat
Quoted 14 lines · Pratik R. Sampat

Hi Tom,

Quoted 10 lines · Tom Lendacky
Quoted 5 lines · Pratik R. Sampat

During platform init, SNP initialization may fail for several reasons, such as firmware command failures and incompatible versions. However, the KVM capability may continue to advertise support for it. Export this information to KVM and withdraw SEV-SNP support if has not been successfully initialized.

Hmmm... rather than creating a new API, can you just issue an SNP_PLATFORM_STATUS command and see if the SNP is not in the UNINIT state?

Quoted 9 lines

Although reading sev->snp_initialized is probably cheaper to do, it is cleaner to query the platform status.

Querying SNP_PLATFORM_STATUS requires the pages to transition to firmware-owned and back, and the helpers for it are implemented within sev-dev.c. So, similar to sev_platform_status(), I'm thinking it is probably better to create the snp_platform_status() API as well and use that within KVM to check the state.

Quoted 8 lines

Although I am guessing the initial intent was to not have an API exposed at all from CCP and only make the SNP_PLATFORM_STATUS call instead?

Since that may not be cleanly possible (we have helpers for page state conversions such as rmp_mark_pages_firmware() in ccp) without duplicating functionality in KVM as well, I guess the question really boils down to whether we export the cheaper snp_initialized() or the snp_platform_status() API instead?

Quoted 7 lines

Taking a closer look, we do already have APIs that KVM uses to allocate firmware pages (output pages for SNP APIs) that can be used:
snp_alloc_firmware_page() and snp_free_firmware_page().

I think that should be enough to use sev_do_cmd() to perform the SEV_CMD_SNP_PLATFORM_STATUS command without exposing a new API.

Ah, I had missed that! Calling the SNP_PLATFORM_STATUS this way works.

Pratik

Quoted 6 lines

Thanks,
Tom

Quoted 3 lines

Thanks again!
Pratik

#9 lore raw

PATCH v7 2/10 KVM: selftests: SEV-SNP test for KVM_SEV_INIT2

In reply to Pratik R. Sampat

Add the X86_FEATURE_SEV_SNP CPU feature to the architectural definition for the SEV-SNP VM type to exercise the KVM_SEV_INIT2 call. Ensure that the SNP test is skipped in scenarios where CPUID supports it but KVM does not, preventing reporting of failure in such cases.

---
v6..v7:

* No change

---
 tools/testing/selftests/kvm/include/x86/processor.h |  1 +
 tools/testing/selftests/kvm/x86/sev_init2_tests.c   | 13 +++++++++++++
 2 files changed, 14 insertions(+)
tools/testing/selftests/kvm/include/x86/processor.h +1 -0
index d60da8966772..6f63fd10bbc6 100644
@@ -199,6 +199,7 @@ struct kvm_x86_cpu_feature {
 #define	X86_FEATURE_VGIF		KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 16)
 #define X86_FEATURE_SEV			KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 1)
 #define X86_FEATURE_SEV_ES		KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 3)
+#define X86_FEATURE_SEV_SNP			KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 4)
 
 /*
  * KVM defined paravirt features.
tools/testing/selftests/kvm/x86/sev_init2_tests.c +13 -0
index 3fb967f40c6a..ab3dd11ac163 100644
@@ -28,6 +28,7 @@
 int kvm_fd;
 u64 supported_vmsa_features;
 bool have_sev_es;
+bool have_snp;
 
 static int __sev_ioctl(int vm_fd, int cmd_id, void *data)
 {
@@ -83,6 +84,9 @@ void test_vm_types(void)
 	if (have_sev_es)
 		test_init2(KVM_X86_SEV_ES_VM, &(struct kvm_sev_init){});
 
+	if (have_snp)
+		test_init2(KVM_X86_SNP_VM, &(struct kvm_sev_init){});
+
 	test_init2_invalid(0, &(struct kvm_sev_init){},
 			   "VM type is KVM_X86_DEFAULT_VM");
 	if (kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM))
@@ -138,15 +142,24 @@ int main(int argc, char *argv[])
 		    "sev-es: KVM_CAP_VM_TYPES (%x) does not match cpuid (checking %x)",
 		    kvm_check_cap(KVM_CAP_VM_TYPES), 1 << KVM_X86_SEV_ES_VM);
 
+	have_snp = kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SNP_VM);
+	TEST_ASSERT(!have_snp || kvm_cpu_has(X86_FEATURE_SEV_SNP),
+		    "sev-snp: KVM_CAP_VM_TYPES (%x) indicates SNP support (bit %d), but CPUID does not",
+		    kvm_check_cap(KVM_CAP_VM_TYPES), KVM_X86_SNP_VM);
+
 	test_vm_types();
 
 	test_flags(KVM_X86_SEV_VM);
 	if (have_sev_es)
 		test_flags(KVM_X86_SEV_ES_VM);
+	if (have_snp)
+		test_flags(KVM_X86_SNP_VM);
 
 	test_features(KVM_X86_SEV_VM, 0);
 	if (have_sev_es)
 		test_features(KVM_X86_SEV_ES_VM, supported_vmsa_features);
+	if (have_snp)
+		test_features(KVM_X86_SNP_VM, supported_vmsa_features);
 
 	return 0;
 }
Signature · 1 line
2.43.0
#10 lore raw

PATCH v7 3/10 KVM: selftests: Add vmgexit helper

In reply to Pratik R. Sampat

Abstract rep vmmcall coded into the vmgexit helper for the sev library.

No functional change intended.

---
v6..v7:

* Change vmgexit macro define to an inline function (Sean)

---
 tools/testing/selftests/kvm/include/x86/sev.h    | 5 +++++
 tools/testing/selftests/kvm/x86/sev_smoke_test.c | 2 +-
 2 files changed, 6 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/include/x86/sev.h +5 -0
index 82c11c81a956..3003dc837fb7 100644
@@ -71,6 +71,11 @@ kvm_static_assert(SEV_RET_SUCCESS == 0);
 void sev_vm_init(struct kvm_vm *vm);
 void sev_es_vm_init(struct kvm_vm *vm);
 
+static inline void vmgexit(void)
+{
+	__asm__ __volatile__("rep; vmmcall");
+}
+
 static inline void sev_register_encrypted_memory(struct kvm_vm *vm,
 						 struct userspace_mem_region *region)
 {
tools/testing/selftests/kvm/x86/sev_smoke_test.c +1 -1
index a1a688e75266..6812b94bf5b6 100644
@@ -27,7 +27,7 @@ static void guest_sev_es_code(void)
 	 * force "termination" to signal "done" via the GHCB MSR protocol.
 	 */
 	wrmsr(MSR_AMD64_SEV_ES_GHCB, GHCB_MSR_TERM_REQ);
-	__asm__ __volatile__("rep; vmmcall");
+	vmgexit();
 }
 
 static void guest_sev_code(void)
Signature · 1 line
2.43.0
#11 lore raw

PATCH v7 4/10 KVM: selftests: Add SMT control state helper

In reply to Pratik R. Sampat

Move the SMT control check out of the hyperv_cpuid selftest so that it is generally accessible all selftests. Split the functionality into a helper that populates a buffer with SMT control value which other helpers can use to ascertain if SMT state is available and active.

---
v6..v7:

* New - Implement SMT control interface within kvm_util so that it is accessible to KVM KSTs and subsequently the SEV library (Sean)

---
 .../testing/selftests/kvm/include/kvm_util.h  | 35 +++++++++++++++++++
 .../testing/selftests/kvm/x86/hyperv_cpuid.c  | 19 ----------
 2 files changed, 35 insertions(+), 19 deletions(-)
tools/testing/selftests/kvm/include/kvm_util.h +35 -0
index 4c4e5a847f67..446f04b2710f 100644
@@ -542,6 +542,41 @@ static inline uint64_t vm_get_stat(struct kvm_vm *vm, const char *stat_name)
 	return data;
 }
 
+static inline bool read_smt_control(char *buf, size_t buf_size)
+{
+	FILE *f = fopen("/sys/devices/system/cpu/smt/control", "r");
+	bool ret;
+
+	if (!f)
+		return false;
+
+	ret = fread(buf, sizeof(*buf), buf_size, f) > 0;
+	fclose(f);
+
+	return ret;
+}
+
+static inline bool smt_possible(void)
+{
+	char buf[16];
+
+	if (read_smt_control(buf, sizeof(buf)) &&
+	    (!strncmp(buf, "forceoff", 8) || !strncmp(buf, "notsupported", 12)))
+		return false;
+
+	return true;
+}
+
+static inline bool smt_on(void)
+{
+	char buf[16];
+
+	if (read_smt_control(buf, sizeof(buf)) && !strncmp(buf, "on", 2))
+		return true;
+
+	return false;
+}
+
 void vm_create_irqchip(struct kvm_vm *vm);
 
 static inline int __vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size,
tools/testing/selftests/kvm/x86/hyperv_cpuid.c +0 -19
index 4e920705681a..1eb55d0b7297 100644
@@ -22,25 +22,6 @@ static void guest_code(void)
 {
 }
 
-static bool smt_possible(void)
-{
-	char buf[16];
-	FILE *f;
-	bool res = true;
-
-	f = fopen("/sys/devices/system/cpu/smt/control", "r");
-	if (f) {
-		if (fread(buf, sizeof(*buf), sizeof(buf), f) > 0) {
-			if (!strncmp(buf, "forceoff", 8) ||
-			    !strncmp(buf, "notsupported", 12))
-				res = false;
-		}
-		fclose(f);
-	}
-
-	return res;
-}
-
 static void test_hv_cpuid(struct kvm_vcpu *vcpu, bool evmcs_expected)
 {
 	const bool has_irqchip = !vcpu || vcpu->vm->has_irqchip;
Signature · 1 line
2.43.0
#12 lore raw

PATCH v7 5/10 KVM: selftests: Replace assert() with TEST_ASSERT_EQ()

In reply to Pratik R. Sampat

For SEV tests, assert() failures on VM type or fd do not provide sufficient error reporting. Replace assert() with TEST_ASSERT_EQ() to obtain more detailed information on the assertion condition failure, including the call stack.

---
v6..v7:

* New - Replace older instances of assert with TEST_ASSERT for richer error reporing

---
 tools/testing/selftests/kvm/lib/x86/sev.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/kvm/lib/x86/sev.c +4 -4
index e9535ee20b7f..60d7a03dc1c2 100644
@@ -37,12 +37,12 @@ static void encrypt_region(struct kvm_vm *vm, struct userspace_mem_region *regio
 void sev_vm_init(struct kvm_vm *vm)
 {
 	if (vm->type == KVM_X86_DEFAULT_VM) {
-		assert(vm->arch.sev_fd == -1);
+		TEST_ASSERT_EQ(vm->arch.sev_fd, -1);
 		vm->arch.sev_fd = open_sev_dev_path_or_exit();
 		vm_sev_ioctl(vm, KVM_SEV_INIT, NULL);
 	} else {
 		struct kvm_sev_init init = { 0 };
-		assert(vm->type == KVM_X86_SEV_VM);
+		TEST_ASSERT_EQ(vm->type, KVM_X86_SEV_VM);
 		vm_sev_ioctl(vm, KVM_SEV_INIT2, &init);
 	}
 }
@@ -50,12 +50,12 @@ void sev_vm_init(struct kvm_vm *vm)
 void sev_es_vm_init(struct kvm_vm *vm)
 {
 	if (vm->type == KVM_X86_DEFAULT_VM) {
-		assert(vm->arch.sev_fd == -1);
+		TEST_ASSERT_EQ(vm->arch.sev_fd, -1);
 		vm->arch.sev_fd = open_sev_dev_path_or_exit();
 		vm_sev_ioctl(vm, KVM_SEV_ES_INIT, NULL);
 	} else {
 		struct kvm_sev_init init = { 0 };
-		assert(vm->type == KVM_X86_SEV_ES_VM);
+		TEST_ASSERT_EQ(vm->type, KVM_X86_SEV_ES_VM);
 		vm_sev_ioctl(vm, KVM_SEV_INIT2, &init);
 	}
 }
Signature · 1 line
2.43.0
#13 lore raw

PATCH v7 6/10 KVM: selftests: Introduce SEV VM type check

In reply to Pratik R. Sampat

In preparation for SNP, declutter the vm type check by introducing a SEV-SNP VM type check as well as a transitive set of helper functions.

The SNP VM type is the subset of SEV-ES. Similarly, the SEV-ES and SNP types are subset of the SEV VM type check.

---
v6..v7:

* No change

---
 tools/testing/selftests/kvm/include/x86/sev.h   |  4 ++++
 tools/testing/selftests/kvm/lib/x86/processor.c |  4 ++--
 tools/testing/selftests/kvm/lib/x86/sev.c       | 17 +++++++++++++++++
 .../testing/selftests/kvm/x86/sev_smoke_test.c  |  2 +-
 4 files changed, 24 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/include/x86/sev.h +4 -0
index 3003dc837fb7..b112f7664534 100644
@@ -27,6 +27,10 @@ enum sev_guest_state {
 
 #define GHCB_MSR_TERM_REQ	0x100
 
+bool is_sev_vm(struct kvm_vm *vm);
+bool is_sev_es_vm(struct kvm_vm *vm);
+bool is_sev_snp_vm(struct kvm_vm *vm);
+
 void sev_vm_launch(struct kvm_vm *vm, uint32_t policy);
 void sev_vm_launch_measure(struct kvm_vm *vm, uint8_t *measurement);
 void sev_vm_launch_finish(struct kvm_vm *vm);
tools/testing/selftests/kvm/lib/x86/processor.c +2 -2
index bd5a802fa7a5..a92dc1dad085 100644
@@ -639,7 +639,7 @@ void kvm_arch_vm_post_create(struct kvm_vm *vm)
 	sync_global_to_guest(vm, host_cpu_is_amd);
 	sync_global_to_guest(vm, is_forced_emulation_enabled);
 
-	if (vm->type == KVM_X86_SEV_VM || vm->type == KVM_X86_SEV_ES_VM) {
+	if (is_sev_vm(vm)) {
 		struct kvm_sev_init init = { 0 };
 
 		vm_sev_ioctl(vm, KVM_SEV_INIT2, &init);
@@ -1156,7 +1156,7 @@ void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits)
 
 void kvm_init_vm_address_properties(struct kvm_vm *vm)
 {
-	if (vm->type == KVM_X86_SEV_VM || vm->type == KVM_X86_SEV_ES_VM) {
+	if (is_sev_vm(vm)) {
 		vm->arch.sev_fd = open_sev_dev_path_or_exit();
 		vm->arch.c_bit = BIT_ULL(this_cpu_property(X86_PROPERTY_SEV_C_BIT));
 		vm->gpa_tag_mask = vm->arch.c_bit;
tools/testing/selftests/kvm/lib/x86/sev.c +17 -0
index 60d7a03dc1c2..4587f2b6bc39 100644
@@ -4,6 +4,23 @@
 
 #include "sev.h"
 
+bool is_sev_snp_vm(struct kvm_vm *vm)
+{
+	return vm->type == KVM_X86_SNP_VM;
+}
+
+/* A SNP VM is also a SEV-ES VM */
+bool is_sev_es_vm(struct kvm_vm *vm)
+{
+	return is_sev_snp_vm(vm) || vm->type == KVM_X86_SEV_ES_VM;
+}
+
+/* A SEV-ES and SNP VM is also a SEV VM */
+bool is_sev_vm(struct kvm_vm *vm)
+{
+	return is_sev_es_vm(vm) || vm->type == KVM_X86_SEV_VM;
+}
+
 /*
  * sparsebit_next_clear() can return 0 if [x, 2**64-1] are all set, and the
  * -1 would then cause an underflow back to 2**64 - 1. This is expected and
tools/testing/selftests/kvm/x86/sev_smoke_test.c +1 -1
index 6812b94bf5b6..a2de1e63c3cb 100644
@@ -123,7 +123,7 @@ static void test_sev(void *guest_code, uint64_t policy)
 	for (;;) {
 		vcpu_run(vcpu);
 
-		if (policy & SEV_POLICY_ES) {
+		if (is_sev_es_vm(vm)) {
 			TEST_ASSERT(vcpu->run->exit_reason == KVM_EXIT_SYSTEM_EVENT,
 				    "Wanted SYSTEM_EVENT, got %s",
 				    exit_reason_str(vcpu->run->exit_reason));
Signature · 1 line
2.43.0
#14 lore raw

PATCH v7 7/10 KVM: selftests: Add library support for interacting with SNP

In reply to Pratik R. Sampat

Extend the SEV library to include support for SNP ioctl() wrappers, which aid in launching and interacting with a SEV-SNP guest.

---
v6..v7:

* Remove FW version macros (Sean)
* Define a new type KVM_SEV_PAGE_TYPE_INVALID so that encrypt_region called using the SEV path does not have to pass a literal (Sean)
* Replace assert() within snp_vm_init() to TEST_ASSERT_EQ() (Sean)
* As SNP currently uses privatization and encryption of a region in tandem, remove privatize_region() and include it's functionality within encrypt_region() by adding a parameter (Sean)
* Minor cleanup of code braces (Sean)

---
 arch/x86/include/uapi/asm/kvm.h               |  1 +
 tools/arch/x86/include/uapi/asm/kvm.h         |  1 +
 tools/testing/selftests/kvm/include/x86/sev.h | 33 ++++++++-
 tools/testing/selftests/kvm/lib/x86/sev.c     | 68 +++++++++++++++++--
 4 files changed, 97 insertions(+), 6 deletions(-)
arch/x86/include/uapi/asm/kvm.h +1 -0
index 9e75da97bce0..565e4d054627 100644
@@ -841,6 +841,7 @@ struct kvm_sev_snp_launch_start {
 };
 
 /* Kept in sync with firmware values for simplicity. */
+#define KVM_SEV_PAGE_TYPE_INVALID		0x0
 #define KVM_SEV_SNP_PAGE_TYPE_NORMAL		0x1
 #define KVM_SEV_SNP_PAGE_TYPE_ZERO		0x3
 #define KVM_SEV_SNP_PAGE_TYPE_UNMEASURED	0x4
tools/arch/x86/include/uapi/asm/kvm.h +1 -0
index 88585c1de416..17e44fbdc2a7 100644
@@ -841,6 +841,7 @@ struct kvm_sev_snp_launch_start {
 };
 
 /* Kept in sync with firmware values for simplicity. */
+#define KVM_SEV_PAGE_TYPE_INVALID		0x0
 #define KVM_SEV_SNP_PAGE_TYPE_NORMAL		0x1
 #define KVM_SEV_SNP_PAGE_TYPE_ZERO		0x3
 #define KVM_SEV_SNP_PAGE_TYPE_UNMEASURED	0x4
tools/testing/selftests/kvm/include/x86/sev.h +32 -1
index b112f7664534..c696d10f9332 100644
@@ -25,6 +25,10 @@ enum sev_guest_state {
 #define SEV_POLICY_NO_DBG	(1UL << 0)
 #define SEV_POLICY_ES		(1UL << 2)
 
+#define SNP_POLICY_SMT		(1ULL << 16)
+#define SNP_POLICY_RSVD_MBO	(1ULL << 17)
+#define SNP_POLICY_DBG		(1ULL << 19)
+
 #define GHCB_MSR_TERM_REQ	0x100
 
 bool is_sev_vm(struct kvm_vm *vm);
@@ -34,13 +38,26 @@ bool is_sev_snp_vm(struct kvm_vm *vm);
 void sev_vm_launch(struct kvm_vm *vm, uint32_t policy);
 void sev_vm_launch_measure(struct kvm_vm *vm, uint8_t *measurement);
 void sev_vm_launch_finish(struct kvm_vm *vm);
+void snp_vm_launch_start(struct kvm_vm *vm, uint64_t policy);
+void snp_vm_launch_update(struct kvm_vm *vm);
+void snp_vm_launch_finish(struct kvm_vm *vm);
 
 struct kvm_vm *vm_sev_create_with_one_vcpu(uint32_t type, void *guest_code,
 					   struct kvm_vcpu **cpu);
-void vm_sev_launch(struct kvm_vm *vm, uint32_t policy, uint8_t *measurement);
+void vm_sev_launch(struct kvm_vm *vm, uint64_t policy, uint8_t *measurement);
 
 kvm_static_assert(SEV_RET_SUCCESS == 0);
 
+/*
+ * A SEV-SNP VM requires the policy reserved bit to always be set.
+ * The SMT policy bit is also required to be set based on SMT being
+ * available and active on the system.
+ */
+static inline u64 snp_default_policy(void)
+{
+	return SNP_POLICY_RSVD_MBO | (smt_on() ? SNP_POLICY_SMT : 0);
+}
+
 /*
  * The KVM_MEMORY_ENCRYPT_OP uAPI is utter garbage and takes an "unsigned long"
  * instead of a proper struct.  The size of the parameter is embedded in the
@@ -74,6 +91,7 @@ kvm_static_assert(SEV_RET_SUCCESS == 0);
 
 void sev_vm_init(struct kvm_vm *vm);
 void sev_es_vm_init(struct kvm_vm *vm);
+void snp_vm_init(struct kvm_vm *vm);
 
 static inline void vmgexit(void)
 {
@@ -102,4 +120,17 @@ static inline void sev_launch_update_data(struct kvm_vm *vm, vm_paddr_t gpa,
 	vm_sev_ioctl(vm, KVM_SEV_LAUNCH_UPDATE_DATA, &update_data);
 }
 
+static inline void snp_launch_update_data(struct kvm_vm *vm, vm_paddr_t gpa,
+					  uint64_t hva, uint64_t size, uint8_t type)
+{
+	struct kvm_sev_snp_launch_update update_data = {
+		.uaddr = hva,
+		.gfn_start = gpa >> PAGE_SHIFT,
+		.len = size,
+		.type = type,
+	};
+
+	vm_sev_ioctl(vm, KVM_SEV_SNP_LAUNCH_UPDATE, &update_data);
+}
+
 #endif /* SELFTEST_KVM_SEV_H */
tools/testing/selftests/kvm/lib/x86/sev.c +63 -5

110 lines not on this page · raw message

Signature · 1 line
2.43.0
#15 lore raw

PATCH v7 8/10 KVM: selftests: Force GUEST_MEMFD flag for SNP VM type

In reply to Pratik R. Sampat

Force the SEV-SNP VM type to set the KVM_MEM_GUEST_MEMFD flag for the creation of private memslots.

---
v6..v7:

* No change

---
 tools/testing/selftests/kvm/lib/kvm_util.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/lib/kvm_util.c +5 -2
index 33fefeb3ca44..089488e2eaf6 100644
@@ -413,14 +413,17 @@ struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus,
 						 nr_extra_pages);
 	struct userspace_mem_region *slot0;
 	struct kvm_vm *vm;
-	int i;
+	int i, flags = 0;
 
 	pr_debug("%s: mode='%s' type='%d', pages='%ld'\n", __func__,
 		 vm_guest_mode_string(shape.mode), shape.type, nr_pages);
 
 	vm = ____vm_create(shape);
 
-	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, 0, 0, nr_pages, 0);
+	if (shape.type == KVM_X86_SNP_VM)
+		flags |=  KVM_MEM_GUEST_MEMFD;
+
+	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, 0, 0, nr_pages, flags);
 	for (i = 0; i < NR_MEM_REGIONS; i++)
 		vm->memslots[i] = 0;
 
Signature · 1 line
2.43.0
#16 lore raw

PATCH v7 9/10 KVM: selftests: Abstractions for SEV to decouple policy from type

In reply to Pratik R. Sampat

In preparation for SNP, cleanup the smoke test to decouple deriving type from policy. This enables us to reuse existing interfaces as well as deduplicate the test calls that are called for SEV and SEV-ES.

No functional change intended.

---
v6..v7:

* Remove individual wrappers around sev and sev-es helpers
* Combine common tests and deduplicate the SEV, SEV-ES calls from the main function (Sean)

---
 .../selftests/kvm/x86/sev_smoke_test.c        | 50 ++++++++++---------
 1 file changed, 26 insertions(+), 24 deletions(-)
tools/testing/selftests/kvm/x86/sev_smoke_test.c +26 -24
index a2de1e63c3cb..620aa7c41f7a 100644
@@ -61,7 +61,7 @@ static void compare_xsave(u8 *from_host, u8 *from_guest)
 		abort();
 }
 
-static void test_sync_vmsa(uint32_t policy)
+static void test_sync_vmsa(uint32_t type, uint64_t policy)
 {
 	struct kvm_vcpu *vcpu;
 	struct kvm_vm *vm;
@@ -71,7 +71,7 @@ static void test_sync_vmsa(uint32_t policy)
 	double x87val = M_PI;
 	struct kvm_xsave __attribute__((aligned(64))) xsave = { 0 };
 
-	vm = vm_sev_create_with_one_vcpu(KVM_X86_SEV_ES_VM, guest_code_xsave, &vcpu);
+	vm = vm_sev_create_with_one_vcpu(type, guest_code_xsave, &vcpu);
 	gva = vm_vaddr_alloc_shared(vm, PAGE_SIZE, KVM_UTIL_MIN_VADDR,
 				    MEM_REGION_TEST_DATA);
 	hva = addr_gva2hva(vm, gva);
@@ -88,7 +88,7 @@ static void test_sync_vmsa(uint32_t policy)
 	    : "ymm4", "st", "st(1)", "st(2)", "st(3)", "st(4)", "st(5)", "st(6)", "st(7)");
 	vcpu_xsave_set(vcpu, &xsave);
 
-	vm_sev_launch(vm, SEV_POLICY_ES | policy, NULL);
+	vm_sev_launch(vm, policy, NULL);
 
 	/* This page is shared, so make it decrypted.  */
 	memset(hva, 0, 4096);
@@ -107,14 +107,12 @@ static void test_sync_vmsa(uint32_t policy)
 	kvm_vm_free(vm);
 }
 
-static void test_sev(void *guest_code, uint64_t policy)
+static void test_sev(void *guest_code, uint32_t type, uint64_t policy)
 {
 	struct kvm_vcpu *vcpu;
 	struct kvm_vm *vm;
 	struct ucall uc;
 
-	uint32_t type = policy & SEV_POLICY_ES ? KVM_X86_SEV_ES_VM : KVM_X86_SEV_VM;
-
 	vm = vm_sev_create_with_one_vcpu(type, guest_code, &vcpu);
 
 	/* TODO: Validate the measurement is as expected. */
@@ -160,16 +158,14 @@ static void guest_shutdown_code(void)
 	__asm__ __volatile__("ud2");
 }
 
-static void test_sev_es_shutdown(void)
+static void test_sev_shutdown(uint32_t type, uint64_t policy)
 {
 	struct kvm_vcpu *vcpu;
 	struct kvm_vm *vm;
 
-	uint32_t type = KVM_X86_SEV_ES_VM;
-
 	vm = vm_sev_create_with_one_vcpu(type, guest_shutdown_code, &vcpu);
 
-	vm_sev_launch(vm, SEV_POLICY_ES, NULL);
+	vm_sev_launch(vm, policy, NULL);
 
 	vcpu_run(vcpu);
 	TEST_ASSERT(vcpu->run->exit_reason == KVM_EXIT_SHUTDOWN,
@@ -179,27 +175,33 @@ static void test_sev_es_shutdown(void)
 	kvm_vm_free(vm);
 }
 
-int main(int argc, char *argv[])
+static void test_sev_smoke(void *guest, uint32_t type, uint64_t policy)
 {
 	const u64 xf_mask = XFEATURE_MASK_X87_AVX;
 
-	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SEV));
-
-	test_sev(guest_sev_code, SEV_POLICY_NO_DBG);
-	test_sev(guest_sev_code, 0);
+	test_sev(guest, type, policy | SEV_POLICY_NO_DBG);
+	test_sev(guest, type, policy);
 
-	if (kvm_cpu_has(X86_FEATURE_SEV_ES)) {
-		test_sev(guest_sev_es_code, SEV_POLICY_ES | SEV_POLICY_NO_DBG);
-		test_sev(guest_sev_es_code, SEV_POLICY_ES);
+	if (type == KVM_X86_SEV_VM)
+		return;
 
-		test_sev_es_shutdown();
+	test_sev_shutdown(type, policy);
 
-		if (kvm_has_cap(KVM_CAP_XCRS) &&
-		    (xgetbv(0) & kvm_cpu_supported_xcr0() & xf_mask) == xf_mask) {
-			test_sync_vmsa(0);
-			test_sync_vmsa(SEV_POLICY_NO_DBG);
-		}
+	if (kvm_has_cap(KVM_CAP_XCRS) &&
+	    (xgetbv(0) & kvm_cpu_supported_xcr0() & xf_mask) == xf_mask) {
+		test_sync_vmsa(type, policy);
+		test_sync_vmsa(type, policy | SEV_POLICY_NO_DBG);
 	}
+}
+
+int main(int argc, char *argv[])
+{
+	TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SEV));
+
+	test_sev_smoke(guest_sev_code, KVM_X86_SEV_VM, 0);
+
+	if (kvm_cpu_has(X86_FEATURE_SEV_ES))
+		test_sev_smoke(guest_sev_es_code, KVM_X86_SEV_ES_VM, SEV_POLICY_ES);
 
 	return 0;
 }
Signature · 1 line
2.43.0
#17 lore raw

PATCH v7 10/10 KVM: selftests: Add a basic SEV-SNP smoke test

In reply to Pratik R. Sampat

Extend sev_smoke_test to also run a minimal SEV-SNP smoke test that initializes and sets up private memory regions required to run a simple SEV-SNP guest.

Similar to its SEV-ES smoke test counterpart, this also does not support GHCB and ucall yet and uses the GHCB MSR protocol to trigger an exit of the type KVM_EXIT_SYSTEM_EVENT.

---
v6..v7:

* Remove FW version checks for SNP (Sean)
* Include testing for policy flag SNP_POLICY_DBG
* Rework test in accordance with the last deduplication cleanup of main()

---
 .../selftests/kvm/x86/sev_smoke_test.c        | 25 +++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/x86/sev_smoke_test.c +23 -2
index 620aa7c41f7a..0505cde77358 100644
@@ -16,6 +16,18 @@
 
 #define XFEATURE_MASK_X87_AVX (XFEATURE_MASK_FP | XFEATURE_MASK_SSE | XFEATURE_MASK_YMM)
 
+static void guest_snp_code(void)
+{
+	uint64_t sev_msr = rdmsr(MSR_AMD64_SEV);
+
+	GUEST_ASSERT(sev_msr & MSR_AMD64_SEV_ENABLED);
+	GUEST_ASSERT(sev_msr & MSR_AMD64_SEV_ES_ENABLED);
+	GUEST_ASSERT(sev_msr & MSR_AMD64_SEV_SNP_ENABLED);
+
+	wrmsr(MSR_AMD64_SEV_ES_GHCB, GHCB_MSR_TERM_REQ);
+	vmgexit();
+}
+
 static void guest_sev_es_code(void)
 {
 	/* TODO: Check CPUID after GHCB-based hypercall support is added. */
@@ -179,7 +191,10 @@ static void test_sev_smoke(void *guest, uint32_t type, uint64_t policy)
 {
 	const u64 xf_mask = XFEATURE_MASK_X87_AVX;
 
-	test_sev(guest, type, policy | SEV_POLICY_NO_DBG);
+	if (type == KVM_X86_SNP_VM)
+		test_sev(guest, type, policy | SNP_POLICY_DBG);
+	else
+		test_sev(guest, type, policy | SEV_POLICY_NO_DBG);
 	test_sev(guest, type, policy);
 
 	if (type == KVM_X86_SEV_VM)
@@ -190,7 +205,10 @@ static void test_sev_smoke(void *guest, uint32_t type, uint64_t policy)
 	if (kvm_has_cap(KVM_CAP_XCRS) &&
 	    (xgetbv(0) & kvm_cpu_supported_xcr0() & xf_mask) == xf_mask) {
 		test_sync_vmsa(type, policy);
-		test_sync_vmsa(type, policy | SEV_POLICY_NO_DBG);
+		if (type == KVM_X86_SNP_VM)
+			test_sync_vmsa(type, policy | SNP_POLICY_DBG);
+		else
+			test_sync_vmsa(type, policy | SEV_POLICY_NO_DBG);
 	}
 }
 
@@ -203,5 +221,8 @@ int main(int argc, char *argv[])
 	if (kvm_cpu_has(X86_FEATURE_SEV_ES))
 		test_sev_smoke(guest_sev_es_code, KVM_X86_SEV_ES_VM, SEV_POLICY_ES);
 
+	if (kvm_cpu_has(X86_FEATURE_SEV_SNP))
+		test_sev_smoke(guest_snp_code, KVM_X86_SNP_VM, snp_default_policy());
+
 	return 0;
 }
Signature · 1 line
2.43.0