VirtualBox

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

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

Removed global VMM lock.

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