VirtualBox

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

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

Return to ring-3 trace points.

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