VirtualBox

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

Last change on this file since 20522 was 20406, checked in by vboxsync, 16 years ago

Removed obsolete REMR3ReplayInvalidatedPages

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