VirtualBox

source: vbox/trunk/src/VBox/VMM/include/NEMInternal.h@ 72569

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

EM,IEM,NEM: Started working on optimizing adjacent exits using IEM. Keep track of frequent exits, after 256 hits do a trial run in IEM to look for adjacent exits. Proof of concept using CPUID in NEMR3/win. bugref:9044

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.9 KB
Line 
1/* $Id: NEMInternal.h 72569 2018-06-15 19:04:01Z vboxsync $ */
2/** @file
3 * NEM - Internal header file.
4 */
5
6/*
7 * Copyright (C) 2018 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifndef ___NEMInternal_h
19#define ___NEMInternal_h
20
21#include <VBox/cdefs.h>
22#include <VBox/types.h>
23#include <VBox/vmm/nem.h>
24#include <VBox/vmm/cpum.h> /* For CPUMCPUVENDOR. */
25#include <VBox/vmm/stam.h>
26#include <VBox/vmm/vmapi.h>
27#ifdef RT_OS_WINDOWS
28#include <iprt/nt/hyperv.h>
29#include <iprt/critsect.h>
30#endif
31
32RT_C_DECLS_BEGIN
33
34
35/** @defgroup grp_nem_int Internal
36 * @ingroup grp_nem
37 * @internal
38 * @{
39 */
40
41
42#ifdef RT_OS_WINDOWS
43/*
44 * Windows: Code configuration.
45 */
46# define NEM_WIN_USE_HYPERCALLS_FOR_PAGES
47//# define NEM_WIN_USE_HYPERCALLS_FOR_REGISTERS
48//# define NEM_WIN_USE_OUR_OWN_RUN_API
49# if defined(NEM_WIN_USE_OUR_OWN_RUN_API) && !defined(NEM_WIN_USE_HYPERCALLS_FOR_REGISTERS)
50# error "NEM_WIN_USE_OUR_OWN_RUN_API requires NEM_WIN_USE_HYPERCALLS_FOR_REGISTERS"
51# endif
52# if defined(NEM_WIN_USE_OUR_OWN_RUN_API) && !defined(NEM_WIN_USE_HYPERCALLS_FOR_PAGES)
53# error "NEM_WIN_USE_OUR_OWN_RUN_API requires NEM_WIN_USE_HYPERCALLS_FOR_PAGES"
54# endif
55
56/**
57 * Windows VID I/O control information.
58 */
59typedef struct NEMWINIOCTL
60{
61 /** The I/O control function number. */
62 uint32_t uFunction;
63 uint32_t cbInput;
64 uint32_t cbOutput;
65} NEMWINIOCTL;
66
67/** @name Windows: Our two-bit physical page state for PGMPAGE
68 * @{ */
69# define NEM_WIN_PAGE_STATE_NOT_SET 0
70# define NEM_WIN_PAGE_STATE_UNMAPPED 1
71# define NEM_WIN_PAGE_STATE_READABLE 2
72# define NEM_WIN_PAGE_STATE_WRITABLE 3
73/** @} */
74
75/** Windows: Checks if a_GCPhys is subject to the limited A20 gate emulation. */
76# define NEM_WIN_IS_SUBJECT_TO_A20(a_GCPhys) ((RTGCPHYS)((a_GCPhys) - _1M) < (RTGCPHYS)_64K)
77/** Windows: Checks if a_GCPhys is relevant to the limited A20 gate emulation. */
78# define NEM_WIN_IS_RELEVANT_TO_A20(a_GCPhys) \
79 ( ((RTGCPHYS)((a_GCPhys) - _1M) < (RTGCPHYS)_64K) || ((RTGCPHYS)(a_GCPhys) < (RTGCPHYS)_64K) )
80
81/** The CPUMCTX_EXTRN_XXX mask for IEM. */
82# define NEM_WIN_CPUMCTX_EXTRN_MASK_FOR_IEM ( IEM_CPUMCTX_EXTRN_MUST_MASK | CPUMCTX_EXTRN_NEM_WIN_INHIBIT_INT \
83 | CPUMCTX_EXTRN_NEM_WIN_INHIBIT_NMI )
84/** The CPUMCTX_EXTRN_XXX mask for IEM when raising exceptions. */
85# define NEM_WIN_CPUMCTX_EXTRN_MASK_FOR_IEM_XCPT (IEM_CPUMCTX_EXTRN_XCPT_MASK | NEM_WIN_CPUMCTX_EXTRN_MASK_FOR_IEM)
86
87/** @name Windows: Interrupt window flags (NEM_WIN_INTW_F_XXX).
88 * @{ */
89# define NEM_WIN_INTW_F_NMI UINT8_C(0x01)
90# define NEM_WIN_INTW_F_REGULAR UINT8_C(0x02)
91# define NEM_WIN_INTW_F_PRIO_MASK UINT8_C(0x3c)
92# define NEM_WIN_INTW_F_PRIO_SHIFT 2
93/** @} */
94
95#endif /* RT_OS_WINDOWS */
96
97
98/** Trick to make slickedit see the static functions in the template. */
99#ifndef IN_SLICKEDIT
100# define NEM_TMPL_STATIC static
101#else
102# define NEM_TMPL_STATIC
103#endif
104
105
106/**
107 * Generic NEM exit type enumeration for use with EMHistoryAddExit.
108 *
109 * On windows we've got two different set of exit types and they are both jumping
110 * around the place value wise, so EM can use their values.
111 *
112 * @note We only have exit types for exits not covered by EM here.
113 */
114typedef enum NEMEXITTYPE
115{
116 /* windows: */
117 NEMEXITTYPE_UNRECOVERABLE_EXCEPTION = 1,
118 NEMEXITTYPE_INVALID_VP_REGISTER_VALUE,
119 NEMEXITTYPE_INTTERRUPT_WINDOW,
120 NEMEXITTYPE_HALT,
121 NEMEXITTYPE_XCPT_UD,
122 NEMEXITTYPE_XCPT_DB,
123 NEMEXITTYPE_XCPT_BP,
124 NEMEXITTYPE_CANCELED
125} NEMEXITTYPE;
126
127
128/**
129 * NEM VM Instance data.
130 */
131typedef struct NEM
132{
133 /** NEM_MAGIC. */
134 uint32_t u32Magic;
135
136 /** Set if enabled. */
137 bool fEnabled;
138 /** Set if long mode guests are allowed. */
139 bool fAllow64BitGuests;
140#ifdef RT_OS_WINDOWS
141 /** Set if we've created the EMTs. */
142 bool fCreatedEmts : 1;
143 /** WHvRunVpExitReasonX64Cpuid is supported. */
144 bool fExtendedMsrExit : 1;
145 /** WHvRunVpExitReasonX64MsrAccess is supported. */
146 bool fExtendedCpuIdExit : 1;
147 /** WHvRunVpExitReasonException is supported. */
148 bool fExtendedXcptExit : 1;
149 /** Set if we've started more than one CPU and cannot mess with A20. */
150 bool fA20Fixed : 1;
151 /** Set if A20 is enabled. */
152 bool fA20Enabled : 1;
153 /** The reported CPU vendor. */
154 CPUMCPUVENDOR enmCpuVendor;
155 /** Cache line flush size as a power of two. */
156 uint8_t cCacheLineFlushShift;
157 /** The result of WHvCapabilityCodeProcessorFeatures. */
158 union
159 {
160 /** 64-bit view. */
161 uint64_t u64;
162# ifdef _WINHVAPIDEFS_H_
163 /** Interpreed features. */
164 WHV_PROCESSOR_FEATURES u;
165# endif
166 } uCpuFeatures;
167
168 /** The partition handle. */
169# ifdef _WINHVAPIDEFS_H_
170 WHV_PARTITION_HANDLE
171# else
172 RTHCUINTPTR
173# endif
174 hPartition;
175 /** The device handle for the partition, for use with Vid APIs or direct I/O
176 * controls. */
177 RTR3PTR hPartitionDevice;
178 /** The Hyper-V partition ID. */
179 uint64_t idHvPartition;
180
181 /** Number of currently mapped pages. */
182 uint32_t volatile cMappedPages;
183
184 /** Info about the VidGetHvPartitionId I/O control interface. */
185 NEMWINIOCTL IoCtlGetHvPartitionId;
186 /** Info about the VidStartVirtualProcessor I/O control interface. */
187 NEMWINIOCTL IoCtlStartVirtualProcessor;
188 /** Info about the VidStopVirtualProcessor I/O control interface. */
189 NEMWINIOCTL IoCtlStopVirtualProcessor;
190 /** Info about the VidStopVirtualProcessor I/O control interface. */
191 NEMWINIOCTL IoCtlMessageSlotHandleAndGetNext;
192
193 /** Statistics updated by NEMR0UpdateStatistics. */
194 struct
195 {
196 uint64_t cPagesAvailable;
197 uint64_t cPagesInUse;
198 } R0Stats;
199#endif /* RT_OS_WINDOWS */
200} NEM;
201/** Pointer to NEM VM instance data. */
202typedef NEM *PNEM;
203
204/** NEM::u32Magic value. */
205#define NEM_MAGIC UINT32_C(0x004d454e)
206/** NEM::u32Magic value after termination. */
207#define NEM_MAGIC_DEAD UINT32_C(0xdead1111)
208
209
210/**
211 * NEM VMCPU Instance data.
212 */
213typedef struct NEMCPU
214{
215 /** NEMCPU_MAGIC. */
216 uint32_t u32Magic;
217 /** Whether \#UD needs to be intercepted and presented to GIM. */
218 bool fGIMTrapXcptUD : 1;
219#ifdef RT_OS_WINDOWS
220 /** The current state of the interrupt windows (NEM_WIN_INTW_F_XXX). */
221 uint8_t fCurrentInterruptWindows;
222 /** The desired state of the interrupt windows (NEM_WIN_INTW_F_XXX). */
223 uint8_t fDesiredInterruptWindows;
224 /** Last copy of HV_X64_VP_EXECUTION_STATE::InterruptShadow. */
225 bool fLastInterruptShadow : 1;
226 /** Pending APIC base value.
227 * This is set to UINT64_MAX when not pending */
228 uint64_t uPendingApicBase;
229# ifdef NEM_WIN_USE_OUR_OWN_RUN_API
230 /** Pending VINF_NEM_CHANGE_PGM_MODE, VINF_NEM_FLUSH_TLB or VINF_NEM_UPDATE_APIC_BASE. */
231 int32_t rcPending;
232 /** The VID_MSHAGN_F_XXX flags.
233 * Either VID_MSHAGN_F_HANDLE_MESSAGE | VID_MSHAGN_F_GET_NEXT_MESSAGE or zero. */
234 uint32_t fHandleAndGetFlags;
235 /** What VidMessageSlotMap returns and is used for passing exit info. */
236 RTR3PTR pvMsgSlotMapping;
237# endif
238 /** The windows thread handle. */
239 RTR3PTR hNativeThreadHandle;
240 /** Parameters for making Hyper-V hypercalls. */
241 union
242 {
243 uint8_t ab[64];
244 /** Arguments for NEMR0MapPages (HvCallMapGpaPages). */
245 struct
246 {
247 RTGCPHYS GCPhysSrc;
248 RTGCPHYS GCPhysDst; /**< Same as GCPhysSrc except maybe when the A20 gate is disabled. */
249 uint32_t cPages;
250 HV_MAP_GPA_FLAGS fFlags;
251 } MapPages;
252 /** Arguments for NEMR0UnmapPages (HvCallUnmapGpaPages). */
253 struct
254 {
255 RTGCPHYS GCPhys;
256 uint32_t cPages;
257 } UnmapPages;
258 /** Result from NEMR0QueryCpuTick. */
259 struct
260 {
261 uint64_t cTicks;
262 uint32_t uAux;
263 } QueryCpuTick;
264 /** Input and output for NEMR0DoExperiment. */
265 struct
266 {
267 uint32_t uItem;
268 bool fSuccess;
269 uint64_t uStatus;
270 uint64_t uLoValue;
271 uint64_t uHiValue;
272 } Experiment;
273 } Hypercall;
274 /** I/O control buffer, we always use this for I/O controls. */
275 union
276 {
277 uint8_t ab[64];
278 HV_PARTITION_ID idPartition;
279 HV_VP_INDEX idCpu;
280# ifdef VID_MSHAGN_F_GET_NEXT_MESSAGE
281 VID_IOCTL_INPUT_MESSAGE_SLOT_HANDLE_AND_GET_NEXT MsgSlotHandleAndGetNext;
282# endif
283 } uIoCtlBuf;
284
285 /** @name Statistics
286 * @{ */
287 STAMCOUNTER StatExitPortIo;
288 STAMCOUNTER StatExitMemUnmapped;
289 STAMCOUNTER StatExitMemIntercept;
290 STAMCOUNTER StatExitHalt;
291 STAMCOUNTER StatExitInterruptWindow;
292 STAMCOUNTER StatExitCpuId;
293 STAMCOUNTER StatExitMsr;
294 STAMCOUNTER StatExitException;
295 STAMCOUNTER StatExitExceptionBp;
296 STAMCOUNTER StatExitExceptionDb;
297 STAMCOUNTER StatExitExceptionUd;
298 STAMCOUNTER StatExitExceptionUdHandled;
299 STAMCOUNTER StatExitUnrecoverable;
300 STAMCOUNTER StatGetMsgTimeout;
301 STAMCOUNTER StatStopCpuSuccess;
302 STAMCOUNTER StatStopCpuPending;
303 STAMCOUNTER StatStopCpuPendingOdd;
304 STAMCOUNTER StatCancelChangedState;
305 STAMCOUNTER StatCancelAlertedThread;
306 STAMCOUNTER StatBreakOnCancel;
307 STAMCOUNTER StatBreakOnFFPre;
308 STAMCOUNTER StatBreakOnFFPost;
309 STAMCOUNTER StatBreakOnStatus;
310 STAMCOUNTER StatImportOnDemand;
311 STAMCOUNTER StatImportOnReturn;
312 STAMCOUNTER StatImportOnReturnSkipped;
313 STAMCOUNTER StatQueryCpuTick;
314 /** @} */
315#endif /* RT_OS_WINDOWS */
316} NEMCPU;
317/** Pointer to NEM VMCPU instance data. */
318typedef NEMCPU *PNEMCPU;
319
320/** NEMCPU::u32Magic value. */
321#define NEMCPU_MAGIC UINT32_C(0x4d454e20)
322/** NEMCPU::u32Magic value after termination. */
323#define NEMCPU_MAGIC_DEAD UINT32_C(0xdead2222)
324
325
326#ifdef IN_RING0
327# ifdef RT_OS_WINDOWS
328/**
329 * Windows: Hypercall input/ouput page info.
330 */
331typedef struct NEMR0HYPERCALLDATA
332{
333 /** Host physical address of the hypercall input/output page. */
334 RTHCPHYS HCPhysPage;
335 /** Pointer to the hypercall input/output page. */
336 uint8_t *pbPage;
337 /** Handle to the memory object of the hypercall input/output page. */
338 RTR0MEMOBJ hMemObj;
339} NEMR0HYPERCALLDATA;
340/** Pointer to a Windows hypercall input/output page info. */
341typedef NEMR0HYPERCALLDATA *PNEMR0HYPERCALLDATA;
342# endif /* RT_OS_WINDOWS */
343
344/**
345 * NEM GVMCPU instance data.
346 */
347typedef struct NEMR0PERVCPU
348{
349# ifdef RT_OS_WINDOWS
350 /** Hypercall input/ouput page. */
351 NEMR0HYPERCALLDATA HypercallData;
352# else
353 uint32_t uDummy;
354# endif
355} NEMR0PERVCPU;
356
357/**
358 * NEM GVM instance data.
359 */
360typedef struct NEMR0PERVM
361{
362# ifdef RT_OS_WINDOWS
363 /** The partition ID. */
364 uint64_t idHvPartition;
365 /** I/O control context. */
366 PSUPR0IOCTLCTX pIoCtlCtx;
367 /** Delta to add to convert a ring-0 pointer to a ring-3 one. */
368 uintptr_t offRing3ConversionDelta;
369 /** Info about the VidGetHvPartitionId I/O control interface. */
370 NEMWINIOCTL IoCtlGetHvPartitionId;
371 /** Info about the VidStartVirtualProcessor I/O control interface. */
372 NEMWINIOCTL IoCtlStartVirtualProcessor;
373 /** Info about the VidStopVirtualProcessor I/O control interface. */
374 NEMWINIOCTL IoCtlStopVirtualProcessor;
375 /** Info about the VidStopVirtualProcessor I/O control interface. */
376 NEMWINIOCTL IoCtlMessageSlotHandleAndGetNext;
377
378 /** Hypercall input/ouput page for non-EMT. */
379 NEMR0HYPERCALLDATA HypercallData;
380 /** Critical section protecting use of HypercallData. */
381 RTCRITSECT HypercallDataCritSect;
382
383# else
384 uint32_t uDummy;
385# endif
386} NEMR0PERVM;
387
388#endif /* IN_RING*/
389
390
391#ifdef IN_RING3
392int nemR3NativeInit(PVM pVM, bool fFallback, bool fForced);
393int nemR3NativeInitAfterCPUM(PVM pVM);
394int nemR3NativeInitCompleted(PVM pVM, VMINITCOMPLETED enmWhat);
395int nemR3NativeTerm(PVM pVM);
396void nemR3NativeReset(PVM pVM);
397void nemR3NativeResetCpu(PVMCPU pVCpu, bool fInitIpi);
398VBOXSTRICTRC nemR3NativeRunGC(PVM pVM, PVMCPU pVCpu);
399bool nemR3NativeCanExecuteGuest(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx);
400bool nemR3NativeSetSingleInstruction(PVM pVM, PVMCPU pVCpu, bool fEnable);
401void nemR3NativeNotifyFF(PVM pVM, PVMCPU pVCpu, uint32_t fFlags);
402
403int nemR3NativeNotifyPhysRamRegister(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb);
404int nemR3NativeNotifyPhysMmioExMap(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags, void *pvMmio2);
405int nemR3NativeNotifyPhysMmioExUnmap(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags);
406int nemR3NativeNotifyPhysRomRegisterEarly(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags);
407int nemR3NativeNotifyPhysRomRegisterLate(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags);
408void nemR3NativeNotifySetA20(PVMCPU pVCpu, bool fEnabled);
409#endif
410
411void nemHCNativeNotifyHandlerPhysicalRegister(PVM pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhys, RTGCPHYS cb);
412void nemHCNativeNotifyHandlerPhysicalDeregister(PVM pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhys, RTGCPHYS cb,
413 int fRestoreAsRAM, bool fRestoreAsRAM2);
414void nemHCNativeNotifyHandlerPhysicalModify(PVM pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhysOld,
415 RTGCPHYS GCPhysNew, RTGCPHYS cb, bool fRestoreAsRAM);
416int nemHCNativeNotifyPhysPageAllocated(PVM pVM, RTGCPHYS GCPhys, RTHCPHYS HCPhys, uint32_t fPageProt,
417 PGMPAGETYPE enmType, uint8_t *pu2State);
418void nemHCNativeNotifyPhysPageProtChanged(PVM pVM, RTGCPHYS GCPhys, RTHCPHYS HCPhys, uint32_t fPageProt,
419 PGMPAGETYPE enmType, uint8_t *pu2State);
420void nemHCNativeNotifyPhysPageChanged(PVM pVM, RTGCPHYS GCPhys, RTHCPHYS HCPhysPrev, RTHCPHYS HCPhysNew, uint32_t fPageProt,
421 PGMPAGETYPE enmType, uint8_t *pu2State);
422
423
424#ifdef RT_OS_WINDOWS
425/** Maximum number of pages we can map in a single NEMR0MapPages call. */
426# define NEM_MAX_MAP_PAGES ((PAGE_SIZE - RT_UOFFSETOF(HV_INPUT_MAP_GPA_PAGES, PageList)) / sizeof(HV_SPA_PAGE_NUMBER))
427/** Maximum number of pages we can unmap in a single NEMR0UnmapPages call. */
428# define NEM_MAX_UNMAP_PAGES 4095
429
430#endif
431/** @} */
432
433RT_C_DECLS_END
434
435#endif
436
Note: See TracBrowser for help on using the repository browser.

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