VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/IEMAllCImplVmxInstr.cpp.h@ 93291

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

VMM/IEM: Nested VMX: bugref:10092 Nit.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 366.9 KB
Line 
1/* $Id: IEMAllCImplVmxInstr.cpp.h 93291 2022-01-18 05:32:42Z vboxsync $ */
2/** @file
3 * IEM - VT-x instruction implementation.
4 */
5
6/*
7 * Copyright (C) 2011-2022 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
19/*********************************************************************************************************************************
20* Defined Constants And Macros *
21*********************************************************************************************************************************/
22#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
23/**
24 * Gets the ModR/M, SIB and displacement byte(s) from decoded opcodes given their
25 * relative offsets.
26 */
27# ifdef IEM_WITH_CODE_TLB
28# define IEM_MODRM_GET_U8(a_pVCpu, a_bModRm, a_offModRm) do { } while (0)
29# define IEM_SIB_GET_U8(a_pVCpu, a_bSib, a_offSib) do { } while (0)
30# define IEM_DISP_GET_U16(a_pVCpu, a_u16Disp, a_offDisp) do { } while (0)
31# define IEM_DISP_GET_S8_SX_U16(a_pVCpu, a_u16Disp, a_offDisp) do { } while (0)
32# define IEM_DISP_GET_U32(a_pVCpu, a_u32Disp, a_offDisp) do { } while (0)
33# define IEM_DISP_GET_S8_SX_U32(a_pVCpu, a_u32Disp, a_offDisp) do { } while (0)
34# define IEM_DISP_GET_S32_SX_U64(a_pVCpu, a_u64Disp, a_offDisp) do { } while (0)
35# define IEM_DISP_GET_S8_SX_U64(a_pVCpu, a_u64Disp, a_offDisp) do { } while (0)
36# error "Implement me: Getting ModR/M, SIB, displacement needs to work even when instruction crosses a page boundary."
37# else /* !IEM_WITH_CODE_TLB */
38# define IEM_MODRM_GET_U8(a_pVCpu, a_bModRm, a_offModRm) \
39 do \
40 { \
41 Assert((a_offModRm) < (a_pVCpu)->iem.s.cbOpcode); \
42 (a_bModRm) = (a_pVCpu)->iem.s.abOpcode[(a_offModRm)]; \
43 } while (0)
44
45# define IEM_SIB_GET_U8(a_pVCpu, a_bSib, a_offSib) IEM_MODRM_GET_U8(a_pVCpu, a_bSib, a_offSib)
46
47# define IEM_DISP_GET_U16(a_pVCpu, a_u16Disp, a_offDisp) \
48 do \
49 { \
50 Assert((a_offDisp) + 1 < (a_pVCpu)->iem.s.cbOpcode); \
51 uint8_t const bTmpLo = (a_pVCpu)->iem.s.abOpcode[(a_offDisp)]; \
52 uint8_t const bTmpHi = (a_pVCpu)->iem.s.abOpcode[(a_offDisp) + 1]; \
53 (a_u16Disp) = RT_MAKE_U16(bTmpLo, bTmpHi); \
54 } while (0)
55
56# define IEM_DISP_GET_S8_SX_U16(a_pVCpu, a_u16Disp, a_offDisp) \
57 do \
58 { \
59 Assert((a_offDisp) < (a_pVCpu)->iem.s.cbOpcode); \
60 (a_u16Disp) = (int8_t)((a_pVCpu)->iem.s.abOpcode[(a_offDisp)]); \
61 } while (0)
62
63# define IEM_DISP_GET_U32(a_pVCpu, a_u32Disp, a_offDisp) \
64 do \
65 { \
66 Assert((a_offDisp) + 3 < (a_pVCpu)->iem.s.cbOpcode); \
67 uint8_t const bTmp0 = (a_pVCpu)->iem.s.abOpcode[(a_offDisp)]; \
68 uint8_t const bTmp1 = (a_pVCpu)->iem.s.abOpcode[(a_offDisp) + 1]; \
69 uint8_t const bTmp2 = (a_pVCpu)->iem.s.abOpcode[(a_offDisp) + 2]; \
70 uint8_t const bTmp3 = (a_pVCpu)->iem.s.abOpcode[(a_offDisp) + 3]; \
71 (a_u32Disp) = RT_MAKE_U32_FROM_U8(bTmp0, bTmp1, bTmp2, bTmp3); \
72 } while (0)
73
74# define IEM_DISP_GET_S8_SX_U32(a_pVCpu, a_u32Disp, a_offDisp) \
75 do \
76 { \
77 Assert((a_offDisp) + 1 < (a_pVCpu)->iem.s.cbOpcode); \
78 (a_u32Disp) = (int8_t)((a_pVCpu)->iem.s.abOpcode[(a_offDisp)]); \
79 } while (0)
80
81# define IEM_DISP_GET_S8_SX_U64(a_pVCpu, a_u64Disp, a_offDisp) \
82 do \
83 { \
84 Assert((a_offDisp) + 1 < (a_pVCpu)->iem.s.cbOpcode); \
85 (a_u64Disp) = (int8_t)((a_pVCpu)->iem.s.abOpcode[(a_offDisp)]); \
86 } while (0)
87
88# define IEM_DISP_GET_S32_SX_U64(a_pVCpu, a_u64Disp, a_offDisp) \
89 do \
90 { \
91 Assert((a_offDisp) + 3 < (a_pVCpu)->iem.s.cbOpcode); \
92 uint8_t const bTmp0 = (a_pVCpu)->iem.s.abOpcode[(a_offDisp)]; \
93 uint8_t const bTmp1 = (a_pVCpu)->iem.s.abOpcode[(a_offDisp) + 1]; \
94 uint8_t const bTmp2 = (a_pVCpu)->iem.s.abOpcode[(a_offDisp) + 2]; \
95 uint8_t const bTmp3 = (a_pVCpu)->iem.s.abOpcode[(a_offDisp) + 3]; \
96 (a_u64Disp) = (int32_t)RT_MAKE_U32_FROM_U8(bTmp0, bTmp1, bTmp2, bTmp3); \
97 } while (0)
98# endif /* !IEM_WITH_CODE_TLB */
99
100/** Gets the guest-physical address of the shadows VMCS for the given VCPU. */
101# define IEM_VMX_GET_SHADOW_VMCS(a_pVCpu) ((a_pVCpu)->cpum.GstCtx.hwvirt.vmx.GCPhysShadowVmcs)
102
103/** Whether a shadow VMCS is present for the given VCPU. */
104# define IEM_VMX_HAS_SHADOW_VMCS(a_pVCpu) RT_BOOL(IEM_VMX_GET_SHADOW_VMCS(a_pVCpu) != NIL_RTGCPHYS)
105
106/** Gets the VMXON region pointer. */
107# define IEM_VMX_GET_VMXON_PTR(a_pVCpu) ((a_pVCpu)->cpum.GstCtx.hwvirt.vmx.GCPhysVmxon)
108
109/** Gets the guest-physical address of the current VMCS for the given VCPU. */
110# define IEM_VMX_GET_CURRENT_VMCS(a_pVCpu) ((a_pVCpu)->cpum.GstCtx.hwvirt.vmx.GCPhysVmcs)
111
112/** Whether a current VMCS is present for the given VCPU. */
113# define IEM_VMX_HAS_CURRENT_VMCS(a_pVCpu) RT_BOOL(IEM_VMX_GET_CURRENT_VMCS(a_pVCpu) != NIL_RTGCPHYS)
114
115/** Assigns the guest-physical address of the current VMCS for the given VCPU. */
116# define IEM_VMX_SET_CURRENT_VMCS(a_pVCpu, a_GCPhysVmcs) \
117 do \
118 { \
119 Assert((a_GCPhysVmcs) != NIL_RTGCPHYS); \
120 (a_pVCpu)->cpum.GstCtx.hwvirt.vmx.GCPhysVmcs = (a_GCPhysVmcs); \
121 } while (0)
122
123/** Clears any current VMCS for the given VCPU. */
124# define IEM_VMX_CLEAR_CURRENT_VMCS(a_pVCpu) \
125 do \
126 { \
127 (a_pVCpu)->cpum.GstCtx.hwvirt.vmx.GCPhysVmcs = NIL_RTGCPHYS; \
128 } while (0)
129
130/** Check for VMX instructions requiring to be in VMX operation.
131 * @note Any changes here, check if IEMOP_HLP_IN_VMX_OPERATION needs updating. */
132# define IEM_VMX_IN_VMX_OPERATION(a_pVCpu, a_szInstr, a_InsDiagPrefix) \
133 do \
134 { \
135 if (IEM_VMX_IS_ROOT_MODE(a_pVCpu)) \
136 { /* likely */ } \
137 else \
138 { \
139 Log((a_szInstr ": Not in VMX operation (root mode) -> #UD\n")); \
140 (a_pVCpu)->cpum.GstCtx.hwvirt.vmx.enmDiag = a_InsDiagPrefix##_VmxRoot; \
141 return iemRaiseUndefinedOpcode(a_pVCpu); \
142 } \
143 } while (0)
144
145/** Marks a VM-entry failure with a diagnostic reason, logs and returns. */
146# define IEM_VMX_VMENTRY_FAILED_RET(a_pVCpu, a_pszInstr, a_pszFailure, a_VmxDiag) \
147 do \
148 { \
149 LogRel(("%s: VM-entry failed! enmDiag=%u (%s) -> %s\n", (a_pszInstr), (a_VmxDiag), \
150 HMGetVmxDiagDesc(a_VmxDiag), (a_pszFailure))); \
151 (a_pVCpu)->cpum.GstCtx.hwvirt.vmx.enmDiag = (a_VmxDiag); \
152 return VERR_VMX_VMENTRY_FAILED; \
153 } while (0)
154
155/** Marks a VM-exit failure with a diagnostic reason and logs. */
156# define IEM_VMX_VMEXIT_FAILED(a_pVCpu, a_uExitReason, a_pszFailure, a_VmxDiag) \
157 do \
158 { \
159 LogRel(("VM-exit failed! uExitReason=%u enmDiag=%u (%s) -> %s\n", (a_uExitReason), (a_VmxDiag), \
160 HMGetVmxDiagDesc(a_VmxDiag), (a_pszFailure))); \
161 (a_pVCpu)->cpum.GstCtx.hwvirt.vmx.enmDiag = (a_VmxDiag); \
162 } while (0)
163
164/** Marks a VM-exit failure with a diagnostic reason, logs and returns. */
165# define IEM_VMX_VMEXIT_FAILED_RET(a_pVCpu, a_uExitReason, a_pszFailure, a_VmxDiag) \
166 do \
167 { \
168 IEM_VMX_VMEXIT_FAILED(a_pVCpu, a_uExitReason, a_pszFailure, a_VmxDiag); \
169 return VERR_VMX_VMEXIT_FAILED; \
170 } while (0)
171
172
173/*********************************************************************************************************************************
174* Global Variables *
175*********************************************************************************************************************************/
176/** @todo NSTVMX: The following VM-exit intercepts are pending:
177 * VMX_EXIT_IO_SMI
178 * VMX_EXIT_SMI
179 * VMX_EXIT_GETSEC
180 * VMX_EXIT_RSM
181 * VMX_EXIT_MONITOR (APIC access VM-exit caused by MONITOR pending)
182 * VMX_EXIT_ERR_MACHINE_CHECK (we never need to raise this?)
183 * VMX_EXIT_INVEPT
184 * VMX_EXIT_RDRAND
185 * VMX_EXIT_VMFUNC
186 * VMX_EXIT_ENCLS
187 * VMX_EXIT_RDSEED
188 * VMX_EXIT_PML_FULL
189 * VMX_EXIT_XSAVES
190 * VMX_EXIT_XRSTORS
191 */
192/**
193 * Map of VMCS field encodings to their virtual-VMCS structure offsets.
194 *
195 * The first array dimension is VMCS field encoding of Width OR'ed with Type and the
196 * second dimension is the Index, see VMXVMCSFIELD.
197 */
198uint16_t const g_aoffVmcsMap[16][VMX_V_VMCS_MAX_INDEX + 1] =
199{
200 /* VMX_VMCSFIELD_WIDTH_16BIT | VMX_VMCSFIELD_TYPE_CONTROL: */
201 {
202 /* 0 */ RT_UOFFSETOF(VMXVVMCS, u16Vpid),
203 /* 1 */ RT_UOFFSETOF(VMXVVMCS, u16PostIntNotifyVector),
204 /* 2 */ RT_UOFFSETOF(VMXVVMCS, u16EptpIndex),
205 /* 3-10 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
206 /* 11-18 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
207 /* 19-26 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
208 /* 27 */ UINT16_MAX,
209 },
210 /* VMX_VMCSFIELD_WIDTH_16BIT | VMX_VMCSFIELD_TYPE_VMEXIT_INFO: */
211 {
212 /* 0-7 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
213 /* 8-15 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
214 /* 16-23 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
215 /* 24-27 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX
216 },
217 /* VMX_VMCSFIELD_WIDTH_16BIT | VMX_VMCSFIELD_TYPE_GUEST_STATE: */
218 {
219 /* 0 */ RT_UOFFSETOF(VMXVVMCS, GuestEs),
220 /* 1 */ RT_UOFFSETOF(VMXVVMCS, GuestCs),
221 /* 2 */ RT_UOFFSETOF(VMXVVMCS, GuestSs),
222 /* 3 */ RT_UOFFSETOF(VMXVVMCS, GuestDs),
223 /* 4 */ RT_UOFFSETOF(VMXVVMCS, GuestFs),
224 /* 5 */ RT_UOFFSETOF(VMXVVMCS, GuestGs),
225 /* 6 */ RT_UOFFSETOF(VMXVVMCS, GuestLdtr),
226 /* 7 */ RT_UOFFSETOF(VMXVVMCS, GuestTr),
227 /* 8 */ RT_UOFFSETOF(VMXVVMCS, u16GuestIntStatus),
228 /* 9 */ RT_UOFFSETOF(VMXVVMCS, u16PmlIndex),
229 /* 10-17 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
230 /* 18-25 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
231 /* 26-27 */ UINT16_MAX, UINT16_MAX
232 },
233 /* VMX_VMCSFIELD_WIDTH_16BIT | VMX_VMCSFIELD_TYPE_HOST_STATE: */
234 {
235 /* 0 */ RT_UOFFSETOF(VMXVVMCS, HostEs),
236 /* 1 */ RT_UOFFSETOF(VMXVVMCS, HostCs),
237 /* 2 */ RT_UOFFSETOF(VMXVVMCS, HostSs),
238 /* 3 */ RT_UOFFSETOF(VMXVVMCS, HostDs),
239 /* 4 */ RT_UOFFSETOF(VMXVVMCS, HostFs),
240 /* 5 */ RT_UOFFSETOF(VMXVVMCS, HostGs),
241 /* 6 */ RT_UOFFSETOF(VMXVVMCS, HostTr),
242 /* 7-14 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
243 /* 15-22 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
244 /* 23-27 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX
245 },
246 /* VMX_VMCSFIELD_WIDTH_64BIT | VMX_VMCSFIELD_TYPE_CONTROL: */
247 {
248 /* 0 */ RT_UOFFSETOF(VMXVVMCS, u64AddrIoBitmapA),
249 /* 1 */ RT_UOFFSETOF(VMXVVMCS, u64AddrIoBitmapB),
250 /* 2 */ RT_UOFFSETOF(VMXVVMCS, u64AddrMsrBitmap),
251 /* 3 */ RT_UOFFSETOF(VMXVVMCS, u64AddrExitMsrStore),
252 /* 4 */ RT_UOFFSETOF(VMXVVMCS, u64AddrExitMsrLoad),
253 /* 5 */ RT_UOFFSETOF(VMXVVMCS, u64AddrEntryMsrLoad),
254 /* 6 */ RT_UOFFSETOF(VMXVVMCS, u64ExecVmcsPtr),
255 /* 7 */ RT_UOFFSETOF(VMXVVMCS, u64AddrPml),
256 /* 8 */ RT_UOFFSETOF(VMXVVMCS, u64TscOffset),
257 /* 9 */ RT_UOFFSETOF(VMXVVMCS, u64AddrVirtApic),
258 /* 10 */ RT_UOFFSETOF(VMXVVMCS, u64AddrApicAccess),
259 /* 11 */ RT_UOFFSETOF(VMXVVMCS, u64AddrPostedIntDesc),
260 /* 12 */ RT_UOFFSETOF(VMXVVMCS, u64VmFuncCtls),
261 /* 13 */ RT_UOFFSETOF(VMXVVMCS, u64EptPtr),
262 /* 14 */ RT_UOFFSETOF(VMXVVMCS, u64EoiExitBitmap0),
263 /* 15 */ RT_UOFFSETOF(VMXVVMCS, u64EoiExitBitmap1),
264 /* 16 */ RT_UOFFSETOF(VMXVVMCS, u64EoiExitBitmap2),
265 /* 17 */ RT_UOFFSETOF(VMXVVMCS, u64EoiExitBitmap3),
266 /* 18 */ RT_UOFFSETOF(VMXVVMCS, u64AddrEptpList),
267 /* 19 */ RT_UOFFSETOF(VMXVVMCS, u64AddrVmreadBitmap),
268 /* 20 */ RT_UOFFSETOF(VMXVVMCS, u64AddrVmwriteBitmap),
269 /* 21 */ RT_UOFFSETOF(VMXVVMCS, u64AddrXcptVeInfo),
270 /* 22 */ RT_UOFFSETOF(VMXVVMCS, u64XssExitBitmap),
271 /* 23 */ RT_UOFFSETOF(VMXVVMCS, u64EnclsExitBitmap),
272 /* 24 */ RT_UOFFSETOF(VMXVVMCS, u64SppTablePtr),
273 /* 25 */ RT_UOFFSETOF(VMXVVMCS, u64TscMultiplier),
274 /* 26 */ RT_UOFFSETOF(VMXVVMCS, u64ProcCtls3),
275 /* 27 */ RT_UOFFSETOF(VMXVVMCS, u64EnclvExitBitmap)
276 },
277 /* VMX_VMCSFIELD_WIDTH_64BIT | VMX_VMCSFIELD_TYPE_VMEXIT_INFO: */
278 {
279 /* 0 */ RT_UOFFSETOF(VMXVVMCS, u64RoGuestPhysAddr),
280 /* 1-8 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
281 /* 9-16 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
282 /* 17-24 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
283 /* 25-27 */ UINT16_MAX, UINT16_MAX, UINT16_MAX
284 },
285 /* VMX_VMCSFIELD_WIDTH_64BIT | VMX_VMCSFIELD_TYPE_GUEST_STATE: */
286 {
287 /* 0 */ RT_UOFFSETOF(VMXVVMCS, u64VmcsLinkPtr),
288 /* 1 */ RT_UOFFSETOF(VMXVVMCS, u64GuestDebugCtlMsr),
289 /* 2 */ RT_UOFFSETOF(VMXVVMCS, u64GuestPatMsr),
290 /* 3 */ RT_UOFFSETOF(VMXVVMCS, u64GuestEferMsr),
291 /* 4 */ RT_UOFFSETOF(VMXVVMCS, u64GuestPerfGlobalCtlMsr),
292 /* 5 */ RT_UOFFSETOF(VMXVVMCS, u64GuestPdpte0),
293 /* 6 */ RT_UOFFSETOF(VMXVVMCS, u64GuestPdpte1),
294 /* 7 */ RT_UOFFSETOF(VMXVVMCS, u64GuestPdpte2),
295 /* 8 */ RT_UOFFSETOF(VMXVVMCS, u64GuestPdpte3),
296 /* 9 */ RT_UOFFSETOF(VMXVVMCS, u64GuestBndcfgsMsr),
297 /* 10 */ RT_UOFFSETOF(VMXVVMCS, u64GuestRtitCtlMsr),
298 /* 11 */ UINT16_MAX,
299 /* 12 */ RT_UOFFSETOF(VMXVVMCS, u64GuestPkrsMsr),
300 /* 13-20 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
301 /* 21-27 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX
302 },
303 /* VMX_VMCSFIELD_WIDTH_64BIT | VMX_VMCSFIELD_TYPE_HOST_STATE: */
304 {
305 /* 0 */ RT_UOFFSETOF(VMXVVMCS, u64HostPatMsr),
306 /* 1 */ RT_UOFFSETOF(VMXVVMCS, u64HostEferMsr),
307 /* 2 */ RT_UOFFSETOF(VMXVVMCS, u64HostPerfGlobalCtlMsr),
308 /* 3 */ RT_UOFFSETOF(VMXVVMCS, u64HostPkrsMsr),
309 /* 4-11 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
310 /* 12-19 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
311 /* 20-27 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX
312 },
313 /* VMX_VMCSFIELD_WIDTH_32BIT | VMX_VMCSFIELD_TYPE_CONTROL: */
314 {
315 /* 0 */ RT_UOFFSETOF(VMXVVMCS, u32PinCtls),
316 /* 1 */ RT_UOFFSETOF(VMXVVMCS, u32ProcCtls),
317 /* 2 */ RT_UOFFSETOF(VMXVVMCS, u32XcptBitmap),
318 /* 3 */ RT_UOFFSETOF(VMXVVMCS, u32XcptPFMask),
319 /* 4 */ RT_UOFFSETOF(VMXVVMCS, u32XcptPFMatch),
320 /* 5 */ RT_UOFFSETOF(VMXVVMCS, u32Cr3TargetCount),
321 /* 6 */ RT_UOFFSETOF(VMXVVMCS, u32ExitCtls),
322 /* 7 */ RT_UOFFSETOF(VMXVVMCS, u32ExitMsrStoreCount),
323 /* 8 */ RT_UOFFSETOF(VMXVVMCS, u32ExitMsrLoadCount),
324 /* 9 */ RT_UOFFSETOF(VMXVVMCS, u32EntryCtls),
325 /* 10 */ RT_UOFFSETOF(VMXVVMCS, u32EntryMsrLoadCount),
326 /* 11 */ RT_UOFFSETOF(VMXVVMCS, u32EntryIntInfo),
327 /* 12 */ RT_UOFFSETOF(VMXVVMCS, u32EntryXcptErrCode),
328 /* 13 */ RT_UOFFSETOF(VMXVVMCS, u32EntryInstrLen),
329 /* 14 */ RT_UOFFSETOF(VMXVVMCS, u32TprThreshold),
330 /* 15 */ RT_UOFFSETOF(VMXVVMCS, u32ProcCtls2),
331 /* 16 */ RT_UOFFSETOF(VMXVVMCS, u32PleGap),
332 /* 17 */ RT_UOFFSETOF(VMXVVMCS, u32PleWindow),
333 /* 18-25 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
334 /* 26-27 */ UINT16_MAX, UINT16_MAX
335 },
336 /* VMX_VMCSFIELD_WIDTH_32BIT | VMX_VMCSFIELD_TYPE_VMEXIT_INFO: */
337 {
338 /* 0 */ RT_UOFFSETOF(VMXVVMCS, u32RoVmInstrError),
339 /* 1 */ RT_UOFFSETOF(VMXVVMCS, u32RoExitReason),
340 /* 2 */ RT_UOFFSETOF(VMXVVMCS, u32RoExitIntInfo),
341 /* 3 */ RT_UOFFSETOF(VMXVVMCS, u32RoExitIntErrCode),
342 /* 4 */ RT_UOFFSETOF(VMXVVMCS, u32RoIdtVectoringInfo),
343 /* 5 */ RT_UOFFSETOF(VMXVVMCS, u32RoIdtVectoringErrCode),
344 /* 6 */ RT_UOFFSETOF(VMXVVMCS, u32RoExitInstrLen),
345 /* 7 */ RT_UOFFSETOF(VMXVVMCS, u32RoExitInstrInfo),
346 /* 8-15 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
347 /* 16-23 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
348 /* 24-27 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX
349 },
350 /* VMX_VMCSFIELD_WIDTH_32BIT | VMX_VMCSFIELD_TYPE_GUEST_STATE: */
351 {
352 /* 0 */ RT_UOFFSETOF(VMXVVMCS, u32GuestEsLimit),
353 /* 1 */ RT_UOFFSETOF(VMXVVMCS, u32GuestCsLimit),
354 /* 2 */ RT_UOFFSETOF(VMXVVMCS, u32GuestSsLimit),
355 /* 3 */ RT_UOFFSETOF(VMXVVMCS, u32GuestDsLimit),
356 /* 4 */ RT_UOFFSETOF(VMXVVMCS, u32GuestFsLimit),
357 /* 5 */ RT_UOFFSETOF(VMXVVMCS, u32GuestGsLimit),
358 /* 6 */ RT_UOFFSETOF(VMXVVMCS, u32GuestLdtrLimit),
359 /* 7 */ RT_UOFFSETOF(VMXVVMCS, u32GuestTrLimit),
360 /* 8 */ RT_UOFFSETOF(VMXVVMCS, u32GuestGdtrLimit),
361 /* 9 */ RT_UOFFSETOF(VMXVVMCS, u32GuestIdtrLimit),
362 /* 10 */ RT_UOFFSETOF(VMXVVMCS, u32GuestEsAttr),
363 /* 11 */ RT_UOFFSETOF(VMXVVMCS, u32GuestCsAttr),
364 /* 12 */ RT_UOFFSETOF(VMXVVMCS, u32GuestSsAttr),
365 /* 13 */ RT_UOFFSETOF(VMXVVMCS, u32GuestDsAttr),
366 /* 14 */ RT_UOFFSETOF(VMXVVMCS, u32GuestFsAttr),
367 /* 15 */ RT_UOFFSETOF(VMXVVMCS, u32GuestGsAttr),
368 /* 16 */ RT_UOFFSETOF(VMXVVMCS, u32GuestLdtrAttr),
369 /* 17 */ RT_UOFFSETOF(VMXVVMCS, u32GuestTrAttr),
370 /* 18 */ RT_UOFFSETOF(VMXVVMCS, u32GuestIntrState),
371 /* 19 */ RT_UOFFSETOF(VMXVVMCS, u32GuestActivityState),
372 /* 20 */ RT_UOFFSETOF(VMXVVMCS, u32GuestSmBase),
373 /* 21 */ RT_UOFFSETOF(VMXVVMCS, u32GuestSysenterCS),
374 /* 22 */ UINT16_MAX,
375 /* 23 */ RT_UOFFSETOF(VMXVVMCS, u32PreemptTimer),
376 /* 24-27 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX
377 },
378 /* VMX_VMCSFIELD_WIDTH_32BIT | VMX_VMCSFIELD_TYPE_HOST_STATE: */
379 {
380 /* 0 */ RT_UOFFSETOF(VMXVVMCS, u32HostSysenterCs),
381 /* 1-8 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
382 /* 9-16 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
383 /* 17-24 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
384 /* 25-27 */ UINT16_MAX, UINT16_MAX, UINT16_MAX
385 },
386 /* VMX_VMCSFIELD_WIDTH_NATURAL | VMX_VMCSFIELD_TYPE_CONTROL: */
387 {
388 /* 0 */ RT_UOFFSETOF(VMXVVMCS, u64Cr0Mask),
389 /* 1 */ RT_UOFFSETOF(VMXVVMCS, u64Cr4Mask),
390 /* 2 */ RT_UOFFSETOF(VMXVVMCS, u64Cr0ReadShadow),
391 /* 3 */ RT_UOFFSETOF(VMXVVMCS, u64Cr4ReadShadow),
392 /* 4 */ RT_UOFFSETOF(VMXVVMCS, u64Cr3Target0),
393 /* 5 */ RT_UOFFSETOF(VMXVVMCS, u64Cr3Target1),
394 /* 6 */ RT_UOFFSETOF(VMXVVMCS, u64Cr3Target2),
395 /* 7 */ RT_UOFFSETOF(VMXVVMCS, u64Cr3Target3),
396 /* 8-15 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
397 /* 16-23 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
398 /* 24-27 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX
399 },
400 /* VMX_VMCSFIELD_WIDTH_NATURAL | VMX_VMCSFIELD_TYPE_VMEXIT_INFO: */
401 {
402 /* 0 */ RT_UOFFSETOF(VMXVVMCS, u64RoExitQual),
403 /* 1 */ RT_UOFFSETOF(VMXVVMCS, u64RoIoRcx),
404 /* 2 */ RT_UOFFSETOF(VMXVVMCS, u64RoIoRsi),
405 /* 3 */ RT_UOFFSETOF(VMXVVMCS, u64RoIoRdi),
406 /* 4 */ RT_UOFFSETOF(VMXVVMCS, u64RoIoRip),
407 /* 5 */ RT_UOFFSETOF(VMXVVMCS, u64RoGuestLinearAddr),
408 /* 6-13 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
409 /* 14-21 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
410 /* 22-27 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX
411 },
412 /* VMX_VMCSFIELD_WIDTH_NATURAL | VMX_VMCSFIELD_TYPE_GUEST_STATE: */
413 {
414 /* 0 */ RT_UOFFSETOF(VMXVVMCS, u64GuestCr0),
415 /* 1 */ RT_UOFFSETOF(VMXVVMCS, u64GuestCr3),
416 /* 2 */ RT_UOFFSETOF(VMXVVMCS, u64GuestCr4),
417 /* 3 */ RT_UOFFSETOF(VMXVVMCS, u64GuestEsBase),
418 /* 4 */ RT_UOFFSETOF(VMXVVMCS, u64GuestCsBase),
419 /* 5 */ RT_UOFFSETOF(VMXVVMCS, u64GuestSsBase),
420 /* 6 */ RT_UOFFSETOF(VMXVVMCS, u64GuestDsBase),
421 /* 7 */ RT_UOFFSETOF(VMXVVMCS, u64GuestFsBase),
422 /* 8 */ RT_UOFFSETOF(VMXVVMCS, u64GuestGsBase),
423 /* 9 */ RT_UOFFSETOF(VMXVVMCS, u64GuestLdtrBase),
424 /* 10 */ RT_UOFFSETOF(VMXVVMCS, u64GuestTrBase),
425 /* 11 */ RT_UOFFSETOF(VMXVVMCS, u64GuestGdtrBase),
426 /* 12 */ RT_UOFFSETOF(VMXVVMCS, u64GuestIdtrBase),
427 /* 13 */ RT_UOFFSETOF(VMXVVMCS, u64GuestDr7),
428 /* 14 */ RT_UOFFSETOF(VMXVVMCS, u64GuestRsp),
429 /* 15 */ RT_UOFFSETOF(VMXVVMCS, u64GuestRip),
430 /* 16 */ RT_UOFFSETOF(VMXVVMCS, u64GuestRFlags),
431 /* 17 */ RT_UOFFSETOF(VMXVVMCS, u64GuestPendingDbgXcpts),
432 /* 18 */ RT_UOFFSETOF(VMXVVMCS, u64GuestSysenterEsp),
433 /* 19 */ RT_UOFFSETOF(VMXVVMCS, u64GuestSysenterEip),
434 /* 20 */ RT_UOFFSETOF(VMXVVMCS, u64GuestSCetMsr),
435 /* 21 */ RT_UOFFSETOF(VMXVVMCS, u64GuestSsp),
436 /* 22 */ RT_UOFFSETOF(VMXVVMCS, u64GuestIntrSspTableAddrMsr),
437 /* 23-27 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX
438 },
439 /* VMX_VMCSFIELD_WIDTH_NATURAL | VMX_VMCSFIELD_TYPE_HOST_STATE: */
440 {
441 /* 0 */ RT_UOFFSETOF(VMXVVMCS, u64HostCr0),
442 /* 1 */ RT_UOFFSETOF(VMXVVMCS, u64HostCr3),
443 /* 2 */ RT_UOFFSETOF(VMXVVMCS, u64HostCr4),
444 /* 3 */ RT_UOFFSETOF(VMXVVMCS, u64HostFsBase),
445 /* 4 */ RT_UOFFSETOF(VMXVVMCS, u64HostGsBase),
446 /* 5 */ RT_UOFFSETOF(VMXVVMCS, u64HostTrBase),
447 /* 6 */ RT_UOFFSETOF(VMXVVMCS, u64HostGdtrBase),
448 /* 7 */ RT_UOFFSETOF(VMXVVMCS, u64HostIdtrBase),
449 /* 8 */ RT_UOFFSETOF(VMXVVMCS, u64HostSysenterEsp),
450 /* 9 */ RT_UOFFSETOF(VMXVVMCS, u64HostSysenterEip),
451 /* 10 */ RT_UOFFSETOF(VMXVVMCS, u64HostRsp),
452 /* 11 */ RT_UOFFSETOF(VMXVVMCS, u64HostRip),
453 /* 12 */ RT_UOFFSETOF(VMXVVMCS, u64HostSCetMsr),
454 /* 13 */ RT_UOFFSETOF(VMXVVMCS, u64HostSsp),
455 /* 14 */ RT_UOFFSETOF(VMXVVMCS, u64HostIntrSspTableAddrMsr),
456 /* 15-22 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
457 /* 23-27 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX
458 }
459};
460
461
462/**
463 * Gets a host selector from the VMCS.
464 *
465 * @param pVmcs Pointer to the virtual VMCS.
466 * @param iSelReg The index of the segment register (X86_SREG_XXX).
467 */
468DECLINLINE(RTSEL) iemVmxVmcsGetHostSelReg(PCVMXVVMCS pVmcs, uint8_t iSegReg)
469{
470 Assert(iSegReg < X86_SREG_COUNT);
471 RTSEL HostSel;
472 uint8_t const uWidth = VMX_VMCSFIELD_WIDTH_16BIT;
473 uint8_t const uType = VMX_VMCSFIELD_TYPE_HOST_STATE;
474 uint8_t const uWidthType = (uWidth << 2) | uType;
475 uint8_t const uIndex = iSegReg + RT_BF_GET(VMX_VMCS16_HOST_ES_SEL, VMX_BF_VMCSFIELD_INDEX);
476 Assert(uIndex <= VMX_V_VMCS_MAX_INDEX);
477 uint16_t const offField = g_aoffVmcsMap[uWidthType][uIndex];
478 uint8_t const *pbVmcs = (uint8_t *)pVmcs;
479 uint8_t const *pbField = pbVmcs + offField;
480 HostSel = *(uint16_t *)pbField;
481 return HostSel;
482}
483
484
485/**
486 * Sets a guest segment register in the VMCS.
487 *
488 * @param pVmcs Pointer to the virtual VMCS.
489 * @param iSegReg The index of the segment register (X86_SREG_XXX).
490 * @param pSelReg Pointer to the segment register.
491 */
492IEM_STATIC void iemVmxVmcsSetGuestSegReg(PCVMXVVMCS pVmcs, uint8_t iSegReg, PCCPUMSELREG pSelReg)
493{
494 Assert(pSelReg);
495 Assert(iSegReg < X86_SREG_COUNT);
496
497 /* Selector. */
498 {
499 uint8_t const uWidth = VMX_VMCSFIELD_WIDTH_16BIT;
500 uint8_t const uType = VMX_VMCSFIELD_TYPE_GUEST_STATE;
501 uint8_t const uWidthType = (uWidth << 2) | uType;
502 uint8_t const uIndex = iSegReg + RT_BF_GET(VMX_VMCS16_GUEST_ES_SEL, VMX_BF_VMCSFIELD_INDEX);
503 Assert(uIndex <= VMX_V_VMCS_MAX_INDEX);
504 uint16_t const offField = g_aoffVmcsMap[uWidthType][uIndex];
505 uint8_t *pbVmcs = (uint8_t *)pVmcs;
506 uint8_t *pbField = pbVmcs + offField;
507 *(uint16_t *)pbField = pSelReg->Sel;
508 }
509
510 /* Limit. */
511 {
512 uint8_t const uWidth = VMX_VMCSFIELD_WIDTH_32BIT;
513 uint8_t const uType = VMX_VMCSFIELD_TYPE_GUEST_STATE;
514 uint8_t const uWidthType = (uWidth << 2) | uType;
515 uint8_t const uIndex = iSegReg + RT_BF_GET(VMX_VMCS32_GUEST_ES_LIMIT, VMX_BF_VMCSFIELD_INDEX);
516 Assert(uIndex <= VMX_V_VMCS_MAX_INDEX);
517 uint16_t const offField = g_aoffVmcsMap[uWidthType][uIndex];
518 uint8_t *pbVmcs = (uint8_t *)pVmcs;
519 uint8_t *pbField = pbVmcs + offField;
520 *(uint32_t *)pbField = pSelReg->u32Limit;
521 }
522
523 /* Base. */
524 {
525 uint8_t const uWidth = VMX_VMCSFIELD_WIDTH_NATURAL;
526 uint8_t const uType = VMX_VMCSFIELD_TYPE_GUEST_STATE;
527 uint8_t const uWidthType = (uWidth << 2) | uType;
528 uint8_t const uIndex = iSegReg + RT_BF_GET(VMX_VMCS_GUEST_ES_BASE, VMX_BF_VMCSFIELD_INDEX);
529 Assert(uIndex <= VMX_V_VMCS_MAX_INDEX);
530 uint16_t const offField = g_aoffVmcsMap[uWidthType][uIndex];
531 uint8_t const *pbVmcs = (uint8_t *)pVmcs;
532 uint8_t const *pbField = pbVmcs + offField;
533 *(uint64_t *)pbField = pSelReg->u64Base;
534 }
535
536 /* Attributes. */
537 {
538 uint32_t const fValidAttrMask = X86DESCATTR_TYPE | X86DESCATTR_DT | X86DESCATTR_DPL | X86DESCATTR_P
539 | X86DESCATTR_AVL | X86DESCATTR_L | X86DESCATTR_D | X86DESCATTR_G
540 | X86DESCATTR_UNUSABLE;
541 uint8_t const uWidth = VMX_VMCSFIELD_WIDTH_32BIT;
542 uint8_t const uType = VMX_VMCSFIELD_TYPE_GUEST_STATE;
543 uint8_t const uWidthType = (uWidth << 2) | uType;
544 uint8_t const uIndex = iSegReg + RT_BF_GET(VMX_VMCS32_GUEST_ES_ACCESS_RIGHTS, VMX_BF_VMCSFIELD_INDEX);
545 Assert(uIndex <= VMX_V_VMCS_MAX_INDEX);
546 uint16_t const offField = g_aoffVmcsMap[uWidthType][uIndex];
547 uint8_t *pbVmcs = (uint8_t *)pVmcs;
548 uint8_t *pbField = pbVmcs + offField;
549 *(uint32_t *)pbField = pSelReg->Attr.u & fValidAttrMask;
550 }
551}
552
553
554/**
555 * Gets a guest segment register from the VMCS.
556 *
557 * @returns VBox status code.
558 * @param pVmcs Pointer to the virtual VMCS.
559 * @param iSegReg The index of the segment register (X86_SREG_XXX).
560 * @param pSelReg Where to store the segment register (only updated when
561 * VINF_SUCCESS is returned).
562 *
563 * @remarks Warning! This does not validate the contents of the retrieved segment
564 * register.
565 */
566IEM_STATIC int iemVmxVmcsGetGuestSegReg(PCVMXVVMCS pVmcs, uint8_t iSegReg, PCPUMSELREG pSelReg)
567{
568 Assert(pSelReg);
569 Assert(iSegReg < X86_SREG_COUNT);
570
571 /* Selector. */
572 uint16_t u16Sel;
573 {
574 uint8_t const uWidth = VMX_VMCSFIELD_WIDTH_16BIT;
575 uint8_t const uType = VMX_VMCSFIELD_TYPE_GUEST_STATE;
576 uint8_t const uWidthType = (uWidth << 2) | uType;
577 uint8_t const uIndex = iSegReg + RT_BF_GET(VMX_VMCS16_GUEST_ES_SEL, VMX_BF_VMCSFIELD_INDEX);
578 AssertReturn(uIndex <= VMX_V_VMCS_MAX_INDEX, VERR_IEM_IPE_3);
579 uint16_t const offField = g_aoffVmcsMap[uWidthType][uIndex];
580 uint8_t const *pbVmcs = (uint8_t *)pVmcs;
581 uint8_t const *pbField = pbVmcs + offField;
582 u16Sel = *(uint16_t *)pbField;
583 }
584
585 /* Limit. */
586 uint32_t u32Limit;
587 {
588 uint8_t const uWidth = VMX_VMCSFIELD_WIDTH_32BIT;
589 uint8_t const uType = VMX_VMCSFIELD_TYPE_GUEST_STATE;
590 uint8_t const uWidthType = (uWidth << 2) | uType;
591 uint8_t const uIndex = iSegReg + RT_BF_GET(VMX_VMCS32_GUEST_ES_LIMIT, VMX_BF_VMCSFIELD_INDEX);
592 AssertReturn(uIndex <= VMX_V_VMCS_MAX_INDEX, VERR_IEM_IPE_3);
593 uint16_t const offField = g_aoffVmcsMap[uWidthType][uIndex];
594 uint8_t const *pbVmcs = (uint8_t *)pVmcs;
595 uint8_t const *pbField = pbVmcs + offField;
596 u32Limit = *(uint32_t *)pbField;
597 }
598
599 /* Base. */
600 uint64_t u64Base;
601 {
602 uint8_t const uWidth = VMX_VMCSFIELD_WIDTH_NATURAL;
603 uint8_t const uType = VMX_VMCSFIELD_TYPE_GUEST_STATE;
604 uint8_t const uWidthType = (uWidth << 2) | uType;
605 uint8_t const uIndex = iSegReg + RT_BF_GET(VMX_VMCS_GUEST_ES_BASE, VMX_BF_VMCSFIELD_INDEX);
606 AssertReturn(uIndex <= VMX_V_VMCS_MAX_INDEX, VERR_IEM_IPE_3);
607 uint16_t const offField = g_aoffVmcsMap[uWidthType][uIndex];
608 uint8_t const *pbVmcs = (uint8_t *)pVmcs;
609 uint8_t const *pbField = pbVmcs + offField;
610 u64Base = *(uint64_t *)pbField;
611 /** @todo NSTVMX: Should we zero out high bits here for 32-bit virtual CPUs? */
612 }
613
614 /* Attributes. */
615 uint32_t u32Attr;
616 {
617 uint8_t const uWidth = VMX_VMCSFIELD_WIDTH_32BIT;
618 uint8_t const uType = VMX_VMCSFIELD_TYPE_GUEST_STATE;
619 uint8_t const uWidthType = (uWidth << 2) | uType;
620 uint8_t const uIndex = iSegReg + RT_BF_GET(VMX_VMCS32_GUEST_ES_ACCESS_RIGHTS, VMX_BF_VMCSFIELD_INDEX);
621 AssertReturn(uIndex <= VMX_V_VMCS_MAX_INDEX, VERR_IEM_IPE_3);
622 uint16_t const offField = g_aoffVmcsMap[uWidthType][uIndex];
623 uint8_t const *pbVmcs = (uint8_t *)pVmcs;
624 uint8_t const *pbField = pbVmcs + offField;
625 u32Attr = *(uint32_t *)pbField;
626 }
627
628 pSelReg->Sel = u16Sel;
629 pSelReg->ValidSel = u16Sel;
630 pSelReg->fFlags = CPUMSELREG_FLAGS_VALID;
631 pSelReg->u32Limit = u32Limit;
632 pSelReg->u64Base = u64Base;
633 pSelReg->Attr.u = u32Attr;
634 return VINF_SUCCESS;
635}
636
637
638/**
639 * Converts an IEM exception event type to a VMX event type.
640 *
641 * @returns The VMX event type.
642 * @param uVector The interrupt / exception vector.
643 * @param fFlags The IEM event flag (see IEM_XCPT_FLAGS_XXX).
644 */
645DECLINLINE(uint8_t) iemVmxGetEventType(uint32_t uVector, uint32_t fFlags)
646{
647 /* Paranoia (callers may use these interchangeably). */
648 AssertCompile(VMX_EXIT_INT_INFO_TYPE_NMI == VMX_IDT_VECTORING_INFO_TYPE_NMI);
649 AssertCompile(VMX_EXIT_INT_INFO_TYPE_HW_XCPT == VMX_IDT_VECTORING_INFO_TYPE_HW_XCPT);
650 AssertCompile(VMX_EXIT_INT_INFO_TYPE_EXT_INT == VMX_IDT_VECTORING_INFO_TYPE_EXT_INT);
651 AssertCompile(VMX_EXIT_INT_INFO_TYPE_SW_XCPT == VMX_IDT_VECTORING_INFO_TYPE_SW_XCPT);
652 AssertCompile(VMX_EXIT_INT_INFO_TYPE_SW_INT == VMX_IDT_VECTORING_INFO_TYPE_SW_INT);
653 AssertCompile(VMX_EXIT_INT_INFO_TYPE_PRIV_SW_XCPT == VMX_IDT_VECTORING_INFO_TYPE_PRIV_SW_XCPT);
654 AssertCompile(VMX_EXIT_INT_INFO_TYPE_NMI == VMX_ENTRY_INT_INFO_TYPE_NMI);
655 AssertCompile(VMX_EXIT_INT_INFO_TYPE_HW_XCPT == VMX_ENTRY_INT_INFO_TYPE_HW_XCPT);
656 AssertCompile(VMX_EXIT_INT_INFO_TYPE_EXT_INT == VMX_ENTRY_INT_INFO_TYPE_EXT_INT);
657 AssertCompile(VMX_EXIT_INT_INFO_TYPE_SW_XCPT == VMX_ENTRY_INT_INFO_TYPE_SW_XCPT);
658 AssertCompile(VMX_EXIT_INT_INFO_TYPE_SW_INT == VMX_ENTRY_INT_INFO_TYPE_SW_INT);
659 AssertCompile(VMX_EXIT_INT_INFO_TYPE_PRIV_SW_XCPT == VMX_ENTRY_INT_INFO_TYPE_PRIV_SW_XCPT);
660
661 if (fFlags & IEM_XCPT_FLAGS_T_CPU_XCPT)
662 {
663 if (uVector == X86_XCPT_NMI)
664 return VMX_EXIT_INT_INFO_TYPE_NMI;
665 return VMX_EXIT_INT_INFO_TYPE_HW_XCPT;
666 }
667
668 if (fFlags & IEM_XCPT_FLAGS_T_SOFT_INT)
669 {
670 if (fFlags & (IEM_XCPT_FLAGS_BP_INSTR | IEM_XCPT_FLAGS_OF_INSTR))
671 return VMX_EXIT_INT_INFO_TYPE_SW_XCPT;
672 if (fFlags & IEM_XCPT_FLAGS_ICEBP_INSTR)
673 return VMX_EXIT_INT_INFO_TYPE_PRIV_SW_XCPT;
674 return VMX_EXIT_INT_INFO_TYPE_SW_INT;
675 }
676
677 Assert(fFlags & IEM_XCPT_FLAGS_T_EXT_INT);
678 return VMX_EXIT_INT_INFO_TYPE_EXT_INT;
679}
680
681
682/**
683 * Determines whether the guest is using PAE paging given the VMCS.
684 *
685 * @returns @c true if PAE paging mode is used, @c false otherwise.
686 * @param pVmcs Pointer to the virtual VMCS.
687 */
688DECL_FORCE_INLINE(bool) iemVmxVmcsIsGuestPaePagingEnabled(PCVMXVVMCS pVmcs)
689{
690 return ( !(pVmcs->u32EntryCtls & VMX_ENTRY_CTLS_IA32E_MODE_GUEST)
691 && (pVmcs->u64GuestCr4.u & X86_CR4_PAE)
692 && (pVmcs->u64GuestCr0.u & X86_CR0_PG));
693}
694
695
696/**
697 * Sets the Exit qualification VMCS field.
698 *
699 * @param pVCpu The cross context virtual CPU structure.
700 * @param u64ExitQual The Exit qualification.
701 */
702DECL_FORCE_INLINE(void) iemVmxVmcsSetExitQual(PVMCPUCC pVCpu, uint64_t u64ExitQual)
703{
704 pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64RoExitQual.u = u64ExitQual;
705}
706
707
708/**
709 * Sets the VM-exit interruption information field.
710 *
711 * @param pVCpu The cross context virtual CPU structure.
712 * @param uExitIntInfo The VM-exit interruption information.
713 */
714DECL_FORCE_INLINE(void) iemVmxVmcsSetExitIntInfo(PVMCPUCC pVCpu, uint32_t uExitIntInfo)
715{
716 pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32RoExitIntInfo = uExitIntInfo;
717}
718
719
720/**
721 * Sets the VM-exit interruption error code.
722 *
723 * @param pVCpu The cross context virtual CPU structure.
724 * @param uErrCode The error code.
725 */
726DECL_FORCE_INLINE(void) iemVmxVmcsSetExitIntErrCode(PVMCPUCC pVCpu, uint32_t uErrCode)
727{
728 pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32RoExitIntErrCode = uErrCode;
729}
730
731
732/**
733 * Sets the IDT-vectoring information field.
734 *
735 * @param pVCpu The cross context virtual CPU structure.
736 * @param uIdtVectorInfo The IDT-vectoring information.
737 */
738DECL_FORCE_INLINE(void) iemVmxVmcsSetIdtVectoringInfo(PVMCPUCC pVCpu, uint32_t uIdtVectorInfo)
739{
740 pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32RoIdtVectoringInfo = uIdtVectorInfo;
741}
742
743
744/**
745 * Sets the IDT-vectoring error code field.
746 *
747 * @param pVCpu The cross context virtual CPU structure.
748 * @param uErrCode The error code.
749 */
750DECL_FORCE_INLINE(void) iemVmxVmcsSetIdtVectoringErrCode(PVMCPUCC pVCpu, uint32_t uErrCode)
751{
752 pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32RoIdtVectoringErrCode = uErrCode;
753}
754
755
756/**
757 * Sets the VM-exit guest-linear address VMCS field.
758 *
759 * @param pVCpu The cross context virtual CPU structure.
760 * @param uGuestLinearAddr The VM-exit guest-linear address.
761 */
762DECL_FORCE_INLINE(void) iemVmxVmcsSetExitGuestLinearAddr(PVMCPUCC pVCpu, uint64_t uGuestLinearAddr)
763{
764 /* Bits 63:32 of guest-linear address MBZ if the guest isn't in long mode prior to the VM-exit. */
765 Assert(CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)) || !(uGuestLinearAddr & UINT64_C(0xffffffff00000000)));
766 pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64RoGuestLinearAddr.u = uGuestLinearAddr;
767}
768
769
770/**
771 * Sets the VM-exit guest-physical address VMCS field.
772 *
773 * @param pVCpu The cross context virtual CPU structure.
774 * @param uGuestPhysAddr The VM-exit guest-physical address.
775 */
776DECL_FORCE_INLINE(void) iemVmxVmcsSetExitGuestPhysAddr(PVMCPUCC pVCpu, uint64_t uGuestPhysAddr)
777{
778 pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64RoGuestPhysAddr.u = uGuestPhysAddr;
779}
780
781
782/**
783 * Sets the VM-exit instruction length VMCS field.
784 *
785 * @param pVCpu The cross context virtual CPU structure.
786 * @param cbInstr The VM-exit instruction length in bytes.
787 *
788 * @remarks Callers may clear this field to 0. Hence, this function does not check
789 * the validity of the instruction length.
790 */
791DECL_FORCE_INLINE(void) iemVmxVmcsSetExitInstrLen(PVMCPUCC pVCpu, uint32_t cbInstr)
792{
793 pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32RoExitInstrLen = cbInstr;
794}
795
796
797/**
798 * Sets the VM-exit instruction info. VMCS field.
799 *
800 * @param pVCpu The cross context virtual CPU structure.
801 * @param uExitInstrInfo The VM-exit instruction information.
802 */
803DECL_FORCE_INLINE(void) iemVmxVmcsSetExitInstrInfo(PVMCPUCC pVCpu, uint32_t uExitInstrInfo)
804{
805 pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32RoExitInstrInfo = uExitInstrInfo;
806}
807
808
809/**
810 * Sets the guest pending-debug exceptions field.
811 *
812 * @param pVCpu The cross context virtual CPU structure.
813 * @param uGuestPendingDbgXcpts The guest pending-debug exceptions.
814 */
815DECL_FORCE_INLINE(void) iemVmxVmcsSetGuestPendingDbgXcpts(PVMCPUCC pVCpu, uint64_t uGuestPendingDbgXcpts)
816{
817 Assert(!(uGuestPendingDbgXcpts & VMX_VMCS_GUEST_PENDING_DEBUG_VALID_MASK));
818 pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64GuestPendingDbgXcpts.u = uGuestPendingDbgXcpts;
819}
820
821
822/**
823 * Implements VMSucceed for VMX instruction success.
824 *
825 * @param pVCpu The cross context virtual CPU structure.
826 */
827DECL_FORCE_INLINE(void) iemVmxVmSucceed(PVMCPUCC pVCpu)
828{
829 return CPUMSetGuestVmxVmSucceed(&pVCpu->cpum.GstCtx);
830}
831
832
833/**
834 * Implements VMFailInvalid for VMX instruction failure.
835 *
836 * @param pVCpu The cross context virtual CPU structure.
837 */
838DECL_FORCE_INLINE(void) iemVmxVmFailInvalid(PVMCPUCC pVCpu)
839{
840 return CPUMSetGuestVmxVmFailInvalid(&pVCpu->cpum.GstCtx);
841}
842
843
844/**
845 * Implements VMFail for VMX instruction failure.
846 *
847 * @param pVCpu The cross context virtual CPU structure.
848 * @param enmInsErr The VM instruction error.
849 */
850DECL_FORCE_INLINE(void) iemVmxVmFail(PVMCPUCC pVCpu, VMXINSTRERR enmInsErr)
851{
852 return CPUMSetGuestVmxVmFail(&pVCpu->cpum.GstCtx, enmInsErr);
853}
854
855
856/**
857 * Checks if the given auto-load/store MSR area count is valid for the
858 * implementation.
859 *
860 * @returns @c true if it's within the valid limit, @c false otherwise.
861 * @param pVCpu The cross context virtual CPU structure.
862 * @param uMsrCount The MSR area count to check.
863 */
864DECL_FORCE_INLINE(bool) iemVmxIsAutoMsrCountValid(PCVMCPU pVCpu, uint32_t uMsrCount)
865{
866 uint64_t const u64VmxMiscMsr = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Misc;
867 uint32_t const cMaxSupportedMsrs = VMX_MISC_MAX_MSRS(u64VmxMiscMsr);
868 Assert(cMaxSupportedMsrs <= VMX_V_AUTOMSR_AREA_SIZE / sizeof(VMXAUTOMSR));
869 if (uMsrCount <= cMaxSupportedMsrs)
870 return true;
871 return false;
872}
873
874
875/**
876 * Flushes the current VMCS contents back to guest memory.
877 *
878 * @returns VBox status code.
879 * @param pVCpu The cross context virtual CPU structure.
880 */
881DECL_FORCE_INLINE(int) iemVmxWriteCurrentVmcsToGstMem(PVMCPUCC pVCpu)
882{
883 Assert(IEM_VMX_HAS_CURRENT_VMCS(pVCpu));
884 int rc = PGMPhysSimpleWriteGCPhys(pVCpu->CTX_SUFF(pVM), IEM_VMX_GET_CURRENT_VMCS(pVCpu),
885 &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs, sizeof(pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs));
886 return rc;
887}
888
889
890/**
891 * Populates the current VMCS contents from guest memory.
892 *
893 * @returns VBox status code.
894 * @param pVCpu The cross context virtual CPU structure.
895 */
896DECL_FORCE_INLINE(int) iemVmxReadCurrentVmcsFromGstMem(PVMCPUCC pVCpu)
897{
898 Assert(IEM_VMX_HAS_CURRENT_VMCS(pVCpu));
899 int rc = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs,
900 IEM_VMX_GET_CURRENT_VMCS(pVCpu), sizeof(pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs));
901 return rc;
902}
903
904
905/**
906 * Implements VMSucceed for the VMREAD instruction and increments the guest RIP.
907 *
908 * @param pVCpu The cross context virtual CPU structure.
909 */
910DECL_FORCE_INLINE(void) iemVmxVmreadSuccess(PVMCPUCC pVCpu, uint8_t cbInstr)
911{
912 iemVmxVmSucceed(pVCpu);
913 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
914}
915
916
917/**
918 * Gets the instruction diagnostic for segment base checks during VM-entry of a
919 * nested-guest.
920 *
921 * @param iSegReg The segment index (X86_SREG_XXX).
922 */
923IEM_STATIC VMXVDIAG iemVmxGetDiagVmentrySegBase(unsigned iSegReg)
924{
925 switch (iSegReg)
926 {
927 case X86_SREG_CS: return kVmxVDiag_Vmentry_GuestSegBaseCs;
928 case X86_SREG_DS: return kVmxVDiag_Vmentry_GuestSegBaseDs;
929 case X86_SREG_ES: return kVmxVDiag_Vmentry_GuestSegBaseEs;
930 case X86_SREG_FS: return kVmxVDiag_Vmentry_GuestSegBaseFs;
931 case X86_SREG_GS: return kVmxVDiag_Vmentry_GuestSegBaseGs;
932 case X86_SREG_SS: return kVmxVDiag_Vmentry_GuestSegBaseSs;
933 IEM_NOT_REACHED_DEFAULT_CASE_RET2(kVmxVDiag_Ipe_1);
934 }
935}
936
937
938/**
939 * Gets the instruction diagnostic for segment base checks during VM-entry of a
940 * nested-guest that is in Virtual-8086 mode.
941 *
942 * @param iSegReg The segment index (X86_SREG_XXX).
943 */
944IEM_STATIC VMXVDIAG iemVmxGetDiagVmentrySegBaseV86(unsigned iSegReg)
945{
946 switch (iSegReg)
947 {
948 case X86_SREG_CS: return kVmxVDiag_Vmentry_GuestSegBaseV86Cs;
949 case X86_SREG_DS: return kVmxVDiag_Vmentry_GuestSegBaseV86Ds;
950 case X86_SREG_ES: return kVmxVDiag_Vmentry_GuestSegBaseV86Es;
951 case X86_SREG_FS: return kVmxVDiag_Vmentry_GuestSegBaseV86Fs;
952 case X86_SREG_GS: return kVmxVDiag_Vmentry_GuestSegBaseV86Gs;
953 case X86_SREG_SS: return kVmxVDiag_Vmentry_GuestSegBaseV86Ss;
954 IEM_NOT_REACHED_DEFAULT_CASE_RET2(kVmxVDiag_Ipe_2);
955 }
956}
957
958
959/**
960 * Gets the instruction diagnostic for segment limit checks during VM-entry of a
961 * nested-guest that is in Virtual-8086 mode.
962 *
963 * @param iSegReg The segment index (X86_SREG_XXX).
964 */
965IEM_STATIC VMXVDIAG iemVmxGetDiagVmentrySegLimitV86(unsigned iSegReg)
966{
967 switch (iSegReg)
968 {
969 case X86_SREG_CS: return kVmxVDiag_Vmentry_GuestSegLimitV86Cs;
970 case X86_SREG_DS: return kVmxVDiag_Vmentry_GuestSegLimitV86Ds;
971 case X86_SREG_ES: return kVmxVDiag_Vmentry_GuestSegLimitV86Es;
972 case X86_SREG_FS: return kVmxVDiag_Vmentry_GuestSegLimitV86Fs;
973 case X86_SREG_GS: return kVmxVDiag_Vmentry_GuestSegLimitV86Gs;
974 case X86_SREG_SS: return kVmxVDiag_Vmentry_GuestSegLimitV86Ss;
975 IEM_NOT_REACHED_DEFAULT_CASE_RET2(kVmxVDiag_Ipe_3);
976 }
977}
978
979
980/**
981 * Gets the instruction diagnostic for segment attribute checks during VM-entry of a
982 * nested-guest that is in Virtual-8086 mode.
983 *
984 * @param iSegReg The segment index (X86_SREG_XXX).
985 */
986IEM_STATIC VMXVDIAG iemVmxGetDiagVmentrySegAttrV86(unsigned iSegReg)
987{
988 switch (iSegReg)
989 {
990 case X86_SREG_CS: return kVmxVDiag_Vmentry_GuestSegAttrV86Cs;
991 case X86_SREG_DS: return kVmxVDiag_Vmentry_GuestSegAttrV86Ds;
992 case X86_SREG_ES: return kVmxVDiag_Vmentry_GuestSegAttrV86Es;
993 case X86_SREG_FS: return kVmxVDiag_Vmentry_GuestSegAttrV86Fs;
994 case X86_SREG_GS: return kVmxVDiag_Vmentry_GuestSegAttrV86Gs;
995 case X86_SREG_SS: return kVmxVDiag_Vmentry_GuestSegAttrV86Ss;
996 IEM_NOT_REACHED_DEFAULT_CASE_RET2(kVmxVDiag_Ipe_4);
997 }
998}
999
1000
1001/**
1002 * Gets the instruction diagnostic for segment attributes reserved bits failure
1003 * during VM-entry of a nested-guest.
1004 *
1005 * @param iSegReg The segment index (X86_SREG_XXX).
1006 */
1007IEM_STATIC VMXVDIAG iemVmxGetDiagVmentrySegAttrRsvd(unsigned iSegReg)
1008{
1009 switch (iSegReg)
1010 {
1011 case X86_SREG_CS: return kVmxVDiag_Vmentry_GuestSegAttrRsvdCs;
1012 case X86_SREG_DS: return kVmxVDiag_Vmentry_GuestSegAttrRsvdDs;
1013 case X86_SREG_ES: return kVmxVDiag_Vmentry_GuestSegAttrRsvdEs;
1014 case X86_SREG_FS: return kVmxVDiag_Vmentry_GuestSegAttrRsvdFs;
1015 case X86_SREG_GS: return kVmxVDiag_Vmentry_GuestSegAttrRsvdGs;
1016 case X86_SREG_SS: return kVmxVDiag_Vmentry_GuestSegAttrRsvdSs;
1017 IEM_NOT_REACHED_DEFAULT_CASE_RET2(kVmxVDiag_Ipe_5);
1018 }
1019}
1020
1021
1022/**
1023 * Gets the instruction diagnostic for segment attributes descriptor-type
1024 * (code/segment or system) failure during VM-entry of a nested-guest.
1025 *
1026 * @param iSegReg The segment index (X86_SREG_XXX).
1027 */
1028IEM_STATIC VMXVDIAG iemVmxGetDiagVmentrySegAttrDescType(unsigned iSegReg)
1029{
1030 switch (iSegReg)
1031 {
1032 case X86_SREG_CS: return kVmxVDiag_Vmentry_GuestSegAttrDescTypeCs;
1033 case X86_SREG_DS: return kVmxVDiag_Vmentry_GuestSegAttrDescTypeDs;
1034 case X86_SREG_ES: return kVmxVDiag_Vmentry_GuestSegAttrDescTypeEs;
1035 case X86_SREG_FS: return kVmxVDiag_Vmentry_GuestSegAttrDescTypeFs;
1036 case X86_SREG_GS: return kVmxVDiag_Vmentry_GuestSegAttrDescTypeGs;
1037 case X86_SREG_SS: return kVmxVDiag_Vmentry_GuestSegAttrDescTypeSs;
1038 IEM_NOT_REACHED_DEFAULT_CASE_RET2(kVmxVDiag_Ipe_6);
1039 }
1040}
1041
1042
1043/**
1044 * Gets the instruction diagnostic for segment attributes descriptor-type
1045 * (code/segment or system) failure during VM-entry of a nested-guest.
1046 *
1047 * @param iSegReg The segment index (X86_SREG_XXX).
1048 */
1049IEM_STATIC VMXVDIAG iemVmxGetDiagVmentrySegAttrPresent(unsigned iSegReg)
1050{
1051 switch (iSegReg)
1052 {
1053 case X86_SREG_CS: return kVmxVDiag_Vmentry_GuestSegAttrPresentCs;
1054 case X86_SREG_DS: return kVmxVDiag_Vmentry_GuestSegAttrPresentDs;
1055 case X86_SREG_ES: return kVmxVDiag_Vmentry_GuestSegAttrPresentEs;
1056 case X86_SREG_FS: return kVmxVDiag_Vmentry_GuestSegAttrPresentFs;
1057 case X86_SREG_GS: return kVmxVDiag_Vmentry_GuestSegAttrPresentGs;
1058 case X86_SREG_SS: return kVmxVDiag_Vmentry_GuestSegAttrPresentSs;
1059 IEM_NOT_REACHED_DEFAULT_CASE_RET2(kVmxVDiag_Ipe_7);
1060 }
1061}
1062
1063
1064/**
1065 * Gets the instruction diagnostic for segment attribute granularity failure during
1066 * VM-entry of a nested-guest.
1067 *
1068 * @param iSegReg The segment index (X86_SREG_XXX).
1069 */
1070IEM_STATIC VMXVDIAG iemVmxGetDiagVmentrySegAttrGran(unsigned iSegReg)
1071{
1072 switch (iSegReg)
1073 {
1074 case X86_SREG_CS: return kVmxVDiag_Vmentry_GuestSegAttrGranCs;
1075 case X86_SREG_DS: return kVmxVDiag_Vmentry_GuestSegAttrGranDs;
1076 case X86_SREG_ES: return kVmxVDiag_Vmentry_GuestSegAttrGranEs;
1077 case X86_SREG_FS: return kVmxVDiag_Vmentry_GuestSegAttrGranFs;
1078 case X86_SREG_GS: return kVmxVDiag_Vmentry_GuestSegAttrGranGs;
1079 case X86_SREG_SS: return kVmxVDiag_Vmentry_GuestSegAttrGranSs;
1080 IEM_NOT_REACHED_DEFAULT_CASE_RET2(kVmxVDiag_Ipe_8);
1081 }
1082}
1083
1084/**
1085 * Gets the instruction diagnostic for segment attribute DPL/RPL failure during
1086 * VM-entry of a nested-guest.
1087 *
1088 * @param iSegReg The segment index (X86_SREG_XXX).
1089 */
1090IEM_STATIC VMXVDIAG iemVmxGetDiagVmentrySegAttrDplRpl(unsigned iSegReg)
1091{
1092 switch (iSegReg)
1093 {
1094 case X86_SREG_CS: return kVmxVDiag_Vmentry_GuestSegAttrDplRplCs;
1095 case X86_SREG_DS: return kVmxVDiag_Vmentry_GuestSegAttrDplRplDs;
1096 case X86_SREG_ES: return kVmxVDiag_Vmentry_GuestSegAttrDplRplEs;
1097 case X86_SREG_FS: return kVmxVDiag_Vmentry_GuestSegAttrDplRplFs;
1098 case X86_SREG_GS: return kVmxVDiag_Vmentry_GuestSegAttrDplRplGs;
1099 case X86_SREG_SS: return kVmxVDiag_Vmentry_GuestSegAttrDplRplSs;
1100 IEM_NOT_REACHED_DEFAULT_CASE_RET2(kVmxVDiag_Ipe_9);
1101 }
1102}
1103
1104
1105/**
1106 * Gets the instruction diagnostic for segment attribute type accessed failure
1107 * during VM-entry of a nested-guest.
1108 *
1109 * @param iSegReg The segment index (X86_SREG_XXX).
1110 */
1111IEM_STATIC VMXVDIAG iemVmxGetDiagVmentrySegAttrTypeAcc(unsigned iSegReg)
1112{
1113 switch (iSegReg)
1114 {
1115 case X86_SREG_CS: return kVmxVDiag_Vmentry_GuestSegAttrTypeAccCs;
1116 case X86_SREG_DS: return kVmxVDiag_Vmentry_GuestSegAttrTypeAccDs;
1117 case X86_SREG_ES: return kVmxVDiag_Vmentry_GuestSegAttrTypeAccEs;
1118 case X86_SREG_FS: return kVmxVDiag_Vmentry_GuestSegAttrTypeAccFs;
1119 case X86_SREG_GS: return kVmxVDiag_Vmentry_GuestSegAttrTypeAccGs;
1120 case X86_SREG_SS: return kVmxVDiag_Vmentry_GuestSegAttrTypeAccSs;
1121 IEM_NOT_REACHED_DEFAULT_CASE_RET2(kVmxVDiag_Ipe_10);
1122 }
1123}
1124
1125
1126/**
1127 * Saves the guest control registers, debug registers and some MSRs are part of
1128 * VM-exit.
1129 *
1130 * @param pVCpu The cross context virtual CPU structure.
1131 */
1132IEM_STATIC void iemVmxVmexitSaveGuestControlRegsMsrs(PVMCPUCC pVCpu)
1133{
1134 /*
1135 * Saves the guest control registers, debug registers and some MSRs.
1136 * See Intel spec. 27.3.1 "Saving Control Registers, Debug Registers and MSRs".
1137 */
1138 PVMXVVMCS pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
1139
1140 /* Save control registers. */
1141 pVmcs->u64GuestCr0.u = pVCpu->cpum.GstCtx.cr0;
1142 pVmcs->u64GuestCr3.u = pVCpu->cpum.GstCtx.cr3;
1143 pVmcs->u64GuestCr4.u = pVCpu->cpum.GstCtx.cr4;
1144
1145 /* Save SYSENTER CS, ESP, EIP. */
1146 pVmcs->u32GuestSysenterCS = pVCpu->cpum.GstCtx.SysEnter.cs;
1147 if (IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fLongMode)
1148 {
1149 pVmcs->u64GuestSysenterEsp.u = pVCpu->cpum.GstCtx.SysEnter.esp;
1150 pVmcs->u64GuestSysenterEip.u = pVCpu->cpum.GstCtx.SysEnter.eip;
1151 }
1152 else
1153 {
1154 pVmcs->u64GuestSysenterEsp.s.Lo = pVCpu->cpum.GstCtx.SysEnter.esp;
1155 pVmcs->u64GuestSysenterEip.s.Lo = pVCpu->cpum.GstCtx.SysEnter.eip;
1156 }
1157
1158 /* Save debug registers (DR7 and IA32_DEBUGCTL MSR). */
1159 if (pVmcs->u32ExitCtls & VMX_EXIT_CTLS_SAVE_DEBUG)
1160 {
1161 pVmcs->u64GuestDr7.u = pVCpu->cpum.GstCtx.dr[7];
1162 /** @todo NSTVMX: Support IA32_DEBUGCTL MSR */
1163 }
1164
1165 /* Save PAT MSR. */
1166 if (pVmcs->u32ExitCtls & VMX_EXIT_CTLS_SAVE_PAT_MSR)
1167 pVmcs->u64GuestPatMsr.u = pVCpu->cpum.GstCtx.msrPAT;
1168
1169 /* Save EFER MSR. */
1170 if (pVmcs->u32ExitCtls & VMX_EXIT_CTLS_SAVE_EFER_MSR)
1171 pVmcs->u64GuestEferMsr.u = pVCpu->cpum.GstCtx.msrEFER;
1172
1173 /* We don't support clearing IA32_BNDCFGS MSR yet. */
1174 Assert(!(pVmcs->u32ExitCtls & VMX_EXIT_CTLS_CLEAR_BNDCFGS_MSR));
1175
1176 /* Nothing to do for SMBASE register - We don't support SMM yet. */
1177}
1178
1179
1180/**
1181 * Saves the guest force-flags in preparation of entering the nested-guest.
1182 *
1183 * @param pVCpu The cross context virtual CPU structure.
1184 */
1185IEM_STATIC void iemVmxVmentrySaveNmiBlockingFF(PVMCPUCC pVCpu)
1186{
1187 /* We shouldn't be called multiple times during VM-entry. */
1188 Assert(pVCpu->cpum.GstCtx.hwvirt.fLocalForcedActions == 0);
1189
1190 /* MTF should not be set outside VMX non-root mode. */
1191 Assert(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_VMX_MTF));
1192
1193 /*
1194 * Preserve the required force-flags.
1195 *
1196 * We cache and clear force-flags that would affect the execution of the
1197 * nested-guest. Cached flags are then restored while returning to the guest
1198 * if necessary.
1199 *
1200 * - VMCPU_FF_INHIBIT_INTERRUPTS need not be cached as it only affects
1201 * interrupts until the completion of the current VMLAUNCH/VMRESUME
1202 * instruction. Interrupt inhibition for any nested-guest instruction
1203 * is supplied by the guest-interruptibility state VMCS field and will
1204 * be set up as part of loading the guest state.
1205 *
1206 * - VMCPU_FF_BLOCK_NMIS needs to be cached as VM-exits caused before
1207 * successful VM-entry (due to invalid guest-state) need to continue
1208 * blocking NMIs if it was in effect before VM-entry.
1209 *
1210 * - MTF need not be preserved as it's used only in VMX non-root mode and
1211 * is supplied through the VM-execution controls.
1212 *
1213 * The remaining FFs (e.g. timers, APIC updates) can stay in place so that
1214 * we will be able to generate interrupts that may cause VM-exits for
1215 * the nested-guest.
1216 */
1217 pVCpu->cpum.GstCtx.hwvirt.fLocalForcedActions = pVCpu->fLocalForcedActions & VMCPU_FF_BLOCK_NMIS;
1218}
1219
1220
1221/**
1222 * Restores the guest force-flags in preparation of exiting the nested-guest.
1223 *
1224 * @param pVCpu The cross context virtual CPU structure.
1225 */
1226IEM_STATIC void iemVmxVmexitRestoreNmiBlockingFF(PVMCPUCC pVCpu)
1227{
1228 if (pVCpu->cpum.GstCtx.hwvirt.fLocalForcedActions)
1229 {
1230 VMCPU_FF_SET_MASK(pVCpu, pVCpu->cpum.GstCtx.hwvirt.fLocalForcedActions);
1231 pVCpu->cpum.GstCtx.hwvirt.fLocalForcedActions = 0;
1232 }
1233}
1234
1235
1236/**
1237 * Performs the VMX transition to/from VMX non-root mode.
1238 *
1239 * @param pVCpu The cross context virtual CPU structure.
1240*/
1241IEM_STATIC int iemVmxTransition(PVMCPUCC pVCpu)
1242{
1243 /*
1244 * Inform PGM about paging mode changes.
1245 * We include X86_CR0_PE because PGM doesn't handle paged-real mode yet,
1246 * see comment in iemMemPageTranslateAndCheckAccess().
1247 */
1248 int rc = PGMChangeMode(pVCpu, pVCpu->cpum.GstCtx.cr0 | X86_CR0_PE, pVCpu->cpum.GstCtx.cr4, pVCpu->cpum.GstCtx.msrEFER,
1249 true /* fForce */);
1250 AssertRCReturn(rc, rc);
1251
1252 /* Invalidate IEM TLBs now that we've forced a PGM mode change. */
1253 IEMTlbInvalidateAll(pVCpu, false /*fVmm*/);
1254
1255 /* Inform CPUM (recompiler), can later be removed. */
1256 CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_ALL);
1257
1258 /* Re-initialize IEM cache/state after the drastic mode switch. */
1259 iemReInitExec(pVCpu);
1260 return rc;
1261}
1262
1263
1264/**
1265 * Calculates the current VMX-preemption timer value.
1266 *
1267 * @returns The current VMX-preemption timer value.
1268 * @param pVCpu The cross context virtual CPU structure.
1269 */
1270IEM_STATIC uint32_t iemVmxCalcPreemptTimer(PVMCPUCC pVCpu)
1271{
1272 /*
1273 * Assume the following:
1274 * PreemptTimerShift = 5
1275 * VmcsPreemptTimer = 2 (i.e. need to decrement by 1 every 2 * RT_BIT(5) = 20000 TSC ticks)
1276 * EntryTick = 50000 (TSC at time of VM-entry)
1277 *
1278 * CurTick Delta PreemptTimerVal
1279 * ----------------------------------
1280 * 60000 10000 2
1281 * 80000 30000 1
1282 * 90000 40000 0 -> VM-exit.
1283 *
1284 * If Delta >= VmcsPreemptTimer * RT_BIT(PreemptTimerShift) cause a VMX-preemption timer VM-exit.
1285 * The saved VMX-preemption timer value is calculated as follows:
1286 * PreemptTimerVal = VmcsPreemptTimer - (Delta / (VmcsPreemptTimer * RT_BIT(PreemptTimerShift)))
1287 * E.g.:
1288 * Delta = 10000
1289 * Tmp = 10000 / (2 * 10000) = 0.5
1290 * NewPt = 2 - 0.5 = 2
1291 * Delta = 30000
1292 * Tmp = 30000 / (2 * 10000) = 1.5
1293 * NewPt = 2 - 1.5 = 1
1294 * Delta = 40000
1295 * Tmp = 40000 / 20000 = 2
1296 * NewPt = 2 - 2 = 0
1297 */
1298 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_HWVIRT);
1299 uint32_t const uVmcsPreemptVal = pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32PreemptTimer;
1300 if (uVmcsPreemptVal > 0)
1301 {
1302 uint64_t const uCurTick = TMCpuTickGetNoCheck(pVCpu);
1303 uint64_t const uEntryTick = pVCpu->cpum.GstCtx.hwvirt.vmx.uEntryTick;
1304 uint64_t const uDelta = uCurTick - uEntryTick;
1305 uint32_t const uPreemptTimer = uVmcsPreemptVal
1306 - ASMDivU64ByU32RetU32(uDelta, uVmcsPreemptVal * RT_BIT(VMX_V_PREEMPT_TIMER_SHIFT));
1307 return uPreemptTimer;
1308 }
1309 return 0;
1310}
1311
1312
1313/**
1314 * Saves guest segment registers, GDTR, IDTR, LDTR, TR as part of VM-exit.
1315 *
1316 * @param pVCpu The cross context virtual CPU structure.
1317 */
1318IEM_STATIC void iemVmxVmexitSaveGuestSegRegs(PVMCPUCC pVCpu)
1319{
1320 /*
1321 * Save guest segment registers, GDTR, IDTR, LDTR, TR.
1322 * See Intel spec 27.3.2 "Saving Segment Registers and Descriptor-Table Registers".
1323 */
1324 /* CS, SS, ES, DS, FS, GS. */
1325 PVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
1326 for (unsigned iSegReg = 0; iSegReg < X86_SREG_COUNT; iSegReg++)
1327 {
1328 PCCPUMSELREG pSelReg = &pVCpu->cpum.GstCtx.aSRegs[iSegReg];
1329 if (!pSelReg->Attr.n.u1Unusable)
1330 iemVmxVmcsSetGuestSegReg(pVmcs, iSegReg, pSelReg);
1331 else
1332 {
1333 /*
1334 * For unusable segments the attributes are undefined except for CS and SS.
1335 * For the rest we don't bother preserving anything but the unusable bit.
1336 */
1337 switch (iSegReg)
1338 {
1339 case X86_SREG_CS:
1340 pVmcs->GuestCs = pSelReg->Sel;
1341 pVmcs->u64GuestCsBase.u = pSelReg->u64Base;
1342 pVmcs->u32GuestCsLimit = pSelReg->u32Limit;
1343 pVmcs->u32GuestCsAttr = pSelReg->Attr.u & ( X86DESCATTR_L | X86DESCATTR_D | X86DESCATTR_G
1344 | X86DESCATTR_UNUSABLE);
1345 break;
1346
1347 case X86_SREG_SS:
1348 pVmcs->GuestSs = pSelReg->Sel;
1349 if (IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fLongMode)
1350 pVmcs->u64GuestSsBase.u &= UINT32_C(0xffffffff);
1351 pVmcs->u32GuestSsAttr = pSelReg->Attr.u & (X86DESCATTR_DPL | X86DESCATTR_UNUSABLE);
1352 break;
1353
1354 case X86_SREG_DS:
1355 pVmcs->GuestDs = pSelReg->Sel;
1356 if (IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fLongMode)
1357 pVmcs->u64GuestDsBase.u &= UINT32_C(0xffffffff);
1358 pVmcs->u32GuestDsAttr = X86DESCATTR_UNUSABLE;
1359 break;
1360
1361 case X86_SREG_ES:
1362 pVmcs->GuestEs = pSelReg->Sel;
1363 if (IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fLongMode)
1364 pVmcs->u64GuestEsBase.u &= UINT32_C(0xffffffff);
1365 pVmcs->u32GuestEsAttr = X86DESCATTR_UNUSABLE;
1366 break;
1367
1368 case X86_SREG_FS:
1369 pVmcs->GuestFs = pSelReg->Sel;
1370 pVmcs->u64GuestFsBase.u = pSelReg->u64Base;
1371 pVmcs->u32GuestFsAttr = X86DESCATTR_UNUSABLE;
1372 break;
1373
1374 case X86_SREG_GS:
1375 pVmcs->GuestGs = pSelReg->Sel;
1376 pVmcs->u64GuestGsBase.u = pSelReg->u64Base;
1377 pVmcs->u32GuestGsAttr = X86DESCATTR_UNUSABLE;
1378 break;
1379 }
1380 }
1381 }
1382
1383 /* Segment attribute bits 31:17 and 11:8 MBZ. */
1384 uint32_t const fValidAttrMask = X86DESCATTR_TYPE | X86DESCATTR_DT | X86DESCATTR_DPL | X86DESCATTR_P
1385 | X86DESCATTR_AVL | X86DESCATTR_L | X86DESCATTR_D | X86DESCATTR_G
1386 | X86DESCATTR_UNUSABLE;
1387 /* LDTR. */
1388 {
1389 PCCPUMSELREG pSelReg = &pVCpu->cpum.GstCtx.ldtr;
1390 pVmcs->GuestLdtr = pSelReg->Sel;
1391 pVmcs->u64GuestLdtrBase.u = pSelReg->u64Base;
1392 Assert(X86_IS_CANONICAL(pSelReg->u64Base));
1393 pVmcs->u32GuestLdtrLimit = pSelReg->u32Limit;
1394 pVmcs->u32GuestLdtrAttr = pSelReg->Attr.u & fValidAttrMask;
1395 }
1396
1397 /* TR. */
1398 {
1399 PCCPUMSELREG pSelReg = &pVCpu->cpum.GstCtx.tr;
1400 pVmcs->GuestTr = pSelReg->Sel;
1401 pVmcs->u64GuestTrBase.u = pSelReg->u64Base;
1402 pVmcs->u32GuestTrLimit = pSelReg->u32Limit;
1403 pVmcs->u32GuestTrAttr = pSelReg->Attr.u & fValidAttrMask;
1404 }
1405
1406 /* GDTR. */
1407 pVmcs->u64GuestGdtrBase.u = pVCpu->cpum.GstCtx.gdtr.pGdt;
1408 pVmcs->u32GuestGdtrLimit = pVCpu->cpum.GstCtx.gdtr.cbGdt;
1409
1410 /* IDTR. */
1411 pVmcs->u64GuestIdtrBase.u = pVCpu->cpum.GstCtx.idtr.pIdt;
1412 pVmcs->u32GuestIdtrLimit = pVCpu->cpum.GstCtx.idtr.cbIdt;
1413}
1414
1415
1416/**
1417 * Saves guest non-register state as part of VM-exit.
1418 *
1419 * @param pVCpu The cross context virtual CPU structure.
1420 * @param uExitReason The VM-exit reason.
1421 */
1422IEM_STATIC void iemVmxVmexitSaveGuestNonRegState(PVMCPUCC pVCpu, uint32_t uExitReason)
1423{
1424 /*
1425 * Save guest non-register state.
1426 * See Intel spec. 27.3.4 "Saving Non-Register State".
1427 */
1428 PVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
1429
1430 /*
1431 * Activity state.
1432 * Most VM-exits will occur in the active state. However, if the first instruction
1433 * following the VM-entry is a HLT instruction, and the MTF VM-execution control is set,
1434 * the VM-exit will be from the HLT activity state.
1435 *
1436 * See Intel spec. 25.5.2 "Monitor Trap Flag".
1437 */
1438 /** @todo NSTVMX: Does triple-fault VM-exit reflect a shutdown activity state or
1439 * not? */
1440 EMSTATE const enmActivityState = EMGetState(pVCpu);
1441 switch (enmActivityState)
1442 {
1443 case EMSTATE_HALTED: pVmcs->u32GuestActivityState = VMX_VMCS_GUEST_ACTIVITY_HLT; break;
1444 default: pVmcs->u32GuestActivityState = VMX_VMCS_GUEST_ACTIVITY_ACTIVE; break;
1445 }
1446
1447 /*
1448 * Interruptibility-state.
1449 */
1450 /* NMI. */
1451 pVmcs->u32GuestIntrState = 0;
1452 if (pVmcs->u32PinCtls & VMX_PIN_CTLS_VIRT_NMI)
1453 {
1454 if (pVCpu->cpum.GstCtx.hwvirt.vmx.fVirtNmiBlocking)
1455 pVmcs->u32GuestIntrState |= VMX_VMCS_GUEST_INT_STATE_BLOCK_NMI;
1456 }
1457 else
1458 {
1459 if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_BLOCK_NMIS))
1460 pVmcs->u32GuestIntrState |= VMX_VMCS_GUEST_INT_STATE_BLOCK_NMI;
1461 }
1462
1463 /* Blocking-by-STI. */
1464 if ( VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS)
1465 && pVCpu->cpum.GstCtx.rip == EMGetInhibitInterruptsPC(pVCpu))
1466 {
1467 /** @todo NSTVMX: We can't distinguish between blocking-by-MovSS and blocking-by-STI
1468 * currently. */
1469 pVmcs->u32GuestIntrState |= VMX_VMCS_GUEST_INT_STATE_BLOCK_STI;
1470 }
1471 /* Nothing to do for SMI/enclave. We don't support enclaves or SMM yet. */
1472
1473 /*
1474 * Pending debug exceptions.
1475 *
1476 * For VM-exits where it is not applicable, we can safely zero out the field.
1477 * For VM-exits where it is applicable, it's expected to be updated by the caller already.
1478 */
1479 if ( uExitReason != VMX_EXIT_INIT_SIGNAL
1480 && uExitReason != VMX_EXIT_SMI
1481 && uExitReason != VMX_EXIT_ERR_MACHINE_CHECK
1482 && !VMXIsVmexitTrapLike(uExitReason))
1483 {
1484 /** @todo NSTVMX: also must exclude VM-exits caused by debug exceptions when
1485 * block-by-MovSS is in effect. */
1486 pVmcs->u64GuestPendingDbgXcpts.u = 0;
1487 }
1488
1489 /*
1490 * Save the VMX-preemption timer value back into the VMCS if the feature is enabled.
1491 *
1492 * For VMX-preemption timer VM-exits, we should have already written back 0 if the
1493 * feature is supported back into the VMCS, and thus there is nothing further to do here.
1494 */
1495 if ( uExitReason != VMX_EXIT_PREEMPT_TIMER
1496 && (pVmcs->u32ExitCtls & VMX_EXIT_CTLS_SAVE_PREEMPT_TIMER))
1497 pVmcs->u32PreemptTimer = iemVmxCalcPreemptTimer(pVCpu);
1498
1499 /*
1500 * PAE PDPTEs.
1501 *
1502 * If EPT is enabled and PAE paging was used at the time of the VM-exit,
1503 * the PDPTEs are saved from the VMCS. Otherwise they're undefined but
1504 * we zero them for consistency.
1505 */
1506 if (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_EPT)
1507 {
1508 if ( !(pVmcs->u32EntryCtls & VMX_ENTRY_CTLS_IA32E_MODE_GUEST)
1509 && (pVCpu->cpum.GstCtx.cr4 & X86_CR4_PAE)
1510 && (pVCpu->cpum.GstCtx.cr0 & X86_CR0_PG))
1511 {
1512 pVmcs->u64GuestPdpte0.u = pVCpu->cpum.GstCtx.aPaePdpes[0].u;
1513 pVmcs->u64GuestPdpte1.u = pVCpu->cpum.GstCtx.aPaePdpes[1].u;
1514 pVmcs->u64GuestPdpte2.u = pVCpu->cpum.GstCtx.aPaePdpes[2].u;
1515 pVmcs->u64GuestPdpte3.u = pVCpu->cpum.GstCtx.aPaePdpes[3].u;
1516 }
1517 else
1518 {
1519 pVmcs->u64GuestPdpte0.u = 0;
1520 pVmcs->u64GuestPdpte1.u = 0;
1521 pVmcs->u64GuestPdpte2.u = 0;
1522 pVmcs->u64GuestPdpte3.u = 0;
1523 }
1524
1525 /* Clear PGM's copy of the EPT pointer for added safety. */
1526 PGMSetGuestEptPtr(pVCpu, 0 /* uEptPtr */);
1527 }
1528 else
1529 {
1530 pVmcs->u64GuestPdpte0.u = 0;
1531 pVmcs->u64GuestPdpte1.u = 0;
1532 pVmcs->u64GuestPdpte2.u = 0;
1533 pVmcs->u64GuestPdpte3.u = 0;
1534 }
1535}
1536
1537
1538/**
1539 * Saves the guest-state as part of VM-exit.
1540 *
1541 * @returns VBox status code.
1542 * @param pVCpu The cross context virtual CPU structure.
1543 * @param uExitReason The VM-exit reason.
1544 */
1545IEM_STATIC void iemVmxVmexitSaveGuestState(PVMCPUCC pVCpu, uint32_t uExitReason)
1546{
1547 iemVmxVmexitSaveGuestControlRegsMsrs(pVCpu);
1548 iemVmxVmexitSaveGuestSegRegs(pVCpu);
1549
1550 pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64GuestRip.u = pVCpu->cpum.GstCtx.rip;
1551 pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64GuestRsp.u = pVCpu->cpum.GstCtx.rsp;
1552 pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64GuestRFlags.u = pVCpu->cpum.GstCtx.rflags.u; /** @todo NSTVMX: Check RFLAGS.RF handling. */
1553
1554 iemVmxVmexitSaveGuestNonRegState(pVCpu, uExitReason);
1555}
1556
1557
1558/**
1559 * Saves the guest MSRs into the VM-exit MSR-store area as part of VM-exit.
1560 *
1561 * @returns VBox status code.
1562 * @param pVCpu The cross context virtual CPU structure.
1563 * @param uExitReason The VM-exit reason (for diagnostic purposes).
1564 */
1565IEM_STATIC int iemVmxVmexitSaveGuestAutoMsrs(PVMCPUCC pVCpu, uint32_t uExitReason)
1566{
1567 /*
1568 * Save guest MSRs.
1569 * See Intel spec. 27.4 "Saving MSRs".
1570 */
1571 PVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
1572 const char * const pszFailure = "VMX-abort";
1573
1574 /*
1575 * The VM-exit MSR-store area address need not be a valid guest-physical address if the
1576 * VM-exit MSR-store count is 0. If this is the case, bail early without reading it.
1577 * See Intel spec. 24.7.2 "VM-Exit Controls for MSRs".
1578 */
1579 uint32_t const cMsrs = RT_MIN(pVmcs->u32ExitMsrStoreCount, RT_ELEMENTS(pVCpu->cpum.GstCtx.hwvirt.vmx.aExitMsrStoreArea));
1580 if (!cMsrs)
1581 return VINF_SUCCESS;
1582
1583 /*
1584 * Verify the MSR auto-store count. Physical CPUs can behave unpredictably if the count
1585 * is exceeded including possibly raising #MC exceptions during VMX transition. Our
1586 * implementation causes a VMX-abort followed by a triple-fault.
1587 */
1588 bool const fIsMsrCountValid = iemVmxIsAutoMsrCountValid(pVCpu, cMsrs);
1589 if (fIsMsrCountValid)
1590 { /* likely */ }
1591 else
1592 IEM_VMX_VMEXIT_FAILED_RET(pVCpu, uExitReason, pszFailure, kVmxVDiag_Vmexit_MsrStoreCount);
1593
1594 /*
1595 * Optimization if the nested hypervisor is using the same guest-physical page for both
1596 * the VM-entry MSR-load area as well as the VM-exit MSR store area.
1597 */
1598 PVMXAUTOMSR pMsrArea;
1599 RTGCPHYS const GCPhysVmEntryMsrLoadArea = pVmcs->u64AddrEntryMsrLoad.u;
1600 RTGCPHYS const GCPhysVmExitMsrStoreArea = pVmcs->u64AddrExitMsrStore.u;
1601 if (GCPhysVmEntryMsrLoadArea == GCPhysVmExitMsrStoreArea)
1602 pMsrArea = pVCpu->cpum.GstCtx.hwvirt.vmx.aEntryMsrLoadArea;
1603 else
1604 {
1605 int rc = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), &pVCpu->cpum.GstCtx.hwvirt.vmx.aExitMsrStoreArea[0],
1606 GCPhysVmExitMsrStoreArea, cMsrs * sizeof(VMXAUTOMSR));
1607 if (RT_SUCCESS(rc))
1608 pMsrArea = pVCpu->cpum.GstCtx.hwvirt.vmx.aExitMsrStoreArea;
1609 else
1610 {
1611 AssertMsgFailed(("VM-exit: Failed to read MSR auto-store area at %#RGp, rc=%Rrc\n", GCPhysVmExitMsrStoreArea, rc));
1612 IEM_VMX_VMEXIT_FAILED_RET(pVCpu, uExitReason, pszFailure, kVmxVDiag_Vmexit_MsrStorePtrReadPhys);
1613 }
1614 }
1615
1616 /*
1617 * Update VM-exit MSR store area.
1618 */
1619 PVMXAUTOMSR pMsr = pMsrArea;
1620 for (uint32_t idxMsr = 0; idxMsr < cMsrs; idxMsr++, pMsr++)
1621 {
1622 if ( !pMsr->u32Reserved
1623 && pMsr->u32Msr != MSR_IA32_SMBASE
1624 && pMsr->u32Msr >> 8 != MSR_IA32_X2APIC_START >> 8)
1625 {
1626 VBOXSTRICTRC rcStrict = CPUMQueryGuestMsr(pVCpu, pMsr->u32Msr, &pMsr->u64Value);
1627 if (rcStrict == VINF_SUCCESS)
1628 continue;
1629
1630 /*
1631 * If we're in ring-0, we cannot handle returns to ring-3 at this point and continue VM-exit.
1632 * If any nested hypervisor loads MSRs that require ring-3 handling, we cause a VMX-abort
1633 * recording the MSR index in the auxiliary info. field and indicated further by our
1634 * own, specific diagnostic code. Later, we can try implement handling of the MSR in ring-0
1635 * if possible, or come up with a better, generic solution.
1636 */
1637 pVCpu->cpum.GstCtx.hwvirt.vmx.uAbortAux = pMsr->u32Msr;
1638 VMXVDIAG const enmDiag = rcStrict == VINF_CPUM_R3_MSR_READ
1639 ? kVmxVDiag_Vmexit_MsrStoreRing3
1640 : kVmxVDiag_Vmexit_MsrStore;
1641 IEM_VMX_VMEXIT_FAILED_RET(pVCpu, uExitReason, pszFailure, enmDiag);
1642 }
1643 else
1644 {
1645 pVCpu->cpum.GstCtx.hwvirt.vmx.uAbortAux = pMsr->u32Msr;
1646 IEM_VMX_VMEXIT_FAILED_RET(pVCpu, uExitReason, pszFailure, kVmxVDiag_Vmexit_MsrStoreRsvd);
1647 }
1648 }
1649
1650 /*
1651 * Commit the VM-exit MSR store are to guest memory.
1652 */
1653 int rc = PGMPhysSimpleWriteGCPhys(pVCpu->CTX_SUFF(pVM), GCPhysVmExitMsrStoreArea, pMsrArea, cMsrs * sizeof(VMXAUTOMSR));
1654 if (RT_SUCCESS(rc))
1655 return VINF_SUCCESS;
1656
1657 NOREF(uExitReason);
1658 NOREF(pszFailure);
1659
1660 AssertMsgFailed(("VM-exit: Failed to write MSR auto-store area at %#RGp, rc=%Rrc\n", GCPhysVmExitMsrStoreArea, rc));
1661 IEM_VMX_VMEXIT_FAILED_RET(pVCpu, uExitReason, pszFailure, kVmxVDiag_Vmexit_MsrStorePtrWritePhys);
1662}
1663
1664
1665/**
1666 * Performs a VMX abort (due to an fatal error during VM-exit).
1667 *
1668 * @returns Strict VBox status code.
1669 * @param pVCpu The cross context virtual CPU structure.
1670 * @param enmAbort The VMX abort reason.
1671 */
1672IEM_STATIC VBOXSTRICTRC iemVmxAbort(PVMCPUCC pVCpu, VMXABORT enmAbort)
1673{
1674 /*
1675 * Perform the VMX abort.
1676 * See Intel spec. 27.7 "VMX Aborts".
1677 */
1678 LogFunc(("enmAbort=%u (%s) -> RESET\n", enmAbort, VMXGetAbortDesc(enmAbort)));
1679
1680 /* We don't support SMX yet. */
1681 pVCpu->cpum.GstCtx.hwvirt.vmx.enmAbort = enmAbort;
1682 if (IEM_VMX_HAS_CURRENT_VMCS(pVCpu))
1683 {
1684 RTGCPHYS const GCPhysVmcs = IEM_VMX_GET_CURRENT_VMCS(pVCpu);
1685 uint32_t const offVmxAbort = RT_UOFFSETOF(VMXVVMCS, enmVmxAbort);
1686 PGMPhysSimpleWriteGCPhys(pVCpu->CTX_SUFF(pVM), GCPhysVmcs + offVmxAbort, &enmAbort, sizeof(enmAbort));
1687 }
1688
1689 return VINF_EM_TRIPLE_FAULT;
1690}
1691
1692
1693/**
1694 * Loads host control registers, debug registers and MSRs as part of VM-exit.
1695 *
1696 * @param pVCpu The cross context virtual CPU structure.
1697 */
1698IEM_STATIC void iemVmxVmexitLoadHostControlRegsMsrs(PVMCPUCC pVCpu)
1699{
1700 /*
1701 * Load host control registers, debug registers and MSRs.
1702 * See Intel spec. 27.5.1 "Loading Host Control Registers, Debug Registers, MSRs".
1703 */
1704 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
1705 bool const fHostInLongMode = RT_BOOL(pVmcs->u32ExitCtls & VMX_EXIT_CTLS_HOST_ADDR_SPACE_SIZE);
1706
1707 /* CR0. */
1708 {
1709 /* Bits 63:32, 28:19, 17, 15:6, ET, CD, NW and CR0 fixed bits are not modified. */
1710 uint64_t const uCr0Mb1 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr0Fixed0;
1711 uint64_t const uCr0Mb0 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr0Fixed1;
1712 uint64_t const fCr0IgnMask = VMX_EXIT_HOST_CR0_IGNORE_MASK | uCr0Mb1 | ~uCr0Mb0;
1713 uint64_t const uHostCr0 = pVmcs->u64HostCr0.u;
1714 uint64_t const uGuestCr0 = pVCpu->cpum.GstCtx.cr0;
1715 uint64_t const uValidHostCr0 = (uHostCr0 & ~fCr0IgnMask) | (uGuestCr0 & fCr0IgnMask);
1716
1717 /* Verify we have not modified CR0 fixed bits in VMX non-root operation. */
1718 Assert((uGuestCr0 & uCr0Mb1) == uCr0Mb1);
1719 Assert((uGuestCr0 & ~uCr0Mb0) == 0);
1720 CPUMSetGuestCR0(pVCpu, uValidHostCr0);
1721 }
1722
1723 /* CR4. */
1724 {
1725 /* CR4 fixed bits are not modified. */
1726 uint64_t const uCr4Mb1 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr4Fixed0;
1727 uint64_t const uCr4Mb0 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr4Fixed1;
1728 uint64_t const fCr4IgnMask = uCr4Mb1 | ~uCr4Mb0;
1729 uint64_t const uHostCr4 = pVmcs->u64HostCr4.u;
1730 uint64_t const uGuestCr4 = pVCpu->cpum.GstCtx.cr4;
1731 uint64_t uValidHostCr4 = (uHostCr4 & ~fCr4IgnMask) | (uGuestCr4 & fCr4IgnMask);
1732 if (fHostInLongMode)
1733 uValidHostCr4 |= X86_CR4_PAE;
1734 else
1735 uValidHostCr4 &= ~(uint64_t)X86_CR4_PCIDE;
1736
1737 /* Verify we have not modified CR4 fixed bits in VMX non-root operation. */
1738 Assert((uGuestCr4 & uCr4Mb1) == uCr4Mb1);
1739 Assert((uGuestCr4 & ~uCr4Mb0) == 0);
1740 CPUMSetGuestCR4(pVCpu, uValidHostCr4);
1741 }
1742
1743 /* CR3 (host value validated while checking host-state during VM-entry). */
1744 pVCpu->cpum.GstCtx.cr3 = pVmcs->u64HostCr3.u;
1745
1746 /* DR7. */
1747 pVCpu->cpum.GstCtx.dr[7] = X86_DR7_INIT_VAL;
1748
1749 /** @todo NSTVMX: Support IA32_DEBUGCTL MSR */
1750
1751 /* Save SYSENTER CS, ESP, EIP (host value validated while checking host-state during VM-entry). */
1752 pVCpu->cpum.GstCtx.SysEnter.eip = pVmcs->u64HostSysenterEip.u;
1753 pVCpu->cpum.GstCtx.SysEnter.esp = pVmcs->u64HostSysenterEsp.u;
1754 pVCpu->cpum.GstCtx.SysEnter.cs = pVmcs->u32HostSysenterCs;
1755
1756 /* FS, GS bases are loaded later while we load host segment registers. */
1757
1758 /* EFER MSR (host value validated while checking host-state during VM-entry). */
1759 if (pVmcs->u32ExitCtls & VMX_EXIT_CTLS_LOAD_EFER_MSR)
1760 pVCpu->cpum.GstCtx.msrEFER = pVmcs->u64HostEferMsr.u;
1761 else if (IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fLongMode)
1762 {
1763 if (fHostInLongMode)
1764 pVCpu->cpum.GstCtx.msrEFER |= (MSR_K6_EFER_LMA | MSR_K6_EFER_LME);
1765 else
1766 pVCpu->cpum.GstCtx.msrEFER &= ~(MSR_K6_EFER_LMA | MSR_K6_EFER_LME);
1767 }
1768
1769 /* We don't support IA32_PERF_GLOBAL_CTRL MSR yet. */
1770
1771 /* PAT MSR (host value is validated while checking host-state during VM-entry). */
1772 if (pVmcs->u32ExitCtls & VMX_EXIT_CTLS_LOAD_PAT_MSR)
1773 pVCpu->cpum.GstCtx.msrPAT = pVmcs->u64HostPatMsr.u;
1774
1775 /* We don't support IA32_BNDCFGS MSR yet. */
1776}
1777
1778
1779/**
1780 * Loads host segment registers, GDTR, IDTR, LDTR and TR as part of VM-exit.
1781 *
1782 * @param pVCpu The cross context virtual CPU structure.
1783 */
1784IEM_STATIC void iemVmxVmexitLoadHostSegRegs(PVMCPUCC pVCpu)
1785{
1786 /*
1787 * Load host segment registers, GDTR, IDTR, LDTR and TR.
1788 * See Intel spec. 27.5.2 "Loading Host Segment and Descriptor-Table Registers".
1789 *
1790 * Warning! Be careful to not touch fields that are reserved by VT-x,
1791 * e.g. segment limit high bits stored in segment attributes (in bits 11:8).
1792 */
1793 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
1794 bool const fHostInLongMode = RT_BOOL(pVmcs->u32ExitCtls & VMX_EXIT_CTLS_HOST_ADDR_SPACE_SIZE);
1795
1796 /* CS, SS, ES, DS, FS, GS. */
1797 for (unsigned iSegReg = 0; iSegReg < X86_SREG_COUNT; iSegReg++)
1798 {
1799 RTSEL const HostSel = iemVmxVmcsGetHostSelReg(pVmcs, iSegReg);
1800 bool const fUnusable = RT_BOOL(HostSel == 0);
1801 PCPUMSELREG pSelReg = &pVCpu->cpum.GstCtx.aSRegs[iSegReg];
1802
1803 /* Selector. */
1804 pSelReg->Sel = HostSel;
1805 pSelReg->ValidSel = HostSel;
1806 pSelReg->fFlags = CPUMSELREG_FLAGS_VALID;
1807
1808 /* Limit. */
1809 pSelReg->u32Limit = 0xffffffff;
1810
1811 /* Base. */
1812 pSelReg->u64Base = 0;
1813
1814 /* Attributes. */
1815 if (iSegReg == X86_SREG_CS)
1816 {
1817 pSelReg->Attr.n.u4Type = X86_SEL_TYPE_CODE | X86_SEL_TYPE_READ | X86_SEL_TYPE_ACCESSED;
1818 pSelReg->Attr.n.u1DescType = 1;
1819 pSelReg->Attr.n.u2Dpl = 0;
1820 pSelReg->Attr.n.u1Present = 1;
1821 pSelReg->Attr.n.u1Long = fHostInLongMode;
1822 pSelReg->Attr.n.u1DefBig = !fHostInLongMode;
1823 pSelReg->Attr.n.u1Granularity = 1;
1824 Assert(!pSelReg->Attr.n.u1Unusable);
1825 Assert(!fUnusable);
1826 }
1827 else
1828 {
1829 pSelReg->Attr.n.u4Type = X86_SEL_TYPE_RW | X86_SEL_TYPE_ACCESSED;
1830 pSelReg->Attr.n.u1DescType = 1;
1831 pSelReg->Attr.n.u2Dpl = 0;
1832 pSelReg->Attr.n.u1Present = 1;
1833 pSelReg->Attr.n.u1DefBig = 1;
1834 pSelReg->Attr.n.u1Granularity = 1;
1835 pSelReg->Attr.n.u1Unusable = fUnusable;
1836 }
1837 }
1838
1839 /* FS base. */
1840 if ( !pVCpu->cpum.GstCtx.fs.Attr.n.u1Unusable
1841 || fHostInLongMode)
1842 {
1843 Assert(X86_IS_CANONICAL(pVmcs->u64HostFsBase.u));
1844 pVCpu->cpum.GstCtx.fs.u64Base = pVmcs->u64HostFsBase.u;
1845 }
1846
1847 /* GS base. */
1848 if ( !pVCpu->cpum.GstCtx.gs.Attr.n.u1Unusable
1849 || fHostInLongMode)
1850 {
1851 Assert(X86_IS_CANONICAL(pVmcs->u64HostGsBase.u));
1852 pVCpu->cpum.GstCtx.gs.u64Base = pVmcs->u64HostGsBase.u;
1853 }
1854
1855 /* TR. */
1856 Assert(X86_IS_CANONICAL(pVmcs->u64HostTrBase.u));
1857 Assert(!pVCpu->cpum.GstCtx.tr.Attr.n.u1Unusable);
1858 pVCpu->cpum.GstCtx.tr.Sel = pVmcs->HostTr;
1859 pVCpu->cpum.GstCtx.tr.ValidSel = pVmcs->HostTr;
1860 pVCpu->cpum.GstCtx.tr.fFlags = CPUMSELREG_FLAGS_VALID;
1861 pVCpu->cpum.GstCtx.tr.u32Limit = X86_SEL_TYPE_SYS_386_TSS_LIMIT_MIN;
1862 pVCpu->cpum.GstCtx.tr.u64Base = pVmcs->u64HostTrBase.u;
1863 pVCpu->cpum.GstCtx.tr.Attr.n.u4Type = X86_SEL_TYPE_SYS_386_TSS_BUSY;
1864 pVCpu->cpum.GstCtx.tr.Attr.n.u1DescType = 0;
1865 pVCpu->cpum.GstCtx.tr.Attr.n.u2Dpl = 0;
1866 pVCpu->cpum.GstCtx.tr.Attr.n.u1Present = 1;
1867 pVCpu->cpum.GstCtx.tr.Attr.n.u1DefBig = 0;
1868 pVCpu->cpum.GstCtx.tr.Attr.n.u1Granularity = 0;
1869
1870 /* LDTR (Warning! do not touch the base and limits here). */
1871 pVCpu->cpum.GstCtx.ldtr.Sel = 0;
1872 pVCpu->cpum.GstCtx.ldtr.ValidSel = 0;
1873 pVCpu->cpum.GstCtx.ldtr.fFlags = CPUMSELREG_FLAGS_VALID;
1874 pVCpu->cpum.GstCtx.ldtr.Attr.u = X86DESCATTR_UNUSABLE;
1875
1876 /* GDTR. */
1877 Assert(X86_IS_CANONICAL(pVmcs->u64HostGdtrBase.u));
1878 pVCpu->cpum.GstCtx.gdtr.pGdt = pVmcs->u64HostGdtrBase.u;
1879 pVCpu->cpum.GstCtx.gdtr.cbGdt = 0xffff;
1880
1881 /* IDTR.*/
1882 Assert(X86_IS_CANONICAL(pVmcs->u64HostIdtrBase.u));
1883 pVCpu->cpum.GstCtx.idtr.pIdt = pVmcs->u64HostIdtrBase.u;
1884 pVCpu->cpum.GstCtx.idtr.cbIdt = 0xffff;
1885}
1886
1887
1888/**
1889 * Loads the host MSRs from the VM-exit MSR-load area as part of VM-exit.
1890 *
1891 * @returns VBox status code.
1892 * @param pVCpu The cross context virtual CPU structure.
1893 * @param uExitReason The VMX instruction name (for logging purposes).
1894 */
1895IEM_STATIC int iemVmxVmexitLoadHostAutoMsrs(PVMCPUCC pVCpu, uint32_t uExitReason)
1896{
1897 /*
1898 * Load host MSRs.
1899 * See Intel spec. 27.6 "Loading MSRs".
1900 */
1901 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
1902 const char * const pszFailure = "VMX-abort";
1903
1904 /*
1905 * The VM-exit MSR-load area address need not be a valid guest-physical address if the
1906 * VM-exit MSR load count is 0. If this is the case, bail early without reading it.
1907 * See Intel spec. 24.7.2 "VM-Exit Controls for MSRs".
1908 */
1909 uint32_t const cMsrs = RT_MIN(pVmcs->u32ExitMsrLoadCount, RT_ELEMENTS(pVCpu->cpum.GstCtx.hwvirt.vmx.aExitMsrLoadArea));
1910 if (!cMsrs)
1911 return VINF_SUCCESS;
1912
1913 /*
1914 * Verify the MSR auto-load count. Physical CPUs can behave unpredictably if the count
1915 * is exceeded including possibly raising #MC exceptions during VMX transition. Our
1916 * implementation causes a VMX-abort followed by a triple-fault.
1917 */
1918 bool const fIsMsrCountValid = iemVmxIsAutoMsrCountValid(pVCpu, cMsrs);
1919 if (fIsMsrCountValid)
1920 { /* likely */ }
1921 else
1922 IEM_VMX_VMEXIT_FAILED_RET(pVCpu, uExitReason, pszFailure, kVmxVDiag_Vmexit_MsrLoadCount);
1923
1924 RTGCPHYS const GCPhysVmExitMsrLoadArea = pVmcs->u64AddrExitMsrLoad.u;
1925 int rc = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), &pVCpu->cpum.GstCtx.hwvirt.vmx.aExitMsrLoadArea[0],
1926 GCPhysVmExitMsrLoadArea, cMsrs * sizeof(VMXAUTOMSR));
1927 if (RT_SUCCESS(rc))
1928 {
1929 PCVMXAUTOMSR pMsr = pVCpu->cpum.GstCtx.hwvirt.vmx.aExitMsrLoadArea;
1930 for (uint32_t idxMsr = 0; idxMsr < cMsrs; idxMsr++, pMsr++)
1931 {
1932 if ( !pMsr->u32Reserved
1933 && pMsr->u32Msr != MSR_K8_FS_BASE
1934 && pMsr->u32Msr != MSR_K8_GS_BASE
1935 && pMsr->u32Msr != MSR_K6_EFER
1936 && pMsr->u32Msr != MSR_IA32_SMM_MONITOR_CTL
1937 && pMsr->u32Msr >> 8 != MSR_IA32_X2APIC_START >> 8)
1938 {
1939 VBOXSTRICTRC rcStrict = CPUMSetGuestMsr(pVCpu, pMsr->u32Msr, pMsr->u64Value);
1940 if (rcStrict == VINF_SUCCESS)
1941 continue;
1942
1943 /*
1944 * If we're in ring-0, we cannot handle returns to ring-3 at this point and continue VM-exit.
1945 * If any nested hypervisor loads MSRs that require ring-3 handling, we cause a VMX-abort
1946 * recording the MSR index in the auxiliary info. field and indicated further by our
1947 * own, specific diagnostic code. Later, we can try implement handling of the MSR in ring-0
1948 * if possible, or come up with a better, generic solution.
1949 */
1950 pVCpu->cpum.GstCtx.hwvirt.vmx.uAbortAux = pMsr->u32Msr;
1951 VMXVDIAG const enmDiag = rcStrict == VINF_CPUM_R3_MSR_WRITE
1952 ? kVmxVDiag_Vmexit_MsrLoadRing3
1953 : kVmxVDiag_Vmexit_MsrLoad;
1954 IEM_VMX_VMEXIT_FAILED_RET(pVCpu, uExitReason, pszFailure, enmDiag);
1955 }
1956 else
1957 IEM_VMX_VMEXIT_FAILED_RET(pVCpu, uExitReason, pszFailure, kVmxVDiag_Vmexit_MsrLoadRsvd);
1958 }
1959 }
1960 else
1961 {
1962 AssertMsgFailed(("VM-exit: Failed to read MSR auto-load area at %#RGp, rc=%Rrc\n", GCPhysVmExitMsrLoadArea, rc));
1963 IEM_VMX_VMEXIT_FAILED_RET(pVCpu, uExitReason, pszFailure, kVmxVDiag_Vmexit_MsrLoadPtrReadPhys);
1964 }
1965
1966 NOREF(uExitReason);
1967 NOREF(pszFailure);
1968 return VINF_SUCCESS;
1969}
1970
1971
1972/**
1973 * Loads the host state as part of VM-exit.
1974 *
1975 * @returns Strict VBox status code.
1976 * @param pVCpu The cross context virtual CPU structure.
1977 * @param uExitReason The VM-exit reason (for logging purposes).
1978 */
1979IEM_STATIC VBOXSTRICTRC iemVmxVmexitLoadHostState(PVMCPUCC pVCpu, uint32_t uExitReason)
1980{
1981 /*
1982 * Load host state.
1983 * See Intel spec. 27.5 "Loading Host State".
1984 */
1985 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
1986 bool const fHostInLongMode = RT_BOOL(pVmcs->u32ExitCtls & VMX_EXIT_CTLS_HOST_ADDR_SPACE_SIZE);
1987
1988 /* We cannot return from a long-mode guest to a host that is not in long mode. */
1989 if ( CPUMIsGuestInLongMode(pVCpu)
1990 && !fHostInLongMode)
1991 {
1992 Log(("VM-exit from long-mode guest to host not in long-mode -> VMX-Abort\n"));
1993 return iemVmxAbort(pVCpu, VMXABORT_HOST_NOT_IN_LONG_MODE);
1994 }
1995
1996 /*
1997 * Check host PAE PDPTEs prior to loading the host state.
1998 * See Intel spec. 26.5.4 "Checking and Loading Host Page-Directory-Pointer-Table Entries".
1999 */
2000 if ( (pVmcs->u64HostCr4.u & X86_CR4_PAE)
2001 && !fHostInLongMode
2002 && ( !CPUMIsGuestInPAEModeEx(&pVCpu->cpum.GstCtx)
2003 || pVmcs->u64HostCr3.u != pVCpu->cpum.GstCtx.cr3))
2004 {
2005 int const rc = PGMGstMapPaePdpesAtCr3(pVCpu, pVmcs->u64HostCr3.u);
2006 if (RT_SUCCESS(rc))
2007 { /* likely*/ }
2008 else
2009 {
2010 IEM_VMX_VMEXIT_FAILED(pVCpu, uExitReason, "VMX-abort", kVmxVDiag_Vmexit_HostPdpte);
2011 return iemVmxAbort(pVCpu, VMXBOART_HOST_PDPTE);
2012 }
2013 }
2014
2015 iemVmxVmexitLoadHostControlRegsMsrs(pVCpu);
2016 iemVmxVmexitLoadHostSegRegs(pVCpu);
2017
2018 /*
2019 * Load host RIP, RSP and RFLAGS.
2020 * See Intel spec. 27.5.3 "Loading Host RIP, RSP and RFLAGS"
2021 */
2022 pVCpu->cpum.GstCtx.rip = pVmcs->u64HostRip.u;
2023 pVCpu->cpum.GstCtx.rsp = pVmcs->u64HostRsp.u;
2024 pVCpu->cpum.GstCtx.rflags.u = X86_EFL_1;
2025
2026 /* Clear address range monitoring. */
2027 EMMonitorWaitClear(pVCpu);
2028
2029 /* Perform the VMX transition (PGM updates). */
2030 VBOXSTRICTRC rcStrict = iemVmxTransition(pVCpu);
2031 if (rcStrict == VINF_SUCCESS)
2032 { /* likely */ }
2033 else if (RT_SUCCESS(rcStrict))
2034 {
2035 Log3(("VM-exit: iemVmxTransition returns %Rrc (uExitReason=%u) -> Setting passup status\n", VBOXSTRICTRC_VAL(rcStrict),
2036 uExitReason));
2037 rcStrict = iemSetPassUpStatus(pVCpu, rcStrict);
2038 }
2039 else
2040 {
2041 Log3(("VM-exit: iemVmxTransition failed! rc=%Rrc (uExitReason=%u)\n", VBOXSTRICTRC_VAL(rcStrict), uExitReason));
2042 return VBOXSTRICTRC_VAL(rcStrict);
2043 }
2044
2045 Assert(rcStrict == VINF_SUCCESS);
2046
2047 /* Load MSRs from the VM-exit auto-load MSR area. */
2048 int rc = iemVmxVmexitLoadHostAutoMsrs(pVCpu, uExitReason);
2049 if (RT_FAILURE(rc))
2050 {
2051 Log(("VM-exit failed while loading host MSRs -> VMX-Abort\n"));
2052 return iemVmxAbort(pVCpu, VMXABORT_LOAD_HOST_MSR);
2053 }
2054 return VINF_SUCCESS;
2055}
2056
2057
2058/**
2059 * Gets VM-exit instruction information along with any displacement for an
2060 * instruction VM-exit.
2061 *
2062 * @returns The VM-exit instruction information.
2063 * @param pVCpu The cross context virtual CPU structure.
2064 * @param uExitReason The VM-exit reason.
2065 * @param uInstrId The VM-exit instruction identity (VMXINSTRID_XXX).
2066 * @param pGCPtrDisp Where to store the displacement field. Optional, can be
2067 * NULL.
2068 */
2069IEM_STATIC uint32_t iemVmxGetExitInstrInfo(PVMCPUCC pVCpu, uint32_t uExitReason, VMXINSTRID uInstrId, PRTGCPTR pGCPtrDisp)
2070{
2071 RTGCPTR GCPtrDisp;
2072 VMXEXITINSTRINFO ExitInstrInfo;
2073 ExitInstrInfo.u = 0;
2074
2075 /*
2076 * Get and parse the ModR/M byte from our decoded opcodes.
2077 */
2078 uint8_t bRm;
2079 uint8_t const offModRm = pVCpu->iem.s.offModRm;
2080 IEM_MODRM_GET_U8(pVCpu, bRm, offModRm);
2081 if ((bRm & X86_MODRM_MOD_MASK) == (3 << X86_MODRM_MOD_SHIFT))
2082 {
2083 /*
2084 * ModR/M indicates register addressing.
2085 *
2086 * The primary/secondary register operands are reported in the iReg1 or iReg2
2087 * fields depending on whether it is a read/write form.
2088 */
2089 uint8_t idxReg1;
2090 uint8_t idxReg2;
2091 if (!VMXINSTRID_IS_MODRM_PRIMARY_OP_W(uInstrId))
2092 {
2093 idxReg1 = ((bRm >> X86_MODRM_REG_SHIFT) & X86_MODRM_REG_SMASK) | pVCpu->iem.s.uRexReg;
2094 idxReg2 = (bRm & X86_MODRM_RM_MASK) | pVCpu->iem.s.uRexB;
2095 }
2096 else
2097 {
2098 idxReg1 = (bRm & X86_MODRM_RM_MASK) | pVCpu->iem.s.uRexB;
2099 idxReg2 = ((bRm >> X86_MODRM_REG_SHIFT) & X86_MODRM_REG_SMASK) | pVCpu->iem.s.uRexReg;
2100 }
2101 ExitInstrInfo.All.u2Scaling = 0;
2102 ExitInstrInfo.All.iReg1 = idxReg1;
2103 ExitInstrInfo.All.u3AddrSize = pVCpu->iem.s.enmEffAddrMode;
2104 ExitInstrInfo.All.fIsRegOperand = 1;
2105 ExitInstrInfo.All.uOperandSize = pVCpu->iem.s.enmEffOpSize;
2106 ExitInstrInfo.All.iSegReg = 0;
2107 ExitInstrInfo.All.iIdxReg = 0;
2108 ExitInstrInfo.All.fIdxRegInvalid = 1;
2109 ExitInstrInfo.All.iBaseReg = 0;
2110 ExitInstrInfo.All.fBaseRegInvalid = 1;
2111 ExitInstrInfo.All.iReg2 = idxReg2;
2112
2113 /* Displacement not applicable for register addressing. */
2114 GCPtrDisp = 0;
2115 }
2116 else
2117 {
2118 /*
2119 * ModR/M indicates memory addressing.
2120 */
2121 uint8_t uScale = 0;
2122 bool fBaseRegValid = false;
2123 bool fIdxRegValid = false;
2124 uint8_t iBaseReg = 0;
2125 uint8_t iIdxReg = 0;
2126 if (pVCpu->iem.s.enmEffAddrMode == IEMMODE_16BIT)
2127 {
2128 /*
2129 * Parse the ModR/M, displacement for 16-bit addressing mode.
2130 * See Intel instruction spec. Table 2-1. "16-Bit Addressing Forms with the ModR/M Byte".
2131 */
2132 uint16_t u16Disp = 0;
2133 uint8_t const offDisp = offModRm + sizeof(bRm);
2134 if ((bRm & (X86_MODRM_MOD_MASK | X86_MODRM_RM_MASK)) == 6)
2135 {
2136 /* Displacement without any registers. */
2137 IEM_DISP_GET_U16(pVCpu, u16Disp, offDisp);
2138 }
2139 else
2140 {
2141 /* Register (index and base). */
2142 switch (bRm & X86_MODRM_RM_MASK)
2143 {
2144 case 0: fBaseRegValid = true; iBaseReg = X86_GREG_xBX; fIdxRegValid = true; iIdxReg = X86_GREG_xSI; break;
2145 case 1: fBaseRegValid = true; iBaseReg = X86_GREG_xBX; fIdxRegValid = true; iIdxReg = X86_GREG_xDI; break;
2146 case 2: fBaseRegValid = true; iBaseReg = X86_GREG_xBP; fIdxRegValid = true; iIdxReg = X86_GREG_xSI; break;
2147 case 3: fBaseRegValid = true; iBaseReg = X86_GREG_xBP; fIdxRegValid = true; iIdxReg = X86_GREG_xDI; break;
2148 case 4: fIdxRegValid = true; iIdxReg = X86_GREG_xSI; break;
2149 case 5: fIdxRegValid = true; iIdxReg = X86_GREG_xDI; break;
2150 case 6: fBaseRegValid = true; iBaseReg = X86_GREG_xBP; break;
2151 case 7: fBaseRegValid = true; iBaseReg = X86_GREG_xBX; break;
2152 }
2153
2154 /* Register + displacement. */
2155 switch ((bRm >> X86_MODRM_MOD_SHIFT) & X86_MODRM_MOD_SMASK)
2156 {
2157 case 0: break;
2158 case 1: IEM_DISP_GET_S8_SX_U16(pVCpu, u16Disp, offDisp); break;
2159 case 2: IEM_DISP_GET_U16(pVCpu, u16Disp, offDisp); break;
2160 default:
2161 {
2162 /* Register addressing, handled at the beginning. */
2163 AssertMsgFailed(("ModR/M %#x implies register addressing, memory addressing expected!", bRm));
2164 break;
2165 }
2166 }
2167 }
2168
2169 Assert(!uScale); /* There's no scaling/SIB byte for 16-bit addressing. */
2170 GCPtrDisp = (int16_t)u16Disp; /* Sign-extend the displacement. */
2171 }
2172 else if (pVCpu->iem.s.enmEffAddrMode == IEMMODE_32BIT)
2173 {
2174 /*
2175 * Parse the ModR/M, SIB, displacement for 32-bit addressing mode.
2176 * See Intel instruction spec. Table 2-2. "32-Bit Addressing Forms with the ModR/M Byte".
2177 */
2178 uint32_t u32Disp = 0;
2179 if ((bRm & (X86_MODRM_MOD_MASK | X86_MODRM_RM_MASK)) == 5)
2180 {
2181 /* Displacement without any registers. */
2182 uint8_t const offDisp = offModRm + sizeof(bRm);
2183 IEM_DISP_GET_U32(pVCpu, u32Disp, offDisp);
2184 }
2185 else
2186 {
2187 /* Register (and perhaps scale, index and base). */
2188 uint8_t offDisp = offModRm + sizeof(bRm);
2189 iBaseReg = (bRm & X86_MODRM_RM_MASK);
2190 if (iBaseReg == 4)
2191 {
2192 /* An SIB byte follows the ModR/M byte, parse it. */
2193 uint8_t bSib;
2194 uint8_t const offSib = offModRm + sizeof(bRm);
2195 IEM_SIB_GET_U8(pVCpu, bSib, offSib);
2196
2197 /* A displacement may follow SIB, update its offset. */
2198 offDisp += sizeof(bSib);
2199
2200 /* Get the scale. */
2201 uScale = (bSib >> X86_SIB_SCALE_SHIFT) & X86_SIB_SCALE_SMASK;
2202
2203 /* Get the index register. */
2204 iIdxReg = (bSib >> X86_SIB_INDEX_SHIFT) & X86_SIB_INDEX_SMASK;
2205 fIdxRegValid = RT_BOOL(iIdxReg != 4);
2206
2207 /* Get the base register. */
2208 iBaseReg = bSib & X86_SIB_BASE_MASK;
2209 fBaseRegValid = true;
2210 if (iBaseReg == 5)
2211 {
2212 if ((bRm & X86_MODRM_MOD_MASK) == 0)
2213 {
2214 /* Mod is 0 implies a 32-bit displacement with no base. */
2215 fBaseRegValid = false;
2216 IEM_DISP_GET_U32(pVCpu, u32Disp, offDisp);
2217 }
2218 else
2219 {
2220 /* Mod is not 0 implies an 8-bit/32-bit displacement (handled below) with an EBP base. */
2221 iBaseReg = X86_GREG_xBP;
2222 }
2223 }
2224 }
2225
2226 /* Register + displacement. */
2227 switch ((bRm >> X86_MODRM_MOD_SHIFT) & X86_MODRM_MOD_SMASK)
2228 {
2229 case 0: /* Handled above */ break;
2230 case 1: IEM_DISP_GET_S8_SX_U32(pVCpu, u32Disp, offDisp); break;
2231 case 2: IEM_DISP_GET_U32(pVCpu, u32Disp, offDisp); break;
2232 default:
2233 {
2234 /* Register addressing, handled at the beginning. */
2235 AssertMsgFailed(("ModR/M %#x implies register addressing, memory addressing expected!", bRm));
2236 break;
2237 }
2238 }
2239 }
2240
2241 GCPtrDisp = (int32_t)u32Disp; /* Sign-extend the displacement. */
2242 }
2243 else
2244 {
2245 Assert(pVCpu->iem.s.enmEffAddrMode == IEMMODE_64BIT);
2246
2247 /*
2248 * Parse the ModR/M, SIB, displacement for 64-bit addressing mode.
2249 * See Intel instruction spec. 2.2 "IA-32e Mode".
2250 */
2251 uint64_t u64Disp = 0;
2252 bool const fRipRelativeAddr = RT_BOOL((bRm & (X86_MODRM_MOD_MASK | X86_MODRM_RM_MASK)) == 5);
2253 if (fRipRelativeAddr)
2254 {
2255 /*
2256 * RIP-relative addressing mode.
2257 *
2258 * The displacement is 32-bit signed implying an offset range of +/-2G.
2259 * See Intel instruction spec. 2.2.1.6 "RIP-Relative Addressing".
2260 */
2261 uint8_t const offDisp = offModRm + sizeof(bRm);
2262 IEM_DISP_GET_S32_SX_U64(pVCpu, u64Disp, offDisp);
2263 }
2264 else
2265 {
2266 uint8_t offDisp = offModRm + sizeof(bRm);
2267
2268 /*
2269 * Register (and perhaps scale, index and base).
2270 *
2271 * REX.B extends the most-significant bit of the base register. However, REX.B
2272 * is ignored while determining whether an SIB follows the opcode. Hence, we
2273 * shall OR any REX.B bit -after- inspecting for an SIB byte below.
2274 *
2275 * See Intel instruction spec. Table 2-5. "Special Cases of REX Encodings".
2276 */
2277 iBaseReg = (bRm & X86_MODRM_RM_MASK);
2278 if (iBaseReg == 4)
2279 {
2280 /* An SIB byte follows the ModR/M byte, parse it. Displacement (if any) follows SIB. */
2281 uint8_t bSib;
2282 uint8_t const offSib = offModRm + sizeof(bRm);
2283 IEM_SIB_GET_U8(pVCpu, bSib, offSib);
2284
2285 /* Displacement may follow SIB, update its offset. */
2286 offDisp += sizeof(bSib);
2287
2288 /* Get the scale. */
2289 uScale = (bSib >> X86_SIB_SCALE_SHIFT) & X86_SIB_SCALE_SMASK;
2290
2291 /* Get the index. */
2292 iIdxReg = ((bSib >> X86_SIB_INDEX_SHIFT) & X86_SIB_INDEX_SMASK) | pVCpu->iem.s.uRexIndex;
2293 fIdxRegValid = RT_BOOL(iIdxReg != 4); /* R12 -can- be used as an index register. */
2294
2295 /* Get the base. */
2296 iBaseReg = (bSib & X86_SIB_BASE_MASK);
2297 fBaseRegValid = true;
2298 if (iBaseReg == 5)
2299 {
2300 if ((bRm & X86_MODRM_MOD_MASK) == 0)
2301 {
2302 /* Mod is 0 implies a signed 32-bit displacement with no base. */
2303 IEM_DISP_GET_S32_SX_U64(pVCpu, u64Disp, offDisp);
2304 }
2305 else
2306 {
2307 /* Mod is non-zero implies an 8-bit/32-bit displacement (handled below) with RBP or R13 as base. */
2308 iBaseReg = pVCpu->iem.s.uRexB ? X86_GREG_x13 : X86_GREG_xBP;
2309 }
2310 }
2311 }
2312 iBaseReg |= pVCpu->iem.s.uRexB;
2313
2314 /* Register + displacement. */
2315 switch ((bRm >> X86_MODRM_MOD_SHIFT) & X86_MODRM_MOD_SMASK)
2316 {
2317 case 0: /* Handled above */ break;
2318 case 1: IEM_DISP_GET_S8_SX_U64(pVCpu, u64Disp, offDisp); break;
2319 case 2: IEM_DISP_GET_S32_SX_U64(pVCpu, u64Disp, offDisp); break;
2320 default:
2321 {
2322 /* Register addressing, handled at the beginning. */
2323 AssertMsgFailed(("ModR/M %#x implies register addressing, memory addressing expected!", bRm));
2324 break;
2325 }
2326 }
2327 }
2328
2329 GCPtrDisp = fRipRelativeAddr ? pVCpu->cpum.GstCtx.rip + u64Disp : u64Disp;
2330 }
2331
2332 /*
2333 * The primary or secondary register operand is reported in iReg2 depending
2334 * on whether the primary operand is in read/write form.
2335 */
2336 uint8_t idxReg2;
2337 if (!VMXINSTRID_IS_MODRM_PRIMARY_OP_W(uInstrId))
2338 {
2339 idxReg2 = bRm & X86_MODRM_RM_MASK;
2340 if (pVCpu->iem.s.enmEffAddrMode == IEMMODE_64BIT)
2341 idxReg2 |= pVCpu->iem.s.uRexB;
2342 }
2343 else
2344 {
2345 idxReg2 = (bRm >> X86_MODRM_REG_SHIFT) & X86_MODRM_REG_SMASK;
2346 if (pVCpu->iem.s.enmEffAddrMode == IEMMODE_64BIT)
2347 idxReg2 |= pVCpu->iem.s.uRexReg;
2348 }
2349 ExitInstrInfo.All.u2Scaling = uScale;
2350 ExitInstrInfo.All.iReg1 = 0; /* Not applicable for memory addressing. */
2351 ExitInstrInfo.All.u3AddrSize = pVCpu->iem.s.enmEffAddrMode;
2352 ExitInstrInfo.All.fIsRegOperand = 0;
2353 ExitInstrInfo.All.uOperandSize = pVCpu->iem.s.enmEffOpSize;
2354 ExitInstrInfo.All.iSegReg = pVCpu->iem.s.iEffSeg;
2355 ExitInstrInfo.All.iIdxReg = iIdxReg;
2356 ExitInstrInfo.All.fIdxRegInvalid = !fIdxRegValid;
2357 ExitInstrInfo.All.iBaseReg = iBaseReg;
2358 ExitInstrInfo.All.iIdxReg = !fBaseRegValid;
2359 ExitInstrInfo.All.iReg2 = idxReg2;
2360 }
2361
2362 /*
2363 * Handle exceptions to the norm for certain instructions.
2364 * (e.g. some instructions convey an instruction identity in place of iReg2).
2365 */
2366 switch (uExitReason)
2367 {
2368 case VMX_EXIT_GDTR_IDTR_ACCESS:
2369 {
2370 Assert(VMXINSTRID_IS_VALID(uInstrId));
2371 Assert(VMXINSTRID_GET_ID(uInstrId) == (uInstrId & 0x3));
2372 ExitInstrInfo.GdtIdt.u2InstrId = VMXINSTRID_GET_ID(uInstrId);
2373 ExitInstrInfo.GdtIdt.u2Undef0 = 0;
2374 break;
2375 }
2376
2377 case VMX_EXIT_LDTR_TR_ACCESS:
2378 {
2379 Assert(VMXINSTRID_IS_VALID(uInstrId));
2380 Assert(VMXINSTRID_GET_ID(uInstrId) == (uInstrId & 0x3));
2381 ExitInstrInfo.LdtTr.u2InstrId = VMXINSTRID_GET_ID(uInstrId);
2382 ExitInstrInfo.LdtTr.u2Undef0 = 0;
2383 break;
2384 }
2385
2386 case VMX_EXIT_RDRAND:
2387 case VMX_EXIT_RDSEED:
2388 {
2389 Assert(ExitInstrInfo.RdrandRdseed.u2OperandSize != 3);
2390 break;
2391 }
2392 }
2393
2394 /* Update displacement and return the constructed VM-exit instruction information field. */
2395 if (pGCPtrDisp)
2396 *pGCPtrDisp = GCPtrDisp;
2397
2398 return ExitInstrInfo.u;
2399}
2400
2401
2402/**
2403 * VMX VM-exit handler.
2404 *
2405 * @returns Strict VBox status code.
2406 * @retval VINF_VMX_VMEXIT when the VM-exit is successful.
2407 * @retval VINF_EM_TRIPLE_FAULT when VM-exit is unsuccessful and leads to a
2408 * triple-fault.
2409 *
2410 * @param pVCpu The cross context virtual CPU structure.
2411 * @param uExitReason The VM-exit reason.
2412 * @param u64ExitQual The Exit qualification.
2413 *
2414 * @remarks We need not necessarily have completed VM-entry before a VM-exit is
2415 * called. Failures during VM-entry can cause VM-exits as well, so we
2416 * -cannot- assert we're in VMX non-root mode here.
2417 */
2418IEM_STATIC VBOXSTRICTRC iemVmxVmexit(PVMCPUCC pVCpu, uint32_t uExitReason, uint64_t u64ExitQual)
2419{
2420# if defined(VBOX_WITH_NESTED_HWVIRT_ONLY_IN_IEM) && !defined(IN_RING3)
2421 RT_NOREF3(pVCpu, uExitReason, u64ExitQual);
2422 AssertMsgFailed(("VM-exit should only be invoked from ring-3 when nested-guest executes only in ring-3!\n"));
2423 return VERR_IEM_IPE_7;
2424# else
2425 PVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
2426
2427 /*
2428 * Import all the guest-CPU state.
2429 *
2430 * HM on returning to guest execution would have to reset up a whole lot of state
2431 * anyway, (e.g., VM-entry/VM-exit controls) and we do not ever import a part of
2432 * the state and flag reloading the entire state on re-entry. So import the entire
2433 * state here, see HMNotifyVmxNstGstVmexit() for more comments.
2434 */
2435 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_ALL);
2436
2437 /*
2438 * Ensure VM-entry interruption information valid bit is cleared.
2439 *
2440 * We do it here on every VM-exit so that even premature VM-exits (e.g. those caused
2441 * by invalid-guest state or machine-check exceptions) also clear this bit.
2442 *
2443 * See Intel spec. 27.2 "Recording VM-exit Information And Updating VM-entry control fields".
2444 */
2445 if (VMX_ENTRY_INT_INFO_IS_VALID(pVmcs->u32EntryIntInfo))
2446 pVmcs->u32EntryIntInfo &= ~VMX_ENTRY_INT_INFO_VALID;
2447
2448 /*
2449 * Update the VM-exit reason and Exit qualification.
2450 * Other VMCS read-only data fields are expected to be updated by the caller already.
2451 */
2452 pVmcs->u32RoExitReason = uExitReason;
2453 pVmcs->u64RoExitQual.u = u64ExitQual;
2454
2455 Log3(("vmexit: reason=%#RX32 qual=%#RX64 cs:rip=%04x:%#RX64 cr0=%#RX64 cr3=%#RX64 cr4=%#RX64\n", uExitReason,
2456 pVmcs->u64RoExitQual.u, pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip, pVCpu->cpum.GstCtx.cr0,
2457 pVCpu->cpum.GstCtx.cr3, pVCpu->cpum.GstCtx.cr4));
2458
2459 /*
2460 * Update the IDT-vectoring information fields if the VM-exit is triggered during delivery of an event.
2461 * See Intel spec. 27.2.4 "Information for VM Exits During Event Delivery".
2462 */
2463 {
2464 uint8_t uVector;
2465 uint32_t fFlags;
2466 uint32_t uErrCode;
2467 bool const fInEventDelivery = IEMGetCurrentXcpt(pVCpu, &uVector, &fFlags, &uErrCode, NULL /* puCr2 */);
2468 if (fInEventDelivery)
2469 {
2470 /*
2471 * A VM-exit is not considered to occur during event delivery when the VM-exit is
2472 * caused by a triple-fault or the original event results in a double-fault that
2473 * causes the VM exit directly (exception bitmap). Therefore, we must not set the
2474 * original event information into the IDT-vectoring information fields.
2475 *
2476 * See Intel spec. 27.2.4 "Information for VM Exits During Event Delivery".
2477 */
2478 if ( uExitReason != VMX_EXIT_TRIPLE_FAULT
2479 && ( uExitReason != VMX_EXIT_XCPT_OR_NMI
2480 || !VMX_EXIT_INT_INFO_IS_XCPT_DF(pVmcs->u32RoExitIntInfo)))
2481 {
2482 uint8_t const uIdtVectoringType = iemVmxGetEventType(uVector, fFlags);
2483 uint8_t const fErrCodeValid = RT_BOOL(fFlags & IEM_XCPT_FLAGS_ERR);
2484 uint32_t const uIdtVectoringInfo = RT_BF_MAKE(VMX_BF_IDT_VECTORING_INFO_VECTOR, uVector)
2485 | RT_BF_MAKE(VMX_BF_IDT_VECTORING_INFO_TYPE, uIdtVectoringType)
2486 | RT_BF_MAKE(VMX_BF_IDT_VECTORING_INFO_ERR_CODE_VALID, fErrCodeValid)
2487 | RT_BF_MAKE(VMX_BF_IDT_VECTORING_INFO_VALID, 1);
2488 iemVmxVmcsSetIdtVectoringInfo(pVCpu, uIdtVectoringInfo);
2489 iemVmxVmcsSetIdtVectoringErrCode(pVCpu, uErrCode);
2490 LogFlow(("vmexit: idt_info=%#RX32 idt_err_code=%#RX32 cr2=%#RX64\n", uIdtVectoringInfo, uErrCode,
2491 pVCpu->cpum.GstCtx.cr2));
2492 }
2493 }
2494 }
2495
2496 /* The following VMCS fields should always be zero since we don't support injecting SMIs into a guest. */
2497 Assert(pVmcs->u64RoIoRcx.u == 0);
2498 Assert(pVmcs->u64RoIoRsi.u == 0);
2499 Assert(pVmcs->u64RoIoRdi.u == 0);
2500 Assert(pVmcs->u64RoIoRip.u == 0);
2501
2502 /*
2503 * Save the guest state back into the VMCS.
2504 * We only need to save the state when the VM-entry was successful.
2505 */
2506 bool const fVmentryFailed = VMX_EXIT_REASON_HAS_ENTRY_FAILED(uExitReason);
2507 if (!fVmentryFailed)
2508 {
2509 /* We should not cause an NMI-window/interrupt-window VM-exit when injecting events as part of VM-entry. */
2510 if (!CPUMIsGuestVmxInterceptEvents(&pVCpu->cpum.GstCtx))
2511 {
2512 Assert(uExitReason != VMX_EXIT_NMI_WINDOW);
2513 Assert(uExitReason != VMX_EXIT_INT_WINDOW);
2514 }
2515
2516 /* For exception or NMI VM-exits the VM-exit interruption info. field must be valid. */
2517 Assert(uExitReason != VMX_EXIT_XCPT_OR_NMI || VMX_EXIT_INT_INFO_IS_VALID(pVmcs->u32RoExitIntInfo));
2518
2519 /*
2520 * If we support storing EFER.LMA into IA32e-mode guest field on VM-exit, we need to do that now.
2521 * See Intel spec. 27.2 "Recording VM-exit Information And Updating VM-entry Control".
2522 *
2523 * It is not clear from the Intel spec. if this is done only when VM-entry succeeds.
2524 * If a VM-exit happens before loading guest EFER, we risk restoring the host EFER.LMA
2525 * as guest-CPU state would not been modified. Hence for now, we do this only when
2526 * the VM-entry succeeded.
2527 */
2528 /** @todo r=ramshankar: Figure out if this bit gets set to host EFER.LMA on real
2529 * hardware when VM-exit fails during VM-entry (e.g. VERR_VMX_INVALID_GUEST_STATE). */
2530 if (IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fVmxExitSaveEferLma)
2531 {
2532 if (pVCpu->cpum.GstCtx.msrEFER & MSR_K6_EFER_LMA)
2533 pVmcs->u32EntryCtls |= VMX_ENTRY_CTLS_IA32E_MODE_GUEST;
2534 else
2535 pVmcs->u32EntryCtls &= ~VMX_ENTRY_CTLS_IA32E_MODE_GUEST;
2536 }
2537
2538 /*
2539 * The rest of the high bits of the VM-exit reason are only relevant when the VM-exit
2540 * occurs in enclave mode/SMM which we don't support yet.
2541 *
2542 * If we ever add support for it, we can pass just the lower bits to the functions
2543 * below, till then an assert should suffice.
2544 */
2545 Assert(!RT_HI_U16(uExitReason));
2546
2547 /* Save the guest state into the VMCS and restore guest MSRs from the auto-store guest MSR area. */
2548 iemVmxVmexitSaveGuestState(pVCpu, uExitReason);
2549 int rc = iemVmxVmexitSaveGuestAutoMsrs(pVCpu, uExitReason);
2550 if (RT_SUCCESS(rc))
2551 { /* likely */ }
2552 else
2553 return iemVmxAbort(pVCpu, VMXABORT_SAVE_GUEST_MSRS);
2554
2555 /* Clear any saved NMI-blocking state so we don't assert on next VM-entry (if it was in effect on the previous one). */
2556 pVCpu->cpum.GstCtx.hwvirt.fLocalForcedActions &= ~VMCPU_FF_BLOCK_NMIS;
2557 }
2558 else
2559 {
2560 /* Restore the NMI-blocking state if VM-entry failed due to invalid guest state or while loading MSRs. */
2561 uint32_t const uExitReasonBasic = VMX_EXIT_REASON_BASIC(uExitReason);
2562 if ( uExitReasonBasic == VMX_EXIT_ERR_INVALID_GUEST_STATE
2563 || uExitReasonBasic == VMX_EXIT_ERR_MSR_LOAD)
2564 iemVmxVmexitRestoreNmiBlockingFF(pVCpu);
2565 }
2566
2567 /*
2568 * Stop any running VMX-preemption timer if necessary.
2569 */
2570 if (pVmcs->u32PinCtls & VMX_PIN_CTLS_PREEMPT_TIMER)
2571 CPUMStopGuestVmxPremptTimer(pVCpu);
2572
2573 /*
2574 * Clear any pending VMX nested-guest force-flags.
2575 * These force-flags have no effect on (outer) guest execution and will
2576 * be re-evaluated and setup on the next nested-guest VM-entry.
2577 */
2578 VMCPU_FF_CLEAR_MASK(pVCpu, VMCPU_FF_VMX_ALL_MASK);
2579
2580 /*
2581 * We're no longer in nested-guest execution mode.
2582 *
2583 * It is important to do this prior to loading the host state because
2584 * PGM looks at fInVmxNonRootMode to determine if it needs to perform
2585 * second-level address translation while switching to host CR3.
2586 */
2587 pVCpu->cpum.GstCtx.hwvirt.vmx.fInVmxNonRootMode = false;
2588
2589 /* Restore the host (outer guest) state. */
2590 VBOXSTRICTRC rcStrict = iemVmxVmexitLoadHostState(pVCpu, uExitReason);
2591 if (RT_SUCCESS(rcStrict))
2592 {
2593 Assert(rcStrict == VINF_SUCCESS);
2594 rcStrict = VINF_VMX_VMEXIT;
2595 }
2596 else
2597 Log3(("vmexit: Loading host-state failed. uExitReason=%u rc=%Rrc\n", uExitReason, VBOXSTRICTRC_VAL(rcStrict)));
2598
2599 /* Notify HM that the current VMCS fields have been modified. */
2600 HMNotifyVmxNstGstCurrentVmcsChanged(pVCpu);
2601
2602 /* Notify HM that we've completed the VM-exit. */
2603 HMNotifyVmxNstGstVmexit(pVCpu);
2604
2605# if defined(VBOX_WITH_NESTED_HWVIRT_ONLY_IN_IEM) && defined(IN_RING3)
2606 /* Revert any IEM-only nested-guest execution policy, otherwise return rcStrict. */
2607 Log(("vmexit: Disabling IEM-only EM execution policy!\n"));
2608 int rcSched = EMR3SetExecutionPolicy(pVCpu->CTX_SUFF(pVM)->pUVM, EMEXECPOLICY_IEM_ALL, false);
2609 if (rcSched != VINF_SUCCESS)
2610 iemSetPassUpStatus(pVCpu, rcSched);
2611# endif
2612 return rcStrict;
2613# endif
2614}
2615
2616
2617/**
2618 * VMX VM-exit handler for VM-exits due to instruction execution.
2619 *
2620 * This is intended for instructions where the caller provides all the relevant
2621 * VM-exit information.
2622 *
2623 * @returns Strict VBox status code.
2624 * @param pVCpu The cross context virtual CPU structure.
2625 * @param pExitInfo Pointer to the VM-exit information.
2626 */
2627IEM_STATIC VBOXSTRICTRC iemVmxVmexitInstrWithInfo(PVMCPUCC pVCpu, PCVMXVEXITINFO pExitInfo)
2628{
2629 /*
2630 * For instructions where any of the following fields are not applicable:
2631 * - Exit qualification must be cleared.
2632 * - VM-exit instruction info. is undefined.
2633 * - Guest-linear address is undefined.
2634 * - Guest-physical address is undefined.
2635 *
2636 * The VM-exit instruction length is mandatory for all VM-exits that are caused by
2637 * instruction execution. For VM-exits that are not due to instruction execution this
2638 * field is undefined.
2639 *
2640 * In our implementation in IEM, all undefined fields are generally cleared. However,
2641 * if the caller supplies information (from say the physical CPU directly) it is
2642 * then possible that the undefined fields are not cleared.
2643 *
2644 * See Intel spec. 27.2.1 "Basic VM-Exit Information".
2645 * See Intel spec. 27.2.4 "Information for VM Exits Due to Instruction Execution".
2646 */
2647 Assert(pExitInfo);
2648 AssertMsg(pExitInfo->uReason <= VMX_EXIT_MAX, ("uReason=%u\n", pExitInfo->uReason));
2649 AssertMsg(pExitInfo->cbInstr >= 1 && pExitInfo->cbInstr <= 15,
2650 ("uReason=%u cbInstr=%u\n", pExitInfo->uReason, pExitInfo->cbInstr));
2651
2652 /* Update all the relevant fields from the VM-exit instruction information struct. */
2653 iemVmxVmcsSetExitInstrInfo(pVCpu, pExitInfo->InstrInfo.u);
2654 iemVmxVmcsSetExitGuestLinearAddr(pVCpu, pExitInfo->u64GuestLinearAddr);
2655 iemVmxVmcsSetExitGuestPhysAddr(pVCpu, pExitInfo->u64GuestPhysAddr);
2656 iemVmxVmcsSetExitInstrLen(pVCpu, pExitInfo->cbInstr);
2657
2658 /* Perform the VM-exit. */
2659 return iemVmxVmexit(pVCpu, pExitInfo->uReason, pExitInfo->u64Qual);
2660}
2661
2662
2663/**
2664 * VMX VM-exit handler for VM-exits due to instruction execution.
2665 *
2666 * This is intended for instructions that only provide the VM-exit instruction
2667 * length.
2668 *
2669 * @param pVCpu The cross context virtual CPU structure.
2670 * @param uExitReason The VM-exit reason.
2671 * @param cbInstr The instruction length in bytes.
2672 */
2673IEM_STATIC VBOXSTRICTRC iemVmxVmexitInstr(PVMCPUCC pVCpu, uint32_t uExitReason, uint8_t cbInstr)
2674{
2675 VMXVEXITINFO ExitInfo;
2676 RT_ZERO(ExitInfo);
2677 ExitInfo.uReason = uExitReason;
2678 ExitInfo.cbInstr = cbInstr;
2679
2680#ifdef VBOX_STRICT
2681 /*
2682 * To prevent us from shooting ourselves in the foot.
2683 * The follow instructions should convey more than just the instruction length.
2684 */
2685 switch (uExitReason)
2686 {
2687 case VMX_EXIT_INVEPT:
2688 case VMX_EXIT_INVPCID:
2689 case VMX_EXIT_INVVPID:
2690 case VMX_EXIT_LDTR_TR_ACCESS:
2691 case VMX_EXIT_GDTR_IDTR_ACCESS:
2692 case VMX_EXIT_VMCLEAR:
2693 case VMX_EXIT_VMPTRLD:
2694 case VMX_EXIT_VMPTRST:
2695 case VMX_EXIT_VMREAD:
2696 case VMX_EXIT_VMWRITE:
2697 case VMX_EXIT_VMXON:
2698 case VMX_EXIT_XRSTORS:
2699 case VMX_EXIT_XSAVES:
2700 case VMX_EXIT_RDRAND:
2701 case VMX_EXIT_RDSEED:
2702 case VMX_EXIT_IO_INSTR:
2703 AssertMsgFailedReturn(("Use iemVmxVmexitInstrNeedsInfo for uExitReason=%u\n", uExitReason), VERR_IEM_IPE_5);
2704 break;
2705 }
2706#endif
2707
2708 return iemVmxVmexitInstrWithInfo(pVCpu, &ExitInfo);
2709}
2710
2711
2712/**
2713 * VMX VM-exit handler for VM-exits due to instruction execution.
2714 *
2715 * This is intended for instructions that have a ModR/M byte and update the VM-exit
2716 * instruction information and Exit qualification fields.
2717 *
2718 * @param pVCpu The cross context virtual CPU structure.
2719 * @param uExitReason The VM-exit reason.
2720 * @param uInstrid The instruction identity (VMXINSTRID_XXX).
2721 * @param cbInstr The instruction length in bytes.
2722 *
2723 * @remarks Do not use this for INS/OUTS instruction.
2724 */
2725IEM_STATIC VBOXSTRICTRC iemVmxVmexitInstrNeedsInfo(PVMCPUCC pVCpu, uint32_t uExitReason, VMXINSTRID uInstrId, uint8_t cbInstr)
2726{
2727 VMXVEXITINFO ExitInfo;
2728 RT_ZERO(ExitInfo);
2729 ExitInfo.uReason = uExitReason;
2730 ExitInfo.cbInstr = cbInstr;
2731
2732 /*
2733 * Update the Exit qualification field with displacement bytes.
2734 * See Intel spec. 27.2.1 "Basic VM-Exit Information".
2735 */
2736 switch (uExitReason)
2737 {
2738 case VMX_EXIT_INVEPT:
2739 case VMX_EXIT_INVPCID:
2740 case VMX_EXIT_INVVPID:
2741 case VMX_EXIT_LDTR_TR_ACCESS:
2742 case VMX_EXIT_GDTR_IDTR_ACCESS:
2743 case VMX_EXIT_VMCLEAR:
2744 case VMX_EXIT_VMPTRLD:
2745 case VMX_EXIT_VMPTRST:
2746 case VMX_EXIT_VMREAD:
2747 case VMX_EXIT_VMWRITE:
2748 case VMX_EXIT_VMXON:
2749 case VMX_EXIT_XRSTORS:
2750 case VMX_EXIT_XSAVES:
2751 case VMX_EXIT_RDRAND:
2752 case VMX_EXIT_RDSEED:
2753 {
2754 /* Construct the VM-exit instruction information. */
2755 RTGCPTR GCPtrDisp;
2756 uint32_t const uInstrInfo = iemVmxGetExitInstrInfo(pVCpu, uExitReason, uInstrId, &GCPtrDisp);
2757
2758 /* Update the VM-exit instruction information. */
2759 ExitInfo.InstrInfo.u = uInstrInfo;
2760
2761 /* Update the Exit qualification. */
2762 ExitInfo.u64Qual = GCPtrDisp;
2763 break;
2764 }
2765
2766 default:
2767 AssertMsgFailedReturn(("Use instruction-specific handler\n"), VERR_IEM_IPE_5);
2768 break;
2769 }
2770
2771 return iemVmxVmexitInstrWithInfo(pVCpu, &ExitInfo);
2772}
2773
2774
2775/**
2776 * VMX VM-exit handler for VM-exits due to INVLPG.
2777 *
2778 * @returns Strict VBox status code.
2779 * @param pVCpu The cross context virtual CPU structure.
2780 * @param GCPtrPage The guest-linear address of the page being invalidated.
2781 * @param cbInstr The instruction length in bytes.
2782 */
2783IEM_STATIC VBOXSTRICTRC iemVmxVmexitInstrInvlpg(PVMCPUCC pVCpu, RTGCPTR GCPtrPage, uint8_t cbInstr)
2784{
2785 VMXVEXITINFO ExitInfo;
2786 RT_ZERO(ExitInfo);
2787 ExitInfo.uReason = VMX_EXIT_INVLPG;
2788 ExitInfo.cbInstr = cbInstr;
2789 ExitInfo.u64Qual = GCPtrPage;
2790 Assert(IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fLongMode || !RT_HI_U32(ExitInfo.u64Qual));
2791
2792 return iemVmxVmexitInstrWithInfo(pVCpu, &ExitInfo);
2793}
2794
2795
2796/**
2797 * VMX VM-exit handler for VM-exits due to LMSW.
2798 *
2799 * @returns Strict VBox status code.
2800 * @param pVCpu The cross context virtual CPU structure.
2801 * @param uGuestCr0 The current guest CR0.
2802 * @param pu16NewMsw The machine-status word specified in LMSW's source
2803 * operand. This will be updated depending on the VMX
2804 * guest/host CR0 mask if LMSW is not intercepted.
2805 * @param GCPtrEffDst The guest-linear address of the source operand in case
2806 * of a memory operand. For register operand, pass
2807 * NIL_RTGCPTR.
2808 * @param cbInstr The instruction length in bytes.
2809 */
2810IEM_STATIC VBOXSTRICTRC iemVmxVmexitInstrLmsw(PVMCPUCC pVCpu, uint32_t uGuestCr0, uint16_t *pu16NewMsw, RTGCPTR GCPtrEffDst,
2811 uint8_t cbInstr)
2812{
2813 Assert(pu16NewMsw);
2814
2815 uint16_t const uNewMsw = *pu16NewMsw;
2816 if (CPUMIsGuestVmxLmswInterceptSet(&pVCpu->cpum.GstCtx, uNewMsw))
2817 {
2818 Log2(("lmsw: Guest intercept -> VM-exit\n"));
2819
2820 VMXVEXITINFO ExitInfo;
2821 RT_ZERO(ExitInfo);
2822 ExitInfo.uReason = VMX_EXIT_MOV_CRX;
2823 ExitInfo.cbInstr = cbInstr;
2824
2825 bool const fMemOperand = RT_BOOL(GCPtrEffDst != NIL_RTGCPTR);
2826 if (fMemOperand)
2827 {
2828 Assert(IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fLongMode || !RT_HI_U32(GCPtrEffDst));
2829 ExitInfo.u64GuestLinearAddr = GCPtrEffDst;
2830 }
2831
2832 ExitInfo.u64Qual = RT_BF_MAKE(VMX_BF_EXIT_QUAL_CRX_REGISTER, 0) /* CR0 */
2833 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_CRX_ACCESS, VMX_EXIT_QUAL_CRX_ACCESS_LMSW)
2834 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_CRX_LMSW_OP, fMemOperand)
2835 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_CRX_LMSW_DATA, uNewMsw);
2836
2837 return iemVmxVmexitInstrWithInfo(pVCpu, &ExitInfo);
2838 }
2839
2840 /*
2841 * If LMSW did not cause a VM-exit, any CR0 bits in the range 0:3 that is set in the
2842 * CR0 guest/host mask must be left unmodified.
2843 *
2844 * See Intel Spec. 25.3 "Changes To Instruction Behavior In VMX Non-root Operation".
2845 */
2846 uint32_t const fGstHostMask = pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64Cr0Mask.u;
2847 uint32_t const fGstHostLmswMask = fGstHostMask & (X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS);
2848 *pu16NewMsw = (uGuestCr0 & fGstHostLmswMask) | (uNewMsw & ~fGstHostLmswMask);
2849
2850 return VINF_VMX_INTERCEPT_NOT_ACTIVE;
2851}
2852
2853
2854/**
2855 * VMX VM-exit handler for VM-exits due to CLTS.
2856 *
2857 * @returns Strict VBox status code.
2858 * @retval VINF_VMX_MODIFIES_BEHAVIOR if the CLTS instruction did not cause a
2859 * VM-exit but must not modify the guest CR0.TS bit.
2860 * @retval VINF_VMX_INTERCEPT_NOT_ACTIVE if the CLTS instruction did not cause a
2861 * VM-exit and modification to the guest CR0.TS bit is allowed (subject to
2862 * CR0 fixed bits in VMX operation).
2863 * @param pVCpu The cross context virtual CPU structure.
2864 * @param cbInstr The instruction length in bytes.
2865 */
2866IEM_STATIC VBOXSTRICTRC iemVmxVmexitInstrClts(PVMCPUCC pVCpu, uint8_t cbInstr)
2867{
2868 uint32_t const fGstHostMask = pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64Cr0Mask.u;
2869 uint32_t const fReadShadow = pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64Cr0ReadShadow.u;
2870
2871 /*
2872 * If CR0.TS is owned by the host:
2873 * - If CR0.TS is set in the read-shadow, we must cause a VM-exit.
2874 * - If CR0.TS is cleared in the read-shadow, no VM-exit is caused and the
2875 * CLTS instruction completes without clearing CR0.TS.
2876 *
2877 * See Intel spec. 25.1.3 "Instructions That Cause VM Exits Conditionally".
2878 */
2879 if (fGstHostMask & X86_CR0_TS)
2880 {
2881 if (fReadShadow & X86_CR0_TS)
2882 {
2883 Log2(("clts: Guest intercept -> VM-exit\n"));
2884
2885 VMXVEXITINFO ExitInfo;
2886 RT_ZERO(ExitInfo);
2887 ExitInfo.uReason = VMX_EXIT_MOV_CRX;
2888 ExitInfo.cbInstr = cbInstr;
2889 ExitInfo.u64Qual = RT_BF_MAKE(VMX_BF_EXIT_QUAL_CRX_REGISTER, 0) /* CR0 */
2890 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_CRX_ACCESS, VMX_EXIT_QUAL_CRX_ACCESS_CLTS);
2891 return iemVmxVmexitInstrWithInfo(pVCpu, &ExitInfo);
2892 }
2893
2894 return VINF_VMX_MODIFIES_BEHAVIOR;
2895 }
2896
2897 /*
2898 * If CR0.TS is not owned by the host, the CLTS instructions operates normally
2899 * and may modify CR0.TS (subject to CR0 fixed bits in VMX operation).
2900 */
2901 return VINF_VMX_INTERCEPT_NOT_ACTIVE;
2902}
2903
2904
2905/**
2906 * VMX VM-exit handler for VM-exits due to 'Mov CR0,GReg' and 'Mov CR4,GReg'
2907 * (CR0/CR4 write).
2908 *
2909 * @returns Strict VBox status code.
2910 * @param pVCpu The cross context virtual CPU structure.
2911 * @param iCrReg The control register (either CR0 or CR4).
2912 * @param uGuestCrX The current guest CR0/CR4.
2913 * @param puNewCrX Pointer to the new CR0/CR4 value. Will be updated if no
2914 * VM-exit is caused.
2915 * @param iGReg The general register from which the CR0/CR4 value is being
2916 * loaded.
2917 * @param cbInstr The instruction length in bytes.
2918 */
2919IEM_STATIC VBOXSTRICTRC iemVmxVmexitInstrMovToCr0Cr4(PVMCPUCC pVCpu, uint8_t iCrReg, uint64_t *puNewCrX, uint8_t iGReg,
2920 uint8_t cbInstr)
2921{
2922 Assert(puNewCrX);
2923 Assert(iCrReg == 0 || iCrReg == 4);
2924 Assert(iGReg < X86_GREG_COUNT);
2925
2926 uint64_t const uNewCrX = *puNewCrX;
2927 if (CPUMIsGuestVmxMovToCr0Cr4InterceptSet(&pVCpu->cpum.GstCtx, iCrReg, uNewCrX))
2928 {
2929 Log2(("mov_Cr_Rd: (CR%u) Guest intercept -> VM-exit\n", iCrReg));
2930
2931 VMXVEXITINFO ExitInfo;
2932 RT_ZERO(ExitInfo);
2933 ExitInfo.uReason = VMX_EXIT_MOV_CRX;
2934 ExitInfo.cbInstr = cbInstr;
2935 ExitInfo.u64Qual = RT_BF_MAKE(VMX_BF_EXIT_QUAL_CRX_REGISTER, iCrReg)
2936 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_CRX_ACCESS, VMX_EXIT_QUAL_CRX_ACCESS_WRITE)
2937 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_CRX_GENREG, iGReg);
2938 return iemVmxVmexitInstrWithInfo(pVCpu, &ExitInfo);
2939 }
2940
2941 /*
2942 * If the Mov-to-CR0/CR4 did not cause a VM-exit, any bits owned by the host
2943 * must not be modified the instruction.
2944 *
2945 * See Intel Spec. 25.3 "Changes To Instruction Behavior In VMX Non-root Operation".
2946 */
2947 uint64_t uGuestCrX;
2948 uint64_t fGstHostMask;
2949 if (iCrReg == 0)
2950 {
2951 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0);
2952 uGuestCrX = pVCpu->cpum.GstCtx.cr0;
2953 fGstHostMask = pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64Cr0Mask.u;
2954 }
2955 else
2956 {
2957 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR4);
2958 uGuestCrX = pVCpu->cpum.GstCtx.cr4;
2959 fGstHostMask = pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64Cr4Mask.u;
2960 }
2961
2962 *puNewCrX = (uGuestCrX & fGstHostMask) | (*puNewCrX & ~fGstHostMask);
2963 return VINF_VMX_INTERCEPT_NOT_ACTIVE;
2964}
2965
2966
2967/**
2968 * VMX VM-exit handler for VM-exits due to 'Mov GReg,CR3' (CR3 read).
2969 *
2970 * @returns VBox strict status code.
2971 * @param pVCpu The cross context virtual CPU structure.
2972 * @param iGReg The general register to which the CR3 value is being stored.
2973 * @param cbInstr The instruction length in bytes.
2974 */
2975IEM_STATIC VBOXSTRICTRC iemVmxVmexitInstrMovFromCr3(PVMCPUCC pVCpu, uint8_t iGReg, uint8_t cbInstr)
2976{
2977 Assert(iGReg < X86_GREG_COUNT);
2978 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR3);
2979
2980 /*
2981 * If the CR3-store exiting control is set, we must cause a VM-exit.
2982 * See Intel spec. 25.1.3 "Instructions That Cause VM Exits Conditionally".
2983 */
2984 if (pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32ProcCtls & VMX_PROC_CTLS_CR3_STORE_EXIT)
2985 {
2986 Log2(("mov_Rd_Cr: (CR3) Guest intercept -> VM-exit\n"));
2987
2988 VMXVEXITINFO ExitInfo;
2989 RT_ZERO(ExitInfo);
2990 ExitInfo.uReason = VMX_EXIT_MOV_CRX;
2991 ExitInfo.cbInstr = cbInstr;
2992 ExitInfo.u64Qual = RT_BF_MAKE(VMX_BF_EXIT_QUAL_CRX_REGISTER, 3) /* CR3 */
2993 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_CRX_ACCESS, VMX_EXIT_QUAL_CRX_ACCESS_READ)
2994 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_CRX_GENREG, iGReg);
2995 return iemVmxVmexitInstrWithInfo(pVCpu, &ExitInfo);
2996 }
2997
2998 return VINF_VMX_INTERCEPT_NOT_ACTIVE;
2999}
3000
3001
3002/**
3003 * VMX VM-exit handler for VM-exits due to 'Mov CR3,GReg' (CR3 write).
3004 *
3005 * @returns VBox strict status code.
3006 * @param pVCpu The cross context virtual CPU structure.
3007 * @param uNewCr3 The new CR3 value.
3008 * @param iGReg The general register from which the CR3 value is being
3009 * loaded.
3010 * @param cbInstr The instruction length in bytes.
3011 */
3012IEM_STATIC VBOXSTRICTRC iemVmxVmexitInstrMovToCr3(PVMCPUCC pVCpu, uint64_t uNewCr3, uint8_t iGReg, uint8_t cbInstr)
3013{
3014 Assert(iGReg < X86_GREG_COUNT);
3015
3016 /*
3017 * If the CR3-load exiting control is set and the new CR3 value does not
3018 * match any of the CR3-target values in the VMCS, we must cause a VM-exit.
3019 *
3020 * See Intel spec. 25.1.3 "Instructions That Cause VM Exits Conditionally".
3021 */
3022 if (CPUMIsGuestVmxMovToCr3InterceptSet(pVCpu, uNewCr3))
3023 {
3024 Log2(("mov_Cr_Rd: (CR3) Guest intercept -> VM-exit\n"));
3025
3026 VMXVEXITINFO ExitInfo;
3027 RT_ZERO(ExitInfo);
3028 ExitInfo.uReason = VMX_EXIT_MOV_CRX;
3029 ExitInfo.cbInstr = cbInstr;
3030 ExitInfo.u64Qual = RT_BF_MAKE(VMX_BF_EXIT_QUAL_CRX_REGISTER, 3) /* CR3 */
3031 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_CRX_ACCESS, VMX_EXIT_QUAL_CRX_ACCESS_WRITE)
3032 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_CRX_GENREG, iGReg);
3033 return iemVmxVmexitInstrWithInfo(pVCpu, &ExitInfo);
3034 }
3035
3036 return VINF_VMX_INTERCEPT_NOT_ACTIVE;
3037}
3038
3039
3040/**
3041 * VMX VM-exit handler for VM-exits due to 'Mov GReg,CR8' (CR8 read).
3042 *
3043 * @returns VBox strict status code.
3044 * @param pVCpu The cross context virtual CPU structure.
3045 * @param iGReg The general register to which the CR8 value is being stored.
3046 * @param cbInstr The instruction length in bytes.
3047 */
3048IEM_STATIC VBOXSTRICTRC iemVmxVmexitInstrMovFromCr8(PVMCPUCC pVCpu, uint8_t iGReg, uint8_t cbInstr)
3049{
3050 Assert(iGReg < X86_GREG_COUNT);
3051
3052 /*
3053 * If the CR8-store exiting control is set, we must cause a VM-exit.
3054 * See Intel spec. 25.1.3 "Instructions That Cause VM Exits Conditionally".
3055 */
3056 if (pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32ProcCtls & VMX_PROC_CTLS_CR8_STORE_EXIT)
3057 {
3058 Log2(("mov_Rd_Cr: (CR8) Guest intercept -> VM-exit\n"));
3059
3060 VMXVEXITINFO ExitInfo;
3061 RT_ZERO(ExitInfo);
3062 ExitInfo.uReason = VMX_EXIT_MOV_CRX;
3063 ExitInfo.cbInstr = cbInstr;
3064 ExitInfo.u64Qual = RT_BF_MAKE(VMX_BF_EXIT_QUAL_CRX_REGISTER, 8) /* CR8 */
3065 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_CRX_ACCESS, VMX_EXIT_QUAL_CRX_ACCESS_READ)
3066 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_CRX_GENREG, iGReg);
3067 return iemVmxVmexitInstrWithInfo(pVCpu, &ExitInfo);
3068 }
3069
3070 return VINF_VMX_INTERCEPT_NOT_ACTIVE;
3071}
3072
3073
3074/**
3075 * VMX VM-exit handler for VM-exits due to 'Mov CR8,GReg' (CR8 write).
3076 *
3077 * @returns VBox strict status code.
3078 * @param pVCpu The cross context virtual CPU structure.
3079 * @param iGReg The general register from which the CR8 value is being
3080 * loaded.
3081 * @param cbInstr The instruction length in bytes.
3082 */
3083IEM_STATIC VBOXSTRICTRC iemVmxVmexitInstrMovToCr8(PVMCPUCC pVCpu, uint8_t iGReg, uint8_t cbInstr)
3084{
3085 Assert(iGReg < X86_GREG_COUNT);
3086
3087 /*
3088 * If the CR8-load exiting control is set, we must cause a VM-exit.
3089 * See Intel spec. 25.1.3 "Instructions That Cause VM Exits Conditionally".
3090 */
3091 if (pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32ProcCtls & VMX_PROC_CTLS_CR8_LOAD_EXIT)
3092 {
3093 Log2(("mov_Cr_Rd: (CR8) Guest intercept -> VM-exit\n"));
3094
3095 VMXVEXITINFO ExitInfo;
3096 RT_ZERO(ExitInfo);
3097 ExitInfo.uReason = VMX_EXIT_MOV_CRX;
3098 ExitInfo.cbInstr = cbInstr;
3099 ExitInfo.u64Qual = RT_BF_MAKE(VMX_BF_EXIT_QUAL_CRX_REGISTER, 8) /* CR8 */
3100 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_CRX_ACCESS, VMX_EXIT_QUAL_CRX_ACCESS_WRITE)
3101 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_CRX_GENREG, iGReg);
3102 return iemVmxVmexitInstrWithInfo(pVCpu, &ExitInfo);
3103 }
3104
3105 return VINF_VMX_INTERCEPT_NOT_ACTIVE;
3106}
3107
3108
3109/**
3110 * VMX VM-exit handler for VM-exits due to 'Mov DRx,GReg' (DRx write) and 'Mov
3111 * GReg,DRx' (DRx read).
3112 *
3113 * @returns VBox strict status code.
3114 * @param pVCpu The cross context virtual CPU structure.
3115 * @param uInstrid The instruction identity (VMXINSTRID_MOV_TO_DRX or
3116 * VMXINSTRID_MOV_FROM_DRX).
3117 * @param iDrReg The debug register being accessed.
3118 * @param iGReg The general register to/from which the DRx value is being
3119 * store/loaded.
3120 * @param cbInstr The instruction length in bytes.
3121 */
3122IEM_STATIC VBOXSTRICTRC iemVmxVmexitInstrMovDrX(PVMCPUCC pVCpu, VMXINSTRID uInstrId, uint8_t iDrReg, uint8_t iGReg,
3123 uint8_t cbInstr)
3124{
3125 Assert(iDrReg <= 7);
3126 Assert(uInstrId == VMXINSTRID_MOV_TO_DRX || uInstrId == VMXINSTRID_MOV_FROM_DRX);
3127 Assert(iGReg < X86_GREG_COUNT);
3128
3129 if (pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32ProcCtls & VMX_PROC_CTLS_MOV_DR_EXIT)
3130 {
3131 uint32_t const uDirection = uInstrId == VMXINSTRID_MOV_TO_DRX ? VMX_EXIT_QUAL_DRX_DIRECTION_WRITE
3132 : VMX_EXIT_QUAL_DRX_DIRECTION_READ;
3133 VMXVEXITINFO ExitInfo;
3134 RT_ZERO(ExitInfo);
3135 ExitInfo.uReason = VMX_EXIT_MOV_DRX;
3136 ExitInfo.cbInstr = cbInstr;
3137 ExitInfo.u64Qual = RT_BF_MAKE(VMX_BF_EXIT_QUAL_DRX_REGISTER, iDrReg)
3138 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_DRX_DIRECTION, uDirection)
3139 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_DRX_GENREG, iGReg);
3140 return iemVmxVmexitInstrWithInfo(pVCpu, &ExitInfo);
3141 }
3142
3143 return VINF_VMX_INTERCEPT_NOT_ACTIVE;
3144}
3145
3146
3147/**
3148 * VMX VM-exit handler for VM-exits due to I/O instructions (IN and OUT).
3149 *
3150 * @returns VBox strict status code.
3151 * @param pVCpu The cross context virtual CPU structure.
3152 * @param uInstrId The VM-exit instruction identity (VMXINSTRID_IO_IN or
3153 * VMXINSTRID_IO_OUT).
3154 * @param u16Port The I/O port being accessed.
3155 * @param fImm Whether the I/O port was encoded using an immediate operand
3156 * or the implicit DX register.
3157 * @param cbAccess The size of the I/O access in bytes (1, 2 or 4 bytes).
3158 * @param cbInstr The instruction length in bytes.
3159 */
3160IEM_STATIC VBOXSTRICTRC iemVmxVmexitInstrIo(PVMCPUCC pVCpu, VMXINSTRID uInstrId, uint16_t u16Port, bool fImm, uint8_t cbAccess,
3161 uint8_t cbInstr)
3162{
3163 Assert(uInstrId == VMXINSTRID_IO_IN || uInstrId == VMXINSTRID_IO_OUT);
3164 Assert(cbAccess == 1 || cbAccess == 2 || cbAccess == 4);
3165
3166 bool const fIntercept = CPUMIsGuestVmxIoInterceptSet(pVCpu, u16Port, cbAccess);
3167 if (fIntercept)
3168 {
3169 uint32_t const uDirection = uInstrId == VMXINSTRID_IO_IN ? VMX_EXIT_QUAL_IO_DIRECTION_IN
3170 : VMX_EXIT_QUAL_IO_DIRECTION_OUT;
3171 VMXVEXITINFO ExitInfo;
3172 RT_ZERO(ExitInfo);
3173 ExitInfo.uReason = VMX_EXIT_IO_INSTR;
3174 ExitInfo.cbInstr = cbInstr;
3175 ExitInfo.u64Qual = RT_BF_MAKE(VMX_BF_EXIT_QUAL_IO_WIDTH, cbAccess - 1)
3176 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_IO_DIRECTION, uDirection)
3177 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_IO_ENCODING, fImm)
3178 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_IO_PORT, u16Port);
3179 return iemVmxVmexitInstrWithInfo(pVCpu, &ExitInfo);
3180 }
3181
3182 return VINF_VMX_INTERCEPT_NOT_ACTIVE;
3183}
3184
3185
3186/**
3187 * VMX VM-exit handler for VM-exits due to string I/O instructions (INS and OUTS).
3188 *
3189 * @returns VBox strict status code.
3190 * @param pVCpu The cross context virtual CPU structure.
3191 * @param uInstrId The VM-exit instruction identity (VMXINSTRID_IO_INS or
3192 * VMXINSTRID_IO_OUTS).
3193 * @param u16Port The I/O port being accessed.
3194 * @param cbAccess The size of the I/O access in bytes (1, 2 or 4 bytes).
3195 * @param fRep Whether the instruction has a REP prefix or not.
3196 * @param ExitInstrInfo The VM-exit instruction info. field.
3197 * @param cbInstr The instruction length in bytes.
3198 */
3199IEM_STATIC VBOXSTRICTRC iemVmxVmexitInstrStrIo(PVMCPUCC pVCpu, VMXINSTRID uInstrId, uint16_t u16Port, uint8_t cbAccess, bool fRep,
3200 VMXEXITINSTRINFO ExitInstrInfo, uint8_t cbInstr)
3201{
3202 Assert(uInstrId == VMXINSTRID_IO_INS || uInstrId == VMXINSTRID_IO_OUTS);
3203 Assert(cbAccess == 1 || cbAccess == 2 || cbAccess == 4);
3204 Assert(ExitInstrInfo.StrIo.iSegReg < X86_SREG_COUNT);
3205 Assert(ExitInstrInfo.StrIo.u3AddrSize == 0 || ExitInstrInfo.StrIo.u3AddrSize == 1 || ExitInstrInfo.StrIo.u3AddrSize == 2);
3206 Assert(uInstrId != VMXINSTRID_IO_INS || ExitInstrInfo.StrIo.iSegReg == X86_SREG_ES);
3207
3208 bool const fIntercept = CPUMIsGuestVmxIoInterceptSet(pVCpu, u16Port, cbAccess);
3209 if (fIntercept)
3210 {
3211 /*
3212 * Figure out the guest-linear address and the direction bit (INS/OUTS).
3213 */
3214 /** @todo r=ramshankar: Is there something in IEM that already does this? */
3215 static uint64_t const s_auAddrSizeMasks[] = { UINT64_C(0xffff), UINT64_C(0xffffffff), UINT64_C(0xffffffffffffffff) };
3216 uint8_t const iSegReg = ExitInstrInfo.StrIo.iSegReg;
3217 uint8_t const uAddrSize = ExitInstrInfo.StrIo.u3AddrSize;
3218 uint64_t const uAddrSizeMask = s_auAddrSizeMasks[uAddrSize];
3219
3220 uint32_t uDirection;
3221 uint64_t uGuestLinearAddr;
3222 if (uInstrId == VMXINSTRID_IO_INS)
3223 {
3224 uDirection = VMX_EXIT_QUAL_IO_DIRECTION_IN;
3225 uGuestLinearAddr = pVCpu->cpum.GstCtx.aSRegs[iSegReg].u64Base + (pVCpu->cpum.GstCtx.rdi & uAddrSizeMask);
3226 }
3227 else
3228 {
3229 uDirection = VMX_EXIT_QUAL_IO_DIRECTION_OUT;
3230 uGuestLinearAddr = pVCpu->cpum.GstCtx.aSRegs[iSegReg].u64Base + (pVCpu->cpum.GstCtx.rsi & uAddrSizeMask);
3231 }
3232
3233 /*
3234 * If the segment is unusable, the guest-linear address in undefined.
3235 * We shall clear it for consistency.
3236 *
3237 * See Intel spec. 27.2.1 "Basic VM-Exit Information".
3238 */
3239 if (pVCpu->cpum.GstCtx.aSRegs[iSegReg].Attr.n.u1Unusable)
3240 uGuestLinearAddr = 0;
3241
3242 VMXVEXITINFO ExitInfo;
3243 RT_ZERO(ExitInfo);
3244 ExitInfo.uReason = VMX_EXIT_IO_INSTR;
3245 ExitInfo.cbInstr = cbInstr;
3246 ExitInfo.u64GuestLinearAddr = uGuestLinearAddr;
3247 ExitInfo.u64Qual = RT_BF_MAKE(VMX_BF_EXIT_QUAL_IO_WIDTH, cbAccess - 1)
3248 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_IO_DIRECTION, uDirection)
3249 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_IO_IS_STRING, 1)
3250 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_IO_IS_REP, fRep)
3251 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_IO_ENCODING, VMX_EXIT_QUAL_IO_ENCODING_DX)
3252 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_IO_PORT, u16Port);
3253 if (IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fVmxInsOutInfo)
3254 ExitInfo.InstrInfo = ExitInstrInfo;
3255 return iemVmxVmexitInstrWithInfo(pVCpu, &ExitInfo);
3256 }
3257
3258 return VINF_VMX_INTERCEPT_NOT_ACTIVE;
3259}
3260
3261
3262/**
3263 * VMX VM-exit handler for VM-exits due to MWAIT.
3264 *
3265 * @returns VBox strict status code.
3266 * @param pVCpu The cross context virtual CPU structure.
3267 * @param fMonitorHwArmed Whether the address-range monitor hardware is armed.
3268 * @param cbInstr The instruction length in bytes.
3269 */
3270IEM_STATIC VBOXSTRICTRC iemVmxVmexitInstrMwait(PVMCPUCC pVCpu, bool fMonitorHwArmed, uint8_t cbInstr)
3271{
3272 VMXVEXITINFO ExitInfo;
3273 RT_ZERO(ExitInfo);
3274 ExitInfo.uReason = VMX_EXIT_MWAIT;
3275 ExitInfo.cbInstr = cbInstr;
3276 ExitInfo.u64Qual = fMonitorHwArmed;
3277 return iemVmxVmexitInstrWithInfo(pVCpu, &ExitInfo);
3278}
3279
3280
3281/**
3282 * VMX VM-exit handler for VM-exits due to PAUSE.
3283 *
3284 * @returns VBox strict status code.
3285 * @param pVCpu The cross context virtual CPU structure.
3286 * @param cbInstr The instruction length in bytes.
3287 */
3288IEM_STATIC VBOXSTRICTRC iemVmxVmexitInstrPause(PVMCPUCC pVCpu, uint8_t cbInstr)
3289{
3290 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
3291
3292 /*
3293 * The PAUSE VM-exit is controlled by the "PAUSE exiting" control and the
3294 * "PAUSE-loop exiting" control.
3295 *
3296 * The PLE-Gap is the maximum number of TSC ticks between two successive executions of
3297 * the PAUSE instruction before we cause a VM-exit. The PLE-Window is the maximum amount
3298 * of TSC ticks the guest is allowed to execute in a pause loop before we must cause
3299 * a VM-exit.
3300 *
3301 * See Intel spec. 24.6.13 "Controls for PAUSE-Loop Exiting".
3302 * See Intel spec. 25.1.3 "Instructions That Cause VM Exits Conditionally".
3303 */
3304 bool fIntercept = false;
3305 if (pVmcs->u32ProcCtls & VMX_PROC_CTLS_PAUSE_EXIT)
3306 fIntercept = true;
3307 else if ( (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_PAUSE_LOOP_EXIT)
3308 && pVCpu->iem.s.uCpl == 0)
3309 {
3310 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_HWVIRT);
3311
3312 /*
3313 * A previous-PAUSE-tick value of 0 is used to identify the first time
3314 * execution of a PAUSE instruction after VM-entry at CPL 0. We must
3315 * consider this to be the first execution of PAUSE in a loop according
3316 * to the Intel.
3317 *
3318 * All subsequent records for the previous-PAUSE-tick we ensure that it
3319 * cannot be zero by OR'ing 1 to rule out the TSC wrap-around cases at 0.
3320 */
3321 uint64_t *puFirstPauseLoopTick = &pVCpu->cpum.GstCtx.hwvirt.vmx.uFirstPauseLoopTick;
3322 uint64_t *puPrevPauseTick = &pVCpu->cpum.GstCtx.hwvirt.vmx.uPrevPauseTick;
3323 uint64_t const uTick = TMCpuTickGet(pVCpu);
3324 uint32_t const uPleGap = pVmcs->u32PleGap;
3325 uint32_t const uPleWindow = pVmcs->u32PleWindow;
3326 if ( *puPrevPauseTick == 0
3327 || uTick - *puPrevPauseTick > uPleGap)
3328 *puFirstPauseLoopTick = uTick;
3329 else if (uTick - *puFirstPauseLoopTick > uPleWindow)
3330 fIntercept = true;
3331
3332 *puPrevPauseTick = uTick | 1;
3333 }
3334
3335 if (fIntercept)
3336 return iemVmxVmexitInstr(pVCpu, VMX_EXIT_PAUSE, cbInstr);
3337
3338 return VINF_VMX_INTERCEPT_NOT_ACTIVE;
3339}
3340
3341
3342/**
3343 * VMX VM-exit handler for VM-exits due to task switches.
3344 *
3345 * @returns VBox strict status code.
3346 * @param pVCpu The cross context virtual CPU structure.
3347 * @param enmTaskSwitch The cause of the task switch.
3348 * @param SelNewTss The selector of the new TSS.
3349 * @param cbInstr The instruction length in bytes.
3350 */
3351IEM_STATIC VBOXSTRICTRC iemVmxVmexitTaskSwitch(PVMCPUCC pVCpu, IEMTASKSWITCH enmTaskSwitch, RTSEL SelNewTss, uint8_t cbInstr)
3352{
3353 /*
3354 * Task-switch VM-exits are unconditional and provide the Exit qualification.
3355 *
3356 * If the cause of the task switch is due to execution of CALL, IRET or the JMP
3357 * instruction or delivery of the exception generated by one of these instructions
3358 * lead to a task switch through a task gate in the IDT, we need to provide the
3359 * VM-exit instruction length. Any other means of invoking a task switch VM-exit
3360 * leaves the VM-exit instruction length field undefined.
3361 *
3362 * See Intel spec. 25.2 "Other Causes Of VM Exits".
3363 * See Intel spec. 27.2.4 "Information for VM Exits Due to Instruction Execution".
3364 */
3365 Assert(cbInstr <= 15);
3366
3367 uint8_t uType;
3368 switch (enmTaskSwitch)
3369 {
3370 case IEMTASKSWITCH_CALL: uType = VMX_EXIT_QUAL_TASK_SWITCH_TYPE_CALL; break;
3371 case IEMTASKSWITCH_IRET: uType = VMX_EXIT_QUAL_TASK_SWITCH_TYPE_IRET; break;
3372 case IEMTASKSWITCH_JUMP: uType = VMX_EXIT_QUAL_TASK_SWITCH_TYPE_JMP; break;
3373 case IEMTASKSWITCH_INT_XCPT: uType = VMX_EXIT_QUAL_TASK_SWITCH_TYPE_IDT; break;
3374 IEM_NOT_REACHED_DEFAULT_CASE_RET();
3375 }
3376
3377 uint64_t const u64ExitQual = RT_BF_MAKE(VMX_BF_EXIT_QUAL_TASK_SWITCH_NEW_TSS, SelNewTss)
3378 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_TASK_SWITCH_SOURCE, uType);
3379 iemVmxVmcsSetExitInstrLen(pVCpu, cbInstr);
3380 return iemVmxVmexit(pVCpu, VMX_EXIT_TASK_SWITCH, u64ExitQual);
3381}
3382
3383
3384/**
3385 * VMX VM-exit handler for trap-like VM-exits.
3386 *
3387 * @returns VBox strict status code.
3388 * @param pVCpu The cross context virtual CPU structure.
3389 * @param pExitInfo Pointer to the VM-exit information.
3390 * @param pExitEventInfo Pointer to the VM-exit event information.
3391 */
3392IEM_STATIC VBOXSTRICTRC iemVmxVmexitTrapLikeWithInfo(PVMCPUCC pVCpu, PCVMXVEXITINFO pExitInfo)
3393{
3394 Assert(VMXIsVmexitTrapLike(pExitInfo->uReason));
3395 iemVmxVmcsSetGuestPendingDbgXcpts(pVCpu, pExitInfo->u64GuestPendingDbgXcpts);
3396 return iemVmxVmexit(pVCpu, pExitInfo->uReason, pExitInfo->u64Qual);
3397}
3398
3399
3400/**
3401 * VMX VM-exit handler for VM-exits due to task switches.
3402 *
3403 * This is intended for task switches where the caller provides all the relevant
3404 * VM-exit information.
3405 *
3406 * @returns VBox strict status code.
3407 * @param pVCpu The cross context virtual CPU structure.
3408 * @param pExitInfo Pointer to the VM-exit information.
3409 * @param pExitEventInfo Pointer to the VM-exit event information.
3410 */
3411IEM_STATIC VBOXSTRICTRC iemVmxVmexitTaskSwitchWithInfo(PVMCPUCC pVCpu, PCVMXVEXITINFO pExitInfo,
3412 PCVMXVEXITEVENTINFO pExitEventInfo)
3413{
3414 Assert(pExitInfo->uReason == VMX_EXIT_TASK_SWITCH);
3415 iemVmxVmcsSetExitInstrLen(pVCpu, pExitInfo->cbInstr);
3416 iemVmxVmcsSetIdtVectoringInfo(pVCpu, pExitEventInfo->uIdtVectoringInfo);
3417 iemVmxVmcsSetIdtVectoringErrCode(pVCpu, pExitEventInfo->uIdtVectoringErrCode);
3418 return iemVmxVmexit(pVCpu, VMX_EXIT_TASK_SWITCH, pExitInfo->u64Qual);
3419}
3420
3421
3422/**
3423 * VMX VM-exit handler for VM-exits due to expiring of the preemption timer.
3424 *
3425 * @returns VBox strict status code.
3426 * @param pVCpu The cross context virtual CPU structure.
3427 */
3428IEM_STATIC VBOXSTRICTRC iemVmxVmexitPreemptTimer(PVMCPUCC pVCpu)
3429{
3430 Assert(VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_VMX_PREEMPT_TIMER));
3431 Assert(pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32PinCtls & VMX_PIN_CTLS_PREEMPT_TIMER);
3432
3433 /* Import the hardware virtualization state (for nested-guest VM-entry TSC-tick). */
3434 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_HWVIRT);
3435
3436 /* Save the VMX-preemption timer value (of 0) back in to the VMCS if the CPU supports this feature. */
3437 if (pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32ExitCtls & VMX_EXIT_CTLS_SAVE_PREEMPT_TIMER)
3438 pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32PreemptTimer = 0;
3439
3440 /* Cause the VMX-preemption timer VM-exit. The Exit qualification MBZ. */
3441 return iemVmxVmexit(pVCpu, VMX_EXIT_PREEMPT_TIMER, 0 /* u64ExitQual */);
3442}
3443
3444
3445/**
3446 * VMX VM-exit handler for VM-exits due to external interrupts.
3447 *
3448 * @returns VBox strict status code.
3449 * @param pVCpu The cross context virtual CPU structure.
3450 * @param uVector The external interrupt vector (pass 0 if the interrupt
3451 * is still pending since we typically won't know the
3452 * vector).
3453 * @param fIntPending Whether the external interrupt is pending or
3454 * acknowledged in the interrupt controller.
3455 */
3456IEM_STATIC VBOXSTRICTRC iemVmxVmexitExtInt(PVMCPUCC pVCpu, uint8_t uVector, bool fIntPending)
3457{
3458 Assert(!fIntPending || uVector == 0);
3459
3460 /* The VM-exit is subject to "External interrupt exiting" being set. */
3461 if (pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32PinCtls & VMX_PIN_CTLS_EXT_INT_EXIT)
3462 {
3463 if (fIntPending)
3464 {
3465 /*
3466 * If the interrupt is pending and we don't need to acknowledge the
3467 * interrupt on VM-exit, cause the VM-exit immediately.
3468 *
3469 * See Intel spec 25.2 "Other Causes Of VM Exits".
3470 */
3471 if (!(pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32ExitCtls & VMX_EXIT_CTLS_ACK_EXT_INT))
3472 return iemVmxVmexit(pVCpu, VMX_EXIT_EXT_INT, 0 /* u64ExitQual */);
3473
3474 /*
3475 * If the interrupt is pending and we -do- need to acknowledge the interrupt
3476 * on VM-exit, postpone VM-exit till after the interrupt controller has been
3477 * acknowledged that the interrupt has been consumed. Callers would have to call
3478 * us again after getting the vector (and ofc, with fIntPending with false).
3479 */
3480 return VINF_VMX_INTERCEPT_NOT_ACTIVE;
3481 }
3482
3483 /*
3484 * If the interrupt is no longer pending (i.e. it has been acknowledged) and the
3485 * "External interrupt exiting" and "Acknowledge interrupt on VM-exit" controls are
3486 * all set, we need to record the vector of the external interrupt in the
3487 * VM-exit interruption information field. Otherwise, mark this field as invalid.
3488 *
3489 * See Intel spec. 27.2.2 "Information for VM Exits Due to Vectored Events".
3490 */
3491 uint32_t uExitIntInfo;
3492 if (pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32ExitCtls & VMX_EXIT_CTLS_ACK_EXT_INT)
3493 {
3494 bool const fNmiUnblocking = pVCpu->cpum.GstCtx.hwvirt.vmx.fNmiUnblockingIret;
3495 uExitIntInfo = RT_BF_MAKE(VMX_BF_EXIT_INT_INFO_VECTOR, uVector)
3496 | RT_BF_MAKE(VMX_BF_EXIT_INT_INFO_TYPE, VMX_EXIT_INT_INFO_TYPE_EXT_INT)
3497 | RT_BF_MAKE(VMX_BF_EXIT_INT_INFO_NMI_UNBLOCK_IRET, fNmiUnblocking)
3498 | RT_BF_MAKE(VMX_BF_EXIT_INT_INFO_VALID, 1);
3499 }
3500 else
3501 uExitIntInfo = 0;
3502 iemVmxVmcsSetExitIntInfo(pVCpu, uExitIntInfo);
3503
3504 /*
3505 * Cause the VM-exit whether or not the vector has been stored
3506 * in the VM-exit interruption-information field.
3507 */
3508 return iemVmxVmexit(pVCpu, VMX_EXIT_EXT_INT, 0 /* u64ExitQual */);
3509 }
3510
3511 return VINF_VMX_INTERCEPT_NOT_ACTIVE;
3512}
3513
3514
3515/**
3516 * VMX VM-exit handler for VM-exits due to a double fault caused during delivery of
3517 * an event.
3518 *
3519 * @returns VBox strict status code.
3520 * @param pVCpu The cross context virtual CPU structure.
3521 */
3522IEM_STATIC VBOXSTRICTRC iemVmxVmexitEventDoubleFault(PVMCPUCC pVCpu)
3523{
3524 uint32_t const fXcptBitmap = pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32XcptBitmap;
3525 if (fXcptBitmap & RT_BIT(X86_XCPT_DF))
3526 {
3527 /*
3528 * The NMI-unblocking due to IRET field need not be set for double faults.
3529 * See Intel spec. 31.7.1.2 "Resuming Guest Software After Handling An Exception".
3530 */
3531 uint32_t const uExitIntInfo = RT_BF_MAKE(VMX_BF_EXIT_INT_INFO_VECTOR, X86_XCPT_DF)
3532 | RT_BF_MAKE(VMX_BF_EXIT_INT_INFO_TYPE, VMX_EXIT_INT_INFO_TYPE_HW_XCPT)
3533 | RT_BF_MAKE(VMX_BF_EXIT_INT_INFO_ERR_CODE_VALID, 1)
3534 | RT_BF_MAKE(VMX_BF_EXIT_INT_INFO_NMI_UNBLOCK_IRET, 0)
3535 | RT_BF_MAKE(VMX_BF_EXIT_INT_INFO_VALID, 1);
3536 iemVmxVmcsSetExitIntInfo(pVCpu, uExitIntInfo);
3537 return iemVmxVmexit(pVCpu, VMX_EXIT_XCPT_OR_NMI, 0 /* u64ExitQual */);
3538 }
3539
3540 return VINF_VMX_INTERCEPT_NOT_ACTIVE;
3541}
3542
3543
3544/**
3545 * VMX VM-exit handler for VM-exit due to delivery of an events.
3546 *
3547 * This is intended for VM-exit due to exceptions or NMIs where the caller provides
3548 * all the relevant VM-exit information.
3549 *
3550 * @returns VBox strict status code.
3551 * @param pVCpu The cross context virtual CPU structure.
3552 * @param pExitInfo Pointer to the VM-exit information.
3553 * @param pExitEventInfo Pointer to the VM-exit event information.
3554 */
3555IEM_STATIC VBOXSTRICTRC iemVmxVmexitEventWithInfo(PVMCPUCC pVCpu, PCVMXVEXITINFO pExitInfo, PCVMXVEXITEVENTINFO pExitEventInfo)
3556{
3557 Assert(pExitInfo);
3558 Assert(pExitEventInfo);
3559 Assert(pExitInfo->uReason == VMX_EXIT_XCPT_OR_NMI);
3560 Assert(VMX_EXIT_INT_INFO_IS_VALID(pExitEventInfo->uExitIntInfo));
3561
3562 iemVmxVmcsSetExitInstrLen(pVCpu, pExitInfo->cbInstr);
3563 iemVmxVmcsSetExitIntInfo(pVCpu, pExitEventInfo->uExitIntInfo);
3564 iemVmxVmcsSetExitIntErrCode(pVCpu, pExitEventInfo->uExitIntErrCode);
3565 iemVmxVmcsSetIdtVectoringInfo(pVCpu, pExitEventInfo->uIdtVectoringInfo);
3566 iemVmxVmcsSetIdtVectoringErrCode(pVCpu, pExitEventInfo->uIdtVectoringErrCode);
3567 return iemVmxVmexit(pVCpu, VMX_EXIT_XCPT_OR_NMI, pExitInfo->u64Qual);
3568}
3569
3570
3571/**
3572 * VMX VM-exit handler for VM-exits due to delivery of an event.
3573 *
3574 * @returns VBox strict status code.
3575 * @param pVCpu The cross context virtual CPU structure.
3576 * @param uVector The interrupt / exception vector.
3577 * @param fFlags The flags (see IEM_XCPT_FLAGS_XXX).
3578 * @param uErrCode The error code associated with the event.
3579 * @param uCr2 The CR2 value in case of a \#PF exception.
3580 * @param cbInstr The instruction length in bytes.
3581 */
3582IEM_STATIC VBOXSTRICTRC iemVmxVmexitEvent(PVMCPUCC pVCpu, uint8_t uVector, uint32_t fFlags, uint32_t uErrCode, uint64_t uCr2,
3583 uint8_t cbInstr)
3584{
3585 /*
3586 * If the event is being injected as part of VM-entry, it is -not- subject to event
3587 * intercepts in the nested-guest. However, secondary exceptions that occur during
3588 * injection of any event -are- subject to event interception.
3589 *
3590 * See Intel spec. 26.5.1.2 "VM Exits During Event Injection".
3591 */
3592 if (!CPUMIsGuestVmxInterceptEvents(&pVCpu->cpum.GstCtx))
3593 {
3594 /*
3595 * If the event is a virtual-NMI (which is an NMI being inject during VM-entry)
3596 * virtual-NMI blocking must be set in effect rather than physical NMI blocking.
3597 *
3598 * See Intel spec. 24.6.1 "Pin-Based VM-Execution Controls".
3599 */
3600 if ( uVector == X86_XCPT_NMI
3601 && (fFlags & IEM_XCPT_FLAGS_T_CPU_XCPT)
3602 && (pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32PinCtls & VMX_PIN_CTLS_VIRT_NMI))
3603 pVCpu->cpum.GstCtx.hwvirt.vmx.fVirtNmiBlocking = true;
3604 else
3605 Assert(!pVCpu->cpum.GstCtx.hwvirt.vmx.fVirtNmiBlocking);
3606
3607 CPUMSetGuestVmxInterceptEvents(&pVCpu->cpum.GstCtx, true);
3608 return VINF_VMX_INTERCEPT_NOT_ACTIVE;
3609 }
3610
3611 /*
3612 * We are injecting an external interrupt, check if we need to cause a VM-exit now.
3613 * If not, the caller will continue delivery of the external interrupt as it would
3614 * normally. The interrupt is no longer pending in the interrupt controller at this
3615 * point.
3616 */
3617 if (fFlags & IEM_XCPT_FLAGS_T_EXT_INT)
3618 {
3619 Assert(!VMX_IDT_VECTORING_INFO_IS_VALID(pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32RoIdtVectoringInfo));
3620 return iemVmxVmexitExtInt(pVCpu, uVector, false /* fIntPending */);
3621 }
3622
3623 /*
3624 * Evaluate intercepts for hardware exceptions, software exceptions (#BP, #OF),
3625 * and privileged software exceptions (#DB generated by INT1/ICEBP) and software
3626 * interrupts.
3627 */
3628 Assert(fFlags & (IEM_XCPT_FLAGS_T_CPU_XCPT | IEM_XCPT_FLAGS_T_SOFT_INT));
3629 bool fIntercept;
3630 if ( !(fFlags & IEM_XCPT_FLAGS_T_SOFT_INT)
3631 || (fFlags & (IEM_XCPT_FLAGS_BP_INSTR | IEM_XCPT_FLAGS_OF_INSTR | IEM_XCPT_FLAGS_ICEBP_INSTR)))
3632 fIntercept = CPUMIsGuestVmxXcptInterceptSet(&pVCpu->cpum.GstCtx, uVector, uErrCode);
3633 else
3634 {
3635 /* Software interrupts cannot be intercepted and therefore do not cause a VM-exit. */
3636 fIntercept = false;
3637 }
3638
3639 /*
3640 * Now that we've determined whether the event causes a VM-exit, we need to construct the
3641 * relevant VM-exit information and cause the VM-exit.
3642 */
3643 if (fIntercept)
3644 {
3645 Assert(!(fFlags & IEM_XCPT_FLAGS_T_EXT_INT));
3646
3647 /* Construct the rest of the event related information fields and cause the VM-exit. */
3648 uint64_t u64ExitQual;
3649 if (uVector == X86_XCPT_PF)
3650 {
3651 Assert(fFlags & IEM_XCPT_FLAGS_CR2);
3652 u64ExitQual = uCr2;
3653 }
3654 else if (uVector == X86_XCPT_DB)
3655 {
3656 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_DR6);
3657 u64ExitQual = pVCpu->cpum.GstCtx.dr[6] & VMX_VMCS_EXIT_QUAL_VALID_MASK;
3658 }
3659 else
3660 u64ExitQual = 0;
3661
3662 uint8_t const fNmiUnblocking = pVCpu->cpum.GstCtx.hwvirt.vmx.fNmiUnblockingIret;
3663 bool const fErrCodeValid = RT_BOOL(fFlags & IEM_XCPT_FLAGS_ERR);
3664 uint8_t const uIntInfoType = iemVmxGetEventType(uVector, fFlags);
3665 uint32_t const uExitIntInfo = RT_BF_MAKE(VMX_BF_EXIT_INT_INFO_VECTOR, uVector)
3666 | RT_BF_MAKE(VMX_BF_EXIT_INT_INFO_TYPE, uIntInfoType)
3667 | RT_BF_MAKE(VMX_BF_EXIT_INT_INFO_ERR_CODE_VALID, fErrCodeValid)
3668 | RT_BF_MAKE(VMX_BF_EXIT_INT_INFO_NMI_UNBLOCK_IRET, fNmiUnblocking)
3669 | RT_BF_MAKE(VMX_BF_EXIT_INT_INFO_VALID, 1);
3670 iemVmxVmcsSetExitIntInfo(pVCpu, uExitIntInfo);
3671 iemVmxVmcsSetExitIntErrCode(pVCpu, uErrCode);
3672
3673 /*
3674 * For VM-exits due to software exceptions (those generated by INT3 or INTO) or privileged
3675 * software exceptions (those generated by INT1/ICEBP) we need to supply the VM-exit instruction
3676 * length.
3677 */
3678 if ( (fFlags & IEM_XCPT_FLAGS_T_SOFT_INT)
3679 || (fFlags & (IEM_XCPT_FLAGS_BP_INSTR | IEM_XCPT_FLAGS_OF_INSTR | IEM_XCPT_FLAGS_ICEBP_INSTR)))
3680 iemVmxVmcsSetExitInstrLen(pVCpu, cbInstr);
3681 else
3682 iemVmxVmcsSetExitInstrLen(pVCpu, 0);
3683
3684 return iemVmxVmexit(pVCpu, VMX_EXIT_XCPT_OR_NMI, u64ExitQual);
3685 }
3686
3687 return VINF_VMX_INTERCEPT_NOT_ACTIVE;
3688}
3689
3690
3691/**
3692 * VMX VM-exit handler for EPT misconfiguration.
3693 *
3694 * @param pVCpu The cross context virtual CPU structure.
3695 * @param GCPhysAddr The physical address causing the EPT misconfiguration. This
3696 * must be page aligned.
3697 */
3698IEM_STATIC VBOXSTRICTRC iemVmxVmexitEptMisconfig(PVMCPUCC pVCpu, RTGCPHYS GCPhysAddr)
3699{
3700 Assert(!(GCPhysAddr & PAGE_OFFSET_MASK));
3701 iemVmxVmcsSetExitGuestPhysAddr(pVCpu, GCPhysAddr);
3702 return iemVmxVmexit(pVCpu, VMX_EXIT_EPT_MISCONFIG, 0 /* u64ExitQual */);
3703}
3704
3705
3706/**
3707 * VMX VM-exit handler for EPT violation.
3708 *
3709 * @param pVCpu The cross context virtual CPU structure.
3710 * @param fAccess The access causing the EPT violation, IEM_ACCESS_XXX.
3711 * @param fSlatFail The SLAT failure info, IEM_SLAT_FAIL_XXX.
3712 * @param fEptAccess The EPT paging structure bits.
3713 * @param GCPhysAddr The physical address causing the EPT violation. This
3714 * must be page aligned.
3715 * @param fIsLinearAddrValid Whether translation of a linear address caused this
3716 * EPT violation. If @c false, GCPtrAddr must be 0.
3717 * @param GCPtrAddr The linear address causing the EPT violation.
3718 * @param cbInstr The VM-exit instruction length.
3719 */
3720IEM_STATIC VBOXSTRICTRC iemVmxVmexitEptViolation(PVMCPUCC pVCpu, uint32_t fAccess, uint32_t fSlatFail, uint64_t fEptAccess,
3721 RTGCPHYS GCPhysAddr, bool fLinearAddrValid, uint64_t GCPtrAddr, uint8_t cbInstr)
3722{
3723 /*
3724 * If the linear address isn't valid (can happen when loading PDPTEs
3725 * as part of MOV CR execution) the linear address field is undefined.
3726 * While we can leave it this way, it's preferrable to zero it for consistency.
3727 */
3728 Assert(fLinearAddrValid || GCPtrAddr == 0);
3729 Assert(!(GCPhysAddr & PAGE_OFFSET_MASK));
3730
3731 uint64_t const fCaps = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64EptVpidCaps;
3732 uint8_t const fSupportsAccessDirty = fCaps & MSR_IA32_VMX_EPT_VPID_CAP_ACCESS_DIRTY;
3733
3734 uint8_t const fDataRead = ((fAccess & IEM_ACCESS_DATA_R) == IEM_ACCESS_DATA_R) | fSupportsAccessDirty;
3735 uint8_t const fDataWrite = ((fAccess & IEM_ACCESS_DATA_RW) == IEM_ACCESS_DATA_RW) | fSupportsAccessDirty;
3736 uint8_t const fInstrFetch = (fAccess & IEM_ACCESS_INSTRUCTION) == IEM_ACCESS_INSTRUCTION;
3737 bool const fEptRead = RT_BOOL(fEptAccess & EPT_E_READ);
3738 bool const fEptWrite = RT_BOOL(fEptAccess & EPT_E_WRITE);
3739 bool const fEptExec = RT_BOOL(fEptAccess & EPT_E_EXECUTE);
3740 bool const fNmiUnblocking = pVCpu->cpum.GstCtx.hwvirt.vmx.fNmiUnblockingIret;
3741 bool const fLinearToPhysAddr = fLinearAddrValid & RT_BOOL(fSlatFail & IEM_SLAT_FAIL_LINEAR_TO_PHYS_ADDR);
3742
3743 uint64_t const u64ExitQual = RT_BF_MAKE(VMX_BF_EXIT_QUAL_EPT_ACCESS_READ, fDataRead)
3744 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_EPT_ACCESS_WRITE, fDataWrite)
3745 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_EPT_ACCESS_INSTR_FETCH, fInstrFetch)
3746 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_EPT_ENTRY_READ, fEptRead)
3747 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_EPT_ENTRY_WRITE, fEptWrite)
3748 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_EPT_ENTRY_EXECUTE, fEptExec)
3749 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_EPT_LINEAR_ADDR_VALID, fLinearAddrValid)
3750 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_EPT_LINEAR_TO_PHYS_ADDR, fLinearToPhysAddr)
3751 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_EPT_NMI_UNBLOCK_IRET, fNmiUnblocking);
3752
3753#ifdef VBOX_STRICT
3754 uint64_t const fMiscCaps = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Misc;
3755 uint32_t const fProcCtls2 = pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32ProcCtls2;
3756 Assert(!(fCaps & MSR_IA32_VMX_EPT_VPID_CAP_ADVEXITINFO_EPT_VIOLATION)); /* Advanced VM-exit info. not supported */
3757 Assert(!(fCaps & MSR_IA32_VMX_EPT_VPID_CAP_SUPER_SHW_STACK)); /* Supervisor shadow stack control not supported. */
3758 Assert(!(RT_BF_GET(fMiscCaps, VMX_BF_MISC_INTEL_PT))); /* Intel PT not supported. */
3759 Assert(!(fProcCtls2 & VMX_PROC_CTLS2_MODE_BASED_EPT_PERM)); /* Mode-based execute control not supported. */
3760#endif
3761
3762 iemVmxVmcsSetExitGuestPhysAddr(pVCpu, GCPhysAddr);
3763 iemVmxVmcsSetExitGuestLinearAddr(pVCpu, GCPtrAddr);
3764 iemVmxVmcsSetExitInstrLen(pVCpu, cbInstr);
3765
3766 return iemVmxVmexit(pVCpu, VMX_EXIT_EPT_VIOLATION, u64ExitQual);
3767}
3768
3769
3770/**
3771 * VMX VM-exit handler for EPT-induced VM-exits.
3772 *
3773 * @param pVCpu The cross context virtual CPU structure.
3774 * @param pWalk The page walk info.
3775 * @param fAccess The access causing the EPT event, IEM_ACCESS_XXX.
3776 * @param fSlatFail Additional SLAT info, IEM_SLAT_FAIL_XXX.
3777 * @param cbInstr The VM-exit instruction length if applicable. Pass 0 if not
3778 * applicable.
3779 */
3780IEM_STATIC VBOXSTRICTRC iemVmxVmexitEpt(PVMCPUCC pVCpu, PPGMPTWALK pWalk, uint32_t fAccess, uint32_t fSlatFail,
3781 uint8_t cbInstr)
3782{
3783 Assert(pWalk->fIsSlat);
3784 Assert(pWalk->fFailed & PGM_WALKFAIL_EPT);
3785 Assert(!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fVmxEptXcptVe); /* #VE exceptions not supported. */
3786 Assert(!(pWalk->fFailed & PGM_WALKFAIL_EPT_VIOLATION_CONVERTIBLE)); /* Without #VE, convertible violations not possible. */
3787
3788 if (pWalk->fFailed & PGM_WALKFAIL_EPT_VIOLATION)
3789 {
3790 uint64_t const fEptAccess = (pWalk->fEffective & PGM_PTATTRS_EPT_MASK) >> PGM_PTATTRS_EPT_SHIFT;
3791 return iemVmxVmexitEptViolation(pVCpu, fAccess, fSlatFail, fEptAccess, pWalk->GCPhysNested, pWalk->fIsLinearAddrValid,
3792 pWalk->GCPtr, cbInstr);
3793 }
3794
3795 Assert(pWalk->fFailed & PGM_WALKFAIL_EPT_MISCONFIG);
3796 return iemVmxVmexitEptMisconfig(pVCpu, pWalk->GCPhysNested);
3797}
3798
3799
3800/**
3801 * VMX VM-exit handler for APIC accesses.
3802 *
3803 * @param pVCpu The cross context virtual CPU structure.
3804 * @param offAccess The offset of the register being accessed.
3805 * @param fAccess The type of access (must contain IEM_ACCESS_TYPE_READ or
3806 * IEM_ACCESS_TYPE_WRITE or IEM_ACCESS_INSTRUCTION).
3807 */
3808IEM_STATIC VBOXSTRICTRC iemVmxVmexitApicAccess(PVMCPUCC pVCpu, uint16_t offAccess, uint32_t fAccess)
3809{
3810 Assert((fAccess & IEM_ACCESS_TYPE_READ) || (fAccess & IEM_ACCESS_TYPE_WRITE) || (fAccess & IEM_ACCESS_INSTRUCTION));
3811
3812 VMXAPICACCESS enmAccess;
3813 bool const fInEventDelivery = IEMGetCurrentXcpt(pVCpu, NULL, NULL, NULL, NULL);
3814 if (fInEventDelivery)
3815 enmAccess = VMXAPICACCESS_LINEAR_EVENT_DELIVERY;
3816 else if (fAccess & IEM_ACCESS_INSTRUCTION)
3817 enmAccess = VMXAPICACCESS_LINEAR_INSTR_FETCH;
3818 else if (fAccess & IEM_ACCESS_TYPE_WRITE)
3819 enmAccess = VMXAPICACCESS_LINEAR_WRITE;
3820 else
3821 enmAccess = VMXAPICACCESS_LINEAR_READ;
3822
3823 uint64_t const u64ExitQual = RT_BF_MAKE(VMX_BF_EXIT_QUAL_APIC_ACCESS_OFFSET, offAccess)
3824 | RT_BF_MAKE(VMX_BF_EXIT_QUAL_APIC_ACCESS_TYPE, enmAccess);
3825 return iemVmxVmexit(pVCpu, VMX_EXIT_APIC_ACCESS, u64ExitQual);
3826}
3827
3828
3829/**
3830 * VMX VM-exit handler for APIC accesses.
3831 *
3832 * This is intended for APIC accesses where the caller provides all the
3833 * relevant VM-exit information.
3834 *
3835 * @returns VBox strict status code.
3836 * @param pVCpu The cross context virtual CPU structure.
3837 * @param pExitInfo Pointer to the VM-exit information.
3838 * @param pExitEventInfo Pointer to the VM-exit event information.
3839 */
3840IEM_STATIC VBOXSTRICTRC iemVmxVmexitApicAccessWithInfo(PVMCPUCC pVCpu, PCVMXVEXITINFO pExitInfo,
3841 PCVMXVEXITEVENTINFO pExitEventInfo)
3842{
3843 /* VM-exit interruption information should not be valid for APIC-access VM-exits. */
3844 Assert(!VMX_EXIT_INT_INFO_IS_VALID(pExitEventInfo->uExitIntInfo));
3845 Assert(pExitInfo->uReason == VMX_EXIT_APIC_ACCESS);
3846 iemVmxVmcsSetExitIntInfo(pVCpu, 0);
3847 iemVmxVmcsSetExitIntErrCode(pVCpu, 0);
3848 iemVmxVmcsSetExitInstrLen(pVCpu, pExitInfo->cbInstr);
3849 iemVmxVmcsSetIdtVectoringInfo(pVCpu, pExitEventInfo->uIdtVectoringInfo);
3850 iemVmxVmcsSetIdtVectoringErrCode(pVCpu, pExitEventInfo->uIdtVectoringErrCode);
3851 return iemVmxVmexit(pVCpu, VMX_EXIT_APIC_ACCESS, pExitInfo->u64Qual);
3852}
3853
3854
3855/**
3856 * VMX VM-exit handler for APIC-write VM-exits.
3857 *
3858 * @param pVCpu The cross context virtual CPU structure.
3859 * @param offApic The write to the virtual-APIC page offset that caused this
3860 * VM-exit.
3861 */
3862IEM_STATIC VBOXSTRICTRC iemVmxVmexitApicWrite(PVMCPUCC pVCpu, uint16_t offApic)
3863{
3864 Assert(offApic < XAPIC_OFF_END + 4);
3865 /* Write only bits 11:0 of the APIC offset into the Exit qualification field. */
3866 offApic &= UINT16_C(0xfff);
3867 return iemVmxVmexit(pVCpu, VMX_EXIT_APIC_WRITE, offApic);
3868}
3869
3870
3871/**
3872 * Sets virtual-APIC write emulation as pending.
3873 *
3874 * @param pVCpu The cross context virtual CPU structure.
3875 * @param offApic The offset in the virtual-APIC page that was written.
3876 */
3877DECLINLINE(void) iemVmxVirtApicSetPendingWrite(PVMCPUCC pVCpu, uint16_t offApic)
3878{
3879 Assert(offApic < XAPIC_OFF_END + 4);
3880
3881 /*
3882 * Record the currently updated APIC offset, as we need this later for figuring
3883 * out whether to perform TPR, EOI or self-IPI virtualization as well as well
3884 * as for supplying the exit qualification when causing an APIC-write VM-exit.
3885 */
3886 pVCpu->cpum.GstCtx.hwvirt.vmx.offVirtApicWrite = offApic;
3887
3888 /*
3889 * Flag that we need to perform virtual-APIC write emulation (TPR/PPR/EOI/Self-IPI
3890 * virtualization or APIC-write emulation).
3891 */
3892 if (!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_VMX_APIC_WRITE))
3893 VMCPU_FF_SET(pVCpu, VMCPU_FF_VMX_APIC_WRITE);
3894}
3895
3896
3897/**
3898 * Clears any pending virtual-APIC write emulation.
3899 *
3900 * @returns The virtual-APIC offset that was written before clearing it.
3901 * @param pVCpu The cross context virtual CPU structure.
3902 */
3903DECLINLINE(uint16_t) iemVmxVirtApicClearPendingWrite(PVMCPUCC pVCpu)
3904{
3905 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_HWVIRT);
3906 uint8_t const offVirtApicWrite = pVCpu->cpum.GstCtx.hwvirt.vmx.offVirtApicWrite;
3907 pVCpu->cpum.GstCtx.hwvirt.vmx.offVirtApicWrite = 0;
3908 Assert(VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_VMX_APIC_WRITE));
3909 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_VMX_APIC_WRITE);
3910 return offVirtApicWrite;
3911}
3912
3913
3914/**
3915 * Reads a 32-bit register from the virtual-APIC page at the given offset.
3916 *
3917 * @returns The register from the virtual-APIC page.
3918 * @param pVCpu The cross context virtual CPU structure.
3919 * @param offReg The offset of the register being read.
3920 */
3921IEM_STATIC uint32_t iemVmxVirtApicReadRaw32(PVMCPUCC pVCpu, uint16_t offReg)
3922{
3923 Assert(offReg <= VMX_V_VIRT_APIC_SIZE - sizeof(uint32_t));
3924
3925 uint32_t uReg = 0;
3926 RTGCPHYS const GCPhysVirtApic = pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64AddrVirtApic.u;
3927 int rc = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), &uReg, GCPhysVirtApic + offReg, sizeof(uReg));
3928 AssertMsgStmt(RT_SUCCESS(rc),
3929 ("Failed to read %u bytes at offset %#x of the virtual-APIC page at %#RGp: %Rrc\n",
3930 sizeof(uReg), offReg, GCPhysVirtApic, rc),
3931 uReg = 0);
3932 return uReg;
3933}
3934
3935
3936/**
3937 * Reads a 64-bit register from the virtual-APIC page at the given offset.
3938 *
3939 * @returns The register from the virtual-APIC page.
3940 * @param pVCpu The cross context virtual CPU structure.
3941 * @param offReg The offset of the register being read.
3942 */
3943IEM_STATIC uint64_t iemVmxVirtApicReadRaw64(PVMCPUCC pVCpu, uint16_t offReg)
3944{
3945 Assert(offReg <= VMX_V_VIRT_APIC_SIZE - sizeof(uint64_t));
3946
3947 uint64_t uReg = 0;
3948 RTGCPHYS const GCPhysVirtApic = pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64AddrVirtApic.u;
3949 int rc = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), &uReg, GCPhysVirtApic + offReg, sizeof(uReg));
3950 AssertMsgStmt(RT_SUCCESS(rc),
3951 ("Failed to read %u bytes at offset %#x of the virtual-APIC page at %#RGp: %Rrc\n",
3952 sizeof(uReg), offReg, GCPhysVirtApic, rc),
3953 uReg = 0);
3954 return uReg;
3955}
3956
3957
3958/**
3959 * Writes a 32-bit register to the virtual-APIC page at the given offset.
3960 *
3961 * @param pVCpu The cross context virtual CPU structure.
3962 * @param offReg The offset of the register being written.
3963 * @param uReg The register value to write.
3964 */
3965IEM_STATIC void iemVmxVirtApicWriteRaw32(PVMCPUCC pVCpu, uint16_t offReg, uint32_t uReg)
3966{
3967 Assert(offReg <= VMX_V_VIRT_APIC_SIZE - sizeof(uint32_t));
3968
3969 RTGCPHYS const GCPhysVirtApic = pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64AddrVirtApic.u;
3970 int rc = PGMPhysSimpleWriteGCPhys(pVCpu->CTX_SUFF(pVM), GCPhysVirtApic + offReg, &uReg, sizeof(uReg));
3971 AssertMsgRC(rc, ("Failed to write %u bytes at offset %#x of the virtual-APIC page at %#RGp: %Rrc\n",
3972 sizeof(uReg), offReg, GCPhysVirtApic, rc));
3973}
3974
3975
3976/**
3977 * Writes a 64-bit register to the virtual-APIC page at the given offset.
3978 *
3979 * @param pVCpu The cross context virtual CPU structure.
3980 * @param offReg The offset of the register being written.
3981 * @param uReg The register value to write.
3982 */
3983IEM_STATIC void iemVmxVirtApicWriteRaw64(PVMCPUCC pVCpu, uint16_t offReg, uint64_t uReg)
3984{
3985 Assert(offReg <= VMX_V_VIRT_APIC_SIZE - sizeof(uint64_t));
3986
3987 RTGCPHYS const GCPhysVirtApic = pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64AddrVirtApic.u;
3988 int rc = PGMPhysSimpleWriteGCPhys(pVCpu->CTX_SUFF(pVM), GCPhysVirtApic + offReg, &uReg, sizeof(uReg));
3989 AssertMsgRC(rc, ("Failed to write %u bytes at offset %#x of the virtual-APIC page at %#RGp: %Rrc\n",
3990 sizeof(uReg), offReg, GCPhysVirtApic, rc));
3991}
3992
3993
3994/**
3995 * Sets the vector in a virtual-APIC 256-bit sparse register.
3996 *
3997 * @param pVCpu The cross context virtual CPU structure.
3998 * @param offReg The offset of the 256-bit spare register.
3999 * @param uVector The vector to set.
4000 *
4001 * @remarks This is based on our APIC device code.
4002 */
4003IEM_STATIC void iemVmxVirtApicSetVectorInReg(PVMCPUCC pVCpu, uint16_t offReg, uint8_t uVector)
4004{
4005 /* Determine the vector offset within the chunk. */
4006 uint16_t const offVector = (uVector & UINT32_C(0xe0)) >> 1;
4007
4008 /* Read the chunk at the offset. */
4009 uint32_t uReg;
4010 RTGCPHYS const GCPhysVirtApic = pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64AddrVirtApic.u;
4011 int rc = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), &uReg, GCPhysVirtApic + offReg + offVector, sizeof(uReg));
4012 if (RT_SUCCESS(rc))
4013 {
4014 /* Modify the chunk. */
4015 uint16_t const idxVectorBit = uVector & UINT32_C(0x1f);
4016 uReg |= RT_BIT(idxVectorBit);
4017
4018 /* Write the chunk. */
4019 rc = PGMPhysSimpleWriteGCPhys(pVCpu->CTX_SUFF(pVM), GCPhysVirtApic + offReg + offVector, &uReg, sizeof(uReg));
4020 AssertMsgRC(rc, ("Failed to set vector %#x in 256-bit register at %#x of the virtual-APIC page at %#RGp: %Rrc\n",
4021 uVector, offReg, GCPhysVirtApic, rc));
4022 }
4023 else
4024 AssertMsgFailed(("Failed to get vector %#x in 256-bit register at %#x of the virtual-APIC page at %#RGp: %Rrc\n",
4025 uVector, offReg, GCPhysVirtApic, rc));
4026}
4027
4028
4029/**
4030 * Clears the vector in a virtual-APIC 256-bit sparse register.
4031 *
4032 * @param pVCpu The cross context virtual CPU structure.
4033 * @param offReg The offset of the 256-bit spare register.
4034 * @param uVector The vector to clear.
4035 *
4036 * @remarks This is based on our APIC device code.
4037 */
4038IEM_STATIC void iemVmxVirtApicClearVectorInReg(PVMCPUCC pVCpu, uint16_t offReg, uint8_t uVector)
4039{
4040 /* Determine the vector offset within the chunk. */
4041 uint16_t const offVector = (uVector & UINT32_C(0xe0)) >> 1;
4042
4043 /* Read the chunk at the offset. */
4044 uint32_t uReg;
4045 RTGCPHYS const GCPhysVirtApic = pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64AddrVirtApic.u;
4046 int rc = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), &uReg, GCPhysVirtApic + offReg + offVector, sizeof(uReg));
4047 if (RT_SUCCESS(rc))
4048 {
4049 /* Modify the chunk. */
4050 uint16_t const idxVectorBit = uVector & UINT32_C(0x1f);
4051 uReg &= ~RT_BIT(idxVectorBit);
4052
4053 /* Write the chunk. */
4054 rc = PGMPhysSimpleWriteGCPhys(pVCpu->CTX_SUFF(pVM), GCPhysVirtApic + offReg + offVector, &uReg, sizeof(uReg));
4055 AssertMsgRC(rc, ("Failed to clear vector %#x in 256-bit register at %#x of the virtual-APIC page at %#RGp: %Rrc\n",
4056 uVector, offReg, GCPhysVirtApic, rc));
4057 }
4058 else
4059 AssertMsgFailed(("Failed to get vector %#x in 256-bit register at %#x of the virtual-APIC page at %#RGp: %Rrc\n",
4060 uVector, offReg, GCPhysVirtApic, rc));
4061}
4062
4063
4064/**
4065 * Checks if a memory access to the APIC-access page must causes an APIC-access
4066 * VM-exit.
4067 *
4068 * @param pVCpu The cross context virtual CPU structure.
4069 * @param offAccess The offset of the register being accessed.
4070 * @param cbAccess The size of the access in bytes.
4071 * @param fAccess The type of access (must be IEM_ACCESS_TYPE_READ or
4072 * IEM_ACCESS_TYPE_WRITE).
4073 *
4074 * @remarks This must not be used for MSR-based APIC-access page accesses!
4075 * @sa iemVmxVirtApicAccessMsrWrite, iemVmxVirtApicAccessMsrRead.
4076 */
4077IEM_STATIC bool iemVmxVirtApicIsMemAccessIntercepted(PVMCPUCC pVCpu, uint16_t offAccess, size_t cbAccess, uint32_t fAccess)
4078{
4079 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
4080 Assert(fAccess == IEM_ACCESS_TYPE_READ || fAccess == IEM_ACCESS_TYPE_WRITE);
4081
4082 /*
4083 * We must cause a VM-exit if any of the following are true:
4084 * - TPR shadowing isn't active.
4085 * - The access size exceeds 32-bits.
4086 * - The access is not contained within low 4 bytes of a 16-byte aligned offset.
4087 *
4088 * See Intel spec. 29.4.2 "Virtualizing Reads from the APIC-Access Page".
4089 * See Intel spec. 29.4.3.1 "Determining Whether a Write Access is Virtualized".
4090 */
4091 if ( !(pVmcs->u32ProcCtls & VMX_PROC_CTLS_USE_TPR_SHADOW)
4092 || cbAccess > sizeof(uint32_t)
4093 || ((offAccess + cbAccess - 1) & 0xc)
4094 || offAccess >= XAPIC_OFF_END + 4)
4095 return true;
4096
4097 /*
4098 * If the access is part of an operation where we have already
4099 * virtualized a virtual-APIC write, we must cause a VM-exit.
4100 */
4101 if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_VMX_APIC_WRITE))
4102 return true;
4103
4104 /*
4105 * Check write accesses to the APIC-access page that cause VM-exits.
4106 */
4107 if (fAccess & IEM_ACCESS_TYPE_WRITE)
4108 {
4109 if (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_APIC_REG_VIRT)
4110 {
4111 /*
4112 * With APIC-register virtualization, a write access to any of the
4113 * following registers are virtualized. Accessing any other register
4114 * causes a VM-exit.
4115 */
4116 uint16_t const offAlignedAccess = offAccess & 0xfffc;
4117 switch (offAlignedAccess)
4118 {
4119 case XAPIC_OFF_ID:
4120 case XAPIC_OFF_TPR:
4121 case XAPIC_OFF_EOI:
4122 case XAPIC_OFF_LDR:
4123 case XAPIC_OFF_DFR:
4124 case XAPIC_OFF_SVR:
4125 case XAPIC_OFF_ESR:
4126 case XAPIC_OFF_ICR_LO:
4127 case XAPIC_OFF_ICR_HI:
4128 case XAPIC_OFF_LVT_TIMER:
4129 case XAPIC_OFF_LVT_THERMAL:
4130 case XAPIC_OFF_LVT_PERF:
4131 case XAPIC_OFF_LVT_LINT0:
4132 case XAPIC_OFF_LVT_LINT1:
4133 case XAPIC_OFF_LVT_ERROR:
4134 case XAPIC_OFF_TIMER_ICR:
4135 case XAPIC_OFF_TIMER_DCR:
4136 break;
4137 default:
4138 return true;
4139 }
4140 }
4141 else if (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_INT_DELIVERY)
4142 {
4143 /*
4144 * With virtual-interrupt delivery, a write access to any of the
4145 * following registers are virtualized. Accessing any other register
4146 * causes a VM-exit.
4147 *
4148 * Note! The specification does not allow writing to offsets in-between
4149 * these registers (e.g. TPR + 1 byte) unlike read accesses.
4150 */
4151 switch (offAccess)
4152 {
4153 case XAPIC_OFF_TPR:
4154 case XAPIC_OFF_EOI:
4155 case XAPIC_OFF_ICR_LO:
4156 break;
4157 default:
4158 return true;
4159 }
4160 }
4161 else
4162 {
4163 /*
4164 * Without APIC-register virtualization or virtual-interrupt delivery,
4165 * only TPR accesses are virtualized.
4166 */
4167 if (offAccess == XAPIC_OFF_TPR)
4168 { /* likely */ }
4169 else
4170 return true;
4171 }
4172 }
4173 else
4174 {
4175 /*
4176 * Check read accesses to the APIC-access page that cause VM-exits.
4177 */
4178 if (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_APIC_REG_VIRT)
4179 {
4180 /*
4181 * With APIC-register virtualization, a read access to any of the
4182 * following registers are virtualized. Accessing any other register
4183 * causes a VM-exit.
4184 */
4185 uint16_t const offAlignedAccess = offAccess & 0xfffc;
4186 switch (offAlignedAccess)
4187 {
4188 /** @todo r=ramshankar: What about XAPIC_OFF_LVT_CMCI? */
4189 case XAPIC_OFF_ID:
4190 case XAPIC_OFF_VERSION:
4191 case XAPIC_OFF_TPR:
4192 case XAPIC_OFF_EOI:
4193 case XAPIC_OFF_LDR:
4194 case XAPIC_OFF_DFR:
4195 case XAPIC_OFF_SVR:
4196 case XAPIC_OFF_ISR0: case XAPIC_OFF_ISR1: case XAPIC_OFF_ISR2: case XAPIC_OFF_ISR3:
4197 case XAPIC_OFF_ISR4: case XAPIC_OFF_ISR5: case XAPIC_OFF_ISR6: case XAPIC_OFF_ISR7:
4198 case XAPIC_OFF_TMR0: case XAPIC_OFF_TMR1: case XAPIC_OFF_TMR2: case XAPIC_OFF_TMR3:
4199 case XAPIC_OFF_TMR4: case XAPIC_OFF_TMR5: case XAPIC_OFF_TMR6: case XAPIC_OFF_TMR7:
4200 case XAPIC_OFF_IRR0: case XAPIC_OFF_IRR1: case XAPIC_OFF_IRR2: case XAPIC_OFF_IRR3:
4201 case XAPIC_OFF_IRR4: case XAPIC_OFF_IRR5: case XAPIC_OFF_IRR6: case XAPIC_OFF_IRR7:
4202 case XAPIC_OFF_ESR:
4203 case XAPIC_OFF_ICR_LO:
4204 case XAPIC_OFF_ICR_HI:
4205 case XAPIC_OFF_LVT_TIMER:
4206 case XAPIC_OFF_LVT_THERMAL:
4207 case XAPIC_OFF_LVT_PERF:
4208 case XAPIC_OFF_LVT_LINT0:
4209 case XAPIC_OFF_LVT_LINT1:
4210 case XAPIC_OFF_LVT_ERROR:
4211 case XAPIC_OFF_TIMER_ICR:
4212 case XAPIC_OFF_TIMER_DCR:
4213 break;
4214 default:
4215 return true;
4216 }
4217 }
4218 else
4219 {
4220 /* Without APIC-register virtualization, only TPR accesses are virtualized. */
4221 if (offAccess == XAPIC_OFF_TPR)
4222 { /* likely */ }
4223 else
4224 return true;
4225 }
4226 }
4227
4228 /* The APIC access is virtualized, does not cause a VM-exit. */
4229 return false;
4230}
4231
4232
4233/**
4234 * Virtualizes a memory-based APIC access where the address is not used to access
4235 * memory.
4236 *
4237 * This is for instructions like MONITOR, CLFLUSH, CLFLUSHOPT, ENTER which may cause
4238 * page-faults but do not use the address to access memory.
4239 *
4240 * @param pVCpu The cross context virtual CPU structure.
4241 * @param pGCPhysAccess Pointer to the guest-physical address used.
4242 */
4243IEM_STATIC VBOXSTRICTRC iemVmxVirtApicAccessUnused(PVMCPUCC pVCpu, PRTGCPHYS pGCPhysAccess)
4244{
4245 Assert(pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_APIC_ACCESS);
4246 Assert(pGCPhysAccess);
4247
4248 RTGCPHYS const GCPhysAccess = *pGCPhysAccess & ~(RTGCPHYS)PAGE_OFFSET_MASK;
4249 RTGCPHYS const GCPhysApic = pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u64AddrApicAccess.u;
4250 Assert(!(GCPhysApic & PAGE_OFFSET_MASK));
4251
4252 if (GCPhysAccess == GCPhysApic)
4253 {
4254 uint16_t const offAccess = *pGCPhysAccess & PAGE_OFFSET_MASK;
4255 uint32_t const fAccess = IEM_ACCESS_TYPE_READ;
4256 uint16_t const cbAccess = 1;
4257 bool const fIntercept = iemVmxVirtApicIsMemAccessIntercepted(pVCpu, offAccess, cbAccess, fAccess);
4258 if (fIntercept)
4259 return iemVmxVmexitApicAccess(pVCpu, offAccess, fAccess);
4260
4261 *pGCPhysAccess = GCPhysApic | offAccess;
4262 return VINF_VMX_MODIFIES_BEHAVIOR;
4263 }
4264
4265 return VINF_VMX_INTERCEPT_NOT_ACTIVE;
4266}
4267
4268
4269/**
4270 * Virtualizes a memory-based APIC access.
4271 *
4272 * @returns VBox strict status code.
4273 * @retval VINF_VMX_MODIFIES_BEHAVIOR if the access was virtualized.
4274 * @retval VINF_VMX_VMEXIT if the access causes a VM-exit.
4275 *
4276 * @param pVCpu The cross context virtual CPU structure.
4277 * @param offAccess The offset of the register being accessed (within the
4278 * APIC-access page).
4279 * @param cbAccess The size of the access in bytes.
4280 * @param pvData Pointer to the data being written or where to store the data
4281 * being read.
4282 * @param fAccess The type of access (must contain IEM_ACCESS_TYPE_READ or
4283 * IEM_ACCESS_TYPE_WRITE or IEM_ACCESS_INSTRUCTION).
4284 */
4285IEM_STATIC VBOXSTRICTRC iemVmxVirtApicAccessMem(PVMCPUCC pVCpu, uint16_t offAccess, size_t cbAccess, void *pvData,
4286 uint32_t fAccess)
4287{
4288 Assert(pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_APIC_ACCESS);
4289 Assert(pvData);
4290 Assert( (fAccess & IEM_ACCESS_TYPE_READ)
4291 || (fAccess & IEM_ACCESS_TYPE_WRITE)
4292 || (fAccess & IEM_ACCESS_INSTRUCTION));
4293
4294 bool const fIntercept = iemVmxVirtApicIsMemAccessIntercepted(pVCpu, offAccess, cbAccess, fAccess);
4295 if (fIntercept)
4296 return iemVmxVmexitApicAccess(pVCpu, offAccess, fAccess);
4297
4298 if (fAccess & IEM_ACCESS_TYPE_WRITE)
4299 {
4300 /*
4301 * A write access to the APIC-access page that is virtualized (rather than
4302 * causing a VM-exit) writes data to the virtual-APIC page.
4303 */
4304 uint32_t const u32Data = *(uint32_t *)pvData;
4305 iemVmxVirtApicWriteRaw32(pVCpu, offAccess, u32Data);
4306
4307 /*
4308 * Record the currently updated APIC offset, as we need this later for figuring
4309 * out whether to perform TPR, EOI or self-IPI virtualization as well as well
4310 * as for supplying the exit qualification when causing an APIC-write VM-exit.
4311 *
4312 * After completion of the current operation, we need to perform TPR virtualization,
4313 * EOI virtualization or APIC-write VM-exit depending on which register was written.
4314 *
4315 * The current operation may be a REP-prefixed string instruction, execution of any
4316 * other instruction, or delivery of an event through the IDT.
4317 *
4318 * Thus things like clearing bytes 3:1 of the VTPR, clearing VEOI are not to be
4319 * performed now but later after completion of the current operation.
4320 *
4321 * See Intel spec. 29.4.3.2 "APIC-Write Emulation".
4322 */
4323 iemVmxVirtApicSetPendingWrite(pVCpu, offAccess);
4324 }
4325 else
4326 {
4327 /*
4328 * A read access from the APIC-access page that is virtualized (rather than
4329 * causing a VM-exit) returns data from the virtual-APIC page.
4330 *
4331 * See Intel spec. 29.4.2 "Virtualizing Reads from the APIC-Access Page".
4332 */
4333 Assert(cbAccess <= 4);
4334 Assert(offAccess < XAPIC_OFF_END + 4);
4335 static uint32_t const s_auAccessSizeMasks[] = { 0, 0xff, 0xffff, 0xffffff, 0xffffffff };
4336
4337 uint32_t u32Data = iemVmxVirtApicReadRaw32(pVCpu, offAccess);
4338 u32Data &= s_auAccessSizeMasks[cbAccess];
4339 *(uint32_t *)pvData = u32Data;
4340 }
4341
4342 return VINF_VMX_MODIFIES_BEHAVIOR;
4343}
4344
4345
4346/**
4347 * Virtualizes an MSR-based APIC read access.
4348 *
4349 * @returns VBox strict status code.
4350 * @retval VINF_VMX_MODIFIES_BEHAVIOR if the MSR read was virtualized.
4351 * @retval VINF_VMX_INTERCEPT_NOT_ACTIVE if the MSR read access must be
4352 * handled by the x2APIC device.
4353 * @retval VERR_OUT_RANGE if the MSR read was supposed to be virtualized but was
4354 * not within the range of valid MSRs, caller must raise \#GP(0).
4355 * @param pVCpu The cross context virtual CPU structure.
4356 * @param idMsr The x2APIC MSR being read.
4357 * @param pu64Value Where to store the read x2APIC MSR value (only valid when
4358 * VINF_VMX_MODIFIES_BEHAVIOR is returned).
4359 */
4360IEM_STATIC VBOXSTRICTRC iemVmxVirtApicAccessMsrRead(PVMCPUCC pVCpu, uint32_t idMsr, uint64_t *pu64Value)
4361{
4362 Assert(pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_X2APIC_MODE);
4363 Assert(pu64Value);
4364
4365 if (pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32ProcCtls2 & VMX_PROC_CTLS2_APIC_REG_VIRT)
4366 {
4367 if ( idMsr >= MSR_IA32_X2APIC_START
4368 && idMsr <= MSR_IA32_X2APIC_END)
4369 {
4370 uint16_t const offReg = (idMsr & 0xff) << 4;
4371 uint64_t const u64Value = iemVmxVirtApicReadRaw64(pVCpu, offReg);
4372 *pu64Value = u64Value;
4373 return VINF_VMX_MODIFIES_BEHAVIOR;
4374 }
4375 return VERR_OUT_OF_RANGE;
4376 }
4377
4378 if (idMsr == MSR_IA32_X2APIC_TPR)
4379 {
4380 uint16_t const offReg = (idMsr & 0xff) << 4;
4381 uint64_t const u64Value = iemVmxVirtApicReadRaw64(pVCpu, offReg);
4382 *pu64Value = u64Value;
4383 return VINF_VMX_MODIFIES_BEHAVIOR;
4384 }
4385
4386 return VINF_VMX_INTERCEPT_NOT_ACTIVE;
4387}
4388
4389
4390/**
4391 * Virtualizes an MSR-based APIC write access.
4392 *
4393 * @returns VBox strict status code.
4394 * @retval VINF_VMX_MODIFIES_BEHAVIOR if the MSR write was virtualized.
4395 * @retval VERR_OUT_RANGE if the MSR read was supposed to be virtualized but was
4396 * not within the range of valid MSRs, caller must raise \#GP(0).
4397 * @retval VINF_VMX_INTERCEPT_NOT_ACTIVE if the MSR must be written normally.
4398 *
4399 * @param pVCpu The cross context virtual CPU structure.
4400 * @param idMsr The x2APIC MSR being written.
4401 * @param u64Value The value of the x2APIC MSR being written.
4402 */
4403IEM_STATIC VBOXSTRICTRC iemVmxVirtApicAccessMsrWrite(PVMCPUCC pVCpu, uint32_t idMsr, uint64_t u64Value)
4404{
4405 /*
4406 * Check if the access is to be virtualized.
4407 * See Intel spec. 29.5 "Virtualizing MSR-based APIC Accesses".
4408 */
4409 if ( idMsr == MSR_IA32_X2APIC_TPR
4410 || ( (pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_INT_DELIVERY)
4411 && ( idMsr == MSR_IA32_X2APIC_EOI
4412 || idMsr == MSR_IA32_X2APIC_SELF_IPI)))
4413 {
4414 /* Validate the MSR write depending on the register. */
4415 switch (idMsr)
4416 {
4417 case MSR_IA32_X2APIC_TPR:
4418 case MSR_IA32_X2APIC_SELF_IPI:
4419 {
4420 if (u64Value & UINT64_C(0xffffffffffffff00))
4421 return VERR_OUT_OF_RANGE;
4422 break;
4423 }
4424 case MSR_IA32_X2APIC_EOI:
4425 {
4426 if (u64Value != 0)
4427 return VERR_OUT_OF_RANGE;
4428 break;
4429 }
4430 }
4431
4432 /* Write the MSR to the virtual-APIC page. */
4433 uint16_t const offReg = (idMsr & 0xff) << 4;
4434 iemVmxVirtApicWriteRaw64(pVCpu, offReg, u64Value);
4435
4436 /*
4437 * Record the currently updated APIC offset, as we need this later for figuring
4438 * out whether to perform TPR, EOI or self-IPI virtualization as well as well
4439 * as for supplying the exit qualification when causing an APIC-write VM-exit.
4440 */
4441 iemVmxVirtApicSetPendingWrite(pVCpu, offReg);
4442
4443 return VINF_VMX_MODIFIES_BEHAVIOR;
4444 }
4445
4446 return VINF_VMX_INTERCEPT_NOT_ACTIVE;
4447}
4448
4449
4450/**
4451 * Finds the most significant set bit in a virtual-APIC 256-bit sparse register.
4452 *
4453 * @returns VBox status code.
4454 * @retval VINF_SUCCESS when the highest set bit is found.
4455 * @retval VERR_NOT_FOUND when no bit is set.
4456 *
4457 * @param pVCpu The cross context virtual CPU structure.
4458 * @param offReg The offset of the APIC 256-bit sparse register.
4459 * @param pidxHighestBit Where to store the highest bit (most significant bit)
4460 * set in the register. Only valid when VINF_SUCCESS is
4461 * returned.
4462 *
4463 * @remarks The format of the 256-bit sparse register here mirrors that found in
4464 * real APIC hardware.
4465 */
4466static int iemVmxVirtApicGetHighestSetBitInReg(PVMCPUCC pVCpu, uint16_t offReg, uint8_t *pidxHighestBit)
4467{
4468 Assert(offReg < XAPIC_OFF_END + 4);
4469 Assert(pidxHighestBit);
4470
4471 /*
4472 * There are 8 contiguous fragments (of 16-bytes each) in the sparse register.
4473 * However, in each fragment only the first 4 bytes are used.
4474 */
4475 uint8_t const cFrags = 8;
4476 for (int8_t iFrag = cFrags; iFrag >= 0; iFrag--)
4477 {
4478 uint16_t const offFrag = iFrag * 16;
4479 uint32_t const u32Frag = iemVmxVirtApicReadRaw32(pVCpu, offReg + offFrag);
4480 if (!u32Frag)
4481 continue;
4482
4483 unsigned idxHighestBit = ASMBitLastSetU32(u32Frag);
4484 Assert(idxHighestBit > 0);
4485 --idxHighestBit;
4486 Assert(idxHighestBit <= UINT8_MAX);
4487 *pidxHighestBit = idxHighestBit;
4488 return VINF_SUCCESS;
4489 }
4490 return VERR_NOT_FOUND;
4491}
4492
4493
4494/**
4495 * Evaluates pending virtual interrupts.
4496 *
4497 * @param pVCpu The cross context virtual CPU structure.
4498 */
4499IEM_STATIC void iemVmxEvalPendingVirtIntrs(PVMCPUCC pVCpu)
4500{
4501 Assert(pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_INT_DELIVERY);
4502
4503 if (!(pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32ProcCtls & VMX_PROC_CTLS_INT_WINDOW_EXIT))
4504 {
4505 uint8_t const uRvi = RT_LO_U8(pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u16GuestIntStatus);
4506 uint8_t const uPpr = iemVmxVirtApicReadRaw32(pVCpu, XAPIC_OFF_PPR);
4507
4508 if ((uRvi >> 4) > (uPpr >> 4))
4509 {
4510 Log2(("eval_virt_intrs: uRvi=%#x uPpr=%#x - Signalling pending interrupt\n", uRvi, uPpr));
4511 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_NESTED_GUEST);
4512 }
4513 else
4514 Log2(("eval_virt_intrs: uRvi=%#x uPpr=%#x - Nothing to do\n", uRvi, uPpr));
4515 }
4516}
4517
4518
4519/**
4520 * Performs PPR virtualization.
4521 *
4522 * @returns VBox strict status code.
4523 * @param pVCpu The cross context virtual CPU structure.
4524 */
4525IEM_STATIC void iemVmxPprVirtualization(PVMCPUCC pVCpu)
4526{
4527 Assert(pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32ProcCtls & VMX_PROC_CTLS_USE_TPR_SHADOW);
4528 Assert(pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_INT_DELIVERY);
4529
4530 /*
4531 * PPR virtualization is caused in response to a VM-entry, TPR-virtualization,
4532 * or EOI-virtualization.
4533 *
4534 * See Intel spec. 29.1.3 "PPR Virtualization".
4535 */
4536 uint32_t const uTpr = iemVmxVirtApicReadRaw32(pVCpu, XAPIC_OFF_TPR);
4537 uint32_t const uSvi = RT_HI_U8(pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u16GuestIntStatus);
4538
4539 uint32_t uPpr;
4540 if (((uTpr >> 4) & 0xf) >= ((uSvi >> 4) & 0xf))
4541 uPpr = uTpr & 0xff;
4542 else
4543 uPpr = uSvi & 0xf0;
4544
4545 Log2(("ppr_virt: uTpr=%#x uSvi=%#x uPpr=%#x\n", uTpr, uSvi, uPpr));
4546 iemVmxVirtApicWriteRaw32(pVCpu, XAPIC_OFF_PPR, uPpr);
4547}
4548
4549
4550/**
4551 * Performs VMX TPR virtualization.
4552 *
4553 * @returns VBox strict status code.
4554 * @param pVCpu The cross context virtual CPU structure.
4555 */
4556IEM_STATIC VBOXSTRICTRC iemVmxTprVirtualization(PVMCPUCC pVCpu)
4557{
4558 Assert(pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32ProcCtls & VMX_PROC_CTLS_USE_TPR_SHADOW);
4559
4560 /*
4561 * We should have already performed the virtual-APIC write to the TPR offset
4562 * in the virtual-APIC page. We now perform TPR virtualization.
4563 *
4564 * See Intel spec. 29.1.2 "TPR Virtualization".
4565 */
4566 if (!(pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_INT_DELIVERY))
4567 {
4568 uint32_t const uTprThreshold = pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32TprThreshold;
4569 uint32_t const uTpr = iemVmxVirtApicReadRaw32(pVCpu, XAPIC_OFF_TPR);
4570
4571 /*
4572 * If the VTPR falls below the TPR threshold, we must cause a VM-exit.
4573 * See Intel spec. 29.1.2 "TPR Virtualization".
4574 */
4575 if (((uTpr >> 4) & 0xf) < uTprThreshold)
4576 {
4577 Log2(("tpr_virt: uTpr=%u uTprThreshold=%u -> VM-exit\n", uTpr, uTprThreshold));
4578 return iemVmxVmexit(pVCpu, VMX_EXIT_TPR_BELOW_THRESHOLD, 0 /* u64ExitQual */);
4579 }
4580 }
4581 else
4582 {
4583 iemVmxPprVirtualization(pVCpu);
4584 iemVmxEvalPendingVirtIntrs(pVCpu);
4585 }
4586
4587 return VINF_SUCCESS;
4588}
4589
4590
4591/**
4592 * Checks whether an EOI write for the given interrupt vector causes a VM-exit or
4593 * not.
4594 *
4595 * @returns @c true if the EOI write is intercepted, @c false otherwise.
4596 * @param pVCpu The cross context virtual CPU structure.
4597 * @param uVector The interrupt that was acknowledged using an EOI.
4598 */
4599IEM_STATIC bool iemVmxIsEoiInterceptSet(PCVMCPU pVCpu, uint8_t uVector)
4600{
4601 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
4602 Assert(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_INT_DELIVERY);
4603
4604 if (uVector < 64)
4605 return RT_BOOL(pVmcs->u64EoiExitBitmap0.u & RT_BIT_64(uVector));
4606 if (uVector < 128)
4607 return RT_BOOL(pVmcs->u64EoiExitBitmap1.u & RT_BIT_64(uVector));
4608 if (uVector < 192)
4609 return RT_BOOL(pVmcs->u64EoiExitBitmap2.u & RT_BIT_64(uVector));
4610 return RT_BOOL(pVmcs->u64EoiExitBitmap3.u & RT_BIT_64(uVector));
4611}
4612
4613
4614/**
4615 * Performs EOI virtualization.
4616 *
4617 * @returns VBox strict status code.
4618 * @param pVCpu The cross context virtual CPU structure.
4619 */
4620IEM_STATIC VBOXSTRICTRC iemVmxEoiVirtualization(PVMCPUCC pVCpu)
4621{
4622 PVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
4623 Assert(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_INT_DELIVERY);
4624
4625 /*
4626 * Clear the interrupt guest-interrupt as no longer in-service (ISR)
4627 * and get the next guest-interrupt that's in-service (if any).
4628 *
4629 * See Intel spec. 29.1.4 "EOI Virtualization".
4630 */
4631 uint8_t const uRvi = RT_LO_U8(pVmcs->u16GuestIntStatus);
4632 uint8_t const uSvi = RT_HI_U8(pVmcs->u16GuestIntStatus);
4633 Log2(("eoi_virt: uRvi=%#x uSvi=%#x\n", uRvi, uSvi));
4634
4635 uint8_t uVector = uSvi;
4636 iemVmxVirtApicClearVectorInReg(pVCpu, XAPIC_OFF_ISR0, uVector);
4637
4638 uVector = 0;
4639 iemVmxVirtApicGetHighestSetBitInReg(pVCpu, XAPIC_OFF_ISR0, &uVector);
4640
4641 if (uVector)
4642 Log2(("eoi_virt: next interrupt %#x\n", uVector));
4643 else
4644 Log2(("eoi_virt: no interrupt pending in ISR\n"));
4645
4646 /* Update guest-interrupt status SVI (leave RVI portion as it is) in the VMCS. */
4647 pVmcs->u16GuestIntStatus = RT_MAKE_U16(uRvi, uVector);
4648
4649 iemVmxPprVirtualization(pVCpu);
4650 if (iemVmxIsEoiInterceptSet(pVCpu, uVector))
4651 return iemVmxVmexit(pVCpu, VMX_EXIT_VIRTUALIZED_EOI, uVector);
4652 iemVmxEvalPendingVirtIntrs(pVCpu);
4653 return VINF_SUCCESS;
4654}
4655
4656
4657/**
4658 * Performs self-IPI virtualization.
4659 *
4660 * @returns VBox strict status code.
4661 * @param pVCpu The cross context virtual CPU structure.
4662 */
4663IEM_STATIC VBOXSTRICTRC iemVmxSelfIpiVirtualization(PVMCPUCC pVCpu)
4664{
4665 PVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
4666 Assert(pVmcs->u32ProcCtls & VMX_PROC_CTLS_USE_TPR_SHADOW);
4667
4668 /*
4669 * We should have already performed the virtual-APIC write to the self-IPI offset
4670 * in the virtual-APIC page. We now perform self-IPI virtualization.
4671 *
4672 * See Intel spec. 29.1.5 "Self-IPI Virtualization".
4673 */
4674 uint8_t const uVector = iemVmxVirtApicReadRaw32(pVCpu, XAPIC_OFF_ICR_LO);
4675 Log2(("self_ipi_virt: uVector=%#x\n", uVector));
4676 iemVmxVirtApicSetVectorInReg(pVCpu, XAPIC_OFF_IRR0, uVector);
4677 uint8_t const uRvi = RT_LO_U8(pVmcs->u16GuestIntStatus);
4678 uint8_t const uSvi = RT_HI_U8(pVmcs->u16GuestIntStatus);
4679 if (uVector > uRvi)
4680 pVmcs->u16GuestIntStatus = RT_MAKE_U16(uVector, uSvi);
4681 iemVmxEvalPendingVirtIntrs(pVCpu);
4682 return VINF_SUCCESS;
4683}
4684
4685
4686/**
4687 * Performs VMX APIC-write emulation.
4688 *
4689 * @returns VBox strict status code.
4690 * @param pVCpu The cross context virtual CPU structure.
4691 */
4692IEM_STATIC VBOXSTRICTRC iemVmxApicWriteEmulation(PVMCPUCC pVCpu)
4693{
4694 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
4695
4696 /* Import the virtual-APIC write offset (part of the hardware-virtualization state). */
4697 IEM_CTX_IMPORT_RET(pVCpu, CPUMCTX_EXTRN_HWVIRT);
4698
4699 /*
4700 * Perform APIC-write emulation based on the virtual-APIC register written.
4701 * See Intel spec. 29.4.3.2 "APIC-Write Emulation".
4702 */
4703 uint16_t const offApicWrite = iemVmxVirtApicClearPendingWrite(pVCpu);
4704 VBOXSTRICTRC rcStrict;
4705 switch (offApicWrite)
4706 {
4707 case XAPIC_OFF_TPR:
4708 {
4709 /* Clear bytes 3:1 of the VTPR and perform TPR virtualization. */
4710 uint32_t uTpr = iemVmxVirtApicReadRaw32(pVCpu, XAPIC_OFF_TPR);
4711 uTpr &= UINT32_C(0x000000ff);
4712 iemVmxVirtApicWriteRaw32(pVCpu, XAPIC_OFF_TPR, uTpr);
4713 Log2(("iemVmxApicWriteEmulation: TPR write %#x\n", uTpr));
4714 rcStrict = iemVmxTprVirtualization(pVCpu);
4715 break;
4716 }
4717
4718 case XAPIC_OFF_EOI:
4719 {
4720 if (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_INT_DELIVERY)
4721 {
4722 /* Clear VEOI and perform EOI virtualization. */
4723 iemVmxVirtApicWriteRaw32(pVCpu, XAPIC_OFF_EOI, 0);
4724 Log2(("iemVmxApicWriteEmulation: EOI write\n"));
4725 rcStrict = iemVmxEoiVirtualization(pVCpu);
4726 }
4727 else
4728 rcStrict = iemVmxVmexitApicWrite(pVCpu, offApicWrite);
4729 break;
4730 }
4731
4732 case XAPIC_OFF_ICR_LO:
4733 {
4734 if (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_INT_DELIVERY)
4735 {
4736 /* If the ICR_LO is valid, write it and perform self-IPI virtualization. */
4737 uint32_t const uIcrLo = iemVmxVirtApicReadRaw32(pVCpu, XAPIC_OFF_TPR);
4738 uint32_t const fIcrLoMb0 = UINT32_C(0xfffbb700);
4739 uint32_t const fIcrLoMb1 = UINT32_C(0x000000f0);
4740 if ( !(uIcrLo & fIcrLoMb0)
4741 && (uIcrLo & fIcrLoMb1))
4742 {
4743 Log2(("iemVmxApicWriteEmulation: Self-IPI virtualization with vector %#x\n", (uIcrLo & 0xff)));
4744 rcStrict = iemVmxSelfIpiVirtualization(pVCpu);
4745 }
4746 else
4747 rcStrict = iemVmxVmexitApicWrite(pVCpu, offApicWrite);
4748 }
4749 else
4750 rcStrict = iemVmxVmexitApicWrite(pVCpu, offApicWrite);
4751 break;
4752 }
4753
4754 case XAPIC_OFF_ICR_HI:
4755 {
4756 /* Clear bytes 2:0 of VICR_HI. No other virtualization or VM-exit must occur. */
4757 uint32_t uIcrHi = iemVmxVirtApicReadRaw32(pVCpu, XAPIC_OFF_ICR_HI);
4758 uIcrHi &= UINT32_C(0xff000000);
4759 iemVmxVirtApicWriteRaw32(pVCpu, XAPIC_OFF_ICR_HI, uIcrHi);
4760 rcStrict = VINF_SUCCESS;
4761 break;
4762 }
4763
4764 default:
4765 {
4766 /* Writes to any other virtual-APIC register causes an APIC-write VM-exit. */
4767 rcStrict = iemVmxVmexitApicWrite(pVCpu, offApicWrite);
4768 break;
4769 }
4770 }
4771
4772 return rcStrict;
4773}
4774
4775
4776/**
4777 * Checks guest control registers, debug registers and MSRs as part of VM-entry.
4778 *
4779 * @param pVCpu The cross context virtual CPU structure.
4780 * @param pszInstr The VMX instruction name (for logging purposes).
4781 */
4782DECLINLINE(int) iemVmxVmentryCheckGuestControlRegsMsrs(PVMCPUCC pVCpu, const char *pszInstr)
4783{
4784 /*
4785 * Guest Control Registers, Debug Registers, and MSRs.
4786 * See Intel spec. 26.3.1.1 "Checks on Guest Control Registers, Debug Registers, and MSRs".
4787 */
4788 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
4789 const char * const pszFailure = "VM-exit";
4790 bool const fUnrestrictedGuest = RT_BOOL(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_UNRESTRICTED_GUEST);
4791
4792 /* CR0 reserved bits. */
4793 {
4794 /* CR0 MB1 bits. */
4795 uint64_t u64Cr0Fixed0 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr0Fixed0;
4796 Assert(!(u64Cr0Fixed0 & (X86_CR0_NW | X86_CR0_CD)));
4797 if (fUnrestrictedGuest)
4798 u64Cr0Fixed0 &= ~(X86_CR0_PE | X86_CR0_PG);
4799 if ((pVmcs->u64GuestCr0.u & u64Cr0Fixed0) == u64Cr0Fixed0)
4800 { /* likely */ }
4801 else
4802 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestCr0Fixed0);
4803
4804 /* CR0 MBZ bits. */
4805 uint64_t const u64Cr0Fixed1 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr0Fixed1;
4806 if (!(pVmcs->u64GuestCr0.u & ~u64Cr0Fixed1))
4807 { /* likely */ }
4808 else
4809 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestCr0Fixed1);
4810
4811 /* Without unrestricted guest support, VT-x supports does not support unpaged protected mode. */
4812 if ( !fUnrestrictedGuest
4813 && (pVmcs->u64GuestCr0.u & X86_CR0_PG)
4814 && !(pVmcs->u64GuestCr0.u & X86_CR0_PE))
4815 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestCr0PgPe);
4816 }
4817
4818 /* CR4 reserved bits. */
4819 {
4820 /* CR4 MB1 bits. */
4821 uint64_t const u64Cr4Fixed0 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr4Fixed0;
4822 if ((pVmcs->u64GuestCr4.u & u64Cr4Fixed0) == u64Cr4Fixed0)
4823 { /* likely */ }
4824 else
4825 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestCr4Fixed0);
4826
4827 /* CR4 MBZ bits. */
4828 uint64_t const u64Cr4Fixed1 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr4Fixed1;
4829 if (!(pVmcs->u64GuestCr4.u & ~u64Cr4Fixed1))
4830 { /* likely */ }
4831 else
4832 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestCr4Fixed1);
4833 }
4834
4835 /* DEBUGCTL MSR. */
4836 if ( !(pVmcs->u32EntryCtls & VMX_ENTRY_CTLS_LOAD_DEBUG)
4837 || !(pVmcs->u64GuestDebugCtlMsr.u & ~MSR_IA32_DEBUGCTL_VALID_MASK_INTEL))
4838 { /* likely */ }
4839 else
4840 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestDebugCtl);
4841
4842 /* 64-bit CPU checks. */
4843 bool const fGstInLongMode = RT_BOOL(pVmcs->u32EntryCtls & VMX_ENTRY_CTLS_IA32E_MODE_GUEST);
4844 if (IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fLongMode)
4845 {
4846 if (fGstInLongMode)
4847 {
4848 /* PAE must be set. */
4849 if ( (pVmcs->u64GuestCr0.u & X86_CR0_PG)
4850 && (pVmcs->u64GuestCr0.u & X86_CR4_PAE))
4851 { /* likely */ }
4852 else
4853 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestPae);
4854 }
4855 else
4856 {
4857 /* PCIDE should not be set. */
4858 if (!(pVmcs->u64GuestCr4.u & X86_CR4_PCIDE))
4859 { /* likely */ }
4860 else
4861 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestPcide);
4862 }
4863
4864 /* CR3. */
4865 if (!(pVmcs->u64GuestCr3.u >> IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cMaxPhysAddrWidth))
4866 { /* likely */ }
4867 else
4868 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestCr3);
4869
4870 /* DR7. */
4871 if ( !(pVmcs->u32EntryCtls & VMX_ENTRY_CTLS_LOAD_DEBUG)
4872 || !(pVmcs->u64GuestDr7.u & X86_DR7_MBZ_MASK))
4873 { /* likely */ }
4874 else
4875 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestDr7);
4876
4877 /* SYSENTER ESP and SYSENTER EIP. */
4878 if ( X86_IS_CANONICAL(pVmcs->u64GuestSysenterEsp.u)
4879 && X86_IS_CANONICAL(pVmcs->u64GuestSysenterEip.u))
4880 { /* likely */ }
4881 else
4882 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSysenterEspEip);
4883 }
4884
4885 /* We don't support IA32_PERF_GLOBAL_CTRL MSR yet. */
4886 Assert(!(pVmcs->u32EntryCtls & VMX_ENTRY_CTLS_LOAD_PERF_MSR));
4887
4888 /* PAT MSR. */
4889 if ( !(pVmcs->u32EntryCtls & VMX_ENTRY_CTLS_LOAD_PAT_MSR)
4890 || CPUMIsPatMsrValid(pVmcs->u64GuestPatMsr.u))
4891 { /* likely */ }
4892 else
4893 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestPatMsr);
4894
4895 /* EFER MSR. */
4896 if (pVmcs->u32EntryCtls & VMX_ENTRY_CTLS_LOAD_EFER_MSR)
4897 {
4898 uint64_t const uValidEferMask = CPUMGetGuestEferMsrValidMask(pVCpu->CTX_SUFF(pVM));
4899 if (!(pVmcs->u64GuestEferMsr.u & ~uValidEferMask))
4900 { /* likely */ }
4901 else
4902 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestEferMsrRsvd);
4903
4904 bool const fGstLma = RT_BOOL(pVmcs->u64GuestEferMsr.u & MSR_K6_EFER_LMA);
4905 bool const fGstLme = RT_BOOL(pVmcs->u64GuestEferMsr.u & MSR_K6_EFER_LME);
4906 if ( fGstLma == fGstInLongMode
4907 && ( !(pVmcs->u64GuestCr0.u & X86_CR0_PG)
4908 || fGstLma == fGstLme))
4909 { /* likely */ }
4910 else
4911 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestEferMsr);
4912 }
4913
4914 /* We don't support IA32_BNDCFGS MSR yet. */
4915 Assert(!(pVmcs->u32EntryCtls & VMX_ENTRY_CTLS_LOAD_BNDCFGS_MSR));
4916
4917 NOREF(pszInstr);
4918 NOREF(pszFailure);
4919 return VINF_SUCCESS;
4920}
4921
4922
4923/**
4924 * Checks guest segment registers, LDTR and TR as part of VM-entry.
4925 *
4926 * @param pVCpu The cross context virtual CPU structure.
4927 * @param pszInstr The VMX instruction name (for logging purposes).
4928 */
4929DECLINLINE(int) iemVmxVmentryCheckGuestSegRegs(PVMCPUCC pVCpu, const char *pszInstr)
4930{
4931 /*
4932 * Segment registers.
4933 * See Intel spec. 26.3.1.2 "Checks on Guest Segment Registers".
4934 */
4935 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
4936 const char * const pszFailure = "VM-exit";
4937 bool const fGstInV86Mode = RT_BOOL(pVmcs->u64GuestRFlags.u & X86_EFL_VM);
4938 bool const fUnrestrictedGuest = RT_BOOL(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_UNRESTRICTED_GUEST);
4939 bool const fGstInLongMode = RT_BOOL(pVmcs->u32EntryCtls & VMX_ENTRY_CTLS_IA32E_MODE_GUEST);
4940
4941 /* Selectors. */
4942 if ( !fGstInV86Mode
4943 && !fUnrestrictedGuest
4944 && (pVmcs->GuestSs & X86_SEL_RPL) != (pVmcs->GuestCs & X86_SEL_RPL))
4945 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegSelCsSsRpl);
4946
4947 for (unsigned iSegReg = 0; iSegReg < X86_SREG_COUNT; iSegReg++)
4948 {
4949 CPUMSELREG SelReg;
4950 int rc = iemVmxVmcsGetGuestSegReg(pVmcs, iSegReg, &SelReg);
4951 if (RT_LIKELY(rc == VINF_SUCCESS))
4952 { /* likely */ }
4953 else
4954 return rc;
4955
4956 /*
4957 * Virtual-8086 mode checks.
4958 */
4959 if (fGstInV86Mode)
4960 {
4961 /* Base address. */
4962 if (SelReg.u64Base == (uint64_t)SelReg.Sel << 4)
4963 { /* likely */ }
4964 else
4965 {
4966 VMXVDIAG const enmDiag = iemVmxGetDiagVmentrySegBaseV86(iSegReg);
4967 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, enmDiag);
4968 }
4969
4970 /* Limit. */
4971 if (SelReg.u32Limit == 0xffff)
4972 { /* likely */ }
4973 else
4974 {
4975 VMXVDIAG const enmDiag = iemVmxGetDiagVmentrySegLimitV86(iSegReg);
4976 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, enmDiag);
4977 }
4978
4979 /* Attribute. */
4980 if (SelReg.Attr.u == 0xf3)
4981 { /* likely */ }
4982 else
4983 {
4984 VMXVDIAG const enmDiag = iemVmxGetDiagVmentrySegAttrV86(iSegReg);
4985 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, enmDiag);
4986 }
4987
4988 /* We're done; move to checking the next segment. */
4989 continue;
4990 }
4991
4992 /* Checks done by 64-bit CPUs. */
4993 if (IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fLongMode)
4994 {
4995 /* Base address. */
4996 if ( iSegReg == X86_SREG_FS
4997 || iSegReg == X86_SREG_GS)
4998 {
4999 if (X86_IS_CANONICAL(SelReg.u64Base))
5000 { /* likely */ }
5001 else
5002 {
5003 VMXVDIAG const enmDiag = iemVmxGetDiagVmentrySegBase(iSegReg);
5004 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, enmDiag);
5005 }
5006 }
5007 else if (iSegReg == X86_SREG_CS)
5008 {
5009 if (!RT_HI_U32(SelReg.u64Base))
5010 { /* likely */ }
5011 else
5012 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegBaseCs);
5013 }
5014 else
5015 {
5016 if ( SelReg.Attr.n.u1Unusable
5017 || !RT_HI_U32(SelReg.u64Base))
5018 { /* likely */ }
5019 else
5020 {
5021 VMXVDIAG const enmDiag = iemVmxGetDiagVmentrySegBase(iSegReg);
5022 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, enmDiag);
5023 }
5024 }
5025 }
5026
5027 /*
5028 * Checks outside Virtual-8086 mode.
5029 */
5030 uint8_t const uSegType = SelReg.Attr.n.u4Type;
5031 uint8_t const fCodeDataSeg = SelReg.Attr.n.u1DescType;
5032 uint8_t const fUsable = !SelReg.Attr.n.u1Unusable;
5033 uint8_t const uDpl = SelReg.Attr.n.u2Dpl;
5034 uint8_t const fPresent = SelReg.Attr.n.u1Present;
5035 uint8_t const uGranularity = SelReg.Attr.n.u1Granularity;
5036 uint8_t const uDefBig = SelReg.Attr.n.u1DefBig;
5037 uint8_t const fSegLong = SelReg.Attr.n.u1Long;
5038
5039 /* Code or usable segment. */
5040 if ( iSegReg == X86_SREG_CS
5041 || fUsable)
5042 {
5043 /* Reserved bits (bits 31:17 and bits 11:8). */
5044 if (!(SelReg.Attr.u & 0xfffe0f00))
5045 { /* likely */ }
5046 else
5047 {
5048 VMXVDIAG const enmDiag = iemVmxGetDiagVmentrySegAttrRsvd(iSegReg);
5049 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, enmDiag);
5050 }
5051
5052 /* Descriptor type. */
5053 if (fCodeDataSeg)
5054 { /* likely */ }
5055 else
5056 {
5057 VMXVDIAG const enmDiag = iemVmxGetDiagVmentrySegAttrDescType(iSegReg);
5058 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, enmDiag);
5059 }
5060
5061 /* Present. */
5062 if (fPresent)
5063 { /* likely */ }
5064 else
5065 {
5066 VMXVDIAG const enmDiag = iemVmxGetDiagVmentrySegAttrPresent(iSegReg);
5067 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, enmDiag);
5068 }
5069
5070 /* Granularity. */
5071 if ( ((SelReg.u32Limit & 0x00000fff) == 0x00000fff || !uGranularity)
5072 && ((SelReg.u32Limit & 0xfff00000) == 0x00000000 || uGranularity))
5073 { /* likely */ }
5074 else
5075 {
5076 VMXVDIAG const enmDiag = iemVmxGetDiagVmentrySegAttrGran(iSegReg);
5077 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, enmDiag);
5078 }
5079 }
5080
5081 if (iSegReg == X86_SREG_CS)
5082 {
5083 /* Segment Type and DPL. */
5084 if ( uSegType == (X86_SEL_TYPE_RW | X86_SEL_TYPE_ACCESSED)
5085 && fUnrestrictedGuest)
5086 {
5087 if (uDpl == 0)
5088 { /* likely */ }
5089 else
5090 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegAttrCsDplZero);
5091 }
5092 else if ( uSegType == (X86_SEL_TYPE_CODE | X86_SEL_TYPE_ACCESSED)
5093 || uSegType == (X86_SEL_TYPE_CODE | X86_SEL_TYPE_READ | X86_SEL_TYPE_ACCESSED))
5094 {
5095 X86DESCATTR AttrSs; AttrSs.u = pVmcs->u32GuestSsAttr;
5096 if (uDpl == AttrSs.n.u2Dpl)
5097 { /* likely */ }
5098 else
5099 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegAttrCsDplEqSs);
5100 }
5101 else if ((uSegType & (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF | X86_SEL_TYPE_ACCESSED))
5102 == (X86_SEL_TYPE_CODE | X86_SEL_TYPE_CONF | X86_SEL_TYPE_ACCESSED))
5103 {
5104 X86DESCATTR AttrSs; AttrSs.u = pVmcs->u32GuestSsAttr;
5105 if (uDpl <= AttrSs.n.u2Dpl)
5106 { /* likely */ }
5107 else
5108 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegAttrCsDplLtSs);
5109 }
5110 else
5111 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegAttrCsType);
5112
5113 /* Def/Big. */
5114 if ( fGstInLongMode
5115 && fSegLong)
5116 {
5117 if (uDefBig == 0)
5118 { /* likely */ }
5119 else
5120 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegAttrCsDefBig);
5121 }
5122 }
5123 else if (iSegReg == X86_SREG_SS)
5124 {
5125 /* Segment Type. */
5126 if ( !fUsable
5127 || uSegType == (X86_SEL_TYPE_RW | X86_SEL_TYPE_ACCESSED)
5128 || uSegType == (X86_SEL_TYPE_DOWN | X86_SEL_TYPE_RW | X86_SEL_TYPE_ACCESSED))
5129 { /* likely */ }
5130 else
5131 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegAttrSsType);
5132
5133 /* DPL. */
5134 if (!fUnrestrictedGuest)
5135 {
5136 if (uDpl == (SelReg.Sel & X86_SEL_RPL))
5137 { /* likely */ }
5138 else
5139 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegAttrSsDplEqRpl);
5140 }
5141 X86DESCATTR AttrCs; AttrCs.u = pVmcs->u32GuestCsAttr;
5142 if ( AttrCs.n.u4Type == (X86_SEL_TYPE_RW | X86_SEL_TYPE_ACCESSED)
5143 || !(pVmcs->u64GuestCr0.u & X86_CR0_PE))
5144 {
5145 if (uDpl == 0)
5146 { /* likely */ }
5147 else
5148 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegAttrSsDplZero);
5149 }
5150 }
5151 else
5152 {
5153 /* DS, ES, FS, GS. */
5154 if (fUsable)
5155 {
5156 /* Segment type. */
5157 if (uSegType & X86_SEL_TYPE_ACCESSED)
5158 { /* likely */ }
5159 else
5160 {
5161 VMXVDIAG const enmDiag = iemVmxGetDiagVmentrySegAttrTypeAcc(iSegReg);
5162 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, enmDiag);
5163 }
5164
5165 if ( !(uSegType & X86_SEL_TYPE_CODE)
5166 || (uSegType & X86_SEL_TYPE_READ))
5167 { /* likely */ }
5168 else
5169 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegAttrCsTypeRead);
5170
5171 /* DPL. */
5172 if ( !fUnrestrictedGuest
5173 && uSegType <= (X86_SEL_TYPE_CODE | X86_SEL_TYPE_READ | X86_SEL_TYPE_ACCESSED))
5174 {
5175 if (uDpl >= (SelReg.Sel & X86_SEL_RPL))
5176 { /* likely */ }
5177 else
5178 {
5179 VMXVDIAG const enmDiag = iemVmxGetDiagVmentrySegAttrDplRpl(iSegReg);
5180 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, enmDiag);
5181 }
5182 }
5183 }
5184 }
5185 }
5186
5187 /*
5188 * LDTR.
5189 */
5190 {
5191 CPUMSELREG Ldtr;
5192 Ldtr.Sel = pVmcs->GuestLdtr;
5193 Ldtr.u32Limit = pVmcs->u32GuestLdtrLimit;
5194 Ldtr.u64Base = pVmcs->u64GuestLdtrBase.u;
5195 Ldtr.Attr.u = pVmcs->u32GuestLdtrAttr;
5196
5197 if (!Ldtr.Attr.n.u1Unusable)
5198 {
5199 /* Selector. */
5200 if (!(Ldtr.Sel & X86_SEL_LDT))
5201 { /* likely */ }
5202 else
5203 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegSelLdtr);
5204
5205 /* Base. */
5206 if (IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fLongMode)
5207 {
5208 if (X86_IS_CANONICAL(Ldtr.u64Base))
5209 { /* likely */ }
5210 else
5211 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegBaseLdtr);
5212 }
5213
5214 /* Attributes. */
5215 /* Reserved bits (bits 31:17 and bits 11:8). */
5216 if (!(Ldtr.Attr.u & 0xfffe0f00))
5217 { /* likely */ }
5218 else
5219 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegAttrLdtrRsvd);
5220
5221 if (Ldtr.Attr.n.u4Type == X86_SEL_TYPE_SYS_LDT)
5222 { /* likely */ }
5223 else
5224 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegAttrLdtrType);
5225
5226 if (!Ldtr.Attr.n.u1DescType)
5227 { /* likely */ }
5228 else
5229 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegAttrLdtrDescType);
5230
5231 if (Ldtr.Attr.n.u1Present)
5232 { /* likely */ }
5233 else
5234 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegAttrLdtrPresent);
5235
5236 if ( ((Ldtr.u32Limit & 0x00000fff) == 0x00000fff || !Ldtr.Attr.n.u1Granularity)
5237 && ((Ldtr.u32Limit & 0xfff00000) == 0x00000000 || Ldtr.Attr.n.u1Granularity))
5238 { /* likely */ }
5239 else
5240 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegAttrLdtrGran);
5241 }
5242 }
5243
5244 /*
5245 * TR.
5246 */
5247 {
5248 CPUMSELREG Tr;
5249 Tr.Sel = pVmcs->GuestTr;
5250 Tr.u32Limit = pVmcs->u32GuestTrLimit;
5251 Tr.u64Base = pVmcs->u64GuestTrBase.u;
5252 Tr.Attr.u = pVmcs->u32GuestTrAttr;
5253
5254 /* Selector. */
5255 if (!(Tr.Sel & X86_SEL_LDT))
5256 { /* likely */ }
5257 else
5258 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegSelTr);
5259
5260 /* Base. */
5261 if (IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fLongMode)
5262 {
5263 if (X86_IS_CANONICAL(Tr.u64Base))
5264 { /* likely */ }
5265 else
5266 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegBaseTr);
5267 }
5268
5269 /* Attributes. */
5270 /* Reserved bits (bits 31:17 and bits 11:8). */
5271 if (!(Tr.Attr.u & 0xfffe0f00))
5272 { /* likely */ }
5273 else
5274 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegAttrTrRsvd);
5275
5276 if (!Tr.Attr.n.u1Unusable)
5277 { /* likely */ }
5278 else
5279 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegAttrTrUnusable);
5280
5281 if ( Tr.Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_BUSY
5282 || ( !fGstInLongMode
5283 && Tr.Attr.n.u4Type == X86_SEL_TYPE_SYS_286_TSS_BUSY))
5284 { /* likely */ }
5285 else
5286 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegAttrTrType);
5287
5288 if (!Tr.Attr.n.u1DescType)
5289 { /* likely */ }
5290 else
5291 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegAttrTrDescType);
5292
5293 if (Tr.Attr.n.u1Present)
5294 { /* likely */ }
5295 else
5296 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegAttrTrPresent);
5297
5298 if ( ((Tr.u32Limit & 0x00000fff) == 0x00000fff || !Tr.Attr.n.u1Granularity)
5299 && ((Tr.u32Limit & 0xfff00000) == 0x00000000 || Tr.Attr.n.u1Granularity))
5300 { /* likely */ }
5301 else
5302 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestSegAttrTrGran);
5303 }
5304
5305 NOREF(pszInstr);
5306 NOREF(pszFailure);
5307 return VINF_SUCCESS;
5308}
5309
5310
5311/**
5312 * Checks guest GDTR and IDTR as part of VM-entry.
5313 *
5314 * @param pVCpu The cross context virtual CPU structure.
5315 * @param pszInstr The VMX instruction name (for logging purposes).
5316 */
5317DECLINLINE(int) iemVmxVmentryCheckGuestGdtrIdtr(PVMCPUCC pVCpu, const char *pszInstr)
5318{
5319 /*
5320 * GDTR and IDTR.
5321 * See Intel spec. 26.3.1.3 "Checks on Guest Descriptor-Table Registers".
5322 */
5323 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
5324 const char *const pszFailure = "VM-exit";
5325
5326 if (IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fLongMode)
5327 {
5328 /* Base. */
5329 if (X86_IS_CANONICAL(pVmcs->u64GuestGdtrBase.u))
5330 { /* likely */ }
5331 else
5332 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestGdtrBase);
5333
5334 if (X86_IS_CANONICAL(pVmcs->u64GuestIdtrBase.u))
5335 { /* likely */ }
5336 else
5337 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestIdtrBase);
5338 }
5339
5340 /* Limit. */
5341 if (!RT_HI_U16(pVmcs->u32GuestGdtrLimit))
5342 { /* likely */ }
5343 else
5344 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestGdtrLimit);
5345
5346 if (!RT_HI_U16(pVmcs->u32GuestIdtrLimit))
5347 { /* likely */ }
5348 else
5349 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestIdtrLimit);
5350
5351 NOREF(pszInstr);
5352 NOREF(pszFailure);
5353 return VINF_SUCCESS;
5354}
5355
5356
5357/**
5358 * Checks guest RIP and RFLAGS as part of VM-entry.
5359 *
5360 * @param pVCpu The cross context virtual CPU structure.
5361 * @param pszInstr The VMX instruction name (for logging purposes).
5362 */
5363DECLINLINE(int) iemVmxVmentryCheckGuestRipRFlags(PVMCPUCC pVCpu, const char *pszInstr)
5364{
5365 /*
5366 * RIP and RFLAGS.
5367 * See Intel spec. 26.3.1.4 "Checks on Guest RIP and RFLAGS".
5368 */
5369 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
5370 const char *const pszFailure = "VM-exit";
5371 bool const fGstInLongMode = RT_BOOL(pVmcs->u32EntryCtls & VMX_ENTRY_CTLS_IA32E_MODE_GUEST);
5372
5373 /* RIP. */
5374 if (IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fLongMode)
5375 {
5376 X86DESCATTR AttrCs;
5377 AttrCs.u = pVmcs->u32GuestCsAttr;
5378 if ( !fGstInLongMode
5379 || !AttrCs.n.u1Long)
5380 {
5381 if (!RT_HI_U32(pVmcs->u64GuestRip.u))
5382 { /* likely */ }
5383 else
5384 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestRipRsvd);
5385 }
5386
5387 if ( fGstInLongMode
5388 && AttrCs.n.u1Long)
5389 {
5390 Assert(IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cMaxLinearAddrWidth == 48); /* Canonical. */
5391 if ( IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cMaxLinearAddrWidth < 64
5392 && X86_IS_CANONICAL(pVmcs->u64GuestRip.u))
5393 { /* likely */ }
5394 else
5395 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestRip);
5396 }
5397 }
5398
5399 /* RFLAGS (bits 63:22 (or 31:22), bits 15, 5, 3 are reserved, bit 1 MB1). */
5400 uint64_t const uGuestRFlags = IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fLongMode ? pVmcs->u64GuestRFlags.u
5401 : pVmcs->u64GuestRFlags.s.Lo;
5402 if ( !(uGuestRFlags & ~(X86_EFL_LIVE_MASK | X86_EFL_RA1_MASK))
5403 && (uGuestRFlags & X86_EFL_RA1_MASK) == X86_EFL_RA1_MASK)
5404 { /* likely */ }
5405 else
5406 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestRFlagsRsvd);
5407
5408 if (!(uGuestRFlags & X86_EFL_VM))
5409 { /* likely */ }
5410 else
5411 {
5412 if ( fGstInLongMode
5413 || !(pVmcs->u64GuestCr0.u & X86_CR0_PE))
5414 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestRFlagsVm);
5415 }
5416
5417 if (VMX_ENTRY_INT_INFO_IS_EXT_INT(pVmcs->u32EntryIntInfo))
5418 {
5419 if (uGuestRFlags & X86_EFL_IF)
5420 { /* likely */ }
5421 else
5422 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestRFlagsIf);
5423 }
5424
5425 NOREF(pszInstr);
5426 NOREF(pszFailure);
5427 return VINF_SUCCESS;
5428}
5429
5430
5431/**
5432 * Checks guest non-register state as part of VM-entry.
5433 *
5434 * @param pVCpu The cross context virtual CPU structure.
5435 * @param pszInstr The VMX instruction name (for logging purposes).
5436 */
5437DECLINLINE(int) iemVmxVmentryCheckGuestNonRegState(PVMCPUCC pVCpu, const char *pszInstr)
5438{
5439 /*
5440 * Guest non-register state.
5441 * See Intel spec. 26.3.1.5 "Checks on Guest Non-Register State".
5442 */
5443 PVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
5444 const char *const pszFailure = "VM-exit";
5445
5446 /*
5447 * Activity state.
5448 */
5449 uint64_t const u64GuestVmxMiscMsr = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Misc;
5450 uint32_t const fActivityStateMask = RT_BF_GET(u64GuestVmxMiscMsr, VMX_BF_MISC_ACTIVITY_STATES);
5451 if (!(pVmcs->u32GuestActivityState & fActivityStateMask))
5452 { /* likely */ }
5453 else
5454 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestActStateRsvd);
5455
5456 X86DESCATTR AttrSs; AttrSs.u = pVmcs->u32GuestSsAttr;
5457 if ( !AttrSs.n.u2Dpl
5458 || pVmcs->u32GuestActivityState != VMX_VMCS_GUEST_ACTIVITY_HLT)
5459 { /* likely */ }
5460 else
5461 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestActStateSsDpl);
5462
5463 if ( pVmcs->u32GuestIntrState == VMX_VMCS_GUEST_INT_STATE_BLOCK_STI
5464 || pVmcs->u32GuestIntrState == VMX_VMCS_GUEST_INT_STATE_BLOCK_MOVSS)
5465 {
5466 if (pVmcs->u32GuestActivityState == VMX_VMCS_GUEST_ACTIVITY_ACTIVE)
5467 { /* likely */ }
5468 else
5469 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestActStateStiMovSs);
5470 }
5471
5472 if (VMX_ENTRY_INT_INFO_IS_VALID(pVmcs->u32EntryIntInfo))
5473 {
5474 uint8_t const uType = VMX_ENTRY_INT_INFO_TYPE(pVmcs->u32EntryIntInfo);
5475 uint8_t const uVector = VMX_ENTRY_INT_INFO_VECTOR(pVmcs->u32EntryIntInfo);
5476 AssertCompile(VMX_V_GUEST_ACTIVITY_STATE_MASK == (VMX_VMCS_GUEST_ACTIVITY_HLT | VMX_VMCS_GUEST_ACTIVITY_SHUTDOWN));
5477 switch (pVmcs->u32GuestActivityState)
5478 {
5479 case VMX_VMCS_GUEST_ACTIVITY_HLT:
5480 {
5481 if ( uType == VMX_ENTRY_INT_INFO_TYPE_EXT_INT
5482 || uType == VMX_ENTRY_INT_INFO_TYPE_NMI
5483 || ( uType == VMX_ENTRY_INT_INFO_TYPE_HW_XCPT
5484 && ( uVector == X86_XCPT_DB
5485 || uVector == X86_XCPT_MC))
5486 || ( uType == VMX_ENTRY_INT_INFO_TYPE_OTHER_EVENT
5487 && uVector == VMX_ENTRY_INT_INFO_VECTOR_MTF))
5488 { /* likely */ }
5489 else
5490 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestActStateHlt);
5491 break;
5492 }
5493
5494 case VMX_VMCS_GUEST_ACTIVITY_SHUTDOWN:
5495 {
5496 if ( uType == VMX_ENTRY_INT_INFO_TYPE_NMI
5497 || ( uType == VMX_ENTRY_INT_INFO_TYPE_HW_XCPT
5498 && uVector == X86_XCPT_MC))
5499 { /* likely */ }
5500 else
5501 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestActStateShutdown);
5502 break;
5503 }
5504
5505 case VMX_VMCS_GUEST_ACTIVITY_ACTIVE:
5506 default:
5507 break;
5508 }
5509 }
5510
5511 /*
5512 * Interruptibility state.
5513 */
5514 if (!(pVmcs->u32GuestIntrState & ~VMX_VMCS_GUEST_INT_STATE_MASK))
5515 { /* likely */ }
5516 else
5517 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestIntStateRsvd);
5518
5519 if ((pVmcs->u32GuestIntrState & (VMX_VMCS_GUEST_INT_STATE_BLOCK_MOVSS | VMX_VMCS_GUEST_INT_STATE_BLOCK_STI))
5520 != (VMX_VMCS_GUEST_INT_STATE_BLOCK_MOVSS | VMX_VMCS_GUEST_INT_STATE_BLOCK_STI))
5521 { /* likely */ }
5522 else
5523 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestIntStateStiMovSs);
5524
5525 if ( (pVmcs->u64GuestRFlags.u & X86_EFL_IF)
5526 || !(pVmcs->u32GuestIntrState & VMX_VMCS_GUEST_INT_STATE_BLOCK_STI))
5527 { /* likely */ }
5528 else
5529 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestIntStateRFlagsSti);
5530
5531 if (VMX_ENTRY_INT_INFO_IS_VALID(pVmcs->u32EntryIntInfo))
5532 {
5533 uint8_t const uType = VMX_ENTRY_INT_INFO_TYPE(pVmcs->u32EntryIntInfo);
5534 if (uType == VMX_ENTRY_INT_INFO_TYPE_EXT_INT)
5535 {
5536 if (!(pVmcs->u32GuestIntrState & (VMX_VMCS_GUEST_INT_STATE_BLOCK_MOVSS | VMX_VMCS_GUEST_INT_STATE_BLOCK_STI)))
5537 { /* likely */ }
5538 else
5539 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestIntStateExtInt);
5540 }
5541 else if (uType == VMX_ENTRY_INT_INFO_TYPE_NMI)
5542 {
5543 if (!(pVmcs->u32GuestIntrState & (VMX_VMCS_GUEST_INT_STATE_BLOCK_MOVSS | VMX_VMCS_GUEST_INT_STATE_BLOCK_STI)))
5544 { /* likely */ }
5545 else
5546 {
5547 /*
5548 * We don't support injecting NMIs when blocking-by-STI would be in effect.
5549 * We update the Exit qualification only when blocking-by-STI is set
5550 * without blocking-by-MovSS being set. Although in practise it does not
5551 * make much difference since the order of checks are implementation defined.
5552 */
5553 if (!(pVmcs->u32GuestIntrState & VMX_VMCS_GUEST_INT_STATE_BLOCK_MOVSS))
5554 iemVmxVmcsSetExitQual(pVCpu, VMX_ENTRY_FAIL_QUAL_NMI_INJECT);
5555 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestIntStateNmi);
5556 }
5557
5558 if ( !(pVmcs->u32PinCtls & VMX_PIN_CTLS_VIRT_NMI)
5559 || !(pVmcs->u32GuestIntrState & VMX_VMCS_GUEST_INT_STATE_BLOCK_NMI))
5560 { /* likely */ }
5561 else
5562 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestIntStateVirtNmi);
5563 }
5564 }
5565
5566 /* We don't support SMM yet. So blocking-by-SMIs must not be set. */
5567 if (!(pVmcs->u32GuestIntrState & VMX_VMCS_GUEST_INT_STATE_BLOCK_SMI))
5568 { /* likely */ }
5569 else
5570 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestIntStateSmi);
5571
5572 /* We don't support SGX yet. So enclave-interruption must not be set. */
5573 if (!(pVmcs->u32GuestIntrState & VMX_VMCS_GUEST_INT_STATE_ENCLAVE))
5574 { /* likely */ }
5575 else
5576 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestIntStateEnclave);
5577
5578 /*
5579 * Pending debug exceptions.
5580 */
5581 uint64_t const uPendingDbgXcpts = IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fLongMode
5582 ? pVmcs->u64GuestPendingDbgXcpts.u
5583 : pVmcs->u64GuestPendingDbgXcpts.s.Lo;
5584 if (!(uPendingDbgXcpts & ~VMX_VMCS_GUEST_PENDING_DEBUG_VALID_MASK))
5585 { /* likely */ }
5586 else
5587 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestPndDbgXcptRsvd);
5588
5589 if ( (pVmcs->u32GuestIntrState & (VMX_VMCS_GUEST_INT_STATE_BLOCK_MOVSS | VMX_VMCS_GUEST_INT_STATE_BLOCK_STI))
5590 || pVmcs->u32GuestActivityState == VMX_VMCS_GUEST_ACTIVITY_HLT)
5591 {
5592 if ( (pVmcs->u64GuestRFlags.u & X86_EFL_TF)
5593 && !(pVmcs->u64GuestDebugCtlMsr.u & MSR_IA32_DEBUGCTL_BTF)
5594 && !(uPendingDbgXcpts & VMX_VMCS_GUEST_PENDING_DEBUG_XCPT_BS))
5595 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestPndDbgXcptBsTf);
5596
5597 if ( ( !(pVmcs->u64GuestRFlags.u & X86_EFL_TF)
5598 || (pVmcs->u64GuestDebugCtlMsr.u & MSR_IA32_DEBUGCTL_BTF))
5599 && (uPendingDbgXcpts & VMX_VMCS_GUEST_PENDING_DEBUG_XCPT_BS))
5600 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestPndDbgXcptBsNoTf);
5601 }
5602
5603 /* We don't support RTM (Real-time Transactional Memory) yet. */
5604 if (!(uPendingDbgXcpts & VMX_VMCS_GUEST_PENDING_DEBUG_RTM))
5605 { /* likely */ }
5606 else
5607 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestPndDbgXcptRtm);
5608
5609 /*
5610 * VMCS link pointer.
5611 */
5612 if (pVmcs->u64VmcsLinkPtr.u != UINT64_C(0xffffffffffffffff))
5613 {
5614 RTGCPHYS const GCPhysShadowVmcs = pVmcs->u64VmcsLinkPtr.u;
5615 /* We don't support SMM yet (so VMCS link pointer cannot be the current VMCS). */
5616 if (GCPhysShadowVmcs != IEM_VMX_GET_CURRENT_VMCS(pVCpu))
5617 { /* likely */ }
5618 else
5619 {
5620 iemVmxVmcsSetExitQual(pVCpu, VMX_ENTRY_FAIL_QUAL_VMCS_LINK_PTR);
5621 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_VmcsLinkPtrCurVmcs);
5622 }
5623
5624 /* Validate the address. */
5625 if ( !(GCPhysShadowVmcs & X86_PAGE_4K_OFFSET_MASK)
5626 && !(GCPhysShadowVmcs >> IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cVmxMaxPhysAddrWidth)
5627 && PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), GCPhysShadowVmcs))
5628 { /* likely */ }
5629 else
5630 {
5631 iemVmxVmcsSetExitQual(pVCpu, VMX_ENTRY_FAIL_QUAL_VMCS_LINK_PTR);
5632 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_AddrVmcsLinkPtr);
5633 }
5634 }
5635
5636 NOREF(pszInstr);
5637 NOREF(pszFailure);
5638 return VINF_SUCCESS;
5639}
5640
5641
5642#ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
5643/**
5644 * Checks guest PDPTEs as part of VM-entry.
5645 *
5646 * @param pVCpu The cross context virtual CPU structure.
5647 * @param pszInstr The VMX instruction name (for logging purposes).
5648 */
5649IEM_STATIC int iemVmxVmentryCheckGuestPdptes(PVMCPUCC pVCpu, const char *pszInstr)
5650{
5651 /*
5652 * Guest PDPTEs.
5653 * See Intel spec. 26.3.1.5 "Checks on Guest Page-Directory-Pointer-Table Entries".
5654 */
5655 PVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
5656 const char * const pszFailure = "VM-exit";
5657
5658 /*
5659 * When EPT is used, we only validate the PAE PDPTEs provided in the VMCS.
5660 * Otherwise, we load any PAE PDPTEs referenced by CR3 at a later point.
5661 */
5662 if ( iemVmxVmcsIsGuestPaePagingEnabled(pVmcs)
5663 && (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_EPT))
5664 {
5665 /* Get PDPTEs from the VMCS. */
5666 X86PDPE aPaePdptes[X86_PG_PAE_PDPE_ENTRIES];
5667 aPaePdptes[0].u = pVmcs->u64GuestPdpte0.u;
5668 aPaePdptes[1].u = pVmcs->u64GuestPdpte1.u;
5669 aPaePdptes[2].u = pVmcs->u64GuestPdpte2.u;
5670 aPaePdptes[3].u = pVmcs->u64GuestPdpte3.u;
5671
5672 /* Check validity of the PDPTEs. */
5673 bool const fValid = PGMGstArePaePdpesValid(pVCpu, &aPaePdptes[0]);
5674 if (fValid)
5675 { /* likely */ }
5676 else
5677 {
5678 iemVmxVmcsSetExitQual(pVCpu, VMX_ENTRY_FAIL_QUAL_PDPTE);
5679 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestPdpte);
5680 }
5681 }
5682
5683 NOREF(pszFailure);
5684 NOREF(pszInstr);
5685 return VINF_SUCCESS;
5686}
5687#endif /* VBOX_WITH_NESTED_HWVIRT_VMX_EPT */
5688
5689
5690/**
5691 * Checks guest-state as part of VM-entry.
5692 *
5693 * @returns VBox status code.
5694 * @param pVCpu The cross context virtual CPU structure.
5695 * @param pszInstr The VMX instruction name (for logging purposes).
5696 */
5697IEM_STATIC int iemVmxVmentryCheckGuestState(PVMCPUCC pVCpu, const char *pszInstr)
5698{
5699 int rc = iemVmxVmentryCheckGuestControlRegsMsrs(pVCpu, pszInstr);
5700 if (RT_SUCCESS(rc))
5701 {
5702 rc = iemVmxVmentryCheckGuestSegRegs(pVCpu, pszInstr);
5703 if (RT_SUCCESS(rc))
5704 {
5705 rc = iemVmxVmentryCheckGuestGdtrIdtr(pVCpu, pszInstr);
5706 if (RT_SUCCESS(rc))
5707 {
5708 rc = iemVmxVmentryCheckGuestRipRFlags(pVCpu, pszInstr);
5709 if (RT_SUCCESS(rc))
5710 {
5711 rc = iemVmxVmentryCheckGuestNonRegState(pVCpu, pszInstr);
5712#ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
5713 if (RT_SUCCESS(rc))
5714 rc = iemVmxVmentryCheckGuestPdptes(pVCpu, pszInstr);
5715#endif
5716 }
5717 }
5718 }
5719 }
5720 return rc;
5721}
5722
5723
5724/**
5725 * Checks host-state as part of VM-entry.
5726 *
5727 * @returns VBox status code.
5728 * @param pVCpu The cross context virtual CPU structure.
5729 * @param pszInstr The VMX instruction name (for logging purposes).
5730 */
5731IEM_STATIC int iemVmxVmentryCheckHostState(PVMCPUCC pVCpu, const char *pszInstr)
5732{
5733 /*
5734 * Host Control Registers and MSRs.
5735 * See Intel spec. 26.2.2 "Checks on Host Control Registers and MSRs".
5736 */
5737 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
5738 const char * const pszFailure = "VMFail";
5739
5740 /* CR0 reserved bits. */
5741 {
5742 /* CR0 MB1 bits. */
5743 uint64_t const u64Cr0Fixed0 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr0Fixed0;
5744 if ((pVmcs->u64HostCr0.u & u64Cr0Fixed0) == u64Cr0Fixed0)
5745 { /* likely */ }
5746 else
5747 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_HostCr0Fixed0);
5748
5749 /* CR0 MBZ bits. */
5750 uint64_t const u64Cr0Fixed1 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr0Fixed1;
5751 if (!(pVmcs->u64HostCr0.u & ~u64Cr0Fixed1))
5752 { /* likely */ }
5753 else
5754 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_HostCr0Fixed1);
5755 }
5756
5757 /* CR4 reserved bits. */
5758 {
5759 /* CR4 MB1 bits. */
5760 uint64_t const u64Cr4Fixed0 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr4Fixed0;
5761 if ((pVmcs->u64HostCr4.u & u64Cr4Fixed0) == u64Cr4Fixed0)
5762 { /* likely */ }
5763 else
5764 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_HostCr4Fixed0);
5765
5766 /* CR4 MBZ bits. */
5767 uint64_t const u64Cr4Fixed1 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr4Fixed1;
5768 if (!(pVmcs->u64HostCr4.u & ~u64Cr4Fixed1))
5769 { /* likely */ }
5770 else
5771 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_HostCr4Fixed1);
5772 }
5773
5774 if (IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fLongMode)
5775 {
5776 /* CR3 reserved bits. */
5777 if (!(pVmcs->u64HostCr3.u >> IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cMaxPhysAddrWidth))
5778 { /* likely */ }
5779 else
5780 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_HostCr3);
5781
5782 /* SYSENTER ESP and SYSENTER EIP. */
5783 if ( X86_IS_CANONICAL(pVmcs->u64HostSysenterEsp.u)
5784 && X86_IS_CANONICAL(pVmcs->u64HostSysenterEip.u))
5785 { /* likely */ }
5786 else
5787 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_HostSysenterEspEip);
5788 }
5789
5790 /* We don't support IA32_PERF_GLOBAL_CTRL MSR yet. */
5791 Assert(!(pVmcs->u32ExitCtls & VMX_EXIT_CTLS_LOAD_PERF_MSR));
5792
5793 /* PAT MSR. */
5794 if ( !(pVmcs->u32ExitCtls & VMX_EXIT_CTLS_LOAD_PAT_MSR)
5795 || CPUMIsPatMsrValid(pVmcs->u64HostPatMsr.u))
5796 { /* likely */ }
5797 else
5798 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_HostPatMsr);
5799
5800 /* EFER MSR. */
5801 uint64_t const uValidEferMask = CPUMGetGuestEferMsrValidMask(pVCpu->CTX_SUFF(pVM));
5802 if ( !(pVmcs->u32ExitCtls & VMX_EXIT_CTLS_LOAD_EFER_MSR)
5803 || !(pVmcs->u64HostEferMsr.u & ~uValidEferMask))
5804 { /* likely */ }
5805 else
5806 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_HostEferMsrRsvd);
5807
5808 bool const fHostInLongMode = RT_BOOL(pVmcs->u32ExitCtls & VMX_EXIT_CTLS_HOST_ADDR_SPACE_SIZE);
5809 bool const fHostLma = RT_BOOL(pVmcs->u64HostEferMsr.u & MSR_K6_EFER_LMA);
5810 bool const fHostLme = RT_BOOL(pVmcs->u64HostEferMsr.u & MSR_K6_EFER_LME);
5811 if ( fHostInLongMode == fHostLma
5812 && fHostInLongMode == fHostLme)
5813 { /* likely */ }
5814 else
5815 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_HostEferMsr);
5816
5817 /*
5818 * Host Segment and Descriptor-Table Registers.
5819 * See Intel spec. 26.2.3 "Checks on Host Segment and Descriptor-Table Registers".
5820 */
5821 /* Selector RPL and TI. */
5822 if ( !(pVmcs->HostCs & (X86_SEL_RPL | X86_SEL_LDT))
5823 && !(pVmcs->HostSs & (X86_SEL_RPL | X86_SEL_LDT))
5824 && !(pVmcs->HostDs & (X86_SEL_RPL | X86_SEL_LDT))
5825 && !(pVmcs->HostEs & (X86_SEL_RPL | X86_SEL_LDT))
5826 && !(pVmcs->HostFs & (X86_SEL_RPL | X86_SEL_LDT))
5827 && !(pVmcs->HostGs & (X86_SEL_RPL | X86_SEL_LDT))
5828 && !(pVmcs->HostTr & (X86_SEL_RPL | X86_SEL_LDT)))
5829 { /* likely */ }
5830 else
5831 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_HostSel);
5832
5833 /* CS and TR selectors cannot be 0. */
5834 if ( pVmcs->HostCs
5835 && pVmcs->HostTr)
5836 { /* likely */ }
5837 else
5838 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_HostCsTr);
5839
5840 /* SS cannot be 0 if 32-bit host. */
5841 if ( fHostInLongMode
5842 || pVmcs->HostSs)
5843 { /* likely */ }
5844 else
5845 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_HostSs);
5846
5847 if (IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fLongMode)
5848 {
5849 /* FS, GS, GDTR, IDTR, TR base address. */
5850 if ( X86_IS_CANONICAL(pVmcs->u64HostFsBase.u)
5851 && X86_IS_CANONICAL(pVmcs->u64HostFsBase.u)
5852 && X86_IS_CANONICAL(pVmcs->u64HostGdtrBase.u)
5853 && X86_IS_CANONICAL(pVmcs->u64HostIdtrBase.u)
5854 && X86_IS_CANONICAL(pVmcs->u64HostTrBase.u))
5855 { /* likely */ }
5856 else
5857 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_HostSegBase);
5858 }
5859
5860 /*
5861 * Host address-space size for 64-bit CPUs.
5862 * See Intel spec. 26.2.4 "Checks Related to Address-Space Size".
5863 */
5864 bool const fGstInLongMode = RT_BOOL(pVmcs->u32EntryCtls & VMX_ENTRY_CTLS_IA32E_MODE_GUEST);
5865 if (IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fLongMode)
5866 {
5867 bool const fCpuInLongMode = CPUMIsGuestInLongMode(pVCpu);
5868
5869 /* Logical processor in IA-32e mode. */
5870 if (fCpuInLongMode)
5871 {
5872 if (fHostInLongMode)
5873 {
5874 /* PAE must be set. */
5875 if (pVmcs->u64HostCr4.u & X86_CR4_PAE)
5876 { /* likely */ }
5877 else
5878 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_HostCr4Pae);
5879
5880 /* RIP must be canonical. */
5881 if (X86_IS_CANONICAL(pVmcs->u64HostRip.u))
5882 { /* likely */ }
5883 else
5884 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_HostRip);
5885 }
5886 else
5887 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_HostLongMode);
5888 }
5889 else
5890 {
5891 /* Logical processor is outside IA-32e mode. */
5892 if ( !fGstInLongMode
5893 && !fHostInLongMode)
5894 {
5895 /* PCIDE should not be set. */
5896 if (!(pVmcs->u64HostCr4.u & X86_CR4_PCIDE))
5897 { /* likely */ }
5898 else
5899 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_HostCr4Pcide);
5900
5901 /* The high 32-bits of RIP MBZ. */
5902 if (!pVmcs->u64HostRip.s.Hi)
5903 { /* likely */ }
5904 else
5905 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_HostRipRsvd);
5906 }
5907 else
5908 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_HostGuestLongMode);
5909 }
5910 }
5911 else
5912 {
5913 /* Host address-space size for 32-bit CPUs. */
5914 if ( !fGstInLongMode
5915 && !fHostInLongMode)
5916 { /* likely */ }
5917 else
5918 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_HostGuestLongModeNoCpu);
5919 }
5920
5921 NOREF(pszInstr);
5922 NOREF(pszFailure);
5923 return VINF_SUCCESS;
5924}
5925
5926
5927#ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
5928/**
5929 * Checks the EPT pointer VMCS field as part of VM-entry.
5930 *
5931 * @returns VBox status code.
5932 * @param pVCpu The cross context virtual CPU structure.
5933 * @param uEptPtr The EPT pointer to check.
5934 * @param penmVmxDiag Where to store the diagnostic reason on failure (not
5935 * updated on success). Optional, can be NULL.
5936 */
5937IEM_STATIC int iemVmxVmentryCheckEptPtr(PVMCPUCC pVCpu, uint64_t uEptPtr, VMXVDIAG *penmVmxDiag)
5938{
5939 VMXVDIAG enmVmxDiag;
5940
5941 /* Reserved bits. */
5942 uint8_t const cMaxPhysAddrWidth = IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cMaxPhysAddrWidth;
5943 uint64_t const fValidMask = VMX_EPTP_VALID_MASK & ~(UINT64_MAX << cMaxPhysAddrWidth);
5944 if (uEptPtr & fValidMask)
5945 {
5946 /* Memory Type. */
5947 uint64_t const fCaps = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64EptVpidCaps;
5948 uint8_t const fMemType = RT_BF_GET(uEptPtr, VMX_BF_EPTP_MEMTYPE);
5949 if ( ( fMemType == VMX_EPTP_MEMTYPE_WB
5950 && RT_BF_GET(fCaps, VMX_BF_EPT_VPID_CAP_MEMTYPE_WB))
5951 || ( fMemType == VMX_EPTP_MEMTYPE_UC
5952 && RT_BF_GET(fCaps, VMX_BF_EPT_VPID_CAP_MEMTYPE_UC)))
5953 {
5954 /*
5955 * Page walk length (PML4).
5956 * Intel used to specify bit 7 of IA32_VMX_EPT_VPID_CAP as page walk length
5957 * of 5 but that seems to be removed from the latest specs. leaving only PML4
5958 * as the maximum supported page-walk level hence we hardcode it as 3 (1 less than 4)
5959 */
5960 Assert(RT_BF_GET(fCaps, VMX_BF_EPT_VPID_CAP_PAGE_WALK_LENGTH_4));
5961 if (RT_BF_GET(uEptPtr, VMX_BF_EPTP_PAGE_WALK_LENGTH) == 3)
5962 {
5963 /* Access and dirty bits support in EPT structures. */
5964 if ( !RT_BF_GET(uEptPtr, VMX_BF_EPTP_ACCESS_DIRTY)
5965 || RT_BF_GET(fCaps, VMX_BF_EPT_VPID_CAP_ACCESS_DIRTY))
5966 return VINF_SUCCESS;
5967
5968 enmVmxDiag = kVmxVDiag_Vmentry_EptpAccessDirty;
5969 }
5970 else
5971 enmVmxDiag = kVmxVDiag_Vmentry_EptpPageWalkLength;
5972 }
5973 else
5974 enmVmxDiag = kVmxVDiag_Vmentry_EptpMemType;
5975 }
5976 else
5977 enmVmxDiag = kVmxVDiag_Vmentry_EptpRsvd;
5978
5979 if (penmVmxDiag)
5980 *penmVmxDiag = enmVmxDiag;
5981 return VERR_VMX_VMENTRY_FAILED;
5982}
5983#endif
5984
5985
5986/**
5987 * Checks VMCS controls fields as part of VM-entry.
5988 *
5989 * @returns VBox status code.
5990 * @param pVCpu The cross context virtual CPU structure.
5991 * @param pszInstr The VMX instruction name (for logging purposes).
5992 *
5993 * @remarks This may update secondary-processor based VM-execution control fields
5994 * in the current VMCS if necessary.
5995 */
5996IEM_STATIC int iemVmxVmentryCheckCtls(PVMCPUCC pVCpu, const char *pszInstr)
5997{
5998 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
5999 const char * const pszFailure = "VMFail";
6000 bool const fVmxTrueMsrs = RT_BOOL(pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Basic & VMX_BF_BASIC_TRUE_CTLS_MASK);
6001
6002 /*
6003 * VM-execution controls.
6004 * See Intel spec. 26.2.1.1 "VM-Execution Control Fields".
6005 */
6006 {
6007 /* Pin-based VM-execution controls. */
6008 {
6009 VMXCTLSMSR const PinCtls = fVmxTrueMsrs ? pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.TruePinCtls
6010 : pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.PinCtls;
6011 if (!(~pVmcs->u32PinCtls & PinCtls.n.allowed0))
6012 { /* likely */ }
6013 else
6014 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_PinCtlsDisallowed0);
6015
6016 if (!(pVmcs->u32PinCtls & ~PinCtls.n.allowed1))
6017 { /* likely */ }
6018 else
6019 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_PinCtlsAllowed1);
6020 }
6021
6022 /* Processor-based VM-execution controls. */
6023 {
6024 VMXCTLSMSR const ProcCtls = fVmxTrueMsrs ? pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.TrueProcCtls
6025 : pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.ProcCtls;
6026 if (!(~pVmcs->u32ProcCtls & ProcCtls.n.allowed0))
6027 { /* likely */ }
6028 else
6029 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_ProcCtlsDisallowed0);
6030
6031 if (!(pVmcs->u32ProcCtls & ~ProcCtls.n.allowed1))
6032 { /* likely */ }
6033 else
6034 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_ProcCtlsAllowed1);
6035 }
6036
6037 /* Secondary processor-based VM-execution controls. */
6038 if (pVmcs->u32ProcCtls & VMX_PROC_CTLS_USE_SECONDARY_CTLS)
6039 {
6040 VMXCTLSMSR const ProcCtls2 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.ProcCtls2;
6041 if (!(~pVmcs->u32ProcCtls2 & ProcCtls2.n.allowed0))
6042 { /* likely */ }
6043 else
6044 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_ProcCtls2Disallowed0);
6045
6046 if (!(pVmcs->u32ProcCtls2 & ~ProcCtls2.n.allowed1))
6047 { /* likely */ }
6048 else
6049 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_ProcCtls2Allowed1);
6050 }
6051 else
6052 Assert(!pVmcs->u32ProcCtls2);
6053
6054 /* CR3-target count. */
6055 if (pVmcs->u32Cr3TargetCount <= VMX_V_CR3_TARGET_COUNT)
6056 { /* likely */ }
6057 else
6058 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_Cr3TargetCount);
6059
6060 /* I/O bitmaps physical addresses. */
6061 if (pVmcs->u32ProcCtls & VMX_PROC_CTLS_USE_IO_BITMAPS)
6062 {
6063 RTGCPHYS const GCPhysIoBitmapA = pVmcs->u64AddrIoBitmapA.u;
6064 if ( !(GCPhysIoBitmapA & X86_PAGE_4K_OFFSET_MASK)
6065 && !(GCPhysIoBitmapA >> IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cVmxMaxPhysAddrWidth)
6066 && PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), GCPhysIoBitmapA))
6067 { /* likely */ }
6068 else
6069 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_AddrIoBitmapA);
6070
6071 RTGCPHYS const GCPhysIoBitmapB = pVmcs->u64AddrIoBitmapB.u;
6072 if ( !(GCPhysIoBitmapB & X86_PAGE_4K_OFFSET_MASK)
6073 && !(GCPhysIoBitmapB >> IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cVmxMaxPhysAddrWidth)
6074 && PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), GCPhysIoBitmapB))
6075 { /* likely */ }
6076 else
6077 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_AddrIoBitmapB);
6078 }
6079
6080 /* MSR bitmap physical address. */
6081 if (pVmcs->u32ProcCtls & VMX_PROC_CTLS_USE_MSR_BITMAPS)
6082 {
6083 RTGCPHYS const GCPhysMsrBitmap = pVmcs->u64AddrMsrBitmap.u;
6084 if ( !(GCPhysMsrBitmap & X86_PAGE_4K_OFFSET_MASK)
6085 && !(GCPhysMsrBitmap >> IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cVmxMaxPhysAddrWidth)
6086 && PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), GCPhysMsrBitmap))
6087 { /* likely */ }
6088 else
6089 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_AddrMsrBitmap);
6090 }
6091
6092 /* TPR shadow related controls. */
6093 if (pVmcs->u32ProcCtls & VMX_PROC_CTLS_USE_TPR_SHADOW)
6094 {
6095 /* Virtual-APIC page physical address. */
6096 RTGCPHYS const GCPhysVirtApic = pVmcs->u64AddrVirtApic.u;
6097 if ( !(GCPhysVirtApic & X86_PAGE_4K_OFFSET_MASK)
6098 && !(GCPhysVirtApic >> IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cVmxMaxPhysAddrWidth)
6099 && PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), GCPhysVirtApic))
6100 { /* likely */ }
6101 else
6102 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_AddrVirtApicPage);
6103
6104 /* TPR threshold bits 31:4 MBZ without virtual-interrupt delivery. */
6105 if ( !(pVmcs->u32TprThreshold & ~VMX_TPR_THRESHOLD_MASK)
6106 || (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_INT_DELIVERY))
6107 { /* likely */ }
6108 else
6109 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_TprThresholdRsvd);
6110
6111 /* The rest done XXX document */
6112 }
6113 else
6114 {
6115 if ( !(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_X2APIC_MODE)
6116 && !(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_APIC_REG_VIRT)
6117 && !(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_INT_DELIVERY))
6118 { /* likely */ }
6119 else
6120 {
6121 if (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_X2APIC_MODE)
6122 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_VirtX2ApicTprShadow);
6123 if (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_APIC_REG_VIRT)
6124 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_ApicRegVirt);
6125 Assert(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_INT_DELIVERY);
6126 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_VirtIntDelivery);
6127 }
6128 }
6129
6130 /* NMI exiting and virtual-NMIs. */
6131 if ( (pVmcs->u32PinCtls & VMX_PIN_CTLS_NMI_EXIT)
6132 || !(pVmcs->u32PinCtls & VMX_PIN_CTLS_VIRT_NMI))
6133 { /* likely */ }
6134 else
6135 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_VirtNmi);
6136
6137 /* Virtual-NMIs and NMI-window exiting. */
6138 if ( (pVmcs->u32PinCtls & VMX_PIN_CTLS_VIRT_NMI)
6139 || !(pVmcs->u32ProcCtls & VMX_PROC_CTLS_NMI_WINDOW_EXIT))
6140 { /* likely */ }
6141 else
6142 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_NmiWindowExit);
6143
6144 /* Virtualize APIC accesses. */
6145 if (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_APIC_ACCESS)
6146 {
6147 /* APIC-access physical address. */
6148 RTGCPHYS const GCPhysApicAccess = pVmcs->u64AddrApicAccess.u;
6149 if ( !(GCPhysApicAccess & X86_PAGE_4K_OFFSET_MASK)
6150 && !(GCPhysApicAccess >> IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cVmxMaxPhysAddrWidth)
6151 && PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), GCPhysApicAccess))
6152 { /* likely */ }
6153 else
6154 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_AddrApicAccess);
6155
6156 /*
6157 * Disallow APIC-access page and virtual-APIC page from being the same address.
6158 * Note! This is not an Intel requirement, but one imposed by our implementation.
6159 */
6160 /** @todo r=ramshankar: This is done primarily to simplify recursion scenarios while
6161 * redirecting accesses between the APIC-access page and the virtual-APIC
6162 * page. If any nested hypervisor requires this, we can implement it later. */
6163 if (pVmcs->u32ProcCtls & VMX_PROC_CTLS_USE_TPR_SHADOW)
6164 {
6165 RTGCPHYS const GCPhysVirtApic = pVmcs->u64AddrVirtApic.u;
6166 if (GCPhysVirtApic != GCPhysApicAccess)
6167 { /* likely */ }
6168 else
6169 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_AddrApicAccessEqVirtApic);
6170 }
6171 }
6172
6173 /* Virtualize-x2APIC mode is mutually exclusive with virtualize-APIC accesses. */
6174 if ( !(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_X2APIC_MODE)
6175 || !(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_APIC_ACCESS))
6176 { /* likely */ }
6177 else
6178 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_VirtX2ApicVirtApic);
6179
6180 /* Virtual-interrupt delivery requires external interrupt exiting. */
6181 if ( !(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_INT_DELIVERY)
6182 || (pVmcs->u32PinCtls & VMX_PIN_CTLS_EXT_INT_EXIT))
6183 { /* likely */ }
6184 else
6185 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_VirtX2ApicVirtApic);
6186
6187 /* VPID. */
6188 if ( !(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VPID)
6189 || pVmcs->u16Vpid != 0)
6190 { /* likely */ }
6191 else
6192 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_Vpid);
6193
6194#ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
6195 /* Extended-Page-Table Pointer (EPTP). */
6196 if (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_EPT)
6197 {
6198 VMXVDIAG enmVmxDiag;
6199 int const rc = iemVmxVmentryCheckEptPtr(pVCpu, pVmcs->u64EptPtr.u, &enmVmxDiag);
6200 if (RT_SUCCESS(rc))
6201 { /* likely */ }
6202 else
6203 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, enmVmxDiag);
6204 }
6205#else
6206 Assert(!(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_EPT)); /* Support for EPT is conditional. */
6207 Assert(!(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_UNRESTRICTED_GUEST)); /* Support for Unrestricted-guests is conditional. */
6208#endif
6209
6210 Assert(!(pVmcs->u32PinCtls & VMX_PIN_CTLS_POSTED_INT)); /* We don't support posted interrupts yet. */
6211 Assert(!(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_PML)); /* We don't support PML yet. */
6212 Assert(!(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VMFUNC)); /* We don't support VM functions yet. */
6213 Assert(!(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_EPT_XCPT_VE)); /* We don't support EPT-violation #VE yet. */
6214 Assert(!(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_PAUSE_LOOP_EXIT)); /* We don't support Pause-loop exiting yet. */
6215 Assert(!(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_TSC_SCALING)); /* We don't support TSC-scaling yet. */
6216
6217 /* VMCS shadowing. */
6218 if (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VMCS_SHADOWING)
6219 {
6220 /* VMREAD-bitmap physical address. */
6221 RTGCPHYS const GCPhysVmreadBitmap = pVmcs->u64AddrVmreadBitmap.u;
6222 if ( !(GCPhysVmreadBitmap & X86_PAGE_4K_OFFSET_MASK)
6223 && !(GCPhysVmreadBitmap >> IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cVmxMaxPhysAddrWidth)
6224 && PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), GCPhysVmreadBitmap))
6225 { /* likely */ }
6226 else
6227 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_AddrVmreadBitmap);
6228
6229 /* VMWRITE-bitmap physical address. */
6230 RTGCPHYS const GCPhysVmwriteBitmap = pVmcs->u64AddrVmreadBitmap.u;
6231 if ( !(GCPhysVmwriteBitmap & X86_PAGE_4K_OFFSET_MASK)
6232 && !(GCPhysVmwriteBitmap >> IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cVmxMaxPhysAddrWidth)
6233 && PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), GCPhysVmwriteBitmap))
6234 { /* likely */ }
6235 else
6236 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_AddrVmwriteBitmap);
6237 }
6238 }
6239
6240 /*
6241 * VM-exit controls.
6242 * See Intel spec. 26.2.1.2 "VM-Exit Control Fields".
6243 */
6244 {
6245 VMXCTLSMSR const ExitCtls = fVmxTrueMsrs ? pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.TrueExitCtls
6246 : pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.ExitCtls;
6247 if (!(~pVmcs->u32ExitCtls & ExitCtls.n.allowed0))
6248 { /* likely */ }
6249 else
6250 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_ExitCtlsDisallowed0);
6251
6252 if (!(pVmcs->u32ExitCtls & ~ExitCtls.n.allowed1))
6253 { /* likely */ }
6254 else
6255 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_ExitCtlsAllowed1);
6256
6257 /* Save preemption timer without activating it. */
6258 if ( (pVmcs->u32PinCtls & VMX_PIN_CTLS_PREEMPT_TIMER)
6259 || !(pVmcs->u32ProcCtls & VMX_EXIT_CTLS_SAVE_PREEMPT_TIMER))
6260 { /* likely */ }
6261 else
6262 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_SavePreemptTimer);
6263
6264 /* VM-exit MSR-store count and VM-exit MSR-store area address. */
6265 if (pVmcs->u32ExitMsrStoreCount)
6266 {
6267 if ( !(pVmcs->u64AddrExitMsrStore.u & VMX_AUTOMSR_OFFSET_MASK)
6268 && !(pVmcs->u64AddrExitMsrStore.u >> IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cVmxMaxPhysAddrWidth)
6269 && PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), pVmcs->u64AddrExitMsrStore.u))
6270 { /* likely */ }
6271 else
6272 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_AddrExitMsrStore);
6273 }
6274
6275 /* VM-exit MSR-load count and VM-exit MSR-load area address. */
6276 if (pVmcs->u32ExitMsrLoadCount)
6277 {
6278 if ( !(pVmcs->u64AddrExitMsrLoad.u & VMX_AUTOMSR_OFFSET_MASK)
6279 && !(pVmcs->u64AddrExitMsrLoad.u >> IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cVmxMaxPhysAddrWidth)
6280 && PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), pVmcs->u64AddrExitMsrLoad.u))
6281 { /* likely */ }
6282 else
6283 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_AddrExitMsrLoad);
6284 }
6285 }
6286
6287 /*
6288 * VM-entry controls.
6289 * See Intel spec. 26.2.1.3 "VM-Entry Control Fields".
6290 */
6291 {
6292 VMXCTLSMSR const EntryCtls = fVmxTrueMsrs ? pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.TrueEntryCtls
6293 : pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.EntryCtls;
6294 if (!(~pVmcs->u32EntryCtls & EntryCtls.n.allowed0))
6295 { /* likely */ }
6296 else
6297 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_EntryCtlsDisallowed0);
6298
6299 if (!(pVmcs->u32EntryCtls & ~EntryCtls.n.allowed1))
6300 { /* likely */ }
6301 else
6302 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_EntryCtlsAllowed1);
6303
6304 /* Event injection. */
6305 uint32_t const uIntInfo = pVmcs->u32EntryIntInfo;
6306 if (RT_BF_GET(uIntInfo, VMX_BF_ENTRY_INT_INFO_VALID))
6307 {
6308 /* Type and vector. */
6309 uint8_t const uType = RT_BF_GET(uIntInfo, VMX_BF_ENTRY_INT_INFO_TYPE);
6310 uint8_t const uVector = RT_BF_GET(uIntInfo, VMX_BF_ENTRY_INT_INFO_VECTOR);
6311 uint8_t const uRsvd = RT_BF_GET(uIntInfo, VMX_BF_ENTRY_INT_INFO_RSVD_12_30);
6312 if ( !uRsvd
6313 && VMXIsEntryIntInfoTypeValid(IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fVmxMonitorTrapFlag, uType)
6314 && VMXIsEntryIntInfoVectorValid(uVector, uType))
6315 { /* likely */ }
6316 else
6317 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_EntryIntInfoTypeVecRsvd);
6318
6319 /* Exception error code. */
6320 if (RT_BF_GET(uIntInfo, VMX_BF_ENTRY_INT_INFO_ERR_CODE_VALID))
6321 {
6322 /* Delivery possible only in Unrestricted-guest mode when CR0.PE is set. */
6323 if ( !(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_UNRESTRICTED_GUEST)
6324 || (pVmcs->u64GuestCr0.s.Lo & X86_CR0_PE))
6325 { /* likely */ }
6326 else
6327 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_EntryIntInfoErrCodePe);
6328
6329 /* Exceptions that provide an error code. */
6330 if ( uType == VMX_ENTRY_INT_INFO_TYPE_HW_XCPT
6331 && ( uVector == X86_XCPT_DF
6332 || uVector == X86_XCPT_TS
6333 || uVector == X86_XCPT_NP
6334 || uVector == X86_XCPT_SS
6335 || uVector == X86_XCPT_GP
6336 || uVector == X86_XCPT_PF
6337 || uVector == X86_XCPT_AC))
6338 { /* likely */ }
6339 else
6340 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_EntryIntInfoErrCodeVec);
6341
6342 /* Exception error-code reserved bits. */
6343 if (!(pVmcs->u32EntryXcptErrCode & ~VMX_ENTRY_INT_XCPT_ERR_CODE_VALID_MASK))
6344 { /* likely */ }
6345 else
6346 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_EntryXcptErrCodeRsvd);
6347
6348 /* Injecting a software interrupt, software exception or privileged software exception. */
6349 if ( uType == VMX_ENTRY_INT_INFO_TYPE_SW_INT
6350 || uType == VMX_ENTRY_INT_INFO_TYPE_SW_XCPT
6351 || uType == VMX_ENTRY_INT_INFO_TYPE_PRIV_SW_XCPT)
6352 {
6353 /* Instruction length must be in the range 0-15. */
6354 if (pVmcs->u32EntryInstrLen <= VMX_ENTRY_INSTR_LEN_MAX)
6355 { /* likely */ }
6356 else
6357 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_EntryInstrLen);
6358
6359 /* However, instruction length of 0 is allowed only when its CPU feature is present. */
6360 if ( pVmcs->u32EntryInstrLen != 0
6361 || IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fVmxEntryInjectSoftInt)
6362 { /* likely */ }
6363 else
6364 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_EntryInstrLenZero);
6365 }
6366 }
6367 }
6368
6369 /* VM-entry MSR-load count and VM-entry MSR-load area address. */
6370 if (pVmcs->u32EntryMsrLoadCount)
6371 {
6372 if ( !(pVmcs->u64AddrEntryMsrLoad.u & VMX_AUTOMSR_OFFSET_MASK)
6373 && !(pVmcs->u64AddrEntryMsrLoad.u >> IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cVmxMaxPhysAddrWidth)
6374 && PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), pVmcs->u64AddrEntryMsrLoad.u))
6375 { /* likely */ }
6376 else
6377 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_AddrEntryMsrLoad);
6378 }
6379
6380 Assert(!(pVmcs->u32EntryCtls & VMX_ENTRY_CTLS_ENTRY_TO_SMM)); /* We don't support SMM yet. */
6381 Assert(!(pVmcs->u32EntryCtls & VMX_ENTRY_CTLS_DEACTIVATE_DUAL_MON)); /* We don't support dual-monitor treatment yet. */
6382 }
6383
6384 NOREF(pszInstr);
6385 NOREF(pszFailure);
6386 return VINF_SUCCESS;
6387}
6388
6389
6390/**
6391 * Loads the guest control registers, debug register and some MSRs as part of
6392 * VM-entry.
6393 *
6394 * @param pVCpu The cross context virtual CPU structure.
6395 */
6396IEM_STATIC void iemVmxVmentryLoadGuestControlRegsMsrs(PVMCPUCC pVCpu)
6397{
6398 /*
6399 * Load guest control registers, debug registers and MSRs.
6400 * See Intel spec. 26.3.2.1 "Loading Guest Control Registers, Debug Registers and MSRs".
6401 */
6402 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
6403
6404 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR0);
6405 uint64_t const uGstCr0 = (pVmcs->u64GuestCr0.u & ~VMX_ENTRY_GUEST_CR0_IGNORE_MASK)
6406 | (pVCpu->cpum.GstCtx.cr0 & VMX_ENTRY_GUEST_CR0_IGNORE_MASK);
6407 pVCpu->cpum.GstCtx.cr0 = uGstCr0;
6408 pVCpu->cpum.GstCtx.cr4 = pVmcs->u64GuestCr4.u;
6409 pVCpu->cpum.GstCtx.cr3 = pVmcs->u64GuestCr3.u;
6410
6411 if (pVmcs->u32EntryCtls & VMX_ENTRY_CTLS_LOAD_DEBUG)
6412 pVCpu->cpum.GstCtx.dr[7] = (pVmcs->u64GuestDr7.u & ~VMX_ENTRY_GUEST_DR7_MBZ_MASK) | VMX_ENTRY_GUEST_DR7_MB1_MASK;
6413
6414 pVCpu->cpum.GstCtx.SysEnter.eip = pVmcs->u64GuestSysenterEip.s.Lo;
6415 pVCpu->cpum.GstCtx.SysEnter.esp = pVmcs->u64GuestSysenterEsp.s.Lo;
6416 pVCpu->cpum.GstCtx.SysEnter.cs = pVmcs->u32GuestSysenterCS;
6417
6418 if (IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fLongMode)
6419 {
6420 /* FS base and GS base are loaded while loading the rest of the guest segment registers. */
6421
6422 /* EFER MSR. */
6423 if (!(pVmcs->u32EntryCtls & VMX_ENTRY_CTLS_LOAD_EFER_MSR))
6424 {
6425 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_EFER);
6426 uint64_t const uHostEfer = pVCpu->cpum.GstCtx.msrEFER;
6427 bool const fGstInLongMode = RT_BOOL(pVmcs->u32EntryCtls & VMX_ENTRY_CTLS_IA32E_MODE_GUEST);
6428 bool const fGstPaging = RT_BOOL(uGstCr0 & X86_CR0_PG);
6429 if (fGstInLongMode)
6430 {
6431 /* If the nested-guest is in long mode, LMA and LME are both set. */
6432 Assert(fGstPaging);
6433 pVCpu->cpum.GstCtx.msrEFER = uHostEfer | (MSR_K6_EFER_LMA | MSR_K6_EFER_LME);
6434 }
6435 else
6436 {
6437 /*
6438 * If the nested-guest is outside long mode:
6439 * - With paging: LMA is cleared, LME is cleared.
6440 * - Without paging: LMA is cleared, LME is left unmodified.
6441 */
6442 uint64_t const fLmaLmeMask = MSR_K6_EFER_LMA | (fGstPaging ? MSR_K6_EFER_LME : 0);
6443 pVCpu->cpum.GstCtx.msrEFER = uHostEfer & ~fLmaLmeMask;
6444 }
6445 }
6446 /* else: see below. */
6447 }
6448
6449 /* PAT MSR. */
6450 if (pVmcs->u32EntryCtls & VMX_ENTRY_CTLS_LOAD_PAT_MSR)
6451 pVCpu->cpum.GstCtx.msrPAT = pVmcs->u64GuestPatMsr.u;
6452
6453 /* EFER MSR. */
6454 if (pVmcs->u32EntryCtls & VMX_ENTRY_CTLS_LOAD_EFER_MSR)
6455 pVCpu->cpum.GstCtx.msrEFER = pVmcs->u64GuestEferMsr.u;
6456
6457 /* We don't support IA32_PERF_GLOBAL_CTRL MSR yet. */
6458 Assert(!(pVmcs->u32EntryCtls & VMX_ENTRY_CTLS_LOAD_PERF_MSR));
6459
6460 /* We don't support IA32_BNDCFGS MSR yet. */
6461 Assert(!(pVmcs->u32EntryCtls & VMX_ENTRY_CTLS_LOAD_BNDCFGS_MSR));
6462
6463 /* Nothing to do for SMBASE register - We don't support SMM yet. */
6464}
6465
6466
6467/**
6468 * Loads the guest segment registers, GDTR, IDTR, LDTR and TR as part of VM-entry.
6469 *
6470 * @param pVCpu The cross context virtual CPU structure.
6471 */
6472IEM_STATIC void iemVmxVmentryLoadGuestSegRegs(PVMCPUCC pVCpu)
6473{
6474 /*
6475 * Load guest segment registers, GDTR, IDTR, LDTR and TR.
6476 * See Intel spec. 26.3.2.2 "Loading Guest Segment Registers and Descriptor-Table Registers".
6477 */
6478 /* CS, SS, ES, DS, FS, GS. */
6479 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
6480 for (unsigned iSegReg = 0; iSegReg < X86_SREG_COUNT; iSegReg++)
6481 {
6482 PCPUMSELREG pGstSelReg = &pVCpu->cpum.GstCtx.aSRegs[iSegReg];
6483 CPUMSELREG VmcsSelReg;
6484 int rc = iemVmxVmcsGetGuestSegReg(pVmcs, iSegReg, &VmcsSelReg);
6485 AssertRC(rc); NOREF(rc);
6486 if (!(VmcsSelReg.Attr.u & X86DESCATTR_UNUSABLE))
6487 {
6488 pGstSelReg->Sel = VmcsSelReg.Sel;
6489 pGstSelReg->ValidSel = VmcsSelReg.Sel;
6490 pGstSelReg->fFlags = CPUMSELREG_FLAGS_VALID;
6491 pGstSelReg->u64Base = VmcsSelReg.u64Base;
6492 pGstSelReg->u32Limit = VmcsSelReg.u32Limit;
6493 pGstSelReg->Attr.u = VmcsSelReg.Attr.u;
6494 }
6495 else
6496 {
6497 pGstSelReg->Sel = VmcsSelReg.Sel;
6498 pGstSelReg->ValidSel = VmcsSelReg.Sel;
6499 pGstSelReg->fFlags = CPUMSELREG_FLAGS_VALID;
6500 switch (iSegReg)
6501 {
6502 case X86_SREG_CS:
6503 pGstSelReg->u64Base = VmcsSelReg.u64Base;
6504 pGstSelReg->u32Limit = VmcsSelReg.u32Limit;
6505 pGstSelReg->Attr.u = VmcsSelReg.Attr.u;
6506 break;
6507
6508 case X86_SREG_SS:
6509 pGstSelReg->u64Base = VmcsSelReg.u64Base & UINT32_C(0xfffffff0);
6510 pGstSelReg->u32Limit = 0;
6511 pGstSelReg->Attr.u = (VmcsSelReg.Attr.u & X86DESCATTR_DPL) | X86DESCATTR_D | X86DESCATTR_UNUSABLE;
6512 break;
6513
6514 case X86_SREG_ES:
6515 case X86_SREG_DS:
6516 pGstSelReg->u64Base = 0;
6517 pGstSelReg->u32Limit = 0;
6518 pGstSelReg->Attr.u = X86DESCATTR_UNUSABLE;
6519 break;
6520
6521 case X86_SREG_FS:
6522 case X86_SREG_GS:
6523 pGstSelReg->u64Base = VmcsSelReg.u64Base;
6524 pGstSelReg->u32Limit = 0;
6525 pGstSelReg->Attr.u = X86DESCATTR_UNUSABLE;
6526 break;
6527 }
6528 Assert(pGstSelReg->Attr.n.u1Unusable);
6529 }
6530 }
6531
6532 /* LDTR. */
6533 pVCpu->cpum.GstCtx.ldtr.Sel = pVmcs->GuestLdtr;
6534 pVCpu->cpum.GstCtx.ldtr.ValidSel = pVmcs->GuestLdtr;
6535 pVCpu->cpum.GstCtx.ldtr.fFlags = CPUMSELREG_FLAGS_VALID;
6536 if (!(pVmcs->u32GuestLdtrAttr & X86DESCATTR_UNUSABLE))
6537 {
6538 pVCpu->cpum.GstCtx.ldtr.u64Base = pVmcs->u64GuestLdtrBase.u;
6539 pVCpu->cpum.GstCtx.ldtr.u32Limit = pVmcs->u32GuestLdtrLimit;
6540 pVCpu->cpum.GstCtx.ldtr.Attr.u = pVmcs->u32GuestLdtrAttr;
6541 }
6542 else
6543 {
6544 pVCpu->cpum.GstCtx.ldtr.u64Base = 0;
6545 pVCpu->cpum.GstCtx.ldtr.u32Limit = 0;
6546 pVCpu->cpum.GstCtx.ldtr.Attr.u = X86DESCATTR_UNUSABLE;
6547 }
6548
6549 /* TR. */
6550 Assert(!(pVmcs->u32GuestTrAttr & X86DESCATTR_UNUSABLE));
6551 pVCpu->cpum.GstCtx.tr.Sel = pVmcs->GuestTr;
6552 pVCpu->cpum.GstCtx.tr.ValidSel = pVmcs->GuestTr;
6553 pVCpu->cpum.GstCtx.tr.fFlags = CPUMSELREG_FLAGS_VALID;
6554 pVCpu->cpum.GstCtx.tr.u64Base = pVmcs->u64GuestTrBase.u;
6555 pVCpu->cpum.GstCtx.tr.u32Limit = pVmcs->u32GuestTrLimit;
6556 pVCpu->cpum.GstCtx.tr.Attr.u = pVmcs->u32GuestTrAttr;
6557
6558 /* GDTR. */
6559 pVCpu->cpum.GstCtx.gdtr.cbGdt = pVmcs->u32GuestGdtrLimit;
6560 pVCpu->cpum.GstCtx.gdtr.pGdt = pVmcs->u64GuestGdtrBase.u;
6561
6562 /* IDTR. */
6563 pVCpu->cpum.GstCtx.idtr.cbIdt = pVmcs->u32GuestIdtrLimit;
6564 pVCpu->cpum.GstCtx.idtr.pIdt = pVmcs->u64GuestIdtrBase.u;
6565}
6566
6567
6568/**
6569 * Loads the guest MSRs from the VM-entry MSR-load area as part of VM-entry.
6570 *
6571 * @returns VBox status code.
6572 * @param pVCpu The cross context virtual CPU structure.
6573 * @param pszInstr The VMX instruction name (for logging purposes).
6574 */
6575IEM_STATIC int iemVmxVmentryLoadGuestAutoMsrs(PVMCPUCC pVCpu, const char *pszInstr)
6576{
6577 /*
6578 * Load guest MSRs.
6579 * See Intel spec. 26.4 "Loading MSRs".
6580 */
6581 PVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
6582 const char *const pszFailure = "VM-exit";
6583
6584 /*
6585 * The VM-entry MSR-load area address need not be a valid guest-physical address if the
6586 * VM-entry MSR load count is 0. If this is the case, bail early without reading it.
6587 * See Intel spec. 24.8.2 "VM-Entry Controls for MSRs".
6588 */
6589 uint32_t const cMsrs = RT_MIN(pVmcs->u32EntryMsrLoadCount, RT_ELEMENTS(pVCpu->cpum.GstCtx.hwvirt.vmx.aEntryMsrLoadArea));
6590 if (!cMsrs)
6591 return VINF_SUCCESS;
6592
6593 /*
6594 * Verify the MSR auto-load count. Physical CPUs can behave unpredictably if the count is
6595 * exceeded including possibly raising #MC exceptions during VMX transition. Our
6596 * implementation shall fail VM-entry with an VMX_EXIT_ERR_MSR_LOAD VM-exit.
6597 */
6598 bool const fIsMsrCountValid = iemVmxIsAutoMsrCountValid(pVCpu, cMsrs);
6599 if (fIsMsrCountValid)
6600 { /* likely */ }
6601 else
6602 {
6603 iemVmxVmcsSetExitQual(pVCpu, VMX_V_AUTOMSR_AREA_SIZE / sizeof(VMXAUTOMSR));
6604 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_MsrLoadCount);
6605 }
6606
6607 RTGCPHYS const GCPhysVmEntryMsrLoadArea = pVmcs->u64AddrEntryMsrLoad.u;
6608 int rc = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), &pVCpu->cpum.GstCtx.hwvirt.vmx.aEntryMsrLoadArea[0],
6609 GCPhysVmEntryMsrLoadArea, cMsrs * sizeof(VMXAUTOMSR));
6610 if (RT_SUCCESS(rc))
6611 {
6612 PCVMXAUTOMSR pMsr = &pVCpu->cpum.GstCtx.hwvirt.vmx.aEntryMsrLoadArea[0];
6613 for (uint32_t idxMsr = 0; idxMsr < cMsrs; idxMsr++, pMsr++)
6614 {
6615 if ( !pMsr->u32Reserved
6616 && pMsr->u32Msr != MSR_K8_FS_BASE
6617 && pMsr->u32Msr != MSR_K8_GS_BASE
6618 && pMsr->u32Msr != MSR_K6_EFER
6619 && pMsr->u32Msr != MSR_IA32_SMM_MONITOR_CTL
6620 && pMsr->u32Msr >> 8 != MSR_IA32_X2APIC_START >> 8)
6621 {
6622 VBOXSTRICTRC rcStrict = CPUMSetGuestMsr(pVCpu, pMsr->u32Msr, pMsr->u64Value);
6623 if (rcStrict == VINF_SUCCESS)
6624 continue;
6625
6626 /*
6627 * If we're in ring-0, we cannot handle returns to ring-3 at this point and continue VM-entry.
6628 * If any nested hypervisor loads MSRs that require ring-3 handling, we cause a VM-entry failure
6629 * recording the MSR index in the Exit qualification (as per the Intel spec.) and indicated
6630 * further by our own, specific diagnostic code. Later, we can try implement handling of the
6631 * MSR in ring-0 if possible, or come up with a better, generic solution.
6632 */
6633 iemVmxVmcsSetExitQual(pVCpu, idxMsr);
6634 VMXVDIAG const enmDiag = rcStrict == VINF_CPUM_R3_MSR_WRITE
6635 ? kVmxVDiag_Vmentry_MsrLoadRing3
6636 : kVmxVDiag_Vmentry_MsrLoad;
6637 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, enmDiag);
6638 }
6639 else
6640 {
6641 iemVmxVmcsSetExitQual(pVCpu, idxMsr);
6642 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_MsrLoadRsvd);
6643 }
6644 }
6645 }
6646 else
6647 {
6648 AssertMsgFailed(("%s: Failed to read MSR auto-load area at %#RGp, rc=%Rrc\n", pszInstr, GCPhysVmEntryMsrLoadArea, rc));
6649 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_MsrLoadPtrReadPhys);
6650 }
6651
6652 NOREF(pszInstr);
6653 NOREF(pszFailure);
6654 return VINF_SUCCESS;
6655}
6656
6657
6658/**
6659 * Loads the guest-state non-register state as part of VM-entry.
6660 *
6661 * @returns VBox status code.
6662 * @param pVCpu The cross context virtual CPU structure.
6663 * @param pszInstr The VMX instruction name (for logging purposes).
6664 *
6665 * @remarks This must be called only after loading the nested-guest register state
6666 * (especially nested-guest RIP).
6667 */
6668IEM_STATIC int iemVmxVmentryLoadGuestNonRegState(PVMCPUCC pVCpu, const char *pszInstr)
6669{
6670 /*
6671 * Load guest non-register state.
6672 * See Intel spec. 26.6 "Special Features of VM Entry"
6673 */
6674 const char *const pszFailure = "VM-exit";
6675 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
6676
6677 /*
6678 * If VM-entry is not vectoring, block-by-STI and block-by-MovSS state must be loaded.
6679 * If VM-entry is vectoring, there is no block-by-STI or block-by-MovSS.
6680 *
6681 * See Intel spec. 26.6.1 "Interruptibility State".
6682 */
6683 bool const fEntryVectoring = VMXIsVmentryVectoring(pVmcs->u32EntryIntInfo, NULL /* puEntryIntInfoType */);
6684 if ( !fEntryVectoring
6685 && (pVmcs->u32GuestIntrState & (VMX_VMCS_GUEST_INT_STATE_BLOCK_STI | VMX_VMCS_GUEST_INT_STATE_BLOCK_MOVSS)))
6686 EMSetInhibitInterruptsPC(pVCpu, pVmcs->u64GuestRip.u);
6687 else if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS))
6688 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS);
6689
6690 /* NMI blocking. */
6691 if (pVmcs->u32GuestIntrState & VMX_VMCS_GUEST_INT_STATE_BLOCK_NMI)
6692 {
6693 if (pVmcs->u32PinCtls & VMX_PIN_CTLS_VIRT_NMI)
6694 pVCpu->cpum.GstCtx.hwvirt.vmx.fVirtNmiBlocking = true;
6695 else
6696 {
6697 pVCpu->cpum.GstCtx.hwvirt.vmx.fVirtNmiBlocking = false;
6698 if (!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_BLOCK_NMIS))
6699 VMCPU_FF_SET(pVCpu, VMCPU_FF_BLOCK_NMIS);
6700 }
6701 }
6702 else
6703 pVCpu->cpum.GstCtx.hwvirt.vmx.fVirtNmiBlocking = false;
6704
6705 /* SMI blocking is irrelevant. We don't support SMIs yet. */
6706
6707 /*
6708 * Set PGM's copy of the EPT pointer.
6709 * The EPTP has already been validated while checking guest state.
6710 *
6711 * It is important to do this prior to mapping PAE PDPTEs (below).
6712 */
6713 if (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_EPT)
6714 PGMSetGuestEptPtr(pVCpu, pVmcs->u64EptPtr.u);
6715
6716 /*
6717 * Load the guest's PAE PDPTEs.
6718 */
6719 if (iemVmxVmcsIsGuestPaePagingEnabled(pVmcs))
6720 {
6721 if (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_EPT)
6722 {
6723 /*
6724 * With EPT, we've already validated these while checking the guest state.
6725 * Just load them directly from the VMCS here.
6726 */
6727 X86PDPE aPaePdptes[X86_PG_PAE_PDPE_ENTRIES];
6728 aPaePdptes[0].u = pVmcs->u64GuestPdpte0.u;
6729 aPaePdptes[1].u = pVmcs->u64GuestPdpte1.u;
6730 aPaePdptes[2].u = pVmcs->u64GuestPdpte2.u;
6731 aPaePdptes[3].u = pVmcs->u64GuestPdpte3.u;
6732 AssertCompile(RT_ELEMENTS(aPaePdptes) == RT_ELEMENTS(pVCpu->cpum.GstCtx.aPaePdpes));
6733 for (unsigned i = 0; i < RT_ELEMENTS(pVCpu->cpum.GstCtx.aPaePdpes); i++)
6734 pVCpu->cpum.GstCtx.aPaePdpes[i].u = aPaePdptes[i].u;
6735 }
6736 else
6737 {
6738 /*
6739 * Without EPT, we must load the PAE PDPTEs referenced by CR3.
6740 * This involves loading (and mapping) CR3 and validating them now.
6741 */
6742 int const rc = PGMGstMapPaePdpesAtCr3(pVCpu, pVmcs->u64GuestCr3.u);
6743 if (RT_SUCCESS(rc))
6744 { /* likely */ }
6745 else
6746 {
6747 iemVmxVmcsSetExitQual(pVCpu, VMX_ENTRY_FAIL_QUAL_PDPTE);
6748 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_GuestPdpte);
6749 }
6750 }
6751 }
6752
6753 /* VPID is irrelevant. We don't support VPID yet. */
6754
6755 /* Clear address-range monitoring. */
6756 EMMonitorWaitClear(pVCpu);
6757
6758 return VINF_SUCCESS;
6759}
6760
6761
6762/**
6763 * Loads the guest VMCS referenced state (such as MSR bitmaps, I/O bitmaps etc).
6764 *
6765 * @param pVCpu The cross context virtual CPU structure.
6766 * @param pszInstr The VMX instruction name (for logging purposes).
6767 *
6768 * @remarks This assumes various VMCS related data structure pointers have already
6769 * been verified prior to calling this function.
6770 */
6771IEM_STATIC int iemVmxVmentryLoadGuestVmcsRefState(PVMCPUCC pVCpu, const char *pszInstr)
6772{
6773 const char *const pszFailure = "VM-exit";
6774 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
6775
6776 /*
6777 * Virtualize APIC accesses.
6778 */
6779 if (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_APIC_ACCESS)
6780 {
6781 /* APIC-access physical address. */
6782 RTGCPHYS const GCPhysApicAccess = pVmcs->u64AddrApicAccess.u;
6783
6784 /*
6785 * Register the handler for the APIC-access page.
6786 *
6787 * We don't deregister the APIC-access page handler during the VM-exit as a different
6788 * nested-VCPU might be using the same guest-physical address for its APIC-access page.
6789 *
6790 * We leave the page registered until the first access that happens outside VMX non-root
6791 * mode. Guest software is allowed to access structures such as the APIC-access page
6792 * only when no logical processor with a current VMCS references it in VMX non-root mode,
6793 * otherwise it can lead to unpredictable behavior including guest triple-faults.
6794 *
6795 * See Intel spec. 24.11.4 "Software Access to Related Structures".
6796 */
6797 if (!PGMHandlerPhysicalIsRegistered(pVCpu->CTX_SUFF(pVM), GCPhysApicAccess))
6798 {
6799 PVMCC pVM = pVCpu->CTX_SUFF(pVM);
6800 PVMCPUCC pVCpu0 = VMCC_GET_CPU_0(pVM);
6801 int rc = PGMHandlerPhysicalRegister(pVM, GCPhysApicAccess, GCPhysApicAccess + X86_PAGE_4K_SIZE - 1,
6802 pVCpu0->iem.s.hVmxApicAccessPage, NIL_RTR3PTR /* pvUserR3 */,
6803 NIL_RTR0PTR /* pvUserR0 */, NIL_RTRCPTR /* pvUserRC */, NULL /* pszDesc */);
6804 if (RT_SUCCESS(rc))
6805 { /* likely */ }
6806 else
6807 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_AddrApicAccessHandlerReg);
6808 }
6809 }
6810
6811 /*
6812 * VMCS shadowing.
6813 */
6814 if (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VMCS_SHADOWING)
6815 {
6816 /* Read the VMREAD-bitmap. */
6817 RTGCPHYS const GCPhysVmreadBitmap = pVmcs->u64AddrVmreadBitmap.u;
6818 int rc = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), &pVCpu->cpum.GstCtx.hwvirt.vmx.abVmreadBitmap[0],
6819 GCPhysVmreadBitmap, sizeof(pVCpu->cpum.GstCtx.hwvirt.vmx.abVmreadBitmap));
6820 if (RT_SUCCESS(rc))
6821 { /* likely */ }
6822 else
6823 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_VmreadBitmapPtrReadPhys);
6824
6825 /* Read the VMWRITE-bitmap. */
6826 RTGCPHYS const GCPhysVmwriteBitmap = pVmcs->u64AddrVmwriteBitmap.u;
6827 rc = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), &pVCpu->cpum.GstCtx.hwvirt.vmx.abVmwriteBitmap[0],
6828 GCPhysVmwriteBitmap, sizeof(pVCpu->cpum.GstCtx.hwvirt.vmx.abVmwriteBitmap));
6829 if (RT_SUCCESS(rc))
6830 { /* likely */ }
6831 else
6832 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_VmwriteBitmapPtrReadPhys);
6833 }
6834
6835 /*
6836 * I/O bitmaps.
6837 */
6838 if (pVmcs->u32ProcCtls & VMX_PROC_CTLS_USE_IO_BITMAPS)
6839 {
6840 /* Read the IO bitmap A. */
6841 RTGCPHYS const GCPhysIoBitmapA = pVmcs->u64AddrIoBitmapA.u;
6842 int rc = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), &pVCpu->cpum.GstCtx.hwvirt.vmx.abIoBitmap[0],
6843 GCPhysIoBitmapA, VMX_V_IO_BITMAP_A_SIZE);
6844 if (RT_SUCCESS(rc))
6845 { /* likely */ }
6846 else
6847 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_IoBitmapAPtrReadPhys);
6848
6849 /* Read the IO bitmap B. */
6850 RTGCPHYS const GCPhysIoBitmapB = pVmcs->u64AddrIoBitmapB.u;
6851 rc = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), &pVCpu->cpum.GstCtx.hwvirt.vmx.abIoBitmap[VMX_V_IO_BITMAP_A_SIZE],
6852 GCPhysIoBitmapB, VMX_V_IO_BITMAP_B_SIZE);
6853 if (RT_SUCCESS(rc))
6854 { /* likely */ }
6855 else
6856 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_IoBitmapBPtrReadPhys);
6857 }
6858
6859 /*
6860 * TPR shadow and Virtual-APIC page.
6861 */
6862 if (pVmcs->u32ProcCtls & VMX_PROC_CTLS_USE_TPR_SHADOW)
6863 {
6864 /* Verify TPR threshold and VTPR when both virtualize-APIC accesses and virtual-interrupt delivery aren't used. */
6865 if ( !(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_APIC_ACCESS)
6866 && !(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_INT_DELIVERY))
6867 {
6868 /* Read the VTPR from the virtual-APIC page. */
6869 RTGCPHYS const GCPhysVirtApic = pVmcs->u64AddrVirtApic.u;
6870 uint8_t u8VTpr;
6871 int rc = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), &u8VTpr, GCPhysVirtApic + XAPIC_OFF_TPR, sizeof(u8VTpr));
6872 if (RT_SUCCESS(rc))
6873 { /* likely */ }
6874 else
6875 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_VirtApicPagePtrReadPhys);
6876
6877 /* Bits 3:0 of the TPR-threshold must not be greater than bits 7:4 of VTPR. */
6878 if ((uint8_t)RT_BF_GET(pVmcs->u32TprThreshold, VMX_BF_TPR_THRESHOLD_TPR) <= (u8VTpr & 0xf0))
6879 { /* likely */ }
6880 else
6881 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_TprThresholdVTpr);
6882 }
6883 }
6884
6885 /*
6886 * VMCS link pointer.
6887 */
6888 if (pVmcs->u64VmcsLinkPtr.u != UINT64_C(0xffffffffffffffff))
6889 {
6890 /* Read the VMCS-link pointer from guest memory. */
6891 RTGCPHYS const GCPhysShadowVmcs = pVmcs->u64VmcsLinkPtr.u;
6892 int rc = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), &pVCpu->cpum.GstCtx.hwvirt.vmx.ShadowVmcs,
6893 GCPhysShadowVmcs, sizeof(pVCpu->cpum.GstCtx.hwvirt.vmx.ShadowVmcs));
6894 if (RT_SUCCESS(rc))
6895 { /* likely */ }
6896 else
6897 {
6898 iemVmxVmcsSetExitQual(pVCpu, VMX_ENTRY_FAIL_QUAL_VMCS_LINK_PTR);
6899 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_VmcsLinkPtrReadPhys);
6900 }
6901
6902 /* Verify the VMCS revision specified by the guest matches what we reported to the guest. */
6903 if (pVCpu->cpum.GstCtx.hwvirt.vmx.ShadowVmcs.u32VmcsRevId.n.u31RevisionId == VMX_V_VMCS_REVISION_ID)
6904 { /* likely */ }
6905 else
6906 {
6907 iemVmxVmcsSetExitQual(pVCpu, VMX_ENTRY_FAIL_QUAL_VMCS_LINK_PTR);
6908 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_VmcsLinkPtrRevId);
6909 }
6910
6911 /* Verify the shadow bit is set if VMCS shadowing is enabled . */
6912 if ( !(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VMCS_SHADOWING)
6913 || pVCpu->cpum.GstCtx.hwvirt.vmx.ShadowVmcs.u32VmcsRevId.n.fIsShadowVmcs)
6914 { /* likely */ }
6915 else
6916 {
6917 iemVmxVmcsSetExitQual(pVCpu, VMX_ENTRY_FAIL_QUAL_VMCS_LINK_PTR);
6918 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_VmcsLinkPtrShadow);
6919 }
6920
6921 /* Update our cache of the guest physical address of the shadow VMCS. */
6922 pVCpu->cpum.GstCtx.hwvirt.vmx.GCPhysShadowVmcs = GCPhysShadowVmcs;
6923 }
6924
6925 /*
6926 * MSR bitmap.
6927 */
6928 if (pVmcs->u32ProcCtls & VMX_PROC_CTLS_USE_MSR_BITMAPS)
6929 {
6930 /* Read the MSR bitmap. */
6931 RTGCPHYS const GCPhysMsrBitmap = pVmcs->u64AddrMsrBitmap.u;
6932 int rc = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), &pVCpu->cpum.GstCtx.hwvirt.vmx.abMsrBitmap[0],
6933 GCPhysMsrBitmap, sizeof(pVCpu->cpum.GstCtx.hwvirt.vmx.abMsrBitmap));
6934 if (RT_SUCCESS(rc))
6935 { /* likely */ }
6936 else
6937 IEM_VMX_VMENTRY_FAILED_RET(pVCpu, pszInstr, pszFailure, kVmxVDiag_Vmentry_MsrBitmapPtrReadPhys);
6938 }
6939
6940 NOREF(pszFailure);
6941 NOREF(pszInstr);
6942 return VINF_SUCCESS;
6943}
6944
6945
6946/**
6947 * Loads the guest-state as part of VM-entry.
6948 *
6949 * @returns VBox status code.
6950 * @param pVCpu The cross context virtual CPU structure.
6951 * @param pszInstr The VMX instruction name (for logging purposes).
6952 *
6953 * @remarks This must be done after all the necessary steps prior to loading of
6954 * guest-state (e.g. checking various VMCS state).
6955 */
6956IEM_STATIC int iemVmxVmentryLoadGuestState(PVMCPUCC pVCpu, const char *pszInstr)
6957{
6958 /* Load guest control registers, MSRs (that are directly part of the VMCS). */
6959 iemVmxVmentryLoadGuestControlRegsMsrs(pVCpu);
6960
6961 /* Load guest segment registers. */
6962 iemVmxVmentryLoadGuestSegRegs(pVCpu);
6963
6964 /*
6965 * Load guest RIP, RSP and RFLAGS.
6966 * See Intel spec. 26.3.2.3 "Loading Guest RIP, RSP and RFLAGS".
6967 */
6968 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
6969 pVCpu->cpum.GstCtx.rsp = pVmcs->u64GuestRsp.u;
6970 pVCpu->cpum.GstCtx.rip = pVmcs->u64GuestRip.u;
6971 pVCpu->cpum.GstCtx.rflags.u = pVmcs->u64GuestRFlags.u;
6972
6973 /* Initialize the PAUSE-loop controls as part of VM-entry. */
6974 pVCpu->cpum.GstCtx.hwvirt.vmx.uFirstPauseLoopTick = 0;
6975 pVCpu->cpum.GstCtx.hwvirt.vmx.uPrevPauseTick = 0;
6976
6977 /* Load guest non-register state (such as interrupt shadows, NMI blocking etc). */
6978 int rc = iemVmxVmentryLoadGuestNonRegState(pVCpu, pszInstr);
6979 if (rc == VINF_SUCCESS)
6980 { /* likely */ }
6981 else
6982 return rc;
6983
6984 /* Load VMX related structures and state referenced by the VMCS. */
6985 rc = iemVmxVmentryLoadGuestVmcsRefState(pVCpu, pszInstr);
6986 if (rc == VINF_SUCCESS)
6987 { /* likely */ }
6988 else
6989 return rc;
6990
6991 NOREF(pszInstr);
6992 return VINF_SUCCESS;
6993}
6994
6995
6996/**
6997 * Returns whether there are is a pending debug exception on VM-entry.
6998 *
6999 * @param pVCpu The cross context virtual CPU structure.
7000 * @param pszInstr The VMX instruction name (for logging purposes).
7001 */
7002IEM_STATIC bool iemVmxVmentryIsPendingDebugXcpt(PVMCPUCC pVCpu, const char *pszInstr)
7003{
7004 /*
7005 * Pending debug exceptions.
7006 * See Intel spec. 26.6.3 "Delivery of Pending Debug Exceptions after VM Entry".
7007 */
7008 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
7009 Assert(pVmcs);
7010
7011 bool fPendingDbgXcpt = RT_BOOL(pVmcs->u64GuestPendingDbgXcpts.u & ( VMX_VMCS_GUEST_PENDING_DEBUG_XCPT_BS
7012 | VMX_VMCS_GUEST_PENDING_DEBUG_XCPT_EN_BP));
7013 if (fPendingDbgXcpt)
7014 {
7015 uint8_t uEntryIntInfoType;
7016 bool const fEntryVectoring = VMXIsVmentryVectoring(pVmcs->u32EntryIntInfo, &uEntryIntInfoType);
7017 if (fEntryVectoring)
7018 {
7019 switch (uEntryIntInfoType)
7020 {
7021 case VMX_ENTRY_INT_INFO_TYPE_EXT_INT:
7022 case VMX_ENTRY_INT_INFO_TYPE_NMI:
7023 case VMX_ENTRY_INT_INFO_TYPE_HW_XCPT:
7024 case VMX_ENTRY_INT_INFO_TYPE_PRIV_SW_XCPT:
7025 fPendingDbgXcpt = false;
7026 break;
7027
7028 case VMX_ENTRY_INT_INFO_TYPE_SW_XCPT:
7029 {
7030 /*
7031 * Whether the pending debug exception for software exceptions other than
7032 * #BP and #OF is delivered after injecting the exception or is discard
7033 * is CPU implementation specific. We will discard them (easier).
7034 */
7035 uint8_t const uVector = VMX_ENTRY_INT_INFO_VECTOR(pVmcs->u32EntryIntInfo);
7036 if ( uVector != X86_XCPT_BP
7037 && uVector != X86_XCPT_OF)
7038 fPendingDbgXcpt = false;
7039 RT_FALL_THRU();
7040 }
7041 case VMX_ENTRY_INT_INFO_TYPE_SW_INT:
7042 {
7043 if (!(pVmcs->u32GuestIntrState & VMX_VMCS_GUEST_INT_STATE_BLOCK_MOVSS))
7044 fPendingDbgXcpt = false;
7045 break;
7046 }
7047 }
7048 }
7049 else
7050 {
7051 /*
7052 * When the VM-entry is not vectoring but there is blocking-by-MovSS, whether the
7053 * pending debug exception is held pending or is discarded is CPU implementation
7054 * specific. We will discard them (easier).
7055 */
7056 if (pVmcs->u32GuestIntrState & VMX_VMCS_GUEST_INT_STATE_BLOCK_MOVSS)
7057 fPendingDbgXcpt = false;
7058
7059 /* There's no pending debug exception in the shutdown or wait-for-SIPI state. */
7060 if (pVmcs->u32GuestActivityState & (VMX_VMCS_GUEST_ACTIVITY_SHUTDOWN | VMX_VMCS_GUEST_ACTIVITY_SIPI_WAIT))
7061 fPendingDbgXcpt = false;
7062 }
7063 }
7064
7065 NOREF(pszInstr);
7066 return fPendingDbgXcpt;
7067}
7068
7069
7070/**
7071 * Set up the monitor-trap flag (MTF).
7072 *
7073 * @param pVCpu The cross context virtual CPU structure.
7074 * @param pszInstr The VMX instruction name (for logging purposes).
7075 */
7076IEM_STATIC void iemVmxVmentrySetupMtf(PVMCPUCC pVCpu, const char *pszInstr)
7077{
7078 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
7079 Assert(pVmcs);
7080 if (pVmcs->u32ProcCtls & VMX_PROC_CTLS_MONITOR_TRAP_FLAG)
7081 {
7082 VMCPU_FF_SET(pVCpu, VMCPU_FF_VMX_MTF);
7083 Log(("%s: Monitor-trap flag set on VM-entry\n", pszInstr));
7084 }
7085 else
7086 Assert(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_VMX_MTF));
7087 NOREF(pszInstr);
7088}
7089
7090
7091/**
7092 * Sets up NMI-window exiting.
7093 *
7094 * @param pVCpu The cross context virtual CPU structure.
7095 * @param pszInstr The VMX instruction name (for logging purposes).
7096 */
7097IEM_STATIC void iemVmxVmentrySetupNmiWindow(PVMCPUCC pVCpu, const char *pszInstr)
7098{
7099 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
7100 Assert(pVmcs);
7101 if (pVmcs->u32ProcCtls & VMX_PROC_CTLS_NMI_WINDOW_EXIT)
7102 {
7103 Assert(pVmcs->u32PinCtls & VMX_PIN_CTLS_VIRT_NMI);
7104 VMCPU_FF_SET(pVCpu, VMCPU_FF_VMX_NMI_WINDOW);
7105 Log(("%s: NMI-window set on VM-entry\n", pszInstr));
7106 }
7107 else
7108 Assert(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_VMX_NMI_WINDOW));
7109 NOREF(pszInstr);
7110}
7111
7112
7113/**
7114 * Sets up interrupt-window exiting.
7115 *
7116 * @param pVCpu The cross context virtual CPU structure.
7117 * @param pszInstr The VMX instruction name (for logging purposes).
7118 */
7119IEM_STATIC void iemVmxVmentrySetupIntWindow(PVMCPUCC pVCpu, const char *pszInstr)
7120{
7121 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
7122 Assert(pVmcs);
7123 if (pVmcs->u32ProcCtls & VMX_PROC_CTLS_INT_WINDOW_EXIT)
7124 {
7125 VMCPU_FF_SET(pVCpu, VMCPU_FF_VMX_INT_WINDOW);
7126 Log(("%s: Interrupt-window set on VM-entry\n", pszInstr));
7127 }
7128 else
7129 Assert(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_VMX_INT_WINDOW));
7130 NOREF(pszInstr);
7131}
7132
7133
7134/**
7135 * Set up the VMX-preemption timer.
7136 *
7137 * @param pVCpu The cross context virtual CPU structure.
7138 * @param pszInstr The VMX instruction name (for logging purposes).
7139 */
7140IEM_STATIC void iemVmxVmentrySetupPreemptTimer(PVMCPUCC pVCpu, const char *pszInstr)
7141{
7142 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
7143 Assert(pVmcs);
7144 if (pVmcs->u32PinCtls & VMX_PIN_CTLS_PREEMPT_TIMER)
7145 {
7146 /*
7147 * If the timer is 0, we must cause a VM-exit before executing the first
7148 * nested-guest instruction. So we can flag as though the timer has already
7149 * expired and we will check and cause a VM-exit at the right priority elsewhere
7150 * in the code.
7151 */
7152 uint64_t uEntryTick;
7153 uint32_t const uPreemptTimer = pVmcs->u32PreemptTimer;
7154 if (uPreemptTimer)
7155 {
7156 int rc = CPUMStartGuestVmxPremptTimer(pVCpu, uPreemptTimer, VMX_V_PREEMPT_TIMER_SHIFT, &uEntryTick);
7157 AssertRC(rc);
7158 Log(("%s: VM-entry set up VMX-preemption timer at %#RX64\n", pszInstr, uEntryTick));
7159 }
7160 else
7161 {
7162 uEntryTick = TMCpuTickGetNoCheck(pVCpu);
7163 VMCPU_FF_SET(pVCpu, VMCPU_FF_VMX_PREEMPT_TIMER);
7164 Log(("%s: VM-entry set up VMX-preemption timer at %#RX64 to expire immediately!\n", pszInstr, uEntryTick));
7165 }
7166
7167 pVCpu->cpum.GstCtx.hwvirt.vmx.uEntryTick = uEntryTick;
7168 }
7169 else
7170 Assert(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_VMX_PREEMPT_TIMER));
7171
7172 NOREF(pszInstr);
7173}
7174
7175
7176/**
7177 * Injects an event using TRPM given a VM-entry interruption info. and related
7178 * fields.
7179 *
7180 * @param pVCpu The cross context virtual CPU structure.
7181 * @param pszInstr The VMX instruction name (for logging purposes).
7182 * @param uEntryIntInfo The VM-entry interruption info.
7183 * @param uErrCode The error code associated with the event if any.
7184 * @param cbInstr The VM-entry instruction length (for software
7185 * interrupts and software exceptions). Pass 0
7186 * otherwise.
7187 * @param GCPtrFaultAddress The guest CR2 if this is a \#PF event.
7188 */
7189IEM_STATIC void iemVmxVmentryInjectTrpmEvent(PVMCPUCC pVCpu, const char *pszInstr, uint32_t uEntryIntInfo, uint32_t uErrCode,
7190 uint32_t cbInstr, RTGCUINTPTR GCPtrFaultAddress)
7191{
7192 Assert(VMX_ENTRY_INT_INFO_IS_VALID(uEntryIntInfo));
7193
7194 uint8_t const uType = VMX_ENTRY_INT_INFO_TYPE(uEntryIntInfo);
7195 uint8_t const uVector = VMX_ENTRY_INT_INFO_VECTOR(uEntryIntInfo);
7196 TRPMEVENT const enmTrpmEvent = HMVmxEventTypeToTrpmEventType(uEntryIntInfo);
7197
7198 Assert(uType != VMX_ENTRY_INT_INFO_TYPE_OTHER_EVENT);
7199
7200 int rc = TRPMAssertTrap(pVCpu, uVector, enmTrpmEvent);
7201 AssertRC(rc);
7202 Log(("%s: Injecting: vector=%#x type=%#x (%s)\n", pszInstr, uVector, uType, VMXGetEntryIntInfoTypeDesc(uType)));
7203
7204 if (VMX_ENTRY_INT_INFO_IS_ERROR_CODE_VALID(uEntryIntInfo))
7205 {
7206 TRPMSetErrorCode(pVCpu, uErrCode);
7207 Log(("%s: Injecting: err_code=%#x\n", pszInstr, uErrCode));
7208 }
7209
7210 if (VMX_ENTRY_INT_INFO_IS_XCPT_PF(uEntryIntInfo))
7211 {
7212 TRPMSetFaultAddress(pVCpu, GCPtrFaultAddress);
7213 Log(("%s: Injecting: fault_addr=%RGp\n", pszInstr, GCPtrFaultAddress));
7214 }
7215 else
7216 {
7217 if ( uType == VMX_ENTRY_INT_INFO_TYPE_SW_INT
7218 || uType == VMX_ENTRY_INT_INFO_TYPE_SW_XCPT
7219 || uType == VMX_ENTRY_INT_INFO_TYPE_PRIV_SW_XCPT)
7220 {
7221 TRPMSetInstrLength(pVCpu, cbInstr);
7222 Log(("%s: Injecting: instr_len=%u\n", pszInstr, cbInstr));
7223 }
7224 }
7225
7226 if (VMX_ENTRY_INT_INFO_TYPE(uEntryIntInfo) == VMX_ENTRY_INT_INFO_TYPE_PRIV_SW_XCPT)
7227 {
7228 TRPMSetTrapDueToIcebp(pVCpu);
7229 Log(("%s: Injecting: icebp\n", pszInstr));
7230 }
7231
7232 NOREF(pszInstr);
7233}
7234
7235
7236/**
7237 * Performs event injection (if any) as part of VM-entry.
7238 *
7239 * @param pVCpu The cross context virtual CPU structure.
7240 * @param pszInstr The VMX instruction name (for logging purposes).
7241 */
7242IEM_STATIC void iemVmxVmentryInjectEvent(PVMCPUCC pVCpu, const char *pszInstr)
7243{
7244 PVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
7245
7246 /*
7247 * Inject events.
7248 * The event that is going to be made pending for injection is not subject to VMX intercepts,
7249 * thus we flag ignoring of intercepts. However, recursive exceptions if any during delivery
7250 * of the current event -are- subject to intercepts, hence this flag will be flipped during
7251 * the actually delivery of this event.
7252 *
7253 * See Intel spec. 26.5 "Event Injection".
7254 */
7255 uint32_t const uEntryIntInfo = pVmcs->u32EntryIntInfo;
7256 bool const fEntryIntInfoValid = VMX_ENTRY_INT_INFO_IS_VALID(uEntryIntInfo);
7257
7258 CPUMSetGuestVmxInterceptEvents(&pVCpu->cpum.GstCtx, !fEntryIntInfoValid);
7259 if (fEntryIntInfoValid)
7260 {
7261 if (VMX_ENTRY_INT_INFO_TYPE(uEntryIntInfo) == VMX_ENTRY_INT_INFO_TYPE_OTHER_EVENT)
7262 {
7263 Assert(VMX_ENTRY_INT_INFO_VECTOR(uEntryIntInfo) == VMX_ENTRY_INT_INFO_VECTOR_MTF);
7264 VMCPU_FF_SET(pVCpu, VMCPU_FF_VMX_MTF);
7265 }
7266 else
7267 iemVmxVmentryInjectTrpmEvent(pVCpu, pszInstr, uEntryIntInfo, pVmcs->u32EntryXcptErrCode, pVmcs->u32EntryInstrLen,
7268 pVCpu->cpum.GstCtx.cr2);
7269
7270 /*
7271 * We need to clear the VM-entry interruption information field's valid bit on VM-exit.
7272 *
7273 * However, we do it here on VM-entry as well because while it isn't visible to guest
7274 * software until VM-exit, when and if HM looks at the VMCS to continue nested-guest
7275 * execution using hardware-assisted VMX, it will not be try to inject the event again.
7276 *
7277 * See Intel spec. 24.8.3 "VM-Entry Controls for Event Injection".
7278 */
7279 pVmcs->u32EntryIntInfo &= ~VMX_ENTRY_INT_INFO_VALID;
7280 }
7281 else
7282 {
7283 /*
7284 * Inject any pending guest debug exception.
7285 * Unlike injecting events, this #DB injection on VM-entry is subject to #DB VMX intercept.
7286 * See Intel spec. 26.6.3 "Delivery of Pending Debug Exceptions after VM Entry".
7287 */
7288 bool const fPendingDbgXcpt = iemVmxVmentryIsPendingDebugXcpt(pVCpu, pszInstr);
7289 if (fPendingDbgXcpt)
7290 {
7291 uint32_t const uDbgXcptInfo = RT_BF_MAKE(VMX_BF_ENTRY_INT_INFO_VECTOR, X86_XCPT_DB)
7292 | RT_BF_MAKE(VMX_BF_ENTRY_INT_INFO_TYPE, VMX_ENTRY_INT_INFO_TYPE_HW_XCPT)
7293 | RT_BF_MAKE(VMX_BF_ENTRY_INT_INFO_VALID, 1);
7294 iemVmxVmentryInjectTrpmEvent(pVCpu, pszInstr, uDbgXcptInfo, 0 /* uErrCode */, pVmcs->u32EntryInstrLen,
7295 0 /* GCPtrFaultAddress */);
7296 }
7297 }
7298
7299 NOREF(pszInstr);
7300}
7301
7302
7303/**
7304 * Initializes all read-only VMCS fields as part of VM-entry.
7305 *
7306 * @param pVCpu The cross context virtual CPU structure.
7307 */
7308IEM_STATIC void iemVmxVmentryInitReadOnlyFields(PVMCPUCC pVCpu)
7309{
7310 /*
7311 * Any VMCS field which we do not establish on every VM-exit but may potentially
7312 * be used on the VM-exit path of a nested hypervisor -and- is not explicitly
7313 * specified to be undefined, needs to be initialized here.
7314 *
7315 * Thus, it is especially important to clear the Exit qualification field
7316 * since it must be zero for VM-exits where it is not used. Similarly, the
7317 * VM-exit interruption information field's valid bit needs to be cleared for
7318 * the same reasons.
7319 */
7320 PVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
7321 Assert(pVmcs);
7322
7323 /* 16-bit (none currently). */
7324 /* 32-bit. */
7325 pVmcs->u32RoVmInstrError = 0;
7326 pVmcs->u32RoExitReason = 0;
7327 pVmcs->u32RoExitIntInfo = 0;
7328 pVmcs->u32RoExitIntErrCode = 0;
7329 pVmcs->u32RoIdtVectoringInfo = 0;
7330 pVmcs->u32RoIdtVectoringErrCode = 0;
7331 pVmcs->u32RoExitInstrLen = 0;
7332 pVmcs->u32RoExitInstrInfo = 0;
7333
7334 /* 64-bit. */
7335 pVmcs->u64RoGuestPhysAddr.u = 0;
7336
7337 /* Natural-width. */
7338 pVmcs->u64RoExitQual.u = 0;
7339 pVmcs->u64RoIoRcx.u = 0;
7340 pVmcs->u64RoIoRsi.u = 0;
7341 pVmcs->u64RoIoRdi.u = 0;
7342 pVmcs->u64RoIoRip.u = 0;
7343 pVmcs->u64RoGuestLinearAddr.u = 0;
7344}
7345
7346
7347/**
7348 * VMLAUNCH/VMRESUME instruction execution worker.
7349 *
7350 * @returns Strict VBox status code.
7351 * @param pVCpu The cross context virtual CPU structure.
7352 * @param cbInstr The instruction length in bytes.
7353 * @param uInstrId The instruction identity (VMXINSTRID_VMLAUNCH or
7354 * VMXINSTRID_VMRESUME).
7355 *
7356 * @remarks Common VMX instruction checks are already expected to by the caller,
7357 * i.e. CR4.VMXE, Real/V86 mode, EFER/CS.L checks.
7358 */
7359IEM_STATIC VBOXSTRICTRC iemVmxVmlaunchVmresume(PVMCPUCC pVCpu, uint8_t cbInstr, VMXINSTRID uInstrId)
7360{
7361# if defined(VBOX_WITH_NESTED_HWVIRT_ONLY_IN_IEM) && !defined(IN_RING3)
7362 RT_NOREF3(pVCpu, cbInstr, uInstrId);
7363 return VINF_EM_RAW_EMULATE_INSTR;
7364# else
7365 Assert( uInstrId == VMXINSTRID_VMLAUNCH
7366 || uInstrId == VMXINSTRID_VMRESUME);
7367 const char *pszInstr = uInstrId == VMXINSTRID_VMRESUME ? "vmresume" : "vmlaunch";
7368
7369 /* Nested-guest intercept. */
7370 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
7371 return iemVmxVmexitInstr(pVCpu, uInstrId == VMXINSTRID_VMRESUME ? VMX_EXIT_VMRESUME : VMX_EXIT_VMLAUNCH, cbInstr);
7372
7373 Assert(IEM_VMX_IS_ROOT_MODE(pVCpu));
7374
7375 /*
7376 * Basic VM-entry checks.
7377 * The order of the CPL, current and shadow VMCS and block-by-MovSS are important.
7378 * The checks following that do not have to follow a specific order.
7379 *
7380 * See Intel spec. 26.1 "Basic VM-entry Checks".
7381 */
7382
7383 /* CPL. */
7384 if (pVCpu->iem.s.uCpl == 0)
7385 { /* likely */ }
7386 else
7387 {
7388 Log(("%s: CPL %u -> #GP(0)\n", pszInstr, pVCpu->iem.s.uCpl));
7389 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmentry_Cpl;
7390 return iemRaiseGeneralProtectionFault0(pVCpu);
7391 }
7392
7393 /* Current VMCS valid. */
7394 if (IEM_VMX_HAS_CURRENT_VMCS(pVCpu))
7395 { /* likely */ }
7396 else
7397 {
7398 Log(("%s: VMCS pointer %#RGp invalid -> VMFailInvalid\n", pszInstr, IEM_VMX_GET_CURRENT_VMCS(pVCpu)));
7399 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmentry_PtrInvalid;
7400 iemVmxVmFailInvalid(pVCpu);
7401 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7402 return VINF_SUCCESS;
7403 }
7404
7405 /* Current VMCS is not a shadow VMCS. */
7406 if (!pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.u32VmcsRevId.n.fIsShadowVmcs)
7407 { /* likely */ }
7408 else
7409 {
7410 Log(("%s: VMCS pointer %#RGp is a shadow VMCS -> VMFailInvalid\n", pszInstr, IEM_VMX_GET_CURRENT_VMCS(pVCpu)));
7411 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmentry_PtrShadowVmcs;
7412 iemVmxVmFailInvalid(pVCpu);
7413 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7414 return VINF_SUCCESS;
7415 }
7416
7417 /** @todo Distinguish block-by-MovSS from block-by-STI. Currently we
7418 * use block-by-STI here which is not quite correct. */
7419 if ( !VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS)
7420 || pVCpu->cpum.GstCtx.rip != EMGetInhibitInterruptsPC(pVCpu))
7421 { /* likely */ }
7422 else
7423 {
7424 Log(("%s: VM entry with events blocked by MOV SS -> VMFail\n", pszInstr));
7425 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmentry_BlocKMovSS;
7426 iemVmxVmFail(pVCpu, VMXINSTRERR_VMENTRY_BLOCK_MOVSS);
7427 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7428 return VINF_SUCCESS;
7429 }
7430
7431 if (uInstrId == VMXINSTRID_VMLAUNCH)
7432 {
7433 /* VMLAUNCH with non-clear VMCS. */
7434 if (pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.fVmcsState == VMX_V_VMCS_LAUNCH_STATE_CLEAR)
7435 { /* likely */ }
7436 else
7437 {
7438 Log(("vmlaunch: VMLAUNCH with non-clear VMCS -> VMFail\n"));
7439 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmentry_VmcsClear;
7440 iemVmxVmFail(pVCpu, VMXINSTRERR_VMLAUNCH_NON_CLEAR_VMCS);
7441 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7442 return VINF_SUCCESS;
7443 }
7444 }
7445 else
7446 {
7447 /* VMRESUME with non-launched VMCS. */
7448 if (pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.fVmcsState == VMX_V_VMCS_LAUNCH_STATE_LAUNCHED)
7449 { /* likely */ }
7450 else
7451 {
7452 Log(("vmresume: VMRESUME with non-launched VMCS -> VMFail\n"));
7453 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmentry_VmcsLaunch;
7454 iemVmxVmFail(pVCpu, VMXINSTRERR_VMRESUME_NON_LAUNCHED_VMCS);
7455 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7456 return VINF_SUCCESS;
7457 }
7458 }
7459
7460 /*
7461 * We are allowed to cache VMCS related data structures (such as I/O bitmaps, MSR bitmaps)
7462 * while entering VMX non-root mode. We do some of this while checking VM-execution
7463 * controls. The nested hypervisor should not make assumptions and cannot expect
7464 * predictable behavior if changes to these structures are made in guest memory while
7465 * executing in VMX non-root mode. As far as VirtualBox is concerned, the guest cannot
7466 * modify them anyway as we cache them in host memory.
7467 *
7468 * See Intel spec. 24.11.4 "Software Access to Related Structures".
7469 */
7470 PVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
7471 Assert(pVmcs);
7472 Assert(IEM_VMX_HAS_CURRENT_VMCS(pVCpu));
7473
7474 int rc = iemVmxVmentryCheckCtls(pVCpu, pszInstr);
7475 if (RT_SUCCESS(rc))
7476 {
7477 rc = iemVmxVmentryCheckHostState(pVCpu, pszInstr);
7478 if (RT_SUCCESS(rc))
7479 {
7480 /*
7481 * Initialize read-only VMCS fields before VM-entry since we don't update all of them
7482 * for every VM-exit. This needs to be done before invoking a VM-exit (even those
7483 * ones that may occur during VM-entry below).
7484 */
7485 iemVmxVmentryInitReadOnlyFields(pVCpu);
7486
7487 /*
7488 * Blocking of NMIs need to be restored if VM-entry fails due to invalid-guest state.
7489 * So we save the VMCPU_FF_BLOCK_NMI force-flag here so we can restore it on
7490 * VM-exit when required.
7491 * See Intel spec. 26.7 "VM-entry Failures During or After Loading Guest State"
7492 */
7493 iemVmxVmentrySaveNmiBlockingFF(pVCpu);
7494
7495 rc = iemVmxVmentryCheckGuestState(pVCpu, pszInstr);
7496 if (RT_SUCCESS(rc))
7497 {
7498 /*
7499 * We've now entered nested-guest execution.
7500 *
7501 * It is important do this prior to loading the guest state because
7502 * as part of loading the guest state, PGM (and perhaps other components
7503 * in the future) relies on detecting whether VMX non-root mode has been
7504 * entered.
7505 */
7506 pVCpu->cpum.GstCtx.hwvirt.vmx.fInVmxNonRootMode = true;
7507
7508 rc = iemVmxVmentryLoadGuestState(pVCpu, pszInstr);
7509 if (RT_SUCCESS(rc))
7510 {
7511 rc = iemVmxVmentryLoadGuestAutoMsrs(pVCpu, pszInstr);
7512 if (RT_SUCCESS(rc))
7513 {
7514 Assert(rc != VINF_CPUM_R3_MSR_WRITE);
7515
7516 /* VMLAUNCH instruction must update the VMCS launch state. */
7517 if (uInstrId == VMXINSTRID_VMLAUNCH)
7518 pVmcs->fVmcsState = VMX_V_VMCS_LAUNCH_STATE_LAUNCHED;
7519
7520 /* Perform the VMX transition (PGM updates). */
7521 VBOXSTRICTRC rcStrict = iemVmxTransition(pVCpu);
7522 if (rcStrict == VINF_SUCCESS)
7523 { /* likely */ }
7524 else if (RT_SUCCESS(rcStrict))
7525 {
7526 Log3(("%s: iemVmxTransition returns %Rrc -> Setting passup status\n", pszInstr,
7527 VBOXSTRICTRC_VAL(rcStrict)));
7528 rcStrict = iemSetPassUpStatus(pVCpu, rcStrict);
7529 }
7530 else
7531 {
7532 Log3(("%s: iemVmxTransition failed! rc=%Rrc\n", pszInstr, VBOXSTRICTRC_VAL(rcStrict)));
7533 return rcStrict;
7534 }
7535
7536 /* Paranoia. */
7537 Assert(rcStrict == VINF_SUCCESS);
7538
7539 /*
7540 * The priority of potential VM-exits during VM-entry is important.
7541 * The priorities of VM-exits and events are listed from highest
7542 * to lowest as follows:
7543 *
7544 * 1. Event injection.
7545 * 2. Trap on task-switch (T flag set in TSS).
7546 * 3. TPR below threshold / APIC-write.
7547 * 4. SMI, INIT.
7548 * 5. MTF exit.
7549 * 6. Debug-trap exceptions (EFLAGS.TF), pending debug exceptions.
7550 * 7. VMX-preemption timer.
7551 * 9. NMI-window exit.
7552 * 10. NMI injection.
7553 * 11. Interrupt-window exit.
7554 * 12. Virtual-interrupt injection.
7555 * 13. Interrupt injection.
7556 * 14. Process next instruction (fetch, decode, execute).
7557 */
7558
7559 /* Setup VMX-preemption timer. */
7560 iemVmxVmentrySetupPreemptTimer(pVCpu, pszInstr);
7561
7562 /* Setup monitor-trap flag. */
7563 iemVmxVmentrySetupMtf(pVCpu, pszInstr);
7564
7565 /* Setup NMI-window exiting. */
7566 iemVmxVmentrySetupNmiWindow(pVCpu, pszInstr);
7567
7568 /* Setup interrupt-window exiting. */
7569 iemVmxVmentrySetupIntWindow(pVCpu, pszInstr);
7570
7571 /*
7572 * Inject any event that the nested hypervisor wants to inject.
7573 * Note! We cannot immediately perform the event injection here as we may have
7574 * pending PGM operations to perform due to switching page tables and/or
7575 * mode.
7576 */
7577 iemVmxVmentryInjectEvent(pVCpu, pszInstr);
7578
7579# if defined(VBOX_WITH_NESTED_HWVIRT_ONLY_IN_IEM) && defined(IN_RING3)
7580 /* Reschedule to IEM-only execution of the nested-guest. */
7581 Log(("%s: Enabling IEM-only EM execution policy!\n", pszInstr));
7582 int rcSched = EMR3SetExecutionPolicy(pVCpu->CTX_SUFF(pVM)->pUVM, EMEXECPOLICY_IEM_ALL, true);
7583 if (rcSched != VINF_SUCCESS)
7584 iemSetPassUpStatus(pVCpu, rcSched);
7585# endif
7586
7587 /* Finally, done. */
7588 Log3(("%s: cs:rip=%#04x:%#RX64 cr0=%#RX64 (%#RX64) cr4=%#RX64 (%#RX64) efer=%#RX64\n",
7589 pszInstr, pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip, pVCpu->cpum.GstCtx.cr0,
7590 pVmcs->u64Cr0ReadShadow.u, pVCpu->cpum.GstCtx.cr4, pVmcs->u64Cr4ReadShadow.u,
7591 pVCpu->cpum.GstCtx.msrEFER));
7592 return VINF_SUCCESS;
7593 }
7594 return iemVmxVmexit(pVCpu, VMX_EXIT_ERR_MSR_LOAD | VMX_EXIT_REASON_ENTRY_FAILED, pVmcs->u64RoExitQual.u);
7595 }
7596 }
7597 return iemVmxVmexit(pVCpu, VMX_EXIT_ERR_INVALID_GUEST_STATE | VMX_EXIT_REASON_ENTRY_FAILED, pVmcs->u64RoExitQual.u);
7598 }
7599
7600 iemVmxVmFail(pVCpu, VMXINSTRERR_VMENTRY_INVALID_HOST_STATE);
7601 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7602 return VINF_SUCCESS;
7603 }
7604
7605 iemVmxVmFail(pVCpu, VMXINSTRERR_VMENTRY_INVALID_CTLS);
7606 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7607 return VINF_SUCCESS;
7608# endif
7609}
7610
7611
7612/**
7613 * Checks whether an RDMSR or WRMSR instruction for the given MSR is intercepted
7614 * (causes a VM-exit) or not.
7615 *
7616 * @returns @c true if the instruction is intercepted, @c false otherwise.
7617 * @param pVCpu The cross context virtual CPU structure.
7618 * @param uExitReason The VM-exit reason (VMX_EXIT_RDMSR or
7619 * VMX_EXIT_WRMSR).
7620 * @param idMsr The MSR.
7621 */
7622IEM_STATIC bool iemVmxIsRdmsrWrmsrInterceptSet(PCVMCPU pVCpu, uint32_t uExitReason, uint32_t idMsr)
7623{
7624 Assert(IEM_VMX_IS_NON_ROOT_MODE(pVCpu));
7625 Assert( uExitReason == VMX_EXIT_RDMSR
7626 || uExitReason == VMX_EXIT_WRMSR);
7627
7628 /* Consult the MSR bitmap if the feature is supported. */
7629 PCVMXVVMCS const pVmcs = &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs;
7630 Assert(pVmcs);
7631 if (pVmcs->u32ProcCtls & VMX_PROC_CTLS_USE_MSR_BITMAPS)
7632 {
7633 uint32_t const fMsrpm = CPUMGetVmxMsrPermission(pVCpu->cpum.GstCtx.hwvirt.vmx.abMsrBitmap, idMsr);
7634 if (uExitReason == VMX_EXIT_RDMSR)
7635 return RT_BOOL(fMsrpm & VMXMSRPM_EXIT_RD);
7636 return RT_BOOL(fMsrpm & VMXMSRPM_EXIT_WR);
7637 }
7638
7639 /* Without MSR bitmaps, all MSR accesses are intercepted. */
7640 return true;
7641}
7642
7643
7644/**
7645 * VMREAD instruction execution worker that does not perform any validation checks.
7646 *
7647 * Callers are expected to have performed the necessary checks and to ensure the
7648 * VMREAD will succeed.
7649 *
7650 * @param pVmcs Pointer to the virtual VMCS.
7651 * @param pu64Dst Where to write the VMCS value.
7652 * @param u64VmcsField The VMCS field.
7653 *
7654 * @remarks May be called with interrupts disabled.
7655 */
7656IEM_STATIC void iemVmxVmreadNoCheck(PCVMXVVMCS pVmcs, uint64_t *pu64Dst, uint64_t u64VmcsField)
7657{
7658 VMXVMCSFIELD VmcsField;
7659 VmcsField.u = u64VmcsField;
7660 uint8_t const uWidth = RT_BF_GET(VmcsField.u, VMX_BF_VMCSFIELD_WIDTH);
7661 uint8_t const uType = RT_BF_GET(VmcsField.u, VMX_BF_VMCSFIELD_TYPE);
7662 uint8_t const uWidthType = (uWidth << 2) | uType;
7663 uint8_t const uIndex = RT_BF_GET(VmcsField.u, VMX_BF_VMCSFIELD_INDEX);
7664 Assert(uIndex <= VMX_V_VMCS_MAX_INDEX);
7665 uint16_t const offField = g_aoffVmcsMap[uWidthType][uIndex];
7666 AssertMsg(offField < VMX_V_VMCS_SIZE, ("off=%u field=%#RX64 width=%#x type=%#x index=%#x (%u)\n", offField, u64VmcsField,
7667 uWidth, uType, uIndex, uIndex));
7668 AssertCompile(VMX_V_SHADOW_VMCS_SIZE == VMX_V_VMCS_SIZE);
7669
7670 /*
7671 * Read the VMCS component based on the field's effective width.
7672 *
7673 * The effective width is 64-bit fields adjusted to 32-bits if the access-type
7674 * indicates high bits (little endian).
7675 *
7676 * Note! The caller is responsible to trim the result and update registers
7677 * or memory locations are required. Here we just zero-extend to the largest
7678 * type (i.e. 64-bits).
7679 */
7680 uint8_t const *pbVmcs = (uint8_t const *)pVmcs;
7681 uint8_t const *pbField = pbVmcs + offField;
7682 uint8_t const uEffWidth = VMXGetVmcsFieldWidthEff(VmcsField.u);
7683 switch (uEffWidth)
7684 {
7685 case VMX_VMCSFIELD_WIDTH_64BIT:
7686 case VMX_VMCSFIELD_WIDTH_NATURAL: *pu64Dst = *(uint64_t const *)pbField; break;
7687 case VMX_VMCSFIELD_WIDTH_32BIT: *pu64Dst = *(uint32_t const *)pbField; break;
7688 case VMX_VMCSFIELD_WIDTH_16BIT: *pu64Dst = *(uint16_t const *)pbField; break;
7689 }
7690}
7691
7692
7693/**
7694 * VMREAD common (memory/register) instruction execution worker.
7695 *
7696 * @returns Strict VBox status code.
7697 * @param pVCpu The cross context virtual CPU structure.
7698 * @param cbInstr The instruction length in bytes.
7699 * @param pu64Dst Where to write the VMCS value (only updated when
7700 * VINF_SUCCESS is returned).
7701 * @param u64VmcsField The VMCS field.
7702 * @param pExitInfo Pointer to the VM-exit information. Optional, can be
7703 * NULL.
7704 */
7705IEM_STATIC VBOXSTRICTRC iemVmxVmreadCommon(PVMCPUCC pVCpu, uint8_t cbInstr, uint64_t *pu64Dst, uint64_t u64VmcsField,
7706 PCVMXVEXITINFO pExitInfo)
7707{
7708 /* Nested-guest intercept. */
7709 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
7710 && CPUMIsGuestVmxVmreadVmwriteInterceptSet(pVCpu, VMX_EXIT_VMREAD, u64VmcsField))
7711 {
7712 if (pExitInfo)
7713 return iemVmxVmexitInstrWithInfo(pVCpu, pExitInfo);
7714 return iemVmxVmexitInstrNeedsInfo(pVCpu, VMX_EXIT_VMREAD, VMXINSTRID_VMREAD, cbInstr);
7715 }
7716
7717 /* CPL. */
7718 if (pVCpu->iem.s.uCpl == 0)
7719 { /* likely */ }
7720 else
7721 {
7722 Log(("vmread: CPL %u -> #GP(0)\n", pVCpu->iem.s.uCpl));
7723 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmread_Cpl;
7724 return iemRaiseGeneralProtectionFault0(pVCpu);
7725 }
7726
7727 /* VMCS pointer in root mode. */
7728 if ( !IEM_VMX_IS_ROOT_MODE(pVCpu)
7729 || IEM_VMX_HAS_CURRENT_VMCS(pVCpu))
7730 { /* likely */ }
7731 else
7732 {
7733 Log(("vmread: VMCS pointer %#RGp invalid -> VMFailInvalid\n", IEM_VMX_GET_CURRENT_VMCS(pVCpu)));
7734 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmread_PtrInvalid;
7735 iemVmxVmFailInvalid(pVCpu);
7736 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7737 return VINF_SUCCESS;
7738 }
7739
7740 /* VMCS-link pointer in non-root mode. */
7741 if ( !IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
7742 || IEM_VMX_HAS_SHADOW_VMCS(pVCpu))
7743 { /* likely */ }
7744 else
7745 {
7746 Log(("vmread: VMCS-link pointer %#RGp invalid -> VMFailInvalid\n", IEM_VMX_GET_SHADOW_VMCS(pVCpu)));
7747 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmread_LinkPtrInvalid;
7748 iemVmxVmFailInvalid(pVCpu);
7749 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7750 return VINF_SUCCESS;
7751 }
7752
7753 /* Supported VMCS field. */
7754 if (CPUMIsGuestVmxVmcsFieldValid(pVCpu->CTX_SUFF(pVM), u64VmcsField))
7755 { /* likely */ }
7756 else
7757 {
7758 Log(("vmread: VMCS field %#RX64 invalid -> VMFail\n", u64VmcsField));
7759 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmread_FieldInvalid;
7760 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = u64VmcsField;
7761 iemVmxVmFail(pVCpu, VMXINSTRERR_VMREAD_INVALID_COMPONENT);
7762 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7763 return VINF_SUCCESS;
7764 }
7765
7766 /*
7767 * Reading from the current or shadow VMCS.
7768 */
7769 PCVMXVVMCS pVmcs = !IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
7770 ? &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs
7771 : &pVCpu->cpum.GstCtx.hwvirt.vmx.ShadowVmcs;
7772 iemVmxVmreadNoCheck(pVmcs, pu64Dst, u64VmcsField);
7773 return VINF_SUCCESS;
7774}
7775
7776
7777/**
7778 * VMREAD (64-bit register) instruction execution worker.
7779 *
7780 * @returns Strict VBox status code.
7781 * @param pVCpu The cross context virtual CPU structure.
7782 * @param cbInstr The instruction length in bytes.
7783 * @param pu64Dst Where to store the VMCS field's value.
7784 * @param u64VmcsField The VMCS field.
7785 * @param pExitInfo Pointer to the VM-exit information. Optional, can be
7786 * NULL.
7787 */
7788IEM_STATIC VBOXSTRICTRC iemVmxVmreadReg64(PVMCPUCC pVCpu, uint8_t cbInstr, uint64_t *pu64Dst, uint64_t u64VmcsField,
7789 PCVMXVEXITINFO pExitInfo)
7790{
7791 VBOXSTRICTRC rcStrict = iemVmxVmreadCommon(pVCpu, cbInstr, pu64Dst, u64VmcsField, pExitInfo);
7792 if (rcStrict == VINF_SUCCESS)
7793 {
7794 iemVmxVmreadSuccess(pVCpu, cbInstr);
7795 return VINF_SUCCESS;
7796 }
7797
7798 Log(("vmread/reg: iemVmxVmreadCommon failed rc=%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)));
7799 return rcStrict;
7800}
7801
7802
7803/**
7804 * VMREAD (32-bit register) instruction execution worker.
7805 *
7806 * @returns Strict VBox status code.
7807 * @param pVCpu The cross context virtual CPU structure.
7808 * @param cbInstr The instruction length in bytes.
7809 * @param pu32Dst Where to store the VMCS field's value.
7810 * @param u32VmcsField The VMCS field.
7811 * @param pExitInfo Pointer to the VM-exit information. Optional, can be
7812 * NULL.
7813 */
7814IEM_STATIC VBOXSTRICTRC iemVmxVmreadReg32(PVMCPUCC pVCpu, uint8_t cbInstr, uint32_t *pu32Dst, uint64_t u32VmcsField,
7815 PCVMXVEXITINFO pExitInfo)
7816{
7817 uint64_t u64Dst;
7818 VBOXSTRICTRC rcStrict = iemVmxVmreadCommon(pVCpu, cbInstr, &u64Dst, u32VmcsField, pExitInfo);
7819 if (rcStrict == VINF_SUCCESS)
7820 {
7821 *pu32Dst = u64Dst;
7822 iemVmxVmreadSuccess(pVCpu, cbInstr);
7823 return VINF_SUCCESS;
7824 }
7825
7826 Log(("vmread/reg: iemVmxVmreadCommon failed rc=%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)));
7827 return rcStrict;
7828}
7829
7830
7831/**
7832 * VMREAD (memory) instruction execution worker.
7833 *
7834 * @returns Strict VBox status code.
7835 * @param pVCpu The cross context virtual CPU structure.
7836 * @param cbInstr The instruction length in bytes.
7837 * @param iEffSeg The effective segment register to use with @a u64Val.
7838 * Pass UINT8_MAX if it is a register access.
7839 * @param GCPtrDst The guest linear address to store the VMCS field's
7840 * value.
7841 * @param u64VmcsField The VMCS field.
7842 * @param pExitInfo Pointer to the VM-exit information. Optional, can be
7843 * NULL.
7844 */
7845IEM_STATIC VBOXSTRICTRC iemVmxVmreadMem(PVMCPUCC pVCpu, uint8_t cbInstr, uint8_t iEffSeg, RTGCPTR GCPtrDst, uint64_t u64VmcsField,
7846 PCVMXVEXITINFO pExitInfo)
7847{
7848 uint64_t u64Dst;
7849 VBOXSTRICTRC rcStrict = iemVmxVmreadCommon(pVCpu, cbInstr, &u64Dst, u64VmcsField, pExitInfo);
7850 if (rcStrict == VINF_SUCCESS)
7851 {
7852 /*
7853 * Write the VMCS field's value to the location specified in guest-memory.
7854 */
7855 if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
7856 rcStrict = iemMemStoreDataU64(pVCpu, iEffSeg, GCPtrDst, u64Dst);
7857 else
7858 rcStrict = iemMemStoreDataU32(pVCpu, iEffSeg, GCPtrDst, u64Dst);
7859 if (rcStrict == VINF_SUCCESS)
7860 {
7861 iemVmxVmreadSuccess(pVCpu, cbInstr);
7862 return VINF_SUCCESS;
7863 }
7864
7865 Log(("vmread/mem: Failed to write to memory operand at %#RGv, rc=%Rrc\n", GCPtrDst, VBOXSTRICTRC_VAL(rcStrict)));
7866 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmread_PtrMap;
7867 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = GCPtrDst;
7868 return rcStrict;
7869 }
7870
7871 Log(("vmread/mem: iemVmxVmreadCommon failed rc=%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)));
7872 return rcStrict;
7873}
7874
7875
7876/**
7877 * VMWRITE instruction execution worker that does not perform any validation
7878 * checks.
7879 *
7880 * Callers are expected to have performed the necessary checks and to ensure the
7881 * VMWRITE will succeed.
7882 *
7883 * @param pVmcs Pointer to the virtual VMCS.
7884 * @param u64Val The value to write.
7885 * @param u64VmcsField The VMCS field.
7886 *
7887 * @remarks May be called with interrupts disabled.
7888 */
7889IEM_STATIC void iemVmxVmwriteNoCheck(PVMXVVMCS pVmcs, uint64_t u64Val, uint64_t u64VmcsField)
7890{
7891 VMXVMCSFIELD VmcsField;
7892 VmcsField.u = u64VmcsField;
7893 uint8_t const uWidth = RT_BF_GET(VmcsField.u, VMX_BF_VMCSFIELD_WIDTH);
7894 uint8_t const uType = RT_BF_GET(VmcsField.u, VMX_BF_VMCSFIELD_TYPE);
7895 uint8_t const uWidthType = (uWidth << 2) | uType;
7896 uint8_t const uIndex = RT_BF_GET(VmcsField.u, VMX_BF_VMCSFIELD_INDEX);
7897 Assert(uIndex <= VMX_V_VMCS_MAX_INDEX);
7898 uint16_t const offField = g_aoffVmcsMap[uWidthType][uIndex];
7899 Assert(offField < VMX_V_VMCS_SIZE);
7900 AssertCompile(VMX_V_SHADOW_VMCS_SIZE == VMX_V_VMCS_SIZE);
7901
7902 /*
7903 * Write the VMCS component based on the field's effective width.
7904 *
7905 * The effective width is 64-bit fields adjusted to 32-bits if the access-type
7906 * indicates high bits (little endian).
7907 */
7908 uint8_t *pbVmcs = (uint8_t *)pVmcs;
7909 uint8_t *pbField = pbVmcs + offField;
7910 uint8_t const uEffWidth = VMXGetVmcsFieldWidthEff(VmcsField.u);
7911 switch (uEffWidth)
7912 {
7913 case VMX_VMCSFIELD_WIDTH_64BIT:
7914 case VMX_VMCSFIELD_WIDTH_NATURAL: *(uint64_t *)pbField = u64Val; break;
7915 case VMX_VMCSFIELD_WIDTH_32BIT: *(uint32_t *)pbField = u64Val; break;
7916 case VMX_VMCSFIELD_WIDTH_16BIT: *(uint16_t *)pbField = u64Val; break;
7917 }
7918}
7919
7920
7921/**
7922 * VMWRITE instruction execution worker.
7923 *
7924 * @returns Strict VBox status code.
7925 * @param pVCpu The cross context virtual CPU structure.
7926 * @param cbInstr The instruction length in bytes.
7927 * @param iEffSeg The effective segment register to use with @a u64Val.
7928 * Pass UINT8_MAX if it is a register access.
7929 * @param u64Val The value to write (or guest linear address to the
7930 * value), @a iEffSeg will indicate if it's a memory
7931 * operand.
7932 * @param u64VmcsField The VMCS field.
7933 * @param pExitInfo Pointer to the VM-exit information. Optional, can be
7934 * NULL.
7935 */
7936IEM_STATIC VBOXSTRICTRC iemVmxVmwrite(PVMCPUCC pVCpu, uint8_t cbInstr, uint8_t iEffSeg, uint64_t u64Val, uint64_t u64VmcsField,
7937 PCVMXVEXITINFO pExitInfo)
7938{
7939 /* Nested-guest intercept. */
7940 if ( IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
7941 && CPUMIsGuestVmxVmreadVmwriteInterceptSet(pVCpu, VMX_EXIT_VMWRITE, u64VmcsField))
7942 {
7943 if (pExitInfo)
7944 return iemVmxVmexitInstrWithInfo(pVCpu, pExitInfo);
7945 return iemVmxVmexitInstrNeedsInfo(pVCpu, VMX_EXIT_VMWRITE, VMXINSTRID_VMWRITE, cbInstr);
7946 }
7947
7948 /* CPL. */
7949 if (pVCpu->iem.s.uCpl == 0)
7950 { /* likely */ }
7951 else
7952 {
7953 Log(("vmwrite: CPL %u -> #GP(0)\n", pVCpu->iem.s.uCpl));
7954 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmwrite_Cpl;
7955 return iemRaiseGeneralProtectionFault0(pVCpu);
7956 }
7957
7958 /* VMCS pointer in root mode. */
7959 if ( !IEM_VMX_IS_ROOT_MODE(pVCpu)
7960 || IEM_VMX_HAS_CURRENT_VMCS(pVCpu))
7961 { /* likely */ }
7962 else
7963 {
7964 Log(("vmwrite: VMCS pointer %#RGp invalid -> VMFailInvalid\n", IEM_VMX_GET_CURRENT_VMCS(pVCpu)));
7965 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmwrite_PtrInvalid;
7966 iemVmxVmFailInvalid(pVCpu);
7967 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7968 return VINF_SUCCESS;
7969 }
7970
7971 /* VMCS-link pointer in non-root mode. */
7972 if ( !IEM_VMX_IS_NON_ROOT_MODE(pVCpu)
7973 || IEM_VMX_HAS_SHADOW_VMCS(pVCpu))
7974 { /* likely */ }
7975 else
7976 {
7977 Log(("vmwrite: VMCS-link pointer %#RGp invalid -> VMFailInvalid\n", IEM_VMX_GET_SHADOW_VMCS(pVCpu)));
7978 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmwrite_LinkPtrInvalid;
7979 iemVmxVmFailInvalid(pVCpu);
7980 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
7981 return VINF_SUCCESS;
7982 }
7983
7984 /* If the VMWRITE instruction references memory, access the specified memory operand. */
7985 bool const fIsRegOperand = iEffSeg == UINT8_MAX;
7986 if (!fIsRegOperand)
7987 {
7988 /* Read the value from the specified guest memory location. */
7989 VBOXSTRICTRC rcStrict;
7990 RTGCPTR const GCPtrVal = u64Val;
7991 if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
7992 rcStrict = iemMemFetchDataU64(pVCpu, &u64Val, iEffSeg, GCPtrVal);
7993 else
7994 rcStrict = iemMemFetchDataU32_ZX_U64(pVCpu, &u64Val, iEffSeg, GCPtrVal);
7995 if (RT_UNLIKELY(rcStrict != VINF_SUCCESS))
7996 {
7997 Log(("vmwrite: Failed to read value from memory operand at %#RGv, rc=%Rrc\n", GCPtrVal, VBOXSTRICTRC_VAL(rcStrict)));
7998 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmwrite_PtrMap;
7999 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = GCPtrVal;
8000 return rcStrict;
8001 }
8002 }
8003 else
8004 Assert(!pExitInfo || pExitInfo->InstrInfo.VmreadVmwrite.fIsRegOperand);
8005
8006 /* Supported VMCS field. */
8007 if (CPUMIsGuestVmxVmcsFieldValid(pVCpu->CTX_SUFF(pVM), u64VmcsField))
8008 { /* likely */ }
8009 else
8010 {
8011 Log(("vmwrite: VMCS field %#RX64 invalid -> VMFail\n", u64VmcsField));
8012 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmwrite_FieldInvalid;
8013 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = u64VmcsField;
8014 iemVmxVmFail(pVCpu, VMXINSTRERR_VMWRITE_INVALID_COMPONENT);
8015 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8016 return VINF_SUCCESS;
8017 }
8018
8019 /* Read-only VMCS field. */
8020 bool const fIsFieldReadOnly = VMXIsVmcsFieldReadOnly(u64VmcsField);
8021 if ( !fIsFieldReadOnly
8022 || IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fVmxVmwriteAll)
8023 { /* likely */ }
8024 else
8025 {
8026 Log(("vmwrite: Write to read-only VMCS component %#RX64 -> VMFail\n", u64VmcsField));
8027 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmwrite_FieldRo;
8028 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = u64VmcsField;
8029 iemVmxVmFail(pVCpu, VMXINSTRERR_VMWRITE_RO_COMPONENT);
8030 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8031 return VINF_SUCCESS;
8032 }
8033
8034 /*
8035 * Write to the current or shadow VMCS.
8036 */
8037 bool const fInVmxNonRootMode = IEM_VMX_IS_NON_ROOT_MODE(pVCpu);
8038 PVMXVVMCS pVmcs = !fInVmxNonRootMode
8039 ? &pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs
8040 : &pVCpu->cpum.GstCtx.hwvirt.vmx.ShadowVmcs;
8041 iemVmxVmwriteNoCheck(pVmcs, u64Val, u64VmcsField);
8042
8043 /* Notify HM that the VMCS content might have changed. */
8044 if (!fInVmxNonRootMode)
8045 HMNotifyVmxNstGstCurrentVmcsChanged(pVCpu);
8046
8047 iemVmxVmSucceed(pVCpu);
8048 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8049 return VINF_SUCCESS;
8050}
8051
8052
8053/**
8054 * VMCLEAR instruction execution worker.
8055 *
8056 * @returns Strict VBox status code.
8057 * @param pVCpu The cross context virtual CPU structure.
8058 * @param cbInstr The instruction length in bytes.
8059 * @param iEffSeg The effective segment register to use with @a GCPtrVmcs.
8060 * @param GCPtrVmcs The linear address of the VMCS pointer.
8061 * @param pExitInfo Pointer to the VM-exit information. Optional, can be NULL.
8062 *
8063 * @remarks Common VMX instruction checks are already expected to by the caller,
8064 * i.e. VMX operation, CR4.VMXE, Real/V86 mode, EFER/CS.L checks.
8065 */
8066IEM_STATIC VBOXSTRICTRC iemVmxVmclear(PVMCPUCC pVCpu, uint8_t cbInstr, uint8_t iEffSeg, RTGCPHYS GCPtrVmcs,
8067 PCVMXVEXITINFO pExitInfo)
8068{
8069 /* Nested-guest intercept. */
8070 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
8071 {
8072 if (pExitInfo)
8073 return iemVmxVmexitInstrWithInfo(pVCpu, pExitInfo);
8074 return iemVmxVmexitInstrNeedsInfo(pVCpu, VMX_EXIT_VMCLEAR, VMXINSTRID_NONE, cbInstr);
8075 }
8076
8077 Assert(IEM_VMX_IS_ROOT_MODE(pVCpu));
8078
8079 /* CPL. */
8080 if (pVCpu->iem.s.uCpl == 0)
8081 { /* likely */ }
8082 else
8083 {
8084 Log(("vmclear: CPL %u -> #GP(0)\n", pVCpu->iem.s.uCpl));
8085 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmclear_Cpl;
8086 return iemRaiseGeneralProtectionFault0(pVCpu);
8087 }
8088
8089 /* Get the VMCS pointer from the location specified by the source memory operand. */
8090 RTGCPHYS GCPhysVmcs;
8091 VBOXSTRICTRC rcStrict = iemMemFetchDataU64(pVCpu, &GCPhysVmcs, iEffSeg, GCPtrVmcs);
8092 if (RT_LIKELY(rcStrict == VINF_SUCCESS))
8093 { /* likely */ }
8094 else
8095 {
8096 Log(("vmclear: Failed to read VMCS physaddr from %#RGv, rc=%Rrc\n", GCPtrVmcs, VBOXSTRICTRC_VAL(rcStrict)));
8097 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmclear_PtrMap;
8098 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = GCPtrVmcs;
8099 return rcStrict;
8100 }
8101
8102 /* VMCS pointer alignment. */
8103 if (!(GCPhysVmcs & X86_PAGE_4K_OFFSET_MASK))
8104 { /* likely */ }
8105 else
8106 {
8107 Log(("vmclear: VMCS pointer not page-aligned -> VMFail()\n"));
8108 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmclear_PtrAlign;
8109 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = GCPhysVmcs;
8110 iemVmxVmFail(pVCpu, VMXINSTRERR_VMCLEAR_INVALID_PHYSADDR);
8111 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8112 return VINF_SUCCESS;
8113 }
8114
8115 /* VMCS physical-address width limits. */
8116 if (!(GCPhysVmcs >> IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cVmxMaxPhysAddrWidth))
8117 { /* likely */ }
8118 else
8119 {
8120 Log(("vmclear: VMCS pointer extends beyond physical-address width -> VMFail()\n"));
8121 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmclear_PtrWidth;
8122 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = GCPhysVmcs;
8123 iemVmxVmFail(pVCpu, VMXINSTRERR_VMCLEAR_INVALID_PHYSADDR);
8124 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8125 return VINF_SUCCESS;
8126 }
8127
8128 /* VMCS is not the VMXON region. */
8129 if (GCPhysVmcs != pVCpu->cpum.GstCtx.hwvirt.vmx.GCPhysVmxon)
8130 { /* likely */ }
8131 else
8132 {
8133 Log(("vmclear: VMCS pointer cannot be identical to VMXON region pointer -> VMFail()\n"));
8134 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmclear_PtrVmxon;
8135 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = GCPhysVmcs;
8136 iemVmxVmFail(pVCpu, VMXINSTRERR_VMCLEAR_VMXON_PTR);
8137 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8138 return VINF_SUCCESS;
8139 }
8140
8141 /* Ensure VMCS is not MMIO, ROM etc. This is not an Intel requirement but a
8142 restriction imposed by our implementation. */
8143 if (PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), GCPhysVmcs))
8144 { /* likely */ }
8145 else
8146 {
8147 Log(("vmclear: VMCS not normal memory -> VMFail()\n"));
8148 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmclear_PtrAbnormal;
8149 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = GCPhysVmcs;
8150 iemVmxVmFail(pVCpu, VMXINSTRERR_VMCLEAR_INVALID_PHYSADDR);
8151 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8152 return VINF_SUCCESS;
8153 }
8154
8155 /*
8156 * VMCLEAR allows committing and clearing any valid VMCS pointer.
8157 *
8158 * If the current VMCS is the one being cleared, set its state to 'clear' and commit
8159 * to guest memory. Otherwise, set the state of the VMCS referenced in guest memory
8160 * to 'clear'.
8161 */
8162 uint8_t const fVmcsLaunchStateClear = VMX_V_VMCS_LAUNCH_STATE_CLEAR;
8163 if ( IEM_VMX_HAS_CURRENT_VMCS(pVCpu)
8164 && IEM_VMX_GET_CURRENT_VMCS(pVCpu) == GCPhysVmcs)
8165 {
8166 pVCpu->cpum.GstCtx.hwvirt.vmx.Vmcs.fVmcsState = fVmcsLaunchStateClear;
8167 iemVmxWriteCurrentVmcsToGstMem(pVCpu);
8168 IEM_VMX_CLEAR_CURRENT_VMCS(pVCpu);
8169 }
8170 else
8171 {
8172 AssertCompileMemberSize(VMXVVMCS, fVmcsState, sizeof(fVmcsLaunchStateClear));
8173 rcStrict = PGMPhysSimpleWriteGCPhys(pVCpu->CTX_SUFF(pVM), GCPhysVmcs + RT_UOFFSETOF(VMXVVMCS, fVmcsState),
8174 (const void *)&fVmcsLaunchStateClear, sizeof(fVmcsLaunchStateClear));
8175 if (RT_FAILURE(rcStrict))
8176 return rcStrict;
8177 }
8178
8179 iemVmxVmSucceed(pVCpu);
8180 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8181 return VINF_SUCCESS;
8182}
8183
8184
8185/**
8186 * VMPTRST instruction execution worker.
8187 *
8188 * @returns Strict VBox status code.
8189 * @param pVCpu The cross context virtual CPU structure.
8190 * @param cbInstr The instruction length in bytes.
8191 * @param iEffSeg The effective segment register to use with @a GCPtrVmcs.
8192 * @param GCPtrVmcs The linear address of where to store the current VMCS
8193 * pointer.
8194 * @param pExitInfo Pointer to the VM-exit information. Optional, can be NULL.
8195 *
8196 * @remarks Common VMX instruction checks are already expected to by the caller,
8197 * i.e. VMX operation, CR4.VMXE, Real/V86 mode, EFER/CS.L checks.
8198 */
8199IEM_STATIC VBOXSTRICTRC iemVmxVmptrst(PVMCPUCC pVCpu, uint8_t cbInstr, uint8_t iEffSeg, RTGCPHYS GCPtrVmcs,
8200 PCVMXVEXITINFO pExitInfo)
8201{
8202 /* Nested-guest intercept. */
8203 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
8204 {
8205 if (pExitInfo)
8206 return iemVmxVmexitInstrWithInfo(pVCpu, pExitInfo);
8207 return iemVmxVmexitInstrNeedsInfo(pVCpu, VMX_EXIT_VMPTRST, VMXINSTRID_NONE, cbInstr);
8208 }
8209
8210 Assert(IEM_VMX_IS_ROOT_MODE(pVCpu));
8211
8212 /* CPL. */
8213 if (pVCpu->iem.s.uCpl == 0)
8214 { /* likely */ }
8215 else
8216 {
8217 Log(("vmptrst: CPL %u -> #GP(0)\n", pVCpu->iem.s.uCpl));
8218 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmptrst_Cpl;
8219 return iemRaiseGeneralProtectionFault0(pVCpu);
8220 }
8221
8222 /* Set the VMCS pointer to the location specified by the destination memory operand. */
8223 AssertCompile(NIL_RTGCPHYS == ~(RTGCPHYS)0U);
8224 VBOXSTRICTRC rcStrict = iemMemStoreDataU64(pVCpu, iEffSeg, GCPtrVmcs, IEM_VMX_GET_CURRENT_VMCS(pVCpu));
8225 if (RT_LIKELY(rcStrict == VINF_SUCCESS))
8226 {
8227 iemVmxVmSucceed(pVCpu);
8228 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8229 return rcStrict;
8230 }
8231
8232 Log(("vmptrst: Failed to store VMCS pointer to memory at destination operand %#Rrc\n", VBOXSTRICTRC_VAL(rcStrict)));
8233 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmptrst_PtrMap;
8234 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = GCPtrVmcs;
8235 return rcStrict;
8236}
8237
8238
8239/**
8240 * VMPTRLD instruction execution worker.
8241 *
8242 * @returns Strict VBox status code.
8243 * @param pVCpu The cross context virtual CPU structure.
8244 * @param cbInstr The instruction length in bytes.
8245 * @param GCPtrVmcs The linear address of the current VMCS pointer.
8246 * @param pExitInfo Pointer to the VM-exit information. Optional, can be NULL.
8247 *
8248 * @remarks Common VMX instruction checks are already expected to by the caller,
8249 * i.e. VMX operation, CR4.VMXE, Real/V86 mode, EFER/CS.L checks.
8250 */
8251IEM_STATIC VBOXSTRICTRC iemVmxVmptrld(PVMCPUCC pVCpu, uint8_t cbInstr, uint8_t iEffSeg, RTGCPHYS GCPtrVmcs,
8252 PCVMXVEXITINFO pExitInfo)
8253{
8254 /* Nested-guest intercept. */
8255 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
8256 {
8257 if (pExitInfo)
8258 return iemVmxVmexitInstrWithInfo(pVCpu, pExitInfo);
8259 return iemVmxVmexitInstrNeedsInfo(pVCpu, VMX_EXIT_VMPTRLD, VMXINSTRID_NONE, cbInstr);
8260 }
8261
8262 Assert(IEM_VMX_IS_ROOT_MODE(pVCpu));
8263
8264 /* CPL. */
8265 if (pVCpu->iem.s.uCpl == 0)
8266 { /* likely */ }
8267 else
8268 {
8269 Log(("vmptrld: CPL %u -> #GP(0)\n", pVCpu->iem.s.uCpl));
8270 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmptrld_Cpl;
8271 return iemRaiseGeneralProtectionFault0(pVCpu);
8272 }
8273
8274 /* Get the VMCS pointer from the location specified by the source memory operand. */
8275 RTGCPHYS GCPhysVmcs;
8276 VBOXSTRICTRC rcStrict = iemMemFetchDataU64(pVCpu, &GCPhysVmcs, iEffSeg, GCPtrVmcs);
8277 if (RT_LIKELY(rcStrict == VINF_SUCCESS))
8278 { /* likely */ }
8279 else
8280 {
8281 Log(("vmptrld: Failed to read VMCS physaddr from %#RGv, rc=%Rrc\n", GCPtrVmcs, VBOXSTRICTRC_VAL(rcStrict)));
8282 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmptrld_PtrMap;
8283 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = GCPtrVmcs;
8284 return rcStrict;
8285 }
8286
8287 /* VMCS pointer alignment. */
8288 if (!(GCPhysVmcs & X86_PAGE_4K_OFFSET_MASK))
8289 { /* likely */ }
8290 else
8291 {
8292 Log(("vmptrld: VMCS pointer not page-aligned -> VMFail()\n"));
8293 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmptrld_PtrAlign;
8294 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = GCPhysVmcs;
8295 iemVmxVmFail(pVCpu, VMXINSTRERR_VMPTRLD_INVALID_PHYSADDR);
8296 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8297 return VINF_SUCCESS;
8298 }
8299
8300 /* VMCS physical-address width limits. */
8301 if (!(GCPhysVmcs >> IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cVmxMaxPhysAddrWidth))
8302 { /* likely */ }
8303 else
8304 {
8305 Log(("vmptrld: VMCS pointer extends beyond physical-address width -> VMFail()\n"));
8306 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmptrld_PtrWidth;
8307 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = GCPhysVmcs;
8308 iemVmxVmFail(pVCpu, VMXINSTRERR_VMPTRLD_INVALID_PHYSADDR);
8309 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8310 return VINF_SUCCESS;
8311 }
8312
8313 /* VMCS is not the VMXON region. */
8314 if (GCPhysVmcs != pVCpu->cpum.GstCtx.hwvirt.vmx.GCPhysVmxon)
8315 { /* likely */ }
8316 else
8317 {
8318 Log(("vmptrld: VMCS pointer cannot be identical to VMXON region pointer -> VMFail()\n"));
8319 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmptrld_PtrVmxon;
8320 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = GCPhysVmcs;
8321 iemVmxVmFail(pVCpu, VMXINSTRERR_VMPTRLD_VMXON_PTR);
8322 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8323 return VINF_SUCCESS;
8324 }
8325
8326 /* Ensure VMCS is not MMIO, ROM etc. This is not an Intel requirement but a
8327 restriction imposed by our implementation. */
8328 if (PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), GCPhysVmcs))
8329 { /* likely */ }
8330 else
8331 {
8332 Log(("vmptrld: VMCS not normal memory -> VMFail()\n"));
8333 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmptrld_PtrAbnormal;
8334 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = GCPhysVmcs;
8335 iemVmxVmFail(pVCpu, VMXINSTRERR_VMPTRLD_INVALID_PHYSADDR);
8336 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8337 return VINF_SUCCESS;
8338 }
8339
8340 /* Read just the VMCS revision from the VMCS. */
8341 VMXVMCSREVID VmcsRevId;
8342 int rc = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), &VmcsRevId, GCPhysVmcs, sizeof(VmcsRevId));
8343 if (RT_SUCCESS(rc))
8344 { /* likely */ }
8345 else
8346 {
8347 Log(("vmptrld: Failed to read revision identifier from VMCS at %#RGp, rc=%Rrc\n", GCPhysVmcs, rc));
8348 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmptrld_RevPtrReadPhys;
8349 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = GCPhysVmcs;
8350 return rc;
8351 }
8352
8353 /*
8354 * Verify the VMCS revision specified by the guest matches what we reported to the guest.
8355 * Verify the VMCS is not a shadow VMCS, if the VMCS shadowing feature is supported.
8356 */
8357 if ( VmcsRevId.n.u31RevisionId == VMX_V_VMCS_REVISION_ID
8358 && ( !VmcsRevId.n.fIsShadowVmcs
8359 || IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fVmxVmcsShadowing))
8360 { /* likely */ }
8361 else
8362 {
8363 if (VmcsRevId.n.u31RevisionId != VMX_V_VMCS_REVISION_ID)
8364 {
8365 Log(("vmptrld: VMCS revision mismatch, expected %#RX32 got %#RX32, GCPtrVmcs=%#RGv GCPhysVmcs=%#RGp -> VMFail()\n",
8366 VMX_V_VMCS_REVISION_ID, VmcsRevId.n.u31RevisionId, GCPtrVmcs, GCPhysVmcs));
8367 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmptrld_VmcsRevId;
8368 iemVmxVmFail(pVCpu, VMXINSTRERR_VMPTRLD_INCORRECT_VMCS_REV);
8369 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8370 return VINF_SUCCESS;
8371 }
8372
8373 Log(("vmptrld: Shadow VMCS -> VMFail()\n"));
8374 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmptrld_ShadowVmcs;
8375 iemVmxVmFail(pVCpu, VMXINSTRERR_VMPTRLD_INCORRECT_VMCS_REV);
8376 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8377 return VINF_SUCCESS;
8378 }
8379
8380 /*
8381 * We cache only the current VMCS in CPUMCTX. Therefore, VMPTRLD should always flush
8382 * the cache of an existing, current VMCS back to guest memory before loading a new,
8383 * different current VMCS.
8384 */
8385 if (IEM_VMX_GET_CURRENT_VMCS(pVCpu) != GCPhysVmcs)
8386 {
8387 if (IEM_VMX_HAS_CURRENT_VMCS(pVCpu))
8388 {
8389 iemVmxWriteCurrentVmcsToGstMem(pVCpu);
8390 IEM_VMX_CLEAR_CURRENT_VMCS(pVCpu);
8391 }
8392
8393 /* Set the new VMCS as the current VMCS and read it from guest memory. */
8394 IEM_VMX_SET_CURRENT_VMCS(pVCpu, GCPhysVmcs);
8395 rc = iemVmxReadCurrentVmcsFromGstMem(pVCpu);
8396 if (RT_SUCCESS(rc))
8397 {
8398 /* Notify HM that a new, current VMCS is loaded. */
8399 HMNotifyVmxNstGstCurrentVmcsChanged(pVCpu);
8400 }
8401 else
8402 {
8403 Log(("vmptrld: Failed to read VMCS at %#RGp, rc=%Rrc\n", GCPhysVmcs, rc));
8404 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmptrld_PtrReadPhys;
8405 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = GCPhysVmcs;
8406 return rc;
8407 }
8408 }
8409
8410 Assert(IEM_VMX_HAS_CURRENT_VMCS(pVCpu));
8411 iemVmxVmSucceed(pVCpu);
8412 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8413 return VINF_SUCCESS;
8414}
8415
8416
8417/**
8418 * INVVPID instruction execution worker.
8419 *
8420 * @returns Strict VBox status code.
8421 * @param pVCpu The cross context virtual CPU structure.
8422 * @param cbInstr The instruction length in bytes.
8423 * @param iEffSeg The segment of the invvpid descriptor.
8424 * @param GCPtrInvvpidDesc The address of invvpid descriptor.
8425 * @param u64InvvpidType The invalidation type.
8426 * @param pExitInfo Pointer to the VM-exit information. Optional, can be
8427 * NULL.
8428 *
8429 * @remarks Common VMX instruction checks are already expected to by the caller,
8430 * i.e. VMX operation, CR4.VMXE, Real/V86 mode, EFER/CS.L checks.
8431 */
8432IEM_STATIC VBOXSTRICTRC iemVmxInvvpid(PVMCPUCC pVCpu, uint8_t cbInstr, uint8_t iEffSeg, RTGCPTR GCPtrInvvpidDesc,
8433 uint64_t u64InvvpidType, PCVMXVEXITINFO pExitInfo)
8434{
8435 /* Check if INVVPID instruction is supported, otherwise raise #UD. */
8436 if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fVmxVpid)
8437 return iemRaiseUndefinedOpcode(pVCpu);
8438
8439 /* Nested-guest intercept. */
8440 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
8441 {
8442 if (pExitInfo)
8443 return iemVmxVmexitInstrWithInfo(pVCpu, pExitInfo);
8444 return iemVmxVmexitInstrNeedsInfo(pVCpu, VMX_EXIT_INVVPID, VMXINSTRID_NONE, cbInstr);
8445 }
8446
8447 /* CPL. */
8448 if (pVCpu->iem.s.uCpl != 0)
8449 {
8450 Log(("invvpid: CPL != 0 -> #GP(0)\n"));
8451 return iemRaiseGeneralProtectionFault0(pVCpu);
8452 }
8453
8454 /*
8455 * Validate INVVPID invalidation type.
8456 *
8457 * The instruction specifies exactly ONE of the supported invalidation types.
8458 *
8459 * Each of the types has a bit in IA32_VMX_EPT_VPID_CAP MSR specifying if it is
8460 * supported. In theory, it's possible for a CPU to not support flushing individual
8461 * addresses but all the other types or any other combination. We do not take any
8462 * shortcuts here by assuming the types we currently expose to the guest.
8463 */
8464 uint64_t const fCaps = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64EptVpidCaps;
8465 bool const fInvvpidSupported = RT_BF_GET(fCaps, VMX_BF_EPT_VPID_CAP_INVVPID);
8466 bool const fTypeIndivAddr = RT_BF_GET(fCaps, VMX_BF_EPT_VPID_CAP_INVVPID_INDIV_ADDR);
8467 bool const fTypeSingleCtx = RT_BF_GET(fCaps, VMX_BF_EPT_VPID_CAP_INVVPID_SINGLE_CTX);
8468 bool const fTypeAllCtx = RT_BF_GET(fCaps, VMX_BF_EPT_VPID_CAP_INVVPID_ALL_CTX);
8469 bool const fTypeSingleCtxRetainGlobals = RT_BF_GET(fCaps, VMX_BF_EPT_VPID_CAP_INVVPID_SINGLE_CTX_RETAIN_GLOBALS);
8470
8471 bool afSupportedTypes[4];
8472 afSupportedTypes[0] = fTypeIndivAddr;
8473 afSupportedTypes[1] = fTypeSingleCtx;
8474 afSupportedTypes[2] = fTypeAllCtx;
8475 afSupportedTypes[3] = fTypeSingleCtxRetainGlobals;
8476
8477 if ( fInvvpidSupported
8478 && !(u64InvvpidType & ~(uint64_t)VMX_INVVPID_VALID_MASK)
8479 && afSupportedTypes[u64InvvpidType & 3])
8480 { /* likely */ }
8481 else
8482 {
8483 Log(("invvpid: invalid/unsupported invvpid type %#x -> VMFail\n", u64InvvpidType));
8484 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Invvpid_TypeInvalid;
8485 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = u64InvvpidType;
8486 iemVmxVmFail(pVCpu, VMXINSTRERR_INVEPT_INVVPID_INVALID_OPERAND);
8487 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8488 return VINF_SUCCESS;
8489 }
8490
8491 /*
8492 * Fetch the invvpid descriptor from guest memory.
8493 */
8494 RTUINT128U uDesc;
8495 VBOXSTRICTRC rcStrict = iemMemFetchDataU128(pVCpu, &uDesc, iEffSeg, GCPtrInvvpidDesc);
8496 if (rcStrict == VINF_SUCCESS)
8497 {
8498 /*
8499 * Validate the descriptor.
8500 */
8501 if (uDesc.s.Lo > 0xfff)
8502 {
8503 Log(("invvpid: reserved bits set in invvpid descriptor %#RX64 -> #GP(0)\n", uDesc.s.Lo));
8504 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Invvpid_DescRsvd;
8505 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = uDesc.s.Lo;
8506 iemVmxVmFail(pVCpu, VMXINSTRERR_INVEPT_INVVPID_INVALID_OPERAND);
8507 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8508 return VINF_SUCCESS;
8509 }
8510
8511 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR3);
8512 RTGCUINTPTR64 const GCPtrInvAddr = uDesc.s.Hi;
8513 uint8_t const uVpid = uDesc.s.Lo & UINT64_C(0xfff);
8514 uint64_t const uCr3 = pVCpu->cpum.GstCtx.cr3;
8515 switch (u64InvvpidType)
8516 {
8517 case VMXTLBFLUSHVPID_INDIV_ADDR:
8518 {
8519 if (uVpid != 0)
8520 {
8521 if (IEM_IS_CANONICAL(GCPtrInvAddr))
8522 {
8523 /* Invalidate mappings for the linear address tagged with VPID. */
8524 /** @todo PGM support for VPID? Currently just flush everything. */
8525 PGMFlushTLB(pVCpu, uCr3, true /* fGlobal */);
8526 iemVmxVmSucceed(pVCpu);
8527 }
8528 else
8529 {
8530 Log(("invvpid: invalidation address %#RGP is not canonical -> VMFail\n", GCPtrInvAddr));
8531 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Invvpid_Type0InvalidAddr;
8532 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = GCPtrInvAddr;
8533 iemVmxVmFail(pVCpu, VMXINSTRERR_INVEPT_INVVPID_INVALID_OPERAND);
8534 }
8535 }
8536 else
8537 {
8538 Log(("invvpid: invalid VPID %#x for invalidation type %u -> VMFail\n", uVpid, u64InvvpidType));
8539 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Invvpid_Type0InvalidVpid;
8540 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = u64InvvpidType;
8541 iemVmxVmFail(pVCpu, VMXINSTRERR_INVEPT_INVVPID_INVALID_OPERAND);
8542 }
8543 break;
8544 }
8545
8546 case VMXTLBFLUSHVPID_SINGLE_CONTEXT:
8547 {
8548 if (uVpid != 0)
8549 {
8550 /* Invalidate all mappings with VPID. */
8551 /** @todo PGM support for VPID? Currently just flush everything. */
8552 PGMFlushTLB(pVCpu, uCr3, true /* fGlobal */);
8553 iemVmxVmSucceed(pVCpu);
8554 }
8555 else
8556 {
8557 Log(("invvpid: invalid VPID %#x for invalidation type %u -> VMFail\n", uVpid, u64InvvpidType));
8558 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Invvpid_Type1InvalidVpid;
8559 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = u64InvvpidType;
8560 iemVmxVmFail(pVCpu, VMXINSTRERR_INVEPT_INVVPID_INVALID_OPERAND);
8561 }
8562 break;
8563 }
8564
8565 case VMXTLBFLUSHVPID_ALL_CONTEXTS:
8566 {
8567 /* Invalidate all mappings with non-zero VPIDs. */
8568 /** @todo PGM support for VPID? Currently just flush everything. */
8569 PGMFlushTLB(pVCpu, uCr3, true /* fGlobal */);
8570 iemVmxVmSucceed(pVCpu);
8571 break;
8572 }
8573
8574 case VMXTLBFLUSHVPID_SINGLE_CONTEXT_RETAIN_GLOBALS:
8575 {
8576 if (uVpid != 0)
8577 {
8578 /* Invalidate all mappings with VPID except global translations. */
8579 /** @todo PGM support for VPID? Currently just flush everything. */
8580 PGMFlushTLB(pVCpu, uCr3, true /* fGlobal */);
8581 iemVmxVmSucceed(pVCpu);
8582 }
8583 else
8584 {
8585 Log(("invvpid: invalid VPID %#x for invalidation type %u -> VMFail\n", uVpid, u64InvvpidType));
8586 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Invvpid_Type3InvalidVpid;
8587 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = uVpid;
8588 iemVmxVmFail(pVCpu, VMXINSTRERR_INVEPT_INVVPID_INVALID_OPERAND);
8589 }
8590 break;
8591 }
8592 IEM_NOT_REACHED_DEFAULT_CASE_RET();
8593 }
8594 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8595 }
8596 return rcStrict;
8597}
8598
8599
8600#ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
8601/**
8602 * INVEPT instruction execution worker.
8603 *
8604 * @returns Strict VBox status code.
8605 * @param pVCpu The cross context virtual CPU structure.
8606 * @param cbInstr The instruction length in bytes.
8607 * @param iEffSeg The segment of the invept descriptor.
8608 * @param GCPtrInveptDesc The address of invept descriptor.
8609 * @param u64InveptType The invalidation type.
8610 * @param pExitInfo Pointer to the VM-exit information. Optional, can be
8611 * NULL.
8612 *
8613 * @remarks Common VMX instruction checks are already expected to by the caller,
8614 * i.e. VMX operation, CR4.VMXE, Real/V86 mode, EFER/CS.L checks.
8615 */
8616IEM_STATIC VBOXSTRICTRC iemVmxInvept(PVMCPUCC pVCpu, uint8_t cbInstr, uint8_t iEffSeg, RTGCPTR GCPtrInveptDesc,
8617 uint64_t u64InveptType, PCVMXVEXITINFO pExitInfo)
8618{
8619 /* Check if EPT is supported, otherwise raise #UD. */
8620 if (!IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fVmxEpt)
8621 return iemRaiseUndefinedOpcode(pVCpu);
8622
8623 /* Nested-guest intercept. */
8624 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
8625 {
8626 if (pExitInfo)
8627 return iemVmxVmexitInstrWithInfo(pVCpu, pExitInfo);
8628 return iemVmxVmexitInstrNeedsInfo(pVCpu, VMX_EXIT_INVEPT, VMXINSTRID_NONE, cbInstr);
8629 }
8630
8631 /* CPL. */
8632 if (pVCpu->iem.s.uCpl != 0)
8633 {
8634 Log(("invept: CPL != 0 -> #GP(0)\n"));
8635 return iemRaiseGeneralProtectionFault0(pVCpu);
8636 }
8637
8638 /*
8639 * Validate INVEPT invalidation type.
8640 *
8641 * The instruction specifies exactly ONE of the supported invalidation types.
8642 *
8643 * Each of the types has a bit in IA32_VMX_EPT_VPID_CAP MSR specifying if it is
8644 * supported. In theory, it's possible for a CPU to not support flushing individual
8645 * addresses but all the other types or any other combination. We do not take any
8646 * shortcuts here by assuming the types we currently expose to the guest.
8647 */
8648 uint64_t const fCaps = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64EptVpidCaps;
8649 bool const fInveptSupported = RT_BF_GET(fCaps, VMX_BF_EPT_VPID_CAP_INVEPT);
8650 bool const fTypeSingleCtx = RT_BF_GET(fCaps, VMX_BF_EPT_VPID_CAP_INVEPT_SINGLE_CTX);
8651 bool const fTypeAllCtx = RT_BF_GET(fCaps, VMX_BF_EPT_VPID_CAP_INVEPT_ALL_CTX);
8652
8653 bool afSupportedTypes[4];
8654 afSupportedTypes[0] = false;
8655 afSupportedTypes[1] = fTypeSingleCtx;
8656 afSupportedTypes[2] = fTypeAllCtx;
8657 afSupportedTypes[3] = false;
8658
8659 if ( fInveptSupported
8660 && !(u64InveptType & ~(uint64_t)VMX_INVEPT_VALID_MASK)
8661 && afSupportedTypes[u64InveptType & 3])
8662 { /* likely */ }
8663 else
8664 {
8665 Log(("invept: invalid/unsupported invvpid type %#x -> VMFail\n", u64InveptType));
8666 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Invept_TypeInvalid;
8667 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = u64InveptType;
8668 iemVmxVmFail(pVCpu, VMXINSTRERR_INVEPT_INVVPID_INVALID_OPERAND);
8669 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8670 return VINF_SUCCESS;
8671 }
8672
8673 /*
8674 * Fetch the invept descriptor from guest memory.
8675 */
8676 RTUINT128U uDesc;
8677 VBOXSTRICTRC rcStrict = iemMemFetchDataU128(pVCpu, &uDesc, iEffSeg, GCPtrInveptDesc);
8678 if (rcStrict == VINF_SUCCESS)
8679 {
8680 /*
8681 * Validate the descriptor.
8682 *
8683 * The Intel spec. does not explicit say the INVEPT instruction fails when reserved
8684 * bits in the descriptor are set, but it -does- for INVVPID. Until we test on real
8685 * hardware, it's assumed INVEPT behaves the same as INVVPID in this regard. It's
8686 * better to be strict in our emulation until proven otherwise.
8687 */
8688 if (uDesc.s.Hi)
8689 {
8690 Log(("invept: reserved bits set in invept descriptor %#RX64 -> VMFail\n", uDesc.s.Hi));
8691 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Invept_DescRsvd;
8692 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = uDesc.s.Hi;
8693 iemVmxVmFail(pVCpu, VMXINSTRERR_INVEPT_INVVPID_INVALID_OPERAND);
8694 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8695 return VINF_SUCCESS;
8696 }
8697
8698 /*
8699 * Flush TLB mappings based on the EPT type.
8700 */
8701 if (u64InveptType == VMXTLBFLUSHEPT_SINGLE_CONTEXT)
8702 {
8703 uint64_t const GCPhysEptPtr = uDesc.s.Lo;
8704 int const rc = iemVmxVmentryCheckEptPtr(pVCpu, GCPhysEptPtr, NULL /* enmDiag */);
8705 if (RT_SUCCESS(rc))
8706 { /* likely */ }
8707 else
8708 {
8709 Log(("invept: EPTP invalid %#RX64 -> VMFail\n", GCPhysEptPtr));
8710 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Invept_EptpInvalid;
8711 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = GCPhysEptPtr;
8712 iemVmxVmFail(pVCpu, VMXINSTRERR_INVEPT_INVVPID_INVALID_OPERAND);
8713 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8714 return VINF_SUCCESS;
8715 }
8716 }
8717
8718 /** @todo PGM support for EPT tags? Currently just flush everything. */
8719 IEM_CTX_ASSERT(pVCpu, CPUMCTX_EXTRN_CR3);
8720 uint64_t const uCr3 = pVCpu->cpum.GstCtx.cr3;
8721 PGMFlushTLB(pVCpu, uCr3, true /* fGlobal */);
8722
8723 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8724 }
8725
8726 return rcStrict;
8727}
8728#endif /* VBOX_WITH_NESTED_HWVIRT_VMX_EPT */
8729
8730
8731/**
8732 * VMXON instruction execution worker.
8733 *
8734 * @returns Strict VBox status code.
8735 * @param pVCpu The cross context virtual CPU structure.
8736 * @param cbInstr The instruction length in bytes.
8737 * @param iEffSeg The effective segment register to use with @a
8738 * GCPtrVmxon.
8739 * @param GCPtrVmxon The linear address of the VMXON pointer.
8740 * @param pExitInfo Pointer to the VM-exit information. Optional, can be NULL.
8741 *
8742 * @remarks Common VMX instruction checks are already expected to by the caller,
8743 * i.e. CR4.VMXE, Real/V86 mode, EFER/CS.L checks.
8744 */
8745IEM_STATIC VBOXSTRICTRC iemVmxVmxon(PVMCPUCC pVCpu, uint8_t cbInstr, uint8_t iEffSeg, RTGCPHYS GCPtrVmxon,
8746 PCVMXVEXITINFO pExitInfo)
8747{
8748 if (!IEM_VMX_IS_ROOT_MODE(pVCpu))
8749 {
8750 /* CPL. */
8751 if (pVCpu->iem.s.uCpl == 0)
8752 { /* likely */ }
8753 else
8754 {
8755 Log(("vmxon: CPL %u -> #GP(0)\n", pVCpu->iem.s.uCpl));
8756 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmxon_Cpl;
8757 return iemRaiseGeneralProtectionFault0(pVCpu);
8758 }
8759
8760 /* A20M (A20 Masked) mode. */
8761 if (PGMPhysIsA20Enabled(pVCpu))
8762 { /* likely */ }
8763 else
8764 {
8765 Log(("vmxon: A20M mode -> #GP(0)\n"));
8766 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmxon_A20M;
8767 return iemRaiseGeneralProtectionFault0(pVCpu);
8768 }
8769
8770 /* CR0. */
8771 {
8772 /* CR0 MB1 bits. */
8773 uint64_t const uCr0Fixed0 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr0Fixed0;
8774 if ((pVCpu->cpum.GstCtx.cr0 & uCr0Fixed0) == uCr0Fixed0)
8775 { /* likely */ }
8776 else
8777 {
8778 Log(("vmxon: CR0 fixed0 bits cleared -> #GP(0)\n"));
8779 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmxon_Cr0Fixed0;
8780 return iemRaiseGeneralProtectionFault0(pVCpu);
8781 }
8782
8783 /* CR0 MBZ bits. */
8784 uint64_t const uCr0Fixed1 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr0Fixed1;
8785 if (!(pVCpu->cpum.GstCtx.cr0 & ~uCr0Fixed1))
8786 { /* likely */ }
8787 else
8788 {
8789 Log(("vmxon: CR0 fixed1 bits set -> #GP(0)\n"));
8790 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmxon_Cr0Fixed1;
8791 return iemRaiseGeneralProtectionFault0(pVCpu);
8792 }
8793 }
8794
8795 /* CR4. */
8796 {
8797 /* CR4 MB1 bits. */
8798 uint64_t const uCr4Fixed0 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr4Fixed0;
8799 if ((pVCpu->cpum.GstCtx.cr4 & uCr4Fixed0) == uCr4Fixed0)
8800 { /* likely */ }
8801 else
8802 {
8803 Log(("vmxon: CR4 fixed0 bits cleared -> #GP(0)\n"));
8804 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmxon_Cr4Fixed0;
8805 return iemRaiseGeneralProtectionFault0(pVCpu);
8806 }
8807
8808 /* CR4 MBZ bits. */
8809 uint64_t const uCr4Fixed1 = pVCpu->cpum.GstCtx.hwvirt.vmx.Msrs.u64Cr4Fixed1;
8810 if (!(pVCpu->cpum.GstCtx.cr4 & ~uCr4Fixed1))
8811 { /* likely */ }
8812 else
8813 {
8814 Log(("vmxon: CR4 fixed1 bits set -> #GP(0)\n"));
8815 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmxon_Cr4Fixed1;
8816 return iemRaiseGeneralProtectionFault0(pVCpu);
8817 }
8818 }
8819
8820 /* Feature control MSR's LOCK and VMXON bits. */
8821 uint64_t const uMsrFeatCtl = CPUMGetGuestIa32FeatCtrl(pVCpu);
8822 if ((uMsrFeatCtl & (MSR_IA32_FEATURE_CONTROL_LOCK | MSR_IA32_FEATURE_CONTROL_VMXON))
8823 == (MSR_IA32_FEATURE_CONTROL_LOCK | MSR_IA32_FEATURE_CONTROL_VMXON))
8824 { /* likely */ }
8825 else
8826 {
8827 Log(("vmxon: Feature control lock bit or VMXON bit cleared -> #GP(0)\n"));
8828 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmxon_MsrFeatCtl;
8829 return iemRaiseGeneralProtectionFault0(pVCpu);
8830 }
8831
8832 /* Get the VMXON pointer from the location specified by the source memory operand. */
8833 RTGCPHYS GCPhysVmxon;
8834 VBOXSTRICTRC rcStrict = iemMemFetchDataU64(pVCpu, &GCPhysVmxon, iEffSeg, GCPtrVmxon);
8835 if (RT_LIKELY(rcStrict == VINF_SUCCESS))
8836 { /* likely */ }
8837 else
8838 {
8839 Log(("vmxon: Failed to read VMXON region physaddr from %#RGv, rc=%Rrc\n", GCPtrVmxon, VBOXSTRICTRC_VAL(rcStrict)));
8840 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmxon_PtrMap;
8841 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = GCPtrVmxon;
8842 return rcStrict;
8843 }
8844
8845 /* VMXON region pointer alignment. */
8846 if (!(GCPhysVmxon & X86_PAGE_4K_OFFSET_MASK))
8847 { /* likely */ }
8848 else
8849 {
8850 Log(("vmxon: VMXON region pointer not page-aligned -> VMFailInvalid\n"));
8851 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmxon_PtrAlign;
8852 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = GCPhysVmxon;
8853 iemVmxVmFailInvalid(pVCpu);
8854 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8855 return VINF_SUCCESS;
8856 }
8857
8858 /* VMXON physical-address width limits. */
8859 if (!(GCPhysVmxon >> IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cVmxMaxPhysAddrWidth))
8860 { /* likely */ }
8861 else
8862 {
8863 Log(("vmxon: VMXON region pointer extends beyond physical-address width -> VMFailInvalid\n"));
8864 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmxon_PtrWidth;
8865 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = GCPhysVmxon;
8866 iemVmxVmFailInvalid(pVCpu);
8867 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8868 return VINF_SUCCESS;
8869 }
8870
8871 /* Ensure VMXON region is not MMIO, ROM etc. This is not an Intel requirement but a
8872 restriction imposed by our implementation. */
8873 if (PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), GCPhysVmxon))
8874 { /* likely */ }
8875 else
8876 {
8877 Log(("vmxon: VMXON region not normal memory -> VMFailInvalid\n"));
8878 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmxon_PtrAbnormal;
8879 pVCpu->cpum.GstCtx.hwvirt.vmx.uDiagAux = GCPhysVmxon;
8880 iemVmxVmFailInvalid(pVCpu);
8881 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8882 return VINF_SUCCESS;
8883 }
8884
8885 /* Read the VMCS revision ID from the VMXON region. */
8886 VMXVMCSREVID VmcsRevId;
8887 int rc = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), &VmcsRevId, GCPhysVmxon, sizeof(VmcsRevId));
8888 if (RT_SUCCESS(rc))
8889 { /* likely */ }
8890 else
8891 {
8892 Log(("vmxon: Failed to read VMXON region at %#RGp, rc=%Rrc\n", GCPhysVmxon, rc));
8893 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmxon_PtrReadPhys;
8894 return rc;
8895 }
8896
8897 /* Verify the VMCS revision specified by the guest matches what we reported to the guest. */
8898 if (RT_LIKELY(VmcsRevId.u == VMX_V_VMCS_REVISION_ID))
8899 { /* likely */ }
8900 else
8901 {
8902 /* Revision ID mismatch. */
8903 if (!VmcsRevId.n.fIsShadowVmcs)
8904 {
8905 Log(("vmxon: VMCS revision mismatch, expected %#RX32 got %#RX32 -> VMFailInvalid\n", VMX_V_VMCS_REVISION_ID,
8906 VmcsRevId.n.u31RevisionId));
8907 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmxon_VmcsRevId;
8908 iemVmxVmFailInvalid(pVCpu);
8909 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8910 return VINF_SUCCESS;
8911 }
8912
8913 /* Shadow VMCS disallowed. */
8914 Log(("vmxon: Shadow VMCS -> VMFailInvalid\n"));
8915 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmxon_ShadowVmcs;
8916 iemVmxVmFailInvalid(pVCpu);
8917 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8918 return VINF_SUCCESS;
8919 }
8920
8921 /*
8922 * Record that we're in VMX operation, block INIT, block and disable A20M.
8923 */
8924 pVCpu->cpum.GstCtx.hwvirt.vmx.GCPhysVmxon = GCPhysVmxon;
8925 IEM_VMX_CLEAR_CURRENT_VMCS(pVCpu);
8926 pVCpu->cpum.GstCtx.hwvirt.vmx.fInVmxRootMode = true;
8927
8928 /* Clear address-range monitoring. */
8929 EMMonitorWaitClear(pVCpu);
8930 /** @todo NSTVMX: Intel PT. */
8931
8932 iemVmxVmSucceed(pVCpu);
8933 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8934 return VINF_SUCCESS;
8935 }
8936 else if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
8937 {
8938 /* Nested-guest intercept. */
8939 if (pExitInfo)
8940 return iemVmxVmexitInstrWithInfo(pVCpu, pExitInfo);
8941 return iemVmxVmexitInstrNeedsInfo(pVCpu, VMX_EXIT_VMXON, VMXINSTRID_NONE, cbInstr);
8942 }
8943
8944 Assert(IEM_VMX_IS_ROOT_MODE(pVCpu));
8945
8946 /* CPL. */
8947 if (pVCpu->iem.s.uCpl > 0)
8948 {
8949 Log(("vmxon: In VMX root mode: CPL %u -> #GP(0)\n", pVCpu->iem.s.uCpl));
8950 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmxon_VmxRootCpl;
8951 return iemRaiseGeneralProtectionFault0(pVCpu);
8952 }
8953
8954 /* VMXON when already in VMX root mode. */
8955 iemVmxVmFail(pVCpu, VMXINSTRERR_VMXON_IN_VMXROOTMODE);
8956 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmxon_VmxAlreadyRoot;
8957 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8958 return VINF_SUCCESS;
8959}
8960
8961
8962/**
8963 * Implements 'VMXOFF'.
8964 *
8965 * @remarks Common VMX instruction checks are already expected to by the caller,
8966 * i.e. CR4.VMXE, Real/V86 mode, EFER/CS.L checks.
8967 */
8968IEM_CIMPL_DEF_0(iemCImpl_vmxoff)
8969{
8970 /* Nested-guest intercept. */
8971 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
8972 return iemVmxVmexitInstr(pVCpu, VMX_EXIT_VMXOFF, cbInstr);
8973
8974 /* CPL. */
8975 if (pVCpu->iem.s.uCpl == 0)
8976 { /* likely */ }
8977 else
8978 {
8979 Log(("vmxoff: CPL %u -> #GP(0)\n", pVCpu->iem.s.uCpl));
8980 pVCpu->cpum.GstCtx.hwvirt.vmx.enmDiag = kVmxVDiag_Vmxoff_Cpl;
8981 return iemRaiseGeneralProtectionFault0(pVCpu);
8982 }
8983
8984 /* Dual monitor treatment of SMIs and SMM. */
8985 uint64_t const fSmmMonitorCtl = CPUMGetGuestIa32SmmMonitorCtl(pVCpu);
8986 if (!(fSmmMonitorCtl & MSR_IA32_SMM_MONITOR_VALID))
8987 { /* likely */ }
8988 else
8989 {
8990 iemVmxVmFail(pVCpu, VMXINSTRERR_VMXOFF_DUAL_MON);
8991 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
8992 return VINF_SUCCESS;
8993 }
8994
8995 /* Record that we're no longer in VMX root operation, block INIT, block and disable A20M. */
8996 pVCpu->cpum.GstCtx.hwvirt.vmx.fInVmxRootMode = false;
8997 Assert(!pVCpu->cpum.GstCtx.hwvirt.vmx.fInVmxNonRootMode);
8998
8999 if (fSmmMonitorCtl & MSR_IA32_SMM_MONITOR_VMXOFF_UNBLOCK_SMI)
9000 { /** @todo NSTVMX: Unblock SMI. */ }
9001
9002 EMMonitorWaitClear(pVCpu);
9003 /** @todo NSTVMX: Unblock and enable A20M. */
9004
9005 iemVmxVmSucceed(pVCpu);
9006 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
9007 return VINF_SUCCESS;
9008}
9009
9010
9011/**
9012 * Implements 'VMXON'.
9013 */
9014IEM_CIMPL_DEF_2(iemCImpl_vmxon, uint8_t, iEffSeg, RTGCPTR, GCPtrVmxon)
9015{
9016 return iemVmxVmxon(pVCpu, cbInstr, iEffSeg, GCPtrVmxon, NULL /* pExitInfo */);
9017}
9018
9019
9020/**
9021 * Implements 'VMLAUNCH'.
9022 */
9023IEM_CIMPL_DEF_0(iemCImpl_vmlaunch)
9024{
9025 return iemVmxVmlaunchVmresume(pVCpu, cbInstr, VMXINSTRID_VMLAUNCH);
9026}
9027
9028
9029/**
9030 * Implements 'VMRESUME'.
9031 */
9032IEM_CIMPL_DEF_0(iemCImpl_vmresume)
9033{
9034 return iemVmxVmlaunchVmresume(pVCpu, cbInstr, VMXINSTRID_VMRESUME);
9035}
9036
9037
9038/**
9039 * Implements 'VMPTRLD'.
9040 */
9041IEM_CIMPL_DEF_2(iemCImpl_vmptrld, uint8_t, iEffSeg, RTGCPTR, GCPtrVmcs)
9042{
9043 return iemVmxVmptrld(pVCpu, cbInstr, iEffSeg, GCPtrVmcs, NULL /* pExitInfo */);
9044}
9045
9046
9047/**
9048 * Implements 'VMPTRST'.
9049 */
9050IEM_CIMPL_DEF_2(iemCImpl_vmptrst, uint8_t, iEffSeg, RTGCPTR, GCPtrVmcs)
9051{
9052 return iemVmxVmptrst(pVCpu, cbInstr, iEffSeg, GCPtrVmcs, NULL /* pExitInfo */);
9053}
9054
9055
9056/**
9057 * Implements 'VMCLEAR'.
9058 */
9059IEM_CIMPL_DEF_2(iemCImpl_vmclear, uint8_t, iEffSeg, RTGCPTR, GCPtrVmcs)
9060{
9061 return iemVmxVmclear(pVCpu, cbInstr, iEffSeg, GCPtrVmcs, NULL /* pExitInfo */);
9062}
9063
9064
9065/**
9066 * Implements 'VMWRITE' register.
9067 */
9068IEM_CIMPL_DEF_2(iemCImpl_vmwrite_reg, uint64_t, u64Val, uint64_t, u64VmcsField)
9069{
9070 return iemVmxVmwrite(pVCpu, cbInstr, UINT8_MAX /* iEffSeg */, u64Val, u64VmcsField, NULL /* pExitInfo */);
9071}
9072
9073
9074/**
9075 * Implements 'VMWRITE' memory.
9076 */
9077IEM_CIMPL_DEF_3(iemCImpl_vmwrite_mem, uint8_t, iEffSeg, RTGCPTR, GCPtrVal, uint32_t, u64VmcsField)
9078{
9079 return iemVmxVmwrite(pVCpu, cbInstr, iEffSeg, GCPtrVal, u64VmcsField, NULL /* pExitInfo */);
9080}
9081
9082
9083/**
9084 * Implements 'VMREAD' register (64-bit).
9085 */
9086IEM_CIMPL_DEF_2(iemCImpl_vmread_reg64, uint64_t *, pu64Dst, uint64_t, u64VmcsField)
9087{
9088 return iemVmxVmreadReg64(pVCpu, cbInstr, pu64Dst, u64VmcsField, NULL /* pExitInfo */);
9089}
9090
9091
9092/**
9093 * Implements 'VMREAD' register (32-bit).
9094 */
9095IEM_CIMPL_DEF_2(iemCImpl_vmread_reg32, uint32_t *, pu32Dst, uint32_t, u32VmcsField)
9096{
9097 return iemVmxVmreadReg32(pVCpu, cbInstr, pu32Dst, u32VmcsField, NULL /* pExitInfo */);
9098}
9099
9100
9101/**
9102 * Implements 'VMREAD' memory, 64-bit register.
9103 */
9104IEM_CIMPL_DEF_3(iemCImpl_vmread_mem_reg64, uint8_t, iEffSeg, RTGCPTR, GCPtrDst, uint32_t, u64VmcsField)
9105{
9106 return iemVmxVmreadMem(pVCpu, cbInstr, iEffSeg, GCPtrDst, u64VmcsField, NULL /* pExitInfo */);
9107}
9108
9109
9110/**
9111 * Implements 'VMREAD' memory, 32-bit register.
9112 */
9113IEM_CIMPL_DEF_3(iemCImpl_vmread_mem_reg32, uint8_t, iEffSeg, RTGCPTR, GCPtrDst, uint32_t, u32VmcsField)
9114{
9115 return iemVmxVmreadMem(pVCpu, cbInstr, iEffSeg, GCPtrDst, u32VmcsField, NULL /* pExitInfo */);
9116}
9117
9118
9119/**
9120 * Implements 'INVVPID'.
9121 */
9122IEM_CIMPL_DEF_3(iemCImpl_invvpid, uint8_t, iEffSeg, RTGCPTR, GCPtrInvvpidDesc, uint64_t, uInvvpidType)
9123{
9124 return iemVmxInvvpid(pVCpu, cbInstr, iEffSeg, GCPtrInvvpidDesc, uInvvpidType, NULL /* pExitInfo */);
9125}
9126
9127
9128#ifdef VBOX_WITH_NESTED_HWVIRT_VMX_EPT
9129/**
9130 * Implements 'INVEPT'.
9131 */
9132IEM_CIMPL_DEF_3(iemCImpl_invept, uint8_t, iEffSeg, RTGCPTR, GCPtrInveptDesc, uint64_t, uInveptType)
9133{
9134 return iemVmxInvept(pVCpu, cbInstr, iEffSeg, GCPtrInveptDesc, uInveptType, NULL /* pExitInfo */);
9135}
9136#endif
9137
9138
9139/**
9140 * Implements VMX's implementation of PAUSE.
9141 */
9142IEM_CIMPL_DEF_0(iemCImpl_vmx_pause)
9143{
9144 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
9145 {
9146 VBOXSTRICTRC rcStrict = iemVmxVmexitInstrPause(pVCpu, cbInstr);
9147 if (rcStrict != VINF_VMX_INTERCEPT_NOT_ACTIVE)
9148 return rcStrict;
9149 }
9150
9151 /*
9152 * Outside VMX non-root operation or if the PAUSE instruction does not cause
9153 * a VM-exit, the instruction operates normally.
9154 */
9155 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
9156 return VINF_SUCCESS;
9157}
9158
9159#endif /* VBOX_WITH_NESTED_HWVIRT_VMX */
9160
9161
9162/**
9163 * Implements 'VMCALL'.
9164 */
9165IEM_CIMPL_DEF_0(iemCImpl_vmcall)
9166{
9167#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
9168 /* Nested-guest intercept. */
9169 if (IEM_VMX_IS_NON_ROOT_MODE(pVCpu))
9170 return iemVmxVmexitInstr(pVCpu, VMX_EXIT_VMCALL, cbInstr);
9171#endif
9172
9173 /* Join forces with vmmcall. */
9174 return IEM_CIMPL_CALL_1(iemCImpl_Hypercall, OP_VMCALL);
9175}
9176
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