KVM: selftests: Add simple SEV test
v6 cover KVM: selftests: Add simple SEV test
This patch series continues the work Michael Roth has done in supporting SEV guests in selftests. It continues on top of the work Sean Christopherson has sent to support ucalls from SEV guests. Along with a very simple version of the SEV selftests Michael originally proposed.
V6
* Updated SEV VM create function based on Seanjc's feedback and new changes to VM creation functions.
* Removed pte_me_mask based on feedback.
* Fixed s_bit usage based on TDX
* Fixed bugs and took Ackerly's code for enc_region setup code.
V5
* Rebase onto seanjc@'s latest ucall pool series.
* More review changes based on seanjc:
** use protected instead of encrypted outside of SEV specific files ** Swap memcrypt struct for kvm_vm_arch arch specific struct
** Make protected page table data agnostic of address bit stealing specifics
of SEV
** Further clean up for SEV library to just vm_sev_create_one_vcpu()
* Due to large changes moved more authorships from mroth@ to pgonda@. Gave originally-by tags to mroth@ as suggested by Seanjc for this.
V4
* Rebase ontop of seanjc@'s latest Ucall Pool series:
https://lore.kernel.org/linux-arm-kernel/[email protected]/
* Fix up review comments from seanjc
* Switch authorship on 2 patches because of significant changes, added
* Michael as suggested-by or originally-by.
V3
* Addressed more of andrew.jones@ in ucall patches.
* Fix build in non-x86 archs.
V2
* Dropped RFC tag
* Correctly separated Sean's ucall patches into 2 as originally intended.
* Addressed andrew.jones@ in ucall patches.
* Fixed ucall pool usage to work for other archs
V1
* https://lore.kernel.org/all/[email protected]/
Michael Roth (2):
KVM: selftests: sparsebit: add const where appropriate
KVM: selftests: add support for protected vm_vaddr_* allocations
Peter Gonda (5):
KVM: selftests: add hooks for managing protected guest memory
KVM: selftests: handle protected bits in page tables
KVM: selftests: add library for creating/interacting with SEV guests
KVM: selftests: Update ucall pool to allocate from shared memory
KVM: selftests: Add simple sev vm testing
tools/arch/arm64/include/asm/kvm_host.h | 7 + tools/arch/riscv/include/asm/kvm_host.h | 7 + tools/arch/s390/include/asm/kvm_host.h | 7 + tools/arch/x86/include/asm/kvm_host.h | 14 + tools/testing/selftests/kvm/.gitignore | 84 ++++++ tools/testing/selftests/kvm/Makefile | 4 +- .../selftests/kvm/include/kvm_util_base.h | 49 +++- .../testing/selftests/kvm/include/sparsebit.h | 36 +-- .../selftests/kvm/include/x86_64/processor.h | 1 + .../selftests/kvm/include/x86_64/sev.h | 27 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 66 ++++- tools/testing/selftests/kvm/lib/sparsebit.c | 48 ++-- .../testing/selftests/kvm/lib/ucall_common.c | 2 +- .../selftests/kvm/lib/x86_64/processor.c | 62 ++++- tools/testing/selftests/kvm/lib/x86_64/sev.c | 254 ++++++++++++++++++ .../selftests/kvm/x86_64/sev_all_boot_test.c | 84 ++++++ 16 files changed, 685 insertions(+), 67 deletions(-) create mode 100644 tools/arch/arm64/include/asm/kvm_host.h create mode 100644 tools/arch/riscv/include/asm/kvm_host.h create mode 100644 tools/arch/s390/include/asm/kvm_host.h create mode 100644 tools/arch/x86/include/asm/kvm_host.h create mode 100644 tools/testing/selftests/kvm/include/x86_64/sev.h create mode 100644 tools/testing/selftests/kvm/lib/x86_64/sev.c create mode 100644 tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
PATCH v6 1/7 KVM: selftests: sparsebit: add const where appropriate
From: Michael Roth <[email protected]>
Subsequent patches will introduce an encryption bitmap in kvm_util that would be useful to allow tests to access in read-only fashion. This will be done via a const sparsebit*. To avoid warnings or the need to add casts everywhere, add const to the various sparsebit functions that are applicable for read-only usage of sparsebit.
--- .../testing/selftests/kvm/include/sparsebit.h | 36 +++++++------- tools/testing/selftests/kvm/lib/sparsebit.c | 48 +++++++++---------- 2 files changed, 42 insertions(+), 42 deletions(-)
tools/testing/selftests/kvm/include/sparsebit.h +18 -18
@@ -30,26 +30,26 @@ typedef uint64_t sparsebit_num_t;struct sparsebit *sparsebit_alloc (void );void sparsebit_free (struct sparsebit **sbitp); -void sparsebit_copy (struct sparsebit *dstp,struct sparsebit *src); +void sparsebit_copy (struct sparsebit *dstp,const struct sparsebit *src); -bool sparsebit_is_set (struct sparsebit *sbit,sparsebit_idx_t idx); -bool sparsebit_is_set_num (struct sparsebit *sbit, +bool sparsebit_is_set (const struct sparsebit *sbit,sparsebit_idx_t idx); +bool sparsebit_is_set_num (const struct sparsebit *sbit,sparsebit_idx_t idx,sparsebit_num_t num); -bool sparsebit_is_clear (struct sparsebit *sbit,sparsebit_idx_t idx); -bool sparsebit_is_clear_num (struct sparsebit *sbit, +bool sparsebit_is_clear (const struct sparsebit *sbit,sparsebit_idx_t idx); +bool sparsebit_is_clear_num (const struct sparsebit *sbit,sparsebit_idx_t idx,sparsebit_num_t num); -sparsebit_num_t sparsebit_num_set (struct sparsebit *sbit); -bool sparsebit_any_set (struct sparsebit *sbit); -bool sparsebit_any_clear (struct sparsebit *sbit); -bool sparsebit_all_set (struct sparsebit *sbit); -bool sparsebit_all_clear (struct sparsebit *sbit); -sparsebit_idx_t sparsebit_first_set (struct sparsebit *sbit); -sparsebit_idx_t sparsebit_first_clear (struct sparsebit *sbit); -sparsebit_idx_t sparsebit_next_set (struct sparsebit *sbit,sparsebit_idx_t prev); -sparsebit_idx_t sparsebit_next_clear (struct sparsebit *sbit,sparsebit_idx_t prev); -sparsebit_idx_t sparsebit_next_set_num (struct sparsebit *sbit, +sparsebit_num_t sparsebit_num_set (const struct sparsebit *sbit); +bool sparsebit_any_set (const struct sparsebit *sbit); +bool sparsebit_any_clear (const struct sparsebit *sbit); +bool sparsebit_all_set (const struct sparsebit *sbit); +bool sparsebit_all_clear (const struct sparsebit *sbit); +sparsebit_idx_t sparsebit_first_set (const struct sparsebit *sbit); +sparsebit_idx_t sparsebit_first_clear (const struct sparsebit *sbit); +sparsebit_idx_t sparsebit_next_set (const struct sparsebit *sbit,sparsebit_idx_t prev); +sparsebit_idx_t sparsebit_next_clear (const struct sparsebit *sbit,sparsebit_idx_t prev); +sparsebit_idx_t sparsebit_next_set_num (const struct sparsebit *sbit,sparsebit_idx_t start,sparsebit_num_t num); -sparsebit_idx_t sparsebit_next_clear_num (struct sparsebit *sbit, +sparsebit_idx_t sparsebit_next_clear_num (const struct sparsebit *sbit,sparsebit_idx_t start,sparsebit_num_t num);void sparsebit_set (struct sparsebit *sbitp,sparsebit_idx_t idx);
@@ -62,9 +62,9 @@ void sparsebit_clear_num(struct sparsebit *sbitp, sparsebit_idx_t start, sparsebit_num_t num);void sparsebit_clear_all (struct sparsebit *sbitp); -void sparsebit_dump (FILE *stream,struct sparsebit *sbit, +void sparsebit_dump (FILE *stream,const struct sparsebit *sbit,unsigned int indent); -void sparsebit_validate_internal (struct sparsebit *sbit); +void sparsebit_validate_internal (const struct sparsebit *sbit); #ifdef __cplusplus }
tools/testing/selftests/kvm/lib/sparsebit.c +24 -24
190 lines not on this page · raw message
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
Sean Christopherson
reviewerRe: [PATCH V6 1/7] KVM: selftests: sparsebit: add const where appropriate
I suggest this for the shortlog
KVM: selftests: Make sparsebit structs const where appropriate
Three deep scopes are a net negative IMO. Too much of the shortlog is consumed by the scope, and very few patches are actually capable of such precision, i.e. touch more than a single area.
Quoted 3 lines · Peter Gonda
From: Michael Roth <[email protected]>
Subsequent patches will introduce an encryption bitmap in
kvm_utilthat
protected memory, not encryption
Quoted 4 lines
would be useful to allow tests to access in read-only fashion. This will be done via a const sparsebit*. To avoid warnings or the need to add casts everywhere, add const to the various sparsebit functions that are applicable for read-only usage of sparsebit.
PATCH v6 2/7 KVM: selftests: add hooks for managing protected guest memory
In reply to Peter GondaAdd kvm_vm.protected metadata. Protected VMs memory, potentially register and other state may not be accessible to KVM. This combined with a new protected_phy_pages bitmap will allow the selftests to check if a given pages is accessible.
--- .../selftests/kvm/include/kvm_util_base.h | 14 ++++++++++++-- tools/testing/selftests/kvm/lib/kvm_util.c | 16 +++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +12 -2
@@ -45,6 +45,7 @@ typedef uint64_t vm_vaddr_t; /* Virtual Machine (Guest) virtual address */struct userspace_mem_region {struct kvm_userspace_memory_region region;struct sparsebit *unused_phy_pages; +struct sparsebit *protected_phy_pages;int fd;off_t offset;enum vm_mem_backing_src_type backing_src_type;
@@ -111,6 +112,9 @@ struct kvm_vm {vm_vaddr_t handlers;uint32_t dirty_ring_size; +/* VM protection enabled: SEV, etc*/ +bool protected; +/* Cache of information for binary stats interface */ int stats_fd;struct kvm_stats_header stats_header;
@@ -679,10 +683,16 @@ const char *exit_reason_str(unsigned int exit_reason);vm_paddr_t vm_phy_page_alloc (struct kvm_vm *vm,vm_paddr_t paddr_min,uint32_t memslot); -vm_paddr_t vm_phy_pages_alloc (struct kvm_vm *vm,size_t num, -vm_paddr_t paddr_min,uint32_t memslot); +vm_paddr_t _vm_phy_pages_alloc (struct kvm_vm *vm,size_t num, +vm_paddr_t paddr_min,uint32_t memslot,bool protected);vm_paddr_t vm_alloc_page_table (struct kvm_vm *vm); +static inline vm_paddr_t vm_phy_pages_alloc (struct kvm_vm *vm,size_t num, +vm_paddr_t paddr_min,uint32_t memslot) +{ +return _vm_phy_pages_alloc (vm, num, paddr_min, memslot, vm->protected); +} +/* * ____vm_create() does KVM_CREATE_VM and little else. __vm_create() also * loads the test binary into guest memory and creates an IRQ chip (x86 only).
tools/testing/selftests/kvm/lib/kvm_util.c +13 -3
@@ -663,6 +663,7 @@ static void __vm_mem_region_delete(struct kvm_vm *vm,vm_ioctl (vm,KVM_SET_USER_MEMORY_REGION , ®ion->region);sparsebit_free (®ion->unused_phy_pages); +sparsebit_free (®ion->protected_phy_pages); ret =munmap (region->mmap_start, region->mmap_size);TEST_ASSERT (!ret,__KVM_SYSCALL_ERROR ("munmap()" , ret));if (region->fd >=0 ) {
@@ -1010,6 +1011,7 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, region->backing_src_type = src_type; region->unused_phy_pages =sparsebit_alloc (); + region->protected_phy_pages =sparsebit_alloc ();sparsebit_set_num (region->unused_phy_pages, guest_paddr >> vm->page_shift, npages); region->region.slot = slot;
@@ -1799,6 +1801,10 @@ void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent) region->host_mem);fprintf (stream,"%*sunused_phy_pages: " , indent +2 ,"" );sparsebit_dump (stream, region->unused_phy_pages,0 ); +if (vm->protected) { +fprintf (stream,"%*sprotected_phy_pages: " , indent +2 ,"" ); +sparsebit_dump (stream, region->protected_phy_pages,0 ); + } }fprintf (stream,"%*sMapped Virtual Pages:\n" , indent,"" );sparsebit_dump (stream, vm->vpages_mapped, indent +2 );
@@ -1895,8 +1901,9 @@ const char *exit_reason_str(unsigned int exit_reason)* and their base address is returned. A TEST_ASSERT failure occurs if * not enough pages are available at or above paddr_min. */ -vm_paddr_t vm_phy_pages_alloc (struct kvm_vm *vm,size_t num, -vm_paddr_t paddr_min,uint32_t memslot) +vm_paddr_t _vm_phy_pages_alloc (struct kvm_vm *vm,size_t num, +vm_paddr_t paddr_min,uint32_t memslot, +bool protected) {struct userspace_mem_region *region;sparsebit_idx_t pg, base;
@@ -1929,8 +1936,11 @@ vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num,abort (); } -for (pg = base; pg < base + num; ++pg) +for (pg = base; pg < base + num; ++pg) {sparsebit_clear (region->unused_phy_pages, pg); +if (protected) +sparsebit_set (region->protected_phy_pages, pg); + }return base * vm->page_size; }
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
Sean Christopherson
reviewerRe: [PATCH V6 2/7] KVM: selftests: add hooks for managing protected guest memory
Quoted 40 lines · Peter Gonda
Add kvm_vm.protected metadata. Protected VMs memory, potentially register and other state may not be accessible to KVM. This combined with a new
protected_phy_pagesbitmap will allow the selftests to check if a given pages is accessible.Cc Paolo Bonzini <[email protected]>Cc Sean Christopherson <[email protected]>Cc Vishal Annapurve <[email protected]>Cc Ackerley Tng <[email protected]>cc Andrew Jones <[email protected]>Originally-by Michael Roth <[email protected]>Signed-off-by Peter Gonda <[email protected]>--- .../selftests/kvm/include/kvm_util_base.h | 14 ++++++++++++-- tools/testing/selftests/kvm/lib/kvm_util.c | 16 +++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-)tools/testing/selftests/kvm/include/kvm_util_base.h +4
-0@@ -45,6 +45,7 @@ typedef uint64_t vm_vaddr_t; /* Virtual Machine (Guest) virtual address */struct userspace_mem_region {struct kvm_userspace_memory_region region;struct sparsebit *unused_phy_pages; +struct sparsebit *protected_phy_pages;int fd;off_t offset;enum vm_mem_backing_src_type backing_src_type;@@ -111,6 +112,9 @@ struct kvm_vm {vm_vaddr_t handlers;uint32_t dirty_ring_size; +/* VM protection enabled: SEV, etc*/ +bool protected; +/* Cache of information for binary stats interface */ int stats_fd;struct kvm_stats_header stats_header;
tools/testing/selftests/kvm/include/kvm_util_base.h const char *exit_reason_str(unsigned int exit_reason); · line 686
Quoted 6 lines
tools/testing/selftests/kvm/include/kvm_util_base.h +1
-2@@ -679,10 +683,16 @@ const char *exit_reason_str(unsigned int exit_reason);vm_paddr_t vm_phy_page_alloc (struct kvm_vm *vm,vm_paddr_t paddr_min,uint32_t memslot); -vm_paddr_t vm_phy_pages_alloc (struct kvm_vm *vm,size_t num, -vm_paddr_t paddr_min,uint32_t memslot); +vm_paddr_t _vm_phy_pages_alloc(struct kvm_vm *vm, size_t num,
Two underscores please. Ignore the terrible precedent that has been set, we're slowly purging that crud.
tools/testing/selftests/kvm/include/kvm_util_base.h const char *exit_reason_str(unsigned int exit_reason); · line 687
Quoted 1 line
tools/testing/selftests/kvm/include/kvm_util_base.h +1
-0+ vm_paddr_t paddr_min, uint32_t memslot, bool protected);
Wrap, no strong justification for running long in this case since the declaration has already wrapped, and the definition does wrap.
PATCH v6 3/7 KVM: selftests: handle protected bits in page tables
In reply to Peter GondaSEV guests rely on an encyption bit which resides within the range that current code treats as address bits. Guest code will expect these bits to be set appropriately in their page tables, whereas the rest of the kvm_util functions will generally expect these bits to not be present. Introduce pte_me_mask and struct kvm_vm_arch to allow for arch specific address tagging. Currently just adding x86 c_bit and s_bit support for SEV and TDX.
--- tools/arch/arm64/include/asm/kvm_host.h | 7 +++++++ tools/arch/riscv/include/asm/kvm_host.h | 7 +++++++ tools/arch/s390/include/asm/kvm_host.h | 7 +++++++ tools/arch/x86/include/asm/kvm_host.h | 13 ++++++++++++ .../selftests/kvm/include/kvm_util_base.h | 19 +++++++++++++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 21 ++++++++++++++++++- .../selftests/kvm/lib/x86_64/processor.c | 17 ++++++++++++--- 7 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 tools/arch/arm64/include/asm/kvm_host.h create mode 100644 tools/arch/riscv/include/asm/kvm_host.h create mode 100644 tools/arch/s390/include/asm/kvm_host.h create mode 100644 tools/arch/x86/include/asm/kvm_host.h
tools/arch/arm64/include/asm/kvm_host.h new file +7 -0
@@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _TOOLS_LINUX_ASM_ARM64_KVM_HOST_H +#define _TOOLS_LINUX_ASM_ARM64_KVM_HOST_H + +struct kvm_vm_arch {}; + +#endif // _TOOLS_LINUX_ASM_ARM64_KVM_HOST_H
tools/arch/riscv/include/asm/kvm_host.h new file +7 -0
@@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _TOOLS_LINUX_ASM_RISCV_KVM_HOST_H +#define _TOOLS_LINUX_ASM_RISCV_KVM_HOST_H + +struct kvm_vm_arch {}; + +#endif // _TOOLS_LINUX_ASM_RISCV_KVM_HOST_H
tools/arch/s390/include/asm/kvm_host.h new file +7 -0
@@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _TOOLS_LINUX_ASM_S390_KVM_HOST_H +#define _TOOLS_LINUX_ASM_S390_KVM_HOST_H + +struct kvm_vm_arch {}; + +#endif // _TOOLS_LINUX_ASM_S390_KVM_HOST_H
tools/arch/x86/include/asm/kvm_host.h new file +13 -0
@@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _TOOLS_LINUX_ASM_X86_KVM_HOST_H +#define _TOOLS_LINUX_ASM_X86_KVM_HOST_H + +#include <stdbool.h> +#include <stdint.h> + +struct kvm_vm_arch { +uint64_t c_bit; +uint64_t s_bit; +}; + +#endif // _TOOLS_LINUX_ASM_X86_KVM_HOST_H
tools/testing/selftests/kvm/include/kvm_util_base.h +19 -0
@@ -17,6 +17,8 @@#include "linux/rbtree.h" #include <asm/atomic.h> +#include <asm/kvm.h> +#include <asm/kvm_host.h> #include <sys/ioctl.h>
@@ -111,6 +113,9 @@ struct kvm_vm {vm_vaddr_t idt;vm_vaddr_t handlers;uint32_t dirty_ring_size; +uint64_t gpa_protected_mask; + +struct kvm_vm_arch arch;/* VM protection enabled: SEV, etc*/ bool protected;
@@ -162,6 +167,7 @@ enum vm_guest_mode {VM_MODE_P40V48_16K ,VM_MODE_P40V48_64K ,VM_MODE_PXXV48_4K ,/* For 48bits VA but ANY bits PA */ +VM_MODE_PXXV48_4K_SEV ,/* For 48bits VA but ANY bits PA */ VM_MODE_P47V64_4K ,VM_MODE_P44V64_4K , VM_MODE_P36V48_4K,
@@ -441,6 +447,17 @@ void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva);vm_paddr_t addr_hva2gpa (struct kvm_vm *vm,void *hva);void *addr_gpa2alias (struct kvm_vm *vm,vm_paddr_t gpa); + +static inline vm_paddr_t vm_untag_gpa (struct kvm_vm *vm,vm_paddr_t gpa) +{ +return gpa & ~vm->gpa_protected_mask; +} + +static inline vm_paddr_t vm_tag_gpa (struct kvm_vm *vm,vm_paddr_t gpa) +{ +return gpa | vm->gpa_protected_mask; +} +void vcpu_run (struct kvm_vcpu *vcpu);int _vcpu_run (struct kvm_vcpu *vcpu);
@@ -917,4 +934,6 @@ void kvm_selftest_arch_init(void);void kvm_arch_vm_post_create (struct kvm_vm *vm); +bool vm_is_gpa_protected (struct kvm_vm *vm,vm_paddr_t paddr); +#endif /* SELFTEST_KVM_UTIL_BASE_H */
tools/testing/selftests/kvm/lib/kvm_util.c +20 -1
@@ -1451,9 +1451,10 @@ void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,* address providing the memory to the vm physical address is returned. * A TEST_ASSERT failure occurs if no region containing gpa exists. */ -void *addr_gpa2hva (struct kvm_vm *vm,vm_paddr_t gpa) +void *addr_gpa2hva (struct kvm_vm *vm,vm_paddr_t gpa_tagged) {struct userspace_mem_region *region; +vm_paddr_t gpa =vm_untag_gpa (vm, gpa_tagged); region =userspace_mem_region_find (vm, gpa, gpa);if (!region) {
@@ -2147,3 +2148,21 @@ void __attribute((constructor)) kvm_selftest_init(void)kvm_selftest_arch_init (); } + +bool vm_is_gpa_protected (struct kvm_vm *vm,vm_paddr_t paddr) +{ +sparsebit_idx_t pg =0 ; +struct userspace_mem_region *region; + +if (!vm->protected) +return false; + + region =userspace_mem_region_find (vm, paddr, paddr); +if (!region) { +TEST_FAIL ("No vm physical memory at 0x%lx" , paddr); +return false; + } + + pg = paddr >> vm->page_shift; +return sparsebit_is_set (region->protected_phy_pages, pg); +}
tools/testing/selftests/kvm/lib/x86_64/processor.c +14 -3
@@ -127,6 +127,7 @@ void virt_arch_pgd_alloc(struct kvm_vm *vm)/* If needed, create page map l4 table. */ if (!vm->pgd_created) { vm->pgd =vm_alloc_page_table (vm); + vm->pgd_created = true; } }
@@ -153,13 +154,16 @@ static uint64_t *virt_create_upper_pte(struct kvm_vm *vm, int target_level) {uint64_t *pte =virt_get_pte (vm, parent_pte, vaddr, current_level); +uint64_t paddr_raw =vm_untag_gpa (vm, paddr);if (!(*pte &PTE_PRESENT_MASK )) { *pte =PTE_PRESENT_MASK |PTE_WRITABLE_MASK ;if (current_level == target_level) - *pte |=PTE_LARGE_MASK | (paddr &PHYSICAL_PAGE_MASK ); -else + *pte |=PTE_LARGE_MASK | (paddr_raw &PHYSICAL_PAGE_MASK ); +else { *pte |=vm_alloc_page_table (vm) &PHYSICAL_PAGE_MASK ; + } + }else {/* * Entry already present. Assert that the caller doesn't want
@@ -197,6 +201,8 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level)"Physical address beyond maximum supported,\n" " paddr: 0x%lx vm->max_gfn: 0x%lx vm->page_size: 0x%x" , paddr, vm->max_gfn, vm->page_size); +TEST_ASSERT (vm_untag_gpa (vm, paddr) == paddr, +"Unexpected bits in paddr: %lx" , paddr);/* * Allocate upper level page tables, if not already present. Return
@@ -219,6 +225,11 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level)TEST_ASSERT (!(*pte &PTE_PRESENT_MASK ),"PTE already present for 4k page at vaddr: 0x%lx\n" , vaddr); *pte =PTE_PRESENT_MASK |PTE_WRITABLE_MASK | (paddr &PHYSICAL_PAGE_MASK ); + +if (vm_is_gpa_protected (vm, paddr)) + *pte |= vm->arch.c_bit; +else + *pte |= vm->arch.s_bit; }void virt_arch_pg_map (struct kvm_vm *vm,uint64_t vaddr,uint64_t paddr)
@@ -493,7 +504,7 @@ vm_paddr_t addr_arch_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva)* No need for a hugepage mask on the PTE, x86-64 requires the "unused" * address bits to be zero. */ -return PTE_GET_PA (*pte) | (gva & ~HUGEPAGE_MASK (level)); +return vm_untag_gpa (vm,PTE_GET_PA (*pte)) | (gva & ~HUGEPAGE_MASK (level)); }static void kvm_setup_gdt (struct kvm_vm *vm,struct kvm_dtable *dt)
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
Sean Christopherson
reviewerRe: [PATCH V6 3/7] KVM: selftests: handle protected bits in page tables
Quoted 6 lines · Peter Gonda
SEV guests rely on an encyption bit which resides within the range that current code treats as address bits. Guest code will expect these bits to be set appropriately in their page tables, whereas the rest of the
kvm_utilfunctions will generally expect these bits to not be present. Introducepte_me_maskand structkvm_vm_archto allow for arch specific address tagging. Currently just adding
State what the patch does, not what you are doing.
Quoted 1 line
x86
c_bitands_bitsupport for SEV and TDX.
Move the c_bit and s_bit stuff to a different patch. The introduction of kvm_util_arch.h and kvm_vm_arch should also go in a separate patch.
Quoted 7 lines
Cc Paolo Bonzini <[email protected]>Cc Sean Christopherson <[email protected]>Cc Vishal Annapurve <[email protected]>Cc Ackerley Tng <[email protected]>cc Andrew Jones <[email protected]>Originally-by Michael Roth <[email protected]>Signed-off-by Peter Gonda <[email protected]>
Quoted 5 lines
--- tools/arch/arm64/include/asm/kvm_host.h | 7 +++++++ tools/arch/riscv/include/asm/kvm_host.h | 7 +++++++ tools/arch/s390/include/asm/kvm_host.h | 7 +++++++ tools/arch/x86/include/asm/kvm_host.h | 13 ++++++++++++
Ugh, we need to think of a name other than kvm_host.h, that's going to be far too confusing with KVM proper's versions. I want to use kvm_arch.h in KVM proper too, so that's off the table. Maybe kvm_util_arch.h?
tools/testing/selftests/kvm/include/kvm_util_base.h enum vm_guest_mode { · line 170
Quoted 5 lines
tools/testing/selftests/kvm/include/kvm_util_base.h +1
-0@@ -162,6 +167,7 @@ enum vm_guest_mode {VM_MODE_P40V48_16K ,VM_MODE_P40V48_64K ,VM_MODE_PXXV48_4K ,/* For 48bits VA but ANY bits PA */ + VM_MODE_PXXV48_4K_SEV, /* For 48bits VA but ANY bits PA */
This belongs in the SEV library patch.
tools/testing/selftests/kvm/include/kvm_util_base.h void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva); · line 450
Quoted 8 lines
tools/testing/selftests/kvm/include/kvm_util_base.h +1
-0VM_MODE_P47V64_4K ,VM_MODE_P44V64_4K , VM_MODE_P36V48_4K,@@ -441,6 +447,17 @@ void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva);vm_paddr_t addr_hva2gpa (struct kvm_vm *vm,void *hva);void *addr_gpa2alias (struct kvm_vm *vm,vm_paddr_t gpa); +
Spurious newline.
tools/testing/selftests/kvm/include/kvm_util_base.h void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva); · line 456
Quoted 6 lines
tools/testing/selftests/kvm/include/kvm_util_base.h +6
-0+static inline vm_paddr_t vm_untag_gpa (struct kvm_vm *vm,vm_paddr_t gpa) +{ +return gpa & ~vm->gpa_protected_mask; +} + +static inline vm_paddr_t vm_tag_gpa (struct kvm_vm *vm,vm_paddr_t gpa)
After seeing how this is all used, this should not exist, i.e. common code should not (and cannot) tag GPAs, because common code doesn't know the polarity (the whole C vs. S madness).
So delete this, and then rename gpa_protected_mask to something like gpa_tag_mask, i.e. make it _really_ generic.
Quoted 19 lines
tools/testing/selftests/kvm/include/kvm_util_base.h +6
-0+{ +return gpa | vm->gpa_protected_mask; +} +void vcpu_run (struct kvm_vcpu *vcpu);int _vcpu_run (struct kvm_vcpu *vcpu);@@ -917,4 +934,6 @@ void kvm_selftest_arch_init(void);void kvm_arch_vm_post_create (struct kvm_vm *vm); +bool vm_is_gpa_protected (struct kvm_vm *vm,vm_paddr_t paddr); +#endif /* SELFTEST_KVM_UTIL_BASE_H */ tools/testing/selftests/kvm/lib/kvm_util.c +0
-0
tools/testing/selftests/kvm/lib/kvm_util.c void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, · line 1457
Quoted 8 lines
tools/testing/selftests/kvm/lib/kvm_util.c +2
-1@@ -1451,9 +1451,10 @@ void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,* address providing the memory to the vm physical address is returned. * A TEST_ASSERT failure occurs if no region containing gpa exists. */ -void *addr_gpa2hva (struct kvm_vm *vm,vm_paddr_t gpa) +void *addr_gpa2hva (struct kvm_vm *vm,vm_paddr_t gpa_tagged) {struct userspace_mem_region *region; +vm_paddr_t gpa =vm_untag_gpa (vm, gpa_tagged);
Now need to rename the incoming param, just do
gpa = vm_untag_gpa(gpa);
tools/testing/selftests/kvm/lib/kvm_util.c void __attribute((constructor)) kvm_selftest_init(void) · line 2152
Quoted 9 lines
tools/testing/selftests/kvm/lib/kvm_util.c +2
-0region =userspace_mem_region_find (vm, gpa, gpa);if (!region) {@@ -2147,3 +2148,21 @@ void __attribute((constructor)) kvm_selftest_init(void)kvm_selftest_arch_init (); } + +bool vm_is_gpa_protected (struct kvm_vm *vm,vm_paddr_t paddr)
This is super confusing, I was 100% expecting this to be a wrapper of gpa_protected_mask. I think that largely goes away when the field is named gpa_tag_mask, but I would still prefer to name this
vm_is_memory_protected()
so that it's clear that the helper cares about the physical memory itself, not the GPA that's used to reference the memory. They're related, but two different thing.
Quoted 21 lines
tools/testing/selftests/kvm/lib/kvm_util.c +16
-0+{ +sparsebit_idx_t pg =0 ; +struct userspace_mem_region *region; + +if (!vm->protected) +return false; + + region =userspace_mem_region_find (vm, paddr, paddr); +if (!region) { +TEST_FAIL ("No vm physical memory at 0x%lx" , paddr); +return false; + } + + pg = paddr >> vm->page_shift; +return sparsebit_is_set (region->protected_phy_pages, pg); +}tools/testing/selftests/kvm/lib/x86_64/processor.c +0
-0
tools/testing/selftests/kvm/lib/x86_64/processor.c void virt_arch_pgd_alloc(struct kvm_vm *vm) · line 130
Quoted 4 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c +1
-0@@ -127,6 +127,7 @@ void virt_arch_pgd_alloc(struct kvm_vm *vm)/* If needed, create page map l4 table. */ if (!vm->pgd_created) { vm->pgd =vm_alloc_page_table (vm); +
Spurious change.
tools/testing/selftests/kvm/lib/x86_64/processor.c static uint64_t *virt_create_upper_pte(struct kvm_vm *vm, · line 157
Quoted 8 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c +1
-0vm->pgd_created = true; } }@@ -153,13 +154,16 @@ static uint64_t *virt_create_upper_pte(struct kvm_vm *vm, int target_level) {uint64_t *pte =virt_get_pte (vm, parent_pte, vaddr, current_level); +uint64_t paddr_raw =vm_untag_gpa (vm, paddr);
No need for another variable
paddr = vm_untag_gpa(vm, paddr);
tools/testing/selftests/kvm/lib/x86_64/processor.c static uint64_t *virt_create_upper_pte(struct kvm_vm *vm, · line 163
Quoted 8 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c +2
-2if (!(*pte &PTE_PRESENT_MASK )) { *pte =PTE_PRESENT_MASK |PTE_WRITABLE_MASK ;if (current_level == target_level) - *pte |=PTE_LARGE_MASK | (paddr &PHYSICAL_PAGE_MASK ); -else + *pte |=PTE_LARGE_MASK | (paddr_raw &PHYSICAL_PAGE_MASK ); +else {
Spurious braces
Quoted 16 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c +4
-0*pte |=vm_alloc_page_table (vm) &PHYSICAL_PAGE_MASK ; + } + }else {/* * Entry already present. Assert that the caller doesn't want @@ -197,6 +201,8 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level)"Physical address beyond maximum supported,\n" " paddr: 0x%lx vm->max_gfn: 0x%lx vm->page_size: 0x%x" , paddr, vm->max_gfn, vm->page_size); +TEST_ASSERT (vm_untag_gpa (vm, paddr) == paddr, +"Unexpected bits in paddr: %lx" , paddr);/* * Allocate upper level page tables, if not already present. Return
tools/testing/selftests/kvm/lib/x86_64/processor.c void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level) · line 228
Quoted 4 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c +1
-0@@ -219,6 +225,11 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level)TEST_ASSERT (!(*pte &PTE_PRESENT_MASK ),"PTE already present for 4k page at vaddr: 0x%lx\n" , vaddr); *pte =PTE_PRESENT_MASK |PTE_WRITABLE_MASK | (paddr &PHYSICAL_PAGE_MASK ); +
Add a comment here, the vast majority of folks reading this don't know the subtleties of TDX and SEV
Quoted 19 lines
tools/testing/selftests/kvm/lib/x86_64/processor.c +5
-1+if (vm_is_gpa_protected (vm, paddr)) + *pte |= vm->arch.c_bit; +else + *pte |= vm->arch.s_bit; }void virt_arch_pg_map (struct kvm_vm *vm,uint64_t vaddr,uint64_t paddr)@@ -493,7 +504,7 @@ vm_paddr_t addr_arch_gva2gpa(struct kvm_vm *vm, vm_vaddr_t gva)* No need for a hugepage mask on the PTE, x86-64 requires the "unused" * address bits to be zero. */ -return PTE_GET_PA (*pte) | (gva & ~HUGEPAGE_MASK (level)); +return vm_untag_gpa (vm,PTE_GET_PA (*pte)) | (gva & ~HUGEPAGE_MASK (level)); }static void kvm_setup_gdt (struct kvm_vm *vm,struct kvm_dtable *dt)Signature · 1 line
2.39.0.314.g84b9a713c41-goog
PATCH v6 4/7 KVM: selftests: add support for protected vm_vaddr_* allocations
In reply to Peter GondaFrom: Michael Roth <[email protected]>
Test programs may wish to allocate shared vaddrs for things like sharing memory with the guest. Since protected vms will have their memory encrypted by default an interface is needed to explicitly request shared pages.
Implement this by splitting the common code out from vm_vaddr_alloc() and introducing a new vm_vaddr_alloc_shared().
--- .../selftests/kvm/include/kvm_util_base.h | 1 + tools/testing/selftests/kvm/lib/kvm_util.c | 21 +++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +1 -0
@@ -435,6 +435,7 @@ vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_mivm_vaddr_t vm_vaddr_alloc (struct kvm_vm *vm,size_t sz,vm_vaddr_t vaddr_min);vm_vaddr_t __vm_vaddr_alloc (struct kvm_vm *vm,size_t sz,vm_vaddr_t vaddr_min,enum kvm_mem_region_type type); +vm_vaddr_t vm_vaddr_alloc_shared (struct kvm_vm *vm,size_t sz,vm_vaddr_t vaddr_min);vm_vaddr_t vm_vaddr_alloc_pages (struct kvm_vm *vm,int nr_pages);vm_vaddr_t __vm_vaddr_alloc_page (struct kvm_vm *vm,enum kvm_mem_region_type type);
tools/testing/selftests/kvm/lib/kvm_util.c +17 -4
@@ -1305,15 +1305,17 @@ vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz,return pgidx_start * vm->page_size; } -vm_vaddr_t __vm_vaddr_alloc (struct kvm_vm *vm,size_t sz,vm_vaddr_t vaddr_min, -enum kvm_mem_region_type type) +static vm_vaddr_t ____vm_vaddr_alloc (struct kvm_vm *vm,size_t sz, +vm_vaddr_t vaddr_min, +enum kvm_mem_region_type type, +bool encrypt) {uint64_t pages = (sz >> vm->page_shift) + ((sz % vm->page_size) !=0 );virt_pgd_alloc (vm); -vm_paddr_t paddr =vm_phy_pages_alloc (vm, pages, +vm_paddr_t paddr =_vm_phy_pages_alloc (vm, pages,KVM_UTIL_MIN_PFN * vm->page_size, - vm->memslots[type]); + vm->memslots[type], encrypt);/* * Find an unused range of virtual page addresses of at least
@@ -1333,6 +1335,17 @@ vm_vaddr_t __vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min,return vaddr_start; } +vm_vaddr_t __vm_vaddr_alloc (struct kvm_vm *vm,size_t sz,vm_vaddr_t vaddr_min, +enum kvm_mem_region_type type) +{ +return ____vm_vaddr_alloc (vm, sz, vaddr_min, type, vm->protected); +} + +vm_vaddr_t vm_vaddr_alloc_shared (struct kvm_vm *vm,size_t sz,vm_vaddr_t vaddr_min) +{ +return ____vm_vaddr_alloc (vm, sz, vaddr_min,MEM_REGION_TEST_DATA , false); +} +/* * VM Virtual Address Allocate *
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
Sean Christopherson
reviewerRe: [PATCH V6 4/7] KVM: selftests: add support for protected vm_vaddr_* allocations
Quoted 27 lines · Peter Gonda
From: Michael Roth <[email protected]>
Test programs may wish to allocate shared vaddrs for things like sharing memory with the guest. Since protected vms will have their memory encrypted by default an interface is needed to explicitly request shared pages.
Implement this by splitting the common code out from
vm_vaddr_alloc()and introducing a newvm_vaddr_alloc_shared().Cc Paolo Bonzini <[email protected]>Cc Sean Christopherson <[email protected]>Cc Vishal Annapurve <[email protected]>Cc Ackerley Tng <[email protected]>cc Andrew Jones <[email protected]>Signed-off-by Michael Roth <[email protected]>Signed-off-by Peter Gonda <[email protected]>--- .../selftests/kvm/include/kvm_util_base.h | 1 + tools/testing/selftests/kvm/lib/kvm_util.c | 21 +++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-)tools/testing/selftests/kvm/include/kvm_util_base.h +0
-0
tools/testing/selftests/kvm/include/kvm_util_base.h vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_mi · line 438
Quoted 4 lines
tools/testing/selftests/kvm/include/kvm_util_base.h +1
-0@@ -435,6 +435,7 @@ vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_mivm_vaddr_t vm_vaddr_alloc (struct kvm_vm *vm,size_t sz,vm_vaddr_t vaddr_min);vm_vaddr_t __vm_vaddr_alloc (struct kvm_vm *vm,size_t sz,vm_vaddr_t vaddr_min,enum kvm_mem_region_type type); +vm_vaddr_t vm_vaddr_alloc_shared (struct kvm_vm *vm,size_t sz,vm_vaddr_t vaddr_min);
Wrap.
Quoted 8 lines
tools/testing/selftests/kvm/include/kvm_util_base.h +0
-0vm_vaddr_t vm_vaddr_alloc_pages (struct kvm_vm *vm,int nr_pages);vm_vaddr_t __vm_vaddr_alloc_page (struct kvm_vm *vm,enum kvm_mem_region_type type);tools/testing/selftests/kvm/lib/kvm_util.c +0
-0
tools/testing/selftests/kvm/lib/kvm_util.c vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz, · line 1311
Quoted 9 lines
tools/testing/selftests/kvm/lib/kvm_util.c +4
-2@@ -1305,15 +1305,17 @@ vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz,return pgidx_start * vm->page_size; } -vm_vaddr_t __vm_vaddr_alloc (struct kvm_vm *vm,size_t sz,vm_vaddr_t vaddr_min, -enum kvm_mem_region_type type) +static vm_vaddr_t ____vm_vaddr_alloc (struct kvm_vm *vm,size_t sz, +vm_vaddr_t vaddr_min, +enum kvm_mem_region_type type, +bool encrypt)
s/encrypt/protected, or maybe mark_protected so that it's clear that it's a command.
Quoted 15 lines
tools/testing/selftests/kvm/lib/kvm_util.c +2
-2{uint64_t pages = (sz >> vm->page_shift) + ((sz % vm->page_size) !=0 );virt_pgd_alloc (vm); -vm_paddr_t paddr =vm_phy_pages_alloc (vm, pages, +vm_paddr_t paddr =_vm_phy_pages_alloc (vm, pages,KVM_UTIL_MIN_PFN * vm->page_size, - vm->memslots[type]); + vm->memslots[type], encrypt);/* * Find an unused range of virtual page addresses of at least @@ -1333,6 +1335,17 @@ vm_vaddr_t __vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min,return vaddr_start; }
tools/testing/selftests/kvm/lib/kvm_util.c vm_vaddr_t __vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, · line 1346
Quoted 10 lines
tools/testing/selftests/kvm/lib/kvm_util.c +9
-0+vm_vaddr_t __vm_vaddr_alloc (struct kvm_vm *vm,size_t sz,vm_vaddr_t vaddr_min, +enum kvm_mem_region_type type) +{ +return ____vm_vaddr_alloc (vm, sz, vaddr_min, type, vm->protected); +} + +vm_vaddr_t vm_vaddr_alloc_shared (struct kvm_vm *vm,size_t sz,vm_vaddr_t vaddr_min) +{ +return ____vm_vaddr_alloc (vm, sz, vaddr_min,MEM_REGION_TEST_DATA , false);
This shouldn't be MEM_REGION_TEST_DATA, because then the ucall patches changes from MEM_REGION_DATA to MEM_REGION_TEST_DATA, which I suspect will break ARM's page_fault_test. So, we probably need to have vm_vaddr_alloc_shared() be a true mirror of the non-shared version and take @type.
PATCH v6 5/7 KVM: selftests: add library for creating/interacting with SEV guests
In reply to Peter GondaAdd interfaces to allow tests to create SEV guests. The additional requirements for SEV guests PTs and other state is encapsulated by the new vm_sev_create_with_one_vcpu() function. This can future be generalized for more vCPUs but the first set of SEV selftests in this series only uses a single vCPU.
--- tools/arch/x86/include/asm/kvm_host.h | 1 + tools/testing/selftests/kvm/Makefile | 3 +- .../selftests/kvm/include/kvm_util_base.h | 15 +- .../selftests/kvm/include/x86_64/processor.h | 1 + .../selftests/kvm/include/x86_64/sev.h | 27 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 8 +- .../selftests/kvm/lib/x86_64/processor.c | 45 +++- tools/testing/selftests/kvm/lib/x86_64/sev.c | 254 ++++++++++++++++++ 8 files changed, 343 insertions(+), 11 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/x86_64/sev.h create mode 100644 tools/testing/selftests/kvm/lib/x86_64/sev.c
tools/arch/x86/include/asm/kvm_host.h +1 -0
@@ -8,6 +8,7 @@struct kvm_vm_arch {uint64_t c_bit;uint64_t s_bit; +bool is_pt_protected; };#endif // _TOOLS_LINUX_ASM_X86_KVM_HOST_H
tools/testing/selftests/kvm/Makefile +2 -1
@@ -39,6 +39,7 @@ LIBKVM_x86_64 += lib/x86_64/processor.c LIBKVM_x86_64 += lib/x86_64/svm.c LIBKVM_x86_64 += lib/x86_64/ucall.c LIBKVM_x86_64 += lib/x86_64/vmx.c +LIBKVM_x86_64 += lib/x86_64/sev.c LIBKVM_aarch64 += lib/aarch64/gic.c LIBKVM_aarch64 += lib/aarch64/gic_v3.c
@@ -199,7 +200,7 @@ CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \ -fno-stack-protector -fno-PIE -I$(LINUX_TOOL_INCLUDE) \ -I$(LINUX_TOOL_ARCH_INCLUDE) -I$(LINUX_HDR_PATH) -Iinclude \ -I$(<D) -Iinclude/$(ARCH_DIR) -I ../rseq -I.. $(EXTRA_CFLAGS) \ - $(KHDR_INCLUDES) + $(KHDR_INCLUDES) -static -gdwarf-4 no-pie-option := $(call try-run, echo 'int main(void) { return 0; }' | \ $(CC) -Werror $(CFLAGS) -no-pie -x c - -o "$$TMP", -no-pie)
tools/testing/selftests/kvm/include/kvm_util_base.h +13 -2
@@ -89,6 +89,13 @@ enum kvm_mem_region_type { NR_MEM_REGIONS, }; +/* VM protection policy/configuration. */ +struct protected_vm { +bool enabled; +bool has_protected_bit; +int8_t protected_bit; +}; +struct kvm_vm {int mode;unsigned long type;
@@ -711,6 +718,10 @@ static inline vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num,return _vm_phy_pages_alloc (vm, num, paddr_min, memslot, vm->protected); } +uint64_t vm_nr_pages_required (enum vm_guest_mode mode, +uint32_t nr_runnable_vcpus, +uint64_t extra_mem_pages); +/* * ____vm_create() does KVM_CREATE_VM and little else. __vm_create() also * loads the test binary into guest memory and creates an IRQ chip (x86 only).
@@ -767,8 +778,8 @@ unsigned long vm_compute_max_gfn(struct kvm_vm *vm);unsigned int vm_calc_num_guest_pages (enum vm_guest_mode mode,size_t size);unsigned int vm_num_host_pages (enum vm_guest_mode mode,unsigned int num_guest_pages);unsigned int vm_num_guest_pages (enum vm_guest_mode mode,unsigned int num_host_pages); -static inline unsigned int -vm_adjust_num_guest_pages (enum vm_guest_mode mode,unsigned int num_guest_pages) +static inline unsigned int vm_adjust_num_guest_pages (enum vm_guest_mode mode, +unsigned int num_guest_pages) {unsigned int n; n =vm_num_guest_pages (mode,vm_num_host_pages (mode, num_guest_pages));
tools/testing/selftests/kvm/include/x86_64/processor.h +1 -0
@@ -916,6 +916,7 @@ static inline void vcpu_set_msr(struct kvm_vcpu *vcpu, uint64_t msr_index,void kvm_get_cpu_address_width (unsigned int *pa_bits,unsigned int *va_bits); +void kvm_init_vm_address_properties (struct kvm_vm *vm);bool vm_is_unrestricted_guest (struct kvm_vm *vm);struct ex_regs {
tools/testing/selftests/kvm/include/x86_64/sev.h new file +27 -0
@@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Helpers used for SEV guests +* +*/ +#ifndef SELFTEST_KVM_SEV_H +#define SELFTEST_KVM_SEV_H + +#include <stdint.h> +#include <stdbool.h> + +#include "kvm_util.h" + +#define CPUID_MEM_ENC_LEAF 0x8000001f +#define CPUID_EBX_CBIT_MASK 0x3f + +#define SEV_POLICY_NO_DBG (1UL << 0) +#define SEV_POLICY_ES (1UL << 2) + +bool is_kvm_sev_supported (void ); + +void sev_vm_init (struct kvm_vm *vm); + +struct kvm_vm *vm_sev_create_with_one_vcpu (uint32_t policy,void *guest_code, +struct kvm_vcpu **cpu); + +#endif /* SELFTEST_KVM_SEV_H */
tools/testing/selftests/kvm/lib/kvm_util.c +6 -2
@@ -151,6 +151,7 @@ const char *vm_guest_mode_string(uint32_t i) [VM_MODE_P40V48_16K] = "PA-bits:40, VA-bits:48, 16K pages", [VM_MODE_P40V48_64K] = "PA-bits:40, VA-bits:48, 64K pages", [VM_MODE_PXXV48_4K] = "PA-bits:ANY, VA-bits:48, 4K pages", + [VM_MODE_PXXV48_4K_SEV] = "PA-bits:ANY, VA-bits:48, 4K pages", [VM_MODE_P47V64_4K] = "PA-bits:47, VA-bits:64, 4K pages", [VM_MODE_P44V64_4K] = "PA-bits:44, VA-bits:64, 4K pages", [VM_MODE_P36V48_4K] = "PA-bits:36, VA-bits:48, 4K pages",
@@ -176,6 +177,7 @@ const struct vm_guest_mode_params vm_guest_mode_params[] = { [VM_MODE_P40V48_16K] = { 40, 48, 0x4000, 14 }, [VM_MODE_P40V48_64K] = { 40, 48, 0x10000, 16 }, [VM_MODE_PXXV48_4K] = { 0, 0, 0x1000, 12 }, + [VM_MODE_PXXV48_4K_SEV] = { 0, 0, 0x1000, 12 }, [VM_MODE_P47V64_4K] = { 47, 64, 0x1000, 12 }, [VM_MODE_P44V64_4K] = { 44, 64, 0x1000, 12 }, [VM_MODE_P36V48_4K] = { 36, 48, 0x1000, 12 },
@@ -254,9 +256,11 @@ struct kvm_vm *____vm_create(enum vm_guest_mode mode)case VM_MODE_P36V47_16K : vm->pgtable_levels =3 ;break ; +case VM_MODE_PXXV48_4K_SEV :case VM_MODE_PXXV48_4K :#ifdef __x86_64__kvm_get_cpu_address_width (&vm->pa_bits, &vm->va_bits); +kvm_init_vm_address_properties (vm);/* * Ignore KVM support for 5-level paging (vm->va_bits == 57), * it doesn't take effect unless a CR4.LA57 is set, which it
@@ -270,7 +274,7 @@ struct kvm_vm *____vm_create(enum vm_guest_mode mode) vm->pgtable_levels =4 ; vm->va_bits =48 ;#else -TEST_FAIL ("VM_MODE_PXXV48_4K not supported on non-x86 platforms" ); +TEST_FAIL ("VM_MODE_PXXV48_4K* not supported on non-x86 platforms" );#endif break ;case VM_MODE_P47V64_4K :
@@ -303,7 +307,7 @@ struct kvm_vm *____vm_create(enum vm_guest_mode mode)return vm; } -static uint64_t vm_nr_pages_required (enum vm_guest_mode mode, +uint64_t vm_nr_pages_required (enum vm_guest_mode mode,uint32_t nr_runnable_vcpus,uint64_t extra_mem_pages) {
tools/testing/selftests/kvm/lib/x86_64/processor.c +39 -6
92 lines not on this page · raw message
tools/testing/selftests/kvm/lib/x86_64/sev.c new file +254 -0
254 lines not on this page · raw message
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
Sean Christopherson
reviewerRe: [PATCH V6 5/7] KVM: selftests: add library for creating/interacting with SEV guests
Quoted 33 lines · Peter Gonda
Add interfaces to allow tests to create SEV guests. The additional requirements for SEV guests PTs and other state is encapsulated by the new
vm_sev_create_with_one_vcpu()function. This can future be generalized for more vCPUs but the first set of SEV selftests in this series only uses a single vCPU.Cc Paolo Bonzini <[email protected]>Cc Sean Christopherson <[email protected]>Cc Vishal Annapurve <[email protected]>Cc Ackerley Tng <[email protected]>cc Andrew Jones <[email protected]>Originally-by Michael Roth <[email protected]>Co-developed-by Ackerley Tng <[email protected]>Signed-off-by Peter Gonda <[email protected]>--- tools/arch/x86/include/asm/kvm_host.h | 1 + tools/testing/selftests/kvm/Makefile | 3 +- .../selftests/kvm/include/kvm_util_base.h | 15 +- .../selftests/kvm/include/x86_64/processor.h | 1 + .../selftests/kvm/include/x86_64/sev.h | 27 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 8 +- .../selftests/kvm/lib/x86_64/processor.c | 45 +++- tools/testing/selftests/kvm/lib/x86_64/sev.c | 254 ++++++++++++++++++ 8 files changed, 343 insertions(+), 11 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/x86_64/sev.h create mode 100644 tools/testing/selftests/kvm/lib/x86_64/sev.ctools/arch/x86/include/asm/kvm_host.h +0
-0
tools/arch/x86/include/asm/kvm_host.h line 11
Quoted 4 lines
tools/arch/x86/include/asm/kvm_host.h +1
-0@@ -8,6 +8,7 @@struct kvm_vm_arch {uint64_t c_bit;uint64_t s_bit; +bool is_pt_protected;
Leftover crud from a previous version, no? I.e. can't this just use vm->protected?
Quoted 16 lines
tools/arch/x86/include/asm/kvm_host.h +0
-0};#endif // _TOOLS_LINUX_ASM_X86_KVM_HOST_H tools/testing/selftests/kvm/Makefile +1
-0@@ -39,6 +39,7 @@ LIBKVM_x86_64 += lib/x86_64/processor.c LIBKVM_x86_64 += lib/x86_64/svm.c LIBKVM_x86_64 += lib/x86_64/ucall.c LIBKVM_x86_64 += lib/x86_64/vmx.c +LIBKVM_x86_64 += lib/x86_64/sev.c LIBKVM_aarch64 += lib/aarch64/gic.c LIBKVM_aarch64 += lib/aarch64/gic_v3.c
tools/testing/selftests/kvm/Makefile CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \ · line 203
Quoted 5 lines
tools/testing/selftests/kvm/Makefile +1
-1@@ -199,7 +200,7 @@ CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \ -fno-stack-protector -fno-PIE -I$(LINUX_TOOL_INCLUDE) \ -I$(LINUX_TOOL_ARCH_INCLUDE) -I$(LINUX_HDR_PATH) -Iinclude \ -I$(<D) -Iinclude/$(ARCH_DIR) -I ../rseq -I.. $(EXTRA_CFLAGS) \ - $(KHDR_INCLUDES) + $(KHDR_INCLUDES) -static -gdwarf-4
Spurious stuff from your environment, which by the way can just use EXTRA_CFLAGS, e.g. I build selftests with
EXTRA_CFLAGS="$static -Werror -gdwarf-4"
where $static varies based on the host.
Quoted 7 lines
tools/testing/selftests/kvm/Makefile +0
-0no-pie-option := $(call try-run, echo 'int main(void) { return 0; }' | \ $(CC) -Werror $(CFLAGS) -no-pie -x c - -o "$$TMP", -no-pie)tools/testing/selftests/kvm/include/kvm_util_base.h +0
-0
tools/testing/selftests/kvm/include/kvm_util_base.h enum kvm_mem_region_type { · line 97
Quoted 9 lines
tools/testing/selftests/kvm/include/kvm_util_base.h +6
-0@@ -89,6 +89,13 @@ enum kvm_mem_region_type { NR_MEM_REGIONS, }; +/* VM protection policy/configuration. */ +struct protected_vm { +bool enabled; +bool has_protected_bit; +int8_t protected_bit; +};
More leftover crud.
tools/testing/selftests/kvm/include/kvm_util_base.h static inline vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, · line 724
Quoted 12 lines
tools/testing/selftests/kvm/include/kvm_util_base.h +5
-0+struct kvm_vm {int mode;unsigned long type;@@ -711,6 +718,10 @@ static inline vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num,return _vm_phy_pages_alloc (vm, num, paddr_min, memslot, vm->protected); } +uint64_t vm_nr_pages_required (enum vm_guest_mode mode, +uint32_t nr_runnable_vcpus, +uint64_t extra_mem_pages); +
Making vm_nr_pages_required() globally visible is no longer needed (upstream changed).
Quoted 14 lines
tools/testing/selftests/kvm/include/kvm_util_base.h +1
-0+#endif /* SELFTEST_KVM_SEV_H */ tools/testing/selftests/kvm/lib/kvm_util.c +1
-0@@ -151,6 +151,7 @@ const char *vm_guest_mode_string(uint32_t i) [VM_MODE_P40V48_16K] = "PA-bits:40, VA-bits:48, 16K pages", [VM_MODE_P40V48_64K] = "PA-bits:40, VA-bits:48, 64K pages", [VM_MODE_PXXV48_4K] = "PA-bits:ANY, VA-bits:48, 4K pages", + [VM_MODE_PXXV48_4K_SEV] = "PA-bits:ANY, VA-bits:48, 4K pages", [VM_MODE_P47V64_4K] = "PA-bits:47, VA-bits:64, 4K pages", [VM_MODE_P44V64_4K] = "PA-bits:44, VA-bits:64, 4K pages", [VM_MODE_P36V48_4K] = "PA-bits:36, VA-bits:48, 4K pages",
tools/testing/selftests/kvm/lib/kvm_util.c const struct vm_guest_mode_params vm_guest_mode_params[] = { · line 180
Quoted 4 lines
tools/testing/selftests/kvm/lib/kvm_util.c +1
-0@@ -176,6 +177,7 @@ const struct vm_guest_mode_params vm_guest_mode_params[] = { [VM_MODE_P40V48_16K] = { 40, 48, 0x4000, 14 }, [VM_MODE_P40V48_64K] = { 40, 48, 0x10000, 16 }, [VM_MODE_PXXV48_4K] = { 0, 0, 0x1000, 12 }, + [VM_MODE_PXXV48_4K_SEV] = { 0, 0, 0x1000, 12 },
Aha! An excuse to use my "mode overloading" idea[*]. Similar to concerns I had with the UPM restricted memory stuff, adding dedicated enums for modifier is going to be a maintenance problem. So rather than have VM_MODE_PXXV48_4K_SEV, I propose we split @mode into a set of masks and then have a "primary mode" and a "vm type".
Note, Andrew requested using a struct, so this may need a fair bit of massaging.
tools/testing/selftests/kvm/lib/kvm_util.c const struct vm_guest_mode_params vm_guest_mode_params[] = {
Quoted 5 lines
tools/testing/selftests/kvm/lib/kvm_util.c +5
-0+static void assert_supported_guest_mode (struct kvm_vm *vm) +{ +TEST_ASSERT (vm->mode ==VM_MODE_PXXV48_4K || vm->mode ==VM_MODE_PXXV48_4K_SEV , +"Attempt to use unknown or unsupported guest mode, mode: 0x%x" , + vm->mode);
These changes all go away if we use a modifier for the VM type.
Quoted 21 lines
tools/testing/selftests/kvm/lib/kvm_util.c +4
-4+} +void virt_arch_pgd_alloc (struct kvm_vm *vm) { -TEST_ASSERT (vm->mode ==VM_MODE_PXXV48_4K ,"Attempt to use " -"unknown or unsupported guest mode, mode: 0x%x" , vm->mode); +assert_supported_guest_mode (vm);/* If needed, create page map l4 table. */ if (!vm->pgd_created) {@@ -186,8 +193,7 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level)uint64_t *pml4e, *pdpe, *pde;uint64_t *pte; -TEST_ASSERT (vm->mode ==VM_MODE_PXXV48_4K , -"Unknown or unsupported guest mode, mode: 0x%x" , vm->mode); +assert_supported_guest_mode (vm);TEST_ASSERT ((vaddr % pg_size) ==0 , "Virtual address not aligned,\n"
tools/testing/selftests/kvm/lib/kvm_util.c uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr, · line 284
Quoted 6 lines
tools/testing/selftests/kvm/lib/kvm_util.c +3
-0@@ -273,11 +279,14 @@ uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr, {uint64_t *pml4e, *pdpe, *pde; +TEST_ASSERT ( + !vm->arch.is_pt_protected, +"Protected guests have their page tables protected so gva2gpa conversions are not possible." );
Eww, Google3 C++ "readability".
TEST_ASSERT(!vm->protected, "Walking page tables of protected guests is impossible");
tools/testing/selftests/kvm/lib/kvm_util.c static void vcpu_setup(struct kvm_vm *vm, struct kvm_vcpu *vcpu) · line 580
Quoted 6 lines
tools/testing/selftests/kvm/lib/kvm_util.c +2
-0@@ -566,6 +576,10 @@ static void vcpu_setup(struct kvm_vm *vm, struct kvm_vcpu *vcpu)void kvm_arch_vm_post_create (struct kvm_vm *vm) {vm_create_irqchip (vm); + +if (vm->mode ==VM_MODE_PXXV48_4K_SEV ) {
Unnecesary braces.
Quoted 6 lines
tools/testing/selftests/kvm/lib/kvm_util.c +2
-0+sev_vm_init (vm); + } } struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
tools/testing/selftests/kvm/lib/kvm_util.c void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits) · line 1072
Quoted 9 lines
tools/testing/selftests/kvm/lib/kvm_util.c +6
-0@@ -1050,6 +1064,25 @@ void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits) } } +static void configure_sev_pte_masks (struct kvm_vm *vm) +{ +uint32_t eax, ebx, ecx, edx, enc_bit; + +cpuid (CPUID_MEM_ENC_LEAF , &eax, &ebx, &ecx, &edx); + enc_bit = ebx &CPUID_EBX_CBIT_MASK ;
Add X86_PROPERTY_SEV_C_BIT (or something along those lines).
tools/testing/selftests/kvm/lib/kvm_util.c void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits) · line 1081
Quoted 9 lines
tools/testing/selftests/kvm/lib/kvm_util.c +9
-0+ + vm->arch.c_bit =1ULL << enc_bit; + vm->protected = true; + vm->gpa_protected_mask = vm->arch.c_bit; +} + +void kvm_init_vm_address_properties (struct kvm_vm *vm) +{ +if (vm->mode ==VM_MODE_PXXV48_4K_SEV ) {
Unnecessary braces as written, but having a helper is just silly.
tools/testing/selftests/kvm/lib/kvm_util.c void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits) · line 1082
Quoted 1 line
tools/testing/selftests/kvm/lib/kvm_util.c +1
-0+configure_sev_pte_masks (vm);
if (vm->type == VM_TYPE_SEV) {
vm->protected = true;
vm->arch.c_bit = this_cpu_property(X86_PROPERTY_SEV_C_BIT);
vm->gpa_tag_mask = vm->arch.c_bit;
}Quoted 2 lines
tools/testing/selftests/kvm/lib/kvm_util.c +2
-0+ } +}
...
tools/testing/selftests/kvm/lib/kvm_util.c
Quoted 10 lines
tools/testing/selftests/kvm/lib/kvm_util.c +10
-0+static void sev_ioctl (int cmd,void *data) +{ +int ret; +struct sev_issue_cmd arg; + + arg.cmd = cmd; + arg.data = (unsigned long )data; + ret =ioctl (open_sev_dev_path_or_exit (),SEV_ISSUE_CMD , &arg); +TEST_ASSERT (ret ==0 ,"SEV ioctl %d failed, error: %d, fw_error: %d" , + cmd, ret, arg.error);
This leaks the fd, and it should use kvm_ioctl(). Yean, it's not technically a KVM ioctl(), but there's no need to reinvent the wheel.
tools/testing/selftests/kvm/lib/kvm_util.c
Quoted 10 lines
tools/testing/selftests/kvm/lib/kvm_util.c +10
-0+} + +static void kvm_sev_ioctl (struct kvm_vm *vm,int cmd,void *data) +{ +struct kvm_sev_cmd arg = {0 }; +int ret; + + arg.id = cmd; + arg.sev_fd =open_sev_dev_path_or_exit (); + arg.data = (__u64 )data;
Use a struct initializer:
struct kvm_sev_cmd sev_cmd = {
.id = cmd,
.sev_fd = vm->sev_fd,
.data = (unsigned long)data,
};tools/testing/selftests/kvm/lib/kvm_util.c
Quoted 6 lines
tools/testing/selftests/kvm/lib/kvm_util.c +6
-0+ + ret =ioctl (vm->fd,KVM_MEMORY_ENCRYPT_OP , &arg); +TEST_ASSERT ( + ret ==0 , +"SEV KVM ioctl %d failed, rc: %i errno: %i (%s), fw_error: %d" , + cmd, ret, errno,strerror (errno), arg.error);
Google3 horrors aside, just use vm_ioctl().
vm_ioctl(vm, KVM_MEMORY_ENCRYPT_OP, &sev_cmd);
Quoted 4 lines
tools/testing/selftests/kvm/lib/kvm_util.c +4
-0+} + +static void sev_register_user_region (struct kvm_vm *vm,struct userspace_mem_region *region) +{
tools/testing/selftests/kvm/lib/kvm_util.c
Quoted 10 lines
tools/testing/selftests/kvm/lib/kvm_util.c +10
-0+struct kvm_enc_region range = {0 }; +int ret; + + range.addr = (__u64 )region->region.userspace_addr; + ; + range.size = region->region.memory_size; + + ret =ioctl (vm->fd,KVM_MEMORY_ENCRYPT_REG_REGION , &range); +TEST_ASSERT (ret ==0 ,"failed to register user range, errno: %i\n" , + errno);
See above.
tools/testing/selftests/kvm/lib/kvm_util.c
Quoted 5 lines
tools/testing/selftests/kvm/lib/kvm_util.c +5
-0+} + +static void sev_launch_update_data (struct kvm_vm *vm,vm_paddr_t gpa,uint64_t size) +{ +struct kvm_sev_launch_update_data ksev_update_data = {0 };
These are _local_ variables, there's no need to namespace them, i.e. delete kvev_.
tools/testing/selftests/kvm/lib/kvm_util.c
Quoted 5 lines
tools/testing/selftests/kvm/lib/kvm_util.c +5
-0+ +pr_debug ("%s: addr: 0x%lx, size: %lu\n" , __func__, gpa, size); + + ksev_update_data.uaddr = (__u64 )addr_gpa2hva (vm, gpa); + ksev_update_data.len = size;
Struct initializer.
Quoted 16 lines
tools/testing/selftests/kvm/lib/kvm_util.c +16
-0+ +kvm_sev_ioctl (vm,KVM_SEV_LAUNCH_UPDATE_DATA , &ksev_update_data); +} + + +/* +* Iterate over set ranges within sparsebit @s. In each iteration, +* @range_begin and @range_end will take the beginning and end of the set +* range, which are of type sparsebit_idx_t. +* +* For example, if the range [3, 7] (inclusive) is set, within the +* iteration,@range_begin will take the value 3 and @range_end will take +* the value 7. +* +* Ensure that there is at least one bit set before using this macro with +* sparsebit_any_set(), because sparsebit_first_set() will abort if none
tools/testing/selftests/kvm/lib/kvm_util.c
Quoted 10 lines
tools/testing/selftests/kvm/lib/kvm_util.c +10
-0+* are set. +*/ +#define sparsebit_for_each_set_range (s, range_begin, range_end) \ + for (range_begin = sparsebit_first_set(s), \ + range_end = \ + sparsebit_next_clear(s, range_begin) - 1; \ + range_begin && range_end; \ + range_begin = sparsebit_next_set(s, range_end), \ + range_end = \ + sparsebit_next_clear(s, range_begin) - 1)
This belongs in tools/testing/selftests/kvm/include/sparsebit.h.
Quoted 5 lines
tools/testing/selftests/kvm/lib/kvm_util.c +5
-0+ +/* +* 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 +* correct.
tools/testing/selftests/kvm/lib/kvm_util.c
Quoted 10 lines
tools/testing/selftests/kvm/lib/kvm_util.c +10
-0+* +* If the last range in the sparsebit is [x, y] and we try to iterate, +* sparsebit_next_set() will return 0, and sparsebit_next_clear() will try +* and find the first range, but that's correct because the condition +* expression would cause us to quit the loop. +*/ +static void encrypt_region (struct kvm_vm *vm,struct userspace_mem_region *region) +{ +const struct sparsebit *protected_phy_pages = + region->protected_phy_pages;
Don't wrap, 3 chars in the case is fine. You didn't wrap two lines below, which runs over by 2 chars, so spontaneous combustion is unlikely.
Quoted 11 lines
tools/testing/selftests/kvm/lib/kvm_util.c +11
-0+const vm_paddr_t gpa_base = region->region.guest_phys_addr; +const sparsebit_idx_t lowest_page_in_region = gpa_base >> vm->page_shift; + +sparsebit_idx_t i; +sparsebit_idx_t j; + +if (!sparsebit_any_set (protected_phy_pages)) +return ; + +sev_register_user_region (vm, region); +
tools/testing/selftests/kvm/lib/kvm_util.c
Quoted 10 lines
tools/testing/selftests/kvm/lib/kvm_util.c +10
-0+sparsebit_for_each_set_range (protected_phy_pages, i, j) { +const uint64_t size_to_load = (j - i +1 ) * vm->page_size; +const uint64_t offset = (i - lowest_page_in_region) * vm->page_size; +const uint64_t gpa = gpa_base + offset; + +sev_launch_update_data (vm, gpa, size_to_load); + } +} + +static void sev_encrypt (struct kvm_vm *vm)
Open code this in its one caller.
tools/testing/selftests/kvm/lib/kvm_util.c
Quoted 11 lines
tools/testing/selftests/kvm/lib/kvm_util.c +9
-0+{ +int ctr; +struct userspace_mem_region *region; + +hash_for_each (vm->regions.slot_hash, ctr, region, slot_node) { +encrypt_region (vm, region); + } + + vm->arch.is_pt_protected = true;
Ah, that's why is_pt_protected exists. Yeah, no. I despise that SEV doesn't actually protect guest memory until a magic command is issued. Unless there is a really, _really_ good reason not to treat the VM as fully protected from time zero, let's ignore that detail in selftests.
tools/testing/selftests/kvm/lib/kvm_util.c
Quoted 11 lines
tools/testing/selftests/kvm/lib/kvm_util.c +11
-0+} + +bool is_kvm_sev_supported (void ) +{ +struct sev_user_data_status sev_status; + +sev_ioctl (SEV_PLATFORM_STATUS , &sev_status); + +if (!(sev_status.api_major >SEV_FW_REQ_VER_MAJOR || + (sev_status.api_major ==SEV_FW_REQ_VER_MAJOR && + sev_status.api_minor >=SEV_FW_REQ_VER_MINOR ))) {
This needs a comment explaining what on earth the major+minor stuff is, and why we care.
tools/testing/selftests/kvm/lib/kvm_util.c
Quoted 4 lines
tools/testing/selftests/kvm/lib/kvm_util.c +4
-0+pr_info ("SEV FW version too old. Have API %d.%d (build: %d), need %d.%d, skipping test.\n" , + sev_status.api_major, sev_status.api_minor, + sev_status.build,SEV_FW_REQ_VER_MAJOR , +SEV_FW_REQ_VER_MINOR );
The pr_info() seems kinda pointless. If someone is running these and the firmware is too old, I assume they're going to have to do a lot of dirty work to get the firmware updated. And this is much easier on the eyes:
struct sev_user_data_status sev_status; sev_ioctl(SEV_PLATFORM_STATUS, &sev_status); return sev_status.api_major > SEV_FW_REQ_VER_MAJOR || (sev_status.api_major == SEV_FW_REQ_VER_MAJOR && sev_status.api_minor >= SEV_FW_REQ_VER_MINOR);
If the pr_info() really does add value, then do something like:
supported = sev_status.api_major > SEV_FW_REQ_VER_MAJOR || (sev_status.api_major == SEV_FW_REQ_VER_MAJOR && sev_status.api_minor >= SEV_FW_REQ_VER_MINOR); if (!supported) pr_info(); return supported;
tools/testing/selftests/kvm/lib/kvm_util.c
Quoted 4 lines
tools/testing/selftests/kvm/lib/kvm_util.c +4
-0+static void sev_vm_launch (struct kvm_vm *vm,uint32_t policy) +{ +struct kvm_sev_launch_start ksev_launch_start = {0 }; +struct kvm_sev_guest_status ksev_status;
See above.
tools/testing/selftests/kvm/lib/kvm_util.c
Quoted 7 lines
tools/testing/selftests/kvm/lib/kvm_util.c +7
-0+ + ksev_launch_start.policy = policy; +kvm_sev_ioctl (vm,KVM_SEV_LAUNCH_START , &ksev_launch_start); +kvm_sev_ioctl (vm,KVM_SEV_GUEST_STATUS , &ksev_status); +TEST_ASSERT (ksev_status.policy == policy,"Incorrect guest policy." ); +TEST_ASSERT (ksev_status.state ==SEV_GSTATE_LUPDATE , +"Unexpected guest state: %d" , ksev_status.state);
Print the expected vs. actual.
Quoted 4 lines
tools/testing/selftests/kvm/lib/kvm_util.c +4
-0+ +ucall_init (vm,0 ); + +sev_encrypt (vm);
tools/testing/selftests/kvm/lib/kvm_util.c
Quoted 10 lines
tools/testing/selftests/kvm/lib/kvm_util.c +10
-0+} + +static void sev_vm_launch_measure (struct kvm_vm *vm,uint8_t *measurement) +{ +struct kvm_sev_launch_measure ksev_launch_measure; +struct kvm_sev_guest_status ksev_guest_status; + + ksev_launch_measure.len =256 ; + ksev_launch_measure.uaddr = (__u64 )measurement; +kvm_sev_ioctl (vm,KVM_SEV_LAUNCH_MEASURE , &ksev_launch_measure);
See above.
tools/testing/selftests/kvm/lib/kvm_util.c
Quoted 4 lines
tools/testing/selftests/kvm/lib/kvm_util.c +4
-0+ +kvm_sev_ioctl (vm,KVM_SEV_GUEST_STATUS , &ksev_guest_status); +TEST_ASSERT (ksev_guest_status.state ==SEV_GSTATE_LSECRET , +"Unexpected guest state: %d" , ksev_guest_status.state);
Expected vs. actual
PATCH v6 6/7 KVM: selftests: Update ucall pool to allocate from shared memory
In reply to Peter GondaUpdate the per VM ucall_header allocation from vm_vaddr_alloc() to vm_vaddr_alloc_shared(). This allows encrypted guests to use ucall pools by placing their shared ucall structures in unencrypted (shared) memory. No behavior change for non encrypted guests.
--- tools/testing/selftests/kvm/lib/ucall_common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kvm/lib/ucall_common.c +1 -1
@@ -24,7 +24,7 @@ void ucall_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa)vm_vaddr_t vaddr;int i; - vaddr =__vm_vaddr_alloc (vm,sizeof (*hdr),KVM_UTIL_MIN_VADDR ,MEM_REGION_DATA ); + vaddr =vm_vaddr_alloc_shared (vm,sizeof (*hdr),KVM_UTIL_MIN_VADDR ); hdr = (struct ucall_header *)addr_gva2hva (vm, vaddr);memset (hdr,0 ,sizeof (*hdr));
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
PATCH v6 7/7 KVM: selftests: Add simple sev vm testing
In reply to Peter GondaA very simple of booting SEV guests that checks related CPUID bits. This is a stripped down version of "[PATCH v2 08/13] KVM: selftests: add SEV boot tests" from Michael but much simpler.
--- tools/testing/selftests/kvm/.gitignore | 84 +++++++++++++++++++ tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/x86_64/sev_all_boot_test.c | 84 +++++++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c
tools/testing/selftests/kvm/.gitignore +84 -0
@@ -1,7 +1,91 @@ # SPDX-License-Identifier: GPL-2.0-only +<<<<<<< HEAD * !/**/ !*.c !*.h !*.S !*.sh +======= +/aarch64/aarch32_id_regs +/aarch64/arch_timer +/aarch64/debug-exceptions +/aarch64/get-reg-list +/aarch64/hypercalls +/aarch64/psci_test +/aarch64/vcpu_width_config +/aarch64/vgic_init +/aarch64/vgic_irq +/s390x/memop +/s390x/resets +/s390x/sync_regs_test +/s390x/tprot +/x86_64/amx_test +/x86_64/cpuid_test +/x86_64/cr4_cpuid_sync_test +/x86_64/debug_regs +/x86_64/evmcs_test +/x86_64/emulator_error_test +/x86_64/fix_hypercall_test +/x86_64/get_msr_index_features +/x86_64/kvm_clock_test +/x86_64/kvm_pv_test +/x86_64/hyperv_clock +/x86_64/hyperv_cpuid +/x86_64/hyperv_features +/x86_64/hyperv_svm_test +/x86_64/max_vcpuid_cap_test +/x86_64/mmio_warning_test +/x86_64/monitor_mwait_test +/x86_64/nested_exceptions_test +/x86_64/nx_huge_pages_test +/x86_64/platform_info_test +/x86_64/pmu_event_filter_test +/x86_64/set_boot_cpu_id +/x86_64/set_sregs_test +/x86_64/sev_all_boot_test +/x86_64/sev_migrate_tests +/x86_64/smm_test +/x86_64/state_test +/x86_64/svm_vmcall_test +/x86_64/svm_int_ctl_test +/x86_64/svm_nested_soft_inject_test +/x86_64/sync_regs_test +/x86_64/tsc_msrs_test +/x86_64/tsc_scaling_sync +/x86_64/ucna_injection_test +/x86_64/userspace_io_test +/x86_64/userspace_msr_exit_test +/x86_64/vmx_apic_access_test +/x86_64/vmx_close_while_nested_test +/x86_64/vmx_dirty_log_test +/x86_64/vmx_exception_with_invalid_guest_state +/x86_64/vmx_invalid_nested_guest_state +/x86_64/vmx_msrs_test +/x86_64/vmx_preemption_timer_test +/x86_64/vmx_set_nested_state_test +/x86_64/vmx_tsc_adjust_test +/x86_64/vmx_nested_tsc_scaling_test +/x86_64/xapic_ipi_test +/x86_64/xapic_state_test +/x86_64/xen_shinfo_test +/x86_64/xen_vmcall_test +/x86_64/xss_msr_test +/x86_64/vmx_pmu_caps_test +/x86_64/triple_fault_event_test +/access_tracking_perf_test +/demand_paging_test +/dirty_log_test +/dirty_log_perf_test +/hardware_disable_test +/kvm_create_max_vcpus +/kvm_page_table_test +/max_guest_memory_test +/memslot_modification_stress_test +/memslot_perf_test +/rseq_test +/set_memory_region_test +/steal_time +/kvm_binary_stats_test +/system_counter_offset_test +>>>>>>> KVM: selftests: Add simple sev vm testing
tools/testing/selftests/kvm/Makefile +1 -0
@@ -111,6 +111,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/tsc_msrs_test TEST_GEN_PROGS_x86_64 += x86_64/vmx_pmu_caps_test TEST_GEN_PROGS_x86_64 += x86_64/xen_shinfo_test TEST_GEN_PROGS_x86_64 += x86_64/xen_vmcall_test +TEST_GEN_PROGS_x86_64 += x86_64/sev_all_boot_test TEST_GEN_PROGS_x86_64 += x86_64/sev_migrate_tests TEST_GEN_PROGS_x86_64 += x86_64/amx_test TEST_GEN_PROGS_x86_64 += x86_64/max_vcpuid_cap_test
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +84 -0
84 lines not on this page · raw message
Signature · 1 line
2.39.0.314.g84b9a713c41-goog
Sean Christopherson
reviewerRe: [PATCH V6 7/7] KVM: selftests: Add simple sev vm testing
Quoted 23 lines · Peter Gonda
A very simple of booting SEV guests that checks related CPUID bits. This is a stripped down version of "[PATCH v2 08/13] KVM: selftests: add SEV boot tests" from Michael but much simpler.
Cc Paolo Bonzini <[email protected]>Cc Sean Christopherson <[email protected]>Cc Vishal Annapurve <[email protected]>Cc Ackerley Tng <[email protected]>cc Andrew Jones <[email protected]>Suggested-by Michael Roth <[email protected]>Signed-off-by Peter Gonda <[email protected]>--- tools/testing/selftests/kvm/.gitignore | 84 +++++++++++++++++++ tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/x86_64/sev_all_boot_test.c | 84 +++++++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/sev_all_boot_test.ctools/testing/selftests/kvm/.gitignore +0
-0
tools/testing/selftests/kvm/.gitignore line 9
Quoted 9 lines
tools/testing/selftests/kvm/.gitignore +2
-0@@ -1,7 +1,91 @@ # SPDX-License-Identifier: GPL-2.0-only +<<<<<<< HEAD * !/**/ !*.c !*.h !*.S !*.sh +=======
For the love of all that is holy, please take the time to actually look at what you're posting.
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c
Quoted 17 lines
tools/testing/selftests/kvm/Makefile +1
-0@@ -111,6 +111,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/tsc_msrs_test TEST_GEN_PROGS_x86_64 += x86_64/vmx_pmu_caps_test TEST_GEN_PROGS_x86_64 += x86_64/xen_shinfo_test TEST_GEN_PROGS_x86_64 += x86_64/xen_vmcall_test +TEST_GEN_PROGS_x86_64 += x86_64/sev_all_boot_test TEST_GEN_PROGS_x86_64 += x86_64/sev_migrate_tests TEST_GEN_PROGS_x86_64 += x86_64/amx_test TEST_GEN_PROGS_x86_64 += x86_64/max_vcpuid_cap_testtools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +0
-0
"all_boot" is kinda odd. Maybe just sev_basic_test?
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c line 5
Quoted 6 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +5
-0@@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Basic SEV boot tests. +* +*/
Meh, I'd leave off the comment, the testname should be pretty self-explanatory.
Quoted 6 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +6
-0+#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/ioctl.h> +
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c line 21
Quoted 10 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +10
-0+#include "test_util.h" +#include "kvm_util.h" +#include "processor.h" +#include "svm_util.h" +#include "linux/psp-sev.h" +#include "sev.h" + +#define NR_SYNCS 1 + +#define MSR_AMD64_SEV_BIT 1
msr-index.h exists in tools, use the defines from there.
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c line 23
Quoted 2 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +2
-0+ +static void guest_run_loop (struct kvm_vcpu *vcpu)
Another pointless helper with one caller. _If_ more subtests come along _and_ need this separate loop, then by all means. But right now, it's just noise.
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c line 28
Quoted 5 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +5
-0+{ +struct ucall uc; +int i; + +for (i =0 ; i <=NR_SYNCS ; ++i) {
There's zero reason to cap the number of loops, and it can lead to false passes, e.g. if the guest gets stuck on GUEST_SYNC() then this will pass. Just do
for (;;) {Quoted 20 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +20
-0+vcpu_run (vcpu); +switch (get_ucall (vcpu, &uc)) { +case UCALL_SYNC : +continue ; +case UCALL_DONE : +return ; +case UCALL_ABORT : +REPORT_GUEST_ASSERT (uc); +default : +TEST_FAIL ("Unexpected exit: %s" , +exit_reason_str (vcpu->run->exit_reason)); + } + } +} + +static void is_sev_enabled (void ) +{ +uint64_t sev_status; + +GUEST_ASSERT (this_cpu_has (X86_FEATURE_SEV ));
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c line 58
Quoted 10 lines
tools/testing/selftests/kvm/x86_64/sev_all_boot_test.c new file +10
-0+ + sev_status =rdmsr (MSR_AMD64_SEV ); +GUEST_ASSERT (sev_status &0x1 ); +} + +static void guest_sev_code (void ) +{ +GUEST_SYNC (1 ); + +is_sev_enabled ();
Again, completely unnecessary helper. And is_blah_enabled() _strongly_ suggests a pure function that returns true/false.
All in all, this can be slimmed down to:
// SPDX-License-Identifier: GPL-2.0-only
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include "test_util.h"
#include "kvm_util.h"
#include "processor.h"
#include "svm_util.h"
#include "linux/psp-sev.h"
#include "sev.h"
static void guest_sev_code(void)
{
GUEST_ASSERT(this_cpu_has(X86_FEATURE_SEV));
GUEST_ASSERT(rdmsr(MSR_AMD64_SEV) & MSR_AMD64_SEV_ENABLED);
GUEST_DONE();
}static void test_sev(void *guest_code, uint64_t policy)
{
struct kvm_vcpu *vcpu;
struct kvm_vm *vm;
struct ucall uc;
int i;
vm = vm_sev_create_with_one_vcpu(policy, guest_code, &vcpu);
for (;;) {
vcpu_run(vcpu);
switch (get_ucall(vcpu, &uc)) {
case UCALL_SYNC:
continue;
case UCALL_DONE:
return;
case UCALL_ABORT:
REPORT_GUEST_ASSERT(uc);
default:
TEST_FAIL("Unexpected exit: %s",
exit_reason_str(vcpu->run->exit_reason));
}
}
kvm_vm_free(vm);
}int main(int argc, char *argv[])
{
TEST_REQUIRE(is_kvm_sev_supported());
test_sev(guest_sev_code, SEV_POLICY_NO_DBG);
test_sev(guest_sev_code, 0);
return 0;
}Sean Christopherson
reviewerQuoted 4 lines · Peter Gonda
This patch series continues the work Michael Roth has done in supporting SEV guests in selftests. It continues on top of the work Sean Christopherson has sent to support ucalls from SEV guests. Along with a very simple version of the SEV selftests Michael originally proposed.
I got two copies of this series. AFAICT, the only difference is that LKML is Cc'd on the second send. When resending an _identical_ series, e.g. because you forgot to Cc' someone or because mails got lost in transit, add RESEND in between the square braces in the subject of all patches so as not to confuse folks that get both (or multiple) copies.
Quoted 7 lines · Sean Christopherson
Quoted 4 lines · Peter Gonda
This patch series continues the work Michael Roth has done in supporting SEV guests in selftests. It continues on top of the work Sean Christopherson has sent to support ucalls from SEV guests. Along with a very simple version of the SEV selftests Michael originally proposed.
Quoted 5 lines
I got two copies of this series. AFAICT, the only difference is that LKML is Cc'd on the second send. When resending an _identical_ series, e.g. because you forgot to Cc' someone or because mails got lost in transit, add RESEND in between the square braces in the subject of all patches so as not to confuse folks that get both (or multiple) copies.
Will do. My mistake, sorry.
Sean Christopherson
reviewerQuoted 4 lines · Peter Gonda
This patch series continues the work Michael Roth has done in supporting SEV guests in selftests. It continues on top of the work Sean Christopherson has sent to support ucalls from SEV guests. Along with a very simple version of the SEV selftests Michael originally proposed.
...
Quoted 10 lines
Michael Roth (2):
KVM: selftests: sparsebit: add const where appropriate
KVM: selftests: add support for protected vm_vaddr_* allocationsPeter Gonda (5):
KVM: selftests: add hooks for managing protected guest memory
KVM: selftests: handle protected bits in page tables
KVM: selftests: add library for creating/interacting with SEV guests
KVM: selftests: Update ucall pool to allocate from shared memory
KVM: selftests: Add simple sev vm testing
Lot's of non-trivial feedback. To help move things along, I pushed a completely untested (probably doesn't compile) branch to
https://github.com/sean-jc/linux/tree/x86/sev_selftests_for_peter
that has most of the feedback incorporated. Feel free to massage/rework things as needed (and again, it needs testing+polishing), my goal is purely to avoid you having to duplicate what I've already done as part of working through the review.