Introduce SEV-SNP support
v2 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_v2
which are in turn rebased on kvm-unit-tests/master (201b9e8bdc84)
============
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: Renames SEV-ES's #VC handler to a more generic name that
applies to both SEV-ES and SEV-SNP. No functional change.
Patch 4: Adds support in x86/efi/run for running SEV-SNP unit tests
under UEFI
Patches 5-6: Adds support for SEV-SNP enablement and CC-blob discovery.
Patch 7: Sets up GHCB page table attributes for new page table.
Patches 8-12: Page state change conversions using MSR and GHCB protocol
approaches.
Patches 13-14: Page state change conversions from 2M intermixed states
to 2M Shared/Private states.
Patch 15: PSMASH/UNSMASH page state change requests on 2M large pages.
Patch 16: Injecting random non-zero page offsets via page state change
requests.
================================
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.
Tested this patchset against the following: (Kernel, OVMF, QEMU):
Upstream QEMU:
(commit: e2f346aa9864)
Merge tag 'sdmmc-20240716' of https://github.com/philmd/qemu into staging
Upstream OVMF:
(commit: 11c50d6ca10a)
MdeModulePkg/UfsBlockIoPei: Wait fDeviceInit Be Cleared by Devices
Upstream kernel: kvm/next
(commit: 1c5a0b55abeb)
Merge tag 'kvmarm-6.11' of git://git.kernel.org/pub/scm/linux/kernel/git/kvmarm/kvmarm into HEAD
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 and --enable-efi --amdsev-efi-vc).
Once configured, SEV-SNP unit tests can be tested as follows:
export QEMU=/path/to/qemu-system-x86_64
export EFI_UEFI=/path/to/OVMF.fd
EFI_SNP=y ./x86/efi/run ./x86/amd_sev.efi==========
References
==========
[1] https://lore.kernel.org/all/[email protected]/
=========
Changelog
=========
v1 -> v2:
* Rebased on AMD SEV-ES #VC handler patchset [1]
* Rebased [1] on KUT's master (201b9e8bdc8b).
* Introduced a new test where test issues PSMASH/UNSMASH page state change requests on 2M ranges.
* Introduced a new test where test injects random current page offsets to examine how hypervisor handles page state change requests with non-zero page offsets for 2M large pages.
* Implemented non-identity mappings for all page state change tests (4K/2M) to avoid any odd side-effects arising due to page state conversions on identity mapped pages.
* Implemented a new helper (vmalloc_pages()) to allocate non-identity mapped pages that would also be helpful for non-SNP related tests.
* Introduced pgtable_va_to_pa() to help convert a guest virtual address to guest physical address by doing page table walk that would be helpful when we have non identity mapped pages where virt_to_phys() won't work.
* Introduced snp_free_pages() interface to help bring back all the pages back to default guest-owned states before freeing up the physical pages after the SNP tests finish.
* Got rid of the check where end_entry is being forcefully truncated to 252 when sizeof(struct snp_psc_desc) exceeds GHCB shared buffer size. Instead, VMGEXIT_PSC_MAX_ENTRY is now calculated based on the size of the GHCB shared buffer.
* Introduced add_psc_entry() helper to facilitate easy addition of a new page state change entry for patches that need individual entries to be added.
* Dropped "RFC" tag.
* v1: https://lore.kernel.org/all/[email protected]/
Pavan Kumar Paluri (16):
x86/apic: Use common library outb() implementation x86/apic: Add MMIO access support for SEV-ES/SNP guest with C-bit
unset
x86 AMD SEV-ES: Rename setup_amd_sev_es() to setup_vc_handler() x86/efi: Add support for running tests with UEFI in SEV-SNP
environment
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: Introduce gva to gpa address translation helper
x86: Add support for installing large pages
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: Change guest pages from Intermix->Private using GHCB
NAE
x86 AMD SEV-SNP: Change guest pages from Intermix->Shared using GHCB
NAE
x86 AMD SEV-SNP: Issue PSMASH/UNSMASH PSC requests on 2M ranges x86 AMD SEV-SNP: Inject random cur_page offsets for 2M ranges
lib/asm-generic/page.h | 2 + lib/linux/efi.h | 1 + lib/x86/amd_sev.c | 357 ++++++++++++++++++++++++++++++++++- lib/x86/amd_sev.h | 124 +++++++++++- lib/x86/amd_sev_vc.c | 2 +- lib/x86/apic.c | 22 ++- lib/x86/setup.c | 15 +- lib/x86/svm.h | 1 + lib/x86/vm.c | 70 +++++++ lib/x86/vm.h | 6 + x86/amd_sev.c | 420 +++++++++++++++++++++++++++++++++++++++++ x86/efi/README.md | 6 + x86/efi/run | 33 +++- 13 files changed, 1038 insertions(+), 21 deletions(-)
Signature · 1 line
2.34.1
PATCH v2 1/16 x86/apic: Use common library outb() implementation
Remove the redundant local outb() implementation in favour of using the common outb() implementation defined in lib/x86/asm/io.h, and convert set_irq_line() to not open-code the out instruction.
Verfied no changes in assembly output for all three callsites, tested, no functional changes were observed.
The rationale behind this change is that, support for SNP tests that are introduced later will need apic sources to include common library io code and if we don't remove apic's local outb() implementation, then we get the following compilation conflicts:
In file included from lib/x86/apic.c:7:lib/x86/asm/io.h:30:14: error: conflicting types for ‘outb’; have ‘void(unsigned char, short unsigned int)’
30 | #define outb outb
| ^~~~
--- lib/x86/apic.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
lib/x86/apic.c +2 -6
@@ -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 );
@@ -232,7 +228,7 @@ void set_mask(unsigned line, int mask)void set_irq_line (unsigned line,int val) { - asmvolatile ("out %0, %1" : :"a" ((u8 )val),"d" ((u16)(0x2000 + line))); +outb ((u8 )val, (u16)(0x2000 + line)); }void enable_apic (void )
Signature · 1 line
2.34.1
PATCH v2 2/16 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 | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
lib/x86/apic.c +14 -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,19 @@ 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 ); + +flush_tlb (); + *pte &= ~get_amd_sev_c_bit_mask (); +flush_tlb (); + } +xapic_write (APIC_SPIV ,0x1ff ); }
Signature · 1 line
2.34.1
PATCH v2 3/16 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 applies to 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 is useful since it allows some KUT SNP tests to exercise aspects of OVMF's #VC handler support in addition to testing hypervisor support.
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
PATCH v2 4/16 x86/efi: Add support for running tests with UEFI in SEV-SNP environment
In reply to Pavan Kumar PaluriSEV-SNP guests require different QEMU command line parameters in comparison to SEV-ES, so adjust the QEMU options accordingly. It is important to note that SEV-SNP guests have some additional requirements versus an SEV/SEV-ES guest:
- bios: SEV-SNP guests need a UEFI BIOS, and unlike with SEV-ES they cannot be loaded via pflash and instead rely on -bios option.
- cpu: guest CPUID values are validated by SEV-SNP firmware and only a strictly-validated set of features should be advertised to the guest. This will usually require the use of an updated/architected QEMU CPU model version. "-cpu EPYC-v4" is used here as it has most common set of features compared to EPYC-Milan*/EPYC-Turin*/etc. models.
- memory-backend-memfd: To support freeing memory after it is converted from shared->private, QEMU relies on
memory that can be discarded via FALLOC_FL_PUNCH_HOLE, which is provided via object memory-backend-memfd.
Add these options to the QEMU cmdline (in x86/eri/run) for bringing up SEV-SNP guest only when EFI_SNP is enabled.
--- x86/efi/README.md | 6 ++++++ x86/efi/run | 33 +++++++++++++++++++++++++-------- 2 files changed, 31 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 SEV-SNP tests with UEFI + +To run SEV-SNP related unit tests with UEFI: + + EFI_SNP=y ./x86/efi/run ./x86/amd_sev.efi + ## Code structure ### Code from GNU-EFI
x86/efi/run +25 -8
@@ -18,6 +18,7 @@ source config.mak : "${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"
@@ -54,11 +55,27 @@ 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" +if [ "$EFI_SNP" != "y" ]; then + "$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" + +else + "$TEST_DIR/run" \ + -bios "${EFI_UEFI}" \ + -drive file.dir="$EFI_TEST/$EFI_CASE/",file.driver=vvfat,file.rw=on,format=raw,if=virtio \ + -net none \ + -nographic \ + -m 256 \ + -object memory-backend-memfd,id=ram1,size=256M,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 \ + "$@" \ + -smp "$EFI_SMP" +fi
Signature · 1 line
2.34.1
PATCH v2 5/16 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.
Besides, for SEV-SNP, the requirement is that SEV-ES and SEV be enabled. In addition, setup_vc_handler() is common to both SEV-ES and SEV-SNP. Therefore, call setup_vc_handler() only when SEV-ES is enabled.
--- lib/x86/amd_sev.c | 15 +++++++++++++++ lib/x86/amd_sev.h | 2 ++ lib/x86/setup.c | 13 ++++++++++--- x86/amd_sev.c | 13 +++++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-)
lib/x86/amd_sev.c +15 -0
@@ -89,6 +89,21 @@ bool amd_sev_es_enabled(void)return sev_es_enabled; } +bool amd_sev_snp_enabled (void ) +{ +static bool sev_snp_enabled; +static bool initialized; + +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 +10 -3
@@ -331,9 +331,16 @@ 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. +* setup_vc_handler() already checks whether SEV-ES is enabled +* or not before it does anything. However, for an SEV-guest, a +* function call to setup_vc_handler() can be avoided altogether +* by incorporating amd_sev_es_enabled() check below. +*/ +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 +13 -0
@@ -69,6 +69,18 @@ static void test_sev_es_activation(void) } } +static void test_sev_snp_activation (void ) +{ +report_info ("TEST: SEV-SNP Activation test" ); + +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 +104,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
PATCH v2 6/16 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,8 +69,35 @@ 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; +report_info ("TEST: SEV-SNP Activation test" );if (!(rdmsr (MSR_SEV_STATUS ) &SEV_SNP_ENABLED_MASK )) {
@@ -79,6 +106,9 @@ static void test_sev_snp_activation(void) }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
PATCH v2 7/16 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.
It is important to note that setup_ghcb_pte() is also called from setup_page_table() in lib/x86/setup.c. However, page allocation callers return a null address (0x0) for UEFI based tests with the initial page table setup via this path. Hence, a new page table is setup via setup_vm() to allocate valid pages.
--- 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
PATCH v2 8/16 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 page state change GHCB MSR protocol is used to convert a 4K page from private to shared or vice-versa. Add support for this test by allocating a total of 1024 4K pages to ensure the test can handle large cases.
The purpose of this test is to determine whether the hypervisor changes the page state to shared when using the MSR protocol. 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.
After the page state conversion to shared by the hypervisor, ensure the state of the pages are in shared by writing data to these pages while the C-bit in its PTEs is not set. Report a failure otherwise.
Provide support for cleaning up the physical pages by converting the pages to default guest-owned state before freeing them.
Import GHCB MSR PSC related and pvalidate definitions from upstream linux. (arch/x86/inlcude/asm/sev-common.h and arch/x86/include/asm/sev.h)
--- lib/asm-generic/page.h | 2 + lib/x86/amd_sev.c | 158 +++++++++++++++++++++++++++++++++++++++++ lib/x86/amd_sev.h | 69 ++++++++++++++++++ lib/x86/vm.h | 3 + x86/amd_sev.c | 88 +++++++++++++++++++++++ 5 files changed, 320 insertions(+)
lib/asm-generic/page.h +2 -0
@@ -14,6 +14,8 @@#define PAGE_SHIFT 12#define PAGE_SIZE (_AC(1,UL) << PAGE_SHIFT)#define PAGE_MASK (~(PAGE_SIZE-1)) +#define LARGE_PAGE_SIZE (512 * PAGE_SIZE) +#define LARGE_PAGE_MASK (~(LARGE_PAGE_SIZE - 1))#ifndef __ASSEMBLY__
lib/x86/amd_sev.c +158 -0
167 lines not on this page · raw message
lib/x86/amd_sev.h +69 -0
96 lines not on this page · raw message
lib/x86/vm.h +3 -0
@@ -7,6 +7,9 @@#include "asm/io.h" #include "asm/bitops.h" +#define ORDER_4K 0 +#define ORDER_2M 9 +void setup_5level_page_table (void );struct pte_search {
x86/amd_sev.c +88 -0
105 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v2 9/16 x86: Introduce gva to gpa address translation helper
In reply to Pavan Kumar PaluriPerform the translation of a guest virtual address to a guest physical address using the currently installed page tables. This must be used for virtual addresses that are not identity mapped addresses, e.g. a virtual address returned by alloc_vpage(), where virt_to_phys() won't work.
--- lib/x86/vm.c | 24 ++++++++++++++++++++++++ lib/x86/vm.h | 1 + 2 files changed, 25 insertions(+)
lib/x86/vm.c +24 -0
@@ -332,3 +332,27 @@ void walk_pte(void *virt, size_t len, pte_callback_t callback)callback (search, (void *)curr); } } + +unsigned long pgtable_va_to_pa (unsigned long va) +{ +pteval_t *pt = (pgd_t *)read_cr3 (); +unsigned long offset, paddr; +int level; + +for (level =PAGE_LEVEL ; level; level--) { + offset =PGDIR_OFFSET ((uintptr_t )va, level); +assert_msg (pt[offset],"PTE absent" ); + +if (level ==1 || + (level <=3 && (pt[offset] &PT_PAGE_SIZE_MASK ))) { + paddr = pt[offset] &PT_ADDR_MASK ; + paddr += va & ((1UL <<PGDIR_BITS (level)) -1 ); + +return paddr; + } + + pt =phys_to_virt (pt[offset] &PT_ADDR_MASK ); + } + +__builtin_unreachable (); +}
lib/x86/vm.h +1 -0
@@ -59,6 +59,7 @@ static inline void *current_page_table(void)void split_large_page (unsigned long *ptep,int level);void force_4k_page (void *addr); +unsigned long pgtable_va_to_pa (unsigned long vaddr);struct vm_vcpu_info {u64 cr3;
Signature · 1 line
2.34.1
PATCH v2 10/16 x86: Add support for installing large pages
In reply to Pavan Kumar PaluriIntroduce install_large_pages() helper similar to install_pages(), that is helpful while dealing with more than 1 2M large page mappings. This helper is useful when running SEV-SNP VMGEXIT PSC tests that deal with a bunch of 2M pages.
--- lib/x86/vm.c | 14 ++++++++++++++ lib/x86/vm.h | 1 + 2 files changed, 15 insertions(+)
lib/x86/vm.c +14 -0
@@ -128,6 +128,20 @@ void install_pages(pgd_t *cr3, phys_addr_t phys, size_t len, void *virt) } } +void install_large_pages (pgd_t *cr3,phys_addr_t phys,size_t len,void *virt) +{ +phys_addr_t max = (u64 )len + (u64 )phys; +assert (phys %LARGE_PAGE_SIZE ==0 ); +assert ((uintptr_t )virt %LARGE_PAGE_SIZE ==0 ); +assert (len %LARGE_PAGE_SIZE ==0 ); + +while (phys +LARGE_PAGE_SIZE <= max) { +install_large_page (cr3, phys, virt); + phys +=LARGE_PAGE_SIZE ; + virt = (char *)virt +LARGE_PAGE_SIZE ; + } +} +bool any_present_pages (pgd_t *cr3,void *virt,size_t len) {uintptr_t max = (uintptr_t ) virt + len;
lib/x86/vm.h +1 -0
@@ -41,6 +41,7 @@ pteval_t *install_pte(pgd_t *cr3,pteval_t *install_large_page (pgd_t *cr3,phys_addr_t phys,void *virt);void install_pages (pgd_t *cr3,phys_addr_t phys,size_t len,void *virt); +void install_large_pages (pgd_t *cr3,phys_addr_t phys,size_t len,void *virt);bool any_present_pages (pgd_t *cr3,void *virt,size_t len);void set_pte_opt_mask (void );void reset_pte_opt_mask (void );
Signature · 1 line
2.34.1
PATCH v2 11/16 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 handles both 4K pages and 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_SIZEMISMATCH 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 shared guest pages post page state conversions with C-bits unset appropriately to ensure pages are in expected shared state.
--- lib/x86/amd_sev.c | 168 +++++++++++++++++++++++++++++++++++++++++++ lib/x86/amd_sev.h | 27 +++++++ lib/x86/amd_sev_vc.c | 2 +- lib/x86/svm.h | 1 + lib/x86/vm.c | 26 +++++++ lib/x86/vm.h | 1 + x86/amd_sev.c | 78 +++++++++++++++++++- 7 files changed, 301 insertions(+), 2 deletions(-)
lib/x86/amd_sev.c +168 -0
@@ -346,3 +346,171 @@ enum es_result __sev_set_pages_state_msr_proto(unsigned long vaddr, int npages,return ES_OK ; } + +static void pvalidate_pages (struct snp_psc_desc *desc,unsigned long *vaddr_arr) +{ +struct psc_entry *entry; +int ret, i; +unsigned long vaddr; +bool validate; + +for (i =0 ; i <= desc->hdr.end_entry; i++) { + vaddr = vaddr_arr[i]; + entry = &desc->entries[i]; + validate = entry->operation ==SNP_PAGE_STATE_PRIVATE ? true : false; + + ret =pvalidate (vaddr, entry->pagesize, validate); +if (ret ==PVALIDATE_FAIL_SIZEMISMATCH ) { +assert (entry->pagesize ==RMP_PG_SIZE_2M ); +unsigned long vaddr_end = vaddr +LARGE_PAGE_SIZE ; + +for (; vaddr < vaddr_end; vaddr +=PAGE_SIZE ) { + ret =pvalidate (vaddr,RMP_PG_SIZE_4K , validate); +if (ret) +break ; + } + } +assert (!ret); + } +} + +static int verify_exception (struct ghcb *ghcb) +{ +return ghcb->save.sw_exit_info_1 &GENMASK_ULL (31 ,0 ); +} + +static int sev_ghcb_hv_call (struct ghcb *ghcb,u64 exit_code, +u64 exit_info_1,u64 exit_info_2) +{ + ghcb->version =GHCB_PROTOCOL_MAX ; + ghcb->ghcb_usage =GHCB_DEFAULT_USAGE ; + +ghcb_set_sw_exit_code (ghcb, exit_code); +ghcb_set_sw_exit_info_1 (ghcb, exit_info_1); +ghcb_set_sw_exit_info_2 (ghcb, exit_info_2); + +VMGEXIT (); + +return verify_exception (ghcb); +} + +static int vmgexit_psc (struct snp_psc_desc *desc,struct ghcb *ghcb) +{ +int cur_entry, end_entry, ret =0 ; +struct snp_psc_desc *data; + +/* Ensure end_entry is within bounds */ +assert (desc->hdr.end_entry <VMGEXIT_PSC_MAX_ENTRY ); + +vc_ghcb_invalidate (ghcb); + + data = (struct snp_psc_desc *)ghcb->shared_buffer; +memcpy (ghcb->shared_buffer, desc,GHCB_SHARED_BUF_SIZE ); + + cur_entry = data->hdr.cur_entry; + end_entry = data->hdr.end_entry; + +while (data->hdr.cur_entry <= data->hdr.end_entry) { +ghcb_set_sw_scratch (ghcb, (u64 )__pa (data)); + + ret =sev_ghcb_hv_call (ghcb,SVM_VMGEXIT_PSC ,0 ,0 ); + +if (ret) { +report_info ("SNP: PSC failed with ret: %d\n" , ret); + ret =1 ; +break ; + } + +if (cur_entry > data->hdr.cur_entry) { +report_info ("SNP: PSC processing going backward, cur_entry %d (got %d)\n" , + cur_entry, data->hdr.cur_entry); + ret =1 ; +break ; + } + +if (data->hdr.end_entry != end_entry) { +report_info ("End entry mismatch: end_entry %d (got %d)\n" , + end_entry, data->hdr.end_entry); + ret =1 ; +break ; + } + +if (data->hdr.reserved) { +report_info ("Reserved bit is set in the PSC header\n" ); + ret =1 ; +break ; + } + } + +/* Copy the output in shared buffer back to desc */ +memcpy (desc, ghcb->shared_buffer,GHCB_SHARED_BUF_SIZE ); + +return ret; +} + +static void add_psc_entry (struct snp_psc_desc *desc,u8 idx,u8 op,unsigned long vaddr, +bool large_entry,u16 cur_page_offset) +{ +struct psc_hdr *hdr = &desc->hdr; +struct psc_entry *entry = &desc->entries[idx]; + +assert_msg (!large_entry ||IS_ALIGNED (vaddr,LARGE_PAGE_SIZE ), +"Must use 2M-aligned addresses for large PSC entries" ); + + entry->gfn =pgtable_va_to_pa (vaddr) >>PAGE_SHIFT ; + entry->operation = op; + entry->pagesize = large_entry; + entry->cur_page = cur_page_offset; + hdr->end_entry = idx; +} + +unsigned long __sev_set_pages_state (struct snp_psc_desc *desc,unsigned long vaddr, +unsigned long vaddr_end,int op, +struct ghcb *ghcb,bool large_entry) +{ +unsigned long vaddr_arr[VMGEXIT_PSC_MAX_ENTRY ]; +int ret, iter =0 , iter2 =0 ; +u8 page_size; + +memset (desc,0 ,sizeof (*desc)); + +report_info ("%s: address start %lx end %lx op %d large %d" , + __func__, vaddr, vaddr_end, op, large_entry); + +while (vaddr < vaddr_end && iter <ARRAY_SIZE (desc->entries)) { + vaddr_arr[iter] = vaddr; + +if (large_entry &&IS_ALIGNED (vaddr,LARGE_PAGE_SIZE ) && + (vaddr_end - vaddr) >=LARGE_PAGE_SIZE ) { +add_psc_entry (desc, iter, op, vaddr, true,0 ); + vaddr +=LARGE_PAGE_SIZE ; + }else { +add_psc_entry (desc, iter, op, vaddr, false,0 ); + vaddr +=PAGE_SIZE ; + } + + iter++; + } + +if (op ==SNP_PAGE_STATE_SHARED ) +pvalidate_pages (desc, vaddr_arr); + + ret =vmgexit_psc (desc, ghcb); +assert_msg (!ret,"VMGEXIT failed with ret value: %d" , ret); + +if (op ==SNP_PAGE_STATE_PRIVATE ) +pvalidate_pages (desc, vaddr_arr); + +for (iter2 =0 ; iter2 < iter; iter2++) { + page_size = desc->entries[iter2].pagesize; + +if (page_size ==RMP_PG_SIZE_2M ) +assert_msg (desc->entries[iter2].cur_page ==512 , +"Failed to process sub-entries within 2M range" ); +else if (page_size ==RMP_PG_SIZE_4K ) +assert_msg (desc->entries[iter2].cur_page ==1 , +"Failed to process 4K entry" ); + } + +return vaddr; +}
lib/x86/amd_sev.h +27 -0
@@ -208,6 +208,29 @@ 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 , + gfn :40 , + operation :4 , + pagesize :1 , + reserved :7 ; +}; + +#define VMGEXIT_PSC_MAX_ENTRY \ + ((GHCB_SHARED_BUF_SIZE - sizeof(struct psc_hdr)) / \ + sizeof(struct psc_entry)) + +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 );
@@ -219,6 +242,10 @@ void set_pte_encrypted(unsigned long vaddr, int npages);bool is_validated_private_page (unsigned long vaddr,bool rmp_size);enum es_result __sev_set_pages_state_msr_proto (unsigned long vaddr,int npages,int operation); +unsigned long __sev_set_pages_state (struct snp_psc_desc *desc,unsigned long vaddr, +unsigned long vaddr_end,int op, +struct ghcb *ghcb,bool large_entry); +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
@@ -11,7 +11,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
@@ -372,6 +372,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)
lib/x86/vm.c +26 -0
@@ -370,3 +370,29 @@ unsigned long pgtable_va_to_pa(unsigned long va)__builtin_unreachable (); } + +void *vmalloc_pages (int num_pages,int order,bool large_page) +{ +unsigned long length = num_pages *PAGE_SIZE ; +pgd_t *cr3 = (pgd_t *)read_cr3 (); +void *vaddr, *paddr; + +/* Allocate physical pages */ + paddr =alloc_pages (order); +assert (paddr); + +/* Allocate virtual pages */ + vaddr =alloc_vpages_aligned (num_pages, large_page ?ORDER_2M :ORDER_4K ); +assert (vaddr); + +/* +* Create pagetable entries that map the newly assigned virtual +* pages to physical pages +*/ +if (!large_page) +install_pages (cr3,__pa (paddr), length, vaddr); +else +install_large_pages (cr3,__pa (paddr), length, vaddr); + +return vaddr; +}
lib/x86/vm.h +1 -0
@@ -61,6 +61,7 @@ static inline void *current_page_table(void)void split_large_page (unsigned long *ptep,int level);void force_4k_page (void *addr);unsigned long pgtable_va_to_pa (unsigned long vaddr); +void *vmalloc_pages (int num_pages,int order,bool large_page);struct vm_vcpu_info {u64 cr3;
x86/amd_sev.c +77 -1
97 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v2 12/16 x86 AMD SEV-SNP: Change guest pages from Shared->Private using GHCB NAE
In reply to Pavan Kumar PaluriConvert the same pages back to private that were converted to shared.
The test handles both 4K and 2M large pages depending on the order and the page size specified.
While at it, make changes to pvalidate_pages() to not treat PVALIDATE_FAIL_NOUPDATE as an error when converting the already private pages as a part of cleanup process.
--- lib/x86/amd_sev.c | 22 ++++++++++++++++------ lib/x86/amd_sev.h | 3 ++- x86/amd_sev.c | 23 +++++++++++++++++------ 3 files changed, 35 insertions(+), 13 deletions(-)
lib/x86/amd_sev.c +16 -6
@@ -347,7 +347,16 @@ enum es_result __sev_set_pages_state_msr_proto(unsigned long vaddr, int npages,return ES_OK ; } -static void pvalidate_pages (struct snp_psc_desc *desc,unsigned long *vaddr_arr) +static bool pvalidate_failed (int result,bool allow_noupdate) +{ +if (result && (!allow_noupdate || result !=PVALIDATE_FAIL_NOUPDATE )) +return true; + +return false; +} + +static void pvalidate_pages (struct snp_psc_desc *desc,unsigned long *vaddr_arr, +bool allow_noupdate) {struct psc_entry *entry;int ret, i;
@@ -366,11 +375,11 @@ static void pvalidate_pages(struct snp_psc_desc *desc, unsigned long *vaddr_arr)for (; vaddr < vaddr_end; vaddr +=PAGE_SIZE ) { ret =pvalidate (vaddr,RMP_PG_SIZE_4K , validate); -if (ret) +if (pvalidate_failed (ret, allow_noupdate))break ; } } -assert (!ret); +assert (!pvalidate_failed (ret, allow_noupdate)); } }
@@ -466,7 +475,8 @@ static void add_psc_entry(struct snp_psc_desc *desc, u8 idx, u8 op, unsigned lonunsigned long __sev_set_pages_state (struct snp_psc_desc *desc,unsigned long vaddr,unsigned long vaddr_end,int op, -struct ghcb *ghcb,bool large_entry) +struct ghcb *ghcb,bool large_entry, +bool allow_noupdate) {unsigned long vaddr_arr[VMGEXIT_PSC_MAX_ENTRY ];int ret, iter =0 , iter2 =0 ;
@@ -493,13 +503,13 @@ unsigned long __sev_set_pages_state(struct snp_psc_desc *desc, unsigned long vad }if (op ==SNP_PAGE_STATE_SHARED ) -pvalidate_pages (desc, vaddr_arr); +pvalidate_pages (desc, vaddr_arr, allow_noupdate); ret =vmgexit_psc (desc, ghcb);assert_msg (!ret,"VMGEXIT failed with ret value: %d" , ret);if (op ==SNP_PAGE_STATE_PRIVATE ) -pvalidate_pages (desc, vaddr_arr); +pvalidate_pages (desc, vaddr_arr, allow_noupdate);for (iter2 =0 ; iter2 < iter; iter2++) { page_size = desc->entries[iter2].pagesize;
lib/x86/amd_sev.h +2 -1
@@ -244,7 +244,8 @@ enum es_result __sev_set_pages_state_msr_proto(unsigned long vaddr, int npages, int operation);unsigned long __sev_set_pages_state (struct snp_psc_desc *desc,unsigned long vaddr,unsigned long vaddr_end,int op, -struct ghcb *ghcb,bool large_entry); +struct ghcb *ghcb,bool large_entry, +bool allow_noupdate);void vc_ghcb_invalidate (struct ghcb *ghcb);unsigned long long get_amd_sev_c_bit_mask (void );
x86/amd_sev.c +17 -6
@@ -174,7 +174,7 @@ static int test_write(unsigned long vaddr, int npages) }static void sev_set_pages_state (unsigned long vaddr,int npages,int op, -struct ghcb *ghcb) +struct ghcb *ghcb,bool allow_noupdate) {struct snp_psc_desc desc;unsigned long vaddr_end;
@@ -188,17 +188,19 @@ static void sev_set_pages_state(unsigned long vaddr, int npages, int op,while (vaddr < vaddr_end) { vaddr =__sev_set_pages_state (&desc, vaddr, vaddr_end, - op, ghcb, large_entry); + op, ghcb, large_entry, + allow_noupdate); } }static void snp_free_pages (int order,int npages,unsigned long vaddr, -struct ghcb *ghcb) +struct ghcb *ghcb,bool allow_noupdate) {set_pte_encrypted (vaddr,SEV_ALLOC_PAGE_COUNT );/* Convert pages back to default guest-owned state */ -sev_set_pages_state (vaddr, npages,SNP_PAGE_STATE_PRIVATE , ghcb); +sev_set_pages_state (vaddr, npages,SNP_PAGE_STATE_PRIVATE , ghcb, + allow_noupdate);/* Free all the associated physical pages */ free_pages_by_order ((void *)pgtable_va_to_pa (vaddr), order);
@@ -268,7 +270,7 @@ static void test_sev_psc_ghcb_nae(void) "Expected page state: Private");sev_set_pages_state (vaddr,SEV_ALLOC_PAGE_COUNT ,SNP_PAGE_STATE_SHARED , - ghcb); + ghcb, false);set_pte_decrypted (vaddr,SEV_ALLOC_PAGE_COUNT );
@@ -276,7 +278,16 @@ static void test_sev_psc_ghcb_nae(void)"Write to %d unencrypted 2M pages after private->shared conversion" , (SEV_ALLOC_PAGE_COUNT) / (1 << ORDER_2M)); -snp_free_pages (SEV_ALLOC_ORDER ,SEV_ALLOC_PAGE_COUNT , vaddr, ghcb); +/* Convert pages from shared->private */ +set_pte_encrypted (vaddr,SEV_ALLOC_PAGE_COUNT ); + +sev_set_pages_state (vaddr,SEV_ALLOC_PAGE_COUNT ,SNP_PAGE_STATE_PRIVATE , + ghcb, false); + +report (is_validated_private_page (vaddr,RMP_PG_SIZE_2M ), +"Expected page state: Private" ); + +snp_free_pages (SEV_ALLOC_ORDER ,SEV_ALLOC_PAGE_COUNT , vaddr, ghcb, true); }int main (void )
Signature · 1 line
2.34.1
PATCH v2 13/16 x86 AMD SEV-SNP: Change guest pages from Intermix->Private using GHCB NAE
In reply to Pavan Kumar PaluriThe tests perform 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 the 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. Converts the whole 2M range from an intermixed state to private and perform a re-validation check on the now-private 2M page.
The goal of this test is to ensure 2M page state changes are handled properly even if 2M range contains a mix of private/shared pages.
--- x86/amd_sev.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+)
x86/amd_sev.c +54 -0
@@ -290,6 +290,59 @@ static void test_sev_psc_ghcb_nae(void)snp_free_pages (SEV_ALLOC_ORDER ,SEV_ALLOC_PAGE_COUNT , vaddr, ghcb, true); } +static void __test_sev_psc_private (unsigned long vaddr,struct ghcb *ghcb, +int npages,bool allow_noupdate) +{ +set_pte_encrypted (vaddr, npages); + +/* Convert the whole 2M range back to private */ +sev_set_pages_state (vaddr, npages,SNP_PAGE_STATE_PRIVATE , ghcb, + allow_noupdate); + +report (is_validated_private_page (vaddr,RMP_PG_SIZE_2M ), +"Expected page state: Private" ); +} + +static void test_sev_psc_intermix (bool to_private) +{ +unsigned long vaddr; +struct ghcb *ghcb = (struct ghcb *)(rdmsr (SEV_ES_GHCB_MSR_INDEX )); + +/* Allocate a 2M private page */ + vaddr = (unsigned long )vmalloc_pages ((SEV_ALLOC_PAGE_COUNT ) /2 , +SEV_ALLOC_ORDER -1 ,RMP_PG_SIZE_2M ); + +/* Ensure pages are in private state by checking the page is private */ +report (is_validated_private_page (vaddr,RMP_PG_SIZE_2M ), +"Expected page state: Private" ); + +sev_set_pages_state (vaddr, (SEV_ALLOC_PAGE_COUNT ) /2 , +SNP_PAGE_STATE_SHARED , ghcb, false); + +set_pte_decrypted (vaddr, (SEV_ALLOC_PAGE_COUNT ) /2 ); + +set_pte_encrypted (vaddr, (SEV_ALLOC_PAGE_COUNT ) /2 ); +/* Convert a bunch of sub-pages (256) to private and leave the rest shared */ +sev_set_pages_state (vaddr,256 ,SNP_PAGE_STATE_PRIVATE , ghcb, false); + +report (is_validated_private_page (vaddr,RMP_PG_SIZE_4K ), +"Expected page state: Private" ); + +/* Now convert all the pages back to private */ +if (to_private) +__test_sev_psc_private (vaddr, ghcb, (SEV_ALLOC_PAGE_COUNT ) /2 , true); + +/* Free up all the used pages */ +snp_free_pages (SEV_ALLOC_ORDER -1 , (SEV_ALLOC_PAGE_COUNT ) /2 , + vaddr, ghcb, true); +} + +static void test_sev_psc_intermix_to_private (void ) +{ +report_info ("TEST: 2M Intermixed to Private PSC test" ); +test_sev_psc_intermix (true); +} +int main (void ) {int rtn;
@@ -309,6 +362,7 @@ int main(void)init_vpages ();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
PATCH v2 14/16 x86 AMD SEV-SNP: Change guest pages from Intermix->Shared using GHCB NAE
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 partial page state changes (shared->private) on first 256 4K entries and conducts a re-validation ('pvalidate') check on one of these entries to ensure its state has been changed to private.
3. Performs PSC from intermixed state to shared state on the 2M large page.
4. Conducts a write test on the shared pages to ensure page state change has been successful.
The main goal of this test is to ensure 2M page state changes are handled properly even if 2M range contains a mix of private/shared pages.
--- x86/amd_sev.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
x86/amd_sev.c +24 -0
@@ -303,6 +303,21 @@ static void __test_sev_psc_private(unsigned long vaddr, struct ghcb *ghcb, "Expected page state: Private"); } +static void __test_sev_psc_shared (unsigned long vaddr,struct ghcb *ghcb, +int npages,bool allow_noupdate) +{ +/* Convert the whole 2M range to shared */ +sev_set_pages_state (vaddr, npages,SNP_PAGE_STATE_SHARED , ghcb, + allow_noupdate); + +set_pte_decrypted (vaddr, npages); + +/* Conduct a write test to ensure pages are in expected state */ +report (!test_write (vaddr, npages), +"Write to %d unencrypted 2M pages after private->shared conversion" , + npages / (1 <<ORDER_2M )); +} +static void test_sev_psc_intermix (bool to_private) {unsigned long vaddr;
@@ -331,6 +346,8 @@ static void test_sev_psc_intermix(bool to_private)/* Now convert all the pages back to private */ if (to_private)__test_sev_psc_private (vaddr, ghcb, (SEV_ALLOC_PAGE_COUNT ) /2 , true); +else +__test_sev_psc_shared (vaddr, ghcb, (SEV_ALLOC_PAGE_COUNT ) /2 , true);/* Free up all the used pages */ snp_free_pages(SEV_ALLOC_ORDER - 1, (SEV_ALLOC_PAGE_COUNT) / 2,
@@ -343,6 +360,12 @@ static void test_sev_psc_intermix_to_private(void)test_sev_psc_intermix (true); } +static void test_sev_psc_intermix_to_shared (void ) +{ +report_info ("TEST: 2M Intermixed to Shared PSC test" ); +test_sev_psc_intermix (false); +} +int main (void ) {int rtn;
@@ -363,6 +386,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
PATCH v2 15/16 x86 AMD SEV-SNP: Issue PSMASH/UNSMASH PSC requests on 2M ranges
In reply to Pavan Kumar PaluriGHCB spec specifies that an SNP guest can submit PSMASH/UNSMASH hints to hypervisor via PSC requests. Include a test to create such a PSC request where KUT-SNP guest requests hypervisor to PSMASH/UNSMASH 2M ranges, to ensure hypervisor handles these requests without any issues.
--- lib/x86/amd_sev.c | 10 +++---- lib/x86/amd_sev.h | 5 ++++ x86/amd_sev.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 5 deletions(-)
lib/x86/amd_sev.c +5 -5
@@ -355,8 +355,8 @@ static bool pvalidate_failed(int result, bool allow_noupdate)return false; } -static void pvalidate_pages (struct snp_psc_desc *desc,unsigned long *vaddr_arr, -bool allow_noupdate) +void pvalidate_pages (struct snp_psc_desc *desc,unsigned long *vaddr_arr, +bool allow_noupdate) {struct psc_entry *entry;int ret, i;
@@ -403,7 +403,7 @@ static int sev_ghcb_hv_call(struct ghcb *ghcb, u64 exit_code,return verify_exception (ghcb); } -static int vmgexit_psc (struct snp_psc_desc *desc,struct ghcb *ghcb) +int vmgexit_psc (struct snp_psc_desc *desc,struct ghcb *ghcb) {int cur_entry, end_entry, ret =0 ;struct snp_psc_desc *data;
@@ -457,8 +457,8 @@ static int vmgexit_psc(struct snp_psc_desc *desc, struct ghcb *ghcb)return ret; } -static void add_psc_entry (struct snp_psc_desc *desc,u8 idx,u8 op,unsigned long vaddr, -bool large_entry,u16 cur_page_offset) +void add_psc_entry (struct snp_psc_desc *desc,u8 idx,u8 op,unsigned long vaddr, +bool large_entry,u16 cur_page_offset) {struct psc_hdr *hdr = &desc->hdr;struct psc_entry *entry = &desc->entries[idx];
lib/x86/amd_sev.h +5 -0
@@ -247,6 +247,11 @@ unsigned long __sev_set_pages_state(struct snp_psc_desc *desc, unsigned long vad struct ghcb *ghcb, bool large_entry, bool allow_noupdate);void vc_ghcb_invalidate (struct ghcb *ghcb); +void pvalidate_pages (struct snp_psc_desc *desc,unsigned long *vaddr_arr, +bool allow_noupdate); +int vmgexit_psc (struct snp_psc_desc *desc,struct ghcb *ghcb); +void add_psc_entry (struct snp_psc_desc *desc,u8 idx,u8 op, +unsigned long vaddr,bool large_entry,u16 offset);unsigned long long get_amd_sev_c_bit_mask (void );unsigned long long get_amd_sev_addr_upperbound (void );
x86/amd_sev.c +66 -0
78 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v2 16/16 x86 AMD SEV-SNP: Inject random cur_page offsets for 2M ranges
In reply to Pavan Kumar PaluriWhile issuing a page state change request using VMGEXIT for 2M large ranges, a normal SEV-SNP guest usually sets cur_page offset as zero and it is then the responsibility of the hypervisor to ensure all the 512 4K pages within 2M range are successfully processed or not.
Introduce a test case where a malicious guest sets random cur_page offsets to demonstrate how hypervisor handles such partial page state change requests beginning at a random cur_page offset.
--- x86/amd_sev.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+)
x86/amd_sev.c +58 -0
@@ -431,6 +431,63 @@ static void test_sev_snp_psmash(void) ghcb, true); } +static void __test_sev_snp_page_offset (int cur_page_offset) +{ +struct ghcb *ghcb = (struct ghcb *)(rdmsr (SEV_ES_GHCB_MSR_INDEX )); +struct snp_psc_desc desc = {0 }; +unsigned long vaddr, vaddr_start; +int ret, iter; + +/* Allocate a 2M large page */ + vaddr = (unsigned long )vmalloc_pages ((SEV_ALLOC_PAGE_COUNT ) /2 , +SEV_ALLOC_ORDER -1 , +RMP_PG_SIZE_2M ); +/* +* Create a PSC private->shared request where a non-zero +* cur_page offset is set to examine how hypervisor handles such +* requests. +*/ +add_psc_entry (&desc,0 ,SNP_PAGE_STATE_SHARED , vaddr, true, + cur_page_offset); + + ret =vmgexit_psc (&desc, ghcb); +assert_msg (!ret,"VMGEXIT failed with ret value: %d" , ret); + +/* +* Conduct a re-validation test to examine if the pages within 1 +* to cur_page offset are still in their expected private state. +*/ + vaddr_start = vaddr; +for (iter =0 ; iter < cur_page_offset; iter++) { + ret =is_validated_private_page (vaddr_start,RMP_PG_SIZE_4K ); +assert_msg (ret,"Page not in expected private state" ); + vaddr_start +=PAGE_SIZE ; + } + +pvalidate_pages (&desc, &vaddr, true); + +/* Free up the used pages */ +snp_free_pages (SEV_ALLOC_ORDER -1 , (SEV_ALLOC_PAGE_COUNT ) /2 , + vaddr, ghcb, true); +} + +static void test_sev_snp_page_offset (void ) +{ +int iter; +/* +* Set a pool of current page offsets such that all +* possible edge-cases are covered in order to examine +* how hypervisor handles PSC requests with non-zero cur_page +* offsets. +*/ +int cur_page_offsets[] = {0 ,1 ,256 ,511 ,512 }; + +report_info ("TEST: Injecting non-zero current page offsets" ); + +for (iter =0 ; iter <ARRAY_SIZE (cur_page_offsets); iter++) +__test_sev_snp_page_offset (cur_page_offsets[iter]); +} +int main (void ) {int rtn;
@@ -453,6 +510,7 @@ int main(void)test_sev_psc_intermix_to_private ();test_sev_psc_intermix_to_shared ();test_sev_snp_psmash (); +test_sev_snp_page_offset (); }return report_summary ();
Signature · 1 line
2.34.1