VirtualBox

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

Last change on this file since 47080 was 46861, checked in by vboxsync, 12 years ago

Don't allow mixing builds.

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