VirtualBox

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

Last change on this file since 102869 was 100101, checked in by vboxsync, 18 months ago

VMM: Add VMMR3CpuOn() for ARMv8 instead of using the VMMR3SendStartupIpi() API as the code needs to convey the start address where to execute code and a guest defined context ID, bugref:10454

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 108.1 KB
Line 
1/* $Id: VMM.cpp 100101 2023-06-07 17:52:38Z vboxsync $ */
2/** @file
3 * VMM - The Virtual Machine Monitor Core.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28//#define NO_SUPCALLR0VMM
29
30/** @page pg_vmm VMM - The Virtual Machine Monitor
31 *
32 * The VMM component is two things at the moment, it's a component doing a few
33 * management and routing tasks, and it's the whole virtual machine monitor
34 * thing. For hysterical reasons, it is not doing all the management that one
35 * would expect, this is instead done by @ref pg_vm. We'll address this
36 * misdesign eventually, maybe.
37 *
38 * VMM is made up of these components:
39 * - @subpage pg_cfgm
40 * - @subpage pg_cpum
41 * - @subpage pg_dbgf
42 * - @subpage pg_em
43 * - @subpage pg_gim
44 * - @subpage pg_gmm
45 * - @subpage pg_gvmm
46 * - @subpage pg_hm
47 * - @subpage pg_iem
48 * - @subpage pg_iom
49 * - @subpage pg_mm
50 * - @subpage pg_nem
51 * - @subpage pg_pdm
52 * - @subpage pg_pgm
53 * - @subpage pg_selm
54 * - @subpage pg_ssm
55 * - @subpage pg_stam
56 * - @subpage pg_tm
57 * - @subpage pg_trpm
58 * - @subpage pg_vm
59 *
60 *
61 * @see @ref grp_vmm @ref grp_vm @subpage pg_vmm_guideline @subpage pg_raw
62 *
63 *
64 * @section sec_vmmstate VMM State
65 *
66 * @image html VM_Statechart_Diagram.gif
67 *
68 * To be written.
69 *
70 *
71 * @subsection subsec_vmm_init VMM Initialization
72 *
73 * To be written.
74 *
75 *
76 * @subsection subsec_vmm_term VMM Termination
77 *
78 * To be written.
79 *
80 *
81 * @section sec_vmm_limits VMM Limits
82 *
83 * There are various resource limits imposed by the VMM and it's
84 * sub-components. We'll list some of them here.
85 *
86 * On 64-bit hosts:
87 * - Max 8191 VMs. Imposed by GVMM's handle allocation (GVMM_MAX_HANDLES),
88 * can be increased up to 64K - 1.
89 * - Max 16TB - 64KB of the host memory can be used for backing VM RAM and
90 * ROM pages. The limit is imposed by the 32-bit page ID used by GMM.
91 * - A VM can be assigned all the memory we can use (16TB), however, the
92 * Main API will restrict this to 2TB (MM_RAM_MAX_IN_MB).
93 * - Max 32 virtual CPUs (VMM_MAX_CPU_COUNT).
94 *
95 * On 32-bit hosts:
96 * - Max 127 VMs. Imposed by GMM's per page structure.
97 * - Max 64GB - 64KB of the host memory can be used for backing VM RAM and
98 * ROM pages. The limit is imposed by the 28-bit page ID used
99 * internally in GMM. It is also limited by PAE.
100 * - A VM can be assigned all the memory GMM can allocate, however, the
101 * Main API will restrict this to 3584MB (MM_RAM_MAX_IN_MB).
102 * - Max 32 virtual CPUs (VMM_MAX_CPU_COUNT).
103 *
104 */
105
106
107/*********************************************************************************************************************************
108* Header Files *
109*********************************************************************************************************************************/
110#define LOG_GROUP LOG_GROUP_VMM
111#include <VBox/vmm/vmm.h>
112#include <VBox/vmm/vmapi.h>
113#include <VBox/vmm/pgm.h>
114#include <VBox/vmm/cfgm.h>
115#include <VBox/vmm/pdmqueue.h>
116#include <VBox/vmm/pdmcritsect.h>
117#include <VBox/vmm/pdmcritsectrw.h>
118#include <VBox/vmm/pdmapi.h>
119#include <VBox/vmm/cpum.h>
120#include <VBox/vmm/gim.h>
121#include <VBox/vmm/mm.h>
122#include <VBox/vmm/nem.h>
123#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
124# include <VBox/vmm/iem.h>
125#endif
126#include <VBox/vmm/iom.h>
127#include <VBox/vmm/trpm.h>
128#include <VBox/vmm/selm.h>
129#include <VBox/vmm/em.h>
130#include <VBox/sup.h>
131#include <VBox/vmm/dbgf.h>
132#if defined(VBOX_VMM_TARGET_ARMV8)
133# include <VBox/vmm/gic.h>
134#else
135# include <VBox/vmm/apic.h>
136#endif
137#include <VBox/vmm/ssm.h>
138#include <VBox/vmm/tm.h>
139#include "VMMInternal.h"
140#include <VBox/vmm/vmcc.h>
141
142#include <VBox/err.h>
143#include <VBox/param.h>
144#include <VBox/version.h>
145#include <VBox/vmm/hm.h>
146#include <iprt/assert.h>
147#include <iprt/alloc.h>
148#if defined(VBOX_VMM_TARGET_ARMV8)
149# include <iprt/armv8.h>
150#endif
151#include <iprt/asm.h>
152#include <iprt/time.h>
153#include <iprt/semaphore.h>
154#include <iprt/stream.h>
155#include <iprt/string.h>
156#include <iprt/stdarg.h>
157#include <iprt/ctype.h>
158#include <iprt/x86.h>
159
160
161/*********************************************************************************************************************************
162* Defined Constants And Macros *
163*********************************************************************************************************************************/
164/** The saved state version. */
165#define VMM_SAVED_STATE_VERSION 4
166/** The saved state version used by v3.0 and earlier. (Teleportation) */
167#define VMM_SAVED_STATE_VERSION_3_0 3
168
169/** Macro for flushing the ring-0 logging. */
170#define VMM_FLUSH_R0_LOG(a_pVM, a_pVCpu, a_pLogger, a_pR3Logger) \
171 do { \
172 size_t const idxBuf = (a_pLogger)->idxBuf % VMMLOGGER_BUFFER_COUNT; \
173 if ( (a_pLogger)->aBufs[idxBuf].AuxDesc.offBuf == 0 \
174 || (a_pLogger)->aBufs[idxBuf].AuxDesc.fFlushedIndicator) \
175 { /* likely? */ } \
176 else \
177 vmmR3LogReturnFlush(a_pVM, a_pVCpu, a_pLogger, idxBuf, a_pR3Logger); \
178 } while (0)
179
180
181/*********************************************************************************************************************************
182* Internal Functions *
183*********************************************************************************************************************************/
184static void vmmR3InitRegisterStats(PVM pVM);
185static DECLCALLBACK(int) vmmR3Save(PVM pVM, PSSMHANDLE pSSM);
186static DECLCALLBACK(int) vmmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass);
187#if 0 /* pointless when timers doesn't run on EMT */
188static DECLCALLBACK(void) vmmR3YieldEMT(PVM pVM, TMTIMERHANDLE hTimer, void *pvUser);
189#endif
190static VBOXSTRICTRC vmmR3EmtRendezvousCommon(PVM pVM, PVMCPU pVCpu, bool fIsCaller,
191 uint32_t fFlags, PFNVMMEMTRENDEZVOUS pfnRendezvous, void *pvUser);
192static int vmmR3HandleRing0Assert(PVM pVM, PVMCPU pVCpu);
193static FNRTTHREAD vmmR3LogFlusher;
194static void vmmR3LogReturnFlush(PVM pVM, PVMCPU pVCpu, PVMMR3CPULOGGER pShared, size_t idxBuf,
195 PRTLOGGER pDstLogger);
196static DECLCALLBACK(void) vmmR3InfoFF(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
197
198
199
200/**
201 * Initializes the VMM.
202 *
203 * @returns VBox status code.
204 * @param pVM The cross context VM structure.
205 */
206VMMR3_INT_DECL(int) VMMR3Init(PVM pVM)
207{
208 LogFlow(("VMMR3Init\n"));
209
210 /*
211 * Assert alignment, sizes and order.
212 */
213 AssertCompile(sizeof(pVM->vmm.s) <= sizeof(pVM->vmm.padding));
214 AssertCompile(RT_SIZEOFMEMB(VMCPU, vmm.s) <= RT_SIZEOFMEMB(VMCPU, vmm.padding));
215
216 /*
217 * Init basic VM VMM members.
218 */
219 pVM->vmm.s.pahEvtRendezvousEnterOrdered = NULL;
220 pVM->vmm.s.hEvtRendezvousEnterOneByOne = NIL_RTSEMEVENT;
221 pVM->vmm.s.hEvtMulRendezvousEnterAllAtOnce = NIL_RTSEMEVENTMULTI;
222 pVM->vmm.s.hEvtMulRendezvousDone = NIL_RTSEMEVENTMULTI;
223 pVM->vmm.s.hEvtRendezvousDoneCaller = NIL_RTSEMEVENT;
224 pVM->vmm.s.hEvtMulRendezvousRecursionPush = NIL_RTSEMEVENTMULTI;
225 pVM->vmm.s.hEvtMulRendezvousRecursionPop = NIL_RTSEMEVENTMULTI;
226 pVM->vmm.s.hEvtRendezvousRecursionPushCaller = NIL_RTSEMEVENT;
227 pVM->vmm.s.hEvtRendezvousRecursionPopCaller = NIL_RTSEMEVENT;
228 pVM->vmm.s.nsProgramStart = RTTimeProgramStartNanoTS();
229
230#if 0 /* pointless when timers doesn't run on EMT */
231 /** @cfgm{/YieldEMTInterval, uint32_t, 1, UINT32_MAX, 23, ms}
232 * The EMT yield interval. The EMT yielding is a hack we employ to play a
233 * bit nicer with the rest of the system (like for instance the GUI).
234 */
235 int rc = CFGMR3QueryU32Def(CFGMR3GetRoot(pVM), "YieldEMTInterval", &pVM->vmm.s.cYieldEveryMillies,
236 23 /* Value arrived at after experimenting with the grub boot prompt. */);
237 AssertMsgRCReturn(rc, ("Configuration error. Failed to query \"YieldEMTInterval\", rc=%Rrc\n", rc), rc);
238#endif
239
240 /** @cfgm{/VMM/UsePeriodicPreemptionTimers, boolean, true}
241 * Controls whether we employ per-cpu preemption timers to limit the time
242 * spent executing guest code. This option is not available on all
243 * platforms and we will silently ignore this setting then. If we are
244 * running in VT-x mode, we will use the VMX-preemption timer instead of
245 * this one when possible.
246 */
247 PCFGMNODE pCfgVMM = CFGMR3GetChild(CFGMR3GetRoot(pVM), "VMM");
248 int rc = CFGMR3QueryBoolDef(pCfgVMM, "UsePeriodicPreemptionTimers", &pVM->vmm.s.fUsePeriodicPreemptionTimers, true);
249 AssertMsgRCReturn(rc, ("Configuration error. Failed to query \"VMM/UsePeriodicPreemptionTimers\", rc=%Rrc\n", rc), rc);
250
251 /*
252 * Initialize the VMM rendezvous semaphores.
253 */
254 pVM->vmm.s.pahEvtRendezvousEnterOrdered = (PRTSEMEVENT)MMR3HeapAlloc(pVM, MM_TAG_VMM, sizeof(RTSEMEVENT) * pVM->cCpus);
255 if (!pVM->vmm.s.pahEvtRendezvousEnterOrdered)
256 return VERR_NO_MEMORY;
257 for (VMCPUID i = 0; i < pVM->cCpus; i++)
258 pVM->vmm.s.pahEvtRendezvousEnterOrdered[i] = NIL_RTSEMEVENT;
259 for (VMCPUID i = 0; i < pVM->cCpus; i++)
260 {
261 rc = RTSemEventCreate(&pVM->vmm.s.pahEvtRendezvousEnterOrdered[i]);
262 AssertRCReturn(rc, rc);
263 }
264 rc = RTSemEventCreate(&pVM->vmm.s.hEvtRendezvousEnterOneByOne);
265 AssertRCReturn(rc, rc);
266 rc = RTSemEventMultiCreate(&pVM->vmm.s.hEvtMulRendezvousEnterAllAtOnce);
267 AssertRCReturn(rc, rc);
268 rc = RTSemEventMultiCreate(&pVM->vmm.s.hEvtMulRendezvousDone);
269 AssertRCReturn(rc, rc);
270 rc = RTSemEventCreate(&pVM->vmm.s.hEvtRendezvousDoneCaller);
271 AssertRCReturn(rc, rc);
272 rc = RTSemEventMultiCreate(&pVM->vmm.s.hEvtMulRendezvousRecursionPush);
273 AssertRCReturn(rc, rc);
274 rc = RTSemEventMultiCreate(&pVM->vmm.s.hEvtMulRendezvousRecursionPop);
275 AssertRCReturn(rc, rc);
276 rc = RTSemEventCreate(&pVM->vmm.s.hEvtRendezvousRecursionPushCaller);
277 AssertRCReturn(rc, rc);
278 rc = RTSemEventCreate(&pVM->vmm.s.hEvtRendezvousRecursionPopCaller);
279 AssertRCReturn(rc, rc);
280
281 /*
282 * Register the saved state data unit.
283 */
284 rc = SSMR3RegisterInternal(pVM, "vmm", 1, VMM_SAVED_STATE_VERSION, VMM_STACK_SIZE + sizeof(RTGCPTR),
285 NULL, NULL, NULL,
286 NULL, vmmR3Save, NULL,
287 NULL, vmmR3Load, NULL);
288 if (RT_FAILURE(rc))
289 return rc;
290
291 /*
292 * Register the Ring-0 VM handle with the session for fast ioctl calls.
293 */
294 bool const fDriverless = SUPR3IsDriverless();
295 if (!fDriverless)
296 {
297 rc = SUPR3SetVMForFastIOCtl(VMCC_GET_VMR0_FOR_CALL(pVM));
298 if (RT_FAILURE(rc))
299 return rc;
300 }
301
302#ifdef VBOX_WITH_NMI
303 /*
304 * Allocate mapping for the host APIC.
305 */
306 rc = MMR3HyperReserve(pVM, HOST_PAGE_SIZE, "Host APIC", &pVM->vmm.s.GCPtrApicBase);
307 AssertRC(rc);
308#endif
309 if (RT_SUCCESS(rc))
310 {
311 /*
312 * Start the log flusher thread.
313 */
314 if (!fDriverless)
315 rc = RTThreadCreate(&pVM->vmm.s.hLogFlusherThread, vmmR3LogFlusher, pVM, 0 /*cbStack*/,
316 RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE, "R0LogWrk");
317 if (RT_SUCCESS(rc))
318 {
319
320 /*
321 * Debug info and statistics.
322 */
323 DBGFR3InfoRegisterInternal(pVM, "fflags", "Displays the current Forced actions Flags.", vmmR3InfoFF);
324 vmmR3InitRegisterStats(pVM);
325 vmmInitFormatTypes();
326
327 return VINF_SUCCESS;
328 }
329 }
330 /** @todo Need failure cleanup? */
331
332 return rc;
333}
334
335
336/**
337 * VMMR3Init worker that register the statistics with STAM.
338 *
339 * @param pVM The cross context VM structure.
340 */
341static void vmmR3InitRegisterStats(PVM pVM)
342{
343 RT_NOREF_PV(pVM);
344
345 /* Nothing to do here in driverless mode. */
346 if (SUPR3IsDriverless())
347 return;
348
349 /*
350 * Statistics.
351 */
352 STAM_REG(pVM, &pVM->vmm.s.StatRunGC, STAMTYPE_COUNTER, "/VMM/RunGC", STAMUNIT_OCCURENCES, "Number of context switches.");
353 STAM_REG(pVM, &pVM->vmm.s.StatRZRetNormal, STAMTYPE_COUNTER, "/VMM/RZRet/Normal", STAMUNIT_OCCURENCES, "Number of VINF_SUCCESS returns.");
354 STAM_REG(pVM, &pVM->vmm.s.StatRZRetInterrupt, STAMTYPE_COUNTER, "/VMM/RZRet/Interrupt", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_INTERRUPT returns.");
355 STAM_REG(pVM, &pVM->vmm.s.StatRZRetInterruptHyper, STAMTYPE_COUNTER, "/VMM/RZRet/InterruptHyper", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_INTERRUPT_HYPER returns.");
356 STAM_REG(pVM, &pVM->vmm.s.StatRZRetGuestTrap, STAMTYPE_COUNTER, "/VMM/RZRet/GuestTrap", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_GUEST_TRAP returns.");
357 STAM_REG(pVM, &pVM->vmm.s.StatRZRetRingSwitch, STAMTYPE_COUNTER, "/VMM/RZRet/RingSwitch", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_RING_SWITCH returns.");
358 STAM_REG(pVM, &pVM->vmm.s.StatRZRetRingSwitchInt, STAMTYPE_COUNTER, "/VMM/RZRet/RingSwitchInt", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_RING_SWITCH_INT returns.");
359 STAM_REG(pVM, &pVM->vmm.s.StatRZRetStaleSelector, STAMTYPE_COUNTER, "/VMM/RZRet/StaleSelector", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_STALE_SELECTOR returns.");
360 STAM_REG(pVM, &pVM->vmm.s.StatRZRetIRETTrap, STAMTYPE_COUNTER, "/VMM/RZRet/IRETTrap", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_IRET_TRAP returns.");
361 STAM_REG(pVM, &pVM->vmm.s.StatRZRetEmulate, STAMTYPE_COUNTER, "/VMM/RZRet/Emulate", STAMUNIT_OCCURENCES, "Number of VINF_EM_EXECUTE_INSTRUCTION returns.");
362 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPatchEmulate, STAMTYPE_COUNTER, "/VMM/RZRet/PatchEmulate", STAMUNIT_OCCURENCES, "Number of VINF_PATCH_EMULATE_INSTR returns.");
363 STAM_REG(pVM, &pVM->vmm.s.StatRZRetIORead, STAMTYPE_COUNTER, "/VMM/RZRet/IORead", STAMUNIT_OCCURENCES, "Number of VINF_IOM_R3_IOPORT_READ returns.");
364 STAM_REG(pVM, &pVM->vmm.s.StatRZRetIOWrite, STAMTYPE_COUNTER, "/VMM/RZRet/IOWrite", STAMUNIT_OCCURENCES, "Number of VINF_IOM_R3_IOPORT_WRITE returns.");
365 STAM_REG(pVM, &pVM->vmm.s.StatRZRetIOCommitWrite, STAMTYPE_COUNTER, "/VMM/RZRet/IOCommitWrite", STAMUNIT_OCCURENCES, "Number of VINF_IOM_R3_IOPORT_COMMIT_WRITE returns.");
366 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMMIORead, STAMTYPE_COUNTER, "/VMM/RZRet/MMIORead", STAMUNIT_OCCURENCES, "Number of VINF_IOM_R3_MMIO_READ returns.");
367 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMMIOWrite, STAMTYPE_COUNTER, "/VMM/RZRet/MMIOWrite", STAMUNIT_OCCURENCES, "Number of VINF_IOM_R3_MMIO_WRITE returns.");
368 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMMIOCommitWrite, STAMTYPE_COUNTER, "/VMM/RZRet/MMIOCommitWrite", STAMUNIT_OCCURENCES, "Number of VINF_IOM_R3_MMIO_COMMIT_WRITE returns.");
369 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMMIOReadWrite, STAMTYPE_COUNTER, "/VMM/RZRet/MMIOReadWrite", STAMUNIT_OCCURENCES, "Number of VINF_IOM_R3_MMIO_READ_WRITE returns.");
370 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMMIOPatchRead, STAMTYPE_COUNTER, "/VMM/RZRet/MMIOPatchRead", STAMUNIT_OCCURENCES, "Number of VINF_IOM_HC_MMIO_PATCH_READ returns.");
371 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMMIOPatchWrite, STAMTYPE_COUNTER, "/VMM/RZRet/MMIOPatchWrite", STAMUNIT_OCCURENCES, "Number of VINF_IOM_HC_MMIO_PATCH_WRITE returns.");
372 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMSRRead, STAMTYPE_COUNTER, "/VMM/RZRet/MSRRead", STAMUNIT_OCCURENCES, "Number of VINF_CPUM_R3_MSR_READ returns.");
373 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMSRWrite, STAMTYPE_COUNTER, "/VMM/RZRet/MSRWrite", STAMUNIT_OCCURENCES, "Number of VINF_CPUM_R3_MSR_WRITE returns.");
374 STAM_REG(pVM, &pVM->vmm.s.StatRZRetLDTFault, STAMTYPE_COUNTER, "/VMM/RZRet/LDTFault", STAMUNIT_OCCURENCES, "Number of VINF_EM_EXECUTE_INSTRUCTION_GDT_FAULT returns.");
375 STAM_REG(pVM, &pVM->vmm.s.StatRZRetGDTFault, STAMTYPE_COUNTER, "/VMM/RZRet/GDTFault", STAMUNIT_OCCURENCES, "Number of VINF_EM_EXECUTE_INSTRUCTION_LDT_FAULT returns.");
376 STAM_REG(pVM, &pVM->vmm.s.StatRZRetIDTFault, STAMTYPE_COUNTER, "/VMM/RZRet/IDTFault", STAMUNIT_OCCURENCES, "Number of VINF_EM_EXECUTE_INSTRUCTION_IDT_FAULT returns.");
377 STAM_REG(pVM, &pVM->vmm.s.StatRZRetTSSFault, STAMTYPE_COUNTER, "/VMM/RZRet/TSSFault", STAMUNIT_OCCURENCES, "Number of VINF_EM_EXECUTE_INSTRUCTION_TSS_FAULT returns.");
378 STAM_REG(pVM, &pVM->vmm.s.StatRZRetCSAMTask, STAMTYPE_COUNTER, "/VMM/RZRet/CSAMTask", STAMUNIT_OCCURENCES, "Number of VINF_CSAM_PENDING_ACTION returns.");
379 STAM_REG(pVM, &pVM->vmm.s.StatRZRetSyncCR3, STAMTYPE_COUNTER, "/VMM/RZRet/SyncCR", STAMUNIT_OCCURENCES, "Number of VINF_PGM_SYNC_CR3 returns.");
380 STAM_REG(pVM, &pVM->vmm.s.StatRZRetMisc, STAMTYPE_COUNTER, "/VMM/RZRet/Misc", STAMUNIT_OCCURENCES, "Number of misc returns.");
381 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPatchInt3, STAMTYPE_COUNTER, "/VMM/RZRet/PatchInt3", STAMUNIT_OCCURENCES, "Number of VINF_PATM_PATCH_INT3 returns.");
382 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPatchPF, STAMTYPE_COUNTER, "/VMM/RZRet/PatchPF", STAMUNIT_OCCURENCES, "Number of VINF_PATM_PATCH_TRAP_PF returns.");
383 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPatchGP, STAMTYPE_COUNTER, "/VMM/RZRet/PatchGP", STAMUNIT_OCCURENCES, "Number of VINF_PATM_PATCH_TRAP_GP returns.");
384 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPatchIretIRQ, STAMTYPE_COUNTER, "/VMM/RZRet/PatchIret", STAMUNIT_OCCURENCES, "Number of VINF_PATM_PENDING_IRQ_AFTER_IRET returns.");
385 STAM_REG(pVM, &pVM->vmm.s.StatRZRetRescheduleREM, STAMTYPE_COUNTER, "/VMM/RZRet/ScheduleREM", STAMUNIT_OCCURENCES, "Number of VINF_EM_RESCHEDULE_REM returns.");
386 STAM_REG(pVM, &pVM->vmm.s.StatRZRetToR3Total, STAMTYPE_COUNTER, "/VMM/RZRet/ToR3", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_TO_R3 returns.");
387 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.");
388 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.");
389 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.");
390 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.");
391 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.");
392 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.");
393 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.");
394 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.");
395 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.");
396 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.");
397 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.");
398 STAM_REG(pVM, &pVM->vmm.s.StatRZRetTimerPending, STAMTYPE_COUNTER, "/VMM/RZRet/TimerPending", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_TIMER_PENDING returns.");
399 STAM_REG(pVM, &pVM->vmm.s.StatRZRetInterruptPending, STAMTYPE_COUNTER, "/VMM/RZRet/InterruptPending", STAMUNIT_OCCURENCES, "Number of VINF_EM_RAW_INTERRUPT_PENDING returns.");
400 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPATMDuplicateFn, STAMTYPE_COUNTER, "/VMM/RZRet/PATMDuplicateFn", STAMUNIT_OCCURENCES, "Number of VINF_PATM_DUPLICATE_FUNCTION returns.");
401 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPGMFlushPending, STAMTYPE_COUNTER, "/VMM/RZRet/PGMFlushPending", STAMUNIT_OCCURENCES, "Number of VINF_PGM_POOL_FLUSH_PENDING returns.");
402 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPendingRequest, STAMTYPE_COUNTER, "/VMM/RZRet/PendingRequest", STAMUNIT_OCCURENCES, "Number of VINF_EM_PENDING_REQUEST returns.");
403 STAM_REG(pVM, &pVM->vmm.s.StatRZRetPatchTPR, STAMTYPE_COUNTER, "/VMM/RZRet/PatchTPR", STAMUNIT_OCCURENCES, "Number of VINF_EM_HM_PATCH_TPR_INSTR returns.");
404
405 STAMR3Register(pVM, &pVM->vmm.s.StatLogFlusherFlushes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, "/VMM/LogFlush/00-Flushes", STAMUNIT_OCCURENCES, "Total number of buffer flushes");
406 STAMR3Register(pVM, &pVM->vmm.s.StatLogFlusherNoWakeUp, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, "/VMM/LogFlush/00-NoWakups", STAMUNIT_OCCURENCES, "Times the flusher thread didn't need waking up.");
407
408 for (VMCPUID i = 0; i < pVM->cCpus; i++)
409 {
410 PVMCPU pVCpu = pVM->apCpusR3[i];
411 STAMR3RegisterF(pVM, &pVCpu->vmm.s.StatR0HaltBlock, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_NS_PER_CALL, "", "/PROF/CPU%u/VM/Halt/R0HaltBlock", i);
412 STAMR3RegisterF(pVM, &pVCpu->vmm.s.StatR0HaltBlockOnTime, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_NS_PER_CALL, "", "/PROF/CPU%u/VM/Halt/R0HaltBlockOnTime", i);
413 STAMR3RegisterF(pVM, &pVCpu->vmm.s.StatR0HaltBlockOverslept, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_NS_PER_CALL, "", "/PROF/CPU%u/VM/Halt/R0HaltBlockOverslept", i);
414 STAMR3RegisterF(pVM, &pVCpu->vmm.s.StatR0HaltBlockInsomnia, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_NS_PER_CALL, "", "/PROF/CPU%u/VM/Halt/R0HaltBlockInsomnia", i);
415 STAMR3RegisterF(pVM, &pVCpu->vmm.s.StatR0HaltExec, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "", "/PROF/CPU%u/VM/Halt/R0HaltExec", i);
416 STAMR3RegisterF(pVM, &pVCpu->vmm.s.StatR0HaltExecFromSpin, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "", "/PROF/CPU%u/VM/Halt/R0HaltExec/FromSpin", i);
417 STAMR3RegisterF(pVM, &pVCpu->vmm.s.StatR0HaltExecFromBlock, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "", "/PROF/CPU%u/VM/Halt/R0HaltExec/FromBlock", i);
418 STAMR3RegisterF(pVM, &pVCpu->vmm.s.StatR0HaltToR3, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "", "/PROF/CPU%u/VM/Halt/R0HaltToR3", i);
419 STAMR3RegisterF(pVM, &pVCpu->vmm.s.StatR0HaltToR3FromSpin, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "", "/PROF/CPU%u/VM/Halt/R0HaltToR3/FromSpin", i);
420 STAMR3RegisterF(pVM, &pVCpu->vmm.s.StatR0HaltToR3Other, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "", "/PROF/CPU%u/VM/Halt/R0HaltToR3/Other", i);
421 STAMR3RegisterF(pVM, &pVCpu->vmm.s.StatR0HaltToR3PendingFF, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "", "/PROF/CPU%u/VM/Halt/R0HaltToR3/PendingFF", i);
422 STAMR3RegisterF(pVM, &pVCpu->vmm.s.StatR0HaltToR3SmallDelta, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "", "/PROF/CPU%u/VM/Halt/R0HaltToR3/SmallDelta", i);
423 STAMR3RegisterF(pVM, &pVCpu->vmm.s.StatR0HaltToR3PostNoInt, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "", "/PROF/CPU%u/VM/Halt/R0HaltToR3/PostWaitNoInt", i);
424 STAMR3RegisterF(pVM, &pVCpu->vmm.s.StatR0HaltToR3PostPendingFF,STAMTYPE_COUNTER,STAMVISIBILITY_ALWAYS,STAMUNIT_OCCURENCES, "", "/PROF/CPU%u/VM/Halt/R0HaltToR3/PostWaitPendingFF", i);
425 STAMR3RegisterF(pVM, &pVCpu->vmm.s.cR0Halts, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "", "/PROF/CPU%u/VM/Halt/R0HaltHistoryCounter", i);
426 STAMR3RegisterF(pVM, &pVCpu->vmm.s.cR0HaltsSucceeded, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "", "/PROF/CPU%u/VM/Halt/R0HaltHistorySucceeded", i);
427 STAMR3RegisterF(pVM, &pVCpu->vmm.s.cR0HaltsToRing3, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "", "/PROF/CPU%u/VM/Halt/R0HaltHistoryToRing3", i);
428
429 STAMR3RegisterF(pVM, &pVCpu->cEmtHashCollisions, STAMTYPE_U8, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "", "/VMM/EmtHashCollisions/Emt%02u", i);
430
431 PVMMR3CPULOGGER pShared = &pVCpu->vmm.s.u.s.Logger;
432 STAMR3RegisterF(pVM, &pShared->StatFlushes, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_OCCURENCES, "", "/VMM/LogFlush/CPU%u/Reg", i);
433 STAMR3RegisterF(pVM, &pShared->StatCannotBlock, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_OCCURENCES, "", "/VMM/LogFlush/CPU%u/Reg/CannotBlock", i);
434 STAMR3RegisterF(pVM, &pShared->StatWait, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL, "", "/VMM/LogFlush/CPU%u/Reg/Wait", i);
435 STAMR3RegisterF(pVM, &pShared->StatRaces, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL, "", "/VMM/LogFlush/CPU%u/Reg/Races", i);
436 STAMR3RegisterF(pVM, &pShared->StatRacesToR0, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_OCCURENCES, "", "/VMM/LogFlush/CPU%u/Reg/RacesToR0", i);
437 STAMR3RegisterF(pVM, &pShared->cbDropped, STAMTYPE_U32, STAMVISIBILITY_USED, STAMUNIT_BYTES, "", "/VMM/LogFlush/CPU%u/Reg/cbDropped", i);
438 STAMR3RegisterF(pVM, &pShared->cbBuf, STAMTYPE_U32, STAMVISIBILITY_USED, STAMUNIT_BYTES, "", "/VMM/LogFlush/CPU%u/Reg/cbBuf", i);
439 STAMR3RegisterF(pVM, &pShared->idxBuf, STAMTYPE_U32, STAMVISIBILITY_USED, STAMUNIT_BYTES, "", "/VMM/LogFlush/CPU%u/Reg/idxBuf", i);
440
441 pShared = &pVCpu->vmm.s.u.s.RelLogger;
442 STAMR3RegisterF(pVM, &pShared->StatFlushes, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_OCCURENCES, "", "/VMM/LogFlush/CPU%u/Rel", i);
443 STAMR3RegisterF(pVM, &pShared->StatCannotBlock, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_OCCURENCES, "", "/VMM/LogFlush/CPU%u/Rel/CannotBlock", i);
444 STAMR3RegisterF(pVM, &pShared->StatWait, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL, "", "/VMM/LogFlush/CPU%u/Rel/Wait", i);
445 STAMR3RegisterF(pVM, &pShared->StatRaces, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL, "", "/VMM/LogFlush/CPU%u/Rel/Races", i);
446 STAMR3RegisterF(pVM, &pShared->StatRacesToR0, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_OCCURENCES, "", "/VMM/LogFlush/CPU%u/Rel/RacesToR0", i);
447 STAMR3RegisterF(pVM, &pShared->cbDropped, STAMTYPE_U32, STAMVISIBILITY_USED, STAMUNIT_BYTES, "", "/VMM/LogFlush/CPU%u/Rel/cbDropped", i);
448 STAMR3RegisterF(pVM, &pShared->cbBuf, STAMTYPE_U32, STAMVISIBILITY_USED, STAMUNIT_BYTES, "", "/VMM/LogFlush/CPU%u/Rel/cbBuf", i);
449 STAMR3RegisterF(pVM, &pShared->idxBuf, STAMTYPE_U32, STAMVISIBILITY_USED, STAMUNIT_BYTES, "", "/VMM/LogFlush/CPU%u/Rel/idxBuf", i);
450 }
451}
452
453
454/**
455 * Worker for VMMR3InitR0 that calls ring-0 to do EMT specific initialization.
456 *
457 * @returns VBox status code.
458 * @param pVM The cross context VM structure.
459 * @param pVCpu The cross context per CPU structure.
460 * @thread EMT(pVCpu)
461 */
462static DECLCALLBACK(int) vmmR3InitR0Emt(PVM pVM, PVMCPU pVCpu)
463{
464 return VMMR3CallR0Emt(pVM, pVCpu, VMMR0_DO_VMMR0_INIT_EMT, 0, NULL);
465}
466
467
468/**
469 * Initializes the R0 VMM.
470 *
471 * @returns VBox status code.
472 * @param pVM The cross context VM structure.
473 */
474VMMR3_INT_DECL(int) VMMR3InitR0(PVM pVM)
475{
476 int rc;
477 PVMCPU pVCpu = VMMGetCpu(pVM);
478 Assert(pVCpu && pVCpu->idCpu == 0);
479
480 /*
481 * Nothing to do here in driverless mode.
482 */
483 if (SUPR3IsDriverless())
484 return VINF_SUCCESS;
485
486 /*
487 * Make sure the ring-0 loggers are up to date.
488 */
489 rc = VMMR3UpdateLoggers(pVM);
490 if (RT_FAILURE(rc))
491 return rc;
492
493 /*
494 * Call Ring-0 entry with init code.
495 */
496#ifdef NO_SUPCALLR0VMM
497 //rc = VERR_GENERAL_FAILURE;
498 rc = VINF_SUCCESS;
499#else
500 rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), 0 /*idCpu*/, VMMR0_DO_VMMR0_INIT, RT_MAKE_U64(VMMGetSvnRev(), vmmGetBuildType()), NULL);
501#endif
502
503 /*
504 * Flush the logs & deal with assertions.
505 */
506#ifdef LOG_ENABLED
507 VMM_FLUSH_R0_LOG(pVM, pVCpu, &pVCpu->vmm.s.u.s.Logger, NULL);
508#endif
509 VMM_FLUSH_R0_LOG(pVM, pVCpu, &pVCpu->vmm.s.u.s.RelLogger, RTLogRelGetDefaultInstance());
510 if (rc == VERR_VMM_RING0_ASSERTION)
511 rc = vmmR3HandleRing0Assert(pVM, pVCpu);
512 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
513 {
514 LogRel(("VMM: R0 init failed, rc=%Rra\n", rc));
515 if (RT_SUCCESS(rc))
516 rc = VERR_IPE_UNEXPECTED_INFO_STATUS;
517 }
518
519 /*
520 * Log stuff we learned in ring-0.
521 */
522 /* Log whether thread-context hooks are used (on Linux this can depend on how the kernel is configured). */
523 if (pVM->vmm.s.fIsUsingContextHooks)
524 LogRel(("VMM: Enabled thread-context hooks\n"));
525 else
526 LogRel(("VMM: Thread-context hooks unavailable\n"));
527
528 /* Log RTThreadPreemptIsPendingTrusty() and RTThreadPreemptIsPossible() results. */
529 if (pVM->vmm.s.fIsPreemptPendingApiTrusty)
530 LogRel(("VMM: RTThreadPreemptIsPending() can be trusted\n"));
531 else
532 LogRel(("VMM: Warning! RTThreadPreemptIsPending() cannot be trusted! Need to update kernel info?\n"));
533 if (pVM->vmm.s.fIsPreemptPossible)
534 LogRel(("VMM: Kernel preemption is possible\n"));
535 else
536 LogRel(("VMM: Kernel preemption is not possible it seems\n"));
537
538 /*
539 * Send all EMTs to ring-0 to get their logger initialized.
540 */
541 for (VMCPUID idCpu = 0; RT_SUCCESS(rc) && idCpu < pVM->cCpus; idCpu++)
542 rc = VMR3ReqCallWait(pVM, idCpu, (PFNRT)vmmR3InitR0Emt, 2, pVM, pVM->apCpusR3[idCpu]);
543
544 return rc;
545}
546
547
548/**
549 * Called when an init phase completes.
550 *
551 * @returns VBox status code.
552 * @param pVM The cross context VM structure.
553 * @param enmWhat Which init phase.
554 */
555VMMR3_INT_DECL(int) VMMR3InitCompleted(PVM pVM, VMINITCOMPLETED enmWhat)
556{
557 int rc = VINF_SUCCESS;
558
559 switch (enmWhat)
560 {
561 case VMINITCOMPLETED_RING3:
562 {
563#if 0 /* pointless when timers doesn't run on EMT */
564 /*
565 * Create the EMT yield timer.
566 */
567 rc = TMR3TimerCreate(pVM, TMCLOCK_REAL, vmmR3YieldEMT, NULL, TMTIMER_FLAGS_NO_RING0,
568 "EMT Yielder", &pVM->vmm.s.hYieldTimer);
569 AssertRCReturn(rc, rc);
570
571 rc = TMTimerSetMillies(pVM, pVM->vmm.s.hYieldTimer, pVM->vmm.s.cYieldEveryMillies);
572 AssertRCReturn(rc, rc);
573#endif
574 break;
575 }
576
577 case VMINITCOMPLETED_HM:
578 {
579#if !defined(VBOX_VMM_TARGET_ARMV8)
580 /*
581 * Disable the periodic preemption timers if we can use the
582 * VMX-preemption timer instead.
583 */
584 if ( pVM->vmm.s.fUsePeriodicPreemptionTimers
585 && HMR3IsVmxPreemptionTimerUsed(pVM))
586 pVM->vmm.s.fUsePeriodicPreemptionTimers = false;
587 LogRel(("VMM: fUsePeriodicPreemptionTimers=%RTbool\n", pVM->vmm.s.fUsePeriodicPreemptionTimers));
588#endif
589
590 /*
591 * Last chance for GIM to update its CPUID leaves if it requires
592 * knowledge/information from HM initialization.
593 */
594/** @todo r=bird: This shouldn't be done from here, but rather from VM.cpp. There is no dependency on VMM here. */
595 rc = GIMR3InitCompleted(pVM);
596 AssertRCReturn(rc, rc);
597
598 /*
599 * CPUM's post-initialization (print CPUIDs).
600 */
601 CPUMR3LogCpuIdAndMsrFeatures(pVM);
602 break;
603 }
604
605 default: /* shuts up gcc */
606 break;
607 }
608
609 return rc;
610}
611
612
613/**
614 * Terminate the VMM bits.
615 *
616 * @returns VBox status code.
617 * @param pVM The cross context VM structure.
618 */
619VMMR3_INT_DECL(int) VMMR3Term(PVM pVM)
620{
621 PVMCPU pVCpu = VMMGetCpu(pVM);
622 Assert(pVCpu && pVCpu->idCpu == 0);
623
624 /*
625 * Call Ring-0 entry with termination code.
626 */
627 int rc = VINF_SUCCESS;
628 if (!SUPR3IsDriverless())
629 {
630#ifndef NO_SUPCALLR0VMM
631 rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), 0 /*idCpu*/, VMMR0_DO_VMMR0_TERM, 0, NULL);
632#endif
633 }
634
635 /*
636 * Flush the logs & deal with assertions.
637 */
638#ifdef LOG_ENABLED
639 VMM_FLUSH_R0_LOG(pVM, pVCpu, &pVCpu->vmm.s.u.s.Logger, NULL);
640#endif
641 VMM_FLUSH_R0_LOG(pVM, pVCpu, &pVCpu->vmm.s.u.s.RelLogger, RTLogRelGetDefaultInstance());
642 if (rc == VERR_VMM_RING0_ASSERTION)
643 rc = vmmR3HandleRing0Assert(pVM, pVCpu);
644 if (RT_FAILURE(rc) || (rc >= VINF_EM_FIRST && rc <= VINF_EM_LAST))
645 {
646 LogRel(("VMM: VMMR3Term: R0 term failed, rc=%Rra. (warning)\n", rc));
647 if (RT_SUCCESS(rc))
648 rc = VERR_IPE_UNEXPECTED_INFO_STATUS;
649 }
650
651 /*
652 * Do clean ups.
653 */
654 for (VMCPUID i = 0; i < pVM->cCpus; i++)
655 {
656 RTSemEventDestroy(pVM->vmm.s.pahEvtRendezvousEnterOrdered[i]);
657 pVM->vmm.s.pahEvtRendezvousEnterOrdered[i] = NIL_RTSEMEVENT;
658 }
659 RTSemEventDestroy(pVM->vmm.s.hEvtRendezvousEnterOneByOne);
660 pVM->vmm.s.hEvtRendezvousEnterOneByOne = NIL_RTSEMEVENT;
661 RTSemEventMultiDestroy(pVM->vmm.s.hEvtMulRendezvousEnterAllAtOnce);
662 pVM->vmm.s.hEvtMulRendezvousEnterAllAtOnce = NIL_RTSEMEVENTMULTI;
663 RTSemEventMultiDestroy(pVM->vmm.s.hEvtMulRendezvousDone);
664 pVM->vmm.s.hEvtMulRendezvousDone = NIL_RTSEMEVENTMULTI;
665 RTSemEventDestroy(pVM->vmm.s.hEvtRendezvousDoneCaller);
666 pVM->vmm.s.hEvtRendezvousDoneCaller = NIL_RTSEMEVENT;
667 RTSemEventMultiDestroy(pVM->vmm.s.hEvtMulRendezvousRecursionPush);
668 pVM->vmm.s.hEvtMulRendezvousRecursionPush = NIL_RTSEMEVENTMULTI;
669 RTSemEventMultiDestroy(pVM->vmm.s.hEvtMulRendezvousRecursionPop);
670 pVM->vmm.s.hEvtMulRendezvousRecursionPop = NIL_RTSEMEVENTMULTI;
671 RTSemEventDestroy(pVM->vmm.s.hEvtRendezvousRecursionPushCaller);
672 pVM->vmm.s.hEvtRendezvousRecursionPushCaller = NIL_RTSEMEVENT;
673 RTSemEventDestroy(pVM->vmm.s.hEvtRendezvousRecursionPopCaller);
674 pVM->vmm.s.hEvtRendezvousRecursionPopCaller = NIL_RTSEMEVENT;
675
676 vmmTermFormatTypes();
677
678 /*
679 * Wait for the log flusher thread to complete.
680 */
681 if (pVM->vmm.s.hLogFlusherThread != NIL_RTTHREAD)
682 {
683 int rc2 = RTThreadWait(pVM->vmm.s.hLogFlusherThread, RT_MS_30SEC, NULL);
684 AssertLogRelRC(rc2);
685 if (RT_SUCCESS(rc2))
686 pVM->vmm.s.hLogFlusherThread = NIL_RTTHREAD;
687 }
688
689 return rc;
690}
691
692
693/**
694 * Applies relocations to data and code managed by this
695 * component. This function will be called at init and
696 * whenever the VMM need to relocate it self inside the GC.
697 *
698 * The VMM will need to apply relocations to the core code.
699 *
700 * @param pVM The cross context VM structure.
701 * @param offDelta The relocation delta.
702 */
703VMMR3_INT_DECL(void) VMMR3Relocate(PVM pVM, RTGCINTPTR offDelta)
704{
705 LogFlow(("VMMR3Relocate: offDelta=%RGv\n", offDelta));
706 RT_NOREF(offDelta);
707
708 /*
709 * Update the logger.
710 */
711 VMMR3UpdateLoggers(pVM);
712}
713
714
715/**
716 * Worker for VMMR3UpdateLoggers.
717 */
718static int vmmR3UpdateLoggersWorker(PVM pVM, PVMCPU pVCpu, PRTLOGGER pSrcLogger, bool fReleaseLogger)
719{
720 /*
721 * Get the group count.
722 */
723 uint32_t uGroupsCrc32 = 0;
724 uint32_t cGroups = 0;
725 uint64_t fFlags = 0;
726 int rc = RTLogQueryBulk(pSrcLogger, &fFlags, &uGroupsCrc32, &cGroups, NULL);
727 Assert(rc == VERR_BUFFER_OVERFLOW);
728
729 /*
730 * Allocate the request of the right size.
731 */
732 uint32_t const cbReq = RT_UOFFSETOF_DYN(VMMR0UPDATELOGGERSREQ, afGroups[cGroups]);
733 PVMMR0UPDATELOGGERSREQ pReq = (PVMMR0UPDATELOGGERSREQ)RTMemAllocZVar(cbReq);
734 if (pReq)
735 {
736 pReq->Hdr.u32Magic = SUPVMMR0REQHDR_MAGIC;
737 pReq->Hdr.cbReq = cbReq;
738 pReq->cGroups = cGroups;
739 rc = RTLogQueryBulk(pSrcLogger, &pReq->fFlags, &pReq->uGroupCrc32, &pReq->cGroups, pReq->afGroups);
740 AssertRC(rc);
741 if (RT_SUCCESS(rc))
742 {
743 /*
744 * The 64-bit value argument.
745 */
746 uint64_t fExtraArg = fReleaseLogger;
747
748 /* Only outputting to the parent VMM's logs? Enable ring-0 to flush directly. */
749 uint32_t fDst = RTLogGetDestinations(pSrcLogger);
750 fDst &= ~(RTLOGDEST_DUMMY | RTLOGDEST_F_NO_DENY | RTLOGDEST_F_DELAY_FILE | RTLOGDEST_FIXED_FILE | RTLOGDEST_FIXED_DIR);
751 if ( (fDst & (RTLOGDEST_VMM | RTLOGDEST_VMM_REL))
752 && !(fDst & ~(RTLOGDEST_VMM | RTLOGDEST_VMM_REL)))
753 fExtraArg |= (fDst & RTLOGDEST_VMM ? VMMR0UPDATELOGGER_F_TO_PARENT_VMM_DBG : 0)
754 | (fDst & RTLOGDEST_VMM_REL ? VMMR0UPDATELOGGER_F_TO_PARENT_VMM_REL : 0);
755
756 rc = VMMR3CallR0Emt(pVM, pVCpu, VMMR0_DO_VMMR0_UPDATE_LOGGERS, fExtraArg, &pReq->Hdr);
757 }
758
759 RTMemFree(pReq);
760 }
761 else
762 rc = VERR_NO_MEMORY;
763 return rc;
764}
765
766
767/**
768 * Updates the settings for the RC and R0 loggers.
769 *
770 * @returns VBox status code.
771 * @param pVM The cross context VM structure.
772 * @thread EMT
773 */
774VMMR3_INT_DECL(int) VMMR3UpdateLoggers(PVM pVM)
775{
776 /* Nothing to do here if we're in driverless mode: */
777 if (SUPR3IsDriverless())
778 return VINF_SUCCESS;
779
780 PVMCPU pVCpu = VMMGetCpu(pVM);
781 AssertReturn(pVCpu, VERR_VM_THREAD_NOT_EMT);
782
783 /*
784 * Each EMT has each own logger instance.
785 */
786 /* Debug logging.*/
787 int rcDebug = VINF_SUCCESS;
788#ifdef LOG_ENABLED
789 PRTLOGGER const pDefault = RTLogDefaultInstance();
790 if (pDefault)
791 rcDebug = vmmR3UpdateLoggersWorker(pVM, pVCpu, pDefault, false /*fReleaseLogger*/);
792#else
793 RT_NOREF(pVM);
794#endif
795
796 /* Release logging. */
797 int rcRelease = VINF_SUCCESS;
798 PRTLOGGER const pRelease = RTLogRelGetDefaultInstance();
799 if (pRelease)
800 rcRelease = vmmR3UpdateLoggersWorker(pVM, pVCpu, pRelease, true /*fReleaseLogger*/);
801
802 return RT_SUCCESS(rcDebug) ? rcRelease : rcDebug;
803}
804
805
806/**
807 * @callback_method_impl{FNRTTHREAD, Ring-0 log flusher thread.}
808 */
809static DECLCALLBACK(int) vmmR3LogFlusher(RTTHREAD hThreadSelf, void *pvUser)
810{
811 PVM const pVM = (PVM)pvUser;
812 RT_NOREF(hThreadSelf);
813
814 /* Reset the flusher state before we start: */
815 pVM->vmm.s.LogFlusherItem.u32 = UINT32_MAX;
816
817 /*
818 * The work loop.
819 */
820 for (;;)
821 {
822 /*
823 * Wait for work.
824 */
825 int rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), NIL_VMCPUID, VMMR0_DO_VMMR0_LOG_FLUSHER, 0, NULL);
826 if (RT_SUCCESS(rc))
827 {
828 /* Paranoia: Make another copy of the request, to make sure the validated data can't be changed. */
829 VMMLOGFLUSHERENTRY Item;
830 Item.u32 = pVM->vmm.s.LogFlusherItem.u32;
831 if ( Item.s.idCpu < pVM->cCpus
832 && Item.s.idxLogger < VMMLOGGER_IDX_MAX
833 && Item.s.idxBuffer < VMMLOGGER_BUFFER_COUNT)
834 {
835 /*
836 * Verify the request.
837 */
838 PVMCPU const pVCpu = pVM->apCpusR3[Item.s.idCpu];
839 PVMMR3CPULOGGER const pShared = &pVCpu->vmm.s.u.aLoggers[Item.s.idxLogger];
840 uint32_t const cbToFlush = pShared->aBufs[Item.s.idxBuffer].AuxDesc.offBuf;
841 if (cbToFlush > 0)
842 {
843 if (cbToFlush <= pShared->cbBuf)
844 {
845 char * const pchBufR3 = pShared->aBufs[Item.s.idxBuffer].pchBufR3;
846 if (pchBufR3)
847 {
848 /*
849 * Do the flushing.
850 */
851 PRTLOGGER const pLogger = Item.s.idxLogger == VMMLOGGER_IDX_REGULAR
852 ? RTLogGetDefaultInstance() : RTLogRelGetDefaultInstance();
853 if (pLogger)
854 {
855 char szBefore[128];
856 RTStrPrintf(szBefore, sizeof(szBefore),
857 "*FLUSH* idCpu=%u idxLogger=%u idxBuffer=%u cbToFlush=%#x fFlushed=%RTbool cbDropped=%#x\n",
858 Item.s.idCpu, Item.s.idxLogger, Item.s.idxBuffer, cbToFlush,
859 pShared->aBufs[Item.s.idxBuffer].AuxDesc.fFlushedIndicator, pShared->cbDropped);
860 RTLogBulkWrite(pLogger, szBefore, pchBufR3, cbToFlush, "*FLUSH DONE*\n");
861 }
862 }
863 else
864 Log(("vmmR3LogFlusher: idCpu=%u idxLogger=%u idxBuffer=%u cbToFlush=%#x: Warning! No ring-3 buffer pointer!\n",
865 Item.s.idCpu, Item.s.idxLogger, Item.s.idxBuffer, cbToFlush));
866 }
867 else
868 Log(("vmmR3LogFlusher: idCpu=%u idxLogger=%u idxBuffer=%u cbToFlush=%#x: Warning! Exceeds %#x bytes buffer size!\n",
869 Item.s.idCpu, Item.s.idxLogger, Item.s.idxBuffer, cbToFlush, pShared->cbBuf));
870 }
871 else
872 Log(("vmmR3LogFlusher: idCpu=%u idxLogger=%u idxBuffer=%u cbToFlush=%#x: Warning! Zero bytes to flush!\n",
873 Item.s.idCpu, Item.s.idxLogger, Item.s.idxBuffer, cbToFlush));
874
875 /*
876 * Mark the descriptor as flushed and set the request flag for same.
877 */
878 pShared->aBufs[Item.s.idxBuffer].AuxDesc.fFlushedIndicator = true;
879 }
880 else
881 {
882 Assert(Item.s.idCpu == UINT16_MAX);
883 Assert(Item.s.idxLogger == UINT8_MAX);
884 Assert(Item.s.idxBuffer == UINT8_MAX);
885 }
886 }
887 /*
888 * Interrupted can happen, just ignore it.
889 */
890 else if (rc == VERR_INTERRUPTED)
891 { /* ignore*/ }
892 /*
893 * The ring-0 termination code will set the shutdown flag and wake us
894 * up, and we should return with object destroyed. In case there is
895 * some kind of race, we might also get sempahore destroyed.
896 */
897 else if ( rc == VERR_OBJECT_DESTROYED
898 || rc == VERR_SEM_DESTROYED
899 || rc == VERR_INVALID_HANDLE)
900 {
901 LogRel(("vmmR3LogFlusher: Terminating (%Rrc)\n", rc));
902 return VINF_SUCCESS;
903 }
904 /*
905 * There shouldn't be any other errors...
906 */
907 else
908 {
909 LogRelMax(64, ("vmmR3LogFlusher: VMMR0_DO_VMMR0_LOG_FLUSHER -> %Rrc\n", rc));
910 AssertRC(rc);
911 RTThreadSleep(1);
912 }
913 }
914}
915
916
917/**
918 * Helper for VMM_FLUSH_R0_LOG that does the flushing.
919 *
920 * @param pVM The cross context VM structure.
921 * @param pVCpu The cross context virtual CPU structure of the calling
922 * EMT.
923 * @param pShared The shared logger data.
924 * @param idxBuf The buffer to flush.
925 * @param pDstLogger The destination IPRT logger.
926 */
927static void vmmR3LogReturnFlush(PVM pVM, PVMCPU pVCpu, PVMMR3CPULOGGER pShared, size_t idxBuf, PRTLOGGER pDstLogger)
928{
929 uint32_t const cbToFlush = pShared->aBufs[idxBuf].AuxDesc.offBuf;
930 const char *pszBefore = cbToFlush < 256 ? NULL : "*FLUSH*\n";
931 const char *pszAfter = cbToFlush < 256 ? NULL : "*END*\n";
932
933#if VMMLOGGER_BUFFER_COUNT > 1
934 /*
935 * When we have more than one log buffer, the flusher thread may still be
936 * working on the previous buffer when we get here.
937 */
938 char szBefore[64];
939 if (pShared->cFlushing > 0)
940 {
941 STAM_REL_PROFILE_START(&pShared->StatRaces, a);
942 uint64_t const nsStart = RTTimeNanoTS();
943
944 /* A no-op, but it takes the lock and the hope is that we end up waiting
945 on the flusher to finish up. */
946 RTLogBulkWrite(pDstLogger, NULL, "", 0, NULL);
947 if (pShared->cFlushing != 0)
948 {
949 RTLogBulkWrite(pDstLogger, NULL, "", 0, NULL);
950
951 /* If no luck, go to ring-0 and to proper waiting. */
952 if (pShared->cFlushing != 0)
953 {
954 STAM_REL_COUNTER_INC(&pShared->StatRacesToR0);
955 SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), pVCpu->idCpu, VMMR0_DO_VMMR0_LOG_WAIT_FLUSHED, 0, NULL);
956 }
957 }
958
959 RTStrPrintf(szBefore, sizeof(szBefore), "*%sFLUSH* waited %'RU64 ns\n",
960 pShared->cFlushing == 0 ? "" : " MISORDERED", RTTimeNanoTS() - nsStart);
961 pszBefore = szBefore;
962 STAM_REL_PROFILE_STOP(&pShared->StatRaces, a);
963 }
964#else
965 RT_NOREF(pVM, pVCpu);
966#endif
967
968 RTLogBulkWrite(pDstLogger, pszBefore, pShared->aBufs[idxBuf].pchBufR3, cbToFlush, pszAfter);
969 pShared->aBufs[idxBuf].AuxDesc.fFlushedIndicator = true;
970}
971
972
973/**
974 * Gets the pointer to a buffer containing the R0/RC RTAssertMsg1Weak output.
975 *
976 * @returns Pointer to the buffer.
977 * @param pVM The cross context VM structure.
978 */
979VMMR3DECL(const char *) VMMR3GetRZAssertMsg1(PVM pVM)
980{
981 return pVM->vmm.s.szRing0AssertMsg1;
982}
983
984
985/**
986 * Returns the VMCPU of the specified virtual CPU.
987 *
988 * @returns The VMCPU pointer. NULL if @a idCpu or @a pUVM is invalid.
989 *
990 * @param pUVM The user mode VM handle.
991 * @param idCpu The ID of the virtual CPU.
992 */
993VMMR3DECL(PVMCPU) VMMR3GetCpuByIdU(PUVM pUVM, RTCPUID idCpu)
994{
995 UVM_ASSERT_VALID_EXT_RETURN(pUVM, NULL);
996 AssertReturn(idCpu < pUVM->cCpus, NULL);
997 VM_ASSERT_VALID_EXT_RETURN(pUVM->pVM, NULL);
998 return pUVM->pVM->apCpusR3[idCpu];
999}
1000
1001
1002/**
1003 * Gets the pointer to a buffer containing the R0/RC RTAssertMsg2Weak output.
1004 *
1005 * @returns Pointer to the buffer.
1006 * @param pVM The cross context VM structure.
1007 */
1008VMMR3DECL(const char *) VMMR3GetRZAssertMsg2(PVM pVM)
1009{
1010 return pVM->vmm.s.szRing0AssertMsg2;
1011}
1012
1013
1014/**
1015 * Execute state save operation.
1016 *
1017 * @returns VBox status code.
1018 * @param pVM The cross context VM structure.
1019 * @param pSSM SSM operation handle.
1020 */
1021static DECLCALLBACK(int) vmmR3Save(PVM pVM, PSSMHANDLE pSSM)
1022{
1023 LogFlow(("vmmR3Save:\n"));
1024
1025 /*
1026 * Save the started/stopped state of all CPUs except 0 as it will always
1027 * be running. This avoids breaking the saved state version. :-)
1028 */
1029 for (VMCPUID i = 1; i < pVM->cCpus; i++)
1030 SSMR3PutBool(pSSM, VMCPUSTATE_IS_STARTED(VMCPU_GET_STATE(pVM->apCpusR3[i])));
1031
1032 return SSMR3PutU32(pSSM, UINT32_MAX); /* terminator */
1033}
1034
1035
1036/**
1037 * Execute state load operation.
1038 *
1039 * @returns VBox status code.
1040 * @param pVM The cross context VM structure.
1041 * @param pSSM SSM operation handle.
1042 * @param uVersion Data layout version.
1043 * @param uPass The data pass.
1044 */
1045static DECLCALLBACK(int) vmmR3Load(PVM pVM, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
1046{
1047 LogFlow(("vmmR3Load:\n"));
1048 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
1049
1050 /*
1051 * Validate version.
1052 */
1053 if ( uVersion != VMM_SAVED_STATE_VERSION
1054 && uVersion != VMM_SAVED_STATE_VERSION_3_0)
1055 {
1056 AssertMsgFailed(("vmmR3Load: Invalid version uVersion=%u!\n", uVersion));
1057 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
1058 }
1059
1060 if (uVersion <= VMM_SAVED_STATE_VERSION_3_0)
1061 {
1062 /* Ignore the stack bottom, stack pointer and stack bits. */
1063 RTRCPTR RCPtrIgnored;
1064 SSMR3GetRCPtr(pSSM, &RCPtrIgnored);
1065 SSMR3GetRCPtr(pSSM, &RCPtrIgnored);
1066#ifdef RT_OS_DARWIN
1067 if ( SSMR3HandleVersion(pSSM) >= VBOX_FULL_VERSION_MAKE(3,0,0)
1068 && SSMR3HandleVersion(pSSM) < VBOX_FULL_VERSION_MAKE(3,1,0)
1069 && SSMR3HandleRevision(pSSM) >= 48858
1070 && ( !strcmp(SSMR3HandleHostOSAndArch(pSSM), "darwin.x86")
1071 || !strcmp(SSMR3HandleHostOSAndArch(pSSM), "") )
1072 )
1073 SSMR3Skip(pSSM, 16384);
1074 else
1075 SSMR3Skip(pSSM, 8192);
1076#else
1077 SSMR3Skip(pSSM, 8192);
1078#endif
1079 }
1080
1081 /*
1082 * Restore the VMCPU states. VCPU 0 is always started.
1083 */
1084 VMCPU_SET_STATE(pVM->apCpusR3[0], VMCPUSTATE_STARTED);
1085 for (VMCPUID i = 1; i < pVM->cCpus; i++)
1086 {
1087 bool fStarted;
1088 int rc = SSMR3GetBool(pSSM, &fStarted);
1089 if (RT_FAILURE(rc))
1090 return rc;
1091 VMCPU_SET_STATE(pVM->apCpusR3[i], fStarted ? VMCPUSTATE_STARTED : VMCPUSTATE_STOPPED);
1092 }
1093
1094 /* terminator */
1095 uint32_t u32;
1096 int rc = SSMR3GetU32(pSSM, &u32);
1097 if (RT_FAILURE(rc))
1098 return rc;
1099 if (u32 != UINT32_MAX)
1100 {
1101 AssertMsgFailed(("u32=%#x\n", u32));
1102 return VERR_SSM_DATA_UNIT_FORMAT_CHANGED;
1103 }
1104 return VINF_SUCCESS;
1105}
1106
1107
1108/**
1109 * Suspends the CPU yielder.
1110 *
1111 * @param pVM The cross context VM structure.
1112 */
1113VMMR3_INT_DECL(void) VMMR3YieldSuspend(PVM pVM)
1114{
1115#if 0 /* pointless when timers doesn't run on EMT */
1116 VMCPU_ASSERT_EMT(pVM->apCpusR3[0]);
1117 if (!pVM->vmm.s.cYieldResumeMillies)
1118 {
1119 uint64_t u64Now = TMTimerGet(pVM, pVM->vmm.s.hYieldTimer);
1120 uint64_t u64Expire = TMTimerGetExpire(pVM, pVM->vmm.s.hYieldTimer);
1121 if (u64Now >= u64Expire || u64Expire == ~(uint64_t)0)
1122 pVM->vmm.s.cYieldResumeMillies = pVM->vmm.s.cYieldEveryMillies;
1123 else
1124 pVM->vmm.s.cYieldResumeMillies = TMTimerToMilli(pVM, pVM->vmm.s.hYieldTimer, u64Expire - u64Now);
1125 TMTimerStop(pVM, pVM->vmm.s.hYieldTimer);
1126 }
1127 pVM->vmm.s.u64LastYield = RTTimeNanoTS();
1128#else
1129 RT_NOREF(pVM);
1130#endif
1131}
1132
1133
1134/**
1135 * Stops the CPU yielder.
1136 *
1137 * @param pVM The cross context VM structure.
1138 */
1139VMMR3_INT_DECL(void) VMMR3YieldStop(PVM pVM)
1140{
1141#if 0 /* pointless when timers doesn't run on EMT */
1142 if (!pVM->vmm.s.cYieldResumeMillies)
1143 TMTimerStop(pVM, pVM->vmm.s.hYieldTimer);
1144 pVM->vmm.s.cYieldResumeMillies = pVM->vmm.s.cYieldEveryMillies;
1145 pVM->vmm.s.u64LastYield = RTTimeNanoTS();
1146#else
1147 RT_NOREF(pVM);
1148#endif
1149}
1150
1151
1152/**
1153 * Resumes the CPU yielder when it has been a suspended or stopped.
1154 *
1155 * @param pVM The cross context VM structure.
1156 */
1157VMMR3_INT_DECL(void) VMMR3YieldResume(PVM pVM)
1158{
1159#if 0 /* pointless when timers doesn't run on EMT */
1160 if (pVM->vmm.s.cYieldResumeMillies)
1161 {
1162 TMTimerSetMillies(pVM, pVM->vmm.s.hYieldTimer, pVM->vmm.s.cYieldResumeMillies);
1163 pVM->vmm.s.cYieldResumeMillies = 0;
1164 }
1165#else
1166 RT_NOREF(pVM);
1167#endif
1168}
1169
1170
1171#if 0 /* pointless when timers doesn't run on EMT */
1172/**
1173 * @callback_method_impl{FNTMTIMERINT, EMT yielder}
1174 *
1175 * @todo This is a UNI core/thread thing, really... Should be reconsidered.
1176 */
1177static DECLCALLBACK(void) vmmR3YieldEMT(PVM pVM, TMTIMERHANDLE hTimer, void *pvUser)
1178{
1179 NOREF(pvUser);
1180
1181 /*
1182 * This really needs some careful tuning. While we shouldn't be too greedy since
1183 * that'll cause the rest of the system to stop up, we shouldn't be too nice either
1184 * because that'll cause us to stop up.
1185 *
1186 * The current logic is to use the default interval when there is no lag worth
1187 * mentioning, but when we start accumulating lag we don't bother yielding at all.
1188 *
1189 * (This depends on the TMCLOCK_VIRTUAL_SYNC to be scheduled before TMCLOCK_REAL
1190 * so the lag is up to date.)
1191 */
1192 const uint64_t u64Lag = TMVirtualSyncGetLag(pVM);
1193 if ( u64Lag < 50000000 /* 50ms */
1194 || ( u64Lag < 1000000000 /* 1s */
1195 && RTTimeNanoTS() - pVM->vmm.s.u64LastYield < 500000000 /* 500 ms */)
1196 )
1197 {
1198 uint64_t u64Elapsed = RTTimeNanoTS();
1199 pVM->vmm.s.u64LastYield = u64Elapsed;
1200
1201 RTThreadYield();
1202
1203#ifdef LOG_ENABLED
1204 u64Elapsed = RTTimeNanoTS() - u64Elapsed;
1205 Log(("vmmR3YieldEMT: %RI64 ns\n", u64Elapsed));
1206#endif
1207 }
1208 TMTimerSetMillies(pVM, hTimer, pVM->vmm.s.cYieldEveryMillies);
1209}
1210#endif
1211
1212
1213/**
1214 * Executes guest code (Intel VT-x and AMD-V).
1215 *
1216 * @param pVM The cross context VM structure.
1217 * @param pVCpu The cross context virtual CPU structure.
1218 */
1219VMMR3_INT_DECL(int) VMMR3HmRunGC(PVM pVM, PVMCPU pVCpu)
1220{
1221#if defined(VBOX_VMM_TARGET_ARMV8)
1222 /* We should actually never get here as the only execution engine is NEM. */
1223 RT_NOREF(pVM, pVCpu);
1224 AssertReleaseFailed();
1225 return VERR_NOT_SUPPORTED;
1226#else
1227 Log2(("VMMR3HmRunGC: (cs:rip=%04x:%RX64)\n", CPUMGetGuestCS(pVCpu), CPUMGetGuestRIP(pVCpu)));
1228
1229 int rc;
1230 do
1231 {
1232# ifdef NO_SUPCALLR0VMM
1233 rc = VERR_GENERAL_FAILURE;
1234# else
1235 rc = SUPR3CallVMMR0Fast(VMCC_GET_VMR0_FOR_CALL(pVM), VMMR0_DO_HM_RUN, pVCpu->idCpu);
1236 if (RT_LIKELY(rc == VINF_SUCCESS))
1237 rc = pVCpu->vmm.s.iLastGZRc;
1238# endif
1239 } while (rc == VINF_EM_RAW_INTERRUPT_HYPER);
1240
1241# if 0 /** @todo triggers too often */
1242 Assert(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_TO_R3));
1243# endif
1244
1245 /*
1246 * Flush the logs
1247 */
1248# ifdef LOG_ENABLED
1249 VMM_FLUSH_R0_LOG(pVM, pVCpu, &pVCpu->vmm.s.u.s.Logger, NULL);
1250# endif
1251 VMM_FLUSH_R0_LOG(pVM, pVCpu, &pVCpu->vmm.s.u.s.RelLogger, RTLogRelGetDefaultInstance());
1252 if (rc != VERR_VMM_RING0_ASSERTION)
1253 {
1254 Log2(("VMMR3HmRunGC: returns %Rrc (cs:rip=%04x:%RX64)\n", rc, CPUMGetGuestCS(pVCpu), CPUMGetGuestRIP(pVCpu)));
1255 return rc;
1256 }
1257 return vmmR3HandleRing0Assert(pVM, pVCpu);
1258#endif
1259}
1260
1261
1262/**
1263 * Perform one of the fast I/O control VMMR0 operation.
1264 *
1265 * @returns VBox strict status code.
1266 * @param pVM The cross context VM structure.
1267 * @param pVCpu The cross context virtual CPU structure.
1268 * @param enmOperation The operation to perform.
1269 */
1270VMMR3_INT_DECL(VBOXSTRICTRC) VMMR3CallR0EmtFast(PVM pVM, PVMCPU pVCpu, VMMR0OPERATION enmOperation)
1271{
1272 VBOXSTRICTRC rcStrict;
1273 do
1274 {
1275#ifdef NO_SUPCALLR0VMM
1276 rcStrict = VERR_GENERAL_FAILURE;
1277#else
1278 rcStrict = SUPR3CallVMMR0Fast(VMCC_GET_VMR0_FOR_CALL(pVM), enmOperation, pVCpu->idCpu);
1279 if (RT_LIKELY(rcStrict == VINF_SUCCESS))
1280 rcStrict = pVCpu->vmm.s.iLastGZRc;
1281#endif
1282 } while (rcStrict == VINF_EM_RAW_INTERRUPT_HYPER);
1283
1284 /*
1285 * Flush the logs
1286 */
1287#ifdef LOG_ENABLED
1288 VMM_FLUSH_R0_LOG(pVM, pVCpu, &pVCpu->vmm.s.u.s.Logger, NULL);
1289#endif
1290 VMM_FLUSH_R0_LOG(pVM, pVCpu, &pVCpu->vmm.s.u.s.RelLogger, RTLogRelGetDefaultInstance());
1291 if (rcStrict != VERR_VMM_RING0_ASSERTION)
1292 return rcStrict;
1293 return vmmR3HandleRing0Assert(pVM, pVCpu);
1294}
1295
1296
1297#if defined(VBOX_VMM_TARGET_ARMV8)
1298/**
1299 * VCPU worker for VMMR3CpuOn.
1300 *
1301 * @param pVM The cross context VM structure.
1302 * @param idCpu Virtual CPU to perform SIPI on.
1303 * @param GCPhysExecAddr The guest physical address to start executing at.
1304 * @param u64CtxId The context ID passed in x0/w0.
1305 */
1306static DECLCALLBACK(int) vmmR3CpuOn(PVM pVM, VMCPUID idCpu, RTGCPHYS GCPhysExecAddr, uint64_t u64CtxId)
1307{
1308 PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
1309 VMCPU_ASSERT_EMT(pVCpu);
1310
1311 if (EMGetState(pVCpu) != EMSTATE_WAIT_SIPI)
1312 return VINF_SUCCESS;
1313
1314 PCPUMCTX pCtx = CPUMQueryGuestCtxPtr(pVCpu);
1315
1316 pCtx->aGRegs[ARMV8_AARCH64_REG_X0].x = u64CtxId;
1317 pCtx->Pc.u64 = GCPhysExecAddr;
1318
1319 Log(("vmmR3CpuOn for VCPU %d with GCPhysExecAddr=%RGp u64CtxId=%#RX64\n", idCpu, GCPhysExecAddr, u64CtxId));
1320
1321# if 1 /* If we keep the EMSTATE_WAIT_SIPI method, then move this to EM.cpp. */
1322 EMSetState(pVCpu, EMSTATE_HALTED);
1323 return VINF_EM_RESCHEDULE;
1324# else /* And if we go the VMCPU::enmState way it can stay here. */
1325 VMCPU_ASSERT_STATE(pVCpu, VMCPUSTATE_STOPPED);
1326 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED);
1327 return VINF_SUCCESS;
1328# endif
1329}
1330
1331
1332/**
1333 * Sends a Startup IPI to the virtual CPU by setting CS:EIP into
1334 * vector-dependent state and unhalting processor.
1335 *
1336 * @param pVM The cross context VM structure.
1337 * @param idCpu Virtual CPU to perform SIPI on.
1338 * @param GCPhysExecAddr The guest physical address to start executing at.
1339 * @param u64CtxId The context ID passed in x0/w0.
1340 */
1341VMMR3_INT_DECL(void) VMMR3CpuOn(PVM pVM, VMCPUID idCpu, RTGCPHYS GCPhysExecAddr, uint64_t u64CtxId)
1342{
1343 AssertReturnVoid(idCpu < pVM->cCpus);
1344
1345 int rc = VMR3ReqCallNoWait(pVM, idCpu, (PFNRT)vmmR3CpuOn, 4, pVM, idCpu, GCPhysExecAddr, u64CtxId);
1346 AssertRC(rc);
1347}
1348#else
1349/**
1350 * VCPU worker for VMMR3SendStartupIpi.
1351 *
1352 * @param pVM The cross context VM structure.
1353 * @param idCpu Virtual CPU to perform SIPI on.
1354 * @param uVector The SIPI vector.
1355 */
1356static DECLCALLBACK(int) vmmR3SendStartupIpi(PVM pVM, VMCPUID idCpu, uint32_t uVector)
1357{
1358 PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
1359 VMCPU_ASSERT_EMT(pVCpu);
1360
1361 /*
1362 * In the INIT state, the target CPU is only responsive to an SIPI.
1363 * This is also true for when when the CPU is in VMX non-root mode.
1364 *
1365 * See AMD spec. 16.5 "Interprocessor Interrupts (IPI)".
1366 * See Intel spec. 26.6.2 "Activity State".
1367 */
1368 if (EMGetState(pVCpu) != EMSTATE_WAIT_SIPI)
1369 return VINF_SUCCESS;
1370
1371 PCPUMCTX pCtx = CPUMQueryGuestCtxPtr(pVCpu);
1372# ifdef VBOX_WITH_NESTED_HWVIRT_VMX
1373 if (CPUMIsGuestInVmxRootMode(pCtx))
1374 {
1375 /* If the CPU is in VMX non-root mode we must cause a VM-exit. */
1376 if (CPUMIsGuestInVmxNonRootMode(pCtx))
1377 return VBOXSTRICTRC_TODO(IEMExecVmxVmexitStartupIpi(pVCpu, uVector));
1378
1379 /* If the CPU is in VMX root mode (and not in VMX non-root mode) SIPIs are blocked. */
1380 return VINF_SUCCESS;
1381 }
1382# endif
1383
1384 pCtx->cs.Sel = uVector << 8;
1385 pCtx->cs.ValidSel = uVector << 8;
1386 pCtx->cs.fFlags = CPUMSELREG_FLAGS_VALID;
1387 pCtx->cs.u64Base = uVector << 12;
1388 pCtx->cs.u32Limit = UINT32_C(0x0000ffff);
1389 pCtx->rip = 0;
1390
1391 Log(("vmmR3SendSipi for VCPU %d with vector %x\n", idCpu, uVector));
1392
1393# if 1 /* If we keep the EMSTATE_WAIT_SIPI method, then move this to EM.cpp. */
1394 EMSetState(pVCpu, EMSTATE_HALTED);
1395 return VINF_EM_RESCHEDULE;
1396# else /* And if we go the VMCPU::enmState way it can stay here. */
1397 VMCPU_ASSERT_STATE(pVCpu, VMCPUSTATE_STOPPED);
1398 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED);
1399 return VINF_SUCCESS;
1400# endif
1401}
1402
1403
1404/**
1405 * VCPU worker for VMMR3SendInitIpi.
1406 *
1407 * @returns VBox status code.
1408 * @param pVM The cross context VM structure.
1409 * @param idCpu Virtual CPU to perform SIPI on.
1410 */
1411static DECLCALLBACK(int) vmmR3SendInitIpi(PVM pVM, VMCPUID idCpu)
1412{
1413 PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
1414 VMCPU_ASSERT_EMT(pVCpu);
1415
1416 Log(("vmmR3SendInitIpi for VCPU %d\n", idCpu));
1417
1418 /** @todo r=ramshankar: We should probably block INIT signal when the CPU is in
1419 * wait-for-SIPI state. Verify. */
1420
1421 /* If the CPU is in VMX non-root mode, INIT signals cause VM-exits. */
1422#ifdef VBOX_WITH_NESTED_HWVIRT_VMX
1423 PCCPUMCTX pCtx = CPUMQueryGuestCtxPtr(pVCpu);
1424 if (CPUMIsGuestInVmxNonRootMode(pCtx))
1425 return VBOXSTRICTRC_TODO(IEMExecVmxVmexit(pVCpu, VMX_EXIT_INIT_SIGNAL, 0 /* uExitQual */));
1426#endif
1427
1428 /** @todo Figure out how to handle a SVM nested-guest intercepts here for INIT
1429 * IPI (e.g. SVM_EXIT_INIT). */
1430
1431 PGMR3ResetCpu(pVM, pVCpu);
1432 PDMR3ResetCpu(pVCpu); /* Only clears pending interrupts force flags */
1433#if !defined(VBOX_VMM_TARGET_ARMV8)
1434 APICR3InitIpi(pVCpu);
1435#endif
1436 TRPMR3ResetCpu(pVCpu);
1437 CPUMR3ResetCpu(pVM, pVCpu);
1438 EMR3ResetCpu(pVCpu);
1439 HMR3ResetCpu(pVCpu);
1440 NEMR3ResetCpu(pVCpu, true /*fInitIpi*/);
1441
1442 /* This will trickle up on the target EMT. */
1443 return VINF_EM_WAIT_SIPI;
1444}
1445
1446
1447/**
1448 * Sends a Startup IPI to the virtual CPU by setting CS:EIP into
1449 * vector-dependent state and unhalting processor.
1450 *
1451 * @param pVM The cross context VM structure.
1452 * @param idCpu Virtual CPU to perform SIPI on.
1453 * @param uVector SIPI vector.
1454 */
1455VMMR3_INT_DECL(void) VMMR3SendStartupIpi(PVM pVM, VMCPUID idCpu, uint32_t uVector)
1456{
1457 AssertReturnVoid(idCpu < pVM->cCpus);
1458
1459 int rc = VMR3ReqCallNoWait(pVM, idCpu, (PFNRT)vmmR3SendStartupIpi, 3, pVM, idCpu, uVector);
1460 AssertRC(rc);
1461}
1462
1463
1464/**
1465 * Sends init IPI to the virtual CPU.
1466 *
1467 * @param pVM The cross context VM structure.
1468 * @param idCpu Virtual CPU to perform int IPI on.
1469 */
1470VMMR3_INT_DECL(void) VMMR3SendInitIpi(PVM pVM, VMCPUID idCpu)
1471{
1472 AssertReturnVoid(idCpu < pVM->cCpus);
1473
1474 int rc = VMR3ReqCallNoWait(pVM, idCpu, (PFNRT)vmmR3SendInitIpi, 2, pVM, idCpu);
1475 AssertRC(rc);
1476}
1477#endif
1478
1479
1480/**
1481 * Registers the guest memory range that can be used for patching.
1482 *
1483 * @returns VBox status code.
1484 * @param pVM The cross context VM structure.
1485 * @param pPatchMem Patch memory range.
1486 * @param cbPatchMem Size of the memory range.
1487 */
1488VMMR3DECL(int) VMMR3RegisterPatchMemory(PVM pVM, RTGCPTR pPatchMem, unsigned cbPatchMem)
1489{
1490 VM_ASSERT_EMT(pVM);
1491 if (HMIsEnabled(pVM))
1492 return HMR3EnablePatching(pVM, pPatchMem, cbPatchMem);
1493
1494 return VERR_NOT_SUPPORTED;
1495}
1496
1497
1498/**
1499 * Deregisters the guest memory range that can be used for patching.
1500 *
1501 * @returns VBox status code.
1502 * @param pVM The cross context VM structure.
1503 * @param pPatchMem Patch memory range.
1504 * @param cbPatchMem Size of the memory range.
1505 */
1506VMMR3DECL(int) VMMR3DeregisterPatchMemory(PVM pVM, RTGCPTR pPatchMem, unsigned cbPatchMem)
1507{
1508 if (HMIsEnabled(pVM))
1509 return HMR3DisablePatching(pVM, pPatchMem, cbPatchMem);
1510
1511 return VINF_SUCCESS;
1512}
1513
1514
1515/**
1516 * Common recursion handler for the other EMTs.
1517 *
1518 * @returns Strict VBox status code.
1519 * @param pVM The cross context VM structure.
1520 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1521 * @param rcStrict Current status code to be combined with the one
1522 * from this recursion and returned.
1523 */
1524static VBOXSTRICTRC vmmR3EmtRendezvousCommonRecursion(PVM pVM, PVMCPU pVCpu, VBOXSTRICTRC rcStrict)
1525{
1526 int rc2;
1527
1528 /*
1529 * We wait here while the initiator of this recursion reconfigures
1530 * everything. The last EMT to get in signals the initiator.
1531 */
1532 if (ASMAtomicIncU32(&pVM->vmm.s.cRendezvousEmtsRecursingPush) == pVM->cCpus)
1533 {
1534 rc2 = RTSemEventSignal(pVM->vmm.s.hEvtRendezvousRecursionPushCaller);
1535 AssertLogRelRC(rc2);
1536 }
1537
1538 rc2 = RTSemEventMultiWait(pVM->vmm.s.hEvtMulRendezvousRecursionPush, RT_INDEFINITE_WAIT);
1539 AssertLogRelRC(rc2);
1540
1541 /*
1542 * Do the normal rendezvous processing.
1543 */
1544 VBOXSTRICTRC rcStrict2 = vmmR3EmtRendezvousCommon(pVM, pVCpu, false /* fIsCaller */, pVM->vmm.s.fRendezvousFlags,
1545 pVM->vmm.s.pfnRendezvous, pVM->vmm.s.pvRendezvousUser);
1546
1547 /*
1548 * Wait for the initiator to restore everything.
1549 */
1550 rc2 = RTSemEventMultiWait(pVM->vmm.s.hEvtMulRendezvousRecursionPop, RT_INDEFINITE_WAIT);
1551 AssertLogRelRC(rc2);
1552
1553 /*
1554 * Last thread out of here signals the initiator.
1555 */
1556 if (ASMAtomicIncU32(&pVM->vmm.s.cRendezvousEmtsRecursingPop) == pVM->cCpus)
1557 {
1558 rc2 = RTSemEventSignal(pVM->vmm.s.hEvtRendezvousRecursionPopCaller);
1559 AssertLogRelRC(rc2);
1560 }
1561
1562 /*
1563 * Merge status codes and return.
1564 */
1565 AssertRC(VBOXSTRICTRC_VAL(rcStrict2));
1566 if ( rcStrict2 != VINF_SUCCESS
1567 && ( rcStrict == VINF_SUCCESS
1568 || rcStrict > rcStrict2))
1569 rcStrict = rcStrict2;
1570 return rcStrict;
1571}
1572
1573
1574/**
1575 * Count returns and have the last non-caller EMT wake up the caller.
1576 *
1577 * @returns VBox strict informational status code for EM scheduling. No failures
1578 * will be returned here, those are for the caller only.
1579 *
1580 * @param pVM The cross context VM structure.
1581 * @param rcStrict The current accumulated recursive status code,
1582 * to be merged with i32RendezvousStatus and
1583 * returned.
1584 */
1585DECL_FORCE_INLINE(VBOXSTRICTRC) vmmR3EmtRendezvousNonCallerReturn(PVM pVM, VBOXSTRICTRC rcStrict)
1586{
1587 VBOXSTRICTRC rcStrict2 = ASMAtomicReadS32(&pVM->vmm.s.i32RendezvousStatus);
1588
1589 uint32_t cReturned = ASMAtomicIncU32(&pVM->vmm.s.cRendezvousEmtsReturned);
1590 if (cReturned == pVM->cCpus - 1U)
1591 {
1592 int rc = RTSemEventSignal(pVM->vmm.s.hEvtRendezvousDoneCaller);
1593 AssertLogRelRC(rc);
1594 }
1595
1596 /*
1597 * Merge the status codes, ignoring error statuses in this code path.
1598 */
1599 AssertLogRelMsgReturn( rcStrict2 <= VINF_SUCCESS
1600 || (rcStrict2 >= VINF_EM_FIRST && rcStrict2 <= VINF_EM_LAST),
1601 ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict2)),
1602 VERR_IPE_UNEXPECTED_INFO_STATUS);
1603
1604 if (RT_SUCCESS(rcStrict2))
1605 {
1606 if ( rcStrict2 != VINF_SUCCESS
1607 && ( rcStrict == VINF_SUCCESS
1608 || rcStrict > rcStrict2))
1609 rcStrict = rcStrict2;
1610 }
1611 return rcStrict;
1612}
1613
1614
1615/**
1616 * Common worker for VMMR3EmtRendezvous and VMMR3EmtRendezvousFF.
1617 *
1618 * @returns VBox strict informational status code for EM scheduling. No failures
1619 * will be returned here, those are for the caller only. When
1620 * fIsCaller is set, VINF_SUCCESS is always returned.
1621 *
1622 * @param pVM The cross context VM structure.
1623 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1624 * @param fIsCaller Whether we're the VMMR3EmtRendezvous caller or
1625 * not.
1626 * @param fFlags The flags.
1627 * @param pfnRendezvous The callback.
1628 * @param pvUser The user argument for the callback.
1629 */
1630static VBOXSTRICTRC vmmR3EmtRendezvousCommon(PVM pVM, PVMCPU pVCpu, bool fIsCaller,
1631 uint32_t fFlags, PFNVMMEMTRENDEZVOUS pfnRendezvous, void *pvUser)
1632{
1633 int rc;
1634 VBOXSTRICTRC rcStrictRecursion = VINF_SUCCESS;
1635
1636 /*
1637 * Enter, the last EMT triggers the next callback phase.
1638 */
1639 uint32_t cEntered = ASMAtomicIncU32(&pVM->vmm.s.cRendezvousEmtsEntered);
1640 if (cEntered != pVM->cCpus)
1641 {
1642 if ((fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE)
1643 {
1644 /* Wait for our turn. */
1645 for (;;)
1646 {
1647 rc = RTSemEventWait(pVM->vmm.s.hEvtRendezvousEnterOneByOne, RT_INDEFINITE_WAIT);
1648 AssertLogRelRC(rc);
1649 if (!pVM->vmm.s.fRendezvousRecursion)
1650 break;
1651 rcStrictRecursion = vmmR3EmtRendezvousCommonRecursion(pVM, pVCpu, rcStrictRecursion);
1652 }
1653 }
1654 else if ((fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ALL_AT_ONCE)
1655 {
1656 /* Wait for the last EMT to arrive and wake everyone up. */
1657 rc = RTSemEventMultiWait(pVM->vmm.s.hEvtMulRendezvousEnterAllAtOnce, RT_INDEFINITE_WAIT);
1658 AssertLogRelRC(rc);
1659 Assert(!pVM->vmm.s.fRendezvousRecursion);
1660 }
1661 else if ( (fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING
1662 || (fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING)
1663 {
1664 /* Wait for our turn. */
1665 for (;;)
1666 {
1667 rc = RTSemEventWait(pVM->vmm.s.pahEvtRendezvousEnterOrdered[pVCpu->idCpu], RT_INDEFINITE_WAIT);
1668 AssertLogRelRC(rc);
1669 if (!pVM->vmm.s.fRendezvousRecursion)
1670 break;
1671 rcStrictRecursion = vmmR3EmtRendezvousCommonRecursion(pVM, pVCpu, rcStrictRecursion);
1672 }
1673 }
1674 else
1675 {
1676 Assert((fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE);
1677
1678 /*
1679 * The execute once is handled specially to optimize the code flow.
1680 *
1681 * The last EMT to arrive will perform the callback and the other
1682 * EMTs will wait on the Done/DoneCaller semaphores (instead of
1683 * the EnterOneByOne/AllAtOnce) in the meanwhile. When the callback
1684 * returns, that EMT will initiate the normal return sequence.
1685 */
1686 if (!fIsCaller)
1687 {
1688 for (;;)
1689 {
1690 rc = RTSemEventMultiWait(pVM->vmm.s.hEvtMulRendezvousDone, RT_INDEFINITE_WAIT);
1691 AssertLogRelRC(rc);
1692 if (!pVM->vmm.s.fRendezvousRecursion)
1693 break;
1694 rcStrictRecursion = vmmR3EmtRendezvousCommonRecursion(pVM, pVCpu, rcStrictRecursion);
1695 }
1696
1697 return vmmR3EmtRendezvousNonCallerReturn(pVM, rcStrictRecursion);
1698 }
1699 return VINF_SUCCESS;
1700 }
1701 }
1702 else
1703 {
1704 /*
1705 * All EMTs are waiting, clear the FF and take action according to the
1706 * execution method.
1707 */
1708 VM_FF_CLEAR(pVM, VM_FF_EMT_RENDEZVOUS);
1709
1710 if ((fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ALL_AT_ONCE)
1711 {
1712 /* Wake up everyone. */
1713 rc = RTSemEventMultiSignal(pVM->vmm.s.hEvtMulRendezvousEnterAllAtOnce);
1714 AssertLogRelRC(rc);
1715 }
1716 else if ( (fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING
1717 || (fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING)
1718 {
1719 /* Figure out who to wake up and wake it up. If it's ourself, then
1720 it's easy otherwise wait for our turn. */
1721 VMCPUID iFirst = (fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING
1722 ? 0
1723 : pVM->cCpus - 1U;
1724 if (pVCpu->idCpu != iFirst)
1725 {
1726 rc = RTSemEventSignal(pVM->vmm.s.pahEvtRendezvousEnterOrdered[iFirst]);
1727 AssertLogRelRC(rc);
1728 for (;;)
1729 {
1730 rc = RTSemEventWait(pVM->vmm.s.pahEvtRendezvousEnterOrdered[pVCpu->idCpu], RT_INDEFINITE_WAIT);
1731 AssertLogRelRC(rc);
1732 if (!pVM->vmm.s.fRendezvousRecursion)
1733 break;
1734 rcStrictRecursion = vmmR3EmtRendezvousCommonRecursion(pVM, pVCpu, rcStrictRecursion);
1735 }
1736 }
1737 }
1738 /* else: execute the handler on the current EMT and wake up one or more threads afterwards. */
1739 }
1740
1741
1742 /*
1743 * Do the callback and update the status if necessary.
1744 */
1745 if ( !(fFlags & VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR)
1746 || RT_SUCCESS(ASMAtomicUoReadS32(&pVM->vmm.s.i32RendezvousStatus)) )
1747 {
1748 VBOXSTRICTRC rcStrict2 = pfnRendezvous(pVM, pVCpu, pvUser);
1749 if (rcStrict2 != VINF_SUCCESS)
1750 {
1751 AssertLogRelMsg( rcStrict2 <= VINF_SUCCESS
1752 || (rcStrict2 >= VINF_EM_FIRST && rcStrict2 <= VINF_EM_LAST),
1753 ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict2)));
1754 int32_t i32RendezvousStatus;
1755 do
1756 {
1757 i32RendezvousStatus = ASMAtomicUoReadS32(&pVM->vmm.s.i32RendezvousStatus);
1758 if ( rcStrict2 == i32RendezvousStatus
1759 || RT_FAILURE(i32RendezvousStatus)
1760 || ( i32RendezvousStatus != VINF_SUCCESS
1761 && rcStrict2 > i32RendezvousStatus))
1762 break;
1763 } while (!ASMAtomicCmpXchgS32(&pVM->vmm.s.i32RendezvousStatus, VBOXSTRICTRC_VAL(rcStrict2), i32RendezvousStatus));
1764 }
1765 }
1766
1767 /*
1768 * Increment the done counter and take action depending on whether we're
1769 * the last to finish callback execution.
1770 */
1771 uint32_t cDone = ASMAtomicIncU32(&pVM->vmm.s.cRendezvousEmtsDone);
1772 if ( cDone != pVM->cCpus
1773 && (fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) != VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE)
1774 {
1775 /* Signal the next EMT? */
1776 if ((fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE)
1777 {
1778 rc = RTSemEventSignal(pVM->vmm.s.hEvtRendezvousEnterOneByOne);
1779 AssertLogRelRC(rc);
1780 }
1781 else if ((fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING)
1782 {
1783 Assert(cDone == pVCpu->idCpu + 1U);
1784 rc = RTSemEventSignal(pVM->vmm.s.pahEvtRendezvousEnterOrdered[pVCpu->idCpu + 1U]);
1785 AssertLogRelRC(rc);
1786 }
1787 else if ((fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING)
1788 {
1789 Assert(pVM->cCpus - cDone == pVCpu->idCpu);
1790 rc = RTSemEventSignal(pVM->vmm.s.pahEvtRendezvousEnterOrdered[pVM->cCpus - cDone - 1U]);
1791 AssertLogRelRC(rc);
1792 }
1793
1794 /* Wait for the rest to finish (the caller waits on hEvtRendezvousDoneCaller). */
1795 if (!fIsCaller)
1796 {
1797 for (;;)
1798 {
1799 rc = RTSemEventMultiWait(pVM->vmm.s.hEvtMulRendezvousDone, RT_INDEFINITE_WAIT);
1800 AssertLogRelRC(rc);
1801 if (!pVM->vmm.s.fRendezvousRecursion)
1802 break;
1803 rcStrictRecursion = vmmR3EmtRendezvousCommonRecursion(pVM, pVCpu, rcStrictRecursion);
1804 }
1805 }
1806 }
1807 else
1808 {
1809 /* Callback execution is all done, tell the rest to return. */
1810 rc = RTSemEventMultiSignal(pVM->vmm.s.hEvtMulRendezvousDone);
1811 AssertLogRelRC(rc);
1812 }
1813
1814 if (!fIsCaller)
1815 return vmmR3EmtRendezvousNonCallerReturn(pVM, rcStrictRecursion);
1816 return rcStrictRecursion;
1817}
1818
1819
1820/**
1821 * Called in response to VM_FF_EMT_RENDEZVOUS.
1822 *
1823 * @returns VBox strict status code - EM scheduling. No errors will be returned
1824 * here, nor will any non-EM scheduling status codes be returned.
1825 *
1826 * @param pVM The cross context VM structure.
1827 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
1828 *
1829 * @thread EMT
1830 */
1831VMMR3_INT_DECL(int) VMMR3EmtRendezvousFF(PVM pVM, PVMCPU pVCpu)
1832{
1833 Assert(!pVCpu->vmm.s.fInRendezvous);
1834 Log(("VMMR3EmtRendezvousFF: EMT%#u\n", pVCpu->idCpu));
1835 pVCpu->vmm.s.fInRendezvous = true;
1836 VBOXSTRICTRC rcStrict = vmmR3EmtRendezvousCommon(pVM, pVCpu, false /* fIsCaller */, pVM->vmm.s.fRendezvousFlags,
1837 pVM->vmm.s.pfnRendezvous, pVM->vmm.s.pvRendezvousUser);
1838 pVCpu->vmm.s.fInRendezvous = false;
1839 Log(("VMMR3EmtRendezvousFF: EMT%#u returns %Rrc\n", pVCpu->idCpu, VBOXSTRICTRC_VAL(rcStrict)));
1840 return VBOXSTRICTRC_TODO(rcStrict);
1841}
1842
1843
1844/**
1845 * Helper for resetting an single wakeup event sempahore.
1846 *
1847 * @returns VERR_TIMEOUT on success, RTSemEventWait status otherwise.
1848 * @param hEvt The event semaphore to reset.
1849 */
1850static int vmmR3HlpResetEvent(RTSEMEVENT hEvt)
1851{
1852 for (uint32_t cLoops = 0; ; cLoops++)
1853 {
1854 int rc = RTSemEventWait(hEvt, 0 /*cMsTimeout*/);
1855 if (rc != VINF_SUCCESS || cLoops > _4K)
1856 return rc;
1857 }
1858}
1859
1860
1861/**
1862 * Worker for VMMR3EmtRendezvous that handles recursion.
1863 *
1864 * @returns VBox strict status code. This will be the first error,
1865 * VINF_SUCCESS, or an EM scheduling status code.
1866 *
1867 * @param pVM The cross context VM structure.
1868 * @param pVCpu The cross context virtual CPU structure of the
1869 * calling EMT.
1870 * @param fFlags Flags indicating execution methods. See
1871 * grp_VMMR3EmtRendezvous_fFlags.
1872 * @param pfnRendezvous The callback.
1873 * @param pvUser User argument for the callback.
1874 *
1875 * @thread EMT(pVCpu)
1876 */
1877static VBOXSTRICTRC vmmR3EmtRendezvousRecursive(PVM pVM, PVMCPU pVCpu, uint32_t fFlags,
1878 PFNVMMEMTRENDEZVOUS pfnRendezvous, void *pvUser)
1879{
1880 Log(("vmmR3EmtRendezvousRecursive: %#x EMT#%u depth=%d\n", fFlags, pVCpu->idCpu, pVM->vmm.s.cRendezvousRecursions));
1881 AssertLogRelReturn(pVM->vmm.s.cRendezvousRecursions < 3, VERR_DEADLOCK);
1882 Assert(pVCpu->vmm.s.fInRendezvous);
1883
1884 /*
1885 * Save the current state.
1886 */
1887 uint32_t const fParentFlags = pVM->vmm.s.fRendezvousFlags;
1888 uint32_t const cParentDone = pVM->vmm.s.cRendezvousEmtsDone;
1889 int32_t const iParentStatus = pVM->vmm.s.i32RendezvousStatus;
1890 PFNVMMEMTRENDEZVOUS const pfnParent = pVM->vmm.s.pfnRendezvous;
1891 void * const pvParentUser = pVM->vmm.s.pvRendezvousUser;
1892
1893 /*
1894 * Check preconditions and save the current state.
1895 */
1896 AssertReturn( (fParentFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING
1897 || (fParentFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING
1898 || (fParentFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE
1899 || (fParentFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE,
1900 VERR_INTERNAL_ERROR);
1901 AssertReturn(pVM->vmm.s.cRendezvousEmtsEntered == pVM->cCpus, VERR_INTERNAL_ERROR_2);
1902 AssertReturn(pVM->vmm.s.cRendezvousEmtsReturned == 0, VERR_INTERNAL_ERROR_3);
1903
1904 /*
1905 * Reset the recursion prep and pop semaphores.
1906 */
1907 int rc = RTSemEventMultiReset(pVM->vmm.s.hEvtMulRendezvousRecursionPush);
1908 AssertLogRelRCReturn(rc, rc);
1909 rc = RTSemEventMultiReset(pVM->vmm.s.hEvtMulRendezvousRecursionPop);
1910 AssertLogRelRCReturn(rc, rc);
1911 rc = vmmR3HlpResetEvent(pVM->vmm.s.hEvtRendezvousRecursionPushCaller);
1912 AssertLogRelMsgReturn(rc == VERR_TIMEOUT, ("%Rrc\n", rc), RT_FAILURE_NP(rc) ? rc : VERR_IPE_UNEXPECTED_INFO_STATUS);
1913 rc = vmmR3HlpResetEvent(pVM->vmm.s.hEvtRendezvousRecursionPopCaller);
1914 AssertLogRelMsgReturn(rc == VERR_TIMEOUT, ("%Rrc\n", rc), RT_FAILURE_NP(rc) ? rc : VERR_IPE_UNEXPECTED_INFO_STATUS);
1915
1916 /*
1917 * Usher the other thread into the recursion routine.
1918 */
1919 ASMAtomicWriteU32(&pVM->vmm.s.cRendezvousEmtsRecursingPush, 0);
1920 ASMAtomicWriteBool(&pVM->vmm.s.fRendezvousRecursion, true);
1921
1922 uint32_t cLeft = pVM->cCpus - (cParentDone + 1U);
1923 if ((fParentFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE)
1924 while (cLeft-- > 0)
1925 {
1926 rc = RTSemEventSignal(pVM->vmm.s.hEvtRendezvousEnterOneByOne);
1927 AssertLogRelRC(rc);
1928 }
1929 else if ((fParentFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING)
1930 {
1931 Assert(cLeft == pVM->cCpus - (pVCpu->idCpu + 1U));
1932 for (VMCPUID iCpu = pVCpu->idCpu + 1U; iCpu < pVM->cCpus; iCpu++)
1933 {
1934 rc = RTSemEventSignal(pVM->vmm.s.pahEvtRendezvousEnterOrdered[iCpu]);
1935 AssertLogRelRC(rc);
1936 }
1937 }
1938 else if ((fParentFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING)
1939 {
1940 Assert(cLeft == pVCpu->idCpu);
1941 for (VMCPUID iCpu = pVCpu->idCpu; iCpu > 0; iCpu--)
1942 {
1943 rc = RTSemEventSignal(pVM->vmm.s.pahEvtRendezvousEnterOrdered[iCpu - 1U]);
1944 AssertLogRelRC(rc);
1945 }
1946 }
1947 else
1948 AssertLogRelReturn((fParentFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE,
1949 VERR_INTERNAL_ERROR_4);
1950
1951 rc = RTSemEventMultiSignal(pVM->vmm.s.hEvtMulRendezvousDone);
1952 AssertLogRelRC(rc);
1953 rc = RTSemEventSignal(pVM->vmm.s.hEvtRendezvousDoneCaller);
1954 AssertLogRelRC(rc);
1955
1956
1957 /*
1958 * Wait for the EMTs to wake up and get out of the parent rendezvous code.
1959 */
1960 if (ASMAtomicIncU32(&pVM->vmm.s.cRendezvousEmtsRecursingPush) != pVM->cCpus)
1961 {
1962 rc = RTSemEventWait(pVM->vmm.s.hEvtRendezvousRecursionPushCaller, RT_INDEFINITE_WAIT);
1963 AssertLogRelRC(rc);
1964 }
1965
1966 ASMAtomicWriteBool(&pVM->vmm.s.fRendezvousRecursion, false);
1967
1968 /*
1969 * Clear the slate and setup the new rendezvous.
1970 */
1971 for (VMCPUID i = 0; i < pVM->cCpus; i++)
1972 {
1973 rc = vmmR3HlpResetEvent(pVM->vmm.s.pahEvtRendezvousEnterOrdered[i]);
1974 AssertLogRelMsg(rc == VERR_TIMEOUT, ("%Rrc\n", rc));
1975 }
1976 rc = vmmR3HlpResetEvent(pVM->vmm.s.hEvtRendezvousEnterOneByOne); AssertLogRelMsg(rc == VERR_TIMEOUT, ("%Rrc\n", rc));
1977 rc = RTSemEventMultiReset(pVM->vmm.s.hEvtMulRendezvousEnterAllAtOnce); AssertLogRelRC(rc);
1978 rc = RTSemEventMultiReset(pVM->vmm.s.hEvtMulRendezvousDone); AssertLogRelRC(rc);
1979 rc = vmmR3HlpResetEvent(pVM->vmm.s.hEvtRendezvousDoneCaller); AssertLogRelMsg(rc == VERR_TIMEOUT, ("%Rrc\n", rc));
1980
1981 ASMAtomicWriteU32(&pVM->vmm.s.cRendezvousEmtsEntered, 0);
1982 ASMAtomicWriteU32(&pVM->vmm.s.cRendezvousEmtsDone, 0);
1983 ASMAtomicWriteU32(&pVM->vmm.s.cRendezvousEmtsReturned, 0);
1984 ASMAtomicWriteS32(&pVM->vmm.s.i32RendezvousStatus, VINF_SUCCESS);
1985 ASMAtomicWritePtr((void * volatile *)&pVM->vmm.s.pfnRendezvous, (void *)(uintptr_t)pfnRendezvous);
1986 ASMAtomicWritePtr(&pVM->vmm.s.pvRendezvousUser, pvUser);
1987 ASMAtomicWriteU32(&pVM->vmm.s.fRendezvousFlags, fFlags);
1988 ASMAtomicIncU32(&pVM->vmm.s.cRendezvousRecursions);
1989
1990 /*
1991 * We're ready to go now, do normal rendezvous processing.
1992 */
1993 rc = RTSemEventMultiSignal(pVM->vmm.s.hEvtMulRendezvousRecursionPush);
1994 AssertLogRelRC(rc);
1995
1996 VBOXSTRICTRC rcStrict = vmmR3EmtRendezvousCommon(pVM, pVCpu, true /*fIsCaller*/, fFlags, pfnRendezvous, pvUser);
1997
1998 /*
1999 * The caller waits for the other EMTs to be done, return and waiting on the
2000 * pop semaphore.
2001 */
2002 for (;;)
2003 {
2004 rc = RTSemEventWait(pVM->vmm.s.hEvtRendezvousDoneCaller, RT_INDEFINITE_WAIT);
2005 AssertLogRelRC(rc);
2006 if (!pVM->vmm.s.fRendezvousRecursion)
2007 break;
2008 rcStrict = vmmR3EmtRendezvousCommonRecursion(pVM, pVCpu, rcStrict);
2009 }
2010
2011 /*
2012 * Get the return code and merge it with the above recursion status.
2013 */
2014 VBOXSTRICTRC rcStrict2 = pVM->vmm.s.i32RendezvousStatus;
2015 if ( rcStrict2 != VINF_SUCCESS
2016 && ( rcStrict == VINF_SUCCESS
2017 || rcStrict > rcStrict2))
2018 rcStrict = rcStrict2;
2019
2020 /*
2021 * Restore the parent rendezvous state.
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, pVM->cCpus);
2034 ASMAtomicWriteU32(&pVM->vmm.s.cRendezvousEmtsReturned, 0);
2035 ASMAtomicWriteU32(&pVM->vmm.s.cRendezvousEmtsDone, cParentDone);
2036 ASMAtomicWriteS32(&pVM->vmm.s.i32RendezvousStatus, iParentStatus);
2037 ASMAtomicWriteU32(&pVM->vmm.s.fRendezvousFlags, fParentFlags);
2038 ASMAtomicWritePtr(&pVM->vmm.s.pvRendezvousUser, pvParentUser);
2039 ASMAtomicWritePtr((void * volatile *)&pVM->vmm.s.pfnRendezvous, (void *)(uintptr_t)pfnParent);
2040
2041 /*
2042 * Usher the other EMTs back to their parent recursion routine, waiting
2043 * for them to all get there before we return (makes sure they've been
2044 * scheduled and are past the pop event sem, see below).
2045 */
2046 ASMAtomicWriteU32(&pVM->vmm.s.cRendezvousEmtsRecursingPop, 0);
2047 rc = RTSemEventMultiSignal(pVM->vmm.s.hEvtMulRendezvousRecursionPop);
2048 AssertLogRelRC(rc);
2049
2050 if (ASMAtomicIncU32(&pVM->vmm.s.cRendezvousEmtsRecursingPop) != pVM->cCpus)
2051 {
2052 rc = RTSemEventWait(pVM->vmm.s.hEvtRendezvousRecursionPopCaller, RT_INDEFINITE_WAIT);
2053 AssertLogRelRC(rc);
2054 }
2055
2056 /*
2057 * We must reset the pop semaphore on the way out (doing the pop caller too,
2058 * just in case). The parent may be another recursion.
2059 */
2060 rc = RTSemEventMultiReset(pVM->vmm.s.hEvtMulRendezvousRecursionPop); AssertLogRelRC(rc);
2061 rc = vmmR3HlpResetEvent(pVM->vmm.s.hEvtRendezvousRecursionPopCaller); AssertLogRelMsg(rc == VERR_TIMEOUT, ("%Rrc\n", rc));
2062
2063 ASMAtomicDecU32(&pVM->vmm.s.cRendezvousRecursions);
2064
2065 Log(("vmmR3EmtRendezvousRecursive: %#x EMT#%u depth=%d returns %Rrc\n",
2066 fFlags, pVCpu->idCpu, pVM->vmm.s.cRendezvousRecursions, VBOXSTRICTRC_VAL(rcStrict)));
2067 return rcStrict;
2068}
2069
2070
2071/**
2072 * EMT rendezvous.
2073 *
2074 * Gathers all the EMTs and execute some code on each of them, either in a one
2075 * by one fashion or all at once.
2076 *
2077 * @returns VBox strict status code. This will be the first error,
2078 * VINF_SUCCESS, or an EM scheduling status code.
2079 *
2080 * @retval VERR_DEADLOCK if recursion is attempted using a rendezvous type that
2081 * doesn't support it or if the recursion is too deep.
2082 *
2083 * @param pVM The cross context VM structure.
2084 * @param fFlags Flags indicating execution methods. See
2085 * grp_VMMR3EmtRendezvous_fFlags. The one-by-one,
2086 * descending and ascending rendezvous types support
2087 * recursion from inside @a pfnRendezvous.
2088 * @param pfnRendezvous The callback.
2089 * @param pvUser User argument for the callback.
2090 *
2091 * @thread Any.
2092 */
2093VMMR3DECL(int) VMMR3EmtRendezvous(PVM pVM, uint32_t fFlags, PFNVMMEMTRENDEZVOUS pfnRendezvous, void *pvUser)
2094{
2095 /*
2096 * Validate input.
2097 */
2098 AssertReturn(pVM, VERR_INVALID_VM_HANDLE);
2099 AssertMsg( (fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) != VMMEMTRENDEZVOUS_FLAGS_TYPE_INVALID
2100 && (fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) <= VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING
2101 && !(fFlags & ~VMMEMTRENDEZVOUS_FLAGS_VALID_MASK), ("%#x\n", fFlags));
2102 AssertMsg( !(fFlags & VMMEMTRENDEZVOUS_FLAGS_STOP_ON_ERROR)
2103 || ( (fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) != VMMEMTRENDEZVOUS_FLAGS_TYPE_ALL_AT_ONCE
2104 && (fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) != VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE),
2105 ("type %u\n", fFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK));
2106
2107 VBOXSTRICTRC rcStrict;
2108 PVMCPU pVCpu = VMMGetCpu(pVM);
2109 if (!pVCpu)
2110 {
2111 /*
2112 * Forward the request to an EMT thread.
2113 */
2114 Log(("VMMR3EmtRendezvous: %#x non-EMT\n", fFlags));
2115 if (!(fFlags & VMMEMTRENDEZVOUS_FLAGS_PRIORITY))
2116 rcStrict = VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)VMMR3EmtRendezvous, 4, pVM, fFlags, pfnRendezvous, pvUser);
2117 else
2118 rcStrict = VMR3ReqPriorityCallWait(pVM, VMCPUID_ANY, (PFNRT)VMMR3EmtRendezvous, 4, pVM, fFlags, pfnRendezvous, pvUser);
2119 Log(("VMMR3EmtRendezvous: %#x non-EMT returns %Rrc\n", fFlags, VBOXSTRICTRC_VAL(rcStrict)));
2120 }
2121 else if ( pVM->cCpus == 1
2122 || ( pVM->enmVMState == VMSTATE_DESTROYING
2123 && VMR3GetActiveEmts(pVM->pUVM) < pVM->cCpus ) )
2124 {
2125 /*
2126 * Shortcut for the single EMT case.
2127 *
2128 * We also ends up here if EMT(0) (or others) tries to issue a rendezvous
2129 * during vmR3Destroy after other emulation threads have started terminating.
2130 */
2131 if (!pVCpu->vmm.s.fInRendezvous)
2132 {
2133 Log(("VMMR3EmtRendezvous: %#x EMT (uni)\n", fFlags));
2134 pVCpu->vmm.s.fInRendezvous = true;
2135 pVM->vmm.s.fRendezvousFlags = fFlags;
2136 rcStrict = pfnRendezvous(pVM, pVCpu, pvUser);
2137 pVCpu->vmm.s.fInRendezvous = false;
2138 }
2139 else
2140 {
2141 /* Recursion. Do the same checks as in the SMP case. */
2142 Log(("VMMR3EmtRendezvous: %#x EMT (uni), recursion depth=%d\n", fFlags, pVM->vmm.s.cRendezvousRecursions));
2143 uint32_t fType = pVM->vmm.s.fRendezvousFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK;
2144 AssertLogRelReturn( !pVCpu->vmm.s.fInRendezvous
2145 || fType == VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING
2146 || fType == VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING
2147 || fType == VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE
2148 || fType == VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE
2149 , VERR_DEADLOCK);
2150
2151 AssertLogRelReturn(pVM->vmm.s.cRendezvousRecursions < 3, VERR_DEADLOCK);
2152 pVM->vmm.s.cRendezvousRecursions++;
2153 uint32_t const fParentFlags = pVM->vmm.s.fRendezvousFlags;
2154 pVM->vmm.s.fRendezvousFlags = fFlags;
2155
2156 rcStrict = pfnRendezvous(pVM, pVCpu, pvUser);
2157
2158 pVM->vmm.s.fRendezvousFlags = fParentFlags;
2159 pVM->vmm.s.cRendezvousRecursions--;
2160 }
2161 Log(("VMMR3EmtRendezvous: %#x EMT (uni) returns %Rrc\n", fFlags, VBOXSTRICTRC_VAL(rcStrict)));
2162 }
2163 else
2164 {
2165 /*
2166 * Spin lock. If busy, check for recursion, if not recursing wait for
2167 * the other EMT to finish while keeping a lookout for the RENDEZVOUS FF.
2168 */
2169 int rc;
2170 rcStrict = VINF_SUCCESS;
2171 if (RT_UNLIKELY(!ASMAtomicCmpXchgU32(&pVM->vmm.s.u32RendezvousLock, 0x77778888, 0)))
2172 {
2173 /* Allow recursion in some cases. */
2174 if ( pVCpu->vmm.s.fInRendezvous
2175 && ( (pVM->vmm.s.fRendezvousFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ASCENDING
2176 || (pVM->vmm.s.fRendezvousFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_DESCENDING
2177 || (pVM->vmm.s.fRendezvousFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ONE_BY_ONE
2178 || (pVM->vmm.s.fRendezvousFlags & VMMEMTRENDEZVOUS_FLAGS_TYPE_MASK) == VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE
2179 ))
2180 return VBOXSTRICTRC_TODO(vmmR3EmtRendezvousRecursive(pVM, pVCpu, fFlags, pfnRendezvous, pvUser));
2181
2182 AssertLogRelMsgReturn(!pVCpu->vmm.s.fInRendezvous, ("fRendezvousFlags=%#x\n", pVM->vmm.s.fRendezvousFlags),
2183 VERR_DEADLOCK);
2184
2185 Log(("VMMR3EmtRendezvous: %#x EMT#%u, waiting for lock...\n", fFlags, pVCpu->idCpu));
2186 while (!ASMAtomicCmpXchgU32(&pVM->vmm.s.u32RendezvousLock, 0x77778888, 0))
2187 {
2188 if (VM_FF_IS_SET(pVM, VM_FF_EMT_RENDEZVOUS))
2189 {
2190 rc = VMMR3EmtRendezvousFF(pVM, pVCpu);
2191 if ( rc != VINF_SUCCESS
2192 && ( rcStrict == VINF_SUCCESS
2193 || rcStrict > rc))
2194 rcStrict = rc;
2195 /** @todo Perhaps deal with termination here? */
2196 }
2197 ASMNopPause();
2198 }
2199 }
2200
2201 Log(("VMMR3EmtRendezvous: %#x EMT#%u\n", fFlags, pVCpu->idCpu));
2202 Assert(!VM_FF_IS_SET(pVM, VM_FF_EMT_RENDEZVOUS));
2203 Assert(!pVCpu->vmm.s.fInRendezvous);
2204 pVCpu->vmm.s.fInRendezvous = true;
2205
2206 /*
2207 * Clear the slate and setup the rendezvous. This is a semaphore ping-pong orgy. :-)
2208 */
2209 for (VMCPUID i = 0; i < pVM->cCpus; i++)
2210 {
2211 rc = RTSemEventWait(pVM->vmm.s.pahEvtRendezvousEnterOrdered[i], 0);
2212 AssertLogRelMsg(rc == VERR_TIMEOUT || rc == VINF_SUCCESS, ("%Rrc\n", rc));
2213 }
2214 rc = RTSemEventWait(pVM->vmm.s.hEvtRendezvousEnterOneByOne, 0); AssertLogRelMsg(rc == VERR_TIMEOUT || rc == VINF_SUCCESS, ("%Rrc\n", rc));
2215 rc = RTSemEventMultiReset(pVM->vmm.s.hEvtMulRendezvousEnterAllAtOnce); AssertLogRelRC(rc);
2216 rc = RTSemEventMultiReset(pVM->vmm.s.hEvtMulRendezvousDone); AssertLogRelRC(rc);
2217 rc = RTSemEventWait(pVM->vmm.s.hEvtRendezvousDoneCaller, 0); AssertLogRelMsg(rc == VERR_TIMEOUT || rc == VINF_SUCCESS, ("%Rrc\n", rc));
2218 ASMAtomicWriteU32(&pVM->vmm.s.cRendezvousEmtsEntered, 0);
2219 ASMAtomicWriteU32(&pVM->vmm.s.cRendezvousEmtsDone, 0);
2220 ASMAtomicWriteU32(&pVM->vmm.s.cRendezvousEmtsReturned, 0);
2221 ASMAtomicWriteS32(&pVM->vmm.s.i32RendezvousStatus, VINF_SUCCESS);
2222 ASMAtomicWritePtr((void * volatile *)&pVM->vmm.s.pfnRendezvous, (void *)(uintptr_t)pfnRendezvous);
2223 ASMAtomicWritePtr(&pVM->vmm.s.pvRendezvousUser, pvUser);
2224 ASMAtomicWriteU32(&pVM->vmm.s.fRendezvousFlags, fFlags);
2225
2226 /*
2227 * Set the FF and poke the other EMTs.
2228 */
2229 VM_FF_SET(pVM, VM_FF_EMT_RENDEZVOUS);
2230 VMR3NotifyGlobalFFU(pVM->pUVM, VMNOTIFYFF_FLAGS_POKE);
2231
2232 /*
2233 * Do the same ourselves.
2234 */
2235 VBOXSTRICTRC rcStrict2 = vmmR3EmtRendezvousCommon(pVM, pVCpu, true /* fIsCaller */, fFlags, pfnRendezvous, pvUser);
2236
2237 /*
2238 * The caller waits for the other EMTs to be done and return before doing
2239 * the cleanup. This makes away with wakeup / reset races we would otherwise
2240 * risk in the multiple release event semaphore code (hEvtRendezvousDoneCaller).
2241 */
2242 for (;;)
2243 {
2244 rc = RTSemEventWait(pVM->vmm.s.hEvtRendezvousDoneCaller, RT_INDEFINITE_WAIT);
2245 AssertLogRelRC(rc);
2246 if (!pVM->vmm.s.fRendezvousRecursion)
2247 break;
2248 rcStrict2 = vmmR3EmtRendezvousCommonRecursion(pVM, pVCpu, rcStrict2);
2249 }
2250
2251 /*
2252 * Get the return code and clean up a little bit.
2253 */
2254 VBOXSTRICTRC rcStrict3 = pVM->vmm.s.i32RendezvousStatus;
2255 ASMAtomicWriteNullPtr((void * volatile *)&pVM->vmm.s.pfnRendezvous);
2256
2257 ASMAtomicWriteU32(&pVM->vmm.s.u32RendezvousLock, 0);
2258 pVCpu->vmm.s.fInRendezvous = false;
2259
2260 /*
2261 * Merge rcStrict, rcStrict2 and rcStrict3.
2262 */
2263 AssertRC(VBOXSTRICTRC_VAL(rcStrict));
2264 AssertRC(VBOXSTRICTRC_VAL(rcStrict2));
2265 if ( rcStrict2 != VINF_SUCCESS
2266 && ( rcStrict == VINF_SUCCESS
2267 || rcStrict > rcStrict2))
2268 rcStrict = rcStrict2;
2269 if ( rcStrict3 != VINF_SUCCESS
2270 && ( rcStrict == VINF_SUCCESS
2271 || rcStrict > rcStrict3))
2272 rcStrict = rcStrict3;
2273 Log(("VMMR3EmtRendezvous: %#x EMT#%u returns %Rrc\n", fFlags, pVCpu->idCpu, VBOXSTRICTRC_VAL(rcStrict)));
2274 }
2275
2276 AssertLogRelMsgReturn( rcStrict <= VINF_SUCCESS
2277 || (rcStrict >= VINF_EM_FIRST && rcStrict <= VINF_EM_LAST),
2278 ("%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)),
2279 VERR_IPE_UNEXPECTED_INFO_STATUS);
2280 return VBOXSTRICTRC_VAL(rcStrict);
2281}
2282
2283
2284/**
2285 * Interface for vmR3SetHaltMethodU.
2286 *
2287 * @param pVCpu The cross context virtual CPU structure of the
2288 * calling EMT.
2289 * @param fMayHaltInRing0 The new state.
2290 * @param cNsSpinBlockThreshold The spin-vs-blocking threashold.
2291 * @thread EMT(pVCpu)
2292 *
2293 * @todo Move the EMT handling to VMM (or EM). I soooooo regret that VM
2294 * component.
2295 */
2296VMMR3_INT_DECL(void) VMMR3SetMayHaltInRing0(PVMCPU pVCpu, bool fMayHaltInRing0, uint32_t cNsSpinBlockThreshold)
2297{
2298 LogFlow(("VMMR3SetMayHaltInRing0(#%u, %d, %u)\n", pVCpu->idCpu, fMayHaltInRing0, cNsSpinBlockThreshold));
2299 pVCpu->vmm.s.fMayHaltInRing0 = fMayHaltInRing0;
2300 pVCpu->vmm.s.cNsSpinBlockThreshold = cNsSpinBlockThreshold;
2301}
2302
2303
2304/**
2305 * Read from the ring 0 jump buffer stack.
2306 *
2307 * @returns VBox status code.
2308 *
2309 * @param pVM The cross context VM structure.
2310 * @param idCpu The ID of the source CPU context (for the address).
2311 * @param R0Addr Where to start reading.
2312 * @param pvBuf Where to store the data we've read.
2313 * @param cbRead The number of bytes to read.
2314 */
2315VMMR3_INT_DECL(int) VMMR3ReadR0Stack(PVM pVM, VMCPUID idCpu, RTHCUINTPTR R0Addr, void *pvBuf, size_t cbRead)
2316{
2317 PVMCPU pVCpu = VMMGetCpuById(pVM, idCpu);
2318 AssertReturn(pVCpu, VERR_INVALID_PARAMETER);
2319 AssertReturn(cbRead < ~(size_t)0 / 2, VERR_INVALID_PARAMETER);
2320
2321 /*
2322 * Hopefully we've got all the requested bits. If not supply what we
2323 * can and zero the remaining stuff.
2324 */
2325 RTHCUINTPTR off = R0Addr - pVCpu->vmm.s.AssertJmpBuf.UnwindSp;
2326 if (off < pVCpu->vmm.s.AssertJmpBuf.cbStackValid)
2327 {
2328 size_t const cbValid = pVCpu->vmm.s.AssertJmpBuf.cbStackValid - off;
2329 if (cbRead <= cbValid)
2330 {
2331 memcpy(pvBuf, &pVCpu->vmm.s.abAssertStack[off], cbRead);
2332 return VINF_SUCCESS;
2333 }
2334
2335 memcpy(pvBuf, &pVCpu->vmm.s.abAssertStack[off], cbValid);
2336 RT_BZERO((uint8_t *)pvBuf + cbValid, cbRead - cbValid);
2337 }
2338 else
2339 RT_BZERO(pvBuf, cbRead);
2340
2341 /*
2342 * Supply the setjmp return RIP/EIP if requested.
2343 */
2344 if ( pVCpu->vmm.s.AssertJmpBuf.UnwindRetPcLocation + sizeof(RTR0UINTPTR) > R0Addr
2345 && pVCpu->vmm.s.AssertJmpBuf.UnwindRetPcLocation < R0Addr + cbRead)
2346 {
2347 uint8_t const *pbSrc = (uint8_t const *)&pVCpu->vmm.s.AssertJmpBuf.UnwindRetPcValue;
2348 size_t cbSrc = sizeof(pVCpu->vmm.s.AssertJmpBuf.UnwindRetPcValue);
2349 size_t offDst = 0;
2350 if (R0Addr < pVCpu->vmm.s.AssertJmpBuf.UnwindRetPcLocation)
2351 offDst = pVCpu->vmm.s.AssertJmpBuf.UnwindRetPcLocation - R0Addr;
2352 else if (R0Addr > pVCpu->vmm.s.AssertJmpBuf.UnwindRetPcLocation)
2353 {
2354 size_t offSrc = R0Addr - pVCpu->vmm.s.AssertJmpBuf.UnwindRetPcLocation;
2355 Assert(offSrc < cbSrc);
2356 pbSrc -= offSrc;
2357 cbSrc -= offSrc;
2358 }
2359 if (cbSrc > cbRead - offDst)
2360 cbSrc = cbRead - offDst;
2361 memcpy((uint8_t *)pvBuf + offDst, pbSrc, cbSrc);
2362
2363 //if (cbSrc == cbRead)
2364 // rc = VINF_SUCCESS;
2365 }
2366
2367 return VINF_SUCCESS;
2368}
2369
2370
2371/**
2372 * Used by the DBGF stack unwinder to initialize the register state.
2373 *
2374 * @param pUVM The user mode VM handle.
2375 * @param idCpu The ID of the CPU being unwound.
2376 * @param pState The unwind state to initialize.
2377 */
2378VMMR3_INT_DECL(void) VMMR3InitR0StackUnwindState(PUVM pUVM, VMCPUID idCpu, struct RTDBGUNWINDSTATE *pState)
2379{
2380 PVMCPU pVCpu = VMMR3GetCpuByIdU(pUVM, idCpu);
2381 AssertReturnVoid(pVCpu);
2382
2383 /*
2384 * This is all we really need here if we had proper unwind info (win64 only)...
2385 */
2386 pState->u.x86.auRegs[X86_GREG_xBP] = pVCpu->vmm.s.AssertJmpBuf.UnwindBp;
2387 pState->u.x86.auRegs[X86_GREG_xSP] = pVCpu->vmm.s.AssertJmpBuf.UnwindSp;
2388 pState->uPc = pVCpu->vmm.s.AssertJmpBuf.UnwindPc;
2389
2390 /*
2391 * Locate the resume point on the stack.
2392 */
2393#ifdef RT_ARCH_AMD64
2394 /* This code must match the vmmR0CallRing3LongJmp stack frame setup in VMMR0JmpA-amd64.asm exactly. */
2395 uintptr_t off = 0;
2396# ifdef RT_OS_WINDOWS
2397 off += 0xa0; /* XMM6 thru XMM15 */
2398# endif
2399 pState->u.x86.uRFlags = *(uint64_t const *)&pVCpu->vmm.s.abAssertStack[off];
2400 off += 8;
2401 pState->u.x86.auRegs[X86_GREG_xBX] = *(uint64_t const *)&pVCpu->vmm.s.abAssertStack[off];
2402 off += 8;
2403# ifdef RT_OS_WINDOWS
2404 pState->u.x86.auRegs[X86_GREG_xSI] = *(uint64_t const *)&pVCpu->vmm.s.abAssertStack[off];
2405 off += 8;
2406 pState->u.x86.auRegs[X86_GREG_xDI] = *(uint64_t const *)&pVCpu->vmm.s.abAssertStack[off];
2407 off += 8;
2408# endif
2409 pState->u.x86.auRegs[X86_GREG_x12] = *(uint64_t const *)&pVCpu->vmm.s.abAssertStack[off];
2410 off += 8;
2411 pState->u.x86.auRegs[X86_GREG_x13] = *(uint64_t const *)&pVCpu->vmm.s.abAssertStack[off];
2412 off += 8;
2413 pState->u.x86.auRegs[X86_GREG_x14] = *(uint64_t const *)&pVCpu->vmm.s.abAssertStack[off];
2414 off += 8;
2415 pState->u.x86.auRegs[X86_GREG_x15] = *(uint64_t const *)&pVCpu->vmm.s.abAssertStack[off];
2416 off += 8;
2417 pState->u.x86.auRegs[X86_GREG_xBP] = *(uint64_t const *)&pVCpu->vmm.s.abAssertStack[off];
2418 off += 8;
2419 pState->uPc = *(uint64_t const *)&pVCpu->vmm.s.abAssertStack[off];
2420 pState->u.x86.auRegs[X86_GREG_xSP] = pVCpu->vmm.s.AssertJmpBuf.UnwindRetSp;
2421
2422#elif defined(RT_ARCH_X86)
2423 /* This code must match the vmmR0CallRing3LongJmp stack frame setup in VMMR0JmpA-x86.asm exactly. */
2424 uintptr_t off = 0;
2425 pState->u.x86.uRFlags = *(uint32_t const *)&pVCpu->vmm.s.abAssertStack[off];
2426 off += 4;
2427 pState->u.x86.auRegs[X86_GREG_xBX] = *(uint32_t const *)&pVCpu->vmm.s.abAssertStack[off];
2428 off += 4;
2429 pState->u.x86.auRegs[X86_GREG_xSI] = *(uint32_t const *)&pVCpu->vmm.s.abAssertStack[off];
2430 off += 4;
2431 pState->u.x86.auRegs[X86_GREG_xDI] = *(uint32_t const *)&pVCpu->vmm.s.abAssertStack[off];
2432 off += 4;
2433 pState->u.x86.auRegs[X86_GREG_xBP] = *(uint32_t const *)&pVCpu->vmm.s.abAssertStack[off];
2434 off += 4;
2435 pState->uPc = *(uint32_t const *)&pVCpu->vmm.s.abAssertStack[off];
2436 pState->u.x86.auRegs[X86_GREG_xSP] = pVCpu->vmm.s.AssertJmpBuf.UnwindRetSp;
2437
2438#elif defined(RT_ARCH_ARM64)
2439 /** @todo PORTME: arm ring-0 */
2440
2441#else
2442# error "Port me"
2443#endif
2444}
2445
2446
2447/**
2448 * Wrapper for SUPR3CallVMMR0Ex which will deal with VINF_VMM_CALL_HOST returns.
2449 *
2450 * @returns VBox status code.
2451 * @param pVM The cross context VM structure.
2452 * @param uOperation Operation to execute.
2453 * @param u64Arg Constant argument.
2454 * @param pReqHdr Pointer to a request header. See SUPR3CallVMMR0Ex for
2455 * details.
2456 */
2457VMMR3DECL(int) VMMR3CallR0(PVM pVM, uint32_t uOperation, uint64_t u64Arg, PSUPVMMR0REQHDR pReqHdr)
2458{
2459 PVMCPU pVCpu = VMMGetCpu(pVM);
2460 AssertReturn(pVCpu, VERR_VM_THREAD_NOT_EMT);
2461 return VMMR3CallR0Emt(pVM, pVCpu, (VMMR0OPERATION)uOperation, u64Arg, pReqHdr);
2462}
2463
2464
2465/**
2466 * Wrapper for SUPR3CallVMMR0Ex which will deal with VINF_VMM_CALL_HOST returns.
2467 *
2468 * @returns VBox status code.
2469 * @param pVM The cross context VM structure.
2470 * @param pVCpu The cross context VM structure.
2471 * @param enmOperation Operation to execute.
2472 * @param u64Arg Constant argument.
2473 * @param pReqHdr Pointer to a request header. See SUPR3CallVMMR0Ex for
2474 * details.
2475 */
2476VMMR3_INT_DECL(int) VMMR3CallR0Emt(PVM pVM, PVMCPU pVCpu, VMMR0OPERATION enmOperation, uint64_t u64Arg, PSUPVMMR0REQHDR pReqHdr)
2477{
2478 /*
2479 * Call ring-0.
2480 */
2481#ifdef NO_SUPCALLR0VMM
2482 int rc = VERR_GENERAL_FAILURE;
2483#else
2484 int rc = SUPR3CallVMMR0Ex(VMCC_GET_VMR0_FOR_CALL(pVM), pVCpu->idCpu, enmOperation, u64Arg, pReqHdr);
2485#endif
2486
2487 /*
2488 * Flush the logs and deal with ring-0 assertions.
2489 */
2490#ifdef LOG_ENABLED
2491 VMM_FLUSH_R0_LOG(pVM, pVCpu, &pVCpu->vmm.s.u.s.Logger, NULL);
2492#endif
2493 VMM_FLUSH_R0_LOG(pVM, pVCpu, &pVCpu->vmm.s.u.s.RelLogger, RTLogRelGetDefaultInstance());
2494 if (rc != VERR_VMM_RING0_ASSERTION)
2495 {
2496 AssertLogRelMsgReturn(rc == VINF_SUCCESS || RT_FAILURE(rc),
2497 ("enmOperation=%u rc=%Rrc\n", enmOperation, rc),
2498 VERR_IPE_UNEXPECTED_INFO_STATUS);
2499 return rc;
2500 }
2501 return vmmR3HandleRing0Assert(pVM, pVCpu);
2502}
2503
2504
2505/**
2506 * Logs a ring-0 assertion ASAP after returning to ring-3.
2507 *
2508 * @returns VBox status code.
2509 * @param pVM The cross context VM structure.
2510 * @param pVCpu The cross context virtual CPU structure.
2511 */
2512static int vmmR3HandleRing0Assert(PVM pVM, PVMCPU pVCpu)
2513{
2514 RT_NOREF(pVCpu);
2515 LogRel(("%s", pVM->vmm.s.szRing0AssertMsg1));
2516 LogRel(("%s", pVM->vmm.s.szRing0AssertMsg2));
2517 return VERR_VMM_RING0_ASSERTION;
2518}
2519
2520
2521/**
2522 * Displays the Force action Flags.
2523 *
2524 * @param pVM The cross context VM structure.
2525 * @param pHlp The output helpers.
2526 * @param pszArgs The additional arguments (ignored).
2527 */
2528static DECLCALLBACK(void) vmmR3InfoFF(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
2529{
2530 int c;
2531 uint32_t f;
2532 NOREF(pszArgs);
2533
2534#define PRINT_FLAG(prf,flag) do { \
2535 if (f & (prf##flag)) \
2536 { \
2537 static const char *s_psz = #flag; \
2538 if (!(c % 6)) \
2539 pHlp->pfnPrintf(pHlp, "%s\n %s", c ? "," : "", s_psz); \
2540 else \
2541 pHlp->pfnPrintf(pHlp, ", %s", s_psz); \
2542 c++; \
2543 f &= ~(prf##flag); \
2544 } \
2545 } while (0)
2546
2547#define PRINT_GROUP(prf,grp,sfx) do { \
2548 if (f & (prf##grp##sfx)) \
2549 { \
2550 static const char *s_psz = #grp; \
2551 if (!(c % 5)) \
2552 pHlp->pfnPrintf(pHlp, "%s %s", c ? ",\n" : " Groups:\n", s_psz); \
2553 else \
2554 pHlp->pfnPrintf(pHlp, ", %s", s_psz); \
2555 c++; \
2556 } \
2557 } while (0)
2558
2559 /*
2560 * The global flags.
2561 */
2562 const uint32_t fGlobalForcedActions = pVM->fGlobalForcedActions;
2563 pHlp->pfnPrintf(pHlp, "Global FFs: %#RX32", fGlobalForcedActions);
2564
2565 /* show the flag mnemonics */
2566 c = 0;
2567 f = fGlobalForcedActions;
2568 PRINT_FLAG(VM_FF_,TM_VIRTUAL_SYNC);
2569 PRINT_FLAG(VM_FF_,PDM_QUEUES);
2570 PRINT_FLAG(VM_FF_,PDM_DMA);
2571 PRINT_FLAG(VM_FF_,DBGF);
2572 PRINT_FLAG(VM_FF_,REQUEST);
2573 PRINT_FLAG(VM_FF_,CHECK_VM_STATE);
2574 PRINT_FLAG(VM_FF_,RESET);
2575 PRINT_FLAG(VM_FF_,EMT_RENDEZVOUS);
2576 PRINT_FLAG(VM_FF_,PGM_NEED_HANDY_PAGES);
2577 PRINT_FLAG(VM_FF_,PGM_NO_MEMORY);
2578 PRINT_FLAG(VM_FF_,PGM_POOL_FLUSH_PENDING);
2579 PRINT_FLAG(VM_FF_,DEBUG_SUSPEND);
2580 if (f)
2581 pHlp->pfnPrintf(pHlp, "%s\n Unknown bits: %#RX32\n", c ? "," : "", f);
2582 else
2583 pHlp->pfnPrintf(pHlp, "\n");
2584
2585 /* the groups */
2586 c = 0;
2587 f = fGlobalForcedActions;
2588 PRINT_GROUP(VM_FF_,EXTERNAL_SUSPENDED,_MASK);
2589 PRINT_GROUP(VM_FF_,EXTERNAL_HALTED,_MASK);
2590 PRINT_GROUP(VM_FF_,HIGH_PRIORITY_PRE,_MASK);
2591 PRINT_GROUP(VM_FF_,HIGH_PRIORITY_PRE_RAW,_MASK);
2592 PRINT_GROUP(VM_FF_,HIGH_PRIORITY_POST,_MASK);
2593 PRINT_GROUP(VM_FF_,NORMAL_PRIORITY_POST,_MASK);
2594 PRINT_GROUP(VM_FF_,NORMAL_PRIORITY,_MASK);
2595 PRINT_GROUP(VM_FF_,ALL_REM,_MASK);
2596 if (c)
2597 pHlp->pfnPrintf(pHlp, "\n");
2598
2599 /*
2600 * Per CPU flags.
2601 */
2602 for (VMCPUID i = 0; i < pVM->cCpus; i++)
2603 {
2604 PVMCPU pVCpu = pVM->apCpusR3[i];
2605 const uint64_t fLocalForcedActions = pVCpu->fLocalForcedActions;
2606 pHlp->pfnPrintf(pHlp, "CPU %u FFs: %#RX64", i, fLocalForcedActions);
2607
2608 /* show the flag mnemonics */
2609 c = 0;
2610 f = fLocalForcedActions;
2611#if defined(VBOX_VMM_TARGET_ARMV8)
2612 PRINT_FLAG(VMCPU_FF_,INTERRUPT_IRQ);
2613 PRINT_FLAG(VMCPU_FF_,INTERRUPT_FIQ);
2614#else
2615 PRINT_FLAG(VMCPU_FF_,INTERRUPT_APIC);
2616 PRINT_FLAG(VMCPU_FF_,INTERRUPT_PIC);
2617#endif
2618 PRINT_FLAG(VMCPU_FF_,TIMER);
2619 PRINT_FLAG(VMCPU_FF_,INTERRUPT_NMI);
2620 PRINT_FLAG(VMCPU_FF_,INTERRUPT_SMI);
2621 PRINT_FLAG(VMCPU_FF_,PDM_CRITSECT);
2622 PRINT_FLAG(VMCPU_FF_,UNHALT);
2623 PRINT_FLAG(VMCPU_FF_,IEM);
2624 PRINT_FLAG(VMCPU_FF_,UPDATE_APIC);
2625 PRINT_FLAG(VMCPU_FF_,DBGF);
2626 PRINT_FLAG(VMCPU_FF_,REQUEST);
2627 PRINT_FLAG(VMCPU_FF_,HM_UPDATE_CR3);
2628 PRINT_FLAG(VMCPU_FF_,PGM_SYNC_CR3);
2629 PRINT_FLAG(VMCPU_FF_,PGM_SYNC_CR3_NON_GLOBAL);
2630 PRINT_FLAG(VMCPU_FF_,TLB_FLUSH);
2631 PRINT_FLAG(VMCPU_FF_,TO_R3);
2632 PRINT_FLAG(VMCPU_FF_,IOM);
2633 if (f)
2634 pHlp->pfnPrintf(pHlp, "%s\n Unknown bits: %#RX64\n", c ? "," : "", f);
2635 else
2636 pHlp->pfnPrintf(pHlp, "\n");
2637
2638 /* the groups */
2639 c = 0;
2640 f = fLocalForcedActions;
2641 PRINT_GROUP(VMCPU_FF_,EXTERNAL_SUSPENDED,_MASK);
2642 PRINT_GROUP(VMCPU_FF_,EXTERNAL_HALTED,_MASK);
2643 PRINT_GROUP(VMCPU_FF_,HIGH_PRIORITY_PRE,_MASK);
2644 PRINT_GROUP(VMCPU_FF_,HIGH_PRIORITY_PRE_RAW,_MASK);
2645 PRINT_GROUP(VMCPU_FF_,HIGH_PRIORITY_POST,_MASK);
2646 PRINT_GROUP(VMCPU_FF_,NORMAL_PRIORITY_POST,_MASK);
2647 PRINT_GROUP(VMCPU_FF_,NORMAL_PRIORITY,_MASK);
2648 PRINT_GROUP(VMCPU_FF_,RESUME_GUEST,_MASK);
2649 PRINT_GROUP(VMCPU_FF_,HM_TO_R3,_MASK);
2650 PRINT_GROUP(VMCPU_FF_,ALL_REM,_MASK);
2651 if (c)
2652 pHlp->pfnPrintf(pHlp, "\n");
2653 }
2654
2655#undef PRINT_FLAG
2656#undef PRINT_GROUP
2657}
2658
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