VirtualBox

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

Last change on this file since 88654 was 88654, checked in by vboxsync, 4 years ago

Typo.

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

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