VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR0/VMMR0.cpp@ 92679

Last change on this file since 92679 was 92493, checked in by vboxsync, 3 years ago

VMM: Nested VMX: bugref:10092 Purge VINF_PGM_CHANGE_MODE, no longer used.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 133.6 KB
Line 
1/* $Id: VMMR0.cpp 92493 2021-11-18 14:01:56Z vboxsync $ */
2/** @file
3 * VMM - Host Context Ring 0.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_VMM
23#include <VBox/vmm/vmm.h>
24#include <VBox/sup.h>
25#include <VBox/vmm/iom.h>
26#include <VBox/vmm/trpm.h>
27#include <VBox/vmm/cpum.h>
28#include <VBox/vmm/pdmapi.h>
29#include <VBox/vmm/pgm.h>
30#ifdef VBOX_WITH_NEM_R0
31# include <VBox/vmm/nem.h>
32#endif
33#include <VBox/vmm/em.h>
34#include <VBox/vmm/stam.h>
35#include <VBox/vmm/tm.h>
36#include "VMMInternal.h"
37#include <VBox/vmm/vmcc.h>
38#include <VBox/vmm/gvm.h>
39#ifdef VBOX_WITH_PCI_PASSTHROUGH
40# include <VBox/vmm/pdmpci.h>
41#endif
42#include <VBox/vmm/apic.h>
43
44#include <VBox/vmm/gvmm.h>
45#include <VBox/vmm/gmm.h>
46#include <VBox/vmm/gim.h>
47#include <VBox/intnet.h>
48#include <VBox/vmm/hm.h>
49#include <VBox/param.h>
50#include <VBox/err.h>
51#include <VBox/version.h>
52#include <VBox/log.h>
53
54#include <iprt/asm-amd64-x86.h>
55#include <iprt/assert.h>
56#include <iprt/crc.h>
57#include <iprt/mem.h>
58#include <iprt/memobj.h>
59#include <iprt/mp.h>
60#include <iprt/once.h>
61#include <iprt/semaphore.h>
62#include <iprt/spinlock.h>
63#include <iprt/stdarg.h>
64#include <iprt/string.h>
65#include <iprt/thread.h>
66#include <iprt/timer.h>
67#include <iprt/time.h>
68
69#include "dtrace/VBoxVMM.h"
70
71
72#if defined(_MSC_VER) && defined(RT_ARCH_AMD64) /** @todo check this with with VC7! */
73# pragma intrinsic(_AddressOfReturnAddress)
74#endif
75
76#if defined(RT_OS_DARWIN) && ARCH_BITS == 32
77# error "32-bit darwin is no longer supported. Go back to 4.3 or earlier!"
78#endif
79
80
81/*********************************************************************************************************************************
82* Internal Functions *
83*********************************************************************************************************************************/
84RT_C_DECLS_BEGIN
85#if defined(RT_ARCH_X86) && (defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD))
86extern uint64_t __udivdi3(uint64_t, uint64_t);
87extern uint64_t __umoddi3(uint64_t, uint64_t);
88#endif
89RT_C_DECLS_END
90static int vmmR0UpdateLoggers(PGVM pGVM, VMCPUID idCpu, PVMMR0UPDATELOGGERSREQ pReq, size_t idxLogger);
91static int vmmR0LogFlusher(PGVM pGVM);
92static int vmmR0LogWaitFlushed(PGVM pGVM, VMCPUID idCpu, size_t idxLogger);
93static int vmmR0InitLoggers(PGVM pGVM);
94static void vmmR0CleanupLoggers(PGVM pGVM);
95
96
97/*********************************************************************************************************************************
98* Global Variables *
99*********************************************************************************************************************************/
100/** Drag in necessary library bits.
101 * The runtime lives here (in VMMR0.r0) and VBoxDD*R0.r0 links against us. */
102struct CLANG11WEIRDNOTHROW { PFNRT pfn; } g_VMMR0Deps[] =
103{
104 { (PFNRT)RTCrc32 },
105 { (PFNRT)RTOnce },
106#if defined(RT_ARCH_X86) && (defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD))
107 { (PFNRT)__udivdi3 },
108 { (PFNRT)__umoddi3 },
109#endif
110 { NULL }
111};
112
113#ifdef RT_OS_SOLARIS
114/* Dependency information for the native solaris loader. */
115extern "C" { char _depends_on[] = "vboxdrv"; }
116#endif
117
118
119/**
120 * Initialize the module.
121 * This is called when we're first loaded.
122 *
123 * @returns 0 on success.
124 * @returns VBox status on failure.
125 * @param hMod Image handle for use in APIs.
126 */
127DECLEXPORT(int) ModuleInit(void *hMod)
128{
129#ifdef VBOX_WITH_DTRACE_R0
130 /*
131 * The first thing to do is register the static tracepoints.
132 * (Deregistration is automatic.)
133 */
134 int rc2 = SUPR0TracerRegisterModule(hMod, &g_VTGObjHeader);
135 if (RT_FAILURE(rc2))
136 return rc2;
137#endif
138 LogFlow(("ModuleInit:\n"));
139
140#ifdef VBOX_WITH_64ON32_CMOS_DEBUG
141 /*
142 * Display the CMOS debug code.
143 */
144 ASMOutU8(0x72, 0x03);
145 uint8_t bDebugCode = ASMInU8(0x73);
146 LogRel(("CMOS Debug Code: %#x (%d)\n", bDebugCode, bDebugCode));
147 RTLogComPrintf("CMOS Debug Code: %#x (%d)\n", bDebugCode, bDebugCode);
148#endif
149
150 /*
151 * Initialize the VMM, GVMM, GMM, HM, PGM (Darwin) and INTNET.
152 */
153 int rc = vmmInitFormatTypes();
154 if (RT_SUCCESS(rc))
155 {
156 rc = GVMMR0Init();
157 if (RT_SUCCESS(rc))
158 {
159 rc = GMMR0Init();
160 if (RT_SUCCESS(rc))
161 {
162 rc = HMR0Init();
163 if (RT_SUCCESS(rc))
164 {
165 PDMR0Init(hMod);
166
167 rc = PGMRegisterStringFormatTypes();
168 if (RT_SUCCESS(rc))
169 {
170 rc = IntNetR0Init();
171 if (RT_SUCCESS(rc))
172 {
173#ifdef VBOX_WITH_PCI_PASSTHROUGH
174 rc = PciRawR0Init();
175#endif
176 if (RT_SUCCESS(rc))
177 {
178 rc = CPUMR0ModuleInit();
179 if (RT_SUCCESS(rc))
180 {
181#ifdef VBOX_WITH_TRIPLE_FAULT_HACK
182 rc = vmmR0TripleFaultHackInit();
183 if (RT_SUCCESS(rc))
184#endif
185 {
186#ifdef VBOX_WITH_NEM_R0
187 rc = NEMR0Init();
188 if (RT_SUCCESS(rc))
189#endif
190 {
191 LogFlow(("ModuleInit: returns success\n"));
192 return VINF_SUCCESS;
193 }
194 }
195
196 /*
197 * Bail out.
198 */
199#ifdef VBOX_WITH_TRIPLE_FAULT_HACK
200 vmmR0TripleFaultHackTerm();
201#endif
202 }
203 else
204 LogRel(("ModuleInit: CPUMR0ModuleInit -> %Rrc\n", rc));
205#ifdef VBOX_WITH_PCI_PASSTHROUGH
206 PciRawR0Term();
207#endif
208 }
209 else
210 LogRel(("ModuleInit: PciRawR0Init -> %Rrc\n", rc));
211 IntNetR0Term();
212 }
213 else
214 LogRel(("ModuleInit: IntNetR0Init -> %Rrc\n", rc));
215 PGMDeregisterStringFormatTypes();
216 }
217 else
218 LogRel(("ModuleInit: PGMRegisterStringFormatTypes -> %Rrc\n", rc));
219 HMR0Term();
220 }
221 else
222 LogRel(("ModuleInit: HMR0Init -> %Rrc\n", rc));
223 GMMR0Term();
224 }
225 else
226 LogRel(("ModuleInit: GMMR0Init -> %Rrc\n", rc));
227 GVMMR0Term();
228 }
229 else
230 LogRel(("ModuleInit: GVMMR0Init -> %Rrc\n", rc));
231 vmmTermFormatTypes();
232 }
233 else
234 LogRel(("ModuleInit: vmmInitFormatTypes -> %Rrc\n", rc));
235
236 LogFlow(("ModuleInit: failed %Rrc\n", rc));
237 return rc;
238}
239
240
241/**
242 * Terminate the module.
243 * This is called when we're finally unloaded.
244 *
245 * @param hMod Image handle for use in APIs.
246 */
247DECLEXPORT(void) ModuleTerm(void *hMod)
248{
249 NOREF(hMod);
250 LogFlow(("ModuleTerm:\n"));
251
252 /*
253 * Terminate the CPUM module (Local APIC cleanup).
254 */
255 CPUMR0ModuleTerm();
256
257 /*
258 * Terminate the internal network service.
259 */
260 IntNetR0Term();
261
262 /*
263 * PGM (Darwin), HM and PciRaw global cleanup.
264 */
265#ifdef VBOX_WITH_PCI_PASSTHROUGH
266 PciRawR0Term();
267#endif
268 PGMDeregisterStringFormatTypes();
269 HMR0Term();
270#ifdef VBOX_WITH_TRIPLE_FAULT_HACK
271 vmmR0TripleFaultHackTerm();
272#endif
273#ifdef VBOX_WITH_NEM_R0
274 NEMR0Term();
275#endif
276
277 /*
278 * Destroy the GMM and GVMM instances.
279 */
280 GMMR0Term();
281 GVMMR0Term();
282
283 vmmTermFormatTypes();
284
285 LogFlow(("ModuleTerm: returns\n"));
286}
287
288
289/**
290 * Initializes VMM specific members when the GVM structure is created,
291 * allocating loggers and stuff.
292 *
293 * The loggers are allocated here so that we can update their settings before
294 * doing VMMR0_DO_VMMR0_INIT and have correct logging at that time.
295 *
296 * @returns VBox status code.
297 * @param pGVM The global (ring-0) VM structure.
298 */
299VMMR0_INT_DECL(int) VMMR0InitPerVMData(PGVM pGVM)
300{
301 AssertCompile(sizeof(pGVM->vmmr0.s) <= sizeof(pGVM->vmmr0.padding));
302
303 /*
304 * Initialize all members first.
305 */
306 pGVM->vmmr0.s.fCalledInitVm = false;
307 pGVM->vmmr0.s.hMemObjLogger = NIL_RTR0MEMOBJ;
308 pGVM->vmmr0.s.hMapObjLogger = NIL_RTR0MEMOBJ;
309 pGVM->vmmr0.s.hMemObjReleaseLogger = NIL_RTR0MEMOBJ;
310 pGVM->vmmr0.s.hMapObjReleaseLogger = NIL_RTR0MEMOBJ;
311 pGVM->vmmr0.s.LogFlusher.hSpinlock = NIL_RTSPINLOCK;
312 pGVM->vmmr0.s.LogFlusher.hThread = NIL_RTNATIVETHREAD;
313 pGVM->vmmr0.s.LogFlusher.hEvent = NIL_RTSEMEVENT;
314 pGVM->vmmr0.s.LogFlusher.idxRingHead = 0;
315 pGVM->vmmr0.s.LogFlusher.idxRingTail = 0;
316 pGVM->vmmr0.s.LogFlusher.fThreadWaiting = false;
317
318 for (VMCPUID idCpu = 0; idCpu < pGVM->cCpus; idCpu++)
319 {
320 PGVMCPU pGVCpu = &pGVM->aCpus[idCpu];
321 Assert(pGVCpu->idHostCpu == NIL_RTCPUID);
322 Assert(pGVCpu->iHostCpuSet == UINT32_MAX);
323 pGVCpu->vmmr0.s.pPreemptState = NULL;
324 pGVCpu->vmmr0.s.hCtxHook = NIL_RTTHREADCTXHOOK;
325 pGVCpu->vmmr0.s.AssertJmpBuf.pMirrorBuf = &pGVCpu->vmm.s.AssertJmpBuf;
326 pGVCpu->vmmr0.s.AssertJmpBuf.pvStackBuf = &pGVCpu->vmm.s.abAssertStack[0];
327 pGVCpu->vmmr0.s.AssertJmpBuf.cbStackBuf = sizeof(pGVCpu->vmm.s.abAssertStack);
328
329 for (size_t iLogger = 0; iLogger < RT_ELEMENTS(pGVCpu->vmmr0.s.u.aLoggers); iLogger++)
330 pGVCpu->vmmr0.s.u.aLoggers[iLogger].hEventFlushWait = NIL_RTSEMEVENT;
331 }
332
333 /*
334 * Create the loggers.
335 */
336 return vmmR0InitLoggers(pGVM);
337}
338
339
340/**
341 * Initiates the R0 driver for a particular VM instance.
342 *
343 * @returns VBox status code.
344 *
345 * @param pGVM The global (ring-0) VM structure.
346 * @param uSvnRev The SVN revision of the ring-3 part.
347 * @param uBuildType Build type indicator.
348 * @thread EMT(0)
349 */
350static int vmmR0InitVM(PGVM pGVM, uint32_t uSvnRev, uint32_t uBuildType)
351{
352 /*
353 * Match the SVN revisions and build type.
354 */
355 if (uSvnRev != VMMGetSvnRev())
356 {
357 LogRel(("VMMR0InitVM: Revision mismatch, r3=%d r0=%d\n", uSvnRev, VMMGetSvnRev()));
358 SUPR0Printf("VMMR0InitVM: Revision mismatch, r3=%d r0=%d\n", uSvnRev, VMMGetSvnRev());
359 return VERR_VMM_R0_VERSION_MISMATCH;
360 }
361 if (uBuildType != vmmGetBuildType())
362 {
363 LogRel(("VMMR0InitVM: Build type mismatch, r3=%#x r0=%#x\n", uBuildType, vmmGetBuildType()));
364 SUPR0Printf("VMMR0InitVM: Build type mismatch, r3=%#x r0=%#x\n", uBuildType, vmmGetBuildType());
365 return VERR_VMM_R0_VERSION_MISMATCH;
366 }
367
368 int rc = GVMMR0ValidateGVMandEMT(pGVM, 0 /*idCpu*/);
369 if (RT_FAILURE(rc))
370 return rc;
371
372 /* Don't allow this to be called more than once. */
373 if (!pGVM->vmmr0.s.fCalledInitVm)
374 pGVM->vmmr0.s.fCalledInitVm = true;
375 else
376 return VERR_ALREADY_INITIALIZED;
377
378#ifdef LOG_ENABLED
379
380 /*
381 * Register the EMT R0 logger instance for VCPU 0.
382 */
383 PVMCPUCC pVCpu = VMCC_GET_CPU_0(pGVM);
384 if (pVCpu->vmmr0.s.u.s.Logger.pLogger)
385 {
386# if 0 /* testing of the logger. */
387 LogCom(("vmmR0InitVM: before %p\n", RTLogDefaultInstance()));
388 LogCom(("vmmR0InitVM: pfnFlush=%p actual=%p\n", pR0Logger->Logger.pfnFlush, vmmR0LoggerFlush));
389 LogCom(("vmmR0InitVM: pfnLogger=%p actual=%p\n", pR0Logger->Logger.pfnLogger, vmmR0LoggerWrapper));
390 LogCom(("vmmR0InitVM: offScratch=%d fFlags=%#x fDestFlags=%#x\n", pR0Logger->Logger.offScratch, pR0Logger->Logger.fFlags, pR0Logger->Logger.fDestFlags));
391
392 RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pGVM->pSession);
393 LogCom(("vmmR0InitVM: after %p reg\n", RTLogDefaultInstance()));
394 RTLogSetDefaultInstanceThread(NULL, pGVM->pSession);
395 LogCom(("vmmR0InitVM: after %p dereg\n", RTLogDefaultInstance()));
396
397 pR0Logger->Logger.pfnLogger("hello ring-0 logger\n");
398 LogCom(("vmmR0InitVM: returned successfully from direct logger call.\n"));
399 pR0Logger->Logger.pfnFlush(&pR0Logger->Logger);
400 LogCom(("vmmR0InitVM: returned successfully from direct flush call.\n"));
401
402 RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pGVM->pSession);
403 LogCom(("vmmR0InitVM: after %p reg2\n", RTLogDefaultInstance()));
404 pR0Logger->Logger.pfnLogger("hello ring-0 logger\n");
405 LogCom(("vmmR0InitVM: returned successfully from direct logger call (2). offScratch=%d\n", pR0Logger->Logger.offScratch));
406 RTLogSetDefaultInstanceThread(NULL, pGVM->pSession);
407 LogCom(("vmmR0InitVM: after %p dereg2\n", RTLogDefaultInstance()));
408
409 RTLogLoggerEx(&pR0Logger->Logger, 0, ~0U, "hello ring-0 logger (RTLogLoggerEx)\n");
410 LogCom(("vmmR0InitVM: RTLogLoggerEx returned fine offScratch=%d\n", pR0Logger->Logger.offScratch));
411
412 RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pGVM->pSession);
413 RTLogPrintf("hello ring-0 logger (RTLogPrintf)\n");
414 LogCom(("vmmR0InitVM: RTLogPrintf returned fine offScratch=%d\n", pR0Logger->Logger.offScratch));
415# endif
416# ifdef VBOX_WITH_R0_LOGGING
417 Log(("Switching to per-thread logging instance %p (key=%p)\n", pVCpu->vmmr0.s.u.s.Logger.pLogger, pGVM->pSession));
418 RTLogSetDefaultInstanceThread(pVCpu->vmmr0.s.u.s.Logger.pLogger, (uintptr_t)pGVM->pSession);
419 pVCpu->vmmr0.s.u.s.Logger.fRegistered = true;
420# endif
421 }
422#endif /* LOG_ENABLED */
423
424 /*
425 * Check if the host supports high resolution timers or not.
426 */
427 if ( pGVM->vmm.s.fUsePeriodicPreemptionTimers
428 && !RTTimerCanDoHighResolution())
429 pGVM->vmm.s.fUsePeriodicPreemptionTimers = false;
430
431 /*
432 * Initialize the per VM data for GVMM and GMM.
433 */
434 rc = GVMMR0InitVM(pGVM);
435 if (RT_SUCCESS(rc))
436 {
437 /*
438 * Init HM, CPUM and PGM (Darwin only).
439 */
440 rc = HMR0InitVM(pGVM);
441 if (RT_SUCCESS(rc))
442 {
443 rc = CPUMR0InitVM(pGVM);
444 if (RT_SUCCESS(rc))
445 {
446 rc = PGMR0InitVM(pGVM);
447 if (RT_SUCCESS(rc))
448 {
449 rc = EMR0InitVM(pGVM);
450 if (RT_SUCCESS(rc))
451 {
452#ifdef VBOX_WITH_PCI_PASSTHROUGH
453 rc = PciRawR0InitVM(pGVM);
454#endif
455 if (RT_SUCCESS(rc))
456 {
457 rc = GIMR0InitVM(pGVM);
458 if (RT_SUCCESS(rc))
459 {
460 GVMMR0DoneInitVM(pGVM);
461
462 /*
463 * Collect a bit of info for the VM release log.
464 */
465 pGVM->vmm.s.fIsPreemptPendingApiTrusty = RTThreadPreemptIsPendingTrusty();
466 pGVM->vmm.s.fIsPreemptPossible = RTThreadPreemptIsPossible();;
467 return rc;
468
469 /* bail out*/
470 //GIMR0TermVM(pGVM);
471 }
472#ifdef VBOX_WITH_PCI_PASSTHROUGH
473 PciRawR0TermVM(pGVM);
474#endif
475 }
476 }
477 }
478 }
479 HMR0TermVM(pGVM);
480 }
481 }
482
483 RTLogSetDefaultInstanceThread(NULL, (uintptr_t)pGVM->pSession);
484 return rc;
485}
486
487
488/**
489 * Does EMT specific VM initialization.
490 *
491 * @returns VBox status code.
492 * @param pGVM The ring-0 VM structure.
493 * @param idCpu The EMT that's calling.
494 */
495static int vmmR0InitVMEmt(PGVM pGVM, VMCPUID idCpu)
496{
497 /* Paranoia (caller checked these already). */
498 AssertReturn(idCpu < pGVM->cCpus, VERR_INVALID_CPU_ID);
499 AssertReturn(pGVM->aCpus[idCpu].hEMT == RTThreadNativeSelf(), VERR_INVALID_CPU_ID);
500
501#if defined(LOG_ENABLED) && defined(VBOX_WITH_R0_LOGGING)
502 /*
503 * Registration of ring 0 loggers.
504 */
505 PVMCPUCC pVCpu = &pGVM->aCpus[idCpu];
506 if ( pVCpu->vmmr0.s.u.s.Logger.pLogger
507 && !pVCpu->vmmr0.s.u.s.Logger.fRegistered)
508 {
509 RTLogSetDefaultInstanceThread(pVCpu->vmmr0.s.u.s.Logger.pLogger, (uintptr_t)pGVM->pSession);
510 pVCpu->vmmr0.s.u.s.Logger.fRegistered = true;
511 }
512#endif
513
514 return VINF_SUCCESS;
515}
516
517
518
519/**
520 * Terminates the R0 bits for a particular VM instance.
521 *
522 * This is normally called by ring-3 as part of the VM termination process, but
523 * may alternatively be called during the support driver session cleanup when
524 * the VM object is destroyed (see GVMM).
525 *
526 * @returns VBox status code.
527 *
528 * @param pGVM The global (ring-0) VM structure.
529 * @param idCpu Set to 0 if EMT(0) or NIL_VMCPUID if session cleanup
530 * thread.
531 * @thread EMT(0) or session clean up thread.
532 */
533VMMR0_INT_DECL(int) VMMR0TermVM(PGVM pGVM, VMCPUID idCpu)
534{
535 /*
536 * Check EMT(0) claim if we're called from userland.
537 */
538 if (idCpu != NIL_VMCPUID)
539 {
540 AssertReturn(idCpu == 0, VERR_INVALID_CPU_ID);
541 int rc = GVMMR0ValidateGVMandEMT(pGVM, idCpu);
542 if (RT_FAILURE(rc))
543 return rc;
544 }
545
546#ifdef VBOX_WITH_PCI_PASSTHROUGH
547 PciRawR0TermVM(pGVM);
548#endif
549
550 /*
551 * Tell GVMM what we're up to and check that we only do this once.
552 */
553 if (GVMMR0DoingTermVM(pGVM))
554 {
555 GIMR0TermVM(pGVM);
556
557 /** @todo I wish to call PGMR0PhysFlushHandyPages(pGVM, &pGVM->aCpus[idCpu])
558 * here to make sure we don't leak any shared pages if we crash... */
559 HMR0TermVM(pGVM);
560 }
561
562 /*
563 * Deregister the logger for this EMT.
564 */
565 RTLogSetDefaultInstanceThread(NULL, (uintptr_t)pGVM->pSession);
566
567 /*
568 * Start log flusher thread termination.
569 */
570 ASMAtomicWriteBool(&pGVM->vmmr0.s.LogFlusher.fThreadShutdown, true);
571 if (pGVM->vmmr0.s.LogFlusher.hEvent != NIL_RTSEMEVENT)
572 RTSemEventSignal(pGVM->vmmr0.s.LogFlusher.hEvent);
573
574 return VINF_SUCCESS;
575}
576
577
578/**
579 * This is called at the end of gvmmR0CleanupVM().
580 *
581 * @param pGVM The global (ring-0) VM structure.
582 */
583VMMR0_INT_DECL(void) VMMR0CleanupVM(PGVM pGVM)
584{
585 AssertCompile(NIL_RTTHREADCTXHOOK == (RTTHREADCTXHOOK)0); /* Depends on zero initialized memory working for NIL at the moment. */
586 for (VMCPUID idCpu = 0; idCpu < pGVM->cCpus; idCpu++)
587 {
588 PGVMCPU pGVCpu = &pGVM->aCpus[idCpu];
589
590 /** @todo Can we busy wait here for all thread-context hooks to be
591 * deregistered before releasing (destroying) it? Only until we find a
592 * solution for not deregistering hooks everytime we're leaving HMR0
593 * context. */
594 VMMR0ThreadCtxHookDestroyForEmt(pGVCpu);
595 }
596
597 vmmR0CleanupLoggers(pGVM);
598}
599
600
601/**
602 * An interrupt or unhalt force flag is set, deal with it.
603 *
604 * @returns VINF_SUCCESS (or VINF_EM_HALT).
605 * @param pVCpu The cross context virtual CPU structure.
606 * @param uMWait Result from EMMonitorWaitIsActive().
607 * @param enmInterruptibility Guest CPU interruptbility level.
608 */
609static int vmmR0DoHaltInterrupt(PVMCPUCC pVCpu, unsigned uMWait, CPUMINTERRUPTIBILITY enmInterruptibility)
610{
611 Assert(!TRPMHasTrap(pVCpu));
612 Assert( enmInterruptibility > CPUMINTERRUPTIBILITY_INVALID
613 && enmInterruptibility < CPUMINTERRUPTIBILITY_END);
614
615 /*
616 * Pending interrupts w/o any SMIs or NMIs? That the usual case.
617 */
618 if ( VMCPU_FF_IS_ANY_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC)
619 && !VMCPU_FF_IS_ANY_SET(pVCpu, VMCPU_FF_INTERRUPT_SMI | VMCPU_FF_INTERRUPT_NMI))
620 {
621 if (enmInterruptibility <= CPUMINTERRUPTIBILITY_UNRESTRAINED)
622 {
623 uint8_t u8Interrupt = 0;
624 int rc = PDMGetInterrupt(pVCpu, &u8Interrupt);
625 Log(("vmmR0DoHaltInterrupt: CPU%d u8Interrupt=%d (%#x) rc=%Rrc\n", pVCpu->idCpu, u8Interrupt, u8Interrupt, rc));
626 if (RT_SUCCESS(rc))
627 {
628 VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_UNHALT);
629
630 rc = TRPMAssertTrap(pVCpu, u8Interrupt, TRPM_HARDWARE_INT);
631 AssertRCSuccess(rc);
632 STAM_REL_COUNTER_INC(&pVCpu->vmm.s.StatR0HaltExec);
633 return rc;
634 }
635 }
636 }
637 /*
638 * SMI is not implemented yet, at least not here.
639 */
640 else if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_SMI))
641 {
642 Log12(("vmmR0DoHaltInterrupt: CPU%d failed #3\n", pVCpu->idCpu));
643 STAM_REL_COUNTER_INC(&pVCpu->vmm.s.StatR0HaltToR3);
644 return VINF_EM_HALT;
645 }
646 /*
647 * NMI.
648 */
649 else if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_NMI))
650 {
651 if (enmInterruptibility < CPUMINTERRUPTIBILITY_NMI_INHIBIT)
652 {
653 /** @todo later. */
654 Log12(("vmmR0DoHaltInterrupt: CPU%d failed #2 (uMWait=%u enmInt=%d)\n", pVCpu->idCpu, uMWait, enmInterruptibility));
655 STAM_REL_COUNTER_INC(&pVCpu->vmm.s.StatR0HaltToR3);
656 return VINF_EM_HALT;
657 }
658 }
659 /*
660 * Nested-guest virtual interrupt.
661 */
662 else if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_NESTED_GUEST))
663 {
664 if (enmInterruptibility < CPUMINTERRUPTIBILITY_VIRT_INT_DISABLED)
665 {
666 /** @todo NSTVMX: NSTSVM: Remember, we might have to check and perform VM-exits
667 * here before injecting the virtual interrupt. See emR3ForcedActions
668 * for details. */
669 Log12(("vmmR0DoHaltInterrupt: CPU%d failed #1 (uMWait=%u enmInt=%d)\n", pVCpu->idCpu, uMWait, enmInterruptibility));
670 STAM_REL_COUNTER_INC(&pVCpu->vmm.s.StatR0HaltToR3);
671 return VINF_EM_HALT;
672 }
673 }
674
675 if (VMCPU_FF_TEST_AND_CLEAR(pVCpu, VMCPU_FF_UNHALT))
676 {
677 STAM_REL_COUNTER_INC(&pVCpu->vmm.s.StatR0HaltExec);
678 Log11(("vmmR0DoHaltInterrupt: CPU%d success VINF_SUCCESS (UNHALT)\n", pVCpu->idCpu));
679 return VINF_SUCCESS;
680 }
681 if (uMWait > 1)
682 {
683 STAM_REL_COUNTER_INC(&pVCpu->vmm.s.StatR0HaltExec);
684 Log11(("vmmR0DoHaltInterrupt: CPU%d success VINF_SUCCESS (uMWait=%u > 1)\n", pVCpu->idCpu, uMWait));
685 return VINF_SUCCESS;
686 }
687
688 Log12(("vmmR0DoHaltInterrupt: CPU%d failed #0 (uMWait=%u enmInt=%d)\n", pVCpu->idCpu, uMWait, enmInterruptibility));
689 STAM_REL_COUNTER_INC(&pVCpu->vmm.s.StatR0HaltToR3);
690 return VINF_EM_HALT;
691}
692
693
694/**
695 * This does one round of vmR3HaltGlobal1Halt().
696 *
697 * The rational here is that we'll reduce latency in interrupt situations if we
698 * don't go to ring-3 immediately on a VINF_EM_HALT (guest executed HLT or
699 * MWAIT), but do one round of blocking here instead and hope the interrupt is
700 * raised in the meanwhile.
701 *
702 * If we go to ring-3 we'll quit the inner HM/NEM loop in EM and end up in the
703 * outer loop, which will then call VMR3WaitHalted() and that in turn will do a
704 * ring-0 call (unless we're too close to a timer event). When the interrupt
705 * wakes us up, we'll return from ring-0 and EM will by instinct do a
706 * rescheduling (because of raw-mode) before it resumes the HM/NEM loop and gets
707 * back to VMMR0EntryFast().
708 *
709 * @returns VINF_SUCCESS or VINF_EM_HALT.
710 * @param pGVM The ring-0 VM structure.
711 * @param pGVCpu The ring-0 virtual CPU structure.
712 *
713 * @todo r=bird: All the blocking/waiting and EMT managment should move out of
714 * the VM module, probably to VMM. Then this would be more weird wrt
715 * parameters and statistics.
716 */
717static int vmmR0DoHalt(PGVM pGVM, PGVMCPU pGVCpu)
718{
719 /*
720 * Do spin stat historization.
721 */
722 if (++pGVCpu->vmm.s.cR0Halts & 0xff)
723 { /* likely */ }
724 else if (pGVCpu->vmm.s.cR0HaltsSucceeded > pGVCpu->vmm.s.cR0HaltsToRing3)
725 {
726 pGVCpu->vmm.s.cR0HaltsSucceeded = 2;
727 pGVCpu->vmm.s.cR0HaltsToRing3 = 0;
728 }
729 else
730 {
731 pGVCpu->vmm.s.cR0HaltsSucceeded = 0;
732 pGVCpu->vmm.s.cR0HaltsToRing3 = 2;
733 }
734
735 /*
736 * Flags that makes us go to ring-3.
737 */
738 uint32_t const fVmFFs = VM_FF_TM_VIRTUAL_SYNC | VM_FF_PDM_QUEUES | VM_FF_PDM_DMA
739 | VM_FF_DBGF | VM_FF_REQUEST | VM_FF_CHECK_VM_STATE
740 | VM_FF_RESET | VM_FF_EMT_RENDEZVOUS | VM_FF_PGM_NEED_HANDY_PAGES
741 | VM_FF_PGM_NO_MEMORY | VM_FF_DEBUG_SUSPEND;
742 uint64_t const fCpuFFs = VMCPU_FF_TIMER | VMCPU_FF_PDM_CRITSECT | VMCPU_FF_IEM
743 | VMCPU_FF_REQUEST | VMCPU_FF_DBGF | VMCPU_FF_HM_UPDATE_CR3
744 | VMCPU_FF_PGM_SYNC_CR3 | VMCPU_FF_PGM_SYNC_CR3_NON_GLOBAL
745 | VMCPU_FF_TO_R3 | VMCPU_FF_IOM;
746
747 /*
748 * Check preconditions.
749 */
750 unsigned const uMWait = EMMonitorWaitIsActive(pGVCpu);
751 CPUMINTERRUPTIBILITY const enmInterruptibility = CPUMGetGuestInterruptibility(pGVCpu);
752 if ( pGVCpu->vmm.s.fMayHaltInRing0
753 && !TRPMHasTrap(pGVCpu)
754 && ( enmInterruptibility == CPUMINTERRUPTIBILITY_UNRESTRAINED
755 || uMWait > 1))
756 {
757 if ( !VM_FF_IS_ANY_SET(pGVM, fVmFFs)
758 && !VMCPU_FF_IS_ANY_SET(pGVCpu, fCpuFFs))
759 {
760 /*
761 * Interrupts pending already?
762 */
763 if (VMCPU_FF_TEST_AND_CLEAR(pGVCpu, VMCPU_FF_UPDATE_APIC))
764 APICUpdatePendingInterrupts(pGVCpu);
765
766 /*
767 * Flags that wake up from the halted state.
768 */
769 uint64_t const fIntMask = VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC | VMCPU_FF_INTERRUPT_NESTED_GUEST
770 | VMCPU_FF_INTERRUPT_NMI | VMCPU_FF_INTERRUPT_SMI | VMCPU_FF_UNHALT;
771
772 if (VMCPU_FF_IS_ANY_SET(pGVCpu, fIntMask))
773 return vmmR0DoHaltInterrupt(pGVCpu, uMWait, enmInterruptibility);
774 ASMNopPause();
775
776 /*
777 * Check out how long till the next timer event.
778 */
779 uint64_t u64Delta;
780 uint64_t u64GipTime = TMTimerPollGIP(pGVM, pGVCpu, &u64Delta);
781
782 if ( !VM_FF_IS_ANY_SET(pGVM, fVmFFs)
783 && !VMCPU_FF_IS_ANY_SET(pGVCpu, fCpuFFs))
784 {
785 if (VMCPU_FF_TEST_AND_CLEAR(pGVCpu, VMCPU_FF_UPDATE_APIC))
786 APICUpdatePendingInterrupts(pGVCpu);
787
788 if (VMCPU_FF_IS_ANY_SET(pGVCpu, fIntMask))
789 return vmmR0DoHaltInterrupt(pGVCpu, uMWait, enmInterruptibility);
790
791 /*
792 * Wait if there is enough time to the next timer event.
793 */
794 if (u64Delta >= pGVCpu->vmm.s.cNsSpinBlockThreshold)
795 {
796 /* If there are few other CPU cores around, we will procrastinate a
797 little before going to sleep, hoping for some device raising an
798 interrupt or similar. Though, the best thing here would be to
799 dynamically adjust the spin count according to its usfulness or
800 something... */
801 if ( pGVCpu->vmm.s.cR0HaltsSucceeded > pGVCpu->vmm.s.cR0HaltsToRing3
802 && RTMpGetOnlineCount() >= 4)
803 {
804 /** @todo Figure out how we can skip this if it hasn't help recently...
805 * @bugref{9172#c12} */
806 uint32_t cSpinLoops = 42;
807 while (cSpinLoops-- > 0)
808 {
809 ASMNopPause();
810 if (VMCPU_FF_TEST_AND_CLEAR(pGVCpu, VMCPU_FF_UPDATE_APIC))
811 APICUpdatePendingInterrupts(pGVCpu);
812 ASMNopPause();
813 if (VM_FF_IS_ANY_SET(pGVM, fVmFFs))
814 {
815 STAM_REL_COUNTER_INC(&pGVCpu->vmm.s.StatR0HaltToR3FromSpin);
816 STAM_REL_COUNTER_INC(&pGVCpu->vmm.s.StatR0HaltToR3);
817 return VINF_EM_HALT;
818 }
819 ASMNopPause();
820 if (VMCPU_FF_IS_ANY_SET(pGVCpu, fCpuFFs))
821 {
822 STAM_REL_COUNTER_INC(&pGVCpu->vmm.s.StatR0HaltToR3FromSpin);
823 STAM_REL_COUNTER_INC(&pGVCpu->vmm.s.StatR0HaltToR3);
824 return VINF_EM_HALT;
825 }
826 ASMNopPause();
827 if (VMCPU_FF_IS_ANY_SET(pGVCpu, fIntMask))
828 {
829 STAM_REL_COUNTER_INC(&pGVCpu->vmm.s.StatR0HaltExecFromSpin);
830 return vmmR0DoHaltInterrupt(pGVCpu, uMWait, enmInterruptibility);
831 }
832 ASMNopPause();
833 }
834 }
835
836 /*
837 * We have to set the state to VMCPUSTATE_STARTED_HALTED here so ring-3
838 * knows when to notify us (cannot access VMINTUSERPERVMCPU::fWait from here).
839 * After changing the state we must recheck the force flags of course.
840 */
841 if (VMCPU_CMPXCHG_STATE(pGVCpu, VMCPUSTATE_STARTED_HALTED, VMCPUSTATE_STARTED))
842 {
843 if ( !VM_FF_IS_ANY_SET(pGVM, fVmFFs)
844 && !VMCPU_FF_IS_ANY_SET(pGVCpu, fCpuFFs))
845 {
846 if (VMCPU_FF_TEST_AND_CLEAR(pGVCpu, VMCPU_FF_UPDATE_APIC))
847 APICUpdatePendingInterrupts(pGVCpu);
848
849 if (VMCPU_FF_IS_ANY_SET(pGVCpu, fIntMask))
850 {
851 VMCPU_CMPXCHG_STATE(pGVCpu, VMCPUSTATE_STARTED, VMCPUSTATE_STARTED_HALTED);
852 return vmmR0DoHaltInterrupt(pGVCpu, uMWait, enmInterruptibility);
853 }
854
855 /* Okay, block! */
856 uint64_t const u64StartSchedHalt = RTTimeNanoTS();
857 int rc = GVMMR0SchedHalt(pGVM, pGVCpu, u64GipTime);
858 uint64_t const u64EndSchedHalt = RTTimeNanoTS();
859 uint64_t const cNsElapsedSchedHalt = u64EndSchedHalt - u64StartSchedHalt;
860 Log10(("vmmR0DoHalt: CPU%d: halted %llu ns\n", pGVCpu->idCpu, cNsElapsedSchedHalt));
861
862 VMCPU_CMPXCHG_STATE(pGVCpu, VMCPUSTATE_STARTED, VMCPUSTATE_STARTED_HALTED);
863 STAM_REL_PROFILE_ADD_PERIOD(&pGVCpu->vmm.s.StatR0HaltBlock, cNsElapsedSchedHalt);
864 if ( rc == VINF_SUCCESS
865 || rc == VERR_INTERRUPTED)
866 {
867 /* Keep some stats like ring-3 does. */
868 int64_t const cNsOverslept = u64EndSchedHalt - u64GipTime;
869 if (cNsOverslept > 50000)
870 STAM_REL_PROFILE_ADD_PERIOD(&pGVCpu->vmm.s.StatR0HaltBlockOverslept, cNsOverslept);
871 else if (cNsOverslept < -50000)
872 STAM_REL_PROFILE_ADD_PERIOD(&pGVCpu->vmm.s.StatR0HaltBlockInsomnia, cNsElapsedSchedHalt);
873 else
874 STAM_REL_PROFILE_ADD_PERIOD(&pGVCpu->vmm.s.StatR0HaltBlockOnTime, cNsElapsedSchedHalt);
875
876 /*
877 * Recheck whether we can resume execution or have to go to ring-3.
878 */
879 if ( !VM_FF_IS_ANY_SET(pGVM, fVmFFs)
880 && !VMCPU_FF_IS_ANY_SET(pGVCpu, fCpuFFs))
881 {
882 if (VMCPU_FF_TEST_AND_CLEAR(pGVCpu, VMCPU_FF_UPDATE_APIC))
883 APICUpdatePendingInterrupts(pGVCpu);
884 if (VMCPU_FF_IS_ANY_SET(pGVCpu, fIntMask))
885 {
886 STAM_REL_COUNTER_INC(&pGVCpu->vmm.s.StatR0HaltExecFromBlock);
887 return vmmR0DoHaltInterrupt(pGVCpu, uMWait, enmInterruptibility);
888 }
889 STAM_REL_COUNTER_INC(&pGVCpu->vmm.s.StatR0HaltToR3PostNoInt);
890 Log12(("vmmR0DoHalt: CPU%d post #2 - No pending interrupt\n", pGVCpu->idCpu));
891 }
892 else
893 {
894 STAM_REL_COUNTER_INC(&pGVCpu->vmm.s.StatR0HaltToR3PostPendingFF);
895 Log12(("vmmR0DoHalt: CPU%d post #1 - Pending FF\n", pGVCpu->idCpu));
896 }
897 }
898 else
899 {
900 STAM_REL_COUNTER_INC(&pGVCpu->vmm.s.StatR0HaltToR3Other);
901 Log12(("vmmR0DoHalt: CPU%d GVMMR0SchedHalt failed: %Rrc\n", pGVCpu->idCpu, rc));
902 }
903 }
904 else
905 {
906 VMCPU_CMPXCHG_STATE(pGVCpu, VMCPUSTATE_STARTED, VMCPUSTATE_STARTED_HALTED);
907 STAM_REL_COUNTER_INC(&pGVCpu->vmm.s.StatR0HaltToR3PendingFF);
908 Log12(("vmmR0DoHalt: CPU%d failed #5 - Pending FF\n", pGVCpu->idCpu));
909 }
910 }
911 else
912 {
913 STAM_REL_COUNTER_INC(&pGVCpu->vmm.s.StatR0HaltToR3Other);
914 Log12(("vmmR0DoHalt: CPU%d failed #4 - enmState=%d\n", pGVCpu->idCpu, VMCPU_GET_STATE(pGVCpu)));
915 }
916 }
917 else
918 {
919 STAM_REL_COUNTER_INC(&pGVCpu->vmm.s.StatR0HaltToR3SmallDelta);
920 Log12(("vmmR0DoHalt: CPU%d failed #3 - delta too small: %RU64\n", pGVCpu->idCpu, u64Delta));
921 }
922 }
923 else
924 {
925 STAM_REL_COUNTER_INC(&pGVCpu->vmm.s.StatR0HaltToR3PendingFF);
926 Log12(("vmmR0DoHalt: CPU%d failed #2 - Pending FF\n", pGVCpu->idCpu));
927 }
928 }
929 else
930 {
931 STAM_REL_COUNTER_INC(&pGVCpu->vmm.s.StatR0HaltToR3PendingFF);
932 Log12(("vmmR0DoHalt: CPU%d failed #1 - Pending FF\n", pGVCpu->idCpu));
933 }
934 }
935 else
936 {
937 STAM_REL_COUNTER_INC(&pGVCpu->vmm.s.StatR0HaltToR3Other);
938 Log12(("vmmR0DoHalt: CPU%d failed #0 - fMayHaltInRing0=%d TRPMHasTrap=%d enmInt=%d uMWait=%u\n",
939 pGVCpu->idCpu, pGVCpu->vmm.s.fMayHaltInRing0, TRPMHasTrap(pGVCpu), enmInterruptibility, uMWait));
940 }
941
942 STAM_REL_COUNTER_INC(&pGVCpu->vmm.s.StatR0HaltToR3);
943 return VINF_EM_HALT;
944}
945
946
947/**
948 * VMM ring-0 thread-context callback.
949 *
950 * This does common HM state updating and calls the HM-specific thread-context
951 * callback.
952 *
953 * This is used together with RTThreadCtxHookCreate() on platforms which
954 * supports it, and directly from VMMR0EmtPrepareForBlocking() and
955 * VMMR0EmtResumeAfterBlocking() on platforms which don't.
956 *
957 * @param enmEvent The thread-context event.
958 * @param pvUser Opaque pointer to the VMCPU.
959 *
960 * @thread EMT(pvUser)
961 */
962static DECLCALLBACK(void) vmmR0ThreadCtxCallback(RTTHREADCTXEVENT enmEvent, void *pvUser)
963{
964 PVMCPUCC pVCpu = (PVMCPUCC)pvUser;
965
966 switch (enmEvent)
967 {
968 case RTTHREADCTXEVENT_IN:
969 {
970 /*
971 * Linux may call us with preemption enabled (really!) but technically we
972 * cannot get preempted here, otherwise we end up in an infinite recursion
973 * scenario (i.e. preempted in resume hook -> preempt hook -> resume hook...
974 * ad infinitum). Let's just disable preemption for now...
975 */
976 /** @todo r=bird: I don't believe the above. The linux code is clearly enabling
977 * preemption after doing the callout (one or two functions up the
978 * call chain). */
979 /** @todo r=ramshankar: See @bugref{5313#c30}. */
980 RTTHREADPREEMPTSTATE ParanoidPreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
981 RTThreadPreemptDisable(&ParanoidPreemptState);
982
983 /* We need to update the VCPU <-> host CPU mapping. */
984 RTCPUID idHostCpu;
985 uint32_t iHostCpuSet = RTMpCurSetIndexAndId(&idHostCpu);
986 pVCpu->iHostCpuSet = iHostCpuSet;
987 ASMAtomicWriteU32(&pVCpu->idHostCpu, idHostCpu);
988
989 /* In the very unlikely event that the GIP delta for the CPU we're
990 rescheduled needs calculating, try force a return to ring-3.
991 We unfortunately cannot do the measurements right here. */
992 if (RT_LIKELY(!SUPIsTscDeltaAvailableForCpuSetIndex(iHostCpuSet)))
993 { /* likely */ }
994 else
995 VMCPU_FF_SET(pVCpu, VMCPU_FF_TO_R3);
996
997 /* Invoke the HM-specific thread-context callback. */
998 HMR0ThreadCtxCallback(enmEvent, pvUser);
999
1000 /* Restore preemption. */
1001 RTThreadPreemptRestore(&ParanoidPreemptState);
1002 break;
1003 }
1004
1005 case RTTHREADCTXEVENT_OUT:
1006 {
1007 /* Invoke the HM-specific thread-context callback. */
1008 HMR0ThreadCtxCallback(enmEvent, pvUser);
1009
1010 /*
1011 * Sigh. See VMMGetCpu() used by VMCPU_ASSERT_EMT(). We cannot let several VCPUs
1012 * have the same host CPU associated with it.
1013 */
1014 pVCpu->iHostCpuSet = UINT32_MAX;
1015 ASMAtomicWriteU32(&pVCpu->idHostCpu, NIL_RTCPUID);
1016 break;
1017 }
1018
1019 default:
1020 /* Invoke the HM-specific thread-context callback. */
1021 HMR0ThreadCtxCallback(enmEvent, pvUser);
1022 break;
1023 }
1024}
1025
1026
1027/**
1028 * Creates thread switching hook for the current EMT thread.
1029 *
1030 * This is called by GVMMR0CreateVM and GVMMR0RegisterVCpu. If the host
1031 * platform does not implement switcher hooks, no hooks will be create and the
1032 * member set to NIL_RTTHREADCTXHOOK.
1033 *
1034 * @returns VBox status code.
1035 * @param pVCpu The cross context virtual CPU structure.
1036 * @thread EMT(pVCpu)
1037 */
1038VMMR0_INT_DECL(int) VMMR0ThreadCtxHookCreateForEmt(PVMCPUCC pVCpu)
1039{
1040 VMCPU_ASSERT_EMT(pVCpu);
1041 Assert(pVCpu->vmmr0.s.hCtxHook == NIL_RTTHREADCTXHOOK);
1042
1043#if 1 /* To disable this stuff change to zero. */
1044 int rc = RTThreadCtxHookCreate(&pVCpu->vmmr0.s.hCtxHook, 0, vmmR0ThreadCtxCallback, pVCpu);
1045 if (RT_SUCCESS(rc))
1046 {
1047 pVCpu->pGVM->vmm.s.fIsUsingContextHooks = true;
1048 return rc;
1049 }
1050#else
1051 RT_NOREF(vmmR0ThreadCtxCallback);
1052 int rc = VERR_NOT_SUPPORTED;
1053#endif
1054
1055 pVCpu->vmmr0.s.hCtxHook = NIL_RTTHREADCTXHOOK;
1056 pVCpu->pGVM->vmm.s.fIsUsingContextHooks = false;
1057 if (rc == VERR_NOT_SUPPORTED)
1058 return VINF_SUCCESS;
1059
1060 LogRelMax(32, ("RTThreadCtxHookCreate failed! rc=%Rrc pVCpu=%p idCpu=%RU32\n", rc, pVCpu, pVCpu->idCpu));
1061 return VINF_SUCCESS; /* Just ignore it, we can live without context hooks. */
1062}
1063
1064
1065/**
1066 * Destroys the thread switching hook for the specified VCPU.
1067 *
1068 * @param pVCpu The cross context virtual CPU structure.
1069 * @remarks Can be called from any thread.
1070 */
1071VMMR0_INT_DECL(void) VMMR0ThreadCtxHookDestroyForEmt(PVMCPUCC pVCpu)
1072{
1073 int rc = RTThreadCtxHookDestroy(pVCpu->vmmr0.s.hCtxHook);
1074 AssertRC(rc);
1075 pVCpu->vmmr0.s.hCtxHook = NIL_RTTHREADCTXHOOK;
1076}
1077
1078
1079/**
1080 * Disables the thread switching hook for this VCPU (if we got one).
1081 *
1082 * @param pVCpu The cross context virtual CPU structure.
1083 * @thread EMT(pVCpu)
1084 *
1085 * @remarks This also clears GVMCPU::idHostCpu, so the mapping is invalid after
1086 * this call. This means you have to be careful with what you do!
1087 */
1088VMMR0_INT_DECL(void) VMMR0ThreadCtxHookDisable(PVMCPUCC pVCpu)
1089{
1090 /*
1091 * Clear the VCPU <-> host CPU mapping as we've left HM context.
1092 * @bugref{7726#c19} explains the need for this trick:
1093 *
1094 * VMXR0CallRing3Callback/SVMR0CallRing3Callback &
1095 * hmR0VmxLeaveSession/hmR0SvmLeaveSession disables context hooks during
1096 * longjmp & normal return to ring-3, which opens a window where we may be
1097 * rescheduled without changing GVMCPUID::idHostCpu and cause confusion if
1098 * the CPU starts executing a different EMT. Both functions first disables
1099 * preemption and then calls HMR0LeaveCpu which invalids idHostCpu, leaving
1100 * an opening for getting preempted.
1101 */
1102 /** @todo Make HM not need this API! Then we could leave the hooks enabled
1103 * all the time. */
1104
1105 /*
1106 * Disable the context hook, if we got one.
1107 */
1108 if (pVCpu->vmmr0.s.hCtxHook != NIL_RTTHREADCTXHOOK)
1109 {
1110 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
1111 ASMAtomicWriteU32(&pVCpu->idHostCpu, NIL_RTCPUID);
1112 int rc = RTThreadCtxHookDisable(pVCpu->vmmr0.s.hCtxHook);
1113 AssertRC(rc);
1114 }
1115}
1116
1117
1118/**
1119 * Internal version of VMMR0ThreadCtxHooksAreRegistered.
1120 *
1121 * @returns true if registered, false otherwise.
1122 * @param pVCpu The cross context virtual CPU structure.
1123 */
1124DECLINLINE(bool) vmmR0ThreadCtxHookIsEnabled(PVMCPUCC pVCpu)
1125{
1126 return RTThreadCtxHookIsEnabled(pVCpu->vmmr0.s.hCtxHook);
1127}
1128
1129
1130/**
1131 * Whether thread-context hooks are registered for this VCPU.
1132 *
1133 * @returns true if registered, false otherwise.
1134 * @param pVCpu The cross context virtual CPU structure.
1135 */
1136VMMR0_INT_DECL(bool) VMMR0ThreadCtxHookIsEnabled(PVMCPUCC pVCpu)
1137{
1138 return vmmR0ThreadCtxHookIsEnabled(pVCpu);
1139}
1140
1141
1142/**
1143 * Returns the ring-0 release logger instance.
1144 *
1145 * @returns Pointer to release logger, NULL if not configured.
1146 * @param pVCpu The cross context virtual CPU structure of the caller.
1147 * @thread EMT(pVCpu)
1148 */
1149VMMR0_INT_DECL(PRTLOGGER) VMMR0GetReleaseLogger(PVMCPUCC pVCpu)
1150{
1151 return pVCpu->vmmr0.s.u.s.RelLogger.pLogger;
1152}
1153
1154
1155#ifdef VBOX_WITH_STATISTICS
1156/**
1157 * Record return code statistics
1158 * @param pVM The cross context VM structure.
1159 * @param pVCpu The cross context virtual CPU structure.
1160 * @param rc The status code.
1161 */
1162static void vmmR0RecordRC(PVMCC pVM, PVMCPUCC pVCpu, int rc)
1163{
1164 /*
1165 * Collect statistics.
1166 */
1167 switch (rc)
1168 {
1169 case VINF_SUCCESS:
1170 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetNormal);
1171 break;
1172 case VINF_EM_RAW_INTERRUPT:
1173 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetInterrupt);
1174 break;
1175 case VINF_EM_RAW_INTERRUPT_HYPER:
1176 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetInterruptHyper);
1177 break;
1178 case VINF_EM_RAW_GUEST_TRAP:
1179 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetGuestTrap);
1180 break;
1181 case VINF_EM_RAW_RING_SWITCH:
1182 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetRingSwitch);
1183 break;
1184 case VINF_EM_RAW_RING_SWITCH_INT:
1185 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetRingSwitchInt);
1186 break;
1187 case VINF_EM_RAW_STALE_SELECTOR:
1188 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetStaleSelector);
1189 break;
1190 case VINF_EM_RAW_IRET_TRAP:
1191 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIRETTrap);
1192 break;
1193 case VINF_IOM_R3_IOPORT_READ:
1194 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIORead);
1195 break;
1196 case VINF_IOM_R3_IOPORT_WRITE:
1197 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIOWrite);
1198 break;
1199 case VINF_IOM_R3_IOPORT_COMMIT_WRITE:
1200 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIOCommitWrite);
1201 break;
1202 case VINF_IOM_R3_MMIO_READ:
1203 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIORead);
1204 break;
1205 case VINF_IOM_R3_MMIO_WRITE:
1206 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOWrite);
1207 break;
1208 case VINF_IOM_R3_MMIO_COMMIT_WRITE:
1209 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOCommitWrite);
1210 break;
1211 case VINF_IOM_R3_MMIO_READ_WRITE:
1212 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOReadWrite);
1213 break;
1214 case VINF_PATM_HC_MMIO_PATCH_READ:
1215 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOPatchRead);
1216 break;
1217 case VINF_PATM_HC_MMIO_PATCH_WRITE:
1218 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOPatchWrite);
1219 break;
1220 case VINF_CPUM_R3_MSR_READ:
1221 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMSRRead);
1222 break;
1223 case VINF_CPUM_R3_MSR_WRITE:
1224 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMSRWrite);
1225 break;
1226 case VINF_EM_RAW_EMULATE_INSTR:
1227 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetEmulate);
1228 break;
1229 case VINF_PATCH_EMULATE_INSTR:
1230 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchEmulate);
1231 break;
1232 case VINF_EM_RAW_EMULATE_INSTR_LDT_FAULT:
1233 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetLDTFault);
1234 break;
1235 case VINF_EM_RAW_EMULATE_INSTR_GDT_FAULT:
1236 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetGDTFault);
1237 break;
1238 case VINF_EM_RAW_EMULATE_INSTR_IDT_FAULT:
1239 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIDTFault);
1240 break;
1241 case VINF_EM_RAW_EMULATE_INSTR_TSS_FAULT:
1242 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetTSSFault);
1243 break;
1244 case VINF_CSAM_PENDING_ACTION:
1245 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetCSAMTask);
1246 break;
1247 case VINF_PGM_SYNC_CR3:
1248 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetSyncCR3);
1249 break;
1250 case VINF_PATM_PATCH_INT3:
1251 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchInt3);
1252 break;
1253 case VINF_PATM_PATCH_TRAP_PF:
1254 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchPF);
1255 break;
1256 case VINF_PATM_PATCH_TRAP_GP:
1257 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchGP);
1258 break;
1259 case VINF_PATM_PENDING_IRQ_AFTER_IRET:
1260 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchIretIRQ);
1261 break;
1262 case VINF_EM_RESCHEDULE_REM:
1263 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetRescheduleREM);
1264 break;
1265 case VINF_EM_RAW_TO_R3:
1266 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3Total);
1267 if (VM_FF_IS_SET(pVM, VM_FF_TM_VIRTUAL_SYNC))
1268 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3TMVirt);
1269 else if (VM_FF_IS_SET(pVM, VM_FF_PGM_NEED_HANDY_PAGES))
1270 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3HandyPages);
1271 else if (VM_FF_IS_SET(pVM, VM_FF_PDM_QUEUES))
1272 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3PDMQueues);
1273 else if (VM_FF_IS_SET(pVM, VM_FF_EMT_RENDEZVOUS))
1274 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3Rendezvous);
1275 else if (VM_FF_IS_SET(pVM, VM_FF_PDM_DMA))
1276 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3DMA);
1277 else if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_TIMER))
1278 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3Timer);
1279 else if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_PDM_CRITSECT))
1280 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3CritSect);
1281 else if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_TO_R3))
1282 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3FF);
1283 else if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_IEM))
1284 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3Iem);
1285 else if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_IOM))
1286 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3Iom);
1287 else
1288 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3Unknown);
1289 break;
1290
1291 case VINF_EM_RAW_TIMER_PENDING:
1292 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetTimerPending);
1293 break;
1294 case VINF_EM_RAW_INTERRUPT_PENDING:
1295 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetInterruptPending);
1296 break;
1297 case VINF_PATM_DUPLICATE_FUNCTION:
1298 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPATMDuplicateFn);
1299 break;
1300 case VINF_PGM_POOL_FLUSH_PENDING:
1301 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPGMFlushPending);
1302 break;
1303 case VINF_EM_PENDING_REQUEST:
1304 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPendingRequest);
1305 break;
1306 case VINF_EM_HM_PATCH_TPR_INSTR:
1307 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchTPR);
1308 break;
1309 default:
1310 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMisc);
1311 break;
1312 }
1313}
1314#endif /* VBOX_WITH_STATISTICS */
1315
1316
1317/**
1318 * The Ring 0 entry point, called by the fast-ioctl path.
1319 *
1320 * @param pGVM The global (ring-0) VM structure.
1321 * @param pVMIgnored The cross context VM structure. The return code is
1322 * stored in pVM->vmm.s.iLastGZRc.
1323 * @param idCpu The Virtual CPU ID of the calling EMT.
1324 * @param enmOperation Which operation to execute.
1325 * @remarks Assume called with interrupts _enabled_.
1326 */
1327VMMR0DECL(void) VMMR0EntryFast(PGVM pGVM, PVMCC pVMIgnored, VMCPUID idCpu, VMMR0OPERATION enmOperation)
1328{
1329 RT_NOREF(pVMIgnored);
1330
1331 /*
1332 * Validation.
1333 */
1334 if ( idCpu < pGVM->cCpus
1335 && pGVM->cCpus == pGVM->cCpusUnsafe)
1336 { /*likely*/ }
1337 else
1338 {
1339 SUPR0Printf("VMMR0EntryFast: Bad idCpu=%#x cCpus=%#x cCpusUnsafe=%#x\n", idCpu, pGVM->cCpus, pGVM->cCpusUnsafe);
1340 return;
1341 }
1342
1343 PGVMCPU pGVCpu = &pGVM->aCpus[idCpu];
1344 RTNATIVETHREAD const hNativeThread = RTThreadNativeSelf();
1345 if (RT_LIKELY( pGVCpu->hEMT == hNativeThread
1346 && pGVCpu->hNativeThreadR0 == hNativeThread))
1347 { /* likely */ }
1348 else
1349 {
1350 SUPR0Printf("VMMR0EntryFast: Bad thread idCpu=%#x hNativeSelf=%p pGVCpu->hEmt=%p pGVCpu->hNativeThreadR0=%p\n",
1351 idCpu, hNativeThread, pGVCpu->hEMT, pGVCpu->hNativeThreadR0);
1352 return;
1353 }
1354
1355 /*
1356 * Perform requested operation.
1357 */
1358 switch (enmOperation)
1359 {
1360 /*
1361 * Run guest code using the available hardware acceleration technology.
1362 */
1363 case VMMR0_DO_HM_RUN:
1364 {
1365 for (;;) /* hlt loop */
1366 {
1367 /*
1368 * Disable ring-3 calls & blocking till we've successfully entered HM.
1369 * Otherwise we sometimes end up blocking at the finall Log4 statement
1370 * in VMXR0Enter, while still in a somewhat inbetween state.
1371 */
1372 VMMRZCallRing3Disable(pGVCpu);
1373
1374 /*
1375 * Disable preemption.
1376 */
1377 Assert(!vmmR0ThreadCtxHookIsEnabled(pGVCpu));
1378 RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
1379 RTThreadPreemptDisable(&PreemptState);
1380 pGVCpu->vmmr0.s.pPreemptState = &PreemptState;
1381
1382 /*
1383 * Get the host CPU identifiers, make sure they are valid and that
1384 * we've got a TSC delta for the CPU.
1385 */
1386 RTCPUID idHostCpu;
1387 uint32_t iHostCpuSet = RTMpCurSetIndexAndId(&idHostCpu);
1388 if (RT_LIKELY( iHostCpuSet < RTCPUSET_MAX_CPUS
1389 && SUPIsTscDeltaAvailableForCpuSetIndex(iHostCpuSet)))
1390 {
1391 pGVCpu->iHostCpuSet = iHostCpuSet;
1392 ASMAtomicWriteU32(&pGVCpu->idHostCpu, idHostCpu);
1393
1394 /*
1395 * Update the periodic preemption timer if it's active.
1396 */
1397 if (pGVM->vmm.s.fUsePeriodicPreemptionTimers)
1398 GVMMR0SchedUpdatePeriodicPreemptionTimer(pGVM, pGVCpu->idHostCpu, TMCalcHostTimerFrequency(pGVM, pGVCpu));
1399
1400#ifdef VMM_R0_TOUCH_FPU
1401 /*
1402 * Make sure we've got the FPU state loaded so and we don't need to clear
1403 * CR0.TS and get out of sync with the host kernel when loading the guest
1404 * FPU state. @ref sec_cpum_fpu (CPUM.cpp) and @bugref{4053}.
1405 */
1406 CPUMR0TouchHostFpu();
1407#endif
1408 int rc;
1409 bool fPreemptRestored = false;
1410 if (!HMR0SuspendPending())
1411 {
1412 /*
1413 * Enable the context switching hook.
1414 */
1415 if (pGVCpu->vmmr0.s.hCtxHook != NIL_RTTHREADCTXHOOK)
1416 {
1417 Assert(!RTThreadCtxHookIsEnabled(pGVCpu->vmmr0.s.hCtxHook));
1418 int rc2 = RTThreadCtxHookEnable(pGVCpu->vmmr0.s.hCtxHook); AssertRC(rc2);
1419 }
1420
1421 /*
1422 * Enter HM context.
1423 */
1424 rc = HMR0Enter(pGVCpu);
1425 if (RT_SUCCESS(rc))
1426 {
1427 VMCPU_SET_STATE(pGVCpu, VMCPUSTATE_STARTED_HM);
1428
1429 /*
1430 * When preemption hooks are in place, enable preemption now that
1431 * we're in HM context.
1432 */
1433 if (vmmR0ThreadCtxHookIsEnabled(pGVCpu))
1434 {
1435 fPreemptRestored = true;
1436 pGVCpu->vmmr0.s.pPreemptState = NULL;
1437 RTThreadPreemptRestore(&PreemptState);
1438 }
1439 VMMRZCallRing3Enable(pGVCpu);
1440
1441 /*
1442 * Setup the longjmp machinery and execute guest code (calls HMR0RunGuestCode).
1443 */
1444 rc = vmmR0CallRing3SetJmp(&pGVCpu->vmmr0.s.AssertJmpBuf, HMR0RunGuestCode, pGVM, pGVCpu);
1445
1446 /*
1447 * Assert sanity on the way out. Using manual assertions code here as normal
1448 * assertions are going to panic the host since we're outside the setjmp/longjmp zone.
1449 */
1450 if (RT_UNLIKELY( VMCPU_GET_STATE(pGVCpu) != VMCPUSTATE_STARTED_HM
1451 && RT_SUCCESS_NP(rc)
1452 && rc != VERR_VMM_RING0_ASSERTION ))
1453 {
1454 pGVM->vmm.s.szRing0AssertMsg1[0] = '\0';
1455 RTStrPrintf(pGVM->vmm.s.szRing0AssertMsg2, sizeof(pGVM->vmm.s.szRing0AssertMsg2),
1456 "Got VMCPU state %d expected %d.\n", VMCPU_GET_STATE(pGVCpu), VMCPUSTATE_STARTED_HM);
1457 rc = VERR_VMM_WRONG_HM_VMCPU_STATE;
1458 }
1459#if 0
1460 /** @todo Get rid of this. HM shouldn't disable the context hook. */
1461 else if (RT_UNLIKELY(vmmR0ThreadCtxHookIsEnabled(pGVCpu)))
1462 {
1463 pGVM->vmm.s.szRing0AssertMsg1[0] = '\0';
1464 RTStrPrintf(pGVM->vmm.s.szRing0AssertMsg2, sizeof(pGVM->vmm.s.szRing0AssertMsg2),
1465 "Thread-context hooks still enabled! VCPU=%p Id=%u rc=%d.\n", pGVCpu, pGVCpu->idCpu, rc);
1466 rc = VERR_VMM_CONTEXT_HOOK_STILL_ENABLED;
1467 }
1468#endif
1469
1470 VMMRZCallRing3Disable(pGVCpu); /* Lazy bird: Simpler just disabling it again... */
1471 VMCPU_SET_STATE(pGVCpu, VMCPUSTATE_STARTED);
1472 }
1473 STAM_COUNTER_INC(&pGVM->vmm.s.StatRunGC);
1474
1475 /*
1476 * Invalidate the host CPU identifiers before we disable the context
1477 * hook / restore preemption.
1478 */
1479 pGVCpu->iHostCpuSet = UINT32_MAX;
1480 ASMAtomicWriteU32(&pGVCpu->idHostCpu, NIL_RTCPUID);
1481
1482 /*
1483 * Disable context hooks. Due to unresolved cleanup issues, we
1484 * cannot leave the hooks enabled when we return to ring-3.
1485 *
1486 * Note! At the moment HM may also have disabled the hook
1487 * when we get here, but the IPRT API handles that.
1488 */
1489 if (pGVCpu->vmmr0.s.hCtxHook != NIL_RTTHREADCTXHOOK)
1490 RTThreadCtxHookDisable(pGVCpu->vmmr0.s.hCtxHook);
1491 }
1492 /*
1493 * The system is about to go into suspend mode; go back to ring 3.
1494 */
1495 else
1496 {
1497 pGVCpu->iHostCpuSet = UINT32_MAX;
1498 ASMAtomicWriteU32(&pGVCpu->idHostCpu, NIL_RTCPUID);
1499 rc = VINF_EM_RAW_INTERRUPT;
1500 }
1501
1502 /** @todo When HM stops messing with the context hook state, we'll disable
1503 * preemption again before the RTThreadCtxHookDisable call. */
1504 if (!fPreemptRestored)
1505 {
1506 pGVCpu->vmmr0.s.pPreemptState = NULL;
1507 RTThreadPreemptRestore(&PreemptState);
1508 }
1509
1510 pGVCpu->vmm.s.iLastGZRc = rc;
1511
1512 /* Fire dtrace probe and collect statistics. */
1513 VBOXVMM_R0_VMM_RETURN_TO_RING3_HM(pGVCpu, CPUMQueryGuestCtxPtr(pGVCpu), rc);
1514#ifdef VBOX_WITH_STATISTICS
1515 vmmR0RecordRC(pGVM, pGVCpu, rc);
1516#endif
1517 VMMRZCallRing3Enable(pGVCpu);
1518
1519 /*
1520 * If this is a halt.
1521 */
1522 if (rc != VINF_EM_HALT)
1523 { /* we're not in a hurry for a HLT, so prefer this path */ }
1524 else
1525 {
1526 pGVCpu->vmm.s.iLastGZRc = rc = vmmR0DoHalt(pGVM, pGVCpu);
1527 if (rc == VINF_SUCCESS)
1528 {
1529 pGVCpu->vmm.s.cR0HaltsSucceeded++;
1530 continue;
1531 }
1532 pGVCpu->vmm.s.cR0HaltsToRing3++;
1533 }
1534 }
1535 /*
1536 * Invalid CPU set index or TSC delta in need of measuring.
1537 */
1538 else
1539 {
1540 pGVCpu->vmmr0.s.pPreemptState = NULL;
1541 pGVCpu->iHostCpuSet = UINT32_MAX;
1542 ASMAtomicWriteU32(&pGVCpu->idHostCpu, NIL_RTCPUID);
1543 RTThreadPreemptRestore(&PreemptState);
1544
1545 VMMRZCallRing3Enable(pGVCpu);
1546
1547 if (iHostCpuSet < RTCPUSET_MAX_CPUS)
1548 {
1549 int rc = SUPR0TscDeltaMeasureBySetIndex(pGVM->pSession, iHostCpuSet, 0 /*fFlags*/,
1550 2 /*cMsWaitRetry*/, 5*RT_MS_1SEC /*cMsWaitThread*/,
1551 0 /*default cTries*/);
1552 if (RT_SUCCESS(rc) || rc == VERR_CPU_OFFLINE)
1553 pGVCpu->vmm.s.iLastGZRc = VINF_EM_RAW_TO_R3;
1554 else
1555 pGVCpu->vmm.s.iLastGZRc = rc;
1556 }
1557 else
1558 pGVCpu->vmm.s.iLastGZRc = VERR_INVALID_CPU_INDEX;
1559 }
1560 break;
1561 } /* halt loop. */
1562 break;
1563 }
1564
1565#ifdef VBOX_WITH_NEM_R0
1566# if defined(RT_ARCH_AMD64) && defined(RT_OS_WINDOWS)
1567 case VMMR0_DO_NEM_RUN:
1568 {
1569 /*
1570 * Setup the longjmp machinery and execute guest code (calls NEMR0RunGuestCode).
1571 */
1572# ifdef VBOXSTRICTRC_STRICT_ENABLED
1573 int rc = vmmR0CallRing3SetJmp2(&pGVCpu->vmmr0.s.AssertJmpBuf, (PFNVMMR0SETJMP2)NEMR0RunGuestCode, pGVM, idCpu);
1574# else
1575 int rc = vmmR0CallRing3SetJmp2(&pGVCpu->vmmr0.s.AssertJmpBuf, NEMR0RunGuestCode, pGVM, idCpu);
1576# endif
1577 STAM_COUNTER_INC(&pGVM->vmm.s.StatRunGC);
1578
1579 pGVCpu->vmm.s.iLastGZRc = rc;
1580
1581 /*
1582 * Fire dtrace probe and collect statistics.
1583 */
1584 VBOXVMM_R0_VMM_RETURN_TO_RING3_NEM(pGVCpu, CPUMQueryGuestCtxPtr(pGVCpu), rc);
1585# ifdef VBOX_WITH_STATISTICS
1586 vmmR0RecordRC(pGVM, pGVCpu, rc);
1587# endif
1588 break;
1589 }
1590# endif
1591#endif
1592
1593 /*
1594 * For profiling.
1595 */
1596 case VMMR0_DO_NOP:
1597 pGVCpu->vmm.s.iLastGZRc = VINF_SUCCESS;
1598 break;
1599
1600 /*
1601 * Shouldn't happen.
1602 */
1603 default:
1604 AssertMsgFailed(("%#x\n", enmOperation));
1605 pGVCpu->vmm.s.iLastGZRc = VERR_NOT_SUPPORTED;
1606 break;
1607 }
1608}
1609
1610
1611/**
1612 * Validates a session or VM session argument.
1613 *
1614 * @returns true / false accordingly.
1615 * @param pGVM The global (ring-0) VM structure.
1616 * @param pClaimedSession The session claim to validate.
1617 * @param pSession The session argument.
1618 */
1619DECLINLINE(bool) vmmR0IsValidSession(PGVM pGVM, PSUPDRVSESSION pClaimedSession, PSUPDRVSESSION pSession)
1620{
1621 /* This must be set! */
1622 if (!pSession)
1623 return false;
1624
1625 /* Only one out of the two. */
1626 if (pGVM && pClaimedSession)
1627 return false;
1628 if (pGVM)
1629 pClaimedSession = pGVM->pSession;
1630 return pClaimedSession == pSession;
1631}
1632
1633
1634/**
1635 * VMMR0EntryEx worker function, either called directly or when ever possible
1636 * called thru a longjmp so we can exit safely on failure.
1637 *
1638 * @returns VBox status code.
1639 * @param pGVM The global (ring-0) VM structure.
1640 * @param idCpu Virtual CPU ID argument. Must be NIL_VMCPUID if pVM
1641 * is NIL_RTR0PTR, and may be NIL_VMCPUID if it isn't
1642 * @param enmOperation Which operation to execute.
1643 * @param pReqHdr This points to a SUPVMMR0REQHDR packet. Optional.
1644 * The support driver validates this if it's present.
1645 * @param u64Arg Some simple constant argument.
1646 * @param pSession The session of the caller.
1647 *
1648 * @remarks Assume called with interrupts _enabled_.
1649 */
1650DECL_NO_INLINE(static, int) vmmR0EntryExWorker(PGVM pGVM, VMCPUID idCpu, VMMR0OPERATION enmOperation,
1651 PSUPVMMR0REQHDR pReqHdr, uint64_t u64Arg, PSUPDRVSESSION pSession)
1652{
1653 /*
1654 * Validate pGVM and idCpu for consistency and validity.
1655 */
1656 if (pGVM != NULL)
1657 {
1658 if (RT_LIKELY(((uintptr_t)pGVM & PAGE_OFFSET_MASK) == 0))
1659 { /* likely */ }
1660 else
1661 {
1662 SUPR0Printf("vmmR0EntryExWorker: Invalid pGVM=%p! (op=%d)\n", pGVM, enmOperation);
1663 return VERR_INVALID_POINTER;
1664 }
1665
1666 if (RT_LIKELY(idCpu == NIL_VMCPUID || idCpu < pGVM->cCpus))
1667 { /* likely */ }
1668 else
1669 {
1670 SUPR0Printf("vmmR0EntryExWorker: Invalid idCpu %#x (cCpus=%#x)\n", idCpu, pGVM->cCpus);
1671 return VERR_INVALID_PARAMETER;
1672 }
1673
1674 if (RT_LIKELY( pGVM->enmVMState >= VMSTATE_CREATING
1675 && pGVM->enmVMState <= VMSTATE_TERMINATED
1676 && pGVM->pSession == pSession
1677 && pGVM->pSelf == pGVM))
1678 { /* likely */ }
1679 else
1680 {
1681 SUPR0Printf("vmmR0EntryExWorker: Invalid pGVM=%p:{.enmVMState=%d, .cCpus=%#x, .pSession=%p(==%p), .pSelf=%p(==%p)}! (op=%d)\n",
1682 pGVM, pGVM->enmVMState, pGVM->cCpus, pGVM->pSession, pSession, pGVM->pSelf, pGVM, enmOperation);
1683 return VERR_INVALID_POINTER;
1684 }
1685 }
1686 else if (RT_LIKELY(idCpu == NIL_VMCPUID))
1687 { /* likely */ }
1688 else
1689 {
1690 SUPR0Printf("vmmR0EntryExWorker: Invalid idCpu=%u\n", idCpu);
1691 return VERR_INVALID_PARAMETER;
1692 }
1693
1694 /*
1695 * Process the request.
1696 */
1697 int rc;
1698 switch (enmOperation)
1699 {
1700 /*
1701 * GVM requests
1702 */
1703 case VMMR0_DO_GVMM_CREATE_VM:
1704 if (pGVM == NULL && u64Arg == 0 && idCpu == NIL_VMCPUID)
1705 rc = GVMMR0CreateVMReq((PGVMMCREATEVMREQ)pReqHdr, pSession);
1706 else
1707 rc = VERR_INVALID_PARAMETER;
1708 break;
1709
1710 case VMMR0_DO_GVMM_DESTROY_VM:
1711 if (pReqHdr == NULL && u64Arg == 0)
1712 rc = GVMMR0DestroyVM(pGVM);
1713 else
1714 rc = VERR_INVALID_PARAMETER;
1715 break;
1716
1717 case VMMR0_DO_GVMM_REGISTER_VMCPU:
1718 if (pGVM != NULL)
1719 rc = GVMMR0RegisterVCpu(pGVM, idCpu);
1720 else
1721 rc = VERR_INVALID_PARAMETER;
1722 break;
1723
1724 case VMMR0_DO_GVMM_DEREGISTER_VMCPU:
1725 if (pGVM != NULL)
1726 rc = GVMMR0DeregisterVCpu(pGVM, idCpu);
1727 else
1728 rc = VERR_INVALID_PARAMETER;
1729 break;
1730
1731 case VMMR0_DO_GVMM_REGISTER_WORKER_THREAD:
1732 if (pGVM != NULL && pReqHdr && pReqHdr->cbReq == sizeof(GVMMREGISTERWORKERTHREADREQ))
1733 rc = GVMMR0RegisterWorkerThread(pGVM, (GVMMWORKERTHREAD)(unsigned)u64Arg,
1734 ((PGVMMREGISTERWORKERTHREADREQ)(pReqHdr))->hNativeThreadR3);
1735 else
1736 rc = VERR_INVALID_PARAMETER;
1737 break;
1738
1739 case VMMR0_DO_GVMM_DEREGISTER_WORKER_THREAD:
1740 if (pGVM != NULL)
1741 rc = GVMMR0DeregisterWorkerThread(pGVM, (GVMMWORKERTHREAD)(unsigned)u64Arg);
1742 else
1743 rc = VERR_INVALID_PARAMETER;
1744 break;
1745
1746 case VMMR0_DO_GVMM_SCHED_HALT:
1747 if (pReqHdr)
1748 return VERR_INVALID_PARAMETER;
1749 rc = GVMMR0SchedHaltReq(pGVM, idCpu, u64Arg);
1750 break;
1751
1752 case VMMR0_DO_GVMM_SCHED_WAKE_UP:
1753 if (pReqHdr || u64Arg)
1754 return VERR_INVALID_PARAMETER;
1755 rc = GVMMR0SchedWakeUp(pGVM, idCpu);
1756 break;
1757
1758 case VMMR0_DO_GVMM_SCHED_POKE:
1759 if (pReqHdr || u64Arg)
1760 return VERR_INVALID_PARAMETER;
1761 rc = GVMMR0SchedPoke(pGVM, idCpu);
1762 break;
1763
1764 case VMMR0_DO_GVMM_SCHED_WAKE_UP_AND_POKE_CPUS:
1765 if (u64Arg)
1766 return VERR_INVALID_PARAMETER;
1767 rc = GVMMR0SchedWakeUpAndPokeCpusReq(pGVM, (PGVMMSCHEDWAKEUPANDPOKECPUSREQ)pReqHdr);
1768 break;
1769
1770 case VMMR0_DO_GVMM_SCHED_POLL:
1771 if (pReqHdr || u64Arg > 1)
1772 return VERR_INVALID_PARAMETER;
1773 rc = GVMMR0SchedPoll(pGVM, idCpu, !!u64Arg);
1774 break;
1775
1776 case VMMR0_DO_GVMM_QUERY_STATISTICS:
1777 if (u64Arg)
1778 return VERR_INVALID_PARAMETER;
1779 rc = GVMMR0QueryStatisticsReq(pGVM, (PGVMMQUERYSTATISTICSSREQ)pReqHdr, pSession);
1780 break;
1781
1782 case VMMR0_DO_GVMM_RESET_STATISTICS:
1783 if (u64Arg)
1784 return VERR_INVALID_PARAMETER;
1785 rc = GVMMR0ResetStatisticsReq(pGVM, (PGVMMRESETSTATISTICSSREQ)pReqHdr, pSession);
1786 break;
1787
1788 /*
1789 * Initialize the R0 part of a VM instance.
1790 */
1791 case VMMR0_DO_VMMR0_INIT:
1792 rc = vmmR0InitVM(pGVM, RT_LODWORD(u64Arg), RT_HIDWORD(u64Arg));
1793 break;
1794
1795 /*
1796 * Does EMT specific ring-0 init.
1797 */
1798 case VMMR0_DO_VMMR0_INIT_EMT:
1799 rc = vmmR0InitVMEmt(pGVM, idCpu);
1800 break;
1801
1802 /*
1803 * Terminate the R0 part of a VM instance.
1804 */
1805 case VMMR0_DO_VMMR0_TERM:
1806 rc = VMMR0TermVM(pGVM, 0 /*idCpu*/);
1807 break;
1808
1809 /*
1810 * Update release or debug logger instances.
1811 */
1812 case VMMR0_DO_VMMR0_UPDATE_LOGGERS:
1813 if (idCpu == NIL_VMCPUID)
1814 return VERR_INVALID_CPU_ID;
1815 if (u64Arg < VMMLOGGER_IDX_MAX && pReqHdr != NULL)
1816 rc = vmmR0UpdateLoggers(pGVM, idCpu /*idCpu*/, (PVMMR0UPDATELOGGERSREQ)pReqHdr, (size_t)u64Arg);
1817 else
1818 return VERR_INVALID_PARAMETER;
1819 break;
1820
1821 /*
1822 * Log flusher thread.
1823 */
1824 case VMMR0_DO_VMMR0_LOG_FLUSHER:
1825 if (idCpu != NIL_VMCPUID)
1826 return VERR_INVALID_CPU_ID;
1827 if (pReqHdr == NULL)
1828 rc = vmmR0LogFlusher(pGVM);
1829 else
1830 return VERR_INVALID_PARAMETER;
1831 break;
1832
1833 /*
1834 * Wait for the flush to finish with all the buffers for the given logger.
1835 */
1836 case VMMR0_DO_VMMR0_LOG_WAIT_FLUSHED:
1837 if (idCpu == NIL_VMCPUID)
1838 return VERR_INVALID_CPU_ID;
1839 if (u64Arg < VMMLOGGER_IDX_MAX && pReqHdr == NULL)
1840 rc = vmmR0LogWaitFlushed(pGVM, idCpu /*idCpu*/, (size_t)u64Arg);
1841 else
1842 return VERR_INVALID_PARAMETER;
1843 break;
1844
1845 /*
1846 * Attempt to enable hm mode and check the current setting.
1847 */
1848 case VMMR0_DO_HM_ENABLE:
1849 rc = HMR0EnableAllCpus(pGVM);
1850 break;
1851
1852 /*
1853 * Setup the hardware accelerated session.
1854 */
1855 case VMMR0_DO_HM_SETUP_VM:
1856 rc = HMR0SetupVM(pGVM);
1857 break;
1858
1859 /*
1860 * PGM wrappers.
1861 */
1862 case VMMR0_DO_PGM_ALLOCATE_HANDY_PAGES:
1863 if (idCpu == NIL_VMCPUID)
1864 return VERR_INVALID_CPU_ID;
1865 rc = PGMR0PhysAllocateHandyPages(pGVM, idCpu);
1866 break;
1867
1868 case VMMR0_DO_PGM_FLUSH_HANDY_PAGES:
1869 if (idCpu == NIL_VMCPUID)
1870 return VERR_INVALID_CPU_ID;
1871 rc = PGMR0PhysFlushHandyPages(pGVM, idCpu);
1872 break;
1873
1874 case VMMR0_DO_PGM_ALLOCATE_LARGE_PAGE:
1875 if (idCpu == NIL_VMCPUID)
1876 return VERR_INVALID_CPU_ID;
1877 rc = PGMR0PhysAllocateLargePage(pGVM, idCpu, u64Arg);
1878 break;
1879
1880 case VMMR0_DO_PGM_PHYS_SETUP_IOMMU:
1881 if (idCpu != 0)
1882 return VERR_INVALID_CPU_ID;
1883 rc = PGMR0PhysSetupIoMmu(pGVM);
1884 break;
1885
1886 case VMMR0_DO_PGM_POOL_GROW:
1887 if (idCpu == NIL_VMCPUID)
1888 return VERR_INVALID_CPU_ID;
1889 rc = PGMR0PoolGrow(pGVM, idCpu);
1890 break;
1891
1892 /*
1893 * GMM wrappers.
1894 */
1895 case VMMR0_DO_GMM_INITIAL_RESERVATION:
1896 if (u64Arg)
1897 return VERR_INVALID_PARAMETER;
1898 rc = GMMR0InitialReservationReq(pGVM, idCpu, (PGMMINITIALRESERVATIONREQ)pReqHdr);
1899 break;
1900
1901 case VMMR0_DO_GMM_UPDATE_RESERVATION:
1902 if (u64Arg)
1903 return VERR_INVALID_PARAMETER;
1904 rc = GMMR0UpdateReservationReq(pGVM, idCpu, (PGMMUPDATERESERVATIONREQ)pReqHdr);
1905 break;
1906
1907 case VMMR0_DO_GMM_ALLOCATE_PAGES:
1908 if (u64Arg)
1909 return VERR_INVALID_PARAMETER;
1910 rc = GMMR0AllocatePagesReq(pGVM, idCpu, (PGMMALLOCATEPAGESREQ)pReqHdr);
1911 break;
1912
1913 case VMMR0_DO_GMM_FREE_PAGES:
1914 if (u64Arg)
1915 return VERR_INVALID_PARAMETER;
1916 rc = GMMR0FreePagesReq(pGVM, idCpu, (PGMMFREEPAGESREQ)pReqHdr);
1917 break;
1918
1919 case VMMR0_DO_GMM_FREE_LARGE_PAGE:
1920 if (u64Arg)
1921 return VERR_INVALID_PARAMETER;
1922 rc = GMMR0FreeLargePageReq(pGVM, idCpu, (PGMMFREELARGEPAGEREQ)pReqHdr);
1923 break;
1924
1925 case VMMR0_DO_GMM_QUERY_HYPERVISOR_MEM_STATS:
1926 if (u64Arg)
1927 return VERR_INVALID_PARAMETER;
1928 rc = GMMR0QueryHypervisorMemoryStatsReq((PGMMMEMSTATSREQ)pReqHdr);
1929 break;
1930
1931 case VMMR0_DO_GMM_QUERY_MEM_STATS:
1932 if (idCpu == NIL_VMCPUID)
1933 return VERR_INVALID_CPU_ID;
1934 if (u64Arg)
1935 return VERR_INVALID_PARAMETER;
1936 rc = GMMR0QueryMemoryStatsReq(pGVM, idCpu, (PGMMMEMSTATSREQ)pReqHdr);
1937 break;
1938
1939 case VMMR0_DO_GMM_BALLOONED_PAGES:
1940 if (u64Arg)
1941 return VERR_INVALID_PARAMETER;
1942 rc = GMMR0BalloonedPagesReq(pGVM, idCpu, (PGMMBALLOONEDPAGESREQ)pReqHdr);
1943 break;
1944
1945 case VMMR0_DO_GMM_MAP_UNMAP_CHUNK:
1946 if (u64Arg)
1947 return VERR_INVALID_PARAMETER;
1948 rc = GMMR0MapUnmapChunkReq(pGVM, (PGMMMAPUNMAPCHUNKREQ)pReqHdr);
1949 break;
1950
1951 case VMMR0_DO_GMM_REGISTER_SHARED_MODULE:
1952 if (idCpu == NIL_VMCPUID)
1953 return VERR_INVALID_CPU_ID;
1954 if (u64Arg)
1955 return VERR_INVALID_PARAMETER;
1956 rc = GMMR0RegisterSharedModuleReq(pGVM, idCpu, (PGMMREGISTERSHAREDMODULEREQ)pReqHdr);
1957 break;
1958
1959 case VMMR0_DO_GMM_UNREGISTER_SHARED_MODULE:
1960 if (idCpu == NIL_VMCPUID)
1961 return VERR_INVALID_CPU_ID;
1962 if (u64Arg)
1963 return VERR_INVALID_PARAMETER;
1964 rc = GMMR0UnregisterSharedModuleReq(pGVM, idCpu, (PGMMUNREGISTERSHAREDMODULEREQ)pReqHdr);
1965 break;
1966
1967 case VMMR0_DO_GMM_RESET_SHARED_MODULES:
1968 if (idCpu == NIL_VMCPUID)
1969 return VERR_INVALID_CPU_ID;
1970 if ( u64Arg
1971 || pReqHdr)
1972 return VERR_INVALID_PARAMETER;
1973 rc = GMMR0ResetSharedModules(pGVM, idCpu);
1974 break;
1975
1976#ifdef VBOX_WITH_PAGE_SHARING
1977 case VMMR0_DO_GMM_CHECK_SHARED_MODULES:
1978 {
1979 if (idCpu == NIL_VMCPUID)
1980 return VERR_INVALID_CPU_ID;
1981 if ( u64Arg
1982 || pReqHdr)
1983 return VERR_INVALID_PARAMETER;
1984 rc = GMMR0CheckSharedModules(pGVM, idCpu);
1985 break;
1986 }
1987#endif
1988
1989#if defined(VBOX_STRICT) && HC_ARCH_BITS == 64
1990 case VMMR0_DO_GMM_FIND_DUPLICATE_PAGE:
1991 if (u64Arg)
1992 return VERR_INVALID_PARAMETER;
1993 rc = GMMR0FindDuplicatePageReq(pGVM, (PGMMFINDDUPLICATEPAGEREQ)pReqHdr);
1994 break;
1995#endif
1996
1997 case VMMR0_DO_GMM_QUERY_STATISTICS:
1998 if (u64Arg)
1999 return VERR_INVALID_PARAMETER;
2000 rc = GMMR0QueryStatisticsReq(pGVM, (PGMMQUERYSTATISTICSSREQ)pReqHdr);
2001 break;
2002
2003 case VMMR0_DO_GMM_RESET_STATISTICS:
2004 if (u64Arg)
2005 return VERR_INVALID_PARAMETER;
2006 rc = GMMR0ResetStatisticsReq(pGVM, (PGMMRESETSTATISTICSSREQ)pReqHdr);
2007 break;
2008
2009 /*
2010 * A quick GCFGM mock-up.
2011 */
2012 /** @todo GCFGM with proper access control, ring-3 management interface and all that. */
2013 case VMMR0_DO_GCFGM_SET_VALUE:
2014 case VMMR0_DO_GCFGM_QUERY_VALUE:
2015 {
2016 if (pGVM || !pReqHdr || u64Arg || idCpu != NIL_VMCPUID)
2017 return VERR_INVALID_PARAMETER;
2018 PGCFGMVALUEREQ pReq = (PGCFGMVALUEREQ)pReqHdr;
2019 if (pReq->Hdr.cbReq != sizeof(*pReq))
2020 return VERR_INVALID_PARAMETER;
2021 if (enmOperation == VMMR0_DO_GCFGM_SET_VALUE)
2022 {
2023 rc = GVMMR0SetConfig(pReq->pSession, &pReq->szName[0], pReq->u64Value);
2024 //if (rc == VERR_CFGM_VALUE_NOT_FOUND)
2025 // rc = GMMR0SetConfig(pReq->pSession, &pReq->szName[0], pReq->u64Value);
2026 }
2027 else
2028 {
2029 rc = GVMMR0QueryConfig(pReq->pSession, &pReq->szName[0], &pReq->u64Value);
2030 //if (rc == VERR_CFGM_VALUE_NOT_FOUND)
2031 // rc = GMMR0QueryConfig(pReq->pSession, &pReq->szName[0], &pReq->u64Value);
2032 }
2033 break;
2034 }
2035
2036 /*
2037 * PDM Wrappers.
2038 */
2039 case VMMR0_DO_PDM_DRIVER_CALL_REQ_HANDLER:
2040 {
2041 if (!pReqHdr || u64Arg || idCpu != NIL_VMCPUID)
2042 return VERR_INVALID_PARAMETER;
2043 rc = PDMR0DriverCallReqHandler(pGVM, (PPDMDRIVERCALLREQHANDLERREQ)pReqHdr);
2044 break;
2045 }
2046
2047 case VMMR0_DO_PDM_DEVICE_CREATE:
2048 {
2049 if (!pReqHdr || u64Arg || idCpu != 0)
2050 return VERR_INVALID_PARAMETER;
2051 rc = PDMR0DeviceCreateReqHandler(pGVM, (PPDMDEVICECREATEREQ)pReqHdr);
2052 break;
2053 }
2054
2055 case VMMR0_DO_PDM_DEVICE_GEN_CALL:
2056 {
2057 if (!pReqHdr || u64Arg)
2058 return VERR_INVALID_PARAMETER;
2059 rc = PDMR0DeviceGenCallReqHandler(pGVM, (PPDMDEVICEGENCALLREQ)pReqHdr, idCpu);
2060 break;
2061 }
2062
2063 /** @todo Remove the once all devices has been converted to new style! @bugref{9218} */
2064 case VMMR0_DO_PDM_DEVICE_COMPAT_SET_CRITSECT:
2065 {
2066 if (!pReqHdr || u64Arg || idCpu != 0)
2067 return VERR_INVALID_PARAMETER;
2068 rc = PDMR0DeviceCompatSetCritSectReqHandler(pGVM, (PPDMDEVICECOMPATSETCRITSECTREQ)pReqHdr);
2069 break;
2070 }
2071
2072 /*
2073 * Requests to the internal networking service.
2074 */
2075 case VMMR0_DO_INTNET_OPEN:
2076 {
2077 PINTNETOPENREQ pReq = (PINTNETOPENREQ)pReqHdr;
2078 if (u64Arg || !pReq || !vmmR0IsValidSession(pGVM, pReq->pSession, pSession) || idCpu != NIL_VMCPUID)
2079 return VERR_INVALID_PARAMETER;
2080 rc = IntNetR0OpenReq(pSession, pReq);
2081 break;
2082 }
2083
2084 case VMMR0_DO_INTNET_IF_CLOSE:
2085 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pGVM, ((PINTNETIFCLOSEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
2086 return VERR_INVALID_PARAMETER;
2087 rc = IntNetR0IfCloseReq(pSession, (PINTNETIFCLOSEREQ)pReqHdr);
2088 break;
2089
2090
2091 case VMMR0_DO_INTNET_IF_GET_BUFFER_PTRS:
2092 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pGVM, ((PINTNETIFGETBUFFERPTRSREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
2093 return VERR_INVALID_PARAMETER;
2094 rc = IntNetR0IfGetBufferPtrsReq(pSession, (PINTNETIFGETBUFFERPTRSREQ)pReqHdr);
2095 break;
2096
2097 case VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE:
2098 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pGVM, ((PINTNETIFSETPROMISCUOUSMODEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
2099 return VERR_INVALID_PARAMETER;
2100 rc = IntNetR0IfSetPromiscuousModeReq(pSession, (PINTNETIFSETPROMISCUOUSMODEREQ)pReqHdr);
2101 break;
2102
2103 case VMMR0_DO_INTNET_IF_SET_MAC_ADDRESS:
2104 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pGVM, ((PINTNETIFSETMACADDRESSREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
2105 return VERR_INVALID_PARAMETER;
2106 rc = IntNetR0IfSetMacAddressReq(pSession, (PINTNETIFSETMACADDRESSREQ)pReqHdr);
2107 break;
2108
2109 case VMMR0_DO_INTNET_IF_SET_ACTIVE:
2110 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pGVM, ((PINTNETIFSETACTIVEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
2111 return VERR_INVALID_PARAMETER;
2112 rc = IntNetR0IfSetActiveReq(pSession, (PINTNETIFSETACTIVEREQ)pReqHdr);
2113 break;
2114
2115 case VMMR0_DO_INTNET_IF_SEND:
2116 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pGVM, ((PINTNETIFSENDREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
2117 return VERR_INVALID_PARAMETER;
2118 rc = IntNetR0IfSendReq(pSession, (PINTNETIFSENDREQ)pReqHdr);
2119 break;
2120
2121 case VMMR0_DO_INTNET_IF_WAIT:
2122 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pGVM, ((PINTNETIFWAITREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
2123 return VERR_INVALID_PARAMETER;
2124 rc = IntNetR0IfWaitReq(pSession, (PINTNETIFWAITREQ)pReqHdr);
2125 break;
2126
2127 case VMMR0_DO_INTNET_IF_ABORT_WAIT:
2128 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pGVM, ((PINTNETIFWAITREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
2129 return VERR_INVALID_PARAMETER;
2130 rc = IntNetR0IfAbortWaitReq(pSession, (PINTNETIFABORTWAITREQ)pReqHdr);
2131 break;
2132
2133#if 0 //def VBOX_WITH_PCI_PASSTHROUGH
2134 /*
2135 * Requests to host PCI driver service.
2136 */
2137 case VMMR0_DO_PCIRAW_REQ:
2138 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pGVM, ((PPCIRAWSENDREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
2139 return VERR_INVALID_PARAMETER;
2140 rc = PciRawR0ProcessReq(pGVM, pSession, (PPCIRAWSENDREQ)pReqHdr);
2141 break;
2142#endif
2143
2144 /*
2145 * NEM requests.
2146 */
2147#ifdef VBOX_WITH_NEM_R0
2148# if defined(RT_ARCH_AMD64) && defined(RT_OS_WINDOWS)
2149 case VMMR0_DO_NEM_INIT_VM:
2150 if (u64Arg || pReqHdr || idCpu != 0)
2151 return VERR_INVALID_PARAMETER;
2152 rc = NEMR0InitVM(pGVM);
2153 break;
2154
2155 case VMMR0_DO_NEM_INIT_VM_PART_2:
2156 if (u64Arg || pReqHdr || idCpu != 0)
2157 return VERR_INVALID_PARAMETER;
2158 rc = NEMR0InitVMPart2(pGVM);
2159 break;
2160
2161 case VMMR0_DO_NEM_MAP_PAGES:
2162 if (u64Arg || pReqHdr || idCpu == NIL_VMCPUID)
2163 return VERR_INVALID_PARAMETER;
2164 rc = NEMR0MapPages(pGVM, idCpu);
2165 break;
2166
2167 case VMMR0_DO_NEM_UNMAP_PAGES:
2168 if (u64Arg || pReqHdr || idCpu == NIL_VMCPUID)
2169 return VERR_INVALID_PARAMETER;
2170 rc = NEMR0UnmapPages(pGVM, idCpu);
2171 break;
2172
2173 case VMMR0_DO_NEM_EXPORT_STATE:
2174 if (u64Arg || pReqHdr || idCpu == NIL_VMCPUID)
2175 return VERR_INVALID_PARAMETER;
2176 rc = NEMR0ExportState(pGVM, idCpu);
2177 break;
2178
2179 case VMMR0_DO_NEM_IMPORT_STATE:
2180 if (pReqHdr || idCpu == NIL_VMCPUID)
2181 return VERR_INVALID_PARAMETER;
2182 rc = NEMR0ImportState(pGVM, idCpu, u64Arg);
2183 break;
2184
2185 case VMMR0_DO_NEM_QUERY_CPU_TICK:
2186 if (u64Arg || pReqHdr || idCpu == NIL_VMCPUID)
2187 return VERR_INVALID_PARAMETER;
2188 rc = NEMR0QueryCpuTick(pGVM, idCpu);
2189 break;
2190
2191 case VMMR0_DO_NEM_RESUME_CPU_TICK_ON_ALL:
2192 if (pReqHdr || idCpu == NIL_VMCPUID)
2193 return VERR_INVALID_PARAMETER;
2194 rc = NEMR0ResumeCpuTickOnAll(pGVM, idCpu, u64Arg);
2195 break;
2196
2197 case VMMR0_DO_NEM_UPDATE_STATISTICS:
2198 if (u64Arg || pReqHdr)
2199 return VERR_INVALID_PARAMETER;
2200 rc = NEMR0UpdateStatistics(pGVM, idCpu);
2201 break;
2202
2203# if 1 && defined(DEBUG_bird)
2204 case VMMR0_DO_NEM_EXPERIMENT:
2205 if (pReqHdr)
2206 return VERR_INVALID_PARAMETER;
2207 rc = NEMR0DoExperiment(pGVM, idCpu, u64Arg);
2208 break;
2209# endif
2210# endif
2211#endif
2212
2213 /*
2214 * IOM requests.
2215 */
2216 case VMMR0_DO_IOM_GROW_IO_PORTS:
2217 {
2218 if (pReqHdr || idCpu != 0)
2219 return VERR_INVALID_PARAMETER;
2220 rc = IOMR0IoPortGrowRegistrationTables(pGVM, u64Arg);
2221 break;
2222 }
2223
2224 case VMMR0_DO_IOM_GROW_IO_PORT_STATS:
2225 {
2226 if (pReqHdr || idCpu != 0)
2227 return VERR_INVALID_PARAMETER;
2228 rc = IOMR0IoPortGrowStatisticsTable(pGVM, u64Arg);
2229 break;
2230 }
2231
2232 case VMMR0_DO_IOM_GROW_MMIO_REGS:
2233 {
2234 if (pReqHdr || idCpu != 0)
2235 return VERR_INVALID_PARAMETER;
2236 rc = IOMR0MmioGrowRegistrationTables(pGVM, u64Arg);
2237 break;
2238 }
2239
2240 case VMMR0_DO_IOM_GROW_MMIO_STATS:
2241 {
2242 if (pReqHdr || idCpu != 0)
2243 return VERR_INVALID_PARAMETER;
2244 rc = IOMR0MmioGrowStatisticsTable(pGVM, u64Arg);
2245 break;
2246 }
2247
2248 case VMMR0_DO_IOM_SYNC_STATS_INDICES:
2249 {
2250 if (pReqHdr || idCpu != 0)
2251 return VERR_INVALID_PARAMETER;
2252 rc = IOMR0IoPortSyncStatisticsIndices(pGVM);
2253 if (RT_SUCCESS(rc))
2254 rc = IOMR0MmioSyncStatisticsIndices(pGVM);
2255 break;
2256 }
2257
2258 /*
2259 * DBGF requests.
2260 */
2261#ifdef VBOX_WITH_DBGF_TRACING
2262 case VMMR0_DO_DBGF_TRACER_CREATE:
2263 {
2264 if (!pReqHdr || u64Arg || idCpu != 0)
2265 return VERR_INVALID_PARAMETER;
2266 rc = DBGFR0TracerCreateReqHandler(pGVM, (PDBGFTRACERCREATEREQ)pReqHdr);
2267 break;
2268 }
2269
2270 case VMMR0_DO_DBGF_TRACER_CALL_REQ_HANDLER:
2271 {
2272 if (!pReqHdr || u64Arg)
2273 return VERR_INVALID_PARAMETER;
2274# if 0 /** @todo */
2275 rc = DBGFR0TracerGenCallReqHandler(pGVM, (PDBGFTRACERGENCALLREQ)pReqHdr, idCpu);
2276# else
2277 rc = VERR_NOT_IMPLEMENTED;
2278# endif
2279 break;
2280 }
2281#endif
2282
2283 case VMMR0_DO_DBGF_BP_INIT:
2284 {
2285 if (!pReqHdr || u64Arg || idCpu != 0)
2286 return VERR_INVALID_PARAMETER;
2287 rc = DBGFR0BpInitReqHandler(pGVM, (PDBGFBPINITREQ)pReqHdr);
2288 break;
2289 }
2290
2291 case VMMR0_DO_DBGF_BP_CHUNK_ALLOC:
2292 {
2293 if (!pReqHdr || u64Arg || idCpu != 0)
2294 return VERR_INVALID_PARAMETER;
2295 rc = DBGFR0BpChunkAllocReqHandler(pGVM, (PDBGFBPCHUNKALLOCREQ)pReqHdr);
2296 break;
2297 }
2298
2299 case VMMR0_DO_DBGF_BP_L2_TBL_CHUNK_ALLOC:
2300 {
2301 if (!pReqHdr || u64Arg || idCpu != 0)
2302 return VERR_INVALID_PARAMETER;
2303 rc = DBGFR0BpL2TblChunkAllocReqHandler(pGVM, (PDBGFBPL2TBLCHUNKALLOCREQ)pReqHdr);
2304 break;
2305 }
2306
2307 case VMMR0_DO_DBGF_BP_OWNER_INIT:
2308 {
2309 if (!pReqHdr || u64Arg || idCpu != 0)
2310 return VERR_INVALID_PARAMETER;
2311 rc = DBGFR0BpOwnerInitReqHandler(pGVM, (PDBGFBPOWNERINITREQ)pReqHdr);
2312 break;
2313 }
2314
2315 case VMMR0_DO_DBGF_BP_PORTIO_INIT:
2316 {
2317 if (!pReqHdr || u64Arg || idCpu != 0)
2318 return VERR_INVALID_PARAMETER;
2319 rc = DBGFR0BpPortIoInitReqHandler(pGVM, (PDBGFBPINITREQ)pReqHdr);
2320 break;
2321 }
2322
2323
2324 /*
2325 * TM requests.
2326 */
2327 case VMMR0_DO_TM_GROW_TIMER_QUEUE:
2328 {
2329 if (pReqHdr || idCpu == NIL_VMCPUID)
2330 return VERR_INVALID_PARAMETER;
2331 rc = TMR0TimerQueueGrow(pGVM, RT_HI_U32(u64Arg), RT_LO_U32(u64Arg));
2332 break;
2333 }
2334
2335 /*
2336 * For profiling.
2337 */
2338 case VMMR0_DO_NOP:
2339 case VMMR0_DO_SLOW_NOP:
2340 return VINF_SUCCESS;
2341
2342 /*
2343 * For testing Ring-0 APIs invoked in this environment.
2344 */
2345 case VMMR0_DO_TESTS:
2346 /** @todo make new test */
2347 return VINF_SUCCESS;
2348
2349 default:
2350 /*
2351 * We're returning VERR_NOT_SUPPORT here so we've got something else
2352 * than -1 which the interrupt gate glue code might return.
2353 */
2354 Log(("operation %#x is not supported\n", enmOperation));
2355 return VERR_NOT_SUPPORTED;
2356 }
2357 return rc;
2358}
2359
2360
2361/**
2362 * This is just a longjmp wrapper function for VMMR0EntryEx calls.
2363 *
2364 * @returns VBox status code.
2365 * @param pvArgs The argument package
2366 */
2367static DECLCALLBACK(int) vmmR0EntryExWrapper(void *pvArgs)
2368{
2369 PGVMCPU pGVCpu = (PGVMCPU)pvArgs;
2370 return vmmR0EntryExWorker(pGVCpu->vmmr0.s.pGVM,
2371 pGVCpu->vmmr0.s.idCpu,
2372 pGVCpu->vmmr0.s.enmOperation,
2373 pGVCpu->vmmr0.s.pReq,
2374 pGVCpu->vmmr0.s.u64Arg,
2375 pGVCpu->vmmr0.s.pSession);
2376}
2377
2378
2379/**
2380 * The Ring 0 entry point, called by the support library (SUP).
2381 *
2382 * @returns VBox status code.
2383 * @param pGVM The global (ring-0) VM structure.
2384 * @param pVM The cross context VM structure.
2385 * @param idCpu Virtual CPU ID argument. Must be NIL_VMCPUID if pVM
2386 * is NIL_RTR0PTR, and may be NIL_VMCPUID if it isn't
2387 * @param enmOperation Which operation to execute.
2388 * @param pReq Pointer to the SUPVMMR0REQHDR packet. Optional.
2389 * @param u64Arg Some simple constant argument.
2390 * @param pSession The session of the caller.
2391 * @remarks Assume called with interrupts _enabled_.
2392 */
2393VMMR0DECL(int) VMMR0EntryEx(PGVM pGVM, PVMCC pVM, VMCPUID idCpu, VMMR0OPERATION enmOperation,
2394 PSUPVMMR0REQHDR pReq, uint64_t u64Arg, PSUPDRVSESSION pSession)
2395{
2396 /*
2397 * Requests that should only happen on the EMT thread will be
2398 * wrapped in a setjmp so we can assert without causing too much trouble.
2399 */
2400 if ( pVM != NULL
2401 && pGVM != NULL
2402 && pVM == pGVM /** @todo drop pVM or pGVM */
2403 && idCpu < pGVM->cCpus
2404 && pGVM->pSession == pSession
2405 && pGVM->pSelf == pGVM
2406 && enmOperation != VMMR0_DO_GVMM_DESTROY_VM
2407 && enmOperation != VMMR0_DO_GVMM_REGISTER_VMCPU
2408 && enmOperation != VMMR0_DO_GVMM_SCHED_WAKE_UP /* idCpu is not caller but target. Sigh. */ /** @todo fix*/
2409 && enmOperation != VMMR0_DO_GVMM_SCHED_POKE /* idCpu is not caller but target. Sigh. */ /** @todo fix*/
2410 )
2411 {
2412 PGVMCPU pGVCpu = &pGVM->aCpus[idCpu];
2413 RTNATIVETHREAD hNativeThread = RTThreadNativeSelf();
2414 if (RT_LIKELY( pGVCpu->hEMT == hNativeThread
2415 && pGVCpu->hNativeThreadR0 == hNativeThread))
2416 {
2417 pGVCpu->vmmr0.s.pGVM = pGVM;
2418 pGVCpu->vmmr0.s.idCpu = idCpu;
2419 pGVCpu->vmmr0.s.enmOperation = enmOperation;
2420 pGVCpu->vmmr0.s.pReq = pReq;
2421 pGVCpu->vmmr0.s.u64Arg = u64Arg;
2422 pGVCpu->vmmr0.s.pSession = pSession;
2423 return vmmR0CallRing3SetJmpEx(&pGVCpu->vmmr0.s.AssertJmpBuf, vmmR0EntryExWrapper, pGVCpu,
2424 ((uintptr_t)u64Arg << 16) | (uintptr_t)enmOperation);
2425 }
2426 return VERR_VM_THREAD_NOT_EMT;
2427 }
2428 return vmmR0EntryExWorker(pGVM, idCpu, enmOperation, pReq, u64Arg, pSession);
2429}
2430
2431
2432/*********************************************************************************************************************************
2433* EMT Blocking *
2434*********************************************************************************************************************************/
2435
2436/**
2437 * Checks whether we've armed the ring-0 long jump machinery.
2438 *
2439 * @returns @c true / @c false
2440 * @param pVCpu The cross context virtual CPU structure.
2441 * @thread EMT
2442 * @sa VMMIsLongJumpArmed
2443 */
2444VMMR0_INT_DECL(bool) VMMR0IsLongJumpArmed(PVMCPUCC pVCpu)
2445{
2446#ifdef RT_ARCH_X86
2447 return pVCpu->vmmr0.s.AssertJmpBuf.eip != 0;
2448#else
2449 return pVCpu->vmmr0.s.AssertJmpBuf.rip != 0;
2450#endif
2451}
2452
2453
2454/**
2455 * Locking helper that deals with HM context and checks if the thread can block.
2456 *
2457 * @returns VINF_SUCCESS if we can block. Returns @a rcBusy or
2458 * VERR_VMM_CANNOT_BLOCK if not able to block.
2459 * @param pVCpu The cross context virtual CPU structure of the calling
2460 * thread.
2461 * @param rcBusy What to return in case of a blocking problem. Will IPE
2462 * if VINF_SUCCESS and we cannot block.
2463 * @param pszCaller The caller (for logging problems).
2464 * @param pvLock The lock address (for logging problems).
2465 * @param pCtx Where to return context info for the resume call.
2466 * @thread EMT(pVCpu)
2467 */
2468VMMR0_INT_DECL(int) VMMR0EmtPrepareToBlock(PVMCPUCC pVCpu, int rcBusy, const char *pszCaller, void *pvLock,
2469 PVMMR0EMTBLOCKCTX pCtx)
2470{
2471 const char *pszMsg;
2472
2473 /*
2474 * Check that we are allowed to block.
2475 */
2476 if (RT_LIKELY(VMMRZCallRing3IsEnabled(pVCpu)))
2477 {
2478 /*
2479 * Are we in HM context and w/o a context hook? If so work the context hook.
2480 */
2481 if (pVCpu->idHostCpu != NIL_RTCPUID)
2482 {
2483 Assert(pVCpu->iHostCpuSet != UINT32_MAX);
2484
2485 if (pVCpu->vmmr0.s.hCtxHook == NIL_RTTHREADCTXHOOK)
2486 {
2487 vmmR0ThreadCtxCallback(RTTHREADCTXEVENT_OUT, pVCpu);
2488 if (pVCpu->vmmr0.s.pPreemptState)
2489 RTThreadPreemptRestore(pVCpu->vmmr0.s.pPreemptState);
2490
2491 pCtx->uMagic = VMMR0EMTBLOCKCTX_MAGIC;
2492 pCtx->fWasInHmContext = true;
2493 return VINF_SUCCESS;
2494 }
2495 }
2496
2497 if (RT_LIKELY(!pVCpu->vmmr0.s.pPreemptState))
2498 {
2499 /*
2500 * Not in HM context or we've got hooks, so just check that preemption
2501 * is enabled.
2502 */
2503 if (RT_LIKELY(RTThreadPreemptIsEnabled(NIL_RTTHREAD)))
2504 {
2505 pCtx->uMagic = VMMR0EMTBLOCKCTX_MAGIC;
2506 pCtx->fWasInHmContext = false;
2507 return VINF_SUCCESS;
2508 }
2509 pszMsg = "Preemption is disabled!";
2510 }
2511 else
2512 pszMsg = "Preemption state w/o HM state!";
2513 }
2514 else
2515 pszMsg = "Ring-3 calls are disabled!";
2516
2517 static uint32_t volatile s_cWarnings = 0;
2518 if (++s_cWarnings < 50)
2519 SUPR0Printf("VMMR0EmtPrepareToBlock: %s pvLock=%p pszCaller=%s rcBusy=%p\n", pszMsg, pvLock, pszCaller, rcBusy);
2520 pCtx->uMagic = VMMR0EMTBLOCKCTX_MAGIC_DEAD;
2521 pCtx->fWasInHmContext = false;
2522 return rcBusy != VINF_SUCCESS ? rcBusy : VERR_VMM_CANNOT_BLOCK;
2523}
2524
2525
2526/**
2527 * Counterpart to VMMR0EmtPrepareToBlock.
2528 *
2529 * @param pVCpu The cross context virtual CPU structure of the calling
2530 * thread.
2531 * @param pCtx The context structure used with VMMR0EmtPrepareToBlock.
2532 * @thread EMT(pVCpu)
2533 */
2534VMMR0_INT_DECL(void) VMMR0EmtResumeAfterBlocking(PVMCPUCC pVCpu, PVMMR0EMTBLOCKCTX pCtx)
2535{
2536 AssertReturnVoid(pCtx->uMagic == VMMR0EMTBLOCKCTX_MAGIC);
2537 if (pCtx->fWasInHmContext)
2538 {
2539 if (pVCpu->vmmr0.s.pPreemptState)
2540 RTThreadPreemptDisable(pVCpu->vmmr0.s.pPreemptState);
2541
2542 pCtx->fWasInHmContext = false;
2543 vmmR0ThreadCtxCallback(RTTHREADCTXEVENT_IN, pVCpu);
2544 }
2545 pCtx->uMagic = VMMR0EMTBLOCKCTX_MAGIC_DEAD;
2546}
2547
2548
2549/**
2550 * Helper for waiting on an RTSEMEVENT, caller did VMMR0EmtPrepareToBlock.
2551 *
2552 * @returns
2553 * @retval VERR_THREAD_IS_TERMINATING
2554 * @retval VERR_TIMEOUT if we ended up waiting too long, either according to
2555 * @a cMsTimeout or to maximum wait values.
2556 *
2557 * @param pGVCpu The ring-0 virtual CPU structure.
2558 * @param fFlags VMMR0EMTWAIT_F_XXX.
2559 * @param hEvent The event to wait on.
2560 * @param cMsTimeout The timeout or RT_INDEFINITE_WAIT.
2561 */
2562VMMR0_INT_DECL(int) VMMR0EmtWaitEventInner(PGVMCPU pGVCpu, uint32_t fFlags, RTSEMEVENT hEvent, RTMSINTERVAL cMsTimeout)
2563{
2564 AssertReturn(pGVCpu->hEMT == RTThreadNativeSelf(), VERR_VM_THREAD_NOT_EMT);
2565
2566 /*
2567 * Note! Similar code is found in the PDM critical sections too.
2568 */
2569 uint64_t const nsStart = RTTimeNanoTS();
2570 uint64_t cNsMaxTotal = cMsTimeout == RT_INDEFINITE_WAIT
2571 ? RT_NS_5MIN : RT_MIN(RT_NS_5MIN, RT_NS_1MS_64 * cMsTimeout);
2572 uint32_t cMsMaxOne = RT_MS_5SEC;
2573 bool fNonInterruptible = false;
2574 for (;;)
2575 {
2576 /* Wait. */
2577 int rcWait = !fNonInterruptible
2578 ? RTSemEventWaitNoResume(hEvent, cMsMaxOne)
2579 : RTSemEventWait(hEvent, cMsMaxOne);
2580 if (RT_SUCCESS(rcWait))
2581 return rcWait;
2582
2583 if (rcWait == VERR_TIMEOUT || rcWait == VERR_INTERRUPTED)
2584 {
2585 uint64_t const cNsElapsed = RTTimeNanoTS() - nsStart;
2586
2587 /*
2588 * Check the thread termination status.
2589 */
2590 int const rcTerm = RTThreadQueryTerminationStatus(NIL_RTTHREAD);
2591 AssertMsg(rcTerm == VINF_SUCCESS || rcTerm == VERR_NOT_SUPPORTED || rcTerm == VINF_THREAD_IS_TERMINATING,
2592 ("rcTerm=%Rrc\n", rcTerm));
2593 if ( rcTerm == VERR_NOT_SUPPORTED
2594 && !fNonInterruptible
2595 && cNsMaxTotal > RT_NS_1MIN)
2596 cNsMaxTotal = RT_NS_1MIN;
2597
2598 /* We return immediately if it looks like the thread is terminating. */
2599 if (rcTerm == VINF_THREAD_IS_TERMINATING)
2600 return VERR_THREAD_IS_TERMINATING;
2601
2602 /* We may suppress VERR_INTERRUPTED if VMMR0EMTWAIT_F_TRY_SUPPRESS_INTERRUPTED was
2603 specified, otherwise we'll just return it. */
2604 if (rcWait == VERR_INTERRUPTED)
2605 {
2606 if (!(fFlags & VMMR0EMTWAIT_F_TRY_SUPPRESS_INTERRUPTED))
2607 return VERR_INTERRUPTED;
2608 if (!fNonInterruptible)
2609 {
2610 /* First time: Adjust down the wait parameters and make sure we get at least
2611 one non-interruptible wait before timing out. */
2612 fNonInterruptible = true;
2613 cMsMaxOne = 32;
2614 uint64_t const cNsLeft = cNsMaxTotal - cNsElapsed;
2615 if (cNsLeft > RT_NS_10SEC)
2616 cNsMaxTotal = cNsElapsed + RT_NS_10SEC;
2617 continue;
2618 }
2619 }
2620
2621 /* Check for timeout. */
2622 if (cNsElapsed > cNsMaxTotal)
2623 return VERR_TIMEOUT;
2624 }
2625 else
2626 return rcWait;
2627 }
2628 /* not reached */
2629}
2630
2631
2632/**
2633 * Helper for signalling an SUPSEMEVENT.
2634 *
2635 * This may temporarily leave the HM context if the host requires that for
2636 * signalling SUPSEMEVENT objects.
2637 *
2638 * @returns VBox status code (see VMMR0EmtPrepareToBlock)
2639 * @param pGVM The ring-0 VM structure.
2640 * @param pGVCpu The ring-0 virtual CPU structure.
2641 * @param hEvent The event to signal.
2642 */
2643VMMR0_INT_DECL(int) VMMR0EmtSignalSupEvent(PGVM pGVM, PGVMCPU pGVCpu, SUPSEMEVENT hEvent)
2644{
2645 AssertReturn(pGVCpu->hEMT == RTThreadNativeSelf(), VERR_VM_THREAD_NOT_EMT);
2646 if (RTSemEventIsSignalSafe())
2647 return SUPSemEventSignal(pGVM->pSession, hEvent);
2648
2649 VMMR0EMTBLOCKCTX Ctx;
2650 int rc = VMMR0EmtPrepareToBlock(pGVCpu, VINF_SUCCESS, __FUNCTION__, (void *)(uintptr_t)hEvent, &Ctx);
2651 if (RT_SUCCESS(rc))
2652 {
2653 rc = SUPSemEventSignal(pGVM->pSession, hEvent);
2654 VMMR0EmtResumeAfterBlocking(pGVCpu, &Ctx);
2655 }
2656 return rc;
2657}
2658
2659
2660/**
2661 * Helper for signalling an SUPSEMEVENT, variant supporting non-EMTs.
2662 *
2663 * This may temporarily leave the HM context if the host requires that for
2664 * signalling SUPSEMEVENT objects.
2665 *
2666 * @returns VBox status code (see VMMR0EmtPrepareToBlock)
2667 * @param pGVM The ring-0 VM structure.
2668 * @param hEvent The event to signal.
2669 */
2670VMMR0_INT_DECL(int) VMMR0EmtSignalSupEventByGVM(PGVM pGVM, SUPSEMEVENT hEvent)
2671{
2672 if (!RTSemEventIsSignalSafe())
2673 {
2674 PGVMCPU pGVCpu = GVMMR0GetGVCpuByGVMandEMT(pGVM, NIL_RTNATIVETHREAD);
2675 if (pGVCpu)
2676 {
2677 VMMR0EMTBLOCKCTX Ctx;
2678 int rc = VMMR0EmtPrepareToBlock(pGVCpu, VINF_SUCCESS, __FUNCTION__, (void *)(uintptr_t)hEvent, &Ctx);
2679 if (RT_SUCCESS(rc))
2680 {
2681 rc = SUPSemEventSignal(pGVM->pSession, hEvent);
2682 VMMR0EmtResumeAfterBlocking(pGVCpu, &Ctx);
2683 }
2684 return rc;
2685 }
2686 }
2687 return SUPSemEventSignal(pGVM->pSession, hEvent);
2688}
2689
2690
2691/*********************************************************************************************************************************
2692* Logging. *
2693*********************************************************************************************************************************/
2694
2695/**
2696 * VMMR0_DO_VMMR0_UPDATE_LOGGERS: Updates the EMT loggers for the VM.
2697 *
2698 * @returns VBox status code.
2699 * @param pGVM The global (ring-0) VM structure.
2700 * @param idCpu The ID of the calling EMT.
2701 * @param pReq The request data.
2702 * @param idxLogger Which logger set to update.
2703 * @thread EMT(idCpu)
2704 */
2705static int vmmR0UpdateLoggers(PGVM pGVM, VMCPUID idCpu, PVMMR0UPDATELOGGERSREQ pReq, size_t idxLogger)
2706{
2707 /*
2708 * Check sanity. First we require EMT to be calling us.
2709 */
2710 AssertReturn(idCpu < pGVM->cCpus, VERR_INVALID_CPU_ID);
2711 AssertReturn(pGVM->aCpus[idCpu].hEMT == RTThreadNativeSelf(), VERR_INVALID_CPU_ID);
2712
2713 AssertReturn(pReq->Hdr.cbReq >= RT_UOFFSETOF_DYN(VMMR0UPDATELOGGERSREQ, afGroups[0]), VERR_INVALID_PARAMETER);
2714 AssertReturn(pReq->cGroups < _8K, VERR_INVALID_PARAMETER);
2715 AssertReturn(pReq->Hdr.cbReq == RT_UOFFSETOF_DYN(VMMR0UPDATELOGGERSREQ, afGroups[pReq->cGroups]), VERR_INVALID_PARAMETER);
2716
2717 AssertReturn(idxLogger < VMMLOGGER_IDX_MAX, VERR_OUT_OF_RANGE);
2718
2719 /*
2720 * Adjust flags.
2721 */
2722 /* Always buffered: */
2723 pReq->fFlags |= RTLOGFLAGS_BUFFERED;
2724 /* These doesn't make sense at present: */
2725 pReq->fFlags &= ~(RTLOGFLAGS_FLUSH | RTLOGFLAGS_WRITE_THROUGH);
2726 /* We've traditionally skipped the group restrictions. */
2727 pReq->fFlags &= ~RTLOGFLAGS_RESTRICT_GROUPS;
2728
2729 /*
2730 * Do the updating.
2731 */
2732 int rc = VINF_SUCCESS;
2733 for (idCpu = 0; idCpu < pGVM->cCpus; idCpu++)
2734 {
2735 PGVMCPU pGVCpu = &pGVM->aCpus[idCpu];
2736 PRTLOGGER pLogger = pGVCpu->vmmr0.s.u.aLoggers[idxLogger].pLogger;
2737 if (pLogger)
2738 {
2739 RTLogSetR0ProgramStart(pLogger, pGVM->vmm.s.nsProgramStart);
2740 rc = RTLogBulkUpdate(pLogger, pReq->fFlags, pReq->uGroupCrc32, pReq->cGroups, pReq->afGroups);
2741 }
2742 }
2743
2744 return rc;
2745}
2746
2747
2748/**
2749 * VMMR0_DO_VMMR0_LOG_FLUSHER: Get the next log flushing job.
2750 *
2751 * The job info is copied into VMM::LogFlusherItem.
2752 *
2753 * @returns VBox status code.
2754 * @retval VERR_OBJECT_DESTROYED if we're shutting down.
2755 * @retval VERR_NOT_OWNER if the calling thread is not the flusher thread.
2756 * @param pGVM The global (ring-0) VM structure.
2757 * @thread The log flusher thread (first caller automatically becomes the log
2758 * flusher).
2759 */
2760static int vmmR0LogFlusher(PGVM pGVM)
2761{
2762 /*
2763 * Check that this really is the flusher thread.
2764 */
2765 RTNATIVETHREAD const hNativeSelf = RTThreadNativeSelf();
2766 AssertReturn(hNativeSelf != NIL_RTNATIVETHREAD, VERR_INTERNAL_ERROR_3);
2767 if (RT_LIKELY(pGVM->vmmr0.s.LogFlusher.hThread == hNativeSelf))
2768 { /* likely */ }
2769 else
2770 {
2771 /* The first caller becomes the flusher thread. */
2772 bool fOk;
2773 ASMAtomicCmpXchgHandle(&pGVM->vmmr0.s.LogFlusher.hThread, hNativeSelf, NIL_RTNATIVETHREAD, fOk);
2774 if (!fOk)
2775 return VERR_NOT_OWNER;
2776 pGVM->vmmr0.s.LogFlusher.fThreadRunning = true;
2777 }
2778
2779 /*
2780 * Acknowledge flush, waking up waiting EMT.
2781 */
2782 RTSpinlockAcquire(pGVM->vmmr0.s.LogFlusher.hSpinlock);
2783
2784 uint32_t idxTail = pGVM->vmmr0.s.LogFlusher.idxRingTail % RT_ELEMENTS(pGVM->vmmr0.s.LogFlusher.aRing);
2785 uint32_t idxHead = pGVM->vmmr0.s.LogFlusher.idxRingHead % RT_ELEMENTS(pGVM->vmmr0.s.LogFlusher.aRing);
2786 if ( idxTail != idxHead
2787 && pGVM->vmmr0.s.LogFlusher.aRing[idxHead].s.fProcessing)
2788 {
2789 /* Pop the head off the ring buffer. */
2790 uint32_t const idCpu = pGVM->vmmr0.s.LogFlusher.aRing[idxHead].s.idCpu;
2791 uint32_t const idxLogger = pGVM->vmmr0.s.LogFlusher.aRing[idxHead].s.idxLogger;
2792 uint32_t const idxBuffer = pGVM->vmmr0.s.LogFlusher.aRing[idxHead].s.idxBuffer;
2793
2794 pGVM->vmmr0.s.LogFlusher.aRing[idxHead].u32 = UINT32_MAX >> 1; /* invalidate the entry */
2795 pGVM->vmmr0.s.LogFlusher.idxRingHead = (idxHead + 1) % RT_ELEMENTS(pGVM->vmmr0.s.LogFlusher.aRing);
2796
2797 /* Validate content. */
2798 if ( idCpu < pGVM->cCpus
2799 && idxLogger < VMMLOGGER_IDX_MAX
2800 && idxBuffer < VMMLOGGER_BUFFER_COUNT)
2801 {
2802 PGVMCPU pGVCpu = &pGVM->aCpus[idCpu];
2803 PVMMR0PERVCPULOGGER pR0Log = &pGVCpu->vmmr0.s.u.aLoggers[idxLogger];
2804 PVMMR3CPULOGGER pShared = &pGVCpu->vmm.s.u.aLoggers[idxLogger];
2805
2806 /*
2807 * Accounting.
2808 */
2809 uint32_t cFlushing = pR0Log->cFlushing - 1;
2810 if (RT_LIKELY(cFlushing < VMMLOGGER_BUFFER_COUNT))
2811 { /*likely*/ }
2812 else
2813 cFlushing = 0;
2814 pR0Log->cFlushing = cFlushing;
2815 ASMAtomicWriteU32(&pShared->cFlushing, cFlushing);
2816
2817 /*
2818 * Wake up the EMT if it's waiting.
2819 */
2820 if (!pR0Log->fEmtWaiting)
2821 RTSpinlockRelease(pGVM->vmmr0.s.LogFlusher.hSpinlock);
2822 else
2823 {
2824 pR0Log->fEmtWaiting = false;
2825 RTSpinlockRelease(pGVM->vmmr0.s.LogFlusher.hSpinlock);
2826
2827 int rc = RTSemEventSignal(pR0Log->hEventFlushWait);
2828 if (RT_FAILURE(rc))
2829 LogRelMax(64, ("vmmR0LogFlusher: RTSemEventSignal failed ACKing entry #%u (%u/%u/%u): %Rrc!\n",
2830 idxHead, idCpu, idxLogger, idxBuffer, rc));
2831 }
2832 }
2833 else
2834 {
2835 RTSpinlockRelease(pGVM->vmmr0.s.LogFlusher.hSpinlock);
2836 LogRelMax(64, ("vmmR0LogFlusher: Bad ACK entry #%u: %u/%u/%u!\n", idxHead, idCpu, idxLogger, idxBuffer));
2837 }
2838
2839 RTSpinlockAcquire(pGVM->vmmr0.s.LogFlusher.hSpinlock);
2840 }
2841
2842 /*
2843 * The wait loop.
2844 */
2845 int rc;
2846 for (;;)
2847 {
2848 /*
2849 * Work pending?
2850 */
2851 idxTail = pGVM->vmmr0.s.LogFlusher.idxRingTail % RT_ELEMENTS(pGVM->vmmr0.s.LogFlusher.aRing);
2852 idxHead = pGVM->vmmr0.s.LogFlusher.idxRingHead % RT_ELEMENTS(pGVM->vmmr0.s.LogFlusher.aRing);
2853 if (idxTail != idxHead)
2854 {
2855 pGVM->vmmr0.s.LogFlusher.aRing[idxHead].s.fProcessing = true;
2856 pGVM->vmm.s.LogFlusherItem.u32 = pGVM->vmmr0.s.LogFlusher.aRing[idxHead].u32;
2857
2858 RTSpinlockRelease(pGVM->vmmr0.s.LogFlusher.hSpinlock);
2859 return VINF_SUCCESS;
2860 }
2861
2862 /*
2863 * Nothing to do, so, check for termination and go to sleep.
2864 */
2865 if (!pGVM->vmmr0.s.LogFlusher.fThreadShutdown)
2866 { /* likely */ }
2867 else
2868 {
2869 rc = VERR_OBJECT_DESTROYED;
2870 break;
2871 }
2872
2873 pGVM->vmmr0.s.LogFlusher.fThreadWaiting = true;
2874 RTSpinlockRelease(pGVM->vmmr0.s.LogFlusher.hSpinlock);
2875
2876 rc = RTSemEventWaitNoResume(pGVM->vmmr0.s.LogFlusher.hEvent, RT_MS_5MIN);
2877
2878 RTSpinlockAcquire(pGVM->vmmr0.s.LogFlusher.hSpinlock);
2879 pGVM->vmmr0.s.LogFlusher.fThreadWaiting = false;
2880
2881 if (RT_SUCCESS(rc) || rc == VERR_TIMEOUT)
2882 { /* likely */ }
2883 else if (rc == VERR_INTERRUPTED)
2884 {
2885 RTSpinlockRelease(pGVM->vmmr0.s.LogFlusher.hSpinlock);
2886 return rc;
2887 }
2888 else if (rc == VERR_SEM_DESTROYED || rc == VERR_INVALID_HANDLE)
2889 break;
2890 else
2891 {
2892 LogRel(("vmmR0LogFlusher: RTSemEventWaitNoResume returned unexpected status %Rrc\n", rc));
2893 break;
2894 }
2895 }
2896
2897 /*
2898 * Terminating - prevent further calls and indicate to the EMTs that we're no longer around.
2899 */
2900 pGVM->vmmr0.s.LogFlusher.hThread = ~pGVM->vmmr0.s.LogFlusher.hThread; /* (should be reasonably safe) */
2901 pGVM->vmmr0.s.LogFlusher.fThreadRunning = false;
2902
2903 RTSpinlockRelease(pGVM->vmmr0.s.LogFlusher.hSpinlock);
2904 return rc;
2905}
2906
2907
2908/**
2909 * VMMR0_DO_VMMR0_LOG_WAIT_FLUSHED: Waits for the flusher thread to finish all
2910 * buffers for logger @a idxLogger.
2911 *
2912 * @returns VBox status code.
2913 * @param pGVM The global (ring-0) VM structure.
2914 * @param idCpu The ID of the calling EMT.
2915 * @param idxLogger Which logger to wait on.
2916 * @thread EMT(idCpu)
2917 */
2918static int vmmR0LogWaitFlushed(PGVM pGVM, VMCPUID idCpu, size_t idxLogger)
2919{
2920 /*
2921 * Check sanity. First we require EMT to be calling us.
2922 */
2923 AssertReturn(idCpu < pGVM->cCpus, VERR_INVALID_CPU_ID);
2924 PGVMCPU pGVCpu = &pGVM->aCpus[idCpu];
2925 AssertReturn(pGVCpu->hEMT == RTThreadNativeSelf(), VERR_INVALID_CPU_ID);
2926 AssertReturn(idxLogger < VMMLOGGER_IDX_MAX, VERR_OUT_OF_RANGE);
2927 PVMMR0PERVCPULOGGER const pR0Log = &pGVCpu->vmmr0.s.u.aLoggers[idxLogger];
2928
2929 /*
2930 * Do the waiting.
2931 */
2932 int rc = VINF_SUCCESS;
2933 RTSpinlockAcquire(pGVM->vmmr0.s.LogFlusher.hSpinlock);
2934 uint32_t cFlushing = pR0Log->cFlushing;
2935 while (cFlushing > 0)
2936 {
2937 pR0Log->fEmtWaiting = true;
2938 RTSpinlockRelease(pGVM->vmmr0.s.LogFlusher.hSpinlock);
2939
2940 rc = RTSemEventWaitNoResume(pR0Log->hEventFlushWait, RT_MS_5MIN);
2941
2942 RTSpinlockAcquire(pGVM->vmmr0.s.LogFlusher.hSpinlock);
2943 pR0Log->fEmtWaiting = false;
2944 if (RT_SUCCESS(rc))
2945 {
2946 /* Read the new count, make sure it decreased before looping. That
2947 way we can guarentee that we will only wait more than 5 min * buffers. */
2948 uint32_t const cPrevFlushing = cFlushing;
2949 cFlushing = pR0Log->cFlushing;
2950 if (cFlushing < cPrevFlushing)
2951 continue;
2952 rc = VERR_INTERNAL_ERROR_3;
2953 }
2954 break;
2955 }
2956 RTSpinlockRelease(pGVM->vmmr0.s.LogFlusher.hSpinlock);
2957 return rc;
2958}
2959
2960
2961/**
2962 * Inner worker for vmmR0LoggerFlushCommon.
2963 */
2964static bool vmmR0LoggerFlushInner(PGVM pGVM, PGVMCPU pGVCpu, uint32_t idxLogger, size_t idxBuffer, uint32_t cbToFlush)
2965{
2966 PVMMR0PERVCPULOGGER const pR0Log = &pGVCpu->vmmr0.s.u.aLoggers[idxLogger];
2967 PVMMR3CPULOGGER const pShared = &pGVCpu->vmm.s.u.aLoggers[idxLogger];
2968
2969 /*
2970 * Figure out what we need to do and whether we can.
2971 */
2972 enum { kJustSignal, kPrepAndSignal, kPrepSignalAndWait } enmAction;
2973#if VMMLOGGER_BUFFER_COUNT >= 2
2974 if (pR0Log->cFlushing < VMMLOGGER_BUFFER_COUNT - 1)
2975 {
2976 if (RTSemEventIsSignalSafe())
2977 enmAction = kJustSignal;
2978 else if (VMMRZCallRing3IsEnabled(pGVCpu))
2979 enmAction = kPrepAndSignal;
2980 else
2981 {
2982 /** @todo This is a bit simplistic. We could introduce a FF to signal the
2983 * thread or similar. */
2984 STAM_REL_COUNTER_INC(&pShared->StatCannotBlock);
2985# if defined(RT_OS_LINUX)
2986 SUP_DPRINTF(("vmmR0LoggerFlush: Signalling not safe and EMT blocking disabled! (%u bytes)\n", cbToFlush));
2987# endif
2988 pShared->cbDropped += cbToFlush;
2989 return true;
2990 }
2991 }
2992 else
2993#endif
2994 if (VMMRZCallRing3IsEnabled(pGVCpu))
2995 enmAction = kPrepSignalAndWait;
2996 else
2997 {
2998 STAM_REL_COUNTER_INC(&pShared->StatCannotBlock);
2999# if defined(RT_OS_LINUX)
3000 SUP_DPRINTF(("vmmR0LoggerFlush: EMT blocking disabled! (%u bytes)\n", cbToFlush));
3001# endif
3002 pShared->cbDropped += cbToFlush;
3003 return true;
3004 }
3005
3006 /*
3007 * Prepare for blocking if necessary.
3008 */
3009 VMMR0EMTBLOCKCTX Ctx;
3010 if (enmAction != kJustSignal)
3011 {
3012 int rc = VMMR0EmtPrepareToBlock(pGVCpu, VINF_SUCCESS, "vmmR0LoggerFlushInner", pR0Log->hEventFlushWait, &Ctx);
3013 if (RT_SUCCESS(rc))
3014 { /* likely */ }
3015 else
3016 {
3017 STAM_REL_COUNTER_INC(&pShared->StatCannotBlock);
3018 SUP_DPRINTF(("vmmR0LoggerFlush: VMMR0EmtPrepareToBlock failed! rc=%d\n", rc));
3019 return false;
3020 }
3021 }
3022
3023 /*
3024 * Queue the flush job.
3025 */
3026 bool fFlushedBuffer;
3027 RTSpinlockAcquire(pGVM->vmmr0.s.LogFlusher.hSpinlock);
3028 if (pGVM->vmmr0.s.LogFlusher.fThreadRunning)
3029 {
3030 uint32_t const idxHead = pGVM->vmmr0.s.LogFlusher.idxRingHead % RT_ELEMENTS(pGVM->vmmr0.s.LogFlusher.aRing);
3031 uint32_t const idxTail = pGVM->vmmr0.s.LogFlusher.idxRingTail % RT_ELEMENTS(pGVM->vmmr0.s.LogFlusher.aRing);
3032 uint32_t const idxNewTail = (idxTail + 1) % RT_ELEMENTS(pGVM->vmmr0.s.LogFlusher.aRing);
3033 if (idxNewTail != idxHead)
3034 {
3035 /* Queue it. */
3036 pGVM->vmmr0.s.LogFlusher.aRing[idxTail].s.idCpu = pGVCpu->idCpu;
3037 pGVM->vmmr0.s.LogFlusher.aRing[idxTail].s.idxLogger = idxLogger;
3038 pGVM->vmmr0.s.LogFlusher.aRing[idxTail].s.idxBuffer = (uint32_t)idxBuffer;
3039 pGVM->vmmr0.s.LogFlusher.aRing[idxTail].s.fProcessing = 0;
3040 pGVM->vmmr0.s.LogFlusher.idxRingTail = idxNewTail;
3041
3042 /* Update the number of buffers currently being flushed. */
3043 uint32_t cFlushing = pR0Log->cFlushing;
3044 cFlushing = RT_MIN(cFlushing + 1, VMMLOGGER_BUFFER_COUNT);
3045 pShared->cFlushing = pR0Log->cFlushing = cFlushing;
3046
3047 /* We must wait if all buffers are currently being flushed. */
3048 bool const fEmtWaiting = cFlushing >= VMMLOGGER_BUFFER_COUNT && enmAction != kJustSignal /* paranoia */;
3049 pR0Log->fEmtWaiting = fEmtWaiting;
3050
3051 /* Stats. */
3052 STAM_REL_COUNTER_INC(&pShared->StatFlushes);
3053 STAM_REL_COUNTER_INC(&pGVM->vmm.s.StatLogFlusherFlushes);
3054
3055 /* Signal the worker thread. */
3056 if (pGVM->vmmr0.s.LogFlusher.fThreadWaiting)
3057 {
3058 RTSpinlockRelease(pGVM->vmmr0.s.LogFlusher.hSpinlock);
3059 RTSemEventSignal(pGVM->vmmr0.s.LogFlusher.hEvent);
3060 }
3061 else
3062 {
3063 STAM_REL_COUNTER_INC(&pGVM->vmm.s.StatLogFlusherNoWakeUp);
3064 RTSpinlockRelease(pGVM->vmmr0.s.LogFlusher.hSpinlock);
3065 }
3066
3067 /*
3068 * Wait for a buffer to finish flushing.
3069 *
3070 * Note! Lazy bird is ignoring the status code here. The result is
3071 * that we might end up with an extra even signalling and the
3072 * next time we need to wait we won't and end up with some log
3073 * corruption. However, it's too much hazzle right now for
3074 * a scenario which would most likely end the process rather
3075 * than causing log corruption.
3076 */
3077 if (fEmtWaiting)
3078 {
3079 STAM_REL_PROFILE_START(&pShared->StatWait, a);
3080 VMMR0EmtWaitEventInner(pGVCpu, VMMR0EMTWAIT_F_TRY_SUPPRESS_INTERRUPTED,
3081 pR0Log->hEventFlushWait, RT_INDEFINITE_WAIT);
3082 STAM_REL_PROFILE_STOP(&pShared->StatWait, a);
3083 }
3084
3085 /*
3086 * We always switch buffer if we have more than one.
3087 */
3088#if VMMLOGGER_BUFFER_COUNT == 1
3089 fFlushedBuffer = true;
3090#else
3091 AssertCompile(VMMLOGGER_BUFFER_COUNT >= 1);
3092 pShared->idxBuf = (idxBuffer + 1) % VMMLOGGER_BUFFER_COUNT;
3093 fFlushedBuffer = false;
3094#endif
3095 }
3096 else
3097 {
3098 RTSpinlockRelease(pGVM->vmmr0.s.LogFlusher.hSpinlock);
3099 SUP_DPRINTF(("vmmR0LoggerFlush: ring buffer is full!\n"));
3100 fFlushedBuffer = true;
3101 }
3102 }
3103 else
3104 {
3105 RTSpinlockRelease(pGVM->vmmr0.s.LogFlusher.hSpinlock);
3106 SUP_DPRINTF(("vmmR0LoggerFlush: flusher not active - dropping %u bytes\n", cbToFlush));
3107 fFlushedBuffer = true;
3108 }
3109
3110 /*
3111 * Restore the HM context.
3112 */
3113 if (enmAction != kJustSignal)
3114 VMMR0EmtResumeAfterBlocking(pGVCpu, &Ctx);
3115
3116 return fFlushedBuffer;
3117}
3118
3119
3120/**
3121 * Common worker for vmmR0LogFlush and vmmR0LogRelFlush.
3122 */
3123static bool vmmR0LoggerFlushCommon(PRTLOGGER pLogger, PRTLOGBUFFERDESC pBufDesc, uint32_t idxLogger)
3124{
3125 /*
3126 * Convert the pLogger into a GVMCPU handle and 'call' back to Ring-3.
3127 * (This is a bit paranoid code.)
3128 */
3129 if (RT_VALID_PTR(pLogger))
3130 {
3131 if ( pLogger->u32Magic == RTLOGGER_MAGIC
3132 && (pLogger->u32UserValue1 & VMMR0_LOGGER_FLAGS_MAGIC_MASK) == VMMR0_LOGGER_FLAGS_MAGIC_VALUE
3133 && pLogger->u64UserValue2 == pLogger->u64UserValue3)
3134 {
3135 PGVMCPU const pGVCpu = (PGVMCPU)(uintptr_t)pLogger->u64UserValue2;
3136 if ( RT_VALID_PTR(pGVCpu)
3137 && ((uintptr_t)pGVCpu & PAGE_OFFSET_MASK) == 0)
3138 {
3139 RTNATIVETHREAD const hNativeSelf = RTThreadNativeSelf();
3140 PGVM const pGVM = pGVCpu->pGVM;
3141 if ( hNativeSelf == pGVCpu->hEMT
3142 && RT_VALID_PTR(pGVM))
3143 {
3144 PVMMR0PERVCPULOGGER const pR0Log = &pGVCpu->vmmr0.s.u.aLoggers[idxLogger];
3145 size_t const idxBuffer = pBufDesc - &pR0Log->aBufDescs[0];
3146 if (idxBuffer < VMMLOGGER_BUFFER_COUNT)
3147 {
3148 /*
3149 * Make sure we don't recurse forever here should something in the
3150 * following code trigger logging or an assertion. Do the rest in
3151 * an inner work to avoid hitting the right margin too hard.
3152 */
3153 if (!pR0Log->fFlushing)
3154 {
3155 pR0Log->fFlushing = true;
3156 bool fFlushed = vmmR0LoggerFlushInner(pGVM, pGVCpu, idxLogger, idxBuffer, pBufDesc->offBuf);
3157 pR0Log->fFlushing = false;
3158 return fFlushed;
3159 }
3160
3161 SUP_DPRINTF(("vmmR0LoggerFlush: Recursive flushing!\n"));
3162 }
3163 else
3164 SUP_DPRINTF(("vmmR0LoggerFlush: pLogger=%p pGVCpu=%p: idxBuffer=%#zx\n", pLogger, pGVCpu, idxBuffer));
3165 }
3166 else
3167 SUP_DPRINTF(("vmmR0LoggerFlush: pLogger=%p pGVCpu=%p hEMT=%p hNativeSelf=%p!\n",
3168 pLogger, pGVCpu, pGVCpu->hEMT, hNativeSelf));
3169 }
3170 else
3171 SUP_DPRINTF(("vmmR0LoggerFlush: pLogger=%p pGVCpu=%p!\n", pLogger, pGVCpu));
3172 }
3173 else
3174 SUP_DPRINTF(("vmmR0LoggerFlush: pLogger=%p u32Magic=%#x u32UserValue1=%#x u64UserValue2=%#RX64 u64UserValue3=%#RX64!\n",
3175 pLogger, pLogger->u32Magic, pLogger->u32UserValue1, pLogger->u64UserValue2, pLogger->u64UserValue3));
3176 }
3177 else
3178 SUP_DPRINTF(("vmmR0LoggerFlush: pLogger=%p!\n", pLogger));
3179 return true;
3180}
3181
3182
3183/**
3184 * @callback_method_impl{FNRTLOGFLUSH, Release logger buffer flush callback.}
3185 */
3186static DECLCALLBACK(bool) vmmR0LogRelFlush(PRTLOGGER pLogger, PRTLOGBUFFERDESC pBufDesc)
3187{
3188 return vmmR0LoggerFlushCommon(pLogger, pBufDesc, VMMLOGGER_IDX_RELEASE);
3189}
3190
3191
3192/**
3193 * @callback_method_impl{FNRTLOGFLUSH, Logger (debug) buffer flush callback.}
3194 */
3195static DECLCALLBACK(bool) vmmR0LogFlush(PRTLOGGER pLogger, PRTLOGBUFFERDESC pBufDesc)
3196{
3197#ifdef LOG_ENABLED
3198 return vmmR0LoggerFlushCommon(pLogger, pBufDesc, VMMLOGGER_IDX_REGULAR);
3199#else
3200 RT_NOREF(pLogger, pBufDesc);
3201 return true;
3202#endif
3203}
3204
3205
3206/*
3207 * Override RTLogDefaultInstanceEx so we can do logging from EMTs in ring-0.
3208 */
3209DECLEXPORT(PRTLOGGER) RTLogDefaultInstanceEx(uint32_t fFlagsAndGroup)
3210{
3211#ifdef LOG_ENABLED
3212 PGVMCPU pGVCpu = GVMMR0GetGVCpuByEMT(NIL_RTNATIVETHREAD);
3213 if (pGVCpu)
3214 {
3215 PRTLOGGER pLogger = pGVCpu->vmmr0.s.u.s.Logger.pLogger;
3216 if (RT_VALID_PTR(pLogger))
3217 {
3218 if ( pLogger->u64UserValue2 == (uintptr_t)pGVCpu
3219 && pLogger->u64UserValue3 == (uintptr_t)pGVCpu)
3220 {
3221 if (!pGVCpu->vmmr0.s.u.s.Logger.fFlushing)
3222 {
3223 if (!(pGVCpu->vmmr0.s.fLogFlushingDisabled))
3224 return RTLogCheckGroupFlags(pLogger, fFlagsAndGroup);
3225 return NULL;
3226 }
3227
3228 /*
3229 * When we're flushing we _must_ return NULL here to suppress any
3230 * attempts at using the logger while in vmmR0LoggerFlushCommon.
3231 * The VMMR0EmtPrepareToBlock code may trigger logging in HM,
3232 * which will reset the buffer content before we even get to queue
3233 * the flush request. (Only an issue when VBOX_WITH_R0_LOGGING
3234 * is enabled.)
3235 */
3236 return NULL;
3237 }
3238 }
3239 }
3240#endif
3241 return SUPR0DefaultLogInstanceEx(fFlagsAndGroup);
3242}
3243
3244
3245/*
3246 * Override RTLogRelGetDefaultInstanceEx so we can do LogRel to VBox.log from EMTs in ring-0.
3247 */
3248DECLEXPORT(PRTLOGGER) RTLogRelGetDefaultInstanceEx(uint32_t fFlagsAndGroup)
3249{
3250 PGVMCPU pGVCpu = GVMMR0GetGVCpuByEMT(NIL_RTNATIVETHREAD);
3251 if (pGVCpu)
3252 {
3253 PRTLOGGER pLogger = pGVCpu->vmmr0.s.u.s.RelLogger.pLogger;
3254 if (RT_VALID_PTR(pLogger))
3255 {
3256 if ( pLogger->u64UserValue2 == (uintptr_t)pGVCpu
3257 && pLogger->u64UserValue3 == (uintptr_t)pGVCpu)
3258 {
3259 if (!pGVCpu->vmmr0.s.u.s.RelLogger.fFlushing)
3260 {
3261 if (!(pGVCpu->vmmr0.s.fLogFlushingDisabled))
3262 return RTLogCheckGroupFlags(pLogger, fFlagsAndGroup);
3263 return NULL;
3264 }
3265 }
3266 }
3267 }
3268 return SUPR0GetDefaultLogRelInstanceEx(fFlagsAndGroup);
3269}
3270
3271
3272/**
3273 * Helper for vmmR0InitLoggerSet
3274 */
3275static int vmmR0InitLoggerOne(PGVMCPU pGVCpu, bool fRelease, PVMMR0PERVCPULOGGER pR0Log, PVMMR3CPULOGGER pShared,
3276 uint32_t cbBuf, char *pchBuf, RTR3PTR pchBufR3)
3277{
3278 /*
3279 * Create and configure the logger.
3280 */
3281 for (size_t i = 0; i < VMMLOGGER_BUFFER_COUNT; i++)
3282 {
3283 pR0Log->aBufDescs[i].u32Magic = RTLOGBUFFERDESC_MAGIC;
3284 pR0Log->aBufDescs[i].uReserved = 0;
3285 pR0Log->aBufDescs[i].cbBuf = cbBuf;
3286 pR0Log->aBufDescs[i].offBuf = 0;
3287 pR0Log->aBufDescs[i].pchBuf = pchBuf + i * cbBuf;
3288 pR0Log->aBufDescs[i].pAux = &pShared->aBufs[i].AuxDesc;
3289
3290 pShared->aBufs[i].AuxDesc.fFlushedIndicator = false;
3291 pShared->aBufs[i].AuxDesc.afPadding[0] = 0;
3292 pShared->aBufs[i].AuxDesc.afPadding[1] = 0;
3293 pShared->aBufs[i].AuxDesc.afPadding[2] = 0;
3294 pShared->aBufs[i].AuxDesc.offBuf = 0;
3295 pShared->aBufs[i].pchBufR3 = pchBufR3 + i * cbBuf;
3296 }
3297 pShared->cbBuf = cbBuf;
3298
3299 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
3300 int rc = RTLogCreateEx(&pR0Log->pLogger, fRelease ? "VBOX_RELEASE_LOG" : "VBOX_LOG", RTLOG_F_NO_LOCKING | RTLOGFLAGS_BUFFERED,
3301 "all", RT_ELEMENTS(s_apszGroups), s_apszGroups, UINT32_MAX,
3302 VMMLOGGER_BUFFER_COUNT, pR0Log->aBufDescs, RTLOGDEST_DUMMY,
3303 NULL /*pfnPhase*/, 0 /*cHistory*/, 0 /*cbHistoryFileMax*/, 0 /*cSecsHistoryTimeSlot*/,
3304 NULL /*pErrInfo*/, NULL /*pszFilenameFmt*/);
3305 if (RT_SUCCESS(rc))
3306 {
3307 PRTLOGGER pLogger = pR0Log->pLogger;
3308 pLogger->u32UserValue1 = VMMR0_LOGGER_FLAGS_MAGIC_VALUE;
3309 pLogger->u64UserValue2 = (uintptr_t)pGVCpu;
3310 pLogger->u64UserValue3 = (uintptr_t)pGVCpu;
3311
3312 rc = RTLogSetFlushCallback(pLogger, fRelease ? vmmR0LogRelFlush : vmmR0LogFlush);
3313 if (RT_SUCCESS(rc))
3314 {
3315 RTLogSetR0ThreadNameF(pLogger, "EMT-%u-R0", pGVCpu->idCpu);
3316
3317 /*
3318 * Create the event sem the EMT waits on while flushing is happening.
3319 */
3320 rc = RTSemEventCreate(&pR0Log->hEventFlushWait);
3321 if (RT_SUCCESS(rc))
3322 return VINF_SUCCESS;
3323 pR0Log->hEventFlushWait = NIL_RTSEMEVENT;
3324 }
3325 RTLogDestroy(pLogger);
3326 }
3327 pR0Log->pLogger = NULL;
3328 return rc;
3329}
3330
3331
3332/**
3333 * Worker for VMMR0CleanupVM and vmmR0InitLoggerSet that destroys one logger.
3334 */
3335static void vmmR0TermLoggerOne(PVMMR0PERVCPULOGGER pR0Log, PVMMR3CPULOGGER pShared)
3336{
3337 RTLogDestroy(pR0Log->pLogger);
3338 pR0Log->pLogger = NULL;
3339
3340 for (size_t i = 0; i < VMMLOGGER_BUFFER_COUNT; i++)
3341 pShared->aBufs[i].pchBufR3 = NIL_RTR3PTR;
3342
3343 RTSemEventDestroy(pR0Log->hEventFlushWait);
3344 pR0Log->hEventFlushWait = NIL_RTSEMEVENT;
3345}
3346
3347
3348/**
3349 * Initializes one type of loggers for each EMT.
3350 */
3351static int vmmR0InitLoggerSet(PGVM pGVM, uint8_t idxLogger, uint32_t cbBuf, PRTR0MEMOBJ phMemObj, PRTR0MEMOBJ phMapObj)
3352{
3353 /* Allocate buffers first. */
3354 int rc = RTR0MemObjAllocPage(phMemObj, cbBuf * pGVM->cCpus * VMMLOGGER_BUFFER_COUNT, false /*fExecutable*/);
3355 if (RT_SUCCESS(rc))
3356 {
3357 rc = RTR0MemObjMapUser(phMapObj, *phMemObj, (RTR3PTR)-1, 0 /*uAlignment*/, RTMEM_PROT_READ, NIL_RTR0PROCESS);
3358 if (RT_SUCCESS(rc))
3359 {
3360 char * const pchBuf = (char *)RTR0MemObjAddress(*phMemObj);
3361 AssertPtrReturn(pchBuf, VERR_INTERNAL_ERROR_2);
3362
3363 RTR3PTR const pchBufR3 = RTR0MemObjAddressR3(*phMapObj);
3364 AssertReturn(pchBufR3 != NIL_RTR3PTR, VERR_INTERNAL_ERROR_3);
3365
3366 /* Initialize the per-CPU loggers. */
3367 for (uint32_t i = 0; i < pGVM->cCpus; i++)
3368 {
3369 PGVMCPU pGVCpu = &pGVM->aCpus[i];
3370 PVMMR0PERVCPULOGGER pR0Log = &pGVCpu->vmmr0.s.u.aLoggers[idxLogger];
3371 PVMMR3CPULOGGER pShared = &pGVCpu->vmm.s.u.aLoggers[idxLogger];
3372 rc = vmmR0InitLoggerOne(pGVCpu, idxLogger == VMMLOGGER_IDX_RELEASE, pR0Log, pShared, cbBuf,
3373 pchBuf + i * cbBuf * VMMLOGGER_BUFFER_COUNT,
3374 pchBufR3 + i * cbBuf * VMMLOGGER_BUFFER_COUNT);
3375 if (RT_FAILURE(rc))
3376 {
3377 vmmR0TermLoggerOne(pR0Log, pShared);
3378 while (i-- > 0)
3379 {
3380 pGVCpu = &pGVM->aCpus[i];
3381 vmmR0TermLoggerOne(&pGVCpu->vmmr0.s.u.aLoggers[idxLogger], &pGVCpu->vmm.s.u.aLoggers[idxLogger]);
3382 }
3383 break;
3384 }
3385 }
3386 if (RT_SUCCESS(rc))
3387 return VINF_SUCCESS;
3388
3389 /* Bail out. */
3390 RTR0MemObjFree(*phMapObj, false /*fFreeMappings*/);
3391 *phMapObj = NIL_RTR0MEMOBJ;
3392 }
3393 RTR0MemObjFree(*phMemObj, true /*fFreeMappings*/);
3394 *phMemObj = NIL_RTR0MEMOBJ;
3395 }
3396 return rc;
3397}
3398
3399
3400/**
3401 * Worker for VMMR0InitPerVMData that initializes all the logging related stuff.
3402 *
3403 * @returns VBox status code.
3404 * @param pGVM The global (ring-0) VM structure.
3405 */
3406static int vmmR0InitLoggers(PGVM pGVM)
3407{
3408 /*
3409 * Invalidate the ring buffer (not really necessary).
3410 */
3411 for (size_t idx = 0; idx < RT_ELEMENTS(pGVM->vmmr0.s.LogFlusher.aRing); idx++)
3412 pGVM->vmmr0.s.LogFlusher.aRing[idx].u32 = UINT32_MAX >> 1; /* (all bits except fProcessing set) */
3413
3414 /*
3415 * Create the spinlock and flusher event semaphore.
3416 */
3417 int rc = RTSpinlockCreate(&pGVM->vmmr0.s.LogFlusher.hSpinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "VM-Log-Flusher");
3418 if (RT_SUCCESS(rc))
3419 {
3420 rc = RTSemEventCreate(&pGVM->vmmr0.s.LogFlusher.hEvent);
3421 if (RT_SUCCESS(rc))
3422 {
3423 /*
3424 * Create the ring-0 release loggers.
3425 */
3426 rc = vmmR0InitLoggerSet(pGVM, VMMLOGGER_IDX_RELEASE, _4K,
3427 &pGVM->vmmr0.s.hMemObjReleaseLogger, &pGVM->vmmr0.s.hMapObjReleaseLogger);
3428#ifdef LOG_ENABLED
3429 if (RT_SUCCESS(rc))
3430 {
3431 /*
3432 * Create debug loggers.
3433 */
3434 rc = vmmR0InitLoggerSet(pGVM, VMMLOGGER_IDX_REGULAR, _64K,
3435 &pGVM->vmmr0.s.hMemObjLogger, &pGVM->vmmr0.s.hMapObjLogger);
3436 }
3437#endif
3438 }
3439 }
3440 return rc;
3441}
3442
3443
3444/**
3445 * Worker for VMMR0InitPerVMData that initializes all the logging related stuff.
3446 *
3447 * @param pGVM The global (ring-0) VM structure.
3448 */
3449static void vmmR0CleanupLoggers(PGVM pGVM)
3450{
3451 for (VMCPUID idCpu = 0; idCpu < pGVM->cCpus; idCpu++)
3452 {
3453 PGVMCPU pGVCpu = &pGVM->aCpus[idCpu];
3454 for (size_t iLogger = 0; iLogger < RT_ELEMENTS(pGVCpu->vmmr0.s.u.aLoggers); iLogger++)
3455 vmmR0TermLoggerOne(&pGVCpu->vmmr0.s.u.aLoggers[iLogger], &pGVCpu->vmm.s.u.aLoggers[iLogger]);
3456 }
3457
3458 /*
3459 * Free logger buffer memory.
3460 */
3461 RTR0MemObjFree(pGVM->vmmr0.s.hMapObjReleaseLogger, false /*fFreeMappings*/);
3462 pGVM->vmmr0.s.hMapObjReleaseLogger = NIL_RTR0MEMOBJ;
3463 RTR0MemObjFree(pGVM->vmmr0.s.hMemObjReleaseLogger, true /*fFreeMappings*/);
3464 pGVM->vmmr0.s.hMemObjReleaseLogger = NIL_RTR0MEMOBJ;
3465
3466 RTR0MemObjFree(pGVM->vmmr0.s.hMapObjLogger, false /*fFreeMappings*/);
3467 pGVM->vmmr0.s.hMapObjLogger = NIL_RTR0MEMOBJ;
3468 RTR0MemObjFree(pGVM->vmmr0.s.hMemObjLogger, true /*fFreeMappings*/);
3469 pGVM->vmmr0.s.hMemObjLogger = NIL_RTR0MEMOBJ;
3470
3471 /*
3472 * Free log flusher related stuff.
3473 */
3474 RTSpinlockDestroy(pGVM->vmmr0.s.LogFlusher.hSpinlock);
3475 pGVM->vmmr0.s.LogFlusher.hSpinlock = NIL_RTSPINLOCK;
3476 RTSemEventDestroy(pGVM->vmmr0.s.LogFlusher.hEvent);
3477 pGVM->vmmr0.s.LogFlusher.hEvent = NIL_RTSEMEVENT;
3478}
3479
3480
3481/*********************************************************************************************************************************
3482* Assertions *
3483*********************************************************************************************************************************/
3484
3485/**
3486 * Installs a notification callback for ring-0 assertions.
3487 *
3488 * @param pVCpu The cross context virtual CPU structure.
3489 * @param pfnCallback Pointer to the callback.
3490 * @param pvUser The user argument.
3491 *
3492 * @return VBox status code.
3493 */
3494VMMR0_INT_DECL(int) VMMR0AssertionSetNotification(PVMCPUCC pVCpu, PFNVMMR0ASSERTIONNOTIFICATION pfnCallback, RTR0PTR pvUser)
3495{
3496 AssertPtrReturn(pVCpu, VERR_INVALID_POINTER);
3497 AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
3498
3499 if (!pVCpu->vmmr0.s.pfnAssertCallback)
3500 {
3501 pVCpu->vmmr0.s.pfnAssertCallback = pfnCallback;
3502 pVCpu->vmmr0.s.pvAssertCallbackUser = pvUser;
3503 return VINF_SUCCESS;
3504 }
3505 return VERR_ALREADY_EXISTS;
3506}
3507
3508
3509/**
3510 * Removes the ring-0 callback.
3511 *
3512 * @param pVCpu The cross context virtual CPU structure.
3513 */
3514VMMR0_INT_DECL(void) VMMR0AssertionRemoveNotification(PVMCPUCC pVCpu)
3515{
3516 pVCpu->vmmr0.s.pfnAssertCallback = NULL;
3517 pVCpu->vmmr0.s.pvAssertCallbackUser = NULL;
3518}
3519
3520
3521/**
3522 * Checks whether there is a ring-0 callback notification active.
3523 *
3524 * @param pVCpu The cross context virtual CPU structure.
3525 * @returns true if there the notification is active, false otherwise.
3526 */
3527VMMR0_INT_DECL(bool) VMMR0AssertionIsNotificationSet(PVMCPUCC pVCpu)
3528{
3529 return pVCpu->vmmr0.s.pfnAssertCallback != NULL;
3530}
3531
3532
3533/*
3534 * Jump back to ring-3 if we're the EMT and the longjmp is armed.
3535 *
3536 * @returns true if the breakpoint should be hit, false if it should be ignored.
3537 */
3538DECLEXPORT(bool) RTCALL RTAssertShouldPanic(void)
3539{
3540#if 0
3541 return true;
3542#else
3543 PVMCC pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
3544 if (pVM)
3545 {
3546 PVMCPUCC pVCpu = VMMGetCpu(pVM);
3547
3548 if (pVCpu)
3549 {
3550# ifdef RT_ARCH_X86
3551 if (pVCpu->vmmr0.s.AssertJmpBuf.eip)
3552# else
3553 if (pVCpu->vmmr0.s.AssertJmpBuf.rip)
3554# endif
3555 {
3556 if (pVCpu->vmmr0.s.pfnAssertCallback)
3557 pVCpu->vmmr0.s.pfnAssertCallback(pVCpu, pVCpu->vmmr0.s.pvAssertCallbackUser);
3558 int rc = vmmR0CallRing3LongJmp(&pVCpu->vmmr0.s.AssertJmpBuf, VERR_VMM_RING0_ASSERTION);
3559 return RT_FAILURE_NP(rc);
3560 }
3561 }
3562 }
3563# ifdef RT_OS_LINUX
3564 return true;
3565# else
3566 return false;
3567# endif
3568#endif
3569}
3570
3571
3572/*
3573 * Override this so we can push it up to ring-3.
3574 */
3575DECLEXPORT(void) RTCALL RTAssertMsg1Weak(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
3576{
3577 /*
3578 * To host kernel log/whatever.
3579 */
3580 SUPR0Printf("!!R0-Assertion Failed!!\n"
3581 "Expression: %s\n"
3582 "Location : %s(%d) %s\n",
3583 pszExpr, pszFile, uLine, pszFunction);
3584
3585 /*
3586 * To the log.
3587 */
3588 LogAlways(("\n!!R0-Assertion Failed!!\n"
3589 "Expression: %s\n"
3590 "Location : %s(%d) %s\n",
3591 pszExpr, pszFile, uLine, pszFunction));
3592
3593 /*
3594 * To the global VMM buffer.
3595 */
3596 PVMCC pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
3597 if (pVM)
3598 RTStrPrintf(pVM->vmm.s.szRing0AssertMsg1, sizeof(pVM->vmm.s.szRing0AssertMsg1),
3599 "\n!!R0-Assertion Failed!!\n"
3600 "Expression: %.*s\n"
3601 "Location : %s(%d) %s\n",
3602 sizeof(pVM->vmm.s.szRing0AssertMsg1) / 4 * 3, pszExpr,
3603 pszFile, uLine, pszFunction);
3604
3605 /*
3606 * Continue the normal way.
3607 */
3608 RTAssertMsg1(pszExpr, uLine, pszFile, pszFunction);
3609}
3610
3611
3612/**
3613 * Callback for RTLogFormatV which writes to the ring-3 log port.
3614 * See PFNLOGOUTPUT() for details.
3615 */
3616static DECLCALLBACK(size_t) rtLogOutput(void *pv, const char *pachChars, size_t cbChars)
3617{
3618 for (size_t i = 0; i < cbChars; i++)
3619 {
3620 LogAlways(("%c", pachChars[i])); NOREF(pachChars);
3621 }
3622
3623 NOREF(pv);
3624 return cbChars;
3625}
3626
3627
3628/*
3629 * Override this so we can push it up to ring-3.
3630 */
3631DECLEXPORT(void) RTCALL RTAssertMsg2WeakV(const char *pszFormat, va_list va)
3632{
3633 va_list vaCopy;
3634
3635 /*
3636 * Push the message to the loggers.
3637 */
3638 PRTLOGGER pLog = RTLogRelGetDefaultInstance();
3639 if (pLog)
3640 {
3641 va_copy(vaCopy, va);
3642 RTLogFormatV(rtLogOutput, pLog, pszFormat, vaCopy);
3643 va_end(vaCopy);
3644 }
3645 pLog = RTLogGetDefaultInstance(); /* Don't initialize it here... */
3646 if (pLog)
3647 {
3648 va_copy(vaCopy, va);
3649 RTLogFormatV(rtLogOutput, pLog, pszFormat, vaCopy);
3650 va_end(vaCopy);
3651 }
3652
3653 /*
3654 * Push it to the global VMM buffer.
3655 */
3656 PVMCC pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
3657 if (pVM)
3658 {
3659 va_copy(vaCopy, va);
3660 RTStrPrintfV(pVM->vmm.s.szRing0AssertMsg2, sizeof(pVM->vmm.s.szRing0AssertMsg2), pszFormat, vaCopy);
3661 va_end(vaCopy);
3662 }
3663
3664 /*
3665 * Continue the normal way.
3666 */
3667 RTAssertMsg2V(pszFormat, va);
3668}
3669
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