History

kvm: tests: Add test to verify MSR_IA32_XSS

Ensure that IA32_XSS appears in KVM_GET_MSR_INDEX_LIST if it can be set to a non-zero value.

Aaron Lewis committed 7 years ago

Commit c90992b1 parent 52297435 files · +147 −10Browse files

Unified · Split

5 files
  1. tools/testing/selftests/kvm/.gitignore +1 −0
  2. tools/testing/selftests/kvm/Makefile +1 −0
  3. tools/testing/selftests/kvm/include/x86_64/processor.h +6 −1
  4. tools/testing/selftests/kvm/lib/x86_64/processor.c +63 −9
  5. tools/testing/selftests/kvm/x86_64/xss_msr_test.c +76 −0
tools/testing/selftests/kvm/.gitignore+1−0
@@ -13,6 +13,7 @@ · 12 unmodified lines
1313 /x86_64/vmx_dirty_log_test
1414 /x86_64/vmx_set_nested_state_test
1515 /x86_64/vmx_tsc_adjust_test
16+/x86_64/xss_msr_test
1617 /clear_dirty_log_test
1718 /dirty_log_test
1819 /kvm_create_max_vcpus
tools/testing/selftests/kvm/Makefile+1−0
@@ -25,6 +25,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/vmx_close_while_nested_test · 24 unmodified lines
2525 TEST_GEN_PROGS_x86_64 += x86_64/vmx_dirty_log_test
2626 TEST_GEN_PROGS_x86_64 += x86_64/vmx_set_nested_state_test
2727 TEST_GEN_PROGS_x86_64 += x86_64/vmx_tsc_adjust_test
28+TEST_GEN_PROGS_x86_64 += x86_64/xss_msr_test
2829 TEST_GEN_PROGS_x86_64 += clear_dirty_log_test
2930 TEST_GEN_PROGS_x86_64 += dirty_log_test
3031 TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus
tools/testing/selftests/kvm/include/x86_64/processor.h+6−1
@@ -308,6 +308,8 @@ struct kvm_x86_state *vcpu_save_state(struct kvm_vm *vm, uint32_t vcpuid); · 307 unmodified lines
308308 void vcpu_load_state(struct kvm_vm *vm, uint32_t vcpuid,
309309 struct kvm_x86_state *state);
310310
311+struct kvm_msr_list *kvm_get_msr_index_list(void);
312+
311313 struct kvm_cpuid2 *kvm_get_supported_cpuid(void);
312314 void vcpu_set_cpuid(struct kvm_vm *vm, uint32_t vcpuid,
313315 struct kvm_cpuid2 *cpuid);
@@ -322,10 +324,13 @@ kvm_get_supported_cpuid_entry(uint32_t function) · 8 unmodified lines
322324 }
323325
324326 uint64_t vcpu_get_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index);
327+int _vcpu_set_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index,
328+ uint64_t msr_value);
325329 void vcpu_set_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index,
326330 uint64_t msr_value);
327331
328−uint32_t kvm_get_cpuid_max(void);
332+uint32_t kvm_get_cpuid_max_basic(void);
333+uint32_t kvm_get_cpuid_max_extended(void);
329334 void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits);
330335
331336 /*
tools/testing/selftests/kvm/lib/x86_64/processor.c+63−9
@@ -869,7 +869,7 @@ uint64_t vcpu_get_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index) · 868 unmodified lines
869869 return buffer.entry.data;
870870 }
871871
872−/* VCPU Set MSR
872+/* _VCPU Set MSR
873873 *
874874 * Input Args:
875875 * vm - Virtual Machine
@@ -879,12 +879,12 @@ uint64_t vcpu_get_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index) · 3 unmodified lines
879879 *
880880 * Output Args: None
881881 *
882− * Return: On success, nothing. On failure a TEST_ASSERT is produced.
882+ * Return: The result of KVM_SET_MSRS.
883883 *
884− * Set value of MSR for VCPU.
884+ * Sets the value of an MSR for the given VCPU.
885885 */
886−void vcpu_set_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index,
887− uint64_t msr_value)
886+int _vcpu_set_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index,
887+ uint64_t msr_value)
888888 {
889889 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
890890 struct {
@@ -899,6 +899,29 @@ void vcpu_set_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index, · 8 unmodified lines
899899 buffer.entry.index = msr_index;
900900 buffer.entry.data = msr_value;
901901 r = ioctl(vcpu->fd, KVM_SET_MSRS, &buffer.header);
902+ return r;
903+}
904+
905+/* VCPU Set MSR
906+ *
907+ * Input Args:
908+ * vm - Virtual Machine
909+ * vcpuid - VCPU ID
910+ * msr_index - Index of MSR
911+ * msr_value - New value of MSR
912+ *
913+ * Output Args: None
914+ *
915+ * Return: On success, nothing. On failure a TEST_ASSERT is produced.
916+ *
917+ * Set value of MSR for VCPU.
918+ */
919+void vcpu_set_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index,
920+ uint64_t msr_value)
921+{
922+ int r;
923+
924+ r = _vcpu_set_msr(vm, vcpuid, msr_index, msr_value);
902925 TEST_ASSERT(r == 1, "KVM_SET_MSRS IOCTL failed,\n"
903926 " rc: %i errno: %i", r, errno);
904927 }
@@ -1000,19 +1023,45 @@ struct kvm_x86_state { · 95 unmodified lines
10001023 struct kvm_msrs msrs;
10011024 };
10021025
1003−static int kvm_get_num_msrs(struct kvm_vm *vm)
1026+static int kvm_get_num_msrs_fd(int kvm_fd)
10041027 {
10051028 struct kvm_msr_list nmsrs;
10061029 int r;
10071030
10081031 nmsrs.nmsrs = 0;
1009− r = ioctl(vm->kvm_fd, KVM_GET_MSR_INDEX_LIST, &nmsrs);
1032+ r = ioctl(kvm_fd, KVM_GET_MSR_INDEX_LIST, &nmsrs);
10101033 TEST_ASSERT(r == -1 && errno == E2BIG, "Unexpected result from KVM_GET_MSR_INDEX_LIST probe, r: %i",
10111034 r);
10121035
10131036 return nmsrs.nmsrs;
10141037 }
10151038
1039+static int kvm_get_num_msrs(struct kvm_vm *vm)
1040+{
1041+ return kvm_get_num_msrs_fd(vm->kvm_fd);
1042+}
1043+
1044+struct kvm_msr_list *kvm_get_msr_index_list(void)
1045+{
1046+ struct kvm_msr_list *list;
1047+ int nmsrs, r, kvm_fd;
1048+
1049+ kvm_fd = open(KVM_DEV_PATH, O_RDONLY);
1050+ if (kvm_fd < 0)
1051+ exit(KSFT_SKIP);
1052+
1053+ nmsrs = kvm_get_num_msrs_fd(kvm_fd);
1054+ list = malloc(sizeof(*list) + nmsrs * sizeof(list->indices[0]));
1055+ list->nmsrs = nmsrs;
1056+ r = ioctl(kvm_fd, KVM_GET_MSR_INDEX_LIST, list);
1057+ close(kvm_fd);
1058+
1059+ TEST_ASSERT(r == 0, "Unexpected result from KVM_GET_MSR_INDEX_LIST, r: %i",
1060+ r);
1061+
1062+ return list;
1063+}
1064+
10161065 struct kvm_x86_state *vcpu_save_state(struct kvm_vm *vm, uint32_t vcpuid)
10171066 {
10181067 struct vcpu *vcpu = vcpu_find(vm, vcpuid);
@@ -1158,7 +1207,12 @@ bool is_intel_cpu(void) · 139 unmodified lines
11581207 return (ebx == chunk[0] && edx == chunk[1] && ecx == chunk[2]);
11591208 }
11601209
1161−uint32_t kvm_get_cpuid_max(void)
1210+uint32_t kvm_get_cpuid_max_basic(void)
1211+{
1212+ return kvm_get_supported_cpuid_entry(0)->eax;
1213+}
1214+
1215+uint32_t kvm_get_cpuid_max_extended(void)
11621216 {
11631217 return kvm_get_supported_cpuid_entry(0x80000000)->eax;
11641218 }
@@ -1169,7 +1223,7 @@ void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits) · 4 unmodified lines
11691223 bool pae;
11701224
11711225 /* SDM 4.1.4 */
1172− if (kvm_get_cpuid_max() < 0x80000008) {
1226+ if (kvm_get_cpuid_max_extended() < 0x80000008) {
11731227 pae = kvm_get_supported_cpuid_entry(1)->edx & (1 << 6);
11741228 *pa_bits = pae ? 36 : 32;
11751229 *va_bits = 32;
tools/testing/selftests/kvm/x86_64/xss_msr_test.c+76−0
@@ -0,0 +1,76 @@
1+// SPDX-License-Identifier: GPL-2.0
2+/*
3+ * Copyright (C) 2019, Google LLC.
4+ *
5+ * Tests for the IA32_XSS MSR.
6+ */
7+
8+#define _GNU_SOURCE /* for program_invocation_short_name */
9+#include <sys/ioctl.h>
10+
11+#include "test_util.h"
12+#include "kvm_util.h"
13+#include "vmx.h"
14+
15+#define VCPU_ID 1
16+#define MSR_BITS 64
17+
18+#define X86_FEATURE_XSAVES (1<<3)
19+
20+bool is_supported_msr(u32 msr_index)
21+{
22+ struct kvm_msr_list *list;
23+ bool found = false;
24+ int i;
25+
26+ list = kvm_get_msr_index_list();
27+ for (i = 0; i < list->nmsrs; ++i) {
28+ if (list->indices[i] == msr_index) {
29+ found = true;
30+ break;
31+ }
32+ }
33+
34+ free(list);
35+ return found;
36+}
37+
38+int main(int argc, char *argv[])
39+{
40+ struct kvm_cpuid_entry2 *entry;
41+ bool xss_supported = false;
42+ struct kvm_vm *vm;
43+ uint64_t xss_val;
44+ int i, r;
45+
46+ /* Create VM */
47+ vm = vm_create_default(VCPU_ID, 0, 0);
48+
49+ if (kvm_get_cpuid_max_basic() >= 0xd) {
50+ entry = kvm_get_supported_cpuid_index(0xd, 1);
51+ xss_supported = entry && !!(entry->eax & X86_FEATURE_XSAVES);
52+ }
53+ if (!xss_supported) {
54+ printf("IA32_XSS is not supported by the vCPU.\n");
55+ exit(KSFT_SKIP);
56+ }
57+
58+ xss_val = vcpu_get_msr(vm, VCPU_ID, MSR_IA32_XSS);
59+ TEST_ASSERT(xss_val == 0,
60+ "MSR_IA32_XSS should be initialized to zero\n");
61+
62+ vcpu_set_msr(vm, VCPU_ID, MSR_IA32_XSS, xss_val);
63+ /*
64+ * At present, KVM only supports a guest IA32_XSS value of 0. Verify
65+ * that trying to set the guest IA32_XSS to an unsupported value fails.
66+ * Also, in the future when a non-zero value succeeds check that
67+ * IA32_XSS is in the KVM_GET_MSR_INDEX_LIST.
68+ */
69+ for (i = 0; i < MSR_BITS; ++i) {
70+ r = _vcpu_set_msr(vm, VCPU_ID, MSR_IA32_XSS, 1ull << i);
71+ TEST_ASSERT(r == 0 || is_supported_msr(MSR_IA32_XSS),
72+ "IA32_XSS was able to be set, but was not found in KVM_GET_MSR_INDEX_LIST.\n");
73+ }
74+
75+ kvm_vm_free(vm);
76+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t