VirtualBox

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

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

VMM (and related changes): Add support for Shanghai/Zhaoxin CPUs. Modified and improved contribution by Journey Ren submitted under MIT license. Thank you!

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 162.0 KB
Line 
1/* $Id: HM.cpp 76886 2019-01-18 10:57:02Z 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 =
1093 ASMIsIntelCpu() || ASMIsViaCentaurCpu() || ASMIsShanghaiCpu()
1094 ? &g_apszVmxExitReasons[0]
1095 : &g_apszSvmExitReasons[0];
1096
1097 /*
1098 * Guest Exit reason stats.
1099 */
1100 pVCpu->hm.s.paStatExitReason = NULL;
1101 rc = MMHyperAlloc(pVM, MAX_EXITREASON_STAT * sizeof(*pVCpu->hm.s.paStatExitReason), 0 /* uAlignment */, MM_TAG_HM,
1102 (void **)&pVCpu->hm.s.paStatExitReason);
1103 AssertRCReturn(rc, rc);
1104 for (int j = 0; j < MAX_EXITREASON_STAT; j++)
1105 {
1106 if (papszDesc[j])
1107 {
1108 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.paStatExitReason[j], STAMTYPE_COUNTER, STAMVISIBILITY_USED,
1109 STAMUNIT_OCCURENCES, papszDesc[j], "/HM/CPU%d/Exit/Reason/%02x", i, j);
1110 AssertRCReturn(rc, rc);
1111 }
1112 }
1113 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatExitReasonNpf, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_OCCURENCES,
1114 "Nested page fault", "/HM/CPU%d/Exit/Reason/#NPF", i);
1115 AssertRCReturn(rc, rc);
1116 pVCpu->hm.s.paStatExitReasonR0 = MMHyperR3ToR0(pVM, pVCpu->hm.s.paStatExitReason);
1117# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1118 Assert(pVCpu->hm.s.paStatExitReasonR0 != NIL_RTR0PTR || !HMIsEnabled(pVM));
1119# else
1120 Assert(pVCpu->hm.s.paStatExitReasonR0 != NIL_RTR0PTR);
1121# endif
1122
1123#ifdef VBOX_WITH_NESTED_HWVIRT_SVM
1124 /*
1125 * Nested-guest Exit reason stats.
1126 */
1127 pVCpu->hm.s.paStatNestedExitReason = NULL;
1128 rc = MMHyperAlloc(pVM, MAX_EXITREASON_STAT * sizeof(*pVCpu->hm.s.paStatNestedExitReason), 0 /* uAlignment */, MM_TAG_HM,
1129 (void **)&pVCpu->hm.s.paStatNestedExitReason);
1130 AssertRCReturn(rc, rc);
1131 for (int j = 0; j < MAX_EXITREASON_STAT; j++)
1132 {
1133 if (papszDesc[j])
1134 {
1135 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.paStatNestedExitReason[j], STAMTYPE_COUNTER, STAMVISIBILITY_USED,
1136 STAMUNIT_OCCURENCES, papszDesc[j], "/HM/CPU%d/NestedExit/Reason/%02x", i, j);
1137 AssertRC(rc);
1138 }
1139 }
1140 rc = STAMR3RegisterF(pVM, &pVCpu->hm.s.StatNestedExitReasonNpf, STAMTYPE_COUNTER, STAMVISIBILITY_USED,
1141 STAMUNIT_OCCURENCES, "Nested page fault", "/HM/CPU%d/NestedExit/Reason/#NPF", i);
1142 AssertRCReturn(rc, rc);
1143 pVCpu->hm.s.paStatNestedExitReasonR0 = MMHyperR3ToR0(pVM, pVCpu->hm.s.paStatNestedExitReason);
1144# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1145 Assert(pVCpu->hm.s.paStatNestedExitReasonR0 != NIL_RTR0PTR || !HMIsEnabled(pVM));
1146# else
1147 Assert(pVCpu->hm.s.paStatNestedExitReasonR0 != NIL_RTR0PTR);
1148# endif
1149#endif
1150
1151 /*
1152 * Injected events stats.
1153 */
1154 rc = MMHyperAlloc(pVM, sizeof(STAMCOUNTER) * 256, 8, MM_TAG_HM, (void **)&pVCpu->hm.s.paStatInjectedIrqs);
1155 AssertRCReturn(rc, rc);
1156 pVCpu->hm.s.paStatInjectedIrqsR0 = MMHyperR3ToR0(pVM, pVCpu->hm.s.paStatInjectedIrqs);
1157# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1158 Assert(pVCpu->hm.s.paStatInjectedIrqsR0 != NIL_RTR0PTR || !HMIsEnabled(pVM));
1159# else
1160 Assert(pVCpu->hm.s.paStatInjectedIrqsR0 != NIL_RTR0PTR);
1161# endif
1162 for (unsigned j = 0; j < 255; j++)
1163 {
1164 STAMR3RegisterF(pVM, &pVCpu->hm.s.paStatInjectedIrqs[j], STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_OCCURENCES,
1165 "Injected event.",
1166 (j < 0x20) ? "/HM/CPU%d/EventInject/InjectTrap/%02X" : "/HM/CPU%d/EventInject/InjectIRQ/%02X", i, j);
1167 }
1168
1169#endif /* VBOX_WITH_STATISTICS */
1170 }
1171
1172#ifdef VBOX_WITH_CRASHDUMP_MAGIC
1173 /*
1174 * Magic marker for searching in crash dumps.
1175 */
1176 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1177 {
1178 PVMCPU pVCpu = &pVM->aCpus[i];
1179
1180 PVMCSCACHE pCache = &pVCpu->hm.s.vmx.VMCSCache;
1181 strcpy((char *)pCache->aMagic, "VMCSCACHE Magic");
1182 pCache->uMagic = UINT64_C(0xdeadbeefdeadbeef);
1183 }
1184#endif
1185
1186 return VINF_SUCCESS;
1187}
1188
1189
1190/**
1191 * Called when a init phase has completed.
1192 *
1193 * @returns VBox status code.
1194 * @param pVM The cross context VM structure.
1195 * @param enmWhat The phase that completed.
1196 */
1197VMMR3_INT_DECL(int) HMR3InitCompleted(PVM pVM, VMINITCOMPLETED enmWhat)
1198{
1199 switch (enmWhat)
1200 {
1201 case VMINITCOMPLETED_RING3:
1202 return hmR3InitFinalizeR3(pVM);
1203 case VMINITCOMPLETED_RING0:
1204 return hmR3InitFinalizeR0(pVM);
1205 default:
1206 return VINF_SUCCESS;
1207 }
1208}
1209
1210
1211/**
1212 * Turns off normal raw mode features.
1213 *
1214 * @param pVM The cross context VM structure.
1215 */
1216static void hmR3DisableRawMode(PVM pVM)
1217{
1218/** @todo r=bird: HM shouldn't be doing this crap. */
1219 /* Reinit the paging mode to force the new shadow mode. */
1220 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1221 {
1222 PVMCPU pVCpu = &pVM->aCpus[i];
1223 PGMHCChangeMode(pVM, pVCpu, PGMMODE_REAL);
1224 }
1225}
1226
1227
1228/**
1229 * Initialize VT-x or AMD-V.
1230 *
1231 * @returns VBox status code.
1232 * @param pVM The cross context VM structure.
1233 */
1234static int hmR3InitFinalizeR0(PVM pVM)
1235{
1236 int rc;
1237
1238 if (!HMIsEnabled(pVM))
1239 return VINF_SUCCESS;
1240
1241 /*
1242 * Hack to allow users to work around broken BIOSes that incorrectly set
1243 * EFER.SVME, which makes us believe somebody else is already using AMD-V.
1244 */
1245 if ( !pVM->hm.s.vmx.fSupported
1246 && !pVM->hm.s.svm.fSupported
1247 && pVM->hm.s.rcInit == VERR_SVM_IN_USE /* implies functional AMD-V */
1248 && RTEnvExist("VBOX_HWVIRTEX_IGNORE_SVM_IN_USE"))
1249 {
1250 LogRel(("HM: VBOX_HWVIRTEX_IGNORE_SVM_IN_USE active!\n"));
1251 pVM->hm.s.svm.fSupported = true;
1252 pVM->hm.s.svm.fIgnoreInUseError = true;
1253 pVM->hm.s.rcInit = VINF_SUCCESS;
1254 }
1255
1256 /*
1257 * Report ring-0 init errors.
1258 */
1259 if ( !pVM->hm.s.vmx.fSupported
1260 && !pVM->hm.s.svm.fSupported)
1261 {
1262 LogRel(("HM: Failed to initialize VT-x / AMD-V: %Rrc\n", pVM->hm.s.rcInit));
1263 LogRel(("HM: VMX MSR_IA32_FEATURE_CONTROL=%RX64\n", pVM->hm.s.vmx.Msrs.u64FeatCtrl));
1264 switch (pVM->hm.s.rcInit)
1265 {
1266 case VERR_VMX_IN_VMX_ROOT_MODE:
1267 return VM_SET_ERROR(pVM, VERR_VMX_IN_VMX_ROOT_MODE, "VT-x is being used by another hypervisor");
1268 case VERR_VMX_NO_VMX:
1269 return VM_SET_ERROR(pVM, VERR_VMX_NO_VMX, "VT-x is not available");
1270 case VERR_VMX_MSR_VMX_DISABLED:
1271 return VM_SET_ERROR(pVM, VERR_VMX_MSR_VMX_DISABLED, "VT-x is disabled in the BIOS");
1272 case VERR_VMX_MSR_ALL_VMX_DISABLED:
1273 return VM_SET_ERROR(pVM, VERR_VMX_MSR_ALL_VMX_DISABLED, "VT-x is disabled in the BIOS for all CPU modes");
1274 case VERR_VMX_MSR_LOCKING_FAILED:
1275 return VM_SET_ERROR(pVM, VERR_VMX_MSR_LOCKING_FAILED, "Failed to lock VT-x features while trying to enable VT-x");
1276 case VERR_VMX_MSR_VMX_ENABLE_FAILED:
1277 return VM_SET_ERROR(pVM, VERR_VMX_MSR_VMX_ENABLE_FAILED, "Failed to enable VT-x features");
1278 case VERR_VMX_MSR_SMX_VMX_ENABLE_FAILED:
1279 return VM_SET_ERROR(pVM, VERR_VMX_MSR_SMX_VMX_ENABLE_FAILED, "Failed to enable VT-x features in SMX mode");
1280
1281 case VERR_SVM_IN_USE:
1282 return VM_SET_ERROR(pVM, VERR_SVM_IN_USE, "AMD-V is being used by another hypervisor");
1283 case VERR_SVM_NO_SVM:
1284 return VM_SET_ERROR(pVM, VERR_SVM_NO_SVM, "AMD-V is not available");
1285 case VERR_SVM_DISABLED:
1286 return VM_SET_ERROR(pVM, VERR_SVM_DISABLED, "AMD-V is disabled in the BIOS");
1287 }
1288 return VMSetError(pVM, pVM->hm.s.rcInit, RT_SRC_POS, "HM ring-0 init failed: %Rrc", pVM->hm.s.rcInit);
1289 }
1290
1291 /*
1292 * Enable VT-x or AMD-V on all host CPUs.
1293 */
1294 rc = SUPR3CallVMMR0Ex(pVM->pVMR0, 0 /*idCpu*/, VMMR0_DO_HM_ENABLE, 0, NULL);
1295 if (RT_FAILURE(rc))
1296 {
1297 LogRel(("HM: Failed to enable, error %Rrc\n", rc));
1298 HMR3CheckError(pVM, rc);
1299 return rc;
1300 }
1301
1302 /*
1303 * No TPR patching is required when the IO-APIC is not enabled for this VM.
1304 * (Main should have taken care of this already)
1305 */
1306 if (!PDMHasIoApic(pVM))
1307 {
1308 Assert(!pVM->hm.s.fTprPatchingAllowed); /* paranoia */
1309 pVM->hm.s.fTprPatchingAllowed = false;
1310 }
1311
1312 /*
1313 * Check if L1D flush is needed/possible.
1314 */
1315 if ( !pVM->cpum.ro.HostFeatures.fFlushCmd
1316 || pVM->cpum.ro.HostFeatures.enmMicroarch < kCpumMicroarch_Intel_Core7_Nehalem
1317 || pVM->cpum.ro.HostFeatures.enmMicroarch >= kCpumMicroarch_Intel_Core7_End
1318 || pVM->cpum.ro.HostFeatures.fArchVmmNeedNotFlushL1d
1319 || pVM->cpum.ro.HostFeatures.fArchRdclNo)
1320 pVM->hm.s.fL1dFlushOnSched = pVM->hm.s.fL1dFlushOnVmEntry = false;
1321
1322 /*
1323 * Sync options.
1324 */
1325 /** @todo Move this out of of CPUMCTX and into some ring-0 only HM structure.
1326 * That will require a little bit of work, of course. */
1327 for (VMCPUID iCpu = 0; iCpu < pVM->cCpus; iCpu++)
1328 {
1329 PVMCPU pVCpu = &pVM->aCpus[iCpu];
1330 PCPUMCTX pCpuCtx = &pVCpu->cpum.GstCtx;
1331 pCpuCtx->fWorldSwitcher &= ~(CPUMCTX_WSF_IBPB_EXIT | CPUMCTX_WSF_IBPB_ENTRY);
1332 if (pVM->cpum.ro.HostFeatures.fIbpb)
1333 {
1334 if (pVM->hm.s.fIbpbOnVmExit)
1335 pCpuCtx->fWorldSwitcher |= CPUMCTX_WSF_IBPB_EXIT;
1336 if (pVM->hm.s.fIbpbOnVmEntry)
1337 pCpuCtx->fWorldSwitcher |= CPUMCTX_WSF_IBPB_ENTRY;
1338 }
1339 if (pVM->cpum.ro.HostFeatures.fFlushCmd && pVM->hm.s.fL1dFlushOnVmEntry)
1340 pCpuCtx->fWorldSwitcher |= CPUMCTX_WSF_L1D_ENTRY;
1341 if (iCpu == 0)
1342 LogRel(("HM: fWorldSwitcher=%#x (fIbpbOnVmExit=%RTbool fIbpbOnVmEntry=%RTbool fL1dFlushOnVmEntry=%RTbool); fL1dFlushOnSched=%RTbool\n",
1343 pCpuCtx->fWorldSwitcher, pVM->hm.s.fIbpbOnVmExit, pVM->hm.s.fIbpbOnVmEntry, pVM->hm.s.fL1dFlushOnVmEntry,
1344 pVM->hm.s.fL1dFlushOnSched));
1345 }
1346
1347 /*
1348 * Do the vendor specific initialization
1349 *
1350 * Note! We disable release log buffering here since we're doing relatively
1351 * lot of logging and doesn't want to hit the disk with each LogRel
1352 * statement.
1353 */
1354 AssertLogRelReturn(!pVM->hm.s.fInitialized, VERR_HM_IPE_5);
1355 bool fOldBuffered = RTLogRelSetBuffering(true /*fBuffered*/);
1356 if (pVM->hm.s.vmx.fSupported)
1357 rc = hmR3InitFinalizeR0Intel(pVM);
1358 else
1359 rc = hmR3InitFinalizeR0Amd(pVM);
1360 LogRel(("HM: VT-x/AMD-V init method: %s\n", (pVM->hm.s.fGlobalInit) ? "GLOBAL" : "LOCAL"));
1361 RTLogRelSetBuffering(fOldBuffered);
1362 pVM->hm.s.fInitialized = true;
1363
1364 return rc;
1365}
1366
1367
1368/**
1369 * @callback_method_impl{FNPDMVMMDEVHEAPNOTIFY}
1370 */
1371static DECLCALLBACK(void) hmR3VmmDevHeapNotify(PVM pVM, void *pvAllocation, RTGCPHYS GCPhysAllocation)
1372{
1373 NOREF(pVM);
1374 NOREF(pvAllocation);
1375 NOREF(GCPhysAllocation);
1376}
1377
1378
1379/**
1380 * Returns a description of the VMCS (and associated regions') memory type given the
1381 * IA32_VMX_BASIC MSR.
1382 *
1383 * @returns The descriptive memory type.
1384 * @param uMsrVmxBasic IA32_VMX_BASIC MSR value.
1385 */
1386static const char *hmR3VmxGetMemTypeDesc(uint64_t uMsrVmxBasic)
1387{
1388 uint8_t const uMemType = RT_BF_GET(uMsrVmxBasic, VMX_BF_BASIC_VMCS_MEM_TYPE);
1389 switch (uMemType)
1390 {
1391 case VMX_BASIC_MEM_TYPE_WB: return "Write Back (WB)";
1392 case VMX_BASIC_MEM_TYPE_UC: return "Uncacheable (UC)";
1393 }
1394 return "Unknown";
1395}
1396
1397
1398/**
1399 * Returns a single-line description of all the activity-states supported by the CPU
1400 * given the IA32_VMX_MISC MSR.
1401 *
1402 * @returns All supported activity states.
1403 * @param uMsrMisc IA32_VMX_MISC MSR value.
1404 */
1405static const char *hmR3VmxGetActivityStateAllDesc(uint64_t uMsrMisc)
1406{
1407 static const char * const s_apszActStates[] =
1408 {
1409 "",
1410 " ( HLT )",
1411 " ( SHUTDOWN )",
1412 " ( HLT SHUTDOWN )",
1413 " ( SIPI_WAIT )",
1414 " ( HLT SIPI_WAIT )",
1415 " ( SHUTDOWN SIPI_WAIT )",
1416 " ( HLT SHUTDOWN SIPI_WAIT )"
1417 };
1418 uint8_t const idxActStates = RT_BF_GET(uMsrMisc, VMX_BF_MISC_ACTIVITY_STATES);
1419 Assert(idxActStates < RT_ELEMENTS(s_apszActStates));
1420 return s_apszActStates[idxActStates];
1421}
1422
1423
1424/**
1425 * Reports MSR_IA32_FEATURE_CONTROL MSR to the log.
1426 *
1427 * @param fFeatMsr The feature control MSR value.
1428 */
1429static void hmR3VmxReportFeatCtlMsr(uint64_t fFeatMsr)
1430{
1431 uint64_t const val = fFeatMsr;
1432 LogRel(("HM: MSR_IA32_FEATURE_CONTROL = %#RX64\n", val));
1433 HMVMX_REPORT_MSR_CAP(val, "LOCK", MSR_IA32_FEATURE_CONTROL_LOCK);
1434 HMVMX_REPORT_MSR_CAP(val, "SMX_VMXON", MSR_IA32_FEATURE_CONTROL_SMX_VMXON);
1435 HMVMX_REPORT_MSR_CAP(val, "VMXON", MSR_IA32_FEATURE_CONTROL_VMXON);
1436 HMVMX_REPORT_MSR_CAP(val, "SENTER_LOCAL_FN0", MSR_IA32_FEATURE_CONTROL_SENTER_LOCAL_FN_0);
1437 HMVMX_REPORT_MSR_CAP(val, "SENTER_LOCAL_FN1", MSR_IA32_FEATURE_CONTROL_SENTER_LOCAL_FN_1);
1438 HMVMX_REPORT_MSR_CAP(val, "SENTER_LOCAL_FN2", MSR_IA32_FEATURE_CONTROL_SENTER_LOCAL_FN_2);
1439 HMVMX_REPORT_MSR_CAP(val, "SENTER_LOCAL_FN3", MSR_IA32_FEATURE_CONTROL_SENTER_LOCAL_FN_3);
1440 HMVMX_REPORT_MSR_CAP(val, "SENTER_LOCAL_FN4", MSR_IA32_FEATURE_CONTROL_SENTER_LOCAL_FN_4);
1441 HMVMX_REPORT_MSR_CAP(val, "SENTER_LOCAL_FN5", MSR_IA32_FEATURE_CONTROL_SENTER_LOCAL_FN_5);
1442 HMVMX_REPORT_MSR_CAP(val, "SENTER_LOCAL_FN6", MSR_IA32_FEATURE_CONTROL_SENTER_LOCAL_FN_6);
1443 HMVMX_REPORT_MSR_CAP(val, "SENTER_GLOBAL_EN", MSR_IA32_FEATURE_CONTROL_SENTER_GLOBAL_EN);
1444 HMVMX_REPORT_MSR_CAP(val, "SGX_LAUNCH_EN", MSR_IA32_FEATURE_CONTROL_SGX_LAUNCH_EN);
1445 HMVMX_REPORT_MSR_CAP(val, "SGX_GLOBAL_EN", MSR_IA32_FEATURE_CONTROL_SGX_GLOBAL_EN);
1446 HMVMX_REPORT_MSR_CAP(val, "LMCE", MSR_IA32_FEATURE_CONTROL_LMCE);
1447 if (!(val & MSR_IA32_FEATURE_CONTROL_LOCK))
1448 LogRel(("HM: MSR_IA32_FEATURE_CONTROL lock bit not set, possibly bad hardware!\n"));
1449}
1450
1451
1452/**
1453 * Reports MSR_IA32_VMX_BASIC MSR to the log.
1454 *
1455 * @param uBasicMsr The VMX basic MSR value.
1456 */
1457static void hmR3VmxReportBasicMsr(uint64_t uBasicMsr)
1458{
1459 LogRel(("HM: MSR_IA32_VMX_BASIC = %#RX64\n", uBasicMsr));
1460 LogRel(("HM: VMCS id = %#x\n", RT_BF_GET(uBasicMsr, VMX_BF_BASIC_VMCS_ID)));
1461 LogRel(("HM: VMCS size = %u bytes\n", RT_BF_GET(uBasicMsr, VMX_BF_BASIC_VMCS_SIZE)));
1462 LogRel(("HM: VMCS physical address limit = %s\n", RT_BF_GET(uBasicMsr, VMX_BF_BASIC_PHYSADDR_WIDTH) ?
1463 "< 4 GB" : "None"));
1464 LogRel(("HM: VMCS memory type = %s\n", hmR3VmxGetMemTypeDesc(uBasicMsr)));
1465 LogRel(("HM: Dual-monitor treatment support = %RTbool\n", RT_BF_GET(uBasicMsr, VMX_BF_BASIC_DUAL_MON)));
1466 LogRel(("HM: OUTS & INS instruction-info = %RTbool\n", RT_BF_GET(uBasicMsr, VMX_BF_BASIC_VMCS_INS_OUTS)));
1467 LogRel(("HM: Supports true capability MSRs = %RTbool\n", RT_BF_GET(uBasicMsr, VMX_BF_BASIC_TRUE_CTLS)));
1468}
1469
1470
1471/**
1472 * Reports MSR_IA32_PINBASED_CTLS to the log.
1473 *
1474 * @param pVmxMsr Pointer to the VMX MSR.
1475 */
1476static void hmR3VmxReportPinBasedCtlsMsr(PCVMXCTLSMSR pVmxMsr)
1477{
1478 uint64_t const fAllowed1 = pVmxMsr->n.allowed1;
1479 uint64_t const fAllowed0 = pVmxMsr->n.allowed0;
1480 LogRel(("HM: MSR_IA32_VMX_PINBASED_CTLS = %#RX64\n", pVmxMsr->u));
1481 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "EXT_INT_EXIT", VMX_PIN_CTLS_EXT_INT_EXIT);
1482 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "NMI_EXIT", VMX_PIN_CTLS_NMI_EXIT);
1483 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "VIRTUAL_NMI", VMX_PIN_CTLS_VIRT_NMI);
1484 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "PREEMPT_TIMER", VMX_PIN_CTLS_PREEMPT_TIMER);
1485 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "POSTED_INT", VMX_PIN_CTLS_POSTED_INT);
1486}
1487
1488
1489/**
1490 * Reports MSR_IA32_VMX_PROCBASED_CTLS MSR to the log.
1491 *
1492 * @param pVmxMsr Pointer to the VMX MSR.
1493 */
1494static void hmR3VmxReportProcBasedCtlsMsr(PCVMXCTLSMSR pVmxMsr)
1495{
1496 uint64_t const fAllowed1 = pVmxMsr->n.allowed1;
1497 uint64_t const fAllowed0 = pVmxMsr->n.allowed0;
1498 LogRel(("HM: MSR_IA32_VMX_PROCBASED_CTLS = %#RX64\n", pVmxMsr->u));
1499 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "INT_WINDOW_EXIT", VMX_PROC_CTLS_INT_WINDOW_EXIT);
1500 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "USE_TSC_OFFSETTING", VMX_PROC_CTLS_USE_TSC_OFFSETTING);
1501 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "HLT_EXIT", VMX_PROC_CTLS_HLT_EXIT);
1502 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "INVLPG_EXIT", VMX_PROC_CTLS_INVLPG_EXIT);
1503 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "MWAIT_EXIT", VMX_PROC_CTLS_MWAIT_EXIT);
1504 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "RDPMC_EXIT", VMX_PROC_CTLS_RDPMC_EXIT);
1505 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "RDTSC_EXIT", VMX_PROC_CTLS_RDTSC_EXIT);
1506 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "CR3_LOAD_EXIT", VMX_PROC_CTLS_CR3_LOAD_EXIT);
1507 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "CR3_STORE_EXIT", VMX_PROC_CTLS_CR3_STORE_EXIT);
1508 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "CR8_LOAD_EXIT", VMX_PROC_CTLS_CR8_LOAD_EXIT);
1509 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "CR8_STORE_EXIT", VMX_PROC_CTLS_CR8_STORE_EXIT);
1510 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "USE_TPR_SHADOW", VMX_PROC_CTLS_USE_TPR_SHADOW);
1511 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "NMI_WINDOW_EXIT", VMX_PROC_CTLS_NMI_WINDOW_EXIT);
1512 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "MOV_DR_EXIT", VMX_PROC_CTLS_MOV_DR_EXIT);
1513 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "UNCOND_IO_EXIT", VMX_PROC_CTLS_UNCOND_IO_EXIT);
1514 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "USE_IO_BITMAPS", VMX_PROC_CTLS_USE_IO_BITMAPS);
1515 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "MONITOR_TRAP_FLAG", VMX_PROC_CTLS_MONITOR_TRAP_FLAG);
1516 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "USE_MSR_BITMAPS", VMX_PROC_CTLS_USE_MSR_BITMAPS);
1517 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "MONITOR_EXIT", VMX_PROC_CTLS_MONITOR_EXIT);
1518 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "PAUSE_EXIT", VMX_PROC_CTLS_PAUSE_EXIT);
1519 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "USE_SECONDARY_CTLS", VMX_PROC_CTLS_USE_SECONDARY_CTLS);
1520}
1521
1522
1523/**
1524 * Reports MSR_IA32_VMX_PROCBASED_CTLS2 MSR to the log.
1525 *
1526 * @param pVmxMsr Pointer to the VMX MSR.
1527 */
1528static void hmR3VmxReportProcBasedCtls2Msr(PCVMXCTLSMSR pVmxMsr)
1529{
1530 uint64_t const fAllowed1 = pVmxMsr->n.allowed1;
1531 uint64_t const fAllowed0 = pVmxMsr->n.allowed0;
1532 LogRel(("HM: MSR_IA32_VMX_PROCBASED_CTLS2 = %#RX64\n", pVmxMsr->u));
1533 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "VIRT_APIC_ACCESS", VMX_PROC_CTLS2_VIRT_APIC_ACCESS);
1534 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "EPT", VMX_PROC_CTLS2_EPT);
1535 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "DESC_TABLE_EXIT", VMX_PROC_CTLS2_DESC_TABLE_EXIT);
1536 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "RDTSCP", VMX_PROC_CTLS2_RDTSCP);
1537 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "VIRT_X2APIC_MODE", VMX_PROC_CTLS2_VIRT_X2APIC_MODE);
1538 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "VPID", VMX_PROC_CTLS2_VPID);
1539 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "WBINVD_EXIT", VMX_PROC_CTLS2_WBINVD_EXIT);
1540 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "UNRESTRICTED_GUEST", VMX_PROC_CTLS2_UNRESTRICTED_GUEST);
1541 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "APIC_REG_VIRT", VMX_PROC_CTLS2_APIC_REG_VIRT);
1542 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "VIRT_INT_DELIVERY", VMX_PROC_CTLS2_VIRT_INT_DELIVERY);
1543 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "PAUSE_LOOP_EXIT", VMX_PROC_CTLS2_PAUSE_LOOP_EXIT);
1544 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "RDRAND_EXIT", VMX_PROC_CTLS2_RDRAND_EXIT);
1545 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "INVPCID", VMX_PROC_CTLS2_INVPCID);
1546 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "VMFUNC", VMX_PROC_CTLS2_VMFUNC);
1547 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "VMCS_SHADOWING", VMX_PROC_CTLS2_VMCS_SHADOWING);
1548 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "ENCLS_EXIT", VMX_PROC_CTLS2_ENCLS_EXIT);
1549 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "RDSEED_EXIT", VMX_PROC_CTLS2_RDSEED_EXIT);
1550 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "PML", VMX_PROC_CTLS2_PML);
1551 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "EPT_VE", VMX_PROC_CTLS2_EPT_VE);
1552 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "CONCEAL_FROM_PT", VMX_PROC_CTLS2_CONCEAL_FROM_PT);
1553 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "XSAVES_XRSTORS", VMX_PROC_CTLS2_XSAVES_XRSTORS);
1554 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "TSC_SCALING", VMX_PROC_CTLS2_TSC_SCALING);
1555}
1556
1557
1558/**
1559 * Reports MSR_IA32_VMX_ENTRY_CTLS to the log.
1560 *
1561 * @param pVmxMsr Pointer to the VMX MSR.
1562 */
1563static void hmR3VmxReportEntryCtlsMsr(PCVMXCTLSMSR pVmxMsr)
1564{
1565 uint64_t const fAllowed1 = pVmxMsr->n.allowed1;
1566 uint64_t const fAllowed0 = pVmxMsr->n.allowed0;
1567 LogRel(("HM: MSR_IA32_VMX_ENTRY_CTLS = %#RX64\n", pVmxMsr->u));
1568 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "LOAD_DEBUG", VMX_ENTRY_CTLS_LOAD_DEBUG);
1569 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "IA32E_MODE_GUEST", VMX_ENTRY_CTLS_IA32E_MODE_GUEST);
1570 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "ENTRY_TO_SMM", VMX_ENTRY_CTLS_ENTRY_TO_SMM);
1571 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "DEACTIVATE_DUAL_MON", VMX_ENTRY_CTLS_DEACTIVATE_DUAL_MON);
1572 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "LOAD_PERF_MSR", VMX_ENTRY_CTLS_LOAD_PERF_MSR);
1573 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "LOAD_PAT_MSR", VMX_ENTRY_CTLS_LOAD_PAT_MSR);
1574 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "LOAD_EFER_MSR", VMX_ENTRY_CTLS_LOAD_EFER_MSR);
1575}
1576
1577
1578/**
1579 * Reports MSR_IA32_VMX_EXIT_CTLS to the log.
1580 *
1581 * @param pVmxMsr Pointer to the VMX MSR.
1582 */
1583static void hmR3VmxReportExitCtlsMsr(PCVMXCTLSMSR pVmxMsr)
1584{
1585 uint64_t const fAllowed1 = pVmxMsr->n.allowed1;
1586 uint64_t const fAllowed0 = pVmxMsr->n.allowed0;
1587 LogRel(("HM: MSR_IA32_VMX_EXIT_CTLS = %#RX64\n", pVmxMsr->u));
1588 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "SAVE_DEBUG", VMX_EXIT_CTLS_SAVE_DEBUG);
1589 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "HOST_ADDR_SPACE_SIZE", VMX_EXIT_CTLS_HOST_ADDR_SPACE_SIZE);
1590 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "LOAD_PERF_MSR", VMX_EXIT_CTLS_LOAD_PERF_MSR);
1591 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "ACK_EXT_INT", VMX_EXIT_CTLS_ACK_EXT_INT);
1592 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "SAVE_PAT_MSR", VMX_EXIT_CTLS_SAVE_PAT_MSR);
1593 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "LOAD_PAT_MSR", VMX_EXIT_CTLS_LOAD_PAT_MSR);
1594 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "SAVE_EFER_MSR", VMX_EXIT_CTLS_SAVE_EFER_MSR);
1595 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "LOAD_EFER_MSR", VMX_EXIT_CTLS_LOAD_EFER_MSR);
1596 HMVMX_REPORT_FEAT(fAllowed1, fAllowed0, "SAVE_PREEMPT_TIMER", VMX_EXIT_CTLS_SAVE_PREEMPT_TIMER);
1597}
1598
1599
1600/**
1601 * Reports MSR_IA32_VMX_EPT_VPID_CAP MSR to the log.
1602 *
1603 * @param fCaps The VMX EPT/VPID capability MSR value.
1604 */
1605static void hmR3VmxReportEptVpidCapsMsr(uint64_t fCaps)
1606{
1607 LogRel(("HM: MSR_IA32_VMX_EPT_VPID_CAP = %#RX64\n", fCaps));
1608 HMVMX_REPORT_MSR_CAP(fCaps, "RWX_X_ONLY", MSR_IA32_VMX_EPT_VPID_CAP_RWX_X_ONLY);
1609 HMVMX_REPORT_MSR_CAP(fCaps, "PAGE_WALK_LENGTH_4", MSR_IA32_VMX_EPT_VPID_CAP_PAGE_WALK_LENGTH_4);
1610 HMVMX_REPORT_MSR_CAP(fCaps, "EMT_UC", MSR_IA32_VMX_EPT_VPID_CAP_EMT_UC);
1611 HMVMX_REPORT_MSR_CAP(fCaps, "EMT_WB", MSR_IA32_VMX_EPT_VPID_CAP_EMT_WB);
1612 HMVMX_REPORT_MSR_CAP(fCaps, "PDE_2M", MSR_IA32_VMX_EPT_VPID_CAP_PDE_2M);
1613 HMVMX_REPORT_MSR_CAP(fCaps, "PDPTE_1G", MSR_IA32_VMX_EPT_VPID_CAP_PDPTE_1G);
1614 HMVMX_REPORT_MSR_CAP(fCaps, "INVEPT", MSR_IA32_VMX_EPT_VPID_CAP_INVEPT);
1615 HMVMX_REPORT_MSR_CAP(fCaps, "EPT_ACCESS_DIRTY", MSR_IA32_VMX_EPT_VPID_CAP_EPT_ACCESS_DIRTY);
1616 HMVMX_REPORT_MSR_CAP(fCaps, "INVEPT_SINGLE_CONTEXT", MSR_IA32_VMX_EPT_VPID_CAP_INVEPT_SINGLE_CONTEXT);
1617 HMVMX_REPORT_MSR_CAP(fCaps, "INVEPT_ALL_CONTEXTS", MSR_IA32_VMX_EPT_VPID_CAP_INVEPT_ALL_CONTEXTS);
1618 HMVMX_REPORT_MSR_CAP(fCaps, "INVVPID", MSR_IA32_VMX_EPT_VPID_CAP_INVVPID);
1619 HMVMX_REPORT_MSR_CAP(fCaps, "INVVPID_INDIV_ADDR", MSR_IA32_VMX_EPT_VPID_CAP_INVVPID_INDIV_ADDR);
1620 HMVMX_REPORT_MSR_CAP(fCaps, "INVVPID_SINGLE_CONTEXT", MSR_IA32_VMX_EPT_VPID_CAP_INVVPID_SINGLE_CONTEXT);
1621 HMVMX_REPORT_MSR_CAP(fCaps, "INVVPID_ALL_CONTEXTS", MSR_IA32_VMX_EPT_VPID_CAP_INVVPID_ALL_CONTEXTS);
1622 HMVMX_REPORT_MSR_CAP(fCaps, "INVVPID_SINGLE_CONTEXT_RETAIN_GLOBALS", MSR_IA32_VMX_EPT_VPID_CAP_INVVPID_SINGLE_CONTEXT_RETAIN_GLOBALS);
1623}
1624
1625
1626/**
1627 * Reports MSR_IA32_VMX_MISC MSR to the log.
1628 *
1629 * @param pVM Pointer to the VM.
1630 * @param fMisc The VMX misc. MSR value.
1631 */
1632static void hmR3VmxReportMiscMsr(PVM pVM, uint64_t fMisc)
1633{
1634 LogRel(("HM: MSR_IA32_VMX_MISC = %#RX64\n", fMisc));
1635 uint8_t const cPreemptTimerShift = RT_BF_GET(fMisc, VMX_BF_MISC_PREEMPT_TIMER_TSC);
1636 if (cPreemptTimerShift == pVM->hm.s.vmx.cPreemptTimerShift)
1637 LogRel(("HM: PREEMPT_TIMER_TSC = %#x\n", cPreemptTimerShift));
1638 else
1639 {
1640 LogRel(("HM: PREEMPT_TIMER_TSC = %#x - erratum detected, using %#x instead\n", cPreemptTimerShift,
1641 pVM->hm.s.vmx.cPreemptTimerShift));
1642 }
1643 LogRel(("HM: EXIT_SAVE_EFER_LMA = %RTbool\n", RT_BF_GET(fMisc, VMX_BF_MISC_EXIT_SAVE_EFER_LMA)));
1644 LogRel(("HM: ACTIVITY_STATES = %#x%s\n", RT_BF_GET(fMisc, VMX_BF_MISC_ACTIVITY_STATES),
1645 hmR3VmxGetActivityStateAllDesc(fMisc)));
1646 LogRel(("HM: INTEL_PT = %RTbool\n", RT_BF_GET(fMisc, VMX_BF_MISC_INTEL_PT)));
1647 LogRel(("HM: SMM_READ_SMBASE_MSR = %RTbool\n", RT_BF_GET(fMisc, VMX_BF_MISC_SMM_READ_SMBASE_MSR)));
1648 LogRel(("HM: CR3_TARGET = %#x\n", RT_BF_GET(fMisc, VMX_BF_MISC_CR3_TARGET)));
1649 LogRel(("HM: MAX_MSR = %#x ( %u )\n", RT_BF_GET(fMisc, VMX_BF_MISC_MAX_MSRS),
1650 VMX_MISC_MAX_MSRS(fMisc)));
1651 LogRel(("HM: VMXOFF_BLOCK_SMI = %RTbool\n", RT_BF_GET(fMisc, VMX_BF_MISC_VMXOFF_BLOCK_SMI)));
1652 LogRel(("HM: VMWRITE_ALL = %RTbool\n", RT_BF_GET(fMisc, VMX_BF_MISC_VMWRITE_ALL)));
1653 LogRel(("HM: ENTRY_INJECT_SOFT_INT = %#x\n", RT_BF_GET(fMisc, VMX_BF_MISC_ENTRY_INJECT_SOFT_INT)));
1654 LogRel(("HM: MSEG_ID = %#x\n", RT_BF_GET(fMisc, VMX_BF_MISC_MSEG_ID)));
1655}
1656
1657
1658/**
1659 * Reports MSR_IA32_VMX_VMCS_ENUM MSR to the log.
1660 *
1661 * @param uVmcsEnum The VMX VMCS enum MSR value.
1662 */
1663static void hmR3VmxReportVmcsEnumMsr(uint64_t uVmcsEnum)
1664{
1665 LogRel(("HM: MSR_IA32_VMX_VMCS_ENUM = %#RX64\n", uVmcsEnum));
1666 LogRel(("HM: HIGHEST_IDX = %#x\n", RT_BF_GET(uVmcsEnum, VMX_BF_VMCS_ENUM_HIGHEST_IDX)));
1667}
1668
1669
1670/**
1671 * Reports MSR_IA32_VMX_VMFUNC MSR to the log.
1672 *
1673 * @param uVmFunc The VMX VMFUNC MSR value.
1674 */
1675static void hmR3VmxReportVmFuncMsr(uint64_t uVmFunc)
1676{
1677 LogRel(("HM: MSR_IA32_VMX_VMFUNC = %#RX64\n", uVmFunc));
1678 HMVMX_REPORT_ALLOWED_FEAT(uVmFunc, "EPTP_SWITCHING", RT_BF_GET(uVmFunc, VMX_BF_VMFUNC_EPTP_SWITCHING));
1679}
1680
1681
1682/**
1683 * Reports VMX CR0, CR4 fixed MSRs.
1684 *
1685 * @param pMsrs Pointer to the VMX MSRs.
1686 */
1687static void hmR3VmxReportCrFixedMsrs(PVMXMSRS pMsrs)
1688{
1689 LogRel(("HM: MSR_IA32_VMX_CR0_FIXED0 = %#RX64\n", pMsrs->u64Cr0Fixed0));
1690 LogRel(("HM: MSR_IA32_VMX_CR0_FIXED1 = %#RX64\n", pMsrs->u64Cr0Fixed1));
1691 LogRel(("HM: MSR_IA32_VMX_CR4_FIXED0 = %#RX64\n", pMsrs->u64Cr4Fixed0));
1692 LogRel(("HM: MSR_IA32_VMX_CR4_FIXED1 = %#RX64\n", pMsrs->u64Cr4Fixed1));
1693}
1694
1695
1696/**
1697 * Finish VT-x initialization (after ring-0 init).
1698 *
1699 * @returns VBox status code.
1700 * @param pVM The cross context VM structure.
1701 */
1702static int hmR3InitFinalizeR0Intel(PVM pVM)
1703{
1704 int rc;
1705
1706 Log(("pVM->hm.s.vmx.fSupported = %d\n", pVM->hm.s.vmx.fSupported));
1707 AssertLogRelReturn(pVM->hm.s.vmx.Msrs.u64FeatCtrl != 0, VERR_HM_IPE_4);
1708
1709 LogRel(("HM: Using VT-x implementation 2.0\n"));
1710 LogRel(("HM: Max resume loops = %u\n", pVM->hm.s.cMaxResumeLoops));
1711 LogRel(("HM: Host CR4 = %#RX64\n", pVM->hm.s.vmx.u64HostCr4));
1712 LogRel(("HM: Host EFER = %#RX64\n", pVM->hm.s.vmx.u64HostEfer));
1713 LogRel(("HM: MSR_IA32_SMM_MONITOR_CTL = %#RX64\n", pVM->hm.s.vmx.u64HostSmmMonitorCtl));
1714
1715 hmR3VmxReportFeatCtlMsr(pVM->hm.s.vmx.Msrs.u64FeatCtrl);
1716 hmR3VmxReportBasicMsr(pVM->hm.s.vmx.Msrs.u64Basic);
1717
1718 hmR3VmxReportPinBasedCtlsMsr(&pVM->hm.s.vmx.Msrs.PinCtls);
1719 hmR3VmxReportProcBasedCtlsMsr(&pVM->hm.s.vmx.Msrs.ProcCtls);
1720 if (pVM->hm.s.vmx.Msrs.ProcCtls.n.allowed1 & VMX_PROC_CTLS_USE_SECONDARY_CTLS)
1721 hmR3VmxReportProcBasedCtls2Msr(&pVM->hm.s.vmx.Msrs.ProcCtls2);
1722
1723 hmR3VmxReportEntryCtlsMsr(&pVM->hm.s.vmx.Msrs.EntryCtls);
1724 hmR3VmxReportExitCtlsMsr(&pVM->hm.s.vmx.Msrs.ExitCtls);
1725
1726 if (RT_BF_GET(pVM->hm.s.vmx.Msrs.u64Basic, VMX_BF_BASIC_TRUE_CTLS))
1727 {
1728 /* We don't extensively dump the true capability MSRs as we don't use them, see @bugref{9180#c5}. */
1729 LogRel(("HM: MSR_IA32_VMX_TRUE_PINBASED_CTLS = %#RX64\n", pVM->hm.s.vmx.Msrs.TruePinCtls));
1730 LogRel(("HM: MSR_IA32_VMX_TRUE_PROCBASED_CTLS = %#RX64\n", pVM->hm.s.vmx.Msrs.TrueProcCtls));
1731 LogRel(("HM: MSR_IA32_VMX_TRUE_ENTRY_CTLS = %#RX64\n", pVM->hm.s.vmx.Msrs.TrueEntryCtls));
1732 LogRel(("HM: MSR_IA32_VMX_TRUE_EXIT_CTLS = %#RX64\n", pVM->hm.s.vmx.Msrs.TrueExitCtls));
1733 }
1734
1735 hmR3VmxReportMiscMsr(pVM, pVM->hm.s.vmx.Msrs.u64Misc);
1736 hmR3VmxReportVmcsEnumMsr(pVM->hm.s.vmx.Msrs.u64VmcsEnum);
1737 if (pVM->hm.s.vmx.Msrs.u64EptVpidCaps)
1738 hmR3VmxReportEptVpidCapsMsr(pVM->hm.s.vmx.Msrs.u64EptVpidCaps);
1739 if (pVM->hm.s.vmx.Msrs.u64VmFunc)
1740 hmR3VmxReportVmFuncMsr(pVM->hm.s.vmx.Msrs.u64VmFunc);
1741 hmR3VmxReportCrFixedMsrs(&pVM->hm.s.vmx.Msrs);
1742
1743 LogRel(("HM: APIC-access page physaddr = %#RHp\n", pVM->hm.s.vmx.HCPhysApicAccess));
1744 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1745 {
1746 LogRel(("HM: VCPU%3d: MSR bitmap physaddr = %#RHp\n", i, pVM->aCpus[i].hm.s.vmx.HCPhysMsrBitmap));
1747 LogRel(("HM: VCPU%3d: VMCS physaddr = %#RHp\n", i, pVM->aCpus[i].hm.s.vmx.HCPhysVmcs));
1748 }
1749
1750 /*
1751 * EPT and unrestricted guest execution are determined in HMR3Init, verify the sanity of that.
1752 */
1753 AssertLogRelReturn( !pVM->hm.s.fNestedPaging
1754 || (pVM->hm.s.vmx.Msrs.ProcCtls2.n.allowed1 & VMX_PROC_CTLS2_EPT),
1755 VERR_HM_IPE_1);
1756 AssertLogRelReturn( !pVM->hm.s.vmx.fUnrestrictedGuest
1757 || ( (pVM->hm.s.vmx.Msrs.ProcCtls2.n.allowed1 & VMX_PROC_CTLS2_UNRESTRICTED_GUEST)
1758 && pVM->hm.s.fNestedPaging),
1759 VERR_HM_IPE_1);
1760
1761 /*
1762 * Enable VPID if configured and supported.
1763 */
1764 if (pVM->hm.s.vmx.Msrs.ProcCtls2.n.allowed1 & VMX_PROC_CTLS2_VPID)
1765 pVM->hm.s.vmx.fVpid = pVM->hm.s.vmx.fAllowVpid;
1766
1767#if 0
1768 /*
1769 * Enable APIC register virtualization and virtual-interrupt delivery if supported.
1770 */
1771 if ( (pVM->hm.s.vmx.Msrs.ProcCtls2.n.allowed1 & VMX_PROC_CTLS2_APIC_REG_VIRT)
1772 && (pVM->hm.s.vmx.Msrs.ProcCtls2.n.allowed1 & VMX_PROC_CTLS2_VIRT_INTR_DELIVERY))
1773 pVM->hm.s.fVirtApicRegs = true;
1774
1775 /*
1776 * Enable posted-interrupt processing if supported.
1777 */
1778 /** @todo Add and query IPRT API for host OS support for posted-interrupt IPI
1779 * here. */
1780 if ( (pVM->hm.s.vmx.Msrs.PinCtls.n.allowed1 & VMX_PIN_CTLS_POSTED_INT)
1781 && (pVM->hm.s.vmx.Msrs.ExitCtls.n.allowed1 & VMX_EXIT_CTLS_ACK_EXT_INT))
1782 pVM->hm.s.fPostedIntrs = true;
1783#endif
1784
1785 /*
1786 * Disallow RDTSCP in the guest if there is no secondary process-based VM execution controls as otherwise
1787 * RDTSCP would cause a #UD. There might be no CPUs out there where this happens, as RDTSCP was introduced
1788 * in Nehalems and secondary VM exec. controls should be supported in all of them, but nonetheless it's Intel...
1789 */
1790 if ( !(pVM->hm.s.vmx.Msrs.ProcCtls.n.allowed1 & VMX_PROC_CTLS_USE_SECONDARY_CTLS)
1791 && CPUMR3GetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_RDTSCP))
1792 {
1793 CPUMR3ClearGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_RDTSCP);
1794 LogRel(("HM: Disabled RDTSCP\n"));
1795 }
1796
1797 if (!pVM->hm.s.vmx.fUnrestrictedGuest)
1798 {
1799 /* Allocate three pages for the TSS we need for real mode emulation. (2 pages for the IO bitmap) */
1800 rc = PDMR3VmmDevHeapAlloc(pVM, HM_VTX_TOTAL_DEVHEAP_MEM, hmR3VmmDevHeapNotify, (RTR3PTR *)&pVM->hm.s.vmx.pRealModeTSS);
1801 if (RT_SUCCESS(rc))
1802 {
1803 /* The IO bitmap starts right after the virtual interrupt redirection bitmap.
1804 Refer Intel spec. 20.3.3 "Software Interrupt Handling in Virtual-8086 mode"
1805 esp. Figure 20-5.*/
1806 ASMMemZero32(pVM->hm.s.vmx.pRealModeTSS, sizeof(*pVM->hm.s.vmx.pRealModeTSS));
1807 pVM->hm.s.vmx.pRealModeTSS->offIoBitmap = sizeof(*pVM->hm.s.vmx.pRealModeTSS);
1808
1809 /* Bit set to 0 means software interrupts are redirected to the
1810 8086 program interrupt handler rather than switching to
1811 protected-mode handler. */
1812 memset(pVM->hm.s.vmx.pRealModeTSS->IntRedirBitmap, 0, sizeof(pVM->hm.s.vmx.pRealModeTSS->IntRedirBitmap));
1813
1814 /* Allow all port IO, so that port IO instructions do not cause
1815 exceptions and would instead cause a VM-exit (based on VT-x's
1816 IO bitmap which we currently configure to always cause an exit). */
1817 memset(pVM->hm.s.vmx.pRealModeTSS + 1, 0, PAGE_SIZE * 2);
1818 *((unsigned char *)pVM->hm.s.vmx.pRealModeTSS + HM_VTX_TSS_SIZE - 2) = 0xff;
1819
1820 /*
1821 * Construct a 1024 element page directory with 4 MB pages for the identity mapped
1822 * page table used in real and protected mode without paging with EPT.
1823 */
1824 pVM->hm.s.vmx.pNonPagingModeEPTPageTable = (PX86PD)((char *)pVM->hm.s.vmx.pRealModeTSS + PAGE_SIZE * 3);
1825 for (uint32_t i = 0; i < X86_PG_ENTRIES; i++)
1826 {
1827 pVM->hm.s.vmx.pNonPagingModeEPTPageTable->a[i].u = _4M * i;
1828 pVM->hm.s.vmx.pNonPagingModeEPTPageTable->a[i].u |= X86_PDE4M_P | X86_PDE4M_RW | X86_PDE4M_US
1829 | X86_PDE4M_A | X86_PDE4M_D | X86_PDE4M_PS
1830 | X86_PDE4M_G;
1831 }
1832
1833 /* We convert it here every time as PCI regions could be reconfigured. */
1834 if (PDMVmmDevHeapIsEnabled(pVM))
1835 {
1836 RTGCPHYS GCPhys;
1837 rc = PDMVmmDevHeapR3ToGCPhys(pVM, pVM->hm.s.vmx.pRealModeTSS, &GCPhys);
1838 AssertRCReturn(rc, rc);
1839 LogRel(("HM: Real Mode TSS guest physaddr = %#RGp\n", GCPhys));
1840
1841 rc = PDMVmmDevHeapR3ToGCPhys(pVM, pVM->hm.s.vmx.pNonPagingModeEPTPageTable, &GCPhys);
1842 AssertRCReturn(rc, rc);
1843 LogRel(("HM: Non-Paging Mode EPT CR3 = %#RGp\n", GCPhys));
1844 }
1845 }
1846 else
1847 {
1848 LogRel(("HM: No real mode VT-x support (PDMR3VMMDevHeapAlloc returned %Rrc)\n", rc));
1849 pVM->hm.s.vmx.pRealModeTSS = NULL;
1850 pVM->hm.s.vmx.pNonPagingModeEPTPageTable = NULL;
1851 return VMSetError(pVM, rc, RT_SRC_POS,
1852 "HM failure: No real mode VT-x support (PDMR3VMMDevHeapAlloc returned %Rrc)", rc);
1853 }
1854 }
1855
1856 LogRel((pVM->hm.s.fAllow64BitGuests
1857 ? "HM: Guest support: 32-bit and 64-bit\n"
1858 : "HM: Guest support: 32-bit only\n"));
1859
1860 /*
1861 * Call ring-0 to set up the VM.
1862 */
1863 rc = SUPR3CallVMMR0Ex(pVM->pVMR0, 0 /* idCpu */, VMMR0_DO_HM_SETUP_VM, 0 /* u64Arg */, NULL /* pReqHdr */);
1864 if (rc != VINF_SUCCESS)
1865 {
1866 LogRel(("HM: VMX setup failed with rc=%Rrc!\n", rc));
1867 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1868 {
1869 PVMCPU pVCpu = &pVM->aCpus[i];
1870 LogRel(("HM: CPU[%u] Last instruction error %#x\n", i, pVCpu->hm.s.vmx.LastError.u32InstrError));
1871 LogRel(("HM: CPU[%u] HM error %#x (%u)\n", i, pVCpu->hm.s.u32HMError, pVCpu->hm.s.u32HMError));
1872 }
1873 HMR3CheckError(pVM, rc);
1874 return VMSetError(pVM, rc, RT_SRC_POS, "VT-x setup failed: %Rrc", rc);
1875 }
1876
1877 LogRel(("HM: Supports VMCS EFER fields = %RTbool\n", pVM->hm.s.vmx.fSupportsVmcsEfer));
1878 LogRel(("HM: Enabled VMX\n"));
1879 pVM->hm.s.vmx.fEnabled = true;
1880
1881 hmR3DisableRawMode(pVM); /** @todo make this go away! */
1882
1883 /*
1884 * Change the CPU features.
1885 */
1886 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_SEP);
1887 if (pVM->hm.s.fAllow64BitGuests)
1888 {
1889 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_PAE);
1890 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_LONG_MODE);
1891 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_SYSCALL); /* 64 bits only on Intel CPUs */
1892 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_LAHF);
1893 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_NX);
1894 }
1895 /* Turn on NXE if PAE has been enabled *and* the host has turned on NXE
1896 (we reuse the host EFER in the switcher). */
1897 /** @todo this needs to be fixed properly!! */
1898 else if (CPUMR3GetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_PAE))
1899 {
1900 if (pVM->hm.s.vmx.u64HostEfer & MSR_K6_EFER_NXE)
1901 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_NX);
1902 else
1903 LogRel(("HM: NX not enabled on the host, unavailable to PAE guest\n"));
1904 }
1905
1906 /*
1907 * Log configuration details.
1908 */
1909 if (pVM->hm.s.fNestedPaging)
1910 {
1911 LogRel(("HM: Enabled nested paging\n"));
1912 if (pVM->hm.s.vmx.enmTlbFlushEpt == VMXTLBFLUSHEPT_SINGLE_CONTEXT)
1913 LogRel(("HM: EPT flush type = Single context\n"));
1914 else if (pVM->hm.s.vmx.enmTlbFlushEpt == VMXTLBFLUSHEPT_ALL_CONTEXTS)
1915 LogRel(("HM: EPT flush type = All contexts\n"));
1916 else if (pVM->hm.s.vmx.enmTlbFlushEpt == VMXTLBFLUSHEPT_NOT_SUPPORTED)
1917 LogRel(("HM: EPT flush type = Not supported\n"));
1918 else
1919 LogRel(("HM: EPT flush type = %#x\n", pVM->hm.s.vmx.enmTlbFlushEpt));
1920
1921 if (pVM->hm.s.vmx.fUnrestrictedGuest)
1922 LogRel(("HM: Enabled unrestricted guest execution\n"));
1923
1924#if HC_ARCH_BITS == 64
1925 if (pVM->hm.s.fLargePages)
1926 {
1927 /* Use large (2 MB) pages for our EPT PDEs where possible. */
1928 PGMSetLargePageUsage(pVM, true);
1929 LogRel(("HM: Enabled large page support\n"));
1930 }
1931#endif
1932 }
1933 else
1934 Assert(!pVM->hm.s.vmx.fUnrestrictedGuest);
1935
1936 if (pVM->hm.s.fVirtApicRegs)
1937 LogRel(("HM: Enabled APIC-register virtualization support\n"));
1938
1939 if (pVM->hm.s.fPostedIntrs)
1940 LogRel(("HM: Enabled posted-interrupt processing support\n"));
1941
1942 if (pVM->hm.s.vmx.fVpid)
1943 {
1944 LogRel(("HM: Enabled VPID\n"));
1945 if (pVM->hm.s.vmx.enmTlbFlushVpid == VMXTLBFLUSHVPID_INDIV_ADDR)
1946 LogRel(("HM: VPID flush type = Individual addresses\n"));
1947 else if (pVM->hm.s.vmx.enmTlbFlushVpid == VMXTLBFLUSHVPID_SINGLE_CONTEXT)
1948 LogRel(("HM: VPID flush type = Single context\n"));
1949 else if (pVM->hm.s.vmx.enmTlbFlushVpid == VMXTLBFLUSHVPID_ALL_CONTEXTS)
1950 LogRel(("HM: VPID flush type = All contexts\n"));
1951 else if (pVM->hm.s.vmx.enmTlbFlushVpid == VMXTLBFLUSHVPID_SINGLE_CONTEXT_RETAIN_GLOBALS)
1952 LogRel(("HM: VPID flush type = Single context retain globals\n"));
1953 else
1954 LogRel(("HM: VPID flush type = %#x\n", pVM->hm.s.vmx.enmTlbFlushVpid));
1955 }
1956 else if (pVM->hm.s.vmx.enmTlbFlushVpid == VMXTLBFLUSHVPID_NOT_SUPPORTED)
1957 LogRel(("HM: Ignoring VPID capabilities of CPU\n"));
1958
1959 if (pVM->hm.s.vmx.fUsePreemptTimer)
1960 LogRel(("HM: Enabled VMX-preemption timer (cPreemptTimerShift=%u)\n", pVM->hm.s.vmx.cPreemptTimerShift));
1961 else
1962 LogRel(("HM: Disabled VMX-preemption timer\n"));
1963
1964 return VINF_SUCCESS;
1965}
1966
1967
1968/**
1969 * Finish AMD-V initialization (after ring-0 init).
1970 *
1971 * @returns VBox status code.
1972 * @param pVM The cross context VM structure.
1973 */
1974static int hmR3InitFinalizeR0Amd(PVM pVM)
1975{
1976 Log(("pVM->hm.s.svm.fSupported = %d\n", pVM->hm.s.svm.fSupported));
1977
1978 LogRel(("HM: Using AMD-V implementation 2.0\n"));
1979
1980 uint32_t u32Family;
1981 uint32_t u32Model;
1982 uint32_t u32Stepping;
1983 if (HMSvmIsSubjectToErratum170(&u32Family, &u32Model, &u32Stepping))
1984 LogRel(("HM: AMD Cpu with erratum 170 family %#x model %#x stepping %#x\n", u32Family, u32Model, u32Stepping));
1985 LogRel(("HM: Max resume loops = %u\n", pVM->hm.s.cMaxResumeLoops));
1986 LogRel(("HM: AMD HWCR MSR = %#RX64\n", pVM->hm.s.svm.u64MsrHwcr));
1987 LogRel(("HM: AMD-V revision = %#x\n", pVM->hm.s.svm.u32Rev));
1988 LogRel(("HM: AMD-V max ASID = %RU32\n", pVM->hm.s.uMaxAsid));
1989 LogRel(("HM: AMD-V features = %#x\n", pVM->hm.s.svm.u32Features));
1990
1991 /*
1992 * Enumerate AMD-V features.
1993 */
1994 static const struct { uint32_t fFlag; const char *pszName; } s_aSvmFeatures[] =
1995 {
1996#define HMSVM_REPORT_FEATURE(a_StrDesc, a_Define) { a_Define, a_StrDesc }
1997 HMSVM_REPORT_FEATURE("NESTED_PAGING", X86_CPUID_SVM_FEATURE_EDX_NESTED_PAGING),
1998 HMSVM_REPORT_FEATURE("LBR_VIRT", X86_CPUID_SVM_FEATURE_EDX_LBR_VIRT),
1999 HMSVM_REPORT_FEATURE("SVM_LOCK", X86_CPUID_SVM_FEATURE_EDX_SVM_LOCK),
2000 HMSVM_REPORT_FEATURE("NRIP_SAVE", X86_CPUID_SVM_FEATURE_EDX_NRIP_SAVE),
2001 HMSVM_REPORT_FEATURE("TSC_RATE_MSR", X86_CPUID_SVM_FEATURE_EDX_TSC_RATE_MSR),
2002 HMSVM_REPORT_FEATURE("VMCB_CLEAN", X86_CPUID_SVM_FEATURE_EDX_VMCB_CLEAN),
2003 HMSVM_REPORT_FEATURE("FLUSH_BY_ASID", X86_CPUID_SVM_FEATURE_EDX_FLUSH_BY_ASID),
2004 HMSVM_REPORT_FEATURE("DECODE_ASSISTS", X86_CPUID_SVM_FEATURE_EDX_DECODE_ASSISTS),
2005 HMSVM_REPORT_FEATURE("PAUSE_FILTER", X86_CPUID_SVM_FEATURE_EDX_PAUSE_FILTER),
2006 HMSVM_REPORT_FEATURE("PAUSE_FILTER_THRESHOLD", X86_CPUID_SVM_FEATURE_EDX_PAUSE_FILTER_THRESHOLD),
2007 HMSVM_REPORT_FEATURE("AVIC", X86_CPUID_SVM_FEATURE_EDX_AVIC),
2008 HMSVM_REPORT_FEATURE("VIRT_VMSAVE_VMLOAD", X86_CPUID_SVM_FEATURE_EDX_VIRT_VMSAVE_VMLOAD),
2009 HMSVM_REPORT_FEATURE("VGIF", X86_CPUID_SVM_FEATURE_EDX_VGIF),
2010#undef HMSVM_REPORT_FEATURE
2011 };
2012
2013 uint32_t fSvmFeatures = pVM->hm.s.svm.u32Features;
2014 for (unsigned i = 0; i < RT_ELEMENTS(s_aSvmFeatures); i++)
2015 if (fSvmFeatures & s_aSvmFeatures[i].fFlag)
2016 {
2017 LogRel(("HM: %s\n", s_aSvmFeatures[i].pszName));
2018 fSvmFeatures &= ~s_aSvmFeatures[i].fFlag;
2019 }
2020 if (fSvmFeatures)
2021 for (unsigned iBit = 0; iBit < 32; iBit++)
2022 if (RT_BIT_32(iBit) & fSvmFeatures)
2023 LogRel(("HM: Reserved bit %u\n", iBit));
2024
2025 /*
2026 * Nested paging is determined in HMR3Init, verify the sanity of that.
2027 */
2028 AssertLogRelReturn( !pVM->hm.s.fNestedPaging
2029 || (pVM->hm.s.svm.u32Features & X86_CPUID_SVM_FEATURE_EDX_NESTED_PAGING),
2030 VERR_HM_IPE_1);
2031
2032#if 0
2033 /** @todo Add and query IPRT API for host OS support for posted-interrupt IPI
2034 * here. */
2035 if (RTR0IsPostIpiSupport())
2036 pVM->hm.s.fPostedIntrs = true;
2037#endif
2038
2039 /*
2040 * Call ring-0 to set up the VM.
2041 */
2042 int rc = SUPR3CallVMMR0Ex(pVM->pVMR0, 0 /*idCpu*/, VMMR0_DO_HM_SETUP_VM, 0, NULL);
2043 if (rc != VINF_SUCCESS)
2044 {
2045 AssertMsgFailed(("%Rrc\n", rc));
2046 LogRel(("HM: AMD-V setup failed with rc=%Rrc!\n", rc));
2047 return VMSetError(pVM, rc, RT_SRC_POS, "AMD-V setup failed: %Rrc", rc);
2048 }
2049
2050 LogRel(("HM: Enabled SVM\n"));
2051 pVM->hm.s.svm.fEnabled = true;
2052
2053 if (pVM->hm.s.fNestedPaging)
2054 {
2055 LogRel(("HM: Enabled nested paging\n"));
2056
2057 /*
2058 * Enable large pages (2 MB) if applicable.
2059 */
2060#if HC_ARCH_BITS == 64
2061 if (pVM->hm.s.fLargePages)
2062 {
2063 PGMSetLargePageUsage(pVM, true);
2064 LogRel(("HM: Enabled large page support\n"));
2065 }
2066#endif
2067 }
2068
2069 if (pVM->hm.s.fVirtApicRegs)
2070 LogRel(("HM: Enabled APIC-register virtualization support\n"));
2071
2072 if (pVM->hm.s.fPostedIntrs)
2073 LogRel(("HM: Enabled posted-interrupt processing support\n"));
2074
2075 hmR3DisableRawMode(pVM);
2076
2077 /*
2078 * Change the CPU features.
2079 */
2080 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_SEP);
2081 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_SYSCALL);
2082 if (pVM->hm.s.fAllow64BitGuests)
2083 {
2084 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_PAE);
2085 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_LONG_MODE);
2086 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_NX);
2087 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_LAHF);
2088 }
2089 /* Turn on NXE if PAE has been enabled. */
2090 else if (CPUMR3GetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_PAE))
2091 CPUMR3SetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_NX);
2092
2093 LogRel(("HM: %s TPR patching\n", (pVM->hm.s.fTprPatchingAllowed) ? "Enabled" : "Disabled"));
2094
2095 LogRel((pVM->hm.s.fAllow64BitGuests
2096 ? "HM: Guest support: 32-bit and 64-bit\n"
2097 : "HM: Guest support: 32-bit only\n"));
2098
2099 return VINF_SUCCESS;
2100}
2101
2102
2103/**
2104 * Applies relocations to data and code managed by this
2105 * component. This function will be called at init and
2106 * whenever the VMM need to relocate it self inside the GC.
2107 *
2108 * @param pVM The cross context VM structure.
2109 */
2110VMMR3_INT_DECL(void) HMR3Relocate(PVM pVM)
2111{
2112 Log(("HMR3Relocate to %RGv\n", MMHyperGetArea(pVM, 0)));
2113
2114 /* Fetch the current paging mode during the relocate callback during state loading. */
2115 if (VMR3GetState(pVM) == VMSTATE_LOADING)
2116 {
2117 for (VMCPUID i = 0; i < pVM->cCpus; i++)
2118 {
2119 PVMCPU pVCpu = &pVM->aCpus[i];
2120 pVCpu->hm.s.enmShadowMode = PGMGetShadowMode(pVCpu);
2121 }
2122 }
2123#if HC_ARCH_BITS == 32 && defined(VBOX_ENABLE_64_BITS_GUESTS)
2124 if (HMIsEnabled(pVM))
2125 {
2126 switch (PGMGetHostMode(pVM))
2127 {
2128 case PGMMODE_32_BIT:
2129 pVM->hm.s.pfnHost32ToGuest64R0 = VMMR3GetHostToGuestSwitcher(pVM, VMMSWITCHER_32_TO_AMD64);
2130 break;
2131
2132 case PGMMODE_PAE:
2133 case PGMMODE_PAE_NX:
2134 pVM->hm.s.pfnHost32ToGuest64R0 = VMMR3GetHostToGuestSwitcher(pVM, VMMSWITCHER_PAE_TO_AMD64);
2135 break;
2136
2137 default:
2138 AssertFailed();
2139 break;
2140 }
2141 }
2142#endif
2143 return;
2144}
2145
2146
2147/**
2148 * Terminates the HM.
2149 *
2150 * Termination means cleaning up and freeing all resources,
2151 * the VM itself is, at this point, powered off or suspended.
2152 *
2153 * @returns VBox status code.
2154 * @param pVM The cross context VM structure.
2155 */
2156VMMR3_INT_DECL(int) HMR3Term(PVM pVM)
2157{
2158 if (pVM->hm.s.vmx.pRealModeTSS)
2159 {
2160 PDMR3VmmDevHeapFree(pVM, pVM->hm.s.vmx.pRealModeTSS);
2161 pVM->hm.s.vmx.pRealModeTSS = 0;
2162 }
2163 hmR3TermCPU(pVM);
2164 return 0;
2165}
2166
2167
2168/**
2169 * Terminates the per-VCPU HM.
2170 *
2171 * @returns VBox status code.
2172 * @param pVM The cross context VM structure.
2173 */
2174static int hmR3TermCPU(PVM pVM)
2175{
2176 for (VMCPUID i = 0; i < pVM->cCpus; i++)
2177 {
2178 PVMCPU pVCpu = &pVM->aCpus[i]; NOREF(pVCpu);
2179
2180#ifdef VBOX_WITH_STATISTICS
2181 if (pVCpu->hm.s.paStatExitReason)
2182 {
2183 MMHyperFree(pVM, pVCpu->hm.s.paStatExitReason);
2184 pVCpu->hm.s.paStatExitReason = NULL;
2185 pVCpu->hm.s.paStatExitReasonR0 = NIL_RTR0PTR;
2186 }
2187 if (pVCpu->hm.s.paStatInjectedIrqs)
2188 {
2189 MMHyperFree(pVM, pVCpu->hm.s.paStatInjectedIrqs);
2190 pVCpu->hm.s.paStatInjectedIrqs = NULL;
2191 pVCpu->hm.s.paStatInjectedIrqsR0 = NIL_RTR0PTR;
2192 }
2193#endif
2194
2195#ifdef VBOX_WITH_CRASHDUMP_MAGIC
2196 memset(pVCpu->hm.s.vmx.VMCSCache.aMagic, 0, sizeof(pVCpu->hm.s.vmx.VMCSCache.aMagic));
2197 pVCpu->hm.s.vmx.VMCSCache.uMagic = 0;
2198 pVCpu->hm.s.vmx.VMCSCache.uPos = 0xffffffff;
2199#endif
2200 }
2201 return 0;
2202}
2203
2204
2205/**
2206 * Resets a virtual CPU.
2207 *
2208 * Used by HMR3Reset and CPU hot plugging.
2209 *
2210 * @param pVCpu The cross context virtual CPU structure to reset.
2211 */
2212VMMR3_INT_DECL(void) HMR3ResetCpu(PVMCPU pVCpu)
2213{
2214 /* Sync. entire state on VM reset R0-reentry. It's safe to reset
2215 the HM flags here, all other EMTs are in ring-3. See VMR3Reset(). */
2216 pVCpu->hm.s.fCtxChanged |= HM_CHANGED_HOST_CONTEXT | HM_CHANGED_ALL_GUEST;
2217
2218 pVCpu->hm.s.fActive = false;
2219 pVCpu->hm.s.Event.fPending = false;
2220 pVCpu->hm.s.vmx.fWasInRealMode = true;
2221 pVCpu->hm.s.vmx.u64MsrApicBase = 0;
2222 pVCpu->hm.s.vmx.fSwitchedTo64on32 = false;
2223
2224 /* Reset the contents of the read cache. */
2225 PVMCSCACHE pCache = &pVCpu->hm.s.vmx.VMCSCache;
2226 for (unsigned j = 0; j < pCache->Read.cValidEntries; j++)
2227 pCache->Read.aFieldVal[j] = 0;
2228
2229#ifdef VBOX_WITH_CRASHDUMP_MAGIC
2230 /* Magic marker for searching in crash dumps. */
2231 strcpy((char *)pCache->aMagic, "VMCSCACHE Magic");
2232 pCache->uMagic = UINT64_C(0xdeadbeefdeadbeef);
2233#endif
2234}
2235
2236
2237/**
2238 * The VM is being reset.
2239 *
2240 * For the HM component this means that any GDT/LDT/TSS monitors
2241 * needs to be removed.
2242 *
2243 * @param pVM The cross context VM structure.
2244 */
2245VMMR3_INT_DECL(void) HMR3Reset(PVM pVM)
2246{
2247 LogFlow(("HMR3Reset:\n"));
2248
2249 if (HMIsEnabled(pVM))
2250 hmR3DisableRawMode(pVM);
2251
2252 for (VMCPUID i = 0; i < pVM->cCpus; i++)
2253 {
2254 PVMCPU pVCpu = &pVM->aCpus[i];
2255
2256 HMR3ResetCpu(pVCpu);
2257 }
2258
2259 /* Clear all patch information. */
2260 pVM->hm.s.pGuestPatchMem = 0;
2261 pVM->hm.s.pFreeGuestPatchMem = 0;
2262 pVM->hm.s.cbGuestPatchMem = 0;
2263 pVM->hm.s.cPatches = 0;
2264 pVM->hm.s.PatchTree = 0;
2265 pVM->hm.s.fTPRPatchingActive = false;
2266 ASMMemZero32(pVM->hm.s.aPatches, sizeof(pVM->hm.s.aPatches));
2267}
2268
2269
2270/**
2271 * Callback to patch a TPR instruction (vmmcall or mov cr8).
2272 *
2273 * @returns VBox strict status code.
2274 * @param pVM The cross context VM structure.
2275 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2276 * @param pvUser Unused.
2277 */
2278static DECLCALLBACK(VBOXSTRICTRC) hmR3RemovePatches(PVM pVM, PVMCPU pVCpu, void *pvUser)
2279{
2280 VMCPUID idCpu = (VMCPUID)(uintptr_t)pvUser;
2281
2282 /* Only execute the handler on the VCPU the original patch request was issued. */
2283 if (pVCpu->idCpu != idCpu)
2284 return VINF_SUCCESS;
2285
2286 Log(("hmR3RemovePatches\n"));
2287 for (unsigned i = 0; i < pVM->hm.s.cPatches; i++)
2288 {
2289 uint8_t abInstr[15];
2290 PHMTPRPATCH pPatch = &pVM->hm.s.aPatches[i];
2291 RTGCPTR pInstrGC = (RTGCPTR)pPatch->Core.Key;
2292 int rc;
2293
2294#ifdef LOG_ENABLED
2295 char szOutput[256];
2296 rc = DBGFR3DisasInstrEx(pVM->pUVM, pVCpu->idCpu, CPUMGetGuestCS(pVCpu), pInstrGC, DBGF_DISAS_FLAGS_DEFAULT_MODE,
2297 szOutput, sizeof(szOutput), NULL);
2298 if (RT_SUCCESS(rc))
2299 Log(("Patched instr: %s\n", szOutput));
2300#endif
2301
2302 /* Check if the instruction is still the same. */
2303 rc = PGMPhysSimpleReadGCPtr(pVCpu, abInstr, pInstrGC, pPatch->cbNewOp);
2304 if (rc != VINF_SUCCESS)
2305 {
2306 Log(("Patched code removed? (rc=%Rrc0\n", rc));
2307 continue; /* swapped out or otherwise removed; skip it. */
2308 }
2309
2310 if (memcmp(abInstr, pPatch->aNewOpcode, pPatch->cbNewOp))
2311 {
2312 Log(("Patched instruction was changed! (rc=%Rrc0\n", rc));
2313 continue; /* skip it. */
2314 }
2315
2316 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pInstrGC, pPatch->aOpcode, pPatch->cbOp);
2317 AssertRC(rc);
2318
2319#ifdef LOG_ENABLED
2320 rc = DBGFR3DisasInstrEx(pVM->pUVM, pVCpu->idCpu, CPUMGetGuestCS(pVCpu), pInstrGC, DBGF_DISAS_FLAGS_DEFAULT_MODE,
2321 szOutput, sizeof(szOutput), NULL);
2322 if (RT_SUCCESS(rc))
2323 Log(("Original instr: %s\n", szOutput));
2324#endif
2325 }
2326 pVM->hm.s.cPatches = 0;
2327 pVM->hm.s.PatchTree = 0;
2328 pVM->hm.s.pFreeGuestPatchMem = pVM->hm.s.pGuestPatchMem;
2329 pVM->hm.s.fTPRPatchingActive = false;
2330 return VINF_SUCCESS;
2331}
2332
2333
2334/**
2335 * Worker for enabling patching in a VT-x/AMD-V guest.
2336 *
2337 * @returns VBox status code.
2338 * @param pVM The cross context VM structure.
2339 * @param idCpu VCPU to execute hmR3RemovePatches on.
2340 * @param pPatchMem Patch memory range.
2341 * @param cbPatchMem Size of the memory range.
2342 */
2343static int hmR3EnablePatching(PVM pVM, VMCPUID idCpu, RTRCPTR pPatchMem, unsigned cbPatchMem)
2344{
2345 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE, hmR3RemovePatches, (void *)(uintptr_t)idCpu);
2346 AssertRC(rc);
2347
2348 pVM->hm.s.pGuestPatchMem = pPatchMem;
2349 pVM->hm.s.pFreeGuestPatchMem = pPatchMem;
2350 pVM->hm.s.cbGuestPatchMem = cbPatchMem;
2351 return VINF_SUCCESS;
2352}
2353
2354
2355/**
2356 * Enable patching in a VT-x/AMD-V guest
2357 *
2358 * @returns VBox status code.
2359 * @param pVM The cross context VM structure.
2360 * @param pPatchMem Patch memory range.
2361 * @param cbPatchMem Size of the memory range.
2362 */
2363VMMR3_INT_DECL(int) HMR3EnablePatching(PVM pVM, RTGCPTR pPatchMem, unsigned cbPatchMem)
2364{
2365 VM_ASSERT_EMT(pVM);
2366 Log(("HMR3EnablePatching %RGv size %x\n", pPatchMem, cbPatchMem));
2367 if (pVM->cCpus > 1)
2368 {
2369 /* We own the IOM lock here and could cause a deadlock by waiting for a VCPU that is blocking on the IOM lock. */
2370 int rc = VMR3ReqCallNoWait(pVM, VMCPUID_ANY_QUEUE,
2371 (PFNRT)hmR3EnablePatching, 4, pVM, VMMGetCpuId(pVM), (RTRCPTR)pPatchMem, cbPatchMem);
2372 AssertRC(rc);
2373 return rc;
2374 }
2375 return hmR3EnablePatching(pVM, VMMGetCpuId(pVM), (RTRCPTR)pPatchMem, cbPatchMem);
2376}
2377
2378
2379/**
2380 * Disable patching in a VT-x/AMD-V guest.
2381 *
2382 * @returns VBox status code.
2383 * @param pVM The cross context VM structure.
2384 * @param pPatchMem Patch memory range.
2385 * @param cbPatchMem Size of the memory range.
2386 */
2387VMMR3_INT_DECL(int) HMR3DisablePatching(PVM pVM, RTGCPTR pPatchMem, unsigned cbPatchMem)
2388{
2389 Log(("HMR3DisablePatching %RGv size %x\n", pPatchMem, cbPatchMem));
2390 RT_NOREF2(pPatchMem, cbPatchMem);
2391
2392 Assert(pVM->hm.s.pGuestPatchMem == pPatchMem);
2393 Assert(pVM->hm.s.cbGuestPatchMem == cbPatchMem);
2394
2395 /** @todo Potential deadlock when other VCPUs are waiting on the IOM lock (we own it)!! */
2396 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE, hmR3RemovePatches,
2397 (void *)(uintptr_t)VMMGetCpuId(pVM));
2398 AssertRC(rc);
2399
2400 pVM->hm.s.pGuestPatchMem = 0;
2401 pVM->hm.s.pFreeGuestPatchMem = 0;
2402 pVM->hm.s.cbGuestPatchMem = 0;
2403 pVM->hm.s.fTPRPatchingActive = false;
2404 return VINF_SUCCESS;
2405}
2406
2407
2408/**
2409 * Callback to patch a TPR instruction (vmmcall or mov cr8).
2410 *
2411 * @returns VBox strict status code.
2412 * @param pVM The cross context VM structure.
2413 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2414 * @param pvUser User specified CPU context.
2415 *
2416 */
2417static DECLCALLBACK(VBOXSTRICTRC) hmR3ReplaceTprInstr(PVM pVM, PVMCPU pVCpu, void *pvUser)
2418{
2419 /*
2420 * Only execute the handler on the VCPU the original patch request was
2421 * issued. (The other CPU(s) might not yet have switched to protected
2422 * mode, nor have the correct memory context.)
2423 */
2424 VMCPUID idCpu = (VMCPUID)(uintptr_t)pvUser;
2425 if (pVCpu->idCpu != idCpu)
2426 return VINF_SUCCESS;
2427
2428 /*
2429 * We're racing other VCPUs here, so don't try patch the instruction twice
2430 * and make sure there is still room for our patch record.
2431 */
2432 PCPUMCTX pCtx = &pVCpu->cpum.GstCtx;
2433 PHMTPRPATCH pPatch = (PHMTPRPATCH)RTAvloU32Get(&pVM->hm.s.PatchTree, (AVLOU32KEY)pCtx->eip);
2434 if (pPatch)
2435 {
2436 Log(("hmR3ReplaceTprInstr: already patched %RGv\n", pCtx->rip));
2437 return VINF_SUCCESS;
2438 }
2439 uint32_t const idx = pVM->hm.s.cPatches;
2440 if (idx >= RT_ELEMENTS(pVM->hm.s.aPatches))
2441 {
2442 Log(("hmR3ReplaceTprInstr: no available patch slots (%RGv)\n", pCtx->rip));
2443 return VINF_SUCCESS;
2444 }
2445 pPatch = &pVM->hm.s.aPatches[idx];
2446
2447 Log(("hmR3ReplaceTprInstr: rip=%RGv idxPatch=%u\n", pCtx->rip, idx));
2448
2449 /*
2450 * Disassembler the instruction and get cracking.
2451 */
2452 DBGFR3_DISAS_INSTR_CUR_LOG(pVCpu, "hmR3ReplaceTprInstr");
2453 PDISCPUSTATE pDis = &pVCpu->hm.s.DisState;
2454 uint32_t cbOp;
2455 int rc = EMInterpretDisasCurrent(pVM, pVCpu, pDis, &cbOp);
2456 AssertRC(rc);
2457 if ( rc == VINF_SUCCESS
2458 && pDis->pCurInstr->uOpcode == OP_MOV
2459 && cbOp >= 3)
2460 {
2461 static uint8_t const s_abVMMCall[3] = { 0x0f, 0x01, 0xd9 };
2462
2463 rc = PGMPhysSimpleReadGCPtr(pVCpu, pPatch->aOpcode, pCtx->rip, cbOp);
2464 AssertRC(rc);
2465
2466 pPatch->cbOp = cbOp;
2467
2468 if (pDis->Param1.fUse == DISUSE_DISPLACEMENT32)
2469 {
2470 /* write. */
2471 if (pDis->Param2.fUse == DISUSE_REG_GEN32)
2472 {
2473 pPatch->enmType = HMTPRINSTR_WRITE_REG;
2474 pPatch->uSrcOperand = pDis->Param2.Base.idxGenReg;
2475 Log(("hmR3ReplaceTprInstr: HMTPRINSTR_WRITE_REG %u\n", pDis->Param2.Base.idxGenReg));
2476 }
2477 else
2478 {
2479 Assert(pDis->Param2.fUse == DISUSE_IMMEDIATE32);
2480 pPatch->enmType = HMTPRINSTR_WRITE_IMM;
2481 pPatch->uSrcOperand = pDis->Param2.uValue;
2482 Log(("hmR3ReplaceTprInstr: HMTPRINSTR_WRITE_IMM %#llx\n", pDis->Param2.uValue));
2483 }
2484 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pCtx->rip, s_abVMMCall, sizeof(s_abVMMCall));
2485 AssertRC(rc);
2486
2487 memcpy(pPatch->aNewOpcode, s_abVMMCall, sizeof(s_abVMMCall));
2488 pPatch->cbNewOp = sizeof(s_abVMMCall);
2489 STAM_COUNTER_INC(&pVM->hm.s.StatTprReplaceSuccessVmc);
2490 }
2491 else
2492 {
2493 /*
2494 * TPR Read.
2495 *
2496 * Found:
2497 * mov eax, dword [fffe0080] (5 bytes)
2498 * Check if next instruction is:
2499 * shr eax, 4
2500 */
2501 Assert(pDis->Param1.fUse == DISUSE_REG_GEN32);
2502
2503 uint8_t const idxMmioReg = pDis->Param1.Base.idxGenReg;
2504 uint8_t const cbOpMmio = cbOp;
2505 uint64_t const uSavedRip = pCtx->rip;
2506
2507 pCtx->rip += cbOp;
2508 rc = EMInterpretDisasCurrent(pVM, pVCpu, pDis, &cbOp);
2509 DBGFR3_DISAS_INSTR_CUR_LOG(pVCpu, "Following read");
2510 pCtx->rip = uSavedRip;
2511
2512 if ( rc == VINF_SUCCESS
2513 && pDis->pCurInstr->uOpcode == OP_SHR
2514 && pDis->Param1.fUse == DISUSE_REG_GEN32
2515 && pDis->Param1.Base.idxGenReg == idxMmioReg
2516 && pDis->Param2.fUse == DISUSE_IMMEDIATE8
2517 && pDis->Param2.uValue == 4
2518 && cbOpMmio + cbOp < sizeof(pVM->hm.s.aPatches[idx].aOpcode))
2519 {
2520 uint8_t abInstr[15];
2521
2522 /* Replacing the two instructions above with an AMD-V specific lock-prefixed 32-bit MOV CR8 instruction so as to
2523 access CR8 in 32-bit mode and not cause a #VMEXIT. */
2524 rc = PGMPhysSimpleReadGCPtr(pVCpu, &pPatch->aOpcode, pCtx->rip, cbOpMmio + cbOp);
2525 AssertRC(rc);
2526
2527 pPatch->cbOp = cbOpMmio + cbOp;
2528
2529 /* 0xf0, 0x0f, 0x20, 0xc0 = mov eax, cr8 */
2530 abInstr[0] = 0xf0;
2531 abInstr[1] = 0x0f;
2532 abInstr[2] = 0x20;
2533 abInstr[3] = 0xc0 | pDis->Param1.Base.idxGenReg;
2534 for (unsigned i = 4; i < pPatch->cbOp; i++)
2535 abInstr[i] = 0x90; /* nop */
2536
2537 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pCtx->rip, abInstr, pPatch->cbOp);
2538 AssertRC(rc);
2539
2540 memcpy(pPatch->aNewOpcode, abInstr, pPatch->cbOp);
2541 pPatch->cbNewOp = pPatch->cbOp;
2542 STAM_COUNTER_INC(&pVM->hm.s.StatTprReplaceSuccessCr8);
2543
2544 Log(("Acceptable read/shr candidate!\n"));
2545 pPatch->enmType = HMTPRINSTR_READ_SHR4;
2546 }
2547 else
2548 {
2549 pPatch->enmType = HMTPRINSTR_READ;
2550 pPatch->uDstOperand = idxMmioReg;
2551
2552 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pCtx->rip, s_abVMMCall, sizeof(s_abVMMCall));
2553 AssertRC(rc);
2554
2555 memcpy(pPatch->aNewOpcode, s_abVMMCall, sizeof(s_abVMMCall));
2556 pPatch->cbNewOp = sizeof(s_abVMMCall);
2557 STAM_COUNTER_INC(&pVM->hm.s.StatTprReplaceSuccessVmc);
2558 Log(("hmR3ReplaceTprInstr: HMTPRINSTR_READ %u\n", pPatch->uDstOperand));
2559 }
2560 }
2561
2562 pPatch->Core.Key = pCtx->eip;
2563 rc = RTAvloU32Insert(&pVM->hm.s.PatchTree, &pPatch->Core);
2564 AssertRC(rc);
2565
2566 pVM->hm.s.cPatches++;
2567 return VINF_SUCCESS;
2568 }
2569
2570 /*
2571 * Save invalid patch, so we will not try again.
2572 */
2573 Log(("hmR3ReplaceTprInstr: Failed to patch instr!\n"));
2574 pPatch->Core.Key = pCtx->eip;
2575 pPatch->enmType = HMTPRINSTR_INVALID;
2576 rc = RTAvloU32Insert(&pVM->hm.s.PatchTree, &pPatch->Core);
2577 AssertRC(rc);
2578 pVM->hm.s.cPatches++;
2579 STAM_COUNTER_INC(&pVM->hm.s.StatTprReplaceFailure);
2580 return VINF_SUCCESS;
2581}
2582
2583
2584/**
2585 * Callback to patch a TPR instruction (jump to generated code).
2586 *
2587 * @returns VBox strict status code.
2588 * @param pVM The cross context VM structure.
2589 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2590 * @param pvUser User specified CPU context.
2591 *
2592 */
2593static DECLCALLBACK(VBOXSTRICTRC) hmR3PatchTprInstr(PVM pVM, PVMCPU pVCpu, void *pvUser)
2594{
2595 /*
2596 * Only execute the handler on the VCPU the original patch request was
2597 * issued. (The other CPU(s) might not yet have switched to protected
2598 * mode, nor have the correct memory context.)
2599 */
2600 VMCPUID idCpu = (VMCPUID)(uintptr_t)pvUser;
2601 if (pVCpu->idCpu != idCpu)
2602 return VINF_SUCCESS;
2603
2604 /*
2605 * We're racing other VCPUs here, so don't try patch the instruction twice
2606 * and make sure there is still room for our patch record.
2607 */
2608 PCPUMCTX pCtx = &pVCpu->cpum.GstCtx;
2609 PHMTPRPATCH pPatch = (PHMTPRPATCH)RTAvloU32Get(&pVM->hm.s.PatchTree, (AVLOU32KEY)pCtx->eip);
2610 if (pPatch)
2611 {
2612 Log(("hmR3PatchTprInstr: already patched %RGv\n", pCtx->rip));
2613 return VINF_SUCCESS;
2614 }
2615 uint32_t const idx = pVM->hm.s.cPatches;
2616 if (idx >= RT_ELEMENTS(pVM->hm.s.aPatches))
2617 {
2618 Log(("hmR3PatchTprInstr: no available patch slots (%RGv)\n", pCtx->rip));
2619 return VINF_SUCCESS;
2620 }
2621 pPatch = &pVM->hm.s.aPatches[idx];
2622
2623 Log(("hmR3PatchTprInstr: rip=%RGv idxPatch=%u\n", pCtx->rip, idx));
2624 DBGFR3_DISAS_INSTR_CUR_LOG(pVCpu, "hmR3PatchTprInstr");
2625
2626 /*
2627 * Disassemble the instruction and get cracking.
2628 */
2629 PDISCPUSTATE pDis = &pVCpu->hm.s.DisState;
2630 uint32_t cbOp;
2631 int rc = EMInterpretDisasCurrent(pVM, pVCpu, pDis, &cbOp);
2632 AssertRC(rc);
2633 if ( rc == VINF_SUCCESS
2634 && pDis->pCurInstr->uOpcode == OP_MOV
2635 && cbOp >= 5)
2636 {
2637 uint8_t aPatch[64];
2638 uint32_t off = 0;
2639
2640 rc = PGMPhysSimpleReadGCPtr(pVCpu, pPatch->aOpcode, pCtx->rip, cbOp);
2641 AssertRC(rc);
2642
2643 pPatch->cbOp = cbOp;
2644 pPatch->enmType = HMTPRINSTR_JUMP_REPLACEMENT;
2645
2646 if (pDis->Param1.fUse == DISUSE_DISPLACEMENT32)
2647 {
2648 /*
2649 * TPR write:
2650 *
2651 * push ECX [51]
2652 * push EDX [52]
2653 * push EAX [50]
2654 * xor EDX,EDX [31 D2]
2655 * mov EAX,EAX [89 C0]
2656 * or
2657 * mov EAX,0000000CCh [B8 CC 00 00 00]
2658 * mov ECX,0C0000082h [B9 82 00 00 C0]
2659 * wrmsr [0F 30]
2660 * pop EAX [58]
2661 * pop EDX [5A]
2662 * pop ECX [59]
2663 * jmp return_address [E9 return_address]
2664 */
2665 bool fUsesEax = (pDis->Param2.fUse == DISUSE_REG_GEN32 && pDis->Param2.Base.idxGenReg == DISGREG_EAX);
2666
2667 aPatch[off++] = 0x51; /* push ecx */
2668 aPatch[off++] = 0x52; /* push edx */
2669 if (!fUsesEax)
2670 aPatch[off++] = 0x50; /* push eax */
2671 aPatch[off++] = 0x31; /* xor edx, edx */
2672 aPatch[off++] = 0xd2;
2673 if (pDis->Param2.fUse == DISUSE_REG_GEN32)
2674 {
2675 if (!fUsesEax)
2676 {
2677 aPatch[off++] = 0x89; /* mov eax, src_reg */
2678 aPatch[off++] = MAKE_MODRM(3, pDis->Param2.Base.idxGenReg, DISGREG_EAX);
2679 }
2680 }
2681 else
2682 {
2683 Assert(pDis->Param2.fUse == DISUSE_IMMEDIATE32);
2684 aPatch[off++] = 0xb8; /* mov eax, immediate */
2685 *(uint32_t *)&aPatch[off] = pDis->Param2.uValue;
2686 off += sizeof(uint32_t);
2687 }
2688 aPatch[off++] = 0xb9; /* mov ecx, 0xc0000082 */
2689 *(uint32_t *)&aPatch[off] = MSR_K8_LSTAR;
2690 off += sizeof(uint32_t);
2691
2692 aPatch[off++] = 0x0f; /* wrmsr */
2693 aPatch[off++] = 0x30;
2694 if (!fUsesEax)
2695 aPatch[off++] = 0x58; /* pop eax */
2696 aPatch[off++] = 0x5a; /* pop edx */
2697 aPatch[off++] = 0x59; /* pop ecx */
2698 }
2699 else
2700 {
2701 /*
2702 * TPR read:
2703 *
2704 * push ECX [51]
2705 * push EDX [52]
2706 * push EAX [50]
2707 * mov ECX,0C0000082h [B9 82 00 00 C0]
2708 * rdmsr [0F 32]
2709 * mov EAX,EAX [89 C0]
2710 * pop EAX [58]
2711 * pop EDX [5A]
2712 * pop ECX [59]
2713 * jmp return_address [E9 return_address]
2714 */
2715 Assert(pDis->Param1.fUse == DISUSE_REG_GEN32);
2716
2717 if (pDis->Param1.Base.idxGenReg != DISGREG_ECX)
2718 aPatch[off++] = 0x51; /* push ecx */
2719 if (pDis->Param1.Base.idxGenReg != DISGREG_EDX )
2720 aPatch[off++] = 0x52; /* push edx */
2721 if (pDis->Param1.Base.idxGenReg != DISGREG_EAX)
2722 aPatch[off++] = 0x50; /* push eax */
2723
2724 aPatch[off++] = 0x31; /* xor edx, edx */
2725 aPatch[off++] = 0xd2;
2726
2727 aPatch[off++] = 0xb9; /* mov ecx, 0xc0000082 */
2728 *(uint32_t *)&aPatch[off] = MSR_K8_LSTAR;
2729 off += sizeof(uint32_t);
2730
2731 aPatch[off++] = 0x0f; /* rdmsr */
2732 aPatch[off++] = 0x32;
2733
2734 if (pDis->Param1.Base.idxGenReg != DISGREG_EAX)
2735 {
2736 aPatch[off++] = 0x89; /* mov dst_reg, eax */
2737 aPatch[off++] = MAKE_MODRM(3, DISGREG_EAX, pDis->Param1.Base.idxGenReg);
2738 }
2739
2740 if (pDis->Param1.Base.idxGenReg != DISGREG_EAX)
2741 aPatch[off++] = 0x58; /* pop eax */
2742 if (pDis->Param1.Base.idxGenReg != DISGREG_EDX )
2743 aPatch[off++] = 0x5a; /* pop edx */
2744 if (pDis->Param1.Base.idxGenReg != DISGREG_ECX)
2745 aPatch[off++] = 0x59; /* pop ecx */
2746 }
2747 aPatch[off++] = 0xe9; /* jmp return_address */
2748 *(RTRCUINTPTR *)&aPatch[off] = ((RTRCUINTPTR)pCtx->eip + cbOp) - ((RTRCUINTPTR)pVM->hm.s.pFreeGuestPatchMem + off + 4);
2749 off += sizeof(RTRCUINTPTR);
2750
2751 if (pVM->hm.s.pFreeGuestPatchMem + off <= pVM->hm.s.pGuestPatchMem + pVM->hm.s.cbGuestPatchMem)
2752 {
2753 /* Write new code to the patch buffer. */
2754 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pVM->hm.s.pFreeGuestPatchMem, aPatch, off);
2755 AssertRC(rc);
2756
2757#ifdef LOG_ENABLED
2758 uint32_t cbCurInstr;
2759 for (RTGCPTR GCPtrInstr = pVM->hm.s.pFreeGuestPatchMem;
2760 GCPtrInstr < pVM->hm.s.pFreeGuestPatchMem + off;
2761 GCPtrInstr += RT_MAX(cbCurInstr, 1))
2762 {
2763 char szOutput[256];
2764 rc = DBGFR3DisasInstrEx(pVM->pUVM, pVCpu->idCpu, pCtx->cs.Sel, GCPtrInstr, DBGF_DISAS_FLAGS_DEFAULT_MODE,
2765 szOutput, sizeof(szOutput), &cbCurInstr);
2766 if (RT_SUCCESS(rc))
2767 Log(("Patch instr %s\n", szOutput));
2768 else
2769 Log(("%RGv: rc=%Rrc\n", GCPtrInstr, rc));
2770 }
2771#endif
2772
2773 pPatch->aNewOpcode[0] = 0xE9;
2774 *(RTRCUINTPTR *)&pPatch->aNewOpcode[1] = ((RTRCUINTPTR)pVM->hm.s.pFreeGuestPatchMem) - ((RTRCUINTPTR)pCtx->eip + 5);
2775
2776 /* Overwrite the TPR instruction with a jump. */
2777 rc = PGMPhysSimpleWriteGCPtr(pVCpu, pCtx->eip, pPatch->aNewOpcode, 5);
2778 AssertRC(rc);
2779
2780 DBGFR3_DISAS_INSTR_CUR_LOG(pVCpu, "Jump");
2781
2782 pVM->hm.s.pFreeGuestPatchMem += off;
2783 pPatch->cbNewOp = 5;
2784
2785 pPatch->Core.Key = pCtx->eip;
2786 rc = RTAvloU32Insert(&pVM->hm.s.PatchTree, &pPatch->Core);
2787 AssertRC(rc);
2788
2789 pVM->hm.s.cPatches++;
2790 pVM->hm.s.fTPRPatchingActive = true;
2791 STAM_COUNTER_INC(&pVM->hm.s.StatTprPatchSuccess);
2792 return VINF_SUCCESS;
2793 }
2794
2795 Log(("Ran out of space in our patch buffer!\n"));
2796 }
2797 else
2798 Log(("hmR3PatchTprInstr: Failed to patch instr!\n"));
2799
2800
2801 /*
2802 * Save invalid patch, so we will not try again.
2803 */
2804 pPatch = &pVM->hm.s.aPatches[idx];
2805 pPatch->Core.Key = pCtx->eip;
2806 pPatch->enmType = HMTPRINSTR_INVALID;
2807 rc = RTAvloU32Insert(&pVM->hm.s.PatchTree, &pPatch->Core);
2808 AssertRC(rc);
2809 pVM->hm.s.cPatches++;
2810 STAM_COUNTER_INC(&pVM->hm.s.StatTprPatchFailure);
2811 return VINF_SUCCESS;
2812}
2813
2814
2815/**
2816 * Attempt to patch TPR mmio instructions.
2817 *
2818 * @returns VBox status code.
2819 * @param pVM The cross context VM structure.
2820 * @param pVCpu The cross context virtual CPU structure.
2821 */
2822VMMR3_INT_DECL(int) HMR3PatchTprInstr(PVM pVM, PVMCPU pVCpu)
2823{
2824 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE,
2825 pVM->hm.s.pGuestPatchMem ? hmR3PatchTprInstr : hmR3ReplaceTprInstr,
2826 (void *)(uintptr_t)pVCpu->idCpu);
2827 AssertRC(rc);
2828 return rc;
2829}
2830
2831
2832/**
2833 * Checks if we need to reschedule due to VMM device heap changes.
2834 *
2835 * @returns true if a reschedule is required, otherwise false.
2836 * @param pVM The cross context VM structure.
2837 * @param pCtx VM execution context.
2838 */
2839VMMR3_INT_DECL(bool) HMR3IsRescheduleRequired(PVM pVM, PCPUMCTX pCtx)
2840{
2841 /*
2842 * The VMM device heap is a requirement for emulating real-mode or protected-mode without paging
2843 * when the unrestricted guest execution feature is missing (VT-x only).
2844 */
2845 if ( pVM->hm.s.vmx.fEnabled
2846 && !pVM->hm.s.vmx.fUnrestrictedGuest
2847 && CPUMIsGuestInRealModeEx(pCtx)
2848 && !PDMVmmDevHeapIsEnabled(pVM))
2849 return true;
2850
2851 return false;
2852}
2853
2854
2855/**
2856 * Noticiation callback from DBGF when interrupt breakpoints or generic debug
2857 * event settings changes.
2858 *
2859 * DBGF will call HMR3NotifyDebugEventChangedPerCpu on each CPU afterwards, this
2860 * function is just updating the VM globals.
2861 *
2862 * @param pVM The VM cross context VM structure.
2863 * @thread EMT(0)
2864 */
2865VMMR3_INT_DECL(void) HMR3NotifyDebugEventChanged(PVM pVM)
2866{
2867 /* Interrupts. */
2868 bool fUseDebugLoop = pVM->dbgf.ro.cSoftIntBreakpoints > 0
2869 || pVM->dbgf.ro.cHardIntBreakpoints > 0;
2870
2871 /* CPU Exceptions. */
2872 for (DBGFEVENTTYPE enmEvent = DBGFEVENT_XCPT_FIRST;
2873 !fUseDebugLoop && enmEvent <= DBGFEVENT_XCPT_LAST;
2874 enmEvent = (DBGFEVENTTYPE)(enmEvent + 1))
2875 fUseDebugLoop = DBGF_IS_EVENT_ENABLED(pVM, enmEvent);
2876
2877 /* Common VM exits. */
2878 for (DBGFEVENTTYPE enmEvent = DBGFEVENT_EXIT_FIRST;
2879 !fUseDebugLoop && enmEvent <= DBGFEVENT_EXIT_LAST_COMMON;
2880 enmEvent = (DBGFEVENTTYPE)(enmEvent + 1))
2881 fUseDebugLoop = DBGF_IS_EVENT_ENABLED(pVM, enmEvent);
2882
2883 /* Vendor specific VM exits. */
2884 if (HMR3IsVmxEnabled(pVM->pUVM))
2885 for (DBGFEVENTTYPE enmEvent = DBGFEVENT_EXIT_VMX_FIRST;
2886 !fUseDebugLoop && enmEvent <= DBGFEVENT_EXIT_VMX_LAST;
2887 enmEvent = (DBGFEVENTTYPE)(enmEvent + 1))
2888 fUseDebugLoop = DBGF_IS_EVENT_ENABLED(pVM, enmEvent);
2889 else
2890 for (DBGFEVENTTYPE enmEvent = DBGFEVENT_EXIT_SVM_FIRST;
2891 !fUseDebugLoop && enmEvent <= DBGFEVENT_EXIT_SVM_LAST;
2892 enmEvent = (DBGFEVENTTYPE)(enmEvent + 1))
2893 fUseDebugLoop = DBGF_IS_EVENT_ENABLED(pVM, enmEvent);
2894
2895 /* Done. */
2896 pVM->hm.s.fUseDebugLoop = fUseDebugLoop;
2897}
2898
2899
2900/**
2901 * Follow up notification callback to HMR3NotifyDebugEventChanged for each CPU.
2902 *
2903 * HM uses this to combine the decision made by HMR3NotifyDebugEventChanged with
2904 * per CPU settings.
2905 *
2906 * @param pVM The VM cross context VM structure.
2907 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
2908 */
2909VMMR3_INT_DECL(void) HMR3NotifyDebugEventChangedPerCpu(PVM pVM, PVMCPU pVCpu)
2910{
2911 pVCpu->hm.s.fUseDebugLoop = pVCpu->hm.s.fSingleInstruction | pVM->hm.s.fUseDebugLoop;
2912}
2913
2914
2915/**
2916 * Checks if we are currently using hardware acceleration.
2917 *
2918 * @returns true if hardware acceleration is being used, otherwise false.
2919 * @param pVCpu The cross context virtual CPU structure.
2920 */
2921VMMR3_INT_DECL(bool) HMR3IsActive(PVMCPU pVCpu)
2922{
2923 return pVCpu->hm.s.fActive;
2924}
2925
2926
2927/**
2928 * External interface for querying whether hardware acceleration is enabled.
2929 *
2930 * @returns true if VT-x or AMD-V is being used, otherwise false.
2931 * @param pUVM The user mode VM handle.
2932 * @sa HMIsEnabled, HMIsEnabledNotMacro.
2933 */
2934VMMR3DECL(bool) HMR3IsEnabled(PUVM pUVM)
2935{
2936 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
2937 PVM pVM = pUVM->pVM;
2938 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
2939 return pVM->fHMEnabled; /* Don't use the macro as the GUI may query us very very early. */
2940}
2941
2942
2943/**
2944 * External interface for querying whether VT-x is being used.
2945 *
2946 * @returns true if VT-x is being used, otherwise false.
2947 * @param pUVM The user mode VM handle.
2948 * @sa HMR3IsSvmEnabled, HMIsEnabled
2949 */
2950VMMR3DECL(bool) HMR3IsVmxEnabled(PUVM pUVM)
2951{
2952 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
2953 PVM pVM = pUVM->pVM;
2954 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
2955 return pVM->hm.s.vmx.fEnabled
2956 && pVM->hm.s.vmx.fSupported
2957 && pVM->fHMEnabled;
2958}
2959
2960
2961/**
2962 * External interface for querying whether AMD-V is being used.
2963 *
2964 * @returns true if VT-x is being used, otherwise false.
2965 * @param pUVM The user mode VM handle.
2966 * @sa HMR3IsVmxEnabled, HMIsEnabled
2967 */
2968VMMR3DECL(bool) HMR3IsSvmEnabled(PUVM pUVM)
2969{
2970 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
2971 PVM pVM = pUVM->pVM;
2972 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
2973 return pVM->hm.s.svm.fEnabled
2974 && pVM->hm.s.svm.fSupported
2975 && pVM->fHMEnabled;
2976}
2977
2978
2979/**
2980 * Checks if we are currently using nested paging.
2981 *
2982 * @returns true if nested paging is being used, otherwise false.
2983 * @param pUVM The user mode VM handle.
2984 */
2985VMMR3DECL(bool) HMR3IsNestedPagingActive(PUVM pUVM)
2986{
2987 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
2988 PVM pVM = pUVM->pVM;
2989 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
2990 return pVM->hm.s.fNestedPaging;
2991}
2992
2993
2994/**
2995 * Checks if virtualized APIC registers is enabled.
2996 *
2997 * When enabled this feature allows the hardware to access most of the
2998 * APIC registers in the virtual-APIC page without causing VM-exits. See
2999 * Intel spec. 29.1.1 "Virtualized APIC Registers".
3000 *
3001 * @returns true if virtualized APIC registers is enabled, otherwise
3002 * false.
3003 * @param pUVM The user mode VM handle.
3004 */
3005VMMR3DECL(bool) HMR3IsVirtApicRegsEnabled(PUVM pUVM)
3006{
3007 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
3008 PVM pVM = pUVM->pVM;
3009 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
3010 return pVM->hm.s.fVirtApicRegs;
3011}
3012
3013
3014/**
3015 * Checks if APIC posted-interrupt processing is enabled.
3016 *
3017 * This returns whether we can deliver interrupts to the guest without
3018 * leaving guest-context by updating APIC state from host-context.
3019 *
3020 * @returns true if APIC posted-interrupt processing is enabled,
3021 * otherwise false.
3022 * @param pUVM The user mode VM handle.
3023 */
3024VMMR3DECL(bool) HMR3IsPostedIntrsEnabled(PUVM pUVM)
3025{
3026 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
3027 PVM pVM = pUVM->pVM;
3028 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
3029 return pVM->hm.s.fPostedIntrs;
3030}
3031
3032
3033/**
3034 * Checks if we are currently using VPID in VT-x mode.
3035 *
3036 * @returns true if VPID is being used, otherwise false.
3037 * @param pUVM The user mode VM handle.
3038 */
3039VMMR3DECL(bool) HMR3IsVpidActive(PUVM pUVM)
3040{
3041 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
3042 PVM pVM = pUVM->pVM;
3043 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
3044 return pVM->hm.s.vmx.fVpid;
3045}
3046
3047
3048/**
3049 * Checks if we are currently using VT-x unrestricted execution,
3050 * aka UX.
3051 *
3052 * @returns true if UX is being used, otherwise false.
3053 * @param pUVM The user mode VM handle.
3054 */
3055VMMR3DECL(bool) HMR3IsUXActive(PUVM pUVM)
3056{
3057 UVM_ASSERT_VALID_EXT_RETURN(pUVM, false);
3058 PVM pVM = pUVM->pVM;
3059 VM_ASSERT_VALID_EXT_RETURN(pVM, false);
3060 return pVM->hm.s.vmx.fUnrestrictedGuest
3061 || pVM->hm.s.svm.fSupported;
3062}
3063
3064
3065/**
3066 * Checks if internal events are pending. In that case we are not allowed to dispatch interrupts.
3067 *
3068 * @returns true if an internal event is pending, otherwise false.
3069 * @param pVCpu The cross context virtual CPU structure.
3070 */
3071VMMR3_INT_DECL(bool) HMR3IsEventPending(PVMCPU pVCpu)
3072{
3073 return HMIsEnabled(pVCpu->pVMR3)
3074 && pVCpu->hm.s.Event.fPending;
3075}
3076
3077
3078/**
3079 * Checks if the VMX-preemption timer is being used.
3080 *
3081 * @returns true if the VMX-preemption timer is being used, otherwise false.
3082 * @param pVM The cross context VM structure.
3083 */
3084VMMR3_INT_DECL(bool) HMR3IsVmxPreemptionTimerUsed(PVM pVM)
3085{
3086 return HMIsEnabled(pVM)
3087 && pVM->hm.s.vmx.fEnabled
3088 && pVM->hm.s.vmx.fUsePreemptTimer;
3089}
3090
3091
3092/**
3093 * Check fatal VT-x/AMD-V error and produce some meaningful
3094 * log release message.
3095 *
3096 * @param pVM The cross context VM structure.
3097 * @param iStatusCode VBox status code.
3098 */
3099VMMR3_INT_DECL(void) HMR3CheckError(PVM pVM, int iStatusCode)
3100{
3101 for (VMCPUID i = 0; i < pVM->cCpus; i++)
3102 {
3103 PVMCPU pVCpu = &pVM->aCpus[i];
3104 switch (iStatusCode)
3105 {
3106 /** @todo r=ramshankar: Are all EMTs out of ring-0 at this point!? If not, we
3107 * might be getting inaccurate values for non-guru'ing EMTs. */
3108 case VERR_VMX_INVALID_VMCS_FIELD:
3109 break;
3110
3111 case VERR_VMX_INVALID_VMCS_PTR:
3112 LogRel(("HM: VERR_VMX_INVALID_VMCS_PTR:\n"));
3113 LogRel(("HM: CPU[%u] Current pointer %#RGp vs %#RGp\n", i, pVCpu->hm.s.vmx.LastError.u64VmcsPhys,
3114 pVCpu->hm.s.vmx.HCPhysVmcs));
3115 LogRel(("HM: CPU[%u] Current VMCS version %#x\n", i, pVCpu->hm.s.vmx.LastError.u32VmcsRev));
3116 LogRel(("HM: CPU[%u] Entered Host Cpu %u\n", i, pVCpu->hm.s.vmx.LastError.idEnteredCpu));
3117 LogRel(("HM: CPU[%u] Current Host Cpu %u\n", i, pVCpu->hm.s.vmx.LastError.idCurrentCpu));
3118 break;
3119
3120 case VERR_VMX_UNABLE_TO_START_VM:
3121 LogRel(("HM: VERR_VMX_UNABLE_TO_START_VM:\n"));
3122 LogRel(("HM: CPU[%u] Instruction error %#x\n", i, pVCpu->hm.s.vmx.LastError.u32InstrError));
3123 LogRel(("HM: CPU[%u] Exit reason %#x\n", i, pVCpu->hm.s.vmx.LastError.u32ExitReason));
3124
3125 if ( pVCpu->hm.s.vmx.LastError.u32InstrError == VMXINSTRERR_VMLAUNCH_NON_CLEAR_VMCS
3126 || pVCpu->hm.s.vmx.LastError.u32InstrError == VMXINSTRERR_VMRESUME_NON_LAUNCHED_VMCS)
3127 {
3128 LogRel(("HM: CPU[%u] Entered Host Cpu %u\n", i, pVCpu->hm.s.vmx.LastError.idEnteredCpu));
3129 LogRel(("HM: CPU[%u] Current Host Cpu %u\n", i, pVCpu->hm.s.vmx.LastError.idCurrentCpu));
3130 }
3131 else if (pVCpu->hm.s.vmx.LastError.u32InstrError == VMXINSTRERR_VMENTRY_INVALID_CTLS)
3132 {
3133 LogRel(("HM: CPU[%u] PinCtls %#RX32\n", i, pVCpu->hm.s.vmx.u32PinCtls));
3134 {
3135 uint32_t const u32Val = pVCpu->hm.s.vmx.u32PinCtls;
3136 HMVMX_LOGREL_FEAT(u32Val, VMX_PIN_CTLS_EXT_INT_EXIT );
3137 HMVMX_LOGREL_FEAT(u32Val, VMX_PIN_CTLS_NMI_EXIT );
3138 HMVMX_LOGREL_FEAT(u32Val, VMX_PIN_CTLS_VIRT_NMI );
3139 HMVMX_LOGREL_FEAT(u32Val, VMX_PIN_CTLS_PREEMPT_TIMER);
3140 HMVMX_LOGREL_FEAT(u32Val, VMX_PIN_CTLS_POSTED_INT );
3141 }
3142 LogRel(("HM: CPU[%u] ProcCtls %#RX32\n", i, pVCpu->hm.s.vmx.u32ProcCtls));
3143 {
3144 uint32_t const u32Val = pVCpu->hm.s.vmx.u32ProcCtls;
3145 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_INT_WINDOW_EXIT );
3146 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_USE_TSC_OFFSETTING);
3147 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_HLT_EXIT );
3148 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_INVLPG_EXIT );
3149 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_MWAIT_EXIT );
3150 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_RDPMC_EXIT );
3151 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_RDTSC_EXIT );
3152 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_CR3_LOAD_EXIT );
3153 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_CR3_STORE_EXIT );
3154 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_CR8_LOAD_EXIT );
3155 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_CR8_STORE_EXIT );
3156 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_USE_TPR_SHADOW );
3157 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_NMI_WINDOW_EXIT );
3158 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_MOV_DR_EXIT );
3159 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_UNCOND_IO_EXIT );
3160 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_USE_IO_BITMAPS );
3161 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_MONITOR_TRAP_FLAG );
3162 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_USE_MSR_BITMAPS );
3163 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_MONITOR_EXIT );
3164 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_PAUSE_EXIT );
3165 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS_USE_SECONDARY_CTLS);
3166 }
3167 LogRel(("HM: CPU[%u] ProcCtls2 %#RX32\n", i, pVCpu->hm.s.vmx.u32ProcCtls2));
3168 {
3169 uint32_t const u32Val = pVCpu->hm.s.vmx.u32ProcCtls2;
3170 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_VIRT_APIC_ACCESS );
3171 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_EPT );
3172 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_DESC_TABLE_EXIT );
3173 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_RDTSCP );
3174 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_VIRT_X2APIC_MODE );
3175 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_VPID );
3176 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_WBINVD_EXIT );
3177 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_UNRESTRICTED_GUEST);
3178 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_APIC_REG_VIRT );
3179 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_VIRT_INT_DELIVERY );
3180 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_PAUSE_LOOP_EXIT );
3181 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_RDRAND_EXIT );
3182 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_INVPCID );
3183 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_VMFUNC );
3184 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_VMCS_SHADOWING );
3185 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_ENCLS_EXIT );
3186 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_RDSEED_EXIT );
3187 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_PML );
3188 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_EPT_VE );
3189 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_CONCEAL_FROM_PT );
3190 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_XSAVES_XRSTORS );
3191 HMVMX_LOGREL_FEAT(u32Val, VMX_PROC_CTLS2_TSC_SCALING );
3192 }
3193 LogRel(("HM: CPU[%u] EntryCtls %#RX32\n", i, pVCpu->hm.s.vmx.u32EntryCtls));
3194 {
3195 uint32_t const u32Val = pVCpu->hm.s.vmx.u32EntryCtls;
3196 HMVMX_LOGREL_FEAT(u32Val, VMX_ENTRY_CTLS_LOAD_DEBUG );
3197 HMVMX_LOGREL_FEAT(u32Val, VMX_ENTRY_CTLS_IA32E_MODE_GUEST );
3198 HMVMX_LOGREL_FEAT(u32Val, VMX_ENTRY_CTLS_ENTRY_TO_SMM );
3199 HMVMX_LOGREL_FEAT(u32Val, VMX_ENTRY_CTLS_DEACTIVATE_DUAL_MON);
3200 HMVMX_LOGREL_FEAT(u32Val, VMX_ENTRY_CTLS_LOAD_PERF_MSR );
3201 HMVMX_LOGREL_FEAT(u32Val, VMX_ENTRY_CTLS_LOAD_PAT_MSR );
3202 HMVMX_LOGREL_FEAT(u32Val, VMX_ENTRY_CTLS_LOAD_EFER_MSR );
3203 }
3204 LogRel(("HM: CPU[%u] ExitCtls %#RX32\n", i, pVCpu->hm.s.vmx.u32ExitCtls));
3205 {
3206 uint32_t const u32Val = pVCpu->hm.s.vmx.u32ExitCtls;
3207 HMVMX_LOGREL_FEAT(u32Val, VMX_EXIT_CTLS_SAVE_DEBUG );
3208 HMVMX_LOGREL_FEAT(u32Val, VMX_EXIT_CTLS_HOST_ADDR_SPACE_SIZE );
3209 HMVMX_LOGREL_FEAT(u32Val, VMX_EXIT_CTLS_LOAD_PERF_MSR );
3210 HMVMX_LOGREL_FEAT(u32Val, VMX_EXIT_CTLS_ACK_EXT_INT );
3211 HMVMX_LOGREL_FEAT(u32Val, VMX_EXIT_CTLS_SAVE_PAT_MSR );
3212 HMVMX_LOGREL_FEAT(u32Val, VMX_EXIT_CTLS_LOAD_PAT_MSR );
3213 HMVMX_LOGREL_FEAT(u32Val, VMX_EXIT_CTLS_SAVE_EFER_MSR );
3214 HMVMX_LOGREL_FEAT(u32Val, VMX_EXIT_CTLS_LOAD_EFER_MSR );
3215 HMVMX_LOGREL_FEAT(u32Val, VMX_EXIT_CTLS_SAVE_PREEMPT_TIMER );
3216 }
3217 LogRel(("HM: CPU[%u] HCPhysMsrBitmap %#RHp\n", i, pVCpu->hm.s.vmx.HCPhysMsrBitmap));
3218 LogRel(("HM: CPU[%u] HCPhysGuestMsr %#RHp\n", i, pVCpu->hm.s.vmx.HCPhysGuestMsr));
3219 LogRel(("HM: CPU[%u] HCPhysHostMsr %#RHp\n", i, pVCpu->hm.s.vmx.HCPhysHostMsr));
3220 LogRel(("HM: CPU[%u] cMsrs %u\n", i, pVCpu->hm.s.vmx.cMsrs));
3221 }
3222 /** @todo Log VM-entry event injection control fields
3223 * VMX_VMCS_CTRL_ENTRY_IRQ_INFO, VMX_VMCS_CTRL_ENTRY_EXCEPTION_ERRCODE
3224 * and VMX_VMCS_CTRL_ENTRY_INSTR_LENGTH from the VMCS. */
3225 break;
3226
3227 /* The guru will dump the HM error and exit history. Nothing extra to report for these errors. */
3228 case VERR_VMX_INVALID_VMXON_PTR:
3229 case VERR_HM_UNSUPPORTED_CPU_FEATURE_COMBO:
3230 case VERR_VMX_INVALID_GUEST_STATE:
3231 case VERR_VMX_UNEXPECTED_EXIT:
3232 case VERR_SVM_UNKNOWN_EXIT:
3233 case VERR_SVM_UNEXPECTED_EXIT:
3234 case VERR_SVM_UNEXPECTED_PATCH_TYPE:
3235 case VERR_SVM_UNEXPECTED_XCPT_EXIT:
3236 case VERR_VMX_UNEXPECTED_INTERRUPTION_EXIT_TYPE:
3237 break;
3238 }
3239 }
3240
3241 if (iStatusCode == VERR_VMX_UNABLE_TO_START_VM)
3242 {
3243 LogRel(("HM: VERR_VMX_UNABLE_TO_START_VM: VM-entry allowed-1 %#RX32\n", pVM->hm.s.vmx.Msrs.EntryCtls.n.allowed1));
3244 LogRel(("HM: VERR_VMX_UNABLE_TO_START_VM: VM-entry allowed-0 %#RX32\n", pVM->hm.s.vmx.Msrs.EntryCtls.n.allowed0));
3245 }
3246 else if (iStatusCode == VERR_VMX_INVALID_VMXON_PTR)
3247 LogRel(("HM: HCPhysVmxEnableError = %#RHp\n", pVM->hm.s.vmx.HCPhysVmxEnableError));
3248}
3249
3250
3251/**
3252 * Execute state save operation.
3253 *
3254 * Save only data that cannot be re-loaded while entering HM ring-0 code. This
3255 * is because we always save the VM state from ring-3 and thus most HM state
3256 * will be re-synced dynamically at runtime and don't need to be part of the VM
3257 * saved state.
3258 *
3259 * @returns VBox status code.
3260 * @param pVM The cross context VM structure.
3261 * @param pSSM SSM operation handle.
3262 */
3263static DECLCALLBACK(int) hmR3Save(PVM pVM, PSSMHANDLE pSSM)
3264{
3265 int rc;
3266
3267 Log(("hmR3Save:\n"));
3268
3269 for (VMCPUID i = 0; i < pVM->cCpus; i++)
3270 {
3271 Assert(!pVM->aCpus[i].hm.s.Event.fPending);
3272 if (pVM->cpum.ro.GuestFeatures.fSvm)
3273 {
3274 PCSVMNESTEDVMCBCACHE pVmcbNstGstCache = &pVM->aCpus[i].hm.s.svm.NstGstVmcbCache;
3275 rc = SSMR3PutBool(pSSM, pVmcbNstGstCache->fCacheValid);
3276 rc |= SSMR3PutU16(pSSM, pVmcbNstGstCache->u16InterceptRdCRx);
3277 rc |= SSMR3PutU16(pSSM, pVmcbNstGstCache->u16InterceptWrCRx);
3278 rc |= SSMR3PutU16(pSSM, pVmcbNstGstCache->u16InterceptRdDRx);
3279 rc |= SSMR3PutU16(pSSM, pVmcbNstGstCache->u16InterceptWrDRx);
3280 rc |= SSMR3PutU16(pSSM, pVmcbNstGstCache->u16PauseFilterThreshold);
3281 rc |= SSMR3PutU16(pSSM, pVmcbNstGstCache->u16PauseFilterCount);
3282 rc |= SSMR3PutU32(pSSM, pVmcbNstGstCache->u32InterceptXcpt);
3283 rc |= SSMR3PutU64(pSSM, pVmcbNstGstCache->u64InterceptCtrl);
3284 rc |= SSMR3PutU64(pSSM, pVmcbNstGstCache->u64TSCOffset);
3285 rc |= SSMR3PutBool(pSSM, pVmcbNstGstCache->fVIntrMasking);
3286 rc |= SSMR3PutBool(pSSM, pVmcbNstGstCache->fNestedPaging);
3287 rc |= SSMR3PutBool(pSSM, pVmcbNstGstCache->fLbrVirt);
3288 AssertRCReturn(rc, rc);
3289 }
3290 }
3291
3292 /* Save the guest patch data. */
3293 rc = SSMR3PutGCPtr(pSSM, pVM->hm.s.pGuestPatchMem);
3294 rc |= SSMR3PutGCPtr(pSSM, pVM->hm.s.pFreeGuestPatchMem);
3295 rc |= SSMR3PutU32(pSSM, pVM->hm.s.cbGuestPatchMem);
3296
3297 /* Store all the guest patch records too. */
3298 rc |= SSMR3PutU32(pSSM, pVM->hm.s.cPatches);
3299 AssertRCReturn(rc, rc);
3300
3301 for (uint32_t i = 0; i < pVM->hm.s.cPatches; i++)
3302 {
3303 AssertCompileSize(HMTPRINSTR, 4);
3304 PCHMTPRPATCH pPatch = &pVM->hm.s.aPatches[i];
3305 rc = SSMR3PutU32(pSSM, pPatch->Core.Key);
3306 rc |= SSMR3PutMem(pSSM, pPatch->aOpcode, sizeof(pPatch->aOpcode));
3307 rc |= SSMR3PutU32(pSSM, pPatch->cbOp);
3308 rc |= SSMR3PutMem(pSSM, pPatch->aNewOpcode, sizeof(pPatch->aNewOpcode));
3309 rc |= SSMR3PutU32(pSSM, pPatch->cbNewOp);
3310 rc |= SSMR3PutU32(pSSM, (uint32_t)pPatch->enmType);
3311 rc |= SSMR3PutU32(pSSM, pPatch->uSrcOperand);
3312 rc |= SSMR3PutU32(pSSM, pPatch->uDstOperand);
3313 rc |= SSMR3PutU32(pSSM, pPatch->pJumpTarget);
3314 rc |= SSMR3PutU32(pSSM, pPatch->cFaults);
3315 AssertRCReturn(rc, rc);
3316 }
3317
3318 return VINF_SUCCESS;
3319}
3320
3321
3322/**
3323 * Execute state load operation.
3324 *
3325 * @returns VBox status code.
3326 * @param pVM The cross context VM structure.
3327 * @param pSSM SSM operation handle.
3328 * @param uVersion Data layout version.
3329 * @param uPass The data pass.
3330 */
3331static DECLCALLBACK(int) hmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
3332{
3333 int rc;
3334
3335 LogFlowFunc(("uVersion=%u\n", uVersion));
3336 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
3337
3338 /*
3339 * Validate version.
3340 */
3341 if ( uVersion != HM_SAVED_STATE_VERSION_SVM_NESTED_HWVIRT
3342 && uVersion != HM_SAVED_STATE_VERSION_TPR_PATCHING
3343 && uVersion != HM_SAVED_STATE_VERSION_NO_TPR_PATCHING
3344 && uVersion != HM_SAVED_STATE_VERSION_2_0_X)
3345 {
3346 AssertMsgFailed(("hmR3Load: Invalid version uVersion=%d!\n", uVersion));
3347 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
3348 }
3349
3350 /*
3351 * Load per-VCPU state.
3352 */
3353 for (VMCPUID i = 0; i < pVM->cCpus; i++)
3354 {
3355 if (uVersion >= HM_SAVED_STATE_VERSION_SVM_NESTED_HWVIRT)
3356 {
3357 /* Load the SVM nested hw.virt state if the VM is configured for it. */
3358 if (pVM->cpum.ro.GuestFeatures.fSvm)
3359 {
3360 PSVMNESTEDVMCBCACHE pVmcbNstGstCache = &pVM->aCpus[i].hm.s.svm.NstGstVmcbCache;
3361 rc = SSMR3GetBool(pSSM, &pVmcbNstGstCache->fCacheValid);
3362 rc |= SSMR3GetU16(pSSM, &pVmcbNstGstCache->u16InterceptRdCRx);
3363 rc |= SSMR3GetU16(pSSM, &pVmcbNstGstCache->u16InterceptWrCRx);
3364 rc |= SSMR3GetU16(pSSM, &pVmcbNstGstCache->u16InterceptRdDRx);
3365 rc |= SSMR3GetU16(pSSM, &pVmcbNstGstCache->u16InterceptWrDRx);
3366 rc |= SSMR3GetU16(pSSM, &pVmcbNstGstCache->u16PauseFilterThreshold);
3367 rc |= SSMR3GetU16(pSSM, &pVmcbNstGstCache->u16PauseFilterCount);
3368 rc |= SSMR3GetU32(pSSM, &pVmcbNstGstCache->u32InterceptXcpt);
3369 rc |= SSMR3GetU64(pSSM, &pVmcbNstGstCache->u64InterceptCtrl);
3370 rc |= SSMR3GetU64(pSSM, &pVmcbNstGstCache->u64TSCOffset);
3371 rc |= SSMR3GetBool(pSSM, &pVmcbNstGstCache->fVIntrMasking);
3372 rc |= SSMR3GetBool(pSSM, &pVmcbNstGstCache->fNestedPaging);
3373 rc |= SSMR3GetBool(pSSM, &pVmcbNstGstCache->fLbrVirt);
3374 AssertRCReturn(rc, rc);
3375 }
3376 }
3377 else
3378 {
3379 /* Pending HM event (obsolete for a long time since TPRM holds the info.) */
3380 rc = SSMR3GetU32(pSSM, &pVM->aCpus[i].hm.s.Event.fPending);
3381 rc |= SSMR3GetU32(pSSM, &pVM->aCpus[i].hm.s.Event.u32ErrCode);
3382 rc |= SSMR3GetU64(pSSM, &pVM->aCpus[i].hm.s.Event.u64IntInfo);
3383
3384 /* VMX fWasInRealMode related data. */
3385 uint32_t uDummy;
3386 rc |= SSMR3GetU32(pSSM, &uDummy); AssertRCReturn(rc, rc);
3387 rc |= SSMR3GetU32(pSSM, &uDummy); AssertRCReturn(rc, rc);
3388 rc |= SSMR3GetU32(pSSM, &uDummy); AssertRCReturn(rc, rc);
3389 AssertRCReturn(rc, rc);
3390 }
3391 }
3392
3393 /*
3394 * Load TPR patching data.
3395 */
3396 if (uVersion >= HM_SAVED_STATE_VERSION_TPR_PATCHING)
3397 {
3398 rc = SSMR3GetGCPtr(pSSM, &pVM->hm.s.pGuestPatchMem);
3399 rc |= SSMR3GetGCPtr(pSSM, &pVM->hm.s.pFreeGuestPatchMem);
3400 rc |= SSMR3GetU32(pSSM, &pVM->hm.s.cbGuestPatchMem);
3401
3402 /* Fetch all TPR patch records. */
3403 rc |= SSMR3GetU32(pSSM, &pVM->hm.s.cPatches);
3404 AssertRCReturn(rc, rc);
3405 for (uint32_t i = 0; i < pVM->hm.s.cPatches; i++)
3406 {
3407 PHMTPRPATCH pPatch = &pVM->hm.s.aPatches[i];
3408 rc = SSMR3GetU32(pSSM, &pPatch->Core.Key);
3409 rc |= SSMR3GetMem(pSSM, pPatch->aOpcode, sizeof(pPatch->aOpcode));
3410 rc |= SSMR3GetU32(pSSM, &pPatch->cbOp);
3411 rc |= SSMR3GetMem(pSSM, pPatch->aNewOpcode, sizeof(pPatch->aNewOpcode));
3412 rc |= SSMR3GetU32(pSSM, &pPatch->cbNewOp);
3413 rc |= SSMR3GetU32(pSSM, (uint32_t *)&pPatch->enmType);
3414
3415 if (pPatch->enmType == HMTPRINSTR_JUMP_REPLACEMENT)
3416 pVM->hm.s.fTPRPatchingActive = true;
3417 Assert(pPatch->enmType == HMTPRINSTR_JUMP_REPLACEMENT || pVM->hm.s.fTPRPatchingActive == false);
3418
3419 rc |= SSMR3GetU32(pSSM, &pPatch->uSrcOperand);
3420 rc |= SSMR3GetU32(pSSM, &pPatch->uDstOperand);
3421 rc |= SSMR3GetU32(pSSM, &pPatch->cFaults);
3422 rc |= SSMR3GetU32(pSSM, &pPatch->pJumpTarget);
3423 AssertRCReturn(rc, rc);
3424
3425 LogFlow(("hmR3Load: patch %d\n", i));
3426 LogFlow(("Key = %x\n", pPatch->Core.Key));
3427 LogFlow(("cbOp = %d\n", pPatch->cbOp));
3428 LogFlow(("cbNewOp = %d\n", pPatch->cbNewOp));
3429 LogFlow(("type = %d\n", pPatch->enmType));
3430 LogFlow(("srcop = %d\n", pPatch->uSrcOperand));
3431 LogFlow(("dstop = %d\n", pPatch->uDstOperand));
3432 LogFlow(("cFaults = %d\n", pPatch->cFaults));
3433 LogFlow(("target = %x\n", pPatch->pJumpTarget));
3434
3435 rc = RTAvloU32Insert(&pVM->hm.s.PatchTree, &pPatch->Core);
3436 AssertRCReturn(rc, rc);
3437 }
3438 }
3439
3440 return VINF_SUCCESS;
3441}
3442
3443
3444/**
3445 * Gets the name of a VT-x exit code.
3446 *
3447 * @returns Pointer to read only string if @a uExit is known, otherwise NULL.
3448 * @param uExit The VT-x exit to name.
3449 */
3450VMMR3DECL(const char *) HMR3GetVmxExitName(uint32_t uExit)
3451{
3452 if (uExit < RT_ELEMENTS(g_apszVmxExitReasons))
3453 return g_apszVmxExitReasons[uExit];
3454 return NULL;
3455}
3456
3457
3458/**
3459 * Gets the name of an AMD-V exit code.
3460 *
3461 * @returns Pointer to read only string if @a uExit is known, otherwise NULL.
3462 * @param uExit The AMD-V exit to name.
3463 */
3464VMMR3DECL(const char *) HMR3GetSvmExitName(uint32_t uExit)
3465{
3466 if (uExit < RT_ELEMENTS(g_apszSvmExitReasons))
3467 return g_apszSvmExitReasons[uExit];
3468 return hmSvmGetSpecialExitReasonDesc(uExit);
3469}
3470
3471
3472/**
3473 * Displays HM info.
3474 *
3475 * @param pVM The cross context VM structure.
3476 * @param pHlp The info helper functions.
3477 * @param pszArgs Arguments, ignored.
3478 */
3479static DECLCALLBACK(void) hmR3Info(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
3480{
3481 NOREF(pszArgs);
3482 PVMCPU pVCpu = VMMGetCpu(pVM);
3483 if (!pVCpu)
3484 pVCpu = &pVM->aCpus[0];
3485
3486 if (HMIsEnabled(pVM))
3487 {
3488 if (pVM->hm.s.vmx.fSupported)
3489 pHlp->pfnPrintf(pHlp, "CPU[%u]: VT-x info:\n", pVCpu->idCpu);
3490 else
3491 pHlp->pfnPrintf(pHlp, "CPU[%u]: AMD-V info:\n", pVCpu->idCpu);
3492 pHlp->pfnPrintf(pHlp, " HM error = %#x (%u)\n", pVCpu->hm.s.u32HMError, pVCpu->hm.s.u32HMError);
3493 pHlp->pfnPrintf(pHlp, " rcLastExitToR3 = %Rrc\n", pVCpu->hm.s.rcLastExitToR3);
3494 }
3495 else
3496 pHlp->pfnPrintf(pHlp, "HM is not enabled for this VM!\n");
3497}
3498
3499
3500/**
3501 * Displays the HM pending event.
3502 *
3503 * @param pVM The cross context VM structure.
3504 * @param pHlp The info helper functions.
3505 * @param pszArgs Arguments, ignored.
3506 */
3507static DECLCALLBACK(void) hmR3InfoEventPending(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
3508{
3509 NOREF(pszArgs);
3510 PVMCPU pVCpu = VMMGetCpu(pVM);
3511 if (!pVCpu)
3512 pVCpu = &pVM->aCpus[0];
3513
3514 if (HMIsEnabled(pVM))
3515 {
3516 pHlp->pfnPrintf(pHlp, "CPU[%u]: HM event (fPending=%RTbool)\n", pVCpu->idCpu, pVCpu->hm.s.Event.fPending);
3517 if (pVCpu->hm.s.Event.fPending)
3518 {
3519 pHlp->pfnPrintf(pHlp, " u64IntInfo = %#RX64\n", pVCpu->hm.s.Event.u64IntInfo);
3520 pHlp->pfnPrintf(pHlp, " u32ErrCode = %#RX64\n", pVCpu->hm.s.Event.u32ErrCode);
3521 pHlp->pfnPrintf(pHlp, " cbInstr = %u bytes\n", pVCpu->hm.s.Event.cbInstr);
3522 pHlp->pfnPrintf(pHlp, " GCPtrFaultAddress = %#RGp\n", pVCpu->hm.s.Event.GCPtrFaultAddress);
3523 }
3524 }
3525 else
3526 pHlp->pfnPrintf(pHlp, "HM is not enabled for this VM!\n");
3527}
3528
3529
3530/**
3531 * Displays the SVM nested-guest VMCB cache.
3532 *
3533 * @param pVM The cross context VM structure.
3534 * @param pHlp The info helper functions.
3535 * @param pszArgs Arguments, ignored.
3536 */
3537static DECLCALLBACK(void) hmR3InfoSvmNstGstVmcbCache(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
3538{
3539 NOREF(pszArgs);
3540 PVMCPU pVCpu = VMMGetCpu(pVM);
3541 if (!pVCpu)
3542 pVCpu = &pVM->aCpus[0];
3543
3544 bool const fSvmEnabled = HMR3IsSvmEnabled(pVM->pUVM);
3545 if ( fSvmEnabled
3546 && pVM->cpum.ro.GuestFeatures.fSvm)
3547 {
3548 PCSVMNESTEDVMCBCACHE pVmcbNstGstCache = &pVCpu->hm.s.svm.NstGstVmcbCache;
3549 pHlp->pfnPrintf(pHlp, "CPU[%u]: HM SVM nested-guest VMCB cache\n", pVCpu->idCpu);
3550 pHlp->pfnPrintf(pHlp, " fCacheValid = %#RTbool\n", pVmcbNstGstCache->fCacheValid);
3551 pHlp->pfnPrintf(pHlp, " u16InterceptRdCRx = %#RX16\n", pVmcbNstGstCache->u16InterceptRdCRx);
3552 pHlp->pfnPrintf(pHlp, " u16InterceptWrCRx = %#RX16\n", pVmcbNstGstCache->u16InterceptWrCRx);
3553 pHlp->pfnPrintf(pHlp, " u16InterceptRdDRx = %#RX16\n", pVmcbNstGstCache->u16InterceptRdDRx);
3554 pHlp->pfnPrintf(pHlp, " u16InterceptWrDRx = %#RX16\n", pVmcbNstGstCache->u16InterceptWrDRx);
3555 pHlp->pfnPrintf(pHlp, " u16PauseFilterThreshold = %#RX16\n", pVmcbNstGstCache->u16PauseFilterThreshold);
3556 pHlp->pfnPrintf(pHlp, " u16PauseFilterCount = %#RX16\n", pVmcbNstGstCache->u16PauseFilterCount);
3557 pHlp->pfnPrintf(pHlp, " u32InterceptXcpt = %#RX32\n", pVmcbNstGstCache->u32InterceptXcpt);
3558 pHlp->pfnPrintf(pHlp, " u64InterceptCtrl = %#RX64\n", pVmcbNstGstCache->u64InterceptCtrl);
3559 pHlp->pfnPrintf(pHlp, " u64TSCOffset = %#RX64\n", pVmcbNstGstCache->u64TSCOffset);
3560 pHlp->pfnPrintf(pHlp, " fVIntrMasking = %RTbool\n", pVmcbNstGstCache->fVIntrMasking);
3561 pHlp->pfnPrintf(pHlp, " fNestedPaging = %RTbool\n", pVmcbNstGstCache->fNestedPaging);
3562 pHlp->pfnPrintf(pHlp, " fLbrVirt = %RTbool\n", pVmcbNstGstCache->fLbrVirt);
3563 }
3564 else
3565 {
3566 if (!fSvmEnabled)
3567 pHlp->pfnPrintf(pHlp, "HM SVM is not enabled for this VM!\n");
3568 else
3569 pHlp->pfnPrintf(pHlp, "SVM feature is not exposed to the guest!\n");
3570 }
3571}
3572
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