VirtualBox

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

Last change on this file since 38707 was 37584, checked in by vboxsync, 14 years ago

VMM: Check for VMCPU_FF_PDM_CRITSECT in R0 and RC.

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