VirtualBox

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

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

Take the balloon size into account when applying account limits for the VM.

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