VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/VMM.cpp@ 68155

Last change on this file since 68155 was 66848, checked in by vboxsync, 8 years ago

VMM: use RT_LO/HI_U32 at certain places

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 111.5 KB
Line 
1/* $Id: VMM.cpp 66848 2017-05-09 13:04:57Z vboxsync $ */
2/** @file
3 * VMM - The Virtual Machine Monitor Core.
4 */
5
6/*
7 * Copyright (C) 2006-2016 Oracle Corporation
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
18//#define NO_SUPCALLR0VMM
19
20/** @page pg_vmm VMM - The Virtual Machine Monitor
21 *
22 * The VMM component is two things at the moment, it's a component doing a few
23 * management and routing tasks, and it's the whole virtual machine monitor
24 * thing. For hysterical reasons, it is not doing all the management that one
25 * would expect, this is instead done by @ref pg_vm. We'll address this
26 * misdesign eventually, maybe.
27 *
28 * VMM is made up of these components:
29 * - @subpage pg_cfgm
30 * - @subpage pg_cpum
31 * - @subpage pg_csam
32 * - @subpage pg_dbgf
33 * - @subpage pg_em
34 * - @subpage pg_gim
35 * - @subpage pg_gmm
36 * - @subpage pg_gvmm
37 * - @subpage pg_hm
38 * - @subpage pg_iem
39 * - @subpage pg_iom
40 * - @subpage pg_mm
41 * - @subpage pg_patm
42 * - @subpage pg_pdm
43 * - @subpage pg_pgm
44 * - @subpage pg_rem
45 * - @subpage pg_selm
46 * - @subpage pg_ssm
47 * - @subpage pg_stam
48 * - @subpage pg_tm
49 * - @subpage pg_trpm
50 * - @subpage pg_vm
51 *
52 *
53 * @see @ref grp_vmm @ref grp_vm @subpage pg_vmm_guideline @subpage pg_raw
54 *
55 *
56 * @section sec_vmmstate VMM State
57 *
58 * @image html VM_Statechart_Diagram.gif
59 *
60 * To be written.
61 *
62 *
63 * @subsection subsec_vmm_init VMM Initialization
64 *
65 * To be written.
66 *
67 *
68 * @subsection subsec_vmm_term VMM Termination
69 *
70 * To be written.
71 *
72 *
73 * @section sec_vmm_limits VMM Limits
74 *
75 * There are various resource limits imposed by the VMM and it's
76 * sub-components. We'll list some of them here.
77 *
78 * On 64-bit hosts:
79 * - Max 8191 VMs. Imposed by GVMM's handle allocation (GVMM_MAX_HANDLES),
80 * can be increased up to 64K - 1.
81 * - Max 16TB - 64KB of the host memory can be used for backing VM RAM and
82 * ROM pages. The limit is imposed by the 32-bit page ID used by GMM.
83 * - A VM can be assigned all the memory we can use (16TB), however, the
84 * Main API will restrict this to 2TB (MM_RAM_MAX_IN_MB).
85 * - Max 32 virtual CPUs (VMM_MAX_CPU_COUNT).
86 *
87 * On 32-bit hosts:
88 * - Max 127 VMs. Imposed by GMM's per page structure.
89 * - Max 64GB - 64KB of the host memory can be used for backing VM RAM and
90 * ROM pages. The limit is imposed by the 28-bit page ID used
91 * internally in GMM. It is also limited by PAE.
92 * - A VM can be assigned all the memory GMM can allocate, however, the
93 * Main API will restrict this to 3584MB (MM_RAM_MAX_IN_MB).
94 * - Max 32 virtual CPUs (VMM_MAX_CPU_COUNT).
95 *
96 */
97
98
99/*********************************************************************************************************************************
100* Header Files *
101*********************************************************************************************************************************/
102#define LOG_GROUP LOG_GROUP_VMM
103#include <VBox/vmm/vmm.h>
104#include <VBox/vmm/vmapi.h>
105#include <VBox/vmm/pgm.h>
106#include <VBox/vmm/cfgm.h>
107#include <VBox/vmm/pdmqueue.h>
108#include <VBox/vmm/pdmcritsect.h>
109#include <VBox/vmm/pdmcritsectrw.h>
110#include <VBox/vmm/pdmapi.h>
111#include <VBox/vmm/cpum.h>
112#include <VBox/vmm/gim.h>
113#include <VBox/vmm/mm.h>
114#include <VBox/vmm/iom.h>
115#include <VBox/vmm/trpm.h>
116#include <VBox/vmm/selm.h>
117#include <VBox/vmm/em.h>
118#include <VBox/sup.h>
119#include <VBox/vmm/dbgf.h>
120#include <VBox/vmm/csam.h>
121#include <VBox/vmm/patm.h>
122#include <VBox/vmm/apic.h>
123#ifdef VBOX_WITH_REM
124# include <VBox/vmm/rem.h>
125#endif
126#include <VBox/vmm/ssm.h>
127#include <VBox/vmm/ftm.h>
128#include <VBox/vmm/tm.h>
129#include "VMMInternal.h"
130#include "VMMSwitcher.h"
131#include <VBox/vmm/vm.h>
132#include <VBox/vmm/uvm.h>
133
134#include <VBox/err.h>
135#include <VBox/param.h>
136#include <VBox/version.h>
137#include <VBox/vmm/hm.h>
138#include <iprt/assert.h>
139#include <iprt/alloc.h>
140#include <iprt/asm.h>
141#include <iprt/time.h>
142#include <iprt/semaphore.h>
143#include <iprt/stream.h>
144#include <iprt/string.h>
145#include <iprt/stdarg.h>
146#include <iprt/ctype.h>
147#include <iprt/x86.h>
148
149
150/*********************************************************************************************************************************
151* Defined Constants And Macros *
152*********************************************************************************************************************************/
153/** The saved state version. */
154#define VMM_SAVED_STATE_VERSION 4
155/** The saved state version used by v3.0 and earlier. (Teleportation) */
156#define VMM_SAVED_STATE_VERSION_3_0 3
157
158
159/*********************************************************************************************************************************
160* Internal Functions *
161*********************************************************************************************************************************/
162static int vmmR3InitStacks(PVM pVM);
163static int vmmR3InitLoggers(PVM pVM);
164static void vmmR3InitRegisterStats(PVM pVM);
165static DECLCALLBACK(int) vmmR3Save(PVM pVM, PSSMHANDLE pSSM);
166static DECLCALLBACK(int) vmmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
167static DECLCALLBACK(void) vmmR3YieldEMT(PVM pVM, PTMTIMER pTimer, void *pvUser);
168static VBOXSTRICTRC vmmR3EmtRendezvousCommon(PVM pVM, PVMCPU pVCpu, bool fIsCaller,
169 uint32_t fFlags, PFNVMMEMTRENDEZVOUS pfnRendezvous, void *pvUser);
170static int vmmR3ServiceCallRing3Request(PVM pVM, PVMCPU pVCpu);
171static DECLCALLBACK(void) vmmR3InfoFF(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
172
173
174/**
175 * Initializes the VMM.
176 *
177 * @returns VBox status code.
178 * @param pVM The cross context VM structure.
179 */
180VMMR3_INT_DECL(int) VMMR3Init(PVM pVM)
181{
182 LogFlow(("VMMR3Init\n"));
183
184 /*
185 * Assert alignment, sizes and order.
186 */
187 AssertMsg(pVM->vmm.s.offVM == 0, ("Already initialized!\n"));
188 AssertCompile(sizeof(pVM->vmm.s) <= sizeof(pVM->vmm.padding));
189 AssertCompile(sizeof(pVM->aCpus[0].vmm.s) <= sizeof(pVM->aCpus[0].vmm.padding));
190
191 /*
192 * Init basic VM VMM members.
193 */
194 pVM->vmm.s.offVM = RT_OFFSETOF(VM, vmm);
195 pVM->vmm.s.pahEvtRendezvousEnterOrdered = NULL;
196 pVM->vmm.s.hEvtRendezvousEnterOneByOne = NIL_RTSEMEVENT;
197 pVM->vmm.s.hEvtMulRendezvousEnterAllAtOnce = NIL_RTSEMEVENTMULTI;
198 pVM->vmm.s.hEvtMulRendezvousDone = NIL_RTSEMEVENTMULTI;
199 pVM->vmm.s.hEvtRendezvousDoneCaller = NIL_RTSEMEVENT;
200 pVM->vmm.s.hEvtMulRendezvousRecursionPush = NIL_RTSEMEVENTMULTI;
201 pVM->vmm.s.hEvtMulRendezvousRecursionPop = NIL_RTSEMEVENTMULTI;
202 pVM->vmm.s.hEvtRendezvousRecursionPushCaller = NIL_RTSEMEVENT;
203 pVM->vmm.s.hEvtRendezvousRecursionPopCaller = NIL_RTSEMEVENT;
204
205 /** @cfgm{/YieldEMTInterval, uint32_t, 1, UINT32_MAX, 23, ms}
206 * The EMT yield interval. The EMT yielding is a hack we employ to play a
207 * bit nicer with the rest of the system (like for instance the GUI).
208 */
209 int rc = CFGMR3QueryU32Def(CFGMR3GetRoot(pVM), "YieldEMTInterval", &pVM->vmm.s.cYieldEveryMillies,
210 23 /* Value arrived at after experimenting with the grub boot prompt. */);
211 AssertMsgRCReturn(rc, ("Configuration error. Failed to query \"YieldEMTInterval\", rc=%Rrc\n", rc), rc);
212
213
214 /** @cfgm{/VMM/UsePeriodicPreemptionTimers, boolean, true}
215 * Controls whether we employ per-cpu preemption timers to limit the time
216 * spent executing guest code. This option is not available on all
217 * platforms and we will silently ignore this setting then. If we are
218 * running in VT-x mode, we will use the VMX-preemption timer instead of
219 * this one when possible.
220 */
221 PCFGMNODE pCfgVMM = CFGMR3GetChild(CFGMR3GetRoot(pVM), "VMM");
222 rc = CFGMR3QueryBoolDef(pCfgVMM, "UsePeriodicPreemptionTimers", &pVM->vmm.s.fUsePeriodicPreemptionTimers, true);
223 AssertMsgRCReturn(rc, ("Configuration error. Failed to query \"VMM/UsePeriodicPreemptionTimers\", rc=%Rrc\n", rc), rc);
224
225 /*
226 * Initialize the VMM rendezvous semaphores.
227 */
228 pVM->vmm.s.pahEvtRendezvousEnterOrdered = (PRTSEMEVENT)MMR3HeapAlloc(pVM, MM_TAG_VMM, sizeof(RTSEMEVENT) * pVM->cCpus);
229 if (!pVM->vmm.s.pahEvtRendezvousEnterOrdered)
230 return VERR_NO_MEMORY;
231 for (VMCPUID i = 0; i < pVM->cCpus; i++)
232 pVM->vmm.s.pahEvtRendezvousEnterOrdered[i] = NIL_RTSEMEVENT;
233 for (VMCPUID i = 0; i < pVM->cCpus; i++)
234 {
235 rc = RTSemEventCreate(&pVM->vmm.s.pahEvtRendezvousEnterOrdered[i]);
236 AssertRCReturn(rc, rc);
237 }
238 rc = RTSemEventCreate(&pVM->vmm.s.hEvtRendezvousEnterOneByOne);
239 AssertRCReturn(rc, rc);
240 rc = RTSemEventMultiCreate(&pVM->vmm.s.hEvtMulRendezvousEnterAllAtOnce);
241 AssertRCReturn(rc, rc);
242 rc = RTSemEventMultiCreate(&pVM->vmm.s.hEvtMulRendezvousDone);
243 AssertRCReturn(rc, rc);
244 rc = RTSemEventCreate(&pVM->vmm.s.hEvtRendezvousDoneCaller);
245 AssertRCReturn(rc, rc);
246 rc = RTSemEventMultiCreate(&pVM->vmm.s.hEvtMulRendezvousRecursionPush);
247 AssertRCReturn(rc, rc);
248 rc = RTSemEventMultiCreate(&pVM->vmm.s.hEvtMulRendezvousRecursionPop);
249 AssertRCReturn(rc, rc);
250 rc = RTSemEventCreate(&pVM->vmm.s.hEvtRendezvousRecursionPushCaller);
251 AssertRCReturn(rc, rc);
252 rc = RTSemEventCreate(&pVM->vmm.s.hEvtRendezvousRecursionPopCaller);
253 AssertRCReturn(rc, rc);
254
255 /*
256 * Register the saved state data unit.
257 */
258 rc = SSMR3RegisterInternal(pVM, "vmm", 1, VMM_SAVED_STATE_VERSION, VMM_STACK_SIZE + sizeof(RTGCPTR),
259 NULL, NULL, NULL,
260 NULL, vmmR3Save, NULL,
261 NULL, vmmR3Load, NULL);
262 if (RT_FAILURE(rc))
263 return rc;
264
265 /*
266 * Register the Ring-0 VM handle with the session for fast ioctl calls.
267 */
268 rc = SUPR3SetVMForFastIOCtl(pVM->pVMR0);
269 if (RT_FAILURE(rc))
270 return rc;
271
272 /*
273 * Init various sub-components.
274 */
275 rc = vmmR3SwitcherInit(pVM);
276 if (RT_SUCCESS(rc))
277 {
278 rc = vmmR3InitStacks(pVM);
279 if (RT_SUCCESS(rc))
280 {
281 rc = vmmR3InitLoggers(pVM);
282
283#ifdef VBOX_WITH_NMI
284 /*
285 * Allocate mapping for the host APIC.
286 */
287 if (RT_SUCCESS(rc))
288 {
289 rc = MMR3HyperReserve(pVM, PAGE_SIZE, "Host APIC", &pVM->vmm.s.GCPtrApicBase);
290 AssertRC(rc);
291 }
292#endif
293 if (RT_SUCCESS(rc))
294 {
295 /*
296 * Debug info and statistics.
297 */
298 DBGFR3InfoRegisterInternal(pVM, "fflags", "Displays the current Forced actions Flags.", vmmR3InfoFF);
299 vmmR3InitRegisterStats(pVM);
300 vmmInitFormatTypes();
301
302 return VINF_SUCCESS;
303 }
304 }
305 /** @todo Need failure cleanup. */
306
307 //more todo in here?
308 //if (RT_SUCCESS(rc))
309 //{
310 //}
311 //int rc2 = vmmR3TermCoreCode(pVM);
312 //AssertRC(rc2));
313 }
314
315 return rc;
316}
317
318
319/**
320 * Allocate & setup the VMM RC stack(s) (for EMTs).
321 *
322 * The stacks are also used for long jumps in Ring-0.
323 *
324 * @returns VBox status code.
325 * @param pVM The cross context VM structure.
326 *
327 * @remarks The optional guard page gets it protection setup up during R3 init
328 * completion because of init order issues.
329 */
330static int vmmR3InitStacks(PVM pVM)
331{
332 int rc = VINF_SUCCESS;
333#ifdef VMM_R0_SWITCH_STACK
334 uint32_t fFlags = MMHYPER_AONR_FLAGS_KERNEL_MAPPING;
335#else
336 uint32_t fFlags = 0;
337#endif
338
339 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
340 {
341 PVMCPU pVCpu = &pVM->aCpus[idCpu];
342
343#ifdef VBOX_STRICT_VMM_STACK
344 rc = MMR3HyperAllocOnceNoRelEx(pVM, PAGE_SIZE + VMM_STACK_SIZE + PAGE_SIZE,
345#else
346 rc = MMR3HyperAllocOnceNoRelEx(pVM, VMM_STACK_SIZE,
347#endif
348 PAGE_SIZE, MM_TAG_VMM, fFlags, (void **)&pVCpu->vmm.s.pbEMTStackR3);
349 if (RT_SUCCESS(rc))
350 {
351#ifdef VBOX_STRICT_VMM_STACK
352 pVCpu->vmm.s.pbEMTStackR3 += PAGE_SIZE;
353#endif
354#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
355 /* MMHyperR3ToR0 returns R3 when not doing hardware assisted virtualization. */
356 if (!HMIsEnabled(pVM))
357 pVCpu->vmm.s.CallRing3JmpBufR0.pvSavedStack = NIL_RTR0PTR;
358 else
359#endif
360 pVCpu->vmm.s.CallRing3JmpBufR0.pvSavedStack = MMHyperR3ToR0(pVM, pVCpu->vmm.s.pbEMTStackR3);
361 pVCpu->vmm.s.pbEMTStackRC = MMHyperR3ToRC(pVM, pVCpu->vmm.s.pbEMTStackR3);
362 pVCpu->vmm.s.pbEMTStackBottomRC = pVCpu->vmm.s.pbEMTStackRC + VMM_STACK_SIZE;
363 AssertRelease(pVCpu->vmm.s.pbEMTStackRC);
364
365 CPUMSetHyperESP(pVCpu, pVCpu->vmm.s.pbEMTStackBottomRC);
366 }
367 }
368
369 return rc;
370}
371
372
373/**
374 * Initialize the loggers.
375 *
376 * @returns VBox status code.
377 * @param pVM The cross context VM structure.
378 */
379static int vmmR3InitLoggers(PVM pVM)
380{
381 int rc;
382#define RTLogCalcSizeForR0(cGroups, fFlags) (RT_OFFSETOF(VMMR0LOGGER, Logger.afGroups[cGroups]) + PAGE_SIZE)
383
384 /*
385 * Allocate RC & R0 Logger instances (they are finalized in the relocator).
386 */
387#ifdef LOG_ENABLED
388 PRTLOGGER pLogger = RTLogDefaultInstance();
389 if (pLogger)
390 {
391 if (!HMIsEnabled(pVM))
392 {
393 pVM->vmm.s.cbRCLogger = RT_OFFSETOF(RTLOGGERRC, afGroups[pLogger->cGroups]);
394 rc = MMR3HyperAllocOnceNoRel(pVM, pVM->vmm.s.cbRCLogger, 0, MM_TAG_VMM, (void **)&pVM->vmm.s.pRCLoggerR3);
395 if (RT_FAILURE(rc))
396 return rc;
397 pVM->vmm.s.pRCLoggerRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pRCLoggerR3);
398 }
399
400# ifdef VBOX_WITH_R0_LOGGING
401 size_t const cbLogger = RTLogCalcSizeForR0(pLogger->cGroups, 0);
402 for (VMCPUID i = 0; i < pVM->cCpus; i++)
403 {
404 PVMCPU pVCpu = &pVM->aCpus[i];
405 rc = MMR3HyperAllocOnceNoRelEx(pVM, cbLogger, PAGE_SIZE, MM_TAG_VMM, MMHYPER_AONR_FLAGS_KERNEL_MAPPING,
406 (void **)&pVCpu->vmm.s.pR0LoggerR3);
407 if (RT_FAILURE(rc))
408 return rc;
409 pVCpu->vmm.s.pR0LoggerR3->pVM = pVM->pVMR0;
410 //pVCpu->vmm.s.pR0LoggerR3->fCreated = false;
411 pVCpu->vmm.s.pR0LoggerR3->cbLogger = (uint32_t)cbLogger;
412 pVCpu->vmm.s.pR0LoggerR0 = MMHyperR3ToR0(pVM, pVCpu->vmm.s.pR0LoggerR3);
413 }
414# endif
415 }
416#endif /* LOG_ENABLED */
417
418#ifdef VBOX_WITH_RC_RELEASE_LOGGING
419 /*
420 * Allocate RC release logger instances (finalized in the relocator).
421 */
422 if (!HMIsEnabled(pVM))
423 {
424 PRTLOGGER pRelLogger = RTLogRelGetDefaultInstance();
425 if (pRelLogger)
426 {
427 pVM->vmm.s.cbRCRelLogger = RT_OFFSETOF(RTLOGGERRC, afGroups[pRelLogger->cGroups]);
428 rc = MMR3HyperAllocOnceNoRel(pVM, pVM->vmm.s.cbRCRelLogger, 0, MM_TAG_VMM, (void **)&pVM->vmm.s.pRCRelLoggerR3);
429 if (RT_FAILURE(rc))
430 return rc;
431 pVM->vmm.s.pRCRelLoggerRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pRCRelLoggerR3);
432 }
433 }
434#endif /* VBOX_WITH_RC_RELEASE_LOGGING */
435 return VINF_SUCCESS;
436}
437
438
439/**
440 * VMMR3Init worker that register the statistics with STAM.
441 *
442 * @param pVM The cross context VM structure.
443 */
444static void vmmR3InitRegisterStats(PVM pVM)
445{
446 RT_NOREF_PV(pVM);
447
448 /*
449 * Statistics.
450 */
451 STAM_REG(pVM, &pVM->vmm.s.StatRunRC, STAMTYPE_COUNTER, "/VMM/RunRC", STAMUNIT_OCCURENCES, "Number of context switches.");
452 STAM_REG(pVM, &pVM->vmm.s.StatRZRetNormal, STAMTYPE_COUNTER, "/VMM/RZRet/Normal", STAMUNIT_OCCURENCES, "Number of VINF_SUCCESS returns.");
453 STAM_REG(pVM, &pVM->vmm.s.StatRZRetInterrupt, STAMTYPE_COUNTER, "/VMM/RZRet/Interrupt", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_INTERRUPT returns.");
454 STAM_REG(pVM, &pVM->vmm.s.StatRZRetInterruptHyper, STAMTYPE_COUNTER, "/VMM/RZRet/InterruptHyper", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_INTERRUPT_HYPER returns.");
455 STAM_REG(pVM, &pVM->vmm.s.StatRZRetGuestTrap, STAMTYPE_COUNTER, "/VMM/RZRet/GuestTrap", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_GUEST_TRAP returns.");
456 STAM_REG(pVM, &pVM->vmm.s.StatRZRetRingSwitch, STAMTYPE_COUNTER, "/VMM/RZRet/RingSwitch", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_RING_SWITCH returns.");
457 STAM_REG(pVM, &pVM->vmm.s.StatRZRetRingSwitchInt, STAMTYPE_COUNTER, "/VMM/RZRet/RingSwitchInt", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_RING_SWITCH_INT returns.");
458 STAM_REG(pVM, &pVM->vmm.s.StatRZRetStaleSelector, STAMTYPE_COUNTER, "/VMM/RZRet/StaleSelector", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_STALE_SELECTOR returns.");
459 STAM_REG(pVM, &pVM->vmm.s.StatRZRetIRETTrap, STAMTYPE_COUNTER, "/VMM/RZRet/IRETTrap", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_IRET_TRAP returns.");
460 STAM_REG(pVM, &pVM->vmm.s.StatRZRetEmulate, STAMTYPE_COUNTER, "/VMM/RZRet/Emulate", STAMUNIT_OCCURENCES, "Number of VINF_EM_EXECUTE_INSTRUCTION returns.");
461 STAM_REG(pVM, &pVM->vmm.s.StatRZRetIOBlockEmulate, STAMTYPE_COUNTER, "/VMM/RZRet/EmulateIOBlock", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_EMULATE_IO_BLOCK returns.");
462 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPatchEmulate, STAMTYPE_COUNTER, "/VMM/RZRet/PatchEmulate", STAMUNIT_OCCURENCES, "Number of VINF_PATCH_EMULATE_INSTR returns.");
463 STAM_REG(pVM, &pVM->vmm.s.StatRZRetIORead, STAMTYPE_COUNTER, "/VMM/RZRet/IORead", STAMUNIT_OCCURENCES, "Number of VINF_IOM_R3_IOPORT_READ returns.");
464 STAM_REG(pVM, &pVM->vmm.s.StatRZRetIOWrite, STAMTYPE_COUNTER, "/VMM/RZRet/IOWrite", STAMUNIT_OCCURENCES, "Number of VINF_IOM_R3_IOPORT_WRITE returns.");
465 STAM_REG(pVM, &pVM->vmm.s.StatRZRetIOCommitWrite, STAMTYPE_COUNTER, "/VMM/RZRet/IOCommitWrite", STAMUNIT_OCCURENCES, "Number of VINF_IOM_R3_IOPORT_COMMIT_WRITE returns.");
466 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMMIORead, STAMTYPE_COUNTER, "/VMM/RZRet/MMIORead", STAMUNIT_OCCURENCES, "Number of VINF_IOM_R3_MMIO_READ returns.");
467 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMMIOWrite, STAMTYPE_COUNTER, "/VMM/RZRet/MMIOWrite", STAMUNIT_OCCURENCES, "Number of VINF_IOM_R3_MMIO_WRITE returns.");
468 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMMIOCommitWrite, STAMTYPE_COUNTER, "/VMM/RZRet/MMIOCommitWrite", STAMUNIT_OCCURENCES, "Number of VINF_IOM_R3_MMIO_COMMIT_WRITE returns.");
469 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMMIOReadWrite, STAMTYPE_COUNTER, "/VMM/RZRet/MMIOReadWrite", STAMUNIT_OCCURENCES, "Number of VINF_IOM_R3_MMIO_READ_WRITE returns.");
470 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMMIOPatchRead, STAMTYPE_COUNTER, "/VMM/RZRet/MMIOPatchRead", STAMUNIT_OCCURENCES, "Number of VINF_IOM_HC_MMIO_PATCH_READ returns.");
471 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMMIOPatchWrite, STAMTYPE_COUNTER, "/VMM/RZRet/MMIOPatchWrite", STAMUNIT_OCCURENCES, "Number of VINF_IOM_HC_MMIO_PATCH_WRITE returns.");
472 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMSRRead, STAMTYPE_COUNTER, "/VMM/RZRet/MSRRead", STAMUNIT_OCCURENCES, "Number of VINF_CPUM_R3_MSR_READ returns.");
473 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMSRWrite, STAMTYPE_COUNTER, "/VMM/RZRet/MSRWrite", STAMUNIT_OCCURENCES, "Number of VINF_CPUM_R3_MSR_WRITE returns.");
474 STAM_REG(pVM, &pVM->vmm.s.StatRZRetLDTFault, STAMTYPE_COUNTER, "/VMM/RZRet/LDTFault", STAMUNIT_OCCURENCES, "Number of VINF_EM_EXECUTE_INSTRUCTION_GDT_FAULT returns.");
475 STAM_REG(pVM, &pVM->vmm.s.StatRZRetGDTFault, STAMTYPE_COUNTER, "/VMM/RZRet/GDTFault", STAMUNIT_OCCURENCES, "Number of VINF_EM_EXECUTE_INSTRUCTION_LDT_FAULT returns.");
476 STAM_REG(pVM, &pVM->vmm.s.StatRZRetIDTFault, STAMTYPE_COUNTER, "/VMM/RZRet/IDTFault", STAMUNIT_OCCURENCES, "Number of VINF_EM_EXECUTE_INSTRUCTION_IDT_FAULT returns.");
477 STAM_REG(pVM, &pVM->vmm.s.StatRZRetTSSFault, STAMTYPE_COUNTER, "/VMM/RZRet/TSSFault", STAMUNIT_OCCURENCES, "Number of VINF_EM_EXECUTE_INSTRUCTION_TSS_FAULT returns.");
478 STAM_REG(pVM, &pVM->vmm.s.StatRZRetCSAMTask, STAMTYPE_COUNTER, "/VMM/RZRet/CSAMTask", STAMUNIT_OCCURENCES, "Number of VINF_CSAM_PENDING_ACTION returns.");
479 STAM_REG(pVM, &pVM->vmm.s.StatRZRetSyncCR3, STAMTYPE_COUNTER, "/VMM/RZRet/SyncCR", STAMUNIT_OCCURENCES, "Number of VINF_PGM_SYNC_CR3 returns.");
480 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMisc, STAMTYPE_COUNTER, "/VMM/RZRet/Misc", STAMUNIT_OCCURENCES, "Number of misc returns.");
481 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPatchInt3, STAMTYPE_COUNTER, "/VMM/RZRet/PatchInt3", STAMUNIT_OCCURENCES, "Number of VINF_PATM_PATCH_INT3 returns.");
482 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPatchPF, STAMTYPE_COUNTER, "/VMM/RZRet/PatchPF", STAMUNIT_OCCURENCES, "Number of VINF_PATM_PATCH_TRAP_PF returns.");
483 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPatchGP, STAMTYPE_COUNTER, "/VMM/RZRet/PatchGP", STAMUNIT_OCCURENCES, "Number of VINF_PATM_PATCH_TRAP_GP returns.");
484 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPatchIretIRQ, STAMTYPE_COUNTER, "/VMM/RZRet/PatchIret", STAMUNIT_OCCURENCES, "Number of VINF_PATM_PENDING_IRQ_AFTER_IRET returns.");
485 STAM_REG(pVM, &pVM->vmm.s.StatRZRetRescheduleREM, STAMTYPE_COUNTER, "/VMM/RZRet/ScheduleREM", STAMUNIT_OCCURENCES, "Number of VINF_EM_RESCHEDULE_REM returns.");
486 STAM_REG(pVM, &pVM->vmm.s.StatRZRetToR3Total, STAMTYPE_COUNTER, "/VMM/RZRet/ToR3", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_TO_R3 returns.");
487 STAM_REG(pVM, &pVM->vmm.s.StatRZRetToR3Unknown, STAMTYPE_COUNTER, "/VMM/RZRet/ToR3/Unknown", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_TO_R3 returns without responsible force flag.");
488 STAM_REG(pVM, &pVM->vmm.s.StatRZRetToR3FF, STAMTYPE_COUNTER, "/VMM/RZRet/ToR3/ToR3", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_TO_R3 returns with VMCPU_FF_TO_R3.");
489 STAM_REG(pVM, &pVM->vmm.s.StatRZRetToR3TMVirt, STAMTYPE_COUNTER, "/VMM/RZRet/ToR3/TMVirt", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_TO_R3 returns with VM_FF_TM_VIRTUAL_SYNC.");
490 STAM_REG(pVM, &pVM->vmm.s.StatRZRetToR3HandyPages, STAMTYPE_COUNTER, "/VMM/RZRet/ToR3/Handy", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_TO_R3 returns with VM_FF_PGM_NEED_HANDY_PAGES.");
491 STAM_REG(pVM, &pVM->vmm.s.StatRZRetToR3PDMQueues, STAMTYPE_COUNTER, "/VMM/RZRet/ToR3/PDMQueue", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_TO_R3 returns with VM_FF_PDM_QUEUES.");
492 STAM_REG(pVM, &pVM->vmm.s.StatRZRetToR3Rendezvous, STAMTYPE_COUNTER, "/VMM/RZRet/ToR3/Rendezvous", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_TO_R3 returns with VM_FF_EMT_RENDEZVOUS.");
493 STAM_REG(pVM, &pVM->vmm.s.StatRZRetToR3Timer, STAMTYPE_COUNTER, "/VMM/RZRet/ToR3/Timer", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_TO_R3 returns with VMCPU_FF_TIMER.");
494 STAM_REG(pVM, &pVM->vmm.s.StatRZRetToR3DMA, STAMTYPE_COUNTER, "/VMM/RZRet/ToR3/DMA", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_TO_R3 returns with VM_FF_PDM_DMA.");
495 STAM_REG(pVM, &pVM->vmm.s.StatRZRetToR3CritSect, STAMTYPE_COUNTER, "/VMM/RZRet/ToR3/CritSect", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_TO_R3 returns with VMCPU_FF_PDM_CRITSECT.");
496 STAM_REG(pVM, &pVM->vmm.s.StatRZRetToR3Iem, STAMTYPE_COUNTER, "/VMM/RZRet/ToR3/IEM", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_TO_R3 returns with VMCPU_FF_IEM.");
497 STAM_REG(pVM, &pVM->vmm.s.StatRZRetToR3Iom, STAMTYPE_COUNTER, "/VMM/RZRet/ToR3/IOM", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_TO_R3 returns with VMCPU_FF_IOM.");
498 STAM_REG(pVM, &pVM->vmm.s.StatRZRetTimerPending, STAMTYPE_COUNTER, "/VMM/RZRet/TimerPending", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_TIMER_PENDING returns.");
499 STAM_REG(pVM, &pVM->vmm.s.StatRZRetInterruptPending, STAMTYPE_COUNTER, "/VMM/RZRet/InterruptPending", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_INTERRUPT_PENDING returns.");
500 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPATMDuplicateFn, STAMTYPE_COUNTER, "/VMM/RZRet/PATMDuplicateFn", STAMUNIT_OCCURENCES, "Number of VINF_PATM_DUPLICATE_FUNCTION returns.");
501 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPGMChangeMode, STAMTYPE_COUNTER, "/VMM/RZRet/PGMChangeMode", STAMUNIT_OCCURENCES, "Number of VINF_PGM_CHANGE_MODE returns.");
502 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPGMFlushPending, STAMTYPE_COUNTER, "/VMM/RZRet/PGMFlushPending", STAMUNIT_OCCURENCES, "Number of VINF_PGM_POOL_FLUSH_PENDING returns.");
503 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPendingRequest, STAMTYPE_COUNTER, "/VMM/RZRet/PendingRequest", STAMUNIT_OCCURENCES, "Number of VINF_EM_PENDING_REQUEST returns.");
504 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPatchTPR, STAMTYPE_COUNTER, "/VMM/RZRet/PatchTPR", STAMUNIT_OCCURENCES, "Number of VINF_EM_HM_PATCH_TPR_INSTR returns.");
505 STAM_REG(pVM, &pVM->vmm.s.StatRZRetCallRing3, STAMTYPE_COUNTER, "/VMM/RZCallR3/Misc", STAMUNIT_OCCURENCES, "Number of Other ring-3 calls.");
506 STAM_REG(pVM, &pVM->vmm.s.StatRZCallPDMLock, STAMTYPE_COUNTER, "/VMM/RZCallR3/PDMLock", STAMUNIT_OCCURENCES, "Number of VMMCALLRING3_PDM_LOCK calls.");
507 STAM_REG(pVM, &pVM->vmm.s.StatRZCallPDMCritSectEnter, STAMTYPE_COUNTER, "/VMM/RZCallR3/PDMCritSectEnter", STAMUNIT_OCCURENCES, "Number of VMMCALLRING3_PDM_CRITSECT_ENTER calls.");
508 STAM_REG(pVM, &pVM->vmm.s.StatRZCallPGMLock, STAMTYPE_COUNTER, "/VMM/RZCallR3/PGMLock", STAMUNIT_OCCURENCES, "Number of VMMCALLRING3_PGM_LOCK calls.");
509 STAM_REG(pVM, &pVM->vmm.s.StatRZCallPGMPoolGrow, STAMTYPE_COUNTER, "/VMM/RZCallR3/PGMPoolGrow", STAMUNIT_OCCURENCES, "Number of VMMCALLRING3_PGM_POOL_GROW calls.");
510 STAM_REG(pVM, &pVM->vmm.s.StatRZCallPGMMapChunk, STAMTYPE_COUNTER, "/VMM/RZCallR3/PGMMapChunk", STAMUNIT_OCCURENCES, "Number of VMMCALLRING3_PGM_MAP_CHUNK calls.");
511 STAM_REG(pVM, &pVM->vmm.s.StatRZCallPGMAllocHandy, STAMTYPE_COUNTER, "/VMM/RZCallR3/PGMAllocHandy", STAMUNIT_OCCURENCES, "Number of VMMCALLRING3_PGM_ALLOCATE_HANDY_PAGES calls.");
512 STAM_REG(pVM, &pVM->vmm.s.StatRZCallRemReplay, STAMTYPE_COUNTER, "/VMM/RZCallR3/REMReplay", STAMUNIT_OCCURENCES, "Number of VMMCALLRING3_REM_REPLAY_HANDLER_NOTIFICATIONS calls.");
513 STAM_REG(pVM, &pVM->vmm.s.StatRZCallLogFlush, STAMTYPE_COUNTER, "/VMM/RZCallR3/VMMLogFlush", STAMUNIT_OCCURENCES, "Number of VMMCALLRING3_VMM_LOGGER_FLUSH calls.");
514 STAM_REG(pVM, &pVM->vmm.s.StatRZCallVMSetError, STAMTYPE_COUNTER, "/VMM/RZCallR3/VMSetError", STAMUNIT_OCCURENCES, "Number of VMMCALLRING3_VM_SET_ERROR calls.");
515 STAM_REG(pVM, &pVM->vmm.s.StatRZCallVMSetRuntimeError, STAMTYPE_COUNTER, "/VMM/RZCallR3/VMRuntimeError", STAMUNIT_OCCURENCES, "Number of VMMCALLRING3_VM_SET_RUNTIME_ERROR calls.");
516
517#ifdef VBOX_WITH_STATISTICS
518 for (VMCPUID i = 0; i < pVM->cCpus; i++)
519 {
520 STAMR3RegisterF(pVM, &pVM->aCpus[i].vmm.s.CallRing3JmpBufR0.cbUsedMax, STAMTYPE_U32_RESET, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Max amount of stack used.", "/VMM/Stack/CPU%u/Max", i);
521 STAMR3RegisterF(pVM, &pVM->aCpus[i].vmm.s.CallRing3JmpBufR0.cbUsedAvg, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Average stack usage.", "/VMM/Stack/CPU%u/Avg", i);
522 STAMR3RegisterF(pVM, &pVM->aCpus[i].vmm.s.CallRing3JmpBufR0.cUsedTotal, STAMTYPE_U64, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Number of stack usages.", "/VMM/Stack/CPU%u/Uses", i);
523 }
524#endif
525}
526
527
528/**
529 * Initializes the R0 VMM.
530 *
531 * @returns VBox status code.
532 * @param pVM The cross context VM structure.
533 */
534VMMR3_INT_DECL(int) VMMR3InitR0(PVM pVM)
535{
536 int rc;
537 PVMCPU pVCpu = VMMGetCpu(pVM);
538 Assert(pVCpu && pVCpu->idCpu == 0);
539
540#ifdef LOG_ENABLED
541 /*
542 * Initialize the ring-0 logger if we haven't done so yet.
543 */
544 if ( pVCpu->vmm.s.pR0LoggerR3
545 && !pVCpu->vmm.s.pR0LoggerR3->fCreated)
546 {
547 rc = VMMR3UpdateLoggers(pVM);
548 if (RT_FAILURE(rc))
549 return rc;
550 }
551#endif
552
553 /*
554 * Call Ring-0 entry with init code.
555 */
556 for (;;)
557 {
558#ifdef NO_SUPCALLR0VMM
559 //rc = VERR_GENERAL_FAILURE;
560 rc = VINF_SUCCESS;
561#else
562 rc = SUPR3CallVMMR0Ex(pVM->pVMR0, 0 /*idCpu*/, VMMR0_DO_VMMR0_INIT,
563 RT_MAKE_U64(VMMGetSvnRev(), vmmGetBuildType()), NULL);
564#endif
565 /*
566 * Flush the logs.
567 */
568#ifdef LOG_ENABLED
569 if ( pVCpu->vmm.s.pR0LoggerR3
570 && pVCpu->vmm.s.pR0LoggerR3->Logger.offScratch > 0)
571 RTLogFlushR0(NULL, &pVCpu->vmm.s.pR0LoggerR3->Logger);
572#endif
573 if (rc != VINF_VMM_CALL_HOST)
574 break;
575 rc = vmmR3ServiceCallRing3Request(pVM, pVCpu);
576 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
577 break;
578 /* Resume R0 */
579 }
580
581 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
582 {
583 LogRel(("VMM: R0 init failed, rc=%Rra\n", rc));
584 if (RT_SUCCESS(rc))
585 rc = VERR_IPE_UNEXPECTED_INFO_STATUS;
586 }
587
588 /* Log whether thread-context hooks are used (on Linux this can depend on how the kernel is configured). */
589 if (pVM->aCpus[0].vmm.s.hCtxHook != NIL_RTTHREADCTXHOOK)
590 LogRel(("VMM: Enabled thread-context hooks\n"));
591 else
592 LogRel(("VMM: Thread-context hooks unavailable\n"));
593
594 return rc;
595}
596
597
598#ifdef VBOX_WITH_RAW_MODE
599/**
600 * Initializes the RC VMM.
601 *
602 * @returns VBox status code.
603 * @param pVM The cross context VM structure.
604 */
605VMMR3_INT_DECL(int) VMMR3InitRC(PVM pVM)
606{
607 PVMCPU pVCpu = VMMGetCpu(pVM);
608 Assert(pVCpu && pVCpu->idCpu == 0);
609
610 /* In VMX mode, there's no need to init RC. */
611 if (HMIsEnabled(pVM))
612 return VINF_SUCCESS;
613
614 AssertReturn(pVM->cCpus == 1, VERR_RAW_MODE_INVALID_SMP);
615
616 /*
617 * Call VMMRCInit():
618 * -# resolve the address.
619 * -# setup stackframe and EIP to use the trampoline.
620 * -# do a generic hypervisor call.
621 */
622 RTRCPTR RCPtrEP;
623 int rc = PDMR3LdrGetSymbolRC(pVM, VMMRC_MAIN_MODULE_NAME, "VMMRCEntry", &RCPtrEP);
624 if (RT_SUCCESS(rc))
625 {
626 CPUMSetHyperESP(pVCpu, pVCpu->vmm.s.pbEMTStackBottomRC); /* Clear the stack. */
627 uint64_t u64TS = RTTimeProgramStartNanoTS();
628 CPUMPushHyper(pVCpu, RT_HI_U32(u64TS)); /* Param 4: The program startup TS - Hi. */
629 CPUMPushHyper(pVCpu, RT_LO_U32(u64TS)); /* Param 4: The program startup TS - Lo. */
630 CPUMPushHyper(pVCpu, vmmGetBuildType()); /* Param 3: Version argument. */
631 CPUMPushHyper(pVCpu, VMMGetSvnRev()); /* Param 2: Version argument. */
632 CPUMPushHyper(pVCpu, VMMRC_DO_VMMRC_INIT); /* Param 1: Operation. */
633 CPUMPushHyper(pVCpu, pVM->pVMRC); /* Param 0: pVM */
634 CPUMPushHyper(pVCpu, 6 * sizeof(RTRCPTR)); /* trampoline param: stacksize. */
635 CPUMPushHyper(pVCpu, RCPtrEP); /* Call EIP. */
636 CPUMSetHyperEIP(pVCpu, pVM->vmm.s.pfnCallTrampolineRC);
637 Assert(CPUMGetHyperCR3(pVCpu) && CPUMGetHyperCR3(pVCpu) == PGMGetHyperCR3(pVCpu));
638
639 for (;;)
640 {
641#ifdef NO_SUPCALLR0VMM
642 //rc = VERR_GENERAL_FAILURE;
643 rc = VINF_SUCCESS;
644#else
645 rc = SUPR3CallVMMR0(pVM->pVMR0, 0 /* VCPU 0 */, VMMR0_DO_CALL_HYPERVISOR, NULL);
646#endif
647#ifdef LOG_ENABLED
648 PRTLOGGERRC pLogger = pVM->vmm.s.pRCLoggerR3;
649 if ( pLogger
650 && pLogger->offScratch > 0)
651 RTLogFlushRC(NULL, pLogger);
652#endif
653#ifdef VBOX_WITH_RC_RELEASE_LOGGING
654 PRTLOGGERRC pRelLogger = pVM->vmm.s.pRCRelLoggerR3;
655 if (RT_UNLIKELY(pRelLogger && pRelLogger->offScratch > 0))
656 RTLogFlushRC(RTLogRelGetDefaultInstance(), pRelLogger);
657#endif
658 if (rc != VINF_VMM_CALL_HOST)
659 break;
660 rc = vmmR3ServiceCallRing3Request(pVM, pVCpu);
661 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
662 break;
663 }
664
665 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
666 {
667 VMMR3FatalDump(pVM, pVCpu, rc);
668 if (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST)
669 rc = VERR_IPE_UNEXPECTED_INFO_STATUS;
670 }
671 AssertRC(rc);
672 }
673 return rc;
674}
675#endif /* VBOX_WITH_RAW_MODE */
676
677
678/**
679 * Called when an init phase completes.
680 *
681 * @returns VBox status code.
682 * @param pVM The cross context VM structure.
683 * @param enmWhat Which init phase.
684 */
685VMMR3_INT_DECL(int) VMMR3InitCompleted(PVM pVM, VMINITCOMPLETED enmWhat)
686{
687 int rc = VINF_SUCCESS;
688
689 switch (enmWhat)
690 {
691 case VMINITCOMPLETED_RING3:
692 {
693 /*
694 * Set page attributes to r/w for stack pages.
695 */
696 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
697 {
698 rc = PGMMapSetPage(pVM, pVM->aCpus[idCpu].vmm.s.pbEMTStackRC, VMM_STACK_SIZE,
699 X86_PTE_P | X86_PTE_A | X86_PTE_D | X86_PTE_RW);
700 AssertRCReturn(rc, rc);
701 }
702
703 /*
704 * Create the EMT yield timer.
705 */
706 rc = TMR3TimerCreateInternal(pVM, TMCLOCK_REAL, vmmR3YieldEMT, NULL, "EMT Yielder", &pVM->vmm.s.pYieldTimer);
707 AssertRCReturn(rc, rc);
708
709 rc = TMTimerSetMillies(pVM->vmm.s.pYieldTimer, pVM->vmm.s.cYieldEveryMillies);
710 AssertRCReturn(rc, rc);
711
712#ifdef VBOX_WITH_NMI
713 /*
714 * Map the host APIC into GC - This is AMD/Intel + Host OS specific!
715 */
716 rc = PGMMap(pVM, pVM->vmm.s.GCPtrApicBase, 0xfee00000, PAGE_SIZE,
717 X86_PTE_P | X86_PTE_RW | X86_PTE_PWT | X86_PTE_PCD | X86_PTE_A | X86_PTE_D);
718 AssertRCReturn(rc, rc);
719#endif
720
721#ifdef VBOX_STRICT_VMM_STACK
722 /*
723 * Setup the stack guard pages: Two inaccessible pages at each sides of the
724 * stack to catch over/under-flows.
725 */
726 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
727 {
728 uint8_t *pbEMTStackR3 = pVM->aCpus[idCpu].vmm.s.pbEMTStackR3;
729
730 memset(pbEMTStackR3 - PAGE_SIZE, 0xcc, PAGE_SIZE);
731 MMR3HyperSetGuard(pVM, pbEMTStackR3 - PAGE_SIZE, PAGE_SIZE, true /*fSet*/);
732
733 memset(pbEMTStackR3 + VMM_STACK_SIZE, 0xcc, PAGE_SIZE);
734 MMR3HyperSetGuard(pVM, pbEMTStackR3 + VMM_STACK_SIZE, PAGE_SIZE, true /*fSet*/);
735 }
736 pVM->vmm.s.fStackGuardsStationed = true;
737#endif
738 break;
739 }
740
741 case VMINITCOMPLETED_HM:
742 {
743 /*
744 * Disable the periodic preemption timers if we can use the
745 * VMX-preemption timer instead.
746 */
747 if ( pVM->vmm.s.fUsePeriodicPreemptionTimers
748 && HMR3IsVmxPreemptionTimerUsed(pVM))
749 pVM->vmm.s.fUsePeriodicPreemptionTimers = false;
750 LogRel(("VMM: fUsePeriodicPreemptionTimers=%RTbool\n", pVM->vmm.s.fUsePeriodicPreemptionTimers));
751
752 /*
753 * Last chance for GIM to update its CPUID leaves if it requires
754 * knowledge/information from HM initialization.
755 */
756 rc = GIMR3InitCompleted(pVM);
757 AssertRCReturn(rc, rc);
758
759 /*
760 * CPUM's post-initialization (print CPUIDs).
761 */
762 CPUMR3LogCpuIds(pVM);
763 break;
764 }
765
766 default: /* shuts up gcc */
767 break;
768 }
769
770 return rc;
771}
772
773
774/**
775 * Terminate the VMM bits.
776 *
777 * @returns VBox status code.
778 * @param pVM The cross context VM structure.
779 */
780VMMR3_INT_DECL(int) VMMR3Term(PVM pVM)
781{
782 PVMCPU pVCpu = VMMGetCpu(pVM);
783 Assert(pVCpu && pVCpu->idCpu == 0);
784
785 /*
786 * Call Ring-0 entry with termination code.
787 */
788 int rc;
789 for (;;)
790 {
791#ifdef NO_SUPCALLR0VMM
792 //rc = VERR_GENERAL_FAILURE;
793 rc = VINF_SUCCESS;
794#else
795 rc = SUPR3CallVMMR0Ex(pVM->pVMR0, 0 /*idCpu*/, VMMR0_DO_VMMR0_TERM, 0, NULL);
796#endif
797 /*
798 * Flush the logs.
799 */
800#ifdef LOG_ENABLED
801 if ( pVCpu->vmm.s.pR0LoggerR3
802 && pVCpu->vmm.s.pR0LoggerR3->Logger.offScratch > 0)
803 RTLogFlushR0(NULL, &pVCpu->vmm.s.pR0LoggerR3->Logger);
804#endif
805 if (rc != VINF_VMM_CALL_HOST)
806 break;
807 rc = vmmR3ServiceCallRing3Request(pVM, pVCpu);
808 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
809 break;
810 /* Resume R0 */
811 }
812 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
813 {
814 LogRel(("VMM: VMMR3Term: R0 term failed, rc=%Rra. (warning)\n", rc));
815 if (RT_SUCCESS(rc))
816 rc = VERR_IPE_UNEXPECTED_INFO_STATUS;
817 }
818
819 for (VMCPUID i = 0; i < pVM->cCpus; i++)
820 {
821 RTSemEventDestroy(pVM->vmm.s.pahEvtRendezvousEnterOrdered[i]);
822 pVM->vmm.s.pahEvtRendezvousEnterOrdered[i] = NIL_RTSEMEVENT;
823 }
824 RTSemEventDestroy(pVM->vmm.s.hEvtRendezvousEnterOneByOne);
825 pVM->vmm.s.hEvtRendezvousEnterOneByOne = NIL_RTSEMEVENT;
826 RTSemEventMultiDestroy(pVM->vmm.s.hEvtMulRendezvousEnterAllAtOnce);
827 pVM->vmm.s.hEvtMulRendezvousEnterAllAtOnce = NIL_RTSEMEVENTMULTI;
828 RTSemEventMultiDestroy(pVM->vmm.s.hEvtMulRendezvousDone);
829 pVM->vmm.s.hEvtMulRendezvousDone = NIL_RTSEMEVENTMULTI;
830 RTSemEventDestroy(pVM->vmm.s.hEvtRendezvousDoneCaller);
831 pVM->vmm.s.hEvtRendezvousDoneCaller = NIL_RTSEMEVENT;
832 RTSemEventMultiDestroy(pVM->vmm.s.hEvtMulRendezvousRecursionPush);
833 pVM->vmm.s.hEvtMulRendezvousRecursionPush = NIL_RTSEMEVENTMULTI;
834 RTSemEventMultiDestroy(pVM->vmm.s.hEvtMulRendezvousRecursionPop);
835 pVM->vmm.s.hEvtMulRendezvousRecursionPop = NIL_RTSEMEVENTMULTI;
836 RTSemEventDestroy(pVM->vmm.s.hEvtRendezvousRecursionPushCaller);
837 pVM->vmm.s.hEvtRendezvousRecursionPushCaller = NIL_RTSEMEVENT;
838 RTSemEventDestroy(pVM->vmm.s.hEvtRendezvousRecursionPopCaller);
839 pVM->vmm.s.hEvtRendezvousRecursionPopCaller = NIL_RTSEMEVENT;
840
841#ifdef VBOX_STRICT_VMM_STACK
842 /*
843 * Make the two stack guard pages present again.
844 */
845 if (pVM->vmm.s.fStackGuardsStationed)
846 {
847 for (VMCPUID i = 0; i < pVM->cCpus; i++)
848 {
849 uint8_t *pbEMTStackR3 = pVM->aCpus[i].vmm.s.pbEMTStackR3;
850 MMR3HyperSetGuard(pVM, pbEMTStackR3 - PAGE_SIZE, PAGE_SIZE, false /*fSet*/);
851 MMR3HyperSetGuard(pVM, pbEMTStackR3 + VMM_STACK_SIZE, PAGE_SIZE, false /*fSet*/);
852 }
853 pVM->vmm.s.fStackGuardsStationed = false;
854 }
855#endif
856
857 vmmTermFormatTypes();
858 return rc;
859}
860
861
862/**
863 * Applies relocations to data and code managed by this
864 * component. This function will be called at init and
865 * whenever the VMM need to relocate it self inside the GC.
866 *
867 * The VMM will need to apply relocations to the core code.
868 *
869 * @param pVM The cross context VM structure.
870 * @param offDelta The relocation delta.
871 */
872VMMR3_INT_DECL(void) VMMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
873{
874 LogFlow(("VMMR3Relocate: offDelta=%RGv\n", offDelta));
875
876 /*
877 * Recalc the RC address.
878 */
879#ifdef VBOX_WITH_RAW_MODE
880 pVM->vmm.s.pvCoreCodeRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pvCoreCodeR3);
881#endif
882
883 /*
884 * The stack.
885 */
886 for (VMCPUID i = 0; i < pVM->cCpus; i++)
887 {
888 PVMCPU pVCpu = &pVM->aCpus[i];
889
890 CPUMSetHyperESP(pVCpu, CPUMGetHyperESP(pVCpu) + offDelta);
891
892 pVCpu->vmm.s.pbEMTStackRC = MMHyperR3ToRC(pVM, pVCpu->vmm.s.pbEMTStackR3);
893 pVCpu->vmm.s.pbEMTStackBottomRC = pVCpu->vmm.s.pbEMTStackRC + VMM_STACK_SIZE;
894 }
895
896 /*
897 * All the switchers.
898 */
899 vmmR3SwitcherRelocate(pVM, offDelta);
900
901 /*
902 * Get other RC entry points.
903 */
904 if (!HMIsEnabled(pVM))
905 {
906 int rc = PDMR3LdrGetSymbolRC(pVM, VMMRC_MAIN_MODULE_NAME, "CPUMGCResumeGuest", &pVM->vmm.s.pfnCPUMRCResumeGuest);
907 AssertReleaseMsgRC(rc, ("CPUMGCResumeGuest not found! rc=%Rra\n", rc));
908
909 rc = PDMR3LdrGetSymbolRC(pVM, VMMRC_MAIN_MODULE_NAME, "CPUMGCResumeGuestV86", &pVM->vmm.s.pfnCPUMRCResumeGuestV86);
910 AssertReleaseMsgRC(rc, ("CPUMGCResumeGuestV86 not found! rc=%Rra\n", rc));
911 }
912
913 /*
914 * Update the logger.
915 */
916 VMMR3UpdateLoggers(pVM);
917}
918
919
920/**
921 * Updates the settings for the RC and R0 loggers.
922 *
923 * @returns VBox status code.
924 * @param pVM The cross context VM structure.
925 */
926VMMR3_INT_DECL(int) VMMR3UpdateLoggers(PVM pVM)
927{
928 /*
929 * Simply clone the logger instance (for RC).
930 */
931 int rc = VINF_SUCCESS;
932 RTRCPTR RCPtrLoggerFlush = 0;
933
934 if ( pVM->vmm.s.pRCLoggerR3
935#ifdef VBOX_WITH_RC_RELEASE_LOGGING
936 || pVM->vmm.s.pRCRelLoggerR3
937#endif
938 )
939 {
940 Assert(!HMIsEnabled(pVM));
941 rc = PDMR3LdrGetSymbolRC(pVM, VMMRC_MAIN_MODULE_NAME, "vmmGCLoggerFlush", &RCPtrLoggerFlush);
942 AssertReleaseMsgRC(rc, ("vmmGCLoggerFlush not found! rc=%Rra\n", rc));
943 }
944
945 if (pVM->vmm.s.pRCLoggerR3)
946 {
947 Assert(!HMIsEnabled(pVM));
948 RTRCPTR RCPtrLoggerWrapper = 0;
949 rc = PDMR3LdrGetSymbolRC(pVM, VMMRC_MAIN_MODULE_NAME, "vmmGCLoggerWrapper", &RCPtrLoggerWrapper);
950 AssertReleaseMsgRC(rc, ("vmmGCLoggerWrapper not found! rc=%Rra\n", rc));
951
952 pVM->vmm.s.pRCLoggerRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pRCLoggerR3);
953 rc = RTLogCloneRC(NULL /* default */, pVM->vmm.s.pRCLoggerR3, pVM->vmm.s.cbRCLogger,
954 RCPtrLoggerWrapper, RCPtrLoggerFlush, RTLOGFLAGS_BUFFERED);
955 AssertReleaseMsgRC(rc, ("RTLogCloneRC failed! rc=%Rra\n", rc));
956 }
957
958#ifdef VBOX_WITH_RC_RELEASE_LOGGING
959 if (pVM->vmm.s.pRCRelLoggerR3)
960 {
961 Assert(!HMIsEnabled(pVM));
962 RTRCPTR RCPtrLoggerWrapper = 0;
963 rc = PDMR3LdrGetSymbolRC(pVM, VMMRC_MAIN_MODULE_NAME, "vmmGCRelLoggerWrapper", &RCPtrLoggerWrapper);
964 AssertReleaseMsgRC(rc, ("vmmGCRelLoggerWrapper not found! rc=%Rra\n", rc));
965
966 pVM->vmm.s.pRCRelLoggerRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pRCRelLoggerR3);
967 rc = RTLogCloneRC(RTLogRelGetDefaultInstance(), pVM->vmm.s.pRCRelLoggerR3, pVM->vmm.s.cbRCRelLogger,
968 RCPtrLoggerWrapper, RCPtrLoggerFlush, RTLOGFLAGS_BUFFERED);
969 AssertReleaseMsgRC(rc, ("RTLogCloneRC failed! rc=%Rra\n", rc));
970 }
971#endif /* VBOX_WITH_RC_RELEASE_LOGGING */
972
973#ifdef LOG_ENABLED
974 /*
975 * For the ring-0 EMT logger, we use a per-thread logger instance
976 * in ring-0. Only initialize it once.
977 */
978 PRTLOGGER const pDefault = RTLogDefaultInstance();
979 for (VMCPUID i = 0; i < pVM->cCpus; i++)
980 {
981 PVMCPU pVCpu = &pVM->aCpus[i];
982 PVMMR0LOGGER pR0LoggerR3 = pVCpu->vmm.s.pR0LoggerR3;
983 if (pR0LoggerR3)
984 {
985 if (!pR0LoggerR3->fCreated)
986 {
987 RTR0PTR pfnLoggerWrapper = NIL_RTR0PTR;
988 rc = PDMR3LdrGetSymbolR0(pVM, VMMR0_MAIN_MODULE_NAME, "vmmR0LoggerWrapper", &pfnLoggerWrapper);
989 AssertReleaseMsgRCReturn(rc, ("vmmR0LoggerWrapper not found! rc=%Rra\n", rc), rc);
990
991 RTR0PTR pfnLoggerFlush = NIL_RTR0PTR;
992 rc = PDMR3LdrGetSymbolR0(pVM, VMMR0_MAIN_MODULE_NAME, "vmmR0LoggerFlush", &pfnLoggerFlush);
993 AssertReleaseMsgRCReturn(rc, ("vmmR0LoggerFlush not found! rc=%Rra\n", rc), rc);
994
995 rc = RTLogCreateForR0(&pR0LoggerR3->Logger, pR0LoggerR3->cbLogger,
996 pVCpu->vmm.s.pR0LoggerR0 + RT_OFFSETOF(VMMR0LOGGER, Logger),
997 pfnLoggerWrapper, pfnLoggerFlush,
998 RTLOGFLAGS_BUFFERED, RTLOGDEST_DUMMY);
999 AssertReleaseMsgRCReturn(rc, ("RTLogCreateForR0 failed! rc=%Rra\n", rc), rc);
1000
1001 RTR0PTR pfnLoggerPrefix = NIL_RTR0PTR;
1002 rc = PDMR3LdrGetSymbolR0(pVM, VMMR0_MAIN_MODULE_NAME, "vmmR0LoggerPrefix", &pfnLoggerPrefix);
1003 AssertReleaseMsgRCReturn(rc, ("vmmR0LoggerPrefix not found! rc=%Rra\n", rc), rc);
1004 rc = RTLogSetCustomPrefixCallbackForR0(&pR0LoggerR3->Logger,
1005 pVCpu->vmm.s.pR0LoggerR0 + RT_OFFSETOF(VMMR0LOGGER, Logger),
1006 pfnLoggerPrefix, NIL_RTR0PTR);
1007 AssertReleaseMsgRCReturn(rc, ("RTLogSetCustomPrefixCallback failed! rc=%Rra\n", rc), rc);
1008
1009 pR0LoggerR3->idCpu = i;
1010 pR0LoggerR3->fCreated = true;
1011 pR0LoggerR3->fFlushingDisabled = false;
1012
1013 }
1014
1015 rc = RTLogCopyGroupsAndFlagsForR0(&pR0LoggerR3->Logger, pVCpu->vmm.s.pR0LoggerR0 + RT_OFFSETOF(VMMR0LOGGER, Logger),
1016 pDefault, RTLOGFLAGS_BUFFERED, UINT32_MAX);
1017 AssertRC(rc);
1018 }
1019 }
1020#endif
1021 return rc;
1022}
1023
1024
1025/**
1026 * Gets the pointer to a buffer containing the R0/RC RTAssertMsg1Weak output.
1027 *
1028 * @returns Pointer to the buffer.
1029 * @param pVM The cross context VM structure.
1030 */
1031VMMR3DECL(const char *) VMMR3GetRZAssertMsg1(PVM pVM)
1032{
1033 if (HMIsEnabled(pVM))
1034 return pVM->vmm.s.szRing0AssertMsg1;
1035
1036 RTRCPTR RCPtr;
1037 int rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_szRTAssertMsg1", &RCPtr);
1038 if (RT_SUCCESS(rc))
1039 return (const char *)MMHyperRCToR3(pVM, RCPtr);
1040
1041 return NULL;
1042}
1043
1044
1045/**
1046 * Returns the VMCPU of the specified virtual CPU.
1047 *
1048 * @returns The VMCPU pointer. NULL if @a idCpu or @a pUVM is invalid.
1049 *
1050 * @param pUVM The user mode VM handle.
1051 * @param idCpu The ID of the virtual CPU.
1052 */
1053VMMR3DECL(PVMCPU) VMMR3GetCpuByIdU(PUVM pUVM, RTCPUID idCpu)
1054{
1055 UVM_ASSERT_VALID_EXT_RETURN(pUVM, NULL);
1056 AssertReturn(idCpu < pUVM->cCpus, NULL);
1057 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, NULL);
1058 return &pUVM->pVM->aCpus[idCpu];
1059}
1060
1061
1062/**
1063 * Gets the pointer to a buffer containing the R0/RC RTAssertMsg2Weak output.
1064 *
1065 * @returns Pointer to the buffer.
1066 * @param pVM The cross context VM structure.
1067 */
1068VMMR3DECL(const char *) VMMR3GetRZAssertMsg2(PVM pVM)
1069{
1070 if (HMIsEnabled(pVM))
1071 return pVM->vmm.s.szRing0AssertMsg2;
1072
1073 RTRCPTR RCPtr;
1074 int rc = PDMR3LdrGetSymbolRC(pVM, NULL, "g_szRTAssertMsg2", &RCPtr);
1075 if (RT_SUCCESS(rc))
1076 return (const char *)MMHyperRCToR3(pVM, RCPtr);
1077
1078 return NULL;
1079}
1080
1081
1082/**
1083 * Execute state save operation.
1084 *
1085 * @returns VBox status code.
1086 * @param pVM The cross context VM structure.
1087 * @param pSSM SSM operation handle.
1088 */
1089static DECLCALLBACK(int) vmmR3Save(PVM pVM, PSSMHANDLE pSSM)
1090{
1091 LogFlow(("vmmR3Save:\n"));
1092
1093 /*
1094 * Save the started/stopped state of all CPUs except 0 as it will always
1095 * be running. This avoids breaking the saved state version. :-)
1096 */
1097 for (VMCPUID i = 1; i < pVM->cCpus; i++)
1098 SSMR3PutBool(pSSM, VMCPUSTATE_IS_STARTED(VMCPU_GET_STATE(&pVM->aCpus[i])));
1099
1100 return SSMR3PutU32(pSSM, UINT32_MAX); /* terminator */
1101}
1102
1103
1104/**
1105 * Execute state load operation.
1106 *
1107 * @returns VBox status code.
1108 * @param pVM The cross context VM structure.
1109 * @param pSSM SSM operation handle.
1110 * @param uVersion Data layout version.
1111 * @param uPass The data pass.
1112 */
1113static DECLCALLBACK(int) vmmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
1114{
1115 LogFlow(("vmmR3Load:\n"));
1116 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
1117
1118 /*
1119 * Validate version.
1120 */
1121 if ( uVersion != VMM_SAVED_STATE_VERSION
1122 && uVersion != VMM_SAVED_STATE_VERSION_3_0)
1123 {
1124 AssertMsgFailed(("vmmR3Load: Invalid version uVersion=%u!\n", uVersion));
1125 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1126 }
1127
1128 if (uVersion <= VMM_SAVED_STATE_VERSION_3_0)
1129 {
1130 /* Ignore the stack bottom, stack pointer and stack bits. */
1131 RTRCPTR RCPtrIgnored;
1132 SSMR3GetRCPtr(pSSM, &RCPtrIgnored);
1133 SSMR3GetRCPtr(pSSM, &RCPtrIgnored);
1134#ifdef RT_OS_DARWIN
1135 if ( SSMR3HandleVersion(pSSM) >= VBOX_FULL_VERSION_MAKE(3,0,0)
1136 && SSMR3HandleVersion(pSSM) < VBOX_FULL_VERSION_MAKE(3,1,0)
1137 && SSMR3HandleRevision(pSSM) >= 48858
1138 && ( !strcmp(SSMR3HandleHostOSAndArch(pSSM), "darwin.x86")
1139 || !strcmp(SSMR3HandleHostOSAndArch(pSSM), "") )
1140 )
1141 SSMR3Skip(pSSM, 16384);
1142 else
1143 SSMR3Skip(pSSM, 8192);
1144#else
1145 SSMR3Skip(pSSM, 8192);
1146#endif
1147 }
1148
1149 /*
1150 * Restore the VMCPU states. VCPU 0 is always started.
1151 */
1152 VMCPU_SET_STATE(&pVM->aCpus[0], VMCPUSTATE_STARTED);
1153 for (VMCPUID i = 1; i < pVM->cCpus; i++)
1154 {
1155 bool fStarted;
1156 int rc = SSMR3GetBool(pSSM, &fStarted);
1157 if (RT_FAILURE(rc))
1158 return rc;
1159 VMCPU_SET_STATE(&pVM->aCpus[i], fStarted ? VMCPUSTATE_STARTED : VMCPUSTATE_STOPPED);
1160 }
1161
1162 /* terminator */
1163 uint32_t u32;
1164 int rc = SSMR3GetU32(pSSM, &u32);
1165 if (RT_FAILURE(rc))
1166 return rc;
1167 if (u32 != UINT32_MAX)
1168 {
1169 AssertMsgFailed(("u32=%#x\n", u32));
1170 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1171 }
1172 return VINF_SUCCESS;
1173}
1174
1175
1176#ifdef VBOX_WITH_RAW_MODE
1177/**
1178 * Resolve a builtin RC symbol.
1179 *
1180 * Called by PDM when loading or relocating RC modules.
1181 *
1182 * @returns VBox status
1183 * @param pVM The cross context VM structure.
1184 * @param pszSymbol Symbol to resolve.
1185 * @param pRCPtrValue Where to store the symbol value.
1186 *
1187 * @remark This has to work before VMMR3Relocate() is called.
1188 */
1189VMMR3_INT_DECL(int) VMMR3GetImportRC(PVM pVM, const char *pszSymbol, PRTRCPTR pRCPtrValue)
1190{
1191 if (!strcmp(pszSymbol, "g_Logger"))
1192 {
1193 if (pVM->vmm.s.pRCLoggerR3)
1194 pVM->vmm.s.pRCLoggerRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pRCLoggerR3);
1195 *pRCPtrValue = pVM->vmm.s.pRCLoggerRC;
1196 }
1197 else if (!strcmp(pszSymbol, "g_RelLogger"))
1198 {
1199# ifdef VBOX_WITH_RC_RELEASE_LOGGING
1200 if (pVM->vmm.s.pRCRelLoggerR3)
1201 pVM->vmm.s.pRCRelLoggerRC = MMHyperR3ToRC(pVM, pVM->vmm.s.pRCRelLoggerR3);
1202 *pRCPtrValue = pVM->vmm.s.pRCRelLoggerRC;
1203# else
1204 *pRCPtrValue = NIL_RTRCPTR;
1205# endif
1206 }
1207 else
1208 return VERR_SYMBOL_NOT_FOUND;
1209 return VINF_SUCCESS;
1210}
1211#endif /* VBOX_WITH_RAW_MODE */
1212
1213
1214/**
1215 * Suspends the CPU yielder.
1216 *
1217 * @param pVM The cross context VM structure.
1218 */
1219VMMR3_INT_DECL(void) VMMR3YieldSuspend(PVM pVM)
1220{
1221 VMCPU_ASSERT_EMT(&pVM->aCpus[0]);
1222 if (!pVM->vmm.s.cYieldResumeMillies)
1223 {
1224 uint64_t u64Now = TMTimerGet(pVM->vmm.s.pYieldTimer);
1225 uint64_t u64Expire = TMTimerGetExpire(pVM->vmm.s.pYieldTimer);
1226 if (u64Now >= u64Expire || u64Expire == ~(uint64_t)0)
1227 pVM->vmm.s.cYieldResumeMillies = pVM->vmm.s.cYieldEveryMillies;
1228 else
1229 pVM->vmm.s.cYieldResumeMillies = TMTimerToMilli(pVM->vmm.s.pYieldTimer, u64Expire - u64Now);
1230 TMTimerStop(pVM->vmm.s.pYieldTimer);
1231 }
1232 pVM->vmm.s.u64LastYield = RTTimeNanoTS();
1233}
1234
1235
1236/**
1237 * Stops the CPU yielder.
1238 *
1239 * @param pVM The cross context VM structure.
1240 */
1241VMMR3_INT_DECL(void) VMMR3YieldStop(PVM pVM)
1242{
1243 if (!pVM->vmm.s.cYieldResumeMillies)
1244 TMTimerStop(pVM->vmm.s.pYieldTimer);
1245 pVM->vmm.s.cYieldResumeMillies = pVM->vmm.s.cYieldEveryMillies;
1246 pVM->vmm.s.u64LastYield = RTTimeNanoTS();
1247}
1248
1249
1250/**
1251 * Resumes the CPU yielder when it has been a suspended or stopped.
1252 *
1253 * @param pVM The cross context VM structure.
1254 */
1255VMMR3_INT_DECL(void) VMMR3YieldResume(PVM pVM)
1256{
1257 if (pVM->vmm.s.cYieldResumeMillies)
1258 {
1259 TMTimerSetMillies(pVM->vmm.s.pYieldTimer, pVM->vmm.s.cYieldResumeMillies);
1260 pVM->vmm.s.cYieldResumeMillies = 0;
1261 }
1262}
1263
1264
1265/**
1266 * Internal timer callback function.
1267 *
1268 * @param pVM The cross context VM structure.
1269 * @param pTimer The timer handle.
1270 * @param pvUser User argument specified upon timer creation.
1271 */
1272static DECLCALLBACK(void) vmmR3YieldEMT(PVM pVM, PTMTIMER pTimer, void *pvUser)
1273{
1274 NOREF(pvUser);
1275
1276 /*
1277 * This really needs some careful tuning. While we shouldn't be too greedy since
1278 * that'll cause the rest of the system to stop up, we shouldn't be too nice either
1279 * because that'll cause us to stop up.
1280 *
1281 * The current logic is to use the default interval when there is no lag worth
1282 * mentioning, but when we start accumulating lag we don't bother yielding at all.
1283 *
1284 * (This depends on the TMCLOCK_VIRTUAL_SYNC to be scheduled before TMCLOCK_REAL
1285 * so the lag is up to date.)
1286 */
1287 const uint64_t u64Lag = TMVirtualSyncGetLag(pVM);
1288 if ( u64Lag < 50000000 /* 50ms */
1289 || ( u64Lag < 1000000000 /* 1s */
1290 && RTTimeNanoTS() - pVM->vmm.s.u64LastYield < 500000000 /* 500 ms */)
1291 )
1292 {
1293 uint64_t u64Elapsed = RTTimeNanoTS();
1294 pVM->vmm.s.u64LastYield = u64Elapsed;
1295
1296 RTThreadYield();
1297
1298#ifdef LOG_ENABLED
1299 u64Elapsed = RTTimeNanoTS() - u64Elapsed;
1300 Log(("vmmR3YieldEMT: %RI64 ns\n", u64Elapsed));
1301#endif
1302 }
1303 TMTimerSetMillies(pTimer, pVM->vmm.s.cYieldEveryMillies);
1304}
1305
1306
1307#ifdef VBOX_WITH_RAW_MODE
1308/**
1309 * Executes guest code in the raw-mode context.
1310 *
1311 * @param pVM The cross context VM structure.
1312 * @param pVCpu The cross context virtual CPU structure.
1313 */
1314VMMR3_INT_DECL(int) VMMR3RawRunGC(PVM pVM, PVMCPU pVCpu)
1315{
1316 Log2(("VMMR3RawRunGC: (cs:eip=%04x:%08x)\n", CPUMGetGuestCS(pVCpu), CPUMGetGuestEIP(pVCpu)));
1317
1318 AssertReturn(pVM->cCpus == 1, VERR_RAW_MODE_INVALID_SMP);
1319
1320 /*
1321 * Set the hypervisor to resume executing a CPUM resume function
1322 * in CPUMRCA.asm.
1323 */
1324 CPUMSetHyperState(pVCpu,
1325 CPUMGetGuestEFlags(pVCpu) & X86_EFL_VM
1326 ? pVM->vmm.s.pfnCPUMRCResumeGuestV86
1327 : pVM->vmm.s.pfnCPUMRCResumeGuest, /* eip */
1328 pVCpu->vmm.s.pbEMTStackBottomRC, /* esp */
1329 0, /* eax */
1330 VM_RC_ADDR(pVM, &pVCpu->cpum) /* edx */);
1331
1332 /*
1333 * We hide log flushes (outer) and hypervisor interrupts (inner).
1334 */
1335 for (;;)
1336 {
1337#ifdef VBOX_STRICT
1338 if (RT_UNLIKELY(!CPUMGetHyperCR3(pVCpu) || CPUMGetHyperCR3(pVCpu) != PGMGetHyperCR3(pVCpu)))
1339 EMR3FatalError(pVCpu, VERR_VMM_HYPER_CR3_MISMATCH);
1340 PGMMapCheck(pVM);
1341# ifdef VBOX_WITH_SAFE_STR
1342 SELMR3CheckShadowTR(pVM);
1343# endif
1344#endif
1345 int rc;
1346 do
1347 {
1348#ifdef NO_SUPCALLR0VMM
1349 rc = VERR_GENERAL_FAILURE;
1350#else
1351 rc = SUPR3CallVMMR0Fast(pVM->pVMR0, VMMR0_DO_RAW_RUN, 0);
1352 if (RT_LIKELY(rc == VINF_SUCCESS))
1353 rc = pVCpu->vmm.s.iLastGZRc;
1354#endif
1355 } while (rc == VINF_EM_RAW_INTERRUPT_HYPER);
1356
1357 /*
1358 * Flush the logs.
1359 */
1360#ifdef LOG_ENABLED
1361 PRTLOGGERRC pLogger = pVM->vmm.s.pRCLoggerR3;
1362 if ( pLogger
1363 && pLogger->offScratch > 0)
1364 RTLogFlushRC(NULL, pLogger);
1365#endif
1366#ifdef VBOX_WITH_RC_RELEASE_LOGGING
1367 PRTLOGGERRC pRelLogger = pVM->vmm.s.pRCRelLoggerR3;
1368 if (RT_UNLIKELY(pRelLogger && pRelLogger->offScratch > 0))
1369 RTLogFlushRC(RTLogRelGetDefaultInstance(), pRelLogger);
1370#endif
1371 if (rc != VINF_VMM_CALL_HOST)
1372 {
1373 Log2(("VMMR3RawRunGC: returns %Rrc (cs:eip=%04x:%08x)\n", rc, CPUMGetGuestCS(pVCpu), CPUMGetGuestEIP(pVCpu)));
1374 return rc;
1375 }
1376 rc = vmmR3ServiceCallRing3Request(pVM, pVCpu);
1377 if (RT_FAILURE(rc))
1378 return rc;
1379 /* Resume GC */
1380 }
1381}
1382#endif /* VBOX_WITH_RAW_MODE */
1383
1384
1385/**
1386 * Executes guest code (Intel VT-x and AMD-V).
1387 *
1388 * @param pVM The cross context VM structure.
1389 * @param pVCpu The cross context virtual CPU structure.
1390 */
1391VMMR3_INT_DECL(int) VMMR3HmRunGC(PVM pVM, PVMCPU pVCpu)
1392{
1393 Log2(("VMMR3HmRunGC: (cs:rip=%04x:%RX64)\n", CPUMGetGuestCS(pVCpu), CPUMGetGuestRIP(pVCpu)));
1394
1395 for (;;)
1396 {
1397 int rc;
1398 do
1399 {
1400#ifdef NO_SUPCALLR0VMM
1401 rc = VERR_GENERAL_FAILURE;
1402#else
1403 rc = SUPR3CallVMMR0Fast(pVM->pVMR0, VMMR0_DO_HM_RUN, pVCpu->idCpu);
1404 if (RT_LIKELY(rc == VINF_SUCCESS))
1405 rc = pVCpu->vmm.s.iLastGZRc;
1406#endif
1407 } while (rc == VINF_EM_RAW_INTERRUPT_HYPER);
1408
1409#if 0 /** @todo triggers too often */
1410 Assert(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_TO_R3));
1411#endif
1412
1413#ifdef LOG_ENABLED
1414 /*
1415 * Flush the log
1416 */
1417 PVMMR0LOGGER pR0LoggerR3 = pVCpu->vmm.s.pR0LoggerR3;
1418 if ( pR0LoggerR3
1419 && pR0LoggerR3->Logger.offScratch > 0)
1420 RTLogFlushR0(NULL, &pR0LoggerR3->Logger);
1421#endif /* !LOG_ENABLED */
1422 if (rc != VINF_VMM_CALL_HOST)
1423 {
1424 Log2(("VMMR3HmRunGC: returns %Rrc (cs:rip=%04x:%RX64)\n", rc, CPUMGetGuestCS(pVCpu), CPUMGetGuestRIP(pVCpu)));
1425 return rc;
1426 }
1427 rc = vmmR3ServiceCallRing3Request(pVM, pVCpu);
1428 if (RT_FAILURE(rc))
1429 return rc;
1430 /* Resume R0 */
1431 }
1432}
1433
1434
1435/**
1436 * VCPU worker for VMMSendStartupIpi.
1437 *
1438 * @param pVM The cross context VM structure.
1439 * @param idCpu Virtual CPU to perform SIPI on.
1440 * @param uVector The SIPI vector.
1441 */
1442static DECLCALLBACK(int) vmmR3SendStarupIpi(PVM pVM, VMCPUID idCpu, uint32_t uVector)
1443{
1444 PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
1445 VMCPU_ASSERT_EMT(pVCpu);
1446
1447 /*
1448 * Active, halt and shutdown states of the processor all block SIPIs.
1449 * So we can safely discard the SIPI. See Intel spec. 26.6.2 "Activity State".
1450 */
1451 if (EMGetState(pVCpu) != EMSTATE_WAIT_SIPI)
1452 return VERR_ACCESS_DENIED;
1453
1454
1455 PCPUMCTX pCtx = CPUMQueryGuestCtxPtr(pVCpu);
1456
1457 pCtx->cs.Sel = uVector << 8;
1458 pCtx->cs.ValidSel = uVector << 8;
1459 pCtx->cs.fFlags = CPUMSELREG_FLAGS_VALID;
1460 pCtx->cs.u64Base = uVector << 12;
1461 pCtx->cs.u32Limit = UINT32_C(0x0000ffff);
1462 pCtx->rip = 0;
1463
1464 Log(("vmmR3SendSipi for VCPU %d with vector %x\n", idCpu, uVector));
1465
1466# if 1 /* If we keep the EMSTATE_WAIT_SIPI method, then move this to EM.cpp. */
1467 EMSetState(pVCpu, EMSTATE_HALTED);
1468 return VINF_EM_RESCHEDULE;
1469# else /* And if we go the VMCPU::enmState way it can stay here. */
1470 VMCPU_ASSERT_STATE(pVCpu, VMCPUSTATE_STOPPED);
1471 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED);
1472 return VINF_SUCCESS;
1473# endif
1474}
1475
1476
1477static DECLCALLBACK(int) vmmR3SendInitIpi(PVM pVM, VMCPUID idCpu)
1478{
1479 PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
1480 VMCPU_ASSERT_EMT(pVCpu);
1481
1482 Log(("vmmR3SendInitIpi for VCPU %d\n", idCpu));
1483
1484 /** @todo Figure out how to handle a nested-guest intercepts here for INIT
1485 * IPI (e.g. SVM_EXIT_INIT). */
1486
1487 PGMR3ResetCpu(pVM, pVCpu);
1488 PDMR3ResetCpu(pVCpu); /* Only clears pending interrupts force flags */
1489 APICR3InitIpi(pVCpu);
1490 TRPMR3ResetCpu(pVCpu);
1491 CPUMR3ResetCpu(pVM, pVCpu);
1492 EMR3ResetCpu(pVCpu);
1493 HMR3ResetCpu(pVCpu);
1494
1495 /* This will trickle up on the target EMT. */
1496 return VINF_EM_WAIT_SIPI;
1497}
1498
1499
1500/**
1501 * Sends a Startup IPI to the virtual CPU by setting CS:EIP into
1502 * vector-dependent state and unhalting processor.
1503 *
1504 * @param pVM The cross context VM structure.
1505 * @param idCpu Virtual CPU to perform SIPI on.
1506 * @param uVector SIPI vector.
1507 */
1508VMMR3_INT_DECL(void) VMMR3SendStartupIpi(PVM pVM, VMCPUID idCpu, uint32_t uVector)
1509{
1510 AssertReturnVoid(idCpu < pVM->cCpus);
1511
1512 int rc = VMR3ReqCallNoWait(pVM, idCpu, (PFNRT)vmmR3SendStarupIpi, 3, pVM, idCpu, uVector);
1513 AssertRC(rc);
1514}
1515
1516
1517/**
1518 * Sends init IPI to the virtual CPU.
1519 *
1520 * @param pVM The cross context VM structure.
1521 * @param idCpu Virtual CPU to perform int IPI on.
1522 */
1523VMMR3_INT_DECL(void) VMMR3SendInitIpi(PVM pVM, VMCPUID idCpu)
1524{
1525 AssertReturnVoid(idCpu < pVM->cCpus);
1526
1527 int rc = VMR3ReqCallNoWait(pVM, idCpu, (PFNRT)vmmR3SendInitIpi, 2, pVM, idCpu);
1528 AssertRC(rc);
1529}
1530
1531
1532/**
1533 * Registers the guest memory range that can be used for patching.
1534 *
1535 * @returns VBox status code.
1536 * @param pVM The cross context VM structure.
1537 * @param pPatchMem Patch memory range.
1538 * @param cbPatchMem Size of the memory range.
1539 */
1540VMMR3DECL(int) VMMR3RegisterPatchMemory(PVM pVM, RTGCPTR pPatchMem, unsigned cbPatchMem)
1541{
1542 VM_ASSERT_EMT(pVM);
1543 if (HMIsEnabled(pVM))
1544 return HMR3EnablePatching(pVM, pPatchMem, cbPatchMem);
1545
1546 return VERR_NOT_SUPPORTED;
1547}
1548
1549
1550/**
1551 * Deregisters the guest memory range that can be used for patching.
1552 *
1553 * @returns VBox status code.
1554 * @param pVM The cross context VM structure.
1555 * @param pPatchMem Patch memory range.
1556 * @param cbPatchMem Size of the memory range.
1557 */
1558VMMR3DECL(int) VMMR3DeregisterPatchMemory(PVM pVM, RTGCPTR pPatchMem, unsigned cbPatchMem)
1559{
1560 if (HMIsEnabled(pVM))
1561 return HMR3DisablePatching(pVM, pPatchMem, cbPatchMem);
1562
1563 return VINF_SUCCESS;
1564}
1565
1566
1567/**
1568 * Common recursion handler for the other EMTs.
1569 *
1570 * @returns Strict VBox status code.
1571 * @param pVM The cross context VM structure.
1572 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1573 * @param rcStrict Current status code to be combined with the one
1574 * from this recursion and returned.
1575 */
1576static VBOXSTRICTRC vmmR3EmtRendezvousCommonRecursion(PVM pVM, PVMCPU pVCpu, VBOXSTRICTRC rcStrict)
1577{
1578 int rc2;
1579
1580 /*
1581 * We wait here while the initiator of this recursion reconfigures
1582 * everything. The last EMT to get in signals the initiator.
1583 */
1584 if (ASMAtomicIncU32(&pVM->vmm.s.cRendezvousEmtsRecursingPush) == pVM->cCpus)
1585 {
1586 rc2 = RTSemEventSignal(pVM->vmm.s.hEvtRendezvousRecursionPushCaller);
1587 AssertLogRelRC(rc2);
1588 }
1589
1590 rc2 = RTSemEventMultiWait(pVM->vmm.s.hEvtMulRendezvousRecursionPush, RT_INDEFINITE_WAIT);
1591 AssertLogRelRC(rc2);
1592
1593 /*
1594 * Do the normal rendezvous processing.
1595 */
1596 VBOXSTRICTRC rcStrict2 = vmmR3EmtRendezvousCommon(pVM, pVCpu, false /* fIsCaller */, pVM->vmm.s.fRendezvousFlags,
1597 pVM->vmm.s.pfnRendezvous, pVM->vmm.s.pvRendezvousUser);
1598
1599 /*
1600 * Wait for the initiator to restore everything.
1601 */
1602 rc2 = RTSemEventMultiWait(pVM->vmm.s.hEvtMulRendezvousRecursionPop, RT_INDEFINITE_WAIT);
1603 AssertLogRelRC(rc2);
1604
1605 /*
1606 * Last thread out of here signals the initiator.
1607 */
1608 if (ASMAtomicIncU32(&pVM->vmm.s.cRendezvousEmtsRecursingPop) == pVM->cCpus)
1609 {
1610 rc2 = RTSemEventSignal(pVM->vmm.s.hEvtRendezvousRecursionPopCaller);
1611 AssertLogRelRC(rc2);
1612 }
1613
1614 /*
1615 * Merge status codes and return.
1616 */
1617 AssertRC(VBOXSTRICTRC_VAL(rcStrict2));
1618 if ( rcStrict2 != VINF_SUCCESS
1619 && ( rcStrict == VINF_SUCCESS
1620 || rcStrict > rcStrict2))
1621 rcStrict = rcStrict2;
1622 return rcStrict;
1623}
1624
1625
1626/**
1627 * Count returns and have the last non-caller EMT wake up the caller.
1628 *
1629 * @returns VBox strict informational status code for EM scheduling. No failures
1630 * will be returned here, those are for the caller only.
1631 *
1632 * @param pVM The cross context VM structure.
1633 * @param rcStrict The current accumulated recursive status code,
1634 * to be merged with i32RendezvousStatus and
1635 * returned.
1636 */
1637DECL_FORCE_INLINE(VBOXSTRICTRC) vmmR3EmtRendezvousNonCallerReturn(PVM pVM, VBOXSTRICTRC rcStrict)
1638{
1639 VBOXSTRICTRC rcStrict2 = ASMAtomicReadS32(&pVM->vmm.s.i32RendezvousStatus);
1640
1641 uint32_t cReturned = ASMAtomicIncU32(&pVM->vmm.s.cRendezvousEmtsReturned);
1642 if (cReturned == pVM->cCpus - 1U)
1643 {
1644 int rc = RTSemEventSignal(pVM->vmm.s.hEvtRendezvousDoneCaller);
1645 AssertLogRelRC(rc);
1646 }
1647
1648 /*
1649 * Merge the status codes, ignoring error statuses in this code path.
1650 */
1651 AssertLogRelMsgReturn( rcStrict2 <= VINF_SUCCESS
1652 || (rcStrict2 >= VINF_EM_FIRST && rcStrict2 <= VINF_EM_LAST),
1653 ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict2)),
1654 VERR_IPE_UNEXPECTED_INFO_STATUS);
1655
1656 if (RT_SUCCESS(rcStrict2))
1657 {
1658 if ( rcStrict2 != VINF_SUCCESS
1659 && ( rcStrict == VINF_SUCCESS
1660 || rcStrict > rcStrict2))
1661 rcStrict = rcStrict2;
1662 }
1663 return rcStrict;
1664}
1665
1666
1667/**
1668 * Common worker for VMMR3EmtRendezvous and VMMR3EmtRendezvousFF.
1669 *
1670 * @returns VBox strict informational status code for EM scheduling. No failures
1671 * will be returned here, those are for the caller only. When
1672 * fIsCaller is set, VINF_SUCCESS is always returned.
1673 *
1674 * @param pVM The cross context VM structure.
1675 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1676 * @param fIsCaller Whether we're the VMMR3EmtRendezvous caller or
1677 * not.
1678 * @param fFlags The flags.
1679 * @param pfnRendezvous The callback.
1680 * @param pvUser The user argument for the callback.
1681 */
1682static VBOXSTRICTRC vmmR3EmtRendezvousCommon(PVM pVM, PVMCPU pVCpu, bool fIsCaller,
1683 uint32_t fFlags, PFNVMMEMTRENDEZVOUS pfnRendezvous, void *pvUser)
1684{
1685 int rc;
1686 VBOXSTRICTRC rcStrictRecursion = VINF_SUCCESS;
1687
1688 /*
1689 * Enter, the last EMT triggers the next callback phase.
1690 */
1691 uint32_t cEntered = ASMAtomicIncU32(&pVM->vmm.s.cRendezvousEmtsEntered);
1692 if (cEntered != pVM->cCpus)
1693 {
1694 if ((fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE)
1695 {
1696 /* Wait for our turn. */
1697 for (;;)
1698 {
1699 rc = RTSemEventWait(pVM->vmm.s.hEvtRendezvousEnterOneByOne, RT_INDEFINITE_WAIT);
1700 AssertLogRelRC(rc);
1701 if (!pVM->vmm.s.fRendezvousRecursion)
1702 break;
1703 rcStrictRecursion = vmmR3EmtRendezvousCommonRecursion(pVM, pVCpu, rcStrictRecursion);
1704 }
1705 }
1706 else if ((fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ALL_AT_ONCE)
1707 {
1708 /* Wait for the last EMT to arrive and wake everyone up. */
1709 rc = RTSemEventMultiWait(pVM->vmm.s.hEvtMulRendezvousEnterAllAtOnce, RT_INDEFINITE_WAIT);
1710 AssertLogRelRC(rc);
1711 Assert(!pVM->vmm.s.fRendezvousRecursion);
1712 }
1713 else if ( (fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING
1714 || (fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING)
1715 {
1716 /* Wait for our turn. */
1717 for (;;)
1718 {
1719 rc = RTSemEventWait(pVM->vmm.s.pahEvtRendezvousEnterOrdered[pVCpu->idCpu], RT_INDEFINITE_WAIT);
1720 AssertLogRelRC(rc);
1721 if (!pVM->vmm.s.fRendezvousRecursion)
1722 break;
1723 rcStrictRecursion = vmmR3EmtRendezvousCommonRecursion(pVM, pVCpu, rcStrictRecursion);
1724 }
1725 }
1726 else
1727 {
1728 Assert((fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE);
1729
1730 /*
1731 * The execute once is handled specially to optimize the code flow.
1732 *
1733 * The last EMT to arrive will perform the callback and the other
1734 * EMTs will wait on the Done/DoneCaller semaphores (instead of
1735 * the EnterOneByOne/AllAtOnce) in the meanwhile. When the callback
1736 * returns, that EMT will initiate the normal return sequence.
1737 */
1738 if (!fIsCaller)
1739 {
1740 for (;;)
1741 {
1742 rc = RTSemEventMultiWait(pVM->vmm.s.hEvtMulRendezvousDone, RT_INDEFINITE_WAIT);
1743 AssertLogRelRC(rc);
1744 if (!pVM->vmm.s.fRendezvousRecursion)
1745 break;
1746 rcStrictRecursion = vmmR3EmtRendezvousCommonRecursion(pVM, pVCpu, rcStrictRecursion);
1747 }
1748
1749 return vmmR3EmtRendezvousNonCallerReturn(pVM, rcStrictRecursion);
1750 }
1751 return VINF_SUCCESS;
1752 }
1753 }
1754 else
1755 {
1756 /*
1757 * All EMTs are waiting, clear the FF and take action according to the
1758 * execution method.
1759 */
1760 VM_FF_CLEAR(pVM, VM_FF_EMT_RENDEZVOUS);
1761
1762 if ((fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ALL_AT_ONCE)
1763 {
1764 /* Wake up everyone. */
1765 rc = RTSemEventMultiSignal(pVM->vmm.s.hEvtMulRendezvousEnterAllAtOnce);
1766 AssertLogRelRC(rc);
1767 }
1768 else if ( (fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING
1769 || (fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING)
1770 {
1771 /* Figure out who to wake up and wake it up. If it's ourself, then
1772 it's easy otherwise wait for our turn. */
1773 VMCPUID iFirst = (fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING
1774 ? 0
1775 : pVM->cCpus - 1U;
1776 if (pVCpu->idCpu != iFirst)
1777 {
1778 rc = RTSemEventSignal(pVM->vmm.s.pahEvtRendezvousEnterOrdered[iFirst]);
1779 AssertLogRelRC(rc);
1780 for (;;)
1781 {
1782 rc = RTSemEventWait(pVM->vmm.s.pahEvtRendezvousEnterOrdered[pVCpu->idCpu], RT_INDEFINITE_WAIT);
1783 AssertLogRelRC(rc);
1784 if (!pVM->vmm.s.fRendezvousRecursion)
1785 break;
1786 rcStrictRecursion = vmmR3EmtRendezvousCommonRecursion(pVM, pVCpu, rcStrictRecursion);
1787 }
1788 }
1789 }
1790 /* else: execute the handler on the current EMT and wake up one or more threads afterwards. */
1791 }
1792
1793
1794 /*
1795 * Do the callback and update the status if necessary.
1796 */
1797 if ( !(fFlags & VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR)
1798 || RT_SUCCESS(ASMAtomicUoReadS32(&pVM->vmm.s.i32RendezvousStatus)) )
1799 {
1800 VBOXSTRICTRC rcStrict2 = pfnRendezvous(pVM, pVCpu, pvUser);
1801 if (rcStrict2 != VINF_SUCCESS)
1802 {
1803 AssertLogRelMsg( rcStrict2 <= VINF_SUCCESS
1804 || (rcStrict2 >= VINF_EM_FIRST && rcStrict2 <= VINF_EM_LAST),
1805 ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict2)));
1806 int32_t i32RendezvousStatus;
1807 do
1808 {
1809 i32RendezvousStatus = ASMAtomicUoReadS32(&pVM->vmm.s.i32RendezvousStatus);
1810 if ( rcStrict2 == i32RendezvousStatus
1811 || RT_FAILURE(i32RendezvousStatus)
1812 || ( i32RendezvousStatus != VINF_SUCCESS
1813 && rcStrict2 > i32RendezvousStatus))
1814 break;
1815 } while (!ASMAtomicCmpXchgS32(&pVM->vmm.s.i32RendezvousStatus, VBOXSTRICTRC_VAL(rcStrict2), i32RendezvousStatus));
1816 }
1817 }
1818
1819 /*
1820 * Increment the done counter and take action depending on whether we're
1821 * the last to finish callback execution.
1822 */
1823 uint32_t cDone = ASMAtomicIncU32(&pVM->vmm.s.cRendezvousEmtsDone);
1824 if ( cDone != pVM->cCpus
1825 && (fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) != VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE)
1826 {
1827 /* Signal the next EMT? */
1828 if ((fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE)
1829 {
1830 rc = RTSemEventSignal(pVM->vmm.s.hEvtRendezvousEnterOneByOne);
1831 AssertLogRelRC(rc);
1832 }
1833 else if ((fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING)
1834 {
1835 Assert(cDone == pVCpu->idCpu + 1U);
1836 rc = RTSemEventSignal(pVM->vmm.s.pahEvtRendezvousEnterOrdered[pVCpu->idCpu + 1U]);
1837 AssertLogRelRC(rc);
1838 }
1839 else if ((fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING)
1840 {
1841 Assert(pVM->cCpus - cDone == pVCpu->idCpu);
1842 rc = RTSemEventSignal(pVM->vmm.s.pahEvtRendezvousEnterOrdered[pVM->cCpus - cDone - 1U]);
1843 AssertLogRelRC(rc);
1844 }
1845
1846 /* Wait for the rest to finish (the caller waits on hEvtRendezvousDoneCaller). */
1847 if (!fIsCaller)
1848 {
1849 for (;;)
1850 {
1851 rc = RTSemEventMultiWait(pVM->vmm.s.hEvtMulRendezvousDone, RT_INDEFINITE_WAIT);
1852 AssertLogRelRC(rc);
1853 if (!pVM->vmm.s.fRendezvousRecursion)
1854 break;
1855 rcStrictRecursion = vmmR3EmtRendezvousCommonRecursion(pVM, pVCpu, rcStrictRecursion);
1856 }
1857 }
1858 }
1859 else
1860 {
1861 /* Callback execution is all done, tell the rest to return. */
1862 rc = RTSemEventMultiSignal(pVM->vmm.s.hEvtMulRendezvousDone);
1863 AssertLogRelRC(rc);
1864 }
1865
1866 if (!fIsCaller)
1867 return vmmR3EmtRendezvousNonCallerReturn(pVM, rcStrictRecursion);
1868 return rcStrictRecursion;
1869}
1870
1871
1872/**
1873 * Called in response to VM_FF_EMT_RENDEZVOUS.
1874 *
1875 * @returns VBox strict status code - EM scheduling. No errors will be returned
1876 * here, nor will any non-EM scheduling status codes be returned.
1877 *
1878 * @param pVM The cross context VM structure.
1879 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1880 *
1881 * @thread EMT
1882 */
1883VMMR3_INT_DECL(int) VMMR3EmtRendezvousFF(PVM pVM, PVMCPU pVCpu)
1884{
1885 Assert(!pVCpu->vmm.s.fInRendezvous);
1886 Log(("VMMR3EmtRendezvousFF: EMT%#u\n", pVCpu->idCpu));
1887 pVCpu->vmm.s.fInRendezvous = true;
1888 VBOXSTRICTRC rcStrict = vmmR3EmtRendezvousCommon(pVM, pVCpu, false /* fIsCaller */, pVM->vmm.s.fRendezvousFlags,
1889 pVM->vmm.s.pfnRendezvous, pVM->vmm.s.pvRendezvousUser);
1890 pVCpu->vmm.s.fInRendezvous = false;
1891 Log(("VMMR3EmtRendezvousFF: EMT%#u returns %Rrc\n", pVCpu->idCpu, VBOXSTRICTRC_VAL(rcStrict)));
1892 return VBOXSTRICTRC_TODO(rcStrict);
1893}
1894
1895
1896/**
1897 * Helper for resetting an single wakeup event sempahore.
1898 *
1899 * @returns VERR_TIMEOUT on success, RTSemEventWait status otherwise.
1900 * @param hEvt The event semaphore to reset.
1901 */
1902static int vmmR3HlpResetEvent(RTSEMEVENT hEvt)
1903{
1904 for (uint32_t cLoops = 0; ; cLoops++)
1905 {
1906 int rc = RTSemEventWait(hEvt, 0 /*cMsTimeout*/);
1907 if (rc != VINF_SUCCESS || cLoops > _4K)
1908 return rc;
1909 }
1910}
1911
1912
1913/**
1914 * Worker for VMMR3EmtRendezvous that handles recursion.
1915 *
1916 * @returns VBox strict status code. This will be the first error,
1917 * VINF_SUCCESS, or an EM scheduling status code.
1918 *
1919 * @param pVM The cross context VM structure.
1920 * @param pVCpu The cross context virtual CPU structure of the
1921 * calling EMT.
1922 * @param fFlags Flags indicating execution methods. See
1923 * grp_VMMR3EmtRendezvous_fFlags.
1924 * @param pfnRendezvous The callback.
1925 * @param pvUser User argument for the callback.
1926 *
1927 * @thread EMT(pVCpu)
1928 */
1929static VBOXSTRICTRC vmmR3EmtRendezvousRecursive(PVM pVM, PVMCPU pVCpu, uint32_t fFlags,
1930 PFNVMMEMTRENDEZVOUS pfnRendezvous, void *pvUser)
1931{
1932 Log(("vmmR3EmtRendezvousRecursive: %#x EMT#%u depth=%d\n", fFlags, pVCpu->idCpu, pVM->vmm.s.cRendezvousRecursions));
1933 AssertLogRelReturn(pVM->vmm.s.cRendezvousRecursions < 3, VERR_DEADLOCK);
1934 Assert(pVCpu->vmm.s.fInRendezvous);
1935
1936 /*
1937 * Save the current state.
1938 */
1939 uint32_t const fParentFlags = pVM->vmm.s.fRendezvousFlags;
1940 uint32_t const cParentDone = pVM->vmm.s.cRendezvousEmtsDone;
1941 int32_t const iParentStatus = pVM->vmm.s.i32RendezvousStatus;
1942 PFNVMMEMTRENDEZVOUS const pfnParent = pVM->vmm.s.pfnRendezvous;
1943 void * const pvParentUser = pVM->vmm.s.pvRendezvousUser;
1944
1945 /*
1946 * Check preconditions and save the current state.
1947 */
1948 AssertReturn( (fParentFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING
1949 || (fParentFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING
1950 || (fParentFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE
1951 || (fParentFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE,
1952 VERR_INTERNAL_ERROR);
1953 AssertReturn(pVM->vmm.s.cRendezvousEmtsEntered == pVM->cCpus, VERR_INTERNAL_ERROR_2);
1954 AssertReturn(pVM->vmm.s.cRendezvousEmtsReturned == 0, VERR_INTERNAL_ERROR_3);
1955
1956 /*
1957 * Reset the recursion prep and pop semaphores.
1958 */
1959 int rc = RTSemEventMultiReset(pVM->vmm.s.hEvtMulRendezvousRecursionPush);
1960 AssertLogRelRCReturn(rc, rc);
1961 rc = RTSemEventMultiReset(pVM->vmm.s.hEvtMulRendezvousRecursionPop);
1962 AssertLogRelRCReturn(rc, rc);
1963 rc = vmmR3HlpResetEvent(pVM->vmm.s.hEvtRendezvousRecursionPushCaller);
1964 AssertLogRelMsgReturn(rc == VERR_TIMEOUT, ("%Rrc\n", rc), RT_FAILURE_NP(rc) ? rc : VERR_IPE_UNEXPECTED_INFO_STATUS);
1965 rc = vmmR3HlpResetEvent(pVM->vmm.s.hEvtRendezvousRecursionPopCaller);
1966 AssertLogRelMsgReturn(rc == VERR_TIMEOUT, ("%Rrc\n", rc), RT_FAILURE_NP(rc) ? rc : VERR_IPE_UNEXPECTED_INFO_STATUS);
1967
1968 /*
1969 * Usher the other thread into the recursion routine.
1970 */
1971 ASMAtomicWriteU32(&pVM->vmm.s.cRendezvousEmtsRecursingPush, 0);
1972 ASMAtomicWriteBool(&pVM->vmm.s.fRendezvousRecursion, true);
1973
1974 uint32_t cLeft = pVM->cCpus - (cParentDone + 1U);
1975 if ((fParentFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE)
1976 while (cLeft-- > 0)
1977 {
1978 rc = RTSemEventSignal(pVM->vmm.s.hEvtRendezvousEnterOneByOne);
1979 AssertLogRelRC(rc);
1980 }
1981 else if ((fParentFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING)
1982 {
1983 Assert(cLeft == pVM->cCpus - (pVCpu->idCpu + 1U));
1984 for (VMCPUID iCpu = pVCpu->idCpu + 1U; iCpu < pVM->cCpus; iCpu++)
1985 {
1986 rc = RTSemEventSignal(pVM->vmm.s.pahEvtRendezvousEnterOrdered[iCpu]);
1987 AssertLogRelRC(rc);
1988 }
1989 }
1990 else if ((fParentFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING)
1991 {
1992 Assert(cLeft == pVCpu->idCpu);
1993 for (VMCPUID iCpu = pVCpu->idCpu; iCpu > 0; iCpu--)
1994 {
1995 rc = RTSemEventSignal(pVM->vmm.s.pahEvtRendezvousEnterOrdered[iCpu - 1U]);
1996 AssertLogRelRC(rc);
1997 }
1998 }
1999 else
2000 AssertLogRelReturn((fParentFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE,
2001 VERR_INTERNAL_ERROR_4);
2002
2003 rc = RTSemEventMultiSignal(pVM->vmm.s.hEvtMulRendezvousDone);
2004 AssertLogRelRC(rc);
2005 rc = RTSemEventSignal(pVM->vmm.s.hEvtRendezvousDoneCaller);
2006 AssertLogRelRC(rc);
2007
2008
2009 /*
2010 * Wait for the EMTs to wake up and get out of the parent rendezvous code.
2011 */
2012 if (ASMAtomicIncU32(&pVM->vmm.s.cRendezvousEmtsRecursingPush) != pVM->cCpus)
2013 {
2014 rc = RTSemEventWait(pVM->vmm.s.hEvtRendezvousRecursionPushCaller, RT_INDEFINITE_WAIT);
2015 AssertLogRelRC(rc);
2016 }
2017
2018 ASMAtomicWriteBool(&pVM->vmm.s.fRendezvousRecursion, false);
2019
2020 /*
2021 * Clear the slate and setup the new rendezvous.
2022 */
2023 for (VMCPUID i = 0; i < pVM->cCpus; i++)
2024 {
2025 rc = vmmR3HlpResetEvent(pVM->vmm.s.pahEvtRendezvousEnterOrdered[i]);
2026 AssertLogRelMsg(rc == VERR_TIMEOUT, ("%Rrc\n", rc));
2027 }
2028 rc = vmmR3HlpResetEvent(pVM->vmm.s.hEvtRendezvousEnterOneByOne); AssertLogRelMsg(rc == VERR_TIMEOUT, ("%Rrc\n", rc));
2029 rc = RTSemEventMultiReset(pVM->vmm.s.hEvtMulRendezvousEnterAllAtOnce); AssertLogRelRC(rc);
2030 rc = RTSemEventMultiReset(pVM->vmm.s.hEvtMulRendezvousDone); AssertLogRelRC(rc);
2031 rc = vmmR3HlpResetEvent(pVM->vmm.s.hEvtRendezvousDoneCaller); AssertLogRelMsg(rc == VERR_TIMEOUT, ("%Rrc\n", rc));
2032
2033 ASMAtomicWriteU32(&pVM->vmm.s.cRendezvousEmtsEntered, 0);
2034 ASMAtomicWriteU32(&pVM->vmm.s.cRendezvousEmtsDone, 0);
2035 ASMAtomicWriteU32(&pVM->vmm.s.cRendezvousEmtsReturned, 0);
2036 ASMAtomicWriteS32(&pVM->vmm.s.i32RendezvousStatus, VINF_SUCCESS);
2037 ASMAtomicWritePtr((void * volatile *)&pVM->vmm.s.pfnRendezvous, (void *)(uintptr_t)pfnRendezvous);
2038 ASMAtomicWritePtr(&pVM->vmm.s.pvRendezvousUser, pvUser);
2039 ASMAtomicWriteU32(&pVM->vmm.s.fRendezvousFlags, fFlags);
2040 ASMAtomicIncU32(&pVM->vmm.s.cRendezvousRecursions);
2041
2042 /*
2043 * We're ready to go now, do normal rendezvous processing.
2044 */
2045 rc = RTSemEventMultiSignal(pVM->vmm.s.hEvtMulRendezvousRecursionPush);
2046 AssertLogRelRC(rc);
2047
2048 VBOXSTRICTRC rcStrict = vmmR3EmtRendezvousCommon(pVM, pVCpu, true /*fIsCaller*/, fFlags, pfnRendezvous, pvUser);
2049
2050 /*
2051 * The caller waits for the other EMTs to be done, return and waiting on the
2052 * pop semaphore.
2053 */
2054 for (;;)
2055 {
2056 rc = RTSemEventWait(pVM->vmm.s.hEvtRendezvousDoneCaller, RT_INDEFINITE_WAIT);
2057 AssertLogRelRC(rc);
2058 if (!pVM->vmm.s.fRendezvousRecursion)
2059 break;
2060 rcStrict = vmmR3EmtRendezvousCommonRecursion(pVM, pVCpu, rcStrict);
2061 }
2062
2063 /*
2064 * Get the return code and merge it with the above recursion status.
2065 */
2066 VBOXSTRICTRC rcStrict2 = pVM->vmm.s.i32RendezvousStatus;
2067 if ( rcStrict2 != VINF_SUCCESS
2068 && ( rcStrict == VINF_SUCCESS
2069 || rcStrict > rcStrict2))
2070 rcStrict = rcStrict2;
2071
2072 /*
2073 * Restore the parent rendezvous state.
2074 */
2075 for (VMCPUID i = 0; i < pVM->cCpus; i++)
2076 {
2077 rc = vmmR3HlpResetEvent(pVM->vmm.s.pahEvtRendezvousEnterOrdered[i]);
2078 AssertLogRelMsg(rc == VERR_TIMEOUT, ("%Rrc\n", rc));
2079 }
2080 rc = vmmR3HlpResetEvent(pVM->vmm.s.hEvtRendezvousEnterOneByOne); AssertLogRelMsg(rc == VERR_TIMEOUT, ("%Rrc\n", rc));
2081 rc = RTSemEventMultiReset(pVM->vmm.s.hEvtMulRendezvousEnterAllAtOnce); AssertLogRelRC(rc);
2082 rc = RTSemEventMultiReset(pVM->vmm.s.hEvtMulRendezvousDone); AssertLogRelRC(rc);
2083 rc = vmmR3HlpResetEvent(pVM->vmm.s.hEvtRendezvousDoneCaller); AssertLogRelMsg(rc == VERR_TIMEOUT, ("%Rrc\n", rc));
2084
2085 ASMAtomicWriteU32(&pVM->vmm.s.cRendezvousEmtsEntered, pVM->cCpus);
2086 ASMAtomicWriteU32(&pVM->vmm.s.cRendezvousEmtsReturned, 0);
2087 ASMAtomicWriteU32(&pVM->vmm.s.cRendezvousEmtsDone, cParentDone);
2088 ASMAtomicWriteS32(&pVM->vmm.s.i32RendezvousStatus, iParentStatus);
2089 ASMAtomicWriteU32(&pVM->vmm.s.fRendezvousFlags, fParentFlags);
2090 ASMAtomicWritePtr(&pVM->vmm.s.pvRendezvousUser, pvParentUser);
2091 ASMAtomicWritePtr((void * volatile *)&pVM->vmm.s.pfnRendezvous, (void *)(uintptr_t)pfnParent);
2092
2093 /*
2094 * Usher the other EMTs back to their parent recursion routine, waiting
2095 * for them to all get there before we return (makes sure they've been
2096 * scheduled and are past the pop event sem, see below).
2097 */
2098 ASMAtomicWriteU32(&pVM->vmm.s.cRendezvousEmtsRecursingPop, 0);
2099 rc = RTSemEventMultiSignal(pVM->vmm.s.hEvtMulRendezvousRecursionPop);
2100 AssertLogRelRC(rc);
2101
2102 if (ASMAtomicIncU32(&pVM->vmm.s.cRendezvousEmtsRecursingPop) != pVM->cCpus)
2103 {
2104 rc = RTSemEventWait(pVM->vmm.s.hEvtRendezvousRecursionPopCaller, RT_INDEFINITE_WAIT);
2105 AssertLogRelRC(rc);
2106 }
2107
2108 /*
2109 * We must reset the pop semaphore on the way out (doing the pop caller too,
2110 * just in case). The parent may be another recursion.
2111 */
2112 rc = RTSemEventMultiReset(pVM->vmm.s.hEvtMulRendezvousRecursionPop); AssertLogRelRC(rc);
2113 rc = vmmR3HlpResetEvent(pVM->vmm.s.hEvtRendezvousRecursionPopCaller); AssertLogRelMsg(rc == VERR_TIMEOUT, ("%Rrc\n", rc));
2114
2115 ASMAtomicDecU32(&pVM->vmm.s.cRendezvousRecursions);
2116
2117 Log(("vmmR3EmtRendezvousRecursive: %#x EMT#%u depth=%d returns %Rrc\n",
2118 fFlags, pVCpu->idCpu, pVM->vmm.s.cRendezvousRecursions, VBOXSTRICTRC_VAL(rcStrict)));
2119 return rcStrict;
2120}
2121
2122
2123/**
2124 * EMT rendezvous.
2125 *
2126 * Gathers all the EMTs and execute some code on each of them, either in a one
2127 * by one fashion or all at once.
2128 *
2129 * @returns VBox strict status code. This will be the first error,
2130 * VINF_SUCCESS, or an EM scheduling status code.
2131 *
2132 * @retval VERR_DEADLOCK if recursion is attempted using a rendezvous type that
2133 * doesn't support it or if the recursion is too deep.
2134 *
2135 * @param pVM The cross context VM structure.
2136 * @param fFlags Flags indicating execution methods. See
2137 * grp_VMMR3EmtRendezvous_fFlags. The one-by-one,
2138 * descending and ascending rendezvous types support
2139 * recursion from inside @a pfnRendezvous.
2140 * @param pfnRendezvous The callback.
2141 * @param pvUser User argument for the callback.
2142 *
2143 * @thread Any.
2144 */
2145VMMR3DECL(int) VMMR3EmtRendezvous(PVM pVM, uint32_t fFlags, PFNVMMEMTRENDEZVOUS pfnRendezvous, void *pvUser)
2146{
2147 /*
2148 * Validate input.
2149 */
2150 AssertReturn(pVM, VERR_INVALID_VM_HANDLE);
2151 AssertMsg( (fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) != VMMEMTRENDEZVOUS_FLAGS_TYPE_INVALID
2152 && (fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) <= VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING
2153 && !(fFlags & ~VMMEMTRENDEZVOUS_FLAGS_VALID_MASK), ("%#x\n", fFlags));
2154 AssertMsg( !(fFlags & VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR)
2155 || ( (fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) != VMMEMTRENDEZVOUS_FLAGS_TYPE_ALL_AT_ONCE
2156 && (fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) != VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE),
2157 ("type %u\n", fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK));
2158
2159 VBOXSTRICTRC rcStrict;
2160 PVMCPU pVCpu = VMMGetCpu(pVM);
2161 if (!pVCpu)
2162 /*
2163 * Forward the request to an EMT thread.
2164 */
2165 {
2166 Log(("VMMR3EmtRendezvous: %#x non-EMT\n", fFlags));
2167 if (!(fFlags & VMMEMTRENDEZVOUS_FLAGS_PRIORITY))
2168 rcStrict = VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)VMMR3EmtRendezvous, 4, pVM, fFlags, pfnRendezvous, pvUser);
2169 else
2170 rcStrict = VMR3ReqPriorityCallWait(pVM, VMCPUID_ANY, (PFNRT)VMMR3EmtRendezvous, 4, pVM, fFlags, pfnRendezvous, pvUser);
2171 Log(("VMMR3EmtRendezvous: %#x non-EMT returns %Rrc\n", fFlags, VBOXSTRICTRC_VAL(rcStrict)));
2172 }
2173 else if (pVM->cCpus == 1)
2174 {
2175 /*
2176 * Shortcut for the single EMT case.
2177 */
2178 if (!pVCpu->vmm.s.fInRendezvous)
2179 {
2180 Log(("VMMR3EmtRendezvous: %#x EMT (uni)\n", fFlags));
2181 pVCpu->vmm.s.fInRendezvous = true;
2182 pVM->vmm.s.fRendezvousFlags = fFlags;
2183 rcStrict = pfnRendezvous(pVM, pVCpu, pvUser);
2184 pVCpu->vmm.s.fInRendezvous = false;
2185 }
2186 else
2187 {
2188 /* Recursion. Do the same checks as in the SMP case. */
2189 Log(("VMMR3EmtRendezvous: %#x EMT (uni), recursion depth=%d\n", fFlags, pVM->vmm.s.cRendezvousRecursions));
2190 uint32_t fType = pVM->vmm.s.fRendezvousFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK;
2191 AssertLogRelReturn( !pVCpu->vmm.s.fInRendezvous
2192 || fType == VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING
2193 || fType == VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING
2194 || fType == VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE
2195 || fType == VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE
2196 , VERR_DEADLOCK);
2197
2198 AssertLogRelReturn(pVM->vmm.s.cRendezvousRecursions < 3, VERR_DEADLOCK);
2199 pVM->vmm.s.cRendezvousRecursions++;
2200 uint32_t const fParentFlags = pVM->vmm.s.fRendezvousFlags;
2201 pVM->vmm.s.fRendezvousFlags = fFlags;
2202
2203 rcStrict = pfnRendezvous(pVM, pVCpu, pvUser);
2204
2205 pVM->vmm.s.fRendezvousFlags = fParentFlags;
2206 pVM->vmm.s.cRendezvousRecursions--;
2207 }
2208 Log(("VMMR3EmtRendezvous: %#x EMT (uni) returns %Rrc\n", fFlags, VBOXSTRICTRC_VAL(rcStrict)));
2209 }
2210 else
2211 {
2212 /*
2213 * Spin lock. If busy, check for recursion, if not recursing wait for
2214 * the other EMT to finish while keeping a lookout for the RENDEZVOUS FF.
2215 */
2216 int rc;
2217 rcStrict = VINF_SUCCESS;
2218 if (RT_UNLIKELY(!ASMAtomicCmpXchgU32(&pVM->vmm.s.u32RendezvousLock, 0x77778888, 0)))
2219 {
2220 /* Allow recursion in some cases. */
2221 if ( pVCpu->vmm.s.fInRendezvous
2222 && ( (pVM->vmm.s.fRendezvousFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING
2223 || (pVM->vmm.s.fRendezvousFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING
2224 || (pVM->vmm.s.fRendezvousFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE
2225 || (pVM->vmm.s.fRendezvousFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE
2226 ))
2227 return VBOXSTRICTRC_TODO(vmmR3EmtRendezvousRecursive(pVM, pVCpu, fFlags, pfnRendezvous, pvUser));
2228
2229 AssertLogRelMsgReturn(!pVCpu->vmm.s.fInRendezvous, ("fRendezvousFlags=%#x\n", pVM->vmm.s.fRendezvousFlags),
2230 VERR_DEADLOCK);
2231
2232 Log(("VMMR3EmtRendezvous: %#x EMT#%u, waiting for lock...\n", fFlags, pVCpu->idCpu));
2233 while (!ASMAtomicCmpXchgU32(&pVM->vmm.s.u32RendezvousLock, 0x77778888, 0))
2234 {
2235 if (VM_FF_IS_PENDING(pVM, VM_FF_EMT_RENDEZVOUS))
2236 {
2237 rc = VMMR3EmtRendezvousFF(pVM, pVCpu);
2238 if ( rc != VINF_SUCCESS
2239 && ( rcStrict == VINF_SUCCESS
2240 || rcStrict > rc))
2241 rcStrict = rc;
2242 /** @todo Perhaps deal with termination here? */
2243 }
2244 ASMNopPause();
2245 }
2246 }
2247
2248 Log(("VMMR3EmtRendezvous: %#x EMT#%u\n", fFlags, pVCpu->idCpu));
2249 Assert(!VM_FF_IS_PENDING(pVM, VM_FF_EMT_RENDEZVOUS));
2250 Assert(!pVCpu->vmm.s.fInRendezvous);
2251 pVCpu->vmm.s.fInRendezvous = true;
2252
2253 /*
2254 * Clear the slate and setup the rendezvous. This is a semaphore ping-pong orgy. :-)
2255 */
2256 for (VMCPUID i = 0; i < pVM->cCpus; i++)
2257 {
2258 rc = RTSemEventWait(pVM->vmm.s.pahEvtRendezvousEnterOrdered[i], 0);
2259 AssertLogRelMsg(rc == VERR_TIMEOUT || rc == VINF_SUCCESS, ("%Rrc\n", rc));
2260 }
2261 rc = RTSemEventWait(pVM->vmm.s.hEvtRendezvousEnterOneByOne, 0); AssertLogRelMsg(rc == VERR_TIMEOUT || rc == VINF_SUCCESS, ("%Rrc\n", rc));
2262 rc = RTSemEventMultiReset(pVM->vmm.s.hEvtMulRendezvousEnterAllAtOnce); AssertLogRelRC(rc);
2263 rc = RTSemEventMultiReset(pVM->vmm.s.hEvtMulRendezvousDone); AssertLogRelRC(rc);
2264 rc = RTSemEventWait(pVM->vmm.s.hEvtRendezvousDoneCaller, 0); AssertLogRelMsg(rc == VERR_TIMEOUT || rc == VINF_SUCCESS, ("%Rrc\n", rc));
2265 ASMAtomicWriteU32(&pVM->vmm.s.cRendezvousEmtsEntered, 0);
2266 ASMAtomicWriteU32(&pVM->vmm.s.cRendezvousEmtsDone, 0);
2267 ASMAtomicWriteU32(&pVM->vmm.s.cRendezvousEmtsReturned, 0);
2268 ASMAtomicWriteS32(&pVM->vmm.s.i32RendezvousStatus, VINF_SUCCESS);
2269 ASMAtomicWritePtr((void * volatile *)&pVM->vmm.s.pfnRendezvous, (void *)(uintptr_t)pfnRendezvous);
2270 ASMAtomicWritePtr(&pVM->vmm.s.pvRendezvousUser, pvUser);
2271 ASMAtomicWriteU32(&pVM->vmm.s.fRendezvousFlags, fFlags);
2272
2273 /*
2274 * Set the FF and poke the other EMTs.
2275 */
2276 VM_FF_SET(pVM, VM_FF_EMT_RENDEZVOUS);
2277 VMR3NotifyGlobalFFU(pVM->pUVM, VMNOTIFYFF_FLAGS_POKE);
2278
2279 /*
2280 * Do the same ourselves.
2281 */
2282 VBOXSTRICTRC rcStrict2 = vmmR3EmtRendezvousCommon(pVM, pVCpu, true /* fIsCaller */, fFlags, pfnRendezvous, pvUser);
2283
2284 /*
2285 * The caller waits for the other EMTs to be done and return before doing
2286 * the cleanup. This makes away with wakeup / reset races we would otherwise
2287 * risk in the multiple release event semaphore code (hEvtRendezvousDoneCaller).
2288 */
2289 for (;;)
2290 {
2291 rc = RTSemEventWait(pVM->vmm.s.hEvtRendezvousDoneCaller, RT_INDEFINITE_WAIT);
2292 AssertLogRelRC(rc);
2293 if (!pVM->vmm.s.fRendezvousRecursion)
2294 break;
2295 rcStrict2 = vmmR3EmtRendezvousCommonRecursion(pVM, pVCpu, rcStrict2);
2296 }
2297
2298 /*
2299 * Get the return code and clean up a little bit.
2300 */
2301 VBOXSTRICTRC rcStrict3 = pVM->vmm.s.i32RendezvousStatus;
2302 ASMAtomicWriteNullPtr((void * volatile *)&pVM->vmm.s.pfnRendezvous);
2303
2304 ASMAtomicWriteU32(&pVM->vmm.s.u32RendezvousLock, 0);
2305 pVCpu->vmm.s.fInRendezvous = false;
2306
2307 /*
2308 * Merge rcStrict, rcStrict2 and rcStrict3.
2309 */
2310 AssertRC(VBOXSTRICTRC_VAL(rcStrict));
2311 AssertRC(VBOXSTRICTRC_VAL(rcStrict2));
2312 if ( rcStrict2 != VINF_SUCCESS
2313 && ( rcStrict == VINF_SUCCESS
2314 || rcStrict > rcStrict2))
2315 rcStrict = rcStrict2;
2316 if ( rcStrict3 != VINF_SUCCESS
2317 && ( rcStrict == VINF_SUCCESS
2318 || rcStrict > rcStrict3))
2319 rcStrict = rcStrict3;
2320 Log(("VMMR3EmtRendezvous: %#x EMT#%u returns %Rrc\n", fFlags, pVCpu->idCpu, VBOXSTRICTRC_VAL(rcStrict)));
2321 }
2322
2323 AssertLogRelMsgReturn( rcStrict <= VINF_SUCCESS
2324 || (rcStrict >= VINF_EM_FIRST && rcStrict <= VINF_EM_LAST),
2325 ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)),
2326 VERR_IPE_UNEXPECTED_INFO_STATUS);
2327 return VBOXSTRICTRC_VAL(rcStrict);
2328}
2329
2330
2331/**
2332 * Read from the ring 0 jump buffer stack.
2333 *
2334 * @returns VBox status code.
2335 *
2336 * @param pVM The cross context VM structure.
2337 * @param idCpu The ID of the source CPU context (for the address).
2338 * @param R0Addr Where to start reading.
2339 * @param pvBuf Where to store the data we've read.
2340 * @param cbRead The number of bytes to read.
2341 */
2342VMMR3_INT_DECL(int) VMMR3ReadR0Stack(PVM pVM, VMCPUID idCpu, RTHCUINTPTR R0Addr, void *pvBuf, size_t cbRead)
2343{
2344 PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
2345 AssertReturn(pVCpu, VERR_INVALID_PARAMETER);
2346
2347#ifdef VMM_R0_SWITCH_STACK
2348 RTHCUINTPTR off = R0Addr - MMHyperCCToR0(pVM, pVCpu->vmm.s.pbEMTStackR3);
2349#else
2350 RTHCUINTPTR off = pVCpu->vmm.s.CallRing3JmpBufR0.cbSavedStack - (pVCpu->vmm.s.CallRing3JmpBufR0.SpCheck - R0Addr);
2351#endif
2352 if ( off > VMM_STACK_SIZE
2353 || off + cbRead >= VMM_STACK_SIZE)
2354 return VERR_INVALID_POINTER;
2355
2356 memcpy(pvBuf, &pVCpu->vmm.s.pbEMTStackR3[off], cbRead);
2357 return VINF_SUCCESS;
2358}
2359
2360#ifdef VBOX_WITH_RAW_MODE
2361
2362/**
2363 * Calls a RC function.
2364 *
2365 * @param pVM The cross context VM structure.
2366 * @param RCPtrEntry The address of the RC function.
2367 * @param cArgs The number of arguments in the ....
2368 * @param ... Arguments to the function.
2369 */
2370VMMR3DECL(int) VMMR3CallRC(PVM pVM, RTRCPTR RCPtrEntry, unsigned cArgs, ...)
2371{
2372 va_list args;
2373 va_start(args, cArgs);
2374 int rc = VMMR3CallRCV(pVM, RCPtrEntry, cArgs, args);
2375 va_end(args);
2376 return rc;
2377}
2378
2379
2380/**
2381 * Calls a RC function.
2382 *
2383 * @param pVM The cross context VM structure.
2384 * @param RCPtrEntry The address of the RC function.
2385 * @param cArgs The number of arguments in the ....
2386 * @param args Arguments to the function.
2387 */
2388VMMR3DECL(int) VMMR3CallRCV(PVM pVM, RTRCPTR RCPtrEntry, unsigned cArgs, va_list args)
2389{
2390 /* Raw mode implies 1 VCPU. */
2391 AssertReturn(pVM->cCpus == 1, VERR_RAW_MODE_INVALID_SMP);
2392 PVMCPU pVCpu = &pVM->aCpus[0];
2393
2394 Log2(("VMMR3CallGCV: RCPtrEntry=%RRv cArgs=%d\n", RCPtrEntry, cArgs));
2395
2396 /*
2397 * Setup the call frame using the trampoline.
2398 */
2399 CPUMSetHyperState(pVCpu,
2400 pVM->vmm.s.pfnCallTrampolineRC, /* eip */
2401 pVCpu->vmm.s.pbEMTStackBottomRC - cArgs * sizeof(RTGCUINTPTR32), /* esp */
2402 RCPtrEntry, /* eax */
2403 cArgs /* edx */
2404 );
2405
2406#if 0
2407 memset(pVCpu->vmm.s.pbEMTStackR3, 0xaa, VMM_STACK_SIZE); /* Clear the stack. */
2408#endif
2409 PRTGCUINTPTR32 pFrame = (PRTGCUINTPTR32)(pVCpu->vmm.s.pbEMTStackR3 + VMM_STACK_SIZE) - cArgs;
2410 int i = cArgs;
2411 while (i-- > 0)
2412 *pFrame++ = va_arg(args, RTGCUINTPTR32);
2413
2414 CPUMPushHyper(pVCpu, cArgs * sizeof(RTGCUINTPTR32)); /* stack frame size */
2415 CPUMPushHyper(pVCpu, RCPtrEntry); /* what to call */
2416
2417 /*
2418 * We hide log flushes (outer) and hypervisor interrupts (inner).
2419 */
2420 for (;;)
2421 {
2422 int rc;
2423 Assert(CPUMGetHyperCR3(pVCpu) && CPUMGetHyperCR3(pVCpu) == PGMGetHyperCR3(pVCpu));
2424 do
2425 {
2426#ifdef NO_SUPCALLR0VMM
2427 rc = VERR_GENERAL_FAILURE;
2428#else
2429 rc = SUPR3CallVMMR0Fast(pVM->pVMR0, VMMR0_DO_RAW_RUN, 0);
2430 if (RT_LIKELY(rc == VINF_SUCCESS))
2431 rc = pVCpu->vmm.s.iLastGZRc;
2432#endif
2433 } while (rc == VINF_EM_RAW_INTERRUPT_HYPER);
2434
2435 /*
2436 * Flush the loggers.
2437 */
2438#ifdef LOG_ENABLED
2439 PRTLOGGERRC pLogger = pVM->vmm.s.pRCLoggerR3;
2440 if ( pLogger
2441 && pLogger->offScratch > 0)
2442 RTLogFlushRC(NULL, pLogger);
2443#endif
2444#ifdef VBOX_WITH_RC_RELEASE_LOGGING
2445 PRTLOGGERRC pRelLogger = pVM->vmm.s.pRCRelLoggerR3;
2446 if (RT_UNLIKELY(pRelLogger && pRelLogger->offScratch > 0))
2447 RTLogFlushRC(RTLogRelGetDefaultInstance(), pRelLogger);
2448#endif
2449 if (rc == VERR_TRPM_PANIC || rc == VERR_TRPM_DONT_PANIC)
2450 VMMR3FatalDump(pVM, pVCpu, rc);
2451 if (rc != VINF_VMM_CALL_HOST)
2452 {
2453 Log2(("VMMR3CallGCV: returns %Rrc (cs:eip=%04x:%08x)\n", rc, CPUMGetGuestCS(pVCpu), CPUMGetGuestEIP(pVCpu)));
2454 return rc;
2455 }
2456 rc = vmmR3ServiceCallRing3Request(pVM, pVCpu);
2457 if (RT_FAILURE(rc))
2458 return rc;
2459 }
2460}
2461
2462#endif /* VBOX_WITH_RAW_MODE */
2463
2464/**
2465 * Wrapper for SUPR3CallVMMR0Ex which will deal with VINF_VMM_CALL_HOST returns.
2466 *
2467 * @returns VBox status code.
2468 * @param pVM The cross context VM structure.
2469 * @param uOperation Operation to execute.
2470 * @param u64Arg Constant argument.
2471 * @param pReqHdr Pointer to a request header. See SUPR3CallVMMR0Ex for
2472 * details.
2473 */
2474VMMR3DECL(int) VMMR3CallR0(PVM pVM, uint32_t uOperation, uint64_t u64Arg, PSUPVMMR0REQHDR pReqHdr)
2475{
2476 PVMCPU pVCpu = VMMGetCpu(pVM);
2477 AssertReturn(pVCpu, VERR_VM_THREAD_NOT_EMT);
2478
2479 /*
2480 * Call Ring-0 entry with init code.
2481 */
2482 int rc;
2483 for (;;)
2484 {
2485#ifdef NO_SUPCALLR0VMM
2486 rc = VERR_GENERAL_FAILURE;
2487#else
2488 rc = SUPR3CallVMMR0Ex(pVM->pVMR0, pVCpu->idCpu, uOperation, u64Arg, pReqHdr);
2489#endif
2490 /*
2491 * Flush the logs.
2492 */
2493#ifdef LOG_ENABLED
2494 if ( pVCpu->vmm.s.pR0LoggerR3
2495 && pVCpu->vmm.s.pR0LoggerR3->Logger.offScratch > 0)
2496 RTLogFlushR0(NULL, &pVCpu->vmm.s.pR0LoggerR3->Logger);
2497#endif
2498 if (rc != VINF_VMM_CALL_HOST)
2499 break;
2500 rc = vmmR3ServiceCallRing3Request(pVM, pVCpu);
2501 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
2502 break;
2503 /* Resume R0 */
2504 }
2505
2506 AssertLogRelMsgReturn(rc == VINF_SUCCESS || RT_FAILURE(rc),
2507 ("uOperation=%u rc=%Rrc\n", uOperation, rc),
2508 VERR_IPE_UNEXPECTED_INFO_STATUS);
2509 return rc;
2510}
2511
2512
2513#ifdef VBOX_WITH_RAW_MODE
2514/**
2515 * Resumes executing hypervisor code when interrupted by a queue flush or a
2516 * debug event.
2517 *
2518 * @returns VBox status code.
2519 * @param pVM The cross context VM structure.
2520 * @param pVCpu The cross context virtual CPU structure.
2521 */
2522VMMR3DECL(int) VMMR3ResumeHyper(PVM pVM, PVMCPU pVCpu)
2523{
2524 Log(("VMMR3ResumeHyper: eip=%RRv esp=%RRv\n", CPUMGetHyperEIP(pVCpu), CPUMGetHyperESP(pVCpu)));
2525 AssertReturn(pVM->cCpus == 1, VERR_RAW_MODE_INVALID_SMP);
2526
2527 /*
2528 * We hide log flushes (outer) and hypervisor interrupts (inner).
2529 */
2530 for (;;)
2531 {
2532 int rc;
2533 Assert(CPUMGetHyperCR3(pVCpu) && CPUMGetHyperCR3(pVCpu) == PGMGetHyperCR3(pVCpu));
2534 do
2535 {
2536# ifdef NO_SUPCALLR0VMM
2537 rc = VERR_GENERAL_FAILURE;
2538# else
2539 rc = SUPR3CallVMMR0Fast(pVM->pVMR0, VMMR0_DO_RAW_RUN, 0);
2540 if (RT_LIKELY(rc == VINF_SUCCESS))
2541 rc = pVCpu->vmm.s.iLastGZRc;
2542# endif
2543 } while (rc == VINF_EM_RAW_INTERRUPT_HYPER);
2544
2545 /*
2546 * Flush the loggers.
2547 */
2548# ifdef LOG_ENABLED
2549 PRTLOGGERRC pLogger = pVM->vmm.s.pRCLoggerR3;
2550 if ( pLogger
2551 && pLogger->offScratch > 0)
2552 RTLogFlushRC(NULL, pLogger);
2553# endif
2554# ifdef VBOX_WITH_RC_RELEASE_LOGGING
2555 PRTLOGGERRC pRelLogger = pVM->vmm.s.pRCRelLoggerR3;
2556 if (RT_UNLIKELY(pRelLogger && pRelLogger->offScratch > 0))
2557 RTLogFlushRC(RTLogRelGetDefaultInstance(), pRelLogger);
2558# endif
2559 if (rc == VERR_TRPM_PANIC || rc == VERR_TRPM_DONT_PANIC)
2560 VMMR3FatalDump(pVM, pVCpu, rc);
2561 if (rc != VINF_VMM_CALL_HOST)
2562 {
2563 Log(("VMMR3ResumeHyper: returns %Rrc\n", rc));
2564 return rc;
2565 }
2566 rc = vmmR3ServiceCallRing3Request(pVM, pVCpu);
2567 if (RT_FAILURE(rc))
2568 return rc;
2569 }
2570}
2571#endif /* VBOX_WITH_RAW_MODE */
2572
2573
2574/**
2575 * Service a call to the ring-3 host code.
2576 *
2577 * @returns VBox status code.
2578 * @param pVM The cross context VM structure.
2579 * @param pVCpu The cross context virtual CPU structure.
2580 * @remarks Careful with critsects.
2581 */
2582static int vmmR3ServiceCallRing3Request(PVM pVM, PVMCPU pVCpu)
2583{
2584 /*
2585 * We must also check for pending critsect exits or else we can deadlock
2586 * when entering other critsects here.
2587 */
2588 if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_PDM_CRITSECT))
2589 PDMCritSectBothFF(pVCpu);
2590
2591 switch (pVCpu->vmm.s.enmCallRing3Operation)
2592 {
2593 /*
2594 * Acquire a critical section.
2595 */
2596 case VMMCALLRING3_PDM_CRIT_SECT_ENTER:
2597 {
2598 pVCpu->vmm.s.rcCallRing3 = PDMR3CritSectEnterEx((PPDMCRITSECT)(uintptr_t)pVCpu->vmm.s.u64CallRing3Arg,
2599 true /*fCallRing3*/);
2600 break;
2601 }
2602
2603 /*
2604 * Enter a r/w critical section exclusively.
2605 */
2606 case VMMCALLRING3_PDM_CRIT_SECT_RW_ENTER_EXCL:
2607 {
2608 pVCpu->vmm.s.rcCallRing3 = PDMR3CritSectRwEnterExclEx((PPDMCRITSECTRW)(uintptr_t)pVCpu->vmm.s.u64CallRing3Arg,
2609 true /*fCallRing3*/);
2610 break;
2611 }
2612
2613 /*
2614 * Enter a r/w critical section shared.
2615 */
2616 case VMMCALLRING3_PDM_CRIT_SECT_RW_ENTER_SHARED:
2617 {
2618 pVCpu->vmm.s.rcCallRing3 = PDMR3CritSectRwEnterSharedEx((PPDMCRITSECTRW)(uintptr_t)pVCpu->vmm.s.u64CallRing3Arg,
2619 true /*fCallRing3*/);
2620 break;
2621 }
2622
2623 /*
2624 * Acquire the PDM lock.
2625 */
2626 case VMMCALLRING3_PDM_LOCK:
2627 {
2628 pVCpu->vmm.s.rcCallRing3 = PDMR3LockCall(pVM);
2629 break;
2630 }
2631
2632 /*
2633 * Grow the PGM pool.
2634 */
2635 case VMMCALLRING3_PGM_POOL_GROW:
2636 {
2637 pVCpu->vmm.s.rcCallRing3 = PGMR3PoolGrow(pVM);
2638 break;
2639 }
2640
2641 /*
2642 * Maps an page allocation chunk into ring-3 so ring-0 can use it.
2643 */
2644 case VMMCALLRING3_PGM_MAP_CHUNK:
2645 {
2646 pVCpu->vmm.s.rcCallRing3 = PGMR3PhysChunkMap(pVM, pVCpu->vmm.s.u64CallRing3Arg);
2647 break;
2648 }
2649
2650 /*
2651 * Allocates more handy pages.
2652 */
2653 case VMMCALLRING3_PGM_ALLOCATE_HANDY_PAGES:
2654 {
2655 pVCpu->vmm.s.rcCallRing3 = PGMR3PhysAllocateHandyPages(pVM);
2656 break;
2657 }
2658
2659 /*
2660 * Allocates a large page.
2661 */
2662 case VMMCALLRING3_PGM_ALLOCATE_LARGE_HANDY_PAGE:
2663 {
2664 pVCpu->vmm.s.rcCallRing3 = PGMR3PhysAllocateLargeHandyPage(pVM, pVCpu->vmm.s.u64CallRing3Arg);
2665 break;
2666 }
2667
2668 /*
2669 * Acquire the PGM lock.
2670 */
2671 case VMMCALLRING3_PGM_LOCK:
2672 {
2673 pVCpu->vmm.s.rcCallRing3 = PGMR3LockCall(pVM);
2674 break;
2675 }
2676
2677 /*
2678 * Acquire the MM hypervisor heap lock.
2679 */
2680 case VMMCALLRING3_MMHYPER_LOCK:
2681 {
2682 pVCpu->vmm.s.rcCallRing3 = MMR3LockCall(pVM);
2683 break;
2684 }
2685
2686#ifdef VBOX_WITH_REM
2687 /*
2688 * Flush REM handler notifications.
2689 */
2690 case VMMCALLRING3_REM_REPLAY_HANDLER_NOTIFICATIONS:
2691 {
2692 REMR3ReplayHandlerNotifications(pVM);
2693 pVCpu->vmm.s.rcCallRing3 = VINF_SUCCESS;
2694 break;
2695 }
2696#endif
2697
2698 /*
2699 * This is a noop. We just take this route to avoid unnecessary
2700 * tests in the loops.
2701 */
2702 case VMMCALLRING3_VMM_LOGGER_FLUSH:
2703 pVCpu->vmm.s.rcCallRing3 = VINF_SUCCESS;
2704 LogAlways(("*FLUSH*\n"));
2705 break;
2706
2707 /*
2708 * Set the VM error message.
2709 */
2710 case VMMCALLRING3_VM_SET_ERROR:
2711 VMR3SetErrorWorker(pVM);
2712 pVCpu->vmm.s.rcCallRing3 = VINF_SUCCESS;
2713 break;
2714
2715 /*
2716 * Set the VM runtime error message.
2717 */
2718 case VMMCALLRING3_VM_SET_RUNTIME_ERROR:
2719 pVCpu->vmm.s.rcCallRing3 = VMR3SetRuntimeErrorWorker(pVM);
2720 break;
2721
2722 /*
2723 * Signal a ring 0 hypervisor assertion.
2724 * Cancel the longjmp operation that's in progress.
2725 */
2726 case VMMCALLRING3_VM_R0_ASSERTION:
2727 pVCpu->vmm.s.enmCallRing3Operation = VMMCALLRING3_INVALID;
2728 pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call = false;
2729#ifdef RT_ARCH_X86
2730 pVCpu->vmm.s.CallRing3JmpBufR0.eip = 0;
2731#else
2732 pVCpu->vmm.s.CallRing3JmpBufR0.rip = 0;
2733#endif
2734#ifdef VMM_R0_SWITCH_STACK
2735 *(uint64_t *)pVCpu->vmm.s.pbEMTStackR3 = 0; /* clear marker */
2736#endif
2737 LogRel(("%s", pVM->vmm.s.szRing0AssertMsg1));
2738 LogRel(("%s", pVM->vmm.s.szRing0AssertMsg2));
2739 return VERR_VMM_RING0_ASSERTION;
2740
2741 /*
2742 * A forced switch to ring 0 for preemption purposes.
2743 */
2744 case VMMCALLRING3_VM_R0_PREEMPT:
2745 pVCpu->vmm.s.rcCallRing3 = VINF_SUCCESS;
2746 break;
2747
2748 case VMMCALLRING3_FTM_SET_CHECKPOINT:
2749 pVCpu->vmm.s.rcCallRing3 = FTMR3SetCheckpoint(pVM, (FTMCHECKPOINTTYPE)pVCpu->vmm.s.u64CallRing3Arg);
2750 break;
2751
2752 default:
2753 AssertMsgFailed(("enmCallRing3Operation=%d\n", pVCpu->vmm.s.enmCallRing3Operation));
2754 return VERR_VMM_UNKNOWN_RING3_CALL;
2755 }
2756
2757 pVCpu->vmm.s.enmCallRing3Operation = VMMCALLRING3_INVALID;
2758 return VINF_SUCCESS;
2759}
2760
2761
2762/**
2763 * Displays the Force action Flags.
2764 *
2765 * @param pVM The cross context VM structure.
2766 * @param pHlp The output helpers.
2767 * @param pszArgs The additional arguments (ignored).
2768 */
2769static DECLCALLBACK(void) vmmR3InfoFF(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2770{
2771 int c;
2772 uint32_t f;
2773 NOREF(pszArgs);
2774
2775#define PRINT_FLAG(prf,flag) do { \
2776 if (f & (prf##flag)) \
2777 { \
2778 static const char *s_psz = #flag; \
2779 if (!(c % 6)) \
2780 pHlp->pfnPrintf(pHlp, "%s\n %s", c ? "," : "", s_psz); \
2781 else \
2782 pHlp->pfnPrintf(pHlp, ", %s", s_psz); \
2783 c++; \
2784 f &= ~(prf##flag); \
2785 } \
2786 } while (0)
2787
2788#define PRINT_GROUP(prf,grp,sfx) do { \
2789 if (f & (prf##grp##sfx)) \
2790 { \
2791 static const char *s_psz = #grp; \
2792 if (!(c % 5)) \
2793 pHlp->pfnPrintf(pHlp, "%s %s", c ? ",\n" : " Groups:\n", s_psz); \
2794 else \
2795 pHlp->pfnPrintf(pHlp, ", %s", s_psz); \
2796 c++; \
2797 } \
2798 } while (0)
2799
2800 /*
2801 * The global flags.
2802 */
2803 const uint32_t fGlobalForcedActions = pVM->fGlobalForcedActions;
2804 pHlp->pfnPrintf(pHlp, "Global FFs: %#RX32", fGlobalForcedActions);
2805
2806 /* show the flag mnemonics */
2807 c = 0;
2808 f = fGlobalForcedActions;
2809 PRINT_FLAG(VM_FF_,TM_VIRTUAL_SYNC);
2810 PRINT_FLAG(VM_FF_,PDM_QUEUES);
2811 PRINT_FLAG(VM_FF_,PDM_DMA);
2812 PRINT_FLAG(VM_FF_,DBGF);
2813 PRINT_FLAG(VM_FF_,REQUEST);
2814 PRINT_FLAG(VM_FF_,CHECK_VM_STATE);
2815 PRINT_FLAG(VM_FF_,RESET);
2816 PRINT_FLAG(VM_FF_,EMT_RENDEZVOUS);
2817 PRINT_FLAG(VM_FF_,PGM_NEED_HANDY_PAGES);
2818 PRINT_FLAG(VM_FF_,PGM_NO_MEMORY);
2819 PRINT_FLAG(VM_FF_,PGM_POOL_FLUSH_PENDING);
2820 PRINT_FLAG(VM_FF_,REM_HANDLER_NOTIFY);
2821 PRINT_FLAG(VM_FF_,DEBUG_SUSPEND);
2822 if (f)
2823 pHlp->pfnPrintf(pHlp, "%s\n Unknown bits: %#RX32\n", c ? "," : "", f);
2824 else
2825 pHlp->pfnPrintf(pHlp, "\n");
2826
2827 /* the groups */
2828 c = 0;
2829 f = fGlobalForcedActions;
2830 PRINT_GROUP(VM_FF_,EXTERNAL_SUSPENDED,_MASK);
2831 PRINT_GROUP(VM_FF_,EXTERNAL_HALTED,_MASK);
2832 PRINT_GROUP(VM_FF_,HIGH_PRIORITY_PRE,_MASK);
2833 PRINT_GROUP(VM_FF_,HIGH_PRIORITY_PRE_RAW,_MASK);
2834 PRINT_GROUP(VM_FF_,HIGH_PRIORITY_POST,_MASK);
2835 PRINT_GROUP(VM_FF_,NORMAL_PRIORITY_POST,_MASK);
2836 PRINT_GROUP(VM_FF_,NORMAL_PRIORITY,_MASK);
2837 PRINT_GROUP(VM_FF_,ALL_REM,_MASK);
2838 if (c)
2839 pHlp->pfnPrintf(pHlp, "\n");
2840
2841 /*
2842 * Per CPU flags.
2843 */
2844 for (VMCPUID i = 0; i < pVM->cCpus; i++)
2845 {
2846 const uint32_t fLocalForcedActions = pVM->aCpus[i].fLocalForcedActions;
2847 pHlp->pfnPrintf(pHlp, "CPU %u FFs: %#RX32", i, fLocalForcedActions);
2848
2849 /* show the flag mnemonics */
2850 c = 0;
2851 f = fLocalForcedActions;
2852 PRINT_FLAG(VMCPU_FF_,INTERRUPT_APIC);
2853 PRINT_FLAG(VMCPU_FF_,INTERRUPT_PIC);
2854 PRINT_FLAG(VMCPU_FF_,TIMER);
2855 PRINT_FLAG(VMCPU_FF_,INTERRUPT_NMI);
2856 PRINT_FLAG(VMCPU_FF_,INTERRUPT_SMI);
2857 PRINT_FLAG(VMCPU_FF_,PDM_CRITSECT);
2858 PRINT_FLAG(VMCPU_FF_,UNHALT);
2859 PRINT_FLAG(VMCPU_FF_,IEM);
2860 PRINT_FLAG(VMCPU_FF_,UPDATE_APIC);
2861 PRINT_FLAG(VMCPU_FF_,DBGF);
2862 PRINT_FLAG(VMCPU_FF_,REQUEST);
2863 PRINT_FLAG(VMCPU_FF_,HM_UPDATE_CR3);
2864 PRINT_FLAG(VMCPU_FF_,HM_UPDATE_PAE_PDPES);
2865 PRINT_FLAG(VMCPU_FF_,PGM_SYNC_CR3);
2866 PRINT_FLAG(VMCPU_FF_,PGM_SYNC_CR3_NON_GLOBAL);
2867 PRINT_FLAG(VMCPU_FF_,TLB_FLUSH);
2868 PRINT_FLAG(VMCPU_FF_,INHIBIT_INTERRUPTS);
2869 PRINT_FLAG(VMCPU_FF_,BLOCK_NMIS);
2870 PRINT_FLAG(VMCPU_FF_,TO_R3);
2871 PRINT_FLAG(VMCPU_FF_,IOM);
2872#ifdef VBOX_WITH_RAW_MODE
2873 PRINT_FLAG(VMCPU_FF_,TRPM_SYNC_IDT);
2874 PRINT_FLAG(VMCPU_FF_,SELM_SYNC_TSS);
2875 PRINT_FLAG(VMCPU_FF_,SELM_SYNC_GDT);
2876 PRINT_FLAG(VMCPU_FF_,SELM_SYNC_LDT);
2877 PRINT_FLAG(VMCPU_FF_,CSAM_SCAN_PAGE);
2878 PRINT_FLAG(VMCPU_FF_,CSAM_PENDING_ACTION);
2879 PRINT_FLAG(VMCPU_FF_,CPUM);
2880#endif
2881 if (f)
2882 pHlp->pfnPrintf(pHlp, "%s\n Unknown bits: %#RX32\n", c ? "," : "", f);
2883 else
2884 pHlp->pfnPrintf(pHlp, "\n");
2885
2886 if (fLocalForcedActions & VMCPU_FF_INHIBIT_INTERRUPTS)
2887 pHlp->pfnPrintf(pHlp, " intr inhibit RIP: %RGp\n", EMGetInhibitInterruptsPC(&pVM->aCpus[i]));
2888
2889 /* the groups */
2890 c = 0;
2891 f = fLocalForcedActions;
2892 PRINT_GROUP(VMCPU_FF_,EXTERNAL_SUSPENDED,_MASK);
2893 PRINT_GROUP(VMCPU_FF_,EXTERNAL_HALTED,_MASK);
2894 PRINT_GROUP(VMCPU_FF_,HIGH_PRIORITY_PRE,_MASK);
2895 PRINT_GROUP(VMCPU_FF_,HIGH_PRIORITY_PRE_RAW,_MASK);
2896 PRINT_GROUP(VMCPU_FF_,HIGH_PRIORITY_POST,_MASK);
2897 PRINT_GROUP(VMCPU_FF_,NORMAL_PRIORITY_POST,_MASK);
2898 PRINT_GROUP(VMCPU_FF_,NORMAL_PRIORITY,_MASK);
2899 PRINT_GROUP(VMCPU_FF_,RESUME_GUEST,_MASK);
2900 PRINT_GROUP(VMCPU_FF_,HM_TO_R3,_MASK);
2901 PRINT_GROUP(VMCPU_FF_,ALL_REM,_MASK);
2902 if (c)
2903 pHlp->pfnPrintf(pHlp, "\n");
2904 }
2905
2906#undef PRINT_FLAG
2907#undef PRINT_GROUP
2908}
2909
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