1 | /* $Id: GIMKvm.cpp 58564 2015-11-04 13:53:54Z 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 The cross context VM structure.
|
---|
90 | */
|
---|
91 | VMMR3_INT_DECL(int) gimR3KvmInit(PVM pVM)
|
---|
92 | {
|
---|
93 | AssertReturn(pVM, VERR_INVALID_PARAMETER);
|
---|
94 | AssertReturn(pVM->gim.s.enmProviderId == GIMPROVIDERID_KVM, VERR_INTERNAL_ERROR_5);
|
---|
95 |
|
---|
96 | int rc;
|
---|
97 | PGIMKVM pKvm = &pVM->gim.s.u.Kvm;
|
---|
98 |
|
---|
99 | /*
|
---|
100 | * Determine interface capabilities based on the version.
|
---|
101 | */
|
---|
102 | if (!pVM->gim.s.u32Version)
|
---|
103 | {
|
---|
104 | /* Basic features. */
|
---|
105 | pKvm->uBaseFeat = 0
|
---|
106 | | GIM_KVM_BASE_FEAT_CLOCK_OLD
|
---|
107 | //| GIM_KVM_BASE_FEAT_NOP_IO_DELAY
|
---|
108 | //| GIM_KVM_BASE_FEAT_MMU_OP
|
---|
109 | | GIM_KVM_BASE_FEAT_CLOCK
|
---|
110 | //| GIM_KVM_BASE_FEAT_ASYNC_PF
|
---|
111 | //| GIM_KVM_BASE_FEAT_STEAL_TIME
|
---|
112 | //| GIM_KVM_BASE_FEAT_PV_EOI
|
---|
113 | | GIM_KVM_BASE_FEAT_PV_UNHALT
|
---|
114 | ;
|
---|
115 | /* Rest of the features are determined in gimR3KvmInitCompleted(). */
|
---|
116 | }
|
---|
117 |
|
---|
118 | /*
|
---|
119 | * Expose HVP (Hypervisor Present) bit to the guest.
|
---|
120 | */
|
---|
121 | CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_HVP);
|
---|
122 |
|
---|
123 | /*
|
---|
124 | * Modify the standard hypervisor leaves for KVM.
|
---|
125 | */
|
---|
126 | CPUMCPUIDLEAF HyperLeaf;
|
---|
127 | RT_ZERO(HyperLeaf);
|
---|
128 | HyperLeaf.uLeaf = UINT32_C(0x40000000);
|
---|
129 | HyperLeaf.uEax = UINT32_C(0x40000001); /* Minimum value for KVM is 0x40000001. */
|
---|
130 | HyperLeaf.uEbx = 0x4B4D564B; /* 'KVMK' */
|
---|
131 | HyperLeaf.uEcx = 0x564B4D56; /* 'VMKV' */
|
---|
132 | HyperLeaf.uEdx = 0x0000004D; /* 'M000' */
|
---|
133 | rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
|
---|
134 | AssertLogRelRCReturn(rc, rc);
|
---|
135 |
|
---|
136 | /*
|
---|
137 | * Add KVM specific leaves.
|
---|
138 | */
|
---|
139 | HyperLeaf.uLeaf = UINT32_C(0x40000001);
|
---|
140 | HyperLeaf.uEax = pKvm->uBaseFeat;
|
---|
141 | HyperLeaf.uEbx = 0; /* Reserved */
|
---|
142 | HyperLeaf.uEcx = 0; /* Reserved */
|
---|
143 | HyperLeaf.uEdx = 0; /* Reserved */
|
---|
144 | rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
|
---|
145 | AssertLogRelRCReturn(rc, rc);
|
---|
146 |
|
---|
147 | /*
|
---|
148 | * Insert all MSR ranges of KVM.
|
---|
149 | */
|
---|
150 | for (unsigned i = 0; i < RT_ELEMENTS(g_aMsrRanges_Kvm); i++)
|
---|
151 | {
|
---|
152 | rc = CPUMR3MsrRangesInsert(pVM, &g_aMsrRanges_Kvm[i]);
|
---|
153 | AssertLogRelRCReturn(rc, rc);
|
---|
154 | }
|
---|
155 |
|
---|
156 | /*
|
---|
157 | * Setup hypercall and #UD handling.
|
---|
158 | */
|
---|
159 | for (VMCPUID i = 0; i < pVM->cCpus; i++)
|
---|
160 | VMMHypercallsEnable(&pVM->aCpus[i]);
|
---|
161 |
|
---|
162 | if (ASMIsAmdCpu())
|
---|
163 | {
|
---|
164 | pKvm->fTrapXcptUD = true;
|
---|
165 | pKvm->uOpCodeNative = OP_VMMCALL;
|
---|
166 | }
|
---|
167 | else
|
---|
168 | {
|
---|
169 | Assert(ASMIsIntelCpu() || ASMIsViaCentaurCpu());
|
---|
170 | pKvm->fTrapXcptUD = false;
|
---|
171 | pKvm->uOpCodeNative = OP_VMCALL;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /* We always need to trap VMCALL/VMMCALL hypercall using #UDs for raw-mode VMs. */
|
---|
175 | if (!HMIsEnabled(pVM))
|
---|
176 | pKvm->fTrapXcptUD = true;
|
---|
177 |
|
---|
178 | return VINF_SUCCESS;
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Initializes remaining bits of the KVM provider.
|
---|
184 | *
|
---|
185 | * This is called after initializing HM and almost all other VMM components.
|
---|
186 | *
|
---|
187 | * @returns VBox status code.
|
---|
188 | * @param pVM The cross context VM structure.
|
---|
189 | */
|
---|
190 | VMMR3_INT_DECL(int) gimR3KvmInitCompleted(PVM pVM)
|
---|
191 | {
|
---|
192 | PGIMKVM pKvm = &pVM->gim.s.u.Kvm;
|
---|
193 | pKvm->cTscTicksPerSecond = TMCpuTicksPerSecond(pVM);
|
---|
194 |
|
---|
195 | if (TMR3CpuTickIsFixedRateMonotonic(pVM, true /* fWithParavirtEnabled */))
|
---|
196 | {
|
---|
197 | /** @todo We might want to consider just enabling this bit *always*. As far
|
---|
198 | * as I can see in the Linux guest, the "TSC_STABLE" bit is only
|
---|
199 | * translated as a "monotonic" bit which even in Async systems we
|
---|
200 | * -should- be reporting a strictly monotonic TSC to the guest. */
|
---|
201 | pKvm->uBaseFeat |= GIM_KVM_BASE_FEAT_TSC_STABLE;
|
---|
202 |
|
---|
203 | CPUMCPUIDLEAF HyperLeaf;
|
---|
204 | RT_ZERO(HyperLeaf);
|
---|
205 | HyperLeaf.uLeaf = UINT32_C(0x40000001);
|
---|
206 | HyperLeaf.uEax = pKvm->uBaseFeat;
|
---|
207 | HyperLeaf.uEbx = 0;
|
---|
208 | HyperLeaf.uEcx = 0;
|
---|
209 | HyperLeaf.uEdx = 0;
|
---|
210 | int rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
|
---|
211 | AssertLogRelRCReturn(rc, rc);
|
---|
212 | }
|
---|
213 | return VINF_SUCCESS;
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Terminates the KVM GIM provider.
|
---|
219 | *
|
---|
220 | * @returns VBox status code.
|
---|
221 | * @param pVM The cross context VM structure.
|
---|
222 | */
|
---|
223 | VMMR3_INT_DECL(int) gimR3KvmTerm(PVM pVM)
|
---|
224 | {
|
---|
225 | gimR3KvmReset(pVM);
|
---|
226 | return VINF_SUCCESS;
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * This resets KVM provider MSRs and unmaps whatever KVM regions that
|
---|
232 | * the guest may have mapped.
|
---|
233 | *
|
---|
234 | * This is called when the VM is being reset.
|
---|
235 | *
|
---|
236 | * @param pVM The cross context VM structure.
|
---|
237 | * @thread EMT(0).
|
---|
238 | */
|
---|
239 | VMMR3_INT_DECL(void) gimR3KvmReset(PVM pVM)
|
---|
240 | {
|
---|
241 | VM_ASSERT_EMT0(pVM);
|
---|
242 | LogRel(("GIM: KVM: Resetting MSRs\n"));
|
---|
243 |
|
---|
244 | /*
|
---|
245 | * Reset MSRs.
|
---|
246 | */
|
---|
247 | PGIMKVM pKvm = &pVM->gim.s.u.Kvm;
|
---|
248 | pKvm->u64WallClockMsr = 0;
|
---|
249 | for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
|
---|
250 | {
|
---|
251 | PGIMKVMCPU pKvmCpu = &pVM->aCpus[iCpu].gim.s.u.KvmCpu;
|
---|
252 | pKvmCpu->u64SystemTimeMsr = 0;
|
---|
253 | pKvmCpu->u32SystemTimeVersion = 0;
|
---|
254 | pKvmCpu->fSystemTimeFlags = 0;
|
---|
255 | pKvmCpu->GCPhysSystemTime = 0;
|
---|
256 | pKvmCpu->uTsc = 0;
|
---|
257 | pKvmCpu->uVirtNanoTS = 0;
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * KVM state-save operation.
|
---|
264 | *
|
---|
265 | * @returns VBox status code.
|
---|
266 | * @param pVM The cross context VM structure.
|
---|
267 | * @param pSSM Pointer to the SSM handle.
|
---|
268 | */
|
---|
269 | VMMR3_INT_DECL(int) gimR3KvmSave(PVM pVM, PSSMHANDLE pSSM)
|
---|
270 | {
|
---|
271 | PCGIMKVM pcKvm = &pVM->gim.s.u.Kvm;
|
---|
272 |
|
---|
273 | /*
|
---|
274 | * Save the KVM SSM version.
|
---|
275 | */
|
---|
276 | SSMR3PutU32(pSSM, GIM_KVM_SAVED_STATE_VERSION);
|
---|
277 |
|
---|
278 | /*
|
---|
279 | * Save per-VCPU data.
|
---|
280 | */
|
---|
281 | for (uint32_t i = 0; i < pVM->cCpus; i++)
|
---|
282 | {
|
---|
283 | PCGIMKVMCPU pcKvmCpu = &pVM->aCpus[i].gim.s.u.KvmCpu;
|
---|
284 |
|
---|
285 | /* Guest may alter flags (namely GIM_KVM_SYSTEM_TIME_FLAGS_GUEST_PAUSED bit). So re-read them from guest-memory. */
|
---|
286 | GIMKVMSYSTEMTIME SystemTime;
|
---|
287 | RT_ZERO(SystemTime);
|
---|
288 | if (MSR_GIM_KVM_SYSTEM_TIME_IS_ENABLED(pcKvmCpu->u64SystemTimeMsr))
|
---|
289 | {
|
---|
290 | int rc = PGMPhysSimpleReadGCPhys(pVM, &SystemTime, pcKvmCpu->GCPhysSystemTime, sizeof(GIMKVMSYSTEMTIME));
|
---|
291 | AssertRCReturn(rc, rc);
|
---|
292 | }
|
---|
293 |
|
---|
294 | SSMR3PutU64(pSSM, pcKvmCpu->u64SystemTimeMsr);
|
---|
295 | SSMR3PutU64(pSSM, pcKvmCpu->uTsc);
|
---|
296 | SSMR3PutU64(pSSM, pcKvmCpu->uVirtNanoTS);
|
---|
297 | SSMR3PutGCPhys(pSSM, pcKvmCpu->GCPhysSystemTime);
|
---|
298 | SSMR3PutU32(pSSM, pcKvmCpu->u32SystemTimeVersion);
|
---|
299 | SSMR3PutU8(pSSM, SystemTime.fFlags);
|
---|
300 | }
|
---|
301 |
|
---|
302 | /*
|
---|
303 | * Save per-VM data.
|
---|
304 | */
|
---|
305 | SSMR3PutU64(pSSM, pcKvm->u64WallClockMsr);
|
---|
306 | return SSMR3PutU32(pSSM, pcKvm->uBaseFeat);
|
---|
307 | }
|
---|
308 |
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * KVM state-load operation, final pass.
|
---|
312 | *
|
---|
313 | * @returns VBox status code.
|
---|
314 | * @param pVM The cross context VM structure.
|
---|
315 | * @param pSSM Pointer to the SSM handle.
|
---|
316 | * @param uSSMVersion The GIM saved-state version.
|
---|
317 | */
|
---|
318 | VMMR3_INT_DECL(int) gimR3KvmLoad(PVM pVM, PSSMHANDLE pSSM, uint32_t uSSMVersion)
|
---|
319 | {
|
---|
320 | /*
|
---|
321 | * Load the KVM SSM version first.
|
---|
322 | */
|
---|
323 | uint32_t uKvmSavedStatVersion;
|
---|
324 | int rc = SSMR3GetU32(pSSM, &uKvmSavedStatVersion);
|
---|
325 | AssertRCReturn(rc, rc);
|
---|
326 | if (uKvmSavedStatVersion != GIM_KVM_SAVED_STATE_VERSION)
|
---|
327 | return SSMR3SetLoadError(pSSM, VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION, RT_SRC_POS,
|
---|
328 | N_("Unsupported KVM saved-state version %u (expected %u)."), uKvmSavedStatVersion,
|
---|
329 | GIM_KVM_SAVED_STATE_VERSION);
|
---|
330 |
|
---|
331 | /*
|
---|
332 | * Update the TSC frequency from TM.
|
---|
333 | */
|
---|
334 | PGIMKVM pKvm = &pVM->gim.s.u.Kvm;
|
---|
335 | pKvm->cTscTicksPerSecond = TMCpuTicksPerSecond(pVM);
|
---|
336 |
|
---|
337 | /*
|
---|
338 | * Load per-VCPU data.
|
---|
339 | */
|
---|
340 | for (uint32_t i = 0; i < pVM->cCpus; i++)
|
---|
341 | {
|
---|
342 | PVMCPU pVCpu = &pVM->aCpus[i];
|
---|
343 | PGIMKVMCPU pKvmCpu = &pVCpu->gim.s.u.KvmCpu;
|
---|
344 |
|
---|
345 | uint8_t fSystemTimeFlags = 0;
|
---|
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 *pvData)
|
---|
475 | {
|
---|
476 | Assert(pvData);
|
---|
477 | PKVMWALLCLOCKINFO pWallClockInfo = (PKVMWALLCLOCKINFO)pvData;
|
---|
478 | RTGCPHYS GCPhysWallClock = pWallClockInfo->GCPhysWallClock;
|
---|
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 | if (!(uVersion & 1))
|
---|
496 | ++uVersion;
|
---|
497 | ++uVersion;
|
---|
498 |
|
---|
499 | /*
|
---|
500 | * Update wall-clock guest struct. with UTC information.
|
---|
501 | */
|
---|
502 | RTTIMESPEC TimeSpec;
|
---|
503 | int32_t iSec;
|
---|
504 | int32_t iNano;
|
---|
505 | TMR3UtcNow(pVM, &TimeSpec);
|
---|
506 | RTTimeSpecGetSecondsAndNano(&TimeSpec, &iSec, &iNano);
|
---|
507 |
|
---|
508 | GIMKVMWALLCLOCK WallClock;
|
---|
509 | RT_ZERO(WallClock);
|
---|
510 | AssertCompile(sizeof(uVersion) == sizeof(WallClock.u32Version));
|
---|
511 | WallClock.u32Version = uVersion;
|
---|
512 | WallClock.u32Sec = iSec;
|
---|
513 | WallClock.u32Nano = iNano;
|
---|
514 |
|
---|
515 | /*
|
---|
516 | * Write out the wall-clock struct. to guest memory.
|
---|
517 | */
|
---|
518 | Assert(!(WallClock.u32Version & 1));
|
---|
519 | rc = PGMPhysSimpleWriteGCPhys(pVM, GCPhysWallClock, &WallClock, sizeof(GIMKVMWALLCLOCK));
|
---|
520 | if (RT_SUCCESS(rc))
|
---|
521 | {
|
---|
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 | }
|
---|
525 | else
|
---|
526 | LogRel(("GIM: KVM: Failed to write wall-clock struct. at %#RGp. rc=%Rrc\n", GCPhysWallClock, rc));
|
---|
527 | return rc;
|
---|
528 | }
|
---|
529 |
|
---|
530 |
|
---|
531 | /**
|
---|
532 | * Enables the KVM wall-clock structure.
|
---|
533 | *
|
---|
534 | * Since the wall-clock can be read by any VCPU but it is a global struct. in
|
---|
535 | * guest-memory, we do an EMT rendezvous here to be on the safe side. The
|
---|
536 | * alternative is to use an MMIO2 region and use the WallClock.u32Version field
|
---|
537 | * for transactional update. However, this MSR is rarely written to (typically
|
---|
538 | * once during bootup) it's currently not a performance issue especially since
|
---|
539 | * we're already in ring-3. If we really wanted better performance in this code
|
---|
540 | * path, we should be doing it in ring-0 with transactional update while make
|
---|
541 | * sure there is only 1 writer as well.
|
---|
542 | *
|
---|
543 | * @returns VBox status code.
|
---|
544 | * @param pVM The cross context VM structure.
|
---|
545 | * @param GCPhysWallClock Where the guest wall-clock structure is located.
|
---|
546 | *
|
---|
547 | * @remarks Don't do any release assertions here, these can be triggered by
|
---|
548 | * guest R0 code.
|
---|
549 | */
|
---|
550 | VMMR3_INT_DECL(int) gimR3KvmEnableWallClock(PVM pVM, RTGCPHYS GCPhysWallClock)
|
---|
551 | {
|
---|
552 | KVMWALLCLOCKINFO WallClockInfo;
|
---|
553 | WallClockInfo.GCPhysWallClock = GCPhysWallClock;
|
---|
554 | return VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, gimR3KvmEnableWallClockCallback, &WallClockInfo);
|
---|
555 | }
|
---|
556 |
|
---|