VirtualBox

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

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

Updates for ring 0 call stack dumping. (not enabled nor tested)

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