VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR0/HMSVMR0.cpp@ 70721

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

VMM/HMSVMR0: Nested Hw.virt: Exclude VINTR intercept of the outer guest while executing the nested-guest. It will remain in the outer guest's VMCB and
thus should be used when switching to the outer guest.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 306.4 KB
Line 
1/* $Id: HMSVMR0.cpp 70721 2018-01-24 09:00:50Z vboxsync $ */
2/** @file
3 * HM SVM (AMD-V) - Host Context Ring-0.
4 */
5
6/*
7 * Copyright (C) 2013-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* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_HM
23#define VMCPU_INCL_CPUM_GST_CTX
24#include <iprt/asm-amd64-x86.h>
25#include <iprt/thread.h>
26
27#include <VBox/vmm/pdmapi.h>
28#include <VBox/vmm/dbgf.h>
29#include <VBox/vmm/iem.h>
30#include <VBox/vmm/iom.h>
31#include <VBox/vmm/tm.h>
32#include <VBox/vmm/gim.h>
33#include <VBox/vmm/apic.h>
34#include "HMInternal.h"
35#include <VBox/vmm/vm.h>
36#include "HMSVMR0.h"
37#include "dtrace/VBoxVMM.h"
38
39#define HMSVM_USE_IEM_EVENT_REFLECTION
40#ifdef DEBUG_ramshankar
41# define HMSVM_SYNC_FULL_GUEST_STATE
42# define HMSVM_SYNC_FULL_NESTED_GUEST_STATE
43# define HMSVM_ALWAYS_TRAP_ALL_XCPTS
44# define HMSVM_ALWAYS_TRAP_PF
45# define HMSVM_ALWAYS_TRAP_TASK_SWITCH
46#endif
47
48
49/*********************************************************************************************************************************
50* Defined Constants And Macros *
51*********************************************************************************************************************************/
52#ifdef VBOX_WITH_STATISTICS
53# define HMSVM_EXITCODE_STAM_COUNTER_INC(u64ExitCode) do { \
54 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitAll); \
55 if ((u64ExitCode) == SVM_EXIT_NPF) \
56 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitReasonNpf); \
57 else \
58 STAM_COUNTER_INC(&pVCpu->hm.s.paStatExitReasonR0[(u64ExitCode) & MASK_EXITREASON_STAT]); \
59 } while (0)
60#else
61# define HMSVM_EXITCODE_STAM_COUNTER_INC(u64ExitCode) do { } while (0)
62#endif
63
64/** If we decide to use a function table approach this can be useful to
65 * switch to a "static DECLCALLBACK(int)". */
66#define HMSVM_EXIT_DECL static int
67
68/** Macro for checking and returning from the using function for
69 * \#VMEXIT intercepts that maybe caused during delivering of another
70 * event in the guest. */
71#ifdef VBOX_WITH_NESTED_HWVIRT
72# define HMSVM_CHECK_EXIT_DUE_TO_EVENT_DELIVERY() \
73 do \
74 { \
75 int rc = hmR0SvmCheckExitDueToEventDelivery(pVCpu, pCtx, pSvmTransient); \
76 if (RT_LIKELY(rc == VINF_SUCCESS)) { /* continue #VMEXIT handling */ } \
77 else if ( rc == VINF_HM_DOUBLE_FAULT) { return VINF_SUCCESS; } \
78 else if ( rc == VINF_EM_RESET \
79 && HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_SHUTDOWN)) \
80 return VBOXSTRICTRC_TODO(IEMExecSvmVmexit(pVCpu, SVM_EXIT_SHUTDOWN, 0, 0)); \
81 else \
82 return rc; \
83 } while (0)
84#else
85# define HMSVM_CHECK_EXIT_DUE_TO_EVENT_DELIVERY() \
86 do \
87 { \
88 int rc = hmR0SvmCheckExitDueToEventDelivery(pVCpu, pCtx, pSvmTransient); \
89 if (RT_LIKELY(rc == VINF_SUCCESS)) { /* continue #VMEXIT handling */ } \
90 else if ( rc == VINF_HM_DOUBLE_FAULT) { return VINF_SUCCESS; } \
91 else \
92 return rc; \
93 } while (0)
94#endif
95
96/**
97 * Updates interrupt shadow for the current RIP.
98 */
99#define HMSVM_UPDATE_INTR_SHADOW(pVCpu, pCtx) \
100 do { \
101 /* Update interrupt shadow. */ \
102 if ( VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS) \
103 && pCtx->rip != EMGetInhibitInterruptsPC(pVCpu)) \
104 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS); \
105 } while (0)
106
107/** Macro for upgrading a @a a_rc to VINF_EM_DBG_STEPPED after emulating an
108 * instruction that exited. */
109#define HMSVM_CHECK_SINGLE_STEP(a_pVCpu, a_rc) \
110 do { \
111 if ((a_pVCpu)->hm.s.fSingleInstruction && (a_rc) == VINF_SUCCESS) \
112 (a_rc) = VINF_EM_DBG_STEPPED; \
113 } while (0)
114
115/** Assert that preemption is disabled or covered by thread-context hooks. */
116#define HMSVM_ASSERT_PREEMPT_SAFE() Assert( VMMR0ThreadCtxHookIsEnabled(pVCpu) \
117 || !RTThreadPreemptIsEnabled(NIL_RTTHREAD));
118
119/** Assert that we haven't migrated CPUs when thread-context hooks are not
120 * used. */
121#define HMSVM_ASSERT_CPU_SAFE() AssertMsg( VMMR0ThreadCtxHookIsEnabled(pVCpu) \
122 || pVCpu->hm.s.idEnteredCpu == RTMpCpuId(), \
123 ("Illegal migration! Entered on CPU %u Current %u\n", \
124 pVCpu->hm.s.idEnteredCpu, RTMpCpuId()));
125
126/** Assert that we're not executing a nested-guest. */
127#ifdef VBOX_WITH_NESTED_HWVIRT
128# define HMSVM_ASSERT_NOT_IN_NESTED_GUEST(a_pCtx) Assert(!CPUMIsGuestInSvmNestedHwVirtMode((a_pCtx)))
129#else
130# define HMSVM_ASSERT_NOT_IN_NESTED_GUEST(a_pCtx) do { NOREF((a_pCtx)); } while (0)
131#endif
132
133/** Assert that we're executing a nested-guest. */
134#ifdef VBOX_WITH_NESTED_HWVIRT
135# define HMSVM_ASSERT_IN_NESTED_GUEST(a_pCtx) Assert(CPUMIsGuestInSvmNestedHwVirtMode((a_pCtx)))
136#else
137# define HMSVM_ASSERT_IN_NESTED_GUEST(a_pCtx) do { NOREF((a_pCtx)); } while (0)
138#endif
139
140/** Validate segment descriptor granularity bit. */
141#ifdef VBOX_STRICT
142# define HMSVM_ASSERT_SEG_GRANULARITY(reg) \
143 AssertMsg( !pMixedCtx->reg.Attr.n.u1Present \
144 || ( pMixedCtx->reg.Attr.n.u1Granularity \
145 ? (pMixedCtx->reg.u32Limit & 0xfff) == 0xfff \
146 : pMixedCtx->reg.u32Limit <= UINT32_C(0xfffff)), \
147 ("Invalid Segment Attributes Limit=%#RX32 Attr=%#RX32 Base=%#RX64\n", pMixedCtx->reg.u32Limit, \
148 pMixedCtx->reg.Attr.u, pMixedCtx->reg.u64Base))
149#else
150# define HMSVM_ASSERT_SEG_GRANULARITY(reg) do { } while (0)
151#endif
152
153/**
154 * Exception bitmap mask for all contributory exceptions.
155 *
156 * Page fault is deliberately excluded here as it's conditional as to whether
157 * it's contributory or benign. Page faults are handled separately.
158 */
159#define HMSVM_CONTRIBUTORY_XCPT_MASK ( RT_BIT(X86_XCPT_GP) | RT_BIT(X86_XCPT_NP) | RT_BIT(X86_XCPT_SS) | RT_BIT(X86_XCPT_TS) \
160 | RT_BIT(X86_XCPT_DE))
161
162/**
163 * Mandatory/unconditional guest control intercepts.
164 *
165 * SMIs can and do happen in normal operation. We need not intercept them
166 * while executing the guest or nested-guest.
167 */
168#define HMSVM_MANDATORY_GUEST_CTRL_INTERCEPTS ( SVM_CTRL_INTERCEPT_INTR \
169 | SVM_CTRL_INTERCEPT_NMI \
170 | SVM_CTRL_INTERCEPT_INIT \
171 | SVM_CTRL_INTERCEPT_RDPMC \
172 | SVM_CTRL_INTERCEPT_CPUID \
173 | SVM_CTRL_INTERCEPT_RSM \
174 | SVM_CTRL_INTERCEPT_HLT \
175 | SVM_CTRL_INTERCEPT_IOIO_PROT \
176 | SVM_CTRL_INTERCEPT_MSR_PROT \
177 | SVM_CTRL_INTERCEPT_INVLPGA \
178 | SVM_CTRL_INTERCEPT_SHUTDOWN \
179 | SVM_CTRL_INTERCEPT_FERR_FREEZE \
180 | SVM_CTRL_INTERCEPT_VMRUN \
181 | SVM_CTRL_INTERCEPT_VMMCALL \
182 | SVM_CTRL_INTERCEPT_SKINIT \
183 | SVM_CTRL_INTERCEPT_WBINVD \
184 | SVM_CTRL_INTERCEPT_MONITOR \
185 | SVM_CTRL_INTERCEPT_MWAIT \
186 | SVM_CTRL_INTERCEPT_XSETBV)
187
188/** @name VMCB Clean Bits.
189 *
190 * These flags are used for VMCB-state caching. A set VMCB Clean bit indicates
191 * AMD-V doesn't need to reload the corresponding value(s) from the VMCB in
192 * memory.
193 *
194 * @{ */
195/** All intercepts vectors, TSC offset, PAUSE filter counter. */
196#define HMSVM_VMCB_CLEAN_INTERCEPTS RT_BIT(0)
197/** I/O permission bitmap, MSR permission bitmap. */
198#define HMSVM_VMCB_CLEAN_IOPM_MSRPM RT_BIT(1)
199/** ASID. */
200#define HMSVM_VMCB_CLEAN_ASID RT_BIT(2)
201/** TRP: V_TPR, V_IRQ, V_INTR_PRIO, V_IGN_TPR, V_INTR_MASKING,
202V_INTR_VECTOR. */
203#define HMSVM_VMCB_CLEAN_TPR RT_BIT(3)
204/** Nested Paging: Nested CR3 (nCR3), PAT. */
205#define HMSVM_VMCB_CLEAN_NP RT_BIT(4)
206/** Control registers (CR0, CR3, CR4, EFER). */
207#define HMSVM_VMCB_CLEAN_CRX_EFER RT_BIT(5)
208/** Debug registers (DR6, DR7). */
209#define HMSVM_VMCB_CLEAN_DRX RT_BIT(6)
210/** GDT, IDT limit and base. */
211#define HMSVM_VMCB_CLEAN_DT RT_BIT(7)
212/** Segment register: CS, SS, DS, ES limit and base. */
213#define HMSVM_VMCB_CLEAN_SEG RT_BIT(8)
214/** CR2.*/
215#define HMSVM_VMCB_CLEAN_CR2 RT_BIT(9)
216/** Last-branch record (DbgCtlMsr, br_from, br_to, lastint_from, lastint_to) */
217#define HMSVM_VMCB_CLEAN_LBR RT_BIT(10)
218/** AVIC (AVIC APIC_BAR; AVIC APIC_BACKING_PAGE, AVIC
219PHYSICAL_TABLE and AVIC LOGICAL_TABLE Pointers). */
220#define HMSVM_VMCB_CLEAN_AVIC RT_BIT(11)
221/** Mask of all valid VMCB Clean bits. */
222#define HMSVM_VMCB_CLEAN_ALL ( HMSVM_VMCB_CLEAN_INTERCEPTS \
223 | HMSVM_VMCB_CLEAN_IOPM_MSRPM \
224 | HMSVM_VMCB_CLEAN_ASID \
225 | HMSVM_VMCB_CLEAN_TPR \
226 | HMSVM_VMCB_CLEAN_NP \
227 | HMSVM_VMCB_CLEAN_CRX_EFER \
228 | HMSVM_VMCB_CLEAN_DRX \
229 | HMSVM_VMCB_CLEAN_DT \
230 | HMSVM_VMCB_CLEAN_SEG \
231 | HMSVM_VMCB_CLEAN_CR2 \
232 | HMSVM_VMCB_CLEAN_LBR \
233 | HMSVM_VMCB_CLEAN_AVIC)
234/** @} */
235
236/** @name SVM transient.
237 *
238 * A state structure for holding miscellaneous information across AMD-V
239 * VMRUN/\#VMEXIT operation, restored after the transition.
240 *
241 * @{ */
242typedef struct SVMTRANSIENT
243{
244 /** The host's rflags/eflags. */
245 RTCCUINTREG fEFlags;
246#if HC_ARCH_BITS == 32
247 uint32_t u32Alignment0;
248#endif
249
250 /** The \#VMEXIT exit code (the EXITCODE field in the VMCB). */
251 uint64_t u64ExitCode;
252 /** The guest's TPR value used for TPR shadowing. */
253 uint8_t u8GuestTpr;
254 /** Alignment. */
255 uint8_t abAlignment0[7];
256
257 /** Whether the guest FPU state was active at the time of \#VMEXIT. */
258 bool fWasGuestFPUStateActive;
259 /** Whether the guest debug state was active at the time of \#VMEXIT. */
260 bool fWasGuestDebugStateActive;
261 /** Whether the hyper debug state was active at the time of \#VMEXIT. */
262 bool fWasHyperDebugStateActive;
263 /** Whether the TSC offset mode needs to be updated. */
264 bool fUpdateTscOffsetting;
265 /** Whether the TSC_AUX MSR needs restoring on \#VMEXIT. */
266 bool fRestoreTscAuxMsr;
267 /** Whether the \#VMEXIT was caused by a page-fault during delivery of a
268 * contributary exception or a page-fault. */
269 bool fVectoringDoublePF;
270 /** Whether the \#VMEXIT was caused by a page-fault during delivery of an
271 * external interrupt or NMI. */
272 bool fVectoringPF;
273} SVMTRANSIENT, *PSVMTRANSIENT;
274AssertCompileMemberAlignment(SVMTRANSIENT, u64ExitCode, sizeof(uint64_t));
275AssertCompileMemberAlignment(SVMTRANSIENT, fWasGuestFPUStateActive, sizeof(uint64_t));
276/** @} */
277
278/**
279 * MSRPM (MSR permission bitmap) read permissions (for guest RDMSR).
280 */
281typedef enum SVMMSREXITREAD
282{
283 /** Reading this MSR causes a \#VMEXIT. */
284 SVMMSREXIT_INTERCEPT_READ = 0xb,
285 /** Reading this MSR does not cause a \#VMEXIT. */
286 SVMMSREXIT_PASSTHRU_READ
287} SVMMSREXITREAD;
288
289/**
290 * MSRPM (MSR permission bitmap) write permissions (for guest WRMSR).
291 */
292typedef enum SVMMSREXITWRITE
293{
294 /** Writing to this MSR causes a \#VMEXIT. */
295 SVMMSREXIT_INTERCEPT_WRITE = 0xd,
296 /** Writing to this MSR does not cause a \#VMEXIT. */
297 SVMMSREXIT_PASSTHRU_WRITE
298} SVMMSREXITWRITE;
299
300/**
301 * SVM \#VMEXIT handler.
302 *
303 * @returns VBox status code.
304 * @param pVCpu The cross context virtual CPU structure.
305 * @param pMixedCtx Pointer to the guest-CPU context.
306 * @param pSvmTransient Pointer to the SVM-transient structure.
307 */
308typedef int FNSVMEXITHANDLER(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient);
309
310
311/*********************************************************************************************************************************
312* Internal Functions *
313*********************************************************************************************************************************/
314static void hmR0SvmSetMsrPermission(PSVMVMCB pVmcb, uint8_t *pbMsrBitmap, unsigned uMsr, SVMMSREXITREAD enmRead,
315 SVMMSREXITWRITE enmWrite);
316static void hmR0SvmPendingEventToTrpmTrap(PVMCPU pVCpu);
317static void hmR0SvmLeave(PVMCPU pVCpu);
318
319/** @name \#VMEXIT handlers.
320 * @{
321 */
322static FNSVMEXITHANDLER hmR0SvmExitIntr;
323static FNSVMEXITHANDLER hmR0SvmExitWbinvd;
324static FNSVMEXITHANDLER hmR0SvmExitInvd;
325static FNSVMEXITHANDLER hmR0SvmExitCpuid;
326static FNSVMEXITHANDLER hmR0SvmExitRdtsc;
327static FNSVMEXITHANDLER hmR0SvmExitRdtscp;
328static FNSVMEXITHANDLER hmR0SvmExitRdpmc;
329static FNSVMEXITHANDLER hmR0SvmExitInvlpg;
330static FNSVMEXITHANDLER hmR0SvmExitHlt;
331static FNSVMEXITHANDLER hmR0SvmExitMonitor;
332static FNSVMEXITHANDLER hmR0SvmExitMwait;
333static FNSVMEXITHANDLER hmR0SvmExitShutdown;
334static FNSVMEXITHANDLER hmR0SvmExitUnexpected;
335static FNSVMEXITHANDLER hmR0SvmExitReadCRx;
336static FNSVMEXITHANDLER hmR0SvmExitWriteCRx;
337static FNSVMEXITHANDLER hmR0SvmExitMsr;
338static FNSVMEXITHANDLER hmR0SvmExitReadDRx;
339static FNSVMEXITHANDLER hmR0SvmExitWriteDRx;
340static FNSVMEXITHANDLER hmR0SvmExitXsetbv;
341static FNSVMEXITHANDLER hmR0SvmExitIOInstr;
342static FNSVMEXITHANDLER hmR0SvmExitNestedPF;
343static FNSVMEXITHANDLER hmR0SvmExitVIntr;
344static FNSVMEXITHANDLER hmR0SvmExitTaskSwitch;
345static FNSVMEXITHANDLER hmR0SvmExitVmmCall;
346static FNSVMEXITHANDLER hmR0SvmExitPause;
347static FNSVMEXITHANDLER hmR0SvmExitIret;
348static FNSVMEXITHANDLER hmR0SvmExitXcptPF;
349static FNSVMEXITHANDLER hmR0SvmExitXcptNM;
350static FNSVMEXITHANDLER hmR0SvmExitXcptUD;
351static FNSVMEXITHANDLER hmR0SvmExitXcptMF;
352static FNSVMEXITHANDLER hmR0SvmExitXcptDB;
353static FNSVMEXITHANDLER hmR0SvmExitXcptAC;
354static FNSVMEXITHANDLER hmR0SvmExitXcptBP;
355#if defined(HMSVM_ALWAYS_TRAP_ALL_XCPTS) || defined(VBOX_WITH_NESTED_HWVIRT)
356static FNSVMEXITHANDLER hmR0SvmExitXcptGeneric;
357#endif
358#ifdef VBOX_WITH_NESTED_HWVIRT
359static FNSVMEXITHANDLER hmR0SvmExitXcptPFNested;
360static FNSVMEXITHANDLER hmR0SvmExitClgi;
361static FNSVMEXITHANDLER hmR0SvmExitStgi;
362static FNSVMEXITHANDLER hmR0SvmExitVmload;
363static FNSVMEXITHANDLER hmR0SvmExitVmsave;
364static FNSVMEXITHANDLER hmR0SvmExitInvlpga;
365static FNSVMEXITHANDLER hmR0SvmExitVmrun;
366static FNSVMEXITHANDLER hmR0SvmNestedExitXcptDB;
367static FNSVMEXITHANDLER hmR0SvmNestedExitXcptBP;
368#endif
369/** @} */
370
371static int hmR0SvmHandleExit(PVMCPU pVCpu, PCPUMCTX pMixedCtx, PSVMTRANSIENT pSvmTransient);
372#ifdef VBOX_WITH_NESTED_HWVIRT
373static int hmR0SvmHandleExitNested(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient);
374#endif
375
376
377/*********************************************************************************************************************************
378* Global Variables *
379*********************************************************************************************************************************/
380/** Ring-0 memory object for the IO bitmap. */
381RTR0MEMOBJ g_hMemObjIOBitmap = NIL_RTR0MEMOBJ;
382/** Physical address of the IO bitmap. */
383RTHCPHYS g_HCPhysIOBitmap = 0;
384/** Pointer to the IO bitmap. */
385R0PTRTYPE(void *) g_pvIOBitmap = NULL;
386
387#ifdef VBOX_WITH_NESTED_HWVIRT
388/** Ring-0 memory object for the nested-guest MSRPM bitmap. */
389RTR0MEMOBJ g_hMemObjNstGstMsrBitmap = NIL_RTR0MEMOBJ;
390/** Physical address of the nested-guest MSRPM bitmap. */
391RTHCPHYS g_HCPhysNstGstMsrBitmap = 0;
392/** Pointer to the nested-guest MSRPM bitmap. */
393R0PTRTYPE(void *) g_pvNstGstMsrBitmap = NULL;
394#endif
395
396
397#ifdef VBOX_STRICT
398# define HMSVM_LOG_CS RT_BIT_32(0)
399# define HMSVM_LOG_SS RT_BIT_32(1)
400# define HMSVM_LOG_FS RT_BIT_32(2)
401# define HMSVM_LOG_GS RT_BIT_32(3)
402# define HMSVM_LOG_LBR RT_BIT_32(4)
403# define HMSVM_LOG_ALL ( HMSVM_LOG_CS \
404 | HMSVM_LOG_SS \
405 | HMSVM_LOG_FS \
406 | HMSVM_LOG_GS \
407 | HMSVM_LOG_LBR)
408
409/**
410 * Dumps CPU state and additional info. to the logger for diagnostics.
411 *
412 * @param pVCpu The cross context virtual CPU structure.
413 * @param pVmcb Pointer to the VM control block.
414 * @param pCtx Pointer to the guest-CPU context.
415 * @param pszPrefix Log prefix.
416 * @param fFlags Log flags, see HMSVM_LOG_XXX.
417 * @param uVerbose The verbosity level, currently unused.
418 */
419static void hmR0SvmLogState(PVMCPU pVCpu, PCSVMVMCB pVmcb, PCPUMCTX pCtx, const char *pszPrefix, uint32_t fFlags,
420 uint8_t uVerbose)
421{
422 RT_NOREF2(pVCpu, uVerbose);
423
424 Log4(("%s: cs:rip=%04x:%RX64 efl=%#RX32 cr0=%#RX32 cr3=%#RX32 cr4=%#RX32\n", pszPrefix, pCtx->cs.Sel, pCtx->rip,
425 pCtx->eflags.u, pCtx->cr0, pCtx->cr3, pCtx->cr4));
426 Log4(("%s: rsp=%#RX64 rbp=%#RX64 rdi=%#RX64\n", pszPrefix, pCtx->rsp, pCtx->rbp, pCtx->rdi));
427 if (fFlags & HMSVM_LOG_CS)
428 {
429 Log4(("%s: cs={%04x base=%016RX64 limit=%08x flags=%08x}\n", pszPrefix, pCtx->cs.Sel, pCtx->cs.u64Base,
430 pCtx->cs.u32Limit, pCtx->cs.Attr.u));
431 }
432 if (fFlags & HMSVM_LOG_SS)
433 {
434 Log4(("%s: ss={%04x base=%016RX64 limit=%08x flags=%08x}\n", pszPrefix, pCtx->ss.Sel, pCtx->ss.u64Base,
435 pCtx->ss.u32Limit, pCtx->ss.Attr.u));
436 }
437 if (fFlags & HMSVM_LOG_FS)
438 {
439 Log4(("%s: fs={%04x base=%016RX64 limit=%08x flags=%08x}\n", pszPrefix, pCtx->fs.Sel, pCtx->fs.u64Base,
440 pCtx->fs.u32Limit, pCtx->fs.Attr.u));
441 }
442 if (fFlags & HMSVM_LOG_GS)
443 {
444 Log4(("%s: gs={%04x base=%016RX64 limit=%08x flags=%08x}\n", pszPrefix, pCtx->gs.Sel, pCtx->gs.u64Base,
445 pCtx->gs.u32Limit, pCtx->gs.Attr.u));
446 }
447
448 PCSVMVMCBSTATESAVE pVmcbGuest = &pVmcb->guest;
449 if (fFlags & HMSVM_LOG_LBR)
450 {
451 Log4(("%s: br_from=%#RX64 br_to=%#RX64 lastxcpt_from=%#RX64 lastxcpt_to=%#RX64\n", pszPrefix, pVmcbGuest->u64BR_FROM,
452 pVmcbGuest->u64BR_TO, pVmcbGuest->u64LASTEXCPFROM, pVmcbGuest->u64LASTEXCPTO));
453 }
454 NOREF(pVmcbGuest);
455}
456#endif
457
458
459/**
460 * Sets up and activates AMD-V on the current CPU.
461 *
462 * @returns VBox status code.
463 * @param pCpu Pointer to the CPU info struct.
464 * @param pVM The cross context VM structure. Can be
465 * NULL after a resume!
466 * @param pvCpuPage Pointer to the global CPU page.
467 * @param HCPhysCpuPage Physical address of the global CPU page.
468 * @param fEnabledByHost Whether the host OS has already initialized AMD-V.
469 * @param pvArg Unused on AMD-V.
470 */
471VMMR0DECL(int) SVMR0EnableCpu(PHMGLOBALCPUINFO pCpu, PVM pVM, void *pvCpuPage, RTHCPHYS HCPhysCpuPage, bool fEnabledByHost,
472 void *pvArg)
473{
474 Assert(!fEnabledByHost);
475 Assert(HCPhysCpuPage && HCPhysCpuPage != NIL_RTHCPHYS);
476 Assert(RT_ALIGN_T(HCPhysCpuPage, _4K, RTHCPHYS) == HCPhysCpuPage);
477 Assert(pvCpuPage); NOREF(pvCpuPage);
478 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
479
480 NOREF(pvArg);
481 NOREF(fEnabledByHost);
482
483 /* Paranoid: Disable interrupt as, in theory, interrupt handlers might mess with EFER. */
484 RTCCUINTREG fEFlags = ASMIntDisableFlags();
485
486 /*
487 * We must turn on AMD-V and setup the host state physical address, as those MSRs are per CPU.
488 */
489 uint64_t u64HostEfer = ASMRdMsr(MSR_K6_EFER);
490 if (u64HostEfer & MSR_K6_EFER_SVME)
491 {
492 /* If the VBOX_HWVIRTEX_IGNORE_SVM_IN_USE is active, then we blindly use AMD-V. */
493 if ( pVM
494 && pVM->hm.s.svm.fIgnoreInUseError)
495 {
496 pCpu->fIgnoreAMDVInUseError = true;
497 }
498
499 if (!pCpu->fIgnoreAMDVInUseError)
500 {
501 ASMSetFlags(fEFlags);
502 return VERR_SVM_IN_USE;
503 }
504 }
505
506 /* Turn on AMD-V in the EFER MSR. */
507 ASMWrMsr(MSR_K6_EFER, u64HostEfer | MSR_K6_EFER_SVME);
508
509 /* Write the physical page address where the CPU will store the host state while executing the VM. */
510 ASMWrMsr(MSR_K8_VM_HSAVE_PA, HCPhysCpuPage);
511
512 /* Restore interrupts. */
513 ASMSetFlags(fEFlags);
514
515 /*
516 * Theoretically, other hypervisors may have used ASIDs, ideally we should flush all non-zero ASIDs
517 * when enabling SVM. AMD doesn't have an SVM instruction to flush all ASIDs (flushing is done
518 * upon VMRUN). Therefore, flag that we need to flush the TLB entirely with before executing any
519 * guest code.
520 */
521 pCpu->fFlushAsidBeforeUse = true;
522
523 /*
524 * Ensure each VCPU scheduled on this CPU gets a new ASID on resume. See @bugref{6255}.
525 */
526 ++pCpu->cTlbFlushes;
527
528 return VINF_SUCCESS;
529}
530
531
532/**
533 * Deactivates AMD-V on the current CPU.
534 *
535 * @returns VBox status code.
536 * @param pCpu Pointer to the CPU info struct.
537 * @param pvCpuPage Pointer to the global CPU page.
538 * @param HCPhysCpuPage Physical address of the global CPU page.
539 */
540VMMR0DECL(int) SVMR0DisableCpu(PHMGLOBALCPUINFO pCpu, void *pvCpuPage, RTHCPHYS HCPhysCpuPage)
541{
542 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
543 AssertReturn( HCPhysCpuPage
544 && HCPhysCpuPage != NIL_RTHCPHYS, VERR_INVALID_PARAMETER);
545 AssertReturn(pvCpuPage, VERR_INVALID_PARAMETER);
546 NOREF(pCpu);
547
548 /* Paranoid: Disable interrupts as, in theory, interrupt handlers might mess with EFER. */
549 RTCCUINTREG fEFlags = ASMIntDisableFlags();
550
551 /* Turn off AMD-V in the EFER MSR. */
552 uint64_t u64HostEfer = ASMRdMsr(MSR_K6_EFER);
553 ASMWrMsr(MSR_K6_EFER, u64HostEfer & ~MSR_K6_EFER_SVME);
554
555 /* Invalidate host state physical address. */
556 ASMWrMsr(MSR_K8_VM_HSAVE_PA, 0);
557
558 /* Restore interrupts. */
559 ASMSetFlags(fEFlags);
560
561 return VINF_SUCCESS;
562}
563
564
565/**
566 * Does global AMD-V initialization (called during module initialization).
567 *
568 * @returns VBox status code.
569 */
570VMMR0DECL(int) SVMR0GlobalInit(void)
571{
572 /*
573 * Allocate 12 KB for the IO bitmap. Since this is non-optional and we always intercept all IO accesses, it's done
574 * once globally here instead of per-VM.
575 */
576 Assert(g_hMemObjIOBitmap == NIL_RTR0MEMOBJ);
577 int rc = RTR0MemObjAllocCont(&g_hMemObjIOBitmap, SVM_IOPM_PAGES << X86_PAGE_4K_SHIFT, false /* fExecutable */);
578 if (RT_FAILURE(rc))
579 return rc;
580
581 g_pvIOBitmap = RTR0MemObjAddress(g_hMemObjIOBitmap);
582 g_HCPhysIOBitmap = RTR0MemObjGetPagePhysAddr(g_hMemObjIOBitmap, 0 /* iPage */);
583
584 /* Set all bits to intercept all IO accesses. */
585 ASMMemFill32(g_pvIOBitmap, SVM_IOPM_PAGES << X86_PAGE_4K_SHIFT, UINT32_C(0xffffffff));
586
587#ifdef VBOX_WITH_NESTED_HWVIRT
588 /*
589 * Allocate 8 KB for the MSR permission bitmap for the nested-guest.
590 */
591 Assert(g_hMemObjNstGstMsrBitmap == NIL_RTR0MEMOBJ);
592 rc = RTR0MemObjAllocCont(&g_hMemObjNstGstMsrBitmap, SVM_MSRPM_PAGES << X86_PAGE_4K_SHIFT, false /* fExecutable */);
593 if (RT_FAILURE(rc))
594 return rc;
595
596 g_pvNstGstMsrBitmap = RTR0MemObjAddress(g_hMemObjNstGstMsrBitmap);
597 g_HCPhysNstGstMsrBitmap = RTR0MemObjGetPagePhysAddr(g_hMemObjNstGstMsrBitmap, 0 /* iPage */);
598
599 /* Set all bits to intercept all MSR accesses. */
600 ASMMemFill32(g_pvNstGstMsrBitmap, SVM_MSRPM_PAGES << X86_PAGE_4K_SHIFT, UINT32_C(0xffffffff));
601#endif
602
603 return VINF_SUCCESS;
604}
605
606
607/**
608 * Does global AMD-V termination (called during module termination).
609 */
610VMMR0DECL(void) SVMR0GlobalTerm(void)
611{
612 if (g_hMemObjIOBitmap != NIL_RTR0MEMOBJ)
613 {
614 RTR0MemObjFree(g_hMemObjIOBitmap, true /* fFreeMappings */);
615 g_pvIOBitmap = NULL;
616 g_HCPhysIOBitmap = 0;
617 g_hMemObjIOBitmap = NIL_RTR0MEMOBJ;
618 }
619
620#ifdef VBOX_WITH_NESTED_HWVIRT
621 if (g_hMemObjNstGstMsrBitmap != NIL_RTR0MEMOBJ)
622 {
623 RTR0MemObjFree(g_hMemObjNstGstMsrBitmap, true /* fFreeMappings */);
624 g_pvNstGstMsrBitmap = NULL;
625 g_HCPhysNstGstMsrBitmap = 0;
626 g_hMemObjNstGstMsrBitmap = NIL_RTR0MEMOBJ;
627 }
628#endif
629}
630
631
632/**
633 * Frees any allocated per-VCPU structures for a VM.
634 *
635 * @param pVM The cross context VM structure.
636 */
637DECLINLINE(void) hmR0SvmFreeStructs(PVM pVM)
638{
639 for (uint32_t i = 0; i < pVM->cCpus; i++)
640 {
641 PVMCPU pVCpu = &pVM->aCpus[i];
642 AssertPtr(pVCpu);
643
644 if (pVCpu->hm.s.svm.hMemObjVmcbHost != NIL_RTR0MEMOBJ)
645 {
646 RTR0MemObjFree(pVCpu->hm.s.svm.hMemObjVmcbHost, false);
647 pVCpu->hm.s.svm.HCPhysVmcbHost = 0;
648 pVCpu->hm.s.svm.hMemObjVmcbHost = NIL_RTR0MEMOBJ;
649 }
650
651 if (pVCpu->hm.s.svm.hMemObjVmcb != NIL_RTR0MEMOBJ)
652 {
653 RTR0MemObjFree(pVCpu->hm.s.svm.hMemObjVmcb, false);
654 pVCpu->hm.s.svm.pVmcb = NULL;
655 pVCpu->hm.s.svm.HCPhysVmcb = 0;
656 pVCpu->hm.s.svm.hMemObjVmcb = NIL_RTR0MEMOBJ;
657 }
658
659 if (pVCpu->hm.s.svm.hMemObjMsrBitmap != NIL_RTR0MEMOBJ)
660 {
661 RTR0MemObjFree(pVCpu->hm.s.svm.hMemObjMsrBitmap, false);
662 pVCpu->hm.s.svm.pvMsrBitmap = NULL;
663 pVCpu->hm.s.svm.HCPhysMsrBitmap = 0;
664 pVCpu->hm.s.svm.hMemObjMsrBitmap = NIL_RTR0MEMOBJ;
665 }
666 }
667}
668
669
670/**
671 * Does per-VM AMD-V initialization.
672 *
673 * @returns VBox status code.
674 * @param pVM The cross context VM structure.
675 */
676VMMR0DECL(int) SVMR0InitVM(PVM pVM)
677{
678 int rc = VERR_INTERNAL_ERROR_5;
679
680 /*
681 * Check for an AMD CPU erratum which requires us to flush the TLB before every world-switch.
682 */
683 uint32_t u32Family;
684 uint32_t u32Model;
685 uint32_t u32Stepping;
686 if (HMAmdIsSubjectToErratum170(&u32Family, &u32Model, &u32Stepping))
687 {
688 Log4(("SVMR0InitVM: AMD cpu with erratum 170 family %#x model %#x stepping %#x\n", u32Family, u32Model, u32Stepping));
689 pVM->hm.s.svm.fAlwaysFlushTLB = true;
690 }
691
692 /*
693 * Initialize the R0 memory objects up-front so we can properly cleanup on allocation failures.
694 */
695 for (VMCPUID i = 0; i < pVM->cCpus; i++)
696 {
697 PVMCPU pVCpu = &pVM->aCpus[i];
698 pVCpu->hm.s.svm.hMemObjVmcbHost = NIL_RTR0MEMOBJ;
699 pVCpu->hm.s.svm.hMemObjVmcb = NIL_RTR0MEMOBJ;
700 pVCpu->hm.s.svm.hMemObjMsrBitmap = NIL_RTR0MEMOBJ;
701 }
702
703 for (VMCPUID i = 0; i < pVM->cCpus; i++)
704 {
705 PVMCPU pVCpu = &pVM->aCpus[i];
706
707 /*
708 * Allocate one page for the host-context VM control block (VMCB). This is used for additional host-state (such as
709 * FS, GS, Kernel GS Base, etc.) apart from the host-state save area specified in MSR_K8_VM_HSAVE_PA.
710 */
711 rc = RTR0MemObjAllocCont(&pVCpu->hm.s.svm.hMemObjVmcbHost, SVM_VMCB_PAGES << PAGE_SHIFT, false /* fExecutable */);
712 if (RT_FAILURE(rc))
713 goto failure_cleanup;
714
715 void *pvVmcbHost = RTR0MemObjAddress(pVCpu->hm.s.svm.hMemObjVmcbHost);
716 pVCpu->hm.s.svm.HCPhysVmcbHost = RTR0MemObjGetPagePhysAddr(pVCpu->hm.s.svm.hMemObjVmcbHost, 0 /* iPage */);
717 Assert(pVCpu->hm.s.svm.HCPhysVmcbHost < _4G);
718 ASMMemZeroPage(pvVmcbHost);
719
720 /*
721 * Allocate one page for the guest-state VMCB.
722 */
723 rc = RTR0MemObjAllocCont(&pVCpu->hm.s.svm.hMemObjVmcb, SVM_VMCB_PAGES << PAGE_SHIFT, false /* fExecutable */);
724 if (RT_FAILURE(rc))
725 goto failure_cleanup;
726
727 pVCpu->hm.s.svm.pVmcb = (PSVMVMCB)RTR0MemObjAddress(pVCpu->hm.s.svm.hMemObjVmcb);
728 pVCpu->hm.s.svm.HCPhysVmcb = RTR0MemObjGetPagePhysAddr(pVCpu->hm.s.svm.hMemObjVmcb, 0 /* iPage */);
729 Assert(pVCpu->hm.s.svm.HCPhysVmcb < _4G);
730 ASMMemZeroPage(pVCpu->hm.s.svm.pVmcb);
731
732 /*
733 * Allocate two pages (8 KB) for the MSR permission bitmap. There doesn't seem to be a way to convince
734 * SVM to not require one.
735 */
736 rc = RTR0MemObjAllocCont(&pVCpu->hm.s.svm.hMemObjMsrBitmap, SVM_MSRPM_PAGES << X86_PAGE_4K_SHIFT,
737 false /* fExecutable */);
738 if (RT_FAILURE(rc))
739 goto failure_cleanup;
740
741 pVCpu->hm.s.svm.pvMsrBitmap = RTR0MemObjAddress(pVCpu->hm.s.svm.hMemObjMsrBitmap);
742 pVCpu->hm.s.svm.HCPhysMsrBitmap = RTR0MemObjGetPagePhysAddr(pVCpu->hm.s.svm.hMemObjMsrBitmap, 0 /* iPage */);
743 /* Set all bits to intercept all MSR accesses (changed later on). */
744 ASMMemFill32(pVCpu->hm.s.svm.pvMsrBitmap, SVM_MSRPM_PAGES << X86_PAGE_4K_SHIFT, UINT32_C(0xffffffff));
745 }
746
747 return VINF_SUCCESS;
748
749failure_cleanup:
750 hmR0SvmFreeStructs(pVM);
751 return rc;
752}
753
754
755/**
756 * Does per-VM AMD-V termination.
757 *
758 * @returns VBox status code.
759 * @param pVM The cross context VM structure.
760 */
761VMMR0DECL(int) SVMR0TermVM(PVM pVM)
762{
763 hmR0SvmFreeStructs(pVM);
764 return VINF_SUCCESS;
765}
766
767
768/**
769 * Returns whether the VMCB Clean Bits feature is supported.
770 *
771 * @return @c true if supported, @c false otherwise.
772 * @param pVCpu The cross context virtual CPU structure.
773 * @param pCtx Pointer to the guest-CPU context.
774 */
775DECLINLINE(bool) hmR0SvmSupportsVmcbCleanBits(PVMCPU pVCpu, PCPUMCTX pCtx)
776{
777 PVM pVM = pVCpu->CTX_SUFF(pVM);
778#ifdef VBOX_WITH_NESTED_HWVIRT
779 if (CPUMIsGuestInSvmNestedHwVirtMode(pCtx))
780 {
781 return (pVM->hm.s.svm.u32Features & X86_CPUID_SVM_FEATURE_EDX_VMCB_CLEAN)
782 && pVM->cpum.ro.GuestFeatures.fSvmVmcbClean;
783 }
784#else
785 RT_NOREF(pCtx);
786#endif
787 return RT_BOOL(pVM->hm.s.svm.u32Features & X86_CPUID_SVM_FEATURE_EDX_VMCB_CLEAN);
788}
789
790
791/**
792 * Returns whether the decode assists feature is supported.
793 *
794 * @return @c true if supported, @c false otherwise.
795 * @param pVCpu The cross context virtual CPU structure.
796 * @param pCtx Pointer to the guest-CPU context.
797 */
798DECLINLINE(bool) hmR0SvmSupportsDecodeAssists(PVMCPU pVCpu, PCPUMCTX pCtx)
799{
800 PVM pVM = pVCpu->CTX_SUFF(pVM);
801#ifdef VBOX_WITH_NESTED_HWVIRT
802 if (CPUMIsGuestInSvmNestedHwVirtMode(pCtx))
803 {
804 return (pVM->hm.s.svm.u32Features & X86_CPUID_SVM_FEATURE_EDX_DECODE_ASSISTS)
805 && pVM->cpum.ro.GuestFeatures.fSvmDecodeAssists;
806 }
807#else
808 RT_NOREF(pCtx);
809#endif
810 return RT_BOOL(pVM->hm.s.svm.u32Features & X86_CPUID_SVM_FEATURE_EDX_DECODE_ASSISTS);
811}
812
813
814/**
815 * Returns whether the NRIP_SAVE feature is supported.
816 *
817 * @return @c true if supported, @c false otherwise.
818 * @param pVCpu The cross context virtual CPU structure.
819 * @param pCtx Pointer to the guest-CPU context.
820 */
821DECLINLINE(bool) hmR0SvmSupportsNextRipSave(PVMCPU pVCpu, PCPUMCTX pCtx)
822{
823 PVM pVM = pVCpu->CTX_SUFF(pVM);
824#ifdef VBOX_WITH_NESTED_HWVIRT
825 if (CPUMIsGuestInSvmNestedHwVirtMode(pCtx))
826 {
827 return (pVM->hm.s.svm.u32Features & X86_CPUID_SVM_FEATURE_EDX_NRIP_SAVE)
828 && pVM->cpum.ro.GuestFeatures.fSvmNextRipSave;
829 }
830#else
831 RT_NOREF(pCtx);
832#endif
833 return RT_BOOL(pVM->hm.s.svm.u32Features & X86_CPUID_SVM_FEATURE_EDX_NRIP_SAVE);
834}
835
836
837/**
838 * Sets the permission bits for the specified MSR in the MSRPM.
839 *
840 * @param pVmcb Pointer to the VM control block.
841 * @param pbMsrBitmap Pointer to the MSR bitmap.
842 * @param uMsr The MSR for which the access permissions are being set.
843 * @param enmRead MSR read permissions.
844 * @param enmWrite MSR write permissions.
845 */
846static void hmR0SvmSetMsrPermission(PSVMVMCB pVmcb, uint8_t *pbMsrBitmap, unsigned uMsr, SVMMSREXITREAD enmRead,
847 SVMMSREXITWRITE enmWrite)
848{
849 uint16_t offMsrpm;
850 uint32_t uMsrpmBit;
851 int rc = HMSvmGetMsrpmOffsetAndBit(uMsr, &offMsrpm, &uMsrpmBit);
852 AssertRC(rc);
853
854 Assert(uMsrpmBit < 0x3fff);
855 Assert(offMsrpm < SVM_MSRPM_PAGES << X86_PAGE_4K_SHIFT);
856
857 pbMsrBitmap += offMsrpm;
858 if (enmRead == SVMMSREXIT_INTERCEPT_READ)
859 ASMBitSet(pbMsrBitmap, uMsrpmBit);
860 else
861 ASMBitClear(pbMsrBitmap, uMsrpmBit);
862
863 if (enmWrite == SVMMSREXIT_INTERCEPT_WRITE)
864 ASMBitSet(pbMsrBitmap, uMsrpmBit + 1);
865 else
866 ASMBitClear(pbMsrBitmap, uMsrpmBit + 1);
867
868 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_IOPM_MSRPM;
869}
870
871
872/**
873 * Sets up AMD-V for the specified VM.
874 * This function is only called once per-VM during initalization.
875 *
876 * @returns VBox status code.
877 * @param pVM The cross context VM structure.
878 */
879VMMR0DECL(int) SVMR0SetupVM(PVM pVM)
880{
881 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
882 AssertReturn(pVM, VERR_INVALID_PARAMETER);
883 Assert(pVM->hm.s.svm.fSupported);
884
885 bool const fPauseFilter = RT_BOOL(pVM->hm.s.svm.u32Features & X86_CPUID_SVM_FEATURE_EDX_PAUSE_FILTER);
886 bool const fPauseFilterThreshold = RT_BOOL(pVM->hm.s.svm.u32Features & X86_CPUID_SVM_FEATURE_EDX_PAUSE_FILTER_THRESHOLD);
887 bool const fUsePauseFilter = fPauseFilter && pVM->hm.s.svm.cPauseFilter && pVM->hm.s.svm.cPauseFilterThresholdTicks;
888
889 bool const fLbrVirt = RT_BOOL(pVM->hm.s.svm.u32Features & X86_CPUID_SVM_FEATURE_EDX_LBR_VIRT);
890 bool const fUseLbrVirt = fLbrVirt; /** @todo CFGM, IEM implementation etc. */
891
892#ifdef VBOX_WITH_NESTED_HWVIRT
893 bool const fVirtVmsaveVmload = RT_BOOL(pVM->hm.s.svm.u32Features & X86_CPUID_SVM_FEATURE_EDX_VIRT_VMSAVE_VMLOAD);
894 bool const fUseVirtVmsaveVmload = fVirtVmsaveVmload && pVM->hm.s.svm.fVirtVmsaveVmload && pVM->hm.s.fNestedPaging;
895
896 bool const fVGif = RT_BOOL(pVM->hm.s.svm.u32Features & X86_CPUID_SVM_FEATURE_EDX_VGIF);
897 bool const fUseVGif = fVGif && pVM->hm.s.svm.fVGif;
898#endif
899
900 for (VMCPUID i = 0; i < pVM->cCpus; i++)
901 {
902 PVMCPU pVCpu = &pVM->aCpus[i];
903 PSVMVMCB pVmcb = pVM->aCpus[i].hm.s.svm.pVmcb;
904
905 AssertMsgReturn(pVmcb, ("Invalid pVmcb for vcpu[%u]\n", i), VERR_SVM_INVALID_PVMCB);
906
907 /* Initialize the #VMEXIT history array with end-of-array markers (UINT16_MAX). */
908 Assert(!pVCpu->hm.s.idxExitHistoryFree);
909 HMCPU_EXIT_HISTORY_RESET(pVCpu);
910
911 /* Always trap #AC for reasons of security. */
912 pVmcb->ctrl.u32InterceptXcpt |= RT_BIT_32(X86_XCPT_AC);
913
914 /* Always trap #DB for reasons of security. */
915 pVmcb->ctrl.u32InterceptXcpt |= RT_BIT_32(X86_XCPT_DB);
916
917 /* Trap exceptions unconditionally (debug purposes). */
918#ifdef HMSVM_ALWAYS_TRAP_PF
919 pVmcb->ctrl.u32InterceptXcpt |= RT_BIT(X86_XCPT_PF);
920#endif
921#ifdef HMSVM_ALWAYS_TRAP_ALL_XCPTS
922 /* If you add any exceptions here, make sure to update hmR0SvmHandleExit(). */
923 pVmcb->ctrl.u32InterceptXcpt |= 0
924 | RT_BIT(X86_XCPT_BP)
925 | RT_BIT(X86_XCPT_DE)
926 | RT_BIT(X86_XCPT_NM)
927 | RT_BIT(X86_XCPT_UD)
928 | RT_BIT(X86_XCPT_NP)
929 | RT_BIT(X86_XCPT_SS)
930 | RT_BIT(X86_XCPT_GP)
931 | RT_BIT(X86_XCPT_PF)
932 | RT_BIT(X86_XCPT_MF)
933 ;
934#endif
935
936 /* Set up unconditional intercepts and conditions. */
937 pVmcb->ctrl.u64InterceptCtrl = HMSVM_MANDATORY_GUEST_CTRL_INTERCEPTS;
938
939 /* CR0, CR4 reads must be intercepted, our shadow values are not necessarily the same as the guest's. */
940 pVmcb->ctrl.u16InterceptRdCRx = RT_BIT(0) | RT_BIT(4);
941
942 /* CR0, CR4 writes must be intercepted for the same reasons as above. */
943 pVmcb->ctrl.u16InterceptWrCRx = RT_BIT(0) | RT_BIT(4);
944
945 /* Intercept all DRx reads and writes by default. Changed later on. */
946 pVmcb->ctrl.u16InterceptRdDRx = 0xffff;
947 pVmcb->ctrl.u16InterceptWrDRx = 0xffff;
948
949 /* Virtualize masking of INTR interrupts. (reads/writes from/to CR8 go to the V_TPR register) */
950 pVmcb->ctrl.IntCtrl.n.u1VIntrMasking = 1;
951
952 /* Ignore the priority in the virtual TPR. This is necessary for delivering PIC style (ExtInt) interrupts
953 and we currently deliver both PIC and APIC interrupts alike. See hmR0SvmInjectPendingEvent() */
954 pVmcb->ctrl.IntCtrl.n.u1IgnoreTPR = 1;
955
956 /* Set IO and MSR bitmap permission bitmap physical addresses. */
957 pVmcb->ctrl.u64IOPMPhysAddr = g_HCPhysIOBitmap;
958 pVmcb->ctrl.u64MSRPMPhysAddr = pVCpu->hm.s.svm.HCPhysMsrBitmap;
959
960 /* LBR virtualization. */
961 if (fUseLbrVirt)
962 {
963 pVmcb->ctrl.LbrVirt.n.u1LbrVirt = fUseLbrVirt;
964 pVmcb->guest.u64DBGCTL = MSR_IA32_DEBUGCTL_LBR;
965 }
966 else
967 Assert(pVmcb->ctrl.LbrVirt.n.u1LbrVirt == 0);
968
969#ifdef VBOX_WITH_NESTED_HWVIRT
970 /* Virtualized VMSAVE/VMLOAD. */
971 pVmcb->ctrl.LbrVirt.n.u1VirtVmsaveVmload = fUseVirtVmsaveVmload;
972 if (!fUseVirtVmsaveVmload)
973 {
974 pVmcb->ctrl.u64InterceptCtrl |= SVM_CTRL_INTERCEPT_VMSAVE
975 | SVM_CTRL_INTERCEPT_VMLOAD;
976 }
977
978 /* Virtual GIF. */
979 pVmcb->ctrl.IntCtrl.n.u1VGifEnable = fUseVGif;
980 if (!fUseVGif)
981 {
982 pVmcb->ctrl.u64InterceptCtrl |= SVM_CTRL_INTERCEPT_CLGI
983 | SVM_CTRL_INTERCEPT_STGI;
984 }
985#endif
986
987 /* Initially all VMCB clean bits MBZ indicating that everything should be loaded from the VMCB in memory. */
988 Assert(pVmcb->ctrl.u32VmcbCleanBits == 0);
989
990 /* The host ASID MBZ, for the guest start with 1. */
991 pVmcb->ctrl.TLBCtrl.n.u32ASID = 1;
992
993 /*
994 * Setup the PAT MSR (applicable for Nested Paging only).
995 * The default value should be 0x0007040600070406ULL, but we want to treat all guest memory as WB,
996 * so choose type 6 for all PAT slots.
997 */
998 pVmcb->guest.u64GPAT = UINT64_C(0x0006060606060606);
999
1000 /* Setup Nested Paging. This doesn't change throughout the execution time of the VM. */
1001 pVmcb->ctrl.NestedPaging.n.u1NestedPaging = pVM->hm.s.fNestedPaging;
1002
1003 /* Without Nested Paging, we need additionally intercepts. */
1004 if (!pVM->hm.s.fNestedPaging)
1005 {
1006 /* CR3 reads/writes must be intercepted; our shadow values differ from the guest values. */
1007 pVmcb->ctrl.u16InterceptRdCRx |= RT_BIT(3);
1008 pVmcb->ctrl.u16InterceptWrCRx |= RT_BIT(3);
1009
1010 /* Intercept INVLPG and task switches (may change CR3, EFLAGS, LDT). */
1011 pVmcb->ctrl.u64InterceptCtrl |= SVM_CTRL_INTERCEPT_INVLPG
1012 | SVM_CTRL_INTERCEPT_TASK_SWITCH;
1013
1014 /* Page faults must be intercepted to implement shadow paging. */
1015 pVmcb->ctrl.u32InterceptXcpt |= RT_BIT(X86_XCPT_PF);
1016 }
1017
1018#ifdef HMSVM_ALWAYS_TRAP_TASK_SWITCH
1019 pVmcb->ctrl.u64InterceptCtrl |= SVM_CTRL_INTERCEPT_TASK_SWITCH;
1020#endif
1021
1022 /* Apply the exceptions intercepts needed by the GIM provider. */
1023 if (pVCpu->hm.s.fGIMTrapXcptUD)
1024 pVmcb->ctrl.u32InterceptXcpt |= RT_BIT(X86_XCPT_UD);
1025
1026 /* Setup Pause Filter for guest pause-loop (spinlock) exiting. */
1027 if (fUsePauseFilter)
1028 {
1029 pVmcb->ctrl.u16PauseFilterCount = pVM->hm.s.svm.cPauseFilter;
1030 if (fPauseFilterThreshold)
1031 pVmcb->ctrl.u16PauseFilterThreshold = pVM->hm.s.svm.cPauseFilterThresholdTicks;
1032 }
1033
1034 /*
1035 * The following MSRs are saved/restored automatically during the world-switch.
1036 * Don't intercept guest read/write accesses to these MSRs.
1037 */
1038 uint8_t *pbMsrBitmap = (uint8_t *)pVCpu->hm.s.svm.pvMsrBitmap;
1039 hmR0SvmSetMsrPermission(pVmcb, pbMsrBitmap, MSR_K8_LSTAR, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
1040 hmR0SvmSetMsrPermission(pVmcb, pbMsrBitmap, MSR_K8_CSTAR, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
1041 hmR0SvmSetMsrPermission(pVmcb, pbMsrBitmap, MSR_K6_STAR, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
1042 hmR0SvmSetMsrPermission(pVmcb, pbMsrBitmap, MSR_K8_SF_MASK, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
1043 hmR0SvmSetMsrPermission(pVmcb, pbMsrBitmap, MSR_K8_FS_BASE, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
1044 hmR0SvmSetMsrPermission(pVmcb, pbMsrBitmap, MSR_K8_GS_BASE, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
1045 hmR0SvmSetMsrPermission(pVmcb, pbMsrBitmap, MSR_K8_KERNEL_GS_BASE, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
1046 hmR0SvmSetMsrPermission(pVmcb, pbMsrBitmap, MSR_IA32_SYSENTER_CS, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
1047 hmR0SvmSetMsrPermission(pVmcb, pbMsrBitmap, MSR_IA32_SYSENTER_ESP, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
1048 hmR0SvmSetMsrPermission(pVmcb, pbMsrBitmap, MSR_IA32_SYSENTER_EIP, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
1049 }
1050
1051 return VINF_SUCCESS;
1052}
1053
1054
1055/**
1056 * Gets a pointer to the currently active guest or nested-guest VMCB.
1057 *
1058 * @returns Pointer to the current context VMCB.
1059 * @param pVCpu The cross context virtual CPU structure.
1060 * @param pCtx Pointer to the guest-CPU context.
1061 */
1062DECLINLINE(PSVMVMCB) hmR0SvmGetCurrentVmcb(PVMCPU pVCpu, PCPUMCTX pCtx)
1063{
1064#ifdef VBOX_WITH_NESTED_HWVIRT
1065 if (CPUMIsGuestInSvmNestedHwVirtMode(pCtx))
1066 return pCtx->hwvirt.svm.CTX_SUFF(pVmcb);
1067#else
1068 RT_NOREF(pCtx);
1069#endif
1070 return pVCpu->hm.s.svm.pVmcb;
1071}
1072
1073
1074/**
1075 * Invalidates a guest page by guest virtual address.
1076 *
1077 * @returns VBox status code.
1078 * @param pVM The cross context VM structure.
1079 * @param pVCpu The cross context virtual CPU structure.
1080 * @param GCVirt Guest virtual address of the page to invalidate.
1081 */
1082VMMR0DECL(int) SVMR0InvalidatePage(PVM pVM, PVMCPU pVCpu, RTGCPTR GCVirt)
1083{
1084 AssertReturn(pVM, VERR_INVALID_PARAMETER);
1085 Assert(pVM->hm.s.svm.fSupported);
1086
1087 bool fFlushPending = pVM->hm.s.svm.fAlwaysFlushTLB || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_TLB_FLUSH);
1088
1089 /* Skip it if a TLB flush is already pending. */
1090 if (!fFlushPending)
1091 {
1092 Log4(("SVMR0InvalidatePage %RGv\n", GCVirt));
1093
1094 PCPUMCTX pCtx = CPUMQueryGuestCtxPtr(pVCpu);
1095 PSVMVMCB pVmcb = hmR0SvmGetCurrentVmcb(pVCpu, pCtx);
1096 AssertMsgReturn(pVmcb, ("Invalid pVmcb!\n"), VERR_SVM_INVALID_PVMCB);
1097
1098#if HC_ARCH_BITS == 32
1099 /* If we get a flush in 64-bit guest mode, then force a full TLB flush. INVLPGA takes only 32-bit addresses. */
1100 if (CPUMIsGuestInLongMode(pVCpu))
1101 VMCPU_FF_SET(pVCpu, VMCPU_FF_TLB_FLUSH);
1102 else
1103#endif
1104 {
1105 SVMR0InvlpgA(GCVirt, pVmcb->ctrl.TLBCtrl.n.u32ASID);
1106 STAM_COUNTER_INC(&pVCpu->hm.s.StatFlushTlbInvlpgVirt);
1107 }
1108 }
1109 return VINF_SUCCESS;
1110}
1111
1112
1113/**
1114 * Flushes the appropriate tagged-TLB entries.
1115 *
1116 * @param pVCpu The cross context virtual CPU structure.
1117 * @param pCtx Pointer to the guest-CPU or nested-guest-CPU context.
1118 * @param pVmcb Pointer to the VM control block.
1119 */
1120static void hmR0SvmFlushTaggedTlb(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMVMCB pVmcb)
1121{
1122#ifndef VBOX_WITH_NESTED_HWVIRT
1123 RT_NOREF(pCtx);
1124#endif
1125
1126 PVM pVM = pVCpu->CTX_SUFF(pVM);
1127 PHMGLOBALCPUINFO pCpu = hmR0GetCurrentCpu();
1128
1129 /*
1130 * Force a TLB flush for the first world switch if the current CPU differs from the one we ran on last.
1131 * This can happen both for start & resume due to long jumps back to ring-3.
1132 *
1133 * We also force a TLB flush every time when executing a nested-guest VCPU as there is no correlation
1134 * between it and the physical CPU.
1135 *
1136 * If the TLB flush count changed, another VM (VCPU rather) has hit the ASID limit while flushing the TLB,
1137 * so we cannot reuse the ASIDs without flushing.
1138 */
1139 bool fNewAsid = false;
1140 Assert(pCpu->idCpu != NIL_RTCPUID);
1141 if ( pVCpu->hm.s.idLastCpu != pCpu->idCpu
1142 || pVCpu->hm.s.cTlbFlushes != pCpu->cTlbFlushes
1143#ifdef VBOX_WITH_NESTED_HWVIRT
1144 || CPUMIsGuestInSvmNestedHwVirtMode(pCtx)
1145#endif
1146 )
1147 {
1148 STAM_COUNTER_INC(&pVCpu->hm.s.StatFlushTlbWorldSwitch);
1149 pVCpu->hm.s.fForceTLBFlush = true;
1150 fNewAsid = true;
1151 }
1152
1153 /* Set TLB flush state as checked until we return from the world switch. */
1154 ASMAtomicWriteBool(&pVCpu->hm.s.fCheckedTLBFlush, true);
1155
1156 /* Check for explicit TLB flushes. */
1157 if (VMCPU_FF_TEST_AND_CLEAR(pVCpu, VMCPU_FF_TLB_FLUSH))
1158 {
1159 pVCpu->hm.s.fForceTLBFlush = true;
1160 STAM_COUNTER_INC(&pVCpu->hm.s.StatFlushTlb);
1161 }
1162
1163 /*
1164 * If the AMD CPU erratum 170, We need to flush the entire TLB for each world switch. Sad.
1165 * This Host CPU requirement takes precedence.
1166 */
1167 if (pVM->hm.s.svm.fAlwaysFlushTLB)
1168 {
1169 pCpu->uCurrentAsid = 1;
1170 pVCpu->hm.s.uCurrentAsid = 1;
1171 pVCpu->hm.s.cTlbFlushes = pCpu->cTlbFlushes;
1172 pVmcb->ctrl.TLBCtrl.n.u8TLBFlush = SVM_TLB_FLUSH_ENTIRE;
1173
1174 /* Clear the VMCB Clean Bit for NP while flushing the TLB. See @bugref{7152}. */
1175 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_NP;
1176
1177 /* Keep track of last CPU ID even when flushing all the time. */
1178 if (fNewAsid)
1179 pVCpu->hm.s.idLastCpu = pCpu->idCpu;
1180 }
1181 else
1182 {
1183 pVmcb->ctrl.TLBCtrl.n.u8TLBFlush = SVM_TLB_FLUSH_NOTHING;
1184 if (pVCpu->hm.s.fForceTLBFlush)
1185 {
1186 /* Clear the VMCB Clean Bit for NP while flushing the TLB. See @bugref{7152}. */
1187 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_NP;
1188
1189 if (fNewAsid)
1190 {
1191 ++pCpu->uCurrentAsid;
1192
1193 bool fHitASIDLimit = false;
1194 if (pCpu->uCurrentAsid >= pVM->hm.s.uMaxAsid)
1195 {
1196 pCpu->uCurrentAsid = 1; /* Wraparound at 1; host uses 0 */
1197 pCpu->cTlbFlushes++; /* All VCPUs that run on this host CPU must use a new ASID. */
1198 fHitASIDLimit = true;
1199 }
1200
1201 if ( fHitASIDLimit
1202 || pCpu->fFlushAsidBeforeUse)
1203 {
1204 pVmcb->ctrl.TLBCtrl.n.u8TLBFlush = SVM_TLB_FLUSH_ENTIRE;
1205 pCpu->fFlushAsidBeforeUse = false;
1206 }
1207
1208 pVCpu->hm.s.uCurrentAsid = pCpu->uCurrentAsid;
1209 pVCpu->hm.s.idLastCpu = pCpu->idCpu;
1210 pVCpu->hm.s.cTlbFlushes = pCpu->cTlbFlushes;
1211 }
1212 else
1213 {
1214 if (pVM->hm.s.svm.u32Features & X86_CPUID_SVM_FEATURE_EDX_FLUSH_BY_ASID)
1215 pVmcb->ctrl.TLBCtrl.n.u8TLBFlush = SVM_TLB_FLUSH_SINGLE_CONTEXT;
1216 else
1217 pVmcb->ctrl.TLBCtrl.n.u8TLBFlush = SVM_TLB_FLUSH_ENTIRE;
1218 }
1219
1220 pVCpu->hm.s.fForceTLBFlush = false;
1221 }
1222 }
1223
1224 /* Update VMCB with the ASID. */
1225 if (pVmcb->ctrl.TLBCtrl.n.u32ASID != pVCpu->hm.s.uCurrentAsid)
1226 {
1227 pVmcb->ctrl.TLBCtrl.n.u32ASID = pVCpu->hm.s.uCurrentAsid;
1228 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_ASID;
1229 }
1230
1231 AssertMsg(pVCpu->hm.s.idLastCpu == pCpu->idCpu,
1232 ("vcpu idLastCpu=%u pcpu idCpu=%u\n", pVCpu->hm.s.idLastCpu, pCpu->idCpu));
1233 AssertMsg(pVCpu->hm.s.cTlbFlushes == pCpu->cTlbFlushes,
1234 ("Flush count mismatch for cpu %u (%u vs %u)\n", pCpu->idCpu, pVCpu->hm.s.cTlbFlushes, pCpu->cTlbFlushes));
1235 AssertMsg(pCpu->uCurrentAsid >= 1 && pCpu->uCurrentAsid < pVM->hm.s.uMaxAsid,
1236 ("cpu%d uCurrentAsid = %x\n", pCpu->idCpu, pCpu->uCurrentAsid));
1237 AssertMsg(pVCpu->hm.s.uCurrentAsid >= 1 && pVCpu->hm.s.uCurrentAsid < pVM->hm.s.uMaxAsid,
1238 ("cpu%d VM uCurrentAsid = %x\n", pCpu->idCpu, pVCpu->hm.s.uCurrentAsid));
1239
1240#ifdef VBOX_WITH_STATISTICS
1241 if (pVmcb->ctrl.TLBCtrl.n.u8TLBFlush == SVM_TLB_FLUSH_NOTHING)
1242 STAM_COUNTER_INC(&pVCpu->hm.s.StatNoFlushTlbWorldSwitch);
1243 else if ( pVmcb->ctrl.TLBCtrl.n.u8TLBFlush == SVM_TLB_FLUSH_SINGLE_CONTEXT
1244 || pVmcb->ctrl.TLBCtrl.n.u8TLBFlush == SVM_TLB_FLUSH_SINGLE_CONTEXT_RETAIN_GLOBALS)
1245 {
1246 STAM_COUNTER_INC(&pVCpu->hm.s.StatFlushAsid);
1247 }
1248 else
1249 {
1250 Assert(pVmcb->ctrl.TLBCtrl.n.u8TLBFlush == SVM_TLB_FLUSH_ENTIRE);
1251 STAM_COUNTER_INC(&pVCpu->hm.s.StatFlushEntire);
1252 }
1253#endif
1254}
1255
1256
1257/** @name 64-bit guest on 32-bit host OS helper functions.
1258 *
1259 * The host CPU is still 64-bit capable but the host OS is running in 32-bit
1260 * mode (code segment, paging). These wrappers/helpers perform the necessary
1261 * bits for the 32->64 switcher.
1262 *
1263 * @{ */
1264#if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS)
1265/**
1266 * Prepares for and executes VMRUN (64-bit guests on a 32-bit host).
1267 *
1268 * @returns VBox status code.
1269 * @param HCPhysVmcbHost Physical address of host VMCB.
1270 * @param HCPhysVmcb Physical address of the VMCB.
1271 * @param pCtx Pointer to the guest-CPU context.
1272 * @param pVM The cross context VM structure.
1273 * @param pVCpu The cross context virtual CPU structure.
1274 */
1275DECLASM(int) SVMR0VMSwitcherRun64(RTHCPHYS HCPhysVmcbHost, RTHCPHYS HCPhysVmcb, PCPUMCTX pCtx, PVM pVM, PVMCPU pVCpu)
1276{
1277 uint32_t aParam[8];
1278 aParam[0] = RT_LO_U32(HCPhysVmcbHost); /* Param 1: HCPhysVmcbHost - Lo. */
1279 aParam[1] = RT_HI_U32(HCPhysVmcbHost); /* Param 1: HCPhysVmcbHost - Hi. */
1280 aParam[2] = RT_LO_U32(HCPhysVmcb); /* Param 2: HCPhysVmcb - Lo. */
1281 aParam[3] = RT_HI_U32(HCPhysVmcb); /* Param 2: HCPhysVmcb - Hi. */
1282 aParam[4] = VM_RC_ADDR(pVM, pVM);
1283 aParam[5] = 0;
1284 aParam[6] = VM_RC_ADDR(pVM, pVCpu);
1285 aParam[7] = 0;
1286
1287 return SVMR0Execute64BitsHandler(pVM, pVCpu, pCtx, HM64ON32OP_SVMRCVMRun64, RT_ELEMENTS(aParam), &aParam[0]);
1288}
1289
1290
1291/**
1292 * Executes the specified VMRUN handler in 64-bit mode.
1293 *
1294 * @returns VBox status code.
1295 * @param pVM The cross context VM structure.
1296 * @param pVCpu The cross context virtual CPU structure.
1297 * @param pCtx Pointer to the guest-CPU context.
1298 * @param enmOp The operation to perform.
1299 * @param cParams Number of parameters.
1300 * @param paParam Array of 32-bit parameters.
1301 */
1302VMMR0DECL(int) SVMR0Execute64BitsHandler(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, HM64ON32OP enmOp,
1303 uint32_t cParams, uint32_t *paParam)
1304{
1305 AssertReturn(pVM->hm.s.pfnHost32ToGuest64R0, VERR_HM_NO_32_TO_64_SWITCHER);
1306 Assert(enmOp > HM64ON32OP_INVALID && enmOp < HM64ON32OP_END);
1307
1308 NOREF(pCtx);
1309
1310 /* Disable interrupts. */
1311 RTHCUINTREG uOldEFlags = ASMIntDisableFlags();
1312
1313#ifdef VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
1314 RTCPUID idHostCpu = RTMpCpuId();
1315 CPUMR0SetLApic(pVCpu, idHostCpu);
1316#endif
1317
1318 CPUMSetHyperESP(pVCpu, VMMGetStackRC(pVCpu));
1319 CPUMSetHyperEIP(pVCpu, enmOp);
1320 for (int i = (int)cParams - 1; i >= 0; i--)
1321 CPUMPushHyper(pVCpu, paParam[i]);
1322
1323 STAM_PROFILE_ADV_START(&pVCpu->hm.s.StatWorldSwitch3264, z);
1324 /* Call the switcher. */
1325 int rc = pVM->hm.s.pfnHost32ToGuest64R0(pVM, RT_OFFSETOF(VM, aCpus[pVCpu->idCpu].cpum) - RT_OFFSETOF(VM, cpum));
1326 STAM_PROFILE_ADV_STOP(&pVCpu->hm.s.StatWorldSwitch3264, z);
1327
1328 /* Restore interrupts. */
1329 ASMSetFlags(uOldEFlags);
1330 return rc;
1331}
1332
1333#endif /* HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS) */
1334/** @} */
1335
1336
1337/**
1338 * Adds an exception to the intercept exception bitmap in the VMCB and updates
1339 * the corresponding VMCB Clean bit.
1340 *
1341 * @param pVmcb Pointer to the VM control block.
1342 * @param u32Xcpt The value of the exception (X86_XCPT_*).
1343 */
1344DECLINLINE(void) hmR0SvmAddXcptIntercept(PSVMVMCB pVmcb, uint32_t u32Xcpt)
1345{
1346 if (!(pVmcb->ctrl.u32InterceptXcpt & RT_BIT(u32Xcpt)))
1347 {
1348 pVmcb->ctrl.u32InterceptXcpt |= RT_BIT(u32Xcpt);
1349 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_INTERCEPTS;
1350 }
1351}
1352
1353
1354/**
1355 * Removes an exception from the intercept-exception bitmap in the VMCB and
1356 * updates the corresponding VMCB Clean bit.
1357 *
1358 * @param pVCpu The cross context virtual CPU structure.
1359 * @param pCtx Pointer to the guest-CPU context.
1360 * @param pVmcb Pointer to the VM control block.
1361 * @param u32Xcpt The value of the exception (X86_XCPT_*).
1362 *
1363 * @remarks This takes into account if we're executing a nested-guest and only
1364 * removes the exception intercept if both the guest -and- nested-guest
1365 * are not intercepting it.
1366 */
1367DECLINLINE(void) hmR0SvmRemoveXcptIntercept(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMVMCB pVmcb, uint32_t u32Xcpt)
1368{
1369 Assert(u32Xcpt != X86_XCPT_DB);
1370 Assert(u32Xcpt != X86_XCPT_AC);
1371#ifndef HMSVM_ALWAYS_TRAP_ALL_XCPTS
1372 if (pVmcb->ctrl.u32InterceptXcpt & RT_BIT(u32Xcpt))
1373 {
1374 bool fRemoveXcpt = true;
1375#ifdef VBOX_WITH_NESTED_HWVIRT
1376 /* Only remove the intercept if the nested-guest is also not intercepting it! */
1377 if (CPUMIsGuestInSvmNestedHwVirtMode(pCtx))
1378 {
1379 Assert(pCtx->hwvirt.svm.fHMCachedVmcb); NOREF(pCtx);
1380 PCSVMNESTEDVMCBCACHE pVmcbNstGstCache = &pVCpu->hm.s.svm.NstGstVmcbCache;
1381 fRemoveXcpt = !(pVmcbNstGstCache->u32InterceptXcpt & RT_BIT(u32Xcpt));
1382 }
1383#else
1384 RT_NOREF2(pVCpu, pCtx);
1385#endif
1386 if (fRemoveXcpt)
1387 {
1388 pVmcb->ctrl.u32InterceptXcpt &= ~RT_BIT(u32Xcpt);
1389 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_INTERCEPTS;
1390 }
1391 }
1392#else
1393 RT_NOREF3(pVCpu, pCtx, pVmcb);
1394#endif
1395}
1396
1397
1398/**
1399 * Loads the guest (or nested-guest) CR0 control register into the guest-state
1400 * area in the VMCB.
1401 *
1402 * Although the guest CR0 is a separate field in the VMCB we have to consider
1403 * the FPU state itself which is shared between the host and the guest.
1404 *
1405 * @returns VBox status code.
1406 * @param pVCpu The cross context virtual CPU structure.
1407 * @param pVmcb Pointer to the VM control block.
1408 * @param pCtx Pointer to the guest-CPU context.
1409 *
1410 * @remarks No-long-jump zone!!!
1411 */
1412static void hmR0SvmLoadSharedCR0(PVMCPU pVCpu, PSVMVMCB pVmcb, PCPUMCTX pCtx)
1413{
1414 uint64_t u64GuestCR0 = pCtx->cr0;
1415
1416 /* Always enable caching. */
1417 u64GuestCR0 &= ~(X86_CR0_CD | X86_CR0_NW);
1418
1419 /*
1420 * When Nested Paging is not available use shadow page tables and intercept #PFs (the latter done in SVMR0SetupVM()).
1421 */
1422 if (!pVCpu->CTX_SUFF(pVM)->hm.s.fNestedPaging)
1423 {
1424 u64GuestCR0 |= X86_CR0_PG; /* When Nested Paging is not available, use shadow page tables. */
1425 u64GuestCR0 |= X86_CR0_WP; /* Guest CPL 0 writes to its read-only pages should cause a #PF #VMEXIT. */
1426 }
1427
1428 /*
1429 * Guest FPU bits.
1430 */
1431 bool fInterceptNM = false;
1432 bool fInterceptMF = false;
1433 u64GuestCR0 |= X86_CR0_NE; /* Use internal x87 FPU exceptions handling rather than external interrupts. */
1434 if (CPUMIsGuestFPUStateActive(pVCpu))
1435 {
1436 /* Catch floating point exceptions if we need to report them to the guest in a different way. */
1437 if (!(pCtx->cr0 & X86_CR0_NE))
1438 {
1439 Log4(("hmR0SvmLoadSharedCR0: Intercepting Guest CR0.MP Old-style FPU handling!!!\n"));
1440 fInterceptMF = true;
1441 }
1442 }
1443 else
1444 {
1445 fInterceptNM = true; /* Guest FPU inactive, #VMEXIT on #NM for lazy FPU loading. */
1446 u64GuestCR0 |= X86_CR0_TS /* Guest can task switch quickly and do lazy FPU syncing. */
1447 | X86_CR0_MP; /* FWAIT/WAIT should not ignore CR0.TS and should generate #NM. */
1448 }
1449
1450 /*
1451 * Update the exception intercept bitmap.
1452 */
1453 if (fInterceptNM)
1454 hmR0SvmAddXcptIntercept(pVmcb, X86_XCPT_NM);
1455 else
1456 hmR0SvmRemoveXcptIntercept(pVCpu, pCtx, pVmcb, X86_XCPT_NM);
1457
1458 if (fInterceptMF)
1459 hmR0SvmAddXcptIntercept(pVmcb, X86_XCPT_MF);
1460 else
1461 hmR0SvmRemoveXcptIntercept(pVCpu, pCtx, pVmcb, X86_XCPT_MF);
1462
1463 pVmcb->guest.u64CR0 = u64GuestCR0;
1464 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_CRX_EFER;
1465}
1466
1467
1468/**
1469 * Loads the guest/nested-guest control registers (CR2, CR3, CR4) into the VMCB.
1470 *
1471 * @returns VBox status code.
1472 * @param pVCpu The cross context virtual CPU structure.
1473 * @param pVmcb Pointer to the VM control block.
1474 * @param pCtx Pointer to the guest-CPU context.
1475 *
1476 * @remarks No-long-jump zone!!!
1477 */
1478static int hmR0SvmLoadGuestControlRegs(PVMCPU pVCpu, PSVMVMCB pVmcb, PCPUMCTX pCtx)
1479{
1480 PVM pVM = pVCpu->CTX_SUFF(pVM);
1481
1482 /*
1483 * Guest CR2.
1484 */
1485 if (HMCPU_CF_IS_PENDING(pVCpu, HM_CHANGED_GUEST_CR2))
1486 {
1487 pVmcb->guest.u64CR2 = pCtx->cr2;
1488 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_CR2;
1489 HMCPU_CF_CLEAR(pVCpu, HM_CHANGED_GUEST_CR2);
1490 }
1491
1492 /*
1493 * Guest CR3.
1494 */
1495 if (HMCPU_CF_IS_PENDING(pVCpu, HM_CHANGED_GUEST_CR3))
1496 {
1497 if (pVM->hm.s.fNestedPaging)
1498 {
1499 PGMMODE enmShwPagingMode;
1500#if HC_ARCH_BITS == 32
1501 if (CPUMIsGuestInLongModeEx(pCtx))
1502 enmShwPagingMode = PGMMODE_AMD64_NX;
1503 else
1504#endif
1505 enmShwPagingMode = PGMGetHostMode(pVM);
1506
1507 pVmcb->ctrl.u64NestedPagingCR3 = PGMGetNestedCR3(pVCpu, enmShwPagingMode);
1508 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_NP;
1509 Assert(pVmcb->ctrl.u64NestedPagingCR3);
1510 pVmcb->guest.u64CR3 = pCtx->cr3;
1511 }
1512 else
1513 {
1514 pVmcb->guest.u64CR3 = PGMGetHyperCR3(pVCpu);
1515 Log4(("hmR0SvmLoadGuestControlRegs: CR3=%#RX64 (HyperCR3=%#RX64)\n", pCtx->cr3, pVmcb->guest.u64CR3));
1516 }
1517
1518 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_CRX_EFER;
1519 HMCPU_CF_CLEAR(pVCpu, HM_CHANGED_GUEST_CR3);
1520 }
1521
1522 /*
1523 * Guest CR4.
1524 * ASSUMES this is done everytime we get in from ring-3! (XCR0)
1525 */
1526 if (HMCPU_CF_IS_PENDING(pVCpu, HM_CHANGED_GUEST_CR4))
1527 {
1528 uint64_t u64GuestCR4 = pCtx->cr4;
1529 Assert(RT_HI_U32(u64GuestCR4) == 0);
1530 if (!pVM->hm.s.fNestedPaging)
1531 {
1532 switch (pVCpu->hm.s.enmShadowMode)
1533 {
1534 case PGMMODE_REAL:
1535 case PGMMODE_PROTECTED: /* Protected mode, no paging. */
1536 AssertFailed();
1537 return VERR_PGM_UNSUPPORTED_SHADOW_PAGING_MODE;
1538
1539 case PGMMODE_32_BIT: /* 32-bit paging. */
1540 u64GuestCR4 &= ~X86_CR4_PAE;
1541 break;
1542
1543 case PGMMODE_PAE: /* PAE paging. */
1544 case PGMMODE_PAE_NX: /* PAE paging with NX enabled. */
1545 /** Must use PAE paging as we could use physical memory > 4 GB */
1546 u64GuestCR4 |= X86_CR4_PAE;
1547 break;
1548
1549 case PGMMODE_AMD64: /* 64-bit AMD paging (long mode). */
1550 case PGMMODE_AMD64_NX: /* 64-bit AMD paging (long mode) with NX enabled. */
1551#ifdef VBOX_ENABLE_64_BITS_GUESTS
1552 break;
1553#else
1554 AssertFailed();
1555 return VERR_PGM_UNSUPPORTED_SHADOW_PAGING_MODE;
1556#endif
1557
1558 default: /* shut up gcc */
1559 AssertFailed();
1560 return VERR_PGM_UNSUPPORTED_SHADOW_PAGING_MODE;
1561 }
1562 }
1563
1564 pVmcb->guest.u64CR4 = u64GuestCR4;
1565 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_CRX_EFER;
1566
1567 /* Whether to save/load/restore XCR0 during world switch depends on CR4.OSXSAVE and host+guest XCR0. */
1568 pVCpu->hm.s.fLoadSaveGuestXcr0 = (u64GuestCR4 & X86_CR4_OSXSAVE) && pCtx->aXcr[0] != ASMGetXcr0();
1569
1570 HMCPU_CF_CLEAR(pVCpu, HM_CHANGED_GUEST_CR4);
1571 }
1572
1573 return VINF_SUCCESS;
1574}
1575
1576
1577/**
1578 * Loads the guest (or nested-guest) segment registers into the VMCB.
1579 *
1580 * @returns VBox status code.
1581 * @param pVCpu The cross context virtual CPU structure.
1582 * @param pVmcb Pointer to the VM control block.
1583 * @param pCtx Pointer to the guest-CPU context.
1584 *
1585 * @remarks No-long-jump zone!!!
1586 */
1587static void hmR0SvmLoadGuestSegmentRegs(PVMCPU pVCpu, PSVMVMCB pVmcb, PCPUMCTX pCtx)
1588{
1589 /* Guest Segment registers: CS, SS, DS, ES, FS, GS. */
1590 if (HMCPU_CF_IS_PENDING(pVCpu, HM_CHANGED_GUEST_SEGMENT_REGS))
1591 {
1592 HMSVM_SEG_REG_COPY_TO_VMCB(pCtx, &pVmcb->guest, CS, cs);
1593 HMSVM_SEG_REG_COPY_TO_VMCB(pCtx, &pVmcb->guest, SS, ss);
1594 HMSVM_SEG_REG_COPY_TO_VMCB(pCtx, &pVmcb->guest, DS, ds);
1595 HMSVM_SEG_REG_COPY_TO_VMCB(pCtx, &pVmcb->guest, ES, es);
1596 HMSVM_SEG_REG_COPY_TO_VMCB(pCtx, &pVmcb->guest, FS, fs);
1597 HMSVM_SEG_REG_COPY_TO_VMCB(pCtx, &pVmcb->guest, GS, gs);
1598
1599 pVmcb->guest.u8CPL = pCtx->ss.Attr.n.u2Dpl;
1600 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_SEG;
1601 HMCPU_CF_CLEAR(pVCpu, HM_CHANGED_GUEST_SEGMENT_REGS);
1602 }
1603
1604 /* Guest TR. */
1605 if (HMCPU_CF_IS_PENDING(pVCpu, HM_CHANGED_GUEST_TR))
1606 {
1607 HMSVM_SEG_REG_COPY_TO_VMCB(pCtx, &pVmcb->guest, TR, tr);
1608 HMCPU_CF_CLEAR(pVCpu, HM_CHANGED_GUEST_TR);
1609 }
1610
1611 /* Guest LDTR. */
1612 if (HMCPU_CF_IS_PENDING(pVCpu, HM_CHANGED_GUEST_LDTR))
1613 {
1614 HMSVM_SEG_REG_COPY_TO_VMCB(pCtx, &pVmcb->guest, LDTR, ldtr);
1615 HMCPU_CF_CLEAR(pVCpu, HM_CHANGED_GUEST_LDTR);
1616 }
1617
1618 /* Guest GDTR. */
1619 if (HMCPU_CF_IS_PENDING(pVCpu, HM_CHANGED_GUEST_GDTR))
1620 {
1621 pVmcb->guest.GDTR.u32Limit = pCtx->gdtr.cbGdt;
1622 pVmcb->guest.GDTR.u64Base = pCtx->gdtr.pGdt;
1623 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_DT;
1624 HMCPU_CF_CLEAR(pVCpu, HM_CHANGED_GUEST_GDTR);
1625 }
1626
1627 /* Guest IDTR. */
1628 if (HMCPU_CF_IS_PENDING(pVCpu, HM_CHANGED_GUEST_IDTR))
1629 {
1630 pVmcb->guest.IDTR.u32Limit = pCtx->idtr.cbIdt;
1631 pVmcb->guest.IDTR.u64Base = pCtx->idtr.pIdt;
1632 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_DT;
1633 HMCPU_CF_CLEAR(pVCpu, HM_CHANGED_GUEST_IDTR);
1634 }
1635}
1636
1637
1638/**
1639 * Loads the guest (or nested-guest) MSRs into the VMCB.
1640 *
1641 * @param pVCpu The cross context virtual CPU structure.
1642 * @param pVmcb Pointer to the VM control block.
1643 * @param pCtx Pointer to the guest-CPU context.
1644 *
1645 * @remarks No-long-jump zone!!!
1646 */
1647static void hmR0SvmLoadGuestMsrs(PVMCPU pVCpu, PSVMVMCB pVmcb, PCPUMCTX pCtx)
1648{
1649 /* Guest Sysenter MSRs. */
1650 pVmcb->guest.u64SysEnterCS = pCtx->SysEnter.cs;
1651 pVmcb->guest.u64SysEnterEIP = pCtx->SysEnter.eip;
1652 pVmcb->guest.u64SysEnterESP = pCtx->SysEnter.esp;
1653
1654 /*
1655 * Guest EFER MSR.
1656 * AMD-V requires guest EFER.SVME to be set. Weird.
1657 * See AMD spec. 15.5.1 "Basic Operation" | "Canonicalization and Consistency Checks".
1658 */
1659 if (HMCPU_CF_IS_PENDING(pVCpu, HM_CHANGED_GUEST_EFER_MSR))
1660 {
1661 pVmcb->guest.u64EFER = pCtx->msrEFER | MSR_K6_EFER_SVME;
1662 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_CRX_EFER;
1663 HMCPU_CF_CLEAR(pVCpu, HM_CHANGED_GUEST_EFER_MSR);
1664 }
1665
1666 /* 64-bit MSRs. */
1667 if (CPUMIsGuestInLongModeEx(pCtx))
1668 {
1669 /* Load these always as the guest may modify FS/GS base using MSRs in 64-bit mode which we don't intercept. */
1670 pVmcb->guest.FS.u64Base = pCtx->fs.u64Base;
1671 pVmcb->guest.GS.u64Base = pCtx->gs.u64Base;
1672 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_SEG;
1673 }
1674 else
1675 {
1676 /* If the guest isn't in 64-bit mode, clear MSR_K6_LME bit from guest EFER otherwise AMD-V expects amd64 shadow paging. */
1677 if (pCtx->msrEFER & MSR_K6_EFER_LME)
1678 {
1679 pVmcb->guest.u64EFER &= ~MSR_K6_EFER_LME;
1680 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_CRX_EFER;
1681 }
1682 }
1683
1684 /** @todo The following are used in 64-bit only (SYSCALL/SYSRET) but they might
1685 * be writable in 32-bit mode. Clarify with AMD spec. */
1686 pVmcb->guest.u64STAR = pCtx->msrSTAR;
1687 pVmcb->guest.u64LSTAR = pCtx->msrLSTAR;
1688 pVmcb->guest.u64CSTAR = pCtx->msrCSTAR;
1689 pVmcb->guest.u64SFMASK = pCtx->msrSFMASK;
1690 pVmcb->guest.u64KernelGSBase = pCtx->msrKERNELGSBASE;
1691}
1692
1693
1694/**
1695 * Loads the guest (or nested-guest) debug state into the VMCB and programs the
1696 * necessary intercepts accordingly.
1697 *
1698 * @param pVCpu The cross context virtual CPU structure.
1699 * @param pVmcb Pointer to the VM control block.
1700 * @param pCtx Pointer to the guest-CPU context.
1701 *
1702 * @remarks No-long-jump zone!!!
1703 * @remarks Requires EFLAGS to be up-to-date in the VMCB!
1704 */
1705static void hmR0SvmLoadSharedDebugState(PVMCPU pVCpu, PSVMVMCB pVmcb, PCPUMCTX pCtx)
1706{
1707 bool fInterceptMovDRx = false;
1708
1709 /*
1710 * Anyone single stepping on the host side? If so, we'll have to use the
1711 * trap flag in the guest EFLAGS since AMD-V doesn't have a trap flag on
1712 * the VMM level like the VT-x implementations does.
1713 */
1714 bool const fStepping = pVCpu->hm.s.fSingleInstruction || DBGFIsStepping(pVCpu);
1715 if (fStepping)
1716 {
1717 pVCpu->hm.s.fClearTrapFlag = true;
1718 pVmcb->guest.u64RFlags |= X86_EFL_TF;
1719 fInterceptMovDRx = true; /* Need clean DR6, no guest mess. */
1720 }
1721
1722 if ( fStepping
1723 || (CPUMGetHyperDR7(pVCpu) & X86_DR7_ENABLED_MASK))
1724 {
1725 /*
1726 * Use the combined guest and host DRx values found in the hypervisor
1727 * register set because the debugger has breakpoints active or someone
1728 * is single stepping on the host side.
1729 *
1730 * Note! DBGF expects a clean DR6 state before executing guest code.
1731 */
1732#if HC_ARCH_BITS == 32 && defined(VBOX_WITH_64_BITS_GUESTS)
1733 if ( CPUMIsGuestInLongModeEx(pCtx)
1734 && !CPUMIsHyperDebugStateActivePending(pVCpu))
1735 {
1736 CPUMR0LoadHyperDebugState(pVCpu, false /* include DR6 */);
1737 Assert(!CPUMIsGuestDebugStateActivePending(pVCpu));
1738 Assert(CPUMIsHyperDebugStateActivePending(pVCpu));
1739 }
1740 else
1741#endif
1742 if (!CPUMIsHyperDebugStateActive(pVCpu))
1743 {
1744 CPUMR0LoadHyperDebugState(pVCpu, false /* include DR6 */);
1745 Assert(!CPUMIsGuestDebugStateActive(pVCpu));
1746 Assert(CPUMIsHyperDebugStateActive(pVCpu));
1747 }
1748
1749 /* Update DR6 & DR7. (The other DRx values are handled by CPUM one way or the other.) */
1750 if ( pVmcb->guest.u64DR6 != X86_DR6_INIT_VAL
1751 || pVmcb->guest.u64DR7 != CPUMGetHyperDR7(pVCpu))
1752 {
1753 pVmcb->guest.u64DR7 = CPUMGetHyperDR7(pVCpu);
1754 pVmcb->guest.u64DR6 = X86_DR6_INIT_VAL;
1755 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_DRX;
1756 pVCpu->hm.s.fUsingHyperDR7 = true;
1757 }
1758
1759 /** @todo If we cared, we could optimize to allow the guest to read registers
1760 * with the same values. */
1761 fInterceptMovDRx = true;
1762 Log5(("hmR0SvmLoadSharedDebugState: Loaded hyper DRx\n"));
1763 }
1764 else
1765 {
1766 /*
1767 * Update DR6, DR7 with the guest values if necessary.
1768 */
1769 if ( pVmcb->guest.u64DR7 != pCtx->dr[7]
1770 || pVmcb->guest.u64DR6 != pCtx->dr[6])
1771 {
1772 pVmcb->guest.u64DR7 = pCtx->dr[7];
1773 pVmcb->guest.u64DR6 = pCtx->dr[6];
1774 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_DRX;
1775 pVCpu->hm.s.fUsingHyperDR7 = false;
1776 }
1777
1778 /*
1779 * If the guest has enabled debug registers, we need to load them prior to
1780 * executing guest code so they'll trigger at the right time.
1781 */
1782 if (pCtx->dr[7] & (X86_DR7_ENABLED_MASK | X86_DR7_GD)) /** @todo Why GD? */
1783 {
1784#if HC_ARCH_BITS == 32 && defined(VBOX_WITH_64_BITS_GUESTS)
1785 if ( CPUMIsGuestInLongModeEx(pCtx)
1786 && !CPUMIsGuestDebugStateActivePending(pVCpu))
1787 {
1788 CPUMR0LoadGuestDebugState(pVCpu, false /* include DR6 */);
1789 STAM_COUNTER_INC(&pVCpu->hm.s.StatDRxArmed);
1790 Assert(!CPUMIsHyperDebugStateActivePending(pVCpu));
1791 Assert(CPUMIsGuestDebugStateActivePending(pVCpu));
1792 }
1793 else
1794#endif
1795 if (!CPUMIsGuestDebugStateActive(pVCpu))
1796 {
1797 CPUMR0LoadGuestDebugState(pVCpu, false /* include DR6 */);
1798 STAM_COUNTER_INC(&pVCpu->hm.s.StatDRxArmed);
1799 Assert(!CPUMIsHyperDebugStateActive(pVCpu));
1800 Assert(CPUMIsGuestDebugStateActive(pVCpu));
1801 }
1802 Log5(("hmR0SvmLoadSharedDebugState: Loaded guest DRx\n"));
1803 }
1804 /*
1805 * If no debugging enabled, we'll lazy load DR0-3. We don't need to
1806 * intercept #DB as DR6 is updated in the VMCB.
1807 *
1808 * Note! If we cared and dared, we could skip intercepting \#DB here.
1809 * However, \#DB shouldn't be performance critical, so we'll play safe
1810 * and keep the code similar to the VT-x code and always intercept it.
1811 */
1812#if HC_ARCH_BITS == 32 && defined(VBOX_WITH_64_BITS_GUESTS)
1813 else if ( !CPUMIsGuestDebugStateActivePending(pVCpu)
1814 && !CPUMIsGuestDebugStateActive(pVCpu))
1815#else
1816 else if (!CPUMIsGuestDebugStateActive(pVCpu))
1817#endif
1818 {
1819 fInterceptMovDRx = true;
1820 }
1821 }
1822
1823 Assert(pVmcb->ctrl.u32InterceptXcpt & RT_BIT_32(X86_XCPT_DB));
1824 if (fInterceptMovDRx)
1825 {
1826 if ( pVmcb->ctrl.u16InterceptRdDRx != 0xffff
1827 || pVmcb->ctrl.u16InterceptWrDRx != 0xffff)
1828 {
1829 pVmcb->ctrl.u16InterceptRdDRx = 0xffff;
1830 pVmcb->ctrl.u16InterceptWrDRx = 0xffff;
1831 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_INTERCEPTS;
1832 }
1833 }
1834 else
1835 {
1836 if ( pVmcb->ctrl.u16InterceptRdDRx
1837 || pVmcb->ctrl.u16InterceptWrDRx)
1838 {
1839 pVmcb->ctrl.u16InterceptRdDRx = 0;
1840 pVmcb->ctrl.u16InterceptWrDRx = 0;
1841 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_INTERCEPTS;
1842 }
1843 }
1844 Log4(("hmR0SvmLoadSharedDebugState: DR6=%#RX64 DR7=%#RX64\n", pCtx->dr[6], pCtx->dr[7]));
1845}
1846
1847
1848#ifdef VBOX_WITH_NESTED_HWVIRT
1849/**
1850 * Loads the nested-guest APIC state (currently just the TPR).
1851 *
1852 * @param pVCpu The cross context virtual CPU structure.
1853 * @param pVmcbNstGst Pointer to the nested-guest VM control block.
1854 */
1855static void hmR0SvmLoadGuestApicStateNested(PVMCPU pVCpu, PSVMVMCB pVmcbNstGst)
1856{
1857 if (HMCPU_CF_IS_PENDING(pVCpu, HM_CHANGED_SVM_GUEST_APIC_STATE))
1858 {
1859 /* Always enable V_INTR_MASKING as we do not want to allow access to the physical APIC TPR. */
1860 pVmcbNstGst->ctrl.IntCtrl.n.u1VIntrMasking = 1;
1861 pVCpu->hm.s.svm.fSyncVTpr = false;
1862 pVmcbNstGst->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_TPR;
1863
1864 HMCPU_CF_CLEAR(pVCpu, HM_CHANGED_SVM_GUEST_APIC_STATE);
1865 }
1866}
1867#endif
1868
1869/**
1870 * Loads the guest APIC state (currently just the TPR).
1871 *
1872 * @returns VBox status code.
1873 * @param pVCpu The cross context virtual CPU structure.
1874 * @param pVmcb Pointer to the VM control block.
1875 * @param pCtx Pointer to the guest-CPU context.
1876 */
1877static int hmR0SvmLoadGuestApicState(PVMCPU pVCpu, PSVMVMCB pVmcb, PCPUMCTX pCtx)
1878{
1879 if (!HMCPU_CF_IS_PENDING(pVCpu, HM_CHANGED_SVM_GUEST_APIC_STATE))
1880 return VINF_SUCCESS;
1881
1882 int rc = VINF_SUCCESS;
1883 PVM pVM = pVCpu->CTX_SUFF(pVM);
1884 if ( PDMHasApic(pVM)
1885 && APICIsEnabled(pVCpu))
1886 {
1887 bool fPendingIntr;
1888 uint8_t u8Tpr;
1889 rc = APICGetTpr(pVCpu, &u8Tpr, &fPendingIntr, NULL /* pu8PendingIrq */);
1890 AssertRCReturn(rc, rc);
1891
1892 /* Assume that we need to trap all TPR accesses and thus need not check on
1893 every #VMEXIT if we should update the TPR. */
1894 Assert(pVmcb->ctrl.IntCtrl.n.u1VIntrMasking);
1895 pVCpu->hm.s.svm.fSyncVTpr = false;
1896
1897 /* 32-bit guests uses LSTAR MSR for patching guest code which touches the TPR. */
1898 if (pVM->hm.s.fTPRPatchingActive)
1899 {
1900 pCtx->msrLSTAR = u8Tpr;
1901 uint8_t *pbMsrBitmap = (uint8_t *)pVCpu->hm.s.svm.pvMsrBitmap;
1902
1903 /* If there are interrupts pending, intercept LSTAR writes, otherwise don't intercept reads or writes. */
1904 if (fPendingIntr)
1905 hmR0SvmSetMsrPermission(pVmcb, pbMsrBitmap, MSR_K8_LSTAR, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_INTERCEPT_WRITE);
1906 else
1907 {
1908 hmR0SvmSetMsrPermission(pVmcb, pbMsrBitmap, MSR_K8_LSTAR, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
1909 pVCpu->hm.s.svm.fSyncVTpr = true;
1910 }
1911 }
1912 else
1913 {
1914 /* Bits 3-0 of the VTPR field correspond to bits 7-4 of the TPR (which is the Task-Priority Class). */
1915 pVmcb->ctrl.IntCtrl.n.u8VTPR = (u8Tpr >> 4);
1916
1917 /* If there are interrupts pending, intercept CR8 writes to evaluate ASAP if we can deliver the interrupt to the guest. */
1918 if (fPendingIntr)
1919 pVmcb->ctrl.u16InterceptWrCRx |= RT_BIT(8);
1920 else
1921 {
1922 pVmcb->ctrl.u16InterceptWrCRx &= ~RT_BIT(8);
1923 pVCpu->hm.s.svm.fSyncVTpr = true;
1924 }
1925
1926 pVmcb->ctrl.u32VmcbCleanBits &= ~(HMSVM_VMCB_CLEAN_INTERCEPTS | HMSVM_VMCB_CLEAN_TPR);
1927 }
1928 }
1929
1930 HMCPU_CF_CLEAR(pVCpu, HM_CHANGED_SVM_GUEST_APIC_STATE);
1931 return rc;
1932}
1933
1934
1935/**
1936 * Loads the exception interrupts required for guest (or nested-guest) execution in
1937 * the VMCB.
1938 *
1939 * @param pVCpu The cross context virtual CPU structure.
1940 * @param pVmcb Pointer to the VM control block.
1941 * @param pCtx Pointer to the guest-CPU context.
1942 */
1943static void hmR0SvmLoadGuestXcptIntercepts(PVMCPU pVCpu, PSVMVMCB pVmcb, PCPUMCTX pCtx)
1944{
1945 if (HMCPU_CF_IS_PENDING(pVCpu, HM_CHANGED_GUEST_XCPT_INTERCEPTS))
1946 {
1947 /* Trap #UD for GIM provider (e.g. for hypercalls). */
1948 if (pVCpu->hm.s.fGIMTrapXcptUD)
1949 hmR0SvmAddXcptIntercept(pVmcb, X86_XCPT_UD);
1950 else
1951 hmR0SvmRemoveXcptIntercept(pVCpu, pCtx, pVmcb, X86_XCPT_UD);
1952
1953 /* Trap #BP for INT3 debug breakpoints set by the VM debugger. */
1954 if (pVCpu->CTX_SUFF(pVM)->dbgf.ro.cEnabledInt3Breakpoints)
1955 hmR0SvmAddXcptIntercept(pVmcb, X86_XCPT_BP);
1956 else
1957 hmR0SvmRemoveXcptIntercept(pVCpu, pCtx, pVmcb, X86_XCPT_BP);
1958
1959 /* The remaining intercepts are handled elsewhere, e.g. in hmR0SvmLoadSharedCR0(). */
1960 HMCPU_CF_CLEAR(pVCpu, HM_CHANGED_GUEST_XCPT_INTERCEPTS);
1961 }
1962}
1963
1964
1965#ifdef VBOX_WITH_NESTED_HWVIRT
1966/**
1967 * Loads the intercepts required for nested-guest execution in the VMCB.
1968 *
1969 * This merges the guest and nested-guest intercepts in a way that if the outer
1970 * guest intercepts an exception we need to intercept it in the nested-guest as
1971 * well and handle it accordingly.
1972 *
1973 * @param pVCpu The cross context virtual CPU structure.
1974 * @param pVmcbNstGst Pointer to the nested-guest VM control block.
1975 * @param pCtx Pointer to the guest-CPU context.
1976 */
1977static void hmR0SvmLoadGuestXcptInterceptsNested(PVMCPU pVCpu, PSVMVMCB pVmcbNstGst, PCPUMCTX pCtx)
1978{
1979 if (HMCPU_CF_IS_PENDING(pVCpu, HM_CHANGED_GUEST_XCPT_INTERCEPTS))
1980 {
1981 /* First, load the guest exception intercepts into the guest VMCB. */
1982 PSVMVMCB pVmcb = pVCpu->hm.s.svm.pVmcb;
1983 hmR0SvmLoadGuestXcptIntercepts(pVCpu, pVmcb, pCtx);
1984
1985 /* Next, merge the intercepts into the nested-guest VMCB. */
1986 pVmcbNstGst->ctrl.u16InterceptRdCRx |= pVmcb->ctrl.u16InterceptRdCRx;
1987 pVmcbNstGst->ctrl.u16InterceptWrCRx |= pVmcb->ctrl.u16InterceptWrCRx;
1988
1989 /* Always intercept CR0, CR4 reads and writes as we alter them. */
1990 pVmcbNstGst->ctrl.u16InterceptRdCRx |= RT_BIT(0) | RT_BIT(4);
1991 pVmcbNstGst->ctrl.u16InterceptWrCRx |= RT_BIT(0) | RT_BIT(4);
1992
1993 /* Always intercept CR3 reads and writes without nested-paging as we load shadow page tables. */
1994 if (!pVCpu->CTX_SUFF(pVM)->hm.s.fNestedPaging)
1995 {
1996 pVmcbNstGst->ctrl.u16InterceptRdCRx |= RT_BIT(3);
1997 pVmcbNstGst->ctrl.u16InterceptWrCRx |= RT_BIT(3);
1998 }
1999
2000 /** @todo Figure out debugging with nested-guests, till then just intercept
2001 * all DR[0-15] accesses. */
2002 pVmcbNstGst->ctrl.u16InterceptRdDRx |= 0xffff;
2003 pVmcbNstGst->ctrl.u16InterceptWrDRx |= 0xffff;
2004
2005 /* Exclude the VINTR intercept of the outer guest as we don't need to cause VINTR #VMEXITs
2006 that belong to the nested-guest to the outer guest. */
2007 pVmcbNstGst->ctrl.u32InterceptXcpt |= pVmcb->ctrl.u32InterceptXcpt;
2008 pVmcbNstGst->ctrl.u64InterceptCtrl |= (pVmcb->ctrl.u64InterceptCtrl & ~SVM_CTRL_INTERCEPT_VINTR)
2009 | HMSVM_MANDATORY_GUEST_CTRL_INTERCEPTS;
2010
2011 /*
2012 * Adjust control intercepts while executing the nested-guest that differ
2013 * from the outer guest intercepts.
2014 *
2015 * VMMCALL when not intercepted raises a \#UD exception in the guest. However,
2016 * other SVM instructions like VMSAVE when not intercept can cause havoc on the
2017 * host as they can write to any location in physical memory, hence they always
2018 * need to be intercepted (see below).
2019 */
2020 Assert( (pVmcbNstGst->ctrl.u64InterceptCtrl & HMSVM_MANDATORY_GUEST_CTRL_INTERCEPTS)
2021 == HMSVM_MANDATORY_GUEST_CTRL_INTERCEPTS);
2022 pVmcbNstGst->ctrl.u64InterceptCtrl &= ~SVM_CTRL_INTERCEPT_VMMCALL;
2023
2024 /*
2025 * If we don't expose Virtualized-VMSAVE/VMLOAD feature to the outer guest, we
2026 * need to intercept VMSAVE/VMLOAD instructions executed by the nested-guest.
2027 */
2028 if (!pVCpu->CTX_SUFF(pVM)->cpum.ro.GuestFeatures.fSvmVirtVmsaveVmload)
2029 {
2030 pVmcbNstGst->ctrl.u64InterceptCtrl |= SVM_CTRL_INTERCEPT_VMSAVE
2031 | SVM_CTRL_INTERCEPT_VMLOAD;
2032 }
2033
2034 /*
2035 * If we don't expose Virtual GIF feature to the outer guest, we need to intercept
2036 * CLGI/STGI instructions executed by the nested-guest.
2037 */
2038 if (!pVCpu->CTX_SUFF(pVM)->cpum.ro.GuestFeatures.fSvmVGif)
2039 {
2040 pVmcbNstGst->ctrl.u64InterceptCtrl |= SVM_CTRL_INTERCEPT_CLGI
2041 | SVM_CTRL_INTERCEPT_STGI;
2042 }
2043
2044 /* Finally, update the VMCB clean bits. */
2045 pVmcbNstGst->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_INTERCEPTS;
2046
2047 Assert(!HMCPU_CF_IS_PENDING(pVCpu, HM_CHANGED_GUEST_XCPT_INTERCEPTS));
2048 }
2049}
2050#endif
2051
2052
2053/**
2054 * Sets up the appropriate function to run guest code.
2055 *
2056 * @returns VBox status code.
2057 * @param pVCpu The cross context virtual CPU structure.
2058 *
2059 * @remarks No-long-jump zone!!!
2060 */
2061static int hmR0SvmSetupVMRunHandler(PVMCPU pVCpu)
2062{
2063 if (CPUMIsGuestInLongMode(pVCpu))
2064 {
2065#ifndef VBOX_ENABLE_64_BITS_GUESTS
2066 return VERR_PGM_UNSUPPORTED_SHADOW_PAGING_MODE;
2067#endif
2068 Assert(pVCpu->CTX_SUFF(pVM)->hm.s.fAllow64BitGuests); /* Guaranteed by hmR3InitFinalizeR0(). */
2069#if HC_ARCH_BITS == 32
2070 /* 32-bit host. We need to switch to 64-bit before running the 64-bit guest. */
2071 pVCpu->hm.s.svm.pfnVMRun = SVMR0VMSwitcherRun64;
2072#else
2073 /* 64-bit host or hybrid host. */
2074 pVCpu->hm.s.svm.pfnVMRun = SVMR0VMRun64;
2075#endif
2076 }
2077 else
2078 {
2079 /* Guest is not in long mode, use the 32-bit handler. */
2080 pVCpu->hm.s.svm.pfnVMRun = SVMR0VMRun;
2081 }
2082 return VINF_SUCCESS;
2083}
2084
2085
2086/**
2087 * Enters the AMD-V session.
2088 *
2089 * @returns VBox status code.
2090 * @param pVM The cross context VM structure.
2091 * @param pVCpu The cross context virtual CPU structure.
2092 * @param pCpu Pointer to the CPU info struct.
2093 */
2094VMMR0DECL(int) SVMR0Enter(PVM pVM, PVMCPU pVCpu, PHMGLOBALCPUINFO pCpu)
2095{
2096 AssertPtr(pVM);
2097 AssertPtr(pVCpu);
2098 Assert(pVM->hm.s.svm.fSupported);
2099 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
2100 NOREF(pVM); NOREF(pCpu);
2101
2102 LogFlowFunc(("pVM=%p pVCpu=%p\n", pVM, pVCpu));
2103 Assert(HMCPU_CF_IS_SET(pVCpu, HM_CHANGED_HOST_CONTEXT | HM_CHANGED_HOST_GUEST_SHARED_STATE));
2104
2105 pVCpu->hm.s.fLeaveDone = false;
2106 return VINF_SUCCESS;
2107}
2108
2109
2110/**
2111 * Thread-context callback for AMD-V.
2112 *
2113 * @param enmEvent The thread-context event.
2114 * @param pVCpu The cross context virtual CPU structure.
2115 * @param fGlobalInit Whether global VT-x/AMD-V init. is used.
2116 * @thread EMT(pVCpu)
2117 */
2118VMMR0DECL(void) SVMR0ThreadCtxCallback(RTTHREADCTXEVENT enmEvent, PVMCPU pVCpu, bool fGlobalInit)
2119{
2120 NOREF(fGlobalInit);
2121
2122 switch (enmEvent)
2123 {
2124 case RTTHREADCTXEVENT_OUT:
2125 {
2126 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
2127 Assert(VMMR0ThreadCtxHookIsEnabled(pVCpu));
2128 VMCPU_ASSERT_EMT(pVCpu);
2129
2130 /* No longjmps (log-flush, locks) in this fragile context. */
2131 VMMRZCallRing3Disable(pVCpu);
2132
2133 if (!pVCpu->hm.s.fLeaveDone)
2134 {
2135 hmR0SvmLeave(pVCpu);
2136 pVCpu->hm.s.fLeaveDone = true;
2137 }
2138
2139 /* Leave HM context, takes care of local init (term). */
2140 int rc = HMR0LeaveCpu(pVCpu);
2141 AssertRC(rc); NOREF(rc);
2142
2143 /* Restore longjmp state. */
2144 VMMRZCallRing3Enable(pVCpu);
2145 STAM_REL_COUNTER_INC(&pVCpu->hm.s.StatSwitchPreempt);
2146 break;
2147 }
2148
2149 case RTTHREADCTXEVENT_IN:
2150 {
2151 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
2152 Assert(VMMR0ThreadCtxHookIsEnabled(pVCpu));
2153 VMCPU_ASSERT_EMT(pVCpu);
2154
2155 /* No longjmps (log-flush, locks) in this fragile context. */
2156 VMMRZCallRing3Disable(pVCpu);
2157
2158 /*
2159 * Initialize the bare minimum state required for HM. This takes care of
2160 * initializing AMD-V if necessary (onlined CPUs, local init etc.)
2161 */
2162 int rc = HMR0EnterCpu(pVCpu);
2163 AssertRC(rc); NOREF(rc);
2164 Assert(HMCPU_CF_IS_SET(pVCpu, HM_CHANGED_HOST_CONTEXT | HM_CHANGED_HOST_GUEST_SHARED_STATE));
2165
2166 pVCpu->hm.s.fLeaveDone = false;
2167
2168 /* Restore longjmp state. */
2169 VMMRZCallRing3Enable(pVCpu);
2170 break;
2171 }
2172
2173 default:
2174 break;
2175 }
2176}
2177
2178
2179/**
2180 * Saves the host state.
2181 *
2182 * @returns VBox status code.
2183 * @param pVM The cross context VM structure.
2184 * @param pVCpu The cross context virtual CPU structure.
2185 *
2186 * @remarks No-long-jump zone!!!
2187 */
2188VMMR0DECL(int) SVMR0SaveHostState(PVM pVM, PVMCPU pVCpu)
2189{
2190 NOREF(pVM);
2191 NOREF(pVCpu);
2192 /* Nothing to do here. AMD-V does this for us automatically during the world-switch. */
2193 HMCPU_CF_CLEAR(pVCpu, HM_CHANGED_HOST_CONTEXT);
2194 return VINF_SUCCESS;
2195}
2196
2197
2198/**
2199 * Loads the guest state into the VMCB.
2200 *
2201 * The CPU state will be loaded from these fields on every successful VM-entry.
2202 * Also sets up the appropriate VMRUN function to execute guest code based on
2203 * the guest CPU mode.
2204 *
2205 * @returns VBox status code.
2206 * @param pVM The cross context VM structure.
2207 * @param pVCpu The cross context virtual CPU structure.
2208 * @param pCtx Pointer to the guest-CPU context.
2209 *
2210 * @remarks No-long-jump zone!!!
2211 */
2212static int hmR0SvmLoadGuestState(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
2213{
2214 HMSVM_ASSERT_NOT_IN_NESTED_GUEST(pCtx);
2215
2216 PSVMVMCB pVmcb = pVCpu->hm.s.svm.pVmcb;
2217 AssertMsgReturn(pVmcb, ("Invalid pVmcb\n"), VERR_SVM_INVALID_PVMCB);
2218
2219 STAM_PROFILE_ADV_START(&pVCpu->hm.s.StatLoadGuestState, x);
2220
2221 int rc = hmR0SvmLoadGuestControlRegs(pVCpu, pVmcb, pCtx);
2222 AssertLogRelMsgRCReturn(rc, ("hmR0SvmLoadGuestControlRegs! rc=%Rrc (pVM=%p pVCpu=%p)\n", rc, pVM, pVCpu), rc);
2223
2224 hmR0SvmLoadGuestSegmentRegs(pVCpu, pVmcb, pCtx);
2225 hmR0SvmLoadGuestMsrs(pVCpu, pVmcb, pCtx);
2226
2227 pVmcb->guest.u64RIP = pCtx->rip;
2228 pVmcb->guest.u64RSP = pCtx->rsp;
2229 pVmcb->guest.u64RFlags = pCtx->eflags.u32;
2230 pVmcb->guest.u64RAX = pCtx->rax;
2231
2232#ifdef VBOX_WITH_NESTED_HWVIRT
2233 if (pVmcb->ctrl.IntCtrl.n.u1VGifEnable == 1)
2234 {
2235 Assert(pVM->hm.s.svm.fVGif);
2236 pVmcb->ctrl.IntCtrl.n.u1VGif = pCtx->hwvirt.svm.fGif;
2237 }
2238#endif
2239
2240 rc = hmR0SvmLoadGuestApicState(pVCpu, pVmcb, pCtx);
2241 AssertLogRelMsgRCReturn(rc, ("hmR0SvmLoadGuestApicState! rc=%Rrc (pVM=%p pVCpu=%p)\n", rc, pVM, pVCpu), rc);
2242
2243 hmR0SvmLoadGuestXcptIntercepts(pVCpu, pVmcb, pCtx);
2244
2245 rc = hmR0SvmSetupVMRunHandler(pVCpu);
2246 AssertLogRelMsgRCReturn(rc, ("hmR0SvmSetupVMRunHandler! rc=%Rrc (pVM=%p pVCpu=%p)\n", rc, pVM, pVCpu), rc);
2247
2248 /* Clear any unused and reserved bits. */
2249 HMCPU_CF_CLEAR(pVCpu, HM_CHANGED_GUEST_RIP /* Unused (loaded unconditionally). */
2250 | HM_CHANGED_GUEST_RSP
2251 | HM_CHANGED_GUEST_RFLAGS
2252 | HM_CHANGED_GUEST_SYSENTER_CS_MSR
2253 | HM_CHANGED_GUEST_SYSENTER_EIP_MSR
2254 | HM_CHANGED_GUEST_SYSENTER_ESP_MSR
2255 | HM_CHANGED_GUEST_LAZY_MSRS /* Unused. */
2256 | HM_CHANGED_SVM_RESERVED1 /* Reserved. */
2257 | HM_CHANGED_SVM_RESERVED2
2258 | HM_CHANGED_SVM_RESERVED3
2259 | HM_CHANGED_SVM_RESERVED4);
2260
2261 /* All the guest state bits should be loaded except maybe the host context and/or shared host/guest bits. */
2262 AssertMsg( !HMCPU_CF_IS_PENDING(pVCpu, HM_CHANGED_ALL_GUEST)
2263 || HMCPU_CF_IS_PENDING_ONLY(pVCpu, HM_CHANGED_HOST_CONTEXT | HM_CHANGED_HOST_GUEST_SHARED_STATE),
2264 ("fContextUseFlags=%#RX32\n", HMCPU_CF_VALUE(pVCpu)));
2265
2266 Log4(("hmR0SvmLoadGuestState: CS:RIP=%04x:%RX64 EFL=%#x CR0=%#RX32 CR3=%#RX32 CR4=%#RX32 ESP=%#RX32 EBP=%#RX32\n",
2267 pCtx->cs.Sel, pCtx->rip, pCtx->eflags.u, pCtx->cr0, pCtx->cr3, pCtx->cr4, pCtx->esp, pCtx->ebp));
2268 Log4(("hmR0SvmLoadGuestState: SS={%04x base=%016RX64 limit=%08x flags=%08x}\n", pCtx->ss.Sel, pCtx->ss.u64Base,
2269 pCtx->ss.u32Limit, pCtx->ss.Attr.u));
2270 Log4(("hmR0SvmLoadGuestState: FS={%04x base=%016RX64 limit=%08x flags=%08x}\n", pCtx->fs.Sel, pCtx->fs.u64Base,
2271 pCtx->fs.u32Limit, pCtx->fs.Attr.u));
2272 STAM_PROFILE_ADV_STOP(&pVCpu->hm.s.StatLoadGuestState, x);
2273 return rc;
2274}
2275
2276
2277#ifdef VBOX_WITH_NESTED_HWVIRT
2278/**
2279 * Caches the nested-guest VMCB fields before we modify them for execution using
2280 * hardware-assisted SVM.
2281 *
2282 * @returns true if the VMCB was previously already cached, false otherwise.
2283 * @param pCtx Pointer to the guest-CPU context.
2284 *
2285 * @sa HMSvmNstGstVmExitNotify.
2286 */
2287static bool hmR0SvmVmRunCacheVmcb(PVMCPU pVCpu, PCPUMCTX pCtx)
2288{
2289 PSVMVMCB pVmcbNstGst = pCtx->hwvirt.svm.CTX_SUFF(pVmcb);
2290 PCSVMVMCBCTRL pVmcbNstGstCtrl = &pVmcbNstGst->ctrl;
2291 PCSVMVMCBSTATESAVE pVmcbNstGstState = &pVmcbNstGst->guest;
2292 PSVMNESTEDVMCBCACHE pNstGstVmcbCache = &pVCpu->hm.s.svm.NstGstVmcbCache;
2293
2294 /*
2295 * Cache the nested-guest programmed VMCB fields if we have not cached it yet.
2296 * Otherwise we risk re-caching the values we may have modified, see @bugref{7243#c44}.
2297 *
2298 * Nested-paging CR3 is not saved back into the VMCB on #VMEXIT, hence no need to
2299 * cache and restore it, see AMD spec. 15.25.4 "Nested Paging and VMRUN/#VMEXIT".
2300 */
2301 bool const fWasCached = pCtx->hwvirt.svm.fHMCachedVmcb;
2302 if (!fWasCached)
2303 {
2304 pNstGstVmcbCache->u16InterceptRdCRx = pVmcbNstGstCtrl->u16InterceptRdCRx;
2305 pNstGstVmcbCache->u16InterceptWrCRx = pVmcbNstGstCtrl->u16InterceptWrCRx;
2306 pNstGstVmcbCache->u16InterceptRdDRx = pVmcbNstGstCtrl->u16InterceptRdDRx;
2307 pNstGstVmcbCache->u16InterceptWrDRx = pVmcbNstGstCtrl->u16InterceptWrDRx;
2308 pNstGstVmcbCache->u32InterceptXcpt = pVmcbNstGstCtrl->u32InterceptXcpt;
2309 pNstGstVmcbCache->u64InterceptCtrl = pVmcbNstGstCtrl->u64InterceptCtrl;
2310 pNstGstVmcbCache->u64CR0 = pVmcbNstGstState->u64CR0;
2311 pNstGstVmcbCache->u64CR3 = pVmcbNstGstState->u64CR3;
2312 pNstGstVmcbCache->u64CR4 = pVmcbNstGstState->u64CR4;
2313 pNstGstVmcbCache->u64EFER = pVmcbNstGstState->u64EFER;
2314 pNstGstVmcbCache->u64DBGCTL = pVmcbNstGstState->u64DBGCTL;
2315 pNstGstVmcbCache->u64IOPMPhysAddr = pVmcbNstGstCtrl->u64IOPMPhysAddr;
2316 pNstGstVmcbCache->u64MSRPMPhysAddr = pVmcbNstGstCtrl->u64MSRPMPhysAddr;
2317 pNstGstVmcbCache->u64TSCOffset = pVmcbNstGstCtrl->u64TSCOffset;
2318 pNstGstVmcbCache->u32VmcbCleanBits = pVmcbNstGstCtrl->u32VmcbCleanBits;
2319 pNstGstVmcbCache->fVIntrMasking = pVmcbNstGstCtrl->IntCtrl.n.u1VIntrMasking;
2320 pNstGstVmcbCache->TLBCtrl = pVmcbNstGstCtrl->TLBCtrl;
2321 pNstGstVmcbCache->u1NestedPaging = pVmcbNstGstCtrl->NestedPaging.n.u1NestedPaging;
2322 pNstGstVmcbCache->u1LbrVirt = pVmcbNstGstCtrl->LbrVirt.n.u1LbrVirt;
2323 pCtx->hwvirt.svm.fHMCachedVmcb = true;
2324 Log4(("hmR0SvmVmRunCacheVmcb: Cached VMCB fields\n"));
2325 }
2326
2327 return fWasCached;
2328}
2329
2330
2331/**
2332 * Sets up the nested-guest VMCB for execution using hardware-assisted SVM.
2333 *
2334 * @param pVCpu The cross context virtual CPU structure.
2335 * @param pCtx Pointer to the guest-CPU context.
2336 */
2337static void hmR0SvmVmRunSetupVmcb(PVMCPU pVCpu, PCPUMCTX pCtx)
2338{
2339 PSVMVMCB pVmcbNstGst = pCtx->hwvirt.svm.CTX_SUFF(pVmcb);
2340 PSVMVMCBCTRL pVmcbNstGstCtrl = &pVmcbNstGst->ctrl;
2341
2342 /*
2343 * First cache the nested-guest VMCB fields we may potentially modify.
2344 */
2345 bool const fVmcbCached = hmR0SvmVmRunCacheVmcb(pVCpu, pCtx);
2346 if (!fVmcbCached)
2347 {
2348 /*
2349 * The IOPM of the nested-guest can be ignored because the the guest always
2350 * intercepts all IO port accesses. Thus, we'll swap to the guest IOPM rather
2351 * into the nested-guest one and swap it back on the #VMEXIT.
2352 */
2353 pVmcbNstGstCtrl->u64IOPMPhysAddr = g_HCPhysIOBitmap;
2354
2355 /*
2356 * Load the host-physical address into the MSRPM rather than the nested-guest
2357 * physical address (currently we trap all MSRs in the nested-guest).
2358 */
2359 pVmcbNstGstCtrl->u64MSRPMPhysAddr = g_HCPhysNstGstMsrBitmap;
2360
2361 /*
2362 * Use the same nested-paging as the "outer" guest. We can't dynamically
2363 * switch off nested-paging suddenly while executing a VM (see assertion at the
2364 * end of Trap0eHandler in PGMAllBth.h).
2365 */
2366 pVmcbNstGstCtrl->NestedPaging.n.u1NestedPaging = pVCpu->CTX_SUFF(pVM)->hm.s.fNestedPaging;
2367
2368 /* For now copy the LBR info. from outer guest VMCB. */
2369 /** @todo fix this later. */
2370 PCSVMVMCB pVmcb = pVCpu->hm.s.svm.pVmcb;
2371 pVmcbNstGstCtrl->LbrVirt.n.u1LbrVirt = pVmcb->ctrl.LbrVirt.n.u1LbrVirt;
2372 pVmcbNstGst->guest.u64DBGCTL = pVmcb->guest.u64DBGCTL;
2373 }
2374 else
2375 {
2376 Assert(pVmcbNstGstCtrl->u64IOPMPhysAddr == g_HCPhysIOBitmap);
2377 Assert(pVmcbNstGstCtrl->u64MSRPMPhysAddr = g_HCPhysNstGstMsrBitmap);
2378 Assert(RT_BOOL(pVmcbNstGstCtrl->NestedPaging.n.u1NestedPaging) == pVCpu->CTX_SUFF(pVM)->hm.s.fNestedPaging);
2379 }
2380}
2381
2382
2383/**
2384 * Loads the nested-guest state into the VMCB.
2385 *
2386 * @returns VBox status code.
2387 * @param pVCpu The cross context virtual CPU structure.
2388 * @param pCtx Pointer to the guest-CPU context.
2389 *
2390 * @remarks No-long-jump zone!!!
2391 */
2392static int hmR0SvmLoadGuestStateNested(PVMCPU pVCpu, PCPUMCTX pCtx)
2393{
2394 STAM_PROFILE_ADV_START(&pVCpu->hm.s.StatLoadGuestState, x);
2395
2396 PSVMVMCB pVmcbNstGst = pCtx->hwvirt.svm.CTX_SUFF(pVmcb);
2397 Assert(pVmcbNstGst);
2398
2399 hmR0SvmVmRunSetupVmcb(pVCpu, pCtx);
2400
2401 int rc = hmR0SvmLoadGuestControlRegs(pVCpu, pVmcbNstGst, pCtx);
2402 AssertRCReturn(rc, rc);
2403
2404 /*
2405 * We need to load the entire state (including FS, GS etc.) as we could be continuing
2406 * to execute the nested-guest at any point (not just immediately after VMRUN) and thus
2407 * the VMCB can possibly be out-of-sync with the actual nested-guest state if it was
2408 * executed in IEM.
2409 */
2410 hmR0SvmLoadGuestSegmentRegs(pVCpu, pVmcbNstGst, pCtx);
2411 hmR0SvmLoadGuestMsrs(pVCpu, pVmcbNstGst, pCtx);
2412 hmR0SvmLoadGuestApicStateNested(pVCpu, pVmcbNstGst);
2413
2414 pVmcbNstGst->guest.u64RIP = pCtx->rip;
2415 pVmcbNstGst->guest.u64RSP = pCtx->rsp;
2416 pVmcbNstGst->guest.u64RFlags = pCtx->eflags.u32;
2417 pVmcbNstGst->guest.u64RAX = pCtx->rax;
2418
2419#ifdef VBOX_WITH_NESTED_HWVIRT
2420 Assert(pVmcbNstGst->ctrl.IntCtrl.n.u1VGifEnable == 0); /* Nested VGIF not supported yet. */
2421#endif
2422
2423 hmR0SvmLoadGuestXcptInterceptsNested(pVCpu, pVmcbNstGst, pCtx);
2424
2425 rc = hmR0SvmSetupVMRunHandler(pVCpu);
2426 AssertRCReturn(rc, rc);
2427
2428 /* Clear any unused and reserved bits. */
2429 HMCPU_CF_CLEAR(pVCpu, HM_CHANGED_GUEST_RIP /* Unused (loaded unconditionally). */
2430 | HM_CHANGED_GUEST_RSP
2431 | HM_CHANGED_GUEST_RFLAGS
2432 | HM_CHANGED_GUEST_SYSENTER_CS_MSR
2433 | HM_CHANGED_GUEST_SYSENTER_EIP_MSR
2434 | HM_CHANGED_GUEST_SYSENTER_ESP_MSR
2435 | HM_CHANGED_GUEST_LAZY_MSRS /* Unused. */
2436 | HM_CHANGED_SVM_RESERVED1 /* Reserved. */
2437 | HM_CHANGED_SVM_RESERVED2
2438 | HM_CHANGED_SVM_RESERVED3
2439 | HM_CHANGED_SVM_RESERVED4);
2440
2441 /* All the guest state bits should be loaded except maybe the host context and/or shared host/guest bits. */
2442 AssertMsg( !HMCPU_CF_IS_PENDING(pVCpu, HM_CHANGED_ALL_GUEST)
2443 || HMCPU_CF_IS_PENDING_ONLY(pVCpu, HM_CHANGED_HOST_CONTEXT | HM_CHANGED_HOST_GUEST_SHARED_STATE),
2444 ("fContextUseFlags=%#RX32\n", HMCPU_CF_VALUE(pVCpu)));
2445
2446#ifdef VBOX_STRICT
2447 hmR0SvmLogState(pVCpu, pVmcbNstGst, pCtx, "hmR0SvmLoadGuestStateNested", HMSVM_LOG_ALL, 0 /* uVerbose */);
2448#endif
2449 STAM_PROFILE_ADV_STOP(&pVCpu->hm.s.StatLoadGuestState, x);
2450 return rc;
2451}
2452#endif
2453
2454
2455/**
2456 * Loads the state shared between the host and guest or nested-guest into the
2457 * VMCB.
2458 *
2459 * @param pVCpu The cross context virtual CPU structure.
2460 * @param pVmcb Pointer to the VM control block.
2461 * @param pCtx Pointer to the guest-CPU context.
2462 *
2463 * @remarks No-long-jump zone!!!
2464 */
2465static void hmR0SvmLoadSharedState(PVMCPU pVCpu, PSVMVMCB pVmcb, PCPUMCTX pCtx)
2466{
2467 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
2468 Assert(!VMMRZCallRing3IsEnabled(pVCpu));
2469
2470 if (HMCPU_CF_IS_PENDING(pVCpu, HM_CHANGED_GUEST_CR0))
2471 {
2472 hmR0SvmLoadSharedCR0(pVCpu, pVmcb, pCtx);
2473 HMCPU_CF_CLEAR(pVCpu, HM_CHANGED_GUEST_CR0);
2474 }
2475
2476 if (HMCPU_CF_IS_PENDING(pVCpu, HM_CHANGED_GUEST_DEBUG))
2477 {
2478 /** @todo Figure out stepping with nested-guest. */
2479 if (!CPUMIsGuestInSvmNestedHwVirtMode(pCtx))
2480 hmR0SvmLoadSharedDebugState(pVCpu, pVmcb, pCtx);
2481 else
2482 {
2483 pVmcb->guest.u64DR6 = pCtx->dr[6];
2484 pVmcb->guest.u64DR7 = pCtx->dr[7];
2485 Log4(("hmR0SvmLoadSharedState: DR6=%#RX64 DR7=%#RX64\n", pCtx->dr[6], pCtx->dr[7]));
2486 }
2487
2488 HMCPU_CF_CLEAR(pVCpu, HM_CHANGED_GUEST_DEBUG);
2489 }
2490
2491 /* Unused on AMD-V. */
2492 HMCPU_CF_CLEAR(pVCpu, HM_CHANGED_GUEST_LAZY_MSRS);
2493
2494 AssertMsg(!HMCPU_CF_IS_PENDING(pVCpu, HM_CHANGED_HOST_GUEST_SHARED_STATE),
2495 ("fContextUseFlags=%#RX32\n", HMCPU_CF_VALUE(pVCpu)));
2496}
2497
2498
2499/**
2500 * Saves the guest (or nested-guest) state from the VMCB into the guest-CPU
2501 * context.
2502 *
2503 * Currently there is no residual state left in the CPU that is not updated in the
2504 * VMCB.
2505 *
2506 * @returns VBox status code.
2507 * @param pVCpu The cross context virtual CPU structure.
2508 * @param pMixedCtx Pointer to the guest-CPU context. The data may be
2509 * out-of-sync. Make sure to update the required fields
2510 * before using them.
2511 * @param pVmcb Pointer to the VM control block.
2512 */
2513static void hmR0SvmSaveGuestState(PVMCPU pVCpu, PCPUMCTX pMixedCtx, PCSVMVMCB pVmcb)
2514{
2515 Assert(VMMRZCallRing3IsEnabled(pVCpu));
2516
2517 pMixedCtx->rip = pVmcb->guest.u64RIP;
2518 pMixedCtx->rsp = pVmcb->guest.u64RSP;
2519 pMixedCtx->eflags.u32 = pVmcb->guest.u64RFlags;
2520 pMixedCtx->rax = pVmcb->guest.u64RAX;
2521
2522#ifdef VBOX_WITH_NESTED_HWVIRT
2523 /*
2524 * Guest Virtual GIF (Global Interrupt Flag).
2525 */
2526 if ( pVmcb->ctrl.IntCtrl.n.u1VGifEnable == 1
2527 && !CPUMIsGuestInSvmNestedHwVirtMode(pMixedCtx))
2528 {
2529 Assert(pVCpu->CTX_SUFF(pVM)->hm.s.svm.fVGif);
2530 pMixedCtx->hwvirt.svm.fGif = pVmcb->ctrl.IntCtrl.n.u1VGif;
2531 }
2532#endif
2533
2534 /*
2535 * Guest interrupt shadow.
2536 */
2537 if (pVmcb->ctrl.IntShadow.n.u1IntShadow)
2538 EMSetInhibitInterruptsPC(pVCpu, pMixedCtx->rip);
2539 else if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS))
2540 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS);
2541
2542 /*
2543 * Guest Control registers: CR2, CR3 (handled at the end) - accesses to other control registers are always intercepted.
2544 */
2545 pMixedCtx->cr2 = pVmcb->guest.u64CR2;
2546
2547 /*
2548 * Guest MSRs.
2549 */
2550 pMixedCtx->msrSTAR = pVmcb->guest.u64STAR; /* legacy syscall eip, cs & ss */
2551 pMixedCtx->msrLSTAR = pVmcb->guest.u64LSTAR; /* 64-bit mode syscall rip */
2552 pMixedCtx->msrCSTAR = pVmcb->guest.u64CSTAR; /* compatibility mode syscall rip */
2553 pMixedCtx->msrSFMASK = pVmcb->guest.u64SFMASK; /* syscall flag mask */
2554 pMixedCtx->msrKERNELGSBASE = pVmcb->guest.u64KernelGSBase; /* swapgs exchange value */
2555 pMixedCtx->SysEnter.cs = pVmcb->guest.u64SysEnterCS;
2556 pMixedCtx->SysEnter.eip = pVmcb->guest.u64SysEnterEIP;
2557 pMixedCtx->SysEnter.esp = pVmcb->guest.u64SysEnterESP;
2558
2559 /*
2560 * Guest segment registers (includes FS, GS base MSRs for 64-bit guests).
2561 */
2562 HMSVM_SEG_REG_COPY_FROM_VMCB(pMixedCtx, &pVmcb->guest, CS, cs);
2563 HMSVM_SEG_REG_COPY_FROM_VMCB(pMixedCtx, &pVmcb->guest, SS, ss);
2564 HMSVM_SEG_REG_COPY_FROM_VMCB(pMixedCtx, &pVmcb->guest, DS, ds);
2565 HMSVM_SEG_REG_COPY_FROM_VMCB(pMixedCtx, &pVmcb->guest, ES, es);
2566 HMSVM_SEG_REG_COPY_FROM_VMCB(pMixedCtx, &pVmcb->guest, FS, fs);
2567 HMSVM_SEG_REG_COPY_FROM_VMCB(pMixedCtx, &pVmcb->guest, GS, gs);
2568
2569 /*
2570 * Correct the hidden CS granularity bit. Haven't seen it being wrong in any other
2571 * register (yet).
2572 */
2573 /** @todo SELM might need to be fixed as it too should not care about the
2574 * granularity bit. See @bugref{6785}. */
2575 if ( !pMixedCtx->cs.Attr.n.u1Granularity
2576 && pMixedCtx->cs.Attr.n.u1Present
2577 && pMixedCtx->cs.u32Limit > UINT32_C(0xfffff))
2578 {
2579 Assert((pMixedCtx->cs.u32Limit & 0xfff) == 0xfff);
2580 pMixedCtx->cs.Attr.n.u1Granularity = 1;
2581 }
2582
2583 HMSVM_ASSERT_SEG_GRANULARITY(cs);
2584 HMSVM_ASSERT_SEG_GRANULARITY(ss);
2585 HMSVM_ASSERT_SEG_GRANULARITY(ds);
2586 HMSVM_ASSERT_SEG_GRANULARITY(es);
2587 HMSVM_ASSERT_SEG_GRANULARITY(fs);
2588 HMSVM_ASSERT_SEG_GRANULARITY(gs);
2589
2590 /*
2591 * Sync the hidden SS DPL field. AMD CPUs have a separate CPL field in the VMCB and uses that
2592 * and thus it's possible that when the CPL changes during guest execution that the SS DPL
2593 * isn't updated by AMD-V. Observed on some AMD Fusion CPUs with 64-bit guests.
2594 * See AMD spec. 15.5.1 "Basic operation".
2595 */
2596 Assert(!(pVmcb->guest.u8CPL & ~0x3));
2597 uint8_t const uCpl = pVmcb->guest.u8CPL;
2598 if (pMixedCtx->ss.Attr.n.u2Dpl != uCpl)
2599 {
2600 Log4(("hmR0SvmSaveGuestState: CPL differs. SS.DPL=%u, CPL=%u, overwriting SS.DPL!\n", pMixedCtx->ss.Attr.n.u2Dpl, uCpl));
2601 pMixedCtx->ss.Attr.n.u2Dpl = pVmcb->guest.u8CPL & 0x3;
2602 }
2603
2604 /*
2605 * Guest TR.
2606 * Fixup TR attributes so it's compatible with Intel. Important when saved-states are used
2607 * between Intel and AMD. See @bugref{6208#c39}.
2608 * ASSUME that it's normally correct and that we're in 32-bit or 64-bit mode.
2609 */
2610 HMSVM_SEG_REG_COPY_FROM_VMCB(pMixedCtx, &pVmcb->guest, TR, tr);
2611 if (pMixedCtx->tr.Attr.n.u4Type != X86_SEL_TYPE_SYS_386_TSS_BUSY)
2612 {
2613 if ( pMixedCtx->tr.Attr.n.u4Type == X86_SEL_TYPE_SYS_386_TSS_AVAIL
2614 || CPUMIsGuestInLongModeEx(pMixedCtx))
2615 pMixedCtx->tr.Attr.n.u4Type = X86_SEL_TYPE_SYS_386_TSS_BUSY;
2616 else if (pMixedCtx->tr.Attr.n.u4Type == X86_SEL_TYPE_SYS_286_TSS_AVAIL)
2617 pMixedCtx->tr.Attr.n.u4Type = X86_SEL_TYPE_SYS_286_TSS_BUSY;
2618 }
2619
2620 /*
2621 * Guest Descriptor-Table registers (GDTR, IDTR, LDTR).
2622 */
2623 HMSVM_SEG_REG_COPY_FROM_VMCB(pMixedCtx, &pVmcb->guest, LDTR, ldtr);
2624 pMixedCtx->gdtr.cbGdt = pVmcb->guest.GDTR.u32Limit;
2625 pMixedCtx->gdtr.pGdt = pVmcb->guest.GDTR.u64Base;
2626
2627 pMixedCtx->idtr.cbIdt = pVmcb->guest.IDTR.u32Limit;
2628 pMixedCtx->idtr.pIdt = pVmcb->guest.IDTR.u64Base;
2629
2630 /*
2631 * Guest Debug registers.
2632 */
2633 if (!pVCpu->hm.s.fUsingHyperDR7)
2634 {
2635 pMixedCtx->dr[6] = pVmcb->guest.u64DR6;
2636 pMixedCtx->dr[7] = pVmcb->guest.u64DR7;
2637 }
2638 else
2639 {
2640 Assert(pVmcb->guest.u64DR7 == CPUMGetHyperDR7(pVCpu));
2641 CPUMSetHyperDR6(pVCpu, pVmcb->guest.u64DR6);
2642 }
2643
2644 /*
2645 * With Nested Paging, CR3 changes are not intercepted. Therefore, sync. it now.
2646 * This is done as the very last step of syncing the guest state, as PGMUpdateCR3() may cause longjmp's to ring-3.
2647 */
2648 if ( pVmcb->ctrl.NestedPaging.n.u1NestedPaging
2649 && pMixedCtx->cr3 != pVmcb->guest.u64CR3)
2650 {
2651 CPUMSetGuestCR3(pVCpu, pVmcb->guest.u64CR3);
2652 PGMUpdateCR3(pVCpu, pVmcb->guest.u64CR3);
2653 }
2654
2655#ifdef VBOX_STRICT
2656 if (CPUMIsGuestInSvmNestedHwVirtMode(pMixedCtx))
2657 hmR0SvmLogState(pVCpu, pVmcb, pMixedCtx, "hmR0SvmSaveGuestStateNested", HMSVM_LOG_ALL & ~HMSVM_LOG_LBR, 0 /* uVerbose */);
2658#endif
2659}
2660
2661
2662/**
2663 * Does the necessary state syncing before returning to ring-3 for any reason
2664 * (longjmp, preemption, voluntary exits to ring-3) from AMD-V.
2665 *
2666 * @param pVCpu The cross context virtual CPU structure.
2667 *
2668 * @remarks No-long-jmp zone!!!
2669 */
2670static void hmR0SvmLeave(PVMCPU pVCpu)
2671{
2672 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
2673 Assert(!VMMRZCallRing3IsEnabled(pVCpu));
2674 Assert(VMMR0IsLogFlushDisabled(pVCpu));
2675
2676 /*
2677 * !!! IMPORTANT !!!
2678 * If you modify code here, make sure to check whether hmR0SvmCallRing3Callback() needs to be updated too.
2679 */
2680
2681 /* Restore host FPU state if necessary and resync on next R0 reentry .*/
2682 if (CPUMR0FpuStateMaybeSaveGuestAndRestoreHost(pVCpu))
2683 HMCPU_CF_SET(pVCpu, HM_CHANGED_GUEST_CR0); /** @todo r=ramshankar: This shouldn't be necessary, it's set in HMR0EnterCpu. */
2684
2685 /*
2686 * Restore host debug registers if necessary and resync on next R0 reentry.
2687 */
2688#ifdef VBOX_STRICT
2689 if (CPUMIsHyperDebugStateActive(pVCpu))
2690 {
2691 PSVMVMCB pVmcb = pVCpu->hm.s.svm.pVmcb; /** @todo nested-guest. */
2692 Assert(pVmcb->ctrl.u16InterceptRdDRx == 0xffff);
2693 Assert(pVmcb->ctrl.u16InterceptWrDRx == 0xffff);
2694 }
2695#endif
2696 if (CPUMR0DebugStateMaybeSaveGuestAndRestoreHost(pVCpu, false /* save DR6 */))
2697 HMCPU_CF_SET(pVCpu, HM_CHANGED_GUEST_DEBUG);/** @todo r=ramshankar: This shouldn't be necessary, it's set in HMR0EnterCpu. */
2698
2699 Assert(!CPUMIsHyperDebugStateActive(pVCpu));
2700 Assert(!CPUMIsGuestDebugStateActive(pVCpu));
2701
2702 STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatEntry);
2703 STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatLoadGuestState);
2704 STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatExit1);
2705 STAM_PROFILE_ADV_SET_STOPPED(&pVCpu->hm.s.StatExit2);
2706 STAM_COUNTER_INC(&pVCpu->hm.s.StatSwitchLongJmpToR3);
2707
2708 VMCPU_CMPXCHG_STATE(pVCpu, VMCPUSTATE_STARTED_HM, VMCPUSTATE_STARTED_EXEC);
2709}
2710
2711
2712/**
2713 * Leaves the AMD-V session.
2714 *
2715 * @returns VBox status code.
2716 * @param pVCpu The cross context virtual CPU structure.
2717 */
2718static int hmR0SvmLeaveSession(PVMCPU pVCpu)
2719{
2720 HM_DISABLE_PREEMPT();
2721 Assert(!VMMRZCallRing3IsEnabled(pVCpu));
2722 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
2723
2724 /* When thread-context hooks are used, we can avoid doing the leave again if we had been preempted before
2725 and done this from the SVMR0ThreadCtxCallback(). */
2726 if (!pVCpu->hm.s.fLeaveDone)
2727 {
2728 hmR0SvmLeave(pVCpu);
2729 pVCpu->hm.s.fLeaveDone = true;
2730 }
2731
2732 /*
2733 * !!! IMPORTANT !!!
2734 * If you modify code here, make sure to check whether hmR0SvmCallRing3Callback() needs to be updated too.
2735 */
2736
2737 /** @todo eliminate the need for calling VMMR0ThreadCtxHookDisable here! */
2738 /* Deregister hook now that we've left HM context before re-enabling preemption. */
2739 VMMR0ThreadCtxHookDisable(pVCpu);
2740
2741 /* Leave HM context. This takes care of local init (term). */
2742 int rc = HMR0LeaveCpu(pVCpu);
2743
2744 HM_RESTORE_PREEMPT();
2745 return rc;
2746}
2747
2748
2749/**
2750 * Does the necessary state syncing before doing a longjmp to ring-3.
2751 *
2752 * @returns VBox status code.
2753 * @param pVCpu The cross context virtual CPU structure.
2754 *
2755 * @remarks No-long-jmp zone!!!
2756 */
2757static int hmR0SvmLongJmpToRing3(PVMCPU pVCpu)
2758{
2759 return hmR0SvmLeaveSession(pVCpu);
2760}
2761
2762
2763/**
2764 * VMMRZCallRing3() callback wrapper which saves the guest state (or restores
2765 * any remaining host state) before we longjump to ring-3 and possibly get
2766 * preempted.
2767 *
2768 * @param pVCpu The cross context virtual CPU structure.
2769 * @param enmOperation The operation causing the ring-3 longjump.
2770 * @param pvUser The user argument (pointer to the possibly
2771 * out-of-date guest-CPU context).
2772 */
2773static DECLCALLBACK(int) hmR0SvmCallRing3Callback(PVMCPU pVCpu, VMMCALLRING3 enmOperation, void *pvUser)
2774{
2775 RT_NOREF_PV(pvUser);
2776
2777 if (enmOperation == VMMCALLRING3_VM_R0_ASSERTION)
2778 {
2779 /*
2780 * !!! IMPORTANT !!!
2781 * If you modify code here, make sure to check whether hmR0SvmLeave() and hmR0SvmLeaveSession() needs
2782 * to be updated too. This is a stripped down version which gets out ASAP trying to not trigger any assertion.
2783 */
2784 VMMRZCallRing3RemoveNotification(pVCpu);
2785 VMMRZCallRing3Disable(pVCpu);
2786 HM_DISABLE_PREEMPT();
2787
2788 /* Restore host FPU state if necessary and resync on next R0 reentry. */
2789 CPUMR0FpuStateMaybeSaveGuestAndRestoreHost(pVCpu);
2790
2791 /* Restore host debug registers if necessary and resync on next R0 reentry. */
2792 CPUMR0DebugStateMaybeSaveGuestAndRestoreHost(pVCpu, false /* save DR6 */);
2793
2794 /* Deregister the hook now that we've left HM context before re-enabling preemption. */
2795 /** @todo eliminate the need for calling VMMR0ThreadCtxHookDisable here! */
2796 VMMR0ThreadCtxHookDisable(pVCpu);
2797
2798 /* Leave HM context. This takes care of local init (term). */
2799 HMR0LeaveCpu(pVCpu);
2800
2801 HM_RESTORE_PREEMPT();
2802 return VINF_SUCCESS;
2803 }
2804
2805 Assert(pVCpu);
2806 Assert(pvUser);
2807 Assert(VMMRZCallRing3IsEnabled(pVCpu));
2808 HMSVM_ASSERT_PREEMPT_SAFE();
2809
2810 VMMRZCallRing3Disable(pVCpu);
2811 Assert(VMMR0IsLogFlushDisabled(pVCpu));
2812
2813 Log4(("hmR0SvmCallRing3Callback->hmR0SvmLongJmpToRing3\n"));
2814 int rc = hmR0SvmLongJmpToRing3(pVCpu);
2815 AssertRCReturn(rc, rc);
2816
2817 VMMRZCallRing3Enable(pVCpu);
2818 return VINF_SUCCESS;
2819}
2820
2821
2822/**
2823 * Take necessary actions before going back to ring-3.
2824 *
2825 * An action requires us to go back to ring-3. This function does the necessary
2826 * steps before we can safely return to ring-3. This is not the same as longjmps
2827 * to ring-3, this is voluntary.
2828 *
2829 * @returns VBox status code.
2830 * @param pVM The cross context VM structure.
2831 * @param pVCpu The cross context virtual CPU structure.
2832 * @param pCtx Pointer to the guest-CPU context.
2833 * @param rcExit The reason for exiting to ring-3. Can be
2834 * VINF_VMM_UNKNOWN_RING3_CALL.
2835 */
2836static int hmR0SvmExitToRing3(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, int rcExit)
2837{
2838 Assert(pVM);
2839 Assert(pVCpu);
2840 Assert(pCtx);
2841 HMSVM_ASSERT_PREEMPT_SAFE();
2842
2843 /* Please, no longjumps here (any logging shouldn't flush jump back to ring-3). NO LOGGING BEFORE THIS POINT! */
2844 VMMRZCallRing3Disable(pVCpu);
2845 Log4(("hmR0SvmExitToRing3: VCPU[%u]: rcExit=%d LocalFF=%#RX32 GlobalFF=%#RX32\n", pVCpu->idCpu, rcExit,
2846 pVCpu->fLocalForcedActions, pVM->fGlobalForcedActions));
2847
2848 /* We need to do this only while truly exiting the "inner loop" back to ring-3 and -not- for any longjmp to ring3. */
2849 if (pVCpu->hm.s.Event.fPending)
2850 {
2851 hmR0SvmPendingEventToTrpmTrap(pVCpu);
2852 Assert(!pVCpu->hm.s.Event.fPending);
2853 }
2854
2855 /* Sync. the necessary state for going back to ring-3. */
2856 hmR0SvmLeaveSession(pVCpu);
2857 STAM_COUNTER_DEC(&pVCpu->hm.s.StatSwitchLongJmpToR3);
2858
2859 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_TO_R3);
2860 CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_SYSENTER_MSR
2861 | CPUM_CHANGED_LDTR
2862 | CPUM_CHANGED_GDTR
2863 | CPUM_CHANGED_IDTR
2864 | CPUM_CHANGED_TR
2865 | CPUM_CHANGED_HIDDEN_SEL_REGS);
2866 if ( pVM->hm.s.fNestedPaging
2867 && CPUMIsGuestPagingEnabledEx(pCtx))
2868 {
2869 CPUMSetChangedFlags(pVCpu, CPUM_CHANGED_GLOBAL_TLB_FLUSH);
2870 }
2871
2872 /* On our way back from ring-3 reload the guest state if there is a possibility of it being changed. */
2873 if (rcExit != VINF_EM_RAW_INTERRUPT)
2874 HMCPU_CF_SET(pVCpu, HM_CHANGED_ALL_GUEST);
2875
2876 STAM_COUNTER_INC(&pVCpu->hm.s.StatSwitchExitToR3);
2877
2878 /* We do -not- want any longjmp notifications after this! We must return to ring-3 ASAP. */
2879 VMMRZCallRing3RemoveNotification(pVCpu);
2880 VMMRZCallRing3Enable(pVCpu);
2881
2882 /*
2883 * If we're emulating an instruction, we shouldn't have any TRPM traps pending
2884 * and if we're injecting an event we should have a TRPM trap pending.
2885 */
2886 AssertReturnStmt(rcExit != VINF_EM_RAW_INJECT_TRPM_EVENT || TRPMHasTrap(pVCpu),
2887 pVCpu->hm.s.u32HMError = rcExit,
2888 VERR_SVM_IPE_5);
2889 AssertReturnStmt(rcExit != VINF_EM_RAW_EMULATE_INSTR || !TRPMHasTrap(pVCpu),
2890 pVCpu->hm.s.u32HMError = rcExit,
2891 VERR_SVM_IPE_4);
2892
2893 return rcExit;
2894}
2895
2896
2897#ifdef VBOX_WITH_NESTED_HWVIRT
2898/**
2899 * Updates the use of TSC offsetting mode for the CPU and adjusts the necessary
2900 * intercepts for the nested-guest.
2901 *
2902 * @param pVM The cross context VM structure.
2903 * @param pVCpu The cross context virtual CPU structure.
2904 * @param pCtx Pointer to the nested guest-CPU context.
2905 * @param pVmcbNstGst Pointer to the nested-guest VM control block.
2906 *
2907 * @remarks No-long-jump zone!!!
2908 */
2909static void hmR0SvmUpdateTscOffsettingNested(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, PSVMVMCB pVmcbNstGst)
2910{
2911 Assert(CPUMIsGuestInSvmNestedHwVirtMode(pCtx));
2912
2913 bool fParavirtTsc;
2914 uint64_t uTscOffset;
2915 bool fCanUseRealTsc = TMCpuTickCanUseRealTSC(pVM, pVCpu, &uTscOffset, &fParavirtTsc);
2916
2917 PCSVMNESTEDVMCBCACHE pVmcbNstGstCache = &pVCpu->hm.s.svm.NstGstVmcbCache;
2918 PSVMVMCBCTRL pVmcbNstGstCtrl = &pVmcbNstGst->ctrl;
2919
2920 /*
2921 * Only avoid intercepting if we determined the host TSC (++) is stable enough
2922 * to not intercept -and- the nested-hypervisor itself does not want to intercept it.
2923 */
2924 if ( fCanUseRealTsc
2925 && !(pVmcbNstGstCache->u64InterceptCtrl & (SVM_CTRL_INTERCEPT_RDTSC | SVM_CTRL_INTERCEPT_RDTSCP)))
2926 {
2927 pVmcbNstGstCtrl->u64InterceptCtrl &= ~SVM_CTRL_INTERCEPT_RDTSC;
2928 pVmcbNstGstCtrl->u64InterceptCtrl &= ~SVM_CTRL_INTERCEPT_RDTSCP;
2929 STAM_COUNTER_INC(&pVCpu->hm.s.StatTscOffset);
2930 }
2931 else
2932 {
2933 pVmcbNstGstCtrl->u64InterceptCtrl |= SVM_CTRL_INTERCEPT_RDTSC;
2934 pVmcbNstGstCtrl->u64InterceptCtrl |= SVM_CTRL_INTERCEPT_RDTSCP;
2935 STAM_COUNTER_INC(&pVCpu->hm.s.StatTscIntercept);
2936 }
2937
2938 /* Apply the nested-guest VMCB's TSC offset over the guest one. */
2939 uTscOffset = CPUMApplyNestedGuestTscOffset(pVCpu, uTscOffset);
2940
2941 /* Update the nested-guest VMCB with the combined TSC offset (of guest and nested-guest). */
2942 pVmcbNstGstCtrl->u64TSCOffset = uTscOffset;
2943
2944 /* Finally update the VMCB clean bits since we touched the intercepts as well as the TSC offset. */
2945 pVmcbNstGstCtrl->u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_INTERCEPTS;
2946
2947 if (fParavirtTsc)
2948 {
2949 /* Currently neither Hyper-V nor KVM need to update their paravirt. TSC
2950 information before every VM-entry, hence disable it for performance sake. */
2951 STAM_COUNTER_INC(&pVCpu->hm.s.StatTscParavirt);
2952 }
2953}
2954#endif
2955
2956
2957/**
2958 * Updates the use of TSC offsetting mode for the CPU and adjusts the necessary
2959 * intercepts.
2960 *
2961 * @param pVM The cross context VM structure.
2962 * @param pVCpu The cross context virtual CPU structure.
2963 * @param pVmcb Pointer to the VM control block.
2964 *
2965 * @remarks No-long-jump zone!!!
2966 */
2967static void hmR0SvmUpdateTscOffsetting(PVM pVM, PVMCPU pVCpu, PSVMVMCB pVmcb)
2968{
2969 bool fParavirtTsc;
2970 bool fCanUseRealTsc = TMCpuTickCanUseRealTSC(pVM, pVCpu, &pVmcb->ctrl.u64TSCOffset, &fParavirtTsc);
2971 if (fCanUseRealTsc)
2972 {
2973 pVmcb->ctrl.u64InterceptCtrl &= ~SVM_CTRL_INTERCEPT_RDTSC;
2974 pVmcb->ctrl.u64InterceptCtrl &= ~SVM_CTRL_INTERCEPT_RDTSCP;
2975 STAM_COUNTER_INC(&pVCpu->hm.s.StatTscOffset);
2976 }
2977 else
2978 {
2979 pVmcb->ctrl.u64InterceptCtrl |= SVM_CTRL_INTERCEPT_RDTSC;
2980 pVmcb->ctrl.u64InterceptCtrl |= SVM_CTRL_INTERCEPT_RDTSCP;
2981 STAM_COUNTER_INC(&pVCpu->hm.s.StatTscIntercept);
2982 }
2983 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_INTERCEPTS;
2984
2985 /** @todo later optimize this to be done elsewhere and not before every
2986 * VM-entry. */
2987 if (fParavirtTsc)
2988 {
2989 /* Currently neither Hyper-V nor KVM need to update their paravirt. TSC
2990 information before every VM-entry, hence disable it for performance sake. */
2991#if 0
2992 int rc = GIMR0UpdateParavirtTsc(pVM, 0 /* u64Offset */);
2993 AssertRC(rc);
2994#endif
2995 STAM_COUNTER_INC(&pVCpu->hm.s.StatTscParavirt);
2996 }
2997}
2998
2999
3000/**
3001 * Sets an event as a pending event to be injected into the guest.
3002 *
3003 * @param pVCpu The cross context virtual CPU structure.
3004 * @param pEvent Pointer to the SVM event.
3005 * @param GCPtrFaultAddress The fault-address (CR2) in case it's a
3006 * page-fault.
3007 *
3008 * @remarks Statistics counter assumes this is a guest event being reflected to
3009 * the guest i.e. 'StatInjectPendingReflect' is incremented always.
3010 */
3011DECLINLINE(void) hmR0SvmSetPendingEvent(PVMCPU pVCpu, PSVMEVENT pEvent, RTGCUINTPTR GCPtrFaultAddress)
3012{
3013 Assert(!pVCpu->hm.s.Event.fPending);
3014 Assert(pEvent->n.u1Valid);
3015
3016 pVCpu->hm.s.Event.u64IntInfo = pEvent->u;
3017 pVCpu->hm.s.Event.fPending = true;
3018 pVCpu->hm.s.Event.GCPtrFaultAddress = GCPtrFaultAddress;
3019
3020 Log4(("hmR0SvmSetPendingEvent: u=%#RX64 u8Vector=%#x Type=%#x ErrorCodeValid=%RTbool ErrorCode=%#RX32\n", pEvent->u,
3021 pEvent->n.u8Vector, (uint8_t)pEvent->n.u3Type, !!pEvent->n.u1ErrorCodeValid, pEvent->n.u32ErrorCode));
3022}
3023
3024
3025/**
3026 * Sets an invalid-opcode (\#UD) exception as pending-for-injection into the VM.
3027 *
3028 * @param pVCpu The cross context virtual CPU structure.
3029 */
3030DECLINLINE(void) hmR0SvmSetPendingXcptUD(PVMCPU pVCpu)
3031{
3032 SVMEVENT Event;
3033 Event.u = 0;
3034 Event.n.u1Valid = 1;
3035 Event.n.u3Type = SVM_EVENT_EXCEPTION;
3036 Event.n.u8Vector = X86_XCPT_UD;
3037 hmR0SvmSetPendingEvent(pVCpu, &Event, 0 /* GCPtrFaultAddress */);
3038}
3039
3040
3041/**
3042 * Sets a debug (\#DB) exception as pending-for-injection into the VM.
3043 *
3044 * @param pVCpu The cross context virtual CPU structure.
3045 */
3046DECLINLINE(void) hmR0SvmSetPendingXcptDB(PVMCPU pVCpu)
3047{
3048 SVMEVENT Event;
3049 Event.u = 0;
3050 Event.n.u1Valid = 1;
3051 Event.n.u3Type = SVM_EVENT_EXCEPTION;
3052 Event.n.u8Vector = X86_XCPT_DB;
3053 hmR0SvmSetPendingEvent(pVCpu, &Event, 0 /* GCPtrFaultAddress */);
3054}
3055
3056
3057/**
3058 * Sets a page fault (\#PF) exception as pending-for-injection into the VM.
3059 *
3060 * @param pVCpu The cross context virtual CPU structure.
3061 * @param pCtx Pointer to the guest-CPU context.
3062 * @param u32ErrCode The error-code for the page-fault.
3063 * @param uFaultAddress The page fault address (CR2).
3064 *
3065 * @remarks This updates the guest CR2 with @a uFaultAddress!
3066 */
3067DECLINLINE(void) hmR0SvmSetPendingXcptPF(PVMCPU pVCpu, PCPUMCTX pCtx, uint32_t u32ErrCode, RTGCUINTPTR uFaultAddress)
3068{
3069 SVMEVENT Event;
3070 Event.u = 0;
3071 Event.n.u1Valid = 1;
3072 Event.n.u3Type = SVM_EVENT_EXCEPTION;
3073 Event.n.u8Vector = X86_XCPT_PF;
3074 Event.n.u1ErrorCodeValid = 1;
3075 Event.n.u32ErrorCode = u32ErrCode;
3076
3077 /* Update CR2 of the guest. */
3078 if (pCtx->cr2 != uFaultAddress)
3079 {
3080 pCtx->cr2 = uFaultAddress;
3081 /* The VMCB clean bit for CR2 will be updated while re-loading the guest state. */
3082 HMCPU_CF_SET(pVCpu, HM_CHANGED_GUEST_CR2);
3083 }
3084
3085 hmR0SvmSetPendingEvent(pVCpu, &Event, uFaultAddress);
3086}
3087
3088
3089/**
3090 * Sets a device-not-available (\#NM) exception as pending-for-injection into
3091 * the VM.
3092 *
3093 * @param pVCpu The cross context virtual CPU structure.
3094 */
3095DECLINLINE(void) hmR0SvmSetPendingXcptNM(PVMCPU pVCpu)
3096{
3097 SVMEVENT Event;
3098 Event.u = 0;
3099 Event.n.u1Valid = 1;
3100 Event.n.u3Type = SVM_EVENT_EXCEPTION;
3101 Event.n.u8Vector = X86_XCPT_NM;
3102 hmR0SvmSetPendingEvent(pVCpu, &Event, 0 /* GCPtrFaultAddress */);
3103}
3104
3105
3106/**
3107 * Sets a math-fault (\#MF) exception as pending-for-injection into the VM.
3108 *
3109 * @param pVCpu The cross context virtual CPU structure.
3110 */
3111DECLINLINE(void) hmR0SvmSetPendingXcptMF(PVMCPU pVCpu)
3112{
3113 SVMEVENT Event;
3114 Event.u = 0;
3115 Event.n.u1Valid = 1;
3116 Event.n.u3Type = SVM_EVENT_EXCEPTION;
3117 Event.n.u8Vector = X86_XCPT_MF;
3118 hmR0SvmSetPendingEvent(pVCpu, &Event, 0 /* GCPtrFaultAddress */);
3119}
3120
3121
3122/**
3123 * Sets a double fault (\#DF) exception as pending-for-injection into the VM.
3124 *
3125 * @param pVCpu The cross context virtual CPU structure.
3126 */
3127DECLINLINE(void) hmR0SvmSetPendingXcptDF(PVMCPU pVCpu)
3128{
3129 SVMEVENT Event;
3130 Event.u = 0;
3131 Event.n.u1Valid = 1;
3132 Event.n.u3Type = SVM_EVENT_EXCEPTION;
3133 Event.n.u8Vector = X86_XCPT_DF;
3134 Event.n.u1ErrorCodeValid = 1;
3135 Event.n.u32ErrorCode = 0;
3136 hmR0SvmSetPendingEvent(pVCpu, &Event, 0 /* GCPtrFaultAddress */);
3137}
3138
3139
3140/**
3141 * Injects an event into the guest upon VMRUN by updating the relevant field
3142 * in the VMCB.
3143 *
3144 * @param pVCpu The cross context virtual CPU structure.
3145 * @param pVmcb Pointer to the guest VM control block.
3146 * @param pCtx Pointer to the guest-CPU context.
3147 * @param pEvent Pointer to the event.
3148 *
3149 * @remarks No-long-jump zone!!!
3150 * @remarks Requires CR0!
3151 */
3152DECLINLINE(void) hmR0SvmInjectEventVmcb(PVMCPU pVCpu, PSVMVMCB pVmcb, PCPUMCTX pCtx, PSVMEVENT pEvent)
3153{
3154 NOREF(pVCpu); NOREF(pCtx);
3155
3156 Assert(!pVmcb->ctrl.EventInject.n.u1Valid);
3157 pVmcb->ctrl.EventInject.u = pEvent->u;
3158 STAM_COUNTER_INC(&pVCpu->hm.s.paStatInjectedIrqsR0[pEvent->n.u8Vector & MASK_INJECT_IRQ_STAT]);
3159
3160 Log4(("hmR0SvmInjectEventVmcb: u=%#RX64 u8Vector=%#x Type=%#x ErrorCodeValid=%RTbool ErrorCode=%#RX32\n", pEvent->u,
3161 pEvent->n.u8Vector, (uint8_t)pEvent->n.u3Type, !!pEvent->n.u1ErrorCodeValid, pEvent->n.u32ErrorCode));
3162}
3163
3164
3165
3166/**
3167 * Converts any TRPM trap into a pending HM event. This is typically used when
3168 * entering from ring-3 (not longjmp returns).
3169 *
3170 * @param pVCpu The cross context virtual CPU structure.
3171 */
3172static void hmR0SvmTrpmTrapToPendingEvent(PVMCPU pVCpu)
3173{
3174 Assert(TRPMHasTrap(pVCpu));
3175 Assert(!pVCpu->hm.s.Event.fPending);
3176
3177 uint8_t uVector;
3178 TRPMEVENT enmTrpmEvent;
3179 RTGCUINT uErrCode;
3180 RTGCUINTPTR GCPtrFaultAddress;
3181 uint8_t cbInstr;
3182
3183 int rc = TRPMQueryTrapAll(pVCpu, &uVector, &enmTrpmEvent, &uErrCode, &GCPtrFaultAddress, &cbInstr);
3184 AssertRC(rc);
3185
3186 SVMEVENT Event;
3187 Event.u = 0;
3188 Event.n.u1Valid = 1;
3189 Event.n.u8Vector = uVector;
3190
3191 /* Refer AMD spec. 15.20 "Event Injection" for the format. */
3192 if (enmTrpmEvent == TRPM_TRAP)
3193 {
3194 Event.n.u3Type = SVM_EVENT_EXCEPTION;
3195 switch (uVector)
3196 {
3197 case X86_XCPT_NMI:
3198 {
3199 Event.n.u3Type = SVM_EVENT_NMI;
3200 break;
3201 }
3202
3203 case X86_XCPT_PF:
3204 case X86_XCPT_DF:
3205 case X86_XCPT_TS:
3206 case X86_XCPT_NP:
3207 case X86_XCPT_SS:
3208 case X86_XCPT_GP:
3209 case X86_XCPT_AC:
3210 {
3211 Event.n.u1ErrorCodeValid = 1;
3212 Event.n.u32ErrorCode = uErrCode;
3213 break;
3214 }
3215 }
3216 }
3217 else if (enmTrpmEvent == TRPM_HARDWARE_INT)
3218 Event.n.u3Type = SVM_EVENT_EXTERNAL_IRQ;
3219 else if (enmTrpmEvent == TRPM_SOFTWARE_INT)
3220 Event.n.u3Type = SVM_EVENT_SOFTWARE_INT;
3221 else
3222 AssertMsgFailed(("Invalid TRPM event type %d\n", enmTrpmEvent));
3223
3224 rc = TRPMResetTrap(pVCpu);
3225 AssertRC(rc);
3226
3227 Log4(("TRPM->HM event: u=%#RX64 u8Vector=%#x uErrorCodeValid=%RTbool uErrorCode=%#RX32\n", Event.u, Event.n.u8Vector,
3228 !!Event.n.u1ErrorCodeValid, Event.n.u32ErrorCode));
3229
3230 hmR0SvmSetPendingEvent(pVCpu, &Event, GCPtrFaultAddress);
3231}
3232
3233
3234/**
3235 * Converts any pending SVM event into a TRPM trap. Typically used when leaving
3236 * AMD-V to execute any instruction.
3237 *
3238 * @param pVCpu The cross context virtual CPU structure.
3239 */
3240static void hmR0SvmPendingEventToTrpmTrap(PVMCPU pVCpu)
3241{
3242 Assert(pVCpu->hm.s.Event.fPending);
3243 Assert(TRPMQueryTrap(pVCpu, NULL /* pu8TrapNo */, NULL /* pEnmType */) == VERR_TRPM_NO_ACTIVE_TRAP);
3244
3245 SVMEVENT Event;
3246 Event.u = pVCpu->hm.s.Event.u64IntInfo;
3247
3248 uint8_t uVector = Event.n.u8Vector;
3249 uint8_t uVectorType = Event.n.u3Type;
3250 TRPMEVENT enmTrapType = HMSvmEventToTrpmEventType(&Event);
3251
3252 Log4(("HM event->TRPM: uVector=%#x enmTrapType=%d\n", uVector, uVectorType));
3253
3254 int rc = TRPMAssertTrap(pVCpu, uVector, enmTrapType);
3255 AssertRC(rc);
3256
3257 if (Event.n.u1ErrorCodeValid)
3258 TRPMSetErrorCode(pVCpu, Event.n.u32ErrorCode);
3259
3260 if ( uVectorType == SVM_EVENT_EXCEPTION
3261 && uVector == X86_XCPT_PF)
3262 {
3263 TRPMSetFaultAddress(pVCpu, pVCpu->hm.s.Event.GCPtrFaultAddress);
3264 Assert(pVCpu->hm.s.Event.GCPtrFaultAddress == CPUMGetGuestCR2(pVCpu));
3265 }
3266 else if (uVectorType == SVM_EVENT_SOFTWARE_INT)
3267 {
3268 AssertMsg( uVectorType == SVM_EVENT_SOFTWARE_INT
3269 || (uVector == X86_XCPT_BP || uVector == X86_XCPT_OF),
3270 ("Invalid vector: uVector=%#x uVectorType=%#x\n", uVector, uVectorType));
3271 TRPMSetInstrLength(pVCpu, pVCpu->hm.s.Event.cbInstr);
3272 }
3273 pVCpu->hm.s.Event.fPending = false;
3274}
3275
3276
3277/**
3278 * Checks if the guest (or nested-guest) has an interrupt shadow active right
3279 * now.
3280 *
3281 * @returns @c true if the interrupt shadow is active, @c false otherwise.
3282 * @param pVCpu The cross context virtual CPU structure.
3283 * @param pCtx Pointer to the guest-CPU context.
3284 *
3285 * @remarks No-long-jump zone!!!
3286 * @remarks Has side-effects with VMCPU_FF_INHIBIT_INTERRUPTS force-flag.
3287 */
3288DECLINLINE(bool) hmR0SvmIsIntrShadowActive(PVMCPU pVCpu, PCPUMCTX pCtx)
3289{
3290 /*
3291 * Instructions like STI and MOV SS inhibit interrupts till the next instruction completes. Check if we should
3292 * inhibit interrupts or clear any existing interrupt-inhibition.
3293 */
3294 if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS))
3295 {
3296 if (pCtx->rip != EMGetInhibitInterruptsPC(pVCpu))
3297 {
3298 /*
3299 * We can clear the inhibit force flag as even if we go back to the recompiler without executing guest code in
3300 * AMD-V, the flag's condition to be cleared is met and thus the cleared state is correct.
3301 */
3302 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INHIBIT_INTERRUPTS);
3303 return false;
3304 }
3305 return true;
3306 }
3307 return false;
3308}
3309
3310
3311/**
3312 * Sets the virtual interrupt intercept control in the VMCB.
3313 *
3314 * @param pVmcb Pointer to the VM control block.
3315 */
3316DECLINLINE(void) hmR0SvmSetVirtIntrIntercept(PSVMVMCB pVmcb)
3317{
3318 /*
3319 * When AVIC isn't supported, indicate that a virtual interrupt is pending and to
3320 * cause a #VMEXIT when the guest is ready to accept interrupts. At #VMEXIT, we
3321 * then get the interrupt from the APIC (updating ISR at the right time) and
3322 * inject the interrupt.
3323 *
3324 * With AVIC is supported, we could make use of the asynchronously delivery without
3325 * #VMEXIT and we would be passing the AVIC page to SVM.
3326 */
3327 if (!(pVmcb->ctrl.u64InterceptCtrl & SVM_CTRL_INTERCEPT_VINTR))
3328 {
3329 Assert(pVmcb->ctrl.IntCtrl.n.u1VIrqPending == 0);
3330 pVmcb->ctrl.IntCtrl.n.u1VIrqPending = 1;
3331 pVmcb->ctrl.u64InterceptCtrl |= SVM_CTRL_INTERCEPT_VINTR;
3332 pVmcb->ctrl.u32VmcbCleanBits &= ~(HMSVM_VMCB_CLEAN_INTERCEPTS | HMSVM_VMCB_CLEAN_TPR);
3333 Log4(("Set VINTR intercept\n"));
3334 }
3335}
3336
3337
3338/**
3339 * Clears the virtual interrupt intercept control in the VMCB as
3340 * we are figured the guest is unable process any interrupts
3341 * at this point of time.
3342 *
3343 * @param pVmcb Pointer to the VM control block.
3344 */
3345DECLINLINE(void) hmR0SvmClearVirtIntrIntercept(PSVMVMCB pVmcb)
3346{
3347 if (pVmcb->ctrl.u64InterceptCtrl & SVM_CTRL_INTERCEPT_VINTR)
3348 {
3349 Assert(pVmcb->ctrl.IntCtrl.n.u1VIrqPending == 1);
3350 pVmcb->ctrl.IntCtrl.n.u1VIrqPending = 0;
3351 pVmcb->ctrl.u64InterceptCtrl &= ~SVM_CTRL_INTERCEPT_VINTR;
3352 pVmcb->ctrl.u32VmcbCleanBits &= ~(HMSVM_VMCB_CLEAN_INTERCEPTS | HMSVM_VMCB_CLEAN_TPR);
3353 Log4(("Cleared VINTR intercept\n"));
3354 }
3355}
3356
3357
3358/**
3359 * Sets the IRET intercept control in the VMCB which instructs AMD-V to cause a
3360 * \#VMEXIT as soon as a guest starts executing an IRET. This is used to unblock
3361 * virtual NMIs.
3362 *
3363 * @param pVmcb Pointer to the VM control block.
3364 */
3365DECLINLINE(void) hmR0SvmSetIretIntercept(PSVMVMCB pVmcb)
3366{
3367 if (!(pVmcb->ctrl.u64InterceptCtrl & SVM_CTRL_INTERCEPT_IRET))
3368 {
3369 pVmcb->ctrl.u64InterceptCtrl |= SVM_CTRL_INTERCEPT_IRET;
3370 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_INTERCEPTS;
3371
3372 Log4(("Setting IRET intercept\n"));
3373 }
3374}
3375
3376
3377/**
3378 * Clears the IRET intercept control in the VMCB.
3379 *
3380 * @param pVmcb Pointer to the VM control block.
3381 */
3382DECLINLINE(void) hmR0SvmClearIretIntercept(PSVMVMCB pVmcb)
3383{
3384 if (pVmcb->ctrl.u64InterceptCtrl & SVM_CTRL_INTERCEPT_IRET)
3385 {
3386 pVmcb->ctrl.u64InterceptCtrl &= ~SVM_CTRL_INTERCEPT_IRET;
3387 pVmcb->ctrl.u32VmcbCleanBits &= ~(HMSVM_VMCB_CLEAN_INTERCEPTS);
3388
3389 Log4(("Clearing IRET intercept\n"));
3390 }
3391}
3392
3393#ifdef VBOX_WITH_NESTED_HWVIRT
3394
3395
3396/**
3397 * Evaluates the event to be delivered to the nested-guest and sets it as the
3398 * pending event.
3399 *
3400 * @returns VBox strict status code.
3401 * @param pVCpu The cross context virtual CPU structure.
3402 * @param pCtx Pointer to the guest-CPU context.
3403 */
3404static VBOXSTRICTRC hmR0SvmEvaluatePendingEventNested(PVMCPU pVCpu, PCPUMCTX pCtx)
3405{
3406 Log4Func(("\n"));
3407
3408 Assert(!pVCpu->hm.s.Event.fPending);
3409
3410 bool const fGif = pCtx->hwvirt.svm.fGif;
3411 if (fGif)
3412 {
3413 PSVMVMCB pVmcbNstGst = pCtx->hwvirt.svm.CTX_SUFF(pVmcb);
3414
3415 bool const fIntShadow = hmR0SvmIsIntrShadowActive(pVCpu, pCtx);
3416
3417 /*
3418 * Check if the nested-guest can receive NMIs.
3419 * NMIs are higher priority than regular interrupts.
3420 */
3421 /** @todo SMI. SMIs take priority over NMIs. */
3422 if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INTERRUPT_NMI))
3423 {
3424 bool const fBlockNmi = VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_BLOCK_NMIS);
3425 if (fBlockNmi)
3426 hmR0SvmSetIretIntercept(pVmcbNstGst);
3427 else if (fIntShadow)
3428 {
3429 /** @todo Figure this out, how we shall manage virt. intercept if the
3430 * nested-guest already has one set and/or if we really need it? */
3431 //hmR0SvmSetVirtIntrIntercept(pVmcbNstGst);
3432 }
3433 else
3434 {
3435 Log4(("Pending NMI\n"));
3436
3437 SVMEVENT Event;
3438 Event.u = 0;
3439 Event.n.u1Valid = 1;
3440 Event.n.u8Vector = X86_XCPT_NMI;
3441 Event.n.u3Type = SVM_EVENT_NMI;
3442
3443 hmR0SvmSetPendingEvent(pVCpu, &Event, 0 /* GCPtrFaultAddress */);
3444 hmR0SvmSetIretIntercept(pVmcbNstGst);
3445 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_NMI);
3446 return VINF_SUCCESS;
3447 }
3448 }
3449
3450 /*
3451 * Check if the nested-guest can receive external interrupts (generated by
3452 * the guest's PIC/APIC).
3453 *
3454 * External intercepts, NMI, SMI etc. from the physical CPU are -always- intercepted
3455 * when executing using hardware-assisted SVM, see HMSVM_MANDATORY_GUEST_CTRL_INTERCEPTS.
3456 *
3457 * External interrupts that are generated for the outer guest may be intercepted
3458 * depending on how the nested-guest VMCB was programmed by guest software.
3459 *
3460 * Physical interrupts always take priority over virtual interrupts,
3461 * see AMD spec. 15.21.4 "Injecting Virtual (INTR) Interrupts".
3462 */
3463 if (!fIntShadow)
3464 {
3465 PCSVMNESTEDVMCBCACHE pVmcbNstGstCache = &pVCpu->hm.s.svm.NstGstVmcbCache;
3466 if ( VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC)
3467 && !pVCpu->hm.s.fSingleInstruction
3468 && CPUMCanSvmNstGstTakePhysIntr(pVCpu, pCtx))
3469 {
3470 if (pVmcbNstGstCache->u64InterceptCtrl & SVM_CTRL_INTERCEPT_INTR)
3471 {
3472 Log4(("Intercepting external interrupt -> #VMEXIT\n"));
3473 return IEMExecSvmVmexit(pVCpu, SVM_EXIT_INTR, 0, 0);
3474 }
3475
3476 uint8_t u8Interrupt;
3477 int rc = PDMGetInterrupt(pVCpu, &u8Interrupt);
3478 if (RT_SUCCESS(rc))
3479 {
3480 Log4(("Injecting external interrupt u8Interrupt=%#x\n", u8Interrupt));
3481
3482 SVMEVENT Event;
3483 Event.u = 0;
3484 Event.n.u1Valid = 1;
3485 Event.n.u8Vector = u8Interrupt;
3486 Event.n.u3Type = SVM_EVENT_EXTERNAL_IRQ;
3487
3488 hmR0SvmSetPendingEvent(pVCpu, &Event, 0 /* GCPtrFaultAddress */);
3489 }
3490 else if (rc == VERR_APIC_INTR_MASKED_BY_TPR)
3491 {
3492 /*
3493 * AMD-V has no TPR thresholding feature. TPR and the force-flag will be
3494 * updated eventually when the TPR is written by the guest.
3495 */
3496 STAM_COUNTER_INC(&pVCpu->hm.s.StatSwitchTprMaskedIrq);
3497 }
3498 else
3499 STAM_COUNTER_INC(&pVCpu->hm.s.StatSwitchGuestIrq);
3500 }
3501
3502 /*
3503 * Check if the nested-guest is intercepting virtual (using V_IRQ and related fields)
3504 * interrupt injection. The virtual interrupt injection itself, if any, will be done
3505 * by the physical CPU.
3506 */
3507#if 0
3508 if ( VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INTERRUPT_NESTED_GUEST)
3509 && (pVmcbNstGstCache->u64InterceptCtrl & SVM_CTRL_INTERCEPT_VINTR)
3510 && CPUMCanSvmNstGstTakeVirtIntr(pCtx))
3511 {
3512 Log4(("Intercepting virtual interrupt -> #VMEXIT\n"));
3513 return IEMExecSvmVmexit(pVCpu, SVM_EXIT_VINTR, 0, 0);
3514 }
3515#endif
3516 }
3517 }
3518
3519 return VINF_SUCCESS;
3520}
3521#endif
3522
3523
3524/**
3525 * Evaluates the event to be delivered to the guest and sets it as the pending
3526 * event.
3527 *
3528 * @param pVCpu The cross context virtual CPU structure.
3529 * @param pCtx Pointer to the guest-CPU context.
3530 *
3531 * @remarks Don't use this function when we are actively executing a
3532 * nested-guest, use hmR0SvmEvaluatePendingEventNested instead.
3533 */
3534static void hmR0SvmEvaluatePendingEvent(PVMCPU pVCpu, PCPUMCTX pCtx)
3535{
3536 HMSVM_ASSERT_NOT_IN_NESTED_GUEST(pCtx);
3537 Assert(!pVCpu->hm.s.Event.fPending);
3538
3539#ifdef VBOX_WITH_NESTED_HWVIRT
3540 bool const fGif = pCtx->hwvirt.svm.fGif;
3541#else
3542 bool const fGif = true;
3543#endif
3544 Log4Func(("fGif=%RTbool\n", fGif));
3545
3546 /*
3547 * If the global interrupt flag (GIF) isn't set, even NMIs and other events are blocked.
3548 * See AMD spec. Table 15-10. "Effect of the GIF on Interrupt Handling".
3549 */
3550 if (fGif)
3551 {
3552 bool const fIntShadow = hmR0SvmIsIntrShadowActive(pVCpu, pCtx);
3553 bool const fBlockInt = !(pCtx->eflags.u32 & X86_EFL_IF);
3554 bool const fBlockNmi = VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_BLOCK_NMIS);
3555 PSVMVMCB pVmcb = pVCpu->hm.s.svm.pVmcb;
3556
3557 Log4Func(("fBlockInt=%RTbool fIntShadow=%RTbool APIC/PIC_Pending=%RTbool\n", fBlockInt, fIntShadow,
3558 VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC)));
3559
3560 /** @todo SMI. SMIs take priority over NMIs. */
3561 if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INTERRUPT_NMI)) /* NMI. NMIs take priority over regular interrupts. */
3562 {
3563 if (fBlockNmi)
3564 hmR0SvmSetIretIntercept(pVmcb);
3565 else if (fIntShadow)
3566 hmR0SvmSetVirtIntrIntercept(pVmcb);
3567 else
3568 {
3569 Log4(("Pending NMI\n"));
3570
3571 SVMEVENT Event;
3572 Event.u = 0;
3573 Event.n.u1Valid = 1;
3574 Event.n.u8Vector = X86_XCPT_NMI;
3575 Event.n.u3Type = SVM_EVENT_NMI;
3576
3577 hmR0SvmSetPendingEvent(pVCpu, &Event, 0 /* GCPtrFaultAddress */);
3578 hmR0SvmSetIretIntercept(pVmcb);
3579 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_NMI);
3580 return;
3581 }
3582 }
3583 else if ( VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC)
3584 && !pVCpu->hm.s.fSingleInstruction)
3585 {
3586 /*
3587 * Check if the guest can receive external interrupts (PIC/APIC). Once PDMGetInterrupt() returns
3588 * a valid interrupt we -must- deliver the interrupt. We can no longer re-request it from the APIC.
3589 */
3590 if ( !fBlockInt
3591 && !fIntShadow)
3592 {
3593 uint8_t u8Interrupt;
3594 int rc = PDMGetInterrupt(pVCpu, &u8Interrupt);
3595 if (RT_SUCCESS(rc))
3596 {
3597 Log4(("Injecting external interrupt u8Interrupt=%#x\n", u8Interrupt));
3598
3599 SVMEVENT Event;
3600 Event.u = 0;
3601 Event.n.u1Valid = 1;
3602 Event.n.u8Vector = u8Interrupt;
3603 Event.n.u3Type = SVM_EVENT_EXTERNAL_IRQ;
3604
3605 hmR0SvmSetPendingEvent(pVCpu, &Event, 0 /* GCPtrFaultAddress */);
3606 }
3607 else if (rc == VERR_APIC_INTR_MASKED_BY_TPR)
3608 {
3609 /*
3610 * AMD-V has no TPR thresholding feature. TPR and the force-flag will be
3611 * updated eventually when the TPR is written by the guest.
3612 */
3613 STAM_COUNTER_INC(&pVCpu->hm.s.StatSwitchTprMaskedIrq);
3614 }
3615 else
3616 STAM_COUNTER_INC(&pVCpu->hm.s.StatSwitchGuestIrq);
3617 }
3618 else
3619 hmR0SvmSetVirtIntrIntercept(pVmcb);
3620 }
3621 }
3622}
3623
3624
3625/**
3626 * Injects any pending events into the guest or nested-guest.
3627 *
3628 * @param pVCpu The cross context virtual CPU structure.
3629 * @param pCtx Pointer to the guest-CPU context.
3630 * @param pVmcb Pointer to the VM control block.
3631 */
3632static void hmR0SvmInjectPendingEvent(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMVMCB pVmcb)
3633{
3634 Assert(!TRPMHasTrap(pVCpu));
3635 Assert(!VMMRZCallRing3IsEnabled(pVCpu));
3636
3637 bool const fIntShadow = hmR0SvmIsIntrShadowActive(pVCpu, pCtx);
3638#ifdef VBOX_STRICT
3639 bool const fGif = pCtx->hwvirt.svm.fGif;
3640 bool fAllowInt = fGif;
3641 if (fGif)
3642 {
3643 if (CPUMIsGuestInSvmNestedHwVirtMode(pCtx))
3644 fAllowInt = CPUMCanSvmNstGstTakePhysIntr(pVCpu, pCtx);
3645 else
3646 fAllowInt = RT_BOOL(pCtx->eflags.u32 & X86_EFL_IF);
3647 }
3648#endif
3649
3650 if (pVCpu->hm.s.Event.fPending)
3651 {
3652 SVMEVENT Event;
3653 Event.u = pVCpu->hm.s.Event.u64IntInfo;
3654 Assert(Event.n.u1Valid);
3655
3656 /*
3657 * Validate event injection pre-conditions.
3658 */
3659 if (Event.n.u3Type == SVM_EVENT_EXTERNAL_IRQ)
3660 {
3661 Assert(fAllowInt);
3662 Assert(!fIntShadow);
3663 }
3664 else if (Event.n.u3Type == SVM_EVENT_NMI)
3665 {
3666 Assert(fGif);
3667 Assert(!fIntShadow);
3668 }
3669
3670 /*
3671 * Inject it (update VMCB for injection by the hardware).
3672 */
3673 Log4(("Injecting pending HM event\n"));
3674 hmR0SvmInjectEventVmcb(pVCpu, pVmcb, pCtx, &Event);
3675 pVCpu->hm.s.Event.fPending = false;
3676
3677 if (Event.n.u3Type == SVM_EVENT_EXTERNAL_IRQ)
3678 STAM_COUNTER_INC(&pVCpu->hm.s.StatInjectInterrupt);
3679 else
3680 STAM_COUNTER_INC(&pVCpu->hm.s.StatInjectXcpt);
3681 }
3682 else
3683 {
3684#ifdef VBOX_WITH_NESTED_HWVIRT
3685 /*
3686 * If IEM emulated VMRUN and injected an event, it would not clear the EVENTINJ::Valid bit
3687 * as a physical CPU clears it in the VMCB as part of the #VMEXIT (if the AMD spec. is to
3688 * believed, real behavior might differ). Regardless, IEM does it only on #VMEXIT for now
3689 * and since we are continuing nested-guest execution using hardware-assisted SVM, we need
3690 * to clear this field otherwise we will inject the event twice, see @bugref{7243#78}.
3691 */
3692 if (CPUMIsGuestInSvmNestedHwVirtMode(pCtx))
3693 pVmcb->ctrl.EventInject.n.u1Valid = 0;
3694#endif
3695 Assert(pVmcb->ctrl.EventInject.n.u1Valid == 0);
3696 }
3697
3698 /*
3699 * Update the guest interrupt shadow in the guest or nested-guest VMCB.
3700 *
3701 * For nested-guests: We need to update it too for the scenario where IEM executes
3702 * the nested-guest but execution later continues here with an interrupt shadow active.
3703 */
3704 pVmcb->ctrl.IntShadow.n.u1IntShadow = fIntShadow;
3705}
3706
3707
3708/**
3709 * Reports world-switch error and dumps some useful debug info.
3710 *
3711 * @param pVM The cross context VM structure.
3712 * @param pVCpu The cross context virtual CPU structure.
3713 * @param rcVMRun The return code from VMRUN (or
3714 * VERR_SVM_INVALID_GUEST_STATE for invalid
3715 * guest-state).
3716 * @param pCtx Pointer to the guest-CPU context.
3717 */
3718static void hmR0SvmReportWorldSwitchError(PVM pVM, PVMCPU pVCpu, int rcVMRun, PCPUMCTX pCtx)
3719{
3720 NOREF(pCtx);
3721 HMSVM_ASSERT_PREEMPT_SAFE();
3722 HMSVM_ASSERT_NOT_IN_NESTED_GUEST(pCtx);
3723 PCSVMVMCB pVmcb = pVCpu->hm.s.svm.pVmcb;
3724
3725 if (rcVMRun == VERR_SVM_INVALID_GUEST_STATE)
3726 {
3727 hmR0DumpRegs(pVM, pVCpu, pCtx); NOREF(pVM);
3728#ifdef VBOX_STRICT
3729 Log4(("ctrl.u32VmcbCleanBits %#RX32\n", pVmcb->ctrl.u32VmcbCleanBits));
3730 Log4(("ctrl.u16InterceptRdCRx %#x\n", pVmcb->ctrl.u16InterceptRdCRx));
3731 Log4(("ctrl.u16InterceptWrCRx %#x\n", pVmcb->ctrl.u16InterceptWrCRx));
3732 Log4(("ctrl.u16InterceptRdDRx %#x\n", pVmcb->ctrl.u16InterceptRdDRx));
3733 Log4(("ctrl.u16InterceptWrDRx %#x\n", pVmcb->ctrl.u16InterceptWrDRx));
3734 Log4(("ctrl.u32InterceptXcpt %#x\n", pVmcb->ctrl.u32InterceptXcpt));
3735 Log4(("ctrl.u64InterceptCtrl %#RX64\n", pVmcb->ctrl.u64InterceptCtrl));
3736 Log4(("ctrl.u64IOPMPhysAddr %#RX64\n", pVmcb->ctrl.u64IOPMPhysAddr));
3737 Log4(("ctrl.u64MSRPMPhysAddr %#RX64\n", pVmcb->ctrl.u64MSRPMPhysAddr));
3738 Log4(("ctrl.u64TSCOffset %#RX64\n", pVmcb->ctrl.u64TSCOffset));
3739
3740 Log4(("ctrl.TLBCtrl.u32ASID %#x\n", pVmcb->ctrl.TLBCtrl.n.u32ASID));
3741 Log4(("ctrl.TLBCtrl.u8TLBFlush %#x\n", pVmcb->ctrl.TLBCtrl.n.u8TLBFlush));
3742 Log4(("ctrl.TLBCtrl.u24Reserved %#x\n", pVmcb->ctrl.TLBCtrl.n.u24Reserved));
3743
3744 Log4(("ctrl.IntCtrl.u8VTPR %#x\n", pVmcb->ctrl.IntCtrl.n.u8VTPR));
3745 Log4(("ctrl.IntCtrl.u1VIrqPending %#x\n", pVmcb->ctrl.IntCtrl.n.u1VIrqPending));
3746 Log4(("ctrl.IntCtrl.u1VGif %#x\n", pVmcb->ctrl.IntCtrl.n.u1VGif));
3747 Log4(("ctrl.IntCtrl.u6Reserved0 %#x\n", pVmcb->ctrl.IntCtrl.n.u6Reserved0));
3748 Log4(("ctrl.IntCtrl.u4VIntrPrio %#x\n", pVmcb->ctrl.IntCtrl.n.u4VIntrPrio));
3749 Log4(("ctrl.IntCtrl.u1IgnoreTPR %#x\n", pVmcb->ctrl.IntCtrl.n.u1IgnoreTPR));
3750 Log4(("ctrl.IntCtrl.u3Reserved %#x\n", pVmcb->ctrl.IntCtrl.n.u3Reserved));
3751 Log4(("ctrl.IntCtrl.u1VIntrMasking %#x\n", pVmcb->ctrl.IntCtrl.n.u1VIntrMasking));
3752 Log4(("ctrl.IntCtrl.u1VGifEnable %#x\n", pVmcb->ctrl.IntCtrl.n.u1VGifEnable));
3753 Log4(("ctrl.IntCtrl.u5Reserved1 %#x\n", pVmcb->ctrl.IntCtrl.n.u5Reserved1));
3754 Log4(("ctrl.IntCtrl.u8VIntrVector %#x\n", pVmcb->ctrl.IntCtrl.n.u8VIntrVector));
3755 Log4(("ctrl.IntCtrl.u24Reserved %#x\n", pVmcb->ctrl.IntCtrl.n.u24Reserved));
3756
3757 Log4(("ctrl.IntShadow.u1IntShadow %#x\n", pVmcb->ctrl.IntShadow.n.u1IntShadow));
3758 Log4(("ctrl.IntShadow.u1GuestIntMask %#x\n", pVmcb->ctrl.IntShadow.n.u1GuestIntMask));
3759 Log4(("ctrl.u64ExitCode %#RX64\n", pVmcb->ctrl.u64ExitCode));
3760 Log4(("ctrl.u64ExitInfo1 %#RX64\n", pVmcb->ctrl.u64ExitInfo1));
3761 Log4(("ctrl.u64ExitInfo2 %#RX64\n", pVmcb->ctrl.u64ExitInfo2));
3762 Log4(("ctrl.ExitIntInfo.u8Vector %#x\n", pVmcb->ctrl.ExitIntInfo.n.u8Vector));
3763 Log4(("ctrl.ExitIntInfo.u3Type %#x\n", pVmcb->ctrl.ExitIntInfo.n.u3Type));
3764 Log4(("ctrl.ExitIntInfo.u1ErrorCodeValid %#x\n", pVmcb->ctrl.ExitIntInfo.n.u1ErrorCodeValid));
3765 Log4(("ctrl.ExitIntInfo.u19Reserved %#x\n", pVmcb->ctrl.ExitIntInfo.n.u19Reserved));
3766 Log4(("ctrl.ExitIntInfo.u1Valid %#x\n", pVmcb->ctrl.ExitIntInfo.n.u1Valid));
3767 Log4(("ctrl.ExitIntInfo.u32ErrorCode %#x\n", pVmcb->ctrl.ExitIntInfo.n.u32ErrorCode));
3768 Log4(("ctrl.NestedPaging.u1NestedPaging %#x\n", pVmcb->ctrl.NestedPaging.n.u1NestedPaging));
3769 Log4(("ctrl.NestedPaging.u1Sev %#x\n", pVmcb->ctrl.NestedPaging.n.u1Sev));
3770 Log4(("ctrl.NestedPaging.u1SevEs %#x\n", pVmcb->ctrl.NestedPaging.n.u1SevEs));
3771 Log4(("ctrl.EventInject.u8Vector %#x\n", pVmcb->ctrl.EventInject.n.u8Vector));
3772 Log4(("ctrl.EventInject.u3Type %#x\n", pVmcb->ctrl.EventInject.n.u3Type));
3773 Log4(("ctrl.EventInject.u1ErrorCodeValid %#x\n", pVmcb->ctrl.EventInject.n.u1ErrorCodeValid));
3774 Log4(("ctrl.EventInject.u19Reserved %#x\n", pVmcb->ctrl.EventInject.n.u19Reserved));
3775 Log4(("ctrl.EventInject.u1Valid %#x\n", pVmcb->ctrl.EventInject.n.u1Valid));
3776 Log4(("ctrl.EventInject.u32ErrorCode %#x\n", pVmcb->ctrl.EventInject.n.u32ErrorCode));
3777
3778 Log4(("ctrl.u64NestedPagingCR3 %#RX64\n", pVmcb->ctrl.u64NestedPagingCR3));
3779
3780 Log4(("ctrl.LbrVirt.u1LbrVirt %#x\n", pVmcb->ctrl.LbrVirt.n.u1LbrVirt));
3781 Log4(("ctrl.LbrVirt.u1VirtVmsaveVmload %#x\n", pVmcb->ctrl.LbrVirt.n.u1VirtVmsaveVmload));
3782
3783 Log4(("guest.CS.u16Sel %RTsel\n", pVmcb->guest.CS.u16Sel));
3784 Log4(("guest.CS.u16Attr %#x\n", pVmcb->guest.CS.u16Attr));
3785 Log4(("guest.CS.u32Limit %#RX32\n", pVmcb->guest.CS.u32Limit));
3786 Log4(("guest.CS.u64Base %#RX64\n", pVmcb->guest.CS.u64Base));
3787 Log4(("guest.DS.u16Sel %#RTsel\n", pVmcb->guest.DS.u16Sel));
3788 Log4(("guest.DS.u16Attr %#x\n", pVmcb->guest.DS.u16Attr));
3789 Log4(("guest.DS.u32Limit %#RX32\n", pVmcb->guest.DS.u32Limit));
3790 Log4(("guest.DS.u64Base %#RX64\n", pVmcb->guest.DS.u64Base));
3791 Log4(("guest.ES.u16Sel %RTsel\n", pVmcb->guest.ES.u16Sel));
3792 Log4(("guest.ES.u16Attr %#x\n", pVmcb->guest.ES.u16Attr));
3793 Log4(("guest.ES.u32Limit %#RX32\n", pVmcb->guest.ES.u32Limit));
3794 Log4(("guest.ES.u64Base %#RX64\n", pVmcb->guest.ES.u64Base));
3795 Log4(("guest.FS.u16Sel %RTsel\n", pVmcb->guest.FS.u16Sel));
3796 Log4(("guest.FS.u16Attr %#x\n", pVmcb->guest.FS.u16Attr));
3797 Log4(("guest.FS.u32Limit %#RX32\n", pVmcb->guest.FS.u32Limit));
3798 Log4(("guest.FS.u64Base %#RX64\n", pVmcb->guest.FS.u64Base));
3799 Log4(("guest.GS.u16Sel %RTsel\n", pVmcb->guest.GS.u16Sel));
3800 Log4(("guest.GS.u16Attr %#x\n", pVmcb->guest.GS.u16Attr));
3801 Log4(("guest.GS.u32Limit %#RX32\n", pVmcb->guest.GS.u32Limit));
3802 Log4(("guest.GS.u64Base %#RX64\n", pVmcb->guest.GS.u64Base));
3803
3804 Log4(("guest.GDTR.u32Limit %#RX32\n", pVmcb->guest.GDTR.u32Limit));
3805 Log4(("guest.GDTR.u64Base %#RX64\n", pVmcb->guest.GDTR.u64Base));
3806
3807 Log4(("guest.LDTR.u16Sel %RTsel\n", pVmcb->guest.LDTR.u16Sel));
3808 Log4(("guest.LDTR.u16Attr %#x\n", pVmcb->guest.LDTR.u16Attr));
3809 Log4(("guest.LDTR.u32Limit %#RX32\n", pVmcb->guest.LDTR.u32Limit));
3810 Log4(("guest.LDTR.u64Base %#RX64\n", pVmcb->guest.LDTR.u64Base));
3811
3812 Log4(("guest.IDTR.u32Limit %#RX32\n", pVmcb->guest.IDTR.u32Limit));
3813 Log4(("guest.IDTR.u64Base %#RX64\n", pVmcb->guest.IDTR.u64Base));
3814
3815 Log4(("guest.TR.u16Sel %RTsel\n", pVmcb->guest.TR.u16Sel));
3816 Log4(("guest.TR.u16Attr %#x\n", pVmcb->guest.TR.u16Attr));
3817 Log4(("guest.TR.u32Limit %#RX32\n", pVmcb->guest.TR.u32Limit));
3818 Log4(("guest.TR.u64Base %#RX64\n", pVmcb->guest.TR.u64Base));
3819
3820 Log4(("guest.u8CPL %#x\n", pVmcb->guest.u8CPL));
3821 Log4(("guest.u64CR0 %#RX64\n", pVmcb->guest.u64CR0));
3822 Log4(("guest.u64CR2 %#RX64\n", pVmcb->guest.u64CR2));
3823 Log4(("guest.u64CR3 %#RX64\n", pVmcb->guest.u64CR3));
3824 Log4(("guest.u64CR4 %#RX64\n", pVmcb->guest.u64CR4));
3825 Log4(("guest.u64DR6 %#RX64\n", pVmcb->guest.u64DR6));
3826 Log4(("guest.u64DR7 %#RX64\n", pVmcb->guest.u64DR7));
3827
3828 Log4(("guest.u64RIP %#RX64\n", pVmcb->guest.u64RIP));
3829 Log4(("guest.u64RSP %#RX64\n", pVmcb->guest.u64RSP));
3830 Log4(("guest.u64RAX %#RX64\n", pVmcb->guest.u64RAX));
3831 Log4(("guest.u64RFlags %#RX64\n", pVmcb->guest.u64RFlags));
3832
3833 Log4(("guest.u64SysEnterCS %#RX64\n", pVmcb->guest.u64SysEnterCS));
3834 Log4(("guest.u64SysEnterEIP %#RX64\n", pVmcb->guest.u64SysEnterEIP));
3835 Log4(("guest.u64SysEnterESP %#RX64\n", pVmcb->guest.u64SysEnterESP));
3836
3837 Log4(("guest.u64EFER %#RX64\n", pVmcb->guest.u64EFER));
3838 Log4(("guest.u64STAR %#RX64\n", pVmcb->guest.u64STAR));
3839 Log4(("guest.u64LSTAR %#RX64\n", pVmcb->guest.u64LSTAR));
3840 Log4(("guest.u64CSTAR %#RX64\n", pVmcb->guest.u64CSTAR));
3841 Log4(("guest.u64SFMASK %#RX64\n", pVmcb->guest.u64SFMASK));
3842 Log4(("guest.u64KernelGSBase %#RX64\n", pVmcb->guest.u64KernelGSBase));
3843 Log4(("guest.u64GPAT %#RX64\n", pVmcb->guest.u64GPAT));
3844 Log4(("guest.u64DBGCTL %#RX64\n", pVmcb->guest.u64DBGCTL));
3845 Log4(("guest.u64BR_FROM %#RX64\n", pVmcb->guest.u64BR_FROM));
3846 Log4(("guest.u64BR_TO %#RX64\n", pVmcb->guest.u64BR_TO));
3847 Log4(("guest.u64LASTEXCPFROM %#RX64\n", pVmcb->guest.u64LASTEXCPFROM));
3848 Log4(("guest.u64LASTEXCPTO %#RX64\n", pVmcb->guest.u64LASTEXCPTO));
3849#endif /* VBOX_STRICT */
3850 }
3851 else
3852 Log4(("hmR0SvmReportWorldSwitchError: rcVMRun=%d\n", rcVMRun));
3853
3854 NOREF(pVmcb);
3855}
3856
3857
3858/**
3859 * Check per-VM and per-VCPU force flag actions that require us to go back to
3860 * ring-3 for one reason or another.
3861 *
3862 * @returns VBox status code (information status code included).
3863 * @retval VINF_SUCCESS if we don't have any actions that require going back to
3864 * ring-3.
3865 * @retval VINF_PGM_SYNC_CR3 if we have pending PGM CR3 sync.
3866 * @retval VINF_EM_PENDING_REQUEST if we have pending requests (like hardware
3867 * interrupts)
3868 * @retval VINF_PGM_POOL_FLUSH_PENDING if PGM is doing a pool flush and requires
3869 * all EMTs to be in ring-3.
3870 * @retval VINF_EM_RAW_TO_R3 if there is pending DMA requests.
3871 * @retval VINF_EM_NO_MEMORY PGM is out of memory, we need to return
3872 * to the EM loop.
3873 *
3874 * @param pVM The cross context VM structure.
3875 * @param pVCpu The cross context virtual CPU structure.
3876 * @param pCtx Pointer to the guest-CPU context.
3877 */
3878static int hmR0SvmCheckForceFlags(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
3879{
3880 Assert(VMMRZCallRing3IsEnabled(pVCpu));
3881
3882 /* On AMD-V we don't need to update CR3, PAE PDPES lazily. See hmR0SvmSaveGuestState(). */
3883 Assert(!VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_HM_UPDATE_CR3));
3884 Assert(!VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_HM_UPDATE_PAE_PDPES));
3885
3886 /* Update pending interrupts into the APIC's IRR. */
3887 if (VMCPU_FF_TEST_AND_CLEAR(pVCpu, VMCPU_FF_UPDATE_APIC))
3888 APICUpdatePendingInterrupts(pVCpu);
3889
3890 if ( VM_FF_IS_PENDING(pVM, !pVCpu->hm.s.fSingleInstruction
3891 ? VM_FF_HP_R0_PRE_HM_MASK : VM_FF_HP_R0_PRE_HM_STEP_MASK)
3892 || VMCPU_FF_IS_PENDING(pVCpu, !pVCpu->hm.s.fSingleInstruction
3893 ? VMCPU_FF_HP_R0_PRE_HM_MASK : VMCPU_FF_HP_R0_PRE_HM_STEP_MASK) )
3894 {
3895 /* Pending PGM C3 sync. */
3896 if (VMCPU_FF_IS_PENDING(pVCpu,VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL))
3897 {
3898 int rc = PGMSyncCR3(pVCpu, pCtx->cr0, pCtx->cr3, pCtx->cr4, VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_PGM_SYNC_CR3));
3899 if (rc != VINF_SUCCESS)
3900 {
3901 Log4(("hmR0SvmCheckForceFlags: PGMSyncCR3 forcing us back to ring-3. rc=%d\n", rc));
3902 return rc;
3903 }
3904 }
3905
3906 /* Pending HM-to-R3 operations (critsects, timers, EMT rendezvous etc.) */
3907 /* -XXX- what was that about single stepping? */
3908 if ( VM_FF_IS_PENDING(pVM, VM_FF_HM_TO_R3_MASK)
3909 || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_HM_TO_R3_MASK))
3910 {
3911 STAM_COUNTER_INC(&pVCpu->hm.s.StatSwitchHmToR3FF);
3912 int rc = RT_UNLIKELY(VM_FF_IS_PENDING(pVM, VM_FF_PGM_NO_MEMORY)) ? VINF_EM_NO_MEMORY : VINF_EM_RAW_TO_R3;
3913 Log4(("hmR0SvmCheckForceFlags: HM_TO_R3 forcing us back to ring-3. rc=%d\n", rc));
3914 return rc;
3915 }
3916
3917 /* Pending VM request packets, such as hardware interrupts. */
3918 if ( VM_FF_IS_PENDING(pVM, VM_FF_REQUEST)
3919 || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_REQUEST))
3920 {
3921 Log4(("hmR0SvmCheckForceFlags: Pending VM request forcing us back to ring-3\n"));
3922 return VINF_EM_PENDING_REQUEST;
3923 }
3924
3925 /* Pending PGM pool flushes. */
3926 if (VM_FF_IS_PENDING(pVM, VM_FF_PGM_POOL_FLUSH_PENDING))
3927 {
3928 Log4(("hmR0SvmCheckForceFlags: PGM pool flush pending forcing us back to ring-3\n"));
3929 return VINF_PGM_POOL_FLUSH_PENDING;
3930 }
3931
3932 /* Pending DMA requests. */
3933 if (VM_FF_IS_PENDING(pVM, VM_FF_PDM_DMA))
3934 {
3935 Log4(("hmR0SvmCheckForceFlags: Pending DMA request forcing us back to ring-3\n"));
3936 return VINF_EM_RAW_TO_R3;
3937 }
3938 }
3939
3940 return VINF_SUCCESS;
3941}
3942
3943
3944#ifdef VBOX_WITH_NESTED_HWVIRT
3945/**
3946 * Does the preparations before executing nested-guest code in AMD-V.
3947 *
3948 * @returns VBox status code (informational status codes included).
3949 * @retval VINF_SUCCESS if we can proceed with running the guest.
3950 * @retval VINF_* scheduling changes, we have to go back to ring-3.
3951 *
3952 * @param pVM The cross context VM structure.
3953 * @param pVCpu The cross context virtual CPU structure.
3954 * @param pCtx Pointer to the guest-CPU context.
3955 * @param pSvmTransient Pointer to the SVM transient structure.
3956 *
3957 * @remarks Same caveats regarding longjumps as hmR0SvmPreRunGuest applies.
3958 * @sa hmR0SvmPreRunGuest.
3959 */
3960static int hmR0SvmPreRunGuestNested(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
3961{
3962 HMSVM_ASSERT_PREEMPT_SAFE();
3963
3964 if (CPUMIsGuestInSvmNestedHwVirtMode(pCtx))
3965 {
3966#ifdef VBOX_WITH_NESTED_HWVIRT_ONLY_IN_IEM
3967 Log2(("hmR0SvmPreRunGuest: Rescheduling to IEM due to nested-hwvirt or forced IEM exec -> VINF_EM_RESCHEDULE_REM\n"));
3968 return VINF_EM_RESCHEDULE_REM;
3969#endif
3970 }
3971 else
3972 return VINF_SVM_VMEXIT;
3973
3974 /* Check force flag actions that might require us to go back to ring-3. */
3975 int rc = hmR0SvmCheckForceFlags(pVM, pVCpu, pCtx);
3976 if (rc != VINF_SUCCESS)
3977 return rc;
3978
3979 if (TRPMHasTrap(pVCpu))
3980 hmR0SvmTrpmTrapToPendingEvent(pVCpu);
3981 else if (!pVCpu->hm.s.Event.fPending)
3982 {
3983 VBOXSTRICTRC rcStrict = hmR0SvmEvaluatePendingEventNested(pVCpu, pCtx);
3984 if (rcStrict != VINF_SUCCESS)
3985 return VBOXSTRICTRC_VAL(rcStrict);
3986 if (!CPUMIsGuestInSvmNestedHwVirtMode(pCtx))
3987 return VINF_SVM_VMEXIT;
3988 }
3989
3990 /*
3991 * On the oldest AMD-V systems, we may not get enough information to reinject an NMI.
3992 * Just do it in software, see @bugref{8411}.
3993 * NB: If we could continue a task switch exit we wouldn't need to do this.
3994 */
3995 if (RT_UNLIKELY( !pVM->hm.s.svm.u32Features
3996 && pVCpu->hm.s.Event.fPending
3997 && SVM_EVENT_GET_TYPE(pVCpu->hm.s.Event.u64IntInfo) == SVM_EVENT_NMI))
3998 {
3999 return VINF_EM_RAW_INJECT_TRPM_EVENT;
4000 }
4001
4002#ifdef HMSVM_SYNC_FULL_NESTED_GUEST_STATE
4003 HMCPU_CF_SET(pVCpu, HM_CHANGED_ALL_GUEST);
4004#endif
4005
4006 /*
4007 * Load the nested-guest state.
4008 */
4009 rc = hmR0SvmLoadGuestStateNested(pVCpu, pCtx);
4010 AssertRCReturn(rc, rc);
4011 STAM_COUNTER_INC(&pVCpu->hm.s.StatLoadFull); /** @todo Get new STAM counter for this? */
4012
4013 /* Ensure we've cached (and hopefully modified) the VMCB for execution using hardware SVM. */
4014 Assert(pCtx->hwvirt.svm.fHMCachedVmcb);
4015
4016 /*
4017 * No longjmps to ring-3 from this point on!!!
4018 * Asserts() will still longjmp to ring-3 (but won't return), which is intentional, better than a kernel panic.
4019 * This also disables flushing of the R0-logger instance (if any).
4020 */
4021 VMMRZCallRing3Disable(pVCpu);
4022
4023 /*
4024 * We disable interrupts so that we don't miss any interrupts that would flag preemption (IPI/timers etc.)
4025 * when thread-context hooks aren't used and we've been running with preemption disabled for a while.
4026 *
4027 * We need to check for force-flags that could've possible been altered since we last checked them (e.g.
4028 * by PDMGetInterrupt() leaving the PDM critical section, see @bugref{6398}).
4029 *
4030 * We also check a couple of other force-flags as a last opportunity to get the EMT back to ring-3 before
4031 * executing guest code.
4032 */
4033 pSvmTransient->fEFlags = ASMIntDisableFlags();
4034 if ( VM_FF_IS_PENDING(pVM, VM_FF_EMT_RENDEZVOUS | VM_FF_TM_VIRTUAL_SYNC)
4035 || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_HM_TO_R3_MASK))
4036 {
4037 ASMSetFlags(pSvmTransient->fEFlags);
4038 VMMRZCallRing3Enable(pVCpu);
4039 STAM_COUNTER_INC(&pVCpu->hm.s.StatSwitchHmToR3FF);
4040 return VINF_EM_RAW_TO_R3;
4041 }
4042 if (RTThreadPreemptIsPending(NIL_RTTHREAD))
4043 {
4044 ASMSetFlags(pSvmTransient->fEFlags);
4045 VMMRZCallRing3Enable(pVCpu);
4046 STAM_COUNTER_INC(&pVCpu->hm.s.StatPendingHostIrq);
4047 return VINF_EM_RAW_INTERRUPT;
4048 }
4049
4050 /*
4051 * If we are injecting an NMI, we must set VMCPU_FF_BLOCK_NMIS only when we are going to execute
4052 * guest code for certain (no exits to ring-3). Otherwise, we could re-read the flag on re-entry into
4053 * AMD-V and conclude that NMI inhibition is active when we have not even delivered the NMI.
4054 *
4055 * With VT-x, this is handled by the Guest interruptibility information VMCS field which will set the
4056 * VMCS field after actually delivering the NMI which we read on VM-exit to determine the state.
4057 */
4058 if (pVCpu->hm.s.Event.fPending)
4059 {
4060 SVMEVENT Event;
4061 Event.u = pVCpu->hm.s.Event.u64IntInfo;
4062 if ( Event.n.u1Valid
4063 && Event.n.u3Type == SVM_EVENT_NMI
4064 && Event.n.u8Vector == X86_XCPT_NMI
4065 && !VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_BLOCK_NMIS))
4066 {
4067 VMCPU_FF_SET(pVCpu, VMCPU_FF_BLOCK_NMIS);
4068 }
4069 }
4070
4071 return VINF_SUCCESS;
4072}
4073#endif
4074
4075
4076/**
4077 * Does the preparations before executing guest code in AMD-V.
4078 *
4079 * This may cause longjmps to ring-3 and may even result in rescheduling to the
4080 * recompiler. We must be cautious what we do here regarding committing
4081 * guest-state information into the VMCB assuming we assuredly execute the guest
4082 * in AMD-V. If we fall back to the recompiler after updating the VMCB and
4083 * clearing the common-state (TRPM/forceflags), we must undo those changes so
4084 * that the recompiler can (and should) use them when it resumes guest
4085 * execution. Otherwise such operations must be done when we can no longer
4086 * exit to ring-3.
4087 *
4088 * @returns VBox status code (informational status codes included).
4089 * @retval VINF_SUCCESS if we can proceed with running the guest.
4090 * @retval VINF_* scheduling changes, we have to go back to ring-3.
4091 *
4092 * @param pVM The cross context VM structure.
4093 * @param pVCpu The cross context virtual CPU structure.
4094 * @param pCtx Pointer to the guest-CPU context.
4095 * @param pSvmTransient Pointer to the SVM transient structure.
4096 */
4097static int hmR0SvmPreRunGuest(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
4098{
4099 HMSVM_ASSERT_PREEMPT_SAFE();
4100 HMSVM_ASSERT_NOT_IN_NESTED_GUEST(pCtx);
4101
4102 /* Check force flag actions that might require us to go back to ring-3. */
4103 int rc = hmR0SvmCheckForceFlags(pVM, pVCpu, pCtx);
4104 if (rc != VINF_SUCCESS)
4105 return rc;
4106
4107 if (TRPMHasTrap(pVCpu))
4108 hmR0SvmTrpmTrapToPendingEvent(pVCpu);
4109 else if (!pVCpu->hm.s.Event.fPending)
4110 hmR0SvmEvaluatePendingEvent(pVCpu, pCtx);
4111
4112 /*
4113 * On the oldest AMD-V systems, we may not get enough information to reinject an NMI.
4114 * Just do it in software, see @bugref{8411}.
4115 * NB: If we could continue a task switch exit we wouldn't need to do this.
4116 */
4117 if (RT_UNLIKELY(pVCpu->hm.s.Event.fPending && (((pVCpu->hm.s.Event.u64IntInfo >> 8) & 7) == SVM_EVENT_NMI)))
4118 if (RT_UNLIKELY(!pVM->hm.s.svm.u32Features))
4119 return VINF_EM_RAW_INJECT_TRPM_EVENT;
4120
4121#ifdef HMSVM_SYNC_FULL_GUEST_STATE
4122 HMCPU_CF_SET(pVCpu, HM_CHANGED_ALL_GUEST);
4123#endif
4124
4125 /* Load the guest bits that are not shared with the host in any way since we can longjmp or get preempted. */
4126 rc = hmR0SvmLoadGuestState(pVM, pVCpu, pCtx);
4127 AssertRCReturn(rc, rc);
4128 STAM_COUNTER_INC(&pVCpu->hm.s.StatLoadFull);
4129
4130 /*
4131 * If we're not intercepting TPR changes in the guest, save the guest TPR before the world-switch
4132 * so we can update it on the way back if the guest changed the TPR.
4133 */
4134 if (pVCpu->hm.s.svm.fSyncVTpr)
4135 {
4136 if (pVM->hm.s.fTPRPatchingActive)
4137 pSvmTransient->u8GuestTpr = pCtx->msrLSTAR;
4138 else
4139 {
4140 PCSVMVMCB pVmcb = pVCpu->hm.s.svm.pVmcb;
4141 pSvmTransient->u8GuestTpr = pVmcb->ctrl.IntCtrl.n.u8VTPR;
4142 }
4143 }
4144
4145 /*
4146 * No longjmps to ring-3 from this point on!!!
4147 * Asserts() will still longjmp to ring-3 (but won't return), which is intentional, better than a kernel panic.
4148 * This also disables flushing of the R0-logger instance (if any).
4149 */
4150 VMMRZCallRing3Disable(pVCpu);
4151
4152 /*
4153 * We disable interrupts so that we don't miss any interrupts that would flag preemption (IPI/timers etc.)
4154 * when thread-context hooks aren't used and we've been running with preemption disabled for a while.
4155 *
4156 * We need to check for force-flags that could've possible been altered since we last checked them (e.g.
4157 * by PDMGetInterrupt() leaving the PDM critical section, see @bugref{6398}).
4158 *
4159 * We also check a couple of other force-flags as a last opportunity to get the EMT back to ring-3 before
4160 * executing guest code.
4161 */
4162 pSvmTransient->fEFlags = ASMIntDisableFlags();
4163 if ( VM_FF_IS_PENDING(pVM, VM_FF_EMT_RENDEZVOUS | VM_FF_TM_VIRTUAL_SYNC)
4164 || VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_HM_TO_R3_MASK))
4165 {
4166 ASMSetFlags(pSvmTransient->fEFlags);
4167 VMMRZCallRing3Enable(pVCpu);
4168 STAM_COUNTER_INC(&pVCpu->hm.s.StatSwitchHmToR3FF);
4169 return VINF_EM_RAW_TO_R3;
4170 }
4171 if (RTThreadPreemptIsPending(NIL_RTTHREAD))
4172 {
4173 ASMSetFlags(pSvmTransient->fEFlags);
4174 VMMRZCallRing3Enable(pVCpu);
4175 STAM_COUNTER_INC(&pVCpu->hm.s.StatPendingHostIrq);
4176 return VINF_EM_RAW_INTERRUPT;
4177 }
4178
4179 /*
4180 * If we are injecting an NMI, we must set VMCPU_FF_BLOCK_NMIS only when we are going to execute
4181 * guest code for certain (no exits to ring-3). Otherwise, we could re-read the flag on re-entry into
4182 * AMD-V and conclude that NMI inhibition is active when we have not even delivered the NMI.
4183 *
4184 * With VT-x, this is handled by the Guest interruptibility information VMCS field which will set the
4185 * VMCS field after actually delivering the NMI which we read on VM-exit to determine the state.
4186 */
4187 if (pVCpu->hm.s.Event.fPending)
4188 {
4189 SVMEVENT Event;
4190 Event.u = pVCpu->hm.s.Event.u64IntInfo;
4191 if ( Event.n.u1Valid
4192 && Event.n.u3Type == SVM_EVENT_NMI
4193 && Event.n.u8Vector == X86_XCPT_NMI
4194 && !VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_BLOCK_NMIS))
4195 {
4196 VMCPU_FF_SET(pVCpu, VMCPU_FF_BLOCK_NMIS);
4197 }
4198 }
4199
4200 return VINF_SUCCESS;
4201}
4202
4203
4204#ifdef VBOX_WITH_NESTED_HWVIRT
4205/**
4206 * Prepares to run nested-guest code in AMD-V and we've committed to doing so. This
4207 * means there is no backing out to ring-3 or anywhere else at this point.
4208 *
4209 * @param pVM The cross context VM structure.
4210 * @param pVCpu The cross context virtual CPU structure.
4211 * @param pCtx Pointer to the guest-CPU context.
4212 * @param pSvmTransient Pointer to the SVM transient structure.
4213 *
4214 * @remarks Called with preemption disabled.
4215 * @remarks No-long-jump zone!!!
4216 */
4217static void hmR0SvmPreRunGuestCommittedNested(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
4218{
4219 Assert(!VMMRZCallRing3IsEnabled(pVCpu));
4220 Assert(VMMR0IsLogFlushDisabled(pVCpu));
4221 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
4222 HMSVM_ASSERT_IN_NESTED_GUEST(pCtx);
4223
4224 VMCPU_ASSERT_STATE(pVCpu, VMCPUSTATE_STARTED_HM);
4225 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED_EXEC); /* Indicate the start of guest execution. */
4226
4227 PSVMVMCB pVmcbNstGst = pCtx->hwvirt.svm.CTX_SUFF(pVmcb);
4228 hmR0SvmInjectPendingEvent(pVCpu, pCtx, pVmcbNstGst);
4229
4230 if ( pVCpu->hm.s.fPreloadGuestFpu
4231 && !CPUMIsGuestFPUStateActive(pVCpu))
4232 {
4233 CPUMR0LoadGuestFPU(pVM, pVCpu); /* (Ignore rc, no need to set HM_CHANGED_HOST_CONTEXT for SVM.) */
4234 HMCPU_CF_SET(pVCpu, HM_CHANGED_GUEST_CR0);
4235 }
4236
4237 /* Load the state shared between host and nested-guest (FPU, debug). */
4238 if (HMCPU_CF_IS_PENDING(pVCpu, HM_CHANGED_HOST_GUEST_SHARED_STATE))
4239 hmR0SvmLoadSharedState(pVCpu, pVmcbNstGst, pCtx);
4240
4241 HMCPU_CF_CLEAR(pVCpu, HM_CHANGED_HOST_CONTEXT); /* Preemption might set this, nothing to do on AMD-V. */
4242 AssertMsg(!HMCPU_CF_VALUE(pVCpu), ("fContextUseFlags=%#RX32\n", HMCPU_CF_VALUE(pVCpu)));
4243
4244 /* Setup TSC offsetting. */
4245 RTCPUID idCurrentCpu = hmR0GetCurrentCpu()->idCpu;
4246 if ( pSvmTransient->fUpdateTscOffsetting
4247 || idCurrentCpu != pVCpu->hm.s.idLastCpu)
4248 {
4249 hmR0SvmUpdateTscOffsettingNested(pVM, pVCpu, pCtx, pVmcbNstGst);
4250 pSvmTransient->fUpdateTscOffsetting = false;
4251 }
4252
4253 /* If we've migrating CPUs, mark the VMCB Clean bits as dirty. */
4254 if (idCurrentCpu != pVCpu->hm.s.idLastCpu)
4255 pVmcbNstGst->ctrl.u32VmcbCleanBits = 0;
4256
4257 /* Store status of the shared guest-host state at the time of VMRUN. */
4258#if HC_ARCH_BITS == 32 && defined(VBOX_WITH_64_BITS_GUESTS)
4259 if (CPUMIsGuestInLongModeEx(pCtx))
4260 {
4261 pSvmTransient->fWasGuestDebugStateActive = CPUMIsGuestDebugStateActivePending(pVCpu);
4262 pSvmTransient->fWasHyperDebugStateActive = CPUMIsHyperDebugStateActivePending(pVCpu);
4263 }
4264 else
4265#endif
4266 {
4267 pSvmTransient->fWasGuestDebugStateActive = CPUMIsGuestDebugStateActive(pVCpu);
4268 pSvmTransient->fWasHyperDebugStateActive = CPUMIsHyperDebugStateActive(pVCpu);
4269 }
4270 pSvmTransient->fWasGuestFPUStateActive = CPUMIsGuestFPUStateActive(pVCpu);
4271
4272 /* The TLB flushing would've already been setup by the nested-hypervisor. */
4273 ASMAtomicWriteBool(&pVCpu->hm.s.fCheckedTLBFlush, true); /* Used for TLB flushing, set this across the world switch. */
4274 hmR0SvmFlushTaggedTlb(pVCpu, pCtx, pVmcbNstGst);
4275 Assert(hmR0GetCurrentCpu()->idCpu == pVCpu->hm.s.idLastCpu);
4276
4277 STAM_PROFILE_ADV_STOP_START(&pVCpu->hm.s.StatEntry, &pVCpu->hm.s.StatInGC, x);
4278
4279 TMNotifyStartOfExecution(pVCpu); /* Finally, notify TM to resume its clocks as we're about
4280 to start executing. */
4281
4282 /*
4283 * Save the current Host TSC_AUX and write the guest TSC_AUX to the host, so that
4284 * RDTSCPs (that don't cause exits) reads the guest MSR. See @bugref{3324}.
4285 *
4286 * This should be done -after- any RDTSCPs for obtaining the host timestamp (TM, STAM etc).
4287 */
4288 uint8_t *pbMsrBitmap = (uint8_t *)pCtx->hwvirt.svm.CTX_SUFF(pvMsrBitmap);
4289 if ( (pVM->hm.s.cpuid.u32AMDFeatureEDX & X86_CPUID_EXT_FEATURE_EDX_RDTSCP)
4290 && !(pVmcbNstGst->ctrl.u64InterceptCtrl & SVM_CTRL_INTERCEPT_RDTSCP))
4291 {
4292 hmR0SvmSetMsrPermission(pVmcbNstGst, pbMsrBitmap, MSR_K8_TSC_AUX, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
4293 pVCpu->hm.s.u64HostTscAux = ASMRdMsr(MSR_K8_TSC_AUX);
4294 uint64_t u64GuestTscAux = CPUMR0GetGuestTscAux(pVCpu);
4295 if (u64GuestTscAux != pVCpu->hm.s.u64HostTscAux)
4296 ASMWrMsr(MSR_K8_TSC_AUX, u64GuestTscAux);
4297 pSvmTransient->fRestoreTscAuxMsr = true;
4298 }
4299 else
4300 {
4301 hmR0SvmSetMsrPermission(pVmcbNstGst, pbMsrBitmap, MSR_K8_TSC_AUX, SVMMSREXIT_INTERCEPT_READ, SVMMSREXIT_INTERCEPT_WRITE);
4302 pSvmTransient->fRestoreTscAuxMsr = false;
4303 }
4304
4305 /*
4306 * If VMCB Clean bits isn't supported by the CPU or exposed by the guest,
4307 * mark all state-bits as dirty indicating to the CPU to re-load from VMCB.
4308 */
4309 bool const fSupportsVmcbCleanBits = hmR0SvmSupportsVmcbCleanBits(pVCpu, pCtx);
4310 if (!fSupportsVmcbCleanBits)
4311 pVmcbNstGst->ctrl.u32VmcbCleanBits = 0;
4312}
4313#endif
4314
4315
4316/**
4317 * Prepares to run guest code in AMD-V and we've committed to doing so. This
4318 * means there is no backing out to ring-3 or anywhere else at this
4319 * point.
4320 *
4321 * @param pVM The cross context VM structure.
4322 * @param pVCpu The cross context virtual CPU structure.
4323 * @param pCtx Pointer to the guest-CPU context.
4324 * @param pSvmTransient Pointer to the SVM transient structure.
4325 *
4326 * @remarks Called with preemption disabled.
4327 * @remarks No-long-jump zone!!!
4328 */
4329static void hmR0SvmPreRunGuestCommitted(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
4330{
4331 Assert(!VMMRZCallRing3IsEnabled(pVCpu));
4332 Assert(VMMR0IsLogFlushDisabled(pVCpu));
4333 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
4334 HMSVM_ASSERT_NOT_IN_NESTED_GUEST(pCtx);
4335
4336 VMCPU_ASSERT_STATE(pVCpu, VMCPUSTATE_STARTED_HM);
4337 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED_EXEC); /* Indicate the start of guest execution. */
4338
4339 PSVMVMCB pVmcb = pVCpu->hm.s.svm.pVmcb;
4340 hmR0SvmInjectPendingEvent(pVCpu, pCtx, pVmcb);
4341
4342 if ( pVCpu->hm.s.fPreloadGuestFpu
4343 && !CPUMIsGuestFPUStateActive(pVCpu))
4344 {
4345 CPUMR0LoadGuestFPU(pVM, pVCpu); /* (Ignore rc, no need to set HM_CHANGED_HOST_CONTEXT for SVM.) */
4346 HMCPU_CF_SET(pVCpu, HM_CHANGED_GUEST_CR0);
4347 }
4348
4349 /* Load the state shared between host and guest (FPU, debug). */
4350 if (HMCPU_CF_IS_PENDING(pVCpu, HM_CHANGED_HOST_GUEST_SHARED_STATE))
4351 hmR0SvmLoadSharedState(pVCpu, pVmcb, pCtx);
4352
4353 HMCPU_CF_CLEAR(pVCpu, HM_CHANGED_HOST_CONTEXT); /* Preemption might set this, nothing to do on AMD-V. */
4354 AssertMsg(!HMCPU_CF_VALUE(pVCpu), ("fContextUseFlags=%#RX32\n", HMCPU_CF_VALUE(pVCpu)));
4355
4356 /* Setup TSC offsetting. */
4357 RTCPUID idCurrentCpu = hmR0GetCurrentCpu()->idCpu;
4358 if ( pSvmTransient->fUpdateTscOffsetting
4359 || idCurrentCpu != pVCpu->hm.s.idLastCpu)
4360 {
4361 hmR0SvmUpdateTscOffsetting(pVM, pVCpu, pVmcb);
4362 pSvmTransient->fUpdateTscOffsetting = false;
4363 }
4364
4365 /* If we've migrating CPUs, mark the VMCB Clean bits as dirty. */
4366 if (idCurrentCpu != pVCpu->hm.s.idLastCpu)
4367 pVmcb->ctrl.u32VmcbCleanBits = 0;
4368
4369 /* Store status of the shared guest-host state at the time of VMRUN. */
4370#if HC_ARCH_BITS == 32 && defined(VBOX_WITH_64_BITS_GUESTS)
4371 if (CPUMIsGuestInLongModeEx(pCtx))
4372 {
4373 pSvmTransient->fWasGuestDebugStateActive = CPUMIsGuestDebugStateActivePending(pVCpu);
4374 pSvmTransient->fWasHyperDebugStateActive = CPUMIsHyperDebugStateActivePending(pVCpu);
4375 }
4376 else
4377#endif
4378 {
4379 pSvmTransient->fWasGuestDebugStateActive = CPUMIsGuestDebugStateActive(pVCpu);
4380 pSvmTransient->fWasHyperDebugStateActive = CPUMIsHyperDebugStateActive(pVCpu);
4381 }
4382 pSvmTransient->fWasGuestFPUStateActive = CPUMIsGuestFPUStateActive(pVCpu);
4383
4384 /* Flush the appropriate tagged-TLB entries. */
4385 ASMAtomicWriteBool(&pVCpu->hm.s.fCheckedTLBFlush, true); /* Used for TLB flushing, set this across the world switch. */
4386 hmR0SvmFlushTaggedTlb(pVCpu, pCtx, pVmcb);
4387 Assert(hmR0GetCurrentCpu()->idCpu == pVCpu->hm.s.idLastCpu);
4388
4389 STAM_PROFILE_ADV_STOP_START(&pVCpu->hm.s.StatEntry, &pVCpu->hm.s.StatInGC, x);
4390
4391 TMNotifyStartOfExecution(pVCpu); /* Finally, notify TM to resume its clocks as we're about
4392 to start executing. */
4393
4394 /*
4395 * Save the current Host TSC_AUX and write the guest TSC_AUX to the host, so that
4396 * RDTSCPs (that don't cause exits) reads the guest MSR. See @bugref{3324}.
4397 *
4398 * This should be done -after- any RDTSCPs for obtaining the host timestamp (TM, STAM etc).
4399 */
4400 uint8_t *pbMsrBitmap = (uint8_t *)pVCpu->hm.s.svm.pvMsrBitmap;
4401 if ( (pVM->hm.s.cpuid.u32AMDFeatureEDX & X86_CPUID_EXT_FEATURE_EDX_RDTSCP)
4402 && !(pVmcb->ctrl.u64InterceptCtrl & SVM_CTRL_INTERCEPT_RDTSCP))
4403 {
4404 hmR0SvmSetMsrPermission(pVmcb, pbMsrBitmap, MSR_K8_TSC_AUX, SVMMSREXIT_PASSTHRU_READ, SVMMSREXIT_PASSTHRU_WRITE);
4405 pVCpu->hm.s.u64HostTscAux = ASMRdMsr(MSR_K8_TSC_AUX);
4406 uint64_t u64GuestTscAux = CPUMR0GetGuestTscAux(pVCpu);
4407 if (u64GuestTscAux != pVCpu->hm.s.u64HostTscAux)
4408 ASMWrMsr(MSR_K8_TSC_AUX, u64GuestTscAux);
4409 pSvmTransient->fRestoreTscAuxMsr = true;
4410 }
4411 else
4412 {
4413 hmR0SvmSetMsrPermission(pVmcb, pbMsrBitmap, MSR_K8_TSC_AUX, SVMMSREXIT_INTERCEPT_READ, SVMMSREXIT_INTERCEPT_WRITE);
4414 pSvmTransient->fRestoreTscAuxMsr = false;
4415 }
4416
4417 /* If VMCB Clean bits isn't supported by the CPU, simply mark all state-bits as dirty, indicating (re)load-from-VMCB. */
4418 bool const fSupportsVmcbCleanBits = hmR0SvmSupportsVmcbCleanBits(pVCpu, pCtx);
4419 if (!fSupportsVmcbCleanBits)
4420 pVmcb->ctrl.u32VmcbCleanBits = 0;
4421}
4422
4423
4424/**
4425 * Wrapper for running the guest code in AMD-V.
4426 *
4427 * @returns VBox strict status code.
4428 * @param pVM The cross context VM structure.
4429 * @param pVCpu The cross context virtual CPU structure.
4430 * @param pCtx Pointer to the guest-CPU context.
4431 *
4432 * @remarks No-long-jump zone!!!
4433 */
4434DECLINLINE(int) hmR0SvmRunGuest(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
4435{
4436 /*
4437 * 64-bit Windows uses XMM registers in the kernel as the Microsoft compiler expresses floating-point operations
4438 * using SSE instructions. Some XMM registers (XMM6-XMM15) are callee-saved and thus the need for this XMM wrapper.
4439 * Refer MSDN docs. "Configuring Programs for 64-bit / x64 Software Conventions / Register Usage" for details.
4440 */
4441#ifdef VBOX_WITH_KERNEL_USING_XMM
4442 return hmR0SVMRunWrapXMM(pVCpu->hm.s.svm.HCPhysVmcbHost, pVCpu->hm.s.svm.HCPhysVmcb, pCtx, pVM, pVCpu,
4443 pVCpu->hm.s.svm.pfnVMRun);
4444#else
4445 return pVCpu->hm.s.svm.pfnVMRun(pVCpu->hm.s.svm.HCPhysVmcbHost, pVCpu->hm.s.svm.HCPhysVmcb, pCtx, pVM, pVCpu);
4446#endif
4447}
4448
4449
4450#ifdef VBOX_WITH_NESTED_HWVIRT
4451/**
4452 * Wrapper for running the nested-guest code in AMD-V.
4453 *
4454 * @returns VBox strict status code.
4455 * @param pVM The cross context VM structure.
4456 * @param pVCpu The cross context virtual CPU structure.
4457 * @param pCtx Pointer to the guest-CPU context.
4458 *
4459 * @remarks No-long-jump zone!!!
4460 */
4461DECLINLINE(int) hmR0SvmRunGuestNested(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
4462{
4463 /*
4464 * 64-bit Windows uses XMM registers in the kernel as the Microsoft compiler expresses floating-point operations
4465 * using SSE instructions. Some XMM registers (XMM6-XMM15) are callee-saved and thus the need for this XMM wrapper.
4466 * Refer MSDN docs. "Configuring Programs for 64-bit / x64 Software Conventions / Register Usage" for details.
4467 */
4468#ifdef VBOX_WITH_KERNEL_USING_XMM
4469 return hmR0SVMRunWrapXMM(pVCpu->hm.s.svm.HCPhysVmcbHost, pCtx->hwvirt.svm.HCPhysVmcb, pCtx, pVM, pVCpu,
4470 pVCpu->hm.s.svm.pfnVMRun);
4471#else
4472 return pVCpu->hm.s.svm.pfnVMRun(pVCpu->hm.s.svm.HCPhysVmcbHost, pCtx->hwvirt.svm.HCPhysVmcb, pCtx, pVM, pVCpu);
4473#endif
4474}
4475
4476
4477/**
4478 * Performs some essential restoration of state after running nested-guest code in
4479 * AMD-V.
4480 *
4481 * @param pVM The cross context VM structure.
4482 * @param pVCpu The cross context virtual CPU structure.
4483 * @param pMixedCtx Pointer to the nested-guest-CPU context. The data maybe
4484 * out-of-sync. Make sure to update the required fields
4485 * before using them.
4486 * @param pSvmTransient Pointer to the SVM transient structure.
4487 * @param rcVMRun Return code of VMRUN.
4488 *
4489 * @remarks Called with interrupts disabled.
4490 * @remarks No-long-jump zone!!! This function will however re-enable longjmps
4491 * unconditionally when it is safe to do so.
4492 */
4493static void hmR0SvmPostRunGuestNested(PVM pVM, PVMCPU pVCpu, PCPUMCTX pMixedCtx, PSVMTRANSIENT pSvmTransient, int rcVMRun)
4494{
4495 RT_NOREF(pVM);
4496 Assert(!VMMRZCallRing3IsEnabled(pVCpu));
4497
4498 ASMAtomicWriteBool(&pVCpu->hm.s.fCheckedTLBFlush, false); /* See HMInvalidatePageOnAllVCpus(): used for TLB flushing. */
4499 ASMAtomicIncU32(&pVCpu->hm.s.cWorldSwitchExits); /* Initialized in vmR3CreateUVM(): used for EMT poking. */
4500
4501 /* TSC read must be done early for maximum accuracy. */
4502 PSVMVMCB pVmcbNstGst = pMixedCtx->hwvirt.svm.CTX_SUFF(pVmcb);
4503 PSVMVMCBCTRL pVmcbNstGstCtrl = &pVmcbNstGst->ctrl;
4504 PCSVMNESTEDVMCBCACHE pVmcbNstGstCache = &pVCpu->hm.s.svm.NstGstVmcbCache;
4505 if (!(pVmcbNstGstCtrl->u64InterceptCtrl & SVM_CTRL_INTERCEPT_RDTSC))
4506 {
4507 /*
4508 * Undo what we did in hmR0SvmUpdateTscOffsettingNested() but don't restore the
4509 * nested-guest VMCB TSC offset here. It shall eventually be restored on #VMEXIT
4510 * later by HMSvmNstGstVmExitNotify().
4511 */
4512 TMCpuTickSetLastSeen(pVCpu, ASMReadTSC() + pVmcbNstGstCtrl->u64TSCOffset - pVmcbNstGstCache->u64TSCOffset);
4513 }
4514
4515 if (pSvmTransient->fRestoreTscAuxMsr)
4516 {
4517 uint64_t u64GuestTscAuxMsr = ASMRdMsr(MSR_K8_TSC_AUX);
4518 CPUMR0SetGuestTscAux(pVCpu, u64GuestTscAuxMsr);
4519 if (u64GuestTscAuxMsr != pVCpu->hm.s.u64HostTscAux)
4520 ASMWrMsr(MSR_K8_TSC_AUX, pVCpu->hm.s.u64HostTscAux);
4521 }
4522
4523 STAM_PROFILE_ADV_STOP_START(&pVCpu->hm.s.StatInGC, &pVCpu->hm.s.StatExit1, x);
4524 TMNotifyEndOfExecution(pVCpu); /* Notify TM that the guest is no longer running. */
4525 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED_HM);
4526
4527 Assert(!(ASMGetFlags() & X86_EFL_IF));
4528 ASMSetFlags(pSvmTransient->fEFlags); /* Enable interrupts. */
4529 VMMRZCallRing3Enable(pVCpu); /* It is now safe to do longjmps to ring-3!!! */
4530
4531 /* Mark the VMCB-state cache as unmodified by VMM. */
4532 pVmcbNstGstCtrl->u32VmcbCleanBits = HMSVM_VMCB_CLEAN_ALL;
4533
4534 /* If VMRUN failed, we can bail out early. This does -not- cover SVM_EXIT_INVALID. */
4535 if (RT_UNLIKELY(rcVMRun != VINF_SUCCESS))
4536 {
4537 Log4(("VMRUN failure: rcVMRun=%Rrc\n", rcVMRun));
4538 return;
4539 }
4540
4541 pSvmTransient->u64ExitCode = pVmcbNstGstCtrl->u64ExitCode; /* Save the #VMEXIT reason. */
4542 HMCPU_EXIT_HISTORY_ADD(pVCpu, pVmcbNstGstCtrl->u64ExitCode);/* Update the #VMEXIT history array. */
4543 pSvmTransient->fVectoringDoublePF = false; /* Vectoring double page-fault needs to be determined later. */
4544 pSvmTransient->fVectoringPF = false; /* Vectoring page-fault needs to be determined later. */
4545
4546 Assert(!pVCpu->hm.s.svm.fSyncVTpr);
4547 hmR0SvmSaveGuestState(pVCpu, pMixedCtx, pVmcbNstGst); /* Save the nested-guest state from the VMCB to the
4548 guest-CPU context. */
4549}
4550#endif
4551
4552/**
4553 * Performs some essential restoration of state after running guest code in
4554 * AMD-V.
4555 *
4556 * @param pVM The cross context VM structure.
4557 * @param pVCpu The cross context virtual CPU structure.
4558 * @param pMixedCtx Pointer to the guest-CPU context. The data maybe
4559 * out-of-sync. Make sure to update the required fields
4560 * before using them.
4561 * @param pSvmTransient Pointer to the SVM transient structure.
4562 * @param rcVMRun Return code of VMRUN.
4563 *
4564 * @remarks Called with interrupts disabled.
4565 * @remarks No-long-jump zone!!! This function will however re-enable longjmps
4566 * unconditionally when it is safe to do so.
4567 */
4568static void hmR0SvmPostRunGuest(PVM pVM, PVMCPU pVCpu, PCPUMCTX pMixedCtx, PSVMTRANSIENT pSvmTransient, int rcVMRun)
4569{
4570 Assert(!VMMRZCallRing3IsEnabled(pVCpu));
4571
4572 ASMAtomicWriteBool(&pVCpu->hm.s.fCheckedTLBFlush, false); /* See HMInvalidatePageOnAllVCpus(): used for TLB flushing. */
4573 ASMAtomicIncU32(&pVCpu->hm.s.cWorldSwitchExits); /* Initialized in vmR3CreateUVM(): used for EMT poking. */
4574
4575 PSVMVMCB pVmcb = pVCpu->hm.s.svm.pVmcb;
4576 pVmcb->ctrl.u32VmcbCleanBits = HMSVM_VMCB_CLEAN_ALL; /* Mark the VMCB-state cache as unmodified by VMM. */
4577
4578 /* TSC read must be done early for maximum accuracy. */
4579 if (!(pVmcb->ctrl.u64InterceptCtrl & SVM_CTRL_INTERCEPT_RDTSC))
4580 TMCpuTickSetLastSeen(pVCpu, ASMReadTSC() + pVmcb->ctrl.u64TSCOffset);
4581
4582 if (pSvmTransient->fRestoreTscAuxMsr)
4583 {
4584 uint64_t u64GuestTscAuxMsr = ASMRdMsr(MSR_K8_TSC_AUX);
4585 CPUMR0SetGuestTscAux(pVCpu, u64GuestTscAuxMsr);
4586 if (u64GuestTscAuxMsr != pVCpu->hm.s.u64HostTscAux)
4587 ASMWrMsr(MSR_K8_TSC_AUX, pVCpu->hm.s.u64HostTscAux);
4588 }
4589
4590 STAM_PROFILE_ADV_STOP_START(&pVCpu->hm.s.StatInGC, &pVCpu->hm.s.StatExit1, x);
4591 TMNotifyEndOfExecution(pVCpu); /* Notify TM that the guest is no longer running. */
4592 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED_HM);
4593
4594 Assert(!(ASMGetFlags() & X86_EFL_IF));
4595 ASMSetFlags(pSvmTransient->fEFlags); /* Enable interrupts. */
4596 VMMRZCallRing3Enable(pVCpu); /* It is now safe to do longjmps to ring-3!!! */
4597
4598 /* If VMRUN failed, we can bail out early. This does -not- cover SVM_EXIT_INVALID. */
4599 if (RT_UNLIKELY(rcVMRun != VINF_SUCCESS))
4600 {
4601 Log4(("VMRUN failure: rcVMRun=%Rrc\n", rcVMRun));
4602 return;
4603 }
4604
4605 pSvmTransient->u64ExitCode = pVmcb->ctrl.u64ExitCode; /* Save the #VMEXIT reason. */
4606 HMCPU_EXIT_HISTORY_ADD(pVCpu, pVmcb->ctrl.u64ExitCode); /* Update the #VMEXIT history array. */
4607 pSvmTransient->fVectoringDoublePF = false; /* Vectoring double page-fault needs to be determined later. */
4608 pSvmTransient->fVectoringPF = false; /* Vectoring page-fault needs to be determined later. */
4609
4610 hmR0SvmSaveGuestState(pVCpu, pMixedCtx, pVmcb); /* Save the guest state from the VMCB to the guest-CPU context. */
4611
4612 if (RT_LIKELY(pSvmTransient->u64ExitCode != SVM_EXIT_INVALID))
4613 {
4614 if (pVCpu->hm.s.svm.fSyncVTpr)
4615 {
4616 /* TPR patching (for 32-bit guests) uses LSTAR MSR for holding the TPR value, otherwise uses the VTPR. */
4617 if ( pVM->hm.s.fTPRPatchingActive
4618 && (pMixedCtx->msrLSTAR & 0xff) != pSvmTransient->u8GuestTpr)
4619 {
4620 int rc = APICSetTpr(pVCpu, pMixedCtx->msrLSTAR & 0xff);
4621 AssertRC(rc);
4622 HMCPU_CF_SET(pVCpu, HM_CHANGED_SVM_GUEST_APIC_STATE);
4623 }
4624 else if (pSvmTransient->u8GuestTpr != pVmcb->ctrl.IntCtrl.n.u8VTPR)
4625 {
4626 int rc = APICSetTpr(pVCpu, pVmcb->ctrl.IntCtrl.n.u8VTPR << 4);
4627 AssertRC(rc);
4628 HMCPU_CF_SET(pVCpu, HM_CHANGED_SVM_GUEST_APIC_STATE);
4629 }
4630 }
4631 }
4632}
4633
4634
4635/**
4636 * Runs the guest code using AMD-V.
4637 *
4638 * @returns VBox status code.
4639 * @param pVM The cross context VM structure.
4640 * @param pVCpu The cross context virtual CPU structure.
4641 * @param pCtx Pointer to the guest-CPU context.
4642 * @param pcLoops Pointer to the number of executed loops.
4643 */
4644static int hmR0SvmRunGuestCodeNormal(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, uint32_t *pcLoops)
4645{
4646 uint32_t const cMaxResumeLoops = pVM->hm.s.cMaxResumeLoops;
4647 Assert(pcLoops);
4648 Assert(*pcLoops <= cMaxResumeLoops);
4649
4650 SVMTRANSIENT SvmTransient;
4651 SvmTransient.fUpdateTscOffsetting = true;
4652
4653 int rc = VERR_INTERNAL_ERROR_5;
4654 for (;;)
4655 {
4656 Assert(!HMR0SuspendPending());
4657 HMSVM_ASSERT_CPU_SAFE();
4658
4659 /* Preparatory work for running guest code, this may force us to return
4660 to ring-3. This bugger disables interrupts on VINF_SUCCESS! */
4661 STAM_PROFILE_ADV_START(&pVCpu->hm.s.StatEntry, x);
4662 rc = hmR0SvmPreRunGuest(pVM, pVCpu, pCtx, &SvmTransient);
4663 if (rc != VINF_SUCCESS)
4664 break;
4665
4666 /*
4667 * No longjmps to ring-3 from this point on!!!
4668 * Asserts() will still longjmp to ring-3 (but won't return), which is intentional, better than a kernel panic.
4669 * This also disables flushing of the R0-logger instance (if any).
4670 */
4671 hmR0SvmPreRunGuestCommitted(pVM, pVCpu, pCtx, &SvmTransient);
4672 rc = hmR0SvmRunGuest(pVM, pVCpu, pCtx);
4673
4674 /* Restore any residual host-state and save any bits shared between host
4675 and guest into the guest-CPU state. Re-enables interrupts! */
4676 hmR0SvmPostRunGuest(pVM, pVCpu, pCtx, &SvmTransient, rc);
4677
4678 if (RT_UNLIKELY( rc != VINF_SUCCESS /* Check for VMRUN errors. */
4679 || SvmTransient.u64ExitCode == SVM_EXIT_INVALID)) /* Check for invalid guest-state errors. */
4680 {
4681 if (rc == VINF_SUCCESS)
4682 rc = VERR_SVM_INVALID_GUEST_STATE;
4683 STAM_PROFILE_ADV_STOP(&pVCpu->hm.s.StatExit1, x);
4684 hmR0SvmReportWorldSwitchError(pVM, pVCpu, rc, pCtx);
4685 break;
4686 }
4687
4688 /* Handle the #VMEXIT. */
4689 HMSVM_EXITCODE_STAM_COUNTER_INC(SvmTransient.u64ExitCode);
4690 STAM_PROFILE_ADV_STOP_START(&pVCpu->hm.s.StatExit1, &pVCpu->hm.s.StatExit2, x);
4691 VBOXVMM_R0_HMSVM_VMEXIT(pVCpu, pCtx, SvmTransient.u64ExitCode, pVCpu->hm.s.svm.pVmcb);
4692 rc = hmR0SvmHandleExit(pVCpu, pCtx, &SvmTransient);
4693 STAM_PROFILE_ADV_STOP(&pVCpu->hm.s.StatExit2, x);
4694 if (rc != VINF_SUCCESS)
4695 break;
4696 if (++(*pcLoops) >= cMaxResumeLoops)
4697 {
4698 STAM_COUNTER_INC(&pVCpu->hm.s.StatSwitchMaxResumeLoops);
4699 rc = VINF_EM_RAW_INTERRUPT;
4700 break;
4701 }
4702 }
4703
4704 STAM_PROFILE_ADV_STOP(&pVCpu->hm.s.StatEntry, x);
4705 return rc;
4706}
4707
4708
4709/**
4710 * Runs the guest code using AMD-V in single step mode.
4711 *
4712 * @returns VBox status code.
4713 * @param pVM The cross context VM structure.
4714 * @param pVCpu The cross context virtual CPU structure.
4715 * @param pCtx Pointer to the guest-CPU context.
4716 * @param pcLoops Pointer to the number of executed loops.
4717 */
4718static int hmR0SvmRunGuestCodeStep(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, uint32_t *pcLoops)
4719{
4720 uint32_t const cMaxResumeLoops = pVM->hm.s.cMaxResumeLoops;
4721 Assert(pcLoops);
4722 Assert(*pcLoops <= cMaxResumeLoops);
4723
4724 SVMTRANSIENT SvmTransient;
4725 SvmTransient.fUpdateTscOffsetting = true;
4726
4727 uint16_t uCsStart = pCtx->cs.Sel;
4728 uint64_t uRipStart = pCtx->rip;
4729
4730 int rc = VERR_INTERNAL_ERROR_5;
4731 for (;;)
4732 {
4733 Assert(!HMR0SuspendPending());
4734 AssertMsg(pVCpu->hm.s.idEnteredCpu == RTMpCpuId(),
4735 ("Illegal migration! Entered on CPU %u Current %u cLoops=%u\n", (unsigned)pVCpu->hm.s.idEnteredCpu,
4736 (unsigned)RTMpCpuId(), *pcLoops));
4737
4738 /* Preparatory work for running guest code, this may force us to return
4739 to ring-3. This bugger disables interrupts on VINF_SUCCESS! */
4740 STAM_PROFILE_ADV_START(&pVCpu->hm.s.StatEntry, x);
4741 rc = hmR0SvmPreRunGuest(pVM, pVCpu, pCtx, &SvmTransient);
4742 if (rc != VINF_SUCCESS)
4743 break;
4744
4745 /*
4746 * No longjmps to ring-3 from this point on!!!
4747 * Asserts() will still longjmp to ring-3 (but won't return), which is intentional, better than a kernel panic.
4748 * This also disables flushing of the R0-logger instance (if any).
4749 */
4750 VMMRZCallRing3Disable(pVCpu);
4751 VMMRZCallRing3RemoveNotification(pVCpu);
4752 hmR0SvmPreRunGuestCommitted(pVM, pVCpu, pCtx, &SvmTransient);
4753
4754 rc = hmR0SvmRunGuest(pVM, pVCpu, pCtx);
4755
4756 /*
4757 * Restore any residual host-state and save any bits shared between host and guest into the guest-CPU state.
4758 * This will also re-enable longjmps to ring-3 when it has reached a safe point!!!
4759 */
4760 hmR0SvmPostRunGuest(pVM, pVCpu, pCtx, &SvmTransient, rc);
4761 if (RT_UNLIKELY( rc != VINF_SUCCESS /* Check for VMRUN errors. */
4762 || SvmTransient.u64ExitCode == SVM_EXIT_INVALID)) /* Check for invalid guest-state errors. */
4763 {
4764 if (rc == VINF_SUCCESS)
4765 rc = VERR_SVM_INVALID_GUEST_STATE;
4766 STAM_PROFILE_ADV_STOP(&pVCpu->hm.s.StatExit1, x);
4767 hmR0SvmReportWorldSwitchError(pVM, pVCpu, rc, pCtx);
4768 return rc;
4769 }
4770
4771 /* Handle the #VMEXIT. */
4772 HMSVM_EXITCODE_STAM_COUNTER_INC(SvmTransient.u64ExitCode);
4773 STAM_PROFILE_ADV_STOP_START(&pVCpu->hm.s.StatExit1, &pVCpu->hm.s.StatExit2, x);
4774 VBOXVMM_R0_HMSVM_VMEXIT(pVCpu, pCtx, SvmTransient.u64ExitCode, pVCpu->hm.s.svm.pVmcb);
4775 rc = hmR0SvmHandleExit(pVCpu, pCtx, &SvmTransient);
4776 STAM_PROFILE_ADV_STOP(&pVCpu->hm.s.StatExit2, x);
4777 if (rc != VINF_SUCCESS)
4778 break;
4779 if (++(*pcLoops) >= cMaxResumeLoops)
4780 {
4781 STAM_COUNTER_INC(&pVCpu->hm.s.StatSwitchMaxResumeLoops);
4782 rc = VINF_EM_RAW_INTERRUPT;
4783 break;
4784 }
4785
4786 /*
4787 * Did the RIP change, if so, consider it a single step.
4788 * Otherwise, make sure one of the TFs gets set.
4789 */
4790 if ( pCtx->rip != uRipStart
4791 || pCtx->cs.Sel != uCsStart)
4792 {
4793 rc = VINF_EM_DBG_STEPPED;
4794 break;
4795 }
4796 pVCpu->hm.s.fContextUseFlags |= HM_CHANGED_GUEST_DEBUG;
4797 }
4798
4799 /*
4800 * Clear the X86_EFL_TF if necessary.
4801 */
4802 if (pVCpu->hm.s.fClearTrapFlag)
4803 {
4804 pVCpu->hm.s.fClearTrapFlag = false;
4805 pCtx->eflags.Bits.u1TF = 0;
4806 }
4807
4808 STAM_PROFILE_ADV_STOP(&pVCpu->hm.s.StatEntry, x);
4809 return rc;
4810}
4811
4812#ifdef VBOX_WITH_NESTED_HWVIRT
4813/**
4814 * Runs the nested-guest code using AMD-V.
4815 *
4816 * @returns VBox status code.
4817 * @param pVM The cross context VM structure.
4818 * @param pVCpu The cross context virtual CPU structure.
4819 * @param pCtx Pointer to the guest-CPU context.
4820 * @param pcLoops Pointer to the number of executed loops. If we're switching
4821 * from the guest-code execution loop to this nested-guest
4822 * execution loop pass the remainder value, else pass 0.
4823 */
4824static int hmR0SvmRunGuestCodeNested(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx, uint32_t *pcLoops)
4825{
4826 HMSVM_ASSERT_IN_NESTED_GUEST(pCtx);
4827 Assert(pcLoops);
4828 Assert(*pcLoops <= pVM->hm.s.cMaxResumeLoops);
4829
4830 SVMTRANSIENT SvmTransient;
4831 SvmTransient.fUpdateTscOffsetting = true;
4832
4833 int rc = VERR_INTERNAL_ERROR_4;
4834 for (;;)
4835 {
4836 Assert(!HMR0SuspendPending());
4837 HMSVM_ASSERT_CPU_SAFE();
4838
4839 /* Preparatory work for running nested-guest code, this may force us to return
4840 to ring-3. This bugger disables interrupts on VINF_SUCCESS! */
4841 STAM_PROFILE_ADV_START(&pVCpu->hm.s.StatEntry, x);
4842 rc = hmR0SvmPreRunGuestNested(pVM, pVCpu, pCtx, &SvmTransient);
4843 if ( rc != VINF_SUCCESS
4844 || !CPUMIsGuestInSvmNestedHwVirtMode(pCtx))
4845 {
4846 break;
4847 }
4848
4849 /*
4850 * No longjmps to ring-3 from this point on!!!
4851 * Asserts() will still longjmp to ring-3 (but won't return), which is intentional, better than a kernel panic.
4852 * This also disables flushing of the R0-logger instance (if any).
4853 */
4854 hmR0SvmPreRunGuestCommittedNested(pVM, pVCpu, pCtx, &SvmTransient);
4855
4856 rc = hmR0SvmRunGuestNested(pVM, pVCpu, pCtx);
4857
4858 /* Restore any residual host-state and save any bits shared between host
4859 and guest into the guest-CPU state. Re-enables interrupts! */
4860 hmR0SvmPostRunGuestNested(pVM, pVCpu, pCtx, &SvmTransient, rc);
4861
4862 if (RT_LIKELY( rc == VINF_SUCCESS
4863 && SvmTransient.u64ExitCode != SVM_EXIT_INVALID))
4864 { /* extremely likely */ }
4865 else
4866 {
4867 /* VMRUN failed, shouldn't really happen, Guru. */
4868 if (rc != VINF_SUCCESS)
4869 break;
4870
4871 /* Invalid nested-guest state. Cause a #VMEXIT but assert on strict builds. */
4872 AssertMsgFailed(("Invalid nested-guest state. rc=%Rrc u64ExitCode=%#RX64\n", rc, SvmTransient.u64ExitCode));
4873 rc = VBOXSTRICTRC_TODO(IEMExecSvmVmexit(pVCpu, SVM_EXIT_INVALID, 0, 0));
4874 break;
4875 }
4876
4877 /* Handle the #VMEXIT. */
4878 HMSVM_EXITCODE_STAM_COUNTER_INC(SvmTransient.u64ExitCode);
4879 STAM_PROFILE_ADV_STOP_START(&pVCpu->hm.s.StatExit1, &pVCpu->hm.s.StatExit2, x);
4880 VBOXVMM_R0_HMSVM_VMEXIT(pVCpu, pCtx, SvmTransient.u64ExitCode, pCtx->hwvirt.svm.CTX_SUFF(pVmcb));
4881 rc = hmR0SvmHandleExitNested(pVCpu, pCtx, &SvmTransient);
4882 STAM_PROFILE_ADV_STOP(&pVCpu->hm.s.StatExit2, x);
4883 if (rc != VINF_SUCCESS)
4884 break;
4885 if (++(*pcLoops) >= pVM->hm.s.cMaxResumeLoops)
4886 {
4887 STAM_COUNTER_INC(&pVCpu->hm.s.StatSwitchMaxResumeLoops);
4888 rc = VINF_EM_RAW_INTERRUPT;
4889 break;
4890 }
4891
4892 /** @todo handle single-stepping */
4893 }
4894
4895 STAM_PROFILE_ADV_STOP(&pVCpu->hm.s.StatEntry, x);
4896 return rc;
4897}
4898#endif
4899
4900
4901/**
4902 * Runs the guest code using AMD-V.
4903 *
4904 * @returns Strict VBox status code.
4905 * @param pVM The cross context VM structure.
4906 * @param pVCpu The cross context virtual CPU structure.
4907 * @param pCtx Pointer to the guest-CPU context.
4908 */
4909VMMR0DECL(VBOXSTRICTRC) SVMR0RunGuestCode(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
4910{
4911 Assert(VMMRZCallRing3IsEnabled(pVCpu));
4912 HMSVM_ASSERT_PREEMPT_SAFE();
4913 VMMRZCallRing3SetNotification(pVCpu, hmR0SvmCallRing3Callback, pCtx);
4914
4915 uint32_t cLoops = 0;
4916 int rc;
4917#ifdef VBOX_WITH_NESTED_HWVIRT
4918 if (!CPUMIsGuestInSvmNestedHwVirtMode(pCtx))
4919#endif
4920 {
4921 if (!pVCpu->hm.s.fSingleInstruction)
4922 rc = hmR0SvmRunGuestCodeNormal(pVM, pVCpu, pCtx, &cLoops);
4923 else
4924 rc = hmR0SvmRunGuestCodeStep(pVM, pVCpu, pCtx, &cLoops);
4925 }
4926#ifdef VBOX_WITH_NESTED_HWVIRT
4927 else
4928 {
4929 rc = VINF_SVM_VMRUN;
4930 }
4931
4932 /* Re-check the nested-guest condition here as we may be transitioning from the normal
4933 execution loop into the nested-guest, hence this is not placed in the 'else' part above. */
4934 if (rc == VINF_SVM_VMRUN)
4935 {
4936 rc = hmR0SvmRunGuestCodeNested(pVM, pVCpu, pCtx, &cLoops);
4937 if (rc == VINF_SVM_VMEXIT)
4938 rc = VINF_SUCCESS;
4939 }
4940#endif
4941
4942 /* Fixup error codes. */
4943 if (rc == VERR_EM_INTERPRETER)
4944 rc = VINF_EM_RAW_EMULATE_INSTR;
4945 else if (rc == VINF_EM_RESET)
4946 rc = VINF_EM_TRIPLE_FAULT;
4947
4948 /* Prepare to return to ring-3. This will remove longjmp notifications. */
4949 rc = hmR0SvmExitToRing3(pVM, pVCpu, pCtx, rc);
4950 Assert(!VMMRZCallRing3IsNotificationSet(pVCpu));
4951 return rc;
4952}
4953
4954
4955#ifdef VBOX_WITH_NESTED_HWVIRT
4956/**
4957 * Determines whether an IOIO intercept is active for the nested-guest or not.
4958 *
4959 * @param pvIoBitmap Pointer to the nested-guest IO bitmap.
4960 * @param pIoExitInfo Pointer to the SVMIOIOEXITINFO.
4961 */
4962static bool hmR0SvmIsIoInterceptActive(void *pvIoBitmap, PSVMIOIOEXITINFO pIoExitInfo)
4963{
4964 const uint16_t u16Port = pIoExitInfo->n.u16Port;
4965 const SVMIOIOTYPE enmIoType = (SVMIOIOTYPE)pIoExitInfo->n.u1Type;
4966 const uint8_t cbReg = (pIoExitInfo->u >> SVM_IOIO_OP_SIZE_SHIFT) & 7;
4967 const uint8_t cAddrSizeBits = ((pIoExitInfo->u >> SVM_IOIO_ADDR_SIZE_SHIFT) & 7) << 4;
4968 const uint8_t iEffSeg = pIoExitInfo->n.u3SEG;
4969 const bool fRep = pIoExitInfo->n.u1REP;
4970 const bool fStrIo = pIoExitInfo->n.u1STR;
4971
4972 return HMSvmIsIOInterceptActive(pvIoBitmap, u16Port, enmIoType, cbReg, cAddrSizeBits, iEffSeg, fRep, fStrIo,
4973 NULL /* pIoExitInfo */);
4974}
4975
4976
4977/**
4978 * Handles a nested-guest \#VMEXIT (for all EXITCODE values except
4979 * SVM_EXIT_INVALID).
4980 *
4981 * @returns VBox status code (informational status codes included).
4982 * @param pVCpu The cross context virtual CPU structure.
4983 * @param pCtx Pointer to the guest-CPU context.
4984 * @param pSvmTransient Pointer to the SVM transient structure.
4985 */
4986static int hmR0SvmHandleExitNested(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
4987{
4988 HMSVM_ASSERT_IN_NESTED_GUEST(pCtx);
4989 Assert(pSvmTransient->u64ExitCode != SVM_EXIT_INVALID);
4990 Assert(pSvmTransient->u64ExitCode <= SVM_EXIT_MAX);
4991
4992#define HM_SVM_VMEXIT_NESTED(a_pVCpu, a_uExitCode, a_uExitInfo1, a_uExitInfo2) \
4993 VBOXSTRICTRC_TODO(IEMExecSvmVmexit(a_pVCpu, a_uExitCode, a_uExitInfo1, a_uExitInfo2))
4994
4995 /*
4996 * For all the #VMEXITs here we primarily figure out if the #VMEXIT is expected
4997 * by the nested-guest. If it isn't, it should be handled by the (outer) guest.
4998 */
4999 PSVMVMCB pVmcbNstGst = pCtx->hwvirt.svm.CTX_SUFF(pVmcb);
5000 PSVMVMCBCTRL pVmcbNstGstCtrl = &pVmcbNstGst->ctrl;
5001 uint64_t const uExitCode = pVmcbNstGstCtrl->u64ExitCode;
5002 uint64_t const uExitInfo1 = pVmcbNstGstCtrl->u64ExitInfo1;
5003 uint64_t const uExitInfo2 = pVmcbNstGstCtrl->u64ExitInfo2;
5004
5005 Assert(uExitCode == pVmcbNstGstCtrl->u64ExitCode);
5006 switch (uExitCode)
5007 {
5008 case SVM_EXIT_CPUID:
5009 {
5010 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_CPUID))
5011 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5012 return hmR0SvmExitCpuid(pVCpu, pCtx, pSvmTransient);
5013 }
5014
5015 case SVM_EXIT_RDTSC:
5016 {
5017 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_RDTSC))
5018 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5019 return hmR0SvmExitRdtsc(pVCpu, pCtx, pSvmTransient);
5020 }
5021
5022 case SVM_EXIT_RDTSCP:
5023 {
5024 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_RDTSCP))
5025 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5026 return hmR0SvmExitRdtscp(pVCpu, pCtx, pSvmTransient);
5027 }
5028
5029
5030 case SVM_EXIT_MONITOR:
5031 {
5032 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_MONITOR))
5033 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5034 return hmR0SvmExitMonitor(pVCpu, pCtx, pSvmTransient);
5035 }
5036
5037 case SVM_EXIT_MWAIT:
5038 {
5039 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_MWAIT))
5040 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5041 return hmR0SvmExitMwait(pVCpu, pCtx, pSvmTransient);
5042 }
5043
5044 case SVM_EXIT_HLT:
5045 {
5046 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_HLT))
5047 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5048 return hmR0SvmExitHlt(pVCpu, pCtx, pSvmTransient);
5049 }
5050
5051 case SVM_EXIT_MSR:
5052 {
5053 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_MSR_PROT))
5054 {
5055 uint32_t const idMsr = pCtx->ecx;
5056 uint16_t offMsrpm;
5057 uint32_t uMsrpmBit;
5058 int rc = HMSvmGetMsrpmOffsetAndBit(idMsr, &offMsrpm, &uMsrpmBit);
5059 if (RT_SUCCESS(rc))
5060 {
5061 void const *pvMsrBitmap = pCtx->hwvirt.svm.CTX_SUFF(pvMsrBitmap);
5062 bool const fInterceptRead = ASMBitTest(pvMsrBitmap, (offMsrpm << 3) + uMsrpmBit);
5063 bool const fInterceptWrite = ASMBitTest(pvMsrBitmap, (offMsrpm << 3) + uMsrpmBit + 1);
5064
5065 if ( (fInterceptWrite && pVmcbNstGstCtrl->u64ExitInfo1 == SVM_EXIT1_MSR_WRITE)
5066 || (fInterceptRead && pVmcbNstGstCtrl->u64ExitInfo1 == SVM_EXIT1_MSR_READ))
5067 {
5068 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5069 }
5070 }
5071 else
5072 {
5073 /*
5074 * MSRs not covered by the MSRPM automatically cause an #VMEXIT.
5075 * See AMD-V spec. "15.11 MSR Intercepts".
5076 */
5077 Assert(rc == VERR_OUT_OF_RANGE);
5078 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5079 }
5080 }
5081 return hmR0SvmExitMsr(pVCpu, pCtx, pSvmTransient);
5082 }
5083
5084 case SVM_EXIT_IOIO:
5085 {
5086 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_IOIO_PROT))
5087 {
5088 void *pvIoBitmap = pCtx->hwvirt.svm.CTX_SUFF(pvIoBitmap);
5089 SVMIOIOEXITINFO IoExitInfo;
5090 IoExitInfo.u = pVmcbNstGst->ctrl.u64ExitInfo1;
5091 bool const fIntercept = hmR0SvmIsIoInterceptActive(pvIoBitmap, &IoExitInfo);
5092 if (fIntercept)
5093 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5094 }
5095 return hmR0SvmExitIOInstr(pVCpu, pCtx, pSvmTransient);
5096 }
5097
5098 case SVM_EXIT_EXCEPTION_14: /* X86_XCPT_PF */
5099 {
5100 PVM pVM = pVCpu->CTX_SUFF(pVM);
5101 if (pVM->hm.s.fNestedPaging)
5102 {
5103 uint32_t const u32ErrCode = pVmcbNstGstCtrl->u64ExitInfo1;
5104 uint64_t const uFaultAddress = pVmcbNstGstCtrl->u64ExitInfo2;
5105
5106 /* If the nested-guest is intercepting #PFs, cause a #PF #VMEXIT. */
5107 if (HMIsGuestSvmXcptInterceptSet(pVCpu, pCtx, X86_XCPT_PF))
5108 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, u32ErrCode, uFaultAddress);
5109
5110 /* If the nested-guest is not intercepting #PFs, forward the #PF to the nested-guest. */
5111 hmR0SvmSetPendingXcptPF(pVCpu, pCtx, u32ErrCode, uFaultAddress);
5112 return VINF_SUCCESS;
5113 }
5114 return hmR0SvmExitXcptPFNested(pVCpu, pCtx,pSvmTransient);
5115 }
5116
5117 case SVM_EXIT_EXCEPTION_7: /* X86_XCPT_NM */
5118 {
5119 if (HMIsGuestSvmXcptInterceptSet(pVCpu, pCtx, X86_XCPT_NM))
5120 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5121 hmR0SvmSetPendingXcptNM(pVCpu);
5122 return VINF_SUCCESS;
5123 }
5124
5125 case SVM_EXIT_EXCEPTION_6: /* X86_XCPT_UD */
5126 {
5127 if (HMIsGuestSvmXcptInterceptSet(pVCpu, pCtx, X86_XCPT_UD))
5128 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5129 hmR0SvmSetPendingXcptUD(pVCpu);
5130 return VINF_SUCCESS;
5131 }
5132
5133 case SVM_EXIT_EXCEPTION_16: /* X86_XCPT_MF */
5134 {
5135 if (HMIsGuestSvmXcptInterceptSet(pVCpu, pCtx, X86_XCPT_MF))
5136 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5137 hmR0SvmSetPendingXcptMF(pVCpu);
5138 return VINF_SUCCESS;
5139 }
5140
5141 case SVM_EXIT_EXCEPTION_1: /* X86_XCPT_DB */
5142 {
5143 if (HMIsGuestSvmXcptInterceptSet(pVCpu, pCtx, X86_XCPT_DB))
5144 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5145 return hmR0SvmNestedExitXcptDB(pVCpu, pCtx, pSvmTransient);
5146 }
5147
5148 case SVM_EXIT_EXCEPTION_17: /* X86_XCPT_AC */
5149 {
5150 if (HMIsGuestSvmXcptInterceptSet(pVCpu, pCtx, X86_XCPT_AC))
5151 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5152 return hmR0SvmExitXcptAC(pVCpu, pCtx, pSvmTransient);
5153 }
5154
5155 case SVM_EXIT_EXCEPTION_3: /* X86_XCPT_BP */
5156 {
5157 if (HMIsGuestSvmXcptInterceptSet(pVCpu, pCtx, X86_XCPT_BP))
5158 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5159 return hmR0SvmNestedExitXcptBP(pVCpu, pCtx, pSvmTransient);
5160 }
5161
5162 case SVM_EXIT_READ_CR0:
5163 case SVM_EXIT_READ_CR3:
5164 case SVM_EXIT_READ_CR4:
5165 {
5166 uint8_t const uCr = uExitCode - SVM_EXIT_READ_CR0;
5167 if (HMIsGuestSvmReadCRxInterceptSet(pVCpu, pCtx, uCr))
5168 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5169 return hmR0SvmExitReadCRx(pVCpu, pCtx, pSvmTransient);
5170 }
5171
5172 case SVM_EXIT_WRITE_CR0:
5173 case SVM_EXIT_WRITE_CR3:
5174 case SVM_EXIT_WRITE_CR4:
5175 case SVM_EXIT_WRITE_CR8: /** @todo Shouldn't writes to CR8 go to V_TPR instead since we run with V_INTR_MASKING set?? */
5176 {
5177 uint8_t const uCr = uExitCode - SVM_EXIT_WRITE_CR0;
5178 Log4(("hmR0SvmHandleExitNested: Write CR%u: uExitInfo1=%#RX64 uExitInfo2=%#RX64\n", uCr, uExitInfo1, uExitInfo2));
5179
5180 if (HMIsGuestSvmWriteCRxInterceptSet(pVCpu, pCtx, uCr))
5181 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5182 return hmR0SvmExitWriteCRx(pVCpu, pCtx, pSvmTransient);
5183 }
5184
5185 case SVM_EXIT_PAUSE:
5186 {
5187 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_PAUSE))
5188 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5189 return hmR0SvmExitPause(pVCpu, pCtx, pSvmTransient);
5190 }
5191
5192 case SVM_EXIT_VINTR:
5193 {
5194 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_VINTR))
5195 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5196 return hmR0SvmExitUnexpected(pVCpu, pCtx, pSvmTransient);
5197 }
5198
5199 case SVM_EXIT_INTR:
5200 case SVM_EXIT_NMI:
5201 case SVM_EXIT_SMI:
5202 {
5203 /*
5204 * We shouldn't direct physical interrupts, NMIs, SMIs to the nested-guest.
5205 *
5206 * Although we don't intercept SMIs, the nested-guest might. Therefore, we
5207 * might get an SMI #VMEXIT here so simply ignore rather than causing a
5208 * corresponding nested-guest #VMEXIT.
5209 */
5210 return hmR0SvmExitIntr(pVCpu, pCtx, pSvmTransient);
5211 }
5212
5213 case SVM_EXIT_FERR_FREEZE:
5214 {
5215 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_FERR_FREEZE))
5216 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5217 return hmR0SvmExitIntr(pVCpu, pCtx, pSvmTransient);
5218 }
5219
5220 case SVM_EXIT_INVLPG:
5221 {
5222 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_INVLPG))
5223 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5224 return hmR0SvmExitInvlpg(pVCpu, pCtx, pSvmTransient);
5225 }
5226
5227 case SVM_EXIT_WBINVD:
5228 {
5229 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_WBINVD))
5230 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5231 return hmR0SvmExitWbinvd(pVCpu, pCtx, pSvmTransient);
5232 }
5233
5234 case SVM_EXIT_INVD:
5235 {
5236 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_INVD))
5237 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5238 return hmR0SvmExitInvd(pVCpu, pCtx, pSvmTransient);
5239 }
5240
5241 case SVM_EXIT_RDPMC:
5242 {
5243 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_RDPMC))
5244 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5245 return hmR0SvmExitRdpmc(pVCpu, pCtx, pSvmTransient);
5246 }
5247
5248 default:
5249 {
5250 switch (uExitCode)
5251 {
5252 case SVM_EXIT_READ_DR0: case SVM_EXIT_READ_DR1: case SVM_EXIT_READ_DR2: case SVM_EXIT_READ_DR3:
5253 case SVM_EXIT_READ_DR6: case SVM_EXIT_READ_DR7: case SVM_EXIT_READ_DR8: case SVM_EXIT_READ_DR9:
5254 case SVM_EXIT_READ_DR10: case SVM_EXIT_READ_DR11: case SVM_EXIT_READ_DR12: case SVM_EXIT_READ_DR13:
5255 case SVM_EXIT_READ_DR14: case SVM_EXIT_READ_DR15:
5256 {
5257 uint8_t const uDr = uExitCode - SVM_EXIT_READ_DR0;
5258 if (HMIsGuestSvmReadDRxInterceptSet(pVCpu, pCtx, uDr))
5259 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5260 return hmR0SvmExitReadDRx(pVCpu, pCtx, pSvmTransient);
5261 }
5262
5263 case SVM_EXIT_WRITE_DR0: case SVM_EXIT_WRITE_DR1: case SVM_EXIT_WRITE_DR2: case SVM_EXIT_WRITE_DR3:
5264 case SVM_EXIT_WRITE_DR6: case SVM_EXIT_WRITE_DR7: case SVM_EXIT_WRITE_DR8: case SVM_EXIT_WRITE_DR9:
5265 case SVM_EXIT_WRITE_DR10: case SVM_EXIT_WRITE_DR11: case SVM_EXIT_WRITE_DR12: case SVM_EXIT_WRITE_DR13:
5266 case SVM_EXIT_WRITE_DR14: case SVM_EXIT_WRITE_DR15:
5267 {
5268 uint8_t const uDr = uExitCode - SVM_EXIT_WRITE_DR0;
5269 if (HMIsGuestSvmWriteDRxInterceptSet(pVCpu, pCtx, uDr))
5270 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5271 return hmR0SvmExitWriteDRx(pVCpu, pCtx, pSvmTransient);
5272 }
5273
5274 /* The exceptions not handled here are already handled individually above (as they occur more frequently). */
5275 case SVM_EXIT_EXCEPTION_0: /*case SVM_EXIT_EXCEPTION_1:*/ case SVM_EXIT_EXCEPTION_2:
5276 /*case SVM_EXIT_EXCEPTION_3:*/ case SVM_EXIT_EXCEPTION_4: case SVM_EXIT_EXCEPTION_5:
5277 /*case SVM_EXIT_EXCEPTION_6:*/ /*case SVM_EXIT_EXCEPTION_7:*/ case SVM_EXIT_EXCEPTION_8:
5278 case SVM_EXIT_EXCEPTION_9: case SVM_EXIT_EXCEPTION_10: case SVM_EXIT_EXCEPTION_11:
5279 case SVM_EXIT_EXCEPTION_12: case SVM_EXIT_EXCEPTION_13: /*case SVM_EXIT_EXCEPTION_14:*/
5280 case SVM_EXIT_EXCEPTION_15: case SVM_EXIT_EXCEPTION_16: /*case SVM_EXIT_EXCEPTION_17:*/
5281 case SVM_EXIT_EXCEPTION_18: case SVM_EXIT_EXCEPTION_19: case SVM_EXIT_EXCEPTION_20:
5282 case SVM_EXIT_EXCEPTION_21: case SVM_EXIT_EXCEPTION_22: case SVM_EXIT_EXCEPTION_23:
5283 case SVM_EXIT_EXCEPTION_24: case SVM_EXIT_EXCEPTION_25: case SVM_EXIT_EXCEPTION_26:
5284 case SVM_EXIT_EXCEPTION_27: case SVM_EXIT_EXCEPTION_28: case SVM_EXIT_EXCEPTION_29:
5285 case SVM_EXIT_EXCEPTION_30: case SVM_EXIT_EXCEPTION_31:
5286 {
5287 uint8_t const uVector = uExitCode - SVM_EXIT_EXCEPTION_0;
5288 if (HMIsGuestSvmXcptInterceptSet(pVCpu, pCtx, uVector))
5289 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5290 return hmR0SvmExitXcptGeneric(pVCpu, pCtx, pSvmTransient);
5291 }
5292
5293 case SVM_EXIT_XSETBV:
5294 {
5295 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_XSETBV))
5296 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5297 return hmR0SvmExitXsetbv(pVCpu, pCtx, pSvmTransient);
5298 }
5299
5300 case SVM_EXIT_TASK_SWITCH:
5301 {
5302 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_TASK_SWITCH))
5303 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5304 return hmR0SvmExitTaskSwitch(pVCpu, pCtx, pSvmTransient);
5305 }
5306
5307 case SVM_EXIT_IRET:
5308 {
5309 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_IRET))
5310 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5311 return hmR0SvmExitIret(pVCpu, pCtx, pSvmTransient);
5312 }
5313
5314 case SVM_EXIT_SHUTDOWN:
5315 {
5316 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_SHUTDOWN))
5317 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5318 return hmR0SvmExitShutdown(pVCpu, pCtx, pSvmTransient);
5319 }
5320
5321 case SVM_EXIT_VMMCALL:
5322 {
5323 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_VMMCALL))
5324 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5325 return hmR0SvmExitVmmCall(pVCpu, pCtx, pSvmTransient);
5326 }
5327
5328 case SVM_EXIT_CLGI:
5329 {
5330 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_CLGI))
5331 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5332 return hmR0SvmExitClgi(pVCpu, pCtx, pSvmTransient);
5333 }
5334
5335 case SVM_EXIT_STGI:
5336 {
5337 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_STGI))
5338 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5339 return hmR0SvmExitStgi(pVCpu, pCtx, pSvmTransient);
5340 }
5341
5342 case SVM_EXIT_VMLOAD:
5343 {
5344 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_VMLOAD))
5345 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5346 return hmR0SvmExitVmload(pVCpu, pCtx, pSvmTransient);
5347 }
5348
5349 case SVM_EXIT_VMSAVE:
5350 {
5351 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_VMSAVE))
5352 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5353 return hmR0SvmExitVmsave(pVCpu, pCtx, pSvmTransient);
5354 }
5355
5356 case SVM_EXIT_INVLPGA:
5357 {
5358 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_INVLPGA))
5359 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5360 return hmR0SvmExitInvlpga(pVCpu, pCtx, pSvmTransient);
5361 }
5362
5363 case SVM_EXIT_VMRUN:
5364 {
5365 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_VMRUN))
5366 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5367 return hmR0SvmExitVmrun(pVCpu, pCtx, pSvmTransient);
5368 }
5369
5370 case SVM_EXIT_RSM:
5371 {
5372 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_RSM))
5373 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5374 hmR0SvmSetPendingXcptUD(pVCpu);
5375 return VINF_SUCCESS;
5376 }
5377
5378 case SVM_EXIT_SKINIT:
5379 {
5380 if (HMIsGuestSvmCtrlInterceptSet(pVCpu, pCtx, SVM_CTRL_INTERCEPT_SKINIT))
5381 return HM_SVM_VMEXIT_NESTED(pVCpu, uExitCode, uExitInfo1, uExitInfo2);
5382 hmR0SvmSetPendingXcptUD(pVCpu);
5383 return VINF_SUCCESS;
5384 }
5385
5386 case SVM_EXIT_INIT: /* We shouldn't get INIT signals while executing a nested-guest. */
5387 case SVM_EXIT_NPF: /* We don't yet support nested-paging for nested-guests, so this should never happen. */
5388 {
5389 return hmR0SvmExitUnexpected(pVCpu, pCtx, pSvmTransient);
5390 }
5391
5392 default:
5393 {
5394 AssertMsgFailed(("hmR0SvmHandleExitNested: Unknown exit code %#x\n", pSvmTransient->u64ExitCode));
5395 pVCpu->hm.s.u32HMError = pSvmTransient->u64ExitCode;
5396 return VERR_SVM_UNKNOWN_EXIT;
5397 }
5398 }
5399 }
5400 }
5401 /* not reached */
5402
5403#undef HM_SVM_VMEXIT_NESTED
5404}
5405#endif
5406
5407
5408/**
5409 * Handles a guest \#VMEXIT (for all EXITCODE values except SVM_EXIT_INVALID).
5410 *
5411 * @returns VBox status code (informational status codes included).
5412 * @param pVCpu The cross context virtual CPU structure.
5413 * @param pCtx Pointer to the guest-CPU context.
5414 * @param pSvmTransient Pointer to the SVM transient structure.
5415 */
5416static int hmR0SvmHandleExit(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
5417{
5418 Assert(pSvmTransient->u64ExitCode != SVM_EXIT_INVALID);
5419 Assert(pSvmTransient->u64ExitCode <= SVM_EXIT_MAX);
5420
5421 /*
5422 * The ordering of the case labels is based on most-frequently-occurring #VMEXITs for most guests under
5423 * normal workloads (for some definition of "normal").
5424 */
5425 uint64_t const uExitCode = pSvmTransient->u64ExitCode;
5426 switch (uExitCode)
5427 {
5428 case SVM_EXIT_NPF:
5429 return hmR0SvmExitNestedPF(pVCpu, pCtx, pSvmTransient);
5430
5431 case SVM_EXIT_IOIO:
5432 return hmR0SvmExitIOInstr(pVCpu, pCtx, pSvmTransient);
5433
5434 case SVM_EXIT_RDTSC:
5435 return hmR0SvmExitRdtsc(pVCpu, pCtx, pSvmTransient);
5436
5437 case SVM_EXIT_RDTSCP:
5438 return hmR0SvmExitRdtscp(pVCpu, pCtx, pSvmTransient);
5439
5440 case SVM_EXIT_CPUID:
5441 return hmR0SvmExitCpuid(pVCpu, pCtx, pSvmTransient);
5442
5443 case SVM_EXIT_EXCEPTION_14: /* X86_XCPT_PF */
5444 return hmR0SvmExitXcptPF(pVCpu, pCtx, pSvmTransient);
5445
5446 case SVM_EXIT_EXCEPTION_7: /* X86_XCPT_NM */
5447 return hmR0SvmExitXcptNM(pVCpu, pCtx, pSvmTransient);
5448
5449 case SVM_EXIT_EXCEPTION_6: /* X86_XCPT_UD */
5450 return hmR0SvmExitXcptUD(pVCpu, pCtx, pSvmTransient);
5451
5452 case SVM_EXIT_EXCEPTION_16: /* X86_XCPT_MF */
5453 return hmR0SvmExitXcptMF(pVCpu, pCtx, pSvmTransient);
5454
5455 case SVM_EXIT_EXCEPTION_1: /* X86_XCPT_DB */
5456 return hmR0SvmExitXcptDB(pVCpu, pCtx, pSvmTransient);
5457
5458 case SVM_EXIT_EXCEPTION_17: /* X86_XCPT_AC */
5459 return hmR0SvmExitXcptAC(pVCpu, pCtx, pSvmTransient);
5460
5461 case SVM_EXIT_EXCEPTION_3: /* X86_XCPT_BP */
5462 return hmR0SvmExitXcptBP(pVCpu, pCtx, pSvmTransient);
5463
5464 case SVM_EXIT_MONITOR:
5465 return hmR0SvmExitMonitor(pVCpu, pCtx, pSvmTransient);
5466
5467 case SVM_EXIT_MWAIT:
5468 return hmR0SvmExitMwait(pVCpu, pCtx, pSvmTransient);
5469
5470 case SVM_EXIT_HLT:
5471 return hmR0SvmExitHlt(pVCpu, pCtx, pSvmTransient);
5472
5473 case SVM_EXIT_READ_CR0:
5474 case SVM_EXIT_READ_CR3:
5475 case SVM_EXIT_READ_CR4:
5476 return hmR0SvmExitReadCRx(pVCpu, pCtx, pSvmTransient);
5477
5478 case SVM_EXIT_WRITE_CR0:
5479 case SVM_EXIT_WRITE_CR3:
5480 case SVM_EXIT_WRITE_CR4:
5481 case SVM_EXIT_WRITE_CR8:
5482 {
5483 uint8_t const uCr = uExitCode - SVM_EXIT_WRITE_CR0;
5484 Log4(("hmR0SvmHandleExitNested: Write CR%u\n", uCr)); NOREF(uCr);
5485 return hmR0SvmExitWriteCRx(pVCpu, pCtx, pSvmTransient);
5486 }
5487
5488 case SVM_EXIT_PAUSE:
5489 return hmR0SvmExitPause(pVCpu, pCtx, pSvmTransient);
5490
5491 case SVM_EXIT_VMMCALL:
5492 return hmR0SvmExitVmmCall(pVCpu, pCtx, pSvmTransient);
5493
5494 case SVM_EXIT_VINTR:
5495 return hmR0SvmExitVIntr(pVCpu, pCtx, pSvmTransient);
5496
5497 case SVM_EXIT_INTR:
5498 case SVM_EXIT_FERR_FREEZE:
5499 case SVM_EXIT_NMI:
5500 return hmR0SvmExitIntr(pVCpu, pCtx, pSvmTransient);
5501
5502 case SVM_EXIT_MSR:
5503 return hmR0SvmExitMsr(pVCpu, pCtx, pSvmTransient);
5504
5505 case SVM_EXIT_INVLPG:
5506 return hmR0SvmExitInvlpg(pVCpu, pCtx, pSvmTransient);
5507
5508 case SVM_EXIT_WBINVD:
5509 return hmR0SvmExitWbinvd(pVCpu, pCtx, pSvmTransient);
5510
5511 case SVM_EXIT_INVD:
5512 return hmR0SvmExitInvd(pVCpu, pCtx, pSvmTransient);
5513
5514 case SVM_EXIT_RDPMC:
5515 return hmR0SvmExitRdpmc(pVCpu, pCtx, pSvmTransient);
5516
5517 default:
5518 {
5519 switch (pSvmTransient->u64ExitCode)
5520 {
5521 case SVM_EXIT_READ_DR0: case SVM_EXIT_READ_DR1: case SVM_EXIT_READ_DR2: case SVM_EXIT_READ_DR3:
5522 case SVM_EXIT_READ_DR6: case SVM_EXIT_READ_DR7: case SVM_EXIT_READ_DR8: case SVM_EXIT_READ_DR9:
5523 case SVM_EXIT_READ_DR10: case SVM_EXIT_READ_DR11: case SVM_EXIT_READ_DR12: case SVM_EXIT_READ_DR13:
5524 case SVM_EXIT_READ_DR14: case SVM_EXIT_READ_DR15:
5525 return hmR0SvmExitReadDRx(pVCpu, pCtx, pSvmTransient);
5526
5527 case SVM_EXIT_WRITE_DR0: case SVM_EXIT_WRITE_DR1: case SVM_EXIT_WRITE_DR2: case SVM_EXIT_WRITE_DR3:
5528 case SVM_EXIT_WRITE_DR6: case SVM_EXIT_WRITE_DR7: case SVM_EXIT_WRITE_DR8: case SVM_EXIT_WRITE_DR9:
5529 case SVM_EXIT_WRITE_DR10: case SVM_EXIT_WRITE_DR11: case SVM_EXIT_WRITE_DR12: case SVM_EXIT_WRITE_DR13:
5530 case SVM_EXIT_WRITE_DR14: case SVM_EXIT_WRITE_DR15:
5531 return hmR0SvmExitWriteDRx(pVCpu, pCtx, pSvmTransient);
5532
5533 case SVM_EXIT_XSETBV:
5534 return hmR0SvmExitXsetbv(pVCpu, pCtx, pSvmTransient);
5535
5536 case SVM_EXIT_TASK_SWITCH:
5537 return hmR0SvmExitTaskSwitch(pVCpu, pCtx, pSvmTransient);
5538
5539 case SVM_EXIT_IRET:
5540 return hmR0SvmExitIret(pVCpu, pCtx, pSvmTransient);
5541
5542 case SVM_EXIT_SHUTDOWN:
5543 return hmR0SvmExitShutdown(pVCpu, pCtx, pSvmTransient);
5544
5545 case SVM_EXIT_SMI:
5546 case SVM_EXIT_INIT:
5547 {
5548 /*
5549 * We don't intercept SMIs. As for INIT signals, it really shouldn't ever happen here.
5550 * If it ever does, we want to know about it so log the exit code and bail.
5551 */
5552 return hmR0SvmExitUnexpected(pVCpu, pCtx, pSvmTransient);
5553 }
5554
5555#ifdef VBOX_WITH_NESTED_HWVIRT
5556 case SVM_EXIT_CLGI: return hmR0SvmExitClgi(pVCpu, pCtx, pSvmTransient);
5557 case SVM_EXIT_STGI: return hmR0SvmExitStgi(pVCpu, pCtx, pSvmTransient);
5558 case SVM_EXIT_VMLOAD: return hmR0SvmExitVmload(pVCpu, pCtx, pSvmTransient);
5559 case SVM_EXIT_VMSAVE: return hmR0SvmExitVmsave(pVCpu, pCtx, pSvmTransient);
5560 case SVM_EXIT_INVLPGA: return hmR0SvmExitInvlpga(pVCpu, pCtx, pSvmTransient);
5561 case SVM_EXIT_VMRUN: return hmR0SvmExitVmrun(pVCpu, pCtx, pSvmTransient);
5562#else
5563 case SVM_EXIT_CLGI:
5564 case SVM_EXIT_STGI:
5565 case SVM_EXIT_VMLOAD:
5566 case SVM_EXIT_VMSAVE:
5567 case SVM_EXIT_INVLPGA:
5568 case SVM_EXIT_VMRUN:
5569#endif
5570 case SVM_EXIT_RSM:
5571 case SVM_EXIT_SKINIT:
5572 {
5573 hmR0SvmSetPendingXcptUD(pVCpu);
5574 return VINF_SUCCESS;
5575 }
5576
5577#ifdef HMSVM_ALWAYS_TRAP_ALL_XCPTS
5578 case SVM_EXIT_EXCEPTION_0: /* X86_XCPT_DE */
5579 /* SVM_EXIT_EXCEPTION_1: */ /* X86_XCPT_DB - Handled above. */
5580 case SVM_EXIT_EXCEPTION_2: /* X86_XCPT_NMI */
5581 /* SVM_EXIT_EXCEPTION_3: */ /* X86_XCPT_BP - Handled above. */
5582 case SVM_EXIT_EXCEPTION_4: /* X86_XCPT_OF */
5583 case SVM_EXIT_EXCEPTION_5: /* X86_XCPT_BR */
5584 /* SVM_EXIT_EXCEPTION_6: */ /* X86_XCPT_UD - Handled above. */
5585 /* SVM_EXIT_EXCEPTION_7: */ /* X86_XCPT_NM - Handled above. */
5586 case SVM_EXIT_EXCEPTION_8: /* X86_XCPT_DF */
5587 case SVM_EXIT_EXCEPTION_9: /* X86_XCPT_CO_SEG_OVERRUN */
5588 case SVM_EXIT_EXCEPTION_10: /* X86_XCPT_TS */
5589 case SVM_EXIT_EXCEPTION_11: /* X86_XCPT_NP */
5590 case SVM_EXIT_EXCEPTION_12: /* X86_XCPT_SS */
5591 case SVM_EXIT_EXCEPTION_13: /* X86_XCPT_GP */
5592 /* SVM_EXIT_EXCEPTION_14: */ /* X86_XCPT_PF - Handled above. */
5593 case SVM_EXIT_EXCEPTION_15: /* Reserved. */
5594 /* SVM_EXIT_EXCEPTION_16: */ /* X86_XCPT_MF - Handled above. */
5595 /* SVM_EXIT_EXCEPTION_17: */ /* X86_XCPT_AC - Handled above. */
5596 case SVM_EXIT_EXCEPTION_18: /* X86_XCPT_MC */
5597 case SVM_EXIT_EXCEPTION_19: /* X86_XCPT_XF */
5598 case SVM_EXIT_EXCEPTION_20: case SVM_EXIT_EXCEPTION_21: case SVM_EXIT_EXCEPTION_22:
5599 case SVM_EXIT_EXCEPTION_23: case SVM_EXIT_EXCEPTION_24: case SVM_EXIT_EXCEPTION_25:
5600 case SVM_EXIT_EXCEPTION_26: case SVM_EXIT_EXCEPTION_27: case SVM_EXIT_EXCEPTION_28:
5601 case SVM_EXIT_EXCEPTION_29: case SVM_EXIT_EXCEPTION_30: case SVM_EXIT_EXCEPTION_31:
5602 return hmR0SvmExitXcptGeneric(pVCpu, pCtx, pSvmTransient);
5603#endif /* HMSVM_ALWAYS_TRAP_ALL_XCPTS */
5604
5605 default:
5606 {
5607 AssertMsgFailed(("hmR0SvmHandleExit: Unknown exit code %#RX64\n", uExitCode));
5608 pVCpu->hm.s.u32HMError = uExitCode;
5609 return VERR_SVM_UNKNOWN_EXIT;
5610 }
5611 }
5612 }
5613 }
5614 /* not reached */
5615}
5616
5617
5618#ifdef DEBUG
5619/* Is there some generic IPRT define for this that are not in Runtime/internal/\* ?? */
5620# define HMSVM_ASSERT_PREEMPT_CPUID_VAR() \
5621 RTCPUID const idAssertCpu = RTThreadPreemptIsEnabled(NIL_RTTHREAD) ? NIL_RTCPUID : RTMpCpuId()
5622
5623# define HMSVM_ASSERT_PREEMPT_CPUID() \
5624 do \
5625 { \
5626 RTCPUID const idAssertCpuNow = RTThreadPreemptIsEnabled(NIL_RTTHREAD) ? NIL_RTCPUID : RTMpCpuId(); \
5627 AssertMsg(idAssertCpu == idAssertCpuNow, ("SVM %#x, %#x\n", idAssertCpu, idAssertCpuNow)); \
5628 } while (0)
5629
5630# define HMSVM_VALIDATE_EXIT_HANDLER_PARAMS() \
5631 do { \
5632 AssertPtr(pVCpu); \
5633 AssertPtr(pCtx); \
5634 AssertPtr(pSvmTransient); \
5635 Assert(ASMIntAreEnabled()); \
5636 HMSVM_ASSERT_PREEMPT_SAFE(); \
5637 HMSVM_ASSERT_PREEMPT_CPUID_VAR(); \
5638 Log4Func(("vcpu[%u] -v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-v-\n", (uint32_t)pVCpu->idCpu)); \
5639 HMSVM_ASSERT_PREEMPT_SAFE(); \
5640 if (VMMR0IsLogFlushDisabled(pVCpu)) \
5641 HMSVM_ASSERT_PREEMPT_CPUID(); \
5642 } while (0)
5643#else /* Release builds */
5644# define HMSVM_VALIDATE_EXIT_HANDLER_PARAMS() do { NOREF(pVCpu); NOREF(pCtx); NOREF(pSvmTransient); } while (0)
5645#endif
5646
5647
5648/**
5649 * Worker for hmR0SvmInterpretInvlpg().
5650 *
5651 * @return VBox status code.
5652 * @param pVCpu The cross context virtual CPU structure.
5653 * @param pCpu Pointer to the disassembler state.
5654 * @param pCtx The guest CPU context.
5655 */
5656static int hmR0SvmInterpretInvlPgEx(PVMCPU pVCpu, PDISCPUSTATE pCpu, PCPUMCTX pCtx)
5657{
5658 DISQPVPARAMVAL Param1;
5659 RTGCPTR GCPtrPage;
5660
5661 int rc = DISQueryParamVal(CPUMCTX2CORE(pCtx), pCpu, &pCpu->Param1, &Param1, DISQPVWHICH_SRC);
5662 if (RT_FAILURE(rc))
5663 return VERR_EM_INTERPRETER;
5664
5665 if ( Param1.type == DISQPV_TYPE_IMMEDIATE
5666 || Param1.type == DISQPV_TYPE_ADDRESS)
5667 {
5668 if (!(Param1.flags & (DISQPV_FLAG_32 | DISQPV_FLAG_64)))
5669 return VERR_EM_INTERPRETER;
5670
5671 GCPtrPage = Param1.val.val64;
5672 VBOXSTRICTRC rc2 = EMInterpretInvlpg(pVCpu->CTX_SUFF(pVM), pVCpu, CPUMCTX2CORE(pCtx), GCPtrPage);
5673 rc = VBOXSTRICTRC_VAL(rc2);
5674 }
5675 else
5676 {
5677 Log4(("hmR0SvmInterpretInvlPgEx invalid parameter type %#x\n", Param1.type));
5678 rc = VERR_EM_INTERPRETER;
5679 }
5680
5681 return rc;
5682}
5683
5684
5685/**
5686 * Interprets INVLPG.
5687 *
5688 * @returns VBox status code.
5689 * @retval VINF_* Scheduling instructions.
5690 * @retval VERR_EM_INTERPRETER Something we can't cope with.
5691 * @retval VERR_* Fatal errors.
5692 *
5693 * @param pVM The cross context VM structure.
5694 * @param pVCpu The cross context virtual CPU structure.
5695 * @param pCtx The guest CPU context.
5696 *
5697 * @remarks Updates the RIP if the instruction was executed successfully.
5698 */
5699static int hmR0SvmInterpretInvlpg(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
5700{
5701 /* Only allow 32 & 64 bit code. */
5702 if (CPUMGetGuestCodeBits(pVCpu) != 16)
5703 {
5704 PDISSTATE pDis = &pVCpu->hm.s.DisState;
5705 int rc = EMInterpretDisasCurrent(pVM, pVCpu, pDis, NULL /* pcbInstr */);
5706 if ( RT_SUCCESS(rc)
5707 && pDis->pCurInstr->uOpcode == OP_INVLPG)
5708 {
5709 rc = hmR0SvmInterpretInvlPgEx(pVCpu, pDis, pCtx);
5710 if (RT_SUCCESS(rc))
5711 pCtx->rip += pDis->cbInstr;
5712 return rc;
5713 }
5714 else
5715 Log4(("hmR0SvmInterpretInvlpg: EMInterpretDisasCurrent returned %Rrc uOpCode=%#x\n", rc, pDis->pCurInstr->uOpcode));
5716 }
5717 return VERR_EM_INTERPRETER;
5718}
5719
5720
5721#ifdef HMSVM_USE_IEM_EVENT_REFLECTION
5722/**
5723 * Gets the IEM exception flags for the specified SVM event.
5724 *
5725 * @returns The IEM exception flags.
5726 * @param pEvent Pointer to the SVM event.
5727 *
5728 * @remarks This function currently only constructs flags required for
5729 * IEMEvaluateRecursiveXcpt and not the complete flags (e.g. error-code
5730 * and CR2 aspects of an exception are not included).
5731 */
5732static uint32_t hmR0SvmGetIemXcptFlags(PCSVMEVENT pEvent)
5733{
5734 uint8_t const uEventType = pEvent->n.u3Type;
5735 uint32_t fIemXcptFlags;
5736 switch (uEventType)
5737 {
5738 case SVM_EVENT_EXCEPTION:
5739 /*
5740 * Only INT3 and INTO instructions can raise #BP and #OF exceptions.
5741 * See AMD spec. Table 8-1. "Interrupt Vector Source and Cause".
5742 */
5743 if (pEvent->n.u8Vector == X86_XCPT_BP)
5744 {
5745 fIemXcptFlags = IEM_XCPT_FLAGS_T_SOFT_INT | IEM_XCPT_FLAGS_BP_INSTR;
5746 break;
5747 }
5748 if (pEvent->n.u8Vector == X86_XCPT_OF)
5749 {
5750 fIemXcptFlags = IEM_XCPT_FLAGS_T_SOFT_INT | IEM_XCPT_FLAGS_OF_INSTR;
5751 break;
5752 }
5753 /** @todo How do we distinguish ICEBP \#DB from the regular one? */
5754 RT_FALL_THRU();
5755 case SVM_EVENT_NMI:
5756 fIemXcptFlags = IEM_XCPT_FLAGS_T_CPU_XCPT;
5757 break;
5758
5759 case SVM_EVENT_EXTERNAL_IRQ:
5760 fIemXcptFlags = IEM_XCPT_FLAGS_T_EXT_INT;
5761 break;
5762
5763 case SVM_EVENT_SOFTWARE_INT:
5764 fIemXcptFlags = IEM_XCPT_FLAGS_T_SOFT_INT;
5765 break;
5766
5767 default:
5768 fIemXcptFlags = 0;
5769 AssertMsgFailed(("Unexpected event type! uEventType=%#x uVector=%#x", uEventType, pEvent->n.u8Vector));
5770 break;
5771 }
5772 return fIemXcptFlags;
5773}
5774
5775#else
5776/**
5777 * Determines if an exception is a contributory exception.
5778 *
5779 * Contributory exceptions are ones which can cause double-faults unless the
5780 * original exception was a benign exception. Page-fault is intentionally not
5781 * included here as it's a conditional contributory exception.
5782 *
5783 * @returns @c true if the exception is contributory, @c false otherwise.
5784 * @param uVector The exception vector.
5785 */
5786DECLINLINE(bool) hmR0SvmIsContributoryXcpt(const uint32_t uVector)
5787{
5788 switch (uVector)
5789 {
5790 case X86_XCPT_GP:
5791 case X86_XCPT_SS:
5792 case X86_XCPT_NP:
5793 case X86_XCPT_TS:
5794 case X86_XCPT_DE:
5795 return true;
5796 default:
5797 break;
5798 }
5799 return false;
5800}
5801#endif /* HMSVM_USE_IEM_EVENT_REFLECTION */
5802
5803
5804/**
5805 * Handle a condition that occurred while delivering an event through the guest
5806 * IDT.
5807 *
5808 * @returns VBox status code (informational error codes included).
5809 * @retval VINF_SUCCESS if we should continue handling the \#VMEXIT.
5810 * @retval VINF_HM_DOUBLE_FAULT if a \#DF condition was detected and we ought to
5811 * continue execution of the guest which will delivery the \#DF.
5812 * @retval VINF_EM_RESET if we detected a triple-fault condition.
5813 * @retval VERR_EM_GUEST_CPU_HANG if we detected a guest CPU hang.
5814 *
5815 * @param pVCpu The cross context virtual CPU structure.
5816 * @param pCtx Pointer to the guest-CPU context.
5817 * @param pSvmTransient Pointer to the SVM transient structure.
5818 *
5819 * @remarks No-long-jump zone!!!
5820 */
5821static int hmR0SvmCheckExitDueToEventDelivery(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
5822{
5823 int rc = VINF_SUCCESS;
5824 PSVMVMCB pVmcb = hmR0SvmGetCurrentVmcb(pVCpu, pCtx);
5825
5826 Log4(("EXITINTINFO: Pending vectoring event %#RX64 Valid=%RTbool ErrValid=%RTbool Err=%#RX32 Type=%u Vector=%u\n",
5827 pVmcb->ctrl.ExitIntInfo.u, !!pVmcb->ctrl.ExitIntInfo.n.u1Valid, !!pVmcb->ctrl.ExitIntInfo.n.u1ErrorCodeValid,
5828 pVmcb->ctrl.ExitIntInfo.n.u32ErrorCode, pVmcb->ctrl.ExitIntInfo.n.u3Type, pVmcb->ctrl.ExitIntInfo.n.u8Vector));
5829
5830 /* See AMD spec. 15.7.3 "EXITINFO Pseudo-Code". The EXITINTINFO (if valid) contains the prior exception (IDT vector)
5831 * that was trying to be delivered to the guest which caused a #VMEXIT which was intercepted (Exit vector). */
5832 if (pVmcb->ctrl.ExitIntInfo.n.u1Valid)
5833 {
5834#ifdef HMSVM_USE_IEM_EVENT_REFLECTION
5835 IEMXCPTRAISE enmRaise;
5836 IEMXCPTRAISEINFO fRaiseInfo;
5837 bool const fExitIsHwXcpt = pSvmTransient->u64ExitCode - SVM_EXIT_EXCEPTION_0 <= SVM_EXIT_EXCEPTION_31;
5838 uint8_t const uIdtVector = pVmcb->ctrl.ExitIntInfo.n.u8Vector;
5839 if (fExitIsHwXcpt)
5840 {
5841 uint8_t const uExitVector = pSvmTransient->u64ExitCode - SVM_EXIT_EXCEPTION_0;
5842 uint32_t const fIdtVectorFlags = hmR0SvmGetIemXcptFlags(&pVmcb->ctrl.ExitIntInfo);
5843 uint32_t const fExitVectorFlags = IEM_XCPT_FLAGS_T_CPU_XCPT;
5844 enmRaise = IEMEvaluateRecursiveXcpt(pVCpu, fIdtVectorFlags, uIdtVector, fExitVectorFlags, uExitVector, &fRaiseInfo);
5845 }
5846 else
5847 {
5848 /*
5849 * If delivery of an event caused a #VMEXIT that is not an exception (e.g. #NPF) then we
5850 * end up here.
5851 *
5852 * If the event was:
5853 * - a software interrupt, we can re-execute the instruction which will regenerate
5854 * the event.
5855 * - an NMI, we need to clear NMI blocking and re-inject the NMI.
5856 * - a hardware exception or external interrupt, we re-inject it.
5857 */
5858 fRaiseInfo = IEMXCPTRAISEINFO_NONE;
5859 if (pVmcb->ctrl.ExitIntInfo.n.u3Type == SVM_EVENT_SOFTWARE_INT)
5860 enmRaise = IEMXCPTRAISE_REEXEC_INSTR;
5861 else
5862 enmRaise = IEMXCPTRAISE_PREV_EVENT;
5863 }
5864
5865 switch (enmRaise)
5866 {
5867 case IEMXCPTRAISE_CURRENT_XCPT:
5868 case IEMXCPTRAISE_PREV_EVENT:
5869 {
5870 /* For software interrupts, we shall re-execute the instruction. */
5871 if (!(fRaiseInfo & IEMXCPTRAISEINFO_SOFT_INT_XCPT))
5872 {
5873 RTGCUINTPTR GCPtrFaultAddress = 0;
5874
5875 /* If we are re-injecting an NMI, clear NMI blocking. */
5876 if (pVmcb->ctrl.ExitIntInfo.n.u3Type == SVM_EVENT_NMI)
5877 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_BLOCK_NMIS);
5878
5879 /* Determine a vectoring #PF condition, see comment in hmR0SvmExitXcptPF(). */
5880 if (fRaiseInfo & (IEMXCPTRAISEINFO_EXT_INT_PF | IEMXCPTRAISEINFO_NMI_PF))
5881 pSvmTransient->fVectoringPF = true;
5882 else if ( pVmcb->ctrl.ExitIntInfo.n.u3Type == SVM_EVENT_EXCEPTION
5883 && uIdtVector == X86_XCPT_PF)
5884 {
5885 /*
5886 * If the previous exception was a #PF, we need to recover the CR2 value.
5887 * This can't happen with shadow paging.
5888 */
5889 GCPtrFaultAddress = pCtx->cr2;
5890 }
5891
5892 /*
5893 * Without nested paging, when uExitVector is #PF, CR2 value will be updated from the VMCB's
5894 * exit info. fields, if it's a guest #PF, see hmR0SvmExitXcptPF().
5895 */
5896 Assert(pVmcb->ctrl.ExitIntInfo.n.u3Type != SVM_EVENT_SOFTWARE_INT);
5897 STAM_COUNTER_INC(&pVCpu->hm.s.StatInjectPendingReflect);
5898 hmR0SvmSetPendingEvent(pVCpu, &pVmcb->ctrl.ExitIntInfo, GCPtrFaultAddress);
5899
5900 Log4(("IDT: Pending vectoring event %#RX64 ErrValid=%RTbool Err=%#RX32 GCPtrFaultAddress=%#RX64\n",
5901 pVmcb->ctrl.ExitIntInfo.u, RT_BOOL(pVmcb->ctrl.ExitIntInfo.n.u1ErrorCodeValid),
5902 pVmcb->ctrl.ExitIntInfo.n.u32ErrorCode, GCPtrFaultAddress));
5903 }
5904 break;
5905 }
5906
5907 case IEMXCPTRAISE_REEXEC_INSTR:
5908 {
5909 Assert(rc == VINF_SUCCESS);
5910 break;
5911 }
5912
5913 case IEMXCPTRAISE_DOUBLE_FAULT:
5914 {
5915 /*
5916 * Determing a vectoring double #PF condition. Used later, when PGM evaluates the
5917 * second #PF as a guest #PF (and not a shadow #PF) and needs to be converted into a #DF.
5918 */
5919 if (fRaiseInfo & IEMXCPTRAISEINFO_PF_PF)
5920 {
5921 pSvmTransient->fVectoringDoublePF = true;
5922 Assert(rc == VINF_SUCCESS);
5923 }
5924 else
5925 {
5926 STAM_COUNTER_INC(&pVCpu->hm.s.StatInjectPendingReflect);
5927 hmR0SvmSetPendingXcptDF(pVCpu);
5928 rc = VINF_HM_DOUBLE_FAULT;
5929 }
5930 break;
5931 }
5932
5933 case IEMXCPTRAISE_TRIPLE_FAULT:
5934 {
5935 rc = VINF_EM_RESET;
5936 break;
5937 }
5938
5939 case IEMXCPTRAISE_CPU_HANG:
5940 {
5941 rc = VERR_EM_GUEST_CPU_HANG;
5942 break;
5943 }
5944
5945 default:
5946 {
5947 AssertMsgFailed(("hmR0SvmExitCpuid: EMInterpretCpuId failed with %Rrc\n", rc));
5948 rc = VERR_SVM_IPE_2;
5949 break;
5950 }
5951 }
5952#else
5953 uint8_t uIdtVector = pVmcb->ctrl.ExitIntInfo.n.u8Vector;
5954
5955 typedef enum
5956 {
5957 SVMREFLECTXCPT_XCPT, /* Reflect the exception to the guest or for further evaluation by VMM. */
5958 SVMREFLECTXCPT_DF, /* Reflect the exception as a double-fault to the guest. */
5959 SVMREFLECTXCPT_TF, /* Indicate a triple faulted state to the VMM. */
5960 SVMREFLECTXCPT_HANG, /* Indicate bad VM trying to deadlock the CPU. */
5961 SVMREFLECTXCPT_NONE /* Nothing to reflect. */
5962 } SVMREFLECTXCPT;
5963
5964 SVMREFLECTXCPT enmReflect = SVMREFLECTXCPT_NONE;
5965 bool fReflectingNmi = false;
5966 if (pVmcb->ctrl.ExitIntInfo.n.u3Type == SVM_EVENT_EXCEPTION)
5967 {
5968 if (pSvmTransient->u64ExitCode - SVM_EXIT_EXCEPTION_0 <= SVM_EXIT_EXCEPTION_31)
5969 {
5970 uint8_t uExitVector = (uint8_t)(pSvmTransient->u64ExitCode - SVM_EXIT_EXCEPTION_0);
5971
5972#ifdef VBOX_STRICT
5973 if ( hmR0SvmIsContributoryXcpt(uIdtVector)
5974 && uExitVector == X86_XCPT_PF)
5975 {
5976 Log4(("IDT: Contributory #PF idCpu=%u uCR2=%#RX64\n", pVCpu->idCpu, pCtx->cr2));
5977 }
5978#endif
5979
5980 if ( uIdtVector == X86_XCPT_BP
5981 || uIdtVector == X86_XCPT_OF)
5982 {
5983 /* Ignore INT3/INTO, just re-execute. See @bugref{8357}. */
5984 }
5985 else if ( uExitVector == X86_XCPT_PF
5986 && uIdtVector == X86_XCPT_PF)
5987 {
5988 pSvmTransient->fVectoringDoublePF = true;
5989 Log4(("IDT: Vectoring double #PF uCR2=%#RX64\n", pCtx->cr2));
5990 }
5991 else if ( uExitVector == X86_XCPT_AC
5992 && uIdtVector == X86_XCPT_AC)
5993 {
5994 enmReflect = SVMREFLECTXCPT_HANG;
5995 Log4(("IDT: Nested #AC - Bad guest\n"));
5996 }
5997 else if ( (pVmcb->ctrl.u32InterceptXcpt & HMSVM_CONTRIBUTORY_XCPT_MASK)
5998 && hmR0SvmIsContributoryXcpt(uExitVector)
5999 && ( hmR0SvmIsContributoryXcpt(uIdtVector)
6000 || uIdtVector == X86_XCPT_PF))
6001 {
6002 enmReflect = SVMREFLECTXCPT_DF;
6003 Log4(("IDT: Pending vectoring #DF %#RX64 uIdtVector=%#x uExitVector=%#x\n", pVCpu->hm.s.Event.u64IntInfo,
6004 uIdtVector, uExitVector));
6005 }
6006 else if (uIdtVector == X86_XCPT_DF)
6007 {
6008 enmReflect = SVMREFLECTXCPT_TF;
6009 Log4(("IDT: Pending vectoring triple-fault %#RX64 uIdtVector=%#x uExitVector=%#x\n",
6010 pVCpu->hm.s.Event.u64IntInfo, uIdtVector, uExitVector));
6011 }
6012 else
6013 enmReflect = SVMREFLECTXCPT_XCPT;
6014 }
6015 else
6016 {
6017 /*
6018 * If event delivery caused an #VMEXIT that is not an exception (e.g. #NPF) then reflect the original
6019 * exception to the guest after handling the #VMEXIT.
6020 */
6021 enmReflect = SVMREFLECTXCPT_XCPT;
6022 }
6023 }
6024 else if ( pVmcb->ctrl.ExitIntInfo.n.u3Type == SVM_EVENT_EXTERNAL_IRQ
6025 || pVmcb->ctrl.ExitIntInfo.n.u3Type == SVM_EVENT_NMI)
6026 {
6027 enmReflect = SVMREFLECTXCPT_XCPT;
6028 fReflectingNmi = RT_BOOL(pVmcb->ctrl.ExitIntInfo.n.u3Type == SVM_EVENT_NMI);
6029
6030 if (pSvmTransient->u64ExitCode - SVM_EXIT_EXCEPTION_0 <= SVM_EXIT_EXCEPTION_31)
6031 {
6032 uint8_t uExitVector = (uint8_t)(pSvmTransient->u64ExitCode - SVM_EXIT_EXCEPTION_0);
6033 if (uExitVector == X86_XCPT_PF)
6034 {
6035 pSvmTransient->fVectoringPF = true;
6036 Log4(("IDT: Vectoring #PF due to Ext-Int/NMI. uCR2=%#RX64\n", pCtx->cr2));
6037 }
6038 }
6039 }
6040 /* else: Ignore software interrupts (INT n) as they reoccur when restarting the instruction. */
6041
6042 switch (enmReflect)
6043 {
6044 case SVMREFLECTXCPT_XCPT:
6045 {
6046 /* If we are re-injecting the NMI, clear NMI blocking. */
6047 if (fReflectingNmi)
6048 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_BLOCK_NMIS);
6049
6050 Assert(pVmcb->ctrl.ExitIntInfo.n.u3Type != SVM_EVENT_SOFTWARE_INT);
6051 STAM_COUNTER_INC(&pVCpu->hm.s.StatInjectPendingReflect);
6052 hmR0SvmSetPendingEvent(pVCpu, &pVmcb->ctrl.ExitIntInfo, 0 /* GCPtrFaultAddress */);
6053
6054 /* If uExitVector is #PF, CR2 value will be updated from the VMCB if it's a guest #PF. See hmR0SvmExitXcptPF(). */
6055 Log4(("IDT: Pending vectoring event %#RX64 ErrValid=%RTbool Err=%#RX32\n", pVmcb->ctrl.ExitIntInfo.u,
6056 !!pVmcb->ctrl.ExitIntInfo.n.u1ErrorCodeValid, pVmcb->ctrl.ExitIntInfo.n.u32ErrorCode));
6057 break;
6058 }
6059
6060 case SVMREFLECTXCPT_DF:
6061 {
6062 STAM_COUNTER_INC(&pVCpu->hm.s.StatInjectPendingReflect);
6063 hmR0SvmSetPendingXcptDF(pVCpu);
6064 rc = VINF_HM_DOUBLE_FAULT;
6065 break;
6066 }
6067
6068 case SVMREFLECTXCPT_TF:
6069 {
6070 rc = VINF_EM_RESET;
6071 break;
6072 }
6073
6074 case SVMREFLECTXCPT_HANG:
6075 {
6076 rc = VERR_EM_GUEST_CPU_HANG;
6077 break;
6078 }
6079
6080 default:
6081 Assert(rc == VINF_SUCCESS);
6082 break;
6083 }
6084#endif /* HMSVM_USE_IEM_EVENT_REFLECTION */
6085 }
6086 Assert(rc == VINF_SUCCESS || rc == VINF_HM_DOUBLE_FAULT || rc == VINF_EM_RESET || rc == VERR_EM_GUEST_CPU_HANG);
6087 NOREF(pCtx);
6088 return rc;
6089}
6090
6091
6092/**
6093 * Advances the guest RIP making use of the CPU's NRIP_SAVE feature if
6094 * supported, otherwise advances the RIP by the number of bytes specified in
6095 * @a cb.
6096 *
6097 * @param pVCpu The cross context virtual CPU structure.
6098 * @param pCtx Pointer to the guest-CPU context.
6099 * @param cb RIP increment value in bytes.
6100 *
6101 * @remarks Use this function only from \#VMEXIT's where the NRIP value is valid
6102 * when NRIP_SAVE is supported by the CPU, otherwise use
6103 * hmR0SvmAdvanceRipDumb!
6104 */
6105DECLINLINE(void) hmR0SvmAdvanceRipHwAssist(PVMCPU pVCpu, PCPUMCTX pCtx, uint32_t cb)
6106{
6107 bool const fSupportsNextRipSave = hmR0SvmSupportsNextRipSave(pVCpu, pCtx);
6108 if (fSupportsNextRipSave)
6109 {
6110 PCSVMVMCB pVmcb = hmR0SvmGetCurrentVmcb(pVCpu, pCtx);
6111 Assert(pVmcb->ctrl.u64NextRIP);
6112 AssertRelease(pVmcb->ctrl.u64NextRIP - pCtx->rip == cb); /* temporary, remove later */
6113 pCtx->rip = pVmcb->ctrl.u64NextRIP;
6114 }
6115 else
6116 pCtx->rip += cb;
6117
6118 HMSVM_UPDATE_INTR_SHADOW(pVCpu, pCtx);
6119}
6120
6121
6122#ifdef VBOX_WITH_NESTED_HWVIRT
6123/**
6124 * Gets the length of the current instruction if the CPU supports the NRIP_SAVE
6125 * feature. Otherwise, returns the value in @a cbLikely.
6126 *
6127 * @param pVCpu The cross context virtual CPU structure.
6128 * @param pCtx Pointer to the guest-CPU context.
6129 * @param cbLikely The likely instruction length.
6130 */
6131DECLINLINE(uint8_t) hmR0SvmGetInstrLengthHwAssist(PVMCPU pVCpu, PCPUMCTX pCtx, uint8_t cbLikely)
6132{
6133 Assert(cbLikely <= 15); /* See Intel spec. 2.3.11 "AVX Instruction Length" */
6134 bool const fSupportsNextRipSave = hmR0SvmSupportsNextRipSave(pVCpu, pCtx);
6135 if (fSupportsNextRipSave)
6136 {
6137 PCSVMVMCB pVmcb = hmR0SvmGetCurrentVmcb(pVCpu, pCtx);
6138 uint8_t const cbInstr = pVmcb->ctrl.u64NextRIP - pCtx->rip;
6139 Assert(cbInstr == cbLikely);
6140 return cbInstr;
6141 }
6142 return cbLikely;
6143}
6144#endif
6145
6146
6147/**
6148 * Advances the guest RIP by the number of bytes specified in @a cb. This does
6149 * not make use of any hardware features to determine the instruction length.
6150 *
6151 * @param pVCpu The cross context virtual CPU structure.
6152 * @param pCtx Pointer to the guest-CPU context.
6153 * @param cb RIP increment value in bytes.
6154 */
6155DECLINLINE(void) hmR0SvmAdvanceRipDumb(PVMCPU pVCpu, PCPUMCTX pCtx, uint32_t cb)
6156{
6157 pCtx->rip += cb;
6158 HMSVM_UPDATE_INTR_SHADOW(pVCpu, pCtx);
6159}
6160#undef HMSVM_UPDATE_INTR_SHADOW
6161
6162
6163/* -=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
6164/* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #VMEXIT handlers -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
6165/* -=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
6166
6167/** @name \#VMEXIT handlers.
6168 * @{
6169 */
6170
6171/**
6172 * \#VMEXIT handler for external interrupts, NMIs, FPU assertion freeze and INIT
6173 * signals (SVM_EXIT_INTR, SVM_EXIT_NMI, SVM_EXIT_FERR_FREEZE, SVM_EXIT_INIT).
6174 */
6175HMSVM_EXIT_DECL hmR0SvmExitIntr(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
6176{
6177 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
6178
6179 if (pSvmTransient->u64ExitCode == SVM_EXIT_NMI)
6180 STAM_REL_COUNTER_INC(&pVCpu->hm.s.StatExitHostNmiInGC);
6181 else if (pSvmTransient->u64ExitCode == SVM_EXIT_INTR)
6182 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitExtInt);
6183
6184 /*
6185 * AMD-V has no preemption timer and the generic periodic preemption timer has no way to signal -before- the timer
6186 * fires if the current interrupt is our own timer or a some other host interrupt. We also cannot examine what
6187 * interrupt it is until the host actually take the interrupt.
6188 *
6189 * Going back to executing guest code here unconditionally causes random scheduling problems (observed on an
6190 * AMD Phenom 9850 Quad-Core on Windows 64-bit host).
6191 */
6192 return VINF_EM_RAW_INTERRUPT;
6193}
6194
6195
6196/**
6197 * \#VMEXIT handler for WBINVD (SVM_EXIT_WBINVD). Conditional \#VMEXIT.
6198 */
6199HMSVM_EXIT_DECL hmR0SvmExitWbinvd(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
6200{
6201 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
6202
6203 hmR0SvmAdvanceRipHwAssist(pVCpu, pCtx, 2);
6204 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitWbinvd);
6205 int rc = VINF_SUCCESS;
6206 HMSVM_CHECK_SINGLE_STEP(pVCpu, rc);
6207 return rc;
6208}
6209
6210
6211/**
6212 * \#VMEXIT handler for INVD (SVM_EXIT_INVD). Unconditional \#VMEXIT.
6213 */
6214HMSVM_EXIT_DECL hmR0SvmExitInvd(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
6215{
6216 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
6217
6218 hmR0SvmAdvanceRipHwAssist(pVCpu, pCtx, 2);
6219 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitInvd);
6220 int rc = VINF_SUCCESS;
6221 HMSVM_CHECK_SINGLE_STEP(pVCpu, rc);
6222 return rc;
6223}
6224
6225
6226/**
6227 * \#VMEXIT handler for INVD (SVM_EXIT_CPUID). Conditional \#VMEXIT.
6228 */
6229HMSVM_EXIT_DECL hmR0SvmExitCpuid(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
6230{
6231 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
6232 PVM pVM = pVCpu->CTX_SUFF(pVM);
6233 int rc = EMInterpretCpuId(pVM, pVCpu, CPUMCTX2CORE(pCtx));
6234 if (RT_LIKELY(rc == VINF_SUCCESS))
6235 {
6236 hmR0SvmAdvanceRipHwAssist(pVCpu, pCtx, 2);
6237 HMSVM_CHECK_SINGLE_STEP(pVCpu, rc);
6238 }
6239 else
6240 {
6241 AssertMsgFailed(("hmR0SvmExitCpuid: EMInterpretCpuId failed with %Rrc\n", rc));
6242 rc = VERR_EM_INTERPRETER;
6243 }
6244 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitCpuid);
6245 return rc;
6246}
6247
6248
6249/**
6250 * \#VMEXIT handler for RDTSC (SVM_EXIT_RDTSC). Conditional \#VMEXIT.
6251 */
6252HMSVM_EXIT_DECL hmR0SvmExitRdtsc(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
6253{
6254 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
6255 PVM pVM = pVCpu->CTX_SUFF(pVM);
6256 int rc = EMInterpretRdtsc(pVM, pVCpu, CPUMCTX2CORE(pCtx));
6257 if (RT_LIKELY(rc == VINF_SUCCESS))
6258 {
6259 pSvmTransient->fUpdateTscOffsetting = true;
6260 hmR0SvmAdvanceRipHwAssist(pVCpu, pCtx, 2);
6261 HMSVM_CHECK_SINGLE_STEP(pVCpu, rc);
6262 }
6263 else
6264 {
6265 AssertMsgFailed(("hmR0SvmExitRdtsc: EMInterpretRdtsc failed with %Rrc\n", rc));
6266 rc = VERR_EM_INTERPRETER;
6267 }
6268 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitRdtsc);
6269 return rc;
6270}
6271
6272
6273/**
6274 * \#VMEXIT handler for RDTSCP (SVM_EXIT_RDTSCP). Conditional \#VMEXIT.
6275 */
6276HMSVM_EXIT_DECL hmR0SvmExitRdtscp(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
6277{
6278 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
6279 int rc = EMInterpretRdtscp(pVCpu->CTX_SUFF(pVM), pVCpu, pCtx);
6280 if (RT_LIKELY(rc == VINF_SUCCESS))
6281 {
6282 pSvmTransient->fUpdateTscOffsetting = true;
6283 hmR0SvmAdvanceRipHwAssist(pVCpu, pCtx, 3);
6284 HMSVM_CHECK_SINGLE_STEP(pVCpu, rc);
6285 }
6286 else
6287 {
6288 AssertMsgFailed(("hmR0SvmExitRdtsc: EMInterpretRdtscp failed with %Rrc\n", rc));
6289 rc = VERR_EM_INTERPRETER;
6290 }
6291 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitRdtscp);
6292 return rc;
6293}
6294
6295
6296/**
6297 * \#VMEXIT handler for RDPMC (SVM_EXIT_RDPMC). Conditional \#VMEXIT.
6298 */
6299HMSVM_EXIT_DECL hmR0SvmExitRdpmc(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
6300{
6301 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
6302 int rc = EMInterpretRdpmc(pVCpu->CTX_SUFF(pVM), pVCpu, CPUMCTX2CORE(pCtx));
6303 if (RT_LIKELY(rc == VINF_SUCCESS))
6304 {
6305 hmR0SvmAdvanceRipHwAssist(pVCpu, pCtx, 2);
6306 HMSVM_CHECK_SINGLE_STEP(pVCpu, rc);
6307 }
6308 else
6309 {
6310 AssertMsgFailed(("hmR0SvmExitRdpmc: EMInterpretRdpmc failed with %Rrc\n", rc));
6311 rc = VERR_EM_INTERPRETER;
6312 }
6313 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitRdpmc);
6314 return rc;
6315}
6316
6317
6318/**
6319 * \#VMEXIT handler for INVLPG (SVM_EXIT_INVLPG). Conditional \#VMEXIT.
6320 */
6321HMSVM_EXIT_DECL hmR0SvmExitInvlpg(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
6322{
6323 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
6324 PVM pVM = pVCpu->CTX_SUFF(pVM);
6325 Assert(!pVM->hm.s.fNestedPaging);
6326 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitInvlpg);
6327
6328 bool const fSupportsDecodeAssists = hmR0SvmSupportsDecodeAssists(pVCpu, pCtx);
6329 bool const fSupportsNextRipSave = hmR0SvmSupportsNextRipSave(pVCpu, pCtx);
6330 if ( fSupportsDecodeAssists
6331 && fSupportsNextRipSave)
6332 {
6333 PCSVMVMCB pVmcb = hmR0SvmGetCurrentVmcb(pVCpu, pCtx);
6334 uint8_t const cbInstr = pVmcb->ctrl.u64NextRIP - pCtx->rip;
6335 RTGCPTR const GCPtrPage = pVmcb->ctrl.u64ExitInfo1;
6336 VBOXSTRICTRC rcStrict = IEMExecDecodedInvlpg(pVCpu, cbInstr, GCPtrPage);
6337 HMSVM_CHECK_SINGLE_STEP(pVCpu, rcStrict);
6338 return VBOXSTRICTRC_VAL(rcStrict);
6339 }
6340
6341 int rc = hmR0SvmInterpretInvlpg(pVM, pVCpu, pCtx); /* Updates RIP if successful. */
6342 Assert(rc == VINF_SUCCESS || rc == VERR_EM_INTERPRETER);
6343 HMSVM_CHECK_SINGLE_STEP(pVCpu, rc);
6344 return rc;
6345}
6346
6347
6348/**
6349 * \#VMEXIT handler for HLT (SVM_EXIT_HLT). Conditional \#VMEXIT.
6350 */
6351HMSVM_EXIT_DECL hmR0SvmExitHlt(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
6352{
6353 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
6354
6355 hmR0SvmAdvanceRipHwAssist(pVCpu, pCtx, 1);
6356 int rc = EMShouldContinueAfterHalt(pVCpu, pCtx) ? VINF_SUCCESS : VINF_EM_HALT;
6357 HMSVM_CHECK_SINGLE_STEP(pVCpu, rc);
6358 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitHlt);
6359 if (rc != VINF_SUCCESS)
6360 STAM_COUNTER_INC(&pVCpu->hm.s.StatSwitchHltToR3);
6361 return rc;
6362}
6363
6364
6365/**
6366 * \#VMEXIT handler for MONITOR (SVM_EXIT_MONITOR). Conditional \#VMEXIT.
6367 */
6368HMSVM_EXIT_DECL hmR0SvmExitMonitor(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
6369{
6370 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
6371 int rc = EMInterpretMonitor(pVCpu->CTX_SUFF(pVM), pVCpu, CPUMCTX2CORE(pCtx));
6372 if (RT_LIKELY(rc == VINF_SUCCESS))
6373 {
6374 hmR0SvmAdvanceRipHwAssist(pVCpu, pCtx, 3);
6375 HMSVM_CHECK_SINGLE_STEP(pVCpu, rc);
6376 }
6377 else
6378 {
6379 AssertMsg(rc == VERR_EM_INTERPRETER, ("hmR0SvmExitMonitor: EMInterpretMonitor failed with %Rrc\n", rc));
6380 rc = VERR_EM_INTERPRETER;
6381 }
6382 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitMonitor);
6383 return rc;
6384}
6385
6386
6387/**
6388 * \#VMEXIT handler for MWAIT (SVM_EXIT_MWAIT). Conditional \#VMEXIT.
6389 */
6390HMSVM_EXIT_DECL hmR0SvmExitMwait(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
6391{
6392 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
6393 VBOXSTRICTRC rc2 = EMInterpretMWait(pVCpu->CTX_SUFF(pVM), pVCpu, CPUMCTX2CORE(pCtx));
6394 int rc = VBOXSTRICTRC_VAL(rc2);
6395 if ( rc == VINF_EM_HALT
6396 || rc == VINF_SUCCESS)
6397 {
6398 hmR0SvmAdvanceRipHwAssist(pVCpu, pCtx, 3);
6399
6400 if ( rc == VINF_EM_HALT
6401 && EMMonitorWaitShouldContinue(pVCpu, pCtx))
6402 {
6403 rc = VINF_SUCCESS;
6404 }
6405 HMSVM_CHECK_SINGLE_STEP(pVCpu, rc);
6406 }
6407 else
6408 {
6409 AssertMsg(rc == VERR_EM_INTERPRETER, ("hmR0SvmExitMwait: EMInterpretMWait failed with %Rrc\n", rc));
6410 rc = VERR_EM_INTERPRETER;
6411 }
6412 AssertMsg(rc == VINF_SUCCESS || rc == VINF_EM_HALT || rc == VERR_EM_INTERPRETER,
6413 ("hmR0SvmExitMwait: EMInterpretMWait failed rc=%Rrc\n", rc));
6414 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitMwait);
6415 return rc;
6416}
6417
6418
6419/**
6420 * \#VMEXIT handler for shutdown (triple-fault) (SVM_EXIT_SHUTDOWN). Conditional
6421 * \#VMEXIT.
6422 */
6423HMSVM_EXIT_DECL hmR0SvmExitShutdown(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
6424{
6425 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
6426 return VINF_EM_RESET;
6427}
6428
6429
6430/**
6431 * \#VMEXIT handler for unexpected exits. Conditional \#VMEXIT.
6432 */
6433HMSVM_EXIT_DECL hmR0SvmExitUnexpected(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
6434{
6435 RT_NOREF(pCtx);
6436 AssertMsgFailed(("hmR0SvmExitUnexpected: ExitCode=%#RX64\n", pSvmTransient->u64ExitCode));
6437 pVCpu->hm.s.u32HMError = (uint32_t)pSvmTransient->u64ExitCode;
6438 return VERR_SVM_UNEXPECTED_EXIT;
6439}
6440
6441
6442/**
6443 * \#VMEXIT handler for CRx reads (SVM_EXIT_READ_CR*). Conditional \#VMEXIT.
6444 */
6445HMSVM_EXIT_DECL hmR0SvmExitReadCRx(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
6446{
6447 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
6448
6449 Log4(("hmR0SvmExitReadCRx: CS:RIP=%04x:%#RX64\n", pCtx->cs.Sel, pCtx->rip));
6450 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitCRxRead[pSvmTransient->u64ExitCode - SVM_EXIT_READ_CR0]);
6451
6452 bool const fSupportsDecodeAssists = hmR0SvmSupportsDecodeAssists(pVCpu, pCtx);
6453 bool const fSupportsNextRipSave = hmR0SvmSupportsNextRipSave(pVCpu, pCtx);
6454 if ( fSupportsDecodeAssists
6455 && fSupportsNextRipSave)
6456 {
6457 PCSVMVMCB pVmcb = hmR0SvmGetCurrentVmcb(pVCpu, pCtx);
6458 bool const fMovCRx = RT_BOOL(pVmcb->ctrl.u64ExitInfo1 & SVM_EXIT1_MOV_CRX_MASK);
6459 if (fMovCRx)
6460 {
6461 uint8_t const cbInstr = pVmcb->ctrl.u64NextRIP - pCtx->rip;
6462 uint8_t const iCrReg = pSvmTransient->u64ExitCode - SVM_EXIT_READ_CR0;
6463 uint8_t const iGReg = pVmcb->ctrl.u64ExitInfo1 & SVM_EXIT1_MOV_CRX_GPR_NUMBER;
6464 VBOXSTRICTRC rcStrict = IEMExecDecodedMovCRxRead(pVCpu, cbInstr, iGReg, iCrReg);
6465 HMSVM_CHECK_SINGLE_STEP(pVCpu, rcStrict);
6466 return VBOXSTRICTRC_VAL(rcStrict);
6467 }
6468 /* else: SMSW instruction, fall back below to IEM for this. */
6469 }
6470
6471 VBOXSTRICTRC rc2 = EMInterpretInstruction(pVCpu, CPUMCTX2CORE(pCtx), 0 /* pvFault */);
6472 int rc = VBOXSTRICTRC_VAL(rc2);
6473 AssertMsg(rc == VINF_SUCCESS || rc == VERR_EM_INTERPRETER || rc == VINF_PGM_CHANGE_MODE || rc == VINF_PGM_SYNC_CR3,
6474 ("hmR0SvmExitReadCRx: EMInterpretInstruction failed rc=%Rrc\n", rc));
6475 Assert((pSvmTransient->u64ExitCode - SVM_EXIT_READ_CR0) <= 15);
6476 HMSVM_CHECK_SINGLE_STEP(pVCpu, rc);
6477 return rc;
6478}
6479
6480
6481/**
6482 * \#VMEXIT handler for CRx writes (SVM_EXIT_WRITE_CR*). Conditional \#VMEXIT.
6483 */
6484HMSVM_EXIT_DECL hmR0SvmExitWriteCRx(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
6485{
6486 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
6487
6488 uint8_t const iCrReg = pSvmTransient->u64ExitCode - SVM_EXIT_WRITE_CR0;
6489 Assert(iCrReg <= 15);
6490
6491 VBOXSTRICTRC rcStrict = VERR_SVM_IPE_5;
6492 bool fDecodedInstr = false;
6493 bool const fSupportsDecodeAssists = hmR0SvmSupportsDecodeAssists(pVCpu, pCtx);
6494 bool const fSupportsNextRipSave = hmR0SvmSupportsNextRipSave(pVCpu, pCtx);
6495 if ( fSupportsDecodeAssists
6496 && fSupportsNextRipSave)
6497 {
6498 PCSVMVMCB pVmcb = hmR0SvmGetCurrentVmcb(pVCpu, pCtx);
6499 bool const fMovCRx = RT_BOOL(pVmcb->ctrl.u64ExitInfo1 & SVM_EXIT1_MOV_CRX_MASK);
6500 if (fMovCRx)
6501 {
6502 uint8_t const cbInstr = pVmcb->ctrl.u64NextRIP - pCtx->rip;
6503 uint8_t const iGReg = pVmcb->ctrl.u64ExitInfo1 & SVM_EXIT1_MOV_CRX_GPR_NUMBER;
6504 Log4(("hmR0SvmExitWriteCRx: Mov CR%u w/ iGReg=%#x\n", iCrReg, iGReg));
6505 rcStrict = IEMExecDecodedMovCRxWrite(pVCpu, cbInstr, iCrReg, iGReg);
6506 fDecodedInstr = true;
6507 }
6508 /* else: LMSW or CLTS instruction, fall back below to IEM for this. */
6509 }
6510
6511 if (!fDecodedInstr)
6512 {
6513 Log4(("hmR0SvmExitWriteCRx: iCrReg=%#x\n", iCrReg));
6514 rcStrict = IEMExecOneBypassEx(pVCpu, CPUMCTX2CORE(pCtx), NULL);
6515 if (RT_UNLIKELY( rcStrict == VERR_IEM_ASPECT_NOT_IMPLEMENTED
6516 || rcStrict == VERR_IEM_INSTR_NOT_IMPLEMENTED))
6517 rcStrict = VERR_EM_INTERPRETER;
6518 }
6519
6520 if (rcStrict == VINF_SUCCESS)
6521 {
6522 switch (iCrReg)
6523 {
6524 case 0: /* CR0. */
6525 HMCPU_CF_SET(pVCpu, HM_CHANGED_GUEST_CR0);
6526 break;
6527
6528 case 3: /* CR3. */
6529 HMCPU_CF_SET(pVCpu, HM_CHANGED_GUEST_CR3);
6530 break;
6531
6532 case 4: /* CR4. */
6533 HMCPU_CF_SET(pVCpu, HM_CHANGED_GUEST_CR4);
6534 break;
6535
6536 case 8: /* CR8 (TPR). */
6537 HMCPU_CF_SET(pVCpu, HM_CHANGED_SVM_GUEST_APIC_STATE);
6538 break;
6539
6540 default:
6541 AssertMsgFailed(("hmR0SvmExitWriteCRx: Invalid/Unexpected Write-CRx exit. u64ExitCode=%#RX64 %#x\n",
6542 pSvmTransient->u64ExitCode, iCrReg));
6543 break;
6544 }
6545 HMSVM_CHECK_SINGLE_STEP(pVCpu, rcStrict);
6546 }
6547 else
6548 Assert(rcStrict == VERR_EM_INTERPRETER || rcStrict == VINF_PGM_CHANGE_MODE || rcStrict == VINF_PGM_SYNC_CR3);
6549 return VBOXSTRICTRC_TODO(rcStrict);
6550}
6551
6552
6553/**
6554 * \#VMEXIT handler for MSR read and writes (SVM_EXIT_MSR). Conditional
6555 * \#VMEXIT.
6556 */
6557HMSVM_EXIT_DECL hmR0SvmExitMsr(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
6558{
6559 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
6560 PSVMVMCB pVmcb = hmR0SvmGetCurrentVmcb(pVCpu, pCtx);
6561 PVM pVM = pVCpu->CTX_SUFF(pVM);
6562
6563 int rc;
6564 if (pVmcb->ctrl.u64ExitInfo1 == SVM_EXIT1_MSR_WRITE)
6565 {
6566 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitWrmsr);
6567 Log4(("MSR Write: idMsr=%#RX32\n", pCtx->ecx));
6568
6569 /* Handle TPR patching; intercepted LSTAR write. */
6570 if ( pVM->hm.s.fTPRPatchingActive
6571 && pCtx->ecx == MSR_K8_LSTAR)
6572 {
6573 if ((pCtx->eax & 0xff) != pSvmTransient->u8GuestTpr)
6574 {
6575 /* Our patch code uses LSTAR for TPR caching for 32-bit guests. */
6576 int rc2 = APICSetTpr(pVCpu, pCtx->eax & 0xff);
6577 AssertRC(rc2);
6578 HMCPU_CF_SET(pVCpu, HM_CHANGED_SVM_GUEST_APIC_STATE);
6579 }
6580 rc = VINF_SUCCESS;
6581 hmR0SvmAdvanceRipHwAssist(pVCpu, pCtx, 2);
6582 HMSVM_CHECK_SINGLE_STEP(pVCpu, rc);
6583 return rc;
6584 }
6585
6586 bool const fSupportsNextRipSave = hmR0SvmSupportsNextRipSave(pVCpu, pCtx);
6587 if (fSupportsNextRipSave)
6588 {
6589 rc = EMInterpretWrmsr(pVM, pVCpu, CPUMCTX2CORE(pCtx));
6590 if (RT_LIKELY(rc == VINF_SUCCESS))
6591 {
6592 pCtx->rip = pVmcb->ctrl.u64NextRIP;
6593 HMSVM_CHECK_SINGLE_STEP(pVCpu, rc);
6594 }
6595 else
6596 AssertMsg( rc == VERR_EM_INTERPRETER
6597 || rc == VINF_CPUM_R3_MSR_WRITE, ("hmR0SvmExitMsr: EMInterpretWrmsr failed rc=%Rrc\n", rc));
6598 }
6599 else
6600 {
6601 rc = VBOXSTRICTRC_TODO(EMInterpretInstruction(pVCpu, CPUMCTX2CORE(pCtx), 0 /* pvFault */));
6602 if (RT_LIKELY(rc == VINF_SUCCESS))
6603 HMSVM_CHECK_SINGLE_STEP(pVCpu, rc); /* RIP updated by EMInterpretInstruction(). */
6604 else
6605 AssertMsg( rc == VERR_EM_INTERPRETER
6606 || rc == VINF_CPUM_R3_MSR_WRITE, ("hmR0SvmExitMsr: WrMsr. EMInterpretInstruction failed rc=%Rrc\n", rc));
6607 }
6608
6609 if (rc == VINF_SUCCESS)
6610 {
6611 /* If this is an X2APIC WRMSR access, update the APIC state as well. */
6612 if ( pCtx->ecx >= MSR_IA32_X2APIC_START
6613 && pCtx->ecx <= MSR_IA32_X2APIC_END)
6614 {
6615 /*
6616 * We've already saved the APIC related guest-state (TPR) in hmR0SvmPostRunGuest(). When full APIC register
6617 * virtualization is implemented we'll have to make sure APIC state is saved from the VMCB before
6618 * EMInterpretWrmsr() changes it.
6619 */
6620 HMCPU_CF_SET(pVCpu, HM_CHANGED_SVM_GUEST_APIC_STATE);
6621 }
6622 else
6623 {
6624 switch (pCtx->ecx)
6625 {
6626 case MSR_K6_EFER: HMCPU_CF_SET(pVCpu, HM_CHANGED_GUEST_EFER_MSR); break;
6627 case MSR_IA32_TSC: pSvmTransient->fUpdateTscOffsetting = true; break;
6628 case MSR_K8_FS_BASE:
6629 case MSR_K8_GS_BASE: HMCPU_CF_SET(pVCpu, HM_CHANGED_GUEST_SEGMENT_REGS); break;
6630 case MSR_IA32_SYSENTER_CS: HMCPU_CF_SET(pVCpu, HM_CHANGED_GUEST_SYSENTER_CS_MSR); break;
6631 case MSR_IA32_SYSENTER_EIP: HMCPU_CF_SET(pVCpu, HM_CHANGED_GUEST_SYSENTER_EIP_MSR); break;
6632 case MSR_IA32_SYSENTER_ESP: HMCPU_CF_SET(pVCpu, HM_CHANGED_GUEST_SYSENTER_ESP_MSR); break;
6633 }
6634 }
6635 }
6636 }
6637 else
6638 {
6639 /* MSR Read access. */
6640 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitRdmsr);
6641 Assert(pVmcb->ctrl.u64ExitInfo1 == SVM_EXIT1_MSR_READ);
6642 Log4(("MSR Read: idMsr=%#RX32\n", pCtx->ecx));
6643
6644 bool const fSupportsNextRipSave = hmR0SvmSupportsNextRipSave(pVCpu, pCtx);
6645 if (fSupportsNextRipSave)
6646 {
6647 rc = EMInterpretRdmsr(pVM, pVCpu, CPUMCTX2CORE(pCtx));
6648 if (RT_LIKELY(rc == VINF_SUCCESS))
6649 {
6650 pCtx->rip = pVmcb->ctrl.u64NextRIP;
6651 HMSVM_CHECK_SINGLE_STEP(pVCpu, rc);
6652 }
6653 else
6654 AssertMsg( rc == VERR_EM_INTERPRETER
6655 || rc == VINF_CPUM_R3_MSR_READ, ("hmR0SvmExitMsr: EMInterpretRdmsr failed rc=%Rrc\n", rc));
6656 }
6657 else
6658 {
6659 rc = VBOXSTRICTRC_TODO(EMInterpretInstruction(pVCpu, CPUMCTX2CORE(pCtx), 0));
6660 if (RT_UNLIKELY(rc != VINF_SUCCESS))
6661 {
6662 AssertMsg( rc == VERR_EM_INTERPRETER
6663 || rc == VINF_CPUM_R3_MSR_READ, ("hmR0SvmExitMsr: RdMsr. EMInterpretInstruction failed rc=%Rrc\n", rc));
6664 }
6665 /* RIP updated by EMInterpretInstruction(). */
6666 HMSVM_CHECK_SINGLE_STEP(pVCpu, rc);
6667 }
6668 }
6669
6670 /* RIP has been updated by EMInterpret[Rd|Wr]msr() or EMInterpretInstruction(). */
6671 return rc;
6672}
6673
6674
6675/**
6676 * \#VMEXIT handler for DRx read (SVM_EXIT_READ_DRx). Conditional \#VMEXIT.
6677 */
6678HMSVM_EXIT_DECL hmR0SvmExitReadDRx(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
6679{
6680 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
6681 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitDRxRead);
6682
6683 /** @todo Stepping with nested-guest. */
6684 if (!CPUMIsGuestInSvmNestedHwVirtMode(pCtx))
6685 {
6686 /* We should -not- get this #VMEXIT if the guest's debug registers were active. */
6687 if (pSvmTransient->fWasGuestDebugStateActive)
6688 {
6689 AssertMsgFailed(("hmR0SvmExitReadDRx: Unexpected exit %#RX32\n", (uint32_t)pSvmTransient->u64ExitCode));
6690 pVCpu->hm.s.u32HMError = (uint32_t)pSvmTransient->u64ExitCode;
6691 return VERR_SVM_UNEXPECTED_EXIT;
6692 }
6693
6694 /*
6695 * Lazy DR0-3 loading.
6696 */
6697 if (!pSvmTransient->fWasHyperDebugStateActive)
6698 {
6699 Assert(!DBGFIsStepping(pVCpu)); Assert(!pVCpu->hm.s.fSingleInstruction);
6700 Log5(("hmR0SvmExitReadDRx: Lazy loading guest debug registers\n"));
6701
6702 /* Don't intercept DRx read and writes. */
6703 PSVMVMCB pVmcb = pVCpu->hm.s.svm.pVmcb;
6704 pVmcb->ctrl.u16InterceptRdDRx = 0;
6705 pVmcb->ctrl.u16InterceptWrDRx = 0;
6706 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_INTERCEPTS;
6707
6708 /* We're playing with the host CPU state here, make sure we don't preempt or longjmp. */
6709 VMMRZCallRing3Disable(pVCpu);
6710 HM_DISABLE_PREEMPT();
6711
6712 /* Save the host & load the guest debug state, restart execution of the MOV DRx instruction. */
6713 CPUMR0LoadGuestDebugState(pVCpu, false /* include DR6 */);
6714 Assert(CPUMIsGuestDebugStateActive(pVCpu) || HC_ARCH_BITS == 32);
6715
6716 HM_RESTORE_PREEMPT();
6717 VMMRZCallRing3Enable(pVCpu);
6718
6719 STAM_COUNTER_INC(&pVCpu->hm.s.StatDRxContextSwitch);
6720 return VINF_SUCCESS;
6721 }
6722 }
6723
6724 /*
6725 * Interpret the read/writing of DRx.
6726 */
6727 /** @todo Decode assist. */
6728 VBOXSTRICTRC rc = EMInterpretInstruction(pVCpu, CPUMCTX2CORE(pCtx), 0 /* pvFault */);
6729 Log5(("hmR0SvmExitReadDRx: Emulated DRx access: rc=%Rrc\n", VBOXSTRICTRC_VAL(rc)));
6730 if (RT_LIKELY(rc == VINF_SUCCESS))
6731 {
6732 /* Not necessary for read accesses but whatever doesn't hurt for now, will be fixed with decode assist. */
6733 /** @todo CPUM should set this flag! */
6734 HMCPU_CF_SET(pVCpu, HM_CHANGED_GUEST_DEBUG);
6735 HMSVM_CHECK_SINGLE_STEP(pVCpu, rc);
6736 }
6737 else
6738 Assert(rc == VERR_EM_INTERPRETER);
6739 return VBOXSTRICTRC_TODO(rc);
6740}
6741
6742
6743/**
6744 * \#VMEXIT handler for DRx write (SVM_EXIT_WRITE_DRx). Conditional \#VMEXIT.
6745 */
6746HMSVM_EXIT_DECL hmR0SvmExitWriteDRx(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
6747{
6748 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
6749 /* For now it's the same since we interpret the instruction anyway. Will change when using of Decode Assist is implemented. */
6750 int rc = hmR0SvmExitReadDRx(pVCpu, pCtx, pSvmTransient);
6751 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitDRxWrite);
6752 STAM_COUNTER_DEC(&pVCpu->hm.s.StatExitDRxRead);
6753 return rc;
6754}
6755
6756
6757/**
6758 * \#VMEXIT handler for XCRx write (SVM_EXIT_XSETBV). Conditional \#VMEXIT.
6759 */
6760HMSVM_EXIT_DECL hmR0SvmExitXsetbv(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
6761{
6762 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
6763
6764 /** @todo decode assists... */
6765 VBOXSTRICTRC rcStrict = IEMExecOne(pVCpu);
6766 if (rcStrict == VINF_IEM_RAISED_XCPT)
6767 HMCPU_CF_SET(pVCpu, HM_CHANGED_ALL_GUEST);
6768
6769 pVCpu->hm.s.fLoadSaveGuestXcr0 = (pCtx->cr4 & X86_CR4_OSXSAVE) && pCtx->aXcr[0] != ASMGetXcr0();
6770 Log4(("hmR0SvmExitXsetbv: New XCR0=%#RX64 fLoadSaveGuestXcr0=%d (cr4=%RX64) rcStrict=%Rrc\n",
6771 pCtx->aXcr[0], pVCpu->hm.s.fLoadSaveGuestXcr0, pCtx->cr4, VBOXSTRICTRC_VAL(rcStrict)));
6772
6773 HMSVM_CHECK_SINGLE_STEP(pVCpu, rcStrict);
6774 return VBOXSTRICTRC_TODO(rcStrict);
6775}
6776
6777
6778/**
6779 * \#VMEXIT handler for I/O instructions (SVM_EXIT_IOIO). Conditional \#VMEXIT.
6780 */
6781HMSVM_EXIT_DECL hmR0SvmExitIOInstr(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
6782{
6783 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
6784
6785 /* I/O operation lookup arrays. */
6786 static uint32_t const s_aIOSize[8] = { 0, 1, 2, 0, 4, 0, 0, 0 }; /* Size of the I/O accesses in bytes. */
6787 static uint32_t const s_aIOOpAnd[8] = { 0, 0xff, 0xffff, 0, 0xffffffff, 0, 0, 0 }; /* AND masks for saving
6788 the result (in AL/AX/EAX). */
6789 Log4(("hmR0SvmExitIOInstr: CS:RIP=%04x:%#RX64\n", pCtx->cs.Sel, pCtx->rip));
6790
6791 PVM pVM = pVCpu->CTX_SUFF(pVM);
6792 PSVMVMCB pVmcb = hmR0SvmGetCurrentVmcb(pVCpu, pCtx);
6793
6794 /* Refer AMD spec. 15.10.2 "IN and OUT Behaviour" and Figure 15-2. "EXITINFO1 for IOIO Intercept" for the format. */
6795 SVMIOIOEXITINFO IoExitInfo;
6796 IoExitInfo.u = (uint32_t)pVmcb->ctrl.u64ExitInfo1;
6797 uint32_t uIOWidth = (IoExitInfo.u >> 4) & 0x7;
6798 uint32_t cbValue = s_aIOSize[uIOWidth];
6799 uint32_t uAndVal = s_aIOOpAnd[uIOWidth];
6800
6801 if (RT_UNLIKELY(!cbValue))
6802 {
6803 AssertMsgFailed(("hmR0SvmExitIOInstr: Invalid IO operation. uIOWidth=%u\n", uIOWidth));
6804 return VERR_EM_INTERPRETER;
6805 }
6806
6807 VBOXSTRICTRC rcStrict;
6808 bool fUpdateRipAlready = false;
6809 if (IoExitInfo.n.u1STR)
6810 {
6811#ifdef VBOX_WITH_2ND_IEM_STEP
6812 /* INS/OUTS - I/O String instruction. */
6813 /** @todo Huh? why can't we use the segment prefix information given by AMD-V
6814 * in EXITINFO1? Investigate once this thing is up and running. */
6815 Log4(("CS:RIP=%04x:%08RX64 %#06x/%u %c str\n", pCtx->cs.Sel, pCtx->rip, IoExitInfo.n.u16Port, cbValue,
6816 IoExitInfo.n.u1Type == SVM_IOIO_WRITE ? 'w' : 'r'));
6817 AssertReturn(pCtx->dx == IoExitInfo.n.u16Port, VERR_SVM_IPE_2);
6818 static IEMMODE const s_aenmAddrMode[8] =
6819 {
6820 (IEMMODE)-1, IEMMODE_16BIT, IEMMODE_32BIT, (IEMMODE)-1, IEMMODE_64BIT, (IEMMODE)-1, (IEMMODE)-1, (IEMMODE)-1
6821 };
6822 IEMMODE enmAddrMode = s_aenmAddrMode[(IoExitInfo.u >> 7) & 0x7];
6823 if (enmAddrMode != (IEMMODE)-1)
6824 {
6825 uint64_t cbInstr = pVmcb->ctrl.u64ExitInfo2 - pCtx->rip;
6826 if (cbInstr <= 15 && cbInstr >= 1)
6827 {
6828 Assert(cbInstr >= 1U + IoExitInfo.n.u1REP);
6829 if (IoExitInfo.n.u1Type == SVM_IOIO_WRITE)
6830 {
6831 /* Don't know exactly how to detect whether u3SEG is valid, currently
6832 only enabling it for Bulldozer and later with NRIP. OS/2 broke on
6833 2384 Opterons when only checking NRIP. */
6834 bool const fSupportsNextRipSave = hmR0SvmSupportsNextRipSave(pVCpu, pCtx);
6835 if ( fSupportsNextRipSave
6836 && pVM->cpum.ro.GuestFeatures.enmMicroarch >= kCpumMicroarch_AMD_15h_First)
6837 {
6838 AssertMsg(IoExitInfo.n.u3SEG == X86_SREG_DS || cbInstr > 1U + IoExitInfo.n.u1REP,
6839 ("u32Seg=%d cbInstr=%d u1REP=%d", IoExitInfo.n.u3SEG, cbInstr, IoExitInfo.n.u1REP));
6840 rcStrict = IEMExecStringIoWrite(pVCpu, cbValue, enmAddrMode, IoExitInfo.n.u1REP, (uint8_t)cbInstr,
6841 IoExitInfo.n.u3SEG, true /*fIoChecked*/);
6842 }
6843 else if (cbInstr == 1U + IoExitInfo.n.u1REP)
6844 rcStrict = IEMExecStringIoWrite(pVCpu, cbValue, enmAddrMode, IoExitInfo.n.u1REP, (uint8_t)cbInstr,
6845 X86_SREG_DS, true /*fIoChecked*/);
6846 else
6847 rcStrict = IEMExecOne(pVCpu);
6848 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitIOStringWrite);
6849 }
6850 else
6851 {
6852 AssertMsg(IoExitInfo.n.u3SEG == X86_SREG_ES /*=0*/, ("%#x\n", IoExitInfo.n.u3SEG));
6853 rcStrict = IEMExecStringIoRead(pVCpu, cbValue, enmAddrMode, IoExitInfo.n.u1REP, (uint8_t)cbInstr,
6854 true /*fIoChecked*/);
6855 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitIOStringRead);
6856 }
6857 }
6858 else
6859 {
6860 AssertMsgFailed(("rip=%RX64 nrip=%#RX64 cbInstr=%#RX64\n", pCtx->rip, pVmcb->ctrl.u64ExitInfo2, cbInstr));
6861 rcStrict = IEMExecOne(pVCpu);
6862 }
6863 }
6864 else
6865 {
6866 AssertMsgFailed(("IoExitInfo=%RX64\n", IoExitInfo.u));
6867 rcStrict = IEMExecOne(pVCpu);
6868 }
6869 fUpdateRipAlready = true;
6870
6871#else
6872 /* INS/OUTS - I/O String instruction. */
6873 PDISCPUSTATE pDis = &pVCpu->hm.s.DisState;
6874
6875 /** @todo Huh? why can't we use the segment prefix information given by AMD-V
6876 * in EXITINFO1? Investigate once this thing is up and running. */
6877
6878 rcStrict = EMInterpretDisasCurrent(pVM, pVCpu, pDis, NULL);
6879 if (rcStrict == VINF_SUCCESS)
6880 {
6881 if (IoExitInfo.n.u1Type == SVM_IOIO_WRITE)
6882 {
6883 rcStrict = IOMInterpretOUTSEx(pVM, pVCpu, CPUMCTX2CORE(pCtx), IoExitInfo.n.u16Port, pDis->fPrefix,
6884 (DISCPUMODE)pDis->uAddrMode, cbValue);
6885 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitIOStringWrite);
6886 }
6887 else
6888 {
6889 rcStrict = IOMInterpretINSEx(pVM, pVCpu, CPUMCTX2CORE(pCtx), IoExitInfo.n.u16Port, pDis->fPrefix,
6890 (DISCPUMODE)pDis->uAddrMode, cbValue);
6891 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitIOStringRead);
6892 }
6893 }
6894 else
6895 rcStrict = VINF_EM_RAW_EMULATE_INSTR;
6896#endif
6897 }
6898 else
6899 {
6900 /* IN/OUT - I/O instruction. */
6901 Assert(!IoExitInfo.n.u1REP);
6902
6903 if (IoExitInfo.n.u1Type == SVM_IOIO_WRITE)
6904 {
6905 rcStrict = IOMIOPortWrite(pVM, pVCpu, IoExitInfo.n.u16Port, pCtx->eax & uAndVal, cbValue);
6906 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitIOWrite);
6907 }
6908 else
6909 {
6910 uint32_t u32Val = 0;
6911 rcStrict = IOMIOPortRead(pVM, pVCpu, IoExitInfo.n.u16Port, &u32Val, cbValue);
6912 if (IOM_SUCCESS(rcStrict))
6913 {
6914 /* Save result of I/O IN instr. in AL/AX/EAX. */
6915 /** @todo r=bird: 32-bit op size should clear high bits of rax! */
6916 pCtx->eax = (pCtx->eax & ~uAndVal) | (u32Val & uAndVal);
6917 }
6918 else if (rcStrict == VINF_IOM_R3_IOPORT_READ)
6919 HMR0SavePendingIOPortRead(pVCpu, pCtx->rip, pVmcb->ctrl.u64ExitInfo2, IoExitInfo.n.u16Port, uAndVal, cbValue);
6920
6921 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitIORead);
6922 }
6923 }
6924
6925 if (IOM_SUCCESS(rcStrict))
6926 {
6927 /* AMD-V saves the RIP of the instruction following the IO instruction in EXITINFO2. */
6928 if (!fUpdateRipAlready)
6929 pCtx->rip = pVmcb->ctrl.u64ExitInfo2;
6930
6931 /*
6932 * If any I/O breakpoints are armed, we need to check if one triggered
6933 * and take appropriate action.
6934 * Note that the I/O breakpoint type is undefined if CR4.DE is 0.
6935 */
6936 /** @todo Optimize away the DBGFBpIsHwIoArmed call by having DBGF tell the
6937 * execution engines about whether hyper BPs and such are pending. */
6938 uint32_t const uDr7 = pCtx->dr[7];
6939 if (RT_UNLIKELY( ( (uDr7 & X86_DR7_ENABLED_MASK)
6940 && X86_DR7_ANY_RW_IO(uDr7)
6941 && (pCtx->cr4 & X86_CR4_DE))
6942 || DBGFBpIsHwIoArmed(pVM)))
6943 {
6944 /* We're playing with the host CPU state here, make sure we don't preempt or longjmp. */
6945 VMMRZCallRing3Disable(pVCpu);
6946 HM_DISABLE_PREEMPT();
6947
6948 STAM_COUNTER_INC(&pVCpu->hm.s.StatDRxIoCheck);
6949 CPUMR0DebugStateMaybeSaveGuest(pVCpu, false /*fDr6*/);
6950
6951 VBOXSTRICTRC rcStrict2 = DBGFBpCheckIo(pVM, pVCpu, pCtx, IoExitInfo.n.u16Port, cbValue);
6952 if (rcStrict2 == VINF_EM_RAW_GUEST_TRAP)
6953 {
6954 /* Raise #DB. */
6955 pVmcb->guest.u64DR6 = pCtx->dr[6];
6956 pVmcb->guest.u64DR7 = pCtx->dr[7];
6957 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_DRX;
6958 hmR0SvmSetPendingXcptDB(pVCpu);
6959 }
6960 /* rcStrict is VINF_SUCCESS, VINF_IOM_R3_IOPORT_COMMIT_WRITE, or in [VINF_EM_FIRST..VINF_EM_LAST],
6961 however we can ditch VINF_IOM_R3_IOPORT_COMMIT_WRITE as it has VMCPU_FF_IOM as backup. */
6962 else if ( rcStrict2 != VINF_SUCCESS
6963 && (rcStrict == VINF_SUCCESS || rcStrict2 < rcStrict))
6964 rcStrict = rcStrict2;
6965 AssertCompile(VINF_EM_LAST < VINF_IOM_R3_IOPORT_COMMIT_WRITE);
6966
6967 HM_RESTORE_PREEMPT();
6968 VMMRZCallRing3Enable(pVCpu);
6969 }
6970
6971 HMSVM_CHECK_SINGLE_STEP(pVCpu, rcStrict);
6972 }
6973
6974#ifdef VBOX_STRICT
6975 if (rcStrict == VINF_IOM_R3_IOPORT_READ)
6976 Assert(IoExitInfo.n.u1Type == SVM_IOIO_READ);
6977 else if (rcStrict == VINF_IOM_R3_IOPORT_WRITE || rcStrict == VINF_IOM_R3_IOPORT_COMMIT_WRITE)
6978 Assert(IoExitInfo.n.u1Type == SVM_IOIO_WRITE);
6979 else
6980 {
6981 /** @todo r=bird: This is missing a bunch of VINF_EM_FIRST..VINF_EM_LAST
6982 * statuses, that the VMM device and some others may return. See
6983 * IOM_SUCCESS() for guidance. */
6984 AssertMsg( RT_FAILURE(rcStrict)
6985 || rcStrict == VINF_SUCCESS
6986 || rcStrict == VINF_EM_RAW_EMULATE_INSTR
6987 || rcStrict == VINF_EM_DBG_BREAKPOINT
6988 || rcStrict == VINF_EM_RAW_GUEST_TRAP
6989 || rcStrict == VINF_EM_RAW_TO_R3
6990 || rcStrict == VINF_TRPM_XCPT_DISPATCHED
6991 || rcStrict == VINF_EM_TRIPLE_FAULT, ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)));
6992 }
6993#endif
6994 return VBOXSTRICTRC_TODO(rcStrict);
6995}
6996
6997
6998/**
6999 * \#VMEXIT handler for Nested Page-faults (SVM_EXIT_NPF). Conditional \#VMEXIT.
7000 */
7001HMSVM_EXIT_DECL hmR0SvmExitNestedPF(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
7002{
7003 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
7004 HMSVM_ASSERT_NOT_IN_NESTED_GUEST(pCtx);
7005
7006 PVM pVM = pVCpu->CTX_SUFF(pVM);
7007 Assert(pVM->hm.s.fNestedPaging);
7008
7009 HMSVM_CHECK_EXIT_DUE_TO_EVENT_DELIVERY();
7010
7011 /* See AMD spec. 15.25.6 "Nested versus Guest Page Faults, Fault Ordering" for VMCB details for #NPF. */
7012 PSVMVMCB pVmcb = pVCpu->hm.s.svm.pVmcb;
7013 uint32_t u32ErrCode = pVmcb->ctrl.u64ExitInfo1;
7014 RTGCPHYS GCPhysFaultAddr = pVmcb->ctrl.u64ExitInfo2;
7015
7016 Log4(("#NPF at CS:RIP=%04x:%#RX64 faultaddr=%RGp errcode=%#x \n", pCtx->cs.Sel, pCtx->rip, GCPhysFaultAddr, u32ErrCode));
7017
7018#ifdef VBOX_HM_WITH_GUEST_PATCHING
7019 /* TPR patching for 32-bit guests, using the reserved bit in the page tables for MMIO regions. */
7020 if ( pVM->hm.s.fTprPatchingAllowed
7021 && (GCPhysFaultAddr & PAGE_OFFSET_MASK) == XAPIC_OFF_TPR
7022 && ( !(u32ErrCode & X86_TRAP_PF_P) /* Not present */
7023 || (u32ErrCode & (X86_TRAP_PF_P | X86_TRAP_PF_RSVD)) == (X86_TRAP_PF_P | X86_TRAP_PF_RSVD)) /* MMIO page. */
7024 && !CPUMIsGuestInLongModeEx(pCtx)
7025 && !CPUMGetGuestCPL(pVCpu)
7026 && pVM->hm.s.cPatches < RT_ELEMENTS(pVM->hm.s.aPatches))
7027 {
7028 RTGCPHYS GCPhysApicBase = APICGetBaseMsrNoCheck(pVCpu);
7029 GCPhysApicBase &= PAGE_BASE_GC_MASK;
7030
7031 if (GCPhysFaultAddr == GCPhysApicBase + XAPIC_OFF_TPR)
7032 {
7033 /* Only attempt to patch the instruction once. */
7034 PHMTPRPATCH pPatch = (PHMTPRPATCH)RTAvloU32Get(&pVM->hm.s.PatchTree, (AVLOU32KEY)pCtx->eip);
7035 if (!pPatch)
7036 return VINF_EM_HM_PATCH_TPR_INSTR;
7037 }
7038 }
7039#endif
7040
7041 /*
7042 * Determine the nested paging mode.
7043 */
7044 PGMMODE enmNestedPagingMode;
7045#if HC_ARCH_BITS == 32
7046 if (CPUMIsGuestInLongModeEx(pCtx))
7047 enmNestedPagingMode = PGMMODE_AMD64_NX;
7048 else
7049#endif
7050 enmNestedPagingMode = PGMGetHostMode(pVM);
7051
7052 /*
7053 * MMIO optimization using the reserved (RSVD) bit in the guest page tables for MMIO pages.
7054 */
7055 int rc;
7056 Assert((u32ErrCode & (X86_TRAP_PF_RSVD | X86_TRAP_PF_P)) != X86_TRAP_PF_RSVD);
7057 if ((u32ErrCode & (X86_TRAP_PF_RSVD | X86_TRAP_PF_P)) == (X86_TRAP_PF_RSVD | X86_TRAP_PF_P))
7058 {
7059 /* If event delivery causes an MMIO #NPF, go back to instruction emulation as
7060 otherwise injecting the original pending event would most likely cause the same MMIO #NPF. */
7061 if (pVCpu->hm.s.Event.fPending)
7062 return VINF_EM_RAW_INJECT_TRPM_EVENT;
7063
7064 VBOXSTRICTRC rc2 = PGMR0Trap0eHandlerNPMisconfig(pVM, pVCpu, enmNestedPagingMode, CPUMCTX2CORE(pCtx), GCPhysFaultAddr,
7065 u32ErrCode);
7066 rc = VBOXSTRICTRC_VAL(rc2);
7067
7068 /*
7069 * If we succeed, resume guest execution.
7070 * If we fail in interpreting the instruction because we couldn't get the guest physical address
7071 * of the page containing the instruction via the guest's page tables (we would invalidate the guest page
7072 * in the host TLB), resume execution which would cause a guest page fault to let the guest handle this
7073 * weird case. See @bugref{6043}.
7074 */
7075 if ( rc == VINF_SUCCESS
7076 || rc == VERR_PAGE_TABLE_NOT_PRESENT
7077 || rc == VERR_PAGE_NOT_PRESENT)
7078 {
7079 /* Successfully handled MMIO operation. */
7080 HMCPU_CF_SET(pVCpu, HM_CHANGED_SVM_GUEST_APIC_STATE);
7081 rc = VINF_SUCCESS;
7082 }
7083 return rc;
7084 }
7085
7086 TRPMAssertXcptPF(pVCpu, GCPhysFaultAddr, u32ErrCode);
7087 rc = PGMR0Trap0eHandlerNestedPaging(pVM, pVCpu, enmNestedPagingMode, u32ErrCode, CPUMCTX2CORE(pCtx), GCPhysFaultAddr);
7088 TRPMResetTrap(pVCpu);
7089
7090 Log4(("#NPF: PGMR0Trap0eHandlerNestedPaging returned %Rrc CS:RIP=%04x:%#RX64\n", rc, pCtx->cs.Sel, pCtx->rip));
7091
7092 /*
7093 * Same case as PGMR0Trap0eHandlerNPMisconfig(). See comment above, @bugref{6043}.
7094 */
7095 if ( rc == VINF_SUCCESS
7096 || rc == VERR_PAGE_TABLE_NOT_PRESENT
7097 || rc == VERR_PAGE_NOT_PRESENT)
7098 {
7099 /* We've successfully synced our shadow page tables. */
7100 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitShadowPF);
7101 rc = VINF_SUCCESS;
7102 }
7103
7104 return rc;
7105}
7106
7107
7108/**
7109 * \#VMEXIT handler for virtual interrupt (SVM_EXIT_VINTR). Conditional
7110 * \#VMEXIT.
7111 */
7112HMSVM_EXIT_DECL hmR0SvmExitVIntr(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
7113{
7114 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
7115 HMSVM_ASSERT_NOT_IN_NESTED_GUEST(pCtx);
7116
7117 /* Indicate that we no longer need to #VMEXIT when the guest is ready to receive NMIs, it is now ready. */
7118 PSVMVMCB pVmcb = hmR0SvmGetCurrentVmcb(pVCpu, pCtx);
7119 hmR0SvmClearVirtIntrIntercept(pVmcb);
7120
7121 /* Deliver the pending interrupt via hmR0SvmEvaluatePendingEvent() and resume guest execution. */
7122 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitIntWindow);
7123 return VINF_SUCCESS;
7124}
7125
7126
7127/**
7128 * \#VMEXIT handler for task switches (SVM_EXIT_TASK_SWITCH). Conditional
7129 * \#VMEXIT.
7130 */
7131HMSVM_EXIT_DECL hmR0SvmExitTaskSwitch(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
7132{
7133 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
7134
7135 HMSVM_CHECK_EXIT_DUE_TO_EVENT_DELIVERY();
7136
7137#ifndef HMSVM_ALWAYS_TRAP_TASK_SWITCH
7138 Assert(!pVCpu->CTX_SUFF(pVM)->hm.s.fNestedPaging);
7139#endif
7140
7141 /* Check if this task-switch occurred while delivering an event through the guest IDT. */
7142 if (pVCpu->hm.s.Event.fPending) /* Can happen with exceptions/NMI. See @bugref{8411}. */
7143 {
7144 /*
7145 * AMD-V provides us with the exception which caused the TS; we collect
7146 * the information in the call to hmR0SvmCheckExitDueToEventDelivery.
7147 */
7148 Log4(("hmR0SvmExitTaskSwitch: TS occurred during event delivery.\n"));
7149 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitTaskSwitch);
7150 return VINF_EM_RAW_INJECT_TRPM_EVENT;
7151 }
7152
7153 /** @todo Emulate task switch someday, currently just going back to ring-3 for
7154 * emulation. */
7155 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitTaskSwitch);
7156 return VERR_EM_INTERPRETER;
7157}
7158
7159
7160/**
7161 * \#VMEXIT handler for VMMCALL (SVM_EXIT_VMMCALL). Conditional \#VMEXIT.
7162 */
7163HMSVM_EXIT_DECL hmR0SvmExitVmmCall(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
7164{
7165 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
7166 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitVmcall);
7167
7168 bool fRipUpdated;
7169 VBOXSTRICTRC rcStrict = HMSvmVmmcall(pVCpu, pCtx, &fRipUpdated);
7170 if (RT_SUCCESS(rcStrict))
7171 {
7172 /* Only update the RIP if we're continuing guest execution and not
7173 in the case of say VINF_GIM_R3_HYPERCALL. */
7174 if ( rcStrict == VINF_SUCCESS
7175 && !fRipUpdated)
7176 {
7177 hmR0SvmAdvanceRipHwAssist(pVCpu, pCtx, 3 /* cbInstr */);
7178 }
7179
7180 /* If the hypercall or TPR patching changes anything other than guest's general-purpose registers,
7181 we would need to reload the guest changed bits here before VM-entry. */
7182 return VBOXSTRICTRC_VAL(rcStrict);
7183 }
7184
7185 hmR0SvmSetPendingXcptUD(pVCpu);
7186 return VINF_SUCCESS;
7187}
7188
7189
7190/**
7191 * \#VMEXIT handler for VMMCALL (SVM_EXIT_VMMCALL). Conditional \#VMEXIT.
7192 */
7193HMSVM_EXIT_DECL hmR0SvmExitPause(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
7194{
7195 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
7196 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitPause);
7197 return VINF_EM_RAW_INTERRUPT;
7198}
7199
7200
7201/**
7202 * \#VMEXIT handler for IRET (SVM_EXIT_IRET). Conditional \#VMEXIT.
7203 */
7204HMSVM_EXIT_DECL hmR0SvmExitIret(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
7205{
7206 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
7207
7208 /* Clear NMI blocking. */
7209 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_BLOCK_NMIS);
7210
7211 /* Indicate that we no longer need to #VMEXIT when the guest is ready to receive NMIs, it is now ready. */
7212 PSVMVMCB pVmcb = hmR0SvmGetCurrentVmcb(pVCpu, pCtx);
7213 hmR0SvmClearIretIntercept(pVmcb);
7214
7215 /* Deliver the pending NMI via hmR0SvmEvaluatePendingEvent() and resume guest execution. */
7216 return VINF_SUCCESS;
7217}
7218
7219
7220/**
7221 * \#VMEXIT handler for page-fault exceptions (SVM_EXIT_EXCEPTION_14).
7222 * Conditional \#VMEXIT.
7223 */
7224HMSVM_EXIT_DECL hmR0SvmExitXcptPF(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
7225{
7226 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
7227 HMSVM_ASSERT_NOT_IN_NESTED_GUEST(pCtx);
7228
7229 HMSVM_CHECK_EXIT_DUE_TO_EVENT_DELIVERY();
7230
7231 /* See AMD spec. 15.12.15 "#PF (Page Fault)". */
7232 PSVMVMCB pVmcb = pVCpu->hm.s.svm.pVmcb;
7233 uint32_t u32ErrCode = pVmcb->ctrl.u64ExitInfo1;
7234 RTGCUINTPTR uFaultAddress = pVmcb->ctrl.u64ExitInfo2;
7235 PVM pVM = pVCpu->CTX_SUFF(pVM);
7236
7237#if defined(HMSVM_ALWAYS_TRAP_ALL_XCPTS) || defined(HMSVM_ALWAYS_TRAP_PF)
7238 if (pVM->hm.s.fNestedPaging)
7239 {
7240 pVCpu->hm.s.Event.fPending = false; /* In case it's a contributory or vectoring #PF. */
7241 if (!pSvmTransient->fVectoringDoublePF)
7242 {
7243 /* A genuine guest #PF, reflect it to the guest. */
7244 hmR0SvmSetPendingXcptPF(pVCpu, pCtx, u32ErrCode, uFaultAddress);
7245 Log4(("#PF: Guest page fault at %04X:%RGv FaultAddr=%RGv ErrCode=%#x\n", pCtx->cs.Sel, (RTGCPTR)pCtx->rip,
7246 uFaultAddress, u32ErrCode));
7247 }
7248 else
7249 {
7250 /* A guest page-fault occurred during delivery of a page-fault. Inject #DF. */
7251 hmR0SvmSetPendingXcptDF(pVCpu);
7252 Log4(("Pending #DF due to vectoring #PF. NP\n"));
7253 }
7254 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitGuestPF);
7255 return VINF_SUCCESS;
7256 }
7257#endif
7258
7259 Assert(!pVM->hm.s.fNestedPaging);
7260
7261#ifdef VBOX_HM_WITH_GUEST_PATCHING
7262 /* Shortcut for APIC TPR reads and writes; only applicable to 32-bit guests. */
7263 if ( pVM->hm.s.fTprPatchingAllowed
7264 && (uFaultAddress & 0xfff) == XAPIC_OFF_TPR
7265 && !(u32ErrCode & X86_TRAP_PF_P) /* Not present. */
7266 && !CPUMIsGuestInLongModeEx(pCtx)
7267 && !CPUMGetGuestCPL(pVCpu)
7268 && pVM->hm.s.cPatches < RT_ELEMENTS(pVM->hm.s.aPatches))
7269 {
7270 RTGCPHYS GCPhysApicBase;
7271 GCPhysApicBase = APICGetBaseMsrNoCheck(pVCpu);
7272 GCPhysApicBase &= PAGE_BASE_GC_MASK;
7273
7274 /* Check if the page at the fault-address is the APIC base. */
7275 RTGCPHYS GCPhysPage;
7276 int rc2 = PGMGstGetPage(pVCpu, (RTGCPTR)uFaultAddress, NULL /* pfFlags */, &GCPhysPage);
7277 if ( rc2 == VINF_SUCCESS
7278 && GCPhysPage == GCPhysApicBase)
7279 {
7280 /* Only attempt to patch the instruction once. */
7281 PHMTPRPATCH pPatch = (PHMTPRPATCH)RTAvloU32Get(&pVM->hm.s.PatchTree, (AVLOU32KEY)pCtx->eip);
7282 if (!pPatch)
7283 return VINF_EM_HM_PATCH_TPR_INSTR;
7284 }
7285 }
7286#endif
7287
7288 Log4(("#PF: uFaultAddress=%#RX64 CS:RIP=%#04x:%#RX64 u32ErrCode %#RX32 cr3=%#RX64\n", uFaultAddress, pCtx->cs.Sel,
7289 pCtx->rip, u32ErrCode, pCtx->cr3));
7290
7291 /* If it's a vectoring #PF, emulate injecting the original event injection as PGMTrap0eHandler() is incapable
7292 of differentiating between instruction emulation and event injection that caused a #PF. See @bugref{6607}. */
7293 if (pSvmTransient->fVectoringPF)
7294 {
7295 Assert(pVCpu->hm.s.Event.fPending);
7296 return VINF_EM_RAW_INJECT_TRPM_EVENT;
7297 }
7298
7299 TRPMAssertXcptPF(pVCpu, uFaultAddress, u32ErrCode);
7300 int rc = PGMTrap0eHandler(pVCpu, u32ErrCode, CPUMCTX2CORE(pCtx), (RTGCPTR)uFaultAddress);
7301
7302 Log4(("#PF rc=%Rrc\n", rc));
7303
7304 if (rc == VINF_SUCCESS)
7305 {
7306 /* Successfully synced shadow pages tables or emulated an MMIO instruction. */
7307 TRPMResetTrap(pVCpu);
7308 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitShadowPF);
7309 HMCPU_CF_SET(pVCpu, HM_CHANGED_ALL_GUEST);
7310 return rc;
7311 }
7312 else if (rc == VINF_EM_RAW_GUEST_TRAP)
7313 {
7314 pVCpu->hm.s.Event.fPending = false; /* In case it's a contributory or vectoring #PF. */
7315
7316 if (!pSvmTransient->fVectoringDoublePF)
7317 {
7318 /* It's a guest page fault and needs to be reflected to the guest. */
7319 u32ErrCode = TRPMGetErrorCode(pVCpu); /* The error code might have been changed. */
7320 TRPMResetTrap(pVCpu);
7321 hmR0SvmSetPendingXcptPF(pVCpu, pCtx, u32ErrCode, uFaultAddress);
7322 }
7323 else
7324 {
7325 /* A guest page-fault occurred during delivery of a page-fault. Inject #DF. */
7326 TRPMResetTrap(pVCpu);
7327 hmR0SvmSetPendingXcptDF(pVCpu);
7328 Log4(("#PF: Pending #DF due to vectoring #PF\n"));
7329 }
7330
7331 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitGuestPF);
7332 return VINF_SUCCESS;
7333 }
7334
7335 TRPMResetTrap(pVCpu);
7336 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitShadowPFEM);
7337 return rc;
7338}
7339
7340
7341/**
7342 * \#VMEXIT handler for device-not-available exceptions (SVM_EXIT_EXCEPTION_7).
7343 * Conditional \#VMEXIT.
7344 */
7345HMSVM_EXIT_DECL hmR0SvmExitXcptNM(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
7346{
7347 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
7348
7349 /* Paranoia; Ensure we cannot be called as a result of event delivery. */
7350 PSVMVMCB pVmcb = pVCpu->hm.s.svm.pVmcb;
7351 Assert(!pVmcb->ctrl.ExitIntInfo.n.u1Valid); NOREF(pVmcb);
7352
7353 /* We're playing with the host CPU state here, make sure we don't preempt or longjmp. */
7354 VMMRZCallRing3Disable(pVCpu);
7355 HM_DISABLE_PREEMPT();
7356
7357 int rc;
7358 /* If the guest FPU was active at the time of the #NM exit, then it's a guest fault. */
7359 if (pSvmTransient->fWasGuestFPUStateActive)
7360 {
7361 rc = VINF_EM_RAW_GUEST_TRAP;
7362 Assert(CPUMIsGuestFPUStateActive(pVCpu) || HMCPU_CF_IS_PENDING(pVCpu, HM_CHANGED_GUEST_CR0));
7363 }
7364 else
7365 {
7366#ifndef HMSVM_ALWAYS_TRAP_ALL_XCPTS
7367 Assert(!pSvmTransient->fWasGuestFPUStateActive);
7368#endif
7369 rc = CPUMR0Trap07Handler(pVCpu->CTX_SUFF(pVM), pVCpu); /* (No need to set HM_CHANGED_HOST_CONTEXT for SVM.) */
7370 Assert( rc == VINF_EM_RAW_GUEST_TRAP
7371 || ((rc == VINF_SUCCESS || rc == VINF_CPUM_HOST_CR0_MODIFIED) && CPUMIsGuestFPUStateActive(pVCpu)));
7372 }
7373
7374 HM_RESTORE_PREEMPT();
7375 VMMRZCallRing3Enable(pVCpu);
7376
7377 if (rc == VINF_SUCCESS || rc == VINF_CPUM_HOST_CR0_MODIFIED)
7378 {
7379 /* Guest FPU state was activated, we'll want to change CR0 FPU intercepts before the next VM-reentry. */
7380 HMCPU_CF_SET(pVCpu, HM_CHANGED_GUEST_CR0);
7381 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitShadowNM);
7382 pVCpu->hm.s.fPreloadGuestFpu = true;
7383 }
7384 else
7385 {
7386 /* Forward #NM to the guest. */
7387 Assert(rc == VINF_EM_RAW_GUEST_TRAP);
7388 hmR0SvmSetPendingXcptNM(pVCpu);
7389 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitGuestNM);
7390 }
7391 return VINF_SUCCESS;
7392}
7393
7394
7395/**
7396 * \#VMEXIT handler for undefined opcode (SVM_EXIT_EXCEPTION_6).
7397 * Conditional \#VMEXIT.
7398 */
7399HMSVM_EXIT_DECL hmR0SvmExitXcptUD(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
7400{
7401 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
7402
7403 /* Paranoia; Ensure we cannot be called as a result of event delivery. */
7404 PSVMVMCB pVmcb = pVCpu->hm.s.svm.pVmcb;
7405 Assert(!pVmcb->ctrl.ExitIntInfo.n.u1Valid); NOREF(pVmcb);
7406
7407 int rc = VERR_SVM_UNEXPECTED_XCPT_EXIT;
7408 if (pVCpu->hm.s.fGIMTrapXcptUD)
7409 {
7410 uint8_t cbInstr = 0;
7411 VBOXSTRICTRC rcStrict = GIMXcptUD(pVCpu, pCtx, NULL /* pDis */, &cbInstr);
7412 if (rcStrict == VINF_SUCCESS)
7413 {
7414 /* #UD #VMEXIT does not have valid NRIP information, manually advance RIP. See @bugref{7270#c170}. */
7415 hmR0SvmAdvanceRipDumb(pVCpu, pCtx, cbInstr);
7416 rc = VINF_SUCCESS;
7417 HMSVM_CHECK_SINGLE_STEP(pVCpu, rc);
7418 }
7419 else if (rcStrict == VINF_GIM_HYPERCALL_CONTINUING)
7420 rc = VINF_SUCCESS;
7421 else if (rcStrict == VINF_GIM_R3_HYPERCALL)
7422 rc = VINF_GIM_R3_HYPERCALL;
7423 else
7424 Assert(RT_FAILURE(VBOXSTRICTRC_VAL(rcStrict)));
7425 }
7426
7427 /* If the GIM #UD exception handler didn't succeed for some reason or wasn't needed, raise #UD. */
7428 if (RT_FAILURE(rc))
7429 {
7430 hmR0SvmSetPendingXcptUD(pVCpu);
7431 rc = VINF_SUCCESS;
7432 }
7433
7434 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitGuestUD);
7435 return rc;
7436}
7437
7438
7439/**
7440 * \#VMEXIT handler for math-fault exceptions (SVM_EXIT_EXCEPTION_16).
7441 * Conditional \#VMEXIT.
7442 */
7443HMSVM_EXIT_DECL hmR0SvmExitXcptMF(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
7444{
7445 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
7446
7447 /* Paranoia; Ensure we cannot be called as a result of event delivery. */
7448 PSVMVMCB pVmcb = pVCpu->hm.s.svm.pVmcb;
7449 Assert(!pVmcb->ctrl.ExitIntInfo.n.u1Valid); NOREF(pVmcb);
7450
7451 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitGuestMF);
7452
7453 if (!(pCtx->cr0 & X86_CR0_NE))
7454 {
7455 PVM pVM = pVCpu->CTX_SUFF(pVM);
7456 PDISSTATE pDis = &pVCpu->hm.s.DisState;
7457 unsigned cbOp;
7458 int rc = EMInterpretDisasCurrent(pVM, pVCpu, pDis, &cbOp);
7459 if (RT_SUCCESS(rc))
7460 {
7461 /* Convert a #MF into a FERR -> IRQ 13. See @bugref{6117}. */
7462 /** @todo FERR intercept when in nested-guest mode? */
7463 rc = PDMIsaSetIrq(pVCpu->CTX_SUFF(pVM), 13, 1, 0 /* uTagSrc */);
7464 if (RT_SUCCESS(rc))
7465 pCtx->rip += cbOp;
7466 }
7467 else
7468 Log4(("hmR0SvmExitXcptMF: EMInterpretDisasCurrent returned %Rrc uOpCode=%#x\n", rc, pDis->pCurInstr->uOpcode));
7469 return rc;
7470 }
7471
7472 hmR0SvmSetPendingXcptMF(pVCpu);
7473 return VINF_SUCCESS;
7474}
7475
7476
7477/**
7478 * \#VMEXIT handler for debug exceptions (SVM_EXIT_EXCEPTION_1). Conditional
7479 * \#VMEXIT.
7480 */
7481HMSVM_EXIT_DECL hmR0SvmExitXcptDB(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
7482{
7483 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
7484
7485 /* If this #DB is the result of delivering an event, go back to the interpreter. */
7486 HMSVM_CHECK_EXIT_DUE_TO_EVENT_DELIVERY();
7487 if (RT_UNLIKELY(pVCpu->hm.s.Event.fPending))
7488 {
7489 STAM_COUNTER_INC(&pVCpu->hm.s.StatInjectPendingInterpret);
7490 return VINF_EM_RAW_INJECT_TRPM_EVENT;
7491 }
7492
7493 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitGuestDB);
7494
7495 /* This can be a fault-type #DB (instruction breakpoint) or a trap-type #DB (data breakpoint). However, for both cases
7496 DR6 and DR7 are updated to what the exception handler expects. See AMD spec. 15.12.2 "#DB (Debug)". */
7497 PVM pVM = pVCpu->CTX_SUFF(pVM);
7498 PSVMVMCB pVmcb = pVCpu->hm.s.svm.pVmcb;
7499 int rc = DBGFRZTrap01Handler(pVM, pVCpu, CPUMCTX2CORE(pCtx), pVmcb->guest.u64DR6, pVCpu->hm.s.fSingleInstruction);
7500 if (rc == VINF_EM_RAW_GUEST_TRAP)
7501 {
7502 Log5(("hmR0SvmExitXcptDB: DR6=%#RX64 -> guest trap\n", pVmcb->guest.u64DR6));
7503 if (CPUMIsHyperDebugStateActive(pVCpu))
7504 CPUMSetGuestDR6(pVCpu, CPUMGetGuestDR6(pVCpu) | pVmcb->guest.u64DR6);
7505
7506 /* Reflect the exception back to the guest. */
7507 hmR0SvmSetPendingXcptDB(pVCpu);
7508 rc = VINF_SUCCESS;
7509 }
7510
7511 /*
7512 * Update DR6.
7513 */
7514 if (CPUMIsHyperDebugStateActive(pVCpu))
7515 {
7516 Log5(("hmR0SvmExitXcptDB: DR6=%#RX64 -> %Rrc\n", pVmcb->guest.u64DR6, rc));
7517 pVmcb->guest.u64DR6 = X86_DR6_INIT_VAL;
7518 pVmcb->ctrl.u32VmcbCleanBits &= ~HMSVM_VMCB_CLEAN_DRX;
7519 }
7520 else
7521 {
7522 AssertMsg(rc == VINF_SUCCESS, ("rc=%Rrc\n", rc));
7523 Assert(!pVCpu->hm.s.fSingleInstruction && !DBGFIsStepping(pVCpu));
7524 }
7525
7526 return rc;
7527}
7528
7529
7530/**
7531 * \#VMEXIT handler for alignment check exceptions (SVM_EXIT_EXCEPTION_17).
7532 * Conditional \#VMEXIT.
7533 */
7534HMSVM_EXIT_DECL hmR0SvmExitXcptAC(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
7535{
7536 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
7537
7538 /** @todo if triple-fault is returned in nested-guest scenario convert to a
7539 * shutdown VMEXIT. */
7540 HMSVM_CHECK_EXIT_DUE_TO_EVENT_DELIVERY();
7541
7542 SVMEVENT Event;
7543 Event.u = 0;
7544 Event.n.u1Valid = 1;
7545 Event.n.u3Type = SVM_EVENT_EXCEPTION;
7546 Event.n.u8Vector = X86_XCPT_AC;
7547 Event.n.u1ErrorCodeValid = 1;
7548 hmR0SvmSetPendingEvent(pVCpu, &Event, 0 /* GCPtrFaultAddress */);
7549 return VINF_SUCCESS;
7550}
7551
7552
7553/**
7554 * \#VMEXIT handler for breakpoint exceptions (SVM_EXIT_EXCEPTION_3).
7555 * Conditional \#VMEXIT.
7556 */
7557HMSVM_EXIT_DECL hmR0SvmExitXcptBP(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
7558{
7559 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
7560
7561 HMSVM_CHECK_EXIT_DUE_TO_EVENT_DELIVERY();
7562
7563 int rc = DBGFRZTrap03Handler(pVCpu->CTX_SUFF(pVM), pVCpu, CPUMCTX2CORE(pCtx));
7564 if (rc == VINF_EM_RAW_GUEST_TRAP)
7565 {
7566 SVMEVENT Event;
7567 Event.u = 0;
7568 Event.n.u1Valid = 1;
7569 Event.n.u3Type = SVM_EVENT_EXCEPTION;
7570 Event.n.u8Vector = X86_XCPT_BP;
7571 hmR0SvmSetPendingEvent(pVCpu, &Event, 0 /* GCPtrFaultAddress */);
7572 }
7573
7574 Assert(rc == VINF_SUCCESS || rc == VINF_EM_RAW_GUEST_TRAP || rc == VINF_EM_DBG_BREAKPOINT);
7575 return rc;
7576}
7577
7578
7579#if defined(HMSVM_ALWAYS_TRAP_ALL_XCPTS) || defined(VBOX_WITH_NESTED_HWVIRT)
7580/**
7581 * \#VMEXIT handler for generic exceptions. Conditional \#VMEXIT.
7582 */
7583HMSVM_EXIT_DECL hmR0SvmExitXcptGeneric(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
7584{
7585 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
7586
7587 HMSVM_CHECK_EXIT_DUE_TO_EVENT_DELIVERY();
7588
7589 PCSVMVMCB pVmcb = hmR0SvmGetCurrentVmcb(pVCpu, pCtx);
7590 uint8_t const uVector = pVmcb->ctrl.u64ExitCode - SVM_EXIT_EXCEPTION_0;
7591 uint32_t const uErrCode = pVmcb->ctrl.u64ExitInfo1;
7592 Assert(pSvmTransient->u64ExitCode == pVmcb->ctrl.u64ExitCode);
7593 Assert(uVector <= X86_XCPT_LAST);
7594 Log4(("hmR0SvmExitXcptGeneric: uVector=%#x uErrCode=%u\n", uVector, uErrCode));
7595
7596 SVMEVENT Event;
7597 Event.u = 0;
7598 Event.n.u1Valid = 1;
7599 Event.n.u3Type = SVM_EVENT_EXCEPTION;
7600 Event.n.u8Vector = uVector;
7601 switch (uVector)
7602 {
7603 /* Shouldn't be here for reflecting #PFs (among other things, the fault address isn't passed along). */
7604 case X86_XCPT_PF: AssertMsgFailed(("hmR0SvmExitXcptGeneric: Unexpected exception")); return VERR_SVM_IPE_5;
7605 case X86_XCPT_DF:
7606 case X86_XCPT_TS:
7607 case X86_XCPT_NP:
7608 case X86_XCPT_SS:
7609 case X86_XCPT_GP:
7610 case X86_XCPT_AC:
7611 {
7612 Event.n.u1ErrorCodeValid = 1;
7613 Event.n.u32ErrorCode = uErrCode;
7614 break;
7615 }
7616 }
7617
7618 hmR0SvmSetPendingEvent(pVCpu, &Event, 0 /* GCPtrFaultAddress */);
7619 return VINF_SUCCESS;
7620}
7621#endif
7622
7623#ifdef VBOX_WITH_NESTED_HWVIRT
7624/**
7625 * \#VMEXIT handler for #PF occuring while in nested-guest execution
7626 * (SVM_EXIT_EXCEPTION_14). Conditional \#VMEXIT.
7627 */
7628HMSVM_EXIT_DECL hmR0SvmExitXcptPFNested(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
7629{
7630 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
7631
7632 HMSVM_CHECK_EXIT_DUE_TO_EVENT_DELIVERY();
7633
7634 /* See AMD spec. 15.12.15 "#PF (Page Fault)". */
7635 PSVMVMCB pVmcb = hmR0SvmGetCurrentVmcb(pVCpu, pCtx);
7636 uint32_t u32ErrCode = pVmcb->ctrl.u64ExitInfo1;
7637 uint64_t const uFaultAddress = pVmcb->ctrl.u64ExitInfo2;
7638
7639 Log4(("#PFNested: uFaultAddress=%#RX64 CS:RIP=%#04x:%#RX64 u32ErrCode=%#RX32 CR3=%#RX64\n", uFaultAddress, pCtx->cs.Sel,
7640 pCtx->rip, u32ErrCode, pCtx->cr3));
7641
7642 /* If it's a vectoring #PF, emulate injecting the original event injection as PGMTrap0eHandler() is incapable
7643 of differentiating between instruction emulation and event injection that caused a #PF. See @bugref{6607}. */
7644 if (pSvmTransient->fVectoringPF)
7645 {
7646 Assert(pVCpu->hm.s.Event.fPending);
7647 return VINF_EM_RAW_INJECT_TRPM_EVENT;
7648 }
7649
7650 Assert(!pVCpu->CTX_SUFF(pVM)->hm.s.fNestedPaging);
7651
7652 TRPMAssertXcptPF(pVCpu, uFaultAddress, u32ErrCode);
7653 int rc = PGMTrap0eHandler(pVCpu, u32ErrCode, CPUMCTX2CORE(pCtx), (RTGCPTR)uFaultAddress);
7654
7655 Log4(("#PFNested: rc=%Rrc\n", rc));
7656
7657 if (rc == VINF_SUCCESS)
7658 {
7659 /* Successfully synced shadow pages tables or emulated an MMIO instruction. */
7660 TRPMResetTrap(pVCpu);
7661 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitShadowPF);
7662 HMCPU_CF_SET(pVCpu, HM_CHANGED_ALL_GUEST);
7663 return rc;
7664 }
7665
7666 if (rc == VINF_EM_RAW_GUEST_TRAP)
7667 {
7668 pVCpu->hm.s.Event.fPending = false; /* In case it's a contributory or vectoring #PF. */
7669
7670 if (!pSvmTransient->fVectoringDoublePF)
7671 {
7672 /* It's a nested-guest page fault and needs to be reflected to the nested-guest. */
7673 u32ErrCode = TRPMGetErrorCode(pVCpu); /* The error code might have been changed. */
7674 TRPMResetTrap(pVCpu);
7675 hmR0SvmSetPendingXcptPF(pVCpu, pCtx, u32ErrCode, uFaultAddress);
7676 }
7677 else
7678 {
7679 /* A nested-guest page-fault occurred during delivery of a page-fault. Inject #DF. */
7680 TRPMResetTrap(pVCpu);
7681 hmR0SvmSetPendingXcptDF(pVCpu);
7682 Log4(("#PF: Pending #DF due to vectoring #PF\n"));
7683 }
7684
7685 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitGuestPF);
7686 return VINF_SUCCESS;
7687 }
7688
7689 TRPMResetTrap(pVCpu);
7690 STAM_COUNTER_INC(&pVCpu->hm.s.StatExitShadowPFEM);
7691 return rc;
7692}
7693
7694
7695/**
7696 * \#VMEXIT handler for CLGI (SVM_EXIT_CLGI). Conditional \#VMEXIT.
7697 */
7698HMSVM_EXIT_DECL hmR0SvmExitClgi(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
7699{
7700 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
7701
7702#ifdef VBOX_STRICT
7703 PCSVMVMCB pVmcbTmp = hmR0SvmGetCurrentVmcb(pVCpu, pCtx);
7704 Assert(pVmcbTmp);
7705 Assert(!pVmcbTmp->ctrl.IntCtrl.n.u1VGifEnable);
7706 RT_NOREF(pVmcbTmp);
7707#endif
7708
7709 /** @todo Stat. */
7710 /* STAM_COUNTER_INC(&pVCpu->hm.s.StatExitClgi); */
7711 uint8_t const cbInstr = hmR0SvmGetInstrLengthHwAssist(pVCpu, pCtx, 3);
7712 VBOXSTRICTRC rcStrict = IEMExecDecodedClgi(pVCpu, cbInstr);
7713 return VBOXSTRICTRC_VAL(rcStrict);
7714}
7715
7716
7717/**
7718 * \#VMEXIT handler for STGI (SVM_EXIT_STGI). Conditional \#VMEXIT.
7719 */
7720HMSVM_EXIT_DECL hmR0SvmExitStgi(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
7721{
7722 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
7723
7724#ifdef VBOX_STRICT
7725 PCSVMVMCB pVmcb = hmR0SvmGetCurrentVmcb(pVCpu, pCtx);
7726 Assert(pVmcb);
7727 Assert(!pVmcb->ctrl.IntCtrl.n.u1VGifEnable);
7728 RT_NOREF(pVmcb);
7729#endif
7730
7731 /** @todo Stat. */
7732 /* STAM_COUNTER_INC(&pVCpu->hm.s.StatExitStgi); */
7733 uint8_t const cbInstr = hmR0SvmGetInstrLengthHwAssist(pVCpu, pCtx, 3);
7734 VBOXSTRICTRC rcStrict = IEMExecDecodedStgi(pVCpu, cbInstr);
7735 return VBOXSTRICTRC_VAL(rcStrict);
7736}
7737
7738
7739/**
7740 * \#VMEXIT handler for VMLOAD (SVM_EXIT_VMLOAD). Conditional \#VMEXIT.
7741 */
7742HMSVM_EXIT_DECL hmR0SvmExitVmload(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
7743{
7744 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
7745
7746#ifdef VBOX_STRICT
7747 PCSVMVMCB pVmcb = hmR0SvmGetCurrentVmcb(pVCpu, pCtx);
7748 Assert(pVmcb);
7749 Assert(!pVmcb->ctrl.LbrVirt.n.u1VirtVmsaveVmload);
7750 RT_NOREF(pVmcb);
7751#endif
7752
7753 /** @todo Stat. */
7754 /* STAM_COUNTER_INC(&pVCpu->hm.s.StatExitVmload); */
7755 uint8_t const cbInstr = hmR0SvmGetInstrLengthHwAssist(pVCpu, pCtx, 3);
7756 VBOXSTRICTRC rcStrict = IEMExecDecodedVmload(pVCpu, cbInstr);
7757 if (rcStrict == VINF_SUCCESS)
7758 {
7759 /* We skip flagging changes made to LSTAR, STAR, SFMASK and other MSRs as they are always re-loaded. */
7760 HMCPU_CF_SET(pVCpu, HM_CHANGED_GUEST_SEGMENT_REGS
7761 | HM_CHANGED_GUEST_TR
7762 | HM_CHANGED_GUEST_LDTR);
7763 }
7764 return VBOXSTRICTRC_VAL(rcStrict);
7765}
7766
7767
7768/**
7769 * \#VMEXIT handler for VMSAVE (SVM_EXIT_VMSAVE). Conditional \#VMEXIT.
7770 */
7771HMSVM_EXIT_DECL hmR0SvmExitVmsave(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
7772{
7773 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
7774
7775#ifdef VBOX_STRICT
7776 PCSVMVMCB pVmcb = hmR0SvmGetCurrentVmcb(pVCpu, pCtx);
7777 Assert(pVmcb);
7778 Assert(!pVmcb->ctrl.LbrVirt.n.u1VirtVmsaveVmload);
7779 RT_NOREF(pVmcb);
7780#endif
7781
7782 /** @todo Stat. */
7783 /* STAM_COUNTER_INC(&pVCpu->hm.s.StatExitVmsave); */
7784 uint8_t const cbInstr = hmR0SvmGetInstrLengthHwAssist(pVCpu, pCtx, 3);
7785 VBOXSTRICTRC rcStrict = IEMExecDecodedVmsave(pVCpu, cbInstr);
7786 return VBOXSTRICTRC_VAL(rcStrict);
7787}
7788
7789
7790/**
7791 * \#VMEXIT handler for INVLPGA (SVM_EXIT_INVLPGA). Conditional \#VMEXIT.
7792 */
7793HMSVM_EXIT_DECL hmR0SvmExitInvlpga(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
7794{
7795 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
7796 /** @todo Stat. */
7797 /* STAM_COUNTER_INC(&pVCpu->hm.s.StatExitInvlpga); */
7798 uint8_t const cbInstr = hmR0SvmGetInstrLengthHwAssist(pVCpu, pCtx, 3);
7799 VBOXSTRICTRC rcStrict = IEMExecDecodedInvlpga(pVCpu, cbInstr);
7800 return VBOXSTRICTRC_VAL(rcStrict);
7801}
7802
7803
7804/**
7805 * \#VMEXIT handler for STGI (SVM_EXIT_VMRUN). Conditional \#VMEXIT.
7806 */
7807HMSVM_EXIT_DECL hmR0SvmExitVmrun(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
7808{
7809 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
7810 /** @todo Stat. */
7811 /* STAM_COUNTER_INC(&pVCpu->hm.s.StatExitVmrun); */
7812#if 0
7813 VBOXSTRICTRC rcStrict;
7814 uint8_t const cbInstr = hmR0SvmGetInstrLengthHwAssist(pVCpu, pCtx, 3);
7815 rcStrict = IEMExecDecodedVmrun(pVCpu, cbInstr);
7816 Log4(("IEMExecDecodedVmrun: returned %d\n", VBOXSTRICTRC_VAL(rcStrict)));
7817 if (rcStrict == VINF_SUCCESS)
7818 {
7819 rcStrict = VINF_SVM_VMRUN;
7820 HMCPU_CF_SET(pVCpu, HM_CHANGED_ALL_GUEST);
7821 }
7822 return VBOXSTRICTRC_VAL(rcStrict);
7823#endif
7824 return VERR_EM_INTERPRETER;
7825}
7826
7827
7828/**
7829 * Nested-guest \#VMEXIT handler for debug exceptions (SVM_EXIT_EXCEPTION_1).
7830 * Unconditional \#VMEXIT.
7831 */
7832HMSVM_EXIT_DECL hmR0SvmNestedExitXcptDB(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
7833{
7834 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
7835
7836 /* If this #DB is the result of delivering an event, go back to the interpreter. */
7837 /** @todo if triple-fault is returned in nested-guest scenario convert to a
7838 * shutdown VMEXIT. */
7839 HMSVM_CHECK_EXIT_DUE_TO_EVENT_DELIVERY();
7840 if (RT_UNLIKELY(pVCpu->hm.s.Event.fPending))
7841 {
7842 STAM_COUNTER_INC(&pVCpu->hm.s.StatInjectPendingInterpret);
7843 return VINF_EM_RAW_INJECT_TRPM_EVENT;
7844 }
7845
7846 hmR0SvmSetPendingXcptDB(pVCpu);
7847 return VINF_SUCCESS;
7848}
7849
7850
7851/**
7852 * Nested-guest \#VMEXIT handler for breakpoint exceptions (SVM_EXIT_EXCEPTION_3).
7853 * Conditional \#VMEXIT.
7854 */
7855HMSVM_EXIT_DECL hmR0SvmNestedExitXcptBP(PVMCPU pVCpu, PCPUMCTX pCtx, PSVMTRANSIENT pSvmTransient)
7856{
7857 HMSVM_VALIDATE_EXIT_HANDLER_PARAMS();
7858
7859 /** @todo if triple-fault is returned in nested-guest scenario convert to a
7860 * shutdown VMEXIT. */
7861 HMSVM_CHECK_EXIT_DUE_TO_EVENT_DELIVERY();
7862
7863 SVMEVENT Event;
7864 Event.u = 0;
7865 Event.n.u1Valid = 1;
7866 Event.n.u3Type = SVM_EVENT_EXCEPTION;
7867 Event.n.u8Vector = X86_XCPT_BP;
7868 hmR0SvmSetPendingEvent(pVCpu, &Event, 0 /* GCPtrFaultAddress */);
7869 return VINF_SUCCESS;
7870}
7871
7872#endif /* VBOX_WITH_NESTED_HWVIRT */
7873
7874
7875/** @} */
7876
Note: See TracBrowser for help on using the repository browser.

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