VirtualBox

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

Last change on this file since 53349 was 51643, checked in by vboxsync, 11 years ago

VMM/GIM: More bits for Hyper-V implementation.

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