aboutsummaryrefslogtreecommitdiff
path: root/tools/testing/selftests/kvm/lib
AgeCommit message (Collapse)AuthorFilesLines
2019-06-19treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 482Thomas Gleixner8-16/+8
Based on 1 normalized pattern(s): this work is licensed under the terms of the gnu gpl version 2 extracted by the scancode license scanner the SPDX license identifier GPL-2.0-only has been chosen to replace the boilerplate/reference in 48 file(s). Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Allison Randal <[email protected]> Reviewed-by: Enrico Weigelt <[email protected]> Cc: [email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
2019-06-19tests: kvm: Check for a kernel warningAaron Lewis2-0/+52
When running with /sys/module/kvm_intel/parameters/unrestricted_guest=N, test that a kernel warning does not occur informing us that vcpu->mmio_needed=1. This can happen when KVM_RUN is called after a triple fault. This test was made to detect a bug that was reported by Syzkaller (https://groups.google.com/forum/#!topic/syzkaller/lHfau8E3SOE) and fixed with commit bbeac2830f4de ("KVM: X86: Fix residual mmio emulation request to userspace"). Signed-off-by: Aaron Lewis <[email protected]> Reviewed-by: Jim Mattson <[email protected]> Reviewed-by: Peter Shier <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2019-06-05kvm: selftests: introduce aarch64_vcpu_add_defaultAndrew Jones1-2/+7
This is the same as vm_vcpu_add_default, but it also takes a kvm_vcpu_init struct pointer. Signed-off-by: Andrew Jones <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2019-06-05kvm: selftests: introduce aarch64_vcpu_setupAndrew Jones1-6/+13
This allows aarch64 tests to run on more targets, such as the Arm simulator that doesn't like KVM_ARM_TARGET_GENERIC_V8. And it also allows aarch64 tests to provide vcpu features in struct kvm_vcpu_init. Additionally it drops the unused memslot parameters. Signed-off-by: Andrew Jones <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2019-06-05kvm: selftests: hide vcpu_setup in processor codePaolo Bonzini4-25/+23
This removes the processor-dependent arguments from vm_vcpu_add. Signed-off-by: Paolo Bonzini <[email protected]>
2019-06-04kvm: selftests: ucall improvementsAndrew Jones1-6/+13
Make sure we complete the I/O after determining we have a ucall, which is I/O. Also allow the *uc parameter to optionally be NULL. It's quite possible that a test case will only care about the return value, like for example when looping on a check for UCALL_DONE. Signed-off-by: Andrew Jones <[email protected]> Reviewed-by: Peter Xu <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2019-06-04KVM: selftests: Add processor code for s390xThomas Huth1-0/+286
Code that takes care of basic CPU setup, page table walking, etc. Signed-off-by: Thomas Huth <[email protected]> Message-Id: <[email protected]> Signed-off-by: Christian Borntraeger <[email protected]>
2019-06-04KVM: selftests: Align memory region addresses to 1M on s390xThomas Huth1-5/+16
On s390x, there is a constraint that memory regions have to be aligned to 1M (or running the VM will fail). Introduce a new "alignment" variable in the vm_userspace_mem_region_add() function which now can be used for both, huge page and s390x alignment requirements. Signed-off-by: Thomas Huth <[email protected]> Message-Id: <[email protected]> Signed-off-by: Christian Borntraeger <[email protected]> [prepare for THP as outlined by Andrew Jones]
2019-06-04KVM: selftests: Introduce a VM_MODE_DEFAULT macro for the default bitsThomas Huth2-2/+2
This will be required later for tests like the kvm_create_max_vcpus test that do not use the vm_create_default() function. Reviewed-by: Andrew Jones <[email protected]> Signed-off-by: Thomas Huth <[email protected]> Message-Id: <[email protected]> Signed-off-by: Christian Borntraeger <[email protected]>
2019-06-04KVM: selftests: Guard struct kvm_vcpu_events with __KVM_HAVE_VCPU_EVENTSThomas Huth1-0/+2
The struct kvm_vcpu_events code is only available on certain architectures (arm, arm64 and x86). To be able to compile kvm_util.c also for other architectures, we have to fence the code with __KVM_HAVE_VCPU_EVENTS. Reviewed-by: David Hildenbrand <[email protected]> Reviewed-by: Andrew Jones <[email protected]> Signed-off-by: Thomas Huth <[email protected]> Message-Id: <[email protected]> Signed-off-by: Christian Borntraeger <[email protected]>
2019-05-24KVM: selftests: Wrap vcpu_nested_state_get/set functions with x86 guardThomas Huth1-0/+2
struct kvm_nested_state is only available on x86 so far. To be able to compile the code on other architectures as well, we need to wrap the related code with #ifdefs. Signed-off-by: Thomas Huth <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2019-05-24kvm: selftests: aarch64: compile with warnings onAndrew Jones1-4/+5
aarch64 fixups needed to compile with warnings as errors. Reviewed-by: Thomas Huth <[email protected]> Signed-off-by: Andrew Jones <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2019-05-24kvm: selftests: aarch64: fix default vm modeAndrew Jones1-1/+1
VM_MODE_P52V48_4K is not a valid mode for AArch64. Replace its use in vm_create_default() with a mode that works and represents a good AArch64 default. (We didn't ever see a problem with this because we don't have any unit tests using vm_create_default(), but it's good to get it fixed in advance.) Reported-by: Thomas Huth <[email protected]> Signed-off-by: Andrew Jones <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2019-05-24KVM: selftests: Compile code with warnings enabledThomas Huth2-6/+1
So far the KVM selftests are compiled without any compiler warnings enabled. That's quite bad, since we miss a lot of possible bugs this way. Let's enable at least "-Wall" and some other useful warning flags now, and fix at least the trivial problems in the code (like unused variables). Signed-off-by: Thomas Huth <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2019-05-24kvm: selftests: avoid type punningPaolo Bonzini1-1/+1
Avoid warnings from -Wstrict-aliasing by using memcpy. Reviewed-by: Thomas Huth <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2019-05-08tests: kvm: Add tests for KVM_SET_NESTED_STATEAaron Lewis1-0/+32
Add tests for KVM_SET_NESTED_STATE and for various code paths in its implementation in vmx_set_nested_state(). Signed-off-by: Aaron Lewis <[email protected]> Reviewed-by: Marc Orr <[email protected]> Reviewed-by: Peter Shier <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2019-04-16selftests: kvm: add a selftest for SMMVitaly Kuznetsov1-6/+6
Add a simple test for SMM, based on VMX. The test implements its own sync between the guest and the host as using our ucall library seems to be too cumbersome: SMI handler is happening in real-address mode. This patch also fixes KVM_SET_NESTED_STATE to happen after KVM_SET_VCPU_EVENTS, in fact it places it last. This is because KVM needs to know whether the processor is in SMM or not. Signed-off-by: Vitaly Kuznetsov <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2019-04-16selftests: kvm/evmcs_test: complete I/O before migrating guest statePaolo Bonzini2-0/+13
Starting state migration after an IO exit without first completing IO may result in test failures. We already have two tests that need this (this patch in fact fixes evmcs_test, similar to what was fixed for state_test in commit 0f73bbc851ed, "KVM: selftests: complete IO before migrating guest state", 2019-03-13) and a third is coming. So, move the code to vcpu_save_state, and while at it do not access register state until after I/O is complete. Signed-off-by: Paolo Bonzini <[email protected]>
2019-03-28KVM: selftests: complete IO before migrating guest stateSean Christopherson1-0/+16
Documentation/virtual/kvm/api.txt states: NOTE: For KVM_EXIT_IO, KVM_EXIT_MMIO, KVM_EXIT_OSI, KVM_EXIT_PAPR and KVM_EXIT_EPR the corresponding operations are complete (and guest state is consistent) only after userspace has re-entered the kernel with KVM_RUN. The kernel side will first finish incomplete operations and then check for pending signals. Userspace can re-enter the guest with an unmasked signal pending to complete pending operations. Because guest state may be inconsistent, starting state migration after an IO exit without first completing IO may result in test failures, e.g. a proposed change to KVM's handling of %rip in its fast PIO handling[1] will cause the new VM, i.e. the post-migration VM, to have its %rip set to the IN instruction that triggered KVM_EXIT_IO, leading to a test assertion due to a stage mismatch. For simplicitly, require KVM_CAP_IMMEDIATE_EXIT to complete IO and skip the test if it's not available. The addition of KVM_CAP_IMMEDIATE_EXIT predates the state selftest by more than a year. [1] https://patchwork.kernel.org/patch/10848545/ Fixes: fa3899add1056 ("kvm: selftests: add basic test for state save and restore") Reported-by: Jim Mattson <[email protected]> Signed-off-by: Sean Christopherson <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2019-01-25kvm: selftests: Fix region overlap check in kvm_utilBen Gardon1-7/+2
Fix a call to userspace_mem_region_find to conform to its spec of taking an inclusive, inclusive range. It was previously being called with an inclusive, exclusive range. Also remove a redundant region bounds check in vm_userspace_mem_region_add. Region overlap checking is already performed by the call to userspace_mem_region_find. Tested: Compiled tools/testing/selftests/kvm with -static Ran all resulting test binaries on an Intel Haswell test machine All tests passed Signed-off-by: Ben Gardon <[email protected]> Reviewed-by: Jim Mattson <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2018-12-21kvm: selftests: ucall: fix exit mmio address guessingAndrew Jones1-11/+19
Fix two more bugs in the exit_mmio address guessing. The first bug was that the start and step calculations were wrong since they were dividing the number of address bits instead of the address space. The second other bug was that the guessing algorithm wasn't considering the valid physical and virtual address ranges correctly for an identity map. Signed-off-by: Andrew Jones <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2018-12-21kvm: selftests: aarch64: dirty_log_test: support greater than 40-bit IPAsAndrew Jones2-5/+13
When KVM has KVM_CAP_ARM_VM_IPA_SIZE we can test with > 40-bit IPAs by using the 'type' field of KVM_CREATE_VM. Signed-off-by: Andrew Jones <[email protected]> Signed-off-by: Radim Krčmář <[email protected]>
2018-12-21kvm: selftests: add pa-48/va-48 VM modesAndrew Jones2-8/+33
Signed-off-by: Andrew Jones <[email protected]> Signed-off-by: Radim Krčmář <[email protected]>
2018-12-14kvm: selftests: ucall: improve ucall placement in memory, fix unsigned ↵Paolo Bonzini1-8/+6
comparison Based on a patch by Andrew Jones. Reported-by: Dan Carpenter <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2018-12-14KVM: selftests: implement an unchecked version of vcpu_ioctl()Vitaly Kuznetsov1-2/+12
In case we want to test failing ioctls we need an option to not fail. Following _vcpu_run() precedent implement _vcpu_ioctl(). Signed-off-by: Vitaly Kuznetsov <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2018-12-14kvm: introduce manual dirty log reprotectPaolo Bonzini1-0/+13
There are two problems with KVM_GET_DIRTY_LOG. First, and less important, it can take kvm->mmu_lock for an extended period of time. Second, its user can actually see many false positives in some cases. The latter is due to a benign race like this: 1. KVM_GET_DIRTY_LOG returns a set of dirty pages and write protects them. 2. The guest modifies the pages, causing them to be marked ditry. 3. Userspace actually copies the pages. 4. KVM_GET_DIRTY_LOG returns those pages as dirty again, even though they were not written to since (3). This is especially a problem for large guests, where the time between (1) and (3) can be substantial. This patch introduces a new capability which, when enabled, makes KVM_GET_DIRTY_LOG not write-protect the pages it returns. Instead, userspace has to explicitly clear the dirty log bits just before using the content of the page. The new KVM_CLEAR_DIRTY_LOG ioctl can also operate on a 64-page granularity rather than requiring to sync a full memslot; this way, the mmu_lock is taken for small amounts of time, and only a small amount of time will pass between write protection of pages and the sending of their content. Signed-off-by: Paolo Bonzini <[email protected]>
2018-10-28Merge tag 'linux-kselftest-4.20-rc1' of ↵Linus Torvalds1-1/+1
git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest Pull kselftest updates from Shuah Khan: "This Kselftest update for Linux 4.20-rc1 consists of: - Improvements to ftrace test suite from Masami Hiramatsu. - Color coded ftrace PASS / FAIL results from Steven Rostedt (VMware) to improve readability of reports. - watchdog Fixes and enhancement to add gettimeout and get|set pretimeout options from Jerry Hoemann. - Several fixes to warnings and spelling etc" * tag 'linux-kselftest-4.20-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest: (40 commits) selftests/ftrace: Strip escape sequences for log file selftests/ftrace: Use colored output when available selftests: fix warning: "_GNU_SOURCE" redefined selftests: kvm: Fix -Wformat warnings selftests/ftrace: Add color to the PASS / FAIL results kvm: selftests: fix spelling mistake "Insufficent" -> "Insufficient" selftests: gpio: Fix OUTPUT directory in Makefile selftests: gpio: restructure Makefile selftests: watchdog: Fix ioctl SET* error paths to take oneshot exit path selftests: watchdog: Add gettimeout and get|set pretimeout selftests: watchdog: Fix error message. selftests: watchdog: fix message when /dev/watchdog open fails selftests/ftrace: Add ftrace cpumask testcase selftests/ftrace: Add wakeup_rt tracer testcase selftests/ftrace: Add wakeup tracer testcase selftests/ftrace: Add stacktrace ftrace filter command testcase selftests/ftrace: Add trace_pipe testcase selftests/ftrace: Add function filter on module testcase selftests/ftrace: Add max stack tracer testcase selftests/ftrace: Add function profiling stat testcase ...
2018-10-24kvm: selftests: fix spelling mistake "Insufficent" -> "Insufficient"Colin Ian King1-1/+1
Trivial fix to spelling mistake in TEST_ASSERT message text Signed-off-by: Colin Ian King <[email protected]> Signed-off-by: Shuah Khan (Samsung OSG) <[email protected]>
2018-10-17KVM: selftests: add Enlightened VMCS testVitaly Kuznetsov1-12/+36
Modify test library and add eVMCS test. This includes nVMX save/restore testing. Signed-off-by: Vitaly Kuznetsov <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2018-10-17KVM: selftests: state_test: test bare VMXON migrationVitaly Kuznetsov1-0/+5
Split prepare_for_vmx_operation() into prepare_for_vmx_operation() and load_vmcs() so we can inject GUEST_SYNC() in between. Signed-off-by: Vitaly Kuznetsov <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2018-10-17kvm: selftests: stop lying to aarch64 tests about PA-bitsAndrew Jones2-0/+24
Let's add the 40 PA-bit versions of the VM modes, that AArch64 should have been using, so we can extend the dirty log test without breaking things. Signed-off-by: Andrew Jones <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2018-10-17kvm: selftests: introduce new VM mode for 64K pagesAndrew Jones4-24/+41
Rename VM_MODE_FLAT48PG to be more descriptive of its config and add a new config that has the same parameters, except with 64K pages. Signed-off-by: Andrew Jones <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2018-10-17kvm: selftests: add vcpu support for aarch64Andrew Jones1-0/+83
This code adds VM and VCPU setup code for the VM_MODE_FLAT48PG mode. The VM_MODE_FLAT48PG isn't yet fully supportable, as it defines the guest physical address limit as 52-bits, and KVM currently only supports guests with up to 40-bit physical addresses (see KVM_PHYS_SHIFT). VM_MODE_FLAT48PG will work fine, though, as long as no >= 40-bit physical addresses are used. Signed-off-by: Andrew Jones <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2018-10-17kvm: selftests: add virt mem support for aarch64Andrew Jones3-0/+218
Signed-off-by: Andrew Jones <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2018-10-17kvm: selftests: add vm_phy_pages_allocAndrew Jones1-23/+37
Signed-off-by: Andrew Jones <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2018-10-17kvm: selftests: tidy up kvm_utilAndrew Jones3-344/+361
Tidy up kvm-util code: code/comment formatting, remove unused code, and move x86 specific code out. We also move vcpu_dump() out of common code, because not all arches (AArch64) have KVM_GET_REGS. Signed-off-by: Andrew Jones <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2018-10-17kvm: selftests: move arch-specific files to arch-specific locationsAndrew Jones4-10/+10
Signed-off-by: Andrew Jones <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2018-10-17kvm: selftests: introduce ucallAndrew Jones3-14/+146
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: Andrew Jones <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2018-10-17kvm: selftests: vcpu_setup: set cr4.osfxsrAndrew Jones1-1/+1
Guest code may want to call functions that have variable arguments. To do so, we either need to compile with -mno-sse or enable SSE in the VCPUs. As it should be pretty safe to turn on the feature, and -mno-sse would make linking test code with standard libraries difficult, we choose the feature enabling. Signed-off-by: Andrew Jones <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2018-09-20kvm: selftests: Add platform_info_testDrew Schmitt1-0/+89
Test guest access to MSR_PLATFORM_INFO when the capability is enabled or disabled. Signed-off-by: Drew Schmitt <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2018-08-22kvm: selftest: add dirty logging testPeter Xu1-0/+43
Test KVM dirty logging functionality. The test creates a standalone memory slot to test tracking the dirty pages since we can't really write to the default memory slot which still contains the guest ELF image. We have two threads running during the test: (1) the vcpu thread continuously dirties random guest pages by writting a iteration number to the first 8 bytes of the page (2) the host thread continuously fetches dirty logs for the testing memory region and verify each single bit of the dirty bitmap by checking against the values written onto the page Note that since the guest cannot calls the general userspace APIs like random(), it depends on the host to provide random numbers for the page indexes to dirty. Signed-off-by: Peter Xu <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2018-08-22kvm: selftest: pass in extra memory when create vmPeter Xu1-2/+16
This information can be used to decide the size of the default memory slot, which will need to cover the extra pages with page tables. Signed-off-by: Peter Xu <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2018-08-22kvm: selftest: include the tools headersPeter Xu1-0/+1
Let the kvm selftest include the tools headers, then we can start to use things there like bitmap operations. Signed-off-by: Peter Xu <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2018-08-22kvm: selftest: unify the guest port macrosPeter Xu1-0/+14
Most of the tests are using the same way to do guest to host sync but the code is mostly duplicated. Generalize the guest port macros into the common header file and use it in different tests. Meanwhile provide "struct guest_args" and a helper "guest_args_read()" to hide the register details when playing with these port operations on RDI and RSI. Signed-off-by: Peter Xu <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
2018-08-06KVM: selftests: add tests for shadow VMCS save/restorePaolo Bonzini1-3/+30
This includes setting up the shadow VMCS and the secondary execution controls in lib/vmx.c. Signed-off-by: Paolo Bonzini <[email protected]>
2018-08-06kvm: selftests: add test for nested state save/restorePaolo Bonzini1-0/+29
Signed-off-by: Paolo Bonzini <[email protected]>
2018-08-06kvm: selftests: add basic test for state save and restorePaolo Bonzini3-22/+184
The test calls KVM_RUN repeatedly, and creates an entirely new VM with the old memory and vCPU state on every exit to userspace. The kvm_util API is expanded with two functions that manage the lifetime of a kvm_vm struct: the first closes the file descriptors and leaves the memory allocated, and the second opens the file descriptors and reuses the memory from the previous incarnation of the kvm_vm struct. For now the test is very basic, as it does not test for example XSAVE or vCPU events. However, it will test nested virtualization state starting with the next patch. Signed-off-by: Paolo Bonzini <[email protected]>
2018-08-06kvm: selftests: ensure vcpu file is releasedPaolo Bonzini1-1/+9
The selftests were not munmap-ing the kvm_run area from the vcpu file descriptor. The result was that kvm_vcpu_release was not called and a reference was left in the parent "struct kvm". Ultimately this was visible in the upcoming state save/restore test as an error when KVM attempted to create a duplicate debugfs entry. Signed-off-by: Paolo Bonzini <[email protected]>
2018-08-06kvm: selftests: actually use all of lib/vmx.cPaolo Bonzini1-29/+42
The allocation of the VMXON and VMCS is currently done twice, in lib/vmx.c and in vmx_tsc_adjust_test.c. Reorganize the code to provide a cleaner and easier to use API to the tests. lib/vmx.c now does the complete setup of the VMX data structures, but does not create the VM or set CPUID. This has to be done by the caller. Signed-off-by: Paolo Bonzini <[email protected]>
2018-08-06kvm: selftests: create a GDT and TSSPaolo Bonzini3-44/+76
The GDT and the TSS base were left to zero, and this has interesting effects when the TSS descriptor is later read to set up a VMCS's TR_BASE. Basically it worked by chance, and this patch fixes it by setting up all the protected mode data structures properly. Because the GDT and TSS addresses are virtual, the page tables now always exist at the time of vcpu setup. Signed-off-by: Paolo Bonzini <[email protected]>