VirtualBox

source: vbox/trunk/src/VBox/VMM/VMM.cpp@ 19472

Last change on this file since 19472 was 19462, checked in by vboxsync, 16 years ago

VMM: iLastGZRc from VMM to VMMCPU.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 56.1 KB
Line 
1/* $Id: VMM.cpp 19462 2009-05-06 20:20:47Z vboxsync $ */
2/** @file
3 * VMM - The Virtual Machine Monitor Core.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22//#define NO_SUPCALLR0VMM
23
24/** @page pg_vmm VMM - The Virtual Machine Monitor
25 *
26 * The VMM component is two things at the moment, it's a component doing a few
27 * management and routing tasks, and it's the whole virtual machine monitor
28 * thing. For hysterical reasons, it is not doing all the management that one
29 * would expect, this is instead done by @ref pg_vm. We'll address this
30 * misdesign eventually.
31 *
32 * @see grp_vmm, grp_vm
33 *
34 *
35 * @section sec_vmmstate VMM State
36 *
37 * @image html VM_Statechart_Diagram.gif
38 *
39 * To be written.
40 *
41 *
42 * @subsection subsec_vmm_init VMM Initialization
43 *
44 * To be written.
45 *
46 *
47 * @subsection subsec_vmm_term VMM Termination
48 *
49 * To be written.
50 *
51 */
52
53/*******************************************************************************
54* Header Files *
55*******************************************************************************/
56#define LOG_GROUP LOG_GROUP_VMM
57#include <VBox/vmm.h>
58#include <VBox/vmapi.h>
59#include <VBox/pgm.h>
60#include <VBox/cfgm.h>
61#include <VBox/pdmqueue.h>
62#include <VBox/pdmapi.h>
63#include <VBox/cpum.h>
64#include <VBox/mm.h>
65#include <VBox/iom.h>
66#include <VBox/trpm.h>
67#include <VBox/selm.h>
68#include <VBox/em.h>
69#include <VBox/sup.h>
70#include <VBox/dbgf.h>
71#include <VBox/csam.h>
72#include <VBox/patm.h>
73#include <VBox/rem.h>
74#include <VBox/ssm.h>
75#include <VBox/tm.h>
76#include "VMMInternal.h"
77#include "VMMSwitcher/VMMSwitcher.h"
78#include <VBox/vm.h>
79
80#include <VBox/err.h>
81#include <VBox/param.h>
82#include <VBox/version.h>
83#include <VBox/x86.h>
84#include <VBox/hwaccm.h>
85#include <iprt/assert.h>
86#include <iprt/alloc.h>
87#include <iprt/asm.h>
88#include <iprt/time.h>
89#include <iprt/stream.h>
90#include <iprt/string.h>
91#include <iprt/stdarg.h>
92#include <iprt/ctype.h>
93
94
95
96/** The saved state version. */
97#define VMM_SAVED_STATE_VERSION 3
98
99
100/*******************************************************************************
101* Internal Functions *
102*******************************************************************************/
103static int vmmR3InitStacks(PVM pVM);
104static int vmmR3InitLoggers(PVM pVM);
105static void vmmR3InitRegisterStats(PVM pVM);
106static DECLCALLBACK(int) vmmR3Save(PVM pVM, PSSMHANDLE pSSM);
107static DECLCALLBACK(int) vmmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version);
108static DECLCALLBACK(void) vmmR3YieldEMT(PVM pVM, PTMTIMER pTimer, void *pvUser);
109static int vmmR3ServiceCallHostRequest(PVM pVM, PVMCPU pVCpu);
110static DECLCALLBACK(void) vmmR3InfoFF(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
111
112
113/**
114 * Initializes the VMM.
115 *
116 * @returns VBox status code.
117 * @param pVM The VM to operate on.
118 */
119VMMR3DECL(int) VMMR3Init(PVM pVM)
120{
121 LogFlow(("VMMR3Init\n"));
122
123 /*
124 * Assert alignment, sizes and order.
125 */
126 AssertMsg(pVM->vmm.s.offVM == 0, ("Already initialized!\n"));
127 AssertCompile(sizeof(pVM->vmm.s) <= sizeof(pVM->vmm.padding));
128 AssertCompile(sizeof(pVM->aCpus[0].vmm.s) <= sizeof(pVM->aCpus[0].vmm.padding));
129
130 /*
131 * Init basic VM VMM members.
132 */
133 pVM->vmm.s.offVM = RT_OFFSETOF(VM, vmm);
134 int rc = CFGMR3QueryU32(CFGMR3GetRoot(pVM), "YieldEMTInterval", &pVM->vmm.s.cYieldEveryMillies);
135 if (rc == VERR_CFGM_VALUE_NOT_FOUND)
136 pVM->vmm.s.cYieldEveryMillies = 23; /* Value arrived at after experimenting with the grub boot prompt. */
137 //pVM->vmm.s.cYieldEveryMillies = 8; //debugging
138 else
139 AssertMsgRCReturn(rc, ("Configuration error. Failed to query \"YieldEMTInterval\", rc=%Rrc\n", rc), rc);
140
141 /* GC switchers are enabled by default. Turned off by HWACCM. */
142 pVM->vmm.s.fSwitcherDisabled = false;
143
144 /*
145 * Register the saved state data unit.
146 */
147 rc = SSMR3RegisterInternal(pVM, "vmm", 1, VMM_SAVED_STATE_VERSION, VMM_STACK_SIZE + sizeof(RTGCPTR),
148 NULL, vmmR3Save, NULL,
149 NULL, vmmR3Load, NULL);
150 if (RT_FAILURE(rc))
151 return rc;
152
153 /*
154 * Register the Ring-0 VM handle with the session for fast ioctl calls.
155 */
156 rc = SUPSetVMForFastIOCtl(pVM->pVMR0);
157 if (RT_FAILURE(rc))
158 return rc;
159
160 /*
161 * Init various sub-components.
162 */
163 rc = vmmR3SwitcherInit(pVM);
164 if (RT_SUCCESS(rc))
165 {
166 rc = vmmR3InitStacks(pVM);
167 if (RT_SUCCESS(rc))
168 {
169 rc = vmmR3InitLoggers(pVM);
170
171#ifdef VBOX_WITH_NMI
172 /*
173 * Allocate mapping for the host APIC.
174 */
175 if (RT_SUCCESS(rc))
176 {
177 rc = MMR3HyperReserve(pVM, PAGE_SIZE, "Host APIC", &pVM->vmm.s.GCPtrApicBase);
178 AssertRC(rc);
179 }
180#endif
181 if (RT_SUCCESS(rc))
182 {
183 /*
184 * Debug info and statistics.
185 */
186 DBGFR3InfoRegisterInternal(pVM, "ff", "Displays the current Forced actions Flags.", vmmR3InfoFF);
187 vmmR3InitRegisterStats(pVM);
188
189 return VINF_SUCCESS;
190 }
191 }
192 /** @todo: Need failure cleanup. */
193
194 //more todo in here?
195 //if (RT_SUCCESS(rc))
196 //{
197 //}
198 //int rc2 = vmmR3TermCoreCode(pVM);
199 //AssertRC(rc2));
200 }
201
202 return rc;
203}
204
205
206/**
207 * Allocate & setup the VMM RC stack(s) (for EMTs).
208 *
209 * The stacks are also used for long jumps in Ring-0.
210 *
211 * @returns VBox status code.
212 * @param pVM Pointer to the shared VM structure.
213 *
214 * @remarks The optional guard page gets it protection setup up during R3 init
215 * completion because of init order issues.
216 */
217static int vmmR3InitStacks(PVM pVM)
218{
219 int rc = VINF_SUCCESS;
220
221 for (VMCPUID idCpu = 0; idCpu < pVM->cCPUs; idCpu++)
222 {
223 PVMCPU pVCpu = &pVM->aCpus[idCpu];
224
225#ifdef VBOX_STRICT_VMM_STACK
226 rc = MMR3HyperAllocOnceNoRel(pVM, VMM_STACK_SIZE + PAGE_SIZE + PAGE_SIZE, PAGE_SIZE, MM_TAG_VMM, (void **)&pVCpu->vmm.s.pbEMTStackR3);
227#else
228 rc = MMR3HyperAllocOnceNoRel(pVM, VMM_STACK_SIZE, PAGE_SIZE, MM_TAG_VMM, (void **)&pVCpu->vmm.s.pbEMTStackR3);
229#endif
230 if (RT_SUCCESS(rc))
231 {
232#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
233 /* MMHyperR3ToR0 returns R3 when not doing hardware assisted virtualization. */
234 if (!VMMIsHwVirtExtForced(pVM))
235 pVCpu->vmm.s.CallHostR0JmpBuf.pvSavedStack = NIL_RTR0PTR;
236 else
237#endif
238 pVCpu->vmm.s.CallHostR0JmpBuf.pvSavedStack = MMHyperR3ToR0(pVM, pVCpu->vmm.s.pbEMTStackR3);
239 pVCpu->vmm.s.pbEMTStackRC = MMHyperR3ToRC(pVM, pVCpu->vmm.s.pbEMTStackR3);
240 pVCpu->vmm.s.pbEMTStackBottomRC = pVCpu->vmm.s.pbEMTStackRC + VMM_STACK_SIZE;
241 AssertRelease(pVCpu->vmm.s.pbEMTStackRC);
242
243 CPUMSetHyperESP(pVCpu, pVCpu->vmm.s.pbEMTStackBottomRC);
244 }
245 }
246
247 return rc;
248}
249
250
251/**
252 * Initialize the loggers.
253 *
254 * @returns VBox status code.
255 * @param pVM Pointer to the shared VM structure.
256 */
257static int vmmR3InitLoggers(PVM pVM)
258{
259 int rc;
260
261 /*
262 * Allocate RC & R0 Logger instances (they are finalized in the relocator).
263 */
264#ifdef LOG_ENABLED
265 PRTLOGGER pLogger = RTLogDefaultInstance();
266 if (pLogger)
267 {
268 pVM->vmm.s.cbRCLogger = RT_OFFSETOF(RTLOGGERRC, afGroups[pLogger->cGroups]);
269 rc = MMR3HyperAllocOnceNoRel(pVM, pVM->vmm.s.cbRCLogger, 0, MM_TAG_VMM, (void **)&pVM->vmm.s.pRCLoggerR3);
270 if (RT_FAILURE(rc))
271 return rc;
272 pVM->vmm.s.pRCLoggerRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pRCLoggerR3);
273
274# ifdef VBOX_WITH_R0_LOGGING
275 rc = MMR3HyperAllocOnceNoRel(pVM, RT_OFFSETOF(VMMR0LOGGER, Logger.afGroups[pLogger->cGroups]),
276 0, MM_TAG_VMM, (void **)&pVM->vmm.s.pR0LoggerR3);
277 if (RT_FAILURE(rc))
278 return rc;
279 pVM->vmm.s.pR0LoggerR3->pVM = pVM->pVMR0;
280 //pVM->vmm.s.pR0LoggerR3->fCreated = false;
281 pVM->vmm.s.pR0LoggerR3->cbLogger = RT_OFFSETOF(RTLOGGER, afGroups[pLogger->cGroups]);
282 pVM->vmm.s.pR0LoggerR0 = MMHyperR3ToR0(pVM, pVM->vmm.s.pR0LoggerR3);
283# endif
284 }
285#endif /* LOG_ENABLED */
286
287#ifdef VBOX_WITH_RC_RELEASE_LOGGING
288 /*
289 * Allocate RC release logger instances (finalized in the relocator).
290 */
291 PRTLOGGER pRelLogger = RTLogRelDefaultInstance();
292 if (pRelLogger)
293 {
294 pVM->vmm.s.cbRCRelLogger = RT_OFFSETOF(RTLOGGERRC, afGroups[pRelLogger->cGroups]);
295 rc = MMR3HyperAllocOnceNoRel(pVM, pVM->vmm.s.cbRCRelLogger, 0, MM_TAG_VMM, (void **)&pVM->vmm.s.pRCRelLoggerR3);
296 if (RT_FAILURE(rc))
297 return rc;
298 pVM->vmm.s.pRCRelLoggerRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pRCRelLoggerR3);
299 }
300#endif /* VBOX_WITH_RC_RELEASE_LOGGING */
301 return VINF_SUCCESS;
302}
303
304
305/**
306 * VMMR3Init worker that register the statistics with STAM.
307 *
308 * @param pVM The shared VM structure.
309 */
310static void vmmR3InitRegisterStats(PVM pVM)
311{
312 /*
313 * Statistics.
314 */
315 STAM_REG(pVM, &pVM->vmm.s.StatRunRC, STAMTYPE_COUNTER, "/VMM/RunRC", STAMUNIT_OCCURENCES, "Number of context switches.");
316 STAM_REG(pVM, &pVM->vmm.s.StatRZRetNormal, STAMTYPE_COUNTER, "/VMM/RZRet/Normal", STAMUNIT_OCCURENCES, "Number of VINF_SUCCESS returns.");
317 STAM_REG(pVM, &pVM->vmm.s.StatRZRetInterrupt, STAMTYPE_COUNTER, "/VMM/RZRet/Interrupt", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_INTERRUPT returns.");
318 STAM_REG(pVM, &pVM->vmm.s.StatRZRetInterruptHyper, STAMTYPE_COUNTER, "/VMM/RZRet/InterruptHyper", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_INTERRUPT_HYPER returns.");
319 STAM_REG(pVM, &pVM->vmm.s.StatRZRetGuestTrap, STAMTYPE_COUNTER, "/VMM/RZRet/GuestTrap", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_GUEST_TRAP returns.");
320 STAM_REG(pVM, &pVM->vmm.s.StatRZRetRingSwitch, STAMTYPE_COUNTER, "/VMM/RZRet/RingSwitch", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_RING_SWITCH returns.");
321 STAM_REG(pVM, &pVM->vmm.s.StatRZRetRingSwitchInt, STAMTYPE_COUNTER, "/VMM/RZRet/RingSwitchInt", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_RING_SWITCH_INT returns.");
322 STAM_REG(pVM, &pVM->vmm.s.StatRZRetExceptionPrivilege, STAMTYPE_COUNTER, "/VMM/RZRet/ExceptionPrivilege", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_EXCEPTION_PRIVILEGED returns.");
323 STAM_REG(pVM, &pVM->vmm.s.StatRZRetStaleSelector, STAMTYPE_COUNTER, "/VMM/RZRet/StaleSelector", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_STALE_SELECTOR returns.");
324 STAM_REG(pVM, &pVM->vmm.s.StatRZRetIRETTrap, STAMTYPE_COUNTER, "/VMM/RZRet/IRETTrap", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_IRET_TRAP returns.");
325 STAM_REG(pVM, &pVM->vmm.s.StatRZRetEmulate, STAMTYPE_COUNTER, "/VMM/RZRet/Emulate", STAMUNIT_OCCURENCES, "Number of VINF_EM_EXECUTE_INSTRUCTION returns.");
326 STAM_REG(pVM, &pVM->vmm.s.StatRZRetIOBlockEmulate, STAMTYPE_COUNTER, "/VMM/RZRet/EmulateIOBlock", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_EMULATE_IO_BLOCK returns.");
327 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPatchEmulate, STAMTYPE_COUNTER, "/VMM/RZRet/PatchEmulate", STAMUNIT_OCCURENCES, "Number of VINF_PATCH_EMULATE_INSTR returns.");
328 STAM_REG(pVM, &pVM->vmm.s.StatRZRetIORead, STAMTYPE_COUNTER, "/VMM/RZRet/IORead", STAMUNIT_OCCURENCES, "Number of VINF_IOM_HC_IOPORT_READ returns.");
329 STAM_REG(pVM, &pVM->vmm.s.StatRZRetIOWrite, STAMTYPE_COUNTER, "/VMM/RZRet/IOWrite", STAMUNIT_OCCURENCES, "Number of VINF_IOM_HC_IOPORT_WRITE returns.");
330 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMMIORead, STAMTYPE_COUNTER, "/VMM/RZRet/MMIORead", STAMUNIT_OCCURENCES, "Number of VINF_IOM_HC_MMIO_READ returns.");
331 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMMIOWrite, STAMTYPE_COUNTER, "/VMM/RZRet/MMIOWrite", STAMUNIT_OCCURENCES, "Number of VINF_IOM_HC_MMIO_WRITE returns.");
332 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMMIOReadWrite, STAMTYPE_COUNTER, "/VMM/RZRet/MMIOReadWrite", STAMUNIT_OCCURENCES, "Number of VINF_IOM_HC_MMIO_READ_WRITE returns.");
333 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMMIOPatchRead, STAMTYPE_COUNTER, "/VMM/RZRet/MMIOPatchRead", STAMUNIT_OCCURENCES, "Number of VINF_IOM_HC_MMIO_PATCH_READ returns.");
334 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMMIOPatchWrite, STAMTYPE_COUNTER, "/VMM/RZRet/MMIOPatchWrite", STAMUNIT_OCCURENCES, "Number of VINF_IOM_HC_MMIO_PATCH_WRITE returns.");
335 STAM_REG(pVM, &pVM->vmm.s.StatRZRetLDTFault, STAMTYPE_COUNTER, "/VMM/RZRet/LDTFault", STAMUNIT_OCCURENCES, "Number of VINF_EM_EXECUTE_INSTRUCTION_GDT_FAULT returns.");
336 STAM_REG(pVM, &pVM->vmm.s.StatRZRetGDTFault, STAMTYPE_COUNTER, "/VMM/RZRet/GDTFault", STAMUNIT_OCCURENCES, "Number of VINF_EM_EXECUTE_INSTRUCTION_LDT_FAULT returns.");
337 STAM_REG(pVM, &pVM->vmm.s.StatRZRetIDTFault, STAMTYPE_COUNTER, "/VMM/RZRet/IDTFault", STAMUNIT_OCCURENCES, "Number of VINF_EM_EXECUTE_INSTRUCTION_IDT_FAULT returns.");
338 STAM_REG(pVM, &pVM->vmm.s.StatRZRetTSSFault, STAMTYPE_COUNTER, "/VMM/RZRet/TSSFault", STAMUNIT_OCCURENCES, "Number of VINF_EM_EXECUTE_INSTRUCTION_TSS_FAULT returns.");
339 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPDFault, STAMTYPE_COUNTER, "/VMM/RZRet/PDFault", STAMUNIT_OCCURENCES, "Number of VINF_EM_EXECUTE_INSTRUCTION_PD_FAULT returns.");
340 STAM_REG(pVM, &pVM->vmm.s.StatRZRetCSAMTask, STAMTYPE_COUNTER, "/VMM/RZRet/CSAMTask", STAMUNIT_OCCURENCES, "Number of VINF_CSAM_PENDING_ACTION returns.");
341 STAM_REG(pVM, &pVM->vmm.s.StatRZRetSyncCR3, STAMTYPE_COUNTER, "/VMM/RZRet/SyncCR", STAMUNIT_OCCURENCES, "Number of VINF_PGM_SYNC_CR3 returns.");
342 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMisc, STAMTYPE_COUNTER, "/VMM/RZRet/Misc", STAMUNIT_OCCURENCES, "Number of misc returns.");
343 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPatchInt3, STAMTYPE_COUNTER, "/VMM/RZRet/PatchInt3", STAMUNIT_OCCURENCES, "Number of VINF_PATM_PATCH_INT3 returns.");
344 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPatchPF, STAMTYPE_COUNTER, "/VMM/RZRet/PatchPF", STAMUNIT_OCCURENCES, "Number of VINF_PATM_PATCH_TRAP_PF returns.");
345 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPatchGP, STAMTYPE_COUNTER, "/VMM/RZRet/PatchGP", STAMUNIT_OCCURENCES, "Number of VINF_PATM_PATCH_TRAP_GP returns.");
346 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPatchIretIRQ, STAMTYPE_COUNTER, "/VMM/RZRet/PatchIret", STAMUNIT_OCCURENCES, "Number of VINF_PATM_PENDING_IRQ_AFTER_IRET returns.");
347 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPageOverflow, STAMTYPE_COUNTER, "/VMM/RZRet/InvlpgOverflow", STAMUNIT_OCCURENCES, "Number of VERR_REM_FLUSHED_PAGES_OVERFLOW returns.");
348 STAM_REG(pVM, &pVM->vmm.s.StatRZRetRescheduleREM, STAMTYPE_COUNTER, "/VMM/RZRet/ScheduleREM", STAMUNIT_OCCURENCES, "Number of VINF_EM_RESCHEDULE_REM returns.");
349 STAM_REG(pVM, &pVM->vmm.s.StatRZRetToR3, STAMTYPE_COUNTER, "/VMM/RZRet/ToR3", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_TO_R3 returns.");
350 STAM_REG(pVM, &pVM->vmm.s.StatRZRetTimerPending, STAMTYPE_COUNTER, "/VMM/RZRet/TimerPending", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_TIMER_PENDING returns.");
351 STAM_REG(pVM, &pVM->vmm.s.StatRZRetInterruptPending, STAMTYPE_COUNTER, "/VMM/RZRet/InterruptPending", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_INTERRUPT_PENDING returns.");
352 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPATMDuplicateFn, STAMTYPE_COUNTER, "/VMM/RZRet/PATMDuplicateFn", STAMUNIT_OCCURENCES, "Number of VINF_PATM_DUPLICATE_FUNCTION returns.");
353 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPGMChangeMode, STAMTYPE_COUNTER, "/VMM/RZRet/PGMChangeMode", STAMUNIT_OCCURENCES, "Number of VINF_PGM_CHANGE_MODE returns.");
354 STAM_REG(pVM, &pVM->vmm.s.StatRZRetEmulHlt, STAMTYPE_COUNTER, "/VMM/RZRet/EmulHlt", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_EMULATE_INSTR_HLT returns.");
355 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPendingRequest, STAMTYPE_COUNTER, "/VMM/RZRet/PendingRequest", STAMUNIT_OCCURENCES, "Number of VINF_EM_PENDING_REQUEST returns.");
356
357 STAM_REG(pVM, &pVM->vmm.s.StatRZRetCallHost, STAMTYPE_COUNTER, "/VMM/RZCallR3/Misc", STAMUNIT_OCCURENCES, "Number of Other ring-3 calls.");
358 STAM_REG(pVM, &pVM->vmm.s.StatRZCallPDMLock, STAMTYPE_COUNTER, "/VMM/RZCallR3/PDMLock", STAMUNIT_OCCURENCES, "Number of VMMCALLHOST_PDM_LOCK calls.");
359 STAM_REG(pVM, &pVM->vmm.s.StatRZCallPDMQueueFlush, STAMTYPE_COUNTER, "/VMM/RZCallR3/PDMQueueFlush", STAMUNIT_OCCURENCES, "Number of VMMCALLHOST_PDM_QUEUE_FLUSH calls.");
360 STAM_REG(pVM, &pVM->vmm.s.StatRZCallPGMLock, STAMTYPE_COUNTER, "/VMM/RZCallR3/PGMLock", STAMUNIT_OCCURENCES, "Number of VMMCALLHOST_PGM_LOCK calls.");
361 STAM_REG(pVM, &pVM->vmm.s.StatRZCallPGMPoolGrow, STAMTYPE_COUNTER, "/VMM/RZCallR3/PGMPoolGrow", STAMUNIT_OCCURENCES, "Number of VMMCALLHOST_PGM_POOL_GROW calls.");
362 STAM_REG(pVM, &pVM->vmm.s.StatRZCallPGMMapChunk, STAMTYPE_COUNTER, "/VMM/RZCallR3/PGMMapChunk", STAMUNIT_OCCURENCES, "Number of VMMCALLHOST_PGM_MAP_CHUNK calls.");
363 STAM_REG(pVM, &pVM->vmm.s.StatRZCallPGMAllocHandy, STAMTYPE_COUNTER, "/VMM/RZCallR3/PGMAllocHandy", STAMUNIT_OCCURENCES, "Number of VMMCALLHOST_PGM_ALLOCATE_HANDY_PAGES calls.");
364 STAM_REG(pVM, &pVM->vmm.s.StatRZCallRemReplay, STAMTYPE_COUNTER, "/VMM/RZCallR3/REMReplay", STAMUNIT_OCCURENCES, "Number of VMMCALLHOST_REM_REPLAY_HANDLER_NOTIFICATIONS calls.");
365 STAM_REG(pVM, &pVM->vmm.s.StatRZCallLogFlush, STAMTYPE_COUNTER, "/VMM/RZCallR3/VMMLogFlush", STAMUNIT_OCCURENCES, "Number of VMMCALLHOST_VMM_LOGGER_FLUSH calls.");
366 STAM_REG(pVM, &pVM->vmm.s.StatRZCallVMSetError, STAMTYPE_COUNTER, "/VMM/RZCallR3/VMSetError", STAMUNIT_OCCURENCES, "Number of VMMCALLHOST_VM_SET_ERROR calls.");
367 STAM_REG(pVM, &pVM->vmm.s.StatRZCallVMSetRuntimeError, STAMTYPE_COUNTER, "/VMM/RZCallR3/VMRuntimeError", STAMUNIT_OCCURENCES, "Number of VMMCALLHOST_VM_SET_RUNTIME_ERROR calls.");
368}
369
370
371/**
372 * Initializes the per-VCPU VMM.
373 *
374 * @returns VBox status code.
375 * @param pVM The VM to operate on.
376 */
377VMMR3DECL(int) VMMR3InitCPU(PVM pVM)
378{
379 LogFlow(("VMMR3InitCPU\n"));
380 return VINF_SUCCESS;
381}
382
383
384/**
385 * Ring-3 init finalizing.
386 *
387 * @returns VBox status code.
388 * @param pVM The VM handle.
389 */
390VMMR3DECL(int) VMMR3InitFinalize(PVM pVM)
391{
392 int rc = VINF_SUCCESS;
393
394 for (VMCPUID idCpu = 0; idCpu < pVM->cCPUs; idCpu++)
395 {
396 PVMCPU pVCpu = &pVM->aCpus[idCpu];
397
398#ifdef VBOX_STRICT_VMM_STACK
399 /*
400 * Two inaccessible pages at each sides of the stack to catch over/under-flows.
401 */
402 memset(pVCpu->vmm.s.pbEMTStackR3 - PAGE_SIZE, 0xcc, PAGE_SIZE);
403 PGMMapSetPage(pVM, MMHyperR3ToRC(pVM, pVCpu->vmm.s.pbEMTStackR3 - PAGE_SIZE), PAGE_SIZE, 0);
404 RTMemProtect(pVCpu->vmm.s.pbEMTStackR3 - PAGE_SIZE, PAGE_SIZE, RTMEM_PROT_NONE);
405
406 memset(pVCpu->vmm.s.pbEMTStackR3 + VMM_STACK_SIZE, 0xcc, PAGE_SIZE);
407 PGMMapSetPage(pVM, MMHyperR3ToRC(pVM, pVCpu->vmm.s.pbEMTStackR3 + VMM_STACK_SIZE), PAGE_SIZE, 0);
408 RTMemProtect(pVCpu->vmm.s.pbEMTStackR3 + VMM_STACK_SIZE, PAGE_SIZE, RTMEM_PROT_NONE);
409#endif
410
411 /*
412 * Set page attributes to r/w for stack pages.
413 */
414 rc = PGMMapSetPage(pVM, pVCpu->vmm.s.pbEMTStackRC, VMM_STACK_SIZE, X86_PTE_P | X86_PTE_A | X86_PTE_D | X86_PTE_RW);
415 AssertRC(rc);
416 if (RT_FAILURE(rc))
417 break;
418 }
419 if (RT_SUCCESS(rc))
420 {
421 /*
422 * Create the EMT yield timer.
423 */
424 rc = TMR3TimerCreateInternal(pVM, TMCLOCK_REAL, vmmR3YieldEMT, NULL, "EMT Yielder", &pVM->vmm.s.pYieldTimer);
425 if (RT_SUCCESS(rc))
426 rc = TMTimerSetMillies(pVM->vmm.s.pYieldTimer, pVM->vmm.s.cYieldEveryMillies);
427 }
428
429#ifdef VBOX_WITH_NMI
430 /*
431 * Map the host APIC into GC - This is AMD/Intel + Host OS specific!
432 */
433 if (RT_SUCCESS(rc))
434 rc = PGMMap(pVM, pVM->vmm.s.GCPtrApicBase, 0xfee00000, PAGE_SIZE,
435 X86_PTE_P | X86_PTE_RW | X86_PTE_PWT | X86_PTE_PCD | X86_PTE_A | X86_PTE_D);
436#endif
437 return rc;
438}
439
440
441/**
442 * Initializes the R0 VMM.
443 *
444 * @returns VBox status code.
445 * @param pVM The VM to operate on.
446 */
447VMMR3DECL(int) VMMR3InitR0(PVM pVM)
448{
449 int rc;
450 PVMCPU pVCpu = VMMGetCpu(pVM);
451 Assert(pVCpu && pVCpu->idCpu == 0);
452
453 /*
454 * Initialize the ring-0 logger if we haven't done so yet.
455 */
456 if ( pVM->vmm.s.pR0LoggerR3
457 && !pVM->vmm.s.pR0LoggerR3->fCreated)
458 {
459 rc = VMMR3UpdateLoggers(pVM);
460 if (RT_FAILURE(rc))
461 return rc;
462 }
463
464 /*
465 * Call Ring-0 entry with init code.
466 */
467 for (;;)
468 {
469#ifdef NO_SUPCALLR0VMM
470 //rc = VERR_GENERAL_FAILURE;
471 rc = VINF_SUCCESS;
472#else
473 rc = SUPCallVMMR0Ex(pVM->pVMR0, 0 /* VCPU 0 */, VMMR0_DO_VMMR0_INIT, VMMGetSvnRev(), NULL);
474#endif
475 if ( pVM->vmm.s.pR0LoggerR3
476 && pVM->vmm.s.pR0LoggerR3->Logger.offScratch > 0)
477 RTLogFlushToLogger(&pVM->vmm.s.pR0LoggerR3->Logger, NULL);
478 if (rc != VINF_VMM_CALL_HOST)
479 break;
480 rc = vmmR3ServiceCallHostRequest(pVM, pVCpu);
481 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
482 break;
483 /* Resume R0 */
484 }
485
486 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
487 {
488 LogRel(("R0 init failed, rc=%Rra\n", rc));
489 if (RT_SUCCESS(rc))
490 rc = VERR_INTERNAL_ERROR;
491 }
492 return rc;
493}
494
495
496/**
497 * Initializes the RC VMM.
498 *
499 * @returns VBox status code.
500 * @param pVM The VM to operate on.
501 */
502VMMR3DECL(int) VMMR3InitRC(PVM pVM)
503{
504 PVMCPU pVCpu = VMMGetCpu(pVM);
505 Assert(pVCpu && pVCpu->idCpu == 0);
506
507 /* In VMX mode, there's no need to init RC. */
508 if (pVM->vmm.s.fSwitcherDisabled)
509 return VINF_SUCCESS;
510
511 AssertReturn(pVM->cCPUs == 1, VERR_RAW_MODE_INVALID_SMP);
512
513 /*
514 * Call VMMGCInit():
515 * -# resolve the address.
516 * -# setup stackframe and EIP to use the trampoline.
517 * -# do a generic hypervisor call.
518 */
519 RTRCPTR RCPtrEP;
520 int rc = PDMR3LdrGetSymbolRC(pVM, VMMGC_MAIN_MODULE_NAME, "VMMGCEntry", &RCPtrEP);
521 if (RT_SUCCESS(rc))
522 {
523 CPUMHyperSetCtxCore(pVCpu, NULL);
524 CPUMSetHyperESP(pVCpu, pVCpu->vmm.s.pbEMTStackBottomRC); /* Clear the stack. */
525 uint64_t u64TS = RTTimeProgramStartNanoTS();
526 CPUMPushHyper(pVCpu, (uint32_t)(u64TS >> 32)); /* Param 3: The program startup TS - Hi. */
527 CPUMPushHyper(pVCpu, (uint32_t)u64TS); /* Param 3: The program startup TS - Lo. */
528 CPUMPushHyper(pVCpu, VMMGetSvnRev()); /* Param 2: Version argument. */
529 CPUMPushHyper(pVCpu, VMMGC_DO_VMMGC_INIT); /* Param 1: Operation. */
530 CPUMPushHyper(pVCpu, pVM->pVMRC); /* Param 0: pVM */
531 CPUMPushHyper(pVCpu, 5 * sizeof(RTRCPTR)); /* trampoline param: stacksize. */
532 CPUMPushHyper(pVCpu, RCPtrEP); /* Call EIP. */
533 CPUMSetHyperEIP(pVCpu, pVM->vmm.s.pfnCallTrampolineRC);
534 Assert(CPUMGetHyperCR3(pVCpu) && CPUMGetHyperCR3(pVCpu) == PGMGetHyperCR3(pVCpu));
535
536 for (;;)
537 {
538#ifdef NO_SUPCALLR0VMM
539 //rc = VERR_GENERAL_FAILURE;
540 rc = VINF_SUCCESS;
541#else
542 rc = SUPCallVMMR0(pVM->pVMR0, 0 /* VCPU 0 */, VMMR0_DO_CALL_HYPERVISOR, NULL);
543#endif
544#ifdef LOG_ENABLED
545 PRTLOGGERRC pLogger = pVM->vmm.s.pRCLoggerR3;
546 if ( pLogger
547 && pLogger->offScratch > 0)
548 RTLogFlushRC(NULL, pLogger);
549#endif
550#ifdef VBOX_WITH_RC_RELEASE_LOGGING
551 PRTLOGGERRC pRelLogger = pVM->vmm.s.pRCRelLoggerR3;
552 if (RT_UNLIKELY(pRelLogger && pRelLogger->offScratch > 0))
553 RTLogFlushRC(RTLogRelDefaultInstance(), pRelLogger);
554#endif
555 if (rc != VINF_VMM_CALL_HOST)
556 break;
557 rc = vmmR3ServiceCallHostRequest(pVM, pVCpu);
558 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
559 break;
560 }
561
562 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
563 {
564 VMMR3FatalDump(pVM, pVCpu, rc);
565 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
566 rc = VERR_INTERNAL_ERROR;
567 }
568 AssertRC(rc);
569 }
570 return rc;
571}
572
573
574/**
575 * Terminate the VMM bits.
576 *
577 * @returns VINF_SUCCESS.
578 * @param pVM The VM handle.
579 */
580VMMR3DECL(int) VMMR3Term(PVM pVM)
581{
582 PVMCPU pVCpu = VMMGetCpu(pVM);
583 Assert(pVCpu && pVCpu->idCpu == 0);
584
585 /*
586 * Call Ring-0 entry with termination code.
587 */
588 int rc;
589 for (;;)
590 {
591#ifdef NO_SUPCALLR0VMM
592 //rc = VERR_GENERAL_FAILURE;
593 rc = VINF_SUCCESS;
594#else
595 rc = SUPCallVMMR0Ex(pVM->pVMR0, 0 /* VCPU 0 */, VMMR0_DO_VMMR0_TERM, 0, NULL);
596#endif
597 if ( pVM->vmm.s.pR0LoggerR3
598 && pVM->vmm.s.pR0LoggerR3->Logger.offScratch > 0)
599 RTLogFlushToLogger(&pVM->vmm.s.pR0LoggerR3->Logger, NULL);
600 if (rc != VINF_VMM_CALL_HOST)
601 break;
602 rc = vmmR3ServiceCallHostRequest(pVM, pVCpu);
603 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
604 break;
605 /* Resume R0 */
606 }
607 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
608 {
609 LogRel(("VMMR3Term: R0 term failed, rc=%Rra. (warning)\n", rc));
610 if (RT_SUCCESS(rc))
611 rc = VERR_INTERNAL_ERROR;
612 }
613
614#ifdef VBOX_STRICT_VMM_STACK
615 /*
616 * Make the two stack guard pages present again.
617 */
618 RTMemProtect(pVM->vmm.s.pbEMTStackR3 - PAGE_SIZE, PAGE_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
619 RTMemProtect(pVM->vmm.s.pbEMTStackR3 + VMM_STACK_SIZE, PAGE_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
620#endif
621 return rc;
622}
623
624
625/**
626 * Terminates the per-VCPU VMM.
627 *
628 * Termination means cleaning up and freeing all resources,
629 * the VM it self is at this point powered off or suspended.
630 *
631 * @returns VBox status code.
632 * @param pVM The VM to operate on.
633 */
634VMMR3DECL(int) VMMR3TermCPU(PVM pVM)
635{
636 return VINF_SUCCESS;
637}
638
639
640/**
641 * Applies relocations to data and code managed by this
642 * component. This function will be called at init and
643 * whenever the VMM need to relocate it self inside the GC.
644 *
645 * The VMM will need to apply relocations to the core code.
646 *
647 * @param pVM The VM handle.
648 * @param offDelta The relocation delta.
649 */
650VMMR3DECL(void) VMMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
651{
652 LogFlow(("VMMR3Relocate: offDelta=%RGv\n", offDelta));
653
654 /*
655 * Recalc the RC address.
656 */
657 pVM->vmm.s.pvCoreCodeRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pvCoreCodeR3);
658
659 /*
660 * The stack.
661 */
662 for (VMCPUID i = 0; i < pVM->cCPUs; i++)
663 {
664 PVMCPU pVCpu = &pVM->aCpus[i];
665
666 CPUMSetHyperESP(pVCpu, CPUMGetHyperESP(pVCpu) + offDelta);
667
668 pVCpu->vmm.s.pbEMTStackRC = MMHyperR3ToRC(pVM, pVCpu->vmm.s.pbEMTStackR3);
669 pVCpu->vmm.s.pbEMTStackBottomRC = pVCpu->vmm.s.pbEMTStackRC + VMM_STACK_SIZE;
670 }
671
672 /*
673 * All the switchers.
674 */
675 vmmR3SwitcherRelocate(pVM, offDelta);
676
677 /*
678 * Get other RC entry points.
679 */
680 int rc = PDMR3LdrGetSymbolRC(pVM, VMMGC_MAIN_MODULE_NAME, "CPUMGCResumeGuest", &pVM->vmm.s.pfnCPUMRCResumeGuest);
681 AssertReleaseMsgRC(rc, ("CPUMGCResumeGuest not found! rc=%Rra\n", rc));
682
683 rc = PDMR3LdrGetSymbolRC(pVM, VMMGC_MAIN_MODULE_NAME, "CPUMGCResumeGuestV86", &pVM->vmm.s.pfnCPUMRCResumeGuestV86);
684 AssertReleaseMsgRC(rc, ("CPUMGCResumeGuestV86 not found! rc=%Rra\n", rc));
685
686 /*
687 * Update the logger.
688 */
689 VMMR3UpdateLoggers(pVM);
690}
691
692
693/**
694 * Updates the settings for the RC and R0 loggers.
695 *
696 * @returns VBox status code.
697 * @param pVM The VM handle.
698 */
699VMMR3DECL(int) VMMR3UpdateLoggers(PVM pVM)
700{
701 /*
702 * Simply clone the logger instance (for RC).
703 */
704 int rc = VINF_SUCCESS;
705 RTRCPTR RCPtrLoggerFlush = 0;
706
707 if (pVM->vmm.s.pRCLoggerR3
708#ifdef VBOX_WITH_RC_RELEASE_LOGGING
709 || pVM->vmm.s.pRCRelLoggerR3
710#endif
711 )
712 {
713 rc = PDMR3LdrGetSymbolRC(pVM, VMMGC_MAIN_MODULE_NAME, "vmmGCLoggerFlush", &RCPtrLoggerFlush);
714 AssertReleaseMsgRC(rc, ("vmmGCLoggerFlush not found! rc=%Rra\n", rc));
715 }
716
717 if (pVM->vmm.s.pRCLoggerR3)
718 {
719 RTRCPTR RCPtrLoggerWrapper = 0;
720 rc = PDMR3LdrGetSymbolRC(pVM, VMMGC_MAIN_MODULE_NAME, "vmmGCLoggerWrapper", &RCPtrLoggerWrapper);
721 AssertReleaseMsgRC(rc, ("vmmGCLoggerWrapper not found! rc=%Rra\n", rc));
722
723 pVM->vmm.s.pRCLoggerRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pRCLoggerR3);
724 rc = RTLogCloneRC(NULL /* default */, pVM->vmm.s.pRCLoggerR3, pVM->vmm.s.cbRCLogger,
725 RCPtrLoggerWrapper, RCPtrLoggerFlush, RTLOGFLAGS_BUFFERED);
726 AssertReleaseMsgRC(rc, ("RTLogCloneRC failed! rc=%Rra\n", rc));
727 }
728
729#ifdef VBOX_WITH_RC_RELEASE_LOGGING
730 if (pVM->vmm.s.pRCRelLoggerR3)
731 {
732 RTRCPTR RCPtrLoggerWrapper = 0;
733 rc = PDMR3LdrGetSymbolRC(pVM, VMMGC_MAIN_MODULE_NAME, "vmmGCRelLoggerWrapper", &RCPtrLoggerWrapper);
734 AssertReleaseMsgRC(rc, ("vmmGCRelLoggerWrapper not found! rc=%Rra\n", rc));
735
736 pVM->vmm.s.pRCRelLoggerRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pRCRelLoggerR3);
737 rc = RTLogCloneRC(RTLogRelDefaultInstance(), pVM->vmm.s.pRCRelLoggerR3, pVM->vmm.s.cbRCRelLogger,
738 RCPtrLoggerWrapper, RCPtrLoggerFlush, RTLOGFLAGS_BUFFERED);
739 AssertReleaseMsgRC(rc, ("RTLogCloneRC failed! rc=%Rra\n", rc));
740 }
741#endif /* VBOX_WITH_RC_RELEASE_LOGGING */
742
743 /*
744 * For the ring-0 EMT logger, we use a per-thread logger instance
745 * in ring-0. Only initialize it once.
746 */
747 PVMMR0LOGGER pR0LoggerR3 = pVM->vmm.s.pR0LoggerR3;
748 if (pR0LoggerR3)
749 {
750 if (!pR0LoggerR3->fCreated)
751 {
752 RTR0PTR pfnLoggerWrapper = NIL_RTR0PTR;
753 rc = PDMR3LdrGetSymbolR0(pVM, VMMR0_MAIN_MODULE_NAME, "vmmR0LoggerWrapper", &pfnLoggerWrapper);
754 AssertReleaseMsgRCReturn(rc, ("VMMLoggerWrapper not found! rc=%Rra\n", rc), rc);
755
756 RTR0PTR pfnLoggerFlush = NIL_RTR0PTR;
757 rc = PDMR3LdrGetSymbolR0(pVM, VMMR0_MAIN_MODULE_NAME, "vmmR0LoggerFlush", &pfnLoggerFlush);
758 AssertReleaseMsgRCReturn(rc, ("VMMLoggerFlush not found! rc=%Rra\n", rc), rc);
759
760 rc = RTLogCreateForR0(&pR0LoggerR3->Logger, pR0LoggerR3->cbLogger,
761 *(PFNRTLOGGER *)&pfnLoggerWrapper, *(PFNRTLOGFLUSH *)&pfnLoggerFlush,
762 RTLOGFLAGS_BUFFERED, RTLOGDEST_DUMMY);
763 AssertReleaseMsgRCReturn(rc, ("RTLogCreateForR0 failed! rc=%Rra\n", rc), rc);
764 pR0LoggerR3->fCreated = true;
765 pR0LoggerR3->fFlushingDisabled = false;
766 }
767
768 rc = RTLogCopyGroupsAndFlags(&pR0LoggerR3->Logger, NULL /* default */, pVM->vmm.s.pRCLoggerR3->fFlags, RTLOGFLAGS_BUFFERED);
769 AssertRC(rc);
770 }
771
772 return rc;
773}
774
775
776/**
777 * Gets the pointer to a buffer containing the R0/RC AssertMsg1 output.
778 *
779 * @returns Pointer to the buffer.
780 * @param pVM The VM handle.
781 */
782VMMR3DECL(const char *) VMMR3GetRZAssertMsg1(PVM pVM)
783{
784 if (HWACCMIsEnabled(pVM))
785 return pVM->vmm.s.szRing0AssertMsg1;
786
787 RTRCPTR RCPtr;
788 int rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_szRTAssertMsg1", &RCPtr);
789 if (RT_SUCCESS(rc))
790 return (const char *)MMHyperRCToR3(pVM, RCPtr);
791
792 return NULL;
793}
794
795
796/**
797 * Gets the pointer to a buffer containing the R0/RC AssertMsg2 output.
798 *
799 * @returns Pointer to the buffer.
800 * @param pVM The VM handle.
801 */
802VMMR3DECL(const char *) VMMR3GetRZAssertMsg2(PVM pVM)
803{
804 if (HWACCMIsEnabled(pVM))
805 return pVM->vmm.s.szRing0AssertMsg2;
806
807 RTRCPTR RCPtr;
808 int rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_szRTAssertMsg2", &RCPtr);
809 if (RT_SUCCESS(rc))
810 return (const char *)MMHyperRCToR3(pVM, RCPtr);
811
812 return NULL;
813}
814
815
816/**
817 * Execute state save operation.
818 *
819 * @returns VBox status code.
820 * @param pVM VM Handle.
821 * @param pSSM SSM operation handle.
822 */
823static DECLCALLBACK(int) vmmR3Save(PVM pVM, PSSMHANDLE pSSM)
824{
825 LogFlow(("vmmR3Save:\n"));
826
827 /*
828 * The hypervisor stack.
829 * Note! See note in vmmR3Load (remove this on version change).
830 */
831 PVMCPU pVCpu0 = &pVM->aCpus[0];
832 SSMR3PutRCPtr(pSSM, pVCpu0->vmm.s.pbEMTStackBottomRC);
833 RTRCPTR RCPtrESP = CPUMGetHyperESP(pVCpu0);
834 AssertMsg(pVCpu0->vmm.s.pbEMTStackBottomRC - RCPtrESP <= VMM_STACK_SIZE, ("Bottom %RRv ESP=%RRv\n", pVCpu0->vmm.s.pbEMTStackBottomRC, RCPtrESP));
835 SSMR3PutRCPtr(pSSM, RCPtrESP);
836 SSMR3PutMem(pSSM, pVCpu0->vmm.s.pbEMTStackR3, VMM_STACK_SIZE);
837
838 /*
839 * Save the started/stopped state of all CPUs except 0 as it will always
840 * be running. This avoids breaking the saved state version. :-)
841 */
842 for (VMCPUID i = 1; i < pVM->cCPUs; i++)
843 SSMR3PutBool(pSSM, VMCPUSTATE_IS_STARTED(VMCPU_GET_STATE(&pVM->aCpus[i])));
844
845 return SSMR3PutU32(pSSM, ~0); /* terminator */
846}
847
848
849/**
850 * Execute state load operation.
851 *
852 * @returns VBox status code.
853 * @param pVM VM Handle.
854 * @param pSSM SSM operation handle.
855 * @param u32Version Data layout version.
856 */
857static DECLCALLBACK(int) vmmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version)
858{
859 LogFlow(("vmmR3Load:\n"));
860
861 /*
862 * Validate version.
863 */
864 if (u32Version != VMM_SAVED_STATE_VERSION)
865 {
866 AssertMsgFailed(("vmmR3Load: Invalid version u32Version=%d!\n", u32Version));
867 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
868 }
869
870 /*
871 * Check that the stack is in the same place, or that it's fearly empty.
872 *
873 * Note! This can be skipped next time we update saved state as we will
874 * never be in a R0/RC -> ring-3 call when saving the state. The
875 * stack and the two associated pointers are not required.
876 */
877 RTRCPTR RCPtrStackBottom;
878 SSMR3GetRCPtr(pSSM, &RCPtrStackBottom);
879 RTRCPTR RCPtrESP;
880 int rc = SSMR3GetRCPtr(pSSM, &RCPtrESP);
881 if (RT_FAILURE(rc))
882 return rc;
883 SSMR3GetMem(pSSM, pVM->aCpus[0].vmm.s.pbEMTStackR3, VMM_STACK_SIZE);
884
885 /* Restore the VMCPU states. VCPU 0 is always started. */
886 VMCPU_SET_STATE(&pVM->aCpus[0], VMCPUSTATE_STARTED);
887 for (VMCPUID i = 1; i < pVM->cCPUs; i++)
888 {
889 bool fStarted;
890 rc = SSMR3GetBool(pSSM, &fStarted);
891 if (RT_FAILURE(rc))
892 return rc;
893 VMCPU_SET_STATE(&pVM->aCpus[i], fStarted ? VMCPUSTATE_STARTED : VMCPUSTATE_STOPPED);
894 }
895
896 /* terminator */
897 uint32_t u32;
898 rc = SSMR3GetU32(pSSM, &u32);
899 if (RT_FAILURE(rc))
900 return rc;
901 if (u32 != ~0U)
902 {
903 AssertMsgFailed(("u32=%#x\n", u32));
904 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
905 }
906 return VINF_SUCCESS;
907}
908
909
910/**
911 * Resolve a builtin RC symbol.
912 *
913 * Called by PDM when loading or relocating RC modules.
914 *
915 * @returns VBox status
916 * @param pVM VM Handle.
917 * @param pszSymbol Symbol to resolv
918 * @param pRCPtrValue Where to store the symbol value.
919 *
920 * @remark This has to work before VMMR3Relocate() is called.
921 */
922VMMR3DECL(int) VMMR3GetImportRC(PVM pVM, const char *pszSymbol, PRTRCPTR pRCPtrValue)
923{
924 if (!strcmp(pszSymbol, "g_Logger"))
925 {
926 if (pVM->vmm.s.pRCLoggerR3)
927 pVM->vmm.s.pRCLoggerRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pRCLoggerR3);
928 *pRCPtrValue = pVM->vmm.s.pRCLoggerRC;
929 }
930 else if (!strcmp(pszSymbol, "g_RelLogger"))
931 {
932#ifdef VBOX_WITH_RC_RELEASE_LOGGING
933 if (pVM->vmm.s.pRCRelLoggerR3)
934 pVM->vmm.s.pRCRelLoggerRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pRCRelLoggerR3);
935 *pRCPtrValue = pVM->vmm.s.pRCRelLoggerRC;
936#else
937 *pRCPtrValue = NIL_RTRCPTR;
938#endif
939 }
940 else
941 return VERR_SYMBOL_NOT_FOUND;
942 return VINF_SUCCESS;
943}
944
945
946/**
947 * Suspends the CPU yielder.
948 *
949 * @param pVM The VM handle.
950 */
951VMMR3DECL(void) VMMR3YieldSuspend(PVM pVM)
952{
953 if (!pVM->vmm.s.cYieldResumeMillies)
954 {
955 uint64_t u64Now = TMTimerGet(pVM->vmm.s.pYieldTimer);
956 uint64_t u64Expire = TMTimerGetExpire(pVM->vmm.s.pYieldTimer);
957 if (u64Now >= u64Expire || u64Expire == ~(uint64_t)0)
958 pVM->vmm.s.cYieldResumeMillies = pVM->vmm.s.cYieldEveryMillies;
959 else
960 pVM->vmm.s.cYieldResumeMillies = TMTimerToMilli(pVM->vmm.s.pYieldTimer, u64Expire - u64Now);
961 TMTimerStop(pVM->vmm.s.pYieldTimer);
962 }
963 pVM->vmm.s.u64LastYield = RTTimeNanoTS();
964}
965
966
967/**
968 * Stops the CPU yielder.
969 *
970 * @param pVM The VM handle.
971 */
972VMMR3DECL(void) VMMR3YieldStop(PVM pVM)
973{
974 if (!pVM->vmm.s.cYieldResumeMillies)
975 TMTimerStop(pVM->vmm.s.pYieldTimer);
976 pVM->vmm.s.cYieldResumeMillies = pVM->vmm.s.cYieldEveryMillies;
977 pVM->vmm.s.u64LastYield = RTTimeNanoTS();
978}
979
980
981/**
982 * Resumes the CPU yielder when it has been a suspended or stopped.
983 *
984 * @param pVM The VM handle.
985 */
986VMMR3DECL(void) VMMR3YieldResume(PVM pVM)
987{
988 if (pVM->vmm.s.cYieldResumeMillies)
989 {
990 TMTimerSetMillies(pVM->vmm.s.pYieldTimer, pVM->vmm.s.cYieldResumeMillies);
991 pVM->vmm.s.cYieldResumeMillies = 0;
992 }
993}
994
995
996/**
997 * Internal timer callback function.
998 *
999 * @param pVM The VM.
1000 * @param pTimer The timer handle.
1001 * @param pvUser User argument specified upon timer creation.
1002 */
1003static DECLCALLBACK(void) vmmR3YieldEMT(PVM pVM, PTMTIMER pTimer, void *pvUser)
1004{
1005 /*
1006 * This really needs some careful tuning. While we shouldn't be too greedy since
1007 * that'll cause the rest of the system to stop up, we shouldn't be too nice either
1008 * because that'll cause us to stop up.
1009 *
1010 * The current logic is to use the default interval when there is no lag worth
1011 * mentioning, but when we start accumulating lag we don't bother yielding at all.
1012 *
1013 * (This depends on the TMCLOCK_VIRTUAL_SYNC to be scheduled before TMCLOCK_REAL
1014 * so the lag is up to date.)
1015 */
1016 const uint64_t u64Lag = TMVirtualSyncGetLag(pVM);
1017 if ( u64Lag < 50000000 /* 50ms */
1018 || ( u64Lag < 1000000000 /* 1s */
1019 && RTTimeNanoTS() - pVM->vmm.s.u64LastYield < 500000000 /* 500 ms */)
1020 )
1021 {
1022 uint64_t u64Elapsed = RTTimeNanoTS();
1023 pVM->vmm.s.u64LastYield = u64Elapsed;
1024
1025 RTThreadYield();
1026
1027#ifdef LOG_ENABLED
1028 u64Elapsed = RTTimeNanoTS() - u64Elapsed;
1029 Log(("vmmR3YieldEMT: %RI64 ns\n", u64Elapsed));
1030#endif
1031 }
1032 TMTimerSetMillies(pTimer, pVM->vmm.s.cYieldEveryMillies);
1033}
1034
1035
1036/**
1037 * Executes guest code in the raw-mode context.
1038 *
1039 * @param pVM VM handle.
1040 * @param pVCpu The VMCPU to operate on.
1041 */
1042VMMR3DECL(int) VMMR3RawRunGC(PVM pVM, PVMCPU pVCpu)
1043{
1044 Log2(("VMMR3RawRunGC: (cs:eip=%04x:%08x)\n", CPUMGetGuestCS(pVCpu), CPUMGetGuestEIP(pVCpu)));
1045
1046 AssertReturn(pVM->cCPUs == 1, VERR_RAW_MODE_INVALID_SMP);
1047
1048 /*
1049 * Set the EIP and ESP.
1050 */
1051 CPUMSetHyperEIP(pVCpu, CPUMGetGuestEFlags(pVCpu) & X86_EFL_VM
1052 ? pVM->vmm.s.pfnCPUMRCResumeGuestV86
1053 : pVM->vmm.s.pfnCPUMRCResumeGuest);
1054 CPUMSetHyperESP(pVCpu, pVCpu->vmm.s.pbEMTStackBottomRC);
1055
1056 /*
1057 * We hide log flushes (outer) and hypervisor interrupts (inner).
1058 */
1059 for (;;)
1060 {
1061 Assert(CPUMGetHyperCR3(pVCpu) && CPUMGetHyperCR3(pVCpu) == PGMGetHyperCR3(pVCpu));
1062#ifdef VBOX_STRICT
1063 PGMMapCheck(pVM);
1064#endif
1065 int rc;
1066 do
1067 {
1068#ifdef NO_SUPCALLR0VMM
1069 rc = VERR_GENERAL_FAILURE;
1070#else
1071 rc = SUPCallVMMR0Fast(pVM->pVMR0, VMMR0_DO_RAW_RUN, 0);
1072 if (RT_LIKELY(rc == VINF_SUCCESS))
1073 rc = pVCpu->vmm.s.iLastGZRc;
1074#endif
1075 } while (rc == VINF_EM_RAW_INTERRUPT_HYPER);
1076
1077 /*
1078 * Flush the logs.
1079 */
1080#ifdef LOG_ENABLED
1081 PRTLOGGERRC pLogger = pVM->vmm.s.pRCLoggerR3;
1082 if ( pLogger
1083 && pLogger->offScratch > 0)
1084 RTLogFlushRC(NULL, pLogger);
1085#endif
1086#ifdef VBOX_WITH_RC_RELEASE_LOGGING
1087 PRTLOGGERRC pRelLogger = pVM->vmm.s.pRCRelLoggerR3;
1088 if (RT_UNLIKELY(pRelLogger && pRelLogger->offScratch > 0))
1089 RTLogFlushRC(RTLogRelDefaultInstance(), pRelLogger);
1090#endif
1091 if (rc != VINF_VMM_CALL_HOST)
1092 {
1093 Log2(("VMMR3RawRunGC: returns %Rrc (cs:eip=%04x:%08x)\n", rc, CPUMGetGuestCS(pVCpu), CPUMGetGuestEIP(pVCpu)));
1094 return rc;
1095 }
1096 rc = vmmR3ServiceCallHostRequest(pVM, pVCpu);
1097 if (RT_FAILURE(rc))
1098 return rc;
1099 /* Resume GC */
1100 }
1101}
1102
1103
1104/**
1105 * Executes guest code (Intel VT-x and AMD-V).
1106 *
1107 * @param pVM VM handle.
1108 * @param pVCpu The VMCPU to operate on.
1109 */
1110VMMR3DECL(int) VMMR3HwAccRunGC(PVM pVM, PVMCPU pVCpu)
1111{
1112 Log2(("VMMR3HwAccRunGC: (cs:eip=%04x:%08x)\n", CPUMGetGuestCS(pVCpu), CPUMGetGuestEIP(pVCpu)));
1113
1114 for (;;)
1115 {
1116 int rc;
1117 do
1118 {
1119#ifdef NO_SUPCALLR0VMM
1120 rc = VERR_GENERAL_FAILURE;
1121#else
1122 rc = SUPCallVMMR0Fast(pVM->pVMR0, VMMR0_DO_HWACC_RUN, pVCpu->idCpu);
1123 if (RT_LIKELY(rc == VINF_SUCCESS))
1124 rc = pVCpu->vmm.s.iLastGZRc;
1125#endif
1126 } while (rc == VINF_EM_RAW_INTERRUPT_HYPER);
1127
1128#ifdef LOG_ENABLED
1129 /*
1130 * Flush the log
1131 */
1132 PVMMR0LOGGER pR0LoggerR3 = pVM->vmm.s.pR0LoggerR3;
1133 if ( pR0LoggerR3
1134 && pR0LoggerR3->Logger.offScratch > 0)
1135 RTLogFlushToLogger(&pR0LoggerR3->Logger, NULL);
1136#endif /* !LOG_ENABLED */
1137 if (rc != VINF_VMM_CALL_HOST)
1138 {
1139 Log2(("VMMR3HwAccRunGC: returns %Rrc (cs:eip=%04x:%08x)\n", rc, CPUMGetGuestCS(pVCpu), CPUMGetGuestEIP(pVCpu)));
1140 return rc;
1141 }
1142 rc = vmmR3ServiceCallHostRequest(pVM, pVCpu);
1143 if (RT_FAILURE(rc))
1144 return rc;
1145 /* Resume R0 */
1146 }
1147}
1148
1149
1150/**
1151 * Calls a RC function.
1152 *
1153 * @param pVM The VM handle.
1154 * @param RCPtrEntry The address of the RC function.
1155 * @param cArgs The number of arguments in the ....
1156 * @param ... Arguments to the function.
1157 */
1158VMMR3DECL(int) VMMR3CallRC(PVM pVM, RTRCPTR RCPtrEntry, unsigned cArgs, ...)
1159{
1160 va_list args;
1161 va_start(args, cArgs);
1162 int rc = VMMR3CallRCV(pVM, RCPtrEntry, cArgs, args);
1163 va_end(args);
1164 return rc;
1165}
1166
1167
1168/**
1169 * Calls a RC function.
1170 *
1171 * @param pVM The VM handle.
1172 * @param RCPtrEntry The address of the RC function.
1173 * @param cArgs The number of arguments in the ....
1174 * @param args Arguments to the function.
1175 */
1176VMMR3DECL(int) VMMR3CallRCV(PVM pVM, RTRCPTR RCPtrEntry, unsigned cArgs, va_list args)
1177{
1178 /* Raw mode implies 1 VCPU. */
1179 AssertReturn(pVM->cCPUs == 1, VERR_RAW_MODE_INVALID_SMP);
1180 PVMCPU pVCpu = &pVM->aCpus[0];
1181
1182 Log2(("VMMR3CallGCV: RCPtrEntry=%RRv cArgs=%d\n", RCPtrEntry, cArgs));
1183
1184 /*
1185 * Setup the call frame using the trampoline.
1186 */
1187 CPUMHyperSetCtxCore(pVCpu, NULL);
1188 memset(pVCpu->vmm.s.pbEMTStackR3, 0xaa, VMM_STACK_SIZE); /* Clear the stack. */
1189 CPUMSetHyperESP(pVCpu, pVCpu->vmm.s.pbEMTStackBottomRC - cArgs * sizeof(RTGCUINTPTR32));
1190 PRTGCUINTPTR32 pFrame = (PRTGCUINTPTR32)(pVCpu->vmm.s.pbEMTStackR3 + VMM_STACK_SIZE) - cArgs;
1191 int i = cArgs;
1192 while (i-- > 0)
1193 *pFrame++ = va_arg(args, RTGCUINTPTR32);
1194
1195 CPUMPushHyper(pVCpu, cArgs * sizeof(RTGCUINTPTR32)); /* stack frame size */
1196 CPUMPushHyper(pVCpu, RCPtrEntry); /* what to call */
1197 CPUMSetHyperEIP(pVCpu, pVM->vmm.s.pfnCallTrampolineRC);
1198
1199 /*
1200 * We hide log flushes (outer) and hypervisor interrupts (inner).
1201 */
1202 for (;;)
1203 {
1204 int rc;
1205 Assert(CPUMGetHyperCR3(pVCpu) && CPUMGetHyperCR3(pVCpu) == PGMGetHyperCR3(pVCpu));
1206 do
1207 {
1208#ifdef NO_SUPCALLR0VMM
1209 rc = VERR_GENERAL_FAILURE;
1210#else
1211 rc = SUPCallVMMR0Fast(pVM->pVMR0, VMMR0_DO_RAW_RUN, 0);
1212 if (RT_LIKELY(rc == VINF_SUCCESS))
1213 rc = pVCpu->vmm.s.iLastGZRc;
1214#endif
1215 } while (rc == VINF_EM_RAW_INTERRUPT_HYPER);
1216
1217 /*
1218 * Flush the logs.
1219 */
1220#ifdef LOG_ENABLED
1221 PRTLOGGERRC pLogger = pVM->vmm.s.pRCLoggerR3;
1222 if ( pLogger
1223 && pLogger->offScratch > 0)
1224 RTLogFlushRC(NULL, pLogger);
1225#endif
1226#ifdef VBOX_WITH_RC_RELEASE_LOGGING
1227 PRTLOGGERRC pRelLogger = pVM->vmm.s.pRCRelLoggerR3;
1228 if (RT_UNLIKELY(pRelLogger && pRelLogger->offScratch > 0))
1229 RTLogFlushRC(RTLogRelDefaultInstance(), pRelLogger);
1230#endif
1231 if (rc == VERR_TRPM_PANIC || rc == VERR_TRPM_DONT_PANIC)
1232 VMMR3FatalDump(pVM, pVCpu, rc);
1233 if (rc != VINF_VMM_CALL_HOST)
1234 {
1235 Log2(("VMMR3CallGCV: returns %Rrc (cs:eip=%04x:%08x)\n", rc, CPUMGetGuestCS(pVCpu), CPUMGetGuestEIP(pVCpu)));
1236 return rc;
1237 }
1238 rc = vmmR3ServiceCallHostRequest(pVM, pVCpu);
1239 if (RT_FAILURE(rc))
1240 return rc;
1241 }
1242}
1243
1244
1245/**
1246 * Wrapper for SUPCallVMMR0Ex which will deal with
1247 * VINF_VMM_CALL_HOST returns.
1248 *
1249 * @returns VBox status code.
1250 * @param pVM The VM to operate on.
1251 * @param uOperation Operation to execute.
1252 * @param u64Arg Constant argument.
1253 * @param pReqHdr Pointer to a request header. See SUPCallVMMR0Ex for
1254 * details.
1255 */
1256VMMR3DECL(int) VMMR3CallR0(PVM pVM, uint32_t uOperation, uint64_t u64Arg, PSUPVMMR0REQHDR pReqHdr)
1257{
1258 PVMCPU pVCpu = VMMGetCpu(pVM);
1259 AssertReturn(pVCpu, VERR_VM_THREAD_NOT_EMT);
1260
1261 /*
1262 * Call Ring-0 entry with init code.
1263 */
1264 int rc;
1265 for (;;)
1266 {
1267#ifdef NO_SUPCALLR0VMM
1268 rc = VERR_GENERAL_FAILURE;
1269#else
1270 rc = SUPCallVMMR0Ex(pVM->pVMR0, pVCpu->idCpu, uOperation, u64Arg, pReqHdr);
1271#endif
1272 if ( pVM->vmm.s.pR0LoggerR3
1273 && pVM->vmm.s.pR0LoggerR3->Logger.offScratch > 0)
1274 RTLogFlushToLogger(&pVM->vmm.s.pR0LoggerR3->Logger, NULL);
1275 if (rc != VINF_VMM_CALL_HOST)
1276 break;
1277 rc = vmmR3ServiceCallHostRequest(pVM, pVCpu);
1278 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
1279 break;
1280 /* Resume R0 */
1281 }
1282
1283 AssertLogRelMsgReturn(rc == VINF_SUCCESS || VBOX_FAILURE(rc),
1284 ("uOperation=%u rc=%Rrc\n", uOperation, rc),
1285 VERR_INTERNAL_ERROR);
1286 return rc;
1287}
1288
1289
1290/**
1291 * Resumes executing hypervisor code when interrupted by a queue flush or a
1292 * debug event.
1293 *
1294 * @returns VBox status code.
1295 * @param pVM VM handle.
1296 * @param pVCpu VMCPU handle.
1297 */
1298VMMR3DECL(int) VMMR3ResumeHyper(PVM pVM, PVMCPU pVCpu)
1299{
1300 Log(("VMMR3ResumeHyper: eip=%RRv esp=%RRv\n", CPUMGetHyperEIP(pVCpu), CPUMGetHyperESP(pVCpu)));
1301 AssertReturn(pVM->cCPUs == 1, VERR_RAW_MODE_INVALID_SMP);
1302
1303 /*
1304 * We hide log flushes (outer) and hypervisor interrupts (inner).
1305 */
1306 for (;;)
1307 {
1308 int rc;
1309 Assert(CPUMGetHyperCR3(pVCpu) && CPUMGetHyperCR3(pVCpu) == PGMGetHyperCR3(pVCpu));
1310 do
1311 {
1312#ifdef NO_SUPCALLR0VMM
1313 rc = VERR_GENERAL_FAILURE;
1314#else
1315 rc = SUPCallVMMR0Fast(pVM->pVMR0, VMMR0_DO_RAW_RUN, 0);
1316 if (RT_LIKELY(rc == VINF_SUCCESS))
1317 rc = pVCpu->vmm.s.iLastGZRc;
1318#endif
1319 } while (rc == VINF_EM_RAW_INTERRUPT_HYPER);
1320
1321 /*
1322 * Flush the loggers,
1323 */
1324#ifdef LOG_ENABLED
1325 PRTLOGGERRC pLogger = pVM->vmm.s.pRCLoggerR3;
1326 if ( pLogger
1327 && pLogger->offScratch > 0)
1328 RTLogFlushRC(NULL, pLogger);
1329#endif
1330#ifdef VBOX_WITH_RC_RELEASE_LOGGING
1331 PRTLOGGERRC pRelLogger = pVM->vmm.s.pRCRelLoggerR3;
1332 if (RT_UNLIKELY(pRelLogger && pRelLogger->offScratch > 0))
1333 RTLogFlushRC(RTLogRelDefaultInstance(), pRelLogger);
1334#endif
1335 if (rc == VERR_TRPM_PANIC || rc == VERR_TRPM_DONT_PANIC)
1336 VMMR3FatalDump(pVM, pVCpu, rc);
1337 if (rc != VINF_VMM_CALL_HOST)
1338 {
1339 Log(("VMMR3ResumeHyper: returns %Rrc\n", rc));
1340 return rc;
1341 }
1342 rc = vmmR3ServiceCallHostRequest(pVM, pVCpu);
1343 if (RT_FAILURE(rc))
1344 return rc;
1345 }
1346}
1347
1348
1349/**
1350 * Service a call to the ring-3 host code.
1351 *
1352 * @returns VBox status code.
1353 * @param pVM VM handle.
1354 * @param pVCpu VMCPU handle
1355 * @remark Careful with critsects.
1356 */
1357static int vmmR3ServiceCallHostRequest(PVM pVM, PVMCPU pVCpu)
1358{
1359 switch (pVCpu->vmm.s.enmCallHostOperation)
1360 {
1361 /*
1362 * Acquire the PDM lock.
1363 */
1364 case VMMCALLHOST_PDM_LOCK:
1365 {
1366 pVCpu->vmm.s.rcCallHost = PDMR3LockCall(pVM);
1367 break;
1368 }
1369
1370 /*
1371 * Flush a PDM queue.
1372 */
1373 case VMMCALLHOST_PDM_QUEUE_FLUSH:
1374 {
1375 PDMR3QueueFlushWorker(pVM, NULL);
1376 pVCpu->vmm.s.rcCallHost = VINF_SUCCESS;
1377 break;
1378 }
1379
1380 /*
1381 * Grow the PGM pool.
1382 */
1383 case VMMCALLHOST_PGM_POOL_GROW:
1384 {
1385 pVCpu->vmm.s.rcCallHost = PGMR3PoolGrow(pVM);
1386 break;
1387 }
1388
1389 /*
1390 * Maps an page allocation chunk into ring-3 so ring-0 can use it.
1391 */
1392 case VMMCALLHOST_PGM_MAP_CHUNK:
1393 {
1394 pVCpu->vmm.s.rcCallHost = PGMR3PhysChunkMap(pVM, pVCpu->vmm.s.u64CallHostArg);
1395 break;
1396 }
1397
1398 /*
1399 * Allocates more handy pages.
1400 */
1401 case VMMCALLHOST_PGM_ALLOCATE_HANDY_PAGES:
1402 {
1403 pVCpu->vmm.s.rcCallHost = PGMR3PhysAllocateHandyPages(pVM);
1404 break;
1405 }
1406
1407 /*
1408 * Acquire the PGM lock.
1409 */
1410 case VMMCALLHOST_PGM_LOCK:
1411 {
1412 pVCpu->vmm.s.rcCallHost = PGMR3LockCall(pVM);
1413 break;
1414 }
1415
1416 /*
1417 * Flush REM handler notifications.
1418 */
1419 case VMMCALLHOST_REM_REPLAY_HANDLER_NOTIFICATIONS:
1420 {
1421 REMR3ReplayHandlerNotifications(pVM);
1422 pVCpu->vmm.s.rcCallHost = VINF_SUCCESS;
1423 break;
1424 }
1425
1426 /*
1427 * This is a noop. We just take this route to avoid unnecessary
1428 * tests in the loops.
1429 */
1430 case VMMCALLHOST_VMM_LOGGER_FLUSH:
1431 pVCpu->vmm.s.rcCallHost = VINF_SUCCESS;
1432 LogAlways(("*FLUSH*\n"));
1433 break;
1434
1435 /*
1436 * Set the VM error message.
1437 */
1438 case VMMCALLHOST_VM_SET_ERROR:
1439 VMR3SetErrorWorker(pVM);
1440 pVCpu->vmm.s.rcCallHost = VINF_SUCCESS;
1441 break;
1442
1443 /*
1444 * Set the VM runtime error message.
1445 */
1446 case VMMCALLHOST_VM_SET_RUNTIME_ERROR:
1447 pVCpu->vmm.s.rcCallHost = VMR3SetRuntimeErrorWorker(pVM);
1448 break;
1449
1450 /*
1451 * Signal a ring 0 hypervisor assertion.
1452 * Cancel the longjmp operation that's in progress.
1453 */
1454 case VMMCALLHOST_VM_R0_ASSERTION:
1455 pVCpu->vmm.s.enmCallHostOperation = VMMCALLHOST_INVALID;
1456 pVCpu->vmm.s.CallHostR0JmpBuf.fInRing3Call = false;
1457#ifdef RT_ARCH_X86
1458 pVCpu->vmm.s.CallHostR0JmpBuf.eip = 0;
1459#else
1460 pVCpu->vmm.s.CallHostR0JmpBuf.rip = 0;
1461#endif
1462 LogRel((pVM->vmm.s.szRing0AssertMsg1));
1463 LogRel((pVM->vmm.s.szRing0AssertMsg2));
1464 return VERR_VMM_RING0_ASSERTION;
1465
1466 /*
1467 * A forced switch to ring 0 for preemption purposes.
1468 */
1469 case VMMCALLHOST_VM_R0_PREEMPT:
1470 pVCpu->vmm.s.rcCallHost = VINF_SUCCESS;
1471 break;
1472
1473 default:
1474 AssertMsgFailed(("enmCallHostOperation=%d\n", pVCpu->vmm.s.enmCallHostOperation));
1475 return VERR_INTERNAL_ERROR;
1476 }
1477
1478 pVCpu->vmm.s.enmCallHostOperation = VMMCALLHOST_INVALID;
1479 return VINF_SUCCESS;
1480}
1481
1482
1483/**
1484 * Displays the Force action Flags.
1485 *
1486 * @param pVM The VM handle.
1487 * @param pHlp The output helpers.
1488 * @param pszArgs The additional arguments (ignored).
1489 */
1490static DECLCALLBACK(void) vmmR3InfoFF(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
1491{
1492 int c;
1493 uint32_t f;
1494#define PRINT_FLAG(prf,flag) do { \
1495 if (f & (prf##flag)) \
1496 { \
1497 static const char *s_psz = #flag; \
1498 if (!(c % 6)) \
1499 pHlp->pfnPrintf(pHlp, "%s\n %s", c ? "," : "", s_psz); \
1500 else \
1501 pHlp->pfnPrintf(pHlp, ", %s", s_psz); \
1502 c++; \
1503 f &= ~(prf##flag); \
1504 } \
1505 } while (0)
1506
1507#define PRINT_GROUP(prf,grp,sfx) do { \
1508 if (f & (prf##grp##sfx)) \
1509 { \
1510 static const char *s_psz = #grp; \
1511 if (!(c % 5)) \
1512 pHlp->pfnPrintf(pHlp, "%s %s", c ? ",\n" : " Groups:\n", s_psz); \
1513 else \
1514 pHlp->pfnPrintf(pHlp, ", %s", s_psz); \
1515 c++; \
1516 } \
1517 } while (0)
1518
1519 /*
1520 * The global flags.
1521 */
1522 const uint32_t fGlobalForcedActions = pVM->fGlobalForcedActions;
1523 pHlp->pfnPrintf(pHlp, "Global FFs: %#RX32", fGlobalForcedActions);
1524
1525 /* show the flag mnemonics */
1526 c = 0;
1527 f = fGlobalForcedActions;
1528 PRINT_FLAG(VM_FF_,TIMER);
1529 PRINT_FLAG(VM_FF_,PDM_QUEUES);
1530 PRINT_FLAG(VM_FF_,PDM_DMA);
1531 PRINT_FLAG(VM_FF_,PDM_CRITSECT);
1532 PRINT_FLAG(VM_FF_,DBGF);
1533 PRINT_FLAG(VM_FF_,REQUEST);
1534 PRINT_FLAG(VM_FF_,TERMINATE);
1535 PRINT_FLAG(VM_FF_,RESET);
1536 PRINT_FLAG(VM_FF_,PGM_NEED_HANDY_PAGES);
1537 PRINT_FLAG(VM_FF_,PGM_NO_MEMORY);
1538 PRINT_FLAG(VM_FF_,REM_HANDLER_NOTIFY);
1539 PRINT_FLAG(VM_FF_,DEBUG_SUSPEND);
1540 if (f)
1541 pHlp->pfnPrintf(pHlp, "%s\n Unknown bits: %#RX32\n", c ? "," : "", f);
1542 else
1543 pHlp->pfnPrintf(pHlp, "\n");
1544
1545 /* the groups */
1546 c = 0;
1547 f = fGlobalForcedActions;
1548 PRINT_GROUP(VM_FF_,EXTERNAL_SUSPENDED,_MASK);
1549 PRINT_GROUP(VM_FF_,EXTERNAL_HALTED,_MASK);
1550 PRINT_GROUP(VM_FF_,HIGH_PRIORITY_PRE,_MASK);
1551 PRINT_GROUP(VM_FF_,HIGH_PRIORITY_PRE_RAW,_MASK);
1552 PRINT_GROUP(VM_FF_,HIGH_PRIORITY_POST,_MASK);
1553 PRINT_GROUP(VM_FF_,NORMAL_PRIORITY_POST,_MASK);
1554 PRINT_GROUP(VM_FF_,NORMAL_PRIORITY,_MASK);
1555 PRINT_GROUP(VM_FF_,ALL_BUT_RAW,_MASK);
1556 if (c)
1557 pHlp->pfnPrintf(pHlp, "\n");
1558
1559 /*
1560 * Per CPU flags.
1561 */
1562 for (VMCPUID i = 0; i < pVM->cCPUs; i++)
1563 {
1564 const uint32_t fLocalForcedActions = pVM->aCpus[i].fLocalForcedActions;
1565 pHlp->pfnPrintf(pHlp, "CPU %u FFs: %#RX32", i, f);
1566
1567 /* show the flag mnemonics */
1568 c = 0;
1569 f = fLocalForcedActions;
1570 PRINT_FLAG(VMCPU_FF_,INTERRUPT_APIC);
1571 PRINT_FLAG(VMCPU_FF_,INTERRUPT_PIC);
1572 PRINT_FLAG(VMCPU_FF_,PGM_SYNC_CR3);
1573 PRINT_FLAG(VMCPU_FF_,PGM_SYNC_CR3_NON_GLOBAL);
1574 PRINT_FLAG(VMCPU_FF_,TRPM_SYNC_IDT);
1575 PRINT_FLAG(VMCPU_FF_,SELM_SYNC_TSS);
1576 PRINT_FLAG(VMCPU_FF_,SELM_SYNC_GDT);
1577 PRINT_FLAG(VMCPU_FF_,SELM_SYNC_LDT);
1578 PRINT_FLAG(VMCPU_FF_,INHIBIT_INTERRUPTS);
1579 PRINT_FLAG(VMCPU_FF_,CSAM_SCAN_PAGE);
1580 PRINT_FLAG(VMCPU_FF_,CSAM_PENDING_ACTION);
1581 PRINT_FLAG(VMCPU_FF_,TO_R3);
1582 if (f)
1583 pHlp->pfnPrintf(pHlp, "%s\n Unknown bits: %#RX32\n", c ? "," : "", f);
1584 else
1585 pHlp->pfnPrintf(pHlp, "\n");
1586
1587 /* the groups */
1588 c = 0;
1589 f = fLocalForcedActions;
1590 PRINT_GROUP(VMCPU_FF_,EXTERNAL_SUSPENDED,_MASK);
1591 PRINT_GROUP(VMCPU_FF_,EXTERNAL_HALTED,_MASK);
1592 PRINT_GROUP(VMCPU_FF_,HIGH_PRIORITY_PRE,_MASK);
1593 PRINT_GROUP(VMCPU_FF_,HIGH_PRIORITY_PRE_RAW,_MASK);
1594 PRINT_GROUP(VMCPU_FF_,HIGH_PRIORITY_POST,_MASK);
1595 PRINT_GROUP(VMCPU_FF_,NORMAL_PRIORITY_POST,_MASK);
1596 PRINT_GROUP(VMCPU_FF_,NORMAL_PRIORITY,_MASK);
1597 PRINT_GROUP(VMCPU_FF_,RESUME_GUEST,_MASK);
1598 PRINT_GROUP(VMCPU_FF_,HWACCM_TO_R3,_MASK);
1599 PRINT_GROUP(VMCPU_FF_,ALL_BUT_RAW,_MASK);
1600 if (c)
1601 pHlp->pfnPrintf(pHlp, "\n");
1602 }
1603
1604#undef PRINT_FLAG
1605#undef PRINT_GROUP
1606}
1607
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