VirtualBox

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

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

vmmR3ServiceCallHostRequest: check for pending pdm critical section releases (deadlock).

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