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 #include <fcntl.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/ioctl.h>
17
18 #include "test_util.h"
19 #include "kvm_util.h"
20 #include "processor.h"
21
22 #define MSR_PLATFORM_INFO_MAX_TURBO_RATIO 0xff00
23
guest_code(void)24 static void guest_code(void)
25 {
26 uint64_t msr_platform_info;
27 uint8_t vector;
28
29 GUEST_SYNC(true);
30 msr_platform_info = rdmsr(MSR_PLATFORM_INFO);
31 GUEST_ASSERT_EQ(msr_platform_info & MSR_PLATFORM_INFO_MAX_TURBO_RATIO,
32 MSR_PLATFORM_INFO_MAX_TURBO_RATIO);
33
34 GUEST_SYNC(false);
35 vector = rdmsr_safe(MSR_PLATFORM_INFO, &msr_platform_info);
36 GUEST_ASSERT_EQ(vector, GP_VECTOR);
37
38 GUEST_DONE();
39 }
40
main(int argc,char * argv[])41 int main(int argc, char *argv[])
42 {
43 struct kvm_vcpu *vcpu;
44 struct kvm_vm *vm;
45 uint64_t msr_platform_info;
46 struct ucall uc;
47
48 TEST_REQUIRE(kvm_has_cap(KVM_CAP_MSR_PLATFORM_INFO));
49
50 vm = vm_create_with_one_vcpu(&vcpu, guest_code);
51
52 msr_platform_info = vcpu_get_msr(vcpu, MSR_PLATFORM_INFO);
53 vcpu_set_msr(vcpu, MSR_PLATFORM_INFO,
54 msr_platform_info | MSR_PLATFORM_INFO_MAX_TURBO_RATIO);
55
56 for (;;) {
57 vcpu_run(vcpu);
58 TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
59
60 switch (get_ucall(vcpu, &uc)) {
61 case UCALL_SYNC:
62 vm_enable_cap(vm, KVM_CAP_MSR_PLATFORM_INFO, uc.args[1]);
63 break;
64 case UCALL_DONE:
65 goto done;
66 case UCALL_ABORT:
67 REPORT_GUEST_ASSERT(uc);
68 default:
69 TEST_FAIL("Unexpected ucall %lu", uc.cmd);
70 break;
71 }
72 }
73
74 done:
75 vcpu_set_msr(vcpu, MSR_PLATFORM_INFO, msr_platform_info);
76
77 kvm_vm_free(vm);
78
79 return 0;
80 }
81