VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR0/NEMR0Native-win.cpp@ 92489

Last change on this file since 92489 was 92465, checked in by vboxsync, 3 years ago

VMM/NEM-linux: Some more code. Got far enough to log the BIOS version to the release log. bugref:9044

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 137.6 KB
Line 
1/* $Id: NEMR0Native-win.cpp 92465 2021-11-17 03:01:09Z vboxsync $ */
2/** @file
3 * NEM - Native execution manager, native ring-0 Windows backend.
4 */
5
6/*
7 * Copyright (C) 2018-2020 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_NEM
23#define VMCPU_INCL_CPUM_GST_CTX
24#include <iprt/nt/nt.h>
25#include <iprt/nt/hyperv.h>
26#include <iprt/nt/vid.h>
27#include <winerror.h>
28
29#include <VBox/vmm/nem.h>
30#include <VBox/vmm/iem.h>
31#include <VBox/vmm/em.h>
32#include <VBox/vmm/apic.h>
33#include <VBox/vmm/pdm.h>
34#include <VBox/vmm/dbgftrace.h>
35#include "NEMInternal.h"
36#include <VBox/vmm/gvm.h>
37#include <VBox/vmm/vmcc.h>
38#include <VBox/vmm/gvmm.h>
39#include <VBox/param.h>
40
41#include <iprt/ctype.h>
42#include <iprt/critsect.h>
43#include <iprt/dbg.h>
44#include <iprt/mem.h>
45#include <iprt/memobj.h>
46#include <iprt/string.h>
47#include <iprt/time.h>
48#define PIMAGE_NT_HEADERS32 PIMAGE_NT_HEADERS32_PECOFF
49#include <iprt/formats/pecoff.h>
50
51
52/* Assert compile context sanity. */
53#ifndef RT_OS_WINDOWS
54# error "Windows only file!"
55#endif
56#ifndef RT_ARCH_AMD64
57# error "AMD64 only file!"
58#endif
59
60
61/*********************************************************************************************************************************
62* Internal Functions *
63*********************************************************************************************************************************/
64typedef uint32_t DWORD; /* for winerror.h constants */
65
66
67/*********************************************************************************************************************************
68* Global Variables *
69*********************************************************************************************************************************/
70#ifdef NEM_WIN_USE_HYPERCALLS_FOR_PAGES
71static uint64_t (*g_pfnHvlInvokeHypercall)(uint64_t uCallInfo, uint64_t HCPhysInput, uint64_t HCPhysOutput);
72
73/**
74 * WinHvr.sys!WinHvDepositMemory
75 *
76 * This API will try allocates cPages on IdealNode and deposit it to the
77 * hypervisor for use with the given partition. The memory will be freed when
78 * VID.SYS calls WinHvWithdrawAllMemory when the partition is cleanedup.
79 *
80 * Apparently node numbers above 64 has a different meaning.
81 */
82static NTSTATUS (*g_pfnWinHvDepositMemory)(uintptr_t idPartition, size_t cPages, uintptr_t IdealNode, size_t *pcActuallyAdded);
83#endif
84
85RT_C_DECLS_BEGIN
86/**
87 * The WinHvGetPartitionProperty function we intercept in VID.SYS to get the
88 * Hyper-V partition ID.
89 *
90 * This is used from assembly.
91 */
92NTSTATUS WinHvGetPartitionProperty(uintptr_t idPartition, HV_PARTITION_PROPERTY_CODE enmProperty, PHV_PARTITION_PROPERTY puValue);
93decltype(WinHvGetPartitionProperty) *g_pfnWinHvGetPartitionProperty;
94RT_C_DECLS_END
95
96/** @name VID.SYS image details.
97 * @{ */
98#ifdef NEM_WIN_USE_HYPERCALLS_FOR_PAGES
99static uint8_t *g_pbVidSys = NULL;
100static uintptr_t g_cbVidSys = 0;
101static PIMAGE_NT_HEADERS g_pVidSysHdrs = NULL;
102/** Pointer to the import thunk entry in VID.SYS for WinHvGetPartitionProperty if we found it. */
103static decltype(WinHvGetPartitionProperty) **g_ppfnVidSysWinHvGetPartitionProperty = NULL;
104
105/** Critical section protecting the WinHvGetPartitionProperty hacking. */
106static RTCRITSECT g_VidSysCritSect;
107#endif /* NEM_WIN_USE_HYPERCALLS_FOR_PAGES */
108RT_C_DECLS_BEGIN
109/** The partition ID passed to WinHvGetPartitionProperty by VID.SYS. */
110HV_PARTITION_ID g_idVidSysFoundPartition = HV_PARTITION_ID_INVALID;
111/** The thread which is currently looking for a partition ID. */
112RTNATIVETHREAD g_hVidSysMatchThread = NIL_RTNATIVETHREAD;
113/** The property code we expect in WinHvGetPartitionProperty. */
114VID_PARTITION_PROPERTY_CODE g_enmVidSysMatchProperty = INT64_MAX;
115/* NEMR0NativeA-win.asm: */
116extern uint8_t g_abNemR0WinHvrWinHvGetPartitionProperty_OriginalProlog[64];
117RT_C_DECLS_END
118/** @} */
119
120
121
122/*********************************************************************************************************************************
123* Internal Functions *
124*********************************************************************************************************************************/
125NEM_TMPL_STATIC int nemR0WinMapPages(PGVM pGVM, PGVMCPU pGVCpu, RTGCPHYS GCPhysSrc, RTGCPHYS GCPhysDst,
126 uint32_t cPages, uint32_t fFlags);
127NEM_TMPL_STATIC int nemR0WinUnmapPages(PGVM pGVM, PGVMCPU pGVCpu, RTGCPHYS GCPhys, uint32_t cPages);
128#if defined(NEM_WIN_WITH_RING0_RUNLOOP) || defined(NEM_WIN_USE_HYPERCALLS_FOR_REGISTERS)
129NEM_TMPL_STATIC int nemR0WinExportState(PGVM pGVM, PGVMCPU pGVCpu, PCPUMCTX pCtx);
130NEM_TMPL_STATIC int nemR0WinImportState(PGVM pGVM, PGVMCPU pGVCpu, PCPUMCTX pCtx, uint64_t fWhat, bool fCanUpdateCr3);
131NEM_TMPL_STATIC int nemR0WinQueryCpuTick(PGVM pGVM, PGVMCPU pGVCpu, uint64_t *pcTicks, uint32_t *pcAux);
132NEM_TMPL_STATIC int nemR0WinResumeCpuTickOnAll(PGVM pGVM, PGVMCPU pGVCpu, uint64_t uPausedTscValue);
133#endif
134DECLINLINE(NTSTATUS) nemR0NtPerformIoControl(PGVM pGVM, PGVMCPU pGVCpu, uint32_t uFunction, void *pvInput, uint32_t cbInput,
135 void *pvOutput, uint32_t cbOutput);
136
137/* NEMR0NativeA-win.asm: */
138DECLASM(NTSTATUS) nemR0VidSysWinHvGetPartitionProperty(uintptr_t idPartition, HV_PARTITION_PROPERTY_CODE enmProperty,
139 PHV_PARTITION_PROPERTY puValue);
140DECLASM(NTSTATUS) nemR0WinHvrWinHvGetPartitionProperty(uintptr_t idPartition, HV_PARTITION_PROPERTY_CODE enmProperty,
141 PHV_PARTITION_PROPERTY puValue);
142
143
144/*
145 * Instantate the code we share with ring-0.
146 */
147#ifdef NEM_WIN_WITH_RING0_RUNLOOP
148# define NEM_WIN_TEMPLATE_MODE_OWN_RUN_API
149#else
150# undef NEM_WIN_TEMPLATE_MODE_OWN_RUN_API
151#endif
152#include "../VMMAll/NEMAllNativeTemplate-win.cpp.h"
153
154
155/**
156 * Module initialization for NEM.
157 */
158VMMR0_INT_DECL(int) NEMR0Init(void)
159{
160#ifdef NEM_WIN_USE_HYPERCALLS_FOR_PAGES
161 return RTCritSectInit(&g_VidSysCritSect);
162#else
163 return VINF_SUCCESS;
164#endif
165}
166
167
168/**
169 * Module termination for NEM.
170 */
171VMMR0_INT_DECL(void) NEMR0Term(void)
172{
173#ifdef NEM_WIN_USE_HYPERCALLS_FOR_PAGES
174 RTCritSectDelete(&g_VidSysCritSect);
175#endif
176}
177
178#ifdef NEM_WIN_USE_HYPERCALLS_FOR_PAGES
179
180/**
181 * Worker for NEMR0InitVM that allocates a hypercall page.
182 *
183 * @returns VBox status code.
184 * @param pHypercallData The hypercall data page to initialize.
185 */
186static int nemR0InitHypercallData(PNEMR0HYPERCALLDATA pHypercallData)
187{
188 int rc = RTR0MemObjAllocPage(&pHypercallData->hMemObj, PAGE_SIZE, false /*fExecutable*/);
189 if (RT_SUCCESS(rc))
190 {
191 pHypercallData->HCPhysPage = RTR0MemObjGetPagePhysAddr(pHypercallData->hMemObj, 0 /*iPage*/);
192 AssertStmt(pHypercallData->HCPhysPage != NIL_RTHCPHYS, rc = VERR_INTERNAL_ERROR_3);
193 pHypercallData->pbPage = (uint8_t *)RTR0MemObjAddress(pHypercallData->hMemObj);
194 AssertStmt(pHypercallData->pbPage, rc = VERR_INTERNAL_ERROR_3);
195 if (RT_SUCCESS(rc))
196 return VINF_SUCCESS;
197
198 /* bail out */
199 RTR0MemObjFree(pHypercallData->hMemObj, true /*fFreeMappings*/);
200 }
201 pHypercallData->hMemObj = NIL_RTR0MEMOBJ;
202 pHypercallData->HCPhysPage = NIL_RTHCPHYS;
203 pHypercallData->pbPage = NULL;
204 return rc;
205}
206
207
208/**
209 * Worker for NEMR0CleanupVM and NEMR0InitVM that cleans up a hypercall page.
210 *
211 * @param pHypercallData The hypercall data page to uninitialize.
212 */
213static void nemR0DeleteHypercallData(PNEMR0HYPERCALLDATA pHypercallData)
214{
215 /* Check pbPage here since it's NULL, whereas the hMemObj can be either
216 NIL_RTR0MEMOBJ or 0 (they aren't necessarily the same). */
217 if (pHypercallData->pbPage != NULL)
218 {
219 RTR0MemObjFree(pHypercallData->hMemObj, true /*fFreeMappings*/);
220 pHypercallData->pbPage = NULL;
221 }
222 pHypercallData->hMemObj = NIL_RTR0MEMOBJ;
223 pHypercallData->HCPhysPage = NIL_RTHCPHYS;
224}
225
226
227static int nemR0StrICmp(const char *psz1, const char *psz2)
228{
229 for (;;)
230 {
231 char ch1 = *psz1++;
232 char ch2 = *psz2++;
233 if ( ch1 != ch2
234 && RT_C_TO_LOWER(ch1) != RT_C_TO_LOWER(ch2))
235 return ch1 - ch2;
236 if (!ch1)
237 return 0;
238 }
239}
240
241
242/**
243 * Worker for nemR0PrepareForVidSysIntercept().
244 */
245static void nemR0PrepareForVidSysInterceptInner(void)
246{
247 uint32_t const cbImage = g_cbVidSys;
248 uint8_t * const pbImage = g_pbVidSys;
249 PIMAGE_NT_HEADERS const pNtHdrs = g_pVidSysHdrs;
250 uintptr_t const offEndNtHdrs = (uintptr_t)(pNtHdrs + 1) - (uintptr_t)pbImage;
251
252# define CHECK_LOG_RET(a_Expr, a_LogRel) do { \
253 if (RT_LIKELY(a_Expr)) { /* likely */ } \
254 else \
255 { \
256 LogRel(a_LogRel); \
257 return; \
258 } \
259 } while (0)
260
261 //__try
262 {
263 /*
264 * Get and validate the import directory entry.
265 */
266 CHECK_LOG_RET( pNtHdrs->OptionalHeader.NumberOfRvaAndSizes > IMAGE_DIRECTORY_ENTRY_IMPORT
267 || pNtHdrs->OptionalHeader.NumberOfRvaAndSizes <= IMAGE_NUMBEROF_DIRECTORY_ENTRIES * 4,
268 ("NEMR0: vid.sys: NumberOfRvaAndSizes is out of range: %#x\n", pNtHdrs->OptionalHeader.NumberOfRvaAndSizes));
269
270 IMAGE_DATA_DIRECTORY const ImportDir = pNtHdrs->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
271 CHECK_LOG_RET( ImportDir.Size >= sizeof(IMAGE_IMPORT_DESCRIPTOR)
272 && ImportDir.VirtualAddress >= offEndNtHdrs /* ASSUMES NT headers before imports */
273 && (uint64_t)ImportDir.VirtualAddress + ImportDir.Size <= cbImage,
274 ("NEMR0: vid.sys: Bad import directory entry: %#x LB %#x (cbImage=%#x, offEndNtHdrs=%#zx)\n",
275 ImportDir.VirtualAddress, ImportDir.Size, cbImage, offEndNtHdrs));
276
277 /*
278 * Walk the import descriptor table looking for NTDLL.DLL.
279 */
280 for (PIMAGE_IMPORT_DESCRIPTOR pImps = (PIMAGE_IMPORT_DESCRIPTOR)&pbImage[ImportDir.VirtualAddress];
281 pImps->Name != 0 && pImps->FirstThunk != 0;
282 pImps++)
283 {
284 CHECK_LOG_RET(pImps->Name < cbImage, ("NEMR0: vid.sys: Bad import directory entry name: %#x", pImps->Name));
285 const char *pszModName = (const char *)&pbImage[pImps->Name];
286 if (nemR0StrICmp(pszModName, "winhvr.sys"))
287 continue;
288 CHECK_LOG_RET(pImps->FirstThunk < cbImage && pImps->FirstThunk >= offEndNtHdrs,
289 ("NEMR0: vid.sys: Bad FirstThunk: %#x", pImps->FirstThunk));
290 CHECK_LOG_RET( pImps->u.OriginalFirstThunk == 0
291 || (pImps->u.OriginalFirstThunk >= offEndNtHdrs && pImps->u.OriginalFirstThunk < cbImage),
292 ("NEMR0: vid.sys: Bad OriginalFirstThunk: %#x", pImps->u.OriginalFirstThunk));
293
294 /*
295 * Walk the thunks table(s) looking for WinHvGetPartitionProperty.
296 */
297 uintptr_t *puFirstThunk = (uintptr_t *)&pbImage[pImps->FirstThunk]; /* update this. */
298 if ( pImps->u.OriginalFirstThunk != 0
299 && pImps->u.OriginalFirstThunk != pImps->FirstThunk)
300 {
301 uintptr_t const *puOrgThunk = (uintptr_t const *)&pbImage[pImps->u.OriginalFirstThunk]; /* read from this. */
302 uintptr_t cLeft = (cbImage - (RT_MAX(pImps->FirstThunk, pImps->u.OriginalFirstThunk)))
303 / sizeof(*puFirstThunk);
304 while (cLeft-- > 0 && *puOrgThunk != 0)
305 {
306 if (!(*puOrgThunk & IMAGE_ORDINAL_FLAG64))
307 {
308 CHECK_LOG_RET(*puOrgThunk >= offEndNtHdrs && *puOrgThunk < cbImage,
309 ("NEMR0: vid.sys: Bad thunk entry: %#x", *puOrgThunk));
310
311 const char *pszSymbol = (const char *)&pbImage[*puOrgThunk + 2];
312 if (strcmp(pszSymbol, "WinHvGetPartitionProperty") == 0)
313 g_ppfnVidSysWinHvGetPartitionProperty = (decltype(WinHvGetPartitionProperty) **)puFirstThunk;
314 }
315
316 puOrgThunk++;
317 puFirstThunk++;
318 }
319 }
320 else
321 {
322 /* No original thunk table, so scan the resolved symbols for a match
323 with the WinHvGetPartitionProperty address. */
324 uintptr_t const uNeedle = (uintptr_t)g_pfnWinHvGetPartitionProperty;
325 uintptr_t cLeft = (cbImage - pImps->FirstThunk) / sizeof(*puFirstThunk);
326 while (cLeft-- > 0 && *puFirstThunk != 0)
327 {
328 if (*puFirstThunk == uNeedle)
329 g_ppfnVidSysWinHvGetPartitionProperty = (decltype(WinHvGetPartitionProperty) **)puFirstThunk;
330 puFirstThunk++;
331 }
332 }
333 }
334
335 /* Report the findings: */
336 if (g_ppfnVidSysWinHvGetPartitionProperty)
337 LogRel(("NEMR0: vid.sys: Found WinHvGetPartitionProperty import thunk at %p (value %p vs %p)\n",
338 g_ppfnVidSysWinHvGetPartitionProperty,*g_ppfnVidSysWinHvGetPartitionProperty, g_pfnWinHvGetPartitionProperty));
339 else
340 LogRel(("NEMR0: vid.sys: Did not find WinHvGetPartitionProperty!\n"));
341 }
342 //__except(EXCEPTION_EXECUTE_HANDLER)
343 //{
344 // return;
345 //}
346# undef CHECK_LOG_RET
347}
348
349
350/**
351 * Worker for NEMR0InitVM that prepares for intercepting stuff in VID.SYS.
352 */
353static void nemR0PrepareForVidSysIntercept(RTDBGKRNLINFO hKrnlInfo)
354{
355 /*
356 * Resolve the symbols we need first.
357 */
358 int rc = RTR0DbgKrnlInfoQuerySymbol(hKrnlInfo, "vid.sys", "__ImageBase", (void **)&g_pbVidSys);
359 if (RT_SUCCESS(rc))
360 {
361 rc = RTR0DbgKrnlInfoQuerySymbol(hKrnlInfo, "vid.sys", "__ImageSize", (void **)&g_cbVidSys);
362 if (RT_SUCCESS(rc))
363 {
364 rc = RTR0DbgKrnlInfoQuerySymbol(hKrnlInfo, "vid.sys", "__ImageNtHdrs", (void **)&g_pVidSysHdrs);
365 if (RT_SUCCESS(rc))
366 {
367 rc = RTR0DbgKrnlInfoQuerySymbol(hKrnlInfo, "winhvr.sys", "WinHvGetPartitionProperty",
368 (void **)&g_pfnWinHvGetPartitionProperty);
369 if (RT_SUCCESS(rc))
370 {
371 /*
372 * Now locate the import thunk entry for WinHvGetPartitionProperty in vid.sys.
373 */
374 nemR0PrepareForVidSysInterceptInner();
375 }
376 else
377 LogRel(("NEMR0: Failed to find winhvr.sys!WinHvGetPartitionProperty (%Rrc)\n", rc));
378 }
379 else
380 LogRel(("NEMR0: Failed to find vid.sys!__ImageNtHdrs (%Rrc)\n", rc));
381 }
382 else
383 LogRel(("NEMR0: Failed to find vid.sys!__ImageSize (%Rrc)\n", rc));
384 }
385 else
386 LogRel(("NEMR0: Failed to find vid.sys!__ImageBase (%Rrc)\n", rc));
387}
388
389#endif /* NEM_WIN_USE_HYPERCALLS_FOR_PAGES */
390
391
392/**
393 * Called by NEMR3Init to make sure we've got what we need.
394 *
395 * @returns VBox status code.
396 * @param pGVM The ring-0 VM handle.
397 * @thread EMT(0)
398 */
399VMMR0_INT_DECL(int) NEMR0InitVM(PGVM pGVM)
400{
401 AssertCompile(sizeof(pGVM->nemr0.s) <= sizeof(pGVM->nemr0.padding));
402 AssertCompile(sizeof(pGVM->aCpus[0].nemr0.s) <= sizeof(pGVM->aCpus[0].nemr0.padding));
403
404 int rc = GVMMR0ValidateGVMandEMT(pGVM, 0);
405 AssertRCReturn(rc, rc);
406
407#ifdef NEM_WIN_USE_HYPERCALLS_FOR_PAGES
408 /*
409 * We want to perform hypercalls here. The NT kernel started to expose a very low
410 * level interface to do this thru somewhere between build 14271 and 16299. Since
411 * we need build 17134 to get anywhere at all, the exact build is not relevant here.
412 *
413 * We also need to deposit memory to the hypervisor for use with partition (page
414 * mapping structures, stuff).
415 */
416 RTDBGKRNLINFO hKrnlInfo;
417 rc = RTR0DbgKrnlInfoOpen(&hKrnlInfo, 0);
418 if (RT_SUCCESS(rc))
419 {
420 rc = RTR0DbgKrnlInfoQuerySymbol(hKrnlInfo, NULL, "HvlInvokeHypercall", (void **)&g_pfnHvlInvokeHypercall);
421 if (RT_FAILURE(rc))
422 rc = VERR_NEM_MISSING_KERNEL_API_1;
423 if (RT_SUCCESS(rc))
424 {
425 rc = RTR0DbgKrnlInfoQuerySymbol(hKrnlInfo, "winhvr.sys", "WinHvDepositMemory", (void **)&g_pfnWinHvDepositMemory);
426 if (RT_FAILURE(rc))
427 rc = rc == VERR_MODULE_NOT_FOUND ? VERR_NEM_MISSING_KERNEL_API_2 : VERR_NEM_MISSING_KERNEL_API_3;
428 }
429
430 /*
431 * Since late 2021 we may also need to do some nasty trickery with vid.sys to get
432 * the partition ID. So, ge the necessary info while we have a hKrnlInfo instance.
433 */
434 if (RT_SUCCESS(rc))
435 nemR0PrepareForVidSysIntercept(hKrnlInfo);
436
437 RTR0DbgKrnlInfoRelease(hKrnlInfo);
438 if (RT_SUCCESS(rc))
439 {
440 /*
441 * Allocate a page for non-EMT threads to use for hypercalls (update
442 * statistics and such) and a critical section protecting it.
443 */
444 rc = RTCritSectInit(&pGVM->nemr0.s.HypercallDataCritSect);
445 if (RT_SUCCESS(rc))
446 {
447 rc = nemR0InitHypercallData(&pGVM->nemr0.s.HypercallData);
448 if (RT_SUCCESS(rc))
449 {
450 /*
451 * Allocate a page for each VCPU to place hypercall data on.
452 */
453 for (VMCPUID i = 0; i < pGVM->cCpus; i++)
454 {
455 rc = nemR0InitHypercallData(&pGVM->aCpus[i].nemr0.s.HypercallData);
456 if (RT_FAILURE(rc))
457 {
458 while (i-- > 0)
459 nemR0DeleteHypercallData(&pGVM->aCpus[i].nemr0.s.HypercallData);
460 break;
461 }
462 }
463 if (RT_SUCCESS(rc))
464 {
465 /*
466 * So far, so good.
467 */
468 return rc;
469 }
470
471 /*
472 * Bail out.
473 */
474 nemR0DeleteHypercallData(&pGVM->nemr0.s.HypercallData);
475 }
476 RTCritSectDelete(&pGVM->nemr0.s.HypercallDataCritSect);
477 }
478 }
479 }
480#endif /* NEM_WIN_USE_HYPERCALLS_FOR_PAGES */
481
482 return rc;
483}
484
485#ifdef NEM_WIN_USE_HYPERCALLS_FOR_PAGES
486
487/**
488 * Perform an I/O control operation on the partition handle (VID.SYS).
489 *
490 * @returns NT status code.
491 * @param pGVM The ring-0 VM structure.
492 * @param pGVCpu The global (ring-0) CPU structure of the calling EMT.
493 * @param uFunction The function to perform.
494 * @param pvInput The input buffer. This must point within the VM
495 * structure so we can easily convert to a ring-3
496 * pointer if necessary.
497 * @param cbInput The size of the input. @a pvInput must be NULL when
498 * zero.
499 * @param pvOutput The output buffer. This must also point within the
500 * VM structure for ring-3 pointer magic.
501 * @param cbOutput The size of the output. @a pvOutput must be NULL
502 * when zero.
503 * @thread EMT(pGVCpu)
504 */
505DECLINLINE(NTSTATUS) nemR0NtPerformIoControl(PGVM pGVM, PGVMCPU pGVCpu, uint32_t uFunction, void *pvInput, uint32_t cbInput,
506 void *pvOutput, uint32_t cbOutput)
507{
508# ifdef RT_STRICT
509 /*
510 * Input and output parameters are part of the VM CPU structure.
511 */
512 VMCPU_ASSERT_EMT(pGVCpu);
513 if (pvInput)
514 AssertReturn(((uintptr_t)pvInput + cbInput) - (uintptr_t)pGVCpu <= sizeof(*pGVCpu), VERR_INVALID_PARAMETER);
515 if (pvOutput)
516 AssertReturn(((uintptr_t)pvOutput + cbOutput) - (uintptr_t)pGVCpu <= sizeof(*pGVCpu), VERR_INVALID_PARAMETER);
517# endif
518
519 int32_t rcNt = STATUS_UNSUCCESSFUL;
520 int rc = SUPR0IoCtlPerform(pGVM->nemr0.s.pIoCtlCtx, uFunction,
521 pvInput,
522 pvInput ? (uintptr_t)pvInput + pGVCpu->nemr0.s.offRing3ConversionDelta : NIL_RTR3PTR,
523 cbInput,
524 pvOutput,
525 pvOutput ? (uintptr_t)pvOutput + pGVCpu->nemr0.s.offRing3ConversionDelta : NIL_RTR3PTR,
526 cbOutput,
527 &rcNt);
528 if (RT_SUCCESS(rc) || !NT_SUCCESS((NTSTATUS)rcNt))
529 return (NTSTATUS)rcNt;
530 return STATUS_UNSUCCESSFUL;
531}
532
533
534/**
535 * Here is something that we really do not wish to do, but find us force do to
536 * right now as we cannot rewrite the memory management of VBox 6.1 in time for
537 * windows 11.
538 *
539 * @returns VBox status code.
540 * @param pGVM The ring-0 VM structure.
541 * @param pahMemObjs Array of 6 memory objects that the caller will release.
542 * ASSUMES that they are initialized to NIL.
543 */
544static int nemR0InitVMPart2DontWannaDoTheseUglyPartitionIdFallbacks(PGVM pGVM, PRTR0MEMOBJ pahMemObjs)
545{
546 /*
547 * Check preconditions:
548 */
549 if ( !g_ppfnVidSysWinHvGetPartitionProperty
550 || (uintptr_t)g_ppfnVidSysWinHvGetPartitionProperty & (sizeof(uintptr_t) - 1))
551 {
552 LogRel(("NEMR0: g_ppfnVidSysWinHvGetPartitionProperty is NULL or misaligned (%p), partition ID fallback not possible.\n",
553 g_ppfnVidSysWinHvGetPartitionProperty));
554 return VERR_NEM_INIT_FAILED;
555 }
556 if (!g_pfnWinHvGetPartitionProperty)
557 {
558 LogRel(("NEMR0: g_pfnWinHvGetPartitionProperty is NULL, partition ID fallback not possible.\n"));
559 return VERR_NEM_INIT_FAILED;
560 }
561 if (!pGVM->nem.s.IoCtlGetPartitionProperty.uFunction)
562 {
563 LogRel(("NEMR0: IoCtlGetPartitionProperty.uFunction is 0, partition ID fallback not possible.\n"));
564 return VERR_NEM_INIT_FAILED;
565 }
566
567 /*
568 * Create an alias for the thunk table entry because its very likely to be read-only.
569 */
570 int rc = RTR0MemObjLockKernel(&pahMemObjs[0], g_ppfnVidSysWinHvGetPartitionProperty, sizeof(uintptr_t), RTMEM_PROT_READ);
571 if (RT_FAILURE(rc))
572 {
573 LogRel(("NEMR0: RTR0MemObjLockKernel failed on VID.SYS thunk table entry: %Rrc\n", rc));
574 return rc;
575 }
576
577 rc = RTR0MemObjEnterPhys(&pahMemObjs[1], RTR0MemObjGetPagePhysAddr(pahMemObjs[0], 0), PAGE_SIZE, RTMEM_CACHE_POLICY_DONT_CARE);
578 if (RT_FAILURE(rc))
579 {
580 LogRel(("NEMR0: RTR0MemObjEnterPhys failed on VID.SYS thunk table entry: %Rrc\n", rc));
581 return rc;
582 }
583
584 rc = RTR0MemObjMapKernel(&pahMemObjs[2], pahMemObjs[1], (void *)-1, 0, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
585 if (RT_FAILURE(rc))
586 {
587 LogRel(("NEMR0: RTR0MemObjMapKernel failed on VID.SYS thunk table entry: %Rrc\n", rc));
588 return rc;
589 }
590
591 decltype(WinHvGetPartitionProperty) **ppfnThunkAlias
592 = (decltype(WinHvGetPartitionProperty) **)( (uintptr_t)RTR0MemObjAddress(pahMemObjs[2])
593 | ((uintptr_t)g_ppfnVidSysWinHvGetPartitionProperty & PAGE_OFFSET_MASK));
594 LogRel(("NEMR0: ppfnThunkAlias=%p *ppfnThunkAlias=%p; original: %p & %p, phys %RHp\n", ppfnThunkAlias, *ppfnThunkAlias,
595 g_ppfnVidSysWinHvGetPartitionProperty, *g_ppfnVidSysWinHvGetPartitionProperty,
596 RTR0MemObjGetPagePhysAddr(pahMemObjs[0], 0) ));
597
598 /*
599 * Create an alias for the target code in WinHvr.sys as there is a very decent
600 * chance we have to patch it.
601 */
602 rc = RTR0MemObjLockKernel(&pahMemObjs[3], g_pfnWinHvGetPartitionProperty, sizeof(uintptr_t), RTMEM_PROT_READ);
603 if (RT_FAILURE(rc))
604 {
605 LogRel(("NEMR0: RTR0MemObjLockKernel failed on WinHvGetPartitionProperty (%p): %Rrc\n", g_pfnWinHvGetPartitionProperty, rc));
606 return rc;
607 }
608
609 rc = RTR0MemObjEnterPhys(&pahMemObjs[4], RTR0MemObjGetPagePhysAddr(pahMemObjs[3], 0), PAGE_SIZE, RTMEM_CACHE_POLICY_DONT_CARE);
610 if (RT_FAILURE(rc))
611 {
612 LogRel(("NEMR0: RTR0MemObjEnterPhys failed on WinHvGetPartitionProperty: %Rrc\n", rc));
613 return rc;
614 }
615
616 rc = RTR0MemObjMapKernel(&pahMemObjs[5], pahMemObjs[4], (void *)-1, 0, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
617 if (RT_FAILURE(rc))
618 {
619 LogRel(("NEMR0: RTR0MemObjMapKernel failed on WinHvGetPartitionProperty: %Rrc\n", rc));
620 return rc;
621 }
622
623 uint8_t *pbTargetAlias = (uint8_t *)( (uintptr_t)RTR0MemObjAddress(pahMemObjs[5])
624 | ((uintptr_t)g_pfnWinHvGetPartitionProperty & PAGE_OFFSET_MASK));
625 LogRel(("NEMR0: pbTargetAlias=%p %.16Rhxs; original: %p %.16Rhxs, phys %RHp\n", pbTargetAlias, pbTargetAlias,
626 g_pfnWinHvGetPartitionProperty, g_pfnWinHvGetPartitionProperty, RTR0MemObjGetPagePhysAddr(pahMemObjs[3], 0) ));
627
628 /*
629 * Analyse the target functions prologue to figure out how much we should copy
630 * when patching it. We repeat this every time because we don't want to get
631 * tripped up by someone else doing the same stuff as we're doing here.
632 * We need at least 12 bytes for the patch sequence (MOV RAX, QWORD; JMP RAX)
633 */
634 union
635 {
636 uint8_t ab[48]; /**< Must be equal or smallar than g_abNemR0WinHvrWinHvGetPartitionProperty_OriginalProlog */
637 int64_t ai64[6];
638 } Org;
639 memcpy(Org.ab, g_pfnWinHvGetPartitionProperty, sizeof(Org)); /** @todo ASSUMES 48 valid bytes start at function... */
640
641 uint32_t offJmpBack = 0;
642 uint32_t const cbMinJmpPatch = 12;
643 DISSTATE Dis;
644 while (offJmpBack < cbMinJmpPatch && offJmpBack < sizeof(Org) - 16)
645 {
646 uint32_t cbInstr = 1;
647 rc = DISInstr(&Org.ab[offJmpBack], DISCPUMODE_64BIT, &Dis, &cbInstr);
648 if (RT_FAILURE(rc))
649 {
650 LogRel(("NEMR0: DISInstr failed %#x bytes into WinHvGetPartitionProperty: %Rrc (%.48Rhxs)\n",
651 offJmpBack, rc, Org.ab));
652 break;
653 }
654 if (Dis.pCurInstr->fOpType & DISOPTYPE_CONTROLFLOW)
655 {
656 LogRel(("NEMR0: Control flow instruction %#x bytes into WinHvGetPartitionProperty prologue: %.48Rhxs\n",
657 offJmpBack, Org.ab));
658 break;
659 }
660 if (Dis.ModRM.Bits.Mod == 0 && Dis.ModRM.Bits.Rm == 5 /* wrt RIP */)
661 {
662 LogRel(("NEMR0: RIP relative addressing %#x bytes into WinHvGetPartitionProperty prologue: %.48Rhxs\n",
663 offJmpBack, Org.ab));
664 break;
665 }
666 offJmpBack += cbInstr;
667 }
668
669 uintptr_t const cbLeftInPage = PAGE_SIZE - ((uintptr_t)g_pfnWinHvGetPartitionProperty & PAGE_OFFSET_MASK);
670 if (cbLeftInPage < 16 && offJmpBack >= cbMinJmpPatch)
671 {
672 LogRel(("NEMR0: WinHvGetPartitionProperty patching not possible do the page crossing: %p (%#zx)\n",
673 g_pfnWinHvGetPartitionProperty, cbLeftInPage));
674 offJmpBack = 0;
675 }
676 if (offJmpBack >= cbMinJmpPatch)
677 LogRel(("NEMR0: offJmpBack=%#x for WinHvGetPartitionProperty (%p: %.48Rhxs)\n",
678 offJmpBack, g_pfnWinHvGetPartitionProperty, Org.ab));
679 else
680 offJmpBack = 0;
681 rc = VINF_SUCCESS;
682
683 /*
684 * Now enter serialization lock and get on with it...
685 */
686 PVMCPUCC const pVCpu0 = &pGVM->aCpus[0];
687 NTSTATUS rcNt;
688 RTCritSectEnter(&g_VidSysCritSect);
689
690 /*
691 * First attempt, patching the import table entry.
692 */
693 g_idVidSysFoundPartition = HV_PARTITION_ID_INVALID;
694 g_hVidSysMatchThread = RTThreadNativeSelf();
695 g_enmVidSysMatchProperty = pVCpu0->nem.s.uIoCtlBuf.GetProp.enmProperty = HvPartitionPropertyProcessorVendor;
696 pVCpu0->nem.s.uIoCtlBuf.GetProp.uValue = 0;
697
698 void *pvOld = NULL;
699 if (ASMAtomicCmpXchgExPtr(ppfnThunkAlias, (void *)(uintptr_t)nemR0VidSysWinHvGetPartitionProperty,
700 (void *)(uintptr_t)g_pfnWinHvGetPartitionProperty, &pvOld))
701 {
702 LogRel(("NEMR0: after switch to %p: ppfnThunkAlias=%p *ppfnThunkAlias=%p; original: %p & %p\n",
703 nemR0VidSysWinHvGetPartitionProperty, ppfnThunkAlias, *ppfnThunkAlias,
704 g_ppfnVidSysWinHvGetPartitionProperty, *g_ppfnVidSysWinHvGetPartitionProperty));
705
706 rcNt = nemR0NtPerformIoControl(pGVM, pVCpu0, pGVM->nemr0.s.IoCtlGetPartitionProperty.uFunction,
707 &pVCpu0->nem.s.uIoCtlBuf.GetProp.enmProperty,
708 sizeof(pVCpu0->nem.s.uIoCtlBuf.GetProp.enmProperty),
709 &pVCpu0->nem.s.uIoCtlBuf.GetProp.uValue,
710 sizeof(pVCpu0->nem.s.uIoCtlBuf.GetProp.uValue));
711 ASMAtomicWritePtr(ppfnThunkAlias, (void *)(uintptr_t)g_pfnWinHvGetPartitionProperty);
712 HV_PARTITION_ID idHvPartition = g_idVidSysFoundPartition;
713
714 LogRel(("NEMR0: WinHvGetPartitionProperty trick #1 yielded: rcNt=%#x idHvPartition=%#RX64 uValue=%#RX64\n",
715 rcNt, idHvPartition, pVCpu0->nem.s.uIoCtlBuf.GetProp.uValue));
716 pGVM->nemr0.s.idHvPartition = idHvPartition;
717 }
718 else
719 {
720 LogRel(("NEMR0: Unexpected WinHvGetPartitionProperty pointer in VID.SYS: %p, expected %p\n",
721 pvOld, g_pfnWinHvGetPartitionProperty));
722 rc = VERR_NEM_INIT_FAILED;
723 }
724
725 /*
726 * If that didn't succeed, try patching the winhvr.sys code.
727 */
728 if ( pGVM->nemr0.s.idHvPartition == HV_PARTITION_ID_INVALID
729 && offJmpBack >= cbMinJmpPatch)
730 {
731 g_idVidSysFoundPartition = HV_PARTITION_ID_INVALID;
732 g_hVidSysMatchThread = RTThreadNativeSelf();
733 g_enmVidSysMatchProperty = pVCpu0->nem.s.uIoCtlBuf.GetProp.enmProperty = HvPartitionPropertyProcessorVendor;
734 pVCpu0->nem.s.uIoCtlBuf.GetProp.uValue = 0;
735
736 /*
737 * Prepare the hook area.
738 */
739 uint8_t *pbDst = g_abNemR0WinHvrWinHvGetPartitionProperty_OriginalProlog;
740 memcpy(pbDst, (uint8_t const *)(uintptr_t)g_pfnWinHvGetPartitionProperty, offJmpBack);
741 pbDst += offJmpBack;
742
743 *pbDst++ = 0x48; /* mov rax, imm64 */
744 *pbDst++ = 0xb8;
745 *(uint64_t *)pbDst = (uintptr_t)g_pfnWinHvGetPartitionProperty + offJmpBack;
746 pbDst += sizeof(uint64_t);
747 *pbDst++ = 0xff; /* jmp rax */
748 *pbDst++ = 0xe0;
749 *pbDst++ = 0xcc; /* int3 */
750
751 /*
752 * Patch the original. We use cmpxchg16b here to avoid concurrency problems
753 * (this also makes sure we don't trample over someone else doing similar
754 * patching at the same time).
755 */
756 union
757 {
758 uint8_t ab[16];
759 uint64_t au64[2];
760 } Patch;
761 memcpy(Patch.ab, Org.ab, sizeof(Patch));
762 pbDst = Patch.ab;
763 *pbDst++ = 0x48; /* mov rax, imm64 */
764 *pbDst++ = 0xb8;
765 *(uint64_t *)pbDst = (uintptr_t)nemR0WinHvrWinHvGetPartitionProperty;
766 pbDst += sizeof(uint64_t);
767 *pbDst++ = 0xff; /* jmp rax */
768 *pbDst++ = 0xe0;
769
770 int64_t ai64CmpCopy[2] = { Org.ai64[0], Org.ai64[1] }; /* paranoia */
771 if (_InterlockedCompareExchange128((__int64 volatile *)pbTargetAlias, Patch.au64[1], Patch.au64[0], ai64CmpCopy) != 0)
772 {
773 rcNt = nemR0NtPerformIoControl(pGVM, pVCpu0, pGVM->nemr0.s.IoCtlGetPartitionProperty.uFunction,
774 &pVCpu0->nem.s.uIoCtlBuf.GetProp.enmProperty,
775 sizeof(pVCpu0->nem.s.uIoCtlBuf.GetProp.enmProperty),
776 &pVCpu0->nem.s.uIoCtlBuf.GetProp.uValue,
777 sizeof(pVCpu0->nem.s.uIoCtlBuf.GetProp.uValue));
778
779 for (uint32_t cFailures = 0; cFailures < 10; cFailures++)
780 {
781 ai64CmpCopy[0] = Patch.au64[0]; /* paranoia */
782 ai64CmpCopy[1] = Patch.au64[1];
783 if (_InterlockedCompareExchange128((__int64 volatile *)pbTargetAlias, Org.ai64[1], Org.ai64[0], ai64CmpCopy) != 0)
784 {
785 if (cFailures > 0)
786 LogRel(("NEMR0: Succeeded on try #%u.\n", cFailures));
787 break;
788 }
789 LogRel(("NEMR0: Patch restore failure #%u: %.16Rhxs, expected %.16Rhxs\n",
790 cFailures + 1, &ai64CmpCopy[0], &Patch.au64[0]));
791 RTThreadSleep(1000);
792 }
793
794 HV_PARTITION_ID idHvPartition = g_idVidSysFoundPartition;
795 LogRel(("NEMR0: WinHvGetPartitionProperty trick #2 yielded: rcNt=%#x idHvPartition=%#RX64 uValue=%#RX64\n",
796 rcNt, idHvPartition, pVCpu0->nem.s.uIoCtlBuf.GetProp.uValue));
797 pGVM->nemr0.s.idHvPartition = idHvPartition;
798
799 }
800 else
801 {
802 LogRel(("NEMR0: Failed to install WinHvGetPartitionProperty patch: %.16Rhxs, expected %.16Rhxs\n",
803 &ai64CmpCopy[0], &Org.ai64[0]));
804 rc = VERR_NEM_INIT_FAILED;
805 }
806 }
807
808 RTCritSectLeave(&g_VidSysCritSect);
809
810 return rc;
811}
812
813#endif /* NEM_WIN_USE_HYPERCALLS_FOR_PAGES */
814
815/**
816 * 2nd part of the initialization, after we've got a partition handle.
817 *
818 * @returns VBox status code.
819 * @param pGVM The ring-0 VM handle.
820 * @thread EMT(0)
821 */
822VMMR0_INT_DECL(int) NEMR0InitVMPart2(PGVM pGVM)
823{
824 int rc = GVMMR0ValidateGVMandEMT(pGVM, 0);
825 AssertRCReturn(rc, rc);
826 SUPR0Printf("NEMR0InitVMPart2\n"); LogRel(("2: NEMR0InitVMPart2\n"));
827#ifdef NEM_WIN_USE_HYPERCALLS_FOR_PAGES
828# ifdef NEM_WIN_WITH_RING0_RUNLOOP
829 Assert(pGVM->nemr0.s.fMayUseRing0Runloop == false);
830# endif
831
832 /*
833 * Copy and validate the I/O control information from ring-3.
834 */
835 NEMWINIOCTL Copy = pGVM->nem.s.IoCtlGetHvPartitionId;
836 AssertLogRelReturn(Copy.uFunction != 0, VERR_NEM_INIT_FAILED);
837 AssertLogRelReturn(Copy.cbInput == 0, VERR_NEM_INIT_FAILED);
838 AssertLogRelReturn(Copy.cbOutput == sizeof(HV_PARTITION_ID), VERR_NEM_INIT_FAILED);
839 pGVM->nemr0.s.IoCtlGetHvPartitionId = Copy;
840
841 Copy = pGVM->nem.s.IoCtlGetPartitionProperty;
842 AssertLogRelReturn(Copy.uFunction != 0, VERR_NEM_INIT_FAILED);
843 AssertLogRelReturn(Copy.cbInput == sizeof(VID_PARTITION_PROPERTY_CODE), VERR_NEM_INIT_FAILED);
844 AssertLogRelReturn(Copy.cbOutput == sizeof(HV_PARTITION_PROPERTY), VERR_NEM_INIT_FAILED);
845 pGVM->nemr0.s.IoCtlGetPartitionProperty = Copy;
846
847# ifdef NEM_WIN_WITH_RING0_RUNLOOP
848 pGVM->nemr0.s.fMayUseRing0Runloop = pGVM->nem.s.fUseRing0Runloop;
849
850 Copy = pGVM->nem.s.IoCtlStartVirtualProcessor;
851 AssertLogRelStmt(Copy.uFunction != 0, rc = VERR_NEM_INIT_FAILED);
852 AssertLogRelStmt(Copy.cbInput == sizeof(HV_VP_INDEX), rc = VERR_NEM_INIT_FAILED);
853 AssertLogRelStmt(Copy.cbOutput == 0, rc = VERR_NEM_INIT_FAILED);
854 AssertLogRelStmt(Copy.uFunction != pGVM->nemr0.s.IoCtlGetHvPartitionId.uFunction, rc = VERR_NEM_INIT_FAILED);
855 if (RT_SUCCESS(rc))
856 pGVM->nemr0.s.IoCtlStartVirtualProcessor = Copy;
857
858 Copy = pGVM->nem.s.IoCtlStopVirtualProcessor;
859 AssertLogRelStmt(Copy.uFunction != 0, rc = VERR_NEM_INIT_FAILED);
860 AssertLogRelStmt(Copy.cbInput == sizeof(HV_VP_INDEX), rc = VERR_NEM_INIT_FAILED);
861 AssertLogRelStmt(Copy.cbOutput == 0, rc = VERR_NEM_INIT_FAILED);
862 AssertLogRelStmt(Copy.uFunction != pGVM->nemr0.s.IoCtlGetHvPartitionId.uFunction, rc = VERR_NEM_INIT_FAILED);
863 AssertLogRelStmt(Copy.uFunction != pGVM->nemr0.s.IoCtlStartVirtualProcessor.uFunction, rc = VERR_NEM_INIT_FAILED);
864 if (RT_SUCCESS(rc))
865 pGVM->nemr0.s.IoCtlStopVirtualProcessor = Copy;
866
867 Copy = pGVM->nem.s.IoCtlMessageSlotHandleAndGetNext;
868 AssertLogRelStmt(Copy.uFunction != 0, rc = VERR_NEM_INIT_FAILED);
869 AssertLogRelStmt( Copy.cbInput == sizeof(VID_IOCTL_INPUT_MESSAGE_SLOT_HANDLE_AND_GET_NEXT)
870 || Copy.cbInput == RT_OFFSETOF(VID_IOCTL_INPUT_MESSAGE_SLOT_HANDLE_AND_GET_NEXT, cMillies),
871 rc = VERR_NEM_INIT_FAILED);
872 AssertLogRelStmt(Copy.cbOutput == 0, VERR_NEM_INIT_FAILED);
873 AssertLogRelStmt(Copy.uFunction != pGVM->nemr0.s.IoCtlGetHvPartitionId.uFunction, rc = VERR_NEM_INIT_FAILED);
874 AssertLogRelStmt(Copy.uFunction != pGVM->nemr0.s.IoCtlStartVirtualProcessor.uFunction, rc = VERR_NEM_INIT_FAILED);
875 AssertLogRelStmt(Copy.uFunction != pGVM->nemr0.s.IoCtlStopVirtualProcessor.uFunction, rc = VERR_NEM_INIT_FAILED);
876 if (RT_SUCCESS(rc))
877 pGVM->nemr0.s.IoCtlMessageSlotHandleAndGetNext = Copy;
878# endif
879
880 if ( RT_SUCCESS(rc)
881 || !pGVM->nem.s.fUseRing0Runloop)
882 {
883 /*
884 * Setup of an I/O control context for the partition handle for later use.
885 */
886 rc = SUPR0IoCtlSetupForHandle(pGVM->pSession, pGVM->nem.s.hPartitionDevice, 0, &pGVM->nemr0.s.pIoCtlCtx);
887 AssertLogRelRCReturn(rc, rc);
888 for (VMCPUID idCpu = 0; idCpu < pGVM->cCpus; idCpu++)
889 {
890 PGVMCPU pGVCpu = &pGVM->aCpus[idCpu];
891 pGVCpu->nemr0.s.offRing3ConversionDelta = (uintptr_t)pGVM->aCpus[idCpu].pVCpuR3 - (uintptr_t)pGVCpu;
892 }
893
894 /*
895 * Get the partition ID.
896 */
897 PVMCPUCC pVCpu0 = &pGVM->aCpus[0];
898 NTSTATUS rcNt = nemR0NtPerformIoControl(pGVM, pVCpu0, pGVM->nemr0.s.IoCtlGetHvPartitionId.uFunction, NULL, 0,
899 &pVCpu0->nem.s.uIoCtlBuf.idPartition, sizeof(pVCpu0->nem.s.uIoCtlBuf.idPartition));
900# if 0
901 AssertLogRelMsgReturn(NT_SUCCESS(rcNt), ("IoCtlGetHvPartitionId failed: %#x\n", rcNt), VERR_NEM_INIT_FAILED);
902 pGVM->nemr0.s.idHvPartition = pVCpu0->nem.s.uIoCtlBuf.idPartition;
903# else
904 /*
905 * Since 2021 (Win11) the above I/O control doesn't work on exo-partitions
906 * so we have to go to extremes to get at it. Sigh.
907 */
908 if ( !NT_SUCCESS(rcNt)
909 || pVCpu0->nem.s.uIoCtlBuf.idPartition == HV_PARTITION_ID_INVALID)
910 {
911 LogRel(("IoCtlGetHvPartitionId failed: r0=%#RX64, r3=%#RX64, rcNt=%#x\n",
912 pGVM->nemr0.s.idHvPartition, pGVM->nem.s.idHvPartition, rcNt));
913
914 RTR0MEMOBJ ahMemObjs[6]
915 = { NIL_RTR0MEMOBJ, NIL_RTR0MEMOBJ, NIL_RTR0MEMOBJ, NIL_RTR0MEMOBJ, NIL_RTR0MEMOBJ, NIL_RTR0MEMOBJ };
916 rc = nemR0InitVMPart2DontWannaDoTheseUglyPartitionIdFallbacks(pGVM, ahMemObjs);
917 size_t i = RT_ELEMENTS(ahMemObjs);
918 while (i-- > 0)
919 RTR0MemObjFree(ahMemObjs[i], false /*fFreeMappings*/);
920 }
921 else
922 pGVM->nemr0.s.idHvPartition = pVCpu0->nem.s.uIoCtlBuf.idPartition;
923
924 if (pGVM->nem.s.idHvPartition == HV_PARTITION_ID_INVALID)
925 pGVM->nem.s.idHvPartition = pGVM->nemr0.s.idHvPartition;
926# endif
927 AssertLogRelMsgReturn(pGVM->nemr0.s.idHvPartition == pGVM->nem.s.idHvPartition,
928 ("idHvPartition mismatch: r0=%#RX64, r3=%#RX64\n", pGVM->nemr0.s.idHvPartition, pGVM->nem.s.idHvPartition),
929 VERR_NEM_INIT_FAILED);
930 if (RT_SUCCESS(rc) && pGVM->nemr0.s.idHvPartition == HV_PARTITION_ID_INVALID)
931 rc = VERR_NEM_INIT_FAILED;
932 }
933#endif /* NEM_WIN_USE_HYPERCALLS_FOR_PAGES */
934
935 return rc;
936}
937
938
939/**
940 * Cleanup the NEM parts of the VM in ring-0.
941 *
942 * This is always called and must deal the state regardless of whether
943 * NEMR0InitVM() was called or not. So, take care here.
944 *
945 * @param pGVM The ring-0 VM handle.
946 */
947VMMR0_INT_DECL(void) NEMR0CleanupVM(PGVM pGVM)
948{
949#ifdef NEM_WIN_USE_HYPERCALLS_FOR_PAGES
950 pGVM->nemr0.s.idHvPartition = HV_PARTITION_ID_INVALID;
951
952 /* Clean up I/O control context. */
953 if (pGVM->nemr0.s.pIoCtlCtx)
954 {
955 int rc = SUPR0IoCtlCleanup(pGVM->nemr0.s.pIoCtlCtx);
956 AssertRC(rc);
957 pGVM->nemr0.s.pIoCtlCtx = NULL;
958 }
959
960 /* Free the hypercall pages. */
961 VMCPUID i = pGVM->cCpus;
962 while (i-- > 0)
963 nemR0DeleteHypercallData(&pGVM->aCpus[i].nemr0.s.HypercallData);
964
965 /* The non-EMT one too. */
966 if (RTCritSectIsInitialized(&pGVM->nemr0.s.HypercallDataCritSect))
967 RTCritSectDelete(&pGVM->nemr0.s.HypercallDataCritSect);
968 nemR0DeleteHypercallData(&pGVM->nemr0.s.HypercallData);
969#else
970 RT_NOREF(pGVM);
971#endif
972}
973
974
975#if 0 /* for debugging GPA unmapping. */
976static int nemR3WinDummyReadGpa(PGVM pGVM, PGVMCPU pGVCpu, RTGCPHYS GCPhys)
977{
978 PHV_INPUT_READ_GPA pIn = (PHV_INPUT_READ_GPA)pGVCpu->nemr0.s.pbHypercallData;
979 PHV_OUTPUT_READ_GPA pOut = (PHV_OUTPUT_READ_GPA)(pIn + 1);
980 pIn->PartitionId = pGVM->nemr0.s.idHvPartition;
981 pIn->VpIndex = pGVCpu->idCpu;
982 pIn->ByteCount = 0x10;
983 pIn->BaseGpa = GCPhys;
984 pIn->ControlFlags.AsUINT64 = 0;
985 pIn->ControlFlags.CacheType = HvCacheTypeX64WriteCombining;
986 memset(pOut, 0xfe, sizeof(*pOut));
987 uint64_t volatile uResult = g_pfnHvlInvokeHypercall(HvCallReadGpa, pGVCpu->nemr0.s.HCPhysHypercallData,
988 pGVCpu->nemr0.s.HCPhysHypercallData + sizeof(*pIn));
989 LogRel(("nemR3WinDummyReadGpa: %RGp -> %#RX64; code=%u rsvd=%u abData=%.16Rhxs\n",
990 GCPhys, uResult, pOut->AccessResult.ResultCode, pOut->AccessResult.Reserved, pOut->Data));
991 __debugbreak();
992
993 return uResult != 0 ? VERR_READ_ERROR : VINF_SUCCESS;
994}
995#endif
996
997
998#ifdef NEM_WIN_USE_HYPERCALLS_FOR_PAGES
999/**
1000 * Worker for NEMR0MapPages and others.
1001 */
1002NEM_TMPL_STATIC int nemR0WinMapPages(PGVM pGVM, PGVMCPU pGVCpu, RTGCPHYS GCPhysSrc, RTGCPHYS GCPhysDst,
1003 uint32_t cPages, uint32_t fFlags)
1004{
1005 /*
1006 * Validate.
1007 */
1008 AssertReturn(g_pfnHvlInvokeHypercall, VERR_NEM_MISSING_KERNEL_API_1);
1009
1010 AssertReturn(cPages > 0, VERR_OUT_OF_RANGE);
1011 AssertReturn(cPages <= NEM_MAX_MAP_PAGES, VERR_OUT_OF_RANGE);
1012 AssertReturn(!(fFlags & ~(HV_MAP_GPA_MAYBE_ACCESS_MASK & ~HV_MAP_GPA_DUNNO_ACCESS)), VERR_INVALID_FLAGS);
1013 AssertMsgReturn(!(GCPhysDst & X86_PAGE_OFFSET_MASK), ("GCPhysDst=%RGp\n", GCPhysDst), VERR_OUT_OF_RANGE);
1014 AssertReturn(GCPhysDst < _1E, VERR_OUT_OF_RANGE);
1015 if (GCPhysSrc != GCPhysDst)
1016 {
1017 AssertMsgReturn(!(GCPhysSrc & X86_PAGE_OFFSET_MASK), ("GCPhysSrc=%RGp\n", GCPhysSrc), VERR_OUT_OF_RANGE);
1018 AssertReturn(GCPhysSrc < _1E, VERR_OUT_OF_RANGE);
1019 }
1020
1021 /*
1022 * Compose and make the hypercall.
1023 * Ring-3 is not allowed to fill in the host physical addresses of the call.
1024 */
1025 for (uint32_t iTries = 0;; iTries++)
1026 {
1027 RTGCPHYS GCPhysSrcTmp = GCPhysSrc;
1028 HV_INPUT_MAP_GPA_PAGES *pMapPages = (HV_INPUT_MAP_GPA_PAGES *)pGVCpu->nemr0.s.HypercallData.pbPage;
1029 AssertPtrReturn(pMapPages, VERR_INTERNAL_ERROR_3);
1030 pMapPages->TargetPartitionId = pGVM->nemr0.s.idHvPartition;
1031 pMapPages->TargetGpaBase = GCPhysDst >> X86_PAGE_SHIFT;
1032 pMapPages->MapFlags = fFlags;
1033 pMapPages->u32ExplicitPadding = 0;
1034
1035 for (uint32_t iPage = 0; iPage < cPages; iPage++, GCPhysSrcTmp += X86_PAGE_SIZE)
1036 {
1037 RTHCPHYS HCPhys = NIL_RTGCPHYS;
1038 int rc = PGMPhysGCPhys2HCPhys(pGVM, GCPhysSrcTmp, &HCPhys);
1039 AssertRCReturn(rc, rc);
1040 pMapPages->PageList[iPage] = HCPhys >> X86_PAGE_SHIFT;
1041 }
1042
1043 uint64_t uResult = g_pfnHvlInvokeHypercall(HvCallMapGpaPages | ((uint64_t)cPages << 32),
1044 pGVCpu->nemr0.s.HypercallData.HCPhysPage, 0);
1045 Log6(("NEMR0MapPages: %RGp/%RGp L %u prot %#x -> %#RX64\n",
1046 GCPhysDst, GCPhysSrcTmp - cPages * X86_PAGE_SIZE, cPages, fFlags, uResult));
1047 if (uResult == ((uint64_t)cPages << 32))
1048 return VINF_SUCCESS;
1049
1050 /*
1051 * If the partition is out of memory, try donate another 512 pages to
1052 * it (2MB). VID.SYS does multiples of 512 pages, nothing smaller.
1053 */
1054 if ( uResult != HV_STATUS_INSUFFICIENT_MEMORY
1055 || iTries > 16
1056 || g_pfnWinHvDepositMemory == NULL)
1057 {
1058 LogRel(("g_pfnHvlInvokeHypercall/MapGpaPages -> %#RX64\n", uResult));
1059 return VERR_NEM_MAP_PAGES_FAILED;
1060 }
1061
1062 size_t cPagesAdded = 0;
1063 NTSTATUS rcNt = g_pfnWinHvDepositMemory(pGVM->nemr0.s.idHvPartition, 512, 0, &cPagesAdded);
1064 if (!cPagesAdded)
1065 {
1066 LogRel(("g_pfnWinHvDepositMemory -> %#x / %#RX64\n", rcNt, uResult));
1067 return VERR_NEM_MAP_PAGES_FAILED;
1068 }
1069 }
1070}
1071#endif /* NEM_WIN_USE_HYPERCALLS_FOR_PAGES */
1072
1073
1074/**
1075 * Maps pages into the guest physical address space.
1076 *
1077 * Generally the caller will be under the PGM lock already, so no extra effort
1078 * is needed to make sure all changes happens under it.
1079 *
1080 * @returns VBox status code.
1081 * @param pGVM The ring-0 VM handle.
1082 * @param idCpu The calling EMT. Necessary for getting the
1083 * hypercall page and arguments.
1084 * @thread EMT(idCpu)
1085 */
1086VMMR0_INT_DECL(int) NEMR0MapPages(PGVM pGVM, VMCPUID idCpu)
1087{
1088#ifdef NEM_WIN_USE_HYPERCALLS_FOR_PAGES
1089 /*
1090 * Unpack the call.
1091 */
1092 int rc = GVMMR0ValidateGVMandEMT(pGVM, idCpu);
1093 if (RT_SUCCESS(rc))
1094 {
1095 PGVMCPU pGVCpu = &pGVM->aCpus[idCpu];
1096
1097 RTGCPHYS const GCPhysSrc = pGVCpu->nem.s.Hypercall.MapPages.GCPhysSrc;
1098 RTGCPHYS const GCPhysDst = pGVCpu->nem.s.Hypercall.MapPages.GCPhysDst;
1099 uint32_t const cPages = pGVCpu->nem.s.Hypercall.MapPages.cPages;
1100 HV_MAP_GPA_FLAGS const fFlags = pGVCpu->nem.s.Hypercall.MapPages.fFlags;
1101
1102 /*
1103 * Do the work.
1104 */
1105 rc = nemR0WinMapPages(pGVM, pGVCpu, GCPhysSrc, GCPhysDst, cPages, fFlags);
1106 }
1107 return rc;
1108#else
1109 RT_NOREF(pGVM, idCpu);
1110 return VERR_NOT_IMPLEMENTED;
1111#endif
1112}
1113
1114
1115#ifdef NEM_WIN_USE_HYPERCALLS_FOR_PAGES
1116/**
1117 * Worker for NEMR0UnmapPages and others.
1118 */
1119NEM_TMPL_STATIC int nemR0WinUnmapPages(PGVM pGVM, PGVMCPU pGVCpu, RTGCPHYS GCPhys, uint32_t cPages)
1120{
1121 /*
1122 * Validate input.
1123 */
1124 AssertReturn(g_pfnHvlInvokeHypercall, VERR_NEM_MISSING_KERNEL_API_1);
1125
1126 AssertReturn(cPages > 0, VERR_OUT_OF_RANGE);
1127 AssertReturn(cPages <= NEM_MAX_UNMAP_PAGES, VERR_OUT_OF_RANGE);
1128 AssertMsgReturn(!(GCPhys & X86_PAGE_OFFSET_MASK), ("%RGp\n", GCPhys), VERR_OUT_OF_RANGE);
1129 AssertReturn(GCPhys < _1E, VERR_OUT_OF_RANGE);
1130
1131 /*
1132 * Compose and make the hypercall.
1133 */
1134 HV_INPUT_UNMAP_GPA_PAGES *pUnmapPages = (HV_INPUT_UNMAP_GPA_PAGES *)pGVCpu->nemr0.s.HypercallData.pbPage;
1135 AssertPtrReturn(pUnmapPages, VERR_INTERNAL_ERROR_3);
1136 pUnmapPages->TargetPartitionId = pGVM->nemr0.s.idHvPartition;
1137 pUnmapPages->TargetGpaBase = GCPhys >> X86_PAGE_SHIFT;
1138 pUnmapPages->fFlags = 0;
1139
1140 uint64_t uResult = g_pfnHvlInvokeHypercall(HvCallUnmapGpaPages | ((uint64_t)cPages << 32),
1141 pGVCpu->nemr0.s.HypercallData.HCPhysPage, 0);
1142 Log6(("NEMR0UnmapPages: %RGp L %u -> %#RX64\n", GCPhys, cPages, uResult));
1143 if (uResult == ((uint64_t)cPages << 32))
1144 {
1145# if 1 /* Do we need to do this? Hopefully not... */
1146 uint64_t volatile uR = g_pfnHvlInvokeHypercall(HvCallUncommitGpaPages | ((uint64_t)cPages << 32),
1147 pGVCpu->nemr0.s.HypercallData.HCPhysPage, 0);
1148 AssertMsg(uR == ((uint64_t)cPages << 32), ("uR=%#RX64\n", uR)); NOREF(uR);
1149# endif
1150 return VINF_SUCCESS;
1151 }
1152
1153 LogRel(("g_pfnHvlInvokeHypercall/UnmapGpaPages -> %#RX64\n", uResult));
1154 return VERR_NEM_UNMAP_PAGES_FAILED;
1155}
1156#endif /* NEM_WIN_USE_HYPERCALLS_FOR_PAGES */
1157
1158
1159/**
1160 * Unmaps pages from the guest physical address space.
1161 *
1162 * Generally the caller will be under the PGM lock already, so no extra effort
1163 * is needed to make sure all changes happens under it.
1164 *
1165 * @returns VBox status code.
1166 * @param pGVM The ring-0 VM handle.
1167 * @param idCpu The calling EMT. Necessary for getting the
1168 * hypercall page and arguments.
1169 * @thread EMT(idCpu)
1170 */
1171VMMR0_INT_DECL(int) NEMR0UnmapPages(PGVM pGVM, VMCPUID idCpu)
1172{
1173#ifdef NEM_WIN_USE_HYPERCALLS_FOR_PAGES
1174 /*
1175 * Unpack the call.
1176 */
1177 int rc = GVMMR0ValidateGVMandEMT(pGVM, idCpu);
1178 if (RT_SUCCESS(rc))
1179 {
1180 PGVMCPU pGVCpu = &pGVM->aCpus[idCpu];
1181
1182 RTGCPHYS const GCPhys = pGVCpu->nem.s.Hypercall.UnmapPages.GCPhys;
1183 uint32_t const cPages = pGVCpu->nem.s.Hypercall.UnmapPages.cPages;
1184
1185 /*
1186 * Do the work.
1187 */
1188 rc = nemR0WinUnmapPages(pGVM, pGVCpu, GCPhys, cPages);
1189 }
1190 return rc;
1191#else
1192 RT_NOREF(pGVM, idCpu);
1193 return VERR_NOT_IMPLEMENTED;
1194#endif
1195}
1196
1197
1198#if defined(NEM_WIN_WITH_RING0_RUNLOOP) || defined(NEM_WIN_USE_HYPERCALLS_FOR_REGISTERS)
1199/**
1200 * Worker for NEMR0ExportState.
1201 *
1202 * Intention is to use it internally later.
1203 *
1204 * @returns VBox status code.
1205 * @param pGVM The ring-0 VM handle.
1206 * @param pGVCpu The ring-0 VCPU handle.
1207 * @param pCtx The CPU context structure to import into.
1208 */
1209NEM_TMPL_STATIC int nemR0WinExportState(PGVM pGVM, PGVMCPU pGVCpu, PCPUMCTX pCtx)
1210{
1211 HV_INPUT_SET_VP_REGISTERS *pInput = (HV_INPUT_SET_VP_REGISTERS *)pGVCpu->nemr0.s.HypercallData.pbPage;
1212 AssertPtrReturn(pInput, VERR_INTERNAL_ERROR_3);
1213 AssertReturn(g_pfnHvlInvokeHypercall, VERR_NEM_MISSING_KERNEL_API_1);
1214
1215 pInput->PartitionId = pGVM->nemr0.s.idHvPartition;
1216 pInput->VpIndex = pGVCpu->idCpu;
1217 pInput->RsvdZ = 0;
1218
1219 uint64_t const fWhat = ~pCtx->fExtrn & (CPUMCTX_EXTRN_ALL | CPUMCTX_EXTRN_NEM_WIN_MASK);
1220 if ( !fWhat
1221 && pGVCpu->nem.s.fCurrentInterruptWindows == pGVCpu->nem.s.fDesiredInterruptWindows)
1222 return VINF_SUCCESS;
1223 uintptr_t iReg = 0;
1224
1225 /* GPRs */
1226 if (fWhat & CPUMCTX_EXTRN_GPRS_MASK)
1227 {
1228 if (fWhat & CPUMCTX_EXTRN_RAX)
1229 {
1230 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1231 pInput->Elements[iReg].Name = HvX64RegisterRax;
1232 pInput->Elements[iReg].Value.Reg64 = pCtx->rax;
1233 iReg++;
1234 }
1235 if (fWhat & CPUMCTX_EXTRN_RCX)
1236 {
1237 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1238 pInput->Elements[iReg].Name = HvX64RegisterRcx;
1239 pInput->Elements[iReg].Value.Reg64 = pCtx->rcx;
1240 iReg++;
1241 }
1242 if (fWhat & CPUMCTX_EXTRN_RDX)
1243 {
1244 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1245 pInput->Elements[iReg].Name = HvX64RegisterRdx;
1246 pInput->Elements[iReg].Value.Reg64 = pCtx->rdx;
1247 iReg++;
1248 }
1249 if (fWhat & CPUMCTX_EXTRN_RBX)
1250 {
1251 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1252 pInput->Elements[iReg].Name = HvX64RegisterRbx;
1253 pInput->Elements[iReg].Value.Reg64 = pCtx->rbx;
1254 iReg++;
1255 }
1256 if (fWhat & CPUMCTX_EXTRN_RSP)
1257 {
1258 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1259 pInput->Elements[iReg].Name = HvX64RegisterRsp;
1260 pInput->Elements[iReg].Value.Reg64 = pCtx->rsp;
1261 iReg++;
1262 }
1263 if (fWhat & CPUMCTX_EXTRN_RBP)
1264 {
1265 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1266 pInput->Elements[iReg].Name = HvX64RegisterRbp;
1267 pInput->Elements[iReg].Value.Reg64 = pCtx->rbp;
1268 iReg++;
1269 }
1270 if (fWhat & CPUMCTX_EXTRN_RSI)
1271 {
1272 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1273 pInput->Elements[iReg].Name = HvX64RegisterRsi;
1274 pInput->Elements[iReg].Value.Reg64 = pCtx->rsi;
1275 iReg++;
1276 }
1277 if (fWhat & CPUMCTX_EXTRN_RDI)
1278 {
1279 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1280 pInput->Elements[iReg].Name = HvX64RegisterRdi;
1281 pInput->Elements[iReg].Value.Reg64 = pCtx->rdi;
1282 iReg++;
1283 }
1284 if (fWhat & CPUMCTX_EXTRN_R8_R15)
1285 {
1286 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1287 pInput->Elements[iReg].Name = HvX64RegisterR8;
1288 pInput->Elements[iReg].Value.Reg64 = pCtx->r8;
1289 iReg++;
1290 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1291 pInput->Elements[iReg].Name = HvX64RegisterR9;
1292 pInput->Elements[iReg].Value.Reg64 = pCtx->r9;
1293 iReg++;
1294 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1295 pInput->Elements[iReg].Name = HvX64RegisterR10;
1296 pInput->Elements[iReg].Value.Reg64 = pCtx->r10;
1297 iReg++;
1298 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1299 pInput->Elements[iReg].Name = HvX64RegisterR11;
1300 pInput->Elements[iReg].Value.Reg64 = pCtx->r11;
1301 iReg++;
1302 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1303 pInput->Elements[iReg].Name = HvX64RegisterR12;
1304 pInput->Elements[iReg].Value.Reg64 = pCtx->r12;
1305 iReg++;
1306 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1307 pInput->Elements[iReg].Name = HvX64RegisterR13;
1308 pInput->Elements[iReg].Value.Reg64 = pCtx->r13;
1309 iReg++;
1310 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1311 pInput->Elements[iReg].Name = HvX64RegisterR14;
1312 pInput->Elements[iReg].Value.Reg64 = pCtx->r14;
1313 iReg++;
1314 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1315 pInput->Elements[iReg].Name = HvX64RegisterR15;
1316 pInput->Elements[iReg].Value.Reg64 = pCtx->r15;
1317 iReg++;
1318 }
1319 }
1320
1321 /* RIP & Flags */
1322 if (fWhat & CPUMCTX_EXTRN_RIP)
1323 {
1324 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1325 pInput->Elements[iReg].Name = HvX64RegisterRip;
1326 pInput->Elements[iReg].Value.Reg64 = pCtx->rip;
1327 iReg++;
1328 }
1329 if (fWhat & CPUMCTX_EXTRN_RFLAGS)
1330 {
1331 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1332 pInput->Elements[iReg].Name = HvX64RegisterRflags;
1333 pInput->Elements[iReg].Value.Reg64 = pCtx->rflags.u;
1334 iReg++;
1335 }
1336
1337 /* Segments */
1338# define COPY_OUT_SEG(a_idx, a_enmName, a_SReg) \
1339 do { \
1340 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[a_idx]); \
1341 pInput->Elements[a_idx].Name = a_enmName; \
1342 pInput->Elements[a_idx].Value.Segment.Base = (a_SReg).u64Base; \
1343 pInput->Elements[a_idx].Value.Segment.Limit = (a_SReg).u32Limit; \
1344 pInput->Elements[a_idx].Value.Segment.Selector = (a_SReg).Sel; \
1345 pInput->Elements[a_idx].Value.Segment.Attributes = (a_SReg).Attr.u; \
1346 } while (0)
1347 if (fWhat & CPUMCTX_EXTRN_SREG_MASK)
1348 {
1349 if (fWhat & CPUMCTX_EXTRN_CS)
1350 {
1351 COPY_OUT_SEG(iReg, HvX64RegisterCs, pCtx->cs);
1352 iReg++;
1353 }
1354 if (fWhat & CPUMCTX_EXTRN_ES)
1355 {
1356 COPY_OUT_SEG(iReg, HvX64RegisterEs, pCtx->es);
1357 iReg++;
1358 }
1359 if (fWhat & CPUMCTX_EXTRN_SS)
1360 {
1361 COPY_OUT_SEG(iReg, HvX64RegisterSs, pCtx->ss);
1362 iReg++;
1363 }
1364 if (fWhat & CPUMCTX_EXTRN_DS)
1365 {
1366 COPY_OUT_SEG(iReg, HvX64RegisterDs, pCtx->ds);
1367 iReg++;
1368 }
1369 if (fWhat & CPUMCTX_EXTRN_FS)
1370 {
1371 COPY_OUT_SEG(iReg, HvX64RegisterFs, pCtx->fs);
1372 iReg++;
1373 }
1374 if (fWhat & CPUMCTX_EXTRN_GS)
1375 {
1376 COPY_OUT_SEG(iReg, HvX64RegisterGs, pCtx->gs);
1377 iReg++;
1378 }
1379 }
1380
1381 /* Descriptor tables & task segment. */
1382 if (fWhat & CPUMCTX_EXTRN_TABLE_MASK)
1383 {
1384 if (fWhat & CPUMCTX_EXTRN_LDTR)
1385 {
1386 COPY_OUT_SEG(iReg, HvX64RegisterLdtr, pCtx->ldtr);
1387 iReg++;
1388 }
1389 if (fWhat & CPUMCTX_EXTRN_TR)
1390 {
1391 COPY_OUT_SEG(iReg, HvX64RegisterTr, pCtx->tr);
1392 iReg++;
1393 }
1394
1395 if (fWhat & CPUMCTX_EXTRN_IDTR)
1396 {
1397 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1398 pInput->Elements[iReg].Value.Table.Pad[0] = 0;
1399 pInput->Elements[iReg].Value.Table.Pad[1] = 0;
1400 pInput->Elements[iReg].Value.Table.Pad[2] = 0;
1401 pInput->Elements[iReg].Name = HvX64RegisterIdtr;
1402 pInput->Elements[iReg].Value.Table.Limit = pCtx->idtr.cbIdt;
1403 pInput->Elements[iReg].Value.Table.Base = pCtx->idtr.pIdt;
1404 iReg++;
1405 }
1406 if (fWhat & CPUMCTX_EXTRN_GDTR)
1407 {
1408 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1409 pInput->Elements[iReg].Value.Table.Pad[0] = 0;
1410 pInput->Elements[iReg].Value.Table.Pad[1] = 0;
1411 pInput->Elements[iReg].Value.Table.Pad[2] = 0;
1412 pInput->Elements[iReg].Name = HvX64RegisterGdtr;
1413 pInput->Elements[iReg].Value.Table.Limit = pCtx->gdtr.cbGdt;
1414 pInput->Elements[iReg].Value.Table.Base = pCtx->gdtr.pGdt;
1415 iReg++;
1416 }
1417 }
1418
1419 /* Control registers. */
1420 if (fWhat & CPUMCTX_EXTRN_CR_MASK)
1421 {
1422 if (fWhat & CPUMCTX_EXTRN_CR0)
1423 {
1424 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1425 pInput->Elements[iReg].Name = HvX64RegisterCr0;
1426 pInput->Elements[iReg].Value.Reg64 = pCtx->cr0;
1427 iReg++;
1428 }
1429 if (fWhat & CPUMCTX_EXTRN_CR2)
1430 {
1431 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1432 pInput->Elements[iReg].Name = HvX64RegisterCr2;
1433 pInput->Elements[iReg].Value.Reg64 = pCtx->cr2;
1434 iReg++;
1435 }
1436 if (fWhat & CPUMCTX_EXTRN_CR3)
1437 {
1438 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1439 pInput->Elements[iReg].Name = HvX64RegisterCr3;
1440 pInput->Elements[iReg].Value.Reg64 = pCtx->cr3;
1441 iReg++;
1442 }
1443 if (fWhat & CPUMCTX_EXTRN_CR4)
1444 {
1445 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1446 pInput->Elements[iReg].Name = HvX64RegisterCr4;
1447 pInput->Elements[iReg].Value.Reg64 = pCtx->cr4;
1448 iReg++;
1449 }
1450 }
1451 if (fWhat & CPUMCTX_EXTRN_APIC_TPR)
1452 {
1453 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1454 pInput->Elements[iReg].Name = HvX64RegisterCr8;
1455 pInput->Elements[iReg].Value.Reg64 = CPUMGetGuestCR8(pGVCpu);
1456 iReg++;
1457 }
1458
1459 /** @todo does HvX64RegisterXfem mean XCR0? What about the related MSR. */
1460
1461 /* Debug registers. */
1462/** @todo fixme. Figure out what the hyper-v version of KVM_SET_GUEST_DEBUG would be. */
1463 if (fWhat & CPUMCTX_EXTRN_DR0_DR3)
1464 {
1465 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1466 pInput->Elements[iReg].Name = HvX64RegisterDr0;
1467 //pInput->Elements[iReg].Value.Reg64 = CPUMGetHyperDR0(pGVCpu);
1468 pInput->Elements[iReg].Value.Reg64 = pCtx->dr[0];
1469 iReg++;
1470 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1471 pInput->Elements[iReg].Name = HvX64RegisterDr1;
1472 //pInput->Elements[iReg].Value.Reg64 = CPUMGetHyperDR1(pGVCpu);
1473 pInput->Elements[iReg].Value.Reg64 = pCtx->dr[1];
1474 iReg++;
1475 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1476 pInput->Elements[iReg].Name = HvX64RegisterDr2;
1477 //pInput->Elements[iReg].Value.Reg64 = CPUMGetHyperDR2(pGVCpu);
1478 pInput->Elements[iReg].Value.Reg64 = pCtx->dr[2];
1479 iReg++;
1480 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1481 pInput->Elements[iReg].Name = HvX64RegisterDr3;
1482 //pInput->Elements[iReg].Value.Reg64 = CPUMGetHyperDR3(pGVCpu);
1483 pInput->Elements[iReg].Value.Reg64 = pCtx->dr[3];
1484 iReg++;
1485 }
1486 if (fWhat & CPUMCTX_EXTRN_DR6)
1487 {
1488 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1489 pInput->Elements[iReg].Name = HvX64RegisterDr6;
1490 //pInput->Elements[iReg].Value.Reg64 = CPUMGetHyperDR6(pGVCpu);
1491 pInput->Elements[iReg].Value.Reg64 = pCtx->dr[6];
1492 iReg++;
1493 }
1494 if (fWhat & CPUMCTX_EXTRN_DR7)
1495 {
1496 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1497 pInput->Elements[iReg].Name = HvX64RegisterDr7;
1498 //pInput->Elements[iReg].Value.Reg64 = CPUMGetHyperDR7(pGVCpu);
1499 pInput->Elements[iReg].Value.Reg64 = pCtx->dr[7];
1500 iReg++;
1501 }
1502
1503 /* Floating point state. */
1504 if (fWhat & CPUMCTX_EXTRN_X87)
1505 {
1506 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1507 pInput->Elements[iReg].Name = HvX64RegisterFpMmx0;
1508 pInput->Elements[iReg].Value.Fp.AsUINT128.Low64 = pCtx->XState.x87.aRegs[0].au64[0];
1509 pInput->Elements[iReg].Value.Fp.AsUINT128.High64 = pCtx->XState.x87.aRegs[0].au64[1];
1510 iReg++;
1511 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1512 pInput->Elements[iReg].Name = HvX64RegisterFpMmx1;
1513 pInput->Elements[iReg].Value.Fp.AsUINT128.Low64 = pCtx->XState.x87.aRegs[1].au64[0];
1514 pInput->Elements[iReg].Value.Fp.AsUINT128.High64 = pCtx->XState.x87.aRegs[1].au64[1];
1515 iReg++;
1516 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1517 pInput->Elements[iReg].Name = HvX64RegisterFpMmx2;
1518 pInput->Elements[iReg].Value.Fp.AsUINT128.Low64 = pCtx->XState.x87.aRegs[2].au64[0];
1519 pInput->Elements[iReg].Value.Fp.AsUINT128.High64 = pCtx->XState.x87.aRegs[2].au64[1];
1520 iReg++;
1521 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1522 pInput->Elements[iReg].Name = HvX64RegisterFpMmx3;
1523 pInput->Elements[iReg].Value.Fp.AsUINT128.Low64 = pCtx->XState.x87.aRegs[3].au64[0];
1524 pInput->Elements[iReg].Value.Fp.AsUINT128.High64 = pCtx->XState.x87.aRegs[3].au64[1];
1525 iReg++;
1526 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1527 pInput->Elements[iReg].Name = HvX64RegisterFpMmx4;
1528 pInput->Elements[iReg].Value.Fp.AsUINT128.Low64 = pCtx->XState.x87.aRegs[4].au64[0];
1529 pInput->Elements[iReg].Value.Fp.AsUINT128.High64 = pCtx->XState.x87.aRegs[4].au64[1];
1530 iReg++;
1531 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1532 pInput->Elements[iReg].Name = HvX64RegisterFpMmx5;
1533 pInput->Elements[iReg].Value.Fp.AsUINT128.Low64 = pCtx->XState.x87.aRegs[5].au64[0];
1534 pInput->Elements[iReg].Value.Fp.AsUINT128.High64 = pCtx->XState.x87.aRegs[5].au64[1];
1535 iReg++;
1536 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1537 pInput->Elements[iReg].Name = HvX64RegisterFpMmx6;
1538 pInput->Elements[iReg].Value.Fp.AsUINT128.Low64 = pCtx->XState.x87.aRegs[6].au64[0];
1539 pInput->Elements[iReg].Value.Fp.AsUINT128.High64 = pCtx->XState.x87.aRegs[6].au64[1];
1540 iReg++;
1541 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1542 pInput->Elements[iReg].Name = HvX64RegisterFpMmx7;
1543 pInput->Elements[iReg].Value.Fp.AsUINT128.Low64 = pCtx->XState.x87.aRegs[7].au64[0];
1544 pInput->Elements[iReg].Value.Fp.AsUINT128.High64 = pCtx->XState.x87.aRegs[7].au64[1];
1545 iReg++;
1546
1547 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1548 pInput->Elements[iReg].Name = HvX64RegisterFpControlStatus;
1549 pInput->Elements[iReg].Value.FpControlStatus.FpControl = pCtx->XState.x87.FCW;
1550 pInput->Elements[iReg].Value.FpControlStatus.FpStatus = pCtx->XState.x87.FSW;
1551 pInput->Elements[iReg].Value.FpControlStatus.FpTag = pCtx->XState.x87.FTW;
1552 pInput->Elements[iReg].Value.FpControlStatus.Reserved = pCtx->XState.x87.FTW >> 8;
1553 pInput->Elements[iReg].Value.FpControlStatus.LastFpOp = pCtx->XState.x87.FOP;
1554 pInput->Elements[iReg].Value.FpControlStatus.LastFpRip = (pCtx->XState.x87.FPUIP)
1555 | ((uint64_t)pCtx->XState.x87.CS << 32)
1556 | ((uint64_t)pCtx->XState.x87.Rsrvd1 << 48);
1557 iReg++;
1558/** @todo we've got trouble if if we try write just SSE w/o X87. */
1559 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1560 pInput->Elements[iReg].Name = HvX64RegisterXmmControlStatus;
1561 pInput->Elements[iReg].Value.XmmControlStatus.LastFpRdp = (pCtx->XState.x87.FPUDP)
1562 | ((uint64_t)pCtx->XState.x87.DS << 32)
1563 | ((uint64_t)pCtx->XState.x87.Rsrvd2 << 48);
1564 pInput->Elements[iReg].Value.XmmControlStatus.XmmStatusControl = pCtx->XState.x87.MXCSR;
1565 pInput->Elements[iReg].Value.XmmControlStatus.XmmStatusControlMask = pCtx->XState.x87.MXCSR_MASK; /** @todo ??? (Isn't this an output field?) */
1566 iReg++;
1567 }
1568
1569 /* Vector state. */
1570 if (fWhat & CPUMCTX_EXTRN_SSE_AVX)
1571 {
1572 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1573 pInput->Elements[iReg].Name = HvX64RegisterXmm0;
1574 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->XState.x87.aXMM[0].uXmm.s.Lo;
1575 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->XState.x87.aXMM[0].uXmm.s.Hi;
1576 iReg++;
1577 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1578 pInput->Elements[iReg].Name = HvX64RegisterXmm1;
1579 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->XState.x87.aXMM[1].uXmm.s.Lo;
1580 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->XState.x87.aXMM[1].uXmm.s.Hi;
1581 iReg++;
1582 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1583 pInput->Elements[iReg].Name = HvX64RegisterXmm2;
1584 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->XState.x87.aXMM[2].uXmm.s.Lo;
1585 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->XState.x87.aXMM[2].uXmm.s.Hi;
1586 iReg++;
1587 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1588 pInput->Elements[iReg].Name = HvX64RegisterXmm3;
1589 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->XState.x87.aXMM[3].uXmm.s.Lo;
1590 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->XState.x87.aXMM[3].uXmm.s.Hi;
1591 iReg++;
1592 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1593 pInput->Elements[iReg].Name = HvX64RegisterXmm4;
1594 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->XState.x87.aXMM[4].uXmm.s.Lo;
1595 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->XState.x87.aXMM[4].uXmm.s.Hi;
1596 iReg++;
1597 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1598 pInput->Elements[iReg].Name = HvX64RegisterXmm5;
1599 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->XState.x87.aXMM[5].uXmm.s.Lo;
1600 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->XState.x87.aXMM[5].uXmm.s.Hi;
1601 iReg++;
1602 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1603 pInput->Elements[iReg].Name = HvX64RegisterXmm6;
1604 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->XState.x87.aXMM[6].uXmm.s.Lo;
1605 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->XState.x87.aXMM[6].uXmm.s.Hi;
1606 iReg++;
1607 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1608 pInput->Elements[iReg].Name = HvX64RegisterXmm7;
1609 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->XState.x87.aXMM[7].uXmm.s.Lo;
1610 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->XState.x87.aXMM[7].uXmm.s.Hi;
1611 iReg++;
1612 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1613 pInput->Elements[iReg].Name = HvX64RegisterXmm8;
1614 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->XState.x87.aXMM[8].uXmm.s.Lo;
1615 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->XState.x87.aXMM[8].uXmm.s.Hi;
1616 iReg++;
1617 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1618 pInput->Elements[iReg].Name = HvX64RegisterXmm9;
1619 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->XState.x87.aXMM[9].uXmm.s.Lo;
1620 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->XState.x87.aXMM[9].uXmm.s.Hi;
1621 iReg++;
1622 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1623 pInput->Elements[iReg].Name = HvX64RegisterXmm10;
1624 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->XState.x87.aXMM[10].uXmm.s.Lo;
1625 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->XState.x87.aXMM[10].uXmm.s.Hi;
1626 iReg++;
1627 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1628 pInput->Elements[iReg].Name = HvX64RegisterXmm11;
1629 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->XState.x87.aXMM[11].uXmm.s.Lo;
1630 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->XState.x87.aXMM[11].uXmm.s.Hi;
1631 iReg++;
1632 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1633 pInput->Elements[iReg].Name = HvX64RegisterXmm12;
1634 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->XState.x87.aXMM[12].uXmm.s.Lo;
1635 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->XState.x87.aXMM[12].uXmm.s.Hi;
1636 iReg++;
1637 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1638 pInput->Elements[iReg].Name = HvX64RegisterXmm13;
1639 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->XState.x87.aXMM[13].uXmm.s.Lo;
1640 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->XState.x87.aXMM[13].uXmm.s.Hi;
1641 iReg++;
1642 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1643 pInput->Elements[iReg].Name = HvX64RegisterXmm14;
1644 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->XState.x87.aXMM[14].uXmm.s.Lo;
1645 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->XState.x87.aXMM[14].uXmm.s.Hi;
1646 iReg++;
1647 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1648 pInput->Elements[iReg].Name = HvX64RegisterXmm15;
1649 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->XState.x87.aXMM[15].uXmm.s.Lo;
1650 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->XState.x87.aXMM[15].uXmm.s.Hi;
1651 iReg++;
1652 }
1653
1654 /* MSRs */
1655 // HvX64RegisterTsc - don't touch
1656 if (fWhat & CPUMCTX_EXTRN_EFER)
1657 {
1658 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1659 pInput->Elements[iReg].Name = HvX64RegisterEfer;
1660 pInput->Elements[iReg].Value.Reg64 = pCtx->msrEFER;
1661 iReg++;
1662 }
1663 if (fWhat & CPUMCTX_EXTRN_KERNEL_GS_BASE)
1664 {
1665 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1666 pInput->Elements[iReg].Name = HvX64RegisterKernelGsBase;
1667 pInput->Elements[iReg].Value.Reg64 = pCtx->msrKERNELGSBASE;
1668 iReg++;
1669 }
1670 if (fWhat & CPUMCTX_EXTRN_SYSENTER_MSRS)
1671 {
1672 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1673 pInput->Elements[iReg].Name = HvX64RegisterSysenterCs;
1674 pInput->Elements[iReg].Value.Reg64 = pCtx->SysEnter.cs;
1675 iReg++;
1676 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1677 pInput->Elements[iReg].Name = HvX64RegisterSysenterEip;
1678 pInput->Elements[iReg].Value.Reg64 = pCtx->SysEnter.eip;
1679 iReg++;
1680 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1681 pInput->Elements[iReg].Name = HvX64RegisterSysenterEsp;
1682 pInput->Elements[iReg].Value.Reg64 = pCtx->SysEnter.esp;
1683 iReg++;
1684 }
1685 if (fWhat & CPUMCTX_EXTRN_SYSCALL_MSRS)
1686 {
1687 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1688 pInput->Elements[iReg].Name = HvX64RegisterStar;
1689 pInput->Elements[iReg].Value.Reg64 = pCtx->msrSTAR;
1690 iReg++;
1691 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1692 pInput->Elements[iReg].Name = HvX64RegisterLstar;
1693 pInput->Elements[iReg].Value.Reg64 = pCtx->msrLSTAR;
1694 iReg++;
1695 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1696 pInput->Elements[iReg].Name = HvX64RegisterCstar;
1697 pInput->Elements[iReg].Value.Reg64 = pCtx->msrCSTAR;
1698 iReg++;
1699 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1700 pInput->Elements[iReg].Name = HvX64RegisterSfmask;
1701 pInput->Elements[iReg].Value.Reg64 = pCtx->msrSFMASK;
1702 iReg++;
1703 }
1704 if (fWhat & CPUMCTX_EXTRN_TSC_AUX)
1705 {
1706 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1707 pInput->Elements[iReg].Name = HvX64RegisterTscAux;
1708 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.TscAux;
1709 iReg++;
1710 }
1711 if (fWhat & CPUMCTX_EXTRN_OTHER_MSRS)
1712 {
1713 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1714 pInput->Elements[iReg].Name = HvX64RegisterApicBase;
1715 pInput->Elements[iReg].Value.Reg64 = APICGetBaseMsrNoCheck(pGVCpu);
1716 iReg++;
1717 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1718 pInput->Elements[iReg].Name = HvX64RegisterPat;
1719 pInput->Elements[iReg].Value.Reg64 = pCtx->msrPAT;
1720 iReg++;
1721# if 0 /** @todo HvX64RegisterMtrrCap is read only? Seems it's not even readable. */
1722 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1723 pInput->Elements[iReg].Name = HvX64RegisterMtrrCap;
1724 pInput->Elements[iReg].Value.Reg64 = CPUMGetGuestIa32MtrrCap(pGVCpu);
1725 iReg++;
1726# endif
1727
1728 PCPUMCTXMSRS pCtxMsrs = CPUMQueryGuestCtxMsrsPtr(pGVCpu);
1729
1730 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1731 pInput->Elements[iReg].Name = HvX64RegisterMtrrDefType;
1732 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrDefType;
1733 iReg++;
1734
1735 /** @todo we dont keep state for HvX64RegisterMtrrPhysBaseX and HvX64RegisterMtrrPhysMaskX */
1736
1737 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1738 pInput->Elements[iReg].Name = HvX64RegisterMtrrFix64k00000;
1739 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrFix64K_00000;
1740 iReg++;
1741 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1742 pInput->Elements[iReg].Name = HvX64RegisterMtrrFix16k80000;
1743 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrFix16K_80000;
1744 iReg++;
1745 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1746 pInput->Elements[iReg].Name = HvX64RegisterMtrrFix16kA0000;
1747 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrFix16K_A0000;
1748 iReg++;
1749 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1750 pInput->Elements[iReg].Name = HvX64RegisterMtrrFix4kC0000;
1751 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrFix4K_C0000;
1752 iReg++;
1753 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1754 pInput->Elements[iReg].Name = HvX64RegisterMtrrFix4kC8000;
1755 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrFix4K_C8000;
1756 iReg++;
1757 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1758 pInput->Elements[iReg].Name = HvX64RegisterMtrrFix4kD0000;
1759 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrFix4K_D0000;
1760 iReg++;
1761 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1762 pInput->Elements[iReg].Name = HvX64RegisterMtrrFix4kD8000;
1763 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrFix4K_D8000;
1764 iReg++;
1765 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1766 pInput->Elements[iReg].Name = HvX64RegisterMtrrFix4kE0000;
1767 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrFix4K_E0000;
1768 iReg++;
1769 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1770 pInput->Elements[iReg].Name = HvX64RegisterMtrrFix4kE8000;
1771 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrFix4K_E8000;
1772 iReg++;
1773 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1774 pInput->Elements[iReg].Name = HvX64RegisterMtrrFix4kF0000;
1775 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrFix4K_F0000;
1776 iReg++;
1777 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1778 pInput->Elements[iReg].Name = HvX64RegisterMtrrFix4kF8000;
1779 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrFix4K_F8000;
1780 iReg++;
1781
1782# if 0 /** @todo Why can't we write these on Intel systems? Not that we really care... */
1783 const CPUMCPUVENDOR enmCpuVendor = CPUMGetHostCpuVendor(pGVM);
1784 if (enmCpuVendor != CPUMCPUVENDOR_AMD)
1785 {
1786 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1787 pInput->Elements[iReg].Name = HvX64RegisterIa32MiscEnable;
1788 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MiscEnable;
1789 iReg++;
1790 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1791 pInput->Elements[iReg].Name = HvX64RegisterIa32FeatureControl;
1792 pInput->Elements[iReg].Value.Reg64 = CPUMGetGuestIa32FeatureControl(pGVCpu);
1793 iReg++;
1794 }
1795# endif
1796 }
1797
1798 /* event injection (clear it). */
1799 if (fWhat & CPUMCTX_EXTRN_NEM_WIN_EVENT_INJECT)
1800 {
1801 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1802 pInput->Elements[iReg].Name = HvRegisterPendingInterruption;
1803 pInput->Elements[iReg].Value.Reg64 = 0;
1804 iReg++;
1805 }
1806
1807 /* Interruptibility state. This can get a little complicated since we get
1808 half of the state via HV_X64_VP_EXECUTION_STATE. */
1809 if ( (fWhat & (CPUMCTX_EXTRN_NEM_WIN_INHIBIT_INT | CPUMCTX_EXTRN_NEM_WIN_INHIBIT_NMI))
1810 == (CPUMCTX_EXTRN_NEM_WIN_INHIBIT_INT | CPUMCTX_EXTRN_NEM_WIN_INHIBIT_NMI) )
1811 {
1812 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1813 pInput->Elements[iReg].Name = HvRegisterInterruptState;
1814 pInput->Elements[iReg].Value.Reg64 = 0;
1815 if ( VMCPU_FF_IS_SET(pGVCpu, VMCPU_FF_INHIBIT_INTERRUPTS)
1816 && EMGetInhibitInterruptsPC(pGVCpu) == pCtx->rip)
1817 pInput->Elements[iReg].Value.InterruptState.InterruptShadow = 1;
1818 if (VMCPU_FF_IS_SET(pGVCpu, VMCPU_FF_BLOCK_NMIS))
1819 pInput->Elements[iReg].Value.InterruptState.NmiMasked = 1;
1820 iReg++;
1821 }
1822 else if (fWhat & CPUMCTX_EXTRN_NEM_WIN_INHIBIT_INT)
1823 {
1824 if ( pGVCpu->nem.s.fLastInterruptShadow
1825 || ( VMCPU_FF_IS_SET(pGVCpu, VMCPU_FF_INHIBIT_INTERRUPTS)
1826 && EMGetInhibitInterruptsPC(pGVCpu) == pCtx->rip))
1827 {
1828 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1829 pInput->Elements[iReg].Name = HvRegisterInterruptState;
1830 pInput->Elements[iReg].Value.Reg64 = 0;
1831 if ( VMCPU_FF_IS_SET(pGVCpu, VMCPU_FF_INHIBIT_INTERRUPTS)
1832 && EMGetInhibitInterruptsPC(pGVCpu) == pCtx->rip)
1833 pInput->Elements[iReg].Value.InterruptState.InterruptShadow = 1;
1834 /** @todo Retrieve NMI state, currently assuming it's zero. (yes this may happen on I/O) */
1835 //if (VMCPU_FF_IS_ANY_SET(pGVCpu, VMCPU_FF_BLOCK_NMIS))
1836 // pInput->Elements[iReg].Value.InterruptState.NmiMasked = 1;
1837 iReg++;
1838 }
1839 }
1840 else
1841 Assert(!(fWhat & CPUMCTX_EXTRN_NEM_WIN_INHIBIT_NMI));
1842
1843 /* Interrupt windows. Always set if active as Hyper-V seems to be forgetful. */
1844 uint8_t const fDesiredIntWin = pGVCpu->nem.s.fDesiredInterruptWindows;
1845 if ( fDesiredIntWin
1846 || pGVCpu->nem.s.fCurrentInterruptWindows != fDesiredIntWin)
1847 {
1848 pGVCpu->nem.s.fCurrentInterruptWindows = pGVCpu->nem.s.fDesiredInterruptWindows;
1849 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1850 pInput->Elements[iReg].Name = HvX64RegisterDeliverabilityNotifications;
1851 pInput->Elements[iReg].Value.DeliverabilityNotifications.AsUINT64 = fDesiredIntWin;
1852 Assert(pInput->Elements[iReg].Value.DeliverabilityNotifications.NmiNotification == RT_BOOL(fDesiredIntWin & NEM_WIN_INTW_F_NMI));
1853 Assert(pInput->Elements[iReg].Value.DeliverabilityNotifications.InterruptNotification == RT_BOOL(fDesiredIntWin & NEM_WIN_INTW_F_REGULAR));
1854 Assert(pInput->Elements[iReg].Value.DeliverabilityNotifications.InterruptPriority == (fDesiredIntWin & NEM_WIN_INTW_F_PRIO_MASK) >> NEM_WIN_INTW_F_PRIO_SHIFT);
1855 iReg++;
1856 }
1857
1858 /// @todo HvRegisterPendingEvent0
1859 /// @todo HvRegisterPendingEvent1
1860
1861 /*
1862 * Set the registers.
1863 */
1864 Assert((uintptr_t)&pInput->Elements[iReg] - (uintptr_t)pGVCpu->nemr0.s.HypercallData.pbPage < PAGE_SIZE); /* max is 127 */
1865
1866 /*
1867 * Make the hypercall.
1868 */
1869 uint64_t uResult = g_pfnHvlInvokeHypercall(HV_MAKE_CALL_INFO(HvCallSetVpRegisters, iReg),
1870 pGVCpu->nemr0.s.HypercallData.HCPhysPage, 0 /*GCPhysOutput*/);
1871 AssertLogRelMsgReturn(uResult == HV_MAKE_CALL_REP_RET(iReg),
1872 ("uResult=%RX64 iRegs=%#x\n", uResult, iReg),
1873 VERR_NEM_SET_REGISTERS_FAILED);
1874 //LogFlow(("nemR0WinExportState: uResult=%#RX64 iReg=%zu fWhat=%#018RX64 fExtrn=%#018RX64 -> %#018RX64\n", uResult, iReg, fWhat, pCtx->fExtrn,
1875 // pCtx->fExtrn | CPUMCTX_EXTRN_ALL | CPUMCTX_EXTRN_NEM_WIN_MASK | CPUMCTX_EXTRN_KEEPER_NEM ));
1876 pCtx->fExtrn |= CPUMCTX_EXTRN_ALL | CPUMCTX_EXTRN_NEM_WIN_MASK | CPUMCTX_EXTRN_KEEPER_NEM;
1877 return VINF_SUCCESS;
1878}
1879#endif /* NEM_WIN_WITH_RING0_RUNLOOP || NEM_WIN_USE_HYPERCALLS_FOR_REGISTERS */
1880
1881
1882/**
1883 * Export the state to the native API (out of CPUMCTX).
1884 *
1885 * @returns VBox status code
1886 * @param pGVM The ring-0 VM handle.
1887 * @param idCpu The calling EMT. Necessary for getting the
1888 * hypercall page and arguments.
1889 */
1890VMMR0_INT_DECL(int) NEMR0ExportState(PGVM pGVM, VMCPUID idCpu)
1891{
1892#if defined(NEM_WIN_WITH_RING0_RUNLOOP) || defined(NEM_WIN_USE_HYPERCALLS_FOR_REGISTERS)
1893 /*
1894 * Validate the call.
1895 */
1896 int rc = GVMMR0ValidateGVMandEMT(pGVM, idCpu);
1897 if (RT_SUCCESS(rc))
1898 {
1899 PGVMCPU pGVCpu = &pGVM->aCpus[idCpu];
1900 AssertReturn(g_pfnHvlInvokeHypercall, VERR_NEM_MISSING_KERNEL_API_1);
1901
1902 /*
1903 * Call worker.
1904 */
1905 rc = nemR0WinExportState(pGVM, pGVCpu, &pGVCpu->cpum.GstCtx);
1906 }
1907 return rc;
1908#else
1909 RT_NOREF(pGVM, idCpu);
1910 return VERR_NOT_IMPLEMENTED;
1911#endif
1912}
1913
1914
1915#if defined(NEM_WIN_WITH_RING0_RUNLOOP) || defined(NEM_WIN_USE_HYPERCALLS_FOR_REGISTERS)
1916/**
1917 * Worker for NEMR0ImportState.
1918 *
1919 * Intention is to use it internally later.
1920 *
1921 * @returns VBox status code.
1922 * @param pGVM The ring-0 VM handle.
1923 * @param pGVCpu The ring-0 VCPU handle.
1924 * @param pCtx The CPU context structure to import into.
1925 * @param fWhat What to import, CPUMCTX_EXTRN_XXX.
1926 * @param fCanUpdateCr3 Whether it's safe to update CR3 or not.
1927 */
1928NEM_TMPL_STATIC int nemR0WinImportState(PGVM pGVM, PGVMCPU pGVCpu, PCPUMCTX pCtx, uint64_t fWhat, bool fCanUpdateCr3)
1929{
1930 HV_INPUT_GET_VP_REGISTERS *pInput = (HV_INPUT_GET_VP_REGISTERS *)pGVCpu->nemr0.s.HypercallData.pbPage;
1931 AssertPtrReturn(pInput, VERR_INTERNAL_ERROR_3);
1932 AssertReturn(g_pfnHvlInvokeHypercall, VERR_NEM_MISSING_KERNEL_API_1);
1933 Assert(pCtx == &pGVCpu->cpum.GstCtx);
1934
1935 fWhat &= pCtx->fExtrn;
1936
1937 pInput->PartitionId = pGVM->nemr0.s.idHvPartition;
1938 pInput->VpIndex = pGVCpu->idCpu;
1939 pInput->fFlags = 0;
1940
1941 /* GPRs */
1942 uintptr_t iReg = 0;
1943 if (fWhat & CPUMCTX_EXTRN_GPRS_MASK)
1944 {
1945 if (fWhat & CPUMCTX_EXTRN_RAX)
1946 pInput->Names[iReg++] = HvX64RegisterRax;
1947 if (fWhat & CPUMCTX_EXTRN_RCX)
1948 pInput->Names[iReg++] = HvX64RegisterRcx;
1949 if (fWhat & CPUMCTX_EXTRN_RDX)
1950 pInput->Names[iReg++] = HvX64RegisterRdx;
1951 if (fWhat & CPUMCTX_EXTRN_RBX)
1952 pInput->Names[iReg++] = HvX64RegisterRbx;
1953 if (fWhat & CPUMCTX_EXTRN_RSP)
1954 pInput->Names[iReg++] = HvX64RegisterRsp;
1955 if (fWhat & CPUMCTX_EXTRN_RBP)
1956 pInput->Names[iReg++] = HvX64RegisterRbp;
1957 if (fWhat & CPUMCTX_EXTRN_RSI)
1958 pInput->Names[iReg++] = HvX64RegisterRsi;
1959 if (fWhat & CPUMCTX_EXTRN_RDI)
1960 pInput->Names[iReg++] = HvX64RegisterRdi;
1961 if (fWhat & CPUMCTX_EXTRN_R8_R15)
1962 {
1963 pInput->Names[iReg++] = HvX64RegisterR8;
1964 pInput->Names[iReg++] = HvX64RegisterR9;
1965 pInput->Names[iReg++] = HvX64RegisterR10;
1966 pInput->Names[iReg++] = HvX64RegisterR11;
1967 pInput->Names[iReg++] = HvX64RegisterR12;
1968 pInput->Names[iReg++] = HvX64RegisterR13;
1969 pInput->Names[iReg++] = HvX64RegisterR14;
1970 pInput->Names[iReg++] = HvX64RegisterR15;
1971 }
1972 }
1973
1974 /* RIP & Flags */
1975 if (fWhat & CPUMCTX_EXTRN_RIP)
1976 pInput->Names[iReg++] = HvX64RegisterRip;
1977 if (fWhat & CPUMCTX_EXTRN_RFLAGS)
1978 pInput->Names[iReg++] = HvX64RegisterRflags;
1979
1980 /* Segments */
1981 if (fWhat & CPUMCTX_EXTRN_SREG_MASK)
1982 {
1983 if (fWhat & CPUMCTX_EXTRN_CS)
1984 pInput->Names[iReg++] = HvX64RegisterCs;
1985 if (fWhat & CPUMCTX_EXTRN_ES)
1986 pInput->Names[iReg++] = HvX64RegisterEs;
1987 if (fWhat & CPUMCTX_EXTRN_SS)
1988 pInput->Names[iReg++] = HvX64RegisterSs;
1989 if (fWhat & CPUMCTX_EXTRN_DS)
1990 pInput->Names[iReg++] = HvX64RegisterDs;
1991 if (fWhat & CPUMCTX_EXTRN_FS)
1992 pInput->Names[iReg++] = HvX64RegisterFs;
1993 if (fWhat & CPUMCTX_EXTRN_GS)
1994 pInput->Names[iReg++] = HvX64RegisterGs;
1995 }
1996
1997 /* Descriptor tables and the task segment. */
1998 if (fWhat & CPUMCTX_EXTRN_TABLE_MASK)
1999 {
2000 if (fWhat & CPUMCTX_EXTRN_LDTR)
2001 pInput->Names[iReg++] = HvX64RegisterLdtr;
2002 if (fWhat & CPUMCTX_EXTRN_TR)
2003 pInput->Names[iReg++] = HvX64RegisterTr;
2004 if (fWhat & CPUMCTX_EXTRN_IDTR)
2005 pInput->Names[iReg++] = HvX64RegisterIdtr;
2006 if (fWhat & CPUMCTX_EXTRN_GDTR)
2007 pInput->Names[iReg++] = HvX64RegisterGdtr;
2008 }
2009
2010 /* Control registers. */
2011 if (fWhat & CPUMCTX_EXTRN_CR_MASK)
2012 {
2013 if (fWhat & CPUMCTX_EXTRN_CR0)
2014 pInput->Names[iReg++] = HvX64RegisterCr0;
2015 if (fWhat & CPUMCTX_EXTRN_CR2)
2016 pInput->Names[iReg++] = HvX64RegisterCr2;
2017 if (fWhat & CPUMCTX_EXTRN_CR3)
2018 pInput->Names[iReg++] = HvX64RegisterCr3;
2019 if (fWhat & CPUMCTX_EXTRN_CR4)
2020 pInput->Names[iReg++] = HvX64RegisterCr4;
2021 }
2022 if (fWhat & CPUMCTX_EXTRN_APIC_TPR)
2023 pInput->Names[iReg++] = HvX64RegisterCr8;
2024
2025 /* Debug registers. */
2026 if (fWhat & CPUMCTX_EXTRN_DR7)
2027 pInput->Names[iReg++] = HvX64RegisterDr7;
2028 if (fWhat & CPUMCTX_EXTRN_DR0_DR3)
2029 {
2030 if (!(fWhat & CPUMCTX_EXTRN_DR7) && (pCtx->fExtrn & CPUMCTX_EXTRN_DR7))
2031 {
2032 fWhat |= CPUMCTX_EXTRN_DR7;
2033 pInput->Names[iReg++] = HvX64RegisterDr7;
2034 }
2035 pInput->Names[iReg++] = HvX64RegisterDr0;
2036 pInput->Names[iReg++] = HvX64RegisterDr1;
2037 pInput->Names[iReg++] = HvX64RegisterDr2;
2038 pInput->Names[iReg++] = HvX64RegisterDr3;
2039 }
2040 if (fWhat & CPUMCTX_EXTRN_DR6)
2041 pInput->Names[iReg++] = HvX64RegisterDr6;
2042
2043 /* Floating point state. */
2044 if (fWhat & CPUMCTX_EXTRN_X87)
2045 {
2046 pInput->Names[iReg++] = HvX64RegisterFpMmx0;
2047 pInput->Names[iReg++] = HvX64RegisterFpMmx1;
2048 pInput->Names[iReg++] = HvX64RegisterFpMmx2;
2049 pInput->Names[iReg++] = HvX64RegisterFpMmx3;
2050 pInput->Names[iReg++] = HvX64RegisterFpMmx4;
2051 pInput->Names[iReg++] = HvX64RegisterFpMmx5;
2052 pInput->Names[iReg++] = HvX64RegisterFpMmx6;
2053 pInput->Names[iReg++] = HvX64RegisterFpMmx7;
2054 pInput->Names[iReg++] = HvX64RegisterFpControlStatus;
2055 }
2056 if (fWhat & (CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX))
2057 pInput->Names[iReg++] = HvX64RegisterXmmControlStatus;
2058
2059 /* Vector state. */
2060 if (fWhat & CPUMCTX_EXTRN_SSE_AVX)
2061 {
2062 pInput->Names[iReg++] = HvX64RegisterXmm0;
2063 pInput->Names[iReg++] = HvX64RegisterXmm1;
2064 pInput->Names[iReg++] = HvX64RegisterXmm2;
2065 pInput->Names[iReg++] = HvX64RegisterXmm3;
2066 pInput->Names[iReg++] = HvX64RegisterXmm4;
2067 pInput->Names[iReg++] = HvX64RegisterXmm5;
2068 pInput->Names[iReg++] = HvX64RegisterXmm6;
2069 pInput->Names[iReg++] = HvX64RegisterXmm7;
2070 pInput->Names[iReg++] = HvX64RegisterXmm8;
2071 pInput->Names[iReg++] = HvX64RegisterXmm9;
2072 pInput->Names[iReg++] = HvX64RegisterXmm10;
2073 pInput->Names[iReg++] = HvX64RegisterXmm11;
2074 pInput->Names[iReg++] = HvX64RegisterXmm12;
2075 pInput->Names[iReg++] = HvX64RegisterXmm13;
2076 pInput->Names[iReg++] = HvX64RegisterXmm14;
2077 pInput->Names[iReg++] = HvX64RegisterXmm15;
2078 }
2079
2080 /* MSRs */
2081 // HvX64RegisterTsc - don't touch
2082 if (fWhat & CPUMCTX_EXTRN_EFER)
2083 pInput->Names[iReg++] = HvX64RegisterEfer;
2084 if (fWhat & CPUMCTX_EXTRN_KERNEL_GS_BASE)
2085 pInput->Names[iReg++] = HvX64RegisterKernelGsBase;
2086 if (fWhat & CPUMCTX_EXTRN_SYSENTER_MSRS)
2087 {
2088 pInput->Names[iReg++] = HvX64RegisterSysenterCs;
2089 pInput->Names[iReg++] = HvX64RegisterSysenterEip;
2090 pInput->Names[iReg++] = HvX64RegisterSysenterEsp;
2091 }
2092 if (fWhat & CPUMCTX_EXTRN_SYSCALL_MSRS)
2093 {
2094 pInput->Names[iReg++] = HvX64RegisterStar;
2095 pInput->Names[iReg++] = HvX64RegisterLstar;
2096 pInput->Names[iReg++] = HvX64RegisterCstar;
2097 pInput->Names[iReg++] = HvX64RegisterSfmask;
2098 }
2099
2100# ifdef LOG_ENABLED
2101 const CPUMCPUVENDOR enmCpuVendor = CPUMGetHostCpuVendor(pGVM);
2102# endif
2103 if (fWhat & CPUMCTX_EXTRN_OTHER_MSRS)
2104 {
2105 pInput->Names[iReg++] = HvX64RegisterApicBase; /// @todo APIC BASE
2106 pInput->Names[iReg++] = HvX64RegisterPat;
2107# if 0 /*def LOG_ENABLED*/ /** @todo something's wrong with HvX64RegisterMtrrCap? (AMD) */
2108 pInput->Names[iReg++] = HvX64RegisterMtrrCap;
2109# endif
2110 pInput->Names[iReg++] = HvX64RegisterMtrrDefType;
2111 pInput->Names[iReg++] = HvX64RegisterMtrrFix64k00000;
2112 pInput->Names[iReg++] = HvX64RegisterMtrrFix16k80000;
2113 pInput->Names[iReg++] = HvX64RegisterMtrrFix16kA0000;
2114 pInput->Names[iReg++] = HvX64RegisterMtrrFix4kC0000;
2115 pInput->Names[iReg++] = HvX64RegisterMtrrFix4kC8000;
2116 pInput->Names[iReg++] = HvX64RegisterMtrrFix4kD0000;
2117 pInput->Names[iReg++] = HvX64RegisterMtrrFix4kD8000;
2118 pInput->Names[iReg++] = HvX64RegisterMtrrFix4kE0000;
2119 pInput->Names[iReg++] = HvX64RegisterMtrrFix4kE8000;
2120 pInput->Names[iReg++] = HvX64RegisterMtrrFix4kF0000;
2121 pInput->Names[iReg++] = HvX64RegisterMtrrFix4kF8000;
2122 pInput->Names[iReg++] = HvX64RegisterTscAux;
2123# if 0 /** @todo why can't we read HvX64RegisterIa32MiscEnable? */
2124 if (enmCpuVendor != CPUMCPUVENDOR_AMD)
2125 pInput->Names[iReg++] = HvX64RegisterIa32MiscEnable;
2126# endif
2127# ifdef LOG_ENABLED
2128 if (enmCpuVendor != CPUMCPUVENDOR_AMD && enmCpuVendor != CPUMCPUVENDOR_HYGON)
2129 pInput->Names[iReg++] = HvX64RegisterIa32FeatureControl;
2130# endif
2131 }
2132
2133 /* Interruptibility. */
2134 if (fWhat & (CPUMCTX_EXTRN_NEM_WIN_INHIBIT_INT | CPUMCTX_EXTRN_NEM_WIN_INHIBIT_NMI))
2135 {
2136 pInput->Names[iReg++] = HvRegisterInterruptState;
2137 pInput->Names[iReg++] = HvX64RegisterRip;
2138 }
2139
2140 /* event injection */
2141 pInput->Names[iReg++] = HvRegisterPendingInterruption;
2142 pInput->Names[iReg++] = HvRegisterPendingEvent0;
2143 pInput->Names[iReg++] = HvRegisterPendingEvent1;
2144 size_t const cRegs = iReg;
2145 size_t const cbInput = RT_ALIGN_Z(RT_UOFFSETOF_DYN(HV_INPUT_GET_VP_REGISTERS, Names[cRegs]), 32);
2146
2147 HV_REGISTER_VALUE *paValues = (HV_REGISTER_VALUE *)((uint8_t *)pInput + cbInput);
2148 Assert((uintptr_t)&paValues[cRegs] - (uintptr_t)pGVCpu->nemr0.s.HypercallData.pbPage < PAGE_SIZE); /* (max is around 168 registers) */
2149 RT_BZERO(paValues, cRegs * sizeof(paValues[0]));
2150
2151 /*
2152 * Make the hypercall.
2153 */
2154 uint64_t uResult = g_pfnHvlInvokeHypercall(HV_MAKE_CALL_INFO(HvCallGetVpRegisters, cRegs),
2155 pGVCpu->nemr0.s.HypercallData.HCPhysPage,
2156 pGVCpu->nemr0.s.HypercallData.HCPhysPage + cbInput);
2157 AssertLogRelMsgReturn(uResult == HV_MAKE_CALL_REP_RET(cRegs),
2158 ("uResult=%RX64 cRegs=%#x\n", uResult, cRegs),
2159 VERR_NEM_GET_REGISTERS_FAILED);
2160 //LogFlow(("nemR0WinImportState: uResult=%#RX64 iReg=%zu fWhat=%#018RX64 fExtr=%#018RX64\n", uResult, cRegs, fWhat, pCtx->fExtrn));
2161
2162 /*
2163 * Copy information to the CPUM context.
2164 */
2165 iReg = 0;
2166
2167 /* GPRs */
2168 if (fWhat & CPUMCTX_EXTRN_GPRS_MASK)
2169 {
2170 if (fWhat & CPUMCTX_EXTRN_RAX)
2171 {
2172 Assert(pInput->Names[iReg] == HvX64RegisterRax);
2173 pCtx->rax = paValues[iReg++].Reg64;
2174 }
2175 if (fWhat & CPUMCTX_EXTRN_RCX)
2176 {
2177 Assert(pInput->Names[iReg] == HvX64RegisterRcx);
2178 pCtx->rcx = paValues[iReg++].Reg64;
2179 }
2180 if (fWhat & CPUMCTX_EXTRN_RDX)
2181 {
2182 Assert(pInput->Names[iReg] == HvX64RegisterRdx);
2183 pCtx->rdx = paValues[iReg++].Reg64;
2184 }
2185 if (fWhat & CPUMCTX_EXTRN_RBX)
2186 {
2187 Assert(pInput->Names[iReg] == HvX64RegisterRbx);
2188 pCtx->rbx = paValues[iReg++].Reg64;
2189 }
2190 if (fWhat & CPUMCTX_EXTRN_RSP)
2191 {
2192 Assert(pInput->Names[iReg] == HvX64RegisterRsp);
2193 pCtx->rsp = paValues[iReg++].Reg64;
2194 }
2195 if (fWhat & CPUMCTX_EXTRN_RBP)
2196 {
2197 Assert(pInput->Names[iReg] == HvX64RegisterRbp);
2198 pCtx->rbp = paValues[iReg++].Reg64;
2199 }
2200 if (fWhat & CPUMCTX_EXTRN_RSI)
2201 {
2202 Assert(pInput->Names[iReg] == HvX64RegisterRsi);
2203 pCtx->rsi = paValues[iReg++].Reg64;
2204 }
2205 if (fWhat & CPUMCTX_EXTRN_RDI)
2206 {
2207 Assert(pInput->Names[iReg] == HvX64RegisterRdi);
2208 pCtx->rdi = paValues[iReg++].Reg64;
2209 }
2210 if (fWhat & CPUMCTX_EXTRN_R8_R15)
2211 {
2212 Assert(pInput->Names[iReg] == HvX64RegisterR8);
2213 Assert(pInput->Names[iReg + 7] == HvX64RegisterR15);
2214 pCtx->r8 = paValues[iReg++].Reg64;
2215 pCtx->r9 = paValues[iReg++].Reg64;
2216 pCtx->r10 = paValues[iReg++].Reg64;
2217 pCtx->r11 = paValues[iReg++].Reg64;
2218 pCtx->r12 = paValues[iReg++].Reg64;
2219 pCtx->r13 = paValues[iReg++].Reg64;
2220 pCtx->r14 = paValues[iReg++].Reg64;
2221 pCtx->r15 = paValues[iReg++].Reg64;
2222 }
2223 }
2224
2225 /* RIP & Flags */
2226 if (fWhat & CPUMCTX_EXTRN_RIP)
2227 {
2228 Assert(pInput->Names[iReg] == HvX64RegisterRip);
2229 pCtx->rip = paValues[iReg++].Reg64;
2230 }
2231 if (fWhat & CPUMCTX_EXTRN_RFLAGS)
2232 {
2233 Assert(pInput->Names[iReg] == HvX64RegisterRflags);
2234 pCtx->rflags.u = paValues[iReg++].Reg64;
2235 }
2236
2237 /* Segments */
2238# define COPY_BACK_SEG(a_idx, a_enmName, a_SReg) \
2239 do { \
2240 Assert(pInput->Names[a_idx] == a_enmName); \
2241 (a_SReg).u64Base = paValues[a_idx].Segment.Base; \
2242 (a_SReg).u32Limit = paValues[a_idx].Segment.Limit; \
2243 (a_SReg).ValidSel = (a_SReg).Sel = paValues[a_idx].Segment.Selector; \
2244 (a_SReg).Attr.u = paValues[a_idx].Segment.Attributes; \
2245 (a_SReg).fFlags = CPUMSELREG_FLAGS_VALID; \
2246 } while (0)
2247 if (fWhat & CPUMCTX_EXTRN_SREG_MASK)
2248 {
2249 if (fWhat & CPUMCTX_EXTRN_CS)
2250 {
2251 COPY_BACK_SEG(iReg, HvX64RegisterCs, pCtx->cs);
2252 iReg++;
2253 }
2254 if (fWhat & CPUMCTX_EXTRN_ES)
2255 {
2256 COPY_BACK_SEG(iReg, HvX64RegisterEs, pCtx->es);
2257 iReg++;
2258 }
2259 if (fWhat & CPUMCTX_EXTRN_SS)
2260 {
2261 COPY_BACK_SEG(iReg, HvX64RegisterSs, pCtx->ss);
2262 iReg++;
2263 }
2264 if (fWhat & CPUMCTX_EXTRN_DS)
2265 {
2266 COPY_BACK_SEG(iReg, HvX64RegisterDs, pCtx->ds);
2267 iReg++;
2268 }
2269 if (fWhat & CPUMCTX_EXTRN_FS)
2270 {
2271 COPY_BACK_SEG(iReg, HvX64RegisterFs, pCtx->fs);
2272 iReg++;
2273 }
2274 if (fWhat & CPUMCTX_EXTRN_GS)
2275 {
2276 COPY_BACK_SEG(iReg, HvX64RegisterGs, pCtx->gs);
2277 iReg++;
2278 }
2279 }
2280 /* Descriptor tables and the task segment. */
2281 if (fWhat & CPUMCTX_EXTRN_TABLE_MASK)
2282 {
2283 if (fWhat & CPUMCTX_EXTRN_LDTR)
2284 {
2285 COPY_BACK_SEG(iReg, HvX64RegisterLdtr, pCtx->ldtr);
2286 iReg++;
2287 }
2288 if (fWhat & CPUMCTX_EXTRN_TR)
2289 {
2290 /* AMD-V likes loading TR with in AVAIL state, whereas intel insists on BUSY. So,
2291 avoid to trigger sanity assertions around the code, always fix this. */
2292 COPY_BACK_SEG(iReg, HvX64RegisterTr, pCtx->tr);
2293 switch (pCtx->tr.Attr.n.u4Type)
2294 {
2295 case X86_SEL_TYPE_SYS_386_TSS_BUSY:
2296 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
2297 break;
2298 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
2299 pCtx->tr.Attr.n.u4Type = X86_SEL_TYPE_SYS_386_TSS_BUSY;
2300 break;
2301 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
2302 pCtx->tr.Attr.n.u4Type = X86_SEL_TYPE_SYS_286_TSS_BUSY;
2303 break;
2304 }
2305 iReg++;
2306 }
2307 if (fWhat & CPUMCTX_EXTRN_IDTR)
2308 {
2309 Assert(pInput->Names[iReg] == HvX64RegisterIdtr);
2310 pCtx->idtr.cbIdt = paValues[iReg].Table.Limit;
2311 pCtx->idtr.pIdt = paValues[iReg].Table.Base;
2312 iReg++;
2313 }
2314 if (fWhat & CPUMCTX_EXTRN_GDTR)
2315 {
2316 Assert(pInput->Names[iReg] == HvX64RegisterGdtr);
2317 pCtx->gdtr.cbGdt = paValues[iReg].Table.Limit;
2318 pCtx->gdtr.pGdt = paValues[iReg].Table.Base;
2319 iReg++;
2320 }
2321 }
2322
2323 /* Control registers. */
2324 bool fMaybeChangedMode = false;
2325 bool fUpdateCr3 = false;
2326 if (fWhat & CPUMCTX_EXTRN_CR_MASK)
2327 {
2328 if (fWhat & CPUMCTX_EXTRN_CR0)
2329 {
2330 Assert(pInput->Names[iReg] == HvX64RegisterCr0);
2331 if (pCtx->cr0 != paValues[iReg].Reg64)
2332 {
2333 CPUMSetGuestCR0(pGVCpu, paValues[iReg].Reg64);
2334 fMaybeChangedMode = true;
2335 }
2336 iReg++;
2337 }
2338 if (fWhat & CPUMCTX_EXTRN_CR2)
2339 {
2340 Assert(pInput->Names[iReg] == HvX64RegisterCr2);
2341 pCtx->cr2 = paValues[iReg].Reg64;
2342 iReg++;
2343 }
2344 if (fWhat & CPUMCTX_EXTRN_CR3)
2345 {
2346 Assert(pInput->Names[iReg] == HvX64RegisterCr3);
2347 if (pCtx->cr3 != paValues[iReg].Reg64)
2348 {
2349 CPUMSetGuestCR3(pGVCpu, paValues[iReg].Reg64);
2350 fUpdateCr3 = true;
2351 }
2352 iReg++;
2353 }
2354 if (fWhat & CPUMCTX_EXTRN_CR4)
2355 {
2356 Assert(pInput->Names[iReg] == HvX64RegisterCr4);
2357 if (pCtx->cr4 != paValues[iReg].Reg64)
2358 {
2359 CPUMSetGuestCR4(pGVCpu, paValues[iReg].Reg64);
2360 fMaybeChangedMode = true;
2361 }
2362 iReg++;
2363 }
2364 }
2365 if (fWhat & CPUMCTX_EXTRN_APIC_TPR)
2366 {
2367 Assert(pInput->Names[iReg] == HvX64RegisterCr8);
2368 APICSetTpr(pGVCpu, (uint8_t)paValues[iReg].Reg64 << 4);
2369 iReg++;
2370 }
2371
2372 /* Debug registers. */
2373 if (fWhat & CPUMCTX_EXTRN_DR7)
2374 {
2375 Assert(pInput->Names[iReg] == HvX64RegisterDr7);
2376 if (pCtx->dr[7] != paValues[iReg].Reg64)
2377 CPUMSetGuestDR7(pGVCpu, paValues[iReg].Reg64);
2378 pCtx->fExtrn &= ~CPUMCTX_EXTRN_DR7; /* Hack alert! Avoids asserting when processing CPUMCTX_EXTRN_DR0_DR3. */
2379 iReg++;
2380 }
2381 if (fWhat & CPUMCTX_EXTRN_DR0_DR3)
2382 {
2383 Assert(pInput->Names[iReg] == HvX64RegisterDr0);
2384 Assert(pInput->Names[iReg+3] == HvX64RegisterDr3);
2385 if (pCtx->dr[0] != paValues[iReg].Reg64)
2386 CPUMSetGuestDR0(pGVCpu, paValues[iReg].Reg64);
2387 iReg++;
2388 if (pCtx->dr[1] != paValues[iReg].Reg64)
2389 CPUMSetGuestDR1(pGVCpu, paValues[iReg].Reg64);
2390 iReg++;
2391 if (pCtx->dr[2] != paValues[iReg].Reg64)
2392 CPUMSetGuestDR2(pGVCpu, paValues[iReg].Reg64);
2393 iReg++;
2394 if (pCtx->dr[3] != paValues[iReg].Reg64)
2395 CPUMSetGuestDR3(pGVCpu, paValues[iReg].Reg64);
2396 iReg++;
2397 }
2398 if (fWhat & CPUMCTX_EXTRN_DR6)
2399 {
2400 Assert(pInput->Names[iReg] == HvX64RegisterDr6);
2401 if (pCtx->dr[6] != paValues[iReg].Reg64)
2402 CPUMSetGuestDR6(pGVCpu, paValues[iReg].Reg64);
2403 iReg++;
2404 }
2405
2406 /* Floating point state. */
2407 if (fWhat & CPUMCTX_EXTRN_X87)
2408 {
2409 Assert(pInput->Names[iReg] == HvX64RegisterFpMmx0);
2410 Assert(pInput->Names[iReg + 7] == HvX64RegisterFpMmx7);
2411 pCtx->XState.x87.aRegs[0].au64[0] = paValues[iReg].Fp.AsUINT128.Low64;
2412 pCtx->XState.x87.aRegs[0].au64[1] = paValues[iReg].Fp.AsUINT128.High64;
2413 iReg++;
2414 pCtx->XState.x87.aRegs[1].au64[0] = paValues[iReg].Fp.AsUINT128.Low64;
2415 pCtx->XState.x87.aRegs[1].au64[1] = paValues[iReg].Fp.AsUINT128.High64;
2416 iReg++;
2417 pCtx->XState.x87.aRegs[2].au64[0] = paValues[iReg].Fp.AsUINT128.Low64;
2418 pCtx->XState.x87.aRegs[2].au64[1] = paValues[iReg].Fp.AsUINT128.High64;
2419 iReg++;
2420 pCtx->XState.x87.aRegs[3].au64[0] = paValues[iReg].Fp.AsUINT128.Low64;
2421 pCtx->XState.x87.aRegs[3].au64[1] = paValues[iReg].Fp.AsUINT128.High64;
2422 iReg++;
2423 pCtx->XState.x87.aRegs[4].au64[0] = paValues[iReg].Fp.AsUINT128.Low64;
2424 pCtx->XState.x87.aRegs[4].au64[1] = paValues[iReg].Fp.AsUINT128.High64;
2425 iReg++;
2426 pCtx->XState.x87.aRegs[5].au64[0] = paValues[iReg].Fp.AsUINT128.Low64;
2427 pCtx->XState.x87.aRegs[5].au64[1] = paValues[iReg].Fp.AsUINT128.High64;
2428 iReg++;
2429 pCtx->XState.x87.aRegs[6].au64[0] = paValues[iReg].Fp.AsUINT128.Low64;
2430 pCtx->XState.x87.aRegs[6].au64[1] = paValues[iReg].Fp.AsUINT128.High64;
2431 iReg++;
2432 pCtx->XState.x87.aRegs[7].au64[0] = paValues[iReg].Fp.AsUINT128.Low64;
2433 pCtx->XState.x87.aRegs[7].au64[1] = paValues[iReg].Fp.AsUINT128.High64;
2434 iReg++;
2435
2436 Assert(pInput->Names[iReg] == HvX64RegisterFpControlStatus);
2437 pCtx->XState.x87.FCW = paValues[iReg].FpControlStatus.FpControl;
2438 pCtx->XState.x87.FSW = paValues[iReg].FpControlStatus.FpStatus;
2439 pCtx->XState.x87.FTW = paValues[iReg].FpControlStatus.FpTag
2440 /*| (paValues[iReg].FpControlStatus.Reserved << 8)*/;
2441 pCtx->XState.x87.FOP = paValues[iReg].FpControlStatus.LastFpOp;
2442 pCtx->XState.x87.FPUIP = (uint32_t)paValues[iReg].FpControlStatus.LastFpRip;
2443 pCtx->XState.x87.CS = (uint16_t)(paValues[iReg].FpControlStatus.LastFpRip >> 32);
2444 pCtx->XState.x87.Rsrvd1 = (uint16_t)(paValues[iReg].FpControlStatus.LastFpRip >> 48);
2445 iReg++;
2446 }
2447
2448 if (fWhat & (CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX))
2449 {
2450 Assert(pInput->Names[iReg] == HvX64RegisterXmmControlStatus);
2451 if (fWhat & CPUMCTX_EXTRN_X87)
2452 {
2453 pCtx->XState.x87.FPUDP = (uint32_t)paValues[iReg].XmmControlStatus.LastFpRdp;
2454 pCtx->XState.x87.DS = (uint16_t)(paValues[iReg].XmmControlStatus.LastFpRdp >> 32);
2455 pCtx->XState.x87.Rsrvd2 = (uint16_t)(paValues[iReg].XmmControlStatus.LastFpRdp >> 48);
2456 }
2457 pCtx->XState.x87.MXCSR = paValues[iReg].XmmControlStatus.XmmStatusControl;
2458 pCtx->XState.x87.MXCSR_MASK = paValues[iReg].XmmControlStatus.XmmStatusControlMask; /** @todo ??? (Isn't this an output field?) */
2459 iReg++;
2460 }
2461
2462 /* Vector state. */
2463 if (fWhat & CPUMCTX_EXTRN_SSE_AVX)
2464 {
2465 Assert(pInput->Names[iReg] == HvX64RegisterXmm0);
2466 Assert(pInput->Names[iReg+15] == HvX64RegisterXmm15);
2467 pCtx->XState.x87.aXMM[0].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
2468 pCtx->XState.x87.aXMM[0].uXmm.s.Hi = paValues[iReg].Reg128.High64;
2469 iReg++;
2470 pCtx->XState.x87.aXMM[1].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
2471 pCtx->XState.x87.aXMM[1].uXmm.s.Hi = paValues[iReg].Reg128.High64;
2472 iReg++;
2473 pCtx->XState.x87.aXMM[2].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
2474 pCtx->XState.x87.aXMM[2].uXmm.s.Hi = paValues[iReg].Reg128.High64;
2475 iReg++;
2476 pCtx->XState.x87.aXMM[3].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
2477 pCtx->XState.x87.aXMM[3].uXmm.s.Hi = paValues[iReg].Reg128.High64;
2478 iReg++;
2479 pCtx->XState.x87.aXMM[4].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
2480 pCtx->XState.x87.aXMM[4].uXmm.s.Hi = paValues[iReg].Reg128.High64;
2481 iReg++;
2482 pCtx->XState.x87.aXMM[5].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
2483 pCtx->XState.x87.aXMM[5].uXmm.s.Hi = paValues[iReg].Reg128.High64;
2484 iReg++;
2485 pCtx->XState.x87.aXMM[6].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
2486 pCtx->XState.x87.aXMM[6].uXmm.s.Hi = paValues[iReg].Reg128.High64;
2487 iReg++;
2488 pCtx->XState.x87.aXMM[7].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
2489 pCtx->XState.x87.aXMM[7].uXmm.s.Hi = paValues[iReg].Reg128.High64;
2490 iReg++;
2491 pCtx->XState.x87.aXMM[8].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
2492 pCtx->XState.x87.aXMM[8].uXmm.s.Hi = paValues[iReg].Reg128.High64;
2493 iReg++;
2494 pCtx->XState.x87.aXMM[9].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
2495 pCtx->XState.x87.aXMM[9].uXmm.s.Hi = paValues[iReg].Reg128.High64;
2496 iReg++;
2497 pCtx->XState.x87.aXMM[10].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
2498 pCtx->XState.x87.aXMM[10].uXmm.s.Hi = paValues[iReg].Reg128.High64;
2499 iReg++;
2500 pCtx->XState.x87.aXMM[11].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
2501 pCtx->XState.x87.aXMM[11].uXmm.s.Hi = paValues[iReg].Reg128.High64;
2502 iReg++;
2503 pCtx->XState.x87.aXMM[12].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
2504 pCtx->XState.x87.aXMM[12].uXmm.s.Hi = paValues[iReg].Reg128.High64;
2505 iReg++;
2506 pCtx->XState.x87.aXMM[13].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
2507 pCtx->XState.x87.aXMM[13].uXmm.s.Hi = paValues[iReg].Reg128.High64;
2508 iReg++;
2509 pCtx->XState.x87.aXMM[14].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
2510 pCtx->XState.x87.aXMM[14].uXmm.s.Hi = paValues[iReg].Reg128.High64;
2511 iReg++;
2512 pCtx->XState.x87.aXMM[15].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
2513 pCtx->XState.x87.aXMM[15].uXmm.s.Hi = paValues[iReg].Reg128.High64;
2514 iReg++;
2515 }
2516
2517
2518 /* MSRs */
2519 // HvX64RegisterTsc - don't touch
2520 if (fWhat & CPUMCTX_EXTRN_EFER)
2521 {
2522 Assert(pInput->Names[iReg] == HvX64RegisterEfer);
2523 if (paValues[iReg].Reg64 != pCtx->msrEFER)
2524 {
2525 Log7(("NEM/%u: MSR EFER changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtx->msrEFER, paValues[iReg].Reg64));
2526 if ((paValues[iReg].Reg64 ^ pCtx->msrEFER) & MSR_K6_EFER_NXE)
2527 PGMNotifyNxeChanged(pGVCpu, RT_BOOL(paValues[iReg].Reg64 & MSR_K6_EFER_NXE));
2528 pCtx->msrEFER = paValues[iReg].Reg64;
2529 fMaybeChangedMode = true;
2530 }
2531 iReg++;
2532 }
2533 if (fWhat & CPUMCTX_EXTRN_KERNEL_GS_BASE)
2534 {
2535 Assert(pInput->Names[iReg] == HvX64RegisterKernelGsBase);
2536 if (pCtx->msrKERNELGSBASE != paValues[iReg].Reg64)
2537 Log7(("NEM/%u: MSR KERNELGSBASE changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtx->msrKERNELGSBASE, paValues[iReg].Reg64));
2538 pCtx->msrKERNELGSBASE = paValues[iReg].Reg64;
2539 iReg++;
2540 }
2541 if (fWhat & CPUMCTX_EXTRN_SYSENTER_MSRS)
2542 {
2543 Assert(pInput->Names[iReg] == HvX64RegisterSysenterCs);
2544 if (pCtx->SysEnter.cs != paValues[iReg].Reg64)
2545 Log7(("NEM/%u: MSR SYSENTER.CS changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtx->SysEnter.cs, paValues[iReg].Reg64));
2546 pCtx->SysEnter.cs = paValues[iReg].Reg64;
2547 iReg++;
2548
2549 Assert(pInput->Names[iReg] == HvX64RegisterSysenterEip);
2550 if (pCtx->SysEnter.eip != paValues[iReg].Reg64)
2551 Log7(("NEM/%u: MSR SYSENTER.EIP changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtx->SysEnter.eip, paValues[iReg].Reg64));
2552 pCtx->SysEnter.eip = paValues[iReg].Reg64;
2553 iReg++;
2554
2555 Assert(pInput->Names[iReg] == HvX64RegisterSysenterEsp);
2556 if (pCtx->SysEnter.esp != paValues[iReg].Reg64)
2557 Log7(("NEM/%u: MSR SYSENTER.ESP changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtx->SysEnter.esp, paValues[iReg].Reg64));
2558 pCtx->SysEnter.esp = paValues[iReg].Reg64;
2559 iReg++;
2560 }
2561 if (fWhat & CPUMCTX_EXTRN_SYSCALL_MSRS)
2562 {
2563 Assert(pInput->Names[iReg] == HvX64RegisterStar);
2564 if (pCtx->msrSTAR != paValues[iReg].Reg64)
2565 Log7(("NEM/%u: MSR STAR changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtx->msrSTAR, paValues[iReg].Reg64));
2566 pCtx->msrSTAR = paValues[iReg].Reg64;
2567 iReg++;
2568
2569 Assert(pInput->Names[iReg] == HvX64RegisterLstar);
2570 if (pCtx->msrLSTAR != paValues[iReg].Reg64)
2571 Log7(("NEM/%u: MSR LSTAR changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtx->msrLSTAR, paValues[iReg].Reg64));
2572 pCtx->msrLSTAR = paValues[iReg].Reg64;
2573 iReg++;
2574
2575 Assert(pInput->Names[iReg] == HvX64RegisterCstar);
2576 if (pCtx->msrCSTAR != paValues[iReg].Reg64)
2577 Log7(("NEM/%u: MSR CSTAR changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtx->msrCSTAR, paValues[iReg].Reg64));
2578 pCtx->msrCSTAR = paValues[iReg].Reg64;
2579 iReg++;
2580
2581 Assert(pInput->Names[iReg] == HvX64RegisterSfmask);
2582 if (pCtx->msrSFMASK != paValues[iReg].Reg64)
2583 Log7(("NEM/%u: MSR SFMASK changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtx->msrSFMASK, paValues[iReg].Reg64));
2584 pCtx->msrSFMASK = paValues[iReg].Reg64;
2585 iReg++;
2586 }
2587 if (fWhat & CPUMCTX_EXTRN_OTHER_MSRS)
2588 {
2589 Assert(pInput->Names[iReg] == HvX64RegisterApicBase);
2590 const uint64_t uOldBase = APICGetBaseMsrNoCheck(pGVCpu);
2591 if (paValues[iReg].Reg64 != uOldBase)
2592 {
2593 Log7(("NEM/%u: MSR APICBase changed %RX64 -> %RX64 (%RX64)\n",
2594 pGVCpu->idCpu, uOldBase, paValues[iReg].Reg64, paValues[iReg].Reg64 ^ uOldBase));
2595 int rc2 = APICSetBaseMsr(pGVCpu, paValues[iReg].Reg64);
2596 AssertLogRelMsg(rc2 == VINF_SUCCESS, ("rc2=%Rrc [%#RX64]\n", rc2, paValues[iReg].Reg64));
2597 }
2598 iReg++;
2599
2600 Assert(pInput->Names[iReg] == HvX64RegisterPat);
2601 if (pCtx->msrPAT != paValues[iReg].Reg64)
2602 Log7(("NEM/%u: MSR PAT changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtx->msrPAT, paValues[iReg].Reg64));
2603 pCtx->msrPAT = paValues[iReg].Reg64;
2604 iReg++;
2605
2606# if 0 /*def LOG_ENABLED*/ /** @todo something's wrong with HvX64RegisterMtrrCap? (AMD) */
2607 Assert(pInput->Names[iReg] == HvX64RegisterMtrrCap);
2608 if (paValues[iReg].Reg64 != CPUMGetGuestIa32MtrrCap(pGVCpu))
2609 Log7(("NEM/%u: MSR MTRR_CAP changed %RX64 -> %RX64 (!!)\n", pGVCpu->idCpu, CPUMGetGuestIa32MtrrCap(pGVCpu), paValues[iReg].Reg64));
2610 iReg++;
2611# endif
2612
2613 PCPUMCTXMSRS pCtxMsrs = CPUMQueryGuestCtxMsrsPtr(pGVCpu);
2614 Assert(pInput->Names[iReg] == HvX64RegisterMtrrDefType);
2615 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrDefType )
2616 Log7(("NEM/%u: MSR MTRR_DEF_TYPE changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtxMsrs->msr.MtrrDefType, paValues[iReg].Reg64));
2617 pCtxMsrs->msr.MtrrDefType = paValues[iReg].Reg64;
2618 iReg++;
2619
2620 /** @todo we dont keep state for HvX64RegisterMtrrPhysBaseX and HvX64RegisterMtrrPhysMaskX */
2621
2622 Assert(pInput->Names[iReg] == HvX64RegisterMtrrFix64k00000);
2623 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrFix64K_00000 )
2624 Log7(("NEM/%u: MSR MTRR_FIX16K_00000 changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtxMsrs->msr.MtrrFix64K_00000, paValues[iReg].Reg64));
2625 pCtxMsrs->msr.MtrrFix64K_00000 = paValues[iReg].Reg64;
2626 iReg++;
2627
2628 Assert(pInput->Names[iReg] == HvX64RegisterMtrrFix16k80000);
2629 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrFix16K_80000 )
2630 Log7(("NEM/%u: MSR MTRR_FIX16K_80000 changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtxMsrs->msr.MtrrFix16K_80000, paValues[iReg].Reg64));
2631 pCtxMsrs->msr.MtrrFix16K_80000 = paValues[iReg].Reg64;
2632 iReg++;
2633
2634 Assert(pInput->Names[iReg] == HvX64RegisterMtrrFix16kA0000);
2635 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrFix16K_A0000 )
2636 Log7(("NEM/%u: MSR MTRR_FIX16K_A0000 changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtxMsrs->msr.MtrrFix16K_A0000, paValues[iReg].Reg64));
2637 pCtxMsrs->msr.MtrrFix16K_A0000 = paValues[iReg].Reg64;
2638 iReg++;
2639
2640 Assert(pInput->Names[iReg] == HvX64RegisterMtrrFix4kC0000);
2641 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrFix4K_C0000 )
2642 Log7(("NEM/%u: MSR MTRR_FIX16K_C0000 changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtxMsrs->msr.MtrrFix4K_C0000, paValues[iReg].Reg64));
2643 pCtxMsrs->msr.MtrrFix4K_C0000 = paValues[iReg].Reg64;
2644 iReg++;
2645
2646 Assert(pInput->Names[iReg] == HvX64RegisterMtrrFix4kC8000);
2647 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrFix4K_C8000 )
2648 Log7(("NEM/%u: MSR MTRR_FIX16K_C8000 changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtxMsrs->msr.MtrrFix4K_C8000, paValues[iReg].Reg64));
2649 pCtxMsrs->msr.MtrrFix4K_C8000 = paValues[iReg].Reg64;
2650 iReg++;
2651
2652 Assert(pInput->Names[iReg] == HvX64RegisterMtrrFix4kD0000);
2653 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrFix4K_D0000 )
2654 Log7(("NEM/%u: MSR MTRR_FIX16K_D0000 changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtxMsrs->msr.MtrrFix4K_D0000, paValues[iReg].Reg64));
2655 pCtxMsrs->msr.MtrrFix4K_D0000 = paValues[iReg].Reg64;
2656 iReg++;
2657
2658 Assert(pInput->Names[iReg] == HvX64RegisterMtrrFix4kD8000);
2659 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrFix4K_D8000 )
2660 Log7(("NEM/%u: MSR MTRR_FIX16K_D8000 changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtxMsrs->msr.MtrrFix4K_D8000, paValues[iReg].Reg64));
2661 pCtxMsrs->msr.MtrrFix4K_D8000 = paValues[iReg].Reg64;
2662 iReg++;
2663
2664 Assert(pInput->Names[iReg] == HvX64RegisterMtrrFix4kE0000);
2665 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrFix4K_E0000 )
2666 Log7(("NEM/%u: MSR MTRR_FIX16K_E0000 changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtxMsrs->msr.MtrrFix4K_E0000, paValues[iReg].Reg64));
2667 pCtxMsrs->msr.MtrrFix4K_E0000 = paValues[iReg].Reg64;
2668 iReg++;
2669
2670 Assert(pInput->Names[iReg] == HvX64RegisterMtrrFix4kE8000);
2671 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrFix4K_E8000 )
2672 Log7(("NEM/%u: MSR MTRR_FIX16K_E8000 changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtxMsrs->msr.MtrrFix4K_E8000, paValues[iReg].Reg64));
2673 pCtxMsrs->msr.MtrrFix4K_E8000 = paValues[iReg].Reg64;
2674 iReg++;
2675
2676 Assert(pInput->Names[iReg] == HvX64RegisterMtrrFix4kF0000);
2677 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrFix4K_F0000 )
2678 Log7(("NEM/%u: MSR MTRR_FIX16K_F0000 changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtxMsrs->msr.MtrrFix4K_F0000, paValues[iReg].Reg64));
2679 pCtxMsrs->msr.MtrrFix4K_F0000 = paValues[iReg].Reg64;
2680 iReg++;
2681
2682 Assert(pInput->Names[iReg] == HvX64RegisterMtrrFix4kF8000);
2683 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrFix4K_F8000 )
2684 Log7(("NEM/%u: MSR MTRR_FIX16K_F8000 changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtxMsrs->msr.MtrrFix4K_F8000, paValues[iReg].Reg64));
2685 pCtxMsrs->msr.MtrrFix4K_F8000 = paValues[iReg].Reg64;
2686 iReg++;
2687
2688 Assert(pInput->Names[iReg] == HvX64RegisterTscAux);
2689 if (paValues[iReg].Reg64 != pCtxMsrs->msr.TscAux )
2690 Log7(("NEM/%u: MSR TSC_AUX changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtxMsrs->msr.TscAux, paValues[iReg].Reg64));
2691 pCtxMsrs->msr.TscAux = paValues[iReg].Reg64;
2692 iReg++;
2693
2694# if 0 /** @todo why can't we even read HvX64RegisterIa32MiscEnable? */
2695 if (enmCpuVendor != CPUMCPUVENDOR_AMD)
2696 {
2697 Assert(pInput->Names[iReg] == HvX64RegisterIa32MiscEnable);
2698 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MiscEnable)
2699 Log7(("NEM/%u: MSR MISC_ENABLE changed %RX64 -> %RX64\n", pGVCpu->idCpu, pCtxMsrs->msr.MiscEnable, paValues[iReg].Reg64));
2700 pCtxMsrs->msr.MiscEnable = paValues[iReg].Reg64;
2701 iReg++;
2702 }
2703# endif
2704# ifdef LOG_ENABLED
2705 if (enmCpuVendor != CPUMCPUVENDOR_AMD && enmCpuVendor != CPUMCPUVENDOR_HYGON)
2706 {
2707 Assert(pInput->Names[iReg] == HvX64RegisterIa32FeatureControl);
2708 uint64_t const uFeatCtrl = CPUMGetGuestIa32FeatCtrl(pVCpu);
2709 if (paValues[iReg].Reg64 != uFeatCtrl)
2710 Log7(("NEM/%u: MSR FEATURE_CONTROL changed %RX64 -> %RX64 (!!)\n", pGVCpu->idCpu, uFeatCtrl, paValues[iReg].Reg64));
2711 iReg++;
2712 }
2713# endif
2714 }
2715
2716 /* Interruptibility. */
2717 if (fWhat & (CPUMCTX_EXTRN_NEM_WIN_INHIBIT_INT | CPUMCTX_EXTRN_NEM_WIN_INHIBIT_NMI))
2718 {
2719 Assert(pInput->Names[iReg] == HvRegisterInterruptState);
2720 Assert(pInput->Names[iReg + 1] == HvX64RegisterRip);
2721
2722 if (!(pCtx->fExtrn & CPUMCTX_EXTRN_NEM_WIN_INHIBIT_INT))
2723 {
2724 pGVCpu->nem.s.fLastInterruptShadow = paValues[iReg].InterruptState.InterruptShadow;
2725 if (paValues[iReg].InterruptState.InterruptShadow)
2726 EMSetInhibitInterruptsPC(pGVCpu, paValues[iReg + 1].Reg64);
2727 else
2728 VMCPU_FF_CLEAR(pGVCpu, VMCPU_FF_INHIBIT_INTERRUPTS);
2729 }
2730
2731 if (!(pCtx->fExtrn & CPUMCTX_EXTRN_NEM_WIN_INHIBIT_NMI))
2732 {
2733 if (paValues[iReg].InterruptState.NmiMasked)
2734 VMCPU_FF_SET(pGVCpu, VMCPU_FF_BLOCK_NMIS);
2735 else
2736 VMCPU_FF_CLEAR(pGVCpu, VMCPU_FF_BLOCK_NMIS);
2737 }
2738
2739 fWhat |= CPUMCTX_EXTRN_NEM_WIN_INHIBIT_INT | CPUMCTX_EXTRN_NEM_WIN_INHIBIT_NMI;
2740 iReg += 2;
2741 }
2742
2743 /* Event injection. */
2744 /// @todo HvRegisterPendingInterruption
2745 Assert(pInput->Names[iReg] == HvRegisterPendingInterruption);
2746 if (paValues[iReg].PendingInterruption.InterruptionPending)
2747 {
2748 Log7(("PendingInterruption: type=%u vector=%#x errcd=%RTbool/%#x instr-len=%u nested=%u\n",
2749 paValues[iReg].PendingInterruption.InterruptionType, paValues[iReg].PendingInterruption.InterruptionVector,
2750 paValues[iReg].PendingInterruption.DeliverErrorCode, paValues[iReg].PendingInterruption.ErrorCode,
2751 paValues[iReg].PendingInterruption.InstructionLength, paValues[iReg].PendingInterruption.NestedEvent));
2752 AssertMsg((paValues[iReg].PendingInterruption.AsUINT64 & UINT64_C(0xfc00)) == 0,
2753 ("%#RX64\n", paValues[iReg].PendingInterruption.AsUINT64));
2754 }
2755
2756 /// @todo HvRegisterPendingEvent0
2757 /// @todo HvRegisterPendingEvent1
2758
2759 /* Almost done, just update extrn flags and maybe change PGM mode. */
2760 pCtx->fExtrn &= ~fWhat;
2761 if (!(pCtx->fExtrn & (CPUMCTX_EXTRN_ALL | (CPUMCTX_EXTRN_NEM_WIN_MASK & ~CPUMCTX_EXTRN_NEM_WIN_EVENT_INJECT))))
2762 pCtx->fExtrn = 0;
2763
2764 /* Typical. */
2765 if (!fMaybeChangedMode && !fUpdateCr3)
2766 return VINF_SUCCESS;
2767
2768 /*
2769 * Slow.
2770 */
2771 int rc = VINF_SUCCESS;
2772 if (fMaybeChangedMode)
2773 {
2774 rc = PGMChangeMode(pGVCpu, pCtx->cr0, pCtx->cr4, pCtx->msrEFER);
2775 AssertMsgReturn(rc == VINF_SUCCESS, ("rc=%Rrc\n", rc), RT_FAILURE_NP(rc) ? rc : VERR_NEM_IPE_1);
2776 }
2777
2778 if (fUpdateCr3)
2779 {
2780 if (fCanUpdateCr3)
2781 {
2782 LogFlow(("nemR0WinImportState: -> PGMUpdateCR3!\n"));
2783 rc = PGMUpdateCR3(pGVCpu, pCtx->cr3, false /*fPdpesMapped*/);
2784 if (rc == VINF_SUCCESS)
2785 { /* likely */ }
2786 else
2787 AssertMsgFailedReturn(("rc=%Rrc\n", rc), RT_FAILURE_NP(rc) ? rc : VERR_NEM_IPE_2);
2788 }
2789 else
2790 {
2791 LogFlow(("nemR0WinImportState: -> VERR_NEM_FLUSH_TLB!\n"));
2792 rc = VERR_NEM_FLUSH_TLB; /* Calling PGMFlushTLB w/o long jump setup doesn't work, ring-3 does it. */
2793 }
2794 }
2795
2796 return rc;
2797}
2798#endif /* NEM_WIN_WITH_RING0_RUNLOOP || NEM_WIN_USE_HYPERCALLS_FOR_REGISTERS */
2799
2800
2801/**
2802 * Import the state from the native API (back to CPUMCTX).
2803 *
2804 * @returns VBox status code
2805 * @param pGVM The ring-0 VM handle.
2806 * @param idCpu The calling EMT. Necessary for getting the
2807 * hypercall page and arguments.
2808 * @param fWhat What to import, CPUMCTX_EXTRN_XXX. Set
2809 * CPUMCTX_EXTERN_ALL for everything.
2810 */
2811VMMR0_INT_DECL(int) NEMR0ImportState(PGVM pGVM, VMCPUID idCpu, uint64_t fWhat)
2812{
2813#if defined(NEM_WIN_WITH_RING0_RUNLOOP) || defined(NEM_WIN_USE_HYPERCALLS_FOR_REGISTERS)
2814 /*
2815 * Validate the call.
2816 */
2817 int rc = GVMMR0ValidateGVMandEMT(pGVM, idCpu);
2818 if (RT_SUCCESS(rc))
2819 {
2820 PGVMCPU pGVCpu = &pGVM->aCpus[idCpu];
2821 AssertReturn(g_pfnHvlInvokeHypercall, VERR_NEM_MISSING_KERNEL_API_1);
2822
2823 /*
2824 * Call worker.
2825 */
2826 rc = nemR0WinImportState(pGVM, pGVCpu, &pGVCpu->cpum.GstCtx, fWhat, false /*fCanUpdateCr3*/);
2827 }
2828 return rc;
2829#else
2830 RT_NOREF(pGVM, idCpu, fWhat);
2831 return VERR_NOT_IMPLEMENTED;
2832#endif
2833}
2834
2835
2836#if defined(NEM_WIN_WITH_RING0_RUNLOOP) || defined(NEM_WIN_USE_HYPERCALLS_FOR_REGISTERS)
2837/**
2838 * Worker for NEMR0QueryCpuTick and the ring-0 NEMHCQueryCpuTick.
2839 *
2840 * @returns VBox status code.
2841 * @param pGVM The ring-0 VM handle.
2842 * @param pGVCpu The ring-0 VCPU handle.
2843 * @param pcTicks Where to return the current CPU tick count.
2844 * @param pcAux Where to return the hyper-V TSC_AUX value. Optional.
2845 */
2846NEM_TMPL_STATIC int nemR0WinQueryCpuTick(PGVM pGVM, PGVMCPU pGVCpu, uint64_t *pcTicks, uint32_t *pcAux)
2847{
2848 /*
2849 * Hypercall parameters.
2850 */
2851 HV_INPUT_GET_VP_REGISTERS *pInput = (HV_INPUT_GET_VP_REGISTERS *)pGVCpu->nemr0.s.HypercallData.pbPage;
2852 AssertPtrReturn(pInput, VERR_INTERNAL_ERROR_3);
2853 AssertReturn(g_pfnHvlInvokeHypercall, VERR_NEM_MISSING_KERNEL_API_1);
2854
2855 pInput->PartitionId = pGVM->nemr0.s.idHvPartition;
2856 pInput->VpIndex = pGVCpu->idCpu;
2857 pInput->fFlags = 0;
2858 pInput->Names[0] = HvX64RegisterTsc;
2859 pInput->Names[1] = HvX64RegisterTscAux;
2860
2861 size_t const cbInput = RT_ALIGN_Z(RT_UOFFSETOF(HV_INPUT_GET_VP_REGISTERS, Names[2]), 32);
2862 HV_REGISTER_VALUE *paValues = (HV_REGISTER_VALUE *)((uint8_t *)pInput + cbInput);
2863 RT_BZERO(paValues, sizeof(paValues[0]) * 2);
2864
2865 /*
2866 * Make the hypercall.
2867 */
2868 uint64_t uResult = g_pfnHvlInvokeHypercall(HV_MAKE_CALL_INFO(HvCallGetVpRegisters, 2),
2869 pGVCpu->nemr0.s.HypercallData.HCPhysPage,
2870 pGVCpu->nemr0.s.HypercallData.HCPhysPage + cbInput);
2871 AssertLogRelMsgReturn(uResult == HV_MAKE_CALL_REP_RET(2), ("uResult=%RX64 cRegs=%#x\n", uResult, 2),
2872 VERR_NEM_GET_REGISTERS_FAILED);
2873
2874 /*
2875 * Get results.
2876 */
2877 *pcTicks = paValues[0].Reg64;
2878 if (pcAux)
2879 *pcAux = paValues[0].Reg32;
2880 return VINF_SUCCESS;
2881}
2882#endif /* NEM_WIN_WITH_RING0_RUNLOOP || NEM_WIN_USE_HYPERCALLS_FOR_REGISTERS */
2883
2884
2885/**
2886 * Queries the TSC and TSC_AUX values, putting the results in .
2887 *
2888 * @returns VBox status code
2889 * @param pGVM The ring-0 VM handle.
2890 * @param idCpu The calling EMT. Necessary for getting the
2891 * hypercall page and arguments.
2892 */
2893VMMR0_INT_DECL(int) NEMR0QueryCpuTick(PGVM pGVM, VMCPUID idCpu)
2894{
2895#if defined(NEM_WIN_WITH_RING0_RUNLOOP) || defined(NEM_WIN_USE_HYPERCALLS_FOR_REGISTERS)
2896 /*
2897 * Validate the call.
2898 */
2899 int rc = GVMMR0ValidateGVMandEMT(pGVM, idCpu);
2900 if (RT_SUCCESS(rc))
2901 {
2902 PGVMCPU pGVCpu = &pGVM->aCpus[idCpu];
2903 AssertReturn(g_pfnHvlInvokeHypercall, VERR_NEM_MISSING_KERNEL_API_1);
2904
2905 /*
2906 * Call worker.
2907 */
2908 pGVCpu->nem.s.Hypercall.QueryCpuTick.cTicks = 0;
2909 pGVCpu->nem.s.Hypercall.QueryCpuTick.uAux = 0;
2910 rc = nemR0WinQueryCpuTick(pGVM, pGVCpu, &pGVCpu->nem.s.Hypercall.QueryCpuTick.cTicks,
2911 &pGVCpu->nem.s.Hypercall.QueryCpuTick.uAux);
2912 }
2913 return rc;
2914#else
2915 RT_NOREF(pGVM, idCpu);
2916 return VERR_NOT_IMPLEMENTED;
2917#endif
2918}
2919
2920
2921#if defined(NEM_WIN_WITH_RING0_RUNLOOP) || defined(NEM_WIN_USE_HYPERCALLS_FOR_REGISTERS)
2922/**
2923 * Worker for NEMR0ResumeCpuTickOnAll and the ring-0 NEMHCResumeCpuTickOnAll.
2924 *
2925 * @returns VBox status code.
2926 * @param pGVM The ring-0 VM handle.
2927 * @param pGVCpu The ring-0 VCPU handle.
2928 * @param uPausedTscValue The TSC value at the time of pausing.
2929 */
2930NEM_TMPL_STATIC int nemR0WinResumeCpuTickOnAll(PGVM pGVM, PGVMCPU pGVCpu, uint64_t uPausedTscValue)
2931{
2932 AssertReturn(g_pfnHvlInvokeHypercall, VERR_NEM_MISSING_KERNEL_API_1);
2933
2934 /*
2935 * Set up the hypercall parameters.
2936 */
2937 HV_INPUT_SET_VP_REGISTERS *pInput = (HV_INPUT_SET_VP_REGISTERS *)pGVCpu->nemr0.s.HypercallData.pbPage;
2938 AssertPtrReturn(pInput, VERR_INTERNAL_ERROR_3);
2939
2940 pInput->PartitionId = pGVM->nemr0.s.idHvPartition;
2941 pInput->VpIndex = 0;
2942 pInput->RsvdZ = 0;
2943 pInput->Elements[0].Name = HvX64RegisterTsc;
2944 pInput->Elements[0].Pad0 = 0;
2945 pInput->Elements[0].Pad1 = 0;
2946 pInput->Elements[0].Value.Reg128.High64 = 0;
2947 pInput->Elements[0].Value.Reg64 = uPausedTscValue;
2948
2949 /*
2950 * Disable interrupts and do the first virtual CPU.
2951 */
2952 RTCCINTREG const fSavedFlags = ASMIntDisableFlags();
2953 uint64_t const uFirstTsc = ASMReadTSC();
2954 uint64_t uResult = g_pfnHvlInvokeHypercall(HV_MAKE_CALL_INFO(HvCallSetVpRegisters, 1),
2955 pGVCpu->nemr0.s.HypercallData.HCPhysPage, 0 /* no output */);
2956 AssertLogRelMsgReturnStmt(uResult == HV_MAKE_CALL_REP_RET(1), ("uResult=%RX64 uTsc=%#RX64\n", uResult, uPausedTscValue),
2957 ASMSetFlags(fSavedFlags), VERR_NEM_SET_TSC);
2958
2959 /*
2960 * Do secondary processors, adjusting for elapsed TSC and keeping finger crossed
2961 * that we don't introduce too much drift here.
2962 */
2963 for (VMCPUID iCpu = 1; iCpu < pGVM->cCpus; iCpu++)
2964 {
2965 Assert(pInput->PartitionId == pGVM->nemr0.s.idHvPartition);
2966 Assert(pInput->RsvdZ == 0);
2967 Assert(pInput->Elements[0].Name == HvX64RegisterTsc);
2968 Assert(pInput->Elements[0].Pad0 == 0);
2969 Assert(pInput->Elements[0].Pad1 == 0);
2970 Assert(pInput->Elements[0].Value.Reg128.High64 == 0);
2971
2972 pInput->VpIndex = iCpu;
2973 const uint64_t offDelta = (ASMReadTSC() - uFirstTsc);
2974 pInput->Elements[0].Value.Reg64 = uPausedTscValue + offDelta;
2975
2976 uResult = g_pfnHvlInvokeHypercall(HV_MAKE_CALL_INFO(HvCallSetVpRegisters, 1),
2977 pGVCpu->nemr0.s.HypercallData.HCPhysPage, 0 /* no output */);
2978 AssertLogRelMsgReturnStmt(uResult == HV_MAKE_CALL_REP_RET(1),
2979 ("uResult=%RX64 uTsc=%#RX64 + %#RX64\n", uResult, uPausedTscValue, offDelta),
2980 ASMSetFlags(fSavedFlags), VERR_NEM_SET_TSC);
2981 }
2982
2983 /*
2984 * Done.
2985 */
2986 ASMSetFlags(fSavedFlags);
2987 return VINF_SUCCESS;
2988}
2989#endif /* NEM_WIN_WITH_RING0_RUNLOOP || NEM_WIN_USE_HYPERCALLS_FOR_REGISTERS */
2990
2991
2992/**
2993 * Sets the TSC register to @a uPausedTscValue on all CPUs.
2994 *
2995 * @returns VBox status code
2996 * @param pGVM The ring-0 VM handle.
2997 * @param idCpu The calling EMT. Necessary for getting the
2998 * hypercall page and arguments.
2999 * @param uPausedTscValue The TSC value at the time of pausing.
3000 */
3001VMMR0_INT_DECL(int) NEMR0ResumeCpuTickOnAll(PGVM pGVM, VMCPUID idCpu, uint64_t uPausedTscValue)
3002{
3003#if defined(NEM_WIN_WITH_RING0_RUNLOOP) || defined(NEM_WIN_USE_HYPERCALLS_FOR_REGISTERS)
3004 /*
3005 * Validate the call.
3006 */
3007 int rc = GVMMR0ValidateGVMandEMT(pGVM, idCpu);
3008 if (RT_SUCCESS(rc))
3009 {
3010 PGVMCPU pGVCpu = &pGVM->aCpus[idCpu];
3011 AssertReturn(g_pfnHvlInvokeHypercall, VERR_NEM_MISSING_KERNEL_API_1);
3012
3013 /*
3014 * Call worker.
3015 */
3016 pGVCpu->nem.s.Hypercall.QueryCpuTick.cTicks = 0;
3017 pGVCpu->nem.s.Hypercall.QueryCpuTick.uAux = 0;
3018 rc = nemR0WinResumeCpuTickOnAll(pGVM, pGVCpu, uPausedTscValue);
3019 }
3020 return rc;
3021#else
3022 RT_NOREF(pGVM, idCpu, uPausedTscValue);
3023 return VERR_NOT_IMPLEMENTED;
3024#endif
3025}
3026
3027
3028VMMR0_INT_DECL(VBOXSTRICTRC) NEMR0RunGuestCode(PGVM pGVM, VMCPUID idCpu)
3029{
3030#ifdef NEM_WIN_WITH_RING0_RUNLOOP
3031 if (pGVM->nemr0.s.fMayUseRing0Runloop)
3032 return nemHCWinRunGC(pGVM, &pGVM->aCpus[idCpu]);
3033 return VERR_NEM_RING3_ONLY;
3034#else
3035 RT_NOREF(pGVM, idCpu);
3036 return VERR_NOT_IMPLEMENTED;
3037#endif
3038}
3039
3040
3041/**
3042 * Updates statistics in the VM structure.
3043 *
3044 * @returns VBox status code.
3045 * @param pGVM The ring-0 VM handle.
3046 * @param idCpu The calling EMT, or NIL. Necessary for getting the hypercall
3047 * page and arguments.
3048 */
3049VMMR0_INT_DECL(int) NEMR0UpdateStatistics(PGVM pGVM, VMCPUID idCpu)
3050{
3051#ifdef NEM_WIN_USE_HYPERCALLS_FOR_PAGES
3052 /*
3053 * Validate the call.
3054 */
3055 int rc;
3056 if (idCpu == NIL_VMCPUID)
3057 rc = GVMMR0ValidateGVM(pGVM);
3058 else
3059 rc = GVMMR0ValidateGVMandEMT(pGVM, idCpu);
3060 if (RT_SUCCESS(rc))
3061 {
3062 AssertReturn(g_pfnHvlInvokeHypercall, VERR_NEM_MISSING_KERNEL_API_1);
3063
3064 PNEMR0HYPERCALLDATA pHypercallData = idCpu != NIL_VMCPUID
3065 ? &pGVM->aCpus[idCpu].nemr0.s.HypercallData
3066 : &pGVM->nemr0.s.HypercallData;
3067 if ( RT_VALID_PTR(pHypercallData->pbPage)
3068 && pHypercallData->HCPhysPage != NIL_RTHCPHYS)
3069 {
3070 if (idCpu == NIL_VMCPUID)
3071 rc = RTCritSectEnter(&pGVM->nemr0.s.HypercallDataCritSect);
3072 if (RT_SUCCESS(rc))
3073 {
3074 /*
3075 * Query the memory statistics for the partition.
3076 */
3077 HV_INPUT_GET_MEMORY_BALANCE *pInput = (HV_INPUT_GET_MEMORY_BALANCE *)pHypercallData->pbPage;
3078 pInput->TargetPartitionId = pGVM->nemr0.s.idHvPartition;
3079 pInput->ProximityDomainInfo.Flags.ProximityPreferred = 0;
3080 pInput->ProximityDomainInfo.Flags.ProxyimityInfoValid = 0;
3081 pInput->ProximityDomainInfo.Flags.Reserved = 0;
3082 pInput->ProximityDomainInfo.Id = 0;
3083
3084 HV_OUTPUT_GET_MEMORY_BALANCE *pOutput = (HV_OUTPUT_GET_MEMORY_BALANCE *)(pInput + 1);
3085 RT_ZERO(*pOutput);
3086
3087 uint64_t uResult = g_pfnHvlInvokeHypercall(HvCallGetMemoryBalance,
3088 pHypercallData->HCPhysPage,
3089 pHypercallData->HCPhysPage + sizeof(*pInput));
3090 if (uResult == HV_STATUS_SUCCESS)
3091 {
3092 pGVM->nem.s.R0Stats.cPagesAvailable = pOutput->PagesAvailable;
3093 pGVM->nem.s.R0Stats.cPagesInUse = pOutput->PagesInUse;
3094 rc = VINF_SUCCESS;
3095 }
3096 else
3097 {
3098 LogRel(("HvCallGetMemoryBalance -> %#RX64 (%#RX64 %#RX64)!!\n",
3099 uResult, pOutput->PagesAvailable, pOutput->PagesInUse));
3100 rc = VERR_NEM_IPE_0;
3101 }
3102
3103 if (idCpu == NIL_VMCPUID)
3104 RTCritSectLeave(&pGVM->nemr0.s.HypercallDataCritSect);
3105 }
3106 }
3107 else
3108 rc = VERR_WRONG_ORDER;
3109 }
3110 return rc;
3111#else
3112 RT_NOREF(pGVM, idCpu);
3113 return VINF_SUCCESS;
3114#endif
3115}
3116
3117
3118/**
3119 * Debug only interface for poking around and exploring Hyper-V stuff.
3120 *
3121 * @param pGVM The ring-0 VM handle.
3122 * @param idCpu The calling EMT.
3123 * @param u64Arg What to query. 0 == registers.
3124 */
3125VMMR0_INT_DECL(int) NEMR0DoExperiment(PGVM pGVM, VMCPUID idCpu, uint64_t u64Arg)
3126{
3127#if defined(DEBUG_bird) && defined(NEM_WIN_USE_HYPERCALLS_FOR_PAGES)
3128 /*
3129 * Resolve CPU structures.
3130 */
3131 int rc = GVMMR0ValidateGVMandEMT(pGVM, idCpu);
3132 if (RT_SUCCESS(rc))
3133 {
3134 AssertReturn(g_pfnHvlInvokeHypercall, VERR_NEM_MISSING_KERNEL_API_1);
3135
3136 PGVMCPU pGVCpu = &pGVM->aCpus[idCpu];
3137 if (u64Arg == 0)
3138 {
3139 /*
3140 * Query register.
3141 */
3142 HV_INPUT_GET_VP_REGISTERS *pInput = (HV_INPUT_GET_VP_REGISTERS *)pGVCpu->nemr0.s.HypercallData.pbPage;
3143 AssertPtrReturn(pInput, VERR_INTERNAL_ERROR_3);
3144
3145 size_t const cbInput = RT_ALIGN_Z(RT_UOFFSETOF(HV_INPUT_GET_VP_REGISTERS, Names[1]), 32);
3146 HV_REGISTER_VALUE *paValues = (HV_REGISTER_VALUE *)((uint8_t *)pInput + cbInput);
3147 RT_BZERO(paValues, sizeof(paValues[0]) * 1);
3148
3149 pInput->PartitionId = pGVM->nemr0.s.idHvPartition;
3150 pInput->VpIndex = pGVCpu->idCpu;
3151 pInput->fFlags = 0;
3152 pInput->Names[0] = (HV_REGISTER_NAME)pGVCpu->nem.s.Hypercall.Experiment.uItem;
3153
3154 uint64_t uResult = g_pfnHvlInvokeHypercall(HV_MAKE_CALL_INFO(HvCallGetVpRegisters, 1),
3155 pGVCpu->nemr0.s.HypercallData.HCPhysPage,
3156 pGVCpu->nemr0.s.HypercallData.HCPhysPage + cbInput);
3157 pGVCpu->nem.s.Hypercall.Experiment.fSuccess = uResult == HV_MAKE_CALL_REP_RET(1);
3158 pGVCpu->nem.s.Hypercall.Experiment.uStatus = uResult;
3159 pGVCpu->nem.s.Hypercall.Experiment.uLoValue = paValues[0].Reg128.Low64;
3160 pGVCpu->nem.s.Hypercall.Experiment.uHiValue = paValues[0].Reg128.High64;
3161 rc = VINF_SUCCESS;
3162 }
3163 else if (u64Arg == 1)
3164 {
3165 /*
3166 * Query partition property.
3167 */
3168 HV_INPUT_GET_PARTITION_PROPERTY *pInput = (HV_INPUT_GET_PARTITION_PROPERTY *)pGVCpu->nemr0.s.HypercallData.pbPage;
3169 AssertPtrReturn(pInput, VERR_INTERNAL_ERROR_3);
3170
3171 size_t const cbInput = RT_ALIGN_Z(sizeof(*pInput), 32);
3172 HV_OUTPUT_GET_PARTITION_PROPERTY *pOutput = (HV_OUTPUT_GET_PARTITION_PROPERTY *)((uint8_t *)pInput + cbInput);
3173 pOutput->PropertyValue = 0;
3174
3175 pInput->PartitionId = pGVM->nemr0.s.idHvPartition;
3176 pInput->PropertyCode = (HV_PARTITION_PROPERTY_CODE)pGVCpu->nem.s.Hypercall.Experiment.uItem;
3177 pInput->uPadding = 0;
3178
3179 uint64_t uResult = g_pfnHvlInvokeHypercall(HvCallGetPartitionProperty,
3180 pGVCpu->nemr0.s.HypercallData.HCPhysPage,
3181 pGVCpu->nemr0.s.HypercallData.HCPhysPage + cbInput);
3182 pGVCpu->nem.s.Hypercall.Experiment.fSuccess = uResult == HV_STATUS_SUCCESS;
3183 pGVCpu->nem.s.Hypercall.Experiment.uStatus = uResult;
3184 pGVCpu->nem.s.Hypercall.Experiment.uLoValue = pOutput->PropertyValue;
3185 pGVCpu->nem.s.Hypercall.Experiment.uHiValue = 0;
3186 rc = VINF_SUCCESS;
3187 }
3188 else if (u64Arg == 2)
3189 {
3190 /*
3191 * Set register.
3192 */
3193 HV_INPUT_SET_VP_REGISTERS *pInput = (HV_INPUT_SET_VP_REGISTERS *)pGVCpu->nemr0.s.HypercallData.pbPage;
3194 AssertPtrReturn(pInput, VERR_INTERNAL_ERROR_3);
3195 RT_BZERO(pInput, RT_UOFFSETOF(HV_INPUT_SET_VP_REGISTERS, Elements[1]));
3196
3197 pInput->PartitionId = pGVM->nemr0.s.idHvPartition;
3198 pInput->VpIndex = pGVCpu->idCpu;
3199 pInput->RsvdZ = 0;
3200 pInput->Elements[0].Name = (HV_REGISTER_NAME)pGVCpu->nem.s.Hypercall.Experiment.uItem;
3201 pInput->Elements[0].Value.Reg128.High64 = pGVCpu->nem.s.Hypercall.Experiment.uHiValue;
3202 pInput->Elements[0].Value.Reg128.Low64 = pGVCpu->nem.s.Hypercall.Experiment.uLoValue;
3203
3204 uint64_t uResult = g_pfnHvlInvokeHypercall(HV_MAKE_CALL_INFO(HvCallSetVpRegisters, 1),
3205 pGVCpu->nemr0.s.HypercallData.HCPhysPage, 0);
3206 pGVCpu->nem.s.Hypercall.Experiment.fSuccess = uResult == HV_MAKE_CALL_REP_RET(1);
3207 pGVCpu->nem.s.Hypercall.Experiment.uStatus = uResult;
3208 rc = VINF_SUCCESS;
3209 }
3210 else
3211 rc = VERR_INVALID_FUNCTION;
3212 }
3213 return rc;
3214#else /* !DEBUG_bird */
3215 RT_NOREF(pGVM, idCpu, u64Arg);
3216 return VERR_NOT_SUPPORTED;
3217#endif /* !DEBUG_bird */
3218}
3219
Note: See TracBrowser for help on using the repository browser.

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