VirtualBox

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

Last change on this file since 55300 was 54720, checked in by vboxsync, 10 years ago

VMM: Some more internal decls.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 70.5 KB
Line 
1/* $Id: VMMR0.cpp 54720 2015-03-11 16:27:02Z 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 */
419VMMR0_INT_DECL(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 */
456VMMR0_INT_DECL(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 */
480VMMR0_INT_DECL(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 */
495VMMR0_INT_DECL(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 * @param pVCpu Pointer to the VMCPU.
506 *
507 * @thread EMT(pVCpu)
508 */
509VMMR0_INT_DECL(void) VMMR0ThreadCtxHooksDeregister(PVMCPU pVCpu)
510{
511 /* Clear the VCPU <-> host CPU mapping as we've left HM context. See @bugref{7726} comment #19. */
512 ASMAtomicWriteU32(&pVCpu->idHostCpu, NIL_RTCPUID);
513
514 if (pVCpu->vmm.s.hR0ThreadCtx != NIL_RTTHREADCTX)
515 {
516 Assert(!RTThreadPreemptIsEnabled(NIL_RTTHREAD));
517 int rc = RTThreadCtxHooksDeregister(pVCpu->vmm.s.hR0ThreadCtx);
518 AssertRC(rc);
519 }
520}
521
522
523/**
524 * Whether thread-context hooks are created (implying they're supported) on this
525 * platform.
526 *
527 * @returns true if the hooks are created, false otherwise.
528 * @param pVCpu Pointer to the VMCPU.
529 */
530VMMR0_INT_DECL(bool) VMMR0ThreadCtxHooksAreCreated(PVMCPU pVCpu)
531{
532 return pVCpu->vmm.s.hR0ThreadCtx != NIL_RTTHREADCTX;
533}
534
535
536/**
537 * Whether thread-context hooks are registered for this VCPU.
538 *
539 * @returns true if registered, false otherwise.
540 * @param pVCpu Pointer to the VMCPU.
541 */
542VMMR0_INT_DECL(bool) VMMR0ThreadCtxHooksAreRegistered(PVMCPU pVCpu)
543{
544 return RTThreadCtxHooksAreRegistered(pVCpu->vmm.s.hR0ThreadCtx);
545}
546
547
548/**
549 * VMM ring-0 thread-context callback.
550 *
551 * This does common HM state updating and calls the HM-specific thread-context
552 * callback.
553 *
554 * @param enmEvent The thread-context event.
555 * @param pvUser Opaque pointer to the VMCPU.
556 *
557 * @thread EMT(pvUser)
558 */
559static DECLCALLBACK(void) vmmR0ThreadCtxCallback(RTTHREADCTXEVENT enmEvent, void *pvUser)
560{
561 PVMCPU pVCpu = (PVMCPU)pvUser;
562
563 switch (enmEvent)
564 {
565 case RTTHREADCTXEVENT_RESUMED:
566 {
567 /** @todo Linux may call us with preemption enabled (really!) but technically we
568 * cannot get preempted here, otherwise we end up in an infinite recursion
569 * scenario (i.e. preempted in resume hook -> preempt hook -> resume hook... ad
570 * infinitum). Let's just disable preemption for now...
571 */
572 HM_DISABLE_PREEMPT_IF_NEEDED();
573
574 /* We need to update the VCPU <-> host CPU mapping. */
575 RTCPUID idHostCpu;
576 uint32_t iHostCpuSet = RTMpCurSetIndexAndId(&idHostCpu);
577 pVCpu->iHostCpuSet = iHostCpuSet;
578 ASMAtomicWriteU32(&pVCpu->idHostCpu, idHostCpu);
579
580 /* In the very unlikely event that the GIP delta for the CPU we're
581 rescheduled needs calculating, try force a return to ring-3.
582 We unfortunately cannot do the measurements right here. */
583 if (RT_UNLIKELY(SUPIsTscDeltaAvailableForCpuSetIndex(iHostCpuSet)))
584 VMCPU_FF_SET(pVCpu, VMCPU_FF_TO_R3);
585
586 /* Invoke the HM-specific thread-context callback. */
587 HMR0ThreadCtxCallback(enmEvent, pvUser);
588
589 /* Restore preemption. */
590 HM_RESTORE_PREEMPT_IF_NEEDED();
591 break;
592 }
593
594 case RTTHREADCTXEVENT_PREEMPTING:
595 {
596 /* Invoke the HM-specific thread-context callback. */
597 HMR0ThreadCtxCallback(enmEvent, pvUser);
598
599 /*
600 * Sigh. See VMMGetCpu() used by VMCPU_ASSERT_EMT(). We cannot let several VCPUs
601 * have the same host CPU associated with it.
602 */
603 pVCpu->iHostCpuSet = UINT32_MAX;
604 ASMAtomicWriteU32(&pVCpu->idHostCpu, NIL_RTCPUID);
605 break;
606 }
607
608 default:
609 /* Invoke the HM-specific thread-context callback. */
610 HMR0ThreadCtxCallback(enmEvent, pvUser);
611 break;
612 }
613}
614
615
616#ifdef VBOX_WITH_STATISTICS
617/**
618 * Record return code statistics
619 * @param pVM Pointer to the VM.
620 * @param pVCpu Pointer to the VMCPU.
621 * @param rc The status code.
622 */
623static void vmmR0RecordRC(PVM pVM, PVMCPU pVCpu, int rc)
624{
625 /*
626 * Collect statistics.
627 */
628 switch (rc)
629 {
630 case VINF_SUCCESS:
631 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetNormal);
632 break;
633 case VINF_EM_RAW_INTERRUPT:
634 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetInterrupt);
635 break;
636 case VINF_EM_RAW_INTERRUPT_HYPER:
637 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetInterruptHyper);
638 break;
639 case VINF_EM_RAW_GUEST_TRAP:
640 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetGuestTrap);
641 break;
642 case VINF_EM_RAW_RING_SWITCH:
643 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetRingSwitch);
644 break;
645 case VINF_EM_RAW_RING_SWITCH_INT:
646 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetRingSwitchInt);
647 break;
648 case VINF_EM_RAW_STALE_SELECTOR:
649 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetStaleSelector);
650 break;
651 case VINF_EM_RAW_IRET_TRAP:
652 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIRETTrap);
653 break;
654 case VINF_IOM_R3_IOPORT_READ:
655 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIORead);
656 break;
657 case VINF_IOM_R3_IOPORT_WRITE:
658 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIOWrite);
659 break;
660 case VINF_IOM_R3_MMIO_READ:
661 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIORead);
662 break;
663 case VINF_IOM_R3_MMIO_WRITE:
664 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOWrite);
665 break;
666 case VINF_IOM_R3_MMIO_READ_WRITE:
667 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOReadWrite);
668 break;
669 case VINF_PATM_HC_MMIO_PATCH_READ:
670 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOPatchRead);
671 break;
672 case VINF_PATM_HC_MMIO_PATCH_WRITE:
673 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOPatchWrite);
674 break;
675 case VINF_CPUM_R3_MSR_READ:
676 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMSRRead);
677 break;
678 case VINF_CPUM_R3_MSR_WRITE:
679 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMSRWrite);
680 break;
681 case VINF_EM_RAW_EMULATE_INSTR:
682 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetEmulate);
683 break;
684 case VINF_EM_RAW_EMULATE_IO_BLOCK:
685 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIOBlockEmulate);
686 break;
687 case VINF_PATCH_EMULATE_INSTR:
688 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchEmulate);
689 break;
690 case VINF_EM_RAW_EMULATE_INSTR_LDT_FAULT:
691 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetLDTFault);
692 break;
693 case VINF_EM_RAW_EMULATE_INSTR_GDT_FAULT:
694 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetGDTFault);
695 break;
696 case VINF_EM_RAW_EMULATE_INSTR_IDT_FAULT:
697 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIDTFault);
698 break;
699 case VINF_EM_RAW_EMULATE_INSTR_TSS_FAULT:
700 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetTSSFault);
701 break;
702 case VINF_EM_RAW_EMULATE_INSTR_PD_FAULT:
703 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPDFault);
704 break;
705 case VINF_CSAM_PENDING_ACTION:
706 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetCSAMTask);
707 break;
708 case VINF_PGM_SYNC_CR3:
709 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetSyncCR3);
710 break;
711 case VINF_PATM_PATCH_INT3:
712 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchInt3);
713 break;
714 case VINF_PATM_PATCH_TRAP_PF:
715 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchPF);
716 break;
717 case VINF_PATM_PATCH_TRAP_GP:
718 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchGP);
719 break;
720 case VINF_PATM_PENDING_IRQ_AFTER_IRET:
721 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchIretIRQ);
722 break;
723 case VINF_EM_RESCHEDULE_REM:
724 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetRescheduleREM);
725 break;
726 case VINF_EM_RAW_TO_R3:
727 if (VM_FF_IS_PENDING(pVM, VM_FF_TM_VIRTUAL_SYNC))
728 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3TMVirt);
729 else if (VM_FF_IS_PENDING(pVM, VM_FF_PGM_NEED_HANDY_PAGES))
730 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3HandyPages);
731 else if (VM_FF_IS_PENDING(pVM, VM_FF_PDM_QUEUES))
732 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3PDMQueues);
733 else if (VM_FF_IS_PENDING(pVM, VM_FF_EMT_RENDEZVOUS))
734 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3Rendezvous);
735 else if (VM_FF_IS_PENDING(pVM, VM_FF_PDM_DMA))
736 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3DMA);
737 else if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_TIMER))
738 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3Timer);
739 else if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_PDM_CRITSECT))
740 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3CritSect);
741 else if (VMCPU_FF_IS_PENDING(pVCpu, VMCPU_FF_TO_R3))
742 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3);
743 else
744 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3Unknown);
745 break;
746
747 case VINF_EM_RAW_TIMER_PENDING:
748 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetTimerPending);
749 break;
750 case VINF_EM_RAW_INTERRUPT_PENDING:
751 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetInterruptPending);
752 break;
753 case VINF_VMM_CALL_HOST:
754 switch (pVCpu->vmm.s.enmCallRing3Operation)
755 {
756 case VMMCALLRING3_PDM_CRIT_SECT_ENTER:
757 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPDMCritSectEnter);
758 break;
759 case VMMCALLRING3_PDM_LOCK:
760 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPDMLock);
761 break;
762 case VMMCALLRING3_PGM_POOL_GROW:
763 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMPoolGrow);
764 break;
765 case VMMCALLRING3_PGM_LOCK:
766 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMLock);
767 break;
768 case VMMCALLRING3_PGM_MAP_CHUNK:
769 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMMapChunk);
770 break;
771 case VMMCALLRING3_PGM_ALLOCATE_HANDY_PAGES:
772 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMAllocHandy);
773 break;
774 case VMMCALLRING3_REM_REPLAY_HANDLER_NOTIFICATIONS:
775 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallRemReplay);
776 break;
777 case VMMCALLRING3_VMM_LOGGER_FLUSH:
778 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallLogFlush);
779 break;
780 case VMMCALLRING3_VM_SET_ERROR:
781 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallVMSetError);
782 break;
783 case VMMCALLRING3_VM_SET_RUNTIME_ERROR:
784 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallVMSetRuntimeError);
785 break;
786 case VMMCALLRING3_VM_R0_ASSERTION:
787 default:
788 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetCallRing3);
789 break;
790 }
791 break;
792 case VINF_PATM_DUPLICATE_FUNCTION:
793 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPATMDuplicateFn);
794 break;
795 case VINF_PGM_CHANGE_MODE:
796 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPGMChangeMode);
797 break;
798 case VINF_PGM_POOL_FLUSH_PENDING:
799 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPGMFlushPending);
800 break;
801 case VINF_EM_PENDING_REQUEST:
802 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPendingRequest);
803 break;
804 case VINF_EM_HM_PATCH_TPR_INSTR:
805 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchTPR);
806 break;
807 default:
808 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMisc);
809 break;
810 }
811}
812#endif /* VBOX_WITH_STATISTICS */
813
814
815/**
816 * Unused ring-0 entry point that used to be called from the interrupt gate.
817 *
818 * Will be removed one of the next times we do a major SUPDrv version bump.
819 *
820 * @returns VBox status code.
821 * @param pVM Pointer to the VM.
822 * @param enmOperation Which operation to execute.
823 * @param pvArg Argument to the operation.
824 * @remarks Assume called with interrupts disabled.
825 */
826VMMR0DECL(int) VMMR0EntryInt(PVM pVM, VMMR0OPERATION enmOperation, void *pvArg)
827{
828 /*
829 * We're returning VERR_NOT_SUPPORT here so we've got something else
830 * than -1 which the interrupt gate glue code might return.
831 */
832 Log(("operation %#x is not supported\n", enmOperation));
833 NOREF(enmOperation); NOREF(pvArg); NOREF(pVM);
834 return VERR_NOT_SUPPORTED;
835}
836
837
838/**
839 * The Ring 0 entry point, called by the fast-ioctl path.
840 *
841 * @param pVM Pointer to the VM.
842 * The return code is stored in pVM->vmm.s.iLastGZRc.
843 * @param idCpu The Virtual CPU ID of the calling EMT.
844 * @param enmOperation Which operation to execute.
845 * @remarks Assume called with interrupts _enabled_.
846 */
847VMMR0DECL(void) VMMR0EntryFast(PVM pVM, VMCPUID idCpu, VMMR0OPERATION enmOperation)
848{
849 /*
850 * Validation.
851 */
852 if (RT_UNLIKELY(idCpu >= pVM->cCpus))
853 return;
854 PVMCPU pVCpu = &pVM->aCpus[idCpu];
855 if (RT_UNLIKELY(pVCpu->hNativeThreadR0 != RTThreadNativeSelf()))
856 return;
857
858 /*
859 * Perform requested operation.
860 */
861 switch (enmOperation)
862 {
863 /*
864 * Switch to GC and run guest raw mode code.
865 * Disable interrupts before doing the world switch.
866 */
867 case VMMR0_DO_RAW_RUN:
868 {
869#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
870 /* Some safety precautions first. */
871 if (RT_UNLIKELY(!PGMGetHyperCR3(pVCpu)))
872 {
873 pVCpu->vmm.s.iLastGZRc = VERR_PGM_NO_CR3_SHADOW_ROOT;
874 break;
875 }
876#endif
877
878 /*
879 * Disable preemption.
880 */
881 RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
882 RTThreadPreemptDisable(&PreemptState);
883
884 /*
885 * Get the host CPU identifiers, make sure they are valid and that
886 * we've got a TSC delta for the CPU.
887 */
888 RTCPUID idHostCpu;
889 uint32_t iHostCpuSet = RTMpCurSetIndexAndId(&idHostCpu);
890 if (RT_LIKELY( iHostCpuSet < RTCPUSET_MAX_CPUS
891 && SUPIsTscDeltaAvailableForCpuSetIndex(iHostCpuSet)))
892 {
893 /*
894 * Commit the CPU identifiers and update the periodict preemption timer if it's active.
895 */
896#ifdef VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
897 CPUMR0SetLApic(pVCpu, iHostCpuSet);
898#endif
899 pVCpu->iHostCpuSet = iHostCpuSet;
900 ASMAtomicWriteU32(&pVCpu->idHostCpu, idHostCpu);
901
902 if (pVM->vmm.s.fUsePeriodicPreemptionTimers)
903 GVMMR0SchedUpdatePeriodicPreemptionTimer(pVM, pVCpu->idHostCpu, TMCalcHostTimerFrequency(pVM, pVCpu));
904
905 /*
906 * We might need to disable VT-x if the active switcher turns off paging.
907 */
908 bool fVTxDisabled;
909 int rc = HMR0EnterSwitcher(pVM, pVM->vmm.s.enmSwitcher, &fVTxDisabled);
910 if (RT_SUCCESS(rc))
911 {
912 /*
913 * Disable interrupts and run raw-mode code. The loop is for efficiently
914 * dispatching tracepoints that fired in raw-mode context.
915 */
916 RTCCUINTREG uFlags = ASMIntDisableFlags();
917
918 for (;;)
919 {
920 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED_EXEC);
921 TMNotifyStartOfExecution(pVCpu);
922
923 rc = pVM->vmm.s.pfnR0ToRawMode(pVM);
924 pVCpu->vmm.s.iLastGZRc = rc;
925
926 TMNotifyEndOfExecution(pVCpu);
927 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED);
928
929 if (rc != VINF_VMM_CALL_TRACER)
930 break;
931 SUPR0TracerUmodProbeFire(pVM->pSession, &pVCpu->vmm.s.TracerCtx);
932 }
933
934 /*
935 * Re-enable VT-x before we dispatch any pending host interrupts and
936 * re-enables interrupts.
937 */
938 HMR0LeaveSwitcher(pVM, fVTxDisabled);
939
940 if ( rc == VINF_EM_RAW_INTERRUPT
941 || rc == VINF_EM_RAW_INTERRUPT_HYPER)
942 TRPMR0DispatchHostInterrupt(pVM);
943
944 ASMSetFlags(uFlags);
945
946 /* Fire dtrace probe and collect statistics. */
947 VBOXVMM_R0_VMM_RETURN_TO_RING3_RC(pVCpu, CPUMQueryGuestCtxPtr(pVCpu), rc);
948#ifdef VBOX_WITH_STATISTICS
949 STAM_COUNTER_INC(&pVM->vmm.s.StatRunRC);
950 vmmR0RecordRC(pVM, pVCpu, rc);
951#endif
952 }
953 else
954 pVCpu->vmm.s.iLastGZRc = rc;
955
956 /*
957 * Invalidate the host CPU identifiers as we restore preemption.
958 */
959 pVCpu->iHostCpuSet = UINT32_MAX;
960 ASMAtomicWriteU32(&pVCpu->idHostCpu, NIL_RTCPUID);
961
962 RTThreadPreemptRestore(&PreemptState);
963 }
964 /*
965 * Invalid CPU set index or TSC delta in need of measuring.
966 */
967 else
968 {
969 RTThreadPreemptRestore(&PreemptState);
970 if (iHostCpuSet < RTCPUSET_MAX_CPUS)
971 {
972 int rc = SUPR0TscDeltaMeasureBySetIndex(pVM->pSession, iHostCpuSet, 0 /*fFlags*/,
973 2 /*cMsWaitRetry*/, 5*RT_MS_1SEC /*cMsWaitThread*/,
974 0 /*default cTries*/);
975 if (RT_SUCCESS(rc) || rc == VERR_CPU_OFFLINE)
976 pVCpu->vmm.s.iLastGZRc = VINF_EM_RAW_TO_R3;
977 else
978 pVCpu->vmm.s.iLastGZRc = rc;
979 }
980 else
981 pVCpu->vmm.s.iLastGZRc = VERR_INVALID_CPU_INDEX;
982 }
983 break;
984 }
985
986 /*
987 * Run guest code using the available hardware acceleration technology.
988 */
989 case VMMR0_DO_HM_RUN:
990 {
991 /*
992 * Disable preemption.
993 */
994 Assert(!VMMR0ThreadCtxHooksAreRegistered(pVCpu));
995 RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
996 RTThreadPreemptDisable(&PreemptState);
997
998 /*
999 * Get the host CPU identifiers, make sure they are valid and that
1000 * we've got a TSC delta for the CPU.
1001 */
1002 RTCPUID idHostCpu;
1003 uint32_t iHostCpuSet = RTMpCurSetIndexAndId(&idHostCpu);
1004 if (RT_LIKELY( iHostCpuSet < RTCPUSET_MAX_CPUS
1005 && SUPIsTscDeltaAvailableForCpuSetIndex(iHostCpuSet)))
1006 {
1007 pVCpu->iHostCpuSet = iHostCpuSet;
1008 ASMAtomicWriteU32(&pVCpu->idHostCpu, idHostCpu);
1009
1010 /*
1011 * Update the periodict preemption timer if it's active.
1012 */
1013 if (pVM->vmm.s.fUsePeriodicPreemptionTimers)
1014 GVMMR0SchedUpdatePeriodicPreemptionTimer(pVM, pVCpu->idHostCpu, TMCalcHostTimerFrequency(pVM, pVCpu));
1015
1016#ifdef LOG_ENABLED
1017 /*
1018 * Ugly: Lazy registration of ring 0 loggers.
1019 */
1020 if (pVCpu->idCpu > 0)
1021 {
1022 PVMMR0LOGGER pR0Logger = pVCpu->vmm.s.pR0LoggerR0;
1023 if ( pR0Logger
1024 && RT_UNLIKELY(!pR0Logger->fRegistered))
1025 {
1026 RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
1027 pR0Logger->fRegistered = true;
1028 }
1029 }
1030#endif
1031
1032 int rc;
1033 bool fPreemptRestored = false;
1034 if (!HMR0SuspendPending())
1035 {
1036 /*
1037 * Register thread-context hooks if required.
1038 */
1039 if ( VMMR0ThreadCtxHooksAreCreated(pVCpu)
1040 && !VMMR0ThreadCtxHooksAreRegistered(pVCpu))
1041 {
1042 rc = VMMR0ThreadCtxHooksRegister(pVCpu, vmmR0ThreadCtxCallback);
1043 AssertRC(rc);
1044 }
1045
1046 /*
1047 * Enter HM context.
1048 */
1049 rc = HMR0Enter(pVM, pVCpu);
1050 if (RT_SUCCESS(rc))
1051 {
1052 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED_HM);
1053
1054 /*
1055 * When preemption hooks are in place, enable preemption now that
1056 * we're in HM context.
1057 */
1058 if (VMMR0ThreadCtxHooksAreRegistered(pVCpu))
1059 {
1060 fPreemptRestored = true;
1061 RTThreadPreemptRestore(&PreemptState);
1062 }
1063
1064 /*
1065 * Setup the longjmp machinery and execute guest code (calls HMR0RunGuestCode).
1066 */
1067 rc = vmmR0CallRing3SetJmp(&pVCpu->vmm.s.CallRing3JmpBufR0, HMR0RunGuestCode, pVM, pVCpu);
1068
1069 /*
1070 * Assert sanity on the way out. Using manual assertions code here as normal
1071 * assertions are going to panic the host since we're outside the setjmp/longjmp zone.
1072 */
1073 if (RT_UNLIKELY( VMCPU_GET_STATE(pVCpu) != VMCPUSTATE_STARTED_HM
1074 && RT_SUCCESS_NP(rc) && rc != VINF_VMM_CALL_HOST ))
1075 {
1076 pVM->vmm.s.szRing0AssertMsg1[0] = '\0';
1077 RTStrPrintf(pVM->vmm.s.szRing0AssertMsg2, sizeof(pVM->vmm.s.szRing0AssertMsg2),
1078 "Got VMCPU state %d expected %d.\n", VMCPU_GET_STATE(pVCpu), VMCPUSTATE_STARTED_HM);
1079 rc = VERR_VMM_WRONG_HM_VMCPU_STATE;
1080 }
1081 else if (RT_UNLIKELY(VMMR0ThreadCtxHooksAreRegistered(pVCpu)))
1082 {
1083 pVM->vmm.s.szRing0AssertMsg1[0] = '\0';
1084 RTStrPrintf(pVM->vmm.s.szRing0AssertMsg2, sizeof(pVM->vmm.s.szRing0AssertMsg2),
1085 "Thread-context hooks still registered! VCPU=%p Id=%u rc=%d.\n", pVCpu, pVCpu->idCpu, rc);
1086 rc = VERR_INVALID_STATE;
1087 }
1088
1089 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED);
1090 }
1091 STAM_COUNTER_INC(&pVM->vmm.s.StatRunRC);
1092 }
1093 /*
1094 * The system is about to go into suspend mode; go back to ring 3.
1095 */
1096 else
1097 rc = VINF_EM_RAW_INTERRUPT;
1098
1099 /*
1100 * Invalidate the host CPU identifiers as we restore preemption.
1101 */
1102 pVCpu->iHostCpuSet = UINT32_MAX;
1103 ASMAtomicWriteU32(&pVCpu->idHostCpu, NIL_RTCPUID);
1104
1105 if (!fPreemptRestored)
1106 RTThreadPreemptRestore(&PreemptState);
1107
1108 pVCpu->vmm.s.iLastGZRc = rc;
1109
1110 /* Fire dtrace probe and collect statistics. */
1111 VBOXVMM_R0_VMM_RETURN_TO_RING3_HM(pVCpu, CPUMQueryGuestCtxPtr(pVCpu), rc);
1112#ifdef VBOX_WITH_STATISTICS
1113 vmmR0RecordRC(pVM, pVCpu, rc);
1114#endif
1115 }
1116 /*
1117 * Invalid CPU set index or TSC delta in need of measuring.
1118 */
1119 else
1120 {
1121 pVCpu->iHostCpuSet = UINT32_MAX;
1122 ASMAtomicWriteU32(&pVCpu->idHostCpu, NIL_RTCPUID);
1123 RTThreadPreemptRestore(&PreemptState);
1124 if (iHostCpuSet < RTCPUSET_MAX_CPUS)
1125 {
1126 int rc = SUPR0TscDeltaMeasureBySetIndex(pVM->pSession, iHostCpuSet, 0 /*fFlags*/,
1127 2 /*cMsWaitRetry*/, 5*RT_MS_1SEC /*cMsWaitThread*/,
1128 0 /*default cTries*/);
1129 if (RT_SUCCESS(rc) || rc == VERR_CPU_OFFLINE)
1130 pVCpu->vmm.s.iLastGZRc = VINF_EM_RAW_TO_R3;
1131 else
1132 pVCpu->vmm.s.iLastGZRc = rc;
1133 }
1134 else
1135 pVCpu->vmm.s.iLastGZRc = VERR_INVALID_CPU_INDEX;
1136 }
1137 break;
1138 }
1139
1140 /*
1141 * For profiling.
1142 */
1143 case VMMR0_DO_NOP:
1144 pVCpu->vmm.s.iLastGZRc = VINF_SUCCESS;
1145 break;
1146
1147 /*
1148 * Impossible.
1149 */
1150 default:
1151 AssertMsgFailed(("%#x\n", enmOperation));
1152 pVCpu->vmm.s.iLastGZRc = VERR_NOT_SUPPORTED;
1153 break;
1154 }
1155}
1156
1157
1158/**
1159 * Validates a session or VM session argument.
1160 *
1161 * @returns true / false accordingly.
1162 * @param pVM Pointer to the VM.
1163 * @param pSession The session argument.
1164 */
1165DECLINLINE(bool) vmmR0IsValidSession(PVM pVM, PSUPDRVSESSION pClaimedSession, PSUPDRVSESSION pSession)
1166{
1167 /* This must be set! */
1168 if (!pSession)
1169 return false;
1170
1171 /* Only one out of the two. */
1172 if (pVM && pClaimedSession)
1173 return false;
1174 if (pVM)
1175 pClaimedSession = pVM->pSession;
1176 return pClaimedSession == pSession;
1177}
1178
1179
1180/**
1181 * VMMR0EntryEx worker function, either called directly or when ever possible
1182 * called thru a longjmp so we can exit safely on failure.
1183 *
1184 * @returns VBox status code.
1185 * @param pVM Pointer to the VM.
1186 * @param idCpu Virtual CPU ID argument. Must be NIL_VMCPUID if pVM
1187 * is NIL_RTR0PTR, and may be NIL_VMCPUID if it isn't
1188 * @param enmOperation Which operation to execute.
1189 * @param pReqHdr This points to a SUPVMMR0REQHDR packet. Optional.
1190 * The support driver validates this if it's present.
1191 * @param u64Arg Some simple constant argument.
1192 * @param pSession The session of the caller.
1193 * @remarks Assume called with interrupts _enabled_.
1194 */
1195static int vmmR0EntryExWorker(PVM pVM, VMCPUID idCpu, VMMR0OPERATION enmOperation, PSUPVMMR0REQHDR pReqHdr, uint64_t u64Arg, PSUPDRVSESSION pSession)
1196{
1197 /*
1198 * Common VM pointer validation.
1199 */
1200 if (pVM)
1201 {
1202 if (RT_UNLIKELY( !VALID_PTR(pVM)
1203 || ((uintptr_t)pVM & PAGE_OFFSET_MASK)))
1204 {
1205 SUPR0Printf("vmmR0EntryExWorker: Invalid pVM=%p! (op=%d)\n", pVM, enmOperation);
1206 return VERR_INVALID_POINTER;
1207 }
1208 if (RT_UNLIKELY( pVM->enmVMState < VMSTATE_CREATING
1209 || pVM->enmVMState > VMSTATE_TERMINATED
1210 || pVM->pVMR0 != pVM))
1211 {
1212 SUPR0Printf("vmmR0EntryExWorker: Invalid pVM=%p:{enmVMState=%d, .pVMR0=%p}! (op=%d)\n",
1213 pVM, pVM->enmVMState, pVM->pVMR0, enmOperation);
1214 return VERR_INVALID_POINTER;
1215 }
1216
1217 if (RT_UNLIKELY(idCpu >= pVM->cCpus && idCpu != NIL_VMCPUID))
1218 {
1219 SUPR0Printf("vmmR0EntryExWorker: Invalid idCpu (%u vs cCpus=%u)\n", idCpu, pVM->cCpus);
1220 return VERR_INVALID_PARAMETER;
1221 }
1222 }
1223 else if (RT_UNLIKELY(idCpu != NIL_VMCPUID))
1224 {
1225 SUPR0Printf("vmmR0EntryExWorker: Invalid idCpu=%u\n", idCpu);
1226 return VERR_INVALID_PARAMETER;
1227 }
1228
1229
1230 switch (enmOperation)
1231 {
1232 /*
1233 * GVM requests
1234 */
1235 case VMMR0_DO_GVMM_CREATE_VM:
1236 if (pVM || u64Arg || idCpu != NIL_VMCPUID)
1237 return VERR_INVALID_PARAMETER;
1238 return GVMMR0CreateVMReq((PGVMMCREATEVMREQ)pReqHdr);
1239
1240 case VMMR0_DO_GVMM_DESTROY_VM:
1241 if (pReqHdr || u64Arg)
1242 return VERR_INVALID_PARAMETER;
1243 return GVMMR0DestroyVM(pVM);
1244
1245 case VMMR0_DO_GVMM_REGISTER_VMCPU:
1246 {
1247 if (!pVM)
1248 return VERR_INVALID_PARAMETER;
1249 return GVMMR0RegisterVCpu(pVM, idCpu);
1250 }
1251
1252 case VMMR0_DO_GVMM_SCHED_HALT:
1253 if (pReqHdr)
1254 return VERR_INVALID_PARAMETER;
1255 return GVMMR0SchedHalt(pVM, idCpu, u64Arg);
1256
1257 case VMMR0_DO_GVMM_SCHED_WAKE_UP:
1258 if (pReqHdr || u64Arg)
1259 return VERR_INVALID_PARAMETER;
1260 return GVMMR0SchedWakeUp(pVM, idCpu);
1261
1262 case VMMR0_DO_GVMM_SCHED_POKE:
1263 if (pReqHdr || u64Arg)
1264 return VERR_INVALID_PARAMETER;
1265 return GVMMR0SchedPoke(pVM, idCpu);
1266
1267 case VMMR0_DO_GVMM_SCHED_WAKE_UP_AND_POKE_CPUS:
1268 if (u64Arg)
1269 return VERR_INVALID_PARAMETER;
1270 return GVMMR0SchedWakeUpAndPokeCpusReq(pVM, (PGVMMSCHEDWAKEUPANDPOKECPUSREQ)pReqHdr);
1271
1272 case VMMR0_DO_GVMM_SCHED_POLL:
1273 if (pReqHdr || u64Arg > 1)
1274 return VERR_INVALID_PARAMETER;
1275 return GVMMR0SchedPoll(pVM, idCpu, !!u64Arg);
1276
1277 case VMMR0_DO_GVMM_QUERY_STATISTICS:
1278 if (u64Arg)
1279 return VERR_INVALID_PARAMETER;
1280 return GVMMR0QueryStatisticsReq(pVM, (PGVMMQUERYSTATISTICSSREQ)pReqHdr);
1281
1282 case VMMR0_DO_GVMM_RESET_STATISTICS:
1283 if (u64Arg)
1284 return VERR_INVALID_PARAMETER;
1285 return GVMMR0ResetStatisticsReq(pVM, (PGVMMRESETSTATISTICSSREQ)pReqHdr);
1286
1287 /*
1288 * Initialize the R0 part of a VM instance.
1289 */
1290 case VMMR0_DO_VMMR0_INIT:
1291 return vmmR0InitVM(pVM, RT_LODWORD(u64Arg), RT_HIDWORD(u64Arg));
1292
1293 /*
1294 * Terminate the R0 part of a VM instance.
1295 */
1296 case VMMR0_DO_VMMR0_TERM:
1297 return VMMR0TermVM(pVM, NULL);
1298
1299 /*
1300 * Attempt to enable hm mode and check the current setting.
1301 */
1302 case VMMR0_DO_HM_ENABLE:
1303 return HMR0EnableAllCpus(pVM);
1304
1305 /*
1306 * Setup the hardware accelerated session.
1307 */
1308 case VMMR0_DO_HM_SETUP_VM:
1309 return HMR0SetupVM(pVM);
1310
1311 /*
1312 * Switch to RC to execute Hypervisor function.
1313 */
1314 case VMMR0_DO_CALL_HYPERVISOR:
1315 {
1316 /*
1317 * Validate input / context.
1318 */
1319 if (RT_UNLIKELY(idCpu != 0))
1320 return VERR_INVALID_CPU_ID;
1321 if (RT_UNLIKELY(pVM->cCpus != 1))
1322 return VERR_INVALID_PARAMETER;
1323 PVMCPU pVCpu = &pVM->aCpus[idCpu];
1324#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
1325 if (RT_UNLIKELY(!PGMGetHyperCR3(pVCpu)))
1326 return VERR_PGM_NO_CR3_SHADOW_ROOT;
1327#endif
1328
1329 /*
1330 * Disable interrupts.
1331 */
1332 RTCCUINTREG fFlags = ASMIntDisableFlags();
1333
1334 /*
1335 * Get the host CPU identifiers, make sure they are valid and that
1336 * we've got a TSC delta for the CPU.
1337 */
1338 RTCPUID idHostCpu;
1339 uint32_t iHostCpuSet = RTMpCurSetIndexAndId(&idHostCpu);
1340 if (RT_UNLIKELY(iHostCpuSet >= RTCPUSET_MAX_CPUS))
1341 {
1342 ASMSetFlags(fFlags);
1343 return VERR_INVALID_CPU_INDEX;
1344 }
1345 if (RT_UNLIKELY(!SUPIsTscDeltaAvailableForCpuSetIndex(iHostCpuSet)))
1346 {
1347 ASMSetFlags(fFlags);
1348 int rc = SUPR0TscDeltaMeasureBySetIndex(pVM->pSession, iHostCpuSet, 0 /*fFlags*/,
1349 2 /*cMsWaitRetry*/, 5*RT_MS_1SEC /*cMsWaitThread*/,
1350 0 /*default cTries*/);
1351 if (RT_FAILURE(rc) && rc != VERR_CPU_OFFLINE)
1352 return rc;
1353 }
1354
1355 /*
1356 * Commit the CPU identifiers.
1357 */
1358#ifdef VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
1359 CPUMR0SetLApic(pVCpu, iHostCpuSet);
1360#endif
1361 pVCpu->iHostCpuSet = iHostCpuSet;
1362 ASMAtomicWriteU32(&pVCpu->idHostCpu, idHostCpu);
1363
1364 /*
1365 * We might need to disable VT-x if the active switcher turns off paging.
1366 */
1367 bool fVTxDisabled;
1368 int rc = HMR0EnterSwitcher(pVM, pVM->vmm.s.enmSwitcher, &fVTxDisabled);
1369 if (RT_SUCCESS(rc))
1370 {
1371 /*
1372 * Go through the wormhole...
1373 */
1374 rc = pVM->vmm.s.pfnR0ToRawMode(pVM);
1375
1376 /*
1377 * Re-enable VT-x before we dispatch any pending host interrupts.
1378 */
1379 HMR0LeaveSwitcher(pVM, fVTxDisabled);
1380
1381 if ( rc == VINF_EM_RAW_INTERRUPT
1382 || rc == VINF_EM_RAW_INTERRUPT_HYPER)
1383 TRPMR0DispatchHostInterrupt(pVM);
1384 }
1385
1386 /*
1387 * Invalidate the host CPU identifiers as we restore interrupts.
1388 */
1389 pVCpu->iHostCpuSet = UINT32_MAX;
1390 ASMAtomicWriteU32(&pVCpu->idHostCpu, NIL_RTCPUID);
1391 ASMSetFlags(fFlags);
1392 return rc;
1393 }
1394
1395 /*
1396 * PGM wrappers.
1397 */
1398 case VMMR0_DO_PGM_ALLOCATE_HANDY_PAGES:
1399 if (idCpu == NIL_VMCPUID)
1400 return VERR_INVALID_CPU_ID;
1401 return PGMR0PhysAllocateHandyPages(pVM, &pVM->aCpus[idCpu]);
1402
1403 case VMMR0_DO_PGM_FLUSH_HANDY_PAGES:
1404 if (idCpu == NIL_VMCPUID)
1405 return VERR_INVALID_CPU_ID;
1406 return PGMR0PhysFlushHandyPages(pVM, &pVM->aCpus[idCpu]);
1407
1408 case VMMR0_DO_PGM_ALLOCATE_LARGE_HANDY_PAGE:
1409 if (idCpu == NIL_VMCPUID)
1410 return VERR_INVALID_CPU_ID;
1411 return PGMR0PhysAllocateLargeHandyPage(pVM, &pVM->aCpus[idCpu]);
1412
1413 case VMMR0_DO_PGM_PHYS_SETUP_IOMMU:
1414 if (idCpu != 0)
1415 return VERR_INVALID_CPU_ID;
1416 return PGMR0PhysSetupIommu(pVM);
1417
1418 /*
1419 * GMM wrappers.
1420 */
1421 case VMMR0_DO_GMM_INITIAL_RESERVATION:
1422 if (u64Arg)
1423 return VERR_INVALID_PARAMETER;
1424 return GMMR0InitialReservationReq(pVM, idCpu, (PGMMINITIALRESERVATIONREQ)pReqHdr);
1425
1426 case VMMR0_DO_GMM_UPDATE_RESERVATION:
1427 if (u64Arg)
1428 return VERR_INVALID_PARAMETER;
1429 return GMMR0UpdateReservationReq(pVM, idCpu, (PGMMUPDATERESERVATIONREQ)pReqHdr);
1430
1431 case VMMR0_DO_GMM_ALLOCATE_PAGES:
1432 if (u64Arg)
1433 return VERR_INVALID_PARAMETER;
1434 return GMMR0AllocatePagesReq(pVM, idCpu, (PGMMALLOCATEPAGESREQ)pReqHdr);
1435
1436 case VMMR0_DO_GMM_FREE_PAGES:
1437 if (u64Arg)
1438 return VERR_INVALID_PARAMETER;
1439 return GMMR0FreePagesReq(pVM, idCpu, (PGMMFREEPAGESREQ)pReqHdr);
1440
1441 case VMMR0_DO_GMM_FREE_LARGE_PAGE:
1442 if (u64Arg)
1443 return VERR_INVALID_PARAMETER;
1444 return GMMR0FreeLargePageReq(pVM, idCpu, (PGMMFREELARGEPAGEREQ)pReqHdr);
1445
1446 case VMMR0_DO_GMM_QUERY_HYPERVISOR_MEM_STATS:
1447 if (u64Arg)
1448 return VERR_INVALID_PARAMETER;
1449 return GMMR0QueryHypervisorMemoryStatsReq(pVM, (PGMMMEMSTATSREQ)pReqHdr);
1450
1451 case VMMR0_DO_GMM_QUERY_MEM_STATS:
1452 if (idCpu == NIL_VMCPUID)
1453 return VERR_INVALID_CPU_ID;
1454 if (u64Arg)
1455 return VERR_INVALID_PARAMETER;
1456 return GMMR0QueryMemoryStatsReq(pVM, idCpu, (PGMMMEMSTATSREQ)pReqHdr);
1457
1458 case VMMR0_DO_GMM_BALLOONED_PAGES:
1459 if (u64Arg)
1460 return VERR_INVALID_PARAMETER;
1461 return GMMR0BalloonedPagesReq(pVM, idCpu, (PGMMBALLOONEDPAGESREQ)pReqHdr);
1462
1463 case VMMR0_DO_GMM_MAP_UNMAP_CHUNK:
1464 if (u64Arg)
1465 return VERR_INVALID_PARAMETER;
1466 return GMMR0MapUnmapChunkReq(pVM, (PGMMMAPUNMAPCHUNKREQ)pReqHdr);
1467
1468 case VMMR0_DO_GMM_SEED_CHUNK:
1469 if (pReqHdr)
1470 return VERR_INVALID_PARAMETER;
1471 return GMMR0SeedChunk(pVM, idCpu, (RTR3PTR)u64Arg);
1472
1473 case VMMR0_DO_GMM_REGISTER_SHARED_MODULE:
1474 if (idCpu == NIL_VMCPUID)
1475 return VERR_INVALID_CPU_ID;
1476 if (u64Arg)
1477 return VERR_INVALID_PARAMETER;
1478 return GMMR0RegisterSharedModuleReq(pVM, idCpu, (PGMMREGISTERSHAREDMODULEREQ)pReqHdr);
1479
1480 case VMMR0_DO_GMM_UNREGISTER_SHARED_MODULE:
1481 if (idCpu == NIL_VMCPUID)
1482 return VERR_INVALID_CPU_ID;
1483 if (u64Arg)
1484 return VERR_INVALID_PARAMETER;
1485 return GMMR0UnregisterSharedModuleReq(pVM, idCpu, (PGMMUNREGISTERSHAREDMODULEREQ)pReqHdr);
1486
1487 case VMMR0_DO_GMM_RESET_SHARED_MODULES:
1488 if (idCpu == NIL_VMCPUID)
1489 return VERR_INVALID_CPU_ID;
1490 if ( u64Arg
1491 || pReqHdr)
1492 return VERR_INVALID_PARAMETER;
1493 return GMMR0ResetSharedModules(pVM, idCpu);
1494
1495#ifdef VBOX_WITH_PAGE_SHARING
1496 case VMMR0_DO_GMM_CHECK_SHARED_MODULES:
1497 {
1498 if (idCpu == NIL_VMCPUID)
1499 return VERR_INVALID_CPU_ID;
1500 if ( u64Arg
1501 || pReqHdr)
1502 return VERR_INVALID_PARAMETER;
1503
1504 PVMCPU pVCpu = &pVM->aCpus[idCpu];
1505 Assert(pVCpu->hNativeThreadR0 == RTThreadNativeSelf());
1506
1507# ifdef DEBUG_sandervl
1508 /* 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). */
1509 /* Todo: this can have bad side effects for unexpected jumps back to r3. */
1510 int rc = GMMR0CheckSharedModulesStart(pVM);
1511 if (rc == VINF_SUCCESS)
1512 {
1513 rc = vmmR0CallRing3SetJmp(&pVCpu->vmm.s.CallRing3JmpBufR0, GMMR0CheckSharedModules, pVM, pVCpu); /* this may resume code. */
1514 Assert( rc == VINF_SUCCESS
1515 || (rc == VINF_VMM_CALL_HOST && pVCpu->vmm.s.enmCallRing3Operation == VMMCALLRING3_VMM_LOGGER_FLUSH));
1516 GMMR0CheckSharedModulesEnd(pVM);
1517 }
1518# else
1519 int rc = GMMR0CheckSharedModules(pVM, pVCpu);
1520# endif
1521 return rc;
1522 }
1523#endif
1524
1525#if defined(VBOX_STRICT) && HC_ARCH_BITS == 64
1526 case VMMR0_DO_GMM_FIND_DUPLICATE_PAGE:
1527 if (u64Arg)
1528 return VERR_INVALID_PARAMETER;
1529 return GMMR0FindDuplicatePageReq(pVM, (PGMMFINDDUPLICATEPAGEREQ)pReqHdr);
1530#endif
1531
1532 case VMMR0_DO_GMM_QUERY_STATISTICS:
1533 if (u64Arg)
1534 return VERR_INVALID_PARAMETER;
1535 return GMMR0QueryStatisticsReq(pVM, (PGMMQUERYSTATISTICSSREQ)pReqHdr);
1536
1537 case VMMR0_DO_GMM_RESET_STATISTICS:
1538 if (u64Arg)
1539 return VERR_INVALID_PARAMETER;
1540 return GMMR0ResetStatisticsReq(pVM, (PGMMRESETSTATISTICSSREQ)pReqHdr);
1541
1542 /*
1543 * A quick GCFGM mock-up.
1544 */
1545 /** @todo GCFGM with proper access control, ring-3 management interface and all that. */
1546 case VMMR0_DO_GCFGM_SET_VALUE:
1547 case VMMR0_DO_GCFGM_QUERY_VALUE:
1548 {
1549 if (pVM || !pReqHdr || u64Arg || idCpu != NIL_VMCPUID)
1550 return VERR_INVALID_PARAMETER;
1551 PGCFGMVALUEREQ pReq = (PGCFGMVALUEREQ)pReqHdr;
1552 if (pReq->Hdr.cbReq != sizeof(*pReq))
1553 return VERR_INVALID_PARAMETER;
1554 int rc;
1555 if (enmOperation == VMMR0_DO_GCFGM_SET_VALUE)
1556 {
1557 rc = GVMMR0SetConfig(pReq->pSession, &pReq->szName[0], pReq->u64Value);
1558 //if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1559 // rc = GMMR0SetConfig(pReq->pSession, &pReq->szName[0], pReq->u64Value);
1560 }
1561 else
1562 {
1563 rc = GVMMR0QueryConfig(pReq->pSession, &pReq->szName[0], &pReq->u64Value);
1564 //if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1565 // rc = GMMR0QueryConfig(pReq->pSession, &pReq->szName[0], &pReq->u64Value);
1566 }
1567 return rc;
1568 }
1569
1570 /*
1571 * PDM Wrappers.
1572 */
1573 case VMMR0_DO_PDM_DRIVER_CALL_REQ_HANDLER:
1574 {
1575 if (!pVM || !pReqHdr || u64Arg || idCpu != NIL_VMCPUID)
1576 return VERR_INVALID_PARAMETER;
1577 return PDMR0DriverCallReqHandler(pVM, (PPDMDRIVERCALLREQHANDLERREQ)pReqHdr);
1578 }
1579
1580 case VMMR0_DO_PDM_DEVICE_CALL_REQ_HANDLER:
1581 {
1582 if (!pVM || !pReqHdr || u64Arg || idCpu != NIL_VMCPUID)
1583 return VERR_INVALID_PARAMETER;
1584 return PDMR0DeviceCallReqHandler(pVM, (PPDMDEVICECALLREQHANDLERREQ)pReqHdr);
1585 }
1586
1587 /*
1588 * Requests to the internal networking service.
1589 */
1590 case VMMR0_DO_INTNET_OPEN:
1591 {
1592 PINTNETOPENREQ pReq = (PINTNETOPENREQ)pReqHdr;
1593 if (u64Arg || !pReq || !vmmR0IsValidSession(pVM, pReq->pSession, pSession) || idCpu != NIL_VMCPUID)
1594 return VERR_INVALID_PARAMETER;
1595 return IntNetR0OpenReq(pSession, pReq);
1596 }
1597
1598 case VMMR0_DO_INTNET_IF_CLOSE:
1599 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFCLOSEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1600 return VERR_INVALID_PARAMETER;
1601 return IntNetR0IfCloseReq(pSession, (PINTNETIFCLOSEREQ)pReqHdr);
1602
1603 case VMMR0_DO_INTNET_IF_GET_BUFFER_PTRS:
1604 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFGETBUFFERPTRSREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1605 return VERR_INVALID_PARAMETER;
1606 return IntNetR0IfGetBufferPtrsReq(pSession, (PINTNETIFGETBUFFERPTRSREQ)pReqHdr);
1607
1608 case VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE:
1609 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETPROMISCUOUSMODEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1610 return VERR_INVALID_PARAMETER;
1611 return IntNetR0IfSetPromiscuousModeReq(pSession, (PINTNETIFSETPROMISCUOUSMODEREQ)pReqHdr);
1612
1613 case VMMR0_DO_INTNET_IF_SET_MAC_ADDRESS:
1614 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETMACADDRESSREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1615 return VERR_INVALID_PARAMETER;
1616 return IntNetR0IfSetMacAddressReq(pSession, (PINTNETIFSETMACADDRESSREQ)pReqHdr);
1617
1618 case VMMR0_DO_INTNET_IF_SET_ACTIVE:
1619 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETACTIVEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1620 return VERR_INVALID_PARAMETER;
1621 return IntNetR0IfSetActiveReq(pSession, (PINTNETIFSETACTIVEREQ)pReqHdr);
1622
1623 case VMMR0_DO_INTNET_IF_SEND:
1624 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSENDREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1625 return VERR_INVALID_PARAMETER;
1626 return IntNetR0IfSendReq(pSession, (PINTNETIFSENDREQ)pReqHdr);
1627
1628 case VMMR0_DO_INTNET_IF_WAIT:
1629 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFWAITREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1630 return VERR_INVALID_PARAMETER;
1631 return IntNetR0IfWaitReq(pSession, (PINTNETIFWAITREQ)pReqHdr);
1632
1633 case VMMR0_DO_INTNET_IF_ABORT_WAIT:
1634 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFWAITREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1635 return VERR_INVALID_PARAMETER;
1636 return IntNetR0IfAbortWaitReq(pSession, (PINTNETIFABORTWAITREQ)pReqHdr);
1637
1638#ifdef VBOX_WITH_PCI_PASSTHROUGH
1639 /*
1640 * Requests to host PCI driver service.
1641 */
1642 case VMMR0_DO_PCIRAW_REQ:
1643 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PPCIRAWSENDREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1644 return VERR_INVALID_PARAMETER;
1645 return PciRawR0ProcessReq(pSession, pVM, (PPCIRAWSENDREQ)pReqHdr);
1646#endif
1647 /*
1648 * For profiling.
1649 */
1650 case VMMR0_DO_NOP:
1651 case VMMR0_DO_SLOW_NOP:
1652 return VINF_SUCCESS;
1653
1654 /*
1655 * For testing Ring-0 APIs invoked in this environment.
1656 */
1657 case VMMR0_DO_TESTS:
1658 /** @todo make new test */
1659 return VINF_SUCCESS;
1660
1661
1662#if HC_ARCH_BITS == 32 && defined(VBOX_WITH_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
1663 case VMMR0_DO_TEST_SWITCHER3264:
1664 if (idCpu == NIL_VMCPUID)
1665 return VERR_INVALID_CPU_ID;
1666 return HMR0TestSwitcher3264(pVM);
1667#endif
1668 default:
1669 /*
1670 * We're returning VERR_NOT_SUPPORT here so we've got something else
1671 * than -1 which the interrupt gate glue code might return.
1672 */
1673 Log(("operation %#x is not supported\n", enmOperation));
1674 return VERR_NOT_SUPPORTED;
1675 }
1676}
1677
1678
1679/**
1680 * Argument for vmmR0EntryExWrapper containing the arguments for VMMR0EntryEx.
1681 */
1682typedef struct VMMR0ENTRYEXARGS
1683{
1684 PVM pVM;
1685 VMCPUID idCpu;
1686 VMMR0OPERATION enmOperation;
1687 PSUPVMMR0REQHDR pReq;
1688 uint64_t u64Arg;
1689 PSUPDRVSESSION pSession;
1690} VMMR0ENTRYEXARGS;
1691/** Pointer to a vmmR0EntryExWrapper argument package. */
1692typedef VMMR0ENTRYEXARGS *PVMMR0ENTRYEXARGS;
1693
1694/**
1695 * This is just a longjmp wrapper function for VMMR0EntryEx calls.
1696 *
1697 * @returns VBox status code.
1698 * @param pvArgs The argument package
1699 */
1700static DECLCALLBACK(int) vmmR0EntryExWrapper(void *pvArgs)
1701{
1702 return vmmR0EntryExWorker(((PVMMR0ENTRYEXARGS)pvArgs)->pVM,
1703 ((PVMMR0ENTRYEXARGS)pvArgs)->idCpu,
1704 ((PVMMR0ENTRYEXARGS)pvArgs)->enmOperation,
1705 ((PVMMR0ENTRYEXARGS)pvArgs)->pReq,
1706 ((PVMMR0ENTRYEXARGS)pvArgs)->u64Arg,
1707 ((PVMMR0ENTRYEXARGS)pvArgs)->pSession);
1708}
1709
1710
1711/**
1712 * The Ring 0 entry point, called by the support library (SUP).
1713 *
1714 * @returns VBox status code.
1715 * @param pVM Pointer to the VM.
1716 * @param idCpu Virtual CPU ID argument. Must be NIL_VMCPUID if pVM
1717 * is NIL_RTR0PTR, and may be NIL_VMCPUID if it isn't
1718 * @param enmOperation Which operation to execute.
1719 * @param pReq Pointer to the SUPVMMR0REQHDR packet. Optional.
1720 * @param u64Arg Some simple constant argument.
1721 * @param pSession The session of the caller.
1722 * @remarks Assume called with interrupts _enabled_.
1723 */
1724VMMR0DECL(int) VMMR0EntryEx(PVM pVM, VMCPUID idCpu, VMMR0OPERATION enmOperation, PSUPVMMR0REQHDR pReq, uint64_t u64Arg, PSUPDRVSESSION pSession)
1725{
1726 /*
1727 * Requests that should only happen on the EMT thread will be
1728 * wrapped in a setjmp so we can assert without causing trouble.
1729 */
1730 if ( VALID_PTR(pVM)
1731 && pVM->pVMR0
1732 && idCpu < pVM->cCpus)
1733 {
1734 switch (enmOperation)
1735 {
1736 /* These might/will be called before VMMR3Init. */
1737 case VMMR0_DO_GMM_INITIAL_RESERVATION:
1738 case VMMR0_DO_GMM_UPDATE_RESERVATION:
1739 case VMMR0_DO_GMM_ALLOCATE_PAGES:
1740 case VMMR0_DO_GMM_FREE_PAGES:
1741 case VMMR0_DO_GMM_BALLOONED_PAGES:
1742 /* On the mac we might not have a valid jmp buf, so check these as well. */
1743 case VMMR0_DO_VMMR0_INIT:
1744 case VMMR0_DO_VMMR0_TERM:
1745 {
1746 PVMCPU pVCpu = &pVM->aCpus[idCpu];
1747
1748 if (!pVCpu->vmm.s.CallRing3JmpBufR0.pvSavedStack)
1749 break;
1750
1751 /** @todo validate this EMT claim... GVM knows. */
1752 VMMR0ENTRYEXARGS Args;
1753 Args.pVM = pVM;
1754 Args.idCpu = idCpu;
1755 Args.enmOperation = enmOperation;
1756 Args.pReq = pReq;
1757 Args.u64Arg = u64Arg;
1758 Args.pSession = pSession;
1759 return vmmR0CallRing3SetJmpEx(&pVCpu->vmm.s.CallRing3JmpBufR0, vmmR0EntryExWrapper, &Args);
1760 }
1761
1762 default:
1763 break;
1764 }
1765 }
1766 return vmmR0EntryExWorker(pVM, idCpu, enmOperation, pReq, u64Arg, pSession);
1767}
1768
1769
1770/**
1771 * Checks whether we've armed the ring-0 long jump machinery.
1772 *
1773 * @returns @c true / @c false
1774 * @param pVCpu Pointer to the VMCPU.
1775 * @thread EMT
1776 * @sa VMMIsLongJumpArmed
1777 */
1778VMMR0_INT_DECL(bool) VMMR0IsLongJumpArmed(PVMCPU pVCpu)
1779{
1780#ifdef RT_ARCH_X86
1781 return pVCpu->vmm.s.CallRing3JmpBufR0.eip
1782 && !pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call;
1783#else
1784 return pVCpu->vmm.s.CallRing3JmpBufR0.rip
1785 && !pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call;
1786#endif
1787}
1788
1789
1790/**
1791 * Checks whether we've done a ring-3 long jump.
1792 *
1793 * @returns @c true / @c false
1794 * @param pVCpu Pointer to the VMCPU.
1795 * @thread EMT
1796 */
1797VMMR0_INT_DECL(bool) VMMR0IsInRing3LongJump(PVMCPU pVCpu)
1798{
1799 return pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call;
1800}
1801
1802
1803/**
1804 * Internal R0 logger worker: Flush logger.
1805 *
1806 * @param pLogger The logger instance to flush.
1807 * @remark This function must be exported!
1808 */
1809VMMR0DECL(void) vmmR0LoggerFlush(PRTLOGGER pLogger)
1810{
1811#ifdef LOG_ENABLED
1812 /*
1813 * Convert the pLogger into a VM handle and 'call' back to Ring-3.
1814 * (This is a bit paranoid code.)
1815 */
1816 PVMMR0LOGGER pR0Logger = (PVMMR0LOGGER)((uintptr_t)pLogger - RT_OFFSETOF(VMMR0LOGGER, Logger));
1817 if ( !VALID_PTR(pR0Logger)
1818 || !VALID_PTR(pR0Logger + 1)
1819 || pLogger->u32Magic != RTLOGGER_MAGIC)
1820 {
1821# ifdef DEBUG
1822 SUPR0Printf("vmmR0LoggerFlush: pLogger=%p!\n", pLogger);
1823# endif
1824 return;
1825 }
1826 if (pR0Logger->fFlushingDisabled)
1827 return; /* quietly */
1828
1829 PVM pVM = pR0Logger->pVM;
1830 if ( !VALID_PTR(pVM)
1831 || pVM->pVMR0 != pVM)
1832 {
1833# ifdef DEBUG
1834 SUPR0Printf("vmmR0LoggerFlush: pVM=%p! pVMR0=%p! pLogger=%p\n", pVM, pVM->pVMR0, pLogger);
1835# endif
1836 return;
1837 }
1838
1839 PVMCPU pVCpu = VMMGetCpu(pVM);
1840 if (pVCpu)
1841 {
1842 /*
1843 * Check that the jump buffer is armed.
1844 */
1845# ifdef RT_ARCH_X86
1846 if ( !pVCpu->vmm.s.CallRing3JmpBufR0.eip
1847 || pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call)
1848# else
1849 if ( !pVCpu->vmm.s.CallRing3JmpBufR0.rip
1850 || pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call)
1851# endif
1852 {
1853# ifdef DEBUG
1854 SUPR0Printf("vmmR0LoggerFlush: Jump buffer isn't armed!\n");
1855# endif
1856 return;
1857 }
1858 VMMRZCallRing3(pVM, pVCpu, VMMCALLRING3_VMM_LOGGER_FLUSH, 0);
1859 }
1860# ifdef DEBUG
1861 else
1862 SUPR0Printf("vmmR0LoggerFlush: invalid VCPU context!\n");
1863# endif
1864#endif
1865}
1866
1867/**
1868 * Internal R0 logger worker: Custom prefix.
1869 *
1870 * @returns Number of chars written.
1871 *
1872 * @param pLogger The logger instance.
1873 * @param pchBuf The output buffer.
1874 * @param cchBuf The size of the buffer.
1875 * @param pvUser User argument (ignored).
1876 */
1877VMMR0DECL(size_t) vmmR0LoggerPrefix(PRTLOGGER pLogger, char *pchBuf, size_t cchBuf, void *pvUser)
1878{
1879 NOREF(pvUser);
1880#ifdef LOG_ENABLED
1881 PVMMR0LOGGER pR0Logger = (PVMMR0LOGGER)((uintptr_t)pLogger - RT_OFFSETOF(VMMR0LOGGER, Logger));
1882 if ( !VALID_PTR(pR0Logger)
1883 || !VALID_PTR(pR0Logger + 1)
1884 || pLogger->u32Magic != RTLOGGER_MAGIC
1885 || cchBuf < 2)
1886 return 0;
1887
1888 static const char s_szHex[17] = "0123456789abcdef";
1889 VMCPUID const idCpu = pR0Logger->idCpu;
1890 pchBuf[1] = s_szHex[ idCpu & 15];
1891 pchBuf[0] = s_szHex[(idCpu >> 4) & 15];
1892
1893 return 2;
1894#else
1895 return 0;
1896#endif
1897}
1898
1899#ifdef LOG_ENABLED
1900
1901/**
1902 * Disables flushing of the ring-0 debug log.
1903 *
1904 * @param pVCpu Pointer to the VMCPU.
1905 */
1906VMMR0_INT_DECL(void) VMMR0LogFlushDisable(PVMCPU pVCpu)
1907{
1908 if (pVCpu->vmm.s.pR0LoggerR0)
1909 pVCpu->vmm.s.pR0LoggerR0->fFlushingDisabled = true;
1910}
1911
1912
1913/**
1914 * Enables flushing of the ring-0 debug log.
1915 *
1916 * @param pVCpu Pointer to the VMCPU.
1917 */
1918VMMR0_INT_DECL(void) VMMR0LogFlushEnable(PVMCPU pVCpu)
1919{
1920 if (pVCpu->vmm.s.pR0LoggerR0)
1921 pVCpu->vmm.s.pR0LoggerR0->fFlushingDisabled = false;
1922}
1923
1924
1925/**
1926 * Checks if log flushing is disabled or not.
1927 *
1928 * @param pVCpu Pointer to the VMCPU.
1929 */
1930VMMR0_INT_DECL(bool) VMMR0IsLogFlushDisabled(PVMCPU pVCpu)
1931{
1932 if (pVCpu->vmm.s.pR0LoggerR0)
1933 return pVCpu->vmm.s.pR0LoggerR0->fFlushingDisabled;
1934 return true;
1935}
1936#endif /* LOG_ENABLED */
1937
1938/**
1939 * Jump back to ring-3 if we're the EMT and the longjmp is armed.
1940 *
1941 * @returns true if the breakpoint should be hit, false if it should be ignored.
1942 */
1943DECLEXPORT(bool) RTCALL RTAssertShouldPanic(void)
1944{
1945#if 0
1946 return true;
1947#else
1948 PVM pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
1949 if (pVM)
1950 {
1951 PVMCPU pVCpu = VMMGetCpu(pVM);
1952
1953 if (pVCpu)
1954 {
1955#ifdef RT_ARCH_X86
1956 if ( pVCpu->vmm.s.CallRing3JmpBufR0.eip
1957 && !pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call)
1958#else
1959 if ( pVCpu->vmm.s.CallRing3JmpBufR0.rip
1960 && !pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call)
1961#endif
1962 {
1963 int rc = VMMRZCallRing3(pVM, pVCpu, VMMCALLRING3_VM_R0_ASSERTION, 0);
1964 return RT_FAILURE_NP(rc);
1965 }
1966 }
1967 }
1968#ifdef RT_OS_LINUX
1969 return true;
1970#else
1971 return false;
1972#endif
1973#endif
1974}
1975
1976
1977/**
1978 * Override this so we can push it up to ring-3.
1979 *
1980 * @param pszExpr Expression. Can be NULL.
1981 * @param uLine Location line number.
1982 * @param pszFile Location file name.
1983 * @param pszFunction Location function name.
1984 */
1985DECLEXPORT(void) RTCALL RTAssertMsg1Weak(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
1986{
1987 /*
1988 * To the log.
1989 */
1990 LogAlways(("\n!!R0-Assertion Failed!!\n"
1991 "Expression: %s\n"
1992 "Location : %s(%d) %s\n",
1993 pszExpr, pszFile, uLine, pszFunction));
1994
1995 /*
1996 * To the global VMM buffer.
1997 */
1998 PVM pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
1999 if (pVM)
2000 RTStrPrintf(pVM->vmm.s.szRing0AssertMsg1, sizeof(pVM->vmm.s.szRing0AssertMsg1),
2001 "\n!!R0-Assertion Failed!!\n"
2002 "Expression: %s\n"
2003 "Location : %s(%d) %s\n",
2004 pszExpr, pszFile, uLine, pszFunction);
2005
2006 /*
2007 * Continue the normal way.
2008 */
2009 RTAssertMsg1(pszExpr, uLine, pszFile, pszFunction);
2010}
2011
2012
2013/**
2014 * Callback for RTLogFormatV which writes to the ring-3 log port.
2015 * See PFNLOGOUTPUT() for details.
2016 */
2017static DECLCALLBACK(size_t) rtLogOutput(void *pv, const char *pachChars, size_t cbChars)
2018{
2019 for (size_t i = 0; i < cbChars; i++)
2020 LogAlways(("%c", pachChars[i]));
2021
2022 NOREF(pv);
2023 return cbChars;
2024}
2025
2026
2027/**
2028 * Override this so we can push it up to ring-3.
2029 *
2030 * @param pszFormat The format string.
2031 * @param va Arguments.
2032 */
2033DECLEXPORT(void) RTCALL RTAssertMsg2WeakV(const char *pszFormat, va_list va)
2034{
2035 va_list vaCopy;
2036
2037 /*
2038 * Push the message to the loggers.
2039 */
2040 PRTLOGGER pLog = RTLogGetDefaultInstance(); /* Don't initialize it here... */
2041 if (pLog)
2042 {
2043 va_copy(vaCopy, va);
2044 RTLogFormatV(rtLogOutput, pLog, pszFormat, vaCopy);
2045 va_end(vaCopy);
2046 }
2047 pLog = RTLogRelDefaultInstance();
2048 if (pLog)
2049 {
2050 va_copy(vaCopy, va);
2051 RTLogFormatV(rtLogOutput, pLog, pszFormat, vaCopy);
2052 va_end(vaCopy);
2053 }
2054
2055 /*
2056 * Push it to the global VMM buffer.
2057 */
2058 PVM pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
2059 if (pVM)
2060 {
2061 va_copy(vaCopy, va);
2062 RTStrPrintfV(pVM->vmm.s.szRing0AssertMsg2, sizeof(pVM->vmm.s.szRing0AssertMsg2), pszFormat, vaCopy);
2063 va_end(vaCopy);
2064 }
2065
2066 /*
2067 * Continue the normal way.
2068 */
2069 RTAssertMsg2V(pszFormat, va);
2070}
2071
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