VirtualBox

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

Last change on this file since 48366 was 48236, checked in by vboxsync, 11 years ago

VMM: Use the CMOS for debugging 64-bit on 32-bit issue (VBOX_WITH_64ON32_CMOS_DEBUG, disabled ofc).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 62.0 KB
Line 
1/* $Id: VMMR0.cpp 48236 2013-09-02 19:14:55Z 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_SOLARIS) || defined(RT_OS_LINUX)
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 if (RT_UNLIKELY( VMCPU_GET_STATE(pVCpu) != VMCPUSTATE_STARTED_HM
951 && RT_SUCCESS_NP(rc) && rc != VINF_VMM_CALL_HOST ))
952 {
953 /* Manual assert as normal assertions are going to crash in this case. */
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 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED);
960 }
961 STAM_COUNTER_INC(&pVM->vmm.s.StatRunRC);
962 }
963 else
964 {
965 /* System is about to go into suspend mode; go back to ring 3. */
966 rc = VINF_EM_RAW_INTERRUPT;
967 }
968 pVCpu->vmm.s.iLastGZRc = rc;
969
970 /* Clear the VCPU <-> host CPU mapping as we've left HM context. */
971 ASMAtomicWriteU32(&pVCpu->idHostCpu, NIL_RTCPUID);
972
973 if (!fPreemptRestored)
974 RTThreadPreemptRestore(&PreemptState);
975
976#ifdef VBOX_WITH_STATISTICS
977 vmmR0RecordRC(pVM, pVCpu, rc);
978#endif
979 /* No special action required for external interrupts, just return. */
980 break;
981 }
982
983 /*
984 * For profiling.
985 */
986 case VMMR0_DO_NOP:
987 pVCpu->vmm.s.iLastGZRc = VINF_SUCCESS;
988 break;
989
990 /*
991 * Impossible.
992 */
993 default:
994 AssertMsgFailed(("%#x\n", enmOperation));
995 pVCpu->vmm.s.iLastGZRc = VERR_NOT_SUPPORTED;
996 break;
997 }
998}
999
1000
1001/**
1002 * Validates a session or VM session argument.
1003 *
1004 * @returns true / false accordingly.
1005 * @param pVM Pointer to the VM.
1006 * @param pSession The session argument.
1007 */
1008DECLINLINE(bool) vmmR0IsValidSession(PVM pVM, PSUPDRVSESSION pClaimedSession, PSUPDRVSESSION pSession)
1009{
1010 /* This must be set! */
1011 if (!pSession)
1012 return false;
1013
1014 /* Only one out of the two. */
1015 if (pVM && pClaimedSession)
1016 return false;
1017 if (pVM)
1018 pClaimedSession = pVM->pSession;
1019 return pClaimedSession == pSession;
1020}
1021
1022
1023/**
1024 * VMMR0EntryEx worker function, either called directly or when ever possible
1025 * called thru a longjmp so we can exit safely on failure.
1026 *
1027 * @returns VBox status code.
1028 * @param pVM Pointer to the VM.
1029 * @param idCpu Virtual CPU ID argument. Must be NIL_VMCPUID if pVM
1030 * is NIL_RTR0PTR, and may be NIL_VMCPUID if it isn't
1031 * @param enmOperation Which operation to execute.
1032 * @param pReqHdr This points to a SUPVMMR0REQHDR packet. Optional.
1033 * The support driver validates this if it's present.
1034 * @param u64Arg Some simple constant argument.
1035 * @param pSession The session of the caller.
1036 * @remarks Assume called with interrupts _enabled_.
1037 */
1038static int vmmR0EntryExWorker(PVM pVM, VMCPUID idCpu, VMMR0OPERATION enmOperation, PSUPVMMR0REQHDR pReqHdr, uint64_t u64Arg, PSUPDRVSESSION pSession)
1039{
1040 /*
1041 * Common VM pointer validation.
1042 */
1043 if (pVM)
1044 {
1045 if (RT_UNLIKELY( !VALID_PTR(pVM)
1046 || ((uintptr_t)pVM & PAGE_OFFSET_MASK)))
1047 {
1048 SUPR0Printf("vmmR0EntryExWorker: Invalid pVM=%p! (op=%d)\n", pVM, enmOperation);
1049 return VERR_INVALID_POINTER;
1050 }
1051 if (RT_UNLIKELY( pVM->enmVMState < VMSTATE_CREATING
1052 || pVM->enmVMState > VMSTATE_TERMINATED
1053 || pVM->pVMR0 != pVM))
1054 {
1055 SUPR0Printf("vmmR0EntryExWorker: Invalid pVM=%p:{enmVMState=%d, .pVMR0=%p}! (op=%d)\n",
1056 pVM, pVM->enmVMState, pVM->pVMR0, enmOperation);
1057 return VERR_INVALID_POINTER;
1058 }
1059
1060 if (RT_UNLIKELY(idCpu >= pVM->cCpus && idCpu != NIL_VMCPUID))
1061 {
1062 SUPR0Printf("vmmR0EntryExWorker: Invalid idCpu (%u vs cCpus=%u)\n", idCpu, pVM->cCpus);
1063 return VERR_INVALID_PARAMETER;
1064 }
1065 }
1066 else if (RT_UNLIKELY(idCpu != NIL_VMCPUID))
1067 {
1068 SUPR0Printf("vmmR0EntryExWorker: Invalid idCpu=%u\n", idCpu);
1069 return VERR_INVALID_PARAMETER;
1070 }
1071
1072
1073 switch (enmOperation)
1074 {
1075 /*
1076 * GVM requests
1077 */
1078 case VMMR0_DO_GVMM_CREATE_VM:
1079 if (pVM || u64Arg || idCpu != NIL_VMCPUID)
1080 return VERR_INVALID_PARAMETER;
1081 return GVMMR0CreateVMReq((PGVMMCREATEVMREQ)pReqHdr);
1082
1083 case VMMR0_DO_GVMM_DESTROY_VM:
1084 if (pReqHdr || u64Arg)
1085 return VERR_INVALID_PARAMETER;
1086 return GVMMR0DestroyVM(pVM);
1087
1088 case VMMR0_DO_GVMM_REGISTER_VMCPU:
1089 {
1090 if (!pVM)
1091 return VERR_INVALID_PARAMETER;
1092 return GVMMR0RegisterVCpu(pVM, idCpu);
1093 }
1094
1095 case VMMR0_DO_GVMM_SCHED_HALT:
1096 if (pReqHdr)
1097 return VERR_INVALID_PARAMETER;
1098 return GVMMR0SchedHalt(pVM, idCpu, u64Arg);
1099
1100 case VMMR0_DO_GVMM_SCHED_WAKE_UP:
1101 if (pReqHdr || u64Arg)
1102 return VERR_INVALID_PARAMETER;
1103 return GVMMR0SchedWakeUp(pVM, idCpu);
1104
1105 case VMMR0_DO_GVMM_SCHED_POKE:
1106 if (pReqHdr || u64Arg)
1107 return VERR_INVALID_PARAMETER;
1108 return GVMMR0SchedPoke(pVM, idCpu);
1109
1110 case VMMR0_DO_GVMM_SCHED_WAKE_UP_AND_POKE_CPUS:
1111 if (u64Arg)
1112 return VERR_INVALID_PARAMETER;
1113 return GVMMR0SchedWakeUpAndPokeCpusReq(pVM, (PGVMMSCHEDWAKEUPANDPOKECPUSREQ)pReqHdr);
1114
1115 case VMMR0_DO_GVMM_SCHED_POLL:
1116 if (pReqHdr || u64Arg > 1)
1117 return VERR_INVALID_PARAMETER;
1118 return GVMMR0SchedPoll(pVM, idCpu, !!u64Arg);
1119
1120 case VMMR0_DO_GVMM_QUERY_STATISTICS:
1121 if (u64Arg)
1122 return VERR_INVALID_PARAMETER;
1123 return GVMMR0QueryStatisticsReq(pVM, (PGVMMQUERYSTATISTICSSREQ)pReqHdr);
1124
1125 case VMMR0_DO_GVMM_RESET_STATISTICS:
1126 if (u64Arg)
1127 return VERR_INVALID_PARAMETER;
1128 return GVMMR0ResetStatisticsReq(pVM, (PGVMMRESETSTATISTICSSREQ)pReqHdr);
1129
1130 /*
1131 * Initialize the R0 part of a VM instance.
1132 */
1133 case VMMR0_DO_VMMR0_INIT:
1134 return vmmR0InitVM(pVM, RT_LODWORD(u64Arg), RT_HIDWORD(u64Arg));
1135
1136 /*
1137 * Terminate the R0 part of a VM instance.
1138 */
1139 case VMMR0_DO_VMMR0_TERM:
1140 return VMMR0TermVM(pVM, NULL);
1141
1142 /*
1143 * Attempt to enable hm mode and check the current setting.
1144 */
1145 case VMMR0_DO_HM_ENABLE:
1146 return HMR0EnableAllCpus(pVM);
1147
1148 /*
1149 * Setup the hardware accelerated session.
1150 */
1151 case VMMR0_DO_HM_SETUP_VM:
1152 return HMR0SetupVM(pVM);
1153
1154 /*
1155 * Switch to RC to execute Hypervisor function.
1156 */
1157 case VMMR0_DO_CALL_HYPERVISOR:
1158 {
1159 int rc;
1160 bool fVTxDisabled;
1161
1162#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
1163 if (RT_UNLIKELY(!PGMGetHyperCR3(VMMGetCpu0(pVM))))
1164 return VERR_PGM_NO_CR3_SHADOW_ROOT;
1165#endif
1166
1167 RTCCUINTREG fFlags = ASMIntDisableFlags();
1168
1169#ifdef VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
1170 RTCPUID idHostCpu = RTMpCpuId();
1171 CPUMR0SetLApic(&pVM->aCpus[0], idHostCpu);
1172#endif
1173
1174 /* We might need to disable VT-x if the active switcher turns off paging. */
1175 rc = HMR0EnterSwitcher(pVM, pVM->vmm.s.enmSwitcher, &fVTxDisabled);
1176 if (RT_FAILURE(rc))
1177 return rc;
1178
1179 rc = pVM->vmm.s.pfnR0ToRawMode(pVM);
1180
1181 /* Re-enable VT-x if previously turned off. */
1182 HMR0LeaveSwitcher(pVM, fVTxDisabled);
1183
1184 /** @todo dispatch interrupts? */
1185 ASMSetFlags(fFlags);
1186 return rc;
1187 }
1188
1189 /*
1190 * PGM wrappers.
1191 */
1192 case VMMR0_DO_PGM_ALLOCATE_HANDY_PAGES:
1193 if (idCpu == NIL_VMCPUID)
1194 return VERR_INVALID_CPU_ID;
1195 return PGMR0PhysAllocateHandyPages(pVM, &pVM->aCpus[idCpu]);
1196
1197 case VMMR0_DO_PGM_FLUSH_HANDY_PAGES:
1198 if (idCpu == NIL_VMCPUID)
1199 return VERR_INVALID_CPU_ID;
1200 return PGMR0PhysFlushHandyPages(pVM, &pVM->aCpus[idCpu]);
1201
1202 case VMMR0_DO_PGM_ALLOCATE_LARGE_HANDY_PAGE:
1203 if (idCpu == NIL_VMCPUID)
1204 return VERR_INVALID_CPU_ID;
1205 return PGMR0PhysAllocateLargeHandyPage(pVM, &pVM->aCpus[idCpu]);
1206
1207 case VMMR0_DO_PGM_PHYS_SETUP_IOMMU:
1208 if (idCpu != 0)
1209 return VERR_INVALID_CPU_ID;
1210 return PGMR0PhysSetupIommu(pVM);
1211
1212 /*
1213 * GMM wrappers.
1214 */
1215 case VMMR0_DO_GMM_INITIAL_RESERVATION:
1216 if (u64Arg)
1217 return VERR_INVALID_PARAMETER;
1218 return GMMR0InitialReservationReq(pVM, idCpu, (PGMMINITIALRESERVATIONREQ)pReqHdr);
1219
1220 case VMMR0_DO_GMM_UPDATE_RESERVATION:
1221 if (u64Arg)
1222 return VERR_INVALID_PARAMETER;
1223 return GMMR0UpdateReservationReq(pVM, idCpu, (PGMMUPDATERESERVATIONREQ)pReqHdr);
1224
1225 case VMMR0_DO_GMM_ALLOCATE_PAGES:
1226 if (u64Arg)
1227 return VERR_INVALID_PARAMETER;
1228 return GMMR0AllocatePagesReq(pVM, idCpu, (PGMMALLOCATEPAGESREQ)pReqHdr);
1229
1230 case VMMR0_DO_GMM_FREE_PAGES:
1231 if (u64Arg)
1232 return VERR_INVALID_PARAMETER;
1233 return GMMR0FreePagesReq(pVM, idCpu, (PGMMFREEPAGESREQ)pReqHdr);
1234
1235 case VMMR0_DO_GMM_FREE_LARGE_PAGE:
1236 if (u64Arg)
1237 return VERR_INVALID_PARAMETER;
1238 return GMMR0FreeLargePageReq(pVM, idCpu, (PGMMFREELARGEPAGEREQ)pReqHdr);
1239
1240 case VMMR0_DO_GMM_QUERY_HYPERVISOR_MEM_STATS:
1241 if (u64Arg)
1242 return VERR_INVALID_PARAMETER;
1243 return GMMR0QueryHypervisorMemoryStatsReq(pVM, (PGMMMEMSTATSREQ)pReqHdr);
1244
1245 case VMMR0_DO_GMM_QUERY_MEM_STATS:
1246 if (idCpu == NIL_VMCPUID)
1247 return VERR_INVALID_CPU_ID;
1248 if (u64Arg)
1249 return VERR_INVALID_PARAMETER;
1250 return GMMR0QueryMemoryStatsReq(pVM, idCpu, (PGMMMEMSTATSREQ)pReqHdr);
1251
1252 case VMMR0_DO_GMM_BALLOONED_PAGES:
1253 if (u64Arg)
1254 return VERR_INVALID_PARAMETER;
1255 return GMMR0BalloonedPagesReq(pVM, idCpu, (PGMMBALLOONEDPAGESREQ)pReqHdr);
1256
1257 case VMMR0_DO_GMM_MAP_UNMAP_CHUNK:
1258 if (u64Arg)
1259 return VERR_INVALID_PARAMETER;
1260 return GMMR0MapUnmapChunkReq(pVM, (PGMMMAPUNMAPCHUNKREQ)pReqHdr);
1261
1262 case VMMR0_DO_GMM_SEED_CHUNK:
1263 if (pReqHdr)
1264 return VERR_INVALID_PARAMETER;
1265 return GMMR0SeedChunk(pVM, idCpu, (RTR3PTR)u64Arg);
1266
1267 case VMMR0_DO_GMM_REGISTER_SHARED_MODULE:
1268 if (idCpu == NIL_VMCPUID)
1269 return VERR_INVALID_CPU_ID;
1270 if (u64Arg)
1271 return VERR_INVALID_PARAMETER;
1272 return GMMR0RegisterSharedModuleReq(pVM, idCpu, (PGMMREGISTERSHAREDMODULEREQ)pReqHdr);
1273
1274 case VMMR0_DO_GMM_UNREGISTER_SHARED_MODULE:
1275 if (idCpu == NIL_VMCPUID)
1276 return VERR_INVALID_CPU_ID;
1277 if (u64Arg)
1278 return VERR_INVALID_PARAMETER;
1279 return GMMR0UnregisterSharedModuleReq(pVM, idCpu, (PGMMUNREGISTERSHAREDMODULEREQ)pReqHdr);
1280
1281 case VMMR0_DO_GMM_RESET_SHARED_MODULES:
1282 if (idCpu == NIL_VMCPUID)
1283 return VERR_INVALID_CPU_ID;
1284 if ( u64Arg
1285 || pReqHdr)
1286 return VERR_INVALID_PARAMETER;
1287 return GMMR0ResetSharedModules(pVM, idCpu);
1288
1289#ifdef VBOX_WITH_PAGE_SHARING
1290 case VMMR0_DO_GMM_CHECK_SHARED_MODULES:
1291 {
1292 if (idCpu == NIL_VMCPUID)
1293 return VERR_INVALID_CPU_ID;
1294 if ( u64Arg
1295 || pReqHdr)
1296 return VERR_INVALID_PARAMETER;
1297
1298 PVMCPU pVCpu = &pVM->aCpus[idCpu];
1299 Assert(pVCpu->hNativeThreadR0 == RTThreadNativeSelf());
1300
1301# ifdef DEBUG_sandervl
1302 /* 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). */
1303 /* Todo: this can have bad side effects for unexpected jumps back to r3. */
1304 int rc = GMMR0CheckSharedModulesStart(pVM);
1305 if (rc == VINF_SUCCESS)
1306 {
1307 rc = vmmR0CallRing3SetJmp(&pVCpu->vmm.s.CallRing3JmpBufR0, GMMR0CheckSharedModules, pVM, pVCpu); /* this may resume code. */
1308 Assert( rc == VINF_SUCCESS
1309 || (rc == VINF_VMM_CALL_HOST && pVCpu->vmm.s.enmCallRing3Operation == VMMCALLRING3_VMM_LOGGER_FLUSH));
1310 GMMR0CheckSharedModulesEnd(pVM);
1311 }
1312# else
1313 int rc = GMMR0CheckSharedModules(pVM, pVCpu);
1314# endif
1315 return rc;
1316 }
1317#endif
1318
1319#if defined(VBOX_STRICT) && HC_ARCH_BITS == 64
1320 case VMMR0_DO_GMM_FIND_DUPLICATE_PAGE:
1321 if (u64Arg)
1322 return VERR_INVALID_PARAMETER;
1323 return GMMR0FindDuplicatePageReq(pVM, (PGMMFINDDUPLICATEPAGEREQ)pReqHdr);
1324#endif
1325
1326 case VMMR0_DO_GMM_QUERY_STATISTICS:
1327 if (u64Arg)
1328 return VERR_INVALID_PARAMETER;
1329 return GMMR0QueryStatisticsReq(pVM, (PGMMQUERYSTATISTICSSREQ)pReqHdr);
1330
1331 case VMMR0_DO_GMM_RESET_STATISTICS:
1332 if (u64Arg)
1333 return VERR_INVALID_PARAMETER;
1334 return GMMR0ResetStatisticsReq(pVM, (PGMMRESETSTATISTICSSREQ)pReqHdr);
1335
1336 /*
1337 * A quick GCFGM mock-up.
1338 */
1339 /** @todo GCFGM with proper access control, ring-3 management interface and all that. */
1340 case VMMR0_DO_GCFGM_SET_VALUE:
1341 case VMMR0_DO_GCFGM_QUERY_VALUE:
1342 {
1343 if (pVM || !pReqHdr || u64Arg || idCpu != NIL_VMCPUID)
1344 return VERR_INVALID_PARAMETER;
1345 PGCFGMVALUEREQ pReq = (PGCFGMVALUEREQ)pReqHdr;
1346 if (pReq->Hdr.cbReq != sizeof(*pReq))
1347 return VERR_INVALID_PARAMETER;
1348 int rc;
1349 if (enmOperation == VMMR0_DO_GCFGM_SET_VALUE)
1350 {
1351 rc = GVMMR0SetConfig(pReq->pSession, &pReq->szName[0], pReq->u64Value);
1352 //if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1353 // rc = GMMR0SetConfig(pReq->pSession, &pReq->szName[0], pReq->u64Value);
1354 }
1355 else
1356 {
1357 rc = GVMMR0QueryConfig(pReq->pSession, &pReq->szName[0], &pReq->u64Value);
1358 //if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1359 // rc = GMMR0QueryConfig(pReq->pSession, &pReq->szName[0], &pReq->u64Value);
1360 }
1361 return rc;
1362 }
1363
1364 /*
1365 * PDM Wrappers.
1366 */
1367 case VMMR0_DO_PDM_DRIVER_CALL_REQ_HANDLER:
1368 {
1369 if (!pVM || !pReqHdr || u64Arg || idCpu != NIL_VMCPUID)
1370 return VERR_INVALID_PARAMETER;
1371 return PDMR0DriverCallReqHandler(pVM, (PPDMDRIVERCALLREQHANDLERREQ)pReqHdr);
1372 }
1373
1374 case VMMR0_DO_PDM_DEVICE_CALL_REQ_HANDLER:
1375 {
1376 if (!pVM || !pReqHdr || u64Arg || idCpu != NIL_VMCPUID)
1377 return VERR_INVALID_PARAMETER;
1378 return PDMR0DeviceCallReqHandler(pVM, (PPDMDEVICECALLREQHANDLERREQ)pReqHdr);
1379 }
1380
1381 /*
1382 * Requests to the internal networking service.
1383 */
1384 case VMMR0_DO_INTNET_OPEN:
1385 {
1386 PINTNETOPENREQ pReq = (PINTNETOPENREQ)pReqHdr;
1387 if (u64Arg || !pReq || !vmmR0IsValidSession(pVM, pReq->pSession, pSession) || idCpu != NIL_VMCPUID)
1388 return VERR_INVALID_PARAMETER;
1389 return IntNetR0OpenReq(pSession, pReq);
1390 }
1391
1392 case VMMR0_DO_INTNET_IF_CLOSE:
1393 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFCLOSEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1394 return VERR_INVALID_PARAMETER;
1395 return IntNetR0IfCloseReq(pSession, (PINTNETIFCLOSEREQ)pReqHdr);
1396
1397 case VMMR0_DO_INTNET_IF_GET_BUFFER_PTRS:
1398 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFGETBUFFERPTRSREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1399 return VERR_INVALID_PARAMETER;
1400 return IntNetR0IfGetBufferPtrsReq(pSession, (PINTNETIFGETBUFFERPTRSREQ)pReqHdr);
1401
1402 case VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE:
1403 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETPROMISCUOUSMODEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1404 return VERR_INVALID_PARAMETER;
1405 return IntNetR0IfSetPromiscuousModeReq(pSession, (PINTNETIFSETPROMISCUOUSMODEREQ)pReqHdr);
1406
1407 case VMMR0_DO_INTNET_IF_SET_MAC_ADDRESS:
1408 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETMACADDRESSREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1409 return VERR_INVALID_PARAMETER;
1410 return IntNetR0IfSetMacAddressReq(pSession, (PINTNETIFSETMACADDRESSREQ)pReqHdr);
1411
1412 case VMMR0_DO_INTNET_IF_SET_ACTIVE:
1413 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETACTIVEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1414 return VERR_INVALID_PARAMETER;
1415 return IntNetR0IfSetActiveReq(pSession, (PINTNETIFSETACTIVEREQ)pReqHdr);
1416
1417 case VMMR0_DO_INTNET_IF_SEND:
1418 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSENDREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1419 return VERR_INVALID_PARAMETER;
1420 return IntNetR0IfSendReq(pSession, (PINTNETIFSENDREQ)pReqHdr);
1421
1422 case VMMR0_DO_INTNET_IF_WAIT:
1423 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFWAITREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1424 return VERR_INVALID_PARAMETER;
1425 return IntNetR0IfWaitReq(pSession, (PINTNETIFWAITREQ)pReqHdr);
1426
1427 case VMMR0_DO_INTNET_IF_ABORT_WAIT:
1428 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFWAITREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1429 return VERR_INVALID_PARAMETER;
1430 return IntNetR0IfAbortWaitReq(pSession, (PINTNETIFABORTWAITREQ)pReqHdr);
1431
1432#ifdef VBOX_WITH_PCI_PASSTHROUGH
1433 /*
1434 * Requests to host PCI driver service.
1435 */
1436 case VMMR0_DO_PCIRAW_REQ:
1437 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PPCIRAWSENDREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1438 return VERR_INVALID_PARAMETER;
1439 return PciRawR0ProcessReq(pSession, pVM, (PPCIRAWSENDREQ)pReqHdr);
1440#endif
1441 /*
1442 * For profiling.
1443 */
1444 case VMMR0_DO_NOP:
1445 case VMMR0_DO_SLOW_NOP:
1446 return VINF_SUCCESS;
1447
1448 /*
1449 * For testing Ring-0 APIs invoked in this environment.
1450 */
1451 case VMMR0_DO_TESTS:
1452 /** @todo make new test */
1453 return VINF_SUCCESS;
1454
1455
1456#if HC_ARCH_BITS == 32 && defined(VBOX_WITH_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
1457 case VMMR0_DO_TEST_SWITCHER3264:
1458 if (idCpu == NIL_VMCPUID)
1459 return VERR_INVALID_CPU_ID;
1460 return HMR0TestSwitcher3264(pVM);
1461#endif
1462 default:
1463 /*
1464 * We're returning VERR_NOT_SUPPORT here so we've got something else
1465 * than -1 which the interrupt gate glue code might return.
1466 */
1467 Log(("operation %#x is not supported\n", enmOperation));
1468 return VERR_NOT_SUPPORTED;
1469 }
1470}
1471
1472
1473/**
1474 * Argument for vmmR0EntryExWrapper containing the arguments for VMMR0EntryEx.
1475 */
1476typedef struct VMMR0ENTRYEXARGS
1477{
1478 PVM pVM;
1479 VMCPUID idCpu;
1480 VMMR0OPERATION enmOperation;
1481 PSUPVMMR0REQHDR pReq;
1482 uint64_t u64Arg;
1483 PSUPDRVSESSION pSession;
1484} VMMR0ENTRYEXARGS;
1485/** Pointer to a vmmR0EntryExWrapper argument package. */
1486typedef VMMR0ENTRYEXARGS *PVMMR0ENTRYEXARGS;
1487
1488/**
1489 * This is just a longjmp wrapper function for VMMR0EntryEx calls.
1490 *
1491 * @returns VBox status code.
1492 * @param pvArgs The argument package
1493 */
1494static DECLCALLBACK(int) vmmR0EntryExWrapper(void *pvArgs)
1495{
1496 return vmmR0EntryExWorker(((PVMMR0ENTRYEXARGS)pvArgs)->pVM,
1497 ((PVMMR0ENTRYEXARGS)pvArgs)->idCpu,
1498 ((PVMMR0ENTRYEXARGS)pvArgs)->enmOperation,
1499 ((PVMMR0ENTRYEXARGS)pvArgs)->pReq,
1500 ((PVMMR0ENTRYEXARGS)pvArgs)->u64Arg,
1501 ((PVMMR0ENTRYEXARGS)pvArgs)->pSession);
1502}
1503
1504
1505/**
1506 * The Ring 0 entry point, called by the support library (SUP).
1507 *
1508 * @returns VBox status code.
1509 * @param pVM Pointer to the VM.
1510 * @param idCpu Virtual CPU ID argument. Must be NIL_VMCPUID if pVM
1511 * is NIL_RTR0PTR, and may be NIL_VMCPUID if it isn't
1512 * @param enmOperation Which operation to execute.
1513 * @param pReq Pointer to the SUPVMMR0REQHDR packet. Optional.
1514 * @param u64Arg Some simple constant argument.
1515 * @param pSession The session of the caller.
1516 * @remarks Assume called with interrupts _enabled_.
1517 */
1518VMMR0DECL(int) VMMR0EntryEx(PVM pVM, VMCPUID idCpu, VMMR0OPERATION enmOperation, PSUPVMMR0REQHDR pReq, uint64_t u64Arg, PSUPDRVSESSION pSession)
1519{
1520 /*
1521 * Requests that should only happen on the EMT thread will be
1522 * wrapped in a setjmp so we can assert without causing trouble.
1523 */
1524 if ( VALID_PTR(pVM)
1525 && pVM->pVMR0
1526 && idCpu < pVM->cCpus)
1527 {
1528 switch (enmOperation)
1529 {
1530 /* These might/will be called before VMMR3Init. */
1531 case VMMR0_DO_GMM_INITIAL_RESERVATION:
1532 case VMMR0_DO_GMM_UPDATE_RESERVATION:
1533 case VMMR0_DO_GMM_ALLOCATE_PAGES:
1534 case VMMR0_DO_GMM_FREE_PAGES:
1535 case VMMR0_DO_GMM_BALLOONED_PAGES:
1536 /* On the mac we might not have a valid jmp buf, so check these as well. */
1537 case VMMR0_DO_VMMR0_INIT:
1538 case VMMR0_DO_VMMR0_TERM:
1539 {
1540 PVMCPU pVCpu = &pVM->aCpus[idCpu];
1541
1542 if (!pVCpu->vmm.s.CallRing3JmpBufR0.pvSavedStack)
1543 break;
1544
1545 /** @todo validate this EMT claim... GVM knows. */
1546 VMMR0ENTRYEXARGS Args;
1547 Args.pVM = pVM;
1548 Args.idCpu = idCpu;
1549 Args.enmOperation = enmOperation;
1550 Args.pReq = pReq;
1551 Args.u64Arg = u64Arg;
1552 Args.pSession = pSession;
1553 return vmmR0CallRing3SetJmpEx(&pVCpu->vmm.s.CallRing3JmpBufR0, vmmR0EntryExWrapper, &Args);
1554 }
1555
1556 default:
1557 break;
1558 }
1559 }
1560 return vmmR0EntryExWorker(pVM, idCpu, enmOperation, pReq, u64Arg, pSession);
1561}
1562
1563
1564/**
1565 * Checks whether we've armed the ring-0 long jump machinery.
1566 *
1567 * @returns @c true / @c false
1568 * @param pVCpu Pointer to the VMCPU.
1569 * @thread EMT
1570 * @sa VMMIsLongJumpArmed
1571 */
1572VMMR0_INT_DECL(bool) VMMR0IsLongJumpArmed(PVMCPU pVCpu)
1573{
1574#ifdef RT_ARCH_X86
1575 return pVCpu->vmm.s.CallRing3JmpBufR0.eip
1576 && !pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call;
1577#else
1578 return pVCpu->vmm.s.CallRing3JmpBufR0.rip
1579 && !pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call;
1580#endif
1581}
1582
1583
1584/**
1585 * Checks whether we've done a ring-3 long jump.
1586 *
1587 * @returns @c true / @c false
1588 * @param pVCpu Pointer to the VMCPU.
1589 * @thread EMT
1590 */
1591VMMR0_INT_DECL(bool) VMMR0IsInRing3LongJump(PVMCPU pVCpu)
1592{
1593 return pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call;
1594}
1595
1596
1597/**
1598 * Internal R0 logger worker: Flush logger.
1599 *
1600 * @param pLogger The logger instance to flush.
1601 * @remark This function must be exported!
1602 */
1603VMMR0DECL(void) vmmR0LoggerFlush(PRTLOGGER pLogger)
1604{
1605#ifdef LOG_ENABLED
1606 /*
1607 * Convert the pLogger into a VM handle and 'call' back to Ring-3.
1608 * (This is a bit paranoid code.)
1609 */
1610 PVMMR0LOGGER pR0Logger = (PVMMR0LOGGER)((uintptr_t)pLogger - RT_OFFSETOF(VMMR0LOGGER, Logger));
1611 if ( !VALID_PTR(pR0Logger)
1612 || !VALID_PTR(pR0Logger + 1)
1613 || pLogger->u32Magic != RTLOGGER_MAGIC)
1614 {
1615# ifdef DEBUG
1616 SUPR0Printf("vmmR0LoggerFlush: pLogger=%p!\n", pLogger);
1617# endif
1618 return;
1619 }
1620 if (pR0Logger->fFlushingDisabled)
1621 return; /* quietly */
1622
1623 PVM pVM = pR0Logger->pVM;
1624 if ( !VALID_PTR(pVM)
1625 || pVM->pVMR0 != pVM)
1626 {
1627# ifdef DEBUG
1628 SUPR0Printf("vmmR0LoggerFlush: pVM=%p! pVMR0=%p! pLogger=%p\n", pVM, pVM->pVMR0, pLogger);
1629# endif
1630 return;
1631 }
1632
1633 PVMCPU pVCpu = VMMGetCpu(pVM);
1634 if (pVCpu)
1635 {
1636 /*
1637 * Check that the jump buffer is armed.
1638 */
1639# ifdef RT_ARCH_X86
1640 if ( !pVCpu->vmm.s.CallRing3JmpBufR0.eip
1641 || pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call)
1642# else
1643 if ( !pVCpu->vmm.s.CallRing3JmpBufR0.rip
1644 || pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call)
1645# endif
1646 {
1647# ifdef DEBUG
1648 SUPR0Printf("vmmR0LoggerFlush: Jump buffer isn't armed!\n");
1649# endif
1650 return;
1651 }
1652 VMMRZCallRing3(pVM, pVCpu, VMMCALLRING3_VMM_LOGGER_FLUSH, 0);
1653 }
1654# ifdef DEBUG
1655 else
1656 SUPR0Printf("vmmR0LoggerFlush: invalid VCPU context!\n");
1657# endif
1658#endif
1659}
1660
1661/**
1662 * Internal R0 logger worker: Custom prefix.
1663 *
1664 * @returns Number of chars written.
1665 *
1666 * @param pLogger The logger instance.
1667 * @param pchBuf The output buffer.
1668 * @param cchBuf The size of the buffer.
1669 * @param pvUser User argument (ignored).
1670 */
1671VMMR0DECL(size_t) vmmR0LoggerPrefix(PRTLOGGER pLogger, char *pchBuf, size_t cchBuf, void *pvUser)
1672{
1673 NOREF(pvUser);
1674#ifdef LOG_ENABLED
1675 PVMMR0LOGGER pR0Logger = (PVMMR0LOGGER)((uintptr_t)pLogger - RT_OFFSETOF(VMMR0LOGGER, Logger));
1676 if ( !VALID_PTR(pR0Logger)
1677 || !VALID_PTR(pR0Logger + 1)
1678 || pLogger->u32Magic != RTLOGGER_MAGIC
1679 || cchBuf < 2)
1680 return 0;
1681
1682 static const char s_szHex[17] = "0123456789abcdef";
1683 VMCPUID const idCpu = pR0Logger->idCpu;
1684 pchBuf[1] = s_szHex[ idCpu & 15];
1685 pchBuf[0] = s_szHex[(idCpu >> 4) & 15];
1686
1687 return 2;
1688#else
1689 return 0;
1690#endif
1691}
1692
1693#ifdef LOG_ENABLED
1694
1695/**
1696 * Disables flushing of the ring-0 debug log.
1697 *
1698 * @param pVCpu Pointer to the VMCPU.
1699 */
1700VMMR0DECL(void) VMMR0LogFlushDisable(PVMCPU pVCpu)
1701{
1702 if (pVCpu->vmm.s.pR0LoggerR0)
1703 pVCpu->vmm.s.pR0LoggerR0->fFlushingDisabled = true;
1704}
1705
1706
1707/**
1708 * Enables flushing of the ring-0 debug log.
1709 *
1710 * @param pVCpu Pointer to the VMCPU.
1711 */
1712VMMR0DECL(void) VMMR0LogFlushEnable(PVMCPU pVCpu)
1713{
1714 if (pVCpu->vmm.s.pR0LoggerR0)
1715 pVCpu->vmm.s.pR0LoggerR0->fFlushingDisabled = false;
1716}
1717
1718
1719/**
1720 * Checks if log flushing is disabled or not.
1721 *
1722 * @param pVCpu Pointer to the VMCPU.
1723 */
1724VMMR0DECL(bool) VMMR0IsLogFlushDisabled(PVMCPU pVCpu)
1725{
1726 if (pVCpu->vmm.s.pR0LoggerR0)
1727 return pVCpu->vmm.s.pR0LoggerR0->fFlushingDisabled;
1728 return true;
1729}
1730#endif /* LOG_ENABLED */
1731
1732/**
1733 * Jump back to ring-3 if we're the EMT and the longjmp is armed.
1734 *
1735 * @returns true if the breakpoint should be hit, false if it should be ignored.
1736 */
1737DECLEXPORT(bool) RTCALL RTAssertShouldPanic(void)
1738{
1739#if 0
1740 return true;
1741#else
1742 PVM pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
1743 if (pVM)
1744 {
1745 PVMCPU pVCpu = VMMGetCpu(pVM);
1746
1747 if (pVCpu)
1748 {
1749#ifdef RT_ARCH_X86
1750 if ( pVCpu->vmm.s.CallRing3JmpBufR0.eip
1751 && !pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call)
1752#else
1753 if ( pVCpu->vmm.s.CallRing3JmpBufR0.rip
1754 && !pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call)
1755#endif
1756 {
1757 int rc = VMMRZCallRing3(pVM, pVCpu, VMMCALLRING3_VM_R0_ASSERTION, 0);
1758 return RT_FAILURE_NP(rc);
1759 }
1760 }
1761 }
1762#ifdef RT_OS_LINUX
1763 return true;
1764#else
1765 return false;
1766#endif
1767#endif
1768}
1769
1770
1771/**
1772 * Override this so we can push it up to ring-3.
1773 *
1774 * @param pszExpr Expression. Can be NULL.
1775 * @param uLine Location line number.
1776 * @param pszFile Location file name.
1777 * @param pszFunction Location function name.
1778 */
1779DECLEXPORT(void) RTCALL RTAssertMsg1Weak(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
1780{
1781 /*
1782 * To the log.
1783 */
1784 LogAlways(("\n!!R0-Assertion Failed!!\n"
1785 "Expression: %s\n"
1786 "Location : %s(%d) %s\n",
1787 pszExpr, pszFile, uLine, pszFunction));
1788
1789 /*
1790 * To the global VMM buffer.
1791 */
1792 PVM pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
1793 if (pVM)
1794 RTStrPrintf(pVM->vmm.s.szRing0AssertMsg1, sizeof(pVM->vmm.s.szRing0AssertMsg1),
1795 "\n!!R0-Assertion Failed!!\n"
1796 "Expression: %s\n"
1797 "Location : %s(%d) %s\n",
1798 pszExpr, pszFile, uLine, pszFunction);
1799
1800 /*
1801 * Continue the normal way.
1802 */
1803 RTAssertMsg1(pszExpr, uLine, pszFile, pszFunction);
1804}
1805
1806
1807/**
1808 * Callback for RTLogFormatV which writes to the ring-3 log port.
1809 * See PFNLOGOUTPUT() for details.
1810 */
1811static DECLCALLBACK(size_t) rtLogOutput(void *pv, const char *pachChars, size_t cbChars)
1812{
1813 for (size_t i = 0; i < cbChars; i++)
1814 LogAlways(("%c", pachChars[i]));
1815
1816 NOREF(pv);
1817 return cbChars;
1818}
1819
1820
1821/**
1822 * Override this so we can push it up to ring-3.
1823 *
1824 * @param pszFormat The format string.
1825 * @param va Arguments.
1826 */
1827DECLEXPORT(void) RTCALL RTAssertMsg2WeakV(const char *pszFormat, va_list va)
1828{
1829 va_list vaCopy;
1830
1831 /*
1832 * Push the message to the loggers.
1833 */
1834 PRTLOGGER pLog = RTLogGetDefaultInstance(); /* Don't initialize it here... */
1835 if (pLog)
1836 {
1837 va_copy(vaCopy, va);
1838 RTLogFormatV(rtLogOutput, pLog, pszFormat, vaCopy);
1839 va_end(vaCopy);
1840 }
1841 pLog = RTLogRelDefaultInstance();
1842 if (pLog)
1843 {
1844 va_copy(vaCopy, va);
1845 RTLogFormatV(rtLogOutput, pLog, pszFormat, vaCopy);
1846 va_end(vaCopy);
1847 }
1848
1849 /*
1850 * Push it to the global VMM buffer.
1851 */
1852 PVM pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
1853 if (pVM)
1854 {
1855 va_copy(vaCopy, va);
1856 RTStrPrintfV(pVM->vmm.s.szRing0AssertMsg2, sizeof(pVM->vmm.s.szRing0AssertMsg2), pszFormat, vaCopy);
1857 va_end(vaCopy);
1858 }
1859
1860 /*
1861 * Continue the normal way.
1862 */
1863 RTAssertMsg2V(pszFormat, va);
1864}
1865
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