VirtualBox

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

Last change on this file since 72372 was 72358, checked in by vboxsync, 7 years ago

NEM: Sync more MSR state; don't treat unrecoverable exceptions as triple fault because checking with IEM (need more checking). bugref:9044

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 98.1 KB
Line 
1/* $Id: NEMR0Native-win.cpp 72358 2018-05-28 14:47:51Z vboxsync $ */
2/** @file
3 * NEM - Native execution manager, native ring-0 Windows backend.
4 */
5
6/*
7 * Copyright (C) 2018 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#include <iprt/nt/nt.h>
24#include <iprt/nt/hyperv.h>
25#include <iprt/nt/vid.h>
26#include <winerror.h>
27
28#include <VBox/vmm/nem.h>
29#include <VBox/vmm/iem.h>
30#include <VBox/vmm/em.h>
31#include <VBox/vmm/apic.h>
32#include <VBox/vmm/pdm.h>
33#include "NEMInternal.h"
34#include <VBox/vmm/gvm.h>
35#include <VBox/vmm/vm.h>
36#include <VBox/vmm/gvmm.h>
37#include <VBox/param.h>
38
39#include <iprt/dbg.h>
40#include <iprt/memobj.h>
41#include <iprt/string.h>
42
43
44/* Assert compile context sanity. */
45#ifndef RT_OS_WINDOWS
46# error "Windows only file!"
47#endif
48#ifndef RT_ARCH_AMD64
49# error "AMD64 only file!"
50#endif
51
52
53/*********************************************************************************************************************************
54* Internal Functions *
55*********************************************************************************************************************************/
56typedef uint32_t DWORD; /* for winerror.h constants */
57
58
59/*********************************************************************************************************************************
60* Global Variables *
61*********************************************************************************************************************************/
62static uint64_t (*g_pfnHvlInvokeHypercall)(uint64_t uCallInfo, uint64_t HCPhysInput, uint64_t HCPhysOutput);
63
64/**
65 * WinHvr.sys!WinHvDepositMemory
66 *
67 * This API will try allocates cPages on IdealNode and deposit it to the
68 * hypervisor for use with the given partition. The memory will be freed when
69 * VID.SYS calls WinHvWithdrawAllMemory when the partition is cleanedup.
70 *
71 * Apparently node numbers above 64 has a different meaning.
72 */
73static NTSTATUS (*g_pfnWinHvDepositMemory)(uintptr_t idPartition, size_t cPages, uintptr_t IdealNode, size_t *pcActuallyAdded);
74
75
76/*********************************************************************************************************************************
77* Internal Functions *
78*********************************************************************************************************************************/
79NEM_TMPL_STATIC int nemR0WinMapPages(PGVM pGVM, PVM pVM, PGVMCPU pGVCpu, RTGCPHYS GCPhysSrc, RTGCPHYS GCPhysDst,
80 uint32_t cPages, uint32_t fFlags);
81NEM_TMPL_STATIC int nemR0WinUnmapPages(PGVM pGVM, PGVMCPU pGVCpu, RTGCPHYS GCPhys, uint32_t cPages);
82NEM_TMPL_STATIC int nemR0WinExportState(PGVM pGVM, PGVMCPU pGVCpu, PCPUMCTX pCtx);
83NEM_TMPL_STATIC int nemR0WinImportState(PGVM pGVM, PGVMCPU pGVCpu, PCPUMCTX pCtx, uint64_t fWhat);
84DECLINLINE(NTSTATUS) nemR0NtPerformIoControl(PGVM pGVM, uint32_t uFunction, void *pvInput, uint32_t cbInput,
85 void *pvOutput, uint32_t cbOutput);
86
87
88/*
89 * Instantate the code we share with ring-0.
90 */
91#include "../VMMAll/NEMAllNativeTemplate-win.cpp.h"
92
93/**
94 * Worker for NEMR0InitVM that allocates a hypercall page.
95 *
96 * @returns VBox status code.
97 * @param pHypercallData The hypercall data page to initialize.
98 */
99static int nemR0InitHypercallData(PNEMR0HYPERCALLDATA pHypercallData)
100{
101 int rc = RTR0MemObjAllocPage(&pHypercallData->hMemObj, PAGE_SIZE, false /*fExecutable*/);
102 if (RT_SUCCESS(rc))
103 {
104 pHypercallData->HCPhysPage = RTR0MemObjGetPagePhysAddr(pHypercallData->hMemObj, 0 /*iPage*/);
105 AssertStmt(pHypercallData->HCPhysPage != NIL_RTHCPHYS, rc = VERR_INTERNAL_ERROR_3);
106 pHypercallData->pbPage = (uint8_t *)RTR0MemObjAddress(pHypercallData->hMemObj);
107 AssertStmt(pHypercallData->pbPage, rc = VERR_INTERNAL_ERROR_3);
108 if (RT_SUCCESS(rc))
109 return VINF_SUCCESS;
110
111 /* bail out */
112 RTR0MemObjFree(pHypercallData->hMemObj, true /*fFreeMappings*/);
113 }
114 pHypercallData->hMemObj = NIL_RTR0MEMOBJ;
115 pHypercallData->HCPhysPage = NIL_RTHCPHYS;
116 pHypercallData->pbPage = NULL;
117 return rc;
118}
119
120/**
121 * Worker for NEMR0CleanupVM and NEMR0InitVM that cleans up a hypercall page.
122 *
123 * @param pHypercallData The hypercall data page to uninitialize.
124 */
125static void nemR0DeleteHypercallData(PNEMR0HYPERCALLDATA pHypercallData)
126{
127 /* Check pbPage here since it's NULL, whereas the hMemObj can be either
128 NIL_RTR0MEMOBJ or 0 (they aren't necessarily the same). */
129 if (pHypercallData->pbPage != NULL)
130 {
131 RTR0MemObjFree(pHypercallData->hMemObj, true /*fFreeMappings*/);
132 pHypercallData->pbPage = NULL;
133 }
134 pHypercallData->hMemObj = NIL_RTR0MEMOBJ;
135 pHypercallData->HCPhysPage = NIL_RTHCPHYS;
136}
137
138
139/**
140 * Called by NEMR3Init to make sure we've got what we need.
141 *
142 * @returns VBox status code.
143 * @param pGVM The ring-0 VM handle.
144 * @param pVM The cross context VM handle.
145 * @thread EMT(0)
146 */
147VMMR0_INT_DECL(int) NEMR0InitVM(PGVM pGVM, PVM pVM)
148{
149 AssertCompile(sizeof(pGVM->nem.s) <= sizeof(pGVM->nem.padding));
150 AssertCompile(sizeof(pGVM->aCpus[0].nem.s) <= sizeof(pGVM->aCpus[0].nem.padding));
151
152 int rc = GVMMR0ValidateGVMandVMandEMT(pGVM, pVM, 0);
153 AssertRCReturn(rc, rc);
154
155 /*
156 * We want to perform hypercalls here. The NT kernel started to expose a very low
157 * level interface to do this thru somewhere between build 14271 and 16299. Since
158 * we need build 17134 to get anywhere at all, the exact build is not relevant here.
159 *
160 * We also need to deposit memory to the hypervisor for use with partition (page
161 * mapping structures, stuff).
162 */
163 RTDBGKRNLINFO hKrnlInfo;
164 rc = RTR0DbgKrnlInfoOpen(&hKrnlInfo, 0);
165 if (RT_SUCCESS(rc))
166 {
167 rc = RTR0DbgKrnlInfoQuerySymbol(hKrnlInfo, NULL, "HvlInvokeHypercall", (void **)&g_pfnHvlInvokeHypercall);
168 if (RT_SUCCESS(rc))
169 rc = RTR0DbgKrnlInfoQuerySymbol(hKrnlInfo, "winhvr.sys", "WinHvDepositMemory", (void **)&g_pfnWinHvDepositMemory);
170 RTR0DbgKrnlInfoRelease(hKrnlInfo);
171 if (RT_SUCCESS(rc))
172 {
173 /*
174 * Allocate a page for non-EMT threads to use for hypercalls (update
175 * statistics and such) and a critical section protecting it.
176 */
177 rc = RTCritSectInit(&pGVM->nem.s.HypercallDataCritSect);
178 if (RT_SUCCESS(rc))
179 {
180 rc = nemR0InitHypercallData(&pGVM->nem.s.HypercallData);
181 if (RT_SUCCESS(rc))
182 {
183 /*
184 * Allocate a page for each VCPU to place hypercall data on.
185 */
186 for (VMCPUID i = 0; i < pGVM->cCpus; i++)
187 {
188 rc = nemR0InitHypercallData(&pGVM->aCpus[i].nem.s.HypercallData);
189 if (RT_FAILURE(rc))
190 {
191 while (i-- > 0)
192 nemR0DeleteHypercallData(&pGVM->aCpus[i].nem.s.HypercallData);
193 break;
194 }
195 }
196 if (RT_SUCCESS(rc))
197 {
198 /*
199 * So far, so good.
200 */
201 return rc;
202 }
203
204 /*
205 * Bail out.
206 */
207 nemR0DeleteHypercallData(&pGVM->nem.s.HypercallData);
208 }
209 RTCritSectDelete(&pGVM->nem.s.HypercallDataCritSect);
210 }
211 }
212 else
213 rc = VERR_NEM_MISSING_KERNEL_API;
214 }
215
216 RT_NOREF(pVM);
217 return rc;
218}
219
220
221/**
222 * Perform an I/O control operation on the partition handle (VID.SYS).
223 *
224 * @returns NT status code.
225 * @param pGVM The ring-0 VM structure.
226 * @param uFunction The function to perform.
227 * @param pvInput The input buffer. This must point within the VM
228 * structure so we can easily convert to a ring-3
229 * pointer if necessary.
230 * @param cbInput The size of the input. @a pvInput must be NULL when
231 * zero.
232 * @param pvOutput The output buffer. This must also point within the
233 * VM structure for ring-3 pointer magic.
234 * @param cbOutput The size of the output. @a pvOutput must be NULL
235 * when zero.
236 */
237DECLINLINE(NTSTATUS) nemR0NtPerformIoControl(PGVM pGVM, uint32_t uFunction, void *pvInput, uint32_t cbInput,
238 void *pvOutput, uint32_t cbOutput)
239{
240#ifdef RT_STRICT
241 /*
242 * Input and output parameters are part of the VM CPU structure.
243 */
244 PVM pVM = pGVM->pVM;
245 size_t const cbVM = RT_UOFFSETOF(VM, aCpus[pGVM->cCpus]);
246 if (pvInput)
247 AssertReturn(((uintptr_t)pvInput + cbInput) - (uintptr_t)pVM <= cbVM, VERR_INVALID_PARAMETER);
248 if (pvOutput)
249 AssertReturn(((uintptr_t)pvOutput + cbOutput) - (uintptr_t)pVM <= cbVM, VERR_INVALID_PARAMETER);
250#endif
251
252 int32_t rcNt = STATUS_UNSUCCESSFUL;
253 int rc = SUPR0IoCtlPerform(pGVM->nem.s.pIoCtlCtx, uFunction,
254 pvInput,
255 pvInput ? (uintptr_t)pvInput + pGVM->nem.s.offRing3ConversionDelta : NIL_RTR3PTR,
256 cbInput,
257 pvOutput,
258 pvOutput ? (uintptr_t)pvOutput + pGVM->nem.s.offRing3ConversionDelta : NIL_RTR3PTR,
259 cbOutput,
260 &rcNt);
261 if (RT_SUCCESS(rc) || !NT_SUCCESS((NTSTATUS)rcNt))
262 return (NTSTATUS)rcNt;
263 return STATUS_UNSUCCESSFUL;
264}
265
266
267/**
268 * 2nd part of the initialization, after we've got a partition handle.
269 *
270 * @returns VBox status code.
271 * @param pGVM The ring-0 VM handle.
272 * @param pVM The cross context VM handle.
273 * @thread EMT(0)
274 */
275VMMR0_INT_DECL(int) NEMR0InitVMPart2(PGVM pGVM, PVM pVM)
276{
277 int rc = GVMMR0ValidateGVMandVMandEMT(pGVM, pVM, 0);
278 AssertRCReturn(rc, rc);
279 SUPR0Printf("NEMR0InitVMPart2\n"); LogRel(("2: NEMR0InitVMPart2\n"));
280
281 /*
282 * Copy and validate the I/O control information from ring-3.
283 */
284 NEMWINIOCTL Copy = pVM->nem.s.IoCtlGetHvPartitionId;
285 AssertLogRelReturn(Copy.uFunction != 0, VERR_NEM_INIT_FAILED);
286 AssertLogRelReturn(Copy.cbInput == 0, VERR_NEM_INIT_FAILED);
287 AssertLogRelReturn(Copy.cbOutput == sizeof(HV_PARTITION_ID), VERR_NEM_INIT_FAILED);
288 pGVM->nem.s.IoCtlGetHvPartitionId = Copy;
289
290 Copy = pVM->nem.s.IoCtlStartVirtualProcessor;
291 AssertLogRelReturn(Copy.uFunction != 0, VERR_NEM_INIT_FAILED);
292 AssertLogRelReturn(Copy.cbInput == sizeof(HV_VP_INDEX), VERR_NEM_INIT_FAILED);
293 AssertLogRelReturn(Copy.cbOutput == 0, VERR_NEM_INIT_FAILED);
294 AssertLogRelReturn(Copy.uFunction != pGVM->nem.s.IoCtlGetHvPartitionId.uFunction, VERR_NEM_INIT_FAILED);
295 pGVM->nem.s.IoCtlStartVirtualProcessor = Copy;
296
297 Copy = pVM->nem.s.IoCtlStopVirtualProcessor;
298 AssertLogRelReturn(Copy.uFunction != 0, VERR_NEM_INIT_FAILED);
299 AssertLogRelReturn(Copy.cbInput == sizeof(HV_VP_INDEX), VERR_NEM_INIT_FAILED);
300 AssertLogRelReturn(Copy.cbOutput == 0, VERR_NEM_INIT_FAILED);
301 AssertLogRelReturn(Copy.uFunction != pGVM->nem.s.IoCtlGetHvPartitionId.uFunction, VERR_NEM_INIT_FAILED);
302 AssertLogRelReturn(Copy.uFunction != pGVM->nem.s.IoCtlStartVirtualProcessor.uFunction, VERR_NEM_INIT_FAILED);
303 pGVM->nem.s.IoCtlStopVirtualProcessor = Copy;
304
305 Copy = pVM->nem.s.IoCtlMessageSlotHandleAndGetNext;
306 AssertLogRelReturn(Copy.uFunction != 0, VERR_NEM_INIT_FAILED);
307 AssertLogRelReturn(Copy.cbInput == sizeof(VID_IOCTL_INPUT_MESSAGE_SLOT_HANDLE_AND_GET_NEXT), VERR_NEM_INIT_FAILED);
308 AssertLogRelReturn(Copy.cbOutput == 0, VERR_NEM_INIT_FAILED);
309 AssertLogRelReturn(Copy.uFunction != pGVM->nem.s.IoCtlGetHvPartitionId.uFunction, VERR_NEM_INIT_FAILED);
310 AssertLogRelReturn(Copy.uFunction != pGVM->nem.s.IoCtlStartVirtualProcessor.uFunction, VERR_NEM_INIT_FAILED);
311 AssertLogRelReturn(Copy.uFunction != pGVM->nem.s.IoCtlStopVirtualProcessor.uFunction, VERR_NEM_INIT_FAILED);
312 pGVM->nem.s.IoCtlMessageSlotHandleAndGetNext = Copy;
313
314 /*
315 * Setup of an I/O control context for the partition handle for later use.
316 */
317 rc = SUPR0IoCtlSetupForHandle(pGVM->pSession, pVM->nem.s.hPartitionDevice, 0, &pGVM->nem.s.pIoCtlCtx);
318 AssertLogRelRCReturn(rc, rc);
319 pGVM->nem.s.offRing3ConversionDelta = (uintptr_t)pVM->pVMR3 - (uintptr_t)pGVM->pVM;
320
321 /*
322 * Get the partition ID.
323 */
324 PVMCPU pVCpu = &pGVM->pVM->aCpus[0];
325 NTSTATUS rcNt = nemR0NtPerformIoControl(pGVM, pGVM->nem.s.IoCtlGetHvPartitionId.uFunction, NULL, 0,
326 &pVCpu->nem.s.uIoCtlBuf.idPartition, sizeof(pVCpu->nem.s.uIoCtlBuf.idPartition));
327 AssertLogRelMsgReturn(NT_SUCCESS(rcNt), ("IoCtlGetHvPartitionId failed: %#x\n", rcNt), VERR_NEM_INIT_FAILED);
328 pGVM->nem.s.idHvPartition = pVCpu->nem.s.uIoCtlBuf.idPartition;
329 AssertLogRelMsgReturn(pGVM->nem.s.idHvPartition == pVM->nem.s.idHvPartition,
330 ("idHvPartition mismatch: r0=%#RX64, r3=%#RX64\n", pGVM->nem.s.idHvPartition, pVM->nem.s.idHvPartition),
331 VERR_NEM_INIT_FAILED);
332
333
334 return rc;
335}
336
337
338/**
339 * Cleanup the NEM parts of the VM in ring-0.
340 *
341 * This is always called and must deal the state regardless of whether
342 * NEMR0InitVM() was called or not. So, take care here.
343 *
344 * @param pGVM The ring-0 VM handle.
345 */
346VMMR0_INT_DECL(void) NEMR0CleanupVM(PGVM pGVM)
347{
348 pGVM->nem.s.idHvPartition = HV_PARTITION_ID_INVALID;
349
350 /* Clean up I/O control context. */
351 if (pGVM->nem.s.pIoCtlCtx)
352 {
353 int rc = SUPR0IoCtlCleanup(pGVM->nem.s.pIoCtlCtx);
354 AssertRC(rc);
355 pGVM->nem.s.pIoCtlCtx = NULL;
356 }
357
358 /* Free the hypercall pages. */
359 VMCPUID i = pGVM->cCpus;
360 while (i-- > 0)
361 nemR0DeleteHypercallData(&pGVM->aCpus[i].nem.s.HypercallData);
362
363 /* The non-EMT one too. */
364 if (RTCritSectIsInitialized(&pGVM->nem.s.HypercallDataCritSect))
365 RTCritSectDelete(&pGVM->nem.s.HypercallDataCritSect);
366 nemR0DeleteHypercallData(&pGVM->nem.s.HypercallData);
367}
368
369
370#if 0 /* for debugging GPA unmapping. */
371static int nemR3WinDummyReadGpa(PGVM pGVM, PGVMCPU pGVCpu, RTGCPHYS GCPhys)
372{
373 PHV_INPUT_READ_GPA pIn = (PHV_INPUT_READ_GPA)pGVCpu->nem.s.pbHypercallData;
374 PHV_OUTPUT_READ_GPA pOut = (PHV_OUTPUT_READ_GPA)(pIn + 1);
375 pIn->PartitionId = pGVM->nem.s.idHvPartition;
376 pIn->VpIndex = pGVCpu->idCpu;
377 pIn->ByteCount = 0x10;
378 pIn->BaseGpa = GCPhys;
379 pIn->ControlFlags.AsUINT64 = 0;
380 pIn->ControlFlags.CacheType = HvCacheTypeX64WriteCombining;
381 memset(pOut, 0xfe, sizeof(*pOut));
382 uint64_t volatile uResult = g_pfnHvlInvokeHypercall(HvCallReadGpa, pGVCpu->nem.s.HCPhysHypercallData,
383 pGVCpu->nem.s.HCPhysHypercallData + sizeof(*pIn));
384 LogRel(("nemR3WinDummyReadGpa: %RGp -> %#RX64; code=%u rsvd=%u abData=%.16Rhxs\n",
385 GCPhys, uResult, pOut->AccessResult.ResultCode, pOut->AccessResult.Reserved, pOut->Data));
386 __debugbreak();
387
388 return uResult != 0 ? VERR_READ_ERROR : VINF_SUCCESS;
389}
390#endif
391
392
393/**
394 * Worker for NEMR0MapPages and others.
395 */
396NEM_TMPL_STATIC int nemR0WinMapPages(PGVM pGVM, PVM pVM, PGVMCPU pGVCpu, RTGCPHYS GCPhysSrc, RTGCPHYS GCPhysDst,
397 uint32_t cPages, uint32_t fFlags)
398{
399 /*
400 * Validate.
401 */
402 AssertReturn(g_pfnHvlInvokeHypercall, VERR_NEM_MISSING_KERNEL_API);
403
404 AssertReturn(cPages > 0, VERR_OUT_OF_RANGE);
405 AssertReturn(cPages <= NEM_MAX_MAP_PAGES, VERR_OUT_OF_RANGE);
406 AssertReturn(!(fFlags & ~(HV_MAP_GPA_MAYBE_ACCESS_MASK & ~HV_MAP_GPA_DUNNO_ACCESS)), VERR_INVALID_FLAGS);
407 AssertMsgReturn(!(GCPhysDst & X86_PAGE_OFFSET_MASK), ("GCPhysDst=%RGp\n", GCPhysDst), VERR_OUT_OF_RANGE);
408 AssertReturn(GCPhysDst < _1E, VERR_OUT_OF_RANGE);
409 if (GCPhysSrc != GCPhysDst)
410 {
411 AssertMsgReturn(!(GCPhysSrc & X86_PAGE_OFFSET_MASK), ("GCPhysSrc=%RGp\n", GCPhysSrc), VERR_OUT_OF_RANGE);
412 AssertReturn(GCPhysSrc < _1E, VERR_OUT_OF_RANGE);
413 }
414
415 /*
416 * Compose and make the hypercall.
417 * Ring-3 is not allowed to fill in the host physical addresses of the call.
418 */
419 for (uint32_t iTries = 0;; iTries++)
420 {
421 HV_INPUT_MAP_GPA_PAGES *pMapPages = (HV_INPUT_MAP_GPA_PAGES *)pGVCpu->nem.s.HypercallData.pbPage;
422 AssertPtrReturn(pMapPages, VERR_INTERNAL_ERROR_3);
423 pMapPages->TargetPartitionId = pGVM->nem.s.idHvPartition;
424 pMapPages->TargetGpaBase = GCPhysDst >> X86_PAGE_SHIFT;
425 pMapPages->MapFlags = fFlags;
426 pMapPages->u32ExplicitPadding = 0;
427 for (uint32_t iPage = 0; iPage < cPages; iPage++, GCPhysSrc += X86_PAGE_SIZE)
428 {
429 RTHCPHYS HCPhys = NIL_RTGCPHYS;
430 int rc = PGMPhysGCPhys2HCPhys(pVM, GCPhysSrc, &HCPhys);
431 AssertRCReturn(rc, rc);
432 pMapPages->PageList[iPage] = HCPhys >> X86_PAGE_SHIFT;
433 }
434
435 uint64_t uResult = g_pfnHvlInvokeHypercall(HvCallMapGpaPages | ((uint64_t)cPages << 32),
436 pGVCpu->nem.s.HypercallData.HCPhysPage, 0);
437 Log6(("NEMR0MapPages: %RGp/%RGp L %u prot %#x -> %#RX64\n",
438 GCPhysDst, GCPhysSrc - cPages * X86_PAGE_SIZE, cPages, fFlags, uResult));
439 if (uResult == ((uint64_t)cPages << 32))
440 return VINF_SUCCESS;
441
442 /*
443 * If the partition is out of memory, try donate another 512 pages to
444 * it (2MB). VID.SYS does multiples of 512 pages, nothing smaller.
445 */
446 if ( uResult != HV_STATUS_INSUFFICIENT_MEMORY
447 || iTries > 16
448 || g_pfnWinHvDepositMemory == NULL)
449 {
450 LogRel(("g_pfnHvlInvokeHypercall/MapGpaPages -> %#RX64\n", uResult));
451 return VERR_NEM_MAP_PAGES_FAILED;
452 }
453
454 size_t cPagesAdded = 0;
455 NTSTATUS rcNt = g_pfnWinHvDepositMemory(pGVM->nem.s.idHvPartition, 512, 0, &cPagesAdded);
456 if (!cPagesAdded)
457 {
458 LogRel(("g_pfnWinHvDepositMemory -> %#x / %#RX64\n", rcNt, uResult));
459 return VERR_NEM_MAP_PAGES_FAILED;
460 }
461 }
462}
463
464
465/**
466 * Maps pages into the guest physical address space.
467 *
468 * Generally the caller will be under the PGM lock already, so no extra effort
469 * is needed to make sure all changes happens under it.
470 *
471 * @returns VBox status code.
472 * @param pGVM The ring-0 VM handle.
473 * @param pVM The cross context VM handle.
474 * @param idCpu The calling EMT. Necessary for getting the
475 * hypercall page and arguments.
476 * @thread EMT(idCpu)
477 */
478VMMR0_INT_DECL(int) NEMR0MapPages(PGVM pGVM, PVM pVM, VMCPUID idCpu)
479{
480 /*
481 * Unpack the call.
482 */
483 int rc = GVMMR0ValidateGVMandVMandEMT(pGVM, pVM, idCpu);
484 if (RT_SUCCESS(rc))
485 {
486 PVMCPU pVCpu = &pVM->aCpus[idCpu];
487 PGVMCPU pGVCpu = &pGVM->aCpus[idCpu];
488
489 RTGCPHYS const GCPhysSrc = pVCpu->nem.s.Hypercall.MapPages.GCPhysSrc;
490 RTGCPHYS const GCPhysDst = pVCpu->nem.s.Hypercall.MapPages.GCPhysDst;
491 uint32_t const cPages = pVCpu->nem.s.Hypercall.MapPages.cPages;
492 HV_MAP_GPA_FLAGS const fFlags = pVCpu->nem.s.Hypercall.MapPages.fFlags;
493
494 /*
495 * Do the work.
496 */
497 rc = nemR0WinMapPages(pGVM, pVM, pGVCpu, GCPhysSrc, GCPhysDst, cPages, fFlags);
498 }
499 return rc;
500}
501
502
503/**
504 * Worker for NEMR0UnmapPages and others.
505 */
506NEM_TMPL_STATIC int nemR0WinUnmapPages(PGVM pGVM, PGVMCPU pGVCpu, RTGCPHYS GCPhys, uint32_t cPages)
507{
508 /*
509 * Validate input.
510 */
511 AssertReturn(g_pfnHvlInvokeHypercall, VERR_NEM_MISSING_KERNEL_API);
512
513 AssertReturn(cPages > 0, VERR_OUT_OF_RANGE);
514 AssertReturn(cPages <= NEM_MAX_UNMAP_PAGES, VERR_OUT_OF_RANGE);
515 AssertMsgReturn(!(GCPhys & X86_PAGE_OFFSET_MASK), ("%RGp\n", GCPhys), VERR_OUT_OF_RANGE);
516 AssertReturn(GCPhys < _1E, VERR_OUT_OF_RANGE);
517
518 /*
519 * Compose and make the hypercall.
520 */
521 HV_INPUT_UNMAP_GPA_PAGES *pUnmapPages = (HV_INPUT_UNMAP_GPA_PAGES *)pGVCpu->nem.s.HypercallData.pbPage;
522 AssertPtrReturn(pUnmapPages, VERR_INTERNAL_ERROR_3);
523 pUnmapPages->TargetPartitionId = pGVM->nem.s.idHvPartition;
524 pUnmapPages->TargetGpaBase = GCPhys >> X86_PAGE_SHIFT;
525 pUnmapPages->fFlags = 0;
526
527 uint64_t uResult = g_pfnHvlInvokeHypercall(HvCallUnmapGpaPages | ((uint64_t)cPages << 32),
528 pGVCpu->nem.s.HypercallData.HCPhysPage, 0);
529 Log6(("NEMR0UnmapPages: %RGp L %u -> %#RX64\n", GCPhys, cPages, uResult));
530 if (uResult == ((uint64_t)cPages << 32))
531 {
532#if 1 /* Do we need to do this? Hopefully not... */
533 uint64_t volatile uR = g_pfnHvlInvokeHypercall(HvCallUncommitGpaPages | ((uint64_t)cPages << 32),
534 pGVCpu->nem.s.HypercallData.HCPhysPage, 0);
535 AssertMsg(uR == ((uint64_t)cPages << 32), ("uR=%#RX64\n", uR)); NOREF(uR);
536#endif
537 return VINF_SUCCESS;
538 }
539
540 LogRel(("g_pfnHvlInvokeHypercall/UnmapGpaPages -> %#RX64\n", uResult));
541 return VERR_NEM_UNMAP_PAGES_FAILED;
542}
543
544
545/**
546 * Unmaps pages from the guest physical address space.
547 *
548 * Generally the caller will be under the PGM lock already, so no extra effort
549 * is needed to make sure all changes happens under it.
550 *
551 * @returns VBox status code.
552 * @param pGVM The ring-0 VM handle.
553 * @param pVM The cross context VM handle.
554 * @param idCpu The calling EMT. Necessary for getting the
555 * hypercall page and arguments.
556 * @thread EMT(idCpu)
557 */
558VMMR0_INT_DECL(int) NEMR0UnmapPages(PGVM pGVM, PVM pVM, VMCPUID idCpu)
559{
560 /*
561 * Unpack the call.
562 */
563 int rc = GVMMR0ValidateGVMandVMandEMT(pGVM, pVM, idCpu);
564 if (RT_SUCCESS(rc))
565 {
566 PVMCPU pVCpu = &pVM->aCpus[idCpu];
567 PGVMCPU pGVCpu = &pGVM->aCpus[idCpu];
568
569 RTGCPHYS const GCPhys = pVCpu->nem.s.Hypercall.UnmapPages.GCPhys;
570 uint32_t const cPages = pVCpu->nem.s.Hypercall.UnmapPages.cPages;
571
572 /*
573 * Do the work.
574 */
575 rc = nemR0WinUnmapPages(pGVM, pGVCpu, GCPhys, cPages);
576 }
577 return rc;
578}
579
580
581/**
582 * Worker for NEMR0ExportState.
583 *
584 * Intention is to use it internally later.
585 *
586 * @returns VBox status code.
587 * @param pGVM The ring-0 VM handle.
588 * @param pGVCpu The irng-0 VCPU handle.
589 * @param pCtx The CPU context structure to import into.
590 */
591NEM_TMPL_STATIC int nemR0WinExportState(PGVM pGVM, PGVMCPU pGVCpu, PCPUMCTX pCtx)
592{
593 PVMCPU pVCpu = &pGVM->pVM->aCpus[pGVCpu->idCpu];
594 HV_INPUT_SET_VP_REGISTERS *pInput = (HV_INPUT_SET_VP_REGISTERS *)pGVCpu->nem.s.HypercallData.pbPage;
595 AssertPtrReturn(pInput, VERR_INTERNAL_ERROR_3);
596
597 pInput->PartitionId = pGVM->nem.s.idHvPartition;
598 pInput->VpIndex = pGVCpu->idCpu;
599 pInput->RsvdZ = 0;
600
601 uint64_t const fWhat = ~pCtx->fExtrn & (CPUMCTX_EXTRN_ALL | CPUMCTX_EXTRN_NEM_WIN_MASK);
602 if ( !fWhat
603 && pVCpu->nem.s.fCurrentInterruptWindows == pVCpu->nem.s.fDesiredInterruptWindows)
604 return VINF_SUCCESS;
605 uintptr_t iReg = 0;
606
607 /* GPRs */
608 if (fWhat & CPUMCTX_EXTRN_GPRS_MASK)
609 {
610 if (fWhat & CPUMCTX_EXTRN_RAX)
611 {
612 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
613 pInput->Elements[iReg].Name = HvX64RegisterRax;
614 pInput->Elements[iReg].Value.Reg64 = pCtx->rax;
615 iReg++;
616 }
617 if (fWhat & CPUMCTX_EXTRN_RCX)
618 {
619 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
620 pInput->Elements[iReg].Name = HvX64RegisterRcx;
621 pInput->Elements[iReg].Value.Reg64 = pCtx->rcx;
622 iReg++;
623 }
624 if (fWhat & CPUMCTX_EXTRN_RDX)
625 {
626 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
627 pInput->Elements[iReg].Name = HvX64RegisterRdx;
628 pInput->Elements[iReg].Value.Reg64 = pCtx->rdx;
629 iReg++;
630 }
631 if (fWhat & CPUMCTX_EXTRN_RBX)
632 {
633 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
634 pInput->Elements[iReg].Name = HvX64RegisterRbx;
635 pInput->Elements[iReg].Value.Reg64 = pCtx->rbx;
636 iReg++;
637 }
638 if (fWhat & CPUMCTX_EXTRN_RSP)
639 {
640 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
641 pInput->Elements[iReg].Name = HvX64RegisterRsp;
642 pInput->Elements[iReg].Value.Reg64 = pCtx->rsp;
643 iReg++;
644 }
645 if (fWhat & CPUMCTX_EXTRN_RBP)
646 {
647 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
648 pInput->Elements[iReg].Name = HvX64RegisterRbp;
649 pInput->Elements[iReg].Value.Reg64 = pCtx->rbp;
650 iReg++;
651 }
652 if (fWhat & CPUMCTX_EXTRN_RSI)
653 {
654 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
655 pInput->Elements[iReg].Name = HvX64RegisterRsi;
656 pInput->Elements[iReg].Value.Reg64 = pCtx->rsi;
657 iReg++;
658 }
659 if (fWhat & CPUMCTX_EXTRN_RDI)
660 {
661 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
662 pInput->Elements[iReg].Name = HvX64RegisterRdi;
663 pInput->Elements[iReg].Value.Reg64 = pCtx->rdi;
664 iReg++;
665 }
666 if (fWhat & CPUMCTX_EXTRN_R8_R15)
667 {
668 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
669 pInput->Elements[iReg].Name = HvX64RegisterR8;
670 pInput->Elements[iReg].Value.Reg64 = pCtx->r8;
671 iReg++;
672 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
673 pInput->Elements[iReg].Name = HvX64RegisterR9;
674 pInput->Elements[iReg].Value.Reg64 = pCtx->r9;
675 iReg++;
676 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
677 pInput->Elements[iReg].Name = HvX64RegisterR10;
678 pInput->Elements[iReg].Value.Reg64 = pCtx->r10;
679 iReg++;
680 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
681 pInput->Elements[iReg].Name = HvX64RegisterR11;
682 pInput->Elements[iReg].Value.Reg64 = pCtx->r11;
683 iReg++;
684 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
685 pInput->Elements[iReg].Name = HvX64RegisterR12;
686 pInput->Elements[iReg].Value.Reg64 = pCtx->r12;
687 iReg++;
688 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
689 pInput->Elements[iReg].Name = HvX64RegisterR13;
690 pInput->Elements[iReg].Value.Reg64 = pCtx->r13;
691 iReg++;
692 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
693 pInput->Elements[iReg].Name = HvX64RegisterR14;
694 pInput->Elements[iReg].Value.Reg64 = pCtx->r14;
695 iReg++;
696 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
697 pInput->Elements[iReg].Name = HvX64RegisterR15;
698 pInput->Elements[iReg].Value.Reg64 = pCtx->r15;
699 iReg++;
700 }
701 }
702
703 /* RIP & Flags */
704 if (fWhat & CPUMCTX_EXTRN_RIP)
705 {
706 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
707 pInput->Elements[iReg].Name = HvX64RegisterRip;
708 pInput->Elements[iReg].Value.Reg64 = pCtx->rip;
709 iReg++;
710 }
711 if (fWhat & CPUMCTX_EXTRN_RFLAGS)
712 {
713 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
714 pInput->Elements[iReg].Name = HvX64RegisterRflags;
715 pInput->Elements[iReg].Value.Reg64 = pCtx->rflags.u;
716 iReg++;
717 }
718
719 /* Segments */
720#define COPY_OUT_SEG(a_idx, a_enmName, a_SReg) \
721 do { \
722 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[a_idx]); \
723 pInput->Elements[a_idx].Name = a_enmName; \
724 pInput->Elements[a_idx].Value.Segment.Base = (a_SReg).u64Base; \
725 pInput->Elements[a_idx].Value.Segment.Limit = (a_SReg).u32Limit; \
726 pInput->Elements[a_idx].Value.Segment.Selector = (a_SReg).Sel; \
727 pInput->Elements[a_idx].Value.Segment.Attributes = (a_SReg).Attr.u; \
728 } while (0)
729 if (fWhat & CPUMCTX_EXTRN_SREG_MASK)
730 {
731 if (fWhat & CPUMCTX_EXTRN_CS)
732 {
733 COPY_OUT_SEG(iReg, HvX64RegisterCs, pCtx->cs);
734 iReg++;
735 }
736 if (fWhat & CPUMCTX_EXTRN_ES)
737 {
738 COPY_OUT_SEG(iReg, HvX64RegisterEs, pCtx->es);
739 iReg++;
740 }
741 if (fWhat & CPUMCTX_EXTRN_SS)
742 {
743 COPY_OUT_SEG(iReg, HvX64RegisterSs, pCtx->ss);
744 iReg++;
745 }
746 if (fWhat & CPUMCTX_EXTRN_DS)
747 {
748 COPY_OUT_SEG(iReg, HvX64RegisterDs, pCtx->ds);
749 iReg++;
750 }
751 if (fWhat & CPUMCTX_EXTRN_FS)
752 {
753 COPY_OUT_SEG(iReg, HvX64RegisterFs, pCtx->fs);
754 iReg++;
755 }
756 if (fWhat & CPUMCTX_EXTRN_GS)
757 {
758 COPY_OUT_SEG(iReg, HvX64RegisterGs, pCtx->gs);
759 iReg++;
760 }
761 }
762
763 /* Descriptor tables & task segment. */
764 if (fWhat & CPUMCTX_EXTRN_TABLE_MASK)
765 {
766 if (fWhat & CPUMCTX_EXTRN_LDTR)
767 {
768 COPY_OUT_SEG(iReg, HvX64RegisterLdtr, pCtx->ldtr);
769 iReg++;
770 }
771 if (fWhat & CPUMCTX_EXTRN_TR)
772 {
773 COPY_OUT_SEG(iReg, HvX64RegisterTr, pCtx->tr);
774 iReg++;
775 }
776
777 if (fWhat & CPUMCTX_EXTRN_IDTR)
778 {
779 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
780 pInput->Elements[iReg].Value.Table.Pad[0] = 0;
781 pInput->Elements[iReg].Value.Table.Pad[1] = 0;
782 pInput->Elements[iReg].Value.Table.Pad[2] = 0;
783 pInput->Elements[iReg].Name = HvX64RegisterIdtr;
784 pInput->Elements[iReg].Value.Table.Limit = pCtx->idtr.cbIdt;
785 pInput->Elements[iReg].Value.Table.Base = pCtx->idtr.pIdt;
786 iReg++;
787 }
788 if (fWhat & CPUMCTX_EXTRN_GDTR)
789 {
790 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
791 pInput->Elements[iReg].Value.Table.Pad[0] = 0;
792 pInput->Elements[iReg].Value.Table.Pad[1] = 0;
793 pInput->Elements[iReg].Value.Table.Pad[2] = 0;
794 pInput->Elements[iReg].Name = HvX64RegisterGdtr;
795 pInput->Elements[iReg].Value.Table.Limit = pCtx->gdtr.cbGdt;
796 pInput->Elements[iReg].Value.Table.Base = pCtx->gdtr.pGdt;
797 iReg++;
798 }
799 }
800
801 /* Control registers. */
802 if (fWhat & CPUMCTX_EXTRN_CR_MASK)
803 {
804 if (fWhat & CPUMCTX_EXTRN_CR0)
805 {
806 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
807 pInput->Elements[iReg].Name = HvX64RegisterCr0;
808 pInput->Elements[iReg].Value.Reg64 = pCtx->cr0;
809 iReg++;
810 }
811 if (fWhat & CPUMCTX_EXTRN_CR2)
812 {
813 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
814 pInput->Elements[iReg].Name = HvX64RegisterCr2;
815 pInput->Elements[iReg].Value.Reg64 = pCtx->cr2;
816 iReg++;
817 }
818 if (fWhat & CPUMCTX_EXTRN_CR3)
819 {
820 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
821 pInput->Elements[iReg].Name = HvX64RegisterCr3;
822 pInput->Elements[iReg].Value.Reg64 = pCtx->cr3;
823 iReg++;
824 }
825 if (fWhat & CPUMCTX_EXTRN_CR4)
826 {
827 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
828 pInput->Elements[iReg].Name = HvX64RegisterCr4;
829 pInput->Elements[iReg].Value.Reg64 = pCtx->cr4;
830 iReg++;
831 }
832 }
833 /** @todo CR8/TPR */
834 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
835 pInput->Elements[iReg].Name = HvX64RegisterCr8;
836 pInput->Elements[iReg].Value.Reg64 = CPUMGetGuestCR8(pVCpu);
837 iReg++;
838
839 /** @todo does HvX64RegisterXfem mean XCR0? What about the related MSR. */
840
841 /* Debug registers. */
842/** @todo fixme. Figure out what the hyper-v version of KVM_SET_GUEST_DEBUG would be. */
843 if (fWhat & CPUMCTX_EXTRN_DR0_DR3)
844 {
845 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
846 pInput->Elements[iReg].Name = HvX64RegisterDr0;
847 //pInput->Elements[iReg].Value.Reg64 = CPUMGetHyperDR0(pVCpu);
848 pInput->Elements[iReg].Value.Reg64 = pCtx->dr[0];
849 iReg++;
850 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
851 pInput->Elements[iReg].Name = HvX64RegisterDr1;
852 //pInput->Elements[iReg].Value.Reg64 = CPUMGetHyperDR1(pVCpu);
853 pInput->Elements[iReg].Value.Reg64 = pCtx->dr[1];
854 iReg++;
855 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
856 pInput->Elements[iReg].Name = HvX64RegisterDr2;
857 //pInput->Elements[iReg].Value.Reg64 = CPUMGetHyperDR2(pVCpu);
858 pInput->Elements[iReg].Value.Reg64 = pCtx->dr[2];
859 iReg++;
860 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
861 pInput->Elements[iReg].Name = HvX64RegisterDr3;
862 //pInput->Elements[iReg].Value.Reg64 = CPUMGetHyperDR3(pVCpu);
863 pInput->Elements[iReg].Value.Reg64 = pCtx->dr[3];
864 iReg++;
865 }
866 if (fWhat & CPUMCTX_EXTRN_DR6)
867 {
868 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
869 pInput->Elements[iReg].Name = HvX64RegisterDr6;
870 //pInput->Elements[iReg].Value.Reg64 = CPUMGetHyperDR6(pVCpu);
871 pInput->Elements[iReg].Value.Reg64 = pCtx->dr[6];
872 iReg++;
873 }
874 if (fWhat & CPUMCTX_EXTRN_DR7)
875 {
876 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
877 pInput->Elements[iReg].Name = HvX64RegisterDr7;
878 //pInput->Elements[iReg].Value.Reg64 = CPUMGetHyperDR7(pVCpu);
879 pInput->Elements[iReg].Value.Reg64 = pCtx->dr[7];
880 iReg++;
881 }
882
883 /* Floating point state. */
884 if (fWhat & CPUMCTX_EXTRN_X87)
885 {
886 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
887 pInput->Elements[iReg].Name = HvX64RegisterFpMmx0;
888 pInput->Elements[iReg].Value.Fp.AsUINT128.Low64 = pCtx->pXStateR0->x87.aRegs[0].au64[0];
889 pInput->Elements[iReg].Value.Fp.AsUINT128.High64 = pCtx->pXStateR0->x87.aRegs[0].au64[1];
890 iReg++;
891 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
892 pInput->Elements[iReg].Name = HvX64RegisterFpMmx1;
893 pInput->Elements[iReg].Value.Fp.AsUINT128.Low64 = pCtx->pXStateR0->x87.aRegs[1].au64[0];
894 pInput->Elements[iReg].Value.Fp.AsUINT128.High64 = pCtx->pXStateR0->x87.aRegs[1].au64[1];
895 iReg++;
896 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
897 pInput->Elements[iReg].Name = HvX64RegisterFpMmx2;
898 pInput->Elements[iReg].Value.Fp.AsUINT128.Low64 = pCtx->pXStateR0->x87.aRegs[2].au64[0];
899 pInput->Elements[iReg].Value.Fp.AsUINT128.High64 = pCtx->pXStateR0->x87.aRegs[2].au64[1];
900 iReg++;
901 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
902 pInput->Elements[iReg].Name = HvX64RegisterFpMmx3;
903 pInput->Elements[iReg].Value.Fp.AsUINT128.Low64 = pCtx->pXStateR0->x87.aRegs[3].au64[0];
904 pInput->Elements[iReg].Value.Fp.AsUINT128.High64 = pCtx->pXStateR0->x87.aRegs[3].au64[1];
905 iReg++;
906 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
907 pInput->Elements[iReg].Name = HvX64RegisterFpMmx4;
908 pInput->Elements[iReg].Value.Fp.AsUINT128.Low64 = pCtx->pXStateR0->x87.aRegs[4].au64[0];
909 pInput->Elements[iReg].Value.Fp.AsUINT128.High64 = pCtx->pXStateR0->x87.aRegs[4].au64[1];
910 iReg++;
911 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
912 pInput->Elements[iReg].Name = HvX64RegisterFpMmx5;
913 pInput->Elements[iReg].Value.Fp.AsUINT128.Low64 = pCtx->pXStateR0->x87.aRegs[5].au64[0];
914 pInput->Elements[iReg].Value.Fp.AsUINT128.High64 = pCtx->pXStateR0->x87.aRegs[5].au64[1];
915 iReg++;
916 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
917 pInput->Elements[iReg].Name = HvX64RegisterFpMmx6;
918 pInput->Elements[iReg].Value.Fp.AsUINT128.Low64 = pCtx->pXStateR0->x87.aRegs[6].au64[0];
919 pInput->Elements[iReg].Value.Fp.AsUINT128.High64 = pCtx->pXStateR0->x87.aRegs[6].au64[1];
920 iReg++;
921 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
922 pInput->Elements[iReg].Name = HvX64RegisterFpMmx7;
923 pInput->Elements[iReg].Value.Fp.AsUINT128.Low64 = pCtx->pXStateR0->x87.aRegs[7].au64[0];
924 pInput->Elements[iReg].Value.Fp.AsUINT128.High64 = pCtx->pXStateR0->x87.aRegs[7].au64[1];
925 iReg++;
926
927 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
928 pInput->Elements[iReg].Name = HvX64RegisterFpControlStatus;
929 pInput->Elements[iReg].Value.FpControlStatus.FpControl = pCtx->pXStateR0->x87.FCW;
930 pInput->Elements[iReg].Value.FpControlStatus.FpStatus = pCtx->pXStateR0->x87.FSW;
931 pInput->Elements[iReg].Value.FpControlStatus.FpTag = pCtx->pXStateR0->x87.FTW;
932 pInput->Elements[iReg].Value.FpControlStatus.Reserved = pCtx->pXStateR0->x87.FTW >> 8;
933 pInput->Elements[iReg].Value.FpControlStatus.LastFpOp = pCtx->pXStateR0->x87.FOP;
934 pInput->Elements[iReg].Value.FpControlStatus.LastFpRip = (pCtx->pXStateR0->x87.FPUIP)
935 | ((uint64_t)pCtx->pXStateR0->x87.CS << 32)
936 | ((uint64_t)pCtx->pXStateR0->x87.Rsrvd1 << 48);
937 iReg++;
938/** @todo we've got trouble if if we try write just SSE w/o X87. */
939 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
940 pInput->Elements[iReg].Name = HvX64RegisterXmmControlStatus;
941 pInput->Elements[iReg].Value.XmmControlStatus.LastFpRdp = (pCtx->pXStateR0->x87.FPUDP)
942 | ((uint64_t)pCtx->pXStateR0->x87.DS << 32)
943 | ((uint64_t)pCtx->pXStateR0->x87.Rsrvd2 << 48);
944 pInput->Elements[iReg].Value.XmmControlStatus.XmmStatusControl = pCtx->pXStateR0->x87.MXCSR;
945 pInput->Elements[iReg].Value.XmmControlStatus.XmmStatusControlMask = pCtx->pXStateR0->x87.MXCSR_MASK; /** @todo ??? (Isn't this an output field?) */
946 iReg++;
947 }
948
949 /* Vector state. */
950 if (fWhat & CPUMCTX_EXTRN_SSE_AVX)
951 {
952 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
953 pInput->Elements[iReg].Name = HvX64RegisterXmm0;
954 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->pXStateR0->x87.aXMM[0].uXmm.s.Lo;
955 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->pXStateR0->x87.aXMM[0].uXmm.s.Hi;
956 iReg++;
957 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
958 pInput->Elements[iReg].Name = HvX64RegisterXmm1;
959 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->pXStateR0->x87.aXMM[1].uXmm.s.Lo;
960 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->pXStateR0->x87.aXMM[1].uXmm.s.Hi;
961 iReg++;
962 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
963 pInput->Elements[iReg].Name = HvX64RegisterXmm2;
964 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->pXStateR0->x87.aXMM[2].uXmm.s.Lo;
965 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->pXStateR0->x87.aXMM[2].uXmm.s.Hi;
966 iReg++;
967 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
968 pInput->Elements[iReg].Name = HvX64RegisterXmm3;
969 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->pXStateR0->x87.aXMM[3].uXmm.s.Lo;
970 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->pXStateR0->x87.aXMM[3].uXmm.s.Hi;
971 iReg++;
972 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
973 pInput->Elements[iReg].Name = HvX64RegisterXmm4;
974 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->pXStateR0->x87.aXMM[4].uXmm.s.Lo;
975 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->pXStateR0->x87.aXMM[4].uXmm.s.Hi;
976 iReg++;
977 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
978 pInput->Elements[iReg].Name = HvX64RegisterXmm5;
979 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->pXStateR0->x87.aXMM[5].uXmm.s.Lo;
980 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->pXStateR0->x87.aXMM[5].uXmm.s.Hi;
981 iReg++;
982 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
983 pInput->Elements[iReg].Name = HvX64RegisterXmm6;
984 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->pXStateR0->x87.aXMM[6].uXmm.s.Lo;
985 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->pXStateR0->x87.aXMM[6].uXmm.s.Hi;
986 iReg++;
987 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
988 pInput->Elements[iReg].Name = HvX64RegisterXmm7;
989 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->pXStateR0->x87.aXMM[7].uXmm.s.Lo;
990 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->pXStateR0->x87.aXMM[7].uXmm.s.Hi;
991 iReg++;
992 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
993 pInput->Elements[iReg].Name = HvX64RegisterXmm8;
994 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->pXStateR0->x87.aXMM[8].uXmm.s.Lo;
995 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->pXStateR0->x87.aXMM[8].uXmm.s.Hi;
996 iReg++;
997 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
998 pInput->Elements[iReg].Name = HvX64RegisterXmm9;
999 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->pXStateR0->x87.aXMM[9].uXmm.s.Lo;
1000 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->pXStateR0->x87.aXMM[9].uXmm.s.Hi;
1001 iReg++;
1002 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1003 pInput->Elements[iReg].Name = HvX64RegisterXmm10;
1004 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->pXStateR0->x87.aXMM[10].uXmm.s.Lo;
1005 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->pXStateR0->x87.aXMM[10].uXmm.s.Hi;
1006 iReg++;
1007 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1008 pInput->Elements[iReg].Name = HvX64RegisterXmm11;
1009 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->pXStateR0->x87.aXMM[11].uXmm.s.Lo;
1010 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->pXStateR0->x87.aXMM[11].uXmm.s.Hi;
1011 iReg++;
1012 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1013 pInput->Elements[iReg].Name = HvX64RegisterXmm12;
1014 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->pXStateR0->x87.aXMM[12].uXmm.s.Lo;
1015 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->pXStateR0->x87.aXMM[12].uXmm.s.Hi;
1016 iReg++;
1017 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1018 pInput->Elements[iReg].Name = HvX64RegisterXmm13;
1019 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->pXStateR0->x87.aXMM[13].uXmm.s.Lo;
1020 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->pXStateR0->x87.aXMM[13].uXmm.s.Hi;
1021 iReg++;
1022 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1023 pInput->Elements[iReg].Name = HvX64RegisterXmm14;
1024 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->pXStateR0->x87.aXMM[14].uXmm.s.Lo;
1025 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->pXStateR0->x87.aXMM[14].uXmm.s.Hi;
1026 iReg++;
1027 HV_REGISTER_ASSOC_ZERO_PADDING(&pInput->Elements[iReg]);
1028 pInput->Elements[iReg].Name = HvX64RegisterXmm15;
1029 pInput->Elements[iReg].Value.Reg128.Low64 = pCtx->pXStateR0->x87.aXMM[15].uXmm.s.Lo;
1030 pInput->Elements[iReg].Value.Reg128.High64 = pCtx->pXStateR0->x87.aXMM[15].uXmm.s.Hi;
1031 iReg++;
1032 }
1033
1034 /* MSRs */
1035 // HvX64RegisterTsc - don't touch
1036 /** @todo does HvX64RegisterTsc include TSC_AUX? Is it TSC_AUX? */
1037 if (fWhat & CPUMCTX_EXTRN_EFER)
1038 {
1039 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1040 pInput->Elements[iReg].Name = HvX64RegisterEfer;
1041 pInput->Elements[iReg].Value.Reg64 = pCtx->msrEFER;
1042 iReg++;
1043 }
1044 if (fWhat & CPUMCTX_EXTRN_KERNEL_GS_BASE)
1045 {
1046 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1047 pInput->Elements[iReg].Name = HvX64RegisterKernelGsBase;
1048 pInput->Elements[iReg].Value.Reg64 = pCtx->msrKERNELGSBASE;
1049 iReg++;
1050 }
1051 if (fWhat & CPUMCTX_EXTRN_SYSENTER_MSRS)
1052 {
1053 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1054 pInput->Elements[iReg].Name = HvX64RegisterSysenterCs;
1055 pInput->Elements[iReg].Value.Reg64 = pCtx->SysEnter.cs;
1056 iReg++;
1057 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1058 pInput->Elements[iReg].Name = HvX64RegisterSysenterEip;
1059 pInput->Elements[iReg].Value.Reg64 = pCtx->SysEnter.eip;
1060 iReg++;
1061 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1062 pInput->Elements[iReg].Name = HvX64RegisterSysenterEsp;
1063 pInput->Elements[iReg].Value.Reg64 = pCtx->SysEnter.esp;
1064 iReg++;
1065 }
1066 if (fWhat & CPUMCTX_EXTRN_SYSCALL_MSRS)
1067 {
1068 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1069 pInput->Elements[iReg].Name = HvX64RegisterStar;
1070 pInput->Elements[iReg].Value.Reg64 = pCtx->msrSTAR;
1071 iReg++;
1072 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1073 pInput->Elements[iReg].Name = HvX64RegisterLstar;
1074 pInput->Elements[iReg].Value.Reg64 = pCtx->msrLSTAR;
1075 iReg++;
1076 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1077 pInput->Elements[iReg].Name = HvX64RegisterCstar;
1078 pInput->Elements[iReg].Value.Reg64 = pCtx->msrCSTAR;
1079 iReg++;
1080 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1081 pInput->Elements[iReg].Name = HvX64RegisterSfmask;
1082 pInput->Elements[iReg].Value.Reg64 = pCtx->msrSFMASK;
1083 iReg++;
1084 }
1085 if (fWhat & CPUMCTX_EXTRN_OTHER_MSRS)
1086 {
1087 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1088 pInput->Elements[iReg].Name = HvX64RegisterApicBase;
1089 pInput->Elements[iReg].Value.Reg64 = APICGetBaseMsrNoCheck(pVCpu);
1090 iReg++;
1091 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1092 pInput->Elements[iReg].Name = HvX64RegisterPat;
1093 pInput->Elements[iReg].Value.Reg64 = pCtx->msrPAT;
1094 iReg++;
1095#if 0 /** @todo HvX64RegisterMtrrCap is read only? Seems it's not even readable. */
1096 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1097 pInput->Elements[iReg].Name = HvX64RegisterMtrrCap;
1098 pInput->Elements[iReg].Value.Reg64 = CPUMGetGuestIa32MtrrCap(pVCpu);
1099 iReg++;
1100#endif
1101
1102 PCPUMCTXMSRS pCtxMsrs = CPUMQueryGuestCtxMsrsPtr(pVCpu);
1103
1104 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1105 pInput->Elements[iReg].Name = HvX64RegisterMtrrDefType;
1106 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrDefType;
1107 iReg++;
1108
1109 /** @todo we dont keep state for HvX64RegisterMtrrPhysBaseX and HvX64RegisterMtrrPhysMaskX */
1110
1111 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1112 pInput->Elements[iReg].Name = HvX64RegisterMtrrFix64k00000;
1113 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrFix64K_00000;
1114 iReg++;
1115 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1116 pInput->Elements[iReg].Name = HvX64RegisterMtrrFix16k80000;
1117 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrFix16K_80000;
1118 iReg++;
1119 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1120 pInput->Elements[iReg].Name = HvX64RegisterMtrrFix16kA0000;
1121 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrFix16K_A0000;
1122 iReg++;
1123 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1124 pInput->Elements[iReg].Name = HvX64RegisterMtrrFix4kC0000;
1125 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrFix4K_C0000;
1126 iReg++;
1127 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1128 pInput->Elements[iReg].Name = HvX64RegisterMtrrFix4kC8000;
1129 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrFix4K_C8000;
1130 iReg++;
1131 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1132 pInput->Elements[iReg].Name = HvX64RegisterMtrrFix4kD0000;
1133 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrFix4K_D0000;
1134 iReg++;
1135 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1136 pInput->Elements[iReg].Name = HvX64RegisterMtrrFix4kD8000;
1137 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrFix4K_D8000;
1138 iReg++;
1139 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1140 pInput->Elements[iReg].Name = HvX64RegisterMtrrFix4kE0000;
1141 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrFix4K_E0000;
1142 iReg++;
1143 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1144 pInput->Elements[iReg].Name = HvX64RegisterMtrrFix4kE8000;
1145 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrFix4K_E8000;
1146 iReg++;
1147 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1148 pInput->Elements[iReg].Name = HvX64RegisterMtrrFix4kF0000;
1149 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrFix4K_F0000;
1150 iReg++;
1151 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1152 pInput->Elements[iReg].Name = HvX64RegisterMtrrFix4kF8000;
1153 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MtrrFix4K_F8000;
1154 iReg++;
1155
1156 const CPUMCPUVENDOR enmCpuVendor = CPUMGetHostCpuVendor(pGVM->pVM);
1157 if (enmCpuVendor != CPUMCPUVENDOR_AMD)
1158 {
1159 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1160 pInput->Elements[iReg].Name = HvX64RegisterIa32MiscEnable;
1161 pInput->Elements[iReg].Value.Reg64 = pCtxMsrs->msr.MiscEnable;
1162 iReg++;
1163 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1164 pInput->Elements[iReg].Name = HvX64RegisterIa32FeatureControl;
1165 pInput->Elements[iReg].Value.Reg64 = CPUMGetGuestIa32FeatureControl(pVCpu);
1166 iReg++;
1167 }
1168 }
1169
1170 /* event injection (always clear it). */
1171 if (fWhat & CPUMCTX_EXTRN_NEM_WIN_EVENT_INJECT)
1172 {
1173 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1174 pInput->Elements[iReg].Name = HvRegisterPendingInterruption;
1175 pInput->Elements[iReg].Value.Reg64 = 0;
1176 iReg++;
1177 }
1178
1179 /* Interruptibility state. This can get a little complicated since we get
1180 half of the state via HV_X64_VP_EXECUTION_STATE. */
1181 if ( (fWhat & (CPUMCTX_EXTRN_NEM_WIN_INHIBIT_INT | CPUMCTX_EXTRN_NEM_WIN_INHIBIT_NMI))
1182 == (CPUMCTX_EXTRN_NEM_WIN_INHIBIT_INT | CPUMCTX_EXTRN_NEM_WIN_INHIBIT_NMI) )
1183 {
1184 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1185 pInput->Elements[iReg].Name = HvRegisterInterruptState;
1186 pInput->Elements[iReg].Value.Reg64 = 0;
1187 if ( VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS)
1188 && EMGetInhibitInterruptsPC(pVCpu) == pCtx->rip)
1189 pInput->Elements[iReg].Value.InterruptState.InterruptShadow = 1;
1190 if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_BLOCK_NMIS))
1191 pInput->Elements[iReg].Value.InterruptState.NmiMasked = 1;
1192 iReg++;
1193 }
1194 else if (fWhat & CPUMCTX_EXTRN_NEM_WIN_INHIBIT_INT)
1195 {
1196 if ( pVCpu->nem.s.fLastInterruptShadow
1197 || ( VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS)
1198 && EMGetInhibitInterruptsPC(pVCpu) == pCtx->rip))
1199 {
1200 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1201 pInput->Elements[iReg].Name = HvRegisterInterruptState;
1202 pInput->Elements[iReg].Value.Reg64 = 0;
1203 if ( VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS)
1204 && EMGetInhibitInterruptsPC(pVCpu) == pCtx->rip)
1205 pInput->Elements[iReg].Value.InterruptState.InterruptShadow = 1;
1206 /** @todo Retrieve NMI state, currently assuming it's zero. (yes this may happen on I/O) */
1207 //if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_BLOCK_NMIS))
1208 // pInput->Elements[iReg].Value.InterruptState.NmiMasked = 1;
1209 iReg++;
1210 }
1211 }
1212 else
1213 Assert(!(fWhat & CPUMCTX_EXTRN_NEM_WIN_INHIBIT_NMI));
1214
1215 /* Interrupt windows. Always set if active as Hyper-V seems to be forgetful. */
1216 uint8_t const fDesiredIntWin = pVCpu->nem.s.fDesiredInterruptWindows;
1217 if ( fDesiredIntWin
1218 || pVCpu->nem.s.fCurrentInterruptWindows != fDesiredIntWin)
1219 {
1220 pVCpu->nem.s.fCurrentInterruptWindows = pVCpu->nem.s.fDesiredInterruptWindows;
1221 HV_REGISTER_ASSOC_ZERO_PADDING_AND_HI64(&pInput->Elements[iReg]);
1222 pInput->Elements[iReg].Name = HvX64RegisterDeliverabilityNotifications;
1223 pInput->Elements[iReg].Value.DeliverabilityNotifications.AsUINT64 = fDesiredIntWin;
1224 Assert(pInput->Elements[iReg].Value.DeliverabilityNotifications.NmiNotification == RT_BOOL(fDesiredIntWin & NEM_WIN_INTW_F_NMI));
1225 Assert(pInput->Elements[iReg].Value.DeliverabilityNotifications.InterruptNotification == RT_BOOL(fDesiredIntWin & NEM_WIN_INTW_F_REGULAR));
1226 Assert(pInput->Elements[iReg].Value.DeliverabilityNotifications.InterruptPriority == (fDesiredIntWin & NEM_WIN_INTW_F_PRIO_MASK) >> NEM_WIN_INTW_F_PRIO_SHIFT);
1227 iReg++;
1228 }
1229
1230 /// @todo HvRegisterPendingEvent0
1231 /// @todo HvRegisterPendingEvent1
1232
1233 /*
1234 * Set the registers.
1235 */
1236 Assert((uintptr_t)&pInput->Elements[iReg] - (uintptr_t)pGVCpu->nem.s.HypercallData.pbPage < PAGE_SIZE); /* max is 127 */
1237
1238 /*
1239 * Make the hypercall.
1240 */
1241 uint64_t uResult = g_pfnHvlInvokeHypercall(HV_MAKE_CALL_INFO(HvCallSetVpRegisters, iReg),
1242 pGVCpu->nem.s.HypercallData.HCPhysPage, 0 /*GCPhysOutput*/);
1243 AssertLogRelMsgReturn(uResult == HV_MAKE_CALL_REP_RET(iReg),
1244 ("uResult=%RX64 iRegs=%#x\n", uResult, iReg),
1245 VERR_NEM_SET_REGISTERS_FAILED);
1246 //LogFlow(("nemR0WinExportState: uResult=%#RX64 iReg=%zu fWhat=%#018RX64 fExtrn=%#018RX64 -> %#018RX64\n", uResult, iReg, fWhat, pCtx->fExtrn,
1247 // pCtx->fExtrn | CPUMCTX_EXTRN_ALL | CPUMCTX_EXTRN_NEM_WIN_MASK | CPUMCTX_EXTRN_KEEPER_NEM ));
1248 pCtx->fExtrn |= CPUMCTX_EXTRN_ALL | CPUMCTX_EXTRN_NEM_WIN_MASK | CPUMCTX_EXTRN_KEEPER_NEM;
1249 return VINF_SUCCESS;
1250}
1251
1252
1253/**
1254 * Export the state to the native API (out of CPUMCTX).
1255 *
1256 * @returns VBox status code
1257 * @param pGVM The ring-0 VM handle.
1258 * @param pVM The cross context VM handle.
1259 * @param idCpu The calling EMT. Necessary for getting the
1260 * hypercall page and arguments.
1261 */
1262VMMR0_INT_DECL(int) NEMR0ExportState(PGVM pGVM, PVM pVM, VMCPUID idCpu)
1263{
1264 /*
1265 * Validate the call.
1266 */
1267 int rc = GVMMR0ValidateGVMandVMandEMT(pGVM, pVM, idCpu);
1268 if (RT_SUCCESS(rc))
1269 {
1270 PVMCPU pVCpu = &pVM->aCpus[idCpu];
1271 PGVMCPU pGVCpu = &pGVM->aCpus[idCpu];
1272 AssertReturn(g_pfnHvlInvokeHypercall, VERR_NEM_MISSING_KERNEL_API);
1273
1274 /*
1275 * Call worker.
1276 */
1277 rc = nemR0WinExportState(pGVM, pGVCpu, CPUMQueryGuestCtxPtr(pVCpu));
1278 }
1279 return rc;
1280}
1281
1282
1283/**
1284 * Worker for NEMR0ImportState.
1285 *
1286 * Intention is to use it internally later.
1287 *
1288 * @returns VBox status code.
1289 * @param pGVM The ring-0 VM handle.
1290 * @param pGVCpu The irng-0 VCPU handle.
1291 * @param pCtx The CPU context structure to import into.
1292 * @param fWhat What to import, CPUMCTX_EXTRN_XXX.
1293 */
1294NEM_TMPL_STATIC int nemR0WinImportState(PGVM pGVM, PGVMCPU pGVCpu, PCPUMCTX pCtx, uint64_t fWhat)
1295{
1296 HV_INPUT_GET_VP_REGISTERS *pInput = (HV_INPUT_GET_VP_REGISTERS *)pGVCpu->nem.s.HypercallData.pbPage;
1297 AssertPtrReturn(pInput, VERR_INTERNAL_ERROR_3);
1298
1299 fWhat &= pCtx->fExtrn;
1300
1301 pInput->PartitionId = pGVM->nem.s.idHvPartition;
1302 pInput->VpIndex = pGVCpu->idCpu;
1303 pInput->fFlags = 0;
1304
1305 /* GPRs */
1306 uintptr_t iReg = 0;
1307 if (fWhat & CPUMCTX_EXTRN_GPRS_MASK)
1308 {
1309 if (fWhat & CPUMCTX_EXTRN_RAX)
1310 pInput->Names[iReg++] = HvX64RegisterRax;
1311 if (fWhat & CPUMCTX_EXTRN_RCX)
1312 pInput->Names[iReg++] = HvX64RegisterRcx;
1313 if (fWhat & CPUMCTX_EXTRN_RDX)
1314 pInput->Names[iReg++] = HvX64RegisterRdx;
1315 if (fWhat & CPUMCTX_EXTRN_RBX)
1316 pInput->Names[iReg++] = HvX64RegisterRbx;
1317 if (fWhat & CPUMCTX_EXTRN_RSP)
1318 pInput->Names[iReg++] = HvX64RegisterRsp;
1319 if (fWhat & CPUMCTX_EXTRN_RBP)
1320 pInput->Names[iReg++] = HvX64RegisterRbp;
1321 if (fWhat & CPUMCTX_EXTRN_RSI)
1322 pInput->Names[iReg++] = HvX64RegisterRsi;
1323 if (fWhat & CPUMCTX_EXTRN_RDI)
1324 pInput->Names[iReg++] = HvX64RegisterRdi;
1325 if (fWhat & CPUMCTX_EXTRN_R8_R15)
1326 {
1327 pInput->Names[iReg++] = HvX64RegisterR8;
1328 pInput->Names[iReg++] = HvX64RegisterR9;
1329 pInput->Names[iReg++] = HvX64RegisterR10;
1330 pInput->Names[iReg++] = HvX64RegisterR11;
1331 pInput->Names[iReg++] = HvX64RegisterR12;
1332 pInput->Names[iReg++] = HvX64RegisterR13;
1333 pInput->Names[iReg++] = HvX64RegisterR14;
1334 pInput->Names[iReg++] = HvX64RegisterR15;
1335 }
1336 }
1337
1338 /* RIP & Flags */
1339 if (fWhat & CPUMCTX_EXTRN_RIP)
1340 pInput->Names[iReg++] = HvX64RegisterRip;
1341 if (fWhat & CPUMCTX_EXTRN_RFLAGS)
1342 pInput->Names[iReg++] = HvX64RegisterRflags;
1343
1344 /* Segments */
1345 if (fWhat & CPUMCTX_EXTRN_SREG_MASK)
1346 {
1347 if (fWhat & CPUMCTX_EXTRN_CS)
1348 pInput->Names[iReg++] = HvX64RegisterCs;
1349 if (fWhat & CPUMCTX_EXTRN_ES)
1350 pInput->Names[iReg++] = HvX64RegisterEs;
1351 if (fWhat & CPUMCTX_EXTRN_SS)
1352 pInput->Names[iReg++] = HvX64RegisterSs;
1353 if (fWhat & CPUMCTX_EXTRN_DS)
1354 pInput->Names[iReg++] = HvX64RegisterDs;
1355 if (fWhat & CPUMCTX_EXTRN_FS)
1356 pInput->Names[iReg++] = HvX64RegisterFs;
1357 if (fWhat & CPUMCTX_EXTRN_GS)
1358 pInput->Names[iReg++] = HvX64RegisterGs;
1359 }
1360
1361 /* Descriptor tables and the task segment. */
1362 if (fWhat & CPUMCTX_EXTRN_TABLE_MASK)
1363 {
1364 if (fWhat & CPUMCTX_EXTRN_LDTR)
1365 pInput->Names[iReg++] = HvX64RegisterLdtr;
1366 if (fWhat & CPUMCTX_EXTRN_TR)
1367 pInput->Names[iReg++] = HvX64RegisterTr;
1368 if (fWhat & CPUMCTX_EXTRN_IDTR)
1369 pInput->Names[iReg++] = HvX64RegisterIdtr;
1370 if (fWhat & CPUMCTX_EXTRN_GDTR)
1371 pInput->Names[iReg++] = HvX64RegisterGdtr;
1372 }
1373
1374 /* Control registers. */
1375 if (fWhat & CPUMCTX_EXTRN_CR_MASK)
1376 {
1377 if (fWhat & CPUMCTX_EXTRN_CR0)
1378 pInput->Names[iReg++] = HvX64RegisterCr0;
1379 if (fWhat & CPUMCTX_EXTRN_CR2)
1380 pInput->Names[iReg++] = HvX64RegisterCr2;
1381 if (fWhat & CPUMCTX_EXTRN_CR3)
1382 pInput->Names[iReg++] = HvX64RegisterCr3;
1383 if (fWhat & CPUMCTX_EXTRN_CR4)
1384 pInput->Names[iReg++] = HvX64RegisterCr4;
1385 }
1386 pInput->Names[iReg++] = HvX64RegisterCr8; /// @todo CR8/TPR
1387
1388 /* Debug registers. */
1389 if (fWhat & CPUMCTX_EXTRN_DR0_DR3)
1390 {
1391 pInput->Names[iReg++] = HvX64RegisterDr0;
1392 pInput->Names[iReg++] = HvX64RegisterDr1;
1393 pInput->Names[iReg++] = HvX64RegisterDr2;
1394 pInput->Names[iReg++] = HvX64RegisterDr3;
1395 }
1396 if (fWhat & CPUMCTX_EXTRN_DR6)
1397 pInput->Names[iReg++] = HvX64RegisterDr6;
1398 if (fWhat & CPUMCTX_EXTRN_DR7)
1399 pInput->Names[iReg++] = HvX64RegisterDr7;
1400
1401 /* Floating point state. */
1402 if (fWhat & CPUMCTX_EXTRN_X87)
1403 {
1404 pInput->Names[iReg++] = HvX64RegisterFpMmx0;
1405 pInput->Names[iReg++] = HvX64RegisterFpMmx1;
1406 pInput->Names[iReg++] = HvX64RegisterFpMmx2;
1407 pInput->Names[iReg++] = HvX64RegisterFpMmx3;
1408 pInput->Names[iReg++] = HvX64RegisterFpMmx4;
1409 pInput->Names[iReg++] = HvX64RegisterFpMmx5;
1410 pInput->Names[iReg++] = HvX64RegisterFpMmx6;
1411 pInput->Names[iReg++] = HvX64RegisterFpMmx7;
1412 pInput->Names[iReg++] = HvX64RegisterFpControlStatus;
1413 }
1414 if (fWhat & (CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX))
1415 pInput->Names[iReg++] = HvX64RegisterXmmControlStatus;
1416
1417 /* Vector state. */
1418 if (fWhat & CPUMCTX_EXTRN_SSE_AVX)
1419 {
1420 pInput->Names[iReg++] = HvX64RegisterXmm0;
1421 pInput->Names[iReg++] = HvX64RegisterXmm1;
1422 pInput->Names[iReg++] = HvX64RegisterXmm2;
1423 pInput->Names[iReg++] = HvX64RegisterXmm3;
1424 pInput->Names[iReg++] = HvX64RegisterXmm4;
1425 pInput->Names[iReg++] = HvX64RegisterXmm5;
1426 pInput->Names[iReg++] = HvX64RegisterXmm6;
1427 pInput->Names[iReg++] = HvX64RegisterXmm7;
1428 pInput->Names[iReg++] = HvX64RegisterXmm8;
1429 pInput->Names[iReg++] = HvX64RegisterXmm9;
1430 pInput->Names[iReg++] = HvX64RegisterXmm10;
1431 pInput->Names[iReg++] = HvX64RegisterXmm11;
1432 pInput->Names[iReg++] = HvX64RegisterXmm12;
1433 pInput->Names[iReg++] = HvX64RegisterXmm13;
1434 pInput->Names[iReg++] = HvX64RegisterXmm14;
1435 pInput->Names[iReg++] = HvX64RegisterXmm15;
1436 }
1437
1438 /* MSRs */
1439 // HvX64RegisterTsc - don't touch
1440 if (fWhat & CPUMCTX_EXTRN_EFER)
1441 pInput->Names[iReg++] = HvX64RegisterEfer;
1442 if (fWhat & CPUMCTX_EXTRN_KERNEL_GS_BASE)
1443 pInput->Names[iReg++] = HvX64RegisterKernelGsBase;
1444 if (fWhat & CPUMCTX_EXTRN_SYSENTER_MSRS)
1445 {
1446 pInput->Names[iReg++] = HvX64RegisterSysenterCs;
1447 pInput->Names[iReg++] = HvX64RegisterSysenterEip;
1448 pInput->Names[iReg++] = HvX64RegisterSysenterEsp;
1449 }
1450 if (fWhat & CPUMCTX_EXTRN_SYSCALL_MSRS)
1451 {
1452 pInput->Names[iReg++] = HvX64RegisterStar;
1453 pInput->Names[iReg++] = HvX64RegisterLstar;
1454 pInput->Names[iReg++] = HvX64RegisterCstar;
1455 pInput->Names[iReg++] = HvX64RegisterSfmask;
1456 }
1457
1458 const CPUMCPUVENDOR enmCpuVendor = CPUMGetHostCpuVendor(pGVM->pVM);
1459 if (fWhat & CPUMCTX_EXTRN_OTHER_MSRS)
1460 {
1461 pInput->Names[iReg++] = HvX64RegisterApicBase; /// @todo APIC BASE
1462 pInput->Names[iReg++] = HvX64RegisterPat;
1463#if 0 /*def LOG_ENABLED*/ /** @todo something's wrong with HvX64RegisterMtrrCap? (AMD) */
1464 pInput->Names[iReg++] = HvX64RegisterMtrrCap;
1465#endif
1466 pInput->Names[iReg++] = HvX64RegisterMtrrDefType;
1467 pInput->Names[iReg++] = HvX64RegisterMtrrFix64k00000;
1468 pInput->Names[iReg++] = HvX64RegisterMtrrFix16k80000;
1469 pInput->Names[iReg++] = HvX64RegisterMtrrFix16kA0000;
1470 pInput->Names[iReg++] = HvX64RegisterMtrrFix4kC0000;
1471 pInput->Names[iReg++] = HvX64RegisterMtrrFix4kC8000;
1472 pInput->Names[iReg++] = HvX64RegisterMtrrFix4kD0000;
1473 pInput->Names[iReg++] = HvX64RegisterMtrrFix4kD8000;
1474 pInput->Names[iReg++] = HvX64RegisterMtrrFix4kE0000;
1475 pInput->Names[iReg++] = HvX64RegisterMtrrFix4kE8000;
1476 pInput->Names[iReg++] = HvX64RegisterMtrrFix4kF0000;
1477 pInput->Names[iReg++] = HvX64RegisterMtrrFix4kF8000;
1478 if (enmCpuVendor != CPUMCPUVENDOR_AMD)
1479 {
1480 pInput->Names[iReg++] = HvX64RegisterIa32MiscEnable;
1481#ifdef LOG_ENABLED
1482 pInput->Names[iReg++] = HvX64RegisterIa32FeatureControl;
1483#endif
1484 }
1485 }
1486
1487 /* Interruptibility. */
1488 if (fWhat & (CPUMCTX_EXTRN_NEM_WIN_INHIBIT_INT | CPUMCTX_EXTRN_NEM_WIN_INHIBIT_NMI))
1489 {
1490 pInput->Names[iReg++] = HvRegisterInterruptState;
1491 pInput->Names[iReg++] = HvX64RegisterRip;
1492 }
1493
1494 /* event injection */
1495 pInput->Names[iReg++] = HvRegisterPendingInterruption;
1496 pInput->Names[iReg++] = HvRegisterPendingEvent0;
1497 pInput->Names[iReg++] = HvRegisterPendingEvent1;
1498 size_t const cRegs = iReg;
1499 size_t const cbInput = RT_ALIGN_Z(RT_OFFSETOF(HV_INPUT_GET_VP_REGISTERS, Names[cRegs]), 32);
1500
1501 HV_REGISTER_VALUE *paValues = (HV_REGISTER_VALUE *)((uint8_t *)pInput + cbInput);
1502 Assert((uintptr_t)&paValues[cRegs] - (uintptr_t)pGVCpu->nem.s.HypercallData.pbPage < PAGE_SIZE); /* (max is around 168 registers) */
1503 RT_BZERO(paValues, cRegs * sizeof(paValues[0]));
1504
1505 /*
1506 * Make the hypercall.
1507 */
1508 uint64_t uResult = g_pfnHvlInvokeHypercall(HV_MAKE_CALL_INFO(HvCallGetVpRegisters, cRegs),
1509 pGVCpu->nem.s.HypercallData.HCPhysPage,
1510 pGVCpu->nem.s.HypercallData.HCPhysPage + cbInput);
1511 AssertLogRelMsgReturn(uResult == HV_MAKE_CALL_REP_RET(cRegs),
1512 ("uResult=%RX64 cRegs=%#x\n", uResult, cRegs),
1513 VERR_NEM_GET_REGISTERS_FAILED);
1514 //LogFlow(("nemR0WinImportState: uResult=%#RX64 iReg=%zu fWhat=%#018RX64 fExtr=%#018RX64\n", uResult, cRegs, fWhat, pCtx->fExtrn));
1515
1516 /*
1517 * Copy information to the CPUM context.
1518 */
1519 PVMCPU pVCpu = &pGVM->pVM->aCpus[pGVCpu->idCpu];
1520 iReg = 0;
1521
1522 /* GPRs */
1523 if (fWhat & CPUMCTX_EXTRN_GPRS_MASK)
1524 {
1525 if (fWhat & CPUMCTX_EXTRN_RAX)
1526 {
1527 Assert(pInput->Names[iReg] == HvX64RegisterRax);
1528 pCtx->rax = paValues[iReg++].Reg64;
1529 }
1530 if (fWhat & CPUMCTX_EXTRN_RCX)
1531 {
1532 Assert(pInput->Names[iReg] == HvX64RegisterRcx);
1533 pCtx->rcx = paValues[iReg++].Reg64;
1534 }
1535 if (fWhat & CPUMCTX_EXTRN_RDX)
1536 {
1537 Assert(pInput->Names[iReg] == HvX64RegisterRdx);
1538 pCtx->rdx = paValues[iReg++].Reg64;
1539 }
1540 if (fWhat & CPUMCTX_EXTRN_RBX)
1541 {
1542 Assert(pInput->Names[iReg] == HvX64RegisterRbx);
1543 pCtx->rbx = paValues[iReg++].Reg64;
1544 }
1545 if (fWhat & CPUMCTX_EXTRN_RSP)
1546 {
1547 Assert(pInput->Names[iReg] == HvX64RegisterRsp);
1548 pCtx->rsp = paValues[iReg++].Reg64;
1549 }
1550 if (fWhat & CPUMCTX_EXTRN_RBP)
1551 {
1552 Assert(pInput->Names[iReg] == HvX64RegisterRbp);
1553 pCtx->rbp = paValues[iReg++].Reg64;
1554 }
1555 if (fWhat & CPUMCTX_EXTRN_RSI)
1556 {
1557 Assert(pInput->Names[iReg] == HvX64RegisterRsi);
1558 pCtx->rsi = paValues[iReg++].Reg64;
1559 }
1560 if (fWhat & CPUMCTX_EXTRN_RDI)
1561 {
1562 Assert(pInput->Names[iReg] == HvX64RegisterRdi);
1563 pCtx->rdi = paValues[iReg++].Reg64;
1564 }
1565 if (fWhat & CPUMCTX_EXTRN_R8_R15)
1566 {
1567 Assert(pInput->Names[iReg] == HvX64RegisterR8);
1568 Assert(pInput->Names[iReg + 7] == HvX64RegisterR15);
1569 pCtx->r8 = paValues[iReg++].Reg64;
1570 pCtx->r9 = paValues[iReg++].Reg64;
1571 pCtx->r10 = paValues[iReg++].Reg64;
1572 pCtx->r11 = paValues[iReg++].Reg64;
1573 pCtx->r12 = paValues[iReg++].Reg64;
1574 pCtx->r13 = paValues[iReg++].Reg64;
1575 pCtx->r14 = paValues[iReg++].Reg64;
1576 pCtx->r15 = paValues[iReg++].Reg64;
1577 }
1578 }
1579
1580 /* RIP & Flags */
1581 if (fWhat & CPUMCTX_EXTRN_RIP)
1582 {
1583 Assert(pInput->Names[iReg] == HvX64RegisterRip);
1584 pCtx->rip = paValues[iReg++].Reg64;
1585 }
1586 if (fWhat & CPUMCTX_EXTRN_RFLAGS)
1587 {
1588 Assert(pInput->Names[iReg] == HvX64RegisterRflags);
1589 pCtx->rflags.u = paValues[iReg++].Reg64;
1590 }
1591
1592 /* Segments */
1593#define COPY_BACK_SEG(a_idx, a_enmName, a_SReg) \
1594 do { \
1595 Assert(pInput->Names[a_idx] == a_enmName); \
1596 (a_SReg).u64Base = paValues[a_idx].Segment.Base; \
1597 (a_SReg).u32Limit = paValues[a_idx].Segment.Limit; \
1598 (a_SReg).ValidSel = (a_SReg).Sel = paValues[a_idx].Segment.Selector; \
1599 (a_SReg).Attr.u = paValues[a_idx].Segment.Attributes; \
1600 (a_SReg).fFlags = CPUMSELREG_FLAGS_VALID; \
1601 } while (0)
1602 if (fWhat & CPUMCTX_EXTRN_SREG_MASK)
1603 {
1604 if (fWhat & CPUMCTX_EXTRN_CS)
1605 {
1606 COPY_BACK_SEG(iReg, HvX64RegisterCs, pCtx->cs);
1607 iReg++;
1608 }
1609 if (fWhat & CPUMCTX_EXTRN_ES)
1610 {
1611 COPY_BACK_SEG(iReg, HvX64RegisterEs, pCtx->es);
1612 iReg++;
1613 }
1614 if (fWhat & CPUMCTX_EXTRN_SS)
1615 {
1616 COPY_BACK_SEG(iReg, HvX64RegisterSs, pCtx->ss);
1617 iReg++;
1618 }
1619 if (fWhat & CPUMCTX_EXTRN_DS)
1620 {
1621 COPY_BACK_SEG(iReg, HvX64RegisterDs, pCtx->ds);
1622 iReg++;
1623 }
1624 if (fWhat & CPUMCTX_EXTRN_FS)
1625 {
1626 COPY_BACK_SEG(iReg, HvX64RegisterFs, pCtx->fs);
1627 iReg++;
1628 }
1629 if (fWhat & CPUMCTX_EXTRN_GS)
1630 {
1631 COPY_BACK_SEG(iReg, HvX64RegisterGs, pCtx->gs);
1632 iReg++;
1633 }
1634 }
1635 /* Descriptor tables and the task segment. */
1636 if (fWhat & CPUMCTX_EXTRN_TABLE_MASK)
1637 {
1638 if (fWhat & CPUMCTX_EXTRN_LDTR)
1639 {
1640 COPY_BACK_SEG(iReg, HvX64RegisterLdtr, pCtx->ldtr);
1641 iReg++;
1642 }
1643 if (fWhat & CPUMCTX_EXTRN_TR)
1644 {
1645 /* AMD-V likes loading TR with in AVAIL state, whereas intel insists on BUSY. So,
1646 avoid to trigger sanity assertions around the code, always fix this. */
1647 COPY_BACK_SEG(iReg, HvX64RegisterTr, pCtx->tr);
1648 switch (pCtx->tr.Attr.n.u4Type)
1649 {
1650 case X86_SEL_TYPE_SYS_386_TSS_BUSY:
1651 case X86_SEL_TYPE_SYS_286_TSS_BUSY:
1652 break;
1653 case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
1654 pCtx->tr.Attr.n.u4Type = X86_SEL_TYPE_SYS_386_TSS_BUSY;
1655 break;
1656 case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
1657 pCtx->tr.Attr.n.u4Type = X86_SEL_TYPE_SYS_286_TSS_BUSY;
1658 break;
1659 }
1660 iReg++;
1661 }
1662 if (fWhat & CPUMCTX_EXTRN_IDTR)
1663 {
1664 Assert(pInput->Names[iReg] == HvX64RegisterIdtr);
1665 pCtx->idtr.cbIdt = paValues[iReg].Table.Limit;
1666 pCtx->idtr.pIdt = paValues[iReg].Table.Base;
1667 iReg++;
1668 }
1669 if (fWhat & CPUMCTX_EXTRN_GDTR)
1670 {
1671 Assert(pInput->Names[iReg] == HvX64RegisterGdtr);
1672 pCtx->gdtr.cbGdt = paValues[iReg].Table.Limit;
1673 pCtx->gdtr.pGdt = paValues[iReg].Table.Base;
1674 iReg++;
1675 }
1676 }
1677
1678 /* Control registers. */
1679 bool fMaybeChangedMode = false;
1680 bool fFlushTlb = false;
1681 bool fFlushGlobalTlb = false;
1682 if (fWhat & CPUMCTX_EXTRN_CR_MASK)
1683 {
1684 if (fWhat & CPUMCTX_EXTRN_CR0)
1685 {
1686 Assert(pInput->Names[iReg] == HvX64RegisterCr0);
1687 if (pCtx->cr0 != paValues[iReg].Reg64)
1688 {
1689 CPUMSetGuestCR0(pVCpu, paValues[iReg].Reg64);
1690 fMaybeChangedMode = true;
1691 fFlushTlb = fFlushGlobalTlb = true; /// @todo fix this
1692 }
1693 iReg++;
1694 }
1695 if (fWhat & CPUMCTX_EXTRN_CR2)
1696 {
1697 Assert(pInput->Names[iReg] == HvX64RegisterCr2);
1698 pCtx->cr2 = paValues[iReg].Reg64;
1699 iReg++;
1700 }
1701 if (fWhat & CPUMCTX_EXTRN_CR3)
1702 {
1703 Assert(pInput->Names[iReg] == HvX64RegisterCr3);
1704 if (pCtx->cr3 != paValues[iReg].Reg64)
1705 {
1706 CPUMSetGuestCR3(pVCpu, paValues[iReg].Reg64);
1707 fFlushTlb = true;
1708 }
1709 iReg++;
1710 }
1711 if (fWhat & CPUMCTX_EXTRN_CR4)
1712 {
1713 Assert(pInput->Names[iReg] == HvX64RegisterCr4);
1714 if (pCtx->cr4 != paValues[iReg].Reg64)
1715 {
1716 CPUMSetGuestCR4(pVCpu, paValues[iReg].Reg64);
1717 fMaybeChangedMode = true;
1718 fFlushTlb = fFlushGlobalTlb = true; /// @todo fix this
1719 }
1720 iReg++;
1721 }
1722 }
1723
1724 /// @todo CR8/TPR
1725 Assert(pInput->Names[iReg] == HvX64RegisterCr8);
1726 APICSetTpr(pVCpu, (uint8_t)paValues[iReg].Reg64 << 4);
1727 iReg++;
1728
1729 /* Debug registers. */
1730/** @todo fixme */
1731 if (fWhat & CPUMCTX_EXTRN_DR0_DR3)
1732 {
1733 Assert(pInput->Names[iReg] == HvX64RegisterDr0);
1734 Assert(pInput->Names[iReg+3] == HvX64RegisterDr3);
1735 if (pCtx->dr[0] != paValues[iReg].Reg64)
1736 CPUMSetGuestDR0(pVCpu, paValues[iReg].Reg64);
1737 iReg++;
1738 if (pCtx->dr[1] != paValues[iReg].Reg64)
1739 CPUMSetGuestDR1(pVCpu, paValues[iReg].Reg64);
1740 iReg++;
1741 if (pCtx->dr[2] != paValues[iReg].Reg64)
1742 CPUMSetGuestDR2(pVCpu, paValues[iReg].Reg64);
1743 iReg++;
1744 if (pCtx->dr[3] != paValues[iReg].Reg64)
1745 CPUMSetGuestDR3(pVCpu, paValues[iReg].Reg64);
1746 iReg++;
1747 }
1748 if (fWhat & CPUMCTX_EXTRN_DR6)
1749 {
1750 Assert(pInput->Names[iReg] == HvX64RegisterDr6);
1751 if (pCtx->dr[6] != paValues[iReg].Reg64)
1752 CPUMSetGuestDR6(pVCpu, paValues[iReg].Reg64);
1753 iReg++;
1754 }
1755 if (fWhat & CPUMCTX_EXTRN_DR7)
1756 {
1757 Assert(pInput->Names[iReg] == HvX64RegisterDr7);
1758 if (pCtx->dr[7] != paValues[iReg].Reg64)
1759 CPUMSetGuestDR6(pVCpu, paValues[iReg].Reg64);
1760 iReg++;
1761 }
1762
1763 /* Floating point state. */
1764 if (fWhat & CPUMCTX_EXTRN_X87)
1765 {
1766 Assert(pInput->Names[iReg] == HvX64RegisterFpMmx0);
1767 Assert(pInput->Names[iReg + 7] == HvX64RegisterFpMmx7);
1768 pCtx->pXStateR0->x87.aRegs[0].au64[0] = paValues[iReg].Fp.AsUINT128.Low64;
1769 pCtx->pXStateR0->x87.aRegs[0].au64[1] = paValues[iReg].Fp.AsUINT128.High64;
1770 iReg++;
1771 pCtx->pXStateR0->x87.aRegs[1].au64[0] = paValues[iReg].Fp.AsUINT128.Low64;
1772 pCtx->pXStateR0->x87.aRegs[1].au64[1] = paValues[iReg].Fp.AsUINT128.High64;
1773 iReg++;
1774 pCtx->pXStateR0->x87.aRegs[2].au64[0] = paValues[iReg].Fp.AsUINT128.Low64;
1775 pCtx->pXStateR0->x87.aRegs[2].au64[1] = paValues[iReg].Fp.AsUINT128.High64;
1776 iReg++;
1777 pCtx->pXStateR0->x87.aRegs[3].au64[0] = paValues[iReg].Fp.AsUINT128.Low64;
1778 pCtx->pXStateR0->x87.aRegs[3].au64[1] = paValues[iReg].Fp.AsUINT128.High64;
1779 iReg++;
1780 pCtx->pXStateR0->x87.aRegs[4].au64[0] = paValues[iReg].Fp.AsUINT128.Low64;
1781 pCtx->pXStateR0->x87.aRegs[4].au64[1] = paValues[iReg].Fp.AsUINT128.High64;
1782 iReg++;
1783 pCtx->pXStateR0->x87.aRegs[5].au64[0] = paValues[iReg].Fp.AsUINT128.Low64;
1784 pCtx->pXStateR0->x87.aRegs[5].au64[1] = paValues[iReg].Fp.AsUINT128.High64;
1785 iReg++;
1786 pCtx->pXStateR0->x87.aRegs[6].au64[0] = paValues[iReg].Fp.AsUINT128.Low64;
1787 pCtx->pXStateR0->x87.aRegs[6].au64[1] = paValues[iReg].Fp.AsUINT128.High64;
1788 iReg++;
1789 pCtx->pXStateR0->x87.aRegs[7].au64[0] = paValues[iReg].Fp.AsUINT128.Low64;
1790 pCtx->pXStateR0->x87.aRegs[7].au64[1] = paValues[iReg].Fp.AsUINT128.High64;
1791 iReg++;
1792
1793 Assert(pInput->Names[iReg] == HvX64RegisterFpControlStatus);
1794 pCtx->pXStateR0->x87.FCW = paValues[iReg].FpControlStatus.FpControl;
1795 pCtx->pXStateR0->x87.FSW = paValues[iReg].FpControlStatus.FpStatus;
1796 pCtx->pXStateR0->x87.FTW = paValues[iReg].FpControlStatus.FpTag
1797 /*| (paValues[iReg].FpControlStatus.Reserved << 8)*/;
1798 pCtx->pXStateR0->x87.FOP = paValues[iReg].FpControlStatus.LastFpOp;
1799 pCtx->pXStateR0->x87.FPUIP = (uint32_t)paValues[iReg].FpControlStatus.LastFpRip;
1800 pCtx->pXStateR0->x87.CS = (uint16_t)(paValues[iReg].FpControlStatus.LastFpRip >> 32);
1801 pCtx->pXStateR0->x87.Rsrvd1 = (uint16_t)(paValues[iReg].FpControlStatus.LastFpRip >> 48);
1802 iReg++;
1803 }
1804
1805 if (fWhat & (CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX))
1806 {
1807 Assert(pInput->Names[iReg] == HvX64RegisterXmmControlStatus);
1808 if (fWhat & CPUMCTX_EXTRN_X87)
1809 {
1810 pCtx->pXStateR0->x87.FPUDP = (uint32_t)paValues[iReg].XmmControlStatus.LastFpRdp;
1811 pCtx->pXStateR0->x87.DS = (uint16_t)(paValues[iReg].XmmControlStatus.LastFpRdp >> 32);
1812 pCtx->pXStateR0->x87.Rsrvd2 = (uint16_t)(paValues[iReg].XmmControlStatus.LastFpRdp >> 48);
1813 }
1814 pCtx->pXStateR0->x87.MXCSR = paValues[iReg].XmmControlStatus.XmmStatusControl;
1815 pCtx->pXStateR0->x87.MXCSR_MASK = paValues[iReg].XmmControlStatus.XmmStatusControlMask; /** @todo ??? (Isn't this an output field?) */
1816 iReg++;
1817 }
1818
1819 /* Vector state. */
1820 if (fWhat & CPUMCTX_EXTRN_SSE_AVX)
1821 {
1822 Assert(pInput->Names[iReg] == HvX64RegisterXmm0);
1823 Assert(pInput->Names[iReg+15] == HvX64RegisterXmm15);
1824 pCtx->pXStateR0->x87.aXMM[0].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
1825 pCtx->pXStateR0->x87.aXMM[0].uXmm.s.Hi = paValues[iReg].Reg128.High64;
1826 iReg++;
1827 pCtx->pXStateR0->x87.aXMM[1].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
1828 pCtx->pXStateR0->x87.aXMM[1].uXmm.s.Hi = paValues[iReg].Reg128.High64;
1829 iReg++;
1830 pCtx->pXStateR0->x87.aXMM[2].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
1831 pCtx->pXStateR0->x87.aXMM[2].uXmm.s.Hi = paValues[iReg].Reg128.High64;
1832 iReg++;
1833 pCtx->pXStateR0->x87.aXMM[3].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
1834 pCtx->pXStateR0->x87.aXMM[3].uXmm.s.Hi = paValues[iReg].Reg128.High64;
1835 iReg++;
1836 pCtx->pXStateR0->x87.aXMM[4].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
1837 pCtx->pXStateR0->x87.aXMM[4].uXmm.s.Hi = paValues[iReg].Reg128.High64;
1838 iReg++;
1839 pCtx->pXStateR0->x87.aXMM[5].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
1840 pCtx->pXStateR0->x87.aXMM[5].uXmm.s.Hi = paValues[iReg].Reg128.High64;
1841 iReg++;
1842 pCtx->pXStateR0->x87.aXMM[6].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
1843 pCtx->pXStateR0->x87.aXMM[6].uXmm.s.Hi = paValues[iReg].Reg128.High64;
1844 iReg++;
1845 pCtx->pXStateR0->x87.aXMM[7].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
1846 pCtx->pXStateR0->x87.aXMM[7].uXmm.s.Hi = paValues[iReg].Reg128.High64;
1847 iReg++;
1848 pCtx->pXStateR0->x87.aXMM[8].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
1849 pCtx->pXStateR0->x87.aXMM[8].uXmm.s.Hi = paValues[iReg].Reg128.High64;
1850 iReg++;
1851 pCtx->pXStateR0->x87.aXMM[9].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
1852 pCtx->pXStateR0->x87.aXMM[9].uXmm.s.Hi = paValues[iReg].Reg128.High64;
1853 iReg++;
1854 pCtx->pXStateR0->x87.aXMM[10].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
1855 pCtx->pXStateR0->x87.aXMM[10].uXmm.s.Hi = paValues[iReg].Reg128.High64;
1856 iReg++;
1857 pCtx->pXStateR0->x87.aXMM[11].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
1858 pCtx->pXStateR0->x87.aXMM[11].uXmm.s.Hi = paValues[iReg].Reg128.High64;
1859 iReg++;
1860 pCtx->pXStateR0->x87.aXMM[12].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
1861 pCtx->pXStateR0->x87.aXMM[12].uXmm.s.Hi = paValues[iReg].Reg128.High64;
1862 iReg++;
1863 pCtx->pXStateR0->x87.aXMM[13].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
1864 pCtx->pXStateR0->x87.aXMM[13].uXmm.s.Hi = paValues[iReg].Reg128.High64;
1865 iReg++;
1866 pCtx->pXStateR0->x87.aXMM[14].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
1867 pCtx->pXStateR0->x87.aXMM[14].uXmm.s.Hi = paValues[iReg].Reg128.High64;
1868 iReg++;
1869 pCtx->pXStateR0->x87.aXMM[15].uXmm.s.Lo = paValues[iReg].Reg128.Low64;
1870 pCtx->pXStateR0->x87.aXMM[15].uXmm.s.Hi = paValues[iReg].Reg128.High64;
1871 iReg++;
1872 }
1873
1874
1875 /* MSRs */
1876 // HvX64RegisterTsc - don't touch
1877 if (fWhat & CPUMCTX_EXTRN_EFER)
1878 {
1879 Assert(pInput->Names[iReg] == HvX64RegisterEfer);
1880 if (paValues[iReg].Reg64 != pCtx->msrEFER)
1881 {
1882 Log7(("NEM/%u: MSR EFER changed %RX64 -> %RX64\n", pVCpu->idCpu, pCtx->msrEFER, paValues[iReg].Reg64));
1883 if ((paValues[iReg].Reg64 ^ pCtx->msrEFER) & MSR_K6_EFER_NXE)
1884 PGMNotifyNxeChanged(pVCpu, RT_BOOL(paValues[iReg].Reg64 & MSR_K6_EFER_NXE));
1885 pCtx->msrEFER = paValues[iReg].Reg64;
1886 fMaybeChangedMode = true;
1887 }
1888 iReg++;
1889 }
1890 if (fWhat & CPUMCTX_EXTRN_KERNEL_GS_BASE)
1891 {
1892 Assert(pInput->Names[iReg] == HvX64RegisterKernelGsBase);
1893 if (pCtx->msrKERNELGSBASE != paValues[iReg].Reg64)
1894 Log7(("NEM/%u: MSR KERNELGSBASE changed %RX64 -> %RX64\n", pVCpu->idCpu, pCtx->msrKERNELGSBASE, paValues[iReg].Reg64));
1895 pCtx->msrKERNELGSBASE = paValues[iReg].Reg64;
1896 iReg++;
1897 }
1898 if (fWhat & CPUMCTX_EXTRN_SYSENTER_MSRS)
1899 {
1900 Assert(pInput->Names[iReg] == HvX64RegisterSysenterCs);
1901 if (pCtx->SysEnter.cs != paValues[iReg].Reg64)
1902 Log7(("NEM/%u: MSR SYSENTER.CS changed %RX64 -> %RX64\n", pVCpu->idCpu, pCtx->SysEnter.cs, paValues[iReg].Reg64));
1903 pCtx->SysEnter.cs = paValues[iReg].Reg64;
1904 iReg++;
1905
1906 Assert(pInput->Names[iReg] == HvX64RegisterSysenterEip);
1907 if (pCtx->SysEnter.eip != paValues[iReg].Reg64)
1908 Log7(("NEM/%u: MSR SYSENTER.EIP changed %RX64 -> %RX64\n", pVCpu->idCpu, pCtx->SysEnter.eip, paValues[iReg].Reg64));
1909 pCtx->SysEnter.eip = paValues[iReg].Reg64;
1910 iReg++;
1911
1912 Assert(pInput->Names[iReg] == HvX64RegisterSysenterEsp);
1913 if (pCtx->SysEnter.esp != paValues[iReg].Reg64)
1914 Log7(("NEM/%u: MSR SYSENTER.ESP changed %RX64 -> %RX64\n", pVCpu->idCpu, pCtx->SysEnter.esp, paValues[iReg].Reg64));
1915 pCtx->SysEnter.esp = paValues[iReg].Reg64;
1916 iReg++;
1917 }
1918 if (fWhat & CPUMCTX_EXTRN_SYSCALL_MSRS)
1919 {
1920 Assert(pInput->Names[iReg] == HvX64RegisterStar);
1921 if (pCtx->msrSTAR != paValues[iReg].Reg64)
1922 Log7(("NEM/%u: MSR STAR changed %RX64 -> %RX64\n", pVCpu->idCpu, pCtx->msrSTAR, paValues[iReg].Reg64));
1923 pCtx->msrSTAR = paValues[iReg].Reg64;
1924 iReg++;
1925
1926 Assert(pInput->Names[iReg] == HvX64RegisterLstar);
1927 if (pCtx->msrLSTAR != paValues[iReg].Reg64)
1928 Log7(("NEM/%u: MSR LSTAR changed %RX64 -> %RX64\n", pVCpu->idCpu, pCtx->msrLSTAR, paValues[iReg].Reg64));
1929 pCtx->msrLSTAR = paValues[iReg].Reg64;
1930 iReg++;
1931
1932 Assert(pInput->Names[iReg] == HvX64RegisterCstar);
1933 if (pCtx->msrCSTAR != paValues[iReg].Reg64)
1934 Log7(("NEM/%u: MSR CSTAR changed %RX64 -> %RX64\n", pVCpu->idCpu, pCtx->msrCSTAR, paValues[iReg].Reg64));
1935 pCtx->msrCSTAR = paValues[iReg].Reg64;
1936 iReg++;
1937
1938 Assert(pInput->Names[iReg] == HvX64RegisterSfmask);
1939 if (pCtx->msrSFMASK != paValues[iReg].Reg64)
1940 Log7(("NEM/%u: MSR SFMASK changed %RX64 -> %RX64\n", pVCpu->idCpu, pCtx->msrSFMASK, paValues[iReg].Reg64));
1941 pCtx->msrSFMASK = paValues[iReg].Reg64;
1942 iReg++;
1943 }
1944 if (fWhat & CPUMCTX_EXTRN_OTHER_MSRS)
1945 {
1946 Assert(pInput->Names[iReg] == HvX64RegisterApicBase);
1947 const uint64_t uOldBase = APICGetBaseMsrNoCheck(pVCpu);
1948 if (paValues[iReg].Reg64 != uOldBase)
1949 {
1950 Log7(("NEM/%u: MSR APICBase changed %RX64 -> %RX64 (%RX64)\n",
1951 pVCpu->idCpu, uOldBase, paValues[iReg].Reg64, paValues[iReg].Reg64 ^ uOldBase));
1952 VBOXSTRICTRC rc2 = APICSetBaseMsr(pVCpu, paValues[iReg].Reg64);
1953 Assert(rc2 == VINF_SUCCESS); NOREF(rc2);
1954 }
1955 iReg++;
1956
1957 Assert(pInput->Names[iReg] == HvX64RegisterPat);
1958 if (pCtx->msrPAT != paValues[iReg].Reg64)
1959 Log7(("NEM/%u: MSR PAT changed %RX64 -> %RX64\n", pVCpu->idCpu, pCtx->msrPAT, paValues[iReg].Reg64));
1960 pCtx->msrPAT = paValues[iReg].Reg64;
1961 iReg++;
1962
1963#if 0 /*def LOG_ENABLED*/ /** @todo something's wrong with HvX64RegisterMtrrCap? (AMD) */
1964 Assert(pInput->Names[iReg] == HvX64RegisterMtrrCap);
1965 if (paValues[iReg].Reg64 != CPUMGetGuestIa32MtrrCap(pVCpu))
1966 Log7(("NEM/%u: MSR MTRR_CAP changed %RX64 -> %RX64 (!!)\n", pVCpu->idCpu, CPUMGetGuestIa32MtrrCap(pVCpu), paValues[iReg].Reg64));
1967 iReg++;
1968#endif
1969
1970 PCPUMCTXMSRS pCtxMsrs = CPUMQueryGuestCtxMsrsPtr(pVCpu);
1971 Assert(pInput->Names[iReg] == HvX64RegisterMtrrDefType);
1972 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrDefType )
1973 Log7(("NEM/%u: MSR MTRR_DEF_TYPE changed %RX64 -> %RX64\n", pVCpu->idCpu, pCtxMsrs->msr.MtrrDefType, paValues[iReg].Reg64));
1974 pCtxMsrs->msr.MtrrDefType = paValues[iReg].Reg64;
1975 iReg++;
1976
1977 /** @todo we dont keep state for HvX64RegisterMtrrPhysBaseX and HvX64RegisterMtrrPhysMaskX */
1978
1979 Assert(pInput->Names[iReg] == HvX64RegisterMtrrFix64k00000);
1980 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrFix64K_00000 )
1981 Log7(("NEM/%u: MSR MTRR_FIX16K_00000 changed %RX64 -> %RX64\n", pVCpu->idCpu, pCtxMsrs->msr.MtrrFix64K_00000, paValues[iReg].Reg64));
1982 pCtxMsrs->msr.MtrrFix64K_00000 = paValues[iReg].Reg64;
1983 iReg++;
1984
1985 Assert(pInput->Names[iReg] == HvX64RegisterMtrrFix16k80000);
1986 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrFix16K_80000 )
1987 Log7(("NEM/%u: MSR MTRR_FIX16K_80000 changed %RX64 -> %RX64\n", pVCpu->idCpu, pCtxMsrs->msr.MtrrFix16K_80000, paValues[iReg].Reg64));
1988 pCtxMsrs->msr.MtrrFix16K_80000 = paValues[iReg].Reg64;
1989 iReg++;
1990
1991 Assert(pInput->Names[iReg] == HvX64RegisterMtrrFix16kA0000);
1992 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrFix16K_A0000 )
1993 Log7(("NEM/%u: MSR MTRR_FIX16K_A0000 changed %RX64 -> %RX64\n", pVCpu->idCpu, pCtxMsrs->msr.MtrrFix16K_A0000, paValues[iReg].Reg64));
1994 pCtxMsrs->msr.MtrrFix16K_A0000 = paValues[iReg].Reg64;
1995 iReg++;
1996
1997 Assert(pInput->Names[iReg] == HvX64RegisterMtrrFix4kC0000);
1998 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrFix4K_C0000 )
1999 Log7(("NEM/%u: MSR MTRR_FIX16K_C0000 changed %RX64 -> %RX64\n", pVCpu->idCpu, pCtxMsrs->msr.MtrrFix4K_C0000, paValues[iReg].Reg64));
2000 pCtxMsrs->msr.MtrrFix4K_C0000 = paValues[iReg].Reg64;
2001 iReg++;
2002
2003 Assert(pInput->Names[iReg] == HvX64RegisterMtrrFix4kC8000);
2004 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrFix4K_C8000 )
2005 Log7(("NEM/%u: MSR MTRR_FIX16K_C8000 changed %RX64 -> %RX64\n", pVCpu->idCpu, pCtxMsrs->msr.MtrrFix4K_C8000, paValues[iReg].Reg64));
2006 pCtxMsrs->msr.MtrrFix4K_C8000 = paValues[iReg].Reg64;
2007 iReg++;
2008
2009 Assert(pInput->Names[iReg] == HvX64RegisterMtrrFix4kD0000);
2010 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrFix4K_D0000 )
2011 Log7(("NEM/%u: MSR MTRR_FIX16K_D0000 changed %RX64 -> %RX64\n", pVCpu->idCpu, pCtxMsrs->msr.MtrrFix4K_D0000, paValues[iReg].Reg64));
2012 pCtxMsrs->msr.MtrrFix4K_D0000 = paValues[iReg].Reg64;
2013 iReg++;
2014
2015 Assert(pInput->Names[iReg] == HvX64RegisterMtrrFix4kD8000);
2016 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrFix4K_D8000 )
2017 Log7(("NEM/%u: MSR MTRR_FIX16K_D8000 changed %RX64 -> %RX64\n", pVCpu->idCpu, pCtxMsrs->msr.MtrrFix4K_D8000, paValues[iReg].Reg64));
2018 pCtxMsrs->msr.MtrrFix4K_D8000 = paValues[iReg].Reg64;
2019 iReg++;
2020
2021 Assert(pInput->Names[iReg] == HvX64RegisterMtrrFix4kE0000);
2022 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrFix4K_E0000 )
2023 Log7(("NEM/%u: MSR MTRR_FIX16K_E0000 changed %RX64 -> %RX64\n", pVCpu->idCpu, pCtxMsrs->msr.MtrrFix4K_E0000, paValues[iReg].Reg64));
2024 pCtxMsrs->msr.MtrrFix4K_E0000 = paValues[iReg].Reg64;
2025 iReg++;
2026
2027 Assert(pInput->Names[iReg] == HvX64RegisterMtrrFix4kE8000);
2028 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrFix4K_E8000 )
2029 Log7(("NEM/%u: MSR MTRR_FIX16K_E8000 changed %RX64 -> %RX64\n", pVCpu->idCpu, pCtxMsrs->msr.MtrrFix4K_E8000, paValues[iReg].Reg64));
2030 pCtxMsrs->msr.MtrrFix4K_E8000 = paValues[iReg].Reg64;
2031 iReg++;
2032
2033 Assert(pInput->Names[iReg] == HvX64RegisterMtrrFix4kF0000);
2034 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrFix4K_F0000 )
2035 Log7(("NEM/%u: MSR MTRR_FIX16K_F0000 changed %RX64 -> %RX64\n", pVCpu->idCpu, pCtxMsrs->msr.MtrrFix4K_F0000, paValues[iReg].Reg64));
2036 pCtxMsrs->msr.MtrrFix4K_F0000 = paValues[iReg].Reg64;
2037 iReg++;
2038
2039 Assert(pInput->Names[iReg] == HvX64RegisterMtrrFix4kF8000);
2040 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MtrrFix4K_F8000 )
2041 Log7(("NEM/%u: MSR MTRR_FIX16K_F8000 changed %RX64 -> %RX64\n", pVCpu->idCpu, pCtxMsrs->msr.MtrrFix4K_F8000, paValues[iReg].Reg64));
2042 pCtxMsrs->msr.MtrrFix4K_F8000 = paValues[iReg].Reg64;
2043 iReg++;
2044
2045 if (enmCpuVendor != CPUMCPUVENDOR_AMD)
2046 {
2047 Assert(pInput->Names[iReg] == HvX64RegisterIa32MiscEnable);
2048 if (paValues[iReg].Reg64 != pCtxMsrs->msr.MiscEnable)
2049 Log7(("NEM/%u: MSR MISC_ENABLE changed %RX64 -> %RX64\n", pVCpu->idCpu, pCtxMsrs->msr.MiscEnable, paValues[iReg].Reg64));
2050 pCtxMsrs->msr.MiscEnable = paValues[iReg].Reg64;
2051 iReg++;
2052#ifdef LOG_ENABLED
2053 Assert(pInput->Names[iReg] == HvX64RegisterIa32FeatureControl);
2054 if (paValues[iReg].Reg64 != CPUMGetGuestIa32FeatureControl(pVCpu))
2055 Log7(("NEM/%u: MSR FEATURE_CONTROL changed %RX64 -> %RX64 (!!)\n", pVCpu->idCpu, CPUMGetGuestIa32FeatureControl(pVCpu), paValues[iReg].Reg64));
2056 iReg++;
2057#endif
2058 }
2059
2060 /** @todo we don't save state for HvX64RegisterIa32FeatureControl */
2061 }
2062
2063 /* Interruptibility. */
2064 if (fWhat & (CPUMCTX_EXTRN_NEM_WIN_INHIBIT_INT | CPUMCTX_EXTRN_NEM_WIN_INHIBIT_NMI))
2065 {
2066 Assert(pInput->Names[iReg] == HvRegisterInterruptState);
2067 Assert(pInput->Names[iReg + 1] == HvX64RegisterRip);
2068
2069 if (!(pCtx->fExtrn & CPUMCTX_EXTRN_NEM_WIN_INHIBIT_INT))
2070 {
2071 pVCpu->nem.s.fLastInterruptShadow = paValues[iReg].InterruptState.InterruptShadow;
2072 if (paValues[iReg].InterruptState.InterruptShadow)
2073 {
2074 EMSetInhibitInterruptsPC(pVCpu, paValues[iReg + 1].Reg64);
2075 VMCPU_FF_SET(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS);
2076 }
2077 else
2078 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS);
2079 }
2080
2081 if (!(pCtx->fExtrn & CPUMCTX_EXTRN_NEM_WIN_INHIBIT_NMI))
2082 {
2083 if (paValues[iReg].InterruptState.NmiMasked)
2084 VMCPU_FF_SET(pVCpu, VMCPU_FF_BLOCK_NMIS);
2085 else
2086 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_BLOCK_NMIS);
2087 }
2088
2089 fWhat |= CPUMCTX_EXTRN_NEM_WIN_INHIBIT_INT | CPUMCTX_EXTRN_NEM_WIN_INHIBIT_NMI;
2090 iReg += 2;
2091 }
2092
2093 /* Event injection. */
2094 /// @todo HvRegisterPendingInterruption
2095 Assert(pInput->Names[iReg] == HvRegisterPendingInterruption);
2096 if (paValues[iReg].PendingInterruption.InterruptionPending)
2097 {
2098 Log7(("PendingInterruption: type=%u vector=%#x errcd=%RTbool/%#x instr-len=%u nested=%u\n",
2099 paValues[iReg].PendingInterruption.InterruptionType, paValues[iReg].PendingInterruption.InterruptionVector,
2100 paValues[iReg].PendingInterruption.DeliverErrorCode, paValues[iReg].PendingInterruption.ErrorCode,
2101 paValues[iReg].PendingInterruption.InstructionLength, paValues[iReg].PendingInterruption.NestedEvent));
2102 AssertMsg((paValues[iReg].PendingInterruption.AsUINT64 & UINT64_C(0xfc00)) == 0,
2103 ("%#RX64\n", paValues[iReg].PendingInterruption.AsUINT64));
2104 }
2105
2106 /// @todo HvRegisterPendingEvent0
2107 /// @todo HvRegisterPendingEvent1
2108
2109 /* Almost done, just update extrn flags and maybe change PGM mode. */
2110 pCtx->fExtrn &= ~fWhat;
2111
2112 /* Typical. */
2113 if (!fMaybeChangedMode && !fFlushTlb)
2114 return VINF_SUCCESS;
2115
2116 /*
2117 * Slow.
2118 */
2119 int rc = VINF_SUCCESS;
2120 if (fMaybeChangedMode)
2121 {
2122 rc = PGMChangeMode(pVCpu, pCtx->cr0, pCtx->cr4, pCtx->msrEFER);
2123 if (rc == VINF_PGM_CHANGE_MODE)
2124 {
2125 LogFlow(("nemR0WinImportState: -> VERR_NEM_CHANGE_PGM_MODE!\n"));
2126 return VERR_NEM_CHANGE_PGM_MODE;
2127 }
2128 AssertMsg(rc == VINF_SUCCESS, ("rc=%Rrc\n", rc));
2129 }
2130
2131 if (fFlushTlb)
2132 {
2133 LogFlow(("nemR0WinImportState: -> VERR_NEM_FLUSH_TLB!\n"));
2134 rc = VERR_NEM_FLUSH_TLB; /* Calling PGMFlushTLB w/o long jump setup doesn't work, ring-3 does it. */
2135 }
2136
2137 return rc;
2138}
2139
2140
2141/**
2142 * Import the state from the native API (back to CPUMCTX).
2143 *
2144 * @returns VBox status code
2145 * @param pGVM The ring-0 VM handle.
2146 * @param pVM The cross context VM handle.
2147 * @param idCpu The calling EMT. Necessary for getting the
2148 * hypercall page and arguments.
2149 * @param fWhat What to import, CPUMCTX_EXTRN_XXX. Set
2150 * CPUMCTX_EXTERN_ALL for everything.
2151 */
2152VMMR0_INT_DECL(int) NEMR0ImportState(PGVM pGVM, PVM pVM, VMCPUID idCpu, uint64_t fWhat)
2153{
2154 /*
2155 * Validate the call.
2156 */
2157 int rc = GVMMR0ValidateGVMandVMandEMT(pGVM, pVM, idCpu);
2158 if (RT_SUCCESS(rc))
2159 {
2160 PVMCPU pVCpu = &pVM->aCpus[idCpu];
2161 PGVMCPU pGVCpu = &pGVM->aCpus[idCpu];
2162 AssertReturn(g_pfnHvlInvokeHypercall, VERR_NEM_MISSING_KERNEL_API);
2163
2164 /*
2165 * Call worker.
2166 */
2167 rc = nemR0WinImportState(pGVM, pGVCpu, CPUMQueryGuestCtxPtr(pVCpu), fWhat);
2168 }
2169 return rc;
2170}
2171
2172
2173VMMR0_INT_DECL(VBOXSTRICTRC) NEMR0RunGuestCode(PGVM pGVM, VMCPUID idCpu)
2174{
2175#ifdef NEM_WIN_USE_OUR_OWN_RUN_API
2176 PVM pVM = pGVM->pVM;
2177 return nemHCWinRunGC(pVM, &pVM->aCpus[idCpu], pGVM, &pGVM->aCpus[idCpu]);
2178#else
2179 RT_NOREF(pGVM, idCpu);
2180 return VERR_NOT_IMPLEMENTED;
2181#endif
2182}
2183
2184
2185/**
2186 * Updates statistics in the VM structure.
2187 *
2188 * @returns VBox status code.
2189 * @param pGVM The ring-0 VM handle.
2190 * @param pVM The cross context VM handle.
2191 * @param idCpu The calling EMT, or NIL. Necessary for getting the hypercall
2192 * page and arguments.
2193 */
2194VMMR0_INT_DECL(int) NEMR0UpdateStatistics(PGVM pGVM, PVM pVM, VMCPUID idCpu)
2195{
2196 /*
2197 * Validate the call.
2198 */
2199 int rc;
2200 if (idCpu == NIL_VMCPUID)
2201 rc = GVMMR0ValidateGVMandVM(pGVM, pVM);
2202 else
2203 rc = GVMMR0ValidateGVMandVMandEMT(pGVM, pVM, idCpu);
2204 if (RT_SUCCESS(rc))
2205 {
2206 AssertReturn(g_pfnHvlInvokeHypercall, VERR_NEM_MISSING_KERNEL_API);
2207
2208 PNEMR0HYPERCALLDATA pHypercallData = idCpu != NIL_VMCPUID
2209 ? &pGVM->aCpus[idCpu].nem.s.HypercallData
2210 : &pGVM->nem.s.HypercallData;
2211 if ( RT_VALID_PTR(pHypercallData->pbPage)
2212 && pHypercallData->HCPhysPage != NIL_RTHCPHYS)
2213 {
2214 if (idCpu == NIL_VMCPUID)
2215 rc = RTCritSectEnter(&pGVM->nem.s.HypercallDataCritSect);
2216 if (RT_SUCCESS(rc))
2217 {
2218 /*
2219 * Query the memory statistics for the partition.
2220 */
2221 HV_INPUT_GET_MEMORY_BALANCE *pInput = (HV_INPUT_GET_MEMORY_BALANCE *)pHypercallData->pbPage;
2222 pInput->TargetPartitionId = pGVM->nem.s.idHvPartition;
2223 pInput->ProximityDomainInfo.Flags.ProximityPreferred = 0;
2224 pInput->ProximityDomainInfo.Flags.ProxyimityInfoValid = 0;
2225 pInput->ProximityDomainInfo.Flags.Reserved = 0;
2226 pInput->ProximityDomainInfo.Id = 0;
2227
2228 HV_OUTPUT_GET_MEMORY_BALANCE *pOutput = (HV_OUTPUT_GET_MEMORY_BALANCE *)(pInput + 1);
2229 RT_ZERO(*pOutput);
2230
2231 uint64_t uResult = g_pfnHvlInvokeHypercall(HvCallGetMemoryBalance,
2232 pHypercallData->HCPhysPage,
2233 pHypercallData->HCPhysPage + sizeof(*pInput));
2234 if (uResult == HV_STATUS_SUCCESS)
2235 {
2236 pVM->nem.s.R0Stats.cPagesAvailable = pOutput->PagesAvailable;
2237 pVM->nem.s.R0Stats.cPagesInUse = pOutput->PagesInUse;
2238 rc = VINF_SUCCESS;
2239 }
2240 else
2241 {
2242 LogRel(("HvCallGetMemoryBalance -> %#RX64 (%#RX64 %#RX64)!!\n",
2243 uResult, pOutput->PagesAvailable, pOutput->PagesInUse));
2244 rc = VINF_NEM_IPE_0;
2245 }
2246
2247 if (idCpu == NIL_VMCPUID)
2248 RTCritSectLeave(&pGVM->nem.s.HypercallDataCritSect);
2249 }
2250 }
2251 else
2252 rc = VERR_WRONG_ORDER;
2253 }
2254 return rc;
2255}
2256
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