VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/HM.cpp@ 47472

Last change on this file since 47472 was 47444, checked in by vboxsync, 12 years ago

IEM,HM,PGM: Started on string I/O optimizations using IEM (disabled). Cleaned up confusing status code handling in hmR0VmxCheckForceFlags (involving PGM) as well as some use of incorrect doxygen groups (@name).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 139.1 KB
Line 
1/* $Id: HM.cpp 47444 2013-07-29 00:37:31Z vboxsync $ */
2/** @file
3 * HM - Intel/AMD VM Hardware Support Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_HM
23#include <VBox/vmm/cpum.h>
24#include <VBox/vmm/stam.h>
25#include <VBox/vmm/mm.h>
26#include <VBox/vmm/pdmapi.h>
27#include <VBox/vmm/pgm.h>
28#include <VBox/vmm/ssm.h>
29#include <VBox/vmm/trpm.h>
30#include <VBox/vmm/dbgf.h>
31#include <VBox/vmm/iom.h>
32#include <VBox/vmm/patm.h>
33#include <VBox/vmm/csam.h>
34#include <VBox/vmm/selm.h>
35#ifdef VBOX_WITH_REM
36# include <VBox/vmm/rem.h>
37#endif
38#include <VBox/vmm/hm_vmx.h>
39#include <VBox/vmm/hm_svm.h>
40#include "HMInternal.h"
41#include <VBox/vmm/vm.h>
42#include <VBox/vmm/uvm.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/asm-amd64-x86.h>
50#include <iprt/string.h>
51#include <iprt/env.h>
52#include <iprt/thread.h>
53
54
55/*******************************************************************************
56* Global Variables *
57*******************************************************************************/
58#ifdef VBOX_WITH_STATISTICS
59# define EXIT_REASON(def, val, str) #def " - " #val " - " str
60# define EXIT_REASON_NIL() NULL
61/** Exit reason descriptions for VT-x, used to describe statistics. */
62static const char * const g_apszVTxExitReasons[MAX_EXITREASON_STAT] =
63{
64 EXIT_REASON(VMX_EXIT_XCPT_OR_NMI , 0, "Exception or non-maskable interrupt (NMI)."),
65 EXIT_REASON(VMX_EXIT_EXT_INT , 1, "External interrupt."),
66 EXIT_REASON(VMX_EXIT_TRIPLE_FAULT , 2, "Triple fault."),
67 EXIT_REASON(VMX_EXIT_INIT_SIGNAL , 3, "INIT signal."),
68 EXIT_REASON(VMX_EXIT_SIPI , 4, "Start-up IPI (SIPI)."),
69 EXIT_REASON(VMX_EXIT_IO_SMI_IRQ , 5, "I/O system-management interrupt (SMI)."),
70 EXIT_REASON(VMX_EXIT_SMI_IRQ , 6, "Other SMI."),
71 EXIT_REASON(VMX_EXIT_INT_WINDOW , 7, "Interrupt window."),
72 EXIT_REASON_NIL(),
73 EXIT_REASON(VMX_EXIT_TASK_SWITCH , 9, "Task switch."),
74 EXIT_REASON(VMX_EXIT_CPUID , 10, "Guest attempted to execute CPUID."),
75 EXIT_REASON_NIL(),
76 EXIT_REASON(VMX_EXIT_HLT , 12, "Guest attempted to execute HLT."),
77 EXIT_REASON(VMX_EXIT_INVD , 13, "Guest attempted to execute INVD."),
78 EXIT_REASON(VMX_EXIT_INVLPG , 14, "Guest attempted to execute INVLPG."),
79 EXIT_REASON(VMX_EXIT_RDPMC , 15, "Guest attempted to execute RDPMC."),
80 EXIT_REASON(VMX_EXIT_RDTSC , 16, "Guest attempted to execute RDTSC."),
81 EXIT_REASON(VMX_EXIT_RSM , 17, "Guest attempted to execute RSM in SMM."),
82 EXIT_REASON(VMX_EXIT_VMCALL , 18, "Guest attempted to execute VMCALL."),
83 EXIT_REASON(VMX_EXIT_VMCLEAR , 19, "Guest attempted to execute VMCLEAR."),
84 EXIT_REASON(VMX_EXIT_VMLAUNCH , 20, "Guest attempted to execute VMLAUNCH."),
85 EXIT_REASON(VMX_EXIT_VMPTRLD , 21, "Guest attempted to execute VMPTRLD."),
86 EXIT_REASON(VMX_EXIT_VMPTRST , 22, "Guest attempted to execute VMPTRST."),
87 EXIT_REASON(VMX_EXIT_VMREAD , 23, "Guest attempted to execute VMREAD."),
88 EXIT_REASON(VMX_EXIT_VMRESUME , 24, "Guest attempted to execute VMRESUME."),
89 EXIT_REASON(VMX_EXIT_VMWRITE , 25, "Guest attempted to execute VMWRITE."),
90 EXIT_REASON(VMX_EXIT_VMXOFF , 26, "Guest attempted to execute VMXOFF."),
91 EXIT_REASON(VMX_EXIT_VMXON , 27, "Guest attempted to execute VMXON."),
92 EXIT_REASON(VMX_EXIT_MOV_CRX , 28, "Control-register accesses."),
93 EXIT_REASON(VMX_EXIT_MOV_DRX , 29, "Debug-register accesses."),
94 EXIT_REASON(VMX_EXIT_PORT_IO , 30, "I/O instruction."),
95 EXIT_REASON(VMX_EXIT_RDMSR , 31, "Guest attempted to execute RDMSR."),
96 EXIT_REASON(VMX_EXIT_WRMSR , 32, "Guest attempted to execute WRMSR."),
97 EXIT_REASON(VMX_EXIT_ERR_INVALID_GUEST_STATE, 33, "VM-entry failure due to invalid guest state."),
98 EXIT_REASON(VMX_EXIT_ERR_MSR_LOAD , 34, "VM-entry failure due to MSR loading."),
99 EXIT_REASON_NIL(),
100 EXIT_REASON(VMX_EXIT_MWAIT , 36, "Guest executed MWAIT."),
101 EXIT_REASON(VMX_EXIT_MTF , 37, "Monitor Trap Flag."),
102 EXIT_REASON_NIL(),
103 EXIT_REASON(VMX_EXIT_MONITOR , 39, "Guest attempted to execute MONITOR."),
104 EXIT_REASON(VMX_EXIT_PAUSE , 40, "Guest attempted to execute PAUSE."),
105 EXIT_REASON(VMX_EXIT_ERR_MACHINE_CHECK , 41, "VM-entry failure due to machine-check."),
106 EXIT_REASON_NIL(),
107 EXIT_REASON(VMX_EXIT_TPR_BELOW_THRESHOLD, 43, "TPR below threshold. Guest attempted to execute MOV to CR8."),
108 EXIT_REASON(VMX_EXIT_APIC_ACCESS , 44, "APIC access. Guest attempted to access memory at a physical address on the APIC-access page."),
109 EXIT_REASON_NIL(),
110 EXIT_REASON(VMX_EXIT_XDTR_ACCESS , 46, "Access to GDTR or IDTR. Guest attempted to execute LGDT, LIDT, SGDT, or SIDT."),
111 EXIT_REASON(VMX_EXIT_TR_ACCESS , 47, "Access to LDTR or TR. Guest attempted to execute LLDT, LTR, SLDT, or STR."),
112 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."),
113 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."),
114 EXIT_REASON(VMX_EXIT_INVEPT , 50, "Guest attempted to execute INVEPT."),
115 EXIT_REASON(VMX_EXIT_RDTSCP , 51, "Guest attempted to execute RDTSCP."),
116 EXIT_REASON(VMX_EXIT_PREEMPT_TIMER , 52, "VMX-preemption timer expired."),
117 EXIT_REASON(VMX_EXIT_INVVPID , 53, "Guest attempted to execute INVVPID."),
118 EXIT_REASON(VMX_EXIT_WBINVD , 54, "Guest attempted to execute WBINVD."),
119 EXIT_REASON(VMX_EXIT_XSETBV , 55, "Guest attempted to execute XSETBV."),
120 EXIT_REASON_NIL(),
121 EXIT_REASON(VMX_EXIT_RDRAND , 57, "Guest attempted to execute RDRAND."),
122 EXIT_REASON(VMX_EXIT_INVPCID , 58, "Guest attempted to execute INVPCID."),
123 EXIT_REASON(VMX_EXIT_VMFUNC , 59, "Guest attempted to execute VMFUNC.")
124};
125/** Exit reason descriptions for AMD-V, used to describe statistics. */
126static const char * const g_apszAmdVExitReasons[MAX_EXITREASON_STAT] =
127{
128 EXIT_REASON(SVM_EXIT_READ_CR0 , 0, "Read CR0."),
129 EXIT_REASON(SVM_EXIT_READ_CR1 , 1, "Read CR1."),
130 EXIT_REASON(SVM_EXIT_READ_CR2 , 2, "Read CR2."),
131 EXIT_REASON(SVM_EXIT_READ_CR3 , 3, "Read CR3."),
132 EXIT_REASON(SVM_EXIT_READ_CR4 , 4, "Read CR4."),
133 EXIT_REASON(SVM_EXIT_READ_CR5 , 5, "Read CR5."),
134 EXIT_REASON(SVM_EXIT_READ_CR6 , 6, "Read CR6."),
135 EXIT_REASON(SVM_EXIT_READ_CR7 , 7, "Read CR7."),
136 EXIT_REASON(SVM_EXIT_READ_CR8 , 8, "Read CR8."),
137 EXIT_REASON(SVM_EXIT_READ_CR9 , 9, "Read CR9."),
138 EXIT_REASON(SVM_EXIT_READ_CR10 , 10, "Read CR10."),
139 EXIT_REASON(SVM_EXIT_READ_CR11 , 11, "Read CR11."),
140 EXIT_REASON(SVM_EXIT_READ_CR12 , 12, "Read CR12."),
141 EXIT_REASON(SVM_EXIT_READ_CR13 , 13, "Read CR13."),
142 EXIT_REASON(SVM_EXIT_READ_CR14 , 14, "Read CR14."),
143 EXIT_REASON(SVM_EXIT_READ_CR15 , 15, "Read CR15."),
144 EXIT_REASON(SVM_EXIT_WRITE_CR0 , 16, "Write CR0."),
145 EXIT_REASON(SVM_EXIT_WRITE_CR1 , 17, "Write CR1."),
146 EXIT_REASON(SVM_EXIT_WRITE_CR2 , 18, "Write CR2."),
147 EXIT_REASON(SVM_EXIT_WRITE_CR3 , 19, "Write CR3."),
148 EXIT_REASON(SVM_EXIT_WRITE_CR4 , 20, "Write CR4."),
149 EXIT_REASON(SVM_EXIT_WRITE_CR5 , 21, "Write CR5."),
150 EXIT_REASON(SVM_EXIT_WRITE_CR6 , 22, "Write CR6."),
151 EXIT_REASON(SVM_EXIT_WRITE_CR7 , 23, "Write CR7."),
152 EXIT_REASON(SVM_EXIT_WRITE_CR8 , 24, "Write CR8."),
153 EXIT_REASON(SVM_EXIT_WRITE_CR9 , 25, "Write CR9."),
154 EXIT_REASON(SVM_EXIT_WRITE_CR10 , 26, "Write CR10."),
155 EXIT_REASON(SVM_EXIT_WRITE_CR11 , 27, "Write CR11."),
156 EXIT_REASON(SVM_EXIT_WRITE_CR12 , 28, "Write CR12."),
157 EXIT_REASON(SVM_EXIT_WRITE_CR13 , 29, "Write CR13."),
158 EXIT_REASON(SVM_EXIT_WRITE_CR14 , 30, "Write CR14."),
159 EXIT_REASON(SVM_EXIT_WRITE_CR15 , 31, "Write CR15."),
160 EXIT_REASON(SVM_EXIT_READ_DR0 , 32, "Read DR0."),
161 EXIT_REASON(SVM_EXIT_READ_DR1 , 33, "Read DR1."),
162 EXIT_REASON(SVM_EXIT_READ_DR2 , 34, "Read DR2."),
163 EXIT_REASON(SVM_EXIT_READ_DR3 , 35, "Read DR3."),
164 EXIT_REASON(SVM_EXIT_READ_DR4 , 36, "Read DR4."),
165 EXIT_REASON(SVM_EXIT_READ_DR5 , 37, "Read DR5."),
166 EXIT_REASON(SVM_EXIT_READ_DR6 , 38, "Read DR6."),
167 EXIT_REASON(SVM_EXIT_READ_DR7 , 39, "Read DR7."),
168 EXIT_REASON(SVM_EXIT_READ_DR8 , 40, "Read DR8."),
169 EXIT_REASON(SVM_EXIT_READ_DR9 , 41, "Read DR9."),
170 EXIT_REASON(SVM_EXIT_READ_DR10 , 42, "Read DR10."),
171 EXIT_REASON(SVM_EXIT_READ_DR11 , 43, "Read DR11"),
172 EXIT_REASON(SVM_EXIT_READ_DR12 , 44, "Read DR12."),
173 EXIT_REASON(SVM_EXIT_READ_DR13 , 45, "Read DR13."),
174 EXIT_REASON(SVM_EXIT_READ_DR14 , 46, "Read DR14."),
175 EXIT_REASON(SVM_EXIT_READ_DR15 , 47, "Read DR15."),
176 EXIT_REASON(SVM_EXIT_WRITE_DR0 , 48, "Write DR0."),
177 EXIT_REASON(SVM_EXIT_WRITE_DR1 , 49, "Write DR1."),
178 EXIT_REASON(SVM_EXIT_WRITE_DR2 , 50, "Write DR2."),
179 EXIT_REASON(SVM_EXIT_WRITE_DR3 , 51, "Write DR3."),
180 EXIT_REASON(SVM_EXIT_WRITE_DR4 , 52, "Write DR4."),
181 EXIT_REASON(SVM_EXIT_WRITE_DR5 , 53, "Write DR5."),
182 EXIT_REASON(SVM_EXIT_WRITE_DR6 , 54, "Write DR6."),
183 EXIT_REASON(SVM_EXIT_WRITE_DR7 , 55, "Write DR7."),
184 EXIT_REASON(SVM_EXIT_WRITE_DR8 , 56, "Write DR8."),
185 EXIT_REASON(SVM_EXIT_WRITE_DR9 , 57, "Write DR9."),
186 EXIT_REASON(SVM_EXIT_WRITE_DR10 , 58, "Write DR10."),
187 EXIT_REASON(SVM_EXIT_WRITE_DR11 , 59, "Write DR11."),
188 EXIT_REASON(SVM_EXIT_WRITE_DR12 , 60, "Write DR12."),
189 EXIT_REASON(SVM_EXIT_WRITE_DR13 , 61, "Write DR13."),
190 EXIT_REASON(SVM_EXIT_WRITE_DR14 , 62, "Write DR14."),
191 EXIT_REASON(SVM_EXIT_WRITE_DR15 , 63, "Write DR15."),
192 EXIT_REASON(SVM_EXIT_EXCEPTION_0 , 64, "Exception Vector 0 (#DE)."),
193 EXIT_REASON(SVM_EXIT_EXCEPTION_1 , 65, "Exception Vector 1 (#DB)."),
194 EXIT_REASON(SVM_EXIT_EXCEPTION_2 , 66, "Exception Vector 2 (#NMI)."),
195 EXIT_REASON(SVM_EXIT_EXCEPTION_3 , 67, "Exception Vector 3 (#BP)."),
196 EXIT_REASON(SVM_EXIT_EXCEPTION_4 , 68, "Exception Vector 4 (#OF)."),
197 EXIT_REASON(SVM_EXIT_EXCEPTION_5 , 69, "Exception Vector 5 (#BR)."),
198 EXIT_REASON(SVM_EXIT_EXCEPTION_6 , 70, "Exception Vector 6 (#UD)."),
199 EXIT_REASON(SVM_EXIT_EXCEPTION_7 , 71, "Exception Vector 7 (#NM)."),
200 EXIT_REASON(SVM_EXIT_EXCEPTION_8 , 72, "Exception Vector 8 (#DF)."),
201 EXIT_REASON(SVM_EXIT_EXCEPTION_9 , 73, "Exception Vector 9 (#CO_SEG_OVERRUN)."),
202 EXIT_REASON(SVM_EXIT_EXCEPTION_A , 74, "Exception Vector 10 (#TS)."),
203 EXIT_REASON(SVM_EXIT_EXCEPTION_B , 75, "Exception Vector 11 (#NP)."),
204 EXIT_REASON(SVM_EXIT_EXCEPTION_C , 76, "Exception Vector 12 (#SS)."),
205 EXIT_REASON(SVM_EXIT_EXCEPTION_D , 77, "Exception Vector 13 (#GP)."),
206 EXIT_REASON(SVM_EXIT_EXCEPTION_E , 78, "Exception Vector 14 (#PF)."),
207 EXIT_REASON(SVM_EXIT_EXCEPTION_F , 79, "Exception Vector 15 (0x0f)."),
208 EXIT_REASON(SVM_EXIT_EXCEPTION_10 , 80, "Exception Vector 16 (#MF)."),
209 EXIT_REASON(SVM_EXIT_EXCEPTION_11 , 81, "Exception Vector 17 (#AC)."),
210 EXIT_REASON(SVM_EXIT_EXCEPTION_12 , 82, "Exception Vector 18 (#MC)."),
211 EXIT_REASON(SVM_EXIT_EXCEPTION_13 , 83, "Exception Vector 19 (#XF)."),
212 EXIT_REASON(SVM_EXIT_EXCEPTION_14 , 84, "Exception Vector 20 (0x14)."),
213 EXIT_REASON(SVM_EXIT_EXCEPTION_15 , 85, "Exception Vector 22 (0x15)."),
214 EXIT_REASON(SVM_EXIT_EXCEPTION_16 , 86, "Exception Vector 22 (0x16)."),
215 EXIT_REASON(SVM_EXIT_EXCEPTION_17 , 87, "Exception Vector 23 (0x17)."),
216 EXIT_REASON(SVM_EXIT_EXCEPTION_18 , 88, "Exception Vector 24 (0x18)."),
217 EXIT_REASON(SVM_EXIT_EXCEPTION_19 , 89, "Exception Vector 25 (0x19)."),
218 EXIT_REASON(SVM_EXIT_EXCEPTION_1A , 90, "Exception Vector 26 (0x1A)."),
219 EXIT_REASON(SVM_EXIT_EXCEPTION_1B , 91, "Exception Vector 27 (0x1B)."),
220 EXIT_REASON(SVM_EXIT_EXCEPTION_1C , 92, "Exception Vector 28 (0x1C)."),
221 EXIT_REASON(SVM_EXIT_EXCEPTION_1D , 93, "Exception Vector 29 (0x1D)."),
222 EXIT_REASON(SVM_EXIT_EXCEPTION_1E , 94, "Exception Vector 30 (0x1E)."),
223 EXIT_REASON(SVM_EXIT_EXCEPTION_1F , 95, "Exception Vector 31 (0x1F)."),
224 EXIT_REASON(SVM_EXIT_INTR , 96, "Physical maskable interrupt (host)."),
225 EXIT_REASON(SVM_EXIT_NMI , 97, "Physical non-maskable interrupt (host)."),
226 EXIT_REASON(SVM_EXIT_SMI , 98, "System management interrupt (host)."),
227 EXIT_REASON(SVM_EXIT_INIT , 99, "Physical INIT signal (host)."),
228 EXIT_REASON(SVM_EXIT_VINTR ,100, "Virtual interrupt-window exit."),
229 EXIT_REASON(SVM_EXIT_CR0_SEL_WRITE ,101, "Write to CR0 that changed any bits other than CR0.TS or CR0.MP."),
230 EXIT_REASON(SVM_EXIT_IDTR_READ ,102, "Read IDTR"),
231 EXIT_REASON(SVM_EXIT_GDTR_READ ,103, "Read GDTR"),
232 EXIT_REASON(SVM_EXIT_LDTR_READ ,104, "Read LDTR."),
233 EXIT_REASON(SVM_EXIT_TR_READ ,105, "Read TR."),
234 EXIT_REASON(SVM_EXIT_TR_READ ,106, "Write IDTR."),
235 EXIT_REASON(SVM_EXIT_TR_READ ,107, "Write GDTR."),
236 EXIT_REASON(SVM_EXIT_TR_READ ,108, "Write LDTR."),
237 EXIT_REASON(SVM_EXIT_TR_READ ,109, "Write TR."),
238 EXIT_REASON(SVM_EXIT_RDTSC ,110, "RDTSC instruction."),
239 EXIT_REASON(SVM_EXIT_RDPMC ,111, "RDPMC instruction."),
240 EXIT_REASON(SVM_EXIT_PUSHF ,112, "PUSHF instruction."),
241 EXIT_REASON(SVM_EXIT_POPF ,113, "POPF instruction."),
242 EXIT_REASON(SVM_EXIT_CPUID ,114, "CPUID instruction."),
243 EXIT_REASON(SVM_EXIT_RSM ,115, "RSM instruction."),
244 EXIT_REASON(SVM_EXIT_IRET ,116, "IRET instruction."),
245 EXIT_REASON(SVM_EXIT_SWINT ,117, "Software interrupt (INTn instructions)."),
246 EXIT_REASON(SVM_EXIT_INVD ,118, "INVD instruction."),
247 EXIT_REASON(SVM_EXIT_PAUSE ,119, "PAUSE instruction."),
248 EXIT_REASON(SVM_EXIT_HLT ,120, "HLT instruction."),
249 EXIT_REASON(SVM_EXIT_INVLPG ,121, "INVLPG instruction."),
250 EXIT_REASON(SVM_EXIT_INVLPGA ,122, "INVLPGA instruction."),
251 EXIT_REASON(SVM_EXIT_IOIO ,123, "IN/OUT accessing protected port."),
252 EXIT_REASON(SVM_EXIT_MSR ,124, "RDMSR or WRMSR access to protected MSR."),
253 EXIT_REASON(SVM_EXIT_TASK_SWITCH ,125, "Task switch."),
254 EXIT_REASON(SVM_EXIT_FERR_FREEZE ,126, "Legacy FPU handling enabled; processor is frozen in an x87/mmx instruction waiting for an interrupt"),
255 EXIT_REASON(SVM_EXIT_SHUTDOWN ,127, "Shutdown."),
256 EXIT_REASON(SVM_EXIT_VMRUN ,128, "VMRUN instruction."),
257 EXIT_REASON(SVM_EXIT_VMMCALL ,129, "VMCALL instruction."),
258 EXIT_REASON(SVM_EXIT_VMLOAD ,130, "VMLOAD instruction."),
259 EXIT_REASON(SVM_EXIT_VMSAVE ,131, "VMSAVE instruction."),
260 EXIT_REASON(SVM_EXIT_STGI ,132, "STGI instruction."),
261 EXIT_REASON(SVM_EXIT_CLGI ,133, "CLGI instruction."),
262 EXIT_REASON(SVM_EXIT_SKINIT ,134, "SKINIT instruction."),
263 EXIT_REASON(SVM_EXIT_RDTSCP ,135, "RDTSCP instruction."),
264 EXIT_REASON(SVM_EXIT_ICEBP ,136, "ICEBP instruction."),
265 EXIT_REASON(SVM_EXIT_WBINVD ,137, "WBINVD instruction."),
266 EXIT_REASON(SVM_EXIT_MONITOR ,138, "MONITOR instruction."),
267 EXIT_REASON(SVM_EXIT_MWAIT ,139, "MWAIT instruction."),
268 EXIT_REASON(SVM_EXIT_MWAIT_ARMED ,140, "MWAIT instruction when armed."),
269 EXIT_REASON(SVM_EXIT_NPF ,1024, "Nested paging fault."),
270 EXIT_REASON_NIL()
271};
272# undef EXIT_REASON
273# undef EXIT_REASON_NIL
274#endif /* VBOX_WITH_STATISTICS */
275
276#define HMVMX_REPORT_FEATURE(allowed1, disallowed0, featflag) \
277 do { \
278 if ((allowed1) & (featflag)) \
279 LogRel(("HM: " #featflag "\n")); \
280 else \
281 LogRel(("HM: " #featflag " *must* be cleared\n")); \
282 if ((disallowed0) & (featflag)) \
283 LogRel(("HM: " #featflag " *must* be set\n")); \
284 } while (0)
285
286#define HMVMX_REPORT_ALLOWED_FEATURE(allowed1, featflag) \
287 do { \
288 if ((allowed1) & (featflag)) \
289 LogRel(("HM: " #featflag "\n")); \
290 else \
291 LogRel(("HM: " #featflag " not supported\n")); \
292 } while (0)
293
294#define HMVMX_REPORT_CAPABILITY(msrcaps, cap) \
295 do { \
296 if ((msrcaps) & (cap)) \
297 LogRel(("HM: " #cap "\n")); \
298 } while (0)
299
300
301/*******************************************************************************
302* Internal Functions *
303*******************************************************************************/
304static DECLCALLBACK(int) hmR3Save(PVM pVM, PSSMHANDLE pSSM);
305static DECLCALLBACK(int) hmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
306static int hmR3InitCPU(PVM pVM);
307static int hmR3InitFinalizeR0(PVM pVM);
308static int hmR3InitFinalizeR0Intel(PVM pVM);
309static int hmR3InitFinalizeR0Amd(PVM pVM);
310static int hmR3TermCPU(PVM pVM);
311
312
313
314/**
315 * Initializes the HM.
316 *
317 * This reads the config and check whether VT-x or AMD-V hardware is available
318 * if configured to use it. This is one of the very first components to be
319 * initialized after CFGM, so that we can fall back to raw-mode early in the
320 * initialization process.
321 *
322 * Note that a lot of the set up work is done in ring-0 and thus postponed till
323 * the ring-3 and ring-0 callback to HMR3InitCompleted.
324 *
325 * @returns VBox status code.
326 * @param pVM Pointer to the VM.
327 *
328 * @remarks Be careful with what we call here, since most of the VMM components
329 * are uninitialized.
330 */
331VMMR3_INT_DECL(int) HMR3Init(PVM pVM)
332{
333 LogFlow(("HMR3Init\n"));
334
335 /*
336 * Assert alignment and sizes.
337 */
338 AssertCompileMemberAlignment(VM, hm.s, 32);
339 AssertCompile(sizeof(pVM->hm.s) <= sizeof(pVM->hm.padding));
340
341 /*
342 * Register the saved state data unit.
343 */
344 int rc = SSMR3RegisterInternal(pVM, "HWACCM", 0, HM_SSM_VERSION, sizeof(HM),
345 NULL, NULL, NULL,
346 NULL, hmR3Save, NULL,
347 NULL, hmR3Load, NULL);
348 if (RT_FAILURE(rc))
349 return rc;
350
351 /*
352 * Misc initialisation.
353 */
354 //pVM->hm.s.vmx.fSupported = false;
355 //pVM->hm.s.svm.fSupported = false;
356 //pVM->hm.s.vmx.fEnabled = false;
357 //pVM->hm.s.svm.fEnabled = false;
358 //pVM->hm.s.fNestedPaging = false;
359
360
361 /*
362 * Read configuration.
363 */
364 PCFGMNODE pCfgHM = CFGMR3GetChild(CFGMR3GetRoot(pVM), "HM/");
365
366 /** @cfgm{/HM/HMForced, bool, false}
367 * Forces hardware virtualization, no falling back on raw-mode. HM must be
368 * enabled, i.e. /HMEnabled must be true. */
369 bool fHMForced;
370#ifdef VBOX_WITH_RAW_MODE
371 rc = CFGMR3QueryBoolDef(pCfgHM, "HMForced", &fHMForced, false);
372 AssertRCReturn(rc, rc);
373 AssertLogRelMsgReturn(!fHMForced || pVM->fHMEnabled, ("Configuration error: HM forced but not enabled!\n"),
374 VERR_INVALID_PARAMETER);
375# if defined(RT_OS_DARWIN)
376 if (pVM->fHMEnabled)
377 fHMForced = true;
378# endif
379 AssertLogRelMsgReturn(pVM->cCpus == 1 || pVM->fHMEnabled, ("Configuration error: SMP requires HM to be enabled!\n"),
380 VERR_INVALID_PARAMETER);
381 if (pVM->cCpus > 1)
382 fHMForced = true;
383#else /* !VBOX_WITH_RAW_MODE */
384 AssertRelease(pVM->fHMEnabled);
385 fHMForced = true;
386#endif /* !VBOX_WITH_RAW_MODE */
387
388 /** @cfgm{/HM/EnableNestedPaging, bool, false}
389 * Enables nested paging (aka extended page tables). */
390 rc = CFGMR3QueryBoolDef(pCfgHM, "EnableNestedPaging", &pVM->hm.s.fAllowNestedPaging, false);
391 AssertRCReturn(rc, rc);
392
393 /** @cfgm{/HM/EnableUX, bool, true}
394 * Enables the VT-x unrestricted execution feature. */
395 rc = CFGMR3QueryBoolDef(pCfgHM, "EnableUX", &pVM->hm.s.vmx.fAllowUnrestricted, true);
396 AssertRCReturn(rc, rc);
397
398 /** @cfgm{/HM/EnableLargePages, bool, false}
399 * Enables using large pages (2 MB) for guest memory, thus saving on (nested)
400 * page table walking and maybe better TLB hit rate in some cases. */
401 rc = CFGMR3QueryBoolDef(pCfgHM, "EnableLargePages", &pVM->hm.s.fLargePages, false);
402 AssertRCReturn(rc, rc);
403
404 /** @cfgm{/HM/EnableVPID, bool, false}
405 * Enables the VT-x VPID feature. */
406 rc = CFGMR3QueryBoolDef(pCfgHM, "EnableVPID", &pVM->hm.s.vmx.fAllowVpid, false);
407 AssertRCReturn(rc, rc);
408
409 /** @cfgm{/HM/TPRPatchingEnabled, bool, false}
410 * Enables TPR patching for 32-bit windows guests with IO-APIC. */
411 rc = CFGMR3QueryBoolDef(pCfgHM, "TPRPatchingEnabled", &pVM->hm.s.fTRPPatchingAllowed, false);
412 AssertRCReturn(rc, rc);
413
414 /** @cfgm{/HM/64bitEnabled, bool, 32-bit:false, 64-bit:true}
415 * Enables AMD64 cpu features.
416 * On 32-bit hosts this isn't default and require host CPU support. 64-bit hosts
417 * already have the support. */
418#ifdef VBOX_ENABLE_64_BITS_GUESTS
419 rc = CFGMR3QueryBoolDef(pCfgHM, "64bitEnabled", &pVM->hm.s.fAllow64BitGuests, HC_ARCH_BITS == 64);
420 AssertLogRelRCReturn(rc, rc);
421#else
422 pVM->hm.s.fAllow64BitGuests = false;
423#endif
424
425 /** @cfgm{/HM/Exclusive, bool}
426 * Determines the init method for AMD-V and VT-x. If set to true, HM will do a
427 * global init for each host CPU. If false, we do local init each time we wish
428 * to execute guest code.
429 *
430 * Default is false for Mac OS X and Windows due to the higher risk of conflicts
431 * with other hypervisors.
432 */
433 rc = CFGMR3QueryBoolDef(pCfgHM, "Exclusive", &pVM->hm.s.fGlobalInit,
434#if defined(RT_OS_DARWIN) || defined(RT_OS_WINDOWS)
435 false
436#else
437 true
438#endif
439 );
440 AssertLogRelRCReturn(rc, rc);
441
442 /** @cfgm{/HM/MaxResumeLoops, uint32_t}
443 * The number of times to resume guest execution before we forcibly return to
444 * ring-3. The return value of RTThreadPreemptIsPendingTrusty in ring-0
445 * determines the default value. */
446 rc = CFGMR3QueryU32Def(pCfgHM, "MaxResumeLoops", &pVM->hm.s.cMaxResumeLoops, 0 /* set by R0 later */);
447 AssertLogRelRCReturn(rc, rc);
448
449 /*
450 * Check if VT-x or AMD-v support according to the users wishes.
451 */
452 /** @todo SUPR3QueryVTCaps won't catch VERR_VMX_IN_VMX_ROOT_MODE or
453 * VERR_SVM_IN_USE. */
454 if (pVM->fHMEnabled)
455 {
456 uint32_t fCaps;
457 rc = SUPR3QueryVTCaps(&fCaps);
458 if (RT_SUCCESS(rc))
459 {
460 if (fCaps & SUPVTCAPS_AMD_V)
461 LogRel(("HMR3Init: AMD-V%s\n", fCaps & SUPVTCAPS_NESTED_PAGING ? " w/ nested paging" : ""));
462 else if (fCaps & SUPVTCAPS_VT_X)
463 {
464 rc = SUPR3QueryVTxSupported();
465 if (RT_SUCCESS(rc))
466 LogRel(("HMR3Init: VT-x%s\n", fCaps & SUPVTCAPS_NESTED_PAGING ? " w/ nested paging" : ""));
467 else
468 {
469#ifdef RT_OS_LINUX
470 const char *pszMinReq = " Linux 2.6.13 or newer required!";
471#else
472 const char *pszMinReq = "";
473#endif
474 if (fHMForced)
475 return VMSetError(pVM, rc, RT_SRC_POS, "The host kernel does not support VT-x.%s\n", pszMinReq);
476
477 /* Fall back to raw-mode. */
478 LogRel(("HMR3Init: Falling back to raw-mode: The host kernel does not support VT-x.%s\n", pszMinReq));
479 pVM->fHMEnabled = false;
480 }
481 }
482 else
483 AssertLogRelMsgFailedReturn(("SUPR3QueryVTCaps didn't return either AMD-V or VT-x flag set (%#x)!\n", fCaps),
484 VERR_INTERNAL_ERROR_5);
485
486 /*
487 * Do we require a little bit or raw-mode for 64-bit guest execution?
488 */
489 pVM->fHMNeedRawModeCtx = HC_ARCH_BITS == 32
490 && pVM->fHMEnabled
491 && pVM->hm.s.fAllow64BitGuests;
492 }
493 else
494 {
495 const char *pszMsg;
496 switch (rc)
497 {
498 case VERR_UNSUPPORTED_CPU:
499 pszMsg = "Unknown CPU, VT-x or AMD-v features cannot be ascertained.";
500 break;
501
502 case VERR_VMX_NO_VMX:
503 pszMsg = "VT-x is not available.";
504 break;
505
506 case VERR_VMX_MSR_LOCKED_OR_DISABLED:
507 pszMsg = "VT-x is disabled in the BIOS (or by the host OS).";
508 break;
509
510 case VERR_SVM_NO_SVM:
511 pszMsg = "AMD-V is not available.";
512 break;
513
514 case VERR_SVM_DISABLED:
515 pszMsg = "AMD-V is disabled in the BIOS (or by the host OS).";
516 break;
517
518 default:
519 pszMsg = NULL;
520 break;
521 }
522 if (fHMForced && pszMsg)
523 return VM_SET_ERROR(pVM, rc, pszMsg);
524 if (!pszMsg)
525 return VMSetError(pVM, rc, RT_SRC_POS, "SUPR3QueryVTCaps failed with %Rrc", rc);
526
527 /* Fall back to raw-mode. */
528 LogRel(("HMR3Init: Falling back to raw-mode: %s\n", pszMsg));
529 pVM->fHMEnabled = false;
530 }
531 }
532
533 /* It's now OK to use the predicate function. */
534 pVM->fHMEnabledFixed = true;
535 return VINF_SUCCESS;
536}
537
538
539/**
540 * Initializes the per-VCPU HM.
541 *
542 * @returns VBox status code.
543 * @param pVM Pointer to the VM.
544 */
545static int hmR3InitCPU(PVM pVM)
546{
547 LogFlow(("HMR3InitCPU\n"));
548
549 if (!HMIsEnabled(pVM))
550 return VINF_SUCCESS;
551
552 for (VMCPUID i = 0; i < pVM->cCpus; i++)
553 {
554 PVMCPU pVCpu = &pVM->aCpus[i];
555 pVCpu->hm.s.fActive = false;
556 }
557
558#ifdef VBOX_WITH_STATISTICS
559 STAM_REG(pVM, &pVM->hm.s.StatTprPatchSuccess, STAMTYPE_COUNTER, "/HM/TPR/Patch/Success", STAMUNIT_OCCURENCES, "Number of times an instruction was successfully patched.");
560 STAM_REG(pVM, &pVM->hm.s.StatTprPatchFailure, STAMTYPE_COUNTER, "/HM/TPR/Patch/Failed", STAMUNIT_OCCURENCES, "Number of unsuccessful patch attempts.");
561 STAM_REG(pVM, &pVM->hm.s.StatTprReplaceSuccess, STAMTYPE_COUNTER, "/HM/TPR/Replace/Success",STAMUNIT_OCCURENCES, "Number of times an instruction was successfully patched.");
562 STAM_REG(pVM, &pVM->hm.s.StatTprReplaceFailure, STAMTYPE_COUNTER, "/HM/TPR/Replace/Failed", STAMUNIT_OCCURENCES, "Number of unsuccessful patch attempts.");
563
564 /*
565 * Statistics.
566 */
567 for (VMCPUID i = 0; i < pVM->cCpus; i++)
568 {
569 PVMCPU pVCpu = &pVM->aCpus[i];
570 int rc;
571
572 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatPoke, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
573 "Profiling of RTMpPokeCpu",
574 "/PROF/CPU%d/HM/Poke", i);
575 AssertRC(rc);
576 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatSpinPoke, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
577 "Profiling of poke wait",
578 "/PROF/CPU%d/HM/PokeWait", i);
579 AssertRC(rc);
580 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatSpinPokeFailed, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
581 "Profiling of poke wait when RTMpPokeCpu fails",
582 "/PROF/CPU%d/HM/PokeWaitFailed", i);
583 AssertRC(rc);
584 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatEntry, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
585 "Profiling of VMXR0RunGuestCode entry",
586 "/PROF/CPU%d/HM/StatEntry", i);
587 AssertRC(rc);
588 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatExit1, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
589 "Profiling of VMXR0RunGuestCode exit part 1",
590 "/PROF/CPU%d/HM/SwitchFromGC_1", i);
591 AssertRC(rc);
592 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatExit2, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
593 "Profiling of VMXR0RunGuestCode exit part 2",
594 "/PROF/CPU%d/HM/SwitchFromGC_2", i);
595 AssertRC(rc);
596
597 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatExitIO, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
598 "I/O",
599 "/PROF/CPU%d/HM/SwitchFromGC_2/IO", i);
600 AssertRC(rc);
601 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatExitMovCRx, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
602 "MOV CRx",
603 "/PROF/CPU%d/HM/SwitchFromGC_2/MovCRx", i);
604 AssertRC(rc);
605 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatExitXcptNmi, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
606 "Exceptions, NMIs",
607 "/PROF/CPU%d/HM/SwitchFromGC_2/XcptNmi", i);
608 AssertRC(rc);
609
610 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatLoadGuestState, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
611 "Profiling of VMXR0LoadGuestState",
612 "/PROF/CPU%d/HM/StatLoadGuestState", i);
613 AssertRC(rc);
614 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatInGC, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
615 "Profiling of VMLAUNCH/VMRESUME.",
616 "/PROF/CPU%d/HM/InGC", i);
617 AssertRC(rc);
618
619# if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
620 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatWorldSwitch3264, STAMTYPE_PROFILE, STAMVISIBILITY_USED,
621 STAMUNIT_TICKS_PER_CALL, "Profiling of the 32/64 switcher.",
622 "/PROF/CPU%d/HM/Switcher3264", i);
623 AssertRC(rc);
624# endif
625
626# ifdef HM_PROFILE_EXIT_DISPATCH
627 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatExitDispatch, STAMTYPE_PROFILE_ADV, STAMVISIBILITY_USED,
628 STAMUNIT_TICKS_PER_CALL, "Profiling the dispatching of exit handlers.",
629 "/PROF/CPU%d/HM/ExitDispatch", i);
630 AssertRC(rc);
631# endif
632
633# define HM_REG_COUNTER(a, b, desc) \
634 rc = STAMR3RegisterF(pVM, a, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, desc, b, i); \
635 AssertRC(rc);
636
637 HM_REG_COUNTER(&pVCpu->hm.s.StatExitShadowNM, "/HM/CPU%d/Exit/Trap/Shw/#NM", "Shadow #NM (device not available, no math co-processor) exception.");
638 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestNM, "/HM/CPU%d/Exit/Trap/Gst/#NM", "Guest #NM (device not available, no math co-processor) exception.");
639 HM_REG_COUNTER(&pVCpu->hm.s.StatExitShadowPF, "/HM/CPU%d/Exit/Trap/Shw/#PF", "Shadow #PF (page fault) exception.");
640 HM_REG_COUNTER(&pVCpu->hm.s.StatExitShadowPFEM, "/HM/CPU%d/Exit/Trap/Shw/#PF-EM", "#PF (page fault) exception going back to ring-3 for emulating the instruction.");
641 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestPF, "/HM/CPU%d/Exit/Trap/Gst/#PF", "Guest #PF (page fault) exception.");
642 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestUD, "/HM/CPU%d/Exit/Trap/Gst/#UD", "Guest #UD (undefined opcode) exception.");
643 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestSS, "/HM/CPU%d/Exit/Trap/Gst/#SS", "Guest #SS (stack-segment fault) exception.");
644 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestNP, "/HM/CPU%d/Exit/Trap/Gst/#NP", "Guest #NP (segment not present) exception.");
645 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestGP, "/HM/CPU%d/Exit/Trap/Gst/#GP", "Guest #GP (general protection) execption.");
646 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestMF, "/HM/CPU%d/Exit/Trap/Gst/#MF", "Guest #MF (x87 FPU error, math fault) exception.");
647 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestDE, "/HM/CPU%d/Exit/Trap/Gst/#DE", "Guest #DE (divide error) exception.");
648 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestDB, "/HM/CPU%d/Exit/Trap/Gst/#DB", "Guest #DB (debug) exception.");
649 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestBP, "/HM/CPU%d/Exit/Trap/Gst/#BP", "Guest #BP (breakpoint) exception.");
650 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestXF, "/HM/CPU%d/Exit/Trap/Gst/#XF", "Guest #XF (extended math fault, SIMD FPU) exception.");
651 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestXcpUnk, "/HM/CPU%d/Exit/Trap/Gst/Other", "Other guest exceptions.");
652 HM_REG_COUNTER(&pVCpu->hm.s.StatExitInvlpg, "/HM/CPU%d/Exit/Instr/Invlpg", "Guest attempted to execute INVLPG.");
653 HM_REG_COUNTER(&pVCpu->hm.s.StatExitInvd, "/HM/CPU%d/Exit/Instr/Invd", "Guest attempted to execute INVD.");
654 HM_REG_COUNTER(&pVCpu->hm.s.StatExitWbinvd, "/HM/CPU%d/Exit/Instr/Wbinvd", "Guest attempted to execute WBINVD.");
655 HM_REG_COUNTER(&pVCpu->hm.s.StatExitPause, "/HM/CPU%d/Exit/Instr/Pause", "Guest attempted to execute PAUSE.");
656 HM_REG_COUNTER(&pVCpu->hm.s.StatExitCpuid, "/HM/CPU%d/Exit/Instr/Cpuid", "Guest attempted to execute CPUID.");
657 HM_REG_COUNTER(&pVCpu->hm.s.StatExitRdtsc, "/HM/CPU%d/Exit/Instr/Rdtsc", "Guest attempted to execute RDTSC.");
658 HM_REG_COUNTER(&pVCpu->hm.s.StatExitRdtscp, "/HM/CPU%d/Exit/Instr/Rdtscp", "Guest attempted to execute RDTSCP.");
659 HM_REG_COUNTER(&pVCpu->hm.s.StatExitRdpmc, "/HM/CPU%d/Exit/Instr/Rdpmc", "Guest attempted to execute RDPMC.");
660 HM_REG_COUNTER(&pVCpu->hm.s.StatExitRdrand, "/HM/CPU%d/Exit/Instr/Rdrand", "Guest attempted to execute RDRAND.");
661 HM_REG_COUNTER(&pVCpu->hm.s.StatExitRdmsr, "/HM/CPU%d/Exit/Instr/Rdmsr", "Guest attempted to execute RDMSR.");
662 HM_REG_COUNTER(&pVCpu->hm.s.StatExitWrmsr, "/HM/CPU%d/Exit/Instr/Wrmsr", "Guest attempted to execute WRMSR.");
663 HM_REG_COUNTER(&pVCpu->hm.s.StatExitMwait, "/HM/CPU%d/Exit/Instr/Mwait", "Guest attempted to execute MWAIT.");
664 HM_REG_COUNTER(&pVCpu->hm.s.StatExitMonitor, "/HM/CPU%d/Exit/Instr/Monitor", "Guest attempted to execute MONITOR.");
665 HM_REG_COUNTER(&pVCpu->hm.s.StatExitDRxWrite, "/HM/CPU%d/Exit/Instr/DR/Write", "Guest attempted to write a debug register.");
666 HM_REG_COUNTER(&pVCpu->hm.s.StatExitDRxRead, "/HM/CPU%d/Exit/Instr/DR/Read", "Guest attempted to read a debug register.");
667 HM_REG_COUNTER(&pVCpu->hm.s.StatExitClts, "/HM/CPU%d/Exit/Instr/CLTS", "Guest attempted to execute CLTS.");
668 HM_REG_COUNTER(&pVCpu->hm.s.StatExitLmsw, "/HM/CPU%d/Exit/Instr/LMSW", "Guest attempted to execute LMSW.");
669 HM_REG_COUNTER(&pVCpu->hm.s.StatExitCli, "/HM/CPU%d/Exit/Instr/Cli", "Guest attempted to execute CLI.");
670 HM_REG_COUNTER(&pVCpu->hm.s.StatExitSti, "/HM/CPU%d/Exit/Instr/Sti", "Guest attempted to execute STI.");
671 HM_REG_COUNTER(&pVCpu->hm.s.StatExitPushf, "/HM/CPU%d/Exit/Instr/Pushf", "Guest attempted to execute PUSHF.");
672 HM_REG_COUNTER(&pVCpu->hm.s.StatExitPopf, "/HM/CPU%d/Exit/Instr/Popf", "Guest attempted to execute POPF.");
673 HM_REG_COUNTER(&pVCpu->hm.s.StatExitIret, "/HM/CPU%d/Exit/Instr/Iret", "Guest attempted to execute IRET.");
674 HM_REG_COUNTER(&pVCpu->hm.s.StatExitInt, "/HM/CPU%d/Exit/Instr/Int", "Guest attempted to execute INT.");
675 HM_REG_COUNTER(&pVCpu->hm.s.StatExitHlt, "/HM/CPU%d/Exit/Instr/Hlt", "Guest attempted to execute HLT.");
676 HM_REG_COUNTER(&pVCpu->hm.s.StatExitXdtrAccess, "/HM/CPU%d/Exit/Instr/XdtrAccess", "Guest attempted to access descriptor table register (GDTR, IDTR, LDTR).");
677 HM_REG_COUNTER(&pVCpu->hm.s.StatExitIOWrite, "/HM/CPU%d/Exit/IO/Write", "I/O write.");
678 HM_REG_COUNTER(&pVCpu->hm.s.StatExitIORead, "/HM/CPU%d/Exit/IO/Read", "I/O read.");
679 HM_REG_COUNTER(&pVCpu->hm.s.StatExitIOStringWrite, "/HM/CPU%d/Exit/IO/WriteString", "String I/O write.");
680 HM_REG_COUNTER(&pVCpu->hm.s.StatExitIOStringRead, "/HM/CPU%d/Exit/IO/ReadString", "String I/O read.");
681 HM_REG_COUNTER(&pVCpu->hm.s.StatExitIntWindow, "/HM/CPU%d/Exit/IntWindow", "Interrupt-window exit. Guest is ready to receive interrupts again.");
682 HM_REG_COUNTER(&pVCpu->hm.s.StatExitMaxResume, "/HM/CPU%d/Exit/MaxResume", "Maximum VMRESUME inner-loop counter reached.");
683 HM_REG_COUNTER(&pVCpu->hm.s.StatExitExtInt, "/HM/CPU%d/Exit/ExtInt", "Host interrupt received.");
684 HM_REG_COUNTER(&pVCpu->hm.s.StatExitHostNmi, "/HM/CPU%d/Exit/HostNmi", "Host NMI received.");
685 HM_REG_COUNTER(&pVCpu->hm.s.StatExitPreemptTimer, "/HM/CPU%d/Exit/PreemptTimer", "VMX-preemption timer expired.");
686 HM_REG_COUNTER(&pVCpu->hm.s.StatExitTprBelowThreshold, "/HM/CPU%d/Exit/TprBelowThreshold", "TPR lowered below threshold by the guest.");
687 HM_REG_COUNTER(&pVCpu->hm.s.StatExitTaskSwitch, "/HM/CPU%d/Exit/TaskSwitch", "Guest attempted a task switch.");
688 HM_REG_COUNTER(&pVCpu->hm.s.StatExitMtf, "/HM/CPU%d/Exit/MonitorTrapFlag", "Monitor Trap Flag.");
689 HM_REG_COUNTER(&pVCpu->hm.s.StatExitApicAccess, "/HM/CPU%d/Exit/ApicAccess", "APIC access. Guest attempted to access memory at a physical address on the APIC-access page.");
690
691 HM_REG_COUNTER(&pVCpu->hm.s.StatSwitchGuestIrq, "/HM/CPU%d/Switch/IrqPending", "PDMGetInterrupt() cleared behind our back!?!.");
692 HM_REG_COUNTER(&pVCpu->hm.s.StatPendingHostIrq, "/HM/CPU%d/Switch/PendingHostIrq", "Exit to ring-3 due to pending host interrupt before executing guest code.");
693 HM_REG_COUNTER(&pVCpu->hm.s.StatSwitchHmToR3FF, "/HM/CPU%d/Switch/HmToR3FF", "Exit to ring-3 due to pending timers, EMT rendezvous, critical section etc.");
694 HM_REG_COUNTER(&pVCpu->hm.s.StatSwitchExitToR3, "/HM/CPU%d/Switch/ExitToR3", "Exit to ring-3 (total).");
695 HM_REG_COUNTER(&pVCpu->hm.s.StatSwitchLongJmpToR3, "/HM/CPU%d/Switch/LongJmpToR3", "Longjump to ring-3.");
696
697 HM_REG_COUNTER(&pVCpu->hm.s.StatInjectInterrupt, "/HM/CPU%d/EventInject/Interrupt", "Injected an external interrupt into the guest.");
698 HM_REG_COUNTER(&pVCpu->hm.s.StatInjectXcpt, "/HM/CPU%d/EventInject/Trap", "Injected an exception into the guest.");
699 HM_REG_COUNTER(&pVCpu->hm.s.StatInjectPendingReflect, "/HM/CPU%d/EventInject/PendingReflect", "Reflecting an exception back to the guest.");
700
701 HM_REG_COUNTER(&pVCpu->hm.s.StatFlushPage, "/HM/CPU%d/Flush/Page", "Invalidating a guest page on all guest CPUs.");
702 HM_REG_COUNTER(&pVCpu->hm.s.StatFlushPageManual, "/HM/CPU%d/Flush/Page/Virt", "Invalidating a guest page using guest-virtual address.");
703 HM_REG_COUNTER(&pVCpu->hm.s.StatFlushPhysPageManual, "/HM/CPU%d/Flush/Page/Phys", "Invalidating a guest page using guest-physical address.");
704 HM_REG_COUNTER(&pVCpu->hm.s.StatFlushTlb, "/HM/CPU%d/Flush/TLB", "Forcing a full guest-TLB flush (ring-0).");
705 HM_REG_COUNTER(&pVCpu->hm.s.StatFlushTlbManual, "/HM/CPU%d/Flush/TLB/Manual", "Request a full guest-TLB flush.");
706 HM_REG_COUNTER(&pVCpu->hm.s.StatFlushTlbWorldSwitch, "/HM/CPU%d/Flush/TLB/CpuSwitch", "Forcing a full guest-TLB flush due to host-CPU reschedule or ASID-limit hit by another guest-VCPU.");
707 HM_REG_COUNTER(&pVCpu->hm.s.StatNoFlushTlbWorldSwitch, "/HM/CPU%d/Flush/TLB/Skipped", "No TLB flushing required.");
708 HM_REG_COUNTER(&pVCpu->hm.s.StatFlushEntire, "/HM/CPU%d/Flush/TLB/Entire", "Flush the entire TLB (host + guest).");
709 HM_REG_COUNTER(&pVCpu->hm.s.StatFlushAsid, "/HM/CPU%d/Flush/TLB/ASID", "Flushed guest-TLB entries for the current VPID.");
710 HM_REG_COUNTER(&pVCpu->hm.s.StatFlushNestedPaging, "/HM/CPU%d/Flush/TLB/NestedPaging", "Flushed guest-TLB entries for the current EPT.");
711 HM_REG_COUNTER(&pVCpu->hm.s.StatFlushTlbInvlpgVirt, "/HM/CPU%d/Flush/TLB/InvlpgVirt", "Invalidated a guest-TLB entry for a guest-virtual address.");
712 HM_REG_COUNTER(&pVCpu->hm.s.StatFlushTlbInvlpgPhys, "/HM/CPU%d/Flush/TLB/InvlpgPhys", "Currently not possible, flushes entire guest-TLB.");
713 HM_REG_COUNTER(&pVCpu->hm.s.StatTlbShootdown, "/HM/CPU%d/Flush/Shootdown/Page", "Inter-VCPU request to flush queued guest page.");
714 HM_REG_COUNTER(&pVCpu->hm.s.StatTlbShootdownFlush, "/HM/CPU%d/Flush/Shootdown/TLB", "Inter-VCPU request to flush entire guest-TLB.");
715
716 HM_REG_COUNTER(&pVCpu->hm.s.StatTscOffset, "/HM/CPU%d/TSC/Offset", "TSC offsetting is in effect.");
717 HM_REG_COUNTER(&pVCpu->hm.s.StatTscIntercept, "/HM/CPU%d/TSC/Intercept", "Guest is in catchup mode, intercept TSC accesses.");
718 HM_REG_COUNTER(&pVCpu->hm.s.StatTscInterceptOverFlow, "/HM/CPU%d/TSC/InterceptOverflow", "TSC offset overflow, fallback to intercept TSC accesses.");
719
720 HM_REG_COUNTER(&pVCpu->hm.s.StatDRxArmed, "/HM/CPU%d/Debug/Armed", "Loaded guest-debug state while loading guest-state.");
721 HM_REG_COUNTER(&pVCpu->hm.s.StatDRxContextSwitch, "/HM/CPU%d/Debug/ContextSwitch", "Loaded guest-debug state on MOV DRx.");
722 HM_REG_COUNTER(&pVCpu->hm.s.StatDRxIoCheck, "/HM/CPU%d/Debug/IOCheck", "Checking for I/O breakpoint.");
723
724 HM_REG_COUNTER(&pVCpu->hm.s.StatLoadMinimal, "/HM/CPU%d/Load/Minimal", "VM-entry loading minimal guest-state.");
725 HM_REG_COUNTER(&pVCpu->hm.s.StatLoadFull, "/HM/CPU%d/Load/Full", "VM-entry loading the full guest-state.");
726
727 HM_REG_COUNTER(&pVCpu->hm.s.StatVmxCheckBadRmSelBase, "/HM/CPU%d/VMXCheck/RMSelBase", "Could not use VMX due to unsuitable real-mode selector base.");
728 HM_REG_COUNTER(&pVCpu->hm.s.StatVmxCheckBadRmSelLimit, "/HM/CPU%d/VMXCheck/RMSelLimit", "Could not use VMX due to unsuitable real-mode selector limit.");
729 HM_REG_COUNTER(&pVCpu->hm.s.StatVmxCheckRmOk, "/HM/CPU%d/VMXCheck/VMX_RM", "VMX execution in real (V86) mode OK.");
730 HM_REG_COUNTER(&pVCpu->hm.s.StatVmxCheckBadSel, "/HM/CPU%d/VMXCheck/Selector", "Could not use VMX due to unsuitable selector.");
731 HM_REG_COUNTER(&pVCpu->hm.s.StatVmxCheckBadRpl, "/HM/CPU%d/VMXCheck/RPL", "Could not use VMX due to unsuitable RPL.");
732 HM_REG_COUNTER(&pVCpu->hm.s.StatVmxCheckBadLdt, "/HM/CPU%d/VMXCheck/LDT", "Could not use VMX due to unsuitable LDT.");
733 HM_REG_COUNTER(&pVCpu->hm.s.StatVmxCheckBadTr, "/HM/CPU%d/VMXCheck/TR", "Could not use VMX due to unsuitable TR.");
734 HM_REG_COUNTER(&pVCpu->hm.s.StatVmxCheckPmOk, "/HM/CPU%d/VMXCheck/VMX_PM", "VMX execution in protected mode OK.");
735
736#if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
737 HM_REG_COUNTER(&pVCpu->hm.s.StatFpu64SwitchBack, "/HM/CPU%d/Switch64/Fpu", "Saving guest FPU/XMM state.");
738 HM_REG_COUNTER(&pVCpu->hm.s.StatDebug64SwitchBack, "/HM/CPU%d/Switch64/Debug", "Saving guest debug state.");
739#endif
740
741 for (unsigned j = 0; j < RT_ELEMENTS(pVCpu->hm.s.StatExitCRxWrite); j++)
742 {
743 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatExitCRxWrite[j], STAMTYPE_COUNTER, STAMVISIBILITY_USED,
744 STAMUNIT_OCCURENCES, "Profiling of CRx writes",
745 "/HM/CPU%d/Exit/Instr/CR/Write/%x", i, j);
746 AssertRC(rc);
747 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatExitCRxRead[j], STAMTYPE_COUNTER, STAMVISIBILITY_USED,
748 STAMUNIT_OCCURENCES, "Profiling of CRx reads",
749 "/HM/CPU%d/Exit/Instr/CR/Read/%x", i, j);
750 AssertRC(rc);
751 }
752
753#undef HM_REG_COUNTER
754
755 pVCpu->hm.s.paStatExitReason = NULL;
756
757 rc = MMHyperAlloc(pVM, MAX_EXITREASON_STAT*sizeof(*pVCpu->hm.s.paStatExitReason), 0, MM_TAG_HM,
758 (void **)&pVCpu->hm.s.paStatExitReason);
759 AssertRC(rc);
760 if (RT_SUCCESS(rc))
761 {
762 const char * const *papszDesc = ASMIsIntelCpu() ? &g_apszVTxExitReasons[0] : &g_apszAmdVExitReasons[0];
763 for (int j = 0; j < MAX_EXITREASON_STAT; j++)
764 {
765 if (papszDesc[j])
766 {
767 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.paStatExitReason[j], STAMTYPE_COUNTER, STAMVISIBILITY_USED,
768 STAMUNIT_OCCURENCES, papszDesc[j], "/HM/CPU%d/Exit/Reason/%02x", i, j);
769 AssertRC(rc);
770 }
771 }
772 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatExitReasonNpf, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_OCCURENCES,
773 "Nested page fault", "/HM/CPU%d/Exit/Reason/#NPF", i);
774 AssertRC(rc);
775 }
776 pVCpu->hm.s.paStatExitReasonR0 = MMHyperR3ToR0(pVM, pVCpu->hm.s.paStatExitReason);
777# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
778 Assert(pVCpu->hm.s.paStatExitReasonR0 != NIL_RTR0PTR || !HMIsEnabled(pVM));
779# else
780 Assert(pVCpu->hm.s.paStatExitReasonR0 != NIL_RTR0PTR);
781# endif
782
783 rc = MMHyperAlloc(pVM, sizeof(STAMCOUNTER) * 256, 8, MM_TAG_HM, (void **)&pVCpu->hm.s.paStatInjectedIrqs);
784 AssertRCReturn(rc, rc);
785 pVCpu->hm.s.paStatInjectedIrqsR0 = MMHyperR3ToR0(pVM, pVCpu->hm.s.paStatInjectedIrqs);
786# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
787 Assert(pVCpu->hm.s.paStatInjectedIrqsR0 != NIL_RTR0PTR || !HMIsEnabled(pVM));
788# else
789 Assert(pVCpu->hm.s.paStatInjectedIrqsR0 != NIL_RTR0PTR);
790# endif
791 for (unsigned j = 0; j < 255; j++)
792 {
793 STAMR3RegisterF(pVM, &pVCpu->hm.s.paStatInjectedIrqs[j], STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_OCCURENCES,
794 "Injected event.",
795 (j < 0x20) ? "/HM/CPU%d/EventInject/Event/Trap/%02X" : "/HM/CPU%d/EventInject/Event/IRQ/%02X", i, j);
796 }
797
798 }
799#endif /* VBOX_WITH_STATISTICS */
800
801#ifdef VBOX_WITH_CRASHDUMP_MAGIC
802 /*
803 * Magic marker for searching in crash dumps.
804 */
805 for (VMCPUID i = 0; i < pVM->cCpus; i++)
806 {
807 PVMCPU pVCpu = &pVM->aCpus[i];
808
809 PVMCSCACHE pCache = &pVCpu->hm.s.vmx.VMCSCache;
810 strcpy((char *)pCache->aMagic, "VMCSCACHE Magic");
811 pCache->uMagic = UINT64_C(0xDEADBEEFDEADBEEF);
812 }
813#endif
814
815 return VINF_SUCCESS;
816}
817
818
819/**
820 * Called when a init phase has completed.
821 *
822 * @returns VBox status code.
823 * @param pVM The VM.
824 * @param enmWhat The phase that completed.
825 */
826VMMR3_INT_DECL(int) HMR3InitCompleted(PVM pVM, VMINITCOMPLETED enmWhat)
827{
828 switch (enmWhat)
829 {
830 case VMINITCOMPLETED_RING3:
831 return hmR3InitCPU(pVM);
832 case VMINITCOMPLETED_RING0:
833 return hmR3InitFinalizeR0(pVM);
834 default:
835 return VINF_SUCCESS;
836 }
837}
838
839
840/**
841 * Turns off normal raw mode features.
842 *
843 * @param pVM Pointer to the VM.
844 */
845static void hmR3DisableRawMode(PVM pVM)
846{
847 /* Reinit the paging mode to force the new shadow mode. */
848 for (VMCPUID i = 0; i < pVM->cCpus; i++)
849 {
850 PVMCPU pVCpu = &pVM->aCpus[i];
851
852 PGMR3ChangeMode(pVM, pVCpu, PGMMODE_REAL);
853 }
854}
855
856
857/**
858 * Initialize VT-x or AMD-V.
859 *
860 * @returns VBox status code.
861 * @param pVM Pointer to the VM.
862 */
863static int hmR3InitFinalizeR0(PVM pVM)
864{
865 int rc;
866
867 if (!HMIsEnabled(pVM))
868 return VINF_SUCCESS;
869
870 /*
871 * Hack to allow users to work around broken BIOSes that incorrectly set
872 * EFER.SVME, which makes us believe somebody else is already using AMD-V.
873 */
874 if ( !pVM->hm.s.vmx.fSupported
875 && !pVM->hm.s.svm.fSupported
876 && pVM->hm.s.lLastError == VERR_SVM_IN_USE /* implies functional AMD-V */
877 && RTEnvExist("VBOX_HWVIRTEX_IGNORE_SVM_IN_USE"))
878 {
879 LogRel(("HM: VBOX_HWVIRTEX_IGNORE_SVM_IN_USE active!\n"));
880 pVM->hm.s.svm.fSupported = true;
881 pVM->hm.s.svm.fIgnoreInUseError = true;
882 pVM->hm.s.lLastError = VINF_SUCCESS;
883 }
884
885 /*
886 * Report ring-0 init errors.
887 */
888 if ( !pVM->hm.s.vmx.fSupported
889 && !pVM->hm.s.svm.fSupported)
890 {
891 LogRel(("HM: Failed to initialize VT-x / AMD-V: %Rrc\n", pVM->hm.s.lLastError));
892 LogRel(("HM: VMX MSR_IA32_FEATURE_CONTROL=%RX64\n", pVM->hm.s.vmx.msr.feature_ctrl));
893 switch (pVM->hm.s.lLastError)
894 {
895 case VERR_VMX_IN_VMX_ROOT_MODE:
896 return VM_SET_ERROR(pVM, VERR_VMX_IN_VMX_ROOT_MODE, "VT-x is being used by another hypervisor.");
897 case VERR_VMX_NO_VMX:
898 return VM_SET_ERROR(pVM, VERR_VMX_NO_VMX, "VT-x is not available.");
899 case VERR_VMX_MSR_LOCKED_OR_DISABLED:
900 return VM_SET_ERROR(pVM, VERR_VMX_NO_VMX, "VT-x is disabled in the BIOS (or by the host OS).");
901
902 case VERR_SVM_IN_USE:
903 return VM_SET_ERROR(pVM, VERR_SVM_IN_USE, "AMD-V is being used by another hypervisor.");
904 case VERR_SVM_NO_SVM:
905 return VM_SET_ERROR(pVM, VERR_SVM_NO_SVM, "AMD-V is not available.");
906 case VERR_SVM_DISABLED:
907 return VM_SET_ERROR(pVM, VERR_SVM_DISABLED, "AMD-V is disabled in the BIOS.");
908 }
909 return VMSetError(pVM, pVM->hm.s.lLastError, RT_SRC_POS, "HM ring-0 init failed: %Rrc", pVM->hm.s.lLastError);
910 }
911
912 /*
913 * Enable VT-x or AMD-V on all host CPUs.
914 */
915 rc = SUPR3CallVMMR0Ex(pVM->pVMR0, 0 /*idCpu*/, VMMR0_DO_HM_ENABLE, 0, NULL);
916 if (RT_FAILURE(rc))
917 {
918 LogRel(("HMR3InitFinalize: SUPR3CallVMMR0Ex VMMR0_DO_HM_ENABLE failed with %Rrc\n", rc));
919 return rc;
920 }
921
922 /*
923 * No TPR patching is required when the IO-APIC is not enabled for this VM.
924 * (Main should have taken care of this already)
925 */
926 pVM->hm.s.fHasIoApic = PDMHasIoApic(pVM);
927 if (!pVM->hm.s.fHasIoApic)
928 {
929 Assert(!pVM->hm.s.fTRPPatchingAllowed); /* paranoia */
930 pVM->hm.s.fTRPPatchingAllowed = false;
931 }
932
933 /*
934 * Do the vendor specific initalization .
935 * .
936 * Note! We disable release log buffering here since we're doing relatively .
937 * lot of logging and doesn't want to hit the disk with each LogRel .
938 * statement.
939 */
940 AssertLogRelReturn(!pVM->hm.s.fInitialized, VERR_HM_IPE_5);
941 bool fOldBuffered = RTLogRelSetBuffering(true /*fBuffered*/);
942 if (pVM->hm.s.vmx.fSupported)
943 rc = hmR3InitFinalizeR0Intel(pVM);
944 else
945 rc = hmR3InitFinalizeR0Amd(pVM);
946 LogRel(("HM: VT-x/AMD-V init method: %s\n", (pVM->hm.s.fGlobalInit) ? "GLOBAL" : "LOCAL"));
947 RTLogRelSetBuffering(fOldBuffered);
948 pVM->hm.s.fInitialized = true;
949
950 return rc;
951}
952
953
954/**
955 * Finish VT-x initialization (after ring-0 init).
956 *
957 * @returns VBox status code.
958 * @param pVM The cross context VM structure.
959 */
960static int hmR3InitFinalizeR0Intel(PVM pVM)
961{
962 int rc;
963
964 Log(("pVM->hm.s.vmx.fSupported = %d\n", pVM->hm.s.vmx.fSupported));
965 AssertLogRelReturn(pVM->hm.s.vmx.msr.feature_ctrl != 0, VERR_HM_IPE_4);
966
967 uint64_t val;
968 uint64_t zap;
969 RTGCPHYS GCPhys = 0;
970
971#ifndef VBOX_WITH_OLD_VTX_CODE
972 LogRel(("HM: Using VT-x implementation 2.0!\n"));
973#endif
974 LogRel(("HM: Host CR4 = %08X\n", pVM->hm.s.vmx.hostCR4));
975 LogRel(("HM: MSR_IA32_FEATURE_CONTROL = %RX64\n", pVM->hm.s.vmx.msr.feature_ctrl));
976 LogRel(("HM: MSR_IA32_VMX_BASIC_INFO = %RX64\n", pVM->hm.s.vmx.msr.vmx_basic_info));
977 LogRel(("HM: VMCS id = %x\n", MSR_IA32_VMX_BASIC_INFO_VMCS_ID(pVM->hm.s.vmx.msr.vmx_basic_info)));
978 LogRel(("HM: VMCS size = %x\n", MSR_IA32_VMX_BASIC_INFO_VMCS_SIZE(pVM->hm.s.vmx.msr.vmx_basic_info)));
979 LogRel(("HM: VMCS physical address limit = %s\n", MSR_IA32_VMX_BASIC_INFO_VMCS_PHYS_WIDTH(pVM->hm.s.vmx.msr.vmx_basic_info) ? "< 4 GB" : "None"));
980 LogRel(("HM: VMCS memory type = %x\n", MSR_IA32_VMX_BASIC_INFO_VMCS_MEM_TYPE(pVM->hm.s.vmx.msr.vmx_basic_info)));
981 LogRel(("HM: Dual-monitor treatment = %d\n", MSR_IA32_VMX_BASIC_INFO_VMCS_DUAL_MON(pVM->hm.s.vmx.msr.vmx_basic_info)));
982 LogRel(("HM: OUTS & INS instruction-info = %d\n", MSR_IA32_VMX_BASIC_INFO_VMCS_INS_OUTS(pVM->hm.s.vmx.msr.vmx_basic_info)));
983 LogRel(("HM: Max resume loops = %RX32\n", pVM->hm.s.cMaxResumeLoops));
984
985 LogRel(("HM: MSR_IA32_VMX_PINBASED_CTLS = %RX64\n", pVM->hm.s.vmx.msr.vmx_pin_ctls.u));
986 val = pVM->hm.s.vmx.msr.vmx_pin_ctls.n.allowed1;
987 zap = pVM->hm.s.vmx.msr.vmx_pin_ctls.n.disallowed0;
988 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PIN_EXEC_EXT_INT_EXIT);
989 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PIN_EXEC_NMI_EXIT);
990 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PIN_EXEC_VIRTUAL_NMI);
991 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PIN_EXEC_PREEMPT_TIMER);
992
993 LogRel(("HM: MSR_IA32_VMX_PROCBASED_CTLS = %RX64\n", pVM->hm.s.vmx.msr.vmx_proc_ctls.u));
994 val = pVM->hm.s.vmx.msr.vmx_proc_ctls.n.allowed1;
995 zap = pVM->hm.s.vmx.msr.vmx_proc_ctls.n.disallowed0;
996 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC_INT_WINDOW_EXIT);
997 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC_USE_TSC_OFFSETTING);
998 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC_HLT_EXIT);
999 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC_INVLPG_EXIT);
1000 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC_MWAIT_EXIT);
1001 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC_RDPMC_EXIT);
1002 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC_RDTSC_EXIT);
1003 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC_CR3_LOAD_EXIT);
1004 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC_CR3_STORE_EXIT);
1005 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC_CR8_LOAD_EXIT);
1006 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC_CR8_STORE_EXIT);
1007 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC_USE_TPR_SHADOW);
1008 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC_NMI_WINDOW_EXIT);
1009 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC_MOV_DR_EXIT);
1010 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC_UNCOND_IO_EXIT);
1011 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC_USE_IO_BITMAPS);
1012 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC_MONITOR_TRAP_FLAG);
1013 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC_USE_MSR_BITMAPS);
1014 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC_MONITOR_EXIT);
1015 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC_PAUSE_EXIT);
1016 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC_USE_SECONDARY_EXEC_CTRL);
1017 if (pVM->hm.s.vmx.msr.vmx_proc_ctls.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC_USE_SECONDARY_EXEC_CTRL)
1018 {
1019 LogRel(("HM: MSR_IA32_VMX_PROCBASED_CTLS2 = %RX64\n", pVM->hm.s.vmx.msr.vmx_proc_ctls2.u));
1020 val = pVM->hm.s.vmx.msr.vmx_proc_ctls2.n.allowed1;
1021 zap = pVM->hm.s.vmx.msr.vmx_proc_ctls2.n.disallowed0;
1022 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC2_VIRT_APIC);
1023 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC2_EPT);
1024 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC2_DESCRIPTOR_TABLE_EXIT);
1025 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC2_RDTSCP);
1026 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC2_VIRT_X2APIC);
1027 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC2_VPID);
1028 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC2_WBINVD_EXIT);
1029 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC2_UNRESTRICTED_GUEST);
1030 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC2_PAUSE_LOOP_EXIT);
1031 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC2_RDRAND_EXIT);
1032 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC2_INVPCID);
1033 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_PROC_EXEC2_VMFUNC);
1034 }
1035
1036 LogRel(("HM: MSR_IA32_VMX_ENTRY_CTLS = %RX64\n", pVM->hm.s.vmx.msr.vmx_entry.u));
1037 val = pVM->hm.s.vmx.msr.vmx_entry.n.allowed1;
1038 zap = pVM->hm.s.vmx.msr.vmx_entry.n.disallowed0;
1039 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_ENTRY_LOAD_DEBUG);
1040 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_ENTRY_IA32E_MODE_GUEST);
1041 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_ENTRY_ENTRY_SMM);
1042 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_ENTRY_DEACTIVATE_DUALMON);
1043 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_ENTRY_LOAD_GUEST_PERF_MSR);
1044 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_ENTRY_LOAD_GUEST_PAT_MSR);
1045 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_ENTRY_LOAD_GUEST_EFER_MSR);
1046
1047 LogRel(("HM: MSR_IA32_VMX_EXIT_CTLS = %RX64\n", pVM->hm.s.vmx.msr.vmx_exit.u));
1048 val = pVM->hm.s.vmx.msr.vmx_exit.n.allowed1;
1049 zap = pVM->hm.s.vmx.msr.vmx_exit.n.disallowed0;
1050 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_EXIT_SAVE_DEBUG);
1051 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_EXIT_HOST_ADDR_SPACE_SIZE);
1052 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_EXIT_LOAD_PERF_MSR);
1053 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_EXIT_ACK_EXT_INT);
1054 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_EXIT_SAVE_GUEST_PAT_MSR);
1055 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_EXIT_LOAD_HOST_PAT_MSR);
1056 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_EXIT_SAVE_GUEST_EFER_MSR);
1057 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_EXIT_LOAD_HOST_EFER_MSR);
1058 HMVMX_REPORT_FEATURE(val, zap, VMX_VMCS_CTRL_EXIT_SAVE_VMX_PREEMPT_TIMER);
1059
1060 if (pVM->hm.s.vmx.msr.vmx_ept_vpid_caps)
1061 {
1062 val = pVM->hm.s.vmx.msr.vmx_ept_vpid_caps;
1063 LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP = %RX64\n", val));
1064 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_RWX_X_ONLY);
1065 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_RWX_W_ONLY);
1066 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_RWX_WX_ONLY);
1067 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_GAW_21_BITS);
1068 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_GAW_30_BITS);
1069 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_GAW_39_BITS);
1070 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_GAW_48_BITS);
1071 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_GAW_57_BITS);
1072 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_EMT_UC);
1073 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_EMT_WC);
1074 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_EMT_WT);
1075 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_EMT_WP);
1076 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_EMT_WB);
1077 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_SP_21_BITS);
1078 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_SP_30_BITS);
1079 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_SP_39_BITS);
1080 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_SP_48_BITS);
1081 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_INVEPT);
1082 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_INVEPT_SINGLE_CONTEXT);
1083 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_INVEPT_ALL_CONTEXTS);
1084 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_INVVPID);
1085 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_INVVPID_INDIV_ADDR);
1086 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_INVVPID_SINGLE_CONTEXT);
1087 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_INVVPID_ALL_CONTEXTS);
1088 HMVMX_REPORT_CAPABILITY(val, MSR_IA32_VMX_EPT_VPID_CAP_INVVPID_SINGLE_CONTEXT_RETAIN_GLOBALS);
1089 }
1090
1091 LogRel(("HM: MSR_IA32_VMX_MISC = %RX64\n", pVM->hm.s.vmx.msr.vmx_misc));
1092 if (MSR_IA32_VMX_MISC_PREEMPT_TSC_BIT(pVM->hm.s.vmx.msr.vmx_misc) == pVM->hm.s.vmx.cPreemptTimerShift)
1093 {
1094 LogRel(("HM: MSR_IA32_VMX_MISC_PREEMPT_TSC_BIT = %x\n",
1095 MSR_IA32_VMX_MISC_PREEMPT_TSC_BIT(pVM->hm.s.vmx.msr.vmx_misc)));
1096 }
1097 else
1098 {
1099 LogRel(("HM: MSR_IA32_VMX_MISC_PREEMPT_TSC_BIT = %x - erratum detected, using %x instead\n",
1100 MSR_IA32_VMX_MISC_PREEMPT_TSC_BIT(pVM->hm.s.vmx.msr.vmx_misc), pVM->hm.s.vmx.cPreemptTimerShift));
1101 }
1102
1103 LogRel(("HM: MSR_IA32_VMX_MISC_STORE_EFERLMA_VMEXIT = %x\n", MSR_IA32_VMX_MISC_STORE_EFERLMA_VMEXIT(pVM->hm.s.vmx.msr.vmx_misc)));
1104 LogRel(("HM: MSR_IA32_VMX_MISC_ACTIVITY_STATES = %x\n", MSR_IA32_VMX_MISC_ACTIVITY_STATES(pVM->hm.s.vmx.msr.vmx_misc)));
1105 LogRel(("HM: MSR_IA32_VMX_MISC_CR3_TARGET = %x\n", MSR_IA32_VMX_MISC_CR3_TARGET(pVM->hm.s.vmx.msr.vmx_misc)));
1106 LogRel(("HM: MSR_IA32_VMX_MISC_MAX_MSR = %x\n", MSR_IA32_VMX_MISC_MAX_MSR(pVM->hm.s.vmx.msr.vmx_misc)));
1107 LogRel(("HM: MSR_IA32_VMX_MISC_RDMSR_SMBASE_MSR_SMM = %x\n", MSR_IA32_VMX_MISC_RDMSR_SMBASE_MSR_SMM(pVM->hm.s.vmx.msr.vmx_misc)));
1108 LogRel(("HM: MSR_IA32_VMX_MISC_SMM_MONITOR_CTL_B2 = %x\n", MSR_IA32_VMX_MISC_SMM_MONITOR_CTL_B2(pVM->hm.s.vmx.msr.vmx_misc)));
1109 LogRel(("HM: MSR_IA32_VMX_MISC_VMWRITE_VMEXIT_INFO = %x\n", MSR_IA32_VMX_MISC_VMWRITE_VMEXIT_INFO(pVM->hm.s.vmx.msr.vmx_misc)));
1110 LogRel(("HM: MSR_IA32_VMX_MISC_MSEG_ID = %x\n", MSR_IA32_VMX_MISC_MSEG_ID(pVM->hm.s.vmx.msr.vmx_misc)));
1111
1112 /* Paranoia */
1113 AssertRelease(MSR_IA32_VMX_MISC_MAX_MSR(pVM->hm.s.vmx.msr.vmx_misc) >= 512);
1114
1115 LogRel(("HM: MSR_IA32_VMX_CR0_FIXED0 = %RX64\n", pVM->hm.s.vmx.msr.vmx_cr0_fixed0));
1116 LogRel(("HM: MSR_IA32_VMX_CR0_FIXED1 = %RX64\n", pVM->hm.s.vmx.msr.vmx_cr0_fixed1));
1117 LogRel(("HM: MSR_IA32_VMX_CR4_FIXED0 = %RX64\n", pVM->hm.s.vmx.msr.vmx_cr4_fixed0));
1118 LogRel(("HM: MSR_IA32_VMX_CR4_FIXED1 = %RX64\n", pVM->hm.s.vmx.msr.vmx_cr4_fixed1));
1119 LogRel(("HM: MSR_IA32_VMX_VMCS_ENUM = %RX64\n", pVM->hm.s.vmx.msr.vmx_vmcs_enum));
1120 LogRel(("HM: MSR_IA32_VMX_VMCS_ENUM_HIGHEST_INDEX = %x\n", MSR_IA32_VMX_VMCS_ENUM_HIGHEST_INDEX(pVM->hm.s.vmx.msr.vmx_vmcs_enum)));
1121
1122 val = pVM->hm.s.vmx.msr.vmx_vmfunc;
1123 if (val)
1124 {
1125 LogRel(("HM: MSR_A32_VMX_VMFUNC = %RX64\n", val));
1126 HMVMX_REPORT_ALLOWED_FEATURE(val, VMX_VMCS_CTRL_VMFUNC_EPTP_SWITCHING);
1127 }
1128
1129 LogRel(("HM: APIC-access page physaddr = %RHp\n", pVM->hm.s.vmx.HCPhysApicAccess));
1130
1131 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1132 {
1133 LogRel(("HM: VCPU%3d: MSR bitmap physaddr = %RHp\n", i, pVM->aCpus[i].hm.s.vmx.HCPhysMsrBitmap));
1134 LogRel(("HM: VCPU%3d: VMCS physaddr = %RHp\n", i, pVM->aCpus[i].hm.s.vmx.HCPhysVmcs));
1135 }
1136
1137 if (pVM->hm.s.vmx.msr.vmx_proc_ctls2.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC2_EPT)
1138 pVM->hm.s.fNestedPaging = pVM->hm.s.fAllowNestedPaging;
1139
1140 if (pVM->hm.s.vmx.msr.vmx_proc_ctls2.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC2_VPID)
1141 pVM->hm.s.vmx.fVpid = pVM->hm.s.vmx.fAllowVpid;
1142
1143 /*
1144 * Disallow RDTSCP in the guest if there is no secondary process-based VM execution controls as otherwise
1145 * RDTSCP would cause a #UD. There might be no CPUs out there where this happens, as RDTSCP was introduced
1146 * in Nehalems and secondary VM exec. controls should be supported in all of them, but nonetheless it's Intel...
1147 */
1148 if ( !(pVM->hm.s.vmx.msr.vmx_proc_ctls.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC_USE_SECONDARY_EXEC_CTRL)
1149 && CPUMGetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_RDTSCP))
1150 {
1151 CPUMClearGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_RDTSCP);
1152 LogRel(("HM: RDTSCP disabled.\n"));
1153 }
1154
1155 /* Unrestricted guest execution also requires EPT. */
1156 if ( pVM->hm.s.vmx.fAllowUnrestricted
1157 && pVM->hm.s.fNestedPaging
1158 && (pVM->hm.s.vmx.msr.vmx_proc_ctls2.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC2_UNRESTRICTED_GUEST))
1159 {
1160 pVM->hm.s.vmx.fUnrestrictedGuest = true;
1161 }
1162
1163 if (!pVM->hm.s.vmx.fUnrestrictedGuest)
1164 {
1165 /* Allocate three pages for the TSS we need for real mode emulation. (2 pages for the IO bitmap) */
1166 rc = PDMR3VmmDevHeapAlloc(pVM, HM_VTX_TOTAL_DEVHEAP_MEM, (RTR3PTR *)&pVM->hm.s.vmx.pRealModeTSS);
1167 if (RT_SUCCESS(rc))
1168 {
1169 /* The IO bitmap starts right after the virtual interrupt redirection bitmap.
1170 Refer Intel spec. 20.3.3 "Software Interrupt Handling in Virtual-8086 mode"
1171 esp. Figure 20-5.*/
1172 ASMMemZero32(pVM->hm.s.vmx.pRealModeTSS, sizeof(*pVM->hm.s.vmx.pRealModeTSS));
1173 pVM->hm.s.vmx.pRealModeTSS->offIoBitmap = sizeof(*pVM->hm.s.vmx.pRealModeTSS);
1174
1175 /* Bit set to 0 means software interrupts are redirected to the
1176 8086 program interrupt handler rather than switching to
1177 protected-mode handler. */
1178 memset(pVM->hm.s.vmx.pRealModeTSS->IntRedirBitmap, 0, sizeof(pVM->hm.s.vmx.pRealModeTSS->IntRedirBitmap));
1179
1180 /* Allow all port IO, so that port IO instructions do not cause
1181 exceptions and would instead cause a VM-exit (based on VT-x's
1182 IO bitmap which we currently configure to always cause an exit). */
1183 memset(pVM->hm.s.vmx.pRealModeTSS + 1, 0, PAGE_SIZE * 2);
1184 *((unsigned char *)pVM->hm.s.vmx.pRealModeTSS + HM_VTX_TSS_SIZE - 2) = 0xff;
1185
1186 /*
1187 * Construct a 1024 element page directory with 4 MB pages for
1188 * the identity mapped page table used in real and protected mode
1189 * without paging with EPT.
1190 */
1191 pVM->hm.s.vmx.pNonPagingModeEPTPageTable = (PX86PD)((char *)pVM->hm.s.vmx.pRealModeTSS + PAGE_SIZE * 3);
1192 for (uint32_t i = 0; i < X86_PG_ENTRIES; i++)
1193 {
1194 pVM->hm.s.vmx.pNonPagingModeEPTPageTable->a[i].u = _4M * i;
1195 pVM->hm.s.vmx.pNonPagingModeEPTPageTable->a[i].u |= X86_PDE4M_P | X86_PDE4M_RW | X86_PDE4M_US
1196 | X86_PDE4M_A | X86_PDE4M_D | X86_PDE4M_PS
1197 | X86_PDE4M_G;
1198 }
1199
1200 /* We convert it here every time as pci regions could be reconfigured. */
1201 rc = PDMVmmDevHeapR3ToGCPhys(pVM, pVM->hm.s.vmx.pRealModeTSS, &GCPhys);
1202 AssertRCReturn(rc, rc);
1203 LogRel(("HM: Real Mode TSS guest physaddr = %RGp\n", GCPhys));
1204
1205 rc = PDMVmmDevHeapR3ToGCPhys(pVM, pVM->hm.s.vmx.pNonPagingModeEPTPageTable, &GCPhys);
1206 AssertRCReturn(rc, rc);
1207 LogRel(("HM: Non-Paging Mode EPT CR3 = %RGp\n", GCPhys));
1208 }
1209 else
1210 {
1211 /** @todo This cannot possibly work, there are other places which assumes
1212 * this allocation cannot fail (see HMR3CanExecuteGuest()). Make this
1213 * a failure case. */
1214 LogRel(("HM: No real mode VT-x support (PDMR3VMMDevHeapAlloc returned %Rrc)\n", rc));
1215 pVM->hm.s.vmx.pRealModeTSS = NULL;
1216 pVM->hm.s.vmx.pNonPagingModeEPTPageTable = NULL;
1217 }
1218 }
1219
1220 /*
1221 * Call ring-0 to set up the VM.
1222 */
1223 rc = SUPR3CallVMMR0Ex(pVM->pVMR0, 0 /*idCpu*/, VMMR0_DO_HM_SETUP_VM, 0, NULL);
1224 if (rc != VINF_SUCCESS)
1225 {
1226 AssertMsgFailed(("%Rrc\n", rc));
1227 LogRel(("HM: VMX setup failed with rc=%Rrc!\n", rc));
1228 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1229 LogRel(("HM: CPU[%ld] Last instruction error %x\n", i, pVM->aCpus[i].hm.s.vmx.LastError.u32InstrError));
1230 return VMSetError(pVM, rc, RT_SRC_POS, "VT-x setup failed: %Rrc", rc);
1231 }
1232
1233 LogRel(("HM: VMX enabled!\n"));
1234 pVM->hm.s.vmx.fEnabled = true;
1235
1236 hmR3DisableRawMode(pVM); /** @todo make this go away! */
1237
1238 /*
1239 * Change the CPU features.
1240 */
1241 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_SEP);
1242 if (pVM->hm.s.fAllow64BitGuests)
1243 {
1244 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_PAE);
1245 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_LONG_MODE);
1246 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_SYSCALL); /* 64 bits only on Intel CPUs */
1247 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_LAHF);
1248 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_NX);
1249#if 0 /** @todo r=bird: This ain't making any sense whatsoever. */
1250#if RT_ARCH_X86
1251 if ( !CPUMGetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_PAE)
1252 || !(pVM->hm.s.vmx.hostEFER & MSR_K6_EFER_NXE))
1253 LogRel(("NX is only supported for 64-bit guests!\n"));
1254#endif
1255#endif
1256 }
1257 /* Turn on NXE if PAE has been enabled *and* the host has turned on NXE
1258 (we reuse the host EFER in the switcher). */
1259 /** @todo this needs to be fixed properly!! */
1260 else if ( CPUMGetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_PAE)
1261 && (pVM->hm.s.vmx.hostEFER & MSR_K6_EFER_NXE))
1262 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_NX);
1263 else
1264 LogRel(("HM: NX not supported by the host.\n"));
1265
1266 /*
1267 * Log configuration details.
1268 */
1269 LogRel((pVM->hm.s.fAllow64BitGuests
1270 ? "HM: Guest support: 32-bit and 64-bit.\n"
1271 : "HM: Guest support: 32-bit only.\n"));
1272 if (pVM->hm.s.fNestedPaging)
1273 {
1274 LogRel(("HM: Nested paging enabled!\n"));
1275 LogRel(("HM: EPT root page physaddr = %RHp\n", PGMGetHyperCR3(VMMGetCpu(pVM))));
1276 if (pVM->hm.s.vmx.enmFlushEpt == VMX_FLUSH_EPT_SINGLE_CONTEXT)
1277 LogRel(("HM: EPT flush type = VMX_FLUSH_EPT_SINGLE_CONTEXT\n"));
1278 else if (pVM->hm.s.vmx.enmFlushEpt == VMX_FLUSH_EPT_ALL_CONTEXTS)
1279 LogRel(("HM: EPT flush type = VMX_FLUSH_EPT_ALL_CONTEXTS\n"));
1280 else if (pVM->hm.s.vmx.enmFlushEpt == VMX_FLUSH_EPT_NOT_SUPPORTED)
1281 LogRel(("HM: EPT flush type = VMX_FLUSH_EPT_NOT_SUPPORTED\n"));
1282 else
1283 LogRel(("HM: EPT flush type = %d\n", pVM->hm.s.vmx.enmFlushEpt));
1284
1285 if (pVM->hm.s.vmx.fUnrestrictedGuest)
1286 LogRel(("HM: Unrestricted guest execution enabled!\n"));
1287
1288#if HC_ARCH_BITS == 64
1289 if (pVM->hm.s.fLargePages)
1290 {
1291 /* Use large (2 MB) pages for our EPT PDEs where possible. */
1292 PGMSetLargePageUsage(pVM, true);
1293 LogRel(("HM: Large page support enabled!\n"));
1294 }
1295#endif
1296 }
1297 else
1298 Assert(!pVM->hm.s.vmx.fUnrestrictedGuest);
1299
1300 if (pVM->hm.s.vmx.fVpid)
1301 {
1302 LogRel(("HM: VPID enabled!\n"));
1303 if (pVM->hm.s.vmx.enmFlushVpid == VMX_FLUSH_VPID_INDIV_ADDR)
1304 LogRel(("HM: VPID flush type = VMX_FLUSH_VPID_INDIV_ADDR\n"));
1305 else if (pVM->hm.s.vmx.enmFlushVpid == VMX_FLUSH_VPID_SINGLE_CONTEXT)
1306 LogRel(("HM: VPID flush type = VMX_FLUSH_VPID_SINGLE_CONTEXT\n"));
1307 else if (pVM->hm.s.vmx.enmFlushVpid == VMX_FLUSH_VPID_ALL_CONTEXTS)
1308 LogRel(("HM: VPID flush type = VMX_FLUSH_VPID_ALL_CONTEXTS\n"));
1309 else if (pVM->hm.s.vmx.enmFlushVpid == VMX_FLUSH_VPID_SINGLE_CONTEXT_RETAIN_GLOBALS)
1310 LogRel(("HM: VPID flush type = VMX_FLUSH_VPID_SINGLE_CONTEXT_RETAIN_GLOBALS\n"));
1311 else
1312 LogRel(("HM: VPID flush type = %d\n", pVM->hm.s.vmx.enmFlushVpid));
1313 }
1314 else if (pVM->hm.s.vmx.enmFlushVpid == VMX_FLUSH_VPID_NOT_SUPPORTED)
1315 LogRel(("HM: Ignoring VPID capabilities of CPU.\n"));
1316
1317 /** TPR patching would never have worked on Intel. Leaving it here for the old
1318 * code's sake. See @bugref{6398}. */
1319#ifdef VBOX_WITH_OLD_VTX_CODE
1320 /*
1321 * TPR patching status logging.
1322 */
1323 if (pVM->hm.s.fTRPPatchingAllowed)
1324 {
1325 if ( (pVM->hm.s.vmx.msr.vmx_proc_ctls.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC_USE_SECONDARY_EXEC_CTRL)
1326 && (pVM->hm.s.vmx.msr.vmx_proc_ctls2.n.allowed1 & VMX_VMCS_CTRL_PROC_EXEC2_VIRT_APIC))
1327 {
1328 pVM->hm.s.fTRPPatchingAllowed = false; /* not necessary as we have a hardware solution. */
1329 LogRel(("HM: TPR Patching not required (VMX_VMCS_CTRL_PROC_EXEC2_VIRT_APIC).\n"));
1330 }
1331 else
1332 {
1333 uint32_t u32Eax, u32Dummy;
1334
1335 /* TPR patching needs access to the MSR_K8_LSTAR msr. */
1336 ASMCpuId(0x80000000, &u32Eax, &u32Dummy, &u32Dummy, &u32Dummy);
1337 if ( u32Eax < 0x80000001
1338 || !(ASMCpuId_EDX(0x80000001) & X86_CPUID_EXT_FEATURE_EDX_LONG_MODE))
1339 {
1340 pVM->hm.s.fTRPPatchingAllowed = false;
1341 LogRel(("HM: TPR patching disabled (long mode not supported).\n"));
1342 }
1343 }
1344 }
1345 LogRel(("HM: TPR Patching %s.\n", (pVM->hm.s.fTRPPatchingAllowed) ? "enabled" : "disabled"));
1346#endif
1347
1348
1349 /*
1350 * Check for preemption timer config override and log the state of it.
1351 */
1352 if (pVM->hm.s.vmx.fUsePreemptTimer)
1353 {
1354 PCFGMNODE pCfgHm = CFGMR3GetChild(CFGMR3GetRoot(pVM), "HM");
1355 rc = CFGMR3QueryBoolDef(pCfgHm, "UsePreemptTimer", &pVM->hm.s.vmx.fUsePreemptTimer, true);
1356 AssertLogRelRCReturn(rc, rc);
1357 }
1358 if (pVM->hm.s.vmx.fUsePreemptTimer)
1359 LogRel(("HM: VMX-preemption timer enabled (cPreemptTimerShift=%u).\n", pVM->hm.s.vmx.cPreemptTimerShift));
1360 else
1361 LogRel(("HM: VMX-preemption timer disabled.\n"));
1362
1363 return VINF_SUCCESS;
1364}
1365
1366
1367/**
1368 * Finish AMD-V initialization (after ring-0 init).
1369 *
1370 * @returns VBox status code.
1371 * @param pVM The cross context VM structure.
1372 */
1373static int hmR3InitFinalizeR0Amd(PVM pVM)
1374{
1375 Log(("pVM->hm.s.svm.fSupported = %d\n", pVM->hm.s.svm.fSupported));
1376
1377#ifndef VBOX_WITH_OLD_AMDV_CODE
1378 LogRel(("HM: Using AMD-V implementation 2.0!\n"));
1379#endif
1380
1381 uint32_t u32Family;
1382 uint32_t u32Model;
1383 uint32_t u32Stepping;
1384 if (HMAmdIsSubjectToErratum170(&u32Family, &u32Model, &u32Stepping))
1385 LogRel(("HM: AMD Cpu with erratum 170 family %#x model %#x stepping %#x\n", u32Family, u32Model, u32Stepping));
1386 LogRel(("HM: CPUID 0x80000001.u32AMDFeatureECX = %RX32\n", pVM->hm.s.cpuid.u32AMDFeatureECX));
1387 LogRel(("HM: CPUID 0x80000001.u32AMDFeatureEDX = %RX32\n", pVM->hm.s.cpuid.u32AMDFeatureEDX));
1388 LogRel(("HM: AMD HWCR MSR = %RX64\n", pVM->hm.s.svm.msrHwcr));
1389 LogRel(("HM: AMD-V revision = %X\n", pVM->hm.s.svm.u32Rev));
1390 LogRel(("HM: AMD-V max ASID = %RU32\n", pVM->hm.s.uMaxAsid));
1391 LogRel(("HM: AMD-V features = %X\n", pVM->hm.s.svm.u32Features));
1392
1393 /*
1394 * Enumerate AMD-V features.
1395 */
1396 static const struct { uint32_t fFlag; const char *pszName; } s_aSvmFeatures[] =
1397 {
1398#define HMSVM_REPORT_FEATURE(a_Define) { a_Define, #a_Define }
1399 HMSVM_REPORT_FEATURE(AMD_CPUID_SVM_FEATURE_EDX_NESTED_PAGING),
1400 HMSVM_REPORT_FEATURE(AMD_CPUID_SVM_FEATURE_EDX_LBR_VIRT),
1401 HMSVM_REPORT_FEATURE(AMD_CPUID_SVM_FEATURE_EDX_SVM_LOCK),
1402 HMSVM_REPORT_FEATURE(AMD_CPUID_SVM_FEATURE_EDX_NRIP_SAVE),
1403 HMSVM_REPORT_FEATURE(AMD_CPUID_SVM_FEATURE_EDX_TSC_RATE_MSR),
1404 HMSVM_REPORT_FEATURE(AMD_CPUID_SVM_FEATURE_EDX_VMCB_CLEAN),
1405 HMSVM_REPORT_FEATURE(AMD_CPUID_SVM_FEATURE_EDX_FLUSH_BY_ASID),
1406 HMSVM_REPORT_FEATURE(AMD_CPUID_SVM_FEATURE_EDX_DECODE_ASSIST),
1407 HMSVM_REPORT_FEATURE(AMD_CPUID_SVM_FEATURE_EDX_PAUSE_FILTER),
1408 HMSVM_REPORT_FEATURE(AMD_CPUID_SVM_FEATURE_EDX_PAUSE_FILTER_THRESHOLD),
1409 HMSVM_REPORT_FEATURE(AMD_CPUID_SVM_FEATURE_EDX_AVIC),
1410#undef HMSVM_REPORT_FEATURE
1411 };
1412
1413 uint32_t fSvmFeatures = pVM->hm.s.svm.u32Features;
1414 for (unsigned i = 0; i < RT_ELEMENTS(s_aSvmFeatures); i++)
1415 if (fSvmFeatures & s_aSvmFeatures[i].fFlag)
1416 {
1417 LogRel(("HM: %s\n", s_aSvmFeatures[i].pszName));
1418 fSvmFeatures &= ~s_aSvmFeatures[i].fFlag;
1419 }
1420 if (fSvmFeatures)
1421 for (unsigned iBit = 0; iBit < 32; iBit++)
1422 if (RT_BIT_32(iBit) & fSvmFeatures)
1423 LogRel(("HM: Reserved bit %u\n", iBit));
1424
1425 /*
1426 * Adjust feature(s).
1427 */
1428 if (pVM->hm.s.svm.u32Features & AMD_CPUID_SVM_FEATURE_EDX_NESTED_PAGING)
1429 pVM->hm.s.fNestedPaging = pVM->hm.s.fAllowNestedPaging;
1430
1431 /*
1432 * Call ring-0 to set up the VM.
1433 */
1434 int rc = SUPR3CallVMMR0Ex(pVM->pVMR0, 0 /*idCpu*/, VMMR0_DO_HM_SETUP_VM, 0, NULL);
1435 if (rc != VINF_SUCCESS)
1436 {
1437 AssertMsgFailed(("%Rrc\n", rc));
1438 LogRel(("HM: AMD-V setup failed with rc=%Rrc!\n", rc));
1439 return VMSetError(pVM, rc, RT_SRC_POS, "AMD-V setup failed: %Rrc", rc);
1440 }
1441
1442 LogRel(("HM: AMD-V enabled!\n"));
1443 pVM->hm.s.svm.fEnabled = true;
1444
1445 if (pVM->hm.s.fNestedPaging)
1446 {
1447 LogRel(("HM: Nested paging enabled!\n"));
1448
1449 /*
1450 * Enable large pages (2 MB) if applicable.
1451 */
1452#if HC_ARCH_BITS == 64
1453 if (pVM->hm.s.fLargePages)
1454 {
1455 PGMSetLargePageUsage(pVM, true);
1456 LogRel(("HM: Large page support enabled!\n"));
1457 }
1458#endif
1459 }
1460
1461 hmR3DisableRawMode(pVM);
1462
1463 /*
1464 * Change the CPU features.
1465 */
1466 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_SEP);
1467 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_SYSCALL);
1468 if (pVM->hm.s.fAllow64BitGuests)
1469 {
1470 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_PAE);
1471 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_LONG_MODE);
1472 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_NX);
1473 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_LAHF);
1474 }
1475 /* Turn on NXE if PAE has been enabled. */
1476 else if (CPUMGetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_PAE))
1477 CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_NX);
1478
1479 LogRel(("HM: TPR Patching %s.\n", (pVM->hm.s.fTRPPatchingAllowed) ? "enabled" : "disabled"));
1480
1481 LogRel((pVM->hm.s.fAllow64BitGuests
1482 ? "HM: Guest support: 32-bit and 64-bit.\n"
1483 : "HM: Guest support: 32-bit only.\n"));
1484
1485 return VINF_SUCCESS;
1486}
1487
1488
1489/**
1490 * Applies relocations to data and code managed by this
1491 * component. This function will be called at init and
1492 * whenever the VMM need to relocate it self inside the GC.
1493 *
1494 * @param pVM The VM.
1495 */
1496VMMR3_INT_DECL(void) HMR3Relocate(PVM pVM)
1497{
1498 Log(("HMR3Relocate to %RGv\n", MMHyperGetArea(pVM, 0)));
1499
1500 /* Fetch the current paging mode during the relocate callback during state loading. */
1501 if (VMR3GetState(pVM) == VMSTATE_LOADING)
1502 {
1503 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1504 {
1505 PVMCPU pVCpu = &pVM->aCpus[i];
1506
1507 pVCpu->hm.s.enmShadowMode = PGMGetShadowMode(pVCpu);
1508#ifdef VBOX_WITH_OLD_VTX_CODE
1509 Assert(pVCpu->hm.s.vmx.enmCurrGuestMode == PGMGetGuestMode(pVCpu));
1510 pVCpu->hm.s.vmx.enmCurrGuestMode = PGMGetGuestMode(pVCpu);
1511#endif
1512 }
1513 }
1514#if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
1515 if (HMIsEnabled(pVM))
1516 {
1517 switch (PGMGetHostMode(pVM))
1518 {
1519 case PGMMODE_32_BIT:
1520 pVM->hm.s.pfnHost32ToGuest64R0 = VMMR3GetHostToGuestSwitcher(pVM, VMMSWITCHER_32_TO_AMD64);
1521 break;
1522
1523 case PGMMODE_PAE:
1524 case PGMMODE_PAE_NX:
1525 pVM->hm.s.pfnHost32ToGuest64R0 = VMMR3GetHostToGuestSwitcher(pVM, VMMSWITCHER_PAE_TO_AMD64);
1526 break;
1527
1528 default:
1529 AssertFailed();
1530 break;
1531 }
1532 }
1533#endif
1534 return;
1535}
1536
1537
1538/**
1539 * Notification callback which is called whenever there is a chance that a CR3
1540 * value might have changed.
1541 *
1542 * This is called by PGM.
1543 *
1544 * @param pVM Pointer to the VM.
1545 * @param pVCpu Pointer to the VMCPU.
1546 * @param enmShadowMode New shadow paging mode.
1547 * @param enmGuestMode New guest paging mode.
1548 */
1549VMMR3_INT_DECL(void) HMR3PagingModeChanged(PVM pVM, PVMCPU pVCpu, PGMMODE enmShadowMode, PGMMODE enmGuestMode)
1550{
1551 /* Ignore page mode changes during state loading. */
1552 if (VMR3GetState(pVCpu->pVMR3) == VMSTATE_LOADING)
1553 return;
1554
1555 pVCpu->hm.s.enmShadowMode = enmShadowMode;
1556
1557#ifdef VBOX_WITH_OLD_VTX_CODE
1558 if ( pVM->hm.s.vmx.fEnabled
1559 && HMIsEnabled(pVM))
1560 {
1561 if ( pVCpu->hm.s.vmx.enmLastSeenGuestMode == PGMMODE_REAL
1562 && enmGuestMode >= PGMMODE_PROTECTED)
1563 {
1564 PCPUMCTX pCtx;
1565
1566 pCtx = CPUMQueryGuestCtxPtr(pVCpu);
1567
1568 /* After a real mode switch to protected mode we must force
1569 CPL to 0. Our real mode emulation had to set it to 3. */
1570 pCtx->ss.Attr.n.u2Dpl = 0;
1571 }
1572 }
1573
1574 if (pVCpu->hm.s.vmx.enmCurrGuestMode != enmGuestMode)
1575 {
1576 /* Keep track of paging mode changes. */
1577 pVCpu->hm.s.vmx.enmPrevGuestMode = pVCpu->hm.s.vmx.enmCurrGuestMode;
1578 pVCpu->hm.s.vmx.enmCurrGuestMode = enmGuestMode;
1579
1580 /* Did we miss a change, because all code was executed in the recompiler? */
1581 if (pVCpu->hm.s.vmx.enmLastSeenGuestMode == enmGuestMode)
1582 {
1583 Log(("HMR3PagingModeChanged missed %s->%s transition (prev %s)\n", PGMGetModeName(pVCpu->hm.s.vmx.enmPrevGuestMode),
1584 PGMGetModeName(pVCpu->hm.s.vmx.enmCurrGuestMode), PGMGetModeName(pVCpu->hm.s.vmx.enmLastSeenGuestMode)));
1585 pVCpu->hm.s.vmx.enmLastSeenGuestMode = pVCpu->hm.s.vmx.enmPrevGuestMode;
1586 }
1587 }
1588#else
1589 /* If the guest left protected mode VMX execution, we'll have to be extra
1590 * careful if/when the guest switches back to protected mode.
1591 */
1592 if (enmGuestMode == PGMMODE_REAL)
1593 {
1594 Log(("HMR3PagingModeChanged indicates real mode execution\n"));
1595 pVCpu->hm.s.vmx.fWasInRealMode = true;
1596 }
1597#endif
1598
1599 /** @todo r=ramshankar: Why do we need to do this? */
1600#ifdef VMX_USE_CACHED_VMCS_ACCESSES
1601 /* Reset the contents of the read cache. */
1602 PVMCSCACHE pCache = &pVCpu->hm.s.vmx.VMCSCache;
1603 for (unsigned j = 0; j < pCache->Read.cValidEntries; j++)
1604 pCache->Read.aFieldVal[j] = 0;
1605#endif
1606}
1607
1608
1609/**
1610 * Terminates the HM.
1611 *
1612 * Termination means cleaning up and freeing all resources,
1613 * the VM itself is, at this point, powered off or suspended.
1614 *
1615 * @returns VBox status code.
1616 * @param pVM Pointer to the VM.
1617 */
1618VMMR3_INT_DECL(int) HMR3Term(PVM pVM)
1619{
1620 if (pVM->hm.s.vmx.pRealModeTSS)
1621 {
1622 PDMR3VmmDevHeapFree(pVM, pVM->hm.s.vmx.pRealModeTSS);
1623 pVM->hm.s.vmx.pRealModeTSS = 0;
1624 }
1625 hmR3TermCPU(pVM);
1626 return 0;
1627}
1628
1629
1630/**
1631 * Terminates the per-VCPU HM.
1632 *
1633 * @returns VBox status code.
1634 * @param pVM Pointer to the VM.
1635 */
1636static int hmR3TermCPU(PVM pVM)
1637{
1638 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1639 {
1640 PVMCPU pVCpu = &pVM->aCpus[i]; NOREF(pVCpu);
1641
1642#ifdef VBOX_WITH_STATISTICS
1643 if (pVCpu->hm.s.paStatExitReason)
1644 {
1645 MMHyperFree(pVM, pVCpu->hm.s.paStatExitReason);
1646 pVCpu->hm.s.paStatExitReason = NULL;
1647 pVCpu->hm.s.paStatExitReasonR0 = NIL_RTR0PTR;
1648 }
1649 if (pVCpu->hm.s.paStatInjectedIrqs)
1650 {
1651 MMHyperFree(pVM, pVCpu->hm.s.paStatInjectedIrqs);
1652 pVCpu->hm.s.paStatInjectedIrqs = NULL;
1653 pVCpu->hm.s.paStatInjectedIrqsR0 = NIL_RTR0PTR;
1654 }
1655#endif
1656
1657#ifdef VBOX_WITH_CRASHDUMP_MAGIC
1658 memset(pVCpu->hm.s.vmx.VMCSCache.aMagic, 0, sizeof(pVCpu->hm.s.vmx.VMCSCache.aMagic));
1659 pVCpu->hm.s.vmx.VMCSCache.uMagic = 0;
1660 pVCpu->hm.s.vmx.VMCSCache.uPos = 0xffffffff;
1661#endif
1662 }
1663 return 0;
1664}
1665
1666
1667/**
1668 * Resets a virtual CPU.
1669 *
1670 * Used by HMR3Reset and CPU hot plugging.
1671 *
1672 * @param pVCpu The CPU to reset.
1673 */
1674VMMR3_INT_DECL(void) HMR3ResetCpu(PVMCPU pVCpu)
1675{
1676 /* On first entry we'll sync everything. */
1677 pVCpu->hm.s.fContextUseFlags = (HM_CHANGED_HOST_CONTEXT | HM_CHANGED_ALL_GUEST);
1678
1679 pVCpu->hm.s.vmx.u32CR0Mask = 0;
1680 pVCpu->hm.s.vmx.u32CR4Mask = 0;
1681
1682 pVCpu->hm.s.fActive = false;
1683 pVCpu->hm.s.Event.fPending = false;
1684
1685#ifdef VBOX_WITH_OLD_VTX_CODE
1686 /* Reset state information for real-mode emulation in VT-x. */
1687 pVCpu->hm.s.vmx.enmLastSeenGuestMode = PGMMODE_REAL;
1688 pVCpu->hm.s.vmx.enmPrevGuestMode = PGMMODE_REAL;
1689 pVCpu->hm.s.vmx.enmCurrGuestMode = PGMMODE_REAL;
1690#else
1691 pVCpu->hm.s.vmx.fWasInRealMode = true;
1692#endif
1693
1694 /* Reset the contents of the read cache. */
1695 PVMCSCACHE pCache = &pVCpu->hm.s.vmx.VMCSCache;
1696 for (unsigned j = 0; j < pCache->Read.cValidEntries; j++)
1697 pCache->Read.aFieldVal[j] = 0;
1698
1699#ifdef VBOX_WITH_CRASHDUMP_MAGIC
1700 /* Magic marker for searching in crash dumps. */
1701 strcpy((char *)pCache->aMagic, "VMCSCACHE Magic");
1702 pCache->uMagic = UINT64_C(0xDEADBEEFDEADBEEF);
1703#endif
1704}
1705
1706
1707/**
1708 * The VM is being reset.
1709 *
1710 * For the HM component this means that any GDT/LDT/TSS monitors
1711 * needs to be removed.
1712 *
1713 * @param pVM Pointer to the VM.
1714 */
1715VMMR3_INT_DECL(void) HMR3Reset(PVM pVM)
1716{
1717 LogFlow(("HMR3Reset:\n"));
1718
1719 if (HMIsEnabled(pVM))
1720 hmR3DisableRawMode(pVM);
1721
1722 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1723 {
1724 PVMCPU pVCpu = &pVM->aCpus[i];
1725
1726 HMR3ResetCpu(pVCpu);
1727 }
1728
1729 /* Clear all patch information. */
1730 pVM->hm.s.pGuestPatchMem = 0;
1731 pVM->hm.s.pFreeGuestPatchMem = 0;
1732 pVM->hm.s.cbGuestPatchMem = 0;
1733 pVM->hm.s.cPatches = 0;
1734 pVM->hm.s.PatchTree = 0;
1735 pVM->hm.s.fTPRPatchingActive = false;
1736 ASMMemZero32(pVM->hm.s.aPatches, sizeof(pVM->hm.s.aPatches));
1737}
1738
1739
1740/**
1741 * Callback to patch a TPR instruction (vmmcall or mov cr8).
1742 *
1743 * @returns VBox strict status code.
1744 * @param pVM Pointer to the VM.
1745 * @param pVCpu The VMCPU for the EMT we're being called on.
1746 * @param pvUser Unused.
1747 */
1748DECLCALLBACK(VBOXSTRICTRC) hmR3RemovePatches(PVM pVM, PVMCPU pVCpu, void *pvUser)
1749{
1750 VMCPUID idCpu = (VMCPUID)(uintptr_t)pvUser;
1751
1752 /* Only execute the handler on the VCPU the original patch request was issued. */
1753 if (pVCpu->idCpu != idCpu)
1754 return VINF_SUCCESS;
1755
1756 Log(("hmR3RemovePatches\n"));
1757 for (unsigned i = 0; i < pVM->hm.s.cPatches; i++)
1758 {
1759 uint8_t abInstr[15];
1760 PHMTPRPATCH pPatch = &pVM->hm.s.aPatches[i];
1761 RTGCPTR pInstrGC = (RTGCPTR)pPatch->Core.Key;
1762 int rc;
1763
1764#ifdef LOG_ENABLED
1765 char szOutput[256];
1766
1767 rc = DBGFR3DisasInstrEx(pVM->pUVM, pVCpu->idCpu, CPUMGetGuestCS(pVCpu), pInstrGC, DBGF_DISAS_FLAGS_DEFAULT_MODE,
1768 szOutput, sizeof(szOutput), NULL);
1769 if (RT_SUCCESS(rc))
1770 Log(("Patched instr: %s\n", szOutput));
1771#endif
1772
1773 /* Check if the instruction is still the same. */
1774 rc = PGMPhysSimpleReadGCPtr(pVCpu, abInstr, pInstrGC, pPatch->cbNewOp);
1775 if (rc != VINF_SUCCESS)
1776 {
1777 Log(("Patched code removed? (rc=%Rrc0\n", rc));
1778 continue; /* swapped out or otherwise removed; skip it. */
1779 }
1780
1781 if (memcmp(abInstr, pPatch->aNewOpcode, pPatch->cbNewOp))
1782 {
1783 Log(("Patched instruction was changed! (rc=%Rrc0\n", rc));
1784 continue; /* skip it. */
1785 }
1786
1787 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pInstrGC, pPatch->aOpcode, pPatch->cbOp);
1788 AssertRC(rc);
1789
1790#ifdef LOG_ENABLED
1791 rc = DBGFR3DisasInstrEx(pVM->pUVM, pVCpu->idCpu, CPUMGetGuestCS(pVCpu), pInstrGC, DBGF_DISAS_FLAGS_DEFAULT_MODE,
1792 szOutput, sizeof(szOutput), NULL);
1793 if (RT_SUCCESS(rc))
1794 Log(("Original instr: %s\n", szOutput));
1795#endif
1796 }
1797 pVM->hm.s.cPatches = 0;
1798 pVM->hm.s.PatchTree = 0;
1799 pVM->hm.s.pFreeGuestPatchMem = pVM->hm.s.pGuestPatchMem;
1800 pVM->hm.s.fTPRPatchingActive = false;
1801 return VINF_SUCCESS;
1802}
1803
1804
1805/**
1806 * Worker for enabling patching in a VT-x/AMD-V guest.
1807 *
1808 * @returns VBox status code.
1809 * @param pVM Pointer to the VM.
1810 * @param idCpu VCPU to execute hmR3RemovePatches on.
1811 * @param pPatchMem Patch memory range.
1812 * @param cbPatchMem Size of the memory range.
1813 */
1814static int hmR3EnablePatching(PVM pVM, VMCPUID idCpu, RTRCPTR pPatchMem, unsigned cbPatchMem)
1815{
1816 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE, hmR3RemovePatches, (void *)(uintptr_t)idCpu);
1817 AssertRC(rc);
1818
1819 pVM->hm.s.pGuestPatchMem = pPatchMem;
1820 pVM->hm.s.pFreeGuestPatchMem = pPatchMem;
1821 pVM->hm.s.cbGuestPatchMem = cbPatchMem;
1822 return VINF_SUCCESS;
1823}
1824
1825
1826/**
1827 * Enable patching in a VT-x/AMD-V guest
1828 *
1829 * @returns VBox status code.
1830 * @param pVM Pointer to the VM.
1831 * @param pPatchMem Patch memory range.
1832 * @param cbPatchMem Size of the memory range.
1833 */
1834VMMR3_INT_DECL(int) HMR3EnablePatching(PVM pVM, RTGCPTR pPatchMem, unsigned cbPatchMem)
1835{
1836 VM_ASSERT_EMT(pVM);
1837 Log(("HMR3EnablePatching %RGv size %x\n", pPatchMem, cbPatchMem));
1838 if (pVM->cCpus > 1)
1839 {
1840 /* We own the IOM lock here and could cause a deadlock by waiting for a VCPU that is blocking on the IOM lock. */
1841 int rc = VMR3ReqCallNoWait(pVM, VMCPUID_ANY_QUEUE,
1842 (PFNRT)hmR3EnablePatching, 4, pVM, VMMGetCpuId(pVM), (RTRCPTR)pPatchMem, cbPatchMem);
1843 AssertRC(rc);
1844 return rc;
1845 }
1846 return hmR3EnablePatching(pVM, VMMGetCpuId(pVM), (RTRCPTR)pPatchMem, cbPatchMem);
1847}
1848
1849
1850/**
1851 * Disable patching in a VT-x/AMD-V guest.
1852 *
1853 * @returns VBox status code.
1854 * @param pVM Pointer to the VM.
1855 * @param pPatchMem Patch memory range.
1856 * @param cbPatchMem Size of the memory range.
1857 */
1858VMMR3_INT_DECL(int) HMR3DisablePatching(PVM pVM, RTGCPTR pPatchMem, unsigned cbPatchMem)
1859{
1860 Log(("HMR3DisablePatching %RGv size %x\n", pPatchMem, cbPatchMem));
1861
1862 Assert(pVM->hm.s.pGuestPatchMem == pPatchMem);
1863 Assert(pVM->hm.s.cbGuestPatchMem == cbPatchMem);
1864
1865 /* @todo Potential deadlock when other VCPUs are waiting on the IOM lock (we own it)!! */
1866 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE, hmR3RemovePatches,
1867 (void *)(uintptr_t)VMMGetCpuId(pVM));
1868 AssertRC(rc);
1869
1870 pVM->hm.s.pGuestPatchMem = 0;
1871 pVM->hm.s.pFreeGuestPatchMem = 0;
1872 pVM->hm.s.cbGuestPatchMem = 0;
1873 pVM->hm.s.fTPRPatchingActive = false;
1874 return VINF_SUCCESS;
1875}
1876
1877
1878/**
1879 * Callback to patch a TPR instruction (vmmcall or mov cr8).
1880 *
1881 * @returns VBox strict status code.
1882 * @param pVM Pointer to the VM.
1883 * @param pVCpu The VMCPU for the EMT we're being called on.
1884 * @param pvUser User specified CPU context.
1885 *
1886 */
1887DECLCALLBACK(VBOXSTRICTRC) hmR3ReplaceTprInstr(PVM pVM, PVMCPU pVCpu, void *pvUser)
1888{
1889 /*
1890 * Only execute the handler on the VCPU the original patch request was
1891 * issued. (The other CPU(s) might not yet have switched to protected
1892 * mode, nor have the correct memory context.)
1893 */
1894 VMCPUID idCpu = (VMCPUID)(uintptr_t)pvUser;
1895 if (pVCpu->idCpu != idCpu)
1896 return VINF_SUCCESS;
1897
1898 /*
1899 * We're racing other VCPUs here, so don't try patch the instruction twice
1900 * and make sure there is still room for our patch record.
1901 */
1902 PCPUMCTX pCtx = CPUMQueryGuestCtxPtr(pVCpu);
1903 PHMTPRPATCH pPatch = (PHMTPRPATCH)RTAvloU32Get(&pVM->hm.s.PatchTree, (AVLOU32KEY)pCtx->eip);
1904 if (pPatch)
1905 {
1906 Log(("hmR3ReplaceTprInstr: already patched %RGv\n", pCtx->rip));
1907 return VINF_SUCCESS;
1908 }
1909 uint32_t const idx = pVM->hm.s.cPatches;
1910 if (idx >= RT_ELEMENTS(pVM->hm.s.aPatches))
1911 {
1912 Log(("hmR3ReplaceTprInstr: no available patch slots (%RGv)\n", pCtx->rip));
1913 return VINF_SUCCESS;
1914 }
1915 pPatch = &pVM->hm.s.aPatches[idx];
1916
1917 Log(("hmR3ReplaceTprInstr: rip=%RGv idxPatch=%u\n", pCtx->rip, idx));
1918
1919 /*
1920 * Disassembler the instruction and get cracking.
1921 */
1922 DBGFR3_DISAS_INSTR_CUR_LOG(pVCpu, "hmR3ReplaceTprInstr");
1923 PDISCPUSTATE pDis = &pVCpu->hm.s.DisState;
1924 uint32_t cbOp;
1925 int rc = EMInterpretDisasCurrent(pVM, pVCpu, pDis, &cbOp);
1926 AssertRC(rc);
1927 if ( rc == VINF_SUCCESS
1928 && pDis->pCurInstr->uOpcode == OP_MOV
1929 && cbOp >= 3)
1930 {
1931 static uint8_t const s_abVMMCall[3] = { 0x0f, 0x01, 0xd9 };
1932
1933 rc = PGMPhysSimpleReadGCPtr(pVCpu, pPatch->aOpcode, pCtx->rip, cbOp);
1934 AssertRC(rc);
1935
1936 pPatch->cbOp = cbOp;
1937
1938 if (pDis->Param1.fUse == DISUSE_DISPLACEMENT32)
1939 {
1940 /* write. */
1941 if (pDis->Param2.fUse == DISUSE_REG_GEN32)
1942 {
1943 pPatch->enmType = HMTPRINSTR_WRITE_REG;
1944 pPatch->uSrcOperand = pDis->Param2.Base.idxGenReg;
1945 Log(("hmR3ReplaceTprInstr: HMTPRINSTR_WRITE_REG %u\n", pDis->Param2.Base.idxGenReg));
1946 }
1947 else
1948 {
1949 Assert(pDis->Param2.fUse == DISUSE_IMMEDIATE32);
1950 pPatch->enmType = HMTPRINSTR_WRITE_IMM;
1951 pPatch->uSrcOperand = pDis->Param2.uValue;
1952 Log(("hmR3ReplaceTprInstr: HMTPRINSTR_WRITE_IMM %#llx\n", pDis->Param2.uValue));
1953 }
1954 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pCtx->rip, s_abVMMCall, sizeof(s_abVMMCall));
1955 AssertRC(rc);
1956
1957 memcpy(pPatch->aNewOpcode, s_abVMMCall, sizeof(s_abVMMCall));
1958 pPatch->cbNewOp = sizeof(s_abVMMCall);
1959 }
1960 else
1961 {
1962 /*
1963 * TPR Read.
1964 *
1965 * Found:
1966 * mov eax, dword [fffe0080] (5 bytes)
1967 * Check if next instruction is:
1968 * shr eax, 4
1969 */
1970 Assert(pDis->Param1.fUse == DISUSE_REG_GEN32);
1971
1972 uint8_t const idxMmioReg = pDis->Param1.Base.idxGenReg;
1973 uint8_t const cbOpMmio = cbOp;
1974 uint64_t const uSavedRip = pCtx->rip;
1975
1976 pCtx->rip += cbOp;
1977 rc = EMInterpretDisasCurrent(pVM, pVCpu, pDis, &cbOp);
1978 DBGFR3_DISAS_INSTR_CUR_LOG(pVCpu, "Following read");
1979 pCtx->rip = uSavedRip;
1980
1981 if ( rc == VINF_SUCCESS
1982 && pDis->pCurInstr->uOpcode == OP_SHR
1983 && pDis->Param1.fUse == DISUSE_REG_GEN32
1984 && pDis->Param1.Base.idxGenReg == idxMmioReg
1985 && pDis->Param2.fUse == DISUSE_IMMEDIATE8
1986 && pDis->Param2.uValue == 4
1987 && cbOpMmio + cbOp < sizeof(pVM->hm.s.aPatches[idx].aOpcode))
1988 {
1989 uint8_t abInstr[15];
1990
1991 /* Replacing the two instructions above with an AMD-V specific lock-prefixed 32-bit MOV CR8 instruction so as to
1992 access CR8 in 32-bit mode and not cause a #VMEXIT. */
1993 rc = PGMPhysSimpleReadGCPtr(pVCpu, &pPatch->aOpcode, pCtx->rip, cbOpMmio + cbOp);
1994 AssertRC(rc);
1995
1996 pPatch->cbOp = cbOpMmio + cbOp;
1997
1998 /* 0xF0, 0x0F, 0x20, 0xC0 = mov eax, cr8 */
1999 abInstr[0] = 0xF0;
2000 abInstr[1] = 0x0F;
2001 abInstr[2] = 0x20;
2002 abInstr[3] = 0xC0 | pDis->Param1.Base.idxGenReg;
2003 for (unsigned i = 4; i < pPatch->cbOp; i++)
2004 abInstr[i] = 0x90; /* nop */
2005
2006 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pCtx->rip, abInstr, pPatch->cbOp);
2007 AssertRC(rc);
2008
2009 memcpy(pPatch->aNewOpcode, abInstr, pPatch->cbOp);
2010 pPatch->cbNewOp = pPatch->cbOp;
2011
2012 Log(("Acceptable read/shr candidate!\n"));
2013 pPatch->enmType = HMTPRINSTR_READ_SHR4;
2014 }
2015 else
2016 {
2017 pPatch->enmType = HMTPRINSTR_READ;
2018 pPatch->uDstOperand = idxMmioReg;
2019
2020 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pCtx->rip, s_abVMMCall, sizeof(s_abVMMCall));
2021 AssertRC(rc);
2022
2023 memcpy(pPatch->aNewOpcode, s_abVMMCall, sizeof(s_abVMMCall));
2024 pPatch->cbNewOp = sizeof(s_abVMMCall);
2025 Log(("hmR3ReplaceTprInstr: HMTPRINSTR_READ %u\n", pPatch->uDstOperand));
2026 }
2027 }
2028
2029 pPatch->Core.Key = pCtx->eip;
2030 rc = RTAvloU32Insert(&pVM->hm.s.PatchTree, &pPatch->Core);
2031 AssertRC(rc);
2032
2033 pVM->hm.s.cPatches++;
2034 STAM_COUNTER_INC(&pVM->hm.s.StatTprReplaceSuccess);
2035 return VINF_SUCCESS;
2036 }
2037
2038 /*
2039 * Save invalid patch, so we will not try again.
2040 */
2041 Log(("hmR3ReplaceTprInstr: Failed to patch instr!\n"));
2042 pPatch->Core.Key = pCtx->eip;
2043 pPatch->enmType = HMTPRINSTR_INVALID;
2044 rc = RTAvloU32Insert(&pVM->hm.s.PatchTree, &pPatch->Core);
2045 AssertRC(rc);
2046 pVM->hm.s.cPatches++;
2047 STAM_COUNTER_INC(&pVM->hm.s.StatTprReplaceFailure);
2048 return VINF_SUCCESS;
2049}
2050
2051
2052/**
2053 * Callback to patch a TPR instruction (jump to generated code).
2054 *
2055 * @returns VBox strict status code.
2056 * @param pVM Pointer to the VM.
2057 * @param pVCpu The VMCPU for the EMT we're being called on.
2058 * @param pvUser User specified CPU context.
2059 *
2060 */
2061DECLCALLBACK(VBOXSTRICTRC) hmR3PatchTprInstr(PVM pVM, PVMCPU pVCpu, void *pvUser)
2062{
2063 /*
2064 * Only execute the handler on the VCPU the original patch request was
2065 * issued. (The other CPU(s) might not yet have switched to protected
2066 * mode, nor have the correct memory context.)
2067 */
2068 VMCPUID idCpu = (VMCPUID)(uintptr_t)pvUser;
2069 if (pVCpu->idCpu != idCpu)
2070 return VINF_SUCCESS;
2071
2072 /*
2073 * We're racing other VCPUs here, so don't try patch the instruction twice
2074 * and make sure there is still room for our patch record.
2075 */
2076 PCPUMCTX pCtx = CPUMQueryGuestCtxPtr(pVCpu);
2077 PHMTPRPATCH pPatch = (PHMTPRPATCH)RTAvloU32Get(&pVM->hm.s.PatchTree, (AVLOU32KEY)pCtx->eip);
2078 if (pPatch)
2079 {
2080 Log(("hmR3PatchTprInstr: already patched %RGv\n", pCtx->rip));
2081 return VINF_SUCCESS;
2082 }
2083 uint32_t const idx = pVM->hm.s.cPatches;
2084 if (idx >= RT_ELEMENTS(pVM->hm.s.aPatches))
2085 {
2086 Log(("hmR3PatchTprInstr: no available patch slots (%RGv)\n", pCtx->rip));
2087 return VINF_SUCCESS;
2088 }
2089 pPatch = &pVM->hm.s.aPatches[idx];
2090
2091 Log(("hmR3PatchTprInstr: rip=%RGv idxPatch=%u\n", pCtx->rip, idx));
2092 DBGFR3_DISAS_INSTR_CUR_LOG(pVCpu, "hmR3PatchTprInstr");
2093
2094 /*
2095 * Disassemble the instruction and get cracking.
2096 */
2097 PDISCPUSTATE pDis = &pVCpu->hm.s.DisState;
2098 uint32_t cbOp;
2099 int rc = EMInterpretDisasCurrent(pVM, pVCpu, pDis, &cbOp);
2100 AssertRC(rc);
2101 if ( rc == VINF_SUCCESS
2102 && pDis->pCurInstr->uOpcode == OP_MOV
2103 && cbOp >= 5)
2104 {
2105 uint8_t aPatch[64];
2106 uint32_t off = 0;
2107
2108 rc = PGMPhysSimpleReadGCPtr(pVCpu, pPatch->aOpcode, pCtx->rip, cbOp);
2109 AssertRC(rc);
2110
2111 pPatch->cbOp = cbOp;
2112 pPatch->enmType = HMTPRINSTR_JUMP_REPLACEMENT;
2113
2114 if (pDis->Param1.fUse == DISUSE_DISPLACEMENT32)
2115 {
2116 /*
2117 * TPR write:
2118 *
2119 * push ECX [51]
2120 * push EDX [52]
2121 * push EAX [50]
2122 * xor EDX,EDX [31 D2]
2123 * mov EAX,EAX [89 C0]
2124 * or
2125 * mov EAX,0000000CCh [B8 CC 00 00 00]
2126 * mov ECX,0C0000082h [B9 82 00 00 C0]
2127 * wrmsr [0F 30]
2128 * pop EAX [58]
2129 * pop EDX [5A]
2130 * pop ECX [59]
2131 * jmp return_address [E9 return_address]
2132 *
2133 */
2134 bool fUsesEax = (pDis->Param2.fUse == DISUSE_REG_GEN32 && pDis->Param2.Base.idxGenReg == DISGREG_EAX);
2135
2136 aPatch[off++] = 0x51; /* push ecx */
2137 aPatch[off++] = 0x52; /* push edx */
2138 if (!fUsesEax)
2139 aPatch[off++] = 0x50; /* push eax */
2140 aPatch[off++] = 0x31; /* xor edx, edx */
2141 aPatch[off++] = 0xD2;
2142 if (pDis->Param2.fUse == DISUSE_REG_GEN32)
2143 {
2144 if (!fUsesEax)
2145 {
2146 aPatch[off++] = 0x89; /* mov eax, src_reg */
2147 aPatch[off++] = MAKE_MODRM(3, pDis->Param2.Base.idxGenReg, DISGREG_EAX);
2148 }
2149 }
2150 else
2151 {
2152 Assert(pDis->Param2.fUse == DISUSE_IMMEDIATE32);
2153 aPatch[off++] = 0xB8; /* mov eax, immediate */
2154 *(uint32_t *)&aPatch[off] = pDis->Param2.uValue;
2155 off += sizeof(uint32_t);
2156 }
2157 aPatch[off++] = 0xB9; /* mov ecx, 0xc0000082 */
2158 *(uint32_t *)&aPatch[off] = MSR_K8_LSTAR;
2159 off += sizeof(uint32_t);
2160
2161 aPatch[off++] = 0x0F; /* wrmsr */
2162 aPatch[off++] = 0x30;
2163 if (!fUsesEax)
2164 aPatch[off++] = 0x58; /* pop eax */
2165 aPatch[off++] = 0x5A; /* pop edx */
2166 aPatch[off++] = 0x59; /* pop ecx */
2167 }
2168 else
2169 {
2170 /*
2171 * TPR read:
2172 *
2173 * push ECX [51]
2174 * push EDX [52]
2175 * push EAX [50]
2176 * mov ECX,0C0000082h [B9 82 00 00 C0]
2177 * rdmsr [0F 32]
2178 * mov EAX,EAX [89 C0]
2179 * pop EAX [58]
2180 * pop EDX [5A]
2181 * pop ECX [59]
2182 * jmp return_address [E9 return_address]
2183 *
2184 */
2185 Assert(pDis->Param1.fUse == DISUSE_REG_GEN32);
2186
2187 if (pDis->Param1.Base.idxGenReg != DISGREG_ECX)
2188 aPatch[off++] = 0x51; /* push ecx */
2189 if (pDis->Param1.Base.idxGenReg != DISGREG_EDX )
2190 aPatch[off++] = 0x52; /* push edx */
2191 if (pDis->Param1.Base.idxGenReg != DISGREG_EAX)
2192 aPatch[off++] = 0x50; /* push eax */
2193
2194 aPatch[off++] = 0x31; /* xor edx, edx */
2195 aPatch[off++] = 0xD2;
2196
2197 aPatch[off++] = 0xB9; /* mov ecx, 0xc0000082 */
2198 *(uint32_t *)&aPatch[off] = MSR_K8_LSTAR;
2199 off += sizeof(uint32_t);
2200
2201 aPatch[off++] = 0x0F; /* rdmsr */
2202 aPatch[off++] = 0x32;
2203
2204 if (pDis->Param1.Base.idxGenReg != DISGREG_EAX)
2205 {
2206 aPatch[off++] = 0x89; /* mov dst_reg, eax */
2207 aPatch[off++] = MAKE_MODRM(3, DISGREG_EAX, pDis->Param1.Base.idxGenReg);
2208 }
2209
2210 if (pDis->Param1.Base.idxGenReg != DISGREG_EAX)
2211 aPatch[off++] = 0x58; /* pop eax */
2212 if (pDis->Param1.Base.idxGenReg != DISGREG_EDX )
2213 aPatch[off++] = 0x5A; /* pop edx */
2214 if (pDis->Param1.Base.idxGenReg != DISGREG_ECX)
2215 aPatch[off++] = 0x59; /* pop ecx */
2216 }
2217 aPatch[off++] = 0xE9; /* jmp return_address */
2218 *(RTRCUINTPTR *)&aPatch[off] = ((RTRCUINTPTR)pCtx->eip + cbOp) - ((RTRCUINTPTR)pVM->hm.s.pFreeGuestPatchMem + off + 4);
2219 off += sizeof(RTRCUINTPTR);
2220
2221 if (pVM->hm.s.pFreeGuestPatchMem + off <= pVM->hm.s.pGuestPatchMem + pVM->hm.s.cbGuestPatchMem)
2222 {
2223 /* Write new code to the patch buffer. */
2224 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pVM->hm.s.pFreeGuestPatchMem, aPatch, off);
2225 AssertRC(rc);
2226
2227#ifdef LOG_ENABLED
2228 uint32_t cbCurInstr;
2229 for (RTGCPTR GCPtrInstr = pVM->hm.s.pFreeGuestPatchMem;
2230 GCPtrInstr < pVM->hm.s.pFreeGuestPatchMem + off;
2231 GCPtrInstr += RT_MAX(cbCurInstr, 1))
2232 {
2233 char szOutput[256];
2234 rc = DBGFR3DisasInstrEx(pVM->pUVM, pVCpu->idCpu, pCtx->cs.Sel, GCPtrInstr, DBGF_DISAS_FLAGS_DEFAULT_MODE,
2235 szOutput, sizeof(szOutput), &cbCurInstr);
2236 if (RT_SUCCESS(rc))
2237 Log(("Patch instr %s\n", szOutput));
2238 else
2239 Log(("%RGv: rc=%Rrc\n", GCPtrInstr, rc));
2240 }
2241#endif
2242
2243 pPatch->aNewOpcode[0] = 0xE9;
2244 *(RTRCUINTPTR *)&pPatch->aNewOpcode[1] = ((RTRCUINTPTR)pVM->hm.s.pFreeGuestPatchMem) - ((RTRCUINTPTR)pCtx->eip + 5);
2245
2246 /* Overwrite the TPR instruction with a jump. */
2247 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pCtx->eip, pPatch->aNewOpcode, 5);
2248 AssertRC(rc);
2249
2250 DBGFR3_DISAS_INSTR_CUR_LOG(pVCpu, "Jump");
2251
2252 pVM->hm.s.pFreeGuestPatchMem += off;
2253 pPatch->cbNewOp = 5;
2254
2255 pPatch->Core.Key = pCtx->eip;
2256 rc = RTAvloU32Insert(&pVM->hm.s.PatchTree, &pPatch->Core);
2257 AssertRC(rc);
2258
2259 pVM->hm.s.cPatches++;
2260 pVM->hm.s.fTPRPatchingActive = true;
2261 STAM_COUNTER_INC(&pVM->hm.s.StatTprPatchSuccess);
2262 return VINF_SUCCESS;
2263 }
2264
2265 Log(("Ran out of space in our patch buffer!\n"));
2266 }
2267 else
2268 Log(("hmR3PatchTprInstr: Failed to patch instr!\n"));
2269
2270
2271 /*
2272 * Save invalid patch, so we will not try again.
2273 */
2274 pPatch = &pVM->hm.s.aPatches[idx];
2275 pPatch->Core.Key = pCtx->eip;
2276 pPatch->enmType = HMTPRINSTR_INVALID;
2277 rc = RTAvloU32Insert(&pVM->hm.s.PatchTree, &pPatch->Core);
2278 AssertRC(rc);
2279 pVM->hm.s.cPatches++;
2280 STAM_COUNTER_INC(&pVM->hm.s.StatTprPatchFailure);
2281 return VINF_SUCCESS;
2282}
2283
2284
2285/**
2286 * Attempt to patch TPR mmio instructions.
2287 *
2288 * @returns VBox status code.
2289 * @param pVM Pointer to the VM.
2290 * @param pVCpu Pointer to the VMCPU.
2291 * @param pCtx Pointer to the guest CPU context.
2292 */
2293VMMR3_INT_DECL(int) HMR3PatchTprInstr(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
2294{
2295 NOREF(pCtx);
2296 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE,
2297 pVM->hm.s.pGuestPatchMem ? hmR3PatchTprInstr : hmR3ReplaceTprInstr,
2298 (void *)(uintptr_t)pVCpu->idCpu);
2299 AssertRC(rc);
2300 return rc;
2301}
2302
2303
2304/**
2305 * Checks if a code selector (CS) is suitable for execution
2306 * within VMX when unrestricted execution isn't available.
2307 *
2308 * @returns true if selector is suitable for VMX, otherwise
2309 * false.
2310 * @param pSel Pointer to the selector to check (CS).
2311 * uStackDpl The DPL of the stack segment.
2312 */
2313static bool hmR3IsCodeSelectorOkForVmx(PCPUMSELREG pSel, unsigned uStackDpl)
2314{
2315 bool rc = false;
2316
2317 do
2318 {
2319 /* Segment must be accessed. */
2320 if (!(pSel->Attr.u & X86_SEL_TYPE_ACCESSED))
2321 break;
2322 /* Segment must be a code segment. */
2323 if (!(pSel->Attr.u & X86_SEL_TYPE_CODE))
2324 break;
2325 /* The S bit must be set. */
2326 if (!pSel->Attr.n.u1DescType)
2327 break;
2328 if (pSel->Attr.n.u4Type & X86_SEL_TYPE_CONF)
2329 {
2330 /* For conforming segments, CS.DPL must be <= SS.DPL. */
2331 if (pSel->Attr.n.u2Dpl > uStackDpl)
2332 break;
2333 }
2334 else
2335 {
2336 /* For non-conforming segments, CS.DPL must equal SS.DPL. */
2337 if (pSel->Attr.n.u2Dpl != uStackDpl)
2338 break;
2339 }
2340 /* Segment must be present. */
2341 if (!pSel->Attr.n.u1Present)
2342 break;
2343 /* G bit must be set if any high limit bits are set. */
2344 if ((pSel->u32Limit & 0xfff00000) && !pSel->Attr.n.u1Granularity)
2345 break;
2346 /* G bit must be clear if any low limit bits are clear. */
2347 if ((pSel->u32Limit & 0x0fff) != 0x0fff && pSel->Attr.n.u1Granularity)
2348 break;
2349
2350 rc = true;
2351 } while (0);
2352 return rc;
2353}
2354
2355
2356/**
2357 * Checks if a data selector (DS/ES/FS/GS) is suitable for
2358 * execution within VMX when unrestricted execution isn't
2359 * available.
2360 *
2361 * @returns true if selector is suitable for VMX, otherwise
2362 * false.
2363 * @param pSel Pointer to the selector to check
2364 * (DS/ES/FS/GS).
2365 */
2366static bool hmR3IsDataSelectorOkForVmx(PCPUMSELREG pSel)
2367{
2368 bool rc = false;
2369
2370 /* If attributes are all zero, consider the segment unusable and therefore OK.
2371 * This logic must be in sync with HMVMXR0.cpp!
2372 */
2373 if (!pSel->Attr.u)
2374 return true;
2375
2376 do
2377 {
2378 /* Segment must be accessed. */
2379 if (!(pSel->Attr.u & X86_SEL_TYPE_ACCESSED))
2380 break;
2381 /* Code segments must also be readable. */
2382 if (pSel->Attr.u & X86_SEL_TYPE_CODE && !(pSel->Attr.u & X86_SEL_TYPE_READ))
2383 break;
2384 /* The S bit must be set. */
2385 if (!pSel->Attr.n.u1DescType)
2386 break;
2387 /* Except for conforming segments, DPL >= RPL. */
2388 if (pSel->Attr.n.u4Type <= X86_SEL_TYPE_ER_ACC && pSel->Attr.n.u2Dpl < (pSel->Sel & X86_SEL_RPL))
2389 break;
2390 /* Segment must be present. */
2391 if (!pSel->Attr.n.u1Present)
2392 break;
2393 /* G bit must be set if any high limit bits are set. */
2394 if ((pSel->u32Limit & 0xfff00000) && !pSel->Attr.n.u1Granularity)
2395 break;
2396 /* G bit must be clear if any low limit bits are clear. */
2397 if ((pSel->u32Limit & 0x0fff) != 0x0fff && pSel->Attr.n.u1Granularity)
2398 break;
2399
2400 rc = true;
2401 } while (0);
2402 return rc;
2403}
2404
2405
2406/**
2407 * Checks if the stack selector (SS) is suitable for execution
2408 * within VMX when unrestricted execution isn't available.
2409 *
2410 * @returns true if selector is suitable for VMX, otherwise
2411 * false.
2412 * @param pSel Pointer to the selector to check (SS).
2413 */
2414static bool hmR3IsStackSelectorOkForVmx(PCPUMSELREG pSel)
2415{
2416 bool rc = false;
2417
2418 /* If attributes are all zero, consider the segment unusable and therefore OK.
2419 * This logic must be in sync with HMVMXR0.cpp!
2420 */
2421 if (!pSel->Attr.u)
2422 return true;
2423
2424 do
2425 {
2426 /* Segment must be accessed. */
2427 if (!(pSel->Attr.u & X86_SEL_TYPE_ACCESSED))
2428 break;
2429 /* Segment must be writable. */
2430 if (!(pSel->Attr.u & X86_SEL_TYPE_WRITE))
2431 break;
2432 /* Segment must not be a code segment. */
2433 if (pSel->Attr.u & X86_SEL_TYPE_CODE)
2434 break;
2435 /* The S bit must be set. */
2436 if (!pSel->Attr.n.u1DescType)
2437 break;
2438 /* DPL must equal RPL. */
2439 if (pSel->Attr.n.u2Dpl != (pSel->Sel & X86_SEL_RPL))
2440 break;
2441 /* Segment must be present. */
2442 if (!pSel->Attr.n.u1Present)
2443 break;
2444 /* G bit must be set if any high limit bits are set. */
2445 if ((pSel->u32Limit & 0xfff00000) && !pSel->Attr.n.u1Granularity)
2446 break;
2447 /* G bit must be clear if any low limit bits are clear. */
2448 if ((pSel->u32Limit & 0x0fff) != 0x0fff && pSel->Attr.n.u1Granularity)
2449 break;
2450
2451 rc = true;
2452 } while (0);
2453 return rc;
2454}
2455
2456
2457/**
2458 * Force execution of the current IO code in the recompiler.
2459 *
2460 * @returns VBox status code.
2461 * @param pVM Pointer to the VM.
2462 * @param pCtx Partial VM execution context.
2463 */
2464VMMR3_INT_DECL(int) HMR3EmulateIoBlock(PVM pVM, PCPUMCTX pCtx)
2465{
2466 PVMCPU pVCpu = VMMGetCpu(pVM);
2467
2468 Assert(HMIsEnabled(pVM));
2469 Log(("HMR3EmulateIoBlock\n"));
2470
2471 /* This is primarily intended to speed up Grub, so we don't care about paged protected mode. */
2472 if (HMCanEmulateIoBlockEx(pCtx))
2473 {
2474 Log(("HMR3EmulateIoBlock -> enabled\n"));
2475 pVCpu->hm.s.EmulateIoBlock.fEnabled = true;
2476 pVCpu->hm.s.EmulateIoBlock.GCPtrFunctionEip = pCtx->rip;
2477 pVCpu->hm.s.EmulateIoBlock.cr0 = pCtx->cr0;
2478 return VINF_EM_RESCHEDULE_REM;
2479 }
2480 return VINF_SUCCESS;
2481}
2482
2483
2484/**
2485 * Checks if we can currently use hardware accelerated raw mode.
2486 *
2487 * @returns true if we can currently use hardware acceleration, otherwise false.
2488 * @param pVM Pointer to the VM.
2489 * @param pCtx Partial VM execution context.
2490 */
2491VMMR3DECL(bool) HMR3CanExecuteGuest(PVM pVM, PCPUMCTX pCtx)
2492{
2493 PVMCPU pVCpu = VMMGetCpu(pVM);
2494
2495 Assert(HMIsEnabled(pVM));
2496
2497 /* If we're still executing the IO code, then return false. */
2498 if ( RT_UNLIKELY(pVCpu->hm.s.EmulateIoBlock.fEnabled)
2499 && pCtx->rip < pVCpu->hm.s.EmulateIoBlock.GCPtrFunctionEip + 0x200
2500 && pCtx->rip > pVCpu->hm.s.EmulateIoBlock.GCPtrFunctionEip - 0x200
2501 && pCtx->cr0 == pVCpu->hm.s.EmulateIoBlock.cr0)
2502 return false;
2503
2504 pVCpu->hm.s.EmulateIoBlock.fEnabled = false;
2505
2506 /* AMD-V supports real & protected mode with or without paging. */
2507 if (pVM->hm.s.svm.fEnabled)
2508 {
2509 pVCpu->hm.s.fActive = true;
2510 return true;
2511 }
2512
2513 pVCpu->hm.s.fActive = false;
2514
2515 /* Note! The context supplied by REM is partial. If we add more checks here, be sure to verify that REM provides this info! */
2516 Assert( (pVM->hm.s.vmx.fUnrestrictedGuest && !pVM->hm.s.vmx.pRealModeTSS)
2517 || (!pVM->hm.s.vmx.fUnrestrictedGuest && pVM->hm.s.vmx.pRealModeTSS));
2518
2519 bool fSupportsRealMode = pVM->hm.s.vmx.fUnrestrictedGuest || PDMVmmDevHeapIsEnabled(pVM);
2520 if (!pVM->hm.s.vmx.fUnrestrictedGuest)
2521 {
2522 /*
2523 * The VMM device heap is a requirement for emulating real mode or protected mode without paging with the unrestricted
2524 * guest execution feature i missing (VT-x only).
2525 */
2526 if (fSupportsRealMode)
2527 {
2528 if (CPUMIsGuestInRealModeEx(pCtx))
2529 {
2530 /* In V86 mode (VT-x or not), the CPU enforces real-mode compatible selector
2531 * bases and limits, i.e. limit must be 64K and base must be selector * 16.
2532 * If this is not true, we cannot execute real mode as V86 and have to fall
2533 * back to emulation.
2534 */
2535 if ( pCtx->cs.Sel != (pCtx->cs.u64Base >> 4)
2536 || pCtx->ds.Sel != (pCtx->ds.u64Base >> 4)
2537 || pCtx->es.Sel != (pCtx->es.u64Base >> 4)
2538 || pCtx->ss.Sel != (pCtx->ss.u64Base >> 4)
2539 || pCtx->fs.Sel != (pCtx->fs.u64Base >> 4)
2540 || pCtx->gs.Sel != (pCtx->gs.u64Base >> 4))
2541 {
2542 STAM_COUNTER_INC(&pVCpu->hm.s.StatVmxCheckBadRmSelBase);
2543 return false;
2544 }
2545 if ( (pCtx->cs.u32Limit != 0xffff)
2546 || (pCtx->ds.u32Limit != 0xffff)
2547 || (pCtx->es.u32Limit != 0xffff)
2548 || (pCtx->ss.u32Limit != 0xffff)
2549 || (pCtx->fs.u32Limit != 0xffff)
2550 || (pCtx->gs.u32Limit != 0xffff))
2551 {
2552 STAM_COUNTER_INC(&pVCpu->hm.s.StatVmxCheckBadRmSelLimit);
2553 return false;
2554 }
2555 STAM_COUNTER_INC(&pVCpu->hm.s.StatVmxCheckRmOk);
2556 }
2557 else
2558 {
2559 PGMMODE enmGuestMode = PGMGetGuestMode(pVCpu);
2560 /* Verify the requirements for executing code in protected
2561 mode. VT-x can't handle the CPU state right after a switch
2562 from real to protected mode. (all sorts of RPL & DPL assumptions) */
2563#if VBOX_WITH_OLD_VTX_CODE
2564 if ( pVCpu->hm.s.vmx.enmLastSeenGuestMode == PGMMODE_REAL
2565 && enmGuestMode >= PGMMODE_PROTECTED)
2566#else
2567 if (pVCpu->hm.s.vmx.fWasInRealMode)
2568#endif
2569 {
2570 //@todo: If guest is in V86 mode, these checks should be different!
2571#if VBOX_WITH_OLD_VTX_CODE
2572 if ( (pCtx->cs.Sel & X86_SEL_RPL)
2573 || (pCtx->ds.Sel & X86_SEL_RPL)
2574 || (pCtx->es.Sel & X86_SEL_RPL)
2575 || (pCtx->fs.Sel & X86_SEL_RPL)
2576 || (pCtx->gs.Sel & X86_SEL_RPL)
2577 || (pCtx->ss.Sel & X86_SEL_RPL))
2578 {
2579 STAM_COUNTER_INC(&pVCpu->hm.s.StatVmxCheckBadSel);
2580 return false;
2581 }
2582#else
2583 if ((pCtx->cs.Sel & X86_SEL_RPL) != (pCtx->ss.Sel & X86_SEL_RPL))
2584 {
2585 STAM_COUNTER_INC(&pVCpu->hm.s.StatVmxCheckBadRpl);
2586 return false;
2587 }
2588 if ( !hmR3IsCodeSelectorOkForVmx(&pCtx->cs, pCtx->ss.Attr.n.u2Dpl)
2589 || !hmR3IsDataSelectorOkForVmx(&pCtx->ds)
2590 || !hmR3IsDataSelectorOkForVmx(&pCtx->es)
2591 || !hmR3IsDataSelectorOkForVmx(&pCtx->fs)
2592 || !hmR3IsDataSelectorOkForVmx(&pCtx->gs)
2593 || !hmR3IsStackSelectorOkForVmx(&pCtx->ss))
2594 {
2595 STAM_COUNTER_INC(&pVCpu->hm.s.StatVmxCheckBadSel);
2596 return false;
2597 }
2598#endif
2599 }
2600 /* VT-x also chokes on invalid tr or ldtr selectors (minix) */
2601 if (pCtx->gdtr.cbGdt)
2602 {
2603 if (pCtx->tr.Sel > pCtx->gdtr.cbGdt)
2604 {
2605 STAM_COUNTER_INC(&pVCpu->hm.s.StatVmxCheckBadTr);
2606 return false;
2607 }
2608 else if (pCtx->ldtr.Sel > pCtx->gdtr.cbGdt)
2609 {
2610 STAM_COUNTER_INC(&pVCpu->hm.s.StatVmxCheckBadLdt);
2611 return false;
2612 }
2613 }
2614 STAM_COUNTER_INC(&pVCpu->hm.s.StatVmxCheckPmOk);
2615 }
2616 }
2617 else
2618 {
2619 if ( !CPUMIsGuestInLongModeEx(pCtx)
2620 && !pVM->hm.s.vmx.fUnrestrictedGuest)
2621 {
2622#ifdef VBOX_WITH_OLD_VTX_CODE
2623 /** @todo This should (probably) be set on every excursion to the REM,
2624 * however it's too risky right now. So, only apply it when we go
2625 * back to REM for real mode execution. (The XP hack below doesn't
2626 * work reliably without this.)
2627 * Update: Implemented in EM.cpp, see #ifdef EM_NOTIFY_HM. */
2628 for (uint32_t i = 0; i < pVM->cCpus; i++)
2629 pVM->aCpus[i].hm.s.fContextUseFlags |= HM_CHANGED_ALL_GUEST;
2630#endif
2631
2632 if ( !pVM->hm.s.fNestedPaging /* requires a fake PD for real *and* protected mode without paging - stored in the VMM device heap */
2633 || CPUMIsGuestInRealModeEx(pCtx)) /* requires a fake TSS for real mode - stored in the VMM device heap */
2634 return false;
2635
2636 /* Too early for VT-x; Solaris guests will fail with a guru meditation otherwise; same for XP. */
2637 if (pCtx->idtr.pIdt == 0 || pCtx->idtr.cbIdt == 0 || pCtx->tr.Sel == 0)
2638 return false;
2639
2640 /* The guest is about to complete the switch to protected mode. Wait a bit longer. */
2641 /* Windows XP; switch to protected mode; all selectors are marked not present in the
2642 * hidden registers (possible recompiler bug; see load_seg_vm) */
2643 if (pCtx->cs.Attr.n.u1Present == 0)
2644 return false;
2645 if (pCtx->ss.Attr.n.u1Present == 0)
2646 return false;
2647
2648 /* Windows XP: possible same as above, but new recompiler requires new heuristics?
2649 VT-x doesn't seem to like something about the guest state and this stuff avoids it. */
2650 /** @todo This check is actually wrong, it doesn't take the direction of the
2651 * stack segment into account. But, it does the job for now. */
2652 if (pCtx->rsp >= pCtx->ss.u32Limit)
2653 return false;
2654#if 0
2655 if ( pCtx->cs.Sel >= pCtx->gdtr.cbGdt
2656 || pCtx->ss.Sel >= pCtx->gdtr.cbGdt
2657 || pCtx->ds.Sel >= pCtx->gdtr.cbGdt
2658 || pCtx->es.Sel >= pCtx->gdtr.cbGdt
2659 || pCtx->fs.Sel >= pCtx->gdtr.cbGdt
2660 || pCtx->gs.Sel >= pCtx->gdtr.cbGdt)
2661 return false;
2662#endif
2663 }
2664 }
2665 }
2666
2667 if (pVM->hm.s.vmx.fEnabled)
2668 {
2669 uint32_t mask;
2670
2671 /* if bit N is set in cr0_fixed0, then it must be set in the guest's cr0. */
2672 mask = (uint32_t)pVM->hm.s.vmx.msr.vmx_cr0_fixed0;
2673 /* Note: We ignore the NE bit here on purpose; see vmmr0\hmr0.cpp for details. */
2674 mask &= ~X86_CR0_NE;
2675
2676 if (fSupportsRealMode)
2677 {
2678 /* Note: We ignore the PE & PG bits here on purpose; we emulate real and protected mode without paging. */
2679 mask &= ~(X86_CR0_PG|X86_CR0_PE);
2680 }
2681 else
2682 {
2683 /* We support protected mode without paging using identity mapping. */
2684 mask &= ~X86_CR0_PG;
2685 }
2686 if ((pCtx->cr0 & mask) != mask)
2687 return false;
2688
2689 /* if bit N is cleared in cr0_fixed1, then it must be zero in the guest's cr0. */
2690 mask = (uint32_t)~pVM->hm.s.vmx.msr.vmx_cr0_fixed1;
2691 if ((pCtx->cr0 & mask) != 0)
2692 return false;
2693
2694 /* if bit N is set in cr4_fixed0, then it must be set in the guest's cr4. */
2695 mask = (uint32_t)pVM->hm.s.vmx.msr.vmx_cr4_fixed0;
2696 mask &= ~X86_CR4_VMXE;
2697 if ((pCtx->cr4 & mask) != mask)
2698 return false;
2699
2700 /* if bit N is cleared in cr4_fixed1, then it must be zero in the guest's cr4. */
2701 mask = (uint32_t)~pVM->hm.s.vmx.msr.vmx_cr4_fixed1;
2702 if ((pCtx->cr4 & mask) != 0)
2703 return false;
2704
2705 pVCpu->hm.s.fActive = true;
2706 return true;
2707 }
2708
2709 return false;
2710}
2711
2712
2713/**
2714 * Checks if we need to reschedule due to VMM device heap changes.
2715 *
2716 * @returns true if a reschedule is required, otherwise false.
2717 * @param pVM Pointer to the VM.
2718 * @param pCtx VM execution context.
2719 */
2720VMMR3_INT_DECL(bool) HMR3IsRescheduleRequired(PVM pVM, PCPUMCTX pCtx)
2721{
2722 /*
2723 * The VMM device heap is a requirement for emulating real-mode or protected-mode without paging
2724 * when the unrestricted guest execution feature is missing (VT-x only).
2725 */
2726#ifdef VBOX_WITH_OLD_VTX_CODE
2727 if ( pVM->hm.s.vmx.fEnabled
2728 && !pVM->hm.s.vmx.fUnrestrictedGuest
2729 && !CPUMIsGuestInPagedProtectedModeEx(pCtx)
2730 && !PDMVmmDevHeapIsEnabled(pVM)
2731 && (pVM->hm.s.fNestedPaging || CPUMIsGuestInRealModeEx(pCtx)))
2732 return true;
2733#else
2734 if ( pVM->hm.s.vmx.fEnabled
2735 && !pVM->hm.s.vmx.fUnrestrictedGuest
2736 && CPUMIsGuestInRealModeEx(pCtx)
2737 && !PDMVmmDevHeapIsEnabled(pVM))
2738 return true;
2739#endif
2740
2741 return false;
2742}
2743
2744
2745/**
2746 * Notification from EM about a rescheduling into hardware assisted execution
2747 * mode.
2748 *
2749 * @param pVCpu Pointer to the current VMCPU.
2750 */
2751VMMR3_INT_DECL(void) HMR3NotifyScheduled(PVMCPU pVCpu)
2752{
2753 pVCpu->hm.s.fContextUseFlags |= HM_CHANGED_ALL_GUEST;
2754}
2755
2756
2757/**
2758 * Notification from EM about returning from instruction emulation (REM / EM).
2759 *
2760 * @param pVCpu Pointer to the VMCPU.
2761 */
2762VMMR3_INT_DECL(void) HMR3NotifyEmulated(PVMCPU pVCpu)
2763{
2764 pVCpu->hm.s.fContextUseFlags |= HM_CHANGED_ALL_GUEST;
2765}
2766
2767
2768/**
2769 * Checks if we are currently using hardware accelerated raw mode.
2770 *
2771 * @returns true if hardware acceleration is being used, otherwise false.
2772 * @param pVCpu Pointer to the VMCPU.
2773 */
2774VMMR3_INT_DECL(bool) HMR3IsActive(PVMCPU pVCpu)
2775{
2776 return pVCpu->hm.s.fActive;
2777}
2778
2779
2780/**
2781 * External interface for querying whether hardware accelerated raw mode is
2782 * enabled.
2783 *
2784 * @returns true if nested paging is being used, otherwise false.
2785 * @param pUVM The user mode VM handle.
2786 * @sa HMIsEnabled, HMIsEnabledNotMacro.
2787 */
2788VMMR3DECL(bool) HMR3IsEnabled(PUVM pUVM)
2789{
2790 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
2791 PVM pVM = pUVM->pVM;
2792 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
2793 return pVM->fHMEnabled; /* Don't use the macro as the GUI may query us very very early. */
2794}
2795
2796
2797/**
2798 * Checks if we are currently using nested paging.
2799 *
2800 * @returns true if nested paging is being used, otherwise false.
2801 * @param pUVM The user mode VM handle.
2802 */
2803VMMR3DECL(bool) HMR3IsNestedPagingActive(PUVM pUVM)
2804{
2805 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
2806 PVM pVM = pUVM->pVM;
2807 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
2808 return pVM->hm.s.fNestedPaging;
2809}
2810
2811
2812/**
2813 * Checks if we are currently using VPID in VT-x mode.
2814 *
2815 * @returns true if VPID is being used, otherwise false.
2816 * @param pUVM The user mode VM handle.
2817 */
2818VMMR3DECL(bool) HMR3IsVpidActive(PUVM pUVM)
2819{
2820 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
2821 PVM pVM = pUVM->pVM;
2822 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
2823 return pVM->hm.s.vmx.fVpid;
2824}
2825
2826
2827/**
2828 * Checks if we are currently using VT-x unrestricted execution,
2829 * aka UX.
2830 *
2831 * @returns true if UX is being used, otherwise false.
2832 * @param pUVM The user mode VM handle.
2833 */
2834VMMR3DECL(bool) HMR3IsUXActive(PUVM pUVM)
2835{
2836 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
2837 PVM pVM = pUVM->pVM;
2838 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
2839 return pVM->hm.s.vmx.fUnrestrictedGuest;
2840}
2841
2842
2843/**
2844 * Checks if internal events are pending. In that case we are not allowed to dispatch interrupts.
2845 *
2846 * @returns true if an internal event is pending, otherwise false.
2847 * @param pVM Pointer to the VM.
2848 */
2849VMMR3_INT_DECL(bool) HMR3IsEventPending(PVMCPU pVCpu)
2850{
2851 return HMIsEnabled(pVCpu->pVMR3) && pVCpu->hm.s.Event.fPending;
2852}
2853
2854
2855/**
2856 * Checks if the VMX-preemption timer is being used.
2857 *
2858 * @returns true if the VMX-preemption timer is being used, otherwise false.
2859 * @param pVM Pointer to the VM.
2860 */
2861VMMR3_INT_DECL(bool) HMR3IsVmxPreemptionTimerUsed(PVM pVM)
2862{
2863 return HMIsEnabled(pVM)
2864 && pVM->hm.s.vmx.fEnabled
2865 && pVM->hm.s.vmx.fUsePreemptTimer;
2866}
2867
2868
2869/**
2870 * Restart an I/O instruction that was refused in ring-0
2871 *
2872 * @returns Strict VBox status code. Informational status codes other than the one documented
2873 * here are to be treated as internal failure. Use IOM_SUCCESS() to check for success.
2874 * @retval VINF_SUCCESS Success.
2875 * @retval VINF_EM_FIRST-VINF_EM_LAST Success with some exceptions (see IOM_SUCCESS()), the
2876 * status code must be passed on to EM.
2877 * @retval VERR_NOT_FOUND if no pending I/O instruction.
2878 *
2879 * @param pVM Pointer to the VM.
2880 * @param pVCpu Pointer to the VMCPU.
2881 * @param pCtx Pointer to the guest CPU context.
2882 */
2883VMMR3_INT_DECL(VBOXSTRICTRC) HMR3RestartPendingIOInstr(PVM pVM, PVMCPU pVCpu, PCPUMCTX pCtx)
2884{
2885 HMPENDINGIO enmType = pVCpu->hm.s.PendingIO.enmType;
2886
2887 pVCpu->hm.s.PendingIO.enmType = HMPENDINGIO_INVALID;
2888
2889 if ( pVCpu->hm.s.PendingIO.GCPtrRip != pCtx->rip
2890 || enmType == HMPENDINGIO_INVALID)
2891 return VERR_NOT_FOUND;
2892
2893 VBOXSTRICTRC rcStrict;
2894 switch (enmType)
2895 {
2896 case HMPENDINGIO_PORT_READ:
2897 {
2898 uint32_t uAndVal = pVCpu->hm.s.PendingIO.s.Port.uAndVal;
2899 uint32_t u32Val = 0;
2900
2901 rcStrict = IOMIOPortRead(pVM, pVCpu, pVCpu->hm.s.PendingIO.s.Port.uPort,
2902 &u32Val,
2903 pVCpu->hm.s.PendingIO.s.Port.cbSize);
2904 if (IOM_SUCCESS(rcStrict))
2905 {
2906 /* Write back to the EAX register. */
2907 pCtx->eax = (pCtx->eax & ~uAndVal) | (u32Val & uAndVal);
2908 pCtx->rip = pVCpu->hm.s.PendingIO.GCPtrRipNext;
2909 }
2910 break;
2911 }
2912
2913 case HMPENDINGIO_PORT_WRITE:
2914 rcStrict = IOMIOPortWrite(pVM, pVCpu, pVCpu->hm.s.PendingIO.s.Port.uPort,
2915 pCtx->eax & pVCpu->hm.s.PendingIO.s.Port.uAndVal,
2916 pVCpu->hm.s.PendingIO.s.Port.cbSize);
2917 if (IOM_SUCCESS(rcStrict))
2918 pCtx->rip = pVCpu->hm.s.PendingIO.GCPtrRipNext;
2919 break;
2920
2921 default:
2922 AssertLogRelFailedReturn(VERR_HM_UNKNOWN_IO_INSTRUCTION);
2923 }
2924
2925 return rcStrict;
2926}
2927
2928
2929/**
2930 * Check fatal VT-x/AMD-V error and produce some meaningful
2931 * log release message.
2932 *
2933 * @param pVM Pointer to the VM.
2934 * @param iStatusCode VBox status code.
2935 */
2936VMMR3_INT_DECL(void) HMR3CheckError(PVM pVM, int iStatusCode)
2937{
2938 for (VMCPUID i = 0; i < pVM->cCpus; i++)
2939 {
2940 switch (iStatusCode)
2941 {
2942 case VERR_VMX_INVALID_VMCS_FIELD:
2943 break;
2944
2945 case VERR_VMX_INVALID_VMCS_PTR:
2946 LogRel(("HM: VERR_VMX_INVALID_VMCS_PTR:\n"));
2947 LogRel(("HM: CPU%d Current pointer %RGp vs %RGp\n", i, pVM->aCpus[i].hm.s.vmx.LastError.u64VMCSPhys, pVM->aCpus[i].hm.s.vmx.HCPhysVmcs));
2948 LogRel(("HM: CPU%d Current VMCS version %x\n", i, pVM->aCpus[i].hm.s.vmx.LastError.u32VMCSRevision));
2949 LogRel(("HM: CPU%d Entered Cpu %d\n", i, pVM->aCpus[i].hm.s.vmx.LastError.idEnteredCpu));
2950 LogRel(("HM: CPU%d Current Cpu %d\n", i, pVM->aCpus[i].hm.s.vmx.LastError.idCurrentCpu));
2951 break;
2952
2953 case VERR_VMX_UNABLE_TO_START_VM:
2954 LogRel(("HM: VERR_VMX_UNABLE_TO_START_VM:\n"));
2955 LogRel(("HM: CPU%d Instruction error %#x\n", i, pVM->aCpus[i].hm.s.vmx.LastError.u32InstrError));
2956 LogRel(("HM: CPU%d Exit reason %#x\n", i, pVM->aCpus[i].hm.s.vmx.LastError.u32ExitReason));
2957 if (pVM->aCpus[i].hm.s.vmx.LastError.u32InstrError == VMX_ERROR_VMENTRY_INVALID_CONTROL_FIELDS)
2958 {
2959 LogRel(("HM: CPU%d PinCtls %#RX32\n", i, pVM->aCpus[i].hm.s.vmx.u32PinCtls));
2960 LogRel(("HM: CPU%d ProcCtls %#RX32\n", i, pVM->aCpus[i].hm.s.vmx.u32ProcCtls));
2961 LogRel(("HM: CPU%d ProcCtls2 %#RX32\n", i, pVM->aCpus[i].hm.s.vmx.u32ProcCtls2));
2962 LogRel(("HM: CPU%d EntryCtls %#RX32\n", i, pVM->aCpus[i].hm.s.vmx.u32EntryCtls));
2963 LogRel(("HM: CPU%d ExitCtls %#RX32\n", i, pVM->aCpus[i].hm.s.vmx.u32ExitCtls));
2964 LogRel(("HM: CPU%d MSRBitmapPhys %#RHp\n", i, pVM->aCpus[i].hm.s.vmx.HCPhysMsrBitmap));
2965#ifdef VBOX_WITH_AUTO_MSR_LOAD_RESTORE
2966 LogRel(("HM: CPU%d GuestMSRPhys %#RHp\n", i, pVM->aCpus[i].hm.s.vmx.HCPhysGuestMsr));
2967 LogRel(("HM: CPU%d HostMsrPhys %#RHp\n", i, pVM->aCpus[i].hm.s.vmx.HCPhysHostMsr));
2968 LogRel(("HM: CPU%d cGuestMSRs %u\n", i, pVM->aCpus[i].hm.s.vmx.cGuestMsrs));
2969#endif
2970 }
2971 /** @todo Log VM-entry event injection control fields
2972 * VMX_VMCS_CTRL_ENTRY_IRQ_INFO, VMX_VMCS_CTRL_ENTRY_EXCEPTION_ERRCODE
2973 * and VMX_VMCS_CTRL_ENTRY_INSTR_LENGTH from the VMCS. */
2974 break;
2975
2976 case VERR_VMX_INVALID_VMXON_PTR:
2977 break;
2978
2979 case VERR_VMX_UNEXPECTED_EXIT_CODE:
2980 case VERR_SVM_UNKNOWN_EXIT:
2981 case VERR_SVM_UNEXPECTED_EXIT:
2982 case VERR_SVM_UNEXPECTED_PATCH_TYPE:
2983 case VERR_SVM_UNEXPECTED_XCPT_EXIT:
2984 LogRel(("HM: CPU%d HM error %#x\n", i, pVM->aCpus[i].hm.s.u32HMError));
2985 break;
2986 }
2987 }
2988
2989 if (iStatusCode == VERR_VMX_UNABLE_TO_START_VM)
2990 {
2991 LogRel(("VERR_VMX_UNABLE_TO_START_VM: VM-entry allowed %x\n", pVM->hm.s.vmx.msr.vmx_entry.n.allowed1));
2992 LogRel(("VERR_VMX_UNABLE_TO_START_VM: VM-entry disallowed %x\n", pVM->hm.s.vmx.msr.vmx_entry.n.disallowed0));
2993 }
2994}
2995
2996
2997/**
2998 * Execute state save operation.
2999 *
3000 * @returns VBox status code.
3001 * @param pVM Pointer to the VM.
3002 * @param pSSM SSM operation handle.
3003 */
3004static DECLCALLBACK(int) hmR3Save(PVM pVM, PSSMHANDLE pSSM)
3005{
3006 int rc;
3007
3008 Log(("hmR3Save:\n"));
3009
3010 for (VMCPUID i = 0; i < pVM->cCpus; i++)
3011 {
3012 /*
3013 * Save the basic bits - fortunately all the other things can be resynced on load.
3014 */
3015 rc = SSMR3PutU32(pSSM, pVM->aCpus[i].hm.s.Event.fPending);
3016 AssertRCReturn(rc, rc);
3017 rc = SSMR3PutU32(pSSM, pVM->aCpus[i].hm.s.Event.u32ErrCode);
3018 AssertRCReturn(rc, rc);
3019 rc = SSMR3PutU64(pSSM, pVM->aCpus[i].hm.s.Event.u64IntrInfo);
3020 AssertRCReturn(rc, rc);
3021
3022#ifdef VBOX_WITH_OLD_VTX_CODE
3023 rc = SSMR3PutU32(pSSM, pVM->aCpus[i].hm.s.vmx.enmLastSeenGuestMode);
3024 AssertRCReturn(rc, rc);
3025 rc = SSMR3PutU32(pSSM, pVM->aCpus[i].hm.s.vmx.enmCurrGuestMode);
3026 AssertRCReturn(rc, rc);
3027 rc = SSMR3PutU32(pSSM, pVM->aCpus[i].hm.s.vmx.enmPrevGuestMode);
3028 AssertRCReturn(rc, rc);
3029#else
3030 //@todo: We only need to save pVM->aCpus[i].hm.s.vmx.fWasInRealMode and
3031 // perhaps not even that (the initial value of 'true' is safe).
3032 uint32_t u32Dummy = PGMMODE_REAL;
3033 rc = SSMR3PutU32(pSSM, u32Dummy);
3034 AssertRCReturn(rc, rc);
3035 rc = SSMR3PutU32(pSSM, u32Dummy);
3036 AssertRCReturn(rc, rc);
3037 rc = SSMR3PutU32(pSSM, u32Dummy);
3038 AssertRCReturn(rc, rc);
3039#endif
3040 }
3041#ifdef VBOX_HM_WITH_GUEST_PATCHING
3042 rc = SSMR3PutGCPtr(pSSM, pVM->hm.s.pGuestPatchMem);
3043 AssertRCReturn(rc, rc);
3044 rc = SSMR3PutGCPtr(pSSM, pVM->hm.s.pFreeGuestPatchMem);
3045 AssertRCReturn(rc, rc);
3046 rc = SSMR3PutU32(pSSM, pVM->hm.s.cbGuestPatchMem);
3047 AssertRCReturn(rc, rc);
3048
3049 /* Store all the guest patch records too. */
3050 rc = SSMR3PutU32(pSSM, pVM->hm.s.cPatches);
3051 AssertRCReturn(rc, rc);
3052
3053 for (unsigned i = 0; i < pVM->hm.s.cPatches; i++)
3054 {
3055 PHMTPRPATCH pPatch = &pVM->hm.s.aPatches[i];
3056
3057 rc = SSMR3PutU32(pSSM, pPatch->Core.Key);
3058 AssertRCReturn(rc, rc);
3059
3060 rc = SSMR3PutMem(pSSM, pPatch->aOpcode, sizeof(pPatch->aOpcode));
3061 AssertRCReturn(rc, rc);
3062
3063 rc = SSMR3PutU32(pSSM, pPatch->cbOp);
3064 AssertRCReturn(rc, rc);
3065
3066 rc = SSMR3PutMem(pSSM, pPatch->aNewOpcode, sizeof(pPatch->aNewOpcode));
3067 AssertRCReturn(rc, rc);
3068
3069 rc = SSMR3PutU32(pSSM, pPatch->cbNewOp);
3070 AssertRCReturn(rc, rc);
3071
3072 AssertCompileSize(HMTPRINSTR, 4);
3073 rc = SSMR3PutU32(pSSM, (uint32_t)pPatch->enmType);
3074 AssertRCReturn(rc, rc);
3075
3076 rc = SSMR3PutU32(pSSM, pPatch->uSrcOperand);
3077 AssertRCReturn(rc, rc);
3078
3079 rc = SSMR3PutU32(pSSM, pPatch->uDstOperand);
3080 AssertRCReturn(rc, rc);
3081
3082 rc = SSMR3PutU32(pSSM, pPatch->pJumpTarget);
3083 AssertRCReturn(rc, rc);
3084
3085 rc = SSMR3PutU32(pSSM, pPatch->cFaults);
3086 AssertRCReturn(rc, rc);
3087 }
3088#endif
3089 return VINF_SUCCESS;
3090}
3091
3092
3093/**
3094 * Execute state load operation.
3095 *
3096 * @returns VBox status code.
3097 * @param pVM Pointer to the VM.
3098 * @param pSSM SSM operation handle.
3099 * @param uVersion Data layout version.
3100 * @param uPass The data pass.
3101 */
3102static DECLCALLBACK(int) hmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
3103{
3104 int rc;
3105
3106 Log(("hmR3Load:\n"));
3107 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
3108
3109 /*
3110 * Validate version.
3111 */
3112 if ( uVersion != HM_SSM_VERSION
3113 && uVersion != HM_SSM_VERSION_NO_PATCHING
3114 && uVersion != HM_SSM_VERSION_2_0_X)
3115 {
3116 AssertMsgFailed(("hmR3Load: Invalid version uVersion=%d!\n", uVersion));
3117 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
3118 }
3119 for (VMCPUID i = 0; i < pVM->cCpus; i++)
3120 {
3121 rc = SSMR3GetU32(pSSM, &pVM->aCpus[i].hm.s.Event.fPending);
3122 AssertRCReturn(rc, rc);
3123 rc = SSMR3GetU32(pSSM, &pVM->aCpus[i].hm.s.Event.u32ErrCode);
3124 AssertRCReturn(rc, rc);
3125 rc = SSMR3GetU64(pSSM, &pVM->aCpus[i].hm.s.Event.u64IntrInfo);
3126 AssertRCReturn(rc, rc);
3127
3128 if (uVersion >= HM_SSM_VERSION_NO_PATCHING)
3129 {
3130 uint32_t val;
3131
3132#ifdef VBOX_WITH_OLD_VTX_CODE
3133 rc = SSMR3GetU32(pSSM, &val);
3134 AssertRCReturn(rc, rc);
3135 pVM->aCpus[i].hm.s.vmx.enmLastSeenGuestMode = (PGMMODE)val;
3136
3137 rc = SSMR3GetU32(pSSM, &val);
3138 AssertRCReturn(rc, rc);
3139 pVM->aCpus[i].hm.s.vmx.enmCurrGuestMode = (PGMMODE)val;
3140
3141 rc = SSMR3GetU32(pSSM, &val);
3142 AssertRCReturn(rc, rc);
3143 pVM->aCpus[i].hm.s.vmx.enmPrevGuestMode = (PGMMODE)val;
3144#else
3145 //@todo: See note above re saving enmLastSeenGuestMode
3146 rc = SSMR3GetU32(pSSM, &val);
3147 AssertRCReturn(rc, rc);
3148 rc = SSMR3GetU32(pSSM, &val);
3149 AssertRCReturn(rc, rc);
3150 rc = SSMR3GetU32(pSSM, &val);
3151 AssertRCReturn(rc, rc);
3152#endif
3153 }
3154 }
3155#ifdef VBOX_HM_WITH_GUEST_PATCHING
3156 if (uVersion > HM_SSM_VERSION_NO_PATCHING)
3157 {
3158 rc = SSMR3GetGCPtr(pSSM, &pVM->hm.s.pGuestPatchMem);
3159 AssertRCReturn(rc, rc);
3160 rc = SSMR3GetGCPtr(pSSM, &pVM->hm.s.pFreeGuestPatchMem);
3161 AssertRCReturn(rc, rc);
3162 rc = SSMR3GetU32(pSSM, &pVM->hm.s.cbGuestPatchMem);
3163 AssertRCReturn(rc, rc);
3164
3165 /* Fetch all TPR patch records. */
3166 rc = SSMR3GetU32(pSSM, &pVM->hm.s.cPatches);
3167 AssertRCReturn(rc, rc);
3168
3169 for (unsigned i = 0; i < pVM->hm.s.cPatches; i++)
3170 {
3171 PHMTPRPATCH pPatch = &pVM->hm.s.aPatches[i];
3172
3173 rc = SSMR3GetU32(pSSM, &pPatch->Core.Key);
3174 AssertRCReturn(rc, rc);
3175
3176 rc = SSMR3GetMem(pSSM, pPatch->aOpcode, sizeof(pPatch->aOpcode));
3177 AssertRCReturn(rc, rc);
3178
3179 rc = SSMR3GetU32(pSSM, &pPatch->cbOp);
3180 AssertRCReturn(rc, rc);
3181
3182 rc = SSMR3GetMem(pSSM, pPatch->aNewOpcode, sizeof(pPatch->aNewOpcode));
3183 AssertRCReturn(rc, rc);
3184
3185 rc = SSMR3GetU32(pSSM, &pPatch->cbNewOp);
3186 AssertRCReturn(rc, rc);
3187
3188 rc = SSMR3GetU32(pSSM, (uint32_t *)&pPatch->enmType);
3189 AssertRCReturn(rc, rc);
3190
3191 if (pPatch->enmType == HMTPRINSTR_JUMP_REPLACEMENT)
3192 pVM->hm.s.fTPRPatchingActive = true;
3193
3194 Assert(pPatch->enmType == HMTPRINSTR_JUMP_REPLACEMENT || pVM->hm.s.fTPRPatchingActive == false);
3195
3196 rc = SSMR3GetU32(pSSM, &pPatch->uSrcOperand);
3197 AssertRCReturn(rc, rc);
3198
3199 rc = SSMR3GetU32(pSSM, &pPatch->uDstOperand);
3200 AssertRCReturn(rc, rc);
3201
3202 rc = SSMR3GetU32(pSSM, &pPatch->cFaults);
3203 AssertRCReturn(rc, rc);
3204
3205 rc = SSMR3GetU32(pSSM, &pPatch->pJumpTarget);
3206 AssertRCReturn(rc, rc);
3207
3208 Log(("hmR3Load: patch %d\n", i));
3209 Log(("Key = %x\n", pPatch->Core.Key));
3210 Log(("cbOp = %d\n", pPatch->cbOp));
3211 Log(("cbNewOp = %d\n", pPatch->cbNewOp));
3212 Log(("type = %d\n", pPatch->enmType));
3213 Log(("srcop = %d\n", pPatch->uSrcOperand));
3214 Log(("dstop = %d\n", pPatch->uDstOperand));
3215 Log(("cFaults = %d\n", pPatch->cFaults));
3216 Log(("target = %x\n", pPatch->pJumpTarget));
3217 rc = RTAvloU32Insert(&pVM->hm.s.PatchTree, &pPatch->Core);
3218 AssertRC(rc);
3219 }
3220 }
3221#endif
3222
3223 return VINF_SUCCESS;
3224}
3225
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