VirtualBox

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

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

VMM: Clean out the VBOX_WITH_NEW_PHYS_CODE #ifdefs. (part 1)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 53.9 KB
Line 
1/* $Id: VMM.cpp 18665 2009-04-02 19:44:18Z 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 rc = RTCritSectInit(&pVM->vmm.s.CritSectVMLock);
185 if (RT_SUCCESS(rc))
186 {
187 /*
188 * Debug info and statistics.
189 */
190 DBGFR3InfoRegisterInternal(pVM, "ff", "Displays the current Forced actions Flags.", vmmR3InfoFF);
191 vmmR3InitRegisterStats(pVM);
192
193 return VINF_SUCCESS;
194 }
195 }
196 }
197 /** @todo: Need failure cleanup. */
198
199 //more todo in here?
200 //if (RT_SUCCESS(rc))
201 //{
202 //}
203 //int rc2 = vmmR3TermCoreCode(pVM);
204 //AssertRC(rc2));
205 }
206
207 return rc;
208}
209
210
211/**
212 * Allocate & setup the VMM RC stack(s) (for EMTs).
213 *
214 * The stacks are also used for long jumps in Ring-0.
215 *
216 * @returns VBox status code.
217 * @param pVM Pointer to the shared VM structure.
218 *
219 * @remarks The optional guard page gets it protection setup up during R3 init
220 * completion because of init order issues.
221 */
222static int vmmR3InitStacks(PVM pVM)
223{
224 /** @todo SMP: On stack per vCPU. */
225#ifdef VBOX_STRICT_VMM_STACK
226 int rc = MMR3HyperAllocOnceNoRel(pVM, VMM_STACK_SIZE + PAGE_SIZE + PAGE_SIZE, PAGE_SIZE, MM_TAG_VMM, (void **)&pVM->vmm.s.pbEMTStackR3);
227#else
228 int rc = MMR3HyperAllocOnceNoRel(pVM, VMM_STACK_SIZE, PAGE_SIZE, MM_TAG_VMM, (void **)&pVM->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 pVM->vmm.s.CallHostR0JmpBuf.pvSavedStack = NIL_RTR0PTR;
236 else
237#endif
238 pVM->vmm.s.CallHostR0JmpBuf.pvSavedStack = MMHyperR3ToR0(pVM, pVM->vmm.s.pbEMTStackR3);
239 pVM->vmm.s.pbEMTStackRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pbEMTStackR3);
240 pVM->vmm.s.pbEMTStackBottomRC = pVM->vmm.s.pbEMTStackRC + VMM_STACK_SIZE;
241 AssertRelease(pVM->vmm.s.pbEMTStackRC);
242
243 CPUMSetHyperESP(pVM, pVM->vmm.s.pbEMTStackBottomRC);
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, 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 /* In VMX mode, there's no need to init RC. */
493 if (pVM->vmm.s.fSwitcherDisabled)
494 return VINF_SUCCESS;
495
496 /*
497 * Call VMMGCInit():
498 * -# resolve the address.
499 * -# setup stackframe and EIP to use the trampoline.
500 * -# do a generic hypervisor call.
501 */
502 RTRCPTR RCPtrEP;
503 int rc = PDMR3LdrGetSymbolRC(pVM, VMMGC_MAIN_MODULE_NAME, "VMMGCEntry", &RCPtrEP);
504 if (RT_SUCCESS(rc))
505 {
506 CPUMHyperSetCtxCore(pVM, NULL);
507 CPUMSetHyperESP(pVM, pVM->vmm.s.pbEMTStackBottomRC); /* Clear the stack. */
508 uint64_t u64TS = RTTimeProgramStartNanoTS();
509 CPUMPushHyper(pVM, (uint32_t)(u64TS >> 32)); /* Param 3: The program startup TS - Hi. */
510 CPUMPushHyper(pVM, (uint32_t)u64TS); /* Param 3: The program startup TS - Lo. */
511 CPUMPushHyper(pVM, VMMGetSvnRev()); /* Param 2: Version argument. */
512 CPUMPushHyper(pVM, VMMGC_DO_VMMGC_INIT); /* Param 1: Operation. */
513 CPUMPushHyper(pVM, pVM->pVMRC); /* Param 0: pVM */
514 CPUMPushHyper(pVM, 5 * sizeof(RTRCPTR)); /* trampoline param: stacksize. */
515 CPUMPushHyper(pVM, RCPtrEP); /* Call EIP. */
516 CPUMSetHyperEIP(pVM, pVM->vmm.s.pfnCallTrampolineRC);
517 Assert(CPUMGetHyperCR3(pVM) && CPUMGetHyperCR3(pVM) == PGMGetHyperCR3(pVM));
518
519 for (;;)
520 {
521#ifdef NO_SUPCALLR0VMM
522 //rc = VERR_GENERAL_FAILURE;
523 rc = VINF_SUCCESS;
524#else
525 rc = SUPCallVMMR0(pVM->pVMR0, VMMR0_DO_CALL_HYPERVISOR, NULL);
526#endif
527#ifdef LOG_ENABLED
528 PRTLOGGERRC pLogger = pVM->vmm.s.pRCLoggerR3;
529 if ( pLogger
530 && pLogger->offScratch > 0)
531 RTLogFlushRC(NULL, pLogger);
532#endif
533#ifdef VBOX_WITH_RC_RELEASE_LOGGING
534 PRTLOGGERRC pRelLogger = pVM->vmm.s.pRCRelLoggerR3;
535 if (RT_UNLIKELY(pRelLogger && pRelLogger->offScratch > 0))
536 RTLogFlushRC(RTLogRelDefaultInstance(), pRelLogger);
537#endif
538 if (rc != VINF_VMM_CALL_HOST)
539 break;
540 rc = vmmR3ServiceCallHostRequest(pVM);
541 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
542 break;
543 }
544
545 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
546 {
547 VMMR3FatalDump(pVM, rc);
548 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
549 rc = VERR_INTERNAL_ERROR;
550 }
551 AssertRC(rc);
552 }
553 return rc;
554}
555
556
557/**
558 * Terminate the VMM bits.
559 *
560 * @returns VINF_SUCCESS.
561 * @param pVM The VM handle.
562 */
563VMMR3DECL(int) VMMR3Term(PVM pVM)
564{
565 /*
566 * Call Ring-0 entry with termination code.
567 */
568 int rc;
569 for (;;)
570 {
571#ifdef NO_SUPCALLR0VMM
572 //rc = VERR_GENERAL_FAILURE;
573 rc = VINF_SUCCESS;
574#else
575 rc = SUPCallVMMR0Ex(pVM->pVMR0, VMMR0_DO_VMMR0_TERM, 0, NULL);
576#endif
577 if ( pVM->vmm.s.pR0LoggerR3
578 && pVM->vmm.s.pR0LoggerR3->Logger.offScratch > 0)
579 RTLogFlushToLogger(&pVM->vmm.s.pR0LoggerR3->Logger, NULL);
580 if (rc != VINF_VMM_CALL_HOST)
581 break;
582 rc = vmmR3ServiceCallHostRequest(pVM);
583 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
584 break;
585 /* Resume R0 */
586 }
587 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
588 {
589 LogRel(("VMMR3Term: R0 term failed, rc=%Rra. (warning)\n", rc));
590 if (RT_SUCCESS(rc))
591 rc = VERR_INTERNAL_ERROR;
592 }
593
594#ifdef VBOX_STRICT_VMM_STACK
595 /*
596 * Make the two stack guard pages present again.
597 */
598 RTMemProtect(pVM->vmm.s.pbEMTStackR3 - PAGE_SIZE, PAGE_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
599 RTMemProtect(pVM->vmm.s.pbEMTStackR3 + VMM_STACK_SIZE, PAGE_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
600#endif
601 return rc;
602}
603
604
605/**
606 * Terminates the per-VCPU VMM.
607 *
608 * Termination means cleaning up and freeing all resources,
609 * the VM it self is at this point powered off or suspended.
610 *
611 * @returns VBox status code.
612 * @param pVM The VM to operate on.
613 */
614VMMR3DECL(int) VMMR3TermCPU(PVM pVM)
615{
616 return VINF_SUCCESS;
617}
618
619
620/**
621 * Applies relocations to data and code managed by this
622 * component. This function will be called at init and
623 * whenever the VMM need to relocate it self inside the GC.
624 *
625 * The VMM will need to apply relocations to the core code.
626 *
627 * @param pVM The VM handle.
628 * @param offDelta The relocation delta.
629 */
630VMMR3DECL(void) VMMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
631{
632 LogFlow(("VMMR3Relocate: offDelta=%RGv\n", offDelta));
633
634 /*
635 * Recalc the RC address.
636 */
637 pVM->vmm.s.pvCoreCodeRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pvCoreCodeR3);
638
639 /*
640 * The stack.
641 */
642 CPUMSetHyperESP(pVM, CPUMGetHyperESP(pVM) + offDelta);
643 pVM->vmm.s.pbEMTStackRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pbEMTStackR3);
644 pVM->vmm.s.pbEMTStackBottomRC = pVM->vmm.s.pbEMTStackRC + VMM_STACK_SIZE;
645
646 /*
647 * All the switchers.
648 */
649 vmmR3SwitcherRelocate(pVM, offDelta);
650
651 /*
652 * Get other RC entry points.
653 */
654 int rc = PDMR3LdrGetSymbolRC(pVM, VMMGC_MAIN_MODULE_NAME, "CPUMGCResumeGuest", &pVM->vmm.s.pfnCPUMRCResumeGuest);
655 AssertReleaseMsgRC(rc, ("CPUMGCResumeGuest not found! rc=%Rra\n", rc));
656
657 rc = PDMR3LdrGetSymbolRC(pVM, VMMGC_MAIN_MODULE_NAME, "CPUMGCResumeGuestV86", &pVM->vmm.s.pfnCPUMRCResumeGuestV86);
658 AssertReleaseMsgRC(rc, ("CPUMGCResumeGuestV86 not found! rc=%Rra\n", rc));
659
660 /*
661 * Update the logger.
662 */
663 VMMR3UpdateLoggers(pVM);
664}
665
666
667/**
668 * Updates the settings for the RC and R0 loggers.
669 *
670 * @returns VBox status code.
671 * @param pVM The VM handle.
672 */
673VMMR3DECL(int) VMMR3UpdateLoggers(PVM pVM)
674{
675 /*
676 * Simply clone the logger instance (for RC).
677 */
678 int rc = VINF_SUCCESS;
679 RTRCPTR RCPtrLoggerFlush = 0;
680
681 if (pVM->vmm.s.pRCLoggerR3
682#ifdef VBOX_WITH_RC_RELEASE_LOGGING
683 || pVM->vmm.s.pRCRelLoggerR3
684#endif
685 )
686 {
687 rc = PDMR3LdrGetSymbolRC(pVM, VMMGC_MAIN_MODULE_NAME, "vmmGCLoggerFlush", &RCPtrLoggerFlush);
688 AssertReleaseMsgRC(rc, ("vmmGCLoggerFlush not found! rc=%Rra\n", rc));
689 }
690
691 if (pVM->vmm.s.pRCLoggerR3)
692 {
693 RTRCPTR RCPtrLoggerWrapper = 0;
694 rc = PDMR3LdrGetSymbolRC(pVM, VMMGC_MAIN_MODULE_NAME, "vmmGCLoggerWrapper", &RCPtrLoggerWrapper);
695 AssertReleaseMsgRC(rc, ("vmmGCLoggerWrapper not found! rc=%Rra\n", rc));
696
697 pVM->vmm.s.pRCLoggerRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pRCLoggerR3);
698 rc = RTLogCloneRC(NULL /* default */, pVM->vmm.s.pRCLoggerR3, pVM->vmm.s.cbRCLogger,
699 RCPtrLoggerWrapper, RCPtrLoggerFlush, RTLOGFLAGS_BUFFERED);
700 AssertReleaseMsgRC(rc, ("RTLogCloneRC failed! rc=%Rra\n", rc));
701 }
702
703#ifdef VBOX_WITH_RC_RELEASE_LOGGING
704 if (pVM->vmm.s.pRCRelLoggerR3)
705 {
706 RTRCPTR RCPtrLoggerWrapper = 0;
707 rc = PDMR3LdrGetSymbolRC(pVM, VMMGC_MAIN_MODULE_NAME, "vmmGCRelLoggerWrapper", &RCPtrLoggerWrapper);
708 AssertReleaseMsgRC(rc, ("vmmGCRelLoggerWrapper not found! rc=%Rra\n", rc));
709
710 pVM->vmm.s.pRCRelLoggerRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pRCRelLoggerR3);
711 rc = RTLogCloneRC(RTLogRelDefaultInstance(), pVM->vmm.s.pRCRelLoggerR3, pVM->vmm.s.cbRCRelLogger,
712 RCPtrLoggerWrapper, RCPtrLoggerFlush, RTLOGFLAGS_BUFFERED);
713 AssertReleaseMsgRC(rc, ("RTLogCloneRC failed! rc=%Rra\n", rc));
714 }
715#endif /* VBOX_WITH_RC_RELEASE_LOGGING */
716
717 /*
718 * For the ring-0 EMT logger, we use a per-thread logger instance
719 * in ring-0. Only initialize it once.
720 */
721 PVMMR0LOGGER pR0LoggerR3 = pVM->vmm.s.pR0LoggerR3;
722 if (pR0LoggerR3)
723 {
724 if (!pR0LoggerR3->fCreated)
725 {
726 RTR0PTR pfnLoggerWrapper = NIL_RTR0PTR;
727 rc = PDMR3LdrGetSymbolR0(pVM, VMMR0_MAIN_MODULE_NAME, "vmmR0LoggerWrapper", &pfnLoggerWrapper);
728 AssertReleaseMsgRCReturn(rc, ("VMMLoggerWrapper not found! rc=%Rra\n", rc), rc);
729
730 RTR0PTR pfnLoggerFlush = NIL_RTR0PTR;
731 rc = PDMR3LdrGetSymbolR0(pVM, VMMR0_MAIN_MODULE_NAME, "vmmR0LoggerFlush", &pfnLoggerFlush);
732 AssertReleaseMsgRCReturn(rc, ("VMMLoggerFlush not found! rc=%Rra\n", rc), rc);
733
734 rc = RTLogCreateForR0(&pR0LoggerR3->Logger, pR0LoggerR3->cbLogger,
735 *(PFNRTLOGGER *)&pfnLoggerWrapper, *(PFNRTLOGFLUSH *)&pfnLoggerFlush,
736 RTLOGFLAGS_BUFFERED, RTLOGDEST_DUMMY);
737 AssertReleaseMsgRCReturn(rc, ("RTLogCreateForR0 failed! rc=%Rra\n", rc), rc);
738 pR0LoggerR3->fCreated = true;
739 pR0LoggerR3->fFlushingDisabled = false;
740 }
741
742 rc = RTLogCopyGroupsAndFlags(&pR0LoggerR3->Logger, NULL /* default */, pVM->vmm.s.pRCLoggerR3->fFlags, RTLOGFLAGS_BUFFERED);
743 AssertRC(rc);
744 }
745
746 return rc;
747}
748
749
750/**
751 * Gets the pointer to a buffer containing the R0/RC AssertMsg1 output.
752 *
753 * @returns Pointer to the buffer.
754 * @param pVM The VM handle.
755 */
756VMMR3DECL(const char *) VMMR3GetRZAssertMsg1(PVM pVM)
757{
758 if (HWACCMIsEnabled(pVM))
759 return pVM->vmm.s.szRing0AssertMsg1;
760
761 RTRCPTR RCPtr;
762 int rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_szRTAssertMsg1", &RCPtr);
763 if (RT_SUCCESS(rc))
764 return (const char *)MMHyperRCToR3(pVM, RCPtr);
765
766 return NULL;
767}
768
769
770/**
771 * Gets the pointer to a buffer containing the R0/RC AssertMsg2 output.
772 *
773 * @returns Pointer to the buffer.
774 * @param pVM The VM handle.
775 */
776VMMR3DECL(const char *) VMMR3GetRZAssertMsg2(PVM pVM)
777{
778 if (HWACCMIsEnabled(pVM))
779 return pVM->vmm.s.szRing0AssertMsg2;
780
781 RTRCPTR RCPtr;
782 int rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_szRTAssertMsg2", &RCPtr);
783 if (RT_SUCCESS(rc))
784 return (const char *)MMHyperRCToR3(pVM, RCPtr);
785
786 return NULL;
787}
788
789
790/**
791 * Execute state save operation.
792 *
793 * @returns VBox status code.
794 * @param pVM VM Handle.
795 * @param pSSM SSM operation handle.
796 */
797static DECLCALLBACK(int) vmmR3Save(PVM pVM, PSSMHANDLE pSSM)
798{
799 LogFlow(("vmmR3Save:\n"));
800
801 /*
802 * The hypervisor stack.
803 * Note! See not in vmmR3Load.
804 */
805 SSMR3PutRCPtr(pSSM, pVM->vmm.s.pbEMTStackBottomRC);
806 RTRCPTR RCPtrESP = CPUMGetHyperESP(pVM);
807 AssertMsg(pVM->vmm.s.pbEMTStackBottomRC - RCPtrESP <= VMM_STACK_SIZE, ("Bottom %RRv ESP=%RRv\n", pVM->vmm.s.pbEMTStackBottomRC, RCPtrESP));
808 SSMR3PutRCPtr(pSSM, RCPtrESP);
809 SSMR3PutMem(pSSM, pVM->vmm.s.pbEMTStackR3, VMM_STACK_SIZE);
810 return SSMR3PutU32(pSSM, ~0); /* terminator */
811}
812
813
814/**
815 * Execute state load operation.
816 *
817 * @returns VBox status code.
818 * @param pVM VM Handle.
819 * @param pSSM SSM operation handle.
820 * @param u32Version Data layout version.
821 */
822static DECLCALLBACK(int) vmmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version)
823{
824 LogFlow(("vmmR3Load:\n"));
825
826 /*
827 * Validate version.
828 */
829 if (u32Version != VMM_SAVED_STATE_VERSION)
830 {
831 AssertMsgFailed(("vmmR3Load: Invalid version u32Version=%d!\n", u32Version));
832 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
833 }
834
835 /*
836 * Check that the stack is in the same place, or that it's fearly empty.
837 *
838 * Note! This can be skipped next time we update saved state as we will
839 * never be in a R0/RC -> ring-3 call when saving the state. The
840 * stack and the two associated pointers are not required.
841 */
842 RTRCPTR RCPtrStackBottom;
843 SSMR3GetRCPtr(pSSM, &RCPtrStackBottom);
844 RTRCPTR RCPtrESP;
845 int rc = SSMR3GetRCPtr(pSSM, &RCPtrESP);
846 if (RT_FAILURE(rc))
847 return rc;
848
849 /* restore the stack. */
850 SSMR3GetMem(pSSM, pVM->vmm.s.pbEMTStackR3, VMM_STACK_SIZE);
851
852 /* terminator */
853 uint32_t u32;
854 rc = SSMR3GetU32(pSSM, &u32);
855 if (RT_FAILURE(rc))
856 return rc;
857 if (u32 != ~0U)
858 {
859 AssertMsgFailed(("u32=%#x\n", u32));
860 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
861 }
862 return VINF_SUCCESS;
863}
864
865
866/**
867 * Resolve a builtin RC symbol.
868 *
869 * Called by PDM when loading or relocating RC modules.
870 *
871 * @returns VBox status
872 * @param pVM VM Handle.
873 * @param pszSymbol Symbol to resolv
874 * @param pRCPtrValue Where to store the symbol value.
875 *
876 * @remark This has to work before VMMR3Relocate() is called.
877 */
878VMMR3DECL(int) VMMR3GetImportRC(PVM pVM, const char *pszSymbol, PRTRCPTR pRCPtrValue)
879{
880 if (!strcmp(pszSymbol, "g_Logger"))
881 {
882 if (pVM->vmm.s.pRCLoggerR3)
883 pVM->vmm.s.pRCLoggerRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pRCLoggerR3);
884 *pRCPtrValue = pVM->vmm.s.pRCLoggerRC;
885 }
886 else if (!strcmp(pszSymbol, "g_RelLogger"))
887 {
888#ifdef VBOX_WITH_RC_RELEASE_LOGGING
889 if (pVM->vmm.s.pRCRelLoggerR3)
890 pVM->vmm.s.pRCRelLoggerRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pRCRelLoggerR3);
891 *pRCPtrValue = pVM->vmm.s.pRCRelLoggerRC;
892#else
893 *pRCPtrValue = NIL_RTRCPTR;
894#endif
895 }
896 else
897 return VERR_SYMBOL_NOT_FOUND;
898 return VINF_SUCCESS;
899}
900
901
902/**
903 * Suspends the CPU yielder.
904 *
905 * @param pVM The VM handle.
906 */
907VMMR3DECL(void) VMMR3YieldSuspend(PVM pVM)
908{
909 if (!pVM->vmm.s.cYieldResumeMillies)
910 {
911 uint64_t u64Now = TMTimerGet(pVM->vmm.s.pYieldTimer);
912 uint64_t u64Expire = TMTimerGetExpire(pVM->vmm.s.pYieldTimer);
913 if (u64Now >= u64Expire || u64Expire == ~(uint64_t)0)
914 pVM->vmm.s.cYieldResumeMillies = pVM->vmm.s.cYieldEveryMillies;
915 else
916 pVM->vmm.s.cYieldResumeMillies = TMTimerToMilli(pVM->vmm.s.pYieldTimer, u64Expire - u64Now);
917 TMTimerStop(pVM->vmm.s.pYieldTimer);
918 }
919 pVM->vmm.s.u64LastYield = RTTimeNanoTS();
920}
921
922
923/**
924 * Stops the CPU yielder.
925 *
926 * @param pVM The VM handle.
927 */
928VMMR3DECL(void) VMMR3YieldStop(PVM pVM)
929{
930 if (!pVM->vmm.s.cYieldResumeMillies)
931 TMTimerStop(pVM->vmm.s.pYieldTimer);
932 pVM->vmm.s.cYieldResumeMillies = pVM->vmm.s.cYieldEveryMillies;
933 pVM->vmm.s.u64LastYield = RTTimeNanoTS();
934}
935
936
937/**
938 * Resumes the CPU yielder when it has been a suspended or stopped.
939 *
940 * @param pVM The VM handle.
941 */
942VMMR3DECL(void) VMMR3YieldResume(PVM pVM)
943{
944 if (pVM->vmm.s.cYieldResumeMillies)
945 {
946 TMTimerSetMillies(pVM->vmm.s.pYieldTimer, pVM->vmm.s.cYieldResumeMillies);
947 pVM->vmm.s.cYieldResumeMillies = 0;
948 }
949}
950
951
952/**
953 * Internal timer callback function.
954 *
955 * @param pVM The VM.
956 * @param pTimer The timer handle.
957 * @param pvUser User argument specified upon timer creation.
958 */
959static DECLCALLBACK(void) vmmR3YieldEMT(PVM pVM, PTMTIMER pTimer, void *pvUser)
960{
961 /*
962 * This really needs some careful tuning. While we shouldn't be too greedy since
963 * that'll cause the rest of the system to stop up, we shouldn't be too nice either
964 * because that'll cause us to stop up.
965 *
966 * The current logic is to use the default interval when there is no lag worth
967 * mentioning, but when we start accumulating lag we don't bother yielding at all.
968 *
969 * (This depends on the TMCLOCK_VIRTUAL_SYNC to be scheduled before TMCLOCK_REAL
970 * so the lag is up to date.)
971 */
972 const uint64_t u64Lag = TMVirtualSyncGetLag(pVM);
973 if ( u64Lag < 50000000 /* 50ms */
974 || ( u64Lag < 1000000000 /* 1s */
975 && RTTimeNanoTS() - pVM->vmm.s.u64LastYield < 500000000 /* 500 ms */)
976 )
977 {
978 uint64_t u64Elapsed = RTTimeNanoTS();
979 pVM->vmm.s.u64LastYield = u64Elapsed;
980
981 RTThreadYield();
982
983#ifdef LOG_ENABLED
984 u64Elapsed = RTTimeNanoTS() - u64Elapsed;
985 Log(("vmmR3YieldEMT: %RI64 ns\n", u64Elapsed));
986#endif
987 }
988 TMTimerSetMillies(pTimer, pVM->vmm.s.cYieldEveryMillies);
989}
990
991
992/**
993 * Acquire global VM lock.
994 *
995 * @returns VBox status code
996 * @param pVM The VM to operate on.
997 *
998 * @remarks The global VMM lock isn't really used for anything any longer.
999 */
1000VMMR3DECL(int) VMMR3Lock(PVM pVM)
1001{
1002 return RTCritSectEnter(&pVM->vmm.s.CritSectVMLock);
1003}
1004
1005
1006/**
1007 * Release global VM lock.
1008 *
1009 * @returns VBox status code
1010 * @param pVM The VM to operate on.
1011 *
1012 * @remarks The global VMM lock isn't really used for anything any longer.
1013 */
1014VMMR3DECL(int) VMMR3Unlock(PVM pVM)
1015{
1016 return RTCritSectLeave(&pVM->vmm.s.CritSectVMLock);
1017}
1018
1019
1020/**
1021 * Return global VM lock owner.
1022 *
1023 * @returns Thread id of owner.
1024 * @returns NIL_RTTHREAD if no owner.
1025 * @param pVM The VM to operate on.
1026 *
1027 * @remarks The global VMM lock isn't really used for anything any longer.
1028 */
1029VMMR3DECL(RTNATIVETHREAD) VMMR3LockGetOwner(PVM pVM)
1030{
1031 return RTCritSectGetOwner(&pVM->vmm.s.CritSectVMLock);
1032}
1033
1034
1035/**
1036 * Checks if the current thread is the owner of the global VM lock.
1037 *
1038 * @returns true if owner.
1039 * @returns false if not owner.
1040 * @param pVM The VM to operate on.
1041 *
1042 * @remarks The global VMM lock isn't really used for anything any longer.
1043 */
1044VMMR3DECL(bool) VMMR3LockIsOwner(PVM pVM)
1045{
1046 return RTCritSectIsOwner(&pVM->vmm.s.CritSectVMLock);
1047}
1048
1049
1050/**
1051 * Executes guest code in the raw-mode context.
1052 *
1053 * @param pVM VM handle.
1054 */
1055VMMR3DECL(int) VMMR3RawRunGC(PVM pVM)
1056{
1057 Log2(("VMMR3RawRunGC: (cs:eip=%04x:%08x)\n", CPUMGetGuestCS(pVM), CPUMGetGuestEIP(pVM)));
1058
1059 /*
1060 * Set the EIP and ESP.
1061 */
1062 CPUMSetHyperEIP(pVM, CPUMGetGuestEFlags(pVM) & X86_EFL_VM
1063 ? pVM->vmm.s.pfnCPUMRCResumeGuestV86
1064 : pVM->vmm.s.pfnCPUMRCResumeGuest);
1065 CPUMSetHyperESP(pVM, pVM->vmm.s.pbEMTStackBottomRC);
1066
1067 /*
1068 * We hide log flushes (outer) and hypervisor interrupts (inner).
1069 */
1070 for (;;)
1071 {
1072 Assert(CPUMGetHyperCR3(pVM) && CPUMGetHyperCR3(pVM) == PGMGetHyperCR3(pVM));
1073#ifdef VBOX_STRICT
1074 PGMMapCheck(pVM);
1075#endif
1076 int rc;
1077 do
1078 {
1079#ifdef NO_SUPCALLR0VMM
1080 rc = VERR_GENERAL_FAILURE;
1081#else
1082 rc = SUPCallVMMR0Fast(pVM->pVMR0, VMMR0_DO_RAW_RUN, 0);
1083 if (RT_LIKELY(rc == VINF_SUCCESS))
1084 rc = pVM->vmm.s.iLastGZRc;
1085#endif
1086 } while (rc == VINF_EM_RAW_INTERRUPT_HYPER);
1087
1088 /*
1089 * Flush the logs.
1090 */
1091#ifdef LOG_ENABLED
1092 PRTLOGGERRC pLogger = pVM->vmm.s.pRCLoggerR3;
1093 if ( pLogger
1094 && pLogger->offScratch > 0)
1095 RTLogFlushRC(NULL, pLogger);
1096#endif
1097#ifdef VBOX_WITH_RC_RELEASE_LOGGING
1098 PRTLOGGERRC pRelLogger = pVM->vmm.s.pRCRelLoggerR3;
1099 if (RT_UNLIKELY(pRelLogger && pRelLogger->offScratch > 0))
1100 RTLogFlushRC(RTLogRelDefaultInstance(), pRelLogger);
1101#endif
1102 if (rc != VINF_VMM_CALL_HOST)
1103 {
1104 Log2(("VMMR3RawRunGC: returns %Rrc (cs:eip=%04x:%08x)\n", rc, CPUMGetGuestCS(pVM), CPUMGetGuestEIP(pVM)));
1105 return rc;
1106 }
1107 rc = vmmR3ServiceCallHostRequest(pVM);
1108 if (RT_FAILURE(rc))
1109 return rc;
1110 /* Resume GC */
1111 }
1112}
1113
1114
1115/**
1116 * Executes guest code (Intel VT-x and AMD-V).
1117 *
1118 * @param pVM VM handle.
1119 * @param idCpu VMCPU id.
1120 */
1121VMMR3DECL(int) VMMR3HwAccRunGC(PVM pVM, RTCPUID idCpu)
1122{
1123 Log2(("VMMR3HwAccRunGC: (cs:eip=%04x:%08x)\n", CPUMGetGuestCS(pVM), CPUMGetGuestEIP(pVM)));
1124
1125 for (;;)
1126 {
1127 int rc;
1128 do
1129 {
1130#ifdef NO_SUPCALLR0VMM
1131 rc = VERR_GENERAL_FAILURE;
1132#else
1133 rc = SUPCallVMMR0Fast(pVM->pVMR0, VMMR0_DO_HWACC_RUN, idCpu);
1134 if (RT_LIKELY(rc == VINF_SUCCESS))
1135 rc = pVM->vmm.s.iLastGZRc;
1136#endif
1137 } while (rc == VINF_EM_RAW_INTERRUPT_HYPER);
1138
1139#ifdef LOG_ENABLED
1140 /*
1141 * Flush the log
1142 */
1143 PVMMR0LOGGER pR0LoggerR3 = pVM->vmm.s.pR0LoggerR3;
1144 if ( pR0LoggerR3
1145 && pR0LoggerR3->Logger.offScratch > 0)
1146 RTLogFlushToLogger(&pR0LoggerR3->Logger, NULL);
1147#endif /* !LOG_ENABLED */
1148 if (rc != VINF_VMM_CALL_HOST)
1149 {
1150 Log2(("VMMR3HwAccRunGC: returns %Rrc (cs:eip=%04x:%08x)\n", rc, CPUMGetGuestCS(pVM), CPUMGetGuestEIP(pVM)));
1151 return rc;
1152 }
1153 rc = vmmR3ServiceCallHostRequest(pVM);
1154 if (RT_FAILURE(rc))
1155 return rc;
1156 /* Resume R0 */
1157 }
1158}
1159
1160
1161/**
1162 * Calls a RC function.
1163 *
1164 * @param pVM The VM handle.
1165 * @param RCPtrEntry The address of the RC function.
1166 * @param cArgs The number of arguments in the ....
1167 * @param ... Arguments to the function.
1168 */
1169VMMR3DECL(int) VMMR3CallRC(PVM pVM, RTRCPTR RCPtrEntry, unsigned cArgs, ...)
1170{
1171 va_list args;
1172 va_start(args, cArgs);
1173 int rc = VMMR3CallRCV(pVM, RCPtrEntry, cArgs, args);
1174 va_end(args);
1175 return rc;
1176}
1177
1178
1179/**
1180 * Calls a RC function.
1181 *
1182 * @param pVM The VM handle.
1183 * @param RCPtrEntry The address of the RC function.
1184 * @param cArgs The number of arguments in the ....
1185 * @param args Arguments to the function.
1186 */
1187VMMR3DECL(int) VMMR3CallRCV(PVM pVM, RTRCPTR RCPtrEntry, unsigned cArgs, va_list args)
1188{
1189 Log2(("VMMR3CallGCV: RCPtrEntry=%RRv cArgs=%d\n", RCPtrEntry, cArgs));
1190
1191 /*
1192 * Setup the call frame using the trampoline.
1193 */
1194 CPUMHyperSetCtxCore(pVM, NULL);
1195 memset(pVM->vmm.s.pbEMTStackR3, 0xaa, VMM_STACK_SIZE); /* Clear the stack. */
1196 CPUMSetHyperESP(pVM, pVM->vmm.s.pbEMTStackBottomRC - cArgs * sizeof(RTGCUINTPTR32));
1197 PRTGCUINTPTR32 pFrame = (PRTGCUINTPTR32)(pVM->vmm.s.pbEMTStackR3 + VMM_STACK_SIZE) - cArgs;
1198 int i = cArgs;
1199 while (i-- > 0)
1200 *pFrame++ = va_arg(args, RTGCUINTPTR32);
1201
1202 CPUMPushHyper(pVM, cArgs * sizeof(RTGCUINTPTR32)); /* stack frame size */
1203 CPUMPushHyper(pVM, RCPtrEntry); /* what to call */
1204 CPUMSetHyperEIP(pVM, pVM->vmm.s.pfnCallTrampolineRC);
1205
1206 /*
1207 * We hide log flushes (outer) and hypervisor interrupts (inner).
1208 */
1209 for (;;)
1210 {
1211 int rc;
1212 Assert(CPUMGetHyperCR3(pVM) && CPUMGetHyperCR3(pVM) == PGMGetHyperCR3(pVM));
1213 do
1214 {
1215#ifdef NO_SUPCALLR0VMM
1216 rc = VERR_GENERAL_FAILURE;
1217#else
1218 rc = SUPCallVMMR0Fast(pVM->pVMR0, VMMR0_DO_RAW_RUN, 0);
1219 if (RT_LIKELY(rc == VINF_SUCCESS))
1220 rc = pVM->vmm.s.iLastGZRc;
1221#endif
1222 } while (rc == VINF_EM_RAW_INTERRUPT_HYPER);
1223
1224 /*
1225 * Flush the logs.
1226 */
1227#ifdef LOG_ENABLED
1228 PRTLOGGERRC pLogger = pVM->vmm.s.pRCLoggerR3;
1229 if ( pLogger
1230 && pLogger->offScratch > 0)
1231 RTLogFlushRC(NULL, pLogger);
1232#endif
1233#ifdef VBOX_WITH_RC_RELEASE_LOGGING
1234 PRTLOGGERRC pRelLogger = pVM->vmm.s.pRCRelLoggerR3;
1235 if (RT_UNLIKELY(pRelLogger && pRelLogger->offScratch > 0))
1236 RTLogFlushRC(RTLogRelDefaultInstance(), pRelLogger);
1237#endif
1238 if (rc == VERR_TRPM_PANIC || rc == VERR_TRPM_DONT_PANIC)
1239 VMMR3FatalDump(pVM, rc);
1240 if (rc != VINF_VMM_CALL_HOST)
1241 {
1242 Log2(("VMMR3CallGCV: returns %Rrc (cs:eip=%04x:%08x)\n", rc, CPUMGetGuestCS(pVM), CPUMGetGuestEIP(pVM)));
1243 return rc;
1244 }
1245 rc = vmmR3ServiceCallHostRequest(pVM);
1246 if (RT_FAILURE(rc))
1247 return rc;
1248 }
1249}
1250
1251
1252/**
1253 * Wrapper for SUPCallVMMR0Ex which will deal with
1254 * VINF_VMM_CALL_HOST returns.
1255 *
1256 * @returns VBox status code.
1257 * @param pVM The VM to operate on.
1258 * @param uOperation Operation to execute.
1259 * @param u64Arg Constant argument.
1260 * @param pReqHdr Pointer to a request header. See SUPCallVMMR0Ex for
1261 * details.
1262 */
1263VMMR3DECL(int) VMMR3CallR0(PVM pVM, uint32_t uOperation, uint64_t u64Arg, PSUPVMMR0REQHDR pReqHdr)
1264{
1265 /*
1266 * Call Ring-0 entry with init code.
1267 */
1268 int rc;
1269 for (;;)
1270 {
1271#ifdef NO_SUPCALLR0VMM
1272 rc = VERR_GENERAL_FAILURE;
1273#else
1274 rc = SUPCallVMMR0Ex(pVM->pVMR0, uOperation, u64Arg, pReqHdr);
1275#endif
1276 if ( pVM->vmm.s.pR0LoggerR3
1277 && pVM->vmm.s.pR0LoggerR3->Logger.offScratch > 0)
1278 RTLogFlushToLogger(&pVM->vmm.s.pR0LoggerR3->Logger, NULL);
1279 if (rc != VINF_VMM_CALL_HOST)
1280 break;
1281 rc = vmmR3ServiceCallHostRequest(pVM);
1282 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
1283 break;
1284 /* Resume R0 */
1285 }
1286
1287 AssertLogRelMsgReturn(rc == VINF_SUCCESS || VBOX_FAILURE(rc),
1288 ("uOperation=%u rc=%Rrc\n", uOperation, rc),
1289 VERR_INTERNAL_ERROR);
1290 return rc;
1291}
1292
1293
1294/**
1295 * Resumes executing hypervisor code when interrupted by a queue flush or a
1296 * debug event.
1297 *
1298 * @returns VBox status code.
1299 * @param pVM VM handle.
1300 */
1301VMMR3DECL(int) VMMR3ResumeHyper(PVM pVM)
1302{
1303 Log(("VMMR3ResumeHyper: eip=%RRv esp=%RRv\n", CPUMGetHyperEIP(pVM), CPUMGetHyperESP(pVM)));
1304
1305 /*
1306 * We hide log flushes (outer) and hypervisor interrupts (inner).
1307 */
1308 for (;;)
1309 {
1310 int rc;
1311 Assert(CPUMGetHyperCR3(pVM) && CPUMGetHyperCR3(pVM) == PGMGetHyperCR3(pVM));
1312 do
1313 {
1314#ifdef NO_SUPCALLR0VMM
1315 rc = VERR_GENERAL_FAILURE;
1316#else
1317 rc = SUPCallVMMR0Fast(pVM->pVMR0, VMMR0_DO_RAW_RUN, 0);
1318 if (RT_LIKELY(rc == VINF_SUCCESS))
1319 rc = pVM->vmm.s.iLastGZRc;
1320#endif
1321 } while (rc == VINF_EM_RAW_INTERRUPT_HYPER);
1322
1323 /*
1324 * Flush the loggers,
1325 */
1326#ifdef LOG_ENABLED
1327 PRTLOGGERRC pLogger = pVM->vmm.s.pRCLoggerR3;
1328 if ( pLogger
1329 && pLogger->offScratch > 0)
1330 RTLogFlushRC(NULL, pLogger);
1331#endif
1332#ifdef VBOX_WITH_RC_RELEASE_LOGGING
1333 PRTLOGGERRC pRelLogger = pVM->vmm.s.pRCRelLoggerR3;
1334 if (RT_UNLIKELY(pRelLogger && pRelLogger->offScratch > 0))
1335 RTLogFlushRC(RTLogRelDefaultInstance(), pRelLogger);
1336#endif
1337 if (rc == VERR_TRPM_PANIC || rc == VERR_TRPM_DONT_PANIC)
1338 VMMR3FatalDump(pVM, rc);
1339 if (rc != VINF_VMM_CALL_HOST)
1340 {
1341 Log(("VMMR3ResumeHyper: returns %Rrc\n", rc));
1342 return rc;
1343 }
1344 rc = vmmR3ServiceCallHostRequest(pVM);
1345 if (RT_FAILURE(rc))
1346 return rc;
1347 }
1348}
1349
1350
1351/**
1352 * Service a call to the ring-3 host code.
1353 *
1354 * @returns VBox status code.
1355 * @param pVM VM handle.
1356 * @remark Careful with critsects.
1357 */
1358static int vmmR3ServiceCallHostRequest(PVM pVM)
1359{
1360 switch (pVM->vmm.s.enmCallHostOperation)
1361 {
1362 /*
1363 * Acquire the PDM lock.
1364 */
1365 case VMMCALLHOST_PDM_LOCK:
1366 {
1367 pVM->vmm.s.rcCallHost = PDMR3LockCall(pVM);
1368 break;
1369 }
1370
1371 /*
1372 * Flush a PDM queue.
1373 */
1374 case VMMCALLHOST_PDM_QUEUE_FLUSH:
1375 {
1376 PDMR3QueueFlushWorker(pVM, NULL);
1377 pVM->vmm.s.rcCallHost = VINF_SUCCESS;
1378 break;
1379 }
1380
1381 /*
1382 * Grow the PGM pool.
1383 */
1384 case VMMCALLHOST_PGM_POOL_GROW:
1385 {
1386 pVM->vmm.s.rcCallHost = PGMR3PoolGrow(pVM);
1387 break;
1388 }
1389
1390 /*
1391 * Maps an page allocation chunk into ring-3 so ring-0 can use it.
1392 */
1393 case VMMCALLHOST_PGM_MAP_CHUNK:
1394 {
1395 pVM->vmm.s.rcCallHost = PGMR3PhysChunkMap(pVM, pVM->vmm.s.u64CallHostArg);
1396 break;
1397 }
1398
1399 /*
1400 * Allocates more handy pages.
1401 */
1402 case VMMCALLHOST_PGM_ALLOCATE_HANDY_PAGES:
1403 {
1404 pVM->vmm.s.rcCallHost = PGMR3PhysAllocateHandyPages(pVM);
1405 break;
1406 }
1407
1408 /*
1409 * Acquire the PGM lock.
1410 */
1411 case VMMCALLHOST_PGM_LOCK:
1412 {
1413 pVM->vmm.s.rcCallHost = PGMR3LockCall(pVM);
1414 break;
1415 }
1416
1417 /*
1418 * Flush REM handler notifications.
1419 */
1420 case VMMCALLHOST_REM_REPLAY_HANDLER_NOTIFICATIONS:
1421 {
1422 REMR3ReplayHandlerNotifications(pVM);
1423 pVM->vmm.s.rcCallHost = VINF_SUCCESS;
1424 break;
1425 }
1426
1427 /*
1428 * This is a noop. We just take this route to avoid unnecessary
1429 * tests in the loops.
1430 */
1431 case VMMCALLHOST_VMM_LOGGER_FLUSH:
1432 pVM->vmm.s.rcCallHost = VINF_SUCCESS;
1433 LogAlways(("*FLUSH*\n"));
1434 break;
1435
1436 /*
1437 * Set the VM error message.
1438 */
1439 case VMMCALLHOST_VM_SET_ERROR:
1440 VMR3SetErrorWorker(pVM);
1441 pVM->vmm.s.rcCallHost = VINF_SUCCESS;
1442 break;
1443
1444 /*
1445 * Set the VM runtime error message.
1446 */
1447 case VMMCALLHOST_VM_SET_RUNTIME_ERROR:
1448 pVM->vmm.s.rcCallHost = VMR3SetRuntimeErrorWorker(pVM);
1449 break;
1450
1451 /*
1452 * Signal a ring 0 hypervisor assertion.
1453 * Cancel the longjmp operation that's in progress.
1454 */
1455 case VMMCALLHOST_VM_R0_ASSERTION:
1456 pVM->vmm.s.enmCallHostOperation = VMMCALLHOST_INVALID;
1457 pVM->vmm.s.CallHostR0JmpBuf.fInRing3Call = false;
1458#ifdef RT_ARCH_X86
1459 pVM->vmm.s.CallHostR0JmpBuf.eip = 0;
1460#else
1461 pVM->vmm.s.CallHostR0JmpBuf.rip = 0;
1462#endif
1463 LogRel((pVM->vmm.s.szRing0AssertMsg1));
1464 LogRel((pVM->vmm.s.szRing0AssertMsg2));
1465 return VERR_VMM_RING0_ASSERTION;
1466
1467 /*
1468 * A forced switch to ring 0 for preemption purposes.
1469 */
1470 case VMMCALLHOST_VM_R0_PREEMPT:
1471 pVM->vmm.s.rcCallHost = VINF_SUCCESS;
1472 break;
1473
1474 default:
1475 AssertMsgFailed(("enmCallHostOperation=%d\n", pVM->vmm.s.enmCallHostOperation));
1476 return VERR_INTERNAL_ERROR;
1477 }
1478
1479 pVM->vmm.s.enmCallHostOperation = VMMCALLHOST_INVALID;
1480 return VINF_SUCCESS;
1481}
1482
1483
1484/**
1485 * Displays the Force action Flags.
1486 *
1487 * @param pVM The VM handle.
1488 * @param pHlp The output helpers.
1489 * @param pszArgs The additional arguments (ignored).
1490 */
1491static DECLCALLBACK(void) vmmR3InfoFF(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
1492{
1493 const uint32_t fForcedActions = pVM->fForcedActions;
1494
1495 pHlp->pfnPrintf(pHlp, "Forced action Flags: %#RX32", fForcedActions);
1496
1497 /* show the flag mnemonics */
1498 int c = 0;
1499 uint32_t f = fForcedActions;
1500#define PRINT_FLAG(flag) do { \
1501 if (f & (flag)) \
1502 { \
1503 static const char *s_psz = #flag; \
1504 if (!(c % 6)) \
1505 pHlp->pfnPrintf(pHlp, "%s\n %s", c ? "," : "", s_psz + 6); \
1506 else \
1507 pHlp->pfnPrintf(pHlp, ", %s", s_psz + 6); \
1508 c++; \
1509 f &= ~(flag); \
1510 } \
1511 } while (0)
1512 PRINT_FLAG(VM_FF_INTERRUPT_APIC);
1513 PRINT_FLAG(VM_FF_INTERRUPT_PIC);
1514 PRINT_FLAG(VM_FF_TIMER);
1515 PRINT_FLAG(VM_FF_PDM_QUEUES);
1516 PRINT_FLAG(VM_FF_PDM_DMA);
1517 PRINT_FLAG(VM_FF_PDM_CRITSECT);
1518 PRINT_FLAG(VM_FF_DBGF);
1519 PRINT_FLAG(VM_FF_REQUEST);
1520 PRINT_FLAG(VM_FF_TERMINATE);
1521 PRINT_FLAG(VM_FF_RESET);
1522 PRINT_FLAG(VM_FF_PGM_SYNC_CR3);
1523 PRINT_FLAG(VM_FF_PGM_SYNC_CR3_NON_GLOBAL);
1524 PRINT_FLAG(VM_FF_PGM_NEED_HANDY_PAGES);
1525 PRINT_FLAG(VM_FF_PGM_NO_MEMORY);
1526 PRINT_FLAG(VM_FF_TRPM_SYNC_IDT);
1527 PRINT_FLAG(VM_FF_SELM_SYNC_TSS);
1528 PRINT_FLAG(VM_FF_SELM_SYNC_GDT);
1529 PRINT_FLAG(VM_FF_SELM_SYNC_LDT);
1530 PRINT_FLAG(VM_FF_INHIBIT_INTERRUPTS);
1531 PRINT_FLAG(VM_FF_CSAM_SCAN_PAGE);
1532 PRINT_FLAG(VM_FF_CSAM_PENDING_ACTION);
1533 PRINT_FLAG(VM_FF_TO_R3);
1534 PRINT_FLAG(VM_FF_REM_HANDLER_NOTIFY);
1535 PRINT_FLAG(VM_FF_DEBUG_SUSPEND);
1536 if (f)
1537 pHlp->pfnPrintf(pHlp, "%s\n Unknown bits: %#RX32\n", c ? "," : "", f);
1538 else
1539 pHlp->pfnPrintf(pHlp, "\n");
1540#undef PRINT_FLAG
1541
1542 /* the groups */
1543 c = 0;
1544#define PRINT_GROUP(grp) do { \
1545 if (fForcedActions & (grp)) \
1546 { \
1547 static const char *s_psz = #grp; \
1548 if (!(c % 5)) \
1549 pHlp->pfnPrintf(pHlp, "%s %s", c ? ",\n" : "Groups:\n", s_psz + 6); \
1550 else \
1551 pHlp->pfnPrintf(pHlp, ", %s", s_psz + 6); \
1552 c++; \
1553 } \
1554 } while (0)
1555 PRINT_GROUP(VM_FF_EXTERNAL_SUSPENDED_MASK);
1556 PRINT_GROUP(VM_FF_EXTERNAL_HALTED_MASK);
1557 PRINT_GROUP(VM_FF_HIGH_PRIORITY_PRE_MASK);
1558 PRINT_GROUP(VM_FF_HIGH_PRIORITY_PRE_RAW_MASK);
1559 PRINT_GROUP(VM_FF_HIGH_PRIORITY_POST_MASK);
1560 PRINT_GROUP(VM_FF_NORMAL_PRIORITY_POST_MASK);
1561 PRINT_GROUP(VM_FF_NORMAL_PRIORITY_MASK);
1562 PRINT_GROUP(VM_FF_RESUME_GUEST_MASK);
1563 PRINT_GROUP(VM_FF_ALL_BUT_RAW_MASK);
1564 if (c)
1565 pHlp->pfnPrintf(pHlp, "\n");
1566#undef PRINT_GROUP
1567}
1568
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