VirtualBox

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

Last change on this file since 20871 was 20871, checked in by vboxsync, 16 years ago

VMM: Make sure there is enough room for a few physical handler notification before we disable ring-3 calls. Partial VMM[GC|R0]CallHost unification.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 45.7 KB
Line 
1/* $Id: VMMR0.cpp 20871 2009-06-24 01:56:19Z 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/**
310 * Calls the ring-3 host code.
311 *
312 * @returns VBox status code of the ring-3 call.
313 * @param pVM The VM handle.
314 * @param enmOperation The operation.
315 * @param uArg The argument to the operation.
316 *
317 * @deprecated Use VMMRZCallRing3.
318 */
319VMMR0DECL(int) VMMR0CallHost(PVM pVM, VMMCALLHOST enmOperation, uint64_t uArg)
320{
321 return VMMRZCallRing3(pVM, VMMGetCpu(pVM), enmOperation, uArg);
322}
323
324
325#ifdef VBOX_WITH_STATISTICS
326/**
327 * Record return code statistics
328 * @param pVM The VM handle.
329 * @param pVCpu The VMCPU handle.
330 * @param rc The status code.
331 */
332static void vmmR0RecordRC(PVM pVM, PVMCPU pVCpu, int rc)
333{
334 /*
335 * Collect statistics.
336 */
337 switch (rc)
338 {
339 case VINF_SUCCESS:
340 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetNormal);
341 break;
342 case VINF_EM_RAW_INTERRUPT:
343 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetInterrupt);
344 break;
345 case VINF_EM_RAW_INTERRUPT_HYPER:
346 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetInterruptHyper);
347 break;
348 case VINF_EM_RAW_GUEST_TRAP:
349 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetGuestTrap);
350 break;
351 case VINF_EM_RAW_RING_SWITCH:
352 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetRingSwitch);
353 break;
354 case VINF_EM_RAW_RING_SWITCH_INT:
355 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetRingSwitchInt);
356 break;
357 case VINF_EM_RAW_EXCEPTION_PRIVILEGED:
358 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetExceptionPrivilege);
359 break;
360 case VINF_EM_RAW_STALE_SELECTOR:
361 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetStaleSelector);
362 break;
363 case VINF_EM_RAW_IRET_TRAP:
364 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIRETTrap);
365 break;
366 case VINF_IOM_HC_IOPORT_READ:
367 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIORead);
368 break;
369 case VINF_IOM_HC_IOPORT_WRITE:
370 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIOWrite);
371 break;
372 case VINF_IOM_HC_MMIO_READ:
373 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIORead);
374 break;
375 case VINF_IOM_HC_MMIO_WRITE:
376 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOWrite);
377 break;
378 case VINF_IOM_HC_MMIO_READ_WRITE:
379 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOReadWrite);
380 break;
381 case VINF_PATM_HC_MMIO_PATCH_READ:
382 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOPatchRead);
383 break;
384 case VINF_PATM_HC_MMIO_PATCH_WRITE:
385 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOPatchWrite);
386 break;
387 case VINF_EM_RAW_EMULATE_INSTR:
388 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetEmulate);
389 break;
390 case VINF_EM_RAW_EMULATE_IO_BLOCK:
391 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIOBlockEmulate);
392 break;
393 case VINF_PATCH_EMULATE_INSTR:
394 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchEmulate);
395 break;
396 case VINF_EM_RAW_EMULATE_INSTR_LDT_FAULT:
397 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetLDTFault);
398 break;
399 case VINF_EM_RAW_EMULATE_INSTR_GDT_FAULT:
400 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetGDTFault);
401 break;
402 case VINF_EM_RAW_EMULATE_INSTR_IDT_FAULT:
403 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIDTFault);
404 break;
405 case VINF_EM_RAW_EMULATE_INSTR_TSS_FAULT:
406 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetTSSFault);
407 break;
408 case VINF_EM_RAW_EMULATE_INSTR_PD_FAULT:
409 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPDFault);
410 break;
411 case VINF_CSAM_PENDING_ACTION:
412 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetCSAMTask);
413 break;
414 case VINF_PGM_SYNC_CR3:
415 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetSyncCR3);
416 break;
417 case VINF_PATM_PATCH_INT3:
418 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchInt3);
419 break;
420 case VINF_PATM_PATCH_TRAP_PF:
421 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchPF);
422 break;
423 case VINF_PATM_PATCH_TRAP_GP:
424 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchGP);
425 break;
426 case VINF_PATM_PENDING_IRQ_AFTER_IRET:
427 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchIretIRQ);
428 break;
429 case VINF_EM_RESCHEDULE_REM:
430 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetRescheduleREM);
431 break;
432 case VINF_EM_RAW_TO_R3:
433 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3);
434 break;
435 case VINF_EM_RAW_TIMER_PENDING:
436 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetTimerPending);
437 break;
438 case VINF_EM_RAW_INTERRUPT_PENDING:
439 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetInterruptPending);
440 break;
441 case VINF_VMM_CALL_HOST:
442 switch (pVCpu->vmm.s.enmCallHostOperation)
443 {
444 case VMMCALLHOST_PDM_LOCK:
445 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPDMLock);
446 break;
447 case VMMCALLHOST_PDM_QUEUE_FLUSH:
448 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPDMQueueFlush);
449 break;
450 case VMMCALLHOST_PGM_POOL_GROW:
451 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMPoolGrow);
452 break;
453 case VMMCALLHOST_PGM_LOCK:
454 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMLock);
455 break;
456 case VMMCALLHOST_PGM_MAP_CHUNK:
457 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMMapChunk);
458 break;
459 case VMMCALLHOST_PGM_ALLOCATE_HANDY_PAGES:
460 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMAllocHandy);
461 break;
462 case VMMCALLHOST_REM_REPLAY_HANDLER_NOTIFICATIONS:
463 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallRemReplay);
464 break;
465 case VMMCALLHOST_VMM_LOGGER_FLUSH:
466 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallLogFlush);
467 break;
468 case VMMCALLHOST_VM_SET_ERROR:
469 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallVMSetError);
470 break;
471 case VMMCALLHOST_VM_SET_RUNTIME_ERROR:
472 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallVMSetRuntimeError);
473 break;
474 case VMMCALLHOST_VM_R0_ASSERTION:
475 default:
476 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetCallHost);
477 break;
478 }
479 break;
480 case VINF_PATM_DUPLICATE_FUNCTION:
481 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPATMDuplicateFn);
482 break;
483 case VINF_PGM_CHANGE_MODE:
484 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPGMChangeMode);
485 break;
486 case VINF_EM_RAW_EMULATE_INSTR_HLT:
487 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetEmulHlt);
488 break;
489 case VINF_EM_PENDING_REQUEST:
490 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPendingRequest);
491 break;
492 default:
493 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMisc);
494 break;
495 }
496}
497#endif /* VBOX_WITH_STATISTICS */
498
499
500/**
501 * Unused ring-0 entry point that used to be called from the interrupt gate.
502 *
503 * Will be removed one of the next times we do a major SUPDrv version bump.
504 *
505 * @returns VBox status code.
506 * @param pVM The VM to operate on.
507 * @param enmOperation Which operation to execute.
508 * @param pvArg Argument to the operation.
509 * @remarks Assume called with interrupts disabled.
510 */
511VMMR0DECL(int) VMMR0EntryInt(PVM pVM, VMMR0OPERATION enmOperation, void *pvArg)
512{
513 /*
514 * We're returning VERR_NOT_SUPPORT here so we've got something else
515 * than -1 which the interrupt gate glue code might return.
516 */
517 Log(("operation %#x is not supported\n", enmOperation));
518 return VERR_NOT_SUPPORTED;
519}
520
521
522/**
523 * The Ring 0 entry point, called by the fast-ioctl path.
524 *
525 * @param pVM The VM to operate on.
526 * The return code is stored in pVM->vmm.s.iLastGZRc.
527 * @param idCpu The Virtual CPU ID of the calling EMT.
528 * @param enmOperation Which operation to execute.
529 * @remarks Assume called with interrupts _enabled_.
530 */
531VMMR0DECL(void) VMMR0EntryFast(PVM pVM, VMCPUID idCpu, VMMR0OPERATION enmOperation)
532{
533 if (RT_UNLIKELY(idCpu >= pVM->cCPUs))
534 return;
535 PVMCPU pVCpu = &pVM->aCpus[idCpu];
536
537 switch (enmOperation)
538 {
539 /*
540 * Switch to GC and run guest raw mode code.
541 * Disable interrupts before doing the world switch.
542 */
543 case VMMR0_DO_RAW_RUN:
544 {
545 /* Safety precaution as hwaccm disables the switcher. */
546 if (RT_LIKELY(!pVM->vmm.s.fSwitcherDisabled))
547 {
548 RTCCUINTREG uFlags = ASMIntDisableFlags();
549 int rc;
550 bool fVTxDisabled;
551
552 if (RT_UNLIKELY(pVM->cCPUs > 1))
553 {
554 pVCpu->vmm.s.iLastGZRc = VERR_RAW_MODE_INVALID_SMP;
555 return;
556 }
557
558#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
559 if (RT_UNLIKELY(!PGMGetHyperCR3(pVCpu)))
560 {
561 pVCpu->vmm.s.iLastGZRc = VERR_PGM_NO_CR3_SHADOW_ROOT;
562 return;
563 }
564#endif
565
566 /* We might need to disable VT-x if the active switcher turns off paging. */
567 rc = HWACCMR0EnterSwitcher(pVM, &fVTxDisabled);
568 if (RT_FAILURE(rc))
569 {
570 pVCpu->vmm.s.iLastGZRc = rc;
571 return;
572 }
573
574 ASMAtomicWriteU32(&pVCpu->idHostCpu, RTMpCpuId());
575 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED_EXEC);
576
577 TMNotifyStartOfExecution(pVCpu);
578 rc = pVM->vmm.s.pfnHostToGuestR0(pVM);
579 pVCpu->vmm.s.iLastGZRc = rc;
580 TMNotifyEndOfExecution(pVCpu);
581
582 VMCPU_SET_STATE(pVCpu, VMCPUSTATE_STARTED);
583 ASMAtomicWriteU32(&pVCpu->idHostCpu, NIL_RTCPUID);
584
585 /* Re-enable VT-x if previously turned off. */
586 HWACCMR0LeaveSwitcher(pVM, fVTxDisabled);
587
588 if ( rc == VINF_EM_RAW_INTERRUPT
589 || rc == VINF_EM_RAW_INTERRUPT_HYPER)
590 TRPMR0DispatchHostInterrupt(pVM);
591
592 ASMSetFlags(uFlags);
593
594#ifdef VBOX_WITH_STATISTICS
595 STAM_COUNTER_INC(&pVM->vmm.s.StatRunRC);
596 vmmR0RecordRC(pVM, pVCpu, rc);
597#endif
598 }
599 else
600 {
601 Assert(!pVM->vmm.s.fSwitcherDisabled);
602 pVCpu->vmm.s.iLastGZRc = VERR_NOT_SUPPORTED;
603 }
604 break;
605 }
606
607 /*
608 * Run guest code using the available hardware acceleration technology.
609 *
610 * Disable interrupts before we do anything interesting. On Windows we avoid
611 * this by having the support driver raise the IRQL before calling us, this way
612 * we hope to get away with page faults and later calling into the kernel.
613 */
614 case VMMR0_DO_HWACC_RUN:
615 {
616 int rc;
617
618 STAM_COUNTER_INC(&pVM->vmm.s.StatRunRC);
619
620#ifdef VBOX_WITH_VMMR0_DISABLE_PREEMPTION
621 RTTHREADPREEMPTSTATE PreemptState = RTTHREADPREEMPTSTATE_INITIALIZER;
622 RTThreadPreemptDisable(&PreemptState);
623#elif !defined(RT_OS_WINDOWS)
624 RTCCUINTREG uFlags = ASMIntDisableFlags();
625#endif
626 ASMAtomicWriteU32(&pVCpu->idHostCpu, NIL_RTCPUID);
627
628#ifdef LOG_ENABLED
629 if (pVCpu->idCpu > 0)
630 {
631 /* Lazy registration of ring 0 loggers. */
632 PVMMR0LOGGER pR0Logger = pVCpu->vmm.s.pR0LoggerR0;
633 if ( pR0Logger
634 && !pR0Logger->fRegistered)
635 {
636 RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
637 pR0Logger->fRegistered = true;
638 }
639 }
640#endif
641 if (!HWACCMR0SuspendPending())
642 {
643 rc = HWACCMR0Enter(pVM, pVCpu);
644 if (RT_SUCCESS(rc))
645 {
646 rc = vmmR0CallHostSetJmp(&pVCpu->vmm.s.CallHostR0JmpBuf, HWACCMR0RunGuestCode, pVM, pVCpu); /* this may resume code. */
647 int rc2 = HWACCMR0Leave(pVM, pVCpu);
648 AssertRC(rc2);
649 }
650 }
651 else
652 {
653 /* System is about to go into suspend mode; go back to ring 3. */
654 rc = VINF_EM_RAW_INTERRUPT;
655 }
656 pVCpu->vmm.s.iLastGZRc = rc;
657
658 ASMAtomicWriteU32(&pVCpu->idHostCpu, NIL_RTCPUID);
659#ifdef VBOX_WITH_VMMR0_DISABLE_PREEMPTION
660 RTThreadPreemptRestore(&PreemptState);
661#elif !defined(RT_OS_WINDOWS)
662 ASMSetFlags(uFlags);
663#endif
664
665#ifdef VBOX_WITH_STATISTICS
666 vmmR0RecordRC(pVM, pVCpu, rc);
667#endif
668 /* No special action required for external interrupts, just return. */
669 break;
670 }
671
672 /*
673 * For profiling.
674 */
675 case VMMR0_DO_NOP:
676 pVCpu->vmm.s.iLastGZRc = VINF_SUCCESS;
677 break;
678
679 /*
680 * Impossible.
681 */
682 default:
683 AssertMsgFailed(("%#x\n", enmOperation));
684 pVCpu->vmm.s.iLastGZRc = VERR_NOT_SUPPORTED;
685 break;
686 }
687}
688
689
690/**
691 * Validates a session or VM session argument.
692 *
693 * @returns true / false accordingly.
694 * @param pVM The VM argument.
695 * @param pSession The session argument.
696 */
697DECLINLINE(bool) vmmR0IsValidSession(PVM pVM, PSUPDRVSESSION pClaimedSession, PSUPDRVSESSION pSession)
698{
699 /* This must be set! */
700 if (!pSession)
701 return false;
702
703 /* Only one out of the two. */
704 if (pVM && pClaimedSession)
705 return false;
706 if (pVM)
707 pClaimedSession = pVM->pSession;
708 return pClaimedSession == pSession;
709}
710
711
712/**
713 * VMMR0EntryEx worker function, either called directly or when ever possible
714 * called thru a longjmp so we can exit safely on failure.
715 *
716 * @returns VBox status code.
717 * @param pVM The VM to operate on.
718 * @param idCpu Virtual CPU ID argument. Must be NIL_VMCPUID if pVM
719 * is NIL_RTR0PTR, and may be NIL_VMCPUID if it isn't
720 * @param enmOperation Which operation to execute.
721 * @param pReqHdr This points to a SUPVMMR0REQHDR packet. Optional.
722 * The support driver validates this if it's present.
723 * @param u64Arg Some simple constant argument.
724 * @param pSession The session of the caller.
725 * @remarks Assume called with interrupts _enabled_.
726 */
727static int vmmR0EntryExWorker(PVM pVM, VMCPUID idCpu, VMMR0OPERATION enmOperation, PSUPVMMR0REQHDR pReqHdr, uint64_t u64Arg, PSUPDRVSESSION pSession)
728{
729 /*
730 * Common VM pointer validation.
731 */
732 if (pVM)
733 {
734 if (RT_UNLIKELY( !VALID_PTR(pVM)
735 || ((uintptr_t)pVM & PAGE_OFFSET_MASK)))
736 {
737 SUPR0Printf("vmmR0EntryExWorker: Invalid pVM=%p! (op=%d)\n", pVM, enmOperation);
738 return VERR_INVALID_POINTER;
739 }
740 if (RT_UNLIKELY( pVM->enmVMState < VMSTATE_CREATING
741 || pVM->enmVMState > VMSTATE_TERMINATED
742 || pVM->pVMR0 != pVM))
743 {
744 SUPR0Printf("vmmR0EntryExWorker: Invalid pVM=%p:{enmVMState=%d, .pVMR0=%p}! (op=%d)\n",
745 pVM, pVM->enmVMState, pVM->pVMR0, enmOperation);
746 return VERR_INVALID_POINTER;
747 }
748
749 if (RT_UNLIKELY(idCpu >= pVM->cCPUs && idCpu != NIL_VMCPUID))
750 {
751 SUPR0Printf("vmmR0EntryExWorker: Invalid idCpu (%u vs cCPUs=%u)\n", idCpu, pVM->cCPUs);
752 return VERR_INVALID_PARAMETER;
753 }
754 }
755 else if (RT_UNLIKELY(idCpu != NIL_VMCPUID))
756 {
757 SUPR0Printf("vmmR0EntryExWorker: Invalid idCpu=%u\n", idCpu);
758 return VERR_INVALID_PARAMETER;
759 }
760
761
762 switch (enmOperation)
763 {
764 /*
765 * GVM requests
766 */
767 case VMMR0_DO_GVMM_CREATE_VM:
768 if (pVM || u64Arg || idCpu != NIL_VMCPUID)
769 return VERR_INVALID_PARAMETER;
770 return GVMMR0CreateVMReq((PGVMMCREATEVMREQ)pReqHdr);
771
772 case VMMR0_DO_GVMM_DESTROY_VM:
773 if (pReqHdr || u64Arg)
774 return VERR_INVALID_PARAMETER;
775 return GVMMR0DestroyVM(pVM);
776
777 case VMMR0_DO_GVMM_REGISTER_VMCPU:
778 {
779 if (!pVM)
780 return VERR_INVALID_PARAMETER;
781 return GVMMR0RegisterVCpu(pVM, idCpu);
782 }
783
784 case VMMR0_DO_GVMM_SCHED_HALT:
785 if (pReqHdr)
786 return VERR_INVALID_PARAMETER;
787 return GVMMR0SchedHalt(pVM, idCpu, u64Arg);
788
789 case VMMR0_DO_GVMM_SCHED_WAKE_UP:
790 if (pReqHdr || u64Arg)
791 return VERR_INVALID_PARAMETER;
792 return GVMMR0SchedWakeUp(pVM, idCpu);
793
794 case VMMR0_DO_GVMM_SCHED_POKE:
795 if (pReqHdr || u64Arg)
796 return VERR_INVALID_PARAMETER;
797 return GVMMR0SchedPoke(pVM, idCpu);
798
799 case VMMR0_DO_GVMM_SCHED_WAKE_UP_AND_POKE_CPUS:
800 if (u64Arg)
801 return VERR_INVALID_PARAMETER;
802 return GVMMR0SchedWakeUpAndPokeCpusReq(pVM, (PGVMMSCHEDWAKEUPANDPOKECPUSREQ)pReqHdr);
803
804 case VMMR0_DO_GVMM_SCHED_POLL:
805 if (pReqHdr || u64Arg > 1)
806 return VERR_INVALID_PARAMETER;
807 return GVMMR0SchedPoll(pVM, idCpu, !!u64Arg);
808
809 case VMMR0_DO_GVMM_QUERY_STATISTICS:
810 if (u64Arg)
811 return VERR_INVALID_PARAMETER;
812 return GVMMR0QueryStatisticsReq(pVM, (PGVMMQUERYSTATISTICSSREQ)pReqHdr);
813
814 case VMMR0_DO_GVMM_RESET_STATISTICS:
815 if (u64Arg)
816 return VERR_INVALID_PARAMETER;
817 return GVMMR0ResetStatisticsReq(pVM, (PGVMMRESETSTATISTICSSREQ)pReqHdr);
818
819 /*
820 * Initialize the R0 part of a VM instance.
821 */
822 case VMMR0_DO_VMMR0_INIT:
823 return vmmR0InitVM(pVM, (uint32_t)u64Arg);
824
825 /*
826 * Terminate the R0 part of a VM instance.
827 */
828 case VMMR0_DO_VMMR0_TERM:
829 return VMMR0TermVM(pVM, NULL);
830
831 /*
832 * Attempt to enable hwacc mode and check the current setting.
833 *
834 */
835 case VMMR0_DO_HWACC_ENABLE:
836 return HWACCMR0EnableAllCpus(pVM);
837
838 /*
839 * Setup the hardware accelerated raw-mode session.
840 */
841 case VMMR0_DO_HWACC_SETUP_VM:
842 {
843 RTCCUINTREG fFlags = ASMIntDisableFlags();
844 int rc = HWACCMR0SetupVM(pVM);
845 ASMSetFlags(fFlags);
846 return rc;
847 }
848
849 /*
850 * Switch to RC to execute Hypervisor function.
851 */
852 case VMMR0_DO_CALL_HYPERVISOR:
853 {
854 int rc;
855 bool fVTxDisabled;
856
857 /* Safety precaution as HWACCM can disable the switcher. */
858 Assert(!pVM->vmm.s.fSwitcherDisabled);
859 if (RT_UNLIKELY(pVM->vmm.s.fSwitcherDisabled))
860 return VERR_NOT_SUPPORTED;
861
862#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
863 if (RT_UNLIKELY(!PGMGetHyperCR3(VMMGetCpu0(pVM))))
864 return VERR_PGM_NO_CR3_SHADOW_ROOT;
865#endif
866
867 RTCCUINTREG fFlags = ASMIntDisableFlags();
868
869 /* We might need to disable VT-x if the active switcher turns off paging. */
870 rc = HWACCMR0EnterSwitcher(pVM, &fVTxDisabled);
871 if (RT_FAILURE(rc))
872 return rc;
873
874 rc = pVM->vmm.s.pfnHostToGuestR0(pVM);
875
876 /* Re-enable VT-x if previously turned off. */
877 HWACCMR0LeaveSwitcher(pVM, fVTxDisabled);
878
879 /** @todo dispatch interrupts? */
880 ASMSetFlags(fFlags);
881 return rc;
882 }
883
884 /*
885 * PGM wrappers.
886 */
887 case VMMR0_DO_PGM_ALLOCATE_HANDY_PAGES:
888 if (idCpu == NIL_VMCPUID)
889 return VERR_INVALID_CPU_ID;
890 return PGMR0PhysAllocateHandyPages(pVM, &pVM->aCpus[idCpu]);
891
892 /*
893 * GMM wrappers.
894 */
895 case VMMR0_DO_GMM_INITIAL_RESERVATION:
896 if (u64Arg)
897 return VERR_INVALID_PARAMETER;
898 return GMMR0InitialReservationReq(pVM, idCpu, (PGMMINITIALRESERVATIONREQ)pReqHdr);
899
900 case VMMR0_DO_GMM_UPDATE_RESERVATION:
901 if (u64Arg)
902 return VERR_INVALID_PARAMETER;
903 return GMMR0UpdateReservationReq(pVM, idCpu, (PGMMUPDATERESERVATIONREQ)pReqHdr);
904
905 case VMMR0_DO_GMM_ALLOCATE_PAGES:
906 if (u64Arg)
907 return VERR_INVALID_PARAMETER;
908 return GMMR0AllocatePagesReq(pVM, idCpu, (PGMMALLOCATEPAGESREQ)pReqHdr);
909
910 case VMMR0_DO_GMM_FREE_PAGES:
911 if (u64Arg)
912 return VERR_INVALID_PARAMETER;
913 return GMMR0FreePagesReq(pVM, idCpu, (PGMMFREEPAGESREQ)pReqHdr);
914
915 case VMMR0_DO_GMM_BALLOONED_PAGES:
916 if (u64Arg)
917 return VERR_INVALID_PARAMETER;
918 return GMMR0BalloonedPagesReq(pVM, idCpu, (PGMMBALLOONEDPAGESREQ)pReqHdr);
919
920 case VMMR0_DO_GMM_DEFLATED_BALLOON:
921 if (pReqHdr)
922 return VERR_INVALID_PARAMETER;
923 return GMMR0DeflatedBalloon(pVM, idCpu, (uint32_t)u64Arg);
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 /*
936 * A quick GCFGM mock-up.
937 */
938 /** @todo GCFGM with proper access control, ring-3 management interface and all that. */
939 case VMMR0_DO_GCFGM_SET_VALUE:
940 case VMMR0_DO_GCFGM_QUERY_VALUE:
941 {
942 if (pVM || !pReqHdr || u64Arg || idCpu != NIL_VMCPUID)
943 return VERR_INVALID_PARAMETER;
944 PGCFGMVALUEREQ pReq = (PGCFGMVALUEREQ)pReqHdr;
945 if (pReq->Hdr.cbReq != sizeof(*pReq))
946 return VERR_INVALID_PARAMETER;
947 int rc;
948 if (enmOperation == VMMR0_DO_GCFGM_SET_VALUE)
949 {
950 rc = GVMMR0SetConfig(pReq->pSession, &pReq->szName[0], pReq->u64Value);
951 //if (rc == VERR_CFGM_VALUE_NOT_FOUND)
952 // rc = GMMR0SetConfig(pReq->pSession, &pReq->szName[0], pReq->u64Value);
953 }
954 else
955 {
956 rc = GVMMR0QueryConfig(pReq->pSession, &pReq->szName[0], &pReq->u64Value);
957 //if (rc == VERR_CFGM_VALUE_NOT_FOUND)
958 // rc = GMMR0QueryConfig(pReq->pSession, &pReq->szName[0], &pReq->u64Value);
959 }
960 return rc;
961 }
962
963
964 /*
965 * Requests to the internal networking service.
966 */
967 case VMMR0_DO_INTNET_OPEN:
968 {
969 PINTNETOPENREQ pReq = (PINTNETOPENREQ)pReqHdr;
970 if (u64Arg || !pReq || !vmmR0IsValidSession(pVM, pReq->pSession, pSession) || idCpu != NIL_VMCPUID)
971 return VERR_INVALID_PARAMETER;
972 if (!g_pIntNet)
973 return VERR_NOT_SUPPORTED;
974 return INTNETR0OpenReq(g_pIntNet, pSession, pReq);
975 }
976
977 case VMMR0_DO_INTNET_IF_CLOSE:
978 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFCLOSEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
979 return VERR_INVALID_PARAMETER;
980 if (!g_pIntNet)
981 return VERR_NOT_SUPPORTED;
982 return INTNETR0IfCloseReq(g_pIntNet, pSession, (PINTNETIFCLOSEREQ)pReqHdr);
983
984 case VMMR0_DO_INTNET_IF_GET_RING3_BUFFER:
985 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFGETRING3BUFFERREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
986 return VERR_INVALID_PARAMETER;
987 if (!g_pIntNet)
988 return VERR_NOT_SUPPORTED;
989 return INTNETR0IfGetRing3BufferReq(g_pIntNet, pSession, (PINTNETIFGETRING3BUFFERREQ)pReqHdr);
990
991 case VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE:
992 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETPROMISCUOUSMODEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
993 return VERR_INVALID_PARAMETER;
994 if (!g_pIntNet)
995 return VERR_NOT_SUPPORTED;
996 return INTNETR0IfSetPromiscuousModeReq(g_pIntNet, pSession, (PINTNETIFSETPROMISCUOUSMODEREQ)pReqHdr);
997
998 case VMMR0_DO_INTNET_IF_SET_MAC_ADDRESS:
999 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETMACADDRESSREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1000 return VERR_INVALID_PARAMETER;
1001 if (!g_pIntNet)
1002 return VERR_NOT_SUPPORTED;
1003 return INTNETR0IfSetMacAddressReq(g_pIntNet, pSession, (PINTNETIFSETMACADDRESSREQ)pReqHdr);
1004
1005 case VMMR0_DO_INTNET_IF_SET_ACTIVE:
1006 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETACTIVEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1007 return VERR_INVALID_PARAMETER;
1008 if (!g_pIntNet)
1009 return VERR_NOT_SUPPORTED;
1010 return INTNETR0IfSetActiveReq(g_pIntNet, pSession, (PINTNETIFSETACTIVEREQ)pReqHdr);
1011
1012 case VMMR0_DO_INTNET_IF_SEND:
1013 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSENDREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1014 return VERR_INVALID_PARAMETER;
1015 if (!g_pIntNet)
1016 return VERR_NOT_SUPPORTED;
1017 return INTNETR0IfSendReq(g_pIntNet, pSession, (PINTNETIFSENDREQ)pReqHdr);
1018
1019 case VMMR0_DO_INTNET_IF_WAIT:
1020 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFWAITREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1021 return VERR_INVALID_PARAMETER;
1022 if (!g_pIntNet)
1023 return VERR_NOT_SUPPORTED;
1024 return INTNETR0IfWaitReq(g_pIntNet, pSession, (PINTNETIFWAITREQ)pReqHdr);
1025
1026 /*
1027 * For profiling.
1028 */
1029 case VMMR0_DO_NOP:
1030 case VMMR0_DO_SLOW_NOP:
1031 return VINF_SUCCESS;
1032
1033 /*
1034 * For testing Ring-0 APIs invoked in this environment.
1035 */
1036 case VMMR0_DO_TESTS:
1037 /** @todo make new test */
1038 return VINF_SUCCESS;
1039
1040
1041#if HC_ARCH_BITS == 32 && defined(VBOX_WITH_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
1042 case VMMR0_DO_TEST_SWITCHER3264:
1043 if (idCpu == NIL_VMCPUID)
1044 return VERR_INVALID_CPU_ID;
1045 return HWACCMR0TestSwitcher3264(pVM);
1046#endif
1047 default:
1048 /*
1049 * We're returning VERR_NOT_SUPPORT here so we've got something else
1050 * than -1 which the interrupt gate glue code might return.
1051 */
1052 Log(("operation %#x is not supported\n", enmOperation));
1053 return VERR_NOT_SUPPORTED;
1054 }
1055}
1056
1057
1058/**
1059 * Argument for vmmR0EntryExWrapper containing the arguments for VMMR0EntryEx.
1060 */
1061typedef struct VMMR0ENTRYEXARGS
1062{
1063 PVM pVM;
1064 VMCPUID idCpu;
1065 VMMR0OPERATION enmOperation;
1066 PSUPVMMR0REQHDR pReq;
1067 uint64_t u64Arg;
1068 PSUPDRVSESSION pSession;
1069} VMMR0ENTRYEXARGS;
1070/** Pointer to a vmmR0EntryExWrapper argument package. */
1071typedef VMMR0ENTRYEXARGS *PVMMR0ENTRYEXARGS;
1072
1073/**
1074 * This is just a longjmp wrapper function for VMMR0EntryEx calls.
1075 *
1076 * @returns VBox status code.
1077 * @param pvArgs The argument package
1078 */
1079static int vmmR0EntryExWrapper(void *pvArgs)
1080{
1081 return vmmR0EntryExWorker(((PVMMR0ENTRYEXARGS)pvArgs)->pVM,
1082 ((PVMMR0ENTRYEXARGS)pvArgs)->idCpu,
1083 ((PVMMR0ENTRYEXARGS)pvArgs)->enmOperation,
1084 ((PVMMR0ENTRYEXARGS)pvArgs)->pReq,
1085 ((PVMMR0ENTRYEXARGS)pvArgs)->u64Arg,
1086 ((PVMMR0ENTRYEXARGS)pvArgs)->pSession);
1087}
1088
1089
1090/**
1091 * The Ring 0 entry point, called by the support library (SUP).
1092 *
1093 * @returns VBox status code.
1094 * @param pVM The VM to operate on.
1095 * @param idCpu Virtual CPU ID argument. Must be NIL_VMCPUID if pVM
1096 * is NIL_RTR0PTR, and may be NIL_VMCPUID if it isn't
1097 * @param enmOperation Which operation to execute.
1098 * @param pReq This points to a SUPVMMR0REQHDR packet. Optional.
1099 * @param u64Arg Some simple constant argument.
1100 * @param pSession The session of the caller.
1101 * @remarks Assume called with interrupts _enabled_.
1102 */
1103VMMR0DECL(int) VMMR0EntryEx(PVM pVM, VMCPUID idCpu, VMMR0OPERATION enmOperation, PSUPVMMR0REQHDR pReq, uint64_t u64Arg, PSUPDRVSESSION pSession)
1104{
1105 /*
1106 * Requests that should only happen on the EMT thread will be
1107 * wrapped in a setjmp so we can assert without causing trouble.
1108 */
1109 if ( VALID_PTR(pVM)
1110 && pVM->pVMR0
1111 && idCpu < pVM->cCPUs)
1112 {
1113 switch (enmOperation)
1114 {
1115 /* These might/will be called before VMMR3Init. */
1116 case VMMR0_DO_GMM_INITIAL_RESERVATION:
1117 case VMMR0_DO_GMM_UPDATE_RESERVATION:
1118 case VMMR0_DO_GMM_ALLOCATE_PAGES:
1119 case VMMR0_DO_GMM_FREE_PAGES:
1120 case VMMR0_DO_GMM_BALLOONED_PAGES:
1121 case VMMR0_DO_GMM_DEFLATED_BALLOON:
1122 /* On the mac we might not have a valid jmp buf, so check these as well. */
1123 case VMMR0_DO_VMMR0_INIT:
1124 case VMMR0_DO_VMMR0_TERM:
1125 {
1126 PVMCPU pVCpu = &pVM->aCpus[idCpu];
1127
1128 if (!pVCpu->vmm.s.CallHostR0JmpBuf.pvSavedStack)
1129 break;
1130
1131 /** @todo validate this EMT claim... GVM knows. */
1132 VMMR0ENTRYEXARGS Args;
1133 Args.pVM = pVM;
1134 Args.idCpu = idCpu;
1135 Args.enmOperation = enmOperation;
1136 Args.pReq = pReq;
1137 Args.u64Arg = u64Arg;
1138 Args.pSession = pSession;
1139 return vmmR0CallHostSetJmpEx(&pVCpu->vmm.s.CallHostR0JmpBuf, vmmR0EntryExWrapper, &Args);
1140 }
1141
1142 default:
1143 break;
1144 }
1145 }
1146 return vmmR0EntryExWorker(pVM, idCpu, enmOperation, pReq, u64Arg, pSession);
1147}
1148
1149/**
1150 * Internal R0 logger worker: Flush logger.
1151 *
1152 * @param pLogger The logger instance to flush.
1153 * @remark This function must be exported!
1154 */
1155VMMR0DECL(void) vmmR0LoggerFlush(PRTLOGGER pLogger)
1156{
1157#ifdef LOG_ENABLED
1158 /*
1159 * Convert the pLogger into a VM handle and 'call' back to Ring-3.
1160 * (This is a bit paranoid code.)
1161 */
1162 PVMMR0LOGGER pR0Logger = (PVMMR0LOGGER)((uintptr_t)pLogger - RT_OFFSETOF(VMMR0LOGGER, Logger));
1163 if ( !VALID_PTR(pR0Logger)
1164 || !VALID_PTR(pR0Logger + 1)
1165 || pLogger->u32Magic != RTLOGGER_MAGIC)
1166 {
1167# ifdef DEBUG
1168 SUPR0Printf("vmmR0LoggerFlush: pLogger=%p!\n", pLogger);
1169# endif
1170 return;
1171 }
1172 if (pR0Logger->fFlushingDisabled)
1173 return; /* quietly */
1174
1175 PVM pVM = pR0Logger->pVM;
1176 if ( !VALID_PTR(pVM)
1177 || pVM->pVMR0 != pVM)
1178 {
1179# ifdef DEBUG
1180 SUPR0Printf("vmmR0LoggerFlush: pVM=%p! pVMR0=%p! pLogger=%p\n", pVM, pVM->pVMR0, pLogger);
1181# endif
1182 return;
1183 }
1184
1185 PVMCPU pVCpu = VMMGetCpu(pVM);
1186
1187 /*
1188 * Check that the jump buffer is armed.
1189 */
1190# ifdef RT_ARCH_X86
1191 if ( !pVCpu->vmm.s.CallHostR0JmpBuf.eip
1192 || pVCpu->vmm.s.CallHostR0JmpBuf.fInRing3Call)
1193# else
1194 if ( !pVCpu->vmm.s.CallHostR0JmpBuf.rip
1195 || pVCpu->vmm.s.CallHostR0JmpBuf.fInRing3Call)
1196# endif
1197 {
1198# ifdef DEBUG
1199 SUPR0Printf("vmmR0LoggerFlush: Jump buffer isn't armed!\n");
1200# endif
1201 return;
1202 }
1203 VMMR0CallHost(pVM, VMMCALLHOST_VMM_LOGGER_FLUSH, 0);
1204#endif
1205}
1206
1207/**
1208 * Interal R0 logger worker: Custom prefix.
1209 *
1210 * @returns Number of chars written.
1211 *
1212 * @param pLogger The logger instance.
1213 * @param pchBuf The output buffer.
1214 * @param cchBuf The size of the buffer.
1215 * @param pvUser User argument (ignored).
1216 */
1217VMMR0DECL(size_t) vmmR0LoggerPrefix(PRTLOGGER pLogger, char *pchBuf, size_t cchBuf, void *pvUser)
1218{
1219 NOREF(pvUser);
1220#ifdef LOG_ENABLED
1221 PVMMR0LOGGER pR0Logger = (PVMMR0LOGGER)((uintptr_t)pLogger - RT_OFFSETOF(VMMR0LOGGER, Logger));
1222 if ( !VALID_PTR(pR0Logger)
1223 || !VALID_PTR(pR0Logger + 1)
1224 || pLogger->u32Magic != RTLOGGER_MAGIC
1225 || cchBuf < 2)
1226 return 0;
1227
1228 static const char s_szHex[17] = "0123456789abcdef";
1229 VMCPUID const idCpu = pR0Logger->idCpu;
1230 pchBuf[1] = s_szHex[ idCpu & 15];
1231 pchBuf[0] = s_szHex[(idCpu >> 4) & 15];
1232
1233 return 2;
1234#else
1235 return 0;
1236#endif
1237}
1238
1239
1240#ifdef LOG_ENABLED
1241/**
1242 * Disables flushing of the ring-0 debug log.
1243 *
1244 * @param pVCpu The shared virtual cpu structure.
1245 */
1246VMMR0DECL(void) VMMR0LogFlushDisable(PVMCPU pVCpu)
1247{
1248 PVM pVM = pVCpu->pVMR0;
1249 if (pVCpu->vmm.s.pR0LoggerR0)
1250 pVCpu->vmm.s.pR0LoggerR0->fFlushingDisabled = true;
1251}
1252
1253
1254/**
1255 * Enables flushing of the ring-0 debug log.
1256 *
1257 * @param pVCpu The shared virtual cpu structure.
1258 */
1259VMMR0DECL(void) VMMR0LogFlushEnable(PVMCPU pVCpu)
1260{
1261 PVM pVM = pVCpu->pVMR0;
1262 if (pVCpu->vmm.s.pR0LoggerR0)
1263 pVCpu->vmm.s.pR0LoggerR0->fFlushingDisabled = false;
1264}
1265#endif
1266
1267/**
1268 * Jump back to ring-3 if we're the EMT and the longjmp is armed.
1269 *
1270 * @returns true if the breakpoint should be hit, false if it should be ignored.
1271 */
1272DECLEXPORT(bool) RTCALL RTAssertShouldPanic(void)
1273{
1274#if 0
1275 return true;
1276#else
1277 PVM pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
1278 if (pVM)
1279 {
1280 PVMCPU pVCpu = VMMGetCpu(pVM);
1281
1282#ifdef RT_ARCH_X86
1283 if ( pVCpu->vmm.s.CallHostR0JmpBuf.eip
1284 && !pVCpu->vmm.s.CallHostR0JmpBuf.fInRing3Call)
1285#else
1286 if ( pVCpu->vmm.s.CallHostR0JmpBuf.rip
1287 && !pVCpu->vmm.s.CallHostR0JmpBuf.fInRing3Call)
1288#endif
1289 {
1290 int rc = VMMR0CallHost(pVM, VMMCALLHOST_VM_R0_ASSERTION, 0);
1291 return RT_FAILURE_NP(rc);
1292 }
1293 }
1294#ifdef RT_OS_LINUX
1295 return true;
1296#else
1297 return false;
1298#endif
1299#endif
1300}
1301
1302
1303/**
1304 * Override this so we can push it up to ring-3.
1305 *
1306 * @param pszExpr Expression. Can be NULL.
1307 * @param uLine Location line number.
1308 * @param pszFile Location file name.
1309 * @param pszFunction Location function name.
1310 */
1311DECLEXPORT(void) RTCALL AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
1312{
1313#if !defined(DEBUG_sandervl) && !defined(RT_OS_DARWIN)
1314 SUPR0Printf("\n!!R0-Assertion Failed!!\n"
1315 "Expression: %s\n"
1316 "Location : %s(%d) %s\n",
1317 pszExpr, pszFile, uLine, pszFunction);
1318#endif
1319 LogAlways(("\n!!R0-Assertion Failed!!\n"
1320 "Expression: %s\n"
1321 "Location : %s(%d) %s\n",
1322 pszExpr, pszFile, uLine, pszFunction));
1323
1324 PVM pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
1325 if (pVM)
1326 RTStrPrintf(pVM->vmm.s.szRing0AssertMsg1, sizeof(pVM->vmm.s.szRing0AssertMsg1),
1327 "\n!!R0-Assertion Failed!!\n"
1328 "Expression: %s\n"
1329 "Location : %s(%d) %s\n",
1330 pszExpr, pszFile, uLine, pszFunction);
1331#ifdef RT_OS_DARWIN
1332 RTAssertMsg1(pszExpr, uLine, pszFile, pszFunction);
1333#endif
1334}
1335
1336
1337/**
1338 * Callback for RTLogFormatV which writes to the ring-3 log port.
1339 * See PFNLOGOUTPUT() for details.
1340 */
1341static DECLCALLBACK(size_t) rtLogOutput(void *pv, const char *pachChars, size_t cbChars)
1342{
1343 for (size_t i = 0; i < cbChars; i++)
1344 {
1345#if !defined(DEBUG_sandervl) && !defined(RT_OS_DARWIN)
1346 SUPR0Printf("%c", pachChars[i]);
1347#endif
1348 LogAlways(("%c", pachChars[i]));
1349 }
1350
1351 return cbChars;
1352}
1353
1354
1355DECLEXPORT(void) RTCALL AssertMsg2(const char *pszFormat, ...)
1356{
1357 va_list va;
1358
1359 PRTLOGGER pLog = RTLogDefaultInstance(); /** @todo we want this for release as well! */
1360 if (pLog)
1361 {
1362 va_start(va, pszFormat);
1363 RTLogFormatV(rtLogOutput, pLog, pszFormat, va);
1364 va_end(va);
1365
1366 PVM pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
1367 if (pVM)
1368 {
1369 va_start(va, pszFormat);
1370 RTStrPrintfV(pVM->vmm.s.szRing0AssertMsg2, sizeof(pVM->vmm.s.szRing0AssertMsg2), pszFormat, va);
1371 va_end(va);
1372 }
1373 }
1374
1375#ifdef RT_OS_DARWIN
1376 va_start(va, pszFormat);
1377 RTAssertMsg2V(pszFormat, va);
1378 va_end(va);
1379#endif
1380}
1381
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