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