VirtualBox

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

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

Proper debug logging

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