VirtualBox

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

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

VMCPU: Added a iHostCpuSet field to go with the idHostCpu one. This allows for better GIP time & TSC code in raw-mode context.

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