VirtualBox

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

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

GIM nits: Must check version and config when loading saved state. Saved state version macros shouldn't be called guest-interface-manager-saved-state-manager-version, but rather guest-interface-manager-saved-state-version. SSM failures are sticky, so no need for AssertRCReturn for every SSM call, only when we want to use the data. First sentence of function documentation provide a brief description of the function and for readability purposes stand alone (also, keep it short when possible).

  • 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 52761 2014-09-16 15:49:57Z 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 * This is called after initializing HM and almost all other VMM components.
236 *
237 * @returns VBox status code.
238 * @param pVM Pointer to the VM.
239 */
240VMMR3_INT_DECL(int) GIMR3HvInitCompleted(PVM pVM)
241{
242 PGIMHV pHv = &pVM->gim.s.u.Hv;
243
244 /*
245 * Determine interface capabilities based on the version.
246 */
247 if (!pVM->gim.s.u32Version)
248 {
249 /* Hypervisor capabilities; features used by the hypervisor. */
250 pHv->uHyperCaps = HMIsNestedPagingActive(pVM) ? GIM_HV_HOST_FEAT_NESTED_PAGING : 0;
251 pHv->uHyperCaps |= HMAreMsrBitmapsAvailable(pVM) ? GIM_HV_HOST_FEAT_MSR_BITMAP : 0;
252 }
253
254 CPUMCPUIDLEAF HyperLeaf;
255 RT_ZERO(HyperLeaf);
256 HyperLeaf.uLeaf = UINT32_C(0x40000006);
257 HyperLeaf.uEax = pHv->uHyperCaps;
258 HyperLeaf.uEbx = 0;
259 HyperLeaf.uEcx = 0;
260 HyperLeaf.uEdx = 0;
261 int rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
262 AssertLogRelRCReturn(rc, rc);
263
264 return rc;
265}
266
267
268#if 0
269VMMR3_INT_DECL(int) GIMR3HvInitFinalize(PVM pVM)
270{
271 pVM->gim.s.pfnHypercallR3 = &GIMHvHypercall;
272 if (!HMIsEnabled(pVM))
273 {
274 rc = PDMR3LdrGetSymbolRC(pVM, NULL /* pszModule */, GIMHV_HYPERCALL, &pVM->gim.s.pfnHypercallRC);
275 AssertRCReturn(rc, rc);
276 }
277 rc = PDMR3LdrGetSymbolR0(pVM, NULL /* pszModule */, GIMHV_HYPERCALL, &pVM->gim.s.pfnHypercallR0);
278 AssertRCReturn(rc, rc);
279}
280#endif
281
282
283/**
284 * Terminates the Hyper-V GIM provider.
285 *
286 * @returns VBox status code.
287 * @param pVM Pointer to the VM.
288 */
289VMMR3_INT_DECL(int) GIMR3HvTerm(PVM pVM)
290{
291 GIMR3HvReset(pVM);
292 return VINF_SUCCESS;
293}
294
295
296/**
297 * Applies relocations to data and code managed by this component. This function
298 * will be called at init and whenever the VMM need to relocate itself inside
299 * the GC.
300 *
301 * @param pVM Pointer to the VM.
302 * @param offDelta Relocation delta relative to old location.
303 */
304VMMR3_INT_DECL(void) GIMR3HvRelocate(PVM pVM, RTGCINTPTR offDelta)
305{
306#if 0
307 int rc = PDMR3LdrGetSymbolRC(pVM, NULL /* pszModule */, GIMHV_HYPERCALL, &pVM->gim.s.pfnHypercallRC);
308 AssertFatalRC(rc);
309#endif
310}
311
312
313/**
314 * The VM is being reset. This resets Hyper-V provider MSRs and unmaps whatever
315 * Hyper-V regions that the guest may have mapped.
316 *
317 * @param pVM Pointer to the VM.
318 */
319VMMR3_INT_DECL(void) GIMR3HvReset(PVM pVM)
320{
321 /*
322 * Unmap MMIO2 pages that the guest may have setup.
323 */
324 LogRel(("GIM: HyperV: Resetting Hyper-V MMIO2 regions and MSRs\n"));
325 PGIMHV pHv = &pVM->gim.s.u.Hv;
326 for (unsigned i = 0; i < RT_ELEMENTS(pHv->aMmio2Regions); i++)
327 {
328 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[i];
329 GIMR3Mmio2Unmap(pVM, pRegion);
330 }
331
332 /*
333 * Reset MSRs.
334 */
335 pHv->u64GuestOsIdMsr = 0;
336 pHv->u64HypercallMsr = 0;
337 pHv->u64TscPageMsr = 0;
338}
339
340
341/**
342 * Returns a pointer to the MMIO2 regions supported by Hyper-V.
343 *
344 * @returns Pointer to an array of MMIO2 regions.
345 * @param pVM Pointer to the VM.
346 * @param pcRegions Where to store the number of regions in the array.
347 */
348VMMR3_INT_DECL(PGIMMMIO2REGION) GIMR3HvGetMmio2Regions(PVM pVM, uint32_t *pcRegions)
349{
350 Assert(GIMIsEnabled(pVM));
351 PGIMHV pHv = &pVM->gim.s.u.Hv;
352
353 *pcRegions = RT_ELEMENTS(pHv->aMmio2Regions);
354 Assert(*pcRegions <= UINT8_MAX); /* See PGMR3PhysMMIO2Register(). */
355 return pHv->aMmio2Regions;
356}
357
358
359/**
360 * Hyper-V state-save operation.
361 *
362 * @returns VBox status code.
363 * @param pVM Pointer to the VM.
364 * @param pSSM Pointer to the SSM handle.
365 */
366VMMR3_INT_DECL(int) GIMR3HvSave(PVM pVM, PSSMHANDLE pSSM)
367{
368 PCGIMHV pcHv = &pVM->gim.s.u.Hv;
369
370 /*
371 * Save the Hyper-V SSM version.
372 */
373 SSMR3PutU32(pSSM, GIM_HV_SAVED_STATE_VERSION);
374
375 /** @todo Save per-VCPU data. */
376
377 /*
378 * Save per-VM MSRs.
379 */
380 SSMR3PutU64(pSSM, pcHv->u64GuestOsIdMsr);
381 SSMR3PutU64(pSSM, pcHv->u64HypercallMsr);
382 SSMR3PutU64(pSSM, pcHv->u64TscPageMsr);
383
384 /*
385 * Save Hyper-V features / capabilities.
386 */
387 SSMR3PutU32(pSSM, pcHv->uBaseFeat);
388 SSMR3PutU32(pSSM, pcHv->uPartFlags);
389 SSMR3PutU32(pSSM, pcHv->uPowMgmtFeat);
390 SSMR3PutU32(pSSM, pcHv->uMiscFeat);
391 SSMR3PutU32(pSSM, pcHv->uHyperHints);
392 SSMR3PutU32(pSSM, pcHv->uHyperCaps);
393
394 /*
395 * Save the Hypercall region.
396 */
397 PCGIMMMIO2REGION pcRegion = &pcHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
398 SSMR3PutU8(pSSM, pcRegion->iRegion);
399 SSMR3PutBool(pSSM, pcRegion->fRCMapping);
400 SSMR3PutU32(pSSM, pcRegion->cbRegion);
401 SSMR3PutGCPhys(pSSM, pcRegion->GCPhysPage);
402 SSMR3PutStrZ(pSSM, pcRegion->szDescription);
403
404 /*
405 * Save the reference TSC region.
406 */
407 pcRegion = &pcHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
408 SSMR3PutU8(pSSM, pcRegion->iRegion);
409 SSMR3PutBool(pSSM, pcRegion->fRCMapping);
410 SSMR3PutU32(pSSM, pcRegion->cbRegion);
411 SSMR3PutGCPhys(pSSM, pcRegion->GCPhysPage);
412 SSMR3PutStrZ(pSSM, pcRegion->szDescription);
413 /* Save the TSC sequence so we can bump it on restore (as the CPU frequency/offset may change). */
414 uint32_t uTscSequence = 0;
415 if ( pcRegion->fMapped
416 && MSR_GIM_HV_REF_TSC_IS_ENABLED(pcHv->u64TscPageMsr))
417 {
418 PCGIMHVREFTSC pcRefTsc = (PCGIMHVREFTSC)pcRegion->pvPageR3;
419 uTscSequence = pcRefTsc->u32TscSequence;
420 }
421
422 return SSMR3PutU32(pSSM, uTscSequence);
423}
424
425
426/**
427 * Hyper-V state-load operation, final pass.
428 *
429 * @returns VBox status code.
430 * @param pVM Pointer to the VM.
431 * @param pSSM Pointer to the SSM handle.
432 * @param uSSMVersion The GIM saved-state version.
433 */
434VMMR3_INT_DECL(int) GIMR3HvLoad(PVM pVM, PSSMHANDLE pSSM, uint32_t uSSMVersion)
435{
436 PGIMHV pHv = &pVM->gim.s.u.Hv;
437
438 /*
439 * Load the Hyper-V SSM version first.
440 */
441 uint32_t uHvSavedStatVersion;
442 int rc = SSMR3GetU32(pSSM, &uHvSavedStatVersion);
443 AssertRCReturn(rc, rc);
444 if (uHvSavedStatVersion != GIM_HV_SAVED_STATE_VERSION)
445 return SSMR3SetLoadError(pSSM, VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION, RT_SRC_POS,
446 N_("Unsupported Hyper-V saved state version %u (expected %u)."),
447 uHvSavedStatVersion, GIM_HV_SAVED_STATE_VERSION);
448
449
450 /** @todo Load per-VCPU data. */
451
452 /*
453 * Load per-VM MSRs.
454 */
455 SSMR3GetU64(pSSM, &pHv->u64GuestOsIdMsr);
456 SSMR3GetU64(pSSM, &pHv->u64HypercallMsr);
457 SSMR3GetU64(pSSM, &pHv->u64TscPageMsr);
458
459 /*
460 * Load Hyper-V features / capabilities.
461 */
462 SSMR3GetU32(pSSM, &pHv->uBaseFeat);
463 SSMR3GetU32(pSSM, &pHv->uPartFlags);
464 SSMR3GetU32(pSSM, &pHv->uPowMgmtFeat);
465 SSMR3GetU32(pSSM, &pHv->uMiscFeat);
466 SSMR3GetU32(pSSM, &pHv->uHyperHints);
467 SSMR3GetU32(pSSM, &pHv->uHyperCaps);
468
469 /*
470 * Load and enable the Hypercall region.
471 */
472 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
473 SSMR3GetU8(pSSM, &pRegion->iRegion);
474 SSMR3GetBool(pSSM, &pRegion->fRCMapping);
475 SSMR3GetU32(pSSM, &pRegion->cbRegion);
476 SSMR3GetGCPhys(pSSM, &pRegion->GCPhysPage);
477 rc = SSMR3GetStrZ(pSSM, pRegion->szDescription, sizeof(pRegion->szDescription));
478 AssertRCReturn(rc, rc);
479 if (MSR_GIM_HV_HYPERCALL_IS_ENABLED(pHv->u64HypercallMsr))
480 {
481 Assert(pRegion->GCPhysPage != NIL_RTGCPHYS);
482 if (RT_LIKELY(pRegion->fRegistered))
483 {
484 rc = GIMR3HvEnableHypercallPage(pVM, pRegion->GCPhysPage);
485 if (RT_FAILURE(rc))
486 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Failed to enable the hypercall page. GCPhys=%#RGp rc=%Rrc"),
487 pRegion->GCPhysPage, rc);
488 }
489 else
490 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Hypercall MMIO2 region not registered. Missing GIM device?!"));
491 }
492
493 /*
494 * Load and enable the reference TSC region.
495 */
496 uint32_t uTscSequence;
497 pRegion = &pHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
498 SSMR3GetU8(pSSM, &pRegion->iRegion);
499 SSMR3GetBool(pSSM, &pRegion->fRCMapping);
500 SSMR3GetU32(pSSM, &pRegion->cbRegion);
501 SSMR3GetGCPhys(pSSM, &pRegion->GCPhysPage);
502 SSMR3GetStrZ(pSSM, pRegion->szDescription, sizeof(pRegion->szDescription));
503 rc = SSMR3GetU32(pSSM, &uTscSequence);
504 AssertRCReturn(rc, rc);
505 if (MSR_GIM_HV_REF_TSC_IS_ENABLED(pHv->u64TscPageMsr))
506 {
507 Assert(pRegion->GCPhysPage != NIL_RTGCPHYS);
508 if (pRegion->fRegistered)
509 {
510 rc = GIMR3HvEnableTscPage(pVM, pRegion->GCPhysPage, true /* fUseThisTscSeq */, uTscSequence);
511 if (RT_FAILURE(rc))
512 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Failed to enable the TSC page. GCPhys=%#RGp rc=%Rrc"),
513 pRegion->GCPhysPage, rc);
514 }
515 else
516 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("TSC-page MMIO2 region not registered. Missing GIM device?!"));
517 }
518
519 return rc;
520}
521
522
523/**
524 * Enables the Hyper-V TSC page.
525 *
526 * @returns VBox status code.
527 * @param pVM Pointer to the VM.
528 * @param GCPhysTscPage Where to map the TSC page.
529 * @param fUseThisTscSequence Whether to set the TSC sequence number to
530 * the one specified in @a uTscSequence.
531 * @param uTscSequence The TSC sequence value to use. Ignored if @a
532 * fUseThisTscSequence is false.
533 */
534VMMR3_INT_DECL(int) GIMR3HvEnableTscPage(PVM pVM, RTGCPHYS GCPhysTscPage, bool fUseThisTscSequence, uint32_t uTscSequence)
535{
536 PPDMDEVINSR3 pDevIns = pVM->gim.s.pDevInsR3;
537 PGIMMMIO2REGION pRegion = &pVM->gim.s.u.Hv.aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
538 AssertPtrReturn(pDevIns, VERR_GIM_DEVICE_NOT_REGISTERED);
539
540 int rc;
541 if (pRegion->fMapped)
542 {
543 /*
544 * Is it already enabled at the given guest-address?
545 */
546 if (pRegion->GCPhysPage == GCPhysTscPage)
547 return VINF_SUCCESS;
548
549 /*
550 * If it's mapped at a different address, unmap the previous address.
551 */
552 rc = GIMR3HvDisableTscPage(pVM);
553 AssertRC(rc);
554 }
555
556 /*
557 * Map the TSC-page at the specified address.
558 */
559 Assert(!pRegion->fMapped);
560 rc = GIMR3Mmio2Map(pVM, pRegion, GCPhysTscPage);
561 if (RT_SUCCESS(rc))
562 {
563 Assert(pRegion->GCPhysPage == GCPhysTscPage);
564
565 /*
566 * Update the TSC scale. Windows guests expect a non-zero TSC sequence, otherwise
567 * they fallback to using the reference count MSR which is not ideal in terms of VM-exits.
568 *
569 * Also, Hyper-V normalizes the time in 10 MHz, see:
570 * http://technet.microsoft.com/it-it/sysinternals/dn553408%28v=vs.110%29
571 */
572 PGIMHVREFTSC pRefTsc = (PGIMHVREFTSC)pRegion->pvPageR3;
573 Assert(pRefTsc);
574
575 uint64_t const u64TscKHz = TMCpuTicksPerSecond(pVM) / UINT64_C(1000);
576 uint32_t u32TscSeq = 1;
577 if ( fUseThisTscSequence
578 && uTscSequence < UINT32_C(0xfffffffe))
579 {
580 u32TscSeq = uTscSequence + 1;
581 }
582 pRefTsc->u32TscSequence = u32TscSeq;
583 pRefTsc->u64TscScale = ((INT64_C(10000) << 32) / u64TscKHz) << 32;
584
585 LogRel(("GIM: HyperV: Enabled TSC page at %#RGp - u64TscScale=%#RX64 u64TscKHz=%#RX64 (%'RU64)\n", GCPhysTscPage,
586 pRefTsc->u64TscScale, u64TscKHz, u64TscKHz));
587 return VINF_SUCCESS;
588 }
589 else
590 LogRelFunc(("GIMR3Mmio2Map failed. rc=%Rrc\n", rc));
591
592 return VERR_GIM_OPERATION_FAILED;
593}
594
595
596/**
597 * Disables the Hyper-V TSC page.
598 *
599 * @returns VBox status code.
600 * @param pVM Pointer to the VM.
601 */
602VMMR3_INT_DECL(int) GIMR3HvDisableTscPage(PVM pVM)
603{
604 PGIMHV pHv = &pVM->gim.s.u.Hv;
605 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
606 if (pRegion->fMapped)
607 {
608 GIMR3Mmio2Unmap(pVM, pRegion);
609 Assert(!pRegion->fMapped);
610 LogRel(("GIM: HyperV: Disabled TSC-page\n"));
611 return VINF_SUCCESS;
612 }
613 return VERR_GIM_PVTSC_NOT_ENABLED;
614}
615
616
617/**
618 * Disables the Hyper-V Hypercall page.
619 *
620 * @returns VBox status code.
621 */
622VMMR3_INT_DECL(int) GIMR3HvDisableHypercallPage(PVM pVM)
623{
624 PGIMHV pHv = &pVM->gim.s.u.Hv;
625 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
626 if (pRegion->fMapped)
627 {
628 GIMR3Mmio2Unmap(pVM, pRegion);
629 Assert(!pRegion->fMapped);
630 LogRel(("GIM: HyperV: Disabled Hypercall-page\n"));
631 return VINF_SUCCESS;
632 }
633 return VERR_GIM_HYPERCALLS_NOT_ENABLED;
634}
635
636
637/**
638 * Enables the Hyper-V Hypercall page.
639 *
640 * @returns VBox status code.
641 * @param pVM Pointer to the VM.
642 * @param GCPhysHypercallPage Where to map the hypercall page.
643 */
644VMMR3_INT_DECL(int) GIMR3HvEnableHypercallPage(PVM pVM, RTGCPHYS GCPhysHypercallPage)
645{
646 PPDMDEVINSR3 pDevIns = pVM->gim.s.pDevInsR3;
647 PGIMMMIO2REGION pRegion = &pVM->gim.s.u.Hv.aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
648 AssertPtrReturn(pDevIns, VERR_GIM_DEVICE_NOT_REGISTERED);
649
650 if (pRegion->fMapped)
651 {
652 /*
653 * Is it already enabled at the given guest-address?
654 */
655 if (pRegion->GCPhysPage == GCPhysHypercallPage)
656 return VINF_SUCCESS;
657
658 /*
659 * If it's mapped at a different address, unmap the previous address.
660 */
661 int rc2 = GIMR3HvDisableHypercallPage(pVM);
662 AssertRC(rc2);
663 }
664
665 /*
666 * Map the hypercall-page at the specified address.
667 */
668 Assert(!pRegion->fMapped);
669 int rc = GIMR3Mmio2Map(pVM, pRegion, GCPhysHypercallPage);
670 if (RT_SUCCESS(rc))
671 {
672 Assert(pRegion->GCPhysPage == GCPhysHypercallPage);
673
674 /*
675 * Patch the hypercall-page.
676 */
677 if (HMIsEnabled(pVM))
678 {
679 size_t cbWritten = 0;
680 rc = HMPatchHypercall(pVM, pRegion->pvPageR3, PAGE_SIZE, &cbWritten);
681 if ( RT_SUCCESS(rc)
682 && cbWritten < PAGE_SIZE)
683 {
684 uint8_t *pbLast = (uint8_t *)pRegion->pvPageR3 + cbWritten;
685 *pbLast = 0xc3; /* RET */
686
687 LogRel(("GIM: HyperV: Enabled hypercalls at %#RGp\n", GCPhysHypercallPage));
688 return VINF_SUCCESS;
689 }
690 else
691 {
692 if (rc == VINF_SUCCESS)
693 rc = VERR_GIM_OPERATION_FAILED;
694 LogRelFunc(("HMPatchHypercall failed. rc=%Rrc cbWritten=%u\n", rc, cbWritten));
695 }
696 }
697 else
698 {
699 /** @todo Handle raw-mode hypercall page patching. */
700 LogRel(("GIM: HyperV: Raw-mode hypercalls not yet implemented!\n"));
701 }
702 GIMR3Mmio2Unmap(pVM, pRegion);
703 }
704 else
705 LogRelFunc(("GIMR3Mmio2Map failed. rc=%Rrc\n", rc));
706
707 return rc;
708}
709
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