VirtualBox

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

Last change on this file since 29698 was 29669, checked in by vboxsync, 15 years ago

DrvIntNet,SrvIntNet: Added IntNetR0AbortWait to address races in drvIntNetDestruct.

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