~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/kvm/x86_64/tsc_msrs_test.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 /*
  3  * Tests for MSR_IA32_TSC and MSR_IA32_TSC_ADJUST.
  4  *
  5  * Copyright (C) 2020, Red Hat, Inc.
  6  */
  7 #include <stdio.h>
  8 #include <string.h>
  9 #include "kvm_util.h"
 10 #include "processor.h"
 11 
 12 #define UNITY                  (1ull << 30)
 13 #define HOST_ADJUST            (UNITY * 64)
 14 #define GUEST_STEP             (UNITY * 4)
 15 #define ROUND(x)               ((x + UNITY / 2) & -UNITY)
 16 #define rounded_rdmsr(x)       ROUND(rdmsr(x))
 17 #define rounded_host_rdmsr(x)  ROUND(vcpu_get_msr(vcpu, x))
 18 
 19 static void guest_code(void)
 20 {
 21         u64 val = 0;
 22 
 23         GUEST_ASSERT_EQ(rounded_rdmsr(MSR_IA32_TSC), val);
 24         GUEST_ASSERT_EQ(rounded_rdmsr(MSR_IA32_TSC_ADJUST), val);
 25 
 26         /* Guest: writes to MSR_IA32_TSC affect both MSRs.  */
 27         val = 1ull * GUEST_STEP;
 28         wrmsr(MSR_IA32_TSC, val);
 29         GUEST_ASSERT_EQ(rounded_rdmsr(MSR_IA32_TSC), val);
 30         GUEST_ASSERT_EQ(rounded_rdmsr(MSR_IA32_TSC_ADJUST), val);
 31 
 32         /* Guest: writes to MSR_IA32_TSC_ADJUST affect both MSRs.  */
 33         GUEST_SYNC(2);
 34         val = 2ull * GUEST_STEP;
 35         wrmsr(MSR_IA32_TSC_ADJUST, val);
 36         GUEST_ASSERT_EQ(rounded_rdmsr(MSR_IA32_TSC), val);
 37         GUEST_ASSERT_EQ(rounded_rdmsr(MSR_IA32_TSC_ADJUST), val);
 38 
 39         /* Host: setting the TSC offset.  */
 40         GUEST_SYNC(3);
 41         GUEST_ASSERT_EQ(rounded_rdmsr(MSR_IA32_TSC), HOST_ADJUST + val);
 42         GUEST_ASSERT_EQ(rounded_rdmsr(MSR_IA32_TSC_ADJUST), val);
 43 
 44         /*
 45          * Guest: writes to MSR_IA32_TSC_ADJUST do not destroy the
 46          * host-side offset and affect both MSRs.
 47          */
 48         GUEST_SYNC(4);
 49         val = 3ull * GUEST_STEP;
 50         wrmsr(MSR_IA32_TSC_ADJUST, val);
 51         GUEST_ASSERT_EQ(rounded_rdmsr(MSR_IA32_TSC), HOST_ADJUST + val);
 52         GUEST_ASSERT_EQ(rounded_rdmsr(MSR_IA32_TSC_ADJUST), val);
 53 
 54         /*
 55          * Guest: writes to MSR_IA32_TSC affect both MSRs, so the host-side
 56          * offset is now visible in MSR_IA32_TSC_ADJUST.
 57          */
 58         GUEST_SYNC(5);
 59         val = 4ull * GUEST_STEP;
 60         wrmsr(MSR_IA32_TSC, val);
 61         GUEST_ASSERT_EQ(rounded_rdmsr(MSR_IA32_TSC), val);
 62         GUEST_ASSERT_EQ(rounded_rdmsr(MSR_IA32_TSC_ADJUST), val - HOST_ADJUST);
 63 
 64         GUEST_DONE();
 65 }
 66 
 67 static void run_vcpu(struct kvm_vcpu *vcpu, int stage)
 68 {
 69         struct ucall uc;
 70 
 71         vcpu_run(vcpu);
 72 
 73         switch (get_ucall(vcpu, &uc)) {
 74         case UCALL_SYNC:
 75                 if (!strcmp((const char *)uc.args[0], "hello") &&
 76                     uc.args[1] == stage + 1)
 77                         ksft_test_result_pass("stage %d passed\n", stage + 1);
 78                 else
 79                         ksft_test_result_fail(
 80                                 "stage %d: Unexpected register values vmexit, got %lx",
 81                                 stage + 1, (ulong)uc.args[1]);
 82                 return;
 83         case UCALL_DONE:
 84                 ksft_test_result_pass("stage %d passed\n", stage + 1);
 85                 return;
 86         case UCALL_ABORT:
 87                 REPORT_GUEST_ASSERT(uc);
 88         default:
 89                 TEST_ASSERT(false, "Unexpected exit: %s",
 90                             exit_reason_str(vcpu->run->exit_reason));
 91         }
 92 }
 93 
 94 int main(void)
 95 {
 96         struct kvm_vcpu *vcpu;
 97         struct kvm_vm *vm;
 98         uint64_t val;
 99 
100         ksft_print_header();
101         ksft_set_plan(5);
102 
103         vm = vm_create_with_one_vcpu(&vcpu, guest_code);
104 
105         val = 0;
106         TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), val);
107         TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC_ADJUST), val);
108 
109         /* Guest: writes to MSR_IA32_TSC affect both MSRs.  */
110         run_vcpu(vcpu, 1);
111         val = 1ull * GUEST_STEP;
112         TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), val);
113         TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC_ADJUST), val);
114 
115         /* Guest: writes to MSR_IA32_TSC_ADJUST affect both MSRs.  */
116         run_vcpu(vcpu, 2);
117         val = 2ull * GUEST_STEP;
118         TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), val);
119         TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC_ADJUST), val);
120 
121         /*
122          * Host: writes to MSR_IA32_TSC set the host-side offset
123          * and therefore do not change MSR_IA32_TSC_ADJUST.
124          */
125         vcpu_set_msr(vcpu, MSR_IA32_TSC, HOST_ADJUST + val);
126         TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), HOST_ADJUST + val);
127         TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC_ADJUST), val);
128         run_vcpu(vcpu, 3);
129 
130         /* Host: writes to MSR_IA32_TSC_ADJUST do not modify the TSC.  */
131         vcpu_set_msr(vcpu, MSR_IA32_TSC_ADJUST, UNITY * 123456);
132         TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), HOST_ADJUST + val);
133         TEST_ASSERT_EQ(vcpu_get_msr(vcpu, MSR_IA32_TSC_ADJUST), UNITY * 123456);
134 
135         /* Restore previous value.  */
136         vcpu_set_msr(vcpu, MSR_IA32_TSC_ADJUST, val);
137         TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), HOST_ADJUST + val);
138         TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC_ADJUST), val);
139 
140         /*
141          * Guest: writes to MSR_IA32_TSC_ADJUST do not destroy the
142          * host-side offset and affect both MSRs.
143          */
144         run_vcpu(vcpu, 4);
145         val = 3ull * GUEST_STEP;
146         TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), HOST_ADJUST + val);
147         TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC_ADJUST), val);
148 
149         /*
150          * Guest: writes to MSR_IA32_TSC affect both MSRs, so the host-side
151          * offset is now visible in MSR_IA32_TSC_ADJUST.
152          */
153         run_vcpu(vcpu, 5);
154         val = 4ull * GUEST_STEP;
155         TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC), val);
156         TEST_ASSERT_EQ(rounded_host_rdmsr(MSR_IA32_TSC_ADJUST), val - HOST_ADJUST);
157 
158         kvm_vm_free(vm);
159 
160         ksft_finished();        /* Print results and exit() accordingly */
161 }
162 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php