VirtualBox

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

Last change on this file since 76734 was 76678, checked in by vboxsync, 6 years ago

Port r124260, r124263, r124271, r124273, r124277, r124278, r124279, r124284, r124285, r124286, r124287, r124288, r124289 and r124290 (Ported fixes over from 5.2, see bugref:9179 for more information)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 162.0 KB
Line 
1/* $Id: HM.cpp 76678 2019-01-07 13:48:16Z vboxsync $ */
2/** @file
3 * HM - Intel/AMD VM Hardware Support Manager.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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/** @page pg_hm HM - Hardware Assisted Virtualization Manager
19 *
20 * The HM manages guest execution using the VT-x and AMD-V CPU hardware
21 * extensions.
22 *
23 * {summary of what HM does}
24 *
25 * Hardware assisted virtualization manager was originally abbreviated HWACCM,
26 * however that was cumbersome to write and parse for such a central component,
27 * so it was shortened to HM when refactoring the code in the 4.3 development
28 * cycle.
29 *
30 * {add sections with more details}
31 *
32 * @sa @ref grp_hm
33 */
34
35
36/*********************************************************************************************************************************
37* Header Files *
38*********************************************************************************************************************************/
39#define LOG_GROUP LOG_GROUP_HM
40#define VMCPU_INCL_CPUM_GST_CTX
41#include <VBox/vmm/cpum.h>
42#include <VBox/vmm/stam.h>
43#include <VBox/vmm/mm.h>
44#include <VBox/vmm/em.h>
45#include <VBox/vmm/pdmapi.h>
46#include <VBox/vmm/pgm.h>
47#include <VBox/vmm/ssm.h>
48#include <VBox/vmm/gim.h>
49#include <VBox/vmm/trpm.h>
50#include <VBox/vmm/dbgf.h>
51#include <VBox/vmm/iom.h>
52#include <VBox/vmm/iem.h>
53#include <VBox/vmm/patm.h>
54#include <VBox/vmm/csam.h>
55#include <VBox/vmm/selm.h>
56#include <VBox/vmm/nem.h>
57#ifdef VBOX_WITH_REM
58# include <VBox/vmm/rem.h>
59#endif
60#include <VBox/vmm/hm_vmx.h>
61#include <VBox/vmm/hm_svm.h>
62#include "HMInternal.h"
63#include <VBox/vmm/vm.h>
64#include <VBox/vmm/uvm.h>
65#include <VBox/err.h>
66#include <VBox/param.h>
67
68#include <iprt/assert.h>
69#include <VBox/log.h>
70#include <iprt/asm.h>
71#include <iprt/asm-amd64-x86.h>
72#include <iprt/env.h>
73#include <iprt/thread.h>
74
75
76/*********************************************************************************************************************************
77* Global Variables *
78*********************************************************************************************************************************/
79#define EXIT_REASON(def, val, str) #def " - " #val " - " str
80#define EXIT_REASON_NIL() NULL
81/** Exit reason descriptions for VT-x, used to describe statistics and exit
82 * history. */
83static const char * const g_apszVmxExitReasons[MAX_EXITREASON_STAT] =
84{
85 EXIT_REASON(VMX_EXIT_XCPT_OR_NMI , 0, "Exception or non-maskable interrupt (NMI)."),
86 EXIT_REASON(VMX_EXIT_EXT_INT , 1, "External interrupt."),
87 EXIT_REASON(VMX_EXIT_TRIPLE_FAULT , 2, "Triple fault."),
88 EXIT_REASON(VMX_EXIT_INIT_SIGNAL , 3, "INIT signal."),
89 EXIT_REASON(VMX_EXIT_SIPI , 4, "Start-up IPI (SIPI)."),
90 EXIT_REASON(VMX_EXIT_IO_SMI_IRQ , 5, "I/O system-management interrupt (SMI)."),
91 EXIT_REASON(VMX_EXIT_SMI_IRQ , 6, "Other SMI."),
92 EXIT_REASON(VMX_EXIT_INT_WINDOW , 7, "Interrupt window."),
93 EXIT_REASON(VMX_EXIT_NMI_WINDOW , 8, "NMI window."),
94 EXIT_REASON(VMX_EXIT_TASK_SWITCH , 9, "Task switch."),
95 EXIT_REASON(VMX_EXIT_CPUID , 10, "CPUID instruction."),
96 EXIT_REASON(VMX_EXIT_GETSEC , 11, "GETSEC instrunction."),
97 EXIT_REASON(VMX_EXIT_HLT , 12, "HLT instruction."),
98 EXIT_REASON(VMX_EXIT_INVD , 13, "INVD instruction."),
99 EXIT_REASON(VMX_EXIT_INVLPG , 14, "INVLPG instruction."),
100 EXIT_REASON(VMX_EXIT_RDPMC , 15, "RDPMCinstruction."),
101 EXIT_REASON(VMX_EXIT_RDTSC , 16, "RDTSC instruction."),
102 EXIT_REASON(VMX_EXIT_RSM , 17, "RSM instruction in SMM."),
103 EXIT_REASON(VMX_EXIT_VMCALL , 18, "VMCALL instruction."),
104 EXIT_REASON(VMX_EXIT_VMCLEAR , 19, "VMCLEAR instruction."),
105 EXIT_REASON(VMX_EXIT_VMLAUNCH , 20, "VMLAUNCH instruction."),
106 EXIT_REASON(VMX_EXIT_VMPTRLD , 21, "VMPTRLD instruction."),
107 EXIT_REASON(VMX_EXIT_VMPTRST , 22, "VMPTRST instruction."),
108 EXIT_REASON(VMX_EXIT_VMREAD , 23, "VMREAD instruction."),
109 EXIT_REASON(VMX_EXIT_VMRESUME , 24, "VMRESUME instruction."),
110 EXIT_REASON(VMX_EXIT_VMWRITE , 25, "VMWRITE instruction."),
111 EXIT_REASON(VMX_EXIT_VMXOFF , 26, "VMXOFF instruction."),
112 EXIT_REASON(VMX_EXIT_VMXON , 27, "VMXON instruction."),
113 EXIT_REASON(VMX_EXIT_MOV_CRX , 28, "Control-register accesses."),
114 EXIT_REASON(VMX_EXIT_MOV_DRX , 29, "Debug-register accesses."),
115 EXIT_REASON(VMX_EXIT_PORT_IO , 30, "I/O instruction."),
116 EXIT_REASON(VMX_EXIT_RDMSR , 31, "RDMSR instruction."),
117 EXIT_REASON(VMX_EXIT_WRMSR , 32, "WRMSR instruction."),
118 EXIT_REASON(VMX_EXIT_ERR_INVALID_GUEST_STATE, 33, "VM-entry failure due to invalid guest state."),
119 EXIT_REASON(VMX_EXIT_ERR_MSR_LOAD , 34, "VM-entry failure due to MSR loading."),
120 EXIT_REASON_NIL(),
121 EXIT_REASON(VMX_EXIT_MWAIT , 36, "MWAIT instruction."),
122 EXIT_REASON(VMX_EXIT_MTF , 37, "Monitor Trap Flag."),
123 EXIT_REASON_NIL(),
124 EXIT_REASON(VMX_EXIT_MONITOR , 39, "MONITOR instruction."),
125 EXIT_REASON(VMX_EXIT_PAUSE , 40, "PAUSE instruction."),
126 EXIT_REASON(VMX_EXIT_ERR_MACHINE_CHECK , 41, "VM-entry failure due to machine-check."),
127 EXIT_REASON_NIL(),
128 EXIT_REASON(VMX_EXIT_TPR_BELOW_THRESHOLD , 43, "TPR below threshold (MOV to CR8)."),
129 EXIT_REASON(VMX_EXIT_APIC_ACCESS , 44, "APIC access."),
130 EXIT_REASON(VMX_EXIT_VIRTUALIZED_EOI , 45, "Virtualized EOI."),
131 EXIT_REASON(VMX_EXIT_GDTR_IDTR_ACCESS , 46, "GDTR/IDTR access using LGDT/SGDT/LIDT/SIDT."),
132 EXIT_REASON(VMX_EXIT_LDTR_TR_ACCESS , 47, "LDTR/TR access using LLDT/SLDT/LTR/STR."),
133 EXIT_REASON(VMX_EXIT_EPT_VIOLATION , 48, "EPT violation."),
134 EXIT_REASON(VMX_EXIT_EPT_MISCONFIG , 49, "EPT misconfiguration."),
135 EXIT_REASON(VMX_EXIT_INVEPT , 50, "INVEPT instruction."),
136 EXIT_REASON(VMX_EXIT_RDTSCP , 51, "RDTSCP instruction."),
137 EXIT_REASON(VMX_EXIT_PREEMPT_TIMER , 52, "VMX-preemption timer expired."),
138 EXIT_REASON(VMX_EXIT_INVVPID , 53, "INVVPID instruction."),
139 EXIT_REASON(VMX_EXIT_WBINVD , 54, "WBINVD instruction."),
140 EXIT_REASON(VMX_EXIT_XSETBV , 55, "XSETBV instruction."),
141 EXIT_REASON(VMX_EXIT_APIC_WRITE , 56, "APIC write completed to virtual-APIC page."),
142 EXIT_REASON(VMX_EXIT_RDRAND , 57, "RDRAND instruction."),
143 EXIT_REASON(VMX_EXIT_INVPCID , 58, "INVPCID instruction."),
144 EXIT_REASON(VMX_EXIT_VMFUNC , 59, "VMFUNC instruction."),
145 EXIT_REASON(VMX_EXIT_ENCLS , 60, "ENCLS instruction."),
146 EXIT_REASON(VMX_EXIT_RDSEED , 61, "RDSEED instruction."),
147 EXIT_REASON(VMX_EXIT_PML_FULL , 62, "Page-modification log full."),
148 EXIT_REASON(VMX_EXIT_XSAVES , 63, "XSAVES instruction."),
149 EXIT_REASON(VMX_EXIT_XRSTORS , 64, "XRSTORS instruction.")
150};
151/** Array index of the last valid VT-x exit reason. */
152#define MAX_EXITREASON_VTX 64
153
154/** A partial list of \#EXIT reason descriptions for AMD-V, used to describe
155 * statistics and exit history.
156 *
157 * @note AMD-V have annoyingly large gaps (e.g. \#NPF VMEXIT comes at 1024),
158 * this array doesn't contain the entire set of exit reasons, we
159 * handle them via hmSvmGetSpecialExitReasonDesc(). */
160static const char * const g_apszSvmExitReasons[MAX_EXITREASON_STAT] =
161{
162 EXIT_REASON(SVM_EXIT_READ_CR0 , 0, "Read CR0."),
163 EXIT_REASON(SVM_EXIT_READ_CR1 , 1, "Read CR1."),
164 EXIT_REASON(SVM_EXIT_READ_CR2 , 2, "Read CR2."),
165 EXIT_REASON(SVM_EXIT_READ_CR3 , 3, "Read CR3."),
166 EXIT_REASON(SVM_EXIT_READ_CR4 , 4, "Read CR4."),
167 EXIT_REASON(SVM_EXIT_READ_CR5 , 5, "Read CR5."),
168 EXIT_REASON(SVM_EXIT_READ_CR6 , 6, "Read CR6."),
169 EXIT_REASON(SVM_EXIT_READ_CR7 , 7, "Read CR7."),
170 EXIT_REASON(SVM_EXIT_READ_CR8 , 8, "Read CR8."),
171 EXIT_REASON(SVM_EXIT_READ_CR9 , 9, "Read CR9."),
172 EXIT_REASON(SVM_EXIT_READ_CR10 , 10, "Read CR10."),
173 EXIT_REASON(SVM_EXIT_READ_CR11 , 11, "Read CR11."),
174 EXIT_REASON(SVM_EXIT_READ_CR12 , 12, "Read CR12."),
175 EXIT_REASON(SVM_EXIT_READ_CR13 , 13, "Read CR13."),
176 EXIT_REASON(SVM_EXIT_READ_CR14 , 14, "Read CR14."),
177 EXIT_REASON(SVM_EXIT_READ_CR15 , 15, "Read CR15."),
178 EXIT_REASON(SVM_EXIT_WRITE_CR0 , 16, "Write CR0."),
179 EXIT_REASON(SVM_EXIT_WRITE_CR1 , 17, "Write CR1."),
180 EXIT_REASON(SVM_EXIT_WRITE_CR2 , 18, "Write CR2."),
181 EXIT_REASON(SVM_EXIT_WRITE_CR3 , 19, "Write CR3."),
182 EXIT_REASON(SVM_EXIT_WRITE_CR4 , 20, "Write CR4."),
183 EXIT_REASON(SVM_EXIT_WRITE_CR5 , 21, "Write CR5."),
184 EXIT_REASON(SVM_EXIT_WRITE_CR6 , 22, "Write CR6."),
185 EXIT_REASON(SVM_EXIT_WRITE_CR7 , 23, "Write CR7."),
186 EXIT_REASON(SVM_EXIT_WRITE_CR8 , 24, "Write CR8."),
187 EXIT_REASON(SVM_EXIT_WRITE_CR9 , 25, "Write CR9."),
188 EXIT_REASON(SVM_EXIT_WRITE_CR10 , 26, "Write CR10."),
189 EXIT_REASON(SVM_EXIT_WRITE_CR11 , 27, "Write CR11."),
190 EXIT_REASON(SVM_EXIT_WRITE_CR12 , 28, "Write CR12."),
191 EXIT_REASON(SVM_EXIT_WRITE_CR13 , 29, "Write CR13."),
192 EXIT_REASON(SVM_EXIT_WRITE_CR14 , 30, "Write CR14."),
193 EXIT_REASON(SVM_EXIT_WRITE_CR15 , 31, "Write CR15."),
194 EXIT_REASON(SVM_EXIT_READ_DR0 , 32, "Read DR0."),
195 EXIT_REASON(SVM_EXIT_READ_DR1 , 33, "Read DR1."),
196 EXIT_REASON(SVM_EXIT_READ_DR2 , 34, "Read DR2."),
197 EXIT_REASON(SVM_EXIT_READ_DR3 , 35, "Read DR3."),
198 EXIT_REASON(SVM_EXIT_READ_DR4 , 36, "Read DR4."),
199 EXIT_REASON(SVM_EXIT_READ_DR5 , 37, "Read DR5."),
200 EXIT_REASON(SVM_EXIT_READ_DR6 , 38, "Read DR6."),
201 EXIT_REASON(SVM_EXIT_READ_DR7 , 39, "Read DR7."),
202 EXIT_REASON(SVM_EXIT_READ_DR8 , 40, "Read DR8."),
203 EXIT_REASON(SVM_EXIT_READ_DR9 , 41, "Read DR9."),
204 EXIT_REASON(SVM_EXIT_READ_DR10 , 42, "Read DR10."),
205 EXIT_REASON(SVM_EXIT_READ_DR11 , 43, "Read DR11"),
206 EXIT_REASON(SVM_EXIT_READ_DR12 , 44, "Read DR12."),
207 EXIT_REASON(SVM_EXIT_READ_DR13 , 45, "Read DR13."),
208 EXIT_REASON(SVM_EXIT_READ_DR14 , 46, "Read DR14."),
209 EXIT_REASON(SVM_EXIT_READ_DR15 , 47, "Read DR15."),
210 EXIT_REASON(SVM_EXIT_WRITE_DR0 , 48, "Write DR0."),
211 EXIT_REASON(SVM_EXIT_WRITE_DR1 , 49, "Write DR1."),
212 EXIT_REASON(SVM_EXIT_WRITE_DR2 , 50, "Write DR2."),
213 EXIT_REASON(SVM_EXIT_WRITE_DR3 , 51, "Write DR3."),
214 EXIT_REASON(SVM_EXIT_WRITE_DR4 , 52, "Write DR4."),
215 EXIT_REASON(SVM_EXIT_WRITE_DR5 , 53, "Write DR5."),
216 EXIT_REASON(SVM_EXIT_WRITE_DR6 , 54, "Write DR6."),
217 EXIT_REASON(SVM_EXIT_WRITE_DR7 , 55, "Write DR7."),
218 EXIT_REASON(SVM_EXIT_WRITE_DR8 , 56, "Write DR8."),
219 EXIT_REASON(SVM_EXIT_WRITE_DR9 , 57, "Write DR9."),
220 EXIT_REASON(SVM_EXIT_WRITE_DR10 , 58, "Write DR10."),
221 EXIT_REASON(SVM_EXIT_WRITE_DR11 , 59, "Write DR11."),
222 EXIT_REASON(SVM_EXIT_WRITE_DR12 , 60, "Write DR12."),
223 EXIT_REASON(SVM_EXIT_WRITE_DR13 , 61, "Write DR13."),
224 EXIT_REASON(SVM_EXIT_WRITE_DR14 , 62, "Write DR14."),
225 EXIT_REASON(SVM_EXIT_WRITE_DR15 , 63, "Write DR15."),
226 EXIT_REASON(SVM_EXIT_XCPT_0 , 64, "Exception 0 (#DE)."),
227 EXIT_REASON(SVM_EXIT_XCPT_1 , 65, "Exception 1 (#DB)."),
228 EXIT_REASON(SVM_EXIT_XCPT_2 , 66, "Exception 2 (#NMI)."),
229 EXIT_REASON(SVM_EXIT_XCPT_3 , 67, "Exception 3 (#BP)."),
230 EXIT_REASON(SVM_EXIT_XCPT_4 , 68, "Exception 4 (#OF)."),
231 EXIT_REASON(SVM_EXIT_XCPT_5 , 69, "Exception 5 (#BR)."),
232 EXIT_REASON(SVM_EXIT_XCPT_6 , 70, "Exception 6 (#UD)."),
233 EXIT_REASON(SVM_EXIT_XCPT_7 , 71, "Exception 7 (#NM)."),
234 EXIT_REASON(SVM_EXIT_XCPT_8 , 72, "Exception 8 (#DF)."),
235 EXIT_REASON(SVM_EXIT_XCPT_9 , 73, "Exception 9 (#CO_SEG_OVERRUN)."),
236 EXIT_REASON(SVM_EXIT_XCPT_10 , 74, "Exception 10 (#TS)."),
237 EXIT_REASON(SVM_EXIT_XCPT_11 , 75, "Exception 11 (#NP)."),
238 EXIT_REASON(SVM_EXIT_XCPT_12 , 76, "Exception 12 (#SS)."),
239 EXIT_REASON(SVM_EXIT_XCPT_13 , 77, "Exception 13 (#GP)."),
240 EXIT_REASON(SVM_EXIT_XCPT_14 , 78, "Exception 14 (#PF)."),
241 EXIT_REASON(SVM_EXIT_XCPT_15 , 79, "Exception 15 (0x0f)."),
242 EXIT_REASON(SVM_EXIT_XCPT_16 , 80, "Exception 16 (#MF)."),
243 EXIT_REASON(SVM_EXIT_XCPT_17 , 81, "Exception 17 (#AC)."),
244 EXIT_REASON(SVM_EXIT_XCPT_18 , 82, "Exception 18 (#MC)."),
245 EXIT_REASON(SVM_EXIT_XCPT_19 , 83, "Exception 19 (#XF)."),
246 EXIT_REASON(SVM_EXIT_XCPT_20 , 84, "Exception 20 (#VE)."),
247 EXIT_REASON(SVM_EXIT_XCPT_21 , 85, "Exception 22 (0x15)."),
248 EXIT_REASON(SVM_EXIT_XCPT_22 , 86, "Exception 22 (0x16)."),
249 EXIT_REASON(SVM_EXIT_XCPT_23 , 87, "Exception 23 (0x17)."),
250 EXIT_REASON(SVM_EXIT_XCPT_24 , 88, "Exception 24 (0x18)."),
251 EXIT_REASON(SVM_EXIT_XCPT_25 , 89, "Exception 25 (0x19)."),
252 EXIT_REASON(SVM_EXIT_XCPT_26 , 90, "Exception 26 (0x1a)."),
253 EXIT_REASON(SVM_EXIT_XCPT_27 , 91, "Exception 27 (0x1b)."),
254 EXIT_REASON(SVM_EXIT_XCPT_28 , 92, "Exception 28 (0x1c)."),
255 EXIT_REASON(SVM_EXIT_XCPT_29 , 93, "Exception 29 (0x1d)."),
256 EXIT_REASON(SVM_EXIT_XCPT_30 , 94, "Exception 30 (#SX)."),
257 EXIT_REASON(SVM_EXIT_XCPT_31 , 95, "Exception 31 (0x1F)."),
258 EXIT_REASON(SVM_EXIT_INTR , 96, "Physical maskable interrupt (host)."),
259 EXIT_REASON(SVM_EXIT_NMI , 97, "Physical non-maskable interrupt (host)."),
260 EXIT_REASON(SVM_EXIT_SMI , 98, "System management interrupt (host)."),
261 EXIT_REASON(SVM_EXIT_INIT , 99, "Physical INIT signal (host)."),
262 EXIT_REASON(SVM_EXIT_VINTR , 100, "Virtual interrupt-window exit."),
263 EXIT_REASON(SVM_EXIT_CR0_SEL_WRITE, 101, "Selective CR0 Write (to bits other than CR0.TS and CR0.MP)."),
264 EXIT_REASON(SVM_EXIT_IDTR_READ , 102, "Read IDTR."),
265 EXIT_REASON(SVM_EXIT_GDTR_READ , 103, "Read GDTR."),
266 EXIT_REASON(SVM_EXIT_LDTR_READ , 104, "Read LDTR."),
267 EXIT_REASON(SVM_EXIT_TR_READ , 105, "Read TR."),
268 EXIT_REASON(SVM_EXIT_IDTR_WRITE , 106, "Write IDTR."),
269 EXIT_REASON(SVM_EXIT_GDTR_WRITE , 107, "Write GDTR."),
270 EXIT_REASON(SVM_EXIT_LDTR_WRITE , 108, "Write LDTR."),
271 EXIT_REASON(SVM_EXIT_TR_WRITE , 109, "Write TR."),
272 EXIT_REASON(SVM_EXIT_RDTSC , 110, "RDTSC instruction."),
273 EXIT_REASON(SVM_EXIT_RDPMC , 111, "RDPMC instruction."),
274 EXIT_REASON(SVM_EXIT_PUSHF , 112, "PUSHF instruction."),
275 EXIT_REASON(SVM_EXIT_POPF , 113, "POPF instruction."),
276 EXIT_REASON(SVM_EXIT_CPUID , 114, "CPUID instruction."),
277 EXIT_REASON(SVM_EXIT_RSM , 115, "RSM instruction."),
278 EXIT_REASON(SVM_EXIT_IRET , 116, "IRET instruction."),
279 EXIT_REASON(SVM_EXIT_SWINT , 117, "Software interrupt (INTn instructions)."),
280 EXIT_REASON(SVM_EXIT_INVD , 118, "INVD instruction."),
281 EXIT_REASON(SVM_EXIT_PAUSE , 119, "PAUSE instruction."),
282 EXIT_REASON(SVM_EXIT_HLT , 120, "HLT instruction."),
283 EXIT_REASON(SVM_EXIT_INVLPG , 121, "INVLPG instruction."),
284 EXIT_REASON(SVM_EXIT_INVLPGA , 122, "INVLPGA instruction."),
285 EXIT_REASON(SVM_EXIT_IOIO , 123, "IN/OUT/INS/OUTS instruction."),
286 EXIT_REASON(SVM_EXIT_MSR , 124, "RDMSR or WRMSR access to protected MSR."),
287 EXIT_REASON(SVM_EXIT_TASK_SWITCH , 125, "Task switch."),
288 EXIT_REASON(SVM_EXIT_FERR_FREEZE , 126, "FERR Freeze; CPU frozen in an x87/mmx instruction waiting for interrupt."),
289 EXIT_REASON(SVM_EXIT_SHUTDOWN , 127, "Shutdown."),
290 EXIT_REASON(SVM_EXIT_VMRUN , 128, "VMRUN instruction."),
291 EXIT_REASON(SVM_EXIT_VMMCALL , 129, "VMCALL instruction."),
292 EXIT_REASON(SVM_EXIT_VMLOAD , 130, "VMLOAD instruction."),
293 EXIT_REASON(SVM_EXIT_VMSAVE , 131, "VMSAVE instruction."),
294 EXIT_REASON(SVM_EXIT_STGI , 132, "STGI instruction."),
295 EXIT_REASON(SVM_EXIT_CLGI , 133, "CLGI instruction."),
296 EXIT_REASON(SVM_EXIT_SKINIT , 134, "SKINIT instruction."),
297 EXIT_REASON(SVM_EXIT_RDTSCP , 135, "RDTSCP instruction."),
298 EXIT_REASON(SVM_EXIT_ICEBP , 136, "ICEBP instruction."),
299 EXIT_REASON(SVM_EXIT_WBINVD , 137, "WBINVD instruction."),
300 EXIT_REASON(SVM_EXIT_MONITOR , 138, "MONITOR instruction."),
301 EXIT_REASON(SVM_EXIT_MWAIT , 139, "MWAIT instruction."),
302 EXIT_REASON(SVM_EXIT_MWAIT_ARMED , 140, "MWAIT instruction when armed."),
303 EXIT_REASON(SVM_EXIT_XSETBV , 141, "XSETBV instruction."),
304};
305/** Array index of the last valid AMD-V exit reason. */
306#define MAX_EXITREASON_AMDV 141
307
308/** Special exit reasons not covered in the array above. */
309#define SVM_EXIT_REASON_NPF EXIT_REASON(SVM_EXIT_NPF , 1024, "Nested Page Fault.")
310#define SVM_EXIT_REASON_AVIC_INCOMPLETE_IPI EXIT_REASON(SVM_EXIT_AVIC_INCOMPLETE_IPI, 1025, "AVIC - Incomplete IPI delivery.")
311#define SVM_EXIT_REASON_AVIC_NOACCEL EXIT_REASON(SVM_EXIT_AVIC_NOACCEL , 1026, "AVIC - Unhandled register.")
312
313/**
314 * Gets the SVM exit reason if it's one of the reasons not present in the @c
315 * g_apszSvmExitReasons array.
316 *
317 * @returns The exit reason or NULL if unknown.
318 * @param uExit The exit.
319 */
320DECLINLINE(const char *) hmSvmGetSpecialExitReasonDesc(uint16_t uExit)
321{
322 switch (uExit)
323 {
324 case SVM_EXIT_NPF: return SVM_EXIT_REASON_NPF;
325 case SVM_EXIT_AVIC_INCOMPLETE_IPI: return SVM_EXIT_REASON_AVIC_INCOMPLETE_IPI;
326 case SVM_EXIT_AVIC_NOACCEL: return SVM_EXIT_REASON_AVIC_NOACCEL;
327 }
328 return EXIT_REASON_NIL();
329}
330#undef EXIT_REASON_NIL
331#undef EXIT_REASON
332
333/** @def HMVMX_REPORT_FEAT
334 * Reports VT-x feature to the release log.
335 *
336 * @param a_uAllowed1 Mask of allowed-1 feature bits.
337 * @param a_uAllowed0 Mask of allowed-0 feature bits.
338 * @param a_StrDesc The description string to report.
339 * @param a_Featflag Mask of the feature to report.
340 */
341#define HMVMX_REPORT_FEAT(a_uAllowed1, a_uAllowed0, a_StrDesc, a_Featflag) \
342 do { \
343 if ((a_uAllowed1) & (a_Featflag)) \
344 { \
345 if ((a_uAllowed0) & (a_Featflag)) \
346 LogRel(("HM: " a_StrDesc " (must be set)\n")); \
347 else \
348 LogRel(("HM: " a_StrDesc "\n")); \
349 } \
350 else \
351 LogRel(("HM: " a_StrDesc " (must be cleared)\n")); \
352 } while (0)
353
354/** @def HMVMX_REPORT_ALLOWED_FEAT
355 * Reports an allowed VT-x feature to the release log.
356 *
357 * @param a_uAllowed1 Mask of allowed-1 feature bits.
358 * @param a_StrDesc The description string to report.
359 * @param a_FeatFlag Mask of the feature to report.
360 */
361#define HMVMX_REPORT_ALLOWED_FEAT(a_uAllowed1, a_StrDesc, a_FeatFlag) \
362 do { \
363 if ((a_uAllowed1) & (a_FeatFlag)) \
364 LogRel(("HM: " a_StrDesc "\n")); \
365 else \
366 LogRel(("HM: " a_StrDesc " not supported\n")); \
367 } while (0)
368
369/** @def HMVMX_REPORT_MSR_CAP
370 * Reports MSR feature capability.
371 *
372 * @param a_MsrCaps Mask of MSR feature bits.
373 * @param a_StrDesc The description string to report.
374 * @param a_fCap Mask of the feature to report.
375 */
376#define HMVMX_REPORT_MSR_CAP(a_MsrCaps, a_StrDesc, a_fCap) \
377 do { \
378 if ((a_MsrCaps) & (a_fCap)) \
379 LogRel(("HM: " a_StrDesc "\n")); \
380 } while (0)
381
382/** @def HMVMX_LOGREL_FEAT
383 * Dumps a feature flag from a bitmap of features to the release log.
384 *
385 * @param a_fVal The value of all the features.
386 * @param a_fMask The specific bitmask of the feature.
387 */
388#define HMVMX_LOGREL_FEAT(a_fVal, a_fMask) \
389 do { \
390 if ((a_fVal) & (a_fMask)) \
391 LogRel(("HM: %s\n", #a_fMask)); \
392 } while (0)
393
394
395/*********************************************************************************************************************************
396* Internal Functions *
397*********************************************************************************************************************************/
398static DECLCALLBACK(int) hmR3Save(PVM pVM, PSSMHANDLE pSSM);
399static DECLCALLBACK(int) hmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
400static DECLCALLBACK(void) hmR3InfoSvmNstGstVmcbCache(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
401static DECLCALLBACK(void) hmR3Info(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
402static DECLCALLBACK(void) hmR3InfoEventPending(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
403static int hmR3InitFinalizeR3(PVM pVM);
404static int hmR3InitFinalizeR0(PVM pVM);
405static int hmR3InitFinalizeR0Intel(PVM pVM);
406static int hmR3InitFinalizeR0Amd(PVM pVM);
407static int hmR3TermCPU(PVM pVM);
408
409
410
411/**
412 * Initializes the HM.
413 *
414 * This is the very first component to really do init after CFGM so that we can
415 * establish the predominant execution engine for the VM prior to initializing
416 * other modules. It takes care of NEM initialization if needed (HM disabled or
417 * not available in HW).
418 *
419 * If VT-x or AMD-V hardware isn't available, HM will try fall back on a native
420 * hypervisor API via NEM, and then back on raw-mode if that isn't available
421 * either. The fallback to raw-mode will not happen if /HM/HMForced is set
422 * (like for guest using SMP or 64-bit as well as for complicated guest like OS
423 * X, OS/2 and others).
424 *
425 * Note that a lot of the set up work is done in ring-0 and thus postponed till
426 * the ring-3 and ring-0 callback to HMR3InitCompleted.
427 *
428 * @returns VBox status code.
429 * @param pVM The cross context VM structure.
430 *
431 * @remarks Be careful with what we call here, since most of the VMM components
432 * are uninitialized.
433 */
434VMMR3_INT_DECL(int) HMR3Init(PVM pVM)
435{
436 LogFlow(("HMR3Init\n"));
437
438 /*
439 * Assert alignment and sizes.
440 */
441 AssertCompileMemberAlignment(VM, hm.s, 32);
442 AssertCompile(sizeof(pVM->hm.s) <= sizeof(pVM->hm.padding));
443
444 /*
445 * Register the saved state data unit.
446 */
447 int rc = SSMR3RegisterInternal(pVM, "HWACCM", 0, HM_SAVED_STATE_VERSION, sizeof(HM),
448 NULL, NULL, NULL,
449 NULL, hmR3Save, NULL,
450 NULL, hmR3Load, NULL);
451 if (RT_FAILURE(rc))
452 return rc;
453
454 /*
455 * Register info handlers.
456 */
457 rc = DBGFR3InfoRegisterInternalEx(pVM, "hm", "Dumps HM info.", hmR3Info, DBGFINFO_FLAGS_ALL_EMTS);
458 AssertRCReturn(rc, rc);
459
460 rc = DBGFR3InfoRegisterInternalEx(pVM, "hmeventpending", "Dumps the pending HM event.", hmR3InfoEventPending,
461 DBGFINFO_FLAGS_ALL_EMTS);
462 AssertRCReturn(rc, rc);
463
464 rc = DBGFR3InfoRegisterInternalEx(pVM, "svmvmcbcache", "Dumps the HM SVM nested-guest VMCB cache.",
465 hmR3InfoSvmNstGstVmcbCache, DBGFINFO_FLAGS_ALL_EMTS);
466 AssertRCReturn(rc, rc);
467
468 /*
469 * Read configuration.
470 */
471 PCFGMNODE pCfgHm = CFGMR3GetChild(CFGMR3GetRoot(pVM), "HM/");
472
473 /*
474 * Validate the HM settings.
475 */
476 rc = CFGMR3ValidateConfig(pCfgHm, "/HM/",
477 "HMForced"
478 "|UseNEMInstead"
479 "|FallbackToNEM"
480 "|EnableNestedPaging"
481 "|EnableUX"
482 "|EnableLargePages"
483 "|EnableVPID"
484 "|IBPBOnVMExit"
485 "|IBPBOnVMEntry"
486 "|SpecCtrlByHost"
487 "|L1DFlushOnSched"
488 "|L1DFlushOnVMEntry"
489 "|TPRPatchingEnabled"
490 "|64bitEnabled"
491 "|Exclusive"
492 "|MaxResumeLoops"
493 "|VmxPleGap"
494 "|VmxPleWindow"
495 "|UseVmxPreemptTimer"
496 "|SvmPauseFilter"
497 "|SvmPauseFilterThreshold"
498 "|SvmVirtVmsaveVmload"
499 "|SvmVGif"
500 "|LovelyMesaDrvWorkaround",
501 "" /* pszValidNodes */, "HM" /* pszWho */, 0 /* uInstance */);
502 if (RT_FAILURE(rc))
503 return rc;
504
505 /** @cfgm{/HM/HMForced, bool, false}
506 * Forces hardware virtualization, no falling back on raw-mode. HM must be
507 * enabled, i.e. /HMEnabled must be true. */
508 bool fHMForced;
509#ifdef VBOX_WITH_RAW_MODE
510 rc = CFGMR3QueryBoolDef(pCfgHm, "HMForced", &fHMForced, false);
511 AssertRCReturn(rc, rc);
512 AssertLogRelMsgReturn(!fHMForced || pVM->fHMEnabled, ("Configuration error: HM forced but not enabled!\n"),
513 VERR_INVALID_PARAMETER);
514# if defined(RT_OS_DARWIN)
515 if (pVM->fHMEnabled)
516 fHMForced = true;
517# endif
518 AssertLogRelMsgReturn(pVM->cCpus == 1 || pVM->fHMEnabled, ("Configuration error: SMP requires HM to be enabled!\n"),
519 VERR_INVALID_PARAMETER);
520 if (pVM->cCpus > 1)
521 fHMForced = true;
522#else /* !VBOX_WITH_RAW_MODE */
523 AssertRelease(pVM->fHMEnabled);
524 fHMForced = true;
525#endif /* !VBOX_WITH_RAW_MODE */
526
527 /** @cfgm{/HM/UseNEMInstead, bool, true}
528 * Don't use HM, use NEM instead. */
529 bool fUseNEMInstead = false;
530 rc = CFGMR3QueryBoolDef(pCfgHm, "UseNEMInstead", &fUseNEMInstead, false);
531 AssertRCReturn(rc, rc);
532 if (fUseNEMInstead && pVM->fHMEnabled)
533 {
534 LogRel(("HM: Setting fHMEnabled to false because fUseNEMInstead is set.\n"));
535 pVM->fHMEnabled = false;
536 }
537
538 /** @cfgm{/HM/FallbackToNEM, bool, true}
539 * Enables fallback on NEM. */
540 bool fFallbackToNEM = true;
541 rc = CFGMR3QueryBoolDef(pCfgHm, "FallbackToNEM", &fFallbackToNEM, true);
542 AssertRCReturn(rc, rc);
543
544 /** @cfgm{/HM/EnableNestedPaging, bool, false}
545 * Enables nested paging (aka extended page tables). */
546 rc = CFGMR3QueryBoolDef(pCfgHm, "EnableNestedPaging", &pVM->hm.s.fAllowNestedPaging, false);
547 AssertRCReturn(rc, rc);
548
549 /** @cfgm{/HM/EnableUX, bool, true}
550 * Enables the VT-x unrestricted execution feature. */
551 rc = CFGMR3QueryBoolDef(pCfgHm, "EnableUX", &pVM->hm.s.vmx.fAllowUnrestricted, true);
552 AssertRCReturn(rc, rc);
553
554 /** @cfgm{/HM/EnableLargePages, bool, false}
555 * Enables using large pages (2 MB) for guest memory, thus saving on (nested)
556 * page table walking and maybe better TLB hit rate in some cases. */
557 rc = CFGMR3QueryBoolDef(pCfgHm, "EnableLargePages", &pVM->hm.s.fLargePages, false);
558 AssertRCReturn(rc, rc);
559
560 /** @cfgm{/HM/EnableVPID, bool, false}
561 * Enables the VT-x VPID feature. */
562 rc = CFGMR3QueryBoolDef(pCfgHm, "EnableVPID", &pVM->hm.s.vmx.fAllowVpid, false);
563 AssertRCReturn(rc, rc);
564
565 /** @cfgm{/HM/TPRPatchingEnabled, bool, false}
566 * Enables TPR patching for 32-bit windows guests with IO-APIC. */
567 rc = CFGMR3QueryBoolDef(pCfgHm, "TPRPatchingEnabled", &pVM->hm.s.fTprPatchingAllowed, false);
568 AssertRCReturn(rc, rc);
569
570 /** @cfgm{/HM/64bitEnabled, bool, 32-bit:false, 64-bit:true}
571 * Enables AMD64 cpu features.
572 * On 32-bit hosts this isn't default and require host CPU support. 64-bit hosts
573 * already have the support. */
574#ifdef VBOX_ENABLE_64_BITS_GUESTS
575 rc = CFGMR3QueryBoolDef(pCfgHm, "64bitEnabled", &pVM->hm.s.fAllow64BitGuests, HC_ARCH_BITS == 64);
576 AssertLogRelRCReturn(rc, rc);
577#else
578 pVM->hm.s.fAllow64BitGuests = false;
579#endif
580
581 /** @cfgm{/HM/VmxPleGap, uint32_t, 0}
582 * The pause-filter exiting gap in TSC ticks. When the number of ticks between
583 * two successive PAUSE instructions exceeds VmxPleGap, the CPU considers the
584 * latest PAUSE instruction to be start of a new PAUSE loop.
585 */
586 rc = CFGMR3QueryU32Def(pCfgHm, "VmxPleGap", &pVM->hm.s.vmx.cPleGapTicks, 0);
587 AssertRCReturn(rc, rc);
588
589 /** @cfgm{/HM/VmxPleWindow, uint32_t, 0}
590 * The pause-filter exiting window in TSC ticks. When the number of ticks
591 * between the current PAUSE instruction and first PAUSE of a loop exceeds
592 * VmxPleWindow, a VM-exit is triggered.
593 *
594 * Setting VmxPleGap and VmxPleGap to 0 disables pause-filter exiting.
595 */
596 rc = CFGMR3QueryU32Def(pCfgHm, "VmxPleWindow", &pVM->hm.s.vmx.cPleWindowTicks, 0);
597 AssertRCReturn(rc, rc);
598
599 /** @cfgm{/HM/SvmPauseFilterCount, uint16_t, 0}
600 * A counter that is decrement each time a PAUSE instruction is executed by the
601 * guest. When the counter is 0, a \#VMEXIT is triggered.
602 *
603 * Setting SvmPauseFilterCount to 0 disables pause-filter exiting.
604 */
605 rc = CFGMR3QueryU16Def(pCfgHm, "SvmPauseFilter", &pVM->hm.s.svm.cPauseFilter, 0);
606 AssertRCReturn(rc, rc);
607
608 /** @cfgm{/HM/SvmPauseFilterThreshold, uint16_t, 0}
609 * The pause filter threshold in ticks. When the elapsed time (in ticks) between
610 * two successive PAUSE instructions exceeds SvmPauseFilterThreshold, the
611 * PauseFilter count is reset to its initial value. However, if PAUSE is
612 * executed PauseFilter times within PauseFilterThreshold ticks, a VM-exit will
613 * be triggered.
614 *
615 * Requires SvmPauseFilterCount to be non-zero for pause-filter threshold to be
616 * activated.
617 */
618 rc = CFGMR3QueryU16Def(pCfgHm, "SvmPauseFilterThreshold", &pVM->hm.s.svm.cPauseFilterThresholdTicks, 0);
619 AssertRCReturn(rc, rc);
620
621 /** @cfgm{/HM/SvmVirtVmsaveVmload, bool, true}
622 * Whether to make use of virtualized VMSAVE/VMLOAD feature of the CPU if it's
623 * available. */
624 rc = CFGMR3QueryBoolDef(pCfgHm, "SvmVirtVmsaveVmload", &pVM->hm.s.svm.fVirtVmsaveVmload, true);
625 AssertRCReturn(rc, rc);
626
627 /** @cfgm{/HM/SvmVGif, bool, true}
628 * Whether to make use of Virtual GIF (Global Interrupt Flag) feature of the CPU
629 * if it's available. */
630 rc = CFGMR3QueryBoolDef(pCfgHm, "SvmVGif", &pVM->hm.s.svm.fVGif, true);
631 AssertRCReturn(rc, rc);
632
633 /** @cfgm{/HM/Exclusive, bool}
634 * Determines the init method for AMD-V and VT-x. If set to true, HM will do a
635 * global init for each host CPU. If false, we do local init each time we wish
636 * to execute guest code.
637 *
638 * On Windows, default is false due to the higher risk of conflicts with other
639 * hypervisors.
640 *
641 * On Mac OS X, this setting is ignored since the code does not handle local
642 * init when it utilizes the OS provided VT-x function, SUPR0EnableVTx().
643 */
644#if defined(RT_OS_DARWIN)
645 pVM->hm.s.fGlobalInit = true;
646#else
647 rc = CFGMR3QueryBoolDef(pCfgHm, "Exclusive", &pVM->hm.s.fGlobalInit,
648# if defined(RT_OS_WINDOWS)
649 false
650# else
651 true
652# endif
653 );
654 AssertLogRelRCReturn(rc, rc);
655#endif
656
657 /** @cfgm{/HM/MaxResumeLoops, uint32_t}
658 * The number of times to resume guest execution before we forcibly return to
659 * ring-3. The return value of RTThreadPreemptIsPendingTrusty in ring-0
660 * determines the default value. */
661 rc = CFGMR3QueryU32Def(pCfgHm, "MaxResumeLoops", &pVM->hm.s.cMaxResumeLoops, 0 /* set by R0 later */);
662 AssertLogRelRCReturn(rc, rc);
663
664 /** @cfgm{/HM/UseVmxPreemptTimer, bool}
665 * Whether to make use of the VMX-preemption timer feature of the CPU if it's
666 * available. */
667 rc = CFGMR3QueryBoolDef(pCfgHm, "UseVmxPreemptTimer", &pVM->hm.s.vmx.fUsePreemptTimer, true);
668 AssertLogRelRCReturn(rc, rc);
669
670 /** @cfgm{/HM/IBPBOnVMExit, bool}
671 * Costly paranoia setting. */
672 rc = CFGMR3QueryBoolDef(pCfgHm, "IBPBOnVMExit", &pVM->hm.s.fIbpbOnVmExit, false);
673 AssertLogRelRCReturn(rc, rc);
674
675 /** @cfgm{/HM/IBPBOnVMEntry, bool}
676 * Costly paranoia setting. */
677 rc = CFGMR3QueryBoolDef(pCfgHm, "IBPBOnVMEntry", &pVM->hm.s.fIbpbOnVmEntry, false);
678 AssertLogRelRCReturn(rc, rc);
679
680 /** @cfgm{/HM/L1DFlushOnSched, bool, true}
681 * CVS-2018-3646 workaround, ignored on CPUs that aren't affected. */
682 rc = CFGMR3QueryBoolDef(pCfgHm, "L1DFlushOnSched", &pVM->hm.s.fL1dFlushOnSched, true);
683 AssertLogRelRCReturn(rc, rc);
684
685 /** @cfgm{/HM/L1DFlushOnVMEntry, bool}
686 * CVS-2018-3646 workaround, ignored on CPUs that aren't affected. */
687 rc = CFGMR3QueryBoolDef(pCfgHm, "L1DFlushOnVMEntry", &pVM->hm.s.fL1dFlushOnVmEntry, false);
688 AssertLogRelRCReturn(rc, rc);
689
690 /* Disable L1DFlushOnSched if L1DFlushOnVMEntry is enabled. */
691 if (pVM->hm.s.fL1dFlushOnVmEntry)
692 pVM->hm.s.fL1dFlushOnSched = false;
693
694 /** @cfgm{/HM/SpecCtrlByHost, bool}
695 * Another expensive paranoia setting. */
696 rc = CFGMR3QueryBoolDef(pCfgHm, "SpecCtrlByHost", &pVM->hm.s.fSpecCtrlByHost, false);
697 AssertLogRelRCReturn(rc, rc);
698
699 /** @cfgm{/HM/LovelyMesaDrvWorkaround,bool}
700 * Workaround for mesa vmsvga 3d driver making incorrect assumptions about
701 * the hypervisor it is running under. */
702 bool f;
703 rc = CFGMR3QueryBoolDef(pCfgHm, "LovelyMesaDrvWorkaround", &f, false);
704 AssertLogRelRCReturn(rc, rc);
705 for (VMCPUID i = 0; i < pVM->cCpus; i++)
706 pVM->aCpus[i].hm.s.fTrapXcptGpForLovelyMesaDrv = f;
707
708 /*
709 * Check if VT-x or AMD-v support according to the users wishes.
710 */
711 /** @todo SUPR3QueryVTCaps won't catch VERR_VMX_IN_VMX_ROOT_MODE or
712 * VERR_SVM_IN_USE. */
713 if (pVM->fHMEnabled)
714 {
715 uint32_t fCaps;
716 rc = SUPR3QueryVTCaps(&fCaps);
717 if (RT_SUCCESS(rc))
718 {
719 if (fCaps & SUPVTCAPS_AMD_V)
720 {
721 pVM->hm.s.svm.fSupported = true;
722 LogRel(("HM: HMR3Init: AMD-V%s\n", fCaps & SUPVTCAPS_NESTED_PAGING ? " w/ nested paging" : ""));
723 VM_SET_MAIN_EXECUTION_ENGINE(pVM, VM_EXEC_ENGINE_HW_VIRT);
724 }
725 else if (fCaps & SUPVTCAPS_VT_X)
726 {
727 const char *pszWhy;
728 rc = SUPR3QueryVTxSupported(&pszWhy);
729 if (RT_SUCCESS(rc))
730 {
731 pVM->hm.s.vmx.fSupported = true;
732 LogRel(("HM: HMR3Init: VT-x%s%s%s\n",
733 fCaps & SUPVTCAPS_NESTED_PAGING ? " w/ nested paging" : "",
734 fCaps & SUPVTCAPS_VTX_UNRESTRICTED_GUEST ? " and unrestricted guest execution" : "",
735 (fCaps & (SUPVTCAPS_NESTED_PAGING | SUPVTCAPS_VTX_UNRESTRICTED_GUEST)) ? " hw support" : ""));
736 VM_SET_MAIN_EXECUTION_ENGINE(pVM, VM_EXEC_ENGINE_HW_VIRT);
737 }
738 else
739 {
740 /*
741 * Before failing, try fallback to NEM if we're allowed to do that.
742 */
743 pVM->fHMEnabled = false;
744 Assert(pVM->bMainExecutionEngine == VM_EXEC_ENGINE_NOT_SET);
745 if (fFallbackToNEM)
746 {
747 LogRel(("HM: HMR3Init: Attempting fall back to NEM: The host kernel does not support VT-x - %s\n", pszWhy));
748 int rc2 = NEMR3Init(pVM, true /*fFallback*/, fHMForced);
749
750 ASMCompilerBarrier(); /* NEMR3Init may have changed bMainExecutionEngine. */
751 if ( RT_SUCCESS(rc2)
752 && pVM->bMainExecutionEngine != VM_EXEC_ENGINE_NOT_SET)
753 rc = VINF_SUCCESS;
754 }
755 if (RT_FAILURE(rc))
756 {
757 if (fHMForced)
758 return VMSetError(pVM, rc, RT_SRC_POS, "The host kernel does not support VT-x: %s\n", pszWhy);
759
760 /* Fall back to raw-mode. */
761 LogRel(("HM: HMR3Init: Falling back to raw-mode: The host kernel does not support VT-x - %s\n", pszWhy));
762 VM_SET_MAIN_EXECUTION_ENGINE(pVM, VM_EXEC_ENGINE_RAW_MODE);
763 }
764 }
765 }
766 else
767 AssertLogRelMsgFailedReturn(("SUPR3QueryVTCaps didn't return either AMD-V or VT-x flag set (%#x)!\n", fCaps),
768 VERR_INTERNAL_ERROR_5);
769
770 /*
771 * Do we require a little bit or raw-mode for 64-bit guest execution?
772 */
773 pVM->fHMNeedRawModeCtx = HC_ARCH_BITS == 32
774 && pVM->fHMEnabled
775 && pVM->hm.s.fAllow64BitGuests;
776
777 /*
778 * Disable nested paging and unrestricted guest execution now if they're
779 * configured so that CPUM can make decisions based on our configuration.
780 */
781 Assert(!pVM->hm.s.fNestedPaging);
782 if (pVM->hm.s.fAllowNestedPaging)
783 {
784 if (fCaps & SUPVTCAPS_NESTED_PAGING)
785 pVM->hm.s.fNestedPaging = true;
786 else
787 pVM->hm.s.fAllowNestedPaging = false;
788 }
789
790 if (fCaps & SUPVTCAPS_VT_X)
791 {
792 Assert(!pVM->hm.s.vmx.fUnrestrictedGuest);
793 if (pVM->hm.s.vmx.fAllowUnrestricted)
794 {
795 if ( (fCaps & SUPVTCAPS_VTX_UNRESTRICTED_GUEST)
796 && pVM->hm.s.fNestedPaging)
797 pVM->hm.s.vmx.fUnrestrictedGuest = true;
798 else
799 pVM->hm.s.vmx.fAllowUnrestricted = false;
800 }
801 }
802 }
803 else
804 {
805 const char *pszMsg;
806 switch (rc)
807 {
808 case VERR_UNSUPPORTED_CPU: pszMsg = "Unknown CPU, VT-x or AMD-v features cannot be ascertained"; break;
809 case VERR_VMX_NO_VMX: pszMsg = "VT-x is not available"; break;
810 case VERR_VMX_MSR_VMX_DISABLED: pszMsg = "VT-x is disabled in the BIOS"; break;
811 case VERR_VMX_MSR_ALL_VMX_DISABLED: pszMsg = "VT-x is disabled in the BIOS for all CPU modes"; break;
812 case VERR_VMX_MSR_LOCKING_FAILED: pszMsg = "Failed to enable and lock VT-x features"; break;
813 case VERR_SVM_NO_SVM: pszMsg = "AMD-V is not available"; break;
814 case VERR_SVM_DISABLED: pszMsg = "AMD-V is disabled in the BIOS (or by the host OS)"; break;
815 default:
816 return VMSetError(pVM, rc, RT_SRC_POS, "SUPR3QueryVTCaps failed with %Rrc", rc);
817 }
818
819 /*
820 * Before failing, try fallback to NEM if we're allowed to do that.
821 */
822 pVM->fHMEnabled = false;
823 if (fFallbackToNEM)
824 {
825 LogRel(("HM: HMR3Init: Attempting fall back to NEM: %s\n", pszMsg));
826 int rc2 = NEMR3Init(pVM, true /*fFallback*/, fHMForced);
827 ASMCompilerBarrier(); /* NEMR3Init may have changed bMainExecutionEngine. */
828 if ( RT_SUCCESS(rc2)
829 && pVM->bMainExecutionEngine != VM_EXEC_ENGINE_NOT_SET)
830 rc = VINF_SUCCESS;
831 }
832 if (RT_FAILURE(rc))
833 {
834 if (fHMForced)
835 return VM_SET_ERROR(pVM, rc, pszMsg);
836
837 LogRel(("HM: HMR3Init: Falling back to raw-mode: %s\n", pszMsg));
838 VM_SET_MAIN_EXECUTION_ENGINE(pVM, VM_EXEC_ENGINE_RAW_MODE);
839 }
840 }
841 }
842 else
843 {
844 /*
845 * Disabled HM mean raw-mode, unless NEM is supposed to be used.
846 */
847 if (!fUseNEMInstead)
848 VM_SET_MAIN_EXECUTION_ENGINE(pVM, VM_EXEC_ENGINE_RAW_MODE);
849 else
850 {
851 rc = NEMR3Init(pVM, false /*fFallback*/, true);
852 ASMCompilerBarrier(); /* NEMR3Init may have changed bMainExecutionEngine. */
853 if (RT_FAILURE(rc))
854 return rc;
855 }
856 }
857
858 return VINF_SUCCESS;
859}
860
861
862/**
863 * Initializes HM components after ring-3 phase has been fully initialized.
864 *
865 * @returns VBox status code.
866 * @param pVM The cross context VM structure.
867 */
868static int hmR3InitFinalizeR3(PVM pVM)
869{
870 LogFlow(("HMR3InitCPU\n"));
871
872 if (!HMIsEnabled(pVM))
873 return VINF_SUCCESS;
874
875 for (VMCPUID i = 0; i < pVM->cCpus; i++)
876 {
877 PVMCPU pVCpu = &pVM->aCpus[i];
878 pVCpu->hm.s.fActive = false;
879 pVCpu->hm.s.fGIMTrapXcptUD = GIMShouldTrapXcptUD(pVCpu); /* Is safe to call now since GIMR3Init() has completed. */
880 }
881
882#ifdef VBOX_WITH_STATISTICS
883 STAM_REG(pVM, &pVM->hm.s.StatTprPatchSuccess, STAMTYPE_COUNTER, "/HM/TPR/Patch/Success", STAMUNIT_OCCURENCES, "Number of times an instruction was successfully patched.");
884 STAM_REG(pVM, &pVM->hm.s.StatTprPatchFailure, STAMTYPE_COUNTER, "/HM/TPR/Patch/Failed", STAMUNIT_OCCURENCES, "Number of unsuccessful patch attempts.");
885 STAM_REG(pVM, &pVM->hm.s.StatTprReplaceSuccessCr8, STAMTYPE_COUNTER, "/HM/TPR/Replace/SuccessCR8",STAMUNIT_OCCURENCES, "Number of instruction replacements by MOV CR8.");
886 STAM_REG(pVM, &pVM->hm.s.StatTprReplaceSuccessVmc, STAMTYPE_COUNTER, "/HM/TPR/Replace/SuccessVMC",STAMUNIT_OCCURENCES, "Number of instruction replacements by VMMCALL.");
887 STAM_REG(pVM, &pVM->hm.s.StatTprReplaceFailure, STAMTYPE_COUNTER, "/HM/TPR/Replace/Failed", STAMUNIT_OCCURENCES, "Number of unsuccessful replace attempts.");
888#endif
889
890 /*
891 * Statistics.
892 */
893 for (VMCPUID i = 0; i < pVM->cCpus; i++)
894 {
895 PVMCPU pVCpu = &pVM->aCpus[i];
896 int rc;
897
898#ifdef VBOX_WITH_STATISTICS
899 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatPoke, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
900 "Profiling of RTMpPokeCpu.",
901 "/PROF/CPU%d/HM/Poke", i);
902 AssertRC(rc);
903 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatSpinPoke, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
904 "Profiling of poke wait.",
905 "/PROF/CPU%d/HM/PokeWait", i);
906 AssertRC(rc);
907 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatSpinPokeFailed, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
908 "Profiling of poke wait when RTMpPokeCpu fails.",
909 "/PROF/CPU%d/HM/PokeWaitFailed", i);
910 AssertRC(rc);
911 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatEntry, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
912 "Profiling of entry until entering GC.",
913 "/PROF/CPU%d/HM/Entry", i);
914 AssertRC(rc);
915 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatPreExit, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
916 "Profiling of pre-exit processing after returning from GC.",
917 "/PROF/CPU%d/HM/SwitchFromGC_1", i);
918 AssertRC(rc);
919 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatExitHandling, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
920 "Profiling of exit handling (longjmps not included!)",
921 "/PROF/CPU%d/HM/SwitchFromGC_2", i);
922 AssertRC(rc);
923
924 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatExitIO, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
925 "I/O.",
926 "/PROF/CPU%d/HM/SwitchFromGC_2/IO", i);
927 AssertRC(rc);
928 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatExitMovCRx, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
929 "MOV CRx.",
930 "/PROF/CPU%d/HM/SwitchFromGC_2/MovCRx", i);
931 AssertRC(rc);
932 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatExitXcptNmi, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
933 "Exceptions, NMIs.",
934 "/PROF/CPU%d/HM/SwitchFromGC_2/XcptNmi", i);
935 AssertRC(rc);
936
937 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatImportGuestState, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
938 "Profiling of importing guest state from hardware after VM-exit.",
939 "/PROF/CPU%d/HM/ImportGuestState", i);
940 AssertRC(rc);
941 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatExportGuestState, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
942 "Profiling of exporting guest state to hardware before VM-entry.",
943 "/PROF/CPU%d/HM/ExportGuestState", i);
944 AssertRC(rc);
945 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatLoadGuestFpuState, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
946 "Profiling of CPUMR0LoadGuestFPU.",
947 "/PROF/CPU%d/HM/LoadGuestFpuState", i);
948 AssertRC(rc);
949 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatInGC, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL,
950 "Profiling of execution of guest-code in hardware.",
951 "/PROF/CPU%d/HM/InGC", i);
952 AssertRC(rc);
953
954# if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS)
955 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatWorldSwitch3264, STAMTYPE_PROFILE, STAMVISIBILITY_USED,
956 STAMUNIT_TICKS_PER_CALL, "Profiling of the 32/64 switcher.",
957 "/PROF/CPU%d/HM/Switcher3264", i);
958 AssertRC(rc);
959# endif
960
961# ifdef HM_PROFILE_EXIT_DISPATCH
962 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatExitDispatch, STAMTYPE_PROFILE_ADV, STAMVISIBILITY_USED,
963 STAMUNIT_TICKS_PER_CALL, "Profiling the dispatching of exit handlers.",
964 "/PROF/CPU%d/HM/ExitDispatch", i);
965 AssertRC(rc);
966# endif
967
968#endif
969# define HM_REG_COUNTER(a, b, desc) \
970 rc = STAMR3RegisterF(pVM, a, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, desc, b, i); \
971 AssertRC(rc);
972
973#ifdef VBOX_WITH_STATISTICS
974 HM_REG_COUNTER(&pVCpu->hm.s.StatExitAll, "/HM/CPU%d/Exit/All", "Exits (total).");
975 HM_REG_COUNTER(&pVCpu->hm.s.StatExitShadowNM, "/HM/CPU%d/Exit/Trap/Shw/#NM", "Shadow #NM (device not available, no math co-processor) exception.");
976 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestNM, "/HM/CPU%d/Exit/Trap/Gst/#NM", "Guest #NM (device not available, no math co-processor) exception.");
977 HM_REG_COUNTER(&pVCpu->hm.s.StatExitShadowPF, "/HM/CPU%d/Exit/Trap/Shw/#PF", "Shadow #PF (page fault) exception.");
978 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.");
979 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestPF, "/HM/CPU%d/Exit/Trap/Gst/#PF", "Guest #PF (page fault) exception.");
980 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestUD, "/HM/CPU%d/Exit/Trap/Gst/#UD", "Guest #UD (undefined opcode) exception.");
981 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestSS, "/HM/CPU%d/Exit/Trap/Gst/#SS", "Guest #SS (stack-segment fault) exception.");
982 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestNP, "/HM/CPU%d/Exit/Trap/Gst/#NP", "Guest #NP (segment not present) exception.");
983 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestGP, "/HM/CPU%d/Exit/Trap/Gst/#GP", "Guest #GP (general protection) exception.");
984 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestMF, "/HM/CPU%d/Exit/Trap/Gst/#MF", "Guest #MF (x87 FPU error, math fault) exception.");
985 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestDE, "/HM/CPU%d/Exit/Trap/Gst/#DE", "Guest #DE (divide error) exception.");
986 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestDB, "/HM/CPU%d/Exit/Trap/Gst/#DB", "Guest #DB (debug) exception.");
987 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestBP, "/HM/CPU%d/Exit/Trap/Gst/#BP", "Guest #BP (breakpoint) exception.");
988 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestXF, "/HM/CPU%d/Exit/Trap/Gst/#XF", "Guest #XF (extended math fault, SIMD FPU) exception.");
989 HM_REG_COUNTER(&pVCpu->hm.s.StatExitGuestXcpUnk, "/HM/CPU%d/Exit/Trap/Gst/Other", "Other guest exceptions.");
990 HM_REG_COUNTER(&pVCpu->hm.s.StatExitHlt, "/HM/CPU%d/Exit/Instr/Hlt", "HLT instruction.");
991 HM_REG_COUNTER(&pVCpu->hm.s.StatExitRdmsr, "/HM/CPU%d/Exit/Instr/Rdmsr", "RDMSR instruction.");
992 HM_REG_COUNTER(&pVCpu->hm.s.StatExitWrmsr, "/HM/CPU%d/Exit/Instr/Wrmsr", "WRMSR instruction.");
993 HM_REG_COUNTER(&pVCpu->hm.s.StatExitMwait, "/HM/CPU%d/Exit/Instr/Mwait", "MWAIT instruction.");
994 HM_REG_COUNTER(&pVCpu->hm.s.StatExitMonitor, "/HM/CPU%d/Exit/Instr/Monitor", "MONITOR instruction.");
995 HM_REG_COUNTER(&pVCpu->hm.s.StatExitDRxWrite, "/HM/CPU%d/Exit/Instr/DR-Write", "Debug register write.");
996 HM_REG_COUNTER(&pVCpu->hm.s.StatExitDRxRead, "/HM/CPU%d/Exit/Instr/DR-Read", "Debug register read.");
997 HM_REG_COUNTER(&pVCpu->hm.s.StatExitCR0Read, "/HM/CPU%d/Exit/Instr/CR-Read/CR0", "CR0 read.");
998 HM_REG_COUNTER(&pVCpu->hm.s.StatExitCR2Read, "/HM/CPU%d/Exit/Instr/CR-Read/CR2", "CR2 read.");
999 HM_REG_COUNTER(&pVCpu->hm.s.StatExitCR3Read, "/HM/CPU%d/Exit/Instr/CR-Read/CR3", "CR3 read.");
1000 HM_REG_COUNTER(&pVCpu->hm.s.StatExitCR4Read, "/HM/CPU%d/Exit/Instr/CR-Read/CR4", "CR4 read.");
1001 HM_REG_COUNTER(&pVCpu->hm.s.StatExitCR8Read, "/HM/CPU%d/Exit/Instr/CR-Read/CR8", "CR8 read.");
1002 HM_REG_COUNTER(&pVCpu->hm.s.StatExitCR0Write, "/HM/CPU%d/Exit/Instr/CR-Write/CR0", "CR0 write.");
1003 HM_REG_COUNTER(&pVCpu->hm.s.StatExitCR2Write, "/HM/CPU%d/Exit/Instr/CR-Write/CR2", "CR2 write.");
1004 HM_REG_COUNTER(&pVCpu->hm.s.StatExitCR3Write, "/HM/CPU%d/Exit/Instr/CR-Write/CR3", "CR3 write.");
1005 HM_REG_COUNTER(&pVCpu->hm.s.StatExitCR4Write, "/HM/CPU%d/Exit/Instr/CR-Write/CR4", "CR4 write.");
1006 HM_REG_COUNTER(&pVCpu->hm.s.StatExitCR8Write, "/HM/CPU%d/Exit/Instr/CR-Write/CR8", "CR8 write.");
1007 HM_REG_COUNTER(&pVCpu->hm.s.StatExitClts, "/HM/CPU%d/Exit/Instr/CLTS", "CLTS instruction.");
1008 HM_REG_COUNTER(&pVCpu->hm.s.StatExitLmsw, "/HM/CPU%d/Exit/Instr/LMSW", "LMSW instruction.");
1009 HM_REG_COUNTER(&pVCpu->hm.s.StatExitCli, "/HM/CPU%d/Exit/Instr/Cli", "CLI instruction.");
1010 HM_REG_COUNTER(&pVCpu->hm.s.StatExitSti, "/HM/CPU%d/Exit/Instr/Sti", "STI instruction.");
1011 HM_REG_COUNTER(&pVCpu->hm.s.StatExitPushf, "/HM/CPU%d/Exit/Instr/Pushf", "PUSHF instruction.");
1012 HM_REG_COUNTER(&pVCpu->hm.s.StatExitPopf, "/HM/CPU%d/Exit/Instr/Popf", "POPF instruction.");
1013 HM_REG_COUNTER(&pVCpu->hm.s.StatExitIret, "/HM/CPU%d/Exit/Instr/Iret", "IRET instruction.");
1014 HM_REG_COUNTER(&pVCpu->hm.s.StatExitInt, "/HM/CPU%d/Exit/Instr/Int", "INT instruction.");
1015 HM_REG_COUNTER(&pVCpu->hm.s.StatExitXdtrAccess, "/HM/CPU%d/Exit/Instr/XdtrAccess", "GDTR, IDTR, LDTR access.");
1016 HM_REG_COUNTER(&pVCpu->hm.s.StatExitIOWrite, "/HM/CPU%d/Exit/IO/Write", "I/O write.");
1017 HM_REG_COUNTER(&pVCpu->hm.s.StatExitIORead, "/HM/CPU%d/Exit/IO/Read", "I/O read.");
1018 HM_REG_COUNTER(&pVCpu->hm.s.StatExitIOStringWrite, "/HM/CPU%d/Exit/IO/WriteString", "String I/O write.");
1019 HM_REG_COUNTER(&pVCpu->hm.s.StatExitIOStringRead, "/HM/CPU%d/Exit/IO/ReadString", "String I/O read.");
1020 HM_REG_COUNTER(&pVCpu->hm.s.StatExitIntWindow, "/HM/CPU%d/Exit/IntWindow", "Interrupt-window exit. Guest is ready to receive interrupts again.");
1021 HM_REG_COUNTER(&pVCpu->hm.s.StatExitExtInt, "/HM/CPU%d/Exit/ExtInt", "Physical maskable interrupt (host).");
1022#endif
1023 HM_REG_COUNTER(&pVCpu->hm.s.StatExitHostNmiInGC, "/HM/CPU%d/Exit/HostNmiInGC", "Host NMI received while in guest context.");
1024#ifdef VBOX_WITH_STATISTICS
1025 HM_REG_COUNTER(&pVCpu->hm.s.StatExitPreemptTimer, "/HM/CPU%d/Exit/PreemptTimer", "VMX-preemption timer expired.");
1026 HM_REG_COUNTER(&pVCpu->hm.s.StatExitTprBelowThreshold, "/HM/CPU%d/Exit/TprBelowThreshold", "TPR lowered below threshold by the guest.");
1027 HM_REG_COUNTER(&pVCpu->hm.s.StatExitTaskSwitch, "/HM/CPU%d/Exit/TaskSwitch", "Task switch.");
1028 HM_REG_COUNTER(&pVCpu->hm.s.StatExitMtf, "/HM/CPU%d/Exit/MonitorTrapFlag", "Monitor Trap Flag.");
1029 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.");
1030
1031 HM_REG_COUNTER(&pVCpu->hm.s.StatSwitchTprMaskedIrq, "/HM/CPU%d/Switch/TprMaskedIrq", "PDMGetInterrupt() signals TPR masks pending Irq.");
1032 HM_REG_COUNTER(&pVCpu->hm.s.StatSwitchGuestIrq, "/HM/CPU%d/Switch/IrqPending", "PDMGetInterrupt() cleared behind our back!?!.");
1033 HM_REG_COUNTER(&pVCpu->hm.s.StatSwitchPendingHostIrq, "/HM/CPU%d/Switch/PendingHostIrq", "Exit to ring-3 due to pending host interrupt before executing guest code.");
1034 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.");
1035 HM_REG_COUNTER(&pVCpu->hm.s.StatSwitchExitToR3, "/HM/CPU%d/Switch/ExitToR3", "Exit to ring-3 (total).");
1036 HM_REG_COUNTER(&pVCpu->hm.s.StatSwitchLongJmpToR3, "/HM/CPU%d/Switch/LongJmpToR3", "Longjump to ring-3.");
1037 HM_REG_COUNTER(&pVCpu->hm.s.StatSwitchMaxResumeLoops, "/HM/CPU%d/Switch/MaxResumeToR3", "Maximum VMRESUME inner-loop counter reached.");
1038 HM_REG_COUNTER(&pVCpu->hm.s.StatSwitchHltToR3, "/HM/CPU%d/Switch/HltToR3", "HLT causing us to go to ring-3.");
1039 HM_REG_COUNTER(&pVCpu->hm.s.StatSwitchApicAccessToR3, "/HM/CPU%d/Switch/ApicAccessToR3", "APIC access causing us to go to ring-3.");
1040#endif
1041 HM_REG_COUNTER(&pVCpu->hm.s.StatSwitchPreempt, "/HM/CPU%d/Switch/Preempting", "EMT has been preempted while in HM context.");
1042#ifdef VBOX_WITH_STATISTICS
1043 HM_REG_COUNTER(&pVCpu->hm.s.StatSwitchPreemptExportHostState, "/HM/CPU%d/Switch/ExportHostState", "Preemption caused us to re-export the host state.");
1044
1045 HM_REG_COUNTER(&pVCpu->hm.s.StatInjectInterrupt, "/HM/CPU%d/EventInject/Interrupt", "Injected an external interrupt into the guest.");
1046 HM_REG_COUNTER(&pVCpu->hm.s.StatInjectXcpt, "/HM/CPU%d/EventInject/Trap", "Injected an exception into the guest.");
1047 HM_REG_COUNTER(&pVCpu->hm.s.StatInjectPendingReflect, "/HM/CPU%d/EventInject/PendingReflect", "Reflecting an exception (or #DF) caused due to event injection.");
1048 HM_REG_COUNTER(&pVCpu->hm.s.StatInjectPendingInterpret, "/HM/CPU%d/EventInject/PendingInterpret", "Falling to interpreter for handling exception caused due to event injection.");
1049
1050 HM_REG_COUNTER(&pVCpu->hm.s.StatFlushPage, "/HM/CPU%d/Flush/Page", "Invalidating a guest page on all guest CPUs.");
1051 HM_REG_COUNTER(&pVCpu->hm.s.StatFlushPageManual, "/HM/CPU%d/Flush/Page/Virt", "Invalidating a guest page using guest-virtual address.");
1052 HM_REG_COUNTER(&pVCpu->hm.s.StatFlushPhysPageManual, "/HM/CPU%d/Flush/Page/Phys", "Invalidating a guest page using guest-physical address.");
1053 HM_REG_COUNTER(&pVCpu->hm.s.StatFlushTlb, "/HM/CPU%d/Flush/TLB", "Forcing a full guest-TLB flush (ring-0).");
1054 HM_REG_COUNTER(&pVCpu->hm.s.StatFlushTlbManual, "/HM/CPU%d/Flush/TLB/Manual", "Request a full guest-TLB flush.");
1055 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.");
1056 HM_REG_COUNTER(&pVCpu->hm.s.StatNoFlushTlbWorldSwitch, "/HM/CPU%d/Flush/TLB/Skipped", "No TLB flushing required.");
1057 HM_REG_COUNTER(&pVCpu->hm.s.StatFlushEntire, "/HM/CPU%d/Flush/TLB/Entire", "Flush the entire TLB (host + guest).");
1058 HM_REG_COUNTER(&pVCpu->hm.s.StatFlushAsid, "/HM/CPU%d/Flush/TLB/ASID", "Flushed guest-TLB entries for the current VPID.");
1059 HM_REG_COUNTER(&pVCpu->hm.s.StatFlushNestedPaging, "/HM/CPU%d/Flush/TLB/NestedPaging", "Flushed guest-TLB entries for the current EPT.");
1060 HM_REG_COUNTER(&pVCpu->hm.s.StatFlushTlbInvlpgVirt, "/HM/CPU%d/Flush/TLB/InvlpgVirt", "Invalidated a guest-TLB entry for a guest-virtual address.");
1061 HM_REG_COUNTER(&pVCpu->hm.s.StatFlushTlbInvlpgPhys, "/HM/CPU%d/Flush/TLB/InvlpgPhys", "Currently not possible, flushes entire guest-TLB.");
1062 HM_REG_COUNTER(&pVCpu->hm.s.StatTlbShootdown, "/HM/CPU%d/Flush/Shootdown/Page", "Inter-VCPU request to flush queued guest page.");
1063 HM_REG_COUNTER(&pVCpu->hm.s.StatTlbShootdownFlush, "/HM/CPU%d/Flush/Shootdown/TLB", "Inter-VCPU request to flush entire guest-TLB.");
1064
1065 HM_REG_COUNTER(&pVCpu->hm.s.StatTscParavirt, "/HM/CPU%d/TSC/Paravirt", "Paravirtualized TSC in effect.");
1066 HM_REG_COUNTER(&pVCpu->hm.s.StatTscOffset, "/HM/CPU%d/TSC/Offset", "TSC offsetting is in effect.");
1067 HM_REG_COUNTER(&pVCpu->hm.s.StatTscIntercept, "/HM/CPU%d/TSC/Intercept", "Intercept TSC accesses.");
1068
1069 HM_REG_COUNTER(&pVCpu->hm.s.StatDRxArmed, "/HM/CPU%d/Debug/Armed", "Loaded guest-debug state while loading guest-state.");
1070 HM_REG_COUNTER(&pVCpu->hm.s.StatDRxContextSwitch, "/HM/CPU%d/Debug/ContextSwitch", "Loaded guest-debug state on MOV DRx.");
1071 HM_REG_COUNTER(&pVCpu->hm.s.StatDRxIoCheck, "/HM/CPU%d/Debug/IOCheck", "Checking for I/O breakpoint.");
1072
1073 HM_REG_COUNTER(&pVCpu->hm.s.StatExportMinimal, "/HM/CPU%d/Export/Minimal", "VM-entry exporting minimal guest-state.");
1074 HM_REG_COUNTER(&pVCpu->hm.s.StatExportFull, "/HM/CPU%d/Export/Full", "VM-entry exporting the full guest-state.");
1075 HM_REG_COUNTER(&pVCpu->hm.s.StatLoadGuestFpu, "/HM/CPU%d/Export/GuestFpu", "VM-entry loading the guest-FPU state.");
1076
1077 HM_REG_COUNTER(&pVCpu->hm.s.StatVmxCheckBadRmSelBase, "/HM/CPU%d/VMXCheck/RMSelBase", "Could not use VMX due to unsuitable real-mode selector base.");
1078 HM_REG_COUNTER(&pVCpu->hm.s.StatVmxCheckBadRmSelLimit, "/HM/CPU%d/VMXCheck/RMSelLimit", "Could not use VMX due to unsuitable real-mode selector limit.");
1079 HM_REG_COUNTER(&pVCpu->hm.s.StatVmxCheckBadRmSelAttr, "/HM/CPU%d/VMXCheck/RMSelAttrs", "Could not use VMX due to unsuitable real-mode selector limit.");
1080 HM_REG_COUNTER(&pVCpu->hm.s.StatVmxCheckRmOk, "/HM/CPU%d/VMXCheck/VMX_RM", "VMX execution in real (V86) mode OK.");
1081 HM_REG_COUNTER(&pVCpu->hm.s.StatVmxCheckBadSel, "/HM/CPU%d/VMXCheck/Selector", "Could not use VMX due to unsuitable selector.");
1082 HM_REG_COUNTER(&pVCpu->hm.s.StatVmxCheckBadRpl, "/HM/CPU%d/VMXCheck/RPL", "Could not use VMX due to unsuitable RPL.");
1083 HM_REG_COUNTER(&pVCpu->hm.s.StatVmxCheckPmOk, "/HM/CPU%d/VMXCheck/VMX_PM", "VMX execution in protected mode OK.");
1084
1085#if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS)
1086 HM_REG_COUNTER(&pVCpu->hm.s.StatFpu64SwitchBack, "/HM/CPU%d/Switch64/Fpu", "Saving guest FPU/XMM state.");
1087 HM_REG_COUNTER(&pVCpu->hm.s.StatDebug64SwitchBack, "/HM/CPU%d/Switch64/Debug", "Saving guest debug state.");
1088#endif
1089
1090#undef HM_REG_COUNTER
1091
1092 const char *const *papszDesc = ASMIsIntelCpu() || ASMIsViaCentaurCpu() ? &g_apszVmxExitReasons[0]
1093 : &g_apszSvmExitReasons[0];
1094
1095 /*
1096 * Guest Exit reason stats.
1097 */
1098 pVCpu->hm.s.paStatExitReason = NULL;
1099 rc = MMHyperAlloc(pVM, MAX_EXITREASON_STAT * sizeof(*pVCpu->hm.s.paStatExitReason), 0 /* uAlignment */, MM_TAG_HM,
1100 (void **)&pVCpu->hm.s.paStatExitReason);
1101 AssertRCReturn(rc, rc);
1102 for (int j = 0; j < MAX_EXITREASON_STAT; j++)
1103 {
1104 if (papszDesc[j])
1105 {
1106 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.paStatExitReason[j], STAMTYPE_COUNTER, STAMVISIBILITY_USED,
1107 STAMUNIT_OCCURENCES, papszDesc[j], "/HM/CPU%d/Exit/Reason/%02x", i, j);
1108 AssertRCReturn(rc, rc);
1109 }
1110 }
1111 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatExitReasonNpf, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_OCCURENCES,
1112 "Nested page fault", "/HM/CPU%d/Exit/Reason/#NPF", i);
1113 AssertRCReturn(rc, rc);
1114 pVCpu->hm.s.paStatExitReasonR0 = MMHyperR3ToR0(pVM, pVCpu->hm.s.paStatExitReason);
1115# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1116 Assert(pVCpu->hm.s.paStatExitReasonR0 != NIL_RTR0PTR || !HMIsEnabled(pVM));
1117# else
1118 Assert(pVCpu->hm.s.paStatExitReasonR0 != NIL_RTR0PTR);
1119# endif
1120
1121#ifdef VBOX_WITH_NESTED_HWVIRT_SVM
1122 /*
1123 * Nested-guest Exit reason stats.
1124 */
1125 pVCpu->hm.s.paStatNestedExitReason = NULL;
1126 rc = MMHyperAlloc(pVM, MAX_EXITREASON_STAT * sizeof(*pVCpu->hm.s.paStatNestedExitReason), 0 /* uAlignment */, MM_TAG_HM,
1127 (void **)&pVCpu->hm.s.paStatNestedExitReason);
1128 AssertRCReturn(rc, rc);
1129 for (int j = 0; j < MAX_EXITREASON_STAT; j++)
1130 {
1131 if (papszDesc[j])
1132 {
1133 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.paStatNestedExitReason[j], STAMTYPE_COUNTER, STAMVISIBILITY_USED,
1134 STAMUNIT_OCCURENCES, papszDesc[j], "/HM/CPU%d/NestedExit/Reason/%02x", i, j);
1135 AssertRC(rc);
1136 }
1137 }
1138 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatNestedExitReasonNpf, STAMTYPE_COUNTER, STAMVISIBILITY_USED,
1139 STAMUNIT_OCCURENCES, "Nested page fault", "/HM/CPU%d/NestedExit/Reason/#NPF", i);
1140 AssertRCReturn(rc, rc);
1141 pVCpu->hm.s.paStatNestedExitReasonR0 = MMHyperR3ToR0(pVM, pVCpu->hm.s.paStatNestedExitReason);
1142# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1143 Assert(pVCpu->hm.s.paStatNestedExitReasonR0 != NIL_RTR0PTR || !HMIsEnabled(pVM));
1144# else
1145 Assert(pVCpu->hm.s.paStatNestedExitReasonR0 != NIL_RTR0PTR);
1146# endif
1147#endif
1148
1149 /*
1150 * Injected events stats.
1151 */
1152 rc = MMHyperAlloc(pVM, sizeof(STAMCOUNTER) * 256, 8, MM_TAG_HM, (void **)&pVCpu->hm.s.paStatInjectedIrqs);
1153 AssertRCReturn(rc, rc);
1154 pVCpu->hm.s.paStatInjectedIrqsR0 = MMHyperR3ToR0(pVM, pVCpu->hm.s.paStatInjectedIrqs);
1155# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1156 Assert(pVCpu->hm.s.paStatInjectedIrqsR0 != NIL_RTR0PTR || !HMIsEnabled(pVM));
1157# else
1158 Assert(pVCpu->hm.s.paStatInjectedIrqsR0 != NIL_RTR0PTR);
1159# endif
1160 for (unsigned j = 0; j < 255; j++)
1161 {
1162 STAMR3RegisterF(pVM, &pVCpu->hm.s.paStatInjectedIrqs[j], STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_OCCURENCES,
1163 "Injected event.",
1164 (j < 0x20) ? "/HM/CPU%d/EventInject/InjectTrap/%02X" : "/HM/CPU%d/EventInject/InjectIRQ/%02X", i, j);
1165 }
1166
1167#endif /* VBOX_WITH_STATISTICS */
1168 }
1169
1170#ifdef VBOX_WITH_CRASHDUMP_MAGIC
1171 /*
1172 * Magic marker for searching in crash dumps.
1173 */
1174 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1175 {
1176 PVMCPU pVCpu = &pVM->aCpus[i];
1177
1178 PVMCSCACHE pCache = &pVCpu->hm.s.vmx.VMCSCache;
1179 strcpy((char *)pCache->aMagic, "VMCSCACHE Magic");
1180 pCache->uMagic = UINT64_C(0xdeadbeefdeadbeef);
1181 }
1182#endif
1183
1184 return VINF_SUCCESS;
1185}
1186
1187
1188/**
1189 * Called when a init phase has completed.
1190 *
1191 * @returns VBox status code.
1192 * @param pVM The cross context VM structure.
1193 * @param enmWhat The phase that completed.
1194 */
1195VMMR3_INT_DECL(int) HMR3InitCompleted(PVM pVM, VMINITCOMPLETED enmWhat)
1196{
1197 switch (enmWhat)
1198 {
1199 case VMINITCOMPLETED_RING3:
1200 return hmR3InitFinalizeR3(pVM);
1201 case VMINITCOMPLETED_RING0:
1202 return hmR3InitFinalizeR0(pVM);
1203 default:
1204 return VINF_SUCCESS;
1205 }
1206}
1207
1208
1209/**
1210 * Turns off normal raw mode features.
1211 *
1212 * @param pVM The cross context VM structure.
1213 */
1214static void hmR3DisableRawMode(PVM pVM)
1215{
1216/** @todo r=bird: HM shouldn't be doing this crap. */
1217 /* Reinit the paging mode to force the new shadow mode. */
1218 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1219 {
1220 PVMCPU pVCpu = &pVM->aCpus[i];
1221 PGMHCChangeMode(pVM, pVCpu, PGMMODE_REAL);
1222 }
1223}
1224
1225
1226/**
1227 * Initialize VT-x or AMD-V.
1228 *
1229 * @returns VBox status code.
1230 * @param pVM The cross context VM structure.
1231 */
1232static int hmR3InitFinalizeR0(PVM pVM)
1233{
1234 int rc;
1235
1236 if (!HMIsEnabled(pVM))
1237 return VINF_SUCCESS;
1238
1239 /*
1240 * Hack to allow users to work around broken BIOSes that incorrectly set
1241 * EFER.SVME, which makes us believe somebody else is already using AMD-V.
1242 */
1243 if ( !pVM->hm.s.vmx.fSupported
1244 && !pVM->hm.s.svm.fSupported
1245 && pVM->hm.s.rcInit == VERR_SVM_IN_USE /* implies functional AMD-V */
1246 && RTEnvExist("VBOX_HWVIRTEX_IGNORE_SVM_IN_USE"))
1247 {
1248 LogRel(("HM: VBOX_HWVIRTEX_IGNORE_SVM_IN_USE active!\n"));
1249 pVM->hm.s.svm.fSupported = true;
1250 pVM->hm.s.svm.fIgnoreInUseError = true;
1251 pVM->hm.s.rcInit = VINF_SUCCESS;
1252 }
1253
1254 /*
1255 * Report ring-0 init errors.
1256 */
1257 if ( !pVM->hm.s.vmx.fSupported
1258 && !pVM->hm.s.svm.fSupported)
1259 {
1260 LogRel(("HM: Failed to initialize VT-x / AMD-V: %Rrc\n", pVM->hm.s.rcInit));
1261 LogRel(("HM: VMX MSR_IA32_FEATURE_CONTROL=%RX64\n", pVM->hm.s.vmx.Msrs.u64FeatCtrl));
1262 switch (pVM->hm.s.rcInit)
1263 {
1264 case VERR_VMX_IN_VMX_ROOT_MODE:
1265 return VM_SET_ERROR(pVM, VERR_VMX_IN_VMX_ROOT_MODE, "VT-x is being used by another hypervisor");
1266 case VERR_VMX_NO_VMX:
1267 return VM_SET_ERROR(pVM, VERR_VMX_NO_VMX, "VT-x is not available");
1268 case VERR_VMX_MSR_VMX_DISABLED:
1269 return VM_SET_ERROR(pVM, VERR_VMX_MSR_VMX_DISABLED, "VT-x is disabled in the BIOS");
1270 case VERR_VMX_MSR_ALL_VMX_DISABLED:
1271 return VM_SET_ERROR(pVM, VERR_VMX_MSR_ALL_VMX_DISABLED, "VT-x is disabled in the BIOS for all CPU modes");
1272 case VERR_VMX_MSR_LOCKING_FAILED:
1273 return VM_SET_ERROR(pVM, VERR_VMX_MSR_LOCKING_FAILED, "Failed to lock VT-x features while trying to enable VT-x");
1274 case VERR_VMX_MSR_VMX_ENABLE_FAILED:
1275 return VM_SET_ERROR(pVM, VERR_VMX_MSR_VMX_ENABLE_FAILED, "Failed to enable VT-x features");
1276 case VERR_VMX_MSR_SMX_VMX_ENABLE_FAILED:
1277 return VM_SET_ERROR(pVM, VERR_VMX_MSR_SMX_VMX_ENABLE_FAILED, "Failed to enable VT-x features in SMX mode");
1278
1279 case VERR_SVM_IN_USE:
1280 return VM_SET_ERROR(pVM, VERR_SVM_IN_USE, "AMD-V is being used by another hypervisor");
1281 case VERR_SVM_NO_SVM:
1282 return VM_SET_ERROR(pVM, VERR_SVM_NO_SVM, "AMD-V is not available");
1283 case VERR_SVM_DISABLED:
1284 return VM_SET_ERROR(pVM, VERR_SVM_DISABLED, "AMD-V is disabled in the BIOS");
1285 }
1286 return VMSetError(pVM, pVM->hm.s.rcInit, RT_SRC_POS, "HM ring-0 init failed: %Rrc", pVM->hm.s.rcInit);
1287 }
1288
1289 /*
1290 * Enable VT-x or AMD-V on all host CPUs.
1291 */
1292 rc = SUPR3CallVMMR0Ex(pVM->pVMR0, 0 /*idCpu*/, VMMR0_DO_HM_ENABLE, 0, NULL);
1293 if (RT_FAILURE(rc))
1294 {
1295 LogRel(("HM: Failed to enable, error %Rrc\n", rc));
1296 HMR3CheckError(pVM, rc);
1297 return rc;
1298 }
1299
1300 /*
1301 * No TPR patching is required when the IO-APIC is not enabled for this VM.
1302 * (Main should have taken care of this already)
1303 */
1304 if (!PDMHasIoApic(pVM))
1305 {
1306 Assert(!pVM->hm.s.fTprPatchingAllowed); /* paranoia */
1307 pVM->hm.s.fTprPatchingAllowed = false;
1308 }
1309
1310 /*
1311 * Check if L1D flush is needed/possible.
1312 */
1313 if ( !pVM->cpum.ro.HostFeatures.fFlushCmd
1314 || pVM->cpum.ro.HostFeatures.enmMicroarch < kCpumMicroarch_Intel_Core7_Nehalem
1315 || pVM->cpum.ro.HostFeatures.enmMicroarch >= kCpumMicroarch_Intel_Core7_End
1316 || pVM->cpum.ro.HostFeatures.fArchVmmNeedNotFlushL1d
1317 || pVM->cpum.ro.HostFeatures.fArchRdclNo)
1318 pVM->hm.s.fL1dFlushOnSched = pVM->hm.s.fL1dFlushOnVmEntry = false;
1319
1320 /*
1321 * Sync options.
1322 */
1323 /** @todo Move this out of of CPUMCTX and into some ring-0 only HM structure.
1324 * That will require a little bit of work, of course. */
1325 for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
1326 {
1327 PVMCPU pVCpu = &pVM->aCpus[iCpu];
1328 PCPUMCTX pCpuCtx = &pVCpu->cpum.GstCtx;
1329 pCpuCtx->fWorldSwitcher &= ~(CPUMCTX_WSF_IBPB_EXIT | CPUMCTX_WSF_IBPB_ENTRY);
1330 if (pVM->cpum.ro.HostFeatures.fIbpb)
1331 {
1332 if (pVM->hm.s.fIbpbOnVmExit)
1333 pCpuCtx->fWorldSwitcher |= CPUMCTX_WSF_IBPB_EXIT;
1334 if (pVM->hm.s.fIbpbOnVmEntry)
1335 pCpuCtx->fWorldSwitcher |= CPUMCTX_WSF_IBPB_ENTRY;
1336 }
1337 if (pVM->cpum.ro.HostFeatures.fFlushCmd && pVM->hm.s.fL1dFlushOnVmEntry)
1338 pCpuCtx->fWorldSwitcher |= CPUMCTX_WSF_L1D_ENTRY;
1339 if (iCpu == 0)
1340 LogRel(("HM: fWorldSwitcher=%#x (fIbpbOnVmExit=%RTbool fIbpbOnVmEntry=%RTbool fL1dFlushOnVmEntry=%RTbool); fL1dFlushOnSched=%RTbool\n",
1341 pCpuCtx->fWorldSwitcher, pVM->hm.s.fIbpbOnVmExit, pVM->hm.s.fIbpbOnVmEntry, pVM->hm.s.fL1dFlushOnVmEntry,
1342 pVM->hm.s.fL1dFlushOnSched));
1343 }
1344
1345 /*
1346 * Do the vendor specific initialization
1347 *
1348 * Note! We disable release log buffering here since we're doing relatively
1349 * lot of logging and doesn't want to hit the disk with each LogRel
1350 * statement.
1351 */
1352 AssertLogRelReturn(!pVM->hm.s.fInitialized, VERR_HM_IPE_5);
1353 bool fOldBuffered = RTLogRelSetBuffering(true /*fBuffered*/);
1354 if (pVM->hm.s.vmx.fSupported)
1355 rc = hmR3InitFinalizeR0Intel(pVM);
1356 else
1357 rc = hmR3InitFinalizeR0Amd(pVM);
1358 LogRel(("HM: VT-x/AMD-V init method: %s\n", (pVM->hm.s.fGlobalInit) ? "GLOBAL" : "LOCAL"));
1359 RTLogRelSetBuffering(fOldBuffered);
1360 pVM->hm.s.fInitialized = true;
1361
1362 return rc;
1363}
1364
1365
1366/**
1367 * @callback_method_impl{FNPDMVMMDEVHEAPNOTIFY}
1368 */
1369static DECLCALLBACK(void) hmR3VmmDevHeapNotify(PVM pVM, void *pvAllocation, RTGCPHYS GCPhysAllocation)
1370{
1371 NOREF(pVM);
1372 NOREF(pvAllocation);
1373 NOREF(GCPhysAllocation);
1374}
1375
1376
1377/**
1378 * Returns a description of the VMCS (and associated regions') memory type given the
1379 * IA32_VMX_BASIC MSR.
1380 *
1381 * @returns The descriptive memory type.
1382 * @param uMsrVmxBasic IA32_VMX_BASIC MSR value.
1383 */
1384static const char *hmR3VmxGetMemTypeDesc(uint64_t uMsrVmxBasic)
1385{
1386 uint8_t const uMemType = RT_BF_GET(uMsrVmxBasic, VMX_BF_BASIC_VMCS_MEM_TYPE);
1387 switch (uMemType)
1388 {
1389 case VMX_BASIC_MEM_TYPE_WB: return "Write Back (WB)";
1390 case VMX_BASIC_MEM_TYPE_UC: return "Uncacheable (UC)";
1391 }
1392 return "Unknown";
1393}
1394
1395
1396/**
1397 * Returns a single-line description of all the activity-states supported by the CPU
1398 * given the IA32_VMX_MISC MSR.
1399 *
1400 * @returns All supported activity states.
1401 * @param uMsrMisc IA32_VMX_MISC MSR value.
1402 */
1403static const char *hmR3VmxGetActivityStateAllDesc(uint64_t uMsrMisc)
1404{
1405 static const char * const s_apszActStates[] =
1406 {
1407 "",
1408 " ( HLT )",
1409 " ( SHUTDOWN )",
1410 " ( HLT SHUTDOWN )",
1411 " ( SIPI_WAIT )",
1412 " ( HLT SIPI_WAIT )",
1413 " ( SHUTDOWN SIPI_WAIT )",
1414 " ( HLT SHUTDOWN SIPI_WAIT )"
1415 };
1416 uint8_t const idxActStates = RT_BF_GET(uMsrMisc, VMX_BF_MISC_ACTIVITY_STATES);
1417 Assert(idxActStates < RT_ELEMENTS(s_apszActStates));
1418 return s_apszActStates[idxActStates];
1419}
1420
1421
1422/**
1423 * Reports MSR_IA32_FEATURE_CONTROL MSR to the log.
1424 *
1425 * @param fFeatMsr The feature control MSR value.
1426 */
1427static void hmR3VmxReportFeatCtlMsr(uint64_t fFeatMsr)
1428{
1429 uint64_t const val = fFeatMsr;
1430 LogRel(("HM: MSR_IA32_FEATURE_CONTROL = %#RX64\n", val));
1431 HMVMX_REPORT_MSR_CAP(val, "LOCK", MSR_IA32_FEATURE_CONTROL_LOCK);
1432 HMVMX_REPORT_MSR_CAP(val, "SMX_VMXON", MSR_IA32_FEATURE_CONTROL_SMX_VMXON);
1433 HMVMX_REPORT_MSR_CAP(val, "VMXON", MSR_IA32_FEATURE_CONTROL_VMXON);
1434 HMVMX_REPORT_MSR_CAP(val, "SENTER_LOCAL_FN0", MSR_IA32_FEATURE_CONTROL_SENTER_LOCAL_FN_0);
1435 HMVMX_REPORT_MSR_CAP(val, "SENTER_LOCAL_FN1", MSR_IA32_FEATURE_CONTROL_SENTER_LOCAL_FN_1);
1436 HMVMX_REPORT_MSR_CAP(val, "SENTER_LOCAL_FN2", MSR_IA32_FEATURE_CONTROL_SENTER_LOCAL_FN_2);
1437 HMVMX_REPORT_MSR_CAP(val, "SENTER_LOCAL_FN3", MSR_IA32_FEATURE_CONTROL_SENTER_LOCAL_FN_3);
1438 HMVMX_REPORT_MSR_CAP(val, "SENTER_LOCAL_FN4", MSR_IA32_FEATURE_CONTROL_SENTER_LOCAL_FN_4);
1439 HMVMX_REPORT_MSR_CAP(val, "SENTER_LOCAL_FN5", MSR_IA32_FEATURE_CONTROL_SENTER_LOCAL_FN_5);
1440 HMVMX_REPORT_MSR_CAP(val, "SENTER_LOCAL_FN6", MSR_IA32_FEATURE_CONTROL_SENTER_LOCAL_FN_6);
1441 HMVMX_REPORT_MSR_CAP(val, "SENTER_GLOBAL_EN", MSR_IA32_FEATURE_CONTROL_SENTER_GLOBAL_EN);
1442 HMVMX_REPORT_MSR_CAP(val, "SGX_LAUNCH_EN", MSR_IA32_FEATURE_CONTROL_SGX_LAUNCH_EN);
1443 HMVMX_REPORT_MSR_CAP(val, "SGX_GLOBAL_EN", MSR_IA32_FEATURE_CONTROL_SGX_GLOBAL_EN);
1444 HMVMX_REPORT_MSR_CAP(val, "LMCE", MSR_IA32_FEATURE_CONTROL_LMCE);
1445 if (!(val & MSR_IA32_FEATURE_CONTROL_LOCK))
1446 LogRel(("HM: MSR_IA32_FEATURE_CONTROL lock bit not set, possibly bad hardware!\n"));
1447}
1448
1449
1450/**
1451 * Reports MSR_IA32_VMX_BASIC MSR to the log.
1452 *
1453 * @param uBasicMsr The VMX basic MSR value.
1454 */
1455static void hmR3VmxReportBasicMsr(uint64_t uBasicMsr)
1456{
1457 LogRel(("HM: MSR_IA32_VMX_BASIC = %#RX64\n", uBasicMsr));
1458 LogRel(("HM: VMCS id = %#x\n", RT_BF_GET(uBasicMsr, VMX_BF_BASIC_VMCS_ID)));
1459 LogRel(("HM: VMCS size = %u bytes\n", RT_BF_GET(uBasicMsr, VMX_BF_BASIC_VMCS_SIZE)));
1460 LogRel(("HM: VMCS physical address limit = %s\n", RT_BF_GET(uBasicMsr, VMX_BF_BASIC_PHYSADDR_WIDTH) ?
1461 "< 4 GB" : "None"));
1462 LogRel(("HM: VMCS memory type = %s\n", hmR3VmxGetMemTypeDesc(uBasicMsr)));
1463 LogRel(("HM: Dual-monitor treatment support = %RTbool\n", RT_BF_GET(uBasicMsr, VMX_BF_BASIC_DUAL_MON)));
1464 LogRel(("HM: OUTS & INS instruction-info = %RTbool\n", RT_BF_GET(uBasicMsr, VMX_BF_BASIC_VMCS_INS_OUTS)));
1465 LogRel(("HM: Supports true capability MSRs = %RTbool\n", RT_BF_GET(uBasicMsr, VMX_BF_BASIC_TRUE_CTLS)));
1466}
1467
1468
1469/**
1470 * Reports MSR_IA32_PINBASED_CTLS to the log.
1471 *
1472 * @param pVmxMsr Pointer to the VMX MSR.
1473 */
1474static void hmR3VmxReportPinBasedCtlsMsr(PCVMXCTLSMSR pVmxMsr)
1475{
1476 uint64_t const fAllowed1 = pVmxMsr->n.allowed1;
1477 uint64_t const fAllowed0 = pVmxMsr->n.allowed0;
1478 LogRel(("HM: MSR_IA32_VMX_PINBASED_CTLS = %#RX64\n", pVmxMsr->u));
1479 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "EXT_INT_EXIT", VMX_PIN_CTLS_EXT_INT_EXIT);
1480 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "NMI_EXIT", VMX_PIN_CTLS_NMI_EXIT);
1481 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "VIRTUAL_NMI", VMX_PIN_CTLS_VIRT_NMI);
1482 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "PREEMPT_TIMER", VMX_PIN_CTLS_PREEMPT_TIMER);
1483 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "POSTED_INT", VMX_PIN_CTLS_POSTED_INT);
1484}
1485
1486
1487/**
1488 * Reports MSR_IA32_VMX_PROCBASED_CTLS MSR to the log.
1489 *
1490 * @param pVmxMsr Pointer to the VMX MSR.
1491 */
1492static void hmR3VmxReportProcBasedCtlsMsr(PCVMXCTLSMSR pVmxMsr)
1493{
1494 uint64_t const fAllowed1 = pVmxMsr->n.allowed1;
1495 uint64_t const fAllowed0 = pVmxMsr->n.allowed0;
1496 LogRel(("HM: MSR_IA32_VMX_PROCBASED_CTLS = %#RX64\n", pVmxMsr->u));
1497 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "INT_WINDOW_EXIT", VMX_PROC_CTLS_INT_WINDOW_EXIT);
1498 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "USE_TSC_OFFSETTING", VMX_PROC_CTLS_USE_TSC_OFFSETTING);
1499 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "HLT_EXIT", VMX_PROC_CTLS_HLT_EXIT);
1500 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "INVLPG_EXIT", VMX_PROC_CTLS_INVLPG_EXIT);
1501 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "MWAIT_EXIT", VMX_PROC_CTLS_MWAIT_EXIT);
1502 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "RDPMC_EXIT", VMX_PROC_CTLS_RDPMC_EXIT);
1503 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "RDTSC_EXIT", VMX_PROC_CTLS_RDTSC_EXIT);
1504 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "CR3_LOAD_EXIT", VMX_PROC_CTLS_CR3_LOAD_EXIT);
1505 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "CR3_STORE_EXIT", VMX_PROC_CTLS_CR3_STORE_EXIT);
1506 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "CR8_LOAD_EXIT", VMX_PROC_CTLS_CR8_LOAD_EXIT);
1507 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "CR8_STORE_EXIT", VMX_PROC_CTLS_CR8_STORE_EXIT);
1508 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "USE_TPR_SHADOW", VMX_PROC_CTLS_USE_TPR_SHADOW);
1509 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "NMI_WINDOW_EXIT", VMX_PROC_CTLS_NMI_WINDOW_EXIT);
1510 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "MOV_DR_EXIT", VMX_PROC_CTLS_MOV_DR_EXIT);
1511 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "UNCOND_IO_EXIT", VMX_PROC_CTLS_UNCOND_IO_EXIT);
1512 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "USE_IO_BITMAPS", VMX_PROC_CTLS_USE_IO_BITMAPS);
1513 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "MONITOR_TRAP_FLAG", VMX_PROC_CTLS_MONITOR_TRAP_FLAG);
1514 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "USE_MSR_BITMAPS", VMX_PROC_CTLS_USE_MSR_BITMAPS);
1515 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "MONITOR_EXIT", VMX_PROC_CTLS_MONITOR_EXIT);
1516 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "PAUSE_EXIT", VMX_PROC_CTLS_PAUSE_EXIT);
1517 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "USE_SECONDARY_CTLS", VMX_PROC_CTLS_USE_SECONDARY_CTLS);
1518}
1519
1520
1521/**
1522 * Reports MSR_IA32_VMX_PROCBASED_CTLS2 MSR to the log.
1523 *
1524 * @param pVmxMsr Pointer to the VMX MSR.
1525 */
1526static void hmR3VmxReportProcBasedCtls2Msr(PCVMXCTLSMSR pVmxMsr)
1527{
1528 uint64_t const fAllowed1 = pVmxMsr->n.allowed1;
1529 uint64_t const fAllowed0 = pVmxMsr->n.allowed0;
1530 LogRel(("HM: MSR_IA32_VMX_PROCBASED_CTLS2 = %#RX64\n", pVmxMsr->u));
1531 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "VIRT_APIC_ACCESS", VMX_PROC_CTLS2_VIRT_APIC_ACCESS);
1532 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "EPT", VMX_PROC_CTLS2_EPT);
1533 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "DESC_TABLE_EXIT", VMX_PROC_CTLS2_DESC_TABLE_EXIT);
1534 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "RDTSCP", VMX_PROC_CTLS2_RDTSCP);
1535 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "VIRT_X2APIC_MODE", VMX_PROC_CTLS2_VIRT_X2APIC_MODE);
1536 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "VPID", VMX_PROC_CTLS2_VPID);
1537 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "WBINVD_EXIT", VMX_PROC_CTLS2_WBINVD_EXIT);
1538 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "UNRESTRICTED_GUEST", VMX_PROC_CTLS2_UNRESTRICTED_GUEST);
1539 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "APIC_REG_VIRT", VMX_PROC_CTLS2_APIC_REG_VIRT);
1540 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "VIRT_INT_DELIVERY", VMX_PROC_CTLS2_VIRT_INT_DELIVERY);
1541 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "PAUSE_LOOP_EXIT", VMX_PROC_CTLS2_PAUSE_LOOP_EXIT);
1542 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "RDRAND_EXIT", VMX_PROC_CTLS2_RDRAND_EXIT);
1543 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "INVPCID", VMX_PROC_CTLS2_INVPCID);
1544 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "VMFUNC", VMX_PROC_CTLS2_VMFUNC);
1545 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "VMCS_SHADOWING", VMX_PROC_CTLS2_VMCS_SHADOWING);
1546 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "ENCLS_EXIT", VMX_PROC_CTLS2_ENCLS_EXIT);
1547 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "RDSEED_EXIT", VMX_PROC_CTLS2_RDSEED_EXIT);
1548 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "PML", VMX_PROC_CTLS2_PML);
1549 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "EPT_VE", VMX_PROC_CTLS2_EPT_VE);
1550 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "CONCEAL_FROM_PT", VMX_PROC_CTLS2_CONCEAL_FROM_PT);
1551 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "XSAVES_XRSTORS", VMX_PROC_CTLS2_XSAVES_XRSTORS);
1552 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "TSC_SCALING", VMX_PROC_CTLS2_TSC_SCALING);
1553}
1554
1555
1556/**
1557 * Reports MSR_IA32_VMX_ENTRY_CTLS to the log.
1558 *
1559 * @param pVmxMsr Pointer to the VMX MSR.
1560 */
1561static void hmR3VmxReportEntryCtlsMsr(PCVMXCTLSMSR pVmxMsr)
1562{
1563 uint64_t const fAllowed1 = pVmxMsr->n.allowed1;
1564 uint64_t const fAllowed0 = pVmxMsr->n.allowed0;
1565 LogRel(("HM: MSR_IA32_VMX_ENTRY_CTLS = %#RX64\n", pVmxMsr->u));
1566 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "LOAD_DEBUG", VMX_ENTRY_CTLS_LOAD_DEBUG);
1567 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "IA32E_MODE_GUEST", VMX_ENTRY_CTLS_IA32E_MODE_GUEST);
1568 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "ENTRY_TO_SMM", VMX_ENTRY_CTLS_ENTRY_TO_SMM);
1569 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "DEACTIVATE_DUAL_MON", VMX_ENTRY_CTLS_DEACTIVATE_DUAL_MON);
1570 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "LOAD_PERF_MSR", VMX_ENTRY_CTLS_LOAD_PERF_MSR);
1571 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "LOAD_PAT_MSR", VMX_ENTRY_CTLS_LOAD_PAT_MSR);
1572 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "LOAD_EFER_MSR", VMX_ENTRY_CTLS_LOAD_EFER_MSR);
1573}
1574
1575
1576/**
1577 * Reports MSR_IA32_VMX_EXIT_CTLS to the log.
1578 *
1579 * @param pVmxMsr Pointer to the VMX MSR.
1580 */
1581static void hmR3VmxReportExitCtlsMsr(PCVMXCTLSMSR pVmxMsr)
1582{
1583 uint64_t const fAllowed1 = pVmxMsr->n.allowed1;
1584 uint64_t const fAllowed0 = pVmxMsr->n.allowed0;
1585 LogRel(("HM: MSR_IA32_VMX_EXIT_CTLS = %#RX64\n", pVmxMsr->u));
1586 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "SAVE_DEBUG", VMX_EXIT_CTLS_SAVE_DEBUG);
1587 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "HOST_ADDR_SPACE_SIZE", VMX_EXIT_CTLS_HOST_ADDR_SPACE_SIZE);
1588 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "LOAD_PERF_MSR", VMX_EXIT_CTLS_LOAD_PERF_MSR);
1589 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "ACK_EXT_INT", VMX_EXIT_CTLS_ACK_EXT_INT);
1590 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "SAVE_PAT_MSR", VMX_EXIT_CTLS_SAVE_PAT_MSR);
1591 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "LOAD_PAT_MSR", VMX_EXIT_CTLS_LOAD_PAT_MSR);
1592 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "SAVE_EFER_MSR", VMX_EXIT_CTLS_SAVE_EFER_MSR);
1593 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "LOAD_EFER_MSR", VMX_EXIT_CTLS_LOAD_EFER_MSR);
1594 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "SAVE_PREEMPT_TIMER", VMX_EXIT_CTLS_SAVE_PREEMPT_TIMER);
1595}
1596
1597
1598/**
1599 * Reports MSR_IA32_VMX_EPT_VPID_CAP MSR to the log.
1600 *
1601 * @param fCaps The VMX EPT/VPID capability MSR value.
1602 */
1603static void hmR3VmxReportEptVpidCapsMsr(uint64_t fCaps)
1604{
1605 LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP = %#RX64\n", fCaps));
1606 HMVMX_REPORT_MSR_CAP(fCaps, "RWX_X_ONLY", MSR_IA32_VMX_EPT_VPID_CAP_RWX_X_ONLY);
1607 HMVMX_REPORT_MSR_CAP(fCaps, "PAGE_WALK_LENGTH_4", MSR_IA32_VMX_EPT_VPID_CAP_PAGE_WALK_LENGTH_4);
1608 HMVMX_REPORT_MSR_CAP(fCaps, "EMT_UC", MSR_IA32_VMX_EPT_VPID_CAP_EMT_UC);
1609 HMVMX_REPORT_MSR_CAP(fCaps, "EMT_WB", MSR_IA32_VMX_EPT_VPID_CAP_EMT_WB);
1610 HMVMX_REPORT_MSR_CAP(fCaps, "PDE_2M", MSR_IA32_VMX_EPT_VPID_CAP_PDE_2M);
1611 HMVMX_REPORT_MSR_CAP(fCaps, "PDPTE_1G", MSR_IA32_VMX_EPT_VPID_CAP_PDPTE_1G);
1612 HMVMX_REPORT_MSR_CAP(fCaps, "INVEPT", MSR_IA32_VMX_EPT_VPID_CAP_INVEPT);
1613 HMVMX_REPORT_MSR_CAP(fCaps, "EPT_ACCESS_DIRTY", MSR_IA32_VMX_EPT_VPID_CAP_EPT_ACCESS_DIRTY);
1614 HMVMX_REPORT_MSR_CAP(fCaps, "INVEPT_SINGLE_CONTEXT", MSR_IA32_VMX_EPT_VPID_CAP_INVEPT_SINGLE_CONTEXT);
1615 HMVMX_REPORT_MSR_CAP(fCaps, "INVEPT_ALL_CONTEXTS", MSR_IA32_VMX_EPT_VPID_CAP_INVEPT_ALL_CONTEXTS);
1616 HMVMX_REPORT_MSR_CAP(fCaps, "INVVPID", MSR_IA32_VMX_EPT_VPID_CAP_INVVPID);
1617 HMVMX_REPORT_MSR_CAP(fCaps, "INVVPID_INDIV_ADDR", MSR_IA32_VMX_EPT_VPID_CAP_INVVPID_INDIV_ADDR);
1618 HMVMX_REPORT_MSR_CAP(fCaps, "INVVPID_SINGLE_CONTEXT", MSR_IA32_VMX_EPT_VPID_CAP_INVVPID_SINGLE_CONTEXT);
1619 HMVMX_REPORT_MSR_CAP(fCaps, "INVVPID_ALL_CONTEXTS", MSR_IA32_VMX_EPT_VPID_CAP_INVVPID_ALL_CONTEXTS);
1620 HMVMX_REPORT_MSR_CAP(fCaps, "INVVPID_SINGLE_CONTEXT_RETAIN_GLOBALS", MSR_IA32_VMX_EPT_VPID_CAP_INVVPID_SINGLE_CONTEXT_RETAIN_GLOBALS);
1621}
1622
1623
1624/**
1625 * Reports MSR_IA32_VMX_MISC MSR to the log.
1626 *
1627 * @param pVM Pointer to the VM.
1628 * @param fMisc The VMX misc. MSR value.
1629 */
1630static void hmR3VmxReportMiscMsr(PVM pVM, uint64_t fMisc)
1631{
1632 LogRel(("HM: MSR_IA32_VMX_MISC = %#RX64\n", fMisc));
1633 uint8_t const cPreemptTimerShift = RT_BF_GET(fMisc, VMX_BF_MISC_PREEMPT_TIMER_TSC);
1634 if (cPreemptTimerShift == pVM->hm.s.vmx.cPreemptTimerShift)
1635 LogRel(("HM: PREEMPT_TIMER_TSC = %#x\n", cPreemptTimerShift));
1636 else
1637 {
1638 LogRel(("HM: PREEMPT_TIMER_TSC = %#x - erratum detected, using %#x instead\n", cPreemptTimerShift,
1639 pVM->hm.s.vmx.cPreemptTimerShift));
1640 }
1641 LogRel(("HM: EXIT_SAVE_EFER_LMA = %RTbool\n", RT_BF_GET(fMisc, VMX_BF_MISC_EXIT_SAVE_EFER_LMA)));
1642 LogRel(("HM: ACTIVITY_STATES = %#x%s\n", RT_BF_GET(fMisc, VMX_BF_MISC_ACTIVITY_STATES),
1643 hmR3VmxGetActivityStateAllDesc(fMisc)));
1644 LogRel(("HM: INTEL_PT = %RTbool\n", RT_BF_GET(fMisc, VMX_BF_MISC_INTEL_PT)));
1645 LogRel(("HM: SMM_READ_SMBASE_MSR = %RTbool\n", RT_BF_GET(fMisc, VMX_BF_MISC_SMM_READ_SMBASE_MSR)));
1646 LogRel(("HM: CR3_TARGET = %#x\n", RT_BF_GET(fMisc, VMX_BF_MISC_CR3_TARGET)));
1647 LogRel(("HM: MAX_MSR = %#x ( %u )\n", RT_BF_GET(fMisc, VMX_BF_MISC_MAX_MSRS),
1648 VMX_MISC_MAX_MSRS(fMisc)));
1649 LogRel(("HM: VMXOFF_BLOCK_SMI = %RTbool\n", RT_BF_GET(fMisc, VMX_BF_MISC_VMXOFF_BLOCK_SMI)));
1650 LogRel(("HM: VMWRITE_ALL = %RTbool\n", RT_BF_GET(fMisc, VMX_BF_MISC_VMWRITE_ALL)));
1651 LogRel(("HM: ENTRY_INJECT_SOFT_INT = %#x\n", RT_BF_GET(fMisc, VMX_BF_MISC_ENTRY_INJECT_SOFT_INT)));
1652 LogRel(("HM: MSEG_ID = %#x\n", RT_BF_GET(fMisc, VMX_BF_MISC_MSEG_ID)));
1653}
1654
1655
1656/**
1657 * Reports MSR_IA32_VMX_VMCS_ENUM MSR to the log.
1658 *
1659 * @param uVmcsEnum The VMX VMCS enum MSR value.
1660 */
1661static void hmR3VmxReportVmcsEnumMsr(uint64_t uVmcsEnum)
1662{
1663 LogRel(("HM: MSR_IA32_VMX_VMCS_ENUM = %#RX64\n", uVmcsEnum));
1664 LogRel(("HM: HIGHEST_IDX = %#x\n", RT_BF_GET(uVmcsEnum, VMX_BF_VMCS_ENUM_HIGHEST_IDX)));
1665}
1666
1667
1668/**
1669 * Reports MSR_IA32_VMX_VMFUNC MSR to the log.
1670 *
1671 * @param uVmFunc The VMX VMFUNC MSR value.
1672 */
1673static void hmR3VmxReportVmFuncMsr(uint64_t uVmFunc)
1674{
1675 LogRel(("HM: MSR_IA32_VMX_VMFUNC = %#RX64\n", uVmFunc));
1676 HMVMX_REPORT_ALLOWED_FEAT(uVmFunc, "EPTP_SWITCHING", RT_BF_GET(uVmFunc, VMX_BF_VMFUNC_EPTP_SWITCHING));
1677}
1678
1679
1680/**
1681 * Reports VMX CR0, CR4 fixed MSRs.
1682 *
1683 * @param pMsrs Pointer to the VMX MSRs.
1684 */
1685static void hmR3VmxReportCrFixedMsrs(PVMXMSRS pMsrs)
1686{
1687 LogRel(("HM: MSR_IA32_VMX_CR0_FIXED0 = %#RX64\n", pMsrs->u64Cr0Fixed0));
1688 LogRel(("HM: MSR_IA32_VMX_CR0_FIXED1 = %#RX64\n", pMsrs->u64Cr0Fixed1));
1689 LogRel(("HM: MSR_IA32_VMX_CR4_FIXED0 = %#RX64\n", pMsrs->u64Cr4Fixed0));
1690 LogRel(("HM: MSR_IA32_VMX_CR4_FIXED1 = %#RX64\n", pMsrs->u64Cr4Fixed1));
1691}
1692
1693
1694/**
1695 * Finish VT-x initialization (after ring-0 init).
1696 *
1697 * @returns VBox status code.
1698 * @param pVM The cross context VM structure.
1699 */
1700static int hmR3InitFinalizeR0Intel(PVM pVM)
1701{
1702 int rc;
1703
1704 Log(("pVM->hm.s.vmx.fSupported = %d\n", pVM->hm.s.vmx.fSupported));
1705 AssertLogRelReturn(pVM->hm.s.vmx.Msrs.u64FeatCtrl != 0, VERR_HM_IPE_4);
1706
1707 LogRel(("HM: Using VT-x implementation 2.0\n"));
1708 LogRel(("HM: Max resume loops = %u\n", pVM->hm.s.cMaxResumeLoops));
1709 LogRel(("HM: Host CR4 = %#RX64\n", pVM->hm.s.vmx.u64HostCr4));
1710 LogRel(("HM: Host EFER = %#RX64\n", pVM->hm.s.vmx.u64HostEfer));
1711 LogRel(("HM: MSR_IA32_SMM_MONITOR_CTL = %#RX64\n", pVM->hm.s.vmx.u64HostSmmMonitorCtl));
1712
1713 hmR3VmxReportFeatCtlMsr(pVM->hm.s.vmx.Msrs.u64FeatCtrl);
1714 hmR3VmxReportBasicMsr(pVM->hm.s.vmx.Msrs.u64Basic);
1715
1716 hmR3VmxReportPinBasedCtlsMsr(&pVM->hm.s.vmx.Msrs.PinCtls);
1717 hmR3VmxReportProcBasedCtlsMsr(&pVM->hm.s.vmx.Msrs.ProcCtls);
1718 if (pVM->hm.s.vmx.Msrs.ProcCtls.n.allowed1 & VMX_PROC_CTLS_USE_SECONDARY_CTLS)
1719 hmR3VmxReportProcBasedCtls2Msr(&pVM->hm.s.vmx.Msrs.ProcCtls2);
1720
1721 hmR3VmxReportEntryCtlsMsr(&pVM->hm.s.vmx.Msrs.EntryCtls);
1722 hmR3VmxReportExitCtlsMsr(&pVM->hm.s.vmx.Msrs.ExitCtls);
1723
1724 if (RT_BF_GET(pVM->hm.s.vmx.Msrs.u64Basic, VMX_BF_BASIC_TRUE_CTLS))
1725 {
1726 /* We don't extensively dump the true capability MSRs as we don't use them, see @bugref{9180#c5}. */
1727 LogRel(("HM: MSR_IA32_VMX_TRUE_PINBASED_CTLS = %#RX64\n", pVM->hm.s.vmx.Msrs.TruePinCtls));
1728 LogRel(("HM: MSR_IA32_VMX_TRUE_PROCBASED_CTLS = %#RX64\n", pVM->hm.s.vmx.Msrs.TrueProcCtls));
1729 LogRel(("HM: MSR_IA32_VMX_TRUE_ENTRY_CTLS = %#RX64\n", pVM->hm.s.vmx.Msrs.TrueEntryCtls));
1730 LogRel(("HM: MSR_IA32_VMX_TRUE_EXIT_CTLS = %#RX64\n", pVM->hm.s.vmx.Msrs.TrueExitCtls));
1731 }
1732
1733 hmR3VmxReportMiscMsr(pVM, pVM->hm.s.vmx.Msrs.u64Misc);
1734 hmR3VmxReportVmcsEnumMsr(pVM->hm.s.vmx.Msrs.u64VmcsEnum);
1735 if (pVM->hm.s.vmx.Msrs.u64EptVpidCaps)
1736 hmR3VmxReportEptVpidCapsMsr(pVM->hm.s.vmx.Msrs.u64EptVpidCaps);
1737 if (pVM->hm.s.vmx.Msrs.u64VmFunc)
1738 hmR3VmxReportVmFuncMsr(pVM->hm.s.vmx.Msrs.u64VmFunc);
1739 hmR3VmxReportCrFixedMsrs(&pVM->hm.s.vmx.Msrs);
1740
1741 LogRel(("HM: APIC-access page physaddr = %#RHp\n", pVM->hm.s.vmx.HCPhysApicAccess));
1742 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1743 {
1744 LogRel(("HM: VCPU%3d: MSR bitmap physaddr = %#RHp\n", i, pVM->aCpus[i].hm.s.vmx.HCPhysMsrBitmap));
1745 LogRel(("HM: VCPU%3d: VMCS physaddr = %#RHp\n", i, pVM->aCpus[i].hm.s.vmx.HCPhysVmcs));
1746 }
1747
1748 /*
1749 * EPT and unrestricted guest execution are determined in HMR3Init, verify the sanity of that.
1750 */
1751 AssertLogRelReturn( !pVM->hm.s.fNestedPaging
1752 || (pVM->hm.s.vmx.Msrs.ProcCtls2.n.allowed1 & VMX_PROC_CTLS2_EPT),
1753 VERR_HM_IPE_1);
1754 AssertLogRelReturn( !pVM->hm.s.vmx.fUnrestrictedGuest
1755 || ( (pVM->hm.s.vmx.Msrs.ProcCtls2.n.allowed1 & VMX_PROC_CTLS2_UNRESTRICTED_GUEST)
1756 && pVM->hm.s.fNestedPaging),
1757 VERR_HM_IPE_1);
1758
1759 /*
1760 * Enable VPID if configured and supported.
1761 */
1762 if (pVM->hm.s.vmx.Msrs.ProcCtls2.n.allowed1 & VMX_PROC_CTLS2_VPID)
1763 pVM->hm.s.vmx.fVpid = pVM->hm.s.vmx.fAllowVpid;
1764
1765#if 0
1766 /*
1767 * Enable APIC register virtualization and virtual-interrupt delivery if supported.
1768 */
1769 if ( (pVM->hm.s.vmx.Msrs.ProcCtls2.n.allowed1 & VMX_PROC_CTLS2_APIC_REG_VIRT)
1770 && (pVM->hm.s.vmx.Msrs.ProcCtls2.n.allowed1 & VMX_PROC_CTLS2_VIRT_INTR_DELIVERY))
1771 pVM->hm.s.fVirtApicRegs = true;
1772
1773 /*
1774 * Enable posted-interrupt processing if supported.
1775 */
1776 /** @todo Add and query IPRT API for host OS support for posted-interrupt IPI
1777 * here. */
1778 if ( (pVM->hm.s.vmx.Msrs.PinCtls.n.allowed1 & VMX_PIN_CTLS_POSTED_INT)
1779 && (pVM->hm.s.vmx.Msrs.ExitCtls.n.allowed1 & VMX_EXIT_CTLS_ACK_EXT_INT))
1780 pVM->hm.s.fPostedIntrs = true;
1781#endif
1782
1783 /*
1784 * Disallow RDTSCP in the guest if there is no secondary process-based VM execution controls as otherwise
1785 * RDTSCP would cause a #UD. There might be no CPUs out there where this happens, as RDTSCP was introduced
1786 * in Nehalems and secondary VM exec. controls should be supported in all of them, but nonetheless it's Intel...
1787 */
1788 if ( !(pVM->hm.s.vmx.Msrs.ProcCtls.n.allowed1 & VMX_PROC_CTLS_USE_SECONDARY_CTLS)
1789 && CPUMR3GetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_RDTSCP))
1790 {
1791 CPUMR3ClearGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_RDTSCP);
1792 LogRel(("HM: Disabled RDTSCP\n"));
1793 }
1794
1795 if (!pVM->hm.s.vmx.fUnrestrictedGuest)
1796 {
1797 /* Allocate three pages for the TSS we need for real mode emulation. (2 pages for the IO bitmap) */
1798 rc = PDMR3VmmDevHeapAlloc(pVM, HM_VTX_TOTAL_DEVHEAP_MEM, hmR3VmmDevHeapNotify, (RTR3PTR *)&pVM->hm.s.vmx.pRealModeTSS);
1799 if (RT_SUCCESS(rc))
1800 {
1801 /* The IO bitmap starts right after the virtual interrupt redirection bitmap.
1802 Refer Intel spec. 20.3.3 "Software Interrupt Handling in Virtual-8086 mode"
1803 esp. Figure 20-5.*/
1804 ASMMemZero32(pVM->hm.s.vmx.pRealModeTSS, sizeof(*pVM->hm.s.vmx.pRealModeTSS));
1805 pVM->hm.s.vmx.pRealModeTSS->offIoBitmap = sizeof(*pVM->hm.s.vmx.pRealModeTSS);
1806
1807 /* Bit set to 0 means software interrupts are redirected to the
1808 8086 program interrupt handler rather than switching to
1809 protected-mode handler. */
1810 memset(pVM->hm.s.vmx.pRealModeTSS->IntRedirBitmap, 0, sizeof(pVM->hm.s.vmx.pRealModeTSS->IntRedirBitmap));
1811
1812 /* Allow all port IO, so that port IO instructions do not cause
1813 exceptions and would instead cause a VM-exit (based on VT-x's
1814 IO bitmap which we currently configure to always cause an exit). */
1815 memset(pVM->hm.s.vmx.pRealModeTSS + 1, 0, PAGE_SIZE * 2);
1816 *((unsigned char *)pVM->hm.s.vmx.pRealModeTSS + HM_VTX_TSS_SIZE - 2) = 0xff;
1817
1818 /*
1819 * Construct a 1024 element page directory with 4 MB pages for the identity mapped
1820 * page table used in real and protected mode without paging with EPT.
1821 */
1822 pVM->hm.s.vmx.pNonPagingModeEPTPageTable = (PX86PD)((char *)pVM->hm.s.vmx.pRealModeTSS + PAGE_SIZE * 3);
1823 for (uint32_t i = 0; i < X86_PG_ENTRIES; i++)
1824 {
1825 pVM->hm.s.vmx.pNonPagingModeEPTPageTable->a[i].u = _4M * i;
1826 pVM->hm.s.vmx.pNonPagingModeEPTPageTable->a[i].u |= X86_PDE4M_P | X86_PDE4M_RW | X86_PDE4M_US
1827 | X86_PDE4M_A | X86_PDE4M_D | X86_PDE4M_PS
1828 | X86_PDE4M_G;
1829 }
1830
1831 /* We convert it here every time as PCI regions could be reconfigured. */
1832 if (PDMVmmDevHeapIsEnabled(pVM))
1833 {
1834 RTGCPHYS GCPhys;
1835 rc = PDMVmmDevHeapR3ToGCPhys(pVM, pVM->hm.s.vmx.pRealModeTSS, &GCPhys);
1836 AssertRCReturn(rc, rc);
1837 LogRel(("HM: Real Mode TSS guest physaddr = %#RGp\n", GCPhys));
1838
1839 rc = PDMVmmDevHeapR3ToGCPhys(pVM, pVM->hm.s.vmx.pNonPagingModeEPTPageTable, &GCPhys);
1840 AssertRCReturn(rc, rc);
1841 LogRel(("HM: Non-Paging Mode EPT CR3 = %#RGp\n", GCPhys));
1842 }
1843 }
1844 else
1845 {
1846 LogRel(("HM: No real mode VT-x support (PDMR3VMMDevHeapAlloc returned %Rrc)\n", rc));
1847 pVM->hm.s.vmx.pRealModeTSS = NULL;
1848 pVM->hm.s.vmx.pNonPagingModeEPTPageTable = NULL;
1849 return VMSetError(pVM, rc, RT_SRC_POS,
1850 "HM failure: No real mode VT-x support (PDMR3VMMDevHeapAlloc returned %Rrc)", rc);
1851 }
1852 }
1853
1854 LogRel((pVM->hm.s.fAllow64BitGuests
1855 ? "HM: Guest support: 32-bit and 64-bit\n"
1856 : "HM: Guest support: 32-bit only\n"));
1857
1858 /*
1859 * Call ring-0 to set up the VM.
1860 */
1861 rc = SUPR3CallVMMR0Ex(pVM->pVMR0, 0 /* idCpu */, VMMR0_DO_HM_SETUP_VM, 0 /* u64Arg */, NULL /* pReqHdr */);
1862 if (rc != VINF_SUCCESS)
1863 {
1864 LogRel(("HM: VMX setup failed with rc=%Rrc!\n", rc));
1865 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1866 {
1867 PVMCPU pVCpu = &pVM->aCpus[i];
1868 LogRel(("HM: CPU[%u] Last instruction error %#x\n", i, pVCpu->hm.s.vmx.LastError.u32InstrError));
1869 LogRel(("HM: CPU[%u] HM error %#x (%u)\n", i, pVCpu->hm.s.u32HMError, pVCpu->hm.s.u32HMError));
1870 }
1871 HMR3CheckError(pVM, rc);
1872 return VMSetError(pVM, rc, RT_SRC_POS, "VT-x setup failed: %Rrc", rc);
1873 }
1874
1875 LogRel(("HM: Supports VMCS EFER fields = %RTbool\n", pVM->hm.s.vmx.fSupportsVmcsEfer));
1876 LogRel(("HM: Enabled VMX\n"));
1877 pVM->hm.s.vmx.fEnabled = true;
1878
1879 hmR3DisableRawMode(pVM); /** @todo make this go away! */
1880
1881 /*
1882 * Change the CPU features.
1883 */
1884 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_SEP);
1885 if (pVM->hm.s.fAllow64BitGuests)
1886 {
1887 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_PAE);
1888 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_LONG_MODE);
1889 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_SYSCALL); /* 64 bits only on Intel CPUs */
1890 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_LAHF);
1891 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_NX);
1892 }
1893 /* Turn on NXE if PAE has been enabled *and* the host has turned on NXE
1894 (we reuse the host EFER in the switcher). */
1895 /** @todo this needs to be fixed properly!! */
1896 else if (CPUMR3GetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_PAE))
1897 {
1898 if (pVM->hm.s.vmx.u64HostEfer & MSR_K6_EFER_NXE)
1899 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_NX);
1900 else
1901 LogRel(("HM: NX not enabled on the host, unavailable to PAE guest\n"));
1902 }
1903
1904 /*
1905 * Log configuration details.
1906 */
1907 if (pVM->hm.s.fNestedPaging)
1908 {
1909 LogRel(("HM: Enabled nested paging\n"));
1910 if (pVM->hm.s.vmx.enmTlbFlushEpt == VMXTLBFLUSHEPT_SINGLE_CONTEXT)
1911 LogRel(("HM: EPT flush type = Single context\n"));
1912 else if (pVM->hm.s.vmx.enmTlbFlushEpt == VMXTLBFLUSHEPT_ALL_CONTEXTS)
1913 LogRel(("HM: EPT flush type = All contexts\n"));
1914 else if (pVM->hm.s.vmx.enmTlbFlushEpt == VMXTLBFLUSHEPT_NOT_SUPPORTED)
1915 LogRel(("HM: EPT flush type = Not supported\n"));
1916 else
1917 LogRel(("HM: EPT flush type = %#x\n", pVM->hm.s.vmx.enmTlbFlushEpt));
1918
1919 if (pVM->hm.s.vmx.fUnrestrictedGuest)
1920 LogRel(("HM: Enabled unrestricted guest execution\n"));
1921
1922#if HC_ARCH_BITS == 64
1923 if (pVM->hm.s.fLargePages)
1924 {
1925 /* Use large (2 MB) pages for our EPT PDEs where possible. */
1926 PGMSetLargePageUsage(pVM, true);
1927 LogRel(("HM: Enabled large page support\n"));
1928 }
1929#endif
1930 }
1931 else
1932 Assert(!pVM->hm.s.vmx.fUnrestrictedGuest);
1933
1934 if (pVM->hm.s.fVirtApicRegs)
1935 LogRel(("HM: Enabled APIC-register virtualization support\n"));
1936
1937 if (pVM->hm.s.fPostedIntrs)
1938 LogRel(("HM: Enabled posted-interrupt processing support\n"));
1939
1940 if (pVM->hm.s.vmx.fVpid)
1941 {
1942 LogRel(("HM: Enabled VPID\n"));
1943 if (pVM->hm.s.vmx.enmTlbFlushVpid == VMXTLBFLUSHVPID_INDIV_ADDR)
1944 LogRel(("HM: VPID flush type = Individual addresses\n"));
1945 else if (pVM->hm.s.vmx.enmTlbFlushVpid == VMXTLBFLUSHVPID_SINGLE_CONTEXT)
1946 LogRel(("HM: VPID flush type = Single context\n"));
1947 else if (pVM->hm.s.vmx.enmTlbFlushVpid == VMXTLBFLUSHVPID_ALL_CONTEXTS)
1948 LogRel(("HM: VPID flush type = All contexts\n"));
1949 else if (pVM->hm.s.vmx.enmTlbFlushVpid == VMXTLBFLUSHVPID_SINGLE_CONTEXT_RETAIN_GLOBALS)
1950 LogRel(("HM: VPID flush type = Single context retain globals\n"));
1951 else
1952 LogRel(("HM: VPID flush type = %#x\n", pVM->hm.s.vmx.enmTlbFlushVpid));
1953 }
1954 else if (pVM->hm.s.vmx.enmTlbFlushVpid == VMXTLBFLUSHVPID_NOT_SUPPORTED)
1955 LogRel(("HM: Ignoring VPID capabilities of CPU\n"));
1956
1957 if (pVM->hm.s.vmx.fUsePreemptTimer)
1958 LogRel(("HM: Enabled VMX-preemption timer (cPreemptTimerShift=%u)\n", pVM->hm.s.vmx.cPreemptTimerShift));
1959 else
1960 LogRel(("HM: Disabled VMX-preemption timer\n"));
1961
1962 return VINF_SUCCESS;
1963}
1964
1965
1966/**
1967 * Finish AMD-V initialization (after ring-0 init).
1968 *
1969 * @returns VBox status code.
1970 * @param pVM The cross context VM structure.
1971 */
1972static int hmR3InitFinalizeR0Amd(PVM pVM)
1973{
1974 Log(("pVM->hm.s.svm.fSupported = %d\n", pVM->hm.s.svm.fSupported));
1975
1976 LogRel(("HM: Using AMD-V implementation 2.0\n"));
1977
1978 uint32_t u32Family;
1979 uint32_t u32Model;
1980 uint32_t u32Stepping;
1981 if (HMSvmIsSubjectToErratum170(&u32Family, &u32Model, &u32Stepping))
1982 LogRel(("HM: AMD Cpu with erratum 170 family %#x model %#x stepping %#x\n", u32Family, u32Model, u32Stepping));
1983 LogRel(("HM: Max resume loops = %u\n", pVM->hm.s.cMaxResumeLoops));
1984 LogRel(("HM: AMD HWCR MSR = %#RX64\n", pVM->hm.s.svm.u64MsrHwcr));
1985 LogRel(("HM: AMD-V revision = %#x\n", pVM->hm.s.svm.u32Rev));
1986 LogRel(("HM: AMD-V max ASID = %RU32\n", pVM->hm.s.uMaxAsid));
1987 LogRel(("HM: AMD-V features = %#x\n", pVM->hm.s.svm.u32Features));
1988
1989 /*
1990 * Enumerate AMD-V features.
1991 */
1992 static const struct { uint32_t fFlag; const char *pszName; } s_aSvmFeatures[] =
1993 {
1994#define HMSVM_REPORT_FEATURE(a_StrDesc, a_Define) { a_Define, a_StrDesc }
1995 HMSVM_REPORT_FEATURE("NESTED_PAGING", X86_CPUID_SVM_FEATURE_EDX_NESTED_PAGING),
1996 HMSVM_REPORT_FEATURE("LBR_VIRT", X86_CPUID_SVM_FEATURE_EDX_LBR_VIRT),
1997 HMSVM_REPORT_FEATURE("SVM_LOCK", X86_CPUID_SVM_FEATURE_EDX_SVM_LOCK),
1998 HMSVM_REPORT_FEATURE("NRIP_SAVE", X86_CPUID_SVM_FEATURE_EDX_NRIP_SAVE),
1999 HMSVM_REPORT_FEATURE("TSC_RATE_MSR", X86_CPUID_SVM_FEATURE_EDX_TSC_RATE_MSR),
2000 HMSVM_REPORT_FEATURE("VMCB_CLEAN", X86_CPUID_SVM_FEATURE_EDX_VMCB_CLEAN),
2001 HMSVM_REPORT_FEATURE("FLUSH_BY_ASID", X86_CPUID_SVM_FEATURE_EDX_FLUSH_BY_ASID),
2002 HMSVM_REPORT_FEATURE("DECODE_ASSISTS", X86_CPUID_SVM_FEATURE_EDX_DECODE_ASSISTS),
2003 HMSVM_REPORT_FEATURE("PAUSE_FILTER", X86_CPUID_SVM_FEATURE_EDX_PAUSE_FILTER),
2004 HMSVM_REPORT_FEATURE("PAUSE_FILTER_THRESHOLD", X86_CPUID_SVM_FEATURE_EDX_PAUSE_FILTER_THRESHOLD),
2005 HMSVM_REPORT_FEATURE("AVIC", X86_CPUID_SVM_FEATURE_EDX_AVIC),
2006 HMSVM_REPORT_FEATURE("VIRT_VMSAVE_VMLOAD", X86_CPUID_SVM_FEATURE_EDX_VIRT_VMSAVE_VMLOAD),
2007 HMSVM_REPORT_FEATURE("VGIF", X86_CPUID_SVM_FEATURE_EDX_VGIF),
2008#undef HMSVM_REPORT_FEATURE
2009 };
2010
2011 uint32_t fSvmFeatures = pVM->hm.s.svm.u32Features;
2012 for (unsigned i = 0; i < RT_ELEMENTS(s_aSvmFeatures); i++)
2013 if (fSvmFeatures & s_aSvmFeatures[i].fFlag)
2014 {
2015 LogRel(("HM: %s\n", s_aSvmFeatures[i].pszName));
2016 fSvmFeatures &= ~s_aSvmFeatures[i].fFlag;
2017 }
2018 if (fSvmFeatures)
2019 for (unsigned iBit = 0; iBit < 32; iBit++)
2020 if (RT_BIT_32(iBit) & fSvmFeatures)
2021 LogRel(("HM: Reserved bit %u\n", iBit));
2022
2023 /*
2024 * Nested paging is determined in HMR3Init, verify the sanity of that.
2025 */
2026 AssertLogRelReturn( !pVM->hm.s.fNestedPaging
2027 || (pVM->hm.s.svm.u32Features & X86_CPUID_SVM_FEATURE_EDX_NESTED_PAGING),
2028 VERR_HM_IPE_1);
2029
2030#if 0
2031 /** @todo Add and query IPRT API for host OS support for posted-interrupt IPI
2032 * here. */
2033 if (RTR0IsPostIpiSupport())
2034 pVM->hm.s.fPostedIntrs = true;
2035#endif
2036
2037 /*
2038 * Call ring-0 to set up the VM.
2039 */
2040 int rc = SUPR3CallVMMR0Ex(pVM->pVMR0, 0 /*idCpu*/, VMMR0_DO_HM_SETUP_VM, 0, NULL);
2041 if (rc != VINF_SUCCESS)
2042 {
2043 AssertMsgFailed(("%Rrc\n", rc));
2044 LogRel(("HM: AMD-V setup failed with rc=%Rrc!\n", rc));
2045 return VMSetError(pVM, rc, RT_SRC_POS, "AMD-V setup failed: %Rrc", rc);
2046 }
2047
2048 LogRel(("HM: Enabled SVM\n"));
2049 pVM->hm.s.svm.fEnabled = true;
2050
2051 if (pVM->hm.s.fNestedPaging)
2052 {
2053 LogRel(("HM: Enabled nested paging\n"));
2054
2055 /*
2056 * Enable large pages (2 MB) if applicable.
2057 */
2058#if HC_ARCH_BITS == 64
2059 if (pVM->hm.s.fLargePages)
2060 {
2061 PGMSetLargePageUsage(pVM, true);
2062 LogRel(("HM: Enabled large page support\n"));
2063 }
2064#endif
2065 }
2066
2067 if (pVM->hm.s.fVirtApicRegs)
2068 LogRel(("HM: Enabled APIC-register virtualization support\n"));
2069
2070 if (pVM->hm.s.fPostedIntrs)
2071 LogRel(("HM: Enabled posted-interrupt processing support\n"));
2072
2073 hmR3DisableRawMode(pVM);
2074
2075 /*
2076 * Change the CPU features.
2077 */
2078 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_SEP);
2079 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_SYSCALL);
2080 if (pVM->hm.s.fAllow64BitGuests)
2081 {
2082 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_PAE);
2083 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_LONG_MODE);
2084 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_NX);
2085 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_LAHF);
2086 }
2087 /* Turn on NXE if PAE has been enabled. */
2088 else if (CPUMR3GetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_PAE))
2089 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_NX);
2090
2091 LogRel(("HM: %s TPR patching\n", (pVM->hm.s.fTprPatchingAllowed) ? "Enabled" : "Disabled"));
2092
2093 LogRel((pVM->hm.s.fAllow64BitGuests
2094 ? "HM: Guest support: 32-bit and 64-bit\n"
2095 : "HM: Guest support: 32-bit only\n"));
2096
2097 return VINF_SUCCESS;
2098}
2099
2100
2101/**
2102 * Applies relocations to data and code managed by this
2103 * component. This function will be called at init and
2104 * whenever the VMM need to relocate it self inside the GC.
2105 *
2106 * @param pVM The cross context VM structure.
2107 */
2108VMMR3_INT_DECL(void) HMR3Relocate(PVM pVM)
2109{
2110 Log(("HMR3Relocate to %RGv\n", MMHyperGetArea(pVM, 0)));
2111
2112 /* Fetch the current paging mode during the relocate callback during state loading. */
2113 if (VMR3GetState(pVM) == VMSTATE_LOADING)
2114 {
2115 for (VMCPUID i = 0; i < pVM->cCpus; i++)
2116 {
2117 PVMCPU pVCpu = &pVM->aCpus[i];
2118 pVCpu->hm.s.enmShadowMode = PGMGetShadowMode(pVCpu);
2119 }
2120 }
2121#if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS)
2122 if (HMIsEnabled(pVM))
2123 {
2124 switch (PGMGetHostMode(pVM))
2125 {
2126 case PGMMODE_32_BIT:
2127 pVM->hm.s.pfnHost32ToGuest64R0 = VMMR3GetHostToGuestSwitcher(pVM, VMMSWITCHER_32_TO_AMD64);
2128 break;
2129
2130 case PGMMODE_PAE:
2131 case PGMMODE_PAE_NX:
2132 pVM->hm.s.pfnHost32ToGuest64R0 = VMMR3GetHostToGuestSwitcher(pVM, VMMSWITCHER_PAE_TO_AMD64);
2133 break;
2134
2135 default:
2136 AssertFailed();
2137 break;
2138 }
2139 }
2140#endif
2141 return;
2142}
2143
2144
2145/**
2146 * Terminates the HM.
2147 *
2148 * Termination means cleaning up and freeing all resources,
2149 * the VM itself is, at this point, powered off or suspended.
2150 *
2151 * @returns VBox status code.
2152 * @param pVM The cross context VM structure.
2153 */
2154VMMR3_INT_DECL(int) HMR3Term(PVM pVM)
2155{
2156 if (pVM->hm.s.vmx.pRealModeTSS)
2157 {
2158 PDMR3VmmDevHeapFree(pVM, pVM->hm.s.vmx.pRealModeTSS);
2159 pVM->hm.s.vmx.pRealModeTSS = 0;
2160 }
2161 hmR3TermCPU(pVM);
2162 return 0;
2163}
2164
2165
2166/**
2167 * Terminates the per-VCPU HM.
2168 *
2169 * @returns VBox status code.
2170 * @param pVM The cross context VM structure.
2171 */
2172static int hmR3TermCPU(PVM pVM)
2173{
2174 for (VMCPUID i = 0; i < pVM->cCpus; i++)
2175 {
2176 PVMCPU pVCpu = &pVM->aCpus[i]; NOREF(pVCpu);
2177
2178#ifdef VBOX_WITH_STATISTICS
2179 if (pVCpu->hm.s.paStatExitReason)
2180 {
2181 MMHyperFree(pVM, pVCpu->hm.s.paStatExitReason);
2182 pVCpu->hm.s.paStatExitReason = NULL;
2183 pVCpu->hm.s.paStatExitReasonR0 = NIL_RTR0PTR;
2184 }
2185 if (pVCpu->hm.s.paStatInjectedIrqs)
2186 {
2187 MMHyperFree(pVM, pVCpu->hm.s.paStatInjectedIrqs);
2188 pVCpu->hm.s.paStatInjectedIrqs = NULL;
2189 pVCpu->hm.s.paStatInjectedIrqsR0 = NIL_RTR0PTR;
2190 }
2191#endif
2192
2193#ifdef VBOX_WITH_CRASHDUMP_MAGIC
2194 memset(pVCpu->hm.s.vmx.VMCSCache.aMagic, 0, sizeof(pVCpu->hm.s.vmx.VMCSCache.aMagic));
2195 pVCpu->hm.s.vmx.VMCSCache.uMagic = 0;
2196 pVCpu->hm.s.vmx.VMCSCache.uPos = 0xffffffff;
2197#endif
2198 }
2199 return 0;
2200}
2201
2202
2203/**
2204 * Resets a virtual CPU.
2205 *
2206 * Used by HMR3Reset and CPU hot plugging.
2207 *
2208 * @param pVCpu The cross context virtual CPU structure to reset.
2209 */
2210VMMR3_INT_DECL(void) HMR3ResetCpu(PVMCPU pVCpu)
2211{
2212 /* Sync. entire state on VM reset R0-reentry. It's safe to reset
2213 the HM flags here, all other EMTs are in ring-3. See VMR3Reset(). */
2214 pVCpu->hm.s.fCtxChanged |= HM_CHANGED_HOST_CONTEXT | HM_CHANGED_ALL_GUEST;
2215
2216 pVCpu->hm.s.fActive = false;
2217 pVCpu->hm.s.Event.fPending = false;
2218 pVCpu->hm.s.vmx.fWasInRealMode = true;
2219 pVCpu->hm.s.vmx.u64MsrApicBase = 0;
2220 pVCpu->hm.s.vmx.fSwitchedTo64on32 = false;
2221
2222 /* Reset the contents of the read cache. */
2223 PVMCSCACHE pCache = &pVCpu->hm.s.vmx.VMCSCache;
2224 for (unsigned j = 0; j < pCache->Read.cValidEntries; j++)
2225 pCache->Read.aFieldVal[j] = 0;
2226
2227#ifdef VBOX_WITH_CRASHDUMP_MAGIC
2228 /* Magic marker for searching in crash dumps. */
2229 strcpy((char *)pCache->aMagic, "VMCSCACHE Magic");
2230 pCache->uMagic = UINT64_C(0xdeadbeefdeadbeef);
2231#endif
2232}
2233
2234
2235/**
2236 * The VM is being reset.
2237 *
2238 * For the HM component this means that any GDT/LDT/TSS monitors
2239 * needs to be removed.
2240 *
2241 * @param pVM The cross context VM structure.
2242 */
2243VMMR3_INT_DECL(void) HMR3Reset(PVM pVM)
2244{
2245 LogFlow(("HMR3Reset:\n"));
2246
2247 if (HMIsEnabled(pVM))
2248 hmR3DisableRawMode(pVM);
2249
2250 for (VMCPUID i = 0; i < pVM->cCpus; i++)
2251 {
2252 PVMCPU pVCpu = &pVM->aCpus[i];
2253
2254 HMR3ResetCpu(pVCpu);
2255 }
2256
2257 /* Clear all patch information. */
2258 pVM->hm.s.pGuestPatchMem = 0;
2259 pVM->hm.s.pFreeGuestPatchMem = 0;
2260 pVM->hm.s.cbGuestPatchMem = 0;
2261 pVM->hm.s.cPatches = 0;
2262 pVM->hm.s.PatchTree = 0;
2263 pVM->hm.s.fTPRPatchingActive = false;
2264 ASMMemZero32(pVM->hm.s.aPatches, sizeof(pVM->hm.s.aPatches));
2265}
2266
2267
2268/**
2269 * Callback to patch a TPR instruction (vmmcall or mov cr8).
2270 *
2271 * @returns VBox strict status code.
2272 * @param pVM The cross context VM structure.
2273 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2274 * @param pvUser Unused.
2275 */
2276static DECLCALLBACK(VBOXSTRICTRC) hmR3RemovePatches(PVM pVM, PVMCPU pVCpu, void *pvUser)
2277{
2278 VMCPUID idCpu = (VMCPUID)(uintptr_t)pvUser;
2279
2280 /* Only execute the handler on the VCPU the original patch request was issued. */
2281 if (pVCpu->idCpu != idCpu)
2282 return VINF_SUCCESS;
2283
2284 Log(("hmR3RemovePatches\n"));
2285 for (unsigned i = 0; i < pVM->hm.s.cPatches; i++)
2286 {
2287 uint8_t abInstr[15];
2288 PHMTPRPATCH pPatch = &pVM->hm.s.aPatches[i];
2289 RTGCPTR pInstrGC = (RTGCPTR)pPatch->Core.Key;
2290 int rc;
2291
2292#ifdef LOG_ENABLED
2293 char szOutput[256];
2294 rc = DBGFR3DisasInstrEx(pVM->pUVM, pVCpu->idCpu, CPUMGetGuestCS(pVCpu), pInstrGC, DBGF_DISAS_FLAGS_DEFAULT_MODE,
2295 szOutput, sizeof(szOutput), NULL);
2296 if (RT_SUCCESS(rc))
2297 Log(("Patched instr: %s\n", szOutput));
2298#endif
2299
2300 /* Check if the instruction is still the same. */
2301 rc = PGMPhysSimpleReadGCPtr(pVCpu, abInstr, pInstrGC, pPatch->cbNewOp);
2302 if (rc != VINF_SUCCESS)
2303 {
2304 Log(("Patched code removed? (rc=%Rrc0\n", rc));
2305 continue; /* swapped out or otherwise removed; skip it. */
2306 }
2307
2308 if (memcmp(abInstr, pPatch->aNewOpcode, pPatch->cbNewOp))
2309 {
2310 Log(("Patched instruction was changed! (rc=%Rrc0\n", rc));
2311 continue; /* skip it. */
2312 }
2313
2314 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pInstrGC, pPatch->aOpcode, pPatch->cbOp);
2315 AssertRC(rc);
2316
2317#ifdef LOG_ENABLED
2318 rc = DBGFR3DisasInstrEx(pVM->pUVM, pVCpu->idCpu, CPUMGetGuestCS(pVCpu), pInstrGC, DBGF_DISAS_FLAGS_DEFAULT_MODE,
2319 szOutput, sizeof(szOutput), NULL);
2320 if (RT_SUCCESS(rc))
2321 Log(("Original instr: %s\n", szOutput));
2322#endif
2323 }
2324 pVM->hm.s.cPatches = 0;
2325 pVM->hm.s.PatchTree = 0;
2326 pVM->hm.s.pFreeGuestPatchMem = pVM->hm.s.pGuestPatchMem;
2327 pVM->hm.s.fTPRPatchingActive = false;
2328 return VINF_SUCCESS;
2329}
2330
2331
2332/**
2333 * Worker for enabling patching in a VT-x/AMD-V guest.
2334 *
2335 * @returns VBox status code.
2336 * @param pVM The cross context VM structure.
2337 * @param idCpu VCPU to execute hmR3RemovePatches on.
2338 * @param pPatchMem Patch memory range.
2339 * @param cbPatchMem Size of the memory range.
2340 */
2341static int hmR3EnablePatching(PVM pVM, VMCPUID idCpu, RTRCPTR pPatchMem, unsigned cbPatchMem)
2342{
2343 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE, hmR3RemovePatches, (void *)(uintptr_t)idCpu);
2344 AssertRC(rc);
2345
2346 pVM->hm.s.pGuestPatchMem = pPatchMem;
2347 pVM->hm.s.pFreeGuestPatchMem = pPatchMem;
2348 pVM->hm.s.cbGuestPatchMem = cbPatchMem;
2349 return VINF_SUCCESS;
2350}
2351
2352
2353/**
2354 * Enable patching in a VT-x/AMD-V guest
2355 *
2356 * @returns VBox status code.
2357 * @param pVM The cross context VM structure.
2358 * @param pPatchMem Patch memory range.
2359 * @param cbPatchMem Size of the memory range.
2360 */
2361VMMR3_INT_DECL(int) HMR3EnablePatching(PVM pVM, RTGCPTR pPatchMem, unsigned cbPatchMem)
2362{
2363 VM_ASSERT_EMT(pVM);
2364 Log(("HMR3EnablePatching %RGv size %x\n", pPatchMem, cbPatchMem));
2365 if (pVM->cCpus > 1)
2366 {
2367 /* We own the IOM lock here and could cause a deadlock by waiting for a VCPU that is blocking on the IOM lock. */
2368 int rc = VMR3ReqCallNoWait(pVM, VMCPUID_ANY_QUEUE,
2369 (PFNRT)hmR3EnablePatching, 4, pVM, VMMGetCpuId(pVM), (RTRCPTR)pPatchMem, cbPatchMem);
2370 AssertRC(rc);
2371 return rc;
2372 }
2373 return hmR3EnablePatching(pVM, VMMGetCpuId(pVM), (RTRCPTR)pPatchMem, cbPatchMem);
2374}
2375
2376
2377/**
2378 * Disable patching in a VT-x/AMD-V guest.
2379 *
2380 * @returns VBox status code.
2381 * @param pVM The cross context VM structure.
2382 * @param pPatchMem Patch memory range.
2383 * @param cbPatchMem Size of the memory range.
2384 */
2385VMMR3_INT_DECL(int) HMR3DisablePatching(PVM pVM, RTGCPTR pPatchMem, unsigned cbPatchMem)
2386{
2387 Log(("HMR3DisablePatching %RGv size %x\n", pPatchMem, cbPatchMem));
2388 RT_NOREF2(pPatchMem, cbPatchMem);
2389
2390 Assert(pVM->hm.s.pGuestPatchMem == pPatchMem);
2391 Assert(pVM->hm.s.cbGuestPatchMem == cbPatchMem);
2392
2393 /** @todo Potential deadlock when other VCPUs are waiting on the IOM lock (we own it)!! */
2394 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE, hmR3RemovePatches,
2395 (void *)(uintptr_t)VMMGetCpuId(pVM));
2396 AssertRC(rc);
2397
2398 pVM->hm.s.pGuestPatchMem = 0;
2399 pVM->hm.s.pFreeGuestPatchMem = 0;
2400 pVM->hm.s.cbGuestPatchMem = 0;
2401 pVM->hm.s.fTPRPatchingActive = false;
2402 return VINF_SUCCESS;
2403}
2404
2405
2406/**
2407 * Callback to patch a TPR instruction (vmmcall or mov cr8).
2408 *
2409 * @returns VBox strict status code.
2410 * @param pVM The cross context VM structure.
2411 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2412 * @param pvUser User specified CPU context.
2413 *
2414 */
2415static DECLCALLBACK(VBOXSTRICTRC) hmR3ReplaceTprInstr(PVM pVM, PVMCPU pVCpu, void *pvUser)
2416{
2417 /*
2418 * Only execute the handler on the VCPU the original patch request was
2419 * issued. (The other CPU(s) might not yet have switched to protected
2420 * mode, nor have the correct memory context.)
2421 */
2422 VMCPUID idCpu = (VMCPUID)(uintptr_t)pvUser;
2423 if (pVCpu->idCpu != idCpu)
2424 return VINF_SUCCESS;
2425
2426 /*
2427 * We're racing other VCPUs here, so don't try patch the instruction twice
2428 * and make sure there is still room for our patch record.
2429 */
2430 PCPUMCTX pCtx = &pVCpu->cpum.GstCtx;
2431 PHMTPRPATCH pPatch = (PHMTPRPATCH)RTAvloU32Get(&pVM->hm.s.PatchTree, (AVLOU32KEY)pCtx->eip);
2432 if (pPatch)
2433 {
2434 Log(("hmR3ReplaceTprInstr: already patched %RGv\n", pCtx->rip));
2435 return VINF_SUCCESS;
2436 }
2437 uint32_t const idx = pVM->hm.s.cPatches;
2438 if (idx >= RT_ELEMENTS(pVM->hm.s.aPatches))
2439 {
2440 Log(("hmR3ReplaceTprInstr: no available patch slots (%RGv)\n", pCtx->rip));
2441 return VINF_SUCCESS;
2442 }
2443 pPatch = &pVM->hm.s.aPatches[idx];
2444
2445 Log(("hmR3ReplaceTprInstr: rip=%RGv idxPatch=%u\n", pCtx->rip, idx));
2446
2447 /*
2448 * Disassembler the instruction and get cracking.
2449 */
2450 DBGFR3_DISAS_INSTR_CUR_LOG(pVCpu, "hmR3ReplaceTprInstr");
2451 PDISCPUSTATE pDis = &pVCpu->hm.s.DisState;
2452 uint32_t cbOp;
2453 int rc = EMInterpretDisasCurrent(pVM, pVCpu, pDis, &cbOp);
2454 AssertRC(rc);
2455 if ( rc == VINF_SUCCESS
2456 && pDis->pCurInstr->uOpcode == OP_MOV
2457 && cbOp >= 3)
2458 {
2459 static uint8_t const s_abVMMCall[3] = { 0x0f, 0x01, 0xd9 };
2460
2461 rc = PGMPhysSimpleReadGCPtr(pVCpu, pPatch->aOpcode, pCtx->rip, cbOp);
2462 AssertRC(rc);
2463
2464 pPatch->cbOp = cbOp;
2465
2466 if (pDis->Param1.fUse == DISUSE_DISPLACEMENT32)
2467 {
2468 /* write. */
2469 if (pDis->Param2.fUse == DISUSE_REG_GEN32)
2470 {
2471 pPatch->enmType = HMTPRINSTR_WRITE_REG;
2472 pPatch->uSrcOperand = pDis->Param2.Base.idxGenReg;
2473 Log(("hmR3ReplaceTprInstr: HMTPRINSTR_WRITE_REG %u\n", pDis->Param2.Base.idxGenReg));
2474 }
2475 else
2476 {
2477 Assert(pDis->Param2.fUse == DISUSE_IMMEDIATE32);
2478 pPatch->enmType = HMTPRINSTR_WRITE_IMM;
2479 pPatch->uSrcOperand = pDis->Param2.uValue;
2480 Log(("hmR3ReplaceTprInstr: HMTPRINSTR_WRITE_IMM %#llx\n", pDis->Param2.uValue));
2481 }
2482 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pCtx->rip, s_abVMMCall, sizeof(s_abVMMCall));
2483 AssertRC(rc);
2484
2485 memcpy(pPatch->aNewOpcode, s_abVMMCall, sizeof(s_abVMMCall));
2486 pPatch->cbNewOp = sizeof(s_abVMMCall);
2487 STAM_COUNTER_INC(&pVM->hm.s.StatTprReplaceSuccessVmc);
2488 }
2489 else
2490 {
2491 /*
2492 * TPR Read.
2493 *
2494 * Found:
2495 * mov eax, dword [fffe0080] (5 bytes)
2496 * Check if next instruction is:
2497 * shr eax, 4
2498 */
2499 Assert(pDis->Param1.fUse == DISUSE_REG_GEN32);
2500
2501 uint8_t const idxMmioReg = pDis->Param1.Base.idxGenReg;
2502 uint8_t const cbOpMmio = cbOp;
2503 uint64_t const uSavedRip = pCtx->rip;
2504
2505 pCtx->rip += cbOp;
2506 rc = EMInterpretDisasCurrent(pVM, pVCpu, pDis, &cbOp);
2507 DBGFR3_DISAS_INSTR_CUR_LOG(pVCpu, "Following read");
2508 pCtx->rip = uSavedRip;
2509
2510 if ( rc == VINF_SUCCESS
2511 && pDis->pCurInstr->uOpcode == OP_SHR
2512 && pDis->Param1.fUse == DISUSE_REG_GEN32
2513 && pDis->Param1.Base.idxGenReg == idxMmioReg
2514 && pDis->Param2.fUse == DISUSE_IMMEDIATE8
2515 && pDis->Param2.uValue == 4
2516 && cbOpMmio + cbOp < sizeof(pVM->hm.s.aPatches[idx].aOpcode))
2517 {
2518 uint8_t abInstr[15];
2519
2520 /* Replacing the two instructions above with an AMD-V specific lock-prefixed 32-bit MOV CR8 instruction so as to
2521 access CR8 in 32-bit mode and not cause a #VMEXIT. */
2522 rc = PGMPhysSimpleReadGCPtr(pVCpu, &pPatch->aOpcode, pCtx->rip, cbOpMmio + cbOp);
2523 AssertRC(rc);
2524
2525 pPatch->cbOp = cbOpMmio + cbOp;
2526
2527 /* 0xf0, 0x0f, 0x20, 0xc0 = mov eax, cr8 */
2528 abInstr[0] = 0xf0;
2529 abInstr[1] = 0x0f;
2530 abInstr[2] = 0x20;
2531 abInstr[3] = 0xc0 | pDis->Param1.Base.idxGenReg;
2532 for (unsigned i = 4; i < pPatch->cbOp; i++)
2533 abInstr[i] = 0x90; /* nop */
2534
2535 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pCtx->rip, abInstr, pPatch->cbOp);
2536 AssertRC(rc);
2537
2538 memcpy(pPatch->aNewOpcode, abInstr, pPatch->cbOp);
2539 pPatch->cbNewOp = pPatch->cbOp;
2540 STAM_COUNTER_INC(&pVM->hm.s.StatTprReplaceSuccessCr8);
2541
2542 Log(("Acceptable read/shr candidate!\n"));
2543 pPatch->enmType = HMTPRINSTR_READ_SHR4;
2544 }
2545 else
2546 {
2547 pPatch->enmType = HMTPRINSTR_READ;
2548 pPatch->uDstOperand = idxMmioReg;
2549
2550 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pCtx->rip, s_abVMMCall, sizeof(s_abVMMCall));
2551 AssertRC(rc);
2552
2553 memcpy(pPatch->aNewOpcode, s_abVMMCall, sizeof(s_abVMMCall));
2554 pPatch->cbNewOp = sizeof(s_abVMMCall);
2555 STAM_COUNTER_INC(&pVM->hm.s.StatTprReplaceSuccessVmc);
2556 Log(("hmR3ReplaceTprInstr: HMTPRINSTR_READ %u\n", pPatch->uDstOperand));
2557 }
2558 }
2559
2560 pPatch->Core.Key = pCtx->eip;
2561 rc = RTAvloU32Insert(&pVM->hm.s.PatchTree, &pPatch->Core);
2562 AssertRC(rc);
2563
2564 pVM->hm.s.cPatches++;
2565 return VINF_SUCCESS;
2566 }
2567
2568 /*
2569 * Save invalid patch, so we will not try again.
2570 */
2571 Log(("hmR3ReplaceTprInstr: Failed to patch instr!\n"));
2572 pPatch->Core.Key = pCtx->eip;
2573 pPatch->enmType = HMTPRINSTR_INVALID;
2574 rc = RTAvloU32Insert(&pVM->hm.s.PatchTree, &pPatch->Core);
2575 AssertRC(rc);
2576 pVM->hm.s.cPatches++;
2577 STAM_COUNTER_INC(&pVM->hm.s.StatTprReplaceFailure);
2578 return VINF_SUCCESS;
2579}
2580
2581
2582/**
2583 * Callback to patch a TPR instruction (jump to generated code).
2584 *
2585 * @returns VBox strict status code.
2586 * @param pVM The cross context VM structure.
2587 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2588 * @param pvUser User specified CPU context.
2589 *
2590 */
2591static DECLCALLBACK(VBOXSTRICTRC) hmR3PatchTprInstr(PVM pVM, PVMCPU pVCpu, void *pvUser)
2592{
2593 /*
2594 * Only execute the handler on the VCPU the original patch request was
2595 * issued. (The other CPU(s) might not yet have switched to protected
2596 * mode, nor have the correct memory context.)
2597 */
2598 VMCPUID idCpu = (VMCPUID)(uintptr_t)pvUser;
2599 if (pVCpu->idCpu != idCpu)
2600 return VINF_SUCCESS;
2601
2602 /*
2603 * We're racing other VCPUs here, so don't try patch the instruction twice
2604 * and make sure there is still room for our patch record.
2605 */
2606 PCPUMCTX pCtx = &pVCpu->cpum.GstCtx;
2607 PHMTPRPATCH pPatch = (PHMTPRPATCH)RTAvloU32Get(&pVM->hm.s.PatchTree, (AVLOU32KEY)pCtx->eip);
2608 if (pPatch)
2609 {
2610 Log(("hmR3PatchTprInstr: already patched %RGv\n", pCtx->rip));
2611 return VINF_SUCCESS;
2612 }
2613 uint32_t const idx = pVM->hm.s.cPatches;
2614 if (idx >= RT_ELEMENTS(pVM->hm.s.aPatches))
2615 {
2616 Log(("hmR3PatchTprInstr: no available patch slots (%RGv)\n", pCtx->rip));
2617 return VINF_SUCCESS;
2618 }
2619 pPatch = &pVM->hm.s.aPatches[idx];
2620
2621 Log(("hmR3PatchTprInstr: rip=%RGv idxPatch=%u\n", pCtx->rip, idx));
2622 DBGFR3_DISAS_INSTR_CUR_LOG(pVCpu, "hmR3PatchTprInstr");
2623
2624 /*
2625 * Disassemble the instruction and get cracking.
2626 */
2627 PDISCPUSTATE pDis = &pVCpu->hm.s.DisState;
2628 uint32_t cbOp;
2629 int rc = EMInterpretDisasCurrent(pVM, pVCpu, pDis, &cbOp);
2630 AssertRC(rc);
2631 if ( rc == VINF_SUCCESS
2632 && pDis->pCurInstr->uOpcode == OP_MOV
2633 && cbOp >= 5)
2634 {
2635 uint8_t aPatch[64];
2636 uint32_t off = 0;
2637
2638 rc = PGMPhysSimpleReadGCPtr(pVCpu, pPatch->aOpcode, pCtx->rip, cbOp);
2639 AssertRC(rc);
2640
2641 pPatch->cbOp = cbOp;
2642 pPatch->enmType = HMTPRINSTR_JUMP_REPLACEMENT;
2643
2644 if (pDis->Param1.fUse == DISUSE_DISPLACEMENT32)
2645 {
2646 /*
2647 * TPR write:
2648 *
2649 * push ECX [51]
2650 * push EDX [52]
2651 * push EAX [50]
2652 * xor EDX,EDX [31 D2]
2653 * mov EAX,EAX [89 C0]
2654 * or
2655 * mov EAX,0000000CCh [B8 CC 00 00 00]
2656 * mov ECX,0C0000082h [B9 82 00 00 C0]
2657 * wrmsr [0F 30]
2658 * pop EAX [58]
2659 * pop EDX [5A]
2660 * pop ECX [59]
2661 * jmp return_address [E9 return_address]
2662 */
2663 bool fUsesEax = (pDis->Param2.fUse == DISUSE_REG_GEN32 && pDis->Param2.Base.idxGenReg == DISGREG_EAX);
2664
2665 aPatch[off++] = 0x51; /* push ecx */
2666 aPatch[off++] = 0x52; /* push edx */
2667 if (!fUsesEax)
2668 aPatch[off++] = 0x50; /* push eax */
2669 aPatch[off++] = 0x31; /* xor edx, edx */
2670 aPatch[off++] = 0xd2;
2671 if (pDis->Param2.fUse == DISUSE_REG_GEN32)
2672 {
2673 if (!fUsesEax)
2674 {
2675 aPatch[off++] = 0x89; /* mov eax, src_reg */
2676 aPatch[off++] = MAKE_MODRM(3, pDis->Param2.Base.idxGenReg, DISGREG_EAX);
2677 }
2678 }
2679 else
2680 {
2681 Assert(pDis->Param2.fUse == DISUSE_IMMEDIATE32);
2682 aPatch[off++] = 0xb8; /* mov eax, immediate */
2683 *(uint32_t *)&aPatch[off] = pDis->Param2.uValue;
2684 off += sizeof(uint32_t);
2685 }
2686 aPatch[off++] = 0xb9; /* mov ecx, 0xc0000082 */
2687 *(uint32_t *)&aPatch[off] = MSR_K8_LSTAR;
2688 off += sizeof(uint32_t);
2689
2690 aPatch[off++] = 0x0f; /* wrmsr */
2691 aPatch[off++] = 0x30;
2692 if (!fUsesEax)
2693 aPatch[off++] = 0x58; /* pop eax */
2694 aPatch[off++] = 0x5a; /* pop edx */
2695 aPatch[off++] = 0x59; /* pop ecx */
2696 }
2697 else
2698 {
2699 /*
2700 * TPR read:
2701 *
2702 * push ECX [51]
2703 * push EDX [52]
2704 * push EAX [50]
2705 * mov ECX,0C0000082h [B9 82 00 00 C0]
2706 * rdmsr [0F 32]
2707 * mov EAX,EAX [89 C0]
2708 * pop EAX [58]
2709 * pop EDX [5A]
2710 * pop ECX [59]
2711 * jmp return_address [E9 return_address]
2712 */
2713 Assert(pDis->Param1.fUse == DISUSE_REG_GEN32);
2714
2715 if (pDis->Param1.Base.idxGenReg != DISGREG_ECX)
2716 aPatch[off++] = 0x51; /* push ecx */
2717 if (pDis->Param1.Base.idxGenReg != DISGREG_EDX )
2718 aPatch[off++] = 0x52; /* push edx */
2719 if (pDis->Param1.Base.idxGenReg != DISGREG_EAX)
2720 aPatch[off++] = 0x50; /* push eax */
2721
2722 aPatch[off++] = 0x31; /* xor edx, edx */
2723 aPatch[off++] = 0xd2;
2724
2725 aPatch[off++] = 0xb9; /* mov ecx, 0xc0000082 */
2726 *(uint32_t *)&aPatch[off] = MSR_K8_LSTAR;
2727 off += sizeof(uint32_t);
2728
2729 aPatch[off++] = 0x0f; /* rdmsr */
2730 aPatch[off++] = 0x32;
2731
2732 if (pDis->Param1.Base.idxGenReg != DISGREG_EAX)
2733 {
2734 aPatch[off++] = 0x89; /* mov dst_reg, eax */
2735 aPatch[off++] = MAKE_MODRM(3, DISGREG_EAX, pDis->Param1.Base.idxGenReg);
2736 }
2737
2738 if (pDis->Param1.Base.idxGenReg != DISGREG_EAX)
2739 aPatch[off++] = 0x58; /* pop eax */
2740 if (pDis->Param1.Base.idxGenReg != DISGREG_EDX )
2741 aPatch[off++] = 0x5a; /* pop edx */
2742 if (pDis->Param1.Base.idxGenReg != DISGREG_ECX)
2743 aPatch[off++] = 0x59; /* pop ecx */
2744 }
2745 aPatch[off++] = 0xe9; /* jmp return_address */
2746 *(RTRCUINTPTR *)&aPatch[off] = ((RTRCUINTPTR)pCtx->eip + cbOp) - ((RTRCUINTPTR)pVM->hm.s.pFreeGuestPatchMem + off + 4);
2747 off += sizeof(RTRCUINTPTR);
2748
2749 if (pVM->hm.s.pFreeGuestPatchMem + off <= pVM->hm.s.pGuestPatchMem + pVM->hm.s.cbGuestPatchMem)
2750 {
2751 /* Write new code to the patch buffer. */
2752 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pVM->hm.s.pFreeGuestPatchMem, aPatch, off);
2753 AssertRC(rc);
2754
2755#ifdef LOG_ENABLED
2756 uint32_t cbCurInstr;
2757 for (RTGCPTR GCPtrInstr = pVM->hm.s.pFreeGuestPatchMem;
2758 GCPtrInstr < pVM->hm.s.pFreeGuestPatchMem + off;
2759 GCPtrInstr += RT_MAX(cbCurInstr, 1))
2760 {
2761 char szOutput[256];
2762 rc = DBGFR3DisasInstrEx(pVM->pUVM, pVCpu->idCpu, pCtx->cs.Sel, GCPtrInstr, DBGF_DISAS_FLAGS_DEFAULT_MODE,
2763 szOutput, sizeof(szOutput), &cbCurInstr);
2764 if (RT_SUCCESS(rc))
2765 Log(("Patch instr %s\n", szOutput));
2766 else
2767 Log(("%RGv: rc=%Rrc\n", GCPtrInstr, rc));
2768 }
2769#endif
2770
2771 pPatch->aNewOpcode[0] = 0xE9;
2772 *(RTRCUINTPTR *)&pPatch->aNewOpcode[1] = ((RTRCUINTPTR)pVM->hm.s.pFreeGuestPatchMem) - ((RTRCUINTPTR)pCtx->eip + 5);
2773
2774 /* Overwrite the TPR instruction with a jump. */
2775 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pCtx->eip, pPatch->aNewOpcode, 5);
2776 AssertRC(rc);
2777
2778 DBGFR3_DISAS_INSTR_CUR_LOG(pVCpu, "Jump");
2779
2780 pVM->hm.s.pFreeGuestPatchMem += off;
2781 pPatch->cbNewOp = 5;
2782
2783 pPatch->Core.Key = pCtx->eip;
2784 rc = RTAvloU32Insert(&pVM->hm.s.PatchTree, &pPatch->Core);
2785 AssertRC(rc);
2786
2787 pVM->hm.s.cPatches++;
2788 pVM->hm.s.fTPRPatchingActive = true;
2789 STAM_COUNTER_INC(&pVM->hm.s.StatTprPatchSuccess);
2790 return VINF_SUCCESS;
2791 }
2792
2793 Log(("Ran out of space in our patch buffer!\n"));
2794 }
2795 else
2796 Log(("hmR3PatchTprInstr: Failed to patch instr!\n"));
2797
2798
2799 /*
2800 * Save invalid patch, so we will not try again.
2801 */
2802 pPatch = &pVM->hm.s.aPatches[idx];
2803 pPatch->Core.Key = pCtx->eip;
2804 pPatch->enmType = HMTPRINSTR_INVALID;
2805 rc = RTAvloU32Insert(&pVM->hm.s.PatchTree, &pPatch->Core);
2806 AssertRC(rc);
2807 pVM->hm.s.cPatches++;
2808 STAM_COUNTER_INC(&pVM->hm.s.StatTprPatchFailure);
2809 return VINF_SUCCESS;
2810}
2811
2812
2813/**
2814 * Attempt to patch TPR mmio instructions.
2815 *
2816 * @returns VBox status code.
2817 * @param pVM The cross context VM structure.
2818 * @param pVCpu The cross context virtual CPU structure.
2819 */
2820VMMR3_INT_DECL(int) HMR3PatchTprInstr(PVM pVM, PVMCPU pVCpu)
2821{
2822 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE,
2823 pVM->hm.s.pGuestPatchMem ? hmR3PatchTprInstr : hmR3ReplaceTprInstr,
2824 (void *)(uintptr_t)pVCpu->idCpu);
2825 AssertRC(rc);
2826 return rc;
2827}
2828
2829
2830/**
2831 * Checks if we need to reschedule due to VMM device heap changes.
2832 *
2833 * @returns true if a reschedule is required, otherwise false.
2834 * @param pVM The cross context VM structure.
2835 * @param pCtx VM execution context.
2836 */
2837VMMR3_INT_DECL(bool) HMR3IsRescheduleRequired(PVM pVM, PCPUMCTX pCtx)
2838{
2839 /*
2840 * The VMM device heap is a requirement for emulating real-mode or protected-mode without paging
2841 * when the unrestricted guest execution feature is missing (VT-x only).
2842 */
2843 if ( pVM->hm.s.vmx.fEnabled
2844 && !pVM->hm.s.vmx.fUnrestrictedGuest
2845 && CPUMIsGuestInRealModeEx(pCtx)
2846 && !PDMVmmDevHeapIsEnabled(pVM))
2847 return true;
2848
2849 return false;
2850}
2851
2852
2853/**
2854 * Noticiation callback from DBGF when interrupt breakpoints or generic debug
2855 * event settings changes.
2856 *
2857 * DBGF will call HMR3NotifyDebugEventChangedPerCpu on each CPU afterwards, this
2858 * function is just updating the VM globals.
2859 *
2860 * @param pVM The VM cross context VM structure.
2861 * @thread EMT(0)
2862 */
2863VMMR3_INT_DECL(void) HMR3NotifyDebugEventChanged(PVM pVM)
2864{
2865 /* Interrupts. */
2866 bool fUseDebugLoop = pVM->dbgf.ro.cSoftIntBreakpoints > 0
2867 || pVM->dbgf.ro.cHardIntBreakpoints > 0;
2868
2869 /* CPU Exceptions. */
2870 for (DBGFEVENTTYPE enmEvent = DBGFEVENT_XCPT_FIRST;
2871 !fUseDebugLoop && enmEvent <= DBGFEVENT_XCPT_LAST;
2872 enmEvent = (DBGFEVENTTYPE)(enmEvent + 1))
2873 fUseDebugLoop = DBGF_IS_EVENT_ENABLED(pVM, enmEvent);
2874
2875 /* Common VM exits. */
2876 for (DBGFEVENTTYPE enmEvent = DBGFEVENT_EXIT_FIRST;
2877 !fUseDebugLoop && enmEvent <= DBGFEVENT_EXIT_LAST_COMMON;
2878 enmEvent = (DBGFEVENTTYPE)(enmEvent + 1))
2879 fUseDebugLoop = DBGF_IS_EVENT_ENABLED(pVM, enmEvent);
2880
2881 /* Vendor specific VM exits. */
2882 if (HMR3IsVmxEnabled(pVM->pUVM))
2883 for (DBGFEVENTTYPE enmEvent = DBGFEVENT_EXIT_VMX_FIRST;
2884 !fUseDebugLoop && enmEvent <= DBGFEVENT_EXIT_VMX_LAST;
2885 enmEvent = (DBGFEVENTTYPE)(enmEvent + 1))
2886 fUseDebugLoop = DBGF_IS_EVENT_ENABLED(pVM, enmEvent);
2887 else
2888 for (DBGFEVENTTYPE enmEvent = DBGFEVENT_EXIT_SVM_FIRST;
2889 !fUseDebugLoop && enmEvent <= DBGFEVENT_EXIT_SVM_LAST;
2890 enmEvent = (DBGFEVENTTYPE)(enmEvent + 1))
2891 fUseDebugLoop = DBGF_IS_EVENT_ENABLED(pVM, enmEvent);
2892
2893 /* Done. */
2894 pVM->hm.s.fUseDebugLoop = fUseDebugLoop;
2895}
2896
2897
2898/**
2899 * Follow up notification callback to HMR3NotifyDebugEventChanged for each CPU.
2900 *
2901 * HM uses this to combine the decision made by HMR3NotifyDebugEventChanged with
2902 * per CPU settings.
2903 *
2904 * @param pVM The VM cross context VM structure.
2905 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2906 */
2907VMMR3_INT_DECL(void) HMR3NotifyDebugEventChangedPerCpu(PVM pVM, PVMCPU pVCpu)
2908{
2909 pVCpu->hm.s.fUseDebugLoop = pVCpu->hm.s.fSingleInstruction | pVM->hm.s.fUseDebugLoop;
2910}
2911
2912
2913/**
2914 * Checks if we are currently using hardware acceleration.
2915 *
2916 * @returns true if hardware acceleration is being used, otherwise false.
2917 * @param pVCpu The cross context virtual CPU structure.
2918 */
2919VMMR3_INT_DECL(bool) HMR3IsActive(PVMCPU pVCpu)
2920{
2921 return pVCpu->hm.s.fActive;
2922}
2923
2924
2925/**
2926 * External interface for querying whether hardware acceleration is enabled.
2927 *
2928 * @returns true if VT-x or AMD-V is being used, otherwise false.
2929 * @param pUVM The user mode VM handle.
2930 * @sa HMIsEnabled, HMIsEnabledNotMacro.
2931 */
2932VMMR3DECL(bool) HMR3IsEnabled(PUVM pUVM)
2933{
2934 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
2935 PVM pVM = pUVM->pVM;
2936 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
2937 return pVM->fHMEnabled; /* Don't use the macro as the GUI may query us very very early. */
2938}
2939
2940
2941/**
2942 * External interface for querying whether VT-x is being used.
2943 *
2944 * @returns true if VT-x is being used, otherwise false.
2945 * @param pUVM The user mode VM handle.
2946 * @sa HMR3IsSvmEnabled, HMIsEnabled
2947 */
2948VMMR3DECL(bool) HMR3IsVmxEnabled(PUVM pUVM)
2949{
2950 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
2951 PVM pVM = pUVM->pVM;
2952 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
2953 return pVM->hm.s.vmx.fEnabled
2954 && pVM->hm.s.vmx.fSupported
2955 && pVM->fHMEnabled;
2956}
2957
2958
2959/**
2960 * External interface for querying whether AMD-V is being used.
2961 *
2962 * @returns true if VT-x is being used, otherwise false.
2963 * @param pUVM The user mode VM handle.
2964 * @sa HMR3IsVmxEnabled, HMIsEnabled
2965 */
2966VMMR3DECL(bool) HMR3IsSvmEnabled(PUVM pUVM)
2967{
2968 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
2969 PVM pVM = pUVM->pVM;
2970 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
2971 return pVM->hm.s.svm.fEnabled
2972 && pVM->hm.s.svm.fSupported
2973 && pVM->fHMEnabled;
2974}
2975
2976
2977/**
2978 * Checks if we are currently using nested paging.
2979 *
2980 * @returns true if nested paging is being used, otherwise false.
2981 * @param pUVM The user mode VM handle.
2982 */
2983VMMR3DECL(bool) HMR3IsNestedPagingActive(PUVM pUVM)
2984{
2985 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
2986 PVM pVM = pUVM->pVM;
2987 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
2988 return pVM->hm.s.fNestedPaging;
2989}
2990
2991
2992/**
2993 * Checks if virtualized APIC registers is enabled.
2994 *
2995 * When enabled this feature allows the hardware to access most of the
2996 * APIC registers in the virtual-APIC page without causing VM-exits. See
2997 * Intel spec. 29.1.1 "Virtualized APIC Registers".
2998 *
2999 * @returns true if virtualized APIC registers is enabled, otherwise
3000 * false.
3001 * @param pUVM The user mode VM handle.
3002 */
3003VMMR3DECL(bool) HMR3IsVirtApicRegsEnabled(PUVM pUVM)
3004{
3005 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
3006 PVM pVM = pUVM->pVM;
3007 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
3008 return pVM->hm.s.fVirtApicRegs;
3009}
3010
3011
3012/**
3013 * Checks if APIC posted-interrupt processing is enabled.
3014 *
3015 * This returns whether we can deliver interrupts to the guest without
3016 * leaving guest-context by updating APIC state from host-context.
3017 *
3018 * @returns true if APIC posted-interrupt processing is enabled,
3019 * otherwise false.
3020 * @param pUVM The user mode VM handle.
3021 */
3022VMMR3DECL(bool) HMR3IsPostedIntrsEnabled(PUVM pUVM)
3023{
3024 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
3025 PVM pVM = pUVM->pVM;
3026 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
3027 return pVM->hm.s.fPostedIntrs;
3028}
3029
3030
3031/**
3032 * Checks if we are currently using VPID in VT-x mode.
3033 *
3034 * @returns true if VPID is being used, otherwise false.
3035 * @param pUVM The user mode VM handle.
3036 */
3037VMMR3DECL(bool) HMR3IsVpidActive(PUVM pUVM)
3038{
3039 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
3040 PVM pVM = pUVM->pVM;
3041 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
3042 return pVM->hm.s.vmx.fVpid;
3043}
3044
3045
3046/**
3047 * Checks if we are currently using VT-x unrestricted execution,
3048 * aka UX.
3049 *
3050 * @returns true if UX is being used, otherwise false.
3051 * @param pUVM The user mode VM handle.
3052 */
3053VMMR3DECL(bool) HMR3IsUXActive(PUVM pUVM)
3054{
3055 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
3056 PVM pVM = pUVM->pVM;
3057 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
3058 return pVM->hm.s.vmx.fUnrestrictedGuest
3059 || pVM->hm.s.svm.fSupported;
3060}
3061
3062
3063/**
3064 * Checks if internal events are pending. In that case we are not allowed to dispatch interrupts.
3065 *
3066 * @returns true if an internal event is pending, otherwise false.
3067 * @param pVCpu The cross context virtual CPU structure.
3068 */
3069VMMR3_INT_DECL(bool) HMR3IsEventPending(PVMCPU pVCpu)
3070{
3071 return HMIsEnabled(pVCpu->pVMR3)
3072 && pVCpu->hm.s.Event.fPending;
3073}
3074
3075
3076/**
3077 * Checks if the VMX-preemption timer is being used.
3078 *
3079 * @returns true if the VMX-preemption timer is being used, otherwise false.
3080 * @param pVM The cross context VM structure.
3081 */
3082VMMR3_INT_DECL(bool) HMR3IsVmxPreemptionTimerUsed(PVM pVM)
3083{
3084 return HMIsEnabled(pVM)
3085 && pVM->hm.s.vmx.fEnabled
3086 && pVM->hm.s.vmx.fUsePreemptTimer;
3087}
3088
3089
3090/**
3091 * Check fatal VT-x/AMD-V error and produce some meaningful
3092 * log release message.
3093 *
3094 * @param pVM The cross context VM structure.
3095 * @param iStatusCode VBox status code.
3096 */
3097VMMR3_INT_DECL(void) HMR3CheckError(PVM pVM, int iStatusCode)
3098{
3099 for (VMCPUID i = 0; i < pVM->cCpus; i++)
3100 {
3101 PVMCPU pVCpu = &pVM->aCpus[i];
3102 switch (iStatusCode)
3103 {
3104 /** @todo r=ramshankar: Are all EMTs out of ring-0 at this point!? If not, we
3105 * might be getting inaccurate values for non-guru'ing EMTs. */
3106 case VERR_VMX_INVALID_VMCS_FIELD:
3107 break;
3108
3109 case VERR_VMX_INVALID_VMCS_PTR:
3110 LogRel(("HM: VERR_VMX_INVALID_VMCS_PTR:\n"));
3111 LogRel(("HM: CPU[%u] Current pointer %#RGp vs %#RGp\n", i, pVCpu->hm.s.vmx.LastError.u64VmcsPhys,
3112 pVCpu->hm.s.vmx.HCPhysVmcs));
3113 LogRel(("HM: CPU[%u] Current VMCS version %#x\n", i, pVCpu->hm.s.vmx.LastError.u32VmcsRev));
3114 LogRel(("HM: CPU[%u] Entered Host Cpu %u\n", i, pVCpu->hm.s.vmx.LastError.idEnteredCpu));
3115 LogRel(("HM: CPU[%u] Current Host Cpu %u\n", i, pVCpu->hm.s.vmx.LastError.idCurrentCpu));
3116 break;
3117
3118 case VERR_VMX_UNABLE_TO_START_VM:
3119 LogRel(("HM: VERR_VMX_UNABLE_TO_START_VM:\n"));
3120 LogRel(("HM: CPU[%u] Instruction error %#x\n", i, pVCpu->hm.s.vmx.LastError.u32InstrError));
3121 LogRel(("HM: CPU[%u] Exit reason %#x\n", i, pVCpu->hm.s.vmx.LastError.u32ExitReason));
3122
3123 if ( pVCpu->hm.s.vmx.LastError.u32InstrError == VMXINSTRERR_VMLAUNCH_NON_CLEAR_VMCS
3124 || pVCpu->hm.s.vmx.LastError.u32InstrError == VMXINSTRERR_VMRESUME_NON_LAUNCHED_VMCS)
3125 {
3126 LogRel(("HM: CPU[%u] Entered Host Cpu %u\n", i, pVCpu->hm.s.vmx.LastError.idEnteredCpu));
3127 LogRel(("HM: CPU[%u] Current Host Cpu %u\n", i, pVCpu->hm.s.vmx.LastError.idCurrentCpu));
3128 }
3129 else if (pVCpu->hm.s.vmx.LastError.u32InstrError == VMXINSTRERR_VMENTRY_INVALID_CTLS)
3130 {
3131 LogRel(("HM: CPU[%u] PinCtls %#RX32\n", i, pVCpu->hm.s.vmx.u32PinCtls));
3132 {
3133 uint32_t const u32Val = pVCpu->hm.s.vmx.u32PinCtls;
3134 HMVMX_LOGREL_FEAT(u32Val, VMX_PIN_CTLS_EXT_INT_EXIT );
3135 HMVMX_LOGREL_FEAT(u32Val, VMX_PIN_CTLS_NMI_EXIT );
3136 HMVMX_LOGREL_FEAT(u32Val, VMX_PIN_CTLS_VIRT_NMI );
3137 HMVMX_LOGREL_FEAT(u32Val, VMX_PIN_CTLS_PREEMPT_TIMER);
3138 HMVMX_LOGREL_FEAT(u32Val, VMX_PIN_CTLS_POSTED_INT );
3139 }
3140 LogRel(("HM: CPU[%u] ProcCtls %#RX32\n", i, pVCpu->hm.s.vmx.u32ProcCtls));
3141 {
3142 uint32_t const u32Val = pVCpu->hm.s.vmx.u32ProcCtls;
3143 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_INT_WINDOW_EXIT );
3144 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_USE_TSC_OFFSETTING);
3145 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_HLT_EXIT );
3146 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_INVLPG_EXIT );
3147 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_MWAIT_EXIT );
3148 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_RDPMC_EXIT );
3149 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_RDTSC_EXIT );
3150 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_CR3_LOAD_EXIT );
3151 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_CR3_STORE_EXIT );
3152 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_CR8_LOAD_EXIT );
3153 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_CR8_STORE_EXIT );
3154 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_USE_TPR_SHADOW );
3155 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_NMI_WINDOW_EXIT );
3156 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_MOV_DR_EXIT );
3157 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_UNCOND_IO_EXIT );
3158 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_USE_IO_BITMAPS );
3159 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_MONITOR_TRAP_FLAG );
3160 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_USE_MSR_BITMAPS );
3161 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_MONITOR_EXIT );
3162 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_PAUSE_EXIT );
3163 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_USE_SECONDARY_CTLS);
3164 }
3165 LogRel(("HM: CPU[%u] ProcCtls2 %#RX32\n", i, pVCpu->hm.s.vmx.u32ProcCtls2));
3166 {
3167 uint32_t const u32Val = pVCpu->hm.s.vmx.u32ProcCtls2;
3168 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_VIRT_APIC_ACCESS );
3169 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_EPT );
3170 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_DESC_TABLE_EXIT );
3171 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_RDTSCP );
3172 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_VIRT_X2APIC_MODE );
3173 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_VPID );
3174 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_WBINVD_EXIT );
3175 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_UNRESTRICTED_GUEST);
3176 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_APIC_REG_VIRT );
3177 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_VIRT_INT_DELIVERY );
3178 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_PAUSE_LOOP_EXIT );
3179 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_RDRAND_EXIT );
3180 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_INVPCID );
3181 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_VMFUNC );
3182 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_VMCS_SHADOWING );
3183 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_ENCLS_EXIT );
3184 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_RDSEED_EXIT );
3185 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_PML );
3186 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_EPT_VE );
3187 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_CONCEAL_FROM_PT );
3188 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_XSAVES_XRSTORS );
3189 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_TSC_SCALING );
3190 }
3191 LogRel(("HM: CPU[%u] EntryCtls %#RX32\n", i, pVCpu->hm.s.vmx.u32EntryCtls));
3192 {
3193 uint32_t const u32Val = pVCpu->hm.s.vmx.u32EntryCtls;
3194 HMVMX_LOGREL_FEAT(u32Val, VMX_ENTRY_CTLS_LOAD_DEBUG );
3195 HMVMX_LOGREL_FEAT(u32Val, VMX_ENTRY_CTLS_IA32E_MODE_GUEST );
3196 HMVMX_LOGREL_FEAT(u32Val, VMX_ENTRY_CTLS_ENTRY_TO_SMM );
3197 HMVMX_LOGREL_FEAT(u32Val, VMX_ENTRY_CTLS_DEACTIVATE_DUAL_MON);
3198 HMVMX_LOGREL_FEAT(u32Val, VMX_ENTRY_CTLS_LOAD_PERF_MSR );
3199 HMVMX_LOGREL_FEAT(u32Val, VMX_ENTRY_CTLS_LOAD_PAT_MSR );
3200 HMVMX_LOGREL_FEAT(u32Val, VMX_ENTRY_CTLS_LOAD_EFER_MSR );
3201 }
3202 LogRel(("HM: CPU[%u] ExitCtls %#RX32\n", i, pVCpu->hm.s.vmx.u32ExitCtls));
3203 {
3204 uint32_t const u32Val = pVCpu->hm.s.vmx.u32ExitCtls;
3205 HMVMX_LOGREL_FEAT(u32Val, VMX_EXIT_CTLS_SAVE_DEBUG );
3206 HMVMX_LOGREL_FEAT(u32Val, VMX_EXIT_CTLS_HOST_ADDR_SPACE_SIZE );
3207 HMVMX_LOGREL_FEAT(u32Val, VMX_EXIT_CTLS_LOAD_PERF_MSR );
3208 HMVMX_LOGREL_FEAT(u32Val, VMX_EXIT_CTLS_ACK_EXT_INT );
3209 HMVMX_LOGREL_FEAT(u32Val, VMX_EXIT_CTLS_SAVE_PAT_MSR );
3210 HMVMX_LOGREL_FEAT(u32Val, VMX_EXIT_CTLS_LOAD_PAT_MSR );
3211 HMVMX_LOGREL_FEAT(u32Val, VMX_EXIT_CTLS_SAVE_EFER_MSR );
3212 HMVMX_LOGREL_FEAT(u32Val, VMX_EXIT_CTLS_LOAD_EFER_MSR );
3213 HMVMX_LOGREL_FEAT(u32Val, VMX_EXIT_CTLS_SAVE_PREEMPT_TIMER );
3214 }
3215 LogRel(("HM: CPU[%u] HCPhysMsrBitmap %#RHp\n", i, pVCpu->hm.s.vmx.HCPhysMsrBitmap));
3216 LogRel(("HM: CPU[%u] HCPhysGuestMsr %#RHp\n", i, pVCpu->hm.s.vmx.HCPhysGuestMsr));
3217 LogRel(("HM: CPU[%u] HCPhysHostMsr %#RHp\n", i, pVCpu->hm.s.vmx.HCPhysHostMsr));
3218 LogRel(("HM: CPU[%u] cMsrs %u\n", i, pVCpu->hm.s.vmx.cMsrs));
3219 }
3220 /** @todo Log VM-entry event injection control fields
3221 * VMX_VMCS_CTRL_ENTRY_IRQ_INFO, VMX_VMCS_CTRL_ENTRY_EXCEPTION_ERRCODE
3222 * and VMX_VMCS_CTRL_ENTRY_INSTR_LENGTH from the VMCS. */
3223 break;
3224
3225 /* The guru will dump the HM error and exit history. Nothing extra to report for these errors. */
3226 case VERR_VMX_INVALID_VMXON_PTR:
3227 case VERR_HM_UNSUPPORTED_CPU_FEATURE_COMBO:
3228 case VERR_VMX_INVALID_GUEST_STATE:
3229 case VERR_VMX_UNEXPECTED_EXIT:
3230 case VERR_SVM_UNKNOWN_EXIT:
3231 case VERR_SVM_UNEXPECTED_EXIT:
3232 case VERR_SVM_UNEXPECTED_PATCH_TYPE:
3233 case VERR_SVM_UNEXPECTED_XCPT_EXIT:
3234 case VERR_VMX_UNEXPECTED_INTERRUPTION_EXIT_TYPE:
3235 break;
3236 }
3237 }
3238
3239 if (iStatusCode == VERR_VMX_UNABLE_TO_START_VM)
3240 {
3241 LogRel(("HM: VERR_VMX_UNABLE_TO_START_VM: VM-entry allowed-1 %#RX32\n", pVM->hm.s.vmx.Msrs.EntryCtls.n.allowed1));
3242 LogRel(("HM: VERR_VMX_UNABLE_TO_START_VM: VM-entry allowed-0 %#RX32\n", pVM->hm.s.vmx.Msrs.EntryCtls.n.allowed0));
3243 }
3244 else if (iStatusCode == VERR_VMX_INVALID_VMXON_PTR)
3245 LogRel(("HM: HCPhysVmxEnableError = %#RHp\n", pVM->hm.s.vmx.HCPhysVmxEnableError));
3246}
3247
3248
3249/**
3250 * Execute state save operation.
3251 *
3252 * Save only data that cannot be re-loaded while entering HM ring-0 code. This
3253 * is because we always save the VM state from ring-3 and thus most HM state
3254 * will be re-synced dynamically at runtime and don't need to be part of the VM
3255 * saved state.
3256 *
3257 * @returns VBox status code.
3258 * @param pVM The cross context VM structure.
3259 * @param pSSM SSM operation handle.
3260 */
3261static DECLCALLBACK(int) hmR3Save(PVM pVM, PSSMHANDLE pSSM)
3262{
3263 int rc;
3264
3265 Log(("hmR3Save:\n"));
3266
3267 for (VMCPUID i = 0; i < pVM->cCpus; i++)
3268 {
3269 Assert(!pVM->aCpus[i].hm.s.Event.fPending);
3270 if (pVM->cpum.ro.GuestFeatures.fSvm)
3271 {
3272 PCSVMNESTEDVMCBCACHE pVmcbNstGstCache = &pVM->aCpus[i].hm.s.svm.NstGstVmcbCache;
3273 rc = SSMR3PutBool(pSSM, pVmcbNstGstCache->fCacheValid);
3274 rc |= SSMR3PutU16(pSSM, pVmcbNstGstCache->u16InterceptRdCRx);
3275 rc |= SSMR3PutU16(pSSM, pVmcbNstGstCache->u16InterceptWrCRx);
3276 rc |= SSMR3PutU16(pSSM, pVmcbNstGstCache->u16InterceptRdDRx);
3277 rc |= SSMR3PutU16(pSSM, pVmcbNstGstCache->u16InterceptWrDRx);
3278 rc |= SSMR3PutU16(pSSM, pVmcbNstGstCache->u16PauseFilterThreshold);
3279 rc |= SSMR3PutU16(pSSM, pVmcbNstGstCache->u16PauseFilterCount);
3280 rc |= SSMR3PutU32(pSSM, pVmcbNstGstCache->u32InterceptXcpt);
3281 rc |= SSMR3PutU64(pSSM, pVmcbNstGstCache->u64InterceptCtrl);
3282 rc |= SSMR3PutU64(pSSM, pVmcbNstGstCache->u64TSCOffset);
3283 rc |= SSMR3PutBool(pSSM, pVmcbNstGstCache->fVIntrMasking);
3284 rc |= SSMR3PutBool(pSSM, pVmcbNstGstCache->fNestedPaging);
3285 rc |= SSMR3PutBool(pSSM, pVmcbNstGstCache->fLbrVirt);
3286 AssertRCReturn(rc, rc);
3287 }
3288 }
3289
3290 /* Save the guest patch data. */
3291 rc = SSMR3PutGCPtr(pSSM, pVM->hm.s.pGuestPatchMem);
3292 rc |= SSMR3PutGCPtr(pSSM, pVM->hm.s.pFreeGuestPatchMem);
3293 rc |= SSMR3PutU32(pSSM, pVM->hm.s.cbGuestPatchMem);
3294
3295 /* Store all the guest patch records too. */
3296 rc |= SSMR3PutU32(pSSM, pVM->hm.s.cPatches);
3297 AssertRCReturn(rc, rc);
3298
3299 for (uint32_t i = 0; i < pVM->hm.s.cPatches; i++)
3300 {
3301 AssertCompileSize(HMTPRINSTR, 4);
3302 PCHMTPRPATCH pPatch = &pVM->hm.s.aPatches[i];
3303 rc = SSMR3PutU32(pSSM, pPatch->Core.Key);
3304 rc |= SSMR3PutMem(pSSM, pPatch->aOpcode, sizeof(pPatch->aOpcode));
3305 rc |= SSMR3PutU32(pSSM, pPatch->cbOp);
3306 rc |= SSMR3PutMem(pSSM, pPatch->aNewOpcode, sizeof(pPatch->aNewOpcode));
3307 rc |= SSMR3PutU32(pSSM, pPatch->cbNewOp);
3308 rc |= SSMR3PutU32(pSSM, (uint32_t)pPatch->enmType);
3309 rc |= SSMR3PutU32(pSSM, pPatch->uSrcOperand);
3310 rc |= SSMR3PutU32(pSSM, pPatch->uDstOperand);
3311 rc |= SSMR3PutU32(pSSM, pPatch->pJumpTarget);
3312 rc |= SSMR3PutU32(pSSM, pPatch->cFaults);
3313 AssertRCReturn(rc, rc);
3314 }
3315
3316 return VINF_SUCCESS;
3317}
3318
3319
3320/**
3321 * Execute state load operation.
3322 *
3323 * @returns VBox status code.
3324 * @param pVM The cross context VM structure.
3325 * @param pSSM SSM operation handle.
3326 * @param uVersion Data layout version.
3327 * @param uPass The data pass.
3328 */
3329static DECLCALLBACK(int) hmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
3330{
3331 int rc;
3332
3333 LogFlowFunc(("uVersion=%u\n", uVersion));
3334 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
3335
3336 /*
3337 * Validate version.
3338 */
3339 if ( uVersion != HM_SAVED_STATE_VERSION_SVM_NESTED_HWVIRT
3340 && uVersion != HM_SAVED_STATE_VERSION_TPR_PATCHING
3341 && uVersion != HM_SAVED_STATE_VERSION_NO_TPR_PATCHING
3342 && uVersion != HM_SAVED_STATE_VERSION_2_0_X)
3343 {
3344 AssertMsgFailed(("hmR3Load: Invalid version uVersion=%d!\n", uVersion));
3345 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
3346 }
3347
3348 /*
3349 * Load per-VCPU state.
3350 */
3351 for (VMCPUID i = 0; i < pVM->cCpus; i++)
3352 {
3353 if (uVersion >= HM_SAVED_STATE_VERSION_SVM_NESTED_HWVIRT)
3354 {
3355 /* Load the SVM nested hw.virt state if the VM is configured for it. */
3356 if (pVM->cpum.ro.GuestFeatures.fSvm)
3357 {
3358 PSVMNESTEDVMCBCACHE pVmcbNstGstCache = &pVM->aCpus[i].hm.s.svm.NstGstVmcbCache;
3359 rc = SSMR3GetBool(pSSM, &pVmcbNstGstCache->fCacheValid);
3360 rc |= SSMR3GetU16(pSSM, &pVmcbNstGstCache->u16InterceptRdCRx);
3361 rc |= SSMR3GetU16(pSSM, &pVmcbNstGstCache->u16InterceptWrCRx);
3362 rc |= SSMR3GetU16(pSSM, &pVmcbNstGstCache->u16InterceptRdDRx);
3363 rc |= SSMR3GetU16(pSSM, &pVmcbNstGstCache->u16InterceptWrDRx);
3364 rc |= SSMR3GetU16(pSSM, &pVmcbNstGstCache->u16PauseFilterThreshold);
3365 rc |= SSMR3GetU16(pSSM, &pVmcbNstGstCache->u16PauseFilterCount);
3366 rc |= SSMR3GetU32(pSSM, &pVmcbNstGstCache->u32InterceptXcpt);
3367 rc |= SSMR3GetU64(pSSM, &pVmcbNstGstCache->u64InterceptCtrl);
3368 rc |= SSMR3GetU64(pSSM, &pVmcbNstGstCache->u64TSCOffset);
3369 rc |= SSMR3GetBool(pSSM, &pVmcbNstGstCache->fVIntrMasking);
3370 rc |= SSMR3GetBool(pSSM, &pVmcbNstGstCache->fNestedPaging);
3371 rc |= SSMR3GetBool(pSSM, &pVmcbNstGstCache->fLbrVirt);
3372 AssertRCReturn(rc, rc);
3373 }
3374 }
3375 else
3376 {
3377 /* Pending HM event (obsolete for a long time since TPRM holds the info.) */
3378 rc = SSMR3GetU32(pSSM, &pVM->aCpus[i].hm.s.Event.fPending);
3379 rc |= SSMR3GetU32(pSSM, &pVM->aCpus[i].hm.s.Event.u32ErrCode);
3380 rc |= SSMR3GetU64(pSSM, &pVM->aCpus[i].hm.s.Event.u64IntInfo);
3381
3382 /* VMX fWasInRealMode related data. */
3383 uint32_t uDummy;
3384 rc |= SSMR3GetU32(pSSM, &uDummy); AssertRCReturn(rc, rc);
3385 rc |= SSMR3GetU32(pSSM, &uDummy); AssertRCReturn(rc, rc);
3386 rc |= SSMR3GetU32(pSSM, &uDummy); AssertRCReturn(rc, rc);
3387 AssertRCReturn(rc, rc);
3388 }
3389 }
3390
3391 /*
3392 * Load TPR patching data.
3393 */
3394 if (uVersion >= HM_SAVED_STATE_VERSION_TPR_PATCHING)
3395 {
3396 rc = SSMR3GetGCPtr(pSSM, &pVM->hm.s.pGuestPatchMem);
3397 rc |= SSMR3GetGCPtr(pSSM, &pVM->hm.s.pFreeGuestPatchMem);
3398 rc |= SSMR3GetU32(pSSM, &pVM->hm.s.cbGuestPatchMem);
3399
3400 /* Fetch all TPR patch records. */
3401 rc |= SSMR3GetU32(pSSM, &pVM->hm.s.cPatches);
3402 AssertRCReturn(rc, rc);
3403 for (uint32_t i = 0; i < pVM->hm.s.cPatches; i++)
3404 {
3405 PHMTPRPATCH pPatch = &pVM->hm.s.aPatches[i];
3406 rc = SSMR3GetU32(pSSM, &pPatch->Core.Key);
3407 rc |= SSMR3GetMem(pSSM, pPatch->aOpcode, sizeof(pPatch->aOpcode));
3408 rc |= SSMR3GetU32(pSSM, &pPatch->cbOp);
3409 rc |= SSMR3GetMem(pSSM, pPatch->aNewOpcode, sizeof(pPatch->aNewOpcode));
3410 rc |= SSMR3GetU32(pSSM, &pPatch->cbNewOp);
3411 rc |= SSMR3GetU32(pSSM, (uint32_t *)&pPatch->enmType);
3412
3413 if (pPatch->enmType == HMTPRINSTR_JUMP_REPLACEMENT)
3414 pVM->hm.s.fTPRPatchingActive = true;
3415 Assert(pPatch->enmType == HMTPRINSTR_JUMP_REPLACEMENT || pVM->hm.s.fTPRPatchingActive == false);
3416
3417 rc |= SSMR3GetU32(pSSM, &pPatch->uSrcOperand);
3418 rc |= SSMR3GetU32(pSSM, &pPatch->uDstOperand);
3419 rc |= SSMR3GetU32(pSSM, &pPatch->cFaults);
3420 rc |= SSMR3GetU32(pSSM, &pPatch->pJumpTarget);
3421 AssertRCReturn(rc, rc);
3422
3423 LogFlow(("hmR3Load: patch %d\n", i));
3424 LogFlow(("Key = %x\n", pPatch->Core.Key));
3425 LogFlow(("cbOp = %d\n", pPatch->cbOp));
3426 LogFlow(("cbNewOp = %d\n", pPatch->cbNewOp));
3427 LogFlow(("type = %d\n", pPatch->enmType));
3428 LogFlow(("srcop = %d\n", pPatch->uSrcOperand));
3429 LogFlow(("dstop = %d\n", pPatch->uDstOperand));
3430 LogFlow(("cFaults = %d\n", pPatch->cFaults));
3431 LogFlow(("target = %x\n", pPatch->pJumpTarget));
3432
3433 rc = RTAvloU32Insert(&pVM->hm.s.PatchTree, &pPatch->Core);
3434 AssertRCReturn(rc, rc);
3435 }
3436 }
3437
3438 return VINF_SUCCESS;
3439}
3440
3441
3442/**
3443 * Gets the name of a VT-x exit code.
3444 *
3445 * @returns Pointer to read only string if @a uExit is known, otherwise NULL.
3446 * @param uExit The VT-x exit to name.
3447 */
3448VMMR3DECL(const char *) HMR3GetVmxExitName(uint32_t uExit)
3449{
3450 if (uExit < RT_ELEMENTS(g_apszVmxExitReasons))
3451 return g_apszVmxExitReasons[uExit];
3452 return NULL;
3453}
3454
3455
3456/**
3457 * Gets the name of an AMD-V exit code.
3458 *
3459 * @returns Pointer to read only string if @a uExit is known, otherwise NULL.
3460 * @param uExit The AMD-V exit to name.
3461 */
3462VMMR3DECL(const char *) HMR3GetSvmExitName(uint32_t uExit)
3463{
3464 if (uExit < RT_ELEMENTS(g_apszSvmExitReasons))
3465 return g_apszSvmExitReasons[uExit];
3466 return hmSvmGetSpecialExitReasonDesc(uExit);
3467}
3468
3469
3470/**
3471 * Displays HM info.
3472 *
3473 * @param pVM The cross context VM structure.
3474 * @param pHlp The info helper functions.
3475 * @param pszArgs Arguments, ignored.
3476 */
3477static DECLCALLBACK(void) hmR3Info(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
3478{
3479 NOREF(pszArgs);
3480 PVMCPU pVCpu = VMMGetCpu(pVM);
3481 if (!pVCpu)
3482 pVCpu = &pVM->aCpus[0];
3483
3484 if (HMIsEnabled(pVM))
3485 {
3486 if (pVM->hm.s.vmx.fSupported)
3487 pHlp->pfnPrintf(pHlp, "CPU[%u]: VT-x info:\n", pVCpu->idCpu);
3488 else
3489 pHlp->pfnPrintf(pHlp, "CPU[%u]: AMD-V info:\n", pVCpu->idCpu);
3490 pHlp->pfnPrintf(pHlp, " HM error = %#x (%u)\n", pVCpu->hm.s.u32HMError, pVCpu->hm.s.u32HMError);
3491 pHlp->pfnPrintf(pHlp, " rcLastExitToR3 = %Rrc\n", pVCpu->hm.s.rcLastExitToR3);
3492 }
3493 else
3494 pHlp->pfnPrintf(pHlp, "HM is not enabled for this VM!\n");
3495}
3496
3497
3498/**
3499 * Displays the HM pending event.
3500 *
3501 * @param pVM The cross context VM structure.
3502 * @param pHlp The info helper functions.
3503 * @param pszArgs Arguments, ignored.
3504 */
3505static DECLCALLBACK(void) hmR3InfoEventPending(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
3506{
3507 NOREF(pszArgs);
3508 PVMCPU pVCpu = VMMGetCpu(pVM);
3509 if (!pVCpu)
3510 pVCpu = &pVM->aCpus[0];
3511
3512 if (HMIsEnabled(pVM))
3513 {
3514 pHlp->pfnPrintf(pHlp, "CPU[%u]: HM event (fPending=%RTbool)\n", pVCpu->idCpu, pVCpu->hm.s.Event.fPending);
3515 if (pVCpu->hm.s.Event.fPending)
3516 {
3517 pHlp->pfnPrintf(pHlp, " u64IntInfo = %#RX64\n", pVCpu->hm.s.Event.u64IntInfo);
3518 pHlp->pfnPrintf(pHlp, " u32ErrCode = %#RX64\n", pVCpu->hm.s.Event.u32ErrCode);
3519 pHlp->pfnPrintf(pHlp, " cbInstr = %u bytes\n", pVCpu->hm.s.Event.cbInstr);
3520 pHlp->pfnPrintf(pHlp, " GCPtrFaultAddress = %#RGp\n", pVCpu->hm.s.Event.GCPtrFaultAddress);
3521 }
3522 }
3523 else
3524 pHlp->pfnPrintf(pHlp, "HM is not enabled for this VM!\n");
3525}
3526
3527
3528/**
3529 * Displays the SVM nested-guest VMCB cache.
3530 *
3531 * @param pVM The cross context VM structure.
3532 * @param pHlp The info helper functions.
3533 * @param pszArgs Arguments, ignored.
3534 */
3535static DECLCALLBACK(void) hmR3InfoSvmNstGstVmcbCache(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
3536{
3537 NOREF(pszArgs);
3538 PVMCPU pVCpu = VMMGetCpu(pVM);
3539 if (!pVCpu)
3540 pVCpu = &pVM->aCpus[0];
3541
3542 bool const fSvmEnabled = HMR3IsSvmEnabled(pVM->pUVM);
3543 if ( fSvmEnabled
3544 && pVM->cpum.ro.GuestFeatures.fSvm)
3545 {
3546 PCSVMNESTEDVMCBCACHE pVmcbNstGstCache = &pVCpu->hm.s.svm.NstGstVmcbCache;
3547 pHlp->pfnPrintf(pHlp, "CPU[%u]: HM SVM nested-guest VMCB cache\n", pVCpu->idCpu);
3548 pHlp->pfnPrintf(pHlp, " fCacheValid = %#RTbool\n", pVmcbNstGstCache->fCacheValid);
3549 pHlp->pfnPrintf(pHlp, " u16InterceptRdCRx = %#RX16\n", pVmcbNstGstCache->u16InterceptRdCRx);
3550 pHlp->pfnPrintf(pHlp, " u16InterceptWrCRx = %#RX16\n", pVmcbNstGstCache->u16InterceptWrCRx);
3551 pHlp->pfnPrintf(pHlp, " u16InterceptRdDRx = %#RX16\n", pVmcbNstGstCache->u16InterceptRdDRx);
3552 pHlp->pfnPrintf(pHlp, " u16InterceptWrDRx = %#RX16\n", pVmcbNstGstCache->u16InterceptWrDRx);
3553 pHlp->pfnPrintf(pHlp, " u16PauseFilterThreshold = %#RX16\n", pVmcbNstGstCache->u16PauseFilterThreshold);
3554 pHlp->pfnPrintf(pHlp, " u16PauseFilterCount = %#RX16\n", pVmcbNstGstCache->u16PauseFilterCount);
3555 pHlp->pfnPrintf(pHlp, " u32InterceptXcpt = %#RX32\n", pVmcbNstGstCache->u32InterceptXcpt);
3556 pHlp->pfnPrintf(pHlp, " u64InterceptCtrl = %#RX64\n", pVmcbNstGstCache->u64InterceptCtrl);
3557 pHlp->pfnPrintf(pHlp, " u64TSCOffset = %#RX64\n", pVmcbNstGstCache->u64TSCOffset);
3558 pHlp->pfnPrintf(pHlp, " fVIntrMasking = %RTbool\n", pVmcbNstGstCache->fVIntrMasking);
3559 pHlp->pfnPrintf(pHlp, " fNestedPaging = %RTbool\n", pVmcbNstGstCache->fNestedPaging);
3560 pHlp->pfnPrintf(pHlp, " fLbrVirt = %RTbool\n", pVmcbNstGstCache->fLbrVirt);
3561 }
3562 else
3563 {
3564 if (!fSvmEnabled)
3565 pHlp->pfnPrintf(pHlp, "HM SVM is not enabled for this VM!\n");
3566 else
3567 pHlp->pfnPrintf(pHlp, "SVM feature is not exposed to the guest!\n");
3568 }
3569}
3570
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