VirtualBox

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

Last change on this file since 48935 was 48448, checked in by vboxsync, 11 years ago

VMM: Assertions.

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