VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/IEMAllCImplSvmInstr.cpp.h@ 72518

Last change on this file since 72518 was 72518, checked in by vboxsync, 7 years ago

IEM: Implemented rsm for the purpose of SVM intercepting. Renamed IEMOP_HLP_SVM_INSTR_INTERCEPT_AND_NRIP to IEMCIMPL_HLP_SVM_INSTR_INTERCEPT_AND_NRIP.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 60.6 KB
Line 
1/* $Id: IEMAllCImplSvmInstr.cpp.h 72518 2018-06-11 15:00:30Z vboxsync $ */
2/** @file
3 * IEM - AMD-V (Secure Virtual Machine) instruction implementation.
4 */
5
6/*
7 * Copyright (C) 2011-2017 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#ifdef VBOX_WITH_NESTED_HWVIRT_SVM
20/** Check and handles SVM nested-guest instruction intercept and updates
21 * NRIP if needed.
22 */
23# define IEMCIMPL_HLP_SVM_INSTR_INTERCEPT_AND_NRIP(a_pVCpu, a_Intercept, a_uExitCode, a_uExitInfo1, a_uExitInfo2) \
24 do \
25 { \
26 if (IEM_IS_SVM_CTRL_INTERCEPT_SET(a_pVCpu, a_Intercept)) \
27 { \
28 IEM_SVM_UPDATE_NRIP(a_pVCpu); \
29 IEM_RETURN_SVM_VMEXIT(a_pVCpu, a_uExitCode, a_uExitInfo1, a_uExitInfo2); \
30 } \
31 } while (0)
32
33/** Checks and handles SVM nested-guest CR0 read intercept. */
34# define IEMCIMPL_HLP_SVM_READ_CR_INTERCEPT(a_pVCpu, a_uCr, a_uExitInfo1, a_uExitInfo2) \
35 do \
36 { \
37 if (!IEM_IS_SVM_READ_CR_INTERCEPT_SET(a_pVCpu, a_uCr)) \
38 { /* probably likely */ } \
39 else \
40 { \
41 IEM_SVM_UPDATE_NRIP(a_pVCpu); \
42 IEM_RETURN_SVM_VMEXIT(a_pVCpu, SVM_EXIT_READ_CR0 + (a_uCr), a_uExitInfo1, a_uExitInfo2); \
43 } \
44 } while (0)
45
46#else /* !VBOX_WITH_NESTED_HWVIRT_SVM */
47# define IEMCIMPL_HLP_SVM_INSTR_INTERCEPT_AND_NRIP(a_pVCpu, a_Intercept, a_uExitCode, a_uExitInfo1, a_uExitInfo2) do { } while (0)
48# define IEMCIMPL_HLP_SVM_READ_CR_INTERCEPT(a_pVCpu, a_uCr, a_uExitInfo1, a_uExitInfo2) do { } while (0)
49#endif /* !VBOX_WITH_NESTED_HWVIRT_SVM */
50
51
52#ifdef VBOX_WITH_NESTED_HWVIRT_SVM
53
54/**
55 * Converts an IEM exception event type to an SVM event type.
56 *
57 * @returns The SVM event type.
58 * @retval UINT8_MAX if the specified type of event isn't among the set
59 * of recognized IEM event types.
60 *
61 * @param uVector The vector of the event.
62 * @param fIemXcptFlags The IEM exception / interrupt flags.
63 */
64IEM_STATIC uint8_t iemGetSvmEventType(uint32_t uVector, uint32_t fIemXcptFlags)
65{
66 if (fIemXcptFlags & IEM_XCPT_FLAGS_T_CPU_XCPT)
67 {
68 if (uVector != X86_XCPT_NMI)
69 return SVM_EVENT_EXCEPTION;
70 return SVM_EVENT_NMI;
71 }
72
73 /* See AMD spec. Table 15-1. "Guest Exception or Interrupt Types". */
74 if (fIemXcptFlags & (IEM_XCPT_FLAGS_BP_INSTR | IEM_XCPT_FLAGS_ICEBP_INSTR | IEM_XCPT_FLAGS_OF_INSTR))
75 return SVM_EVENT_EXCEPTION;
76
77 if (fIemXcptFlags & IEM_XCPT_FLAGS_T_EXT_INT)
78 return SVM_EVENT_EXTERNAL_IRQ;
79
80 if (fIemXcptFlags & IEM_XCPT_FLAGS_T_SOFT_INT)
81 return SVM_EVENT_SOFTWARE_INT;
82
83 AssertMsgFailed(("iemGetSvmEventType: Invalid IEM xcpt/int. type %#x, uVector=%#x\n", fIemXcptFlags, uVector));
84 return UINT8_MAX;
85}
86
87
88/**
89 * Performs an SVM world-switch (VMRUN, \#VMEXIT) updating PGM and IEM internals.
90 *
91 * @returns Strict VBox status code.
92 * @param pVCpu The cross context virtual CPU structure.
93 */
94DECLINLINE(VBOXSTRICTRC) iemSvmWorldSwitch(PVMCPU pVCpu)
95{
96 /*
97 * Inform PGM about paging mode changes.
98 * We include X86_CR0_PE because PGM doesn't handle paged-real mode yet,
99 * see comment in iemMemPageTranslateAndCheckAccess().
100 */
101 int rc = PGMChangeMode(pVCpu, pVCpu->cpum.GstCtx.cr0 | X86_CR0_PE, pVCpu->cpum.GstCtx.cr4, pVCpu->cpum.GstCtx.msrEFER);
102# ifdef IN_RING3
103 Assert(rc != VINF_PGM_CHANGE_MODE);
104# endif
105 AssertRCReturn(rc, rc);
106
107 /* Inform CPUM (recompiler), can later be removed. */
108 CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_ALL);
109
110 /*
111 * Flush the TLB with new CR3. This is required in case the PGM mode change
112 * above doesn't actually change anything.
113 */
114 if (rc == VINF_SUCCESS)
115 {
116 rc = PGMFlushTLB(pVCpu, pVCpu->cpum.GstCtx.cr3, true);
117 AssertRCReturn(rc, rc);
118 }
119
120 /* Re-initialize IEM cache/state after the drastic mode switch. */
121 iemReInitExec(pVCpu);
122 return rc;
123}
124
125
126/**
127 * SVM \#VMEXIT handler.
128 *
129 * @returns Strict VBox status code.
130 * @retval VINF_SVM_VMEXIT when the \#VMEXIT is successful.
131 * @retval VERR_SVM_VMEXIT_FAILED when the \#VMEXIT failed restoring the guest's
132 * "host state" and a shutdown is required.
133 *
134 * @param pVCpu The cross context virtual CPU structure.
135 * @param uExitCode The exit code.
136 * @param uExitInfo1 The exit info. 1 field.
137 * @param uExitInfo2 The exit info. 2 field.
138 */
139IEM_STATIC VBOXSTRICTRC iemSvmVmexit(PVMCPU pVCpu, uint64_t uExitCode, uint64_t uExitInfo1, uint64_t uExitInfo2)
140{
141 VBOXSTRICTRC rcStrict;
142 if ( CPUMIsGuestInSvmNestedHwVirtMode(IEM_GET_CTX(pVCpu))
143 || uExitCode == SVM_EXIT_INVALID)
144 {
145 LogFlow(("iemSvmVmexit: CS:RIP=%04x:%08RX64 uExitCode=%#RX64 uExitInfo1=%#RX64 uExitInfo2=%#RX64\n", pVCpu->cpum.GstCtx.cs.Sel,
146 pVCpu->cpum.GstCtx.rip, uExitCode, uExitInfo1, uExitInfo2));
147
148 /*
149 * Disable the global interrupt flag to prevent interrupts during the 'atomic' world switch.
150 */
151 pVCpu->cpum.GstCtx.hwvirt.fGif = false;
152
153 Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, &pVCpu->cpum.GstCtx.es));
154 Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, &pVCpu->cpum.GstCtx.cs));
155 Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, &pVCpu->cpum.GstCtx.ss));
156 Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, &pVCpu->cpum.GstCtx.ds));
157
158 /*
159 * Map the nested-guest VMCB from its location in guest memory.
160 * Write exactly what the CPU does on #VMEXIT thereby preserving most other bits in the
161 * guest's VMCB in memory, see @bugref{7243#c113} and related comment on iemSvmVmrun().
162 */
163 PSVMVMCB pVmcbMem;
164 PGMPAGEMAPLOCK PgLockMem;
165 PSVMVMCBCTRL pVmcbCtrl = &pVCpu->cpum.GstCtx.hwvirt.svm.CTX_SUFF(pVmcb)->ctrl;
166 rcStrict = iemMemPageMap(pVCpu, pVCpu->cpum.GstCtx.hwvirt.svm.GCPhysVmcb, IEM_ACCESS_DATA_RW, (void **)&pVmcbMem, &PgLockMem);
167 if (rcStrict == VINF_SUCCESS)
168 {
169 /*
170 * Notify HM in case the nested-guest was executed using hardware-assisted SVM (which
171 * would have modified some VMCB state) that might need to be restored on #VMEXIT before
172 * writing the VMCB back to guest memory.
173 */
174 HMSvmNstGstVmExitNotify(pVCpu, IEM_GET_CTX(pVCpu));
175
176 /*
177 * Save the nested-guest state into the VMCB state-save area.
178 */
179 PSVMVMCBSTATESAVE pVmcbMemState = &pVmcbMem->guest;
180 HMSVM_SEG_REG_COPY_TO_VMCB(IEM_GET_CTX(pVCpu), pVmcbMemState, ES, es);
181 HMSVM_SEG_REG_COPY_TO_VMCB(IEM_GET_CTX(pVCpu), pVmcbMemState, CS, cs);
182 HMSVM_SEG_REG_COPY_TO_VMCB(IEM_GET_CTX(pVCpu), pVmcbMemState, SS, ss);
183 HMSVM_SEG_REG_COPY_TO_VMCB(IEM_GET_CTX(pVCpu), pVmcbMemState, DS, ds);
184 pVmcbMemState->GDTR.u32Limit = pVCpu->cpum.GstCtx.gdtr.cbGdt;
185 pVmcbMemState->GDTR.u64Base = pVCpu->cpum.GstCtx.gdtr.pGdt;
186 pVmcbMemState->IDTR.u32Limit = pVCpu->cpum.GstCtx.idtr.cbIdt;
187 pVmcbMemState->IDTR.u64Base = pVCpu->cpum.GstCtx.idtr.pIdt;
188 pVmcbMemState->u64EFER = pVCpu->cpum.GstCtx.msrEFER;
189 pVmcbMemState->u64CR4 = pVCpu->cpum.GstCtx.cr4;
190 pVmcbMemState->u64CR3 = pVCpu->cpum.GstCtx.cr3;
191 pVmcbMemState->u64CR2 = pVCpu->cpum.GstCtx.cr2;
192 pVmcbMemState->u64CR0 = pVCpu->cpum.GstCtx.cr0;
193 /** @todo Nested paging. */
194 pVmcbMemState->u64RFlags = pVCpu->cpum.GstCtx.rflags.u64;
195 pVmcbMemState->u64RIP = pVCpu->cpum.GstCtx.rip;
196 pVmcbMemState->u64RSP = pVCpu->cpum.GstCtx.rsp;
197 pVmcbMemState->u64RAX = pVCpu->cpum.GstCtx.rax;
198 pVmcbMemState->u64DR7 = pVCpu->cpum.GstCtx.dr[7];
199 pVmcbMemState->u64DR6 = pVCpu->cpum.GstCtx.dr[6];
200 pVmcbMemState->u8CPL = pVCpu->cpum.GstCtx.ss.Attr.n.u2Dpl; /* See comment in CPUMGetGuestCPL(). */
201 Assert(CPUMGetGuestCPL(pVCpu) == pVCpu->cpum.GstCtx.ss.Attr.n.u2Dpl);
202 if (CPUMIsGuestSvmNestedPagingEnabled(pVCpu, IEM_GET_CTX(pVCpu)))
203 pVmcbMemState->u64PAT = pVCpu->cpum.GstCtx.msrPAT;
204
205 /*
206 * Save additional state and intercept information.
207 *
208 * - V_IRQ: Tracked using VMCPU_FF_INTERRUPT_NESTED_GUEST force-flag and updated below.
209 * - V_TPR: Updated by iemCImpl_load_CrX or by the physical CPU for hardware-assisted
210 * SVM execution.
211 * - Interrupt shadow: Tracked using VMCPU_FF_INHIBIT_INTERRUPTS and RIP.
212 */
213 PSVMVMCBCTRL pVmcbMemCtrl = &pVmcbMem->ctrl;
214 if (!VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INTERRUPT_NESTED_GUEST)) /* V_IRQ. */
215 pVmcbMemCtrl->IntCtrl.n.u1VIrqPending = 0;
216 else
217 {
218 Assert(pVmcbCtrl->IntCtrl.n.u1VIrqPending);
219 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_NESTED_GUEST);
220 }
221
222 pVmcbMemCtrl->IntCtrl.n.u8VTPR = pVmcbCtrl->IntCtrl.n.u8VTPR; /* V_TPR. */
223
224 if ( VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS) /* Interrupt shadow. */
225 && EMGetInhibitInterruptsPC(pVCpu) == pVCpu->cpum.GstCtx.rip)
226 {
227 pVmcbMemCtrl->IntShadow.n.u1IntShadow = 1;
228 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS);
229 LogFlow(("iemSvmVmexit: Interrupt shadow till %#RX64\n", pVCpu->cpum.GstCtx.rip));
230 }
231 else
232 pVmcbMemCtrl->IntShadow.n.u1IntShadow = 0;
233
234 /*
235 * Save nRIP, instruction length and byte fields.
236 */
237 pVmcbMemCtrl->u64NextRIP = pVmcbCtrl->u64NextRIP;
238 pVmcbMemCtrl->cbInstrFetched = pVmcbCtrl->cbInstrFetched;
239 memcpy(&pVmcbMemCtrl->abInstr[0], &pVmcbCtrl->abInstr[0], sizeof(pVmcbMemCtrl->abInstr));
240
241 /*
242 * Save exit information.
243 */
244 pVmcbMemCtrl->u64ExitCode = uExitCode;
245 pVmcbMemCtrl->u64ExitInfo1 = uExitInfo1;
246 pVmcbMemCtrl->u64ExitInfo2 = uExitInfo2;
247
248 /*
249 * Update the exit interrupt-information field if this #VMEXIT happened as a result
250 * of delivering an event through IEM.
251 *
252 * Don't update the exit interrupt-information field if the event wasn't being injected
253 * through IEM, as it would have been updated by real hardware if the nested-guest was
254 * executed using hardware-assisted SVM.
255 */
256 {
257 uint8_t uExitIntVector;
258 uint32_t uExitIntErr;
259 uint32_t fExitIntFlags;
260 bool const fRaisingEvent = IEMGetCurrentXcpt(pVCpu, &uExitIntVector, &fExitIntFlags, &uExitIntErr,
261 NULL /* uExitIntCr2 */);
262 if (fRaisingEvent)
263 {
264 pVmcbCtrl->ExitIntInfo.n.u1Valid = 1;
265 pVmcbCtrl->ExitIntInfo.n.u8Vector = uExitIntVector;
266 pVmcbCtrl->ExitIntInfo.n.u3Type = iemGetSvmEventType(uExitIntVector, fExitIntFlags);
267 if (fExitIntFlags & IEM_XCPT_FLAGS_ERR)
268 {
269 pVmcbCtrl->ExitIntInfo.n.u1ErrorCodeValid = true;
270 pVmcbCtrl->ExitIntInfo.n.u32ErrorCode = uExitIntErr;
271 }
272 }
273 }
274
275 /*
276 * Save the exit interrupt-information field.
277 *
278 * We write the whole field including overwriting reserved bits as it was observed on an
279 * AMD Ryzen 5 Pro 1500 that the CPU does not preserve reserved bits in EXITINTINFO.
280 */
281 pVmcbMemCtrl->ExitIntInfo = pVmcbCtrl->ExitIntInfo;
282
283 /*
284 * Clear event injection.
285 */
286 pVmcbMemCtrl->EventInject.n.u1Valid = 0;
287
288 iemMemPageUnmap(pVCpu, pVCpu->cpum.GstCtx.hwvirt.svm.GCPhysVmcb, IEM_ACCESS_DATA_RW, pVmcbMem, &PgLockMem);
289 }
290
291 /*
292 * Prepare for guest's "host mode" by clearing internal processor state bits.
293 *
294 * We don't need to zero out the state-save area, just the controls should be
295 * sufficient because it has the critical bit of indicating whether we're inside
296 * the nested-guest or not.
297 */
298 memset(pVmcbCtrl, 0, sizeof(*pVmcbCtrl));
299 Assert(!CPUMIsGuestInSvmNestedHwVirtMode(IEM_GET_CTX(pVCpu)));
300
301 /*
302 * Restore the subset of force-flags that were preserved.
303 */
304 if (pVCpu->cpum.GstCtx.hwvirt.fLocalForcedActions)
305 {
306 VMCPU_FF_SET(pVCpu, pVCpu->cpum.GstCtx.hwvirt.fLocalForcedActions);
307 pVCpu->cpum.GstCtx.hwvirt.fLocalForcedActions = 0;
308 }
309
310 if (rcStrict == VINF_SUCCESS)
311 {
312 /** @todo Nested paging. */
313 /** @todo ASID. */
314
315 /*
316 * Reload the guest's "host state".
317 */
318 CPUMSvmVmExitRestoreHostState(pVCpu, IEM_GET_CTX(pVCpu));
319
320 /*
321 * Update PGM, IEM and others of a world-switch.
322 */
323 rcStrict = iemSvmWorldSwitch(pVCpu);
324 if (rcStrict == VINF_SUCCESS)
325 rcStrict = VINF_SVM_VMEXIT;
326 else if (RT_SUCCESS(rcStrict))
327 {
328 LogFlow(("iemSvmVmexit: Setting passup status from iemSvmWorldSwitch %Rrc\n", VBOXSTRICTRC_VAL(rcStrict)));
329 iemSetPassUpStatus(pVCpu, rcStrict);
330 rcStrict = VINF_SVM_VMEXIT;
331 }
332 else
333 LogFlow(("iemSvmVmexit: iemSvmWorldSwitch unexpected failure. rc=%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)));
334 }
335 else
336 {
337 AssertMsgFailed(("iemSvmVmexit: Mapping VMCB at %#RGp failed. rc=%Rrc\n", pVCpu->cpum.GstCtx.hwvirt.svm.GCPhysVmcb, VBOXSTRICTRC_VAL(rcStrict)));
338 rcStrict = VERR_SVM_VMEXIT_FAILED;
339 }
340 }
341 else
342 {
343 AssertMsgFailed(("iemSvmVmexit: Not in SVM guest mode! uExitCode=%#RX64 uExitInfo1=%#RX64 uExitInfo2=%#RX64\n", uExitCode, uExitInfo1, uExitInfo2));
344 rcStrict = VERR_SVM_IPE_3;
345 }
346
347# if defined(VBOX_WITH_NESTED_HWVIRT_ONLY_IN_IEM) && defined(IN_RING3)
348 /* CLGI/STGI may not have been intercepted and thus not executed in IEM. */
349 if (HMSvmIsVGifActive(pVCpu->CTX_SUFF(pVM)))
350 return EMR3SetExecutionPolicy(pVCpu->CTX_SUFF(pVM)->pUVM, EMEXECPOLICY_IEM_ALL, false);
351# endif
352 return rcStrict;
353}
354
355
356/**
357 * Performs the operations necessary that are part of the vmrun instruction
358 * execution in the guest.
359 *
360 * @returns Strict VBox status code (i.e. informational status codes too).
361 * @retval VINF_SUCCESS successully executed VMRUN and entered nested-guest
362 * code execution.
363 * @retval VINF_SVM_VMEXIT when executing VMRUN causes a \#VMEXIT
364 * (SVM_EXIT_INVALID most likely).
365 *
366 * @param pVCpu The cross context virtual CPU structure.
367 * @param cbInstr The length of the VMRUN instruction.
368 * @param GCPhysVmcb Guest physical address of the VMCB to run.
369 */
370IEM_STATIC VBOXSTRICTRC iemSvmVmrun(PVMCPU pVCpu, uint8_t cbInstr, RTGCPHYS GCPhysVmcb)
371{
372 LogFlow(("iemSvmVmrun\n"));
373
374 /*
375 * Cache the physical address of the VMCB for #VMEXIT exceptions.
376 */
377 pVCpu->cpum.GstCtx.hwvirt.svm.GCPhysVmcb = GCPhysVmcb;
378
379 /*
380 * Save the host state.
381 */
382 CPUMSvmVmRunSaveHostState(IEM_GET_CTX(pVCpu), cbInstr);
383
384 /*
385 * Read the guest VMCB.
386 */
387 PVM pVM = pVCpu->CTX_SUFF(pVM);
388 int rc = PGMPhysSimpleReadGCPhys(pVM, pVCpu->cpum.GstCtx.hwvirt.svm.CTX_SUFF(pVmcb), GCPhysVmcb, sizeof(SVMVMCB));
389 if (RT_SUCCESS(rc))
390 {
391 /*
392 * AMD-V seems to preserve reserved fields and only writes back selected, recognized
393 * fields on #VMEXIT. However, not all reserved bits are preserved (e.g, EXITINTINFO)
394 * but in our implementation we try to preserve as much as we possibly can.
395 *
396 * We could read the entire page here and only write back the relevant fields on
397 * #VMEXIT but since our internal VMCB is also being used by HM during hardware-assisted
398 * SVM execution, it creates a potential for a nested-hypervisor to set bits that are
399 * currently reserved but may be recognized as features bits in future CPUs causing
400 * unexpected & undesired results. Hence, we zero out unrecognized fields here as we
401 * typically enter hardware-assisted SVM soon anyway, see @bugref{7243#c113}.
402 */
403 PSVMVMCBCTRL pVmcbCtrl = &pVCpu->cpum.GstCtx.hwvirt.svm.CTX_SUFF(pVmcb)->ctrl;
404 PSVMVMCBSTATESAVE pVmcbNstGst = &pVCpu->cpum.GstCtx.hwvirt.svm.CTX_SUFF(pVmcb)->guest;
405
406 RT_ZERO(pVmcbCtrl->u8Reserved0);
407 RT_ZERO(pVmcbCtrl->u8Reserved1);
408 RT_ZERO(pVmcbCtrl->u8Reserved2);
409 RT_ZERO(pVmcbNstGst->u8Reserved0);
410 RT_ZERO(pVmcbNstGst->u8Reserved1);
411 RT_ZERO(pVmcbNstGst->u8Reserved2);
412 RT_ZERO(pVmcbNstGst->u8Reserved3);
413 RT_ZERO(pVmcbNstGst->u8Reserved4);
414 RT_ZERO(pVmcbNstGst->u8Reserved5);
415 pVmcbCtrl->u32Reserved0 = 0;
416 pVmcbCtrl->TLBCtrl.n.u24Reserved = 0;
417 pVmcbCtrl->IntCtrl.n.u6Reserved = 0;
418 pVmcbCtrl->IntCtrl.n.u3Reserved = 0;
419 pVmcbCtrl->IntCtrl.n.u5Reserved = 0;
420 pVmcbCtrl->IntCtrl.n.u24Reserved = 0;
421 pVmcbCtrl->IntShadow.n.u30Reserved = 0;
422 pVmcbCtrl->ExitIntInfo.n.u19Reserved = 0;
423 pVmcbCtrl->NestedPagingCtrl.n.u29Reserved = 0;
424 pVmcbCtrl->EventInject.n.u19Reserved = 0;
425 pVmcbCtrl->LbrVirt.n.u30Reserved = 0;
426
427 /*
428 * Validate guest-state and controls.
429 */
430 /* VMRUN must always be intercepted. */
431 if (!CPUMIsGuestSvmCtrlInterceptSet(pVCpu, IEM_GET_CTX(pVCpu), SVM_CTRL_INTERCEPT_VMRUN))
432 {
433 Log(("iemSvmVmrun: VMRUN instruction not intercepted -> #VMEXIT\n"));
434 return iemSvmVmexit(pVCpu, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
435 }
436
437 /* Nested paging. */
438 if ( pVmcbCtrl->NestedPagingCtrl.n.u1NestedPaging
439 && !pVM->cpum.ro.GuestFeatures.fSvmNestedPaging)
440 {
441 Log(("iemSvmVmrun: Nested paging not supported -> Disabling\n"));
442 pVmcbCtrl->NestedPagingCtrl.n.u1NestedPaging = 0;
443 }
444
445 /* AVIC. */
446 if ( pVmcbCtrl->IntCtrl.n.u1AvicEnable
447 && !pVM->cpum.ro.GuestFeatures.fSvmAvic)
448 {
449 Log(("iemSvmVmrun: AVIC not supported -> Disabling\n"));
450 pVmcbCtrl->IntCtrl.n.u1AvicEnable = 0;
451 }
452
453 /* Last branch record (LBR) virtualization. */
454 if ( pVmcbCtrl->LbrVirt.n.u1LbrVirt
455 && !pVM->cpum.ro.GuestFeatures.fSvmLbrVirt)
456 {
457 Log(("iemSvmVmrun: LBR virtualization not supported -> Disabling\n"));
458 pVmcbCtrl->LbrVirt.n.u1LbrVirt = 0;
459 }
460
461 /* Virtualized VMSAVE/VMLOAD. */
462 if ( pVmcbCtrl->LbrVirt.n.u1VirtVmsaveVmload
463 && !pVM->cpum.ro.GuestFeatures.fSvmVirtVmsaveVmload)
464 {
465 Log(("iemSvmVmrun: Virtualized VMSAVE/VMLOAD not supported -> Disabling\n"));
466 pVmcbCtrl->LbrVirt.n.u1VirtVmsaveVmload = 0;
467 }
468
469 /* Virtual GIF. */
470 if ( pVmcbCtrl->IntCtrl.n.u1VGifEnable
471 && !pVM->cpum.ro.GuestFeatures.fSvmVGif)
472 {
473 Log(("iemSvmVmrun: Virtual GIF not supported -> Disabling\n"));
474 pVmcbCtrl->IntCtrl.n.u1VGifEnable = 0;
475 }
476
477 /* Guest ASID. */
478 if (!pVmcbCtrl->TLBCtrl.n.u32ASID)
479 {
480 Log(("iemSvmVmrun: Guest ASID is invalid -> #VMEXIT\n"));
481 return iemSvmVmexit(pVCpu, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
482 }
483
484 /* Guest AVIC. */
485 if ( pVmcbCtrl->IntCtrl.n.u1AvicEnable
486 && !pVM->cpum.ro.GuestFeatures.fSvmAvic)
487 {
488 Log(("iemSvmVmrun: AVIC not supported -> Disabling\n"));
489 pVmcbCtrl->IntCtrl.n.u1AvicEnable = 0;
490 }
491
492 /* Guest Secure Encrypted Virtualization. */
493 if ( ( pVmcbCtrl->NestedPagingCtrl.n.u1Sev
494 || pVmcbCtrl->NestedPagingCtrl.n.u1SevEs)
495 && !pVM->cpum.ro.GuestFeatures.fSvmAvic)
496 {
497 Log(("iemSvmVmrun: SEV not supported -> Disabling\n"));
498 pVmcbCtrl->NestedPagingCtrl.n.u1Sev = 0;
499 pVmcbCtrl->NestedPagingCtrl.n.u1SevEs = 0;
500 }
501
502 /* Flush by ASID. */
503 if ( !pVM->cpum.ro.GuestFeatures.fSvmFlusbByAsid
504 && pVmcbCtrl->TLBCtrl.n.u8TLBFlush != SVM_TLB_FLUSH_NOTHING
505 && pVmcbCtrl->TLBCtrl.n.u8TLBFlush != SVM_TLB_FLUSH_ENTIRE)
506 {
507 Log(("iemSvmVmrun: Flush-by-ASID not supported -> #VMEXIT\n"));
508 return iemSvmVmexit(pVCpu, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
509 }
510
511 /* IO permission bitmap. */
512 RTGCPHYS const GCPhysIOBitmap = pVmcbCtrl->u64IOPMPhysAddr;
513 if ( (GCPhysIOBitmap & X86_PAGE_4K_OFFSET_MASK)
514 || !PGMPhysIsGCPhysNormal(pVM, GCPhysIOBitmap)
515 || !PGMPhysIsGCPhysNormal(pVM, GCPhysIOBitmap + X86_PAGE_4K_SIZE)
516 || !PGMPhysIsGCPhysNormal(pVM, GCPhysIOBitmap + (X86_PAGE_4K_SIZE << 1)))
517 {
518 Log(("iemSvmVmrun: IO bitmap physaddr invalid. GCPhysIOBitmap=%#RX64 -> #VMEXIT\n", GCPhysIOBitmap));
519 return iemSvmVmexit(pVCpu, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
520 }
521
522 /* MSR permission bitmap. */
523 RTGCPHYS const GCPhysMsrBitmap = pVmcbCtrl->u64MSRPMPhysAddr;
524 if ( (GCPhysMsrBitmap & X86_PAGE_4K_OFFSET_MASK)
525 || !PGMPhysIsGCPhysNormal(pVM, GCPhysMsrBitmap)
526 || !PGMPhysIsGCPhysNormal(pVM, GCPhysMsrBitmap + X86_PAGE_4K_SIZE))
527 {
528 Log(("iemSvmVmrun: MSR bitmap physaddr invalid. GCPhysMsrBitmap=%#RX64 -> #VMEXIT\n", GCPhysMsrBitmap));
529 return iemSvmVmexit(pVCpu, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
530 }
531
532 /* CR0. */
533 if ( !(pVmcbNstGst->u64CR0 & X86_CR0_CD)
534 && (pVmcbNstGst->u64CR0 & X86_CR0_NW))
535 {
536 Log(("iemSvmVmrun: CR0 no-write through with cache disabled. CR0=%#RX64 -> #VMEXIT\n", pVmcbNstGst->u64CR0));
537 return iemSvmVmexit(pVCpu, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
538 }
539 if (pVmcbNstGst->u64CR0 >> 32)
540 {
541 Log(("iemSvmVmrun: CR0 reserved bits set. CR0=%#RX64 -> #VMEXIT\n", pVmcbNstGst->u64CR0));
542 return iemSvmVmexit(pVCpu, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
543 }
544 /** @todo Implement all reserved bits/illegal combinations for CR3, CR4. */
545
546 /* DR6 and DR7. */
547 if ( pVmcbNstGst->u64DR6 >> 32
548 || pVmcbNstGst->u64DR7 >> 32)
549 {
550 Log(("iemSvmVmrun: DR6 and/or DR7 reserved bits set. DR6=%#RX64 DR7=%#RX64 -> #VMEXIT\n", pVmcbNstGst->u64DR6,
551 pVmcbNstGst->u64DR6));
552 return iemSvmVmexit(pVCpu, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
553 }
554
555 /*
556 * PAT (Page Attribute Table) MSR.
557 *
558 * The CPU only validates and loads it when nested-paging is enabled.
559 * See AMD spec. "15.25.4 Nested Paging and VMRUN/#VMEXIT".
560 */
561 if ( pVmcbCtrl->NestedPagingCtrl.n.u1NestedPaging
562 && !CPUMIsPatMsrValid(pVmcbNstGst->u64PAT))
563 {
564 Log(("iemSvmVmrun: PAT invalid. u64PAT=%#RX64 -> #VMEXIT\n", pVmcbNstGst->u64PAT));
565 return iemSvmVmexit(pVCpu, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
566 }
567
568 /*
569 * Copy the IO permission bitmap into the cache.
570 */
571 Assert(pVCpu->cpum.GstCtx.hwvirt.svm.CTX_SUFF(pvIoBitmap));
572 rc = PGMPhysSimpleReadGCPhys(pVM, pVCpu->cpum.GstCtx.hwvirt.svm.CTX_SUFF(pvIoBitmap), GCPhysIOBitmap,
573 SVM_IOPM_PAGES * X86_PAGE_4K_SIZE);
574 if (RT_FAILURE(rc))
575 {
576 Log(("iemSvmVmrun: Failed reading the IO permission bitmap at %#RGp. rc=%Rrc\n", GCPhysIOBitmap, rc));
577 return iemSvmVmexit(pVCpu, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
578 }
579
580 /*
581 * Copy the MSR permission bitmap into the cache.
582 */
583 Assert(pVCpu->cpum.GstCtx.hwvirt.svm.CTX_SUFF(pvMsrBitmap));
584 rc = PGMPhysSimpleReadGCPhys(pVM, pVCpu->cpum.GstCtx.hwvirt.svm.CTX_SUFF(pvMsrBitmap), GCPhysMsrBitmap,
585 SVM_MSRPM_PAGES * X86_PAGE_4K_SIZE);
586 if (RT_FAILURE(rc))
587 {
588 Log(("iemSvmVmrun: Failed reading the MSR permission bitmap at %#RGp. rc=%Rrc\n", GCPhysMsrBitmap, rc));
589 return iemSvmVmexit(pVCpu, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
590 }
591
592 /*
593 * Copy segments from nested-guest VMCB state to the guest-CPU state.
594 *
595 * We do this here as we need to use the CS attributes and it's easier this way
596 * then using the VMCB format selectors. It doesn't really matter where we copy
597 * the state, we restore the guest-CPU context state on the \#VMEXIT anyway.
598 */
599 HMSVM_SEG_REG_COPY_FROM_VMCB(IEM_GET_CTX(pVCpu), pVmcbNstGst, ES, es);
600 HMSVM_SEG_REG_COPY_FROM_VMCB(IEM_GET_CTX(pVCpu), pVmcbNstGst, CS, cs);
601 HMSVM_SEG_REG_COPY_FROM_VMCB(IEM_GET_CTX(pVCpu), pVmcbNstGst, SS, ss);
602 HMSVM_SEG_REG_COPY_FROM_VMCB(IEM_GET_CTX(pVCpu), pVmcbNstGst, DS, ds);
603
604 /** @todo Segment attribute overrides by VMRUN. */
605
606 /*
607 * CPL adjustments and overrides.
608 *
609 * SS.DPL is apparently the CPU's CPL, see comment in CPUMGetGuestCPL().
610 * We shall thus adjust both CS.DPL and SS.DPL here.
611 */
612 pVCpu->cpum.GstCtx.cs.Attr.n.u2Dpl = pVCpu->cpum.GstCtx.ss.Attr.n.u2Dpl = pVmcbNstGst->u8CPL;
613 if (CPUMIsGuestInV86ModeEx(IEM_GET_CTX(pVCpu)))
614 pVCpu->cpum.GstCtx.cs.Attr.n.u2Dpl = pVCpu->cpum.GstCtx.ss.Attr.n.u2Dpl = 3;
615 if (CPUMIsGuestInRealModeEx(IEM_GET_CTX(pVCpu)))
616 pVCpu->cpum.GstCtx.cs.Attr.n.u2Dpl = pVCpu->cpum.GstCtx.ss.Attr.n.u2Dpl = 0;
617 Assert(CPUMSELREG_ARE_HIDDEN_PARTS_VALID(pVCpu, &pVCpu->cpum.GstCtx.ss));
618
619 /*
620 * Continue validating guest-state and controls.
621 *
622 * We pass CR0 as 0 to CPUMQueryValidatedGuestEfer below to skip the illegal
623 * EFER.LME bit transition check. We pass the nested-guest's EFER as both the
624 * old and new EFER value to not have any guest EFER bits influence the new
625 * nested-guest EFER.
626 */
627 uint64_t uValidEfer;
628 rc = CPUMQueryValidatedGuestEfer(pVM, 0 /* CR0 */, pVmcbNstGst->u64EFER, pVmcbNstGst->u64EFER, &uValidEfer);
629 if (RT_FAILURE(rc))
630 {
631 Log(("iemSvmVmrun: EFER invalid uOldEfer=%#RX64 -> #VMEXIT\n", pVmcbNstGst->u64EFER));
632 return iemSvmVmexit(pVCpu, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
633 }
634
635 /* Validate paging and CPU mode bits. */
636 bool const fSvm = RT_BOOL(uValidEfer & MSR_K6_EFER_SVME);
637 bool const fLongModeSupported = RT_BOOL(pVM->cpum.ro.GuestFeatures.fLongMode);
638 bool const fLongModeEnabled = RT_BOOL(uValidEfer & MSR_K6_EFER_LME);
639 bool const fPaging = RT_BOOL(pVmcbNstGst->u64CR0 & X86_CR0_PG);
640 bool const fPae = RT_BOOL(pVmcbNstGst->u64CR4 & X86_CR4_PAE);
641 bool const fProtMode = RT_BOOL(pVmcbNstGst->u64CR0 & X86_CR0_PE);
642 bool const fLongModeWithPaging = fLongModeEnabled && fPaging;
643 bool const fLongModeConformCS = pVCpu->cpum.GstCtx.cs.Attr.n.u1Long && pVCpu->cpum.GstCtx.cs.Attr.n.u1DefBig;
644 /* Adjust EFER.LMA (this is normally done by the CPU when system software writes CR0). */
645 if (fLongModeWithPaging)
646 uValidEfer |= MSR_K6_EFER_LMA;
647 bool const fLongModeActiveOrEnabled = RT_BOOL(uValidEfer & (MSR_K6_EFER_LME | MSR_K6_EFER_LMA));
648 if ( !fSvm
649 || (!fLongModeSupported && fLongModeActiveOrEnabled)
650 || (fLongModeWithPaging && !fPae)
651 || (fLongModeWithPaging && !fProtMode)
652 || ( fLongModeEnabled
653 && fPaging
654 && fPae
655 && fLongModeConformCS))
656 {
657 Log(("iemSvmVmrun: EFER invalid. uValidEfer=%#RX64 -> #VMEXIT\n", uValidEfer));
658 return iemSvmVmexit(pVCpu, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
659 }
660
661 /*
662 * Preserve the required force-flags.
663 *
664 * We only preserve the force-flags that would affect the execution of the
665 * nested-guest (or the guest).
666 *
667 * - VMCPU_FF_INHIBIT_INTERRUPTS need -not- be preserved as it's for a single
668 * instruction which is this VMRUN instruction itself.
669 *
670 * - VMCPU_FF_BLOCK_NMIS needs to be preserved as it blocks NMI until the
671 * execution of a subsequent IRET instruction in the guest.
672 *
673 * - The remaining FFs (e.g. timers) can stay in place so that we will be
674 * able to generate interrupts that should cause #VMEXITs for the
675 * nested-guest.
676 */
677 pVCpu->cpum.GstCtx.hwvirt.fLocalForcedActions = pVCpu->fLocalForcedActions & VMCPU_FF_BLOCK_NMIS;
678 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_BLOCK_NMIS);
679
680 /*
681 * Pause filter.
682 */
683 if (pVM->cpum.ro.GuestFeatures.fSvmPauseFilter)
684 {
685 pVCpu->cpum.GstCtx.hwvirt.svm.cPauseFilter = pVmcbCtrl->u16PauseFilterCount;
686 if (pVM->cpum.ro.GuestFeatures.fSvmPauseFilterThreshold)
687 pVCpu->cpum.GstCtx.hwvirt.svm.cPauseFilterThreshold = pVmcbCtrl->u16PauseFilterCount;
688 }
689
690 /*
691 * Interrupt shadow.
692 */
693 if (pVmcbCtrl->IntShadow.n.u1IntShadow)
694 {
695 LogFlow(("iemSvmVmrun: setting interrupt shadow. inhibit PC=%#RX64\n", pVmcbNstGst->u64RIP));
696 /** @todo will this cause trouble if the nested-guest is 64-bit but the guest is 32-bit? */
697 EMSetInhibitInterruptsPC(pVCpu, pVmcbNstGst->u64RIP);
698 }
699
700 /*
701 * TLB flush control.
702 * Currently disabled since it's redundant as we unconditionally flush the TLB
703 * in iemSvmWorldSwitch() below.
704 */
705# if 0
706 /** @todo @bugref{7243}: ASID based PGM TLB flushes. */
707 if ( pVmcbCtrl->TLBCtrl.n.u8TLBFlush == SVM_TLB_FLUSH_ENTIRE
708 || pVmcbCtrl->TLBCtrl.n.u8TLBFlush == SVM_TLB_FLUSH_SINGLE_CONTEXT
709 || pVmcbCtrl->TLBCtrl.n.u8TLBFlush == SVM_TLB_FLUSH_SINGLE_CONTEXT_RETAIN_GLOBALS)
710 PGMFlushTLB(pVCpu, pVmcbNstGst->u64CR3, true /* fGlobal */);
711# endif
712
713 /*
714 * Copy the remaining guest state from the VMCB to the guest-CPU context.
715 */
716 pVCpu->cpum.GstCtx.gdtr.cbGdt = pVmcbNstGst->GDTR.u32Limit;
717 pVCpu->cpum.GstCtx.gdtr.pGdt = pVmcbNstGst->GDTR.u64Base;
718 pVCpu->cpum.GstCtx.idtr.cbIdt = pVmcbNstGst->IDTR.u32Limit;
719 pVCpu->cpum.GstCtx.idtr.pIdt = pVmcbNstGst->IDTR.u64Base;
720 CPUMSetGuestCR0(pVCpu, pVmcbNstGst->u64CR0);
721 CPUMSetGuestCR4(pVCpu, pVmcbNstGst->u64CR4);
722 pVCpu->cpum.GstCtx.cr3 = pVmcbNstGst->u64CR3;
723 pVCpu->cpum.GstCtx.cr2 = pVmcbNstGst->u64CR2;
724 pVCpu->cpum.GstCtx.dr[6] = pVmcbNstGst->u64DR6;
725 pVCpu->cpum.GstCtx.dr[7] = pVmcbNstGst->u64DR7;
726 pVCpu->cpum.GstCtx.rflags.u64 = pVmcbNstGst->u64RFlags;
727 pVCpu->cpum.GstCtx.rax = pVmcbNstGst->u64RAX;
728 pVCpu->cpum.GstCtx.rsp = pVmcbNstGst->u64RSP;
729 pVCpu->cpum.GstCtx.rip = pVmcbNstGst->u64RIP;
730 CPUMSetGuestMsrEferNoCheck(pVCpu, pVCpu->cpum.GstCtx.msrEFER, uValidEfer);
731 if (pVmcbCtrl->NestedPagingCtrl.n.u1NestedPaging)
732 pVCpu->cpum.GstCtx.msrPAT = pVmcbNstGst->u64PAT;
733
734 /* Mask DR6, DR7 bits mandatory set/clear bits. */
735 pVCpu->cpum.GstCtx.dr[6] &= ~(X86_DR6_RAZ_MASK | X86_DR6_MBZ_MASK);
736 pVCpu->cpum.GstCtx.dr[6] |= X86_DR6_RA1_MASK;
737 pVCpu->cpum.GstCtx.dr[7] &= ~(X86_DR7_RAZ_MASK | X86_DR7_MBZ_MASK);
738 pVCpu->cpum.GstCtx.dr[7] |= X86_DR7_RA1_MASK;
739
740 /*
741 * Check for pending virtual interrupts.
742 */
743 if (pVmcbCtrl->IntCtrl.n.u1VIrqPending)
744 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_NESTED_GUEST);
745 else
746 Assert(!VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INTERRUPT_NESTED_GUEST));
747
748 /*
749 * Update PGM, IEM and others of a world-switch.
750 */
751 VBOXSTRICTRC rcStrict = iemSvmWorldSwitch(pVCpu);
752 if (rcStrict == VINF_SUCCESS)
753 { /* likely */ }
754 else if (RT_SUCCESS(rcStrict))
755 {
756 LogFlow(("iemSvmVmrun: iemSvmWorldSwitch returned %Rrc, setting passup status\n", VBOXSTRICTRC_VAL(rcStrict)));
757 rcStrict = iemSetPassUpStatus(pVCpu, rcStrict);
758 }
759 else
760 {
761 LogFlow(("iemSvmVmrun: iemSvmWorldSwitch unexpected failure. rc=%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)));
762 return rcStrict;
763 }
764
765 /*
766 * Clear global interrupt flags to allow interrupts in the guest.
767 */
768 pVCpu->cpum.GstCtx.hwvirt.fGif = true;
769
770 /*
771 * Event injection.
772 */
773 PCSVMEVENT pEventInject = &pVmcbCtrl->EventInject;
774 pVCpu->cpum.GstCtx.hwvirt.svm.fInterceptEvents = !pEventInject->n.u1Valid;
775 if (pEventInject->n.u1Valid)
776 {
777 uint8_t const uVector = pEventInject->n.u8Vector;
778 TRPMEVENT const enmType = HMSvmEventToTrpmEventType(pEventInject);
779 uint16_t const uErrorCode = pEventInject->n.u1ErrorCodeValid ? pEventInject->n.u32ErrorCode : 0;
780
781 /* Validate vectors for hardware exceptions, see AMD spec. 15.20 "Event Injection". */
782 if (RT_UNLIKELY(enmType == TRPM_32BIT_HACK))
783 {
784 Log(("iemSvmVmrun: Invalid event type =%#x -> #VMEXIT\n", (uint8_t)pEventInject->n.u3Type));
785 return iemSvmVmexit(pVCpu, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
786 }
787 if (pEventInject->n.u3Type == SVM_EVENT_EXCEPTION)
788 {
789 if ( uVector == X86_XCPT_NMI
790 || uVector > X86_XCPT_LAST)
791 {
792 Log(("iemSvmVmrun: Invalid vector for hardware exception. uVector=%#x -> #VMEXIT\n", uVector));
793 return iemSvmVmexit(pVCpu, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
794 }
795 if ( uVector == X86_XCPT_BR
796 && CPUMIsGuestInLongModeEx(IEM_GET_CTX(pVCpu)))
797 {
798 Log(("iemSvmVmrun: Cannot inject #BR when not in long mode -> #VMEXIT\n"));
799 return iemSvmVmexit(pVCpu, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
800 }
801 /** @todo any others? */
802 }
803
804 /*
805 * Invalidate the exit interrupt-information field here. This field is fully updated
806 * on #VMEXIT as events other than the one below can also cause intercepts during
807 * their injection (e.g. exceptions).
808 */
809 pVmcbCtrl->ExitIntInfo.n.u1Valid = 0;
810
811 /*
812 * Clear the event injection valid bit here. While the AMD spec. mentions that the CPU
813 * clears this bit from the VMCB unconditionally on #VMEXIT, internally the CPU could be
814 * clearing it at any time, most likely before/after injecting the event. Since VirtualBox
815 * doesn't have any virtual-CPU internal representation of this bit, we clear/update the
816 * VMCB here. This also has the added benefit that we avoid the risk of injecting the event
817 * twice if we fallback to executing the nested-guest using hardware-assisted SVM after
818 * injecting the event through IEM here.
819 */
820 pVmcbCtrl->EventInject.n.u1Valid = 0;
821
822 /** @todo NRIP: Software interrupts can only be pushed properly if we support
823 * NRIP for the nested-guest to calculate the instruction length
824 * below. */
825 LogFlow(("iemSvmVmrun: Injecting event: %04x:%08RX64 vec=%#x type=%d uErr=%u cr2=%#RX64 cr3=%#RX64 efer=%#RX64\n",
826 pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip, uVector, enmType, uErrorCode, pVCpu->cpum.GstCtx.cr2, pVCpu->cpum.GstCtx.cr3, pVCpu->cpum.GstCtx.msrEFER));
827
828 /*
829 * We shall not inject the event here right away. There may be paging mode related updates
830 * as a result of the world-switch above that are yet to be honored. Instead flag the event
831 * as pending for injection.
832 */
833 TRPMAssertTrap(pVCpu, uVector, enmType);
834 if (pEventInject->n.u1ErrorCodeValid)
835 TRPMSetErrorCode(pVCpu, uErrorCode);
836 if ( enmType == TRPM_TRAP
837 && uVector == X86_XCPT_PF)
838 TRPMSetFaultAddress(pVCpu, pVCpu->cpum.GstCtx.cr2);
839 }
840 else
841 LogFlow(("iemSvmVmrun: Entering nested-guest: %04x:%08RX64 cr0=%#RX64 cr3=%#RX64 cr4=%#RX64 efer=%#RX64 efl=%#x\n",
842 pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip, pVCpu->cpum.GstCtx.cr0, pVCpu->cpum.GstCtx.cr3, pVCpu->cpum.GstCtx.cr4, pVCpu->cpum.GstCtx.msrEFER, pVCpu->cpum.GstCtx.rflags.u64));
843
844 LogFlow(("iemSvmVmrun: returns %d\n", VBOXSTRICTRC_VAL(rcStrict)));
845
846# if defined(VBOX_WITH_NESTED_HWVIRT_ONLY_IN_IEM) && defined(IN_RING3)
847 /* If CLGI/STGI isn't intercepted we force IEM-only nested-guest execution here. */
848 if (HMSvmIsVGifActive(pVM))
849 return EMR3SetExecutionPolicy(pVCpu->CTX_SUFF(pVM)->pUVM, EMEXECPOLICY_IEM_ALL, true);
850# endif
851
852 return rcStrict;
853 }
854
855 /* Shouldn't really happen as the caller should've validated the physical address already. */
856 Log(("iemSvmVmrun: Failed to read nested-guest VMCB at %#RGp (rc=%Rrc) -> #VMEXIT\n", GCPhysVmcb, rc));
857 return rc;
858}
859
860
861/**
862 * Checks if the event intercepts and performs the \#VMEXIT if the corresponding
863 * intercept is active.
864 *
865 * @returns Strict VBox status code.
866 * @retval VINF_HM_INTERCEPT_NOT_ACTIVE if the intercept is not active or
867 * we're not executing a nested-guest.
868 * @retval VINF_SVM_VMEXIT if the intercept is active and the \#VMEXIT occurred
869 * successfully.
870 * @retval VERR_SVM_VMEXIT_FAILED if the intercept is active and the \#VMEXIT
871 * failed and a shutdown needs to be initiated for the geust.
872 *
873 * @returns VBox strict status code.
874 * @param pVCpu The cross context virtual CPU structure of the calling thread.
875 * @param u8Vector The interrupt or exception vector.
876 * @param fFlags The exception flags (see IEM_XCPT_FLAGS_XXX).
877 * @param uErr The error-code associated with the exception.
878 * @param uCr2 The CR2 value in case of a \#PF exception.
879 */
880IEM_STATIC VBOXSTRICTRC iemHandleSvmEventIntercept(PVMCPU pVCpu, uint8_t u8Vector, uint32_t fFlags, uint32_t uErr, uint64_t uCr2)
881{
882 Assert(CPUMIsGuestInSvmNestedHwVirtMode(IEM_GET_CTX(pVCpu)));
883
884 /*
885 * Handle SVM exception and software interrupt intercepts, see AMD spec. 15.12 "Exception Intercepts".
886 *
887 * - NMI intercepts have their own exit code and do not cause SVM_EXIT_XCPT_2 #VMEXITs.
888 * - External interrupts and software interrupts (INTn instruction) do not check the exception intercepts
889 * even when they use a vector in the range 0 to 31.
890 * - ICEBP should not trigger #DB intercept, but its own intercept.
891 * - For #PF exceptions, its intercept is checked before CR2 is written by the exception.
892 */
893 /* Check NMI intercept */
894 if ( u8Vector == X86_XCPT_NMI
895 && (fFlags & IEM_XCPT_FLAGS_T_CPU_XCPT)
896 && IEM_IS_SVM_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_NMI))
897 {
898 Log2(("iemHandleSvmNstGstEventIntercept: NMI intercept -> #VMEXIT\n"));
899 IEM_RETURN_SVM_VMEXIT(pVCpu, SVM_EXIT_NMI, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
900 }
901
902 /* Check ICEBP intercept. */
903 if ( (fFlags & IEM_XCPT_FLAGS_ICEBP_INSTR)
904 && IEM_IS_SVM_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_ICEBP))
905 {
906 Log2(("iemHandleSvmNstGstEventIntercept: ICEBP intercept -> #VMEXIT\n"));
907 IEM_SVM_UPDATE_NRIP(pVCpu);
908 IEM_RETURN_SVM_VMEXIT(pVCpu, SVM_EXIT_ICEBP, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
909 }
910
911 /* Check CPU exception intercepts. */
912 if ( (fFlags & IEM_XCPT_FLAGS_T_CPU_XCPT)
913 && IEM_IS_SVM_XCPT_INTERCEPT_SET(pVCpu, u8Vector))
914 {
915 Assert(u8Vector <= X86_XCPT_LAST);
916 uint64_t const uExitInfo1 = fFlags & IEM_XCPT_FLAGS_ERR ? uErr : 0;
917 uint64_t const uExitInfo2 = fFlags & IEM_XCPT_FLAGS_CR2 ? uCr2 : 0;
918 if ( IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSvmDecodeAssists
919 && u8Vector == X86_XCPT_PF
920 && !(uErr & X86_TRAP_PF_ID))
921 {
922 PSVMVMCBCTRL pVmcbCtrl = &pVCpu->cpum.GstCtx.hwvirt.svm.CTX_SUFF(pVmcb)->ctrl;
923# ifdef IEM_WITH_CODE_TLB
924 uint8_t const *pbInstrBuf = pVCpu->iem.s.pbInstrBuf;
925 uint8_t const cbInstrBuf = pVCpu->iem.s.cbInstrBuf;
926 pVmcbCtrl->cbInstrFetched = RT_MIN(cbInstrBuf, SVM_CTRL_GUEST_INSTR_BYTES_MAX);
927 if ( pbInstrBuf
928 && cbInstrBuf > 0)
929 memcpy(&pVmcbCtrl->abInstr[0], pbInstrBuf, pVmcbCtrl->cbInstrFetched);
930# else
931 uint8_t const cbOpcode = pVCpu->iem.s.cbOpcode;
932 pVmcbCtrl->cbInstrFetched = RT_MIN(cbOpcode, SVM_CTRL_GUEST_INSTR_BYTES_MAX);
933 if (cbOpcode > 0)
934 memcpy(&pVmcbCtrl->abInstr[0], &pVCpu->iem.s.abOpcode[0], pVmcbCtrl->cbInstrFetched);
935# endif
936 }
937 if (u8Vector == X86_XCPT_BR)
938 IEM_SVM_UPDATE_NRIP(pVCpu);
939 Log2(("iemHandleSvmNstGstEventIntercept: Xcpt intercept u32InterceptXcpt=%#RX32 u8Vector=%#x "
940 "uExitInfo1=%#RX64 uExitInfo2=%#RX64 -> #VMEXIT\n", pVCpu->cpum.GstCtx.hwvirt.svm.CTX_SUFF(pVmcb)->ctrl.u32InterceptXcpt,
941 u8Vector, uExitInfo1, uExitInfo2));
942 IEM_RETURN_SVM_VMEXIT(pVCpu, SVM_EXIT_XCPT_0 + u8Vector, uExitInfo1, uExitInfo2);
943 }
944
945 /* Check software interrupt (INTn) intercepts. */
946 if ( (fFlags & ( IEM_XCPT_FLAGS_T_SOFT_INT
947 | IEM_XCPT_FLAGS_BP_INSTR
948 | IEM_XCPT_FLAGS_ICEBP_INSTR
949 | IEM_XCPT_FLAGS_OF_INSTR)) == IEM_XCPT_FLAGS_T_SOFT_INT
950 && IEM_IS_SVM_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_INTN))
951 {
952 uint64_t const uExitInfo1 = IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSvmDecodeAssists ? u8Vector : 0;
953 Log2(("iemHandleSvmNstGstEventIntercept: Software INT intercept (u8Vector=%#x) -> #VMEXIT\n", u8Vector));
954 IEM_SVM_UPDATE_NRIP(pVCpu);
955 IEM_RETURN_SVM_VMEXIT(pVCpu, SVM_EXIT_SWINT, uExitInfo1, 0 /* uExitInfo2 */);
956 }
957
958 return VINF_HM_INTERCEPT_NOT_ACTIVE;
959}
960
961
962/**
963 * Checks the SVM IO permission bitmap and performs the \#VMEXIT if the
964 * corresponding intercept is active.
965 *
966 * @returns Strict VBox status code.
967 * @retval VINF_HM_INTERCEPT_NOT_ACTIVE if the intercept is not active or
968 * we're not executing a nested-guest.
969 * @retval VINF_SVM_VMEXIT if the intercept is active and the \#VMEXIT occurred
970 * successfully.
971 * @retval VERR_SVM_VMEXIT_FAILED if the intercept is active and the \#VMEXIT
972 * failed and a shutdown needs to be initiated for the geust.
973 *
974 * @returns VBox strict status code.
975 * @param pVCpu The cross context virtual CPU structure of the calling thread.
976 * @param u16Port The IO port being accessed.
977 * @param enmIoType The type of IO access.
978 * @param cbReg The IO operand size in bytes.
979 * @param cAddrSizeBits The address size bits (for 16, 32 or 64).
980 * @param iEffSeg The effective segment number.
981 * @param fRep Whether this is a repeating IO instruction (REP prefix).
982 * @param fStrIo Whether this is a string IO instruction.
983 * @param cbInstr The length of the IO instruction in bytes.
984 */
985IEM_STATIC VBOXSTRICTRC iemSvmHandleIOIntercept(PVMCPU pVCpu, uint16_t u16Port, SVMIOIOTYPE enmIoType, uint8_t cbReg,
986 uint8_t cAddrSizeBits, uint8_t iEffSeg, bool fRep, bool fStrIo, uint8_t cbInstr)
987{
988 Assert(IEM_IS_SVM_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_IOIO_PROT));
989 Assert(cAddrSizeBits == 16 || cAddrSizeBits == 32 || cAddrSizeBits == 64);
990 Assert(cbReg == 1 || cbReg == 2 || cbReg == 4 || cbReg == 8);
991
992 Log3(("iemSvmHandleIOIntercept: u16Port=%#x (%u)\n", u16Port, u16Port));
993
994 SVMIOIOEXITINFO IoExitInfo;
995 void *pvIoBitmap = pVCpu->cpum.GstCtx.hwvirt.svm.CTX_SUFF(pvIoBitmap);
996 bool const fIntercept = HMSvmIsIOInterceptActive(pvIoBitmap, u16Port, enmIoType, cbReg, cAddrSizeBits, iEffSeg, fRep, fStrIo,
997 &IoExitInfo);
998 if (fIntercept)
999 {
1000 Log3(("iemSvmHandleIOIntercept: u16Port=%#x (%u) -> #VMEXIT\n", u16Port, u16Port));
1001 IEM_SVM_UPDATE_NRIP(pVCpu);
1002 return iemSvmVmexit(pVCpu, SVM_EXIT_IOIO, IoExitInfo.u, pVCpu->cpum.GstCtx.rip + cbInstr);
1003 }
1004
1005 /** @todo remove later (for debugging as VirtualBox always traps all IO
1006 * intercepts). */
1007 AssertMsgFailed(("iemSvmHandleIOIntercept: We expect an IO intercept here!\n"));
1008 return VINF_HM_INTERCEPT_NOT_ACTIVE;
1009}
1010
1011
1012/**
1013 * Checks the SVM MSR permission bitmap and performs the \#VMEXIT if the
1014 * corresponding intercept is active.
1015 *
1016 * @returns Strict VBox status code.
1017 * @retval VINF_HM_INTERCEPT_NOT_ACTIVE if the MSR permission bitmap does not
1018 * specify interception of the accessed MSR @a idMsr.
1019 * @retval VINF_SVM_VMEXIT if the intercept is active and the \#VMEXIT occurred
1020 * successfully.
1021 * @retval VERR_SVM_VMEXIT_FAILED if the intercept is active and the \#VMEXIT
1022 * failed and a shutdown needs to be initiated for the geust.
1023 *
1024 * @param pVCpu The cross context virtual CPU structure.
1025 * @param idMsr The MSR being accessed in the nested-guest.
1026 * @param fWrite Whether this is an MSR write access, @c false implies an
1027 * MSR read.
1028 * @param cbInstr The length of the MSR read/write instruction in bytes.
1029 */
1030IEM_STATIC VBOXSTRICTRC iemSvmHandleMsrIntercept(PVMCPU pVCpu, uint32_t idMsr, bool fWrite)
1031{
1032 /*
1033 * Check if any MSRs are being intercepted.
1034 */
1035 Assert(CPUMIsGuestSvmCtrlInterceptSet(pVCpu, IEM_GET_CTX(pVCpu), SVM_CTRL_INTERCEPT_MSR_PROT));
1036 Assert(CPUMIsGuestInSvmNestedHwVirtMode(IEM_GET_CTX(pVCpu)));
1037
1038 uint64_t const uExitInfo1 = fWrite ? SVM_EXIT1_MSR_WRITE : SVM_EXIT1_MSR_READ;
1039
1040 /*
1041 * Get the byte and bit offset of the permission bits corresponding to the MSR.
1042 */
1043 uint16_t offMsrpm;
1044 uint8_t uMsrpmBit;
1045 int rc = HMSvmGetMsrpmOffsetAndBit(idMsr, &offMsrpm, &uMsrpmBit);
1046 if (RT_SUCCESS(rc))
1047 {
1048 Assert(uMsrpmBit == 0 || uMsrpmBit == 2 || uMsrpmBit == 4 || uMsrpmBit == 6);
1049 Assert(offMsrpm < SVM_MSRPM_PAGES << X86_PAGE_4K_SHIFT);
1050 if (fWrite)
1051 ++uMsrpmBit;
1052
1053 /*
1054 * Check if the bit is set, if so, trigger a #VMEXIT.
1055 */
1056 uint8_t *pbMsrpm = (uint8_t *)pVCpu->cpum.GstCtx.hwvirt.svm.CTX_SUFF(pvMsrBitmap);
1057 pbMsrpm += offMsrpm;
1058 if (*pbMsrpm & RT_BIT(uMsrpmBit))
1059 {
1060 IEM_SVM_UPDATE_NRIP(pVCpu);
1061 return iemSvmVmexit(pVCpu, SVM_EXIT_MSR, uExitInfo1, 0 /* uExitInfo2 */);
1062 }
1063 }
1064 else
1065 {
1066 /*
1067 * This shouldn't happen, but if it does, cause a #VMEXIT and let the "host" (guest hypervisor) deal with it.
1068 */
1069 Log(("iemSvmHandleMsrIntercept: Invalid/out-of-range MSR %#RX32 fWrite=%RTbool -> #VMEXIT\n", idMsr, fWrite));
1070 return iemSvmVmexit(pVCpu, SVM_EXIT_MSR, uExitInfo1, 0 /* uExitInfo2 */);
1071 }
1072 return VINF_HM_INTERCEPT_NOT_ACTIVE;
1073}
1074
1075
1076
1077/**
1078 * Implements 'VMRUN'.
1079 */
1080IEM_CIMPL_DEF_0(iemCImpl_vmrun)
1081{
1082# if defined(VBOX_WITH_NESTED_HWVIRT_ONLY_IN_IEM) && !defined(IN_RING3)
1083 RT_NOREF2(pVCpu, cbInstr);
1084 return VINF_EM_RAW_EMULATE_INSTR;
1085# else
1086 LogFlow(("iemCImpl_vmrun\n"));
1087 IEM_SVM_INSTR_COMMON_CHECKS(pVCpu, vmrun);
1088
1089 /** @todo Check effective address size using address size prefix. */
1090 RTGCPHYS const GCPhysVmcb = pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT ? pVCpu->cpum.GstCtx.rax : pVCpu->cpum.GstCtx.eax;
1091 if ( (GCPhysVmcb & X86_PAGE_4K_OFFSET_MASK)
1092 || !PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), GCPhysVmcb))
1093 {
1094 Log(("vmrun: VMCB physaddr (%#RGp) not valid -> #GP(0)\n", GCPhysVmcb));
1095 return iemRaiseGeneralProtectionFault0(pVCpu);
1096 }
1097
1098 if (IEM_IS_SVM_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_VMRUN))
1099 {
1100 Log(("vmrun: Guest intercept -> #VMEXIT\n"));
1101 IEM_RETURN_SVM_VMEXIT(pVCpu, SVM_EXIT_VMRUN, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
1102 }
1103
1104 VBOXSTRICTRC rcStrict = iemSvmVmrun(pVCpu, cbInstr, GCPhysVmcb);
1105 if (rcStrict == VERR_SVM_VMEXIT_FAILED)
1106 {
1107 Assert(!CPUMIsGuestInSvmNestedHwVirtMode(IEM_GET_CTX(pVCpu)));
1108 rcStrict = VINF_EM_TRIPLE_FAULT;
1109 }
1110 return rcStrict;
1111# endif
1112}
1113
1114#endif /* VBOX_WITH_NESTED_HWVIRT_SVM */
1115
1116/**
1117 * Common code for iemCImpl_vmmcall and iemCImpl_vmcall (latter in IEMAllCImplVmxInstr.cpp.h).
1118 */
1119IEM_CIMPL_DEF_1(iemCImpl_Hypercall, uint16_t, uDisOpcode)
1120{
1121 if (EMAreHypercallInstructionsEnabled(pVCpu))
1122 {
1123 NOREF(uDisOpcode);
1124 VBOXSTRICTRC rcStrict = GIMHypercallEx(pVCpu, IEM_GET_CTX(pVCpu), uDisOpcode, cbInstr);
1125 if (RT_SUCCESS(rcStrict))
1126 {
1127 if (rcStrict == VINF_SUCCESS)
1128 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1129 if ( rcStrict == VINF_SUCCESS
1130 || rcStrict == VINF_GIM_HYPERCALL_CONTINUING)
1131 return VINF_SUCCESS;
1132 AssertMsgReturn(rcStrict == VINF_GIM_R3_HYPERCALL, ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)), VERR_IEM_IPE_4);
1133 return rcStrict;
1134 }
1135 AssertMsgReturn( rcStrict == VERR_GIM_HYPERCALL_ACCESS_DENIED
1136 || rcStrict == VERR_GIM_HYPERCALLS_NOT_AVAILABLE
1137 || rcStrict == VERR_GIM_NOT_ENABLED
1138 || rcStrict == VERR_GIM_HYPERCALL_MEMORY_READ_FAILED
1139 || rcStrict == VERR_GIM_HYPERCALL_MEMORY_WRITE_FAILED,
1140 ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)), VERR_IEM_IPE_4);
1141
1142 /* Raise #UD on all failures. */
1143 }
1144 return iemRaiseUndefinedOpcode(pVCpu);
1145}
1146
1147/**
1148 * Implements 'VMMCALL'.
1149 */
1150IEM_CIMPL_DEF_0(iemCImpl_vmmcall)
1151{
1152 if (IEM_IS_SVM_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_VMMCALL))
1153 {
1154 Log(("vmmcall: Guest intercept -> #VMEXIT\n"));
1155 IEM_RETURN_SVM_VMEXIT(pVCpu, SVM_EXIT_VMMCALL, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
1156 }
1157
1158#ifndef IN_RC
1159 /* This is a little bit more complicated than the VT-x version because HM/SVM may
1160 patch MOV CR8 instructions too speed up APIC.TPR access for 32-bit windows guests. */
1161 if (VM_IS_HM_ENABLED(pVCpu->CTX_SUFF(pVM)))
1162 {
1163 int rc = HMHCSvmMaybeMovTprHypercall(pVCpu, IEM_GET_CTX(pVCpu));
1164 if (RT_SUCCESS(rc))
1165 {
1166 Log(("vmmcall: MovTrp\n"));
1167 return VINF_SUCCESS;
1168 }
1169 }
1170#endif
1171
1172 /* Join forces with vmcall. */
1173 return IEM_CIMPL_CALL_1(iemCImpl_Hypercall, OP_VMMCALL);
1174}
1175
1176#ifdef VBOX_WITH_NESTED_HWVIRT_SVM
1177
1178/**
1179 * Implements 'VMLOAD'.
1180 */
1181IEM_CIMPL_DEF_0(iemCImpl_vmload)
1182{
1183# if defined(VBOX_WITH_NESTED_HWVIRT_ONLY_IN_IEM) && !defined(IN_RING3)
1184 RT_NOREF2(pVCpu, cbInstr);
1185 return VINF_EM_RAW_EMULATE_INSTR;
1186# else
1187 LogFlow(("iemCImpl_vmload\n"));
1188 IEM_SVM_INSTR_COMMON_CHECKS(pVCpu, vmload);
1189
1190 /** @todo Check effective address size using address size prefix. */
1191 RTGCPHYS const GCPhysVmcb = pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT ? pVCpu->cpum.GstCtx.rax : pVCpu->cpum.GstCtx.eax;
1192 if ( (GCPhysVmcb & X86_PAGE_4K_OFFSET_MASK)
1193 || !PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), GCPhysVmcb))
1194 {
1195 Log(("vmload: VMCB physaddr (%#RGp) not valid -> #GP(0)\n", GCPhysVmcb));
1196 return iemRaiseGeneralProtectionFault0(pVCpu);
1197 }
1198
1199 if (IEM_IS_SVM_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_VMLOAD))
1200 {
1201 Log(("vmload: Guest intercept -> #VMEXIT\n"));
1202 IEM_RETURN_SVM_VMEXIT(pVCpu, SVM_EXIT_VMLOAD, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
1203 }
1204
1205 SVMVMCBSTATESAVE VmcbNstGst;
1206 VBOXSTRICTRC rcStrict = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), &VmcbNstGst, GCPhysVmcb + RT_OFFSETOF(SVMVMCB, guest),
1207 sizeof(SVMVMCBSTATESAVE));
1208 if (rcStrict == VINF_SUCCESS)
1209 {
1210 LogFlow(("vmload: Loading VMCB at %#RGp enmEffAddrMode=%d\n", GCPhysVmcb, pVCpu->iem.s.enmEffAddrMode));
1211 HMSVM_SEG_REG_COPY_FROM_VMCB(IEM_GET_CTX(pVCpu), &VmcbNstGst, FS, fs);
1212 HMSVM_SEG_REG_COPY_FROM_VMCB(IEM_GET_CTX(pVCpu), &VmcbNstGst, GS, gs);
1213 HMSVM_SEG_REG_COPY_FROM_VMCB(IEM_GET_CTX(pVCpu), &VmcbNstGst, TR, tr);
1214 HMSVM_SEG_REG_COPY_FROM_VMCB(IEM_GET_CTX(pVCpu), &VmcbNstGst, LDTR, ldtr);
1215
1216 pVCpu->cpum.GstCtx.msrKERNELGSBASE = VmcbNstGst.u64KernelGSBase;
1217 pVCpu->cpum.GstCtx.msrSTAR = VmcbNstGst.u64STAR;
1218 pVCpu->cpum.GstCtx.msrLSTAR = VmcbNstGst.u64LSTAR;
1219 pVCpu->cpum.GstCtx.msrCSTAR = VmcbNstGst.u64CSTAR;
1220 pVCpu->cpum.GstCtx.msrSFMASK = VmcbNstGst.u64SFMASK;
1221
1222 pVCpu->cpum.GstCtx.SysEnter.cs = VmcbNstGst.u64SysEnterCS;
1223 pVCpu->cpum.GstCtx.SysEnter.esp = VmcbNstGst.u64SysEnterESP;
1224 pVCpu->cpum.GstCtx.SysEnter.eip = VmcbNstGst.u64SysEnterEIP;
1225
1226 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1227 }
1228 return rcStrict;
1229# endif
1230}
1231
1232
1233/**
1234 * Implements 'VMSAVE'.
1235 */
1236IEM_CIMPL_DEF_0(iemCImpl_vmsave)
1237{
1238# if defined(VBOX_WITH_NESTED_HWVIRT_ONLY_IN_IEM) && !defined(IN_RING3)
1239 RT_NOREF2(pVCpu, cbInstr);
1240 return VINF_EM_RAW_EMULATE_INSTR;
1241# else
1242 LogFlow(("iemCImpl_vmsave\n"));
1243 IEM_SVM_INSTR_COMMON_CHECKS(pVCpu, vmsave);
1244
1245 /** @todo Check effective address size using address size prefix. */
1246 RTGCPHYS const GCPhysVmcb = pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT ? pVCpu->cpum.GstCtx.rax : pVCpu->cpum.GstCtx.eax;
1247 if ( (GCPhysVmcb & X86_PAGE_4K_OFFSET_MASK)
1248 || !PGMPhysIsGCPhysNormal(pVCpu->CTX_SUFF(pVM), GCPhysVmcb))
1249 {
1250 Log(("vmsave: VMCB physaddr (%#RGp) not valid -> #GP(0)\n", GCPhysVmcb));
1251 return iemRaiseGeneralProtectionFault0(pVCpu);
1252 }
1253
1254 if (IEM_IS_SVM_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_VMSAVE))
1255 {
1256 Log(("vmsave: Guest intercept -> #VMEXIT\n"));
1257 IEM_RETURN_SVM_VMEXIT(pVCpu, SVM_EXIT_VMSAVE, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
1258 }
1259
1260 SVMVMCBSTATESAVE VmcbNstGst;
1261 VBOXSTRICTRC rcStrict = PGMPhysSimpleReadGCPhys(pVCpu->CTX_SUFF(pVM), &VmcbNstGst, GCPhysVmcb + RT_OFFSETOF(SVMVMCB, guest),
1262 sizeof(SVMVMCBSTATESAVE));
1263 if (rcStrict == VINF_SUCCESS)
1264 {
1265 LogFlow(("vmsave: Saving VMCB at %#RGp enmEffAddrMode=%d\n", GCPhysVmcb, pVCpu->iem.s.enmEffAddrMode));
1266 HMSVM_SEG_REG_COPY_TO_VMCB(IEM_GET_CTX(pVCpu), &VmcbNstGst, FS, fs);
1267 HMSVM_SEG_REG_COPY_TO_VMCB(IEM_GET_CTX(pVCpu), &VmcbNstGst, GS, gs);
1268 HMSVM_SEG_REG_COPY_TO_VMCB(IEM_GET_CTX(pVCpu), &VmcbNstGst, TR, tr);
1269 HMSVM_SEG_REG_COPY_TO_VMCB(IEM_GET_CTX(pVCpu), &VmcbNstGst, LDTR, ldtr);
1270
1271 VmcbNstGst.u64KernelGSBase = pVCpu->cpum.GstCtx.msrKERNELGSBASE;
1272 VmcbNstGst.u64STAR = pVCpu->cpum.GstCtx.msrSTAR;
1273 VmcbNstGst.u64LSTAR = pVCpu->cpum.GstCtx.msrLSTAR;
1274 VmcbNstGst.u64CSTAR = pVCpu->cpum.GstCtx.msrCSTAR;
1275 VmcbNstGst.u64SFMASK = pVCpu->cpum.GstCtx.msrSFMASK;
1276
1277 VmcbNstGst.u64SysEnterCS = pVCpu->cpum.GstCtx.SysEnter.cs;
1278 VmcbNstGst.u64SysEnterESP = pVCpu->cpum.GstCtx.SysEnter.esp;
1279 VmcbNstGst.u64SysEnterEIP = pVCpu->cpum.GstCtx.SysEnter.eip;
1280
1281 rcStrict = PGMPhysSimpleWriteGCPhys(pVCpu->CTX_SUFF(pVM), GCPhysVmcb + RT_OFFSETOF(SVMVMCB, guest), &VmcbNstGst,
1282 sizeof(SVMVMCBSTATESAVE));
1283 if (rcStrict == VINF_SUCCESS)
1284 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1285 }
1286 return rcStrict;
1287# endif
1288}
1289
1290
1291/**
1292 * Implements 'CLGI'.
1293 */
1294IEM_CIMPL_DEF_0(iemCImpl_clgi)
1295{
1296# if defined(VBOX_WITH_NESTED_HWVIRT_ONLY_IN_IEM) && !defined(IN_RING3)
1297 RT_NOREF2(pVCpu, cbInstr);
1298 return VINF_EM_RAW_EMULATE_INSTR;
1299# else
1300 LogFlow(("iemCImpl_clgi\n"));
1301 IEM_SVM_INSTR_COMMON_CHECKS(pVCpu, clgi);
1302 if (IEM_IS_SVM_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_CLGI))
1303 {
1304 Log(("clgi: Guest intercept -> #VMEXIT\n"));
1305 IEM_RETURN_SVM_VMEXIT(pVCpu, SVM_EXIT_CLGI, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
1306 }
1307
1308 pVCpu->cpum.GstCtx.hwvirt.fGif = false;
1309 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1310
1311# if defined(VBOX_WITH_NESTED_HWVIRT_ONLY_IN_IEM) && defined(IN_RING3)
1312 return EMR3SetExecutionPolicy(pVCpu->CTX_SUFF(pVM)->pUVM, EMEXECPOLICY_IEM_ALL, true);
1313# else
1314 return VINF_SUCCESS;
1315# endif
1316# endif
1317}
1318
1319
1320/**
1321 * Implements 'STGI'.
1322 */
1323IEM_CIMPL_DEF_0(iemCImpl_stgi)
1324{
1325# if defined(VBOX_WITH_NESTED_HWVIRT_ONLY_IN_IEM) && !defined(IN_RING3)
1326 RT_NOREF2(pVCpu, cbInstr);
1327 return VINF_EM_RAW_EMULATE_INSTR;
1328# else
1329 LogFlow(("iemCImpl_stgi\n"));
1330 IEM_SVM_INSTR_COMMON_CHECKS(pVCpu, stgi);
1331 if (IEM_IS_SVM_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_STGI))
1332 {
1333 Log2(("stgi: Guest intercept -> #VMEXIT\n"));
1334 IEM_RETURN_SVM_VMEXIT(pVCpu, SVM_EXIT_STGI, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
1335 }
1336
1337 pVCpu->cpum.GstCtx.hwvirt.fGif = true;
1338 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1339
1340# if defined(VBOX_WITH_NESTED_HWVIRT_ONLY_IN_IEM) && defined(IN_RING3)
1341 return EMR3SetExecutionPolicy(pVCpu->CTX_SUFF(pVM)->pUVM, EMEXECPOLICY_IEM_ALL, false);
1342# else
1343 return VINF_SUCCESS;
1344# endif
1345# endif
1346}
1347
1348
1349/**
1350 * Implements 'INVLPGA'.
1351 */
1352IEM_CIMPL_DEF_0(iemCImpl_invlpga)
1353{
1354 /** @todo Check effective address size using address size prefix. */
1355 RTGCPTR const GCPtrPage = pVCpu->iem.s.enmCpuMode == IEMMODE_64BIT ? pVCpu->cpum.GstCtx.rax : pVCpu->cpum.GstCtx.eax;
1356 /** @todo PGM needs virtual ASID support. */
1357# if 0
1358 uint32_t const uAsid = pVCpu->cpum.GstCtx.ecx;
1359# endif
1360
1361 IEM_SVM_INSTR_COMMON_CHECKS(pVCpu, invlpga);
1362 if (IEM_IS_SVM_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_INVLPGA))
1363 {
1364 Log2(("invlpga: Guest intercept (%RGp) -> #VMEXIT\n", GCPtrPage));
1365 IEM_RETURN_SVM_VMEXIT(pVCpu, SVM_EXIT_INVLPGA, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
1366 }
1367
1368 PGMInvalidatePage(pVCpu, GCPtrPage);
1369 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1370 return VINF_SUCCESS;
1371}
1372
1373
1374/**
1375 * Implements 'SKINIT'.
1376 */
1377IEM_CIMPL_DEF_0(iemCImpl_skinit)
1378{
1379 IEM_SVM_INSTR_COMMON_CHECKS(pVCpu, invlpga);
1380
1381 uint32_t uIgnore;
1382 uint32_t fFeaturesECX;
1383 CPUMGetGuestCpuId(pVCpu, 0x80000001, 0 /* iSubLeaf */, &uIgnore, &uIgnore, &fFeaturesECX, &uIgnore);
1384 if (!(fFeaturesECX & X86_CPUID_AMD_FEATURE_ECX_SKINIT))
1385 return iemRaiseUndefinedOpcode(pVCpu);
1386
1387 if (IEM_IS_SVM_CTRL_INTERCEPT_SET(pVCpu, SVM_CTRL_INTERCEPT_SKINIT))
1388 {
1389 Log2(("skinit: Guest intercept -> #VMEXIT\n"));
1390 IEM_RETURN_SVM_VMEXIT(pVCpu, SVM_EXIT_SKINIT, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
1391 }
1392
1393 RT_NOREF(cbInstr);
1394 return VERR_IEM_INSTR_NOT_IMPLEMENTED;
1395}
1396
1397IEM_CIMPL_DEF_0(iemCImpl_svm_pause)
1398{
1399 bool fCheckIntercept = true;
1400 if (IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSvmPauseFilter)
1401 {
1402 /* TSC based pause-filter thresholding. */
1403 if ( IEM_GET_GUEST_CPU_FEATURES(pVCpu)->fSvmPauseFilterThreshold
1404 && pVCpu->cpum.GstCtx.hwvirt.svm.cPauseFilterThreshold > 0)
1405 {
1406 uint64_t const uTick = TMCpuTickGet(pVCpu);
1407 if (uTick - pVCpu->cpum.GstCtx.hwvirt.svm.uPrevPauseTick > pVCpu->cpum.GstCtx.hwvirt.svm.cPauseFilterThreshold)
1408 pVCpu->cpum.GstCtx.hwvirt.svm.cPauseFilter = IEM_GET_SVM_PAUSE_FILTER_COUNT(pVCpu);
1409 pVCpu->cpum.GstCtx.hwvirt.svm.uPrevPauseTick = uTick;
1410 }
1411
1412 /* Simple pause-filter counter. */
1413 if (pVCpu->cpum.GstCtx.hwvirt.svm.cPauseFilter > 0)
1414 {
1415 --pVCpu->cpum.GstCtx.hwvirt.svm.cPauseFilter;
1416 fCheckIntercept = false;
1417 }
1418 }
1419
1420 if (fCheckIntercept)
1421 IEMCIMPL_HLP_SVM_INSTR_INTERCEPT_AND_NRIP(pVCpu, SVM_CTRL_INTERCEPT_PAUSE, SVM_EXIT_PAUSE, 0, 0);
1422
1423 iemRegAddToRipAndClearRF(pVCpu, cbInstr);
1424 return VINF_SUCCESS;
1425}
1426
1427#endif /* VBOX_WITH_NESTED_HWVIRT_SVM */
1428
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