VirtualBox

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

Last change on this file since 48025 was 48006, checked in by vboxsync, 11 years ago

VMMR0ThreadCtxHooksCreate: Disabled the preemption hooks on solaris for the moment as the driver seems unwilling to unload after they were enabled.

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