VirtualBox

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

Last change on this file since 48134 was 48132, checked in by vboxsync, 11 years ago

VMM: Ditch VBOX_WITH_VMMR0_DISABLE_PREEMPTION.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 61.9 KB
Line 
1/* $Id: VMMR0.cpp 48132 2013-08-28 17:27:43Z vboxsync $ */
2/** @file
3 * VMM - Host Context Ring 0.
4 */
5
6/*
7 * Copyright (C) 2006-2012 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* Header Files *
20*******************************************************************************/
21#define LOG_GROUP LOG_GROUP_VMM
22#include <VBox/vmm/vmm.h>
23#include <VBox/sup.h>
24#include <VBox/vmm/trpm.h>
25#include <VBox/vmm/cpum.h>
26#include <VBox/vmm/pdmapi.h>
27#include <VBox/vmm/pgm.h>
28#include <VBox/vmm/stam.h>
29#include <VBox/vmm/tm.h>
30#include "VMMInternal.h"
31#include <VBox/vmm/vm.h>
32#ifdef VBOX_WITH_PCI_PASSTHROUGH
33# include <VBox/vmm/pdmpci.h>
34#endif
35
36#include <VBox/vmm/gvmm.h>
37#include <VBox/vmm/gmm.h>
38#include <VBox/intnet.h>
39#include <VBox/vmm/hm.h>
40#include <VBox/param.h>
41#include <VBox/err.h>
42#include <VBox/version.h>
43#include <VBox/log.h>
44
45#include <iprt/asm-amd64-x86.h>
46#include <iprt/assert.h>
47#include <iprt/crc.h>
48#include <iprt/mp.h>
49#include <iprt/once.h>
50#include <iprt/stdarg.h>
51#include <iprt/string.h>
52#include <iprt/thread.h>
53#include <iprt/timer.h>
54
55#include "dtrace/VBoxVMM.h"
56
57
58#if defined(_MSC_VER) && defined(RT_ARCH_AMD64) /** @todo check this with with VC7! */
59# pragma intrinsic(_AddressOfReturnAddress)
60#endif
61
62
63/*******************************************************************************
64* Internal Functions *
65*******************************************************************************/
66RT_C_DECLS_BEGIN
67#if defined(RT_ARCH_X86) && (defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD))
68extern uint64_t __udivdi3(uint64_t, uint64_t);
69extern uint64_t __umoddi3(uint64_t, uint64_t);
70#endif
71RT_C_DECLS_END
72
73
74/*******************************************************************************
75* Global Variables *
76*******************************************************************************/
77/** Drag in necessary library bits.
78 * The runtime lives here (in VMMR0.r0) and VBoxDD*R0.r0 links against us. */
79PFNRT g_VMMGCDeps[] =
80{
81 (PFNRT)RTCrc32,
82 (PFNRT)RTOnce,
83#if defined(RT_ARCH_X86) && (defined(RT_OS_SOLARIS) || defined(RT_OS_FREEBSD))
84 (PFNRT)__udivdi3,
85 (PFNRT)__umoddi3,
86#endif
87 NULL
88};
89
90#ifdef RT_OS_SOLARIS
91/* Dependency information for the native solaris loader. */
92extern "C" { char _depends_on[] = "vboxdrv"; }
93#endif
94
95
96
97/**
98 * Initialize the module.
99 * This is called when we're first loaded.
100 *
101 * @returns 0 on success.
102 * @returns VBox status on failure.
103 * @param hMod Image handle for use in APIs.
104 */
105DECLEXPORT(int) ModuleInit(void *hMod)
106{
107#ifdef VBOX_WITH_DTRACE_R0
108 /*
109 * The first thing to do is register the static tracepoints.
110 * (Deregistration is automatic.)
111 */
112 int rc2 = SUPR0TracerRegisterModule(hMod, &g_VTGObjHeader);
113 if (RT_FAILURE(rc2))
114 return rc2;
115#endif
116 LogFlow(("ModuleInit:\n"));
117
118 /*
119 * Initialize the VMM, GVMM, GMM, HM, PGM (Darwin) and INTNET.
120 */
121 int rc = vmmInitFormatTypes();
122 if (RT_SUCCESS(rc))
123 {
124 rc = GVMMR0Init();
125 if (RT_SUCCESS(rc))
126 {
127 rc = GMMR0Init();
128 if (RT_SUCCESS(rc))
129 {
130 rc = HMR0Init();
131 if (RT_SUCCESS(rc))
132 {
133 rc = PGMRegisterStringFormatTypes();
134 if (RT_SUCCESS(rc))
135 {
136#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
137 rc = PGMR0DynMapInit();
138#endif
139 if (RT_SUCCESS(rc))
140 {
141 rc = IntNetR0Init();
142 if (RT_SUCCESS(rc))
143 {
144#ifdef VBOX_WITH_PCI_PASSTHROUGH
145 rc = PciRawR0Init();
146#endif
147 if (RT_SUCCESS(rc))
148 {
149 rc = CPUMR0ModuleInit();
150 if (RT_SUCCESS(rc))
151 {
152#ifdef VBOX_WITH_TRIPLE_FAULT_HACK
153 rc = vmmR0TripleFaultHackInit();
154 if (RT_SUCCESS(rc))
155#endif
156 {
157 LogFlow(("ModuleInit: returns success.\n"));
158 return VINF_SUCCESS;
159 }
160
161 /*
162 * Bail out.
163 */
164#ifdef VBOX_WITH_TRIPLE_FAULT_HACK
165 vmmR0TripleFaultHackTerm();
166#endif
167 }
168 else
169 LogRel(("ModuleInit: CPUMR0ModuleInit -> %Rrc\n", rc));
170#ifdef VBOX_WITH_PCI_PASSTHROUGH
171 PciRawR0Term();
172#endif
173 }
174 else
175 LogRel(("ModuleInit: PciRawR0Init -> %Rrc\n", rc));
176 IntNetR0Term();
177 }
178 else
179 LogRel(("ModuleInit: IntNetR0Init -> %Rrc\n", rc));
180#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
181 PGMR0DynMapTerm();
182#endif
183 }
184 else
185 LogRel(("ModuleInit: PGMR0DynMapInit -> %Rrc\n", rc));
186 PGMDeregisterStringFormatTypes();
187 }
188 else
189 LogRel(("ModuleInit: PGMRegisterStringFormatTypes -> %Rrc\n", rc));
190 HMR0Term();
191 }
192 else
193 LogRel(("ModuleInit: HMR0Init -> %Rrc\n", rc));
194 GMMR0Term();
195 }
196 else
197 LogRel(("ModuleInit: GMMR0Init -> %Rrc\n", rc));
198 GVMMR0Term();
199 }
200 else
201 LogRel(("ModuleInit: GVMMR0Init -> %Rrc\n", rc));
202 vmmTermFormatTypes();
203 }
204 else
205 LogRel(("ModuleInit: vmmInitFormatTypes -> %Rrc\n", rc));
206
207 LogFlow(("ModuleInit: failed %Rrc\n", rc));
208 return rc;
209}
210
211
212/**
213 * Terminate the module.
214 * This is called when we're finally unloaded.
215 *
216 * @param hMod Image handle for use in APIs.
217 */
218DECLEXPORT(void) ModuleTerm(void *hMod)
219{
220 LogFlow(("ModuleTerm:\n"));
221
222 /*
223 * Terminate the CPUM module (Local APIC cleanup).
224 */
225 CPUMR0ModuleTerm();
226
227 /*
228 * Terminate the internal network service.
229 */
230 IntNetR0Term();
231
232 /*
233 * PGM (Darwin), HM and PciRaw global cleanup.
234 */
235#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
236 PGMR0DynMapTerm();
237#endif
238#ifdef VBOX_WITH_PCI_PASSTHROUGH
239 PciRawR0Term();
240#endif
241 PGMDeregisterStringFormatTypes();
242 HMR0Term();
243#ifdef VBOX_WITH_TRIPLE_FAULT_HACK
244 vmmR0TripleFaultHackTerm();
245#endif
246
247 /*
248 * Destroy the GMM and GVMM instances.
249 */
250 GMMR0Term();
251 GVMMR0Term();
252
253 vmmTermFormatTypes();
254
255 LogFlow(("ModuleTerm: returns\n"));
256}
257
258
259/**
260 * Initiates the R0 driver for a particular VM instance.
261 *
262 * @returns VBox status code.
263 *
264 * @param pVM Pointer to the VM.
265 * @param uSvnRev The SVN revision of the ring-3 part.
266 * @param uBuildType Build type indicator.
267 * @thread EMT.
268 */
269static int vmmR0InitVM(PVM pVM, uint32_t uSvnRev, uint32_t uBuildType)
270{
271 /*
272 * Match the SVN revisions and build type.
273 */
274 if (uSvnRev != VMMGetSvnRev())
275 {
276 LogRel(("VMMR0InitVM: Revision mismatch, r3=%d r0=%d\n", uSvnRev, VMMGetSvnRev()));
277 SUPR0Printf("VMMR0InitVM: Revision mismatch, r3=%d r0=%d\n", uSvnRev, VMMGetSvnRev());
278 return VERR_VMM_R0_VERSION_MISMATCH;
279 }
280 if (uBuildType != vmmGetBuildType())
281 {
282 LogRel(("VMMR0InitVM: Build type mismatch, r3=%#x r0=%#x\n", uBuildType, vmmGetBuildType()));
283 SUPR0Printf("VMMR0InitVM: Build type mismatch, r3=%#x r0=%#x\n", uBuildType, vmmGetBuildType());
284 return VERR_VMM_R0_VERSION_MISMATCH;
285 }
286 if ( !VALID_PTR(pVM)
287 || pVM->pVMR0 != pVM)
288 return VERR_INVALID_PARAMETER;
289
290
291#ifdef LOG_ENABLED
292 /*
293 * Register the EMT R0 logger instance for VCPU 0.
294 */
295 PVMCPU pVCpu = &pVM->aCpus[0];
296
297 PVMMR0LOGGER pR0Logger = pVCpu->vmm.s.pR0LoggerR0;
298 if (pR0Logger)
299 {
300# if 0 /* testing of the logger. */
301 LogCom(("vmmR0InitVM: before %p\n", RTLogDefaultInstance()));
302 LogCom(("vmmR0InitVM: pfnFlush=%p actual=%p\n", pR0Logger->Logger.pfnFlush, vmmR0LoggerFlush));
303 LogCom(("vmmR0InitVM: pfnLogger=%p actual=%p\n", pR0Logger->Logger.pfnLogger, vmmR0LoggerWrapper));
304 LogCom(("vmmR0InitVM: offScratch=%d fFlags=%#x fDestFlags=%#x\n", pR0Logger->Logger.offScratch, pR0Logger->Logger.fFlags, pR0Logger->Logger.fDestFlags));
305
306 RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
307 LogCom(("vmmR0InitVM: after %p reg\n", RTLogDefaultInstance()));
308 RTLogSetDefaultInstanceThread(NULL, pVM->pSession);
309 LogCom(("vmmR0InitVM: after %p dereg\n", RTLogDefaultInstance()));
310
311 pR0Logger->Logger.pfnLogger("hello ring-0 logger\n");
312 LogCom(("vmmR0InitVM: returned successfully from direct logger call.\n"));
313 pR0Logger->Logger.pfnFlush(&pR0Logger->Logger);
314 LogCom(("vmmR0InitVM: returned successfully from direct flush call.\n"));
315
316 RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
317 LogCom(("vmmR0InitVM: after %p reg2\n", RTLogDefaultInstance()));
318 pR0Logger->Logger.pfnLogger("hello ring-0 logger\n");
319 LogCom(("vmmR0InitVM: returned successfully from direct logger call (2). offScratch=%d\n", pR0Logger->Logger.offScratch));
320 RTLogSetDefaultInstanceThread(NULL, pVM->pSession);
321 LogCom(("vmmR0InitVM: after %p dereg2\n", RTLogDefaultInstance()));
322
323 RTLogLoggerEx(&pR0Logger->Logger, 0, ~0U, "hello ring-0 logger (RTLogLoggerEx)\n");
324 LogCom(("vmmR0InitVM: RTLogLoggerEx returned fine offScratch=%d\n", pR0Logger->Logger.offScratch));
325
326 RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
327 RTLogPrintf("hello ring-0 logger (RTLogPrintf)\n");
328 LogCom(("vmmR0InitVM: RTLogPrintf returned fine offScratch=%d\n", pR0Logger->Logger.offScratch));
329# endif
330 Log(("Switching to per-thread logging instance %p (key=%p)\n", &pR0Logger->Logger, pVM->pSession));
331 RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
332 pR0Logger->fRegistered = true;
333 }
334#endif /* LOG_ENABLED */
335
336 /*
337 * Check if the host supports high resolution timers or not.
338 */
339 if ( pVM->vmm.s.fUsePeriodicPreemptionTimers
340 && !RTTimerCanDoHighResolution())
341 pVM->vmm.s.fUsePeriodicPreemptionTimers = false;
342
343 /*
344 * Initialize the per VM data for GVMM and GMM.
345 */
346 int rc = GVMMR0InitVM(pVM);
347// if (RT_SUCCESS(rc))
348// rc = GMMR0InitPerVMData(pVM);
349 if (RT_SUCCESS(rc))
350 {
351 /*
352 * Init HM, CPUM and PGM (Darwin only).
353 */
354 rc = HMR0InitVM(pVM);
355 if (RT_SUCCESS(rc))
356 {
357 rc = CPUMR0InitVM(pVM);
358 if (RT_SUCCESS(rc))
359 {
360#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
361 rc = PGMR0DynMapInitVM(pVM);
362#endif
363 if (RT_SUCCESS(rc))
364 {
365#ifdef VBOX_WITH_PCI_PASSTHROUGH
366 rc = PciRawR0InitVM(pVM);
367#endif
368 if (RT_SUCCESS(rc))
369 {
370 GVMMR0DoneInitVM(pVM);
371 return rc;
372 }
373 }
374
375 /* bail out */
376 }
377#ifdef VBOX_WITH_PCI_PASSTHROUGH
378 PciRawR0TermVM(pVM);
379#endif
380 HMR0TermVM(pVM);
381 }
382 }
383
384
385 RTLogSetDefaultInstanceThread(NULL, (uintptr_t)pVM->pSession);
386 return rc;
387}
388
389
390/**
391 * Terminates the R0 bits for a particular VM instance.
392 *
393 * This is normally called by ring-3 as part of the VM termination process, but
394 * may alternatively be called during the support driver session cleanup when
395 * the VM object is destroyed (see GVMM).
396 *
397 * @returns VBox status code.
398 *
399 * @param pVM Pointer to the VM.
400 * @param pGVM Pointer to the global VM structure. Optional.
401 * @thread EMT or session clean up thread.
402 */
403VMMR0DECL(int) VMMR0TermVM(PVM pVM, PGVM pGVM)
404{
405#ifdef VBOX_WITH_PCI_PASSTHROUGH
406 PciRawR0TermVM(pVM);
407#endif
408
409
410 /*
411 * Tell GVMM what we're up to and check that we only do this once.
412 */
413 if (GVMMR0DoingTermVM(pVM, pGVM))
414 {
415 /** @todo I wish to call PGMR0PhysFlushHandyPages(pVM, &pVM->aCpus[idCpu])
416 * here to make sure we don't leak any shared pages if we crash... */
417#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
418 PGMR0DynMapTermVM(pVM);
419#endif
420 HMR0TermVM(pVM);
421 }
422
423 /*
424 * Deregister the logger.
425 */
426 RTLogSetDefaultInstanceThread(NULL, (uintptr_t)pVM->pSession);
427 return VINF_SUCCESS;
428}
429
430
431/**
432 * Creates R0 thread-context hooks for the current EMT thread.
433 *
434 * @returns VBox status code.
435 *
436 * @param pVCpu Pointer to the VMCPU.
437 * @thread EMT.
438 */
439VMMR0DECL(int) VMMR0ThreadCtxHooksCreate(PVMCPU pVCpu)
440{
441 VMCPU_ASSERT_EMT(pVCpu);
442 Assert(pVCpu->vmm.s.hR0ThreadCtx == NIL_RTTHREADCTX);
443#if defined(RT_OS_LINUX) || defined(RT_OS_SOLARIS)
444 int rc = RTThreadCtxHooksCreate(&pVCpu->vmm.s.hR0ThreadCtx);
445 if ( RT_FAILURE(rc)
446 && rc != VERR_NOT_SUPPORTED)
447 {
448 Log(("RTThreadCtxHooksCreate failed! rc=%Rrc pVCpu=%p idCpu=%RU32\n", rc, pVCpu, pVCpu->idCpu));
449 return rc;
450 }
451#endif
452
453 return VINF_SUCCESS;
454}
455
456
457/**
458 * Releases the object reference for the thread-context hook.
459 *
460 * @param pVCpu Pointer to the VMCPU.
461 * @remarks Can be called from any thread.
462 */
463VMMR0DECL(void) VMMR0ThreadCtxHooksRelease(PVMCPU pVCpu)
464{
465 RTThreadCtxHooksRelease(pVCpu->vmm.s.hR0ThreadCtx);
466}
467
468
469/**
470 * Registers the thread-context hook for this VCPU.
471 *
472 * @returns VBox status code.
473 * @param pVCpu Pointer to the VMCPU.
474 * @param pfnThreadHook Pointer to the thread-context callback.
475 *
476 * @thread EMT.
477 */
478VMMR0DECL(int) VMMR0ThreadCtxHooksRegister(PVMCPU pVCpu, PFNRTTHREADCTXHOOK pfnThreadHook)
479{
480 VMCPU_ASSERT_EMT(pVCpu);
481 return RTThreadCtxHooksRegister(pVCpu->vmm.s.hR0ThreadCtx, pfnThreadHook, pVCpu);
482}
483
484
485/**
486 * Deregisters the thread-context hook for this VCPU.
487 *
488 * @returns VBox status code.
489 * @param pVCpu Pointer to the VMCPU.
490 *
491 * @thread EMT.
492 */
493VMMR0DECL(int) VMMR0ThreadCtxHooksDeregister(PVMCPU pVCpu)
494{
495 return RTThreadCtxHooksDeregister(pVCpu->vmm.s.hR0ThreadCtx);
496}
497
498
499/**
500 * Whether thread-context hooks are created (implying they're supported) on this
501 * platform.
502 *
503 * @returns true if the hooks are created, false otherwise.
504 * @param pVCpu Pointer to the VMCPU.
505 *
506 * @remarks Can be called from any thread.
507 */
508VMMR0DECL(bool) VMMR0ThreadCtxHooksAreCreated(PVMCPU pVCpu)
509{
510 return pVCpu->vmm.s.hR0ThreadCtx != NIL_RTTHREADCTX;
511}
512
513
514/**
515 * Whether thread-context hooks are registered for this VCPU.
516 *
517 * @returns true if registered, false otherwise.
518 * @param pVCpu Pointer to the VMCPU.
519 */
520VMMR0DECL(bool) VMMR0ThreadCtxHooksAreRegistered(PVMCPU pVCpu)
521{
522 return RTThreadCtxHooksAreRegistered(pVCpu->vmm.s.hR0ThreadCtx);
523}
524
525
526/**
527 * VMM ring-0 thread-context callback.
528 *
529 * This does common HM state updating and calls the HM-specific thread-context
530 * callback.
531 *
532 * @param enmEvent The thread-context event.
533 * @param pvUser Opaque pointer to the VMCPU.
534 */
535static DECLCALLBACK(void) vmmR0ThreadCtxCallback(RTTHREADCTXEVENT enmEvent, void *pvUser)
536{
537 PVMCPU pVCpu = (PVMCPU)pvUser;
538
539 switch (enmEvent)
540 {
541 case RTTHREADCTXEVENT_RESUMED:
542 {
543 /** @todo Linux may call us with preemption enabled (really!) but technically we
544 * cannot get preempted here, otherwise we end up in an infinite recursion
545 * scenario (i.e. preempted in resume hook -> preempt hook -> resume hook... ad
546 * infinitum). Let's just disable preemption for now...
547 */
548 HM_DISABLE_PREEMPT_IF_NEEDED();
549
550 /* We need to update the VCPU <-> host CPU mapping. */
551 RTCPUID idHostCpu = RTMpCpuId();
552 ASMAtomicWriteU32(&pVCpu->idHostCpu, idHostCpu);
553
554 /* Invoke the HM-specific thread-context callback. */
555 HMR0ThreadCtxCallback(enmEvent, pvUser);
556
557 /* Restore preemption. */
558 HM_RESTORE_PREEMPT_IF_NEEDED();
559 break;
560 }
561
562 case RTTHREADCTXEVENT_PREEMPTING:
563 {
564 /* Invoke the HM-specific thread-context callback. */
565 HMR0ThreadCtxCallback(enmEvent, pvUser);
566
567 /*
568 * Sigh. See VMMGetCpu() used by VMCPU_ASSERT_EMT(). We cannot let several VCPUs
569 * have the same host CPU associated with it.
570 */
571 ASMAtomicWriteU32(&pVCpu->idHostCpu, NIL_RTCPUID);
572 break;
573 }
574
575 default:
576 /* Invoke the HM-specific thread-context callback. */
577 HMR0ThreadCtxCallback(enmEvent, pvUser);
578 break;
579 }
580}
581
582
583#ifdef VBOX_WITH_STATISTICS
584/**
585 * Record return code statistics
586 * @param pVM Pointer to the VM.
587 * @param pVCpu Pointer to the VMCPU.
588 * @param rc The status code.
589 */
590static void vmmR0RecordRC(PVM pVM, PVMCPU pVCpu, int rc)
591{
592 /*
593 * Collect statistics.
594 */
595 switch (rc)
596 {
597 case VINF_SUCCESS:
598 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetNormal);
599 break;
600 case VINF_EM_RAW_INTERRUPT:
601 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetInterrupt);
602 break;
603 case VINF_EM_RAW_INTERRUPT_HYPER:
604 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetInterruptHyper);
605 break;
606 case VINF_EM_RAW_GUEST_TRAP:
607 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetGuestTrap);
608 break;
609 case VINF_EM_RAW_RING_SWITCH:
610 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetRingSwitch);
611 break;
612 case VINF_EM_RAW_RING_SWITCH_INT:
613 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetRingSwitchInt);
614 break;
615 case VINF_EM_RAW_STALE_SELECTOR:
616 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetStaleSelector);
617 break;
618 case VINF_EM_RAW_IRET_TRAP:
619 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIRETTrap);
620 break;
621 case VINF_IOM_R3_IOPORT_READ:
622 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIORead);
623 break;
624 case VINF_IOM_R3_IOPORT_WRITE:
625 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIOWrite);
626 break;
627 case VINF_IOM_R3_MMIO_READ:
628 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIORead);
629 break;
630 case VINF_IOM_R3_MMIO_WRITE:
631 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOWrite);
632 break;
633 case VINF_IOM_R3_MMIO_READ_WRITE:
634 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOReadWrite);
635 break;
636 case VINF_PATM_HC_MMIO_PATCH_READ:
637 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOPatchRead);
638 break;
639 case VINF_PATM_HC_MMIO_PATCH_WRITE:
640 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOPatchWrite);
641 break;
642 case VINF_EM_RAW_EMULATE_INSTR:
643 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetEmulate);
644 break;
645 case VINF_EM_RAW_EMULATE_IO_BLOCK:
646 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIOBlockEmulate);
647 break;
648 case VINF_PATCH_EMULATE_INSTR:
649 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchEmulate);
650 break;
651 case VINF_EM_RAW_EMULATE_INSTR_LDT_FAULT:
652 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetLDTFault);
653 break;
654 case VINF_EM_RAW_EMULATE_INSTR_GDT_FAULT:
655 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetGDTFault);
656 break;
657 case VINF_EM_RAW_EMULATE_INSTR_IDT_FAULT:
658 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIDTFault);
659 break;
660 case VINF_EM_RAW_EMULATE_INSTR_TSS_FAULT:
661 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetTSSFault);
662 break;
663 case VINF_EM_RAW_EMULATE_INSTR_PD_FAULT:
664 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPDFault);
665 break;
666 case VINF_CSAM_PENDING_ACTION:
667 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetCSAMTask);
668 break;
669 case VINF_PGM_SYNC_CR3:
670 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetSyncCR3);
671 break;
672 case VINF_PATM_PATCH_INT3:
673 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchInt3);
674 break;
675 case VINF_PATM_PATCH_TRAP_PF:
676 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchPF);
677 break;
678 case VINF_PATM_PATCH_TRAP_GP:
679 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchGP);
680 break;
681 case VINF_PATM_PENDING_IRQ_AFTER_IRET:
682 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchIretIRQ);
683 break;
684 case VINF_EM_RESCHEDULE_REM:
685 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetRescheduleREM);
686 break;
687 case VINF_EM_RAW_TO_R3:
688 if (VM_FF_IS_PENDING(pVM, VM_FF_TM_VIRTUAL_SYNC))
689 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3TMVirt);
690 else if (VM_FF_IS_PENDING(pVM, VM_FF_PGM_NEED_HANDY_PAGES))
691 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3HandyPages);
692 else if (VM_FF_IS_PENDING(pVM, VM_FF_PDM_QUEUES))
693 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3PDMQueues);
694 else if (VM_FF_IS_PENDING(pVM, VM_FF_EMT_RENDEZVOUS))
695 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3Rendezvous);
696 else if (VM_FF_IS_PENDING(pVM, VM_FF_PDM_DMA))
697 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3DMA);
698 else if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_TIMER))
699 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3Timer);
700 else if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_PDM_CRITSECT))
701 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3CritSect);
702 else if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_TO_R3))
703 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3);
704 else
705 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3Unknown);
706 break;
707
708 case VINF_EM_RAW_TIMER_PENDING:
709 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetTimerPending);
710 break;
711 case VINF_EM_RAW_INTERRUPT_PENDING:
712 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetInterruptPending);
713 break;
714 case VINF_VMM_CALL_HOST:
715 switch (pVCpu->vmm.s.enmCallRing3Operation)
716 {
717 case VMMCALLRING3_PDM_CRIT_SECT_ENTER:
718 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPDMCritSectEnter);
719 break;
720 case VMMCALLRING3_PDM_LOCK:
721 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPDMLock);
722 break;
723 case VMMCALLRING3_PGM_POOL_GROW:
724 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMPoolGrow);
725 break;
726 case VMMCALLRING3_PGM_LOCK:
727 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMLock);
728 break;
729 case VMMCALLRING3_PGM_MAP_CHUNK:
730 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMMapChunk);
731 break;
732 case VMMCALLRING3_PGM_ALLOCATE_HANDY_PAGES:
733 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMAllocHandy);
734 break;
735 case VMMCALLRING3_REM_REPLAY_HANDLER_NOTIFICATIONS:
736 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallRemReplay);
737 break;
738 case VMMCALLRING3_VMM_LOGGER_FLUSH:
739 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallLogFlush);
740 break;
741 case VMMCALLRING3_VM_SET_ERROR:
742 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallVMSetError);
743 break;
744 case VMMCALLRING3_VM_SET_RUNTIME_ERROR:
745 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallVMSetRuntimeError);
746 break;
747 case VMMCALLRING3_VM_R0_ASSERTION:
748 default:
749 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetCallRing3);
750 break;
751 }
752 break;
753 case VINF_PATM_DUPLICATE_FUNCTION:
754 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPATMDuplicateFn);
755 break;
756 case VINF_PGM_CHANGE_MODE:
757 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPGMChangeMode);
758 break;
759 case VINF_PGM_POOL_FLUSH_PENDING:
760 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPGMFlushPending);
761 break;
762 case VINF_EM_PENDING_REQUEST:
763 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPendingRequest);
764 break;
765 case VINF_EM_HM_PATCH_TPR_INSTR:
766 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchTPR);
767 break;
768 default:
769 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMisc);
770 break;
771 }
772}
773#endif /* VBOX_WITH_STATISTICS */
774
775
776/**
777 * Unused ring-0 entry point that used to be called from the interrupt gate.
778 *
779 * Will be removed one of the next times we do a major SUPDrv version bump.
780 *
781 * @returns VBox status code.
782 * @param pVM Pointer to the VM.
783 * @param enmOperation Which operation to execute.
784 * @param pvArg Argument to the operation.
785 * @remarks Assume called with interrupts disabled.
786 */
787VMMR0DECL(int) VMMR0EntryInt(PVM pVM, VMMR0OPERATION enmOperation, void *pvArg)
788{
789 /*
790 * We're returning VERR_NOT_SUPPORT here so we've got something else
791 * than -1 which the interrupt gate glue code might return.
792 */
793 Log(("operation %#x is not supported\n", enmOperation));
794 NOREF(enmOperation); NOREF(pvArg); NOREF(pVM);
795 return VERR_NOT_SUPPORTED;
796}
797
798
799/**
800 * The Ring 0 entry point, called by the fast-ioctl path.
801 *
802 * @param pVM Pointer to the VM.
803 * The return code is stored in pVM->vmm.s.iLastGZRc.
804 * @param idCpu The Virtual CPU ID of the calling EMT.
805 * @param enmOperation Which operation to execute.
806 * @remarks Assume called with interrupts _enabled_.
807 */
808VMMR0DECL(void) VMMR0EntryFast(PVM pVM, VMCPUID idCpu, VMMR0OPERATION enmOperation)
809{
810 if (RT_UNLIKELY(idCpu >= pVM->cCpus))
811 return;
812 PVMCPU pVCpu = &pVM->aCpus[idCpu];
813
814 switch (enmOperation)
815 {
816 /*
817 * Switch to GC and run guest raw mode code.
818 * Disable interrupts before doing the world switch.
819 */
820 case VMMR0_DO_RAW_RUN:
821 {
822#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
823 /* Some safety precautions first. */
824 if (RT_UNLIKELY(!PGMGetHyperCR3(pVCpu)))
825 {
826 pVCpu->vmm.s.iLastGZRc = VERR_PGM_NO_CR3_SHADOW_ROOT;
827 break;
828 }
829#endif
830
831 /* Disable preemption and update the periodic preemption timer. */
832 RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
833 RTThreadPreemptDisable(&PreemptState);
834 RTCPUID idHostCpu = RTMpCpuId();
835#ifdef VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
836 CPUMR0SetLApic(pVCpu, idHostCpu);
837#endif
838 ASMAtomicWriteU32(&pVCpu->idHostCpu, idHostCpu);
839 if (pVM->vmm.s.fUsePeriodicPreemptionTimers)
840 GVMMR0SchedUpdatePeriodicPreemptionTimer(pVM, pVCpu->idHostCpu, TMCalcHostTimerFrequency(pVM, pVCpu));
841
842 /* We might need to disable VT-x if the active switcher turns off paging. */
843 bool fVTxDisabled;
844 int rc = HMR0EnterSwitcher(pVM, pVM->vmm.s.enmSwitcher, &fVTxDisabled);
845 if (RT_SUCCESS(rc))
846 {
847 RTCCUINTREG uFlags = ASMIntDisableFlags();
848
849 for (;;)
850 {
851 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED_EXEC);
852 TMNotifyStartOfExecution(pVCpu);
853
854 rc = pVM->vmm.s.pfnR0ToRawMode(pVM);
855 pVCpu->vmm.s.iLastGZRc = rc;
856
857 TMNotifyEndOfExecution(pVCpu);
858 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED);
859
860 if (rc != VINF_VMM_CALL_TRACER)
861 break;
862 SUPR0TracerUmodProbeFire(pVM->pSession, &pVCpu->vmm.s.TracerCtx);
863 }
864
865 /* Re-enable VT-x if previously turned off. */
866 HMR0LeaveSwitcher(pVM, fVTxDisabled);
867
868 if ( rc == VINF_EM_RAW_INTERRUPT
869 || rc == VINF_EM_RAW_INTERRUPT_HYPER)
870 TRPMR0DispatchHostInterrupt(pVM);
871
872 ASMSetFlags(uFlags);
873
874#ifdef VBOX_WITH_STATISTICS
875 STAM_COUNTER_INC(&pVM->vmm.s.StatRunRC);
876 vmmR0RecordRC(pVM, pVCpu, rc);
877#endif
878 }
879 else
880 pVCpu->vmm.s.iLastGZRc = rc;
881 ASMAtomicWriteU32(&pVCpu->idHostCpu, NIL_RTCPUID);
882 RTThreadPreemptRestore(&PreemptState);
883 break;
884 }
885
886 /*
887 * Run guest code using the available hardware acceleration technology.
888 */
889 case VMMR0_DO_HM_RUN:
890 {
891 Assert(!VMMR0ThreadCtxHooksAreRegistered(pVCpu));
892 RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
893 RTThreadPreemptDisable(&PreemptState);
894
895 /* Update the VCPU <-> host CPU mapping before doing anything else. */
896 ASMAtomicWriteU32(&pVCpu->idHostCpu, RTMpCpuId());
897 if (pVM->vmm.s.fUsePeriodicPreemptionTimers)
898 GVMMR0SchedUpdatePeriodicPreemptionTimer(pVM, pVCpu->idHostCpu, TMCalcHostTimerFrequency(pVM, pVCpu));
899#ifdef LOG_ENABLED
900 if (pVCpu->idCpu > 0)
901 {
902 /* Lazy registration of ring 0 loggers. */
903 PVMMR0LOGGER pR0Logger = pVCpu->vmm.s.pR0LoggerR0;
904 if ( pR0Logger
905 && !pR0Logger->fRegistered)
906 {
907 RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
908 pR0Logger->fRegistered = true;
909 }
910 }
911#endif
912
913 int rc;
914 bool fPreemptRestored = false;
915 if (!HMR0SuspendPending())
916 {
917 /* Register thread-context hooks if required. */
918 if ( VMMR0ThreadCtxHooksAreCreated(pVCpu)
919 && !VMMR0ThreadCtxHooksAreRegistered(pVCpu))
920 {
921 rc = VMMR0ThreadCtxHooksRegister(pVCpu, vmmR0ThreadCtxCallback);
922 AssertRC(rc);
923 }
924
925 /* Enter HM context. */
926 rc = HMR0Enter(pVM, pVCpu);
927 if (RT_SUCCESS(rc))
928 {
929 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED_HM);
930
931 /* When preemption hooks are in place, enable preemption now that we're in HM context. */
932 if (VMMR0ThreadCtxHooksAreRegistered(pVCpu))
933 {
934 fPreemptRestored = true;
935 RTThreadPreemptRestore(&PreemptState);
936 }
937
938 /* Setup the longjmp machinery and execute guest code. */
939 rc = vmmR0CallRing3SetJmp(&pVCpu->vmm.s.CallRing3JmpBufR0, HMR0RunGuestCode, pVM, pVCpu);
940
941 /* Leave HM context. */
942 int rc2 = HMR0Leave(pVM, pVCpu);
943 AssertRC(rc2);
944
945 if (RT_UNLIKELY( VMCPU_GET_STATE(pVCpu) != VMCPUSTATE_STARTED_HM
946 && RT_SUCCESS_NP(rc) && rc != VINF_VMM_CALL_HOST ))
947 {
948 /* Manual assert as normal assertions are going to crash in this case. */
949 pVM->vmm.s.szRing0AssertMsg1[0] = '\0';
950 RTStrPrintf(pVM->vmm.s.szRing0AssertMsg2, sizeof(pVM->vmm.s.szRing0AssertMsg2),
951 "Got VMCPU state %d expected %d.\n", VMCPU_GET_STATE(pVCpu), VMCPUSTATE_STARTED_HM);
952 rc = VERR_VMM_WRONG_HM_VMCPU_STATE;
953 }
954 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED);
955 }
956 STAM_COUNTER_INC(&pVM->vmm.s.StatRunRC);
957 }
958 else
959 {
960 /* System is about to go into suspend mode; go back to ring 3. */
961 rc = VINF_EM_RAW_INTERRUPT;
962 }
963 pVCpu->vmm.s.iLastGZRc = rc;
964
965 /* Clear the VCPU <-> host CPU mapping as we've left HM context. */
966 ASMAtomicWriteU32(&pVCpu->idHostCpu, NIL_RTCPUID);
967
968 if (!fPreemptRestored)
969 RTThreadPreemptRestore(&PreemptState);
970
971#ifdef VBOX_WITH_STATISTICS
972 vmmR0RecordRC(pVM, pVCpu, rc);
973#endif
974 /* No special action required for external interrupts, just return. */
975 break;
976 }
977
978 /*
979 * For profiling.
980 */
981 case VMMR0_DO_NOP:
982 pVCpu->vmm.s.iLastGZRc = VINF_SUCCESS;
983 break;
984
985 /*
986 * Impossible.
987 */
988 default:
989 AssertMsgFailed(("%#x\n", enmOperation));
990 pVCpu->vmm.s.iLastGZRc = VERR_NOT_SUPPORTED;
991 break;
992 }
993}
994
995
996/**
997 * Validates a session or VM session argument.
998 *
999 * @returns true / false accordingly.
1000 * @param pVM Pointer to the VM.
1001 * @param pSession The session argument.
1002 */
1003DECLINLINE(bool) vmmR0IsValidSession(PVM pVM, PSUPDRVSESSION pClaimedSession, PSUPDRVSESSION pSession)
1004{
1005 /* This must be set! */
1006 if (!pSession)
1007 return false;
1008
1009 /* Only one out of the two. */
1010 if (pVM && pClaimedSession)
1011 return false;
1012 if (pVM)
1013 pClaimedSession = pVM->pSession;
1014 return pClaimedSession == pSession;
1015}
1016
1017
1018/**
1019 * VMMR0EntryEx worker function, either called directly or when ever possible
1020 * called thru a longjmp so we can exit safely on failure.
1021 *
1022 * @returns VBox status code.
1023 * @param pVM Pointer to the VM.
1024 * @param idCpu Virtual CPU ID argument. Must be NIL_VMCPUID if pVM
1025 * is NIL_RTR0PTR, and may be NIL_VMCPUID if it isn't
1026 * @param enmOperation Which operation to execute.
1027 * @param pReqHdr This points to a SUPVMMR0REQHDR packet. Optional.
1028 * The support driver validates this if it's present.
1029 * @param u64Arg Some simple constant argument.
1030 * @param pSession The session of the caller.
1031 * @remarks Assume called with interrupts _enabled_.
1032 */
1033static int vmmR0EntryExWorker(PVM pVM, VMCPUID idCpu, VMMR0OPERATION enmOperation, PSUPVMMR0REQHDR pReqHdr, uint64_t u64Arg, PSUPDRVSESSION pSession)
1034{
1035 /*
1036 * Common VM pointer validation.
1037 */
1038 if (pVM)
1039 {
1040 if (RT_UNLIKELY( !VALID_PTR(pVM)
1041 || ((uintptr_t)pVM & PAGE_OFFSET_MASK)))
1042 {
1043 SUPR0Printf("vmmR0EntryExWorker: Invalid pVM=%p! (op=%d)\n", pVM, enmOperation);
1044 return VERR_INVALID_POINTER;
1045 }
1046 if (RT_UNLIKELY( pVM->enmVMState < VMSTATE_CREATING
1047 || pVM->enmVMState > VMSTATE_TERMINATED
1048 || pVM->pVMR0 != pVM))
1049 {
1050 SUPR0Printf("vmmR0EntryExWorker: Invalid pVM=%p:{enmVMState=%d, .pVMR0=%p}! (op=%d)\n",
1051 pVM, pVM->enmVMState, pVM->pVMR0, enmOperation);
1052 return VERR_INVALID_POINTER;
1053 }
1054
1055 if (RT_UNLIKELY(idCpu >= pVM->cCpus && idCpu != NIL_VMCPUID))
1056 {
1057 SUPR0Printf("vmmR0EntryExWorker: Invalid idCpu (%u vs cCpus=%u)\n", idCpu, pVM->cCpus);
1058 return VERR_INVALID_PARAMETER;
1059 }
1060 }
1061 else if (RT_UNLIKELY(idCpu != NIL_VMCPUID))
1062 {
1063 SUPR0Printf("vmmR0EntryExWorker: Invalid idCpu=%u\n", idCpu);
1064 return VERR_INVALID_PARAMETER;
1065 }
1066
1067
1068 switch (enmOperation)
1069 {
1070 /*
1071 * GVM requests
1072 */
1073 case VMMR0_DO_GVMM_CREATE_VM:
1074 if (pVM || u64Arg || idCpu != NIL_VMCPUID)
1075 return VERR_INVALID_PARAMETER;
1076 return GVMMR0CreateVMReq((PGVMMCREATEVMREQ)pReqHdr);
1077
1078 case VMMR0_DO_GVMM_DESTROY_VM:
1079 if (pReqHdr || u64Arg)
1080 return VERR_INVALID_PARAMETER;
1081 return GVMMR0DestroyVM(pVM);
1082
1083 case VMMR0_DO_GVMM_REGISTER_VMCPU:
1084 {
1085 if (!pVM)
1086 return VERR_INVALID_PARAMETER;
1087 return GVMMR0RegisterVCpu(pVM, idCpu);
1088 }
1089
1090 case VMMR0_DO_GVMM_SCHED_HALT:
1091 if (pReqHdr)
1092 return VERR_INVALID_PARAMETER;
1093 return GVMMR0SchedHalt(pVM, idCpu, u64Arg);
1094
1095 case VMMR0_DO_GVMM_SCHED_WAKE_UP:
1096 if (pReqHdr || u64Arg)
1097 return VERR_INVALID_PARAMETER;
1098 return GVMMR0SchedWakeUp(pVM, idCpu);
1099
1100 case VMMR0_DO_GVMM_SCHED_POKE:
1101 if (pReqHdr || u64Arg)
1102 return VERR_INVALID_PARAMETER;
1103 return GVMMR0SchedPoke(pVM, idCpu);
1104
1105 case VMMR0_DO_GVMM_SCHED_WAKE_UP_AND_POKE_CPUS:
1106 if (u64Arg)
1107 return VERR_INVALID_PARAMETER;
1108 return GVMMR0SchedWakeUpAndPokeCpusReq(pVM, (PGVMMSCHEDWAKEUPANDPOKECPUSREQ)pReqHdr);
1109
1110 case VMMR0_DO_GVMM_SCHED_POLL:
1111 if (pReqHdr || u64Arg > 1)
1112 return VERR_INVALID_PARAMETER;
1113 return GVMMR0SchedPoll(pVM, idCpu, !!u64Arg);
1114
1115 case VMMR0_DO_GVMM_QUERY_STATISTICS:
1116 if (u64Arg)
1117 return VERR_INVALID_PARAMETER;
1118 return GVMMR0QueryStatisticsReq(pVM, (PGVMMQUERYSTATISTICSSREQ)pReqHdr);
1119
1120 case VMMR0_DO_GVMM_RESET_STATISTICS:
1121 if (u64Arg)
1122 return VERR_INVALID_PARAMETER;
1123 return GVMMR0ResetStatisticsReq(pVM, (PGVMMRESETSTATISTICSSREQ)pReqHdr);
1124
1125 /*
1126 * Initialize the R0 part of a VM instance.
1127 */
1128 case VMMR0_DO_VMMR0_INIT:
1129 return vmmR0InitVM(pVM, RT_LODWORD(u64Arg), RT_HIDWORD(u64Arg));
1130
1131 /*
1132 * Terminate the R0 part of a VM instance.
1133 */
1134 case VMMR0_DO_VMMR0_TERM:
1135 return VMMR0TermVM(pVM, NULL);
1136
1137 /*
1138 * Attempt to enable hm mode and check the current setting.
1139 */
1140 case VMMR0_DO_HM_ENABLE:
1141 return HMR0EnableAllCpus(pVM);
1142
1143 /*
1144 * Setup the hardware accelerated session.
1145 */
1146 case VMMR0_DO_HM_SETUP_VM:
1147 return HMR0SetupVM(pVM);
1148
1149 /*
1150 * Switch to RC to execute Hypervisor function.
1151 */
1152 case VMMR0_DO_CALL_HYPERVISOR:
1153 {
1154 int rc;
1155 bool fVTxDisabled;
1156
1157#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
1158 if (RT_UNLIKELY(!PGMGetHyperCR3(VMMGetCpu0(pVM))))
1159 return VERR_PGM_NO_CR3_SHADOW_ROOT;
1160#endif
1161
1162 RTCCUINTREG fFlags = ASMIntDisableFlags();
1163
1164#ifdef VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
1165 RTCPUID idHostCpu = RTMpCpuId();
1166 CPUMR0SetLApic(&pVM->aCpus[0], idHostCpu);
1167#endif
1168
1169 /* We might need to disable VT-x if the active switcher turns off paging. */
1170 rc = HMR0EnterSwitcher(pVM, pVM->vmm.s.enmSwitcher, &fVTxDisabled);
1171 if (RT_FAILURE(rc))
1172 return rc;
1173
1174 rc = pVM->vmm.s.pfnR0ToRawMode(pVM);
1175
1176 /* Re-enable VT-x if previously turned off. */
1177 HMR0LeaveSwitcher(pVM, fVTxDisabled);
1178
1179 /** @todo dispatch interrupts? */
1180 ASMSetFlags(fFlags);
1181 return rc;
1182 }
1183
1184 /*
1185 * PGM wrappers.
1186 */
1187 case VMMR0_DO_PGM_ALLOCATE_HANDY_PAGES:
1188 if (idCpu == NIL_VMCPUID)
1189 return VERR_INVALID_CPU_ID;
1190 return PGMR0PhysAllocateHandyPages(pVM, &pVM->aCpus[idCpu]);
1191
1192 case VMMR0_DO_PGM_FLUSH_HANDY_PAGES:
1193 if (idCpu == NIL_VMCPUID)
1194 return VERR_INVALID_CPU_ID;
1195 return PGMR0PhysFlushHandyPages(pVM, &pVM->aCpus[idCpu]);
1196
1197 case VMMR0_DO_PGM_ALLOCATE_LARGE_HANDY_PAGE:
1198 if (idCpu == NIL_VMCPUID)
1199 return VERR_INVALID_CPU_ID;
1200 return PGMR0PhysAllocateLargeHandyPage(pVM, &pVM->aCpus[idCpu]);
1201
1202 case VMMR0_DO_PGM_PHYS_SETUP_IOMMU:
1203 if (idCpu != 0)
1204 return VERR_INVALID_CPU_ID;
1205 return PGMR0PhysSetupIommu(pVM);
1206
1207 /*
1208 * GMM wrappers.
1209 */
1210 case VMMR0_DO_GMM_INITIAL_RESERVATION:
1211 if (u64Arg)
1212 return VERR_INVALID_PARAMETER;
1213 return GMMR0InitialReservationReq(pVM, idCpu, (PGMMINITIALRESERVATIONREQ)pReqHdr);
1214
1215 case VMMR0_DO_GMM_UPDATE_RESERVATION:
1216 if (u64Arg)
1217 return VERR_INVALID_PARAMETER;
1218 return GMMR0UpdateReservationReq(pVM, idCpu, (PGMMUPDATERESERVATIONREQ)pReqHdr);
1219
1220 case VMMR0_DO_GMM_ALLOCATE_PAGES:
1221 if (u64Arg)
1222 return VERR_INVALID_PARAMETER;
1223 return GMMR0AllocatePagesReq(pVM, idCpu, (PGMMALLOCATEPAGESREQ)pReqHdr);
1224
1225 case VMMR0_DO_GMM_FREE_PAGES:
1226 if (u64Arg)
1227 return VERR_INVALID_PARAMETER;
1228 return GMMR0FreePagesReq(pVM, idCpu, (PGMMFREEPAGESREQ)pReqHdr);
1229
1230 case VMMR0_DO_GMM_FREE_LARGE_PAGE:
1231 if (u64Arg)
1232 return VERR_INVALID_PARAMETER;
1233 return GMMR0FreeLargePageReq(pVM, idCpu, (PGMMFREELARGEPAGEREQ)pReqHdr);
1234
1235 case VMMR0_DO_GMM_QUERY_HYPERVISOR_MEM_STATS:
1236 if (u64Arg)
1237 return VERR_INVALID_PARAMETER;
1238 return GMMR0QueryHypervisorMemoryStatsReq(pVM, (PGMMMEMSTATSREQ)pReqHdr);
1239
1240 case VMMR0_DO_GMM_QUERY_MEM_STATS:
1241 if (idCpu == NIL_VMCPUID)
1242 return VERR_INVALID_CPU_ID;
1243 if (u64Arg)
1244 return VERR_INVALID_PARAMETER;
1245 return GMMR0QueryMemoryStatsReq(pVM, idCpu, (PGMMMEMSTATSREQ)pReqHdr);
1246
1247 case VMMR0_DO_GMM_BALLOONED_PAGES:
1248 if (u64Arg)
1249 return VERR_INVALID_PARAMETER;
1250 return GMMR0BalloonedPagesReq(pVM, idCpu, (PGMMBALLOONEDPAGESREQ)pReqHdr);
1251
1252 case VMMR0_DO_GMM_MAP_UNMAP_CHUNK:
1253 if (u64Arg)
1254 return VERR_INVALID_PARAMETER;
1255 return GMMR0MapUnmapChunkReq(pVM, (PGMMMAPUNMAPCHUNKREQ)pReqHdr);
1256
1257 case VMMR0_DO_GMM_SEED_CHUNK:
1258 if (pReqHdr)
1259 return VERR_INVALID_PARAMETER;
1260 return GMMR0SeedChunk(pVM, idCpu, (RTR3PTR)u64Arg);
1261
1262 case VMMR0_DO_GMM_REGISTER_SHARED_MODULE:
1263 if (idCpu == NIL_VMCPUID)
1264 return VERR_INVALID_CPU_ID;
1265 if (u64Arg)
1266 return VERR_INVALID_PARAMETER;
1267 return GMMR0RegisterSharedModuleReq(pVM, idCpu, (PGMMREGISTERSHAREDMODULEREQ)pReqHdr);
1268
1269 case VMMR0_DO_GMM_UNREGISTER_SHARED_MODULE:
1270 if (idCpu == NIL_VMCPUID)
1271 return VERR_INVALID_CPU_ID;
1272 if (u64Arg)
1273 return VERR_INVALID_PARAMETER;
1274 return GMMR0UnregisterSharedModuleReq(pVM, idCpu, (PGMMUNREGISTERSHAREDMODULEREQ)pReqHdr);
1275
1276 case VMMR0_DO_GMM_RESET_SHARED_MODULES:
1277 if (idCpu == NIL_VMCPUID)
1278 return VERR_INVALID_CPU_ID;
1279 if ( u64Arg
1280 || pReqHdr)
1281 return VERR_INVALID_PARAMETER;
1282 return GMMR0ResetSharedModules(pVM, idCpu);
1283
1284#ifdef VBOX_WITH_PAGE_SHARING
1285 case VMMR0_DO_GMM_CHECK_SHARED_MODULES:
1286 {
1287 if (idCpu == NIL_VMCPUID)
1288 return VERR_INVALID_CPU_ID;
1289 if ( u64Arg
1290 || pReqHdr)
1291 return VERR_INVALID_PARAMETER;
1292
1293 PVMCPU pVCpu = &pVM->aCpus[idCpu];
1294 Assert(pVCpu->hNativeThreadR0 == RTThreadNativeSelf());
1295
1296# ifdef DEBUG_sandervl
1297 /* Make sure that log flushes can jump back to ring-3; annoying to get an incomplete log (this is risky though as the code doesn't take this into account). */
1298 /* Todo: this can have bad side effects for unexpected jumps back to r3. */
1299 int rc = GMMR0CheckSharedModulesStart(pVM);
1300 if (rc == VINF_SUCCESS)
1301 {
1302 rc = vmmR0CallRing3SetJmp(&pVCpu->vmm.s.CallRing3JmpBufR0, GMMR0CheckSharedModules, pVM, pVCpu); /* this may resume code. */
1303 Assert( rc == VINF_SUCCESS
1304 || (rc == VINF_VMM_CALL_HOST && pVCpu->vmm.s.enmCallRing3Operation == VMMCALLRING3_VMM_LOGGER_FLUSH));
1305 GMMR0CheckSharedModulesEnd(pVM);
1306 }
1307# else
1308 int rc = GMMR0CheckSharedModules(pVM, pVCpu);
1309# endif
1310 return rc;
1311 }
1312#endif
1313
1314#if defined(VBOX_STRICT) && HC_ARCH_BITS == 64
1315 case VMMR0_DO_GMM_FIND_DUPLICATE_PAGE:
1316 if (u64Arg)
1317 return VERR_INVALID_PARAMETER;
1318 return GMMR0FindDuplicatePageReq(pVM, (PGMMFINDDUPLICATEPAGEREQ)pReqHdr);
1319#endif
1320
1321 case VMMR0_DO_GMM_QUERY_STATISTICS:
1322 if (u64Arg)
1323 return VERR_INVALID_PARAMETER;
1324 return GMMR0QueryStatisticsReq(pVM, (PGMMQUERYSTATISTICSSREQ)pReqHdr);
1325
1326 case VMMR0_DO_GMM_RESET_STATISTICS:
1327 if (u64Arg)
1328 return VERR_INVALID_PARAMETER;
1329 return GMMR0ResetStatisticsReq(pVM, (PGMMRESETSTATISTICSSREQ)pReqHdr);
1330
1331 /*
1332 * A quick GCFGM mock-up.
1333 */
1334 /** @todo GCFGM with proper access control, ring-3 management interface and all that. */
1335 case VMMR0_DO_GCFGM_SET_VALUE:
1336 case VMMR0_DO_GCFGM_QUERY_VALUE:
1337 {
1338 if (pVM || !pReqHdr || u64Arg || idCpu != NIL_VMCPUID)
1339 return VERR_INVALID_PARAMETER;
1340 PGCFGMVALUEREQ pReq = (PGCFGMVALUEREQ)pReqHdr;
1341 if (pReq->Hdr.cbReq != sizeof(*pReq))
1342 return VERR_INVALID_PARAMETER;
1343 int rc;
1344 if (enmOperation == VMMR0_DO_GCFGM_SET_VALUE)
1345 {
1346 rc = GVMMR0SetConfig(pReq->pSession, &pReq->szName[0], pReq->u64Value);
1347 //if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1348 // rc = GMMR0SetConfig(pReq->pSession, &pReq->szName[0], pReq->u64Value);
1349 }
1350 else
1351 {
1352 rc = GVMMR0QueryConfig(pReq->pSession, &pReq->szName[0], &pReq->u64Value);
1353 //if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1354 // rc = GMMR0QueryConfig(pReq->pSession, &pReq->szName[0], &pReq->u64Value);
1355 }
1356 return rc;
1357 }
1358
1359 /*
1360 * PDM Wrappers.
1361 */
1362 case VMMR0_DO_PDM_DRIVER_CALL_REQ_HANDLER:
1363 {
1364 if (!pVM || !pReqHdr || u64Arg || idCpu != NIL_VMCPUID)
1365 return VERR_INVALID_PARAMETER;
1366 return PDMR0DriverCallReqHandler(pVM, (PPDMDRIVERCALLREQHANDLERREQ)pReqHdr);
1367 }
1368
1369 case VMMR0_DO_PDM_DEVICE_CALL_REQ_HANDLER:
1370 {
1371 if (!pVM || !pReqHdr || u64Arg || idCpu != NIL_VMCPUID)
1372 return VERR_INVALID_PARAMETER;
1373 return PDMR0DeviceCallReqHandler(pVM, (PPDMDEVICECALLREQHANDLERREQ)pReqHdr);
1374 }
1375
1376 /*
1377 * Requests to the internal networking service.
1378 */
1379 case VMMR0_DO_INTNET_OPEN:
1380 {
1381 PINTNETOPENREQ pReq = (PINTNETOPENREQ)pReqHdr;
1382 if (u64Arg || !pReq || !vmmR0IsValidSession(pVM, pReq->pSession, pSession) || idCpu != NIL_VMCPUID)
1383 return VERR_INVALID_PARAMETER;
1384 return IntNetR0OpenReq(pSession, pReq);
1385 }
1386
1387 case VMMR0_DO_INTNET_IF_CLOSE:
1388 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFCLOSEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1389 return VERR_INVALID_PARAMETER;
1390 return IntNetR0IfCloseReq(pSession, (PINTNETIFCLOSEREQ)pReqHdr);
1391
1392 case VMMR0_DO_INTNET_IF_GET_BUFFER_PTRS:
1393 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFGETBUFFERPTRSREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1394 return VERR_INVALID_PARAMETER;
1395 return IntNetR0IfGetBufferPtrsReq(pSession, (PINTNETIFGETBUFFERPTRSREQ)pReqHdr);
1396
1397 case VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE:
1398 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETPROMISCUOUSMODEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1399 return VERR_INVALID_PARAMETER;
1400 return IntNetR0IfSetPromiscuousModeReq(pSession, (PINTNETIFSETPROMISCUOUSMODEREQ)pReqHdr);
1401
1402 case VMMR0_DO_INTNET_IF_SET_MAC_ADDRESS:
1403 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETMACADDRESSREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1404 return VERR_INVALID_PARAMETER;
1405 return IntNetR0IfSetMacAddressReq(pSession, (PINTNETIFSETMACADDRESSREQ)pReqHdr);
1406
1407 case VMMR0_DO_INTNET_IF_SET_ACTIVE:
1408 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETACTIVEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1409 return VERR_INVALID_PARAMETER;
1410 return IntNetR0IfSetActiveReq(pSession, (PINTNETIFSETACTIVEREQ)pReqHdr);
1411
1412 case VMMR0_DO_INTNET_IF_SEND:
1413 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSENDREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1414 return VERR_INVALID_PARAMETER;
1415 return IntNetR0IfSendReq(pSession, (PINTNETIFSENDREQ)pReqHdr);
1416
1417 case VMMR0_DO_INTNET_IF_WAIT:
1418 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFWAITREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1419 return VERR_INVALID_PARAMETER;
1420 return IntNetR0IfWaitReq(pSession, (PINTNETIFWAITREQ)pReqHdr);
1421
1422 case VMMR0_DO_INTNET_IF_ABORT_WAIT:
1423 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFWAITREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1424 return VERR_INVALID_PARAMETER;
1425 return IntNetR0IfAbortWaitReq(pSession, (PINTNETIFABORTWAITREQ)pReqHdr);
1426
1427#ifdef VBOX_WITH_PCI_PASSTHROUGH
1428 /*
1429 * Requests to host PCI driver service.
1430 */
1431 case VMMR0_DO_PCIRAW_REQ:
1432 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PPCIRAWSENDREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1433 return VERR_INVALID_PARAMETER;
1434 return PciRawR0ProcessReq(pSession, pVM, (PPCIRAWSENDREQ)pReqHdr);
1435#endif
1436 /*
1437 * For profiling.
1438 */
1439 case VMMR0_DO_NOP:
1440 case VMMR0_DO_SLOW_NOP:
1441 return VINF_SUCCESS;
1442
1443 /*
1444 * For testing Ring-0 APIs invoked in this environment.
1445 */
1446 case VMMR0_DO_TESTS:
1447 /** @todo make new test */
1448 return VINF_SUCCESS;
1449
1450
1451#if HC_ARCH_BITS == 32 && defined(VBOX_WITH_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
1452 case VMMR0_DO_TEST_SWITCHER3264:
1453 if (idCpu == NIL_VMCPUID)
1454 return VERR_INVALID_CPU_ID;
1455 return HMR0TestSwitcher3264(pVM);
1456#endif
1457 default:
1458 /*
1459 * We're returning VERR_NOT_SUPPORT here so we've got something else
1460 * than -1 which the interrupt gate glue code might return.
1461 */
1462 Log(("operation %#x is not supported\n", enmOperation));
1463 return VERR_NOT_SUPPORTED;
1464 }
1465}
1466
1467
1468/**
1469 * Argument for vmmR0EntryExWrapper containing the arguments for VMMR0EntryEx.
1470 */
1471typedef struct VMMR0ENTRYEXARGS
1472{
1473 PVM pVM;
1474 VMCPUID idCpu;
1475 VMMR0OPERATION enmOperation;
1476 PSUPVMMR0REQHDR pReq;
1477 uint64_t u64Arg;
1478 PSUPDRVSESSION pSession;
1479} VMMR0ENTRYEXARGS;
1480/** Pointer to a vmmR0EntryExWrapper argument package. */
1481typedef VMMR0ENTRYEXARGS *PVMMR0ENTRYEXARGS;
1482
1483/**
1484 * This is just a longjmp wrapper function for VMMR0EntryEx calls.
1485 *
1486 * @returns VBox status code.
1487 * @param pvArgs The argument package
1488 */
1489static DECLCALLBACK(int) vmmR0EntryExWrapper(void *pvArgs)
1490{
1491 return vmmR0EntryExWorker(((PVMMR0ENTRYEXARGS)pvArgs)->pVM,
1492 ((PVMMR0ENTRYEXARGS)pvArgs)->idCpu,
1493 ((PVMMR0ENTRYEXARGS)pvArgs)->enmOperation,
1494 ((PVMMR0ENTRYEXARGS)pvArgs)->pReq,
1495 ((PVMMR0ENTRYEXARGS)pvArgs)->u64Arg,
1496 ((PVMMR0ENTRYEXARGS)pvArgs)->pSession);
1497}
1498
1499
1500/**
1501 * The Ring 0 entry point, called by the support library (SUP).
1502 *
1503 * @returns VBox status code.
1504 * @param pVM Pointer to the VM.
1505 * @param idCpu Virtual CPU ID argument. Must be NIL_VMCPUID if pVM
1506 * is NIL_RTR0PTR, and may be NIL_VMCPUID if it isn't
1507 * @param enmOperation Which operation to execute.
1508 * @param pReq Pointer to the SUPVMMR0REQHDR packet. Optional.
1509 * @param u64Arg Some simple constant argument.
1510 * @param pSession The session of the caller.
1511 * @remarks Assume called with interrupts _enabled_.
1512 */
1513VMMR0DECL(int) VMMR0EntryEx(PVM pVM, VMCPUID idCpu, VMMR0OPERATION enmOperation, PSUPVMMR0REQHDR pReq, uint64_t u64Arg, PSUPDRVSESSION pSession)
1514{
1515 /*
1516 * Requests that should only happen on the EMT thread will be
1517 * wrapped in a setjmp so we can assert without causing trouble.
1518 */
1519 if ( VALID_PTR(pVM)
1520 && pVM->pVMR0
1521 && idCpu < pVM->cCpus)
1522 {
1523 switch (enmOperation)
1524 {
1525 /* These might/will be called before VMMR3Init. */
1526 case VMMR0_DO_GMM_INITIAL_RESERVATION:
1527 case VMMR0_DO_GMM_UPDATE_RESERVATION:
1528 case VMMR0_DO_GMM_ALLOCATE_PAGES:
1529 case VMMR0_DO_GMM_FREE_PAGES:
1530 case VMMR0_DO_GMM_BALLOONED_PAGES:
1531 /* On the mac we might not have a valid jmp buf, so check these as well. */
1532 case VMMR0_DO_VMMR0_INIT:
1533 case VMMR0_DO_VMMR0_TERM:
1534 {
1535 PVMCPU pVCpu = &pVM->aCpus[idCpu];
1536
1537 if (!pVCpu->vmm.s.CallRing3JmpBufR0.pvSavedStack)
1538 break;
1539
1540 /** @todo validate this EMT claim... GVM knows. */
1541 VMMR0ENTRYEXARGS Args;
1542 Args.pVM = pVM;
1543 Args.idCpu = idCpu;
1544 Args.enmOperation = enmOperation;
1545 Args.pReq = pReq;
1546 Args.u64Arg = u64Arg;
1547 Args.pSession = pSession;
1548 return vmmR0CallRing3SetJmpEx(&pVCpu->vmm.s.CallRing3JmpBufR0, vmmR0EntryExWrapper, &Args);
1549 }
1550
1551 default:
1552 break;
1553 }
1554 }
1555 return vmmR0EntryExWorker(pVM, idCpu, enmOperation, pReq, u64Arg, pSession);
1556}
1557
1558
1559/**
1560 * Checks whether we've armed the ring-0 long jump machinery.
1561 *
1562 * @returns @c true / @c false
1563 * @param pVCpu Pointer to the VMCPU.
1564 * @thread EMT
1565 * @sa VMMIsLongJumpArmed
1566 */
1567VMMR0_INT_DECL(bool) VMMR0IsLongJumpArmed(PVMCPU pVCpu)
1568{
1569#ifdef RT_ARCH_X86
1570 return pVCpu->vmm.s.CallRing3JmpBufR0.eip
1571 && !pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call;
1572#else
1573 return pVCpu->vmm.s.CallRing3JmpBufR0.rip
1574 && !pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call;
1575#endif
1576}
1577
1578
1579/**
1580 * Checks whether we've done a ring-3 long jump.
1581 *
1582 * @returns @c true / @c false
1583 * @param pVCpu Pointer to the VMCPU.
1584 * @thread EMT
1585 */
1586VMMR0_INT_DECL(bool) VMMR0IsInRing3LongJump(PVMCPU pVCpu)
1587{
1588 return pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call;
1589}
1590
1591
1592/**
1593 * Internal R0 logger worker: Flush logger.
1594 *
1595 * @param pLogger The logger instance to flush.
1596 * @remark This function must be exported!
1597 */
1598VMMR0DECL(void) vmmR0LoggerFlush(PRTLOGGER pLogger)
1599{
1600#ifdef LOG_ENABLED
1601 /*
1602 * Convert the pLogger into a VM handle and 'call' back to Ring-3.
1603 * (This is a bit paranoid code.)
1604 */
1605 PVMMR0LOGGER pR0Logger = (PVMMR0LOGGER)((uintptr_t)pLogger - RT_OFFSETOF(VMMR0LOGGER, Logger));
1606 if ( !VALID_PTR(pR0Logger)
1607 || !VALID_PTR(pR0Logger + 1)
1608 || pLogger->u32Magic != RTLOGGER_MAGIC)
1609 {
1610# ifdef DEBUG
1611 SUPR0Printf("vmmR0LoggerFlush: pLogger=%p!\n", pLogger);
1612# endif
1613 return;
1614 }
1615 if (pR0Logger->fFlushingDisabled)
1616 return; /* quietly */
1617
1618 PVM pVM = pR0Logger->pVM;
1619 if ( !VALID_PTR(pVM)
1620 || pVM->pVMR0 != pVM)
1621 {
1622# ifdef DEBUG
1623 SUPR0Printf("vmmR0LoggerFlush: pVM=%p! pVMR0=%p! pLogger=%p\n", pVM, pVM->pVMR0, pLogger);
1624# endif
1625 return;
1626 }
1627
1628 PVMCPU pVCpu = VMMGetCpu(pVM);
1629 if (pVCpu)
1630 {
1631 /*
1632 * Check that the jump buffer is armed.
1633 */
1634# ifdef RT_ARCH_X86
1635 if ( !pVCpu->vmm.s.CallRing3JmpBufR0.eip
1636 || pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call)
1637# else
1638 if ( !pVCpu->vmm.s.CallRing3JmpBufR0.rip
1639 || pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call)
1640# endif
1641 {
1642# ifdef DEBUG
1643 SUPR0Printf("vmmR0LoggerFlush: Jump buffer isn't armed!\n");
1644# endif
1645 return;
1646 }
1647 VMMRZCallRing3(pVM, pVCpu, VMMCALLRING3_VMM_LOGGER_FLUSH, 0);
1648 }
1649# ifdef DEBUG
1650 else
1651 SUPR0Printf("vmmR0LoggerFlush: invalid VCPU context!\n");
1652# endif
1653#endif
1654}
1655
1656/**
1657 * Internal R0 logger worker: Custom prefix.
1658 *
1659 * @returns Number of chars written.
1660 *
1661 * @param pLogger The logger instance.
1662 * @param pchBuf The output buffer.
1663 * @param cchBuf The size of the buffer.
1664 * @param pvUser User argument (ignored).
1665 */
1666VMMR0DECL(size_t) vmmR0LoggerPrefix(PRTLOGGER pLogger, char *pchBuf, size_t cchBuf, void *pvUser)
1667{
1668 NOREF(pvUser);
1669#ifdef LOG_ENABLED
1670 PVMMR0LOGGER pR0Logger = (PVMMR0LOGGER)((uintptr_t)pLogger - RT_OFFSETOF(VMMR0LOGGER, Logger));
1671 if ( !VALID_PTR(pR0Logger)
1672 || !VALID_PTR(pR0Logger + 1)
1673 || pLogger->u32Magic != RTLOGGER_MAGIC
1674 || cchBuf < 2)
1675 return 0;
1676
1677 static const char s_szHex[17] = "0123456789abcdef";
1678 VMCPUID const idCpu = pR0Logger->idCpu;
1679 pchBuf[1] = s_szHex[ idCpu & 15];
1680 pchBuf[0] = s_szHex[(idCpu >> 4) & 15];
1681
1682 return 2;
1683#else
1684 return 0;
1685#endif
1686}
1687
1688#ifdef LOG_ENABLED
1689
1690/**
1691 * Disables flushing of the ring-0 debug log.
1692 *
1693 * @param pVCpu Pointer to the VMCPU.
1694 */
1695VMMR0DECL(void) VMMR0LogFlushDisable(PVMCPU pVCpu)
1696{
1697 if (pVCpu->vmm.s.pR0LoggerR0)
1698 pVCpu->vmm.s.pR0LoggerR0->fFlushingDisabled = true;
1699}
1700
1701
1702/**
1703 * Enables flushing of the ring-0 debug log.
1704 *
1705 * @param pVCpu Pointer to the VMCPU.
1706 */
1707VMMR0DECL(void) VMMR0LogFlushEnable(PVMCPU pVCpu)
1708{
1709 if (pVCpu->vmm.s.pR0LoggerR0)
1710 pVCpu->vmm.s.pR0LoggerR0->fFlushingDisabled = false;
1711}
1712
1713
1714/**
1715 * Checks if log flushing is disabled or not.
1716 *
1717 * @param pVCpu Pointer to the VMCPU.
1718 */
1719VMMR0DECL(bool) VMMR0IsLogFlushDisabled(PVMCPU pVCpu)
1720{
1721 if (pVCpu->vmm.s.pR0LoggerR0)
1722 return pVCpu->vmm.s.pR0LoggerR0->fFlushingDisabled;
1723 return true;
1724}
1725#endif /* LOG_ENABLED */
1726
1727/**
1728 * Jump back to ring-3 if we're the EMT and the longjmp is armed.
1729 *
1730 * @returns true if the breakpoint should be hit, false if it should be ignored.
1731 */
1732DECLEXPORT(bool) RTCALL RTAssertShouldPanic(void)
1733{
1734#if 0
1735 return true;
1736#else
1737 PVM pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
1738 if (pVM)
1739 {
1740 PVMCPU pVCpu = VMMGetCpu(pVM);
1741
1742 if (pVCpu)
1743 {
1744#ifdef RT_ARCH_X86
1745 if ( pVCpu->vmm.s.CallRing3JmpBufR0.eip
1746 && !pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call)
1747#else
1748 if ( pVCpu->vmm.s.CallRing3JmpBufR0.rip
1749 && !pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call)
1750#endif
1751 {
1752 int rc = VMMRZCallRing3(pVM, pVCpu, VMMCALLRING3_VM_R0_ASSERTION, 0);
1753 return RT_FAILURE_NP(rc);
1754 }
1755 }
1756 }
1757#ifdef RT_OS_LINUX
1758 return true;
1759#else
1760 return false;
1761#endif
1762#endif
1763}
1764
1765
1766/**
1767 * Override this so we can push it up to ring-3.
1768 *
1769 * @param pszExpr Expression. Can be NULL.
1770 * @param uLine Location line number.
1771 * @param pszFile Location file name.
1772 * @param pszFunction Location function name.
1773 */
1774DECLEXPORT(void) RTCALL RTAssertMsg1Weak(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
1775{
1776 /*
1777 * To the log.
1778 */
1779 LogAlways(("\n!!R0-Assertion Failed!!\n"
1780 "Expression: %s\n"
1781 "Location : %s(%d) %s\n",
1782 pszExpr, pszFile, uLine, pszFunction));
1783
1784 /*
1785 * To the global VMM buffer.
1786 */
1787 PVM pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
1788 if (pVM)
1789 RTStrPrintf(pVM->vmm.s.szRing0AssertMsg1, sizeof(pVM->vmm.s.szRing0AssertMsg1),
1790 "\n!!R0-Assertion Failed!!\n"
1791 "Expression: %s\n"
1792 "Location : %s(%d) %s\n",
1793 pszExpr, pszFile, uLine, pszFunction);
1794
1795 /*
1796 * Continue the normal way.
1797 */
1798 RTAssertMsg1(pszExpr, uLine, pszFile, pszFunction);
1799}
1800
1801
1802/**
1803 * Callback for RTLogFormatV which writes to the ring-3 log port.
1804 * See PFNLOGOUTPUT() for details.
1805 */
1806static DECLCALLBACK(size_t) rtLogOutput(void *pv, const char *pachChars, size_t cbChars)
1807{
1808 for (size_t i = 0; i < cbChars; i++)
1809 LogAlways(("%c", pachChars[i]));
1810
1811 NOREF(pv);
1812 return cbChars;
1813}
1814
1815
1816/**
1817 * Override this so we can push it up to ring-3.
1818 *
1819 * @param pszFormat The format string.
1820 * @param va Arguments.
1821 */
1822DECLEXPORT(void) RTCALL RTAssertMsg2WeakV(const char *pszFormat, va_list va)
1823{
1824 va_list vaCopy;
1825
1826 /*
1827 * Push the message to the loggers.
1828 */
1829 PRTLOGGER pLog = RTLogGetDefaultInstance(); /* Don't initialize it here... */
1830 if (pLog)
1831 {
1832 va_copy(vaCopy, va);
1833 RTLogFormatV(rtLogOutput, pLog, pszFormat, vaCopy);
1834 va_end(vaCopy);
1835 }
1836 pLog = RTLogRelDefaultInstance();
1837 if (pLog)
1838 {
1839 va_copy(vaCopy, va);
1840 RTLogFormatV(rtLogOutput, pLog, pszFormat, vaCopy);
1841 va_end(vaCopy);
1842 }
1843
1844 /*
1845 * Push it to the global VMM buffer.
1846 */
1847 PVM pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
1848 if (pVM)
1849 {
1850 va_copy(vaCopy, va);
1851 RTStrPrintfV(pVM->vmm.s.szRing0AssertMsg2, sizeof(pVM->vmm.s.szRing0AssertMsg2), pszFormat, vaCopy);
1852 va_end(vaCopy);
1853 }
1854
1855 /*
1856 * Continue the normal way.
1857 */
1858 RTAssertMsg2V(pszFormat, va);
1859}
1860
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