VirtualBox

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

Last change on this file since 56766 was 56758, checked in by vboxsync, 10 years ago

VMM/GIM: Don't unmap MMIO2 pages on Hyper-V reset as we don't map them currently.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 28.1 KB
Line 
1/* $Id: GIMHv.cpp 56758 2015-07-02 16:33:32Z vboxsync $ */
2/** @file
3 * GIM - Guest Interface Manager, Hyper-V implementation.
4 */
5
6/*
7 * Copyright (C) 2014-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/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_MSRRANGE
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; /* Sanity checked in gimR3HvLoad(), gimR3HvEnableTscPage() & gimR3HvEnableHypercallPage() */
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; /* Sanity checked in gimR3HvLoad(), gimR3HvEnableTscPage() & gimR3HvEnableHypercallPage() */
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 pHv->cTscTicksPerSecond = TMCpuTicksPerSecond(pVM);
245
246 /*
247 * Determine interface capabilities based on the version.
248 */
249 if (!pVM->gim.s.u32Version)
250 {
251 /* Hypervisor capabilities; features used by the hypervisor. */
252 pHv->uHyperCaps = HMIsNestedPagingActive(pVM) ? GIM_HV_HOST_FEAT_NESTED_PAGING : 0;
253 pHv->uHyperCaps |= HMAreMsrBitmapsAvailable(pVM) ? GIM_HV_HOST_FEAT_MSR_BITMAP : 0;
254 }
255
256 CPUMCPUIDLEAF HyperLeaf;
257 RT_ZERO(HyperLeaf);
258 HyperLeaf.uLeaf = UINT32_C(0x40000006);
259 HyperLeaf.uEax = pHv->uHyperCaps;
260 HyperLeaf.uEbx = 0;
261 HyperLeaf.uEcx = 0;
262 HyperLeaf.uEdx = 0;
263 int rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
264 AssertLogRelRCReturn(rc, rc);
265
266 return rc;
267}
268
269
270#if 0
271VMMR3_INT_DECL(int) gimR3HvInitFinalize(PVM pVM)
272{
273 pVM->gim.s.pfnHypercallR3 = &GIMHvHypercall;
274 if (!HMIsEnabled(pVM))
275 {
276 rc = PDMR3LdrGetSymbolRC(pVM, NULL /* pszModule */, GIMHV_HYPERCALL, &pVM->gim.s.pfnHypercallRC);
277 AssertRCReturn(rc, rc);
278 }
279 rc = PDMR3LdrGetSymbolR0(pVM, NULL /* pszModule */, GIMHV_HYPERCALL, &pVM->gim.s.pfnHypercallR0);
280 AssertRCReturn(rc, rc);
281}
282#endif
283
284
285/**
286 * Terminates the Hyper-V GIM provider.
287 *
288 * @returns VBox status code.
289 * @param pVM Pointer to the VM.
290 */
291VMMR3_INT_DECL(int) gimR3HvTerm(PVM pVM)
292{
293 gimR3HvReset(pVM);
294 return VINF_SUCCESS;
295}
296
297
298/**
299 * Applies relocations to data and code managed by this component.
300 *
301 * This function will be called at init and whenever the VMM need to relocate
302 * itself inside the GC.
303 *
304 * @param pVM Pointer to the VM.
305 * @param offDelta Relocation delta relative to old location.
306 */
307VMMR3_INT_DECL(void) gimR3HvRelocate(PVM pVM, RTGCINTPTR offDelta)
308{
309#if 0
310 int rc = PDMR3LdrGetSymbolRC(pVM, NULL /* pszModule */, GIMHV_HYPERCALL, &pVM->gim.s.pfnHypercallRC);
311 AssertFatalRC(rc);
312#endif
313}
314
315
316/**
317 * This resets Hyper-V provider MSRs and unmaps whatever Hyper-V regions that
318 * the guest may have mapped.
319 *
320 * This is called when the VM is being reset.
321 *
322 * @param pVM Pointer to the VM.
323 * @thread EMT(0).
324 */
325VMMR3_INT_DECL(void) gimR3HvReset(PVM pVM)
326{
327 VM_ASSERT_EMT0(pVM);
328
329 /*
330 * Unmap MMIO2 pages that the guest may have setup.
331 */
332 LogRel(("GIM: HyperV: Resetting MMIO2 regions and MSRs\n"));
333 PGIMHV pHv = &pVM->gim.s.u.Hv;
334 for (unsigned i = 0; i < RT_ELEMENTS(pHv->aMmio2Regions); i++)
335 {
336 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[i];
337#if 0
338 GIMR3Mmio2Unmap(pVM, pRegion);
339#else
340 pRegion->fMapped = false;
341 pRegion->GCPhysPage = NIL_RTGCPHYS;
342#endif
343 }
344
345 /*
346 * Reset MSRs.
347 */
348 pHv->u64GuestOsIdMsr = 0;
349 pHv->u64HypercallMsr = 0;
350 pHv->u64TscPageMsr = 0;
351}
352
353
354/**
355 * Returns a pointer to the MMIO2 regions supported by Hyper-V.
356 *
357 * @returns Pointer to an array of MMIO2 regions.
358 * @param pVM Pointer to the VM.
359 * @param pcRegions Where to store the number of regions in the array.
360 */
361VMMR3_INT_DECL(PGIMMMIO2REGION) gimR3HvGetMmio2Regions(PVM pVM, uint32_t *pcRegions)
362{
363 Assert(GIMIsEnabled(pVM));
364 PGIMHV pHv = &pVM->gim.s.u.Hv;
365
366 *pcRegions = RT_ELEMENTS(pHv->aMmio2Regions);
367 Assert(*pcRegions <= UINT8_MAX); /* See PGMR3PhysMMIO2Register(). */
368 return pHv->aMmio2Regions;
369}
370
371
372/**
373 * Hyper-V state-save operation.
374 *
375 * @returns VBox status code.
376 * @param pVM Pointer to the VM.
377 * @param pSSM Pointer to the SSM handle.
378 */
379VMMR3_INT_DECL(int) gimR3HvSave(PVM pVM, PSSMHANDLE pSSM)
380{
381 PCGIMHV pcHv = &pVM->gim.s.u.Hv;
382
383 /*
384 * Save the Hyper-V SSM version.
385 */
386 SSMR3PutU32(pSSM, GIM_HV_SAVED_STATE_VERSION);
387
388 /*
389 * Save per-VM MSRs.
390 */
391 SSMR3PutU64(pSSM, pcHv->u64GuestOsIdMsr);
392 SSMR3PutU64(pSSM, pcHv->u64HypercallMsr);
393 SSMR3PutU64(pSSM, pcHv->u64TscPageMsr);
394
395 /*
396 * Save Hyper-V features / capabilities.
397 */
398 SSMR3PutU32(pSSM, pcHv->uBaseFeat);
399 SSMR3PutU32(pSSM, pcHv->uPartFlags);
400 SSMR3PutU32(pSSM, pcHv->uPowMgmtFeat);
401 SSMR3PutU32(pSSM, pcHv->uMiscFeat);
402 SSMR3PutU32(pSSM, pcHv->uHyperHints);
403 SSMR3PutU32(pSSM, pcHv->uHyperCaps);
404
405 /*
406 * Save the Hypercall region.
407 */
408 PCGIMMMIO2REGION pcRegion = &pcHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
409 SSMR3PutU8(pSSM, pcRegion->iRegion);
410 SSMR3PutBool(pSSM, pcRegion->fRCMapping);
411 SSMR3PutU32(pSSM, pcRegion->cbRegion);
412 SSMR3PutGCPhys(pSSM, pcRegion->GCPhysPage);
413 SSMR3PutStrZ(pSSM, pcRegion->szDescription);
414
415 /*
416 * Save the reference TSC region.
417 */
418 pcRegion = &pcHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
419 SSMR3PutU8(pSSM, pcRegion->iRegion);
420 SSMR3PutBool(pSSM, pcRegion->fRCMapping);
421 SSMR3PutU32(pSSM, pcRegion->cbRegion);
422 SSMR3PutGCPhys(pSSM, pcRegion->GCPhysPage);
423 SSMR3PutStrZ(pSSM, pcRegion->szDescription);
424 /* Save the TSC sequence so we can bump it on restore (as the CPU frequency/offset may change). */
425 uint32_t uTscSequence = 0;
426 if ( pcRegion->fMapped
427 && MSR_GIM_HV_REF_TSC_IS_ENABLED(pcHv->u64TscPageMsr))
428 {
429 PCGIMHVREFTSC pcRefTsc = (PCGIMHVREFTSC)pcRegion->pvPageR3;
430 uTscSequence = pcRefTsc->u32TscSequence;
431 }
432
433 return SSMR3PutU32(pSSM, uTscSequence);
434}
435
436
437/**
438 * Hyper-V state-load operation, final pass.
439 *
440 * @returns VBox status code.
441 * @param pVM Pointer to the VM.
442 * @param pSSM Pointer to the SSM handle.
443 * @param uSSMVersion The GIM saved-state version.
444 */
445VMMR3_INT_DECL(int) gimR3HvLoad(PVM pVM, PSSMHANDLE pSSM, uint32_t uSSMVersion)
446{
447 /*
448 * Load the Hyper-V SSM version first.
449 */
450 uint32_t uHvSavedStatVersion;
451 int rc = SSMR3GetU32(pSSM, &uHvSavedStatVersion);
452 AssertRCReturn(rc, rc);
453 if (uHvSavedStatVersion != GIM_HV_SAVED_STATE_VERSION)
454 return SSMR3SetLoadError(pSSM, VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION, RT_SRC_POS,
455 N_("Unsupported Hyper-V saved-state version %u (expected %u)."), uHvSavedStatVersion,
456 GIM_HV_SAVED_STATE_VERSION);
457
458 /*
459 * Update the TSC frequency from TM.
460 */
461 PGIMHV pHv = &pVM->gim.s.u.Hv;
462 pHv->cTscTicksPerSecond = TMCpuTicksPerSecond(pVM);
463
464 /*
465 * Load per-VM MSRs.
466 */
467 SSMR3GetU64(pSSM, &pHv->u64GuestOsIdMsr);
468 SSMR3GetU64(pSSM, &pHv->u64HypercallMsr);
469 SSMR3GetU64(pSSM, &pHv->u64TscPageMsr);
470
471 /*
472 * Load Hyper-V features / capabilities.
473 */
474 SSMR3GetU32(pSSM, &pHv->uBaseFeat);
475 SSMR3GetU32(pSSM, &pHv->uPartFlags);
476 SSMR3GetU32(pSSM, &pHv->uPowMgmtFeat);
477 SSMR3GetU32(pSSM, &pHv->uMiscFeat);
478 SSMR3GetU32(pSSM, &pHv->uHyperHints);
479 SSMR3GetU32(pSSM, &pHv->uHyperCaps);
480
481 /*
482 * Load and enable the Hypercall region.
483 */
484 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
485 SSMR3GetU8(pSSM, &pRegion->iRegion);
486 SSMR3GetBool(pSSM, &pRegion->fRCMapping);
487 SSMR3GetU32(pSSM, &pRegion->cbRegion);
488 SSMR3GetGCPhys(pSSM, &pRegion->GCPhysPage);
489 rc = SSMR3GetStrZ(pSSM, pRegion->szDescription, sizeof(pRegion->szDescription));
490 AssertRCReturn(rc, rc);
491
492 if (pRegion->cbRegion != PAGE_SIZE)
493 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Hypercall page region size %u invalid, expected %u"),
494 pRegion->cbRegion, PAGE_SIZE);
495
496 if (MSR_GIM_HV_HYPERCALL_IS_ENABLED(pHv->u64HypercallMsr))
497 {
498 Assert(pRegion->GCPhysPage != NIL_RTGCPHYS);
499 if (RT_LIKELY(pRegion->fRegistered))
500 {
501 rc = gimR3HvEnableHypercallPage(pVM, pRegion->GCPhysPage);
502 if (RT_FAILURE(rc))
503 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Failed to enable the hypercall page. GCPhys=%#RGp rc=%Rrc"),
504 pRegion->GCPhysPage, rc);
505 }
506 else
507 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Hypercall MMIO2 region not registered. Missing GIM device?!"));
508 }
509
510 /*
511 * Load and enable the reference TSC region.
512 */
513 uint32_t uTscSequence;
514 pRegion = &pHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
515 SSMR3GetU8(pSSM, &pRegion->iRegion);
516 SSMR3GetBool(pSSM, &pRegion->fRCMapping);
517 SSMR3GetU32(pSSM, &pRegion->cbRegion);
518 SSMR3GetGCPhys(pSSM, &pRegion->GCPhysPage);
519 SSMR3GetStrZ(pSSM, pRegion->szDescription, sizeof(pRegion->szDescription));
520 rc = SSMR3GetU32(pSSM, &uTscSequence);
521 AssertRCReturn(rc, rc);
522
523 if (pRegion->cbRegion != PAGE_SIZE)
524 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("TSC page region size %u invalid, expected %u"),
525 pRegion->cbRegion, PAGE_SIZE);
526
527 if (MSR_GIM_HV_REF_TSC_IS_ENABLED(pHv->u64TscPageMsr))
528 {
529 Assert(pRegion->GCPhysPage != NIL_RTGCPHYS);
530 if (pRegion->fRegistered)
531 {
532 rc = gimR3HvEnableTscPage(pVM, pRegion->GCPhysPage, true /* fUseThisTscSeq */, uTscSequence);
533 if (RT_FAILURE(rc))
534 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Failed to enable the TSC page. GCPhys=%#RGp rc=%Rrc"),
535 pRegion->GCPhysPage, rc);
536 }
537 else
538 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("TSC-page MMIO2 region not registered. Missing GIM device?!"));
539 }
540
541 return rc;
542}
543
544
545/**
546 * Enables the Hyper-V TSC page.
547 *
548 * @returns VBox status code.
549 * @param pVM Pointer to the VM.
550 * @param GCPhysTscPage Where to map the TSC page.
551 * @param fUseThisTscSeq Whether to set the TSC sequence number to the one
552 * specified in @a uTscSeq.
553 * @param uTscSeq The TSC sequence value to use. Ignored if
554 * @a fUseThisTscSeq is false.
555 */
556VMMR3_INT_DECL(int) gimR3HvEnableTscPage(PVM pVM, RTGCPHYS GCPhysTscPage, bool fUseThisTscSeq, uint32_t uTscSeq)
557{
558 PPDMDEVINSR3 pDevIns = pVM->gim.s.pDevInsR3;
559 PGIMMMIO2REGION pRegion = &pVM->gim.s.u.Hv.aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
560 AssertPtrReturn(pDevIns, VERR_GIM_DEVICE_NOT_REGISTERED);
561
562 int rc;
563 if (pRegion->fMapped)
564 {
565 /*
566 * Is it already enabled at the given guest-address?
567 */
568 if (pRegion->GCPhysPage == GCPhysTscPage)
569 return VINF_SUCCESS;
570
571 /*
572 * If it's mapped at a different address, unmap the previous address.
573 */
574 rc = gimR3HvDisableTscPage(pVM);
575 AssertRC(rc);
576 }
577
578 /*
579 * Map the TSC-page at the specified address.
580 */
581 Assert(!pRegion->fMapped);
582
583 /** @todo this is buggy when large pages are used due to a PGM limitation, see
584 * @bugref{7532}. Instead of the overlay style mapping, we just
585 * rewrite guest memory directly. */
586#if 0
587 rc = GIMR3Mmio2Map(pVM, pRegion, GCPhysTscPage);
588 if (RT_SUCCESS(rc))
589 {
590 Assert(pRegion->GCPhysPage == GCPhysTscPage);
591
592 /*
593 * Update the TSC scale. Windows guests expect a non-zero TSC sequence, otherwise
594 * they fallback to using the reference count MSR which is not ideal in terms of VM-exits.
595 *
596 * Also, Hyper-V normalizes the time in 10 MHz, see:
597 * http://technet.microsoft.com/it-it/sysinternals/dn553408%28v=vs.110%29
598 */
599 PGIMHVREFTSC pRefTsc = (PGIMHVREFTSC)pRegion->pvPageR3;
600 Assert(pRefTsc);
601
602 PGIMHV pHv = &pVM->gim.s.u.Hv;
603 uint64_t const u64TscKHz = pHv->cTscTicksPerSecond / UINT64_C(1000);
604 uint32_t u32TscSeq = 1;
605 if ( fUseThisTscSeq
606 && uTscSeq < UINT32_C(0xfffffffe))
607 u32TscSeq = uTscSeq + 1;
608 pRefTsc->u32TscSequence = u32TscSeq;
609 pRefTsc->u64TscScale = ((INT64_C(10000) << 32) / u64TscKHz) << 32;
610 pRefTsc->i64TscOffset = 0;
611
612 LogRel(("GIM: HyperV: Enabled TSC page at %#RGp - u64TscScale=%#RX64 u64TscKHz=%#RX64 (%'RU64) Seq=%#RU32\n",
613 GCPhysTscPage, pRefTsc->u64TscScale, u64TscKHz, u64TscKHz, pRefTsc->u32TscSequence));
614
615 TMR3CpuTickParavirtEnable(pVM);
616 return VINF_SUCCESS;
617 }
618 else
619 LogRelFunc(("GIMR3Mmio2Map failed. rc=%Rrc\n", rc));
620 return VERR_GIM_OPERATION_FAILED;
621#else
622 AssertReturn(pRegion->cbRegion == PAGE_SIZE, VERR_GIM_IPE_2);
623 PGIMHVREFTSC pRefTsc = (PGIMHVREFTSC)RTMemAllocZ(PAGE_SIZE);
624 if (RT_UNLIKELY(!pRefTsc))
625 {
626 LogRelFunc(("Failed to alloc %u bytes\n", PAGE_SIZE));
627 return VERR_NO_MEMORY;
628 }
629
630 PGIMHV pHv = &pVM->gim.s.u.Hv;
631 uint64_t const u64TscKHz = pHv->cTscTicksPerSecond / UINT64_C(1000);
632 uint32_t u32TscSeq = 1;
633 if ( fUseThisTscSeq
634 && uTscSeq < UINT32_C(0xfffffffe))
635 u32TscSeq = uTscSeq + 1;
636 pRefTsc->u32TscSequence = u32TscSeq;
637 pRefTsc->u64TscScale = ((INT64_C(10000) << 32) / u64TscKHz) << 32;
638 pRefTsc->i64TscOffset = 0;
639
640 rc = PGMPhysSimpleWriteGCPhys(pVM, GCPhysTscPage, pRefTsc, sizeof(*pRefTsc));
641 if (RT_SUCCESS(rc))
642 {
643 LogRel(("GIM: HyperV: Enabled TSC page at %#RGp - u64TscScale=%#RX64 u64TscKHz=%#RX64 (%'RU64) Seq=%#RU32\n",
644 GCPhysTscPage, pRefTsc->u64TscScale, u64TscKHz, u64TscKHz, pRefTsc->u32TscSequence));
645
646 pRegion->GCPhysPage = GCPhysTscPage;
647 pRegion->fMapped = true;
648 TMR3CpuTickParavirtEnable(pVM);
649 }
650 else
651 {
652 LogRelFunc(("GIM: HyperV: PGMPhysSimpleWriteGCPhys failed. rc=%Rrc\n", rc));
653 rc = VERR_GIM_OPERATION_FAILED;
654 }
655 RTMemFree(pRefTsc);
656 return rc;
657#endif
658}
659
660
661/**
662 * Disables the Hyper-V TSC page.
663 *
664 * @returns VBox status code.
665 * @param pVM Pointer to the VM.
666 */
667VMMR3_INT_DECL(int) gimR3HvDisableTscPage(PVM pVM)
668{
669 PGIMHV pHv = &pVM->gim.s.u.Hv;
670 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
671 if (pRegion->fMapped)
672 {
673#if 0
674 GIMR3Mmio2Unmap(pVM, pRegion);
675 Assert(!pRegion->fMapped);
676#else
677 pRegion->fMapped = false;
678#endif
679 LogRel(("GIM: HyperV: Disabled TSC-page\n"));
680
681 TMR3CpuTickParavirtDisable(pVM);
682 return VINF_SUCCESS;
683 }
684 return VERR_GIM_PVTSC_NOT_ENABLED;
685}
686
687
688/**
689 * Disables the Hyper-V Hypercall page.
690 *
691 * @returns VBox status code.
692 */
693VMMR3_INT_DECL(int) gimR3HvDisableHypercallPage(PVM pVM)
694{
695 PGIMHV pHv = &pVM->gim.s.u.Hv;
696 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
697 if (pRegion->fMapped)
698 {
699#if 0
700 GIMR3Mmio2Unmap(pVM, pRegion);
701 Assert(!pRegion->fMapped);
702#else
703 pRegion->fMapped = false;
704#endif
705 for (VMCPUID i = 0; i < pVM->cCpus; i++)
706 VMMHypercallsDisable(&pVM->aCpus[i]);
707 LogRel(("GIM: HyperV: Disabled Hypercall-page\n"));
708 return VINF_SUCCESS;
709 }
710 return VERR_GIM_HYPERCALLS_NOT_ENABLED;
711}
712
713
714/**
715 * Enables the Hyper-V Hypercall page.
716 *
717 * @returns VBox status code.
718 * @param pVM Pointer to the VM.
719 * @param GCPhysHypercallPage Where to map the hypercall page.
720 */
721VMMR3_INT_DECL(int) gimR3HvEnableHypercallPage(PVM pVM, RTGCPHYS GCPhysHypercallPage)
722{
723 PPDMDEVINSR3 pDevIns = pVM->gim.s.pDevInsR3;
724 PGIMMMIO2REGION pRegion = &pVM->gim.s.u.Hv.aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
725 AssertPtrReturn(pDevIns, VERR_GIM_DEVICE_NOT_REGISTERED);
726
727 if (pRegion->fMapped)
728 {
729 /*
730 * Is it already enabled at the given guest-address?
731 */
732 if (pRegion->GCPhysPage == GCPhysHypercallPage)
733 return VINF_SUCCESS;
734
735 /*
736 * If it's mapped at a different address, unmap the previous address.
737 */
738 int rc2 = gimR3HvDisableHypercallPage(pVM);
739 AssertRC(rc2);
740 }
741
742 /*
743 * Map the hypercall-page at the specified address.
744 */
745 Assert(!pRegion->fMapped);
746
747 /** @todo this is buggy when large pages are used due to a PGM limitation, see
748 * @bugref{7532}. Instead of the overlay style mapping, we just
749 * rewrite guest memory directly. */
750#if 0
751 int rc = GIMR3Mmio2Map(pVM, pRegion, GCPhysHypercallPage);
752 if (RT_SUCCESS(rc))
753 {
754 Assert(pRegion->GCPhysPage == GCPhysHypercallPage);
755
756 /*
757 * Patch the hypercall-page.
758 */
759 size_t cbWritten = 0;
760 rc = VMMPatchHypercall(pVM, pRegion->pvPageR3, PAGE_SIZE, &cbWritten);
761 if ( RT_SUCCESS(rc)
762 && cbWritten < PAGE_SIZE)
763 {
764 uint8_t *pbLast = (uint8_t *)pRegion->pvPageR3 + cbWritten;
765 *pbLast = 0xc3; /* RET */
766
767 /*
768 * Notify VMM that hypercalls are now enabled for all VCPUs.
769 */
770 for (VMCPUID i = 0; i < pVM->cCpus; i++)
771 VMMHypercallsEnable(&pVM->aCpus[i]);
772
773 LogRel(("GIM: HyperV: Enabled hypercalls at %#RGp\n", GCPhysHypercallPage));
774 return VINF_SUCCESS;
775 }
776 else
777 {
778 if (rc == VINF_SUCCESS)
779 rc = VERR_GIM_OPERATION_FAILED;
780 LogRel(("GIM: HyperV: VMMPatchHypercall failed. rc=%Rrc cbWritten=%u\n", rc, cbWritten));
781 }
782
783 GIMR3Mmio2Unmap(pVM, pRegion);
784 }
785
786 LogRel(("GIM: HyperV: GIMR3Mmio2Map failed. rc=%Rrc\n", rc));
787 return rc;
788#else
789 AssertReturn(pRegion->cbRegion == PAGE_SIZE, VERR_GIM_IPE_3);
790 void *pvHypercallPage = RTMemAllocZ(PAGE_SIZE);
791 if (RT_UNLIKELY(!pvHypercallPage))
792 {
793 LogRelFunc(("Failed to alloc %u bytes\n", PAGE_SIZE));
794 return VERR_NO_MEMORY;
795 }
796
797 /*
798 * Patch the hypercall-page.
799 */
800 size_t cbWritten = 0;
801 int rc = VMMPatchHypercall(pVM, pvHypercallPage, PAGE_SIZE, &cbWritten);
802 if ( RT_SUCCESS(rc)
803 && cbWritten < PAGE_SIZE)
804 {
805 uint8_t *pbLast = (uint8_t *)pvHypercallPage + cbWritten;
806 *pbLast = 0xc3; /* RET */
807
808 rc = PGMPhysSimpleWriteGCPhys(pVM, GCPhysHypercallPage, pvHypercallPage, PAGE_SIZE);
809 if (RT_SUCCESS(rc))
810 {
811 /*
812 * Notify VMM that hypercalls are now enabled for all VCPUs.
813 */
814 for (VMCPUID i = 0; i < pVM->cCpus; i++)
815 VMMHypercallsEnable(&pVM->aCpus[i]);
816
817 pRegion->GCPhysPage = GCPhysHypercallPage;
818 pRegion->fMapped = true;
819 LogRel(("GIM: HyperV: Enabled hypercalls at %#RGp\n", GCPhysHypercallPage));
820 }
821 else
822 LogRel(("GIM: HyperV: PGMPhysSimpleWriteGCPhys failed during hypercall page setup. rc=%Rrc\n", rc));
823 }
824 else
825 {
826 if (rc == VINF_SUCCESS)
827 rc = VERR_GIM_OPERATION_FAILED;
828 LogRel(("GIM: HyperV: VMMPatchHypercall failed. rc=%Rrc cbWritten=%u\n", rc, cbWritten));
829 }
830
831 RTMemFree(pvHypercallPage);
832 return rc;
833#endif
834}
835
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