VirtualBox

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

Last change on this file since 20543 was 20533, checked in by vboxsync, 15 years ago

VMM: Guard the hyper stack in strict builds using the new MMR3HyperSetGuard API.

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