VirtualBox

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

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

GIM/HyperV: Don't identify as Microsoft as the default vendor ID, use VBox specific ID; made configurable through CFGM.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 29.9 KB
Line 
1/* $Id: GIMHv.cpp 57506 2015-08-24 11:28:41Z 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/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_GIM
23#include "GIMInternal.h"
24
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/vmm/ssm.h>
33#include <VBox/vmm/vm.h>
34#include <VBox/vmm/hm.h>
35#include <VBox/vmm/pdmapi.h>
36#include <VBox/version.h>
37
38
39/*********************************************************************************************************************************
40* Defined Constants And Macros *
41*********************************************************************************************************************************/
42//#define GIMHV_HYPERCALL "GIMHvHypercall"
43
44/**
45 * GIM Hyper-V saved-state version.
46 */
47#define GIM_HV_SAVED_STATE_VERSION UINT32_C(1)
48
49
50/*********************************************************************************************************************************
51* Global Variables *
52*********************************************************************************************************************************/
53#ifdef VBOX_WITH_STATISTICS
54# define GIMHV_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 GIMHV_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 Hyper-V.
63 */
64static CPUMMSRRANGE const g_aMsrRanges_HyperV[] =
65{
66 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE0_START, MSR_GIM_HV_RANGE0_END, "Hyper-V range 0"),
67 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE1_START, MSR_GIM_HV_RANGE1_END, "Hyper-V range 1"),
68 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE2_START, MSR_GIM_HV_RANGE2_END, "Hyper-V range 2"),
69 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE3_START, MSR_GIM_HV_RANGE3_END, "Hyper-V range 3"),
70 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE4_START, MSR_GIM_HV_RANGE4_END, "Hyper-V range 4"),
71 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE5_START, MSR_GIM_HV_RANGE5_END, "Hyper-V range 5"),
72 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE6_START, MSR_GIM_HV_RANGE6_END, "Hyper-V range 6"),
73 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE7_START, MSR_GIM_HV_RANGE7_END, "Hyper-V range 7"),
74 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE8_START, MSR_GIM_HV_RANGE8_END, "Hyper-V range 8"),
75 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE9_START, MSR_GIM_HV_RANGE9_END, "Hyper-V range 9"),
76 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE10_START, MSR_GIM_HV_RANGE10_END, "Hyper-V range 10"),
77 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE11_START, MSR_GIM_HV_RANGE11_END, "Hyper-V range 11")
78};
79#undef GIMHV_MSRRANGE
80
81
82/**
83 * Initializes the Hyper-V GIM provider.
84 *
85 * @returns VBox status code.
86 * @param pVM Pointer to the VM.
87 * @param uVersion The interface version this VM should use.
88 */
89VMMR3_INT_DECL(int) gimR3HvInit(PVM pVM)
90{
91 AssertReturn(pVM, VERR_INVALID_PARAMETER);
92 AssertReturn(pVM->gim.s.enmProviderId == GIMPROVIDERID_HYPERV, VERR_INTERNAL_ERROR_5);
93
94 int rc;
95 PGIMHV pHv = &pVM->gim.s.u.Hv;
96
97 /*
98 * Read configuration.
99 */
100 PCFGMNODE pCfgNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "GIM/HyperV");
101
102 /** @cfgm{/GIM/HyperV/VendorID, string, 'VBoxVBoxVBox'}
103 * The Hyper-V vendor signature, must be 12 characters. */
104 char szVendor[13];
105 rc = CFGMR3QueryStringDef(pCfgNode, "VendorID", szVendor, sizeof(szVendor), "VBoxVBoxVBox");
106 AssertLogRelRCReturn(rc, rc);
107
108 /*
109 * Determine interface capabilities based on the version.
110 */
111 if (!pVM->gim.s.u32Version)
112 {
113 /* Basic features. */
114 pHv->uBaseFeat = 0
115 //| GIM_HV_BASE_FEAT_VP_RUNTIME_MSR
116 | GIM_HV_BASE_FEAT_PART_TIME_REF_COUNT_MSR
117 //| GIM_HV_BASE_FEAT_BASIC_SYNTH_IC
118 //| GIM_HV_BASE_FEAT_SYNTH_TIMER_MSRS
119 | GIM_HV_BASE_FEAT_APIC_ACCESS_MSRS
120 | GIM_HV_BASE_FEAT_HYPERCALL_MSRS
121 | GIM_HV_BASE_FEAT_VP_ID_MSR
122 | GIM_HV_BASE_FEAT_VIRT_SYS_RESET_MSR
123 //| GIM_HV_BASE_FEAT_STAT_PAGES_MSR
124 | GIM_HV_BASE_FEAT_PART_REF_TSC_MSR
125 //| GIM_HV_BASE_FEAT_GUEST_IDLE_STATE_MSR
126 | GIM_HV_BASE_FEAT_TIMER_FREQ_MSRS
127 //| GIM_HV_BASE_FEAT_DEBUG_MSRS
128 ;
129
130 /* Miscellaneous features. */
131 pHv->uMiscFeat = GIM_HV_MISC_FEAT_TIMER_FREQ
132 | GIM_HV_MISC_FEAT_GUEST_CRASH_MSRS;
133
134 /* Hypervisor recommendations to the guest. */
135 pHv->uHyperHints = GIM_HV_HINT_MSR_FOR_SYS_RESET
136 | GIM_HV_HINT_RELAX_TIME_CHECKS;
137 }
138
139 /*
140 * Populate the required fields in MMIO2 region records for registering.
141 */
142 AssertCompile(GIM_HV_PAGE_SIZE == PAGE_SIZE);
143 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
144 pRegion->iRegion = GIM_HV_HYPERCALL_PAGE_REGION_IDX;
145 pRegion->fRCMapping = false;
146 pRegion->cbRegion = PAGE_SIZE; /* Sanity checked in gimR3HvLoad(), gimR3HvEnableTscPage() & gimR3HvEnableHypercallPage() */
147 pRegion->GCPhysPage = NIL_RTGCPHYS;
148 RTStrCopy(pRegion->szDescription, sizeof(pRegion->szDescription), "Hyper-V hypercall page");
149
150 pRegion = &pHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
151 pRegion->iRegion = GIM_HV_REF_TSC_PAGE_REGION_IDX;
152 pRegion->fRCMapping = false;
153 pRegion->cbRegion = PAGE_SIZE; /* Sanity checked in gimR3HvLoad(), gimR3HvEnableTscPage() & gimR3HvEnableHypercallPage() */
154 pRegion->GCPhysPage = NIL_RTGCPHYS;
155 RTStrCopy(pRegion->szDescription, sizeof(pRegion->szDescription), "Hyper-V TSC page");
156
157 /*
158 * Make sure the CPU ID bit are in accordance to the Hyper-V
159 * requirement and other paranoia checks.
160 * See "Requirements for implementing the Microsoft hypervisor interface" spec.
161 */
162 Assert(!(pHv->uPartFlags & ( GIM_HV_PART_FLAGS_CREATE_PART
163 | GIM_HV_PART_FLAGS_ACCESS_MEMORY_POOL
164 | GIM_HV_PART_FLAGS_ACCESS_PART_ID
165 | GIM_HV_PART_FLAGS_ADJUST_MSG_BUFFERS
166 | GIM_HV_PART_FLAGS_CREATE_PORT
167 | GIM_HV_PART_FLAGS_ACCESS_STATS
168 | GIM_HV_PART_FLAGS_CPU_MGMT
169 | GIM_HV_PART_FLAGS_CPU_PROFILER)));
170 Assert((pHv->uBaseFeat & (GIM_HV_BASE_FEAT_HYPERCALL_MSRS | GIM_HV_BASE_FEAT_VP_ID_MSR))
171 == (GIM_HV_BASE_FEAT_HYPERCALL_MSRS | GIM_HV_BASE_FEAT_VP_ID_MSR));
172 for (unsigned i = 0; i < RT_ELEMENTS(pHv->aMmio2Regions); i++)
173 {
174 PCGIMMMIO2REGION pcCur = &pHv->aMmio2Regions[i];
175 Assert(!pcCur->fRCMapping);
176 Assert(!pcCur->fMapped);
177 Assert(pcCur->GCPhysPage == NIL_RTGCPHYS);
178 }
179
180 /*
181 * Expose HVP (Hypervisor Present) bit to the guest.
182 */
183 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_HVP);
184
185 /*
186 * Modify the standard hypervisor leaves for Hyper-V.
187 */
188 CPUMCPUIDLEAF HyperLeaf;
189 RT_ZERO(HyperLeaf);
190 HyperLeaf.uLeaf = UINT32_C(0x40000000);
191 HyperLeaf.uEax = UINT32_C(0x40000006); /* Minimum value for Hyper-V is 0x40000005. */
192 /* Don't report vendor as 'Microsoft Hv' by default, see @bugref{7270#c152}. */
193 {
194 uint32_t uVendorEbx;
195 uint32_t uVendorEcx;
196 uint32_t uVendorEdx;
197 uVendorEbx = ((uint32_t)szVendor[ 3]) << 24 | ((uint32_t)szVendor[ 2]) << 16 | ((uint32_t)szVendor[1]) << 8
198 | (uint32_t)szVendor[ 0];
199 uVendorEcx = ((uint32_t)szVendor[ 7]) << 24 | ((uint32_t)szVendor[ 6]) << 16 | ((uint32_t)szVendor[5]) << 8
200 | (uint32_t)szVendor[ 4];
201 uVendorEdx = ((uint32_t)szVendor[11]) << 24 | ((uint32_t)szVendor[10]) << 16 | ((uint32_t)szVendor[9]) << 8
202 | (uint32_t)szVendor[ 8];
203 HyperLeaf.uEbx = uVendorEbx;
204 HyperLeaf.uEcx = uVendorEcx;
205 HyperLeaf.uEdx = uVendorEdx;
206 }
207 rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
208 AssertLogRelRCReturn(rc, rc);
209
210 HyperLeaf.uLeaf = UINT32_C(0x40000001);
211 HyperLeaf.uEax = 0x31237648; /* 'Hv#1' */
212 HyperLeaf.uEbx = 0; /* Reserved */
213 HyperLeaf.uEcx = 0; /* Reserved */
214 HyperLeaf.uEdx = 0; /* Reserved */
215 rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
216 AssertLogRelRCReturn(rc, rc);
217
218 /*
219 * Add Hyper-V specific leaves.
220 */
221 HyperLeaf.uLeaf = UINT32_C(0x40000002); /* MBZ until MSR_GIM_HV_GUEST_OS_ID is set by the guest. */
222 HyperLeaf.uEax = 0;
223 HyperLeaf.uEbx = 0;
224 HyperLeaf.uEcx = 0;
225 HyperLeaf.uEdx = 0;
226 rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
227 AssertLogRelRCReturn(rc, rc);
228
229 HyperLeaf.uLeaf = UINT32_C(0x40000003);
230 HyperLeaf.uEax = pHv->uBaseFeat;
231 HyperLeaf.uEbx = pHv->uPartFlags;
232 HyperLeaf.uEcx = pHv->uPowMgmtFeat;
233 HyperLeaf.uEdx = pHv->uMiscFeat;
234 rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
235 AssertLogRelRCReturn(rc, rc);
236
237 HyperLeaf.uLeaf = UINT32_C(0x40000004);
238 HyperLeaf.uEax = pHv->uHyperHints;
239 HyperLeaf.uEbx = 0xffffffff;
240 HyperLeaf.uEcx = 0;
241 HyperLeaf.uEdx = 0;
242 rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
243 AssertLogRelRCReturn(rc, rc);
244
245 /*
246 * Insert all MSR ranges of Hyper-V.
247 */
248 for (unsigned i = 0; i < RT_ELEMENTS(g_aMsrRanges_HyperV); i++)
249 {
250 rc = CPUMR3MsrRangesInsert(pVM, &g_aMsrRanges_HyperV[i]);
251 AssertLogRelRCReturn(rc, rc);
252 }
253
254 /*
255 * Setup non-zero MSRs.
256 */
257 if (pHv->uMiscFeat & GIM_HV_MISC_FEAT_GUEST_CRASH_MSRS)
258 pHv->uCrashCtl = MSR_GIM_HV_CRASH_CTL_NOTIFY_BIT;
259
260 return VINF_SUCCESS;
261}
262
263
264/**
265 * Initializes remaining bits of the Hyper-V provider.
266 *
267 * This is called after initializing HM and almost all other VMM components.
268 *
269 * @returns VBox status code.
270 * @param pVM Pointer to the VM.
271 */
272VMMR3_INT_DECL(int) gimR3HvInitCompleted(PVM pVM)
273{
274 PGIMHV pHv = &pVM->gim.s.u.Hv;
275 pHv->cTscTicksPerSecond = TMCpuTicksPerSecond(pVM);
276
277 /*
278 * Determine interface capabilities based on the version.
279 */
280 if (!pVM->gim.s.u32Version)
281 {
282 /* Hypervisor capabilities; features used by the hypervisor. */
283 pHv->uHyperCaps = HMIsNestedPagingActive(pVM) ? GIM_HV_HOST_FEAT_NESTED_PAGING : 0;
284 pHv->uHyperCaps |= HMAreMsrBitmapsAvailable(pVM) ? GIM_HV_HOST_FEAT_MSR_BITMAP : 0;
285 }
286
287 CPUMCPUIDLEAF HyperLeaf;
288 RT_ZERO(HyperLeaf);
289 HyperLeaf.uLeaf = UINT32_C(0x40000006);
290 HyperLeaf.uEax = pHv->uHyperCaps;
291 HyperLeaf.uEbx = 0;
292 HyperLeaf.uEcx = 0;
293 HyperLeaf.uEdx = 0;
294 int rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
295 AssertLogRelRCReturn(rc, rc);
296
297 return rc;
298}
299
300
301#if 0
302VMMR3_INT_DECL(int) gimR3HvInitFinalize(PVM pVM)
303{
304 pVM->gim.s.pfnHypercallR3 = &GIMHvHypercall;
305 if (!HMIsEnabled(pVM))
306 {
307 rc = PDMR3LdrGetSymbolRC(pVM, NULL /* pszModule */, GIMHV_HYPERCALL, &pVM->gim.s.pfnHypercallRC);
308 AssertRCReturn(rc, rc);
309 }
310 rc = PDMR3LdrGetSymbolR0(pVM, NULL /* pszModule */, GIMHV_HYPERCALL, &pVM->gim.s.pfnHypercallR0);
311 AssertRCReturn(rc, rc);
312}
313#endif
314
315
316/**
317 * Terminates the Hyper-V GIM provider.
318 *
319 * @returns VBox status code.
320 * @param pVM Pointer to the VM.
321 */
322VMMR3_INT_DECL(int) gimR3HvTerm(PVM pVM)
323{
324 gimR3HvReset(pVM);
325 return VINF_SUCCESS;
326}
327
328
329/**
330 * Applies relocations to data and code managed by this component.
331 *
332 * This function will be called at init and whenever the VMM need to relocate
333 * itself inside the GC.
334 *
335 * @param pVM Pointer to the VM.
336 * @param offDelta Relocation delta relative to old location.
337 */
338VMMR3_INT_DECL(void) gimR3HvRelocate(PVM pVM, RTGCINTPTR offDelta)
339{
340#if 0
341 int rc = PDMR3LdrGetSymbolRC(pVM, NULL /* pszModule */, GIMHV_HYPERCALL, &pVM->gim.s.pfnHypercallRC);
342 AssertFatalRC(rc);
343#endif
344}
345
346
347/**
348 * This resets Hyper-V provider MSRs and unmaps whatever Hyper-V regions that
349 * the guest may have mapped.
350 *
351 * This is called when the VM is being reset.
352 *
353 * @param pVM Pointer to the VM.
354 * @thread EMT(0).
355 */
356VMMR3_INT_DECL(void) gimR3HvReset(PVM pVM)
357{
358 VM_ASSERT_EMT0(pVM);
359
360 /*
361 * Unmap MMIO2 pages that the guest may have setup.
362 */
363 LogRel(("GIM: HyperV: Resetting MMIO2 regions and MSRs\n"));
364 PGIMHV pHv = &pVM->gim.s.u.Hv;
365 for (unsigned i = 0; i < RT_ELEMENTS(pHv->aMmio2Regions); i++)
366 {
367 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[i];
368#if 0
369 GIMR3Mmio2Unmap(pVM, pRegion);
370#else
371 pRegion->fMapped = false;
372 pRegion->GCPhysPage = NIL_RTGCPHYS;
373#endif
374 }
375
376 /*
377 * Reset MSRs.
378 */
379 pHv->u64GuestOsIdMsr = 0;
380 pHv->u64HypercallMsr = 0;
381 pHv->u64TscPageMsr = 0;
382 pHv->uCrashP0 = 0;
383 pHv->uCrashP1 = 0;
384 pHv->uCrashP2 = 0;
385 pHv->uCrashP3 = 0;
386 pHv->uCrashP4 = 0;
387}
388
389
390/**
391 * Returns a pointer to the MMIO2 regions supported by Hyper-V.
392 *
393 * @returns Pointer to an array of MMIO2 regions.
394 * @param pVM Pointer to the VM.
395 * @param pcRegions Where to store the number of regions in the array.
396 */
397VMMR3_INT_DECL(PGIMMMIO2REGION) gimR3HvGetMmio2Regions(PVM pVM, uint32_t *pcRegions)
398{
399 Assert(GIMIsEnabled(pVM));
400 PGIMHV pHv = &pVM->gim.s.u.Hv;
401
402 *pcRegions = RT_ELEMENTS(pHv->aMmio2Regions);
403 Assert(*pcRegions <= UINT8_MAX); /* See PGMR3PhysMMIO2Register(). */
404 return pHv->aMmio2Regions;
405}
406
407
408/**
409 * Hyper-V state-save operation.
410 *
411 * @returns VBox status code.
412 * @param pVM Pointer to the VM.
413 * @param pSSM Pointer to the SSM handle.
414 */
415VMMR3_INT_DECL(int) gimR3HvSave(PVM pVM, PSSMHANDLE pSSM)
416{
417 PCGIMHV pcHv = &pVM->gim.s.u.Hv;
418
419 /*
420 * Save the Hyper-V SSM version.
421 */
422 SSMR3PutU32(pSSM, GIM_HV_SAVED_STATE_VERSION);
423
424 /*
425 * Save per-VM MSRs.
426 */
427 SSMR3PutU64(pSSM, pcHv->u64GuestOsIdMsr);
428 SSMR3PutU64(pSSM, pcHv->u64HypercallMsr);
429 SSMR3PutU64(pSSM, pcHv->u64TscPageMsr);
430
431 /*
432 * Save Hyper-V features / capabilities.
433 */
434 SSMR3PutU32(pSSM, pcHv->uBaseFeat);
435 SSMR3PutU32(pSSM, pcHv->uPartFlags);
436 SSMR3PutU32(pSSM, pcHv->uPowMgmtFeat);
437 SSMR3PutU32(pSSM, pcHv->uMiscFeat);
438 SSMR3PutU32(pSSM, pcHv->uHyperHints);
439 SSMR3PutU32(pSSM, pcHv->uHyperCaps);
440
441 /*
442 * Save the Hypercall region.
443 */
444 PCGIMMMIO2REGION pcRegion = &pcHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
445 SSMR3PutU8(pSSM, pcRegion->iRegion);
446 SSMR3PutBool(pSSM, pcRegion->fRCMapping);
447 SSMR3PutU32(pSSM, pcRegion->cbRegion);
448 SSMR3PutGCPhys(pSSM, pcRegion->GCPhysPage);
449 SSMR3PutStrZ(pSSM, pcRegion->szDescription);
450
451 /*
452 * Save the reference TSC region.
453 */
454 pcRegion = &pcHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
455 SSMR3PutU8(pSSM, pcRegion->iRegion);
456 SSMR3PutBool(pSSM, pcRegion->fRCMapping);
457 SSMR3PutU32(pSSM, pcRegion->cbRegion);
458 SSMR3PutGCPhys(pSSM, pcRegion->GCPhysPage);
459 SSMR3PutStrZ(pSSM, pcRegion->szDescription);
460 /* Save the TSC sequence so we can bump it on restore (as the CPU frequency/offset may change). */
461 uint32_t uTscSequence = 0;
462 if ( pcRegion->fMapped
463 && MSR_GIM_HV_REF_TSC_IS_ENABLED(pcHv->u64TscPageMsr))
464 {
465 PCGIMHVREFTSC pcRefTsc = (PCGIMHVREFTSC)pcRegion->pvPageR3;
466 uTscSequence = pcRefTsc->u32TscSequence;
467 }
468
469 return SSMR3PutU32(pSSM, uTscSequence);
470}
471
472
473/**
474 * Hyper-V state-load operation, final pass.
475 *
476 * @returns VBox status code.
477 * @param pVM Pointer to the VM.
478 * @param pSSM Pointer to the SSM handle.
479 * @param uSSMVersion The GIM saved-state version.
480 */
481VMMR3_INT_DECL(int) gimR3HvLoad(PVM pVM, PSSMHANDLE pSSM, uint32_t uSSMVersion)
482{
483 /*
484 * Load the Hyper-V SSM version first.
485 */
486 uint32_t uHvSavedStatVersion;
487 int rc = SSMR3GetU32(pSSM, &uHvSavedStatVersion);
488 AssertRCReturn(rc, rc);
489 if (uHvSavedStatVersion != GIM_HV_SAVED_STATE_VERSION)
490 return SSMR3SetLoadError(pSSM, VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION, RT_SRC_POS,
491 N_("Unsupported Hyper-V saved-state version %u (expected %u)."), uHvSavedStatVersion,
492 GIM_HV_SAVED_STATE_VERSION);
493
494 /*
495 * Update the TSC frequency from TM.
496 */
497 PGIMHV pHv = &pVM->gim.s.u.Hv;
498 pHv->cTscTicksPerSecond = TMCpuTicksPerSecond(pVM);
499
500 /*
501 * Load per-VM MSRs.
502 */
503 SSMR3GetU64(pSSM, &pHv->u64GuestOsIdMsr);
504 SSMR3GetU64(pSSM, &pHv->u64HypercallMsr);
505 SSMR3GetU64(pSSM, &pHv->u64TscPageMsr);
506
507 /*
508 * Load Hyper-V features / capabilities.
509 */
510 SSMR3GetU32(pSSM, &pHv->uBaseFeat);
511 SSMR3GetU32(pSSM, &pHv->uPartFlags);
512 SSMR3GetU32(pSSM, &pHv->uPowMgmtFeat);
513 SSMR3GetU32(pSSM, &pHv->uMiscFeat);
514 SSMR3GetU32(pSSM, &pHv->uHyperHints);
515 SSMR3GetU32(pSSM, &pHv->uHyperCaps);
516
517 /*
518 * Load and enable the Hypercall region.
519 */
520 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
521 SSMR3GetU8(pSSM, &pRegion->iRegion);
522 SSMR3GetBool(pSSM, &pRegion->fRCMapping);
523 SSMR3GetU32(pSSM, &pRegion->cbRegion);
524 SSMR3GetGCPhys(pSSM, &pRegion->GCPhysPage);
525 rc = SSMR3GetStrZ(pSSM, pRegion->szDescription, sizeof(pRegion->szDescription));
526 AssertRCReturn(rc, rc);
527
528 if (pRegion->cbRegion != PAGE_SIZE)
529 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Hypercall page region size %u invalid, expected %u"),
530 pRegion->cbRegion, PAGE_SIZE);
531
532 if (MSR_GIM_HV_HYPERCALL_IS_ENABLED(pHv->u64HypercallMsr))
533 {
534 Assert(pRegion->GCPhysPage != NIL_RTGCPHYS);
535 if (RT_LIKELY(pRegion->fRegistered))
536 {
537 rc = gimR3HvEnableHypercallPage(pVM, pRegion->GCPhysPage);
538 if (RT_FAILURE(rc))
539 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Failed to enable the hypercall page. GCPhys=%#RGp rc=%Rrc"),
540 pRegion->GCPhysPage, rc);
541 }
542 else
543 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Hypercall MMIO2 region not registered. Missing GIM device?!"));
544 }
545
546 /*
547 * Load and enable the reference TSC region.
548 */
549 uint32_t uTscSequence;
550 pRegion = &pHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
551 SSMR3GetU8(pSSM, &pRegion->iRegion);
552 SSMR3GetBool(pSSM, &pRegion->fRCMapping);
553 SSMR3GetU32(pSSM, &pRegion->cbRegion);
554 SSMR3GetGCPhys(pSSM, &pRegion->GCPhysPage);
555 SSMR3GetStrZ(pSSM, pRegion->szDescription, sizeof(pRegion->szDescription));
556 rc = SSMR3GetU32(pSSM, &uTscSequence);
557 AssertRCReturn(rc, rc);
558
559 if (pRegion->cbRegion != PAGE_SIZE)
560 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("TSC page region size %u invalid, expected %u"),
561 pRegion->cbRegion, PAGE_SIZE);
562
563 if (MSR_GIM_HV_REF_TSC_IS_ENABLED(pHv->u64TscPageMsr))
564 {
565 Assert(pRegion->GCPhysPage != NIL_RTGCPHYS);
566 if (pRegion->fRegistered)
567 {
568 rc = gimR3HvEnableTscPage(pVM, pRegion->GCPhysPage, true /* fUseThisTscSeq */, uTscSequence);
569 if (RT_FAILURE(rc))
570 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Failed to enable the TSC page. GCPhys=%#RGp rc=%Rrc"),
571 pRegion->GCPhysPage, rc);
572 }
573 else
574 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("TSC-page MMIO2 region not registered. Missing GIM device?!"));
575 }
576
577 return rc;
578}
579
580
581/**
582 * Enables the Hyper-V TSC page.
583 *
584 * @returns VBox status code.
585 * @param pVM Pointer to the VM.
586 * @param GCPhysTscPage Where to map the TSC page.
587 * @param fUseThisTscSeq Whether to set the TSC sequence number to the one
588 * specified in @a uTscSeq.
589 * @param uTscSeq The TSC sequence value to use. Ignored if
590 * @a fUseThisTscSeq is false.
591 */
592VMMR3_INT_DECL(int) gimR3HvEnableTscPage(PVM pVM, RTGCPHYS GCPhysTscPage, bool fUseThisTscSeq, uint32_t uTscSeq)
593{
594 PPDMDEVINSR3 pDevIns = pVM->gim.s.pDevInsR3;
595 PGIMMMIO2REGION pRegion = &pVM->gim.s.u.Hv.aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
596 AssertPtrReturn(pDevIns, VERR_GIM_DEVICE_NOT_REGISTERED);
597
598 int rc;
599 if (pRegion->fMapped)
600 {
601 /*
602 * Is it already enabled at the given guest-address?
603 */
604 if (pRegion->GCPhysPage == GCPhysTscPage)
605 return VINF_SUCCESS;
606
607 /*
608 * If it's mapped at a different address, unmap the previous address.
609 */
610 rc = gimR3HvDisableTscPage(pVM);
611 AssertRC(rc);
612 }
613
614 /*
615 * Map the TSC-page at the specified address.
616 */
617 Assert(!pRegion->fMapped);
618
619 /** @todo this is buggy when large pages are used due to a PGM limitation, see
620 * @bugref{7532}. Instead of the overlay style mapping, we just
621 * rewrite guest memory directly. */
622#if 0
623 rc = GIMR3Mmio2Map(pVM, pRegion, GCPhysTscPage);
624 if (RT_SUCCESS(rc))
625 {
626 Assert(pRegion->GCPhysPage == GCPhysTscPage);
627
628 /*
629 * Update the TSC scale. Windows guests expect a non-zero TSC sequence, otherwise
630 * they fallback to using the reference count MSR which is not ideal in terms of VM-exits.
631 *
632 * Also, Hyper-V normalizes the time in 10 MHz, see:
633 * http://technet.microsoft.com/it-it/sysinternals/dn553408%28v=vs.110%29
634 */
635 PGIMHVREFTSC pRefTsc = (PGIMHVREFTSC)pRegion->pvPageR3;
636 Assert(pRefTsc);
637
638 PGIMHV pHv = &pVM->gim.s.u.Hv;
639 uint64_t const u64TscKHz = pHv->cTscTicksPerSecond / UINT64_C(1000);
640 uint32_t u32TscSeq = 1;
641 if ( fUseThisTscSeq
642 && uTscSeq < UINT32_C(0xfffffffe))
643 u32TscSeq = uTscSeq + 1;
644 pRefTsc->u32TscSequence = u32TscSeq;
645 pRefTsc->u64TscScale = ((INT64_C(10000) << 32) / u64TscKHz) << 32;
646 pRefTsc->i64TscOffset = 0;
647
648 LogRel(("GIM: HyperV: Enabled TSC page at %#RGp - u64TscScale=%#RX64 u64TscKHz=%#RX64 (%'RU64) Seq=%#RU32\n",
649 GCPhysTscPage, pRefTsc->u64TscScale, u64TscKHz, u64TscKHz, pRefTsc->u32TscSequence));
650
651 TMR3CpuTickParavirtEnable(pVM);
652 return VINF_SUCCESS;
653 }
654 else
655 LogRelFunc(("GIMR3Mmio2Map failed. rc=%Rrc\n", rc));
656 return VERR_GIM_OPERATION_FAILED;
657#else
658 AssertReturn(pRegion->cbRegion == PAGE_SIZE, VERR_GIM_IPE_2);
659 PGIMHVREFTSC pRefTsc = (PGIMHVREFTSC)RTMemAllocZ(PAGE_SIZE);
660 if (RT_UNLIKELY(!pRefTsc))
661 {
662 LogRelFunc(("Failed to alloc %u bytes\n", PAGE_SIZE));
663 return VERR_NO_MEMORY;
664 }
665
666 PGIMHV pHv = &pVM->gim.s.u.Hv;
667 uint64_t const u64TscKHz = pHv->cTscTicksPerSecond / UINT64_C(1000);
668 uint32_t u32TscSeq = 1;
669 if ( fUseThisTscSeq
670 && uTscSeq < UINT32_C(0xfffffffe))
671 u32TscSeq = uTscSeq + 1;
672 pRefTsc->u32TscSequence = u32TscSeq;
673 pRefTsc->u64TscScale = ((INT64_C(10000) << 32) / u64TscKHz) << 32;
674 pRefTsc->i64TscOffset = 0;
675
676 rc = PGMPhysSimpleWriteGCPhys(pVM, GCPhysTscPage, pRefTsc, sizeof(*pRefTsc));
677 if (RT_SUCCESS(rc))
678 {
679 LogRel(("GIM: HyperV: Enabled TSC page at %#RGp - u64TscScale=%#RX64 u64TscKHz=%#RX64 (%'RU64) Seq=%#RU32\n",
680 GCPhysTscPage, pRefTsc->u64TscScale, u64TscKHz, u64TscKHz, pRefTsc->u32TscSequence));
681
682 pRegion->GCPhysPage = GCPhysTscPage;
683 pRegion->fMapped = true;
684 TMR3CpuTickParavirtEnable(pVM);
685 }
686 else
687 {
688 LogRelFunc(("GIM: HyperV: PGMPhysSimpleWriteGCPhys failed. rc=%Rrc\n", rc));
689 rc = VERR_GIM_OPERATION_FAILED;
690 }
691 RTMemFree(pRefTsc);
692 return rc;
693#endif
694}
695
696
697/**
698 * Disables the Hyper-V TSC page.
699 *
700 * @returns VBox status code.
701 * @param pVM Pointer to the VM.
702 */
703VMMR3_INT_DECL(int) gimR3HvDisableTscPage(PVM pVM)
704{
705 PGIMHV pHv = &pVM->gim.s.u.Hv;
706 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
707 if (pRegion->fMapped)
708 {
709#if 0
710 GIMR3Mmio2Unmap(pVM, pRegion);
711 Assert(!pRegion->fMapped);
712#else
713 pRegion->fMapped = false;
714#endif
715 LogRel(("GIM: HyperV: Disabled TSC-page\n"));
716
717 TMR3CpuTickParavirtDisable(pVM);
718 return VINF_SUCCESS;
719 }
720 return VERR_GIM_PVTSC_NOT_ENABLED;
721}
722
723
724/**
725 * Disables the Hyper-V Hypercall page.
726 *
727 * @returns VBox status code.
728 */
729VMMR3_INT_DECL(int) gimR3HvDisableHypercallPage(PVM pVM)
730{
731 PGIMHV pHv = &pVM->gim.s.u.Hv;
732 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
733 if (pRegion->fMapped)
734 {
735#if 0
736 GIMR3Mmio2Unmap(pVM, pRegion);
737 Assert(!pRegion->fMapped);
738#else
739 pRegion->fMapped = false;
740#endif
741 for (VMCPUID i = 0; i < pVM->cCpus; i++)
742 VMMHypercallsDisable(&pVM->aCpus[i]);
743 LogRel(("GIM: HyperV: Disabled Hypercall-page\n"));
744 return VINF_SUCCESS;
745 }
746 return VERR_GIM_HYPERCALLS_NOT_ENABLED;
747}
748
749
750/**
751 * Enables the Hyper-V Hypercall page.
752 *
753 * @returns VBox status code.
754 * @param pVM Pointer to the VM.
755 * @param GCPhysHypercallPage Where to map the hypercall page.
756 */
757VMMR3_INT_DECL(int) gimR3HvEnableHypercallPage(PVM pVM, RTGCPHYS GCPhysHypercallPage)
758{
759 PPDMDEVINSR3 pDevIns = pVM->gim.s.pDevInsR3;
760 PGIMMMIO2REGION pRegion = &pVM->gim.s.u.Hv.aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
761 AssertPtrReturn(pDevIns, VERR_GIM_DEVICE_NOT_REGISTERED);
762
763 if (pRegion->fMapped)
764 {
765 /*
766 * Is it already enabled at the given guest-address?
767 */
768 if (pRegion->GCPhysPage == GCPhysHypercallPage)
769 return VINF_SUCCESS;
770
771 /*
772 * If it's mapped at a different address, unmap the previous address.
773 */
774 int rc2 = gimR3HvDisableHypercallPage(pVM);
775 AssertRC(rc2);
776 }
777
778 /*
779 * Map the hypercall-page at the specified address.
780 */
781 Assert(!pRegion->fMapped);
782
783 /** @todo this is buggy when large pages are used due to a PGM limitation, see
784 * @bugref{7532}. Instead of the overlay style mapping, we just
785 * rewrite guest memory directly. */
786#if 0
787 int rc = GIMR3Mmio2Map(pVM, pRegion, GCPhysHypercallPage);
788 if (RT_SUCCESS(rc))
789 {
790 Assert(pRegion->GCPhysPage == GCPhysHypercallPage);
791
792 /*
793 * Patch the hypercall-page.
794 */
795 size_t cbWritten = 0;
796 rc = VMMPatchHypercall(pVM, pRegion->pvPageR3, PAGE_SIZE, &cbWritten);
797 if ( RT_SUCCESS(rc)
798 && cbWritten < PAGE_SIZE)
799 {
800 uint8_t *pbLast = (uint8_t *)pRegion->pvPageR3 + cbWritten;
801 *pbLast = 0xc3; /* RET */
802
803 /*
804 * Notify VMM that hypercalls are now enabled for all VCPUs.
805 */
806 for (VMCPUID i = 0; i < pVM->cCpus; i++)
807 VMMHypercallsEnable(&pVM->aCpus[i]);
808
809 LogRel(("GIM: HyperV: Enabled hypercalls at %#RGp\n", GCPhysHypercallPage));
810 return VINF_SUCCESS;
811 }
812 else
813 {
814 if (rc == VINF_SUCCESS)
815 rc = VERR_GIM_OPERATION_FAILED;
816 LogRel(("GIM: HyperV: VMMPatchHypercall failed. rc=%Rrc cbWritten=%u\n", rc, cbWritten));
817 }
818
819 GIMR3Mmio2Unmap(pVM, pRegion);
820 }
821
822 LogRel(("GIM: HyperV: GIMR3Mmio2Map failed. rc=%Rrc\n", rc));
823 return rc;
824#else
825 AssertReturn(pRegion->cbRegion == PAGE_SIZE, VERR_GIM_IPE_3);
826 void *pvHypercallPage = RTMemAllocZ(PAGE_SIZE);
827 if (RT_UNLIKELY(!pvHypercallPage))
828 {
829 LogRelFunc(("Failed to alloc %u bytes\n", PAGE_SIZE));
830 return VERR_NO_MEMORY;
831 }
832
833 /*
834 * Patch the hypercall-page.
835 */
836 size_t cbWritten = 0;
837 int rc = VMMPatchHypercall(pVM, pvHypercallPage, PAGE_SIZE, &cbWritten);
838 if ( RT_SUCCESS(rc)
839 && cbWritten < PAGE_SIZE)
840 {
841 uint8_t *pbLast = (uint8_t *)pvHypercallPage + cbWritten;
842 *pbLast = 0xc3; /* RET */
843
844 rc = PGMPhysSimpleWriteGCPhys(pVM, GCPhysHypercallPage, pvHypercallPage, PAGE_SIZE);
845 if (RT_SUCCESS(rc))
846 {
847 /*
848 * Notify VMM that hypercalls are now enabled for all VCPUs.
849 */
850 for (VMCPUID i = 0; i < pVM->cCpus; i++)
851 VMMHypercallsEnable(&pVM->aCpus[i]);
852
853 pRegion->GCPhysPage = GCPhysHypercallPage;
854 pRegion->fMapped = true;
855 LogRel(("GIM: HyperV: Enabled hypercalls at %#RGp\n", GCPhysHypercallPage));
856 }
857 else
858 LogRel(("GIM: HyperV: PGMPhysSimpleWriteGCPhys failed during hypercall page setup. rc=%Rrc\n", rc));
859 }
860 else
861 {
862 if (rc == VINF_SUCCESS)
863 rc = VERR_GIM_OPERATION_FAILED;
864 LogRel(("GIM: HyperV: VMMPatchHypercall failed. rc=%Rrc cbWritten=%u\n", rc, cbWritten));
865 }
866
867 RTMemFree(pvHypercallPage);
868 return rc;
869#endif
870}
871
Note: See TracBrowser for help on using the repository browser.

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