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 "processor.h"
23
24 #define MSR_PLATFORM_INFO_MAX_TURBO_RATIO 0xff00
25
guest_code(void)26 static void guest_code(void)
27 {
28 uint64_t msr_platform_info;
29
30 for (;;) {
31 msr_platform_info = rdmsr(MSR_PLATFORM_INFO);
32 GUEST_SYNC(msr_platform_info);
33 asm volatile ("inc %r11");
34 }
35 }
36
test_msr_platform_info_enabled(struct kvm_vcpu * vcpu)37 static void test_msr_platform_info_enabled(struct kvm_vcpu *vcpu)
38 {
39 struct ucall uc;
40
41 vm_enable_cap(vcpu->vm, KVM_CAP_MSR_PLATFORM_INFO, true);
42 vcpu_run(vcpu);
43 TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
44
45 get_ucall(vcpu, &uc);
46 TEST_ASSERT(uc.cmd == UCALL_SYNC,
47 "Received ucall other than UCALL_SYNC: %lu\n", uc.cmd);
48 TEST_ASSERT((uc.args[1] & MSR_PLATFORM_INFO_MAX_TURBO_RATIO) ==
49 MSR_PLATFORM_INFO_MAX_TURBO_RATIO,
50 "Expected MSR_PLATFORM_INFO to have max turbo ratio mask: %i.",
51 MSR_PLATFORM_INFO_MAX_TURBO_RATIO);
52 }
53
test_msr_platform_info_disabled(struct kvm_vcpu * vcpu)54 static void test_msr_platform_info_disabled(struct kvm_vcpu *vcpu)
55 {
56 vm_enable_cap(vcpu->vm, KVM_CAP_MSR_PLATFORM_INFO, false);
57 vcpu_run(vcpu);
58 TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_SHUTDOWN);
59 }
60
main(int argc,char * argv[])61 int main(int argc, char *argv[])
62 {
63 struct kvm_vcpu *vcpu;
64 struct kvm_vm *vm;
65 uint64_t msr_platform_info;
66
67 TEST_REQUIRE(kvm_has_cap(KVM_CAP_MSR_PLATFORM_INFO));
68
69 vm = vm_create_with_one_vcpu(&vcpu, guest_code);
70
71 msr_platform_info = vcpu_get_msr(vcpu, MSR_PLATFORM_INFO);
72 vcpu_set_msr(vcpu, MSR_PLATFORM_INFO,
73 msr_platform_info | MSR_PLATFORM_INFO_MAX_TURBO_RATIO);
74 test_msr_platform_info_enabled(vcpu);
75 test_msr_platform_info_disabled(vcpu);
76 vcpu_set_msr(vcpu, MSR_PLATFORM_INFO, msr_platform_info);
77
78 kvm_vm_free(vm);
79
80 return 0;
81 }
82