VirtualBox

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

Last change on this file since 49284 was 49215, checked in by vboxsync, 11 years ago

updates

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