VirtualBox

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

Last change on this file since 41906 was 41836, checked in by vboxsync, 12 years ago

Doxygen.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 54.5 KB
Line 
1/* $Id: VMMR0.cpp 41836 2012-06-19 16:20:52Z vboxsync $ */
2/** @file
3 * VMM - Host Context Ring 0.
4 */
5
6/*
7 * Copyright (C) 2006-2011 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/hwaccm.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, HWACCM, 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 = HWACCMR0Init();
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#ifdef VBOX_WITH_PCI_PASSTHROUGH
169 PciRawR0Term();
170#endif
171 }
172 IntNetR0Term();
173 }
174#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
175 PGMR0DynMapTerm();
176#endif
177 }
178 PGMDeregisterStringFormatTypes();
179 }
180 HWACCMR0Term();
181 }
182 GMMR0Term();
183 }
184 GVMMR0Term();
185 }
186 vmmTermFormatTypes();
187 }
188
189 LogFlow(("ModuleInit: failed %Rrc\n", rc));
190 return rc;
191}
192
193
194/**
195 * Terminate the module.
196 * This is called when we're finally unloaded.
197 *
198 * @param hMod Image handle for use in APIs.
199 */
200DECLEXPORT(void) ModuleTerm(void *hMod)
201{
202 LogFlow(("ModuleTerm:\n"));
203
204 /*
205 * Terminate the CPUM module (Local APIC cleanup).
206 */
207 CPUMR0ModuleTerm();
208
209 /*
210 * Terminate the internal network service.
211 */
212 IntNetR0Term();
213
214 /*
215 * PGM (Darwin), HWACCM and PciRaw global cleanup.
216 */
217#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
218 PGMR0DynMapTerm();
219#endif
220#ifdef VBOX_WITH_PCI_PASSTHROUGH
221 PciRawR0Term();
222#endif
223 PGMDeregisterStringFormatTypes();
224 HWACCMR0Term();
225#ifdef VBOX_WITH_TRIPLE_FAULT_HACK
226 vmmR0TripleFaultHackTerm();
227#endif
228
229 /*
230 * Destroy the GMM and GVMM instances.
231 */
232 GMMR0Term();
233 GVMMR0Term();
234
235 vmmTermFormatTypes();
236
237 LogFlow(("ModuleTerm: returns\n"));
238}
239
240
241/**
242 * Initiates the R0 driver for a particular VM instance.
243 *
244 * @returns VBox status code.
245 *
246 * @param pVM Pointer to the VM.
247 * @param uSvnRev The SVN revision of the ring-3 part.
248 * @thread EMT.
249 */
250static int vmmR0InitVM(PVM pVM, uint32_t uSvnRev)
251{
252 /*
253 * Match the SVN revisions.
254 */
255 if (uSvnRev != VMMGetSvnRev())
256 {
257 LogRel(("VMMR0InitVM: Revision mismatch, r3=%d r0=%d\n", uSvnRev, VMMGetSvnRev()));
258 SUPR0Printf("VMMR0InitVM: Revision mismatch, r3=%d r0=%d\n", uSvnRev, VMMGetSvnRev());
259 return VERR_VMM_R0_VERSION_MISMATCH;
260 }
261 if ( !VALID_PTR(pVM)
262 || pVM->pVMR0 != pVM)
263 return VERR_INVALID_PARAMETER;
264
265#ifdef LOG_ENABLED
266 /*
267 * Register the EMT R0 logger instance for VCPU 0.
268 */
269 PVMCPU pVCpu = &pVM->aCpus[0];
270
271 PVMMR0LOGGER pR0Logger = pVCpu->vmm.s.pR0LoggerR0;
272 if (pR0Logger)
273 {
274# if 0 /* testing of the logger. */
275 LogCom(("vmmR0InitVM: before %p\n", RTLogDefaultInstance()));
276 LogCom(("vmmR0InitVM: pfnFlush=%p actual=%p\n", pR0Logger->Logger.pfnFlush, vmmR0LoggerFlush));
277 LogCom(("vmmR0InitVM: pfnLogger=%p actual=%p\n", pR0Logger->Logger.pfnLogger, vmmR0LoggerWrapper));
278 LogCom(("vmmR0InitVM: offScratch=%d fFlags=%#x fDestFlags=%#x\n", pR0Logger->Logger.offScratch, pR0Logger->Logger.fFlags, pR0Logger->Logger.fDestFlags));
279
280 RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
281 LogCom(("vmmR0InitVM: after %p reg\n", RTLogDefaultInstance()));
282 RTLogSetDefaultInstanceThread(NULL, pVM->pSession);
283 LogCom(("vmmR0InitVM: after %p dereg\n", RTLogDefaultInstance()));
284
285 pR0Logger->Logger.pfnLogger("hello ring-0 logger\n");
286 LogCom(("vmmR0InitVM: returned successfully from direct logger call.\n"));
287 pR0Logger->Logger.pfnFlush(&pR0Logger->Logger);
288 LogCom(("vmmR0InitVM: returned successfully from direct flush call.\n"));
289
290 RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
291 LogCom(("vmmR0InitVM: after %p reg2\n", RTLogDefaultInstance()));
292 pR0Logger->Logger.pfnLogger("hello ring-0 logger\n");
293 LogCom(("vmmR0InitVM: returned successfully from direct logger call (2). offScratch=%d\n", pR0Logger->Logger.offScratch));
294 RTLogSetDefaultInstanceThread(NULL, pVM->pSession);
295 LogCom(("vmmR0InitVM: after %p dereg2\n", RTLogDefaultInstance()));
296
297 RTLogLoggerEx(&pR0Logger->Logger, 0, ~0U, "hello ring-0 logger (RTLogLoggerEx)\n");
298 LogCom(("vmmR0InitVM: RTLogLoggerEx returned fine offScratch=%d\n", pR0Logger->Logger.offScratch));
299
300 RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
301 RTLogPrintf("hello ring-0 logger (RTLogPrintf)\n");
302 LogCom(("vmmR0InitVM: RTLogPrintf returned fine offScratch=%d\n", pR0Logger->Logger.offScratch));
303# endif
304 Log(("Switching to per-thread logging instance %p (key=%p)\n", &pR0Logger->Logger, pVM->pSession));
305 RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
306 pR0Logger->fRegistered = true;
307 }
308#endif /* LOG_ENABLED */
309
310 /*
311 * Check if the host supports high resolution timers or not.
312 */
313 if ( pVM->vmm.s.fUsePeriodicPreemptionTimers
314 && !RTTimerCanDoHighResolution())
315 pVM->vmm.s.fUsePeriodicPreemptionTimers = false;
316
317 /*
318 * Initialize the per VM data for GVMM and GMM.
319 */
320 int rc = GVMMR0InitVM(pVM);
321// if (RT_SUCCESS(rc))
322// rc = GMMR0InitPerVMData(pVM);
323 if (RT_SUCCESS(rc))
324 {
325 /*
326 * Init HWACCM, CPUM and PGM (Darwin only).
327 */
328 rc = HWACCMR0InitVM(pVM);
329 if (RT_SUCCESS(rc))
330 {
331 rc = CPUMR0Init(pVM); /** @todo rename to CPUMR0InitVM */
332 if (RT_SUCCESS(rc))
333 {
334#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
335 rc = PGMR0DynMapInitVM(pVM);
336#endif
337 if (RT_SUCCESS(rc))
338 {
339#ifdef VBOX_WITH_PCI_PASSTHROUGH
340 rc = PciRawR0InitVM(pVM);
341#endif
342 if (RT_SUCCESS(rc))
343 {
344 GVMMR0DoneInitVM(pVM);
345 return rc;
346 }
347 }
348
349 /* bail out */
350 }
351#ifdef VBOX_WITH_PCI_PASSTHROUGH
352 PciRawR0TermVM(pVM);
353#endif
354 HWACCMR0TermVM(pVM);
355 }
356 }
357
358
359 RTLogSetDefaultInstanceThread(NULL, (uintptr_t)pVM->pSession);
360 return rc;
361}
362
363
364/**
365 * Terminates the R0 driver for a particular VM instance.
366 *
367 * This is normally called by ring-3 as part of the VM termination process, but
368 * may alternatively be called during the support driver session cleanup when
369 * the VM object is destroyed (see GVMM).
370 *
371 * @returns VBox status code.
372 *
373 * @param pVM Pointer to the VM.
374 * @param pGVM Pointer to the global VM structure. Optional.
375 * @thread EMT or session clean up thread.
376 */
377VMMR0DECL(int) VMMR0TermVM(PVM pVM, PGVM pGVM)
378{
379#ifdef VBOX_WITH_PCI_PASSTHROUGH
380 PciRawR0TermVM(pVM);
381#endif
382
383 /*
384 * Tell GVMM what we're up to and check that we only do this once.
385 */
386 if (GVMMR0DoingTermVM(pVM, pGVM))
387 {
388#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
389 PGMR0DynMapTermVM(pVM);
390#endif
391 HWACCMR0TermVM(pVM);
392 }
393
394 /*
395 * Deregister the logger.
396 */
397 RTLogSetDefaultInstanceThread(NULL, (uintptr_t)pVM->pSession);
398 return VINF_SUCCESS;
399}
400
401
402#ifdef VBOX_WITH_STATISTICS
403/**
404 * Record return code statistics
405 * @param pVM Pointer to the VM.
406 * @param pVCpu Pointer to the VMCPU.
407 * @param rc The status code.
408 */
409static void vmmR0RecordRC(PVM pVM, PVMCPU pVCpu, int rc)
410{
411 /*
412 * Collect statistics.
413 */
414 switch (rc)
415 {
416 case VINF_SUCCESS:
417 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetNormal);
418 break;
419 case VINF_EM_RAW_INTERRUPT:
420 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetInterrupt);
421 break;
422 case VINF_EM_RAW_INTERRUPT_HYPER:
423 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetInterruptHyper);
424 break;
425 case VINF_EM_RAW_GUEST_TRAP:
426 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetGuestTrap);
427 break;
428 case VINF_EM_RAW_RING_SWITCH:
429 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetRingSwitch);
430 break;
431 case VINF_EM_RAW_RING_SWITCH_INT:
432 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetRingSwitchInt);
433 break;
434 case VINF_EM_RAW_STALE_SELECTOR:
435 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetStaleSelector);
436 break;
437 case VINF_EM_RAW_IRET_TRAP:
438 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIRETTrap);
439 break;
440 case VINF_IOM_R3_IOPORT_READ:
441 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIORead);
442 break;
443 case VINF_IOM_R3_IOPORT_WRITE:
444 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIOWrite);
445 break;
446 case VINF_IOM_R3_MMIO_READ:
447 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIORead);
448 break;
449 case VINF_IOM_R3_MMIO_WRITE:
450 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOWrite);
451 break;
452 case VINF_IOM_R3_MMIO_READ_WRITE:
453 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOReadWrite);
454 break;
455 case VINF_PATM_HC_MMIO_PATCH_READ:
456 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOPatchRead);
457 break;
458 case VINF_PATM_HC_MMIO_PATCH_WRITE:
459 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOPatchWrite);
460 break;
461 case VINF_EM_RAW_EMULATE_INSTR:
462 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetEmulate);
463 break;
464 case VINF_EM_RAW_EMULATE_IO_BLOCK:
465 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIOBlockEmulate);
466 break;
467 case VINF_PATCH_EMULATE_INSTR:
468 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchEmulate);
469 break;
470 case VINF_EM_RAW_EMULATE_INSTR_LDT_FAULT:
471 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetLDTFault);
472 break;
473 case VINF_EM_RAW_EMULATE_INSTR_GDT_FAULT:
474 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetGDTFault);
475 break;
476 case VINF_EM_RAW_EMULATE_INSTR_IDT_FAULT:
477 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIDTFault);
478 break;
479 case VINF_EM_RAW_EMULATE_INSTR_TSS_FAULT:
480 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetTSSFault);
481 break;
482 case VINF_EM_RAW_EMULATE_INSTR_PD_FAULT:
483 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPDFault);
484 break;
485 case VINF_CSAM_PENDING_ACTION:
486 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetCSAMTask);
487 break;
488 case VINF_PGM_SYNC_CR3:
489 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetSyncCR3);
490 break;
491 case VINF_PATM_PATCH_INT3:
492 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchInt3);
493 break;
494 case VINF_PATM_PATCH_TRAP_PF:
495 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchPF);
496 break;
497 case VINF_PATM_PATCH_TRAP_GP:
498 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchGP);
499 break;
500 case VINF_PATM_PENDING_IRQ_AFTER_IRET:
501 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchIretIRQ);
502 break;
503 case VINF_EM_RESCHEDULE_REM:
504 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetRescheduleREM);
505 break;
506 case VINF_EM_RAW_TO_R3:
507 if (VM_FF_ISPENDING(pVM, VM_FF_TM_VIRTUAL_SYNC))
508 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3TMVirt);
509 else if (VM_FF_ISPENDING(pVM, VM_FF_PGM_NEED_HANDY_PAGES))
510 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3HandyPages);
511 else if (VM_FF_ISPENDING(pVM, VM_FF_PDM_QUEUES))
512 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3PDMQueues);
513 else if (VM_FF_ISPENDING(pVM, VM_FF_EMT_RENDEZVOUS))
514 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3Rendezvous);
515 else if (VM_FF_ISPENDING(pVM, VM_FF_PDM_DMA))
516 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3DMA);
517 else if (VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_TIMER))
518 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3Timer);
519 else if (VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_PDM_CRITSECT))
520 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3CritSect);
521 else if (VMCPU_FF_ISPENDING(pVCpu, VMCPU_FF_TO_R3))
522 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3);
523 else
524 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3Unknown);
525 break;
526
527 case VINF_EM_RAW_TIMER_PENDING:
528 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetTimerPending);
529 break;
530 case VINF_EM_RAW_INTERRUPT_PENDING:
531 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetInterruptPending);
532 break;
533 case VINF_VMM_CALL_HOST:
534 switch (pVCpu->vmm.s.enmCallRing3Operation)
535 {
536 case VMMCALLRING3_PDM_CRIT_SECT_ENTER:
537 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPDMCritSectEnter);
538 break;
539 case VMMCALLRING3_PDM_LOCK:
540 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPDMLock);
541 break;
542 case VMMCALLRING3_PGM_POOL_GROW:
543 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMPoolGrow);
544 break;
545 case VMMCALLRING3_PGM_LOCK:
546 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMLock);
547 break;
548 case VMMCALLRING3_PGM_MAP_CHUNK:
549 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMMapChunk);
550 break;
551 case VMMCALLRING3_PGM_ALLOCATE_HANDY_PAGES:
552 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMAllocHandy);
553 break;
554 case VMMCALLRING3_REM_REPLAY_HANDLER_NOTIFICATIONS:
555 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallRemReplay);
556 break;
557 case VMMCALLRING3_VMM_LOGGER_FLUSH:
558 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallLogFlush);
559 break;
560 case VMMCALLRING3_VM_SET_ERROR:
561 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallVMSetError);
562 break;
563 case VMMCALLRING3_VM_SET_RUNTIME_ERROR:
564 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallVMSetRuntimeError);
565 break;
566 case VMMCALLRING3_VM_R0_ASSERTION:
567 default:
568 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetCallRing3);
569 break;
570 }
571 break;
572 case VINF_PATM_DUPLICATE_FUNCTION:
573 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPATMDuplicateFn);
574 break;
575 case VINF_PGM_CHANGE_MODE:
576 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPGMChangeMode);
577 break;
578 case VINF_PGM_POOL_FLUSH_PENDING:
579 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPGMFlushPending);
580 break;
581 case VINF_EM_PENDING_REQUEST:
582 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPendingRequest);
583 break;
584 case VINF_EM_HWACCM_PATCH_TPR_INSTR:
585 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchTPR);
586 break;
587 default:
588 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMisc);
589 break;
590 }
591}
592#endif /* VBOX_WITH_STATISTICS */
593
594
595/**
596 * Unused ring-0 entry point that used to be called from the interrupt gate.
597 *
598 * Will be removed one of the next times we do a major SUPDrv version bump.
599 *
600 * @returns VBox status code.
601 * @param pVM Pointer to the VM.
602 * @param enmOperation Which operation to execute.
603 * @param pvArg Argument to the operation.
604 * @remarks Assume called with interrupts disabled.
605 */
606VMMR0DECL(int) VMMR0EntryInt(PVM pVM, VMMR0OPERATION enmOperation, void *pvArg)
607{
608 /*
609 * We're returning VERR_NOT_SUPPORT here so we've got something else
610 * than -1 which the interrupt gate glue code might return.
611 */
612 Log(("operation %#x is not supported\n", enmOperation));
613 NOREF(enmOperation); NOREF(pvArg); NOREF(pVM);
614 return VERR_NOT_SUPPORTED;
615}
616
617
618/**
619 * The Ring 0 entry point, called by the fast-ioctl path.
620 *
621 * @param pVM Pointer to the VM.
622 * The return code is stored in pVM->vmm.s.iLastGZRc.
623 * @param idCpu The Virtual CPU ID of the calling EMT.
624 * @param enmOperation Which operation to execute.
625 * @remarks Assume called with interrupts _enabled_.
626 */
627VMMR0DECL(void) VMMR0EntryFast(PVM pVM, VMCPUID idCpu, VMMR0OPERATION enmOperation)
628{
629 if (RT_UNLIKELY(idCpu >= pVM->cCpus))
630 return;
631 PVMCPU pVCpu = &pVM->aCpus[idCpu];
632
633 switch (enmOperation)
634 {
635 /*
636 * Switch to GC and run guest raw mode code.
637 * Disable interrupts before doing the world switch.
638 */
639 case VMMR0_DO_RAW_RUN:
640 {
641 /* Some safety precautions first. */
642#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
643 if (RT_LIKELY( !pVM->vmm.s.fSwitcherDisabled /* hwaccm */
644 && pVM->cCpus == 1 /* !smp */
645 && PGMGetHyperCR3(pVCpu)))
646#else
647 if (RT_LIKELY( !pVM->vmm.s.fSwitcherDisabled
648 && pVM->cCpus == 1))
649#endif
650 {
651 /* Disable preemption and update the periodic preemption timer. */
652 RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
653 RTThreadPreemptDisable(&PreemptState);
654 RTCPUID idHostCpu = RTMpCpuId();
655#ifdef VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
656 CPUMR0SetLApic(pVM, idHostCpu);
657#endif
658 ASMAtomicWriteU32(&pVCpu->idHostCpu, idHostCpu);
659 if (pVM->vmm.s.fUsePeriodicPreemptionTimers)
660 GVMMR0SchedUpdatePeriodicPreemptionTimer(pVM, pVCpu->idHostCpu, TMCalcHostTimerFrequency(pVM, pVCpu));
661
662 /* We might need to disable VT-x if the active switcher turns off paging. */
663 bool fVTxDisabled;
664 int rc = HWACCMR0EnterSwitcher(pVM, &fVTxDisabled);
665 if (RT_SUCCESS(rc))
666 {
667 RTCCUINTREG uFlags = ASMIntDisableFlags();
668
669 for (;;)
670 {
671 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED_EXEC);
672 TMNotifyStartOfExecution(pVCpu);
673
674 rc = pVM->vmm.s.pfnHostToGuestR0(pVM);
675 pVCpu->vmm.s.iLastGZRc = rc;
676
677 TMNotifyEndOfExecution(pVCpu);
678 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED);
679
680 if (rc != VINF_VMM_CALL_TRACER)
681 break;
682 SUPR0TracerUmodProbeFire(pVM->pSession, &pVCpu->vmm.s.TracerCtx);
683 }
684
685 /* Re-enable VT-x if previously turned off. */
686 HWACCMR0LeaveSwitcher(pVM, fVTxDisabled);
687
688 if ( rc == VINF_EM_RAW_INTERRUPT
689 || rc == VINF_EM_RAW_INTERRUPT_HYPER)
690 TRPMR0DispatchHostInterrupt(pVM);
691
692 ASMSetFlags(uFlags);
693
694#ifdef VBOX_WITH_STATISTICS
695 STAM_COUNTER_INC(&pVM->vmm.s.StatRunRC);
696 vmmR0RecordRC(pVM, pVCpu, rc);
697#endif
698 }
699 else
700 pVCpu->vmm.s.iLastGZRc = rc;
701 ASMAtomicWriteU32(&pVCpu->idHostCpu, NIL_RTCPUID);
702 RTThreadPreemptRestore(&PreemptState);
703 }
704 else
705 {
706 Assert(!pVM->vmm.s.fSwitcherDisabled);
707 pVCpu->vmm.s.iLastGZRc = VERR_NOT_SUPPORTED;
708 if (pVM->cCpus != 1)
709 pVCpu->vmm.s.iLastGZRc = VERR_RAW_MODE_INVALID_SMP;
710#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
711 if (!PGMGetHyperCR3(pVCpu))
712 pVCpu->vmm.s.iLastGZRc = VERR_PGM_NO_CR3_SHADOW_ROOT;
713#endif
714 }
715 break;
716 }
717
718 /*
719 * Run guest code using the available hardware acceleration technology.
720 *
721 * Disable interrupts before we do anything interesting. On Windows we avoid
722 * this by having the support driver raise the IRQL before calling us, this way
723 * we hope to get away with page faults and later calling into the kernel.
724 */
725 case VMMR0_DO_HWACC_RUN:
726 {
727#ifdef VBOX_WITH_VMMR0_DISABLE_PREEMPTION
728 RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
729 RTThreadPreemptDisable(&PreemptState);
730#elif !defined(RT_OS_WINDOWS)
731 RTCCUINTREG uFlags = ASMIntDisableFlags();
732#endif
733 ASMAtomicWriteU32(&pVCpu->idHostCpu, RTMpCpuId());
734 if (pVM->vmm.s.fUsePeriodicPreemptionTimers)
735 GVMMR0SchedUpdatePeriodicPreemptionTimer(pVM, pVCpu->idHostCpu, TMCalcHostTimerFrequency(pVM, pVCpu));
736
737#ifdef LOG_ENABLED
738 if (pVCpu->idCpu > 0)
739 {
740 /* Lazy registration of ring 0 loggers. */
741 PVMMR0LOGGER pR0Logger = pVCpu->vmm.s.pR0LoggerR0;
742 if ( pR0Logger
743 && !pR0Logger->fRegistered)
744 {
745 RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
746 pR0Logger->fRegistered = true;
747 }
748 }
749#endif
750 int rc;
751 if (!HWACCMR0SuspendPending())
752 {
753 rc = HWACCMR0Enter(pVM, pVCpu);
754 if (RT_SUCCESS(rc))
755 {
756 rc = vmmR0CallRing3SetJmp(&pVCpu->vmm.s.CallRing3JmpBufR0, HWACCMR0RunGuestCode, pVM, pVCpu); /* this may resume code. */
757 int rc2 = HWACCMR0Leave(pVM, pVCpu);
758 AssertRC(rc2);
759 }
760 STAM_COUNTER_INC(&pVM->vmm.s.StatRunRC);
761 }
762 else
763 {
764 /* System is about to go into suspend mode; go back to ring 3. */
765 rc = VINF_EM_RAW_INTERRUPT;
766 }
767 pVCpu->vmm.s.iLastGZRc = rc;
768
769 ASMAtomicWriteU32(&pVCpu->idHostCpu, NIL_RTCPUID);
770#ifdef VBOX_WITH_VMMR0_DISABLE_PREEMPTION
771 RTThreadPreemptRestore(&PreemptState);
772#elif !defined(RT_OS_WINDOWS)
773 ASMSetFlags(uFlags);
774#endif
775
776#ifdef VBOX_WITH_STATISTICS
777 vmmR0RecordRC(pVM, pVCpu, rc);
778#endif
779 /* No special action required for external interrupts, just return. */
780 break;
781 }
782
783 /*
784 * For profiling.
785 */
786 case VMMR0_DO_NOP:
787 pVCpu->vmm.s.iLastGZRc = VINF_SUCCESS;
788 break;
789
790 /*
791 * Impossible.
792 */
793 default:
794 AssertMsgFailed(("%#x\n", enmOperation));
795 pVCpu->vmm.s.iLastGZRc = VERR_NOT_SUPPORTED;
796 break;
797 }
798}
799
800
801/**
802 * Validates a session or VM session argument.
803 *
804 * @returns true / false accordingly.
805 * @param pVM Pointer to the VM.
806 * @param pSession The session argument.
807 */
808DECLINLINE(bool) vmmR0IsValidSession(PVM pVM, PSUPDRVSESSION pClaimedSession, PSUPDRVSESSION pSession)
809{
810 /* This must be set! */
811 if (!pSession)
812 return false;
813
814 /* Only one out of the two. */
815 if (pVM && pClaimedSession)
816 return false;
817 if (pVM)
818 pClaimedSession = pVM->pSession;
819 return pClaimedSession == pSession;
820}
821
822
823/**
824 * VMMR0EntryEx worker function, either called directly or when ever possible
825 * called thru a longjmp so we can exit safely on failure.
826 *
827 * @returns VBox status code.
828 * @param pVM Pointer to the VM.
829 * @param idCpu Virtual CPU ID argument. Must be NIL_VMCPUID if pVM
830 * is NIL_RTR0PTR, and may be NIL_VMCPUID if it isn't
831 * @param enmOperation Which operation to execute.
832 * @param pReqHdr This points to a SUPVMMR0REQHDR packet. Optional.
833 * The support driver validates this if it's present.
834 * @param u64Arg Some simple constant argument.
835 * @param pSession The session of the caller.
836 * @remarks Assume called with interrupts _enabled_.
837 */
838static int vmmR0EntryExWorker(PVM pVM, VMCPUID idCpu, VMMR0OPERATION enmOperation, PSUPVMMR0REQHDR pReqHdr, uint64_t u64Arg, PSUPDRVSESSION pSession)
839{
840 /*
841 * Common VM pointer validation.
842 */
843 if (pVM)
844 {
845 if (RT_UNLIKELY( !VALID_PTR(pVM)
846 || ((uintptr_t)pVM & PAGE_OFFSET_MASK)))
847 {
848 SUPR0Printf("vmmR0EntryExWorker: Invalid pVM=%p! (op=%d)\n", pVM, enmOperation);
849 return VERR_INVALID_POINTER;
850 }
851 if (RT_UNLIKELY( pVM->enmVMState < VMSTATE_CREATING
852 || pVM->enmVMState > VMSTATE_TERMINATED
853 || pVM->pVMR0 != pVM))
854 {
855 SUPR0Printf("vmmR0EntryExWorker: Invalid pVM=%p:{enmVMState=%d, .pVMR0=%p}! (op=%d)\n",
856 pVM, pVM->enmVMState, pVM->pVMR0, enmOperation);
857 return VERR_INVALID_POINTER;
858 }
859
860 if (RT_UNLIKELY(idCpu >= pVM->cCpus && idCpu != NIL_VMCPUID))
861 {
862 SUPR0Printf("vmmR0EntryExWorker: Invalid idCpu (%u vs cCpus=%u)\n", idCpu, pVM->cCpus);
863 return VERR_INVALID_PARAMETER;
864 }
865 }
866 else if (RT_UNLIKELY(idCpu != NIL_VMCPUID))
867 {
868 SUPR0Printf("vmmR0EntryExWorker: Invalid idCpu=%u\n", idCpu);
869 return VERR_INVALID_PARAMETER;
870 }
871
872
873 switch (enmOperation)
874 {
875 /*
876 * GVM requests
877 */
878 case VMMR0_DO_GVMM_CREATE_VM:
879 if (pVM || u64Arg || idCpu != NIL_VMCPUID)
880 return VERR_INVALID_PARAMETER;
881 return GVMMR0CreateVMReq((PGVMMCREATEVMREQ)pReqHdr);
882
883 case VMMR0_DO_GVMM_DESTROY_VM:
884 if (pReqHdr || u64Arg)
885 return VERR_INVALID_PARAMETER;
886 return GVMMR0DestroyVM(pVM);
887
888 case VMMR0_DO_GVMM_REGISTER_VMCPU:
889 {
890 if (!pVM)
891 return VERR_INVALID_PARAMETER;
892 return GVMMR0RegisterVCpu(pVM, idCpu);
893 }
894
895 case VMMR0_DO_GVMM_SCHED_HALT:
896 if (pReqHdr)
897 return VERR_INVALID_PARAMETER;
898 return GVMMR0SchedHalt(pVM, idCpu, u64Arg);
899
900 case VMMR0_DO_GVMM_SCHED_WAKE_UP:
901 if (pReqHdr || u64Arg)
902 return VERR_INVALID_PARAMETER;
903 return GVMMR0SchedWakeUp(pVM, idCpu);
904
905 case VMMR0_DO_GVMM_SCHED_POKE:
906 if (pReqHdr || u64Arg)
907 return VERR_INVALID_PARAMETER;
908 return GVMMR0SchedPoke(pVM, idCpu);
909
910 case VMMR0_DO_GVMM_SCHED_WAKE_UP_AND_POKE_CPUS:
911 if (u64Arg)
912 return VERR_INVALID_PARAMETER;
913 return GVMMR0SchedWakeUpAndPokeCpusReq(pVM, (PGVMMSCHEDWAKEUPANDPOKECPUSREQ)pReqHdr);
914
915 case VMMR0_DO_GVMM_SCHED_POLL:
916 if (pReqHdr || u64Arg > 1)
917 return VERR_INVALID_PARAMETER;
918 return GVMMR0SchedPoll(pVM, idCpu, !!u64Arg);
919
920 case VMMR0_DO_GVMM_QUERY_STATISTICS:
921 if (u64Arg)
922 return VERR_INVALID_PARAMETER;
923 return GVMMR0QueryStatisticsReq(pVM, (PGVMMQUERYSTATISTICSSREQ)pReqHdr);
924
925 case VMMR0_DO_GVMM_RESET_STATISTICS:
926 if (u64Arg)
927 return VERR_INVALID_PARAMETER;
928 return GVMMR0ResetStatisticsReq(pVM, (PGVMMRESETSTATISTICSSREQ)pReqHdr);
929
930 /*
931 * Initialize the R0 part of a VM instance.
932 */
933 case VMMR0_DO_VMMR0_INIT:
934 return vmmR0InitVM(pVM, (uint32_t)u64Arg);
935
936 /*
937 * Terminate the R0 part of a VM instance.
938 */
939 case VMMR0_DO_VMMR0_TERM:
940 return VMMR0TermVM(pVM, NULL);
941
942 /*
943 * Attempt to enable hwacc mode and check the current setting.
944 */
945 case VMMR0_DO_HWACC_ENABLE:
946 return HWACCMR0EnableAllCpus(pVM);
947
948 /*
949 * Setup the hardware accelerated session.
950 */
951 case VMMR0_DO_HWACC_SETUP_VM:
952 return HWACCMR0SetupVM(pVM);
953
954 /*
955 * Switch to RC to execute Hypervisor function.
956 */
957 case VMMR0_DO_CALL_HYPERVISOR:
958 {
959 int rc;
960 bool fVTxDisabled;
961
962 /* Safety precaution as HWACCM can disable the switcher. */
963 Assert(!pVM->vmm.s.fSwitcherDisabled);
964 if (RT_UNLIKELY(pVM->vmm.s.fSwitcherDisabled))
965 return VERR_NOT_SUPPORTED;
966
967#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
968 if (RT_UNLIKELY(!PGMGetHyperCR3(VMMGetCpu0(pVM))))
969 return VERR_PGM_NO_CR3_SHADOW_ROOT;
970#endif
971
972 RTCCUINTREG fFlags = ASMIntDisableFlags();
973
974#ifdef VBOX_WITH_VMMR0_DISABLE_LAPIC_NMI
975 RTCPUID idHostCpu = RTMpCpuId();
976 CPUMR0SetLApic(pVM, idHostCpu);
977#endif
978
979 /* We might need to disable VT-x if the active switcher turns off paging. */
980 rc = HWACCMR0EnterSwitcher(pVM, &fVTxDisabled);
981 if (RT_FAILURE(rc))
982 return rc;
983
984 rc = pVM->vmm.s.pfnHostToGuestR0(pVM);
985
986 /* Re-enable VT-x if previously turned off. */
987 HWACCMR0LeaveSwitcher(pVM, fVTxDisabled);
988
989 /** @todo dispatch interrupts? */
990 ASMSetFlags(fFlags);
991 return rc;
992 }
993
994 /*
995 * PGM wrappers.
996 */
997 case VMMR0_DO_PGM_ALLOCATE_HANDY_PAGES:
998 if (idCpu == NIL_VMCPUID)
999 return VERR_INVALID_CPU_ID;
1000 return PGMR0PhysAllocateHandyPages(pVM, &pVM->aCpus[idCpu]);
1001
1002 case VMMR0_DO_PGM_ALLOCATE_LARGE_HANDY_PAGE:
1003 if (idCpu == NIL_VMCPUID)
1004 return VERR_INVALID_CPU_ID;
1005 return PGMR0PhysAllocateLargeHandyPage(pVM, &pVM->aCpus[idCpu]);
1006
1007 case VMMR0_DO_PGM_PHYS_SETUP_IOMMU:
1008 if (idCpu != 0)
1009 return VERR_INVALID_CPU_ID;
1010 return PGMR0PhysSetupIommu(pVM);
1011
1012 /*
1013 * GMM wrappers.
1014 */
1015 case VMMR0_DO_GMM_INITIAL_RESERVATION:
1016 if (u64Arg)
1017 return VERR_INVALID_PARAMETER;
1018 return GMMR0InitialReservationReq(pVM, idCpu, (PGMMINITIALRESERVATIONREQ)pReqHdr);
1019
1020 case VMMR0_DO_GMM_UPDATE_RESERVATION:
1021 if (u64Arg)
1022 return VERR_INVALID_PARAMETER;
1023 return GMMR0UpdateReservationReq(pVM, idCpu, (PGMMUPDATERESERVATIONREQ)pReqHdr);
1024
1025 case VMMR0_DO_GMM_ALLOCATE_PAGES:
1026 if (u64Arg)
1027 return VERR_INVALID_PARAMETER;
1028 return GMMR0AllocatePagesReq(pVM, idCpu, (PGMMALLOCATEPAGESREQ)pReqHdr);
1029
1030 case VMMR0_DO_GMM_FREE_PAGES:
1031 if (u64Arg)
1032 return VERR_INVALID_PARAMETER;
1033 return GMMR0FreePagesReq(pVM, idCpu, (PGMMFREEPAGESREQ)pReqHdr);
1034
1035 case VMMR0_DO_GMM_FREE_LARGE_PAGE:
1036 if (u64Arg)
1037 return VERR_INVALID_PARAMETER;
1038 return GMMR0FreeLargePageReq(pVM, idCpu, (PGMMFREELARGEPAGEREQ)pReqHdr);
1039
1040 case VMMR0_DO_GMM_QUERY_HYPERVISOR_MEM_STATS:
1041 if (u64Arg)
1042 return VERR_INVALID_PARAMETER;
1043 return GMMR0QueryHypervisorMemoryStatsReq(pVM, (PGMMMEMSTATSREQ)pReqHdr);
1044
1045 case VMMR0_DO_GMM_QUERY_MEM_STATS:
1046 if (idCpu == NIL_VMCPUID)
1047 return VERR_INVALID_CPU_ID;
1048 if (u64Arg)
1049 return VERR_INVALID_PARAMETER;
1050 return GMMR0QueryMemoryStatsReq(pVM, idCpu, (PGMMMEMSTATSREQ)pReqHdr);
1051
1052 case VMMR0_DO_GMM_BALLOONED_PAGES:
1053 if (u64Arg)
1054 return VERR_INVALID_PARAMETER;
1055 return GMMR0BalloonedPagesReq(pVM, idCpu, (PGMMBALLOONEDPAGESREQ)pReqHdr);
1056
1057 case VMMR0_DO_GMM_MAP_UNMAP_CHUNK:
1058 if (u64Arg)
1059 return VERR_INVALID_PARAMETER;
1060 return GMMR0MapUnmapChunkReq(pVM, (PGMMMAPUNMAPCHUNKREQ)pReqHdr);
1061
1062 case VMMR0_DO_GMM_SEED_CHUNK:
1063 if (pReqHdr)
1064 return VERR_INVALID_PARAMETER;
1065 return GMMR0SeedChunk(pVM, idCpu, (RTR3PTR)u64Arg);
1066
1067 case VMMR0_DO_GMM_REGISTER_SHARED_MODULE:
1068 if (idCpu == NIL_VMCPUID)
1069 return VERR_INVALID_CPU_ID;
1070 if (u64Arg)
1071 return VERR_INVALID_PARAMETER;
1072 return GMMR0RegisterSharedModuleReq(pVM, idCpu, (PGMMREGISTERSHAREDMODULEREQ)pReqHdr);
1073
1074 case VMMR0_DO_GMM_UNREGISTER_SHARED_MODULE:
1075 if (idCpu == NIL_VMCPUID)
1076 return VERR_INVALID_CPU_ID;
1077 if (u64Arg)
1078 return VERR_INVALID_PARAMETER;
1079 return GMMR0UnregisterSharedModuleReq(pVM, idCpu, (PGMMUNREGISTERSHAREDMODULEREQ)pReqHdr);
1080
1081 case VMMR0_DO_GMM_RESET_SHARED_MODULES:
1082 if (idCpu == NIL_VMCPUID)
1083 return VERR_INVALID_CPU_ID;
1084 if ( u64Arg
1085 || pReqHdr)
1086 return VERR_INVALID_PARAMETER;
1087 return GMMR0ResetSharedModules(pVM, idCpu);
1088
1089#ifdef VBOX_WITH_PAGE_SHARING
1090 case VMMR0_DO_GMM_CHECK_SHARED_MODULES:
1091 {
1092 if (idCpu == NIL_VMCPUID)
1093 return VERR_INVALID_CPU_ID;
1094 if ( u64Arg
1095 || pReqHdr)
1096 return VERR_INVALID_PARAMETER;
1097
1098 PVMCPU pVCpu = &pVM->aCpus[idCpu];
1099 Assert(pVCpu->hNativeThreadR0 == RTThreadNativeSelf());
1100
1101# ifdef DEBUG_sandervl
1102 /* 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). */
1103 /* Todo: this can have bad side effects for unexpected jumps back to r3. */
1104 int rc = GMMR0CheckSharedModulesStart(pVM);
1105 if (rc == VINF_SUCCESS)
1106 {
1107 rc = vmmR0CallRing3SetJmp(&pVCpu->vmm.s.CallRing3JmpBufR0, GMMR0CheckSharedModules, pVM, pVCpu); /* this may resume code. */
1108 Assert( rc == VINF_SUCCESS
1109 || (rc == VINF_VMM_CALL_HOST && pVCpu->vmm.s.enmCallRing3Operation == VMMCALLRING3_VMM_LOGGER_FLUSH));
1110 GMMR0CheckSharedModulesEnd(pVM);
1111 }
1112# else
1113 int rc = GMMR0CheckSharedModules(pVM, pVCpu);
1114# endif
1115 return rc;
1116 }
1117#endif
1118
1119#if defined(VBOX_STRICT) && HC_ARCH_BITS == 64
1120 case VMMR0_DO_GMM_FIND_DUPLICATE_PAGE:
1121 if (u64Arg)
1122 return VERR_INVALID_PARAMETER;
1123 return GMMR0FindDuplicatePageReq(pVM, (PGMMFINDDUPLICATEPAGEREQ)pReqHdr);
1124#endif
1125
1126 case VMMR0_DO_GMM_QUERY_STATISTICS:
1127 if (u64Arg)
1128 return VERR_INVALID_PARAMETER;
1129 return GMMR0QueryStatisticsReq(pVM, (PGMMQUERYSTATISTICSSREQ)pReqHdr);
1130
1131 case VMMR0_DO_GMM_RESET_STATISTICS:
1132 if (u64Arg)
1133 return VERR_INVALID_PARAMETER;
1134 return GMMR0ResetStatisticsReq(pVM, (PGMMRESETSTATISTICSSREQ)pReqHdr);
1135
1136 /*
1137 * A quick GCFGM mock-up.
1138 */
1139 /** @todo GCFGM with proper access control, ring-3 management interface and all that. */
1140 case VMMR0_DO_GCFGM_SET_VALUE:
1141 case VMMR0_DO_GCFGM_QUERY_VALUE:
1142 {
1143 if (pVM || !pReqHdr || u64Arg || idCpu != NIL_VMCPUID)
1144 return VERR_INVALID_PARAMETER;
1145 PGCFGMVALUEREQ pReq = (PGCFGMVALUEREQ)pReqHdr;
1146 if (pReq->Hdr.cbReq != sizeof(*pReq))
1147 return VERR_INVALID_PARAMETER;
1148 int rc;
1149 if (enmOperation == VMMR0_DO_GCFGM_SET_VALUE)
1150 {
1151 rc = GVMMR0SetConfig(pReq->pSession, &pReq->szName[0], pReq->u64Value);
1152 //if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1153 // rc = GMMR0SetConfig(pReq->pSession, &pReq->szName[0], pReq->u64Value);
1154 }
1155 else
1156 {
1157 rc = GVMMR0QueryConfig(pReq->pSession, &pReq->szName[0], &pReq->u64Value);
1158 //if (rc == VERR_CFGM_VALUE_NOT_FOUND)
1159 // rc = GMMR0QueryConfig(pReq->pSession, &pReq->szName[0], &pReq->u64Value);
1160 }
1161 return rc;
1162 }
1163
1164 /*
1165 * PDM Wrappers.
1166 */
1167 case VMMR0_DO_PDM_DRIVER_CALL_REQ_HANDLER:
1168 {
1169 if (!pVM || !pReqHdr || u64Arg || idCpu != NIL_VMCPUID)
1170 return VERR_INVALID_PARAMETER;
1171 return PDMR0DriverCallReqHandler(pVM, (PPDMDRIVERCALLREQHANDLERREQ)pReqHdr);
1172 }
1173
1174 case VMMR0_DO_PDM_DEVICE_CALL_REQ_HANDLER:
1175 {
1176 if (!pVM || !pReqHdr || u64Arg || idCpu != NIL_VMCPUID)
1177 return VERR_INVALID_PARAMETER;
1178 return PDMR0DeviceCallReqHandler(pVM, (PPDMDEVICECALLREQHANDLERREQ)pReqHdr);
1179 }
1180
1181 /*
1182 * Requests to the internal networking service.
1183 */
1184 case VMMR0_DO_INTNET_OPEN:
1185 {
1186 PINTNETOPENREQ pReq = (PINTNETOPENREQ)pReqHdr;
1187 if (u64Arg || !pReq || !vmmR0IsValidSession(pVM, pReq->pSession, pSession) || idCpu != NIL_VMCPUID)
1188 return VERR_INVALID_PARAMETER;
1189 return IntNetR0OpenReq(pSession, pReq);
1190 }
1191
1192 case VMMR0_DO_INTNET_IF_CLOSE:
1193 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFCLOSEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1194 return VERR_INVALID_PARAMETER;
1195 return IntNetR0IfCloseReq(pSession, (PINTNETIFCLOSEREQ)pReqHdr);
1196
1197 case VMMR0_DO_INTNET_IF_GET_BUFFER_PTRS:
1198 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFGETBUFFERPTRSREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1199 return VERR_INVALID_PARAMETER;
1200 return IntNetR0IfGetBufferPtrsReq(pSession, (PINTNETIFGETBUFFERPTRSREQ)pReqHdr);
1201
1202 case VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE:
1203 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETPROMISCUOUSMODEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1204 return VERR_INVALID_PARAMETER;
1205 return IntNetR0IfSetPromiscuousModeReq(pSession, (PINTNETIFSETPROMISCUOUSMODEREQ)pReqHdr);
1206
1207 case VMMR0_DO_INTNET_IF_SET_MAC_ADDRESS:
1208 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETMACADDRESSREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1209 return VERR_INVALID_PARAMETER;
1210 return IntNetR0IfSetMacAddressReq(pSession, (PINTNETIFSETMACADDRESSREQ)pReqHdr);
1211
1212 case VMMR0_DO_INTNET_IF_SET_ACTIVE:
1213 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETACTIVEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1214 return VERR_INVALID_PARAMETER;
1215 return IntNetR0IfSetActiveReq(pSession, (PINTNETIFSETACTIVEREQ)pReqHdr);
1216
1217 case VMMR0_DO_INTNET_IF_SEND:
1218 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSENDREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1219 return VERR_INVALID_PARAMETER;
1220 return IntNetR0IfSendReq(pSession, (PINTNETIFSENDREQ)pReqHdr);
1221
1222 case VMMR0_DO_INTNET_IF_WAIT:
1223 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFWAITREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1224 return VERR_INVALID_PARAMETER;
1225 return IntNetR0IfWaitReq(pSession, (PINTNETIFWAITREQ)pReqHdr);
1226
1227 case VMMR0_DO_INTNET_IF_ABORT_WAIT:
1228 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFWAITREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1229 return VERR_INVALID_PARAMETER;
1230 return IntNetR0IfAbortWaitReq(pSession, (PINTNETIFABORTWAITREQ)pReqHdr);
1231
1232#ifdef VBOX_WITH_PCI_PASSTHROUGH
1233 /*
1234 * Requests to host PCI driver service.
1235 */
1236 case VMMR0_DO_PCIRAW_REQ:
1237 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PPCIRAWSENDREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1238 return VERR_INVALID_PARAMETER;
1239 return PciRawR0ProcessReq(pSession, pVM, (PPCIRAWSENDREQ)pReqHdr);
1240#endif
1241 /*
1242 * For profiling.
1243 */
1244 case VMMR0_DO_NOP:
1245 case VMMR0_DO_SLOW_NOP:
1246 return VINF_SUCCESS;
1247
1248 /*
1249 * For testing Ring-0 APIs invoked in this environment.
1250 */
1251 case VMMR0_DO_TESTS:
1252 /** @todo make new test */
1253 return VINF_SUCCESS;
1254
1255
1256#if HC_ARCH_BITS == 32 && defined(VBOX_WITH_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
1257 case VMMR0_DO_TEST_SWITCHER3264:
1258 if (idCpu == NIL_VMCPUID)
1259 return VERR_INVALID_CPU_ID;
1260 return HWACCMR0TestSwitcher3264(pVM);
1261#endif
1262 default:
1263 /*
1264 * We're returning VERR_NOT_SUPPORT here so we've got something else
1265 * than -1 which the interrupt gate glue code might return.
1266 */
1267 Log(("operation %#x is not supported\n", enmOperation));
1268 return VERR_NOT_SUPPORTED;
1269 }
1270}
1271
1272
1273/**
1274 * Argument for vmmR0EntryExWrapper containing the arguments for VMMR0EntryEx.
1275 */
1276typedef struct VMMR0ENTRYEXARGS
1277{
1278 PVM pVM;
1279 VMCPUID idCpu;
1280 VMMR0OPERATION enmOperation;
1281 PSUPVMMR0REQHDR pReq;
1282 uint64_t u64Arg;
1283 PSUPDRVSESSION pSession;
1284} VMMR0ENTRYEXARGS;
1285/** Pointer to a vmmR0EntryExWrapper argument package. */
1286typedef VMMR0ENTRYEXARGS *PVMMR0ENTRYEXARGS;
1287
1288/**
1289 * This is just a longjmp wrapper function for VMMR0EntryEx calls.
1290 *
1291 * @returns VBox status code.
1292 * @param pvArgs The argument package
1293 */
1294static DECLCALLBACK(int) vmmR0EntryExWrapper(void *pvArgs)
1295{
1296 return vmmR0EntryExWorker(((PVMMR0ENTRYEXARGS)pvArgs)->pVM,
1297 ((PVMMR0ENTRYEXARGS)pvArgs)->idCpu,
1298 ((PVMMR0ENTRYEXARGS)pvArgs)->enmOperation,
1299 ((PVMMR0ENTRYEXARGS)pvArgs)->pReq,
1300 ((PVMMR0ENTRYEXARGS)pvArgs)->u64Arg,
1301 ((PVMMR0ENTRYEXARGS)pvArgs)->pSession);
1302}
1303
1304
1305/**
1306 * The Ring 0 entry point, called by the support library (SUP).
1307 *
1308 * @returns VBox status code.
1309 * @param pVM Pointer to the VM.
1310 * @param idCpu Virtual CPU ID argument. Must be NIL_VMCPUID if pVM
1311 * is NIL_RTR0PTR, and may be NIL_VMCPUID if it isn't
1312 * @param enmOperation Which operation to execute.
1313 * @param pReq Pointer to the SUPVMMR0REQHDR packet. Optional.
1314 * @param u64Arg Some simple constant argument.
1315 * @param pSession The session of the caller.
1316 * @remarks Assume called with interrupts _enabled_.
1317 */
1318VMMR0DECL(int) VMMR0EntryEx(PVM pVM, VMCPUID idCpu, VMMR0OPERATION enmOperation, PSUPVMMR0REQHDR pReq, uint64_t u64Arg, PSUPDRVSESSION pSession)
1319{
1320 /*
1321 * Requests that should only happen on the EMT thread will be
1322 * wrapped in a setjmp so we can assert without causing trouble.
1323 */
1324 if ( VALID_PTR(pVM)
1325 && pVM->pVMR0
1326 && idCpu < pVM->cCpus)
1327 {
1328 switch (enmOperation)
1329 {
1330 /* These might/will be called before VMMR3Init. */
1331 case VMMR0_DO_GMM_INITIAL_RESERVATION:
1332 case VMMR0_DO_GMM_UPDATE_RESERVATION:
1333 case VMMR0_DO_GMM_ALLOCATE_PAGES:
1334 case VMMR0_DO_GMM_FREE_PAGES:
1335 case VMMR0_DO_GMM_BALLOONED_PAGES:
1336 /* On the mac we might not have a valid jmp buf, so check these as well. */
1337 case VMMR0_DO_VMMR0_INIT:
1338 case VMMR0_DO_VMMR0_TERM:
1339 {
1340 PVMCPU pVCpu = &pVM->aCpus[idCpu];
1341
1342 if (!pVCpu->vmm.s.CallRing3JmpBufR0.pvSavedStack)
1343 break;
1344
1345 /** @todo validate this EMT claim... GVM knows. */
1346 VMMR0ENTRYEXARGS Args;
1347 Args.pVM = pVM;
1348 Args.idCpu = idCpu;
1349 Args.enmOperation = enmOperation;
1350 Args.pReq = pReq;
1351 Args.u64Arg = u64Arg;
1352 Args.pSession = pSession;
1353 return vmmR0CallRing3SetJmpEx(&pVCpu->vmm.s.CallRing3JmpBufR0, vmmR0EntryExWrapper, &Args);
1354 }
1355
1356 default:
1357 break;
1358 }
1359 }
1360 return vmmR0EntryExWorker(pVM, idCpu, enmOperation, pReq, u64Arg, pSession);
1361}
1362
1363/**
1364 * Internal R0 logger worker: Flush logger.
1365 *
1366 * @param pLogger The logger instance to flush.
1367 * @remark This function must be exported!
1368 */
1369VMMR0DECL(void) vmmR0LoggerFlush(PRTLOGGER pLogger)
1370{
1371#ifdef LOG_ENABLED
1372 /*
1373 * Convert the pLogger into a VM handle and 'call' back to Ring-3.
1374 * (This is a bit paranoid code.)
1375 */
1376 PVMMR0LOGGER pR0Logger = (PVMMR0LOGGER)((uintptr_t)pLogger - RT_OFFSETOF(VMMR0LOGGER, Logger));
1377 if ( !VALID_PTR(pR0Logger)
1378 || !VALID_PTR(pR0Logger + 1)
1379 || pLogger->u32Magic != RTLOGGER_MAGIC)
1380 {
1381# ifdef DEBUG
1382 SUPR0Printf("vmmR0LoggerFlush: pLogger=%p!\n", pLogger);
1383# endif
1384 return;
1385 }
1386 if (pR0Logger->fFlushingDisabled)
1387 return; /* quietly */
1388
1389 PVM pVM = pR0Logger->pVM;
1390 if ( !VALID_PTR(pVM)
1391 || pVM->pVMR0 != pVM)
1392 {
1393# ifdef DEBUG
1394 SUPR0Printf("vmmR0LoggerFlush: pVM=%p! pVMR0=%p! pLogger=%p\n", pVM, pVM->pVMR0, pLogger);
1395# endif
1396 return;
1397 }
1398
1399 PVMCPU pVCpu = VMMGetCpu(pVM);
1400 if (pVCpu)
1401 {
1402 /*
1403 * Check that the jump buffer is armed.
1404 */
1405# ifdef RT_ARCH_X86
1406 if ( !pVCpu->vmm.s.CallRing3JmpBufR0.eip
1407 || pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call)
1408# else
1409 if ( !pVCpu->vmm.s.CallRing3JmpBufR0.rip
1410 || pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call)
1411# endif
1412 {
1413# ifdef DEBUG
1414 SUPR0Printf("vmmR0LoggerFlush: Jump buffer isn't armed!\n");
1415# endif
1416 return;
1417 }
1418 VMMRZCallRing3(pVM, pVCpu, VMMCALLRING3_VMM_LOGGER_FLUSH, 0);
1419 }
1420# ifdef DEBUG
1421 else
1422 SUPR0Printf("vmmR0LoggerFlush: invalid VCPU context!\n");
1423# endif
1424#endif
1425}
1426
1427/**
1428 * Internal R0 logger worker: Custom prefix.
1429 *
1430 * @returns Number of chars written.
1431 *
1432 * @param pLogger The logger instance.
1433 * @param pchBuf The output buffer.
1434 * @param cchBuf The size of the buffer.
1435 * @param pvUser User argument (ignored).
1436 */
1437VMMR0DECL(size_t) vmmR0LoggerPrefix(PRTLOGGER pLogger, char *pchBuf, size_t cchBuf, void *pvUser)
1438{
1439 NOREF(pvUser);
1440#ifdef LOG_ENABLED
1441 PVMMR0LOGGER pR0Logger = (PVMMR0LOGGER)((uintptr_t)pLogger - RT_OFFSETOF(VMMR0LOGGER, Logger));
1442 if ( !VALID_PTR(pR0Logger)
1443 || !VALID_PTR(pR0Logger + 1)
1444 || pLogger->u32Magic != RTLOGGER_MAGIC
1445 || cchBuf < 2)
1446 return 0;
1447
1448 static const char s_szHex[17] = "0123456789abcdef";
1449 VMCPUID const idCpu = pR0Logger->idCpu;
1450 pchBuf[1] = s_szHex[ idCpu & 15];
1451 pchBuf[0] = s_szHex[(idCpu >> 4) & 15];
1452
1453 return 2;
1454#else
1455 return 0;
1456#endif
1457}
1458
1459#ifdef LOG_ENABLED
1460
1461/**
1462 * Disables flushing of the ring-0 debug log.
1463 *
1464 * @param pVCpu Pointer to the VMCPU.
1465 */
1466VMMR0DECL(void) VMMR0LogFlushDisable(PVMCPU pVCpu)
1467{
1468 if (pVCpu->vmm.s.pR0LoggerR0)
1469 pVCpu->vmm.s.pR0LoggerR0->fFlushingDisabled = true;
1470}
1471
1472
1473/**
1474 * Enables flushing of the ring-0 debug log.
1475 *
1476 * @param pVCpu Pointer to the VMCPU.
1477 */
1478VMMR0DECL(void) VMMR0LogFlushEnable(PVMCPU pVCpu)
1479{
1480 if (pVCpu->vmm.s.pR0LoggerR0)
1481 pVCpu->vmm.s.pR0LoggerR0->fFlushingDisabled = false;
1482}
1483
1484#endif /* LOG_ENABLED */
1485
1486/**
1487 * Jump back to ring-3 if we're the EMT and the longjmp is armed.
1488 *
1489 * @returns true if the breakpoint should be hit, false if it should be ignored.
1490 */
1491DECLEXPORT(bool) RTCALL RTAssertShouldPanic(void)
1492{
1493#if 0
1494 return true;
1495#else
1496 PVM pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
1497 if (pVM)
1498 {
1499 PVMCPU pVCpu = VMMGetCpu(pVM);
1500
1501 if (pVCpu)
1502 {
1503#ifdef RT_ARCH_X86
1504 if ( pVCpu->vmm.s.CallRing3JmpBufR0.eip
1505 && !pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call)
1506#else
1507 if ( pVCpu->vmm.s.CallRing3JmpBufR0.rip
1508 && !pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call)
1509#endif
1510 {
1511 int rc = VMMRZCallRing3(pVM, pVCpu, VMMCALLRING3_VM_R0_ASSERTION, 0);
1512 return RT_FAILURE_NP(rc);
1513 }
1514 }
1515 }
1516#ifdef RT_OS_LINUX
1517 return true;
1518#else
1519 return false;
1520#endif
1521#endif
1522}
1523
1524
1525/**
1526 * Override this so we can push it up to ring-3.
1527 *
1528 * @param pszExpr Expression. Can be NULL.
1529 * @param uLine Location line number.
1530 * @param pszFile Location file name.
1531 * @param pszFunction Location function name.
1532 */
1533DECLEXPORT(void) RTCALL RTAssertMsg1Weak(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
1534{
1535 /*
1536 * To the log.
1537 */
1538 LogAlways(("\n!!R0-Assertion Failed!!\n"
1539 "Expression: %s\n"
1540 "Location : %s(%d) %s\n",
1541 pszExpr, pszFile, uLine, pszFunction));
1542
1543 /*
1544 * To the global VMM buffer.
1545 */
1546 PVM pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
1547 if (pVM)
1548 RTStrPrintf(pVM->vmm.s.szRing0AssertMsg1, sizeof(pVM->vmm.s.szRing0AssertMsg1),
1549 "\n!!R0-Assertion Failed!!\n"
1550 "Expression: %s\n"
1551 "Location : %s(%d) %s\n",
1552 pszExpr, pszFile, uLine, pszFunction);
1553
1554 /*
1555 * Continue the normal way.
1556 */
1557 RTAssertMsg1(pszExpr, uLine, pszFile, pszFunction);
1558}
1559
1560
1561/**
1562 * Callback for RTLogFormatV which writes to the ring-3 log port.
1563 * See PFNLOGOUTPUT() for details.
1564 */
1565static DECLCALLBACK(size_t) rtLogOutput(void *pv, const char *pachChars, size_t cbChars)
1566{
1567 for (size_t i = 0; i < cbChars; i++)
1568 LogAlways(("%c", pachChars[i]));
1569
1570 NOREF(pv);
1571 return cbChars;
1572}
1573
1574
1575/**
1576 * Override this so we can push it up to ring-3.
1577 *
1578 * @param pszFormat The format string.
1579 * @param va Arguments.
1580 */
1581DECLEXPORT(void) RTCALL RTAssertMsg2WeakV(const char *pszFormat, va_list va)
1582{
1583 va_list vaCopy;
1584
1585 /*
1586 * Push the message to the logger.
1587 */
1588 PRTLOGGER pLog = RTLogDefaultInstance(); /** @todo we want this for release as well! */
1589 if (pLog)
1590 {
1591 va_copy(vaCopy, va);
1592 RTLogFormatV(rtLogOutput, pLog, pszFormat, vaCopy);
1593 va_end(vaCopy);
1594 }
1595
1596 /*
1597 * Push it to the global VMM buffer.
1598 */
1599 PVM pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
1600 if (pVM)
1601 {
1602 va_copy(vaCopy, va);
1603 RTStrPrintfV(pVM->vmm.s.szRing0AssertMsg2, sizeof(pVM->vmm.s.szRing0AssertMsg2), pszFormat, vaCopy);
1604 va_end(vaCopy);
1605 }
1606
1607 /*
1608 * Continue the normal way.
1609 */
1610 RTAssertMsg2V(pszFormat, va);
1611}
1612
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