VirtualBox

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

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

VMM/NEM: Added some more #ifdef'ing to reduce the amount of ioctl probing to what we actually need. bugref:10118

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