VirtualBox

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

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

VMM/NEM-win: Quick and very dirty hack to make the code work on windows 11. This is _not_ something we want to be doing in the longish run... bugref:10118

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