VirtualBox

source: vbox/trunk/src/VBox/VMM/HWACCM.cpp@ 26685

Last change on this file since 26685 was 26685, checked in by vboxsync, 15 years ago

Large page changes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 129.4 KB
Line 
1/* $Id: HWACCM.cpp 26685 2010-02-22 17:48:23Z vboxsync $ */
2/** @file
3 * HWACCM - Intel/AMD VM Hardware Support Manager
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22/*******************************************************************************
23* Header Files *
24*******************************************************************************/
25#define LOG_GROUP LOG_GROUP_HWACCM
26#include <VBox/cpum.h>
27#include <VBox/stam.h>
28#include <VBox/mm.h>
29#include <VBox/pdmapi.h>
30#include <VBox/pgm.h>
31#include <VBox/ssm.h>
32#include <VBox/trpm.h>
33#include <VBox/dbgf.h>
34#include <VBox/iom.h>
35#include <VBox/patm.h>
36#include <VBox/csam.h>
37#include <VBox/selm.h>
38#include <VBox/rem.h>
39#include <VBox/hwacc_vmx.h>
40#include <VBox/hwacc_svm.h>
41#include "HWACCMInternal.h"
42#include <VBox/vm.h>
43#include <VBox/err.h>
44#include <VBox/param.h>
45
46#include <iprt/assert.h>
47#include <VBox/log.h>
48#include <iprt/asm.h>
49#include <iprt/string.h>
50#include <iprt/env.h>
51#include <iprt/thread.h>
52
53/*******************************************************************************
54* Global Variables *
55*******************************************************************************/
56#ifdef VBOX_WITH_STATISTICS
57# define EXIT_REASON(def, val, str) #def " - " #val " - " str
58# define EXIT_REASON_NIL() NULL
59/** Exit reason descriptions for VT-x, used to describe statistics. */
60static const char * const g_apszVTxExitReasons[MAX_EXITREASON_STAT] =
61{
62 EXIT_REASON(VMX_EXIT_EXCEPTION , 0, "Exception or non-maskable interrupt (NMI)."),
63 EXIT_REASON(VMX_EXIT_EXTERNAL_IRQ , 1, "External interrupt."),
64 EXIT_REASON(VMX_EXIT_TRIPLE_FAULT , 2, "Triple fault."),
65 EXIT_REASON(VMX_EXIT_INIT_SIGNAL , 3, "INIT signal."),
66 EXIT_REASON(VMX_EXIT_SIPI , 4, "Start-up IPI (SIPI)."),
67 EXIT_REASON(VMX_EXIT_IO_SMI_IRQ , 5, "I/O system-management interrupt (SMI)."),
68 EXIT_REASON(VMX_EXIT_SMI_IRQ , 6, "Other SMI."),
69 EXIT_REASON(VMX_EXIT_IRQ_WINDOW , 7, "Interrupt window."),
70 EXIT_REASON_NIL(),
71 EXIT_REASON(VMX_EXIT_TASK_SWITCH , 9, "Task switch."),
72 EXIT_REASON(VMX_EXIT_CPUID , 10, "Guest software attempted to execute CPUID."),
73 EXIT_REASON_NIL(),
74 EXIT_REASON(VMX_EXIT_HLT , 12, "Guest software attempted to execute HLT."),
75 EXIT_REASON(VMX_EXIT_INVD , 13, "Guest software attempted to execute INVD."),
76 EXIT_REASON(VMX_EXIT_INVPG , 14, "Guest software attempted to execute INVPG."),
77 EXIT_REASON(VMX_EXIT_RDPMC , 15, "Guest software attempted to execute RDPMC."),
78 EXIT_REASON(VMX_EXIT_RDTSC , 16, "Guest software attempted to execute RDTSC."),
79 EXIT_REASON(VMX_EXIT_RSM , 17, "Guest software attempted to execute RSM in SMM."),
80 EXIT_REASON(VMX_EXIT_VMCALL , 18, "Guest software executed VMCALL."),
81 EXIT_REASON(VMX_EXIT_VMCLEAR , 19, "Guest software executed VMCLEAR."),
82 EXIT_REASON(VMX_EXIT_VMLAUNCH , 20, "Guest software executed VMLAUNCH."),
83 EXIT_REASON(VMX_EXIT_VMPTRLD , 21, "Guest software executed VMPTRLD."),
84 EXIT_REASON(VMX_EXIT_VMPTRST , 22, "Guest software executed VMPTRST."),
85 EXIT_REASON(VMX_EXIT_VMREAD , 23, "Guest software executed VMREAD."),
86 EXIT_REASON(VMX_EXIT_VMRESUME , 24, "Guest software executed VMRESUME."),
87 EXIT_REASON(VMX_EXIT_VMWRITE , 25, "Guest software executed VMWRITE."),
88 EXIT_REASON(VMX_EXIT_VMXOFF , 26, "Guest software executed VMXOFF."),
89 EXIT_REASON(VMX_EXIT_VMXON , 27, "Guest software executed VMXON."),
90 EXIT_REASON(VMX_EXIT_CRX_MOVE , 28, "Control-register accesses."),
91 EXIT_REASON(VMX_EXIT_DRX_MOVE , 29, "Debug-register accesses."),
92 EXIT_REASON(VMX_EXIT_PORT_IO , 30, "I/O instruction."),
93 EXIT_REASON(VMX_EXIT_RDMSR , 31, "RDMSR. Guest software attempted to execute RDMSR."),
94 EXIT_REASON(VMX_EXIT_WRMSR , 32, "WRMSR. Guest software attempted to execute WRMSR."),
95 EXIT_REASON(VMX_EXIT_ERR_INVALID_GUEST_STATE, 33, "VM-entry failure due to invalid guest state."),
96 EXIT_REASON(VMX_EXIT_ERR_MSR_LOAD , 34, "VM-entry failure due to MSR loading."),
97 EXIT_REASON_NIL(),
98 EXIT_REASON(VMX_EXIT_MWAIT , 36, "Guest software executed MWAIT."),
99 EXIT_REASON_NIL(),
100 EXIT_REASON_NIL(),
101 EXIT_REASON(VMX_EXIT_MONITOR , 39, "Guest software attempted to execute MONITOR."),
102 EXIT_REASON(VMX_EXIT_PAUSE , 40, "Guest software attempted to execute PAUSE."),
103 EXIT_REASON(VMX_EXIT_ERR_MACHINE_CHECK , 41, "VM-entry failure due to machine-check."),
104 EXIT_REASON_NIL(),
105 EXIT_REASON(VMX_EXIT_TPR , 43, "TPR below threshold. Guest software executed MOV to CR8."),
106 EXIT_REASON(VMX_EXIT_APIC_ACCESS , 44, "APIC access. Guest software attempted to access memory at a physical address on the APIC-access page."),
107 EXIT_REASON_NIL(),
108 EXIT_REASON(VMX_EXIT_XDTR_ACCESS , 46, "Access to GDTR or IDTR. Guest software attempted to execute LGDT, LIDT, SGDT, or SIDT."),
109 EXIT_REASON(VMX_EXIT_TR_ACCESS , 47, "Access to LDTR or TR. Guest software attempted to execute LLDT, LTR, SLDT, or STR."),
110 EXIT_REASON(VMX_EXIT_EPT_VIOLATION , 48, "EPT violation. An attempt to access memory with a guest-physical address was disallowed by the configuration of the EPT paging structures."),
111 EXIT_REASON(VMX_EXIT_EPT_MISCONFIG , 49, "EPT misconfiguration. An attempt to access memory with a guest-physical address encountered a misconfigured EPT paging-structure entry."),
112 EXIT_REASON(VMX_EXIT_INVEPT , 50, "INVEPT. Guest software attempted to execute INVEPT."),
113 EXIT_REASON_NIL(),
114 EXIT_REASON(VMX_EXIT_PREEMPTION_TIMER , 52, "VMX-preemption timer expired. The preemption timer counted down to zero."),
115 EXIT_REASON(VMX_EXIT_INVVPID , 53, "INVVPID. Guest software attempted to execute INVVPID."),
116 EXIT_REASON(VMX_EXIT_WBINVD , 54, "WBINVD. Guest software attempted to execute WBINVD."),
117 EXIT_REASON(VMX_EXIT_XSETBV , 55, "XSETBV. Guest software attempted to execute XSETBV."),
118 EXIT_REASON_NIL()
119};
120/** Exit reason descriptions for AMD-V, used to describe statistics. */
121static const char * const g_apszAmdVExitReasons[MAX_EXITREASON_STAT] =
122{
123 EXIT_REASON(SVM_EXIT_READ_CR0 , 0, "Read CR0."),
124 EXIT_REASON(SVM_EXIT_READ_CR1 , 1, "Read CR1."),
125 EXIT_REASON(SVM_EXIT_READ_CR2 , 2, "Read CR2."),
126 EXIT_REASON(SVM_EXIT_READ_CR3 , 3, "Read CR3."),
127 EXIT_REASON(SVM_EXIT_READ_CR4 , 4, "Read CR4."),
128 EXIT_REASON(SVM_EXIT_READ_CR5 , 5, "Read CR5."),
129 EXIT_REASON(SVM_EXIT_READ_CR6 , 6, "Read CR6."),
130 EXIT_REASON(SVM_EXIT_READ_CR7 , 7, "Read CR7."),
131 EXIT_REASON(SVM_EXIT_READ_CR8 , 8, "Read CR8."),
132 EXIT_REASON(SVM_EXIT_READ_CR9 , 9, "Read CR9."),
133 EXIT_REASON(SVM_EXIT_READ_CR10 , 10, "Read CR10."),
134 EXIT_REASON(SVM_EXIT_READ_CR11 , 11, "Read CR11."),
135 EXIT_REASON(SVM_EXIT_READ_CR12 , 12, "Read CR12."),
136 EXIT_REASON(SVM_EXIT_READ_CR13 , 13, "Read CR13."),
137 EXIT_REASON(SVM_EXIT_READ_CR14 , 14, "Read CR14."),
138 EXIT_REASON(SVM_EXIT_READ_CR15 , 15, "Read CR15."),
139 EXIT_REASON(SVM_EXIT_WRITE_CR0 , 16, "Write CR0."),
140 EXIT_REASON(SVM_EXIT_WRITE_CR1 , 17, "Write CR1."),
141 EXIT_REASON(SVM_EXIT_WRITE_CR2 , 18, "Write CR2."),
142 EXIT_REASON(SVM_EXIT_WRITE_CR3 , 19, "Write CR3."),
143 EXIT_REASON(SVM_EXIT_WRITE_CR4 , 20, "Write CR4."),
144 EXIT_REASON(SVM_EXIT_WRITE_CR5 , 21, "Write CR5."),
145 EXIT_REASON(SVM_EXIT_WRITE_CR6 , 22, "Write CR6."),
146 EXIT_REASON(SVM_EXIT_WRITE_CR7 , 23, "Write CR7."),
147 EXIT_REASON(SVM_EXIT_WRITE_CR8 , 24, "Write CR8."),
148 EXIT_REASON(SVM_EXIT_WRITE_CR9 , 25, "Write CR9."),
149 EXIT_REASON(SVM_EXIT_WRITE_CR10 , 26, "Write CR10."),
150 EXIT_REASON(SVM_EXIT_WRITE_CR11 , 27, "Write CR11."),
151 EXIT_REASON(SVM_EXIT_WRITE_CR12 , 28, "Write CR12."),
152 EXIT_REASON(SVM_EXIT_WRITE_CR13 , 29, "Write CR13."),
153 EXIT_REASON(SVM_EXIT_WRITE_CR14 , 30, "Write CR14."),
154 EXIT_REASON(SVM_EXIT_WRITE_CR15 , 31, "Write CR15."),
155 EXIT_REASON(SVM_EXIT_READ_DR0 , 32, "Read DR0."),
156 EXIT_REASON(SVM_EXIT_READ_DR1 , 33, "Read DR1."),
157 EXIT_REASON(SVM_EXIT_READ_DR2 , 34, "Read DR2."),
158 EXIT_REASON(SVM_EXIT_READ_DR3 , 35, "Read DR3."),
159 EXIT_REASON(SVM_EXIT_READ_DR4 , 36, "Read DR4."),
160 EXIT_REASON(SVM_EXIT_READ_DR5 , 37, "Read DR5."),
161 EXIT_REASON(SVM_EXIT_READ_DR6 , 38, "Read DR6."),
162 EXIT_REASON(SVM_EXIT_READ_DR7 , 39, "Read DR7."),
163 EXIT_REASON(SVM_EXIT_READ_DR8 , 40, "Read DR8."),
164 EXIT_REASON(SVM_EXIT_READ_DR9 , 41, "Read DR9."),
165 EXIT_REASON(SVM_EXIT_READ_DR10 , 42, "Read DR10."),
166 EXIT_REASON(SVM_EXIT_READ_DR11 , 43, "Read DR11"),
167 EXIT_REASON(SVM_EXIT_READ_DR12 , 44, "Read DR12."),
168 EXIT_REASON(SVM_EXIT_READ_DR13 , 45, "Read DR13."),
169 EXIT_REASON(SVM_EXIT_READ_DR14 , 46, "Read DR14."),
170 EXIT_REASON(SVM_EXIT_READ_DR15 , 47, "Read DR15."),
171 EXIT_REASON(SVM_EXIT_WRITE_DR0 , 48, "Write DR0."),
172 EXIT_REASON(SVM_EXIT_WRITE_DR1 , 49, "Write DR1."),
173 EXIT_REASON(SVM_EXIT_WRITE_DR2 , 50, "Write DR2."),
174 EXIT_REASON(SVM_EXIT_WRITE_DR3 , 51, "Write DR3."),
175 EXIT_REASON(SVM_EXIT_WRITE_DR4 , 52, "Write DR4."),
176 EXIT_REASON(SVM_EXIT_WRITE_DR5 , 53, "Write DR5."),
177 EXIT_REASON(SVM_EXIT_WRITE_DR6 , 54, "Write DR6."),
178 EXIT_REASON(SVM_EXIT_WRITE_DR7 , 55, "Write DR7."),
179 EXIT_REASON(SVM_EXIT_WRITE_DR8 , 56, "Write DR8."),
180 EXIT_REASON(SVM_EXIT_WRITE_DR9 , 57, "Write DR9."),
181 EXIT_REASON(SVM_EXIT_WRITE_DR10 , 58, "Write DR10."),
182 EXIT_REASON(SVM_EXIT_WRITE_DR11 , 59, "Write DR11."),
183 EXIT_REASON(SVM_EXIT_WRITE_DR12 , 60, "Write DR12."),
184 EXIT_REASON(SVM_EXIT_WRITE_DR13 , 61, "Write DR13."),
185 EXIT_REASON(SVM_EXIT_WRITE_DR14 , 62, "Write DR14."),
186 EXIT_REASON(SVM_EXIT_WRITE_DR15 , 63, "Write DR15."),
187 EXIT_REASON(SVM_EXIT_EXCEPTION_0 , 64, "Exception Vector 0 (0x0)."),
188 EXIT_REASON(SVM_EXIT_EXCEPTION_1 , 65, "Exception Vector 1 (0x1)."),
189 EXIT_REASON(SVM_EXIT_EXCEPTION_2 , 66, "Exception Vector 2 (0x2)."),
190 EXIT_REASON(SVM_EXIT_EXCEPTION_3 , 67, "Exception Vector 3 (0x3)."),
191 EXIT_REASON(SVM_EXIT_EXCEPTION_4 , 68, "Exception Vector 4 (0x4)."),
192 EXIT_REASON(SVM_EXIT_EXCEPTION_5 , 69, "Exception Vector 5 (0x5)."),
193 EXIT_REASON(SVM_EXIT_EXCEPTION_6 , 70, "Exception Vector 6 (0x6)."),
194 EXIT_REASON(SVM_EXIT_EXCEPTION_7 , 71, "Exception Vector 7 (0x7)."),
195 EXIT_REASON(SVM_EXIT_EXCEPTION_8 , 72, "Exception Vector 8 (0x8)."),
196 EXIT_REASON(SVM_EXIT_EXCEPTION_9 , 73, "Exception Vector 9 (0x9)."),
197 EXIT_REASON(SVM_EXIT_EXCEPTION_A , 74, "Exception Vector 10 (0xA)."),
198 EXIT_REASON(SVM_EXIT_EXCEPTION_B , 75, "Exception Vector 11 (0xB)."),
199 EXIT_REASON(SVM_EXIT_EXCEPTION_C , 76, "Exception Vector 12 (0xC)."),
200 EXIT_REASON(SVM_EXIT_EXCEPTION_D , 77, "Exception Vector 13 (0xD)."),
201 EXIT_REASON(SVM_EXIT_EXCEPTION_E , 78, "Exception Vector 14 (0xE)."),
202 EXIT_REASON(SVM_EXIT_EXCEPTION_F , 79, "Exception Vector 15 (0xF)."),
203 EXIT_REASON(SVM_EXIT_EXCEPTION_10 , 80, "Exception Vector 16 (0x10)."),
204 EXIT_REASON(SVM_EXIT_EXCEPTION_11 , 81, "Exception Vector 17 (0x11)."),
205 EXIT_REASON(SVM_EXIT_EXCEPTION_12 , 82, "Exception Vector 18 (0x12)."),
206 EXIT_REASON(SVM_EXIT_EXCEPTION_13 , 83, "Exception Vector 19 (0x13)."),
207 EXIT_REASON(SVM_EXIT_EXCEPTION_14 , 84, "Exception Vector 20 (0x14)."),
208 EXIT_REASON(SVM_EXIT_EXCEPTION_15 , 85, "Exception Vector 22 (0x15)."),
209 EXIT_REASON(SVM_EXIT_EXCEPTION_16 , 86, "Exception Vector 22 (0x16)."),
210 EXIT_REASON(SVM_EXIT_EXCEPTION_17 , 87, "Exception Vector 23 (0x17)."),
211 EXIT_REASON(SVM_EXIT_EXCEPTION_18 , 88, "Exception Vector 24 (0x18)."),
212 EXIT_REASON(SVM_EXIT_EXCEPTION_19 , 89, "Exception Vector 25 (0x19)."),
213 EXIT_REASON(SVM_EXIT_EXCEPTION_1A , 90, "Exception Vector 26 (0x1A)."),
214 EXIT_REASON(SVM_EXIT_EXCEPTION_1B , 91, "Exception Vector 27 (0x1B)."),
215 EXIT_REASON(SVM_EXIT_EXCEPTION_1C , 92, "Exception Vector 28 (0x1C)."),
216 EXIT_REASON(SVM_EXIT_EXCEPTION_1D , 93, "Exception Vector 29 (0x1D)."),
217 EXIT_REASON(SVM_EXIT_EXCEPTION_1E , 94, "Exception Vector 30 (0x1E)."),
218 EXIT_REASON(SVM_EXIT_EXCEPTION_1F , 95, "Exception Vector 31 (0x1F)."),
219 EXIT_REASON(SVM_EXIT_EXCEPTION_INTR , 96, "Physical maskable interrupt."),
220 EXIT_REASON(SVM_EXIT_EXCEPTION_NMI , 97, "Physical non-maskable interrupt."),
221 EXIT_REASON(SVM_EXIT_EXCEPTION_SMI , 98, "System management interrupt."),
222 EXIT_REASON(SVM_EXIT_EXCEPTION_INIT , 99, "Physical INIT signal."),
223 EXIT_REASON(SVM_EXIT_EXCEPTION_VINTR ,100, "Visual interrupt."),
224 EXIT_REASON(SVM_EXIT_EXCEPTION_CR0_SEL_WRITE ,101, "Write to CR0 that changed any bits other than CR0.TS or CR0.MP."),
225 EXIT_REASON(SVM_EXIT_EXCEPTION_IDTR_READ ,102, "Read IDTR"),
226 EXIT_REASON(SVM_EXIT_EXCEPTION_GDTR_READ ,103, "Read GDTR"),
227 EXIT_REASON(SVM_EXIT_EXCEPTION_LDTR_READ ,104, "Read LDTR."),
228 EXIT_REASON(SVM_EXIT_EXCEPTION_TR_READ ,105, "Read TR."),
229 EXIT_REASON(SVM_EXIT_EXCEPTION_TR_READ ,106, "Write IDTR."),
230 EXIT_REASON(SVM_EXIT_EXCEPTION_TR_READ ,107, "Write GDTR."),
231 EXIT_REASON(SVM_EXIT_EXCEPTION_TR_READ ,108, "Write LDTR."),
232 EXIT_REASON(SVM_EXIT_EXCEPTION_TR_READ ,109, "Write TR."),
233 EXIT_REASON(SVM_EXIT_RDTSC ,110, "RDTSC instruction."),
234 EXIT_REASON(SVM_EXIT_RDPMC ,111, "RDPMC instruction."),
235 EXIT_REASON(SVM_EXIT_PUSHF ,112, "PUSHF instruction."),
236 EXIT_REASON(SVM_EXIT_POPF ,113, "POPF instruction."),
237 EXIT_REASON(SVM_EXIT_CPUID ,114, "CPUID instruction."),
238 EXIT_REASON(SVM_EXIT_RSM ,115, "RSM instruction."),
239 EXIT_REASON(SVM_EXIT_IRET ,116, "IRET instruction."),
240 EXIT_REASON(SVM_EXIT_SWINT ,117, "Software interrupt (INTn instructions)."),
241 EXIT_REASON(SVM_EXIT_INVD ,118, "INVD instruction."),
242 EXIT_REASON(SVM_EXIT_PAUSE ,119, "PAUSE instruction."),
243 EXIT_REASON(SVM_EXIT_HLT ,120, "HLT instruction."),
244 EXIT_REASON(SVM_EXIT_INVLPG ,121, "INVLPG instruction."),
245 EXIT_REASON(SVM_EXIT_INVLPGA ,122, "INVLPGA instruction."),
246 EXIT_REASON(SVM_EXIT_IOIO ,123, "IN/OUT accessing protected port (EXITINFO1 field provides more information)."),
247 EXIT_REASON(SVM_EXIT_MSR ,124, "RDMSR or WRMSR access to protected MSR."),
248 EXIT_REASON(SVM_EXIT_TASK_SWITCH ,125, "Task switch."),
249 EXIT_REASON(SVM_EXIT_FERR_FREEZE ,126, "FP legacy handling enabled, and processor is frozen in an x87/mmx instruction waiting for an interrupt"),
250 EXIT_REASON(SVM_EXIT_TASK_SHUTDOWN ,127, "Shutdown."),
251 EXIT_REASON(SVM_EXIT_TASK_VMRUN ,128, "VMRUN instruction."),
252 EXIT_REASON(SVM_EXIT_TASK_VMCALL ,129, "VMCALL instruction."),
253 EXIT_REASON(SVM_EXIT_TASK_VMLOAD ,130, "VMLOAD instruction."),
254 EXIT_REASON(SVM_EXIT_TASK_VMSAVE ,131, "VMSAVE instruction."),
255 EXIT_REASON(SVM_EXIT_TASK_STGI ,132, "STGI instruction."),
256 EXIT_REASON(SVM_EXIT_TASK_CLGI ,133, "CLGI instruction."),
257 EXIT_REASON(SVM_EXIT_TASK_SKINIT ,134, "SKINIT instruction."),
258 EXIT_REASON(SVM_EXIT_TASK_RDTSCP ,135, "RDTSCP instruction."),
259 EXIT_REASON(SVM_EXIT_TASK_ICEBP ,136, "ICEBP instruction."),
260 EXIT_REASON(SVM_EXIT_TASK_WBINVD ,137, "WBINVD instruction."),
261 EXIT_REASON(SVM_EXIT_TASK_MONITOR ,138, "MONITOR instruction."),
262 EXIT_REASON(SVM_EXIT_MWAIT_UNCOND ,139, "MWAIT instruction unconditional."),
263 EXIT_REASON(SVM_EXIT_MWAIT_ARMED ,140, "MWAIT instruction when armed."),
264 EXIT_REASON(SVM_EXIT_NPF ,1024, "Nested paging: host-level page fault occurred (EXITINFO1 contains fault errorcode; EXITINFO2 contains the guest physical address causing the fault)."),
265 EXIT_REASON_NIL()
266};
267# undef EXIT_REASON
268# undef EXIT_REASON_NIL
269#endif /* VBOX_WITH_STATISTICS */
270
271/*******************************************************************************
272* Internal Functions *
273*******************************************************************************/
274static DECLCALLBACK(int) hwaccmR3Save(PVM pVM, PSSMHANDLE pSSM);
275static DECLCALLBACK(int) hwaccmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
276
277
278/**
279 * Initializes the HWACCM.
280 *
281 * @returns VBox status code.
282 * @param pVM The VM to operate on.
283 */
284VMMR3DECL(int) HWACCMR3Init(PVM pVM)
285{
286 LogFlow(("HWACCMR3Init\n"));
287
288 /*
289 * Assert alignment and sizes.
290 */
291 AssertCompileMemberAlignment(VM, hwaccm.s, 32);
292 AssertCompile(sizeof(pVM->hwaccm.s) <= sizeof(pVM->hwaccm.padding));
293
294 /* Some structure checks. */
295 AssertReleaseMsg(RT_OFFSETOF(SVM_VMCB, u8Reserved3) == 0xC0, ("u8Reserved3 offset = %x\n", RT_OFFSETOF(SVM_VMCB, u8Reserved3)));
296 AssertReleaseMsg(RT_OFFSETOF(SVM_VMCB, ctrl.EventInject) == 0xA8, ("ctrl.EventInject offset = %x\n", RT_OFFSETOF(SVM_VMCB, ctrl.EventInject)));
297 AssertReleaseMsg(RT_OFFSETOF(SVM_VMCB, ctrl.ExitIntInfo) == 0x88, ("ctrl.ExitIntInfo offset = %x\n", RT_OFFSETOF(SVM_VMCB, ctrl.ExitIntInfo)));
298 AssertReleaseMsg(RT_OFFSETOF(SVM_VMCB, ctrl.TLBCtrl) == 0x58, ("ctrl.TLBCtrl offset = %x\n", RT_OFFSETOF(SVM_VMCB, ctrl.TLBCtrl)));
299
300 AssertReleaseMsg(RT_OFFSETOF(SVM_VMCB, guest) == 0x400, ("guest offset = %x\n", RT_OFFSETOF(SVM_VMCB, guest)));
301 AssertReleaseMsg(RT_OFFSETOF(SVM_VMCB, guest.u8Reserved4) == 0x4A0, ("guest.u8Reserved4 offset = %x\n", RT_OFFSETOF(SVM_VMCB, guest.u8Reserved4)));
302 AssertReleaseMsg(RT_OFFSETOF(SVM_VMCB, guest.u8Reserved6) == 0x4D8, ("guest.u8Reserved6 offset = %x\n", RT_OFFSETOF(SVM_VMCB, guest.u8Reserved6)));
303 AssertReleaseMsg(RT_OFFSETOF(SVM_VMCB, guest.u8Reserved7) == 0x580, ("guest.u8Reserved7 offset = %x\n", RT_OFFSETOF(SVM_VMCB, guest.u8Reserved7)));
304 AssertReleaseMsg(RT_OFFSETOF(SVM_VMCB, guest.u8Reserved9) == 0x648, ("guest.u8Reserved9 offset = %x\n", RT_OFFSETOF(SVM_VMCB, guest.u8Reserved9)));
305 AssertReleaseMsg(RT_OFFSETOF(SVM_VMCB, u8Reserved10) == 0x698, ("u8Reserved3 offset = %x\n", RT_OFFSETOF(SVM_VMCB, u8Reserved10)));
306 AssertReleaseMsg(sizeof(SVM_VMCB) == 0x1000, ("SVM_VMCB size = %x\n", sizeof(SVM_VMCB)));
307
308
309 /*
310 * Register the saved state data unit.
311 */
312 int rc = SSMR3RegisterInternal(pVM, "HWACCM", 0, HWACCM_SSM_VERSION, sizeof(HWACCM),
313 NULL, NULL, NULL,
314 NULL, hwaccmR3Save, NULL,
315 NULL, hwaccmR3Load, NULL);
316 if (RT_FAILURE(rc))
317 return rc;
318
319 /* Misc initialisation. */
320 pVM->hwaccm.s.vmx.fSupported = false;
321 pVM->hwaccm.s.svm.fSupported = false;
322 pVM->hwaccm.s.vmx.fEnabled = false;
323 pVM->hwaccm.s.svm.fEnabled = false;
324
325 pVM->hwaccm.s.fNestedPaging = false;
326
327 /* Disabled by default. */
328 pVM->fHWACCMEnabled = false;
329
330 /*
331 * Check CFGM options.
332 */
333 PCFGMNODE pRoot = CFGMR3GetRoot(pVM);
334 PCFGMNODE pHWVirtExt = CFGMR3GetChild(pRoot, "HWVirtExt/");
335 /* Nested paging: disabled by default. */
336 rc = CFGMR3QueryBoolDef(pHWVirtExt, "EnableNestedPaging", &pVM->hwaccm.s.fAllowNestedPaging, false);
337 AssertRC(rc);
338
339 /* VT-x VPID: disabled by default. */
340 rc = CFGMR3QueryBoolDef(pHWVirtExt, "EnableVPID", &pVM->hwaccm.s.vmx.fAllowVPID, false);
341 AssertRC(rc);
342
343 /* HWACCM support must be explicitely enabled in the configuration file. */
344 rc = CFGMR3QueryBoolDef(pHWVirtExt, "Enabled", &pVM->hwaccm.s.fAllowed, false);
345 AssertRC(rc);
346
347 /* TPR patching for 32 bits (Windows) guests with IO-APIC: disabled by default. */
348 rc = CFGMR3QueryBoolDef(pHWVirtExt, "TPRPatchingEnabled", &pVM->hwaccm.s.fTRPPatchingAllowed, false);
349 AssertRC(rc);
350
351#ifdef RT_OS_DARWIN
352 if (VMMIsHwVirtExtForced(pVM) != pVM->hwaccm.s.fAllowed)
353#else
354 if (VMMIsHwVirtExtForced(pVM) && !pVM->hwaccm.s.fAllowed)
355#endif
356 {
357 AssertLogRelMsgFailed(("VMMIsHwVirtExtForced=%RTbool fAllowed=%RTbool\n",
358 VMMIsHwVirtExtForced(pVM), pVM->hwaccm.s.fAllowed));
359 return VERR_HWACCM_CONFIG_MISMATCH;
360 }
361
362 if (VMMIsHwVirtExtForced(pVM))
363 pVM->fHWACCMEnabled = true;
364
365#if HC_ARCH_BITS == 32
366 /* 64-bit mode is configurable and it depends on both the kernel mode and VT-x.
367 * (To use the default, don't set 64bitEnabled in CFGM.) */
368 rc = CFGMR3QueryBoolDef(pHWVirtExt, "64bitEnabled", &pVM->hwaccm.s.fAllow64BitGuests, false);
369 AssertLogRelRCReturn(rc, rc);
370 if (pVM->hwaccm.s.fAllow64BitGuests)
371 {
372# ifdef RT_OS_DARWIN
373 if (!VMMIsHwVirtExtForced(pVM))
374# else
375 if (!pVM->hwaccm.s.fAllowed)
376# endif
377 return VM_SET_ERROR(pVM, VERR_INVALID_PARAMETER, "64-bit guest support was requested without also enabling HWVirtEx (VT-x/AMD-V).");
378 }
379#else
380 /* On 64-bit hosts 64-bit guest support is enabled by default, but allow this to be overridden
381 * via VBoxInternal/HWVirtExt/64bitEnabled=0. (ConsoleImpl2.cpp doesn't set this to false for 64-bit.) */
382 rc = CFGMR3QueryBoolDef(pHWVirtExt, "64bitEnabled", &pVM->hwaccm.s.fAllow64BitGuests, true);
383 AssertLogRelRCReturn(rc, rc);
384#endif
385
386
387 /** Determine the init method for AMD-V and VT-x; either one global init for each host CPU
388 * or local init each time we wish to execute guest code.
389 *
390 * Default false for Mac OS X and Windows due to the higher risk of conflicts with other hypervisors.
391 */
392 rc = CFGMR3QueryBoolDef(pHWVirtExt, "Exclusive", &pVM->hwaccm.s.fGlobalInit,
393#if defined(RT_OS_DARWIN) || defined(RT_OS_WINDOWS)
394 false
395#else
396 true
397#endif
398 );
399
400 /* Max number of resume loops. */
401 rc = CFGMR3QueryU32Def(pHWVirtExt, "MaxResumeLoops", &pVM->hwaccm.s.cMaxResumeLoops, 0 /* set by R0 later */);
402 AssertRC(rc);
403
404 return VINF_SUCCESS;
405}
406
407/**
408 * Initializes the per-VCPU HWACCM.
409 *
410 * @returns VBox status code.
411 * @param pVM The VM to operate on.
412 */
413VMMR3DECL(int) HWACCMR3InitCPU(PVM pVM)
414{
415 LogFlow(("HWACCMR3InitCPU\n"));
416
417 for (VMCPUID i = 0; i < pVM->cCpus; i++)
418 {
419 PVMCPU pVCpu = &pVM->aCpus[i];
420
421 pVCpu->hwaccm.s.fActive = false;
422 }
423
424#ifdef VBOX_WITH_STATISTICS
425 STAM_REG(pVM, &pVM->hwaccm.s.StatTPRPatchSuccess, STAMTYPE_COUNTER, "/HWACCM/TPR/Patch/Success", STAMUNIT_OCCURENCES, "Number of times an instruction was successfully patched.");
426 STAM_REG(pVM, &pVM->hwaccm.s.StatTPRPatchFailure, STAMTYPE_COUNTER, "/HWACCM/TPR/Patch/Failed", STAMUNIT_OCCURENCES, "Number of unsuccessful patch attempts.");
427 STAM_REG(pVM, &pVM->hwaccm.s.StatTPRReplaceSuccess, STAMTYPE_COUNTER, "/HWACCM/TPR/Replace/Success",STAMUNIT_OCCURENCES, "Number of times an instruction was successfully patched.");
428 STAM_REG(pVM, &pVM->hwaccm.s.StatTPRReplaceFailure, STAMTYPE_COUNTER, "/HWACCM/TPR/Replace/Failed", STAMUNIT_OCCURENCES, "Number of unsuccessful patch attempts.");
429
430 /*
431 * Statistics.
432 */
433 for (VMCPUID i = 0; i < pVM->cCpus; i++)
434 {
435 PVMCPU pVCpu = &pVM->aCpus[i];
436 int rc;
437
438 rc = STAMR3RegisterF(pVM, &pVCpu->hwaccm.s.StatPoke, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL, "Profiling of RTMpPokeCpu",
439 "/PROF/HWACCM/CPU%d/Poke", i);
440 AssertRC(rc);
441 rc = STAMR3RegisterF(pVM, &pVCpu->hwaccm.s.StatSpinPoke, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL, "Profiling of poke wait",
442 "/PROF/HWACCM/CPU%d/PokeWait", i);
443 AssertRC(rc);
444 rc = STAMR3RegisterF(pVM, &pVCpu->hwaccm.s.StatSpinPokeFailed, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL, "Profiling of poke wait when RTMpPokeCpu fails",
445 "/PROF/HWACCM/CPU%d/PokeWaitFailed", i);
446 AssertRC(rc);
447 rc = STAMR3RegisterF(pVM, &pVCpu->hwaccm.s.StatEntry, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL, "Profiling of VMXR0RunGuestCode entry",
448 "/PROF/HWACCM/CPU%d/SwitchToGC", i);
449 AssertRC(rc);
450 rc = STAMR3RegisterF(pVM, &pVCpu->hwaccm.s.StatExit1, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL, "Profiling of VMXR0RunGuestCode exit part 1",
451 "/PROF/HWACCM/CPU%d/SwitchFromGC_1", i);
452 AssertRC(rc);
453 rc = STAMR3RegisterF(pVM, &pVCpu->hwaccm.s.StatExit2, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL, "Profiling of VMXR0RunGuestCode exit part 2",
454 "/PROF/HWACCM/CPU%d/SwitchFromGC_2", i);
455 AssertRC(rc);
456# if 1 /* temporary for tracking down darwin holdup. */
457 rc = STAMR3RegisterF(pVM, &pVCpu->hwaccm.s.StatExit2Sub1, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL, "Temporary - I/O",
458 "/PROF/HWACCM/CPU%d/SwitchFromGC_2/Sub1", i);
459 AssertRC(rc);
460 rc = STAMR3RegisterF(pVM, &pVCpu->hwaccm.s.StatExit2Sub2, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL, "Temporary - CRx RWs",
461 "/PROF/HWACCM/CPU%d/SwitchFromGC_2/Sub2", i);
462 AssertRC(rc);
463 rc = STAMR3RegisterF(pVM, &pVCpu->hwaccm.s.StatExit2Sub3, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL, "Temporary - Exceptions",
464 "/PROF/HWACCM/CPU%d/SwitchFromGC_2/Sub3", i);
465 AssertRC(rc);
466# endif
467 rc = STAMR3RegisterF(pVM, &pVCpu->hwaccm.s.StatInGC, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL, "Profiling of vmlaunch",
468 "/PROF/HWACCM/CPU%d/InGC", i);
469 AssertRC(rc);
470
471# if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
472 rc = STAMR3RegisterF(pVM, &pVCpu->hwaccm.s.StatWorldSwitch3264, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL, "Profiling of the 32/64 switcher",
473 "/PROF/HWACCM/CPU%d/Switcher3264", i);
474 AssertRC(rc);
475# endif
476
477# define HWACCM_REG_COUNTER(a, b) \
478 rc = STAMR3RegisterF(pVM, a, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Profiling of vmlaunch", b, i); \
479 AssertRC(rc);
480
481 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitShadowNM, "/HWACCM/CPU%d/Exit/Trap/Shw/#NM");
482 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitGuestNM, "/HWACCM/CPU%d/Exit/Trap/Gst/#NM");
483 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitShadowPF, "/HWACCM/CPU%d/Exit/Trap/Shw/#PF");
484 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitGuestPF, "/HWACCM/CPU%d/Exit/Trap/Gst/#PF");
485 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitGuestUD, "/HWACCM/CPU%d/Exit/Trap/Gst/#UD");
486 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitGuestSS, "/HWACCM/CPU%d/Exit/Trap/Gst/#SS");
487 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitGuestNP, "/HWACCM/CPU%d/Exit/Trap/Gst/#NP");
488 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitGuestGP, "/HWACCM/CPU%d/Exit/Trap/Gst/#GP");
489 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitGuestMF, "/HWACCM/CPU%d/Exit/Trap/Gst/#MF");
490 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitGuestDE, "/HWACCM/CPU%d/Exit/Trap/Gst/#DE");
491 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitGuestDB, "/HWACCM/CPU%d/Exit/Trap/Gst/#DB");
492 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitInvpg, "/HWACCM/CPU%d/Exit/Instr/Invlpg");
493 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitInvd, "/HWACCM/CPU%d/Exit/Instr/Invd");
494 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitCpuid, "/HWACCM/CPU%d/Exit/Instr/Cpuid");
495 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitRdtsc, "/HWACCM/CPU%d/Exit/Instr/Rdtsc");
496 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitRdpmc, "/HWACCM/CPU%d/Exit/Instr/Rdpmc");
497 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitRdmsr, "/HWACCM/CPU%d/Exit/Instr/Rdmsr");
498 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitWrmsr, "/HWACCM/CPU%d/Exit/Instr/Wrmsr");
499 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitMwait, "/HWACCM/CPU%d/Exit/Instr/Mwait");
500 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitDRxWrite, "/HWACCM/CPU%d/Exit/Instr/DR/Write");
501 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitDRxRead, "/HWACCM/CPU%d/Exit/Instr/DR/Read");
502 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitCLTS, "/HWACCM/CPU%d/Exit/Instr/CLTS");
503 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitLMSW, "/HWACCM/CPU%d/Exit/Instr/LMSW");
504 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitCli, "/HWACCM/CPU%d/Exit/Instr/Cli");
505 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitSti, "/HWACCM/CPU%d/Exit/Instr/Sti");
506 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitPushf, "/HWACCM/CPU%d/Exit/Instr/Pushf");
507 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitPopf, "/HWACCM/CPU%d/Exit/Instr/Popf");
508 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitIret, "/HWACCM/CPU%d/Exit/Instr/Iret");
509 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitInt, "/HWACCM/CPU%d/Exit/Instr/Int");
510 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitHlt, "/HWACCM/CPU%d/Exit/Instr/Hlt");
511 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitIOWrite, "/HWACCM/CPU%d/Exit/IO/Write");
512 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitIORead, "/HWACCM/CPU%d/Exit/IO/Read");
513 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitIOStringWrite, "/HWACCM/CPU%d/Exit/IO/WriteString");
514 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitIOStringRead, "/HWACCM/CPU%d/Exit/IO/ReadString");
515 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitIrqWindow, "/HWACCM/CPU%d/Exit/IrqWindow");
516 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitMaxResume, "/HWACCM/CPU%d/Exit/MaxResume");
517 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatExitPreemptPending, "/HWACCM/CPU%d/Exit/PreemptPending");
518
519 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatSwitchGuestIrq, "/HWACCM/CPU%d/Switch/IrqPending");
520 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatSwitchToR3, "/HWACCM/CPU%d/Switch/ToR3");
521
522 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatIntInject, "/HWACCM/CPU%d/Irq/Inject");
523 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatIntReinject, "/HWACCM/CPU%d/Irq/Reinject");
524 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatPendingHostIrq, "/HWACCM/CPU%d/Irq/PendingOnHost");
525
526 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatFlushPage, "/HWACCM/CPU%d/Flush/Page");
527 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatFlushPageManual, "/HWACCM/CPU%d/Flush/Page/Virt");
528 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatFlushPhysPageManual, "/HWACCM/CPU%d/Flush/Page/Phys");
529 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatFlushTLB, "/HWACCM/CPU%d/Flush/TLB");
530 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatFlushTLBManual, "/HWACCM/CPU%d/Flush/TLB/Manual");
531 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatFlushTLBCRxChange, "/HWACCM/CPU%d/Flush/TLB/CRx");
532 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatFlushPageInvlpg, "/HWACCM/CPU%d/Flush/Page/Invlpg");
533 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatFlushTLBWorldSwitch, "/HWACCM/CPU%d/Flush/TLB/Switch");
534 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatNoFlushTLBWorldSwitch, "/HWACCM/CPU%d/Flush/TLB/Skipped");
535 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatFlushASID, "/HWACCM/CPU%d/Flush/TLB/ASID");
536 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatFlushTLBInvlpga, "/HWACCM/CPU%d/Flush/TLB/PhysInvl");
537 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatTlbShootdown, "/HWACCM/CPU%d/Flush/Shootdown/Page");
538 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatTlbShootdownFlush, "/HWACCM/CPU%d/Flush/Shootdown/TLB");
539
540 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatTSCOffset, "/HWACCM/CPU%d/TSC/Offset");
541 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatTSCIntercept, "/HWACCM/CPU%d/TSC/Intercept");
542 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatTSCInterceptOverFlow, "/HWACCM/CPU%d/TSC/InterceptOverflow");
543
544 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatDRxArmed, "/HWACCM/CPU%d/Debug/Armed");
545 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatDRxContextSwitch, "/HWACCM/CPU%d/Debug/ContextSwitch");
546 HWACCM_REG_COUNTER(&pVCpu->hwaccm.s.StatDRxIOCheck, "/HWACCM/CPU%d/Debug/IOCheck");
547
548 for (unsigned j=0;j<RT_ELEMENTS(pVCpu->hwaccm.s.StatExitCRxWrite);j++)
549 {
550 rc = STAMR3RegisterF(pVM, &pVCpu->hwaccm.s.StatExitCRxWrite[j], STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_OCCURENCES, "Profiling of CRx writes",
551 "/HWACCM/CPU%d/Exit/Instr/CR/Write/%x", i, j);
552 AssertRC(rc);
553 rc = STAMR3RegisterF(pVM, &pVCpu->hwaccm.s.StatExitCRxRead[j], STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_OCCURENCES, "Profiling of CRx reads",
554 "/HWACCM/CPU%d/Exit/Instr/CR/Read/%x", i, j);
555 AssertRC(rc);
556 }
557
558#undef HWACCM_REG_COUNTER
559
560 pVCpu->hwaccm.s.paStatExitReason = NULL;
561
562 rc = MMHyperAlloc(pVM, MAX_EXITREASON_STAT*sizeof(*pVCpu->hwaccm.s.paStatExitReason), 0, MM_TAG_HWACCM, (void **)&pVCpu->hwaccm.s.paStatExitReason);
563 AssertRC(rc);
564 if (RT_SUCCESS(rc))
565 {
566 const char * const *papszDesc = ASMIsIntelCpu() ? &g_apszVTxExitReasons[0] : &g_apszAmdVExitReasons[0];
567 for (int j=0;j<MAX_EXITREASON_STAT;j++)
568 {
569 if (papszDesc[j])
570 {
571 rc = STAMR3RegisterF(pVM, &pVCpu->hwaccm.s.paStatExitReason[j], STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_OCCURENCES,
572 papszDesc[j], "/HWACCM/CPU%d/Exit/Reason/%02x", i, j);
573 AssertRC(rc);
574 }
575 }
576 rc = STAMR3RegisterF(pVM, &pVCpu->hwaccm.s.StatExitReasonNPF, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_OCCURENCES, "Nested page fault", "/HWACCM/CPU%d/Exit/Reason/#NPF", i);
577 AssertRC(rc);
578 }
579 pVCpu->hwaccm.s.paStatExitReasonR0 = MMHyperR3ToR0(pVM, pVCpu->hwaccm.s.paStatExitReason);
580# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
581 Assert(pVCpu->hwaccm.s.paStatExitReasonR0 != NIL_RTR0PTR || !VMMIsHwVirtExtForced(pVM));
582# else
583 Assert(pVCpu->hwaccm.s.paStatExitReasonR0 != NIL_RTR0PTR);
584# endif
585
586 rc = MMHyperAlloc(pVM, sizeof(STAMCOUNTER) * 256, 8, MM_TAG_HWACCM, (void **)&pVCpu->hwaccm.s.paStatInjectedIrqs);
587 AssertRCReturn(rc, rc);
588 pVCpu->hwaccm.s.paStatInjectedIrqsR0 = MMHyperR3ToR0(pVM, pVCpu->hwaccm.s.paStatInjectedIrqs);
589# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
590 Assert(pVCpu->hwaccm.s.paStatInjectedIrqsR0 != NIL_RTR0PTR || !VMMIsHwVirtExtForced(pVM));
591# else
592 Assert(pVCpu->hwaccm.s.paStatInjectedIrqsR0 != NIL_RTR0PTR);
593# endif
594 for (unsigned j = 0; j < 255; j++)
595 STAMR3RegisterF(pVM, &pVCpu->hwaccm.s.paStatInjectedIrqs[j], STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_OCCURENCES, "Forwarded interrupts.",
596 (j < 0x20) ? "/HWACCM/CPU%d/Interrupt/Trap/%02X" : "/HWACCM/CPU%d/Interrupt/IRQ/%02X", i, j);
597
598 }
599#endif /* VBOX_WITH_STATISTICS */
600
601#ifdef VBOX_WITH_CRASHDUMP_MAGIC
602 /* Magic marker for searching in crash dumps. */
603 for (VMCPUID i = 0; i < pVM->cCpus; i++)
604 {
605 PVMCPU pVCpu = &pVM->aCpus[i];
606
607 PVMCSCACHE pCache = &pVCpu->hwaccm.s.vmx.VMCSCache;
608 strcpy((char *)pCache->aMagic, "VMCSCACHE Magic");
609 pCache->uMagic = UINT64_C(0xDEADBEEFDEADBEEF);
610 }
611#endif
612 return VINF_SUCCESS;
613}
614
615/**
616 * Turns off normal raw mode features
617 *
618 * @param pVM The VM to operate on.
619 */
620static void hwaccmR3DisableRawMode(PVM pVM)
621{
622 /* Disable PATM & CSAM. */
623 PATMR3AllowPatching(pVM, false);
624 CSAMDisableScanning(pVM);
625
626 /* Turn off IDT/LDT/GDT and TSS monitoring and sycing. */
627 SELMR3DisableMonitoring(pVM);
628 TRPMR3DisableMonitoring(pVM);
629
630 /* Disable the switcher code (safety precaution). */
631 VMMR3DisableSwitcher(pVM);
632
633 /* Disable mapping of the hypervisor into the shadow page table. */
634 PGMR3MappingsDisable(pVM);
635
636 /* Disable the switcher */
637 VMMR3DisableSwitcher(pVM);
638
639 /* Reinit the paging mode to force the new shadow mode. */
640 for (VMCPUID i = 0; i < pVM->cCpus; i++)
641 {
642 PVMCPU pVCpu = &pVM->aCpus[i];
643
644 PGMR3ChangeMode(pVM, pVCpu, PGMMODE_REAL);
645 }
646}
647
648/**
649 * Initialize VT-x or AMD-V.
650 *
651 * @returns VBox status code.
652 * @param pVM The VM handle.
653 */
654VMMR3DECL(int) HWACCMR3InitFinalizeR0(PVM pVM)
655{
656 int rc;
657
658 /* Hack to allow users to work around broken BIOSes that incorrectly set EFER.SVME, which makes us believe somebody else
659 * is already using AMD-V.
660 */
661 if ( !pVM->hwaccm.s.vmx.fSupported
662 && !pVM->hwaccm.s.svm.fSupported
663 && pVM->hwaccm.s.lLastError == VERR_SVM_IN_USE /* implies functional AMD-V */
664 && RTEnvExist("VBOX_HWVIRTEX_IGNORE_SVM_IN_USE"))
665 {
666 LogRel(("HWACCM: VBOX_HWVIRTEX_IGNORE_SVM_IN_USE active!\n"));
667 pVM->hwaccm.s.svm.fSupported = true;
668 pVM->hwaccm.s.svm.fIgnoreInUseError = true;
669 }
670 else
671 if ( !pVM->hwaccm.s.vmx.fSupported
672 && !pVM->hwaccm.s.svm.fSupported)
673 {
674 LogRel(("HWACCM: No VT-x or AMD-V CPU extension found. Reason %Rrc\n", pVM->hwaccm.s.lLastError));
675 LogRel(("HWACCM: VMX MSR_IA32_FEATURE_CONTROL=%RX64\n", pVM->hwaccm.s.vmx.msr.feature_ctrl));
676
677 if (VMMIsHwVirtExtForced(pVM))
678 {
679 switch (pVM->hwaccm.s.lLastError)
680 {
681 case VERR_VMX_NO_VMX:
682 return VM_SET_ERROR(pVM, VERR_VMX_NO_VMX, "VT-x is not available.");
683 case VERR_VMX_IN_VMX_ROOT_MODE:
684 return VM_SET_ERROR(pVM, VERR_VMX_IN_VMX_ROOT_MODE, "VT-x is being used by another hypervisor.");
685 case VERR_SVM_IN_USE:
686 return VM_SET_ERROR(pVM, VERR_SVM_IN_USE, "AMD-V is being used by another hypervisor.");
687 case VERR_SVM_NO_SVM:
688 return VM_SET_ERROR(pVM, VERR_SVM_NO_SVM, "AMD-V is not available.");
689 case VERR_SVM_DISABLED:
690 return VM_SET_ERROR(pVM, VERR_SVM_DISABLED, "AMD-V is disabled in the BIOS.");
691 default:
692 return pVM->hwaccm.s.lLastError;
693 }
694 }
695 return VINF_SUCCESS;
696 }
697
698 if (pVM->hwaccm.s.vmx.fSupported)
699 {
700 rc = SUPR3QueryVTxSupported();
701 if (RT_FAILURE(rc))
702 {
703#ifdef RT_OS_LINUX
704 LogRel(("HWACCM: The host kernel does not support VT-x -- Linux 2.6.13 or newer required!\n"));
705#else
706 LogRel(("HWACCM: The host kernel does not support VT-x!\n"));
707#endif
708 if ( pVM->cCpus > 1
709 || VMMIsHwVirtExtForced(pVM))
710 return rc;
711
712 /* silently fall back to raw mode */
713 return VINF_SUCCESS;
714 }
715 }
716
717 if (!pVM->hwaccm.s.fAllowed)
718 return VINF_SUCCESS; /* nothing to do */
719
720 /* Enable VT-x or AMD-V on all host CPUs. */
721 rc = SUPR3CallVMMR0Ex(pVM->pVMR0, 0 /*idCpu*/, VMMR0_DO_HWACC_ENABLE, 0, NULL);
722 if (RT_FAILURE(rc))
723 {
724 LogRel(("HWACCMR3InitFinalize: SUPR3CallVMMR0Ex VMMR0_DO_HWACC_ENABLE failed with %Rrc\n", rc));
725 return rc;
726 }
727 Assert(!pVM->fHWACCMEnabled || VMMIsHwVirtExtForced(pVM));
728
729 pVM->hwaccm.s.fHasIoApic = PDMHasIoApic(pVM);
730 /* No TPR patching is required when the IO-APIC is not enabled for this VM. (Main should have taken care of this already) */
731 if (!pVM->hwaccm.s.fHasIoApic)
732 {
733 Assert(!pVM->hwaccm.s.fTRPPatchingAllowed); /* paranoia */
734 pVM->hwaccm.s.fTRPPatchingAllowed = false;
735 }
736
737 if (pVM->hwaccm.s.vmx.fSupported)
738 {
739 Log(("pVM->hwaccm.s.vmx.fSupported = %d\n", pVM->hwaccm.s.vmx.fSupported));
740
741 if ( pVM->hwaccm.s.fInitialized == false
742 && pVM->hwaccm.s.vmx.msr.feature_ctrl != 0)
743 {
744 uint64_t val;
745 RTGCPHYS GCPhys = 0;
746
747 LogRel(("HWACCM: Host CR4=%08X\n", pVM->hwaccm.s.vmx.hostCR4));
748 LogRel(("HWACCM: MSR_IA32_FEATURE_CONTROL = %RX64\n", pVM->hwaccm.s.vmx.msr.feature_ctrl));
749 LogRel(("HWACCM: MSR_IA32_VMX_BASIC_INFO = %RX64\n", pVM->hwaccm.s.vmx.msr.vmx_basic_info));
750 LogRel(("HWACCM: VMCS id = %x\n", MSR_IA32_VMX_BASIC_INFO_VMCS_ID(pVM->hwaccm.s.vmx.msr.vmx_basic_info)));
751 LogRel(("HWACCM: VMCS size = %x\n", MSR_IA32_VMX_BASIC_INFO_VMCS_SIZE(pVM->hwaccm.s.vmx.msr.vmx_basic_info)));
752 LogRel(("HWACCM: VMCS physical address limit = %s\n", MSR_IA32_VMX_BASIC_INFO_VMCS_PHYS_WIDTH(pVM->hwaccm.s.vmx.msr.vmx_basic_info) ? "< 4 GB" : "None"));
753 LogRel(("HWACCM: VMCS memory type = %x\n", MSR_IA32_VMX_BASIC_INFO_VMCS_MEM_TYPE(pVM->hwaccm.s.vmx.msr.vmx_basic_info)));
754 LogRel(("HWACCM: Dual monitor treatment = %d\n", MSR_IA32_VMX_BASIC_INFO_VMCS_DUAL_MON(pVM->hwaccm.s.vmx.msr.vmx_basic_info)));
755
756 LogRel(("HWACCM: MSR_IA32_VMX_PINBASED_CTLS = %RX64\n", pVM->hwaccm.s.vmx.msr.vmx_pin_ctls.u));
757 val = pVM->hwaccm.s.vmx.msr.vmx_pin_ctls.n.allowed1;
758 if (val & VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_EXT_INT_EXIT)
759 LogRel(("HWACCM: VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_EXT_INT_EXIT\n"));
760 if (val & VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_NMI_EXIT)
761 LogRel(("HWACCM: VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_NMI_EXIT\n"));
762 if (val & VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_VIRTUAL_NMI)
763 LogRel(("HWACCM: VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_VIRTUAL_NMI\n"));
764 if (val & VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_PREEMPT_TIMER)
765 LogRel(("HWACCM: VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_PREEMPT_TIMER\n"));
766 val = pVM->hwaccm.s.vmx.msr.vmx_pin_ctls.n.disallowed0;
767 if (val & VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_EXT_INT_EXIT)
768 LogRel(("HWACCM: VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_EXT_INT_EXIT *must* be set\n"));
769 if (val & VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_NMI_EXIT)
770 LogRel(("HWACCM: VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_NMI_EXIT *must* be set\n"));
771 if (val & VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_VIRTUAL_NMI)
772 LogRel(("HWACCM: VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_VIRTUAL_NMI *must* be set\n"));
773 if (val & VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_PREEMPT_TIMER)
774 LogRel(("HWACCM: VMX_VMCS_CTRL_PIN_EXEC_CONTROLS_PREEMPT_TIMER *must* be set\n"));
775
776 LogRel(("HWACCM: MSR_IA32_VMX_PROCBASED_CTLS = %RX64\n", pVM->hwaccm.s.vmx.msr.vmx_proc_ctls.u));
777 val = pVM->hwaccm.s.vmx.msr.vmx_proc_ctls.n.allowed1;
778 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_IRQ_WINDOW_EXIT)
779 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_IRQ_WINDOW_EXIT\n"));
780 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_TSC_OFFSET)
781 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_TSC_OFFSET\n"));
782 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_HLT_EXIT)
783 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_HLT_EXIT\n"));
784 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_INVLPG_EXIT)
785 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_INVLPG_EXIT\n"));
786 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MWAIT_EXIT)
787 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MWAIT_EXIT\n"));
788 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_RDPMC_EXIT)
789 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_RDPMC_EXIT\n"));
790 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_RDTSC_EXIT)
791 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_RDTSC_EXIT\n"));
792 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR3_LOAD_EXIT)
793 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR3_LOAD_EXIT\n"));
794 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR3_STORE_EXIT)
795 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR3_STORE_EXIT\n"));
796 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR8_LOAD_EXIT)
797 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR8_LOAD_EXIT\n"));
798 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR8_STORE_EXIT)
799 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR8_STORE_EXIT\n"));
800 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_TPR_SHADOW)
801 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_TPR_SHADOW\n"));
802 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_NMI_WINDOW_EXIT)
803 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_NMI_WINDOW_EXIT\n"));
804 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MOV_DR_EXIT)
805 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MOV_DR_EXIT\n"));
806 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_UNCOND_IO_EXIT)
807 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_UNCOND_IO_EXIT\n"));
808 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_IO_BITMAPS)
809 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_IO_BITMAPS\n"));
810 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MONITOR_TRAP_FLAG)
811 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MONITOR_TRAP_FLAG\n"));
812 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_MSR_BITMAPS)
813 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_MSR_BITMAPS\n"));
814 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MONITOR_EXIT)
815 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MONITOR_EXIT\n"));
816 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_PAUSE_EXIT)
817 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_PAUSE_EXIT\n"));
818 if (val & VMX_VMCS_CTRL_PROC_EXEC_USE_SECONDARY_EXEC_CTRL)
819 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_USE_SECONDARY_EXEC_CTRL\n"));
820
821 val = pVM->hwaccm.s.vmx.msr.vmx_proc_ctls.n.disallowed0;
822 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_IRQ_WINDOW_EXIT)
823 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_IRQ_WINDOW_EXIT *must* be set\n"));
824 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_TSC_OFFSET)
825 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_TSC_OFFSET *must* be set\n"));
826 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_HLT_EXIT)
827 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_HLT_EXIT *must* be set\n"));
828 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_INVLPG_EXIT)
829 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_INVLPG_EXIT *must* be set\n"));
830 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MWAIT_EXIT)
831 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MWAIT_EXIT *must* be set\n"));
832 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_RDPMC_EXIT)
833 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_RDPMC_EXIT *must* be set\n"));
834 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_RDTSC_EXIT)
835 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_RDTSC_EXIT *must* be set\n"));
836 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR3_LOAD_EXIT)
837 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR3_LOAD_EXIT *must* be set\n"));
838 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR3_STORE_EXIT)
839 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR3_STORE_EXIT *must* be set\n"));
840 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR8_LOAD_EXIT)
841 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR8_LOAD_EXIT *must* be set\n"));
842 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR8_STORE_EXIT)
843 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_CR8_STORE_EXIT *must* be set\n"));
844 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_TPR_SHADOW)
845 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_TPR_SHADOW *must* be set\n"));
846 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_NMI_WINDOW_EXIT)
847 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_NMI_WINDOW_EXIT *must* be set\n"));
848 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MOV_DR_EXIT)
849 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MOV_DR_EXIT *must* be set\n"));
850 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_UNCOND_IO_EXIT)
851 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_UNCOND_IO_EXIT *must* be set\n"));
852 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_IO_BITMAPS)
853 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_IO_BITMAPS *must* be set\n"));
854 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MONITOR_TRAP_FLAG)
855 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MONITOR_TRAP_FLAG *must* be set\n"));
856 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_MSR_BITMAPS)
857 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_USE_MSR_BITMAPS *must* be set\n"));
858 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MONITOR_EXIT)
859 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_MONITOR_EXIT *must* be set\n"));
860 if (val & VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_PAUSE_EXIT)
861 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_CONTROLS_PAUSE_EXIT *must* be set\n"));
862 if (val & VMX_VMCS_CTRL_PROC_EXEC_USE_SECONDARY_EXEC_CTRL)
863 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC_USE_SECONDARY_EXEC_CTRL *must* be set\n"));
864
865 if (pVM->hwaccm.s.vmx.msr.vmx_proc_ctls.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC_USE_SECONDARY_EXEC_CTRL)
866 {
867 LogRel(("HWACCM: MSR_IA32_VMX_PROCBASED_CTLS2 = %RX64\n", pVM->hwaccm.s.vmx.msr.vmx_proc_ctls2.u));
868 val = pVM->hwaccm.s.vmx.msr.vmx_proc_ctls2.n.allowed1;
869 if (val & VMX_VMCS_CTRL_PROC_EXEC2_VIRT_APIC)
870 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC2_VIRT_APIC\n"));
871 if (val & VMX_VMCS_CTRL_PROC_EXEC2_EPT)
872 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC2_EPT\n"));
873 if (val & VMX_VMCS_CTRL_PROC_EXEC2_DESCRIPTOR_INSTR_EXIT)
874 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC2_DESCRIPTOR_INSTR_EXIT\n"));
875 if (val & VMX_VMCS_CTRL_PROC_EXEC2_RDTSCP_EXIT)
876 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC2_RDTSCP_EXIT\n"));
877 if (val & VMX_VMCS_CTRL_PROC_EXEC2_X2APIC)
878 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC2_X2APIC\n"));
879 if (val & VMX_VMCS_CTRL_PROC_EXEC2_VPID)
880 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC2_VPID\n"));
881 if (val & VMX_VMCS_CTRL_PROC_EXEC2_WBINVD_EXIT)
882 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC2_WBINVD_EXIT\n"));
883 if (val & VMX_VMCS_CTRL_PROC_EXEC2_REAL_MODE)
884 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC2_REAL_MODE\n"));
885 if (val & VMX_VMCS_CTRL_PROC_EXEC2_PAUSE_LOOP_EXIT)
886 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC2_PAUSE_LOOP_EXIT\n"));
887
888 val = pVM->hwaccm.s.vmx.msr.vmx_proc_ctls2.n.disallowed0;
889 if (val & VMX_VMCS_CTRL_PROC_EXEC2_VIRT_APIC)
890 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC2_VIRT_APIC *must* be set\n"));
891 if (val & VMX_VMCS_CTRL_PROC_EXEC2_DESCRIPTOR_INSTR_EXIT)
892 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC2_DESCRIPTOR_INSTR_EXIT *must* be set\n"));
893 if (val & VMX_VMCS_CTRL_PROC_EXEC2_RDTSCP_EXIT)
894 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC2_RDTSCP_EXIT *must* be set\n"));
895 if (val & VMX_VMCS_CTRL_PROC_EXEC2_X2APIC)
896 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC2_X2APIC *must* be set\n"));
897 if (val & VMX_VMCS_CTRL_PROC_EXEC2_EPT)
898 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC2_EPT *must* be set\n"));
899 if (val & VMX_VMCS_CTRL_PROC_EXEC2_VPID)
900 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC2_VPID *must* be set\n"));
901 if (val & VMX_VMCS_CTRL_PROC_EXEC2_WBINVD_EXIT)
902 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC2_WBINVD_EXIT *must* be set\n"));
903 if (val & VMX_VMCS_CTRL_PROC_EXEC2_REAL_MODE)
904 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC2_REAL_MODE *must* be set\n"));
905 if (val & VMX_VMCS_CTRL_PROC_EXEC2_PAUSE_LOOP_EXIT)
906 LogRel(("HWACCM: VMX_VMCS_CTRL_PROC_EXEC2_PAUSE_LOOP_EXIT *must* be set\n"));
907 }
908
909 LogRel(("HWACCM: MSR_IA32_VMX_ENTRY_CTLS = %RX64\n", pVM->hwaccm.s.vmx.msr.vmx_entry.u));
910 val = pVM->hwaccm.s.vmx.msr.vmx_entry.n.allowed1;
911 if (val & VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_DEBUG)
912 LogRel(("HWACCM: VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_DEBUG\n"));
913 if (val & VMX_VMCS_CTRL_ENTRY_CONTROLS_IA64_MODE)
914 LogRel(("HWACCM: VMX_VMCS_CTRL_ENTRY_CONTROLS_IA64_MODE\n"));
915 if (val & VMX_VMCS_CTRL_ENTRY_CONTROLS_ENTRY_SMM)
916 LogRel(("HWACCM: VMX_VMCS_CTRL_ENTRY_CONTROLS_ENTRY_SMM\n"));
917 if (val & VMX_VMCS_CTRL_ENTRY_CONTROLS_DEACTIVATE_DUALMON)
918 LogRel(("HWACCM: VMX_VMCS_CTRL_ENTRY_CONTROLS_DEACTIVATE_DUALMON\n"));
919 if (val & VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_GUEST_PERF_MSR)
920 LogRel(("HWACCM: VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_GUEST_PERF_MSR\n"));
921 if (val & VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_GUEST_PAT_MSR)
922 LogRel(("HWACCM: VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_GUEST_PAT_MSR\n"));
923 if (val & VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_GUEST_EFER_MSR)
924 LogRel(("HWACCM: VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_GUEST_EFER_MSR\n"));
925 val = pVM->hwaccm.s.vmx.msr.vmx_entry.n.disallowed0;
926 if (val & VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_DEBUG)
927 LogRel(("HWACCM: VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_DEBUG *must* be set\n"));
928 if (val & VMX_VMCS_CTRL_ENTRY_CONTROLS_IA64_MODE)
929 LogRel(("HWACCM: VMX_VMCS_CTRL_ENTRY_CONTROLS_IA64_MODE *must* be set\n"));
930 if (val & VMX_VMCS_CTRL_ENTRY_CONTROLS_ENTRY_SMM)
931 LogRel(("HWACCM: VMX_VMCS_CTRL_ENTRY_CONTROLS_ENTRY_SMM *must* be set\n"));
932 if (val & VMX_VMCS_CTRL_ENTRY_CONTROLS_DEACTIVATE_DUALMON)
933 LogRel(("HWACCM: VMX_VMCS_CTRL_ENTRY_CONTROLS_DEACTIVATE_DUALMON *must* be set\n"));
934 if (val & VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_GUEST_PERF_MSR)
935 LogRel(("HWACCM: VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_GUEST_PERF_MSR *must* be set\n"));
936 if (val & VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_GUEST_PAT_MSR)
937 LogRel(("HWACCM: VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_GUEST_PAT_MSR *must* be set\n"));
938 if (val & VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_GUEST_EFER_MSR)
939 LogRel(("HWACCM: VMX_VMCS_CTRL_ENTRY_CONTROLS_LOAD_GUEST_EFER_MSR *must* be set\n"));
940
941 LogRel(("HWACCM: MSR_IA32_VMX_EXIT_CTLS = %RX64\n", pVM->hwaccm.s.vmx.msr.vmx_exit.u));
942 val = pVM->hwaccm.s.vmx.msr.vmx_exit.n.allowed1;
943 if (val & VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_DEBUG)
944 LogRel(("HWACCM: VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_DEBUG\n"));
945 if (val & VMX_VMCS_CTRL_EXIT_CONTROLS_HOST_AMD64)
946 LogRel(("HWACCM: VMX_VMCS_CTRL_EXIT_CONTROLS_HOST_AMD64\n"));
947 if (val & VMX_VMCS_CTRL_EXIT_CONTROLS_ACK_EXTERNAL_IRQ)
948 LogRel(("HWACCM: VMX_VMCS_CTRL_EXIT_CONTROLS_ACK_EXTERNAL_IRQ\n"));
949 if (val & VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_GUEST_PAT_MSR)
950 LogRel(("HWACCM: VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_GUEST_PAT_MSR\n"));
951 if (val & VMX_VMCS_CTRL_EXIT_CONTROLS_LOAD_HOST_PAT_MSR)
952 LogRel(("HWACCM: VMX_VMCS_CTRL_EXIT_CONTROLS_LOAD_HOST_PAT_MSR\n"));
953 if (val & VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_GUEST_EFER_MSR)
954 LogRel(("HWACCM: VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_GUEST_EFER_MSR\n"));
955 if (val & VMX_VMCS_CTRL_EXIT_CONTROLS_LOAD_HOST_EFER_MSR)
956 LogRel(("HWACCM: VMX_VMCS_CTRL_EXIT_CONTROLS_LOAD_HOST_EFER_MSR\n"));
957 if (val & VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_VMX_PREEMPT_TIMER)
958 LogRel(("HWACCM: VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_VMX_PREEMPT_TIMER\n"));
959 val = pVM->hwaccm.s.vmx.msr.vmx_exit.n.disallowed0;
960 if (val & VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_DEBUG)
961 LogRel(("HWACCM: VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_DEBUG *must* be set\n"));
962 if (val & VMX_VMCS_CTRL_EXIT_CONTROLS_HOST_AMD64)
963 LogRel(("HWACCM: VMX_VMCS_CTRL_EXIT_CONTROLS_HOST_AMD64 *must* be set\n"));
964 if (val & VMX_VMCS_CTRL_EXIT_CONTROLS_ACK_EXTERNAL_IRQ)
965 LogRel(("HWACCM: VMX_VMCS_CTRL_EXIT_CONTROLS_ACK_EXTERNAL_IRQ *must* be set\n"));
966 if (val & VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_GUEST_PAT_MSR)
967 LogRel(("HWACCM: VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_GUEST_PAT_MSR *must* be set\n"));
968 if (val & VMX_VMCS_CTRL_EXIT_CONTROLS_LOAD_HOST_PAT_MSR)
969 LogRel(("HWACCM: VMX_VMCS_CTRL_EXIT_CONTROLS_LOAD_HOST_PAT_MSR *must* be set\n"));
970 if (val & VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_GUEST_EFER_MSR)
971 LogRel(("HWACCM: VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_GUEST_EFER_MSR *must* be set\n"));
972 if (val & VMX_VMCS_CTRL_EXIT_CONTROLS_LOAD_HOST_EFER_MSR)
973 LogRel(("HWACCM: VMX_VMCS_CTRL_EXIT_CONTROLS_LOAD_HOST_EFER_MSR *must* be set\n"));
974 if (val & VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_VMX_PREEMPT_TIMER)
975 LogRel(("HWACCM: VMX_VMCS_CTRL_EXIT_CONTROLS_SAVE_VMX_PREEMPT_TIMER *must* be set\n"));
976
977 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps)
978 {
979 LogRel(("HWACCM: MSR_IA32_VMX_EPT_VPID_CAPS = %RX64\n", pVM->hwaccm.s.vmx.msr.vmx_eptcaps));
980
981 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_RWX_X_ONLY)
982 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_RWX_X_ONLY\n"));
983 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_RWX_W_ONLY)
984 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_RWX_W_ONLY\n"));
985 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_RWX_WX_ONLY)
986 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_RWX_WX_ONLY\n"));
987 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_GAW_21_BITS)
988 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_GAW_21_BITS\n"));
989 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_GAW_30_BITS)
990 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_GAW_30_BITS\n"));
991 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_GAW_39_BITS)
992 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_GAW_39_BITS\n"));
993 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_GAW_48_BITS)
994 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_GAW_48_BITS\n"));
995 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_GAW_57_BITS)
996 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_GAW_57_BITS\n"));
997 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_EMT_UC)
998 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_EMT_UC\n"));
999 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_EMT_WC)
1000 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_EMT_WC\n"));
1001 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_EMT_WT)
1002 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_EMT_WT\n"));
1003 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_EMT_WP)
1004 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_EMT_WP\n"));
1005 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_EMT_WB)
1006 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_EMT_WB\n"));
1007 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_SP_21_BITS)
1008 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_SP_21_BITS\n"));
1009 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_SP_30_BITS)
1010 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_SP_30_BITS\n"));
1011 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_SP_39_BITS)
1012 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_SP_39_BITS\n"));
1013 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_SP_48_BITS)
1014 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_SP_48_BITS\n"));
1015 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_INVEPT)
1016 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_INVEPT\n"));
1017 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_INVEPT_CAPS_INDIV)
1018 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_INVEPT_CAPS_INDIV\n"));
1019 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_INVEPT_CAPS_CONTEXT)
1020 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_INVEPT_CAPS_CONTEXT\n"));
1021 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_INVEPT_CAPS_ALL)
1022 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_INVEPT_CAPS_ALL\n"));
1023 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_INVVPID)
1024 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_INVVPID\n"));
1025 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_INVVPID_CAPS_INDIV)
1026 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_INVVPID_CAPS_INDIV\n"));
1027 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_INVVPID_CAPS_CONTEXT)
1028 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_INVVPID_CAPS_CONTEXT\n"));
1029 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_INVVPID_CAPS_ALL)
1030 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_INVVPID_CAPS_ALL\n"));
1031 if (pVM->hwaccm.s.vmx.msr.vmx_eptcaps & MSR_IA32_VMX_EPT_CAPS_INVVPID_CAPS_CONTEXT_GLOBAL)
1032 LogRel(("HWACCM: MSR_IA32_VMX_EPT_CAPS_INVVPID_CAPS_CONTEXT_GLOBAL\n"));
1033 }
1034
1035 LogRel(("HWACCM: MSR_IA32_VMX_MISC = %RX64\n", pVM->hwaccm.s.vmx.msr.vmx_misc));
1036 LogRel(("HWACCM: MSR_IA32_VMX_MISC_PREEMPT_TSC_BIT %x\n", MSR_IA32_VMX_MISC_PREEMPT_TSC_BIT(pVM->hwaccm.s.vmx.msr.vmx_misc)));
1037 LogRel(("HWACCM: MSR_IA32_VMX_MISC_ACTIVITY_STATES %x\n", MSR_IA32_VMX_MISC_ACTIVITY_STATES(pVM->hwaccm.s.vmx.msr.vmx_misc)));
1038 LogRel(("HWACCM: MSR_IA32_VMX_MISC_CR3_TARGET %x\n", MSR_IA32_VMX_MISC_CR3_TARGET(pVM->hwaccm.s.vmx.msr.vmx_misc)));
1039 LogRel(("HWACCM: MSR_IA32_VMX_MISC_MAX_MSR %x\n", MSR_IA32_VMX_MISC_MAX_MSR(pVM->hwaccm.s.vmx.msr.vmx_misc)));
1040 LogRel(("HWACCM: MSR_IA32_VMX_MISC_MSEG_ID %x\n", MSR_IA32_VMX_MISC_MSEG_ID(pVM->hwaccm.s.vmx.msr.vmx_misc)));
1041
1042 LogRel(("HWACCM: MSR_IA32_VMX_CR0_FIXED0 = %RX64\n", pVM->hwaccm.s.vmx.msr.vmx_cr0_fixed0));
1043 LogRel(("HWACCM: MSR_IA32_VMX_CR0_FIXED1 = %RX64\n", pVM->hwaccm.s.vmx.msr.vmx_cr0_fixed1));
1044 LogRel(("HWACCM: MSR_IA32_VMX_CR4_FIXED0 = %RX64\n", pVM->hwaccm.s.vmx.msr.vmx_cr4_fixed0));
1045 LogRel(("HWACCM: MSR_IA32_VMX_CR4_FIXED1 = %RX64\n", pVM->hwaccm.s.vmx.msr.vmx_cr4_fixed1));
1046 LogRel(("HWACCM: MSR_IA32_VMX_VMCS_ENUM = %RX64\n", pVM->hwaccm.s.vmx.msr.vmx_vmcs_enum));
1047
1048 LogRel(("HWACCM: TPR shadow physaddr = %RHp\n", pVM->hwaccm.s.vmx.pAPICPhys));
1049
1050 /* Paranoia */
1051 AssertRelease(MSR_IA32_VMX_MISC_MAX_MSR(pVM->hwaccm.s.vmx.msr.vmx_misc) >= 512);
1052
1053 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1054 {
1055 LogRel(("HWACCM: VCPU%d: MSR bitmap physaddr = %RHp\n", i, pVM->aCpus[i].hwaccm.s.vmx.pMSRBitmapPhys));
1056 LogRel(("HWACCM: VCPU%d: VMCS physaddr = %RHp\n", i, pVM->aCpus[i].hwaccm.s.vmx.pVMCSPhys));
1057 }
1058
1059#ifdef HWACCM_VTX_WITH_EPT
1060 if (pVM->hwaccm.s.vmx.msr.vmx_proc_ctls2.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC2_EPT)
1061 pVM->hwaccm.s.fNestedPaging = pVM->hwaccm.s.fAllowNestedPaging;
1062#endif /* HWACCM_VTX_WITH_EPT */
1063#ifdef HWACCM_VTX_WITH_VPID
1064 if ( (pVM->hwaccm.s.vmx.msr.vmx_proc_ctls2.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC2_VPID)
1065 && !pVM->hwaccm.s.fNestedPaging) /* VPID and EPT are mutually exclusive. */
1066 pVM->hwaccm.s.vmx.fVPID = pVM->hwaccm.s.vmx.fAllowVPID;
1067#endif /* HWACCM_VTX_WITH_VPID */
1068
1069 /* Unrestricted guest execution relies on EPT. */
1070 if ( pVM->hwaccm.s.fNestedPaging
1071 && (pVM->hwaccm.s.vmx.msr.vmx_proc_ctls2.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC2_REAL_MODE))
1072 {
1073 pVM->hwaccm.s.vmx.fUnrestrictedGuest = true;
1074 }
1075
1076 /* Only try once. */
1077 pVM->hwaccm.s.fInitialized = true;
1078
1079 if (!pVM->hwaccm.s.vmx.fUnrestrictedGuest)
1080 {
1081 /* Allocate three pages for the TSS we need for real mode emulation. (2 pages for the IO bitmap) */
1082 rc = PDMR3VMMDevHeapAlloc(pVM, HWACCM_VTX_TOTAL_DEVHEAP_MEM, (RTR3PTR *)&pVM->hwaccm.s.vmx.pRealModeTSS);
1083 if (RT_SUCCESS(rc))
1084 {
1085 /* The I/O bitmap starts right after the virtual interrupt redirection bitmap. */
1086 ASMMemZero32(pVM->hwaccm.s.vmx.pRealModeTSS, sizeof(*pVM->hwaccm.s.vmx.pRealModeTSS));
1087 pVM->hwaccm.s.vmx.pRealModeTSS->offIoBitmap = sizeof(*pVM->hwaccm.s.vmx.pRealModeTSS);
1088 /* Bit set to 0 means redirection enabled. */
1089 memset(pVM->hwaccm.s.vmx.pRealModeTSS->IntRedirBitmap, 0x0, sizeof(pVM->hwaccm.s.vmx.pRealModeTSS->IntRedirBitmap));
1090 /* Allow all port IO, so the VT-x IO intercepts do their job. */
1091 memset(pVM->hwaccm.s.vmx.pRealModeTSS + 1, 0, PAGE_SIZE*2);
1092 *((unsigned char *)pVM->hwaccm.s.vmx.pRealModeTSS + HWACCM_VTX_TSS_SIZE - 2) = 0xff;
1093
1094 /* Construct a 1024 element page directory with 4 MB pages for the identity mapped page table used in
1095 * real and protected mode without paging with EPT.
1096 */
1097 pVM->hwaccm.s.vmx.pNonPagingModeEPTPageTable = (PX86PD)((char *)pVM->hwaccm.s.vmx.pRealModeTSS + PAGE_SIZE * 3);
1098 for (unsigned i=0;i<X86_PG_ENTRIES;i++)
1099 {
1100 pVM->hwaccm.s.vmx.pNonPagingModeEPTPageTable->a[i].u = _4M * i;
1101 pVM->hwaccm.s.vmx.pNonPagingModeEPTPageTable->a[i].u |= X86_PDE4M_P | X86_PDE4M_RW | X86_PDE4M_US | X86_PDE4M_A | X86_PDE4M_D | X86_PDE4M_PS | X86_PDE4M_G;
1102 }
1103
1104 /* We convert it here every time as pci regions could be reconfigured. */
1105 rc = PDMVMMDevHeapR3ToGCPhys(pVM, pVM->hwaccm.s.vmx.pRealModeTSS, &GCPhys);
1106 AssertRC(rc);
1107 LogRel(("HWACCM: Real Mode TSS guest physaddr = %RGp\n", GCPhys));
1108
1109 rc = PDMVMMDevHeapR3ToGCPhys(pVM, pVM->hwaccm.s.vmx.pNonPagingModeEPTPageTable, &GCPhys);
1110 AssertRC(rc);
1111 LogRel(("HWACCM: Non-Paging Mode EPT CR3 = %RGp\n", GCPhys));
1112 }
1113 else
1114 {
1115 LogRel(("HWACCM: No real mode VT-x support (PDMR3VMMDevHeapAlloc returned %Rrc)\n", rc));
1116 pVM->hwaccm.s.vmx.pRealModeTSS = NULL;
1117 pVM->hwaccm.s.vmx.pNonPagingModeEPTPageTable = NULL;
1118 }
1119 }
1120
1121 rc = SUPR3CallVMMR0Ex(pVM->pVMR0, 0 /*idCpu*/, VMMR0_DO_HWACC_SETUP_VM, 0, NULL);
1122 AssertRC(rc);
1123 if (rc == VINF_SUCCESS)
1124 {
1125 pVM->fHWACCMEnabled = true;
1126 pVM->hwaccm.s.vmx.fEnabled = true;
1127 hwaccmR3DisableRawMode(pVM);
1128
1129 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_SEP);
1130#ifdef VBOX_ENABLE_64_BITS_GUESTS
1131 if (pVM->hwaccm.s.fAllow64BitGuests)
1132 {
1133 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_PAE);
1134 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_LONG_MODE);
1135 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_SYSCALL); /* 64 bits only on Intel CPUs */
1136 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_LAHF);
1137 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_NXE);
1138 }
1139 else
1140 /* Turn on NXE if PAE has been enabled *and* the host has turned on NXE (we reuse the host EFER in the switcher) */
1141 /* Todo: this needs to be fixed properly!! */
1142 if ( CPUMGetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_PAE)
1143 && (pVM->hwaccm.s.vmx.hostEFER & MSR_K6_EFER_NXE))
1144 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_NXE);
1145
1146 LogRel((pVM->hwaccm.s.fAllow64BitGuests
1147 ? "HWACCM: 32-bit and 64-bit guests supported.\n"
1148 : "HWACCM: 32-bit guests supported.\n"));
1149#else
1150 LogRel(("HWACCM: 32-bit guests supported.\n"));
1151#endif
1152 LogRel(("HWACCM: VMX enabled!\n"));
1153 if (pVM->hwaccm.s.fNestedPaging)
1154 {
1155 LogRel(("HWACCM: Enabled nested paging\n"));
1156 LogRel(("HWACCM: EPT root page = %RHp\n", PGMGetHyperCR3(VMMGetCpu(pVM))));
1157 if (pVM->hwaccm.s.vmx.fUnrestrictedGuest)
1158 LogRel(("HWACCM: Unrestricted guest execution enabled!\n"));
1159
1160#ifdef DEBUG_sandervl
1161 /* Use large (2 MB) pages for our EPT PDEs where possible. */
1162 PGMSetLargePageUsage(pVM, true);
1163#endif
1164 }
1165 else
1166 Assert(!pVM->hwaccm.s.vmx.fUnrestrictedGuest);
1167
1168 if (pVM->hwaccm.s.vmx.fVPID)
1169 LogRel(("HWACCM: Enabled VPID\n"));
1170
1171 if ( pVM->hwaccm.s.fNestedPaging
1172 || pVM->hwaccm.s.vmx.fVPID)
1173 {
1174 LogRel(("HWACCM: enmFlushPage %d\n", pVM->hwaccm.s.vmx.enmFlushPage));
1175 LogRel(("HWACCM: enmFlushContext %d\n", pVM->hwaccm.s.vmx.enmFlushContext));
1176 }
1177
1178 /* TPR patching status logging. */
1179 if (pVM->hwaccm.s.fTRPPatchingAllowed)
1180 {
1181 if ( (pVM->hwaccm.s.vmx.msr.vmx_proc_ctls.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC_USE_SECONDARY_EXEC_CTRL)
1182 && (pVM->hwaccm.s.vmx.msr.vmx_proc_ctls2.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC2_VIRT_APIC))
1183 {
1184 pVM->hwaccm.s.fTRPPatchingAllowed = false; /* not necessary as we have a hardware solution. */
1185 LogRel(("HWACCM: TPR Patching not required (VMX_VMCS_CTRL_PROC_EXEC2_VIRT_APIC).\n"));
1186 }
1187 else
1188 {
1189 uint32_t u32Eax, u32Dummy;
1190
1191 /* TPR patching needs access to the MSR_K8_LSTAR msr. */
1192 ASMCpuId(0x80000000, &u32Eax, &u32Dummy, &u32Dummy, &u32Dummy);
1193 if ( u32Eax < 0x80000001
1194 || !(ASMCpuId_EDX(0x80000001) & X86_CPUID_AMD_FEATURE_EDX_LONG_MODE))
1195 {
1196 pVM->hwaccm.s.fTRPPatchingAllowed = false;
1197 LogRel(("HWACCM: TPR patching disabled (long mode not supported).\n"));
1198 }
1199 }
1200 }
1201 LogRel(("HWACCM: TPR Patching %s.\n", (pVM->hwaccm.s.fTRPPatchingAllowed) ? "enabled" : "disabled"));
1202 }
1203 else
1204 {
1205 LogRel(("HWACCM: VMX setup failed with rc=%Rrc!\n", rc));
1206 LogRel(("HWACCM: Last instruction error %x\n", pVM->aCpus[0].hwaccm.s.vmx.lasterror.ulInstrError));
1207 pVM->fHWACCMEnabled = false;
1208 }
1209 }
1210 }
1211 else
1212 if (pVM->hwaccm.s.svm.fSupported)
1213 {
1214 Log(("pVM->hwaccm.s.svm.fSupported = %d\n", pVM->hwaccm.s.svm.fSupported));
1215
1216 if (pVM->hwaccm.s.fInitialized == false)
1217 {
1218 /* Erratum 170 which requires a forced TLB flush for each world switch:
1219 * See http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/33610.pdf
1220 *
1221 * All BH-G1/2 and DH-G1/2 models include a fix:
1222 * Athlon X2: 0x6b 1/2
1223 * 0x68 1/2
1224 * Athlon 64: 0x7f 1
1225 * 0x6f 2
1226 * Sempron: 0x7f 1/2
1227 * 0x6f 2
1228 * 0x6c 2
1229 * 0x7c 2
1230 * Turion 64: 0x68 2
1231 *
1232 */
1233 uint32_t u32Dummy;
1234 uint32_t u32Version, u32Family, u32Model, u32Stepping, u32BaseFamily;
1235 ASMCpuId(1, &u32Version, &u32Dummy, &u32Dummy, &u32Dummy);
1236 u32BaseFamily= (u32Version >> 8) & 0xf;
1237 u32Family = u32BaseFamily + (u32BaseFamily == 0xf ? ((u32Version >> 20) & 0x7f) : 0);
1238 u32Model = ((u32Version >> 4) & 0xf);
1239 u32Model = u32Model | ((u32BaseFamily == 0xf ? (u32Version >> 16) & 0x0f : 0) << 4);
1240 u32Stepping = u32Version & 0xf;
1241 if ( u32Family == 0xf
1242 && !((u32Model == 0x68 || u32Model == 0x6b || u32Model == 0x7f) && u32Stepping >= 1)
1243 && !((u32Model == 0x6f || u32Model == 0x6c || u32Model == 0x7c) && u32Stepping >= 2))
1244 {
1245 LogRel(("HWACMM: AMD cpu with erratum 170 family %x model %x stepping %x\n", u32Family, u32Model, u32Stepping));
1246 }
1247
1248 LogRel(("HWACMM: cpuid 0x80000001.u32AMDFeatureECX = %RX32\n", pVM->hwaccm.s.cpuid.u32AMDFeatureECX));
1249 LogRel(("HWACMM: cpuid 0x80000001.u32AMDFeatureEDX = %RX32\n", pVM->hwaccm.s.cpuid.u32AMDFeatureEDX));
1250 LogRel(("HWACCM: AMD HWCR MSR = %RX64\n", pVM->hwaccm.s.svm.msrHWCR));
1251 LogRel(("HWACCM: AMD-V revision = %X\n", pVM->hwaccm.s.svm.u32Rev));
1252 LogRel(("HWACCM: AMD-V max ASID = %d\n", pVM->hwaccm.s.uMaxASID));
1253 LogRel(("HWACCM: AMD-V features = %X\n", pVM->hwaccm.s.svm.u32Features));
1254
1255 if (pVM->hwaccm.s.svm.u32Features & AMD_CPUID_SVM_FEATURE_EDX_NESTED_PAGING)
1256 LogRel(("HWACCM: AMD_CPUID_SVM_FEATURE_EDX_NESTED_PAGING\n"));
1257 if (pVM->hwaccm.s.svm.u32Features & AMD_CPUID_SVM_FEATURE_EDX_LBR_VIRT)
1258 LogRel(("HWACCM: AMD_CPUID_SVM_FEATURE_EDX_LBR_VIRT\n"));
1259 if (pVM->hwaccm.s.svm.u32Features & AMD_CPUID_SVM_FEATURE_EDX_SVM_LOCK)
1260 LogRel(("HWACCM: AMD_CPUID_SVM_FEATURE_EDX_SVM_LOCK\n"));
1261 if (pVM->hwaccm.s.svm.u32Features & AMD_CPUID_SVM_FEATURE_EDX_NRIP_SAVE)
1262 LogRel(("HWACCM: AMD_CPUID_SVM_FEATURE_EDX_NRIP_SAVE\n"));
1263 if (pVM->hwaccm.s.svm.u32Features & AMD_CPUID_SVM_FEATURE_EDX_SSE_3_5_DISABLE)
1264 LogRel(("HWACCM: AMD_CPUID_SVM_FEATURE_EDX_SSE_3_5_DISABLE\n"));
1265 if (pVM->hwaccm.s.svm.u32Features & AMD_CPUID_SVM_FEATURE_EDX_PAUSE_FILTER)
1266 LogRel(("HWACCM: AMD_CPUID_SVM_FEATURE_EDX_PAUSE_FILTER\n"));
1267
1268 /* Only try once. */
1269 pVM->hwaccm.s.fInitialized = true;
1270
1271 if (pVM->hwaccm.s.svm.u32Features & AMD_CPUID_SVM_FEATURE_EDX_NESTED_PAGING)
1272 pVM->hwaccm.s.fNestedPaging = pVM->hwaccm.s.fAllowNestedPaging;
1273
1274 rc = SUPR3CallVMMR0Ex(pVM->pVMR0, 0 /*idCpu*/, VMMR0_DO_HWACC_SETUP_VM, 0, NULL);
1275 AssertRC(rc);
1276 if (rc == VINF_SUCCESS)
1277 {
1278 pVM->fHWACCMEnabled = true;
1279 pVM->hwaccm.s.svm.fEnabled = true;
1280
1281 if (pVM->hwaccm.s.fNestedPaging)
1282 LogRel(("HWACCM: Enabled nested paging\n"));
1283
1284 hwaccmR3DisableRawMode(pVM);
1285 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_SEP);
1286 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_SYSCALL);
1287 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_RDTSCP);
1288#ifdef VBOX_ENABLE_64_BITS_GUESTS
1289 if (pVM->hwaccm.s.fAllow64BitGuests)
1290 {
1291 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_PAE);
1292 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_LONG_MODE);
1293 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_NXE);
1294 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_LAHF);
1295 }
1296 else
1297 /* Turn on NXE if PAE has been enabled. */
1298 if (CPUMGetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_PAE))
1299 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_NXE);
1300#endif
1301
1302 LogRel((pVM->hwaccm.s.fAllow64BitGuests
1303 ? "HWACCM: 32-bit and 64-bit guest supported.\n"
1304 : "HWACCM: 32-bit guest supported.\n"));
1305
1306 LogRel(("HWACCM: TPR Patching %s.\n", (pVM->hwaccm.s.fTRPPatchingAllowed) ? "enabled" : "disabled"));
1307 }
1308 else
1309 {
1310 pVM->fHWACCMEnabled = false;
1311 }
1312 }
1313 }
1314 if (pVM->fHWACCMEnabled)
1315 LogRel(("HWACCM: VT-x/AMD-V init method: %s\n", (pVM->hwaccm.s.fGlobalInit) ? "GLOBAL" : "LOCAL"));
1316 return VINF_SUCCESS;
1317}
1318
1319/**
1320 * Applies relocations to data and code managed by this
1321 * component. This function will be called at init and
1322 * whenever the VMM need to relocate it self inside the GC.
1323 *
1324 * @param pVM The VM.
1325 */
1326VMMR3DECL(void) HWACCMR3Relocate(PVM pVM)
1327{
1328 Log(("HWACCMR3Relocate to %RGv\n", MMHyperGetArea(pVM, 0)));
1329
1330 /* Fetch the current paging mode during the relocate callback during state loading. */
1331 if (VMR3GetState(pVM) == VMSTATE_LOADING)
1332 {
1333 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1334 {
1335 PVMCPU pVCpu = &pVM->aCpus[i];
1336
1337 pVCpu->hwaccm.s.enmShadowMode = PGMGetShadowMode(pVCpu);
1338 Assert(pVCpu->hwaccm.s.vmx.enmCurrGuestMode == PGMGetGuestMode(pVCpu));
1339 pVCpu->hwaccm.s.vmx.enmCurrGuestMode = PGMGetGuestMode(pVCpu);
1340 }
1341 }
1342#if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
1343 if (pVM->fHWACCMEnabled)
1344 {
1345 int rc;
1346
1347 switch(PGMGetHostMode(pVM))
1348 {
1349 case PGMMODE_32_BIT:
1350 pVM->hwaccm.s.pfnHost32ToGuest64R0 = VMMR3GetHostToGuestSwitcher(pVM, VMMSWITCHER_32_TO_AMD64);
1351 break;
1352
1353 case PGMMODE_PAE:
1354 case PGMMODE_PAE_NX:
1355 pVM->hwaccm.s.pfnHost32ToGuest64R0 = VMMR3GetHostToGuestSwitcher(pVM, VMMSWITCHER_PAE_TO_AMD64);
1356 break;
1357
1358 default:
1359 AssertFailed();
1360 break;
1361 }
1362 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "VMXGCStartVM64", &pVM->hwaccm.s.pfnVMXGCStartVM64);
1363 AssertReleaseMsgRC(rc, ("VMXGCStartVM64 -> rc=%Rrc\n", rc));
1364
1365 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "SVMGCVMRun64", &pVM->hwaccm.s.pfnSVMGCVMRun64);
1366 AssertReleaseMsgRC(rc, ("SVMGCVMRun64 -> rc=%Rrc\n", rc));
1367
1368 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "HWACCMSaveGuestFPU64", &pVM->hwaccm.s.pfnSaveGuestFPU64);
1369 AssertReleaseMsgRC(rc, ("HWACCMSetupFPU64 -> rc=%Rrc\n", rc));
1370
1371 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "HWACCMSaveGuestDebug64", &pVM->hwaccm.s.pfnSaveGuestDebug64);
1372 AssertReleaseMsgRC(rc, ("HWACCMSetupDebug64 -> rc=%Rrc\n", rc));
1373
1374# ifdef DEBUG
1375 rc = PDMR3LdrGetSymbolRC(pVM, NULL, "HWACCMTestSwitcher64", &pVM->hwaccm.s.pfnTest64);
1376 AssertReleaseMsgRC(rc, ("HWACCMTestSwitcher64 -> rc=%Rrc\n", rc));
1377# endif
1378 }
1379#endif
1380 return;
1381}
1382
1383/**
1384 * Checks hardware accelerated raw mode is allowed.
1385 *
1386 * @returns boolean
1387 * @param pVM The VM to operate on.
1388 */
1389VMMR3DECL(bool) HWACCMR3IsAllowed(PVM pVM)
1390{
1391 return pVM->hwaccm.s.fAllowed;
1392}
1393
1394/**
1395 * Notification callback which is called whenever there is a chance that a CR3
1396 * value might have changed.
1397 *
1398 * This is called by PGM.
1399 *
1400 * @param pVM The VM to operate on.
1401 * @param pVCpu The VMCPU to operate on.
1402 * @param enmShadowMode New shadow paging mode.
1403 * @param enmGuestMode New guest paging mode.
1404 */
1405VMMR3DECL(void) HWACCMR3PagingModeChanged(PVM pVM, PVMCPU pVCpu, PGMMODE enmShadowMode, PGMMODE enmGuestMode)
1406{
1407 /* Ignore page mode changes during state loading. */
1408 if (VMR3GetState(pVCpu->pVMR3) == VMSTATE_LOADING)
1409 return;
1410
1411 pVCpu->hwaccm.s.enmShadowMode = enmShadowMode;
1412
1413 if ( pVM->hwaccm.s.vmx.fEnabled
1414 && pVM->fHWACCMEnabled)
1415 {
1416 if ( pVCpu->hwaccm.s.vmx.enmLastSeenGuestMode == PGMMODE_REAL
1417 && enmGuestMode >= PGMMODE_PROTECTED)
1418 {
1419 PCPUMCTX pCtx;
1420
1421 pCtx = CPUMQueryGuestCtxPtr(pVCpu);
1422
1423 /* After a real mode switch to protected mode we must force
1424 * CPL to 0. Our real mode emulation had to set it to 3.
1425 */
1426 pCtx->ssHid.Attr.n.u2Dpl = 0;
1427 }
1428 }
1429
1430 if (pVCpu->hwaccm.s.vmx.enmCurrGuestMode != enmGuestMode)
1431 {
1432 /* Keep track of paging mode changes. */
1433 pVCpu->hwaccm.s.vmx.enmPrevGuestMode = pVCpu->hwaccm.s.vmx.enmCurrGuestMode;
1434 pVCpu->hwaccm.s.vmx.enmCurrGuestMode = enmGuestMode;
1435
1436 /* Did we miss a change, because all code was executed in the recompiler? */
1437 if (pVCpu->hwaccm.s.vmx.enmLastSeenGuestMode == enmGuestMode)
1438 {
1439 Log(("HWACCMR3PagingModeChanged missed %s->%s transition (prev %s)\n", PGMGetModeName(pVCpu->hwaccm.s.vmx.enmPrevGuestMode), PGMGetModeName(pVCpu->hwaccm.s.vmx.enmCurrGuestMode), PGMGetModeName(pVCpu->hwaccm.s.vmx.enmLastSeenGuestMode)));
1440 pVCpu->hwaccm.s.vmx.enmLastSeenGuestMode = pVCpu->hwaccm.s.vmx.enmPrevGuestMode;
1441 }
1442 }
1443
1444 /* Reset the contents of the read cache. */
1445 PVMCSCACHE pCache = &pVCpu->hwaccm.s.vmx.VMCSCache;
1446 for (unsigned j=0;j<pCache->Read.cValidEntries;j++)
1447 pCache->Read.aFieldVal[j] = 0;
1448}
1449
1450/**
1451 * Terminates the HWACCM.
1452 *
1453 * Termination means cleaning up and freeing all resources,
1454 * the VM it self is at this point powered off or suspended.
1455 *
1456 * @returns VBox status code.
1457 * @param pVM The VM to operate on.
1458 */
1459VMMR3DECL(int) HWACCMR3Term(PVM pVM)
1460{
1461 if (pVM->hwaccm.s.vmx.pRealModeTSS)
1462 {
1463 PDMR3VMMDevHeapFree(pVM, pVM->hwaccm.s.vmx.pRealModeTSS);
1464 pVM->hwaccm.s.vmx.pRealModeTSS = 0;
1465 }
1466 HWACCMR3TermCPU(pVM);
1467 return 0;
1468}
1469
1470/**
1471 * Terminates the per-VCPU HWACCM.
1472 *
1473 * Termination means cleaning up and freeing all resources,
1474 * the VM it self is at this point powered off or suspended.
1475 *
1476 * @returns VBox status code.
1477 * @param pVM The VM to operate on.
1478 */
1479VMMR3DECL(int) HWACCMR3TermCPU(PVM pVM)
1480{
1481 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1482 {
1483 PVMCPU pVCpu = &pVM->aCpus[i];
1484
1485#ifdef VBOX_WITH_STATISTICS
1486 if (pVCpu->hwaccm.s.paStatExitReason)
1487 {
1488 MMHyperFree(pVM, pVCpu->hwaccm.s.paStatExitReason);
1489 pVCpu->hwaccm.s.paStatExitReason = NULL;
1490 pVCpu->hwaccm.s.paStatExitReasonR0 = NIL_RTR0PTR;
1491 }
1492 if (pVCpu->hwaccm.s.paStatInjectedIrqs)
1493 {
1494 MMHyperFree(pVM, pVCpu->hwaccm.s.paStatInjectedIrqs);
1495 pVCpu->hwaccm.s.paStatInjectedIrqs = NULL;
1496 pVCpu->hwaccm.s.paStatInjectedIrqsR0 = NIL_RTR0PTR;
1497 }
1498#endif
1499
1500#ifdef VBOX_WITH_CRASHDUMP_MAGIC
1501 memset(pVCpu->hwaccm.s.vmx.VMCSCache.aMagic, 0, sizeof(pVCpu->hwaccm.s.vmx.VMCSCache.aMagic));
1502 pVCpu->hwaccm.s.vmx.VMCSCache.uMagic = 0;
1503 pVCpu->hwaccm.s.vmx.VMCSCache.uPos = 0xffffffff;
1504#endif
1505 }
1506 return 0;
1507}
1508
1509/**
1510 * Resets a virtual CPU.
1511 *
1512 * Used by HWACCMR3Reset and CPU hot plugging.
1513 *
1514 * @param pVCpu The CPU to reset.
1515 */
1516VMMR3DECL(void) HWACCMR3ResetCpu(PVMCPU pVCpu)
1517{
1518 /* On first entry we'll sync everything. */
1519 pVCpu->hwaccm.s.fContextUseFlags = HWACCM_CHANGED_ALL;
1520
1521 pVCpu->hwaccm.s.vmx.cr0_mask = 0;
1522 pVCpu->hwaccm.s.vmx.cr4_mask = 0;
1523
1524 pVCpu->hwaccm.s.fActive = false;
1525 pVCpu->hwaccm.s.Event.fPending = false;
1526
1527 /* Reset state information for real-mode emulation in VT-x. */
1528 pVCpu->hwaccm.s.vmx.enmLastSeenGuestMode = PGMMODE_REAL;
1529 pVCpu->hwaccm.s.vmx.enmPrevGuestMode = PGMMODE_REAL;
1530 pVCpu->hwaccm.s.vmx.enmCurrGuestMode = PGMMODE_REAL;
1531
1532 /* Reset the contents of the read cache. */
1533 PVMCSCACHE pCache = &pVCpu->hwaccm.s.vmx.VMCSCache;
1534 for (unsigned j=0;j<pCache->Read.cValidEntries;j++)
1535 pCache->Read.aFieldVal[j] = 0;
1536
1537#ifdef VBOX_WITH_CRASHDUMP_MAGIC
1538 /* Magic marker for searching in crash dumps. */
1539 strcpy((char *)pCache->aMagic, "VMCSCACHE Magic");
1540 pCache->uMagic = UINT64_C(0xDEADBEEFDEADBEEF);
1541#endif
1542}
1543
1544/**
1545 * The VM is being reset.
1546 *
1547 * For the HWACCM component this means that any GDT/LDT/TSS monitors
1548 * needs to be removed.
1549 *
1550 * @param pVM VM handle.
1551 */
1552VMMR3DECL(void) HWACCMR3Reset(PVM pVM)
1553{
1554 LogFlow(("HWACCMR3Reset:\n"));
1555
1556 if (pVM->fHWACCMEnabled)
1557 hwaccmR3DisableRawMode(pVM);
1558
1559 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1560 {
1561 PVMCPU pVCpu = &pVM->aCpus[i];
1562
1563 HWACCMR3ResetCpu(pVCpu);
1564 }
1565
1566 /* Clear all patch information. */
1567 pVM->hwaccm.s.pGuestPatchMem = 0;
1568 pVM->hwaccm.s.pFreeGuestPatchMem = 0;
1569 pVM->hwaccm.s.cbGuestPatchMem = 0;
1570 pVM->hwaccm.s.cPatches = 0;
1571 pVM->hwaccm.s.PatchTree = 0;
1572 pVM->hwaccm.s.fTPRPatchingActive = false;
1573 ASMMemZero32(pVM->hwaccm.s.aPatches, sizeof(pVM->hwaccm.s.aPatches));
1574}
1575
1576/**
1577 * Callback to patch a TPR instruction (vmmcall or mov cr8)
1578 *
1579 * @returns VBox strict status code.
1580 * @param pVM The VM handle.
1581 * @param pVCpu The VMCPU for the EMT we're being called on.
1582 * @param pvUser Unused
1583 *
1584 */
1585DECLCALLBACK(VBOXSTRICTRC) hwaccmR3RemovePatches(PVM pVM, PVMCPU pVCpu, void *pvUser)
1586{
1587 VMCPUID idCpu = (VMCPUID)(uintptr_t)pvUser;
1588
1589 /* Only execute the handler on the VCPU the original patch request was issued. */
1590 if (pVCpu->idCpu != idCpu)
1591 return VINF_SUCCESS;
1592
1593 Log(("hwaccmR3RemovePatches\n"));
1594 for (unsigned i = 0; i < pVM->hwaccm.s.cPatches; i++)
1595 {
1596 uint8_t szInstr[15];
1597 PHWACCMTPRPATCH pPatch = &pVM->hwaccm.s.aPatches[i];
1598 RTGCPTR pInstrGC = (RTGCPTR)pPatch->Core.Key;
1599 int rc;
1600
1601#ifdef LOG_ENABLED
1602 char szOutput[256];
1603
1604 rc = DBGFR3DisasInstrEx(pVM, pVCpu->idCpu, CPUMGetGuestCS(pVCpu), pInstrGC, 0, szOutput, sizeof(szOutput), 0);
1605 if (RT_SUCCESS(rc))
1606 Log(("Patched instr: %s\n", szOutput));
1607#endif
1608
1609 /* Check if the instruction is still the same. */
1610 rc = PGMPhysSimpleReadGCPtr(pVCpu, szInstr, pInstrGC, pPatch->cbNewOp);
1611 if (rc != VINF_SUCCESS)
1612 {
1613 Log(("Patched code removed? (rc=%Rrc0\n", rc));
1614 continue; /* swapped out or otherwise removed; skip it. */
1615 }
1616
1617 if (memcmp(szInstr, pPatch->aNewOpcode, pPatch->cbNewOp))
1618 {
1619 Log(("Patched instruction was changed! (rc=%Rrc0\n", rc));
1620 continue; /* skip it. */
1621 }
1622
1623 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pInstrGC, pPatch->aOpcode, pPatch->cbOp);
1624 AssertRC(rc);
1625
1626#ifdef LOG_ENABLED
1627 rc = DBGFR3DisasInstrEx(pVM, pVCpu->idCpu, CPUMGetGuestCS(pVCpu), pInstrGC, 0, szOutput, sizeof(szOutput), 0);
1628 if (RT_SUCCESS(rc))
1629 Log(("Original instr: %s\n", szOutput));
1630#endif
1631 }
1632 pVM->hwaccm.s.cPatches = 0;
1633 pVM->hwaccm.s.PatchTree = 0;
1634 pVM->hwaccm.s.pFreeGuestPatchMem = pVM->hwaccm.s.pGuestPatchMem;
1635 pVM->hwaccm.s.fTPRPatchingActive = false;
1636 return VINF_SUCCESS;
1637}
1638
1639/**
1640 * Enable patching in a VT-x/AMD-V guest
1641 *
1642 * @returns VBox status code.
1643 * @param pVM The VM to operate on.
1644 * @param idCpu VCPU to execute hwaccmR3RemovePatches on
1645 * @param pPatchMem Patch memory range
1646 * @param cbPatchMem Size of the memory range
1647 */
1648int hwaccmR3EnablePatching(PVM pVM, VMCPUID idCpu, RTRCPTR pPatchMem, unsigned cbPatchMem)
1649{
1650 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE, hwaccmR3RemovePatches, (void *)idCpu);
1651 AssertRC(rc);
1652
1653 pVM->hwaccm.s.pGuestPatchMem = pPatchMem;
1654 pVM->hwaccm.s.pFreeGuestPatchMem = pPatchMem;
1655 pVM->hwaccm.s.cbGuestPatchMem = cbPatchMem;
1656 return VINF_SUCCESS;
1657}
1658
1659/**
1660 * Enable patching in a VT-x/AMD-V guest
1661 *
1662 * @returns VBox status code.
1663 * @param pVM The VM to operate on.
1664 * @param pPatchMem Patch memory range
1665 * @param cbPatchMem Size of the memory range
1666 */
1667VMMR3DECL(int) HWACMMR3EnablePatching(PVM pVM, RTGCPTR pPatchMem, unsigned cbPatchMem)
1668{
1669 Log(("HWACMMR3EnablePatching %RGv size %x\n", pPatchMem, cbPatchMem));
1670 if (pVM->cCpus > 1)
1671 {
1672 /* We own the IOM lock here and could cause a deadlock by waiting for a VCPU that is blocking on the IOM lock. */
1673 int rc = VMR3ReqCallNoWaitU(pVM->pUVM, VMCPUID_ANY_QUEUE,
1674 (PFNRT)hwaccmR3EnablePatching, 4, pVM, VMMGetCpuId(pVM), (RTRCPTR)pPatchMem, cbPatchMem);
1675 AssertRC(rc);
1676 return rc;
1677 }
1678 return hwaccmR3EnablePatching(pVM, VMMGetCpuId(pVM), (RTRCPTR)pPatchMem, cbPatchMem);
1679}
1680
1681/**
1682 * Disable patching in a VT-x/AMD-V guest
1683 *
1684 * @returns VBox status code.
1685 * @param pVM The VM to operate on.
1686 * @param pPatchMem Patch memory range
1687 * @param cbPatchMem Size of the memory range
1688 */
1689VMMR3DECL(int) HWACMMR3DisablePatching(PVM pVM, RTGCPTR pPatchMem, unsigned cbPatchMem)
1690{
1691 Log(("HWACMMR3DisablePatching %RGv size %x\n", pPatchMem, cbPatchMem));
1692
1693 Assert(pVM->hwaccm.s.pGuestPatchMem == pPatchMem);
1694 Assert(pVM->hwaccm.s.cbGuestPatchMem == cbPatchMem);
1695
1696 /* @todo Potential deadlock when other VCPUs are waiting on the IOM lock (we own it)!! */
1697 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE, hwaccmR3RemovePatches, (void *)VMMGetCpuId(pVM));
1698 AssertRC(rc);
1699
1700 pVM->hwaccm.s.pGuestPatchMem = 0;
1701 pVM->hwaccm.s.pFreeGuestPatchMem = 0;
1702 pVM->hwaccm.s.cbGuestPatchMem = 0;
1703 pVM->hwaccm.s.fTPRPatchingActive = false;
1704 return VINF_SUCCESS;
1705}
1706
1707
1708/**
1709 * Callback to patch a TPR instruction (vmmcall or mov cr8)
1710 *
1711 * @returns VBox strict status code.
1712 * @param pVM The VM handle.
1713 * @param pVCpu The VMCPU for the EMT we're being called on.
1714 * @param pvUser User specified CPU context
1715 *
1716 */
1717DECLCALLBACK(VBOXSTRICTRC) hwaccmR3ReplaceTprInstr(PVM pVM, PVMCPU pVCpu, void *pvUser)
1718{
1719 VMCPUID idCpu = (VMCPUID)(uintptr_t)pvUser;
1720 PCPUMCTX pCtx = CPUMQueryGuestCtxPtr(pVCpu);
1721 PDISCPUSTATE pDis = &pVCpu->hwaccm.s.DisState;
1722 unsigned cbOp;
1723
1724 /* Only execute the handler on the VCPU the original patch request was issued. (the other CPU(s) might not yet have switched to protected mode) */
1725 if (pVCpu->idCpu != idCpu)
1726 return VINF_SUCCESS;
1727
1728 Log(("hwaccmR3ReplaceTprInstr: %RGv\n", pCtx->rip));
1729
1730 /* Two or more VCPUs were racing to patch this instruction. */
1731 PHWACCMTPRPATCH pPatch = (PHWACCMTPRPATCH)RTAvloU32Get(&pVM->hwaccm.s.PatchTree, (AVLOU32KEY)pCtx->eip);
1732 if (pPatch)
1733 return VINF_SUCCESS;
1734
1735 Assert(pVM->hwaccm.s.cPatches < RT_ELEMENTS(pVM->hwaccm.s.aPatches));
1736
1737 int rc = EMInterpretDisasOne(pVM, pVCpu, CPUMCTX2CORE(pCtx), pDis, &cbOp);
1738 AssertRC(rc);
1739 if ( rc == VINF_SUCCESS
1740 && pDis->pCurInstr->opcode == OP_MOV
1741 && cbOp >= 3)
1742 {
1743 uint8_t aVMMCall[3] = { 0xf, 0x1, 0xd9};
1744 uint32_t idx = pVM->hwaccm.s.cPatches;
1745
1746 pPatch = &pVM->hwaccm.s.aPatches[idx];
1747
1748 rc = PGMPhysSimpleReadGCPtr(pVCpu, pPatch->aOpcode, pCtx->rip, cbOp);
1749 AssertRC(rc);
1750
1751 pPatch->cbOp = cbOp;
1752
1753 if (pDis->param1.flags == USE_DISPLACEMENT32)
1754 {
1755 /* write. */
1756 if (pDis->param2.flags == USE_REG_GEN32)
1757 {
1758 pPatch->enmType = HWACCMTPRINSTR_WRITE_REG;
1759 pPatch->uSrcOperand = pDis->param2.base.reg_gen;
1760 }
1761 else
1762 {
1763 Assert(pDis->param2.flags == USE_IMMEDIATE32);
1764 pPatch->enmType = HWACCMTPRINSTR_WRITE_IMM;
1765 pPatch->uSrcOperand = pDis->param2.parval;
1766 }
1767 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pCtx->rip, aVMMCall, sizeof(aVMMCall));
1768 AssertRC(rc);
1769
1770 memcpy(pPatch->aNewOpcode, aVMMCall, sizeof(aVMMCall));
1771 pPatch->cbNewOp = sizeof(aVMMCall);
1772 }
1773 else
1774 {
1775 RTGCPTR oldrip = pCtx->rip;
1776 uint32_t oldcbOp = cbOp;
1777 uint32_t uMmioReg = pDis->param1.base.reg_gen;
1778
1779 /* read */
1780 Assert(pDis->param1.flags == USE_REG_GEN32);
1781
1782 /* Found:
1783 * mov eax, dword [fffe0080] (5 bytes)
1784 * Check if next instruction is:
1785 * shr eax, 4
1786 */
1787 pCtx->rip += cbOp;
1788 rc = EMInterpretDisasOne(pVM, pVCpu, CPUMCTX2CORE(pCtx), pDis, &cbOp);
1789 pCtx->rip = oldrip;
1790 if ( rc == VINF_SUCCESS
1791 && pDis->pCurInstr->opcode == OP_SHR
1792 && pDis->param1.flags == USE_REG_GEN32
1793 && pDis->param1.base.reg_gen == uMmioReg
1794 && pDis->param2.flags == USE_IMMEDIATE8
1795 && pDis->param2.parval == 4
1796 && oldcbOp + cbOp < sizeof(pVM->hwaccm.s.aPatches[idx].aOpcode))
1797 {
1798 uint8_t szInstr[15];
1799
1800 /* Replacing two instructions now. */
1801 rc = PGMPhysSimpleReadGCPtr(pVCpu, &pPatch->aOpcode, pCtx->rip, oldcbOp + cbOp);
1802 AssertRC(rc);
1803
1804 pPatch->cbOp = oldcbOp + cbOp;
1805
1806 /* 0xF0, 0x0F, 0x20, 0xC0 = mov eax, cr8 */
1807 szInstr[0] = 0xF0;
1808 szInstr[1] = 0x0F;
1809 szInstr[2] = 0x20;
1810 szInstr[3] = 0xC0 | pDis->param1.base.reg_gen;
1811 for (unsigned i = 4; i < pPatch->cbOp; i++)
1812 szInstr[i] = 0x90; /* nop */
1813
1814 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pCtx->rip, szInstr, pPatch->cbOp);
1815 AssertRC(rc);
1816
1817 memcpy(pPatch->aNewOpcode, szInstr, pPatch->cbOp);
1818 pPatch->cbNewOp = pPatch->cbOp;
1819
1820 Log(("Acceptable read/shr candidate!\n"));
1821 pPatch->enmType = HWACCMTPRINSTR_READ_SHR4;
1822 }
1823 else
1824 {
1825 pPatch->enmType = HWACCMTPRINSTR_READ;
1826 pPatch->uDstOperand = pDis->param1.base.reg_gen;
1827
1828 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pCtx->rip, aVMMCall, sizeof(aVMMCall));
1829 AssertRC(rc);
1830
1831 memcpy(pPatch->aNewOpcode, aVMMCall, sizeof(aVMMCall));
1832 pPatch->cbNewOp = sizeof(aVMMCall);
1833 }
1834 }
1835
1836 pPatch->Core.Key = pCtx->eip;
1837 rc = RTAvloU32Insert(&pVM->hwaccm.s.PatchTree, &pPatch->Core);
1838 AssertRC(rc);
1839
1840 pVM->hwaccm.s.cPatches++;
1841 STAM_COUNTER_INC(&pVM->hwaccm.s.StatTPRReplaceSuccess);
1842 return VINF_SUCCESS;
1843 }
1844
1845 /* Save invalid patch, so we will not try again. */
1846 uint32_t idx = pVM->hwaccm.s.cPatches;
1847
1848#ifdef LOG_ENABLED
1849 char szOutput[256];
1850 rc = DBGFR3DisasInstrEx(pVM, pVCpu->idCpu, pCtx->cs, pCtx->rip, 0, szOutput, sizeof(szOutput), 0);
1851 if (RT_SUCCESS(rc))
1852 Log(("Failed to patch instr: %s\n", szOutput));
1853#endif
1854
1855 pPatch = &pVM->hwaccm.s.aPatches[idx];
1856 pPatch->Core.Key = pCtx->eip;
1857 pPatch->enmType = HWACCMTPRINSTR_INVALID;
1858 rc = RTAvloU32Insert(&pVM->hwaccm.s.PatchTree, &pPatch->Core);
1859 AssertRC(rc);
1860 pVM->hwaccm.s.cPatches++;
1861 STAM_COUNTER_INC(&pVM->hwaccm.s.StatTPRReplaceFailure);
1862 return VINF_SUCCESS;
1863}
1864
1865/**
1866 * Callback to patch a TPR instruction (jump to generated code)
1867 *
1868 * @returns VBox strict status code.
1869 * @param pVM The VM handle.
1870 * @param pVCpu The VMCPU for the EMT we're being called on.
1871 * @param pvUser User specified CPU context
1872 *
1873 */
1874DECLCALLBACK(VBOXSTRICTRC) hwaccmR3PatchTprInstr(PVM pVM, PVMCPU pVCpu, void *pvUser)
1875{
1876 VMCPUID idCpu = (VMCPUID)(uintptr_t)pvUser;
1877 PCPUMCTX pCtx = CPUMQueryGuestCtxPtr(pVCpu);
1878 PDISCPUSTATE pDis = &pVCpu->hwaccm.s.DisState;
1879 unsigned cbOp;
1880 int rc;
1881#ifdef LOG_ENABLED
1882 RTGCPTR pInstr;
1883 char szOutput[256];
1884#endif
1885
1886 /* Only execute the handler on the VCPU the original patch request was issued. (the other CPU(s) might not yet have switched to protected mode) */
1887 if (pVCpu->idCpu != idCpu)
1888 return VINF_SUCCESS;
1889
1890 Assert(pVM->hwaccm.s.cPatches < RT_ELEMENTS(pVM->hwaccm.s.aPatches));
1891
1892 /* Two or more VCPUs were racing to patch this instruction. */
1893 PHWACCMTPRPATCH pPatch = (PHWACCMTPRPATCH)RTAvloU32Get(&pVM->hwaccm.s.PatchTree, (AVLOU32KEY)pCtx->eip);
1894 if (pPatch)
1895 {
1896 Log(("hwaccmR3PatchTprInstr: already patched %RGv\n", pCtx->rip));
1897 return VINF_SUCCESS;
1898 }
1899
1900 Log(("hwaccmR3PatchTprInstr %RGv\n", pCtx->rip));
1901
1902 rc = EMInterpretDisasOne(pVM, pVCpu, CPUMCTX2CORE(pCtx), pDis, &cbOp);
1903 AssertRC(rc);
1904 if ( rc == VINF_SUCCESS
1905 && pDis->pCurInstr->opcode == OP_MOV
1906 && cbOp >= 5)
1907 {
1908 uint32_t idx = pVM->hwaccm.s.cPatches;
1909 uint8_t aPatch[64];
1910 uint32_t off = 0;
1911
1912 pPatch = &pVM->hwaccm.s.aPatches[idx];
1913
1914#ifdef LOG_ENABLED
1915 rc = DBGFR3DisasInstrEx(pVM, pVCpu->idCpu, pCtx->cs, pCtx->rip, 0, szOutput, sizeof(szOutput), 0);
1916 if (RT_SUCCESS(rc))
1917 Log(("Original instr: %s\n", szOutput));
1918#endif
1919
1920 rc = PGMPhysSimpleReadGCPtr(pVCpu, pPatch->aOpcode, pCtx->rip, cbOp);
1921 AssertRC(rc);
1922
1923 pPatch->cbOp = cbOp;
1924 pPatch->enmType = HWACCMTPRINSTR_JUMP_REPLACEMENT;
1925
1926 if (pDis->param1.flags == USE_DISPLACEMENT32)
1927 {
1928 /*
1929 * TPR write:
1930 *
1931 * push ECX [51]
1932 * push EDX [52]
1933 * push EAX [50]
1934 * xor EDX,EDX [31 D2]
1935 * mov EAX,EAX [89 C0]
1936 * or
1937 * mov EAX,0000000CCh [B8 CC 00 00 00]
1938 * mov ECX,0C0000082h [B9 82 00 00 C0]
1939 * wrmsr [0F 30]
1940 * pop EAX [58]
1941 * pop EDX [5A]
1942 * pop ECX [59]
1943 * jmp return_address [E9 return_address]
1944 *
1945 */
1946 bool fUsesEax = (pDis->param2.flags == USE_REG_GEN32 && pDis->param2.base.reg_gen == USE_REG_EAX);
1947
1948 aPatch[off++] = 0x51; /* push ecx */
1949 aPatch[off++] = 0x52; /* push edx */
1950 if (!fUsesEax)
1951 aPatch[off++] = 0x50; /* push eax */
1952 aPatch[off++] = 0x31; /* xor edx, edx */
1953 aPatch[off++] = 0xD2;
1954 if (pDis->param2.flags == USE_REG_GEN32)
1955 {
1956 if (!fUsesEax)
1957 {
1958 aPatch[off++] = 0x89; /* mov eax, src_reg */
1959 aPatch[off++] = MAKE_MODRM(3, pDis->param2.base.reg_gen, USE_REG_EAX);
1960 }
1961 }
1962 else
1963 {
1964 Assert(pDis->param2.flags == USE_IMMEDIATE32);
1965 aPatch[off++] = 0xB8; /* mov eax, immediate */
1966 *(uint32_t *)&aPatch[off] = pDis->param2.parval;
1967 off += sizeof(uint32_t);
1968 }
1969 aPatch[off++] = 0xB9; /* mov ecx, 0xc0000082 */
1970 *(uint32_t *)&aPatch[off] = MSR_K8_LSTAR;
1971 off += sizeof(uint32_t);
1972
1973 aPatch[off++] = 0x0F; /* wrmsr */
1974 aPatch[off++] = 0x30;
1975 if (!fUsesEax)
1976 aPatch[off++] = 0x58; /* pop eax */
1977 aPatch[off++] = 0x5A; /* pop edx */
1978 aPatch[off++] = 0x59; /* pop ecx */
1979 }
1980 else
1981 {
1982 /*
1983 * TPR read:
1984 *
1985 * push ECX [51]
1986 * push EDX [52]
1987 * push EAX [50]
1988 * mov ECX,0C0000082h [B9 82 00 00 C0]
1989 * rdmsr [0F 32]
1990 * mov EAX,EAX [89 C0]
1991 * pop EAX [58]
1992 * pop EDX [5A]
1993 * pop ECX [59]
1994 * jmp return_address [E9 return_address]
1995 *
1996 */
1997 Assert(pDis->param1.flags == USE_REG_GEN32);
1998
1999 if (pDis->param1.base.reg_gen != USE_REG_ECX)
2000 aPatch[off++] = 0x51; /* push ecx */
2001 if (pDis->param1.base.reg_gen != USE_REG_EDX)
2002 aPatch[off++] = 0x52; /* push edx */
2003 if (pDis->param1.base.reg_gen != USE_REG_EAX)
2004 aPatch[off++] = 0x50; /* push eax */
2005
2006 aPatch[off++] = 0x31; /* xor edx, edx */
2007 aPatch[off++] = 0xD2;
2008
2009 aPatch[off++] = 0xB9; /* mov ecx, 0xc0000082 */
2010 *(uint32_t *)&aPatch[off] = MSR_K8_LSTAR;
2011 off += sizeof(uint32_t);
2012
2013 aPatch[off++] = 0x0F; /* rdmsr */
2014 aPatch[off++] = 0x32;
2015
2016 if (pDis->param1.base.reg_gen != USE_REG_EAX)
2017 {
2018 aPatch[off++] = 0x89; /* mov dst_reg, eax */
2019 aPatch[off++] = MAKE_MODRM(3, USE_REG_EAX, pDis->param1.base.reg_gen);
2020 }
2021
2022 if (pDis->param1.base.reg_gen != USE_REG_EAX)
2023 aPatch[off++] = 0x58; /* pop eax */
2024 if (pDis->param1.base.reg_gen != USE_REG_EDX)
2025 aPatch[off++] = 0x5A; /* pop edx */
2026 if (pDis->param1.base.reg_gen != USE_REG_ECX)
2027 aPatch[off++] = 0x59; /* pop ecx */
2028 }
2029 aPatch[off++] = 0xE9; /* jmp return_address */
2030 *(RTRCUINTPTR *)&aPatch[off] = ((RTRCUINTPTR)pCtx->eip + cbOp) - ((RTRCUINTPTR)pVM->hwaccm.s.pFreeGuestPatchMem + off + 4);
2031 off += sizeof(RTRCUINTPTR);
2032
2033 if (pVM->hwaccm.s.pFreeGuestPatchMem + off <= pVM->hwaccm.s.pGuestPatchMem + pVM->hwaccm.s.cbGuestPatchMem)
2034 {
2035 /* Write new code to the patch buffer. */
2036 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pVM->hwaccm.s.pFreeGuestPatchMem, aPatch, off);
2037 AssertRC(rc);
2038
2039#ifdef LOG_ENABLED
2040 pInstr = pVM->hwaccm.s.pFreeGuestPatchMem;
2041 while (true)
2042 {
2043 uint32_t cb;
2044
2045 rc = DBGFR3DisasInstrEx(pVM, pVCpu->idCpu, pCtx->cs, pInstr, 0, szOutput, sizeof(szOutput), &cb);
2046 if (RT_SUCCESS(rc))
2047 Log(("Patch instr %s\n", szOutput));
2048
2049 pInstr += cb;
2050
2051 if (pInstr >= pVM->hwaccm.s.pFreeGuestPatchMem + off)
2052 break;
2053 }
2054#endif
2055
2056 pPatch->aNewOpcode[0] = 0xE9;
2057 *(RTRCUINTPTR *)&pPatch->aNewOpcode[1] = ((RTRCUINTPTR)pVM->hwaccm.s.pFreeGuestPatchMem) - ((RTRCUINTPTR)pCtx->eip + 5);
2058
2059 /* Overwrite the TPR instruction with a jump. */
2060 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pCtx->eip, pPatch->aNewOpcode, 5);
2061 AssertRC(rc);
2062
2063#ifdef LOG_ENABLED
2064 rc = DBGFR3DisasInstrEx(pVM, pVCpu->idCpu, pCtx->cs, pCtx->rip, 0, szOutput, sizeof(szOutput), 0);
2065 if (RT_SUCCESS(rc))
2066 Log(("Jump: %s\n", szOutput));
2067#endif
2068 pVM->hwaccm.s.pFreeGuestPatchMem += off;
2069 pPatch->cbNewOp = 5;
2070
2071 pPatch->Core.Key = pCtx->eip;
2072 rc = RTAvloU32Insert(&pVM->hwaccm.s.PatchTree, &pPatch->Core);
2073 AssertRC(rc);
2074
2075 pVM->hwaccm.s.cPatches++;
2076 pVM->hwaccm.s.fTPRPatchingActive = true;
2077 STAM_COUNTER_INC(&pVM->hwaccm.s.StatTPRPatchSuccess);
2078 return VINF_SUCCESS;
2079 }
2080 else
2081 Log(("Ran out of space in our patch buffer!\n"));
2082 }
2083
2084 /* Save invalid patch, so we will not try again. */
2085 uint32_t idx = pVM->hwaccm.s.cPatches;
2086
2087#ifdef LOG_ENABLED
2088 rc = DBGFR3DisasInstrEx(pVM, pVCpu->idCpu, pCtx->cs, pCtx->rip, 0, szOutput, sizeof(szOutput), 0);
2089 if (RT_SUCCESS(rc))
2090 Log(("Failed to patch instr: %s\n", szOutput));
2091#endif
2092
2093 pPatch = &pVM->hwaccm.s.aPatches[idx];
2094 pPatch->Core.Key = pCtx->eip;
2095 pPatch->enmType = HWACCMTPRINSTR_INVALID;
2096 rc = RTAvloU32Insert(&pVM->hwaccm.s.PatchTree, &pPatch->Core);
2097 AssertRC(rc);
2098 pVM->hwaccm.s.cPatches++;
2099 STAM_COUNTER_INC(&pVM->hwaccm.s.StatTPRPatchFailure);
2100 return VINF_SUCCESS;
2101}
2102
2103/**
2104 * Attempt to patch TPR mmio instructions
2105 *
2106 * @returns VBox status code.
2107 * @param pVM The VM to operate on.
2108 * @param pVCpu The VM CPU to operate on.
2109 * @param pCtx CPU context
2110 */
2111VMMR3DECL(int) HWACCMR3PatchTprInstr(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
2112{
2113 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE, (pVM->hwaccm.s.pGuestPatchMem) ? hwaccmR3PatchTprInstr : hwaccmR3ReplaceTprInstr, (void *)pVCpu->idCpu);
2114 AssertRC(rc);
2115 return rc;
2116}
2117
2118/**
2119 * Force execution of the current IO code in the recompiler
2120 *
2121 * @returns VBox status code.
2122 * @param pVM The VM to operate on.
2123 * @param pCtx Partial VM execution context
2124 */
2125VMMR3DECL(int) HWACCMR3EmulateIoBlock(PVM pVM, PCPUMCTX pCtx)
2126{
2127 PVMCPU pVCpu = VMMGetCpu(pVM);
2128
2129 Assert(pVM->fHWACCMEnabled);
2130 Log(("HWACCMR3EmulateIoBlock\n"));
2131
2132 /* This is primarily intended to speed up Grub, so we don't care about paged protected mode. */
2133 if (HWACCMCanEmulateIoBlockEx(pCtx))
2134 {
2135 Log(("HWACCMR3EmulateIoBlock -> enabled\n"));
2136 pVCpu->hwaccm.s.EmulateIoBlock.fEnabled = true;
2137 pVCpu->hwaccm.s.EmulateIoBlock.GCPtrFunctionEip = pCtx->rip;
2138 pVCpu->hwaccm.s.EmulateIoBlock.cr0 = pCtx->cr0;
2139 return VINF_EM_RESCHEDULE_REM;
2140 }
2141 return VINF_SUCCESS;
2142}
2143
2144/**
2145 * Checks if we can currently use hardware accelerated raw mode.
2146 *
2147 * @returns boolean
2148 * @param pVM The VM to operate on.
2149 * @param pCtx Partial VM execution context
2150 */
2151VMMR3DECL(bool) HWACCMR3CanExecuteGuest(PVM pVM, PCPUMCTX pCtx)
2152{
2153 PVMCPU pVCpu = VMMGetCpu(pVM);
2154
2155 Assert(pVM->fHWACCMEnabled);
2156
2157 /* If we're still executing the IO code, then return false. */
2158 if ( RT_UNLIKELY(pVCpu->hwaccm.s.EmulateIoBlock.fEnabled)
2159 && pCtx->rip < pVCpu->hwaccm.s.EmulateIoBlock.GCPtrFunctionEip + 0x200
2160 && pCtx->rip > pVCpu->hwaccm.s.EmulateIoBlock.GCPtrFunctionEip - 0x200
2161 && pCtx->cr0 == pVCpu->hwaccm.s.EmulateIoBlock.cr0)
2162 return false;
2163
2164 pVCpu->hwaccm.s.EmulateIoBlock.fEnabled = false;
2165
2166 /* AMD-V supports real & protected mode with or without paging. */
2167 if (pVM->hwaccm.s.svm.fEnabled)
2168 {
2169 pVCpu->hwaccm.s.fActive = true;
2170 return true;
2171 }
2172
2173 pVCpu->hwaccm.s.fActive = false;
2174
2175 /* Note! The context supplied by REM is partial. If we add more checks here, be sure to verify that REM provides this info! */
2176#ifdef HWACCM_VMX_EMULATE_REALMODE
2177 bool fVMMDeviceHeapEnabled = PDMVMMDevHeapIsEnabled(pVM);
2178
2179 Assert((pVM->hwaccm.s.vmx.fUnrestrictedGuest && !pVM->hwaccm.s.vmx.pRealModeTSS) || (!pVM->hwaccm.s.vmx.fUnrestrictedGuest && pVM->hwaccm.s.vmx.pRealModeTSS));
2180
2181 /** The VMM device heap is a requirement for emulating real mode or protected mode without paging when the unrestricted guest execution feature is missing. */
2182 if (fVMMDeviceHeapEnabled)
2183 {
2184 if (CPUMIsGuestInRealModeEx(pCtx))
2185 {
2186 /* VT-x will not allow high selector bases in v86 mode; fall back to the recompiler in that case.
2187 * The base must also be equal to (sel << 4).
2188 */
2189 if ( ( pCtx->cs != (pCtx->csHid.u64Base >> 4)
2190 && pCtx->csHid.u64Base != 0xffff0000 /* we can deal with the BIOS code as it's also mapped into the lower region. */)
2191 || pCtx->ds != (pCtx->dsHid.u64Base >> 4)
2192 || pCtx->es != (pCtx->esHid.u64Base >> 4)
2193 || pCtx->fs != (pCtx->fsHid.u64Base >> 4)
2194 || pCtx->gs != (pCtx->gsHid.u64Base >> 4)
2195 || pCtx->ss != (pCtx->ssHid.u64Base >> 4))
2196 {
2197 return false;
2198 }
2199 }
2200 else
2201 {
2202 PGMMODE enmGuestMode = PGMGetGuestMode(pVCpu);
2203 /* Verify the requirements for executing code in protected mode. VT-x can't handle the CPU state right after a switch
2204 * from real to protected mode. (all sorts of RPL & DPL assumptions)
2205 */
2206 if ( pVCpu->hwaccm.s.vmx.enmLastSeenGuestMode == PGMMODE_REAL
2207 && enmGuestMode >= PGMMODE_PROTECTED)
2208 {
2209 if ( (pCtx->cs & X86_SEL_RPL)
2210 || (pCtx->ds & X86_SEL_RPL)
2211 || (pCtx->es & X86_SEL_RPL)
2212 || (pCtx->fs & X86_SEL_RPL)
2213 || (pCtx->gs & X86_SEL_RPL)
2214 || (pCtx->ss & X86_SEL_RPL))
2215 {
2216 return false;
2217 }
2218 }
2219 }
2220 }
2221 else
2222#endif /* HWACCM_VMX_EMULATE_REALMODE */
2223 {
2224 if ( !CPUMIsGuestInLongModeEx(pCtx)
2225 && !pVM->hwaccm.s.vmx.fUnrestrictedGuest)
2226 {
2227 /** @todo This should (probably) be set on every excursion to the REM,
2228 * however it's too risky right now. So, only apply it when we go
2229 * back to REM for real mode execution. (The XP hack below doesn't
2230 * work reliably without this.)
2231 * Update: Implemented in EM.cpp, see #ifdef EM_NOTIFY_HWACCM. */
2232 pVM->aCpus[0].hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_ALL_GUEST;
2233
2234 if ( !pVM->hwaccm.s.fNestedPaging /* requires a fake PD for real *and* protected mode without paging - stored in the VMM device heap*/
2235 || CPUMIsGuestInRealModeEx(pCtx)) /* requires a fake TSS for real mode - stored in the VMM device heap */
2236 return false;
2237
2238 /* Too early for VT-x; Solaris guests will fail with a guru meditation otherwise; same for XP. */
2239 if (pCtx->idtr.pIdt == 0 || pCtx->idtr.cbIdt == 0 || pCtx->tr == 0)
2240 return false;
2241
2242 /* The guest is about to complete the switch to protected mode. Wait a bit longer. */
2243 /* Windows XP; switch to protected mode; all selectors are marked not present in the
2244 * hidden registers (possible recompiler bug; see load_seg_vm) */
2245 if (pCtx->csHid.Attr.n.u1Present == 0)
2246 return false;
2247 if (pCtx->ssHid.Attr.n.u1Present == 0)
2248 return false;
2249
2250 /* Windows XP: possible same as above, but new recompiler requires new heuristics?
2251 VT-x doesn't seem to like something about the guest state and this stuff avoids it. */
2252 /** @todo This check is actually wrong, it doesn't take the direction of the
2253 * stack segment into account. But, it does the job for now. */
2254 if (pCtx->rsp >= pCtx->ssHid.u32Limit)
2255 return false;
2256#if 0
2257 if ( pCtx->cs >= pCtx->gdtr.cbGdt
2258 || pCtx->ss >= pCtx->gdtr.cbGdt
2259 || pCtx->ds >= pCtx->gdtr.cbGdt
2260 || pCtx->es >= pCtx->gdtr.cbGdt
2261 || pCtx->fs >= pCtx->gdtr.cbGdt
2262 || pCtx->gs >= pCtx->gdtr.cbGdt)
2263 return false;
2264#endif
2265 }
2266 }
2267
2268 if (pVM->hwaccm.s.vmx.fEnabled)
2269 {
2270 uint32_t mask;
2271
2272 if (!pVM->hwaccm.s.vmx.fUnrestrictedGuest)
2273 {
2274 /* if bit N is set in cr0_fixed0, then it must be set in the guest's cr0. */
2275 mask = (uint32_t)pVM->hwaccm.s.vmx.msr.vmx_cr0_fixed0;
2276 /* Note: We ignore the NE bit here on purpose; see vmmr0\hwaccmr0.cpp for details. */
2277 mask &= ~X86_CR0_NE;
2278
2279#ifdef HWACCM_VMX_EMULATE_REALMODE
2280 if (fVMMDeviceHeapEnabled)
2281 {
2282 /* Note: We ignore the PE & PG bits here on purpose; we emulate real and protected mode without paging. */
2283 mask &= ~(X86_CR0_PG|X86_CR0_PE);
2284 }
2285 else
2286#endif
2287 {
2288 /* We support protected mode without paging using identity mapping. */
2289 mask &= ~X86_CR0_PG;
2290 }
2291 if ((pCtx->cr0 & mask) != mask)
2292 return false;
2293
2294 /* if bit N is cleared in cr0_fixed1, then it must be zero in the guest's cr0. */
2295 mask = (uint32_t)~pVM->hwaccm.s.vmx.msr.vmx_cr0_fixed1;
2296 if ((pCtx->cr0 & mask) != 0)
2297 return false;
2298 }
2299
2300 /* if bit N is set in cr4_fixed0, then it must be set in the guest's cr4. */
2301 mask = (uint32_t)pVM->hwaccm.s.vmx.msr.vmx_cr4_fixed0;
2302 mask &= ~X86_CR4_VMXE;
2303 if ((pCtx->cr4 & mask) != mask)
2304 return false;
2305
2306 /* if bit N is cleared in cr4_fixed1, then it must be zero in the guest's cr4. */
2307 mask = (uint32_t)~pVM->hwaccm.s.vmx.msr.vmx_cr4_fixed1;
2308 if ((pCtx->cr4 & mask) != 0)
2309 return false;
2310
2311 pVCpu->hwaccm.s.fActive = true;
2312 return true;
2313 }
2314
2315 return false;
2316}
2317
2318/**
2319 * Checks if we need to reschedule due to VMM device heap changes
2320 *
2321 * @returns boolean
2322 * @param pVM The VM to operate on.
2323 * @param pCtx VM execution context
2324 */
2325VMMR3DECL(bool) HWACCMR3IsRescheduleRequired(PVM pVM, PCPUMCTX pCtx)
2326{
2327 /** The VMM device heap is a requirement for emulating real mode or protected mode without paging when the unrestricted guest execution feature is missing. (VT-x only) */
2328 if ( pVM->hwaccm.s.vmx.fEnabled
2329 && !CPUMIsGuestInPagedProtectedModeEx(pCtx)
2330 && !PDMVMMDevHeapIsEnabled(pVM)
2331 && (pVM->hwaccm.s.fNestedPaging || CPUMIsGuestInRealModeEx(pCtx)))
2332 return true;
2333
2334 return false;
2335}
2336
2337
2338/**
2339 * Notifcation from EM about a rescheduling into hardware assisted execution
2340 * mode.
2341 *
2342 * @param pVCpu Pointer to the current virtual cpu structure.
2343 */
2344VMMR3DECL(void) HWACCMR3NotifyScheduled(PVMCPU pVCpu)
2345{
2346 pVCpu->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_ALL_GUEST;
2347}
2348
2349/**
2350 * Notifcation from EM about returning from instruction emulation (REM / EM).
2351 *
2352 * @param pVCpu Pointer to the current virtual cpu structure.
2353 */
2354VMMR3DECL(void) HWACCMR3NotifyEmulated(PVMCPU pVCpu)
2355{
2356 pVCpu->hwaccm.s.fContextUseFlags |= HWACCM_CHANGED_ALL_GUEST;
2357}
2358
2359/**
2360 * Checks if we are currently using hardware accelerated raw mode.
2361 *
2362 * @returns boolean
2363 * @param pVCpu The VMCPU to operate on.
2364 */
2365VMMR3DECL(bool) HWACCMR3IsActive(PVMCPU pVCpu)
2366{
2367 return pVCpu->hwaccm.s.fActive;
2368}
2369
2370/**
2371 * Checks if we are currently using nested paging.
2372 *
2373 * @returns boolean
2374 * @param pVM The VM to operate on.
2375 */
2376VMMR3DECL(bool) HWACCMR3IsNestedPagingActive(PVM pVM)
2377{
2378 return pVM->hwaccm.s.fNestedPaging;
2379}
2380
2381/**
2382 * Checks if we are currently using VPID in VT-x mode.
2383 *
2384 * @returns boolean
2385 * @param pVM The VM to operate on.
2386 */
2387VMMR3DECL(bool) HWACCMR3IsVPIDActive(PVM pVM)
2388{
2389 return pVM->hwaccm.s.vmx.fVPID;
2390}
2391
2392
2393/**
2394 * Checks if internal events are pending. In that case we are not allowed to dispatch interrupts.
2395 *
2396 * @returns boolean
2397 * @param pVM The VM to operate on.
2398 */
2399VMMR3DECL(bool) HWACCMR3IsEventPending(PVMCPU pVCpu)
2400{
2401 return HWACCMIsEnabled(pVCpu->pVMR3) && pVCpu->hwaccm.s.Event.fPending;
2402}
2403
2404/**
2405 * Restart an I/O instruction that was refused in ring-0
2406 *
2407 * @returns Strict VBox status code. Informational status codes other than the one documented
2408 * here are to be treated as internal failure. Use IOM_SUCCESS() to check for success.
2409 * @retval VINF_SUCCESS Success.
2410 * @retval VINF_EM_FIRST-VINF_EM_LAST Success with some exceptions (see IOM_SUCCESS()), the
2411 * status code must be passed on to EM.
2412 * @retval VERR_NOT_FOUND if no pending I/O instruction.
2413 *
2414 * @param pVM The VM to operate on.
2415 * @param pVCpu The VMCPU to operate on.
2416 * @param pCtx VCPU register context
2417 */
2418VMMR3DECL(VBOXSTRICTRC) HWACCMR3RestartPendingIOInstr(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
2419{
2420 HWACCMPENDINGIO enmType = pVCpu->hwaccm.s.PendingIO.enmType;
2421
2422 pVCpu->hwaccm.s.PendingIO.enmType = HWACCMPENDINGIO_INVALID;
2423
2424 if ( pVCpu->hwaccm.s.PendingIO.GCPtrRip != pCtx->rip
2425 || enmType == HWACCMPENDINGIO_INVALID)
2426 return VERR_NOT_FOUND;
2427
2428 VBOXSTRICTRC rcStrict;
2429 switch (enmType)
2430 {
2431 case HWACCMPENDINGIO_PORT_READ:
2432 {
2433 uint32_t uAndVal = pVCpu->hwaccm.s.PendingIO.s.Port.uAndVal;
2434 uint32_t u32Val = 0;
2435
2436 rcStrict = IOMIOPortRead(pVM, pVCpu->hwaccm.s.PendingIO.s.Port.uPort,
2437 &u32Val,
2438 pVCpu->hwaccm.s.PendingIO.s.Port.cbSize);
2439 if (IOM_SUCCESS(rcStrict))
2440 {
2441 /* Write back to the EAX register. */
2442 pCtx->eax = (pCtx->eax & ~uAndVal) | (u32Val & uAndVal);
2443 pCtx->rip = pVCpu->hwaccm.s.PendingIO.GCPtrRipNext;
2444 }
2445 break;
2446 }
2447
2448 case HWACCMPENDINGIO_PORT_WRITE:
2449 rcStrict = IOMIOPortWrite(pVM, pVCpu->hwaccm.s.PendingIO.s.Port.uPort,
2450 pCtx->eax & pVCpu->hwaccm.s.PendingIO.s.Port.uAndVal,
2451 pVCpu->hwaccm.s.PendingIO.s.Port.cbSize);
2452 if (IOM_SUCCESS(rcStrict))
2453 pCtx->rip = pVCpu->hwaccm.s.PendingIO.GCPtrRipNext;
2454 break;
2455
2456 default:
2457 AssertFailed();
2458 return VERR_INTERNAL_ERROR;
2459 }
2460
2461 return rcStrict;
2462}
2463
2464/**
2465 * Inject an NMI into a running VM (only VCPU 0!)
2466 *
2467 * @returns boolean
2468 * @param pVM The VM to operate on.
2469 */
2470VMMR3DECL(int) HWACCMR3InjectNMI(PVM pVM)
2471{
2472 VMCPU_FF_SET(&pVM->aCpus[0], VMCPU_FF_INTERRUPT_NMI);
2473 return VINF_SUCCESS;
2474}
2475
2476/**
2477 * Check fatal VT-x/AMD-V error and produce some meaningful
2478 * log release message.
2479 *
2480 * @param pVM The VM to operate on.
2481 * @param iStatusCode VBox status code
2482 */
2483VMMR3DECL(void) HWACCMR3CheckError(PVM pVM, int iStatusCode)
2484{
2485 for (VMCPUID i = 0; i < pVM->cCpus; i++)
2486 {
2487 switch(iStatusCode)
2488 {
2489 case VERR_VMX_INVALID_VMCS_FIELD:
2490 break;
2491
2492 case VERR_VMX_INVALID_VMCS_PTR:
2493 LogRel(("VERR_VMX_INVALID_VMCS_PTR: CPU%d Current pointer %RGp vs %RGp\n", i, pVM->aCpus[i].hwaccm.s.vmx.lasterror.u64VMCSPhys, pVM->aCpus[i].hwaccm.s.vmx.pVMCSPhys));
2494 LogRel(("VERR_VMX_INVALID_VMCS_PTR: CPU%d Current VMCS version %x\n", i, pVM->aCpus[i].hwaccm.s.vmx.lasterror.ulVMCSRevision));
2495 LogRel(("VERR_VMX_INVALID_VMCS_PTR: CPU%d Entered Cpu %d\n", i, pVM->aCpus[i].hwaccm.s.vmx.lasterror.idEnteredCpu));
2496 LogRel(("VERR_VMX_INVALID_VMCS_PTR: CPU%d Current Cpu %d\n", i, pVM->aCpus[i].hwaccm.s.vmx.lasterror.idCurrentCpu));
2497 break;
2498
2499 case VERR_VMX_UNABLE_TO_START_VM:
2500 LogRel(("VERR_VMX_UNABLE_TO_START_VM: CPU%d instruction error %x\n", i, pVM->aCpus[i].hwaccm.s.vmx.lasterror.ulInstrError));
2501 LogRel(("VERR_VMX_UNABLE_TO_START_VM: CPU%d exit reason %x\n", i, pVM->aCpus[i].hwaccm.s.vmx.lasterror.ulExitReason));
2502#if 0 /* @todo dump the current control fields to the release log */
2503 if (pVM->aCpus[i].hwaccm.s.vmx.lasterror.ulInstrError == VMX_ERROR_VMENTRY_INVALID_CONTROL_FIELDS)
2504 {
2505
2506 }
2507#endif
2508 break;
2509
2510 case VERR_VMX_UNABLE_TO_RESUME_VM:
2511 LogRel(("VERR_VMX_UNABLE_TO_RESUME_VM: CPU%d instruction error %x\n", i, pVM->aCpus[i].hwaccm.s.vmx.lasterror.ulInstrError));
2512 LogRel(("VERR_VMX_UNABLE_TO_RESUME_VM: CPU%d exit reason %x\n", i, pVM->aCpus[i].hwaccm.s.vmx.lasterror.ulExitReason));
2513 break;
2514
2515 case VERR_VMX_INVALID_VMXON_PTR:
2516 break;
2517 }
2518 }
2519}
2520
2521/**
2522 * Execute state save operation.
2523 *
2524 * @returns VBox status code.
2525 * @param pVM VM Handle.
2526 * @param pSSM SSM operation handle.
2527 */
2528static DECLCALLBACK(int) hwaccmR3Save(PVM pVM, PSSMHANDLE pSSM)
2529{
2530 int rc;
2531
2532 Log(("hwaccmR3Save:\n"));
2533
2534 for (VMCPUID i = 0; i < pVM->cCpus; i++)
2535 {
2536 /*
2537 * Save the basic bits - fortunately all the other things can be resynced on load.
2538 */
2539 rc = SSMR3PutU32(pSSM, pVM->aCpus[i].hwaccm.s.Event.fPending);
2540 AssertRCReturn(rc, rc);
2541 rc = SSMR3PutU32(pSSM, pVM->aCpus[i].hwaccm.s.Event.errCode);
2542 AssertRCReturn(rc, rc);
2543 rc = SSMR3PutU64(pSSM, pVM->aCpus[i].hwaccm.s.Event.intInfo);
2544 AssertRCReturn(rc, rc);
2545
2546 rc = SSMR3PutU32(pSSM, pVM->aCpus[i].hwaccm.s.vmx.enmLastSeenGuestMode);
2547 AssertRCReturn(rc, rc);
2548 rc = SSMR3PutU32(pSSM, pVM->aCpus[i].hwaccm.s.vmx.enmCurrGuestMode);
2549 AssertRCReturn(rc, rc);
2550 rc = SSMR3PutU32(pSSM, pVM->aCpus[i].hwaccm.s.vmx.enmPrevGuestMode);
2551 AssertRCReturn(rc, rc);
2552 }
2553#ifdef VBOX_HWACCM_WITH_GUEST_PATCHING
2554 rc = SSMR3PutGCPtr(pSSM, pVM->hwaccm.s.pGuestPatchMem);
2555 AssertRCReturn(rc, rc);
2556 rc = SSMR3PutGCPtr(pSSM, pVM->hwaccm.s.pFreeGuestPatchMem);
2557 AssertRCReturn(rc, rc);
2558 rc = SSMR3PutU32(pSSM, pVM->hwaccm.s.cbGuestPatchMem);
2559 AssertRCReturn(rc, rc);
2560
2561 /* Store all the guest patch records too. */
2562 rc = SSMR3PutU32(pSSM, pVM->hwaccm.s.cPatches);
2563 AssertRCReturn(rc, rc);
2564
2565 for (unsigned i = 0; i < pVM->hwaccm.s.cPatches; i++)
2566 {
2567 PHWACCMTPRPATCH pPatch = &pVM->hwaccm.s.aPatches[i];
2568
2569 rc = SSMR3PutU32(pSSM, pPatch->Core.Key);
2570 AssertRCReturn(rc, rc);
2571
2572 rc = SSMR3PutMem(pSSM, pPatch->aOpcode, sizeof(pPatch->aOpcode));
2573 AssertRCReturn(rc, rc);
2574
2575 rc = SSMR3PutU32(pSSM, pPatch->cbOp);
2576 AssertRCReturn(rc, rc);
2577
2578 rc = SSMR3PutMem(pSSM, pPatch->aNewOpcode, sizeof(pPatch->aNewOpcode));
2579 AssertRCReturn(rc, rc);
2580
2581 rc = SSMR3PutU32(pSSM, pPatch->cbNewOp);
2582 AssertRCReturn(rc, rc);
2583
2584 AssertCompileSize(HWACCMTPRINSTR, 4);
2585 rc = SSMR3PutU32(pSSM, (uint32_t)pPatch->enmType);
2586 AssertRCReturn(rc, rc);
2587
2588 rc = SSMR3PutU32(pSSM, pPatch->uSrcOperand);
2589 AssertRCReturn(rc, rc);
2590
2591 rc = SSMR3PutU32(pSSM, pPatch->uDstOperand);
2592 AssertRCReturn(rc, rc);
2593
2594 rc = SSMR3PutU32(pSSM, pPatch->pJumpTarget);
2595 AssertRCReturn(rc, rc);
2596
2597 rc = SSMR3PutU32(pSSM, pPatch->cFaults);
2598 AssertRCReturn(rc, rc);
2599 }
2600#endif
2601 return VINF_SUCCESS;
2602}
2603
2604/**
2605 * Execute state load operation.
2606 *
2607 * @returns VBox status code.
2608 * @param pVM VM Handle.
2609 * @param pSSM SSM operation handle.
2610 * @param uVersion Data layout version.
2611 * @param uPass The data pass.
2612 */
2613static DECLCALLBACK(int) hwaccmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
2614{
2615 int rc;
2616
2617 Log(("hwaccmR3Load:\n"));
2618 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
2619
2620 /*
2621 * Validate version.
2622 */
2623 if ( uVersion != HWACCM_SSM_VERSION
2624 && uVersion != HWACCM_SSM_VERSION_NO_PATCHING
2625 && uVersion != HWACCM_SSM_VERSION_2_0_X)
2626 {
2627 AssertMsgFailed(("hwaccmR3Load: Invalid version uVersion=%d!\n", uVersion));
2628 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
2629 }
2630 for (VMCPUID i = 0; i < pVM->cCpus; i++)
2631 {
2632 rc = SSMR3GetU32(pSSM, &pVM->aCpus[i].hwaccm.s.Event.fPending);
2633 AssertRCReturn(rc, rc);
2634 rc = SSMR3GetU32(pSSM, &pVM->aCpus[i].hwaccm.s.Event.errCode);
2635 AssertRCReturn(rc, rc);
2636 rc = SSMR3GetU64(pSSM, &pVM->aCpus[i].hwaccm.s.Event.intInfo);
2637 AssertRCReturn(rc, rc);
2638
2639 if (uVersion >= HWACCM_SSM_VERSION_NO_PATCHING)
2640 {
2641 uint32_t val;
2642
2643 rc = SSMR3GetU32(pSSM, &val);
2644 AssertRCReturn(rc, rc);
2645 pVM->aCpus[i].hwaccm.s.vmx.enmLastSeenGuestMode = (PGMMODE)val;
2646
2647 rc = SSMR3GetU32(pSSM, &val);
2648 AssertRCReturn(rc, rc);
2649 pVM->aCpus[i].hwaccm.s.vmx.enmCurrGuestMode = (PGMMODE)val;
2650
2651 rc = SSMR3GetU32(pSSM, &val);
2652 AssertRCReturn(rc, rc);
2653 pVM->aCpus[i].hwaccm.s.vmx.enmPrevGuestMode = (PGMMODE)val;
2654 }
2655 }
2656#ifdef VBOX_HWACCM_WITH_GUEST_PATCHING
2657 if (uVersion > HWACCM_SSM_VERSION_NO_PATCHING)
2658 {
2659 rc = SSMR3GetGCPtr(pSSM, &pVM->hwaccm.s.pGuestPatchMem);
2660 AssertRCReturn(rc, rc);
2661 rc = SSMR3GetGCPtr(pSSM, &pVM->hwaccm.s.pFreeGuestPatchMem);
2662 AssertRCReturn(rc, rc);
2663 rc = SSMR3GetU32(pSSM, &pVM->hwaccm.s.cbGuestPatchMem);
2664 AssertRCReturn(rc, rc);
2665
2666 /* Fetch all TPR patch records. */
2667 rc = SSMR3GetU32(pSSM, &pVM->hwaccm.s.cPatches);
2668 AssertRCReturn(rc, rc);
2669
2670 for (unsigned i = 0; i < pVM->hwaccm.s.cPatches; i++)
2671 {
2672 PHWACCMTPRPATCH pPatch = &pVM->hwaccm.s.aPatches[i];
2673
2674 rc = SSMR3GetU32(pSSM, &pPatch->Core.Key);
2675 AssertRCReturn(rc, rc);
2676
2677 rc = SSMR3GetMem(pSSM, pPatch->aOpcode, sizeof(pPatch->aOpcode));
2678 AssertRCReturn(rc, rc);
2679
2680 rc = SSMR3GetU32(pSSM, &pPatch->cbOp);
2681 AssertRCReturn(rc, rc);
2682
2683 rc = SSMR3GetMem(pSSM, pPatch->aNewOpcode, sizeof(pPatch->aNewOpcode));
2684 AssertRCReturn(rc, rc);
2685
2686 rc = SSMR3GetU32(pSSM, &pPatch->cbNewOp);
2687 AssertRCReturn(rc, rc);
2688
2689 rc = SSMR3GetU32(pSSM, (uint32_t *)&pPatch->enmType);
2690 AssertRCReturn(rc, rc);
2691
2692 if (pPatch->enmType == HWACCMTPRINSTR_JUMP_REPLACEMENT)
2693 pVM->hwaccm.s.fTPRPatchingActive = true;
2694
2695 Assert(pPatch->enmType == HWACCMTPRINSTR_JUMP_REPLACEMENT || pVM->hwaccm.s.fTPRPatchingActive == false);
2696
2697 rc = SSMR3GetU32(pSSM, &pPatch->uSrcOperand);
2698 AssertRCReturn(rc, rc);
2699
2700 rc = SSMR3GetU32(pSSM, &pPatch->uDstOperand);
2701 AssertRCReturn(rc, rc);
2702
2703 rc = SSMR3GetU32(pSSM, &pPatch->cFaults);
2704 AssertRCReturn(rc, rc);
2705
2706 rc = SSMR3GetU32(pSSM, &pPatch->pJumpTarget);
2707 AssertRCReturn(rc, rc);
2708
2709 Log(("hwaccmR3Load: patch %d\n", i));
2710 Log(("Key = %x\n", pPatch->Core.Key));
2711 Log(("cbOp = %d\n", pPatch->cbOp));
2712 Log(("cbNewOp = %d\n", pPatch->cbNewOp));
2713 Log(("type = %d\n", pPatch->enmType));
2714 Log(("srcop = %d\n", pPatch->uSrcOperand));
2715 Log(("dstop = %d\n", pPatch->uDstOperand));
2716 Log(("cFaults = %d\n", pPatch->cFaults));
2717 Log(("target = %x\n", pPatch->pJumpTarget));
2718 rc = RTAvloU32Insert(&pVM->hwaccm.s.PatchTree, &pPatch->Core);
2719 AssertRC(rc);
2720 }
2721 }
2722#endif
2723
2724 /* Recheck all VCPUs if we can go staight into hwaccm execution mode. */
2725 if (HWACCMIsEnabled(pVM))
2726 {
2727 for (VMCPUID i = 0; i < pVM->cCpus; i++)
2728 {
2729 PVMCPU pVCpu = &pVM->aCpus[i];
2730
2731 HWACCMR3CanExecuteGuest(pVM, CPUMQueryGuestCtxPtr(pVCpu));
2732 }
2733 }
2734 return VINF_SUCCESS;
2735}
2736
Note: See TracBrowser for help on using the repository browser.

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