Ensure that IA32_XSS appears in KVM_GET_MSR_INDEX_LIST if it can be set to a non-zero value.
Unified · Split
| @@ -13,6 +13,7 @@ · 12 unmodified lines | ||
| 13 | 13 | /x86_64/vmx_dirty_log_test |
| 14 | 14 | /x86_64/vmx_set_nested_state_test |
| 15 | 15 | /x86_64/vmx_tsc_adjust_test |
| 16 | +/x86_64/xss_msr_test | |
| 16 | 17 | /clear_dirty_log_test |
| 17 | 18 | /dirty_log_test |
| 18 | 19 | /kvm_create_max_vcpus |
| @@ -25,6 +25,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/vmx_close_while_nested_test · 24 unmodified lines | ||
| 25 | 25 | TEST_GEN_PROGS_x86_64 += x86_64/vmx_dirty_log_test |
| 26 | 26 | TEST_GEN_PROGS_x86_64 += x86_64/vmx_set_nested_state_test |
| 27 | 27 | TEST_GEN_PROGS_x86_64 += x86_64/vmx_tsc_adjust_test |
| 28 | +TEST_GEN_PROGS_x86_64 += x86_64/xss_msr_test | |
| 28 | 29 | TEST_GEN_PROGS_x86_64 += clear_dirty_log_test |
| 29 | 30 | TEST_GEN_PROGS_x86_64 += dirty_log_test |
| 30 | 31 | TEST_GEN_PROGS_x86_64 += kvm_create_max_vcpus |
| @@ -308,6 +308,8 @@ struct kvm_x86_state *vcpu_save_state(struct kvm_vm *vm, uint32_t vcpuid); · 307 unmodified lines | ||
| 308 | 308 | void vcpu_load_state(struct kvm_vm *vm, uint32_t vcpuid, |
| 309 | 309 | struct kvm_x86_state *state); |
| 310 | 310 | |
| 311 | +struct kvm_msr_list *kvm_get_msr_index_list(void); | |
| 312 | + | |
| 311 | 313 | struct kvm_cpuid2 *kvm_get_supported_cpuid(void); |
| 312 | 314 | void vcpu_set_cpuid(struct kvm_vm *vm, uint32_t vcpuid, |
| 313 | 315 | struct kvm_cpuid2 *cpuid); |
| @@ -322,10 +324,13 @@ kvm_get_supported_cpuid_entry(uint32_t function) · 8 unmodified lines | ||
| 322 | 324 | } |
| 323 | 325 | |
| 324 | 326 | 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); | |
| 325 | 329 | void vcpu_set_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index, |
| 326 | 330 | uint64_t msr_value); |
| 327 | 331 | |
| 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); | |
| 329 | 334 | void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits); |
| 330 | 335 | |
| 331 | 336 | /* |
| @@ -869,7 +869,7 @@ uint64_t vcpu_get_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index) · 868 unmodified lines | ||
| 869 | 869 | return buffer.entry.data; |
| 870 | 870 | } |
| 871 | 871 | |
| 872 | −/* VCPU Set MSR | |
| 872 | +/* _VCPU Set MSR | |
| 873 | 873 | * |
| 874 | 874 | * Input Args: |
| 875 | 875 | * 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 | ||
| 879 | 879 | * |
| 880 | 880 | * Output Args: None |
| 881 | 881 | * |
| 882 | − * Return: On success, nothing. On failure a TEST_ASSERT is produced. | |
| 882 | + * Return: The result of KVM_SET_MSRS. | |
| 883 | 883 | * |
| 884 | − * Set value of MSR for VCPU. | |
| 884 | + * Sets the value of an MSR for the given VCPU. | |
| 885 | 885 | */ |
| 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) | |
| 888 | 888 | { |
| 889 | 889 | struct vcpu *vcpu = vcpu_find(vm, vcpuid); |
| 890 | 890 | struct { |
| @@ -899,6 +899,29 @@ void vcpu_set_msr(struct kvm_vm *vm, uint32_t vcpuid, uint64_t msr_index, · 8 unmodified lines | ||
| 899 | 899 | buffer.entry.index = msr_index; |
| 900 | 900 | buffer.entry.data = msr_value; |
| 901 | 901 | 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); | |
| 902 | 925 | TEST_ASSERT(r == 1, "KVM_SET_MSRS IOCTL failed,\n" |
| 903 | 926 | " rc: %i errno: %i", r, errno); |
| 904 | 927 | } |
| @@ -1000,19 +1023,45 @@ struct kvm_x86_state { · 95 unmodified lines | ||
| 1000 | 1023 | struct kvm_msrs msrs; |
| 1001 | 1024 | }; |
| 1002 | 1025 | |
| 1003 | −static int kvm_get_num_msrs(struct kvm_vm *vm) | |
| 1026 | +static int kvm_get_num_msrs_fd(int kvm_fd) | |
| 1004 | 1027 | { |
| 1005 | 1028 | struct kvm_msr_list nmsrs; |
| 1006 | 1029 | int r; |
| 1007 | 1030 | |
| 1008 | 1031 | 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); | |
| 1010 | 1033 | TEST_ASSERT(r == -1 && errno == E2BIG, "Unexpected result from KVM_GET_MSR_INDEX_LIST probe, r: %i", |
| 1011 | 1034 | r); |
| 1012 | 1035 | |
| 1013 | 1036 | return nmsrs.nmsrs; |
| 1014 | 1037 | } |
| 1015 | 1038 | |
| 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 | + | |
| 1016 | 1065 | struct kvm_x86_state *vcpu_save_state(struct kvm_vm *vm, uint32_t vcpuid) |
| 1017 | 1066 | { |
| 1018 | 1067 | struct vcpu *vcpu = vcpu_find(vm, vcpuid); |
| @@ -1158,7 +1207,12 @@ bool is_intel_cpu(void) · 139 unmodified lines | ||
| 1158 | 1207 | return (ebx == chunk[0] && edx == chunk[1] && ecx == chunk[2]); |
| 1159 | 1208 | } |
| 1160 | 1209 | |
| 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) | |
| 1162 | 1216 | { |
| 1163 | 1217 | return kvm_get_supported_cpuid_entry(0x80000000)->eax; |
| 1164 | 1218 | } |
| @@ -1169,7 +1223,7 @@ void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits) · 4 unmodified lines | ||
| 1169 | 1223 | bool pae; |
| 1170 | 1224 | |
| 1171 | 1225 | /* SDM 4.1.4 */ |
| 1172 | − if (kvm_get_cpuid_max() < 0x80000008) { | |
| 1226 | + if (kvm_get_cpuid_max_extended() < 0x80000008) { | |
| 1173 | 1227 | pae = kvm_get_supported_cpuid_entry(1)->edx & (1 << 6); |
| 1174 | 1228 | *pa_bits = pae ? 36 : 32; |
| 1175 | 1229 | *va_bits = 32; |
| @@ -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 | +} | |