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