Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Test for x86 KVM_CAP_MSR_PLATFORM_INFO
0004  *
0005  * Copyright (C) 2018, Google LLC.
0006  *
0007  * This work is licensed under the terms of the GNU GPL, version 2.
0008  *
0009  * Verifies expected behavior of controlling guest access to
0010  * MSR_PLATFORM_INFO.
0011  */
0012 
0013 #define _GNU_SOURCE /* for program_invocation_short_name */
0014 #include <fcntl.h>
0015 #include <stdio.h>
0016 #include <stdlib.h>
0017 #include <string.h>
0018 #include <sys/ioctl.h>
0019 
0020 #include "test_util.h"
0021 #include "kvm_util.h"
0022 #include "processor.h"
0023 
0024 #define MSR_PLATFORM_INFO_MAX_TURBO_RATIO 0xff00
0025 
0026 static void guest_code(void)
0027 {
0028     uint64_t msr_platform_info;
0029 
0030     for (;;) {
0031         msr_platform_info = rdmsr(MSR_PLATFORM_INFO);
0032         GUEST_SYNC(msr_platform_info);
0033         asm volatile ("inc %r11");
0034     }
0035 }
0036 
0037 static void test_msr_platform_info_enabled(struct kvm_vcpu *vcpu)
0038 {
0039     struct kvm_run *run = vcpu->run;
0040     struct ucall uc;
0041 
0042     vm_enable_cap(vcpu->vm, KVM_CAP_MSR_PLATFORM_INFO, true);
0043     vcpu_run(vcpu);
0044     TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
0045             "Exit_reason other than KVM_EXIT_IO: %u (%s),\n",
0046             run->exit_reason,
0047             exit_reason_str(run->exit_reason));
0048     get_ucall(vcpu, &uc);
0049     TEST_ASSERT(uc.cmd == UCALL_SYNC,
0050             "Received ucall other than UCALL_SYNC: %lu\n", uc.cmd);
0051     TEST_ASSERT((uc.args[1] & MSR_PLATFORM_INFO_MAX_TURBO_RATIO) ==
0052         MSR_PLATFORM_INFO_MAX_TURBO_RATIO,
0053         "Expected MSR_PLATFORM_INFO to have max turbo ratio mask: %i.",
0054         MSR_PLATFORM_INFO_MAX_TURBO_RATIO);
0055 }
0056 
0057 static void test_msr_platform_info_disabled(struct kvm_vcpu *vcpu)
0058 {
0059     struct kvm_run *run = vcpu->run;
0060 
0061     vm_enable_cap(vcpu->vm, KVM_CAP_MSR_PLATFORM_INFO, false);
0062     vcpu_run(vcpu);
0063     TEST_ASSERT(run->exit_reason == KVM_EXIT_SHUTDOWN,
0064             "Exit_reason other than KVM_EXIT_SHUTDOWN: %u (%s)\n",
0065             run->exit_reason,
0066             exit_reason_str(run->exit_reason));
0067 }
0068 
0069 int main(int argc, char *argv[])
0070 {
0071     struct kvm_vcpu *vcpu;
0072     struct kvm_vm *vm;
0073     uint64_t msr_platform_info;
0074 
0075     /* Tell stdout not to buffer its content */
0076     setbuf(stdout, NULL);
0077 
0078     TEST_REQUIRE(kvm_has_cap(KVM_CAP_MSR_PLATFORM_INFO));
0079 
0080     vm = vm_create_with_one_vcpu(&vcpu, guest_code);
0081 
0082     msr_platform_info = vcpu_get_msr(vcpu, MSR_PLATFORM_INFO);
0083     vcpu_set_msr(vcpu, MSR_PLATFORM_INFO,
0084              msr_platform_info | MSR_PLATFORM_INFO_MAX_TURBO_RATIO);
0085     test_msr_platform_info_enabled(vcpu);
0086     test_msr_platform_info_disabled(vcpu);
0087     vcpu_set_msr(vcpu, MSR_PLATFORM_INFO, msr_platform_info);
0088 
0089     kvm_vm_free(vm);
0090 
0091     return 0;
0092 }