1 | /** @file
|
---|
2 | * HM - Intel/AMD VM Hardware Assisted Virtualization Manager (VMM)
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2013 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.215389.xyz. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef ___VBox_vmm_hm_h
|
---|
27 | #define ___VBox_vmm_hm_h
|
---|
28 |
|
---|
29 | #include <VBox/vmm/pgm.h>
|
---|
30 | #include <VBox/vmm/cpum.h>
|
---|
31 | #include <VBox/vmm/vmm.h>
|
---|
32 | #include <iprt/mp.h>
|
---|
33 |
|
---|
34 |
|
---|
35 | /** @defgroup grp_hm The VM Hardware Manager API
|
---|
36 | * @{
|
---|
37 | */
|
---|
38 |
|
---|
39 | RT_C_DECLS_BEGIN
|
---|
40 |
|
---|
41 | /** @def VMCPU_HMCF_CLEAR
|
---|
42 | * Clears a HM-context flag for the given VCPU.
|
---|
43 | *
|
---|
44 | * @param pVCpu Pointer to the VMCPU.
|
---|
45 | * @param fFlag The flag to clear.
|
---|
46 | */
|
---|
47 | #define VMCPU_HMCF_CLEAR(pVCpu, fFlag) ((pVCpu)->hm.s.fContextUseFlags &= ~(fFlag))
|
---|
48 |
|
---|
49 | /** @def VMCPU_FF_SET
|
---|
50 | * Sets a HM-context flag for the given VCPU.
|
---|
51 | *
|
---|
52 | * @param pVCpu Pointer to the VMCPU.
|
---|
53 | * @param fFlag The flag to set.
|
---|
54 | */
|
---|
55 | #define VMCPU_HMCF_SET(pVCpu, fFlag) ((pVCpu)->hm.s.fContextUseFlags |= (fFlag))
|
---|
56 |
|
---|
57 | /** @def VMCPU_HMCF_IS_SET
|
---|
58 | * Checks if all the flags in the specified HM-context set is pending.
|
---|
59 | *
|
---|
60 | * @param pVCpu Pointer to the VMCPU.
|
---|
61 | * @param fFlag The flag to check.
|
---|
62 | */
|
---|
63 | #define VMCPU_HMCF_IS_SET(pVCpu, fFlag) (((pVCpu)->hm.s.fContextUseFlags & (fFlag)) == (fFlag))
|
---|
64 |
|
---|
65 | /** @def VMCPU_HMCF_IS_PENDING
|
---|
66 | * Checks if one or more of the flags in the specified HM-context set is
|
---|
67 | * pending.
|
---|
68 | *
|
---|
69 | * @param pVCpu Pointer to the VMCPU.
|
---|
70 | * @param fFlags The flags to check for.
|
---|
71 | */
|
---|
72 | #define VMCPU_HMCF_IS_PENDING(pVCpu, fFlags) RT_BOOL((pVCpu)->hm.s.fContextUseFlags & (fFlags))
|
---|
73 |
|
---|
74 | /** @def VMCPU_HMCF_IS_PENDING_ONLY
|
---|
75 | * Checks if -only- one or more of the specified HM-context flags is pending.
|
---|
76 | *
|
---|
77 | * @param pVCpu Pointer to the VMCPU.
|
---|
78 | * @param fFlags The flags to check for.
|
---|
79 | */
|
---|
80 | #define VMCPU_HMCF_IS_PENDING_ONLY(pVCpu, fFlags) !RT_BOOL((pVCpu)->hm.s.fContextUseFlags & ~(fFlags))
|
---|
81 |
|
---|
82 | /** @def VMCPU_HMCF_IS_SET_ONLY
|
---|
83 | * Checks if -only- all the flags in the specified HM-context set is pending.
|
---|
84 | *
|
---|
85 | * @param pVCpu Pointer to the VMCPU.
|
---|
86 | * @param fFlags The flags to check for.
|
---|
87 | */
|
---|
88 | #define VMCPU_HMCF_IS_SET_ONLY(pVCpu, fFlags) ((pVCpu)->hm.s.fContextUseFlags == (fFlags))
|
---|
89 |
|
---|
90 | /** @def VMCPU_HMCF_RESET_TO
|
---|
91 | * Resets the HM-context flags to the specified value.
|
---|
92 | *
|
---|
93 | * @param pVCpu Pointer to the VMCPU.
|
---|
94 | * @param fFlags The new value.
|
---|
95 | */
|
---|
96 | #define VMCPU_HMCF_RESET_TO(pVCpu, fFlags) ((pVCpu)->hm.s.fContextUseFlags = (fFlags))
|
---|
97 |
|
---|
98 | /** @def VMCPU_HMCF_VALUE
|
---|
99 | * Returns the current HM-context flags value.
|
---|
100 | *
|
---|
101 | * @param pVCpu Pointer to the VMCPU.
|
---|
102 | */
|
---|
103 | #define VMCPU_HMCF_VALUE(pVCpu) ((pVCpu)->hm.s.fContextUseFlags)
|
---|
104 |
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Checks whether HM (VT-x/AMD-V) is being used by this VM.
|
---|
108 | *
|
---|
109 | * @retval @c true if used.
|
---|
110 | * @retval @c false if software virtualization (raw-mode) is used.
|
---|
111 | *
|
---|
112 | * @param a_pVM The cross context VM structure.
|
---|
113 | * @sa HMIsEnabledNotMacro, HMR3IsEnabled
|
---|
114 | * @internal
|
---|
115 | */
|
---|
116 | #if defined(VBOX_STRICT) && defined(IN_RING3)
|
---|
117 | # define HMIsEnabled(a_pVM) HMIsEnabledNotMacro(a_pVM)
|
---|
118 | #else
|
---|
119 | # define HMIsEnabled(a_pVM) ((a_pVM)->fHMEnabled)
|
---|
120 | #endif
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Checks whether raw-mode context is required for any purpose.
|
---|
124 | *
|
---|
125 | * @retval @c true if required either by raw-mode itself or by HM for doing
|
---|
126 | * switching the cpu to 64-bit mode.
|
---|
127 | * @retval @c false if not required.
|
---|
128 | *
|
---|
129 | * @param a_pVM The cross context VM structure.
|
---|
130 | * @internal
|
---|
131 | */
|
---|
132 | #if HC_ARCH_BITS == 64
|
---|
133 | # define HMIsRawModeCtxNeeded(a_pVM) (!HMIsEnabled(a_pVM))
|
---|
134 | #else
|
---|
135 | # define HMIsRawModeCtxNeeded(a_pVM) (!HMIsEnabled(a_pVM) || (a_pVM)->fHMNeedRawModeCtx)
|
---|
136 | #endif
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Check if the current CPU state is valid for emulating IO blocks in the recompiler
|
---|
140 | *
|
---|
141 | * @returns boolean
|
---|
142 | * @param a_pVCpu Pointer to the shared virtual CPU structure.
|
---|
143 | * @internal
|
---|
144 | */
|
---|
145 | #define HMCanEmulateIoBlock(a_pVCpu) (!CPUMIsGuestInPagedProtectedMode(a_pVCpu))
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Check if the current CPU state is valid for emulating IO blocks in the recompiler
|
---|
149 | *
|
---|
150 | * @returns boolean
|
---|
151 | * @param a_pCtx Pointer to the CPU context (within PVM).
|
---|
152 | * @internal
|
---|
153 | */
|
---|
154 | #define HMCanEmulateIoBlockEx(a_pCtx) (!CPUMIsGuestInPagedProtectedModeEx(a_pCtx))
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Checks whether we're in the special hardware virtualization context.
|
---|
158 | * @returns true / false.
|
---|
159 | * @param a_pVCpu The caller's cross context virtual CPU structure.
|
---|
160 | * @thread EMT
|
---|
161 | */
|
---|
162 | #ifdef IN_RING0
|
---|
163 | # define HMIsInHwVirtCtx(a_pVCpu) (VMCPU_GET_STATE(a_pVCpu) == VMCPUSTATE_STARTED_HM)
|
---|
164 | #else
|
---|
165 | # define HMIsInHwVirtCtx(a_pVCpu) (false)
|
---|
166 | #endif
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Checks whether we're in the special hardware virtualization context and we
|
---|
170 | * cannot perform long jump without guru meditating and possibly messing up the
|
---|
171 | * host and/or guest state.
|
---|
172 | *
|
---|
173 | * This is after we've turned interrupts off and such.
|
---|
174 | *
|
---|
175 | * @returns true / false.
|
---|
176 | * @param a_pVCpu The caller's cross context virtual CPU structure.
|
---|
177 | * @thread EMT
|
---|
178 | */
|
---|
179 | #ifdef IN_RING0
|
---|
180 | # define HMIsInHwVirtNoLongJmpCtx(a_pVCpu) (VMCPU_GET_STATE(a_pVCpu) == VMCPUSTATE_STARTED_EXEC)
|
---|
181 | #else
|
---|
182 | # define HMIsInHwVirtNoLongJmpCtx(a_pVCpu) (false)
|
---|
183 | #endif
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * 64-bit raw-mode (intermediate memory context) operations.
|
---|
187 | *
|
---|
188 | * These are special hypervisor eip values used when running 64-bit guests on
|
---|
189 | * 32-bit hosts. Each operation corresponds to a routine.
|
---|
190 | *
|
---|
191 | * @note Duplicated in the assembly code!
|
---|
192 | */
|
---|
193 | typedef enum HM64ON32OP
|
---|
194 | {
|
---|
195 | HM64ON32OP_INVALID = 0,
|
---|
196 | HM64ON32OP_VMXRCStartVM64,
|
---|
197 | HM64ON32OP_SVMRCVMRun64,
|
---|
198 | HM64ON32OP_HMRCSaveGuestFPU64,
|
---|
199 | HM64ON32OP_HMRCSaveGuestDebug64,
|
---|
200 | HM64ON32OP_HMRCTestSwitcher64,
|
---|
201 | HM64ON32OP_END,
|
---|
202 | HM64ON32OP_32BIT_HACK = 0x7fffffff
|
---|
203 | } HM64ON32OP;
|
---|
204 |
|
---|
205 | VMMDECL(bool) HMIsEnabledNotMacro(PVM pVM);
|
---|
206 | VMM_INT_DECL(int) HMInvalidatePage(PVMCPU pVCpu, RTGCPTR GCVirt);
|
---|
207 | VMM_INT_DECL(bool) HMHasPendingIrq(PVM pVM);
|
---|
208 | VMM_INT_DECL(PX86PDPE) HMGetPaePdpes(PVMCPU pVCpu);
|
---|
209 | VMM_INT_DECL(int) HMAmdIsSubjectToErratum170(uint32_t *pu32Family, uint32_t *pu32Model, uint32_t *pu32Stepping);
|
---|
210 | VMM_INT_DECL(bool) HMSetSingleInstruction(PVMCPU pVCpu, bool fEnable);
|
---|
211 |
|
---|
212 | #ifndef IN_RC
|
---|
213 | VMM_INT_DECL(int) HMFlushTLB(PVMCPU pVCpu);
|
---|
214 | VMM_INT_DECL(int) HMFlushTLBOnAllVCpus(PVM pVM);
|
---|
215 | VMM_INT_DECL(int) HMInvalidatePageOnAllVCpus(PVM pVM, RTGCPTR GCVirt);
|
---|
216 | VMM_INT_DECL(int) HMInvalidatePhysPage(PVM pVM, RTGCPHYS GCPhys);
|
---|
217 | VMM_INT_DECL(bool) HMIsNestedPagingActive(PVM pVM);
|
---|
218 | VMM_INT_DECL(PGMMODE) HMGetShwPagingMode(PVM pVM);
|
---|
219 | #else /* Nops in RC: */
|
---|
220 | # define HMFlushTLB(pVCpu) do { } while (0)
|
---|
221 | # define HMIsNestedPagingActive(pVM) false
|
---|
222 | # define HMFlushTLBOnAllVCpus(pVM) do { } while (0)
|
---|
223 | #endif
|
---|
224 |
|
---|
225 | #ifdef IN_RING0
|
---|
226 | /** @defgroup grp_hm_r0 The VM Hardware Manager API
|
---|
227 | * @ingroup grp_hm
|
---|
228 | * @{
|
---|
229 | */
|
---|
230 | VMMR0_INT_DECL(int) HMR0Init(void);
|
---|
231 | VMMR0_INT_DECL(int) HMR0Term(void);
|
---|
232 | VMMR0_INT_DECL(int) HMR0InitVM(PVM pVM);
|
---|
233 | VMMR0_INT_DECL(int) HMR0TermVM(PVM pVM);
|
---|
234 | VMMR0_INT_DECL(int) HMR0EnableAllCpus(PVM pVM);
|
---|
235 | VMMR0_INT_DECL(int) HMR0EnterSwitcher(PVM pVM, VMMSWITCHER enmSwitcher, bool *pfVTxDisabled);
|
---|
236 | VMMR0_INT_DECL(void) HMR0LeaveSwitcher(PVM pVM, bool fVTxDisabled);
|
---|
237 |
|
---|
238 | VMMR0_INT_DECL(void) HMR0SavePendingIOPortWrite(PVMCPU pVCpu, RTGCPTR GCPtrRip, RTGCPTR GCPtrRipNext,
|
---|
239 | unsigned uPort, unsigned uAndVal, unsigned cbSize);
|
---|
240 | VMMR0_INT_DECL(void) HMR0SavePendingIOPortRead(PVMCPU pVCpu, RTGCPTR GCPtrRip, RTGCPTR GCPtrRipNext,
|
---|
241 | unsigned uPort, unsigned uAndVal, unsigned cbSize);
|
---|
242 |
|
---|
243 | /** @} */
|
---|
244 | #endif /* IN_RING0 */
|
---|
245 |
|
---|
246 |
|
---|
247 | #ifdef IN_RING3
|
---|
248 | /** @defgroup grp_hm_r3 The VM Hardware Manager API
|
---|
249 | * @ingroup grp_hm
|
---|
250 | * @{
|
---|
251 | */
|
---|
252 | VMMR3DECL(bool) HMR3IsEnabled(PUVM pUVM);
|
---|
253 | VMMR3DECL(bool) HMR3IsNestedPagingActive(PUVM pUVM);
|
---|
254 | VMMR3DECL(bool) HMR3IsVpidActive(PUVM pVUM);
|
---|
255 | VMMR3DECL(bool) HMR3IsUXActive(PUVM pVUM);
|
---|
256 | VMMR3DECL(bool) HMR3IsSvmEnabled(PUVM pUVM);
|
---|
257 | VMMR3DECL(bool) HMR3IsVmxEnabled(PUVM pUVM);
|
---|
258 |
|
---|
259 | VMMR3_INT_DECL(bool) HMR3IsEventPending(PVMCPU pVCpu);
|
---|
260 | VMMR3_INT_DECL(int) HMR3Init(PVM pVM);
|
---|
261 | VMMR3_INT_DECL(int) HMR3InitCompleted(PVM pVM, VMINITCOMPLETED enmWhat);
|
---|
262 | VMMR3_INT_DECL(void) HMR3Relocate(PVM pVM);
|
---|
263 | VMMR3_INT_DECL(int) HMR3Term(PVM pVM);
|
---|
264 | VMMR3_INT_DECL(void) HMR3Reset(PVM pVM);
|
---|
265 | VMMR3_INT_DECL(void) HMR3ResetCpu(PVMCPU pVCpu);
|
---|
266 | VMMR3_INT_DECL(void) HMR3CheckError(PVM pVM, int iStatusCode);
|
---|
267 | VMMR3DECL(bool) HMR3CanExecuteGuest(PVM pVM, PCPUMCTX pCtx);
|
---|
268 | VMMR3_INT_DECL(void) HMR3NotifyScheduled(PVMCPU pVCpu);
|
---|
269 | VMMR3_INT_DECL(void) HMR3NotifyEmulated(PVMCPU pVCpu);
|
---|
270 | VMMR3_INT_DECL(bool) HMR3IsActive(PVMCPU pVCpu);
|
---|
271 | VMMR3_INT_DECL(void) HMR3PagingModeChanged(PVM pVM, PVMCPU pVCpu, PGMMODE enmShadowMode, PGMMODE enmGuestMode);
|
---|
272 | VMMR3_INT_DECL(int) HMR3EmulateIoBlock(PVM pVM, PCPUMCTX pCtx);
|
---|
273 | VMMR3_INT_DECL(VBOXSTRICTRC) HMR3RestartPendingIOInstr(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx);
|
---|
274 | VMMR3_INT_DECL(int) HMR3EnablePatching(PVM pVM, RTGCPTR pPatchMem, unsigned cbPatchMem);
|
---|
275 | VMMR3_INT_DECL(int) HMR3DisablePatching(PVM pVM, RTGCPTR pPatchMem, unsigned cbPatchMem);
|
---|
276 | VMMR3_INT_DECL(int) HMR3PatchTprInstr(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx);
|
---|
277 | VMMR3_INT_DECL(bool) HMR3IsRescheduleRequired(PVM pVM, PCPUMCTX pCtx);
|
---|
278 | VMMR3_INT_DECL(bool) HMR3IsVmxPreemptionTimerUsed(PVM pVM);
|
---|
279 |
|
---|
280 | /** @} */
|
---|
281 | #endif /* IN_RING3 */
|
---|
282 |
|
---|
283 | #ifdef IN_RING0
|
---|
284 | /** @addtogroup grp_hm_r0
|
---|
285 | * @{
|
---|
286 | */
|
---|
287 | /** Disables preemption if required. */
|
---|
288 | # define HM_DISABLE_PREEMPT_IF_NEEDED() \
|
---|
289 | RTTHREADPREEMPTSTATE PreemptStateInternal = RTTHREADPREEMPTSTATE_INITIALIZER; \
|
---|
290 | bool fPreemptDisabledInternal = false; \
|
---|
291 | if (RTThreadPreemptIsEnabled(NIL_RTTHREAD)) \
|
---|
292 | { \
|
---|
293 | Assert(VMMR0ThreadCtxHooksAreRegistered(pVCpu)); \
|
---|
294 | RTThreadPreemptDisable(&PreemptStateInternal); \
|
---|
295 | fPreemptDisabledInternal = true; \
|
---|
296 | }
|
---|
297 |
|
---|
298 | /** Restores preemption if previously disabled by HM_DISABLE_PREEMPT(). */
|
---|
299 | # define HM_RESTORE_PREEMPT_IF_NEEDED() \
|
---|
300 | do \
|
---|
301 | { \
|
---|
302 | if (fPreemptDisabledInternal) \
|
---|
303 | RTThreadPreemptRestore(&PreemptStateInternal); \
|
---|
304 | } while (0)
|
---|
305 |
|
---|
306 | VMMR0_INT_DECL(int) HMR0SetupVM(PVM pVM);
|
---|
307 | VMMR0_INT_DECL(int) HMR0RunGuestCode(PVM pVM, PVMCPU pVCpu);
|
---|
308 | VMMR0_INT_DECL(int) HMR0Enter(PVM pVM, PVMCPU pVCpu);
|
---|
309 | VMMR0_INT_DECL(int) HMR0Leave(PVM pVM, PVMCPU pVCpu);
|
---|
310 | VMMR0_INT_DECL(int) HMR0EnterCpu(PVMCPU pVCpu);
|
---|
311 | VMMR0_INT_DECL(int) HMR0LeaveCpu(PVMCPU pVCpu);
|
---|
312 | VMMR0_INT_DECL(void) HMR0ThreadCtxCallback(RTTHREADCTXEVENT enmEvent, void *pvUser);
|
---|
313 | VMMR0_INT_DECL(bool) HMR0SuspendPending(void);
|
---|
314 |
|
---|
315 | # if HC_ARCH_BITS == 32 && defined(VBOX_WITH_64_BITS_GUESTS)
|
---|
316 | VMMR0_INT_DECL(int) HMR0SaveFPUState(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx);
|
---|
317 | VMMR0_INT_DECL(int) HMR0SaveDebugState(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx);
|
---|
318 | VMMR0_INT_DECL(int) HMR0TestSwitcher3264(PVM pVM);
|
---|
319 | # endif
|
---|
320 |
|
---|
321 | /** @} */
|
---|
322 | #endif /* IN_RING0 */
|
---|
323 |
|
---|
324 |
|
---|
325 | /** @} */
|
---|
326 | RT_C_DECLS_END
|
---|
327 |
|
---|
328 |
|
---|
329 | #endif
|
---|
330 |
|
---|