TDX KVM selftests
Subsystems (3)
v3 cover TDX KVM selftests
Hello,
This is v3 of the patch series for TDX selftests.
It has been updated for Intel’s V10 of the TDX host patches which was proposed in https://lkml.org/lkml/2022/8/8/877
The tree can be found at
https://github.com/googleprodkernel/linux-cc/tree/tdx-selftests-rfc-v3/
Changes from RFC v2:
Selftest setup now builds upon the KVM selftest framework when setting up the guest for testing. We now use the KVM selftest framework to build the guest page tables and load the ELF binary into guest memory.
Inlining of the entire guest image is no longer required and that allows us to cleanly separate code into different compilation units and be able to use proper assembly instead of inline assembly (addresses Sean’s comment).
To achieve this, we take a dependency on the SEV VM tests:
https://lore.kernel.org/lkml/[email protected]/T/. Those patches provide functions for the host to allocate and track protected memory in the guest.
In RFCv3, TDX selftest code is organized into:
+ headers in tools/testing/selftests/kvm/include/x86_64/tdx/
+ common code in tools/testing/selftests/kvm/lib/x86_64/tdx/
+ selftests in tools/testing/selftests/kvm/x86_64/tdx_*
RFCv3 also adds additional selftests for UPM.
Dependencies
+ Peter’s patches, which provide functions for the host to allocate
and track protected memory in the
guest. https://lore.kernel.org/lkml/[email protected]/T/
+ Peter’s patches depend on Sean’s patches:
+ https://lore.kernel.org/linux-arm-kernel/[email protected]/T/
+ https://lore.kernel.org/lkml/[email protected]/T/
+ Proposed fixes for these these issues mentioned on the mailing list
+ https://lore.kernel.org/lkml/[email protected]/
+ https://lore.kernel.org/lkml/[email protected]/
+ https://lore.kernel.org/lkml/[email protected]/
+ https://lore.kernel.org/lkml/[email protected]/
+ https://lore.kernel.org/linux-mm/[email protected]/
Further work for this patch series/TODOs
+ Sean’s comments for the non-confidential UPM selftests patch series at https://lore.kernel.org/lkml/[email protected]/T/#u apply here as well
+ Add ucall support for TDX selftests
I would also like to acknowledge the following people, who helped review or test patches in RFCv1 and RFCv2:
+ Sean Christopherson <[email protected]>
+ Zhenzhong Duan <[email protected]>
+ Peter Gonda <[email protected]>
+ Andrew Jones <[email protected]>
+ Maxim Levitsky <[email protected]>
+ Xiaoyao Li <[email protected]>
+ David Matlack <[email protected]>
+ Marc Orr <[email protected]>
+ Isaku Yamahata <[email protected]>
Links to earlier patch series
+ RFC v1: https://lore.kernel.org/lkml/[email protected]/T/#u
+ RFC v2: https://lore.kernel.org/lkml/[email protected]/T/#u
Ackerley Tng (14):
KVM: selftests: Add function to allow one-to-one GVA to GPA mappings
KVM: selftests: Expose function that sets up sregs based on VM's mode
KVM: selftests: Store initial stack address in struct kvm_vcpu
KVM: selftests: Refactor steps in vCPU descriptor table initialization
KVM: selftests: TDX: Use KVM_TDX_CAPABILITIES to validate TDs'
attribute configuration
KVM: selftests: Require GCC to realign stacks on function entry
KVM: selftests: Add functions to allow mapping as shared
KVM: selftests: Add support for restricted memory
KVM: selftests: TDX: Update load_td_memory_region for VM memory backed
by restricted memfd
KVM: selftests: Expose _vm_vaddr_alloc
KVM: selftests: TDX: Add support for TDG.MEM.PAGE.ACCEPT
KVM: selftests: TDX: Add support for TDG.VP.VEINFO.GET
KVM: selftests: TDX: Add TDX UPM selftest
KVM: selftests: TDX: Add TDX UPM selftests for implicit conversion
Erdem Aktas (4):
KVM: selftests: Add support for creating non-default type VMs
KVM: selftests: Add helper functions to create TDX VMs
KVM: selftests: TDX: Add TDX lifecycle test
KVM: selftests: TDX: Adding test case for TDX port IO
Roger Wang (1):
KVM: selftests: TDX: Add TDG.VP.INFO test
Ryan Afranji (2):
KVM: selftests: TDX: Verify the behavior when host consumes a TD
private memory
KVM: selftests: TDX: Add shared memory test
Sagi Shahar (10):
KVM: selftests: TDX: Add report_fatal_error test
KVM: selftests: TDX: Add basic TDX CPUID test
KVM: selftests: TDX: Add basic get_td_vmcall_info test
KVM: selftests: TDX: Add TDX IO writes test
KVM: selftests: TDX: Add TDX IO reads test
KVM: selftests: TDX: Add TDX MSR read/write tests
KVM: selftests: TDX: Add TDX HLT exit test
KVM: selftests: TDX: Add TDX MMIO reads test
KVM: selftests: TDX: Add TDX MMIO writes test
KVM: selftests: TDX: Add TDX CPUID TDVMCALL test
tools/testing/selftests/kvm/.gitignore | 3 + tools/testing/selftests/kvm/Makefile | 10 +- .../selftests/kvm/include/kvm_util_base.h | 43 +- .../testing/selftests/kvm/include/test_util.h | 2 + .../selftests/kvm/include/x86_64/processor.h | 4 + .../kvm/include/x86_64/tdx/td_boot.h | 82 + .../kvm/include/x86_64/tdx/td_boot_asm.h | 16 + .../selftests/kvm/include/x86_64/tdx/tdcall.h | 59 + .../selftests/kvm/include/x86_64/tdx/tdx.h | 65 + .../kvm/include/x86_64/tdx/tdx_util.h | 19 + .../kvm/include/x86_64/tdx/test_util.h | 164 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 123 +- tools/testing/selftests/kvm/lib/test_util.c | 7 + .../selftests/kvm/lib/x86_64/processor.c | 77 +- tools/testing/selftests/kvm/lib/x86_64/sev.c | 2 +- .../selftests/kvm/lib/x86_64/tdx/td_boot.S | 101 ++ .../selftests/kvm/lib/x86_64/tdx/tdcall.S | 158 ++ .../selftests/kvm/lib/x86_64/tdx/tdx.c | 231 +++ .../selftests/kvm/lib/x86_64/tdx/tdx_util.c | 562 +++++++ .../selftests/kvm/lib/x86_64/tdx/test_util.c | 101 ++ .../kvm/x86_64/tdx_shared_mem_test.c | 137 ++ .../selftests/kvm/x86_64/tdx_upm_test.c | 460 ++++++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 1329 +++++++++++++++++ 23 files changed, 3709 insertions(+), 46 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/x86_64/tdx/td_boot.h create mode 100644 tools/testing/selftests/kvm/include/x86_64/tdx/td_boot_asm.h create mode 100644 tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h create mode 100644 tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h create mode 100644 tools/testing/selftests/kvm/include/x86_64/tdx/tdx_util.h create mode 100644 tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h create mode 100644 tools/testing/selftests/kvm/lib/x86_64/tdx/td_boot.S create mode 100644 tools/testing/selftests/kvm/lib/x86_64/tdx/tdcall.S create mode 100644 tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c create mode 100644 tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c create mode 100644 tools/testing/selftests/kvm/lib/x86_64/tdx/test_util.c create mode 100644 tools/testing/selftests/kvm/x86_64/tdx_shared_mem_test.c create mode 100644 tools/testing/selftests/kvm/x86_64/tdx_upm_test.c create mode 100644 tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c
--
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 1/31 KVM: selftests: Add function to allow one-to-one GVA to GPA mappings
One-to-one GVA to GPA mappings can be used in the guest to set up boot sequences during which paging is enabled, hence requiring a transition from using physical to virtual addresses in consecutive instructions.
--- .../selftests/kvm/include/kvm_util_base.h | 2 ++ tools/testing/selftests/kvm/lib/kvm_util.c | 35 ++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +2 -0
@@ -402,6 +402,8 @@ void vm_mem_region_delete(struct kvm_vm *vm, uint32_t slot);struct kvm_vcpu *__vm_vcpu_add (struct kvm_vm *vm,uint32_t vcpu_id);vm_vaddr_t vm_vaddr_alloc (struct kvm_vm *vm,size_t sz,vm_vaddr_t vaddr_min);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_1to1 (struct kvm_vm *vm,size_t sz,vm_vaddr_t vaddr_min, +uint32_t data_memslot);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);
tools/testing/selftests/kvm/lib/kvm_util.c +30 -5
@@ -1237,6 +1237,8 @@ static vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz,* vm - Virtual Machine * sz - Size in bytes * vaddr_min - Minimum starting virtual address +* paddr_min - Minimum starting physical address +* data_memslot - memslot number to allocate in * encrypt - Whether the region should be handled as encrypted * * Output Args: None
@@ -1251,14 +1253,15 @@ static vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz,* a page. */ static vm_vaddr_t -_vm_vaddr_alloc (struct kvm_vm *vm,size_t sz,vm_vaddr_t vaddr_min,bool encrypt) +_vm_vaddr_alloc (struct kvm_vm *vm,size_t sz,vm_vaddr_t vaddr_min, +vm_paddr_t paddr_min,uint32_t data_memslot,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, -KVM_UTIL_MIN_PFN * vm->page_size, -0 , encrypt); + paddr_min, + data_memslot, encrypt);/* * Find an unused range of virtual page addresses of at least
@@ -1281,12 +1284,34 @@ _vm_vaddr_alloc(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min, bool encryptvm_vaddr_t vm_vaddr_alloc (struct kvm_vm *vm,size_t sz,vm_vaddr_t vaddr_min) { -return _vm_vaddr_alloc (vm, sz, vaddr_min, vm->protected); +return _vm_vaddr_alloc (vm, sz, vaddr_min, +KVM_UTIL_MIN_PFN * vm->page_size,0 , + 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, false); +return _vm_vaddr_alloc (vm, sz, vaddr_min, +KVM_UTIL_MIN_PFN * vm->page_size,0 , false); +} + +/** +* Allocate memory in @vm of size @sz in memslot with id @data_memslot, +* beginning with the desired address of @vaddr_min. +* +* If there isn't enough memory at @vaddr_min, find the next possible address +* that can meet the requested size in the given memslot. +* +* Return the address where the memory is allocated. +*/ +vm_vaddr_t vm_vaddr_alloc_1to1 (struct kvm_vm *vm,size_t sz,vm_vaddr_t vaddr_min, +uint32_t data_memslot) +{ +vm_vaddr_t gva =_vm_vaddr_alloc (vm, sz, vaddr_min, (vm_paddr_t ) vaddr_min, + data_memslot, vm->protected); +ASSERT_EQ (gva,addr_gva2gpa (vm, gva)); + +return gva; }/*
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 2/31 KVM: selftests: Add support for creating non-default type VMs
In reply to Ackerley TngFrom: Erdem Aktas <[email protected]>
Currently vm_create function only creates KVM_VM_TYPE_DEFAULT type VMs. Adding type parameter to ____vm_create to create new VM types.
--- tools/testing/selftests/kvm/include/kvm_util_base.h | 6 ++++-- tools/testing/selftests/kvm/lib/kvm_util.c | 6 +++--- tools/testing/selftests/kvm/lib/x86_64/sev.c | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +4 -2
@@ -27,6 +27,8 @@#define NSEC_PER_SEC 1000000000L +#define KVM_VM_TYPE_DEFAULT 0 +typedef uint64_t vm_paddr_t ;/* Virtual Machine (Guest) physical address */ typedef uint64_t vm_vaddr_t ;/* Virtual Machine (Guest) virtual address */
@@ -686,13 +688,13 @@ uint64_t vm_nr_pages_required(enum vm_guest_mode mode,* __vm_create() does NOT create vCPUs, @nr_runnable_vcpus is used purely to * calculate the amount of memory needed for per-vCPU data, e.g. stacks. */ -struct kvm_vm *____vm_create (enum vm_guest_mode mode,uint64_t nr_pages); +struct kvm_vm *____vm_create (enum vm_guest_mode mode,uint64_t nr_pages,int type);struct kvm_vm *__vm_create (enum vm_guest_mode mode,uint32_t nr_runnable_vcpus,uint64_t nr_extra_pages);static inline struct kvm_vm *vm_create_barebones (void ) { -return ____vm_create (VM_MODE_DEFAULT ,0 ); +return ____vm_create (VM_MODE_DEFAULT ,0 ,KVM_VM_TYPE_DEFAULT ); }static inline struct kvm_vm *vm_create (uint32_t nr_runnable_vcpus)
tools/testing/selftests/kvm/lib/kvm_util.c +3 -3
@@ -185,7 +185,7 @@ const struct vm_guest_mode_params vm_guest_mode_params[] = {_Static_assert (sizeof (vm_guest_mode_params)/sizeof (struct vm_guest_mode_params ) ==NUM_VM_MODES ,"Missing new mode params?" ); -struct kvm_vm *____vm_create (enum vm_guest_mode mode,uint64_t nr_pages) +struct kvm_vm *____vm_create (enum vm_guest_mode mode,uint64_t nr_pages,int type) {struct kvm_vm *vm;
@@ -201,7 +201,7 @@ struct kvm_vm *____vm_create(enum vm_guest_mode mode, uint64_t nr_pages)hash_init (vm->regions.slot_hash); vm->mode = mode; - vm->type =0 ; + vm->type = type; vm->pa_bits = vm_guest_mode_params[mode].pa_bits; vm->va_bits = vm_guest_mode_params[mode].va_bits;
@@ -337,7 +337,7 @@ struct kvm_vm *__vm_create(enum vm_guest_mode mode, uint32_t nr_runnable_vcpus,struct userspace_mem_region *slot0;struct kvm_vm *vm; - vm =____vm_create (mode, nr_pages); + vm =____vm_create (mode, nr_pages,KVM_VM_TYPE_DEFAULT );kvm_vm_elf_load (vm, program_invocation_name);
tools/testing/selftests/kvm/lib/x86_64/sev.c +1 -1
@@ -221,7 +221,7 @@ struct kvm_vm *vm_sev_create_with_one_vcpu(uint32_t policy, void *guest_code,uint64_t nr_pages =vm_nr_pages_required (mode,1 ,0 );struct kvm_vm *vm; - vm =____vm_create (mode, nr_pages); + vm =____vm_create (mode, nr_pages,KVM_VM_TYPE_DEFAULT );kvm_sev_ioctl (vm,KVM_SEV_INIT ,NULL );
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 3/31 KVM: selftests: Expose function that sets up sregs based on VM's mode
In reply to Ackerley TngThis allows initializing sregs without setting vCPU registers in KVM.
No functional change intended.
--- .../selftests/kvm/include/x86_64/processor.h | 2 + .../selftests/kvm/lib/x86_64/processor.c | 39 ++++++++++--------- 2 files changed, 23 insertions(+), 18 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +2 -0
@@ -644,6 +644,8 @@ const struct kvm_cpuid_entry2 *get_cpuid_entry(const struct kvm_cpuid2 *cpuid,void vcpu_init_cpuid (struct kvm_vcpu *vcpu,const struct kvm_cpuid2 *cpuid);void vcpu_set_hv_cpuid (struct kvm_vcpu *vcpu); +void vcpu_setup_mode_sregs (struct kvm_vm *vm,struct kvm_sregs *sregs); +static inline struct kvm_cpuid_entry2 *__vcpu_get_cpuid_entry (struct kvm_vcpu *vcpu,uint32_t function,uint32_t index)
tools/testing/selftests/kvm/lib/x86_64/processor.c +21 -18
@@ -589,35 +589,38 @@ static void kvm_setup_tss_64bit(struct kvm_vm *vm, struct kvm_segment *segp,kvm_seg_fill_gdt_64bit (vm, segp); } -static void vcpu_setup (struct kvm_vm *vm,struct kvm_vcpu *vcpu) +void vcpu_setup_mode_sregs (struct kvm_vm *vm,struct kvm_sregs *sregs) { -struct kvm_sregs sregs; - -/* Set mode specific system register values. */ -vcpu_sregs_get (vcpu, &sregs); - - sregs.idt.limit =0 ; + sregs->idt.limit =0 ; -kvm_setup_gdt (vm, &sregs.gdt); +kvm_setup_gdt (vm, &sregs->gdt);switch (vm->mode) {case VM_MODE_PXXV48_4K : - sregs.cr0 =X86_CR0_PE |X86_CR0_NE |X86_CR0_PG ; - sregs.cr4 |=X86_CR4_PAE |X86_CR4_OSFXSR ; - sregs.efer |= (EFER_LME |EFER_LMA |EFER_NX ); - -kvm_seg_set_unusable (&sregs.ldt); -kvm_seg_set_kernel_code_64bit (vm,DEFAULT_CODE_SELECTOR , &sregs.cs); -kvm_seg_set_kernel_data_64bit (vm,DEFAULT_DATA_SELECTOR , &sregs.ds); -kvm_seg_set_kernel_data_64bit (vm,DEFAULT_DATA_SELECTOR , &sregs.es); -kvm_setup_tss_64bit (vm, &sregs.tr,0x18 ); + sregs->cr0 =X86_CR0_PE |X86_CR0_NE |X86_CR0_PG ; + sregs->cr4 |=X86_CR4_PAE |X86_CR4_OSFXSR ; + sregs->efer |= (EFER_LME |EFER_LMA |EFER_NX ); + +kvm_seg_set_unusable (&sregs->ldt); +kvm_seg_set_kernel_code_64bit (vm,DEFAULT_CODE_SELECTOR , &sregs->cs); +kvm_seg_set_kernel_data_64bit (vm,DEFAULT_DATA_SELECTOR , &sregs->ds); +kvm_seg_set_kernel_data_64bit (vm,DEFAULT_DATA_SELECTOR , &sregs->es); +kvm_setup_tss_64bit (vm, &sregs->tr,0x18 );break ;default :TEST_FAIL ("Unknown guest mode, mode: 0x%x" , vm->mode); } - sregs.cr3 = vm->pgd; + sregs->cr3 = vm->pgd; +} + +static void vcpu_setup (struct kvm_vm *vm,struct kvm_vcpu *vcpu) +{ +struct kvm_sregs sregs; + +vcpu_sregs_get (vcpu, &sregs); +vcpu_setup_mode_sregs (vm, &sregs);vcpu_sregs_set (vcpu, &sregs); }
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 4/31 KVM: selftests: Store initial stack address in struct kvm_vcpu
In reply to Ackerley TngTDX guests' registers cannot be initialized directly using vcpu_regs_set(), hence the stack pointer needs to be initialized by the guest itself, running boot code beginning at the reset vector.
We store the stack address as part of struct kvm_vcpu so that it can be accessible later to be passed to the boot code for rsp initialization.
--- tools/testing/selftests/kvm/include/kvm_util_base.h | 1 + tools/testing/selftests/kvm/lib/x86_64/processor.c | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +1 -0
@@ -54,6 +54,7 @@ struct kvm_vcpu {int fd;struct kvm_vm *vm;struct kvm_run *run; +vm_vaddr_t initial_stack_addr;#ifdef __x86_64__struct kvm_cpuid2 *cpuid;#endif
tools/testing/selftests/kvm/lib/x86_64/processor.c +3 -1
@@ -673,10 +673,12 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,vcpu_init_cpuid (vcpu,kvm_get_supported_cpuid ());vcpu_setup (vm, vcpu); + vcpu->initial_stack_addr = stack_vaddr + (DEFAULT_STACK_PGS *getpagesize ()); +/* Setup guest general purpose registers */ vcpu_regs_get (vcpu, ®s); regs.rflags = regs.rflags |0x2 ; - regs.rsp = stack_vaddr + (DEFAULT_STACK_PGS *getpagesize ()); + regs.rsp = vcpu->initial_stack_addr; regs.rip = (unsigned long ) guest_code;vcpu_regs_set (vcpu, ®s);
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 5/31 KVM: selftests: Refactor steps in vCPU descriptor table initialization
In reply to Ackerley TngSplit the vCPU descriptor table initialization process into a few steps and expose them:
+ Setting up the IDT
+ Syncing exception handlers into the guest
In kvm_setup_idt(), we conditionally allocate guest memory for vm->idt to avoid double allocation when kvm_setup_idt() is used after vm_init_descriptor_tables().
--- .../selftests/kvm/include/x86_64/processor.h | 2 ++ .../selftests/kvm/lib/x86_64/processor.c | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/include/x86_64/processor.h +2 -0
@@ -750,6 +750,8 @@ struct ex_regs {uint64_t rflags; }; +void kvm_setup_idt (struct kvm_vm *vm,struct kvm_dtable *dt); +void sync_exception_handlers_to_guest (struct kvm_vm *vm);void vm_init_descriptor_tables (struct kvm_vm *vm);void vcpu_init_descriptor_tables (struct kvm_vcpu *vcpu); void vm_install_exception_handler(struct kvm_vm *vm, int vector,
tools/testing/selftests/kvm/lib/x86_64/processor.c +16 -3
@@ -1174,19 +1174,32 @@ void vm_init_descriptor_tables(struct kvm_vm *vm) DEFAULT_CODE_SELECTOR); } +void kvm_setup_idt (struct kvm_vm *vm,struct kvm_dtable *dt) +{ +if (!vm->idt) + vm->idt =vm_vaddr_alloc_page (vm); + + dt->base = vm->idt; + dt->limit =NUM_INTERRUPTS *sizeof (struct idt_entry ) -1 ; +} + +void sync_exception_handlers_to_guest (struct kvm_vm *vm) +{ + *(vm_vaddr_t *)addr_gva2hva (vm, (vm_vaddr_t)(&exception_handlers)) = vm->handlers; +} +void vcpu_init_descriptor_tables (struct kvm_vcpu *vcpu) {struct kvm_vm *vm = vcpu->vm;struct kvm_sregs sregs;vcpu_sregs_get (vcpu, &sregs); - sregs.idt.base = vm->idt; - sregs.idt.limit =NUM_INTERRUPTS *sizeof (struct idt_entry ) -1 ; +kvm_setup_idt (vcpu->vm, &sregs.idt); sregs.gdt.base = vm->gdt; sregs.gdt.limit =getpagesize () -1 ;kvm_seg_set_kernel_data_64bit (NULL ,DEFAULT_DATA_SELECTOR , &sregs.gs);vcpu_sregs_set (vcpu, &sregs); - *(vm_vaddr_t *)addr_gva2hva (vm, (vm_vaddr_t)(&exception_handlers)) = vm->handlers; +sync_exception_handlers_to_guest (vm); } void vm_install_exception_handler(struct kvm_vm *vm, int vector,
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 6/31 KVM: selftests: Add helper functions to create TDX VMs
In reply to Ackerley TngFrom: Erdem Aktas <[email protected]>
TDX requires additional IOCTLs to initialize VM and vCPUs to add private memory and to finalize the VM memory. Also additional utility functions are provided to manipulate a TD, similar to those that manipulate a VM in the current selftest framework.
A TD's initial register state cannot be manipulated directly by setting the VM's memory, hence boot code is provided at the TD's reset vector. This boot code takes boot parameters loaded in the TD's memory and sets up the TD for the selftest.
---
Changes RFCv2 -> RFCv3
+ Refactored out required cpuid changes as tdx_apply_cpuid_restrictions (a more meaningful function)
+ Also fixed logic for setting LBR and CET features to use sub-leaf 1 instead of 0 previously
+ Renamed ioctl calls to match names used in the kernel for easy referencing
--- tools/testing/selftests/kvm/Makefile | 2 + .../kvm/include/x86_64/tdx/td_boot.h | 82 ++++ .../kvm/include/x86_64/tdx/td_boot_asm.h | 16 + .../kvm/include/x86_64/tdx/tdx_util.h | 16 + .../selftests/kvm/lib/x86_64/tdx/td_boot.S | 101 ++++ .../selftests/kvm/lib/x86_64/tdx/tdx_util.c | 438 ++++++++++++++++++ 6 files changed, 655 insertions(+) create mode 100644 tools/testing/selftests/kvm/include/x86_64/tdx/td_boot.h create mode 100644 tools/testing/selftests/kvm/include/x86_64/tdx/td_boot_asm.h create mode 100644 tools/testing/selftests/kvm/include/x86_64/tdx/tdx_util.h create mode 100644 tools/testing/selftests/kvm/lib/x86_64/tdx/td_boot.S create mode 100644 tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c
tools/testing/selftests/kvm/Makefile +2 -0
@@ -59,6 +59,8 @@ 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_x86_64 += lib/x86_64/tdx/tdx_util.c +LIBKVM_x86_64 += lib/x86_64/tdx/td_boot.S LIBKVM_aarch64 += lib/aarch64/gic.c LIBKVM_aarch64 += lib/aarch64/gic_v3.c
tools/testing/selftests/kvm/include/x86_64/tdx/td_boot.h new file +82 -0
82 lines not on this page · raw message
tools/testing/selftests/kvm/include/x86_64/tdx/td_boot_asm.h new file +16 -0
@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SELFTEST_TDX_TD_BOOT_ASM_H +#define SELFTEST_TDX_TD_BOOT_ASM_H + +/* +* GPA where TD boot parameters wil lbe loaded. +* +* TD_BOOT_PARAMETERS_GPA is arbitrarily chosen to +* +* + be within the 4GB address space +* + provide enough contiguous memory for the struct td_boot_parameters such +* that there is one struct td_per_vcpu_parameters for KVM_MAX_VCPUS +*/ +#define TD_BOOT_PARAMETERS_GPA 0xffff0000 + +#endif // SELFTEST_TDX_TD_BOOT_ASM_H
tools/testing/selftests/kvm/include/x86_64/tdx/tdx_util.h new file +16 -0
@@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SELFTESTS_TDX_KVM_UTIL_H +#define SELFTESTS_TDX_KVM_UTIL_H + +#include <stdint.h> + +#include "kvm_util_base.h" + +struct kvm_vcpu *td_vcpu_add (struct kvm_vm *vm,uint32_t vcpu_id,void *guest_code); + +struct kvm_vm *td_create (void ); +void td_initialize (struct kvm_vm *vm,enum vm_mem_backing_src_type src_type, +uint64_t attributes); +void td_finalize (struct kvm_vm *vm); + +#endif // SELFTESTS_TDX_KVM_UTIL_H
tools/testing/selftests/kvm/lib/x86_64/tdx/td_boot.S new file +101 -0
101 lines not on this page · raw message
tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c new file +438 -0
438 lines not on this page · raw message
--
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 7/31 KVM: selftests: TDX: Use KVM_TDX_CAPABILITIES to validate TDs' attribute configuration
In reply to Ackerley TngThis also exercises the KVM_TDX_CAPABILITIES ioctl.
--- .../selftests/kvm/lib/x86_64/tdx/tdx_util.c | 75 ++++++++++++++++++- 1 file changed, 72 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c +72 -3
@@ -27,10 +27,9 @@ static char *tdx_cmd_str[] = { };#define TDX_MAX_CMD_STR (ARRAY_SIZE(tdx_cmd_str)) -static void tdx_ioctl (int fd,int ioctl_no,uint32_t flags,void *data) +static int _tdx_ioctl (int fd,int ioctl_no,uint32_t flags,void *data) {struct kvm_tdx_cmd tdx_cmd; -int r;TEST_ASSERT (ioctl_no <TDX_MAX_CMD_STR ,"Unknown TDX CMD : %d\n" , ioctl_no);
@@ -40,11 +39,63 @@ static void tdx_ioctl(int fd, int ioctl_no, uint32_t flags, void *data) tdx_cmd.flags = flags; tdx_cmd.data = (uint64_t )data; - r =ioctl (fd,KVM_MEMORY_ENCRYPT_OP , &tdx_cmd); +return ioctl (fd,KVM_MEMORY_ENCRYPT_OP , &tdx_cmd); +} + +static void tdx_ioctl (int fd,int ioctl_no,uint32_t flags,void *data) +{ +int r; + + r =_tdx_ioctl (fd, ioctl_no, flags, data);TEST_ASSERT (r ==0 ,"%s failed: %d %d" , tdx_cmd_str[ioctl_no], r, errno); } +static struct kvm_tdx_capabilities *tdx_read_capabilities (void ) +{ +int i; +int rc =-1 ; +int nr_cpuid_configs =4 ; +struct kvm_tdx_capabilities *tdx_cap =NULL ; +int kvm_fd; + + kvm_fd =open_kvm_dev_path_or_exit (); + +do { + nr_cpuid_configs *=2 ; + + tdx_cap =realloc ( + tdx_cap,sizeof (*tdx_cap) + + nr_cpuid_configs *sizeof (*tdx_cap->cpuid_configs)); +TEST_ASSERT (tdx_cap !=NULL , +"Could not allocate memory for tdx capability nr_cpuid_configs %d\n" , + nr_cpuid_configs); + + tdx_cap->nr_cpuid_configs = nr_cpuid_configs; + rc =_tdx_ioctl (kvm_fd,KVM_TDX_CAPABILITIES ,0 , tdx_cap); + }while (rc <0 && errno ==E2BIG ); + +TEST_ASSERT (rc ==0 ,"KVM_TDX_CAPABILITIES failed: %d %d" , + rc, errno); + +pr_debug ("tdx_cap: attrs: fixed0 0x%016llx fixed1 0x%016llx\n" +"tdx_cap: xfam fixed0 0x%016llx fixed1 0x%016llx\n" , + tdx_cap->attrs_fixed0, tdx_cap->attrs_fixed1, + tdx_cap->xfam_fixed0, tdx_cap->xfam_fixed1); + +for (i =0 ; i < tdx_cap->nr_cpuid_configs; i++) { +const struct kvm_tdx_cpuid_config *config = + &tdx_cap->cpuid_configs[i]; +pr_debug ("cpuid config[%d]: leaf 0x%x sub_leaf 0x%x eax 0x%08x ebx 0x%08x ecx 0x%08x edx 0x%08x\n" , + i, config->leaf, config->sub_leaf, + config->eax, config->ebx, config->ecx, config->edx); + } + +close (kvm_fd); + +return tdx_cap; +} +#define XFEATURE_LBR 15#define XFEATURE_XTILECFG 17#define XFEATURE_XTILEDATA 18
@@ -90,6 +141,21 @@ static void tdx_apply_cpuid_restrictions(struct kvm_cpuid2 *cpuid_data) } } +static void tdx_check_attributes (uint64_t attributes) +{ +struct kvm_tdx_capabilities *tdx_cap; + + tdx_cap =tdx_read_capabilities (); + +/* TDX spec: any bits 0 in attrs_fixed0 must be 0 in attributes */ +ASSERT_EQ (attributes & ~tdx_cap->attrs_fixed0,0 ); + +/* TDX spec: any bits 1 in attrs_fixed1 must be 1 in attributes */ +ASSERT_EQ (attributes & tdx_cap->attrs_fixed1, tdx_cap->attrs_fixed1); + +free (tdx_cap); +} +static void tdx_td_init (struct kvm_vm *vm,uint64_t attributes) {const struct kvm_cpuid2 *cpuid;
@@ -100,6 +166,9 @@ static void tdx_td_init(struct kvm_vm *vm, uint64_t attributes) cpuid =kvm_get_supported_cpuid ();memcpy (&init_vm.cpuid, cpuid,kvm_cpuid2_size (cpuid->nent)); + +tdx_check_attributes (attributes); + init_vm.attributes = attributes;tdx_apply_cpuid_restrictions (&init_vm.cpuid);
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 8/31 KVM: selftests: Require GCC to realign stacks on function entry
In reply to Ackerley TngSome SSE instructions assume a 16-byte aligned stack, and GCC compiles assuming the stack is aligned:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838. This combination results in a #GP in guests.
Adding this compiler flag will generate an alternate prologue and epilogue to realign the runtime stack, which makes selftest code slower and bigger, but this is okay since we do not need selftest code to be extremely performant.
Similar issue discussed at
https://lore.kernel.org/all/CAGtprH9yKvuaF5yruh3BupQe4BxDGiBQk3ExtY2m39yP-tppsg@mail.gmail.com/
--- tools/testing/selftests/kvm/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kvm/Makefile +1 -1
@@ -205,7 +205,7 @@ LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/x86/include else LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/$(ARCH)/include endif -CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \ +CFLAGS += -mstackrealign -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/$(UNAME_M) -I ../rseq -I.. $(EXTRA_CFLAGS) \
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
Sean Christopherson
reviewerRe: [RFC PATCH v3 08/31] KVM: selftests: Require GCC to realign stacks on function entry
Quoted 9 lines · Ackerley Tng
Some SSE instructions assume a 16-byte aligned stack, and GCC compiles assuming the stack is aligned:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838. This combination results in a #GP in guests.Adding this compiler flag will generate an alternate prologue and epilogue to realign the runtime stack, which makes selftest code slower and bigger, but this is okay since we do not need selftest code to be extremely performant.
Huh, I had completely forgotten that this is why SSE is problematic. I ran into this with the base UPM selftests and just disabled SSE. /facepalm.
We should figure out exactly what is causing a misaligned stack. As you've noted, the x86-64 ABI requires a 16-byte aligned RSP. Unless I'm misreading vm_arch_vcpu_add(), the starting stack should be page aligned, which means something is causing the stack to become unaligned at runtime. I'd rather hunt down that something than paper over it by having the compiler force realignment.
Quoted 24 lines
Similar issue discussed at
https://lore.kernel.org/all/CAGtprH9yKvuaF5yruh3BupQe4BxDGiBQk3ExtY2m39yP-tppsg@mail.gmail.com/Signed-off-by Ackerley Tng <[email protected]>--- tools/testing/selftests/kvm/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/kvm/Makefile +1
-1@@ -205,7 +205,7 @@ LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/x86/include else LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/$(ARCH)/include endif -CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \ +CFLAGS += -mstackrealign -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/$(UNAME_M) -I ../rseq -I.. $(EXTRA_CFLAGS) \Signature · 1 line
2.39.0.246.g2a6d74b583-goog
Re: [RFC PATCH v3 08/31] KVM: selftests: Require GCC to realign stacks on function entry
Quoted 12 lines · Sean Christopherson
Quoted 9 lines · Ackerley Tng
Some SSE instructions assume a 16-byte aligned stack, and GCC compiles assuming the stack is aligned:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838. This combination results in a #GP in guests.Adding this compiler flag will generate an alternate prologue and epilogue to realign the runtime stack, which makes selftest code slower and bigger, but this is okay since we do not need selftest code to be extremely performant.
Quoted 8 lines
Huh, I had completely forgotten that this is why SSE is problematic. I ran into this with the base UPM selftests and just disabled SSE. /facepalm.
We should figure out exactly what is causing a misaligned stack. As you've noted, the x86-64 ABI requires a 16-byte aligned RSP. Unless I'm misreading
vm_arch_vcpu_add(), the starting stack should be page aligned, which means something is causing the stack to become unaligned at runtime. I'd rather hunt down that something than paper over it by having the compiler force realignment.
Is not it due to the 32bit execution part of the guest code at boot time. Any push/pop of 32bit registers might make it a 16-byte unaligned stack.
Quoted 25 lines
Quoted 25 lines
Similar issue discussed at
https://lore.kernel.org/all/CAGtprH9yKvuaF5yruh3BupQe4BxDGiBQk3ExtY2m39yP-tppsg@mail.gmail.com/Signed-off-by Ackerley Tng <[email protected]>--- tools/testing/selftests/kvm/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/testing/selftests/kvm/Makefile +1
-2@@ -205,7 +205,7 @@ LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/x86/include else LINUX_TOOL_ARCH_INCLUDE = $(top_srcdir)/tools/arch/$(ARCH)/include endif -CFLAGS += -Wall -Wstrict-prototypes -Wuninitialized -O2 -g -std=gnu99 \ +CFLAGS += -mstackrealign -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/$(UNAME_M) -I ../rseq -I.. $(EXTRA_CFLAGS) \ --2.39.0.246.g2a6d74b583-goog
Re: [RFC PATCH v3 08/31] KVM: selftests: Require GCC to realign stacks on function entry
Quoted 22 lines · Erdem Aktas
Quoted 12 lines · Sean Christopherson
Quoted 9 lines · Ackerley Tng
Some SSE instructions assume a 16-byte aligned stack, and GCC compiles assuming the stack is aligned:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838. This combination results in a #GP in guests.Adding this compiler flag will generate an alternate prologue and epilogue to realign the runtime stack, which makes selftest code slower and bigger, but this is okay since we do not need selftest code to be extremely performant.
Quoted 8 lines
Huh, I had completely forgotten that this is why SSE is problematic. I ran into this with the base UPM selftests and just disabled SSE. /facepalm.
We should figure out exactly what is causing a misaligned stack. As you've noted, the x86-64 ABI requires a 16-byte aligned RSP. Unless I'm misreading
vm_arch_vcpu_add(), the starting stack should be page aligned, which means something is causing the stack to become unaligned at runtime. I'd rather hunt down that something than paper over it by having the compiler force realignment.
Quoted 3 lines
Is not it due to the 32bit execution part of the guest code at boot time. Any push/pop of 32bit registers might make it a 16-byte unaligned stack.
32-bit stack needs to be 16-byte aligned, too (at function call boundaries) - see [1] chapter 2.2.2 "The Stack Frame"
Thanks,
Maciej
Sean Christopherson
reviewerRe: [RFC PATCH v3 08/31] KVM: selftests: Require GCC to realign stacks on function entry
Quoted 23 lines · Maciej S. Szmigiero
Quoted 22 lines · Erdem Aktas
Quoted 20 lines · Sean Christopherson
Quoted 9 lines · Ackerley Tng
Some SSE instructions assume a 16-byte aligned stack, and GCC compiles assuming the stack is aligned:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838. This combination results in a #GP in guests.Adding this compiler flag will generate an alternate prologue and epilogue to realign the runtime stack, which makes selftest code slower and bigger, but this is okay since we do not need selftest code to be extremely performant.
Huh, I had completely forgotten that this is why SSE is problematic. I ran into this with the base UPM selftests and just disabled SSE. /facepalm.
We should figure out exactly what is causing a misaligned stack. As you've noted, the x86-64 ABI requires a 16-byte aligned RSP. Unless I'm misreading
vm_arch_vcpu_add(), the starting stack should be page aligned, which means something is causing the stack to become unaligned at runtime. I'd rather hunt down that something than paper over it by having the compiler force realignment.
Quoted 6 lines
Quoted 3 lines
Is not it due to the 32bit execution part of the guest code at boot time. Any push/pop of 32bit registers might make it a 16-byte unaligned stack.
32-bit stack needs to be 16-byte aligned, too (at function call boundaries) - see [1] chapter 2.2.2 "The Stack Frame"
And this showing up in the non-TDX selftests rules that out as the sole problem; the selftests stuff 64-bit mode, i.e. don't have 32-bit boot code.
Re: [RFC PATCH v3 08/31] KVM: selftests: Require GCC to realign stacks on function entry
Quoted 29 lines · Sean Christopherson
Quoted 27 lines · Maciej S. Szmigiero
Quoted 25 lines · Erdem Aktas
Quoted 12 lines · Sean Christopherson
Quoted 9 lines · Ackerley Tng
Some SSE instructions assume a 16-byte aligned stack, and GCC compiles assuming the stack is aligned:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838. This combination results in a #GP in guests.Adding this compiler flag will generate an alternate prologue and epilogue to realign the runtime stack, which makes selftest code slower and bigger, but this is okay since we do not need selftest code to be extremely performant.
Quoted 8 lines
Huh, I had completely forgotten that this is why SSE is problematic. I ran into this with the base UPM selftests and just disabled SSE. /facepalm.
We should figure out exactly what is causing a misaligned stack. As you've noted, the x86-64 ABI requires a 16-byte aligned RSP. Unless I'm misreading
vm_arch_vcpu_add(), the starting stack should be page aligned, which means something is causing the stack to become unaligned at runtime. I'd rather hunt down that something than paper over it by having the compiler force realignment.Is not it due to the 32bit execution part of the guest code at boot time. Any push/pop of 32bit registers might make it a 16-byte unaligned stack.
Quoted 5 lines
Quoted 2 lines
32-bit stack needs to be 16-byte aligned, too (at function call boundaries) - see [1] chapter 2.2.2 "The Stack Frame"
And this showing up in the non-TDX selftests rules that out as the sole problem; the selftests stuff 64-bit mode, i.e. don't have 32-bit boot code.
Thanks Maciej and Sean for the clarification. I was suspecting the hand-coded assembly part that we have for TDX tests but it being happening in the non-TDX selftests disproves it.
Sean Christopherson
reviewerRe: [RFC PATCH v3 08/31] KVM: selftests: Require GCC to realign stacks on function entry
Quoted 33 lines · Erdem Aktas
Quoted 32 lines · Sean Christopherson
Quoted 29 lines · Maciej S. Szmigiero
Quoted 22 lines · Erdem Aktas
Quoted 12 lines · Sean Christopherson
Quoted 9 lines · Ackerley Tng
Some SSE instructions assume a 16-byte aligned stack, and GCC compiles assuming the stack is aligned:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838. This combination results in a #GP in guests.Adding this compiler flag will generate an alternate prologue and epilogue to realign the runtime stack, which makes selftest code slower and bigger, but this is okay since we do not need selftest code to be extremely performant.
Quoted 8 lines
Huh, I had completely forgotten that this is why SSE is problematic. I ran into this with the base UPM selftests and just disabled SSE. /facepalm.
We should figure out exactly what is causing a misaligned stack. As you've noted, the x86-64 ABI requires a 16-byte aligned RSP. Unless I'm misreading
vm_arch_vcpu_add(), the starting stack should be page aligned, which means something is causing the stack to become unaligned at runtime. I'd rather hunt down that something than paper over it by having the compiler force realignment.Quoted 3 lines
Is not it due to the 32bit execution part of the guest code at boot time. Any push/pop of 32bit registers might make it a 16-byte unaligned stack.
32-bit stack needs to be 16-byte aligned, too (at function call boundaries) - see [1] chapter 2.2.2 "The Stack Frame"
Quoted 6 lines
Quoted 2 lines
And this showing up in the non-TDX selftests rules that out as the sole problem; the selftests stuff 64-bit mode, i.e. don't have 32-bit boot code.
Thanks Maciej and Sean for the clarification. I was suspecting the hand-coded assembly part that we have for TDX tests but it being happening in the non-TDX selftests disproves it.
Not necessarily, it could be both. Goofs in the handcoded assembly and PEBKAC on my end :-)
Re: [RFC PATCH v3 08/31] KVM: selftests: Require GCC to realign stacks on function entry
Quoted 52 lines · Erdem Aktas
Quoted 1 line
On Mon, Jan 23, 2023 at 10:53 AM Sean Christopherson
Quoted 4 lines · <[email protected]> wrote:
Quoted 4 lines · Maciej S. Szmigiero
Quoted 2 lines · Erdem Aktas
Quoted 1 line
On Fri, Jan 20, 2023 at 4:28 PM Sean Christopherson
Quoted 3 lines · <[email protected]> wrote:
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines · Ackerley Tng
Quoted 1 line
Some SSE instructions assume a 16-byte aligned stack, and GCC
compiles
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
assuming the stack is aligned:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838. Thiscombination
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
results in a #GP in guests.
Adding this compiler flag will generate an alternate prologue
and
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
epilogue to realign the runtime stack, which makes selftest
code
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
slower and bigger, but this is okay since we do not need
selftest code
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
to be extremely performant.
Huh, I had completely forgotten that this is why SSE is
problematic. I ran into
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
this with the base UPM selftests and just disabled SSE.
/facepalm.
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
We should figure out exactly what is causing a misaligned
stack. As you've noted,
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
the x86-64 ABI requires a 16-byte aligned RSP. Unless I'm
misreading
vm_arch_vcpu_add(),Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
the starting stack should be page aligned, which means
something is causing the
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
stack to become unaligned at runtime. I'd rather hunt down
that something than
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
paper over it by having the compiler force realignment.
Is not it due to the 32bit execution part of the guest code at
boot
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
time. Any push/pop of 32bit registers might make it a 16-byte unaligned stack.
32-bit stack needs to be 16-byte aligned, too (at function call
boundaries) -
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
see [1] chapter 2.2.2 "The Stack Frame"
And this showing up in the non-TDX selftests rules that out as the
sole problem;
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
the selftests stuff 64-bit mode, i.e. don't have 32-bit boot code.
Thanks Maciej and Sean for the clarification. I was suspecting the hand-coded assembly part that we have for TDX tests but it being happening in the non-TDX selftests disproves it.
Not necessarily, it could be both. Goofs in the handcoded assembly and PEBKAC on my end :-)
I figured it out!
GCC assumes that the stack is 16-byte aligned **before** the call instruction. Since call pushes rip to the stack, GCC will compile code assuming that on entrance to the function, the stack is -8 from a 16-byte aligned address.
Since for TDs we do a ljmp to guest code, providing a function's address, the stack was not modified by a call instruction pushing rip to the stack, so the stack is 16-byte aligned when the guest code starts running, instead of 16-byte aligned -8 that GCC expects.
For VMs, we set rip to a function pointer, and the VM starts running with a 16-byte algined stack too.
To fix this, I propose that in vm_arch_vcpu_add(), we align the allocated stack address and then subtract 8 from that:
@@ -573,10 +573,13 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
vcpu_init_cpuid(vcpu, kvm_get_supported_cpuid());
vcpu_setup(vm, vcpu);+ stack_vaddr += (DEFAULT_STACK_PGS * getpagesize());
+ stack_vaddr = ALIGN_DOWN(stack_vaddr, 16) - 8;
+
/* Setup guest general purpose registers */
vcpu_regs_get(vcpu, ®s);
regs.rflags = regs.rflags | 0x2;
- regs.rsp = stack_vaddr + (DEFAULT_STACK_PGS * getpagesize());
+ regs.rsp = stack_vaddr;
regs.rip = (unsigned long) guest_code;
vcpu_regs_set(vcpu, ®s);Re: [RFC PATCH v3 08/31] KVM: selftests: Require GCC to realign stacks on function entry
Quoted 57 lines · Ackerley Tng
Quoted 38 lines · Erdem Aktas
Quoted 36 lines · Sean Christopherson
Quoted 34 lines · Maciej S. Szmigiero
Quoted 23 lines · Erdem Aktas
Quoted 22 lines · Sean Christopherson
Quoted 20 lines · Ackerley Tng
Quoted 9 lines
Some SSE instructions assume a 16-byte aligned stack, and GCC compiles assuming the stack is aligned:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40838. This combination results in a #GP in guests.Adding this compiler flag will generate an alternate prologue and epilogue to realign the runtime stack, which makes selftest code slower and bigger, but this is okay since we do not need selftest code to be extremely performant.
Huh, I had completely forgotten that this is why SSE is problematic. I ran into this with the base UPM selftests and just disabled SSE. /facepalm.
We should figure out exactly what is causing a misaligned stack. As you've noted, the x86-64 ABI requires a 16-byte aligned RSP. Unless I'm misreading
vm_arch_vcpu_add(), the starting stack should be page aligned, which means something is causing the stack to become unaligned at runtime. I'd rather hunt down that something than paper over it by having the compiler force realignment.Quoted 6 lines
Quoted 3 lines
Is not it due to the 32bit execution part of the guest code at boot time. Any push/pop of 32bit registers might make it a 16-byte unaligned stack.
32-bit stack needs to be 16-byte aligned, too (at function call boundaries) - see [1] chapter 2.2.2 "The Stack Frame"
And this showing up in the non-TDX selftests rules that out as the sole problem; the selftests stuff 64-bit mode, i.e. don't have 32-bit boot code.
Quoted 6 lines
Quoted 3 lines
Thanks Maciej and Sean for the clarification. I was suspecting the hand-coded assembly part that we have for TDX tests but it being happening in the non-TDX selftests disproves it.
Not necessarily, it could be both. Goofs in the handcoded assembly and PEBKAC on my end :-)
I figured it out!
GCC assumes that the stack is 16-byte aligned **before** the call instruction. Since call pushes rip to the stack, GCC will compile code assuming that on entrance to the function, the stack is -8 from a 16-byte aligned address.
Since for TDs we do a ljmp to guest code, providing a function's address, the stack was not modified by a call instruction pushing rip to the stack, so the stack is 16-byte aligned when the guest code starts running, instead of 16-byte aligned -8 that GCC expects.
Quoted 6 lines
For VMs, we set rip to a function pointer, and the VM starts running with a 16-byte algined stack too.
To fix this, I propose that in
vm_arch_vcpu_add(), we align the allocated stack address and then subtract 8 from that:
Note that if this code is ever used to launch a vCPU with 32-bit entry point it will need to subtract 4 bytes instead of 8 bytes.
I think it would be worthwhile to at least place a comment mentioning this near the stack aligning expression so nobody misses this fact.
Thanks,
Maciej
Sean Christopherson
reviewerRe: [RFC PATCH v3 08/31] KVM: selftests: Require GCC to realign stacks on function entry
Quoted 9 lines · Maciej S. Szmigiero
Quoted 2 lines · Ackerley Tng
To fix this, I propose that in
vm_arch_vcpu_add(), we align the allocated stack address and then subtract 8 from that:Note that if this code is ever used to launch a vCPU with 32-bit entry point it will need to subtract 4 bytes instead of 8 bytes.
I think it would be worthwhile to at least place a comment mentioning this near the stack aligning expression so nobody misses this fact.
Heh, I've no objection to a comment, though this really is the tip of the iceberg if we want to add 32-bit guest support in selftests.
Sean Christopherson
reviewerRe: [RFC PATCH v3 08/31] KVM: selftests: Require GCC to realign stacks on function entry
In reply to Ackerley TngQuoted 19 lines · Ackerley Tng
I figured it out!
GCC assumes that the stack is 16-byte aligned **before** the call instruction. Since call pushes rip to the stack, GCC will compile code assuming that on entrance to the function, the stack is -8 from a 16-byte aligned address.
Since for TDs we do a ljmp to guest code, providing a function's address, the stack was not modified by a call instruction pushing rip to the stack, so the stack is 16-byte aligned when the guest code starts running, instead of 16-byte aligned -8 that GCC expects.
For VMs, we set rip to a function pointer, and the VM starts running with a 16-byte algined stack too.
To fix this, I propose that in
vm_arch_vcpu_add(), we align the allocated stack address and then subtract 8 from that:
Review struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, · line 578
Quoted 6 lines
(quoted diff) +2
-0@@ -573,10 +573,13 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id, vcpu_init_cpuid(vcpu, kvm_get_supported_cpuid()); vcpu_setup(vm, vcpu); + stack_vaddr += (DEFAULT_STACK_PGS * getpagesize()); + stack_vaddr = ALIGN_DOWN(stack_vaddr, 16) - 8;
The ALIGN_DOWN should be unnecessary, we've got larger issues if getpagesize() isn't 16-byte aligned and/or if __vm_vaddr_alloc() returns anything but a page-aligned address. Maybe add a TEST_ASSERT() sanity check that stack_vaddr is page-aligned at this point?
And in addition to the comment suggested by Maciej, can you also add a comment explaining the -8 adjust? Yeah, someone can go read the changelog, but I think this is worth explicitly documenting in code.
Lastly, can you post it as a standalone patch?
Many thanks!
Re: [RFC PATCH v3 08/31] KVM: selftests: Require GCC to realign stacks on function entry
Quoted 33 lines
Review struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm
Quoted 20 lines
I figured it out!
GCC assumes that the stack is 16-byte aligned **before** the call instruction. Since call pushes rip to the stack, GCC will compile code assuming that on entrance to the function, the stack is -8 from a 16-byte aligned address.
Since for TDs we do a ljmp to guest code, providing a function's address, the stack was not modified by a call instruction pushing rip to the stack, so the stack is 16-byte aligned when the guest code starts running, instead of 16-byte aligned -8 that GCC expects.
For VMs, we set rip to a function pointer, and the VM starts running with a 16-byte algined stack too.
To fix this, I propose that in
vm_arch_vcpu_add(), we align the allocated stack address and then subtract 8 from that:(quoted diff) +0
-0@@ -573,10 +573,13 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm*vm,
Review struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm · line 578
Quoted 6 lines
(quoted diff) +2
-0uint32_t vcpu_id, vcpu_init_cpuid(vcpu, kvm_get_supported_cpuid()); vcpu_setup(vm, vcpu); + stack_vaddr += (DEFAULT_STACK_PGS * getpagesize()); + stack_vaddr = ALIGN_DOWN(stack_vaddr, 16) - 8;The
ALIGN_DOWNshould be unnecessary, we've got larger issues ifgetpagesize()isn't 16-byte aligned and/or if__vm_vaddr_alloc()returns anything but a page-aligned address. Maybe add aTEST_ASSERT()sanity check thatstack_vaddris page-aligned at this point?
Quoted 7 lines
And in addition to the comment suggested by Maciej, can you also add a comment explaining the -8 adjust? Yeah, someone can go read the changelog, but I think this is worth explicitly documenting in code.
Lastly, can you post it as a standalone patch?
Many thanks!
Thanks Maciej and Sean, I've made the changes you requested and posted it as a standalone patch at
https://lore.kernel.org/lkml/32866e5d00174697730d6231d2fb81f6b8d98c8a.1676659352.git.ackerleytng@google.com/
RFC PATCH v3 9/31 KVM: selftests: TDX: Add TDX lifecycle test
In reply to Ackerley TngFrom: Erdem Aktas <[email protected]>
Adding a test to verify TDX lifecycle by creating a TD and running a dummy TDG.VP.VMCALL <Instruction.IO> inside it.
---
Changes RFCv2 -> RFCv3
+ Add gitignore for tdx_vm_tests binary
+ Thanks to Isaku for the comment about saving rbp separately for gcc! In this revision, the assembly that wraps tdcall has been mostly taken from the kernel’s implementation, which would be familiar to you!
+ TDX test-related code is now grouped in tools/testing/selftests/kvm/include/x86_64/tdx/test_util.{c,h} and test-related functions and macros are prefixed with tdx_test to indicate its purpose.
--- tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 4 + .../selftests/kvm/include/x86_64/tdx/tdcall.h | 35 ++++++++ .../selftests/kvm/include/x86_64/tdx/tdx.h | 12 +++ .../kvm/include/x86_64/tdx/test_util.h | 52 +++++++++++ .../selftests/kvm/lib/x86_64/tdx/tdcall.S | 90 +++++++++++++++++++ .../selftests/kvm/lib/x86_64/tdx/tdx.c | 27 ++++++ .../selftests/kvm/lib/x86_64/tdx/test_util.c | 34 +++++++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 45 ++++++++++ 9 files changed, 300 insertions(+) create mode 100644 tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h create mode 100644 tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h create mode 100644 tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h create mode 100644 tools/testing/selftests/kvm/lib/x86_64/tdx/tdcall.S create mode 100644 tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c create mode 100644 tools/testing/selftests/kvm/lib/x86_64/tdx/test_util.c create mode 100644 tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c
tools/testing/selftests/kvm/.gitignore +1 -0
@@ -65,6 +65,7 @@ /x86_64/xss_msr_test /x86_64/vmx_pmu_caps_test /x86_64/triple_fault_event_test +/x86_64/tdx_vm_tests /access_tracking_perf_test /demand_paging_test /dirty_log_test
tools/testing/selftests/kvm/Makefile +4 -0
@@ -61,6 +61,9 @@ LIBKVM_x86_64 += lib/x86_64/vmx.c LIBKVM_x86_64 += lib/x86_64/sev.c LIBKVM_x86_64 += lib/x86_64/tdx/tdx_util.c LIBKVM_x86_64 += lib/x86_64/tdx/td_boot.S +LIBKVM_x86_64 += lib/x86_64/tdx/tdcall.S +LIBKVM_x86_64 += lib/x86_64/tdx/tdx.c +LIBKVM_x86_64 += lib/x86_64/tdx/test_util.c LIBKVM_aarch64 += lib/aarch64/gic.c LIBKVM_aarch64 += lib/aarch64/gic_v3.c
@@ -148,6 +151,7 @@ TEST_GEN_PROGS_x86_64 += set_memory_region_test TEST_GEN_PROGS_x86_64 += steal_time TEST_GEN_PROGS_x86_64 += kvm_binary_stats_test TEST_GEN_PROGS_x86_64 += system_counter_offset_test +TEST_GEN_PROGS_x86_64 += x86_64/tdx_vm_tests # Compiled outputs used by test targets TEST_GEN_PROGS_EXTENDED_x86_64 += x86_64/nx_huge_pages_test
tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h new file +35 -0
@@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Adapted from arch/x86/include/asm/shared/tdx.h */ + +#ifndef SELFTESTS_TDX_TDCALL_H +#define SELFTESTS_TDX_TDCALL_H + +#include <linux/bits.h> +#include <linux/types.h> + +#define TDG_VP_VMCALL_INSTRUCTION_IO_READ 0 +#define TDG_VP_VMCALL_INSTRUCTION_IO_WRITE 1 + +#define TDX_HCALL_HAS_OUTPUT BIT(0) + +#define TDX_HYPERCALL_STANDARD 0 + +/* +* Used in __tdx_hypercall() to pass down and get back registers' values of +* the TDCALL instruction when requesting services from the VMM. +* +* This is a software only structure and not part of the TDX module/VMM ABI. +*/ +struct tdx_hypercall_args { +u64 r10; +u64 r11; +u64 r12; +u64 r13; +u64 r14; +u64 r15; +}; + +/* Used to request services from the VMM */ +u64 __tdx_hypercall (struct tdx_hypercall_args *args,unsigned long flags); + +#endif // SELFTESTS_TDX_TDCALL_H
tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h new file +12 -0
@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SELFTEST_TDX_TDX_H +#define SELFTEST_TDX_TDX_H + +#include <stdint.h> + +#define TDG_VP_VMCALL_INSTRUCTION_IO 30 + +uint64_t tdg_vp_vmcall_instruction_io (uint64_t port,uint64_t size, +uint64_t write,uint64_t *data); + +#endif // SELFTEST_TDX_TDX_H
tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h new file +52 -0
52 lines not on this page · raw message
tools/testing/selftests/kvm/lib/x86_64/tdx/tdcall.S new file +90 -0
90 lines not on this page · raw message
tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c new file +27 -0
@@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include "tdx/tdcall.h" +#include "tdx/tdx.h" + +uint64_t tdg_vp_vmcall_instruction_io (uint64_t port,uint64_t size, +uint64_t write,uint64_t *data) +{ +uint64_t ret; +struct tdx_hypercall_args args = { + .r10 =TDX_HYPERCALL_STANDARD , + .r11 =TDG_VP_VMCALL_INSTRUCTION_IO , + .r12 = size, + .r13 = write, + .r14 = port, + }; + +if (write) + args.r15 = *data; + + ret =__tdx_hypercall (&args, write ?0 :TDX_HCALL_HAS_OUTPUT ); + +if (!write) + *data = args.r11; + +return ret; +}
tools/testing/selftests/kvm/lib/x86_64/tdx/test_util.c new file +34 -0
@@ -0,0 +1,34 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include <stdbool.h> +#include <stdint.h> +#include <stdlib.h> +#include <sys/wait.h> +#include <unistd.h> + +#include "kvm_util_base.h" +#include "tdx/tdx.h" +#include "tdx/test_util.h" + +void run_in_new_process (void (*func)(void )) +{ +if (fork () ==0 ) { +func (); +exit (0 ); + } +wait (NULL ); +} + +bool is_tdx_enabled (void ) +{ +return !!(kvm_check_cap (KVM_CAP_VM_TYPES ) &BIT (KVM_X86_TDX_VM )); +} + +void tdx_test_success (void ) +{ +uint64_t code =0 ; + +tdg_vp_vmcall_instruction_io (TDX_TEST_SUCCESS_PORT , +TDX_TEST_SUCCESS_SIZE , +TDG_VP_VMCALL_INSTRUCTION_IO_WRITE , &code); +}
tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c new file +45 -0
45 lines not on this page · raw message
--
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 10/31 KVM: selftests: TDX: Add report_fatal_error test
In reply to Ackerley TngFrom: Sagi Shahar <[email protected]>
The test checks report_fatal_error functionality.
--- .../selftests/kvm/include/x86_64/tdx/tdx.h | 3 ++ .../kvm/include/x86_64/tdx/test_util.h | 19 ++++++++ .../selftests/kvm/lib/x86_64/tdx/tdx.c | 18 ++++++++ .../selftests/kvm/lib/x86_64/tdx/test_util.c | 10 +++++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 44 +++++++++++++++++++ 5 files changed, 94 insertions(+)
tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +3 -0
@@ -4,9 +4,12 @@#include <stdint.h> +#define TDG_VP_VMCALL_REPORT_FATAL_ERROR 0x10003 +#define TDG_VP_VMCALL_INSTRUCTION_IO 30uint64_t tdg_vp_vmcall_instruction_io (uint64_t port,uint64_t size,uint64_t write,uint64_t *data); +void tdg_vp_vmcall_report_fatal_error (uint64_t error_code,uint64_t data_gpa);#endif // SELFTEST_TDX_TDX_H
tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h +19 -0
@@ -49,4 +49,23 @@ bool is_tdx_enabled(void);*/ void tdx_test_success (void ); +/** +* Report an error with @error_code to userspace. +* +* Return value from tdg_vp_vmcall_report_fatal_error is ignored since execution +* is not expected to continue beyond this point. +*/ +void tdx_test_fatal (uint64_t error_code); + +/** +* Report an error with @error_code to userspace. +* +* @data_gpa may point to an optional shared guest memory holding the error +* string. +* +* Return value from tdg_vp_vmcall_report_fatal_error is ignored since execution +* is not expected to continue beyond this point. +*/ +void tdx_test_fatal_with_data (uint64_t error_code,uint64_t data_gpa); +#endif // SELFTEST_TDX_TEST_UTIL_H
tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +18 -0
@@ -1,5 +1,7 @@// SPDX-License-Identifier: GPL-2.0-only +#include <string.h> +#include "tdx/tdcall.h" #include "tdx/tdx.h"
@@ -25,3 +27,19 @@ uint64_t tdg_vp_vmcall_instruction_io(uint64_t port, uint64_t size,return ret; } + +void tdg_vp_vmcall_report_fatal_error (uint64_t error_code,uint64_t data_gpa) +{ +struct tdx_hypercall_args args; + +memset (&args,0 ,sizeof (struct tdx_hypercall_args )); + +if (data_gpa) + error_code |=0x8000000000000000 ; + + args.r11 =TDG_VP_VMCALL_REPORT_FATAL_ERROR ; + args.r12 = error_code; + args.r13 = data_gpa; + +__tdx_hypercall (&args,0 ); +}
tools/testing/selftests/kvm/lib/x86_64/tdx/test_util.c +10 -0
@@ -32,3 +32,13 @@ void tdx_test_success(void)TDX_TEST_SUCCESS_SIZE , TDG_VP_VMCALL_INSTRUCTION_IO_WRITE, &code); } + +void tdx_test_fatal_with_data (uint64_t error_code,uint64_t data_gpa) +{ +tdg_vp_vmcall_report_fatal_error (error_code, data_gpa); +} + +void tdx_test_fatal (uint64_t error_code) +{ +tdx_test_fatal_with_data (error_code,0 ); +}
tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +44 -0
62 lines not on this page · raw message
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 11/31 KVM: selftests: TDX: Adding test case for TDX port IO
In reply to Ackerley TngFrom: Erdem Aktas <[email protected]>
Verifies TDVMCALL<INSTRUCTION.IO> READ and WRITE operations.
--- .../kvm/include/x86_64/tdx/test_util.h | 34 ++++++++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 82 +++++++++++++++++++ 2 files changed, 116 insertions(+)
tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h +34 -0
@@ -9,6 +9,40 @@#define TDX_TEST_SUCCESS_PORT 0x30#define TDX_TEST_SUCCESS_SIZE 4 +/** +* Assert that some IO operation involving tdg_vp_vmcall_instruction_io() was +* called in the guest. +*/ +#define TDX_TEST_ASSERT_IO (VCPU ,PORT ,SIZE ,DIR ) \ + do { \ + TEST_ASSERT((VCPU)->run->exit_reason == KVM_EXIT_IO, \ + "Got exit_reason other than KVM_EXIT_IO: %u (%s)\n", \ + (VCPU)->run->exit_reason, \ + exit_reason_str((VCPU)->run->exit_reason)); \ + \ + TEST_ASSERT(((VCPU)->run->exit_reason == KVM_EXIT_IO) && \ + ((VCPU)->run->io.port == (PORT)) && \ + ((VCPU)->run->io.size == (SIZE)) && \ + ((VCPU)->run->io.direction == (DIR)), \ + "Got unexpected IO exit values: %u (%s) %d %d %d\n", \ + (VCPU)->run->exit_reason, \ + exit_reason_str((VCPU)->run->exit_reason), \ + (VCPU)->run->io.port, (VCPU)->run->io.size, \ + (VCPU)->run->io.direction); \ + } while (0) + +/** +* Check and report if there was some failure in the guest, either an exception +* like a triple fault, or if a tdx_test_fatal() was hit. +*/ +#define TDX_TEST_CHECK_GUEST_FAILURE (VCPU ) \ + do { \ + if ((VCPU)->run->exit_reason == KVM_EXIT_SYSTEM_EVENT) \ + TEST_FAIL("Guest reported error. error code: %lld (0x%llx)\n", \ + (VCPU)->run->system_event.data[1], \ + (VCPU)->run->system_event.data[1]); \ + } while (0) +/** * Assert that tdx_test_success() was called in the guest. */
tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +82 -0
100 lines not on this page · raw message
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 12/31 KVM: selftests: TDX: Add basic TDX CPUID test
In reply to Ackerley TngFrom: Sagi Shahar <[email protected]>
The test reads CPUID values from inside a TD VM and compare them to expected values.
The test targets CPUID values which are virtualized as "As Configured", "As Configured (if Native)", "Calculated", "Fixed" and "Native" according to the TDX spec.
---
Changes RFCv2 -> RFCv3
+ Manually inlined cpuid function in cpuid test. This highlights the purpose of this test - to test the result of the cpuid instruction.
+ Replace find_cpuid_entry with kvm_get_supported_cpuid_entry from tools/testing/selftests/kvm/lib/x86_64/processor.c
--- .../kvm/include/x86_64/tdx/test_util.h | 9 ++ .../selftests/kvm/lib/x86_64/tdx/test_util.c | 11 ++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 106 ++++++++++++++++++ 3 files changed, 126 insertions(+)
tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h +9 -0
@@ -9,6 +9,9 @@#define TDX_TEST_SUCCESS_PORT 0x30#define TDX_TEST_SUCCESS_SIZE 4 +#define TDX_TEST_REPORT_PORT 0x31 +#define TDX_TEST_REPORT_SIZE 4 +/** * Assert that some IO operation involving tdg_vp_vmcall_instruction_io() was * called in the guest.
@@ -102,4 +105,10 @@ void tdx_test_fatal(uint64_t error_code);*/ void tdx_test_fatal_with_data (uint64_t error_code,uint64_t data_gpa); +/** +* Report a 32 bit value from the guest to user space using TDG.VP.VMCALL +* <Instruction.IO> call. Data is reported on port TDX_TEST_REPORT_PORT. +*/ +uint64_t tdx_test_report_to_user_space (uint32_t data); +#endif // SELFTEST_TDX_TEST_UTIL_H
tools/testing/selftests/kvm/lib/x86_64/tdx/test_util.c +11 -0
@@ -42,3 +42,14 @@ void tdx_test_fatal(uint64_t error_code) {tdx_test_fatal_with_data (error_code,0 ); } + +uint64_t tdx_test_report_to_user_space (uint32_t data) +{ +/* Upcast data to match tdg_vp_vmcall_instruction_io signature */ +uint64_t data_64 = data; + +return tdg_vp_vmcall_instruction_io (TDX_TEST_REPORT_PORT , +TDX_TEST_REPORT_SIZE , +TDG_VP_VMCALL_INSTRUCTION_IO_WRITE , + &data_64); +}
tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +106 -0
124 lines not on this page · raw message
--
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 13/31 KVM: selftests: TDX: Add basic get_td_vmcall_info test
In reply to Ackerley TngFrom: Sagi Shahar <[email protected]>
The test calls get_td_vmcall_info from the guest and verifies the expected returned values.
--- .../selftests/kvm/include/x86_64/tdx/tdx.h | 3 + .../kvm/include/x86_64/tdx/test_util.h | 27 +++++++ .../selftests/kvm/lib/x86_64/tdx/tdx.c | 23 ++++++ .../selftests/kvm/lib/x86_64/tdx/test_util.c | 46 +++++++++++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 80 +++++++++++++++++++ 5 files changed, 179 insertions(+)
tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +3 -0
@@ -4,6 +4,7 @@#include <stdint.h> +#define TDG_VP_VMCALL_GET_TD_VM_CALL_INFO 0x10000#define TDG_VP_VMCALL_REPORT_FATAL_ERROR 0x10003#define TDG_VP_VMCALL_INSTRUCTION_IO 30
@@ -11,5 +12,7 @@uint64_t tdg_vp_vmcall_instruction_io (uint64_t port,uint64_t size,uint64_t write,uint64_t *data);void tdg_vp_vmcall_report_fatal_error (uint64_t error_code,uint64_t data_gpa); +uint64_t tdg_vp_vmcall_get_td_vmcall_info (uint64_t *r11,uint64_t *r12, +uint64_t *r13,uint64_t *r14);#endif // SELFTEST_TDX_TDX_H
tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h +27 -0
@@ -4,6 +4,7 @@#include <stdbool.h> +#include "kvm_util_base.h" #include "tdcall.h" #define TDX_TEST_SUCCESS_PORT 0x30
@@ -111,4 +112,30 @@ void tdx_test_fatal_with_data(uint64_t error_code, uint64_t data_gpa);*/ uint64_t tdx_test_report_to_user_space (uint32_t data); +/** +* Report a 64 bit value from the guest to user space using TDG.VP.VMCALL +* <Instruction.IO> call. +* +* Data is sent to host in 2 calls. LSB is sent (and needs to be read) first. +*/ +uint64_t tdx_test_send_64bit (uint64_t port,uint64_t data); + +/** +* Report a 64 bit value from the guest to user space using TDG.VP.VMCALL +* <Instruction.IO> call. Data is reported on port TDX_TEST_REPORT_PORT. +*/ +uint64_t tdx_test_report_64bit_to_user_space (uint64_t data); + +/** +* Read a 64 bit value from the guest in user space, sent using +* tdx_test_send_64bit(). +*/ +uint64_t tdx_test_read_64bit (struct kvm_vcpu *vcpu,uint64_t port); + +/** +* Read a 64 bit value from the guest in user space, sent using +* tdx_test_report_64bit_to_user_space. +*/ +uint64_t tdx_test_read_64bit_report_from_guest (struct kvm_vcpu *vcpu); +#endif // SELFTEST_TDX_TEST_UTIL_H
tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +23 -0
@@ -43,3 +43,26 @@ void tdg_vp_vmcall_report_fatal_error(uint64_t error_code, uint64_t data_gpa)__tdx_hypercall (&args,0 ); } + +uint64_t tdg_vp_vmcall_get_td_vmcall_info (uint64_t *r11,uint64_t *r12, +uint64_t *r13,uint64_t *r14) +{ +uint64_t ret; +struct tdx_hypercall_args args = { + .r11 =TDG_VP_VMCALL_GET_TD_VM_CALL_INFO , + .r12 =0 , + }; + + ret =__tdx_hypercall (&args,TDX_HCALL_HAS_OUTPUT ); + +if (r11) + *r11 = args.r11; +if (r12) + *r12 = args.r12; +if (r13) + *r13 = args.r13; +if (r14) + *r14 = args.r14; + +return ret; +}
tools/testing/selftests/kvm/lib/x86_64/tdx/test_util.c +46 -0
55 lines not on this page · raw message
tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +80 -0
92 lines not on this page · raw message
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 14/31 KVM: selftests: TDX: Add TDX IO writes test
In reply to Ackerley TngFrom: Sagi Shahar <[email protected]>
The test verifies IO writes of various sizes from the guest to the host.
--- .../selftests/kvm/include/x86_64/tdx/tdcall.h | 3 + .../selftests/kvm/x86_64/tdx_vm_tests.c | 91 +++++++++++++++++++ 2 files changed, 94 insertions(+)
tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h +3 -0
@@ -10,6 +10,9 @@#define TDG_VP_VMCALL_INSTRUCTION_IO_READ 0#define TDG_VP_VMCALL_INSTRUCTION_IO_WRITE 1 +#define TDG_VP_VMCALL_SUCCESS 0x0000000000000000 +#define TDG_VP_VMCALL_INVALID_OPERAND 0x8000000000000000 +#define TDX_HCALL_HAS_OUTPUT BIT(0)#define TDX_HYPERCALL_STANDARD 0
tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +91 -0
103 lines not on this page · raw message
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 15/31 KVM: selftests: TDX: Add TDX IO reads test
In reply to Ackerley TngFrom: Sagi Shahar <[email protected]>
The test verifies IO reads of various sizes from the host to the guest.
--- .../selftests/kvm/x86_64/tdx_vm_tests.c | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+)
tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +87 -0
@@ -428,6 +428,92 @@ void verify_guest_writes(void)printf ("\t ... PASSED\n" ); } +#define TDX_IO_READS_TEST_PORT 0x52 + +/* +* Verifies IO functionality by reading values of different sizes +* from the host. +*/ +void guest_io_reads (void ) +{ +uint64_t data; +uint64_t ret; + + ret =tdg_vp_vmcall_instruction_io (TDX_IO_READS_TEST_PORT ,1 , +TDG_VP_VMCALL_INSTRUCTION_IO_READ , + &data); +if (ret) +tdx_test_fatal (ret); +if (data !=0xAB ) +tdx_test_fatal (1 ); + + ret =tdg_vp_vmcall_instruction_io (TDX_IO_READS_TEST_PORT ,2 , +TDG_VP_VMCALL_INSTRUCTION_IO_READ , + &data); +if (ret) +tdx_test_fatal (ret); +if (data !=0xABCD ) +tdx_test_fatal (2 ); + + ret =tdg_vp_vmcall_instruction_io (TDX_IO_READS_TEST_PORT ,4 , +TDG_VP_VMCALL_INSTRUCTION_IO_READ , + &data); +if (ret) +tdx_test_fatal (ret); +if (data !=0xFFABCDEF ) +tdx_test_fatal (4 ); + +// Read an invalid number of bytes. + ret =tdg_vp_vmcall_instruction_io (TDX_IO_READS_TEST_PORT ,5 , +TDG_VP_VMCALL_INSTRUCTION_IO_READ , + &data); +if (ret) +tdx_test_fatal (ret); + +tdx_test_success (); +} + +void verify_guest_reads (void ) +{ +struct kvm_vm *vm; +struct kvm_vcpu *vcpu; + + vm =td_create (); +td_initialize (vm,VM_MEM_SRC_ANONYMOUS ,0 ); + vcpu =td_vcpu_add (vm,0 , guest_io_reads); +td_finalize (vm); + +printf ("Verifying guest reads:\n" ); + +vcpu_run (vcpu); +TDX_TEST_CHECK_GUEST_FAILURE (vcpu); +TDX_TEST_ASSERT_IO (vcpu,TDX_IO_READS_TEST_PORT ,1 , +TDG_VP_VMCALL_INSTRUCTION_IO_READ ); + *(uint8_t *)((void *)vcpu->run + vcpu->run->io.data_offset) =0xAB ; + +vcpu_run (vcpu); +TDX_TEST_CHECK_GUEST_FAILURE (vcpu); +TDX_TEST_ASSERT_IO (vcpu,TDX_IO_READS_TEST_PORT ,2 , +TDG_VP_VMCALL_INSTRUCTION_IO_READ ); + *(uint16_t *)((void *)vcpu->run + vcpu->run->io.data_offset) =0xABCD ; + +vcpu_run (vcpu); +TDX_TEST_CHECK_GUEST_FAILURE (vcpu); +TDX_TEST_ASSERT_IO (vcpu,TDX_IO_READS_TEST_PORT ,4 , +TDG_VP_VMCALL_INSTRUCTION_IO_READ ); + *(uint32_t *)((void *)vcpu->run + vcpu->run->io.data_offset) =0xFFABCDEF ; + +vcpu_run (vcpu); +ASSERT_EQ (vcpu->run->exit_reason,KVM_EXIT_SYSTEM_EVENT ); +ASSERT_EQ (vcpu->run->system_event.data[1 ],TDG_VP_VMCALL_INVALID_OPERAND ); + +vcpu_run (vcpu); +TDX_TEST_ASSERT_SUCCESS (vcpu); + +kvm_vm_free (vm); +printf ("\t ... PASSED\n" ); +} +int main (int argc,char **argv) {setbuf (stdout,NULL );
@@ -443,6 +529,7 @@ int main(int argc, char **argv)run_in_new_process (&verify_td_cpuid);run_in_new_process (&verify_get_td_vmcall_info);run_in_new_process (&verify_guest_writes); +run_in_new_process (&verify_guest_reads);return 0 ; }
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 16/31 KVM: selftests: TDX: Add TDX MSR read/write tests
In reply to Ackerley TngFrom: Sagi Shahar <[email protected]>
The test verifies reads and writes for MSR registers with different access level.
---
Changes RFCv2 -> RFCv3
+ Fixed typo in MTTR->MTRR
--- .../selftests/kvm/include/x86_64/tdx/tdx.h | 4 + .../selftests/kvm/lib/x86_64/tdx/tdx.c | 27 +++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 217 ++++++++++++++++++ 3 files changed, 248 insertions(+)
tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +4 -0
@@ -8,11 +8,15 @@#define TDG_VP_VMCALL_REPORT_FATAL_ERROR 0x10003#define TDG_VP_VMCALL_INSTRUCTION_IO 30 +#define TDG_VP_VMCALL_INSTRUCTION_RDMSR 31 +#define TDG_VP_VMCALL_INSTRUCTION_WRMSR 32uint64_t tdg_vp_vmcall_instruction_io (uint64_t port,uint64_t size,uint64_t write,uint64_t *data);void tdg_vp_vmcall_report_fatal_error (uint64_t error_code,uint64_t data_gpa);uint64_t tdg_vp_vmcall_get_td_vmcall_info (uint64_t *r11,uint64_t *r12,uint64_t *r13,uint64_t *r14); +uint64_t tdg_vp_vmcall_instruction_rdmsr (uint64_t index,uint64_t *ret_value); +uint64_t tdg_vp_vmcall_instruction_wrmsr (uint64_t index,uint64_t value);#endif // SELFTEST_TDX_TDX_H
tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +27 -0
@@ -66,3 +66,30 @@ uint64_t tdg_vp_vmcall_get_td_vmcall_info(uint64_t *r11, uint64_t *r12,return ret; } + +uint64_t tdg_vp_vmcall_instruction_rdmsr (uint64_t index,uint64_t *ret_value) +{ +uint64_t ret; +struct tdx_hypercall_args args = { + .r11 =TDG_VP_VMCALL_INSTRUCTION_RDMSR , + .r12 = index, + }; + + ret =__tdx_hypercall (&args,TDX_HCALL_HAS_OUTPUT ); + +if (ret_value) + *ret_value = args.r11; + +return ret; +} + +uint64_t tdg_vp_vmcall_instruction_wrmsr (uint64_t index,uint64_t value) +{ +struct tdx_hypercall_args args = { + .r11 =TDG_VP_VMCALL_INSTRUCTION_WRMSR , + .r12 = index, + .r13 = value, + }; + +return __tdx_hypercall (&args,0 ); +}
tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +217 -0
229 lines not on this page · raw message
--
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 17/31 KVM: selftests: TDX: Add TDX HLT exit test
In reply to Ackerley TngFrom: Sagi Shahar <[email protected]>
The test verifies that the guest runs TDVMCALL<INSTRUCTION.HLT> and the guest vCPU enters to the halted state.
--- .../selftests/kvm/include/x86_64/tdx/tdx.h | 3 + .../selftests/kvm/lib/x86_64/tdx/tdx.c | 10 +++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 78 +++++++++++++++++++ 3 files changed, 91 insertions(+)
tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +3 -0
@@ -7,10 +7,12 @@#define TDG_VP_VMCALL_GET_TD_VM_CALL_INFO 0x10000#define TDG_VP_VMCALL_REPORT_FATAL_ERROR 0x10003 +#define TDG_VP_VMCALL_INSTRUCTION_HLT 12#define TDG_VP_VMCALL_INSTRUCTION_IO 30#define TDG_VP_VMCALL_INSTRUCTION_RDMSR 31#define TDG_VP_VMCALL_INSTRUCTION_WRMSR 32 +uint64_t tdg_vp_vmcall_instruction_io (uint64_t port,uint64_t size,uint64_t write,uint64_t *data);void tdg_vp_vmcall_report_fatal_error (uint64_t error_code,uint64_t data_gpa);
@@ -18,5 +20,6 @@ uint64_t tdg_vp_vmcall_get_td_vmcall_info(uint64_t *r11, uint64_t *r12, uint64_t *r13, uint64_t *r14);uint64_t tdg_vp_vmcall_instruction_rdmsr (uint64_t index,uint64_t *ret_value);uint64_t tdg_vp_vmcall_instruction_wrmsr (uint64_t index,uint64_t value); +uint64_t tdg_vp_vmcall_instruction_hlt (uint64_t interrupt_blocked_flag);#endif // SELFTEST_TDX_TDX_H
tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +10 -0
@@ -93,3 +93,13 @@ uint64_t tdg_vp_vmcall_instruction_wrmsr(uint64_t index, uint64_t value)return __tdx_hypercall (&args,0 ); } + +uint64_t tdg_vp_vmcall_instruction_hlt (uint64_t interrupt_blocked_flag) +{ +struct tdx_hypercall_args args = { + .r11 =TDG_VP_VMCALL_INSTRUCTION_HLT , + .r12 = interrupt_blocked_flag, + }; + +return __tdx_hypercall (&args,0 ); +}
tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +78 -0
90 lines not on this page · raw message
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 18/31 KVM: selftests: TDX: Add TDX MMIO reads test
In reply to Ackerley TngFrom: Sagi Shahar <[email protected]>
The test verifies MMIO reads of various sizes from the host to the guest.
--- .../selftests/kvm/include/x86_64/tdx/tdcall.h | 2 + .../selftests/kvm/include/x86_64/tdx/tdx.h | 3 + .../kvm/include/x86_64/tdx/test_util.h | 23 +++++ .../selftests/kvm/lib/x86_64/tdx/tdx.c | 19 ++++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 87 +++++++++++++++++++ 5 files changed, 134 insertions(+)
tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h +2 -0
@@ -9,6 +9,8 @@#define TDG_VP_VMCALL_INSTRUCTION_IO_READ 0#define TDG_VP_VMCALL_INSTRUCTION_IO_WRITE 1 +#define TDG_VP_VMCALL_VE_REQUEST_MMIO_READ 0 +#define TDG_VP_VMCALL_VE_REQUEST_MMIO_WRITE 1#define TDG_VP_VMCALL_SUCCESS 0x0000000000000000#define TDG_VP_VMCALL_INVALID_OPERAND 0x8000000000000000
tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +3 -0
@@ -11,6 +11,7 @@#define TDG_VP_VMCALL_INSTRUCTION_IO 30#define TDG_VP_VMCALL_INSTRUCTION_RDMSR 31#define TDG_VP_VMCALL_INSTRUCTION_WRMSR 32 +#define TDG_VP_VMCALL_VE_REQUEST_MMIO 48 uint64_t tdg_vp_vmcall_instruction_io(uint64_t port, uint64_t size,
@@ -21,5 +22,7 @@ uint64_t tdg_vp_vmcall_get_td_vmcall_info(uint64_t *r11, uint64_t *r12,uint64_t tdg_vp_vmcall_instruction_rdmsr (uint64_t index,uint64_t *ret_value);uint64_t tdg_vp_vmcall_instruction_wrmsr (uint64_t index,uint64_t value);uint64_t tdg_vp_vmcall_instruction_hlt (uint64_t interrupt_blocked_flag); +uint64_t tdg_vp_vmcall_ve_request_mmio_read (uint64_t address,uint64_t size, +uint64_t *data_out);#endif // SELFTEST_TDX_TDX_H
tools/testing/selftests/kvm/include/x86_64/tdx/test_util.h +23 -0
@@ -35,6 +35,29 @@ (VCPU)->run->io.direction); \ }while (0 ) + +/** +* Assert that some MMIO operation involving TDG.VP.VMCALL <#VERequestMMIO> was +* called in the guest. + */ +#define TDX_TEST_ASSERT_MMIO (VCPU ,ADDR ,SIZE ,DIR ) \ + do { \ + TEST_ASSERT((VCPU)->run->exit_reason == KVM_EXIT_MMIO, \ + "Got exit_reason other than KVM_EXIT_MMIO: %u (%s)\n", \ + (VCPU)->run->exit_reason, \ + exit_reason_str((VCPU)->run->exit_reason)); \ + \ + TEST_ASSERT(((VCPU)->run->exit_reason == KVM_EXIT_MMIO) && \ + ((VCPU)->run->mmio.phys_addr == (ADDR)) && \ + ((VCPU)->run->mmio.len == (SIZE)) && \ + ((VCPU)->run->mmio.is_write == (DIR)), \ + "Got an unexpected MMIO exit values: %u (%s) %llu %d %d\n", \ + (VCPU)->run->exit_reason, \ + exit_reason_str((VCPU)->run->exit_reason), \ + (VCPU)->run->mmio.phys_addr, (VCPU)->run->mmio.len, \ + (VCPU)->run->mmio.is_write); \ + } while (0) +/** * Check and report if there was some failure in the guest, either an exception * like a triple fault, or if a tdx_test_fatal() was hit.
tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +19 -0
@@ -103,3 +103,22 @@ uint64_t tdg_vp_vmcall_instruction_hlt(uint64_t interrupt_blocked_flag)return __tdx_hypercall (&args,0 ); } + +uint64_t tdg_vp_vmcall_ve_request_mmio_read (uint64_t address,uint64_t size, +uint64_t *data_out) +{ +uint64_t ret; +struct tdx_hypercall_args args = { + .r11 =TDG_VP_VMCALL_VE_REQUEST_MMIO , + .r12 = size, + .r13 =TDG_VP_VMCALL_VE_REQUEST_MMIO_READ , + .r14 = address, + }; + + ret =__tdx_hypercall (&args,TDX_HCALL_HAS_OUTPUT ); + +if (data_out) + *data_out = args.r11; + +return ret; +}
tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +87 -0
99 lines not on this page · raw message
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 19/31 KVM: selftests: TDX: Add TDX MMIO writes test
In reply to Ackerley TngFrom: Sagi Shahar <[email protected]>
The test verifies MMIO writes of various sizes from the guest to the host.
--- .../selftests/kvm/include/x86_64/tdx/tdx.h | 2 + .../selftests/kvm/lib/x86_64/tdx/tdx.c | 14 +++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 85 +++++++++++++++++++ 3 files changed, 101 insertions(+)
tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +2 -0
@@ -24,5 +24,7 @@ uint64_t tdg_vp_vmcall_instruction_wrmsr(uint64_t index, uint64_t value);uint64_t tdg_vp_vmcall_instruction_hlt (uint64_t interrupt_blocked_flag);uint64_t tdg_vp_vmcall_ve_request_mmio_read (uint64_t address,uint64_t size,uint64_t *data_out); +uint64_t tdg_vp_vmcall_ve_request_mmio_write (uint64_t address,uint64_t size, +uint64_t data_in);#endif // SELFTEST_TDX_TDX_H
tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +14 -0
@@ -122,3 +122,17 @@ uint64_t tdg_vp_vmcall_ve_request_mmio_read(uint64_t address, uint64_t size,return ret; } + +uint64_t tdg_vp_vmcall_ve_request_mmio_write (uint64_t address,uint64_t size, +uint64_t data_in) +{ +struct tdx_hypercall_args args = { + .r11 =TDG_VP_VMCALL_VE_REQUEST_MMIO , + .r12 = size, + .r13 =TDG_VP_VMCALL_VE_REQUEST_MMIO_WRITE , + .r14 = address, + .r15 = data_in, + }; + +return __tdx_hypercall (&args,0 ); +}
tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +85 -0
97 lines not on this page · raw message
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 20/31 KVM: selftests: TDX: Add TDX CPUID TDVMCALL test
In reply to Ackerley TngFrom: Sagi Shahar <[email protected]>
This test issues a CPUID TDVMCALL from inside the guest to get the CPUID values as seen by KVM.
--- .../selftests/kvm/include/x86_64/tdx/tdx.h | 4 + .../selftests/kvm/lib/x86_64/tdx/tdx.c | 26 +++++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 94 +++++++++++++++++++ 3 files changed, 124 insertions(+)
tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +4 -0
@@ -7,6 +7,7 @@#define TDG_VP_VMCALL_GET_TD_VM_CALL_INFO 0x10000#define TDG_VP_VMCALL_REPORT_FATAL_ERROR 0x10003 +#define TDG_VP_VMCALL_INSTRUCTION_CPUID 10#define TDG_VP_VMCALL_INSTRUCTION_HLT 12#define TDG_VP_VMCALL_INSTRUCTION_IO 30#define TDG_VP_VMCALL_INSTRUCTION_RDMSR 31
@@ -26,5 +27,8 @@ uint64_t tdg_vp_vmcall_ve_request_mmio_read(uint64_t address, uint64_t size, uint64_t *data_out);uint64_t tdg_vp_vmcall_ve_request_mmio_write (uint64_t address,uint64_t size,uint64_t data_in); +uint64_t tdg_vp_vmcall_instruction_cpuid (uint32_t eax,uint32_t ecx, +uint32_t *ret_eax,uint32_t *ret_ebx, +uint32_t *ret_ecx,uint32_t *ret_edx);#endif // SELFTEST_TDX_TDX_H
tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +26 -0
@@ -136,3 +136,29 @@ uint64_t tdg_vp_vmcall_ve_request_mmio_write(uint64_t address, uint64_t size,return __tdx_hypercall (&args,0 ); } + +uint64_t tdg_vp_vmcall_instruction_cpuid (uint32_t eax,uint32_t ecx, +uint32_t *ret_eax,uint32_t *ret_ebx, +uint32_t *ret_ecx,uint32_t *ret_edx) +{ +uint64_t ret; +struct tdx_hypercall_args args = { + .r11 =TDG_VP_VMCALL_INSTRUCTION_CPUID , + .r12 = eax, + .r13 = ecx, + }; + + + ret =__tdx_hypercall (&args,TDX_HCALL_HAS_OUTPUT ); + +if (ret_eax) + *ret_eax = args.r12; +if (ret_ebx) + *ret_ebx = args.r13; +if (ret_ecx) + *ret_ecx = args.r14; +if (ret_edx) + *ret_edx = args.r15; + +return ret; +}
tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +94 -0
106 lines not on this page · raw message
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 21/31 KVM: selftests: TDX: Verify the behavior when host consumes a TD private memory
In reply to Ackerley TngFrom: Ryan Afranji <[email protected]>
The test checks that host can only read fixed values when trying to access the guest's private memory.
--- .../selftests/kvm/x86_64/tdx_vm_tests.c | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+)
tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +85 -0
@@ -1069,6 +1069,90 @@ void verify_td_cpuid_tdcall(void)printf ("\t ... PASSED\n" ); } +/* +* Shared variables between guest and host for host reading private mem test +*/ +static uint64_t tdx_test_host_read_private_mem_addr; +#define TDX_HOST_READ_PRIVATE_MEM_PORT_TEST 0x53 + +void guest_host_read_priv_mem (void ) +{ +uint64_t ret; +uint64_t placeholder =0 ; + +/* Set value */ + *((uint32_t *) tdx_test_host_read_private_mem_addr) =0xABCD ; + +/* Exit so host can read value */ + ret =tdg_vp_vmcall_instruction_io ( +TDX_HOST_READ_PRIVATE_MEM_PORT_TEST ,4 , +TDG_VP_VMCALL_INSTRUCTION_IO_WRITE , &placeholder); +if (ret) +tdx_test_fatal (ret); + +/* Update guest_var's value and have host reread it. */ + *((uint32_t *) tdx_test_host_read_private_mem_addr) =0xFEDC ; + +tdx_test_success (); +} + +void verify_host_reading_private_mem (void ) +{ +struct kvm_vm *vm; +struct kvm_vcpu *vcpu; + +vm_vaddr_t test_page; +uint64_t *host_virt; +uint64_t first_host_read; +uint64_t second_host_read; + + vm =td_create (); +td_initialize (vm,VM_MEM_SRC_ANONYMOUS ,0 ); + vcpu =td_vcpu_add (vm,0 , guest_host_read_priv_mem); + + test_page =vm_vaddr_alloc_page (vm); +TEST_ASSERT (test_page <BIT_ULL (32 ), +"Test address should fit in 32 bits so it can be sent to the guest" ); + + host_virt =addr_gva2hva (vm, test_page); +TEST_ASSERT (host_virt !=NULL , +"Guest address not found in guest memory regions\n" ); + + tdx_test_host_read_private_mem_addr = test_page; +sync_global_to_guest (vm, tdx_test_host_read_private_mem_addr); + +td_finalize (vm); + +printf ("Verifying host's behavior when reading TD private memory:\n" ); + +vcpu_run (vcpu); +TDX_TEST_CHECK_GUEST_FAILURE (vcpu); +TDX_TEST_ASSERT_IO (vcpu,TDX_HOST_READ_PRIVATE_MEM_PORT_TEST , +4 ,TDG_VP_VMCALL_INSTRUCTION_IO_WRITE ); +printf ("\t ... Guest's variable contains 0xABCD\n" ); + +/* Host reads guest's variable. */ + first_host_read = *host_virt; +printf ("\t ... Host's read attempt value: %lu\n" , first_host_read); + +/* Guest updates variable and host rereads it. */ +vcpu_run (vcpu); +TDX_TEST_CHECK_GUEST_FAILURE (vcpu); +printf ("\t ... Guest's variable updated to 0xFEDC\n" ); + + second_host_read = *host_virt; +printf ("\t ... Host's second read attempt value: %lu\n" , + second_host_read); + +TEST_ASSERT (first_host_read == second_host_read, +"Host did not read a fixed pattern\n" ); + +printf ("\t ... Fixed pattern was returned to the host\n" ); + +kvm_vm_free (vm); +printf ("\t ... PASSED\n" ); +} +int main (int argc,char **argv) {setbuf (stdout,NULL );
@@ -1091,6 +1175,7 @@ int main(int argc, char **argv)run_in_new_process (&verify_mmio_reads);run_in_new_process (&verify_mmio_writes);run_in_new_process (&verify_td_cpuid_tdcall); +run_in_new_process (&verify_host_reading_private_mem);return 0 ; }
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 22/31 KVM: selftests: TDX: Add TDG.VP.INFO test
In reply to Ackerley TngFrom: Roger Wang <[email protected]>
Adds a test for TDG.VP.INFO
---
Changes RFCv2 -> RFCv3
+ Use KVM_CAP_MAX_VCPUS to set max_vcpus, check that it was passed to the TDX module by reading it back
--- .../selftests/kvm/include/x86_64/tdx/tdcall.h | 19 +++ .../selftests/kvm/include/x86_64/tdx/tdx.h | 6 + .../selftests/kvm/lib/x86_64/tdx/tdcall.S | 68 ++++++++ .../selftests/kvm/lib/x86_64/tdx/tdx.c | 27 ++++ .../selftests/kvm/x86_64/tdx_vm_tests.c | 148 ++++++++++++++++++ 5 files changed, 268 insertions(+)
tools/testing/selftests/kvm/include/x86_64/tdx/tdcall.h +19 -0
@@ -37,4 +37,23 @@ struct tdx_hypercall_args {/* Used to request services from the VMM */ u64 __tdx_hypercall (struct tdx_hypercall_args *args,unsigned long flags); +/* +* Used to gather the output registers values of the TDCALL and SEAMCALL +* instructions when requesting services from the TDX module. +* +* This is a software only structure and not part of the TDX module/VMM ABI. +*/ +struct tdx_module_output { +u64 rcx; +u64 rdx; +u64 r8; +u64 r9; +u64 r10; +u64 r11; +}; + +/* Used to communicate with the TDX module */ +u64 __tdx_module_call (u64 fn,u64 rcx,u64 rdx,u64 r8,u64 r9, +struct tdx_module_output *out); +#endif // SELFTESTS_TDX_TDCALL_H
tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +6 -0
@@ -4,6 +4,8 @@#include <stdint.h> +#define TDG_VP_INFO 1 +#define TDG_VP_VMCALL_GET_TD_VM_CALL_INFO 0x10000#define TDG_VP_VMCALL_REPORT_FATAL_ERROR 0x10003
@@ -31,4 +33,8 @@ uint64_t tdg_vp_vmcall_instruction_cpuid(uint32_t eax, uint32_t ecx, uint32_t *ret_eax, uint32_t *ret_ebx, uint32_t *ret_ecx, uint32_t *ret_edx); +uint64_t tdg_vp_info (uint64_t *rcx,uint64_t *rdx, +uint64_t *r8,uint64_t *r9, +uint64_t *r10,uint64_t *r11); +#endif // SELFTEST_TDX_TDX_H
tools/testing/selftests/kvm/lib/x86_64/tdx/tdcall.S +68 -0
73 lines not on this page · raw message
tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +27 -0
@@ -162,3 +162,30 @@ uint64_t tdg_vp_vmcall_instruction_cpuid(uint32_t eax, uint32_t ecx,return ret; } + +uint64_t tdg_vp_info (uint64_t *rcx,uint64_t *rdx, +uint64_t *r8,uint64_t *r9, +uint64_t *r10,uint64_t *r11) +{ +uint64_t ret; +struct tdx_module_output out; + +memset (&out,0 ,sizeof (struct tdx_module_output )); + + ret =__tdx_module_call (TDG_VP_INFO ,0 ,0 ,0 ,0 , &out); + +if (rcx) + *rcx = out.rcx; +if (rdx) + *rdx = out.rdx; +if (r8) + *r8 = out.r8; +if (r9) + *r9 = out.r9; +if (r10) + *r10 = out.r10; +if (r11) + *r11 = out.r11; + +return ret; +}
tools/testing/selftests/kvm/x86_64/tdx_vm_tests.c +148 -0
160 lines not on this page · raw message
--
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 23/31 KVM: selftests: Add functions to allow mapping as shared
In reply to Ackerley Tng--- .../selftests/kvm/include/kvm_util_base.h | 24 ++++++++++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 32 +++++++++++++++++++ .../selftests/kvm/lib/x86_64/processor.c | 15 +++++++-- 3 files changed, 69 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +24 -0
@@ -412,6 +412,8 @@ vm_vaddr_t vm_vaddr_alloc_page(struct kvm_vm *vm);void virt_map (struct kvm_vm *vm,uint64_t vaddr,uint64_t paddr,unsigned int npages); +void virt_map_shared (struct kvm_vm *vm,uint64_t vaddr,uint64_t paddr, +unsigned int npages);void *addr_gpa2hva (struct kvm_vm *vm,vm_paddr_t gpa);void *addr_gva2hva (struct kvm_vm *vm,vm_vaddr_t gva);vm_paddr_t addr_hva2gpa (struct kvm_vm *vm,void *hva);
@@ -843,6 +845,28 @@ static inline void virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddrvirt_arch_pg_map (vm, vaddr, paddr); } +/* +* VM Virtual Page Map as Shared +* +* Input Args: +* vm - Virtual Machine +* vaddr - VM Virtual Address +* paddr - VM Physical Address +* memslot - Memory region slot for new virtual translation tables +* +* Output Args: None +* +* Return: None +* +* Within @vm, creates a virtual translation for the page starting +* at @vaddr to the page starting at @paddr. +*/ +void virt_arch_pg_map_shared (struct kvm_vm *vm,uint64_t vaddr,uint64_t paddr); + +static inline void virt_pg_map_shared (struct kvm_vm *vm,uint64_t vaddr,uint64_t paddr) +{ +virt_arch_pg_map_shared (vm, vaddr, paddr); +}/* * Address Guest Virtual to Guest Physical
tools/testing/selftests/kvm/lib/kvm_util.c +32 -0
@@ -1384,6 +1384,38 @@ void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, } } +/* +* Map a range of VM virtual address to the VM's physical address as shared +* +* Input Args: +* vm - Virtual Machine +* vaddr - Virtuall address to map +* paddr - VM Physical Address +* npages - The number of pages to map +* +* Output Args: None +* +* Return: None +* +* Within the VM given by @vm, creates a virtual translation for +* @npages starting at @vaddr to the page range starting at @paddr. +*/ +void virt_map_shared (struct kvm_vm *vm,uint64_t vaddr,uint64_t paddr, +unsigned int npages) +{ +size_t page_size = vm->page_size; +size_t size = npages * page_size; + +TEST_ASSERT (vaddr + size > vaddr,"Vaddr overflow" ); +TEST_ASSERT (paddr + size > paddr,"Paddr overflow" ); + +while (npages--) { +virt_pg_map_shared (vm, vaddr, paddr); + vaddr += page_size; + paddr += page_size; + } +} +/* * Address VM Physical to Host Virtual *
tools/testing/selftests/kvm/lib/x86_64/processor.c +13 -2
@@ -176,7 +176,8 @@ static uint64_t *virt_create_upper_pte(struct kvm_vm *vm,return pte; } -void __virt_pg_map (struct kvm_vm *vm,uint64_t vaddr,uint64_t paddr,int level) +static void ___virt_pg_map (struct kvm_vm *vm,uint64_t vaddr,uint64_t paddr, +int level,bool protected) {const uint64_t pg_size =PG_LEVEL_SIZE (level);uint64_t *pml4e, *pdpe, *pde;
@@ -223,17 +224,27 @@ void __virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, int level) "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)) +if (protected) *pte |= vm->arch.c_bit;else *pte |= vm->arch.s_bit; } +void __virt_pg_map (struct kvm_vm *vm,uint64_t vaddr,uint64_t paddr,int level) +{ +___virt_pg_map (vm, vaddr, paddr, level,vm_is_gpa_protected (vm, paddr)); +} +void virt_arch_pg_map (struct kvm_vm *vm,uint64_t vaddr,uint64_t paddr) {__virt_pg_map (vm, vaddr, paddr,PG_LEVEL_4K ); } +void virt_arch_pg_map_shared (struct kvm_vm *vm,uint64_t vaddr,uint64_t paddr) +{ +___virt_pg_map (vm, vaddr, paddr,PG_LEVEL_4K , false); +} +void virt_map_level (struct kvm_vm *vm,uint64_t vaddr,uint64_t paddr,uint64_t nr_bytes,int level) {
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 24/31 KVM: selftests: TDX: Add shared memory test
In reply to Ackerley TngFrom: Ryan Afranji <[email protected]>
Adds a test that sets up shared memory between the host and guest.
--- tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/include/x86_64/tdx/tdx.h | 2 + .../kvm/include/x86_64/tdx/tdx_util.h | 3 + .../selftests/kvm/lib/x86_64/tdx/tdx.c | 16 ++ .../selftests/kvm/lib/x86_64/tdx/tdx_util.c | 30 ++++ .../kvm/x86_64/tdx_shared_mem_test.c | 137 ++++++++++++++++++ 7 files changed, 190 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/tdx_shared_mem_test.c
tools/testing/selftests/kvm/.gitignore +1 -0
@@ -66,6 +66,7 @@ /x86_64/vmx_pmu_caps_test /x86_64/triple_fault_event_test /x86_64/tdx_vm_tests +/x86_64/tdx_shared_mem_test /access_tracking_perf_test /demand_paging_test /dirty_log_test
tools/testing/selftests/kvm/Makefile +1 -0
@@ -152,6 +152,7 @@ TEST_GEN_PROGS_x86_64 += steal_time TEST_GEN_PROGS_x86_64 += kvm_binary_stats_test TEST_GEN_PROGS_x86_64 += system_counter_offset_test TEST_GEN_PROGS_x86_64 += x86_64/tdx_vm_tests +TEST_GEN_PROGS_x86_64 += x86_64/tdx_shared_mem_test # Compiled outputs used by test targets TEST_GEN_PROGS_EXTENDED_x86_64 += x86_64/nx_huge_pages_test
tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +2 -0
@@ -7,6 +7,7 @@#define TDG_VP_INFO 1#define TDG_VP_VMCALL_GET_TD_VM_CALL_INFO 0x10000 +#define TDG_VP_VMCALL_MAP_GPA 0x10001#define TDG_VP_VMCALL_REPORT_FATAL_ERROR 0x10003#define TDG_VP_VMCALL_INSTRUCTION_CPUID 10
@@ -36,5 +37,6 @@ uint64_t tdg_vp_vmcall_instruction_cpuid(uint32_t eax, uint32_t ecx,uint64_t tdg_vp_info (uint64_t *rcx,uint64_t *rdx,uint64_t *r8,uint64_t *r9,uint64_t *r10,uint64_t *r11); +uint64_t tdg_vp_vmcall_map_gpa (uint64_t address,uint64_t size,uint64_t *data_out);#endif // SELFTEST_TDX_TDX_H
tools/testing/selftests/kvm/include/x86_64/tdx/tdx_util.h +3 -0
@@ -13,4 +13,7 @@ void td_initialize(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, uint64_t attributes);void td_finalize (struct kvm_vm *vm); +void handle_memory_conversion (struct kvm_vm *vm,uint64_t gpa,uint64_t size, +bool shared_to_private); +#endif // SELFTESTS_TDX_KVM_UTIL_H
tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +16 -0
@@ -189,3 +189,19 @@ uint64_t tdg_vp_info(uint64_t *rcx, uint64_t *rdx,return ret; } + +uint64_t tdg_vp_vmcall_map_gpa (uint64_t address,uint64_t size,uint64_t *data_out) +{ +uint64_t ret; +struct tdx_hypercall_args args = { + .r11 =TDG_VP_VMCALL_MAP_GPA , + .r12 = address, + .r13 = size + }; + + ret =__tdx_hypercall (&args,TDX_HCALL_HAS_OUTPUT ); + +if (data_out) + *data_out = args.r11; +return ret; +}
tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c +30 -0
@@ -505,3 +505,33 @@ void td_finalize(struct kvm_vm *vm)tdx_td_finalizemr (vm); } + +/** +* Handle conversion of memory with @size beginning @gpa for @vm. Set +* @shared_to_private to true for shared to private conversions and false +* otherwise. +* +* Since this is just for selftests, we will just keep both pieces of backing +* memory allocated and not deallocate/allocate memory; we'll just do the +* minimum of calling KVM_MEMORY_ENCRYPT_REG_REGION and +* KVM_MEMORY_ENCRYPT_UNREG_REGION. +*/ +void handle_memory_conversion (struct kvm_vm *vm,uint64_t gpa,uint64_t size, +bool shared_to_private) +{ +struct kvm_enc_region range; +char *ioctl_string = shared_to_private + ?"KVM_MEMORY_ENCRYPT_REG_REGION" + :"KVM_MEMORY_ENCRYPT_UNREG_REGION" ; + + range.addr = gpa; + range.size = size; + +printf ("\t ... calling %s ioctl with gpa=%#lx, size=%#lx\n" , + ioctl_string, gpa, size); + +if (shared_to_private) +vm_ioctl (vm,KVM_MEMORY_ENCRYPT_REG_REGION , &range); +else +vm_ioctl (vm,KVM_MEMORY_ENCRYPT_UNREG_REGION , &range); +}
tools/testing/selftests/kvm/x86_64/tdx_shared_mem_test.c new file +137 -0
137 lines not on this page · raw message
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 25/31 KVM: selftests: Add support for restricted memory
In reply to Ackerley TngWith this, vm_userspace_mem_region_add() can use restricted memory to back guest memory.
--- .../selftests/kvm/include/kvm_util_base.h | 7 ++- .../testing/selftests/kvm/include/test_util.h | 2 + tools/testing/selftests/kvm/lib/kvm_util.c | 48 ++++++++++++++++--- tools/testing/selftests/kvm/lib/test_util.c | 7 +++ 4 files changed, 55 insertions(+), 9 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +5 -2
@@ -33,7 +33,10 @@ typedef uint64_t vm_paddr_t; /* Virtual Machine (Guest) physical address */typedef uint64_t vm_vaddr_t ;/* Virtual Machine (Guest) virtual address */ struct userspace_mem_region { -struct kvm_userspace_memory_region region; +union { +struct kvm_userspace_memory_region region; +struct kvm_userspace_memory_region_ext region_ext; + };struct sparsebit *unused_phy_pages;struct sparsebit *protected_phy_pages;int fd;
@@ -214,7 +217,7 @@ static inline bool kvm_has_cap(long cap)#define kvm_do_ioctl (fd, cmd, arg) \ ({ \ - static_assert(!_IOC_SIZE(cmd) || sizeof(*arg) == _IOC_SIZE(cmd), ""); \ + static_assert(!_IOC_SIZE(cmd) || sizeof(*arg) >= _IOC_SIZE(cmd), ""); \ ioctl(fd, cmd, arg); \ })
tools/testing/selftests/kvm/include/test_util.h +2 -0
@@ -94,6 +94,7 @@ enum vm_mem_backing_src_type {VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB ,VM_MEM_SRC_ANONYMOUS_HUGETLB_2GB ,VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB , +VM_MEM_SRC_ANONYMOUS_AND_RESTRICTED_MEMFD ,VM_MEM_SRC_SHMEM ,VM_MEM_SRC_SHARED_HUGETLB , NUM_SRC_TYPES,
@@ -104,6 +105,7 @@ enum vm_mem_backing_src_type {struct vm_mem_backing_src_alias {const char *name;uint32_t flag; +bool need_restricted_memfd; };#define MIN_RUN_DELAY_NS 200000UL
tools/testing/selftests/kvm/lib/kvm_util.c +41 -7
87 lines not on this page · raw message
tools/testing/selftests/kvm/lib/test_util.c +7 -0
@@ -8,6 +8,7 @@#include <assert.h> #include <ctype.h> #include <limits.h> +#include <stdbool.h> #include <stdlib.h> #include <time.h> #include <sys/stat.h>
@@ -254,6 +255,11 @@ const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i)*/ .flag = MAP_SHARED, }, + [VM_MEM_SRC_ANONYMOUS_AND_RESTRICTED_MEMFD] = { + .name = "anonymous_and_restricted_memfd", + .flag = ANON_FLAGS, + .need_restricted_memfd = true, + }, };_Static_assert (ARRAY_SIZE (aliases) ==NUM_SRC_TYPES ,"Missing new backing src types?" );
@@ -272,6 +278,7 @@ size_t get_backing_src_pagesz(uint32_t i)switch (i) {case VM_MEM_SRC_ANONYMOUS :case VM_MEM_SRC_SHMEM : +case VM_MEM_SRC_ANONYMOUS_AND_RESTRICTED_MEMFD :return getpagesize ();case VM_MEM_SRC_ANONYMOUS_THP :return get_trans_hugepagesz ();
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 26/31 KVM: selftests: TDX: Update load_td_memory_region for VM memory backed by restricted memfd
In reply to Ackerley TngIf guest memory is backed by restricted memfd
+ UPM is being used, hence encrypted memory region has to be registered
+ Can avoid making a copy of guest memory before getting TDX to initialize the memory region
--- .../selftests/kvm/lib/x86_64/tdx/tdx_util.c | 43 +++++++++++++++---- 1 file changed, 34 insertions(+), 9 deletions(-)
tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c +34 -9
@@ -207,6 +207,23 @@ static void tdx_td_finalizemr(struct kvm_vm *vm)tdx_ioctl (vm->fd,KVM_TDX_FINALIZE_VM ,0 ,NULL ); } +/* +* Other ioctls +*/ + +/** +* Register a memory region that may contain encrypted data in KVM. +*/ +static void register_encrypted_memory_region ( +struct kvm_vm *vm,struct userspace_mem_region *region) +{ +struct kvm_enc_region range = { + .addr = region->region.guest_phys_addr, + .size = region->region.memory_size, + }; +vm_ioctl (vm,KVM_MEMORY_ENCRYPT_REG_REGION , &range); +} +/* * TD creation/setup/finalization */
@@ -393,30 +410,38 @@ static void load_td_memory_region(struct kvm_vm *vm,if (!sparsebit_any_set (pages))return ; + +if (region->region_ext.restricted_fd !=-1 ) +register_encrypted_memory_region (vm, region); +sparsebit_for_each_set_range (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 hva = hva_base + offset;const uint64_t gpa = gpa_base + offset; -void *source_addr; +void *source_addr = (void *)hva;/* * KVM_TDX_INIT_MEM_REGION ioctl cannot encrypt memory in place, * hence we have to make a copy if there's only one backing * memory source */ - source_addr =mmap (NULL , size_to_load,PROT_READ |PROT_WRITE , -MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 ); -TEST_ASSERT ( - source_addr, -"Could not allocate memory for loading memory region" ); - -memcpy (source_addr, (void *)hva, size_to_load); +if (region->region_ext.restricted_fd ==-1 ) { + source_addr =mmap (NULL , size_to_load,PROT_READ |PROT_WRITE , +MAP_ANONYMOUS |MAP_PRIVATE ,-1 ,0 ); +TEST_ASSERT ( + source_addr, +"Could not allocate memory for loading memory region" ); + +memcpy (source_addr, (void *)hva, size_to_load); +memset ((void *)hva,0 , size_to_load); + }tdx_init_mem_region (vm, source_addr, gpa, size_to_load); -munmap (source_addr, size_to_load); +if (region->region_ext.restricted_fd ==-1 ) +munmap (source_addr, size_to_load); } }
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 27/31 KVM: selftests: Expose _vm_vaddr_alloc
In reply to Ackerley Tngvm_vaddr_alloc always allocates memory in memslot 0. This allows users of this function to choose which memslot to allocate virtual memory in.
--- tools/testing/selftests/kvm/include/kvm_util_base.h | 3 +++ tools/testing/selftests/kvm/lib/kvm_util.c | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +3 -0
@@ -406,6 +406,9 @@ 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);struct kvm_vcpu *__vm_vcpu_add (struct kvm_vm *vm,uint32_t vcpu_id); +vm_vaddr_t +_vm_vaddr_alloc (struct kvm_vm *vm,size_t sz,vm_vaddr_t vaddr_min, +vm_paddr_t paddr_min,uint32_t data_memslot,bool encrypt);vm_vaddr_t vm_vaddr_alloc (struct kvm_vm *vm,size_t sz,vm_vaddr_t vaddr_min);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_1to1(struct kvm_vm *vm, size_t sz, vm_vaddr_t vaddr_min,
tools/testing/selftests/kvm/lib/kvm_util.c +1 -1
@@ -1286,7 +1286,7 @@ static vm_vaddr_t vm_vaddr_unused_gap(struct kvm_vm *vm, size_t sz,* a unique set of pages, with the minimum real allocation being at least * a page. */ -static vm_vaddr_t +vm_vaddr_t _vm_vaddr_alloc (struct kvm_vm *vm,size_t sz,vm_vaddr_t vaddr_min,vm_paddr_t paddr_min,uint32_t data_memslot,bool encrypt) {
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 28/31 KVM: selftests: TDX: Add support for TDG.MEM.PAGE.ACCEPT
In reply to Ackerley Tng--- tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h | 2 ++ tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c | 5 +++++ 2 files changed, 7 insertions(+)
tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +2 -0
@@ -5,6 +5,7 @@#include <stdint.h> #define TDG_VP_INFO 1 +#define TDG_MEM_PAGE_ACCEPT 6#define TDG_VP_VMCALL_GET_TD_VM_CALL_INFO 0x10000#define TDG_VP_VMCALL_MAP_GPA 0x10001
@@ -38,5 +39,6 @@ uint64_t tdg_vp_info(uint64_t *rcx, uint64_t *rdx, uint64_t *r8, uint64_t *r9, uint64_t *r10, uint64_t *r11);uint64_t tdg_vp_vmcall_map_gpa (uint64_t address,uint64_t size,uint64_t *data_out); +uint64_t tdg_mem_page_accept (uint64_t gpa,uint8_t level);#endif // SELFTEST_TDX_TDX_H
tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +5 -0
@@ -205,3 +205,8 @@ uint64_t tdg_vp_vmcall_map_gpa(uint64_t address, uint64_t size, uint64_t *data_o *data_out = args.r11;return ret; } + +uint64_t tdg_mem_page_accept (uint64_t gpa,uint8_t level) +{ +return __tdx_module_call (TDG_MEM_PAGE_ACCEPT , gpa | level,0 ,0 ,0 ,NULL ); +}
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 29/31 KVM: selftests: TDX: Add support for TDG.VP.VEINFO.GET
In reply to Ackerley Tng--- .../selftests/kvm/include/x86_64/tdx/tdx.h | 21 +++++++++++++++++++ .../selftests/kvm/lib/x86_64/tdx/tdx.c | 19 +++++++++++++++++ 2 files changed, 40 insertions(+)
tools/testing/selftests/kvm/include/x86_64/tdx/tdx.h +21 -0
@@ -5,6 +5,7 @@#include <stdint.h> #define TDG_VP_INFO 1 +#define TDG_VP_VEINFO_GET 3#define TDG_MEM_PAGE_ACCEPT 6#define TDG_VP_VMCALL_GET_TD_VM_CALL_INFO 0x10000
@@ -41,4 +42,24 @@ uint64_t tdg_vp_info(uint64_t *rcx, uint64_t *rdx,uint64_t tdg_vp_vmcall_map_gpa (uint64_t address,uint64_t size,uint64_t *data_out);uint64_t tdg_mem_page_accept (uint64_t gpa,uint8_t level); +/* +* Used by the #VE exception handler to gather the #VE exception +* info from the TDX module. This is a software only structure +* and not part of the TDX module/VMM ABI. +* +* Adapted from arch/x86/include/asm/tdx.h +*/ +struct ve_info { +uint64_t exit_reason; +uint64_t exit_qual; +/* Guest Linear (virtual) Address */ +uint64_t gla; +/* Guest Physical Address */ +uint64_t gpa; +uint32_t instr_len; +uint32_t instr_info; +}; + +uint64_t tdg_vp_veinfo_get (struct ve_info *ve); +#endif // SELFTEST_TDX_TDX_H
tools/testing/selftests/kvm/lib/x86_64/tdx/tdx.c +19 -0
@@ -210,3 +210,22 @@ uint64_t tdg_mem_page_accept(uint64_t gpa, uint8_t level) {return __tdx_module_call (TDG_MEM_PAGE_ACCEPT , gpa | level,0 ,0 ,0 ,NULL ); } + +uint64_t tdg_vp_veinfo_get (struct ve_info *ve) +{ +uint64_t ret; +struct tdx_module_output out; + +memset (&out,0 ,sizeof (struct tdx_module_output )); + + ret =__tdx_module_call (TDG_VP_VEINFO_GET ,0 ,0 ,0 ,0 , &out); + + ve->exit_reason = out.rcx; + ve->exit_qual = out.rdx; + ve->gla = out.r8; + ve->gpa = out.r9; + ve->instr_len = out.r10 &0xffffffff ; + ve->instr_info = out.r10 >>32 ; + +return ret; +}
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 30/31 KVM: selftests: TDX: Add TDX UPM selftest
In reply to Ackerley TngThis tests the use of restricted memory with explicit MapGPA calls.
--- tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/x86_64/tdx_upm_test.c | 392 ++++++++++++++++++ 3 files changed, 394 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/tdx_upm_test.c
tools/testing/selftests/kvm/.gitignore +1 -0
@@ -67,6 +67,7 @@ /x86_64/triple_fault_event_test /x86_64/tdx_vm_tests /x86_64/tdx_shared_mem_test +/x86_64/tdx_upm_test /access_tracking_perf_test /demand_paging_test /dirty_log_test
tools/testing/selftests/kvm/Makefile +1 -0
@@ -153,6 +153,7 @@ TEST_GEN_PROGS_x86_64 += kvm_binary_stats_test TEST_GEN_PROGS_x86_64 += system_counter_offset_test TEST_GEN_PROGS_x86_64 += x86_64/tdx_vm_tests TEST_GEN_PROGS_x86_64 += x86_64/tdx_shared_mem_test +TEST_GEN_PROGS_x86_64 += x86_64/tdx_upm_test # Compiled outputs used by test targets TEST_GEN_PROGS_EXTENDED_x86_64 += x86_64/nx_huge_pages_test
tools/testing/selftests/kvm/x86_64/tdx_upm_test.c new file +392 -0
392 lines not on this page · raw message
Signature · 1 line
2.39.0.246.g2a6d74b583-goog
RFC PATCH v3 31/31 KVM: selftests: TDX: Add TDX UPM selftests for implicit conversion
In reply to Ackerley TngThis tests the use of restricted memory without explicit MapGPA calls.
--- .../selftests/kvm/x86_64/tdx_upm_test.c | 88 ++++++++++++++++--- 1 file changed, 78 insertions(+), 10 deletions(-)
tools/testing/selftests/kvm/x86_64/tdx_upm_test.c +78 -10
@@ -149,11 +149,18 @@ enum {* Does vcpu_run, and also manages memory conversions if requested by the TD. */ void vcpu_run_and_manage_memory_conversions (struct kvm_vm *vm, -struct kvm_vcpu *vcpu) +struct kvm_vcpu *vcpu,bool handle_conversions) {for (;;) {vcpu_run (vcpu); -if ( +if (handle_conversions && + vcpu->run->exit_reason ==KVM_EXIT_MEMORY_FAULT ) { +handle_memory_conversion ( + vm, vcpu->run->memory.gpa, + vcpu->run->memory.size, + vcpu->run->memory.flags ==KVM_MEMORY_EXIT_FLAG_PRIVATE ); +continue ; + }else if ( vcpu->run->exit_reason ==KVM_EXIT_IO && vcpu->run->io.port ==TDX_UPM_TEST_ACCEPT_PRINT_PORT ) { uint64_t gpa = tdx_test_read_64bit(
@@ -233,8 +240,53 @@ static void guest_upm_explicit(void)tdx_test_success (); } +static void guest_upm_implicit (void ) +{ +struct tdx_upm_test_area *test_area_gva_private = + (struct tdx_upm_test_area *)TDX_UPM_TEST_AREA_GVA_PRIVATE ; +struct tdx_upm_test_area *test_area_gva_shared = + (struct tdx_upm_test_area *)TDX_UPM_TEST_AREA_GVA_SHARED ; + +/* Check: host reading private memory does not modify guest's view */ +fill_test_area (test_area_gva_private,PATTERN_GUEST_GENERAL ); + +tdx_test_report_to_user_space (SYNC_CHECK_READ_PRIVATE_MEMORY_FROM_HOST ); + +TDX_UPM_TEST_ASSERT ( +check_test_area (test_area_gva_private,PATTERN_GUEST_GENERAL )); + +/* Use focus area as shared */ +fill_focus_area (test_area_gva_shared,PATTERN_GUEST_FOCUS ); + +/* General areas should not be affected */ +TDX_UPM_TEST_ASSERT ( +check_general_areas (test_area_gva_private,PATTERN_GUEST_GENERAL )); + +tdx_test_report_to_user_space (SYNC_CHECK_READ_SHARED_MEMORY_FROM_HOST ); + +/* Check that guest has the same view of shared memory */ +TDX_UPM_TEST_ASSERT ( +check_focus_area (test_area_gva_shared,PATTERN_HOST_FOCUS )); + +/* Use focus area as private */ +fill_focus_area (test_area_gva_private,PATTERN_GUEST_FOCUS ); + +/* General areas should be unaffected by remapping */ +TDX_UPM_TEST_ASSERT ( +check_general_areas (test_area_gva_private,PATTERN_GUEST_GENERAL )); + +tdx_test_report_to_user_space (SYNC_CHECK_READ_PRIVATE_MEMORY_FROM_HOST_AGAIN ); + +/* Check that guest can use private memory after focus area is remapped as private */ +TDX_UPM_TEST_ASSERT ( +fill_and_check (test_area_gva_private,PATTERN_GUEST_GENERAL )); + +tdx_test_success (); +} +static void run_selftest (struct kvm_vm *vm,struct kvm_vcpu *vcpu, -struct tdx_upm_test_area *test_area_base_hva) +struct tdx_upm_test_area *test_area_base_hva, +bool implicit) {vcpu_run (vcpu);TDX_TEST_CHECK_GUEST_FAILURE (vcpu);
@@ -253,7 +305,7 @@ static void run_selftest(struct kvm_vm *vm, struct kvm_vcpu *vcpu,TEST_ASSERT (check_test_area (test_area_base_hva,PATTERN_CONFIDENCE_CHECK ),"Host should read PATTERN_CONFIDENCE_CHECK from guest's private memory." ); -vcpu_run_and_manage_memory_conversions (vm, vcpu); +vcpu_run_and_manage_memory_conversions (vm, vcpu, implicit);TDX_TEST_CHECK_GUEST_FAILURE (vcpu);TDX_TEST_ASSERT_IO (vcpu,TDX_TEST_REPORT_PORT ,TDX_TEST_REPORT_SIZE ,TDG_VP_VMCALL_INSTRUCTION_IO_WRITE );
@@ -270,7 +322,7 @@ static void run_selftest(struct kvm_vm *vm, struct kvm_vcpu *vcpu,TEST_ASSERT (check_focus_area (test_area_base_hva,PATTERN_HOST_FOCUS ),"Host should be able to use shared memory." ); -vcpu_run_and_manage_memory_conversions (vm, vcpu); +vcpu_run_and_manage_memory_conversions (vm, vcpu, implicit);TDX_TEST_CHECK_GUEST_FAILURE (vcpu);TDX_TEST_ASSERT_IO (vcpu,TDX_TEST_REPORT_PORT ,TDX_TEST_REPORT_SIZE ,TDG_VP_VMCALL_INSTRUCTION_IO_WRITE );
@@ -319,18 +371,20 @@ static void guest_ve_handler(struct ex_regs *regs)TDX_UPM_TEST_ASSERT (!ret); } -static void verify_upm_test (void ) +static void verify_upm_test (bool implicit) {struct kvm_vm *vm;struct kvm_vcpu *vcpu; +void *guest_code;vm_vaddr_t test_area_gva_private;struct tdx_upm_test_area *test_area_base_hva;uint64_t test_area_npages; vm =td_create ();td_initialize (vm,VM_MEM_SRC_ANONYMOUS ,0 ); - vcpu =td_vcpu_add (vm,0 , guest_upm_explicit); + guest_code = implicit ? guest_upm_implicit : guest_upm_explicit; + vcpu =td_vcpu_add (vm,0 , guest_code);vm_install_exception_handler (vm,VE_VECTOR , guest_ve_handler);
@@ -370,13 +424,26 @@ static void verify_upm_test(void)td_finalize (vm); -printf ("Verifying UPM functionality: explicit MapGPA\n" ); +if (implicit) +printf ("Verifying UPM functionality: implicit conversion\n" ); +else +printf ("Verifying UPM functionality: explicit MapGPA\n" ); -run_selftest (vm, vcpu, test_area_base_hva); +run_selftest (vm, vcpu, test_area_base_hva, implicit);kvm_vm_free (vm); } +void verify_upm_test_explicit (void ) +{ +verify_upm_test (false); +} + +void verify_upm_test_implicit (void ) +{ +verify_upm_test (true); +} +int main (int argc,char **argv) {/* Disable stdout buffering */
@@ -388,5 +455,6 @@ int main(int argc, char **argv)return 0 ; } -run_in_new_process (&verify_upm_test); +run_in_new_process (&verify_upm_test_explicit); +run_in_new_process (&verify_upm_test_implicit); }
Signature · 1 line
2.39.0.246.g2a6d74b583-goog