1 | /* $Id: GIMKvm.cpp 62641 2016-07-28 21:11:13Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * GIM - Guest Interface Manager, KVM implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2015-2016 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_GIM
|
---|
23 | #include <VBox/vmm/gim.h>
|
---|
24 | #include <VBox/vmm/cpum.h>
|
---|
25 | #include <VBox/vmm/hm.h>
|
---|
26 | #include <VBox/vmm/pdmapi.h>
|
---|
27 | #include <VBox/vmm/ssm.h>
|
---|
28 | #include "GIMInternal.h"
|
---|
29 | #include <VBox/vmm/vm.h>
|
---|
30 |
|
---|
31 | #include <VBox/disopcode.h>
|
---|
32 | #include <VBox/version.h>
|
---|
33 |
|
---|
34 | #include <iprt/asm-math.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 | #include <iprt/mem.h>
|
---|
39 | #include <iprt/spinlock.h>
|
---|
40 |
|
---|
41 |
|
---|
42 |
|
---|
43 | /*********************************************************************************************************************************
|
---|
44 | * Defined Constants And Macros *
|
---|
45 | *********************************************************************************************************************************/
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * GIM KVM saved-state version.
|
---|
49 | */
|
---|
50 | #define GIM_KVM_SAVED_STATE_VERSION UINT32_C(1)
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * VBox internal struct. to passback to EMT rendezvous callback while enabling
|
---|
54 | * the KVM wall-clock.
|
---|
55 | */
|
---|
56 | typedef struct KVMWALLCLOCKINFO
|
---|
57 | {
|
---|
58 | /** Guest physical address of the wall-clock struct. */
|
---|
59 | RTGCPHYS GCPhysWallClock;
|
---|
60 | } KVMWALLCLOCKINFO;
|
---|
61 | /** Pointer to the wall-clock info. struct. */
|
---|
62 | typedef KVMWALLCLOCKINFO *PKVMWALLCLOCKINFO;
|
---|
63 |
|
---|
64 |
|
---|
65 | /*********************************************************************************************************************************
|
---|
66 | * Global Variables *
|
---|
67 | *********************************************************************************************************************************/
|
---|
68 | #ifdef VBOX_WITH_STATISTICS
|
---|
69 | # define GIMKVM_MSRRANGE(a_uFirst, a_uLast, a_szName) \
|
---|
70 | { (a_uFirst), (a_uLast), kCpumMsrRdFn_Gim, kCpumMsrWrFn_Gim, 0, 0, 0, 0, 0, a_szName, { 0 }, { 0 }, { 0 }, { 0 } }
|
---|
71 | #else
|
---|
72 | # define GIMKVM_MSRRANGE(a_uFirst, a_uLast, a_szName) \
|
---|
73 | { (a_uFirst), (a_uLast), kCpumMsrRdFn_Gim, kCpumMsrWrFn_Gim, 0, 0, 0, 0, 0, a_szName }
|
---|
74 | #endif
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Array of MSR ranges supported by KVM.
|
---|
78 | */
|
---|
79 | static CPUMMSRRANGE const g_aMsrRanges_Kvm[] =
|
---|
80 | {
|
---|
81 | GIMKVM_MSRRANGE(MSR_GIM_KVM_RANGE0_START, MSR_GIM_KVM_RANGE0_END, "KVM range 0"),
|
---|
82 | GIMKVM_MSRRANGE(MSR_GIM_KVM_RANGE1_START, MSR_GIM_KVM_RANGE1_END, "KVM range 1")
|
---|
83 | };
|
---|
84 | #undef GIMKVM_MSRRANGE
|
---|
85 |
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Initializes the KVM GIM provider.
|
---|
89 | *
|
---|
90 | * @returns VBox status code.
|
---|
91 | * @param pVM The cross context VM structure.
|
---|
92 | */
|
---|
93 | VMMR3_INT_DECL(int) gimR3KvmInit(PVM pVM)
|
---|
94 | {
|
---|
95 | AssertReturn(pVM, VERR_INVALID_PARAMETER);
|
---|
96 | AssertReturn(pVM->gim.s.enmProviderId == GIMPROVIDERID_KVM, VERR_INTERNAL_ERROR_5);
|
---|
97 |
|
---|
98 | int rc;
|
---|
99 | PGIMKVM pKvm = &pVM->gim.s.u.Kvm;
|
---|
100 |
|
---|
101 | /*
|
---|
102 | * Determine interface capabilities based on the version.
|
---|
103 | */
|
---|
104 | if (!pVM->gim.s.u32Version)
|
---|
105 | {
|
---|
106 | /* Basic features. */
|
---|
107 | pKvm->uBaseFeat = 0
|
---|
108 | | GIM_KVM_BASE_FEAT_CLOCK_OLD
|
---|
109 | //| GIM_KVM_BASE_FEAT_NOP_IO_DELAY
|
---|
110 | //| GIM_KVM_BASE_FEAT_MMU_OP
|
---|
111 | | GIM_KVM_BASE_FEAT_CLOCK
|
---|
112 | //| GIM_KVM_BASE_FEAT_ASYNC_PF
|
---|
113 | //| GIM_KVM_BASE_FEAT_STEAL_TIME
|
---|
114 | //| GIM_KVM_BASE_FEAT_PV_EOI
|
---|
115 | | GIM_KVM_BASE_FEAT_PV_UNHALT
|
---|
116 | ;
|
---|
117 | /* Rest of the features are determined in gimR3KvmInitCompleted(). */
|
---|
118 | }
|
---|
119 |
|
---|
120 | /*
|
---|
121 | * Expose HVP (Hypervisor Present) bit to the guest.
|
---|
122 | */
|
---|
123 | CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_HVP);
|
---|
124 |
|
---|
125 | /*
|
---|
126 | * Modify the standard hypervisor leaves for KVM.
|
---|
127 | */
|
---|
128 | CPUMCPUIDLEAF HyperLeaf;
|
---|
129 | RT_ZERO(HyperLeaf);
|
---|
130 | HyperLeaf.uLeaf = UINT32_C(0x40000000);
|
---|
131 | HyperLeaf.uEax = UINT32_C(0x40000001); /* Minimum value for KVM is 0x40000001. */
|
---|
132 | HyperLeaf.uEbx = 0x4B4D564B; /* 'KVMK' */
|
---|
133 | HyperLeaf.uEcx = 0x564B4D56; /* 'VMKV' */
|
---|
134 | HyperLeaf.uEdx = 0x0000004D; /* 'M000' */
|
---|
135 | rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
|
---|
136 | AssertLogRelRCReturn(rc, rc);
|
---|
137 |
|
---|
138 | /*
|
---|
139 | * Add KVM specific leaves.
|
---|
140 | */
|
---|
141 | HyperLeaf.uLeaf = UINT32_C(0x40000001);
|
---|
142 | HyperLeaf.uEax = pKvm->uBaseFeat;
|
---|
143 | HyperLeaf.uEbx = 0; /* Reserved */
|
---|
144 | HyperLeaf.uEcx = 0; /* Reserved */
|
---|
145 | HyperLeaf.uEdx = 0; /* Reserved */
|
---|
146 | rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
|
---|
147 | AssertLogRelRCReturn(rc, rc);
|
---|
148 |
|
---|
149 | /*
|
---|
150 | * Insert all MSR ranges of KVM.
|
---|
151 | */
|
---|
152 | for (unsigned i = 0; i < RT_ELEMENTS(g_aMsrRanges_Kvm); i++)
|
---|
153 | {
|
---|
154 | rc = CPUMR3MsrRangesInsert(pVM, &g_aMsrRanges_Kvm[i]);
|
---|
155 | AssertLogRelRCReturn(rc, rc);
|
---|
156 | }
|
---|
157 |
|
---|
158 | /*
|
---|
159 | * Setup hypercall and #UD handling.
|
---|
160 | */
|
---|
161 | for (VMCPUID i = 0; i < pVM->cCpus; i++)
|
---|
162 | VMMHypercallsEnable(&pVM->aCpus[i]);
|
---|
163 |
|
---|
164 | if (ASMIsAmdCpu())
|
---|
165 | {
|
---|
166 | pKvm->fTrapXcptUD = true;
|
---|
167 | pKvm->uOpCodeNative = OP_VMMCALL;
|
---|
168 | }
|
---|
169 | else
|
---|
170 | {
|
---|
171 | Assert(ASMIsIntelCpu() || ASMIsViaCentaurCpu());
|
---|
172 | pKvm->fTrapXcptUD = false;
|
---|
173 | pKvm->uOpCodeNative = OP_VMCALL;
|
---|
174 | }
|
---|
175 |
|
---|
176 | /* We always need to trap VMCALL/VMMCALL hypercall using #UDs for raw-mode VMs. */
|
---|
177 | if (!HMIsEnabled(pVM))
|
---|
178 | pKvm->fTrapXcptUD = true;
|
---|
179 |
|
---|
180 | return VINF_SUCCESS;
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Initializes remaining bits of the KVM provider.
|
---|
186 | *
|
---|
187 | * This is called after initializing HM and almost all other VMM components.
|
---|
188 | *
|
---|
189 | * @returns VBox status code.
|
---|
190 | * @param pVM The cross context VM structure.
|
---|
191 | */
|
---|
192 | VMMR3_INT_DECL(int) gimR3KvmInitCompleted(PVM pVM)
|
---|
193 | {
|
---|
194 | PGIMKVM pKvm = &pVM->gim.s.u.Kvm;
|
---|
195 | pKvm->cTscTicksPerSecond = TMCpuTicksPerSecond(pVM);
|
---|
196 |
|
---|
197 | if (TMR3CpuTickIsFixedRateMonotonic(pVM, true /* fWithParavirtEnabled */))
|
---|
198 | {
|
---|
199 | /** @todo We might want to consider just enabling this bit *always*. As far
|
---|
200 | * as I can see in the Linux guest, the "TSC_STABLE" bit is only
|
---|
201 | * translated as a "monotonic" bit which even in Async systems we
|
---|
202 | * -should- be reporting a strictly monotonic TSC to the guest. */
|
---|
203 | pKvm->uBaseFeat |= GIM_KVM_BASE_FEAT_TSC_STABLE;
|
---|
204 |
|
---|
205 | CPUMCPUIDLEAF HyperLeaf;
|
---|
206 | RT_ZERO(HyperLeaf);
|
---|
207 | HyperLeaf.uLeaf = UINT32_C(0x40000001);
|
---|
208 | HyperLeaf.uEax = pKvm->uBaseFeat;
|
---|
209 | HyperLeaf.uEbx = 0;
|
---|
210 | HyperLeaf.uEcx = 0;
|
---|
211 | HyperLeaf.uEdx = 0;
|
---|
212 | int rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
|
---|
213 | AssertLogRelRCReturn(rc, rc);
|
---|
214 | }
|
---|
215 | return VINF_SUCCESS;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Terminates the KVM GIM provider.
|
---|
221 | *
|
---|
222 | * @returns VBox status code.
|
---|
223 | * @param pVM The cross context VM structure.
|
---|
224 | */
|
---|
225 | VMMR3_INT_DECL(int) gimR3KvmTerm(PVM pVM)
|
---|
226 | {
|
---|
227 | gimR3KvmReset(pVM);
|
---|
228 | return VINF_SUCCESS;
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * This resets KVM provider MSRs and unmaps whatever KVM regions that
|
---|
234 | * the guest may have mapped.
|
---|
235 | *
|
---|
236 | * This is called when the VM is being reset.
|
---|
237 | *
|
---|
238 | * @param pVM The cross context VM structure.
|
---|
239 | * @thread EMT(0).
|
---|
240 | */
|
---|
241 | VMMR3_INT_DECL(void) gimR3KvmReset(PVM pVM)
|
---|
242 | {
|
---|
243 | VM_ASSERT_EMT0(pVM);
|
---|
244 | LogRel(("GIM: KVM: Resetting MSRs\n"));
|
---|
245 |
|
---|
246 | /*
|
---|
247 | * Reset MSRs.
|
---|
248 | */
|
---|
249 | PGIMKVM pKvm = &pVM->gim.s.u.Kvm;
|
---|
250 | pKvm->u64WallClockMsr = 0;
|
---|
251 | for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
|
---|
252 | {
|
---|
253 | PGIMKVMCPU pKvmCpu = &pVM->aCpus[iCpu].gim.s.u.KvmCpu;
|
---|
254 | pKvmCpu->u64SystemTimeMsr = 0;
|
---|
255 | pKvmCpu->u32SystemTimeVersion = 0;
|
---|
256 | pKvmCpu->fSystemTimeFlags = 0;
|
---|
257 | pKvmCpu->GCPhysSystemTime = 0;
|
---|
258 | pKvmCpu->uTsc = 0;
|
---|
259 | pKvmCpu->uVirtNanoTS = 0;
|
---|
260 | }
|
---|
261 | }
|
---|
262 |
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * KVM state-save operation.
|
---|
266 | *
|
---|
267 | * @returns VBox status code.
|
---|
268 | * @param pVM The cross context VM structure.
|
---|
269 | * @param pSSM The saved state handle.
|
---|
270 | */
|
---|
271 | VMMR3_INT_DECL(int) gimR3KvmSave(PVM pVM, PSSMHANDLE pSSM)
|
---|
272 | {
|
---|
273 | PCGIMKVM pKvm = &pVM->gim.s.u.Kvm;
|
---|
274 |
|
---|
275 | /*
|
---|
276 | * Save the KVM SSM version.
|
---|
277 | */
|
---|
278 | SSMR3PutU32(pSSM, GIM_KVM_SAVED_STATE_VERSION);
|
---|
279 |
|
---|
280 | /*
|
---|
281 | * Save per-VCPU data.
|
---|
282 | */
|
---|
283 | for (uint32_t i = 0; i < pVM->cCpus; i++)
|
---|
284 | {
|
---|
285 | PCGIMKVMCPU pKvmCpu = &pVM->aCpus[i].gim.s.u.KvmCpu;
|
---|
286 |
|
---|
287 | /* Guest may alter flags (namely GIM_KVM_SYSTEM_TIME_FLAGS_GUEST_PAUSED bit). So re-read them from guest-memory. */
|
---|
288 | GIMKVMSYSTEMTIME SystemTime;
|
---|
289 | RT_ZERO(SystemTime);
|
---|
290 | if (MSR_GIM_KVM_SYSTEM_TIME_IS_ENABLED(pKvmCpu->u64SystemTimeMsr))
|
---|
291 | {
|
---|
292 | int rc = PGMPhysSimpleReadGCPhys(pVM, &SystemTime, pKvmCpu->GCPhysSystemTime, sizeof(GIMKVMSYSTEMTIME));
|
---|
293 | AssertRCReturn(rc, rc);
|
---|
294 | }
|
---|
295 |
|
---|
296 | SSMR3PutU64(pSSM, pKvmCpu->u64SystemTimeMsr);
|
---|
297 | SSMR3PutU64(pSSM, pKvmCpu->uTsc);
|
---|
298 | SSMR3PutU64(pSSM, pKvmCpu->uVirtNanoTS);
|
---|
299 | SSMR3PutGCPhys(pSSM, pKvmCpu->GCPhysSystemTime);
|
---|
300 | SSMR3PutU32(pSSM, pKvmCpu->u32SystemTimeVersion);
|
---|
301 | SSMR3PutU8(pSSM, SystemTime.fFlags);
|
---|
302 | }
|
---|
303 |
|
---|
304 | /*
|
---|
305 | * Save per-VM data.
|
---|
306 | */
|
---|
307 | SSMR3PutU64(pSSM, pKvm->u64WallClockMsr);
|
---|
308 | return SSMR3PutU32(pSSM, pKvm->uBaseFeat);
|
---|
309 | }
|
---|
310 |
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * KVM state-load operation, final pass.
|
---|
314 | *
|
---|
315 | * @returns VBox status code.
|
---|
316 | * @param pVM The cross context VM structure.
|
---|
317 | * @param pSSM The saved state handle.
|
---|
318 | */
|
---|
319 | VMMR3_INT_DECL(int) gimR3KvmLoad(PVM pVM, PSSMHANDLE pSSM)
|
---|
320 | {
|
---|
321 | /*
|
---|
322 | * Load the KVM SSM version first.
|
---|
323 | */
|
---|
324 | uint32_t uKvmSavedStatVersion;
|
---|
325 | int rc = SSMR3GetU32(pSSM, &uKvmSavedStatVersion);
|
---|
326 | AssertRCReturn(rc, rc);
|
---|
327 | if (uKvmSavedStatVersion != GIM_KVM_SAVED_STATE_VERSION)
|
---|
328 | return SSMR3SetLoadError(pSSM, VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION, RT_SRC_POS,
|
---|
329 | N_("Unsupported KVM saved-state version %u (expected %u)."),
|
---|
330 | uKvmSavedStatVersion, GIM_KVM_SAVED_STATE_VERSION);
|
---|
331 |
|
---|
332 | /*
|
---|
333 | * Update the TSC frequency from TM.
|
---|
334 | */
|
---|
335 | PGIMKVM pKvm = &pVM->gim.s.u.Kvm;
|
---|
336 | pKvm->cTscTicksPerSecond = TMCpuTicksPerSecond(pVM);
|
---|
337 |
|
---|
338 | /*
|
---|
339 | * Load per-VCPU data.
|
---|
340 | */
|
---|
341 | for (uint32_t i = 0; i < pVM->cCpus; i++)
|
---|
342 | {
|
---|
343 | PVMCPU pVCpu = &pVM->aCpus[i];
|
---|
344 | PGIMKVMCPU pKvmCpu = &pVCpu->gim.s.u.KvmCpu;
|
---|
345 |
|
---|
346 | SSMR3GetU64(pSSM, &pKvmCpu->u64SystemTimeMsr);
|
---|
347 | SSMR3GetU64(pSSM, &pKvmCpu->uTsc);
|
---|
348 | SSMR3GetU64(pSSM, &pKvmCpu->uVirtNanoTS);
|
---|
349 | SSMR3GetGCPhys(pSSM, &pKvmCpu->GCPhysSystemTime);
|
---|
350 | SSMR3GetU32(pSSM, &pKvmCpu->u32SystemTimeVersion);
|
---|
351 | rc = SSMR3GetU8(pSSM, &pKvmCpu->fSystemTimeFlags);
|
---|
352 | AssertRCReturn(rc, rc);
|
---|
353 |
|
---|
354 | /* Enable the system-time struct. if necessary. */
|
---|
355 | /** @todo update guest struct only if cTscTicksPerSecond doesn't match host
|
---|
356 | * anymore. */
|
---|
357 | if (MSR_GIM_KVM_SYSTEM_TIME_IS_ENABLED(pKvmCpu->u64SystemTimeMsr))
|
---|
358 | {
|
---|
359 | Assert(!TMVirtualIsTicking(pVM)); /* paranoia. */
|
---|
360 | Assert(!TMCpuTickIsTicking(pVCpu));
|
---|
361 | rc = gimR3KvmEnableSystemTime(pVM, pVCpu);
|
---|
362 | AssertRCReturn(rc, rc);
|
---|
363 | }
|
---|
364 | }
|
---|
365 |
|
---|
366 | /*
|
---|
367 | * Load per-VM data.
|
---|
368 | */
|
---|
369 | SSMR3GetU64(pSSM, &pKvm->u64WallClockMsr);
|
---|
370 | rc = SSMR3GetU32(pSSM, &pKvm->uBaseFeat);
|
---|
371 | AssertRCReturn(rc, rc);
|
---|
372 |
|
---|
373 | return VINF_SUCCESS;
|
---|
374 | }
|
---|
375 |
|
---|
376 |
|
---|
377 | /**
|
---|
378 | * Enables the KVM VCPU system-time structure.
|
---|
379 | *
|
---|
380 | * @returns VBox status code.
|
---|
381 | * @param pVM The cross context VM structure.
|
---|
382 | * @param pVCpu The cross context virtual CPU structure.
|
---|
383 | *
|
---|
384 | * @remarks Don't do any release assertions here, these can be triggered by
|
---|
385 | * guest R0 code.
|
---|
386 | */
|
---|
387 | VMMR3_INT_DECL(int) gimR3KvmEnableSystemTime(PVM pVM, PVMCPU pVCpu)
|
---|
388 | {
|
---|
389 | PGIMKVM pKvm = &pVM->gim.s.u.Kvm;
|
---|
390 | PGIMKVMCPU pKvmCpu = &pVCpu->gim.s.u.KvmCpu;
|
---|
391 |
|
---|
392 | /*
|
---|
393 | * Validate the mapping address first.
|
---|
394 | */
|
---|
395 | if (!PGMPhysIsGCPhysNormal(pVM, pKvmCpu->GCPhysSystemTime))
|
---|
396 | {
|
---|
397 | LogRel(("GIM: KVM: VCPU%3d: Invalid physical addr requested for mapping system-time struct. GCPhysSystemTime=%#RGp\n",
|
---|
398 | pVCpu->idCpu, pKvmCpu->GCPhysSystemTime));
|
---|
399 | return VERR_GIM_OPERATION_FAILED;
|
---|
400 | }
|
---|
401 |
|
---|
402 | /*
|
---|
403 | * Construct the system-time struct.
|
---|
404 | */
|
---|
405 | GIMKVMSYSTEMTIME SystemTime;
|
---|
406 | RT_ZERO(SystemTime);
|
---|
407 | SystemTime.u32Version = pKvmCpu->u32SystemTimeVersion;
|
---|
408 | SystemTime.u64NanoTS = pKvmCpu->uVirtNanoTS;
|
---|
409 | SystemTime.u64Tsc = pKvmCpu->uTsc;
|
---|
410 | SystemTime.fFlags = pKvmCpu->fSystemTimeFlags | GIM_KVM_SYSTEM_TIME_FLAGS_TSC_STABLE;
|
---|
411 |
|
---|
412 | /*
|
---|
413 | * How the guest calculates the system time (nanoseconds):
|
---|
414 | *
|
---|
415 | * tsc = rdtsc - SysTime.u64Tsc
|
---|
416 | * if (SysTime.i8TscShift >= 0)
|
---|
417 | * tsc <<= i8TscShift;
|
---|
418 | * else
|
---|
419 | * tsc >>= -i8TscShift;
|
---|
420 | * time = ((tsc * SysTime.u32TscScale) >> 32) + SysTime.u64NanoTS
|
---|
421 | */
|
---|
422 | uint64_t u64TscFreq = pKvm->cTscTicksPerSecond;
|
---|
423 | SystemTime.i8TscShift = 0;
|
---|
424 | while (u64TscFreq > 2 * RT_NS_1SEC_64)
|
---|
425 | {
|
---|
426 | u64TscFreq >>= 1;
|
---|
427 | SystemTime.i8TscShift--;
|
---|
428 | }
|
---|
429 | uint32_t uTscFreqLo = (uint32_t)u64TscFreq;
|
---|
430 | while (uTscFreqLo <= RT_NS_1SEC)
|
---|
431 | {
|
---|
432 | uTscFreqLo <<= 1;
|
---|
433 | SystemTime.i8TscShift++;
|
---|
434 | }
|
---|
435 | SystemTime.u32TscScale = ASMDivU64ByU32RetU32(RT_NS_1SEC_64 << 32, uTscFreqLo);
|
---|
436 |
|
---|
437 | /*
|
---|
438 | * Update guest memory with the system-time struct.
|
---|
439 | */
|
---|
440 | Assert(!(SystemTime.u32Version & UINT32_C(1)));
|
---|
441 | int rc = PGMPhysSimpleWriteGCPhys(pVM, pKvmCpu->GCPhysSystemTime, &SystemTime, sizeof(GIMKVMSYSTEMTIME));
|
---|
442 | if (RT_SUCCESS(rc))
|
---|
443 | {
|
---|
444 | LogRel(("GIM: KVM: VCPU%3d: Enabled system-time struct. at %#RGp - u32TscScale=%#RX32 i8TscShift=%d uVersion=%#RU32 "
|
---|
445 | "fFlags=%#x uTsc=%#RX64 uVirtNanoTS=%#RX64\n", pVCpu->idCpu, pKvmCpu->GCPhysSystemTime, SystemTime.u32TscScale,
|
---|
446 | SystemTime.i8TscShift, SystemTime.u32Version, SystemTime.fFlags, pKvmCpu->uTsc, pKvmCpu->uVirtNanoTS));
|
---|
447 | TMR3CpuTickParavirtEnable(pVM);
|
---|
448 | }
|
---|
449 | else
|
---|
450 | LogRel(("GIM: KVM: VCPU%3d: Failed to write system-time struct. at %#RGp. rc=%Rrc\n",
|
---|
451 | pVCpu->idCpu, pKvmCpu->GCPhysSystemTime, rc));
|
---|
452 |
|
---|
453 | return rc;
|
---|
454 | }
|
---|
455 |
|
---|
456 |
|
---|
457 | /**
|
---|
458 | * Disables the KVM system-time struct.
|
---|
459 | *
|
---|
460 | * @returns VBox status code.
|
---|
461 | * @param pVM The cross context VM structure.
|
---|
462 | */
|
---|
463 | VMMR3_INT_DECL(int) gimR3KvmDisableSystemTime(PVM pVM)
|
---|
464 | {
|
---|
465 | TMR3CpuTickParavirtDisable(pVM);
|
---|
466 | return VINF_SUCCESS;
|
---|
467 | }
|
---|
468 |
|
---|
469 |
|
---|
470 | /**
|
---|
471 | * @callback_method_impl{PFNVMMEMTRENDEZVOUS,
|
---|
472 | * Worker for gimR3KvmEnableWallClock}
|
---|
473 | */
|
---|
474 | static DECLCALLBACK(VBOXSTRICTRC) gimR3KvmEnableWallClockCallback(PVM pVM, PVMCPU pVCpu, void *pvUser)
|
---|
475 | {
|
---|
476 | PKVMWALLCLOCKINFO pWallClockInfo = (PKVMWALLCLOCKINFO)pvUser; AssertPtr(pWallClockInfo);
|
---|
477 | RTGCPHYS GCPhysWallClock = pWallClockInfo->GCPhysWallClock;
|
---|
478 | RT_NOREF1(pVCpu);
|
---|
479 |
|
---|
480 | /*
|
---|
481 | * Read the wall-clock version (sequence) from the guest.
|
---|
482 | */
|
---|
483 | uint32_t uVersion;
|
---|
484 | Assert(PGMPhysIsGCPhysNormal(pVM, GCPhysWallClock));
|
---|
485 | int rc = PGMPhysSimpleReadGCPhys(pVM, &uVersion, GCPhysWallClock, sizeof(uVersion));
|
---|
486 | if (RT_FAILURE(rc))
|
---|
487 | {
|
---|
488 | LogRel(("GIM: KVM: Failed to read wall-clock struct. version at %#RGp. rc=%Rrc\n", GCPhysWallClock, rc));
|
---|
489 | return rc;
|
---|
490 | }
|
---|
491 |
|
---|
492 | /*
|
---|
493 | * Ensure the version is incrementally even.
|
---|
494 | */
|
---|
495 | /* faster: uVersion = (uVersion | 1) + 1; */
|
---|
496 | if (!(uVersion & 1))
|
---|
497 | ++uVersion;
|
---|
498 | ++uVersion;
|
---|
499 |
|
---|
500 | /*
|
---|
501 | * Update wall-clock guest struct. with UTC information.
|
---|
502 | */
|
---|
503 | RTTIMESPEC TimeSpec;
|
---|
504 | int32_t iSec;
|
---|
505 | int32_t iNano;
|
---|
506 | TMR3UtcNow(pVM, &TimeSpec);
|
---|
507 | RTTimeSpecGetSecondsAndNano(&TimeSpec, &iSec, &iNano);
|
---|
508 |
|
---|
509 | GIMKVMWALLCLOCK WallClock;
|
---|
510 | RT_ZERO(WallClock);
|
---|
511 | AssertCompile(sizeof(uVersion) == sizeof(WallClock.u32Version));
|
---|
512 | WallClock.u32Version = uVersion;
|
---|
513 | WallClock.u32Sec = iSec;
|
---|
514 | WallClock.u32Nano = iNano;
|
---|
515 |
|
---|
516 | /*
|
---|
517 | * Write out the wall-clock struct. to guest memory.
|
---|
518 | */
|
---|
519 | Assert(!(WallClock.u32Version & 1));
|
---|
520 | rc = PGMPhysSimpleWriteGCPhys(pVM, GCPhysWallClock, &WallClock, sizeof(GIMKVMWALLCLOCK));
|
---|
521 | if (RT_SUCCESS(rc))
|
---|
522 | LogRel(("GIM: KVM: Enabled wall-clock struct. at %#RGp - u32Sec=%u u32Nano=%u uVersion=%#RU32\n", GCPhysWallClock,
|
---|
523 | WallClock.u32Sec, WallClock.u32Nano, WallClock.u32Version));
|
---|
524 | else
|
---|
525 | LogRel(("GIM: KVM: Failed to write wall-clock struct. at %#RGp. rc=%Rrc\n", GCPhysWallClock, rc));
|
---|
526 | return rc;
|
---|
527 | }
|
---|
528 |
|
---|
529 |
|
---|
530 | /**
|
---|
531 | * Enables the KVM wall-clock structure.
|
---|
532 | *
|
---|
533 | * Since the wall-clock can be read by any VCPU but it is a global struct. in
|
---|
534 | * guest-memory, we do an EMT rendezvous here to be on the safe side. The
|
---|
535 | * alternative is to use an MMIO2 region and use the WallClock.u32Version field
|
---|
536 | * for transactional update. However, this MSR is rarely written to (typically
|
---|
537 | * once during bootup) it's currently not a performance issue especially since
|
---|
538 | * we're already in ring-3. If we really wanted better performance in this code
|
---|
539 | * path, we should be doing it in ring-0 with transactional update while make
|
---|
540 | * sure there is only 1 writer as well.
|
---|
541 | *
|
---|
542 | * @returns VBox status code.
|
---|
543 | * @param pVM The cross context VM structure.
|
---|
544 | * @param GCPhysWallClock Where the guest wall-clock structure is located.
|
---|
545 | *
|
---|
546 | * @remarks Don't do any release assertions here, these can be triggered by
|
---|
547 | * guest R0 code.
|
---|
548 | */
|
---|
549 | VMMR3_INT_DECL(int) gimR3KvmEnableWallClock(PVM pVM, RTGCPHYS GCPhysWallClock)
|
---|
550 | {
|
---|
551 | KVMWALLCLOCKINFO WallClockInfo;
|
---|
552 | WallClockInfo.GCPhysWallClock = GCPhysWallClock;
|
---|
553 | return VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, gimR3KvmEnableWallClockCallback, &WallClockInfo);
|
---|
554 | }
|
---|
555 |
|
---|