VirtualBox

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

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

VMM/VMMR0: Clear idHostCpu *before* deregistering the preemption hook.

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