VirtualBox

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

Last change on this file since 74065 was 74065, checked in by vboxsync, 6 years ago

VMM/IEM, HM: Nested VMX: bugref:9180 vmlaunch/vmresume bits.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 112.5 KB
Line 
1/* $Id: IEMAllCImplVmxInstr.cpp.h 74065 2018-09-04 12:38:25Z vboxsync $ */
2/** @file
3 * IEM - VT-x instruction implementation.
4 */
5
6/*
7 * Copyright (C) 2011-2018 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/**
20 * Implements 'VMCALL'.
21 */
22IEM_CIMPL_DEF_0(iemCImpl_vmcall)
23{
24 /** @todo NSTVMX: intercept. */
25
26 /* Join forces with vmmcall. */
27 return IEM_CIMPL_CALL_1(iemCImpl_Hypercall, OP_VMCALL);
28}
29
30#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
31/**
32 * Map of VMCS field encodings to their virtual-VMCS structure offsets.
33 *
34 * The first array dimension is VMCS field encoding of Width OR'ed with Type and the
35 * second dimension is the Index, see VMXVMCSFIELDENC.
36 */
37uint16_t const g_aoffVmcsMap[16][VMX_V_VMCS_MAX_INDEX + 1] =
38{
39 /* VMX_VMCS_ENC_WIDTH_16BIT | VMX_VMCS_ENC_TYPE_CONTROL: */
40 {
41 /* 0 */ RT_OFFSETOF(VMXVVMCS, u16Vpid),
42 /* 1 */ RT_OFFSETOF(VMXVVMCS, u16PostIntNotifyVector),
43 /* 2 */ RT_OFFSETOF(VMXVVMCS, u16EptpIndex),
44 /* 3-10 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
45 /* 11-18 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
46 /* 19-25 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX
47 },
48 /* VMX_VMCS_ENC_WIDTH_16BIT | VMX_VMCS_ENC_TYPE_VMEXIT_INFO: */
49 {
50 /* 0-7 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
51 /* 8-15 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
52 /* 16-23 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
53 /* 24-25 */ UINT16_MAX, UINT16_MAX
54 },
55 /* VMX_VMCS_ENC_WIDTH_16BIT | VMX_VMCS_ENC_TYPE_GUEST_STATE: */
56 {
57 /* 0 */ RT_OFFSETOF(VMXVVMCS, GuestEs),
58 /* 1 */ RT_OFFSETOF(VMXVVMCS, GuestCs),
59 /* 2 */ RT_OFFSETOF(VMXVVMCS, GuestSs),
60 /* 3 */ RT_OFFSETOF(VMXVVMCS, GuestDs),
61 /* 4 */ RT_OFFSETOF(VMXVVMCS, GuestFs),
62 /* 5 */ RT_OFFSETOF(VMXVVMCS, GuestGs),
63 /* 6 */ RT_OFFSETOF(VMXVVMCS, GuestLdtr),
64 /* 7 */ RT_OFFSETOF(VMXVVMCS, GuestTr),
65 /* 8 */ RT_OFFSETOF(VMXVVMCS, u16GuestIntStatus),
66 /* 9 */ RT_OFFSETOF(VMXVVMCS, u16PmlIndex),
67 /* 10-17 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
68 /* 18-25 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX
69 },
70 /* VMX_VMCS_ENC_WIDTH_16BIT | VMX_VMCS_ENC_TYPE_HOST_STATE: */
71 {
72 /* 0 */ RT_OFFSETOF(VMXVVMCS, HostEs),
73 /* 1 */ RT_OFFSETOF(VMXVVMCS, HostCs),
74 /* 2 */ RT_OFFSETOF(VMXVVMCS, HostSs),
75 /* 3 */ RT_OFFSETOF(VMXVVMCS, HostDs),
76 /* 4 */ RT_OFFSETOF(VMXVVMCS, HostFs),
77 /* 5 */ RT_OFFSETOF(VMXVVMCS, HostGs),
78 /* 6 */ RT_OFFSETOF(VMXVVMCS, HostTr),
79 /* 7-14 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
80 /* 15-22 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
81 /* 23-25 */ UINT16_MAX, UINT16_MAX, UINT16_MAX
82 },
83 /* VMX_VMCS_ENC_WIDTH_64BIT | VMX_VMCS_ENC_TYPE_CONTROL: */
84 {
85 /* 0 */ RT_OFFSETOF(VMXVVMCS, u64AddrIoBitmapA),
86 /* 1 */ RT_OFFSETOF(VMXVVMCS, u64AddrIoBitmapB),
87 /* 2 */ RT_OFFSETOF(VMXVVMCS, u64AddrMsrBitmap),
88 /* 3 */ RT_OFFSETOF(VMXVVMCS, u64AddrExitMsrStore),
89 /* 4 */ RT_OFFSETOF(VMXVVMCS, u64AddrExitMsrLoad),
90 /* 5 */ RT_OFFSETOF(VMXVVMCS, u64AddrEntryMsrLoad),
91 /* 6 */ RT_OFFSETOF(VMXVVMCS, u64ExecVmcsPtr),
92 /* 7 */ RT_OFFSETOF(VMXVVMCS, u64AddrPml),
93 /* 8 */ RT_OFFSETOF(VMXVVMCS, u64TscOffset),
94 /* 9 */ RT_OFFSETOF(VMXVVMCS, u64AddrVirtApic),
95 /* 10 */ RT_OFFSETOF(VMXVVMCS, u64AddrApicAccess),
96 /* 11 */ RT_OFFSETOF(VMXVVMCS, u64AddrPostedIntDesc),
97 /* 12 */ RT_OFFSETOF(VMXVVMCS, u64VmFuncCtls),
98 /* 13 */ RT_OFFSETOF(VMXVVMCS, u64EptpPtr),
99 /* 14 */ RT_OFFSETOF(VMXVVMCS, u64EoiExitBitmap0),
100 /* 15 */ RT_OFFSETOF(VMXVVMCS, u64EoiExitBitmap1),
101 /* 16 */ RT_OFFSETOF(VMXVVMCS, u64EoiExitBitmap2),
102 /* 17 */ RT_OFFSETOF(VMXVVMCS, u64EoiExitBitmap3),
103 /* 18 */ RT_OFFSETOF(VMXVVMCS, u64AddrEptpList),
104 /* 19 */ RT_OFFSETOF(VMXVVMCS, u64AddrVmreadBitmap),
105 /* 20 */ RT_OFFSETOF(VMXVVMCS, u64AddrVmwriteBitmap),
106 /* 21 */ RT_OFFSETOF(VMXVVMCS, u64AddrXcptVeInfo),
107 /* 22 */ RT_OFFSETOF(VMXVVMCS, u64AddrXssBitmap),
108 /* 23 */ RT_OFFSETOF(VMXVVMCS, u64AddrEnclsBitmap),
109 /* 24 */ UINT16_MAX,
110 /* 25 */ RT_OFFSETOF(VMXVVMCS, u64TscMultiplier)
111 },
112 /* VMX_VMCS_ENC_WIDTH_64BIT | VMX_VMCS_ENC_TYPE_VMEXIT_INFO: */
113 {
114 /* 0 */ RT_OFFSETOF(VMXVVMCS, u64GuestPhysAddr),
115 /* 1-8 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
116 /* 9-16 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
117 /* 17-24 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
118 /* 25 */ UINT16_MAX
119 },
120 /* VMX_VMCS_ENC_WIDTH_64BIT | VMX_VMCS_ENC_TYPE_GUEST_STATE: */
121 {
122 /* 0 */ RT_OFFSETOF(VMXVVMCS, u64VmcsLinkPtr),
123 /* 1 */ RT_OFFSETOF(VMXVVMCS, u64GuestDebugCtlMsr),
124 /* 2 */ RT_OFFSETOF(VMXVVMCS, u64GuestPatMsr),
125 /* 3 */ RT_OFFSETOF(VMXVVMCS, u64GuestEferMsr),
126 /* 4 */ RT_OFFSETOF(VMXVVMCS, u64GuestPerfGlobalCtlMsr),
127 /* 5 */ RT_OFFSETOF(VMXVVMCS, u64GuestPdpte0),
128 /* 6 */ RT_OFFSETOF(VMXVVMCS, u64GuestPdpte1),
129 /* 7 */ RT_OFFSETOF(VMXVVMCS, u64GuestPdpte2),
130 /* 8 */ RT_OFFSETOF(VMXVVMCS, u64GuestPdpte3),
131 /* 9 */ RT_OFFSETOF(VMXVVMCS, u64GuestBndcfgsMsr),
132 /* 10-17 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
133 /* 18-25 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX
134 },
135 /* VMX_VMCS_ENC_WIDTH_64BIT | VMX_VMCS_ENC_TYPE_HOST_STATE: */
136 {
137 /* 0 */ RT_OFFSETOF(VMXVVMCS, u64HostPatMsr),
138 /* 1 */ RT_OFFSETOF(VMXVVMCS, u64HostEferMsr),
139 /* 2 */ RT_OFFSETOF(VMXVVMCS, u64HostPerfGlobalCtlMsr),
140 /* 3-10 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
141 /* 11-18 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
142 /* 19-25 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX
143 },
144 /* VMX_VMCS_ENC_WIDTH_32BIT | VMX_VMCS_ENC_TYPE_CONTROL: */
145 {
146 /* 0 */ RT_OFFSETOF(VMXVVMCS, u32PinCtls),
147 /* 1 */ RT_OFFSETOF(VMXVVMCS, u32ProcCtls),
148 /* 2 */ RT_OFFSETOF(VMXVVMCS, u32XcptBitmap),
149 /* 3 */ RT_OFFSETOF(VMXVVMCS, u32XcptPFMask),
150 /* 4 */ RT_OFFSETOF(VMXVVMCS, u32XcptPFMatch),
151 /* 5 */ RT_OFFSETOF(VMXVVMCS, u32Cr3TargetCount),
152 /* 6 */ RT_OFFSETOF(VMXVVMCS, u32ExitCtls),
153 /* 7 */ RT_OFFSETOF(VMXVVMCS, u32ExitMsrStoreCount),
154 /* 8 */ RT_OFFSETOF(VMXVVMCS, u32ExitMsrLoadCount),
155 /* 9 */ RT_OFFSETOF(VMXVVMCS, u32EntryCtls),
156 /* 10 */ RT_OFFSETOF(VMXVVMCS, u32EntryMsrLoadCount),
157 /* 11 */ RT_OFFSETOF(VMXVVMCS, u32EntryIntInfo),
158 /* 12 */ RT_OFFSETOF(VMXVVMCS, u32EntryXcptErrCode),
159 /* 13 */ RT_OFFSETOF(VMXVVMCS, u32EntryInstrLen),
160 /* 14 */ RT_OFFSETOF(VMXVVMCS, u32TprThreshold),
161 /* 15 */ RT_OFFSETOF(VMXVVMCS, u32ProcCtls2),
162 /* 16 */ RT_OFFSETOF(VMXVVMCS, u32PleGap),
163 /* 17 */ RT_OFFSETOF(VMXVVMCS, u32PleWindow),
164 /* 18-25 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX
165 },
166 /* VMX_VMCS_ENC_WIDTH_32BIT | VMX_VMCS_ENC_TYPE_VMEXIT_INFO: */
167 {
168 /* 0 */ RT_OFFSETOF(VMXVVMCS, u32RoVmInstrError),
169 /* 1 */ RT_OFFSETOF(VMXVVMCS, u32RoExitReason),
170 /* 2 */ RT_OFFSETOF(VMXVVMCS, u32RoExitIntInfo),
171 /* 3 */ RT_OFFSETOF(VMXVVMCS, u32RoExitErrCode),
172 /* 4 */ RT_OFFSETOF(VMXVVMCS, u32RoIdtVectoringInfo),
173 /* 5 */ RT_OFFSETOF(VMXVVMCS, u32RoIdtVectoringErrCode),
174 /* 6 */ RT_OFFSETOF(VMXVVMCS, u32RoExitInstrLen),
175 /* 7 */ RT_OFFSETOF(VMXVVMCS, u32RoExitInstrInfo),
176 /* 8-15 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
177 /* 16-23 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
178 /* 24-25 */ UINT16_MAX, UINT16_MAX
179 },
180 /* VMX_VMCS_ENC_WIDTH_32BIT | VMX_VMCS_ENC_TYPE_GUEST_STATE: */
181 {
182 /* 0 */ RT_OFFSETOF(VMXVVMCS, u32GuestEsLimit),
183 /* 1 */ RT_OFFSETOF(VMXVVMCS, u32GuestCsLimit),
184 /* 2 */ RT_OFFSETOF(VMXVVMCS, u32GuestSsLimit),
185 /* 3 */ RT_OFFSETOF(VMXVVMCS, u32GuestDsLimit),
186 /* 4 */ RT_OFFSETOF(VMXVVMCS, u32GuestEsLimit),
187 /* 5 */ RT_OFFSETOF(VMXVVMCS, u32GuestFsLimit),
188 /* 6 */ RT_OFFSETOF(VMXVVMCS, u32GuestGsLimit),
189 /* 7 */ RT_OFFSETOF(VMXVVMCS, u32GuestLdtrLimit),
190 /* 8 */ RT_OFFSETOF(VMXVVMCS, u32GuestTrLimit),
191 /* 9 */ RT_OFFSETOF(VMXVVMCS, u32GuestGdtrLimit),
192 /* 10 */ RT_OFFSETOF(VMXVVMCS, u32GuestIdtrLimit),
193 /* 11 */ RT_OFFSETOF(VMXVVMCS, u32GuestEsAttr),
194 /* 12 */ RT_OFFSETOF(VMXVVMCS, u32GuestCsAttr),
195 /* 13 */ RT_OFFSETOF(VMXVVMCS, u32GuestSsAttr),
196 /* 14 */ RT_OFFSETOF(VMXVVMCS, u32GuestDsAttr),
197 /* 15 */ RT_OFFSETOF(VMXVVMCS, u32GuestFsAttr),
198 /* 16 */ RT_OFFSETOF(VMXVVMCS, u32GuestGsAttr),
199 /* 17 */ RT_OFFSETOF(VMXVVMCS, u32GuestLdtrAttr),
200 /* 18 */ RT_OFFSETOF(VMXVVMCS, u32GuestTrAttr),
201 /* 19 */ RT_OFFSETOF(VMXVVMCS, u32GuestIntrState),
202 /* 20 */ RT_OFFSETOF(VMXVVMCS, u32GuestActivityState),
203 /* 21 */ RT_OFFSETOF(VMXVVMCS, u32GuestSmBase),
204 /* 22 */ RT_OFFSETOF(VMXVVMCS, u32GuestSysenterCS),
205 /* 23 */ RT_OFFSETOF(VMXVVMCS, u32PreemptTimer),
206 /* 24-25 */ UINT16_MAX, UINT16_MAX
207 },
208 /* VMX_VMCS_ENC_WIDTH_32BIT | VMX_VMCS_ENC_TYPE_HOST_STATE: */
209 {
210 /* 0 */ RT_OFFSETOF(VMXVVMCS, u32HostSysenterCs),
211 /* 1-8 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
212 /* 9-16 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
213 /* 17-24 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
214 /* 25 */ UINT16_MAX
215 },
216 /* VMX_VMCS_ENC_WIDTH_NATURAL | VMX_VMCS_ENC_TYPE_CONTROL: */
217 {
218 /* 0 */ RT_OFFSETOF(VMXVVMCS, u64Cr0Mask),
219 /* 1 */ RT_OFFSETOF(VMXVVMCS, u64Cr4Mask),
220 /* 2 */ RT_OFFSETOF(VMXVVMCS, u64Cr0ReadShadow),
221 /* 3 */ RT_OFFSETOF(VMXVVMCS, u64Cr4ReadShadow),
222 /* 4 */ RT_OFFSETOF(VMXVVMCS, u64Cr3Target0),
223 /* 5 */ RT_OFFSETOF(VMXVVMCS, u64Cr3Target1),
224 /* 6 */ RT_OFFSETOF(VMXVVMCS, u64Cr3Target2),
225 /* 7 */ RT_OFFSETOF(VMXVVMCS, u64Cr3Target3),
226 /* 8-15 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
227 /* 16-23 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
228 /* 24-25 */ UINT16_MAX, UINT16_MAX
229 },
230 /* VMX_VMCS_ENC_WIDTH_NATURAL | VMX_VMCS_ENC_TYPE_VMEXIT_INFO: */
231 {
232 /* 0 */ RT_OFFSETOF(VMXVVMCS, u64ExitQual),
233 /* 1 */ RT_OFFSETOF(VMXVVMCS, u64IoRcx),
234 /* 2 */ RT_OFFSETOF(VMXVVMCS, u64IoRsi),
235 /* 3 */ RT_OFFSETOF(VMXVVMCS, u64IoRdi),
236 /* 4 */ RT_OFFSETOF(VMXVVMCS, u64IoRip),
237 /* 5 */ RT_OFFSETOF(VMXVVMCS, u64GuestLinearAddr),
238 /* 6-13 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
239 /* 14-21 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
240 /* 22-25 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX
241 },
242 /* VMX_VMCS_ENC_WIDTH_NATURAL | VMX_VMCS_ENC_TYPE_GUEST_STATE: */
243 {
244 /* 0 */ RT_OFFSETOF(VMXVVMCS, u64GuestCr0),
245 /* 1 */ RT_OFFSETOF(VMXVVMCS, u64GuestCr3),
246 /* 2 */ RT_OFFSETOF(VMXVVMCS, u64GuestCr4),
247 /* 3 */ RT_OFFSETOF(VMXVVMCS, u64GuestEsBase),
248 /* 4 */ RT_OFFSETOF(VMXVVMCS, u64GuestCsBase),
249 /* 5 */ RT_OFFSETOF(VMXVVMCS, u64GuestSsBase),
250 /* 6 */ RT_OFFSETOF(VMXVVMCS, u64GuestDsBase),
251 /* 7 */ RT_OFFSETOF(VMXVVMCS, u64GuestFsBase),
252 /* 8 */ RT_OFFSETOF(VMXVVMCS, u64GuestGsBase),
253 /* 9 */ RT_OFFSETOF(VMXVVMCS, u64GuestLdtrBase),
254 /* 10 */ RT_OFFSETOF(VMXVVMCS, u64GuestTrBase),
255 /* 11 */ RT_OFFSETOF(VMXVVMCS, u64GuestGdtrBase),
256 /* 12 */ RT_OFFSETOF(VMXVVMCS, u64GuestIdtrBase),
257 /* 13 */ RT_OFFSETOF(VMXVVMCS, u64GuestDr7),
258 /* 14 */ RT_OFFSETOF(VMXVVMCS, u64GuestRsp),
259 /* 15 */ RT_OFFSETOF(VMXVVMCS, u64GuestRip),
260 /* 16 */ RT_OFFSETOF(VMXVVMCS, u64GuestRFlags),
261 /* 17 */ RT_OFFSETOF(VMXVVMCS, u64GuestPendingDbgXcpt),
262 /* 18 */ RT_OFFSETOF(VMXVVMCS, u64GuestSysenterEsp),
263 /* 19 */ RT_OFFSETOF(VMXVVMCS, u64GuestSysenterEip),
264 /* 20-25 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX
265 },
266 /* VMX_VMCS_ENC_WIDTH_NATURAL | VMX_VMCS_ENC_TYPE_HOST_STATE: */
267 {
268 /* 0 */ RT_OFFSETOF(VMXVVMCS, u64HostCr0),
269 /* 1 */ RT_OFFSETOF(VMXVVMCS, u64HostCr3),
270 /* 2 */ RT_OFFSETOF(VMXVVMCS, u64HostCr4),
271 /* 3 */ RT_OFFSETOF(VMXVVMCS, u64HostFsBase),
272 /* 4 */ RT_OFFSETOF(VMXVVMCS, u64HostGsBase),
273 /* 5 */ RT_OFFSETOF(VMXVVMCS, u64HostTrBase),
274 /* 6 */ RT_OFFSETOF(VMXVVMCS, u64HostGdtrBase),
275 /* 7 */ RT_OFFSETOF(VMXVVMCS, u64HostIdtrBase),
276 /* 8 */ RT_OFFSETOF(VMXVVMCS, u64HostSysenterEsp),
277 /* 9 */ RT_OFFSETOF(VMXVVMCS, u64HostSysenterEip),
278 /* 10 */ RT_OFFSETOF(VMXVVMCS, u64HostRsp),
279 /* 11 */ RT_OFFSETOF(VMXVVMCS, u64HostRip),
280 /* 12-19 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX,
281 /* 20-25 */ UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX, UINT16_MAX
282 }
283};
284
285
286/**
287 * Gets the ModR/M, SIB and displacement byte(s) from decoded opcodes given their
288 * relative offsets.
289 */
290# ifdef IEM_WITH_CODE_TLB
291# define IEM_MODRM_GET_U8(a_pVCpu, a_bModRm, a_offModRm) do { } while (0)
292# define IEM_SIB_GET_U8(a_pVCpu, a_bSib, a_offSib) do { } while (0)
293# define IEM_DISP_GET_U16(a_pVCpu, a_u16Disp, a_offDisp) do { } while (0)
294# define IEM_DISP_GET_S8_SX_U16(a_pVCpu, a_u16Disp, a_offDisp) do { } while (0)
295# define IEM_DISP_GET_U32(a_pVCpu, a_u32Disp, a_offDisp) do { } while (0)
296# define IEM_DISP_GET_S8_SX_U32(a_pVCpu, a_u32Disp, a_offDisp) do { } while (0)
297# define IEM_DISP_GET_S32_SX_U64(a_pVCpu, a_u64Disp, a_offDisp) do { } while (0)
298# define IEM_DISP_GET_S8_SX_U64(a_pVCpu, a_u64Disp, a_offDisp) do { } while (0)
299# error "Implement me: Getting ModR/M, SIB, displacement needs to work even when instruction crosses a page boundary."
300# else /* !IEM_WITH_CODE_TLB */
301# define IEM_MODRM_GET_U8(a_pVCpu, a_bModRm, a_offModRm) \
302 do \
303 { \
304 Assert((a_offModRm) < (a_pVCpu)->iem.s.cbOpcode); \
305 (a_bModRm) = (a_pVCpu)->iem.s.abOpcode[(a_offModRm)]; \
306 } while (0)
307
308# define IEM_SIB_GET_U8(a_pVCpu, a_bSib, a_offSib) IEM_MODRM_GET_U8(a_pVCpu, a_bSib, a_offSib)
309
310# define IEM_DISP_GET_U16(a_pVCpu, a_u16Disp, a_offDisp) \
311 do \
312 { \
313 Assert((a_offDisp) + 1 < (a_pVCpu)->iem.s.cbOpcode); \
314 uint8_t const bTmpLo = (a_pVCpu)->iem.s.abOpcode[(a_offDisp)]; \
315 uint8_t const bTmpHi = (a_pVCpu)->iem.s.abOpcode[(a_offDisp) + 1]; \
316 (a_u16Disp) = RT_MAKE_U16(bTmpLo, bTmpHi); \
317 } while (0)
318
319# define IEM_DISP_GET_S8_SX_U16(a_pVCpu, a_u16Disp, a_offDisp) \
320 do \
321 { \
322 Assert((a_offDisp) < (a_pVCpu)->iem.s.cbOpcode); \
323 (a_u16Disp) = (int8_t)((a_pVCpu)->iem.s.abOpcode[(a_offDisp)]); \
324 } while (0)
325
326# define IEM_DISP_GET_U32(a_pVCpu, a_u32Disp, a_offDisp) \
327 do \
328 { \
329 Assert((a_offDisp) + 3 < (a_pVCpu)->iem.s.cbOpcode); \
330 uint8_t const bTmp0 = (a_pVCpu)->iem.s.abOpcode[(a_offDisp)]; \
331 uint8_t const bTmp1 = (a_pVCpu)->iem.s.abOpcode[(a_offDisp) + 1]; \
332 uint8_t const bTmp2 = (a_pVCpu)->iem.s.abOpcode[(a_offDisp) + 2]; \
333 uint8_t const bTmp3 = (a_pVCpu)->iem.s.abOpcode[(a_offDisp) + 3]; \
334 (a_u32Disp) = RT_MAKE_U32_FROM_U8(bTmp0, bTmp1, bTmp2, bTmp3); \
335 } while (0)
336
337# define IEM_DISP_GET_S8_SX_U32(a_pVCpu, a_u32Disp, a_offDisp) \
338 do \
339 { \
340 Assert((a_offDisp) + 1 < (a_pVCpu)->iem.s.cbOpcode); \
341 (a_u32Disp) = (int8_t)((a_pVCpu)->iem.s.abOpcode[(a_offDisp)]); \
342 } while (0)
343
344# define IEM_DISP_GET_S8_SX_U64(a_pVCpu, a_u64Disp, a_offDisp) \
345 do \
346 { \
347 Assert((a_offDisp) + 1 < (a_pVCpu)->iem.s.cbOpcode); \
348 (a_u64Disp) = (int8_t)((a_pVCpu)->iem.s.abOpcode[(a_offDisp)]); \
349 } while (0)
350
351# define IEM_DISP_GET_S32_SX_U64(a_pVCpu, a_u64Disp, a_offDisp) \
352 do \
353 { \
354 Assert((a_offDisp) + 3 < (a_pVCpu)->iem.s.cbOpcode); \
355 uint8_t const bTmp0 = (a_pVCpu)->iem.s.abOpcode[(a_offDisp)]; \
356 uint8_t const bTmp1 = (a_pVCpu)->iem.s.abOpcode[(a_offDisp) + 1]; \
357 uint8_t const bTmp2 = (a_pVCpu)->iem.s.abOpcode[(a_offDisp) + 2]; \
358 uint8_t const bTmp3 = (a_pVCpu)->iem.s.abOpcode[(a_offDisp) + 3]; \
359 (a_u64Disp) = (int32_t)RT_MAKE_U32_FROM_U8(bTmp0, bTmp1, bTmp2, bTmp3); \
360 } while (0)
361# endif /* !IEM_WITH_CODE_TLB */
362
363/** Whether a shadow VMCS is present for the given VCPU. */
364#define IEM_VMX_HAS_SHADOW_VMCS(a_pVCpu) RT_BOOL(IEM_VMX_GET_SHADOW_VMCS(a_pVCpu) != NIL_RTGCPHYS)
365
366/** Gets the guest-physical address of the shadows VMCS for the given VCPU. */
367#define IEM_VMX_GET_SHADOW_VMCS(a_pVCpu) ((a_pVCpu)->cpum.GstCtx.hwvirt.vmx.CTX_SUFF(pVmcs)->u64VmcsLinkPtr.u)
368
369/** Whether a current VMCS is present for the given VCPU. */
370#define IEM_VMX_HAS_CURRENT_VMCS(a_pVCpu) RT_BOOL(IEM_VMX_GET_CURRENT_VMCS(a_pVCpu) != NIL_RTGCPHYS)
371
372/** Gets the guest-physical address of the current VMCS for the given VCPU. */
373#define IEM_VMX_GET_CURRENT_VMCS(a_pVCpu) ((a_pVCpu)->cpum.GstCtx.hwvirt.vmx.GCPhysVmcs)
374
375/** Assigns the guest-physical address of the current VMCS for the given VCPU. */
376#define IEM_VMX_SET_CURRENT_VMCS(a_pVCpu, a_GCPhysVmcs) \
377 do \
378 { \
379 Assert((a_GCPhysVmcs) != NIL_RTGCPHYS); \
380 (a_pVCpu)->cpum.GstCtx.hwvirt.vmx.GCPhysVmcs = (a_GCPhysVmcs); \
381 } while (0)
382
383/** Clears any current VMCS for the given VCPU. */
384#define IEM_VMX_CLEAR_CURRENT_VMCS(a_pVCpu) \
385 do \
386 { \
387 (a_pVCpu)->cpum.GstCtx.hwvirt.vmx.GCPhysVmcs = NIL_RTGCPHYS; \
388 } while (0)
389
390/** Check the common VMX instruction preconditions.
391 * @note Any changes here, also check if IEMOP_HLP_VMX_INSTR needs updating.
392 */
393#define IEM_VMX_INSTR_CHECKS(a_pVCpu, a_szInstr, a_InsDiagPrefix) \
394 do { \
395 if ( !IEM_IS_REAL_OR_V86_MODE(a_pVCpu) \
396 && ( !IEM_IS_LONG_MODE(a_pVCpu) \
397 || IEM_IS_64BIT_CODE(a_pVCpu))) \
398 { /* likely */ } \
399 else \
400 { \
401 if (IEM_IS_REAL_OR_V86_MODE(a_pVCpu)) \
402 { \
403 Log((a_szInstr ": Real or v8086 mode -> #UD\n")); \
404 (a_pVCpu)->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = a_InsDiagPrefix##_RealOrV86Mode; \
405 return iemRaiseUndefinedOpcode(a_pVCpu); \
406 } \
407 if (IEM_IS_LONG_MODE(a_pVCpu) && !IEM_IS_64BIT_CODE(a_pVCpu)) \
408 { \
409 Log((a_szInstr ": Long mode without 64-bit code segment -> #UD\n")); \
410 (a_pVCpu)->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = a_InsDiagPrefix##_LongModeCS; \
411 return iemRaiseUndefinedOpcode(a_pVCpu); \
412 } \
413 } \
414 } while (0)
415
416/** Check for VMX instructions requiring to be in VMX operation.
417 * @note Any changes here, check if IEMOP_HLP_IN_VMX_OPERATION needs udpating. */
418#define IEM_VMX_IN_VMX_OPERATION(a_pVCpu, a_szInstr, a_InsDiagPrefix) \
419 do \
420 { \
421 if (IEM_IS_VMX_ROOT_MODE(a_pVCpu)) \
422 { /* likely */ } \
423 else \
424 { \
425 Log((a_szInstr ": Not in VMX operation (root mode) -> #UD\n")); \
426 (a_pVCpu)->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = a_InsDiagPrefix##_VmxRoot; \
427 return iemRaiseUndefinedOpcode(a_pVCpu); \
428 } \
429 } while (0)
430
431
432/**
433 * Returns whether the given VMCS field is valid and supported by our emulation.
434 *
435 * @param pVCpu The cross context virtual CPU structure.
436 * @param u64FieldEnc The VMCS field encoding.
437 *
438 * @remarks This takes into account the CPU features exposed to the guest.
439 */
440IEM_STATIC bool iemVmxIsVmcsFieldValid(PVMCPU pVCpu, uint64_t u64FieldEnc)
441{
442 uint32_t const uFieldEncHi = RT_HI_U32(u64FieldEnc);
443 uint32_t const uFieldEncLo = RT_LO_U32(u64FieldEnc);
444 if (!uFieldEncHi)
445 { /* likely */ }
446 else
447 return false;
448
449 PCCPUMFEATURES pFeat = IEM_GET_GUEST_CPU_FEATURES(pVCpu);
450 switch (uFieldEncLo)
451 {
452 /*
453 * 16-bit fields.
454 */
455 /* Control fields. */
456 case VMX_VMCS16_VPID: return pFeat->fVmxVpid;
457 case VMX_VMCS16_POSTED_INT_NOTIFY_VECTOR: return pFeat->fVmxPostedInt;
458 case VMX_VMCS16_EPTP_INDEX: return pFeat->fVmxEptXcptVe;
459
460 /* Guest-state fields. */
461 case VMX_VMCS16_GUEST_ES_SEL:
462 case VMX_VMCS16_GUEST_CS_SEL:
463 case VMX_VMCS16_GUEST_SS_SEL:
464 case VMX_VMCS16_GUEST_DS_SEL:
465 case VMX_VMCS16_GUEST_FS_SEL:
466 case VMX_VMCS16_GUEST_GS_SEL:
467 case VMX_VMCS16_GUEST_LDTR_SEL:
468 case VMX_VMCS16_GUEST_TR_SEL:
469 case VMX_VMCS16_GUEST_INTR_STATUS: return pFeat->fVmxVirtIntDelivery;
470 case VMX_VMCS16_GUEST_PML_INDEX: return pFeat->fVmxPml;
471
472 /* Host-state fields. */
473 case VMX_VMCS16_HOST_ES_SEL:
474 case VMX_VMCS16_HOST_CS_SEL:
475 case VMX_VMCS16_HOST_SS_SEL:
476 case VMX_VMCS16_HOST_DS_SEL:
477 case VMX_VMCS16_HOST_FS_SEL:
478 case VMX_VMCS16_HOST_GS_SEL:
479 case VMX_VMCS16_HOST_TR_SEL: return true;
480
481 /*
482 * 64-bit fields.
483 */
484 /* Control fields. */
485 case VMX_VMCS64_CTRL_IO_BITMAP_A_FULL:
486 case VMX_VMCS64_CTRL_IO_BITMAP_A_HIGH:
487 case VMX_VMCS64_CTRL_IO_BITMAP_B_FULL:
488 case VMX_VMCS64_CTRL_IO_BITMAP_B_HIGH: return pFeat->fVmxUseIoBitmaps;
489 case VMX_VMCS64_CTRL_MSR_BITMAP_FULL:
490 case VMX_VMCS64_CTRL_MSR_BITMAP_HIGH: return pFeat->fVmxUseMsrBitmaps;
491 case VMX_VMCS64_CTRL_EXIT_MSR_STORE_FULL:
492 case VMX_VMCS64_CTRL_EXIT_MSR_STORE_HIGH:
493 case VMX_VMCS64_CTRL_EXIT_MSR_LOAD_FULL:
494 case VMX_VMCS64_CTRL_EXIT_MSR_LOAD_HIGH:
495 case VMX_VMCS64_CTRL_ENTRY_MSR_LOAD_FULL:
496 case VMX_VMCS64_CTRL_ENTRY_MSR_LOAD_HIGH:
497 case VMX_VMCS64_CTRL_EXEC_VMCS_PTR_FULL:
498 case VMX_VMCS64_CTRL_EXEC_VMCS_PTR_HIGH: return true;
499 case VMX_VMCS64_CTRL_EXEC_PML_ADDR_FULL:
500 case VMX_VMCS64_CTRL_EXEC_PML_ADDR_HIGH: return pFeat->fVmxPml;
501 case VMX_VMCS64_CTRL_TSC_OFFSET_FULL:
502 case VMX_VMCS64_CTRL_TSC_OFFSET_HIGH: return true;
503 case VMX_VMCS64_CTRL_VIRT_APIC_PAGEADDR_FULL:
504 case VMX_VMCS64_CTRL_VIRT_APIC_PAGEADDR_HIGH: return pFeat->fVmxUseTprShadow;
505 case VMX_VMCS64_CTRL_APIC_ACCESSADDR_FULL:
506 case VMX_VMCS64_CTRL_APIC_ACCESSADDR_HIGH: return pFeat->fVmxVirtApicAccess;
507 case VMX_VMCS64_CTRL_POSTED_INTR_DESC_FULL:
508 case VMX_VMCS64_CTRL_POSTED_INTR_DESC_HIGH: return pFeat->fVmxPostedInt;
509 case VMX_VMCS64_CTRL_VMFUNC_CTRLS_FULL:
510 case VMX_VMCS64_CTRL_VMFUNC_CTRLS_HIGH: return pFeat->fVmxVmFunc;
511 case VMX_VMCS64_CTRL_EPTP_FULL:
512 case VMX_VMCS64_CTRL_EPTP_HIGH: return pFeat->fVmxEpt;
513 case VMX_VMCS64_CTRL_EOI_BITMAP_0_FULL:
514 case VMX_VMCS64_CTRL_EOI_BITMAP_0_HIGH:
515 case VMX_VMCS64_CTRL_EOI_BITMAP_1_FULL:
516 case VMX_VMCS64_CTRL_EOI_BITMAP_1_HIGH:
517 case VMX_VMCS64_CTRL_EOI_BITMAP_2_FULL:
518 case VMX_VMCS64_CTRL_EOI_BITMAP_2_HIGH:
519 case VMX_VMCS64_CTRL_EOI_BITMAP_3_FULL:
520 case VMX_VMCS64_CTRL_EOI_BITMAP_3_HIGH: return pFeat->fVmxVirtIntDelivery;
521 case VMX_VMCS64_CTRL_EPTP_LIST_FULL:
522 case VMX_VMCS64_CTRL_EPTP_LIST_HIGH:
523 {
524 uint64_t const uVmFuncMsr = CPUMGetGuestIa32VmxVmFunc(pVCpu);
525 return RT_BOOL(RT_BF_GET(uVmFuncMsr, VMX_BF_VMFUNC_EPTP_SWITCHING));
526 }
527 case VMX_VMCS64_CTRL_VMREAD_BITMAP_FULL:
528 case VMX_VMCS64_CTRL_VMREAD_BITMAP_HIGH:
529 case VMX_VMCS64_CTRL_VMWRITE_BITMAP_FULL:
530 case VMX_VMCS64_CTRL_VMWRITE_BITMAP_HIGH: return pFeat->fVmxVmcsShadowing;
531 case VMX_VMCS64_CTRL_VIRTXCPT_INFO_ADDR_FULL:
532 case VMX_VMCS64_CTRL_VIRTXCPT_INFO_ADDR_HIGH: return pFeat->fVmxEptXcptVe;
533 case VMX_VMCS64_CTRL_XSS_EXITING_BITMAP_FULL:
534 case VMX_VMCS64_CTRL_XSS_EXITING_BITMAP_HIGH: return pFeat->fVmxXsavesXrstors;
535 case VMX_VMCS64_CTRL_ENCLS_EXITING_BITMAP_FULL:
536 case VMX_VMCS64_CTRL_ENCLS_EXITING_BITMAP_HIGH: return false;
537 case VMX_VMCS64_CTRL_TSC_MULTIPLIER_FULL:
538 case VMX_VMCS64_CTRL_TSC_MULTIPLIER_HIGH: return pFeat->fVmxUseTscScaling;
539
540 /* Read-only data fields. */
541 case VMX_VMCS64_RO_GUEST_PHYS_ADDR_FULL:
542 case VMX_VMCS64_RO_GUEST_PHYS_ADDR_HIGH: return pFeat->fVmxEpt;
543
544 /* Guest-state fields. */
545 case VMX_VMCS64_GUEST_VMCS_LINK_PTR_FULL:
546 case VMX_VMCS64_GUEST_VMCS_LINK_PTR_HIGH:
547 case VMX_VMCS64_GUEST_DEBUGCTL_FULL:
548 case VMX_VMCS64_GUEST_DEBUGCTL_HIGH: return true;
549 case VMX_VMCS64_GUEST_PAT_FULL:
550 case VMX_VMCS64_GUEST_PAT_HIGH: return pFeat->fVmxEntryLoadPatMsr || pFeat->fVmxExitSavePatMsr;
551 case VMX_VMCS64_GUEST_EFER_FULL:
552 case VMX_VMCS64_GUEST_EFER_HIGH: return pFeat->fVmxEntryLoadEferMsr || pFeat->fVmxExitSaveEferMsr;
553 case VMX_VMCS64_GUEST_PERF_GLOBAL_CTRL_FULL:
554 case VMX_VMCS64_GUEST_PERF_GLOBAL_CTRL_HIGH: return false;
555 case VMX_VMCS64_GUEST_PDPTE0_FULL:
556 case VMX_VMCS64_GUEST_PDPTE0_HIGH:
557 case VMX_VMCS64_GUEST_PDPTE1_FULL:
558 case VMX_VMCS64_GUEST_PDPTE1_HIGH:
559 case VMX_VMCS64_GUEST_PDPTE2_FULL:
560 case VMX_VMCS64_GUEST_PDPTE2_HIGH:
561 case VMX_VMCS64_GUEST_PDPTE3_FULL:
562 case VMX_VMCS64_GUEST_PDPTE3_HIGH: return pFeat->fVmxEpt;
563 case VMX_VMCS64_GUEST_BNDCFGS_FULL:
564 case VMX_VMCS64_GUEST_BNDCFGS_HIGH: return false;
565
566 /* Host-state fields. */
567 case VMX_VMCS64_HOST_PAT_FULL:
568 case VMX_VMCS64_HOST_PAT_HIGH: return pFeat->fVmxExitLoadPatMsr;
569 case VMX_VMCS64_HOST_EFER_FULL:
570 case VMX_VMCS64_HOST_EFER_HIGH: return pFeat->fVmxExitLoadEferMsr;
571 case VMX_VMCS64_HOST_PERF_GLOBAL_CTRL_FULL:
572 case VMX_VMCS64_HOST_PERF_GLOBAL_CTRL_HIGH: return false;
573
574 /*
575 * 32-bit fields.
576 */
577 /* Control fields. */
578 case VMX_VMCS32_CTRL_PIN_EXEC:
579 case VMX_VMCS32_CTRL_PROC_EXEC:
580 case VMX_VMCS32_CTRL_EXCEPTION_BITMAP:
581 case VMX_VMCS32_CTRL_PAGEFAULT_ERROR_MASK:
582 case VMX_VMCS32_CTRL_PAGEFAULT_ERROR_MATCH:
583 case VMX_VMCS32_CTRL_CR3_TARGET_COUNT:
584 case VMX_VMCS32_CTRL_EXIT:
585 case VMX_VMCS32_CTRL_EXIT_MSR_STORE_COUNT:
586 case VMX_VMCS32_CTRL_EXIT_MSR_LOAD_COUNT:
587 case VMX_VMCS32_CTRL_ENTRY:
588 case VMX_VMCS32_CTRL_ENTRY_MSR_LOAD_COUNT:
589 case VMX_VMCS32_CTRL_ENTRY_INTERRUPTION_INFO:
590 case VMX_VMCS32_CTRL_ENTRY_EXCEPTION_ERRCODE:
591 case VMX_VMCS32_CTRL_ENTRY_INSTR_LENGTH: return true;
592 case VMX_VMCS32_CTRL_TPR_THRESHOLD: return pFeat->fVmxUseTprShadow;
593 case VMX_VMCS32_CTRL_PROC_EXEC2: return pFeat->fVmxSecondaryExecCtls;
594 case VMX_VMCS32_CTRL_PLE_GAP:
595 case VMX_VMCS32_CTRL_PLE_WINDOW: return pFeat->fVmxPauseLoopExit;
596
597 /* Read-only data fields. */
598 case VMX_VMCS32_RO_VM_INSTR_ERROR:
599 case VMX_VMCS32_RO_EXIT_REASON:
600 case VMX_VMCS32_RO_EXIT_INTERRUPTION_INFO:
601 case VMX_VMCS32_RO_EXIT_INTERRUPTION_ERROR_CODE:
602 case VMX_VMCS32_RO_IDT_VECTORING_INFO:
603 case VMX_VMCS32_RO_IDT_VECTORING_ERROR_CODE:
604 case VMX_VMCS32_RO_EXIT_INSTR_LENGTH:
605 case VMX_VMCS32_RO_EXIT_INSTR_INFO: return true;
606
607 /* Guest-state fields. */
608 case VMX_VMCS32_GUEST_ES_LIMIT:
609 case VMX_VMCS32_GUEST_CS_LIMIT:
610 case VMX_VMCS32_GUEST_SS_LIMIT:
611 case VMX_VMCS32_GUEST_DS_LIMIT:
612 case VMX_VMCS32_GUEST_FS_LIMIT:
613 case VMX_VMCS32_GUEST_GS_LIMIT:
614 case VMX_VMCS32_GUEST_LDTR_LIMIT:
615 case VMX_VMCS32_GUEST_TR_LIMIT:
616 case VMX_VMCS32_GUEST_GDTR_LIMIT:
617 case VMX_VMCS32_GUEST_IDTR_LIMIT:
618 case VMX_VMCS32_GUEST_ES_ACCESS_RIGHTS:
619 case VMX_VMCS32_GUEST_CS_ACCESS_RIGHTS:
620 case VMX_VMCS32_GUEST_SS_ACCESS_RIGHTS:
621 case VMX_VMCS32_GUEST_DS_ACCESS_RIGHTS:
622 case VMX_VMCS32_GUEST_FS_ACCESS_RIGHTS:
623 case VMX_VMCS32_GUEST_GS_ACCESS_RIGHTS:
624 case VMX_VMCS32_GUEST_LDTR_ACCESS_RIGHTS:
625 case VMX_VMCS32_GUEST_TR_ACCESS_RIGHTS:
626 case VMX_VMCS32_GUEST_INT_STATE:
627 case VMX_VMCS32_GUEST_ACTIVITY_STATE:
628 case VMX_VMCS32_GUEST_SMBASE:
629 case VMX_VMCS32_GUEST_SYSENTER_CS: return true;
630 case VMX_VMCS32_PREEMPT_TIMER_VALUE: return pFeat->fVmxPreemptTimer;
631
632 /* Host-state fields. */
633 case VMX_VMCS32_HOST_SYSENTER_CS: return true;
634
635 /*
636 * Natural-width fields.
637 */
638 /* Control fields. */
639 case VMX_VMCS_CTRL_CR0_MASK:
640 case VMX_VMCS_CTRL_CR4_MASK:
641 case VMX_VMCS_CTRL_CR0_READ_SHADOW:
642 case VMX_VMCS_CTRL_CR4_READ_SHADOW:
643 case VMX_VMCS_CTRL_CR3_TARGET_VAL0:
644 case VMX_VMCS_CTRL_CR3_TARGET_VAL1:
645 case VMX_VMCS_CTRL_CR3_TARGET_VAL2:
646 case VMX_VMCS_CTRL_CR3_TARGET_VAL3: return true;
647
648 /* Read-only data fields. */
649 case VMX_VMCS_RO_EXIT_QUALIFICATION:
650 case VMX_VMCS_RO_IO_RCX:
651 case VMX_VMCS_RO_IO_RSX:
652 case VMX_VMCS_RO_IO_RDI:
653 case VMX_VMCS_RO_IO_RIP:
654 case VMX_VMCS_RO_EXIT_GUEST_LINEAR_ADDR: return true;
655
656 /* Guest-state fields. */
657 case VMX_VMCS_GUEST_CR0:
658 case VMX_VMCS_GUEST_CR3:
659 case VMX_VMCS_GUEST_CR4:
660 case VMX_VMCS_GUEST_ES_BASE:
661 case VMX_VMCS_GUEST_CS_BASE:
662 case VMX_VMCS_GUEST_SS_BASE:
663 case VMX_VMCS_GUEST_DS_BASE:
664 case VMX_VMCS_GUEST_FS_BASE:
665 case VMX_VMCS_GUEST_GS_BASE:
666 case VMX_VMCS_GUEST_LDTR_BASE:
667 case VMX_VMCS_GUEST_TR_BASE:
668 case VMX_VMCS_GUEST_GDTR_BASE:
669 case VMX_VMCS_GUEST_IDTR_BASE:
670 case VMX_VMCS_GUEST_DR7:
671 case VMX_VMCS_GUEST_RSP:
672 case VMX_VMCS_GUEST_RIP:
673 case VMX_VMCS_GUEST_RFLAGS:
674 case VMX_VMCS_GUEST_PENDING_DEBUG_XCPTS:
675 case VMX_VMCS_GUEST_SYSENTER_ESP:
676 case VMX_VMCS_GUEST_SYSENTER_EIP: return true;
677
678 /* Host-state fields. */
679 case VMX_VMCS_HOST_CR0:
680 case VMX_VMCS_HOST_CR3:
681 case VMX_VMCS_HOST_CR4:
682 case VMX_VMCS_HOST_FS_BASE:
683 case VMX_VMCS_HOST_GS_BASE:
684 case VMX_VMCS_HOST_TR_BASE:
685 case VMX_VMCS_HOST_GDTR_BASE:
686 case VMX_VMCS_HOST_IDTR_BASE:
687 case VMX_VMCS_HOST_SYSENTER_ESP:
688 case VMX_VMCS_HOST_SYSENTER_EIP:
689 case VMX_VMCS_HOST_RSP:
690 case VMX_VMCS_HOST_RIP: return true;
691 }
692
693 return false;
694}
695
696
697/**
698 * Gets VM-exit instruction information along with any displacement for an
699 * instruction VM-exit.
700 *
701 * @returns The VM-exit instruction information.
702 * @param pVCpu The cross context virtual CPU structure.
703 * @param uExitReason The VM-exit reason.
704 * @param uInstrId The VM-exit instruction identity (VMXINSTRID_XXX) if
705 * any. Pass VMXINSTRID_NONE otherwise.
706 * @param fPrimaryOpRead If the primary operand of the ModR/M byte (bits 0:3) is
707 * a read or write.
708 * @param pGCPtrDisp Where to store the displacement field. Optional, can be
709 * NULL.
710 */
711IEM_STATIC uint32_t iemVmxGetExitInstrInfo(PVMCPU pVCpu, uint32_t uExitReason, VMXINSTRID uInstrId, bool fPrimaryOpRead,
712 PRTGCPTR pGCPtrDisp)
713{
714 RTGCPTR GCPtrDisp;
715 VMXEXITINSTRINFO ExitInstrInfo;
716 ExitInstrInfo.u = 0;
717
718 /*
719 * Get and parse the ModR/M byte from our decoded opcodes.
720 */
721 uint8_t bRm;
722 uint8_t const offModRm = pVCpu->iem.s.offModRm;
723 IEM_MODRM_GET_U8(pVCpu, bRm, offModRm);
724 if ((bRm & X86_MODRM_MOD_MASK) == (3 << X86_MODRM_MOD_SHIFT))
725 {
726 /*
727 * ModR/M indicates register addressing.
728 *
729 * The primary/secondary register operands are reported in the iReg1 or iReg2
730 * fields depending on whether it is a read/write form.
731 */
732 uint8_t idxReg1;
733 uint8_t idxReg2;
734 if (fPrimaryOpRead)
735 {
736 idxReg1 = ((bRm >> X86_MODRM_REG_SHIFT) & X86_MODRM_REG_SMASK) | pVCpu->iem.s.uRexReg;
737 idxReg2 = (bRm & X86_MODRM_RM_MASK) | pVCpu->iem.s.uRexB;
738 }
739 else
740 {
741 idxReg1 = (bRm & X86_MODRM_RM_MASK) | pVCpu->iem.s.uRexB;
742 idxReg2 = ((bRm >> X86_MODRM_REG_SHIFT) & X86_MODRM_REG_SMASK) | pVCpu->iem.s.uRexReg;
743 }
744 ExitInstrInfo.All.u2Scaling = 0;
745 ExitInstrInfo.All.iReg1 = idxReg1;
746 ExitInstrInfo.All.u3AddrSize = pVCpu->iem.s.enmEffAddrMode;
747 ExitInstrInfo.All.fIsRegOperand = 1;
748 ExitInstrInfo.All.uOperandSize = pVCpu->iem.s.enmEffOpSize;
749 ExitInstrInfo.All.iSegReg = 0;
750 ExitInstrInfo.All.iIdxReg = 0;
751 ExitInstrInfo.All.fIdxRegInvalid = 1;
752 ExitInstrInfo.All.iBaseReg = 0;
753 ExitInstrInfo.All.fBaseRegInvalid = 1;
754 ExitInstrInfo.All.iReg2 = idxReg2;
755
756 /* Displacement not applicable for register addressing. */
757 GCPtrDisp = 0;
758 }
759 else
760 {
761 /*
762 * ModR/M indicates memory addressing.
763 */
764 uint8_t uScale = 0;
765 bool fBaseRegValid = false;
766 bool fIdxRegValid = false;
767 uint8_t iBaseReg = 0;
768 uint8_t iIdxReg = 0;
769 if (pVCpu->iem.s.enmEffAddrMode == IEMMODE_16BIT)
770 {
771 /*
772 * Parse the ModR/M, displacement for 16-bit addressing mode.
773 * See Intel instruction spec. Table 2-1. "16-Bit Addressing Forms with the ModR/M Byte".
774 */
775 uint16_t u16Disp = 0;
776 uint8_t const offDisp = offModRm + sizeof(bRm);
777 if ((bRm & (X86_MODRM_MOD_MASK | X86_MODRM_RM_MASK)) == 6)
778 {
779 /* Displacement without any registers. */
780 IEM_DISP_GET_U16(pVCpu, u16Disp, offDisp);
781 }
782 else
783 {
784 /* Register (index and base). */
785 switch (bRm & X86_MODRM_RM_MASK)
786 {
787 case 0: fBaseRegValid = true; iBaseReg = X86_GREG_xBX; fIdxRegValid = true; iIdxReg = X86_GREG_xSI; break;
788 case 1: fBaseRegValid = true; iBaseReg = X86_GREG_xBX; fIdxRegValid = true; iIdxReg = X86_GREG_xDI; break;
789 case 2: fBaseRegValid = true; iBaseReg = X86_GREG_xBP; fIdxRegValid = true; iIdxReg = X86_GREG_xSI; break;
790 case 3: fBaseRegValid = true; iBaseReg = X86_GREG_xBP; fIdxRegValid = true; iIdxReg = X86_GREG_xDI; break;
791 case 4: fIdxRegValid = true; iIdxReg = X86_GREG_xSI; break;
792 case 5: fIdxRegValid = true; iIdxReg = X86_GREG_xDI; break;
793 case 6: fBaseRegValid = true; iBaseReg = X86_GREG_xBP; break;
794 case 7: fBaseRegValid = true; iBaseReg = X86_GREG_xBX; break;
795 }
796
797 /* Register + displacement. */
798 switch ((bRm >> X86_MODRM_MOD_SHIFT) & X86_MODRM_MOD_SMASK)
799 {
800 case 0: break;
801 case 1: IEM_DISP_GET_S8_SX_U16(pVCpu, u16Disp, offDisp); break;
802 case 2: IEM_DISP_GET_U16(pVCpu, u16Disp, offDisp); break;
803 default:
804 {
805 /* Register addressing, handled at the beginning. */
806 AssertMsgFailed(("ModR/M %#x implies register addressing, memory addressing expected!", bRm));
807 break;
808 }
809 }
810 }
811
812 Assert(!uScale); /* There's no scaling/SIB byte for 16-bit addressing. */
813 GCPtrDisp = (int16_t)u16Disp; /* Sign-extend the displacement. */
814 }
815 else if (pVCpu->iem.s.enmEffAddrMode == IEMMODE_32BIT)
816 {
817 /*
818 * Parse the ModR/M, SIB, displacement for 32-bit addressing mode.
819 * See Intel instruction spec. Table 2-2. "32-Bit Addressing Forms with the ModR/M Byte".
820 */
821 uint32_t u32Disp = 0;
822 if ((bRm & (X86_MODRM_MOD_MASK | X86_MODRM_RM_MASK)) == 5)
823 {
824 /* Displacement without any registers. */
825 uint8_t const offDisp = offModRm + sizeof(bRm);
826 IEM_DISP_GET_U32(pVCpu, u32Disp, offDisp);
827 }
828 else
829 {
830 /* Register (and perhaps scale, index and base). */
831 uint8_t offDisp = offModRm + sizeof(bRm);
832 iBaseReg = (bRm & X86_MODRM_RM_MASK);
833 if (iBaseReg == 4)
834 {
835 /* An SIB byte follows the ModR/M byte, parse it. */
836 uint8_t bSib;
837 uint8_t const offSib = offModRm + sizeof(bRm);
838 IEM_SIB_GET_U8(pVCpu, bSib, offSib);
839
840 /* A displacement may follow SIB, update its offset. */
841 offDisp += sizeof(bSib);
842
843 /* Get the scale. */
844 uScale = (bSib >> X86_SIB_SCALE_SHIFT) & X86_SIB_SCALE_SMASK;
845
846 /* Get the index register. */
847 iIdxReg = (bSib >> X86_SIB_INDEX_SHIFT) & X86_SIB_INDEX_SMASK;
848 fIdxRegValid = RT_BOOL(iIdxReg != 4);
849
850 /* Get the base register. */
851 iBaseReg = bSib & X86_SIB_BASE_MASK;
852 fBaseRegValid = true;
853 if (iBaseReg == 5)
854 {
855 if ((bRm & X86_MODRM_MOD_MASK) == 0)
856 {
857 /* Mod is 0 implies a 32-bit displacement with no base. */
858 fBaseRegValid = false;
859 IEM_DISP_GET_U32(pVCpu, u32Disp, offDisp);
860 }
861 else
862 {
863 /* Mod is not 0 implies an 8-bit/32-bit displacement (handled below) with an EBP base. */
864 iBaseReg = X86_GREG_xBP;
865 }
866 }
867 }
868
869 /* Register + displacement. */
870 switch ((bRm >> X86_MODRM_MOD_SHIFT) & X86_MODRM_MOD_SMASK)
871 {
872 case 0: /* Handled above */ break;
873 case 1: IEM_DISP_GET_S8_SX_U32(pVCpu, u32Disp, offDisp); break;
874 case 2: IEM_DISP_GET_U32(pVCpu, u32Disp, offDisp); break;
875 default:
876 {
877 /* Register addressing, handled at the beginning. */
878 AssertMsgFailed(("ModR/M %#x implies register addressing, memory addressing expected!", bRm));
879 break;
880 }
881 }
882 }
883
884 GCPtrDisp = (int32_t)u32Disp; /* Sign-extend the displacement. */
885 }
886 else
887 {
888 Assert(pVCpu->iem.s.enmEffAddrMode == IEMMODE_64BIT);
889
890 /*
891 * Parse the ModR/M, SIB, displacement for 64-bit addressing mode.
892 * See Intel instruction spec. 2.2 "IA-32e Mode".
893 */
894 uint64_t u64Disp = 0;
895 bool const fRipRelativeAddr = RT_BOOL((bRm & (X86_MODRM_MOD_MASK | X86_MODRM_RM_MASK)) == 5);
896 if (fRipRelativeAddr)
897 {
898 /*
899 * RIP-relative addressing mode.
900 *
901 * The displacment is 32-bit signed implying an offset range of +/-2G.
902 * See Intel instruction spec. 2.2.1.6 "RIP-Relative Addressing".
903 */
904 uint8_t const offDisp = offModRm + sizeof(bRm);
905 IEM_DISP_GET_S32_SX_U64(pVCpu, u64Disp, offDisp);
906 }
907 else
908 {
909 uint8_t offDisp = offModRm + sizeof(bRm);
910
911 /*
912 * Register (and perhaps scale, index and base).
913 *
914 * REX.B extends the most-significant bit of the base register. However, REX.B
915 * is ignored while determining whether an SIB follows the opcode. Hence, we
916 * shall OR any REX.B bit -after- inspecting for an SIB byte below.
917 *
918 * See Intel instruction spec. Table 2-5. "Special Cases of REX Encodings".
919 */
920 iBaseReg = (bRm & X86_MODRM_RM_MASK);
921 if (iBaseReg == 4)
922 {
923 /* An SIB byte follows the ModR/M byte, parse it. Displacement (if any) follows SIB. */
924 uint8_t bSib;
925 uint8_t const offSib = offModRm + sizeof(bRm);
926 IEM_SIB_GET_U8(pVCpu, bSib, offSib);
927
928 /* Displacement may follow SIB, update its offset. */
929 offDisp += sizeof(bSib);
930
931 /* Get the scale. */
932 uScale = (bSib >> X86_SIB_SCALE_SHIFT) & X86_SIB_SCALE_SMASK;
933
934 /* Get the index. */
935 iIdxReg = ((bSib >> X86_SIB_INDEX_SHIFT) & X86_SIB_INDEX_SMASK) | pVCpu->iem.s.uRexIndex;
936 fIdxRegValid = RT_BOOL(iIdxReg != 4); /* R12 -can- be used as an index register. */
937
938 /* Get the base. */
939 iBaseReg = (bSib & X86_SIB_BASE_MASK);
940 fBaseRegValid = true;
941 if (iBaseReg == 5)
942 {
943 if ((bRm & X86_MODRM_MOD_MASK) == 0)
944 {
945 /* Mod is 0 implies a signed 32-bit displacement with no base. */
946 IEM_DISP_GET_S32_SX_U64(pVCpu, u64Disp, offDisp);
947 }
948 else
949 {
950 /* Mod is non-zero implies an 8-bit/32-bit displacement (handled below) with RBP or R13 as base. */
951 iBaseReg = pVCpu->iem.s.uRexB ? X86_GREG_x13 : X86_GREG_xBP;
952 }
953 }
954 }
955 iBaseReg |= pVCpu->iem.s.uRexB;
956
957 /* Register + displacement. */
958 switch ((bRm >> X86_MODRM_MOD_SHIFT) & X86_MODRM_MOD_SMASK)
959 {
960 case 0: /* Handled above */ break;
961 case 1: IEM_DISP_GET_S8_SX_U64(pVCpu, u64Disp, offDisp); break;
962 case 2: IEM_DISP_GET_S32_SX_U64(pVCpu, u64Disp, offDisp); break;
963 default:
964 {
965 /* Register addressing, handled at the beginning. */
966 AssertMsgFailed(("ModR/M %#x implies register addressing, memory addressing expected!", bRm));
967 break;
968 }
969 }
970 }
971
972 GCPtrDisp = fRipRelativeAddr ? pVCpu->cpum.GstCtx.rip + u64Disp : u64Disp;
973 }
974
975 /*
976 * The primary or secondary register operand is reported in iReg2 depending
977 * on whether the primary operand is in read/write form.
978 */
979 uint8_t idxReg2;
980 if (fPrimaryOpRead)
981 {
982 idxReg2 = bRm & X86_MODRM_RM_MASK;
983 if (pVCpu->iem.s.enmEffAddrMode == IEMMODE_64BIT)
984 idxReg2 |= pVCpu->iem.s.uRexB;
985 }
986 else
987 {
988 idxReg2 = (bRm >> X86_MODRM_REG_SHIFT) & X86_MODRM_REG_SMASK;
989 if (pVCpu->iem.s.enmEffAddrMode == IEMMODE_64BIT)
990 idxReg2 |= pVCpu->iem.s.uRexReg;
991 }
992 ExitInstrInfo.All.u2Scaling = uScale;
993 ExitInstrInfo.All.iReg1 = 0; /* Not applicable for memory addressing. */
994 ExitInstrInfo.All.u3AddrSize = pVCpu->iem.s.enmEffAddrMode;
995 ExitInstrInfo.All.fIsRegOperand = 0;
996 ExitInstrInfo.All.uOperandSize = pVCpu->iem.s.enmEffOpSize;
997 ExitInstrInfo.All.iSegReg = pVCpu->iem.s.iEffSeg;
998 ExitInstrInfo.All.iIdxReg = iIdxReg;
999 ExitInstrInfo.All.fIdxRegInvalid = !fIdxRegValid;
1000 ExitInstrInfo.All.iBaseReg = iBaseReg;
1001 ExitInstrInfo.All.iIdxReg = !fBaseRegValid;
1002 ExitInstrInfo.All.iReg2 = idxReg2;
1003 }
1004
1005 /*
1006 * Handle exceptions for certain instructions.
1007 * (e.g. some instructions convey an instruction identity).
1008 */
1009 switch (uExitReason)
1010 {
1011 case VMX_EXIT_GDTR_IDTR_ACCESS:
1012 {
1013 Assert(VMXINSTRID_IS_VALID(uInstrId));
1014 ExitInstrInfo.GdtIdt.u2InstrId = VMXINSTRID_GET_ID(uInstrId);
1015 ExitInstrInfo.GdtIdt.u2Undef0 = 0;
1016 break;
1017 }
1018
1019 case VMX_EXIT_LDTR_TR_ACCESS:
1020 {
1021 Assert(VMXINSTRID_IS_VALID(uInstrId));
1022 ExitInstrInfo.LdtTr.u2InstrId = VMXINSTRID_GET_ID(uInstrId);
1023 ExitInstrInfo.LdtTr.u2Undef0 = 0;
1024 break;
1025 }
1026
1027 case VMX_EXIT_RDRAND:
1028 case VMX_EXIT_RDSEED:
1029 {
1030 Assert(ExitInstrInfo.RdrandRdseed.u2OperandSize != 3);
1031 break;
1032 }
1033 }
1034
1035 /* Update displacement and return the constructed VM-exit instruction information field. */
1036 if (pGCPtrDisp)
1037 *pGCPtrDisp = GCPtrDisp;
1038 return ExitInstrInfo.u;
1039}
1040
1041
1042/**
1043 * Implements VMSucceed for VMX instruction success.
1044 *
1045 * @param pVCpu The cross context virtual CPU structure.
1046 */
1047DECL_FORCE_INLINE(void) iemVmxVmSucceed(PVMCPU pVCpu)
1048{
1049 pVCpu->cpum.GstCtx.eflags.u32 &= ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF);
1050}
1051
1052
1053/**
1054 * Implements VMFailInvalid for VMX instruction failure.
1055 *
1056 * @param pVCpu The cross context virtual CPU structure.
1057 */
1058DECL_FORCE_INLINE(void) iemVmxVmFailInvalid(PVMCPU pVCpu)
1059{
1060 pVCpu->cpum.GstCtx.eflags.u32 &= ~(X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF);
1061 pVCpu->cpum.GstCtx.eflags.u32 |= X86_EFL_CF;
1062}
1063
1064
1065/**
1066 * Implements VMFailValid for VMX instruction failure.
1067 *
1068 * @param pVCpu The cross context virtual CPU structure.
1069 * @param enmInsErr The VM instruction error.
1070 */
1071DECL_FORCE_INLINE(void) iemVmxVmFailValid(PVMCPU pVCpu, VMXINSTRERR enmInsErr)
1072{
1073 if (IEM_VMX_HAS_CURRENT_VMCS(pVCpu))
1074 {
1075 pVCpu->cpum.GstCtx.eflags.u32 &= ~(X86_EFL_CF | X86_EFL_PF | X86_EFL_AF | X86_EFL_ZF | X86_EFL_SF | X86_EFL_OF);
1076 pVCpu->cpum.GstCtx.eflags.u32 |= X86_EFL_ZF;
1077 /** @todo NSTVMX: VMWrite enmInsErr to VM-instruction error field. */
1078 RT_NOREF(enmInsErr);
1079 }
1080}
1081
1082
1083/**
1084 * Implements VMFail for VMX instruction failure.
1085 *
1086 * @param pVCpu The cross context virtual CPU structure.
1087 * @param enmInsErr The VM instruction error.
1088 */
1089DECL_FORCE_INLINE(void) iemVmxVmFail(PVMCPU pVCpu, VMXINSTRERR enmInsErr)
1090{
1091 if (IEM_VMX_HAS_CURRENT_VMCS(pVCpu))
1092 {
1093 iemVmxVmFailValid(pVCpu, enmInsErr);
1094 /** @todo Set VM-instruction error field in the current virtual-VMCS. */
1095 }
1096 else
1097 iemVmxVmFailInvalid(pVCpu);
1098}
1099
1100
1101/**
1102 * Flushes the current VMCS contents back to guest memory.
1103 *
1104 * @returns VBox status code.
1105 * @param pVCpu The cross context virtual CPU structure.
1106 */
1107DECL_FORCE_INLINE(int) iemVmxCommitCurrentVmcsToMemory(PVMCPU pVCpu)
1108{
1109 Assert(IEM_VMX_HAS_CURRENT_VMCS(pVCpu));
1110 int rc = PGMPhysSimpleWriteGCPhys(pVCpu->CTX_SUFF(pVM), IEM_VMX_GET_CURRENT_VMCS(pVCpu),
1111 pVCpu->cpum.GstCtx.hwvirt.vmx.CTX_SUFF(pVmcs), sizeof(VMXVVMCS));
1112 IEM_VMX_CLEAR_CURRENT_VMCS(pVCpu);
1113 return rc;
1114}
1115
1116
1117/**
1118 * Implements VMSucceed for the VMREAD instruction and increments the guest RIP.
1119 *
1120 * @param pVCpu The cross context virtual CPU structure.
1121 */
1122DECL_FORCE_INLINE(void) iemVmxVmreadSuccess(PVMCPU pVCpu, uint8_t cbInstr)
1123{
1124 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmread_Success;
1125 iemVmxVmSucceed(pVCpu);
1126 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1127}
1128
1129
1130/**
1131 * VMREAD common (memory/register) instruction execution worker
1132 *
1133 * @param pVCpu The cross context virtual CPU structure.
1134 * @param cbInstr The instruction length.
1135 * @param pu64Dst Where to write the VMCS value (only updated when
1136 * VINF_SUCCESS is returned).
1137 * @param u64FieldEnc The VMCS field encoding.
1138 * @param pExitInfo Pointer to the VM-exit information struct. Optional, can
1139 * be NULL.
1140 */
1141IEM_STATIC VBOXSTRICTRC iemVmxVmreadCommon(PVMCPU pVCpu, uint8_t cbInstr, uint64_t *pu64Dst, uint64_t u64FieldEnc,
1142 PCVMXVEXITINFO pExitInfo)
1143{
1144 if (IEM_IS_VMX_NON_ROOT_MODE(pVCpu))
1145 {
1146 RT_NOREF(pExitInfo); RT_NOREF(cbInstr);
1147 /** @todo NSTVMX: intercept. */
1148 /** @todo NSTVMX: VMCS shadowing intercept (VMREAD bitmap). */
1149 }
1150
1151 /* CPL. */
1152 if (pVCpu->iem.s.uCpl > 0)
1153 {
1154 Log(("vmread: CPL %u -> #GP(0)\n", pVCpu->iem.s.uCpl));
1155 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmread_Cpl;
1156 return iemRaiseGeneralProtectionFault0(pVCpu);
1157 }
1158
1159 /* VMCS pointer in root mode. */
1160 if ( IEM_IS_VMX_ROOT_MODE(pVCpu)
1161 && !IEM_VMX_HAS_CURRENT_VMCS(pVCpu))
1162 {
1163 Log(("vmread: VMCS pointer %#RGp invalid -> VMFailInvalid\n", IEM_VMX_GET_CURRENT_VMCS(pVCpu)));
1164 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmread_PtrInvalid;
1165 iemVmxVmFailInvalid(pVCpu);
1166 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1167 return VINF_SUCCESS;
1168 }
1169
1170 /* VMCS-link pointer in non-root mode. */
1171 if ( IEM_IS_VMX_NON_ROOT_MODE(pVCpu)
1172 && !IEM_VMX_HAS_SHADOW_VMCS(pVCpu))
1173 {
1174 Log(("vmread: VMCS-link pointer %#RGp invalid -> VMFailInvalid\n", IEM_VMX_GET_SHADOW_VMCS(pVCpu)));
1175 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmread_LinkPtrInvalid;
1176 iemVmxVmFailInvalid(pVCpu);
1177 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1178 return VINF_SUCCESS;
1179 }
1180
1181 /* Supported VMCS field. */
1182 if (iemVmxIsVmcsFieldValid(pVCpu, u64FieldEnc))
1183 {
1184 Log(("vmread: VMCS field %#RX64 invalid -> VMFail\n", u64FieldEnc));
1185 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmread_FieldInvalid;
1186 iemVmxVmFail(pVCpu, VMXINSTRERR_VMREAD_INVALID_COMPONENT);
1187 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1188 return VINF_SUCCESS;
1189 }
1190
1191 /*
1192 * Setup reading from the current or shadow VMCS.
1193 */
1194 uint8_t *pbVmcs;
1195 if (IEM_IS_VMX_NON_ROOT_MODE(pVCpu))
1196 pbVmcs = (uint8_t *)pVCpu->cpum.GstCtx.hwvirt.vmx.CTX_SUFF(pShadowVmcs);
1197 else
1198 pbVmcs = (uint8_t *)pVCpu->cpum.GstCtx.hwvirt.vmx.CTX_SUFF(pVmcs);
1199 Assert(pbVmcs);
1200
1201 VMXVMCSFIELDENC FieldEnc;
1202 FieldEnc.u = RT_LO_U32(u64FieldEnc);
1203 uint8_t const uWidth = FieldEnc.n.u2Width;
1204 uint8_t const uType = FieldEnc.n.u2Type;
1205 uint8_t const uWidthType = (uWidth << 2) | uType;
1206 uint8_t const uIndex = FieldEnc.n.u8Index;
1207 AssertRCReturn(uIndex <= VMX_V_VMCS_MAX_INDEX, VERR_IEM_IPE_2);
1208 uint16_t const offField = g_aoffVmcsMap[uWidthType][uIndex];
1209
1210 /*
1211 * Read the VMCS component based on the field's effective width.
1212 *
1213 * The effective width is 64-bit fields adjusted to 32-bits if the access-type
1214 * indicates high bits (little endian).
1215 *
1216 * Note! The caller is responsible to trim the result and update registers
1217 * or memory locations are required. Here we just zero-extend to the largest
1218 * type (i.e. 64-bits).
1219 */
1220 uint8_t *pbField = pbVmcs + offField;
1221 uint8_t const uEffWidth = HMVmxGetVmcsFieldWidthEff(FieldEnc.u);
1222 switch (uEffWidth)
1223 {
1224 case VMX_VMCS_ENC_WIDTH_64BIT:
1225 case VMX_VMCS_ENC_WIDTH_NATURAL: *pu64Dst = *(uint64_t *)pbField; break;
1226 case VMX_VMCS_ENC_WIDTH_32BIT: *pu64Dst = *(uint32_t *)pbField; break;
1227 case VMX_VMCS_ENC_WIDTH_16BIT: *pu64Dst = *(uint16_t *)pbField; break;
1228 }
1229 return VINF_SUCCESS;
1230}
1231
1232
1233/**
1234 * VMREAD (64-bit register) instruction execution worker.
1235 *
1236 * @param pVCpu The cross context virtual CPU structure.
1237 * @param cbInstr The instruction length.
1238 * @param pu64Dst Where to store the VMCS field's value.
1239 * @param u64FieldEnc The VMCS field encoding.
1240 * @param pExitInfo Pointer to the VM-exit information struct. Optional, can
1241 * be NULL.
1242 */
1243IEM_STATIC VBOXSTRICTRC iemVmxVmreadReg64(PVMCPU pVCpu, uint8_t cbInstr, uint64_t *pu64Dst, uint64_t u64FieldEnc,
1244 PCVMXVEXITINFO pExitInfo)
1245{
1246 VBOXSTRICTRC rcStrict = iemVmxVmreadCommon(pVCpu, cbInstr, pu64Dst, u64FieldEnc, pExitInfo);
1247 if (rcStrict == VINF_SUCCESS)
1248 {
1249 iemVmxVmreadSuccess(pVCpu, cbInstr);
1250 return VINF_SUCCESS;
1251 }
1252
1253 Log(("vmread/reg: iemVmxVmreadCommon failed rc=%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)));
1254 return rcStrict;
1255}
1256
1257
1258/**
1259 * VMREAD (32-bit register) instruction execution worker.
1260 *
1261 * @param pVCpu The cross context virtual CPU structure.
1262 * @param cbInstr The instruction length.
1263 * @param pu32Dst Where to store the VMCS field's value.
1264 * @param u32FieldEnc The VMCS field encoding.
1265 * @param pExitInfo Pointer to the VM-exit information struct. Optional, can
1266 * be NULL.
1267 */
1268IEM_STATIC VBOXSTRICTRC iemVmxVmreadReg32(PVMCPU pVCpu, uint8_t cbInstr, uint32_t *pu32Dst, uint64_t u32FieldEnc,
1269 PCVMXVEXITINFO pExitInfo)
1270{
1271 uint64_t u64Dst;
1272 VBOXSTRICTRC rcStrict = iemVmxVmreadCommon(pVCpu, cbInstr, &u64Dst, u32FieldEnc, pExitInfo);
1273 if (rcStrict == VINF_SUCCESS)
1274 {
1275 *pu32Dst = u64Dst;
1276 iemVmxVmreadSuccess(pVCpu, cbInstr);
1277 return VINF_SUCCESS;
1278 }
1279
1280 Log(("vmread/reg: iemVmxVmreadCommon failed rc=%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)));
1281 return rcStrict;
1282}
1283
1284
1285/**
1286 * VMREAD (memory) instruction execution worker.
1287 *
1288 * @param pVCpu The cross context virtual CPU structure.
1289 * @param cbInstr The instruction length.
1290 * @param iEffSeg The effective segment register to use with @a u64Val.
1291 * Pass UINT8_MAX if it is a register access.
1292 * @param enmEffAddrMode The effective addressing mode (only used with memory
1293 * operand).
1294 * @param GCPtrDst The guest linear address to store the VMCS field's
1295 * value.
1296 * @param u64FieldEnc The VMCS field encoding.
1297 * @param pExitInfo Pointer to the VM-exit information struct. Optional, can
1298 * be NULL.
1299 */
1300IEM_STATIC VBOXSTRICTRC iemVmxVmreadMem(PVMCPU pVCpu, uint8_t cbInstr, uint8_t iEffSeg, IEMMODE enmEffAddrMode,
1301 RTGCPTR GCPtrDst, uint64_t u64FieldEnc, PCVMXVEXITINFO pExitInfo)
1302{
1303 uint64_t u64Dst;
1304 VBOXSTRICTRC rcStrict = iemVmxVmreadCommon(pVCpu, cbInstr, &u64Dst, u64FieldEnc, pExitInfo);
1305 if (rcStrict == VINF_SUCCESS)
1306 {
1307 /*
1308 * Write the VMCS field's value to the location specified in guest-memory.
1309 *
1310 * The pointer size depends on the address size (address-size prefix allowed).
1311 * The operand size depends on IA-32e mode (operand-size prefix not allowed).
1312 */
1313 static uint64_t const s_auAddrSizeMasks[] = { UINT64_C(0xffff), UINT64_C(0xffffffff), UINT64_C(0xffffffffffffffff) };
1314 Assert(enmEffAddrMode < RT_ELEMENTS(s_auAddrSizeMasks));
1315 GCPtrDst &= s_auAddrSizeMasks[enmEffAddrMode];
1316
1317 if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
1318 rcStrict = iemMemStoreDataU64(pVCpu, iEffSeg, GCPtrDst, u64Dst);
1319 else
1320 rcStrict = iemMemStoreDataU32(pVCpu, iEffSeg, GCPtrDst, u64Dst);
1321 if (rcStrict == VINF_SUCCESS)
1322 {
1323 iemVmxVmreadSuccess(pVCpu, cbInstr);
1324 return VINF_SUCCESS;
1325 }
1326
1327 Log(("vmread/mem: Failed to write to memory operand at %#RGv, rc=%Rrc\n", GCPtrDst, VBOXSTRICTRC_VAL(rcStrict)));
1328 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmread_PtrMap;
1329 return rcStrict;
1330 }
1331
1332 Log(("vmread/mem: iemVmxVmreadCommon failed rc=%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)));
1333 return rcStrict;
1334}
1335
1336
1337/**
1338 * VMWRITE instruction execution worker.
1339 *
1340 * @param pVCpu The cross context virtual CPU structure.
1341 * @param cbInstr The instruction length.
1342 * @param iEffSeg The effective segment register to use with @a u64Val.
1343 * Pass UINT8_MAX if it is a register access.
1344 * @param enmEffAddrMode The effective addressing mode (only used with memory
1345 * operand).
1346 * @param u64Val The value to write (or guest linear address to the
1347 * value), @a iEffSeg will indicate if it's a memory
1348 * operand.
1349 * @param u64FieldEnc The VMCS field encoding.
1350 * @param pExitInfo Pointer to the VM-exit information struct. Optional, can
1351 * be NULL.
1352 */
1353IEM_STATIC VBOXSTRICTRC iemVmxVmwrite(PVMCPU pVCpu, uint8_t cbInstr, uint8_t iEffSeg, IEMMODE enmEffAddrMode, uint64_t u64Val,
1354 uint64_t u64FieldEnc, PCVMXVEXITINFO pExitInfo)
1355{
1356 if (IEM_IS_VMX_NON_ROOT_MODE(pVCpu))
1357 {
1358 RT_NOREF(pExitInfo);
1359 /** @todo NSTVMX: intercept. */
1360 /** @todo NSTVMX: VMCS shadowing intercept (VMWRITE bitmap). */
1361 }
1362
1363 /* CPL. */
1364 if (pVCpu->iem.s.uCpl > 0)
1365 {
1366 Log(("vmwrite: CPL %u -> #GP(0)\n", pVCpu->iem.s.uCpl));
1367 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmwrite_Cpl;
1368 return iemRaiseGeneralProtectionFault0(pVCpu);
1369 }
1370
1371 /* VMCS pointer in root mode. */
1372 if ( IEM_IS_VMX_ROOT_MODE(pVCpu)
1373 && !IEM_VMX_HAS_CURRENT_VMCS(pVCpu))
1374 {
1375 Log(("vmwrite: VMCS pointer %#RGp invalid -> VMFailInvalid\n", IEM_VMX_GET_CURRENT_VMCS(pVCpu)));
1376 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmwrite_PtrInvalid;
1377 iemVmxVmFailInvalid(pVCpu);
1378 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1379 return VINF_SUCCESS;
1380 }
1381
1382 /* VMCS-link pointer in non-root mode. */
1383 if ( IEM_IS_VMX_NON_ROOT_MODE(pVCpu)
1384 && !IEM_VMX_HAS_SHADOW_VMCS(pVCpu))
1385 {
1386 Log(("vmwrite: VMCS-link pointer %#RGp invalid -> VMFailInvalid\n", IEM_VMX_GET_SHADOW_VMCS(pVCpu)));
1387 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmwrite_LinkPtrInvalid;
1388 iemVmxVmFailInvalid(pVCpu);
1389 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1390 return VINF_SUCCESS;
1391 }
1392
1393 /* If the VMWRITE instruction references memory, access the specified memory operand. */
1394 bool const fIsRegOperand = iEffSeg == UINT8_MAX;
1395 if (!fIsRegOperand)
1396 {
1397 static uint64_t const s_auAddrSizeMasks[] = { UINT64_C(0xffff), UINT64_C(0xffffffff), UINT64_C(0xffffffffffffffff) };
1398 Assert(enmEffAddrMode < RT_ELEMENTS(s_auAddrSizeMasks));
1399 RTGCPTR const GCPtrVal = u64Val & s_auAddrSizeMasks[enmEffAddrMode];
1400
1401 /* Read the value from the specified guest memory location. */
1402 VBOXSTRICTRC rcStrict;
1403 if (pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT)
1404 rcStrict = iemMemFetchDataU64(pVCpu, &u64Val, iEffSeg, GCPtrVal);
1405 else
1406 {
1407 uint32_t u32Val;
1408 rcStrict = iemMemFetchDataU32(pVCpu, &u32Val, iEffSeg, GCPtrVal);
1409 u64Val = u32Val;
1410 }
1411 if (RT_UNLIKELY(rcStrict != VINF_SUCCESS))
1412 {
1413 Log(("vmwrite: Failed to read value from memory operand at %#RGv, rc=%Rrc\n", GCPtrVal, VBOXSTRICTRC_VAL(rcStrict)));
1414 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmwrite_PtrMap;
1415 return rcStrict;
1416 }
1417 }
1418 else
1419 Assert(!pExitInfo || pExitInfo->InstrInfo.VmreadVmwrite.fIsRegOperand);
1420
1421 /* Supported VMCS field. */
1422 if (!iemVmxIsVmcsFieldValid(pVCpu, u64FieldEnc))
1423 {
1424 Log(("vmwrite: VMCS field %#RX64 invalid -> VMFail\n", u64FieldEnc));
1425 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmwrite_FieldInvalid;
1426 iemVmxVmFail(pVCpu, VMXINSTRERR_VMWRITE_INVALID_COMPONENT);
1427 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1428 return VINF_SUCCESS;
1429 }
1430
1431 /* Read-only VMCS field. */
1432 bool const fReadOnlyField = HMVmxIsVmcsFieldReadOnly(u64FieldEnc);
1433 if ( fReadOnlyField
1434 && !IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fVmxVmwriteAll)
1435 {
1436 Log(("vmwrite: Write to read-only VMCS component %#RX64 -> VMFail\n", u64FieldEnc));
1437 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmwrite_FieldRo;
1438 iemVmxVmFail(pVCpu, VMXINSTRERR_VMWRITE_RO_COMPONENT);
1439 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1440 return VINF_SUCCESS;
1441 }
1442
1443 /*
1444 * Setup writing to the current or shadow VMCS.
1445 */
1446 uint8_t *pbVmcs;
1447 if (IEM_IS_VMX_NON_ROOT_MODE(pVCpu))
1448 pbVmcs = (uint8_t *)pVCpu->cpum.GstCtx.hwvirt.vmx.CTX_SUFF(pShadowVmcs);
1449 else
1450 pbVmcs = (uint8_t *)pVCpu->cpum.GstCtx.hwvirt.vmx.CTX_SUFF(pVmcs);
1451 Assert(pbVmcs);
1452
1453 VMXVMCSFIELDENC FieldEnc;
1454 FieldEnc.u = RT_LO_U32(u64FieldEnc);
1455 uint8_t const uWidth = FieldEnc.n.u2Width;
1456 uint8_t const uType = FieldEnc.n.u2Type;
1457 uint8_t const uWidthType = (uWidth << 2) | uType;
1458 uint8_t const uIndex = FieldEnc.n.u8Index;
1459 AssertRCReturn(uIndex <= VMX_V_VMCS_MAX_INDEX, VERR_IEM_IPE_2);
1460 uint16_t const offField = g_aoffVmcsMap[uWidthType][uIndex];
1461
1462 /*
1463 * Write the VMCS component based on the field's effective width.
1464 *
1465 * The effective width is 64-bit fields adjusted to 32-bits if the access-type
1466 * indicates high bits (little endian).
1467 */
1468 uint8_t *pbField = pbVmcs + offField;
1469 uint8_t const uEffWidth = HMVmxGetVmcsFieldWidthEff(FieldEnc.u);
1470 switch (uEffWidth)
1471 {
1472 case VMX_VMCS_ENC_WIDTH_64BIT:
1473 case VMX_VMCS_ENC_WIDTH_NATURAL: *(uint64_t *)pbField = u64Val; break;
1474 case VMX_VMCS_ENC_WIDTH_32BIT: *(uint32_t *)pbField = u64Val; break;
1475 case VMX_VMCS_ENC_WIDTH_16BIT: *(uint16_t *)pbField = u64Val; break;
1476 }
1477
1478 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmwrite_Success;
1479 iemVmxVmSucceed(pVCpu);
1480 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1481 return VINF_SUCCESS;
1482}
1483
1484
1485/**
1486 * VMCLEAR instruction execution worker.
1487 *
1488 * @param pVCpu The cross context virtual CPU structure.
1489 * @param cbInstr The instruction length.
1490 * @param iEffSeg The effective segment register to use with @a GCPtrVmcs.
1491 * @param GCPtrVmcs The linear address of the VMCS pointer.
1492 * @param pExitInfo Pointer to the VM-exit information struct. Optional, can
1493 * be NULL.
1494 *
1495 * @remarks Common VMX instruction checks are already expected to by the caller,
1496 * i.e. VMX operation, CR4.VMXE, Real/V86 mode, EFER/CS.L checks.
1497 */
1498IEM_STATIC VBOXSTRICTRC iemVmxVmclear(PVMCPU pVCpu, uint8_t cbInstr, uint8_t iEffSeg, RTGCPHYS GCPtrVmcs,
1499 PCVMXVEXITINFO pExitInfo)
1500{
1501 if (IEM_IS_VMX_NON_ROOT_MODE(pVCpu))
1502 {
1503 RT_NOREF(pExitInfo);
1504 /** @todo NSTVMX: intercept. */
1505 }
1506 Assert(IEM_IS_VMX_ROOT_MODE(pVCpu));
1507
1508 /* CPL. */
1509 if (pVCpu->iem.s.uCpl > 0)
1510 {
1511 Log(("vmclear: CPL %u -> #GP(0)\n", pVCpu->iem.s.uCpl));
1512 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmclear_Cpl;
1513 return iemRaiseGeneralProtectionFault0(pVCpu);
1514 }
1515
1516 /* Get the VMCS pointer from the location specified by the source memory operand. */
1517 RTGCPHYS GCPhysVmcs;
1518 VBOXSTRICTRC rcStrict = iemMemFetchDataU64(pVCpu, &GCPhysVmcs, iEffSeg, GCPtrVmcs);
1519 if (RT_UNLIKELY(rcStrict != VINF_SUCCESS))
1520 {
1521 Log(("vmclear: Failed to read VMCS physaddr from %#RGv, rc=%Rrc\n", GCPtrVmcs, VBOXSTRICTRC_VAL(rcStrict)));
1522 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmclear_PtrMap;
1523 return rcStrict;
1524 }
1525
1526 /* VMCS pointer alignment. */
1527 if (GCPhysVmcs & X86_PAGE_4K_OFFSET_MASK)
1528 {
1529 Log(("vmclear: VMCS pointer not page-aligned -> VMFail()\n"));
1530 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmclear_PtrAlign;
1531 iemVmxVmFail(pVCpu, VMXINSTRERR_VMCLEAR_INVALID_PHYSADDR);
1532 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1533 return VINF_SUCCESS;
1534 }
1535
1536 /* VMCS physical-address width limits. */
1537 Assert(!VMX_V_VMCS_PHYSADDR_4G_LIMIT);
1538 if (GCPhysVmcs >> IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cMaxPhysAddrWidth)
1539 {
1540 Log(("vmclear: VMCS pointer extends beyond physical-address width -> VMFail()\n"));
1541 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmclear_PtrWidth;
1542 iemVmxVmFail(pVCpu, VMXINSTRERR_VMCLEAR_INVALID_PHYSADDR);
1543 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1544 return VINF_SUCCESS;
1545 }
1546
1547 /* VMCS is not the VMXON region. */
1548 if (GCPhysVmcs == pVCpu->cpum.GstCtx.hwvirt.vmx.GCPhysVmxon)
1549 {
1550 Log(("vmclear: VMCS pointer cannot be identical to VMXON region pointer -> VMFail()\n"));
1551 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmclear_PtrVmxon;
1552 iemVmxVmFail(pVCpu, VMXINSTRERR_VMCLEAR_VMXON_PTR);
1553 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1554 return VINF_SUCCESS;
1555 }
1556
1557 /* Ensure VMCS is not MMIO, ROM etc. This is not an Intel requirement but a
1558 restriction imposed by our implementation. */
1559 if (!PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), GCPhysVmcs))
1560 {
1561 Log(("vmclear: VMCS not normal memory -> VMFail()\n"));
1562 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmclear_PtrAbnormal;
1563 iemVmxVmFail(pVCpu, VMXINSTRERR_VMCLEAR_INVALID_PHYSADDR);
1564 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1565 return VINF_SUCCESS;
1566 }
1567
1568 /*
1569 * VMCLEAR allows committing and clearing any valid VMCS pointer.
1570 *
1571 * If the current VMCS is the one being cleared, set its state to 'clear' and commit
1572 * to guest memory. Otherwise, set the state of the VMCS referenced in guest memory
1573 * to 'clear'.
1574 */
1575 uint8_t const fVmcsStateClear = VMX_V_VMCS_STATE_CLEAR;
1576 if (IEM_VMX_GET_CURRENT_VMCS(pVCpu) == GCPhysVmcs)
1577 {
1578 Assert(GCPhysVmcs != NIL_RTGCPHYS); /* Paranoia. */
1579 pVCpu->cpum.GstCtx.hwvirt.vmx.CTX_SUFF(pVmcs)->fVmcsState = fVmcsStateClear;
1580 iemVmxCommitCurrentVmcsToMemory(pVCpu);
1581 Assert(!IEM_VMX_HAS_CURRENT_VMCS(pVCpu));
1582 }
1583 else
1584 {
1585 rcStrict = PGMPhysSimpleWriteGCPhys(pVCpu->CTX_SUFF(pVM), GCPtrVmcs + RT_OFFSETOF(VMXVVMCS, fVmcsState),
1586 (const void *)&fVmcsStateClear, sizeof(fVmcsStateClear));
1587 }
1588
1589 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmclear_Success;
1590 iemVmxVmSucceed(pVCpu);
1591 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1592 return rcStrict;
1593}
1594
1595
1596/**
1597 * VMPTRST instruction execution worker.
1598 *
1599 * @param pVCpu The cross context virtual CPU structure.
1600 * @param cbInstr The instruction length.
1601 * @param iEffSeg The effective segment register to use with @a GCPtrVmcs.
1602 * @param GCPtrVmcs The linear address of where to store the current VMCS
1603 * pointer.
1604 * @param pExitInfo Pointer to the VM-exit information struct. Optional, can
1605 * be NULL.
1606 *
1607 * @remarks Common VMX instruction checks are already expected to by the caller,
1608 * i.e. VMX operation, CR4.VMXE, Real/V86 mode, EFER/CS.L checks.
1609 */
1610IEM_STATIC VBOXSTRICTRC iemVmxVmptrst(PVMCPU pVCpu, uint8_t cbInstr, uint8_t iEffSeg, RTGCPHYS GCPtrVmcs,
1611 PCVMXVEXITINFO pExitInfo)
1612{
1613 if (IEM_IS_VMX_NON_ROOT_MODE(pVCpu))
1614 {
1615 RT_NOREF(pExitInfo);
1616 /** @todo NSTVMX: intercept. */
1617 }
1618 Assert(IEM_IS_VMX_ROOT_MODE(pVCpu));
1619
1620 /* CPL. */
1621 if (pVCpu->iem.s.uCpl > 0)
1622 {
1623 Log(("vmptrst: CPL %u -> #GP(0)\n", pVCpu->iem.s.uCpl));
1624 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmptrst_Cpl;
1625 return iemRaiseGeneralProtectionFault0(pVCpu);
1626 }
1627
1628 /* Set the VMCS pointer to the location specified by the destination memory operand. */
1629 AssertCompile(NIL_RTGCPHYS == ~(RTGCPHYS)0U);
1630 VBOXSTRICTRC rcStrict = iemMemStoreDataU64(pVCpu, iEffSeg, GCPtrVmcs, IEM_VMX_GET_CURRENT_VMCS(pVCpu));
1631 if (RT_LIKELY(rcStrict == VINF_SUCCESS))
1632 {
1633 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmptrst_Success;
1634 iemVmxVmSucceed(pVCpu);
1635 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1636 return rcStrict;
1637 }
1638
1639 Log(("vmptrst: Failed to store VMCS pointer to memory at destination operand %#Rrc\n", VBOXSTRICTRC_VAL(rcStrict)));
1640 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmptrst_PtrMap;
1641 return rcStrict;
1642}
1643
1644
1645/**
1646 * VMPTRLD instruction execution worker.
1647 *
1648 * @param pVCpu The cross context virtual CPU structure.
1649 * @param cbInstr The instruction length.
1650 * @param GCPtrVmcs The linear address of the current VMCS pointer.
1651 * @param pExitInfo Pointer to the VM-exit information struct. Optional, can
1652 * be NULL.
1653 *
1654 * @remarks Common VMX instruction checks are already expected to by the caller,
1655 * i.e. VMX operation, CR4.VMXE, Real/V86 mode, EFER/CS.L checks.
1656 */
1657IEM_STATIC VBOXSTRICTRC iemVmxVmptrld(PVMCPU pVCpu, uint8_t cbInstr, uint8_t iEffSeg, RTGCPHYS GCPtrVmcs,
1658 PCVMXVEXITINFO pExitInfo)
1659{
1660 if (IEM_IS_VMX_NON_ROOT_MODE(pVCpu))
1661 {
1662 RT_NOREF(pExitInfo);
1663 /** @todo NSTVMX: intercept. */
1664 }
1665 Assert(IEM_IS_VMX_ROOT_MODE(pVCpu));
1666
1667 /* CPL. */
1668 if (pVCpu->iem.s.uCpl > 0)
1669 {
1670 Log(("vmptrld: CPL %u -> #GP(0)\n", pVCpu->iem.s.uCpl));
1671 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmptrld_Cpl;
1672 return iemRaiseGeneralProtectionFault0(pVCpu);
1673 }
1674
1675 /* Get the VMCS pointer from the location specified by the source memory operand. */
1676 RTGCPHYS GCPhysVmcs;
1677 VBOXSTRICTRC rcStrict = iemMemFetchDataU64(pVCpu, &GCPhysVmcs, iEffSeg, GCPtrVmcs);
1678 if (RT_UNLIKELY(rcStrict != VINF_SUCCESS))
1679 {
1680 Log(("vmptrld: Failed to read VMCS physaddr from %#RGv, rc=%Rrc\n", GCPtrVmcs, VBOXSTRICTRC_VAL(rcStrict)));
1681 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmptrld_PtrMap;
1682 return rcStrict;
1683 }
1684
1685 /* VMCS pointer alignment. */
1686 if (GCPhysVmcs & X86_PAGE_4K_OFFSET_MASK)
1687 {
1688 Log(("vmptrld: VMCS pointer not page-aligned -> VMFail()\n"));
1689 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmptrld_PtrAlign;
1690 iemVmxVmFail(pVCpu, VMXINSTRERR_VMPTRLD_INVALID_PHYSADDR);
1691 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1692 return VINF_SUCCESS;
1693 }
1694
1695 /* VMCS physical-address width limits. */
1696 Assert(!VMX_V_VMCS_PHYSADDR_4G_LIMIT);
1697 if (GCPhysVmcs >> IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cMaxPhysAddrWidth)
1698 {
1699 Log(("vmptrld: VMCS pointer extends beyond physical-address width -> VMFail()\n"));
1700 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmptrld_PtrWidth;
1701 iemVmxVmFail(pVCpu, VMXINSTRERR_VMPTRLD_INVALID_PHYSADDR);
1702 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1703 return VINF_SUCCESS;
1704 }
1705
1706 /* VMCS is not the VMXON region. */
1707 if (GCPhysVmcs == pVCpu->cpum.GstCtx.hwvirt.vmx.GCPhysVmxon)
1708 {
1709 Log(("vmptrld: VMCS pointer cannot be identical to VMXON region pointer -> VMFail()\n"));
1710 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmptrld_PtrVmxon;
1711 iemVmxVmFail(pVCpu, VMXINSTRERR_VMPTRLD_VMXON_PTR);
1712 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1713 return VINF_SUCCESS;
1714 }
1715
1716 /* Ensure VMCS is not MMIO, ROM etc. This is not an Intel requirement but a
1717 restriction imposed by our implementation. */
1718 if (!PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), GCPhysVmcs))
1719 {
1720 Log(("vmptrld: VMCS not normal memory -> VMFail()\n"));
1721 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmptrld_PtrAbnormal;
1722 iemVmxVmFail(pVCpu, VMXINSTRERR_VMPTRLD_INVALID_PHYSADDR);
1723 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1724 return VINF_SUCCESS;
1725 }
1726
1727 /* Read the VMCS revision ID from the VMCS. */
1728 VMXVMCSREVID VmcsRevId;
1729 int rc = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), &VmcsRevId, GCPhysVmcs, sizeof(VmcsRevId));
1730 if (RT_FAILURE(rc))
1731 {
1732 Log(("vmptrld: Failed to read VMCS at %#RGp, rc=%Rrc\n", GCPhysVmcs, rc));
1733 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmptrld_PtrReadPhys;
1734 return rc;
1735 }
1736
1737 /* Verify the VMCS revision specified by the guest matches what we reported to the guest,
1738 also check VMCS shadowing feature. */
1739 if ( VmcsRevId.n.u31RevisionId != VMX_V_VMCS_REVISION_ID
1740 || ( VmcsRevId.n.fIsShadowVmcs
1741 && !IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fVmxVmcsShadowing))
1742 {
1743 if (VmcsRevId.n.u31RevisionId != VMX_V_VMCS_REVISION_ID)
1744 {
1745 Log(("vmptrld: VMCS revision mismatch, expected %#RX32 got %#RX32 -> VMFail()\n", VMX_V_VMCS_REVISION_ID,
1746 VmcsRevId.n.u31RevisionId));
1747 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmptrld_VmcsRevId;
1748 iemVmxVmFail(pVCpu, VMXINSTRERR_VMPTRLD_INCORRECT_VMCS_REV);
1749 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1750 return VINF_SUCCESS;
1751 }
1752
1753 Log(("vmptrld: Shadow VMCS -> VMFail()\n"));
1754 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmptrld_ShadowVmcs;
1755 iemVmxVmFail(pVCpu, VMXINSTRERR_VMPTRLD_INCORRECT_VMCS_REV);
1756 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1757 return VINF_SUCCESS;
1758 }
1759
1760 /*
1761 * We only maintain only the current VMCS in our virtual CPU context (CPUMCTX). Therefore,
1762 * VMPTRLD shall always flush any existing current VMCS back to guest memory before loading
1763 * a new VMCS as current.
1764 */
1765 if (IEM_VMX_GET_CURRENT_VMCS(pVCpu) != GCPhysVmcs)
1766 {
1767 iemVmxCommitCurrentVmcsToMemory(pVCpu);
1768 IEM_VMX_SET_CURRENT_VMCS(pVCpu, GCPhysVmcs);
1769 }
1770 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmptrld_Success;
1771 iemVmxVmSucceed(pVCpu);
1772 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1773 return VINF_SUCCESS;
1774}
1775
1776
1777/**
1778 * VMXON instruction execution worker.
1779 *
1780 * @param pVCpu The cross context virtual CPU structure.
1781 * @param cbInstr The instruction length.
1782 * @param iEffSeg The effective segment register to use with @a
1783 * GCPtrVmxon.
1784 * @param GCPtrVmxon The linear address of the VMXON pointer.
1785 * @param pExitInfo Pointer to the VM-exit instruction information struct.
1786 * Optional, can be NULL.
1787 *
1788 * @remarks Common VMX instruction checks are already expected to by the caller,
1789 * i.e. CR4.VMXE, Real/V86 mode, EFER/CS.L checks.
1790 */
1791IEM_STATIC VBOXSTRICTRC iemVmxVmxon(PVMCPU pVCpu, uint8_t cbInstr, uint8_t iEffSeg, RTGCPHYS GCPtrVmxon,
1792 PCVMXVEXITINFO pExitInfo)
1793{
1794#if defined(VBOX_WITH_NESTED_HWVIRT_ONLY_IN_IEM) && !defined(IN_RING3)
1795 RT_NOREF5(pVCpu, cbInstr, iEffSeg, GCPtrVmxon, pExitInfo);
1796 return VINF_EM_RAW_EMULATE_INSTR;
1797#else
1798 if (!IEM_IS_VMX_ROOT_MODE(pVCpu))
1799 {
1800 /* CPL. */
1801 if (pVCpu->iem.s.uCpl > 0)
1802 {
1803 Log(("vmxon: CPL %u -> #GP(0)\n", pVCpu->iem.s.uCpl));
1804 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmxon_Cpl;
1805 return iemRaiseGeneralProtectionFault0(pVCpu);
1806 }
1807
1808 /* A20M (A20 Masked) mode. */
1809 if (!PGMPhysIsA20Enabled(pVCpu))
1810 {
1811 Log(("vmxon: A20M mode -> #GP(0)\n"));
1812 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmxon_A20M;
1813 return iemRaiseGeneralProtectionFault0(pVCpu);
1814 }
1815
1816 /* CR0 fixed bits. */
1817 bool const fUnrestrictedGuest = IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fVmxUnrestrictedGuest;
1818 uint64_t const uCr0Fixed0 = fUnrestrictedGuest ? VMX_V_CR0_FIXED0_UX : VMX_V_CR0_FIXED0;
1819 if ((pVCpu->cpum.GstCtx.cr0 & uCr0Fixed0) != uCr0Fixed0)
1820 {
1821 Log(("vmxon: CR0 fixed0 bits cleared -> #GP(0)\n"));
1822 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmxon_Cr0Fixed0;
1823 return iemRaiseGeneralProtectionFault0(pVCpu);
1824 }
1825
1826 /* CR4 fixed bits. */
1827 if ((pVCpu->cpum.GstCtx.cr4 & VMX_V_CR4_FIXED0) != VMX_V_CR4_FIXED0)
1828 {
1829 Log(("vmxon: CR4 fixed0 bits cleared -> #GP(0)\n"));
1830 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmxon_Cr4Fixed0;
1831 return iemRaiseGeneralProtectionFault0(pVCpu);
1832 }
1833
1834 /* Feature control MSR's LOCK and VMXON bits. */
1835 uint64_t const uMsrFeatCtl = CPUMGetGuestIa32FeatureControl(pVCpu);
1836 if (!(uMsrFeatCtl & (MSR_IA32_FEATURE_CONTROL_LOCK | MSR_IA32_FEATURE_CONTROL_VMXON)))
1837 {
1838 Log(("vmxon: Feature control lock bit or VMXON bit cleared -> #GP(0)\n"));
1839 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmxon_MsrFeatCtl;
1840 return iemRaiseGeneralProtectionFault0(pVCpu);
1841 }
1842
1843 /* Get the VMXON pointer from the location specified by the source memory operand. */
1844 RTGCPHYS GCPhysVmxon;
1845 VBOXSTRICTRC rcStrict = iemMemFetchDataU64(pVCpu, &GCPhysVmxon, iEffSeg, GCPtrVmxon);
1846 if (RT_UNLIKELY(rcStrict != VINF_SUCCESS))
1847 {
1848 Log(("vmxon: Failed to read VMXON region physaddr from %#RGv, rc=%Rrc\n", GCPtrVmxon, VBOXSTRICTRC_VAL(rcStrict)));
1849 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmxon_PtrMap;
1850 return rcStrict;
1851 }
1852
1853 /* VMXON region pointer alignment. */
1854 if (GCPhysVmxon & X86_PAGE_4K_OFFSET_MASK)
1855 {
1856 Log(("vmxon: VMXON region pointer not page-aligned -> VMFailInvalid\n"));
1857 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmxon_PtrAlign;
1858 iemVmxVmFailInvalid(pVCpu);
1859 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1860 return VINF_SUCCESS;
1861 }
1862
1863 /* VMXON physical-address width limits. */
1864 Assert(!VMX_V_VMCS_PHYSADDR_4G_LIMIT);
1865 if (GCPhysVmxon >> IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cMaxPhysAddrWidth)
1866 {
1867 Log(("vmxon: VMXON region pointer extends beyond physical-address width -> VMFailInvalid\n"));
1868 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmxon_PtrWidth;
1869 iemVmxVmFailInvalid(pVCpu);
1870 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1871 return VINF_SUCCESS;
1872 }
1873
1874 /* Ensure VMXON region is not MMIO, ROM etc. This is not an Intel requirement but a
1875 restriction imposed by our implementation. */
1876 if (!PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), GCPhysVmxon))
1877 {
1878 Log(("vmxon: VMXON region not normal memory -> VMFailInvalid\n"));
1879 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmxon_PtrAbnormal;
1880 iemVmxVmFailInvalid(pVCpu);
1881 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1882 return VINF_SUCCESS;
1883 }
1884
1885 /* Read the VMCS revision ID from the VMXON region. */
1886 VMXVMCSREVID VmcsRevId;
1887 int rc = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), &VmcsRevId, GCPhysVmxon, sizeof(VmcsRevId));
1888 if (RT_FAILURE(rc))
1889 {
1890 Log(("vmxon: Failed to read VMXON region at %#RGp, rc=%Rrc\n", GCPhysVmxon, rc));
1891 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmxon_PtrReadPhys;
1892 return rc;
1893 }
1894
1895 /* Verify the VMCS revision specified by the guest matches what we reported to the guest. */
1896 if (RT_UNLIKELY(VmcsRevId.u != VMX_V_VMCS_REVISION_ID))
1897 {
1898 /* Revision ID mismatch. */
1899 if (!VmcsRevId.n.fIsShadowVmcs)
1900 {
1901 Log(("vmxon: VMCS revision mismatch, expected %#RX32 got %#RX32 -> VMFailInvalid\n", VMX_V_VMCS_REVISION_ID,
1902 VmcsRevId.n.u31RevisionId));
1903 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmxon_VmcsRevId;
1904 iemVmxVmFailInvalid(pVCpu);
1905 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1906 return VINF_SUCCESS;
1907 }
1908
1909 /* Shadow VMCS disallowed. */
1910 Log(("vmxon: Shadow VMCS -> VMFailInvalid\n"));
1911 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmxon_ShadowVmcs;
1912 iemVmxVmFailInvalid(pVCpu);
1913 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1914 return VINF_SUCCESS;
1915 }
1916
1917 /*
1918 * Record that we're in VMX operation, block INIT, block and disable A20M.
1919 */
1920 pVCpu->cpum.GstCtx.hwvirt.vmx.GCPhysVmxon = GCPhysVmxon;
1921 IEM_VMX_CLEAR_CURRENT_VMCS(pVCpu);
1922 pVCpu->cpum.GstCtx.hwvirt.vmx.fInVmxRootMode = true;
1923 /** @todo NSTVMX: clear address-range monitoring. */
1924 /** @todo NSTVMX: Intel PT. */
1925 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmxon_Success;
1926 iemVmxVmSucceed(pVCpu);
1927 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1928# if defined(VBOX_WITH_NESTED_HWVIRT_ONLY_IN_IEM) && defined(IN_RING3)
1929 return EMR3SetExecutionPolicy(pVCpu->CTX_SUFF(pVM)->pUVM, EMEXECPOLICY_IEM_ALL, true);
1930# else
1931 return VINF_SUCCESS;
1932# endif
1933 }
1934 else if (IEM_IS_VMX_NON_ROOT_MODE(pVCpu))
1935 {
1936 RT_NOREF(pExitInfo);
1937 /** @todo NSTVMX: intercept. */
1938 }
1939
1940 Assert(IEM_IS_VMX_ROOT_MODE(pVCpu));
1941
1942 /* CPL. */
1943 if (pVCpu->iem.s.uCpl > 0)
1944 {
1945 Log(("vmxon: In VMX root mode: CPL %u -> #GP(0)\n", pVCpu->iem.s.uCpl));
1946 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmxon_VmxRootCpl;
1947 return iemRaiseGeneralProtectionFault0(pVCpu);
1948 }
1949
1950 /* VMXON when already in VMX root mode. */
1951 iemVmxVmFail(pVCpu, VMXINSTRERR_VMXON_IN_VMXROOTMODE);
1952 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmxon_VmxAlreadyRoot;
1953 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1954 return VINF_SUCCESS;
1955#endif
1956}
1957
1958
1959/**
1960 * Checks VM-entry controls fields as part of VM-entry.
1961 * See Intel spec. 26.2.1.3 "VM-Entry Control Fields".
1962 *
1963 * @returns VBox status code.
1964 * @param pVCpu The cross context virtual CPU structure.
1965 * @param pszInstr The VMX instruction name (for logging purposes).
1966 */
1967IEM_STATIC VBOXSTRICTRC iemVmxVmentryCheckEntryCtls(PVMCPU pVCpu, const char *pszInstr)
1968{
1969 PCVMXVVMCS pVmcs = pVCpu->cpum.GstCtx.hwvirt.vmx.CTX_SUFF(pVmcs);
1970
1971 /* VM-entry controls. */
1972 VMXCTLSMSR EntryCtls;
1973 EntryCtls.u = CPUMGetGuestIa32VmxEntryCtls(pVCpu);
1974 if (~pVmcs->u32EntryCtls & EntryCtls.n.disallowed0)
1975 {
1976 Log(("%s: Invalid EntryCtls %#RX32 (disallowed0) -> VMFail\n", pszInstr, pVmcs->u32EntryCtls));
1977 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_EntryCtlsDisallowed0;
1978 return VERR_VMX_VMENTRY_FAILED;
1979 }
1980 if (pVmcs->u32EntryCtls & ~EntryCtls.n.allowed1)
1981 {
1982 Log(("%s: Invalid EntryCtls %#RX32 (allowed1) -> VMFail\n", pszInstr, pVmcs->u32EntryCtls));
1983 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_EntryCtlsAllowed1;
1984 return VERR_VMX_VMENTRY_FAILED;
1985 }
1986
1987 /** @todo NSTVMX: rest of entry ctls. */
1988
1989 /* VM-entry MSR-load count and VM-entry MSR-load area address. */
1990 uint8_t const cMaxPhysAddrWidth = IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cMaxPhysAddrWidth;
1991 if (pVmcs->u32EntryMsrLoadCount)
1992 {
1993 if ( (pVmcs->u64AddrEntryMsrLoad.u & VMX_AUTOMSR_OFFSET_MASK)
1994 || (pVmcs->u64AddrEntryMsrLoad.u >> cMaxPhysAddrWidth)
1995 || !PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), pVmcs->u64AddrEntryMsrLoad.u))
1996 {
1997 Log(("%s: VM-entry MSR-load area address %#RX64 invalid -> VMFail\n", pszInstr, pVmcs->u64AddrEntryMsrLoad.u));
1998 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_AddrEntryMsrLoad;
1999 return VERR_VMX_VMENTRY_FAILED;
2000 }
2001 }
2002
2003 Assert(!(pVmcs->u32EntryCtls & VMX_ENTRY_CTLS_ENTRY_TO_SMM)); /* We don't support SMM yet. */
2004 Assert(!(pVmcs->u32EntryCtls & VMX_ENTRY_CTLS_DEACTIVATE_DUAL_MON)); /* We don't support dual-monitor treatment yet. */
2005
2006 NOREF(pszInstr);
2007 return VINF_SUCCESS;
2008}
2009
2010
2011/**
2012 * Checks VM-exit controls fields as part of VM-entry.
2013 * See Intel spec. 26.2.1.2 "VM-Exit Control Fields".
2014 *
2015 * @returns VBox status code.
2016 * @param pVCpu The cross context virtual CPU structure.
2017 * @param pszInstr The VMX instruction name (for logging purposes).
2018 */
2019IEM_STATIC VBOXSTRICTRC iemVmxVmentryCheckExitCtls(PVMCPU pVCpu, const char *pszInstr)
2020{
2021 PCVMXVVMCS pVmcs = pVCpu->cpum.GstCtx.hwvirt.vmx.CTX_SUFF(pVmcs);
2022
2023 /* VM-exit controls. */
2024 VMXCTLSMSR ExitCtls;
2025 ExitCtls.u = CPUMGetGuestIa32VmxExitCtls(pVCpu);
2026 if (~pVmcs->u32ExitCtls & ExitCtls.n.disallowed0)
2027 {
2028 Log(("%s: Invalid ExitCtls %#RX32 (disallowed0) -> VMFail\n", pszInstr, pVmcs->u32ExitCtls));
2029 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_ExitCtlsDisallowed0;
2030 return VERR_VMX_VMENTRY_FAILED;
2031 }
2032 if (pVmcs->u32ExitCtls & ~ExitCtls.n.allowed1)
2033 {
2034 Log(("%s: Invalid ExitCtls %#RX32 (allowed1) -> VMFail\n", pszInstr, pVmcs->u32ExitCtls));
2035 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_ExitCtlsAllowed1;
2036 return VERR_VMX_VMENTRY_FAILED;
2037 }
2038
2039 /* Save preemption timer without activating it. */
2040 if ( !(pVmcs->u32PinCtls & VMX_PIN_CTLS_PREEMPT_TIMER)
2041 && (pVmcs->u32ProcCtls & VMX_EXIT_CTLS_SAVE_PREEMPT_TIMER))
2042 {
2043 Log(("%s: Save Preempt-Timer without activate Preempt timer -> VMFail\n", pszInstr, pVmcs->u32ExitCtls));
2044 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_SavePreemptTimer;
2045 return VERR_VMX_VMENTRY_FAILED;
2046 }
2047
2048 /* VM-exit MSR-store count and VM-exit MSR-store area address. */
2049 uint8_t const cMaxPhysAddrWidth = IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cMaxPhysAddrWidth;
2050 if (pVmcs->u32ExitMsrStoreCount)
2051 {
2052 if ( (pVmcs->u64AddrExitMsrStore.u & VMX_AUTOMSR_OFFSET_MASK)
2053 || (pVmcs->u64AddrExitMsrStore.u >> cMaxPhysAddrWidth)
2054 || !PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), pVmcs->u64AddrExitMsrStore.u))
2055 {
2056 Log(("%s: VM-exit MSR-store area address %#RX64 invalid -> VMFail\n", pszInstr, pVmcs->u64AddrExitMsrStore.u));
2057 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_AddrExitMsrStore;
2058 return VERR_VMX_VMENTRY_FAILED;
2059 }
2060 }
2061
2062 /* VM-exit MSR-load count and VM-exit MSR-load area address. */
2063 if (pVmcs->u32ExitMsrLoadCount)
2064 {
2065 if ( (pVmcs->u64AddrExitMsrLoad.u & VMX_AUTOMSR_OFFSET_MASK)
2066 || (pVmcs->u64AddrExitMsrLoad.u >> cMaxPhysAddrWidth)
2067 || !PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), pVmcs->u64AddrExitMsrLoad.u))
2068 {
2069 Log(("%s: VM-exit MSR-store area address %#RX64 invalid -> VMFail\n", pszInstr, pVmcs->u64AddrExitMsrLoad.u));
2070 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_AddrExitMsrLoad;
2071 return VERR_VMX_VMENTRY_FAILED;
2072 }
2073 }
2074
2075 NOREF(pszInstr);
2076 return VINF_SUCCESS;
2077}
2078
2079
2080/**
2081 * Checks VM-execution controls fields as part of VM-entry.
2082 * See Intel spec. 26.2.1.1 "VM-Execution Control Fields".
2083 *
2084 * @returns VBox status code.
2085 * @param pVCpu The cross context virtual CPU structure.
2086 * @param pszInstr The VMX instruction name (for logging purposes).
2087 */
2088IEM_STATIC VBOXSTRICTRC iemVmxVmentryCheckExecCtls(PVMCPU pVCpu, const char *pszInstr)
2089{
2090 PVMXVVMCS pVmcs = pVCpu->cpum.GstCtx.hwvirt.vmx.CTX_SUFF(pVmcs);
2091 /* Pin-based VM-execution controls. */
2092 {
2093 VMXCTLSMSR PinCtls;
2094 PinCtls.u = CPUMGetGuestIa32VmxPinbasedCtls(pVCpu);
2095 if (~pVmcs->u32PinCtls & PinCtls.n.disallowed0)
2096 {
2097 Log(("%s: Invalid PinCtls %#RX32 (disallowed0) -> VMFail\n", pszInstr, pVmcs->u32PinCtls));
2098 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_PinCtlsDisallowed0;
2099 return VERR_VMX_VMENTRY_FAILED;
2100 }
2101 if (pVmcs->u32PinCtls & ~PinCtls.n.allowed1)
2102 {
2103 Log(("%s: Invalid PinCtls %#RX32 (allowed1) -> VMFail\n", pszInstr, pVmcs->u32PinCtls));
2104 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_PinCtlsAllowed1;
2105 return VERR_VMX_VMENTRY_FAILED;
2106 }
2107 }
2108
2109 /* Processor-based VM-execution controls. */
2110 {
2111 VMXCTLSMSR ProcCtls;
2112 ProcCtls.u = CPUMGetGuestIa32VmxProcbasedCtls(pVCpu);
2113 if (~pVmcs->u32ProcCtls & ProcCtls.n.disallowed0)
2114 {
2115 Log(("%s: Invalid ProcCtls %#RX32 (disallowed0) -> VMFail\n", pszInstr, pVmcs->u32ProcCtls));
2116 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_ProcCtlsDisallowed0;
2117 return VERR_VMX_VMENTRY_FAILED;
2118 }
2119 if (pVmcs->u32ProcCtls & ~ProcCtls.n.allowed1)
2120 {
2121 Log(("%s: Invalid ProcCtls %#RX32 (allowed1) -> VMFail\n", pszInstr, pVmcs->u32ProcCtls));
2122 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_ProcCtlsAllowed1;
2123 return VERR_VMX_VMENTRY_FAILED;
2124 }
2125 }
2126
2127 /* Secondary processor-based VM-execution controls. */
2128 if (pVmcs->u32ProcCtls & VMX_PROC_CTLS_USE_SECONDARY_CTLS)
2129 {
2130 VMXCTLSMSR ProcCtls2;
2131 ProcCtls2.u = CPUMGetGuestIa32VmxProcbasedCtls(pVCpu);
2132 if (~pVmcs->u32ProcCtls2 & ProcCtls2.n.disallowed0)
2133 {
2134 Log(("%s: Invalid ProcCtls2 %#RX32 (disallowed0) -> VMFail\n", pszInstr, pVmcs->u32ProcCtls2));
2135 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_ProcCtls2Disallowed0;
2136 return VERR_VMX_VMENTRY_FAILED;
2137 }
2138 if (pVmcs->u32ProcCtls2 & ~ProcCtls2.n.allowed1)
2139 {
2140 Log(("%s: Invalid ProcCtls2 %#RX32 (allowed1) -> VMFail\n", pszInstr, pVmcs->u32ProcCtls2));
2141 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_ProcCtls2Allowed1;
2142 return VERR_VMX_VMENTRY_FAILED;
2143 }
2144 }
2145 else
2146 pVmcs->u32ProcCtls2 = 0;
2147
2148 /* CR3-target count. */
2149 if (pVmcs->u32Cr3TargetCount > VMX_V_CR3_TARGET_COUNT)
2150 {
2151 Log(("%s: CR3-target count exceeded %#x -> VMFail\n", pszInstr, pVmcs->u32Cr3TargetCount));
2152 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_Cr3TargetCount;
2153 return VERR_VMX_VMENTRY_FAILED;
2154 }
2155
2156 /* IO bitmaps physical addresses. */
2157 uint8_t const cMaxPhysAddrWidth = IEM_GET_GUEST_CPU_FEATURES(pVCpu)->cMaxPhysAddrWidth;
2158 Assert(!VMX_V_VMCS_PHYSADDR_4G_LIMIT);
2159 if (pVmcs->u32ProcCtls & VMX_PROC_CTLS_USE_IO_BITMAPS)
2160 {
2161 if ( (pVmcs->u64AddrIoBitmapA.u & X86_PAGE_4K_OFFSET_MASK)
2162 || (pVmcs->u64AddrIoBitmapA.u >> cMaxPhysAddrWidth)
2163 || !PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), pVmcs->u64AddrIoBitmapA.u))
2164 {
2165 Log(("%s: I/O Bitmap A physaddr invalid %#RX64 -> VMFail\n", pszInstr, pVmcs->u64AddrIoBitmapA.u));
2166 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_AddrIoBitmapA;
2167 return VERR_VMX_VMENTRY_FAILED;
2168 }
2169
2170 if ( (pVmcs->u64AddrIoBitmapB.u & X86_PAGE_4K_OFFSET_MASK)
2171 || (pVmcs->u64AddrIoBitmapB.u >> cMaxPhysAddrWidth)
2172 || !PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), pVmcs->u64AddrIoBitmapB.u))
2173 {
2174 Log(("%s: I/O Bitmap B physaddr invalid %#RX64 -> VMFail\n", pszInstr, pVmcs->u64AddrIoBitmapB.u));
2175 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_AddrIoBitmapB;
2176 return VERR_VMX_VMENTRY_FAILED;
2177 }
2178 }
2179
2180 /* MSR bitmap physical address. */
2181 if (pVmcs->u32ProcCtls & VMX_PROC_CTLS_USE_MSR_BITMAPS)
2182 {
2183 if ( (pVmcs->u64AddrMsrBitmap.u & X86_PAGE_4K_OFFSET_MASK)
2184 || (pVmcs->u64AddrMsrBitmap.u >> cMaxPhysAddrWidth)
2185 || !PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), pVmcs->u64AddrMsrBitmap.u))
2186 {
2187 Log(("%s: MSR Bitmap physaddr invalid %#RX64 -> VMFail\n", pszInstr, pVmcs->u64AddrMsrBitmap.u));
2188 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_AddrMsrBitmap;
2189 return VERR_VMX_VMENTRY_FAILED;
2190 }
2191 }
2192
2193 /* TPR shadow related controls. */
2194 if (pVmcs->u32ProcCtls & VMX_PROC_CTLS_USE_TPR_SHADOW)
2195 {
2196 /* Virtual-APIC page physical address. */
2197 RTGCPHYS GCPhysVirtApic = pVmcs->u64AddrVirtApic.u;
2198 if ( (GCPhysVirtApic & X86_PAGE_4K_OFFSET_MASK)
2199 || (GCPhysVirtApic >> cMaxPhysAddrWidth)
2200 || !PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), GCPhysVirtApic))
2201 {
2202 Log(("%s: Virtual-APIC page physaddr invalid %#RX64 -> VMFail\n", pszInstr, GCPhysVirtApic));
2203 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_AddrVirtApicPage;
2204 return VERR_VMX_VMENTRY_FAILED;
2205 }
2206
2207 /* Read the Virtual-APIC page. */
2208 int rc = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), pVCpu->cpum.GstCtx.hwvirt.vmx.CTX_SUFF(pvVirtApicPage),
2209 GCPhysVirtApic, VMX_V_VIRT_APIC_PAGES);
2210 if (RT_FAILURE(rc))
2211 {
2212 Log(("%s: Failed to read Virtual-APIC page at %#RGp, rc=%Rrc\n", pszInstr, GCPhysVirtApic, rc));
2213 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_VirtApicPagePtrReadPhys;
2214 return rc;
2215 }
2216
2217 /* TPR threshold without virtual-interrupt delivery. */
2218 if ( !(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_INT_DELIVERY)
2219 && (pVmcs->u32TprThreshold & VMX_TPR_THRESHOLD_MASK))
2220 {
2221 Log(("%s: TPR-threshold (%#RX32) invalid -> VMFail\n", pszInstr, pVmcs->u32TprThreshold));
2222 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_TprThreshold;
2223 return VERR_VMX_VMENTRY_FAILED;
2224 }
2225
2226 /* TPR threshold and VTPR. */
2227 uint8_t const *pbVirtApic = (uint8_t *)pVCpu->cpum.GstCtx.hwvirt.vmx.CTX_SUFF(pvVirtApicPage);
2228 uint8_t const u8VTpr = *(pbVirtApic + XAPIC_OFF_TPR);
2229 if ( !(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_APIC_ACCESS)
2230 && !(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_INT_DELIVERY)
2231 && RT_BF_GET(pVmcs->u32TprThreshold, VMX_BF_TPR_THRESHOLD_TPR) > ((u8VTpr >> 4) & UINT32_C(0xf)) /* Bits 4:7 */)
2232 {
2233 Log(("%s: TPR-threshold (%#x) exceeds VTPR (%#x) -> VMFail\n", pszInstr,
2234 (pVmcs->u32TprThreshold & VMX_TPR_THRESHOLD_MASK), u8VTpr));
2235 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_TprThresholdVTpr;
2236 return VERR_VMX_VMENTRY_FAILED;
2237 }
2238 }
2239 else
2240 {
2241 if ( !(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_X2APIC_MODE)
2242 && !(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_APIC_REG_VIRT)
2243 && !(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_INT_DELIVERY))
2244 { /* likely */ }
2245 else
2246 {
2247 if (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_X2APIC_MODE)
2248 {
2249 Log(("%s: Virtualize x2APIC access without TPR shadowing -> VMFail\n", pszInstr));
2250 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_VirtX2ApicTprShadow;
2251 return VERR_VMX_VMENTRY_FAILED;
2252 }
2253 else if (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_APIC_REG_VIRT)
2254 {
2255 Log(("%s: APIC-register virtualization without TPR shadowing -> VMFail\n", pszInstr));
2256 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_ApicRegVirt;
2257 return VERR_VMX_VMENTRY_FAILED;
2258 }
2259 else
2260 {
2261 Assert(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_INT_DELIVERY);
2262 Log(("%s: Virtual-interrupt delivery without TPR shadowing -> VMFail\n", pszInstr));
2263 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_VirtIntDelivery;
2264 return VERR_VMX_VMENTRY_FAILED;
2265 }
2266 }
2267 }
2268
2269 /* NMI exiting and virtual-NMIs. */
2270 if ( !(pVmcs->u32PinCtls & VMX_PIN_CTLS_NMI_EXIT)
2271 && (pVmcs->u32PinCtls & VMX_PIN_CTLS_VIRT_NMI))
2272 {
2273 Log(("%s: Virtual-NMIs invalid without NMI-exiting -> VMFail\n", pszInstr));
2274 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_VirtNmi;
2275 return VERR_VMX_VMENTRY_FAILED;
2276 }
2277
2278 /* Virtual-NMIs and NMI-window exiting. */
2279 if ( !(pVmcs->u32PinCtls & VMX_PIN_CTLS_VIRT_NMI)
2280 && (pVmcs->u32ProcCtls & VMX_PROC_CTLS_NMI_WINDOW_EXIT))
2281 {
2282 Log(("%s: NMI-window exiting invalid without virtual-NMIs -> VMFail\n", pszInstr));
2283 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_NmiWindowExit;
2284 return VERR_VMX_VMENTRY_FAILED;
2285 }
2286
2287 /* Virtualize APIC accesses. */
2288 if (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_APIC_ACCESS)
2289 {
2290 /* APIC-access physical address. */
2291 RTGCPHYS GCPhysApicAccess = pVmcs->u64AddrApicAccess.u;
2292 if ( (GCPhysApicAccess & X86_PAGE_4K_OFFSET_MASK)
2293 || (GCPhysApicAccess >> cMaxPhysAddrWidth)
2294 || !PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), GCPhysApicAccess))
2295 {
2296 Log(("%s: APIC-access address invalid %#RX64 -> VMFail\n", pszInstr, GCPhysApicAccess));
2297 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_AddrApicAccess;
2298 return VERR_VMX_VMENTRY_FAILED;
2299 }
2300 }
2301
2302 /* Virtualize-x2APIC mode is mutually exclusive with virtualize-APIC accesses. */
2303 if ( (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_X2APIC_MODE)
2304 && (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_APIC_ACCESS))
2305 {
2306 Log(("%s: Virtualize-APIC access when virtualize-x2APIC mode is enabled -> VMFail", pszInstr));
2307 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_VirtX2ApicVirtApic;
2308 return VERR_VMX_VMENTRY_FAILED;
2309 }
2310
2311 /* Virtual-interrupt delivery requires external interrupt exiting. */
2312 if ( (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VIRT_INT_DELIVERY)
2313 && !(pVmcs->u32PinCtls & VMX_PIN_CTLS_EXT_INT_EXIT))
2314 {
2315 Log(("%s: Virtual-interrupt delivery without external interrupt exiting -> VMFail\n", pszInstr));
2316 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_VirtX2ApicVirtApic;
2317 return VERR_VMX_VMENTRY_FAILED;
2318 }
2319
2320 /* VPID. */
2321 if ( (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VPID)
2322 && pVmcs->u16Vpid == 0)
2323 {
2324 Log(("%s: VPID invalid -> VMFail\n", pszInstr));
2325 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_Vpid;
2326 return VERR_VMX_VMENTRY_FAILED;
2327 }
2328
2329 Assert(!(pVmcs->u32PinCtls & VMX_PIN_CTLS_POSTED_INT)); /* We don't support posted interrupts yet. */
2330 Assert(!(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_EPT)); /* We don't support EPT yet. */
2331 Assert(!(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_PML)); /* We don't support PML yet. */
2332 Assert(!(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_UNRESTRICTED_GUEST)); /* We don't support Unrestricted-guests yet. */
2333 Assert(!(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VMFUNC)); /* We don't support VM functions yet. */
2334 Assert(!(pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_EPT_VE)); /* We don't support EPT-violation #VE yet. */
2335
2336 /* VMCS shadowing. */
2337 if (pVmcs->u32ProcCtls2 & VMX_PROC_CTLS2_VMCS_SHADOWING)
2338 {
2339 /* VMREAD-bitmap physical address. */
2340 RTGCPHYS GCPhysVmreadBitmap = pVmcs->u64AddrVmreadBitmap.u;
2341 if ( ( GCPhysVmreadBitmap & X86_PAGE_4K_OFFSET_MASK)
2342 || ( GCPhysVmreadBitmap >> cMaxPhysAddrWidth)
2343 || !PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), GCPhysVmreadBitmap))
2344 {
2345 Log(("%s: VMREAD-bitmap address invalid %#RX64 -> VMFail\n", pszInstr, GCPhysVmreadBitmap));
2346 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_AddrVmreadBitmap;
2347 return VERR_VMX_VMENTRY_FAILED;
2348 }
2349
2350 /* VMWRITE-bitmap physical address. */
2351 RTGCPHYS GCPhysVmwriteBitmap = pVmcs->u64AddrVmreadBitmap.u;
2352 if ( ( GCPhysVmwriteBitmap & X86_PAGE_4K_OFFSET_MASK)
2353 || ( GCPhysVmwriteBitmap >> cMaxPhysAddrWidth)
2354 || !PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), GCPhysVmwriteBitmap))
2355 {
2356 Log(("%s: VMWRITE-bitmap address invalid %#RX64 -> VMFail\n", pszInstr, GCPhysVmwriteBitmap));
2357 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_AddrVmwriteBitmap;
2358 return VERR_VMX_VMENTRY_FAILED;
2359 }
2360
2361 /* Read the VMREAD-bitmap. */
2362 int rc = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), pVCpu->cpum.GstCtx.hwvirt.vmx.CTX_SUFF(pvVmreadBitmap),
2363 GCPhysVmreadBitmap, VMX_V_VMREAD_VMWRITE_BITMAP_SIZE);
2364 if (RT_FAILURE(rc))
2365 {
2366 Log(("%s: Failed to read VMREAD-bitmap at %#RGp, rc=%Rrc\n", pszInstr, GCPhysVmreadBitmap, rc));
2367 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_VmreadBitmapPtrReadPhys;
2368 return rc;
2369 }
2370
2371 /* Read the VMWRITE-bitmap. */
2372 rc = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), pVCpu->cpum.GstCtx.hwvirt.vmx.CTX_SUFF(pvVmwriteBitmap),
2373 GCPhysVmwriteBitmap, VMX_V_VMREAD_VMWRITE_BITMAP_SIZE);
2374 if (RT_FAILURE(rc))
2375 {
2376 Log(("%s: Failed to read VMWRITE-bitmap at %#RGp, rc=%Rrc\n", pszInstr, GCPhysVmwriteBitmap, rc));
2377 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_VmwriteBitmapPtrReadPhys;
2378 return rc;
2379 }
2380 }
2381
2382 NOREF(pszInstr);
2383 return VINF_SUCCESS;
2384}
2385
2386
2387/**
2388 * VMLAUNCH/VMRESUME instruction execution worker.
2389 *
2390 * @param pVCpu The cross context virtual CPU structure.
2391 * @param cbInstr The instruction length.
2392 * @param uInstrId The instruction identity (either VMXINSTRID_VMLAUNCH or
2393 * VMXINSTRID_VMRESUME).
2394 * @param pExitInfo Pointer to the VM-exit instruction information struct.
2395 * Optional, can be NULL.
2396 *
2397 * @remarks Common VMX instruction checks are already expected to by the caller,
2398 * i.e. CR4.VMXE, Real/V86 mode, EFER/CS.L checks.
2399 */
2400IEM_STATIC VBOXSTRICTRC iemVmxVmlaunchVmresume(PVMCPU pVCpu, uint8_t cbInstr, VMXINSTRID uInstrId, PCVMXVEXITINFO pExitInfo)
2401{
2402 Assert( uInstrId == VMXINSTRID_VMLAUNCH
2403 || uInstrId == VMXINSTRID_VMRESUME);
2404
2405 const char *pszInstr = uInstrId == VMXINSTRID_VMLAUNCH ? "vmlaunch" : "vmresume";
2406 if (IEM_IS_VMX_NON_ROOT_MODE(pVCpu))
2407 {
2408 RT_NOREF(pExitInfo);
2409 /** @todo NSTVMX: intercept. */
2410 }
2411 Assert(IEM_IS_VMX_ROOT_MODE(pVCpu));
2412
2413 /* CPL. */
2414 if (pVCpu->iem.s.uCpl > 0)
2415 {
2416 Log(("%s: CPL %u -> #GP(0)\n", pszInstr, pVCpu->iem.s.uCpl));
2417 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_Cpl;
2418 return iemRaiseGeneralProtectionFault0(pVCpu);
2419 }
2420
2421 /* Current VMCS valid. */
2422 if (!IEM_VMX_HAS_CURRENT_VMCS(pVCpu))
2423 {
2424 Log(("%s: VMCS pointer %#RGp invalid -> VMFailInvalid\n", pszInstr, IEM_VMX_GET_CURRENT_VMCS(pVCpu)));
2425 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_PtrInvalid;
2426 iemVmxVmFailInvalid(pVCpu);
2427 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
2428 return VINF_SUCCESS;
2429 }
2430
2431 /** @todo Distinguish block-by-MOV-SS from block-by-STI. Currently we
2432 * use block-by-STI here which is not quite correct. */
2433 if ( VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS)
2434 && pVCpu->cpum.GstCtx.rip == EMGetInhibitInterruptsPC(pVCpu))
2435 {
2436 Log(("%s: VM entry with events blocked by MOV SS -> VMFail\n", pszInstr));
2437 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_BlocKMovSS;
2438 iemVmxVmFail(pVCpu, VMXINSTRERR_VMENTRY_BLOCK_MOVSS);
2439 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
2440 return VINF_SUCCESS;
2441 }
2442
2443 if (uInstrId == VMXINSTRID_VMLAUNCH)
2444 {
2445 /* VMLAUNCH with non-clear VMCS. */
2446 if (pVCpu->cpum.GstCtx.hwvirt.vmx.CTX_SUFF(pVmcs)->fVmcsState != VMX_V_VMCS_STATE_CLEAR)
2447 {
2448 Log(("vmlaunch: VMLAUNCH with non-clear VMCS -> VMFail\n"));
2449 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_VmcsClear;
2450 iemVmxVmFail(pVCpu, VMXINSTRERR_VMLAUNCH_NON_CLEAR_VMCS);
2451 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
2452 return VINF_SUCCESS;
2453 }
2454 }
2455 else
2456 {
2457 /* VMRESUME with non-launched VMCS. */
2458 if (pVCpu->cpum.GstCtx.hwvirt.vmx.CTX_SUFF(pVmcs)->fVmcsState != VMX_V_VMCS_STATE_LAUNCHED)
2459 {
2460 Log(("vmresume: VMRESUME with non-launched VMCS -> VMFail\n"));
2461 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_VmcsLaunch;
2462 iemVmxVmFail(pVCpu, VMXINSTRERR_VMRESUME_NON_LAUNCHED_VMCS);
2463 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
2464 return VINF_SUCCESS;
2465 }
2466 }
2467
2468 /*
2469 * Load the current VMCS.
2470 */
2471 int rc = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), pVCpu->cpum.GstCtx.hwvirt.vmx.CTX_SUFF(pVmcs),
2472 IEM_VMX_GET_CURRENT_VMCS(pVCpu), VMX_V_VMCS_SIZE);
2473 if (RT_FAILURE(rc))
2474 {
2475 Log(("%s: Failed to read VMCS at %#RGp, rc=%Rrc\n", pszInstr, rc));
2476 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_PtrReadPhys;
2477 return rc;
2478 }
2479
2480 /*
2481 * Check VM-execution control fields.
2482 */
2483 rc = iemVmxVmentryCheckExecCtls(pVCpu, pszInstr);
2484 if (rc == VINF_SUCCESS)
2485 { /* likely */ }
2486 else
2487 {
2488 iemVmxVmFail(pVCpu, VMXINSTRERR_VMENTRY_INVALID_CTLS);
2489 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
2490 return VINF_SUCCESS;
2491 }
2492
2493 /*
2494 * Check VM-exit control fields.
2495 */
2496 rc = iemVmxVmentryCheckExitCtls(pVCpu, pszInstr);
2497 if (rc == VINF_SUCCESS)
2498 { /* likely */ }
2499 else
2500 {
2501 iemVmxVmFail(pVCpu, VMXINSTRERR_VMENTRY_INVALID_CTLS);
2502 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
2503 return VINF_SUCCESS;
2504 }
2505
2506 /*
2507 * Check VM-entry control fields.
2508 */
2509 rc = iemVmxVmentryCheckEntryCtls(pVCpu, pszInstr);
2510 if (rc == VINF_SUCCESS)
2511 { /* likely */ }
2512 else
2513 {
2514 iemVmxVmFail(pVCpu, VMXINSTRERR_VMENTRY_INVALID_CTLS);
2515 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
2516 return VINF_SUCCESS;
2517 }
2518
2519 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmentry_Success;
2520 iemVmxVmSucceed(pVCpu);
2521 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
2522 return VERR_IEM_IPE_2;
2523}
2524
2525
2526/**
2527 * Implements 'VMXON'.
2528 */
2529IEM_CIMPL_DEF_2(iemCImpl_vmxon, uint8_t, iEffSeg, RTGCPTR, GCPtrVmxon)
2530{
2531 return iemVmxVmxon(pVCpu, cbInstr, iEffSeg, GCPtrVmxon, NULL /* pExitInfo */);
2532}
2533
2534
2535/**
2536 * Implements 'VMXOFF'.
2537 *
2538 * @remarks Common VMX instruction checks are already expected to by the caller,
2539 * i.e. CR4.VMXE, Real/V86 mode, EFER/CS.L checks.
2540 */
2541IEM_CIMPL_DEF_0(iemCImpl_vmxoff)
2542{
2543# if defined(VBOX_WITH_NESTED_HWVIRT_ONLY_IN_IEM) && !defined(IN_RING3)
2544 RT_NOREF2(pVCpu, cbInstr);
2545 return VINF_EM_RAW_EMULATE_INSTR;
2546# else
2547 if (IEM_IS_VMX_NON_ROOT_MODE(pVCpu))
2548 {
2549 /** @todo NSTVMX: intercept. */
2550 }
2551
2552 /* CPL. */
2553 if (pVCpu->iem.s.uCpl > 0)
2554 {
2555 Log(("vmxoff: CPL %u -> #GP(0)\n", pVCpu->iem.s.uCpl));
2556 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmxoff_Cpl;
2557 return iemRaiseGeneralProtectionFault0(pVCpu);
2558 }
2559
2560 /* Dual monitor treatment of SMIs and SMM. */
2561 uint64_t const fSmmMonitorCtl = CPUMGetGuestIa32SmmMonitorCtl(pVCpu);
2562 if (fSmmMonitorCtl & MSR_IA32_SMM_MONITOR_VALID)
2563 {
2564 iemVmxVmFail(pVCpu, VMXINSTRERR_VMXOFF_DUAL_MON);
2565 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
2566 return VINF_SUCCESS;
2567 }
2568
2569 /*
2570 * Record that we're no longer in VMX root operation, block INIT, block and disable A20M.
2571 */
2572 pVCpu->cpum.GstCtx.hwvirt.vmx.fInVmxRootMode = false;
2573 Assert(!pVCpu->cpum.GstCtx.hwvirt.vmx.fInVmxNonRootMode);
2574
2575 if (fSmmMonitorCtl & MSR_IA32_SMM_MONITOR_VMXOFF_UNBLOCK_SMI)
2576 { /** @todo NSTVMX: Unblock SMI. */ }
2577 /** @todo NSTVMX: Unblock and enable A20M. */
2578 /** @todo NSTVMX: Clear address-range monitoring. */
2579
2580 pVCpu->cpum.GstCtx.hwvirt.vmx.enmInstrDiag = kVmxVInstrDiag_Vmxoff_Success;
2581 iemVmxVmSucceed(pVCpu);
2582 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
2583# if defined(VBOX_WITH_NESTED_HWVIRT_ONLY_IN_IEM) && defined(IN_RING3)
2584 return EMR3SetExecutionPolicy(pVCpu->CTX_SUFF(pVM)->pUVM, EMEXECPOLICY_IEM_ALL, false);
2585# else
2586 return VINF_SUCCESS;
2587# endif
2588# endif
2589}
2590
2591
2592/**
2593 * Implements 'VMLAUNCH'.
2594 */
2595IEM_CIMPL_DEF_0(iemCImpl_vmlaunch)
2596{
2597 return iemVmxVmlaunchVmresume(pVCpu, cbInstr, VMXINSTRID_VMLAUNCH, NULL /* pExitInfo */);
2598}
2599
2600
2601/**
2602 * Implements 'VMRESUME'.
2603 */
2604IEM_CIMPL_DEF_0(iemCImpl_vmresume)
2605{
2606 return iemVmxVmlaunchVmresume(pVCpu, cbInstr, VMXINSTRID_VMRESUME, NULL /* pExitInfo */);
2607}
2608
2609
2610/**
2611 * Implements 'VMPTRLD'.
2612 */
2613IEM_CIMPL_DEF_2(iemCImpl_vmptrld, uint8_t, iEffSeg, RTGCPTR, GCPtrVmcs)
2614{
2615 return iemVmxVmptrld(pVCpu, cbInstr, iEffSeg, GCPtrVmcs, NULL /* pExitInfo */);
2616}
2617
2618
2619/**
2620 * Implements 'VMPTRST'.
2621 */
2622IEM_CIMPL_DEF_2(iemCImpl_vmptrst, uint8_t, iEffSeg, RTGCPTR, GCPtrVmcs)
2623{
2624 return iemVmxVmptrst(pVCpu, cbInstr, iEffSeg, GCPtrVmcs, NULL /* pExitInfo */);
2625}
2626
2627
2628/**
2629 * Implements 'VMCLEAR'.
2630 */
2631IEM_CIMPL_DEF_2(iemCImpl_vmclear, uint8_t, iEffSeg, RTGCPTR, GCPtrVmcs)
2632{
2633 return iemVmxVmclear(pVCpu, cbInstr, iEffSeg, GCPtrVmcs, NULL /* pExitInfo */);
2634}
2635
2636
2637/**
2638 * Implements 'VMWRITE' register.
2639 */
2640IEM_CIMPL_DEF_2(iemCImpl_vmwrite_reg, uint64_t, u64Val, uint64_t, u64FieldEnc)
2641{
2642 return iemVmxVmwrite(pVCpu, cbInstr, UINT8_MAX /* iEffSeg */, IEMMODE_64BIT /* N/A */, u64Val, u64FieldEnc,
2643 NULL /* pExitInfo */);
2644}
2645
2646
2647/**
2648 * Implements 'VMWRITE' memory.
2649 */
2650IEM_CIMPL_DEF_4(iemCImpl_vmwrite_mem, uint8_t, iEffSeg, IEMMODE, enmEffAddrMode, RTGCPTR, GCPtrVal, uint32_t, u64FieldEnc)
2651{
2652 return iemVmxVmwrite(pVCpu, cbInstr, iEffSeg, enmEffAddrMode, GCPtrVal, u64FieldEnc, NULL /* pExitInfo */);
2653}
2654
2655
2656/**
2657 * Implements 'VMREAD' 64-bit register.
2658 */
2659IEM_CIMPL_DEF_2(iemCImpl_vmread64_reg, uint64_t *, pu64Dst, uint64_t, u64FieldEnc)
2660{
2661 return iemVmxVmreadReg64(pVCpu, cbInstr, pu64Dst, u64FieldEnc, NULL /* pExitInfo */);
2662}
2663
2664
2665/**
2666 * Implements 'VMREAD' 32-bit register.
2667 */
2668IEM_CIMPL_DEF_2(iemCImpl_vmread32_reg, uint32_t *, pu32Dst, uint32_t, u32FieldEnc)
2669{
2670 return iemVmxVmreadReg32(pVCpu, cbInstr, pu32Dst, u32FieldEnc, NULL /* pExitInfo */);
2671}
2672
2673
2674/**
2675 * Implements 'VMREAD' memory.
2676 */
2677IEM_CIMPL_DEF_4(iemCImpl_vmread_mem, uint8_t, iEffSeg, IEMMODE, enmEffAddrMode, RTGCPTR, GCPtrDst, uint32_t, u64FieldEnc)
2678{
2679 return iemVmxVmreadMem(pVCpu, cbInstr, iEffSeg, enmEffAddrMode, GCPtrDst, u64FieldEnc, NULL /* pExitInfo */);
2680}
2681
2682#endif
2683
Note: See TracBrowser for help on using the repository browser.

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