VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/NEMR3Native-win.cpp@ 74791

Last change on this file since 74791 was 74791, checked in by vboxsync, 6 years ago

vm.h,VMM,REM: s/VM_FF_IS_PENDING/VM_FF_IS_ANY_SET/g to emphasize the plurality of the flags argument and encourage using VM_FF_IS_SET. bugref:9180

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 135.7 KB
Line 
1/* $Id: NEMR3Native-win.cpp 74791 2018-10-12 10:44:17Z vboxsync $ */
2/** @file
3 * NEM - Native execution manager, native ring-3 Windows backend.
4 *
5 * Log group 2: Exit logging.
6 * Log group 3: Log context on exit.
7 * Log group 5: Ring-3 memory management
8 * Log group 6: Ring-0 memory management
9 * Log group 12: API intercepts.
10 */
11
12/*
13 * Copyright (C) 2018 Oracle Corporation
14 *
15 * This file is part of VirtualBox Open Source Edition (OSE), as
16 * available from http://www.virtualbox.org. This file is free software;
17 * you can redistribute it and/or modify it under the terms of the GNU
18 * General Public License (GPL) as published by the Free Software
19 * Foundation, in version 2 as it comes in the "COPYING" file of the
20 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
21 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
22 */
23
24
25/*********************************************************************************************************************************
26* Header Files *
27*********************************************************************************************************************************/
28#define LOG_GROUP LOG_GROUP_NEM
29#define VMCPU_INCL_CPUM_GST_CTX
30#include <iprt/nt/nt-and-windows.h>
31#include <iprt/nt/hyperv.h>
32#include <iprt/nt/vid.h>
33#include <WinHvPlatform.h>
34
35#ifndef _WIN32_WINNT_WIN10
36# error "Missing _WIN32_WINNT_WIN10"
37#endif
38#ifndef _WIN32_WINNT_WIN10_RS1 /* Missing define, causing trouble for us. */
39# define _WIN32_WINNT_WIN10_RS1 (_WIN32_WINNT_WIN10 + 1)
40#endif
41#include <sysinfoapi.h>
42#include <debugapi.h>
43#include <errhandlingapi.h>
44#include <fileapi.h>
45#include <winerror.h> /* no api header for this. */
46
47#include <VBox/vmm/nem.h>
48#include <VBox/vmm/iem.h>
49#include <VBox/vmm/em.h>
50#include <VBox/vmm/apic.h>
51#include <VBox/vmm/pdm.h>
52#include <VBox/vmm/dbgftrace.h>
53#include "NEMInternal.h"
54#include <VBox/vmm/vm.h>
55
56#include <iprt/ldr.h>
57#include <iprt/path.h>
58#include <iprt/string.h>
59#include <iprt/system.h>
60
61
62/*********************************************************************************************************************************
63* Defined Constants And Macros *
64*********************************************************************************************************************************/
65#ifdef LOG_ENABLED
66# define NEM_WIN_INTERCEPT_NT_IO_CTLS
67#endif
68
69/** VID I/O control detection: Fake partition handle input. */
70#define NEM_WIN_IOCTL_DETECTOR_FAKE_HANDLE ((HANDLE)(uintptr_t)38479125)
71/** VID I/O control detection: Fake partition ID return. */
72#define NEM_WIN_IOCTL_DETECTOR_FAKE_PARTITION_ID UINT64_C(0xfa1e000042424242)
73/** VID I/O control detection: Fake CPU index input. */
74#define NEM_WIN_IOCTL_DETECTOR_FAKE_VP_INDEX UINT32_C(42)
75/** VID I/O control detection: Fake timeout input. */
76#define NEM_WIN_IOCTL_DETECTOR_FAKE_TIMEOUT UINT32_C(0x00080286)
77
78
79/*********************************************************************************************************************************
80* Global Variables *
81*********************************************************************************************************************************/
82/** @name APIs imported from WinHvPlatform.dll
83 * @{ */
84static decltype(WHvGetCapability) * g_pfnWHvGetCapability;
85static decltype(WHvCreatePartition) * g_pfnWHvCreatePartition;
86static decltype(WHvSetupPartition) * g_pfnWHvSetupPartition;
87static decltype(WHvDeletePartition) * g_pfnWHvDeletePartition;
88static decltype(WHvGetPartitionProperty) * g_pfnWHvGetPartitionProperty;
89static decltype(WHvSetPartitionProperty) * g_pfnWHvSetPartitionProperty;
90static decltype(WHvMapGpaRange) * g_pfnWHvMapGpaRange;
91static decltype(WHvUnmapGpaRange) * g_pfnWHvUnmapGpaRange;
92static decltype(WHvTranslateGva) * g_pfnWHvTranslateGva;
93#ifndef NEM_WIN_USE_OUR_OWN_RUN_API
94static decltype(WHvCreateVirtualProcessor) * g_pfnWHvCreateVirtualProcessor;
95static decltype(WHvDeleteVirtualProcessor) * g_pfnWHvDeleteVirtualProcessor;
96static decltype(WHvRunVirtualProcessor) * g_pfnWHvRunVirtualProcessor;
97static decltype(WHvCancelRunVirtualProcessor) * g_pfnWHvCancelRunVirtualProcessor;
98static decltype(WHvGetVirtualProcessorRegisters) * g_pfnWHvGetVirtualProcessorRegisters;
99static decltype(WHvSetVirtualProcessorRegisters) * g_pfnWHvSetVirtualProcessorRegisters;
100#endif
101/** @} */
102
103/** @name APIs imported from Vid.dll
104 * @{ */
105static decltype(VidGetHvPartitionId) *g_pfnVidGetHvPartitionId;
106static decltype(VidStartVirtualProcessor) *g_pfnVidStartVirtualProcessor;
107static decltype(VidStopVirtualProcessor) *g_pfnVidStopVirtualProcessor;
108static decltype(VidMessageSlotMap) *g_pfnVidMessageSlotMap;
109static decltype(VidMessageSlotHandleAndGetNext) *g_pfnVidMessageSlotHandleAndGetNext;
110#ifdef LOG_ENABLED
111static decltype(VidGetVirtualProcessorState) *g_pfnVidGetVirtualProcessorState;
112static decltype(VidSetVirtualProcessorState) *g_pfnVidSetVirtualProcessorState;
113static decltype(VidGetVirtualProcessorRunningStatus) *g_pfnVidGetVirtualProcessorRunningStatus;
114#endif
115/** @} */
116
117/** The Windows build number. */
118static uint32_t g_uBuildNo = 17134;
119
120
121
122/**
123 * Import instructions.
124 */
125static const struct
126{
127 uint8_t idxDll; /**< 0 for WinHvPlatform.dll, 1 for vid.dll. */
128 bool fOptional; /**< Set if import is optional. */
129 PFNRT *ppfn; /**< The function pointer variable. */
130 const char *pszName; /**< The function name. */
131} g_aImports[] =
132{
133#define NEM_WIN_IMPORT(a_idxDll, a_fOptional, a_Name) { (a_idxDll), (a_fOptional), (PFNRT *)&RT_CONCAT(g_pfn,a_Name), #a_Name }
134 NEM_WIN_IMPORT(0, false, WHvGetCapability),
135 NEM_WIN_IMPORT(0, false, WHvCreatePartition),
136 NEM_WIN_IMPORT(0, false, WHvSetupPartition),
137 NEM_WIN_IMPORT(0, false, WHvDeletePartition),
138 NEM_WIN_IMPORT(0, false, WHvGetPartitionProperty),
139 NEM_WIN_IMPORT(0, false, WHvSetPartitionProperty),
140 NEM_WIN_IMPORT(0, false, WHvMapGpaRange),
141 NEM_WIN_IMPORT(0, false, WHvUnmapGpaRange),
142 NEM_WIN_IMPORT(0, false, WHvTranslateGva),
143#ifndef NEM_WIN_USE_OUR_OWN_RUN_API
144 NEM_WIN_IMPORT(0, false, WHvCreateVirtualProcessor),
145 NEM_WIN_IMPORT(0, false, WHvDeleteVirtualProcessor),
146 NEM_WIN_IMPORT(0, false, WHvRunVirtualProcessor),
147 NEM_WIN_IMPORT(0, false, WHvCancelRunVirtualProcessor),
148 NEM_WIN_IMPORT(0, false, WHvGetVirtualProcessorRegisters),
149 NEM_WIN_IMPORT(0, false, WHvSetVirtualProcessorRegisters),
150#endif
151 NEM_WIN_IMPORT(1, false, VidGetHvPartitionId),
152 NEM_WIN_IMPORT(1, false, VidMessageSlotMap),
153 NEM_WIN_IMPORT(1, false, VidMessageSlotHandleAndGetNext),
154 NEM_WIN_IMPORT(1, false, VidStartVirtualProcessor),
155 NEM_WIN_IMPORT(1, false, VidStopVirtualProcessor),
156#ifdef LOG_ENABLED
157 NEM_WIN_IMPORT(1, false, VidGetVirtualProcessorState),
158 NEM_WIN_IMPORT(1, false, VidSetVirtualProcessorState),
159 NEM_WIN_IMPORT(1, false, VidGetVirtualProcessorRunningStatus),
160#endif
161#undef NEM_WIN_IMPORT
162};
163
164
165/** The real NtDeviceIoControlFile API in NTDLL. */
166static decltype(NtDeviceIoControlFile) *g_pfnNtDeviceIoControlFile;
167/** Pointer to the NtDeviceIoControlFile import table entry. */
168static decltype(NtDeviceIoControlFile) **g_ppfnVidNtDeviceIoControlFile;
169/** Info about the VidGetHvPartitionId I/O control interface. */
170static NEMWINIOCTL g_IoCtlGetHvPartitionId;
171/** Info about the VidStartVirtualProcessor I/O control interface. */
172static NEMWINIOCTL g_IoCtlStartVirtualProcessor;
173/** Info about the VidStopVirtualProcessor I/O control interface. */
174static NEMWINIOCTL g_IoCtlStopVirtualProcessor;
175/** Info about the VidMessageSlotHandleAndGetNext I/O control interface. */
176static NEMWINIOCTL g_IoCtlMessageSlotHandleAndGetNext;
177#ifdef LOG_ENABLED
178/** Info about the VidMessageSlotMap I/O control interface - for logging. */
179static NEMWINIOCTL g_IoCtlMessageSlotMap;
180/* Info about the VidGetVirtualProcessorState I/O control interface - for logging. */
181static NEMWINIOCTL g_IoCtlGetVirtualProcessorState;
182/* Info about the VidSetVirtualProcessorState I/O control interface - for logging. */
183static NEMWINIOCTL g_IoCtlSetVirtualProcessorState;
184/** Pointer to what nemR3WinIoctlDetector_ForLogging should fill in. */
185static NEMWINIOCTL *g_pIoCtlDetectForLogging;
186#endif
187
188#ifdef NEM_WIN_INTERCEPT_NT_IO_CTLS
189/** Mapping slot for CPU #0.
190 * @{ */
191static VID_MESSAGE_MAPPING_HEADER *g_pMsgSlotMapping = NULL;
192static const HV_MESSAGE_HEADER *g_pHvMsgHdr;
193static const HV_X64_INTERCEPT_MESSAGE_HEADER *g_pX64MsgHdr;
194/** @} */
195#endif
196
197
198/*
199 * Let the preprocessor alias the APIs to import variables for better autocompletion.
200 */
201#ifndef IN_SLICKEDIT
202# define WHvGetCapability g_pfnWHvGetCapability
203# define WHvCreatePartition g_pfnWHvCreatePartition
204# define WHvSetupPartition g_pfnWHvSetupPartition
205# define WHvDeletePartition g_pfnWHvDeletePartition
206# define WHvGetPartitionProperty g_pfnWHvGetPartitionProperty
207# define WHvSetPartitionProperty g_pfnWHvSetPartitionProperty
208# define WHvMapGpaRange g_pfnWHvMapGpaRange
209# define WHvUnmapGpaRange g_pfnWHvUnmapGpaRange
210# define WHvTranslateGva g_pfnWHvTranslateGva
211# define WHvCreateVirtualProcessor g_pfnWHvCreateVirtualProcessor
212# define WHvDeleteVirtualProcessor g_pfnWHvDeleteVirtualProcessor
213# define WHvRunVirtualProcessor g_pfnWHvRunVirtualProcessor
214# define WHvGetRunExitContextSize g_pfnWHvGetRunExitContextSize
215# define WHvCancelRunVirtualProcessor g_pfnWHvCancelRunVirtualProcessor
216# define WHvGetVirtualProcessorRegisters g_pfnWHvGetVirtualProcessorRegisters
217# define WHvSetVirtualProcessorRegisters g_pfnWHvSetVirtualProcessorRegisters
218
219# define VidMessageSlotHandleAndGetNext g_pfnVidMessageSlotHandleAndGetNext
220# define VidStartVirtualProcessor g_pfnVidStartVirtualProcessor
221# define VidStopVirtualProcessor g_pfnVidStopVirtualProcessor
222
223#endif
224
225/** WHV_MEMORY_ACCESS_TYPE names */
226static const char * const g_apszWHvMemAccesstypes[4] = { "read", "write", "exec", "!undefined!" };
227
228
229/*********************************************************************************************************************************
230* Internal Functions *
231*********************************************************************************************************************************/
232
233/*
234 * Instantate the code we share with ring-0.
235 */
236#ifdef NEM_WIN_USE_OUR_OWN_RUN_API
237# define NEM_WIN_TEMPLATE_MODE_OWN_RUN_API
238#else
239# undef NEM_WIN_TEMPLATE_MODE_OWN_RUN_API
240#endif
241#include "../VMMAll/NEMAllNativeTemplate-win.cpp.h"
242
243
244
245#ifdef NEM_WIN_INTERCEPT_NT_IO_CTLS
246/**
247 * Wrapper that logs the call from VID.DLL.
248 *
249 * This is very handy for figuring out why an API call fails.
250 */
251static NTSTATUS WINAPI
252nemR3WinLogWrapper_NtDeviceIoControlFile(HANDLE hFile, HANDLE hEvt, PIO_APC_ROUTINE pfnApcCallback, PVOID pvApcCtx,
253 PIO_STATUS_BLOCK pIos, ULONG uFunction, PVOID pvInput, ULONG cbInput,
254 PVOID pvOutput, ULONG cbOutput)
255{
256
257 char szFunction[32];
258 const char *pszFunction;
259 if (uFunction == g_IoCtlMessageSlotHandleAndGetNext.uFunction)
260 pszFunction = "VidMessageSlotHandleAndGetNext";
261 else if (uFunction == g_IoCtlStartVirtualProcessor.uFunction)
262 pszFunction = "VidStartVirtualProcessor";
263 else if (uFunction == g_IoCtlStopVirtualProcessor.uFunction)
264 pszFunction = "VidStopVirtualProcessor";
265 else if (uFunction == g_IoCtlMessageSlotMap.uFunction)
266 pszFunction = "VidMessageSlotMap";
267 else if (uFunction == g_IoCtlGetVirtualProcessorState.uFunction)
268 pszFunction = "VidGetVirtualProcessorState";
269 else if (uFunction == g_IoCtlSetVirtualProcessorState.uFunction)
270 pszFunction = "VidSetVirtualProcessorState";
271 else
272 {
273 RTStrPrintf(szFunction, sizeof(szFunction), "%#x", uFunction);
274 pszFunction = szFunction;
275 }
276
277 if (cbInput > 0 && pvInput)
278 Log12(("VID!NtDeviceIoControlFile: %s/input: %.*Rhxs\n", pszFunction, RT_MIN(cbInput, 32), pvInput));
279 NTSTATUS rcNt = g_pfnNtDeviceIoControlFile(hFile, hEvt, pfnApcCallback, pvApcCtx, pIos, uFunction,
280 pvInput, cbInput, pvOutput, cbOutput);
281 if (!hEvt && !pfnApcCallback && !pvApcCtx)
282 Log12(("VID!NtDeviceIoControlFile: hFile=%#zx pIos=%p->{s:%#x, i:%#zx} uFunction=%s Input=%p LB %#x Output=%p LB %#x) -> %#x; Caller=%p\n",
283 hFile, pIos, pIos->Status, pIos->Information, pszFunction, pvInput, cbInput, pvOutput, cbOutput, rcNt, ASMReturnAddress()));
284 else
285 Log12(("VID!NtDeviceIoControlFile: hFile=%#zx hEvt=%#zx Apc=%p/%p pIos=%p->{s:%#x, i:%#zx} uFunction=%s Input=%p LB %#x Output=%p LB %#x) -> %#x; Caller=%p\n",
286 hFile, hEvt, pfnApcCallback, pvApcCtx, pIos, pIos->Status, pIos->Information, pszFunction,
287 pvInput, cbInput, pvOutput, cbOutput, rcNt, ASMReturnAddress()));
288 if (cbOutput > 0 && pvOutput)
289 {
290 Log12(("VID!NtDeviceIoControlFile: %s/output: %.*Rhxs\n", pszFunction, RT_MIN(cbOutput, 32), pvOutput));
291 if (uFunction == 0x2210cc && g_pMsgSlotMapping == NULL && cbOutput >= sizeof(void *))
292 {
293 g_pMsgSlotMapping = *(VID_MESSAGE_MAPPING_HEADER **)pvOutput;
294 g_pHvMsgHdr = (const HV_MESSAGE_HEADER *)(g_pMsgSlotMapping + 1);
295 g_pX64MsgHdr = (const HV_X64_INTERCEPT_MESSAGE_HEADER *)(g_pHvMsgHdr + 1);
296 Log12(("VID!NtDeviceIoControlFile: Message slot mapping: %p\n", g_pMsgSlotMapping));
297 }
298 }
299 if ( g_pMsgSlotMapping
300 && ( uFunction == g_IoCtlMessageSlotHandleAndGetNext.uFunction
301 || uFunction == g_IoCtlStopVirtualProcessor.uFunction
302 || uFunction == g_IoCtlMessageSlotMap.uFunction
303 ))
304 Log12(("VID!NtDeviceIoControlFile: enmVidMsgType=%#x cb=%#x msg=%#x payload=%u cs:rip=%04x:%08RX64 (%s)\n",
305 g_pMsgSlotMapping->enmVidMsgType, g_pMsgSlotMapping->cbMessage,
306 g_pHvMsgHdr->MessageType, g_pHvMsgHdr->PayloadSize,
307 g_pX64MsgHdr->CsSegment.Selector, g_pX64MsgHdr->Rip, pszFunction));
308
309 return rcNt;
310}
311#endif /* NEM_WIN_INTERCEPT_NT_IO_CTLS */
312
313
314/**
315 * Patches the call table of VID.DLL so we can intercept NtDeviceIoControlFile.
316 *
317 * This is for used to figure out the I/O control codes and in logging builds
318 * for logging API calls that WinHvPlatform.dll does.
319 *
320 * @returns VBox status code.
321 * @param hLdrModVid The VID module handle.
322 * @param pErrInfo Where to return additional error information.
323 */
324static int nemR3WinInitVidIntercepts(RTLDRMOD hLdrModVid, PRTERRINFO pErrInfo)
325{
326 /*
327 * Locate the real API.
328 */
329 g_pfnNtDeviceIoControlFile = (decltype(NtDeviceIoControlFile) *)RTLdrGetSystemSymbol("NTDLL.DLL", "NtDeviceIoControlFile");
330 AssertReturn(g_pfnNtDeviceIoControlFile != NULL,
331 RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED, "Failed to resolve NtDeviceIoControlFile from NTDLL.DLL"));
332
333 /*
334 * Locate the PE header and get what we need from it.
335 */
336 uint8_t const *pbImage = (uint8_t const *)RTLdrGetNativeHandle(hLdrModVid);
337 IMAGE_DOS_HEADER const *pMzHdr = (IMAGE_DOS_HEADER const *)pbImage;
338 AssertReturn(pMzHdr->e_magic == IMAGE_DOS_SIGNATURE,
339 RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED, "VID.DLL mapping doesn't start with MZ signature: %#x", pMzHdr->e_magic));
340 IMAGE_NT_HEADERS const *pNtHdrs = (IMAGE_NT_HEADERS const *)&pbImage[pMzHdr->e_lfanew];
341 AssertReturn(pNtHdrs->Signature == IMAGE_NT_SIGNATURE,
342 RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED, "VID.DLL has invalid PE signaturre: %#x @%#x",
343 pNtHdrs->Signature, pMzHdr->e_lfanew));
344
345 uint32_t const cbImage = pNtHdrs->OptionalHeader.SizeOfImage;
346 IMAGE_DATA_DIRECTORY const ImportDir = pNtHdrs->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
347
348 /*
349 * Walk the import descriptor table looking for NTDLL.DLL.
350 */
351 AssertReturn( ImportDir.Size > 0
352 && ImportDir.Size < cbImage,
353 RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED, "VID.DLL bad import directory size: %#x", ImportDir.Size));
354 AssertReturn( ImportDir.VirtualAddress > 0
355 && ImportDir.VirtualAddress <= cbImage - ImportDir.Size,
356 RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED, "VID.DLL bad import directory RVA: %#x", ImportDir.VirtualAddress));
357
358 for (PIMAGE_IMPORT_DESCRIPTOR pImps = (PIMAGE_IMPORT_DESCRIPTOR)&pbImage[ImportDir.VirtualAddress];
359 pImps->Name != 0 && pImps->FirstThunk != 0;
360 pImps++)
361 {
362 AssertReturn(pImps->Name < cbImage,
363 RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED, "VID.DLL bad import directory entry name: %#x", pImps->Name));
364 const char *pszModName = (const char *)&pbImage[pImps->Name];
365 if (RTStrICmpAscii(pszModName, "ntdll.dll"))
366 continue;
367 AssertReturn(pImps->FirstThunk < cbImage,
368 RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED, "VID.DLL bad FirstThunk: %#x", pImps->FirstThunk));
369 AssertReturn(pImps->OriginalFirstThunk < cbImage,
370 RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED, "VID.DLL bad FirstThunk: %#x", pImps->FirstThunk));
371
372 /*
373 * Walk the thunks table(s) looking for NtDeviceIoControlFile.
374 */
375 PIMAGE_THUNK_DATA pFirstThunk = (PIMAGE_THUNK_DATA)&pbImage[pImps->FirstThunk]; /* update this. */
376 PIMAGE_THUNK_DATA pThunk = pImps->OriginalFirstThunk == 0 /* read from this. */
377 ? (PIMAGE_THUNK_DATA)&pbImage[pImps->FirstThunk]
378 : (PIMAGE_THUNK_DATA)&pbImage[pImps->OriginalFirstThunk];
379 while (pThunk->u1.Ordinal != 0)
380 {
381 if (!(pThunk->u1.Ordinal & IMAGE_ORDINAL_FLAG32))
382 {
383 AssertReturn(pThunk->u1.Ordinal > 0 && pThunk->u1.Ordinal < cbImage,
384 RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED, "VID.DLL bad FirstThunk: %#x", pImps->FirstThunk));
385
386 const char *pszSymbol = (const char *)&pbImage[(uintptr_t)pThunk->u1.AddressOfData + 2];
387 if (strcmp(pszSymbol, "NtDeviceIoControlFile") == 0)
388 {
389 DWORD fOldProt = PAGE_READONLY;
390 VirtualProtect(&pFirstThunk->u1.Function, sizeof(uintptr_t), PAGE_EXECUTE_READWRITE, &fOldProt);
391 g_ppfnVidNtDeviceIoControlFile = (decltype(NtDeviceIoControlFile) **)&pFirstThunk->u1.Function;
392 /* Don't restore the protection here, so we modify the NtDeviceIoControlFile pointer later. */
393 }
394 }
395
396 pThunk++;
397 pFirstThunk++;
398 }
399 }
400
401 if (*g_ppfnVidNtDeviceIoControlFile)
402 {
403#ifdef NEM_WIN_INTERCEPT_NT_IO_CTLS
404 *g_ppfnVidNtDeviceIoControlFile = nemR3WinLogWrapper_NtDeviceIoControlFile;
405#endif
406 return VINF_SUCCESS;
407 }
408 return RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED, "Failed to patch NtDeviceIoControlFile import in VID.DLL!");
409}
410
411
412/**
413 * Worker for nemR3NativeInit that probes and load the native API.
414 *
415 * @returns VBox status code.
416 * @param fForced Whether the HMForced flag is set and we should
417 * fail if we cannot initialize.
418 * @param pErrInfo Where to always return error info.
419 */
420static int nemR3WinInitProbeAndLoad(bool fForced, PRTERRINFO pErrInfo)
421{
422 /*
423 * Check that the DLL files we need are present, but without loading them.
424 * We'd like to avoid loading them unnecessarily.
425 */
426 WCHAR wszPath[MAX_PATH + 64];
427 UINT cwcPath = GetSystemDirectoryW(wszPath, MAX_PATH);
428 if (cwcPath >= MAX_PATH || cwcPath < 2)
429 return RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED, "GetSystemDirectoryW failed (%#x / %u)", cwcPath, GetLastError());
430
431 if (wszPath[cwcPath - 1] != '\\' || wszPath[cwcPath - 1] != '/')
432 wszPath[cwcPath++] = '\\';
433 RTUtf16CopyAscii(&wszPath[cwcPath], RT_ELEMENTS(wszPath) - cwcPath, "WinHvPlatform.dll");
434 if (GetFileAttributesW(wszPath) == INVALID_FILE_ATTRIBUTES)
435 return RTErrInfoSetF(pErrInfo, VERR_NEM_NOT_AVAILABLE, "The native API dll was not found (%ls)", wszPath);
436
437 /*
438 * Check that we're in a VM and that the hypervisor identifies itself as Hyper-V.
439 */
440 if (!ASMHasCpuId())
441 return RTErrInfoSet(pErrInfo, VERR_NEM_NOT_AVAILABLE, "No CPUID support");
442 if (!ASMIsValidStdRange(ASMCpuId_EAX(0)))
443 return RTErrInfoSet(pErrInfo, VERR_NEM_NOT_AVAILABLE, "No CPUID leaf #1");
444 if (!(ASMCpuId_ECX(1) & X86_CPUID_FEATURE_ECX_HVP))
445 return RTErrInfoSet(pErrInfo, VERR_NEM_NOT_AVAILABLE, "Not in a hypervisor partition (HVP=0)");
446
447 uint32_t cMaxHyperLeaf = 0;
448 uint32_t uEbx = 0;
449 uint32_t uEcx = 0;
450 uint32_t uEdx = 0;
451 ASMCpuIdExSlow(0x40000000, 0, 0, 0, &cMaxHyperLeaf, &uEbx, &uEcx, &uEdx);
452 if (!ASMIsValidHypervisorRange(cMaxHyperLeaf))
453 return RTErrInfoSetF(pErrInfo, VERR_NEM_NOT_AVAILABLE, "Invalid hypervisor CPUID range (%#x %#x %#x %#x)",
454 cMaxHyperLeaf, uEbx, uEcx, uEdx);
455 if ( uEbx != UINT32_C(0x7263694d) /* Micr */
456 || uEcx != UINT32_C(0x666f736f) /* osof */
457 || uEdx != UINT32_C(0x76482074) /* t Hv */)
458 return RTErrInfoSetF(pErrInfo, VERR_NEM_NOT_AVAILABLE,
459 "Not Hyper-V CPUID signature: %#x %#x %#x (expected %#x %#x %#x)",
460 uEbx, uEcx, uEdx, UINT32_C(0x7263694d), UINT32_C(0x666f736f), UINT32_C(0x76482074));
461 if (cMaxHyperLeaf < UINT32_C(0x40000005))
462 return RTErrInfoSetF(pErrInfo, VERR_NEM_NOT_AVAILABLE, "Too narrow hypervisor CPUID range (%#x)", cMaxHyperLeaf);
463
464 /** @todo would be great if we could recognize a root partition from the
465 * CPUID info, but I currently don't dare do that. */
466
467 /*
468 * Now try load the DLLs and resolve the APIs.
469 */
470 static const char * const s_apszDllNames[2] = { "WinHvPlatform.dll", "vid.dll" };
471 RTLDRMOD ahMods[2] = { NIL_RTLDRMOD, NIL_RTLDRMOD };
472 int rc = VINF_SUCCESS;
473 for (unsigned i = 0; i < RT_ELEMENTS(s_apszDllNames); i++)
474 {
475 int rc2 = RTLdrLoadSystem(s_apszDllNames[i], true /*fNoUnload*/, &ahMods[i]);
476 if (RT_FAILURE(rc2))
477 {
478 if (!RTErrInfoIsSet(pErrInfo))
479 RTErrInfoSetF(pErrInfo, rc2, "Failed to load API DLL: %s: %Rrc", s_apszDllNames[i], rc2);
480 else
481 RTErrInfoAddF(pErrInfo, rc2, "; %s: %Rrc", s_apszDllNames[i], rc2);
482 ahMods[i] = NIL_RTLDRMOD;
483 rc = VERR_NEM_INIT_FAILED;
484 }
485 }
486 if (RT_SUCCESS(rc))
487 rc = nemR3WinInitVidIntercepts(ahMods[1], pErrInfo);
488 if (RT_SUCCESS(rc))
489 {
490 for (unsigned i = 0; i < RT_ELEMENTS(g_aImports); i++)
491 {
492 int rc2 = RTLdrGetSymbol(ahMods[g_aImports[i].idxDll], g_aImports[i].pszName, (void **)g_aImports[i].ppfn);
493 if (RT_FAILURE(rc2))
494 {
495 *g_aImports[i].ppfn = NULL;
496
497 LogRel(("NEM: %s: Failed to import %s!%s: %Rrc",
498 g_aImports[i].fOptional ? "info" : fForced ? "fatal" : "error",
499 s_apszDllNames[g_aImports[i].idxDll], g_aImports[i].pszName, rc2));
500 if (!g_aImports[i].fOptional)
501 {
502 if (RTErrInfoIsSet(pErrInfo))
503 RTErrInfoAddF(pErrInfo, rc2, ", %s!%s",
504 s_apszDllNames[g_aImports[i].idxDll], g_aImports[i].pszName);
505 else
506 rc = RTErrInfoSetF(pErrInfo, rc2, "Failed to import: %s!%s",
507 s_apszDllNames[g_aImports[i].idxDll], g_aImports[i].pszName);
508 Assert(RT_FAILURE(rc));
509 }
510 }
511 }
512 if (RT_SUCCESS(rc))
513 {
514 Assert(!RTErrInfoIsSet(pErrInfo));
515 }
516 }
517
518 for (unsigned i = 0; i < RT_ELEMENTS(ahMods); i++)
519 RTLdrClose(ahMods[i]);
520 return rc;
521}
522
523
524/**
525 * Wrapper for different WHvGetCapability signatures.
526 */
527DECLINLINE(HRESULT) WHvGetCapabilityWrapper(WHV_CAPABILITY_CODE enmCap, WHV_CAPABILITY *pOutput, uint32_t cbOutput)
528{
529 return g_pfnWHvGetCapability(enmCap, pOutput, cbOutput, NULL);
530}
531
532
533/**
534 * Worker for nemR3NativeInit that gets the hypervisor capabilities.
535 *
536 * @returns VBox status code.
537 * @param pVM The cross context VM structure.
538 * @param pErrInfo Where to always return error info.
539 */
540static int nemR3WinInitCheckCapabilities(PVM pVM, PRTERRINFO pErrInfo)
541{
542#define NEM_LOG_REL_CAP_EX(a_szField, a_szFmt, a_Value) LogRel(("NEM: %-38s= " a_szFmt "\n", a_szField, a_Value))
543#define NEM_LOG_REL_CAP_SUB_EX(a_szField, a_szFmt, a_Value) LogRel(("NEM: %36s: " a_szFmt "\n", a_szField, a_Value))
544#define NEM_LOG_REL_CAP_SUB(a_szField, a_Value) NEM_LOG_REL_CAP_SUB_EX(a_szField, "%d", a_Value)
545
546 /*
547 * Is the hypervisor present with the desired capability?
548 *
549 * In build 17083 this translates into:
550 * - CPUID[0x00000001].HVP is set
551 * - CPUID[0x40000000] == "Microsoft Hv"
552 * - CPUID[0x40000001].eax == "Hv#1"
553 * - CPUID[0x40000003].ebx[12] is set.
554 * - VidGetExoPartitionProperty(INVALID_HANDLE_VALUE, 0x60000, &Ignored) returns
555 * a non-zero value.
556 */
557 /**
558 * @todo Someone at Microsoft please explain weird API design:
559 * 1. Pointless CapabilityCode duplication int the output;
560 * 2. No output size.
561 */
562 WHV_CAPABILITY Caps;
563 RT_ZERO(Caps);
564 SetLastError(0);
565 HRESULT hrc = WHvGetCapabilityWrapper(WHvCapabilityCodeHypervisorPresent, &Caps, sizeof(Caps));
566 DWORD rcWin = GetLastError();
567 if (FAILED(hrc))
568 return RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED,
569 "WHvGetCapability/WHvCapabilityCodeHypervisorPresent failed: %Rhrc (Last=%#x/%u)",
570 hrc, RTNtLastStatusValue(), RTNtLastErrorValue());
571 if (!Caps.HypervisorPresent)
572 {
573 if (!RTPathExists(RTPATH_NT_PASSTHRU_PREFIX "Device\\VidExo"))
574 return RTErrInfoSetF(pErrInfo, VERR_NEM_NOT_AVAILABLE,
575 "WHvCapabilityCodeHypervisorPresent is FALSE! Make sure you have enabled the 'Windows Hypervisor Platform' feature.");
576 return RTErrInfoSetF(pErrInfo, VERR_NEM_NOT_AVAILABLE, "WHvCapabilityCodeHypervisorPresent is FALSE! (%u)", rcWin);
577 }
578 LogRel(("NEM: WHvCapabilityCodeHypervisorPresent is TRUE, so this might work...\n"));
579
580
581 /*
582 * Check what extended VM exits are supported.
583 */
584 RT_ZERO(Caps);
585 hrc = WHvGetCapabilityWrapper(WHvCapabilityCodeExtendedVmExits, &Caps, sizeof(Caps));
586 if (FAILED(hrc))
587 return RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED,
588 "WHvGetCapability/WHvCapabilityCodeExtendedVmExits failed: %Rhrc (Last=%#x/%u)",
589 hrc, RTNtLastStatusValue(), RTNtLastErrorValue());
590 NEM_LOG_REL_CAP_EX("WHvCapabilityCodeExtendedVmExits", "%'#018RX64", Caps.ExtendedVmExits.AsUINT64);
591 pVM->nem.s.fExtendedMsrExit = RT_BOOL(Caps.ExtendedVmExits.X64MsrExit);
592 pVM->nem.s.fExtendedCpuIdExit = RT_BOOL(Caps.ExtendedVmExits.X64CpuidExit);
593 pVM->nem.s.fExtendedXcptExit = RT_BOOL(Caps.ExtendedVmExits.ExceptionExit);
594 NEM_LOG_REL_CAP_SUB("fExtendedMsrExit", pVM->nem.s.fExtendedMsrExit);
595 NEM_LOG_REL_CAP_SUB("fExtendedCpuIdExit", pVM->nem.s.fExtendedCpuIdExit);
596 NEM_LOG_REL_CAP_SUB("fExtendedXcptExit", pVM->nem.s.fExtendedXcptExit);
597 if (Caps.ExtendedVmExits.AsUINT64 & ~(uint64_t)7)
598 LogRel(("NEM: Warning! Unknown VM exit definitions: %#RX64\n", Caps.ExtendedVmExits.AsUINT64));
599 /** @todo RECHECK: WHV_EXTENDED_VM_EXITS typedef. */
600
601 /*
602 * Check features in case they end up defining any.
603 */
604 RT_ZERO(Caps);
605 hrc = WHvGetCapabilityWrapper(WHvCapabilityCodeFeatures, &Caps, sizeof(Caps));
606 if (FAILED(hrc))
607 return RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED,
608 "WHvGetCapability/WHvCapabilityCodeFeatures failed: %Rhrc (Last=%#x/%u)",
609 hrc, RTNtLastStatusValue(), RTNtLastErrorValue());
610 if (Caps.Features.AsUINT64 & ~(uint64_t)0)
611 LogRel(("NEM: Warning! Unknown feature definitions: %#RX64\n", Caps.Features.AsUINT64));
612 /** @todo RECHECK: WHV_CAPABILITY_FEATURES typedef. */
613
614 /*
615 * Check supported exception exit bitmap bits.
616 * We don't currently require this, so we just log failure.
617 */
618 RT_ZERO(Caps);
619 hrc = WHvGetCapabilityWrapper(WHvCapabilityCodeExceptionExitBitmap, &Caps, sizeof(Caps));
620 if (SUCCEEDED(hrc))
621 LogRel(("NEM: Supported exception exit bitmap: %#RX64\n", Caps.ExceptionExitBitmap));
622 else
623 LogRel(("NEM: Warning! WHvGetCapability/WHvCapabilityCodeExceptionExitBitmap failed: %Rhrc (Last=%#x/%u)",
624 hrc, RTNtLastStatusValue(), RTNtLastErrorValue()));
625
626 /*
627 * Check that the CPU vendor is supported.
628 */
629 RT_ZERO(Caps);
630 hrc = WHvGetCapabilityWrapper(WHvCapabilityCodeProcessorVendor, &Caps, sizeof(Caps));
631 if (FAILED(hrc))
632 return RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED,
633 "WHvGetCapability/WHvCapabilityCodeProcessorVendor failed: %Rhrc (Last=%#x/%u)",
634 hrc, RTNtLastStatusValue(), RTNtLastErrorValue());
635 switch (Caps.ProcessorVendor)
636 {
637 /** @todo RECHECK: WHV_PROCESSOR_VENDOR typedef. */
638 case WHvProcessorVendorIntel:
639 NEM_LOG_REL_CAP_EX("WHvCapabilityCodeProcessorVendor", "%d - Intel", Caps.ProcessorVendor);
640 pVM->nem.s.enmCpuVendor = CPUMCPUVENDOR_INTEL;
641 break;
642 case WHvProcessorVendorAmd:
643 NEM_LOG_REL_CAP_EX("WHvCapabilityCodeProcessorVendor", "%d - AMD", Caps.ProcessorVendor);
644 pVM->nem.s.enmCpuVendor = CPUMCPUVENDOR_AMD;
645 break;
646 default:
647 NEM_LOG_REL_CAP_EX("WHvCapabilityCodeProcessorVendor", "%d", Caps.ProcessorVendor);
648 return RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED, "Unknown processor vendor: %d", Caps.ProcessorVendor);
649 }
650
651 /*
652 * CPU features, guessing these are virtual CPU features?
653 */
654 RT_ZERO(Caps);
655 hrc = WHvGetCapabilityWrapper(WHvCapabilityCodeProcessorFeatures, &Caps, sizeof(Caps));
656 if (FAILED(hrc))
657 return RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED,
658 "WHvGetCapability/WHvCapabilityCodeProcessorFeatures failed: %Rhrc (Last=%#x/%u)",
659 hrc, RTNtLastStatusValue(), RTNtLastErrorValue());
660 NEM_LOG_REL_CAP_EX("WHvCapabilityCodeProcessorFeatures", "%'#018RX64", Caps.ProcessorFeatures.AsUINT64);
661#define NEM_LOG_REL_CPU_FEATURE(a_Field) NEM_LOG_REL_CAP_SUB(#a_Field, Caps.ProcessorFeatures.a_Field)
662 NEM_LOG_REL_CPU_FEATURE(Sse3Support);
663 NEM_LOG_REL_CPU_FEATURE(LahfSahfSupport);
664 NEM_LOG_REL_CPU_FEATURE(Ssse3Support);
665 NEM_LOG_REL_CPU_FEATURE(Sse4_1Support);
666 NEM_LOG_REL_CPU_FEATURE(Sse4_2Support);
667 NEM_LOG_REL_CPU_FEATURE(Sse4aSupport);
668 NEM_LOG_REL_CPU_FEATURE(XopSupport);
669 NEM_LOG_REL_CPU_FEATURE(PopCntSupport);
670 NEM_LOG_REL_CPU_FEATURE(Cmpxchg16bSupport);
671 NEM_LOG_REL_CPU_FEATURE(Altmovcr8Support);
672 NEM_LOG_REL_CPU_FEATURE(LzcntSupport);
673 NEM_LOG_REL_CPU_FEATURE(MisAlignSseSupport);
674 NEM_LOG_REL_CPU_FEATURE(MmxExtSupport);
675 NEM_LOG_REL_CPU_FEATURE(Amd3DNowSupport);
676 NEM_LOG_REL_CPU_FEATURE(ExtendedAmd3DNowSupport);
677 NEM_LOG_REL_CPU_FEATURE(Page1GbSupport);
678 NEM_LOG_REL_CPU_FEATURE(AesSupport);
679 NEM_LOG_REL_CPU_FEATURE(PclmulqdqSupport);
680 NEM_LOG_REL_CPU_FEATURE(PcidSupport);
681 NEM_LOG_REL_CPU_FEATURE(Fma4Support);
682 NEM_LOG_REL_CPU_FEATURE(F16CSupport);
683 NEM_LOG_REL_CPU_FEATURE(RdRandSupport);
684 NEM_LOG_REL_CPU_FEATURE(RdWrFsGsSupport);
685 NEM_LOG_REL_CPU_FEATURE(SmepSupport);
686 NEM_LOG_REL_CPU_FEATURE(EnhancedFastStringSupport);
687 NEM_LOG_REL_CPU_FEATURE(Bmi1Support);
688 NEM_LOG_REL_CPU_FEATURE(Bmi2Support);
689 /* two reserved bits here, see below */
690 NEM_LOG_REL_CPU_FEATURE(MovbeSupport);
691 NEM_LOG_REL_CPU_FEATURE(Npiep1Support);
692 NEM_LOG_REL_CPU_FEATURE(DepX87FPUSaveSupport);
693 NEM_LOG_REL_CPU_FEATURE(RdSeedSupport);
694 NEM_LOG_REL_CPU_FEATURE(AdxSupport);
695 NEM_LOG_REL_CPU_FEATURE(IntelPrefetchSupport);
696 NEM_LOG_REL_CPU_FEATURE(SmapSupport);
697 NEM_LOG_REL_CPU_FEATURE(HleSupport);
698 NEM_LOG_REL_CPU_FEATURE(RtmSupport);
699 NEM_LOG_REL_CPU_FEATURE(RdtscpSupport);
700 NEM_LOG_REL_CPU_FEATURE(ClflushoptSupport);
701 NEM_LOG_REL_CPU_FEATURE(ClwbSupport);
702 NEM_LOG_REL_CPU_FEATURE(ShaSupport);
703 NEM_LOG_REL_CPU_FEATURE(X87PointersSavedSupport);
704#undef NEM_LOG_REL_CPU_FEATURE
705 if (Caps.ProcessorFeatures.AsUINT64 & (~(RT_BIT_64(43) - 1) | RT_BIT_64(27) | RT_BIT_64(28)))
706 LogRel(("NEM: Warning! Unknown CPU features: %#RX64\n", Caps.ProcessorFeatures.AsUINT64));
707 pVM->nem.s.uCpuFeatures.u64 = Caps.ProcessorFeatures.AsUINT64;
708 /** @todo RECHECK: WHV_PROCESSOR_FEATURES typedef. */
709
710 /*
711 * The cache line flush size.
712 */
713 RT_ZERO(Caps);
714 hrc = WHvGetCapabilityWrapper(WHvCapabilityCodeProcessorClFlushSize, &Caps, sizeof(Caps));
715 if (FAILED(hrc))
716 return RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED,
717 "WHvGetCapability/WHvCapabilityCodeProcessorClFlushSize failed: %Rhrc (Last=%#x/%u)",
718 hrc, RTNtLastStatusValue(), RTNtLastErrorValue());
719 NEM_LOG_REL_CAP_EX("WHvCapabilityCodeProcessorClFlushSize", "2^%u", Caps.ProcessorClFlushSize);
720 if (Caps.ProcessorClFlushSize < 8 && Caps.ProcessorClFlushSize > 9)
721 return RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED, "Unsupported cache line flush size: %u", Caps.ProcessorClFlushSize);
722 pVM->nem.s.cCacheLineFlushShift = Caps.ProcessorClFlushSize;
723
724 /*
725 * See if they've added more properties that we're not aware of.
726 */
727 /** @todo RECHECK: WHV_CAPABILITY_CODE typedef. */
728 if (!IsDebuggerPresent()) /* Too noisy when in debugger, so skip. */
729 {
730 static const struct
731 {
732 uint32_t iMin, iMax; } s_aUnknowns[] =
733 {
734 { 0x0004, 0x000f },
735 { 0x1003, 0x100f },
736 { 0x2000, 0x200f },
737 { 0x3000, 0x300f },
738 { 0x4000, 0x400f },
739 };
740 for (uint32_t j = 0; j < RT_ELEMENTS(s_aUnknowns); j++)
741 for (uint32_t i = s_aUnknowns[j].iMin; i <= s_aUnknowns[j].iMax; i++)
742 {
743 RT_ZERO(Caps);
744 hrc = WHvGetCapabilityWrapper((WHV_CAPABILITY_CODE)i, &Caps, sizeof(Caps));
745 if (SUCCEEDED(hrc))
746 LogRel(("NEM: Warning! Unknown capability %#x returning: %.*Rhxs\n", i, sizeof(Caps), &Caps));
747 }
748 }
749
750 /*
751 * For proper operation, we require CPUID exits.
752 */
753 if (!pVM->nem.s.fExtendedCpuIdExit)
754 return RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED, "Missing required extended CPUID exit support");
755 if (!pVM->nem.s.fExtendedMsrExit)
756 return RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED, "Missing required extended MSR exit support");
757 if (!pVM->nem.s.fExtendedXcptExit)
758 return RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED, "Missing required extended exception exit support");
759
760#undef NEM_LOG_REL_CAP_EX
761#undef NEM_LOG_REL_CAP_SUB_EX
762#undef NEM_LOG_REL_CAP_SUB
763 return VINF_SUCCESS;
764}
765
766
767/**
768 * Used to fill in g_IoCtlGetHvPartitionId.
769 */
770static NTSTATUS WINAPI
771nemR3WinIoctlDetector_GetHvPartitionId(HANDLE hFile, HANDLE hEvt, PIO_APC_ROUTINE pfnApcCallback, PVOID pvApcCtx,
772 PIO_STATUS_BLOCK pIos, ULONG uFunction, PVOID pvInput, ULONG cbInput,
773 PVOID pvOutput, ULONG cbOutput)
774{
775 AssertLogRelMsgReturn(hFile == NEM_WIN_IOCTL_DETECTOR_FAKE_HANDLE, ("hFile=%p\n", hFile), STATUS_INVALID_PARAMETER_1);
776 RT_NOREF(hEvt); RT_NOREF(pfnApcCallback); RT_NOREF(pvApcCtx);
777 AssertLogRelMsgReturn(RT_VALID_PTR(pIos), ("pIos=%p\n", pIos), STATUS_INVALID_PARAMETER_5);
778 AssertLogRelMsgReturn(cbInput == 0, ("cbInput=%#x\n", cbInput), STATUS_INVALID_PARAMETER_8);
779 RT_NOREF(pvInput);
780
781 AssertLogRelMsgReturn(RT_VALID_PTR(pvOutput), ("pvOutput=%p\n", pvOutput), STATUS_INVALID_PARAMETER_9);
782 AssertLogRelMsgReturn(cbOutput == sizeof(HV_PARTITION_ID), ("cbInput=%#x\n", cbInput), STATUS_INVALID_PARAMETER_10);
783 *(HV_PARTITION_ID *)pvOutput = NEM_WIN_IOCTL_DETECTOR_FAKE_PARTITION_ID;
784
785 g_IoCtlGetHvPartitionId.cbInput = cbInput;
786 g_IoCtlGetHvPartitionId.cbOutput = cbOutput;
787 g_IoCtlGetHvPartitionId.uFunction = uFunction;
788
789 return STATUS_SUCCESS;
790}
791
792
793/**
794 * Used to fill in g_IoCtlStartVirtualProcessor.
795 */
796static NTSTATUS WINAPI
797nemR3WinIoctlDetector_StartVirtualProcessor(HANDLE hFile, HANDLE hEvt, PIO_APC_ROUTINE pfnApcCallback, PVOID pvApcCtx,
798 PIO_STATUS_BLOCK pIos, ULONG uFunction, PVOID pvInput, ULONG cbInput,
799 PVOID pvOutput, ULONG cbOutput)
800{
801 AssertLogRelMsgReturn(hFile == NEM_WIN_IOCTL_DETECTOR_FAKE_HANDLE, ("hFile=%p\n", hFile), STATUS_INVALID_PARAMETER_1);
802 RT_NOREF(hEvt); RT_NOREF(pfnApcCallback); RT_NOREF(pvApcCtx);
803 AssertLogRelMsgReturn(RT_VALID_PTR(pIos), ("pIos=%p\n", pIos), STATUS_INVALID_PARAMETER_5);
804 AssertLogRelMsgReturn(cbInput == sizeof(HV_VP_INDEX), ("cbInput=%#x\n", cbInput), STATUS_INVALID_PARAMETER_8);
805 AssertLogRelMsgReturn(RT_VALID_PTR(pvInput), ("pvInput=%p\n", pvInput), STATUS_INVALID_PARAMETER_9);
806 AssertLogRelMsgReturn(*(HV_VP_INDEX *)pvInput == NEM_WIN_IOCTL_DETECTOR_FAKE_VP_INDEX,
807 ("*piCpu=%u\n", *(HV_VP_INDEX *)pvInput), STATUS_INVALID_PARAMETER_9);
808 AssertLogRelMsgReturn(cbOutput == 0, ("cbInput=%#x\n", cbInput), STATUS_INVALID_PARAMETER_10);
809 RT_NOREF(pvOutput);
810
811 g_IoCtlStartVirtualProcessor.cbInput = cbInput;
812 g_IoCtlStartVirtualProcessor.cbOutput = cbOutput;
813 g_IoCtlStartVirtualProcessor.uFunction = uFunction;
814
815 return STATUS_SUCCESS;
816}
817
818
819/**
820 * Used to fill in g_IoCtlStartVirtualProcessor.
821 */
822static NTSTATUS WINAPI
823nemR3WinIoctlDetector_StopVirtualProcessor(HANDLE hFile, HANDLE hEvt, PIO_APC_ROUTINE pfnApcCallback, PVOID pvApcCtx,
824 PIO_STATUS_BLOCK pIos, ULONG uFunction, PVOID pvInput, ULONG cbInput,
825 PVOID pvOutput, ULONG cbOutput)
826{
827 AssertLogRelMsgReturn(hFile == NEM_WIN_IOCTL_DETECTOR_FAKE_HANDLE, ("hFile=%p\n", hFile), STATUS_INVALID_PARAMETER_1);
828 RT_NOREF(hEvt); RT_NOREF(pfnApcCallback); RT_NOREF(pvApcCtx);
829 AssertLogRelMsgReturn(RT_VALID_PTR(pIos), ("pIos=%p\n", pIos), STATUS_INVALID_PARAMETER_5);
830 AssertLogRelMsgReturn(cbInput == sizeof(HV_VP_INDEX), ("cbInput=%#x\n", cbInput), STATUS_INVALID_PARAMETER_8);
831 AssertLogRelMsgReturn(RT_VALID_PTR(pvInput), ("pvInput=%p\n", pvInput), STATUS_INVALID_PARAMETER_9);
832 AssertLogRelMsgReturn(*(HV_VP_INDEX *)pvInput == NEM_WIN_IOCTL_DETECTOR_FAKE_VP_INDEX,
833 ("*piCpu=%u\n", *(HV_VP_INDEX *)pvInput), STATUS_INVALID_PARAMETER_9);
834 AssertLogRelMsgReturn(cbOutput == 0, ("cbInput=%#x\n", cbInput), STATUS_INVALID_PARAMETER_10);
835 RT_NOREF(pvOutput);
836
837 g_IoCtlStopVirtualProcessor.cbInput = cbInput;
838 g_IoCtlStopVirtualProcessor.cbOutput = cbOutput;
839 g_IoCtlStopVirtualProcessor.uFunction = uFunction;
840
841 return STATUS_SUCCESS;
842}
843
844
845/**
846 * Used to fill in g_IoCtlMessageSlotHandleAndGetNext
847 */
848static NTSTATUS WINAPI
849nemR3WinIoctlDetector_MessageSlotHandleAndGetNext(HANDLE hFile, HANDLE hEvt, PIO_APC_ROUTINE pfnApcCallback, PVOID pvApcCtx,
850 PIO_STATUS_BLOCK pIos, ULONG uFunction, PVOID pvInput, ULONG cbInput,
851 PVOID pvOutput, ULONG cbOutput)
852{
853 AssertLogRelMsgReturn(hFile == NEM_WIN_IOCTL_DETECTOR_FAKE_HANDLE, ("hFile=%p\n", hFile), STATUS_INVALID_PARAMETER_1);
854 RT_NOREF(hEvt); RT_NOREF(pfnApcCallback); RT_NOREF(pvApcCtx);
855 AssertLogRelMsgReturn(RT_VALID_PTR(pIos), ("pIos=%p\n", pIos), STATUS_INVALID_PARAMETER_5);
856
857 if (g_uBuildNo >= 17758)
858 {
859 /* No timeout since about build 17758, it's now always an infinite wait. So, a somewhat compatible change. */
860 AssertLogRelMsgReturn(cbInput == RT_UOFFSETOF(VID_IOCTL_INPUT_MESSAGE_SLOT_HANDLE_AND_GET_NEXT, cMillies),
861 ("cbInput=%#x\n", cbInput),
862 STATUS_INVALID_PARAMETER_8);
863 AssertLogRelMsgReturn(RT_VALID_PTR(pvInput), ("pvInput=%p\n", pvInput), STATUS_INVALID_PARAMETER_9);
864 PCVID_IOCTL_INPUT_MESSAGE_SLOT_HANDLE_AND_GET_NEXT pVidIn = (PCVID_IOCTL_INPUT_MESSAGE_SLOT_HANDLE_AND_GET_NEXT)pvInput;
865 AssertLogRelMsgReturn( pVidIn->iCpu == NEM_WIN_IOCTL_DETECTOR_FAKE_VP_INDEX
866 && pVidIn->fFlags == VID_MSHAGN_F_HANDLE_MESSAGE,
867 ("iCpu=%u fFlags=%#x cMillies=%#x\n", pVidIn->iCpu, pVidIn->fFlags, pVidIn->cMillies),
868 STATUS_INVALID_PARAMETER_9);
869 AssertLogRelMsgReturn(cbOutput == 0, ("cbInput=%#x\n", cbInput), STATUS_INVALID_PARAMETER_10);
870 }
871 else
872 {
873 AssertLogRelMsgReturn(cbInput == sizeof(VID_IOCTL_INPUT_MESSAGE_SLOT_HANDLE_AND_GET_NEXT), ("cbInput=%#x\n", cbInput),
874 STATUS_INVALID_PARAMETER_8);
875 AssertLogRelMsgReturn(RT_VALID_PTR(pvInput), ("pvInput=%p\n", pvInput), STATUS_INVALID_PARAMETER_9);
876 PCVID_IOCTL_INPUT_MESSAGE_SLOT_HANDLE_AND_GET_NEXT pVidIn = (PCVID_IOCTL_INPUT_MESSAGE_SLOT_HANDLE_AND_GET_NEXT)pvInput;
877 AssertLogRelMsgReturn( pVidIn->iCpu == NEM_WIN_IOCTL_DETECTOR_FAKE_VP_INDEX
878 && pVidIn->fFlags == VID_MSHAGN_F_HANDLE_MESSAGE
879 && pVidIn->cMillies == NEM_WIN_IOCTL_DETECTOR_FAKE_TIMEOUT,
880 ("iCpu=%u fFlags=%#x cMillies=%#x\n", pVidIn->iCpu, pVidIn->fFlags, pVidIn->cMillies),
881 STATUS_INVALID_PARAMETER_9);
882 AssertLogRelMsgReturn(cbOutput == 0, ("cbInput=%#x\n", cbInput), STATUS_INVALID_PARAMETER_10);
883 RT_NOREF(pvOutput);
884 }
885
886 g_IoCtlMessageSlotHandleAndGetNext.cbInput = cbInput;
887 g_IoCtlMessageSlotHandleAndGetNext.cbOutput = cbOutput;
888 g_IoCtlMessageSlotHandleAndGetNext.uFunction = uFunction;
889
890 return STATUS_SUCCESS;
891}
892
893
894#ifdef LOG_ENABLED
895/**
896 * Used to fill in what g_pIoCtlDetectForLogging points to.
897 */
898static NTSTATUS WINAPI nemR3WinIoctlDetector_ForLogging(HANDLE hFile, HANDLE hEvt, PIO_APC_ROUTINE pfnApcCallback, PVOID pvApcCtx,
899 PIO_STATUS_BLOCK pIos, ULONG uFunction, PVOID pvInput, ULONG cbInput,
900 PVOID pvOutput, ULONG cbOutput)
901{
902 RT_NOREF(hFile, hEvt, pfnApcCallback, pvApcCtx, pIos, pvInput, pvOutput);
903
904 g_pIoCtlDetectForLogging->cbInput = cbInput;
905 g_pIoCtlDetectForLogging->cbOutput = cbOutput;
906 g_pIoCtlDetectForLogging->uFunction = uFunction;
907
908 return STATUS_SUCCESS;
909}
910#endif
911
912
913/**
914 * Worker for nemR3NativeInit that detect I/O control function numbers for VID.
915 *
916 * We use the function numbers directly in ring-0 and to name functions when
917 * logging NtDeviceIoControlFile calls.
918 *
919 * @note We could alternatively do this by disassembling the respective
920 * functions, but hooking NtDeviceIoControlFile and making fake calls
921 * more easily provides the desired information.
922 *
923 * @returns VBox status code.
924 * @param pVM The cross context VM structure. Will set I/O
925 * control info members.
926 * @param pErrInfo Where to always return error info.
927 */
928static int nemR3WinInitDiscoverIoControlProperties(PVM pVM, PRTERRINFO pErrInfo)
929{
930 /*
931 * Probe the I/O control information for select VID APIs so we can use
932 * them directly from ring-0 and better log them.
933 *
934 */
935 decltype(NtDeviceIoControlFile) * const pfnOrg = *g_ppfnVidNtDeviceIoControlFile;
936
937 /* VidGetHvPartitionId - must work due to memory. */
938 *g_ppfnVidNtDeviceIoControlFile = nemR3WinIoctlDetector_GetHvPartitionId;
939 HV_PARTITION_ID idHvPartition = HV_PARTITION_ID_INVALID;
940 BOOL fRet = g_pfnVidGetHvPartitionId(NEM_WIN_IOCTL_DETECTOR_FAKE_HANDLE, &idHvPartition);
941 *g_ppfnVidNtDeviceIoControlFile = pfnOrg;
942 AssertReturn(fRet && idHvPartition == NEM_WIN_IOCTL_DETECTOR_FAKE_PARTITION_ID && g_IoCtlGetHvPartitionId.uFunction != 0,
943 RTErrInfoSetF(pErrInfo, VERR_NEM_INIT_FAILED,
944 "Problem figuring out VidGetHvPartitionId: fRet=%u idHvPartition=%#x dwErr=%u",
945 fRet, idHvPartition, GetLastError()) );
946 LogRel(("NEM: VidGetHvPartitionId -> fun:%#x in:%#x out:%#x\n",
947 g_IoCtlGetHvPartitionId.uFunction, g_IoCtlGetHvPartitionId.cbInput, g_IoCtlGetHvPartitionId.cbOutput));
948
949 int rcRet = VINF_SUCCESS;
950 /* VidStartVirtualProcessor */
951 *g_ppfnVidNtDeviceIoControlFile = nemR3WinIoctlDetector_StartVirtualProcessor;
952 fRet = g_pfnVidStartVirtualProcessor(NEM_WIN_IOCTL_DETECTOR_FAKE_HANDLE, NEM_WIN_IOCTL_DETECTOR_FAKE_VP_INDEX);
953 *g_ppfnVidNtDeviceIoControlFile = pfnOrg;
954 AssertStmt(fRet && g_IoCtlStartVirtualProcessor.uFunction != 0,
955 rcRet = RTERRINFO_LOG_REL_SET_F(pErrInfo, VERR_NEM_RING3_ONLY,
956 "Problem figuring out VidStartVirtualProcessor: fRet=%u dwErr=%u",
957 fRet, GetLastError()) );
958 LogRel(("NEM: VidStartVirtualProcessor -> fun:%#x in:%#x out:%#x\n", g_IoCtlStartVirtualProcessor.uFunction,
959 g_IoCtlStartVirtualProcessor.cbInput, g_IoCtlStartVirtualProcessor.cbOutput));
960
961 /* VidStopVirtualProcessor */
962 *g_ppfnVidNtDeviceIoControlFile = nemR3WinIoctlDetector_StopVirtualProcessor;
963 fRet = g_pfnVidStopVirtualProcessor(NEM_WIN_IOCTL_DETECTOR_FAKE_HANDLE, NEM_WIN_IOCTL_DETECTOR_FAKE_VP_INDEX);
964 *g_ppfnVidNtDeviceIoControlFile = pfnOrg;
965 AssertStmt(fRet && g_IoCtlStopVirtualProcessor.uFunction != 0,
966 rcRet = RTERRINFO_LOG_REL_SET_F(pErrInfo, VERR_NEM_RING3_ONLY,
967 "Problem figuring out VidStopVirtualProcessor: fRet=%u dwErr=%u",
968 fRet, GetLastError()) );
969 LogRel(("NEM: VidStopVirtualProcessor -> fun:%#x in:%#x out:%#x\n", g_IoCtlStopVirtualProcessor.uFunction,
970 g_IoCtlStopVirtualProcessor.cbInput, g_IoCtlStopVirtualProcessor.cbOutput));
971
972 /* VidMessageSlotHandleAndGetNext */
973 *g_ppfnVidNtDeviceIoControlFile = nemR3WinIoctlDetector_MessageSlotHandleAndGetNext;
974 fRet = g_pfnVidMessageSlotHandleAndGetNext(NEM_WIN_IOCTL_DETECTOR_FAKE_HANDLE,
975 NEM_WIN_IOCTL_DETECTOR_FAKE_VP_INDEX, VID_MSHAGN_F_HANDLE_MESSAGE,
976 NEM_WIN_IOCTL_DETECTOR_FAKE_TIMEOUT);
977 *g_ppfnVidNtDeviceIoControlFile = pfnOrg;
978 AssertStmt(fRet && g_IoCtlMessageSlotHandleAndGetNext.uFunction != 0,
979 rcRet = RTERRINFO_LOG_REL_SET_F(pErrInfo, VERR_NEM_RING3_ONLY,
980 "Problem figuring out VidMessageSlotHandleAndGetNext: fRet=%u dwErr=%u",
981 fRet, GetLastError()) );
982 LogRel(("NEM: VidMessageSlotHandleAndGetNext -> fun:%#x in:%#x out:%#x\n",
983 g_IoCtlMessageSlotHandleAndGetNext.uFunction, g_IoCtlMessageSlotHandleAndGetNext.cbInput,
984 g_IoCtlMessageSlotHandleAndGetNext.cbOutput));
985
986#ifdef LOG_ENABLED
987 /* The following are only for logging: */
988 union
989 {
990 VID_MAPPED_MESSAGE_SLOT MapSlot;
991 HV_REGISTER_NAME Name;
992 HV_REGISTER_VALUE Value;
993 } uBuf;
994
995 /* VidMessageSlotMap */
996 g_pIoCtlDetectForLogging = &g_IoCtlMessageSlotMap;
997 *g_ppfnVidNtDeviceIoControlFile = nemR3WinIoctlDetector_ForLogging;
998 fRet = g_pfnVidMessageSlotMap(NEM_WIN_IOCTL_DETECTOR_FAKE_HANDLE, &uBuf.MapSlot, NEM_WIN_IOCTL_DETECTOR_FAKE_VP_INDEX);
999 *g_ppfnVidNtDeviceIoControlFile = pfnOrg;
1000 Assert(fRet);
1001 LogRel(("NEM: VidMessageSlotMap -> fun:%#x in:%#x out:%#x\n", g_pIoCtlDetectForLogging->uFunction,
1002 g_pIoCtlDetectForLogging->cbInput, g_pIoCtlDetectForLogging->cbOutput));
1003
1004 /* VidGetVirtualProcessorState */
1005 uBuf.Name = HvRegisterExplicitSuspend;
1006 g_pIoCtlDetectForLogging = &g_IoCtlGetVirtualProcessorState;
1007 *g_ppfnVidNtDeviceIoControlFile = nemR3WinIoctlDetector_ForLogging;
1008 fRet = g_pfnVidGetVirtualProcessorState(NEM_WIN_IOCTL_DETECTOR_FAKE_HANDLE, NEM_WIN_IOCTL_DETECTOR_FAKE_VP_INDEX,
1009 &uBuf.Name, 1, &uBuf.Value);
1010 *g_ppfnVidNtDeviceIoControlFile = pfnOrg;
1011 Assert(fRet);
1012 LogRel(("NEM: VidGetVirtualProcessorState -> fun:%#x in:%#x out:%#x\n", g_pIoCtlDetectForLogging->uFunction,
1013 g_pIoCtlDetectForLogging->cbInput, g_pIoCtlDetectForLogging->cbOutput));
1014
1015 /* VidSetVirtualProcessorState */
1016 uBuf.Name = HvRegisterExplicitSuspend;
1017 g_pIoCtlDetectForLogging = &g_IoCtlSetVirtualProcessorState;
1018 *g_ppfnVidNtDeviceIoControlFile = nemR3WinIoctlDetector_ForLogging;
1019 fRet = g_pfnVidSetVirtualProcessorState(NEM_WIN_IOCTL_DETECTOR_FAKE_HANDLE, NEM_WIN_IOCTL_DETECTOR_FAKE_VP_INDEX,
1020 &uBuf.Name, 1, &uBuf.Value);
1021 *g_ppfnVidNtDeviceIoControlFile = pfnOrg;
1022 Assert(fRet);
1023 LogRel(("NEM: VidSetVirtualProcessorState -> fun:%#x in:%#x out:%#x\n", g_pIoCtlDetectForLogging->uFunction,
1024 g_pIoCtlDetectForLogging->cbInput, g_pIoCtlDetectForLogging->cbOutput));
1025
1026 g_pIoCtlDetectForLogging = NULL;
1027#endif
1028
1029 /* Done. */
1030 pVM->nem.s.IoCtlGetHvPartitionId = g_IoCtlGetHvPartitionId;
1031 pVM->nem.s.IoCtlStartVirtualProcessor = g_IoCtlStartVirtualProcessor;
1032 pVM->nem.s.IoCtlStopVirtualProcessor = g_IoCtlStopVirtualProcessor;
1033 pVM->nem.s.IoCtlMessageSlotHandleAndGetNext = g_IoCtlMessageSlotHandleAndGetNext;
1034 return rcRet;
1035}
1036
1037
1038/**
1039 * Creates and sets up a Hyper-V (exo) partition.
1040 *
1041 * @returns VBox status code.
1042 * @param pVM The cross context VM structure.
1043 * @param pErrInfo Where to always return error info.
1044 */
1045static int nemR3WinInitCreatePartition(PVM pVM, PRTERRINFO pErrInfo)
1046{
1047 AssertReturn(!pVM->nem.s.hPartition, RTErrInfoSet(pErrInfo, VERR_WRONG_ORDER, "Wrong initalization order"));
1048 AssertReturn(!pVM->nem.s.hPartitionDevice, RTErrInfoSet(pErrInfo, VERR_WRONG_ORDER, "Wrong initalization order"));
1049
1050 /*
1051 * Create the partition.
1052 */
1053 WHV_PARTITION_HANDLE hPartition;
1054 HRESULT hrc = WHvCreatePartition(&hPartition);
1055 if (FAILED(hrc))
1056 return RTErrInfoSetF(pErrInfo, VERR_NEM_VM_CREATE_FAILED, "WHvCreatePartition failed with %Rhrc (Last=%#x/%u)",
1057 hrc, RTNtLastStatusValue(), RTNtLastErrorValue());
1058
1059 int rc;
1060
1061 /*
1062 * Set partition properties, most importantly the CPU count.
1063 */
1064 /**
1065 * @todo Someone at Microsoft please explain another weird API:
1066 * - Why this API doesn't take the WHV_PARTITION_PROPERTY_CODE value as an
1067 * argument rather than as part of the struct. That is so weird if you've
1068 * used any other NT or windows API, including WHvGetCapability().
1069 * - Why use PVOID when WHV_PARTITION_PROPERTY is what's expected. We
1070 * technically only need 9 bytes for setting/getting
1071 * WHVPartitionPropertyCodeProcessorClFlushSize, but the API insists on 16. */
1072 WHV_PARTITION_PROPERTY Property;
1073 RT_ZERO(Property);
1074 Property.ProcessorCount = pVM->cCpus;
1075 hrc = WHvSetPartitionProperty(hPartition, WHvPartitionPropertyCodeProcessorCount, &Property, sizeof(Property));
1076 if (SUCCEEDED(hrc))
1077 {
1078 RT_ZERO(Property);
1079 Property.ExtendedVmExits.X64CpuidExit = pVM->nem.s.fExtendedCpuIdExit; /** @todo Register fixed results and restrict cpuid exits */
1080 Property.ExtendedVmExits.X64MsrExit = pVM->nem.s.fExtendedMsrExit;
1081 Property.ExtendedVmExits.ExceptionExit = pVM->nem.s.fExtendedXcptExit;
1082 hrc = WHvSetPartitionProperty(hPartition, WHvPartitionPropertyCodeExtendedVmExits, &Property, sizeof(Property));
1083 if (SUCCEEDED(hrc))
1084 {
1085 /*
1086 * We'll continue setup in nemR3NativeInitAfterCPUM.
1087 */
1088 pVM->nem.s.fCreatedEmts = false;
1089 pVM->nem.s.hPartition = hPartition;
1090 LogRel(("NEM: Created partition %p.\n", hPartition));
1091 return VINF_SUCCESS;
1092 }
1093
1094 rc = RTErrInfoSetF(pErrInfo, VERR_NEM_VM_CREATE_FAILED,
1095 "Failed setting WHvPartitionPropertyCodeExtendedVmExits to %'#RX64: %Rhrc",
1096 Property.ExtendedVmExits.AsUINT64, hrc);
1097 }
1098 else
1099 rc = RTErrInfoSetF(pErrInfo, VERR_NEM_VM_CREATE_FAILED,
1100 "Failed setting WHvPartitionPropertyCodeProcessorCount to %u: %Rhrc (Last=%#x/%u)",
1101 pVM->cCpus, hrc, RTNtLastStatusValue(), RTNtLastErrorValue());
1102 WHvDeletePartition(hPartition);
1103
1104 Assert(!pVM->nem.s.hPartitionDevice);
1105 Assert(!pVM->nem.s.hPartition);
1106 return rc;
1107}
1108
1109
1110/**
1111 * Makes sure APIC and firmware will not allow X2APIC mode.
1112 *
1113 * This is rather ugly.
1114 *
1115 * @returns VBox status code
1116 * @param pVM The cross context VM structure.
1117 */
1118static int nemR3WinDisableX2Apic(PVM pVM)
1119{
1120 /*
1121 * First make sure the 'Mode' config value of the APIC isn't set to X2APIC.
1122 * This defaults to APIC, so no need to change unless it's X2APIC.
1123 */
1124 PCFGMNODE pCfg = CFGMR3GetChild(CFGMR3GetRoot(pVM), "/Devices/apic/0/Config");
1125 if (pCfg)
1126 {
1127 uint8_t bMode = 0;
1128 int rc = CFGMR3QueryU8(pCfg, "Mode", &bMode);
1129 AssertLogRelMsgReturn(RT_SUCCESS(rc) || rc == VERR_CFGM_VALUE_NOT_FOUND, ("%Rrc\n", rc), rc);
1130 if (RT_SUCCESS(rc) && bMode == PDMAPICMODE_X2APIC)
1131 {
1132 LogRel(("NEM: Adjusting APIC configuration from X2APIC to APIC max mode. X2APIC is not supported by the WinHvPlatform API!\n"));
1133 LogRel(("NEM: Disable Hyper-V if you need X2APIC for your guests!\n"));
1134 rc = CFGMR3RemoveValue(pCfg, "Mode");
1135 rc = CFGMR3InsertInteger(pCfg, "Mode", PDMAPICMODE_APIC);
1136 AssertLogRelRCReturn(rc, rc);
1137 }
1138 }
1139
1140 /*
1141 * Now the firmwares.
1142 * These also defaults to APIC and only needs adjusting if configured to X2APIC (2).
1143 */
1144 static const char * const s_apszFirmwareConfigs[] =
1145 {
1146 "/Devices/efi/0/Config",
1147 "/Devices/pcbios/0/Config",
1148 };
1149 for (unsigned i = 0; i < RT_ELEMENTS(s_apszFirmwareConfigs); i++)
1150 {
1151 pCfg = CFGMR3GetChild(CFGMR3GetRoot(pVM), "/Devices/APIC/0/Config");
1152 if (pCfg)
1153 {
1154 uint8_t bMode = 0;
1155 int rc = CFGMR3QueryU8(pCfg, "APIC", &bMode);
1156 AssertLogRelMsgReturn(RT_SUCCESS(rc) || rc == VERR_CFGM_VALUE_NOT_FOUND, ("%Rrc\n", rc), rc);
1157 if (RT_SUCCESS(rc) && bMode == 2)
1158 {
1159 LogRel(("NEM: Adjusting %s/Mode from 2 (X2APIC) to 1 (APIC).\n", s_apszFirmwareConfigs[i]));
1160 rc = CFGMR3RemoveValue(pCfg, "APIC");
1161 rc = CFGMR3InsertInteger(pCfg, "APIC", 1);
1162 AssertLogRelRCReturn(rc, rc);
1163 }
1164 }
1165 }
1166
1167 return VINF_SUCCESS;
1168}
1169
1170
1171/**
1172 * Try initialize the native API.
1173 *
1174 * This may only do part of the job, more can be done in
1175 * nemR3NativeInitAfterCPUM() and nemR3NativeInitCompleted().
1176 *
1177 * @returns VBox status code.
1178 * @param pVM The cross context VM structure.
1179 * @param fFallback Whether we're in fallback mode or use-NEM mode. In
1180 * the latter we'll fail if we cannot initialize.
1181 * @param fForced Whether the HMForced flag is set and we should
1182 * fail if we cannot initialize.
1183 */
1184int nemR3NativeInit(PVM pVM, bool fFallback, bool fForced)
1185{
1186 g_uBuildNo = RTSystemGetNtBuildNo();
1187
1188 /*
1189 * Some state init.
1190 */
1191 pVM->nem.s.fA20Enabled = true;
1192#if 0
1193 for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
1194 {
1195 PNEMCPU pNemCpu = &pVM->aCpus[iCpu].nem.s;
1196 }
1197#endif
1198
1199 /*
1200 * Error state.
1201 * The error message will be non-empty on failure and 'rc' will be set too.
1202 */
1203 RTERRINFOSTATIC ErrInfo;
1204 PRTERRINFO pErrInfo = RTErrInfoInitStatic(&ErrInfo);
1205 int rc = nemR3WinInitProbeAndLoad(fForced, pErrInfo);
1206 if (RT_SUCCESS(rc))
1207 {
1208 /*
1209 * Check the capabilties of the hypervisor, starting with whether it's present.
1210 */
1211 rc = nemR3WinInitCheckCapabilities(pVM, pErrInfo);
1212 if (RT_SUCCESS(rc))
1213 {
1214 /*
1215 * Discover the VID I/O control function numbers we need.
1216 */
1217 rc = nemR3WinInitDiscoverIoControlProperties(pVM, pErrInfo);
1218 if (rc == VERR_NEM_RING3_ONLY)
1219 {
1220 if (pVM->nem.s.fUseRing0Runloop)
1221 {
1222 LogRel(("NEM: Disabling UseRing0Runloop.\n"));
1223 pVM->nem.s.fUseRing0Runloop = false;
1224 }
1225 rc = VINF_SUCCESS;
1226 }
1227 if (RT_SUCCESS(rc))
1228 {
1229 /*
1230 * Check out our ring-0 capabilities.
1231 */
1232 rc = SUPR3CallVMMR0Ex(pVM->pVMR0, 0 /*idCpu*/, VMMR0_DO_NEM_INIT_VM, 0, NULL);
1233 if (RT_SUCCESS(rc))
1234 {
1235 /*
1236 * Create and initialize a partition.
1237 */
1238 rc = nemR3WinInitCreatePartition(pVM, pErrInfo);
1239 if (RT_SUCCESS(rc))
1240 {
1241 VM_SET_MAIN_EXECUTION_ENGINE(pVM, VM_EXEC_ENGINE_NATIVE_API);
1242 Log(("NEM: Marked active!\n"));
1243 nemR3WinDisableX2Apic(pVM);
1244
1245 /* Register release statistics */
1246 for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
1247 {
1248 PNEMCPU pNemCpu = &pVM->aCpus[iCpu].nem.s;
1249 STAMR3RegisterF(pVM, &pNemCpu->StatExitPortIo, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of port I/O exits", "/NEM/CPU%u/ExitPortIo", iCpu);
1250 STAMR3RegisterF(pVM, &pNemCpu->StatExitMemUnmapped, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of unmapped memory exits", "/NEM/CPU%u/ExitMemUnmapped", iCpu);
1251 STAMR3RegisterF(pVM, &pNemCpu->StatExitMemIntercept, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of intercepted memory exits", "/NEM/CPU%u/ExitMemIntercept", iCpu);
1252 STAMR3RegisterF(pVM, &pNemCpu->StatExitHalt, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of HLT exits", "/NEM/CPU%u/ExitHalt", iCpu);
1253 STAMR3RegisterF(pVM, &pNemCpu->StatExitInterruptWindow, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of HLT exits", "/NEM/CPU%u/ExitInterruptWindow", iCpu);
1254 STAMR3RegisterF(pVM, &pNemCpu->StatExitCpuId, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of CPUID exits", "/NEM/CPU%u/ExitCpuId", iCpu);
1255 STAMR3RegisterF(pVM, &pNemCpu->StatExitMsr, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of MSR access exits", "/NEM/CPU%u/ExitMsr", iCpu);
1256 STAMR3RegisterF(pVM, &pNemCpu->StatExitException, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of exception exits", "/NEM/CPU%u/ExitException", iCpu);
1257 STAMR3RegisterF(pVM, &pNemCpu->StatExitExceptionBp, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of #BP exits", "/NEM/CPU%u/ExitExceptionBp", iCpu);
1258 STAMR3RegisterF(pVM, &pNemCpu->StatExitExceptionDb, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of #DB exits", "/NEM/CPU%u/ExitExceptionDb", iCpu);
1259 STAMR3RegisterF(pVM, &pNemCpu->StatExitExceptionUd, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of #UD exits", "/NEM/CPU%u/ExitExceptionUd", iCpu);
1260 STAMR3RegisterF(pVM, &pNemCpu->StatExitExceptionUdHandled, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of handled #UD exits", "/NEM/CPU%u/ExitExceptionUdHandled", iCpu);
1261 STAMR3RegisterF(pVM, &pNemCpu->StatExitUnrecoverable, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of unrecoverable exits", "/NEM/CPU%u/ExitUnrecoverable", iCpu);
1262 STAMR3RegisterF(pVM, &pNemCpu->StatGetMsgTimeout, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of get message timeouts/alerts", "/NEM/CPU%u/GetMsgTimeout", iCpu);
1263 STAMR3RegisterF(pVM, &pNemCpu->StatStopCpuSuccess, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of successful CPU stops", "/NEM/CPU%u/StopCpuSuccess", iCpu);
1264 STAMR3RegisterF(pVM, &pNemCpu->StatStopCpuPending, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of pending CPU stops", "/NEM/CPU%u/StopCpuPending", iCpu);
1265 STAMR3RegisterF(pVM, &pNemCpu->StatStopCpuPendingAlerts,STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of pending CPU stop alerts", "/NEM/CPU%u/StopCpuPendingAlerts", iCpu);
1266 STAMR3RegisterF(pVM, &pNemCpu->StatStopCpuPendingOdd, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of odd pending CPU stops (see code)", "/NEM/CPU%u/StopCpuPendingOdd", iCpu);
1267 STAMR3RegisterF(pVM, &pNemCpu->StatCancelChangedState, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of cancel changed state", "/NEM/CPU%u/CancelChangedState", iCpu);
1268 STAMR3RegisterF(pVM, &pNemCpu->StatCancelAlertedThread, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of cancel alerted EMT", "/NEM/CPU%u/CancelAlertedEMT", iCpu);
1269 STAMR3RegisterF(pVM, &pNemCpu->StatBreakOnFFPre, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of pre execution FF breaks", "/NEM/CPU%u/BreakOnFFPre", iCpu);
1270 STAMR3RegisterF(pVM, &pNemCpu->StatBreakOnFFPost, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of post execution FF breaks", "/NEM/CPU%u/BreakOnFFPost", iCpu);
1271 STAMR3RegisterF(pVM, &pNemCpu->StatBreakOnCancel, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of cancel execution breaks", "/NEM/CPU%u/BreakOnCancel", iCpu);
1272 STAMR3RegisterF(pVM, &pNemCpu->StatBreakOnStatus, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of status code breaks", "/NEM/CPU%u/BreakOnStatus", iCpu);
1273 STAMR3RegisterF(pVM, &pNemCpu->StatImportOnDemand, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of on-demand state imports", "/NEM/CPU%u/ImportOnDemand", iCpu);
1274 STAMR3RegisterF(pVM, &pNemCpu->StatImportOnReturn, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of state imports on loop return", "/NEM/CPU%u/ImportOnReturn", iCpu);
1275 STAMR3RegisterF(pVM, &pNemCpu->StatImportOnReturnSkipped, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of skipped state imports on loop return", "/NEM/CPU%u/ImportOnReturnSkipped", iCpu);
1276 STAMR3RegisterF(pVM, &pNemCpu->StatQueryCpuTick, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of TSC queries", "/NEM/CPU%u/QueryCpuTick", iCpu);
1277 }
1278
1279 PUVM pUVM = pVM->pUVM;
1280 STAMR3RegisterRefresh(pUVM, &pVM->nem.s.R0Stats.cPagesAvailable, STAMTYPE_U64, STAMVISIBILITY_ALWAYS,
1281 STAMUNIT_PAGES, STAM_REFRESH_GRP_NEM, "Free pages available to the hypervisor",
1282 "/NEM/R0Stats/cPagesAvailable");
1283 STAMR3RegisterRefresh(pUVM, &pVM->nem.s.R0Stats.cPagesInUse, STAMTYPE_U64, STAMVISIBILITY_ALWAYS,
1284 STAMUNIT_PAGES, STAM_REFRESH_GRP_NEM, "Pages in use by hypervisor",
1285 "/NEM/R0Stats/cPagesInUse");
1286 }
1287 }
1288 }
1289 }
1290 }
1291
1292 /*
1293 * We only fail if in forced mode, otherwise just log the complaint and return.
1294 */
1295 Assert(pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API || RTErrInfoIsSet(pErrInfo));
1296 if ( (fForced || !fFallback)
1297 && pVM->bMainExecutionEngine != VM_EXEC_ENGINE_NATIVE_API)
1298 return VMSetError(pVM, RT_SUCCESS_NP(rc) ? VERR_NEM_NOT_AVAILABLE : rc, RT_SRC_POS, "%s", pErrInfo->pszMsg);
1299
1300 if (RTErrInfoIsSet(pErrInfo))
1301 LogRel(("NEM: Not available: %s\n", pErrInfo->pszMsg));
1302 return VINF_SUCCESS;
1303}
1304
1305
1306/**
1307 * This is called after CPUMR3Init is done.
1308 *
1309 * @returns VBox status code.
1310 * @param pVM The VM handle..
1311 */
1312int nemR3NativeInitAfterCPUM(PVM pVM)
1313{
1314 /*
1315 * Validate sanity.
1316 */
1317 WHV_PARTITION_HANDLE hPartition = pVM->nem.s.hPartition;
1318 AssertReturn(hPartition != NULL, VERR_WRONG_ORDER);
1319 AssertReturn(!pVM->nem.s.hPartitionDevice, VERR_WRONG_ORDER);
1320 AssertReturn(!pVM->nem.s.fCreatedEmts, VERR_WRONG_ORDER);
1321 AssertReturn(pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NATIVE_API, VERR_WRONG_ORDER);
1322
1323 /*
1324 * Continue setting up the partition now that we've got most of the CPUID feature stuff.
1325 */
1326 WHV_PARTITION_PROPERTY Property;
1327 HRESULT hrc;
1328
1329#if 0
1330 /* Not sure if we really need to set the vendor.
1331 Update: Apparently we don't. WHvPartitionPropertyCodeProcessorVendor was removed in 17110. */
1332 RT_ZERO(Property);
1333 Property.ProcessorVendor = pVM->nem.s.enmCpuVendor == CPUMCPUVENDOR_AMD ? WHvProcessorVendorAmd
1334 : WHvProcessorVendorIntel;
1335 hrc = WHvSetPartitionProperty(hPartition, WHvPartitionPropertyCodeProcessorVendor, &Property, sizeof(Property));
1336 if (FAILED(hrc))
1337 return VMSetError(pVM, VERR_NEM_VM_CREATE_FAILED, RT_SRC_POS,
1338 "Failed to set WHvPartitionPropertyCodeProcessorVendor to %u: %Rhrc (Last=%#x/%u)",
1339 Property.ProcessorVendor, hrc, RTNtLastStatusValue(), RTNtLastErrorValue());
1340#endif
1341
1342 /* Not sure if we really need to set the cache line flush size. */
1343 RT_ZERO(Property);
1344 Property.ProcessorClFlushSize = pVM->nem.s.cCacheLineFlushShift;
1345 hrc = WHvSetPartitionProperty(hPartition, WHvPartitionPropertyCodeProcessorClFlushSize, &Property, sizeof(Property));
1346 if (FAILED(hrc))
1347 return VMSetError(pVM, VERR_NEM_VM_CREATE_FAILED, RT_SRC_POS,
1348 "Failed to set WHvPartitionPropertyCodeProcessorClFlushSize to %u: %Rhrc (Last=%#x/%u)",
1349 pVM->nem.s.cCacheLineFlushShift, hrc, RTNtLastStatusValue(), RTNtLastErrorValue());
1350
1351 /* Intercept #DB, #BP and #UD exceptions. */
1352 RT_ZERO(Property);
1353 Property.ExceptionExitBitmap = RT_BIT_64(WHvX64ExceptionTypeDebugTrapOrFault)
1354 | RT_BIT_64(WHvX64ExceptionTypeBreakpointTrap)
1355 | RT_BIT_64(WHvX64ExceptionTypeInvalidOpcodeFault);
1356 hrc = WHvSetPartitionProperty(hPartition, WHvPartitionPropertyCodeExceptionExitBitmap, &Property, sizeof(Property));
1357 if (FAILED(hrc))
1358 return VMSetError(pVM, VERR_NEM_VM_CREATE_FAILED, RT_SRC_POS,
1359 "Failed to set WHvPartitionPropertyCodeExceptionExitBitmap to %#RX64: %Rhrc (Last=%#x/%u)",
1360 Property.ExceptionExitBitmap, hrc, RTNtLastStatusValue(), RTNtLastErrorValue());
1361
1362
1363 /*
1364 * Sync CPU features with CPUM.
1365 */
1366 /** @todo sync CPU features with CPUM. */
1367
1368 /* Set the partition property. */
1369 RT_ZERO(Property);
1370 Property.ProcessorFeatures.AsUINT64 = pVM->nem.s.uCpuFeatures.u64;
1371 hrc = WHvSetPartitionProperty(hPartition, WHvPartitionPropertyCodeProcessorFeatures, &Property, sizeof(Property));
1372 if (FAILED(hrc))
1373 return VMSetError(pVM, VERR_NEM_VM_CREATE_FAILED, RT_SRC_POS,
1374 "Failed to set WHvPartitionPropertyCodeProcessorFeatures to %'#RX64: %Rhrc (Last=%#x/%u)",
1375 pVM->nem.s.uCpuFeatures.u64, hrc, RTNtLastStatusValue(), RTNtLastErrorValue());
1376
1377 /*
1378 * Set up the partition and create EMTs.
1379 *
1380 * Seems like this is where the partition is actually instantiated and we get
1381 * a handle to it.
1382 */
1383 hrc = WHvSetupPartition(hPartition);
1384 if (FAILED(hrc))
1385 return VMSetError(pVM, VERR_NEM_VM_CREATE_FAILED, RT_SRC_POS,
1386 "Call to WHvSetupPartition failed: %Rhrc (Last=%#x/%u)",
1387 hrc, RTNtLastStatusValue(), RTNtLastErrorValue());
1388
1389 /* Get the handle. */
1390 HANDLE hPartitionDevice;
1391 __try
1392 {
1393 hPartitionDevice = ((HANDLE *)hPartition)[1];
1394 }
1395 __except(EXCEPTION_EXECUTE_HANDLER)
1396 {
1397 hrc = GetExceptionCode();
1398 hPartitionDevice = NULL;
1399 }
1400 if ( hPartitionDevice == NULL
1401 || hPartitionDevice == (HANDLE)(intptr_t)-1)
1402 return VMSetError(pVM, VERR_NEM_VM_CREATE_FAILED, RT_SRC_POS,
1403 "Failed to get device handle for partition %p: %Rhrc", hPartition, hrc);
1404
1405 HV_PARTITION_ID idHvPartition = HV_PARTITION_ID_INVALID;
1406 if (!g_pfnVidGetHvPartitionId(hPartitionDevice, &idHvPartition))
1407 return VMSetError(pVM, VERR_NEM_VM_CREATE_FAILED, RT_SRC_POS,
1408 "Failed to get device handle and/or partition ID for %p (hPartitionDevice=%p, Last=%#x/%u)",
1409 hPartition, hPartitionDevice, RTNtLastStatusValue(), RTNtLastErrorValue());
1410 pVM->nem.s.hPartitionDevice = hPartitionDevice;
1411 pVM->nem.s.idHvPartition = idHvPartition;
1412
1413 /*
1414 * Setup the EMTs.
1415 */
1416 VMCPUID iCpu;
1417 for (iCpu = 0; iCpu < pVM->cCpus; iCpu++)
1418 {
1419 PVMCPU pVCpu = &pVM->aCpus[iCpu];
1420
1421 pVCpu->nem.s.hNativeThreadHandle = (RTR3PTR)RTThreadGetNativeHandle(VMR3GetThreadHandle(pVCpu->pUVCpu));
1422 Assert((HANDLE)pVCpu->nem.s.hNativeThreadHandle != INVALID_HANDLE_VALUE);
1423
1424#ifndef NEM_WIN_USE_OUR_OWN_RUN_API
1425# ifdef NEM_WIN_WITH_RING0_RUNLOOP
1426 if (!pVM->nem.s.fUseRing0Runloop)
1427# endif
1428 {
1429 hrc = WHvCreateVirtualProcessor(hPartition, iCpu, 0 /*fFlags*/);
1430 if (FAILED(hrc))
1431 {
1432 NTSTATUS const rcNtLast = RTNtLastStatusValue();
1433 DWORD const dwErrLast = RTNtLastErrorValue();
1434 while (iCpu-- > 0)
1435 {
1436 HRESULT hrc2 = WHvDeleteVirtualProcessor(hPartition, iCpu);
1437 AssertLogRelMsg(SUCCEEDED(hrc2), ("WHvDeleteVirtualProcessor(%p, %u) -> %Rhrc (Last=%#x/%u)\n",
1438 hPartition, iCpu, hrc2, RTNtLastStatusValue(),
1439 RTNtLastErrorValue()));
1440 }
1441 return VMSetError(pVM, VERR_NEM_VM_CREATE_FAILED, RT_SRC_POS,
1442 "Call to WHvSetupPartition failed: %Rhrc (Last=%#x/%u)", hrc, rcNtLast, dwErrLast);
1443 }
1444 }
1445# ifdef NEM_WIN_WITH_RING0_RUNLOOP
1446 else
1447# endif
1448#endif /* !NEM_WIN_USE_OUR_OWN_RUN_API */
1449#if defined(NEM_WIN_WITH_RING0_RUNLOOP) || defined(NEM_WIN_USE_OUR_OWN_RUN_API)
1450 {
1451 VID_MAPPED_MESSAGE_SLOT MappedMsgSlot = { NULL, UINT32_MAX, UINT32_MAX };
1452 if (g_pfnVidMessageSlotMap(hPartitionDevice, &MappedMsgSlot, iCpu))
1453 {
1454 AssertLogRelMsg(MappedMsgSlot.iCpu == iCpu && MappedMsgSlot.uParentAdvisory == UINT32_MAX,
1455 ("%#x %#x (iCpu=%#x)\n", MappedMsgSlot.iCpu, MappedMsgSlot.uParentAdvisory, iCpu));
1456 pVCpu->nem.s.pvMsgSlotMapping = MappedMsgSlot.pMsgBlock;
1457 }
1458 else
1459 {
1460 NTSTATUS const rcNtLast = RTNtLastStatusValue();
1461 DWORD const dwErrLast = RTNtLastErrorValue();
1462 return VMSetError(pVM, VERR_NEM_VM_CREATE_FAILED, RT_SRC_POS,
1463 "Call to WHvSetupPartition failed: %Rhrc (Last=%#x/%u)", hrc, rcNtLast, dwErrLast);
1464 }
1465 }
1466#endif
1467 }
1468 pVM->nem.s.fCreatedEmts = true;
1469
1470 /*
1471 * Do some more ring-0 initialization now that we've got the partition handle.
1472 */
1473 int rc = VMMR3CallR0Emt(pVM, &pVM->aCpus[0], VMMR0_DO_NEM_INIT_VM_PART_2, 0, NULL);
1474 if (RT_SUCCESS(rc))
1475 {
1476 LogRel(("NEM: Successfully set up partition (device handle %p, partition ID %#llx)\n", hPartitionDevice, idHvPartition));
1477
1478#if 1
1479 VMMR3CallR0Emt(pVM, &pVM->aCpus[0], VMMR0_DO_NEM_UPDATE_STATISTICS, 0, NULL);
1480 LogRel(("NEM: Memory balance: %#RX64 out of %#RX64 pages in use\n",
1481 pVM->nem.s.R0Stats.cPagesInUse, pVM->nem.s.R0Stats.cPagesAvailable));
1482#endif
1483
1484 /*
1485 * Register statistics on shared pages.
1486 */
1487 /** @todo HvCallMapStatsPage */
1488
1489 /*
1490 * Adjust features.
1491 * Note! We've already disabled X2APIC via CFGM during the first init call.
1492 */
1493
1494#if 0 && defined(DEBUG_bird)
1495 /*
1496 * Poke and probe a little.
1497 */
1498 PVMCPU pVCpu = &pVM->aCpus[0];
1499 uint32_t aRegNames[1024];
1500 HV_REGISTER_VALUE aRegValues[1024];
1501 uint32_t aPropCodes[128];
1502 uint64_t aPropValues[128];
1503 for (int iOuter = 0; iOuter < 5; iOuter++)
1504 {
1505 LogRel(("\niOuter %d\n", iOuter));
1506# if 1
1507 /* registers */
1508 uint32_t iRegValue = 0;
1509 uint32_t cRegChanges = 0;
1510 for (uint32_t iReg = 0; iReg < 0x001101ff; iReg++)
1511 {
1512 if (iOuter != 0 && aRegNames[iRegValue] > iReg)
1513 continue;
1514 RT_ZERO(pVCpu->nem.s.Hypercall.Experiment);
1515 pVCpu->nem.s.Hypercall.Experiment.uItem = iReg;
1516 int rc2 = VMMR3CallR0Emt(pVM, pVCpu, VMMR0_DO_NEM_EXPERIMENT, 0, NULL);
1517 AssertLogRelRCBreak(rc2);
1518 if (pVCpu->nem.s.Hypercall.Experiment.fSuccess)
1519 {
1520 LogRel(("Register %#010x = %#18RX64, %#18RX64\n", iReg,
1521 pVCpu->nem.s.Hypercall.Experiment.uLoValue, pVCpu->nem.s.Hypercall.Experiment.uHiValue));
1522 if (iReg == HvX64RegisterTsc)
1523 {
1524 uint64_t uTsc = ASMReadTSC();
1525 LogRel(("TSC = %#18RX64; Delta %#18RX64 or %#18RX64\n",
1526 uTsc, pVCpu->nem.s.Hypercall.Experiment.uLoValue - uTsc, uTsc - pVCpu->nem.s.Hypercall.Experiment.uLoValue));
1527 }
1528
1529 if (iOuter == 0)
1530 aRegNames[iRegValue] = iReg;
1531 else if( aRegValues[iRegValue].Reg128.Low64 != pVCpu->nem.s.Hypercall.Experiment.uLoValue
1532 || aRegValues[iRegValue].Reg128.High64 != pVCpu->nem.s.Hypercall.Experiment.uHiValue)
1533 {
1534 LogRel(("Changed from %#18RX64, %#18RX64 !!\n",
1535 aRegValues[iRegValue].Reg128.Low64, aRegValues[iRegValue].Reg128.High64));
1536 LogRel(("Delta %#18RX64, %#18RX64 !!\n",
1537 pVCpu->nem.s.Hypercall.Experiment.uLoValue - aRegValues[iRegValue].Reg128.Low64,
1538 pVCpu->nem.s.Hypercall.Experiment.uHiValue - aRegValues[iRegValue].Reg128.High64));
1539 cRegChanges++;
1540 }
1541 aRegValues[iRegValue].Reg128.Low64 = pVCpu->nem.s.Hypercall.Experiment.uLoValue;
1542 aRegValues[iRegValue].Reg128.High64 = pVCpu->nem.s.Hypercall.Experiment.uHiValue;
1543 iRegValue++;
1544 AssertBreak(iRegValue < RT_ELEMENTS(aRegValues));
1545 }
1546 }
1547 LogRel(("Found %u registers, %u changed\n", iRegValue, cRegChanges));
1548# endif
1549# if 1
1550 /* partition properties */
1551 uint32_t iPropValue = 0;
1552 uint32_t cPropChanges = 0;
1553 for (uint32_t iProp = 0; iProp < 0xc11ff; iProp++)
1554 {
1555 if (iProp == HvPartitionPropertyDebugChannelId /* hangs host */)
1556 continue;
1557 if (iOuter != 0 && aPropCodes[iPropValue] > iProp)
1558 continue;
1559 RT_ZERO(pVCpu->nem.s.Hypercall.Experiment);
1560 pVCpu->nem.s.Hypercall.Experiment.uItem = iProp;
1561 int rc2 = VMMR3CallR0Emt(pVM, pVCpu, VMMR0_DO_NEM_EXPERIMENT, 1, NULL);
1562 AssertLogRelRCBreak(rc2);
1563 if (pVCpu->nem.s.Hypercall.Experiment.fSuccess)
1564 {
1565 LogRel(("Property %#010x = %#18RX64\n", iProp, pVCpu->nem.s.Hypercall.Experiment.uLoValue));
1566 if (iOuter == 0)
1567 aPropCodes[iPropValue] = iProp;
1568 else if (aPropValues[iPropValue] != pVCpu->nem.s.Hypercall.Experiment.uLoValue)
1569 {
1570 LogRel(("Changed from %#18RX64, delta %#18RX64!!\n",
1571 aPropValues[iPropValue], pVCpu->nem.s.Hypercall.Experiment.uLoValue - aPropValues[iPropValue]));
1572 cRegChanges++;
1573 }
1574 aPropValues[iPropValue] = pVCpu->nem.s.Hypercall.Experiment.uLoValue;
1575 iPropValue++;
1576 AssertBreak(iPropValue < RT_ELEMENTS(aPropValues));
1577 }
1578 }
1579 LogRel(("Found %u properties, %u changed\n", iPropValue, cPropChanges));
1580# endif
1581
1582 /* Modify the TSC register value and see what changes. */
1583 if (iOuter != 0)
1584 {
1585 RT_ZERO(pVCpu->nem.s.Hypercall.Experiment);
1586 pVCpu->nem.s.Hypercall.Experiment.uItem = HvX64RegisterTsc;
1587 pVCpu->nem.s.Hypercall.Experiment.uHiValue = UINT64_C(0x00000fffffffffff) >> iOuter;
1588 pVCpu->nem.s.Hypercall.Experiment.uLoValue = UINT64_C(0x0011100000000000) << iOuter;
1589 VMMR3CallR0Emt(pVM, pVCpu, VMMR0_DO_NEM_EXPERIMENT, 2, NULL);
1590 LogRel(("Setting HvX64RegisterTsc -> %RTbool (%#RX64)\n", pVCpu->nem.s.Hypercall.Experiment.fSuccess, pVCpu->nem.s.Hypercall.Experiment.uStatus));
1591 }
1592
1593 RT_ZERO(pVCpu->nem.s.Hypercall.Experiment);
1594 pVCpu->nem.s.Hypercall.Experiment.uItem = HvX64RegisterTsc;
1595 VMMR3CallR0Emt(pVM, pVCpu, VMMR0_DO_NEM_EXPERIMENT, 0, NULL);
1596 LogRel(("HvX64RegisterTsc = %#RX64, %#RX64\n", pVCpu->nem.s.Hypercall.Experiment.uLoValue, pVCpu->nem.s.Hypercall.Experiment.uHiValue));
1597 }
1598
1599#endif
1600 return VINF_SUCCESS;
1601 }
1602 return VMSetError(pVM, VERR_NEM_VM_CREATE_FAILED, RT_SRC_POS, "Call to NEMR0InitVMPart2 failed: %Rrc", rc);
1603}
1604
1605
1606int nemR3NativeInitCompleted(PVM pVM, VMINITCOMPLETED enmWhat)
1607{
1608 //BOOL fRet = SetThreadPriority(GetCurrentThread(), 0);
1609 //AssertLogRel(fRet);
1610
1611 NOREF(pVM); NOREF(enmWhat);
1612 return VINF_SUCCESS;
1613}
1614
1615
1616int nemR3NativeTerm(PVM pVM)
1617{
1618 /*
1619 * Delete the partition.
1620 */
1621 WHV_PARTITION_HANDLE hPartition = pVM->nem.s.hPartition;
1622 pVM->nem.s.hPartition = NULL;
1623 pVM->nem.s.hPartitionDevice = NULL;
1624 if (hPartition != NULL)
1625 {
1626 VMCPUID iCpu = pVM->nem.s.fCreatedEmts ? pVM->cCpus : 0;
1627 LogRel(("NEM: Destroying partition %p with its %u VCpus...\n", hPartition, iCpu));
1628 while (iCpu-- > 0)
1629 {
1630 pVM->aCpus[iCpu].nem.s.pvMsgSlotMapping = NULL;
1631#ifndef NEM_WIN_USE_OUR_OWN_RUN_API
1632# ifdef NEM_WIN_WITH_RING0_RUNLOOP
1633 if (!pVM->nem.s.fUseRing0Runloop)
1634# endif
1635 {
1636 HRESULT hrc = WHvDeleteVirtualProcessor(hPartition, iCpu);
1637 AssertLogRelMsg(SUCCEEDED(hrc), ("WHvDeleteVirtualProcessor(%p, %u) -> %Rhrc (Last=%#x/%u)\n",
1638 hPartition, iCpu, hrc, RTNtLastStatusValue(),
1639 RTNtLastErrorValue()));
1640 }
1641#endif
1642 }
1643 WHvDeletePartition(hPartition);
1644 }
1645 pVM->nem.s.fCreatedEmts = false;
1646 return VINF_SUCCESS;
1647}
1648
1649
1650/**
1651 * VM reset notification.
1652 *
1653 * @param pVM The cross context VM structure.
1654 */
1655void nemR3NativeReset(PVM pVM)
1656{
1657 /* Unfix the A20 gate. */
1658 pVM->nem.s.fA20Fixed = false;
1659}
1660
1661
1662/**
1663 * Reset CPU due to INIT IPI or hot (un)plugging.
1664 *
1665 * @param pVCpu The cross context virtual CPU structure of the CPU being
1666 * reset.
1667 * @param fInitIpi Whether this is the INIT IPI or hot (un)plugging case.
1668 */
1669void nemR3NativeResetCpu(PVMCPU pVCpu, bool fInitIpi)
1670{
1671 /* Lock the A20 gate if INIT IPI, make sure it's enabled. */
1672 if (fInitIpi && pVCpu->idCpu > 0)
1673 {
1674 PVM pVM = pVCpu->CTX_SUFF(pVM);
1675 if (!pVM->nem.s.fA20Enabled)
1676 nemR3NativeNotifySetA20(pVCpu, true);
1677 pVM->nem.s.fA20Enabled = true;
1678 pVM->nem.s.fA20Fixed = true;
1679 }
1680}
1681
1682
1683VBOXSTRICTRC nemR3NativeRunGC(PVM pVM, PVMCPU pVCpu)
1684{
1685#ifdef NEM_WIN_WITH_RING0_RUNLOOP
1686 if (pVM->nem.s.fUseRing0Runloop)
1687 {
1688 for (;;)
1689 {
1690 VBOXSTRICTRC rcStrict = VMMR3CallR0EmtFast(pVM, pVCpu, VMMR0_DO_NEM_RUN);
1691 if (RT_SUCCESS(rcStrict))
1692 {
1693 /*
1694 * We deal with VINF_NEM_FLUSH_TLB here, since we're running the risk of
1695 * getting these while we already got another RC (I/O ports).
1696 */
1697 /* Status codes: */
1698 VBOXSTRICTRC rcPending = pVCpu->nem.s.rcPending;
1699 pVCpu->nem.s.rcPending = VINF_SUCCESS;
1700 if (rcStrict == VINF_NEM_FLUSH_TLB || rcPending == VINF_NEM_FLUSH_TLB)
1701 {
1702 LogFlow(("nemR3NativeRunGC: calling PGMFlushTLB...\n"));
1703 int rc = PGMFlushTLB(pVCpu, CPUMGetGuestCR3(pVCpu), true);
1704 AssertRCReturn(rc, rc);
1705 if (rcStrict == VINF_NEM_FLUSH_TLB)
1706 {
1707 if ( !VM_FF_IS_ANY_SET(pVM, VM_FF_HIGH_PRIORITY_POST_MASK | VM_FF_HP_R0_PRE_HM_MASK)
1708 && !VMCPU_FF_IS_ANY_SET(pVCpu, (VMCPU_FF_HIGH_PRIORITY_POST_MASK | VMCPU_FF_HP_R0_PRE_HM_MASK)
1709 & ~VMCPU_FF_RESUME_GUEST_MASK))
1710 {
1711 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_RESUME_GUEST_MASK);
1712 continue;
1713 }
1714 rcStrict = VINF_SUCCESS;
1715 }
1716 }
1717 else
1718 AssertMsg(rcPending == VINF_SUCCESS, ("rcPending=%Rrc\n", VBOXSTRICTRC_VAL(rcPending) ));
1719 }
1720 LogFlow(("nemR3NativeRunGC: returns %Rrc\n", VBOXSTRICTRC_VAL(rcStrict) ));
1721 return rcStrict;
1722 }
1723 }
1724#endif
1725 return nemHCWinRunGC(pVM, pVCpu, NULL /*pGVM*/, NULL /*pGVCpu*/);
1726}
1727
1728
1729bool nemR3NativeCanExecuteGuest(PVM pVM, PVMCPU pVCpu)
1730{
1731 NOREF(pVM); NOREF(pVCpu);
1732 return true;
1733}
1734
1735
1736bool nemR3NativeSetSingleInstruction(PVM pVM, PVMCPU pVCpu, bool fEnable)
1737{
1738 NOREF(pVM); NOREF(pVCpu); NOREF(fEnable);
1739 return false;
1740}
1741
1742
1743/**
1744 * Forced flag notification call from VMEmt.h.
1745 *
1746 * This is only called when pVCpu is in the VMCPUSTATE_STARTED_EXEC_NEM state.
1747 *
1748 * @param pVM The cross context VM structure.
1749 * @param pVCpu The cross context virtual CPU structure of the CPU
1750 * to be notified.
1751 * @param fFlags Notification flags, VMNOTIFYFF_FLAGS_XXX.
1752 */
1753void nemR3NativeNotifyFF(PVM pVM, PVMCPU pVCpu, uint32_t fFlags)
1754{
1755#ifdef NEM_WIN_USE_OUR_OWN_RUN_API
1756 nemHCWinCancelRunVirtualProcessor(pVM, pVCpu);
1757#else
1758# ifdef NEM_WIN_WITH_RING0_RUNLOOP
1759 if (pVM->nem.s.fUseRing0Runloop)
1760 nemHCWinCancelRunVirtualProcessor(pVM, pVCpu);
1761 else
1762# endif
1763 {
1764 Log8(("nemR3NativeNotifyFF: canceling %u\n", pVCpu->idCpu));
1765 HRESULT hrc = WHvCancelRunVirtualProcessor(pVM->nem.s.hPartition, pVCpu->idCpu, 0);
1766 AssertMsg(SUCCEEDED(hrc), ("WHvCancelRunVirtualProcessor -> hrc=%Rhrc\n", hrc));
1767 RT_NOREF_PV(hrc);
1768 }
1769#endif
1770 RT_NOREF_PV(fFlags);
1771}
1772
1773
1774DECLINLINE(int) nemR3NativeGCPhys2R3PtrReadOnly(PVM pVM, RTGCPHYS GCPhys, const void **ppv)
1775{
1776 PGMPAGEMAPLOCK Lock;
1777 int rc = PGMPhysGCPhys2CCPtrReadOnly(pVM, GCPhys, ppv, &Lock);
1778 if (RT_SUCCESS(rc))
1779 PGMPhysReleasePageMappingLock(pVM, &Lock);
1780 return rc;
1781}
1782
1783
1784DECLINLINE(int) nemR3NativeGCPhys2R3PtrWriteable(PVM pVM, RTGCPHYS GCPhys, void **ppv)
1785{
1786 PGMPAGEMAPLOCK Lock;
1787 int rc = PGMPhysGCPhys2CCPtr(pVM, GCPhys, ppv, &Lock);
1788 if (RT_SUCCESS(rc))
1789 PGMPhysReleasePageMappingLock(pVM, &Lock);
1790 return rc;
1791}
1792
1793
1794int nemR3NativeNotifyPhysRamRegister(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb)
1795{
1796 Log5(("nemR3NativeNotifyPhysRamRegister: %RGp LB %RGp\n", GCPhys, cb));
1797 NOREF(pVM); NOREF(GCPhys); NOREF(cb);
1798 return VINF_SUCCESS;
1799}
1800
1801
1802int nemR3NativeNotifyPhysMmioExMap(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags, void *pvMmio2)
1803{
1804 Log5(("nemR3NativeNotifyPhysMmioExMap: %RGp LB %RGp fFlags=%#x pvMmio2=%p\n", GCPhys, cb, fFlags, pvMmio2));
1805 NOREF(pVM); NOREF(GCPhys); NOREF(cb); NOREF(fFlags); NOREF(pvMmio2);
1806 return VINF_SUCCESS;
1807}
1808
1809
1810int nemR3NativeNotifyPhysMmioExUnmap(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags)
1811{
1812 Log5(("nemR3NativeNotifyPhysMmioExUnmap: %RGp LB %RGp fFlags=%#x\n", GCPhys, cb, fFlags));
1813 NOREF(pVM); NOREF(GCPhys); NOREF(cb); NOREF(fFlags);
1814 return VINF_SUCCESS;
1815}
1816
1817
1818/**
1819 * Called early during ROM registration, right after the pages have been
1820 * allocated and the RAM range updated.
1821 *
1822 * This will be succeeded by a number of NEMHCNotifyPhysPageProtChanged() calls
1823 * and finally a NEMR3NotifyPhysRomRegisterEarly().
1824 *
1825 * @returns VBox status code
1826 * @param pVM The cross context VM structure.
1827 * @param GCPhys The ROM address (page aligned).
1828 * @param cb The size (page aligned).
1829 * @param fFlags NEM_NOTIFY_PHYS_ROM_F_XXX.
1830 */
1831int nemR3NativeNotifyPhysRomRegisterEarly(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags)
1832{
1833 Log5(("nemR3NativeNotifyPhysRomRegisterEarly: %RGp LB %RGp fFlags=%#x\n", GCPhys, cb, fFlags));
1834#if 0 /* Let's not do this after all. We'll protection change notifications for each page and if not we'll map them lazily. */
1835 RTGCPHYS const cPages = cb >> X86_PAGE_SHIFT;
1836 for (RTGCPHYS iPage = 0; iPage < cPages; iPage++, GCPhys += X86_PAGE_SIZE)
1837 {
1838 const void *pvPage;
1839 int rc = nemR3NativeGCPhys2R3PtrReadOnly(pVM, GCPhys, &pvPage);
1840 if (RT_SUCCESS(rc))
1841 {
1842 HRESULT hrc = WHvMapGpaRange(pVM->nem.s.hPartition, (void *)pvPage, GCPhys, X86_PAGE_SIZE,
1843 WHvMapGpaRangeFlagRead | WHvMapGpaRangeFlagExecute);
1844 if (SUCCEEDED(hrc))
1845 { /* likely */ }
1846 else
1847 {
1848 LogRel(("nemR3NativeNotifyPhysRomRegisterEarly: GCPhys=%RGp hrc=%Rhrc (%#x) Last=%#x/%u\n",
1849 GCPhys, hrc, hrc, RTNtLastStatusValue(), RTNtLastErrorValue()));
1850 return VERR_NEM_INIT_FAILED;
1851 }
1852 }
1853 else
1854 {
1855 LogRel(("nemR3NativeNotifyPhysRomRegisterEarly: GCPhys=%RGp rc=%Rrc\n", GCPhys, rc));
1856 return rc;
1857 }
1858 }
1859#else
1860 NOREF(pVM); NOREF(GCPhys); NOREF(cb);
1861#endif
1862 RT_NOREF_PV(fFlags);
1863 return VINF_SUCCESS;
1864}
1865
1866
1867/**
1868 * Called after the ROM range has been fully completed.
1869 *
1870 * This will be preceeded by a NEMR3NotifyPhysRomRegisterEarly() call as well a
1871 * number of NEMHCNotifyPhysPageProtChanged calls.
1872 *
1873 * @returns VBox status code
1874 * @param pVM The cross context VM structure.
1875 * @param GCPhys The ROM address (page aligned).
1876 * @param cb The size (page aligned).
1877 * @param fFlags NEM_NOTIFY_PHYS_ROM_F_XXX.
1878 */
1879int nemR3NativeNotifyPhysRomRegisterLate(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags)
1880{
1881 Log5(("nemR3NativeNotifyPhysRomRegisterLate: %RGp LB %RGp fFlags=%#x\n", GCPhys, cb, fFlags));
1882 NOREF(pVM); NOREF(GCPhys); NOREF(cb); NOREF(fFlags);
1883 return VINF_SUCCESS;
1884}
1885
1886
1887/**
1888 * @callback_method_impl{FNPGMPHYSNEMCHECKPAGE}
1889 */
1890static DECLCALLBACK(int) nemR3WinUnsetForA20CheckerCallback(PVM pVM, PVMCPU pVCpu, RTGCPHYS GCPhys,
1891 PPGMPHYSNEMPAGEINFO pInfo, void *pvUser)
1892{
1893 /* We'll just unmap the memory. */
1894 if (pInfo->u2NemState > NEM_WIN_PAGE_STATE_UNMAPPED)
1895 {
1896#ifdef NEM_WIN_USE_HYPERCALLS_FOR_PAGES
1897 int rc = nemHCWinHypercallUnmapPage(pVM, pVCpu, GCPhys);
1898 AssertRC(rc);
1899 if (RT_SUCCESS(rc))
1900#else
1901 HRESULT hrc = WHvUnmapGpaRange(pVM->nem.s.hPartition, GCPhys, X86_PAGE_SIZE);
1902 if (SUCCEEDED(hrc))
1903#endif
1904 {
1905 uint32_t cMappedPages = ASMAtomicDecU32(&pVM->nem.s.cMappedPages); NOREF(cMappedPages);
1906 Log5(("NEM GPA unmapped/A20: %RGp (was %s, cMappedPages=%u)\n", GCPhys, g_apszPageStates[pInfo->u2NemState], cMappedPages));
1907 pInfo->u2NemState = NEM_WIN_PAGE_STATE_UNMAPPED;
1908 }
1909 else
1910 {
1911#ifdef NEM_WIN_USE_HYPERCALLS_FOR_PAGES
1912 LogRel(("nemR3WinUnsetForA20CheckerCallback/unmap: GCPhys=%RGp rc=%Rrc\n", GCPhys, rc));
1913 return rc;
1914#else
1915 LogRel(("nemR3WinUnsetForA20CheckerCallback/unmap: GCPhys=%RGp hrc=%Rhrc (%#x) Last=%#x/%u\n",
1916 GCPhys, hrc, hrc, RTNtLastStatusValue(), RTNtLastErrorValue()));
1917 return VERR_INTERNAL_ERROR_2;
1918#endif
1919 }
1920 }
1921 RT_NOREF(pVCpu, pvUser);
1922 return VINF_SUCCESS;
1923}
1924
1925
1926/**
1927 * Unmaps a page from Hyper-V for the purpose of emulating A20 gate behavior.
1928 *
1929 * @returns The PGMPhysNemQueryPageInfo result.
1930 * @param pVM The cross context VM structure.
1931 * @param pVCpu The cross context virtual CPU structure.
1932 * @param GCPhys The page to unmap.
1933 */
1934static int nemR3WinUnmapPageForA20Gate(PVM pVM, PVMCPU pVCpu, RTGCPHYS GCPhys)
1935{
1936 PGMPHYSNEMPAGEINFO Info;
1937 return PGMPhysNemPageInfoChecker(pVM, pVCpu, GCPhys, false /*fMakeWritable*/, &Info,
1938 nemR3WinUnsetForA20CheckerCallback, NULL);
1939}
1940
1941
1942/**
1943 * Called when the A20 state changes.
1944 *
1945 * Hyper-V doesn't seem to offer a simple way of implementing the A20 line
1946 * features of PCs. So, we do a very minimal emulation of the HMA to make DOS
1947 * happy.
1948 *
1949 * @param pVCpu The CPU the A20 state changed on.
1950 * @param fEnabled Whether it was enabled (true) or disabled.
1951 */
1952void nemR3NativeNotifySetA20(PVMCPU pVCpu, bool fEnabled)
1953{
1954 Log(("nemR3NativeNotifySetA20: fEnabled=%RTbool\n", fEnabled));
1955 PVM pVM = pVCpu->CTX_SUFF(pVM);
1956 if (!pVM->nem.s.fA20Fixed)
1957 {
1958 pVM->nem.s.fA20Enabled = fEnabled;
1959 for (RTGCPHYS GCPhys = _1M; GCPhys < _1M + _64K; GCPhys += X86_PAGE_SIZE)
1960 nemR3WinUnmapPageForA20Gate(pVM, pVCpu, GCPhys);
1961 }
1962}
1963
1964
1965/** @page pg_nem_win NEM/win - Native Execution Manager, Windows.
1966 *
1967 * On Windows the Hyper-V root partition (dom0 in zen terminology) does not have
1968 * nested VT-x or AMD-V capabilities. Early on raw-mode worked inside it, but
1969 * for a while now we've been getting \#GPs when trying to modify CR4 in the
1970 * world switcher. So, when Hyper-V is active on Windows we have little choice
1971 * but to use Hyper-V to run our VMs.
1972 *
1973 *
1974 * @section sub_nem_win_whv The WinHvPlatform API
1975 *
1976 * Since Windows 10 build 17083 there is a documented API for managing Hyper-V
1977 * VMs: header file WinHvPlatform.h and implementation in WinHvPlatform.dll.
1978 * This interface is a wrapper around the undocumented Virtualization
1979 * Infrastructure Driver (VID) API - VID.DLL and VID.SYS. The wrapper is
1980 * written in C++, namespaced, early versions (at least) was using standard C++
1981 * container templates in several places.
1982 *
1983 * When creating a VM using WHvCreatePartition, it will only create the
1984 * WinHvPlatform structures for it, to which you get an abstract pointer. The
1985 * VID API that actually creates the partition is first engaged when you call
1986 * WHvSetupPartition after first setting a lot of properties using
1987 * WHvSetPartitionProperty. Since the VID API is just a very thin wrapper
1988 * around CreateFile and NtDeviceIoControlFile, it returns an actual HANDLE for
1989 * the partition to WinHvPlatform. We fish this HANDLE out of the WinHvPlatform
1990 * partition structures because we need to talk directly to VID for reasons
1991 * we'll get to in a bit. (Btw. we could also intercept the CreateFileW or
1992 * NtDeviceIoControlFile calls from VID.DLL to get the HANDLE should fishing in
1993 * the partition structures become difficult.)
1994 *
1995 * The WinHvPlatform API requires us to both set the number of guest CPUs before
1996 * setting up the partition and call WHvCreateVirtualProcessor for each of them.
1997 * The CPU creation function boils down to a VidMessageSlotMap call that sets up
1998 * and maps a message buffer into ring-3 for async communication with hyper-V
1999 * and/or the VID.SYS thread actually running the CPU thru
2000 * WinHvRunVpDispatchLoop(). When for instance a VMEXIT is encountered, hyper-V
2001 * sends a message that the WHvRunVirtualProcessor API retrieves (and later
2002 * acknowledges) via VidMessageSlotHandleAndGetNext. Since or about build
2003 * 17757 a register page is also mapped into user space when creating the
2004 * virtual CPU. It should be noteded that WHvDeleteVirtualProcessor doesn't do
2005 * much as there seems to be no partner function VidMessagesSlotMap that
2006 * reverses what it did.
2007 *
2008 * Memory is managed thru calls to WHvMapGpaRange and WHvUnmapGpaRange (GPA does
2009 * not mean grade point average here, but rather guest physical addressspace),
2010 * which corresponds to VidCreateVaGpaRangeSpecifyUserVa and VidDestroyGpaRange
2011 * respectively. As 'UserVa' indicates, the functions works on user process
2012 * memory. The mappings are also subject to quota restrictions, so the number
2013 * of ranges are limited and probably their total size as well. Obviously
2014 * VID.SYS keeps track of the ranges, but so does WinHvPlatform, which means
2015 * there is a bit of overhead involved and quota restrctions makes sense.
2016 *
2017 * Running guest code is done through the WHvRunVirtualProcessor function. It
2018 * asynchronously starts or resumes hyper-V CPU execution and then waits for an
2019 * VMEXIT message. Hyper-V / VID.SYS will return information about the message
2020 * in the message buffer mapping, and WHvRunVirtualProcessor will convert that
2021 * finto it's own WHV_RUN_VP_EXIT_CONTEXT format.
2022 *
2023 * Other threads can interrupt the execution by using WHvCancelVirtualProcessor,
2024 * which since or about build 17757 uses VidMessageSlotHandleAndGetNext to do
2025 * the work (earlier builds would open the waiting thread, do a dummy
2026 * QueueUserAPC on it, and let it upon return use VidStopVirtualProcessor to
2027 * do the actual stopping). While there is certainly a race between cancelation
2028 * and the CPU causing a natural VMEXIT, it is not known whether this still
2029 * causes extra work on subsequent WHvRunVirtualProcessor calls (it did in and
2030 * earlier 17134).
2031 *
2032 * Registers are retrieved and set via WHvGetVirtualProcessorRegisters and
2033 * WHvSetVirtualProcessorRegisters. In addition, several VMEXITs include
2034 * essential register state in the exit context information, potentially making
2035 * it possible to emulate the instruction causing the exit without involving
2036 * WHvGetVirtualProcessorRegisters.
2037 *
2038 *
2039 * @subsection subsec_nem_win_whv_cons Issues & Feedback
2040 *
2041 * Here are some observations (mostly against build 17101):
2042 *
2043 * - The VMEXIT performance is dismal (build 17134).
2044 *
2045 * Our proof of concept implementation with a kernel runloop (i.e. not using
2046 * WHvRunVirtualProcessor and friends, but calling VID.SYS fast I/O control
2047 * entry point directly) delivers 9-10% of the port I/O performance and only
2048 * 6-7% of the MMIO performance that we have with our own hypervisor.
2049 *
2050 * When using the offical WinHvPlatform API, the numbers are %3 for port I/O
2051 * and 5% for MMIO.
2052 *
2053 * While the tests we've done are using tight tight loops only doing port I/O
2054 * and MMIO, the problem is clearly visible when running regular guest OSes.
2055 * Anything that hammers the VGA device would be suffering, for example:
2056 *
2057 * - Windows 2000 boot screen animation overloads us with MMIO exits
2058 * and won't even boot because all the time is spent in interrupt
2059 * handlers and redrawin the screen.
2060 *
2061 * - DSL 4.4 and its bootmenu logo is slower than molasses in january.
2062 *
2063 * We have not found a workaround for this yet.
2064 *
2065 * Something that might improve the issue a little is to detect blocks with
2066 * excessive MMIO and port I/O exits and emulate instructions to cover
2067 * multiple exits before letting Hyper-V have a go at the guest execution
2068 * again. This will only improve the situation under some circumstances,
2069 * since emulating instructions without recompilation can be expensive, so
2070 * there will only be real gains if the exitting instructions are tightly
2071 * packed.
2072 *
2073 * Update: Security fixes during the summer of 2018 caused the performance to
2074 * dropped even more.
2075 *
2076 * Update [build 17757]: Some performance improvements here, but they don't
2077 * yet make up for what was lost this summer.
2078 *
2079 *
2080 * - We need a way to directly modify the TSC offset (or bias if you like).
2081 *
2082 * The current approach of setting the WHvX64RegisterTsc register one by one
2083 * on each virtual CPU in sequence will introduce random inaccuracies,
2084 * especially if the thread doing the job is reschduled at a bad time.
2085 *
2086 *
2087 * - Unable to access WHvX64RegisterMsrMtrrCap (build 17134).
2088 *
2089 *
2090 * - On AMD Ryzen grub/debian 9.0 ends up with a unrecoverable exception
2091 * when IA32_MTRR_PHYSMASK0 is written.
2092 *
2093 *
2094 * - The IA32_APIC_BASE register does not work right:
2095 *
2096 * - Attempts by the guest to clear bit 11 (EN) are ignored, both the
2097 * guest and the VMM reads back the old value.
2098 *
2099 * - Attempts to modify the base address (bits NN:12) seems to be ignored
2100 * in the same way.
2101 *
2102 * - The VMM can modify both the base address as well as the the EN and
2103 * BSP bits, however this is useless if we cannot intercept the WRMSR.
2104 *
2105 * - Attempts by the guest to set the EXTD bit (X2APIC) result in \#GP(0),
2106 * while the VMM ends up with with ERROR_HV_INVALID_PARAMETER. Seems
2107 * there is no way to support X2APIC.
2108 *
2109 *
2110 * - Not sure if this is a thing, but WHvCancelVirtualProcessor seems to cause
2111 * cause a lot more spurious WHvRunVirtualProcessor returns that what we get
2112 * with the replacement code. By spurious returns we mean that the
2113 * subsequent call to WHvRunVirtualProcessor would return immediately.
2114 *
2115 * Update [build 17757]: New cancelation code might have addressed this, but
2116 * haven't had time to test it yet.
2117 *
2118 *
2119 * - There is no API for modifying protection of a page within a GPA range.
2120 *
2121 * From what we can tell, the only way to modify the protection (like readonly
2122 * -> writable, or vice versa) is to first unmap the range and then remap it
2123 * with the new protection.
2124 *
2125 * We are for instance doing this quite a bit in order to track dirty VRAM
2126 * pages. VRAM pages starts out as readonly, when the guest writes to a page
2127 * we take an exit, notes down which page it is, makes it writable and restart
2128 * the instruction. After refreshing the display, we reset all the writable
2129 * pages to readonly again, bulk fashion.
2130 *
2131 * Now to work around this issue, we do page sized GPA ranges. In addition to
2132 * add a lot of tracking overhead to WinHvPlatform and VID.SYS, this also
2133 * causes us to exceed our quota before we've even mapped a default sized
2134 * (128MB) VRAM page-by-page. So, to work around this quota issue we have to
2135 * lazily map pages and actively restrict the number of mappings.
2136 *
2137 * Our best workaround thus far is bypassing WinHvPlatform and VID entirely
2138 * when in comes to guest memory management and instead use the underlying
2139 * hypercalls (HvCallMapGpaPages, HvCallUnmapGpaPages) to do it ourselves.
2140 * (This also maps a whole lot better into our own guest page management
2141 * infrastructure.)
2142 *
2143 * Update [build 17757]: Introduces a KVM like dirty logging API which could
2144 * help tracking dirty VGA pages, while being useless for shadow ROM and
2145 * devices trying catch the guest updating descriptors and such.
2146 *
2147 *
2148 * - Observed problems doing WHvUnmapGpaRange immediately followed by
2149 * WHvMapGpaRange.
2150 *
2151 * As mentioned above, we've been forced to use this sequence when modifying
2152 * page protection. However, when transitioning from readonly to writable,
2153 * we've ended up looping forever with the same write to readonly memory
2154 * VMEXIT. We're wondering if this issue might be related to the lazy mapping
2155 * logic in WinHvPlatform.
2156 *
2157 * Workaround: Insert a WHvRunVirtualProcessor call and make sure to get a GPA
2158 * unmapped exit between the two calls. Not entirely great performance wise
2159 * (or the santity of our code).
2160 *
2161 *
2162 * - Implementing A20 gate behavior is tedious, where as correctly emulating the
2163 * A20M# pin (present on 486 and later) is near impossible for SMP setups
2164 * (e.g. possiblity of two CPUs with different A20 status).
2165 *
2166 * Workaround: Only do A20 on CPU 0, restricting the emulation to HMA. We
2167 * unmap all pages related to HMA (0x100000..0x10ffff) when the A20 state
2168 * changes, lazily syncing the right pages back when accessed.
2169 *
2170 *
2171 * - WHVRunVirtualProcessor wastes time converting VID/Hyper-V messages to its
2172 * own format (WHV_RUN_VP_EXIT_CONTEXT).
2173 *
2174 * We understand this might be because Microsoft wishes to remain free to
2175 * modify the VID/Hyper-V messages, but it's still rather silly and does slow
2176 * things down a little. We'd much rather just process the messages directly.
2177 *
2178 *
2179 * - WHVRunVirtualProcessor would've benefited from using a callback interface:
2180 *
2181 * - The potential size changes of the exit context structure wouldn't be
2182 * an issue, since the function could manage that itself.
2183 *
2184 * - State handling could probably be simplified (like cancelation).
2185 *
2186 *
2187 * - WHvGetVirtualProcessorRegisters and WHvSetVirtualProcessorRegisters
2188 * internally converts register names, probably using temporary heap buffers.
2189 *
2190 * From the looks of things, they are converting from WHV_REGISTER_NAME to
2191 * HV_REGISTER_NAME from in the "Virtual Processor Register Names" section in
2192 * the "Hypervisor Top-Level Functional Specification" document. This feels
2193 * like an awful waste of time.
2194 *
2195 * We simply cannot understand why HV_REGISTER_NAME isn't used directly here,
2196 * or at least the same values, making any conversion reduntant. Restricting
2197 * access to certain registers could easily be implement by scanning the
2198 * inputs.
2199 *
2200 * To avoid the heap + conversion overhead, we're currently using the
2201 * HvCallGetVpRegisters and HvCallSetVpRegisters calls directly, at least for
2202 * the ring-0 code.
2203 *
2204 * Update [build 17757]: Register translation has been very cleverly
2205 * optimized and made table driven (2 top level tables, 4 + 1 leaf tables).
2206 * Register information consists of the 32-bit HV register name, register page
2207 * offset, and flags (giving valid offset, size and more). Register
2208 * getting/settings seems to be done by hoping that the register page provides
2209 * it all, and falling back on the VidSetVirtualProcessorState if one or more
2210 * registers are not available there.
2211 *
2212 * Note! We have currently not updated our ring-0 code to take the register
2213 * page into account, so it's suffering a little compared to the ring-3 code
2214 * that now uses the offical APIs for registers.
2215 *
2216 *
2217 * - The YMM and XCR0 registers are not yet named (17083). This probably
2218 * wouldn't be a problem if HV_REGISTER_NAME was used, see previous point.
2219 *
2220 * Update [build 17757]: XCR0 is added. YMM register values seems to be put
2221 * into a yet undocumented XsaveState interface. Approach is a little bulky,
2222 * but saves number of enums and dispenses with register transation. Also,
2223 * the underlying Vid setter API duplicates the input buffer on the heap,
2224 * adding a 16 byte header.
2225 *
2226 *
2227 * - Why does VID.SYS only query/set 32 registers at the time thru the
2228 * HvCallGetVpRegisters and HvCallSetVpRegisters hypercalls?
2229 *
2230 * We've not trouble getting/setting all the registers defined by
2231 * WHV_REGISTER_NAME in one hypercall (around 80). Some kind of stack
2232 * buffering or similar?
2233 *
2234 *
2235 * - To handle the VMMCALL / VMCALL instructions, it seems we need to intercept
2236 * \#UD exceptions and inspect the opcodes. A dedicated exit for hypercalls
2237 * would be more efficient, esp. for guests using \#UD for other purposes..
2238 *
2239 *
2240 * - Wrong instruction length in the VpContext with unmapped GPA memory exit
2241 * contexts on 17115/AMD.
2242 *
2243 * One byte "PUSH CS" was reported as 2 bytes, while a two byte
2244 * "MOV [EBX],EAX" was reported with a 1 byte instruction length. Problem
2245 * naturally present in untranslated hyper-v messages.
2246 *
2247 *
2248 * - The I/O port exit context information seems to be missing the address size
2249 * information needed for correct string I/O emulation.
2250 *
2251 * VT-x provides this information in bits 7:9 in the instruction information
2252 * field on newer CPUs. AMD-V in bits 7:9 in the EXITINFO1 field in the VMCB.
2253 *
2254 * We can probably work around this by scanning the instruction bytes for
2255 * address size prefixes. Haven't investigated it any further yet.
2256 *
2257 *
2258 * - Querying WHvCapabilityCodeExceptionExitBitmap returns zero even when
2259 * intercepts demonstrably works (17134).
2260 *
2261 *
2262 * - Querying HvPartitionPropertyDebugChannelId via HvCallGetPartitionProperty
2263 * (hypercall) hangs the host (17134).
2264 *
2265 *
2266 *
2267 * Old concerns that have been addressed:
2268 *
2269 * - The WHvCancelVirtualProcessor API schedules a dummy usermode APC callback
2270 * in order to cancel any current or future alertable wait in VID.SYS during
2271 * the VidMessageSlotHandleAndGetNext call.
2272 *
2273 * IIRC this will make the kernel schedule the specified callback thru
2274 * NTDLL!KiUserApcDispatcher by modifying the thread context and quite
2275 * possibly the userland thread stack. When the APC callback returns to
2276 * KiUserApcDispatcher, it will call NtContinue to restore the old thread
2277 * context and resume execution from there. This naturally adds up to some
2278 * CPU cycles, ring transitions aren't for free, especially after Spectre &
2279 * Meltdown mitigations.
2280 *
2281 * Using NtAltertThread call could do the same without the thread context
2282 * modifications and the extra kernel call.
2283 *
2284 * Update: All concerns have addressed in or about build 17757.
2285 *
2286 * The WHvCancelVirtualProcessor API is now implemented using a new
2287 * VidMessageSlotHandleAndGetNext() flag (4). Codepath is slightly longer
2288 * than NtAlertThread, but has the added benefit that spurious wakeups can be
2289 * more easily reduced.
2290 *
2291 *
2292 * - When WHvRunVirtualProcessor returns without a message, or on a terse
2293 * VID message like HLT, it will make a kernel call to get some registers.
2294 * This is potentially inefficient if the caller decides he needs more
2295 * register state.
2296 *
2297 * It would be better to just return what's available and let the caller fetch
2298 * what is missing from his point of view in a single kernel call.
2299 *
2300 * Update: All concerns have been addressed in or about build 17757. Selected
2301 * registers are now available via shared memory and thus HLT should (not
2302 * verified) no longer require a system call to compose the exit context data.
2303 *
2304 *
2305 * - The WHvRunVirtualProcessor implementation does lazy GPA range mappings when
2306 * a unmapped GPA message is received from hyper-V.
2307 *
2308 * Since MMIO is currently realized as unmapped GPA, this will slow down all
2309 * MMIO accesses a tiny little bit as WHvRunVirtualProcessor looks up the
2310 * guest physical address to check if it is a pending lazy mapping.
2311 *
2312 * The lazy mapping feature makes no sense to us. We as API user have all the
2313 * information and can do lazy mapping ourselves if we want/have to (see next
2314 * point).
2315 *
2316 * Update: All concerns have been addressed in or about build 17757.
2317 *
2318 *
2319 * - The WHvGetCapability function has a weird design:
2320 * - The CapabilityCode parameter is pointlessly duplicated in the output
2321 * structure (WHV_CAPABILITY).
2322 *
2323 * - API takes void pointer, but everyone will probably be using
2324 * WHV_CAPABILITY due to WHV_CAPABILITY::CapabilityCode making it
2325 * impractical to use anything else.
2326 *
2327 * - No output size.
2328 *
2329 * - See GetFileAttributesEx, GetFileInformationByHandleEx,
2330 * FindFirstFileEx, and others for typical pattern for generic
2331 * information getters.
2332 *
2333 * Update: All concerns have been addressed in build 17110.
2334 *
2335 *
2336 * - The WHvGetPartitionProperty function uses the same weird design as
2337 * WHvGetCapability, see above.
2338 *
2339 * Update: All concerns have been addressed in build 17110.
2340 *
2341 *
2342 * - The WHvSetPartitionProperty function has a totally weird design too:
2343 * - In contrast to its partner WHvGetPartitionProperty, the property code
2344 * is not a separate input parameter here but part of the input
2345 * structure.
2346 *
2347 * - The input structure is a void pointer rather than a pointer to
2348 * WHV_PARTITION_PROPERTY which everyone probably will be using because
2349 * of the WHV_PARTITION_PROPERTY::PropertyCode field.
2350 *
2351 * - Really, why use PVOID for the input when the function isn't accepting
2352 * minimal sizes. E.g. WHVPartitionPropertyCodeProcessorClFlushSize only
2353 * requires a 9 byte input, but the function insists on 16 bytes (17083).
2354 *
2355 * - See GetFileAttributesEx, SetFileInformationByHandle, FindFirstFileEx,
2356 * and others for typical pattern for generic information setters and
2357 * getters.
2358 *
2359 * Update: All concerns have been addressed in build 17110.
2360 *
2361 *
2362 *
2363 * @section sec_nem_win_impl Our implementation.
2364 *
2365 * We set out with the goal of wanting to run as much as possible in ring-0,
2366 * reasoning that this would give use the best performance.
2367 *
2368 * This goal was approached gradually, starting out with a pure WinHvPlatform
2369 * implementation, gradually replacing parts: register access, guest memory
2370 * handling, running virtual processors. Then finally moving it all into
2371 * ring-0, while keeping most of it configurable so that we could make
2372 * comparisons (see NEMInternal.h and nemR3NativeRunGC()).
2373 *
2374 *
2375 * @subsection subsect_nem_win_impl_ioctl VID.SYS I/O control calls
2376 *
2377 * To run things in ring-0 we need to talk directly to VID.SYS thru its I/O
2378 * control interface. Looking at changes between like build 17083 and 17101 (if
2379 * memory serves) a set of the VID I/O control numbers shifted a little, which
2380 * means we need to determin them dynamically. We currently do this by hooking
2381 * the NtDeviceIoControlFile API call from VID.DLL and snooping up the
2382 * parameters when making dummy calls to relevant APIs. (We could also
2383 * disassemble the relevant APIs and try fish out the information from that, but
2384 * this is way simpler.)
2385 *
2386 * Issuing I/O control calls from ring-0 is facing a small challenge with
2387 * respect to direct buffering. When using direct buffering the device will
2388 * typically check that the buffer is actually in the user address space range
2389 * and reject kernel addresses. Fortunately, we've got the cross context VM
2390 * structure that is mapped into both kernel and user space, it's also locked
2391 * and safe to access from kernel space. So, we place the I/O control buffers
2392 * in the per-CPU part of it (NEMCPU::uIoCtlBuf) and give the driver the user
2393 * address if direct access buffering or kernel address if not.
2394 *
2395 * The I/O control calls are 'abstracted' in the support driver, see
2396 * SUPR0IoCtlSetupForHandle(), SUPR0IoCtlPerform() and SUPR0IoCtlCleanup().
2397 *
2398 *
2399 * @subsection subsect_nem_win_impl_cpumctx CPUMCTX
2400 *
2401 * Since the CPU state needs to live in Hyper-V when executing, we probably
2402 * should not transfer more than necessary when handling VMEXITs. To help us
2403 * manage this CPUMCTX got a new field CPUMCTX::fExtrn that to indicate which
2404 * part of the state is currently externalized (== in Hyper-V).
2405 *
2406 *
2407 * @subsection sec_nem_win_benchmarks Benchmarks.
2408 *
2409 * @subsubsection subsect_nem_win_benchmarks_bs2t1 17134/2018-06-22: Bootsector2-test1
2410 *
2411 * This is ValidationKit/bootsectors/bootsector2-test1.asm as of 2018-06-22
2412 * (internal r123172) running a the release build of VirtualBox from the same
2413 * source, though with exit optimizations disabled. Host is AMD Threadripper 1950X
2414 * running out an up to date 64-bit Windows 10 build 17134.
2415 *
2416 * The base line column is using the official WinHv API for everything but physical
2417 * memory mapping. The 2nd column is the default NEM/win configuration where we
2418 * put the main execution loop in ring-0, using hypercalls when we can and VID for
2419 * managing execution. The 3rd column is regular VirtualBox using AMD-V directly,
2420 * hyper-V is disabled, main execution loop in ring-0.
2421 *
2422 * @verbatim
2423TESTING... WinHv API Hypercalls + VID VirtualBox AMD-V
2424 32-bit paged protected mode, CPUID : 108 874 ins/sec 113% / 123 602 1198% / 1 305 113
2425 32-bit pae protected mode, CPUID : 106 722 ins/sec 115% / 122 740 1232% / 1 315 201
2426 64-bit long mode, CPUID : 106 798 ins/sec 114% / 122 111 1198% / 1 280 404
2427 16-bit unpaged protected mode, CPUID : 106 835 ins/sec 114% / 121 994 1216% / 1 299 665
2428 32-bit unpaged protected mode, CPUID : 105 257 ins/sec 115% / 121 772 1235% / 1 300 860
2429 real mode, CPUID : 104 507 ins/sec 116% / 121 800 1228% / 1 283 848
2430CPUID EAX=1 : PASSED
2431 32-bit paged protected mode, RDTSC : 99 581 834 ins/sec 100% / 100 323 307 93% / 93 473 299
2432 32-bit pae protected mode, RDTSC : 99 620 585 ins/sec 100% / 99 960 952 84% / 83 968 839
2433 64-bit long mode, RDTSC : 100 540 009 ins/sec 100% / 100 946 372 93% / 93 652 826
2434 16-bit unpaged protected mode, RDTSC : 99 688 473 ins/sec 100% / 100 097 751 76% / 76 281 287
2435 32-bit unpaged protected mode, RDTSC : 98 385 857 ins/sec 102% / 100 510 404 94% / 93 379 536
2436 real mode, RDTSC : 100 087 967 ins/sec 101% / 101 386 138 93% / 93 234 999
2437RDTSC : PASSED
2438 32-bit paged protected mode, Read CR4 : 2 156 102 ins/sec 98% / 2 121 967 17114% / 369 009 009
2439 32-bit pae protected mode, Read CR4 : 2 163 820 ins/sec 98% / 2 133 804 17469% / 377 999 261
2440 64-bit long mode, Read CR4 : 2 164 822 ins/sec 98% / 2 128 698 18875% / 408 619 313
2441 16-bit unpaged protected mode, Read CR4 : 2 162 367 ins/sec 100% / 2 168 508 17132% / 370 477 568
2442 32-bit unpaged protected mode, Read CR4 : 2 163 189 ins/sec 100% / 2 169 808 16768% / 362 734 679
2443 real mode, Read CR4 : 2 162 436 ins/sec 100% / 2 164 914 15551% / 336 288 998
2444Read CR4 : PASSED
2445 real mode, 32-bit IN : 104 649 ins/sec 118% / 123 513 1028% / 1 075 831
2446 real mode, 32-bit OUT : 107 102 ins/sec 115% / 123 660 982% / 1 052 259
2447 real mode, 32-bit IN-to-ring-3 : 105 697 ins/sec 98% / 104 471 201% / 213 216
2448 real mode, 32-bit OUT-to-ring-3 : 105 830 ins/sec 98% / 104 598 198% / 210 495
2449 16-bit unpaged protected mode, 32-bit IN : 104 855 ins/sec 117% / 123 174 1029% / 1 079 591
2450 16-bit unpaged protected mode, 32-bit OUT : 107 529 ins/sec 115% / 124 250 992% / 1 067 053
2451 16-bit unpaged protected mode, 32-bit IN-to-ring-3 : 106 337 ins/sec 103% / 109 565 196% / 209 367
2452 16-bit unpaged protected mode, 32-bit OUT-to-ring-3 : 107 558 ins/sec 100% / 108 237 191% / 206 387
2453 32-bit unpaged protected mode, 32-bit IN : 106 351 ins/sec 116% / 123 584 1016% / 1 081 325
2454 32-bit unpaged protected mode, 32-bit OUT : 106 424 ins/sec 116% / 124 252 995% / 1 059 408
2455 32-bit unpaged protected mode, 32-bit IN-to-ring-3 : 104 035 ins/sec 101% / 105 305 202% / 210 750
2456 32-bit unpaged protected mode, 32-bit OUT-to-ring-3 : 103 831 ins/sec 102% / 106 919 205% / 213 198
2457 32-bit paged protected mode, 32-bit IN : 103 356 ins/sec 119% / 123 870 1041% / 1 076 463
2458 32-bit paged protected mode, 32-bit OUT : 107 177 ins/sec 115% / 124 302 998% / 1 069 655
2459 32-bit paged protected mode, 32-bit IN-to-ring-3 : 104 491 ins/sec 100% / 104 744 200% / 209 264
2460 32-bit paged protected mode, 32-bit OUT-to-ring-3 : 106 603 ins/sec 97% / 103 849 197% / 210 219
2461 32-bit pae protected mode, 32-bit IN : 105 923 ins/sec 115% / 122 759 1041% / 1 103 261
2462 32-bit pae protected mode, 32-bit OUT : 107 083 ins/sec 117% / 126 057 1024% / 1 096 667
2463 32-bit pae protected mode, 32-bit IN-to-ring-3 : 106 114 ins/sec 97% / 103 496 199% / 211 312
2464 32-bit pae protected mode, 32-bit OUT-to-ring-3 : 105 675 ins/sec 96% / 102 096 198% / 209 890
2465 64-bit long mode, 32-bit IN : 105 800 ins/sec 113% / 120 006 1013% / 1 072 116
2466 64-bit long mode, 32-bit OUT : 105 635 ins/sec 113% / 120 375 997% / 1 053 655
2467 64-bit long mode, 32-bit IN-to-ring-3 : 105 274 ins/sec 95% / 100 763 197% / 208 026
2468 64-bit long mode, 32-bit OUT-to-ring-3 : 106 262 ins/sec 94% / 100 749 196% / 209 288
2469NOP I/O Port Access : PASSED
2470 32-bit paged protected mode, 32-bit read : 57 687 ins/sec 119% / 69 136 1197% / 690 548
2471 32-bit paged protected mode, 32-bit write : 57 957 ins/sec 118% / 68 935 1183% / 685 930
2472 32-bit paged protected mode, 32-bit read-to-ring-3 : 57 958 ins/sec 95% / 55 432 276% / 160 505
2473 32-bit paged protected mode, 32-bit write-to-ring-3 : 57 922 ins/sec 100% / 58 340 304% / 176 464
2474 32-bit pae protected mode, 32-bit read : 57 478 ins/sec 119% / 68 453 1141% / 656 159
2475 32-bit pae protected mode, 32-bit write : 57 226 ins/sec 118% / 68 097 1157% / 662 504
2476 32-bit pae protected mode, 32-bit read-to-ring-3 : 57 582 ins/sec 94% / 54 651 268% / 154 867
2477 32-bit pae protected mode, 32-bit write-to-ring-3 : 57 697 ins/sec 100% / 57 750 299% / 173 030
2478 64-bit long mode, 32-bit read : 57 128 ins/sec 118% / 67 779 1071% / 611 949
2479 64-bit long mode, 32-bit write : 57 127 ins/sec 118% / 67 632 1084% / 619 395
2480 64-bit long mode, 32-bit read-to-ring-3 : 57 181 ins/sec 94% / 54 123 265% / 151 937
2481 64-bit long mode, 32-bit write-to-ring-3 : 57 297 ins/sec 99% / 57 286 294% / 168 694
2482 16-bit unpaged protected mode, 32-bit read : 58 827 ins/sec 118% / 69 545 1185% / 697 602
2483 16-bit unpaged protected mode, 32-bit write : 58 678 ins/sec 118% / 69 442 1183% / 694 387
2484 16-bit unpaged protected mode, 32-bit read-to-ring-3 : 57 841 ins/sec 96% / 55 730 275% / 159 163
2485 16-bit unpaged protected mode, 32-bit write-to-ring-3 : 57 855 ins/sec 101% / 58 834 304% / 176 169
2486 32-bit unpaged protected mode, 32-bit read : 58 063 ins/sec 120% / 69 690 1233% / 716 444
2487 32-bit unpaged protected mode, 32-bit write : 57 936 ins/sec 120% / 69 633 1199% / 694 753
2488 32-bit unpaged protected mode, 32-bit read-to-ring-3 : 58 451 ins/sec 96% / 56 183 273% / 159 972
2489 32-bit unpaged protected mode, 32-bit write-to-ring-3 : 58 962 ins/sec 99% / 58 955 298% / 175 936
2490 real mode, 32-bit read : 58 571 ins/sec 118% / 69 478 1160% / 679 917
2491 real mode, 32-bit write : 58 418 ins/sec 118% / 69 320 1185% / 692 513
2492 real mode, 32-bit read-to-ring-3 : 58 072 ins/sec 96% / 55 751 274% / 159 145
2493 real mode, 32-bit write-to-ring-3 : 57 870 ins/sec 101% / 58 755 307% / 178 042
2494NOP MMIO Access : PASSED
2495SUCCESS
2496 * @endverbatim
2497 *
2498 * What we see here is:
2499 *
2500 * - The WinHv API approach is 10 to 12 times slower for exits we can
2501 * handle directly in ring-0 in the VBox AMD-V code.
2502 *
2503 * - The WinHv API approach is 2 to 3 times slower for exits we have to
2504 * go to ring-3 to handle with the VBox AMD-V code.
2505 *
2506 * - By using hypercalls and VID.SYS from ring-0 we gain between
2507 * 13% and 20% over the WinHv API on exits handled in ring-0.
2508 *
2509 * - For exits requiring ring-3 handling are between 6% slower and 3% faster
2510 * than the WinHv API.
2511 *
2512 *
2513 * As a side note, it looks like Hyper-V doesn't let the guest read CR4 but
2514 * triggers exits all the time. This isn't all that important these days since
2515 * OSes like Linux cache the CR4 value specifically to avoid these kinds of exits.
2516 *
2517 *
2518 * @subsubsection subsect_nem_win_benchmarks_bs2t1u1 17134/2018-10-02: Bootsector2-test1
2519 *
2520 * Update on 17134. While expectantly testing a couple of newer builds (17758,
2521 * 17763) hoping for some increases in performance, the numbers turned out
2522 * altogether worse than the June test run. So, we went back to the 1803
2523 * (17134) installation, made sure it was fully up to date (as per 2018-10-02)
2524 * and re-tested.
2525 *
2526 * The numbers had somehow turned significantly worse over the last 3-4 months,
2527 * dropping around 70% for the WinHv API test, more for Hypercalls + VID.
2528 *
2529 * @verbatim
2530TESTING... WinHv API Hypercalls + VID VirtualBox AMD-V *
2531 32-bit paged protected mode, CPUID : 33 270 ins/sec 33 154
2532 real mode, CPUID : 33 534 ins/sec 32 711
2533 [snip]
2534 32-bit paged protected mode, RDTSC : 102 216 011 ins/sec 98 225 419
2535 real mode, RDTSC : 102 492 243 ins/sec 98 225 419
2536 [snip]
2537 32-bit paged protected mode, Read CR4 : 2 096 165 ins/sec 2 123 815
2538 real mode, Read CR4 : 2 081 047 ins/sec 2 075 151
2539 [snip]
2540 32-bit paged protected mode, 32-bit IN : 32 739 ins/sec 33 655
2541 32-bit paged protected mode, 32-bit OUT : 32 702 ins/sec 33 777
2542 32-bit paged protected mode, 32-bit IN-to-ring-3 : 32 579 ins/sec 29 985
2543 32-bit paged protected mode, 32-bit OUT-to-ring-3 : 32 750 ins/sec 29 757
2544 [snip]
2545 32-bit paged protected mode, 32-bit read : 20 042 ins/sec 21 489
2546 32-bit paged protected mode, 32-bit write : 20 036 ins/sec 21 493
2547 32-bit paged protected mode, 32-bit read-to-ring-3 : 19 985 ins/sec 19 143
2548 32-bit paged protected mode, 32-bit write-to-ring-3 : 19 972 ins/sec 19 595
2549
2550 * @endverbatim
2551 *
2552 * Suspects are security updates and/or microcode updates installed since then.
2553 * Given that the RDTSC and CR4 numbers are reasonably unchanges, it seems that
2554 * the Hyper-V core loop (in hvax64.exe) aren't affected. Our ring-0 runloop
2555 * is equally affected as the ring-3 based runloop, so it cannot be ring
2556 * switching as such (unless the ring-0 loop is borked and we didn't notice yet).
2557 *
2558 * The issue is probably in the thread / process switching area, could be
2559 * something special for hyper-V interrupt delivery or worker thread switching.
2560 *
2561 * Really wish this thread ping-pong going on in VID.SYS could be eliminated!
2562 *
2563 *
2564 * @subsubsection subsect_nem_win_benchmarks_bs2t1u2 17763: Bootsector2-test1
2565 *
2566 * Some preliminary numbers for build 17763 on the 3.4 GHz AMD 1950X, the second
2567 * column will improve we get time to have a look the register page.
2568 *
2569 * There is a 50% performance loss here compared to the June numbers with
2570 * build 17134. The RDTSC numbers hits that it isn't in the Hyper-V core
2571 * (hvax64.exe), but something on the NT side.
2572 *
2573 * Clearing bit 20 in nt!KiSpeculationFeatures speeds things up (i.e. changing
2574 * the dword from 0x00300065 to 0x00200065 in windbg). This is checked by
2575 * nt!KePrepareToDispatchVirtualProcessor, making it a no-op if the flag is
2576 * clear. winhvr!WinHvpVpDispatchLoop call that function before making
2577 * hypercall 0xc2, which presumably does the heavy VCpu lifting in hvcax64.exe.
2578 *
2579 * @verbatim
2580TESTING... WinHv API Hypercalls + VID clr(bit-20) + WinHv API
2581 32-bit paged protected mode, CPUID : 54 145 ins/sec 51 436 130 076
2582 real mode, CPUID : 54 178 ins/sec 51 713 130 449
2583 [snip]
2584 32-bit paged protected mode, RDTSC : 98 927 639 ins/sec 100 254 552 100 549 882
2585 real mode, RDTSC : 99 601 206 ins/sec 100 886 699 100 470 957
2586 [snip]
2587 32-bit paged protected mode, 32-bit IN : 54 621 ins/sec 51 524 128 294
2588 32-bit paged protected mode, 32-bit OUT : 54 870 ins/sec 51 671 129 397
2589 32-bit paged protected mode, 32-bit IN-to-ring-3 : 54 624 ins/sec 43 964 127 874
2590 32-bit paged protected mode, 32-bit OUT-to-ring-3 : 54 803 ins/sec 44 087 129 443
2591 [snip]
2592 32-bit paged protected mode, 32-bit read : 28 230 ins/sec 34 042 48 113
2593 32-bit paged protected mode, 32-bit write : 27 962 ins/sec 34 050 48 069
2594 32-bit paged protected mode, 32-bit read-to-ring-3 : 27 841 ins/sec 28 397 48 146
2595 32-bit paged protected mode, 32-bit write-to-ring-3 : 27 896 ins/sec 29 455 47 970
2596 * @endverbatim
2597 *
2598 *
2599 * @subsubsection subsect_nem_win_benchmarks_w2k 17134/2018-06-22: Windows 2000 Boot & Shutdown
2600 *
2601 * Timing the startup and automatic shutdown of a Windows 2000 SP4 guest serves
2602 * as a real world benchmark and example of why exit performance is import. When
2603 * Windows 2000 boots up is doing a lot of VGA redrawing of the boot animation,
2604 * which is very costly. Not having installed guest additions leaves it in a VGA
2605 * mode after the bootup sequence is done, keep up the screen access expenses,
2606 * though the graphics driver more economical than the bootvid code.
2607 *
2608 * The VM was configured to automatically logon. A startup script was installed
2609 * to perform the automatic shuting down and powering off the VM (thru
2610 * vts_shutdown.exe -f -p). An offline snapshot of the VM was taken an restored
2611 * before each test run. The test time run time is calculated from the monotonic
2612 * VBox.log timestamps, starting with the state change to 'RUNNING' and stopping
2613 * at 'POWERING_OFF'.
2614 *
2615 * The host OS and VirtualBox build is the same as for the bootsector2-test1
2616 * scenario.
2617 *
2618 * Results:
2619 *
2620 * - WinHv API for all but physical page mappings:
2621 * 32 min 12.19 seconds
2622 *
2623 * - The default NEM/win configuration where we put the main execution loop
2624 * in ring-0, using hypercalls when we can and VID for managing execution:
2625 * 3 min 23.18 seconds
2626 *
2627 * - Regular VirtualBox using AMD-V directly, hyper-V is disabled, main
2628 * execution loop in ring-0:
2629 * 58.09 seconds
2630 *
2631 * - WinHv API with exit history based optimizations:
2632 * 58.66 seconds
2633 *
2634 * - Hypercall + VID.SYS with exit history base optimizations:
2635 * 58.94 seconds
2636 *
2637 * With a well above average machine needing over half an hour for booting a
2638 * nearly 20 year old guest kind of says it all. The 13%-20% exit performance
2639 * increase we get by using hypercalls and VID.SYS directly pays off a lot here.
2640 * The 3m23s is almost acceptable in comparison to the half an hour.
2641 *
2642 * The similarity between the last three results strongly hits at windows 2000
2643 * doing a lot of waiting during boot and shutdown and isn't the best testcase
2644 * once a basic performance level is reached.
2645 *
2646 *
2647 * @subsubsection subsection_iem_win_benchmarks_deb9_nat Debian 9 NAT performance
2648 *
2649 * This benchmark is about network performance over NAT from a 64-bit Debian 9
2650 * VM with a single CPU. For network performance measurements, we use our own
2651 * NetPerf tool (ValidationKit/utils/network/NetPerf.cpp) to measure latency
2652 * and throughput.
2653 *
2654 * The setups, builds and configurations are as in the previous benchmarks
2655 * (release r123172 on 1950X running 64-bit W10/17134 (2016-06-xx). Please note
2656 * that the exit optimizations hasn't yet been in tuned with NetPerf in mind.
2657 *
2658 * The NAT network setup was selected here since it's the default one and the
2659 * slowest one. There is quite a bit of IPC with worker threads and packet
2660 * processing involved.
2661 *
2662 * Latency test is first up. This is a classic back and forth between the two
2663 * NetPerf instances, where the key measurement is the roundrip latency. The
2664 * values here are the lowest result over 3-6 runs.
2665 *
2666 * Against host system:
2667 * - 152 258 ns/roundtrip - 100% - regular VirtualBox SVM
2668 * - 271 059 ns/roundtrip - 178% - Hypercalls + VID.SYS in ring-0 with exit optimizations.
2669 * - 280 149 ns/roundtrip - 184% - Hypercalls + VID.SYS in ring-0
2670 * - 317 735 ns/roundtrip - 209% - Win HV API with exit optimizations.
2671 * - 342 440 ns/roundtrip - 225% - Win HV API
2672 *
2673 * Against a remote Windows 10 system over a 10Gbps link:
2674 * - 243 969 ns/roundtrip - 100% - regular VirtualBox SVM
2675 * - 384 427 ns/roundtrip - 158% - Win HV API with exit optimizations.
2676 * - 402 411 ns/roundtrip - 165% - Hypercalls + VID.SYS in ring-0
2677 * - 406 313 ns/roundtrip - 167% - Win HV API
2678 * - 413 160 ns/roundtrip - 169% - Hypercalls + VID.SYS in ring-0 with exit optimizations.
2679 *
2680 * What we see here is:
2681 *
2682 * - Consistent and signficant latency increase using Hyper-V compared
2683 * to directly harnessing AMD-V ourselves.
2684 *
2685 * - When talking to the host, it's clear that the hypercalls + VID.SYS
2686 * in ring-0 method pays off.
2687 *
2688 * - When talking to a different host, the numbers are closer and it
2689 * is not longer clear which Hyper-V execution method is better.
2690 *
2691 *
2692 * Throughput benchmarks are performed by one side pushing data full throttle
2693 * for 10 seconds (minus a 1 second at each end of the test), then reversing
2694 * the roles and measuring it in the other direction. The tests ran 3-5 times
2695 * and below are the highest and lowest results in each direction.
2696 *
2697 * Receiving from host system:
2698 * - Regular VirtualBox SVM:
2699 * Max: 96 907 549 bytes/s - 100%
2700 * Min: 86 912 095 bytes/s - 100%
2701 * - Hypercalls + VID.SYS in ring-0:
2702 * Max: 84 036 544 bytes/s - 87%
2703 * Min: 64 978 112 bytes/s - 75%
2704 * - Hypercalls + VID.SYS in ring-0 with exit optimizations:
2705 * Max: 77 760 699 bytes/s - 80%
2706 * Min: 72 677 171 bytes/s - 84%
2707 * - Win HV API with exit optimizations:
2708 * Max: 64 465 905 bytes/s - 67%
2709 * Min: 62 286 369 bytes/s - 72%
2710 * - Win HV API:
2711 * Max: 62 466 631 bytes/s - 64%
2712 * Min: 61 362 782 bytes/s - 70%
2713 *
2714 * Sending to the host system:
2715 * - Regular VirtualBox SVM:
2716 * Max: 87 728 652 bytes/s - 100%
2717 * Min: 86 923 198 bytes/s - 100%
2718 * - Hypercalls + VID.SYS in ring-0:
2719 * Max: 84 280 749 bytes/s - 96%
2720 * Min: 78 369 842 bytes/s - 90%
2721 * - Hypercalls + VID.SYS in ring-0 with exit optimizations:
2722 * Max: 84 119 932 bytes/s - 96%
2723 * Min: 77 396 811 bytes/s - 89%
2724 * - Win HV API:
2725 * Max: 81 714 377 bytes/s - 93%
2726 * Min: 78 697 419 bytes/s - 91%
2727 * - Win HV API with exit optimizations:
2728 * Max: 80 502 488 bytes/s - 91%
2729 * Min: 71 164 978 bytes/s - 82%
2730 *
2731 * Receiving from a remote Windows 10 system over a 10Gbps link:
2732 * - Hypercalls + VID.SYS in ring-0:
2733 * Max: 115 346 922 bytes/s - 136%
2734 * Min: 112 912 035 bytes/s - 137%
2735 * - Regular VirtualBox SVM:
2736 * Max: 84 517 504 bytes/s - 100%
2737 * Min: 82 597 049 bytes/s - 100%
2738 * - Hypercalls + VID.SYS in ring-0 with exit optimizations:
2739 * Max: 77 736 251 bytes/s - 92%
2740 * Min: 73 813 784 bytes/s - 89%
2741 * - Win HV API with exit optimizations:
2742 * Max: 63 035 587 bytes/s - 75%
2743 * Min: 57 538 380 bytes/s - 70%
2744 * - Win HV API:
2745 * Max: 62 279 185 bytes/s - 74%
2746 * Min: 56 813 866 bytes/s - 69%
2747 *
2748 * Sending to a remote Windows 10 system over a 10Gbps link:
2749 * - Win HV API with exit optimizations:
2750 * Max: 116 502 357 bytes/s - 103%
2751 * Min: 49 046 550 bytes/s - 59%
2752 * - Regular VirtualBox SVM:
2753 * Max: 113 030 991 bytes/s - 100%
2754 * Min: 83 059 511 bytes/s - 100%
2755 * - Hypercalls + VID.SYS in ring-0:
2756 * Max: 106 435 031 bytes/s - 94%
2757 * Min: 47 253 510 bytes/s - 57%
2758 * - Hypercalls + VID.SYS in ring-0 with exit optimizations:
2759 * Max: 94 842 287 bytes/s - 84%
2760 * Min: 68 362 172 bytes/s - 82%
2761 * - Win HV API:
2762 * Max: 65 165 225 bytes/s - 58%
2763 * Min: 47 246 573 bytes/s - 57%
2764 *
2765 * What we see here is:
2766 *
2767 * - Again consistent numbers when talking to the host. Showing that the
2768 * ring-0 approach is preferable to the ring-3 one.
2769 *
2770 * - Again when talking to a remote host, things get more difficult to
2771 * make sense of. The spread is larger and direct AMD-V gets beaten by
2772 * a different the Hyper-V approaches in each direction.
2773 *
2774 * - However, if we treat the first entry (remote host) as weird spikes, the
2775 * other entries are consistently worse compared to direct AMD-V. For the
2776 * send case we get really bad results for WinHV.
2777 *
2778 */
2779
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use