KVM: selftests: Add SEV and SEV-ES smoke tests
Sean Christopherson
reviewerv9 cover KVM: selftests: Add SEV and SEV-ES smoke tests
Add basic SEV and SEV-ES smoke tests. Unlike the intra-host migration tests, this one actually runs a small chunk of code in the guest.
Unless anyone strongly objects to the quick and dirty approach I've taken for SEV-ES, I'll get all of this queued for 6.9 soon-ish.
As for _why_ I added the quick-and-dirty SEV-ES testcase, I have a series to cleanup __svm_sev_es_vcpu_run(), and found out that apparently I have a version of OVMF that doesn't quite have to the right <something> for SEV-ES, and so I could even get a "real" VM to reach KVM_RUN. I assumed (correctly, yay!) that hacking together a selftest would be faster than figuring out what firmware magic I am missing.
v9:
- Drop is_kvm_sev_supported() and rely purely on KVM capabilities.
- Check X86_FEATURE_SEV to ensure SEV is actually enabled.
- Collect tags. [Carlos, Itaru]
v8:
- https://lore.kernel.org/all/cc9a1951-e76c-470d-a4d1-8ad67bae5794 at amd.com
- Undo the kvm.h uAPI breakage.
- Take advantage of "struct vm_shape", introduced by the guest_memfd selftests, to simply tracking the SEV/SEV-ES subtypes.
- Rename the test to "sev_smoke_test" instead of "sev_all_boot_test", as the "all" is rather nonsensical, and the test isn't booting anything in the traditional sense of the word.
- Drop vm->protected and instead add an arch hook to query if the VM has protected memory.
- Assert that the target memory region supports protected memory when allocating protected memory.
- Allocate protected_phy_pages for memory regions if and only if the VM supports protected memory.
- Rename kvm_host.h to kvm_util_arch.h, and move it to selftests/kvm where it belongs.
- Fix up some SoB goofs.
- Convert the intrahost SEV/SEV-ES migration tests to use common ioctl() wrappers.
Ackerley Tng (1):
KVM: selftests: Add a macro to iterate over a sparsebit range
Michael Roth (2):
KVM: selftests: Make sparsebit structs const where appropriate
KVM: selftests: Add support for protected vm_vaddr_* allocations
Peter Gonda (5):
KVM: selftests: Add support for allocating/managing protected guest
memory
KVM: selftests: Explicitly ucall pool from shared memory
KVM: selftests: Allow tagging protected memory in guest page tables
KVM: selftests: Add library for creating and interacting with SEV
guests
KVM: selftests: Add a basic SEV smoke test
Sean Christopherson (3):
KVM: selftests: Extend VM creation's @shape to allow control of VM
subtype
KVM: selftests: Use the SEV library APIs in the intra-host migration
test
KVM: selftests: Add a basic SEV-ES smoke test
tools/testing/selftests/kvm/Makefile | 2 + .../kvm/include/aarch64/kvm_util_arch.h | 7 ++ .../selftests/kvm/include/kvm_util_base.h | 50 +++++++- .../kvm/include/riscv/kvm_util_arch.h | 7 ++ .../kvm/include/s390x/kvm_util_arch.h | 7 ++ .../testing/selftests/kvm/include/sparsebit.h | 56 ++++++--- .../kvm/include/x86_64/kvm_util_arch.h | 23 ++++ .../selftests/kvm/include/x86_64/processor.h | 8 ++ .../selftests/kvm/include/x86_64/sev.h | 107 ++++++++++++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 67 ++++++++-- tools/testing/selftests/kvm/lib/sparsebit.c | 48 ++++---- .../testing/selftests/kvm/lib/ucall_common.c | 3 +- .../selftests/kvm/lib/x86_64/processor.c | 32 ++++- tools/testing/selftests/kvm/lib/x86_64/sev.c | 114 ++++++++++++++++++ .../selftests/kvm/x86_64/sev_migrate_tests.c | 67 ++++------ .../selftests/kvm/x86_64/sev_smoke_test.c | 88 ++++++++++++++ 16 files changed, 583 insertions(+), 103 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/aarch64/kvm_util_arch.h create mode 100644 tools/testing/selftests/kvm/include/riscv/kvm_util_arch.h create mode 100644 tools/testing/selftests/kvm/include/s390x/kvm_util_arch.h create mode 100644 tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.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_smoke_test.c
base-commit: 60eedcfceda9db46f1b333e5e1aa9359793f04fb
Signature · 1 line
2.44.0.rc0.258.g7320e95886-goog
Sean Christopherson
reviewerPATCH v9 1/11 KVM: selftests: Extend VM creation's @shape to allow control of VM subtype
Carve out space in the @shape passed to the various VM creation helpers to allow using the shape to control the subtype of VM, e.g. to identify x86's SEV VMs (which are "regular" VMs as far as KVM is concerned).
--- tools/testing/selftests/kvm/include/kvm_util_base.h | 9 +++++++-- tools/testing/selftests/kvm/lib/kvm_util.c | 1 + 2 files changed, 8 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +7 -2
@@ -90,6 +90,7 @@ enum kvm_mem_region_type {struct kvm_vm {int mode;unsigned long type; +uint8_t subtype;int kvm_fd;int fd;unsigned int pgtable_levels;
@@ -191,10 +192,14 @@ enum vm_guest_mode { };struct vm_shape { -enum vm_guest_mode mode; -unsigned int type; +uint32_t type; +uint8_t mode; +uint8_t subtype; +uint16_t padding; }; +kvm_static_assert (sizeof (struct vm_shape ) ==sizeof (uint64_t )); +#define VM_TYPE_DEFAULT 0#define VM_SHAPE (__mode) \
tools/testing/selftests/kvm/lib/kvm_util.c +1 -0
@@ -276,6 +276,7 @@ struct kvm_vm *____vm_create(struct vm_shape shape) vm->mode = shape.mode; vm->type = shape.type; + vm->subtype = shape.subtype; vm->pa_bits = vm_guest_mode_params[vm->mode].pa_bits; vm->va_bits = vm_guest_mode_params[vm->mode].va_bits;
Signature · 1 line
2.44.0.rc0.258.g7320e95886-goog
Sean Christopherson
reviewerPATCH v9 2/11 KVM: selftests: Make sparsebit structs const where appropriate
In reply to Sean ChristophersonFrom: Michael Roth <[email protected]>
Make all sparsebit struct pointers "const" where appropriate. This will allow adding a bitmap to track protected/encrypted physical memory that tests can access in a read-only fashion.
--- .../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.44.0.rc0.258.g7320e95886-goog
Sean Christopherson
reviewerPATCH v9 3/11 KVM: selftests: Add a macro to iterate over a sparsebit range
In reply to Sean ChristophersonFrom: Ackerley Tng <[email protected]>
Add sparsebit_for_each_set_range() to allow iterator over a range of set bits in a range. This will be used by x86 SEV guests to process protected physical pages (each such page needs to be encrypted _after_ being "added" to the VM).
--- .../testing/selftests/kvm/include/sparsebit.h | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+)
tools/testing/selftests/kvm/include/sparsebit.h +20 -0
@@ -66,6 +66,26 @@ void sparsebit_dump(FILE *stream, const struct sparsebit *sbit, unsigned int indent);void sparsebit_validate_internal (const struct sparsebit *sbit); +/* +* Iterate over an inclusive 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, at 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 +* 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) + #ifdef __cplusplus }#endif
Signature · 1 line
2.44.0.rc0.258.g7320e95886-goog
Sean Christopherson
reviewerPATCH v9 4/11 KVM: selftests: Add support for allocating/managing protected guest memory
In reply to Sean ChristophersonFrom: Peter Gonda <[email protected]>
Add support for differentiating between protected (a.k.a. private, a.k.a. encrypted) memory and normal (a.k.a. shared) memory for VMs that support protected guest memory, e.g. x86's SEV. Provide and manage a common bitmap for tracking whether a given physical page resides in protected memory, as support for protected memory isn't x86 specific, i.e. adding a arch hook would be a net negative now, and in the future.
--- .../selftests/kvm/include/kvm_util_base.h | 25 +++++++++++++++++-- tools/testing/selftests/kvm/lib/kvm_util.c | 22 +++++++++++++--- 2 files changed, 41 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +23 -2
@@ -46,6 +46,7 @@ typedef uint64_t vm_vaddr_t; /* Virtual Machine (Guest) virtual address */struct userspace_mem_region {struct kvm_userspace_memory_region2 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;
@@ -573,6 +574,13 @@ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, uint64_t guest_paddr, uint32_t slot, uint64_t npages, uint32_t flags, int guest_memfd_fd, uint64_t guest_memfd_offset); +#ifndef vm_arch_has_protected_memory +static inline bool vm_arch_has_protected_memory (struct kvm_vm *vm) +{ +return false; +} +#endif +void vm_mem_region_set_flags (struct kvm_vm *vm,uint32_t slot,uint32_t flags);void vm_mem_region_move (struct kvm_vm *vm,uint32_t slot,uint64_t new_gpa);void vm_mem_region_delete (struct kvm_vm *vm,uint32_t slot);
@@ -836,10 +844,23 @@ 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) +{ +/* +* By default, allocate memory as protected for VMs that support +* protected memory, as the majority of memory for such VMs is +* protected, i.e. using shared memory is effectively opt-in. +*/ +return __vm_phy_pages_alloc (vm, num, paddr_min, memslot, +vm_arch_has_protected_memory (vm)); +} +/* * ____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 +18 -4
@@ -717,6 +717,7 @@ static void __vm_mem_region_delete(struct kvm_vm *vm,vm_ioctl (vm,KVM_SET_USER_MEMORY_REGION2 , ®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 ) {
@@ -1098,6 +1099,8 @@ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, } region->unused_phy_pages =sparsebit_alloc (); +if (vm_arch_has_protected_memory (vm)) + region->protected_phy_pages =sparsebit_alloc ();sparsebit_set_num (region->unused_phy_pages, guest_paddr >> vm->page_shift, npages); region->region.slot = slot;
@@ -1924,6 +1927,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 (region->protected_phy_pages) { +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 );
@@ -2025,6 +2032,7 @@ const char *exit_reason_str(unsigned int exit_reason)* num - number of pages * paddr_min - Physical address minimum * memslot - Memory region to allocate page from +* protected - True if the pages will be used as protected/private memory * * Output Args: None *
@@ -2036,8 +2044,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;
@@ -2050,8 +2059,10 @@ vm_paddr_t vm_phy_pages_alloc(struct kvm_vm *vm, size_t num, paddr_min, vm->page_size); region =memslot2region (vm, memslot); +TEST_ASSERT (!protected || region->protected_phy_pages, +"Region doesn't support protected memory" ); + base = pg = paddr_min >> vm->page_shift; - do { for (; pg < base + num; ++pg) { if (!sparsebit_is_set(region->unused_phy_pages, pg)) {
@@ -2070,8 +2081,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.44.0.rc0.258.g7320e95886-goog
Sean Christopherson
reviewerPATCH v9 5/11 KVM: selftests: Add support for protected vm_vaddr_* allocations
In reply to Sean ChristophersonFrom: 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 | 3 +++ tools/testing/selftests/kvm/lib/kvm_util.c | 26 +++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +3 -0
@@ -590,6 +590,9 @@ 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, +enum kvm_mem_region_type type);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 +21 -5
@@ -1431,15 +1431,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 protected) {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, -KVM_UTIL_MIN_PFN * vm->page_size, - vm->memslots[type]); +vm_paddr_t paddr =__vm_phy_pages_alloc (vm, pages, +KVM_UTIL_MIN_PFN * vm->page_size, + vm->memslots[type], protected);/* * Find an unused range of virtual page addresses of at least
@@ -1459,6 +1461,20 @@ 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_arch_has_protected_memory (vm)); +} + +vm_vaddr_t vm_vaddr_alloc_shared (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, false); +} +/* * VM Virtual Address Allocate *
Signature · 1 line
2.44.0.rc0.258.g7320e95886-goog
Sean Christopherson
reviewerPATCH v9 6/11 KVM: selftests: Explicitly ucall pool from shared memory
In reply to Sean ChristophersonFrom: Peter Gonda <[email protected]>
Allocate the common ucall pool using vm_vaddr_alloc_shared() so that the ucall structures will be placed in shared (unencrypted) memory for VMs with support for protected (encrypted) memory, e.g. x86's SEV.
--- tools/testing/selftests/kvm/lib/ucall_common.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/lib/ucall_common.c +2 -1
@@ -29,7 +29,8 @@ 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 , +MEM_REGION_DATA ); hdr = (struct ucall_header *)addr_gva2hva (vm, vaddr);memset (hdr,0 ,sizeof (*hdr));
Signature · 1 line
2.44.0.rc0.258.g7320e95886-goog
Sean Christopherson
reviewerPATCH v9 7/11 KVM: selftests: Allow tagging protected memory in guest page tables
In reply to Sean ChristophersonFrom: Peter Gonda <[email protected]>
Add support for tagging and untagging guest physical address, e.g. to allow x86's SEV and TDX guests to embed shared vs. private information in the GPA. SEV (encryption, a.k.a. C-bit) and TDX (shared, a.k.a. S-bit) steal bits from the guest's physical address space that is consumed by the CPU metadata, i.e. effectively aliases the "real" GPA.
Implement generic "tagging" so that the shared vs. private metadata can be managed by x86 without bleeding too many details into common code.
--- .../kvm/include/aarch64/kvm_util_arch.h | 7 +++++++ .../selftests/kvm/include/kvm_util_base.h | 13 ++++++++++++ .../kvm/include/riscv/kvm_util_arch.h | 7 +++++++ .../kvm/include/s390x/kvm_util_arch.h | 7 +++++++ .../kvm/include/x86_64/kvm_util_arch.h | 21 +++++++++++++++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 17 +++++++++++++++ .../selftests/kvm/lib/x86_64/processor.c | 15 ++++++++++++- 7 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/kvm/include/aarch64/kvm_util_arch.h create mode 100644 tools/testing/selftests/kvm/include/riscv/kvm_util_arch.h create mode 100644 tools/testing/selftests/kvm/include/s390x/kvm_util_arch.h create mode 100644 tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h
tools/testing/selftests/kvm/include/aarch64/kvm_util_arch.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/testing/selftests/kvm/include/kvm_util_base.h +13 -0
@@ -18,9 +18,11 @@#include <linux/types.h> #include <asm/atomic.h> +#include <asm/kvm.h> #include <sys/ioctl.h> +#include "kvm_util_arch.h" #include "sparsebit.h" /*
@@ -113,6 +115,9 @@ struct kvm_vm {vm_vaddr_t idt;vm_vaddr_t handlers;uint32_t dirty_ring_size; +uint64_t gpa_tag_mask; + +struct kvm_vm_arch arch;/* Cache of information for binary stats interface */ int stats_fd;
@@ -605,6 +610,12 @@ 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_tag_mask; +} +void vcpu_run (struct kvm_vcpu *vcpu);int _vcpu_run (struct kvm_vcpu *vcpu);
@@ -1114,4 +1125,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/include/riscv/kvm_util_arch.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/testing/selftests/kvm/include/s390x/kvm_util_arch.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/testing/selftests/kvm/include/x86_64/kvm_util_arch.h new file +21 -0
@@ -0,0 +1,21 @@ +/* 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; +}; + +static inline bool __vm_arch_has_protected_memory (struct kvm_vm_arch *arch) +{ +return arch->c_bit || arch->s_bit; +} + +#define vm_arch_has_protected_memory (vm) \ + __vm_arch_has_protected_memory(&(vm)->arch) + +#endif // _TOOLS_LINUX_ASM_X86_KVM_HOST_H
tools/testing/selftests/kvm/lib/kvm_util.c +17 -0
@@ -1597,6 +1597,8 @@ void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa) {struct userspace_mem_region *region; + gpa =vm_untag_gpa (vm, gpa); + region =userspace_mem_region_find (vm, gpa, gpa);if (!region) {TEST_FAIL ("No vm physical memory at 0x%lx" , gpa);
@@ -2305,3 +2307,18 @@ 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_arch_has_protected_memory (vm)) +return false; + + region =userspace_mem_region_find (vm, paddr, paddr); +TEST_ASSERT (region,"No vm physical memory at 0x%lx" , paddr); + + pg = paddr >> vm->page_shift; +return sparsebit_is_set (region->protected_phy_pages, pg); +}
tools/testing/selftests/kvm/lib/x86_64/processor.c +14 -1
@@ -157,6 +157,8 @@ static uint64_t *virt_create_upper_pte(struct kvm_vm *vm, {uint64_t *pte =virt_get_pte (vm, parent_pte, vaddr, current_level); + paddr =vm_untag_gpa (vm, paddr); +if (!(*pte &PTE_PRESENT_MASK )) { *pte =PTE_PRESENT_MASK |PTE_WRITABLE_MASK ;if (current_level == target_level)
@@ -200,6 +202,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
@@ -222,6 +226,15 @@ 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" , vaddr); *pte =PTE_PRESENT_MASK |PTE_WRITABLE_MASK | (paddr &PHYSICAL_PAGE_MASK ); + +/* +* Neither SEV nor TDX supports shared page tables, so only the final +* leaf PTE needs manually set the C/S-bit. +*/ +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)
@@ -496,7 +509,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.44.0.rc0.258.g7320e95886-goog
Re: [PATCH v9 07/11] KVM: selftests: Allow tagging protected memory in guest page tables
On 2/23/2024 8:42 AM, Sean Christopherson wrote:
...
tools/testing/selftests/kvm/include/aarch64/kvm_util_arch.h line 3
Quoted 9 lines
tools/testing/selftests/kvm/include/aarch64/kvm_util_arch.h new file +3
-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
Since the file name is changed from kvm_host.h (in v7) to kvm_util_arch.h, we need to update it as well.
Ditto for other archs
Quoted 3 lines
tools/testing/selftests/kvm/include/aarch64/kvm_util_arch.h new file +3
-0+struct kvm_vm_arch {}; + +#endif // _TOOLS_LINUX_ASM_ARM64_KVM_HOST_H
Sean Christopherson
reviewerRe: [PATCH v9 07/11] KVM: selftests: Allow tagging protected memory in guest page tables
Quoted 15 lines · Xiaoyao Li
On 2/23/2024 8:42 AM, Sean Christopherson wrote:
...tools/testing/selftests/kvm/include/aarch64/kvm_util_arch.h line 3
Quoted 9 lines
tools/testing/selftests/kvm/include/aarch64/kvm_util_arch.h new file +3
-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_HSince the file name is changed from
kvm_host.h(in v7) tokvm_util_arch.h, we need to update it as well.
Quoted 1 line
Ditto for other archs
Ugh, nice catch. I'll fixup and force push (likely tomorrow).
Quoted 5 lines
Quoted 3 lines
tools/testing/selftests/kvm/include/aarch64/kvm_util_arch.h new file +3
-0+struct kvm_vm_arch {}; + +#endif // _TOOLS_LINUX_ASM_ARM64_KVM_HOST_H
Sean Christopherson
reviewerRe: [PATCH v9 07/11] KVM: selftests: Allow tagging protected memory in guest page tables
Quoted 16 lines · Sean Christopherson
Quoted 15 lines · Xiaoyao Li
On 2/23/2024 8:42 AM, Sean Christopherson wrote:
...tools/testing/selftests/kvm/include/aarch64/kvm_util_arch.h line 3
Quoted 9 lines
tools/testing/selftests/kvm/include/aarch64/kvm_util_arch.h new file +3
-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_HSince the file name is changed from
kvm_host.h(in v7) tokvm_util_arch.h, we need to update it as well.
Quoted 3 lines
Quoted 1 line
Ditto for other archs
Ugh, nice catch. I'll fixup and force push (likely tomorrow).
Here's the diff of my fixup:
tools/testing/selftests/kvm/include/aarch64/kvm_util_arch.h +3 -3
@@ -1,7 +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 +#ifndef SELFTEST_KVM_UTIL_ARCH_H +#define SELFTEST_KVM_UTIL_ARCH_H struct kvm_vm_arch {}; -#endif // _TOOLS_LINUX_ASM_ARM64_KVM_HOST_H +#endif // SELFTEST_KVM_UTIL_ARCH_H
tools/testing/selftests/kvm/include/riscv/kvm_util_arch.h +3 -3
@@ -1,7 +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 +#ifndef SELFTEST_KVM_UTIL_ARCH_H +#define SELFTEST_KVM_UTIL_ARCH_H struct kvm_vm_arch {}; -#endif // _TOOLS_LINUX_ASM_RISCV_KVM_HOST_H +#endif // SELFTEST_KVM_UTIL_ARCH_H
tools/testing/selftests/kvm/include/s390x/kvm_util_arch.h +3 -3
@@ -1,7 +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 +#ifndef SELFTEST_KVM_UTIL_ARCH_H +#define SELFTEST_KVM_UTIL_ARCH_H struct kvm_vm_arch {}; -#endif // _TOOLS_LINUX_ASM_S390_KVM_HOST_H +#endif // SELFTEST_KVM_UTIL_ARCH_H
tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h +3 -3
@@ -1,6 +1,6 @@/* SPDX-License-Identifier: GPL-2.0-only */ -#ifndef _TOOLS_LINUX_ASM_X86_KVM_HOST_H -#define _TOOLS_LINUX_ASM_X86_KVM_HOST_H +#ifndef SELFTEST_KVM_UTIL_ARCH_H +#define SELFTEST_KVM_UTIL_ARCH_H #include <stdbool.h> #include <stdint.h>
@@ -20,4 +20,4 @@ static inline bool __vm_arch_has_protected_memory(struct kvm_vm_arch *arch)#define vm_arch_has_protected_memory (vm) \ __vm_arch_has_protected_memory(&(vm)->arch) -#endif // _TOOLS_LINUX_ASM_X86_KVM_HOST_H +#endif // SELFTEST_KVM_UTIL_ARCH_H
Sean Christopherson
reviewerPATCH v9 8/11 KVM: selftests: Add library for creating and interacting with SEV guests
In reply to Sean ChristophersonFrom: Peter Gonda <[email protected]>
Add a library/APIs for creating and interfacing with SEV guests, all of which need some amount of common functionality, e.g. an open file handle for the SEV driver (/dev/sev), ioctl() wrappers to pass said file handle to KVM, tracking of the C-bit, etc.
Add an x86-specific hook to initialize address properties, a.k.a. the location of the C-bit. An arch specific hook is rather gross, but x86 already has a dedicated #ifdef-protected kvm_get_cpu_address_width() hook, i.e. the ugliest code already exists.
--- tools/testing/selftests/kvm/Makefile | 1 + .../kvm/include/x86_64/kvm_util_arch.h | 2 + .../selftests/kvm/include/x86_64/processor.h | 8 ++ .../selftests/kvm/include/x86_64/sev.h | 105 +++++++++++++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 1 + .../selftests/kvm/lib/x86_64/processor.c | 17 +++ tools/testing/selftests/kvm/lib/x86_64/sev.c | 110 ++++++++++++++++++ 7 files changed, 244 insertions(+) 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/testing/selftests/kvm/Makefile +1 -0
@@ -38,6 +38,7 @@ LIBKVM_x86_64 += lib/x86_64/hyperv.c LIBKVM_x86_64 += lib/x86_64/memstress.c LIBKVM_x86_64 += lib/x86_64/pmu.c LIBKVM_x86_64 += lib/x86_64/processor.c +LIBKVM_x86_64 += lib/x86_64/sev.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
tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.h +2 -0
@@ -8,6 +8,8 @@struct kvm_vm_arch {uint64_t c_bit;uint64_t s_bit; +int sev_fd; +bool is_pt_protected; };static inline bool __vm_arch_has_protected_memory (struct kvm_vm_arch *arch)
tools/testing/selftests/kvm/include/x86_64/processor.h +8 -0
@@ -23,6 +23,12 @@extern bool host_cpu_is_intel;extern bool host_cpu_is_amd; +enum vm_guest_x86_subtype { +VM_SUBTYPE_NONE =0 , +VM_SUBTYPE_SEV , +VM_SUBTYPE_SEV_ES , +}; +/* Forced emulation prefix, used to invoke the emulator unconditionally. */ #define KVM_FEP "ud2; .byte 'k', 'v', 'm';"
@@ -276,6 +282,7 @@ struct kvm_x86_cpu_property {#define X86_PROPERTY_MAX_EXT_LEAF KVM_X86_CPU_PROPERTY(0x80000000, 0, EAX, 0, 31)#define X86_PROPERTY_MAX_PHY_ADDR KVM_X86_CPU_PROPERTY(0x80000008, 0, EAX, 0, 7)#define X86_PROPERTY_MAX_VIRT_ADDR KVM_X86_CPU_PROPERTY(0x80000008, 0, EAX, 8, 15) +#define X86_PROPERTY_SEV_C_BIT KVM_X86_CPU_PROPERTY(0x8000001F, 0, EBX, 0, 5)#define X86_PROPERTY_PHYS_ADDR_REDUCTION KVM_X86_CPU_PROPERTY(0x8000001F, 0, EBX, 6, 11)#define X86_PROPERTY_MAX_CENTAUR_LEAF KVM_X86_CPU_PROPERTY(0xC0000000, 0, EAX, 0, 31)
@@ -1093,6 +1100,7 @@ do { \ }while (0 ) 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 +105 -0
105 lines not on this page · raw message
tools/testing/selftests/kvm/lib/kvm_util.c +1 -0
@@ -317,6 +317,7 @@ struct kvm_vm *____vm_create(struct vm_shape shape)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
tools/testing/selftests/kvm/lib/x86_64/processor.c +17 -0
@@ -9,6 +9,7 @@#include "test_util.h" #include "kvm_util.h" #include "processor.h" +#include "sev.h" #ifndef NUM_INTERRUPTS #define NUM_INTERRUPTS 256
@@ -278,6 +279,9 @@ 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, +"Walking page tables of protected guests is impossible" ); +TEST_ASSERT (*level >=PG_LEVEL_NONE && *level <PG_LEVEL_NUM ,"Invalid PG_LEVEL_* '%d'" , *level);
@@ -573,6 +577,11 @@ void kvm_arch_vm_post_create(struct kvm_vm *vm)vm_create_irqchip (vm);sync_global_to_guest (vm, host_cpu_is_intel);sync_global_to_guest (vm, host_cpu_is_amd); + +if (vm->subtype ==VM_SUBTYPE_SEV ) +sev_vm_init (vm); +else if (vm->subtype ==VM_SUBTYPE_SEV_ES ) +sev_es_vm_init (vm); } struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
@@ -1063,6 +1072,14 @@ void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits) } } +void kvm_init_vm_address_properties (struct kvm_vm *vm) +{ +if (vm->subtype ==VM_SUBTYPE_SEV ) { + vm->arch.c_bit =BIT_ULL (this_cpu_property (X86_PROPERTY_SEV_C_BIT )); + vm->gpa_tag_mask = vm->arch.c_bit; + } +} +static void set_idt_entry (struct kvm_vm *vm,int vector,unsigned long addr,int dpl,unsigned short selector) {
tools/testing/selftests/kvm/lib/x86_64/sev.c new file +110 -0
110 lines not on this page · raw message
Signature · 1 line
2.44.0.rc0.258.g7320e95886-goog
Sean Christopherson
reviewerPATCH v9 9/11 KVM: selftests: Use the SEV library APIs in the intra-host migration test
In reply to Sean ChristophersonPort the existing intra-host SEV(-ES) migration test to the recently added SEV library, which handles much of the boilerplate needed to create and configure SEV guests.
--- .../selftests/kvm/x86_64/sev_migrate_tests.c | 67 ++++++------------- 1 file changed, 21 insertions(+), 46 deletions(-)
tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c +21 -46
@@ -10,11 +10,9 @@#include "test_util.h" #include "kvm_util.h" #include "processor.h" -#include "svm_util.h" +#include "sev.h" #include "kselftest.h" -#define SEV_POLICY_ES 0b100 -#define NR_MIGRATE_TEST_VCPUS 4#define NR_MIGRATE_TEST_VMS 3#define NR_LOCK_TESTING_THREADS 3
@@ -22,46 +20,24 @@bool have_sev_es; -static int __sev_ioctl (int vm_fd,int cmd_id,void *data,__u32 *fw_error) -{ -struct kvm_sev_cmd cmd = { - .id = cmd_id, - .data = (uint64_t )data, - .sev_fd =open_sev_dev_path_or_exit (), - }; -int ret; - - ret =ioctl (vm_fd,KVM_MEMORY_ENCRYPT_OP , &cmd); - *fw_error = cmd.error; -return ret; -} - -static void sev_ioctl (int vm_fd,int cmd_id,void *data) -{ -int ret; -__u32 fw_error; - - ret =__sev_ioctl (vm_fd, cmd_id, data, &fw_error); -TEST_ASSERT (ret ==0 && fw_error ==SEV_RET_SUCCESS , -"%d failed: return code: %d, errno: %d, fw error: %d" , - cmd_id, ret, errno, fw_error); -} -static struct kvm_vm *sev_vm_create (bool es) {struct kvm_vm *vm; -struct kvm_sev_launch_start start = {0 };int i; vm =vm_create_barebones (); -sev_ioctl (vm->fd, es ?KVM_SEV_ES_INIT :KVM_SEV_INIT ,NULL ); +if (!es) +sev_vm_init (vm); +else +sev_es_vm_init (vm); +for (i =0 ; i <NR_MIGRATE_TEST_VCPUS ; ++i)__vm_vcpu_add (vm, i); + +sev_vm_launch (vm, es ?SEV_POLICY_ES :0 ); +if (es) - start.policy |=SEV_POLICY_ES ; -sev_ioctl (vm->fd,KVM_SEV_LAUNCH_START , &start); -if (es) -sev_ioctl (vm->fd,KVM_SEV_LAUNCH_UPDATE_VMSA ,NULL ); +vm_sev_ioctl (vm,KVM_SEV_LAUNCH_UPDATE_VMSA ,NULL );return vm; }
@@ -181,7 +157,7 @@ static void test_sev_migrate_parameters(void) sev_vm =sev_vm_create (/* es= */ false); sev_es_vm =sev_vm_create (/* es= */ true); sev_es_vm_no_vmsa =vm_create_barebones (); -sev_ioctl (sev_es_vm_no_vmsa->fd,KVM_SEV_ES_INIT ,NULL ); +sev_es_vm_init (sev_es_vm_no_vmsa);__vm_vcpu_add (sev_es_vm_no_vmsa,1 ); ret =__sev_migrate_from (sev_vm, sev_es_vm);
@@ -230,13 +206,13 @@ static void sev_mirror_create(struct kvm_vm *dst, struct kvm_vm *src)TEST_ASSERT (!ret,"Copying context failed, ret: %d, errno: %d" , ret, errno); } -static void verify_mirror_allowed_cmds (int vm_fd) +static void verify_mirror_allowed_cmds (struct kvm_vm *vm) {struct kvm_sev_guest_status status; +int cmd_id; -for (int cmd_id =KVM_SEV_INIT ; cmd_id <KVM_SEV_NR_MAX ; ++cmd_id) { +for (cmd_id =KVM_SEV_INIT ; cmd_id <KVM_SEV_NR_MAX ; ++cmd_id) {int ret; -__u32 fw_error;/* * These commands are allowed for mirror VMs, all others are
@@ -256,14 +232,13 @@ static void verify_mirror_allowed_cmds(int vm_fd)* These commands should be disallowed before the data * parameter is examined so NULL is OK here. */ - ret =__sev_ioctl (vm_fd, cmd_id,NULL , &fw_error); -TEST_ASSERT ( - ret ==-1 && errno ==EINVAL , -"Should not be able call command: %d. ret: %d, errno: %d" , - cmd_id, ret, errno); + ret =__vm_sev_ioctl (vm, cmd_id,NULL ); +TEST_ASSERT (ret ==-1 && errno ==EINVAL , +"Should not be able call command: %d. ret: %d, errno: %d" , + cmd_id, ret, errno); } -sev_ioctl (vm_fd,KVM_SEV_GUEST_STATUS , &status); +vm_sev_ioctl (vm,KVM_SEV_GUEST_STATUS , &status); }static void test_sev_mirror (bool es)
@@ -281,9 +256,9 @@ static void test_sev_mirror(bool es)__vm_vcpu_add (dst_vm, i);if (es) -sev_ioctl (dst_vm->fd,KVM_SEV_LAUNCH_UPDATE_VMSA ,NULL ); +vm_sev_ioctl (dst_vm,KVM_SEV_LAUNCH_UPDATE_VMSA ,NULL ); -verify_mirror_allowed_cmds (dst_vm->fd); +verify_mirror_allowed_cmds (dst_vm);kvm_vm_free (src_vm);kvm_vm_free (dst_vm);
Signature · 1 line
2.44.0.rc0.258.g7320e95886-goog
Sean Christopherson
reviewerPATCH v9 10/11 KVM: selftests: Add a basic SEV smoke test
In reply to Sean ChristophersonFrom: Peter Gonda <[email protected]>
Add a basic smoke test for SEV guests to verify that KVM can launch an SEV guest and run a few instructions without exploding. To verify that SEV is indeed enabled, assert that SEV is reported as enabled in MSR_AMD64_SEV, a.k.a. SEV_STATUS, which cannot be intercepted by KVM (architecturally enforced).
--- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/x86_64/sev_smoke_test.c | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/sev_smoke_test.c
tools/testing/selftests/kvm/Makefile +1 -0
@@ -120,6 +120,7 @@ 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_migrate_tests +TEST_GEN_PROGS_x86_64 += x86_64/sev_smoke_test TEST_GEN_PROGS_x86_64 += x86_64/amx_test TEST_GEN_PROGS_x86_64 += x86_64/max_vcpuid_cap_test TEST_GEN_PROGS_x86_64 += x86_64/triple_fault_event_test
tools/testing/selftests/kvm/x86_64/sev_smoke_test.c new file +58 -0
58 lines not on this page · raw message
Signature · 1 line
2.44.0.rc0.258.g7320e95886-goog
Sean Christopherson
reviewerPATCH v9 11/11 KVM: selftests: Add a basic SEV-ES smoke test
In reply to Sean ChristophersonExtend sev_smoke_test to also run a minimal SEV-ES smoke test so that it's possible to test KVM's unique VMRUN=>#VMEXIT path for SEV-ES guests without needing a full blown SEV-ES capable VM, which requires a rather absurd amount of properly configured collateral.
Punt on proper GHCB and ucall support, and instead use the GHCB MSR protocol to signal test completion. The most important thing at this point is to have _any_ kind of testing of KVM's __svm_sev_es_vcpu_run().
--- .../selftests/kvm/include/x86_64/sev.h | 2 ++ .../selftests/kvm/lib/x86_64/processor.c | 2 +- tools/testing/selftests/kvm/lib/x86_64/sev.c | 6 +++- .../selftests/kvm/x86_64/sev_smoke_test.c | 30 +++++++++++++++++++ 4 files changed, 38 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/include/x86_64/sev.h +2 -0
@@ -25,6 +25,8 @@ enum sev_guest_state {#define SEV_POLICY_NO_DBG (1UL << 0)#define SEV_POLICY_ES (1UL << 2) +#define GHCB_MSR_TERM_REQ 0x100 +void sev_vm_launch (struct kvm_vm *vm,uint32_t policy);void sev_vm_launch_measure (struct kvm_vm *vm,uint8_t *measurement);void sev_vm_launch_finish (struct kvm_vm *vm);
tools/testing/selftests/kvm/lib/x86_64/processor.c +1 -1
@@ -1074,7 +1074,7 @@ void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits)void kvm_init_vm_address_properties (struct kvm_vm *vm) { -if (vm->subtype ==VM_SUBTYPE_SEV ) { +if (vm->subtype ==VM_SUBTYPE_SEV || vm->subtype ==VM_SUBTYPE_SEV_ES ) { vm->arch.c_bit =BIT_ULL (this_cpu_property (X86_PROPERTY_SEV_C_BIT )); vm->gpa_tag_mask = vm->arch.c_bit; }
tools/testing/selftests/kvm/lib/x86_64/sev.c +5 -1
@@ -53,6 +53,9 @@ void sev_vm_launch(struct kvm_vm *vm, uint32_t policy)hash_for_each (vm->regions.slot_hash, ctr, region, slot_node)encrypt_region (vm, region); +if (policy &SEV_POLICY_ES ) +vm_sev_ioctl (vm,KVM_SEV_LAUNCH_UPDATE_VMSA ,NULL ); + vm->arch.is_pt_protected = true; }
@@ -90,7 +93,8 @@ struct kvm_vm *vm_sev_create_with_one_vcpu(uint32_t policy, void *guest_code,struct vm_shape shape = { .type =VM_TYPE_DEFAULT , .mode =VM_MODE_DEFAULT , - .subtype =VM_SUBTYPE_SEV , + .subtype = policy &SEV_POLICY_ES ?VM_SUBTYPE_SEV_ES : +VM_SUBTYPE_SEV , };struct kvm_vm *vm;struct kvm_vcpu *cpus[1 ];
tools/testing/selftests/kvm/x86_64/sev_smoke_test.c +30 -0
@@ -12,6 +12,21 @@#include "linux/psp-sev.h" #include "sev.h" + +static void guest_sev_es_code (void ) +{ +/* TODO: Check CPUID after GHCB-based hypercall support is added. */ +GUEST_ASSERT (rdmsr (MSR_AMD64_SEV ) &MSR_AMD64_SEV_ENABLED ); +GUEST_ASSERT (rdmsr (MSR_AMD64_SEV ) &MSR_AMD64_SEV_ES_ENABLED ); + +/* +* TODO: Add GHCB and ucall support for SEV-ES guests. For now, simply +* force "termination" to signal "done" via the GHCB MSR protocol. +*/ +wrmsr (MSR_AMD64_SEV_ES_GHCB ,GHCB_MSR_TERM_REQ ); + __asm__ __volatile__("rep; vmmcall" ); +} +static void guest_sev_code (void ) {GUEST_ASSERT (this_cpu_has (X86_FEATURE_SEV ));
@@ -31,6 +46,16 @@ static void test_sev(void *guest_code, uint64_t policy)for (;;) {vcpu_run (vcpu); +if (policy &SEV_POLICY_ES ) { +TEST_ASSERT (vcpu->run->exit_reason ==KVM_EXIT_SYSTEM_EVENT , +"Wanted SYSTEM_EVENT, got %s" , +exit_reason_str (vcpu->run->exit_reason)); +TEST_ASSERT_EQ (vcpu->run->system_event.type,KVM_SYSTEM_EVENT_SEV_TERM ); +TEST_ASSERT_EQ (vcpu->run->system_event.ndata,1 ); +TEST_ASSERT_EQ (vcpu->run->system_event.data[0 ],GHCB_MSR_TERM_REQ ); +break ; + } +switch (get_ucall (vcpu, &uc)) {case UCALL_SYNC :continue ;
@@ -54,5 +79,10 @@ int main(int argc, char *argv[])test_sev (guest_sev_code,SEV_POLICY_NO_DBG );test_sev (guest_sev_code,0 ); +if (kvm_cpu_has (X86_FEATURE_SEV_ES )) { +test_sev (guest_sev_es_code,SEV_POLICY_ES |SEV_POLICY_NO_DBG ); +test_sev (guest_sev_es_code,SEV_POLICY_ES ); + } +return 0 ; }
Signature · 1 line
2.44.0.rc0.258.g7320e95886-goog
Hello,
Quoted 6 lines · Sean Christopherson
Add basic SEV and SEV-ES smoke tests. Unlike the intra-host migration tests, this one actually runs a small chunk of code in the guest.
Unless anyone strongly objects to the quick and dirty approach I've taken for SEV-ES, I'll get all of this queued for 6.9 soon-ish.
Quoted 6 lines
As for _why_ I added the quick-and-dirty SEV-ES testcase, I have a series to cleanup
__svm_sev_es_vcpu_run(), and found out that apparently I have a version of OVMF that doesn't quite have to the right <something> for SEV-ES, and so I could even get a "real" VM to reachKVM_RUN. I assumed (correctly, yay!) that hacking together a selftest would be faster than figuring out what firmware magic I am missing.
Quoted 75 lines
v9:
- Dropis_kvm_sev_supported()and rely purely on KVM capabilities.
- CheckX86_FEATURE_SEVto ensure SEV is actually enabled.
- Collect tags. [Carlos, Itaru]v8:
- https://lore.kernel.org/all/cc9a1951-e76c-470d-a4d1-8ad67bae5794 at amd.com
- Undo thekvm.huAPI breakage.
- Take advantage of "structvm_shape", introduced by theguest_memfdselftests, to simply tracking the SEV/SEV-ES subtypes.
- Rename the test to "sev_smoke_test" instead of "sev_all_boot_test", as the "all" is rather nonsensical, and the test isn't booting anything in the traditional sense of the word.
- Dropvm->protectedand instead add an arch hook to query if the VM has protected memory.
- Assert that the target memory region supports protected memory when allocating protected memory.
- Allocateprotected_phy_pagesfor memory regions if and only if the VM supports protected memory.
- Renamekvm_host.htokvm_util_arch.h, and move it to selftests/kvm where it belongs.
- Fix up some SoB goofs.
- Convert the intrahost SEV/SEV-ES migration tests to use commonioctl()wrappers.Ackerley Tng (1):
KVM: selftests: Add a macro to iterate over a sparsebit rangeMichael Roth (2):
KVM: selftests: Make sparsebit structs const where appropriate
KVM: selftests: Add support for protected vm_vaddr_* allocationsPeter Gonda (5):
KVM: selftests: Add support for allocating/managing protected guest
memory
KVM: selftests: Explicitly ucall pool from shared memory
KVM: selftests: Allow tagging protected memory in guest page tables
KVM: selftests: Add library for creating and interacting with SEV
guests
KVM: selftests: Add a basic SEV smoke testSean Christopherson (3):
KVM: selftests: Extend VM creation's @shape to allow control of VM
subtype
KVM: selftests: Use the SEV library APIs in the intra-host migration
test
KVM: selftests: Add a basic SEV-ES smoke testtools/testing/selftests/kvm/Makefile | 2 + .../kvm/include/aarch64/kvm_util_arch.h | 7 ++ .../selftests/kvm/include/kvm_util_base.h | 50 +++++++- .../kvm/include/riscv/kvm_util_arch.h | 7 ++ .../kvm/include/s390x/kvm_util_arch.h | 7 ++ .../testing/selftests/kvm/include/sparsebit.h | 56 ++++++--- .../kvm/include/x86_64/kvm_util_arch.h | 23 ++++ .../selftests/kvm/include/x86_64/processor.h | 8 ++ .../selftests/kvm/include/x86_64/sev.h | 107 ++++++++++++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 67 ++++++++-- tools/testing/selftests/kvm/lib/sparsebit.c | 48 ++++---- .../testing/selftests/kvm/lib/ucall_common.c | 3 +- .../selftests/kvm/lib/x86_64/processor.c | 32 ++++- tools/testing/selftests/kvm/lib/x86_64/sev.c | 114 ++++++++++++++++++ .../selftests/kvm/x86_64/sev_migrate_tests.c | 67 ++++------ .../selftests/kvm/x86_64/sev_smoke_test.c | 88 ++++++++++++++ 16 files changed, 583 insertions(+), 103 deletions(-)create mode 100644
tools/testing/selftests/kvm/include/aarch64/kvm_util_arch.hcreate mode 100644tools/testing/selftests/kvm/include/riscv/kvm_util_arch.hcreate mode 100644tools/testing/selftests/kvm/include/s390x/kvm_util_arch.hcreate mode 100644tools/testing/selftests/kvm/include/x86_64/kvm_util_arch.hcreate mode 100644tools/testing/selftests/kvm/include/x86_64/sev.hcreate mode 100644tools/testing/selftests/kvm/lib/x86_64/sev.ccreate mode 100644tools/testing/selftests/kvm/x86_64/sev_smoke_test.c
Quoted 1 line
base-commit:
60eedcfceda9db46f1b333e5e1aa9359793f04fb
Thanks,
Carlos
Sean Christopherson
reviewerQuoted 6 lines · Sean Christopherson
Add basic SEV and SEV-ES smoke tests. Unlike the intra-host migration tests, this one actually runs a small chunk of code in the guest.
Unless anyone strongly objects to the quick and dirty approach I've taken for SEV-ES, I'll get all of this queued for 6.9 soon-ish.
Quoted 8 lines
As for _why_ I added the quick-and-dirty SEV-ES testcase, I have a series to cleanup
__svm_sev_es_vcpu_run(), and found out that apparently I have a version of OVMF that doesn't quite have to the right <something> for SEV-ES, and so I could even get a "real" VM to reachKVM_RUN. I assumed (correctly, yay!) that hacking together a selftest would be faster than figuring out what firmware magic I am missing.[...]
Applied to kvm-x86 selftests, thanks!
[01/11] KVM: selftests: Extend VM creation's @shape to allow control of VM subtype
https://github.com/kvm-x86/linux/commit/309d1ad7b6ff [02/11] KVM: selftests: Make sparsebit structs const where appropriate https://github.com/kvm-x86/linux/commit/6077c3ce4021 [03/11] KVM: selftests: Add a macro to iterate over a sparsebit range https://github.com/kvm-x86/linux/commit/8811565ff68e [04/11] KVM: selftests: Add support for allocating/managing protected guest memory https://github.com/kvm-x86/linux/commit/29e749e8faff [05/11] KVM: selftests: Add support for protected vm_vaddr_* allocations https://github.com/kvm-x86/linux/commit/1e3af7cf984a [06/11] KVM: selftests: Explicitly ucall pool from shared memory https://github.com/kvm-x86/linux/commit/5ef7196273b6 [07/11] KVM: selftests: Allow tagging protected memory in guest page tables https://github.com/kvm-x86/linux/commit/a8446cd81de8 [08/11] KVM: selftests: Add library for creating and interacting with SEV guests https://github.com/kvm-x86/linux/commit/f3ff1e9b2f9c [09/11] KVM: selftests: Use the SEV library APIs in the intra-host migration test https://github.com/kvm-x86/linux/commit/0837ddb51f9b [10/11] KVM: selftests: Add a basic SEV smoke test https://github.com/kvm-x86/linux/commit/5101f1e27683 [11/11] KVM: selftests: Add a basic SEV-ES smoke test https://github.com/kvm-x86/linux/commit/f3750b0c7f6e
Sean Christopherson
reviewerQuoted 32 lines · Sean Christopherson
Quoted 6 lines · Sean Christopherson
Add basic SEV and SEV-ES smoke tests. Unlike the intra-host migration tests, this one actually runs a small chunk of code in the guest.
Unless anyone strongly objects to the quick and dirty approach I've taken for SEV-ES, I'll get all of this queued for 6.9 soon-ish.
Quoted 8 lines
As for _why_ I added the quick-and-dirty SEV-ES testcase, I have a series to cleanup
__svm_sev_es_vcpu_run(), and found out that apparently I have a version of OVMF that doesn't quite have to the right <something> for SEV-ES, and so I could even get a "real" VM to reachKVM_RUN. I assumed (correctly, yay!) that hacking together a selftest would be faster than figuring out what firmware magic I am missing.[...]
Applied to kvm-x86 selftests, thanks!
[01/11] KVM: selftests: Extend VM creation's @shape to allow control of VM subtype
https://github.com/kvm-x86/linux/commit/309d1ad7b6ff [02/11] KVM: selftests: Make sparsebit structs const where appropriate https://github.com/kvm-x86/linux/commit/6077c3ce4021 [03/11] KVM: selftests: Add a macro to iterate over a sparsebit range https://github.com/kvm-x86/linux/commit/8811565ff68e [04/11] KVM: selftests: Add support for allocating/managing protected guest memory https://github.com/kvm-x86/linux/commit/29e749e8faff [05/11] KVM: selftests: Add support for protected vm_vaddr_* allocations https://github.com/kvm-x86/linux/commit/1e3af7cf984a [06/11] KVM: selftests: Explicitly ucall pool from shared memory https://github.com/kvm-x86/linux/commit/5ef7196273b6 [07/11] KVM: selftests: Allow tagging protected memory in guest page tables https://github.com/kvm-x86/linux/commit/a8446cd81de8
Quoted 8 lines
[08/11] KVM: selftests: Add library for creating and interacting with SEV guests
https://github.com/kvm-x86/linux/commit/f3ff1e9b2f9c [09/11] KVM: selftests: Use the SEV library APIs in the intra-host migration test https://github.com/kvm-x86/linux/commit/0837ddb51f9b [10/11] KVM: selftests: Add a basic SEV smoke test https://github.com/kvm-x86/linux/commit/5101f1e27683 [11/11] KVM: selftests: Add a basic SEV-ES smoke test https://github.com/kvm-x86/linux/commit/f3750b0c7f6e
FYI, the hashes changed due to a force push to squash a bug in a different series.
[1/11] KVM: selftests: Extend VM creation's @shape to allow control of VM subtype
https://github.com/kvm-x86/linux/commit/126190379c57 [2/11] KVM: selftests: Make sparsebit structs const where appropriate https://github.com/kvm-x86/linux/commit/35f50c91c43e [3/11] KVM: selftests: Add a macro to iterate over a sparsebit range https://github.com/kvm-x86/linux/commit/57e19f057758 [4/11] KVM: selftests: Add support for allocating/managing protected guest memory https://github.com/kvm-x86/linux/commit/cd8eb2913205 [5/11] KVM: selftests: Add support for protected vm_vaddr_* allocations https://github.com/kvm-x86/linux/commit/d210eebb51a2 [6/11] KVM: selftests: Explicitly ucall pool from shared memory https://github.com/kvm-x86/linux/commit/31e00dae72fd [7/11] KVM: selftests: Allow tagging protected memory in guest page tables https://github.com/kvm-x86/linux/commit/bf47e87c65be [8/11] KVM: selftests: Add library for creating and interacting with SEV guests https://github.com/kvm-x86/linux/commit/bdceeebcddb8 [9/11] KVM: selftests: Use the SEV library APIs in the intra-host migration test https://github.com/kvm-x86/linux/commit/8b174eb9d289 [10/11] KVM: selftests: Add a basic SEV smoke test https://github.com/kvm-x86/linux/commit/faa0d7027de3 [11/11] KVM: selftests: Add a basic SEV-ES smoke test https://github.com/kvm-x86/linux/commit/974ba6f0e595
Sean Christopherson
reviewerQuoted 17 lines · Sean Christopherson
FYI, the hashes changed due to a force push to squash a bug in a different series.
[1/11] KVM: selftests: Extend VM creation's @shape to allow control of VM subtype
https://github.com/kvm-x86/linux/commit/126190379c57 [2/11] KVM: selftests: Make sparsebit structs const where appropriate https://github.com/kvm-x86/linux/commit/35f50c91c43e [3/11] KVM: selftests: Add a macro to iterate over a sparsebit range https://github.com/kvm-x86/linux/commit/57e19f057758 [4/11] KVM: selftests: Add support for allocating/managing protected guest memory https://github.com/kvm-x86/linux/commit/cd8eb2913205 [5/11] KVM: selftests: Add support for protected vm_vaddr_* allocations https://github.com/kvm-x86/linux/commit/d210eebb51a2 [6/11] KVM: selftests: Explicitly ucall pool from shared memory https://github.com/kvm-x86/linux/commit/31e00dae72fd [7/11] KVM: selftests: Allow tagging protected memory in guest page tables https://github.com/kvm-x86/linux/commit/bf47e87c65be
Quoted 8 lines
[8/11] KVM: selftests: Add library for creating and interacting with SEV guests
https://github.com/kvm-x86/linux/commit/bdceeebcddb8
[9/11] KVM: selftests: Use the SEV library APIs in the intra-host migration testhttps://github.com/kvm-x86/linux/commit/8b174eb9d289 [10/11] KVM: selftests: Add a basic SEV smoke test https://github.com/kvm-x86/linux/commit/faa0d7027de3 [11/11] KVM: selftests: Add a basic SEV-ES smoke test https://github.com/kvm-x86/linux/commit/974ba6f0e595
That didn't last long. I forgot that I needed to change the include guards in kvm_util_arch.h. New-new hashes:
[1/11] KVM: selftests: Extend VM creation's @shape to allow control of VM subtype
https://github.com/kvm-x86/linux/commit/126190379c57 [2/11] KVM: selftests: Make sparsebit structs const where appropriate https://github.com/kvm-x86/linux/commit/35f50c91c43e [3/11] KVM: selftests: Add a macro to iterate over a sparsebit range https://github.com/kvm-x86/linux/commit/57e19f057758 [4/11] KVM: selftests: Add support for allocating/managing protected guest memory https://github.com/kvm-x86/linux/commit/cd8eb2913205 [5/11] KVM: selftests: Add support for protected vm_vaddr_* allocations https://github.com/kvm-x86/linux/commit/d210eebb51a2 [6/11] KVM: selftests: Explicitly ucall pool from shared memory https://github.com/kvm-x86/linux/commit/31e00dae72fd [7/11] KVM: selftests: Allow tagging protected memory in guest page tables https://github.com/kvm-x86/linux/commit/be1bd4c5394f [8/11] KVM: selftests: Add library for creating and interacting with SEV guests https://github.com/kvm-x86/linux/commit/ae20eef5323c [9/11] KVM: selftests: Use the SEV library APIs in the intra-host migration test https://github.com/kvm-x86/linux/commit/69f8e15ab61f [10/11] KVM: selftests: Add a basic SEV smoke test https://github.com/kvm-x86/linux/commit/be250ff437fa [11/11] KVM: selftests: Add a basic SEV-ES smoke test https://github.com/kvm-x86/linux/commit/40e09b3ccfac