aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorGreg Kroah-Hartman <gregkh@linuxfoundation.org>2018-09-21 10:21:42 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2018-09-21 10:21:42 -0400
commita27fb6d983c7b5bb0129ae4d7a7c81758173bfab (patch)
treea13e1dd4eb1a60d54bc2c14853049c6a280333af /tools
parent0eba8697bce15dc06e2b5c4c66d672c37ca43be0 (diff)
parent26b471c7e2f7befd0f59c35b257749ca57e0ed70 (diff)
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
Paolo writes: "It's mostly small bugfixes and cleanups, mostly around x86 nested virtualization. One important change, not related to nested virtualization, is that the ability for the guest kernel to trap CPUID instructions (in Linux that's the ARCH_SET_CPUID arch_prctl) is now masked by default. This is because the feature is detected through an MSR; a very bad idea that Intel seems to like more and more. Some applications choke if the other fields of that MSR are not initialized as on real hardware, hence we have to disable the whole MSR by default, as was the case before Linux 4.12." * tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm: (23 commits) KVM: nVMX: Fix bad cleanup on error of get/set nested state IOCTLs kvm: selftests: Add platform_info_test KVM: x86: Control guest reads of MSR_PLATFORM_INFO KVM: x86: Turbo bits in MSR_PLATFORM_INFO nVMX x86: Check VPID value on vmentry of L2 guests nVMX x86: check posted-interrupt descriptor addresss on vmentry of L2 KVM: nVMX: Wake blocked vCPU in guest-mode if pending interrupt in virtual APICv KVM: VMX: check nested state and CR4.VMXE against SMM kvm: x86: make kvm_{load|put}_guest_fpu() static x86/hyper-v: rename ipi_arg_{ex,non_ex} structures KVM: VMX: use preemption timer to force immediate VMExit KVM: VMX: modify preemption timer bit only when arming timer KVM: VMX: immediately mark preemption timer expired only for zero value KVM: SVM: Switch to bitmap_zalloc() KVM/MMU: Fix comment in walk_shadow_page_lockless_end() kvm: selftests: use -pthread instead of -lpthread KVM: x86: don't reset root in kvm_mmu_setup() kvm: mmu: Don't read PDPTEs when paging is not enabled x86/kvm/lapic: always disable MMIO interface in x2APIC mode KVM: s390: Make huge pages unavailable in ucontrol VMs ...
Diffstat (limited to 'tools')
-rw-r--r--tools/testing/selftests/kvm/.gitignore1
-rw-r--r--tools/testing/selftests/kvm/Makefile5
-rw-r--r--tools/testing/selftests/kvm/include/kvm_util.h4
-rw-r--r--tools/testing/selftests/kvm/lib/kvm_util.c89
-rw-r--r--tools/testing/selftests/kvm/platform_info_test.c110
5 files changed, 207 insertions, 2 deletions
diff --git a/tools/testing/selftests/kvm/.gitignore b/tools/testing/selftests/kvm/.gitignore
index 4202139d81d9..5c34752e1cff 100644
--- a/tools/testing/selftests/kvm/.gitignore
+++ b/tools/testing/selftests/kvm/.gitignore
@@ -1,4 +1,5 @@
1cr4_cpuid_sync_test 1cr4_cpuid_sync_test
2platform_info_test
2set_sregs_test 3set_sregs_test
3sync_regs_test 4sync_regs_test
4vmx_tsc_adjust_test 5vmx_tsc_adjust_test
diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile
index 87d1a8488af8..ec32dad3c3f0 100644
--- a/tools/testing/selftests/kvm/Makefile
+++ b/tools/testing/selftests/kvm/Makefile
@@ -6,7 +6,8 @@ UNAME_M := $(shell uname -m)
6LIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c lib/sparsebit.c 6LIBKVM = lib/assert.c lib/elf.c lib/io.c lib/kvm_util.c lib/sparsebit.c
7LIBKVM_x86_64 = lib/x86.c lib/vmx.c 7LIBKVM_x86_64 = lib/x86.c lib/vmx.c
8 8
9TEST_GEN_PROGS_x86_64 = set_sregs_test 9TEST_GEN_PROGS_x86_64 = platform_info_test
10TEST_GEN_PROGS_x86_64 += set_sregs_test
10TEST_GEN_PROGS_x86_64 += sync_regs_test 11TEST_GEN_PROGS_x86_64 += sync_regs_test
11TEST_GEN_PROGS_x86_64 += vmx_tsc_adjust_test 12TEST_GEN_PROGS_x86_64 += vmx_tsc_adjust_test
12TEST_GEN_PROGS_x86_64 += cr4_cpuid_sync_test 13TEST_GEN_PROGS_x86_64 += cr4_cpuid_sync_test
@@ -20,7 +21,7 @@ INSTALL_HDR_PATH = $(top_srcdir)/usr
20LINUX_HDR_PATH = $(INSTALL_HDR_PATH)/include/ 21LINUX_HDR_PATH = $(INSTALL_HDR_PATH)/include/
21LINUX_TOOL_INCLUDE = $(top_srcdir)tools/include 22LINUX_TOOL_INCLUDE = $(top_srcdir)tools/include
22CFLAGS += -O2 -g -std=gnu99 -I$(LINUX_TOOL_INCLUDE) -I$(LINUX_HDR_PATH) -Iinclude -I$(<D) -I.. 23CFLAGS += -O2 -g -std=gnu99 -I$(LINUX_TOOL_INCLUDE) -I$(LINUX_HDR_PATH) -Iinclude -I$(<D) -I..
23LDFLAGS += -lpthread 24LDFLAGS += -pthread
24 25
25# After inclusion, $(OUTPUT) is defined and 26# After inclusion, $(OUTPUT) is defined and
26# $(TEST_GEN_PROGS) starts with $(OUTPUT)/ 27# $(TEST_GEN_PROGS) starts with $(OUTPUT)/
diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
index bb5a25fb82c6..3acf9a91704c 100644
--- a/tools/testing/selftests/kvm/include/kvm_util.h
+++ b/tools/testing/selftests/kvm/include/kvm_util.h
@@ -50,6 +50,7 @@ enum vm_mem_backing_src_type {
50}; 50};
51 51
52int kvm_check_cap(long cap); 52int kvm_check_cap(long cap);
53int vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap);
53 54
54struct kvm_vm *vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm); 55struct kvm_vm *vm_create(enum vm_guest_mode mode, uint64_t phy_pages, int perm);
55void kvm_vm_free(struct kvm_vm *vmp); 56void kvm_vm_free(struct kvm_vm *vmp);
@@ -108,6 +109,9 @@ void vcpu_events_get(struct kvm_vm *vm, uint32_t vcpuid,
108 struct kvm_vcpu_events *events); 109 struct kvm_vcpu_events *events);
109void vcpu_events_set(struct kvm_vm *vm, uint32_t vcpuid, 110void vcpu_events_set(struct kvm_vm *vm, uint32_t vcpuid,
110 struct kvm_vcpu_events *events); 111 struct kvm_vcpu_events *events);
112uint64_t vcpu_get_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index);
113void vcpu_set_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index,
114 uint64_t msr_value);
111 115
112const char *exit_reason_str(unsigned int exit_reason); 116const char *exit_reason_str(unsigned int exit_reason);
113 117
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index e9ba389c48db..6fd8c089cafc 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -63,6 +63,29 @@ int kvm_check_cap(long cap)
63 return ret; 63 return ret;
64} 64}
65 65
66/* VM Enable Capability
67 *
68 * Input Args:
69 * vm - Virtual Machine
70 * cap - Capability
71 *
72 * Output Args: None
73 *
74 * Return: On success, 0. On failure a TEST_ASSERT failure is produced.
75 *
76 * Enables a capability (KVM_CAP_*) on the VM.
77 */
78int vm_enable_cap(struct kvm_vm *vm, struct kvm_enable_cap *cap)
79{
80 int ret;
81
82 ret = ioctl(vm->fd, KVM_ENABLE_CAP, cap);
83 TEST_ASSERT(ret == 0, "KVM_ENABLE_CAP IOCTL failed,\n"
84 " rc: %i errno: %i", ret, errno);
85
86 return ret;
87}
88
66static void vm_open(struct kvm_vm *vm, int perm) 89static void vm_open(struct kvm_vm *vm, int perm)
67{ 90{
68 vm->kvm_fd = open(KVM_DEV_PATH, perm); 91 vm->kvm_fd = open(KVM_DEV_PATH, perm);
@@ -1220,6 +1243,72 @@ void vcpu_events_set(struct kvm_vm *vm, uint32_t vcpuid,
1220 ret, errno); 1243 ret, errno);
1221} 1244}
1222 1245
1246/* VCPU Get MSR
1247 *
1248 * Input Args:
1249 * vm - Virtual Machine
1250 * vcpuid - VCPU ID
1251 * msr_index - Index of MSR
1252 *
1253 * Output Args: None
1254 *
1255 * Return: On success, value of the MSR. On failure a TEST_ASSERT is produced.
1256 *
1257 * Get value of MSR for VCPU.
1258 */
1259uint64_t vcpu_get_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index)
1260{
1261 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1262 struct {
1263 struct kvm_msrs header;
1264 struct kvm_msr_entry entry;
1265 } buffer = {};
1266 int r;
1267
1268 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1269 buffer.header.nmsrs = 1;
1270 buffer.entry.index = msr_index;
1271 r = ioctl(vcpu->fd, KVM_GET_MSRS, &buffer.header);
1272 TEST_ASSERT(r == 1, "KVM_GET_MSRS IOCTL failed,\n"
1273 " rc: %i errno: %i", r, errno);
1274
1275 return buffer.entry.data;
1276}
1277
1278/* VCPU Set MSR
1279 *
1280 * Input Args:
1281 * vm - Virtual Machine
1282 * vcpuid - VCPU ID
1283 * msr_index - Index of MSR
1284 * msr_value - New value of MSR
1285 *
1286 * Output Args: None
1287 *
1288 * Return: On success, nothing. On failure a TEST_ASSERT is produced.
1289 *
1290 * Set value of MSR for VCPU.
1291 */
1292void vcpu_set_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index,
1293 uint64_t msr_value)
1294{
1295 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
1296 struct {
1297 struct kvm_msrs header;
1298 struct kvm_msr_entry entry;
1299 } buffer = {};
1300 int r;
1301
1302 TEST_ASSERT(vcpu != NULL, "vcpu not found, vcpuid: %u", vcpuid);
1303 memset(&buffer, 0, sizeof(buffer));
1304 buffer.header.nmsrs = 1;
1305 buffer.entry.index = msr_index;
1306 buffer.entry.data = msr_value;
1307 r = ioctl(vcpu->fd, KVM_SET_MSRS, &buffer.header);
1308 TEST_ASSERT(r == 1, "KVM_SET_MSRS IOCTL failed,\n"
1309 " rc: %i errno: %i", r, errno);
1310}
1311
1223/* VM VCPU Args Set 1312/* VM VCPU Args Set
1224 * 1313 *
1225 * Input Args: 1314 * Input Args:
diff --git a/tools/testing/selftests/kvm/platform_info_test.c b/tools/testing/selftests/kvm/platform_info_test.c
new file mode 100644
index 000000000000..3764e7121265
--- /dev/null
+++ b/tools/testing/selftests/kvm/platform_info_test.c
@@ -0,0 +1,110 @@
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Test for x86 KVM_CAP_MSR_PLATFORM_INFO
4 *
5 * Copyright (C) 2018, Google LLC.
6 *
7 * This work is licensed under the terms of the GNU GPL, version 2.
8 *
9 * Verifies expected behavior of controlling guest access to
10 * MSR_PLATFORM_INFO.
11 */
12
13#define _GNU_SOURCE /* for program_invocation_short_name */
14#include <fcntl.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <sys/ioctl.h>
19
20#include "test_util.h"
21#include "kvm_util.h"
22#include "x86.h"
23
24#define VCPU_ID 0
25#define MSR_PLATFORM_INFO_MAX_TURBO_RATIO 0xff00
26
27static void guest_code(void)
28{
29 uint64_t msr_platform_info;
30
31 for (;;) {
32 msr_platform_info = rdmsr(MSR_PLATFORM_INFO);
33 GUEST_SYNC(msr_platform_info);
34 asm volatile ("inc %r11");
35 }
36}
37
38static void set_msr_platform_info_enabled(struct kvm_vm *vm, bool enable)
39{
40 struct kvm_enable_cap cap = {};
41
42 cap.cap = KVM_CAP_MSR_PLATFORM_INFO;
43 cap.flags = 0;
44 cap.args[0] = (int)enable;
45 vm_enable_cap(vm, &cap);
46}
47
48static void test_msr_platform_info_enabled(struct kvm_vm *vm)
49{
50 struct kvm_run *run = vcpu_state(vm, VCPU_ID);
51 struct guest_args args;
52
53 set_msr_platform_info_enabled(vm, true);
54 vcpu_run(vm, VCPU_ID);
55 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
56 "Exit_reason other than KVM_EXIT_IO: %u (%s),\n",
57 run->exit_reason,
58 exit_reason_str(run->exit_reason));
59 guest_args_read(vm, VCPU_ID, &args);
60 TEST_ASSERT(args.port == GUEST_PORT_SYNC,
61 "Received IO from port other than PORT_HOST_SYNC: %u\n",
62 run->io.port);
63 TEST_ASSERT((args.arg1 & MSR_PLATFORM_INFO_MAX_TURBO_RATIO) ==
64 MSR_PLATFORM_INFO_MAX_TURBO_RATIO,
65 "Expected MSR_PLATFORM_INFO to have max turbo ratio mask: %i.",
66 MSR_PLATFORM_INFO_MAX_TURBO_RATIO);
67}
68
69static void test_msr_platform_info_disabled(struct kvm_vm *vm)
70{
71 struct kvm_run *run = vcpu_state(vm, VCPU_ID);
72
73 set_msr_platform_info_enabled(vm, false);
74 vcpu_run(vm, VCPU_ID);
75 TEST_ASSERT(run->exit_reason == KVM_EXIT_SHUTDOWN,
76 "Exit_reason other than KVM_EXIT_SHUTDOWN: %u (%s)\n",
77 run->exit_reason,
78 exit_reason_str(run->exit_reason));
79}
80
81int main(int argc, char *argv[])
82{
83 struct kvm_vm *vm;
84 struct kvm_run *state;
85 int rv;
86 uint64_t msr_platform_info;
87
88 /* Tell stdout not to buffer its content */
89 setbuf(stdout, NULL);
90
91 rv = kvm_check_cap(KVM_CAP_MSR_PLATFORM_INFO);
92 if (!rv) {
93 fprintf(stderr,
94 "KVM_CAP_MSR_PLATFORM_INFO not supported, skip test\n");
95 exit(KSFT_SKIP);
96 }
97
98 vm = vm_create_default(VCPU_ID, 0, guest_code);
99
100 msr_platform_info = vcpu_get_msr(vm, VCPU_ID, MSR_PLATFORM_INFO);
101 vcpu_set_msr(vm, VCPU_ID, MSR_PLATFORM_INFO,
102 msr_platform_info | MSR_PLATFORM_INFO_MAX_TURBO_RATIO);
103 test_msr_platform_info_disabled(vm);
104 test_msr_platform_info_enabled(vm);
105 vcpu_set_msr(vm, VCPU_ID, MSR_PLATFORM_INFO, msr_platform_info);
106
107 kvm_vm_free(vm);
108
109 return 0;
110}