VirtualBox

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

最後變更 在這個檔案從56694是 56694,由 vboxsync 提交於 10 年 前

VMM/GIM: Cleanup.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 28.0 KB
 
1/* $Id: GIMHv.cpp 56694 2015-06-30 09:19:15Z 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.alldomusa.eu.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 GIMR3Mmio2Unmap(pVM, pRegion);
338 }
339
340 /*
341 * Reset MSRs.
342 */
343 pHv->u64GuestOsIdMsr = 0;
344 pHv->u64HypercallMsr = 0;
345 pHv->u64TscPageMsr = 0;
346}
347
348
349/**
350 * Returns a pointer to the MMIO2 regions supported by Hyper-V.
351 *
352 * @returns Pointer to an array of MMIO2 regions.
353 * @param pVM Pointer to the VM.
354 * @param pcRegions Where to store the number of regions in the array.
355 */
356VMMR3_INT_DECL(PGIMMMIO2REGION) gimR3HvGetMmio2Regions(PVM pVM, uint32_t *pcRegions)
357{
358 Assert(GIMIsEnabled(pVM));
359 PGIMHV pHv = &pVM->gim.s.u.Hv;
360
361 *pcRegions = RT_ELEMENTS(pHv->aMmio2Regions);
362 Assert(*pcRegions <= UINT8_MAX); /* See PGMR3PhysMMIO2Register(). */
363 return pHv->aMmio2Regions;
364}
365
366
367/**
368 * Hyper-V state-save operation.
369 *
370 * @returns VBox status code.
371 * @param pVM Pointer to the VM.
372 * @param pSSM Pointer to the SSM handle.
373 */
374VMMR3_INT_DECL(int) gimR3HvSave(PVM pVM, PSSMHANDLE pSSM)
375{
376 PCGIMHV pcHv = &pVM->gim.s.u.Hv;
377
378 /*
379 * Save the Hyper-V SSM version.
380 */
381 SSMR3PutU32(pSSM, GIM_HV_SAVED_STATE_VERSION);
382
383 /*
384 * Save per-VM MSRs.
385 */
386 SSMR3PutU64(pSSM, pcHv->u64GuestOsIdMsr);
387 SSMR3PutU64(pSSM, pcHv->u64HypercallMsr);
388 SSMR3PutU64(pSSM, pcHv->u64TscPageMsr);
389
390 /*
391 * Save Hyper-V features / capabilities.
392 */
393 SSMR3PutU32(pSSM, pcHv->uBaseFeat);
394 SSMR3PutU32(pSSM, pcHv->uPartFlags);
395 SSMR3PutU32(pSSM, pcHv->uPowMgmtFeat);
396 SSMR3PutU32(pSSM, pcHv->uMiscFeat);
397 SSMR3PutU32(pSSM, pcHv->uHyperHints);
398 SSMR3PutU32(pSSM, pcHv->uHyperCaps);
399
400 /*
401 * Save the Hypercall region.
402 */
403 PCGIMMMIO2REGION pcRegion = &pcHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
404 SSMR3PutU8(pSSM, pcRegion->iRegion);
405 SSMR3PutBool(pSSM, pcRegion->fRCMapping);
406 SSMR3PutU32(pSSM, pcRegion->cbRegion);
407 SSMR3PutGCPhys(pSSM, pcRegion->GCPhysPage);
408 SSMR3PutStrZ(pSSM, pcRegion->szDescription);
409
410 /*
411 * Save the reference TSC region.
412 */
413 pcRegion = &pcHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
414 SSMR3PutU8(pSSM, pcRegion->iRegion);
415 SSMR3PutBool(pSSM, pcRegion->fRCMapping);
416 SSMR3PutU32(pSSM, pcRegion->cbRegion);
417 SSMR3PutGCPhys(pSSM, pcRegion->GCPhysPage);
418 SSMR3PutStrZ(pSSM, pcRegion->szDescription);
419 /* Save the TSC sequence so we can bump it on restore (as the CPU frequency/offset may change). */
420 uint32_t uTscSequence = 0;
421 if ( pcRegion->fMapped
422 && MSR_GIM_HV_REF_TSC_IS_ENABLED(pcHv->u64TscPageMsr))
423 {
424 PCGIMHVREFTSC pcRefTsc = (PCGIMHVREFTSC)pcRegion->pvPageR3;
425 uTscSequence = pcRefTsc->u32TscSequence;
426 }
427
428 return SSMR3PutU32(pSSM, uTscSequence);
429}
430
431
432/**
433 * Hyper-V state-load operation, final pass.
434 *
435 * @returns VBox status code.
436 * @param pVM Pointer to the VM.
437 * @param pSSM Pointer to the SSM handle.
438 * @param uSSMVersion The GIM saved-state version.
439 */
440VMMR3_INT_DECL(int) gimR3HvLoad(PVM pVM, PSSMHANDLE pSSM, uint32_t uSSMVersion)
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)."), uHvSavedStatVersion,
451 GIM_HV_SAVED_STATE_VERSION);
452
453 /*
454 * Update the TSC frequency from TM.
455 */
456 PGIMHV pHv = &pVM->gim.s.u.Hv;
457 pHv->cTscTicksPerSecond = TMCpuTicksPerSecond(pVM);
458
459 /*
460 * Load per-VM MSRs.
461 */
462 SSMR3GetU64(pSSM, &pHv->u64GuestOsIdMsr);
463 SSMR3GetU64(pSSM, &pHv->u64HypercallMsr);
464 SSMR3GetU64(pSSM, &pHv->u64TscPageMsr);
465
466 /*
467 * Load Hyper-V features / capabilities.
468 */
469 SSMR3GetU32(pSSM, &pHv->uBaseFeat);
470 SSMR3GetU32(pSSM, &pHv->uPartFlags);
471 SSMR3GetU32(pSSM, &pHv->uPowMgmtFeat);
472 SSMR3GetU32(pSSM, &pHv->uMiscFeat);
473 SSMR3GetU32(pSSM, &pHv->uHyperHints);
474 SSMR3GetU32(pSSM, &pHv->uHyperCaps);
475
476 /*
477 * Load and enable the Hypercall region.
478 */
479 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
480 SSMR3GetU8(pSSM, &pRegion->iRegion);
481 SSMR3GetBool(pSSM, &pRegion->fRCMapping);
482 SSMR3GetU32(pSSM, &pRegion->cbRegion);
483 SSMR3GetGCPhys(pSSM, &pRegion->GCPhysPage);
484 rc = SSMR3GetStrZ(pSSM, pRegion->szDescription, sizeof(pRegion->szDescription));
485 AssertRCReturn(rc, rc);
486
487 if (pRegion->cbRegion != PAGE_SIZE)
488 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Hypercall page region size %u invalid, expected %u"),
489 pRegion->cbRegion, PAGE_SIZE);
490
491 if (MSR_GIM_HV_HYPERCALL_IS_ENABLED(pHv->u64HypercallMsr))
492 {
493 Assert(pRegion->GCPhysPage != NIL_RTGCPHYS);
494 if (RT_LIKELY(pRegion->fRegistered))
495 {
496 rc = gimR3HvEnableHypercallPage(pVM, pRegion->GCPhysPage);
497 if (RT_FAILURE(rc))
498 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Failed to enable the hypercall page. GCPhys=%#RGp rc=%Rrc"),
499 pRegion->GCPhysPage, rc);
500 }
501 else
502 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Hypercall MMIO2 region not registered. Missing GIM device?!"));
503 }
504
505 /*
506 * Load and enable the reference TSC region.
507 */
508 uint32_t uTscSequence;
509 pRegion = &pHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
510 SSMR3GetU8(pSSM, &pRegion->iRegion);
511 SSMR3GetBool(pSSM, &pRegion->fRCMapping);
512 SSMR3GetU32(pSSM, &pRegion->cbRegion);
513 SSMR3GetGCPhys(pSSM, &pRegion->GCPhysPage);
514 SSMR3GetStrZ(pSSM, pRegion->szDescription, sizeof(pRegion->szDescription));
515 rc = SSMR3GetU32(pSSM, &uTscSequence);
516 AssertRCReturn(rc, rc);
517
518 if (pRegion->cbRegion != PAGE_SIZE)
519 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("TSC page region size %u invalid, expected %u"),
520 pRegion->cbRegion, PAGE_SIZE);
521
522 if (MSR_GIM_HV_REF_TSC_IS_ENABLED(pHv->u64TscPageMsr))
523 {
524 Assert(pRegion->GCPhysPage != NIL_RTGCPHYS);
525 if (pRegion->fRegistered)
526 {
527 rc = gimR3HvEnableTscPage(pVM, pRegion->GCPhysPage, true /* fUseThisTscSeq */, uTscSequence);
528 if (RT_FAILURE(rc))
529 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Failed to enable the TSC page. GCPhys=%#RGp rc=%Rrc"),
530 pRegion->GCPhysPage, rc);
531 }
532 else
533 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("TSC-page MMIO2 region not registered. Missing GIM device?!"));
534 }
535
536 return rc;
537}
538
539
540/**
541 * Enables the Hyper-V TSC page.
542 *
543 * @returns VBox status code.
544 * @param pVM Pointer to the VM.
545 * @param GCPhysTscPage Where to map the TSC page.
546 * @param fUseThisTscSeq Whether to set the TSC sequence number to the one
547 * specified in @a uTscSeq.
548 * @param uTscSeq The TSC sequence value to use. Ignored if
549 * @a fUseThisTscSeq is false.
550 */
551VMMR3_INT_DECL(int) gimR3HvEnableTscPage(PVM pVM, RTGCPHYS GCPhysTscPage, bool fUseThisTscSeq, uint32_t uTscSeq)
552{
553 PPDMDEVINSR3 pDevIns = pVM->gim.s.pDevInsR3;
554 PGIMMMIO2REGION pRegion = &pVM->gim.s.u.Hv.aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
555 AssertPtrReturn(pDevIns, VERR_GIM_DEVICE_NOT_REGISTERED);
556
557 int rc;
558 if (pRegion->fMapped)
559 {
560 /*
561 * Is it already enabled at the given guest-address?
562 */
563 if (pRegion->GCPhysPage == GCPhysTscPage)
564 return VINF_SUCCESS;
565
566 /*
567 * If it's mapped at a different address, unmap the previous address.
568 */
569 rc = gimR3HvDisableTscPage(pVM);
570 AssertRC(rc);
571 }
572
573 /*
574 * Map the TSC-page at the specified address.
575 */
576 Assert(!pRegion->fMapped);
577
578 /** @todo this is buggy when large pages are used due to a PGM limitation, see
579 * @bugref{7532}. Instead of the overlay style mapping, we just
580 * rewrite guest memory directly. */
581#if 0
582 rc = GIMR3Mmio2Map(pVM, pRegion, GCPhysTscPage);
583 if (RT_SUCCESS(rc))
584 {
585 Assert(pRegion->GCPhysPage == GCPhysTscPage);
586
587 /*
588 * Update the TSC scale. Windows guests expect a non-zero TSC sequence, otherwise
589 * they fallback to using the reference count MSR which is not ideal in terms of VM-exits.
590 *
591 * Also, Hyper-V normalizes the time in 10 MHz, see:
592 * http://technet.microsoft.com/it-it/sysinternals/dn553408%28v=vs.110%29
593 */
594 PGIMHVREFTSC pRefTsc = (PGIMHVREFTSC)pRegion->pvPageR3;
595 Assert(pRefTsc);
596
597 PGIMHV pHv = &pVM->gim.s.u.Hv;
598 uint64_t const u64TscKHz = pHv->cTscTicksPerSecond / UINT64_C(1000);
599 uint32_t u32TscSeq = 1;
600 if ( fUseThisTscSeq
601 && uTscSeq < UINT32_C(0xfffffffe))
602 u32TscSeq = uTscSeq + 1;
603 pRefTsc->u32TscSequence = u32TscSeq;
604 pRefTsc->u64TscScale = ((INT64_C(10000) << 32) / u64TscKHz) << 32;
605 pRefTsc->i64TscOffset = 0;
606
607 LogRel(("GIM: HyperV: Enabled TSC page at %#RGp - u64TscScale=%#RX64 u64TscKHz=%#RX64 (%'RU64) Seq=%#RU32\n",
608 GCPhysTscPage, pRefTsc->u64TscScale, u64TscKHz, u64TscKHz, pRefTsc->u32TscSequence));
609
610 TMR3CpuTickParavirtEnable(pVM);
611 return VINF_SUCCESS;
612 }
613 else
614 LogRelFunc(("GIMR3Mmio2Map failed. rc=%Rrc\n", rc));
615 return VERR_GIM_OPERATION_FAILED;
616#else
617 AssertReturn(pRegion->cbRegion == PAGE_SIZE, VERR_GIM_IPE_2);
618 PGIMHVREFTSC pRefTsc = (PGIMHVREFTSC)RTMemAllocZ(PAGE_SIZE);
619 if (RT_UNLIKELY(!pRefTsc))
620 {
621 LogRelFunc(("Failed to alloc %u bytes\n", PAGE_SIZE));
622 return VERR_NO_MEMORY;
623 }
624
625 PGIMHV pHv = &pVM->gim.s.u.Hv;
626 uint64_t const u64TscKHz = pHv->cTscTicksPerSecond / UINT64_C(1000);
627 uint32_t u32TscSeq = 1;
628 if ( fUseThisTscSeq
629 && uTscSeq < UINT32_C(0xfffffffe))
630 u32TscSeq = uTscSeq + 1;
631 pRefTsc->u32TscSequence = u32TscSeq;
632 pRefTsc->u64TscScale = ((INT64_C(10000) << 32) / u64TscKHz) << 32;
633 pRefTsc->i64TscOffset = 0;
634
635 rc = PGMPhysSimpleWriteGCPhys(pVM, GCPhysTscPage, pRefTsc, sizeof(*pRefTsc));
636 if (RT_SUCCESS(rc))
637 {
638 LogRel(("GIM: HyperV: Enabled TSC page at %#RGp - u64TscScale=%#RX64 u64TscKHz=%#RX64 (%'RU64) Seq=%#RU32\n",
639 GCPhysTscPage, pRefTsc->u64TscScale, u64TscKHz, u64TscKHz, pRefTsc->u32TscSequence));
640
641 pRegion->GCPhysPage = GCPhysTscPage;
642 pRegion->fMapped = true;
643 TMR3CpuTickParavirtEnable(pVM);
644 }
645 else
646 {
647 LogRelFunc(("GIM: HyperV: PGMPhysSimpleWriteGCPhys failed. rc=%Rrc\n", rc));
648 rc = VERR_GIM_OPERATION_FAILED;
649 }
650 RTMemFree(pRefTsc);
651 return rc;
652#endif
653}
654
655
656/**
657 * Disables the Hyper-V TSC page.
658 *
659 * @returns VBox status code.
660 * @param pVM Pointer to the VM.
661 */
662VMMR3_INT_DECL(int) gimR3HvDisableTscPage(PVM pVM)
663{
664 PGIMHV pHv = &pVM->gim.s.u.Hv;
665 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
666 if (pRegion->fMapped)
667 {
668#if 0
669 GIMR3Mmio2Unmap(pVM, pRegion);
670 Assert(!pRegion->fMapped);
671#else
672 pRegion->fMapped = false;
673#endif
674 LogRel(("GIM: HyperV: Disabled TSC-page\n"));
675
676 TMR3CpuTickParavirtDisable(pVM);
677 return VINF_SUCCESS;
678 }
679 return VERR_GIM_PVTSC_NOT_ENABLED;
680}
681
682
683/**
684 * Disables the Hyper-V Hypercall page.
685 *
686 * @returns VBox status code.
687 */
688VMMR3_INT_DECL(int) gimR3HvDisableHypercallPage(PVM pVM)
689{
690 PGIMHV pHv = &pVM->gim.s.u.Hv;
691 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
692 if (pRegion->fMapped)
693 {
694#if 0
695 GIMR3Mmio2Unmap(pVM, pRegion);
696 Assert(!pRegion->fMapped);
697#else
698 pRegion->fMapped = false;
699#endif
700 for (VMCPUID i = 0; i < pVM->cCpus; i++)
701 VMMHypercallsDisable(&pVM->aCpus[i]);
702 LogRel(("GIM: HyperV: Disabled Hypercall-page\n"));
703 return VINF_SUCCESS;
704 }
705 return VERR_GIM_HYPERCALLS_NOT_ENABLED;
706}
707
708
709/**
710 * Enables the Hyper-V Hypercall page.
711 *
712 * @returns VBox status code.
713 * @param pVM Pointer to the VM.
714 * @param GCPhysHypercallPage Where to map the hypercall page.
715 */
716VMMR3_INT_DECL(int) gimR3HvEnableHypercallPage(PVM pVM, RTGCPHYS GCPhysHypercallPage)
717{
718 PPDMDEVINSR3 pDevIns = pVM->gim.s.pDevInsR3;
719 PGIMMMIO2REGION pRegion = &pVM->gim.s.u.Hv.aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
720 AssertPtrReturn(pDevIns, VERR_GIM_DEVICE_NOT_REGISTERED);
721
722 if (pRegion->fMapped)
723 {
724 /*
725 * Is it already enabled at the given guest-address?
726 */
727 if (pRegion->GCPhysPage == GCPhysHypercallPage)
728 return VINF_SUCCESS;
729
730 /*
731 * If it's mapped at a different address, unmap the previous address.
732 */
733 int rc2 = gimR3HvDisableHypercallPage(pVM);
734 AssertRC(rc2);
735 }
736
737 /*
738 * Map the hypercall-page at the specified address.
739 */
740 Assert(!pRegion->fMapped);
741
742 /** @todo this is buggy when large pages are used due to a PGM limitation, see
743 * @bugref{7532}. Instead of the overlay style mapping, we just
744 * rewrite guest memory directly. */
745#if 0
746 int rc = GIMR3Mmio2Map(pVM, pRegion, GCPhysHypercallPage);
747 if (RT_SUCCESS(rc))
748 {
749 Assert(pRegion->GCPhysPage == GCPhysHypercallPage);
750
751 /*
752 * Patch the hypercall-page.
753 */
754 size_t cbWritten = 0;
755 rc = VMMPatchHypercall(pVM, pRegion->pvPageR3, PAGE_SIZE, &cbWritten);
756 if ( RT_SUCCESS(rc)
757 && cbWritten < PAGE_SIZE)
758 {
759 uint8_t *pbLast = (uint8_t *)pRegion->pvPageR3 + cbWritten;
760 *pbLast = 0xc3; /* RET */
761
762 /*
763 * Notify VMM that hypercalls are now enabled for all VCPUs.
764 */
765 for (VMCPUID i = 0; i < pVM->cCpus; i++)
766 VMMHypercallsEnable(&pVM->aCpus[i]);
767
768 LogRel(("GIM: HyperV: Enabled hypercalls at %#RGp\n", GCPhysHypercallPage));
769 return VINF_SUCCESS;
770 }
771 else
772 {
773 if (rc == VINF_SUCCESS)
774 rc = VERR_GIM_OPERATION_FAILED;
775 LogRel(("GIM: HyperV: VMMPatchHypercall failed. rc=%Rrc cbWritten=%u\n", rc, cbWritten));
776 }
777
778 GIMR3Mmio2Unmap(pVM, pRegion);
779 }
780
781 LogRel(("GIM: HyperV: GIMR3Mmio2Map failed. rc=%Rrc\n", rc));
782 return rc;
783#else
784 AssertReturn(pRegion->cbRegion == PAGE_SIZE, VERR_GIM_IPE_3);
785 void *pvHypercallPage = RTMemAllocZ(PAGE_SIZE);
786 if (RT_UNLIKELY(!pvHypercallPage))
787 {
788 LogRelFunc(("Failed to alloc %u bytes\n", PAGE_SIZE));
789 return VERR_NO_MEMORY;
790 }
791
792 /*
793 * Patch the hypercall-page.
794 */
795 size_t cbWritten = 0;
796 int rc = VMMPatchHypercall(pVM, pvHypercallPage, PAGE_SIZE, &cbWritten);
797 if ( RT_SUCCESS(rc)
798 && cbWritten < PAGE_SIZE)
799 {
800 uint8_t *pbLast = (uint8_t *)pvHypercallPage + cbWritten;
801 *pbLast = 0xc3; /* RET */
802
803 rc = PGMPhysSimpleWriteGCPhys(pVM, GCPhysHypercallPage, pvHypercallPage, PAGE_SIZE);
804 if (RT_SUCCESS(rc))
805 {
806 /*
807 * Notify VMM that hypercalls are now enabled for all VCPUs.
808 */
809 for (VMCPUID i = 0; i < pVM->cCpus; i++)
810 VMMHypercallsEnable(&pVM->aCpus[i]);
811
812 pRegion->GCPhysPage = GCPhysHypercallPage;
813 pRegion->fMapped = true;
814 LogRel(("GIM: HyperV: Enabled hypercalls at %#RGp\n", GCPhysHypercallPage));
815 }
816 else
817 LogRel(("GIM: HyperV: PGMPhysSimpleWriteGCPhys failed during hypercall page setup. rc=%Rrc\n", rc));
818 }
819 else
820 {
821 if (rc == VINF_SUCCESS)
822 rc = VERR_GIM_OPERATION_FAILED;
823 LogRel(("GIM: HyperV: VMMPatchHypercall failed. rc=%Rrc cbWritten=%u\n", rc, cbWritten));
824 }
825
826 RTMemFree(pvHypercallPage);
827 return rc;
828#endif
829}
830
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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