VirtualBox

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

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

VMM: Fixed almost all the Doxygen warnings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 46.3 KB
Line 
1/* $Id: GIMHv.cpp 58126 2015-10-08 20:59:48Z 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/semaphore.h>
30#include <iprt/spinlock.h>
31
32#include <VBox/vmm/cpum.h>
33#include <VBox/vmm/mm.h>
34#include <VBox/vmm/ssm.h>
35#include <VBox/vmm/vm.h>
36#include <VBox/vmm/hm.h>
37#include <VBox/vmm/pdmapi.h>
38#include <VBox/version.h>
39
40
41/*********************************************************************************************************************************
42* Defined Constants And Macros *
43*********************************************************************************************************************************/
44/**
45 * GIM Hyper-V saved-state version.
46 */
47#define GIM_HV_SAVED_STATE_VERSION UINT32_C(1)
48
49#ifdef VBOX_WITH_STATISTICS
50# define GIMHV_MSRRANGE(a_uFirst, a_uLast, a_szName) \
51 { (a_uFirst), (a_uLast), kCpumMsrRdFn_Gim, kCpumMsrWrFn_Gim, 0, 0, 0, 0, 0, a_szName, { 0 }, { 0 }, { 0 }, { 0 } }
52#else
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 }
55#endif
56
57
58/*********************************************************************************************************************************
59* Global Variables *
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 GIMHV_MSRRANGE(MSR_GIM_HV_RANGE12_START, MSR_GIM_HV_RANGE12_END, "Hyper-V range 12")
79};
80#undef GIMHV_MSRRANGE
81
82
83/*********************************************************************************************************************************
84* Internal Functions *
85*********************************************************************************************************************************/
86static int gimR3HvInitDebugSupport(PVM pVM);
87static void gimR3HvTermDebugSupport(PVM pVM);
88
89
90/**
91 * Initializes the Hyper-V GIM provider.
92 *
93 * @returns VBox status code.
94 * @param pVM The cross context VM structure.
95 */
96VMMR3_INT_DECL(int) gimR3HvInit(PVM pVM)
97{
98 AssertReturn(pVM, VERR_INVALID_PARAMETER);
99 AssertReturn(pVM->gim.s.enmProviderId == GIMPROVIDERID_HYPERV, VERR_INTERNAL_ERROR_5);
100
101 int rc;
102 PGIMHV pHv = &pVM->gim.s.u.Hv;
103
104 /*
105 * Read configuration.
106 */
107 PCFGMNODE pCfgNode = CFGMR3GetChild(CFGMR3GetRoot(pVM), "GIM/HyperV");
108
109 /** @cfgm{/GIM/HyperV/VendorID, string, 'VBoxVBoxVBox'}
110 * The Hyper-V vendor signature, must be 12 characters. */
111 char szVendor[13];
112 rc = CFGMR3QueryStringDef(pCfgNode, "VendorID", szVendor, sizeof(szVendor), "VBoxVBoxVBox");
113 AssertLogRelRCReturn(rc, rc);
114
115 LogRel(("GIM: HyperV: Reporting vendor as '%s'\n", szVendor));
116 if (!RTStrNCmp(szVendor, GIM_HV_VENDOR_MICROSOFT, sizeof(GIM_HV_VENDOR_MICROSOFT) - 1))
117 {
118 LogRel(("GIM: HyperV: Warning! Posing as the Microsoft vendor, guest behavior may be altered!\n"));
119 pHv->fIsVendorMsHv = true;
120 }
121
122 /*
123 * Determine interface capabilities based on the version.
124 */
125 if (!pVM->gim.s.u32Version)
126 {
127 /* Basic features. */
128 pHv->uBaseFeat = 0
129 //| GIM_HV_BASE_FEAT_VP_RUNTIME_MSR
130 | GIM_HV_BASE_FEAT_PART_TIME_REF_COUNT_MSR
131 //| GIM_HV_BASE_FEAT_BASIC_SYNTH_IC
132 //| GIM_HV_BASE_FEAT_SYNTH_TIMER_MSRS
133 | GIM_HV_BASE_FEAT_APIC_ACCESS_MSRS
134 | GIM_HV_BASE_FEAT_HYPERCALL_MSRS
135 | GIM_HV_BASE_FEAT_VP_ID_MSR
136 | GIM_HV_BASE_FEAT_VIRT_SYS_RESET_MSR
137 //| GIM_HV_BASE_FEAT_STAT_PAGES_MSR
138 | GIM_HV_BASE_FEAT_PART_REF_TSC_MSR
139 //| GIM_HV_BASE_FEAT_GUEST_IDLE_STATE_MSR
140 | GIM_HV_BASE_FEAT_TIMER_FREQ_MSRS
141 //| GIM_HV_BASE_FEAT_DEBUG_MSRS
142 ;
143
144 /* Miscellaneous features. */
145 pHv->uMiscFeat = 0
146 //| GIM_HV_MISC_FEAT_GUEST_DEBUGGING
147 //| GIM_HV_MISC_FEAT_XMM_HYPERCALL_INPUT
148 | GIM_HV_MISC_FEAT_TIMER_FREQ
149 | GIM_HV_MISC_FEAT_GUEST_CRASH_MSRS
150 //| GIM_HV_MISC_FEAT_DEBUG_MSRS
151 ;
152
153 /* Hypervisor recommendations to the guest. */
154 pHv->uHyperHints = GIM_HV_HINT_MSR_FOR_SYS_RESET
155 | GIM_HV_HINT_RELAX_TIME_CHECKS;
156
157 /* Expose more if we're posing as Microsoft. */
158 if (pHv->fIsVendorMsHv)
159 {
160 pHv->uMiscFeat |= GIM_HV_MISC_FEAT_GUEST_DEBUGGING
161 | GIM_HV_MISC_FEAT_DEBUG_MSRS;
162
163 pHv->uPartFlags |= GIM_HV_PART_FLAGS_DEBUGGING;
164 }
165 }
166
167 /*
168 * Populate the required fields in MMIO2 region records for registering.
169 */
170 AssertCompile(GIM_HV_PAGE_SIZE == PAGE_SIZE);
171 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
172 pRegion->iRegion = GIM_HV_HYPERCALL_PAGE_REGION_IDX;
173 pRegion->fRCMapping = false;
174 pRegion->cbRegion = PAGE_SIZE; /* Sanity checked in gimR3HvLoad(), gimR3HvEnableTscPage() & gimR3HvEnableHypercallPage() */
175 pRegion->GCPhysPage = NIL_RTGCPHYS;
176 RTStrCopy(pRegion->szDescription, sizeof(pRegion->szDescription), "Hyper-V hypercall page");
177
178 pRegion = &pHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
179 pRegion->iRegion = GIM_HV_REF_TSC_PAGE_REGION_IDX;
180 pRegion->fRCMapping = false;
181 pRegion->cbRegion = PAGE_SIZE; /* Sanity checked in gimR3HvLoad(), gimR3HvEnableTscPage() & gimR3HvEnableHypercallPage() */
182 pRegion->GCPhysPage = NIL_RTGCPHYS;
183 RTStrCopy(pRegion->szDescription, sizeof(pRegion->szDescription), "Hyper-V TSC page");
184
185 /*
186 * Make sure the CPU ID bit are in accordance to the Hyper-V
187 * requirement and other paranoia checks.
188 * See "Requirements for implementing the Microsoft hypervisor interface" spec.
189 */
190 Assert(!(pHv->uPartFlags & ( GIM_HV_PART_FLAGS_CREATE_PART
191 | GIM_HV_PART_FLAGS_ACCESS_MEMORY_POOL
192 | GIM_HV_PART_FLAGS_ACCESS_PART_ID
193 | GIM_HV_PART_FLAGS_ADJUST_MSG_BUFFERS
194 | GIM_HV_PART_FLAGS_CREATE_PORT
195 | GIM_HV_PART_FLAGS_ACCESS_STATS
196 | GIM_HV_PART_FLAGS_CPU_MGMT
197 | GIM_HV_PART_FLAGS_CPU_PROFILER)));
198 Assert((pHv->uBaseFeat & (GIM_HV_BASE_FEAT_HYPERCALL_MSRS | GIM_HV_BASE_FEAT_VP_ID_MSR))
199 == (GIM_HV_BASE_FEAT_HYPERCALL_MSRS | GIM_HV_BASE_FEAT_VP_ID_MSR));
200 for (unsigned i = 0; i < RT_ELEMENTS(pHv->aMmio2Regions); i++)
201 {
202 PCGIMMMIO2REGION pcCur = &pHv->aMmio2Regions[i];
203 Assert(!pcCur->fRCMapping);
204 Assert(!pcCur->fMapped);
205 Assert(pcCur->GCPhysPage == NIL_RTGCPHYS);
206 }
207
208 /*
209 * Expose HVP (Hypervisor Present) bit to the guest.
210 */
211 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_HVP);
212
213 /*
214 * Modify the standard hypervisor leaves for Hyper-V.
215 */
216 CPUMCPUIDLEAF HyperLeaf;
217 RT_ZERO(HyperLeaf);
218 HyperLeaf.uLeaf = UINT32_C(0x40000000);
219 HyperLeaf.uEax = UINT32_C(0x40000006); /* Minimum value for Hyper-V is 0x40000005. */
220 /*
221 * Don't report vendor as 'Microsoft Hv'[1] by default, see @bugref{7270#c152}.
222 * [1]: ebx=0x7263694d ('rciM') ecx=0x666f736f ('foso') edx=0x76482074 ('vH t')
223 */
224 {
225 uint32_t uVendorEbx;
226 uint32_t uVendorEcx;
227 uint32_t uVendorEdx;
228 uVendorEbx = ((uint32_t)szVendor[ 3]) << 24 | ((uint32_t)szVendor[ 2]) << 16 | ((uint32_t)szVendor[1]) << 8
229 | (uint32_t)szVendor[ 0];
230 uVendorEcx = ((uint32_t)szVendor[ 7]) << 24 | ((uint32_t)szVendor[ 6]) << 16 | ((uint32_t)szVendor[5]) << 8
231 | (uint32_t)szVendor[ 4];
232 uVendorEdx = ((uint32_t)szVendor[11]) << 24 | ((uint32_t)szVendor[10]) << 16 | ((uint32_t)szVendor[9]) << 8
233 | (uint32_t)szVendor[ 8];
234 HyperLeaf.uEbx = uVendorEbx;
235 HyperLeaf.uEcx = uVendorEcx;
236 HyperLeaf.uEdx = uVendorEdx;
237 }
238 rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
239 AssertLogRelRCReturn(rc, rc);
240
241 HyperLeaf.uLeaf = UINT32_C(0x40000001);
242 HyperLeaf.uEax = 0x31237648; /* 'Hv#1' */
243 HyperLeaf.uEbx = 0; /* Reserved */
244 HyperLeaf.uEcx = 0; /* Reserved */
245 HyperLeaf.uEdx = 0; /* Reserved */
246 rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
247 AssertLogRelRCReturn(rc, rc);
248
249 /*
250 * Add Hyper-V specific leaves.
251 */
252 HyperLeaf.uLeaf = UINT32_C(0x40000002); /* MBZ until MSR_GIM_HV_GUEST_OS_ID is set by the guest. */
253 HyperLeaf.uEax = 0;
254 HyperLeaf.uEbx = 0;
255 HyperLeaf.uEcx = 0;
256 HyperLeaf.uEdx = 0;
257 rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
258 AssertLogRelRCReturn(rc, rc);
259
260 HyperLeaf.uLeaf = UINT32_C(0x40000003);
261 HyperLeaf.uEax = pHv->uBaseFeat;
262 HyperLeaf.uEbx = pHv->uPartFlags;
263 HyperLeaf.uEcx = pHv->uPowMgmtFeat;
264 HyperLeaf.uEdx = pHv->uMiscFeat;
265 rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
266 AssertLogRelRCReturn(rc, rc);
267
268 HyperLeaf.uLeaf = UINT32_C(0x40000004);
269 HyperLeaf.uEax = pHv->uHyperHints;
270 HyperLeaf.uEbx = 0xffffffff;
271 HyperLeaf.uEcx = 0;
272 HyperLeaf.uEdx = 0;
273 rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
274 AssertLogRelRCReturn(rc, rc);
275
276 /*
277 * Insert all MSR ranges of Hyper-V.
278 */
279 for (unsigned i = 0; i < RT_ELEMENTS(g_aMsrRanges_HyperV); i++)
280 {
281 rc = CPUMR3MsrRangesInsert(pVM, &g_aMsrRanges_HyperV[i]);
282 AssertLogRelRCReturn(rc, rc);
283 }
284
285 /*
286 * Setup non-zero MSRs.
287 */
288 if (pHv->uMiscFeat & GIM_HV_MISC_FEAT_GUEST_CRASH_MSRS)
289 pHv->uCrashCtl = MSR_GIM_HV_CRASH_CTL_NOTIFY_BIT;
290
291 /*
292 * Setup guest-host debugging connection.
293 */
294 if (pHv->uMiscFeat & GIM_HV_MISC_FEAT_GUEST_DEBUGGING)
295 {
296 rc = gimR3HvInitDebugSupport(pVM);
297 AssertLogRelRCReturn(rc, rc);
298 }
299
300 return VINF_SUCCESS;
301}
302
303
304/**
305 * Initializes remaining bits of the Hyper-V provider.
306 *
307 * This is called after initializing HM and almost all other VMM components.
308 *
309 * @returns VBox status code.
310 * @param pVM The cross context VM structure.
311 */
312VMMR3_INT_DECL(int) gimR3HvInitCompleted(PVM pVM)
313{
314 PGIMHV pHv = &pVM->gim.s.u.Hv;
315 pHv->cTscTicksPerSecond = TMCpuTicksPerSecond(pVM);
316
317 /*
318 * Determine interface capabilities based on the version.
319 */
320 if (!pVM->gim.s.u32Version)
321 {
322 /* Hypervisor capabilities; features used by the hypervisor. */
323 pHv->uHyperCaps = HMIsNestedPagingActive(pVM) ? GIM_HV_HOST_FEAT_NESTED_PAGING : 0;
324 pHv->uHyperCaps |= HMAreMsrBitmapsAvailable(pVM) ? GIM_HV_HOST_FEAT_MSR_BITMAP : 0;
325 }
326
327 CPUMCPUIDLEAF HyperLeaf;
328 RT_ZERO(HyperLeaf);
329 HyperLeaf.uLeaf = UINT32_C(0x40000006);
330 HyperLeaf.uEax = pHv->uHyperCaps;
331 HyperLeaf.uEbx = 0;
332 HyperLeaf.uEcx = 0;
333 HyperLeaf.uEdx = 0;
334 int rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
335 AssertLogRelRCReturn(rc, rc);
336
337 return rc;
338}
339
340
341#if 0
342VMMR3_INT_DECL(int) gimR3HvInitFinalize(PVM pVM)
343{
344 pVM->gim.s.pfnHypercallR3 = &GIMHvHypercall;
345 if (!HMIsEnabled(pVM))
346 {
347 rc = PDMR3LdrGetSymbolRC(pVM, NULL /* pszModule */, GIMHV_HYPERCALL, &pVM->gim.s.pfnHypercallRC);
348 AssertRCReturn(rc, rc);
349 }
350 rc = PDMR3LdrGetSymbolR0(pVM, NULL /* pszModule */, GIMHV_HYPERCALL, &pVM->gim.s.pfnHypercallR0);
351 AssertRCReturn(rc, rc);
352}
353#endif
354
355
356/**
357 * Terminates the Hyper-V GIM provider.
358 *
359 * @returns VBox status code.
360 * @param pVM The cross context VM structure.
361 */
362VMMR3_INT_DECL(int) gimR3HvTerm(PVM pVM)
363{
364 gimR3HvReset(pVM);
365
366 PGIMHV pHv = &pVM->gim.s.u.Hv;
367 if (pHv->uMiscFeat & GIM_HV_MISC_FEAT_GUEST_DEBUGGING)
368 gimR3HvTermDebugSupport(pVM);
369 return VINF_SUCCESS;
370}
371
372
373/**
374 * Applies relocations to data and code managed by this component.
375 *
376 * This function will be called at init and whenever the VMM need to relocate
377 * itself inside the GC.
378 *
379 * @param pVM The cross context VM structure.
380 * @param offDelta Relocation delta relative to old location.
381 */
382VMMR3_INT_DECL(void) gimR3HvRelocate(PVM pVM, RTGCINTPTR offDelta)
383{
384#if 0
385 int rc = PDMR3LdrGetSymbolRC(pVM, NULL /* pszModule */, GIMHV_HYPERCALL, &pVM->gim.s.pfnHypercallRC);
386 AssertFatalRC(rc);
387#endif
388}
389
390
391/**
392 * This resets Hyper-V provider MSRs and unmaps whatever Hyper-V regions that
393 * the guest may have mapped.
394 *
395 * This is called when the VM is being reset.
396 *
397 * @param pVM The cross context VM structure.
398 *
399 * @thread EMT(0).
400 */
401VMMR3_INT_DECL(void) gimR3HvReset(PVM pVM)
402{
403 VM_ASSERT_EMT0(pVM);
404
405 /*
406 * Unmap MMIO2 pages that the guest may have setup.
407 */
408 LogRel(("GIM: HyperV: Resetting MMIO2 regions and MSRs\n"));
409 PGIMHV pHv = &pVM->gim.s.u.Hv;
410 for (unsigned i = 0; i < RT_ELEMENTS(pHv->aMmio2Regions); i++)
411 {
412 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[i];
413#if 0
414 GIMR3Mmio2Unmap(pVM, pRegion);
415#else
416 pRegion->fMapped = false;
417 pRegion->GCPhysPage = NIL_RTGCPHYS;
418#endif
419 }
420
421 /*
422 * Reset MSRs (Careful! Don't reset non-zero MSRs).
423 */
424 pHv->u64GuestOsIdMsr = 0;
425 pHv->u64HypercallMsr = 0;
426 pHv->u64TscPageMsr = 0;
427 pHv->uCrashP0 = 0;
428 pHv->uCrashP1 = 0;
429 pHv->uCrashP2 = 0;
430 pHv->uCrashP3 = 0;
431 pHv->uCrashP4 = 0;
432}
433
434
435/**
436 * Returns a pointer to the MMIO2 regions supported by Hyper-V.
437 *
438 * @returns Pointer to an array of MMIO2 regions.
439 * @param pVM The cross context VM structure.
440 * @param pcRegions Where to store the number of regions in the array.
441 */
442VMMR3_INT_DECL(PGIMMMIO2REGION) gimR3HvGetMmio2Regions(PVM pVM, uint32_t *pcRegions)
443{
444 Assert(GIMIsEnabled(pVM));
445 PGIMHV pHv = &pVM->gim.s.u.Hv;
446
447 *pcRegions = RT_ELEMENTS(pHv->aMmio2Regions);
448 Assert(*pcRegions <= UINT8_MAX); /* See PGMR3PhysMMIO2Register(). */
449 return pHv->aMmio2Regions;
450}
451
452
453/**
454 * Hyper-V state-save operation.
455 *
456 * @returns VBox status code.
457 * @param pVM The cross context VM structure.
458 * @param pSSM Pointer to the SSM handle.
459 */
460VMMR3_INT_DECL(int) gimR3HvSave(PVM pVM, PSSMHANDLE pSSM)
461{
462 PCGIMHV pcHv = &pVM->gim.s.u.Hv;
463
464 /*
465 * Save the Hyper-V SSM version.
466 */
467 SSMR3PutU32(pSSM, GIM_HV_SAVED_STATE_VERSION);
468
469 /*
470 * Save per-VM MSRs.
471 */
472 SSMR3PutU64(pSSM, pcHv->u64GuestOsIdMsr);
473 SSMR3PutU64(pSSM, pcHv->u64HypercallMsr);
474 SSMR3PutU64(pSSM, pcHv->u64TscPageMsr);
475
476 /*
477 * Save Hyper-V features / capabilities.
478 */
479 SSMR3PutU32(pSSM, pcHv->uBaseFeat);
480 SSMR3PutU32(pSSM, pcHv->uPartFlags);
481 SSMR3PutU32(pSSM, pcHv->uPowMgmtFeat);
482 SSMR3PutU32(pSSM, pcHv->uMiscFeat);
483 SSMR3PutU32(pSSM, pcHv->uHyperHints);
484 SSMR3PutU32(pSSM, pcHv->uHyperCaps);
485
486 /*
487 * Save the Hypercall region.
488 */
489 PCGIMMMIO2REGION pcRegion = &pcHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
490 SSMR3PutU8(pSSM, pcRegion->iRegion);
491 SSMR3PutBool(pSSM, pcRegion->fRCMapping);
492 SSMR3PutU32(pSSM, pcRegion->cbRegion);
493 SSMR3PutGCPhys(pSSM, pcRegion->GCPhysPage);
494 SSMR3PutStrZ(pSSM, pcRegion->szDescription);
495
496 /*
497 * Save the reference TSC region.
498 */
499 pcRegion = &pcHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
500 SSMR3PutU8(pSSM, pcRegion->iRegion);
501 SSMR3PutBool(pSSM, pcRegion->fRCMapping);
502 SSMR3PutU32(pSSM, pcRegion->cbRegion);
503 SSMR3PutGCPhys(pSSM, pcRegion->GCPhysPage);
504 SSMR3PutStrZ(pSSM, pcRegion->szDescription);
505 /* Save the TSC sequence so we can bump it on restore (as the CPU frequency/offset may change). */
506 uint32_t uTscSequence = 0;
507 if ( pcRegion->fMapped
508 && MSR_GIM_HV_REF_TSC_IS_ENABLED(pcHv->u64TscPageMsr))
509 {
510 PCGIMHVREFTSC pcRefTsc = (PCGIMHVREFTSC)pcRegion->pvPageR3;
511 uTscSequence = pcRefTsc->u32TscSequence;
512 }
513
514 return SSMR3PutU32(pSSM, uTscSequence);
515}
516
517
518/**
519 * Hyper-V state-load operation, final pass.
520 *
521 * @returns VBox status code.
522 * @param pVM The cross context VM structure.
523 * @param pSSM Pointer to the SSM handle.
524 * @param uSSMVersion The GIM saved-state version.
525 */
526VMMR3_INT_DECL(int) gimR3HvLoad(PVM pVM, PSSMHANDLE pSSM, uint32_t uSSMVersion)
527{
528 /*
529 * Load the Hyper-V SSM version first.
530 */
531 uint32_t uHvSavedStatVersion;
532 int rc = SSMR3GetU32(pSSM, &uHvSavedStatVersion);
533 AssertRCReturn(rc, rc);
534 if (uHvSavedStatVersion != GIM_HV_SAVED_STATE_VERSION)
535 return SSMR3SetLoadError(pSSM, VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION, RT_SRC_POS,
536 N_("Unsupported Hyper-V saved-state version %u (expected %u)."), uHvSavedStatVersion,
537 GIM_HV_SAVED_STATE_VERSION);
538
539 /*
540 * Update the TSC frequency from TM.
541 */
542 PGIMHV pHv = &pVM->gim.s.u.Hv;
543 pHv->cTscTicksPerSecond = TMCpuTicksPerSecond(pVM);
544
545 /*
546 * Load per-VM MSRs.
547 */
548 SSMR3GetU64(pSSM, &pHv->u64GuestOsIdMsr);
549 SSMR3GetU64(pSSM, &pHv->u64HypercallMsr);
550 SSMR3GetU64(pSSM, &pHv->u64TscPageMsr);
551
552 /*
553 * Load Hyper-V features / capabilities.
554 */
555 SSMR3GetU32(pSSM, &pHv->uBaseFeat);
556 SSMR3GetU32(pSSM, &pHv->uPartFlags);
557 SSMR3GetU32(pSSM, &pHv->uPowMgmtFeat);
558 SSMR3GetU32(pSSM, &pHv->uMiscFeat);
559 SSMR3GetU32(pSSM, &pHv->uHyperHints);
560 SSMR3GetU32(pSSM, &pHv->uHyperCaps);
561
562 /*
563 * Load and enable the Hypercall region.
564 */
565 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
566 SSMR3GetU8(pSSM, &pRegion->iRegion);
567 SSMR3GetBool(pSSM, &pRegion->fRCMapping);
568 SSMR3GetU32(pSSM, &pRegion->cbRegion);
569 SSMR3GetGCPhys(pSSM, &pRegion->GCPhysPage);
570 rc = SSMR3GetStrZ(pSSM, pRegion->szDescription, sizeof(pRegion->szDescription));
571 AssertRCReturn(rc, rc);
572
573 if (pRegion->cbRegion != PAGE_SIZE)
574 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Hypercall page region size %u invalid, expected %u"),
575 pRegion->cbRegion, PAGE_SIZE);
576
577 if (MSR_GIM_HV_HYPERCALL_PAGE_IS_ENABLED(pHv->u64HypercallMsr))
578 {
579 Assert(pRegion->GCPhysPage != NIL_RTGCPHYS);
580 if (RT_LIKELY(pRegion->fRegistered))
581 {
582 rc = gimR3HvEnableHypercallPage(pVM, pRegion->GCPhysPage);
583 if (RT_FAILURE(rc))
584 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Failed to enable the hypercall page. GCPhys=%#RGp rc=%Rrc"),
585 pRegion->GCPhysPage, rc);
586 }
587 else
588 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Hypercall MMIO2 region not registered. Missing GIM device?!"));
589 }
590
591 /*
592 * Load and enable the reference TSC region.
593 */
594 uint32_t uTscSequence;
595 pRegion = &pHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
596 SSMR3GetU8(pSSM, &pRegion->iRegion);
597 SSMR3GetBool(pSSM, &pRegion->fRCMapping);
598 SSMR3GetU32(pSSM, &pRegion->cbRegion);
599 SSMR3GetGCPhys(pSSM, &pRegion->GCPhysPage);
600 SSMR3GetStrZ(pSSM, pRegion->szDescription, sizeof(pRegion->szDescription));
601 rc = SSMR3GetU32(pSSM, &uTscSequence);
602 AssertRCReturn(rc, rc);
603
604 if (pRegion->cbRegion != PAGE_SIZE)
605 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("TSC page region size %u invalid, expected %u"),
606 pRegion->cbRegion, PAGE_SIZE);
607
608 if (MSR_GIM_HV_REF_TSC_IS_ENABLED(pHv->u64TscPageMsr))
609 {
610 Assert(pRegion->GCPhysPage != NIL_RTGCPHYS);
611 if (pRegion->fRegistered)
612 {
613 rc = gimR3HvEnableTscPage(pVM, pRegion->GCPhysPage, true /* fUseThisTscSeq */, uTscSequence);
614 if (RT_FAILURE(rc))
615 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Failed to enable the TSC page. GCPhys=%#RGp rc=%Rrc"),
616 pRegion->GCPhysPage, rc);
617 }
618 else
619 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("TSC-page MMIO2 region not registered. Missing GIM device?!"));
620 }
621
622 return rc;
623}
624
625
626/**
627 * Enables the Hyper-V TSC page.
628 *
629 * @returns VBox status code.
630 * @param pVM The cross context VM structure.
631 * @param GCPhysTscPage Where to map the TSC page.
632 * @param fUseThisTscSeq Whether to set the TSC sequence number to the one
633 * specified in @a uTscSeq.
634 * @param uTscSeq The TSC sequence value to use. Ignored if
635 * @a fUseThisTscSeq is false.
636 */
637VMMR3_INT_DECL(int) gimR3HvEnableTscPage(PVM pVM, RTGCPHYS GCPhysTscPage, bool fUseThisTscSeq, uint32_t uTscSeq)
638{
639 PPDMDEVINSR3 pDevIns = pVM->gim.s.pDevInsR3;
640 PGIMMMIO2REGION pRegion = &pVM->gim.s.u.Hv.aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
641 AssertPtrReturn(pDevIns, VERR_GIM_DEVICE_NOT_REGISTERED);
642
643 int rc;
644 if (pRegion->fMapped)
645 {
646 /*
647 * Is it already enabled at the given guest-address?
648 */
649 if (pRegion->GCPhysPage == GCPhysTscPage)
650 return VINF_SUCCESS;
651
652 /*
653 * If it's mapped at a different address, unmap the previous address.
654 */
655 rc = gimR3HvDisableTscPage(pVM);
656 AssertRC(rc);
657 }
658
659 /*
660 * Map the TSC-page at the specified address.
661 */
662 Assert(!pRegion->fMapped);
663
664 /** @todo this is buggy when large pages are used due to a PGM limitation, see
665 * @bugref{7532}. Instead of the overlay style mapping, we just
666 * rewrite guest memory directly. */
667#if 0
668 rc = GIMR3Mmio2Map(pVM, pRegion, GCPhysTscPage);
669 if (RT_SUCCESS(rc))
670 {
671 Assert(pRegion->GCPhysPage == GCPhysTscPage);
672
673 /*
674 * Update the TSC scale. Windows guests expect a non-zero TSC sequence, otherwise
675 * they fallback to using the reference count MSR which is not ideal in terms of VM-exits.
676 *
677 * Also, Hyper-V normalizes the time in 10 MHz, see:
678 * http://technet.microsoft.com/it-it/sysinternals/dn553408%28v=vs.110%29
679 */
680 PGIMHVREFTSC pRefTsc = (PGIMHVREFTSC)pRegion->pvPageR3;
681 Assert(pRefTsc);
682
683 PGIMHV pHv = &pVM->gim.s.u.Hv;
684 uint64_t const u64TscKHz = pHv->cTscTicksPerSecond / UINT64_C(1000);
685 uint32_t u32TscSeq = 1;
686 if ( fUseThisTscSeq
687 && uTscSeq < UINT32_C(0xfffffffe))
688 u32TscSeq = uTscSeq + 1;
689 pRefTsc->u32TscSequence = u32TscSeq;
690 pRefTsc->u64TscScale = ((INT64_C(10000) << 32) / u64TscKHz) << 32;
691 pRefTsc->i64TscOffset = 0;
692
693 LogRel(("GIM: HyperV: Enabled TSC page at %#RGp - u64TscScale=%#RX64 u64TscKHz=%#RX64 (%'RU64) Seq=%#RU32\n",
694 GCPhysTscPage, pRefTsc->u64TscScale, u64TscKHz, u64TscKHz, pRefTsc->u32TscSequence));
695
696 TMR3CpuTickParavirtEnable(pVM);
697 return VINF_SUCCESS;
698 }
699 else
700 LogRelFunc(("GIMR3Mmio2Map failed. rc=%Rrc\n", rc));
701 return VERR_GIM_OPERATION_FAILED;
702#else
703 AssertReturn(pRegion->cbRegion == PAGE_SIZE, VERR_GIM_IPE_2);
704 PGIMHVREFTSC pRefTsc = (PGIMHVREFTSC)RTMemAllocZ(PAGE_SIZE);
705 if (RT_UNLIKELY(!pRefTsc))
706 {
707 LogRelFunc(("Failed to alloc %u bytes\n", PAGE_SIZE));
708 return VERR_NO_MEMORY;
709 }
710
711 PGIMHV pHv = &pVM->gim.s.u.Hv;
712 uint64_t const u64TscKHz = pHv->cTscTicksPerSecond / UINT64_C(1000);
713 uint32_t u32TscSeq = 1;
714 if ( fUseThisTscSeq
715 && uTscSeq < UINT32_C(0xfffffffe))
716 u32TscSeq = uTscSeq + 1;
717 pRefTsc->u32TscSequence = u32TscSeq;
718 pRefTsc->u64TscScale = ((INT64_C(10000) << 32) / u64TscKHz) << 32;
719 pRefTsc->i64TscOffset = 0;
720
721 rc = PGMPhysSimpleWriteGCPhys(pVM, GCPhysTscPage, pRefTsc, sizeof(*pRefTsc));
722 if (RT_SUCCESS(rc))
723 {
724 LogRel(("GIM: HyperV: Enabled TSC page at %#RGp - u64TscScale=%#RX64 u64TscKHz=%#RX64 (%'RU64) Seq=%#RU32\n",
725 GCPhysTscPage, pRefTsc->u64TscScale, u64TscKHz, u64TscKHz, pRefTsc->u32TscSequence));
726
727 pRegion->GCPhysPage = GCPhysTscPage;
728 pRegion->fMapped = true;
729 TMR3CpuTickParavirtEnable(pVM);
730 }
731 else
732 {
733 LogRelFunc(("GIM: HyperV: PGMPhysSimpleWriteGCPhys failed. rc=%Rrc\n", rc));
734 rc = VERR_GIM_OPERATION_FAILED;
735 }
736 RTMemFree(pRefTsc);
737 return rc;
738#endif
739}
740
741
742/**
743 * Disables the Hyper-V TSC page.
744 *
745 * @returns VBox status code.
746 * @param pVM The cross context VM structure.
747 */
748VMMR3_INT_DECL(int) gimR3HvDisableTscPage(PVM pVM)
749{
750 PGIMHV pHv = &pVM->gim.s.u.Hv;
751 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
752 if (pRegion->fMapped)
753 {
754#if 0
755 GIMR3Mmio2Unmap(pVM, pRegion);
756 Assert(!pRegion->fMapped);
757#else
758 pRegion->fMapped = false;
759#endif
760 LogRel(("GIM: HyperV: Disabled TSC-page\n"));
761
762 TMR3CpuTickParavirtDisable(pVM);
763 return VINF_SUCCESS;
764 }
765 return VERR_GIM_PVTSC_NOT_ENABLED;
766}
767
768
769/**
770 * Disables the Hyper-V Hypercall page.
771 *
772 * @returns VBox status code.
773 */
774VMMR3_INT_DECL(int) gimR3HvDisableHypercallPage(PVM pVM)
775{
776 PGIMHV pHv = &pVM->gim.s.u.Hv;
777 PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
778 if (pRegion->fMapped)
779 {
780#if 0
781 GIMR3Mmio2Unmap(pVM, pRegion);
782 Assert(!pRegion->fMapped);
783#else
784 pRegion->fMapped = false;
785#endif
786 LogRel(("GIM: HyperV: Disabled Hypercall-page\n"));
787 return VINF_SUCCESS;
788 }
789 return VERR_GIM_HYPERCALLS_NOT_ENABLED;
790}
791
792
793/**
794 * Enables the Hyper-V Hypercall page.
795 *
796 * @returns VBox status code.
797 * @param pVM The cross context VM structure.
798 * @param GCPhysHypercallPage Where to map the hypercall page.
799 */
800VMMR3_INT_DECL(int) gimR3HvEnableHypercallPage(PVM pVM, RTGCPHYS GCPhysHypercallPage)
801{
802 PPDMDEVINSR3 pDevIns = pVM->gim.s.pDevInsR3;
803 PGIMMMIO2REGION pRegion = &pVM->gim.s.u.Hv.aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
804 AssertPtrReturn(pDevIns, VERR_GIM_DEVICE_NOT_REGISTERED);
805
806 if (pRegion->fMapped)
807 {
808 /*
809 * Is it already enabled at the given guest-address?
810 */
811 if (pRegion->GCPhysPage == GCPhysHypercallPage)
812 return VINF_SUCCESS;
813
814 /*
815 * If it's mapped at a different address, unmap the previous address.
816 */
817 int rc2 = gimR3HvDisableHypercallPage(pVM);
818 AssertRC(rc2);
819 }
820
821 /*
822 * Map the hypercall-page at the specified address.
823 */
824 Assert(!pRegion->fMapped);
825
826 /** @todo this is buggy when large pages are used due to a PGM limitation, see
827 * @bugref{7532}. Instead of the overlay style mapping, we just
828 * rewrite guest memory directly. */
829#if 0
830 int rc = GIMR3Mmio2Map(pVM, pRegion, GCPhysHypercallPage);
831 if (RT_SUCCESS(rc))
832 {
833 Assert(pRegion->GCPhysPage == GCPhysHypercallPage);
834
835 /*
836 * Patch the hypercall-page.
837 */
838 size_t cbWritten = 0;
839 rc = VMMPatchHypercall(pVM, pRegion->pvPageR3, PAGE_SIZE, &cbWritten);
840 if ( RT_SUCCESS(rc)
841 && cbWritten < PAGE_SIZE)
842 {
843 uint8_t *pbLast = (uint8_t *)pRegion->pvPageR3 + cbWritten;
844 *pbLast = 0xc3; /* RET */
845
846 /*
847 * Notify VMM that hypercalls are now enabled for all VCPUs.
848 */
849 for (VMCPUID i = 0; i < pVM->cCpus; i++)
850 VMMHypercallsEnable(&pVM->aCpus[i]);
851
852 LogRel(("GIM: HyperV: Enabled hypercall page at %#RGp\n", GCPhysHypercallPage));
853 return VINF_SUCCESS;
854 }
855 else
856 {
857 if (rc == VINF_SUCCESS)
858 rc = VERR_GIM_OPERATION_FAILED;
859 LogRel(("GIM: HyperV: VMMPatchHypercall failed. rc=%Rrc cbWritten=%u\n", rc, cbWritten));
860 }
861
862 GIMR3Mmio2Unmap(pVM, pRegion);
863 }
864
865 LogRel(("GIM: HyperV: GIMR3Mmio2Map failed. rc=%Rrc\n", rc));
866 return rc;
867#else
868 AssertReturn(pRegion->cbRegion == PAGE_SIZE, VERR_GIM_IPE_3);
869 void *pvHypercallPage = RTMemAllocZ(PAGE_SIZE);
870 if (RT_UNLIKELY(!pvHypercallPage))
871 {
872 LogRelFunc(("Failed to alloc %u bytes\n", PAGE_SIZE));
873 return VERR_NO_MEMORY;
874 }
875
876 /*
877 * Patch the hypercall-page.
878 */
879 size_t cbWritten = 0;
880 int rc = VMMPatchHypercall(pVM, pvHypercallPage, PAGE_SIZE, &cbWritten);
881 if ( RT_SUCCESS(rc)
882 && cbWritten < PAGE_SIZE)
883 {
884 uint8_t *pbLast = (uint8_t *)pvHypercallPage + cbWritten;
885 *pbLast = 0xc3; /* RET */
886
887 rc = PGMPhysSimpleWriteGCPhys(pVM, GCPhysHypercallPage, pvHypercallPage, PAGE_SIZE);
888 if (RT_SUCCESS(rc))
889 {
890 pRegion->GCPhysPage = GCPhysHypercallPage;
891 pRegion->fMapped = true;
892 LogRel(("GIM: HyperV: Enabled hypercall page at %#RGp\n", GCPhysHypercallPage));
893 }
894 else
895 LogRel(("GIM: HyperV: PGMPhysSimpleWriteGCPhys failed during hypercall page setup. rc=%Rrc\n", rc));
896 }
897 else
898 {
899 if (rc == VINF_SUCCESS)
900 rc = VERR_GIM_OPERATION_FAILED;
901 LogRel(("GIM: HyperV: VMMPatchHypercall failed. rc=%Rrc cbWritten=%u\n", rc, cbWritten));
902 }
903
904 RTMemFree(pvHypercallPage);
905 return rc;
906#endif
907}
908
909
910/**
911 * Initializes Hyper-V guest debugging support.
912 *
913 * @returns VBox status code.
914 * @param pVM The cross context VM structure.
915 */
916static int gimR3HvInitDebugSupport(PVM pVM)
917{
918 int rc = VINF_SUCCESS;
919 PGIMHV pHv = &pVM->gim.s.u.Hv;
920 pHv->pbHypercallIn = (uint8_t *)RTMemAllocZ(GIM_HV_PAGE_SIZE);
921 if (RT_LIKELY(pHv->pbHypercallIn))
922 {
923 pHv->pbHypercallOut = (uint8_t *)RTMemAllocZ(GIM_HV_PAGE_SIZE);
924 if (RT_LIKELY(pHv->pbHypercallOut))
925 return VINF_SUCCESS;
926 RTMemFree(pHv->pbHypercallIn);
927 }
928 return VERR_NO_MEMORY;
929}
930
931
932/**
933 * Terminates Hyper-V guest debugging support.
934 *
935 * @param pVM The cross context VM structure.
936 */
937static void gimR3HvTermDebugSupport(PVM pVM)
938{
939 PGIMHV pHv = &pVM->gim.s.u.Hv;
940 RTMemFree(pHv->pbHypercallIn);
941 pHv->pbHypercallIn = NULL;
942
943 RTMemFree(pHv->pbHypercallOut);
944 pHv->pbHypercallOut = NULL;
945}
946
947
948/**
949 * Reads data from a debugger connection, asynchronous.
950 *
951 * @returns VBox status code.
952 * @param pVM The cross context VM structure.
953 * @param pvBuf Where to read the data.
954 * @param cbBuf Size of the read buffer @a pvBuf.
955 * @param pcbRead Where to store how many bytes were really read.
956 * @param cMsTimeout Timeout of the read operation in milliseconds.
957 *
958 * @thread EMT.
959 */
960static int gimR3HvDebugRead(PVM pVM, void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead, uint32_t cMsTimeout)
961{
962 NOREF(cMsTimeout); /** @todo implement */
963 PGIMHV pHv = &pVM->gim.s.u.Hv;
964 AssertCompile(sizeof(size_t) >= sizeof(uint32_t));
965 size_t cbRead = cbBuf;
966 int rc = GIMR3DebugRead(pVM, pvBuf, &cbRead);
967 *pcbRead = (uint32_t)cbRead;
968 return rc;
969}
970
971
972/**
973 * Writes data to the debugger connection, asynchronous.
974 *
975 * @returns VBox status code.
976 * @param pVM The cross context VM structure.
977 * @param pvBuf Pointer to the data to be written.
978 * @param cbBuf Size of the write buffer @a pvBuf.
979 * @param pcbWritten Where to store how many bytes were really written.
980 *
981 * @thread EMT.
982 */
983static int gimR3HvDebugWrite(PVM pVM, void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten)
984{
985 PGIMHV pHv = &pVM->gim.s.u.Hv;
986 AssertCompile(sizeof(size_t) >= sizeof(uint32_t));
987 size_t cbWrite = cbBuf;
988 int rc = GIMR3DebugWrite(pVM, pvBuf, &cbWrite);
989 *pcbWritten = (uint32_t)cbWrite;
990 return rc;
991}
992
993
994/**
995 * Performs the HvPostDebugData hypercall.
996 *
997 * @returns VBox status code.
998 * @param pVM The cross context VM structure.
999 * @param GCPhysOut Where to write the hypercall output parameters after
1000 * performing the hypercall.
1001 * @param prcHv Where to store the result of the hypercall operation.
1002 *
1003 * @thread EMT.
1004 */
1005VMMR3_INT_DECL(int) gimR3HvHypercallPostDebugData(PVM pVM, RTGCPHYS GCPhysOut, int *prcHv)
1006{
1007 AssertPtr(pVM);
1008 AssertPtr(prcHv);
1009 PGIMHV pHv = &pVM->gim.s.u.Hv;
1010 int rcHv = GIM_HV_STATUS_OPERATION_DENIED;
1011
1012 /*
1013 * Grab the parameters.
1014 */
1015 PGIMHVDEBUGPOSTIN pIn = (PGIMHVDEBUGPOSTIN)pHv->pbHypercallIn;
1016 AssertPtrReturn(pIn, VERR_GIM_IPE_1);
1017 uint32_t cbWrite = pIn->cbWrite;
1018 uint32_t fFlags = pIn->fFlags;
1019 uint8_t *pbData = ((uint8_t *)pIn) + sizeof(PGIMHVDEBUGPOSTIN);
1020
1021 PGIMHVDEBUGPOSTOUT pOut = (PGIMHVDEBUGPOSTOUT)pHv->pbHypercallOut;
1022 AssertPtrReturn(pOut, VERR_GIM_IPE_2);
1023 uint32_t *pcbPendingWrite = &pOut->cbPending;
1024
1025 /*
1026 * Perform the hypercall.
1027 */
1028#if 0
1029 /* Currently disabled as Windows 10 guest passes us undocumented flags. */
1030 if (fFlags & ~GIM_HV_DEBUG_POST_OPTIONS_MASK))
1031 rcHv = GIM_HV_STATUS_INVALID_PARAMETER;
1032#endif
1033 if (cbWrite > GIM_HV_DEBUG_MAX_DATA_SIZE)
1034 rcHv = GIM_HV_STATUS_INVALID_PARAMETER;
1035 else if (!cbWrite)
1036 rcHv = GIM_HV_STATUS_SUCCESS;
1037 else if (cbWrite > 0)
1038 {
1039 bool fIgnorePacket = false;
1040 if (pHv->fIsVendorMsHv)
1041 {
1042 /*
1043 * Windows guests sends us ethernet frames over the Hyper-V debug connection.
1044 * It sends DHCP/ARP queries with zero'd out MAC addresses and requires fudging up the
1045 * packets somewhere.
1046 *
1047 * The Microsoft WinDbg debugger talks UDP and thus only expects the actual debug
1048 * protocol payload.
1049 *
1050 * At present, we only handle guests configured with the "nodhcp" option. This makes
1051 * the guest send ARP queries with a self-chosen IP and after a couple of attempts of
1052 * receiving no replies, the guest picks its own IP address. After this, the guest
1053 * starts sending the UDP packets we require. We thus ignore the initial ARP packets
1054 * (and to be safe all non-UDP packets) until the guest eventually starts talking
1055 * UDP. Then we can finally feed the UDP payload over the debug connection.
1056 */
1057 if (cbWrite > sizeof(RTNETETHERHDR))
1058 {
1059 PCRTNETETHERHDR pEtherHdr = (PCRTNETETHERHDR)pbData;
1060 if (pEtherHdr->EtherType == RT_H2N_U16_C(RTNET_ETHERTYPE_IPV4))
1061 {
1062 if (cbWrite > sizeof(RTNETETHERHDR) + RTNETIPV4_MIN_LEN + RTNETUDP_MIN_LEN)
1063 {
1064 size_t const cbMaxIpHdr = cbWrite - sizeof(RTNETETHERHDR) - sizeof(RTNETUDP) - 1;
1065 size_t const cbMaxIpPkt = cbWrite - sizeof(RTNETETHERHDR);
1066 PCRTNETIPV4 pIp4Hdr = (PCRTNETIPV4)(pbData + sizeof(RTNETETHERHDR));
1067 bool const fValidIp4 = RTNetIPv4IsHdrValid(pIp4Hdr, cbMaxIpHdr, cbMaxIpPkt, false /*fChecksum*/);
1068 if ( fValidIp4
1069 && pIp4Hdr->ip_p == RTNETIPV4_PROT_UDP)
1070 {
1071 uint32_t const cbIpHdr = pIp4Hdr->ip_hl * 4;
1072 uint32_t const cbMaxUdpPkt = cbWrite - sizeof(RTNETETHERHDR) - cbIpHdr;
1073 PCRTNETUDP pUdpHdr = (PCRTNETUDP)((uint8_t *)pIp4Hdr + cbIpHdr);
1074 if ( pUdpHdr->uh_ulen > RT_H2N_U16(sizeof(RTNETUDP))
1075 && pUdpHdr->uh_ulen <= RT_H2N_U16((uint16_t)cbMaxUdpPkt))
1076 {
1077 /*
1078 * Extract the UDP payload and pass it to the debugger and record the guest IP address.
1079 * Hyper-V sends UDP debugger packets with source and destination port as 0. If we don't
1080 * filter out the ports here, we would receive BOOTP, NETBIOS and other UDP sub-protocol
1081 * packets which the debugger yells as "Bad packet received from...".
1082 */
1083 if ( !pUdpHdr->uh_dport
1084 && !pUdpHdr->uh_sport)
1085 {
1086 uint32_t const cbFrameHdr = sizeof(RTNETETHERHDR) + cbIpHdr + sizeof(RTNETUDP);
1087 pbData += cbFrameHdr;
1088 cbWrite -= cbFrameHdr;
1089 pHv->DbgGuestAddr = pIp4Hdr->ip_src;
1090 }
1091 else
1092 {
1093 LogFlow(("GIM: HyperV: Ignoring UDP packet not src and dst port 0\n"));
1094 fIgnorePacket = true;
1095 }
1096 }
1097 else
1098 {
1099 LogFlow(("GIM: HyperV: Ignoring malformed UDP packet. cbMaxUdpPkt=%u UdpPkt.len=%u\n",
1100 cbMaxUdpPkt, RT_N2H_U16(pUdpHdr->uh_ulen)));
1101 fIgnorePacket = true;
1102 }
1103 }
1104 else
1105 {
1106 LogFlow(("GIM: HyperV: Ignoring non-IP / non-UDP packet. fValidIp4=%RTBool Proto=%u\n", fValidIp4,
1107 pIp4Hdr->ip_p));
1108 fIgnorePacket = true;
1109 }
1110 }
1111 else
1112 {
1113 LogFlow(("GIM: HyperV: Ignoring IPv4 packet; too short to be valid UDP. cbWrite=%u\n", cbWrite));
1114 fIgnorePacket = true;
1115 }
1116 }
1117 else
1118 {
1119 LogFlow(("GIM: HyperV: Ignoring non-IP packet. Ethertype=%#x\n", RT_N2H_U16(pEtherHdr->EtherType)));
1120 fIgnorePacket = true;
1121 }
1122 }
1123 }
1124
1125 if (!fIgnorePacket)
1126 {
1127 uint32_t cbReallyWritten = 0;
1128 int rc2 = gimR3HvDebugWrite(pVM, pbData, cbWrite, &cbReallyWritten);
1129 if ( RT_SUCCESS(rc2)
1130 && cbReallyWritten == cbWrite)
1131 {
1132 *pcbPendingWrite = 0;
1133 rcHv = GIM_HV_STATUS_SUCCESS;
1134 }
1135 else
1136 {
1137 /*
1138 * No need to update "*pcbPendingWrite" here as the guest isn't supposed to/doesn't
1139 * look at any of the output parameters when we fail the hypercall operation.
1140 */
1141 rcHv = GIM_HV_STATUS_INSUFFICIENT_BUFFERS;
1142 }
1143 }
1144 else
1145 {
1146 /* Pretend success. */
1147 *pcbPendingWrite = 0;
1148 rcHv = GIM_HV_STATUS_SUCCESS;
1149 }
1150 }
1151
1152 /*
1153 * Update the guest memory with result.
1154 */
1155 int rc = PGMPhysSimpleWriteGCPhys(pVM, GCPhysOut, pHv->pbHypercallOut, sizeof(GIMHVDEBUGPOSTOUT));
1156 if (RT_FAILURE(rc))
1157 {
1158 LogRelMax(10, ("GIM: HyperV: HvPostDebugData failed to update guest memory. rc=%Rrc\n", rc));
1159 rc = VERR_GIM_HYPERCALL_MEMORY_WRITE_FAILED;
1160 }
1161
1162 *prcHv = rcHv;
1163 return rc;
1164}
1165
1166
1167/**
1168 * Performs the HvRetrieveDebugData hypercall.
1169 *
1170 * @returns VBox status code.
1171 * @param pVM The cross context VM structure.
1172 * @param GCPhysOut Where to write the hypercall output parameters after
1173 * performing the hypercall.
1174 * @param prcHv Where to store the result of the hypercall operation.
1175 *
1176 * @thread EMT.
1177 */
1178VMMR3_INT_DECL(int) gimR3HvHypercallRetrieveDebugData(PVM pVM, RTGCPHYS GCPhysOut, int *prcHv)
1179{
1180 AssertPtr(pVM);
1181 AssertPtr(prcHv);
1182 PGIMHV pHv = &pVM->gim.s.u.Hv;
1183 int rcHv = GIM_HV_STATUS_OPERATION_DENIED;
1184
1185 /*
1186 * Grab the parameters.
1187 */
1188 PGIMHVDEBUGRETRIEVEIN pIn = (PGIMHVDEBUGRETRIEVEIN)pHv->pbHypercallIn;
1189 AssertPtrReturn(pIn, VERR_GIM_IPE_1);
1190 uint32_t cbRead = pIn->cbRead;
1191 uint32_t fFlags = pIn->fFlags;
1192 uint64_t uTimeout = pIn->u64Timeout;
1193 uint32_t cMsTimeout = (fFlags & GIM_HV_DEBUG_RETREIVE_LOOP) ? (uTimeout * 100) / RT_NS_1MS_64 : 0;
1194
1195 PGIMHVDEBUGRETRIEVEOUT pOut = (PGIMHVDEBUGRETRIEVEOUT)pHv->pbHypercallOut;
1196 AssertPtrReturn(pOut, VERR_GIM_IPE_2);
1197 uint32_t *pcbReallyRead = &pOut->cbRead;
1198 uint32_t *pcbRemainingRead = &pOut->cbRemaining;
1199 void *pvData = ((uint8_t *)pOut) + sizeof(GIMHVDEBUGRETRIEVEOUT);
1200
1201 /*
1202 * Perform the hypercall.
1203 */
1204 *pcbReallyRead = 0;
1205 *pcbRemainingRead = cbRead;
1206#if 0
1207 /* Currently disabled as Windows 10 guest passes us undocumented flags. */
1208 if (fFlags & ~GIM_HV_DEBUG_RETREIVE_OPTIONS_MASK)
1209 rcHv = GIM_HV_STATUS_INVALID_PARAMETER;
1210#endif
1211 if (cbRead > GIM_HV_DEBUG_MAX_DATA_SIZE)
1212 rcHv = GIM_HV_STATUS_INVALID_PARAMETER;
1213 else if (fFlags & GIM_HV_DEBUG_RETREIVE_TEST_ACTIVITY)
1214 rcHv = GIM_HV_STATUS_SUCCESS; /** @todo implement this. */
1215 else if (!cbRead)
1216 rcHv = GIM_HV_STATUS_SUCCESS;
1217 else if (cbRead > 0)
1218 {
1219 int rc2 = gimR3HvDebugRead(pVM, pvData, cbRead, pcbReallyRead, cMsTimeout);
1220 Assert(*pcbReallyRead <= cbRead);
1221 if ( RT_SUCCESS(rc2)
1222 && *pcbReallyRead > 0)
1223 {
1224 uint8_t abFrame[sizeof(RTNETETHERHDR) + RTNETIPV4_MIN_LEN + sizeof(RTNETUDP)];
1225 if ( pHv->fIsVendorMsHv
1226 && *pcbReallyRead + sizeof(abFrame) <= GIM_HV_PAGE_SIZE)
1227 {
1228 /*
1229 * Windows guests pumps ethernet frames over the Hyper-V debug connection as
1230 * explained in gimR3HvHypercallPostDebugData(). Here, we reconstruct the packet
1231 * with the guest's self-chosen IP ARP address we saved in pHv->DbgGuestAddr.
1232 *
1233 * Note! We really need to pass the minimum IPv4 header length. The Windows 10 guest
1234 * is -not- happy if we include the IPv4 options field, i.e. using sizeof(RTNETIPV4)
1235 * instead of RTNETIPV4_MIN_LEN.
1236 */
1237 RT_ZERO(abFrame);
1238 PRTNETETHERHDR pEthHdr = (PRTNETETHERHDR)&abFrame[0];
1239 PRTNETIPV4 pIpHdr = (PRTNETIPV4) (pEthHdr + 1);
1240 PRTNETUDP pUdpHdr = (PRTNETUDP) ((uint8_t *)pIpHdr + RTNETIPV4_MIN_LEN);
1241
1242 /* Ethernet */
1243 pEthHdr->EtherType = RT_H2N_U16_C(RTNET_ETHERTYPE_IPV4);
1244 /* IPv4 */
1245 pIpHdr->ip_v = 4;
1246 pIpHdr->ip_hl = RTNETIPV4_MIN_LEN / sizeof(uint32_t);
1247 pIpHdr->ip_tos = 0;
1248 pIpHdr->ip_len = RT_H2N_U16((uint16_t)*pcbReallyRead + sizeof(RTNETUDP) + RTNETIPV4_MIN_LEN);
1249 pIpHdr->ip_id = 0;
1250 pIpHdr->ip_off = 0;
1251 pIpHdr->ip_ttl = 255;
1252 pIpHdr->ip_p = RTNETIPV4_PROT_UDP;
1253 pIpHdr->ip_sum = 0;
1254 pIpHdr->ip_src.u = 0;
1255 pIpHdr->ip_dst.u = pHv->DbgGuestAddr.u;
1256 pIpHdr->ip_sum = RTNetIPv4HdrChecksum(pIpHdr);
1257 /* UDP */
1258 pUdpHdr->uh_ulen = RT_H2N_U16_C((uint16_t)*pcbReallyRead + sizeof(*pUdpHdr));
1259
1260 /* Make room by moving the payload and prepending the headers. */
1261 uint8_t *pbData = (uint8_t *)pvData;
1262 memmove(pbData + sizeof(abFrame), pbData, *pcbReallyRead);
1263 memcpy(pbData, &abFrame[0], sizeof(abFrame));
1264
1265 /* Update the adjusted sizes. */
1266 *pcbReallyRead += sizeof(abFrame);
1267 *pcbRemainingRead = cbRead - *pcbReallyRead;
1268 }
1269 rcHv = GIM_HV_STATUS_SUCCESS;
1270 }
1271 else
1272 rcHv = GIM_HV_STATUS_NO_DATA;
1273 }
1274
1275 /*
1276 * Update the guest memory with result.
1277 */
1278 int rc = PGMPhysSimpleWriteGCPhys(pVM, GCPhysOut, pHv->pbHypercallOut, sizeof(GIMHVDEBUGRETRIEVEOUT) + *pcbReallyRead);
1279 if (RT_FAILURE(rc))
1280 {
1281 LogRelMax(10, ("GIM: HyperV: HvRetrieveDebugData failed to update guest memory. rc=%Rrc\n", rc));
1282 rc = VERR_GIM_HYPERCALL_MEMORY_WRITE_FAILED;
1283 }
1284
1285 *prcHv = rcHv;
1286 return rc;
1287}
1288
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