VirtualBox

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

Last change on this file since 34079 was 33935, checked in by vboxsync, 14 years ago

VMM: mask all Local APIC interrupt vectors which are set up to NMI mode during world switch (raw mode only)

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