KVM: selftests: Implement ucall "pool" (for SEV)
Sean Christopherson
reviewerv4 cover KVM: selftests: Implement ucall "pool" (for SEV)
This is "v4" of Peter's SEV series, minus the actual SEV tests. My plan is to get this queued sooner than later so that Peter can build on top.
Non-KVM folks, y'all got pulled in because of the atomic_test_and_set_bit() patch.
Rework the ucall infrastructure to use a pool of ucall structs to pass memory instead of using the guest's stack. For confidential VMs with encrypted memory, e.g. SEV, the guest's stack "needs" to be private memory and so can't be used to communicate with the host.
Convert all implementations to the pool as all of the complexity is hidden in common code, and supporting multiple interfaces adds its own kind of complexity.
Tested on x86 and ARM, compile tested on s390 and RISC-V.
Peter Gonda (2):
tools: Add atomic_test_and_set_bit()
KVM: selftests: Add ucall pool based implementation
Sean Christopherson (4):
KVM: selftests: Consolidate common code for populating ucall struct
KVM: selftests: Consolidate boilerplate code in get_ucall()
KVM: selftests: Automatically do init_ucall() for non-barebones VMs
KVM: selftests: Make arm64's MMIO ucall multi-VM friendly
tools/arch/x86/include/asm/atomic.h | 7 ++ tools/include/asm-generic/atomic-gcc.h | 12 ++ tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/aarch64/arch_timer.c | 1 - .../selftests/kvm/aarch64/debug-exceptions.c | 1 - .../selftests/kvm/aarch64/hypercalls.c | 1 - .../testing/selftests/kvm/aarch64/psci_test.c | 1 - .../testing/selftests/kvm/aarch64/vgic_init.c | 2 - .../testing/selftests/kvm/aarch64/vgic_irq.c | 1 - tools/testing/selftests/kvm/dirty_log_test.c | 2 - .../selftests/kvm/include/kvm_util_base.h | 16 +++ .../selftests/kvm/include/ucall_common.h | 13 +- .../selftests/kvm/kvm_page_table_test.c | 1 - .../testing/selftests/kvm/lib/aarch64/ucall.c | 101 +++------------- tools/testing/selftests/kvm/lib/kvm_util.c | 11 ++ .../selftests/kvm/lib/perf_test_util.c | 2 - tools/testing/selftests/kvm/lib/riscv/ucall.c | 40 ++---- tools/testing/selftests/kvm/lib/s390x/ucall.c | 37 ++---- .../testing/selftests/kvm/lib/ucall_common.c | 114 ++++++++++++++++++ .../testing/selftests/kvm/lib/x86_64/ucall.c | 37 ++---- .../testing/selftests/kvm/memslot_perf_test.c | 1 - tools/testing/selftests/kvm/rseq_test.c | 1 - tools/testing/selftests/kvm/steal_time.c | 1 - .../kvm/system_counter_offset_test.c | 1 - 24 files changed, 213 insertions(+), 192 deletions(-) create mode 100644 tools/testing/selftests/kvm/lib/ucall_common.c
base-commit: 372d07084593dc7a399bf9bee815711b1fb1bcf2
Signature · 1 line
2.37.1.595.g718a3a8f04-goog
Sean Christopherson
reviewerPATCH v4 1/6 KVM: selftests: Consolidate common code for populating ucall struct
Make ucall() a common helper that populates struct ucall, and only calls into arch code to make the actually call out to userspace.
Rename all arch-specific helpers to make it clear they're arch-specific, and to avoid collisions with common helpers (one more on its way...)
Add WRITE_ONCE() to stores in ucall() code (as already done to aarch64 code in commit 9e2f6498efbb ("selftests: KVM: Handle compiler optimizations in ucall")) to prevent clang optimizations breaking ucalls.
--- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/include/ucall_common.h | 23 ++++++++++++++++--- .../testing/selftests/kvm/lib/aarch64/ucall.c | 22 ++++-------------- tools/testing/selftests/kvm/lib/riscv/ucall.c | 23 ++++--------------- tools/testing/selftests/kvm/lib/s390x/ucall.c | 23 ++++--------------- .../testing/selftests/kvm/lib/ucall_common.c | 20 ++++++++++++++++ .../testing/selftests/kvm/lib/x86_64/ucall.c | 23 ++++--------------- 7 files changed, 61 insertions(+), 74 deletions(-) create mode 100644 tools/testing/selftests/kvm/lib/ucall_common.c
tools/testing/selftests/kvm/Makefile +1 -0
@@ -47,6 +47,7 @@ LIBKVM += lib/perf_test_util.c LIBKVM += lib/rbtree.c LIBKVM += lib/sparsebit.c LIBKVM += lib/test_util.c +LIBKVM += lib/ucall_common.c LIBKVM_x86_64 += lib/x86_64/apic.c LIBKVM_x86_64 += lib/x86_64/handlers.S
tools/testing/selftests/kvm/include/ucall_common.h +20 -3
@@ -24,10 +24,27 @@ struct ucall {uint64_t args[UCALL_MAX_ARGS ]; }; -void ucall_init (struct kvm_vm *vm,void *arg); -void ucall_uninit (struct kvm_vm *vm); +void ucall_arch_init (struct kvm_vm *vm,void *arg); +void ucall_arch_uninit (struct kvm_vm *vm); +void ucall_arch_do_ucall (vm_vaddr_t uc); +uint64_t ucall_arch_get_ucall (struct kvm_vcpu *vcpu,struct ucall *uc); +void ucall (uint64_t cmd,int nargs, ...); -uint64_t get_ucall (struct kvm_vcpu *vcpu,struct ucall *uc); + +static inline void ucall_init (struct kvm_vm *vm,void *arg) +{ +ucall_arch_init (vm, arg); +} + +static inline void ucall_uninit (struct kvm_vm *vm) +{ +ucall_arch_uninit (vm); +} + +static inline uint64_t get_ucall (struct kvm_vcpu *vcpu,struct ucall *uc) +{ +return ucall_arch_get_ucall (vcpu, uc); +}#define GUEST_SYNC_ARGS (stage, arg1, arg2, arg3, arg4) \ ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4)
tools/testing/selftests/kvm/lib/aarch64/ucall.c +5 -17
@@ -21,7 +21,7 @@ static bool ucall_mmio_init(struct kvm_vm *vm, vm_paddr_t gpa)return true; } -void ucall_init (struct kvm_vm *vm,void *arg) +void ucall_arch_init (struct kvm_vm *vm,void *arg) {vm_paddr_t gpa, start, end, step, offset;unsigned int bits;
@@ -64,30 +64,18 @@ void ucall_init(struct kvm_vm *vm, void *arg)TEST_FAIL ("Can't find a ucall mmio address" ); } -void ucall_uninit (struct kvm_vm *vm) +void ucall_arch_uninit (struct kvm_vm *vm) { ucall_exit_mmio_addr =0 ;sync_global_to_guest (vm, ucall_exit_mmio_addr); } -void ucall (uint64_t cmd,int nargs, ...) +void ucall_arch_do_ucall (vm_vaddr_t uc) { -struct ucall uc = {}; -va_list va; -int i; - -WRITE_ONCE (uc.cmd, cmd); - nargs =min (nargs,UCALL_MAX_ARGS ); - -va_start (va, nargs); -for (i =0 ; i < nargs; ++i) -WRITE_ONCE (uc.args[i],va_arg (va, uint64_t)); -va_end (va); - -WRITE_ONCE (*ucall_exit_mmio_addr, (vm_vaddr_t)&uc); +WRITE_ONCE (*ucall_exit_mmio_addr, uc); } -uint64_t get_ucall (struct kvm_vcpu *vcpu,struct ucall *uc) +uint64_t ucall_arch_get_ucall (struct kvm_vcpu *vcpu,struct ucall *uc) {struct kvm_run *run = vcpu->run;struct ucall ucall = {};
tools/testing/selftests/kvm/lib/riscv/ucall.c +5 -18
@@ -10,11 +10,11 @@#include "kvm_util.h" #include "processor.h" -void ucall_init (struct kvm_vm *vm,void *arg) +void ucall_arch_init (struct kvm_vm *vm,void *arg) { } -void ucall_uninit (struct kvm_vm *vm) +void ucall_arch_uninit (struct kvm_vm *vm) { }
@@ -44,27 +44,14 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,return ret; } -void ucall (uint64_t cmd,int nargs, ...) +void ucall_arch_do_ucall (vm_vaddr_t uc) { -struct ucall uc = { - .cmd = cmd, - }; -va_list va; -int i; - - nargs =min (nargs,UCALL_MAX_ARGS ); - -va_start (va, nargs); -for (i =0 ; i < nargs; ++i) - uc.args[i] =va_arg (va, uint64_t); -va_end (va); -sbi_ecall (KVM_RISCV_SELFTESTS_SBI_EXT ,KVM_RISCV_SELFTESTS_SBI_UCALL , - (vm_vaddr_t)&uc,0 ,0 ,0 ,0 ,0 ); + uc,0 ,0 ,0 ,0 ,0 ); } -uint64_t get_ucall (struct kvm_vcpu *vcpu,struct ucall *uc) +uint64_t ucall_arch_get_ucall (struct kvm_vcpu *vcpu,struct ucall *uc) {struct kvm_run *run = vcpu->run;struct ucall ucall = {};
tools/testing/selftests/kvm/lib/s390x/ucall.c +5 -18
@@ -6,34 +6,21 @@*/ #include "kvm_util.h" -void ucall_init (struct kvm_vm *vm,void *arg) +void ucall_arch_init (struct kvm_vm *vm,void *arg) { } -void ucall_uninit (struct kvm_vm *vm) +void ucall_arch_uninit (struct kvm_vm *vm) { } -void ucall (uint64_t cmd,int nargs, ...) +void ucall_arch_do_ucall (vm_vaddr_t uc) { -struct ucall uc = { - .cmd = cmd, - }; -va_list va; -int i; - - nargs =min (nargs,UCALL_MAX_ARGS ); - -va_start (va, nargs); -for (i =0 ; i < nargs; ++i) - uc.args[i] =va_arg (va, uint64_t); -va_end (va); -/* Exit via DIAGNOSE 0x501 (normally used for breakpoints) */ - asmvolatile ("diag 0,%0,0x501" : :"a" (&uc) :"memory" ); + asmvolatile ("diag 0,%0,0x501" : :"a" (uc) :"memory" ); } -uint64_t get_ucall (struct kvm_vcpu *vcpu,struct ucall *uc) +uint64_t ucall_arch_get_ucall (struct kvm_vcpu *vcpu,struct ucall *uc) {struct kvm_run *run = vcpu->run;struct ucall ucall = {};
tools/testing/selftests/kvm/lib/ucall_common.c new file +20 -0
@@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include "kvm_util.h" + +void ucall (uint64_t cmd,int nargs, ...) +{ +struct ucall uc = {}; +va_list va; +int i; + +WRITE_ONCE (uc.cmd, cmd); + + nargs =min (nargs,UCALL_MAX_ARGS ); + +va_start (va, nargs); +for (i =0 ; i < nargs; ++i) +WRITE_ONCE (uc.args[i],va_arg (va, uint64_t)); +va_end (va); + +ucall_arch_do_ucall ((vm_vaddr_t)&uc); +}
tools/testing/selftests/kvm/lib/x86_64/ucall.c +5 -18
@@ -8,34 +8,21 @@#define UCALL_PIO_PORT ((uint16_t)0x1000) -void ucall_init (struct kvm_vm *vm,void *arg) +void ucall_arch_init (struct kvm_vm *vm,void *arg) { } -void ucall_uninit (struct kvm_vm *vm) +void ucall_arch_uninit (struct kvm_vm *vm) { } -void ucall (uint64_t cmd,int nargs, ...) +void ucall_arch_do_ucall (vm_vaddr_t uc) { -struct ucall uc = { - .cmd = cmd, - }; -va_list va; -int i; - - nargs =min (nargs,UCALL_MAX_ARGS ); - -va_start (va, nargs); -for (i =0 ; i < nargs; ++i) - uc.args[i] =va_arg (va, uint64_t); -va_end (va); - asmvolatile ("in %[port], %%al" - : : [port]"d" (UCALL_PIO_PORT ),"D" (&uc) :"rax" ,"memory" ); + : : [port]"d" (UCALL_PIO_PORT ),"D" (uc) :"rax" ,"memory" ); } -uint64_t get_ucall (struct kvm_vcpu *vcpu,struct ucall *uc) +uint64_t ucall_arch_get_ucall (struct kvm_vcpu *vcpu,struct ucall *uc) {struct kvm_run *run = vcpu->run;struct ucall ucall = {};
Signature · 1 line
2.37.1.595.g718a3a8f04-goog
Sean Christopherson
reviewerPATCH v4 2/6 KVM: selftests: Consolidate boilerplate code in get_ucall()
In reply to Sean ChristophersonConsolidate the actual copying of a ucall struct from guest=>host into the common get_ucall(). Return a host virtual address instead of a guest virtual address even though the addr_gva2hva() part could be moved to get_ucall() too. Conceptually, get_ucall() is invoked from the host and should return a host virtual address (and returning NULL for "nothing to see here" is far superior to returning 0).
Use pointer shenanigans instead of an unnecessary bounce buffer when the caller of get_ucall() provides a valid pointer.
--- .../selftests/kvm/include/ucall_common.h | 8 ++------ .../testing/selftests/kvm/lib/aarch64/ucall.c | 14 +++----------- tools/testing/selftests/kvm/lib/riscv/ucall.c | 19 +++---------------- tools/testing/selftests/kvm/lib/s390x/ucall.c | 16 +++------------- .../testing/selftests/kvm/lib/ucall_common.c | 19 +++++++++++++++++++ .../testing/selftests/kvm/lib/x86_64/ucall.c | 16 +++------------- 6 files changed, 33 insertions(+), 59 deletions(-)
tools/testing/selftests/kvm/include/ucall_common.h +2 -6
@@ -27,9 +27,10 @@ struct ucall {void ucall_arch_init (struct kvm_vm *vm,void *arg);void ucall_arch_uninit (struct kvm_vm *vm);void ucall_arch_do_ucall (vm_vaddr_t uc); -uint64_t ucall_arch_get_ucall (struct kvm_vcpu *vcpu,struct ucall *uc); +void *ucall_arch_get_ucall (struct kvm_vcpu *vcpu);void ucall (uint64_t cmd,int nargs, ...); +uint64_t get_ucall (struct kvm_vcpu *vcpu,struct ucall *uc);static inline void ucall_init (struct kvm_vm *vm,void *arg) {
@@ -41,11 +42,6 @@ static inline void ucall_uninit(struct kvm_vm *vm)ucall_arch_uninit (vm); } -static inline uint64_t get_ucall (struct kvm_vcpu *vcpu,struct ucall *uc) -{ -return ucall_arch_get_ucall (vcpu, uc); -} -#define GUEST_SYNC_ARGS (stage, arg1, arg2, arg3, arg4) \ ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4)#define GUEST_SYNC (stage) ucall(UCALL_SYNC, 2, "hello", stage)
tools/testing/selftests/kvm/lib/aarch64/ucall.c +3 -11
@@ -75,13 +75,9 @@ void ucall_arch_do_ucall(vm_vaddr_t uc)WRITE_ONCE (*ucall_exit_mmio_addr, uc); } -uint64_t ucall_arch_get_ucall (struct kvm_vcpu *vcpu,struct ucall *uc) +void *ucall_arch_get_ucall (struct kvm_vcpu *vcpu) {struct kvm_run *run = vcpu->run; -struct ucall ucall = {}; - -if (uc) -memset (uc,0 ,sizeof (*uc));if (run->exit_reason ==KVM_EXIT_MMIO && run->mmio.phys_addr == (uint64_t )ucall_exit_mmio_addr) {
@@ -90,12 +86,8 @@ uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)TEST_ASSERT (run->mmio.is_write && run->mmio.len ==8 ,"Unexpected ucall exit mmio address access" );memcpy (&gva, run->mmio.data,sizeof (gva)); -memcpy (&ucall,addr_gva2hva (vcpu->vm, gva),sizeof (ucall)); - -vcpu_run_complete_io (vcpu); -if (uc) -memcpy (uc, &ucall,sizeof (ucall)); +return addr_gva2hva (vcpu->vm, gva); } -return ucall.cmd; +return NULL ; }
tools/testing/selftests/kvm/lib/riscv/ucall.c +3 -16
@@ -51,27 +51,15 @@ void ucall_arch_do_ucall(vm_vaddr_t uc) uc, 0, 0, 0, 0, 0); } -uint64_t ucall_arch_get_ucall (struct kvm_vcpu *vcpu,struct ucall *uc) +void *ucall_arch_get_ucall (struct kvm_vcpu *vcpu) {struct kvm_run *run = vcpu->run; -struct ucall ucall = {}; - -if (uc) -memset (uc,0 ,sizeof (*uc));if (run->exit_reason ==KVM_EXIT_RISCV_SBI && run->riscv_sbi.extension_id ==KVM_RISCV_SELFTESTS_SBI_EXT ) {switch (run->riscv_sbi.function_id) {case KVM_RISCV_SELFTESTS_SBI_UCALL : -memcpy (&ucall, -addr_gva2hva (vcpu->vm, run->riscv_sbi.args[0 ]), -sizeof (ucall)); - -vcpu_run_complete_io (vcpu); -if (uc) -memcpy (uc, &ucall,sizeof (ucall)); - -break ; +return addr_gva2hva (vcpu->vm, run->riscv_sbi.args[0 ]);case KVM_RISCV_SELFTESTS_SBI_UNEXP :vcpu_dump (stderr, vcpu,2 );TEST_ASSERT (0 ,"Unexpected trap taken by guest" );
@@ -80,6 +68,5 @@ uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc)break ; } } - -return ucall.cmd; +return NULL ; }
tools/testing/selftests/kvm/lib/s390x/ucall.c +3 -13
@@ -20,13 +20,9 @@ void ucall_arch_do_ucall(vm_vaddr_t uc) asmvolatile ("diag 0,%0,0x501" : :"a" (uc) :"memory" ); } -uint64_t ucall_arch_get_ucall (struct kvm_vcpu *vcpu,struct ucall *uc) +void *ucall_arch_get_ucall (struct kvm_vcpu *vcpu) {struct kvm_run *run = vcpu->run; -struct ucall ucall = {}; - -if (uc) -memset (uc,0 ,sizeof (*uc)); if (run->exit_reason == KVM_EXIT_S390_SIEIC && run->s390_sieic.icptcode == 4 &&
@@ -34,13 +30,7 @@ uint64_t ucall_arch_get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc) (run->s390_sieic.ipb >> 16) == 0x501) {int reg = run->s390_sieic.ipa &0xf ; -memcpy (&ucall,addr_gva2hva (vcpu->vm, run->s.regs.gprs[reg]), -sizeof (ucall)); - -vcpu_run_complete_io (vcpu); -if (uc) -memcpy (uc, &ucall,sizeof (ucall)); +return addr_gva2hva (vcpu->vm, run->s.regs.gprs[reg]); } - -return ucall.cmd; +return NULL ; }
tools/testing/selftests/kvm/lib/ucall_common.c +19 -0
@@ -18,3 +18,22 @@ void ucall(uint64_t cmd, int nargs, ...)ucall_arch_do_ucall ((vm_vaddr_t)&uc); } + +uint64_t get_ucall (struct kvm_vcpu *vcpu,struct ucall *uc) +{ +struct ucall ucall; +void *addr; + +if (!uc) + uc = &ucall; + + addr =ucall_arch_get_ucall (vcpu); +if (addr) { +memcpy (uc, addr,sizeof (*uc)); +vcpu_run_complete_io (vcpu); + }else { +memset (uc,0 ,sizeof (*uc)); + } + +return uc->cmd; +}
tools/testing/selftests/kvm/lib/x86_64/ucall.c +3 -13
@@ -22,25 +22,15 @@ void ucall_arch_do_ucall(vm_vaddr_t uc) : : [port] "d" (UCALL_PIO_PORT), "D" (uc) : "rax", "memory"); } -uint64_t ucall_arch_get_ucall (struct kvm_vcpu *vcpu,struct ucall *uc) +void *ucall_arch_get_ucall (struct kvm_vcpu *vcpu) {struct kvm_run *run = vcpu->run; -struct ucall ucall = {}; - -if (uc) -memset (uc,0 ,sizeof (*uc));if (run->exit_reason ==KVM_EXIT_IO && run->io.port ==UCALL_PIO_PORT ) {struct kvm_regs regs;vcpu_regs_get (vcpu, ®s); -memcpy (&ucall,addr_gva2hva (vcpu->vm, (vm_vaddr_t )regs.rdi), -sizeof (ucall)); - -vcpu_run_complete_io (vcpu); -if (uc) -memcpy (uc, &ucall,sizeof (ucall)); +return addr_gva2hva (vcpu->vm, regs.rdi); } - -return ucall.cmd; +return NULL ; }
Signature · 1 line
2.37.1.595.g718a3a8f04-goog
Sean Christopherson
reviewerPATCH v4 3/6 KVM: selftests: Automatically do init_ucall() for non-barebones VMs
In reply to Sean ChristophersonDo init_ucall() automatically during VM creation to kill two (three?) birds with one stone.
First, initializing ucall immediately after VM creations allows forcing aarch64's MMIO ucall address to immediately follow memslot0. This is still somewhat fragile as tests could clobber the MMIO address with a new memslot, but it's safe-ish since tests have to be conversative when accounting for memslot0. And this can be hardened in the future by creating a read-only memslot for the MMIO page (KVM ARM exits with MMIO if the guest writes to a read-only memslot). Add a TODO to document that selftests can and should use a memslot for the ucall MMIO (doing so requires yet more rework because tests assumes thay can use all memslots except memslot0).
Second, initializing ucall for all VMs prepares for making ucall initialization meaningful on all architectures. aarch64 is currently the only arch that needs to do any setup, but that will change in the future by switching to a pool-based implementation (instead of the current stack-based approach).
Lastly, defining the ucall MMIO address from common code will simplify switching all architectures (except s390) to a common MMIO-based ucall implementation (if there's ever sufficient motivation to do so).
--- .../selftests/kvm/aarch64/arch_timer.c | 1 - .../selftests/kvm/aarch64/debug-exceptions.c | 1 - .../selftests/kvm/aarch64/hypercalls.c | 1 - .../testing/selftests/kvm/aarch64/psci_test.c | 1 - .../testing/selftests/kvm/aarch64/vgic_init.c | 2 - .../testing/selftests/kvm/aarch64/vgic_irq.c | 1 - tools/testing/selftests/kvm/dirty_log_test.c | 2 - .../selftests/kvm/include/ucall_common.h | 6 +-- .../selftests/kvm/kvm_page_table_test.c | 1 - .../testing/selftests/kvm/lib/aarch64/ucall.c | 54 ++----------------- tools/testing/selftests/kvm/lib/kvm_util.c | 11 ++++ .../selftests/kvm/lib/perf_test_util.c | 2 - tools/testing/selftests/kvm/lib/riscv/ucall.c | 2 +- tools/testing/selftests/kvm/lib/s390x/ucall.c | 2 +- .../testing/selftests/kvm/lib/x86_64/ucall.c | 2 +- .../testing/selftests/kvm/memslot_perf_test.c | 1 - tools/testing/selftests/kvm/rseq_test.c | 1 - tools/testing/selftests/kvm/steal_time.c | 1 - .../kvm/system_counter_offset_test.c | 1 - 19 files changed, 20 insertions(+), 73 deletions(-)
tools/testing/selftests/kvm/aarch64/arch_timer.c +0 -1
@@ -375,7 +375,6 @@ static struct kvm_vm *test_vm_create(void)for (i =0 ; i < nr_vcpus; i++)vcpu_init_descriptor_tables (vcpus[i]); -ucall_init (vm,NULL );test_init_timer_irq (vm); gic_fd =vgic_v3_setup (vm, nr_vcpus,64 ,GICD_BASE_GPA ,GICR_BASE_GPA );__TEST_REQUIRE (gic_fd >=0 ,"Failed to create vgic-v3" );
tools/testing/selftests/kvm/aarch64/debug-exceptions.c +0 -1
@@ -254,7 +254,6 @@ int main(int argc, char *argv[])int stage; vm =vm_create_with_one_vcpu (&vcpu, guest_code); -ucall_init (vm,NULL );vm_init_descriptor_tables (vm);vcpu_init_descriptor_tables (vcpu);
tools/testing/selftests/kvm/aarch64/hypercalls.c +0 -1
@@ -236,7 +236,6 @@ static struct kvm_vm *test_vm_create(struct kvm_vcpu **vcpu) vm =vm_create_with_one_vcpu (vcpu, guest_code); -ucall_init (vm,NULL );steal_time_init (*vcpu);return vm;
tools/testing/selftests/kvm/aarch64/psci_test.c +0 -1
@@ -77,7 +77,6 @@ static struct kvm_vm *setup_vm(void *guest_code, struct kvm_vcpu **source,struct kvm_vm *vm; vm =vm_create (2 ); -ucall_init (vm,NULL );vm_ioctl (vm,KVM_ARM_PREFERRED_TARGET , &init); init.features[0 ] |= (1 <<KVM_ARM_VCPU_PSCI_0_2 );
tools/testing/selftests/kvm/aarch64/vgic_init.c +0 -2
@@ -68,8 +68,6 @@ static void guest_code(void)/* we don't want to assert on run execution, hence that helper */ static int run_vcpu (struct kvm_vcpu *vcpu) { -ucall_init (vcpu->vm,NULL ); -return __vcpu_run (vcpu) ? -errno :0 ; }
tools/testing/selftests/kvm/aarch64/vgic_irq.c +0 -1
@@ -756,7 +756,6 @@ static void test_vgic(uint32_t nr_irqs, bool level_sensitive, bool eoi_split)print_args (&args); vm =vm_create_with_one_vcpu (&vcpu, guest_code); -ucall_init (vm,NULL );vm_init_descriptor_tables (vm);vcpu_init_descriptor_tables (vcpu);
tools/testing/selftests/kvm/dirty_log_test.c +0 -2
@@ -754,8 +754,6 @@ static void run_test(enum vm_guest_mode mode, void *arg)/* Cache the HVA pointer of the region */ host_test_mem =addr_gpa2hva (vm, (vm_paddr_t )guest_test_phys_mem); -ucall_init (vm,NULL ); -/* Export the shared variables to the guest */ sync_global_to_guest (vm, host_page_size);sync_global_to_guest (vm, guest_page_size);
tools/testing/selftests/kvm/include/ucall_common.h +3 -3
@@ -24,7 +24,7 @@ struct ucall {uint64_t args[UCALL_MAX_ARGS ]; }; -void ucall_arch_init (struct kvm_vm *vm,void *arg); +void ucall_arch_init (struct kvm_vm *vm,vm_paddr_t mmio_gpa);void ucall_arch_uninit (struct kvm_vm *vm);void ucall_arch_do_ucall (vm_vaddr_t uc);void *ucall_arch_get_ucall (struct kvm_vcpu *vcpu);
@@ -32,9 +32,9 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu);void ucall (uint64_t cmd,int nargs, ...);uint64_t get_ucall (struct kvm_vcpu *vcpu,struct ucall *uc); -static inline void ucall_init (struct kvm_vm *vm,void *arg) +static inline void ucall_init (struct kvm_vm *vm,vm_paddr_t mmio_gpa) { -ucall_arch_init (vm, arg); +ucall_arch_init (vm, mmio_gpa); }static inline void ucall_uninit (struct kvm_vm *vm)
tools/testing/selftests/kvm/kvm_page_table_test.c +0 -1
@@ -289,7 +289,6 @@ static struct kvm_vm *pre_init_before_test(enum vm_guest_mode mode, void *arg) host_test_mem =addr_gpa2hva (vm, (vm_paddr_t )guest_test_phys_mem);/* Export shared structure test_args to guest */ -ucall_init (vm,NULL );sync_global_to_guest (vm, test_args); ret =sem_init (&test_stage_updated,0 ,0 );
tools/testing/selftests/kvm/lib/aarch64/ucall.c +3 -51
63 lines not on this page · raw message
tools/testing/selftests/kvm/lib/kvm_util.c +11 -0
@@ -292,6 +292,7 @@ struct kvm_vm *__vm_create(enum vm_guest_mode mode, uint32_t nr_runnable_vcpus, {uint64_t nr_pages =vm_nr_pages_required (mode, nr_runnable_vcpus, nr_extra_pages); +struct userspace_mem_region *slot0;struct kvm_vm *vm; vm =____vm_create (mode, nr_pages);
@@ -301,6 +302,16 @@ struct kvm_vm *__vm_create(enum vm_guest_mode mode, uint32_t nr_runnable_vcpus,#ifdef __x86_64__vm_create_irqchip (vm);#endif + +/* +* TODO: Add proper defines to protect the library's memslots, and then +* carve out memslot1 for the ucall MMIO address. KVM treats writes to +* read-only memslots as MMIO, and creating a read-only memslot for the +* MMIO region would prevent silently clobbering the MMIO region. +*/ + slot0 =memslot2region (vm,0 ); +ucall_init (vm, slot0->region.guest_phys_addr + slot0->region.memory_size); +return vm; }
tools/testing/selftests/kvm/lib/perf_test_util.c +0 -2
@@ -209,8 +209,6 @@ struct kvm_vm *perf_test_create_vm(enum vm_guest_mode mode, int nr_vcpus,perf_test_setup_nested (vm, nr_vcpus, vcpus); } -ucall_init (vm,NULL ); -/* Export the shared variables to the guest. */ sync_global_to_guest (vm, perf_test_args);
tools/testing/selftests/kvm/lib/riscv/ucall.c +1 -1
@@ -10,7 +10,7 @@#include "kvm_util.h" #include "processor.h" -void ucall_arch_init (struct kvm_vm *vm,void *arg) +void ucall_arch_init (struct kvm_vm *vm,vm_paddr_t mmio_gpa) { }
tools/testing/selftests/kvm/lib/s390x/ucall.c +1 -1
@@ -6,7 +6,7 @@*/ #include "kvm_util.h" -void ucall_arch_init (struct kvm_vm *vm,void *arg) +void ucall_arch_init (struct kvm_vm *vm,vm_paddr_t mmio_gpa) { }
tools/testing/selftests/kvm/lib/x86_64/ucall.c +1 -1
@@ -8,7 +8,7 @@#define UCALL_PIO_PORT ((uint16_t)0x1000) -void ucall_arch_init (struct kvm_vm *vm,void *arg) +void ucall_arch_init (struct kvm_vm *vm,vm_paddr_t mmio_gpa) { }
tools/testing/selftests/kvm/memslot_perf_test.c +0 -1
@@ -277,7 +277,6 @@ static bool prepare_vm(struct vm_data *data, int nslots, uint64_t *maxslots,TEST_ASSERT (data->hva_slots,"malloc() fail" ); data->vm =__vm_create_with_one_vcpu (&data->vcpu, mempages, guest_code); -ucall_init (data->vm,NULL );pr_info_v ("Adding slots 1..%i, each slot with %" PRIu64" pages + %" PRIu64" extra pages last\n" , max_mem_slots -1 , data->pages_per_slot, rempages);
tools/testing/selftests/kvm/rseq_test.c +0 -1
@@ -224,7 +224,6 @@ int main(int argc, char *argv[])* CPU affinity. */ vm =vm_create_with_one_vcpu (&vcpu, guest_code); -ucall_init (vm,NULL );pthread_create (&migration_thread,NULL , migration_worker, (void *)(unsigned long )gettid ());
tools/testing/selftests/kvm/steal_time.c +0 -1
@@ -266,7 +266,6 @@ int main(int ac, char **av) gpages =vm_calc_num_guest_pages (VM_MODE_DEFAULT ,STEAL_TIME_SIZE *NR_VCPUS );vm_userspace_mem_region_add (vm,VM_MEM_SRC_ANONYMOUS ,ST_GPA_BASE ,1 , gpages,0 );virt_map (vm,ST_GPA_BASE ,ST_GPA_BASE , gpages); -ucall_init (vm,NULL );TEST_REQUIRE (is_steal_time_supported (vcpus[0 ]));
tools/testing/selftests/kvm/system_counter_offset_test.c +0 -1
@@ -121,7 +121,6 @@ int main(void) vm =vm_create_with_one_vcpu (&vcpu, guest_main);check_preconditions (vcpu); -ucall_init (vm,NULL );enter_guest (vcpu);kvm_vm_free (vm);
Signature · 1 line
2.37.1.595.g718a3a8f04-goog
Sean Christopherson
reviewerPATCH v4 4/6 tools: Add atomic_test_and_set_bit()
In reply to Sean ChristophersonFrom: Peter Gonda <[email protected]>
Add x86 and generic implementations of atomic_test_and_set_bit() to allow KVM selftests to atomically manage bitmaps.
Note, the generic version is taken from arch_test_and_set_bit() as of commit 415d83249709 ("locking/atomic: Make test_and_*_bit() ordered on failure").
--- tools/arch/x86/include/asm/atomic.h | 7 +++++++ tools/include/asm-generic/atomic-gcc.h | 12 ++++++++++++ 2 files changed, 19 insertions(+)
tools/arch/x86/include/asm/atomic.h +7 -0
@@ -8,6 +8,7 @@#define LOCK_PREFIX "\n\tlock; " +#include <asm/asm.h> #include <asm/cmpxchg.h> /*
@@ -70,4 +71,10 @@ static __always_inline int atomic_cmpxchg(atomic_t *v, int old, int new) return cmpxchg(&v->counter, old, new); } +static inline int atomic_test_and_set_bit(long nr, unsigned long *addr) +{ + GEN_BINARY_RMWcc(LOCK_PREFIX __ASM_SIZE(bts), *addr, "Ir", nr, "%0", "c"); + +} + #endif /* _TOOLS_LINUX_ASM_X86_ATOMIC_H */
tools/include/asm-generic/atomic-gcc.h +12 -0
@@ -4,6 +4,7 @@#include <linux/compiler.h> #include <linux/types.h> +#include <linux/bitops.h> /* * Atomic operations that C can't guarantee us. Useful for
@@ -69,4 +70,15 @@ static inline int atomic_cmpxchg(atomic_t *v, int oldval, int newval) return cmpxchg(&(v)->counter, oldval, newval); } +static inline int atomic_test_and_set_bit (long nr,unsigned long *addr) +{ +unsigned long mask =BIT_MASK (nr); +long old; + + addr +=BIT_WORD (nr); + + old =__sync_fetch_and_or (addr, mask); +return !!(old & mask); +} +#endif /* __TOOLS_ASM_GENERIC_ATOMIC_H */
Signature · 1 line
2.37.1.595.g718a3a8f04-goog
Sean Christopherson
reviewerPATCH v4 5/6 KVM: selftests: Make arm64's MMIO ucall multi-VM friendly
In reply to Sean ChristophersonFix a mostly-theoretical bug where ARM's ucall MMIO setup could result in different VMs stomping on each other by cloberring the global pointer.
Fix the most obvious issue by saving the MMIO gpa into the VM.
A more subtle bug is that creating VMs in parallel (on multiple tasks) could result in a VM using the wrong address. Synchronizing a global to a guest effectively snapshots the value on a per-VM basis, i.e. the "global" is already prepped to work with multiple VMs, but setting the global in the host and copying it to the guest needs to happen atomically. To fix that bug, add atomic_sync_global_pointer_to_guest() to sync "global" pointers that hold per-VM values, i.e. technically need to be handled in a thread-safe manner.
--- .../selftests/kvm/include/kvm_util_base.h | 16 +++++++++++++++ .../testing/selftests/kvm/lib/aarch64/ucall.c | 20 ++++++++++++++----- 2 files changed, 31 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/include/kvm_util_base.h +16 -0
@@ -16,6 +16,7 @@#include <linux/kvm.h> #include "linux/rbtree.h" +#include <asm/atomic.h> #include <sys/ioctl.h>
@@ -81,6 +82,7 @@ struct kvm_vm {struct sparsebit *vpages_mapped;bool has_irqchip;bool pgd_created; +vm_paddr_t ucall_mmio_addr;vm_paddr_t pgd;vm_vaddr_t gdt;vm_vaddr_t tss;
@@ -714,6 +716,20 @@ kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,memcpy (&(g), _p,sizeof (g)); \ }) +/* +* Sync a global pointer to the guest that has a per-VM value, in which case +* writes to the host copy of the "global" must be serialized (in case a test +* is being truly crazy and spawning multiple VMs concurrently). +*/ +#define atomic_sync_global_pointer_to_guest (vm, g, val) ({ \ + typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \ + \ + while (cmpxchg(&g, NULL, val)) \ + ; \ + memcpy(_p, &(g), sizeof(g)); \ + WRITE_ONCE(g, NULL); \ +}) +void assert_on_unhandled_exception (struct kvm_vcpu *vcpu); void vcpu_arch_dump(FILE *stream, struct kvm_vcpu *vcpu,
tools/testing/selftests/kvm/lib/aarch64/ucall.c +15 -5
@@ -6,20 +6,30 @@*/ #include "kvm_util.h" +/* +* This "global" holds different per-VM values, it must not be accessed from +* host code except to sync the guest value, and that must be done atomically. +*/ static vm_vaddr_t *ucall_exit_mmio_addr; +static void ucall_set_mmio_addr (struct kvm_vm *vm,vm_paddr_t mmio_gpa) +{ + vm->ucall_mmio_addr = mmio_gpa; + +atomic_sync_global_pointer_to_guest (vm, ucall_exit_mmio_addr, + (vm_vaddr_t *)mmio_gpa); +} +void ucall_arch_init (struct kvm_vm *vm,vm_paddr_t mmio_gpa) {virt_pg_map (vm, mmio_gpa, mmio_gpa); - ucall_exit_mmio_addr = (vm_vaddr_t *)mmio_gpa; -sync_global_to_guest (vm, ucall_exit_mmio_addr); +ucall_set_mmio_addr (vm, mmio_gpa); }void ucall_arch_uninit (struct kvm_vm *vm) { - ucall_exit_mmio_addr =0 ; -sync_global_to_guest (vm, ucall_exit_mmio_addr); +ucall_set_mmio_addr (vm, (vm_paddr_t )NULL ); }void ucall_arch_do_ucall (vm_vaddr_t uc)
@@ -32,7 +42,7 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)struct kvm_run *run = vcpu->run;if (run->exit_reason ==KVM_EXIT_MMIO && - run->mmio.phys_addr == (uint64_t )ucall_exit_mmio_addr) { + run->mmio.phys_addr == vcpu->vm->ucall_mmio_addr) {vm_vaddr_t gva; TEST_ASSERT(run->mmio.is_write && run->mmio.len == 8,
Signature · 1 line
2.37.1.595.g718a3a8f04-goog
Re: [PATCH v4 5/6] KVM: selftests: Make arm64's MMIO ucall multi-VM friendly
Hi Sean,
Quoted 41 lines · Sean Christopherson
Fix a mostly-theoretical bug where ARM's ucall MMIO setup could result in different VMs stomping on each other by cloberring the global pointer.
Fix the most obvious issue by saving the MMIO gpa into the VM.
A more subtle bug is that creating VMs in parallel (on multiple tasks) could result in a VM using the wrong address. Synchronizing a global to a guest effectively snapshots the value on a per-VM basis, i.e. the "global" is already prepped to work with multiple VMs, but setting the global in the host and copying it to the guest needs to happen atomically. To fix that bug, add
atomic_sync_global_pointer_to_guest()to sync "global" pointers that hold per-VM values, i.e. technically need to be handled in a thread-safe manner.Signed-off-by Sean Christopherson <[email protected]>--- .../selftests/kvm/include/kvm_util_base.h | 16 +++++++++++++++ .../testing/selftests/kvm/lib/aarch64/ucall.c | 20 ++++++++++++++----- 2 files changed, 31 insertions(+), 5 deletions(-)tools/testing/selftests/kvm/include/kvm_util_base.h +2
-0@@ -16,6 +16,7 @@#include <linux/kvm.h> #include "linux/rbtree.h" +#include <asm/atomic.h> #include <sys/ioctl.h> @@ -81,6 +82,7 @@ struct kvm_vm {struct sparsebit *vpages_mapped;bool has_irqchip;bool pgd_created; +vm_paddr_t ucall_mmio_addr;vm_paddr_t pgd;vm_vaddr_t gdt;vm_vaddr_t tss;
tools/testing/selftests/kvm/include/kvm_util_base.h kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start, · line 723
Quoted 8 lines
tools/testing/selftests/kvm/include/kvm_util_base.h +5
-0@@ -714,6 +716,20 @@ kvm_userspace_memory_region_find(struct kvm_vm *vm, uint64_t start,memcpy (&(g), _p,sizeof (g)); \ }) +/* +* Sync a global pointer to the guest that has a per-VM value, in which case +* writes to the host copy of the "global" must be serialized (in case a test +* is being truly crazy and spawning multiple VMs concurrently). +*/
Do we even care about writes to the host's copy of the global pointer? I don't see how the host pointer is used beyond serializing writes into a guest.
IOW, it looks as though we could skip the whole global illusion altogether and write straight into guest memory.
--
Thanks,
Oliver
Sean Christopherson
reviewerRe: [PATCH v4 5/6] KVM: selftests: Make arm64's MMIO ucall multi-VM friendly
Quoted 9 lines · Oliver Upton
Hi Sean,
Quoted 5 lines · Sean Christopherson
+/*
+ * Sync a global pointer to the guest that has a per-VM value, in which case
+ * writes to the host copy of the "global" must be serialized (in case a test
+ * is being truly crazy and spawning multiple VMs concurrently).
+ */
Quoted 6 lines
Do we even care about writes to the host's copy of the global pointer? I don't see how the host pointer is used beyond serializing writes into a guest.
IOW, it looks as though we could skip the whole global illusion altogether and write straight into guest memory.
*sigh*
This exact thought crossed my mind when I first looked at this code, but somehow I couldn't come up with the obvious solution of using a temporary on-stack variable to hold the desired value.
Something like this should work.
#define write_guest_global(vm, g, val) ({ \
typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \
typeof(g) _val = val; \
\
memcpy(_p, &(_val), sizeof(g)); \
})Sean Christopherson
reviewerPATCH v4 6/6 KVM: selftests: Add ucall pool based implementation
In reply to Sean ChristophersonFrom: Peter Gonda <[email protected]>
To play nice with guests whose stack memory is encrypted, e.g. AMD SEV, introduce a new "ucall pool" implementation that passes the ucall struct via dedicated memory (which can be mapped shared, a.k.a. as plain text).
Because not all architectures have access to the vCPU index in the guest, use a bitmap with atomic accesses to track which entries in the pool are free/used. A list+lock could also work in theory, but synchronizing the individual pointers to the guest would be a mess.
Note, there's no need to rewalk the bitmap to ensure success. If all vCPUs are simply allocating, success is guaranteed because there are enough entries for all vCPUs. If one or more vCPUs are freeing and then reallocating, success is guaranteed because vCPUs _always_ walk the bitmap from 0=>N; if vCPU frees an entry and then wins a race to re-allocate, then either it will consume the entry it just freed (bit is the first free bit), or the losing vCPU is guaranteed to see the freed bit (winner consumes an earlier bit, which the loser hasn't yet visited).
--- .../selftests/kvm/include/ucall_common.h | 14 ++-- .../testing/selftests/kvm/lib/aarch64/ucall.c | 7 +- tools/testing/selftests/kvm/lib/riscv/ucall.c | 2 +- tools/testing/selftests/kvm/lib/s390x/ucall.c | 2 +- .../testing/selftests/kvm/lib/ucall_common.c | 83 ++++++++++++++++++- .../testing/selftests/kvm/lib/x86_64/ucall.c | 2 +- 6 files changed, 89 insertions(+), 21 deletions(-)
tools/testing/selftests/kvm/include/ucall_common.h +5 -9
@@ -22,6 +22,9 @@ enum {struct ucall {uint64_t cmd;uint64_t args[UCALL_MAX_ARGS ]; + +/* Host virtual address of this struct. */ +struct ucall *hva; };void ucall_arch_init (struct kvm_vm *vm,vm_paddr_t mmio_gpa);
@@ -32,15 +35,8 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu);void ucall (uint64_t cmd,int nargs, ...);uint64_t get_ucall (struct kvm_vcpu *vcpu,struct ucall *uc); -static inline void ucall_init (struct kvm_vm *vm,vm_paddr_t mmio_gpa) -{ -ucall_arch_init (vm, mmio_gpa); -} - -static inline void ucall_uninit (struct kvm_vm *vm) -{ -ucall_arch_uninit (vm); -} +void ucall_init (struct kvm_vm *vm,vm_paddr_t mmio_gpa); +void ucall_uninit (struct kvm_vm *vm);#define GUEST_SYNC_ARGS (stage, arg1, arg2, arg3, arg4) \ ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4)
tools/testing/selftests/kvm/lib/aarch64/ucall.c +2 -5
@@ -43,12 +43,9 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)if (run->exit_reason ==KVM_EXIT_MMIO && run->mmio.phys_addr == vcpu->vm->ucall_mmio_addr) { -vm_vaddr_t gva; - -TEST_ASSERT (run->mmio.is_write && run->mmio.len ==8 , +TEST_ASSERT (run->mmio.is_write && run->mmio.len ==sizeof (uint64_t ),"Unexpected ucall exit mmio address access" ); -memcpy (&gva, run->mmio.data,sizeof (gva)); -return addr_gva2hva (vcpu->vm, gva); +return (void *)(*((uint64_t *)run->mmio.data)); }return NULL ;
tools/testing/selftests/kvm/lib/riscv/ucall.c +1 -1
@@ -59,7 +59,7 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) run->riscv_sbi.extension_id == KVM_RISCV_SELFTESTS_SBI_EXT) {switch (run->riscv_sbi.function_id) {case KVM_RISCV_SELFTESTS_SBI_UCALL : -return addr_gva2hva (vcpu->vm, run->riscv_sbi.args[0 ]); +return (void *)run->riscv_sbi.args[0 ];case KVM_RISCV_SELFTESTS_SBI_UNEXP :vcpu_dump (stderr, vcpu,2 );TEST_ASSERT (0 ,"Unexpected trap taken by guest" );
tools/testing/selftests/kvm/lib/s390x/ucall.c +1 -1
@@ -30,7 +30,7 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu) (run->s390_sieic.ipb >> 16) == 0x501) {int reg = run->s390_sieic.ipa &0xf ; -return addr_gva2hva (vcpu->vm, run->s.regs.gprs[reg]); +return (void *)run->s.regs.gprs[reg]; }return NULL ; }
tools/testing/selftests/kvm/lib/ucall_common.c +79 -4
101 lines not on this page · raw message
tools/testing/selftests/kvm/lib/x86_64/ucall.c +1 -1
@@ -30,7 +30,7 @@ void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)struct kvm_regs regs;vcpu_regs_get (vcpu, ®s); -return addr_gva2hva (vcpu->vm, regs.rdi); +return (void *)regs.rdi; }return NULL ; }
Signature · 1 line
2.37.1.595.g718a3a8f04-goog