Skip to contentSkip to conversation

kvm: add selftests for API testing

PATCH New Paolo Bonzini 2 participants 3 messages Mar 28, 2018

Subsystems (2)
lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

cover kvm: add selftests for API testing

This is an initial reduced version of the self tests that Google contributed, with the harness reduced from 12000 to 6000 lines thanks to my trusty chainsaw (model "d%").

Rather than including them in kvm-unit-tests, I chose to place these tests into linux.git, which of course had the side effect that the code had to be cleaned up a bit to look more like Linux.

In particular a lot of changes went into lib/sparsebit.c; I don't have a testsuite for that so I chose to fuzz it instead. Turns out there were no bugs introduced by the cleanups, which actually did surprise me, but fuzzing did find some bugs in the driver instead so I think it can be trusted. :)

For now there are two simple tests noly. I'm leaving out the VMX test because nested is still not enabled by default and thus the test fails with a default configuration. In any case, the idea is to first change them to use more of the kselftest infrastructure, and then add more tests.

Paolo

Paolo Bonzini (2):
kvm: selftests: add API testing infrastructure
kvm: selftests: add sync_regs_test

 tools/testing/selftests/Makefile                   |    1 +
 tools/testing/selftests/kvm/Makefile               |   39 +
 tools/testing/selftests/kvm/include/kvm_util.h     |  142 ++
 tools/testing/selftests/kvm/include/sparsebit.h    |   75 +
 tools/testing/selftests/kvm/include/test_util.h    |   45 +
 tools/testing/selftests/kvm/include/x86.h          | 1043 ++++++++++
 tools/testing/selftests/kvm/lib/assert.c           |   87 +
 tools/testing/selftests/kvm/lib/elf.c              |  197 ++
 tools/testing/selftests/kvm/lib/io.c               |  158 ++
 tools/testing/selftests/kvm/lib/kvm_util.c         | 1480 ++++++++++++++
 .../testing/selftests/kvm/lib/kvm_util_internal.h  |   67 +
 tools/testing/selftests/kvm/lib/sparsebit.c        | 2087 ++++++++++++++++++++
 tools/testing/selftests/kvm/lib/x86.c              |  700 +++++++
 tools/testing/selftests/kvm/set_sregs_test.c       |   57 +
 tools/testing/selftests/kvm/sync_regs_test.c       |  235 +++
 15 files changed, 6413 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/Makefile
 create mode 100644 tools/testing/selftests/kvm/include/kvm_util.h
 create mode 100644 tools/testing/selftests/kvm/include/sparsebit.h
 create mode 100644 tools/testing/selftests/kvm/include/test_util.h
 create mode 100644 tools/testing/selftests/kvm/include/x86.h
 create mode 100644 tools/testing/selftests/kvm/lib/assert.c
 create mode 100644 tools/testing/selftests/kvm/lib/elf.c
 create mode 100644 tools/testing/selftests/kvm/lib/io.c
 create mode 100644 tools/testing/selftests/kvm/lib/kvm_util.c
 create mode 100644 tools/testing/selftests/kvm/lib/kvm_util_internal.h
 create mode 100644 tools/testing/selftests/kvm/lib/sparsebit.c
 create mode 100644 tools/testing/selftests/kvm/lib/x86.c
 create mode 100644 tools/testing/selftests/kvm/set_sregs_test.c
 create mode 100644 tools/testing/selftests/kvm/sync_regs_test.c
Signature · 1 line
2.16.2
#1 lore raw

PATCH 1/2 kvm: selftests: add API testing infrastructure

Testsuite contributed by Google and cleaned up by myself for inclusion in Linux.

---
 tools/testing/selftests/Makefile                   |    1 +
 tools/testing/selftests/kvm/Makefile               |   38 +
 tools/testing/selftests/kvm/include/kvm_util.h     |  139 ++
 tools/testing/selftests/kvm/include/sparsebit.h    |   75 +
 tools/testing/selftests/kvm/include/test_util.h    |   45 +
 tools/testing/selftests/kvm/include/x86.h          | 1043 ++++++++++
 tools/testing/selftests/kvm/lib/assert.c           |   87 +
 tools/testing/selftests/kvm/lib/kvm_util.c         | 1480 ++++++++++++++
 .../testing/selftests/kvm/lib/kvm_util_internal.h  |   67 +
 tools/testing/selftests/kvm/lib/sparsebit.c        | 2087 ++++++++++++++++++++
 tools/testing/selftests/kvm/lib/x86.c              |  697 +++++++
 tools/testing/selftests/kvm/set_sregs_test.c       |   57 +
 12 files changed, 5816 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/Makefile
 create mode 100644 tools/testing/selftests/kvm/include/kvm_util.h
 create mode 100644 tools/testing/selftests/kvm/include/sparsebit.h
 create mode 100644 tools/testing/selftests/kvm/include/test_util.h
 create mode 100644 tools/testing/selftests/kvm/include/x86.h
 create mode 100644 tools/testing/selftests/kvm/lib/assert.c
 create mode 100644 tools/testing/selftests/kvm/lib/kvm_util.c
 create mode 100644 tools/testing/selftests/kvm/lib/kvm_util_internal.h
 create mode 100644 tools/testing/selftests/kvm/lib/sparsebit.c
 create mode 100644 tools/testing/selftests/kvm/lib/x86.c
 create mode 100644 tools/testing/selftests/kvm/set_sregs_test.c
tools/testing/selftests/Makefile +1 -0
index 7442dfb73b7f..c98f1b874582 100644
@@ -14,6 +14,7 @@ TARGETS += gpio
 TARGETS += intel_pstate
 TARGETS += ipc
 TARGETS += kcmp
+TARGETS += kvm
 TARGETS += lib
 TARGETS += membarrier
 TARGETS += memfd
tools/testing/selftests/kvm/Makefile new file +38 -0
new file mode 100644
index 000000000000..6aade26e9ca2
@@ -0,0 +1,38 @@
+all:
+
+top_srcdir = ../../../../
+UNAME_M := $(shell uname -m)
+
+LIBKVM = lib/assert.c lib/kvm_util.c lib/sparsebit.c
+LIBKVM_x86_64 = lib/x86.c
+
+TEST_GEN_PROGS_x86_64 = set_sregs_test
+
+TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(UNAME_M))
+LIBKVM += $(LIBKVM_$(UNAME_M))
+
+INSTALL_HDR_PATH = $(top_srcdir)/usr
+LINUX_HDR_PATH = $(INSTALL_HDR_PATH)/include/
+CFLAGS += -O2 -g -I$(LINUX_HDR_PATH) -Iinclude -I$(<D)
+
+# After inclusion, $(OUTPUT) is defined and
+# $(TEST_GEN_PROGS) starts with $(OUTPUT)/
+include ../lib.mk
+
+STATIC_LIBS := $(OUTPUT)/libkvm.a
+LIBKVM_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM))
+EXTRA_CLEAN += $(LIBKVM_OBJ) $(STATIC_LIBS)
+
+x := $(shell mkdir -p $(sort $(dir $(LIBKVM_OBJ))))
+$(LIBKVM_OBJ): $(OUTPUT)/%.o: %.c
+	$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
+
+$(OUTPUT)/libkvm.a: $(LIBKVM_OBJ)
+	$(AR) crs $@ $^
+
+$(LINUX_HDR_PATH):
+	make -C $(top_srcdir) headers_install
+
+all: $(STATIC_LIBS) $(LINUX_HDR_PATH)
+$(TEST_GEN_PROGS): $(STATIC_LIBS)
+$(TEST_GEN_PROGS) $(LIBKVM_OBJ): | $(LINUX_HDR_PATH)
tools/testing/selftests/kvm/include/kvm_util.h new file +139 -0

139 lines not on this page · raw message

tools/testing/selftests/kvm/include/sparsebit.h new file +75 -0

75 lines not on this page · raw message

tools/testing/selftests/kvm/include/test_util.h new file +45 -0

45 lines not on this page · raw message

tools/testing/selftests/kvm/include/x86.h new file +1043 -0

1,043 lines not on this page · raw message

tools/testing/selftests/kvm/lib/assert.c new file +87 -0

87 lines not on this page · raw message

tools/testing/selftests/kvm/lib/kvm_util.c new file +1480 -0

1,480 lines not on this page · raw message

tools/testing/selftests/kvm/lib/kvm_util_internal.h new file +67 -0

67 lines not on this page · raw message

tools/testing/selftests/kvm/lib/sparsebit.c new file +2087 -0

19 lines not on this page · raw message

tools/testing/selftests/kvm/lib/x86.c new file +697 -0

697 lines not on this page · raw message

tools/testing/selftests/kvm/set_sregs_test.c new file +54 -0

54 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.16.2

Part of the diff is not shown here. View the raw message.

author maintainer
#2 lore raw

PATCH 2/2 kvm: selftests: add sync_regs_test

In reply to Paolo Bonzini

This includes the infrastructure to map the test into the guest and run code from the test program inside a VM.

---
 tools/testing/selftests/kvm/Makefile           |   3 +-
 tools/testing/selftests/kvm/include/kvm_util.h |   3 +
 tools/testing/selftests/kvm/lib/elf.c          | 197 +++++++++++++++++++++
 tools/testing/selftests/kvm/lib/io.c           | 158 +++++++++++++++++
 tools/testing/selftests/kvm/lib/x86.c          |   3 +
 tools/testing/selftests/kvm/sync_regs_test.c   | 235 +++++++++++++++++++++++++
 6 files changed, 598 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/kvm/lib/elf.c
 create mode 100644 tools/testing/selftests/kvm/lib/io.c
 create mode 100644 tools/testing/selftests/kvm/sync_regs_test.c
tools/testing/selftests/kvm/Makefile +2 -1
index 6aade26e9ca2..dc44de904797 100644
@@ -3,10 +3,11 @@ all:
 top_srcdir = ../../../../
 UNAME_M := $(shell uname -m)
 
-LIBKVM = lib/assert.c lib/kvm_util.c lib/sparsebit.c
+LIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c lib/sparsebit.c
 LIBKVM_x86_64 = lib/x86.c
 
 TEST_GEN_PROGS_x86_64 = set_sregs_test
+TEST_GEN_PROGS_x86_64 += sync_regs_test
 
 TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(UNAME_M))
 LIBKVM += $(LIBKVM_$(UNAME_M))
tools/testing/selftests/kvm/include/kvm_util.h +3 -0
index 44aa1e204dbc..c823e0cb77e6 100644
@@ -57,6 +57,9 @@ void kvm_vm_free(struct kvm_vm *vmp);
 int kvm_memcmp_hva_gva(void *hva,
 	struct kvm_vm *vm, const vm_vaddr_t gva, size_t len);
 
+void kvm_vm_elf_load(struct kvm_vm *vm, const char *filename,
+	uint32_t data_memslot, uint32_t pgd_memslot);
+
 void vm_dump(FILE *stream, struct kvm_vm *vm, uint8_t indent);
 void vcpu_dump(FILE *stream, struct kvm_vm *vm,
 	uint32_t vcpuid, uint8_t indent);
tools/testing/selftests/kvm/lib/elf.c new file +197 -0

197 lines not on this page · raw message

tools/testing/selftests/kvm/lib/io.c new file +158 -0

158 lines not on this page · raw message

tools/testing/selftests/kvm/lib/x86.c +3 -0
index 2b7f79df72ab..e374c140b68d 100644
@@ -687,6 +687,9 @@ struct kvm_vm *vm_create_default(uint32_t vcpuid, void *guest_code)
 	/* Create VM */
 	vm = vm_create(VM_MODE_FLAT48PG, DEFAULT_GUEST_PHY_PAGES, O_RDWR);
 
+	/* Setup guest code */
+	kvm_vm_elf_load(vm, program_invocation_name, 0, 0);
+
 	/* Setup IRQ Chip */
 	vm_create_irqchip(vm);
 
tools/testing/selftests/kvm/sync_regs_test.c new file +232 -0

232 lines not on this page · raw message

Signature · 1 line
2.16.2