VirtualBox

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

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

VMM: Instrumented the setjump code with stack usage statistics (only 32-bit and stack switching version atm).

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