VirtualBox

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

Last change on this file since 58248 was 58248, checked in by vboxsync, 9 years ago

VMM/GIM: Implemented Hyper-V synthetic debug MSRs support. Added CFGM validation for GIM and Hyper-V.

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