VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/GIMHv.cpp@ 53403

Last change on this file since 53403 was 52768, checked in by vboxsync, 10 years ago

VMM/GIM: doxygen beautifications.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.9 KB
Line 
1/* $Id: GIMHv.cpp 52768 2014-09-16 17:02:30Z vboxsync $ */
2/** @file
3 * GIM - Guest Interface Manager, Hyper-V implementation.
4 */
5
6/*
7 * Copyright (C) 2014 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/assert.h>
25#include <iprt/err.h>
26#include <iprt/string.h>
27#include <iprt/mem.h>
28#include <iprt/spinlock.h>
29
30#include <VBox/vmm/cpum.h>
31#include <VBox/vmm/ssm.h>
32#include <VBox/vmm/vm.h>
33#include <VBox/vmm/hm.h>
34#include <VBox/vmm/pdmapi.h>
35#include <VBox/version.h>
36
37
38/*******************************************************************************
39* Defined Constants And Macros *
40*******************************************************************************/
41//#define GIMHV_HYPERCALL "GIMHvHypercall"
42
43/**
44 * GIM Hyper-V saved-state version.
45 */
46#define GIM_HV_SAVED_STATE_VERSION UINT32_C(1)
47
48
49/*******************************************************************************
50* Global Variables *
51*******************************************************************************/
52#ifdef VBOX_WITH_STATISTICS
53# define GIMHV_MSRRANGE(a_uFirst, a_uLast, a_szName) \
54 { (a_uFirst), (a_uLast), kCpumMsrRdFn_Gim, kCpumMsrWrFn_Gim, 0, 0, 0, 0, 0, a_szName, { 0 }, { 0 }, { 0 }, { 0 } }
55#else
56# define GIMHV_MSRRANGE(a_uFirst, a_uLast, a_szName) \
57 { (a_uFirst), (a_uLast), kCpumMsrRdFn_Gim, kCpumMsrWrFn_Gim, 0, 0, 0, 0, 0, a_szName }
58#endif
59
60/**
61 * Array of MSR ranges supported by Hyper-V.
62 */
63static CPUMMSRRANGE const g_aMsrRanges_HyperV[] =
64{
65 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE0_START, MSR_GIM_HV_RANGE0_END, "Hyper-V range 0"),
66 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE1_START, MSR_GIM_HV_RANGE1_END, "Hyper-V range 1"),
67 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE2_START, MSR_GIM_HV_RANGE2_END, "Hyper-V range 2"),
68 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE3_START, MSR_GIM_HV_RANGE3_END, "Hyper-V range 3"),
69 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE4_START, MSR_GIM_HV_RANGE4_END, "Hyper-V range 4"),
70 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE5_START, MSR_GIM_HV_RANGE5_END, "Hyper-V range 5"),
71 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE6_START, MSR_GIM_HV_RANGE6_END, "Hyper-V range 6"),
72 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE7_START, MSR_GIM_HV_RANGE7_END, "Hyper-V range 7"),
73 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE8_START, MSR_GIM_HV_RANGE8_END, "Hyper-V range 8"),
74 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE9_START, MSR_GIM_HV_RANGE9_END, "Hyper-V range 9"),
75 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE10_START, MSR_GIM_HV_RANGE10_END, "Hyper-V range 10"),
76 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE11_START, MSR_GIM_HV_RANGE11_END, "Hyper-V range 11")
77};
78#undef GIMHV_MSR
79
80
81/**
82 * Initializes the Hyper-V GIM provider.
83 *
84 * @returns VBox status code.
85 * @param pVM Pointer to the VM.
86 * @param uVersion The interface version this VM should use.
87 */
88VMMR3_INT_DECL(int) GIMR3HvInit(PVM pVM)
89{
90 AssertReturn(pVM, VERR_INVALID_PARAMETER);
91 AssertReturn(pVM->gim.s.enmProviderId == GIMPROVIDERID_HYPERV, VERR_INTERNAL_ERROR_5);
92
93 int rc;
94 PGIMHV pHv = &pVM->gim.s.u.Hv;
95
96 /*
97 * Determine interface capabilities based on the version.
98 */
99 if (!pVM->gim.s.u32Version)
100 {
101 /* Basic features. */
102 pHv->uBaseFeat = 0
103 //| GIM_HV_BASE_FEAT_VP_RUNTIME_MSR
104 | GIM_HV_BASE_FEAT_PART_TIME_REF_COUNT_MSR
105 //| GIM_HV_BASE_FEAT_BASIC_SYNTH_IC
106 //| GIM_HV_BASE_FEAT_SYNTH_TIMER_MSRS
107 | GIM_HV_BASE_FEAT_APIC_ACCESS_MSRS
108 | GIM_HV_BASE_FEAT_HYPERCALL_MSRS
109 | GIM_HV_BASE_FEAT_VP_ID_MSR
110 | GIM_HV_BASE_FEAT_VIRT_SYS_RESET_MSR
111 //| GIM_HV_BASE_FEAT_STAT_PAGES_MSR
112 | GIM_HV_BASE_FEAT_PART_REF_TSC_MSR
113 //| GIM_HV_BASE_FEAT_GUEST_IDLE_STATE_MSR
114 | GIM_HV_BASE_FEAT_TIMER_FREQ_MSRS
115 //| GIM_HV_BASE_FEAT_DEBUG_MSRS
116 ;
117
118 /* Miscellaneous features. */
119 pHv->uMiscFeat = GIM_HV_MISC_FEAT_TIMER_FREQ;
120
121 /* Hypervisor recommendations to the guest. */
122 pHv->uHyperHints = GIM_HV_HINT_MSR_FOR_SYS_RESET
123 | GIM_HV_HINT_RELAX_TIME_CHECKS;
124 }
125
126 /*
127 * Populate the required fields in MMIO2 region records for registering.
128 */
129 AssertCompile(GIM_HV_PAGE_SIZE == PAGE_SIZE);
130 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
131 pRegion->iRegion = GIM_HV_HYPERCALL_PAGE_REGION_IDX;
132 pRegion->fRCMapping = false;
133 pRegion->cbRegion = PAGE_SIZE;
134 pRegion->GCPhysPage = NIL_RTGCPHYS;
135 RTStrCopy(pRegion->szDescription, sizeof(pRegion->szDescription), "Hyper-V hypercall page");
136
137 pRegion = &pHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
138 pRegion->iRegion = GIM_HV_REF_TSC_PAGE_REGION_IDX;
139 pRegion->fRCMapping = false;
140 pRegion->cbRegion = PAGE_SIZE;
141 pRegion->GCPhysPage = NIL_RTGCPHYS;
142 RTStrCopy(pRegion->szDescription, sizeof(pRegion->szDescription), "Hyper-V TSC page");
143
144 /*
145 * Make sure the CPU ID bit are in accordance to the Hyper-V
146 * requirement and other paranoia checks.
147 * See "Requirements for implementing the Microsoft hypervisor interface" spec.
148 */
149 Assert(!(pHv->uPartFlags & ( GIM_HV_PART_FLAGS_CREATE_PART
150 | GIM_HV_PART_FLAGS_ACCESS_MEMORY_POOL
151 | GIM_HV_PART_FLAGS_ACCESS_PART_ID
152 | GIM_HV_PART_FLAGS_ADJUST_MSG_BUFFERS
153 | GIM_HV_PART_FLAGS_CREATE_PORT
154 | GIM_HV_PART_FLAGS_ACCESS_STATS
155 | GIM_HV_PART_FLAGS_CPU_MGMT
156 | GIM_HV_PART_FLAGS_CPU_PROFILER)));
157 Assert((pHv->uBaseFeat & (GIM_HV_BASE_FEAT_HYPERCALL_MSRS | GIM_HV_BASE_FEAT_VP_ID_MSR))
158 == (GIM_HV_BASE_FEAT_HYPERCALL_MSRS | GIM_HV_BASE_FEAT_VP_ID_MSR));
159 for (unsigned i = 0; i < RT_ELEMENTS(pHv->aMmio2Regions); i++)
160 {
161 PCGIMMMIO2REGION pcCur = &pHv->aMmio2Regions[i];
162 Assert(!pcCur->fRCMapping);
163 Assert(!pcCur->fMapped);
164 Assert(pcCur->GCPhysPage == NIL_RTGCPHYS);
165 }
166
167 /*
168 * Expose HVP (Hypervisor Present) bit to the guest.
169 */
170 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_HVP);
171
172 /*
173 * Modify the standard hypervisor leaves for Hyper-V.
174 */
175 CPUMCPUIDLEAF HyperLeaf;
176 RT_ZERO(HyperLeaf);
177 HyperLeaf.uLeaf = UINT32_C(0x40000000);
178 HyperLeaf.uEax = UINT32_C(0x40000006); /* Minimum value for Hyper-V is 0x40000005. */
179 HyperLeaf.uEbx = 0x7263694D; /* 'Micr' */
180 HyperLeaf.uEcx = 0x666F736F; /* 'osof' */
181 HyperLeaf.uEdx = 0x76482074; /* 't Hv' */
182 rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
183 AssertLogRelRCReturn(rc, rc);
184
185 HyperLeaf.uLeaf = UINT32_C(0x40000001);
186 HyperLeaf.uEax = 0x31237648; /* 'Hv#1' */
187 HyperLeaf.uEbx = 0; /* Reserved */
188 HyperLeaf.uEcx = 0; /* Reserved */
189 HyperLeaf.uEdx = 0; /* Reserved */
190 rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
191 AssertLogRelRCReturn(rc, rc);
192
193 /*
194 * Add Hyper-V specific leaves.
195 */
196 HyperLeaf.uLeaf = UINT32_C(0x40000002); /* MBZ until MSR_GIM_HV_GUEST_OS_ID is set by the guest. */
197 HyperLeaf.uEax = 0;
198 HyperLeaf.uEbx = 0;
199 HyperLeaf.uEcx = 0;
200 HyperLeaf.uEdx = 0;
201 rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
202 AssertLogRelRCReturn(rc, rc);
203
204 HyperLeaf.uLeaf = UINT32_C(0x40000003);
205 HyperLeaf.uEax = pHv->uBaseFeat;
206 HyperLeaf.uEbx = pHv->uPartFlags;
207 HyperLeaf.uEcx = pHv->uPowMgmtFeat;
208 HyperLeaf.uEdx = pHv->uMiscFeat;
209 rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
210 AssertLogRelRCReturn(rc, rc);
211
212 HyperLeaf.uLeaf = UINT32_C(0x40000004);
213 HyperLeaf.uEax = pHv->uHyperHints;
214 HyperLeaf.uEbx = 0xffffffff;
215 HyperLeaf.uEcx = 0;
216 HyperLeaf.uEdx = 0;
217 rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
218 AssertLogRelRCReturn(rc, rc);
219
220 /*
221 * Insert all MSR ranges of Hyper-V.
222 */
223 for (unsigned i = 0; i < RT_ELEMENTS(g_aMsrRanges_HyperV); i++)
224 {
225 rc = CPUMR3MsrRangesInsert(pVM, &g_aMsrRanges_HyperV[i]);
226 AssertLogRelRCReturn(rc, rc);
227 }
228
229 return VINF_SUCCESS;
230}
231
232
233/**
234 * Initializes remaining bits of the Hyper-V provider.
235 *
236 * This is called after initializing HM and almost all other VMM components.
237 *
238 * @returns VBox status code.
239 * @param pVM Pointer to the VM.
240 */
241VMMR3_INT_DECL(int) GIMR3HvInitCompleted(PVM pVM)
242{
243 PGIMHV pHv = &pVM->gim.s.u.Hv;
244
245 /*
246 * Determine interface capabilities based on the version.
247 */
248 if (!pVM->gim.s.u32Version)
249 {
250 /* Hypervisor capabilities; features used by the hypervisor. */
251 pHv->uHyperCaps = HMIsNestedPagingActive(pVM) ? GIM_HV_HOST_FEAT_NESTED_PAGING : 0;
252 pHv->uHyperCaps |= HMAreMsrBitmapsAvailable(pVM) ? GIM_HV_HOST_FEAT_MSR_BITMAP : 0;
253 }
254
255 CPUMCPUIDLEAF HyperLeaf;
256 RT_ZERO(HyperLeaf);
257 HyperLeaf.uLeaf = UINT32_C(0x40000006);
258 HyperLeaf.uEax = pHv->uHyperCaps;
259 HyperLeaf.uEbx = 0;
260 HyperLeaf.uEcx = 0;
261 HyperLeaf.uEdx = 0;
262 int rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
263 AssertLogRelRCReturn(rc, rc);
264
265 return rc;
266}
267
268
269#if 0
270VMMR3_INT_DECL(int) GIMR3HvInitFinalize(PVM pVM)
271{
272 pVM->gim.s.pfnHypercallR3 = &GIMHvHypercall;
273 if (!HMIsEnabled(pVM))
274 {
275 rc = PDMR3LdrGetSymbolRC(pVM, NULL /* pszModule */, GIMHV_HYPERCALL, &pVM->gim.s.pfnHypercallRC);
276 AssertRCReturn(rc, rc);
277 }
278 rc = PDMR3LdrGetSymbolR0(pVM, NULL /* pszModule */, GIMHV_HYPERCALL, &pVM->gim.s.pfnHypercallR0);
279 AssertRCReturn(rc, rc);
280}
281#endif
282
283
284/**
285 * Terminates the Hyper-V GIM provider.
286 *
287 * @returns VBox status code.
288 * @param pVM Pointer to the VM.
289 */
290VMMR3_INT_DECL(int) GIMR3HvTerm(PVM pVM)
291{
292 GIMR3HvReset(pVM);
293 return VINF_SUCCESS;
294}
295
296
297/**
298 * Applies relocations to data and code managed by this component.
299 *
300 * This function will be called at init and whenever the VMM need to relocate
301 * itself inside the GC.
302 *
303 * @param pVM Pointer to the VM.
304 * @param offDelta Relocation delta relative to old location.
305 */
306VMMR3_INT_DECL(void) GIMR3HvRelocate(PVM pVM, RTGCINTPTR offDelta)
307{
308#if 0
309 int rc = PDMR3LdrGetSymbolRC(pVM, NULL /* pszModule */, GIMHV_HYPERCALL, &pVM->gim.s.pfnHypercallRC);
310 AssertFatalRC(rc);
311#endif
312}
313
314
315/**
316 * This resets Hyper-V provider MSRs and unmaps whatever Hyper-V regions that
317 * the guest may have mapped.
318 *
319 * This is called when the VM is being reset.
320 *
321 * @param pVM Pointer to the VM.
322 */
323VMMR3_INT_DECL(void) GIMR3HvReset(PVM pVM)
324{
325 /*
326 * Unmap MMIO2 pages that the guest may have setup.
327 */
328 LogRel(("GIM: HyperV: Resetting Hyper-V MMIO2 regions and MSRs\n"));
329 PGIMHV pHv = &pVM->gim.s.u.Hv;
330 for (unsigned i = 0; i < RT_ELEMENTS(pHv->aMmio2Regions); i++)
331 {
332 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[i];
333 GIMR3Mmio2Unmap(pVM, pRegion);
334 }
335
336 /*
337 * Reset MSRs.
338 */
339 pHv->u64GuestOsIdMsr = 0;
340 pHv->u64HypercallMsr = 0;
341 pHv->u64TscPageMsr = 0;
342}
343
344
345/**
346 * Returns a pointer to the MMIO2 regions supported by Hyper-V.
347 *
348 * @returns Pointer to an array of MMIO2 regions.
349 * @param pVM Pointer to the VM.
350 * @param pcRegions Where to store the number of regions in the array.
351 */
352VMMR3_INT_DECL(PGIMMMIO2REGION) GIMR3HvGetMmio2Regions(PVM pVM, uint32_t *pcRegions)
353{
354 Assert(GIMIsEnabled(pVM));
355 PGIMHV pHv = &pVM->gim.s.u.Hv;
356
357 *pcRegions = RT_ELEMENTS(pHv->aMmio2Regions);
358 Assert(*pcRegions <= UINT8_MAX); /* See PGMR3PhysMMIO2Register(). */
359 return pHv->aMmio2Regions;
360}
361
362
363/**
364 * Hyper-V state-save operation.
365 *
366 * @returns VBox status code.
367 * @param pVM Pointer to the VM.
368 * @param pSSM Pointer to the SSM handle.
369 */
370VMMR3_INT_DECL(int) GIMR3HvSave(PVM pVM, PSSMHANDLE pSSM)
371{
372 PCGIMHV pcHv = &pVM->gim.s.u.Hv;
373
374 /*
375 * Save the Hyper-V SSM version.
376 */
377 SSMR3PutU32(pSSM, GIM_HV_SAVED_STATE_VERSION);
378
379 /** @todo Save per-VCPU data. */
380
381 /*
382 * Save per-VM MSRs.
383 */
384 SSMR3PutU64(pSSM, pcHv->u64GuestOsIdMsr);
385 SSMR3PutU64(pSSM, pcHv->u64HypercallMsr);
386 SSMR3PutU64(pSSM, pcHv->u64TscPageMsr);
387
388 /*
389 * Save Hyper-V features / capabilities.
390 */
391 SSMR3PutU32(pSSM, pcHv->uBaseFeat);
392 SSMR3PutU32(pSSM, pcHv->uPartFlags);
393 SSMR3PutU32(pSSM, pcHv->uPowMgmtFeat);
394 SSMR3PutU32(pSSM, pcHv->uMiscFeat);
395 SSMR3PutU32(pSSM, pcHv->uHyperHints);
396 SSMR3PutU32(pSSM, pcHv->uHyperCaps);
397
398 /*
399 * Save the Hypercall region.
400 */
401 PCGIMMMIO2REGION pcRegion = &pcHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
402 SSMR3PutU8(pSSM, pcRegion->iRegion);
403 SSMR3PutBool(pSSM, pcRegion->fRCMapping);
404 SSMR3PutU32(pSSM, pcRegion->cbRegion);
405 SSMR3PutGCPhys(pSSM, pcRegion->GCPhysPage);
406 SSMR3PutStrZ(pSSM, pcRegion->szDescription);
407
408 /*
409 * Save the reference TSC region.
410 */
411 pcRegion = &pcHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
412 SSMR3PutU8(pSSM, pcRegion->iRegion);
413 SSMR3PutBool(pSSM, pcRegion->fRCMapping);
414 SSMR3PutU32(pSSM, pcRegion->cbRegion);
415 SSMR3PutGCPhys(pSSM, pcRegion->GCPhysPage);
416 SSMR3PutStrZ(pSSM, pcRegion->szDescription);
417 /* Save the TSC sequence so we can bump it on restore (as the CPU frequency/offset may change). */
418 uint32_t uTscSequence = 0;
419 if ( pcRegion->fMapped
420 && MSR_GIM_HV_REF_TSC_IS_ENABLED(pcHv->u64TscPageMsr))
421 {
422 PCGIMHVREFTSC pcRefTsc = (PCGIMHVREFTSC)pcRegion->pvPageR3;
423 uTscSequence = pcRefTsc->u32TscSequence;
424 }
425
426 return SSMR3PutU32(pSSM, uTscSequence);
427}
428
429
430/**
431 * Hyper-V state-load operation, final pass.
432 *
433 * @returns VBox status code.
434 * @param pVM Pointer to the VM.
435 * @param pSSM Pointer to the SSM handle.
436 * @param uSSMVersion The GIM saved-state version.
437 */
438VMMR3_INT_DECL(int) GIMR3HvLoad(PVM pVM, PSSMHANDLE pSSM, uint32_t uSSMVersion)
439{
440 PGIMHV pHv = &pVM->gim.s.u.Hv;
441
442 /*
443 * Load the Hyper-V SSM version first.
444 */
445 uint32_t uHvSavedStatVersion;
446 int rc = SSMR3GetU32(pSSM, &uHvSavedStatVersion);
447 AssertRCReturn(rc, rc);
448 if (uHvSavedStatVersion != GIM_HV_SAVED_STATE_VERSION)
449 return SSMR3SetLoadError(pSSM, VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION, RT_SRC_POS,
450 N_("Unsupported Hyper-V saved state version %u (expected %u)."),
451 uHvSavedStatVersion, GIM_HV_SAVED_STATE_VERSION);
452
453
454 /** @todo Load per-VCPU data. */
455
456 /*
457 * Load per-VM MSRs.
458 */
459 SSMR3GetU64(pSSM, &pHv->u64GuestOsIdMsr);
460 SSMR3GetU64(pSSM, &pHv->u64HypercallMsr);
461 SSMR3GetU64(pSSM, &pHv->u64TscPageMsr);
462
463 /*
464 * Load Hyper-V features / capabilities.
465 */
466 SSMR3GetU32(pSSM, &pHv->uBaseFeat);
467 SSMR3GetU32(pSSM, &pHv->uPartFlags);
468 SSMR3GetU32(pSSM, &pHv->uPowMgmtFeat);
469 SSMR3GetU32(pSSM, &pHv->uMiscFeat);
470 SSMR3GetU32(pSSM, &pHv->uHyperHints);
471 SSMR3GetU32(pSSM, &pHv->uHyperCaps);
472
473 /*
474 * Load and enable the Hypercall region.
475 */
476 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
477 SSMR3GetU8(pSSM, &pRegion->iRegion);
478 SSMR3GetBool(pSSM, &pRegion->fRCMapping);
479 SSMR3GetU32(pSSM, &pRegion->cbRegion);
480 SSMR3GetGCPhys(pSSM, &pRegion->GCPhysPage);
481 rc = SSMR3GetStrZ(pSSM, pRegion->szDescription, sizeof(pRegion->szDescription));
482 AssertRCReturn(rc, rc);
483 if (MSR_GIM_HV_HYPERCALL_IS_ENABLED(pHv->u64HypercallMsr))
484 {
485 Assert(pRegion->GCPhysPage != NIL_RTGCPHYS);
486 if (RT_LIKELY(pRegion->fRegistered))
487 {
488 rc = GIMR3HvEnableHypercallPage(pVM, pRegion->GCPhysPage);
489 if (RT_FAILURE(rc))
490 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Failed to enable the hypercall page. GCPhys=%#RGp rc=%Rrc"),
491 pRegion->GCPhysPage, rc);
492 }
493 else
494 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Hypercall MMIO2 region not registered. Missing GIM device?!"));
495 }
496
497 /*
498 * Load and enable the reference TSC region.
499 */
500 uint32_t uTscSequence;
501 pRegion = &pHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
502 SSMR3GetU8(pSSM, &pRegion->iRegion);
503 SSMR3GetBool(pSSM, &pRegion->fRCMapping);
504 SSMR3GetU32(pSSM, &pRegion->cbRegion);
505 SSMR3GetGCPhys(pSSM, &pRegion->GCPhysPage);
506 SSMR3GetStrZ(pSSM, pRegion->szDescription, sizeof(pRegion->szDescription));
507 rc = SSMR3GetU32(pSSM, &uTscSequence);
508 AssertRCReturn(rc, rc);
509 if (MSR_GIM_HV_REF_TSC_IS_ENABLED(pHv->u64TscPageMsr))
510 {
511 Assert(pRegion->GCPhysPage != NIL_RTGCPHYS);
512 if (pRegion->fRegistered)
513 {
514 rc = GIMR3HvEnableTscPage(pVM, pRegion->GCPhysPage, true /* fUseThisTscSeq */, uTscSequence);
515 if (RT_FAILURE(rc))
516 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Failed to enable the TSC page. GCPhys=%#RGp rc=%Rrc"),
517 pRegion->GCPhysPage, rc);
518 }
519 else
520 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("TSC-page MMIO2 region not registered. Missing GIM device?!"));
521 }
522
523 return rc;
524}
525
526
527/**
528 * Enables the Hyper-V TSC page.
529 *
530 * @returns VBox status code.
531 * @param pVM Pointer to the VM.
532 * @param GCPhysTscPage Where to map the TSC page.
533 * @param fUseThisTscSequence Whether to set the TSC sequence number to
534 * the one specified in @a uTscSequence.
535 * @param uTscSequence The TSC sequence value to use. Ignored if @a
536 * fUseThisTscSequence is false.
537 */
538VMMR3_INT_DECL(int) GIMR3HvEnableTscPage(PVM pVM, RTGCPHYS GCPhysTscPage, bool fUseThisTscSequence, uint32_t uTscSequence)
539{
540 PPDMDEVINSR3 pDevIns = pVM->gim.s.pDevInsR3;
541 PGIMMMIO2REGION pRegion = &pVM->gim.s.u.Hv.aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
542 AssertPtrReturn(pDevIns, VERR_GIM_DEVICE_NOT_REGISTERED);
543
544 int rc;
545 if (pRegion->fMapped)
546 {
547 /*
548 * Is it already enabled at the given guest-address?
549 */
550 if (pRegion->GCPhysPage == GCPhysTscPage)
551 return VINF_SUCCESS;
552
553 /*
554 * If it's mapped at a different address, unmap the previous address.
555 */
556 rc = GIMR3HvDisableTscPage(pVM);
557 AssertRC(rc);
558 }
559
560 /*
561 * Map the TSC-page at the specified address.
562 */
563 Assert(!pRegion->fMapped);
564 rc = GIMR3Mmio2Map(pVM, pRegion, GCPhysTscPage);
565 if (RT_SUCCESS(rc))
566 {
567 Assert(pRegion->GCPhysPage == GCPhysTscPage);
568
569 /*
570 * Update the TSC scale. Windows guests expect a non-zero TSC sequence, otherwise
571 * they fallback to using the reference count MSR which is not ideal in terms of VM-exits.
572 *
573 * Also, Hyper-V normalizes the time in 10 MHz, see:
574 * http://technet.microsoft.com/it-it/sysinternals/dn553408%28v=vs.110%29
575 */
576 PGIMHVREFTSC pRefTsc = (PGIMHVREFTSC)pRegion->pvPageR3;
577 Assert(pRefTsc);
578
579 uint64_t const u64TscKHz = TMCpuTicksPerSecond(pVM) / UINT64_C(1000);
580 uint32_t u32TscSeq = 1;
581 if ( fUseThisTscSequence
582 && uTscSequence < UINT32_C(0xfffffffe))
583 {
584 u32TscSeq = uTscSequence + 1;
585 }
586 pRefTsc->u32TscSequence = u32TscSeq;
587 pRefTsc->u64TscScale = ((INT64_C(10000) << 32) / u64TscKHz) << 32;
588
589 LogRel(("GIM: HyperV: Enabled TSC page at %#RGp - u64TscScale=%#RX64 u64TscKHz=%#RX64 (%'RU64)\n", GCPhysTscPage,
590 pRefTsc->u64TscScale, u64TscKHz, u64TscKHz));
591 return VINF_SUCCESS;
592 }
593 else
594 LogRelFunc(("GIMR3Mmio2Map failed. rc=%Rrc\n", rc));
595
596 return VERR_GIM_OPERATION_FAILED;
597}
598
599
600/**
601 * Disables the Hyper-V TSC page.
602 *
603 * @returns VBox status code.
604 * @param pVM Pointer to the VM.
605 */
606VMMR3_INT_DECL(int) GIMR3HvDisableTscPage(PVM pVM)
607{
608 PGIMHV pHv = &pVM->gim.s.u.Hv;
609 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
610 if (pRegion->fMapped)
611 {
612 GIMR3Mmio2Unmap(pVM, pRegion);
613 Assert(!pRegion->fMapped);
614 LogRel(("GIM: HyperV: Disabled TSC-page\n"));
615 return VINF_SUCCESS;
616 }
617 return VERR_GIM_PVTSC_NOT_ENABLED;
618}
619
620
621/**
622 * Disables the Hyper-V Hypercall page.
623 *
624 * @returns VBox status code.
625 */
626VMMR3_INT_DECL(int) GIMR3HvDisableHypercallPage(PVM pVM)
627{
628 PGIMHV pHv = &pVM->gim.s.u.Hv;
629 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
630 if (pRegion->fMapped)
631 {
632 GIMR3Mmio2Unmap(pVM, pRegion);
633 Assert(!pRegion->fMapped);
634 LogRel(("GIM: HyperV: Disabled Hypercall-page\n"));
635 return VINF_SUCCESS;
636 }
637 return VERR_GIM_HYPERCALLS_NOT_ENABLED;
638}
639
640
641/**
642 * Enables the Hyper-V Hypercall page.
643 *
644 * @returns VBox status code.
645 * @param pVM Pointer to the VM.
646 * @param GCPhysHypercallPage Where to map the hypercall page.
647 */
648VMMR3_INT_DECL(int) GIMR3HvEnableHypercallPage(PVM pVM, RTGCPHYS GCPhysHypercallPage)
649{
650 PPDMDEVINSR3 pDevIns = pVM->gim.s.pDevInsR3;
651 PGIMMMIO2REGION pRegion = &pVM->gim.s.u.Hv.aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
652 AssertPtrReturn(pDevIns, VERR_GIM_DEVICE_NOT_REGISTERED);
653
654 if (pRegion->fMapped)
655 {
656 /*
657 * Is it already enabled at the given guest-address?
658 */
659 if (pRegion->GCPhysPage == GCPhysHypercallPage)
660 return VINF_SUCCESS;
661
662 /*
663 * If it's mapped at a different address, unmap the previous address.
664 */
665 int rc2 = GIMR3HvDisableHypercallPage(pVM);
666 AssertRC(rc2);
667 }
668
669 /*
670 * Map the hypercall-page at the specified address.
671 */
672 Assert(!pRegion->fMapped);
673 int rc = GIMR3Mmio2Map(pVM, pRegion, GCPhysHypercallPage);
674 if (RT_SUCCESS(rc))
675 {
676 Assert(pRegion->GCPhysPage == GCPhysHypercallPage);
677
678 /*
679 * Patch the hypercall-page.
680 */
681 if (HMIsEnabled(pVM))
682 {
683 size_t cbWritten = 0;
684 rc = HMPatchHypercall(pVM, pRegion->pvPageR3, PAGE_SIZE, &cbWritten);
685 if ( RT_SUCCESS(rc)
686 && cbWritten < PAGE_SIZE)
687 {
688 uint8_t *pbLast = (uint8_t *)pRegion->pvPageR3 + cbWritten;
689 *pbLast = 0xc3; /* RET */
690
691 LogRel(("GIM: HyperV: Enabled hypercalls at %#RGp\n", GCPhysHypercallPage));
692 return VINF_SUCCESS;
693 }
694 else
695 {
696 if (rc == VINF_SUCCESS)
697 rc = VERR_GIM_OPERATION_FAILED;
698 LogRelFunc(("HMPatchHypercall failed. rc=%Rrc cbWritten=%u\n", rc, cbWritten));
699 }
700 }
701 else
702 {
703 /** @todo Handle raw-mode hypercall page patching. */
704 LogRel(("GIM: HyperV: Raw-mode hypercalls not yet implemented!\n"));
705 }
706 GIMR3Mmio2Unmap(pVM, pRegion);
707 }
708 else
709 LogRelFunc(("GIMR3Mmio2Map failed. rc=%Rrc\n", rc));
710
711 return rc;
712}
713
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