VirtualBox

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

Last change on this file since 48200 was 48139, checked in by vboxsync, 11 years ago

disble thread-context hooks on Linux again

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