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