TDX KVM selftests
Subsystems (5)
v9 cover TDX KVM selftests
This is v9 of the TDX selftests.
Thanks everyone for the thorough review on v8 [1]. I tried addressing all the comments. I'm terribly sorry if I missed something.
The original v8 series [1] was split to make reviewing the test framework changes easier. This series includes the original patches up to the TDX lifecycle test which is the first TDX selftest in the series.
This series is based on v6.17-rc2
Changes from v8:
- Rebased on top of v6.17-rc2
- Drop several patches which are no longer needed now that TDX support is integrated into the common flow.
- Split several patches to make reviewing easier.
- Massive refactor compared to v8 to pull TDX special handling into __vm_create() and vm_vcpu_add() instead of creating separate functions for TDX.
- Use kbuild to expose values from c to assembly code.
- Move setup of the reset vectors to c code as suggested by Sean.
- Drop redundant cpuid masking functions which are no longer necessary.
- Initialize TDX protected pages one at a time instead of allocating large chinks of memory.
- Add UCALL support for TDX to align with the rest of the selftests.
- Minor fixes to kselftest_harness.h and virt_map() that were identified as part of this work.
[1] https://lore.kernel.org/lkml/[email protected]/
Ackerley Tng (2):
KVM: selftests: Add helpers to init TDX memory and finalize VM
KVM: selftests: Add ucall support for TDX
Erdem Aktas (2):
KVM: selftests: Add TDX boot code
KVM: selftests: Add support for TDX TDCALL from guest
Isaku Yamahata (2):
KVM: selftests: Update kvm_init_vm_address_properties() for TDX
KVM: selftests: TDX: Use KVM_TDX_CAPABILITIES to validate TDs'
attribute configuration
Sagi Shahar (13):
KVM: selftests: Include overflow.h instead of redefining
is_signed_type()
KVM: selftests: Allocate pgd in virt_map() as necessary
KVM: selftests: Expose functions to get default sregs values
KVM: selftests: Expose function to allocate guest vCPU stack
KVM: selftests: Expose segment definitons to assembly files
KVM: selftests: Add kbuild definitons
KVM: selftests: Define structs to pass parameters to TDX boot code
KVM: selftests: Set up TDX boot code region
KVM: selftests: Set up TDX boot parameters region
KVM: selftests: Add helper to initialize TDX VM
KVM: selftests: Hook TDX support to vm and vcpu creation
KVM: selftests: Add wrapper for TDX MMIO from guest
KVM: selftests: Add TDX lifecycle test
tools/include/linux/kbuild.h | 18 + tools/testing/selftests/kselftest_harness.h | 3 +- tools/testing/selftests/kvm/Makefile.kvm | 32 ++ .../selftests/kvm/include/x86/processor.h | 8 + .../selftests/kvm/include/x86/processor_asm.h | 12 + .../selftests/kvm/include/x86/tdx/td_boot.h | 81 ++++ .../kvm/include/x86/tdx/td_boot_asm.h | 16 + .../selftests/kvm/include/x86/tdx/tdcall.h | 34 ++ .../selftests/kvm/include/x86/tdx/tdx.h | 14 + .../selftests/kvm/include/x86/tdx/tdx_util.h | 86 ++++ .../testing/selftests/kvm/include/x86/ucall.h | 4 +- tools/testing/selftests/kvm/lib/kvm_util.c | 25 +- .../testing/selftests/kvm/lib/x86/processor.c | 122 ++++-- .../selftests/kvm/lib/x86/tdx/td_boot.S | 60 +++ .../kvm/lib/x86/tdx/td_boot_offsets.c | 21 + .../selftests/kvm/lib/x86/tdx/tdcall.S | 93 +++++ .../kvm/lib/x86/tdx/tdcall_offsets.c | 16 + tools/testing/selftests/kvm/lib/x86/tdx/tdx.c | 22 + .../selftests/kvm/lib/x86/tdx/tdx_util.c | 391 ++++++++++++++++++ tools/testing/selftests/kvm/lib/x86/ucall.c | 45 +- tools/testing/selftests/kvm/x86/tdx_vm_test.c | 31 ++ 21 files changed, 1095 insertions(+), 39 deletions(-) create mode 100644 tools/include/linux/kbuild.h create mode 100644 tools/testing/selftests/kvm/include/x86/processor_asm.h create mode 100644 tools/testing/selftests/kvm/include/x86/tdx/td_boot.h create mode 100644 tools/testing/selftests/kvm/include/x86/tdx/td_boot_asm.h create mode 100644 tools/testing/selftests/kvm/include/x86/tdx/tdcall.h create mode 100644 tools/testing/selftests/kvm/include/x86/tdx/tdx.h create mode 100644 tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/td_boot.S create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/td_boot_offsets.c create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/tdcall.S create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/tdcall_offsets.c create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/tdx.c create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c create mode 100644 tools/testing/selftests/kvm/x86/tdx_vm_test.c
Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
PATCH v9 1/19 KVM: selftests: Include overflow.h instead of redefining is_signed_type()
Redefinition of is_signed_type() causes compilation warning for tests which use kselftest_harness. Replace the definition with linux/overflow.h
--- tools/testing/selftests/kselftest_harness.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
tools/testing/selftests/kselftest_harness.h +1 -2
@@ -56,6 +56,7 @@#include <asm/types.h> #include <ctype.h> #include <errno.h> +#include <linux/overflow.h> #include <linux/unistd.h> #include <poll.h> #include <stdbool.h>
@@ -751,8 +752,6 @@for (; _metadata->trigger; _metadata->trigger = \ __bail(_assert, _metadata)) -#define is_signed_type (var) (!!(((__typeof__(var))(-1)) < (__typeof__(var))1)) -#define __EXPECT (_expected, _expected_str, _seen, _seen_str, _t, _assert) do { \/* Avoid multiple evaluation of the cases */ \__typeof__ (_expected) __exp = (_expected); \
Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
Sean Christopherson
maintainerRe: [PATCH v9 01/19] KVM: selftests: Include overflow.h instead of redefining is_signed_type()
Quoted 2 lines · Sagi Shahar
Redefinition of
is_signed_type()causes compilation warning for tests which usekselftest_harness. Replace the definition withlinux/overflow.h
Heh, tried that:
https://lore.kernel.org/all/[email protected]
There's now a fix in kvm-x86/fixes and thus kvm-x86/next, so if you base something off kvm-x86/next, the warnings should be gone.
Re: [PATCH v9 01/19] KVM: selftests: Include overflow.h instead of redefining is_signed_type()
In reply to Sagi ShaharNeed to add the selftest folks.
+ [email protected]
+ Kees Cook <[email protected]>
+ Shuah Khan <[email protected]>
Quoted 4 lines · Sagi Shahar
Redefinition of
is_signed_type()causes compilation warning for tests which usekselftest_harness. Replace the definition withlinux/overflow.hSigned-off-by Sagi Shahar <[email protected]>
Thanks! I've seen this as well and it fixes the warning for me as well. It might be worth picking up separate from this series depending on what the selftest folks say.
Quoted 28 lines
--- tools/testing/selftests/kselftest_harness.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)tools/testing/selftests/kselftest_harness.h +1
-2@@ -56,6 +56,7 @@#include <asm/types.h> #include <ctype.h> #include <errno.h> +#include <linux/overflow.h> #include <linux/unistd.h> #include <poll.h> #include <stdbool.h> @@ -751,8 +752,6 @@for (; _metadata->trigger; _metadata->trigger = \ __bail(_assert, _metadata)) -#define is_signed_type (var) (!!(((__typeof__(var))(-1)) < (__typeof__(var))1)) -#define __EXPECT (_expected, _expected_str, _seen, _seen_str, _t, _assert) do { \/* Avoid multiple evaluation of the cases */ \__typeof__ (_expected) __exp = (_expected); \Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
Sean Christopherson
maintainerRe: [PATCH v9 01/19] KVM: selftests: Include overflow.h instead of redefining is_signed_type()
Quoted 7 lines · Ira Weiny
Need to add the selftest folks.
+ [email protected]
+ Kees Cook <[email protected]>
+ Shuah Khan <[email protected]>Sagi Shahar wrote:
Quoted 8 lines
Quoted 4 lines
Redefinition of
is_signed_type()causes compilation warning for tests which usekselftest_harness. Replace the definition withlinux/overflow.hSigned-off-by Sagi Shahar <[email protected]>Thanks! I've seen this as well and it fixes the warning for me as well. It might be worth picking up separate from this series depending on what the selftest folks say.
Again[1], I already have a fix applied and will send it to Paolo today. And simply including overflow.h doesn't work[2] because not all selftests add tools/include to their include path.
I appreciate the enthusiastic though!
[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/[email protected]
Quoted 34 lines
Tested-by Ira Weiny <[email protected]>Reviewed-by Ira Weiny <[email protected]>Quoted 28 lines
--- tools/testing/selftests/kselftest_harness.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)tools/testing/selftests/kselftest_harness.h +1
-2@@ -56,6 +56,7 @@#include <asm/types.h> #include <ctype.h> #include <errno.h> +#include <linux/overflow.h> #include <linux/unistd.h> #include <poll.h> #include <stdbool.h> @@ -751,8 +752,6 @@for (; _metadata->trigger; _metadata->trigger = \ __bail(_assert, _metadata)) -#define is_signed_type (var) (!!(((__typeof__(var))(-1)) < (__typeof__(var))1)) -#define __EXPECT (_expected, _expected_str, _seen, _seen_str, _t, _assert) do { \/* Avoid multiple evaluation of the cases */ \__typeof__ (_expected) __exp = (_expected); \Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
PATCH v9 2/19 KVM: selftests: Allocate pgd in virt_map() as necessary
In reply to Sagi ShaharIf virt_map() is called before any call to ____vm_vaddr_alloc() it will create the mapping using an invalid pgd.
Add call to virt_pgd_alloc() as part of virt_map() before creating the mapping, similarly to ____vm_vaddr_alloc()
--- tools/testing/selftests/kvm/lib/kvm_util.c | 1 + 1 file changed, 1 insertion(+)
tools/testing/selftests/kvm/lib/kvm_util.c +1 -0
@@ -1609,6 +1609,7 @@ void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,TEST_ASSERT (vaddr + size > vaddr,"Vaddr overflow" );TEST_ASSERT (paddr + size > paddr,"Paddr overflow" ); +virt_pgd_alloc (vm);while (npages--) {virt_pg_map (vm, vaddr, paddr);sparsebit_set (vm->vpages_mapped, vaddr >> vm->page_shift);
Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
Re: [PATCH v9 02/19] KVM: selftests: Allocate pgd in virt_map() as necessary
Quoted 7 lines · Sagi Shahar
If
virt_map()is called before any call to____vm_vaddr_alloc()it will create the mapping using an invalid pgd.Add call to
virt_pgd_alloc()as part ofvirt_map()before creating the mapping, similarly to____vm_vaddr_alloc()Signed-off-by Sagi Shahar <[email protected]>
Quoted 19 lines
--- tools/testing/selftests/kvm/lib/kvm_util.c | 1 + 1 file changed, 1 insertion(+)tools/testing/selftests/kvm/lib/kvm_util.c +1
-0@@ -1609,6 +1609,7 @@ void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,TEST_ASSERT (vaddr + size > vaddr,"Vaddr overflow" );TEST_ASSERT (paddr + size > paddr,"Paddr overflow" ); +virt_pgd_alloc (vm);while (npages--) {virt_pg_map (vm, vaddr, paddr);sparsebit_set (vm->vpages_mapped, vaddr >> vm->page_shift);Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
Re: [PATCH v9 02/19] KVM: selftests: Allocate pgd in virt_map() as necessary
In reply to Sagi ShaharQuoted 7 lines · Sagi Shahar
If
virt_map()is called before any call to____vm_vaddr_alloc()it will create the mapping using an invalid pgd.Add call to
virt_pgd_alloc()as part ofvirt_map()before creating the mapping, similarly to____vm_vaddr_alloc()Signed-off-by Sagi Shahar <[email protected]>
Quoted 16 lines
--- tools/testing/selftests/kvm/lib/kvm_util.c | 1 + 1 file changed, 1 insertion(+)tools/testing/selftests/kvm/lib/kvm_util.c +1
-0@@ -1609,6 +1609,7 @@ void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr,TEST_ASSERT (vaddr + size > vaddr,"Vaddr overflow" );TEST_ASSERT (paddr + size > paddr,"Paddr overflow" ); +virt_pgd_alloc (vm);while (npages--) {virt_pg_map (vm, vaddr, paddr);sparsebit_set (vm->vpages_mapped, vaddr >> vm->page_shift);
PATCH v9 3/19 KVM: selftests: Expose functions to get default sregs values
In reply to Sagi ShaharTDX can't set sregs values directly using KVM_SET_SREGS. Expose the default values of certain sregs used by TDX VMs so they can be set manually.
--- .../selftests/kvm/include/x86/processor.h | 6 +++ .../testing/selftests/kvm/lib/x86/processor.c | 41 +++++++++++++++---- 2 files changed, 40 insertions(+), 7 deletions(-)
tools/testing/selftests/kvm/include/x86/processor.h +6 -0
@@ -1026,6 +1026,12 @@ static inline struct kvm_cpuid2 *allocate_kvm_cpuid2(int nr_entries)void vcpu_init_cpuid (struct kvm_vcpu *vcpu,const struct kvm_cpuid2 *cpuid); +uint16_t kvm_get_default_idt_limit (void ); +uint16_t kvm_get_default_gdt_limit (void ); +uint64_t kvm_get_default_cr0 (void ); +uint64_t kvm_get_default_cr4 (void ); +uint64_t kvm_get_default_efer (void ); +static inline void vcpu_get_cpuid (struct kvm_vcpu *vcpu) {vcpu_ioctl (vcpu,KVM_GET_CPUID2 , vcpu->cpuid);
tools/testing/selftests/kvm/lib/x86/processor.c +34 -7
55 lines not on this page · raw message
Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
Re: [PATCH v9 03/19] KVM: selftests: Expose functions to get default sregs values
Quoted 15 lines · Sagi Shahar
TDX can't set sregs values directly using
KVM_SET_SREGS. Expose the default values of certain sregs used by TDX VMs so they can be set manually.Signed-off-by Sagi Shahar <[email protected]>--- .../selftests/kvm/include/x86/processor.h | 6 +++ .../testing/selftests/kvm/lib/x86/processor.c | 41 +++++++++++++++---- 2 files changed, 40 insertions(+), 7 deletions(-)tools/testing/selftests/kvm/include/x86/processor.h +0
-0
tools/testing/selftests/kvm/include/x86/processor.h static inline struct kvm_cpuid2 *allocate_kvm_cpuid2(int nr_entries) · line 1033
Quoted 8 lines
tools/testing/selftests/kvm/include/x86/processor.h +5
-0@@ -1026,6 +1026,12 @@ static inline struct kvm_cpuid2 *allocate_kvm_cpuid2(int nr_entries)void vcpu_init_cpuid (struct kvm_vcpu *vcpu,const struct kvm_cpuid2 *cpuid); +uint16_t kvm_get_default_idt_limit (void ); +uint16_t kvm_get_default_gdt_limit (void ); +uint64_t kvm_get_default_cr0 (void ); +uint64_t kvm_get_default_cr4 (void ); +uint64_t kvm_get_default_efer (void );
Can these be defined in the header file as static inline?
Quoted 65 lines
tools/testing/selftests/kvm/include/x86/processor.h +1
-0+static inline void vcpu_get_cpuid (struct kvm_vcpu *vcpu) {vcpu_ioctl (vcpu,KVM_GET_CPUID2 , vcpu->cpuid);tools/testing/selftests/kvm/lib/x86/processor.c +34
-7@@ -488,6 +488,35 @@ static void kvm_seg_set_tss_64bit(vm_vaddr_t base, struct kvm_segment *segp) segp->present =1 ; } +uint16_t kvm_get_default_idt_limit (void ) +{ +return NUM_INTERRUPTS *sizeof (struct idt_entry ) -1 ; +} + +uint16_t kvm_get_default_gdt_limit (void ) +{ +return getpagesize () -1 ; +} + +uint64_t kvm_get_default_cr0 (void ) +{ +return X86_CR0_PE |X86_CR0_NE |X86_CR0_PG ; +} + +uint64_t kvm_get_default_cr4 (void ) +{ +uint64_t cr4 =X86_CR4_PAE |X86_CR4_OSFXSR ; + +if (kvm_cpu_has (X86_FEATURE_XSAVE )) + cr4 |=X86_CR4_OSXSAVE ; +return cr4; +} + +uint64_t kvm_get_default_efer (void ) +{ +return EFER_LME |EFER_LMA |EFER_NX ; +} +static void vcpu_init_sregs (struct kvm_vm *vm,struct kvm_vcpu *vcpu) {struct kvm_sregs sregs;@@ -498,15 +527,13 @@ static void vcpu_init_sregs(struct kvm_vm *vm, struct kvm_vcpu *vcpu)vcpu_sregs_get (vcpu, &sregs); sregs.idt.base = vm->arch.idt; - sregs.idt.limit =NUM_INTERRUPTS *sizeof (struct idt_entry ) -1 ; + sregs.idt.limit =kvm_get_default_idt_limit (); sregs.gdt.base = vm->arch.gdt; - sregs.gdt.limit =getpagesize () -1 ; + sregs.gdt.limit =kvm_get_default_gdt_limit (); - sregs.cr0 =X86_CR0_PE |X86_CR0_NE |X86_CR0_PG ; - sregs.cr4 |=X86_CR4_PAE |X86_CR4_OSFXSR ; -if (kvm_cpu_has (X86_FEATURE_XSAVE )) - sregs.cr4 |=X86_CR4_OSXSAVE ; - sregs.efer |= (EFER_LME |EFER_LMA |EFER_NX ); + sregs.cr0 =kvm_get_default_cr0 (); + sregs.cr4 |=kvm_get_default_cr4 (); + sregs.efer |=kvm_get_default_efer ();kvm_seg_set_unusable (&sregs.ldt);kvm_seg_set_kernel_code_64bit (&sregs.cs);
Sean Christopherson
maintainerRe: [PATCH v9 03/19] KVM: selftests: Expose functions to get default sregs values
Quoted 11 lines · Binbin Wu
Quoted 10 lines · Sagi Shahar
TDX can't set sregs values directly using
KVM_SET_SREGS. Expose the default values of certain sregs used by TDX VMs so they can be set manually.Signed-off-by Sagi Shahar <[email protected]>--- .../selftests/kvm/include/x86/processor.h | 6 +++ .../testing/selftests/kvm/lib/x86/processor.c | 41 +++++++++++++++---- 2 files changed, 40 insertions(+), 7 deletions(-)
Quoted 12 lines
tools/testing/selftests/kvm/include/x86/processor.h static inline struct kvm_cpuid2 *allocate_kvm_cpuid2(int nr_entries) · line 1031
Quoted 11 lines
tools/testing/selftests/kvm/include/x86/processor.h +5
-0@@ -1026,6 +1026,12 @@ static inline struct kvm_cpuid2 *allocate_kvm_cpuid2(int nr_entries)void vcpu_init_cpuid (struct kvm_vcpu *vcpu,const struct kvm_cpuid2 *cpuid); +uint16_t kvm_get_default_idt_limit (void ); +uint16_t kvm_get_default_gdt_limit (void ); +uint64_t kvm_get_default_cr0 (void ); +uint64_t kvm_get_default_cr4 (void ); +uint64_t kvm_get_default_efer (void );Can these be defined in the header file as static inline?
Yes please. Performance isn't a concern, but as a developer, it's nice to not have to bounce to a definition to find such simple information.
PATCH v9 4/19 KVM: selftests: Expose function to allocate guest vCPU stack
In reply to Sagi ShaharTDX 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.
Expose the function to allocate the guest stack so that TDX initialization code can allocate it itself and skip the allocation in vm_arch_vcpu_add() in that case.
--- .../selftests/kvm/include/x86/processor.h | 2 ++ tools/testing/selftests/kvm/lib/x86/processor.c | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/include/x86/processor.h +2 -0
@@ -1111,6 +1111,8 @@ static inline void vcpu_clear_cpuid_feature(struct kvm_vcpu *vcpu,vcpu_set_or_clear_cpuid_feature (vcpu, feature, false); } +vm_vaddr_t kvm_allocate_vcpu_stack (struct kvm_vm *vm); +uint64_t vcpu_get_msr (struct kvm_vcpu *vcpu,uint64_t msr_index);int _vcpu_set_msr (struct kvm_vcpu *vcpu,uint64_t msr_index,uint64_t msr_value);
tools/testing/selftests/kvm/lib/x86/processor.c +12 -5
@@ -687,12 +687,9 @@ void vcpu_arch_set_entry_point(struct kvm_vcpu *vcpu, void *guest_code)vcpu_regs_set (vcpu, ®s); } -struct kvm_vcpu *vm_arch_vcpu_add (struct kvm_vm *vm,uint32_t vcpu_id) +vm_vaddr_t kvm_allocate_vcpu_stack (struct kvm_vm *vm) { -struct kvm_mp_state mp_state; -struct kvm_regs regs;vm_vaddr_t stack_vaddr; -struct kvm_vcpu *vcpu; stack_vaddr = __vm_vaddr_alloc(vm, DEFAULT_STACK_PGS * getpagesize(), DEFAULT_GUEST_STACK_VADDR_MIN,
@@ -713,6 +710,15 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id) "__vm_vaddr_alloc() did not provide a page-aligned address"); stack_vaddr -=8 ; +return stack_vaddr; +} + +struct kvm_vcpu *vm_arch_vcpu_add (struct kvm_vm *vm,uint32_t vcpu_id) +{ +struct kvm_mp_state mp_state; +struct kvm_regs regs; +struct kvm_vcpu *vcpu; + vcpu =__vm_vcpu_add (vm, vcpu_id);vcpu_init_cpuid (vcpu,kvm_get_supported_cpuid ());vcpu_init_sregs (vm, vcpu);
@@ -721,7 +727,8 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)/* Setup guest general purpose registers */ vcpu_regs_get (vcpu, ®s); regs.rflags = regs.rflags |0x2 ; - regs.rsp = stack_vaddr; +if (vm->type !=KVM_X86_TDX_VM ) + regs.rsp =kvm_allocate_vcpu_stack (vm);vcpu_regs_set (vcpu, ®s);/* Setup the MP state */
Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
Re: [PATCH v9 04/19] KVM: selftests: Expose function to allocate guest vCPU stack
Sagi Shahar wrote:
[snip]
Quoted 35 lines
tools/testing/selftests/kvm/lib/x86/processor.c +10
-4@@ -687,12 +687,9 @@ void vcpu_arch_set_entry_point(struct kvm_vcpu *vcpu, void *guest_code)vcpu_regs_set (vcpu, ®s); } -struct kvm_vcpu *vm_arch_vcpu_add (struct kvm_vm *vm,uint32_t vcpu_id) +vm_vaddr_t kvm_allocate_vcpu_stack (struct kvm_vm *vm) { -struct kvm_mp_state mp_state; -struct kvm_regs regs;vm_vaddr_t stack_vaddr; -struct kvm_vcpu *vcpu; stack_vaddr = __vm_vaddr_alloc(vm, DEFAULT_STACK_PGS * getpagesize(), DEFAULT_GUEST_STACK_VADDR_MIN,@@ -713,6 +710,15 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id) "__vm_vaddr_alloc() did not provide a page-aligned address"); stack_vaddr -=8 ; +return stack_vaddr; +} + +struct kvm_vcpu *vm_arch_vcpu_add (struct kvm_vm *vm,uint32_t vcpu_id) +{ +struct kvm_mp_state mp_state; +struct kvm_regs regs; +struct kvm_vcpu *vcpu; + vcpu =__vm_vcpu_add (vm, vcpu_id);vcpu_init_cpuid (vcpu,kvm_get_supported_cpuid ());vcpu_init_sregs (vm, vcpu);
tools/testing/selftests/kvm/lib/x86/processor.c struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id) · line 731
Quoted 6 lines
tools/testing/selftests/kvm/lib/x86/processor.c +2
-1@@ -721,7 +727,8 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)/* Setup guest general purpose registers */ vcpu_regs_get (vcpu, ®s); regs.rflags = regs.rflags |0x2 ; - regs.rsp = stack_vaddr; +if (vm->type !=KVM_X86_TDX_VM ) + regs.rsp =kvm_allocate_vcpu_stack (vm);
At this point in the series vm->type can't be KVM_X86_TDX_VM correct?
So that makes this safe during bisect?
Ira
Quoted 6 lines
tools/testing/selftests/kvm/lib/x86/processor.c +0
-0vcpu_regs_set (vcpu, ®s);/* Setup the MP state */ Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
Re: [PATCH v9 04/19] KVM: selftests: Expose function to allocate guest vCPU stack
Quoted 49 lines · Ira Weiny
Sagi Shahar wrote:
[snip]
Quoted 35 lines
tools/testing/selftests/kvm/lib/x86/processor.c +10
-4@@ -687,12 +687,9 @@ void vcpu_arch_set_entry_point(struct kvm_vcpu *vcpu, void *guest_code)vcpu_regs_set (vcpu, ®s); } -struct kvm_vcpu *vm_arch_vcpu_add (struct kvm_vm *vm,uint32_t vcpu_id) +vm_vaddr_t kvm_allocate_vcpu_stack (struct kvm_vm *vm) { -struct kvm_mp_state mp_state; -struct kvm_regs regs;vm_vaddr_t stack_vaddr; -struct kvm_vcpu *vcpu; stack_vaddr = __vm_vaddr_alloc(vm, DEFAULT_STACK_PGS * getpagesize(), DEFAULT_GUEST_STACK_VADDR_MIN,@@ -713,6 +710,15 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id) "__vm_vaddr_alloc() did not provide a page-aligned address"); stack_vaddr -=8 ; +return stack_vaddr; +} + +struct kvm_vcpu *vm_arch_vcpu_add (struct kvm_vm *vm,uint32_t vcpu_id) +{ +struct kvm_mp_state mp_state; +struct kvm_regs regs; +struct kvm_vcpu *vcpu; + vcpu =__vm_vcpu_add (vm, vcpu_id);vcpu_init_cpuid (vcpu,kvm_get_supported_cpuid ());vcpu_init_sregs (vm, vcpu);tools/testing/selftests/kvm/lib/x86/processor.c struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id) · line 731
Quoted 6 lines
tools/testing/selftests/kvm/lib/x86/processor.c +2
-1@@ -721,7 +727,8 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)/* Setup guest general purpose registers */ vcpu_regs_get (vcpu, ®s); regs.rflags = regs.rflags |0x2 ; - regs.rsp = stack_vaddr; +if (vm->type !=KVM_X86_TDX_VM ) + regs.rsp =kvm_allocate_vcpu_stack (vm);At this point in the series
vm->typecan't beKVM_X86_TDX_VMcorrect?
Quoted 2 lines
So that makes this safe during bisect?
I double checked and no one is creating VMs with KVM_X86_TDX_VM. The first test that sets KVM_X86_TDX_VM is the last patch in the series.
Quoted 10 lines
Ira
Quoted 6 lines
tools/testing/selftests/kvm/lib/x86/processor.c +0
-1vcpu_regs_set (vcpu, ®s);/* Setup the MP state */ --2.51.0.rc1.193.gad69d77794-goog
Re: [PATCH v9 04/19] KVM: selftests: Expose function to allocate guest vCPU stack
In reply to Sagi ShaharQuoted 62 lines · Sagi Shahar
TDX 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.Expose the function to allocate the guest stack so that TDX initialization code can allocate it itself and skip the allocation in
vm_arch_vcpu_add()in that case.Signed-off-by Sagi Shahar <[email protected]>--- .../selftests/kvm/include/x86/processor.h | 2 ++ tools/testing/selftests/kvm/lib/x86/processor.c | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-)tools/testing/selftests/kvm/include/x86/processor.h +2
-0@@ -1111,6 +1111,8 @@ static inline void vcpu_clear_cpuid_feature(struct kvm_vcpu *vcpu,vcpu_set_or_clear_cpuid_feature (vcpu, feature, false); } +vm_vaddr_t kvm_allocate_vcpu_stack (struct kvm_vm *vm); +uint64_t vcpu_get_msr (struct kvm_vcpu *vcpu,uint64_t msr_index);int _vcpu_set_msr (struct kvm_vcpu *vcpu,uint64_t msr_index,uint64_t msr_value);tools/testing/selftests/kvm/lib/x86/processor.c +10
-4@@ -687,12 +687,9 @@ void vcpu_arch_set_entry_point(struct kvm_vcpu *vcpu, void *guest_code)vcpu_regs_set (vcpu, ®s); } -struct kvm_vcpu *vm_arch_vcpu_add (struct kvm_vm *vm,uint32_t vcpu_id) +vm_vaddr_t kvm_allocate_vcpu_stack (struct kvm_vm *vm) { -struct kvm_mp_state mp_state; -struct kvm_regs regs;vm_vaddr_t stack_vaddr; -struct kvm_vcpu *vcpu; stack_vaddr = __vm_vaddr_alloc(vm, DEFAULT_STACK_PGS * getpagesize(), DEFAULT_GUEST_STACK_VADDR_MIN,@@ -713,6 +710,15 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id) "__vm_vaddr_alloc() did not provide a page-aligned address"); stack_vaddr -=8 ; +return stack_vaddr; +} + +struct kvm_vcpu *vm_arch_vcpu_add (struct kvm_vm *vm,uint32_t vcpu_id) +{ +struct kvm_mp_state mp_state; +struct kvm_regs regs; +struct kvm_vcpu *vcpu; + vcpu =__vm_vcpu_add (vm, vcpu_id);vcpu_init_cpuid (vcpu,kvm_get_supported_cpuid ());vcpu_init_sregs (vm, vcpu);
tools/testing/selftests/kvm/lib/x86/processor.c struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id) · line 731
Quoted 6 lines
tools/testing/selftests/kvm/lib/x86/processor.c +2
-1@@ -721,7 +727,8 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)/* Setup guest general purpose registers */ vcpu_regs_get (vcpu, ®s); regs.rflags = regs.rflags |0x2 ; - regs.rsp = stack_vaddr; +if (vm->type !=KVM_X86_TDX_VM ) + regs.rsp =kvm_allocate_vcpu_stack (vm);
I am wondering if this could be more generic.
I.e, make vcpu_regs_get() return the error code.
If vcpu_regs_get() failed (for TDX, since it's guest state is protected, the ioctl will return -EINVAL), the vcpu_regs_set(), including the allocation for the vcpu stack, could be skipped.
Quoted 3 lines
tools/testing/selftests/kvm/lib/x86/processor.c +0
-0vcpu_regs_set (vcpu, ®s);/* Setup the MP state */
Re: [PATCH v9 04/19] KVM: selftests: Expose function to allocate guest vCPU stack
Quoted 70 lines · Binbin Wu
Quoted 66 lines · Sagi Shahar
TDX 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.Expose the function to allocate the guest stack so that TDX initialization code can allocate it itself and skip the allocation in
vm_arch_vcpu_add()in that case.Signed-off-by Sagi Shahar <[email protected]>--- .../selftests/kvm/include/x86/processor.h | 2 ++ tools/testing/selftests/kvm/lib/x86/processor.c | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-)tools/testing/selftests/kvm/include/x86/processor.h +2
-0@@ -1111,6 +1111,8 @@ static inline void vcpu_clear_cpuid_feature(struct kvm_vcpu *vcpu,vcpu_set_or_clear_cpuid_feature (vcpu, feature, false); } +vm_vaddr_t kvm_allocate_vcpu_stack (struct kvm_vm *vm); +uint64_t vcpu_get_msr (struct kvm_vcpu *vcpu,uint64_t msr_index);int _vcpu_set_msr (struct kvm_vcpu *vcpu,uint64_t msr_index,uint64_t msr_value);tools/testing/selftests/kvm/lib/x86/processor.c +10
-5@@ -687,12 +687,9 @@ void vcpu_arch_set_entry_point(struct kvm_vcpu *vcpu, void *guest_code)vcpu_regs_set (vcpu, ®s); } -struct kvm_vcpu *vm_arch_vcpu_add (struct kvm_vm *vm,uint32_t vcpu_id) +vm_vaddr_t kvm_allocate_vcpu_stack (struct kvm_vm *vm) { -struct kvm_mp_state mp_state; -struct kvm_regs regs;vm_vaddr_t stack_vaddr; -struct kvm_vcpu *vcpu; stack_vaddr = __vm_vaddr_alloc(vm, DEFAULT_STACK_PGS * getpagesize(), DEFAULT_GUEST_STACK_VADDR_MIN,@@ -713,6 +710,15 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id) "__vm_vaddr_alloc() did not provide a page-aligned address"); stack_vaddr -=8 ; +return stack_vaddr; +} + +struct kvm_vcpu *vm_arch_vcpu_add (struct kvm_vm *vm,uint32_t vcpu_id) +{ +struct kvm_mp_state mp_state; +struct kvm_regs regs; +struct kvm_vcpu *vcpu; + vcpu =__vm_vcpu_add (vm, vcpu_id);vcpu_init_cpuid (vcpu,kvm_get_supported_cpuid ());vcpu_init_sregs (vm, vcpu);@@ -721,7 +727,8 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)/* Setup guest general purpose registers */ vcpu_regs_get (vcpu, ®s); regs.rflags = regs.rflags |0x2 ; - regs.rsp = stack_vaddr;
Quoted 9 lines
tools/testing/selftests/kvm/lib/x86/processor.c struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id) · line 731
Quoted 2 lines
tools/testing/selftests/kvm/lib/x86/processor.c +2
-0+if (vm->type !=KVM_X86_TDX_VM ) + regs.rsp =kvm_allocate_vcpu_stack (vm);I am wondering if this could be more generic.
I.e, makevcpu_regs_get()return the error code.
Ifvcpu_regs_get()failed (for TDX, since it's guest state is protected, the ioctl will return-EINVAL), thevcpu_regs_set(), including the allocation for the vcpu stack, could be skipped.
I'm dropping this check and only keeping the check from "KVM:
selftests: Hook TDX support to vm and vcpu creation" which looks like this:
if (is_tdx_vm(vm)) {
vm_tdx_vcpu_add(vm, vcpu);
} else {
vcpu_init_cpuid(vcpu, kvm_get_supported_cpuid());
vcpu_init_sregs(vm, vcpu);
vcpu_init_xcrs(vm, vcpu);
/* Setup guest general purpose registers */
vcpu_regs_get(vcpu, ®s);
regs.rflags = regs.rflags | 0x2;
regs.rsp = kvm_allocate_vcpu_stack(vm);
vcpu_regs_set(vcpu, ®s);
}Since there are other differences specific to TDX I think this is cleaner than trying to handle kvm_allocate_vcpu_stack() individually.
Quoted 4 lines
Quoted 3 lines
tools/testing/selftests/kvm/lib/x86/processor.c +0
-0vcpu_regs_set (vcpu, ®s);/* Setup the MP state */
Sean Christopherson
maintainerRe: [PATCH v9 04/19] KVM: selftests: Expose function to allocate guest vCPU stack
In reply to Binbin WuQuoted 61 lines · Binbin Wu
Quoted 58 lines · Sagi Shahar
TDX 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.Expose the function to allocate the guest stack so that TDX initialization code can allocate it itself and skip the allocation in
vm_arch_vcpu_add()in that case.Signed-off-by Sagi Shahar <[email protected]>--- .../selftests/kvm/include/x86/processor.h | 2 ++ tools/testing/selftests/kvm/lib/x86/processor.c | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-)tools/testing/selftests/kvm/include/x86/processor.h +2
-0@@ -1111,6 +1111,8 @@ static inline void vcpu_clear_cpuid_feature(struct kvm_vcpu *vcpu,vcpu_set_or_clear_cpuid_feature (vcpu, feature, false); } +vm_vaddr_t kvm_allocate_vcpu_stack (struct kvm_vm *vm); +uint64_t vcpu_get_msr (struct kvm_vcpu *vcpu,uint64_t msr_index);int _vcpu_set_msr (struct kvm_vcpu *vcpu,uint64_t msr_index,uint64_t msr_value);tools/testing/selftests/kvm/lib/x86/processor.c +10
-4@@ -687,12 +687,9 @@ void vcpu_arch_set_entry_point(struct kvm_vcpu *vcpu, void *guest_code)vcpu_regs_set (vcpu, ®s); } -struct kvm_vcpu *vm_arch_vcpu_add (struct kvm_vm *vm,uint32_t vcpu_id) +vm_vaddr_t kvm_allocate_vcpu_stack (struct kvm_vm *vm) { -struct kvm_mp_state mp_state; -struct kvm_regs regs;vm_vaddr_t stack_vaddr; -struct kvm_vcpu *vcpu; stack_vaddr = __vm_vaddr_alloc(vm, DEFAULT_STACK_PGS * getpagesize(), DEFAULT_GUEST_STACK_VADDR_MIN,@@ -713,6 +710,15 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id) "__vm_vaddr_alloc() did not provide a page-aligned address"); stack_vaddr -=8 ; +return stack_vaddr; +} + +struct kvm_vcpu *vm_arch_vcpu_add (struct kvm_vm *vm,uint32_t vcpu_id) +{ +struct kvm_mp_state mp_state; +struct kvm_regs regs; +struct kvm_vcpu *vcpu; + vcpu =__vm_vcpu_add (vm, vcpu_id);vcpu_init_cpuid (vcpu,kvm_get_supported_cpuid ());vcpu_init_sregs (vm, vcpu);@@ -721,7 +727,8 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)/* Setup guest general purpose registers */
Quoted 8 lines
tools/testing/selftests/kvm/lib/x86/processor.c struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id) · line 731
Quoted 5 lines
tools/testing/selftests/kvm/lib/x86/processor.c +2
-1vcpu_regs_get (vcpu, ®s); regs.rflags = regs.rflags |0x2 ; - regs.rsp = stack_vaddr; +if (vm->type !=KVM_X86_TDX_VM ) + regs.rsp =kvm_allocate_vcpu_stack (vm);I am wondering if this could be more generic.
I.e, makevcpu_regs_get()return the error code.
It would need to be a double-underscores variant, i.e. __vcpu_regs_get(). But even then, I don't think it's worth getting that clever, because then to ensure selftests aren't hitting KVM bugs, we'd want to assert that failure only occurs for a TDX VM, i.e. we'd end up with:
if (__vcpu_regs_get(vcpu, ®s)) {
TEST_ASERT(is_tdx_vm(vm), "blah blah blah"
} else {
}which doesn't really "save" anything relative to Sagi's proposed version of:
if (is_tdx_vm(vm)) {
vm_tdx_vcpu_add(vm, vcpu);
} else {
vcpu_init_cpuid(vcpu, kvm_get_supported_cpuid());
vcpu_init_sregs(vm, vcpu);
vcpu_init_xcrs(vm, vcpu);
/* Setup guest general purpose registers */
vcpu_regs_get(vcpu, ®s);
regs.rflags = regs.rflags | 0x2;
regs.rsp = kvm_allocate_vcpu_stack(vm);
vcpu_regs_set(vcpu, ®s);
}Quoted 7 lines
If
vcpu_regs_get()failed (for TDX, since it's guest state is protected, the ioctl will return-EINVAL), thevcpu_regs_set(), including the allocation for the vcpu stack, could be skipped.Quoted 2 lines
tools/testing/selftests/kvm/lib/x86/processor.c +0
-0vcpu_regs_set (vcpu, ®s);/* Setup the MP state */
PATCH v9 5/19 KVM: selftests: Update kvm_init_vm_address_properties() for TDX
In reply to Sagi ShaharFrom: Isaku Yamahata <[email protected]>
Let kvm_init_vm_address_properties() initialize vm->arch.{s_bit, tag_mask} similar to SEV.
TDX sets the shared bit based on the guest physical address width and currently supports 48 and 52 widths.
--- .../selftests/kvm/include/x86/tdx/tdx_util.h | 14 ++++++++++++++ tools/testing/selftests/kvm/lib/x86/processor.c | 12 ++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h new file +14 -0
@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SELFTESTS_TDX_TDX_UTIL_H +#define SELFTESTS_TDX_TDX_UTIL_H + +#include <stdbool.h> + +#include "kvm_util.h" + +static inline bool is_tdx_vm (struct kvm_vm *vm) +{ +return vm->type ==KVM_X86_TDX_VM ; +} + +#endif // SELFTESTS_TDX_TDX_UTIL_H
tools/testing/selftests/kvm/lib/x86/processor.c +10 -2
@@ -8,6 +8,7 @@#include "kvm_util.h" #include "processor.h" #include "sev.h" +#include "tdx/tdx_util.h" #ifndef NUM_INTERRUPTS #define NUM_INTERRUPTS 256
@@ -1190,12 +1191,19 @@ void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits)void kvm_init_vm_address_properties (struct kvm_vm *vm) { +uint32_t gpa_bits =kvm_cpu_property (X86_PROPERTY_GUEST_MAX_PHY_ADDR ); + + vm->arch.sev_fd =-1 ; +if (is_sev_vm (vm)) { vm->arch.sev_fd =open_sev_dev_path_or_exit (); vm->arch.c_bit =BIT_ULL (this_cpu_property (X86_PROPERTY_SEV_C_BIT )); vm->gpa_tag_mask = vm->arch.c_bit; - }else { - vm->arch.sev_fd =-1 ; + }else if (is_tdx_vm (vm)) { +TEST_ASSERT (gpa_bits ==48 || gpa_bits ==52 , +"TDX: bad X86_PROPERTY_GUEST_MAX_PHY_ADDR value: %u" , gpa_bits); + vm->arch.s_bit =1ULL << (gpa_bits -1 ); + vm->gpa_tag_mask = vm->arch.s_bit; } }
Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
Re: [PATCH v9 05/19] KVM: selftests: Update kvm_init_vm_address_properties() for TDX
Quoted 27 lines · Sagi Shahar
From: Isaku Yamahata <[email protected]>
Let
kvm_init_vm_address_properties()initialize vm->arch.{s_bit,tag_mask} similar to SEV.TDX sets the shared bit based on the guest physical address width and currently supports 48 and 52 widths.
Co-developed-by Adrian Hunter <[email protected]>Signed-off-by Adrian Hunter <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Co-developed-by Sagi Shahar <[email protected]>Signed-off-by Sagi Shahar <[email protected]>--- .../selftests/kvm/include/x86/tdx/tdx_util.h | 14 ++++++++++++++ tools/testing/selftests/kvm/lib/x86/processor.c | 12 ++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/x86/tdx/tdx_util.htools/testing/selftests/kvm/include/x86/tdx/tdx_util.h new file +2
-0@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SELFTESTS_TDX_TDX_UTIL_H
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h line 12
Quoted 10 lines
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h new file +10
-0+#define SELFTESTS_TDX_TDX_UTIL_H + +#include <stdbool.h> + +#include "kvm_util.h" + +static inline bool is_tdx_vm (struct kvm_vm *vm) +{ +return vm->type ==KVM_X86_TDX_VM ; +}
NIT: Might have been better to define this in 4/19 and use it there. But looks like that logic is changed later on somewhere. So... meh.
Ira
[snip]
Re: [PATCH v9 05/19] KVM: selftests: Update kvm_init_vm_address_properties() for TDX
Quoted 34 lines · Ira Weiny
Quoted 32 lines · Sagi Shahar
From: Isaku Yamahata <[email protected]>
Let
kvm_init_vm_address_properties()initialize vm->arch.{s_bit,tag_mask} similar to SEV.TDX sets the shared bit based on the guest physical address width and currently supports 48 and 52 widths.
Co-developed-by Adrian Hunter <[email protected]>Signed-off-by Adrian Hunter <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Co-developed-by Sagi Shahar <[email protected]>Signed-off-by Sagi Shahar <[email protected]>--- .../selftests/kvm/include/x86/tdx/tdx_util.h | 14 ++++++++++++++ tools/testing/selftests/kvm/lib/x86/processor.c | 12 ++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/x86/tdx/tdx_util.htools/testing/selftests/kvm/include/x86/tdx/tdx_util.h new file +7
-0@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SELFTESTS_TDX_TDX_UTIL_H +#define SELFTESTS_TDX_TDX_UTIL_H + +#include <stdbool.h> + +#include "kvm_util.h"
Quoted 8 lines
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h line 12
Quoted 5 lines
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h new file +5
-0+ +static inline bool is_tdx_vm (struct kvm_vm *vm) +{ +return vm->type ==KVM_X86_TDX_VM ; +}NIT: Might have been better to define this in 4/19 and use it there. But looks like that logic is changed later on somewhere. So... meh.
We can skip the check for KVM_X86_TDX_VM entirely in 4/19 since like you said, it's replaced with something different later on.
Quoted 4 lines
Ira
[snip]
Re: [PATCH v9 05/19] KVM: selftests: Update kvm_init_vm_address_properties() for TDX
In reply to Sagi ShaharQuoted 27 lines · Sagi Shahar
From: Isaku Yamahata <[email protected]>
Let
kvm_init_vm_address_properties()initialize vm->arch.{s_bit,tag_mask} similar to SEV.TDX sets the shared bit based on the guest physical address width and currently supports 48 and 52 widths.
Co-developed-by Adrian Hunter <[email protected]>Signed-off-by Adrian Hunter <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Co-developed-by Sagi Shahar <[email protected]>Signed-off-by Sagi Shahar <[email protected]>--- .../selftests/kvm/include/x86/tdx/tdx_util.h | 14 ++++++++++++++ tools/testing/selftests/kvm/lib/x86/processor.c | 12 ++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-)create mode 100644
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.htools/testing/selftests/kvm/include/x86/tdx/tdx_util.h new file +2
-0@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SELFTESTS_TDX_TDX_UTIL_H
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h line 12
Quoted 10 lines
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h new file +10
-0+#define SELFTESTS_TDX_TDX_UTIL_H + +#include <stdbool.h> + +#include "kvm_util.h" + +static inline bool is_tdx_vm (struct kvm_vm *vm) +{ +return vm->type ==KVM_X86_TDX_VM ; +}
If the branch "vm->type != KVM_X86_TDX_VM" in patch 04/19
is still needed, this helper could be added earlier and used instead of open code.
Quoted 22 lines
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h new file +2
-0+ +#endif // SELFTESTS_TDX_TDX_UTIL_H tools/testing/selftests/kvm/lib/x86/processor.c +5
-0@@ -8,6 +8,7 @@#include "kvm_util.h" #include "processor.h" #include "sev.h" +#include "tdx/tdx_util.h" #ifndef NUM_INTERRUPTS #define NUM_INTERRUPTS 256@@ -1190,12 +1191,19 @@ void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits)void kvm_init_vm_address_properties (struct kvm_vm *vm) { +uint32_t gpa_bits =kvm_cpu_property (X86_PROPERTY_GUEST_MAX_PHY_ADDR ); + + vm->arch.sev_fd =-1 ; +
tools/testing/selftests/kvm/lib/x86/processor.c void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits) · line 1205
Quoted 10 lines
tools/testing/selftests/kvm/lib/x86/processor.c +4
-2if (is_sev_vm (vm)) { vm->arch.sev_fd =open_sev_dev_path_or_exit (); vm->arch.c_bit =BIT_ULL (this_cpu_property (X86_PROPERTY_SEV_C_BIT )); vm->gpa_tag_mask = vm->arch.c_bit; - }else { - vm->arch.sev_fd =-1 ; + }else if (is_tdx_vm (vm)) { +TEST_ASSERT (gpa_bits ==48 || gpa_bits ==52 , +"TDX: bad X86_PROPERTY_GUEST_MAX_PHY_ADDR value: %u" , gpa_bits); + vm->arch.s_bit =1ULL << (gpa_bits -1 );
Nit: Use BIT_ULL().
Quoted 4 lines
tools/testing/selftests/kvm/lib/x86/processor.c +1
-0+ vm->gpa_tag_mask = vm->arch.s_bit; } }
Re: [PATCH v9 05/19] KVM: selftests: Update kvm_init_vm_address_properties() for TDX
Quoted 37 lines · Binbin Wu
Quoted 33 lines · Sagi Shahar
From: Isaku Yamahata <[email protected]>
Let
kvm_init_vm_address_properties()initialize vm->arch.{s_bit,tag_mask} similar to SEV.TDX sets the shared bit based on the guest physical address width and currently supports 48 and 52 widths.
Co-developed-by Adrian Hunter <[email protected]>Signed-off-by Adrian Hunter <[email protected]>Signed-off-by Isaku Yamahata <[email protected]>Co-developed-by Sagi Shahar <[email protected]>Signed-off-by Sagi Shahar <[email protected]>--- .../selftests/kvm/include/x86/tdx/tdx_util.h | 14 ++++++++++++++ tools/testing/selftests/kvm/lib/x86/processor.c | 12 ++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-)create mode 100644
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.htools/testing/selftests/kvm/include/x86/tdx/tdx_util.h new file +8
-0@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SELFTESTS_TDX_TDX_UTIL_H +#define SELFTESTS_TDX_TDX_UTIL_H + +#include <stdbool.h> + +#include "kvm_util.h" +
Quoted 9 lines
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h line 12
Quoted 4 lines
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h new file +4
-0+static inline bool is_tdx_vm (struct kvm_vm *vm) +{ +return vm->type ==KVM_X86_TDX_VM ; +}If the branch "
vm->type!=KVM_X86_TDX_VM" in patch 04/19
is still needed, this helper could be added earlier and used instead of open code.
I'm dropping the check in 04/19. See my response to Ira.
Quoted 26 lines
Quoted 26 lines
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h new file +2
-0+ +#endif // SELFTESTS_TDX_TDX_UTIL_H tools/testing/selftests/kvm/lib/x86/processor.c +5
-0@@ -8,6 +8,7 @@#include "kvm_util.h" #include "processor.h" #include "sev.h" +#include "tdx/tdx_util.h" #ifndef NUM_INTERRUPTS #define NUM_INTERRUPTS 256@@ -1190,12 +1191,19 @@ void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits)void kvm_init_vm_address_properties (struct kvm_vm *vm) { +uint32_t gpa_bits =kvm_cpu_property (X86_PROPERTY_GUEST_MAX_PHY_ADDR ); + + vm->arch.sev_fd =-1 ; +if (is_sev_vm (vm)) { vm->arch.sev_fd =open_sev_dev_path_or_exit (); vm->arch.c_bit =BIT_ULL (this_cpu_property (X86_PROPERTY_SEV_C_BIT )); vm->gpa_tag_mask = vm->arch.c_bit;
Quoted 9 lines
tools/testing/selftests/kvm/lib/x86/processor.c void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits) · line 1205
Quoted 6 lines
tools/testing/selftests/kvm/lib/x86/processor.c +4
-2- }else { - vm->arch.sev_fd =-1 ; + }else if (is_tdx_vm (vm)) { +TEST_ASSERT (gpa_bits ==48 || gpa_bits ==52 , +"TDX: bad X86_PROPERTY_GUEST_MAX_PHY_ADDR value: %u" , gpa_bits); + vm->arch.s_bit =1ULL << (gpa_bits -1 );Nit: Use
BIT_ULL().
ACK, Will update in next version.
Quoted 5 lines
Quoted 4 lines
tools/testing/selftests/kvm/lib/x86/processor.c +1
-0+ vm->gpa_tag_mask = vm->arch.s_bit; } }
PATCH v9 6/19 KVM: selftests: Expose segment definitons to assembly files
In reply to Sagi ShaharMove kernel segment definitons to a separate file which can be included from assembly files.
--- .../selftests/kvm/include/x86/processor_asm.h | 12 ++++++++++++ tools/testing/selftests/kvm/lib/x86/processor.c | 5 +---- 2 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/x86/processor_asm.h
tools/testing/selftests/kvm/include/x86/processor_asm.h new file +12 -0
@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Used for storing defines used by both processor.c and assembly code. +*/ +#ifndef SELFTEST_KVM_PROCESSOR_ASM_H +#define SELFTEST_KVM_PROCESSOR_ASM_H + +#define KERNEL_CS 0x8 +#define KERNEL_DS 0x10 +#define KERNEL_TSS 0x18 + +#endif // SELFTEST_KVM_PROCESSOR_ASM_H
tools/testing/selftests/kvm/lib/x86/processor.c +1 -4
@@ -7,6 +7,7 @@#include "test_util.h" #include "kvm_util.h" #include "processor.h" +#include "processor_asm.h" #include "sev.h" #include "tdx/tdx_util.h"
@@ -14,10 +15,6 @@#define NUM_INTERRUPTS 256#endif -#define KERNEL_CS 0x8 -#define KERNEL_DS 0x10 -#define KERNEL_TSS 0x18 -vm_vaddr_t exception_handlers;bool host_cpu_is_amd;bool host_cpu_is_intel;
Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
Re: [PATCH v9 06/19] KVM: selftests: Expose segment definitons to assembly files
NIT:
Quoted 1 line · Sagi Shahar
Move kernel segment definitons to a separate file which can be included
^^^^^^^^^^ definitions
And in the subject.
Otherwise seems reasonable.
Quoted 53 lines
from assembly files.
Signed-off-by Sagi Shahar <[email protected]>--- .../selftests/kvm/include/x86/processor_asm.h | 12 ++++++++++++ tools/testing/selftests/kvm/lib/x86/processor.c | 5 +---- 2 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 tools/testing/selftests/kvm/include/x86/processor_asm.htools/testing/selftests/kvm/include/x86/processor_asm.h new file +12
-0@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Used for storing defines used by both processor.c and assembly code. +*/ +#ifndef SELFTEST_KVM_PROCESSOR_ASM_H +#define SELFTEST_KVM_PROCESSOR_ASM_H + +#define KERNEL_CS 0x8 +#define KERNEL_DS 0x10 +#define KERNEL_TSS 0x18 + +#endif // SELFTEST_KVM_PROCESSOR_ASM_H tools/testing/selftests/kvm/lib/x86/processor.c +1
-4@@ -7,6 +7,7 @@#include "test_util.h" #include "kvm_util.h" #include "processor.h" +#include "processor_asm.h" #include "sev.h" #include "tdx/tdx_util.h" @@ -14,10 +15,6 @@#define NUM_INTERRUPTS 256#endif -#define KERNEL_CS 0x8 -#define KERNEL_DS 0x10 -#define KERNEL_TSS 0x18 -vm_vaddr_t exception_handlers;bool host_cpu_is_amd;bool host_cpu_is_intel;Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
PATCH v9 7/19 KVM: selftests: Add kbuild definitons
In reply to Sagi ShaharAdd kbuild.h that can be used by files under tools/
Definitions are taken from the original definitions at include/linux/kbuild.h
This is needed to expose values from c code to assembly code.
--- tools/include/linux/kbuild.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tools/include/linux/kbuild.h
tools/include/linux/kbuild.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __TOOLS_LINUX_KBUILD_H +#define __TOOLS_LINUX_KBUILD_H + +#include <stddef.h> + +#define DEFINE (sym, val) \ + asm volatile("\n.ascii \"->" #sym " %0 " #val "\"" : : "i" (val)) + +#define BLANK () asm volatile("\n.ascii \"->\"" : : ) + +#define OFFSET (sym, str, mem) \ + DEFINE(sym, offsetof(struct str, mem)) + +#define COMMENT (x) \ + asm volatile("\n.ascii \"->#" x "\"") + +#endif /* __TOOLS_LINUX_KBUILD_H */
Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
PATCH v9 8/19 KVM: selftests: Define structs to pass parameters to TDX boot code
In reply to Sagi ShaharTDX registers are inaccesible to KVM. Therefore we need a different mechanism to load boot parameters for TDX code. TDX boot code will read the registers values from memory and set the registers manually.
This patch defines the data structures used to communicate between c code and the TDX assembly boot code which will be added in a later patch.
Use kbuild.h to expose the offsets into the structs from c code to assembly code.
--- tools/testing/selftests/kvm/Makefile.kvm | 18 +++++ .../selftests/kvm/include/x86/tdx/td_boot.h | 76 +++++++++++++++++++ .../kvm/lib/x86/tdx/td_boot_offsets.c | 21 +++++ 3 files changed, 115 insertions(+) create mode 100644 tools/testing/selftests/kvm/include/x86/tdx/td_boot.h create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/td_boot_offsets.c
tools/testing/selftests/kvm/Makefile.kvm +18 -0
@@ -19,6 +19,8 @@ LIBKVM += lib/userfaultfd_util.c LIBKVM_STRING += lib/string_override.c +LIBKVM_ASM_DEFS += lib/x86/tdx/td_boot_offsets.c + LIBKVM_x86 += lib/x86/apic.c LIBKVM_x86 += lib/x86/handlers.S LIBKVM_x86 += lib/x86/hyperv.c
@@ -229,6 +231,10 @@ OVERRIDE_TARGETS = 1 include ../lib.mk include ../cgroup/lib/libcgroup.mk +# Enable Kbuild tools. +include $(top_srcdir)/scripts/Kbuild.include +include $(top_srcdir)/scripts/Makefile.lib + INSTALL_HDR_PATH = $(top_srcdir)/usr LINUX_HDR_PATH = $(INSTALL_HDR_PATH)/include/ LINUX_TOOL_INCLUDE = $(top_srcdir)/tools/include
@@ -281,6 +287,7 @@ LIBKVM_S := $(filter %.S,$(LIBKVM)) LIBKVM_C_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_C)) LIBKVM_S_OBJ := $(patsubst %.S, $(OUTPUT)/%.o, $(LIBKVM_S)) LIBKVM_STRING_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_STRING)) +LIBKVM_ASM_DEFS_OBJ += $(patsubst %.c, $(OUTPUT)/%.s, $(LIBKVM_ASM_DEFS)) LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) $(LIBKVM_STRING_OBJ) $(LIBCGROUP_O) SPLIT_TEST_GEN_PROGS := $(patsubst %, $(OUTPUT)/%, $(SPLIT_TESTS)) SPLIT_TEST_GEN_OBJ := $(patsubst %, $(OUTPUT)/$(ARCH)/%.o, $(SPLIT_TESTS))
@@ -307,6 +314,7 @@ $(SPLIT_TEST_GEN_OBJ): $(OUTPUT)/$(ARCH)/%.o: $(ARCH)/%.c EXTRA_CLEAN += $(GEN_HDRS) \ $(LIBKVM_OBJS) \ + $(LIBKVM_ASM_DEFS_OBJ) \ $(SPLIT_TEST_GEN_OBJ) \ $(TEST_DEP_FILES) \ $(TEST_GEN_OBJ) \
@@ -318,18 +326,28 @@ $(LIBKVM_C_OBJ): $(OUTPUT)/%.o: %.c $(GEN_HDRS) $(LIBKVM_S_OBJ): $(OUTPUT)/%.o: %.S $(GEN_HDRS) $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@ +$(LIBKVM_ASM_DEFS_OBJ): $(OUTPUT)/%.s: %.c FORCE + $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -S $< -o $@ + # Compile the string overrides as freestanding to prevent the compiler from # generating self-referential code, e.g. without "freestanding" the compiler may # "optimize" memcmp() by invoking memcmp(), thus causing infinite recursion. $(LIBKVM_STRING_OBJ): $(OUTPUT)/%.o: %.c $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -ffreestanding $< -o $@ +$(OUTPUT)/include/x86/tdx/td_boot_offsets.h: $(OUTPUT)/lib/x86/tdx/td_boot_offsets.s FORCE + $(call filechk,offsets,__TDX_BOOT_OFFSETS_H__) + +EXTRA_CLEAN += $(OUTPUT)/include/x86/tdx/td_boot_offsets.h + $(shell mkdir -p $(sort $(dir $(TEST_GEN_PROGS)))) $(SPLIT_TEST_GEN_OBJ): $(GEN_HDRS) $(TEST_GEN_PROGS): $(LIBKVM_OBJS) $(TEST_GEN_PROGS_EXTENDED): $(LIBKVM_OBJS) $(TEST_GEN_OBJ): $(GEN_HDRS) +FORCE: + cscope: include_paths = $(LINUX_TOOL_INCLUDE) $(LINUX_HDR_PATH) include lib .. cscope: $(RM) cscope.*
tools/testing/selftests/kvm/include/x86/tdx/td_boot.h new file +76 -0
76 lines not on this page · raw message
tools/testing/selftests/kvm/lib/x86/tdx/td_boot_offsets.c new file +21 -0
@@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0 +#define COMPILE_OFFSETS + +#include <linux/kbuild.h> + +#include "tdx/td_boot.h" + +static void __attribute__((used))common (void ) +{ +OFFSET (TD_BOOT_PARAMETERS_CR0 , td_boot_parameters, cr0); +OFFSET (TD_BOOT_PARAMETERS_CR3 , td_boot_parameters, cr3); +OFFSET (TD_BOOT_PARAMETERS_CR4 , td_boot_parameters, cr4); +OFFSET (TD_BOOT_PARAMETERS_GDT , td_boot_parameters, gdtr); +OFFSET (TD_BOOT_PARAMETERS_IDT , td_boot_parameters, idtr); +OFFSET (TD_BOOT_PARAMETERS_PER_VCPU , td_boot_parameters, per_vcpu); +OFFSET (TD_PER_VCPU_PARAMETERS_ESP_GVA , td_per_vcpu_parameters, esp_gva); +OFFSET (TD_PER_VCPU_PARAMETERS_GUEST_CODE , td_per_vcpu_parameters, + guest_code); +DEFINE (SIZEOF_TD_PER_VCPU_PARAMETERS , +sizeof (struct td_per_vcpu_parameters )); +}
Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
Re: [PATCH v9 08/19] KVM: selftests: Define structs to pass parameters to TDX boot code
On 8/21/2025 12:29 PM, Sagi Shahar wrote:
[...]
tools/testing/selftests/kvm/include/x86/tdx/td_boot.h line 46
Quoted 8 lines
tools/testing/selftests/kvm/include/x86/tdx/td_boot.h new file +8
-0+ +/* +* Allows each vCPU to be initialized with different eip and esp. +* +* __packed is used since the offsets are hardcoded in td_boot.S +* +* TODO: Replace hardcoded offsets with OFFSET(). This requires getting the +* neccesry Kbuild scripts working in KVM selftests.
neccesry -> necessary
Also, are the comments about "__packed" and "TODO" out dated?
Quoted 9 lines
+ */
tools/testing/selftests/kvm/include/x86/tdx/td_boot.h new file +8
-0+struct td_per_vcpu_parameters { +uint32_t esp_gva; +uint64_t guest_code; +}; + +/* +* Boot parameters for the TD. +*
tools/testing/selftests/kvm/include/x86/tdx/td_boot.h line 65
Quoted 10 lines
tools/testing/selftests/kvm/include/x86/tdx/td_boot.h new file +10
-0+* Unlike a regular VM, KVM cannot set registers such as esp, eip, etc +* before boot, so to run selftests, these registers' values have to be +* initialized by the TD. +* +* This struct is loaded in TD private memory at TD_BOOT_PARAMETERS_GPA. +* +* The TD boot code will read off parameters from this struct and set up the +* vCPU for executing selftests. +* +* __packed is used since the offsets are hardcoded in td_boot.S
Same as above for "__packed".
Quoted 2 lines
+ */
[...]
Re: [PATCH v9 08/19] KVM: selftests: Define structs to pass parameters to TDX boot code
Quoted 15 lines · Binbin Wu
On 8/21/2025 12:29 PM, Sagi Shahar wrote:
[...]tools/testing/selftests/kvm/include/x86/tdx/td_boot.h line 46
Quoted 8 lines
tools/testing/selftests/kvm/include/x86/tdx/td_boot.h new file +8
-0+ +/* +* Allows each vCPU to be initialized with different eip and esp. +* +* __packed is used since the offsets are hardcoded in td_boot.S +* +* TODO: Replace hardcoded offsets with OFFSET(). This requires getting the +* neccesry Kbuild scripts working in KVM selftests. neccesry -> necessary
Quoted 2 lines
Also, are the comments about "
__packed" and "TODO" out dated?
Thanks, I forgot to update those.
Quoted 24 lines
Quoted 9 lines
+ */
tools/testing/selftests/kvm/include/x86/tdx/td_boot.h new file +8
-0+struct td_per_vcpu_parameters { +uint32_t esp_gva; +uint64_t guest_code; +}; + +/* +* Boot parameters for the TD. +* tools/testing/selftests/kvm/include/x86/tdx/td_boot.h line 65
Quoted 10 lines
tools/testing/selftests/kvm/include/x86/tdx/td_boot.h new file +10
-0+* Unlike a regular VM, KVM cannot set registers such as esp, eip, etc +* before boot, so to run selftests, these registers' values have to be +* initialized by the TD. +* +* This struct is loaded in TD private memory at TD_BOOT_PARAMETERS_GPA. +* +* The TD boot code will read off parameters from this struct and set up the +* vCPU for executing selftests. +* +* __packed is used since the offsets are hardcoded in td_boot.S Same as above for "
__packed".Quoted 2 lines
+ */
[...]
PATCH v9 9/19 KVM: selftests: Add TDX boot code
In reply to Sagi ShaharFrom: Erdem Aktas <[email protected]>
Add code to boot a TDX test VM. Since TDX registers are inaccesible to KVM, the boot code loads the relevant values from memory into the registers before jumping to the guest code.
--- tools/testing/selftests/kvm/Makefile.kvm | 3 + .../selftests/kvm/include/x86/tdx/td_boot.h | 5 ++ .../kvm/include/x86/tdx/td_boot_asm.h | 16 +++++ .../selftests/kvm/lib/x86/tdx/td_boot.S | 60 +++++++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 tools/testing/selftests/kvm/include/x86/tdx/td_boot_asm.h create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/td_boot.S
tools/testing/selftests/kvm/Makefile.kvm +3 -0
@@ -31,6 +31,7 @@ LIBKVM_x86 += lib/x86/sev.c LIBKVM_x86 += lib/x86/svm.c LIBKVM_x86 += lib/x86/ucall.c LIBKVM_x86 += lib/x86/vmx.c +LIBKVM_x86 += lib/x86/tdx/td_boot.S LIBKVM_arm64 += lib/arm64/gic.c LIBKVM_arm64 += lib/arm64/gic_v3.c
@@ -335,6 +336,8 @@ $(LIBKVM_ASM_DEFS_OBJ): $(OUTPUT)/%.s: %.c FORCE $(LIBKVM_STRING_OBJ): $(OUTPUT)/%.o: %.c $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -ffreestanding $< -o $@ +$(OUTPUT)/lib/x86/tdx/td_boot.o: $(OUTPUT)/include/x86/tdx/td_boot_offsets.h + $(OUTPUT)/include/x86/tdx/td_boot_offsets.h: $(OUTPUT)/lib/x86/tdx/td_boot_offsets.s FORCE $(call filechk,offsets,__TDX_BOOT_OFFSETS_H__)
tools/testing/selftests/kvm/include/x86/tdx/td_boot.h +5 -0
@@ -73,4 +73,9 @@ struct td_boot_parameters { struct td_per_vcpu_parameters per_vcpu[]; }; +void td_boot (void ); +void td_boot_code_end (void ); + +#define TD_BOOT_CODE_SIZE (td_boot_code_end - td_boot) +#endif /* SELFTEST_TDX_TD_BOOT_H */
tools/testing/selftests/kvm/include/x86/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 will be 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/lib/x86/tdx/td_boot.S new file +60 -0
60 lines not on this page · raw message
Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
PATCH v9 10/19 KVM: selftests: Set up TDX boot code region
In reply to Sagi ShaharAdd memory for TDX boot code in a separate memslot.
Use virt_map() to get identity map in this memory region to allow for seamless transition from paging disabled to paging enabled code.
Copy the boot code into the memory region and set up the reset vectors at this point. While it's possible to separate the memory allocation and boot code initialization into separate functions, having all the calculations for memory size and offsets in one place simplifies the code and avoids duplications.
Handcode the reset vector as suggested by Sean Christopherson.
--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/include/x86/tdx/tdx_util.h | 2 + .../selftests/kvm/lib/x86/tdx/tdx_util.c | 54 +++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -31,6 +31,7 @@ LIBKVM_x86 += lib/x86/sev.c LIBKVM_x86 += lib/x86/svm.c LIBKVM_x86 += lib/x86/ucall.c LIBKVM_x86 += lib/x86/vmx.c +LIBKVM_x86 += lib/x86/tdx/tdx_util.c LIBKVM_x86 += lib/x86/tdx/td_boot.S LIBKVM_arm64 += lib/arm64/gic.c
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h +2 -0
@@ -11,4 +11,6 @@ static inline bool is_tdx_vm(struct kvm_vm *vm) return vm->type == KVM_X86_TDX_VM; } +void vm_tdx_setup_boot_code_region (struct kvm_vm *vm); +#endif // SELFTESTS_TDX_TDX_UTIL_H
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c new file +54 -0
54 lines not on this page · raw message
Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
Re: [PATCH v9 10/19] KVM: selftests: Set up TDX boot code region
Quoted 92 lines · Sagi Shahar
Add memory for TDX boot code in a separate memslot.
Use
virt_map()to get identity map in this memory region to allow for seamless transition from paging disabled to paging enabled code.Copy the boot code into the memory region and set up the reset vectors at this point. While it's possible to separate the memory allocation and boot code initialization into separate functions, having all the calculations for memory size and offsets in one place simplifies the code and avoids duplications.
Handcode the reset vector as suggested by Sean Christopherson.
Suggested-by Sean Christopherson <[email protected]>Co-developed-by Erdem Aktas <[email protected]>Signed-off-by Erdem Aktas <[email protected]>Signed-off-by Sagi Shahar <[email protected]>--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/include/x86/tdx/tdx_util.h | 2 + .../selftests/kvm/lib/x86/tdx/tdx_util.c | 54 +++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.ctools/testing/selftests/kvm/Makefile.kvm +1
-0@@ -31,6 +31,7 @@ LIBKVM_x86 += lib/x86/sev.c LIBKVM_x86 += lib/x86/svm.c LIBKVM_x86 += lib/x86/ucall.c LIBKVM_x86 += lib/x86/vmx.c +LIBKVM_x86 += lib/x86/tdx/tdx_util.c LIBKVM_x86 += lib/x86/tdx/td_boot.S LIBKVM_arm64 += lib/arm64/gic.ctools/testing/selftests/kvm/include/x86/tdx/tdx_util.h +2
-0@@ -11,4 +11,6 @@ static inline bool is_tdx_vm(struct kvm_vm *vm) return vm->type == KVM_X86_TDX_VM; } +void vm_tdx_setup_boot_code_region (struct kvm_vm *vm); +#endif // SELFTESTS_TDX_TDX_UTIL_H tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c new file +39
-0@@ -0,0 +1,54 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include <stdint.h> + +#include "kvm_util.h" +#include "processor.h" +#include "tdx/td_boot.h" +#include "tdx/tdx_util.h" + +/* Arbitrarily selected to avoid overlaps with anything else */ +#define TD_BOOT_CODE_SLOT 20 + +#define X86_RESET_VECTOR 0xfffffff0ul +#define X86_RESET_VECTOR_SIZE 16 + +void vm_tdx_setup_boot_code_region (struct kvm_vm *vm) +{ +size_t total_code_size =TD_BOOT_CODE_SIZE +X86_RESET_VECTOR_SIZE ; +vm_paddr_t boot_code_gpa =X86_RESET_VECTOR -TD_BOOT_CODE_SIZE ; +vm_paddr_t alloc_gpa =round_down (boot_code_gpa,PAGE_SIZE ); +size_t nr_pages =DIV_ROUND_UP (total_code_size,PAGE_SIZE ); +vm_paddr_t gpa; +uint8_t *hva; + +vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS , + alloc_gpa, +TD_BOOT_CODE_SLOT , nr_pages, +KVM_MEM_GUEST_MEMFD ); + + gpa =vm_phy_pages_alloc (vm, nr_pages, alloc_gpa,TD_BOOT_CODE_SLOT ); +TEST_ASSERT (gpa == alloc_gpa,"Failed vm_phy_pages_alloc\n" ); + +virt_map (vm, alloc_gpa, alloc_gpa, nr_pages); + hva =addr_gpa2hva (vm, boot_code_gpa); +memcpy (hva, td_boot,TD_BOOT_CODE_SIZE ); + + hva +=TD_BOOT_CODE_SIZE ; + TEST_ASSERT(hva == addr_gpa2hva(vm, X86_RESET_VECTOR), + "Expected RESET vector at hva 0x%lx, got %lx",
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c line 49
Quoted 10 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c new file +10
-0+ (unsigned long)addr_gpa2hva(vm, X86_RESET_VECTOR), (unsigned long)hva); + +/* +* Handcode "JMP rel8" at the RESET vector to jump back to the TD boot +* code, as there are only 16 bytes at the RESET vector before RIP will +* wrap back to zero. Insert a trailing int3 so that the vCPU crashes +* in case the JMP somehow falls through. Note! The target address is +* relative to the end of the instruction! +*/ + TEST_ASSERT(TD_BOOT_CODE_SIZE < 256,
Looks TD_BOOT_CODE_SIZE needs to be <= 126, as the jump range is limited to -128 to +127 for JMP rel8.
Quoted 9 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c new file +5
-0+ "TD boot code not addressable by 'JMP rel8'"); + hva[0 ] =0xeb ; + hva[1 ] =256 -2 -TD_BOOT_CODE_SIZE ; + hva[2 ] =0xcc ; +}Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
Sean Christopherson
maintainerRe: [PATCH v9 10/19] KVM: selftests: Set up TDX boot code region
Quoted 10 lines · Yan Zhao
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c line 49
Quoted 8 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c new file +8
-0+/* +* Handcode "JMP rel8" at the RESET vector to jump back to the TD boot +* code, as there are only 16 bytes at the RESET vector before RIP will +* wrap back to zero. Insert a trailing int3 so that the vCPU crashes +* in case the JMP somehow falls through. Note! The target address is +* relative to the end of the instruction! +*/ + TEST_ASSERT(TD_BOOT_CODE_SIZE < 256,Looks
TD_BOOT_CODE_SIZEneeds to be <= 126, as the jump range is limited to -128 to +127 for JMP rel8.
Gah, I managed to forget that relative targets obviously need to be signed values, and I also forgot to account for the size of the JMP in the assert. Go me.
Maybe express this as:
TEST_ASSERT(TD_BOOT_CODE_SIZE + 2 < 128, "TD boot code not addressable by 'JMP rel8'");
Quoted 3 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c line 52
Quoted 3 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c new file +3
-0+ "TD boot code not addressable by 'JMP rel8'"); + hva[0 ] =0xeb ; + hva[1 ] =256 -2 -TD_BOOT_CODE_SIZE ;
I think I lucked into getting this right though?
Quoted 6 lines
Quoted 6 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c new file +2
-0+ hva[2 ] =0xcc ; +}Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
Re: [PATCH v9 10/19] KVM: selftests: Set up TDX boot code region
Quoted 12 lines · Sean Christopherson
Quoted 10 lines · Yan Zhao
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c line 49
Quoted 8 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c new file +8
-0+/* +* Handcode "JMP rel8" at the RESET vector to jump back to the TD boot +* code, as there are only 16 bytes at the RESET vector before RIP will +* wrap back to zero. Insert a trailing int3 so that the vCPU crashes +* in case the JMP somehow falls through. Note! The target address is +* relative to the end of the instruction! +*/ + TEST_ASSERT(TD_BOOT_CODE_SIZE < 256,Looks
TD_BOOT_CODE_SIZEneeds to be <= 126, as the jump range is limited to -128 to +127 for JMP rel8.
Quoted 7 lines
Gah, I managed to forget that relative targets obviously need to be signed values, and I also forgot to account for the size of the JMP in the assert. Go me.
Maybe express this as:
TEST_ASSERT(TD_BOOT_CODE_SIZE + 2 < 128, "TD boot code not addressable by 'JMP rel8'");
I like this version, which's is much clearer than asserting TD_BOOT_CODE_SIZE alone.
nit: TD_BOOT_CODE_SIZE + 2 can be equal to 128, i.e.,
TEST_ASSERT(TD_BOOT_CODE_SIZE + 2 <= 128, "TD boot code not addressable by 'JMP rel8'");
Quoted 5 lines
Quoted 3 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c line 52
Quoted 3 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c new file +3
-0+ "TD boot code not addressable by 'JMP rel8'"); + hva[0 ] =0xeb ; + hva[1 ] =256 -2 -TD_BOOT_CODE_SIZE ;I think I lucked into getting this right though?
Yes, this one is correct :)
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c new file +2
-0+ hva[2 ] =0xcc ; +}Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
PATCH v9 11/19 KVM: selftests: Set up TDX boot parameters region
In reply to Sagi ShaharAllocate memory for TDX boot parameters and define the utility functions necessary to fill this memory with the boot parameters.
--- .../selftests/kvm/include/x86/tdx/tdx_util.h | 4 + .../selftests/kvm/lib/x86/tdx/tdx_util.c | 73 +++++++++++++++++++ 2 files changed, 77 insertions(+)
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h +4 -0
@@ -12,5 +12,9 @@ static inline bool is_tdx_vm(struct kvm_vm *vm) }void vm_tdx_setup_boot_code_region (struct kvm_vm *vm); +void vm_tdx_setup_boot_parameters_region (struct kvm_vm *vm,uint32_t nr_runnable_vcpus); +void vm_tdx_load_common_boot_parameters (struct kvm_vm *vm); +void vm_tdx_load_vcpu_boot_parameters (struct kvm_vm *vm,struct kvm_vcpu *vcpu); +void vm_tdx_set_vcpu_entry_point (struct kvm_vcpu *vcpu,void *guest_code);#endif // SELFTESTS_TDX_TDX_UTIL_H
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +73 -0
86 lines not on this page · raw message
Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
Re: [PATCH v9 11/19] KVM: selftests: Set up TDX boot parameters region
Quoted 96 lines · Sagi Shahar
Allocate memory for TDX boot parameters and define the utility functions necessary to fill this memory with the boot parameters.
Co-developed-by Ackerley Tng <[email protected]>Signed-off-by Ackerley Tng <[email protected]>Signed-off-by Sagi Shahar <[email protected]>--- .../selftests/kvm/include/x86/tdx/tdx_util.h | 4 + .../selftests/kvm/lib/x86/tdx/tdx_util.c | 73 +++++++++++++++++++ 2 files changed, 77 insertions(+)tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h +4
-0@@ -12,5 +12,9 @@ static inline bool is_tdx_vm(struct kvm_vm *vm) }void vm_tdx_setup_boot_code_region (struct kvm_vm *vm); +void vm_tdx_setup_boot_parameters_region (struct kvm_vm *vm,uint32_t nr_runnable_vcpus); +void vm_tdx_load_common_boot_parameters (struct kvm_vm *vm); +void vm_tdx_load_vcpu_boot_parameters (struct kvm_vm *vm,struct kvm_vcpu *vcpu); +void vm_tdx_set_vcpu_entry_point (struct kvm_vcpu *vcpu,void *guest_code);#endif // SELFTESTS_TDX_TDX_UTIL_H tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +52
-0@@ -5,10 +5,12 @@#include "kvm_util.h" #include "processor.h" #include "tdx/td_boot.h" +#include "tdx/td_boot_asm.h" #include "tdx/tdx_util.h" /* Arbitrarily selected to avoid overlaps with anything else */ #define TD_BOOT_CODE_SLOT 20 +#define TD_BOOT_PARAMETERS_SLOT 21#define X86_RESET_VECTOR 0xfffffff0ul#define X86_RESET_VECTOR_SIZE 16@@ -52,3 +54,74 @@ void vm_tdx_setup_boot_code_region(struct kvm_vm *vm) hva[1 ] =256 -2 -TD_BOOT_CODE_SIZE ; hva[2 ] =0xcc ; } + +void vm_tdx_setup_boot_parameters_region (struct kvm_vm *vm,uint32_t nr_runnable_vcpus) +{ +size_t boot_params_size = +sizeof (struct td_boot_parameters ) + + nr_runnable_vcpus *sizeof (struct td_per_vcpu_parameters ); +int npages =DIV_ROUND_UP (boot_params_size,PAGE_SIZE ); +vm_paddr_t gpa; + +vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS , +TD_BOOT_PARAMETERS_GPA , +TD_BOOT_PARAMETERS_SLOT , npages, +KVM_MEM_GUEST_MEMFD ); + gpa =vm_phy_pages_alloc (vm, npages,TD_BOOT_PARAMETERS_GPA ,TD_BOOT_PARAMETERS_SLOT ); +TEST_ASSERT (gpa ==TD_BOOT_PARAMETERS_GPA ,"Failed vm_phy_pages_alloc\n" ); + +virt_map (vm,TD_BOOT_PARAMETERS_GPA ,TD_BOOT_PARAMETERS_GPA , npages); +} + +void vm_tdx_load_common_boot_parameters (struct kvm_vm *vm) +{ +struct td_boot_parameters *params = +addr_gpa2hva (vm,TD_BOOT_PARAMETERS_GPA ); +uint32_t cr4; + +TEST_ASSERT_EQ (vm->mode,VM_MODE_PXXV48_4K ); + + cr4 =kvm_get_default_cr4 (); + +/* TDX spec 11.6.2: CR4 bit MCE is fixed to 1 */ + cr4 |=X86_CR4_MCE ; + +/* Set this because UEFI also sets this up, to handle XMM exceptions */ + cr4 |=X86_CR4_OSXMMEXCPT ; + +/* TDX spec 11.6.2: CR4 bit VMXE and SMXE are fixed to 0 */ + cr4 &= ~(X86_CR4_VMXE |X86_CR4_SMXE ); + +/* Set parameters! */ + params->cr0 =kvm_get_default_cr0 (); + params->cr3 = vm->pgd; + params->cr4 = cr4; + params->idtr.base = vm->arch.idt; + params->idtr.limit =kvm_get_default_idt_limit (); + params->gdtr.base = vm->arch.gdt; + params->gdtr.limit =kvm_get_default_gdt_limit (); + +TEST_ASSERT (params->cr0 !=0 ,"cr0 should not be 0" ); +TEST_ASSERT (params->cr3 !=0 ,"cr3 should not be 0" ); +TEST_ASSERT (params->cr4 !=0 ,"cr4 should not be 0" );
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c void vm_tdx_setup_boot_code_region(struct kvm_vm *vm) · line 116
Quoted 10 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +10
-0+TEST_ASSERT (params->gdtr.base !=0 ,"gdt base address should not be 0" ); +TEST_ASSERT (params->idtr.base !=0 ,"idt base address should not be 0" ); +} + +void vm_tdx_load_vcpu_boot_parameters (struct kvm_vm *vm,struct kvm_vcpu *vcpu) +{ +struct td_boot_parameters *params = +addr_gpa2hva (vm,TD_BOOT_PARAMETERS_GPA ); +struct td_per_vcpu_parameters *vcpu_params = + ¶ms->per_vcpu[vcpu->id];
Nit: Better to align the style on whether wrap or not due to max characters per line
Quoted 11 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +11
-0+ + vcpu_params->esp_gva =kvm_allocate_vcpu_stack (vm); +} + +void vm_tdx_set_vcpu_entry_point (struct kvm_vcpu *vcpu,void *guest_code) +{ +struct td_boot_parameters *params =addr_gpa2hva (vcpu->vm,TD_BOOT_PARAMETERS_GPA ); +struct td_per_vcpu_parameters *vcpu_params = ¶ms->per_vcpu[vcpu->id]; + + vcpu_params->guest_code = (uint64_t )guest_code; +}
Re: [PATCH v9 11/19] KVM: selftests: Set up TDX boot parameters region
Quoted 105 lines · Binbin Wu
Quoted 101 lines · Sagi Shahar
Allocate memory for TDX boot parameters and define the utility functions necessary to fill this memory with the boot parameters.
Co-developed-by Ackerley Tng <[email protected]>Signed-off-by Ackerley Tng <[email protected]>Signed-off-by Sagi Shahar <[email protected]>--- .../selftests/kvm/include/x86/tdx/tdx_util.h | 4 + .../selftests/kvm/lib/x86/tdx/tdx_util.c | 73 +++++++++++++++++++ 2 files changed, 77 insertions(+)tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h +4
-0@@ -12,5 +12,9 @@ static inline bool is_tdx_vm(struct kvm_vm *vm) }void vm_tdx_setup_boot_code_region (struct kvm_vm *vm); +void vm_tdx_setup_boot_parameters_region (struct kvm_vm *vm,uint32_t nr_runnable_vcpus); +void vm_tdx_load_common_boot_parameters (struct kvm_vm *vm); +void vm_tdx_load_vcpu_boot_parameters (struct kvm_vm *vm,struct kvm_vcpu *vcpu); +void vm_tdx_set_vcpu_entry_point (struct kvm_vcpu *vcpu,void *guest_code);#endif // SELFTESTS_TDX_TDX_UTIL_H tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +57
-0@@ -5,10 +5,12 @@#include "kvm_util.h" #include "processor.h" #include "tdx/td_boot.h" +#include "tdx/td_boot_asm.h" #include "tdx/tdx_util.h" /* Arbitrarily selected to avoid overlaps with anything else */ #define TD_BOOT_CODE_SLOT 20 +#define TD_BOOT_PARAMETERS_SLOT 21#define X86_RESET_VECTOR 0xfffffff0ul#define X86_RESET_VECTOR_SIZE 16@@ -52,3 +54,74 @@ void vm_tdx_setup_boot_code_region(struct kvm_vm *vm) hva[1 ] =256 -2 -TD_BOOT_CODE_SIZE ; hva[2 ] =0xcc ; } + +void vm_tdx_setup_boot_parameters_region (struct kvm_vm *vm,uint32_t nr_runnable_vcpus) +{ +size_t boot_params_size = +sizeof (struct td_boot_parameters ) + + nr_runnable_vcpus *sizeof (struct td_per_vcpu_parameters ); +int npages =DIV_ROUND_UP (boot_params_size,PAGE_SIZE ); +vm_paddr_t gpa; + +vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS , +TD_BOOT_PARAMETERS_GPA , +TD_BOOT_PARAMETERS_SLOT , npages, +KVM_MEM_GUEST_MEMFD ); + gpa =vm_phy_pages_alloc (vm, npages,TD_BOOT_PARAMETERS_GPA ,TD_BOOT_PARAMETERS_SLOT ); +TEST_ASSERT (gpa ==TD_BOOT_PARAMETERS_GPA ,"Failed vm_phy_pages_alloc\n" ); + +virt_map (vm,TD_BOOT_PARAMETERS_GPA ,TD_BOOT_PARAMETERS_GPA , npages); +} + +void vm_tdx_load_common_boot_parameters (struct kvm_vm *vm) +{ +struct td_boot_parameters *params = +addr_gpa2hva (vm,TD_BOOT_PARAMETERS_GPA ); +uint32_t cr4; + +TEST_ASSERT_EQ (vm->mode,VM_MODE_PXXV48_4K ); + + cr4 =kvm_get_default_cr4 (); + +/* TDX spec 11.6.2: CR4 bit MCE is fixed to 1 */ + cr4 |=X86_CR4_MCE ; + +/* Set this because UEFI also sets this up, to handle XMM exceptions */ + cr4 |=X86_CR4_OSXMMEXCPT ; + +/* TDX spec 11.6.2: CR4 bit VMXE and SMXE are fixed to 0 */ + cr4 &= ~(X86_CR4_VMXE |X86_CR4_SMXE ); + +/* Set parameters! */ + params->cr0 =kvm_get_default_cr0 (); + params->cr3 = vm->pgd; + params->cr4 = cr4; + params->idtr.base = vm->arch.idt; + params->idtr.limit =kvm_get_default_idt_limit (); + params->gdtr.base = vm->arch.gdt; + params->gdtr.limit =kvm_get_default_gdt_limit (); + +TEST_ASSERT (params->cr0 !=0 ,"cr0 should not be 0" ); +TEST_ASSERT (params->cr3 !=0 ,"cr3 should not be 0" ); +TEST_ASSERT (params->cr4 !=0 ,"cr4 should not be 0" ); +TEST_ASSERT (params->gdtr.base !=0 ,"gdt base address should not be 0" ); +TEST_ASSERT (params->idtr.base !=0 ,"idt base address should not be 0" ); +} + +void vm_tdx_load_vcpu_boot_parameters (struct kvm_vm *vm,struct kvm_vcpu *vcpu)
Quoted 9 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c void vm_tdx_setup_boot_code_region(struct kvm_vm *vm) · line 116
Quoted 5 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +5
-0+{ +struct td_boot_parameters *params = +addr_gpa2hva (vm,TD_BOOT_PARAMETERS_GPA ); +struct td_per_vcpu_parameters *vcpu_params = + ¶ms->per_vcpu[vcpu->id];Nit: Better to align the style on whether wrap or not due to max characters per line
Thanks, going to wrap in both functions.
Quoted 12 lines
Quoted 11 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +11
-0+ + vcpu_params->esp_gva =kvm_allocate_vcpu_stack (vm); +} + +void vm_tdx_set_vcpu_entry_point (struct kvm_vcpu *vcpu,void *guest_code) +{ +struct td_boot_parameters *params =addr_gpa2hva (vcpu->vm,TD_BOOT_PARAMETERS_GPA ); +struct td_per_vcpu_parameters *vcpu_params = ¶ms->per_vcpu[vcpu->id]; + + vcpu_params->guest_code = (uint64_t )guest_code; +}
PATCH v9 12/19 KVM: selftests: Add helper to initialize TDX VM
In reply to Sagi ShaharKVM_TDX_INIT_VM needs to be called after KVM_CREATE_VM and before creating any VCPUs, thus before KVM_SET_CPUID2. KVM_TDX_INIT_VM accepts the CPUID values directly.
Since KVM_GET_CPUID2 can't be used at this point, calculate the CPUID values manually by using kvm_get_supported_cpuid() and filter the returned CPUIDs against the supported CPUID values read from the TDX module.
--- .../selftests/kvm/include/x86/tdx/tdx_util.h | 54 +++++++ .../selftests/kvm/lib/x86/tdx/tdx_util.c | 132 ++++++++++++++++++ 2 files changed, 186 insertions(+)
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h +54 -0
@@ -11,6 +11,60 @@ static inline bool is_tdx_vm(struct kvm_vm *vm)return vm->type ==KVM_X86_TDX_VM ; } +/* +* TDX ioctls +*/ + +#define __vm_tdx_vm_ioctl (vm, cmd, metadata, arg) \ +({ \ + int r; \ + \ + union { \ + struct kvm_tdx_cmd c; \ + unsigned long raw; \ + } tdx_cmd = { .c = { \ + .id = (cmd), \ + .flags = (uint32_t)(metadata), \ + .data = (uint64_t)(arg), \ + } }; \ + \ + r = __vm_ioctl(vm, KVM_MEMORY_ENCRYPT_OP, &tdx_cmd.raw); \ + r ?: tdx_cmd.c.hw_error; \ +}) + +#define vm_tdx_vm_ioctl (vm, cmd, flags, arg) \ +({ \ + int ret = __vm_tdx_vm_ioctl(vm, cmd, flags, arg); \ + \ + __TEST_ASSERT_VM_VCPU_IOCTL(!ret, #cmd, ret, vm); \ +}) + +#define __vm_tdx_vcpu_ioctl (vcpu, cmd, metadata, arg) \ +({ \ + int r; \ + \ + union { \ + struct kvm_tdx_cmd c; \ + unsigned long raw; \ + } tdx_cmd = { .c = { \ + .id = (cmd), \ + .flags = (uint32_t)(metadata), \ + .data = (uint64_t)(arg), \ + } }; \ + \ + r = __vcpu_ioctl(vcpu, KVM_MEMORY_ENCRYPT_OP, &tdx_cmd.raw); \ + r ?: tdx_cmd.c.hw_error; \ +}) + +#define vm_tdx_vcpu_ioctl (vcpu, cmd, flags, arg) \ +({ \ + int ret = __vm_tdx_vcpu_ioctl(vcpu, cmd, flags, arg); \ + \ + __TEST_ASSERT_VM_VCPU_IOCTL(!ret, #cmd, ret, (vcpu)->vm); \ +}) + +void vm_tdx_init_vm (struct kvm_vm *vm,uint64_t attributes); +void vm_tdx_setup_boot_code_region (struct kvm_vm *vm);void vm_tdx_setup_boot_parameters_region (struct kvm_vm *vm,uint32_t nr_runnable_vcpus);void vm_tdx_load_common_boot_parameters (struct kvm_vm *vm);
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +132 -0
135 lines not on this page · raw message
Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
PATCH v9 13/19 KVM: selftests: TDX: Use KVM_TDX_CAPABILITIES to validate TDs' attribute configuration
In reply to Sagi ShaharFrom: Isaku Yamahata <[email protected]>
This also exercises the KVM_TDX_CAPABILITIES ioctl.
--- .../selftests/kvm/lib/x86/tdx/tdx_util.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +17 -0
@@ -232,6 +232,21 @@ static void vm_tdx_filter_cpuid(struct kvm_vm *vm,free (tdx_cap); } +static void tdx_check_attributes (struct kvm_vm *vm,uint64_t attributes) +{ +struct kvm_tdx_capabilities *tdx_cap; + + tdx_cap =tdx_read_capabilities (vm); + +/* TDX spec: any bits 0 in supported_attrs must be 0 in attributes */ +TEST_ASSERT_EQ (attributes & ~tdx_cap->supported_attrs,0 ); + +/* TDX spec: any bits 1 in attributes must be 1 in supported_attrs */ +TEST_ASSERT_EQ (attributes & tdx_cap->supported_attrs, attributes); + +free (tdx_cap); +} +void vm_tdx_init_vm (struct kvm_vm *vm,uint64_t attributes) {struct kvm_tdx_init_vm *init_vm;
@@ -251,6 +266,8 @@ void vm_tdx_init_vm(struct kvm_vm *vm, uint64_t attributes)memcpy (&init_vm->cpuid, cpuid,kvm_cpuid2_size (cpuid->nent));free (cpuid); +tdx_check_attributes (vm, attributes); + init_vm->attributes = attributes;vm_tdx_vm_ioctl (vm,KVM_TDX_INIT_VM ,0 , init_vm);
Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
Re: [PATCH v9 13/19] KVM: selftests: TDX: Use KVM_TDX_CAPABILITIES to validate TDs' attribute configuration
Quoted 3 lines · Sagi Shahar
From: Isaku Yamahata <[email protected]>
This also exercises the
KVM_TDX_CAPABILITIESioctl.
That commit message should describe what the patch does instead of relying on the title/short log.
Quoted 16 lines
Signed-off-by Isaku Yamahata <[email protected]>Co-developed-by Sagi Shahar <[email protected]>Signed-off-by Sagi Shahar <[email protected]>--- .../selftests/kvm/lib/x86/tdx/tdx_util.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +0
-0@@ -232,6 +232,21 @@ static void vm_tdx_filter_cpuid(struct kvm_vm *vm,free (tdx_cap); }
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c static void vm_tdx_filter_cpuid(struct kvm_vm *vm, · line 244
Quoted 10 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +10
-0+static void tdx_check_attributes (struct kvm_vm *vm,uint64_t attributes) +{ +struct kvm_tdx_capabilities *tdx_cap; + + tdx_cap =tdx_read_capabilities (vm); + +/* TDX spec: any bits 0 in supported_attrs must be 0 in attributes */ +TEST_ASSERT_EQ (attributes & ~tdx_cap->supported_attrs,0 ); + +/* TDX spec: any bits 1 in attributes must be 1 in supported_attrs */
The comments are not accurate.
The descriptions in TDX spec are for ATTRIBUTES_ FIXED0 and ATTRIBUTES_ FIXED1. They are related to tdx_cap->supported_attrs returned by KVM, but they are not the same.
Quoted 17 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +7
-0+TEST_ASSERT_EQ (attributes & tdx_cap->supported_attrs, attributes); + +free (tdx_cap); +} +void vm_tdx_init_vm (struct kvm_vm *vm,uint64_t attributes) {struct kvm_tdx_init_vm *init_vm;@@ -251,6 +266,8 @@ void vm_tdx_init_vm(struct kvm_vm *vm, uint64_t attributes)memcpy (&init_vm->cpuid, cpuid,kvm_cpuid2_size (cpuid->nent));free (cpuid); +tdx_check_attributes (vm, attributes); + init_vm->attributes = attributes;vm_tdx_vm_ioctl (vm,KVM_TDX_INIT_VM ,0 , init_vm);
Re: [PATCH v9 13/19] KVM: selftests: TDX: Use KVM_TDX_CAPABILITIES to validate TDs' attribute configuration
Quoted 40 lines · Binbin Wu
Quoted 3 lines · Sagi Shahar
From: Isaku Yamahata <[email protected]>
This also exercises the
KVM_TDX_CAPABILITIESioctl.That commit message should describe what the patch does instead of relying on the title/short log.
Quoted 16 lines
Signed-off-by Isaku Yamahata <[email protected]>Co-developed-by Sagi Shahar <[email protected]>Signed-off-by Sagi Shahar <[email protected]>--- .../selftests/kvm/lib/x86/tdx/tdx_util.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +0
-0@@ -232,6 +232,21 @@ static void vm_tdx_filter_cpuid(struct kvm_vm *vm,free (tdx_cap); }tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c static void vm_tdx_filter_cpuid(struct kvm_vm *vm, · line 244
Quoted 10 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +10
-0+static void tdx_check_attributes (struct kvm_vm *vm,uint64_t attributes) +{ +struct kvm_tdx_capabilities *tdx_cap; + + tdx_cap =tdx_read_capabilities (vm); + +/* TDX spec: any bits 0 in supported_attrs must be 0 in attributes */ +TEST_ASSERT_EQ (attributes & ~tdx_cap->supported_attrs,0 ); + +/* TDX spec: any bits 1 in attributes must be 1 in supported_attrs */ The comments are not accurate.
Quoted 4 lines
The descriptions in TDX spec are for
ATTRIBUTES_FIXED0 andATTRIBUTES_FIXED1. They are related totdx_cap->supported_attrsreturned by KVM, but they are not the same.
I actually think that one of the conditions is redundant. Here's my reasoning:
If a bit is 0 in attributes then both conditions will be true regardless of the value of supported_attrs.
If a bit is 1 in attributes then both conditions will be true iff the corresponding bit in supported_attrs is 1.
I'm going to keep only the second condition which is clearer and update the comment.
Quoted 19 lines
Quoted 18 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +7
-0+TEST_ASSERT_EQ (attributes & tdx_cap->supported_attrs, attributes); + +free (tdx_cap); +} +void vm_tdx_init_vm (struct kvm_vm *vm,uint64_t attributes) {struct kvm_tdx_init_vm *init_vm;@@ -251,6 +266,8 @@ void vm_tdx_init_vm(struct kvm_vm *vm, uint64_t attributes)memcpy (&init_vm->cpuid, cpuid,kvm_cpuid2_size (cpuid->nent));free (cpuid); +tdx_check_attributes (vm, attributes); + init_vm->attributes = attributes;vm_tdx_vm_ioctl (vm,KVM_TDX_INIT_VM ,0 , init_vm);
PATCH v9 14/19 KVM: selftests: Add helpers to init TDX memory and finalize VM
In reply to Sagi ShaharFrom: Ackerley Tng <[email protected]>
TDX protected memory needs to be measured and encrypted before it can be used by the guest. Traverse the VM's memory regions and initialize all the protected ranges by calling KVM_TDX_INIT_MEM_REGION.
Once all the memory is initialized, the VM can be finalized by calling KVM_TDX_FINALIZE_VM.
--- .../selftests/kvm/include/x86/tdx/tdx_util.h | 2 + .../selftests/kvm/lib/x86/tdx/tdx_util.c | 97 +++++++++++++++++++ 2 files changed, 99 insertions(+)
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h +2 -0
@@ -71,4 +71,6 @@ void vm_tdx_load_common_boot_parameters(struct kvm_vm *vm);void vm_tdx_load_vcpu_boot_parameters (struct kvm_vm *vm,struct kvm_vcpu *vcpu);void vm_tdx_set_vcpu_entry_point (struct kvm_vcpu *vcpu,void *guest_code); +void vm_tdx_finalize (struct kvm_vm *vm); +#endif // SELFTESTS_TDX_TDX_UTIL_H
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +97 -0
100 lines not on this page · raw message
Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
Re: [PATCH v9 14/19] KVM: selftests: Add helpers to init TDX memory and finalize VM
Quoted 113 lines · Sagi Shahar
From: Ackerley Tng <[email protected]>
TDX protected memory needs to be measured and encrypted before it can be used by the guest. Traverse the VM's memory regions and initialize all the protected ranges by calling
KVM_TDX_INIT_MEM_REGION.Once all the memory is initialized, the VM can be finalized by calling
KVM_TDX_FINALIZE_VM.Signed-off-by Ackerley Tng <[email protected]>Co-developed-by Erdem Aktas <[email protected]>Signed-off-by Erdem Aktas <[email protected]>Co-developed-by Sagi Shahar <[email protected]>Signed-off-by Sagi Shahar <[email protected]>--- .../selftests/kvm/include/x86/tdx/tdx_util.h | 2 + .../selftests/kvm/lib/x86/tdx/tdx_util.c | 97 +++++++++++++++++++ 2 files changed, 99 insertions(+)tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h +2
-0@@ -71,4 +71,6 @@ void vm_tdx_load_common_boot_parameters(struct kvm_vm *vm);void vm_tdx_load_vcpu_boot_parameters (struct kvm_vm *vm,struct kvm_vcpu *vcpu);void vm_tdx_set_vcpu_entry_point (struct kvm_vcpu *vcpu,void *guest_code); +void vm_tdx_finalize (struct kvm_vm *vm); +#endif // SELFTESTS_TDX_TDX_UTIL_H tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +75
-0@@ -274,3 +274,100 @@ void vm_tdx_init_vm(struct kvm_vm *vm, uint64_t attributes)free (init_vm); } + +static void tdx_init_mem_region (struct kvm_vm *vm,void *source_pages, +uint64_t gpa,uint64_t size) +{ +uint32_t metadata =KVM_TDX_MEASURE_MEMORY_REGION ; +struct kvm_tdx_init_mem_region mem_region = { + .source_addr = (uint64_t )source_pages, + .gpa = gpa, + .nr_pages = size /PAGE_SIZE , + }; +struct kvm_vcpu *vcpu; + + vcpu = list_first_entry_or_null(&vm->vcpus, struct kvm_vcpu, list); + +TEST_ASSERT ((mem_region.nr_pages >0 ) && + ((mem_region.nr_pages *PAGE_SIZE ) == size), +"Cannot add partial pages to the guest memory.\n" ); +TEST_ASSERT (((uint64_t )source_pages & (PAGE_SIZE -1 )) ==0 , +"Source memory buffer is not page aligned\n" ); +vm_tdx_vcpu_ioctl (vcpu,KVM_TDX_INIT_MEM_REGION , metadata, &mem_region); +} + +static void tdx_init_pages (struct kvm_vm *vm,void *hva,uint64_t gpa, +uint64_t size) +{ +void *scratch_page =calloc (1 ,PAGE_SIZE ); +uint64_t nr_pages = size /PAGE_SIZE ; +int i; + +TEST_ASSERT (scratch_page, +"Could not allocate memory for loading memory region" ); + +for (i =0 ; i < nr_pages; i++) { +memcpy (scratch_page, hva,PAGE_SIZE ); + +tdx_init_mem_region (vm, scratch_page, gpa,PAGE_SIZE ); + + hva +=PAGE_SIZE ; + gpa +=PAGE_SIZE ; + } + +free (scratch_page); +} + +static void load_td_private_memory (struct kvm_vm *vm) +{ +struct userspace_mem_region *region; +int ctr; + +hash_for_each (vm->regions.slot_hash, ctr, region, slot_node) { +const struct sparsebit *protected_pages = region->protected_phy_pages; +const vm_paddr_t gpa_base = region->region.guest_phys_addr; +const uint64_t hva_base = region->region.userspace_addr; +const sparsebit_idx_t lowest_page_in_region = gpa_base >> vm->page_shift; + +sparsebit_idx_t i; +sparsebit_idx_t j; + +if (!sparsebit_any_set (protected_pages)) +continue ; + +sparsebit_for_each_set_range (protected_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; + +vm_set_memory_attributes (vm, gpa, size_to_load, +KVM_MEMORY_ATTRIBUTE_PRIVATE ); + +/* +* Here, memory is being loaded from hva to gpa. If the memory +* mapped to hva is also used to back gpa, then a copy has to be +* made just for loading, since KVM_TDX_INIT_MEM_REGION ioctl
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c void vm_tdx_init_vm(struct kvm_vm *vm, uint64_t attributes) · line 361
Quoted 10 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +10
-0+* cannot encrypt memory in place. +* +* To determine if memory mapped to hva is also used to back +* gpa, use a heuristic: +* +* If this memslot has guest_memfd, then this memslot should +* have memory backed from two sources: hva for shared memory +* and gpa will be backed by guest_memfd. +*/ +if (region->region.guest_memfd ==-1 )
Why to pass !guest_memfd region to tdx_init_mem_region()?
Quoted 16 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +12
-0+tdx_init_pages (vm, (void *)hva, gpa, size_to_load); + else + tdx_init_mem_region(vm, (void *)hva, gpa, size_to_load); + } + } +} + +void vm_tdx_finalize (struct kvm_vm *vm) +{ +load_td_private_memory (vm); +vm_tdx_vm_ioctl (vm,KVM_TDX_FINALIZE_VM ,0 ,NULL ); +}Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
Re: [PATCH v9 14/19] KVM: selftests: Add helpers to init TDX memory and finalize VM
Quoted 118 lines · Yan Zhao
Quoted 116 lines · Sagi Shahar
From: Ackerley Tng <[email protected]>
TDX protected memory needs to be measured and encrypted before it can be used by the guest. Traverse the VM's memory regions and initialize all the protected ranges by calling
KVM_TDX_INIT_MEM_REGION.Once all the memory is initialized, the VM can be finalized by calling
KVM_TDX_FINALIZE_VM.Signed-off-by Ackerley Tng <[email protected]>Co-developed-by Erdem Aktas <[email protected]>Signed-off-by Erdem Aktas <[email protected]>Co-developed-by Sagi Shahar <[email protected]>Signed-off-by Sagi Shahar <[email protected]>--- .../selftests/kvm/include/x86/tdx/tdx_util.h | 2 + .../selftests/kvm/lib/x86/tdx/tdx_util.c | 97 +++++++++++++++++++ 2 files changed, 99 insertions(+)tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h +2
-0@@ -71,4 +71,6 @@ void vm_tdx_load_common_boot_parameters(struct kvm_vm *vm);void vm_tdx_load_vcpu_boot_parameters (struct kvm_vm *vm,struct kvm_vcpu *vcpu);void vm_tdx_set_vcpu_entry_point (struct kvm_vcpu *vcpu,void *guest_code); +void vm_tdx_finalize (struct kvm_vm *vm); +#endif // SELFTESTS_TDX_TDX_UTIL_H tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +78
-0@@ -274,3 +274,100 @@ void vm_tdx_init_vm(struct kvm_vm *vm, uint64_t attributes)free (init_vm); } + +static void tdx_init_mem_region (struct kvm_vm *vm,void *source_pages, +uint64_t gpa,uint64_t size) +{ +uint32_t metadata =KVM_TDX_MEASURE_MEMORY_REGION ; +struct kvm_tdx_init_mem_region mem_region = { + .source_addr = (uint64_t )source_pages, + .gpa = gpa, + .nr_pages = size /PAGE_SIZE , + }; +struct kvm_vcpu *vcpu; + + vcpu = list_first_entry_or_null(&vm->vcpus, struct kvm_vcpu, list); + +TEST_ASSERT ((mem_region.nr_pages >0 ) && + ((mem_region.nr_pages *PAGE_SIZE ) == size), +"Cannot add partial pages to the guest memory.\n" ); +TEST_ASSERT (((uint64_t )source_pages & (PAGE_SIZE -1 )) ==0 , +"Source memory buffer is not page aligned\n" ); +vm_tdx_vcpu_ioctl (vcpu,KVM_TDX_INIT_MEM_REGION , metadata, &mem_region); +} + +static void tdx_init_pages (struct kvm_vm *vm,void *hva,uint64_t gpa, +uint64_t size) +{ +void *scratch_page =calloc (1 ,PAGE_SIZE ); +uint64_t nr_pages = size /PAGE_SIZE ; +int i; + +TEST_ASSERT (scratch_page, +"Could not allocate memory for loading memory region" ); + +for (i =0 ; i < nr_pages; i++) { +memcpy (scratch_page, hva,PAGE_SIZE ); + +tdx_init_mem_region (vm, scratch_page, gpa,PAGE_SIZE ); + + hva +=PAGE_SIZE ; + gpa +=PAGE_SIZE ; + } + +free (scratch_page); +} + +static void load_td_private_memory (struct kvm_vm *vm) +{ +struct userspace_mem_region *region; +int ctr; + +hash_for_each (vm->regions.slot_hash, ctr, region, slot_node) { +const struct sparsebit *protected_pages = region->protected_phy_pages; +const vm_paddr_t gpa_base = region->region.guest_phys_addr; +const uint64_t hva_base = region->region.userspace_addr; +const sparsebit_idx_t lowest_page_in_region = gpa_base >> vm->page_shift; + +sparsebit_idx_t i; +sparsebit_idx_t j; + +if (!sparsebit_any_set (protected_pages)) +continue ; + +sparsebit_for_each_set_range (protected_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; + +vm_set_memory_attributes (vm, gpa, size_to_load, +KVM_MEMORY_ATTRIBUTE_PRIVATE ); + +/* +* Here, memory is being loaded from hva to gpa. If the memory +* mapped to hva is also used to back gpa, then a copy has to be +* made just for loading, since KVM_TDX_INIT_MEM_REGION ioctl +* cannot encrypt memory in place. +* +* To determine if memory mapped to hva is also used to back
Quoted 9 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c void vm_tdx_init_vm(struct kvm_vm *vm, uint64_t attributes) · line 361
Quoted 7 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +7
-0+* gpa, use a heuristic: +* +* If this memslot has guest_memfd, then this memslot should +* have memory backed from two sources: hva for shared memory +* and gpa will be backed by guest_memfd. +*/ +if (region->region.guest_memfd ==-1 )Why to pass !guest_memfd region to
tdx_init_mem_region()?
Not sure I understand your comment.
Quoted 17 lines
Quoted 17 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +12
-1+tdx_init_pages (vm, (void *)hva, gpa, size_to_load); + else + tdx_init_mem_region(vm, (void *)hva, gpa, size_to_load); + } + } +} + +void vm_tdx_finalize (struct kvm_vm *vm) +{ +load_td_private_memory (vm); +vm_tdx_vm_ioctl (vm,KVM_TDX_FINALIZE_VM ,0 ,NULL ); +} --2.51.0.rc1.193.gad69d77794-goog
Re: [PATCH v9 14/19] KVM: selftests: Add helpers to init TDX memory and finalize VM
Quoted 129 lines · Sagi Shahar
Quoted 118 lines · Yan Zhao
Quoted 116 lines · Sagi Shahar
From: Ackerley Tng <[email protected]>
TDX protected memory needs to be measured and encrypted before it can be used by the guest. Traverse the VM's memory regions and initialize all the protected ranges by calling
KVM_TDX_INIT_MEM_REGION.Once all the memory is initialized, the VM can be finalized by calling
KVM_TDX_FINALIZE_VM.Signed-off-by Ackerley Tng <[email protected]>Co-developed-by Erdem Aktas <[email protected]>Signed-off-by Erdem Aktas <[email protected]>Co-developed-by Sagi Shahar <[email protected]>Signed-off-by Sagi Shahar <[email protected]>--- .../selftests/kvm/include/x86/tdx/tdx_util.h | 2 + .../selftests/kvm/lib/x86/tdx/tdx_util.c | 97 +++++++++++++++++++ 2 files changed, 99 insertions(+)tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h +2
-0@@ -71,4 +71,6 @@ void vm_tdx_load_common_boot_parameters(struct kvm_vm *vm);void vm_tdx_load_vcpu_boot_parameters (struct kvm_vm *vm,struct kvm_vcpu *vcpu);void vm_tdx_set_vcpu_entry_point (struct kvm_vcpu *vcpu,void *guest_code); +void vm_tdx_finalize (struct kvm_vm *vm); +#endif // SELFTESTS_TDX_TDX_UTIL_H tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +78
-0@@ -274,3 +274,100 @@ void vm_tdx_init_vm(struct kvm_vm *vm, uint64_t attributes)free (init_vm); } + +static void tdx_init_mem_region (struct kvm_vm *vm,void *source_pages, +uint64_t gpa,uint64_t size) +{ +uint32_t metadata =KVM_TDX_MEASURE_MEMORY_REGION ; +struct kvm_tdx_init_mem_region mem_region = { + .source_addr = (uint64_t )source_pages, + .gpa = gpa, + .nr_pages = size /PAGE_SIZE , + }; +struct kvm_vcpu *vcpu; + + vcpu = list_first_entry_or_null(&vm->vcpus, struct kvm_vcpu, list); + +TEST_ASSERT ((mem_region.nr_pages >0 ) && + ((mem_region.nr_pages *PAGE_SIZE ) == size), +"Cannot add partial pages to the guest memory.\n" ); +TEST_ASSERT (((uint64_t )source_pages & (PAGE_SIZE -1 )) ==0 , +"Source memory buffer is not page aligned\n" ); +vm_tdx_vcpu_ioctl (vcpu,KVM_TDX_INIT_MEM_REGION , metadata, &mem_region); +} + +static void tdx_init_pages (struct kvm_vm *vm,void *hva,uint64_t gpa, +uint64_t size) +{ +void *scratch_page =calloc (1 ,PAGE_SIZE ); +uint64_t nr_pages = size /PAGE_SIZE ; +int i; + +TEST_ASSERT (scratch_page, +"Could not allocate memory for loading memory region" ); + +for (i =0 ; i < nr_pages; i++) { +memcpy (scratch_page, hva,PAGE_SIZE ); + +tdx_init_mem_region (vm, scratch_page, gpa,PAGE_SIZE ); + + hva +=PAGE_SIZE ; + gpa +=PAGE_SIZE ; + } + +free (scratch_page); +} + +static void load_td_private_memory (struct kvm_vm *vm) +{ +struct userspace_mem_region *region; +int ctr; + +hash_for_each (vm->regions.slot_hash, ctr, region, slot_node) { +const struct sparsebit *protected_pages = region->protected_phy_pages; +const vm_paddr_t gpa_base = region->region.guest_phys_addr; +const uint64_t hva_base = region->region.userspace_addr; +const sparsebit_idx_t lowest_page_in_region = gpa_base >> vm->page_shift; + +sparsebit_idx_t i; +sparsebit_idx_t j; + +if (!sparsebit_any_set (protected_pages)) +continue ; + +sparsebit_for_each_set_range (protected_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; + +vm_set_memory_attributes (vm, gpa, size_to_load, +KVM_MEMORY_ATTRIBUTE_PRIVATE ); + +/* +* Here, memory is being loaded from hva to gpa. If the memory +* mapped to hva is also used to back gpa, then a copy has to be +* made just for loading, since KVM_TDX_INIT_MEM_REGION ioctl +* cannot encrypt memory in place. +* +* To determine if memory mapped to hva is also used to back Quoted 9 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c void vm_tdx_init_vm(struct kvm_vm *vm, uint64_t attributes) · line 361
Quoted 7 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +7
-0+* gpa, use a heuristic: +* +* If this memslot has guest_memfd, then this memslot should +* have memory backed from two sources: hva for shared memory +* and gpa will be backed by guest_memfd. +*/ +if (region->region.guest_memfd ==-1 )Why to pass !guest_memfd region to
tdx_init_mem_region()?
Quoted 1 line
Not sure I understand your comment.
From the implementation of tdx_init_pages(), it also invokes tdx_init_mem_region(), which further invokes ioctl KVM_TDX_INIT_MEM_REGION.
However, if the region is with guest_memfd == -1, the ioctl KVM_TDX_INIT_MEM_REGION should fail as kvm_gmem_populate() won't succeed.
So, I'm wondering why there's a need to for the case of "region->region.guest_memfd == -1".
Or anything I missed?
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +12
-1+tdx_init_pages (vm, (void *)hva, gpa, size_to_load); + else + tdx_init_mem_region(vm, (void *)hva, gpa, size_to_load); + } + } +} + +void vm_tdx_finalize (struct kvm_vm *vm) +{ +load_td_private_memory (vm); +vm_tdx_vm_ioctl (vm,KVM_TDX_FINALIZE_VM ,0 ,NULL ); +} --2.51.0.rc1.193.gad69d77794-goog
Re: [PATCH v9 14/19] KVM: selftests: Add helpers to init TDX memory and finalize VM
Quoted 132 lines · Yan Zhao
Quoted 127 lines · Sagi Shahar
Quoted 117 lines · Yan Zhao
Quoted 116 lines · Sagi Shahar
From: Ackerley Tng <[email protected]>
TDX protected memory needs to be measured and encrypted before it can be used by the guest. Traverse the VM's memory regions and initialize all the protected ranges by calling
KVM_TDX_INIT_MEM_REGION.Once all the memory is initialized, the VM can be finalized by calling
KVM_TDX_FINALIZE_VM.Signed-off-by Ackerley Tng <[email protected]>Co-developed-by Erdem Aktas <[email protected]>Signed-off-by Erdem Aktas <[email protected]>Co-developed-by Sagi Shahar <[email protected]>Signed-off-by Sagi Shahar <[email protected]>--- .../selftests/kvm/include/x86/tdx/tdx_util.h | 2 + .../selftests/kvm/lib/x86/tdx/tdx_util.c | 97 +++++++++++++++++++ 2 files changed, 99 insertions(+)tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h +2
-0@@ -71,4 +71,6 @@ void vm_tdx_load_common_boot_parameters(struct kvm_vm *vm);void vm_tdx_load_vcpu_boot_parameters (struct kvm_vm *vm,struct kvm_vcpu *vcpu);void vm_tdx_set_vcpu_entry_point (struct kvm_vcpu *vcpu,void *guest_code); +void vm_tdx_finalize (struct kvm_vm *vm); +#endif // SELFTESTS_TDX_TDX_UTIL_H tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +78
-0@@ -274,3 +274,100 @@ void vm_tdx_init_vm(struct kvm_vm *vm, uint64_t attributes)free (init_vm); } + +static void tdx_init_mem_region (struct kvm_vm *vm,void *source_pages, +uint64_t gpa,uint64_t size) +{ +uint32_t metadata =KVM_TDX_MEASURE_MEMORY_REGION ; +struct kvm_tdx_init_mem_region mem_region = { + .source_addr = (uint64_t )source_pages, + .gpa = gpa, + .nr_pages = size /PAGE_SIZE , + }; +struct kvm_vcpu *vcpu; + + vcpu = list_first_entry_or_null(&vm->vcpus, struct kvm_vcpu, list); + +TEST_ASSERT ((mem_region.nr_pages >0 ) && + ((mem_region.nr_pages *PAGE_SIZE ) == size), +"Cannot add partial pages to the guest memory.\n" ); +TEST_ASSERT (((uint64_t )source_pages & (PAGE_SIZE -1 )) ==0 , +"Source memory buffer is not page aligned\n" ); +vm_tdx_vcpu_ioctl (vcpu,KVM_TDX_INIT_MEM_REGION , metadata, &mem_region); +} + +static void tdx_init_pages (struct kvm_vm *vm,void *hva,uint64_t gpa, +uint64_t size) +{ +void *scratch_page =calloc (1 ,PAGE_SIZE ); +uint64_t nr_pages = size /PAGE_SIZE ; +int i; + +TEST_ASSERT (scratch_page, +"Could not allocate memory for loading memory region" ); + +for (i =0 ; i < nr_pages; i++) { +memcpy (scratch_page, hva,PAGE_SIZE ); + +tdx_init_mem_region (vm, scratch_page, gpa,PAGE_SIZE ); + + hva +=PAGE_SIZE ; + gpa +=PAGE_SIZE ; + } + +free (scratch_page); +} + +static void load_td_private_memory (struct kvm_vm *vm) +{ +struct userspace_mem_region *region; +int ctr; + +hash_for_each (vm->regions.slot_hash, ctr, region, slot_node) { +const struct sparsebit *protected_pages = region->protected_phy_pages; +const vm_paddr_t gpa_base = region->region.guest_phys_addr; +const uint64_t hva_base = region->region.userspace_addr; +const sparsebit_idx_t lowest_page_in_region = gpa_base >> vm->page_shift; + +sparsebit_idx_t i; +sparsebit_idx_t j; + +if (!sparsebit_any_set (protected_pages)) +continue ; + +sparsebit_for_each_set_range (protected_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; + +vm_set_memory_attributes (vm, gpa, size_to_load, +KVM_MEMORY_ATTRIBUTE_PRIVATE ); + +/* +* Here, memory is being loaded from hva to gpa. If the memory +* mapped to hva is also used to back gpa, then a copy has to be +* made just for loading, since KVM_TDX_INIT_MEM_REGION ioctl +* cannot encrypt memory in place. +* +* To determine if memory mapped to hva is also used to back Quoted 9 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c void vm_tdx_init_vm(struct kvm_vm *vm, uint64_t attributes) · line 361
Quoted 7 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +7
-0+* gpa, use a heuristic: +* +* If this memslot has guest_memfd, then this memslot should +* have memory backed from two sources: hva for shared memory +* and gpa will be backed by guest_memfd. +*/ +if (region->region.guest_memfd ==-1 )Why to pass !guest_memfd region to
tdx_init_mem_region()?Quoted 1 line
Not sure I understand your comment.
From the implementation of
tdx_init_pages(), it also invokestdx_init_mem_region(), which further invokes ioctlKVM_TDX_INIT_MEM_REGION.
Quoted 7 lines
However, if the region is with
guest_memfd== -1, the ioctlKVM_TDX_INIT_MEM_REGIONshould fail askvm_gmem_populate()won't succeed.So, I'm wondering why there's a need to for the case of "
region->region.guest_memfd== -1".Or anything I missed?
I had the same question in v8
https://lore.kernel.org/lkml/[email protected]/
I guess the code path for non-guest_memfd is due to some old versions of TDX KVM code before upstream. Currently, KVM doesn't support private memory from non-guest_memfd backed memory.
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +12
-1+tdx_init_pages (vm, (void *)hva, gpa, size_to_load); + else + tdx_init_mem_region(vm, (void *)hva, gpa, size_to_load); + } + } +} + +void vm_tdx_finalize (struct kvm_vm *vm) +{ +load_td_private_memory (vm); +vm_tdx_vm_ioctl (vm,KVM_TDX_FINALIZE_VM ,0 ,NULL ); +} --2.51.0.rc1.193.gad69d77794-goog
Re: [PATCH v9 14/19] KVM: selftests: Add helpers to init TDX memory and finalize VM
Quoted 138 lines · Binbin Wu
Quoted 135 lines · Yan Zhao
Quoted 127 lines · Sagi Shahar
Quoted 117 lines · Yan Zhao
Quoted 116 lines · Sagi Shahar
From: Ackerley Tng <[email protected]>
TDX protected memory needs to be measured and encrypted before it can be used by the guest. Traverse the VM's memory regions and initialize all the protected ranges by calling
KVM_TDX_INIT_MEM_REGION.Once all the memory is initialized, the VM can be finalized by calling
KVM_TDX_FINALIZE_VM.Signed-off-by Ackerley Tng <[email protected]>Co-developed-by Erdem Aktas <[email protected]>Signed-off-by Erdem Aktas <[email protected]>Co-developed-by Sagi Shahar <[email protected]>Signed-off-by Sagi Shahar <[email protected]>--- .../selftests/kvm/include/x86/tdx/tdx_util.h | 2 + .../selftests/kvm/lib/x86/tdx/tdx_util.c | 97 +++++++++++++++++++ 2 files changed, 99 insertions(+)tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h +2
-0@@ -71,4 +71,6 @@ void vm_tdx_load_common_boot_parameters(struct kvm_vm *vm);void vm_tdx_load_vcpu_boot_parameters (struct kvm_vm *vm,struct kvm_vcpu *vcpu);void vm_tdx_set_vcpu_entry_point (struct kvm_vcpu *vcpu,void *guest_code); +void vm_tdx_finalize (struct kvm_vm *vm); +#endif // SELFTESTS_TDX_TDX_UTIL_H tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +78
-0@@ -274,3 +274,100 @@ void vm_tdx_init_vm(struct kvm_vm *vm, uint64_t attributes)free (init_vm); } + +static void tdx_init_mem_region (struct kvm_vm *vm,void *source_pages, +uint64_t gpa,uint64_t size) +{ +uint32_t metadata =KVM_TDX_MEASURE_MEMORY_REGION ; +struct kvm_tdx_init_mem_region mem_region = { + .source_addr = (uint64_t )source_pages, + .gpa = gpa, + .nr_pages = size /PAGE_SIZE , + }; +struct kvm_vcpu *vcpu; + + vcpu = list_first_entry_or_null(&vm->vcpus, struct kvm_vcpu, list); + +TEST_ASSERT ((mem_region.nr_pages >0 ) && + ((mem_region.nr_pages *PAGE_SIZE ) == size), +"Cannot add partial pages to the guest memory.\n" ); +TEST_ASSERT (((uint64_t )source_pages & (PAGE_SIZE -1 )) ==0 , +"Source memory buffer is not page aligned\n" ); +vm_tdx_vcpu_ioctl (vcpu,KVM_TDX_INIT_MEM_REGION , metadata, &mem_region); +} + +static void tdx_init_pages (struct kvm_vm *vm,void *hva,uint64_t gpa, +uint64_t size) +{ +void *scratch_page =calloc (1 ,PAGE_SIZE ); +uint64_t nr_pages = size /PAGE_SIZE ; +int i; + +TEST_ASSERT (scratch_page, +"Could not allocate memory for loading memory region" ); + +for (i =0 ; i < nr_pages; i++) { +memcpy (scratch_page, hva,PAGE_SIZE ); + +tdx_init_mem_region (vm, scratch_page, gpa,PAGE_SIZE ); + + hva +=PAGE_SIZE ; + gpa +=PAGE_SIZE ; + } + +free (scratch_page); +} + +static void load_td_private_memory (struct kvm_vm *vm) +{ +struct userspace_mem_region *region; +int ctr; + +hash_for_each (vm->regions.slot_hash, ctr, region, slot_node) { +const struct sparsebit *protected_pages = region->protected_phy_pages; +const vm_paddr_t gpa_base = region->region.guest_phys_addr; +const uint64_t hva_base = region->region.userspace_addr; +const sparsebit_idx_t lowest_page_in_region = gpa_base >> vm->page_shift; + +sparsebit_idx_t i; +sparsebit_idx_t j; + +if (!sparsebit_any_set (protected_pages)) +continue ; + +sparsebit_for_each_set_range (protected_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; + +vm_set_memory_attributes (vm, gpa, size_to_load, +KVM_MEMORY_ATTRIBUTE_PRIVATE ); + +/* +* Here, memory is being loaded from hva to gpa. If the memory +* mapped to hva is also used to back gpa, then a copy has to be +* made just for loading, since KVM_TDX_INIT_MEM_REGION ioctl +* cannot encrypt memory in place. +* +* To determine if memory mapped to hva is also used to back Quoted 9 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c void vm_tdx_init_vm(struct kvm_vm *vm, uint64_t attributes) · line 361
Quoted 7 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +7
-0+* gpa, use a heuristic: +* +* If this memslot has guest_memfd, then this memslot should +* have memory backed from two sources: hva for shared memory +* and gpa will be backed by guest_memfd. +*/ +if (region->region.guest_memfd ==-1 )Why to pass !guest_memfd region to
tdx_init_mem_region()?Quoted 1 line
Not sure I understand your comment.
From the implementation of
tdx_init_pages(), it also invokestdx_init_mem_region(), which further invokes ioctlKVM_TDX_INIT_MEM_REGION.However, if the region is with
guest_memfd== -1, the ioctlKVM_TDX_INIT_MEM_REGIONshould fail askvm_gmem_populate()won't succeed.
Quoted 6 lines
Quoted 4 lines
So, I'm wondering why there's a need to for the case of "
region->region.guest_memfd== -1".Or anything I missed?
I had the same question in v8
https://lore.kernel.org/lkml/[email protected]/
I agree with you.
In patch "KVM: selftests: TDX: Test LOG_DIRTY_PAGES flag to a non-GUEST_MEMFD memslot", virt_map_shared() prevents the non-guest_memfd region from being searched by the load_td_private_memory().
Otherwise, the tdx_init_mem_region() would fail on this region.
Quoted 3 lines
I guess the code path for non-guest_memfd is due to some old versions of TDX KVM code before upstream. Currently, KVM doesn't support private memory from non-guest_memfd backed memory.
I guess so. Maybe just drop this case and assert?
TEST_ASSERT(region->region.guest_memfd != -1, "TDX private memory only supports guest_memfd backend\n");
Quoted 18 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +12
-1+tdx_init_pages (vm, (void *)hva, gpa, size_to_load); + else + tdx_init_mem_region(vm, (void *)hva, gpa, size_to_load); + } + } +} + +void vm_tdx_finalize (struct kvm_vm *vm) +{ +load_td_private_memory (vm); +vm_tdx_vm_ioctl (vm,KVM_TDX_FINALIZE_VM ,0 ,NULL ); +} --2.51.0.rc1.193.gad69d77794-goog
Re: [PATCH v9 14/19] KVM: selftests: Add helpers to init TDX memory and finalize VM
Quoted 142 lines · Yan Zhao
Quoted 141 lines · Binbin Wu
Quoted 138 lines · Yan Zhao
Quoted 127 lines · Sagi Shahar
Quoted 117 lines · Yan Zhao
Quoted 116 lines · Sagi Shahar
From: Ackerley Tng <[email protected]>
TDX protected memory needs to be measured and encrypted before it can be used by the guest. Traverse the VM's memory regions and initialize all the protected ranges by calling
KVM_TDX_INIT_MEM_REGION.Once all the memory is initialized, the VM can be finalized by calling
KVM_TDX_FINALIZE_VM.Signed-off-by Ackerley Tng <[email protected]>Co-developed-by Erdem Aktas <[email protected]>Signed-off-by Erdem Aktas <[email protected]>Co-developed-by Sagi Shahar <[email protected]>Signed-off-by Sagi Shahar <[email protected]>--- .../selftests/kvm/include/x86/tdx/tdx_util.h | 2 + .../selftests/kvm/lib/x86/tdx/tdx_util.c | 97 +++++++++++++++++++ 2 files changed, 99 insertions(+)tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h +2
-0@@ -71,4 +71,6 @@ void vm_tdx_load_common_boot_parameters(struct kvm_vm *vm);void vm_tdx_load_vcpu_boot_parameters (struct kvm_vm *vm,struct kvm_vcpu *vcpu);void vm_tdx_set_vcpu_entry_point (struct kvm_vcpu *vcpu,void *guest_code); +void vm_tdx_finalize (struct kvm_vm *vm); +#endif // SELFTESTS_TDX_TDX_UTIL_H tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +78
-0@@ -274,3 +274,100 @@ void vm_tdx_init_vm(struct kvm_vm *vm, uint64_t attributes)free (init_vm); } + +static void tdx_init_mem_region (struct kvm_vm *vm,void *source_pages, +uint64_t gpa,uint64_t size) +{ +uint32_t metadata =KVM_TDX_MEASURE_MEMORY_REGION ; +struct kvm_tdx_init_mem_region mem_region = { + .source_addr = (uint64_t )source_pages, + .gpa = gpa, + .nr_pages = size /PAGE_SIZE , + }; +struct kvm_vcpu *vcpu; + + vcpu = list_first_entry_or_null(&vm->vcpus, struct kvm_vcpu, list); + +TEST_ASSERT ((mem_region.nr_pages >0 ) && + ((mem_region.nr_pages *PAGE_SIZE ) == size), +"Cannot add partial pages to the guest memory.\n" ); +TEST_ASSERT (((uint64_t )source_pages & (PAGE_SIZE -1 )) ==0 , +"Source memory buffer is not page aligned\n" ); +vm_tdx_vcpu_ioctl (vcpu,KVM_TDX_INIT_MEM_REGION , metadata, &mem_region); +} + +static void tdx_init_pages (struct kvm_vm *vm,void *hva,uint64_t gpa, +uint64_t size) +{ +void *scratch_page =calloc (1 ,PAGE_SIZE ); +uint64_t nr_pages = size /PAGE_SIZE ; +int i; + +TEST_ASSERT (scratch_page, +"Could not allocate memory for loading memory region" ); + +for (i =0 ; i < nr_pages; i++) { +memcpy (scratch_page, hva,PAGE_SIZE ); + +tdx_init_mem_region (vm, scratch_page, gpa,PAGE_SIZE ); + + hva +=PAGE_SIZE ; + gpa +=PAGE_SIZE ; + } + +free (scratch_page); +} + +static void load_td_private_memory (struct kvm_vm *vm) +{ +struct userspace_mem_region *region; +int ctr; + +hash_for_each (vm->regions.slot_hash, ctr, region, slot_node) { +const struct sparsebit *protected_pages = region->protected_phy_pages; +const vm_paddr_t gpa_base = region->region.guest_phys_addr; +const uint64_t hva_base = region->region.userspace_addr; +const sparsebit_idx_t lowest_page_in_region = gpa_base >> vm->page_shift; + +sparsebit_idx_t i; +sparsebit_idx_t j; + +if (!sparsebit_any_set (protected_pages)) +continue ; + +sparsebit_for_each_set_range (protected_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; + +vm_set_memory_attributes (vm, gpa, size_to_load, +KVM_MEMORY_ATTRIBUTE_PRIVATE ); + +/* +* Here, memory is being loaded from hva to gpa. If the memory +* mapped to hva is also used to back gpa, then a copy has to be +* made just for loading, since KVM_TDX_INIT_MEM_REGION ioctl +* cannot encrypt memory in place. +* +* To determine if memory mapped to hva is also used to back Quoted 9 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c void vm_tdx_init_vm(struct kvm_vm *vm, uint64_t attributes) · line 361
Quoted 7 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +7
-0+* gpa, use a heuristic: +* +* If this memslot has guest_memfd, then this memslot should +* have memory backed from two sources: hva for shared memory +* and gpa will be backed by guest_memfd. +*/ +if (region->region.guest_memfd ==-1 )Why to pass !guest_memfd region to
tdx_init_mem_region()?Quoted 1 line
Not sure I understand your comment.
From the implementation of
tdx_init_pages(), it also invokestdx_init_mem_region(), which further invokes ioctlKVM_TDX_INIT_MEM_REGION.However, if the region is with
guest_memfd== -1, the ioctlKVM_TDX_INIT_MEM_REGIONshould fail askvm_gmem_populate()won't succeed.So, I'm wondering why there's a need to for the case of "
region->region.guest_memfd== -1".
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Or anything I missed?
I had the same question in v8
https://lore.kernel.org/lkml/[email protected]/I agree with you.
In patch "KVM: selftests: TDX: TestLOG_DIRTY_PAGESflag to a non-GUEST_MEMFD memslot",virt_map_shared()prevents the non-guest_memfd region from being searched by theload_td_private_memory().
Specifically, there's no __vm_phy_pages_alloc() being invoked with protected being true for the non-guest-memfd region in verify_log_dirty_pages_flag_on_non_gmemfd_slot().
Quoted 28 lines
Otherwise, the
tdx_init_mem_region()would fail on this region.Quoted 3 lines
I guess the code path for non-guest_memfd is due to some old versions of TDX KVM code before upstream. Currently, KVM doesn't support private memory from non-guest_memfd backed memory.
I guess so. Maybe just drop this case and assert?
TEST_ASSERT(region->region.guest_memfd != -1, "TDX private memory only supports guest_memfd backend\n");Quoted 18 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +12
-1+tdx_init_pages (vm, (void *)hva, gpa, size_to_load); + else + tdx_init_mem_region(vm, (void *)hva, gpa, size_to_load); + } + } +} + +void vm_tdx_finalize (struct kvm_vm *vm) +{ +load_td_private_memory (vm); +vm_tdx_vm_ioctl (vm,KVM_TDX_FINALIZE_VM ,0 ,NULL ); +} --2.51.0.rc1.193.gad69d77794-goog
PATCH v9 15/19 KVM: selftests: Hook TDX support to vm and vcpu creation
In reply to Sagi ShaharTDX require special handling for VM and VCPU initialization for various reasons:
- Special ioctlss for creating VM and VCPU.
- TDX registers are inaccessible to KVM.
- TDX require special boot code trampoline for loading parameters.
- TDX only supports KVM_CAP_SPLIT_IRQCHIP.
Hook this special handling into __vm_create() and vm_arch_vcpu_add() using the utility functions added in previous patches.
--- tools/testing/selftests/kvm/lib/kvm_util.c | 24 ++++++++- .../testing/selftests/kvm/lib/x86/processor.c | 49 ++++++++++++++----- 2 files changed, 61 insertions(+), 12 deletions(-)
tools/testing/selftests/kvm/lib/kvm_util.c +23 -1
@@ -4,6 +4,7 @@* * Copyright (C) 2018, Google LLC. */ +#include "tdx/tdx_util.h" #include "test_util.h" #include "kvm_util.h" #include "processor.h"
@@ -465,7 +466,7 @@ void kvm_set_files_rlimit(uint32_t nr_vcpus)static bool is_guest_memfd_required (struct vm_shape shape) {#ifdef __x86_64__ -return shape.type ==KVM_X86_SNP_VM ; +return (shape.type ==KVM_X86_SNP_VM || shape.type ==KVM_X86_TDX_VM );#else return false;#endif
@@ -499,6 +500,12 @@ struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus,for (i =0 ; i <NR_MEM_REGIONS ; i++) vm->memslots[i] =0 ; +if (is_tdx_vm (vm)) { +/* Setup additional mem regions for TDX. */ +vm_tdx_setup_boot_code_region (vm); +vm_tdx_setup_boot_parameters_region (vm, nr_runnable_vcpus); + } +kvm_vm_elf_load (vm, program_invocation_name);/*
@@ -1728,11 +1735,26 @@ void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa)return (void *) ((uintptr_t ) region->host_alias + offset); } +static bool is_split_irqchip_required (struct kvm_vm *vm) +{ +#ifdef __x86_64__ +return is_tdx_vm (vm); +#else +return false; +#endif +} +/* Create an interrupt controller chip for the specified VM. */ void vm_create_irqchip (struct kvm_vm *vm) {int r; +if (is_split_irqchip_required (vm)) { +vm_enable_cap (vm,KVM_CAP_SPLIT_IRQCHIP ,24 ); + vm->has_irqchip = true; +return ; + } +/* * Allocate a fully in-kernel IRQ chip by default, but fall back to a * split model (x86 only) if that fails (KVM x86 allows compiling out
tools/testing/selftests/kvm/lib/x86/processor.c +38 -11
75 lines not on this page · raw message
Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
Re: [PATCH v9 15/19] KVM: selftests: Hook TDX support to vm and vcpu creation
Quoted 1 line · Sagi Shahar
TDX require special handling for VM and VCPU initialization for various
s/require/requires
Quoted 2 lines
reasons:
- Special ioctlss for creating VM and VCPU.
s/ioctlss/ioctls
Quoted 2 lines
- TDX registers are inaccessible to KVM.
- TDX require special boot code trampoline for loading parameters.
s/require/requires
Quoted 11 lines
- TDX only supports
KVM_CAP_SPLIT_IRQCHIP.Hook this special handling into
__vm_create()andvm_arch_vcpu_add()using the utility functions added in previous patches.Signed-off-by Sagi Shahar <[email protected]>--- tools/testing/selftests/kvm/lib/kvm_util.c | 24 ++++++++- .../testing/selftests/kvm/lib/x86/processor.c | 49 ++++++++++++++----- 2 files changed, 61 insertions(+), 12 deletions(-)
Re: [PATCH v9 15/19] KVM: selftests: Hook TDX support to vm and vcpu creation
Quoted 11 lines · Chenyi Qiang
Quoted 1 line · Sagi Shahar
TDX require special handling for VM and VCPU initialization for various
s/require/requires
Quoted 2 lines
reasons:
- Special ioctlss for creating VM and VCPU.
Quoted 7 lines
s/ioctlss/ioctls
Quoted 2 lines
- TDX registers are inaccessible to KVM.
- TDX require special boot code trampoline for loading parameters.s/require/requires
ACK
Quoted 12 lines
Quoted 11 lines
- TDX only supports
KVM_CAP_SPLIT_IRQCHIP.Hook this special handling into
__vm_create()andvm_arch_vcpu_add()using the utility functions added in previous patches.Signed-off-by Sagi Shahar <[email protected]>--- tools/testing/selftests/kvm/lib/kvm_util.c | 24 ++++++++- .../testing/selftests/kvm/lib/x86/processor.c | 49 ++++++++++++++----- 2 files changed, 61 insertions(+), 12 deletions(-)
Sean Christopherson
maintainerRe: [PATCH v9 15/19] KVM: selftests: Hook TDX support to vm and vcpu creation
In reply to Sagi ShaharQuoted 6 lines · Sagi Shahar
TDX require special handling for VM and VCPU initialization for various reasons:
- Special ioctlss for creating VM and VCPU.
- TDX registers are inaccessible to KVM.
- TDX require special boot code trampoline for loading parameters.
- TDX only supportsKVM_CAP_SPLIT_IRQCHIP.
Please split this up and elaborate at least a little bit on why each flow needs special handling for TDX. Even for someone like me who is fairly familiar with TDX, there's too much "Trust me bro" and not enough explanation of why selftests really need all of these special paths for TDX.
At least four patches, one for each of your bullet points. Probably 5 or 6, as I think the CPUID handling warrants its own patch.
Quoted 56 lines
Hook this special handling into
__vm_create()andvm_arch_vcpu_add()using the utility functions added in previous patches.Signed-off-by Sagi Shahar <[email protected]>--- tools/testing/selftests/kvm/lib/kvm_util.c | 24 ++++++++- .../testing/selftests/kvm/lib/x86/processor.c | 49 ++++++++++++++----- 2 files changed, 61 insertions(+), 12 deletions(-)tools/testing/selftests/kvm/lib/kvm_util.c +17
-1@@ -4,6 +4,7 @@* * Copyright (C) 2018, Google LLC. */ +#include "tdx/tdx_util.h" #include "test_util.h" #include "kvm_util.h" #include "processor.h" @@ -465,7 +466,7 @@ void kvm_set_files_rlimit(uint32_t nr_vcpus)static bool is_guest_memfd_required (struct vm_shape shape) {#ifdef __x86_64__ -return shape.type ==KVM_X86_SNP_VM ; +return (shape.type ==KVM_X86_SNP_VM || shape.type ==KVM_X86_TDX_VM );#else return false;#endif @@ -499,6 +500,12 @@ struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus,for (i =0 ; i <NR_MEM_REGIONS ; i++) vm->memslots[i] =0 ; +if (is_tdx_vm (vm)) { +/* Setup additional mem regions for TDX. */ +vm_tdx_setup_boot_code_region (vm); +vm_tdx_setup_boot_parameters_region (vm, nr_runnable_vcpus); + } +kvm_vm_elf_load (vm, program_invocation_name);/* @@ -1728,11 +1735,26 @@ void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa)return (void *) ((uintptr_t ) region->host_alias + offset); } +static bool is_split_irqchip_required (struct kvm_vm *vm) +{ +#ifdef __x86_64__ +return is_tdx_vm (vm); +#else +return false; +#endif +} +
tools/testing/selftests/kvm/lib/kvm_util.c void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa) · line 1756
Quoted 10 lines
tools/testing/selftests/kvm/lib/kvm_util.c +5
-0/* Create an interrupt controller chip for the specified VM. */ void vm_create_irqchip (struct kvm_vm *vm) {int r; +if (is_split_irqchip_required (vm)) { +vm_enable_cap (vm,KVM_CAP_SPLIT_IRQCHIP ,24 ); + vm->has_irqchip = true; +return ; + }
Ugh. IMO, this is a KVM bug. Allowing KVM_CREATE_IRQCHIP for a TDX VM is simply wrong. It _can't_ work. Waiting until KVM_CREATE_VCPU to fail setup is terrible ABI.
If we stretch the meaning of ENOTTY a bit and return that when trying to create a fully in-kernel IRQCHIP for a TDX VM, then the selftests code Just Works thanks to the code below, which handles the scenario where KVM was be built without support for in-kernel I/O APIC (and PIC and PIT).
Re: [PATCH v9 15/19] KVM: selftests: Hook TDX support to vm and vcpu creation
Quoted 87 lines · Sean Christopherson
Quoted 6 lines · Sagi Shahar
TDX require special handling for VM and VCPU initialization for various reasons:
- Special ioctlss for creating VM and VCPU.
- TDX registers are inaccessible to KVM.
- TDX require special boot code trampoline for loading parameters.
- TDX only supportsKVM_CAP_SPLIT_IRQCHIP.Please split this up and elaborate at least a little bit on why each flow needs special handling for TDX. Even for someone like me who is fairly familiar with TDX, there's too much "Trust me bro" and not enough explanation of why selftests really need all of these special paths for TDX.
At least four patches, one for each of your bullet points. Probably 5 or 6, as I think the CPUID handling warrants its own patch.
Quoted 56 lines
Hook this special handling into
__vm_create()andvm_arch_vcpu_add()using the utility functions added in previous patches.Signed-off-by Sagi Shahar <[email protected]>--- tools/testing/selftests/kvm/lib/kvm_util.c | 24 ++++++++- .../testing/selftests/kvm/lib/x86/processor.c | 49 ++++++++++++++----- 2 files changed, 61 insertions(+), 12 deletions(-)tools/testing/selftests/kvm/lib/kvm_util.c +17
-1@@ -4,6 +4,7 @@* * Copyright (C) 2018, Google LLC. */ +#include "tdx/tdx_util.h" #include "test_util.h" #include "kvm_util.h" #include "processor.h" @@ -465,7 +466,7 @@ void kvm_set_files_rlimit(uint32_t nr_vcpus)static bool is_guest_memfd_required (struct vm_shape shape) {#ifdef __x86_64__ -return shape.type ==KVM_X86_SNP_VM ; +return (shape.type ==KVM_X86_SNP_VM || shape.type ==KVM_X86_TDX_VM );#else return false;#endif @@ -499,6 +500,12 @@ struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus,for (i =0 ; i <NR_MEM_REGIONS ; i++) vm->memslots[i] =0 ; +if (is_tdx_vm (vm)) { +/* Setup additional mem regions for TDX. */ +vm_tdx_setup_boot_code_region (vm); +vm_tdx_setup_boot_parameters_region (vm, nr_runnable_vcpus); + } +kvm_vm_elf_load (vm, program_invocation_name);/* @@ -1728,11 +1735,26 @@ void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa)return (void *) ((uintptr_t ) region->host_alias + offset); } +static bool is_split_irqchip_required (struct kvm_vm *vm) +{ +#ifdef __x86_64__ +return is_tdx_vm (vm); +#else +return false; +#endif +} +tools/testing/selftests/kvm/lib/kvm_util.c void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa) · line 1756
Quoted 10 lines
tools/testing/selftests/kvm/lib/kvm_util.c +5
-0/* Create an interrupt controller chip for the specified VM. */ void vm_create_irqchip (struct kvm_vm *vm) {int r; +if (is_split_irqchip_required (vm)) { +vm_enable_cap (vm,KVM_CAP_SPLIT_IRQCHIP ,24 ); + vm->has_irqchip = true; +return ; + }Ugh. IMO, this is a KVM bug. Allowing
KVM_CREATE_IRQCHIPfor a TDX VM is simply wrong. It _can't_ work. Waiting untilKVM_CREATE_VCPUto fail setup is terrible ABI.
Quoted 3 lines
If we stretch the meaning of ENOTTY a bit and return that when trying to create a fully in-kernel IRQCHIP for a TDX VM, then the selftests code Just Works thanks to the code below, which handles the scenario where KVM was be built without
^^^^^^^^^^
I'm not following. Was there supposed to be a patch attached?
Ira
Quoted 1 line
support for in-kernel I/O APIC (and PIC and PIT).
Re: [PATCH v9 15/19] KVM: selftests: Hook TDX support to vm and vcpu creation
Quoted 89 lines · Ira Weiny
Quoted 87 lines · Sean Christopherson
Quoted 6 lines · Sagi Shahar
TDX require special handling for VM and VCPU initialization for various reasons:
- Special ioctlss for creating VM and VCPU.
- TDX registers are inaccessible to KVM.
- TDX require special boot code trampoline for loading parameters.
- TDX only supportsKVM_CAP_SPLIT_IRQCHIP.Please split this up and elaborate at least a little bit on why each flow needs special handling for TDX. Even for someone like me who is fairly familiar with TDX, there's too much "Trust me bro" and not enough explanation of why selftests really need all of these special paths for TDX.
At least four patches, one for each of your bullet points. Probably 5 or 6, as I think the CPUID handling warrants its own patch.
Quoted 56 lines
Hook this special handling into
__vm_create()andvm_arch_vcpu_add()using the utility functions added in previous patches.Signed-off-by Sagi Shahar <[email protected]>--- tools/testing/selftests/kvm/lib/kvm_util.c | 24 ++++++++- .../testing/selftests/kvm/lib/x86/processor.c | 49 ++++++++++++++----- 2 files changed, 61 insertions(+), 12 deletions(-)tools/testing/selftests/kvm/lib/kvm_util.c +17
-1@@ -4,6 +4,7 @@* * Copyright (C) 2018, Google LLC. */ +#include "tdx/tdx_util.h" #include "test_util.h" #include "kvm_util.h" #include "processor.h" @@ -465,7 +466,7 @@ void kvm_set_files_rlimit(uint32_t nr_vcpus)static bool is_guest_memfd_required (struct vm_shape shape) {#ifdef __x86_64__ -return shape.type ==KVM_X86_SNP_VM ; +return (shape.type ==KVM_X86_SNP_VM || shape.type ==KVM_X86_TDX_VM );#else return false;#endif @@ -499,6 +500,12 @@ struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus,for (i =0 ; i <NR_MEM_REGIONS ; i++) vm->memslots[i] =0 ; +if (is_tdx_vm (vm)) { +/* Setup additional mem regions for TDX. */ +vm_tdx_setup_boot_code_region (vm); +vm_tdx_setup_boot_parameters_region (vm, nr_runnable_vcpus); + } +kvm_vm_elf_load (vm, program_invocation_name);/* @@ -1728,11 +1735,26 @@ void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa)return (void *) ((uintptr_t ) region->host_alias + offset); } +static bool is_split_irqchip_required (struct kvm_vm *vm) +{ +#ifdef __x86_64__ +return is_tdx_vm (vm); +#else +return false; +#endif +} +tools/testing/selftests/kvm/lib/kvm_util.c void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa) · line 1756
Quoted 10 lines
tools/testing/selftests/kvm/lib/kvm_util.c +5
-0/* Create an interrupt controller chip for the specified VM. */ void vm_create_irqchip (struct kvm_vm *vm) {int r; +if (is_split_irqchip_required (vm)) { +vm_enable_cap (vm,KVM_CAP_SPLIT_IRQCHIP ,24 ); + vm->has_irqchip = true; +return ; + }Ugh. IMO, this is a KVM bug. Allowing
KVM_CREATE_IRQCHIPfor a TDX VM is simply wrong. It _can't_ work. Waiting untilKVM_CREATE_VCPUto fail setup is terrible ABI.
Quoted 7 lines
Quoted 3 lines
If we stretch the meaning of ENOTTY a bit and return that when trying to create a fully in-kernel IRQCHIP for a TDX VM, then the selftests code Just Works thanks to the code below, which handles the scenario where KVM was be built without
^^^^^^^^^^I'm not following. Was there supposed to be a patch attached?
I think Sean refers to the original implementation which was out of the scope for the git diff so it was left out of the patch:
/*
* Allocate a fully in-kernel IRQ chip by default, but fall back to a
* split model (x86 only) if that fails (KVM x86 allows compiling out
* support for KVM_CREATE_IRQCHIP).
*/
r = __vm_ioctl(vm, KVM_CREATE_IRQCHIP, NULL);
if (r && errno == ENOTTY && kvm_has_cap(KVM_CAP_SPLIT_IRQCHIP))
vm_enable_cap(vm, KVM_CAP_SPLIT_IRQCHIP, 24);
else
TEST_ASSERT_VM_VCPU_IOCTL(!r, KVM_CREATE_IRQCHIP, r, vm);/*
* Allocate a fully in-kernel IRQ chip by default, but fall back to a
* split model (x86 only) if that fails (KVM x86 allows compiling out
* support for KVM_CREATE_IRQCHIP).
*/
r = __vm_ioctl(vm, KVM_CREATE_IRQCHIP, NULL);
if (r && errno == ENOTTY && kvm_has_cap(KVM_CAP_SPLIT_IRQCHIP)) vm_enable_cap(vm, KVM_CAP_SPLIT_IRQCHIP, 24);
elseTEST_ASSERT_VM_VCPU_IOCTL(!r, KVM_CREATE_IRQCHIP, r, vm); /*
* Allocate a fully in-kernel IRQ chip by default, but fall back to a
* split model (x86 only) if that fails (KVM x86 allows compiling out
* support for KVM_CREATE_IRQCHIP).
*/
r = __vm_ioctl(vm, KVM_CREATE_IRQCHIP, NULL);
if (r && errno == ENOTTY && kvm_has_cap(KVM_CAP_SPLIT_IRQCHIP)) vm_enable_cap(vm, KVM_CAP_SPLIT_IRQCHIP, 24);
elseTEST_ASSERT_VM_VCPU_IOCTL(!r, KVM_CREATE_IRQCHIP, r, vm);
Quoted 5 lines
Ira
Quoted 1 line
support for in-kernel I/O APIC (and PIC and PIT).
Re: [PATCH v9 15/19] KVM: selftests: Hook TDX support to vm and vcpu creation
Quoted 125 lines · Sagi Shahar
Quoted 89 lines · Ira Weiny
Quoted 87 lines · Sean Christopherson
Quoted 6 lines · Sagi Shahar
TDX require special handling for VM and VCPU initialization for various reasons:
- Special ioctlss for creating VM and VCPU.
- TDX registers are inaccessible to KVM.
- TDX require special boot code trampoline for loading parameters.
- TDX only supportsKVM_CAP_SPLIT_IRQCHIP.Please split this up and elaborate at least a little bit on why each flow needs special handling for TDX. Even for someone like me who is fairly familiar with TDX, there's too much "Trust me bro" and not enough explanation of why selftests really need all of these special paths for TDX.
At least four patches, one for each of your bullet points. Probably 5 or 6, as I think the CPUID handling warrants its own patch.
Quoted 56 lines
Hook this special handling into
__vm_create()andvm_arch_vcpu_add()using the utility functions added in previous patches.Signed-off-by Sagi Shahar <[email protected]>--- tools/testing/selftests/kvm/lib/kvm_util.c | 24 ++++++++- .../testing/selftests/kvm/lib/x86/processor.c | 49 ++++++++++++++----- 2 files changed, 61 insertions(+), 12 deletions(-)tools/testing/selftests/kvm/lib/kvm_util.c +17
-1@@ -4,6 +4,7 @@* * Copyright (C) 2018, Google LLC. */ +#include "tdx/tdx_util.h" #include "test_util.h" #include "kvm_util.h" #include "processor.h" @@ -465,7 +466,7 @@ void kvm_set_files_rlimit(uint32_t nr_vcpus)static bool is_guest_memfd_required (struct vm_shape shape) {#ifdef __x86_64__ -return shape.type ==KVM_X86_SNP_VM ; +return (shape.type ==KVM_X86_SNP_VM || shape.type ==KVM_X86_TDX_VM );#else return false;#endif @@ -499,6 +500,12 @@ struct kvm_vm *__vm_create(struct vm_shape shape, uint32_t nr_runnable_vcpus,for (i =0 ; i <NR_MEM_REGIONS ; i++) vm->memslots[i] =0 ; +if (is_tdx_vm (vm)) { +/* Setup additional mem regions for TDX. */ +vm_tdx_setup_boot_code_region (vm); +vm_tdx_setup_boot_parameters_region (vm, nr_runnable_vcpus); + } +kvm_vm_elf_load (vm, program_invocation_name);/* @@ -1728,11 +1735,26 @@ void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa)return (void *) ((uintptr_t ) region->host_alias + offset); } +static bool is_split_irqchip_required (struct kvm_vm *vm) +{ +#ifdef __x86_64__ +return is_tdx_vm (vm); +#else +return false; +#endif +} +tools/testing/selftests/kvm/lib/kvm_util.c void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa) · line 1756
Quoted 10 lines
tools/testing/selftests/kvm/lib/kvm_util.c +5
-0/* Create an interrupt controller chip for the specified VM. */ void vm_create_irqchip (struct kvm_vm *vm) {int r; +if (is_split_irqchip_required (vm)) { +vm_enable_cap (vm,KVM_CAP_SPLIT_IRQCHIP ,24 ); + vm->has_irqchip = true; +return ; + }Ugh. IMO, this is a KVM bug. Allowing
KVM_CREATE_IRQCHIPfor a TDX VM is simply wrong. It _can't_ work. Waiting untilKVM_CREATE_VCPUto fail setup is terrible ABI.Quoted 7 lines
Quoted 3 lines
If we stretch the meaning of ENOTTY a bit and return that when trying to create a fully in-kernel IRQCHIP for a TDX VM, then the selftests code Just Works thanks to the code below, which handles the scenario where KVM was be built without
^^^^^^^^^^I'm not following. Was there supposed to be a patch attached?
I think Sean refers to the original implementation which was out of the scope for the git diff so it was left out of the patch:
/*
* Allocate a fully in-kernel IRQ chip by default, but fall back to a
* split model (x86 only) if that fails (KVM x86 allows compiling out
* support forKVM_CREATE_IRQCHIP).
*/
r =__vm_ioctl(vm, KVM_CREATE_IRQCHIP, NULL);
if (r && errno == ENOTTY &&kvm_has_cap(KVM_CAP_SPLIT_IRQCHIP))vm_enable_cap(vm, KVM_CAP_SPLIT_IRQCHIP, 24); else TEST_ASSERT_VM_VCPU_IOCTL(!r, KVM_CREATE_IRQCHIP, r, vm);/*
* Allocate a fully in-kernel IRQ chip by default, but fall back to a
* split model (x86 only) if that fails (KVM x86 allows compiling out
* support forKVM_CREATE_IRQCHIP).
*/
r =__vm_ioctl(vm, KVM_CREATE_IRQCHIP, NULL);
if (r && errno == ENOTTY &&kvm_has_cap(KVM_CAP_SPLIT_IRQCHIP))vm_enable_cap(vm, KVM_CAP_SPLIT_IRQCHIP, 24);
elseTEST_ASSERT_VM_VCPU_IOCTL(!r, KVM_CREATE_IRQCHIP, r, vm); /*
* Allocate a fully in-kernel IRQ chip by default, but fall back to a
Quoted 9 lines
* split model (x86 only) if that fails (KVM x86 allows compiling out
* support forKVM_CREATE_IRQCHIP).
*/
r =__vm_ioctl(vm, KVM_CREATE_IRQCHIP, NULL);
if (r && errno == ENOTTY &&kvm_has_cap(KVM_CAP_SPLIT_IRQCHIP))vm_enable_cap(vm, KVM_CAP_SPLIT_IRQCHIP, 24);
elseTEST_ASSERT_VM_VCPU_IOCTL(!r, KVM_CREATE_IRQCHIP, r, vm);
Sorry, I messed up the paste somehow:
/*
* Allocate a fully in-kernel IRQ chip by default, but fall back to a
* split model (x86 only) if that fails (KVM x86 allows compiling out
* support for KVM_CREATE_IRQCHIP).
*/
r = __vm_ioctl(vm, KVM_CREATE_IRQCHIP, NULL);
if (r && errno == ENOTTY && kvm_has_cap(KVM_CAP_SPLIT_IRQCHIP))
vm_enable_cap(vm, KVM_CAP_SPLIT_IRQCHIP, 24);
else
TEST_ASSERT_VM_VCPU_IOCTL(!r, KVM_CREATE_IRQCHIP, r, vm);Quoted 5 lines
Quoted 5 lines
Ira
Quoted 1 line
support for in-kernel I/O APIC (and PIC and PIT).
Sean Christopherson
maintainerRe: [PATCH v9 15/19] KVM: selftests: Hook TDX support to vm and vcpu creation
Quoted 12 lines · Sagi Shahar
Quoted 11 lines · Sagi Shahar
Quoted 9 lines · Ira Weiny
Quoted 7 lines · Sean Christopherson
Ugh. IMO, this is a KVM bug. Allowing
KVM_CREATE_IRQCHIPfor a TDX VM is simply wrong. It _can't_ work. Waiting untilKVM_CREATE_VCPUto fail setup is terrible ABI.If we stretch the meaning of ENOTTY a bit and return that when trying to create a fully in-kernel IRQCHIP for a TDX VM, then the selftests code Just Works thanks to the code below, which handles the scenario where KVM was be built without
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
^^^^^^^^^^I'm not following. Was there supposed to be a patch attached?
I think Sean refers to the original implementation which was out of the scope for the git diff so it was left out of the patch:
Yep, exactly.
Quoted 10 lines
/*
* Allocate a fully in-kernel IRQ chip by default, but fall back to a
* split model (x86 only) if that fails (KVM x86 allows compiling out
* support forKVM_CREATE_IRQCHIP).
*/
r =__vm_ioctl(vm, KVM_CREATE_IRQCHIP, NULL);
if (r && errno == ENOTTY &&kvm_has_cap(KVM_CAP_SPLIT_IRQCHIP))vm_enable_cap(vm, KVM_CAP_SPLIT_IRQCHIP, 24); else TEST_ASSERT_VM_VCPU_IOCTL(!r, KVM_CREATE_IRQCHIP, r, vm);
Re: [PATCH v9 15/19] KVM: selftests: Hook TDX support to vm and vcpu creation
Quoted 16 lines · Sean Christopherson
Quoted 14 lines · Sagi Shahar
Quoted 13 lines · Sagi Shahar
Quoted 11 lines · Ira Weiny
Quoted 7 lines · Sean Christopherson
Ugh. IMO, this is a KVM bug. Allowing
KVM_CREATE_IRQCHIPfor a TDX VM is simply wrong. It _can't_ work. Waiting untilKVM_CREATE_VCPUto fail setup is terrible ABI.If we stretch the meaning of ENOTTY a bit and return that when trying to create a fully in-kernel IRQCHIP for a TDX VM, then the selftests code Just Works thanks to the code below, which handles the scenario where KVM was be built without
^^^^^^^^^^
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
I'm not following. Was there supposed to be a patch attached?
I think Sean refers to the original implementation which was out of the scope for the git diff so it was left out of the patch:
Yep, exactly.
I took a stab at updating the KVM ABI and sent out a small patch [1] to fail KVM_CREATE_IRQCHIP with ENOTTY and the test passes without the special handling for SPLIT_IRQCHIP for TDX.
[1] https://lore.kernel.org/lkml/[email protected]/
Quoted 10 lines
Quoted 10 lines
/*
* Allocate a fully in-kernel IRQ chip by default, but fall back to a
* split model (x86 only) if that fails (KVM x86 allows compiling out
* support forKVM_CREATE_IRQCHIP).
*/
r =__vm_ioctl(vm, KVM_CREATE_IRQCHIP, NULL);
if (r && errno == ENOTTY &&kvm_has_cap(KVM_CAP_SPLIT_IRQCHIP))vm_enable_cap(vm, KVM_CAP_SPLIT_IRQCHIP, 24); else TEST_ASSERT_VM_VCPU_IOCTL(!r, KVM_CREATE_IRQCHIP, r, vm);
PATCH v9 16/19 KVM: selftests: Add support for TDX TDCALL from guest
In reply to Sagi ShaharFrom: Erdem Aktas <[email protected]>
Add support for TDX guests to issue TDCALLs to the TDX module.
--- tools/testing/selftests/kvm/Makefile.kvm | 8 ++ .../selftests/kvm/include/x86/tdx/tdcall.h | 34 +++++++ .../selftests/kvm/lib/x86/tdx/tdcall.S | 93 +++++++++++++++++++ .../kvm/lib/x86/tdx/tdcall_offsets.c | 16 ++++ 4 files changed, 151 insertions(+) create mode 100644 tools/testing/selftests/kvm/include/x86/tdx/tdcall.h create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/tdcall.S create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/tdcall_offsets.c
tools/testing/selftests/kvm/Makefile.kvm +8 -0
@@ -20,6 +20,7 @@ LIBKVM += lib/userfaultfd_util.c LIBKVM_STRING += lib/string_override.c LIBKVM_ASM_DEFS += lib/x86/tdx/td_boot_offsets.c +LIBKVM_ASM_DEFS += lib/x86/tdx/tdcall_offsets.c LIBKVM_x86 += lib/x86/apic.c LIBKVM_x86 += lib/x86/handlers.S
@@ -33,6 +34,7 @@ LIBKVM_x86 += lib/x86/ucall.c LIBKVM_x86 += lib/x86/vmx.c LIBKVM_x86 += lib/x86/tdx/tdx_util.c LIBKVM_x86 += lib/x86/tdx/td_boot.S +LIBKVM_x86 += lib/x86/tdx/tdcall.S LIBKVM_arm64 += lib/arm64/gic.c LIBKVM_arm64 += lib/arm64/gic_v3.c
@@ -342,7 +344,13 @@ $(OUTPUT)/lib/x86/tdx/td_boot.o: $(OUTPUT)/include/x86/tdx/td_boot_offsets.h $(OUTPUT)/include/x86/tdx/td_boot_offsets.h: $(OUTPUT)/lib/x86/tdx/td_boot_offsets.s FORCE $(call filechk,offsets,__TDX_BOOT_OFFSETS_H__) +$(OUTPUT)/lib/x86/tdx/tdcall.o: $(OUTPUT)/include/x86/tdx/tdcall_offsets.h + +$(OUTPUT)/include/x86/tdx/tdcall_offsets.h: $(OUTPUT)/lib/x86/tdx/tdcall_offsets.s FORCE + $(call filechk,offsets,__TDCALL__OFFSETS_H__) + EXTRA_CLEAN += $(OUTPUT)/include/x86/tdx/td_boot_offsets.h +EXTRA_CLEAN += $(OUTPUT)/include/x86/tdx/tdcall_offsets.h $(shell mkdir -p $(sort $(dir $(TEST_GEN_PROGS)))) $(SPLIT_TEST_GEN_OBJ): $(GEN_HDRS)
tools/testing/selftests/kvm/include/x86/tdx/tdcall.h new file +34 -0
@@ -0,0 +1,34 @@ +/* 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> + +#define TDX_TDCALL_HAS_OUTPUT BIT(0) + +#ifndef __ASSEMBLY__ + +#include <linux/types.h> + +/* +* Used in __tdx_tdcall() 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_tdcall_args { +u64 r10; +u64 r11; +u64 r12; +u64 r13; +u64 r14; +u64 r15; +}; + +/* Used to request services from the VMM */ +u64 __tdx_tdcall (struct tdx_tdcall_args *args,unsigned long flags); + +#endif // __ASSEMBLY__ +#endif // SELFTESTS_TDX_TDCALL_H
tools/testing/selftests/kvm/lib/x86/tdx/tdcall.S new file +93 -0
93 lines not on this page · raw message
tools/testing/selftests/kvm/lib/x86/tdx/tdcall_offsets.c new file +16 -0
@@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0 +#define COMPILE_OFFSETS + +#include <linux/kbuild.h> + +#include "tdx/tdcall.h" + +static void __attribute__((used))common (void ) +{ +OFFSET (TDX_TDCALL_R10 , tdx_tdcall_args, r10); +OFFSET (TDX_TDCALL_R11 , tdx_tdcall_args, r11); +OFFSET (TDX_TDCALL_R12 , tdx_tdcall_args, r12); +OFFSET (TDX_TDCALL_R13 , tdx_tdcall_args, r13); +OFFSET (TDX_TDCALL_R14 , tdx_tdcall_args, r14); +OFFSET (TDX_TDCALL_R15 , tdx_tdcall_args, r15); +}
Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
PATCH v9 17/19 KVM: selftests: Add wrapper for TDX MMIO from guest
In reply to Sagi ShaharAdd utility function to issue MMIO TDCALL from TDX guests.
--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/include/x86/tdx/tdx.h | 14 ++++++++++++ tools/testing/selftests/kvm/lib/x86/tdx/tdx.c | 22 +++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 tools/testing/selftests/kvm/include/x86/tdx/tdx.h create mode 100644 tools/testing/selftests/kvm/lib/x86/tdx/tdx.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -35,6 +35,7 @@ LIBKVM_x86 += lib/x86/vmx.c LIBKVM_x86 += lib/x86/tdx/tdx_util.c LIBKVM_x86 += lib/x86/tdx/td_boot.S LIBKVM_x86 += lib/x86/tdx/tdcall.S +LIBKVM_x86 += lib/x86/tdx/tdx.c LIBKVM_arm64 += lib/arm64/gic.c LIBKVM_arm64 += lib/arm64/gic_v3.c
tools/testing/selftests/kvm/include/x86/tdx/tdx.h new file +14 -0
@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SELFTESTS_TDX_TDX_H +#define SELFTESTS_TDX_TDX_H + +#include <stdint.h> + +/* MMIO direction */ +#define MMIO_READ 0 +#define MMIO_WRITE 1 + +uint64_t tdg_vp_vmcall_ve_request_mmio_write (uint64_t address,uint64_t size, +uint64_t data_in); + +#endif // SELFTESTS_TDX_TDX_H
tools/testing/selftests/kvm/lib/x86/tdx/tdx.c new file +22 -0
@@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include "tdx/tdcall.h" +#include "tdx/tdx.h" + +#define TDG_VP_VMCALL 0 + +#define TDG_VP_VMCALL_VE_REQUEST_MMIO 48 + +uint64_t tdg_vp_vmcall_ve_request_mmio_write (uint64_t address,uint64_t size, +uint64_t data_in) +{ +struct tdx_tdcall_args args = { + .r11 =TDG_VP_VMCALL_VE_REQUEST_MMIO , + .r12 = size, + .r13 =MMIO_WRITE , + .r14 = address, + .r15 = data_in, + }; + +return __tdx_tdcall (&args,0 ); +}
Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
PATCH v9 18/19 KVM: selftests: Add ucall support for TDX
In reply to Sagi ShaharFrom: Ackerley Tng <[email protected]>
ucalls for non-Coco VMs work by having the guest write to the rdi register, then perform an io instruction to exit to the host. The host then reads rdi using kvm_get_regs().
CPU registers can't be read using kvm_get_regs() for TDX, so TDX guests use MMIO to pass the struct ucall's hva to the host. MMIO was chosen because it is one of the simplest (hence unlikely to fail) mechanisms that support passing 8 bytes from guest to host.
--- .../testing/selftests/kvm/include/x86/ucall.h | 4 +- tools/testing/selftests/kvm/lib/x86/ucall.c | 45 ++++++++++++++++--- 2 files changed, 41 insertions(+), 8 deletions(-)
tools/testing/selftests/kvm/include/x86/ucall.h +1 -3
@@ -6,8 +6,6 @@#define UCALL_EXIT_REASON KVM_EXIT_IO -static inline void ucall_arch_init (struct kvm_vm *vm,vm_paddr_t mmio_gpa) -{ -} +void ucall_arch_init (struct kvm_vm *vm,vm_paddr_t mmio_gpa);#endif
tools/testing/selftests/kvm/lib/x86/ucall.c +40 -5
62 lines not on this page · raw message
Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog
Re: [PATCH v9 18/19] KVM: selftests: Add ucall support for TDX
Quoted 78 lines · Sagi Shahar
From: Ackerley Tng <[email protected]>
ucalls for non-Coco VMs work by having the guest write to the rdi register, then perform an io instruction to exit to the host. The host then reads rdi using
kvm_get_regs().CPU registers can't be read using
kvm_get_regs()for TDX, so TDX guests use MMIO to pass the struct ucall's hva to the host. MMIO was chosen because it is one of the simplest (hence unlikely to fail) mechanisms that support passing 8 bytes from guest to host.Signed-off-by Ackerley Tng <[email protected]>Co-developed-by Sagi Shahar <[email protected]>Signed-off-by Sagi Shahar <[email protected]>--- .../testing/selftests/kvm/include/x86/ucall.h | 4 +- tools/testing/selftests/kvm/lib/x86/ucall.c | 45 ++++++++++++++++--- 2 files changed, 41 insertions(+), 8 deletions(-)tools/testing/selftests/kvm/include/x86/ucall.h +1
-3@@ -6,8 +6,6 @@#define UCALL_EXIT_REASON KVM_EXIT_IO -static inline void ucall_arch_init (struct kvm_vm *vm,vm_paddr_t mmio_gpa) -{ -} +void ucall_arch_init (struct kvm_vm *vm,vm_paddr_t mmio_gpa);#endif tools/testing/selftests/kvm/lib/x86/ucall.c +23
-2@@ -5,11 +5,34 @@* Copyright (C) 2018, Red Hat, Inc. */ #include "kvm_util.h" +#include "tdx/tdx.h" #define UCALL_PIO_PORT ((uint16_t)0x1000) +static uint8_t vm_type; +static vm_paddr_t host_ucall_mmio_gpa; +static vm_paddr_t ucall_mmio_gpa; + +void ucall_arch_init (struct kvm_vm *vm,vm_paddr_t mmio_gpa) +{ + vm_type = vm->type; +sync_global_to_guest (vm, vm_type); + + host_ucall_mmio_gpa = ucall_mmio_gpa = mmio_gpa; + +if (vm_type ==KVM_X86_TDX_VM ) + ucall_mmio_gpa |= vm->arch.s_bit; + +sync_global_to_guest (vm, ucall_mmio_gpa); +} +void ucall_arch_do_ucall (vm_vaddr_t uc) { +if (vm_type ==KVM_X86_TDX_VM ) { +tdg_vp_vmcall_ve_request_mmio_write (ucall_mmio_gpa,8 , uc); +return ; + } +/* * FIXME: Revert this hack (the entire commit that added it) once nVMX * preserves L2 GPRs across a nested VM-Exit. If a ucall from L2, e.g. @@ -46,11 +69,23 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) {struct kvm_run *run = vcpu->run; -if (run->exit_reason ==KVM_EXIT_IO && run->io.port ==UCALL_PIO_PORT ) { -struct kvm_regs regs;
tools/testing/selftests/kvm/lib/x86/ucall.c void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) · line 81
Quoted 10 lines
tools/testing/selftests/kvm/lib/x86/ucall.c +10
-0+switch (vm_type) { +case KVM_X86_TDX_VM : +if (vcpu->run->exit_reason ==KVM_EXIT_MMIO && + vcpu->run->mmio.phys_addr == host_ucall_mmio_gpa && + vcpu->run->mmio.len ==8 && vcpu->run->mmio.is_write) { +uint64_t data = *(uint64_t *)vcpu->run->mmio.data; + +return (void *)data; + } +return NULL ;
My first thought was how did SEV_ES or SNP work for this since they are not able to get RDI neither.
Then I had a check in sev_smoke_test.c, both guest_sev_es_code() and guest_snp_code() call GUEST_ASSERT(), which finally calls ucall_assert(), but in test_sev(), the code doesn't handle ucall for SEV_ES or SNP. Does it mean GUEST_ASSERT() is currently not working and ignored for SEV_ES and SNP? Or did I miss anything?
Quoted 13 lines
tools/testing/selftests/kvm/lib/x86/ucall.c +7
-3+default : +if (run->exit_reason ==KVM_EXIT_IO && run->io.port ==UCALL_PIO_PORT ) { +struct kvm_regs regs; -vcpu_regs_get (vcpu, ®s); -return (void *)regs.rdi; +vcpu_regs_get (vcpu, ®s); +return (void *)regs.rdi; + } +return NULL ; } -return NULL ; }
Sean Christopherson
maintainerRe: [PATCH v9 18/19] KVM: selftests: Add ucall support for TDX
Quoted 17 lines · Binbin Wu
Quoted 5 lines · Sagi Shahar
tools/testing/selftests/kvm/lib/x86/ucall.c +0
-2@@ -46,11 +69,23 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) {struct kvm_run *run = vcpu->run; -if (run->exit_reason ==KVM_EXIT_IO && run->io.port ==UCALL_PIO_PORT ) { -struct kvm_regs regs;tools/testing/selftests/kvm/lib/x86/ucall.c void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) · line 80
Quoted 10 lines
tools/testing/selftests/kvm/lib/x86/ucall.c +10
-0+switch (vm_type) { +case KVM_X86_TDX_VM : +if (vcpu->run->exit_reason ==KVM_EXIT_MMIO && + vcpu->run->mmio.phys_addr == host_ucall_mmio_gpa && + vcpu->run->mmio.len ==8 && vcpu->run->mmio.is_write) { +uint64_t data = *(uint64_t *)vcpu->run->mmio.data; + +return (void *)data; + } +return NULL ;
Quoted 7 lines
My first thought was how did
SEV_ESor SNP work for this since they are not able to get RDI neither.
Then I had a check insev_smoke_test.c, bothguest_sev_es_code()andguest_snp_code()callGUEST_ASSERT(), which finally callsucall_assert(), but intest_sev(), the code doesn't handle ucall forSEV_ESor SNP. Does it meanGUEST_ASSERT()is currently not working and ignored forSEV_ESand SNP? Or did I miss anything?
GUEST_ASSERT() "works" for -ES and -SNP in the sense that it generates as test failure due to the #VC not being handled (leads to SHUTDOWN). But you're correct that ucall isn't functional yet. x86/sev_smoke_test.c fudges around lack of ucall by using the GHCB MSR protocol to signal "done".
/*
* TODO: Add GHCB and ucall support for SEV-ES guests. For now, simply
* force "termination" to signal "done" via the GHCB MSR protocol.
*/
wrmsr(MSR_AMD64_SEV_ES_GHCB, GHCB_MSR_TERM_REQ);
vmgexit();Re: [PATCH v9 18/19] KVM: selftests: Add ucall support for TDX
Quoted 29 lines · Sean Christopherson
Quoted 15 lines · Binbin Wu
Quoted 14 lines · Sagi Shahar
tools/testing/selftests/kvm/lib/x86/ucall.c +9
-2@@ -46,11 +69,23 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) {struct kvm_run *run = vcpu->run; -if (run->exit_reason ==KVM_EXIT_IO && run->io.port ==UCALL_PIO_PORT ) { -struct kvm_regs regs; +switch (vm_type) { +case KVM_X86_TDX_VM : +if (vcpu->run->exit_reason ==KVM_EXIT_MMIO && + vcpu->run->mmio.phys_addr == host_ucall_mmio_gpa && + vcpu->run->mmio.len ==8 && vcpu->run->mmio.is_write) { +uint64_t data = *(uint64_t *)vcpu->run->mmio.data; + +return (void *)data; + }Quoted 8 lines
Review void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) · line 80
Quoted 1 line
(quoted diff) +1
-0+ return NULL;My first thought was how did
SEV_ESor SNP work for this since they are not able to get RDI neither.
Then I had a check insev_smoke_test.c, bothguest_sev_es_code()andguest_snp_code()callGUEST_ASSERT(), which finally callsucall_assert(), but intest_sev(), the code doesn't handle ucall forSEV_ESor SNP. Does it meanGUEST_ASSERT()is currently not working and ignored forSEV_ESand SNP? Or did I miss anything?
GUEST_ASSERT()"works" for -ES and -SNP in the sense that it generates as test failure due to the #VC not being handled (leads to SHUTDOWN). But you're correct that ucall isn't functional yet.x86/sev_smoke_test.cfudges around lack of ucall by using the GHCB MSR protocol to signal "done".
Quoted 7 lines
/*
* TODO: Add GHCB and ucall support for SEV-ES guests. For now, simply
* force "termination" to signal "done" via the GHCB MSR protocol.
*/
wrmsr(MSR_AMD64_SEV_ES_GHCB, GHCB_MSR_TERM_REQ);
vmgexit();OK, thanks for the explanation!
PATCH v9 19/19 KVM: selftests: Add TDX lifecycle test
In reply to Sagi ShaharAdding a test to verify TDX lifecycle by creating a simple TD.
--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/include/x86/tdx/tdx_util.h | 10 ++++++ .../selftests/kvm/lib/x86/tdx/tdx_util.c | 18 +++++++++++ tools/testing/selftests/kvm/x86/tdx_vm_test.c | 31 +++++++++++++++++++ 4 files changed, 60 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86/tdx_vm_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -155,6 +155,7 @@ TEST_GEN_PROGS_x86 += rseq_test TEST_GEN_PROGS_x86 += steal_time TEST_GEN_PROGS_x86 += system_counter_offset_test TEST_GEN_PROGS_x86 += pre_fault_memory_test +TEST_GEN_PROGS_x86 += x86/tdx_vm_test # Compiled outputs used by test targets TEST_GEN_PROGS_EXTENDED_x86 += x86/nx_huge_pages_test
tools/testing/selftests/kvm/include/x86/tdx/tdx_util.h +10 -0
@@ -11,6 +11,14 @@ static inline bool is_tdx_vm(struct kvm_vm *vm)return vm->type ==KVM_X86_TDX_VM ; } +/* +* Verify that TDX is supported by KVM. +*/ +static inline bool is_tdx_enabled (void ) +{ +return !!(kvm_check_cap (KVM_CAP_VM_TYPES ) &BIT (KVM_X86_TDX_VM )); +} +/* * TDX ioctls */
@@ -72,5 +80,7 @@ void vm_tdx_load_vcpu_boot_parameters(struct kvm_vm *vm, struct kvm_vcpu *vcpu);void vm_tdx_set_vcpu_entry_point (struct kvm_vcpu *vcpu,void *guest_code);void vm_tdx_finalize (struct kvm_vm *vm); +struct kvm_vm *vm_tdx_create_with_one_vcpu (void *guest_code, +struct kvm_vcpu **vcpu);#endif // SELFTESTS_TDX_TDX_UTIL_H
tools/testing/selftests/kvm/lib/x86/tdx/tdx_util.c +18 -0
@@ -371,3 +371,21 @@ void vm_tdx_finalize(struct kvm_vm *vm)load_td_private_memory (vm);vm_tdx_vm_ioctl (vm,KVM_TDX_FINALIZE_VM ,0 ,NULL ); } + +struct kvm_vm *vm_tdx_create_with_one_vcpu (void *guest_code, +struct kvm_vcpu **vcpu) +{ +struct vm_shape shape = { + .mode =VM_MODE_DEFAULT , + .type =KVM_X86_TDX_VM , + }; +struct kvm_vm *vm; +struct kvm_vcpu *vcpus[1 ]; + + vm =__vm_create_with_vcpus (shape,1 ,0 , guest_code, vcpus); + *vcpu = vcpus[0 ]; + +vm_tdx_finalize (vm); + +return vm; +}
tools/testing/selftests/kvm/x86/tdx_vm_test.c new file +31 -0
@@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include "kvm_util.h" +#include "tdx/tdx_util.h" +#include "ucall_common.h" +#include "kselftest_harness.h" + +static void guest_code_lifecycle (void ) +{ +GUEST_DONE (); +} + +TEST (verify_td_lifecycle) +{ +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +struct ucall uc; + + vm =vm_tdx_create_with_one_vcpu (guest_code_lifecycle, &vcpu); + +vcpu_run (vcpu); +TEST_ASSERT_EQ (get_ucall (vcpu, &uc),UCALL_DONE ); + +kvm_vm_free (vm); +} + +int main (int argc,char **argv) +{ +TEST_REQUIRE (is_tdx_enabled ()); +return test_harness_run (argc, argv); +}
Signature · 1 line
2.51.0.rc1.193.gad69d77794-goog