VirtualBox

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

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

VMM: Nested VMX: bugref:10092 EPT misconfig bits and note about assertion in iemVmxVmexit.

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