VirtualBox

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

Last change on this file since 32583 was 32504, checked in by vboxsync, 15 years ago

SUPDrv,IPRT,VMM,DevAPIC: Added RTTimerCanDoHighResolution and exposed the RTTimer* API to the ring-0 modules. Fixed two regression from r65858 (TM + APIC).

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette