VirtualBox

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

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

NEM,CPUM,EM: Don't sync in/out the entire state when leaving the inner NEM loop, only what IEM/TRPM might need. Speeds up MMIO and I/O requiring return to ring-3. bugref:9044 [build fix]

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.8 KB
Line 
1/* $Id: NEMInternal.h 72489 2018-06-09 12:28:23Z 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 * NEM VM Instance data.
108 */
109typedef struct NEM
110{
111 /** NEM_MAGIC. */
112 uint32_t u32Magic;
113
114 /** Set if enabled. */
115 bool fEnabled;
116 /** Set if long mode guests are allowed. */
117 bool fAllow64BitGuests;
118#ifdef RT_OS_WINDOWS
119 /** Set if we've created the EMTs. */
120 bool fCreatedEmts : 1;
121 /** WHvRunVpExitReasonX64Cpuid is supported. */
122 bool fExtendedMsrExit : 1;
123 /** WHvRunVpExitReasonX64MsrAccess is supported. */
124 bool fExtendedCpuIdExit : 1;
125 /** WHvRunVpExitReasonException is supported. */
126 bool fExtendedXcptExit : 1;
127 /** Set if we've started more than one CPU and cannot mess with A20. */
128 bool fA20Fixed : 1;
129 /** Set if A20 is enabled. */
130 bool fA20Enabled : 1;
131 /** The reported CPU vendor. */
132 CPUMCPUVENDOR enmCpuVendor;
133 /** Cache line flush size as a power of two. */
134 uint8_t cCacheLineFlushShift;
135 /** The result of WHvCapabilityCodeProcessorFeatures. */
136 union
137 {
138 /** 64-bit view. */
139 uint64_t u64;
140# ifdef _WINHVAPIDEFS_H_
141 /** Interpreed features. */
142 WHV_PROCESSOR_FEATURES u;
143# endif
144 } uCpuFeatures;
145
146 /** The partition handle. */
147# ifdef _WINHVAPIDEFS_H_
148 WHV_PARTITION_HANDLE
149# else
150 RTHCUINTPTR
151# endif
152 hPartition;
153 /** The device handle for the partition, for use with Vid APIs or direct I/O
154 * controls. */
155 RTR3PTR hPartitionDevice;
156 /** The Hyper-V partition ID. */
157 uint64_t idHvPartition;
158
159 /** Number of currently mapped pages. */
160 uint32_t volatile cMappedPages;
161
162 /** Info about the VidGetHvPartitionId I/O control interface. */
163 NEMWINIOCTL IoCtlGetHvPartitionId;
164 /** Info about the VidStartVirtualProcessor I/O control interface. */
165 NEMWINIOCTL IoCtlStartVirtualProcessor;
166 /** Info about the VidStopVirtualProcessor I/O control interface. */
167 NEMWINIOCTL IoCtlStopVirtualProcessor;
168 /** Info about the VidStopVirtualProcessor I/O control interface. */
169 NEMWINIOCTL IoCtlMessageSlotHandleAndGetNext;
170
171 /** Statistics updated by NEMR0UpdateStatistics. */
172 struct
173 {
174 uint64_t cPagesAvailable;
175 uint64_t cPagesInUse;
176 } R0Stats;
177#endif /* RT_OS_WINDOWS */
178} NEM;
179/** Pointer to NEM VM instance data. */
180typedef NEM *PNEM;
181
182/** NEM::u32Magic value. */
183#define NEM_MAGIC UINT32_C(0x004d454e)
184/** NEM::u32Magic value after termination. */
185#define NEM_MAGIC_DEAD UINT32_C(0xdead1111)
186
187
188/**
189 * NEM VMCPU Instance data.
190 */
191typedef struct NEMCPU
192{
193 /** NEMCPU_MAGIC. */
194 uint32_t u32Magic;
195 /** Whether \#UD needs to be intercepted and presented to GIM. */
196 bool fGIMTrapXcptUD : 1;
197#ifdef RT_OS_WINDOWS
198 /** The current state of the interrupt windows (NEM_WIN_INTW_F_XXX). */
199 uint8_t fCurrentInterruptWindows;
200 /** The desired state of the interrupt windows (NEM_WIN_INTW_F_XXX). */
201 uint8_t fDesiredInterruptWindows;
202 /** Last copy of HV_X64_VP_EXECUTION_STATE::InterruptShadow. */
203 bool fLastInterruptShadow : 1;
204 /** Pending APIC base value.
205 * This is set to UINT64_MAX when not pending */
206 uint64_t uPendingApicBase;
207# ifdef NEM_WIN_USE_OUR_OWN_RUN_API
208 /** Pending VINF_NEM_CHANGE_PGM_MODE, VINF_NEM_FLUSH_TLB or VINF_NEM_UPDATE_APIC_BASE. */
209 int32_t rcPending;
210 /** The VID_MSHAGN_F_XXX flags.
211 * Either VID_MSHAGN_F_HANDLE_MESSAGE | VID_MSHAGN_F_GET_NEXT_MESSAGE or zero. */
212 uint32_t fHandleAndGetFlags;
213 /** What VidMessageSlotMap returns and is used for passing exit info. */
214 RTR3PTR pvMsgSlotMapping;
215# endif
216 /** The windows thread handle. */
217 RTR3PTR hNativeThreadHandle;
218 /** Parameters for making Hyper-V hypercalls. */
219 union
220 {
221 uint8_t ab[64];
222 /** Arguments for NEMR0MapPages (HvCallMapGpaPages). */
223 struct
224 {
225 RTGCPHYS GCPhysSrc;
226 RTGCPHYS GCPhysDst; /**< Same as GCPhysSrc except maybe when the A20 gate is disabled. */
227 uint32_t cPages;
228 HV_MAP_GPA_FLAGS fFlags;
229 } MapPages;
230 /** Arguments for NEMR0UnmapPages (HvCallUnmapGpaPages). */
231 struct
232 {
233 RTGCPHYS GCPhys;
234 uint32_t cPages;
235 } UnmapPages;
236 } Hypercall;
237 /** I/O control buffer, we always use this for I/O controls. */
238 union
239 {
240 uint8_t ab[64];
241 HV_PARTITION_ID idPartition;
242 HV_VP_INDEX idCpu;
243# ifdef VID_MSHAGN_F_GET_NEXT_MESSAGE
244 VID_IOCTL_INPUT_MESSAGE_SLOT_HANDLE_AND_GET_NEXT MsgSlotHandleAndGetNext;
245# endif
246 } uIoCtlBuf;
247
248 /** @name Statistics
249 * @{ */
250 STAMCOUNTER StatExitPortIo;
251 STAMCOUNTER StatExitMemUnmapped;
252 STAMCOUNTER StatExitMemIntercept;
253 STAMCOUNTER StatExitHalt;
254 STAMCOUNTER StatExitInterruptWindow;
255 STAMCOUNTER StatExitCpuId;
256 STAMCOUNTER StatExitMsr;
257 STAMCOUNTER StatExitException;
258 STAMCOUNTER StatExitExceptionBp;
259 STAMCOUNTER StatExitExceptionDb;
260 STAMCOUNTER StatExitExceptionUd;
261 STAMCOUNTER StatExitExceptionUdHandled;
262 STAMCOUNTER StatExitUnrecoverable;
263 STAMCOUNTER StatGetMsgTimeout;
264 STAMCOUNTER StatStopCpuSuccess;
265 STAMCOUNTER StatStopCpuPending;
266 STAMCOUNTER StatStopCpuPendingOdd;
267 STAMCOUNTER StatCancelChangedState;
268 STAMCOUNTER StatCancelAlertedThread;
269 STAMCOUNTER StatBreakOnCancel;
270 STAMCOUNTER StatBreakOnFFPre;
271 STAMCOUNTER StatBreakOnFFPost;
272 STAMCOUNTER StatBreakOnStatus;
273 STAMCOUNTER StatImportOnDemand;
274 STAMCOUNTER StatImportOnReturn;
275 STAMCOUNTER StatImportOnReturnSkipped;
276 /** @} */
277#endif /* RT_OS_WINDOWS */
278} NEMCPU;
279/** Pointer to NEM VMCPU instance data. */
280typedef NEMCPU *PNEMCPU;
281
282/** NEMCPU::u32Magic value. */
283#define NEMCPU_MAGIC UINT32_C(0x4d454e20)
284/** NEMCPU::u32Magic value after termination. */
285#define NEMCPU_MAGIC_DEAD UINT32_C(0xdead2222)
286
287
288#ifdef IN_RING0
289# ifdef RT_OS_WINDOWS
290/**
291 * Windows: Hypercall input/ouput page info.
292 */
293typedef struct NEMR0HYPERCALLDATA
294{
295 /** Host physical address of the hypercall input/output page. */
296 RTHCPHYS HCPhysPage;
297 /** Pointer to the hypercall input/output page. */
298 uint8_t *pbPage;
299 /** Handle to the memory object of the hypercall input/output page. */
300 RTR0MEMOBJ hMemObj;
301} NEMR0HYPERCALLDATA;
302/** Pointer to a Windows hypercall input/output page info. */
303typedef NEMR0HYPERCALLDATA *PNEMR0HYPERCALLDATA;
304# endif /* RT_OS_WINDOWS */
305
306/**
307 * NEM GVMCPU instance data.
308 */
309typedef struct NEMR0PERVCPU
310{
311# ifdef RT_OS_WINDOWS
312 /** Hypercall input/ouput page. */
313 NEMR0HYPERCALLDATA HypercallData;
314# else
315 uint32_t uDummy;
316# endif
317} NEMR0PERVCPU;
318
319/**
320 * NEM GVM instance data.
321 */
322typedef struct NEMR0PERVM
323{
324# ifdef RT_OS_WINDOWS
325 /** The partition ID. */
326 uint64_t idHvPartition;
327 /** I/O control context. */
328 PSUPR0IOCTLCTX pIoCtlCtx;
329 /** Delta to add to convert a ring-0 pointer to a ring-3 one. */
330 uintptr_t offRing3ConversionDelta;
331 /** Info about the VidGetHvPartitionId I/O control interface. */
332 NEMWINIOCTL IoCtlGetHvPartitionId;
333 /** Info about the VidStartVirtualProcessor I/O control interface. */
334 NEMWINIOCTL IoCtlStartVirtualProcessor;
335 /** Info about the VidStopVirtualProcessor I/O control interface. */
336 NEMWINIOCTL IoCtlStopVirtualProcessor;
337 /** Info about the VidStopVirtualProcessor I/O control interface. */
338 NEMWINIOCTL IoCtlMessageSlotHandleAndGetNext;
339
340 /** Hypercall input/ouput page for non-EMT. */
341 NEMR0HYPERCALLDATA HypercallData;
342 /** Critical section protecting use of HypercallData. */
343 RTCRITSECT HypercallDataCritSect;
344
345# else
346 uint32_t uDummy;
347# endif
348} NEMR0PERVM;
349
350#endif /* IN_RING*/
351
352
353#ifdef IN_RING3
354int nemR3NativeInit(PVM pVM, bool fFallback, bool fForced);
355int nemR3NativeInitAfterCPUM(PVM pVM);
356int nemR3NativeInitCompleted(PVM pVM, VMINITCOMPLETED enmWhat);
357int nemR3NativeTerm(PVM pVM);
358void nemR3NativeReset(PVM pVM);
359void nemR3NativeResetCpu(PVMCPU pVCpu, bool fInitIpi);
360VBOXSTRICTRC nemR3NativeRunGC(PVM pVM, PVMCPU pVCpu);
361bool nemR3NativeCanExecuteGuest(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx);
362bool nemR3NativeSetSingleInstruction(PVM pVM, PVMCPU pVCpu, bool fEnable);
363void nemR3NativeNotifyFF(PVM pVM, PVMCPU pVCpu, uint32_t fFlags);
364
365int nemR3NativeNotifyPhysRamRegister(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb);
366int nemR3NativeNotifyPhysMmioExMap(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags, void *pvMmio2);
367int nemR3NativeNotifyPhysMmioExUnmap(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags);
368int nemR3NativeNotifyPhysRomRegisterEarly(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags);
369int nemR3NativeNotifyPhysRomRegisterLate(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags);
370void nemR3NativeNotifySetA20(PVMCPU pVCpu, bool fEnabled);
371#endif
372
373void nemHCNativeNotifyHandlerPhysicalRegister(PVM pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhys, RTGCPHYS cb);
374void nemHCNativeNotifyHandlerPhysicalDeregister(PVM pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhys, RTGCPHYS cb,
375 int fRestoreAsRAM, bool fRestoreAsRAM2);
376void nemHCNativeNotifyHandlerPhysicalModify(PVM pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhysOld,
377 RTGCPHYS GCPhysNew, RTGCPHYS cb, bool fRestoreAsRAM);
378int nemHCNativeNotifyPhysPageAllocated(PVM pVM, RTGCPHYS GCPhys, RTHCPHYS HCPhys, uint32_t fPageProt,
379 PGMPAGETYPE enmType, uint8_t *pu2State);
380void nemHCNativeNotifyPhysPageProtChanged(PVM pVM, RTGCPHYS GCPhys, RTHCPHYS HCPhys, uint32_t fPageProt,
381 PGMPAGETYPE enmType, uint8_t *pu2State);
382void nemHCNativeNotifyPhysPageChanged(PVM pVM, RTGCPHYS GCPhys, RTHCPHYS HCPhysPrev, RTHCPHYS HCPhysNew, uint32_t fPageProt,
383 PGMPAGETYPE enmType, uint8_t *pu2State);
384
385
386#ifdef RT_OS_WINDOWS
387/** Maximum number of pages we can map in a single NEMR0MapPages call. */
388# define NEM_MAX_MAP_PAGES ((PAGE_SIZE - RT_UOFFSETOF(HV_INPUT_MAP_GPA_PAGES, PageList)) / sizeof(HV_SPA_PAGE_NUMBER))
389/** Maximum number of pages we can unmap in a single NEMR0UnmapPages call. */
390# define NEM_MAX_UNMAP_PAGES 4095
391
392#endif
393/** @} */
394
395RT_C_DECLS_END
396
397#endif
398
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