VirtualBox

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

最後變更 在這個檔案從81797是 81789,由 vboxsync 提交於 5 年 前

VMM/IEM: Nested VMX: bugref:9180 format string fix in AssertMsg.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette