Commit 14c47b75 authored by Andrew Jones's avatar Andrew Jones Committed by Paolo Bonzini

kvm: selftests: introduce ucall

Rework the guest exit to userspace code to generalize the concept
into what it is, a "hypercall to userspace", and provide two
implementations of it: the PortIO version currently used, but only
useable by x86, and an MMIO version that other architectures (except
s390) can use.
Signed-off-by: default avatarAndrew Jones <drjones@redhat.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 6c930268
...@@ -3,7 +3,7 @@ all: ...@@ -3,7 +3,7 @@ all:
top_srcdir = ../../../../ top_srcdir = ../../../../
UNAME_M := $(shell uname -m) UNAME_M := $(shell uname -m)
LIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c lib/sparsebit.c LIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c lib/ucall.c lib/sparsebit.c
LIBKVM_x86_64 = lib/x86.c lib/vmx.c LIBKVM_x86_64 = lib/x86.c lib/vmx.c
TEST_GEN_PROGS_x86_64 = platform_info_test TEST_GEN_PROGS_x86_64 = platform_info_test
......
...@@ -67,6 +67,7 @@ int main(int argc, char *argv[]) ...@@ -67,6 +67,7 @@ int main(int argc, char *argv[])
struct kvm_vm *vm; struct kvm_vm *vm;
struct kvm_sregs sregs; struct kvm_sregs sregs;
struct kvm_cpuid_entry2 *entry; struct kvm_cpuid_entry2 *entry;
struct ucall uc;
int rc; int rc;
entry = kvm_get_supported_cpuid_entry(1); entry = kvm_get_supported_cpuid_entry(1);
...@@ -87,21 +88,20 @@ int main(int argc, char *argv[]) ...@@ -87,21 +88,20 @@ int main(int argc, char *argv[])
rc = _vcpu_run(vm, VCPU_ID); rc = _vcpu_run(vm, VCPU_ID);
if (run->exit_reason == KVM_EXIT_IO) { if (run->exit_reason == KVM_EXIT_IO) {
switch (run->io.port) { switch (get_ucall(vm, VCPU_ID, &uc)) {
case GUEST_PORT_SYNC: case UCALL_SYNC:
/* emulate hypervisor clearing CR4.OSXSAVE */ /* emulate hypervisor clearing CR4.OSXSAVE */
vcpu_sregs_get(vm, VCPU_ID, &sregs); vcpu_sregs_get(vm, VCPU_ID, &sregs);
sregs.cr4 &= ~X86_CR4_OSXSAVE; sregs.cr4 &= ~X86_CR4_OSXSAVE;
vcpu_sregs_set(vm, VCPU_ID, &sregs); vcpu_sregs_set(vm, VCPU_ID, &sregs);
break; break;
case GUEST_PORT_ABORT: case UCALL_ABORT:
TEST_ASSERT(false, "Guest CR4 bit (OSXSAVE) unsynchronized with CPUID bit."); TEST_ASSERT(false, "Guest CR4 bit (OSXSAVE) unsynchronized with CPUID bit.");
break; break;
case GUEST_PORT_DONE: case UCALL_DONE:
goto done; goto done;
default: default:
TEST_ASSERT(false, "Unknown port 0x%x.", TEST_ASSERT(false, "Unknown ucall 0x%x.", uc.cmd);
run->io.port);
} }
} }
} }
......
...@@ -110,7 +110,7 @@ void *vcpu_worker(void *data) ...@@ -110,7 +110,7 @@ void *vcpu_worker(void *data)
uint64_t loops, *guest_array, pages_count = 0; uint64_t loops, *guest_array, pages_count = 0;
struct kvm_vm *vm = data; struct kvm_vm *vm = data;
struct kvm_run *run; struct kvm_run *run;
struct guest_args args; struct ucall uc;
run = vcpu_state(vm, VCPU_ID); run = vcpu_state(vm, VCPU_ID);
...@@ -124,9 +124,8 @@ void *vcpu_worker(void *data) ...@@ -124,9 +124,8 @@ void *vcpu_worker(void *data)
while (!READ_ONCE(host_quit)) { while (!READ_ONCE(host_quit)) {
/* Let the guest to dirty these random pages */ /* Let the guest to dirty these random pages */
ret = _vcpu_run(vm, VCPU_ID); ret = _vcpu_run(vm, VCPU_ID);
guest_args_read(vm, VCPU_ID, &args);
if (run->exit_reason == KVM_EXIT_IO && if (run->exit_reason == KVM_EXIT_IO &&
args.port == GUEST_PORT_SYNC) { get_ucall(vm, VCPU_ID, &uc) == UCALL_SYNC) {
pages_count += TEST_PAGES_PER_LOOP; pages_count += TEST_PAGES_PER_LOOP;
generate_random_array(guest_array, TEST_PAGES_PER_LOOP); generate_random_array(guest_array, TEST_PAGES_PER_LOOP);
} else { } else {
......
...@@ -152,43 +152,49 @@ allocate_kvm_dirty_log(struct kvm_userspace_memory_region *region); ...@@ -152,43 +152,49 @@ allocate_kvm_dirty_log(struct kvm_userspace_memory_region *region);
int vm_create_device(struct kvm_vm *vm, struct kvm_create_device *cd); int vm_create_device(struct kvm_vm *vm, struct kvm_create_device *cd);
#define GUEST_PORT_SYNC 0x1000 #define sync_global_to_guest(vm, g) ({ \
#define GUEST_PORT_ABORT 0x1001 typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \
#define GUEST_PORT_DONE 0x1002 memcpy(_p, &(g), sizeof(g)); \
})
static inline void __exit_to_l0(uint16_t port, uint64_t arg0, uint64_t arg1)
{ #define sync_global_from_guest(vm, g) ({ \
__asm__ __volatile__("in %[port], %%al" typeof(g) *_p = addr_gva2hva(vm, (vm_vaddr_t)&(g)); \
: memcpy(&(g), _p, sizeof(g)); \
: [port]"d"(port), "D"(arg0), "S"(arg1) })
: "rax");
} /* ucall implementation types */
typedef enum {
/* UCALL_PIO,
* Allows to pass three arguments to the host: port is 16bit wide, UCALL_MMIO,
* arg0 & arg1 are 64bit wide } ucall_type_t;
*/
#define GUEST_SYNC_ARGS(_port, _arg0, _arg1) \ /* Common ucalls */
__exit_to_l0(_port, (uint64_t) (_arg0), (uint64_t) (_arg1)) enum {
UCALL_NONE,
#define GUEST_ASSERT(_condition) do { \ UCALL_SYNC,
if (!(_condition)) \ UCALL_ABORT,
GUEST_SYNC_ARGS(GUEST_PORT_ABORT, \ UCALL_DONE,
"Failed guest assert: " \ };
#_condition, __LINE__); \
} while (0)
#define GUEST_SYNC(stage) GUEST_SYNC_ARGS(GUEST_PORT_SYNC, "hello", stage)
#define GUEST_DONE() GUEST_SYNC_ARGS(GUEST_PORT_DONE, 0, 0) #define UCALL_MAX_ARGS 6
struct guest_args { struct ucall {
uint64_t arg0; uint64_t cmd;
uint64_t arg1; uint64_t args[UCALL_MAX_ARGS];
uint16_t port; };
} __attribute__ ((packed));
void guest_args_read(struct kvm_vm *vm, uint32_t vcpu_id, void ucall_init(struct kvm_vm *vm, ucall_type_t type, void *arg);
struct guest_args *args); void ucall_uninit(struct kvm_vm *vm);
void ucall(uint64_t cmd, int nargs, ...);
uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc);
#define GUEST_SYNC(stage) ucall(UCALL_SYNC, 2, "hello", stage)
#define GUEST_DONE() ucall(UCALL_DONE, 0)
#define GUEST_ASSERT(_condition) do { \
if (!(_condition)) \
ucall(UCALL_ABORT, 2, \
"Failed guest assert: " \
#_condition, __LINE__); \
} while (0)
#endif /* SELFTEST_KVM_UTIL_H */ #endif /* SELFTEST_KVM_UTIL_H */
...@@ -133,6 +133,7 @@ struct kvm_vm *vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm) ...@@ -133,6 +133,7 @@ struct kvm_vm *vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm)
case VM_MODE_FLAT48PG: case VM_MODE_FLAT48PG:
vm->page_size = 0x1000; vm->page_size = 0x1000;
vm->page_shift = 12; vm->page_shift = 12;
vm->va_bits = 48;
/* Limit to 48-bit canonical virtual addresses. */ /* Limit to 48-bit canonical virtual addresses. */
vm->vpages_valid = sparsebit_alloc(); vm->vpages_valid = sparsebit_alloc();
...@@ -1669,17 +1670,3 @@ void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva) ...@@ -1669,17 +1670,3 @@ void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva)
{ {
return addr_gpa2hva(vm, addr_gva2gpa(vm, gva)); return addr_gpa2hva(vm, addr_gva2gpa(vm, gva));
} }
void guest_args_read(struct kvm_vm *vm, uint32_t vcpu_id,
struct guest_args *args)
{
struct kvm_run *run = vcpu_state(vm, vcpu_id);
struct kvm_regs regs;
memset(&regs, 0, sizeof(regs));
vcpu_regs_get(vm, vcpu_id, &regs);
args->port = run->io.port;
args->arg0 = regs.rdi;
args->arg1 = regs.rsi;
}
...@@ -47,6 +47,7 @@ struct kvm_vm { ...@@ -47,6 +47,7 @@ struct kvm_vm {
int fd; int fd;
unsigned int page_size; unsigned int page_size;
unsigned int page_shift; unsigned int page_shift;
unsigned int va_bits;
uint64_t max_gfn; uint64_t max_gfn;
struct vcpu *vcpu_head; struct vcpu *vcpu_head;
struct userspace_mem_region *userspace_mem_region_head; struct userspace_mem_region *userspace_mem_region_head;
......
// SPDX-License-Identifier: GPL-2.0
/*
* ucall support. A ucall is a "hypercall to userspace".
*
* Copyright (C) 2018, Red Hat, Inc.
*/
#include "kvm_util.h"
#include "kvm_util_internal.h"
#define UCALL_PIO_PORT ((uint16_t)0x1000)
static ucall_type_t ucall_type;
static vm_vaddr_t *ucall_exit_mmio_addr;
static bool ucall_mmio_init(struct kvm_vm *vm, vm_paddr_t gpa)
{
if (kvm_userspace_memory_region_find(vm, gpa, gpa + 1))
return false;
virt_pg_map(vm, gpa, gpa, 0);
ucall_exit_mmio_addr = (vm_vaddr_t *)gpa;
sync_global_to_guest(vm, ucall_exit_mmio_addr);
return true;
}
void ucall_init(struct kvm_vm *vm, ucall_type_t type, void *arg)
{
ucall_type = type;
sync_global_to_guest(vm, ucall_type);
if (type == UCALL_PIO)
return;
if (type == UCALL_MMIO) {
vm_paddr_t gpa, start, end, step;
bool ret;
if (arg) {
gpa = (vm_paddr_t)arg;
ret = ucall_mmio_init(vm, gpa);
TEST_ASSERT(ret, "Can't set ucall mmio address to %lx", gpa);
return;
}
/*
* Find an address within the allowed virtual address space,
* that does _not_ have a KVM memory region associated with it.
* Identity mapping an address like this allows the guest to
* access it, but as KVM doesn't know what to do with it, it
* will assume it's something userspace handles and exit with
* KVM_EXIT_MMIO. Well, at least that's how it works for AArch64.
* Here we start with a guess that the addresses around two
* thirds of the VA space are unmapped and then work both down
* and up from there in 1/6 VA space sized steps.
*/
start = 1ul << (vm->va_bits * 2 / 3);
end = 1ul << vm->va_bits;
step = 1ul << (vm->va_bits / 6);
for (gpa = start; gpa >= 0; gpa -= step) {
if (ucall_mmio_init(vm, gpa & ~(vm->page_size - 1)))
return;
}
for (gpa = start + step; gpa < end; gpa += step) {
if (ucall_mmio_init(vm, gpa & ~(vm->page_size - 1)))
return;
}
TEST_ASSERT(false, "Can't find a ucall mmio address");
}
}
void ucall_uninit(struct kvm_vm *vm)
{
ucall_type = 0;
sync_global_to_guest(vm, ucall_type);
ucall_exit_mmio_addr = 0;
sync_global_to_guest(vm, ucall_exit_mmio_addr);
}
static void ucall_pio_exit(struct ucall *uc)
{
#ifdef __x86_64__
asm volatile("in %[port], %%al"
: : [port] "d" (UCALL_PIO_PORT), "D" (uc) : "rax");
#endif
}
static void ucall_mmio_exit(struct ucall *uc)
{
*ucall_exit_mmio_addr = (vm_vaddr_t)uc;
}
void ucall(uint64_t cmd, int nargs, ...)
{
struct ucall uc = {
.cmd = cmd,
};
va_list va;
int i;
nargs = nargs <= UCALL_MAX_ARGS ? 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);
switch (ucall_type) {
case UCALL_PIO:
ucall_pio_exit(&uc);
break;
case UCALL_MMIO:
ucall_mmio_exit(&uc);
break;
};
}
uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc)
{
struct kvm_run *run = vcpu_state(vm, vcpu_id);
memset(uc, 0, sizeof(*uc));
#ifdef __x86_64__
if (ucall_type == UCALL_PIO && run->exit_reason == KVM_EXIT_IO &&
run->io.port == UCALL_PIO_PORT) {
struct kvm_regs regs;
vcpu_regs_get(vm, vcpu_id, &regs);
memcpy(uc, addr_gva2hva(vm, (vm_vaddr_t)regs.rdi), sizeof(*uc));
return uc->cmd;
}
#endif
if (ucall_type == UCALL_MMIO && run->exit_reason == KVM_EXIT_MMIO &&
run->mmio.phys_addr == (uint64_t)ucall_exit_mmio_addr) {
vm_vaddr_t gva;
TEST_ASSERT(run->mmio.is_write && run->mmio.len == 8,
"Unexpected ucall exit mmio address access");
gva = *(vm_vaddr_t *)run->mmio.data;
memcpy(uc, addr_gva2hva(vm, gva), sizeof(*uc));
}
return uc->cmd;
}
...@@ -48,7 +48,7 @@ static void set_msr_platform_info_enabled(struct kvm_vm *vm, bool enable) ...@@ -48,7 +48,7 @@ static void set_msr_platform_info_enabled(struct kvm_vm *vm, bool enable)
static void test_msr_platform_info_enabled(struct kvm_vm *vm) static void test_msr_platform_info_enabled(struct kvm_vm *vm)
{ {
struct kvm_run *run = vcpu_state(vm, VCPU_ID); struct kvm_run *run = vcpu_state(vm, VCPU_ID);
struct guest_args args; struct ucall uc;
set_msr_platform_info_enabled(vm, true); set_msr_platform_info_enabled(vm, true);
vcpu_run(vm, VCPU_ID); vcpu_run(vm, VCPU_ID);
...@@ -56,11 +56,11 @@ static void test_msr_platform_info_enabled(struct kvm_vm *vm) ...@@ -56,11 +56,11 @@ static void test_msr_platform_info_enabled(struct kvm_vm *vm)
"Exit_reason other than KVM_EXIT_IO: %u (%s),\n", "Exit_reason other than KVM_EXIT_IO: %u (%s),\n",
run->exit_reason, run->exit_reason,
exit_reason_str(run->exit_reason)); exit_reason_str(run->exit_reason));
guest_args_read(vm, VCPU_ID, &args); get_ucall(vm, VCPU_ID, &uc);
TEST_ASSERT(args.port == GUEST_PORT_SYNC, TEST_ASSERT(uc.cmd == UCALL_SYNC,
"Received IO from port other than PORT_HOST_SYNC: %u\n", "Received ucall other than UCALL_SYNC: %u\n",
run->io.port); ucall);
TEST_ASSERT((args.arg1 & MSR_PLATFORM_INFO_MAX_TURBO_RATIO) == TEST_ASSERT((uc.args[1] & MSR_PLATFORM_INFO_MAX_TURBO_RATIO) ==
MSR_PLATFORM_INFO_MAX_TURBO_RATIO, MSR_PLATFORM_INFO_MAX_TURBO_RATIO,
"Expected MSR_PLATFORM_INFO to have max turbo ratio mask: %i.", "Expected MSR_PLATFORM_INFO to have max turbo ratio mask: %i.",
MSR_PLATFORM_INFO_MAX_TURBO_RATIO); MSR_PLATFORM_INFO_MAX_TURBO_RATIO);
......
...@@ -127,6 +127,7 @@ int main(int argc, char *argv[]) ...@@ -127,6 +127,7 @@ int main(int argc, char *argv[])
struct kvm_vm *vm; struct kvm_vm *vm;
struct kvm_run *run; struct kvm_run *run;
struct kvm_x86_state *state; struct kvm_x86_state *state;
struct ucall uc;
int stage; int stage;
struct kvm_cpuid_entry2 *entry = kvm_get_supported_cpuid_entry(1); struct kvm_cpuid_entry2 *entry = kvm_get_supported_cpuid_entry(1);
...@@ -155,23 +156,23 @@ int main(int argc, char *argv[]) ...@@ -155,23 +156,23 @@ int main(int argc, char *argv[])
memset(&regs1, 0, sizeof(regs1)); memset(&regs1, 0, sizeof(regs1));
vcpu_regs_get(vm, VCPU_ID, &regs1); vcpu_regs_get(vm, VCPU_ID, &regs1);
switch (run->io.port) { switch (get_ucall(vm, VCPU_ID, &uc)) {
case GUEST_PORT_ABORT: case UCALL_ABORT:
TEST_ASSERT(false, "%s at %s:%d", (const char *) regs1.rdi, TEST_ASSERT(false, "%s at %s:%d", (const char *)uc.args[0],
__FILE__, regs1.rsi); __FILE__, uc.args[1]);
/* NOT REACHED */ /* NOT REACHED */
case GUEST_PORT_SYNC: case UCALL_SYNC:
break; break;
case GUEST_PORT_DONE: case UCALL_DONE:
goto done; goto done;
default: default:
TEST_ASSERT(false, "Unknown port 0x%x.", run->io.port); TEST_ASSERT(false, "Unknown ucall 0x%x.", uc.cmd);
} }
/* PORT_SYNC is handled here. */ /* UCALL_SYNC is handled here. */
TEST_ASSERT(!strcmp((const char *)regs1.rdi, "hello") && TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") &&
regs1.rsi == stage, "Unexpected register values vmexit #%lx, got %lx", uc.args[1] == stage, "Unexpected register values vmexit #%lx, got %lx",
stage, (ulong) regs1.rsi); stage, (ulong)uc.args[1]);
state = vcpu_save_state(vm, VCPU_ID); state = vcpu_save_state(vm, VCPU_ID);
kvm_vm_release(vm); kvm_vm_release(vm);
......
...@@ -146,26 +146,25 @@ int main(int argc, char *argv[]) ...@@ -146,26 +146,25 @@ int main(int argc, char *argv[])
for (;;) { for (;;) {
volatile struct kvm_run *run = vcpu_state(vm, VCPU_ID); volatile struct kvm_run *run = vcpu_state(vm, VCPU_ID);
struct guest_args args; struct ucall uc;
vcpu_run(vm, VCPU_ID); vcpu_run(vm, VCPU_ID);
guest_args_read(vm, VCPU_ID, &args);
TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
"Got exit_reason other than KVM_EXIT_IO: %u (%s)\n", "Got exit_reason other than KVM_EXIT_IO: %u (%s)\n",
run->exit_reason, run->exit_reason,
exit_reason_str(run->exit_reason)); exit_reason_str(run->exit_reason));
switch (args.port) { switch (get_ucall(vm, VCPU_ID, &uc)) {
case GUEST_PORT_ABORT: case UCALL_ABORT:
TEST_ASSERT(false, "%s", (const char *) args.arg0); TEST_ASSERT(false, "%s", (const char *)uc.args[0]);
/* NOT REACHED */ /* NOT REACHED */
case GUEST_PORT_SYNC: case UCALL_SYNC:
report(args.arg1); report(uc.args[1]);
break; break;
case GUEST_PORT_DONE: case UCALL_DONE:
goto done; goto done;
default: default:
TEST_ASSERT(false, "Unknown port 0x%x.", args.port); TEST_ASSERT(false, "Unknown ucall 0x%x.", uc.cmd);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment