1 | /* $Id: NEMInternal.h 71293 2018-03-09 21:11:20Z 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 | #endif
|
---|
30 |
|
---|
31 | RT_C_DECLS_BEGIN
|
---|
32 |
|
---|
33 |
|
---|
34 | /** @defgroup grp_nem_int Internal
|
---|
35 | * @ingroup grp_nem
|
---|
36 | * @internal
|
---|
37 | * @{
|
---|
38 | */
|
---|
39 |
|
---|
40 |
|
---|
41 | #ifdef RT_OS_WINDOWS
|
---|
42 | /*
|
---|
43 | * Windows: Code configuration.
|
---|
44 | */
|
---|
45 | # define NEM_WIN_USE_HYPERCALLS_FOR_PAGES
|
---|
46 | # define NEM_WIN_USE_HYPERCALLS_FOR_REGISTERS
|
---|
47 | # define NEM_WIN_USE_OUR_OWN_RUN_API
|
---|
48 | # if defined(NEM_WIN_USE_OUR_OWN_RUN_API) && !defined(NEM_WIN_USE_HYPERCALLS_FOR_REGISTERS)
|
---|
49 | # error "NEM_WIN_USE_OUR_OWN_RUN_API requires NEM_WIN_USE_HYPERCALLS_FOR_REGISTERS"
|
---|
50 | # endif
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Windows VID I/O control information.
|
---|
54 | */
|
---|
55 | typedef struct NEMWINIOCTL
|
---|
56 | {
|
---|
57 | /** The I/O control function number. */
|
---|
58 | uint32_t uFunction;
|
---|
59 | uint32_t cbInput;
|
---|
60 | uint32_t cbOutput;
|
---|
61 | } NEMWINIOCTL;
|
---|
62 |
|
---|
63 | /** @name Windows: Our two-bit physical page state for PGMPAGE
|
---|
64 | * @{ */
|
---|
65 | # define NEM_WIN_PAGE_STATE_NOT_SET 0
|
---|
66 | # define NEM_WIN_PAGE_STATE_UNMAPPED 1
|
---|
67 | # define NEM_WIN_PAGE_STATE_READABLE 2
|
---|
68 | # define NEM_WIN_PAGE_STATE_WRITABLE 3
|
---|
69 | /** @} */
|
---|
70 |
|
---|
71 | /** Windows: Checks if a_GCPhys is subject to the limited A20 gate emulation. */
|
---|
72 | # define NEM_WIN_IS_SUBJECT_TO_A20(a_GCPhys) ((RTGCPHYS)((a_GCPhys) - _1M) < (RTGCPHYS)_64K)
|
---|
73 | /** Windows: Checks if a_GCPhys is relevant to the limited A20 gate emulation. */
|
---|
74 | # define NEM_WIN_IS_RELEVANT_TO_A20(a_GCPhys) \
|
---|
75 | ( ((RTGCPHYS)((a_GCPhys) - _1M) < (RTGCPHYS)_64K) || ((RTGCPHYS)(a_GCPhys) < (RTGCPHYS)_64K) )
|
---|
76 |
|
---|
77 | /** The CPUMCTX_EXTRN_XXX mask for IEM. */
|
---|
78 | # define NEM_WIN_CPUMCTX_EXTRN_MASK_FOR_IEM CPUMCTX_EXTRN_ALL
|
---|
79 |
|
---|
80 | #endif /* RT_OS_WINDOWS */
|
---|
81 |
|
---|
82 |
|
---|
83 | /** Trick to make slickedit see the static functions in the template. */
|
---|
84 | #ifndef IN_SLICKEDIT
|
---|
85 | # define NEM_TMPL_STATIC static
|
---|
86 | #else
|
---|
87 | # define NEM_TMPL_STATIC
|
---|
88 | #endif
|
---|
89 |
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * NEM VM Instance data.
|
---|
93 | */
|
---|
94 | typedef struct NEM
|
---|
95 | {
|
---|
96 | /** NEM_MAGIC. */
|
---|
97 | uint32_t u32Magic;
|
---|
98 |
|
---|
99 | /** Set if enabled. */
|
---|
100 | bool fEnabled;
|
---|
101 | #ifdef RT_OS_WINDOWS
|
---|
102 | /** Set if we've created the EMTs. */
|
---|
103 | bool fCreatedEmts : 1;
|
---|
104 | /** WHvRunVpExitReasonX64Cpuid is supported. */
|
---|
105 | bool fExtendedMsrExit : 1;
|
---|
106 | /** WHvRunVpExitReasonX64MsrAccess is supported. */
|
---|
107 | bool fExtendedCpuIdExit : 1;
|
---|
108 | /** WHvRunVpExitReasonException is supported. */
|
---|
109 | bool fExtendedXcptExit : 1;
|
---|
110 | /** Set if we've started more than one CPU and cannot mess with A20. */
|
---|
111 | bool fA20Fixed : 1;
|
---|
112 | /** Set if A20 is enabled. */
|
---|
113 | bool fA20Enabled : 1;
|
---|
114 | /** The reported CPU vendor. */
|
---|
115 | CPUMCPUVENDOR enmCpuVendor;
|
---|
116 | /** Cache line flush size as a power of two. */
|
---|
117 | uint8_t cCacheLineFlushShift;
|
---|
118 | /** The result of WHvCapabilityCodeProcessorFeatures. */
|
---|
119 | union
|
---|
120 | {
|
---|
121 | /** 64-bit view. */
|
---|
122 | uint64_t u64;
|
---|
123 | # ifdef _WINHVAPIDEFS_H_
|
---|
124 | /** Interpreed features. */
|
---|
125 | WHV_PROCESSOR_FEATURES u;
|
---|
126 | # endif
|
---|
127 | } uCpuFeatures;
|
---|
128 |
|
---|
129 | /** The partition handle. */
|
---|
130 | # ifdef _WINHVAPIDEFS_H_
|
---|
131 | WHV_PARTITION_HANDLE
|
---|
132 | # else
|
---|
133 | RTHCUINTPTR
|
---|
134 | # endif
|
---|
135 | hPartition;
|
---|
136 | /** The device handle for the partition, for use with Vid APIs or direct I/O
|
---|
137 | * controls. */
|
---|
138 | RTR3PTR hPartitionDevice;
|
---|
139 | /** The Hyper-V partition ID. */
|
---|
140 | uint64_t idHvPartition;
|
---|
141 |
|
---|
142 | /** Number of currently mapped pages. */
|
---|
143 | uint32_t volatile cMappedPages;
|
---|
144 |
|
---|
145 | /** Info about the VidGetHvPartitionId I/O control interface. */
|
---|
146 | NEMWINIOCTL IoCtlGetHvPartitionId;
|
---|
147 | /** Info about the VidStartVirtualProcessor I/O control interface. */
|
---|
148 | NEMWINIOCTL IoCtlStartVirtualProcessor;
|
---|
149 | /** Info about the VidStopVirtualProcessor I/O control interface. */
|
---|
150 | NEMWINIOCTL IoCtlStopVirtualProcessor;
|
---|
151 | /** Info about the VidStopVirtualProcessor I/O control interface. */
|
---|
152 | NEMWINIOCTL IoCtlMessageSlotHandleAndGetNext;
|
---|
153 |
|
---|
154 | #endif /* RT_OS_WINDOWS */
|
---|
155 | } NEM;
|
---|
156 | /** Pointer to NEM VM instance data. */
|
---|
157 | typedef NEM *PNEM;
|
---|
158 |
|
---|
159 | /** NEM::u32Magic value. */
|
---|
160 | #define NEM_MAGIC UINT32_C(0x004d454e)
|
---|
161 | /** NEM::u32Magic value after termination. */
|
---|
162 | #define NEM_MAGIC_DEAD UINT32_C(0xdead1111)
|
---|
163 |
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * NEM VMCPU Instance data.
|
---|
167 | */
|
---|
168 | typedef struct NEMCPU
|
---|
169 | {
|
---|
170 | /** NEMCPU_MAGIC. */
|
---|
171 | uint32_t u32Magic;
|
---|
172 | #ifdef RT_OS_WINDOWS
|
---|
173 | # ifdef NEM_WIN_USE_OUR_OWN_RUN_API
|
---|
174 | /** Pending VERR_NEM_CHANGE_PGM_MODE or VERR_NEM_FLUSH_TLB. */
|
---|
175 | int32_t rcPgmPending;
|
---|
176 | /** The VID_MSHAGN_F_XXX flags.
|
---|
177 | * Either VID_MSHAGN_F_HANDLE_MESSAGE | VID_MSHAGN_F_GET_NEXT_MESSAGE or zero. */
|
---|
178 | uint32_t fHandleAndGetFlags;
|
---|
179 | /** What VidMessageSlotMap returns and is used for passing exit info. */
|
---|
180 | RTR3PTR pvMsgSlotMapping;
|
---|
181 | # endif
|
---|
182 | /** The windows thread handle. */
|
---|
183 | RTR3PTR hNativeThreadHandle;
|
---|
184 | /** Parameters for making Hyper-V hypercalls. */
|
---|
185 | union
|
---|
186 | {
|
---|
187 | uint8_t ab[64];
|
---|
188 | /** Arguments for NEMR0MapPages (HvCallMapGpaPages). */
|
---|
189 | struct
|
---|
190 | {
|
---|
191 | RTGCPHYS GCPhysSrc;
|
---|
192 | RTGCPHYS GCPhysDst; /**< Same as GCPhysSrc except maybe when the A20 gate is disabled. */
|
---|
193 | uint32_t cPages;
|
---|
194 | HV_MAP_GPA_FLAGS fFlags;
|
---|
195 | } MapPages;
|
---|
196 | /** Arguments for NEMR0UnmapPages (HvCallUnmapGpaPages). */
|
---|
197 | struct
|
---|
198 | {
|
---|
199 | RTGCPHYS GCPhys;
|
---|
200 | uint32_t cPages;
|
---|
201 | } UnmapPages;
|
---|
202 | } Hypercall;
|
---|
203 | /** I/O control buffer, we always use this for I/O controls. */
|
---|
204 | union
|
---|
205 | {
|
---|
206 | uint8_t ab[64];
|
---|
207 | HV_PARTITION_ID idPartition;
|
---|
208 | HV_VP_INDEX idCpu;
|
---|
209 | # ifdef VID_MSHAGN_F_GET_NEXT_MESSAGE
|
---|
210 | VID_IOCTL_INPUT_MESSAGE_SLOT_HANDLE_AND_GET_NEXT MsgSlotHandleAndGetNext;
|
---|
211 | # endif
|
---|
212 | } uIoCtlBuf;
|
---|
213 |
|
---|
214 | /** @name Statistics
|
---|
215 | * @{ */
|
---|
216 | STAMCOUNTER StatExitPortIo;
|
---|
217 | STAMCOUNTER StatExitMemUnmapped;
|
---|
218 | STAMCOUNTER StatExitMemIntercept;
|
---|
219 | STAMCOUNTER StatExitHalt;
|
---|
220 | STAMCOUNTER StatGetMsgTimeout;
|
---|
221 | STAMCOUNTER StatStopCpuSuccess;
|
---|
222 | STAMCOUNTER StatStopCpuPending;
|
---|
223 | STAMCOUNTER StatCancelChangedState;
|
---|
224 | STAMCOUNTER StatCancelAlertedThread;
|
---|
225 | STAMCOUNTER StatBreakOnCancel;
|
---|
226 | STAMCOUNTER StatBreakOnFFPre;
|
---|
227 | STAMCOUNTER StatBreakOnFFPost;
|
---|
228 | STAMCOUNTER StatBreakOnStatus;
|
---|
229 | /** @} */
|
---|
230 | #endif
|
---|
231 | } NEMCPU;
|
---|
232 | /** Pointer to NEM VMCPU instance data. */
|
---|
233 | typedef NEMCPU *PNEMCPU;
|
---|
234 |
|
---|
235 | /** NEMCPU::u32Magic value. */
|
---|
236 | #define NEMCPU_MAGIC UINT32_C(0x4d454e20)
|
---|
237 | /** NEMCPU::u32Magic value after termination. */
|
---|
238 | #define NEMCPU_MAGIC_DEAD UINT32_C(0xdead2222)
|
---|
239 |
|
---|
240 |
|
---|
241 | #ifdef IN_RING0
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * NEM GVMCPU instance data.
|
---|
245 | */
|
---|
246 | typedef struct NEMR0PERVCPU
|
---|
247 | {
|
---|
248 | # ifdef RT_OS_WINDOWS
|
---|
249 | /** @name Hypercall input/ouput page.
|
---|
250 | * @{ */
|
---|
251 | /** Host physical address of the hypercall input/output page. */
|
---|
252 | RTHCPHYS HCPhysHypercallData;
|
---|
253 | /** Pointer to the hypercall input/output page. */
|
---|
254 | uint8_t *pbHypercallData;
|
---|
255 | /** Handle to the memory object of the hypercall input/output page. */
|
---|
256 | RTR0MEMOBJ hHypercallDataMemObj;
|
---|
257 | /** @} */
|
---|
258 | # else
|
---|
259 | uint32_t uDummy;
|
---|
260 | # endif
|
---|
261 | } NEMR0PERVCPU;
|
---|
262 |
|
---|
263 | /**
|
---|
264 | * NEM GVM instance data.
|
---|
265 | */
|
---|
266 | typedef struct NEMR0PERVM
|
---|
267 | {
|
---|
268 | # ifdef RT_OS_WINDOWS
|
---|
269 | /** The partition ID. */
|
---|
270 | uint64_t idHvPartition;
|
---|
271 | /** I/O control context. */
|
---|
272 | PSUPR0IOCTLCTX pIoCtlCtx;
|
---|
273 | /** Delta to add to convert a ring-0 pointer to a ring-3 one. */
|
---|
274 | uintptr_t offRing3ConversionDelta;
|
---|
275 | /** Info about the VidGetHvPartitionId I/O control interface. */
|
---|
276 | NEMWINIOCTL IoCtlGetHvPartitionId;
|
---|
277 | /** Info about the VidStartVirtualProcessor I/O control interface. */
|
---|
278 | NEMWINIOCTL IoCtlStartVirtualProcessor;
|
---|
279 | /** Info about the VidStopVirtualProcessor I/O control interface. */
|
---|
280 | NEMWINIOCTL IoCtlStopVirtualProcessor;
|
---|
281 | /** Info about the VidStopVirtualProcessor I/O control interface. */
|
---|
282 | NEMWINIOCTL IoCtlMessageSlotHandleAndGetNext;
|
---|
283 |
|
---|
284 | # else
|
---|
285 | uint32_t uDummy;
|
---|
286 | # endif
|
---|
287 | } NEMR0PERVM;
|
---|
288 |
|
---|
289 | #endif /* IN_RING*/
|
---|
290 |
|
---|
291 |
|
---|
292 | #ifdef IN_RING3
|
---|
293 | int nemR3NativeInit(PVM pVM, bool fFallback, bool fForced);
|
---|
294 | int nemR3NativeInitAfterCPUM(PVM pVM);
|
---|
295 | int nemR3NativeInitCompleted(PVM pVM, VMINITCOMPLETED enmWhat);
|
---|
296 | int nemR3NativeTerm(PVM pVM);
|
---|
297 | void nemR3NativeReset(PVM pVM);
|
---|
298 | void nemR3NativeResetCpu(PVMCPU pVCpu, bool fInitIpi);
|
---|
299 | VBOXSTRICTRC nemR3NativeRunGC(PVM pVM, PVMCPU pVCpu);
|
---|
300 | bool nemR3NativeCanExecuteGuest(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx);
|
---|
301 | bool nemR3NativeSetSingleInstruction(PVM pVM, PVMCPU pVCpu, bool fEnable);
|
---|
302 | void nemR3NativeNotifyFF(PVM pVM, PVMCPU pVCpu, uint32_t fFlags);
|
---|
303 |
|
---|
304 | int nemR3NativeNotifyPhysRamRegister(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb);
|
---|
305 | int nemR3NativeNotifyPhysMmioExMap(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags, void *pvMmio2);
|
---|
306 | int nemR3NativeNotifyPhysMmioExUnmap(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags);
|
---|
307 | int nemR3NativeNotifyPhysRomRegisterEarly(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags);
|
---|
308 | int nemR3NativeNotifyPhysRomRegisterLate(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS cb, uint32_t fFlags);
|
---|
309 | void nemR3NativeNotifySetA20(PVMCPU pVCpu, bool fEnabled);
|
---|
310 | #endif
|
---|
311 |
|
---|
312 | void nemHCNativeNotifyHandlerPhysicalRegister(PVM pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhys, RTGCPHYS cb);
|
---|
313 | void nemHCNativeNotifyHandlerPhysicalDeregister(PVM pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhys, RTGCPHYS cb,
|
---|
314 | int fRestoreAsRAM, bool fRestoreAsRAM2);
|
---|
315 | void nemHCNativeNotifyHandlerPhysicalModify(PVM pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhysOld,
|
---|
316 | RTGCPHYS GCPhysNew, RTGCPHYS cb, bool fRestoreAsRAM);
|
---|
317 | int nemHCNativeNotifyPhysPageAllocated(PVM pVM, RTGCPHYS GCPhys, RTHCPHYS HCPhys, uint32_t fPageProt,
|
---|
318 | PGMPAGETYPE enmType, uint8_t *pu2State);
|
---|
319 | void nemHCNativeNotifyPhysPageProtChanged(PVM pVM, RTGCPHYS GCPhys, RTHCPHYS HCPhys, uint32_t fPageProt,
|
---|
320 | PGMPAGETYPE enmType, uint8_t *pu2State);
|
---|
321 | void nemHCNativeNotifyPhysPageChanged(PVM pVM, RTGCPHYS GCPhys, RTHCPHYS HCPhysPrev, RTHCPHYS HCPhysNew, uint32_t fPageProt,
|
---|
322 | PGMPAGETYPE enmType, uint8_t *pu2State);
|
---|
323 |
|
---|
324 |
|
---|
325 | #ifdef RT_OS_WINDOWS
|
---|
326 | /** Maximum number of pages we can map in a single NEMR0MapPages call. */
|
---|
327 | # define NEM_MAX_MAP_PAGES ((PAGE_SIZE - RT_UOFFSETOF(HV_INPUT_MAP_GPA_PAGES, PageList)) / sizeof(HV_SPA_PAGE_NUMBER))
|
---|
328 | /** Maximum number of pages we can unmap in a single NEMR0UnmapPages call. */
|
---|
329 | # define NEM_MAX_UNMAP_PAGES 4095
|
---|
330 |
|
---|
331 | #endif
|
---|
332 | /** @} */
|
---|
333 |
|
---|
334 | RT_C_DECLS_END
|
---|
335 |
|
---|
336 | #endif
|
---|
337 |
|
---|