diff options
author | Dave Airlie <airlied@redhat.com> | 2018-09-26 21:06:46 -0400 |
---|---|---|
committer | Dave Airlie <airlied@redhat.com> | 2018-09-26 21:06:46 -0400 |
commit | bf78296ab1cb215d0609ac6cff4e43e941e51265 (patch) | |
tree | a193615b327d9ee538e71ca5f13bbfb4f3db4e6b /tools/testing/selftests/kvm/platform_info_test.c | |
parent | 18eb2f6e19d77900695987e3a2b775cccbe5b84e (diff) | |
parent | 6bf4ca7fbc85d80446ac01c0d1d77db4d91a6d84 (diff) |
BackMerge v4.19-rc5 into drm-next
Sean Paul requested an -rc5 backmerge from some sun4i fixes.
Signed-off-by: Dave Airlie <airlied@redhat.com>
Diffstat (limited to 'tools/testing/selftests/kvm/platform_info_test.c')
-rw-r--r-- | tools/testing/selftests/kvm/platform_info_test.c | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/tools/testing/selftests/kvm/platform_info_test.c b/tools/testing/selftests/kvm/platform_info_test.c new file mode 100644 index 000000000000..3764e7121265 --- /dev/null +++ b/tools/testing/selftests/kvm/platform_info_test.c | |||
@@ -0,0 +1,110 @@ | |||
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 "x86.h" | ||
23 | |||
24 | #define VCPU_ID 0 | ||
25 | #define MSR_PLATFORM_INFO_MAX_TURBO_RATIO 0xff00 | ||
26 | |||
27 | static void guest_code(void) | ||
28 | { | ||
29 | uint64_t msr_platform_info; | ||
30 | |||
31 | for (;;) { | ||
32 | msr_platform_info = rdmsr(MSR_PLATFORM_INFO); | ||
33 | GUEST_SYNC(msr_platform_info); | ||
34 | asm volatile ("inc %r11"); | ||
35 | } | ||
36 | } | ||
37 | |||
38 | static void set_msr_platform_info_enabled(struct kvm_vm *vm, bool enable) | ||
39 | { | ||
40 | struct kvm_enable_cap cap = {}; | ||
41 | |||
42 | cap.cap = KVM_CAP_MSR_PLATFORM_INFO; | ||
43 | cap.flags = 0; | ||
44 | cap.args[0] = (int)enable; | ||
45 | vm_enable_cap(vm, &cap); | ||
46 | } | ||
47 | |||
48 | static void test_msr_platform_info_enabled(struct kvm_vm *vm) | ||
49 | { | ||
50 | struct kvm_run *run = vcpu_state(vm, VCPU_ID); | ||
51 | struct guest_args args; | ||
52 | |||
53 | set_msr_platform_info_enabled(vm, true); | ||
54 | vcpu_run(vm, VCPU_ID); | ||
55 | TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, | ||
56 | "Exit_reason other than KVM_EXIT_IO: %u (%s),\n", | ||
57 | run->exit_reason, | ||
58 | exit_reason_str(run->exit_reason)); | ||
59 | guest_args_read(vm, VCPU_ID, &args); | ||
60 | TEST_ASSERT(args.port == GUEST_PORT_SYNC, | ||
61 | "Received IO from port other than PORT_HOST_SYNC: %u\n", | ||
62 | run->io.port); | ||
63 | TEST_ASSERT((args.arg1 & MSR_PLATFORM_INFO_MAX_TURBO_RATIO) == | ||
64 | MSR_PLATFORM_INFO_MAX_TURBO_RATIO, | ||
65 | "Expected MSR_PLATFORM_INFO to have max turbo ratio mask: %i.", | ||
66 | MSR_PLATFORM_INFO_MAX_TURBO_RATIO); | ||
67 | } | ||
68 | |||
69 | static void test_msr_platform_info_disabled(struct kvm_vm *vm) | ||
70 | { | ||
71 | struct kvm_run *run = vcpu_state(vm, VCPU_ID); | ||
72 | |||
73 | set_msr_platform_info_enabled(vm, false); | ||
74 | vcpu_run(vm, VCPU_ID); | ||
75 | TEST_ASSERT(run->exit_reason == KVM_EXIT_SHUTDOWN, | ||
76 | "Exit_reason other than KVM_EXIT_SHUTDOWN: %u (%s)\n", | ||
77 | run->exit_reason, | ||
78 | exit_reason_str(run->exit_reason)); | ||
79 | } | ||
80 | |||
81 | int main(int argc, char *argv[]) | ||
82 | { | ||
83 | struct kvm_vm *vm; | ||
84 | struct kvm_run *state; | ||
85 | int rv; | ||
86 | uint64_t msr_platform_info; | ||
87 | |||
88 | /* Tell stdout not to buffer its content */ | ||
89 | setbuf(stdout, NULL); | ||
90 | |||
91 | rv = kvm_check_cap(KVM_CAP_MSR_PLATFORM_INFO); | ||
92 | if (!rv) { | ||
93 | fprintf(stderr, | ||
94 | "KVM_CAP_MSR_PLATFORM_INFO not supported, skip test\n"); | ||
95 | exit(KSFT_SKIP); | ||
96 | } | ||
97 | |||
98 | vm = vm_create_default(VCPU_ID, 0, guest_code); | ||
99 | |||
100 | msr_platform_info = vcpu_get_msr(vm, VCPU_ID, MSR_PLATFORM_INFO); | ||
101 | vcpu_set_msr(vm, VCPU_ID, MSR_PLATFORM_INFO, | ||
102 | msr_platform_info | MSR_PLATFORM_INFO_MAX_TURBO_RATIO); | ||
103 | test_msr_platform_info_disabled(vm); | ||
104 | test_msr_platform_info_enabled(vm); | ||
105 | vcpu_set_msr(vm, VCPU_ID, MSR_PLATFORM_INFO, msr_platform_info); | ||
106 | |||
107 | kvm_vm_free(vm); | ||
108 | |||
109 | return 0; | ||
110 | } | ||