VirtualBox

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

Last change on this file since 20060 was 20008, checked in by vboxsync, 15 years ago

PDMCritSect: rewrite, ring-0 unlocking not yet enabled.

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