VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/GIMKvm.cpp@ 55384

Last change on this file since 55384 was 55129, checked in by vboxsync, 10 years ago

VMM/GIM: Allow dynamic enabling of #UD traps and per-VCPU hypercalls.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.7 KB
Line 
1/* $Id: GIMKvm.cpp 55129 2015-04-08 11:31:47Z 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/*******************************************************************************
51* Global Variables *
52*******************************************************************************/
53#ifdef VBOX_WITH_STATISTICS
54# define GIMKVM_MSRRANGE(a_uFirst, a_uLast, a_szName) \
55 { (a_uFirst), (a_uLast), kCpumMsrRdFn_Gim, kCpumMsrWrFn_Gim, 0, 0, 0, 0, 0, a_szName, { 0 }, { 0 }, { 0 }, { 0 } }
56#else
57# define GIMKVM_MSRRANGE(a_uFirst, a_uLast, a_szName) \
58 { (a_uFirst), (a_uLast), kCpumMsrRdFn_Gim, kCpumMsrWrFn_Gim, 0, 0, 0, 0, 0, a_szName }
59#endif
60
61/**
62 * Array of MSR ranges supported by KVM.
63 */
64static CPUMMSRRANGE const g_aMsrRanges_Kvm[] =
65{
66 GIMKVM_MSRRANGE(MSR_GIM_KVM_RANGE0_START, MSR_GIM_KVM_RANGE0_END, "KVM range 0"),
67 GIMKVM_MSRRANGE(MSR_GIM_KVM_RANGE1_START, MSR_GIM_KVM_RANGE1_END, "KVM range 1")
68};
69#undef GIMKVM_MSRRANGE
70
71
72/**
73 * Initializes the KVM GIM provider.
74 *
75 * @returns VBox status code.
76 * @param pVM Pointer to the VM.
77 * @param uVersion The interface version this VM should use.
78 */
79VMMR3_INT_DECL(int) gimR3KvmInit(PVM pVM)
80{
81 AssertReturn(pVM, VERR_INVALID_PARAMETER);
82 AssertReturn(pVM->gim.s.enmProviderId == GIMPROVIDERID_KVM, VERR_INTERNAL_ERROR_5);
83
84 int rc;
85 PGIMKVM pKvm = &pVM->gim.s.u.Kvm;
86
87 /*
88 * Determine interface capabilities based on the version.
89 */
90 if (!pVM->gim.s.u32Version)
91 {
92 /* Basic features. */
93 pKvm->uBaseFeat = 0
94 | GIM_KVM_BASE_FEAT_CLOCK_OLD
95 //| GIM_KVM_BASE_FEAT_NOP_IO_DELAY
96 //| GIM_KVM_BASE_FEAT_MMU_OP
97 | GIM_KVM_BASE_FEAT_CLOCK
98 //| GIM_KVM_BASE_FEAT_ASYNC_PF
99 //| GIM_KVM_BASE_FEAT_STEAL_TIME
100 //| GIM_KVM_BASE_FEAT_PV_EOI
101 | GIM_KVM_BASE_FEAT_PV_UNHALT
102 ;
103 /* Rest of the features are determined in gimR3KvmInitCompleted(). */
104 }
105
106 /*
107 * Expose HVP (Hypervisor Present) bit to the guest.
108 */
109 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_HVP);
110
111 /*
112 * Modify the standard hypervisor leaves for KVM.
113 */
114 CPUMCPUIDLEAF HyperLeaf;
115 RT_ZERO(HyperLeaf);
116 HyperLeaf.uLeaf = UINT32_C(0x40000000);
117 HyperLeaf.uEax = UINT32_C(0x40000001); /* Minimum value for KVM is 0x40000001. */
118 HyperLeaf.uEbx = 0x4B4D564B; /* 'KVMK' */
119 HyperLeaf.uEcx = 0x564B4D56; /* 'VMKV' */
120 HyperLeaf.uEdx = 0x0000004D; /* 'M000' */
121 rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
122 AssertLogRelRCReturn(rc, rc);
123
124 /*
125 * Add KVM specific leaves.
126 */
127 HyperLeaf.uLeaf = UINT32_C(0x40000001);
128 HyperLeaf.uEax = pKvm->uBaseFeat;
129 HyperLeaf.uEbx = 0; /* Reserved */
130 HyperLeaf.uEcx = 0; /* Reserved */
131 HyperLeaf.uEdx = 0; /* Reserved */
132 rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
133 AssertLogRelRCReturn(rc, rc);
134
135 /*
136 * Insert all MSR ranges of KVM.
137 */
138 for (unsigned i = 0; i < RT_ELEMENTS(g_aMsrRanges_Kvm); i++)
139 {
140 rc = CPUMR3MsrRangesInsert(pVM, &g_aMsrRanges_Kvm[i]);
141 AssertLogRelRCReturn(rc, rc);
142 }
143
144 /*
145 * Setup hypercall and #UD handling.
146 */
147 for (VMCPUID i = 0; i < pVM->cCpus; i++)
148 VMMHypercallsEnable(&pVM->aCpus[i]);
149
150 if (ASMIsAmdCpu())
151 {
152 pKvm->fTrapXcptUD = true;
153 pKvm->uOpCodeNative = OP_VMMCALL;
154 }
155 else
156 {
157 Assert(ASMIsIntelCpu() || ASMIsViaCentaurCpu());
158 pKvm->fTrapXcptUD = false;
159 pKvm->uOpCodeNative = OP_VMCALL;
160 }
161
162 /* We always need to trap VMCALL/VMMCALL hypercall using #UDs for raw-mode VMs. */
163 if (!HMIsEnabled(pVM))
164 pKvm->fTrapXcptUD = true;
165
166 return VINF_SUCCESS;
167}
168
169
170/**
171 * Initializes remaining bits of the KVM provider.
172 *
173 * This is called after initializing HM and almost all other VMM components.
174 *
175 * @returns VBox status code.
176 * @param pVM Pointer to the VM.
177 */
178VMMR3_INT_DECL(int) gimR3KvmInitCompleted(PVM pVM)
179{
180 if (TMR3CpuTickIsFixedRateMonotonic(pVM, true /* fWithParavirtEnabled */))
181 {
182 PGIMKVM pKvm = &pVM->gim.s.u.Kvm;
183 pKvm->uBaseFeat |= GIM_KVM_BASE_FEAT_TSC_STABLE;
184
185 CPUMCPUIDLEAF HyperLeaf;
186 RT_ZERO(HyperLeaf);
187 HyperLeaf.uLeaf = UINT32_C(0x40000001);
188 HyperLeaf.uEax = pKvm->uBaseFeat;
189 HyperLeaf.uEbx = 0;
190 HyperLeaf.uEcx = 0;
191 HyperLeaf.uEdx = 0;
192 int rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
193 AssertLogRelRCReturn(rc, rc);
194 }
195
196 return VINF_SUCCESS;
197}
198
199
200/**
201 * Terminates the KVM GIM provider.
202 *
203 * @returns VBox status code.
204 * @param pVM Pointer to the VM.
205 */
206VMMR3_INT_DECL(int) gimR3KvmTerm(PVM pVM)
207{
208 gimR3KvmReset(pVM);
209 return VINF_SUCCESS;
210}
211
212
213/**
214 * Applies relocations to data and code managed by this component.
215 *
216 * This function will be called at init and whenever the VMM need to relocate
217 * itself inside the GC.
218 *
219 * @param pVM Pointer to the VM.
220 * @param offDelta Relocation delta relative to old location.
221 */
222VMMR3_INT_DECL(void) gimR3KvmRelocate(PVM pVM, RTGCINTPTR offDelta)
223{
224 NOREF(pVM); NOREF(offDelta);
225}
226
227
228/**
229 * This resets KVM provider MSRs and unmaps whatever KVM regions that
230 * the guest may have mapped.
231 *
232 * This is called when the VM is being reset.
233 *
234 * @param pVM Pointer to the VM.
235 * @thread EMT(0).
236 */
237VMMR3_INT_DECL(void) gimR3KvmReset(PVM pVM)
238{
239 VM_ASSERT_EMT0(pVM);
240
241 /*
242 * Reset MSRs.
243 */
244 PGIMKVM pKvm = &pVM->gim.s.u.Kvm;
245 pKvm->u64WallClockMsr = 0;
246 for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
247 {
248 PGIMKVMCPU pKvmCpu = &pVM->aCpus[iCpu].gim.s.u.KvmCpu;
249 pKvmCpu->u64SystemTimeMsr = 0;
250 }
251}
252
253
254/**
255 * KVM state-save operation.
256 *
257 * @returns VBox status code.
258 * @param pVM Pointer to the VM.
259 * @param pSSM Pointer to the SSM handle.
260 */
261VMMR3_INT_DECL(int) gimR3KvmSave(PVM pVM, PSSMHANDLE pSSM)
262{
263 PCGIMKVM pcKvm = &pVM->gim.s.u.Kvm;
264
265 /*
266 * Save the KVM SSM version.
267 */
268 SSMR3PutU32(pSSM, GIM_KVM_SAVED_STATE_VERSION);
269
270 /*
271 * Save per-VCPU data.
272 */
273 for (uint32_t i = 0; i < pVM->cCpus; i++)
274 {
275 PCGIMKVMCPU pcKvmCpu = &pVM->aCpus[i].gim.s.u.KvmCpu;
276
277 /* Guest may alter flags (namely GIM_KVM_SYSTEM_TIME_FLAGS_GUEST_PAUSED bit). So re-read them from guest-memory. */
278 GIMKVMSYSTEMTIME SystemTime;
279 RT_ZERO(SystemTime);
280 if (MSR_GIM_KVM_SYSTEM_TIME_IS_ENABLED(pcKvmCpu->u64SystemTimeMsr))
281 {
282 int rc = PGMPhysSimpleReadGCPhys(pVM, &SystemTime, pcKvmCpu->GCPhysSystemTime, sizeof(GIMKVMSYSTEMTIME));
283 AssertRCReturn(rc, rc);
284 }
285
286 SSMR3PutU64(pSSM, pcKvmCpu->u64SystemTimeMsr);
287 SSMR3PutU64(pSSM, pcKvmCpu->uTsc);
288 SSMR3PutU64(pSSM, pcKvmCpu->uVirtNanoTS);
289 SSMR3PutGCPhys(pSSM, pcKvmCpu->GCPhysSystemTime);
290 SSMR3PutU32(pSSM, pcKvmCpu->u32SystemTimeVersion);
291 SSMR3PutU8(pSSM, SystemTime.fFlags);
292 }
293
294 /*
295 * Save per-VM data.
296 */
297 SSMR3PutU64(pSSM, pcKvm->u64WallClockMsr);
298 return SSMR3PutU32(pSSM, pcKvm->uBaseFeat);
299}
300
301
302/**
303 * KVM state-load operation, final pass.
304 *
305 * @returns VBox status code.
306 * @param pVM Pointer to the VM.
307 * @param pSSM Pointer to the SSM handle.
308 * @param uSSMVersion The GIM saved-state version.
309 */
310VMMR3_INT_DECL(int) gimR3KvmLoad(PVM pVM, PSSMHANDLE pSSM, uint32_t uSSMVersion)
311{
312 /*
313 * Load the KVM SSM version first.
314 */
315 uint32_t uKvmSavedStatVersion;
316 int rc = SSMR3GetU32(pSSM, &uKvmSavedStatVersion);
317 AssertRCReturn(rc, rc);
318 if (uKvmSavedStatVersion != GIM_KVM_SAVED_STATE_VERSION)
319 return SSMR3SetLoadError(pSSM, VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION, RT_SRC_POS,
320 N_("Unsupported KVM saved-state version %u (expected %u)."), uKvmSavedStatVersion,
321 GIM_KVM_SAVED_STATE_VERSION);
322
323 /*
324 * Load per-VCPU data.
325 */
326 for (uint32_t i = 0; i < pVM->cCpus; i++)
327 {
328 PVMCPU pVCpu = &pVM->aCpus[i];
329 PGIMKVMCPU pKvmCpu = &pVCpu->gim.s.u.KvmCpu;
330
331 uint8_t fSystemTimeFlags = 0;
332 SSMR3GetU64(pSSM, &pKvmCpu->u64SystemTimeMsr);
333 SSMR3GetU64(pSSM, &pKvmCpu->uTsc);
334 SSMR3GetU64(pSSM, &pKvmCpu->uVirtNanoTS);
335 SSMR3GetGCPhys(pSSM, &pKvmCpu->GCPhysSystemTime);
336 SSMR3GetU32(pSSM, &pKvmCpu->u32SystemTimeVersion);
337 SSMR3GetU8(pSSM, &fSystemTimeFlags);
338
339 /* Enable the system-time struct. if necessary. */
340 if (MSR_GIM_KVM_SYSTEM_TIME_IS_ENABLED(pKvmCpu->u64SystemTimeMsr))
341 {
342 Assert(!TMVirtualIsTicking(pVM)); /* paranoia. */
343 Assert(!TMCpuTickIsTicking(pVCpu));
344 rc = gimR3KvmEnableSystemTime(pVM, pVCpu, pKvmCpu, fSystemTimeFlags);
345 AssertRCReturn(rc, rc);
346 }
347 }
348
349 /*
350 * Load per-VM data.
351 */
352 PGIMKVM pKvm = &pVM->gim.s.u.Kvm;
353 SSMR3GetU64(pSSM, &pKvm->u64WallClockMsr);
354 rc = SSMR3GetU32(pSSM, &pKvm->uBaseFeat);
355 AssertRCReturn(rc, rc);
356
357 return VINF_SUCCESS;
358}
359
360
361/**
362 * Enables the KVM VCPU system-time structure.
363 *
364 * @returns VBox status code.
365 * @param pVM Pointer to the VM.
366 * @param pVCpu Pointer to the VMCPU.
367 * @param pKvmCpu Pointer to the GIMKVMCPU with all fields
368 * populated by the caller.
369 * @param fFlags The system-time struct. flags.
370 *
371 * @remarks Don't do any release assertions here, these can be triggered by
372 * guest R0 code.
373 */
374VMMR3_INT_DECL(int) gimR3KvmEnableSystemTime(PVM pVM, PVMCPU pVCpu, PGIMKVMCPU pKvmCpu, uint8_t fFlags)
375{
376 GIMKVMSYSTEMTIME SystemTime;
377 RT_ZERO(SystemTime);
378 SystemTime.u32Version = pKvmCpu->u32SystemTimeVersion;
379 SystemTime.u64NanoTS = pKvmCpu->uVirtNanoTS;
380 SystemTime.u64Tsc = pKvmCpu->uTsc;
381 SystemTime.fFlags = fFlags | GIM_KVM_SYSTEM_TIME_FLAGS_TSC_STABLE;
382
383 /*
384 * How the guest calculates the system time (nanoseconds):
385 *
386 * tsc = rdtsc - SysTime.u64Tsc
387 * if (SysTime.i8TscShift >= 0)
388 * tsc <<= i8TscShift;
389 * else
390 * tsc >>= -i8TscShift;
391 * time = ((tsc * SysTime.u32TscScale) >> 32) + SysTime.u64NanoTS
392 */
393 uint64_t u64TscFreq = TMCpuTicksPerSecond(pVM);
394 SystemTime.i8TscShift = 0;
395 while (u64TscFreq > 2 * RT_NS_1SEC_64)
396 {
397 u64TscFreq >>= 1;
398 SystemTime.i8TscShift--;
399 }
400 uint32_t uTscFreqLo = (uint32_t)u64TscFreq;
401 while (uTscFreqLo <= RT_NS_1SEC)
402 {
403 uTscFreqLo <<= 1;
404 SystemTime.i8TscShift++;
405 }
406 SystemTime.u32TscScale = ASMDivU64ByU32RetU32(RT_NS_1SEC_64 << 32, uTscFreqLo);
407
408 Assert(!(SystemTime.u32Version & UINT32_C(1)));
409 Assert(PGMPhysIsGCPhysNormal(pVM, pKvmCpu->GCPhysSystemTime));
410 int rc = PGMPhysSimpleWriteGCPhys(pVM, pKvmCpu->GCPhysSystemTime, &SystemTime, sizeof(GIMKVMSYSTEMTIME));
411 if (RT_SUCCESS(rc))
412 {
413 LogRel(("GIM: KVM: VCPU%3d: Enabled system-time struct. at %#RGp - u32TscScale=%#RX32 i8TscShift=%d uVersion=%#RU32 "
414 "fFlags=%#x uTsc=%#RX64 uVirtNanoTS=%#RX64\n", pVCpu->idCpu, pKvmCpu->GCPhysSystemTime, SystemTime.u32TscScale,
415 SystemTime.i8TscShift, SystemTime.u32Version, SystemTime.fFlags, pKvmCpu->uTsc, pKvmCpu->uVirtNanoTS));
416 TMR3CpuTickParavirtEnable(pVM);
417 }
418 else
419 LogRel(("GIM: KVM: VCPU%3d: Failed to write system-time struct. at %#RGp. rc=%Rrc\n", pKvmCpu->GCPhysSystemTime, rc));
420
421 return rc;
422}
423
424
425/**
426 * Disables the KVM system-time struct.
427 *
428 * @returns VBox status code.
429 * @param pVM Pointer to the VM.
430 */
431VMMR3_INT_DECL(int) gimR3KvmDisableSystemTime(PVM pVM)
432{
433 TMR3CpuTickParavirtDisable(pVM);
434 return VINF_SUCCESS;
435}
436
437
438/**
439 * Enables the KVM wall-clock structure.
440 *
441 * @returns VBox status code.
442 * @param pVM Pointer to the VM.
443 * @param GCPhysWallClock Where the guest wall-clock structure is located.
444 * @param uVersion The version (sequence number) value to use.
445 *
446 * @remarks Don't do any release assertions here, these can be triggered by
447 * guest R0 code.
448 */
449VMMR3_INT_DECL(int) gimR3KvmEnableWallClock(PVM pVM, RTGCPHYS GCPhysWallClock, uint32_t uVersion)
450{
451 RTTIMESPEC TimeSpec;
452 int32_t iSec;
453 int32_t iNano;
454
455 TMR3UtcNow(pVM, &TimeSpec);
456 RTTimeSpecGetSecondsAndNano(&TimeSpec, &iSec, &iNano);
457
458 GIMKVMWALLCLOCK WallClock;
459 RT_ZERO(WallClock);
460 WallClock.u32Version = uVersion;
461 WallClock.u32Sec = iSec;
462 WallClock.u32Nano = iNano;
463
464 Assert(PGMPhysIsGCPhysNormal(pVM, GCPhysWallClock));
465 Assert(!(WallClock.u32Version & UINT32_C(1)));
466 int rc = PGMPhysSimpleWriteGCPhys(pVM, GCPhysWallClock, &WallClock, sizeof(GIMKVMWALLCLOCK));
467 if (RT_SUCCESS(rc))
468 {
469 LogRel(("GIM: KVM: Enabled wall-clock struct. at %#RGp - u32Sec=%u u32Nano=%u uVersion=%#RU32\n", GCPhysWallClock,
470 WallClock.u32Sec, WallClock.u32Nano, WallClock.u32Version));
471 }
472 else
473 LogRel(("GIM: KVM: Failed to write wall-clock struct. at %#RGp. rc=%Rrc\n", GCPhysWallClock, rc));
474
475 return rc;
476}
477
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette