Introduce SEV-SNP Support
cover Introduce SEV-SNP Support
This series introduces support for SNP features to KVM-Unit-Tests environment. Goal of this work is to use KUT to build and develop a test suite for KVM hypervisor with SEV-SNP support to test basic functionality as well as determine whether hypervisor can handle edge cases that a normal SNP guest otherwise wouldn't perform/request.
These patches are rebased on top of [1] and are available at:
https://github.com/pvpk1994/kvm-unit-tests-1/tree/SNP_RFC_v1
which is in-turn is rebased on top of kvm-unit-tests master tree (Commit: e96011b32944):
https://gitlab.com/kvm-unit-tests/kvm-unit-tests
============
Patch layout
============
Patches 1-2: Provide MMIO access support to the APIC page by unsetting
C-bit in guest page table for SEV-ES/SEV-SNP guest.
Patch 3: Enables support for running SEV-SNP tests in UEFI environment.
Patches 4-6: Enables support for SEV-SNP in KUT and provides an
acitvation test to determine whether SEV-SNP has been enabled or not.
Patch 7: Sets up a new page table to enable page allocation support in
UEFI environment for SEV-SNP related tests that are introduced in this patchset.
Patches 8-9: Enables support to perform page state changes
(Private <=> Shared) using GHCB MSR protocol.
Patches 10-11: Enables support to perform page state changes
(Private <=> Shared) using GHCB NAE events. These tests support 2M and 4K pages.
Patches 12-13: Introduces tests that perform page state conversions within
a 2M range to demonstrate how hypervisor/qemu handle page state conversions that a regular SNP guest would not perform during its lifetime.
================================
Procedure to run this test-suite
================================
SEV-SNP KUT guest requires UEFI/OVMF to bootup. Information on how to run the SEV-SNP tests with UEFI support can be found in x86/efi/README.md introduced in this patchset.
Use the following (Kernel, OVMF, QEMU) to run the SNP tests:
Use qemu (SNP supported) that is listed here:
https://github.com/AMDESE/qemu/tree/snp-latest
(Commit: a4f571b72e03 at the time of writing)
Use OVMF (SNP supported) available at:
https://github.com/AMDESE/ovmf/tree/snp-latest
(Commit: f992fee06f64 at the time of writing)
Use SNP host kernel available at:
https://github.com/AMDESE/linux/tree/snp-host-latest
(Commit: f9b5bc22b945 at the time of writing)
The following scripts can help build QEMU, OVMF, kernel:
https://github.com/AMDESE/AMDSEV/tree/snp-latest
(Commit: 111ad2cc8dfd at the time of writing)
System can be configured as follows to run UEFI SNP tests:
./configure --enable-efi
This will configure KUT to use #VC handler that it sets up once GHCB page is mapped.
./configure --enable-efi --amdsev-efi-vc
The above configuration option will build KUT and let SNP test use #VC handler that is setup by OVMF throughout the lifetime of SNP guest.
The SNP tests introduced in this patchset run well with both the above configuration options (--enable-efi & --enable-efi --amdsev-efi-vc).
Once configured, SEV-SNP support can be tested as follows:
export QEMU=/path/to/qemu-system-x86_64
export EFI_UEFI=/path/to/OVMF_CODE.fd
export EFI_VARS=/path/to/OVMF_VARS.fd (if any)
EFI_SNP=y ./x86/efi/run ./x86/amd_sev.efiNOTE: Ensure the memory provided via "size=" above matches with the memory passed in x86/efi/run ($TEST_DIR/run -m 1G). Otherwise, QEMU will report about machine memory size mismatch with size of memory backend.
=====
TODOs
=====
* Introduce an edge case for when page size is 2MB, the page_state_change.cur_page must be incremented for each successful 4K page processed. (Documented in GHCB spec - Page State Change section)
* Addition of more edge cases in Page state changes to ensure host/qemu handle these cases correctly.
==========
References
==========
[1] https://lore.kernel.org/all/[email protected]/
Any feedback/review is very much appreciated!
Pavan
-----------------------------------------------------------------------
Pavan Kumar Paluri (13):
x86/apic: Include asm/io.h and use those definitions to avoid
duplication
x86/apic: Add MMIO access support for SEV-ES/SNP guest with C-bit
unset
x86/efi: Add support for running tests with UEFI in SEV-SNP
environment
x86 AMD SEV-ES: Rename setup_amd_sev_es() to setup_vc_handler() x86 AMD SEV-SNP: Enable SEV-SNP support
x86 AMD SEV-SNP: Add tests for presence of confidential computing blob x86 AMD SEV-ES: Set GHCB page attributes for a new page table x86 AMD SEV-SNP: Test Private->Shared Page state changes using GHCB
MSR
x86 AMD SEV-SNP: Test Shared->Private Page State Changes using GHCB
MSR
x86 AMD SEV-SNP: Change guest pages from Private->Shared using GHCB
NAE
x86 AMD SEV-SNP: Change guest pages from Shared->Private using GHCB
NAE
x86 AMD SEV-SNP: Test-1: Perform Intermix to 2M Private PSCs x86 AMD SEV-SNP: Test-2: Perform Intermix to 2M private to 2M shared
PSCs
lib/linux/efi.h | 1 + lib/x86/amd_sev.c | 22 +- lib/x86/amd_sev.h | 95 +++++- lib/x86/amd_sev_vc.c | 2 +- lib/x86/apic.c | 18 +- lib/x86/setup.c | 8 +- lib/x86/svm.h | 1 + lib/x86/vm.c | 6 + x86/amd_sev.c | 682 +++++++++++++++++++++++++++++++++++++++++++ x86/efi/README.md | 6 + x86/efi/run | 37 ++- 11 files changed, 858 insertions(+), 20 deletions(-)
Signature · 1 line
2.34.1
RFC PATCH 1/13 x86/apic: Include asm/io.h and use those definitions to avoid duplication
To avoid duplication, use outb() definition from asm/io.h.
No functional change has been introduced in this patch.
--- lib/x86/apic.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
lib/x86/apic.c +1 -5
@@ -4,6 +4,7 @@#include "processor.h" #include "smp.h" #include "asm/barrier.h" +#include "asm/io.h" /* xAPIC and I/O APIC are identify mapped, and never relocated. */ static void *g_apic = (void *)APIC_DEFAULT_PHYS_BASE ;
@@ -23,11 +24,6 @@ static struct apic_ops *get_apic_ops(void)return this_cpu_read_apic_ops (); } -static void outb (unsigned char data,unsigned short port) -{ - asmvolatile ("out %0, %1" : :"a" (data),"d" (port)); -} -void eoi (void ) {apic_write (APIC_EOI ,0 );
Signature · 1 line
2.34.1
RFC PATCH 2/13 x86/apic: Add MMIO access support for SEV-ES/SNP guest with C-bit unset
In reply to Pavan Kumar PaluriMMIO access to APIC's private GPA with C-bit set that is not backed by memslots is no longer treated as MMIO access and is treated as an invalid guest access. So unset the C-bit on APIC page for it to be treated as a valid MMIO access. This applies to both SEV-ES/SNP guests.
--- lib/x86/apic.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
lib/x86/apic.c +12 -0
@@ -5,6 +5,8 @@#include "smp.h" #include "asm/barrier.h" #include "asm/io.h" +#include "amd_sev.h" +#include "x86/vm.h" /* xAPIC and I/O APIC are identify mapped, and never relocated. */ static void *g_apic = (void *)APIC_DEFAULT_PHYS_BASE ;
@@ -233,7 +235,17 @@ void set_irq_line(unsigned line, int val)void enable_apic (void ) { +pteval_t *pte; +printf ("enabling apic\n" ); + +if (amd_sev_es_enabled ()) { + pte =get_pte ((pgd_t *)read_cr3 (), + (void *)APIC_DEFAULT_PHYS_BASE ); + + *pte &= ~(get_amd_sev_c_bit_mask ()); + } +xapic_write (APIC_SPIV ,0x1ff ); }
Signature · 1 line
2.34.1
RFC PATCH 3/13 x86/efi: Add support for running tests with UEFI in SEV-SNP environment
In reply to Pavan Kumar PaluriSEV-SNP no longer supports using pflash unit=0 for loading the OVMF bios, and instead relies on -bios parameter. So add support for this in the runner script (x86/efi/run).
--- x86/efi/README.md | 6 ++++++ x86/efi/run | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 35 insertions(+), 8 deletions(-)
x86/efi/README.md +6 -0
@@ -34,6 +34,12 @@ the env variable `EFI_UEFI`: EFI_UEFI=/path/to/OVMF.fd ./x86/efi/run ./x86/msr.efi +### Run test cases with UEFI in SEV-SNP environment + +To run a test case with UEFI and AMD SEV-SNP enabled: + + EFI_SNP=y ./x86/efi/run ./x86/amd_sev.efi + ## Code structure ### Code from GNU-EFI
x86/efi/run +29 -8
@@ -15,9 +15,11 @@ source config.mak : "${EFI_SRC:=$TEST_DIR}" : "${EFI_UEFI:=/usr/share/ovmf/OVMF.fd}" +: "${EFI_VARS:=/usr/share/ovmf/OVMF_VARS.fd}" : "${EFI_TEST:=efi-tests}" : "${EFI_SMP:=1}" : "${EFI_CASE:=$(basename $1 .efi)}" +: "${EFI_SNP:=n}" if [ ! -f "$EFI_UEFI" ]; then echo "UEFI firmware not found: $EFI_UEFI"
@@ -43,6 +45,24 @@ fi mkdir -p "$EFI_CASE_DIR" cp "$EFI_SRC/$EFI_CASE.efi" "$EFI_CASE_BINARY" +# SEV-SNP no longer supports using pflash unit=0 for loading the bios, +# and instead relies on -bios parameter. pflash unit=0 will instead only +# be used for OVMF_VARS image, if present. +if [ "$EFI_SNP" == "y" ]; then + "$TEST_DIR/run" \ + -bios "${EFI_UEFI}" \ + -drive file="$EFI_VARS",format=raw,if=pflash,unit=0 \ + -drive file.dir="$EFI_TEST/$EFI_CASE/",file.driver=vvfat,file.rw=on,format=raw,if=virtio \ + -net none \ + -nographic \ + -m 512M \ + -object memory-backend-memfd,id=ram1,size=512M,share=true,prealloc=false \ + -machine q35,confidential-guest-support=sev0,memory-backend=ram1 \ + -object sev-snp-guest,id=sev0,cbitpos=51,reduced-phys-bits=1 \ + -cpu EPYC-v4 + + exit $? +else # Run test case with 256MiB QEMU memory. QEMU default memory size is 128MiB. # After UEFI boot up and we call `LibMemoryMap()`, the largest consecutive # memory region is ~42MiB. Although this is sufficient for many test cases to
@@ -54,11 +74,12 @@ cp "$EFI_SRC/$EFI_CASE.efi" "$EFI_CASE_BINARY" # to x86/run. This `smp` flag overrides any previous `smp` flags (e.g., # `-smp 4`). This is necessary because KVM-Unit-Tests do not currently support # SMP under UEFI. This last flag should be removed when this issue is resolved. -"$TEST_DIR/run" \ - -drive file="$EFI_UEFI",format=raw,if=pflash,readonly=on \ - -drive file.dir="$EFI_TEST/$EFI_CASE/",file.driver=vvfat,file.rw=on,format=raw,if=virtio \ - -net none \ - -nographic \ - -m 256 \ - "$@" \ - -smp "$EFI_SMP" + "$TEST_DIR/run" \ + -drive file="$EFI_UEFI",format=raw,if=pflash,readonly=on \ + -drive file.dir="$EFI_TEST/$EFI_CASE/",file.driver=vvfat,file.rw=on,format=raw,if=virtio \ + -net none \ + -nographic \ + -m 256 \ + "$@" \ + -smp "$EFI_SMP" +fi
Signature · 1 line
2.34.1
RFC PATCH 4/13 x86 AMD SEV-ES: Rename setup_amd_sev_es() to setup_vc_handler()
In reply to Pavan Kumar PaluriRe-organize the existing code to include a common helper function setup_vc_handler() to setup #VC handler that now serves as a common #VC handler for both SEV-ES and SEV-SNP guests.
On configuring KUT with --amdsev-efi-vc flag, This setup_vc_handler() continues to re-use UEFI's #VC handler. This will be useful in understanding how UEFI modifies SNP CPUID table and also useful in studying the behavior of OVMF's IOIO and MSR #VC handlers as well.
However, if one prefers using SEV-ES/SNP's #VC handler, then --amdsev-efi-vc flag should not be passed during configuration.
No functional change has been introduced in this patch.
--- lib/x86/amd_sev.c | 6 +++++- lib/x86/amd_sev.h | 2 +- lib/x86/setup.c | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-)
lib/x86/amd_sev.c +5 -1
@@ -89,12 +89,16 @@ bool amd_sev_es_enabled(void)return sev_es_enabled; } -efi_status_t setup_amd_sev_es (void ) +efi_status_t setup_vc_handler (void ) {struct descriptor_table_ptr idtr;idt_entry_t *idt;idt_entry_t vc_handler_idt; +/* +* If AMD SEV-SNP is enabled, then SEV-ES is also enabled, so +* checking for SEV-ES covers both. +*/ if (!amd_sev_es_enabled ()) {return EFI_UNSUPPORTED ; }
lib/x86/amd_sev.h +1 -1
@@ -139,7 +139,7 @@ efi_status_t setup_amd_sev(void);#define SEV_ES_GHCB_MSR_INDEX 0xc0010130bool amd_sev_es_enabled (void ); -efi_status_t setup_amd_sev_es (void ); +efi_status_t setup_vc_handler (void );void setup_ghcb_pte (pgd_t *page_table);void handle_sev_es_vc (struct ex_regs *regs);
lib/x86/setup.c +1 -1
@@ -334,7 +334,7 @@ efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo)/* Continue if AMD SEV is not supported, but skip SEV-ES setup */ if (status ==EFI_SUCCESS ) { phase ="AMD SEV-ES" ; - status =setup_amd_sev_es (); + status =setup_vc_handler (); }if (status !=EFI_SUCCESS && status !=EFI_UNSUPPORTED ) {
Signature · 1 line
2.34.1
RFC PATCH 5/13 x86 AMD SEV-SNP: Enable SEV-SNP support
In reply to Pavan Kumar PaluriIncorporate support for SEV-SNP enablement. Provide a simple activation test to determine whether SEV-SNP is enabled or not.
SKIP this activation test if the guest is not an SEV-SNP guest.
--- lib/x86/amd_sev.c | 16 ++++++++++++++++ lib/x86/amd_sev.h | 2 ++ lib/x86/setup.c | 6 +++--- x86/amd_sev.c | 11 +++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-)
lib/x86/amd_sev.c +16 -0
@@ -89,6 +89,22 @@ bool amd_sev_es_enabled(void)return sev_es_enabled; } +bool amd_sev_snp_enabled (void ) +{ +static bool sev_snp_enabled; +static bool initialized; + +/* Test if SEV-SNP is enabled */ +if (!initialized) { +if (amd_sev_es_enabled ()) + sev_snp_enabled =rdmsr (MSR_SEV_STATUS ) & +SEV_SNP_ENABLED_MASK ; + initialized = true; + } + +return sev_snp_enabled; +} +efi_status_t setup_vc_handler (void ) {struct descriptor_table_ptr idtr;
lib/x86/amd_sev.h +2 -0
@@ -122,6 +122,7 @@ struct es_em_ctxt {#define MSR_SEV_STATUS 0xc0010131#define SEV_ENABLED_MASK 0b1#define SEV_ES_ENABLED_MASK 0b10 +#define SEV_SNP_ENABLED_MASK 0b100bool amd_sev_enabled (void );efi_status_t setup_amd_sev (void );
@@ -140,6 +141,7 @@ efi_status_t setup_amd_sev(void);bool amd_sev_es_enabled (void );efi_status_t setup_vc_handler (void ); +bool amd_sev_snp_enabled (void );void setup_ghcb_pte (pgd_t *page_table);void handle_sev_es_vc (struct ex_regs *regs);
lib/x86/setup.c +3 -3
@@ -331,9 +331,9 @@ efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo) phase ="AMD SEV" ; status =setup_amd_sev (); -/* Continue if AMD SEV is not supported, but skip SEV-ES setup */ -if (status ==EFI_SUCCESS ) { - phase ="AMD SEV-ES" ; +/* Continue if AMD SEV is not supported, but skip SEV-ES or SEV-SNP setup */ +if (status ==EFI_SUCCESS &&amd_sev_es_enabled ()) { + phase =amd_sev_snp_enabled () ?"AMD SEV-SNP" :"AMD SEV-ES" ; status =setup_vc_handler (); }
x86/amd_sev.c +11 -0
@@ -69,6 +69,16 @@ static void test_sev_es_activation(void) } } +static void test_sev_snp_activation (void ) +{ +if (!(rdmsr (MSR_SEV_STATUS ) &SEV_SNP_ENABLED_MASK )) { +report_skip ("SEV-SNP is not enabled" ); +return ; + } + +report_info ("SEV-SNP is enabled" ); +} +static void test_stringio (void ) {int st1_len =sizeof (st1) -1 ;
@@ -92,6 +102,7 @@ int main(void) rtn =test_sev_activation ();report (rtn ==EXIT_SUCCESS ,"SEV activation test." );test_sev_es_activation (); +test_sev_snp_activation ();test_stringio ();return report_summary (); }
Signature · 1 line
2.34.1
RFC PATCH 6/13 x86 AMD SEV-SNP: Add tests for presence of confidential computing blob
In reply to Pavan Kumar PaluriAdd support to enable search for confidential computing blob in the EFI system configuration table for KVM-Unit-Tests.
The SEV-SNP Confidential Computing (CC) blob (GHCB spec, Table-5) contains metadata that needs to remain accessible during the guest's lifetime. The metadata contains information on SNP reserved pages such as pointers to SNP secrets page and SNP CPUID table.
Having access to SNP CPUID table aids in providing CPUID #VC handler support. Also, Determining the presence of SNP CC blob in KUT guest verfies whether OVMF has properly provided the CC blob to the guest via the system configuration table.
Put out a warning message in case the CC blob is not found.
Import the definitions of CC_BLOB_SEV_HDR_MAGIC and cc_blob_sev_info structure from upstream linux (arch/x86/include/asm/sev.h).
--- lib/linux/efi.h | 1 + lib/x86/amd_sev.h | 18 ++++++++++++++++++ x86/amd_sev.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+)
lib/linux/efi.h +1 -0
@@ -69,6 +69,7 @@ typedef guid_t efi_guid_t;#define DEVICE_TREE_GUID EFI_GUID(0xb1b621d5, 0xf19c, 0x41a5, 0x83, 0x0b, 0xd9, 0x15, 0x2c, 0x69, 0xaa, 0xe0)#define LOADED_IMAGE_PROTOCOL_GUID EFI_GUID(0x5b1b31a1, 0x9562, 0x11d2, 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b) +#define EFI_CC_BLOB_GUID EFI_GUID(0x067b1f5f, 0xcf26, 0x44c5, 0x85, 0x54, 0x93, 0xd7, 0x77, 0x91, 0x2d, 0x42)#define EFI_LOAD_FILE2_PROTOCOL_GUID EFI_GUID(0x4006c0c1, 0xfcb3, 0x403e, 0x99, 0x6d, 0x4a, 0x6c, 0x87, 0x24, 0xe0, 0x6d)#define LINUX_EFI_INITRD_MEDIA_GUID EFI_GUID(0x5568e427, 0x68fc, 0x4f3d, 0xac, 0x74, 0xca, 0x55, 0x52, 0x31, 0xcc, 0x68)
lib/x86/amd_sev.h +18 -0
@@ -106,6 +106,24 @@ struct es_em_ctxt {struct es_fault_info fi; }; +/* +* AMD SEV Confidential computing blob structure. The structure is +* defined in OVMF UEFI firmware header: +* https://github.com/tianocore/edk2/blob/master/OvmfPkg/Include/Guid/ConfidentialComputingSevSnpBlob.h +*/ +#define CC_BLOB_SEV_HDR_MAGIC 0x45444d41 +struct cc_blob_sev_info { +u32 magic; +u16 version; +u16 reserved; +u64 secrets_phys; +u32 secrets_len; +u32 rsvd1; +u64 cpuid_phys; +u32 cpuid_len; +u32 rsvd2; +} __packed; +/* * AMD Programmer's Manual Volume 3 * - Section "Function 8000_0000h - Maximum Extended Function Number and Vendor String"
x86/amd_sev.c +30 -0
@@ -69,14 +69,44 @@ static void test_sev_es_activation(void) } } +/* Check to find if SEV-SNP's Confidential Computing Blob is present */ +static efi_status_t find_cc_blob_efi (void ) +{ +struct cc_blob_sev_info *snp_cc_blob; +efi_status_t status; + + status =efi_get_system_config_table (EFI_CC_BLOB_GUID , + (void **)&snp_cc_blob); + +if (status !=EFI_SUCCESS ) +return status; + +if (!snp_cc_blob) { +printf ("SEV-SNP CC blob not found\n" ); +return EFI_NOT_FOUND ; + } + +if (snp_cc_blob->magic !=CC_BLOB_SEV_HDR_MAGIC ) { +printf ("SEV-SNP CC blob header/signature mismatch" ); +return EFI_UNSUPPORTED ; + } + +return EFI_SUCCESS ; +} +static void test_sev_snp_activation (void ) { +efi_status_t status; +if (!(rdmsr (MSR_SEV_STATUS ) &SEV_SNP_ENABLED_MASK )) {report_skip ("SEV-SNP is not enabled" );return ; }report_info ("SEV-SNP is enabled" ); + + status =find_cc_blob_efi (); +report (status ==EFI_SUCCESS ,"SEV-SNP CC-blob presence" ); }static void test_stringio (void )
Signature · 1 line
2.34.1
RFC PATCH 7/13 x86 AMD SEV-ES: Set GHCB page attributes for a new page table
In reply to Pavan Kumar PaluriSEV-ES/SNP guest uses GHCB page to communicate with the host. Such a page should remain unencrypted (its C-bit should be unset in the guest page table). Therefore, call setup_ghcb_pte() in the path of setup_vm() to ensure C-bit of GHCB's pte is unset, for a new page table that will be setup as a part of page allocation for UEFI-based SEV-ES/SNP tests later on.
--- lib/x86/vm.c | 6 ++++++ 1 file changed, 6 insertions(+)
lib/x86/vm.c +6 -0
@@ -3,6 +3,7 @@#include "vmalloc.h" #include "alloc_page.h" #include "smp.h" +#include "amd_sev.h" static pteval_t pte_opt_mask;
@@ -197,6 +198,11 @@ void *setup_mmu(phys_addr_t end_of_memory, void *opt_mask)init_alloc_vpage ((void *)(3ul <<30 ));#endif +#ifdef CONFIG_EFI +if (amd_sev_es_enabled ()) +setup_ghcb_pte (cr3); +#endif +write_cr3 (virt_to_phys (cr3));#ifndef __x86_64__write_cr4 (X86_CR4_PSE );
Signature · 1 line
2.34.1
RFC PATCH 8/13 x86 AMD SEV-SNP: Test Private->Shared Page state changes using GHCB MSR
In reply to Pavan Kumar PaluriAs mentioned in the GHCB spec (Section 2.3.1 GHCB MSR protocol), the SEV-SNP guest VM issues 4K page state change requests to the hypervisor to convert KUT's newly allocated private pages to shared pages using GHCB MSR protcol.
The purpose of this test is to determine whether the hypervisor changes the page state to shared. Before the conversion test, ensure the state of the pages are in an expected state (i.e., private) by issuing a re-validation on one of the newly allocated page to determine the expected state of the page matches with the page's current state. Report failure if the expected page state is not private.
Import GHCB MSR PSC related definitions from upstream linux (arch/x86/include/asm/sev-common.h and arch/x86/include/asm/sev.h)
--- lib/x86/amd_sev.h | 51 ++++++++++ x86/amd_sev.c | 241 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 292 insertions(+)
lib/x86/amd_sev.h +51 -0
@@ -84,6 +84,15 @@ struct ghcb {#define VMGEXIT () { asm volatile("rep; vmmcall\n\r"); } +/* PVALIDATE return codes */ +#define PVALIDATE_FAIL_SIZEMISMATCH 6 + +/* Software defined (when rFlags.CF = 1) */ +#define PVALIDATE_FAIL_NOUPDATE 255 + +/* RMP page size */ +#define RMP_PG_SIZE_4K 0 +enum es_result {ES_OK ,/* All good */ ES_UNSUPPORTED ,/* Requested operation not supported */
@@ -157,6 +166,36 @@ efi_status_t setup_amd_sev(void);*/ #define SEV_ES_GHCB_MSR_INDEX 0xc0010130 +#define GHCB_DATA_LOW 12 +#define GHCB_MSR_INFO_MASK (BIT_ULL(GHCB_DATA_LOW) - 1) +#define GHCB_RESP_CODE (v) ((v) & GHCB_MSR_INFO_MASK) + +/* +* SNP Page State Change Operation +* +* GHCBData[55:52] - Page operation: +* 0x0001 Page assignment, Private +* 0x0002 Page assignment, Shared +*/ +enum psc_op { +SNP_PAGE_STATE_PRIVATE =1 , +SNP_PAGE_STATE_SHARED , +}; + +#define GHCB_MSR_PSC_REQ 0x14 +#define GHCB_MSR_PSC_REQ_GFN (gfn, op) \ +/* GHCBData[55:52] */ \ + (((u64)((op) & 0xf) << 52) | \ +/* GHCBData[51:12] */ \ + ((u64)((gfn) & GENMASK_ULL(39, 0)) << 12) | \ + /* GHCBData[11:0] */ \ + GHCB_MSR_PSC_REQ) + +#define GHCB_MSR_PSC_RESP 0x15 +#define GHCB_MSR_PSC_RESP_VAL (val) \ +/* GHCBData[63:32] */ \ + (((u64)(val) & GENMASK_ULL(63, 32)) >> 32) +bool amd_sev_es_enabled (void );efi_status_t setup_vc_handler (void );bool amd_sev_snp_enabled (void );
@@ -166,6 +205,18 @@ void handle_sev_es_vc(struct ex_regs *regs);unsigned long long get_amd_sev_c_bit_mask (void );unsigned long long get_amd_sev_addr_upperbound (void ); +/* +* Macros to generate condition code outputs from inline assembly, +* The output operand must be type "bool". +*/ +#ifdef __GCC_ASM_FLAG_OUTPUTS__ +# define CC_SET(c) "\n\t/* output condition code " #c "*/\n" +# define CC_OUT (c) "=@cc" #c +#else +# define CC_SET (c) "\n\tset" #c " %[_cc_" #c "]\n" +# define CC_OUT (c)[_cc_ ## c] "=qm" +#endif +/* GHCB Accessor functions from Linux's include/asm/svm.h */ #define GHCB_BITMAP_IDX (field) \ (offsetof(struct ghcb_save_area, field) / sizeof(u64))
x86/amd_sev.c +241 -0
275 lines not on this page · raw message
Signature · 1 line
2.34.1
RFC PATCH 9/13 x86 AMD SEV-SNP: Test Shared->Private Page State Changes using GHCB MSR
In reply to Pavan Kumar PaluriThe SEV-SNP guest VM issues page state change requests to hypervisor to convert hypervisor-owned 4K shared pages back to private (guest-owned) using GHCB MSR protocol. Guest then issues a 'pvalidate' instruction to validate the pages after the conversions.
The purpose of this test is to determine whether the hypervisor changes the page state to shared. After the conversion test, issue a re-validation ('pvalidate' with validated bit set) on one of the converted 4K pages to ensure the page state is actually private.
It is important to note that the re-validation test cannot be performed on a shared page ('pvalidate' with validated bit unset) as pvalidate instruction will raise an undefined #PF exception as the page's C-bit will be 0 during the guest page table walk, as mentioned in APM Vol-3, PVALIDATE. Therefore, perform writes to the shared pages (with C-bit unset) to ensure state of the pages are shared.
--- x86/amd_sev.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-)
x86/amd_sev.c +47 -2
@@ -206,8 +206,28 @@ static void set_pte_decrypted(unsigned long vaddr, int npages)flush_tlb (); } -static efi_status_t sev_set_pages_state_msr_proto (unsigned long vaddr,int npages, -int operation) +static void set_pte_encrypted (unsigned long vaddr,int npages) +{ +pteval_t *pte; +unsigned long vaddr_end = vaddr + (npages *PAGE_SIZE ); + +while (vaddr < vaddr_end) { + pte =get_pte ((pgd_t *)read_cr3 (), (void *)vaddr); + +if (!pte) +assert_msg (pte,"No pte found for vaddr 0x%lx" , vaddr); + +/* Set C-bit */ + *pte |=get_amd_sev_c_bit_mask (); + + vaddr +=PAGE_SIZE ; + } + +flush_tlb (); +} + +static efi_status_t sev_set_pages_state_msr_proto (unsigned long vaddr, +int npages,int operation) {efi_status_t status;
@@ -226,6 +246,16 @@ static efi_status_t sev_set_pages_state_msr_proto(unsigned long vaddr, int npage }set_pte_decrypted (vaddr, npages); + + }else { +set_pte_encrypted (vaddr, npages); + + status =__sev_set_pages_state_msr_proto (vaddr, npages, + operation); +if (status !=ES_OK ) { +printf ("Page state change (Shared->Private failure.\n" ); +return status; + } }return ES_OK ;
@@ -358,6 +388,21 @@ static void test_sev_psc_ghcb_msr(void) 1 << SNP_PSC_ALLOC_ORDER); } +report_info ("Shared->Private conversion test using GHCB MSR" ); + status =sev_set_pages_state_msr_proto ((unsigned long )vaddr, +1 <<SNP_PSC_ALLOC_ORDER , +SNP_PAGE_STATE_PRIVATE ); + +report (status ==ES_OK ,"Shared->Private Page State Change" ); + +/* +* After performing shared->private test, ensure the page is in +* private state by issuing a pvalidate on a 4K page. +*/ +report (is_validated_private_page ((unsigned long )vaddr, +RMP_PG_SIZE_4K , true), +"Expected page state: Private" ); +/* Cleanup */ free_pages_by_order (vaddr,SNP_PSC_ALLOC_ORDER ); }
Signature · 1 line
2.34.1
RFC PATCH 10/13 x86 AMD SEV-SNP: Change guest pages from Private->Shared using GHCB NAE
In reply to Pavan Kumar PaluriAs mentioned in the GHCB spec (Section 4 GHCB Protocol, Table-5 SNP Page state change), perform page state change conversions on a user inputted number of pages from private to hypervisor-owned. The page state change NAE event allows for SEV-SNP guest to request page state changes to hypervisor using GHCB protocol.
The test introduces support for both 4K pages as well as 2M large pages, depending on the order specified and whether the address of the page is 2M aligned or not. If 2M range is backed by a 4K page, 'pvalidate' fails with FAIL_SIZE_MISMATCH error. In such a case, the guest tries to pvalidate all the 4K entries in this 2M range.
Conduct a test to re-validate the private page before conversion to ensure PVALIDATE_FAIL_NOUPDATE is met, indicating that expected page state is met. Then, perform the page state conversions, unset the C-bits on these pages, and write data to the un-encrypted pages after private->shared conversions to confirm we can write data to the shared guest pages post page state conversions with C-bits unset appropriately.
--- lib/x86/amd_sev.h | 22 +++++ lib/x86/amd_sev_vc.c | 2 +- lib/x86/svm.h | 1 + x86/amd_sev.c | 216 ++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 239 insertions(+), 2 deletions(-)
lib/x86/amd_sev.h +22 -0
@@ -92,6 +92,7 @@ struct ghcb {/* RMP page size */ #define RMP_PG_SIZE_4K 0 +#define RMP_PG_SIZE_2M 1enum es_result {ES_OK ,/* All good */
@@ -165,6 +166,7 @@ efi_status_t setup_amd_sev(void);* - Section "GHCB" */ #define SEV_ES_GHCB_MSR_INDEX 0xc0010130 +#define VMGEXIT_PSC_MAX_ENTRY 253#define GHCB_DATA_LOW 12#define GHCB_MSR_INFO_MASK (BIT_ULL(GHCB_DATA_LOW) - 1)
@@ -196,11 +198,31 @@ enum psc_op {/* GHCBData[63:32] */ \ (((u64)(val) & GENMASK_ULL(63, 32)) >> 32) +struct psc_hdr { +u16 cur_entry; +u16 end_entry; +u32 reserved; +}; + +struct psc_entry { +u64 cur_page :12 ; +u64 gfn :40 ; +u64 operation :4 ; +u64 pagesize :1 ; +u64 reserved :7 ; +}; + +struct snp_psc_desc { +struct psc_hdr hdr; +struct psc_entry entries[VMGEXIT_PSC_MAX_ENTRY ]; +}; +bool amd_sev_es_enabled (void );efi_status_t setup_vc_handler (void );bool amd_sev_snp_enabled (void );void setup_ghcb_pte (pgd_t *page_table);void handle_sev_es_vc (struct ex_regs *regs); +void vc_ghcb_invalidate (struct ghcb *ghcb);unsigned long long get_amd_sev_c_bit_mask (void );unsigned long long get_amd_sev_addr_upperbound (void );
lib/x86/amd_sev_vc.c +1 -1
@@ -12,7 +12,7 @@extern phys_addr_t ghcb_addr; -static void vc_ghcb_invalidate (struct ghcb *ghcb) +void vc_ghcb_invalidate (struct ghcb *ghcb) { ghcb->save.sw_exit_code =0 ;memset (ghcb->save.valid_bitmap,0 ,sizeof (ghcb->save.valid_bitmap));
lib/x86/svm.h +1 -0
@@ -362,6 +362,7 @@ struct __attribute__ ((__packed__)) vmcb {#define SVM_EXIT_NPF 0x400#define SVM_EXIT_ERR -1 +#define SVM_VMGEXIT_PSC 0x80000010#define SVM_CR0_SELECTIVE_MASK (X86_CR0_TS | X86_CR0_MP)
x86/amd_sev.c +215 -1
241 lines not on this page · raw message
Signature · 1 line
2.34.1
RFC PATCH 11/13 x86 AMD SEV-SNP: Change guest pages from Shared->Private using GHCB NAE
In reply to Pavan Kumar PaluriPerform page state conversions to private on a range of pages that are already in a hypervisor-owned state.
The test introduces support for both 4K pages as well as 2M large pages depending on the order specified.
Perform a write operation on the pages while they are shared pages. After conversion, run a re-validation test on one of the converted pages to ensure the page state is private.
--- x86/amd_sev.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
x86/amd_sev.c +11 -0
@@ -615,6 +615,17 @@ static void test_sev_psc_ghcb_nae(void)"Write to %d un-encrypted pages after private->shared conversion" , 1 << SNP_PSC_ALLOC_ORDER); +/* Shared->Private operations */ +report_info ("Shared->Private conversion test using GHCB NAE" ); + +set_pte_encrypted ((unsigned long )vm_pages,1 <<SNP_PSC_ALLOC_ORDER ); + +sev_set_pages_state ((unsigned long )vm_pages,1 <<SNP_PSC_ALLOC_ORDER , +SNP_PAGE_STATE_PRIVATE , ghcb, large_page); + +report (is_validated_private_page ((unsigned long )vm_pages, large_page, true), +"Expected page state: Private" ); +/* Cleanup */ free_pages_by_order (vm_pages,SNP_PSC_ALLOC_ORDER ); }
Signature · 1 line
2.34.1
RFC PATCH 12/13 x86 AMD SEV-SNP: Test-1: Perform Intermix to 2M Private PSCs
In reply to Pavan Kumar PaluriThe test performs the following actions:
1. Allocates a 2M private page (512 4K entries) and converts the entire range to shared.
2. Performs a write operation on these un-encrypted pages.
3. Performs partial page state change conversions on first 256 4K entries and conducts a re-validation test on one of these now-private entries to determine whether the current page state is private or not.
4. Also conducts a write test on the 256 4K shared entries (with C-bit unset on PMD) to ensure these are in shared state.
5. Converts the whole 2M range from an intermixed state to private and runs a re-validation check on the now-private 2M page.
Since the test performs partial page state changes within 2M range, pvalidate would result in a failure in the form of PVALIDATE_FAIL_NOUPDATE since the test would perform same page state change operation on the entries that are already in the same state. Do not treat RMP entry validated bit unchanged as an error for this test.
The primary goal of this test is to determine whether a shared->private conversion on a 2M range containing a mix of shared and private entries is handled properly by the hypervisor or not. Such a PSC from an intermixed state may never take place in a conventional SEV-SNP guest.
--- x86/amd_sev.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 98 insertions(+), 3 deletions(-)
x86/amd_sev.c +98 -3
@@ -22,8 +22,10 @@#define TESTDEV_IO_PORT 0xe0#define SNP_PSC_ALLOC_ORDER 10 +#define INTERMIX_PSC_ORDER 9static char st1[] ="abcdefghijklmnop" ; +static bool allow_noupdate;static int test_sev_activation (void ) {
@@ -136,15 +138,22 @@ static void pvalidate_pages(struct snp_psc_desc *desc)for (; vaddr < vaddr_end; vaddr +=PAGE_SIZE ) { pvalidate_result =pvalidate (vaddr,RMP_PG_SIZE_4K , validate); -if (pvalidate_result) +if (!allow_noupdate && pvalidate_result) +break ; +else if (allow_noupdate && + (pvalidate_result && + pvalidate_result !=PVALIDATE_FAIL_NOUPDATE ))break ; } } -if (pvalidate_result) { +if (!allow_noupdate && pvalidate_result) +assert_msg (!pvalidate_result,"Failed to validate address: 0x%lx, ret: %d\n" , + vaddr, pvalidate_result); +else if (allow_noupdate && + (pvalidate_result && pvalidate_result !=PVALIDATE_FAIL_NOUPDATE ))assert_msg (!pvalidate_result,"Failed to validate address: 0x%lx, ret: %d\n" , vaddr, pvalidate_result); - } } }
@@ -630,6 +639,91 @@ static void test_sev_psc_ghcb_nae(void)free_pages_by_order (vm_pages,SNP_PSC_ALLOC_ORDER ); } +static void __test_sev_psc_private (unsigned long vaddr,struct ghcb *ghcb, +bool large_page,pteval_t *pte) +{ + allow_noupdate = true; + +set_pte_encrypted ((unsigned long )vaddr,1 <<INTERMIX_PSC_ORDER ); + +/* Convert whole 2M range back to private */ +sev_set_pages_state (vaddr,512 ,SNP_PAGE_STATE_PRIVATE , ghcb, + large_page); + + allow_noupdate = false; + +/* Test re-validation on the now-private 2M page */ +report (is_validated_private_page (vaddr, large_page,1 ), +"Expected 2M page state: Private" ); +} + +static void test_sev_psc_intermix (bool is_private) +{ +unsigned long *vm_page; +bool large_page = false; +pteval_t *pte; +struct ghcb *ghcb = (struct ghcb *)(rdmsr (SEV_ES_GHCB_MSR_INDEX )); + + vm_page =alloc_pages (INTERMIX_PSC_ORDER ); +assert_msg (vm_page,"Page allocation failure" ); + + pte =get_pte ((pgd_t *)read_cr3 (), (void *)vm_page); +assert_msg (pte,"Invalid PTE" ); + +if (!pte &&IS_ALIGNED ((unsigned long )vm_page,LARGE_PAGE_SIZE )) { +install_large_page ((pgd_t *)read_cr3 (), (phys_addr_t )vm_page, + (void *)(ulong )vm_page); + large_page = true; + } + + pte =get_pte_level ((pgd_t *)read_cr3 (), (void *)vm_page,1 ); +if (!pte) +report_info ("Intermix test will have 2M mapping" ); + +/* Convert the 2M range into shared */ +sev_set_pages_state ((unsigned long )vm_page,512 , +SNP_PAGE_STATE_SHARED , ghcb, + large_page); +set_pte_decrypted ((unsigned long )vm_page,1 <<INTERMIX_PSC_ORDER ); + +report (!test_write ((unsigned long )vm_page,512 ), +"Write to a 2M un-encrypted range" ); + +set_pte_encrypted ((unsigned long )vm_page,1 <<INTERMIX_PSC_ORDER ); + +/* +* Convert half sub-pages into private and leave other +* half in shared state. +*/ +sev_set_pages_state ((unsigned long )vm_page,256 , +SNP_PAGE_STATE_PRIVATE , ghcb, false); + +/* Test re-validation on a now-private 4k page */ +report (is_validated_private_page ((unsigned long )vm_page, false,1 ), +"Expected 4K page state: Private" ); + +/* +* Unset C-bit on 2M PMD before issuing read/write to these +* 256 4K shared entries. +*/ +set_pte_decrypted ((unsigned long )vm_page,1 <<INTERMIX_PSC_ORDER ); + +report (!test_write ((unsigned long )vm_page +256 *PAGE_SIZE ,256 ), +"Write to 256 4K shared pages within 2M un-encrypted page" ); + +if (is_private) +__test_sev_psc_private ((unsigned long )vm_page, ghcb, + large_page, pte); + +/* Cleanup */ +free_pages_by_order (vm_page,INTERMIX_PSC_ORDER ); +} + +static void test_sev_psc_intermix_to_private (void ) +{ +test_sev_psc_intermix (true); +} +int main (void ) {int rtn;
@@ -643,6 +737,7 @@ int main(void)if (amd_sev_snp_enabled ()) {test_sev_psc_ghcb_msr ();test_sev_psc_ghcb_nae (); +test_sev_psc_intermix_to_private (); }return report_summary ();
Signature · 1 line
2.34.1
RFC PATCH 13/13 x86 AMD SEV-SNP: Test-2: Perform Intermix to 2M private to 2M shared PSCs
In reply to Pavan Kumar PaluriThe test performs the following actions:
1. Allocates a 2M private page (512 4K entries) and performs 2M private to shared conversion.
2. Performs a write operation on these un-encrypted pages.
3. Performs partial page state changes (shared->private) on first 256 sub-pages and conducts a re-validation ('pvalidate') check on one of these entries to ensure its state has been changed to private.
4. Performs write test on the other set of sub-pages whose state is shared.
5. Performs PSC from 2M intermixed state to private, backed up with a re-validation check on the 2M range to ensure successfull conversion.
6. Performs PSC from 2M private to 2M shared followed by a write operation to ensure the 2M page is successfully changed to shared.
The main goal of this test is to ensure 2MB page state changes are handled properly even if the 2MB range is a mix of private/shared pages.
--- x86/amd_sev.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)
x86/amd_sev.c +35 -0
@@ -657,6 +657,32 @@ static void __test_sev_psc_private(unsigned long vaddr, struct ghcb *ghcb, "Expected 2M page state: Private"); } +static void __test_sev_psc_shared (unsigned long vaddr,struct ghcb *ghcb, +bool large_page,pteval_t *pte) +{ + allow_noupdate = true; + +set_pte_encrypted ((unsigned long )vaddr,1 <<INTERMIX_PSC_ORDER ); + +/* Convert the intermixed 2M range to 2M private */ +sev_set_pages_state (vaddr,512 ,SNP_PAGE_STATE_PRIVATE , ghcb, + large_page); + + allow_noupdate = false; + +report (is_validated_private_page (vaddr, large_page,1 ), +"Expected 2M page state: Private" ); + +/* 2M private->shared conversion */ +sev_set_pages_state (vaddr,512 ,SNP_PAGE_STATE_SHARED , ghcb, + large_page); + +set_pte_decrypted ((unsigned long )vaddr,1 <<INTERMIX_PSC_ORDER ); + +report (!test_write ((unsigned long )vaddr,512 ), +"Write to a 2M un-encrypted range" ); +} +static void test_sev_psc_intermix (bool is_private) {unsigned long *vm_page;
@@ -714,6 +740,9 @@ static void test_sev_psc_intermix(bool is_private)if (is_private)__test_sev_psc_private ((unsigned long )vm_page, ghcb, large_page, pte); +else +__test_sev_psc_shared ((unsigned long )vm_page, ghcb, + large_page, pte);/* Cleanup */ free_pages_by_order (vm_page,INTERMIX_PSC_ORDER );
@@ -724,6 +753,11 @@ static void test_sev_psc_intermix_to_private(void)test_sev_psc_intermix (true); } +static void test_sev_psc_intermix_to_shared (void ) +{ +test_sev_psc_intermix (false); +} +int main (void ) {int rtn;
@@ -738,6 +772,7 @@ int main(void)test_sev_psc_ghcb_msr ();test_sev_psc_ghcb_nae ();test_sev_psc_intermix_to_private (); +test_sev_psc_intermix_to_shared (); }return report_summary ();
Signature · 1 line
2.34.1