Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / testing / selftests / kvm / x86_64 / platform_info_test.c
blob9cbf283ebc55f46e42c3712fefaef2cbebe79239
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Test for x86 KVM_CAP_MSR_PLATFORM_INFO
5 * Copyright (C) 2018, Google LLC.
7 * This work is licensed under the terms of the GNU GPL, version 2.
9 * Verifies expected behavior of controlling guest access to
10 * MSR_PLATFORM_INFO.
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/ioctl.h>
18 #include "test_util.h"
19 #include "kvm_util.h"
20 #include "processor.h"
22 #define MSR_PLATFORM_INFO_MAX_TURBO_RATIO 0xff00
24 static void guest_code(void)
26 uint64_t msr_platform_info;
27 uint8_t vector;
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);
34 GUEST_SYNC(false);
35 vector = rdmsr_safe(MSR_PLATFORM_INFO, &msr_platform_info);
36 GUEST_ASSERT_EQ(vector, GP_VECTOR);
38 GUEST_DONE();
41 int main(int argc, char *argv[])
43 struct kvm_vcpu *vcpu;
44 struct kvm_vm *vm;
45 uint64_t msr_platform_info;
46 struct ucall uc;
48 TEST_REQUIRE(kvm_has_cap(KVM_CAP_MSR_PLATFORM_INFO));
50 vm = vm_create_with_one_vcpu(&vcpu, guest_code);
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);
56 for (;;) {
57 vcpu_run(vcpu);
58 TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
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;
74 done:
75 kvm_vm_free(vm);
77 return 0;