VirtualBox

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

Last change on this file since 19722 was 19702, checked in by vboxsync, 16 years ago

Need a separate ring 0 logger for each VCPU.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 61.9 KB
Line 
1/* $Id: VMM.cpp 19702 2009-05-14 15:41:49Z 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 /*
466 * Initialize the ring-0 logger if we haven't done so yet.
467 */
468 if ( pVCpu->vmm.s.pR0LoggerR3
469 && !pVCpu->vmm.s.pR0LoggerR3->fCreated)
470 {
471 rc = VMMR3UpdateLoggers(pVM);
472 if (RT_FAILURE(rc))
473 return rc;
474 }
475
476 /*
477 * Call Ring-0 entry with init code.
478 */
479 for (;;)
480 {
481#ifdef NO_SUPCALLR0VMM
482 //rc = VERR_GENERAL_FAILURE;
483 rc = VINF_SUCCESS;
484#else
485 rc = SUPCallVMMR0Ex(pVM->pVMR0, 0 /* VCPU 0 */, VMMR0_DO_VMMR0_INIT, VMMGetSvnRev(), NULL);
486#endif
487 if ( pVCpu->vmm.s.pR0LoggerR3
488 && pVCpu->vmm.s.pR0LoggerR3->Logger.offScratch > 0)
489 RTLogFlushToLogger(&pVCpu->vmm.s.pR0LoggerR3->Logger, NULL);
490 if (rc != VINF_VMM_CALL_HOST)
491 break;
492 rc = vmmR3ServiceCallHostRequest(pVM, pVCpu);
493 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
494 break;
495 /* Resume R0 */
496 }
497
498 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
499 {
500 LogRel(("R0 init failed, rc=%Rra\n", rc));
501 if (RT_SUCCESS(rc))
502 rc = VERR_INTERNAL_ERROR;
503 }
504 return rc;
505}
506
507
508/**
509 * Initializes the RC VMM.
510 *
511 * @returns VBox status code.
512 * @param pVM The VM to operate on.
513 */
514VMMR3DECL(int) VMMR3InitRC(PVM pVM)
515{
516 PVMCPU pVCpu = VMMGetCpu(pVM);
517 Assert(pVCpu && pVCpu->idCpu == 0);
518
519 /* In VMX mode, there's no need to init RC. */
520 if (pVM->vmm.s.fSwitcherDisabled)
521 return VINF_SUCCESS;
522
523 AssertReturn(pVM->cCPUs == 1, VERR_RAW_MODE_INVALID_SMP);
524
525 /*
526 * Call VMMGCInit():
527 * -# resolve the address.
528 * -# setup stackframe and EIP to use the trampoline.
529 * -# do a generic hypervisor call.
530 */
531 RTRCPTR RCPtrEP;
532 int rc = PDMR3LdrGetSymbolRC(pVM, VMMGC_MAIN_MODULE_NAME, "VMMGCEntry", &RCPtrEP);
533 if (RT_SUCCESS(rc))
534 {
535 CPUMHyperSetCtxCore(pVCpu, NULL);
536 CPUMSetHyperESP(pVCpu, pVCpu->vmm.s.pbEMTStackBottomRC); /* Clear the stack. */
537 uint64_t u64TS = RTTimeProgramStartNanoTS();
538 CPUMPushHyper(pVCpu, (uint32_t)(u64TS >> 32)); /* Param 3: The program startup TS - Hi. */
539 CPUMPushHyper(pVCpu, (uint32_t)u64TS); /* Param 3: The program startup TS - Lo. */
540 CPUMPushHyper(pVCpu, VMMGetSvnRev()); /* Param 2: Version argument. */
541 CPUMPushHyper(pVCpu, VMMGC_DO_VMMGC_INIT); /* Param 1: Operation. */
542 CPUMPushHyper(pVCpu, pVM->pVMRC); /* Param 0: pVM */
543 CPUMPushHyper(pVCpu, 5 * sizeof(RTRCPTR)); /* trampoline param: stacksize. */
544 CPUMPushHyper(pVCpu, RCPtrEP); /* Call EIP. */
545 CPUMSetHyperEIP(pVCpu, pVM->vmm.s.pfnCallTrampolineRC);
546 Assert(CPUMGetHyperCR3(pVCpu) && CPUMGetHyperCR3(pVCpu) == PGMGetHyperCR3(pVCpu));
547
548 for (;;)
549 {
550#ifdef NO_SUPCALLR0VMM
551 //rc = VERR_GENERAL_FAILURE;
552 rc = VINF_SUCCESS;
553#else
554 rc = SUPCallVMMR0(pVM->pVMR0, 0 /* VCPU 0 */, VMMR0_DO_CALL_HYPERVISOR, NULL);
555#endif
556#ifdef LOG_ENABLED
557 PRTLOGGERRC pLogger = pVM->vmm.s.pRCLoggerR3;
558 if ( pLogger
559 && pLogger->offScratch > 0)
560 RTLogFlushRC(NULL, pLogger);
561#endif
562#ifdef VBOX_WITH_RC_RELEASE_LOGGING
563 PRTLOGGERRC pRelLogger = pVM->vmm.s.pRCRelLoggerR3;
564 if (RT_UNLIKELY(pRelLogger && pRelLogger->offScratch > 0))
565 RTLogFlushRC(RTLogRelDefaultInstance(), pRelLogger);
566#endif
567 if (rc != VINF_VMM_CALL_HOST)
568 break;
569 rc = vmmR3ServiceCallHostRequest(pVM, pVCpu);
570 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
571 break;
572 }
573
574 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
575 {
576 VMMR3FatalDump(pVM, pVCpu, rc);
577 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
578 rc = VERR_INTERNAL_ERROR;
579 }
580 AssertRC(rc);
581 }
582 return rc;
583}
584
585
586/**
587 * Terminate the VMM bits.
588 *
589 * @returns VINF_SUCCESS.
590 * @param pVM The VM handle.
591 */
592VMMR3DECL(int) VMMR3Term(PVM pVM)
593{
594 PVMCPU pVCpu = VMMGetCpu(pVM);
595 Assert(pVCpu && pVCpu->idCpu == 0);
596
597 /*
598 * Call Ring-0 entry with termination code.
599 */
600 int rc;
601 for (;;)
602 {
603#ifdef NO_SUPCALLR0VMM
604 //rc = VERR_GENERAL_FAILURE;
605 rc = VINF_SUCCESS;
606#else
607 rc = SUPCallVMMR0Ex(pVM->pVMR0, 0 /* VCPU 0 */, VMMR0_DO_VMMR0_TERM, 0, NULL);
608#endif
609 if ( pVCpu->vmm.s.pR0LoggerR3
610 && pVCpu->vmm.s.pR0LoggerR3->Logger.offScratch > 0)
611 RTLogFlushToLogger(&pVCpu->vmm.s.pR0LoggerR3->Logger, NULL);
612 if (rc != VINF_VMM_CALL_HOST)
613 break;
614 rc = vmmR3ServiceCallHostRequest(pVM, pVCpu);
615 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
616 break;
617 /* Resume R0 */
618 }
619 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
620 {
621 LogRel(("VMMR3Term: R0 term failed, rc=%Rra. (warning)\n", rc));
622 if (RT_SUCCESS(rc))
623 rc = VERR_INTERNAL_ERROR;
624 }
625
626 RTCritSectDelete(&pVM->vmm.s.CritSectSync);
627
628#ifdef VBOX_STRICT_VMM_STACK
629 /*
630 * Make the two stack guard pages present again.
631 */
632 RTMemProtect(pVM->vmm.s.pbEMTStackR3 - PAGE_SIZE, PAGE_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
633 RTMemProtect(pVM->vmm.s.pbEMTStackR3 + VMM_STACK_SIZE, PAGE_SIZE, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
634#endif
635 return rc;
636}
637
638
639/**
640 * Terminates the per-VCPU VMM.
641 *
642 * Termination means cleaning up and freeing all resources,
643 * the VM it self is at this point powered off or suspended.
644 *
645 * @returns VBox status code.
646 * @param pVM The VM to operate on.
647 */
648VMMR3DECL(int) VMMR3TermCPU(PVM pVM)
649{
650 return VINF_SUCCESS;
651}
652
653
654/**
655 * Applies relocations to data and code managed by this
656 * component. This function will be called at init and
657 * whenever the VMM need to relocate it self inside the GC.
658 *
659 * The VMM will need to apply relocations to the core code.
660 *
661 * @param pVM The VM handle.
662 * @param offDelta The relocation delta.
663 */
664VMMR3DECL(void) VMMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
665{
666 LogFlow(("VMMR3Relocate: offDelta=%RGv\n", offDelta));
667
668 /*
669 * Recalc the RC address.
670 */
671 pVM->vmm.s.pvCoreCodeRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pvCoreCodeR3);
672
673 /*
674 * The stack.
675 */
676 for (VMCPUID i = 0; i < pVM->cCPUs; i++)
677 {
678 PVMCPU pVCpu = &pVM->aCpus[i];
679
680 CPUMSetHyperESP(pVCpu, CPUMGetHyperESP(pVCpu) + offDelta);
681
682 pVCpu->vmm.s.pbEMTStackRC = MMHyperR3ToRC(pVM, pVCpu->vmm.s.pbEMTStackR3);
683 pVCpu->vmm.s.pbEMTStackBottomRC = pVCpu->vmm.s.pbEMTStackRC + VMM_STACK_SIZE;
684 }
685
686 /*
687 * All the switchers.
688 */
689 vmmR3SwitcherRelocate(pVM, offDelta);
690
691 /*
692 * Get other RC entry points.
693 */
694 int rc = PDMR3LdrGetSymbolRC(pVM, VMMGC_MAIN_MODULE_NAME, "CPUMGCResumeGuest", &pVM->vmm.s.pfnCPUMRCResumeGuest);
695 AssertReleaseMsgRC(rc, ("CPUMGCResumeGuest not found! rc=%Rra\n", rc));
696
697 rc = PDMR3LdrGetSymbolRC(pVM, VMMGC_MAIN_MODULE_NAME, "CPUMGCResumeGuestV86", &pVM->vmm.s.pfnCPUMRCResumeGuestV86);
698 AssertReleaseMsgRC(rc, ("CPUMGCResumeGuestV86 not found! rc=%Rra\n", rc));
699
700 /*
701 * Update the logger.
702 */
703 VMMR3UpdateLoggers(pVM);
704}
705
706
707/**
708 * Updates the settings for the RC and R0 loggers.
709 *
710 * @returns VBox status code.
711 * @param pVM The VM handle.
712 */
713VMMR3DECL(int) VMMR3UpdateLoggers(PVM pVM)
714{
715 /*
716 * Simply clone the logger instance (for RC).
717 */
718 int rc = VINF_SUCCESS;
719 RTRCPTR RCPtrLoggerFlush = 0;
720
721 if (pVM->vmm.s.pRCLoggerR3
722#ifdef VBOX_WITH_RC_RELEASE_LOGGING
723 || pVM->vmm.s.pRCRelLoggerR3
724#endif
725 )
726 {
727 rc = PDMR3LdrGetSymbolRC(pVM, VMMGC_MAIN_MODULE_NAME, "vmmGCLoggerFlush", &RCPtrLoggerFlush);
728 AssertReleaseMsgRC(rc, ("vmmGCLoggerFlush not found! rc=%Rra\n", rc));
729 }
730
731 if (pVM->vmm.s.pRCLoggerR3)
732 {
733 RTRCPTR RCPtrLoggerWrapper = 0;
734 rc = PDMR3LdrGetSymbolRC(pVM, VMMGC_MAIN_MODULE_NAME, "vmmGCLoggerWrapper", &RCPtrLoggerWrapper);
735 AssertReleaseMsgRC(rc, ("vmmGCLoggerWrapper not found! rc=%Rra\n", rc));
736
737 pVM->vmm.s.pRCLoggerRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pRCLoggerR3);
738 rc = RTLogCloneRC(NULL /* default */, pVM->vmm.s.pRCLoggerR3, pVM->vmm.s.cbRCLogger,
739 RCPtrLoggerWrapper, RCPtrLoggerFlush, RTLOGFLAGS_BUFFERED);
740 AssertReleaseMsgRC(rc, ("RTLogCloneRC failed! rc=%Rra\n", rc));
741 }
742
743#ifdef VBOX_WITH_RC_RELEASE_LOGGING
744 if (pVM->vmm.s.pRCRelLoggerR3)
745 {
746 RTRCPTR RCPtrLoggerWrapper = 0;
747 rc = PDMR3LdrGetSymbolRC(pVM, VMMGC_MAIN_MODULE_NAME, "vmmGCRelLoggerWrapper", &RCPtrLoggerWrapper);
748 AssertReleaseMsgRC(rc, ("vmmGCRelLoggerWrapper not found! rc=%Rra\n", rc));
749
750 pVM->vmm.s.pRCRelLoggerRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pRCRelLoggerR3);
751 rc = RTLogCloneRC(RTLogRelDefaultInstance(), pVM->vmm.s.pRCRelLoggerR3, pVM->vmm.s.cbRCRelLogger,
752 RCPtrLoggerWrapper, RCPtrLoggerFlush, RTLOGFLAGS_BUFFERED);
753 AssertReleaseMsgRC(rc, ("RTLogCloneRC failed! rc=%Rra\n", rc));
754 }
755#endif /* VBOX_WITH_RC_RELEASE_LOGGING */
756
757 /*
758 * For the ring-0 EMT logger, we use a per-thread logger instance
759 * in ring-0. Only initialize it once.
760 */
761 for (unsigned i = 0; i < pVM->cCPUs; i++)
762 {
763 PVMCPU pVCpu = &pVM->aCpus[i];
764 PVMMR0LOGGER pR0LoggerR3 = pVCpu->vmm.s.pR0LoggerR3;
765 if (pR0LoggerR3)
766 {
767 if (!pR0LoggerR3->fCreated)
768 {
769 RTR0PTR pfnLoggerWrapper = NIL_RTR0PTR;
770 rc = PDMR3LdrGetSymbolR0(pVM, VMMR0_MAIN_MODULE_NAME, "vmmR0LoggerWrapper", &pfnLoggerWrapper);
771 AssertReleaseMsgRCReturn(rc, ("VMMLoggerWrapper not found! rc=%Rra\n", rc), rc);
772
773 RTR0PTR pfnLoggerFlush = NIL_RTR0PTR;
774 rc = PDMR3LdrGetSymbolR0(pVM, VMMR0_MAIN_MODULE_NAME, "vmmR0LoggerFlush", &pfnLoggerFlush);
775 AssertReleaseMsgRCReturn(rc, ("VMMLoggerFlush not found! rc=%Rra\n", rc), rc);
776
777 rc = RTLogCreateForR0(&pR0LoggerR3->Logger, pR0LoggerR3->cbLogger,
778 *(PFNRTLOGGER *)&pfnLoggerWrapper, *(PFNRTLOGFLUSH *)&pfnLoggerFlush,
779 RTLOGFLAGS_BUFFERED, RTLOGDEST_DUMMY);
780 AssertReleaseMsgRCReturn(rc, ("RTLogCreateForR0 failed! rc=%Rra\n", rc), rc);
781 pR0LoggerR3->fCreated = true;
782 pR0LoggerR3->fFlushingDisabled = false;
783 }
784
785 rc = RTLogCopyGroupsAndFlags(&pR0LoggerR3->Logger, NULL /* default */, pVM->vmm.s.pRCLoggerR3->fFlags, RTLOGFLAGS_BUFFERED);
786 AssertRC(rc);
787 }
788 }
789 return rc;
790}
791
792
793/**
794 * Gets the pointer to a buffer containing the R0/RC AssertMsg1 output.
795 *
796 * @returns Pointer to the buffer.
797 * @param pVM The VM handle.
798 */
799VMMR3DECL(const char *) VMMR3GetRZAssertMsg1(PVM pVM)
800{
801 if (HWACCMIsEnabled(pVM))
802 return pVM->vmm.s.szRing0AssertMsg1;
803
804 RTRCPTR RCPtr;
805 int rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_szRTAssertMsg1", &RCPtr);
806 if (RT_SUCCESS(rc))
807 return (const char *)MMHyperRCToR3(pVM, RCPtr);
808
809 return NULL;
810}
811
812
813/**
814 * Gets the pointer to a buffer containing the R0/RC AssertMsg2 output.
815 *
816 * @returns Pointer to the buffer.
817 * @param pVM The VM handle.
818 */
819VMMR3DECL(const char *) VMMR3GetRZAssertMsg2(PVM pVM)
820{
821 if (HWACCMIsEnabled(pVM))
822 return pVM->vmm.s.szRing0AssertMsg2;
823
824 RTRCPTR RCPtr;
825 int rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_szRTAssertMsg2", &RCPtr);
826 if (RT_SUCCESS(rc))
827 return (const char *)MMHyperRCToR3(pVM, RCPtr);
828
829 return NULL;
830}
831
832
833/**
834 * Execute state save operation.
835 *
836 * @returns VBox status code.
837 * @param pVM VM Handle.
838 * @param pSSM SSM operation handle.
839 */
840static DECLCALLBACK(int) vmmR3Save(PVM pVM, PSSMHANDLE pSSM)
841{
842 LogFlow(("vmmR3Save:\n"));
843
844 /*
845 * The hypervisor stack.
846 * Note! See note in vmmR3Load (remove this on version change).
847 */
848 PVMCPU pVCpu0 = &pVM->aCpus[0];
849 SSMR3PutRCPtr(pSSM, pVCpu0->vmm.s.pbEMTStackBottomRC);
850 RTRCPTR RCPtrESP = CPUMGetHyperESP(pVCpu0);
851 AssertMsg(pVCpu0->vmm.s.pbEMTStackBottomRC - RCPtrESP <= VMM_STACK_SIZE, ("Bottom %RRv ESP=%RRv\n", pVCpu0->vmm.s.pbEMTStackBottomRC, RCPtrESP));
852 SSMR3PutRCPtr(pSSM, RCPtrESP);
853 SSMR3PutMem(pSSM, pVCpu0->vmm.s.pbEMTStackR3, VMM_STACK_SIZE);
854
855 /*
856 * Save the started/stopped state of all CPUs except 0 as it will always
857 * be running. This avoids breaking the saved state version. :-)
858 */
859 for (VMCPUID i = 1; i < pVM->cCPUs; i++)
860 SSMR3PutBool(pSSM, VMCPUSTATE_IS_STARTED(VMCPU_GET_STATE(&pVM->aCpus[i])));
861
862 return SSMR3PutU32(pSSM, ~0); /* terminator */
863}
864
865
866/**
867 * Execute state load operation.
868 *
869 * @returns VBox status code.
870 * @param pVM VM Handle.
871 * @param pSSM SSM operation handle.
872 * @param u32Version Data layout version.
873 */
874static DECLCALLBACK(int) vmmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t u32Version)
875{
876 LogFlow(("vmmR3Load:\n"));
877
878 /*
879 * Validate version.
880 */
881 if (u32Version != VMM_SAVED_STATE_VERSION)
882 {
883 AssertMsgFailed(("vmmR3Load: Invalid version u32Version=%d!\n", u32Version));
884 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
885 }
886
887 /*
888 * Check that the stack is in the same place, or that it's fearly empty.
889 *
890 * Note! This can be skipped next time we update saved state as we will
891 * never be in a R0/RC -> ring-3 call when saving the state. The
892 * stack and the two associated pointers are not required.
893 */
894 RTRCPTR RCPtrStackBottom;
895 SSMR3GetRCPtr(pSSM, &RCPtrStackBottom);
896 RTRCPTR RCPtrESP;
897 int rc = SSMR3GetRCPtr(pSSM, &RCPtrESP);
898 if (RT_FAILURE(rc))
899 return rc;
900 SSMR3GetMem(pSSM, pVM->aCpus[0].vmm.s.pbEMTStackR3, VMM_STACK_SIZE);
901
902 /* Restore the VMCPU states. VCPU 0 is always started. */
903 VMCPU_SET_STATE(&pVM->aCpus[0], VMCPUSTATE_STARTED);
904 for (VMCPUID i = 1; i < pVM->cCPUs; i++)
905 {
906 bool fStarted;
907 rc = SSMR3GetBool(pSSM, &fStarted);
908 if (RT_FAILURE(rc))
909 return rc;
910 VMCPU_SET_STATE(&pVM->aCpus[i], fStarted ? VMCPUSTATE_STARTED : VMCPUSTATE_STOPPED);
911 }
912
913 /* terminator */
914 uint32_t u32;
915 rc = SSMR3GetU32(pSSM, &u32);
916 if (RT_FAILURE(rc))
917 return rc;
918 if (u32 != ~0U)
919 {
920 AssertMsgFailed(("u32=%#x\n", u32));
921 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
922 }
923 return VINF_SUCCESS;
924}
925
926
927/**
928 * Resolve a builtin RC symbol.
929 *
930 * Called by PDM when loading or relocating RC modules.
931 *
932 * @returns VBox status
933 * @param pVM VM Handle.
934 * @param pszSymbol Symbol to resolv
935 * @param pRCPtrValue Where to store the symbol value.
936 *
937 * @remark This has to work before VMMR3Relocate() is called.
938 */
939VMMR3DECL(int) VMMR3GetImportRC(PVM pVM, const char *pszSymbol, PRTRCPTR pRCPtrValue)
940{
941 if (!strcmp(pszSymbol, "g_Logger"))
942 {
943 if (pVM->vmm.s.pRCLoggerR3)
944 pVM->vmm.s.pRCLoggerRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pRCLoggerR3);
945 *pRCPtrValue = pVM->vmm.s.pRCLoggerRC;
946 }
947 else if (!strcmp(pszSymbol, "g_RelLogger"))
948 {
949#ifdef VBOX_WITH_RC_RELEASE_LOGGING
950 if (pVM->vmm.s.pRCRelLoggerR3)
951 pVM->vmm.s.pRCRelLoggerRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pRCRelLoggerR3);
952 *pRCPtrValue = pVM->vmm.s.pRCRelLoggerRC;
953#else
954 *pRCPtrValue = NIL_RTRCPTR;
955#endif
956 }
957 else
958 return VERR_SYMBOL_NOT_FOUND;
959 return VINF_SUCCESS;
960}
961
962
963/**
964 * Suspends the CPU yielder.
965 *
966 * @param pVM The VM handle.
967 */
968VMMR3DECL(void) VMMR3YieldSuspend(PVM pVM)
969{
970 VMCPU_ASSERT_EMT(&pVM->aCpus[0]);
971 if (!pVM->vmm.s.cYieldResumeMillies)
972 {
973 uint64_t u64Now = TMTimerGet(pVM->vmm.s.pYieldTimer);
974 uint64_t u64Expire = TMTimerGetExpire(pVM->vmm.s.pYieldTimer);
975 if (u64Now >= u64Expire || u64Expire == ~(uint64_t)0)
976 pVM->vmm.s.cYieldResumeMillies = pVM->vmm.s.cYieldEveryMillies;
977 else
978 pVM->vmm.s.cYieldResumeMillies = TMTimerToMilli(pVM->vmm.s.pYieldTimer, u64Expire - u64Now);
979 TMTimerStop(pVM->vmm.s.pYieldTimer);
980 }
981 pVM->vmm.s.u64LastYield = RTTimeNanoTS();
982}
983
984
985/**
986 * Stops the CPU yielder.
987 *
988 * @param pVM The VM handle.
989 */
990VMMR3DECL(void) VMMR3YieldStop(PVM pVM)
991{
992 if (!pVM->vmm.s.cYieldResumeMillies)
993 TMTimerStop(pVM->vmm.s.pYieldTimer);
994 pVM->vmm.s.cYieldResumeMillies = pVM->vmm.s.cYieldEveryMillies;
995 pVM->vmm.s.u64LastYield = RTTimeNanoTS();
996}
997
998
999/**
1000 * Resumes the CPU yielder when it has been a suspended or stopped.
1001 *
1002 * @param pVM The VM handle.
1003 */
1004VMMR3DECL(void) VMMR3YieldResume(PVM pVM)
1005{
1006 if (pVM->vmm.s.cYieldResumeMillies)
1007 {
1008 TMTimerSetMillies(pVM->vmm.s.pYieldTimer, pVM->vmm.s.cYieldResumeMillies);
1009 pVM->vmm.s.cYieldResumeMillies = 0;
1010 }
1011}
1012
1013
1014/**
1015 * Internal timer callback function.
1016 *
1017 * @param pVM The VM.
1018 * @param pTimer The timer handle.
1019 * @param pvUser User argument specified upon timer creation.
1020 */
1021static DECLCALLBACK(void) vmmR3YieldEMT(PVM pVM, PTMTIMER pTimer, void *pvUser)
1022{
1023 /*
1024 * This really needs some careful tuning. While we shouldn't be too greedy since
1025 * that'll cause the rest of the system to stop up, we shouldn't be too nice either
1026 * because that'll cause us to stop up.
1027 *
1028 * The current logic is to use the default interval when there is no lag worth
1029 * mentioning, but when we start accumulating lag we don't bother yielding at all.
1030 *
1031 * (This depends on the TMCLOCK_VIRTUAL_SYNC to be scheduled before TMCLOCK_REAL
1032 * so the lag is up to date.)
1033 */
1034 const uint64_t u64Lag = TMVirtualSyncGetLag(pVM);
1035 if ( u64Lag < 50000000 /* 50ms */
1036 || ( u64Lag < 1000000000 /* 1s */
1037 && RTTimeNanoTS() - pVM->vmm.s.u64LastYield < 500000000 /* 500 ms */)
1038 )
1039 {
1040 uint64_t u64Elapsed = RTTimeNanoTS();
1041 pVM->vmm.s.u64LastYield = u64Elapsed;
1042
1043 RTThreadYield();
1044
1045#ifdef LOG_ENABLED
1046 u64Elapsed = RTTimeNanoTS() - u64Elapsed;
1047 Log(("vmmR3YieldEMT: %RI64 ns\n", u64Elapsed));
1048#endif
1049 }
1050 TMTimerSetMillies(pTimer, pVM->vmm.s.cYieldEveryMillies);
1051}
1052
1053
1054/**
1055 * Executes guest code in the raw-mode context.
1056 *
1057 * @param pVM VM handle.
1058 * @param pVCpu The VMCPU to operate on.
1059 */
1060VMMR3DECL(int) VMMR3RawRunGC(PVM pVM, PVMCPU pVCpu)
1061{
1062 Log2(("VMMR3RawRunGC: (cs:eip=%04x:%08x)\n", CPUMGetGuestCS(pVCpu), CPUMGetGuestEIP(pVCpu)));
1063
1064 AssertReturn(pVM->cCPUs == 1, VERR_RAW_MODE_INVALID_SMP);
1065
1066 /*
1067 * Set the EIP and ESP.
1068 */
1069 CPUMSetHyperEIP(pVCpu, CPUMGetGuestEFlags(pVCpu) & X86_EFL_VM
1070 ? pVM->vmm.s.pfnCPUMRCResumeGuestV86
1071 : pVM->vmm.s.pfnCPUMRCResumeGuest);
1072 CPUMSetHyperESP(pVCpu, pVCpu->vmm.s.pbEMTStackBottomRC);
1073
1074 /*
1075 * We hide log flushes (outer) and hypervisor interrupts (inner).
1076 */
1077 for (;;)
1078 {
1079 Assert(CPUMGetHyperCR3(pVCpu) && CPUMGetHyperCR3(pVCpu) == PGMGetHyperCR3(pVCpu));
1080#ifdef VBOX_STRICT
1081 PGMMapCheck(pVM);
1082#endif
1083 int rc;
1084 do
1085 {
1086#ifdef NO_SUPCALLR0VMM
1087 rc = VERR_GENERAL_FAILURE;
1088#else
1089 rc = SUPCallVMMR0Fast(pVM->pVMR0, VMMR0_DO_RAW_RUN, 0);
1090 if (RT_LIKELY(rc == VINF_SUCCESS))
1091 rc = pVCpu->vmm.s.iLastGZRc;
1092#endif
1093 } while (rc == VINF_EM_RAW_INTERRUPT_HYPER);
1094
1095 /*
1096 * Flush the logs.
1097 */
1098#ifdef LOG_ENABLED
1099 PRTLOGGERRC pLogger = pVM->vmm.s.pRCLoggerR3;
1100 if ( pLogger
1101 && pLogger->offScratch > 0)
1102 RTLogFlushRC(NULL, pLogger);
1103#endif
1104#ifdef VBOX_WITH_RC_RELEASE_LOGGING
1105 PRTLOGGERRC pRelLogger = pVM->vmm.s.pRCRelLoggerR3;
1106 if (RT_UNLIKELY(pRelLogger && pRelLogger->offScratch > 0))
1107 RTLogFlushRC(RTLogRelDefaultInstance(), pRelLogger);
1108#endif
1109 if (rc != VINF_VMM_CALL_HOST)
1110 {
1111 Log2(("VMMR3RawRunGC: returns %Rrc (cs:eip=%04x:%08x)\n", rc, CPUMGetGuestCS(pVCpu), CPUMGetGuestEIP(pVCpu)));
1112 return rc;
1113 }
1114 rc = vmmR3ServiceCallHostRequest(pVM, pVCpu);
1115 if (RT_FAILURE(rc))
1116 return rc;
1117 /* Resume GC */
1118 }
1119}
1120
1121
1122/**
1123 * Executes guest code (Intel VT-x and AMD-V).
1124 *
1125 * @param pVM VM handle.
1126 * @param pVCpu The VMCPU to operate on.
1127 */
1128VMMR3DECL(int) VMMR3HwAccRunGC(PVM pVM, PVMCPU pVCpu)
1129{
1130 Log2(("VMMR3HwAccRunGC: (cs:eip=%04x:%08x)\n", CPUMGetGuestCS(pVCpu), CPUMGetGuestEIP(pVCpu)));
1131
1132 for (;;)
1133 {
1134 int rc;
1135 do
1136 {
1137#ifdef NO_SUPCALLR0VMM
1138 rc = VERR_GENERAL_FAILURE;
1139#else
1140 rc = SUPCallVMMR0Fast(pVM->pVMR0, VMMR0_DO_HWACC_RUN, pVCpu->idCpu);
1141 if (RT_LIKELY(rc == VINF_SUCCESS))
1142 rc = pVCpu->vmm.s.iLastGZRc;
1143#endif
1144 } while (rc == VINF_EM_RAW_INTERRUPT_HYPER);
1145
1146#ifdef LOG_ENABLED
1147 /*
1148 * Flush the log
1149 */
1150 PVMMR0LOGGER pR0LoggerR3 = pVCpu->vmm.s.pR0LoggerR3;
1151 if ( pR0LoggerR3
1152 && pR0LoggerR3->Logger.offScratch > 0)
1153 RTLogFlushToLogger(&pR0LoggerR3->Logger, NULL);
1154#endif /* !LOG_ENABLED */
1155 if (rc != VINF_VMM_CALL_HOST)
1156 {
1157 Log2(("VMMR3HwAccRunGC: returns %Rrc (cs:eip=%04x:%08x)\n", rc, CPUMGetGuestCS(pVCpu), CPUMGetGuestEIP(pVCpu)));
1158 return rc;
1159 }
1160 rc = vmmR3ServiceCallHostRequest(pVM, pVCpu);
1161 if (RT_FAILURE(rc))
1162 return rc;
1163 /* Resume R0 */
1164 }
1165}
1166
1167/**
1168 * VCPU worker for VMMSendSipi.
1169 *
1170 * @param pVM The VM to operate on.
1171 * @param idCpu Virtual CPU to perform SIPI on
1172 * @param uVector SIPI vector
1173 */
1174DECLCALLBACK(int) vmmR3SendSipi(PVM pVM, VMCPUID idCpu, uint32_t uVector)
1175{
1176 PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
1177 VMCPU_ASSERT_EMT(pVCpu);
1178
1179 /** @todo what are we supposed to do if the processor is already running? */
1180 if (EMGetState(pVCpu) != EMSTATE_WAIT_SIPI)
1181 return VERR_ACCESS_DENIED;
1182
1183
1184 PCPUMCTX pCtx = CPUMQueryGuestCtxPtr(pVCpu);
1185
1186 pCtx->cs = uVector << 8;
1187 pCtx->csHid.u64Base = uVector << 12;
1188 pCtx->csHid.u32Limit = 0x0000ffff;
1189 pCtx->rip = 0;
1190
1191# if 1 /* If we keep the EMSTATE_WAIT_SIPI method, then move this to EM.cpp. */
1192 EMSetState(pVCpu, EMSTATE_HALTED);
1193 return VINF_EM_RESCHEDULE;
1194# else /* And if we go the VMCPU::enmState way it can stay here. */
1195 VMCPU_ASSERT_STATE(pVCpu, VMCPUSTATE_STOPPED);
1196 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED);
1197 return VINF_SUCCESS;
1198# endif
1199}
1200
1201DECLCALLBACK(int) vmmR3SendInitIpi(PVM pVM, VMCPUID idCpu)
1202{
1203 PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
1204 VMCPU_ASSERT_EMT(pVCpu);
1205
1206 CPUMR3ResetCpu(pVCpu);
1207 return VINF_EM_WAIT_SIPI;
1208}
1209
1210/**
1211 * Sends SIPI to the virtual CPU by setting CS:EIP into vector-dependent state
1212 * and unhalting processor
1213 *
1214 * @param pVM The VM to operate on.
1215 * @param idCpu Virtual CPU to perform SIPI on
1216 * @param uVector SIPI vector
1217 */
1218VMMR3DECL(void) VMMR3SendSipi(PVM pVM, VMCPUID idCpu, uint32_t uVector)
1219{
1220 AssertReturnVoid(idCpu < pVM->cCPUs);
1221
1222 PVMREQ pReq;
1223 int rc = VMR3ReqCallU(pVM->pUVM, idCpu, &pReq, 0, VMREQFLAGS_NO_WAIT,
1224 (PFNRT)vmmR3SendSipi, 3, pVM, idCpu, uVector);
1225 AssertRC(rc);
1226}
1227
1228/**
1229 * Sends init IPI to the virtual CPU.
1230 *
1231 * @param pVM The VM to operate on.
1232 * @param idCpu Virtual CPU to perform int IPI on
1233 */
1234VMMR3DECL(void) VMMR3SendInitIpi(PVM pVM, VMCPUID idCpu)
1235{
1236 AssertReturnVoid(idCpu < pVM->cCPUs);
1237
1238 PVMREQ pReq;
1239 int rc = VMR3ReqCallU(pVM->pUVM, idCpu, &pReq, 0, VMREQFLAGS_NO_WAIT,
1240 (PFNRT)vmmR3SendInitIpi, 2, pVM, idCpu);
1241 AssertRC(rc);
1242}
1243
1244
1245/**
1246 * VCPU worker for VMMR3SynchronizeAllVCpus.
1247 *
1248 * @param pVM The VM to operate on.
1249 * @param idCpu Virtual CPU to perform SIPI on
1250 * @param uVector SIPI vector
1251 */
1252DECLCALLBACK(int) vmmR3SyncVCpu(PVM pVM)
1253{
1254 /* Block until the job in the caller has finished. */
1255 RTCritSectEnter(&pVM->vmm.s.CritSectSync);
1256 RTCritSectLeave(&pVM->vmm.s.CritSectSync);
1257 return VINF_SUCCESS;
1258}
1259
1260
1261/**
1262 * Atomically execute a callback handler
1263 * Note: This is very expensive; avoid using it frequently!
1264 *
1265 * @param pVM The VM to operate on.
1266 * @param pfnHandler Callback handler
1267 * @param pvUser User specified parameter
1268 */
1269VMMR3DECL(int) VMMR3AtomicExecuteHandler(PVM pVM, PFNATOMICHANDLER pfnHandler, void *pvUser)
1270{
1271 int rc;
1272 PVMCPU pVCpu = VMMGetCpu(pVM);
1273 AssertReturn(pVCpu, VERR_VM_THREAD_NOT_EMT);
1274
1275 /* Shortcut for the uniprocessor case. */
1276 if (pVM->cCPUs == 1)
1277 return pfnHandler(pVM, pvUser);
1278
1279 RTCritSectEnter(&pVM->vmm.s.CritSectSync);
1280 for (VMCPUID idCpu = 0; idCpu < pVM->cCPUs; idCpu++)
1281 {
1282 if (idCpu != pVCpu->idCpu)
1283 {
1284 rc = VMR3ReqCallU(pVM->pUVM, idCpu, NULL, 0, VMREQFLAGS_NO_WAIT,
1285 (PFNRT)vmmR3SyncVCpu, 1, pVM);
1286 AssertRC(rc);
1287 }
1288 }
1289 /* Wait until all other VCPUs are waiting for us. */
1290 while (RTCritSectGetWaiters(&pVM->vmm.s.CritSectSync) != (int32_t)(pVM->cCPUs - 1))
1291 RTThreadSleep(1);
1292
1293 rc = pfnHandler(pVM, pvUser);
1294 RTCritSectLeave(&pVM->vmm.s.CritSectSync);
1295 return rc;
1296}
1297
1298
1299/**
1300 * Read from the ring 0 jump buffer stack
1301 *
1302 * @returns VBox status code.
1303 *
1304 * @param pVM Pointer to the shared VM structure.
1305 * @param idCpu The ID of the source CPU context (for the address).
1306 * @param pAddress Where to start reading.
1307 * @param pvBuf Where to store the data we've read.
1308 * @param cbRead The number of bytes to read.
1309 */
1310VMMR3DECL(int) VMMR3ReadR0Stack(PVM pVM, VMCPUID idCpu, RTHCUINTPTR pAddress, void *pvBuf, size_t cbRead)
1311{
1312 PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
1313 AssertReturn(pVCpu, VERR_INVALID_PARAMETER);
1314
1315 RTHCUINTPTR offset = pVCpu->vmm.s.CallHostR0JmpBuf.SpCheck - pAddress;
1316 if (offset >= pVCpu->vmm.s.CallHostR0JmpBuf.cbSavedStack)
1317 return VERR_INVALID_POINTER;
1318
1319 memcpy(pvBuf, pVCpu->vmm.s.pbEMTStackR3 + pVCpu->vmm.s.CallHostR0JmpBuf.cbSavedStack - offset, cbRead);
1320 return VINF_SUCCESS;
1321}
1322
1323
1324/**
1325 * Calls a RC function.
1326 *
1327 * @param pVM The VM handle.
1328 * @param RCPtrEntry The address of the RC function.
1329 * @param cArgs The number of arguments in the ....
1330 * @param ... Arguments to the function.
1331 */
1332VMMR3DECL(int) VMMR3CallRC(PVM pVM, RTRCPTR RCPtrEntry, unsigned cArgs, ...)
1333{
1334 va_list args;
1335 va_start(args, cArgs);
1336 int rc = VMMR3CallRCV(pVM, RCPtrEntry, cArgs, args);
1337 va_end(args);
1338 return rc;
1339}
1340
1341
1342/**
1343 * Calls a RC function.
1344 *
1345 * @param pVM The VM handle.
1346 * @param RCPtrEntry The address of the RC function.
1347 * @param cArgs The number of arguments in the ....
1348 * @param args Arguments to the function.
1349 */
1350VMMR3DECL(int) VMMR3CallRCV(PVM pVM, RTRCPTR RCPtrEntry, unsigned cArgs, va_list args)
1351{
1352 /* Raw mode implies 1 VCPU. */
1353 AssertReturn(pVM->cCPUs == 1, VERR_RAW_MODE_INVALID_SMP);
1354 PVMCPU pVCpu = &pVM->aCpus[0];
1355
1356 Log2(("VMMR3CallGCV: RCPtrEntry=%RRv cArgs=%d\n", RCPtrEntry, cArgs));
1357
1358 /*
1359 * Setup the call frame using the trampoline.
1360 */
1361 CPUMHyperSetCtxCore(pVCpu, NULL);
1362 memset(pVCpu->vmm.s.pbEMTStackR3, 0xaa, VMM_STACK_SIZE); /* Clear the stack. */
1363 CPUMSetHyperESP(pVCpu, pVCpu->vmm.s.pbEMTStackBottomRC - cArgs * sizeof(RTGCUINTPTR32));
1364 PRTGCUINTPTR32 pFrame = (PRTGCUINTPTR32)(pVCpu->vmm.s.pbEMTStackR3 + VMM_STACK_SIZE) - cArgs;
1365 int i = cArgs;
1366 while (i-- > 0)
1367 *pFrame++ = va_arg(args, RTGCUINTPTR32);
1368
1369 CPUMPushHyper(pVCpu, cArgs * sizeof(RTGCUINTPTR32)); /* stack frame size */
1370 CPUMPushHyper(pVCpu, RCPtrEntry); /* what to call */
1371 CPUMSetHyperEIP(pVCpu, pVM->vmm.s.pfnCallTrampolineRC);
1372
1373 /*
1374 * We hide log flushes (outer) and hypervisor interrupts (inner).
1375 */
1376 for (;;)
1377 {
1378 int rc;
1379 Assert(CPUMGetHyperCR3(pVCpu) && CPUMGetHyperCR3(pVCpu) == PGMGetHyperCR3(pVCpu));
1380 do
1381 {
1382#ifdef NO_SUPCALLR0VMM
1383 rc = VERR_GENERAL_FAILURE;
1384#else
1385 rc = SUPCallVMMR0Fast(pVM->pVMR0, VMMR0_DO_RAW_RUN, 0);
1386 if (RT_LIKELY(rc == VINF_SUCCESS))
1387 rc = pVCpu->vmm.s.iLastGZRc;
1388#endif
1389 } while (rc == VINF_EM_RAW_INTERRUPT_HYPER);
1390
1391 /*
1392 * Flush the logs.
1393 */
1394#ifdef LOG_ENABLED
1395 PRTLOGGERRC pLogger = pVM->vmm.s.pRCLoggerR3;
1396 if ( pLogger
1397 && pLogger->offScratch > 0)
1398 RTLogFlushRC(NULL, pLogger);
1399#endif
1400#ifdef VBOX_WITH_RC_RELEASE_LOGGING
1401 PRTLOGGERRC pRelLogger = pVM->vmm.s.pRCRelLoggerR3;
1402 if (RT_UNLIKELY(pRelLogger && pRelLogger->offScratch > 0))
1403 RTLogFlushRC(RTLogRelDefaultInstance(), pRelLogger);
1404#endif
1405 if (rc == VERR_TRPM_PANIC || rc == VERR_TRPM_DONT_PANIC)
1406 VMMR3FatalDump(pVM, pVCpu, rc);
1407 if (rc != VINF_VMM_CALL_HOST)
1408 {
1409 Log2(("VMMR3CallGCV: returns %Rrc (cs:eip=%04x:%08x)\n", rc, CPUMGetGuestCS(pVCpu), CPUMGetGuestEIP(pVCpu)));
1410 return rc;
1411 }
1412 rc = vmmR3ServiceCallHostRequest(pVM, pVCpu);
1413 if (RT_FAILURE(rc))
1414 return rc;
1415 }
1416}
1417
1418
1419/**
1420 * Wrapper for SUPCallVMMR0Ex which will deal with
1421 * VINF_VMM_CALL_HOST returns.
1422 *
1423 * @returns VBox status code.
1424 * @param pVM The VM to operate on.
1425 * @param uOperation Operation to execute.
1426 * @param u64Arg Constant argument.
1427 * @param pReqHdr Pointer to a request header. See SUPCallVMMR0Ex for
1428 * details.
1429 */
1430VMMR3DECL(int) VMMR3CallR0(PVM pVM, uint32_t uOperation, uint64_t u64Arg, PSUPVMMR0REQHDR pReqHdr)
1431{
1432 PVMCPU pVCpu = VMMGetCpu(pVM);
1433 AssertReturn(pVCpu, VERR_VM_THREAD_NOT_EMT);
1434
1435 /*
1436 * Call Ring-0 entry with init code.
1437 */
1438 int rc;
1439 for (;;)
1440 {
1441#ifdef NO_SUPCALLR0VMM
1442 rc = VERR_GENERAL_FAILURE;
1443#else
1444 rc = SUPCallVMMR0Ex(pVM->pVMR0, pVCpu->idCpu, uOperation, u64Arg, pReqHdr);
1445#endif
1446 if ( pVCpu->vmm.s.pR0LoggerR3
1447 && pVCpu->vmm.s.pR0LoggerR3->Logger.offScratch > 0)
1448 RTLogFlushToLogger(&pVCpu->vmm.s.pR0LoggerR3->Logger, NULL);
1449 if (rc != VINF_VMM_CALL_HOST)
1450 break;
1451 rc = vmmR3ServiceCallHostRequest(pVM, pVCpu);
1452 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
1453 break;
1454 /* Resume R0 */
1455 }
1456
1457 AssertLogRelMsgReturn(rc == VINF_SUCCESS || VBOX_FAILURE(rc),
1458 ("uOperation=%u rc=%Rrc\n", uOperation, rc),
1459 VERR_INTERNAL_ERROR);
1460 return rc;
1461}
1462
1463
1464/**
1465 * Resumes executing hypervisor code when interrupted by a queue flush or a
1466 * debug event.
1467 *
1468 * @returns VBox status code.
1469 * @param pVM VM handle.
1470 * @param pVCpu VMCPU handle.
1471 */
1472VMMR3DECL(int) VMMR3ResumeHyper(PVM pVM, PVMCPU pVCpu)
1473{
1474 Log(("VMMR3ResumeHyper: eip=%RRv esp=%RRv\n", CPUMGetHyperEIP(pVCpu), CPUMGetHyperESP(pVCpu)));
1475 AssertReturn(pVM->cCPUs == 1, VERR_RAW_MODE_INVALID_SMP);
1476
1477 /*
1478 * We hide log flushes (outer) and hypervisor interrupts (inner).
1479 */
1480 for (;;)
1481 {
1482 int rc;
1483 Assert(CPUMGetHyperCR3(pVCpu) && CPUMGetHyperCR3(pVCpu) == PGMGetHyperCR3(pVCpu));
1484 do
1485 {
1486#ifdef NO_SUPCALLR0VMM
1487 rc = VERR_GENERAL_FAILURE;
1488#else
1489 rc = SUPCallVMMR0Fast(pVM->pVMR0, VMMR0_DO_RAW_RUN, 0);
1490 if (RT_LIKELY(rc == VINF_SUCCESS))
1491 rc = pVCpu->vmm.s.iLastGZRc;
1492#endif
1493 } while (rc == VINF_EM_RAW_INTERRUPT_HYPER);
1494
1495 /*
1496 * Flush the loggers,
1497 */
1498#ifdef LOG_ENABLED
1499 PRTLOGGERRC pLogger = pVM->vmm.s.pRCLoggerR3;
1500 if ( pLogger
1501 && pLogger->offScratch > 0)
1502 RTLogFlushRC(NULL, pLogger);
1503#endif
1504#ifdef VBOX_WITH_RC_RELEASE_LOGGING
1505 PRTLOGGERRC pRelLogger = pVM->vmm.s.pRCRelLoggerR3;
1506 if (RT_UNLIKELY(pRelLogger && pRelLogger->offScratch > 0))
1507 RTLogFlushRC(RTLogRelDefaultInstance(), pRelLogger);
1508#endif
1509 if (rc == VERR_TRPM_PANIC || rc == VERR_TRPM_DONT_PANIC)
1510 VMMR3FatalDump(pVM, pVCpu, rc);
1511 if (rc != VINF_VMM_CALL_HOST)
1512 {
1513 Log(("VMMR3ResumeHyper: returns %Rrc\n", rc));
1514 return rc;
1515 }
1516 rc = vmmR3ServiceCallHostRequest(pVM, pVCpu);
1517 if (RT_FAILURE(rc))
1518 return rc;
1519 }
1520}
1521
1522
1523/**
1524 * Service a call to the ring-3 host code.
1525 *
1526 * @returns VBox status code.
1527 * @param pVM VM handle.
1528 * @param pVCpu VMCPU handle
1529 * @remark Careful with critsects.
1530 */
1531static int vmmR3ServiceCallHostRequest(PVM pVM, PVMCPU pVCpu)
1532{
1533 /* We must also check for pending releases or else we can deadlock when acquiring a new lock here.
1534 * On return we go straight back to R0/GC.
1535 */
1536 if (VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_PDM_CRITSECT))
1537 PDMR3CritSectFF(pVCpu);
1538
1539 switch (pVCpu->vmm.s.enmCallHostOperation)
1540 {
1541 /*
1542 * Acquire the PDM lock.
1543 */
1544 case VMMCALLHOST_PDM_LOCK:
1545 {
1546 pVCpu->vmm.s.rcCallHost = PDMR3LockCall(pVM);
1547 break;
1548 }
1549
1550 /*
1551 * Flush a PDM queue.
1552 */
1553 case VMMCALLHOST_PDM_QUEUE_FLUSH:
1554 {
1555 PDMR3QueueFlushWorker(pVM, NULL);
1556 pVCpu->vmm.s.rcCallHost = VINF_SUCCESS;
1557 break;
1558 }
1559
1560 /*
1561 * Grow the PGM pool.
1562 */
1563 case VMMCALLHOST_PGM_POOL_GROW:
1564 {
1565 pVCpu->vmm.s.rcCallHost = PGMR3PoolGrow(pVM);
1566 break;
1567 }
1568
1569 /*
1570 * Maps an page allocation chunk into ring-3 so ring-0 can use it.
1571 */
1572 case VMMCALLHOST_PGM_MAP_CHUNK:
1573 {
1574 pVCpu->vmm.s.rcCallHost = PGMR3PhysChunkMap(pVM, pVCpu->vmm.s.u64CallHostArg);
1575 break;
1576 }
1577
1578 /*
1579 * Allocates more handy pages.
1580 */
1581 case VMMCALLHOST_PGM_ALLOCATE_HANDY_PAGES:
1582 {
1583 pVCpu->vmm.s.rcCallHost = PGMR3PhysAllocateHandyPages(pVM);
1584 break;
1585 }
1586
1587 /*
1588 * Acquire the PGM lock.
1589 */
1590 case VMMCALLHOST_PGM_LOCK:
1591 {
1592 pVCpu->vmm.s.rcCallHost = PGMR3LockCall(pVM);
1593 break;
1594 }
1595
1596 /*
1597 * Acquire the MM hypervisor heap lock.
1598 */
1599 case VMMCALLHOST_MMHYPER_LOCK:
1600 {
1601 pVCpu->vmm.s.rcCallHost = MMR3LockCall(pVM);
1602 break;
1603 }
1604
1605 /*
1606 * Flush REM handler notifications.
1607 */
1608 case VMMCALLHOST_REM_REPLAY_HANDLER_NOTIFICATIONS:
1609 {
1610 REMR3ReplayHandlerNotifications(pVM);
1611 pVCpu->vmm.s.rcCallHost = VINF_SUCCESS;
1612 break;
1613 }
1614
1615 /*
1616 * This is a noop. We just take this route to avoid unnecessary
1617 * tests in the loops.
1618 */
1619 case VMMCALLHOST_VMM_LOGGER_FLUSH:
1620 pVCpu->vmm.s.rcCallHost = VINF_SUCCESS;
1621 LogAlways(("*FLUSH*\n"));
1622 break;
1623
1624 /*
1625 * Set the VM error message.
1626 */
1627 case VMMCALLHOST_VM_SET_ERROR:
1628 VMR3SetErrorWorker(pVM);
1629 pVCpu->vmm.s.rcCallHost = VINF_SUCCESS;
1630 break;
1631
1632 /*
1633 * Set the VM runtime error message.
1634 */
1635 case VMMCALLHOST_VM_SET_RUNTIME_ERROR:
1636 pVCpu->vmm.s.rcCallHost = VMR3SetRuntimeErrorWorker(pVM);
1637 break;
1638
1639 /*
1640 * Signal a ring 0 hypervisor assertion.
1641 * Cancel the longjmp operation that's in progress.
1642 */
1643 case VMMCALLHOST_VM_R0_ASSERTION:
1644 pVCpu->vmm.s.enmCallHostOperation = VMMCALLHOST_INVALID;
1645 pVCpu->vmm.s.CallHostR0JmpBuf.fInRing3Call = false;
1646#ifdef RT_ARCH_X86
1647 pVCpu->vmm.s.CallHostR0JmpBuf.eip = 0;
1648#else
1649 pVCpu->vmm.s.CallHostR0JmpBuf.rip = 0;
1650#endif
1651 LogRel((pVM->vmm.s.szRing0AssertMsg1));
1652 LogRel((pVM->vmm.s.szRing0AssertMsg2));
1653 return VERR_VMM_RING0_ASSERTION;
1654
1655 /*
1656 * A forced switch to ring 0 for preemption purposes.
1657 */
1658 case VMMCALLHOST_VM_R0_PREEMPT:
1659 pVCpu->vmm.s.rcCallHost = VINF_SUCCESS;
1660 break;
1661
1662 default:
1663 AssertMsgFailed(("enmCallHostOperation=%d\n", pVCpu->vmm.s.enmCallHostOperation));
1664 return VERR_INTERNAL_ERROR;
1665 }
1666
1667 pVCpu->vmm.s.enmCallHostOperation = VMMCALLHOST_INVALID;
1668 return VINF_SUCCESS;
1669}
1670
1671
1672/**
1673 * Displays the Force action Flags.
1674 *
1675 * @param pVM The VM handle.
1676 * @param pHlp The output helpers.
1677 * @param pszArgs The additional arguments (ignored).
1678 */
1679static DECLCALLBACK(void) vmmR3InfoFF(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
1680{
1681 int c;
1682 uint32_t f;
1683#define PRINT_FLAG(prf,flag) do { \
1684 if (f & (prf##flag)) \
1685 { \
1686 static const char *s_psz = #flag; \
1687 if (!(c % 6)) \
1688 pHlp->pfnPrintf(pHlp, "%s\n %s", c ? "," : "", s_psz); \
1689 else \
1690 pHlp->pfnPrintf(pHlp, ", %s", s_psz); \
1691 c++; \
1692 f &= ~(prf##flag); \
1693 } \
1694 } while (0)
1695
1696#define PRINT_GROUP(prf,grp,sfx) do { \
1697 if (f & (prf##grp##sfx)) \
1698 { \
1699 static const char *s_psz = #grp; \
1700 if (!(c % 5)) \
1701 pHlp->pfnPrintf(pHlp, "%s %s", c ? ",\n" : " Groups:\n", s_psz); \
1702 else \
1703 pHlp->pfnPrintf(pHlp, ", %s", s_psz); \
1704 c++; \
1705 } \
1706 } while (0)
1707
1708 /*
1709 * The global flags.
1710 */
1711 const uint32_t fGlobalForcedActions = pVM->fGlobalForcedActions;
1712 pHlp->pfnPrintf(pHlp, "Global FFs: %#RX32", fGlobalForcedActions);
1713
1714 /* show the flag mnemonics */
1715 c = 0;
1716 f = fGlobalForcedActions;
1717 PRINT_FLAG(VM_FF_,TM_VIRTUAL_SYNC);
1718 PRINT_FLAG(VM_FF_,PDM_QUEUES);
1719 PRINT_FLAG(VM_FF_,PDM_DMA);
1720 PRINT_FLAG(VM_FF_,DBGF);
1721 PRINT_FLAG(VM_FF_,REQUEST);
1722 PRINT_FLAG(VM_FF_,TERMINATE);
1723 PRINT_FLAG(VM_FF_,RESET);
1724 PRINT_FLAG(VM_FF_,PGM_NEED_HANDY_PAGES);
1725 PRINT_FLAG(VM_FF_,PGM_NO_MEMORY);
1726 PRINT_FLAG(VM_FF_,REM_HANDLER_NOTIFY);
1727 PRINT_FLAG(VM_FF_,DEBUG_SUSPEND);
1728 if (f)
1729 pHlp->pfnPrintf(pHlp, "%s\n Unknown bits: %#RX32\n", c ? "," : "", f);
1730 else
1731 pHlp->pfnPrintf(pHlp, "\n");
1732
1733 /* the groups */
1734 c = 0;
1735 f = fGlobalForcedActions;
1736 PRINT_GROUP(VM_FF_,EXTERNAL_SUSPENDED,_MASK);
1737 PRINT_GROUP(VM_FF_,EXTERNAL_HALTED,_MASK);
1738 PRINT_GROUP(VM_FF_,HIGH_PRIORITY_PRE,_MASK);
1739 PRINT_GROUP(VM_FF_,HIGH_PRIORITY_PRE_RAW,_MASK);
1740 PRINT_GROUP(VM_FF_,HIGH_PRIORITY_POST,_MASK);
1741 PRINT_GROUP(VM_FF_,NORMAL_PRIORITY_POST,_MASK);
1742 PRINT_GROUP(VM_FF_,NORMAL_PRIORITY,_MASK);
1743 PRINT_GROUP(VM_FF_,ALL_BUT_RAW,_MASK);
1744 if (c)
1745 pHlp->pfnPrintf(pHlp, "\n");
1746
1747 /*
1748 * Per CPU flags.
1749 */
1750 for (VMCPUID i = 0; i < pVM->cCPUs; i++)
1751 {
1752 const uint32_t fLocalForcedActions = pVM->aCpus[i].fLocalForcedActions;
1753 pHlp->pfnPrintf(pHlp, "CPU %u FFs: %#RX32", i, fLocalForcedActions);
1754
1755 /* show the flag mnemonics */
1756 c = 0;
1757 f = fLocalForcedActions;
1758 PRINT_FLAG(VMCPU_FF_,INTERRUPT_APIC);
1759 PRINT_FLAG(VMCPU_FF_,INTERRUPT_PIC);
1760 PRINT_FLAG(VMCPU_FF_,TIMER);
1761 PRINT_FLAG(VMCPU_FF_,PDM_CRITSECT);
1762 PRINT_FLAG(VMCPU_FF_,PGM_SYNC_CR3);
1763 PRINT_FLAG(VMCPU_FF_,PGM_SYNC_CR3_NON_GLOBAL);
1764 PRINT_FLAG(VMCPU_FF_,TRPM_SYNC_IDT);
1765 PRINT_FLAG(VMCPU_FF_,SELM_SYNC_TSS);
1766 PRINT_FLAG(VMCPU_FF_,SELM_SYNC_GDT);
1767 PRINT_FLAG(VMCPU_FF_,SELM_SYNC_LDT);
1768 PRINT_FLAG(VMCPU_FF_,INHIBIT_INTERRUPTS);
1769 PRINT_FLAG(VMCPU_FF_,CSAM_SCAN_PAGE);
1770 PRINT_FLAG(VMCPU_FF_,CSAM_PENDING_ACTION);
1771 PRINT_FLAG(VMCPU_FF_,TO_R3);
1772 if (f)
1773 pHlp->pfnPrintf(pHlp, "%s\n Unknown bits: %#RX32\n", c ? "," : "", f);
1774 else
1775 pHlp->pfnPrintf(pHlp, "\n");
1776
1777 /* the groups */
1778 c = 0;
1779 f = fLocalForcedActions;
1780 PRINT_GROUP(VMCPU_FF_,EXTERNAL_SUSPENDED,_MASK);
1781 PRINT_GROUP(VMCPU_FF_,EXTERNAL_HALTED,_MASK);
1782 PRINT_GROUP(VMCPU_FF_,HIGH_PRIORITY_PRE,_MASK);
1783 PRINT_GROUP(VMCPU_FF_,HIGH_PRIORITY_PRE_RAW,_MASK);
1784 PRINT_GROUP(VMCPU_FF_,HIGH_PRIORITY_POST,_MASK);
1785 PRINT_GROUP(VMCPU_FF_,NORMAL_PRIORITY_POST,_MASK);
1786 PRINT_GROUP(VMCPU_FF_,NORMAL_PRIORITY,_MASK);
1787 PRINT_GROUP(VMCPU_FF_,RESUME_GUEST,_MASK);
1788 PRINT_GROUP(VMCPU_FF_,HWACCM_TO_R3,_MASK);
1789 PRINT_GROUP(VMCPU_FF_,ALL_BUT_RAW,_MASK);
1790 if (c)
1791 pHlp->pfnPrintf(pHlp, "\n");
1792 }
1793
1794#undef PRINT_FLAG
1795#undef PRINT_GROUP
1796}
1797
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