VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/HMSVMAll.cpp@ 66550

最後變更 在這個檔案從66550是 66386,由 vboxsync 提交於 8 年 前

VMM: Nested Hw.virt: SVM bits.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 47.8 KB
 
1/* $Id: HMSVMAll.cpp 66386 2017-03-31 16:12:52Z vboxsync $ */
2/** @file
3 * HM SVM (AMD-V) - All contexts.
4 */
5
6/*
7 * Copyright (C) 2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_HM
23#define VMCPU_INCL_CPUM_GST_CTX
24#include "HMInternal.h"
25#include <VBox/vmm/apic.h>
26#include <VBox/vmm/gim.h>
27#include <VBox/vmm/hm.h>
28#include <VBox/vmm/iem.h>
29#include <VBox/vmm/vm.h>
30#include <VBox/vmm/hm_svm.h>
31
32
33#ifndef IN_RC
34/**
35 * Emulates a simple MOV TPR (CR8) instruction, used for TPR patching on 32-bit
36 * guests. This simply looks up the patch record at EIP and does the required.
37 *
38 * This VMMCALL is used a fallback mechanism when mov to/from cr8 isn't exactly
39 * like how we want it to be (e.g. not followed by shr 4 as is usually done for
40 * TPR). See hmR3ReplaceTprInstr() for the details.
41 *
42 * @returns VBox status code.
43 * @retval VINF_SUCCESS if the access was handled successfully.
44 * @retval VERR_NOT_FOUND if no patch record for this RIP could be found.
45 * @retval VERR_SVM_UNEXPECTED_PATCH_TYPE if the found patch type is invalid.
46 *
47 * @param pVCpu The cross context virtual CPU structure.
48 * @param pCtx Pointer to the guest-CPU context.
49 * @param pfUpdateRipAndRF Whether the guest RIP/EIP has been updated as
50 * part of the TPR patch operation.
51 */
52static int hmSvmEmulateMovTpr(PVMCPU pVCpu, PCPUMCTX pCtx, bool *pfUpdateRipAndRF)
53{
54 Log4(("Emulated VMMCall TPR access replacement at RIP=%RGv\n", pCtx->rip));
55
56 /*
57 * We do this in a loop as we increment the RIP after a successful emulation
58 * and the new RIP may be a patched instruction which needs emulation as well.
59 */
60 bool fUpdateRipAndRF = false;
61 bool fPatchFound = false;
62 PVM pVM = pVCpu->CTX_SUFF(pVM);
63 for (;;)
64 {
65 bool fPending;
66 uint8_t u8Tpr;
67
68 PHMTPRPATCH pPatch = (PHMTPRPATCH)RTAvloU32Get(&pVM->hm.s.PatchTree, (AVLOU32KEY)pCtx->eip);
69 if (!pPatch)
70 break;
71
72 fPatchFound = true;
73 switch (pPatch->enmType)
74 {
75 case HMTPRINSTR_READ:
76 {
77 int rc = APICGetTpr(pVCpu, &u8Tpr, &fPending, NULL /* pu8PendingIrq */);
78 AssertRC(rc);
79
80 rc = DISWriteReg32(CPUMCTX2CORE(pCtx), pPatch->uDstOperand, u8Tpr);
81 AssertRC(rc);
82 pCtx->rip += pPatch->cbOp;
83 pCtx->eflags.Bits.u1RF = 0;
84 fUpdateRipAndRF = true;
85 break;
86 }
87
88 case HMTPRINSTR_WRITE_REG:
89 case HMTPRINSTR_WRITE_IMM:
90 {
91 if (pPatch->enmType == HMTPRINSTR_WRITE_REG)
92 {
93 uint32_t u32Val;
94 int rc = DISFetchReg32(CPUMCTX2CORE(pCtx), pPatch->uSrcOperand, &u32Val);
95 AssertRC(rc);
96 u8Tpr = u32Val;
97 }
98 else
99 u8Tpr = (uint8_t)pPatch->uSrcOperand;
100
101 int rc2 = APICSetTpr(pVCpu, u8Tpr);
102 AssertRC(rc2);
103 HMCPU_CF_SET(pVCpu, HM_CHANGED_SVM_GUEST_APIC_STATE);
104
105 pCtx->rip += pPatch->cbOp;
106 pCtx->eflags.Bits.u1RF = 0;
107 fUpdateRipAndRF = true;
108 break;
109 }
110
111 default:
112 {
113 AssertMsgFailed(("Unexpected patch type %d\n", pPatch->enmType));
114 pVCpu->hm.s.u32HMError = pPatch->enmType;
115 *pfUpdateRipAndRF = fUpdateRipAndRF;
116 return VERR_SVM_UNEXPECTED_PATCH_TYPE;
117 }
118 }
119 }
120
121 *pfUpdateRipAndRF = fUpdateRipAndRF;
122 if (fPatchFound)
123 return VINF_SUCCESS;
124 return VERR_NOT_FOUND;
125}
126#endif /* !IN_RC */
127
128
129/**
130 * Performs the operations necessary that are part of the vmmcall instruction
131 * execution in the guest.
132 *
133 * @returns Strict VBox status code (i.e. informational status codes too).
134 * @retval VINF_SUCCESS on successful handling, no \#UD needs to be thrown,
135 * update RIP and eflags.RF depending on @a pfUpdatedRipAndRF and
136 * continue guest execution.
137 * @retval VINF_GIM_HYPERCALL_CONTINUING continue hypercall without updating
138 * RIP.
139 * @retval VINF_GIM_R3_HYPERCALL re-start the hypercall from ring-3.
140 *
141 * @param pVCpu The cross context virtual CPU structure.
142 * @param pCtx Pointer to the guest-CPU context.
143 * @param pfUpdatedRipAndRF Whether the guest RIP/EIP has been updated as
144 * part of handling the VMMCALL operation.
145 */
146VMM_INT_DECL(VBOXSTRICTRC) HMSvmVmmcall(PVMCPU pVCpu, PCPUMCTX pCtx, bool *pfUpdatedRipAndRF)
147{
148#ifndef IN_RC
149 /*
150 * TPR patched instruction emulation for 32-bit guests.
151 */
152 PVM pVM = pVCpu->CTX_SUFF(pVM);
153 if (pVM->hm.s.fTprPatchingAllowed)
154 {
155 int rc = hmSvmEmulateMovTpr(pVCpu, pCtx, pfUpdatedRipAndRF);
156 if (RT_SUCCESS(rc))
157 return VINF_SUCCESS;
158
159 if (rc != VERR_NOT_FOUND)
160 {
161 Log(("hmSvmExitVmmCall: hmSvmEmulateMovTpr returns %Rrc\n", rc));
162 return rc;
163 }
164 }
165#endif
166
167 /*
168 * Paravirtualized hypercalls.
169 */
170 *pfUpdatedRipAndRF = false;
171 if (pVCpu->hm.s.fHypercallsEnabled)
172 return GIMHypercall(pVCpu, pCtx);
173
174 return VERR_NOT_AVAILABLE;
175}
176
177
178#ifndef IN_RC
179/**
180 * Performs the operations necessary that are part of the vmrun instruction
181 * execution in the guest.
182 *
183 * @returns Strict VBox status code (i.e. informational status codes too).
184 * @retval VINF_SUCCESS successully executed VMRUN and entered nested-guest
185 * code execution.
186 * @retval VINF_SVM_VMEXIT when executing VMRUN causes a \#VMEXIT
187 * (SVM_EXIT_INVALID most likely).
188 *
189 * @param pVCpu The cross context virtual CPU structure.
190 * @param pCtx Pointer to the guest-CPU context.
191 * @param GCPhysVmcb Guest physical address of the VMCB to run.
192 */
193/** @todo move this to IEM and make the VMRUN version that can execute under
194 * hardware SVM here instead. */
195VMM_INT_DECL(VBOXSTRICTRC) HMSvmVmrun(PVMCPU pVCpu, PCPUMCTX pCtx, RTGCPHYS GCPhysVmcb)
196{
197 Assert(pVCpu);
198 Assert(pCtx);
199 PVM pVM = pVCpu->CTX_SUFF(pVM);
200
201 /*
202 * Cache the physical address of the VMCB for #VMEXIT exceptions.
203 */
204 pCtx->hwvirt.svm.GCPhysVmcb = GCPhysVmcb;
205
206 /*
207 * Save host state.
208 */
209 SVMVMCBSTATESAVE VmcbNstGst;
210 int rc = PGMPhysSimpleReadGCPhys(pVM, &VmcbNstGst, GCPhysVmcb + RT_OFFSETOF(SVMVMCB, guest), sizeof(SVMVMCBSTATESAVE));
211 if (RT_SUCCESS(rc))
212 {
213 PSVMHOSTSTATE pHostState = &pCtx->hwvirt.svm.HostState;
214 pHostState->es = pCtx->es;
215 pHostState->cs = pCtx->cs;
216 pHostState->ss = pCtx->ss;
217 pHostState->ds = pCtx->ds;
218 pHostState->gdtr = pCtx->gdtr;
219 pHostState->idtr = pCtx->idtr;
220 pHostState->uEferMsr = pCtx->msrEFER;
221 pHostState->uCr0 = pCtx->cr0;
222 pHostState->uCr3 = pCtx->cr3;
223 pHostState->uCr4 = pCtx->cr4;
224 pHostState->rflags = pCtx->rflags;
225 pHostState->uRip = pCtx->rip;
226 pHostState->uRsp = pCtx->rsp;
227 pHostState->uRax = pCtx->rax;
228
229 /*
230 * Load the VMCB controls.
231 */
232 rc = PGMPhysSimpleReadGCPhys(pVM, &pCtx->hwvirt.svm.VmcbCtrl, GCPhysVmcb, sizeof(pCtx->hwvirt.svm.VmcbCtrl));
233 if (RT_SUCCESS(rc))
234 {
235 PSVMVMCBCTRL pVmcbCtrl = &pCtx->hwvirt.svm.VmcbCtrl;
236
237 /*
238 * Validate guest-state and controls.
239 */
240 /* VMRUN must always be intercepted. */
241 if (!CPUMIsGuestSvmCtrlInterceptSet(pCtx, SVM_CTRL_INTERCEPT_VMRUN))
242 {
243 Log(("HMSvmVmRun: VMRUN instruction not intercepted -> #VMEXIT\n"));
244 return HMSvmNstGstVmExit(pVCpu, pCtx, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
245 }
246
247 /* Nested paging. */
248 if ( pVmcbCtrl->NestedPaging.n.u1NestedPaging
249 && !pVM->cpum.ro.GuestFeatures.svm.feat.n.fNestedPaging)
250 {
251 Log(("HMSvmVmRun: Nested paging not supported -> #VMEXIT\n"));
252 return HMSvmNstGstVmExit(pVCpu, pCtx, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
253 }
254
255 /* AVIC. */
256 if ( pVmcbCtrl->IntCtrl.n.u1AvicEnable
257 && !pVM->cpum.ro.GuestFeatures.svm.feat.n.fAvic)
258 {
259 Log(("HMSvmVmRun: AVIC not supported -> #VMEXIT\n"));
260 return HMSvmNstGstVmExit(pVCpu, pCtx, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
261 }
262
263 /* Last branch record (LBR) virtualization. */
264 if ( (pVmcbCtrl->u64LBRVirt & SVM_LBR_VIRT_ENABLE)
265 && !pVM->cpum.ro.GuestFeatures.svm.feat.n.fLbrVirt)
266 {
267 Log(("HMSvmVmRun: LBR virtualization not supported -> #VMEXIT\n"));
268 return HMSvmNstGstVmExit(pVCpu, pCtx, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
269 }
270
271 /* Guest ASID. */
272 if (!pVmcbCtrl->TLBCtrl.n.u32ASID)
273 {
274 Log(("HMSvmVmRun: Guest ASID is invalid -> #VMEXIT\n"));
275 return HMSvmNstGstVmExit(pVCpu, pCtx, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
276 }
277
278 /* IO permission bitmap. */
279 RTGCPHYS GCPhysIOBitmap = pVmcbCtrl->u64IOPMPhysAddr;
280 if ( (GCPhysIOBitmap & X86_PAGE_4K_OFFSET_MASK)
281 || !PGMPhysIsGCPhysNormal(pVM, GCPhysIOBitmap))
282 {
283 Log(("HMSvmVmRun: IO bitmap physaddr invalid. GCPhysIOBitmap=%#RX64 -> #VMEXIT\n", GCPhysIOBitmap));
284 return HMSvmNstGstVmExit(pVCpu, pCtx, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
285 }
286
287 /* MSR permission bitmap. */
288 RTGCPHYS GCPhysMsrBitmap = pVmcbCtrl->u64MSRPMPhysAddr;
289 if ( (GCPhysMsrBitmap & X86_PAGE_4K_OFFSET_MASK)
290 || !PGMPhysIsGCPhysNormal(pVM, GCPhysMsrBitmap))
291 {
292 Log(("HMSvmVmRun: MSR bitmap physaddr invalid. GCPhysMsrBitmap=%#RX64 -> #VMEXIT\n", GCPhysMsrBitmap));
293 return HMSvmNstGstVmExit(pVCpu, pCtx, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
294 }
295
296 /* CR0. */
297 if ( !(VmcbNstGst.u64CR0 & X86_CR0_CD)
298 && (VmcbNstGst.u64CR0 & X86_CR0_NW))
299 {
300 Log(("HMSvmVmRun: CR0 no-write through with cache disabled. CR0=%#RX64 -> #VMEXIT\n", VmcbNstGst.u64CR0));
301 return HMSvmNstGstVmExit(pVCpu, pCtx, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
302 }
303 if (VmcbNstGst.u64CR0 >> 32)
304 {
305 Log(("HMSvmVmRun: CR0 reserved bits set. CR0=%#RX64 -> #VMEXIT\n", VmcbNstGst.u64CR0));
306 return HMSvmNstGstVmExit(pVCpu, pCtx, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
307 }
308 /** @todo Implement all reserved bits/illegal combinations for CR3, CR4. */
309
310 /* DR6 and DR7. */
311 if ( VmcbNstGst.u64DR6 >> 32
312 || VmcbNstGst.u64DR7 >> 32)
313 {
314 Log(("HMSvmVmRun: DR6 and/or DR7 reserved bits set. DR6=%#RX64 DR7=%#RX64 -> #VMEXIT\n", VmcbNstGst.u64DR6,
315 VmcbNstGst.u64DR6));
316 return HMSvmNstGstVmExit(pVCpu, pCtx, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
317 }
318
319 /** @todo gPAT MSR validation? */
320
321 /*
322 * Copy segments from nested-guest VMCB state to the guest-CPU state.
323 *
324 * We do this here as we need to use the CS attributes and it's easier this way
325 * then using the VMCB format selectors. It doesn't really matter where we copy
326 * the state, we restore the guest-CPU context state on the \#VMEXIT anyway.
327 */
328 HMSVM_SEG_REG_COPY_FROM_VMCB(pCtx, &VmcbNstGst, ES, es);
329 HMSVM_SEG_REG_COPY_FROM_VMCB(pCtx, &VmcbNstGst, CS, cs);
330 HMSVM_SEG_REG_COPY_FROM_VMCB(pCtx, &VmcbNstGst, SS, ss);
331 HMSVM_SEG_REG_COPY_FROM_VMCB(pCtx, &VmcbNstGst, DS, ds);
332
333 /** @todo Segment attribute overrides by VMRUN. */
334
335 /*
336 * CPL adjustments and overrides.
337 *
338 * SS.DPL is apparently the CPU's CPL, see comment in CPUMGetGuestCPL().
339 * We shall thus adjust both CS.DPL and SS.DPL here.
340 */
341 pCtx->cs.Attr.n.u2Dpl = pCtx->ss.Attr.n.u2Dpl = VmcbNstGst.u8CPL;
342 if (CPUMIsGuestInV86ModeEx(pCtx))
343 pCtx->cs.Attr.n.u2Dpl = pCtx->ss.Attr.n.u2Dpl = 3;
344 if (CPUMIsGuestInRealModeEx(pCtx))
345 pCtx->cs.Attr.n.u2Dpl = pCtx->ss.Attr.n.u2Dpl = 0;
346
347 /*
348 * Continue validating guest-state and controls.
349 */
350 /* EFER, CR0 and CR4. */
351 uint64_t uValidEfer;
352 rc = CPUMGetValidateEfer(pVM, VmcbNstGst.u64CR0, 0 /* uOldEfer */, VmcbNstGst.u64EFER, &uValidEfer);
353 if (RT_FAILURE(rc))
354 {
355 Log(("HMSvmVmRun: EFER invalid uOldEfer=%#RX64 uValidEfer=%#RX64 -> #VMEXIT\n", VmcbNstGst.u64EFER, uValidEfer));
356 return HMSvmNstGstVmExit(pVCpu, pCtx, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
357 }
358 bool const fSvm = RT_BOOL(uValidEfer & MSR_K6_EFER_SVME);
359 bool const fLongModeSupported = RT_BOOL(pVM->cpum.ro.GuestFeatures.fLongMode);
360 bool const fLongModeEnabled = RT_BOOL(uValidEfer & MSR_K6_EFER_LME);
361 bool const fPaging = RT_BOOL(VmcbNstGst.u64CR0 & X86_CR0_PG);
362 bool const fPae = RT_BOOL(VmcbNstGst.u64CR4 & X86_CR4_PAE);
363 bool const fProtMode = RT_BOOL(VmcbNstGst.u64CR0 & X86_CR0_PE);
364 bool const fLongModeWithPaging = fLongModeEnabled && fPaging;
365 bool const fLongModeConformCS = pCtx->cs.Attr.n.u1Long && pCtx->cs.Attr.n.u1DefBig;
366 /* Adjust EFER.LMA (this is normally done by the CPU when system software writes CR0). */
367 if (fLongModeWithPaging)
368 uValidEfer |= MSR_K6_EFER_LMA;
369 bool const fLongModeActiveOrEnabled = RT_BOOL(uValidEfer & (MSR_K6_EFER_LME | MSR_K6_EFER_LMA));
370 if ( !fSvm
371 || (!fLongModeSupported && fLongModeActiveOrEnabled)
372 || (fLongModeWithPaging && !fPae)
373 || (fLongModeWithPaging && !fProtMode)
374 || ( fLongModeEnabled
375 && fPaging
376 && fPae
377 && fLongModeConformCS))
378 {
379 Log(("HMSvmVmRun: EFER invalid. uValidEfer=%#RX64 -> #VMEXIT\n", uValidEfer));
380 return HMSvmNstGstVmExit(pVCpu, pCtx, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
381 }
382
383 /*
384 * Preserve the required force-flags.
385 *
386 * We only preserve the force-flags that would affect the execution of the
387 * nested-guest (or the guest).
388 *
389 * - VMCPU_FF_INHIBIT_INTERRUPTS need not be preserved as it's for a single
390 * instruction which is this VMRUN instruction itself.
391 *
392 * - VMCPU_FF_BLOCK_NMIS needs to be preserved as it blocks NMI until the
393 * execution of a subsequent IRET instruction in the guest.
394 *
395 * - The remaining FFs (e.g. timers) can stay in place so that we will be
396 * able to generate interrupts that should cause #VMEXITs for the
397 * nested-guest.
398 */
399 /** @todo anything missed more here? */
400 pCtx->hwvirt.fLocalForcedActions = pVCpu->fLocalForcedActions & VMCPU_FF_BLOCK_NMIS;
401
402 /*
403 * Interrupt shadow.
404 */
405 if (pVmcbCtrl->u64IntShadow & SVM_INTERRUPT_SHADOW_ACTIVE)
406 EMSetInhibitInterruptsPC(pVCpu, VmcbNstGst.u64RIP);
407
408 /*
409 * TLB flush control.
410 */
411 /** @todo @bugref{7243}: ASID based PGM TLB flushes. */
412 if ( pVmcbCtrl->TLBCtrl.n.u8TLBFlush == SVM_TLB_FLUSH_ENTIRE
413 || pVmcbCtrl->TLBCtrl.n.u8TLBFlush == SVM_TLB_FLUSH_SINGLE_CONTEXT
414 || pVmcbCtrl->TLBCtrl.n.u8TLBFlush == SVM_TLB_FLUSH_SINGLE_CONTEXT_RETAIN_GLOBALS)
415 PGMFlushTLB(pVCpu, VmcbNstGst.u64CR3, true /* fGlobal */);
416
417 /** @todo @bugref{7243}: SVM TSC offset, see tmCpuTickGetInternal. */
418
419 /*
420 * Copy the remaining guest state from the VMCB to the guest-CPU context.
421 */
422 pCtx->gdtr.cbGdt = VmcbNstGst.GDTR.u32Limit;
423 pCtx->gdtr.pGdt = VmcbNstGst.GDTR.u64Base;
424 pCtx->idtr.cbIdt = VmcbNstGst.IDTR.u32Limit;
425 pCtx->idtr.pIdt = VmcbNstGst.IDTR.u64Base;
426 pCtx->cr0 = VmcbNstGst.u64CR0; /** @todo What about informing PGM about CR0.WP? */
427 pCtx->cr4 = VmcbNstGst.u64CR4;
428 pCtx->cr3 = VmcbNstGst.u64CR3;
429 pCtx->cr2 = VmcbNstGst.u64CR2;
430 pCtx->dr[6] = VmcbNstGst.u64DR6;
431 pCtx->dr[7] = VmcbNstGst.u64DR7;
432 pCtx->rflags.u = VmcbNstGst.u64RFlags;
433 pCtx->rax = VmcbNstGst.u64RAX;
434 pCtx->rsp = VmcbNstGst.u64RSP;
435 pCtx->rip = VmcbNstGst.u64RIP;
436 pCtx->msrEFER = uValidEfer;
437
438 /* Mask DR6, DR7 bits mandatory set/clear bits. */
439 pCtx->dr[6] &= ~(X86_DR6_RAZ_MASK | X86_DR6_MBZ_MASK);
440 pCtx->dr[6] |= X86_DR6_RA1_MASK;
441 pCtx->dr[7] &= ~(X86_DR7_RAZ_MASK | X86_DR7_MBZ_MASK);
442 pCtx->dr[7] |= X86_DR7_RA1_MASK;
443
444 /*
445 * Check for pending virtual interrupts.
446 */
447 if (pVmcbCtrl->IntCtrl.n.u1VIrqPending)
448 VMCPU_FF_SET(pVCpu, VMCPU_FF_INTERRUPT_NESTED_GUEST);
449
450 /*
451 * Clear global interrupt flags to allow interrupts in the guest.
452 */
453 pCtx->hwvirt.svm.fGif = 1;
454
455 /*
456 * Event injection.
457 */
458 PCSVMEVENT pEventInject = &pVmcbCtrl->EventInject;
459 if (pEventInject->n.u1Valid)
460 {
461 uint8_t const uVector = pEventInject->n.u8Vector;
462 TRPMEVENT const enmType = hmSvmEventToTrpmEventType(pEventInject);
463 uint16_t const uErrorCode = pEventInject->n.u1ErrorCodeValid ? pEventInject->n.u32ErrorCode : 0;
464
465 /* Validate vectors for hardware exceptions, see AMD spec. 15.20 "Event Injection". */
466 if (enmType == TRPM_32BIT_HACK)
467 {
468 Log(("HMSvmVmRun: Invalid event type =%#x -> #VMEXIT\n", (uint8_t)pEventInject->n.u3Type));
469 return HMSvmNstGstVmExit(pVCpu, pCtx, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
470 }
471 if (pEventInject->n.u3Type == SVM_EVENT_EXCEPTION)
472 {
473 if ( uVector == X86_XCPT_NMI
474 || uVector > 31 /* X86_XCPT_MAX */)
475 {
476 Log(("HMSvmVmRun: Invalid vector for hardware exception. uVector=%#x -> #VMEXIT\n", uVector));
477 return HMSvmNstGstVmExit(pVCpu, pCtx, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
478 }
479 if ( uVector == X86_XCPT_BR
480 && CPUMIsGuestInLongModeEx(pCtx))
481 {
482 Log(("HMSvmVmRun: Cannot inject #BR when not in long mode -> #VMEXIT\n"));
483 return HMSvmNstGstVmExit(pVCpu, pCtx, SVM_EXIT_INVALID, 0 /* uExitInfo1 */, 0 /* uExitInfo2 */);
484 }
485 /** @todo any others? */
486 }
487
488 /*
489 * Update the exit interruption info field so that if an exception occurs
490 * while delivering the event causing a #VMEXIT, we only need to update
491 * the valid bit while the rest is already in place.
492 */
493 pVmcbCtrl->ExitIntInfo.u = pVmcbCtrl->EventInject.u;
494 pVmcbCtrl->ExitIntInfo.n.u1Valid = 0;
495
496 /** @todo NRIP: Software interrupts can only be pushed properly if we support
497 * NRIP for the nested-guest to calculate the instruction length
498 * below. */
499 VBOXSTRICTRC rcStrict = IEMInjectTrap(pVCpu, uVector, enmType, uErrorCode, pCtx->cr2, 0 /* cbInstr */);
500 if ( rcStrict == VINF_SVM_VMEXIT
501 || rcStrict == VERR_SVM_VMEXIT_FAILED)
502 return rcStrict;
503 }
504
505 return VINF_SUCCESS;
506 }
507
508 /* Shouldn't really happen as the caller should've validated the physical address already. */
509 Log(("HMSvmVmRun: Failed to read nested-guest VMCB control area at %#RGp -> #VMEXIT\n",
510 GCPhysVmcb));
511 return VERR_SVM_IPE_4;
512 }
513
514 /* Shouldn't really happen as the caller should've validated the physical address already. */
515 Log(("HMSvmVmRun: Failed to read nested-guest VMCB save-state area at %#RGp -> #VMEXIT\n",
516 GCPhysVmcb + RT_OFFSETOF(SVMVMCB, guest)));
517 return VERR_SVM_IPE_5;
518}
519
520
521/**
522 * SVM nested-guest \#VMEXIT handler.
523 *
524 * @returns Strict VBox status code.
525 * @retval VINF_SVM_VMEXIT when the \#VMEXIT is successful.
526 * @retval VERR_SVM_VMEXIT_FAILED when the \#VMEXIT failed restoring the guest's
527 * "host state" and a shutdown is required.
528 *
529 * @param pVCpu The cross context virtual CPU structure.
530 * @param pCtx The guest-CPU context.
531 * @param uExitCode The exit code.
532 * @param uExitInfo1 The exit info. 1 field.
533 * @param uExitInfo2 The exit info. 2 field.
534 */
535VMM_INT_DECL(VBOXSTRICTRC) HMSvmNstGstVmExit(PVMCPU pVCpu, PCPUMCTX pCtx, uint64_t uExitCode, uint64_t uExitInfo1,
536 uint64_t uExitInfo2)
537{
538 if ( CPUMIsGuestInNestedHwVirtMode(pCtx)
539 || uExitCode == SVM_EXIT_INVALID)
540 {
541 RT_NOREF(pVCpu);
542
543 /*
544 * Disable the global interrupt flag to prevent interrupts during the 'atomic' world switch.
545 */
546 pCtx->hwvirt.svm.fGif = 0;
547
548 /*
549 * Save the nested-guest state into the VMCB state-save area.
550 */
551 SVMVMCBSTATESAVE VmcbNstGst;
552 HMSVM_SEG_REG_COPY_TO_VMCB(pCtx, &VmcbNstGst, ES, es);
553 HMSVM_SEG_REG_COPY_TO_VMCB(pCtx, &VmcbNstGst, CS, cs);
554 HMSVM_SEG_REG_COPY_TO_VMCB(pCtx, &VmcbNstGst, SS, ss);
555 HMSVM_SEG_REG_COPY_TO_VMCB(pCtx, &VmcbNstGst, DS, ds);
556 VmcbNstGst.GDTR.u32Limit = pCtx->gdtr.cbGdt;
557 VmcbNstGst.GDTR.u64Base = pCtx->gdtr.pGdt;
558 VmcbNstGst.IDTR.u32Limit = pCtx->idtr.cbIdt;
559 VmcbNstGst.IDTR.u32Limit = pCtx->idtr.pIdt;
560 VmcbNstGst.u64EFER = pCtx->msrEFER;
561 VmcbNstGst.u64CR4 = pCtx->cr4;
562 VmcbNstGst.u64CR3 = pCtx->cr3;
563 VmcbNstGst.u64CR2 = pCtx->cr2;
564 VmcbNstGst.u64CR0 = pCtx->cr0;
565 /** @todo Nested paging. */
566 VmcbNstGst.u64RFlags = pCtx->rflags.u64;
567 VmcbNstGst.u64RIP = pCtx->rip;
568 VmcbNstGst.u64RSP = pCtx->rsp;
569 VmcbNstGst.u64RAX = pCtx->rax;
570 VmcbNstGst.u64DR7 = pCtx->dr[6];
571 VmcbNstGst.u64DR6 = pCtx->dr[7];
572 VmcbNstGst.u8CPL = pCtx->ss.Attr.n.u2Dpl; /* See comment in CPUMGetGuestCPL(). */
573
574 /* Save interrupt shadow of the nested-guest instruction if any. */
575 if ( VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS)
576 && EMGetInhibitInterruptsPC(pVCpu) == pCtx->rip)
577 pCtx->hwvirt.svm.VmcbCtrl.u64IntShadow |= SVM_INTERRUPT_SHADOW_ACTIVE;
578
579 /*
580 * Save additional state and intercept information.
581 */
582 if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INTERRUPT_NESTED_GUEST))
583 {
584 Assert(pCtx->hwvirt.svm.VmcbCtrl.IntCtrl.n.u1VIrqPending);
585 Assert(pCtx->hwvirt.svm.VmcbCtrl.IntCtrl.n.u8VIntrVector);
586 }
587 /** @todo Save V_TPR, V_IRQ. */
588 /** @todo NRIP. */
589
590 /* Save exit information. */
591 pCtx->hwvirt.svm.VmcbCtrl.u64ExitCode = uExitCode;
592 pCtx->hwvirt.svm.VmcbCtrl.u64ExitInfo1 = uExitInfo1;
593 pCtx->hwvirt.svm.VmcbCtrl.u64ExitInfo2 = uExitInfo2;
594
595 /*
596 * Clear event injection in the VMCB.
597 */
598 pCtx->hwvirt.svm.VmcbCtrl.EventInject.n.u1Valid = 0;
599
600 /*
601 * Write back the VMCB controls to the guest VMCB in guest physical memory.
602 */
603 int rc = PGMPhysSimpleWriteGCPhys(pVCpu->CTX_SUFF(pVM), pCtx->hwvirt.svm.GCPhysVmcb, &pCtx->hwvirt.svm.VmcbCtrl,
604 sizeof(pCtx->hwvirt.svm.VmcbCtrl));
605 if (RT_SUCCESS(rc))
606 {
607 /*
608 * Prepare for guest's "host mode" by clearing internal processor state bits.
609 *
610 * Some of these like TSC offset can then be used unconditionally in our TM code
611 * but the offset in the guest's VMCB will remain as it should as we've written
612 * back the VMCB controls above.
613 */
614 RT_ZERO(pCtx->hwvirt.svm.VmcbCtrl);
615#if 0
616 /* Clear TSC offset. */
617 pCtx->hwvirt.svm.VmcbCtrl.u64TSCOffset = 0;
618 pCtx->hwvirt.svm.VmcbCtrl.IntCtrl.n.u1VIrqValid = 0;
619 pCtx->hwvirt.svm.VmcbCtrl.IntCtrl.n.u1VIntrMasking = 0;
620#endif
621 /* Restore guest's force-flags. */
622 if (pCtx->hwvirt.fLocalForcedActions)
623 VMCPU_FF_SET(pVCpu, pCtx->hwvirt.fLocalForcedActions);
624
625 /* Clear nested-guest's interrupt pending. */
626 if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INTERRUPT_NESTED_GUEST))
627 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_NESTED_GUEST);
628
629 /** @todo Nested paging. */
630 /** @todo ASID. */
631
632 /*
633 * Reload the guest's "host state".
634 */
635 PSVMHOSTSTATE pHostState = &pCtx->hwvirt.svm.HostState;
636 pCtx->es = pHostState->es;
637 pCtx->cs = pHostState->cs;
638 pCtx->ss = pHostState->ss;
639 pCtx->ds = pHostState->ds;
640 pCtx->gdtr = pHostState->gdtr;
641 pCtx->idtr = pHostState->idtr;
642 pCtx->msrEFER = pHostState->uEferMsr;
643 pCtx->cr0 = pHostState->uCr0 | X86_CR0_PE;
644 pCtx->cr3 = pHostState->uCr3;
645 pCtx->cr4 = pHostState->uCr4;
646 pCtx->rflags = pHostState->rflags;
647 pCtx->rflags.Bits.u1VM = 0;
648 pCtx->rip = pHostState->uRip;
649 pCtx->rsp = pHostState->uRsp;
650 pCtx->rax = pHostState->uRax;
651 pCtx->dr[7] &= ~(X86_DR7_ENABLED_MASK | X86_DR7_RAZ_MASK | X86_DR7_MBZ_MASK);
652 pCtx->dr[7] |= X86_DR7_RA1_MASK;
653
654 /** @todo if RIP is not canonical or outside the CS segment limit, we need to
655 * raise \#GP(0) in the guest. */
656
657 /** @todo check the loaded host-state for consistency. Figure out what
658 * exactly this involves? */
659
660 rc = VINF_SVM_VMEXIT;
661 }
662 else
663 {
664 Log(("HMNstGstSvmVmExit: Writing VMCB at %#RGp failed\n", pCtx->hwvirt.svm.GCPhysVmcb));
665 Assert(!CPUMIsGuestInNestedHwVirtMode(pCtx));
666 rc = VERR_SVM_VMEXIT_FAILED;
667 }
668
669 return rc;
670 }
671
672 Log(("HMNstGstSvmVmExit: Not in SVM guest mode! uExitCode=%#RX64 uExitInfo1=%#RX64 uExitInfo2=%#RX64\n", uExitCode,
673 uExitInfo1, uExitInfo2));
674 RT_NOREF2(uExitInfo1, uExitInfo2);
675 return VERR_SVM_IPE_5;
676}
677
678
679/**
680 * Checks whether an interrupt is pending for the nested-guest.
681 *
682 * @returns VBox status code.
683 * @retval true if there's a pending interrupt, false otherwise.
684 *
685 * @param pCtx The guest-CPU context.
686 */
687VMM_INT_DECL(bool) HMSvmNstGstIsInterruptPending(PCCPUMCTX pCtx)
688{
689 PCSVMVMCBCTRL pVmcbCtrl = &pCtx->hwvirt.svm.VmcbCtrl;
690 if (!CPUMIsGuestInNestedHwVirtMode(pCtx))
691 return false;
692
693 X86RFLAGS RFlags;
694 if (pVmcbCtrl->IntCtrl.n.u1VIntrMasking)
695 RFlags.u = pCtx->rflags.u;
696 else
697 RFlags.u = pCtx->hwvirt.svm.HostState.rflags.u;
698
699 if (!RFlags.Bits.u1IF)
700 return false;
701
702 return RT_BOOL(pVmcbCtrl->IntCtrl.n.u1VIrqPending);
703}
704
705
706/**
707 * Gets the pending nested-guest interrupt.
708 *
709 * @returns VBox status code.
710 * @retval VINF_SUCCESS on success.
711 * @retval VERR_APIC_INTR_MASKED_BY_TPR when an APIC interrupt is pending but
712 * can't be delivered due to TPR priority.
713 * @retval VERR_NO_DATA if there is no interrupt to be delivered (either APIC
714 * has been software-disabled since it flagged something was pending,
715 * or other reasons).
716 *
717 * @param pCtx The guest-CPU context.
718 * @param pu8Interrupt Where to store the interrupt.
719 */
720VMM_INT_DECL(int) HMSvmNstGstGetInterrupt(PCCPUMCTX pCtx, uint8_t *pu8Interrupt)
721{
722 PCSVMVMCBCTRL pVmcbCtrl = &pCtx->hwvirt.svm.VmcbCtrl;
723 /** @todo remove later, paranoia for now. */
724#ifdef DEBUG_ramshankar
725 Assert(HMSvmNstGstIsInterruptPending(pCtx));
726#endif
727
728 *pu8Interrupt = pVmcbCtrl->IntCtrl.n.u8VIntrVector;
729 if ( pVmcbCtrl->IntCtrl.n.u1IgnoreTPR
730 || pVmcbCtrl->IntCtrl.n.u4VIntrPrio > pVmcbCtrl->IntCtrl.n.u8VTPR)
731 return VINF_SUCCESS;
732
733 return VERR_APIC_INTR_MASKED_BY_TPR;
734}
735
736
737/**
738 * Handles nested-guest SVM control intercepts and performs the \#VMEXIT if the
739 * intercept is active.
740 *
741 * @returns Strict VBox status code.
742 * @retval VINF_SVM_INTERCEPT_NOT_ACTIVE if the intercept is not active or
743 * we're not executing a nested-guest.
744 * @retval VINF_SVM_VMEXIT if the intercept is active and the \#VMEXIT occurred
745 * successfully.
746 * @retval VERR_SVM_VMEXIT_FAILED if the intercept is active and the \#VMEXIT
747 * failed and a shutdown needs to be initiated for the geust.
748 *
749 * @param pVCpu The cross context virtual CPU structure.
750 * @param pCtx The guest-CPU context.
751 * @param uExitCode The SVM exit code (see SVM_EXIT_XXX).
752 * @param uExitInfo1 The exit info. 1 field.
753 * @param uExitInfo2 The exit info. 2 field.
754 */
755VMM_INT_DECL(VBOXSTRICTRC) HMSvmNstGstHandleCtrlIntercept(PVMCPU pVCpu, PCPUMCTX pCtx, uint64_t uExitCode, uint64_t uExitInfo1,
756 uint64_t uExitInfo2)
757{
758#define HMSVM_VMEXIT_RET() do { return HMSvmNstGstVmExit(pVCpu, pCtx, uExitCode, uExitInfo1, uExitInfo2); } while (0)
759#define HMSVM_CTRL_INTERCEPT_VMEXIT_RET(a_Intercept) \
760 do { \
761 if (CPUMIsGuestSvmCtrlInterceptSet(pCtx, (a_Intercept))) \
762 return HMSvmNstGstVmExit(pVCpu, pCtx, uExitCode, uExitInfo1, uExitInfo2); \
763 break; \
764 } while (0)
765
766 if (!CPUMIsGuestInNestedHwVirtMode(pCtx))
767 return VINF_HM_INTERCEPT_NOT_ACTIVE;
768
769 switch (uExitCode)
770 {
771 case SVM_EXIT_EXCEPTION_0: case SVM_EXIT_EXCEPTION_1: case SVM_EXIT_EXCEPTION_2: case SVM_EXIT_EXCEPTION_3:
772 case SVM_EXIT_EXCEPTION_4: case SVM_EXIT_EXCEPTION_5: case SVM_EXIT_EXCEPTION_6: case SVM_EXIT_EXCEPTION_7:
773 case SVM_EXIT_EXCEPTION_8: case SVM_EXIT_EXCEPTION_9: case SVM_EXIT_EXCEPTION_10: case SVM_EXIT_EXCEPTION_11:
774 case SVM_EXIT_EXCEPTION_12: case SVM_EXIT_EXCEPTION_13: case SVM_EXIT_EXCEPTION_14: case SVM_EXIT_EXCEPTION_15:
775 case SVM_EXIT_EXCEPTION_16: case SVM_EXIT_EXCEPTION_17: case SVM_EXIT_EXCEPTION_18: case SVM_EXIT_EXCEPTION_19:
776 case SVM_EXIT_EXCEPTION_20: case SVM_EXIT_EXCEPTION_21: case SVM_EXIT_EXCEPTION_22: case SVM_EXIT_EXCEPTION_23:
777 case SVM_EXIT_EXCEPTION_24: case SVM_EXIT_EXCEPTION_25: case SVM_EXIT_EXCEPTION_26: case SVM_EXIT_EXCEPTION_27:
778 case SVM_EXIT_EXCEPTION_28: case SVM_EXIT_EXCEPTION_29: case SVM_EXIT_EXCEPTION_30: case SVM_EXIT_EXCEPTION_31:
779 if (CPUMIsGuestSvmXcptInterceptSet(pCtx, (X86XCPT)(uExitCode - SVM_EXIT_EXCEPTION_0)))
780 HMSVM_VMEXIT_RET();
781 break;
782
783 case SVM_EXIT_WRITE_CR0: case SVM_EXIT_WRITE_CR1: case SVM_EXIT_WRITE_CR2: case SVM_EXIT_WRITE_CR3:
784 case SVM_EXIT_WRITE_CR4: case SVM_EXIT_WRITE_CR5: case SVM_EXIT_WRITE_CR6: case SVM_EXIT_WRITE_CR7:
785 case SVM_EXIT_WRITE_CR8: case SVM_EXIT_WRITE_CR9: case SVM_EXIT_WRITE_CR10: case SVM_EXIT_WRITE_CR11:
786 case SVM_EXIT_WRITE_CR12: case SVM_EXIT_WRITE_CR13: case SVM_EXIT_WRITE_CR14: case SVM_EXIT_WRITE_CR15:
787 if (CPUMIsGuestSvmWriteCRxInterceptSet(pCtx, uExitCode - SVM_EXIT_WRITE_CR0))
788 HMSVM_VMEXIT_RET();
789 break;
790
791 case SVM_EXIT_READ_CR0: case SVM_EXIT_READ_CR1: case SVM_EXIT_READ_CR2: case SVM_EXIT_READ_CR3:
792 case SVM_EXIT_READ_CR4: case SVM_EXIT_READ_CR5: case SVM_EXIT_READ_CR6: case SVM_EXIT_READ_CR7:
793 case SVM_EXIT_READ_CR8: case SVM_EXIT_READ_CR9: case SVM_EXIT_READ_CR10: case SVM_EXIT_READ_CR11:
794 case SVM_EXIT_READ_CR12: case SVM_EXIT_READ_CR13: case SVM_EXIT_READ_CR14: case SVM_EXIT_READ_CR15:
795 if (CPUMIsGuestSvmReadCRxInterceptSet(pCtx, uExitCode - SVM_EXIT_READ_CR0))
796 HMSVM_VMEXIT_RET();
797 break;
798
799 case SVM_EXIT_READ_DR0: case SVM_EXIT_READ_DR1: case SVM_EXIT_READ_DR2: case SVM_EXIT_READ_DR3:
800 case SVM_EXIT_READ_DR4: case SVM_EXIT_READ_DR5: case SVM_EXIT_READ_DR6: case SVM_EXIT_READ_DR7:
801 case SVM_EXIT_READ_DR8: case SVM_EXIT_READ_DR9: case SVM_EXIT_READ_DR10: case SVM_EXIT_READ_DR11:
802 case SVM_EXIT_READ_DR12: case SVM_EXIT_READ_DR13: case SVM_EXIT_READ_DR14: case SVM_EXIT_READ_DR15:
803 if (CPUMIsGuestSvmReadDRxInterceptSet(pCtx, uExitCode - SVM_EXIT_READ_DR0))
804 HMSVM_VMEXIT_RET();
805 break;
806
807 case SVM_EXIT_WRITE_DR0: case SVM_EXIT_WRITE_DR1: case SVM_EXIT_WRITE_DR2: case SVM_EXIT_WRITE_DR3:
808 case SVM_EXIT_WRITE_DR4: case SVM_EXIT_WRITE_DR5: case SVM_EXIT_WRITE_DR6: case SVM_EXIT_WRITE_DR7:
809 case SVM_EXIT_WRITE_DR8: case SVM_EXIT_WRITE_DR9: case SVM_EXIT_WRITE_DR10: case SVM_EXIT_WRITE_DR11:
810 case SVM_EXIT_WRITE_DR12: case SVM_EXIT_WRITE_DR13: case SVM_EXIT_WRITE_DR14: case SVM_EXIT_WRITE_DR15:
811 if (CPUMIsGuestSvmWriteDRxInterceptSet(pCtx, uExitCode - SVM_EXIT_WRITE_DR0))
812 HMSVM_VMEXIT_RET();
813 break;
814
815 case SVM_EXIT_INTR: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_INTR);
816 case SVM_EXIT_NMI: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_NMI);
817 case SVM_EXIT_SMI: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_SMI);
818 case SVM_EXIT_INIT: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_INIT);
819 case SVM_EXIT_VINTR: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_VINTR);
820 case SVM_EXIT_CR0_SEL_WRITE: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_CR0_SEL_WRITES);
821 case SVM_EXIT_IDTR_READ: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_IDTR_READS);
822 case SVM_EXIT_GDTR_READ: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_GDTR_READS);
823 case SVM_EXIT_LDTR_READ: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_LDTR_READS);
824 case SVM_EXIT_TR_READ: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_TR_READS);
825 case SVM_EXIT_IDTR_WRITE: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_IDTR_WRITES);
826 case SVM_EXIT_GDTR_WRITE: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_GDTR_WRITES);
827 case SVM_EXIT_LDTR_WRITE: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_LDTR_WRITES);
828 case SVM_EXIT_TR_WRITE: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_TR_WRITES);
829 case SVM_EXIT_RDTSC: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_RDTSC);
830 case SVM_EXIT_RDPMC: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_RDPMC);
831 case SVM_EXIT_PUSHF: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_PUSHF);
832 case SVM_EXIT_POPF: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_POPF);
833 case SVM_EXIT_CPUID: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_CPUID);
834 case SVM_EXIT_RSM: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_RSM);
835 case SVM_EXIT_IRET: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_IRET);
836 case SVM_EXIT_SWINT: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_INTN);
837 case SVM_EXIT_INVD: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_INVD);
838 case SVM_EXIT_PAUSE: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_PAUSE);
839 case SVM_EXIT_HLT: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_HLT);
840 case SVM_EXIT_INVLPG: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_INVLPG);
841 case SVM_EXIT_INVLPGA: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_INVLPGA);
842 case SVM_EXIT_TASK_SWITCH: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_TASK_SWITCH);
843 case SVM_EXIT_FERR_FREEZE: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_FERR_FREEZE);
844 case SVM_EXIT_SHUTDOWN: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_SHUTDOWN);
845 case SVM_EXIT_VMRUN: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_VMRUN);
846 case SVM_EXIT_VMMCALL: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_VMMCALL);
847 case SVM_EXIT_VMLOAD: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_VMLOAD);
848 case SVM_EXIT_VMSAVE: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_VMSAVE);
849 case SVM_EXIT_STGI: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_STGI);
850 case SVM_EXIT_CLGI: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_CLGI);
851 case SVM_EXIT_SKINIT: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_SKINIT);
852 case SVM_EXIT_RDTSCP: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_RDTSCP);
853 case SVM_EXIT_ICEBP: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_ICEBP);
854 case SVM_EXIT_WBINVD: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_WBINVD);
855 case SVM_EXIT_MONITOR: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_MONITOR);
856 case SVM_EXIT_MWAIT: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_MWAIT);
857 case SVM_EXIT_MWAIT_ARMED: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_MWAIT_ARMED);
858 case SVM_EXIT_XSETBV: HMSVM_CTRL_INTERCEPT_VMEXIT_RET(SVM_CTRL_INTERCEPT_XSETBV);
859
860#if 0
861 case SVM_EXIT_IOIO:
862 {
863 if (CPUMIsGuestSvmCtrlInterceptSet(pCtx, SVM_CTRL_INTERCEPT_IOIO_PROT))
864 {
865 SVMIOIOEXIT IOExitInfo;
866 IOExitInfo.u = uExitInfo1;
867 const volatile void *pvIOPM = pCtx->hwvirt.svm.CTX_SUFF(pvIoBitmap);
868 uint16_t const offIoBitmap = pVIOPM + (IOExitInfo.n.u16Port / 8);
869 uint16_t const u16Port = IOExitInfo.n.u16Port;
870 uint8_t const cbIoSize = IOExitInfo.n.u1OP32 ? 4 : IOExitInfo.n.u1OP16;
871 }
872 break;
873 }
874#endif
875
876 case SVM_EXIT_MSR:
877 AssertMsgFailed(("Use HMSvmNstGstHandleMsrIntercept!\n"));
878 return VERR_SVM_IPE_1;
879
880 case SVM_EXIT_NPF:
881 case SVM_EXIT_AVIC_INCOMPLETE_IPI:
882 case SVM_EXIT_AVIC_NOACCEL:
883 AssertMsgFailed(("Todo Implement.\n"));
884 return VERR_SVM_IPE_1;
885
886 default:
887 AssertMsgFailed(("Unsupported.\n"));
888 return VERR_SVM_IPE_1;
889 }
890
891 return VINF_HM_INTERCEPT_NOT_ACTIVE;
892
893#undef HMSVM_VMEXIT_RET
894#undef HMSVM_CTRL_INTERCEPT_VMEXIT_RET
895}
896
897
898/**
899 * Handles nested-guest SVM IO intercepts and performs the \#VMEXIT
900 * if the intercept is active.
901 *
902 * @returns Strict VBox status code.
903 * @retval VINF_SVM_INTERCEPT_NOT_ACTIVE if the intercept is not active or
904 * we're not executing a nested-guest.
905 * @retval VINF_SVM_VMEXIT if the intercept is active and the \#VMEXIT occurred
906 * successfully.
907 * @retval VERR_SVM_VMEXIT_FAILED if the intercept is active and the \#VMEXIT
908 * failed and a shutdown needs to be initiated for the geust.
909 *
910 * @param pVCpu The cross context virtual CPU structure.
911 * @param pCtx The guest-CPU context.
912 * @param pIoExitInfo The SVM IOIO exit info. structure.
913 * @param uNextRip The RIP of the instruction following the IO
914 * instruction.
915 */
916VMM_INT_DECL(VBOXSTRICTRC) HMSvmNstGstHandleIOIntercept(PVMCPU pVCpu, PCPUMCTX pCtx, PCSVMIOIOEXITINFO pIoExitInfo,
917 uint64_t uNextRip)
918{
919 /*
920 * Check if any IO accesses are being intercepted.
921 */
922 if (CPUMIsGuestSvmCtrlInterceptSet(pCtx, SVM_CTRL_INTERCEPT_IOIO_PROT))
923 {
924 Assert(CPUMIsGuestInNestedHwVirtMode(pCtx));
925
926 /*
927 * The IOPM layout:
928 * Each bit represents one 8-bit port. That makes a total of 0..65535 bits or
929 * two 4K pages. However, since it's possible to do a 32-bit port IO at port
930 * 65534 (thus accessing 4 bytes), we need 3 extra bits beyond the two 4K page.
931 *
932 * For IO instructions that access more than a single byte, the permission bits
933 * for all bytes are checked; if any bit is set to 1, the IO access is intercepted.
934 */
935 uint8_t *pbIopm = (uint8_t *)pCtx->hwvirt.svm.CTX_SUFF(pvIoBitmap);
936
937 uint16_t const u16Port = pIoExitInfo->n.u16Port;
938 uint16_t const offIoBitmap = u16Port >> 3;
939 uint16_t const fSizeMask = pIoExitInfo->n.u1OP32 ? 0xf : pIoExitInfo->n.u1OP16 ? 3 : 1;
940 uint8_t const cShift = u16Port - (offIoBitmap << 3);
941 uint16_t const fIopmMask = (1 << cShift) | (fSizeMask << cShift);
942
943 pbIopm += offIoBitmap;
944 uint16_t const fIopmBits = *(uint16_t *)pbIopm;
945 if (fIopmBits & fIopmMask)
946 return HMSvmNstGstVmExit(pVCpu, pCtx, SVM_EXIT_IOIO, pIoExitInfo->u, uNextRip);
947 }
948 return VINF_HM_INTERCEPT_NOT_ACTIVE;
949}
950
951
952/**
953 * Handles nested-guest SVM MSR read/write intercepts and performs the \#VMEXIT
954 * if the intercept is active.
955 *
956 * @returns Strict VBox status code.
957 * @retval VINF_SVM_INTERCEPT_NOT_ACTIVE if the intercept is not active or
958 * we're not executing a nested-guest.
959 * @retval VINF_SVM_VMEXIT if the intercept is active and the \#VMEXIT occurred
960 * successfully.
961 * @retval VERR_SVM_VMEXIT_FAILED if the intercept is active and the \#VMEXIT
962 * failed and a shutdown needs to be initiated for the geust.
963 *
964 * @param pVCpu The cross context virtual CPU structure.
965 * @param pCtx The guest-CPU context.
966 * @param idMsr The MSR being accessed in the nested-guest.
967 * @param fWrite Whether this is an MSR write access, @c false implies an
968 * MSR read.
969 */
970VMM_INT_DECL(VBOXSTRICTRC) HMSvmNstGstHandleMsrIntercept(PVMCPU pVCpu, PCPUMCTX pCtx, uint32_t idMsr, bool fWrite)
971{
972 /*
973 * Check if any MSRs are being intercepted.
974 */
975 if (CPUMIsGuestSvmCtrlInterceptSet(pCtx, SVM_CTRL_INTERCEPT_MSR_PROT))
976 {
977 Assert(CPUMIsGuestInNestedHwVirtMode(pCtx));
978 uint64_t const uExitInfo1 = fWrite ? SVM_EXIT1_MSR_WRITE : SVM_EXIT1_MSR_READ;
979
980 /*
981 * Get the byte and bit offset of the permission bits corresponding to the MSR.
982 */
983 uint16_t offMsrpm;
984 uint32_t uMsrpmBit;
985 int rc = hmSvmGetMsrpmOffsetAndBit(idMsr, &offMsrpm, &uMsrpmBit);
986 if (RT_SUCCESS(rc))
987 {
988 Assert(uMsrpmBit < 0x3fff);
989 Assert(offMsrpm < SVM_MSRPM_PAGES << X86_PAGE_4K_SHIFT);
990 if (fWrite)
991 ++uMsrpmBit;
992
993 /*
994 * Check if the bit is set, if so, trigger a #VMEXIT.
995 */
996 uint8_t *pbMsrpm = (uint8_t *)pCtx->hwvirt.svm.CTX_SUFF(pvMsrBitmap);
997 pbMsrpm += offMsrpm;
998 if (ASMBitTest(pbMsrpm, uMsrpmBit))
999 return HMSvmNstGstVmExit(pVCpu, pCtx, SVM_EXIT_MSR, uExitInfo1, 0 /* uExitInfo2 */);
1000 }
1001 else
1002 {
1003 /*
1004 * This shouldn't happen, but if it does, cause a #VMEXIT and let the "host" (guest hypervisor) deal with it.
1005 */
1006 Log(("HMSvmNstGstHandleIntercept: Invalid/out-of-range MSR %#RX32 fWrite=%RTbool\n", idMsr, fWrite));
1007 return HMSvmNstGstVmExit(pVCpu, pCtx, SVM_EXIT_MSR, uExitInfo1, 0 /* uExitInfo2 */);
1008 }
1009 }
1010 return VINF_HM_INTERCEPT_NOT_ACTIVE;
1011}
1012
1013
1014/**
1015 * Gets the MSR permission bitmap byte and bit offset for the specified MSR.
1016 *
1017 * @returns VBox status code.
1018 * @param idMsr The MSR being requested.
1019 * @param pbOffMsrpm Where to store the byte offset in the MSR permission
1020 * bitmap for @a idMsr.
1021 * @param puMsrpmBit Where to store the bit offset starting at the byte
1022 * returned in @a pbOffMsrpm.
1023 */
1024VMM_INT_DECL(int) hmSvmGetMsrpmOffsetAndBit(uint32_t idMsr, uint16_t *pbOffMsrpm, uint32_t *puMsrpmBit)
1025{
1026 Assert(pbOffMsrpm);
1027 Assert(puMsrpmBit);
1028
1029 /*
1030 * MSRPM Layout:
1031 * Byte offset MSR range
1032 * 0x000 - 0x7ff 0x00000000 - 0x00001fff
1033 * 0x800 - 0xfff 0xc0000000 - 0xc0001fff
1034 * 0x1000 - 0x17ff 0xc0010000 - 0xc0011fff
1035 * 0x1800 - 0x1fff Reserved
1036 *
1037 * Each MSR is represented by 2 permission bits (read and write).
1038 */
1039 if (idMsr <= 0x00001fff)
1040 {
1041 /* Pentium-compatible MSRs. */
1042 *pbOffMsrpm = 0;
1043 *puMsrpmBit = idMsr << 1;
1044 return VINF_SUCCESS;
1045 }
1046
1047 if ( idMsr >= 0xc0000000
1048 && idMsr <= 0xc0001fff)
1049 {
1050 /* AMD Sixth Generation x86 Processor MSRs. */
1051 *pbOffMsrpm = 0x800;
1052 *puMsrpmBit = (idMsr - 0xc0000000) << 1;
1053 return VINF_SUCCESS;
1054 }
1055
1056 if ( idMsr >= 0xc0010000
1057 && idMsr <= 0xc0011fff)
1058 {
1059 /* AMD Seventh and Eighth Generation Processor MSRs. */
1060 *pbOffMsrpm += 0x1000;
1061 *puMsrpmBit = (idMsr - 0xc0001000) << 1;
1062 return VINF_SUCCESS;
1063 }
1064
1065 *pbOffMsrpm = 0;
1066 *puMsrpmBit = 0;
1067 return VERR_OUT_OF_RANGE;
1068}
1069#endif /* !IN_RC */
1070
1071
1072/**
1073 * Converts an SVM event type to a TRPM event type.
1074 *
1075 * @returns The TRPM event type.
1076 * @retval TRPM_32BIT_HACK if the specified type of event isn't among the set
1077 * of recognized trap types.
1078 *
1079 * @param pEvent Pointer to the SVM event.
1080 */
1081VMM_INT_DECL(TRPMEVENT) hmSvmEventToTrpmEventType(PCSVMEVENT pEvent)
1082{
1083 uint8_t const uType = pEvent->n.u3Type;
1084 switch (uType)
1085 {
1086 case SVM_EVENT_EXTERNAL_IRQ: return TRPM_HARDWARE_INT;
1087 case SVM_EVENT_SOFTWARE_INT: return TRPM_SOFTWARE_INT;
1088 case SVM_EVENT_EXCEPTION:
1089 case SVM_EVENT_NMI: return TRPM_TRAP;
1090 default:
1091 break;
1092 }
1093 AssertMsgFailed(("HMSvmEventToTrpmEvent: Invalid pending-event type %#x\n", uType));
1094 return TRPM_32BIT_HACK;
1095}
1096
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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