VirtualBox

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

Last change on this file since 48220 was 48201, checked in by vboxsync, 11 years ago

VMM: Re-enable thread-context hooks on Linux, Solaris.

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