VirtualBox

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

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

VMM: Added VMMR3EmtRendezvous for getting the attention of all EMTs and run some code on one. Made first use of it in vmR3SetHaltMethodU that is called at the end of VMR3CreateVM.

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