VirtualBox

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

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

VMM: Renamed almost all references to CallHost to CallRing3.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 45.4 KB
Line 
1/* $Id: VMMR0.cpp 20875 2009-06-24 02:29:17Z 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_EXCEPTION_PRIVILEGED:
342 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetExceptionPrivilege);
343 break;
344 case VINF_EM_RAW_STALE_SELECTOR:
345 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetStaleSelector);
346 break;
347 case VINF_EM_RAW_IRET_TRAP:
348 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIRETTrap);
349 break;
350 case VINF_IOM_HC_IOPORT_READ:
351 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIORead);
352 break;
353 case VINF_IOM_HC_IOPORT_WRITE:
354 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIOWrite);
355 break;
356 case VINF_IOM_HC_MMIO_READ:
357 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIORead);
358 break;
359 case VINF_IOM_HC_MMIO_WRITE:
360 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOWrite);
361 break;
362 case VINF_IOM_HC_MMIO_READ_WRITE:
363 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOReadWrite);
364 break;
365 case VINF_PATM_HC_MMIO_PATCH_READ:
366 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOPatchRead);
367 break;
368 case VINF_PATM_HC_MMIO_PATCH_WRITE:
369 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetMMIOPatchWrite);
370 break;
371 case VINF_EM_RAW_EMULATE_INSTR:
372 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetEmulate);
373 break;
374 case VINF_EM_RAW_EMULATE_IO_BLOCK:
375 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIOBlockEmulate);
376 break;
377 case VINF_PATCH_EMULATE_INSTR:
378 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchEmulate);
379 break;
380 case VINF_EM_RAW_EMULATE_INSTR_LDT_FAULT:
381 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetLDTFault);
382 break;
383 case VINF_EM_RAW_EMULATE_INSTR_GDT_FAULT:
384 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetGDTFault);
385 break;
386 case VINF_EM_RAW_EMULATE_INSTR_IDT_FAULT:
387 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetIDTFault);
388 break;
389 case VINF_EM_RAW_EMULATE_INSTR_TSS_FAULT:
390 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetTSSFault);
391 break;
392 case VINF_EM_RAW_EMULATE_INSTR_PD_FAULT:
393 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPDFault);
394 break;
395 case VINF_CSAM_PENDING_ACTION:
396 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetCSAMTask);
397 break;
398 case VINF_PGM_SYNC_CR3:
399 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetSyncCR3);
400 break;
401 case VINF_PATM_PATCH_INT3:
402 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchInt3);
403 break;
404 case VINF_PATM_PATCH_TRAP_PF:
405 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchPF);
406 break;
407 case VINF_PATM_PATCH_TRAP_GP:
408 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchGP);
409 break;
410 case VINF_PATM_PENDING_IRQ_AFTER_IRET:
411 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPatchIretIRQ);
412 break;
413 case VINF_EM_RESCHEDULE_REM:
414 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetRescheduleREM);
415 break;
416 case VINF_EM_RAW_TO_R3:
417 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetToR3);
418 break;
419 case VINF_EM_RAW_TIMER_PENDING:
420 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetTimerPending);
421 break;
422 case VINF_EM_RAW_INTERRUPT_PENDING:
423 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetInterruptPending);
424 break;
425 case VINF_VMM_CALL_HOST:
426 switch (pVCpu->vmm.s.enmCallRing3Operation)
427 {
428 case VMMCALLRING3_PDM_LOCK:
429 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPDMLock);
430 break;
431 case VMMCALLRING3_PDM_QUEUE_FLUSH:
432 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPDMQueueFlush);
433 break;
434 case VMMCALLRING3_PGM_POOL_GROW:
435 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMPoolGrow);
436 break;
437 case VMMCALLRING3_PGM_LOCK:
438 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMLock);
439 break;
440 case VMMCALLRING3_PGM_MAP_CHUNK:
441 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMMapChunk);
442 break;
443 case VMMCALLRING3_PGM_ALLOCATE_HANDY_PAGES:
444 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallPGMAllocHandy);
445 break;
446 case VMMCALLRING3_REM_REPLAY_HANDLER_NOTIFICATIONS:
447 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallRemReplay);
448 break;
449 case VMMCALLRING3_VMM_LOGGER_FLUSH:
450 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallLogFlush);
451 break;
452 case VMMCALLRING3_VM_SET_ERROR:
453 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallVMSetError);
454 break;
455 case VMMCALLRING3_VM_SET_RUNTIME_ERROR:
456 STAM_COUNTER_INC(&pVM->vmm.s.StatRZCallVMSetRuntimeError);
457 break;
458 case VMMCALLRING3_VM_R0_ASSERTION:
459 default:
460 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetCallRing3);
461 break;
462 }
463 break;
464 case VINF_PATM_DUPLICATE_FUNCTION:
465 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPATMDuplicateFn);
466 break;
467 case VINF_PGM_CHANGE_MODE:
468 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPGMChangeMode);
469 break;
470 case VINF_EM_RAW_EMULATE_INSTR_HLT:
471 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetEmulHlt);
472 break;
473 case VINF_EM_PENDING_REQUEST:
474 STAM_COUNTER_INC(&pVM->vmm.s.StatRZRetPendingRequest);
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 return;
540 }
541
542#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
543 if (RT_UNLIKELY(!PGMGetHyperCR3(pVCpu)))
544 {
545 pVCpu->vmm.s.iLastGZRc = VERR_PGM_NO_CR3_SHADOW_ROOT;
546 return;
547 }
548#endif
549
550 /* We might need to disable VT-x if the active switcher turns off paging. */
551 rc = HWACCMR0EnterSwitcher(pVM, &fVTxDisabled);
552 if (RT_FAILURE(rc))
553 {
554 pVCpu->vmm.s.iLastGZRc = rc;
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, NIL_RTCPUID);
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 */
819 case VMMR0_DO_HWACC_ENABLE:
820 return HWACCMR0EnableAllCpus(pVM);
821
822 /*
823 * Setup the hardware accelerated raw-mode session.
824 */
825 case VMMR0_DO_HWACC_SETUP_VM:
826 {
827 RTCCUINTREG fFlags = ASMIntDisableFlags();
828 int rc = HWACCMR0SetupVM(pVM);
829 ASMSetFlags(fFlags);
830 return rc;
831 }
832
833 /*
834 * Switch to RC to execute Hypervisor function.
835 */
836 case VMMR0_DO_CALL_HYPERVISOR:
837 {
838 int rc;
839 bool fVTxDisabled;
840
841 /* Safety precaution as HWACCM can disable the switcher. */
842 Assert(!pVM->vmm.s.fSwitcherDisabled);
843 if (RT_UNLIKELY(pVM->vmm.s.fSwitcherDisabled))
844 return VERR_NOT_SUPPORTED;
845
846#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
847 if (RT_UNLIKELY(!PGMGetHyperCR3(VMMGetCpu0(pVM))))
848 return VERR_PGM_NO_CR3_SHADOW_ROOT;
849#endif
850
851 RTCCUINTREG fFlags = ASMIntDisableFlags();
852
853 /* We might need to disable VT-x if the active switcher turns off paging. */
854 rc = HWACCMR0EnterSwitcher(pVM, &fVTxDisabled);
855 if (RT_FAILURE(rc))
856 return rc;
857
858 rc = pVM->vmm.s.pfnHostToGuestR0(pVM);
859
860 /* Re-enable VT-x if previously turned off. */
861 HWACCMR0LeaveSwitcher(pVM, fVTxDisabled);
862
863 /** @todo dispatch interrupts? */
864 ASMSetFlags(fFlags);
865 return rc;
866 }
867
868 /*
869 * PGM wrappers.
870 */
871 case VMMR0_DO_PGM_ALLOCATE_HANDY_PAGES:
872 if (idCpu == NIL_VMCPUID)
873 return VERR_INVALID_CPU_ID;
874 return PGMR0PhysAllocateHandyPages(pVM, &pVM->aCpus[idCpu]);
875
876 /*
877 * GMM wrappers.
878 */
879 case VMMR0_DO_GMM_INITIAL_RESERVATION:
880 if (u64Arg)
881 return VERR_INVALID_PARAMETER;
882 return GMMR0InitialReservationReq(pVM, idCpu, (PGMMINITIALRESERVATIONREQ)pReqHdr);
883
884 case VMMR0_DO_GMM_UPDATE_RESERVATION:
885 if (u64Arg)
886 return VERR_INVALID_PARAMETER;
887 return GMMR0UpdateReservationReq(pVM, idCpu, (PGMMUPDATERESERVATIONREQ)pReqHdr);
888
889 case VMMR0_DO_GMM_ALLOCATE_PAGES:
890 if (u64Arg)
891 return VERR_INVALID_PARAMETER;
892 return GMMR0AllocatePagesReq(pVM, idCpu, (PGMMALLOCATEPAGESREQ)pReqHdr);
893
894 case VMMR0_DO_GMM_FREE_PAGES:
895 if (u64Arg)
896 return VERR_INVALID_PARAMETER;
897 return GMMR0FreePagesReq(pVM, idCpu, (PGMMFREEPAGESREQ)pReqHdr);
898
899 case VMMR0_DO_GMM_BALLOONED_PAGES:
900 if (u64Arg)
901 return VERR_INVALID_PARAMETER;
902 return GMMR0BalloonedPagesReq(pVM, idCpu, (PGMMBALLOONEDPAGESREQ)pReqHdr);
903
904 case VMMR0_DO_GMM_DEFLATED_BALLOON:
905 if (pReqHdr)
906 return VERR_INVALID_PARAMETER;
907 return GMMR0DeflatedBalloon(pVM, idCpu, (uint32_t)u64Arg);
908
909 case VMMR0_DO_GMM_MAP_UNMAP_CHUNK:
910 if (u64Arg)
911 return VERR_INVALID_PARAMETER;
912 return GMMR0MapUnmapChunkReq(pVM, idCpu, (PGMMMAPUNMAPCHUNKREQ)pReqHdr);
913
914 case VMMR0_DO_GMM_SEED_CHUNK:
915 if (pReqHdr)
916 return VERR_INVALID_PARAMETER;
917 return GMMR0SeedChunk(pVM, idCpu, (RTR3PTR)u64Arg);
918
919 /*
920 * A quick GCFGM mock-up.
921 */
922 /** @todo GCFGM with proper access control, ring-3 management interface and all that. */
923 case VMMR0_DO_GCFGM_SET_VALUE:
924 case VMMR0_DO_GCFGM_QUERY_VALUE:
925 {
926 if (pVM || !pReqHdr || u64Arg || idCpu != NIL_VMCPUID)
927 return VERR_INVALID_PARAMETER;
928 PGCFGMVALUEREQ pReq = (PGCFGMVALUEREQ)pReqHdr;
929 if (pReq->Hdr.cbReq != sizeof(*pReq))
930 return VERR_INVALID_PARAMETER;
931 int rc;
932 if (enmOperation == VMMR0_DO_GCFGM_SET_VALUE)
933 {
934 rc = GVMMR0SetConfig(pReq->pSession, &pReq->szName[0], pReq->u64Value);
935 //if (rc == VERR_CFGM_VALUE_NOT_FOUND)
936 // rc = GMMR0SetConfig(pReq->pSession, &pReq->szName[0], pReq->u64Value);
937 }
938 else
939 {
940 rc = GVMMR0QueryConfig(pReq->pSession, &pReq->szName[0], &pReq->u64Value);
941 //if (rc == VERR_CFGM_VALUE_NOT_FOUND)
942 // rc = GMMR0QueryConfig(pReq->pSession, &pReq->szName[0], &pReq->u64Value);
943 }
944 return rc;
945 }
946
947
948 /*
949 * Requests to the internal networking service.
950 */
951 case VMMR0_DO_INTNET_OPEN:
952 {
953 PINTNETOPENREQ pReq = (PINTNETOPENREQ)pReqHdr;
954 if (u64Arg || !pReq || !vmmR0IsValidSession(pVM, pReq->pSession, pSession) || idCpu != NIL_VMCPUID)
955 return VERR_INVALID_PARAMETER;
956 if (!g_pIntNet)
957 return VERR_NOT_SUPPORTED;
958 return INTNETR0OpenReq(g_pIntNet, pSession, pReq);
959 }
960
961 case VMMR0_DO_INTNET_IF_CLOSE:
962 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFCLOSEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
963 return VERR_INVALID_PARAMETER;
964 if (!g_pIntNet)
965 return VERR_NOT_SUPPORTED;
966 return INTNETR0IfCloseReq(g_pIntNet, pSession, (PINTNETIFCLOSEREQ)pReqHdr);
967
968 case VMMR0_DO_INTNET_IF_GET_RING3_BUFFER:
969 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFGETRING3BUFFERREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
970 return VERR_INVALID_PARAMETER;
971 if (!g_pIntNet)
972 return VERR_NOT_SUPPORTED;
973 return INTNETR0IfGetRing3BufferReq(g_pIntNet, pSession, (PINTNETIFGETRING3BUFFERREQ)pReqHdr);
974
975 case VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE:
976 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETPROMISCUOUSMODEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
977 return VERR_INVALID_PARAMETER;
978 if (!g_pIntNet)
979 return VERR_NOT_SUPPORTED;
980 return INTNETR0IfSetPromiscuousModeReq(g_pIntNet, pSession, (PINTNETIFSETPROMISCUOUSMODEREQ)pReqHdr);
981
982 case VMMR0_DO_INTNET_IF_SET_MAC_ADDRESS:
983 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETMACADDRESSREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
984 return VERR_INVALID_PARAMETER;
985 if (!g_pIntNet)
986 return VERR_NOT_SUPPORTED;
987 return INTNETR0IfSetMacAddressReq(g_pIntNet, pSession, (PINTNETIFSETMACADDRESSREQ)pReqHdr);
988
989 case VMMR0_DO_INTNET_IF_SET_ACTIVE:
990 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSETACTIVEREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
991 return VERR_INVALID_PARAMETER;
992 if (!g_pIntNet)
993 return VERR_NOT_SUPPORTED;
994 return INTNETR0IfSetActiveReq(g_pIntNet, pSession, (PINTNETIFSETACTIVEREQ)pReqHdr);
995
996 case VMMR0_DO_INTNET_IF_SEND:
997 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFSENDREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
998 return VERR_INVALID_PARAMETER;
999 if (!g_pIntNet)
1000 return VERR_NOT_SUPPORTED;
1001 return INTNETR0IfSendReq(g_pIntNet, pSession, (PINTNETIFSENDREQ)pReqHdr);
1002
1003 case VMMR0_DO_INTNET_IF_WAIT:
1004 if (u64Arg || !pReqHdr || !vmmR0IsValidSession(pVM, ((PINTNETIFWAITREQ)pReqHdr)->pSession, pSession) || idCpu != NIL_VMCPUID)
1005 return VERR_INVALID_PARAMETER;
1006 if (!g_pIntNet)
1007 return VERR_NOT_SUPPORTED;
1008 return INTNETR0IfWaitReq(g_pIntNet, pSession, (PINTNETIFWAITREQ)pReqHdr);
1009
1010 /*
1011 * For profiling.
1012 */
1013 case VMMR0_DO_NOP:
1014 case VMMR0_DO_SLOW_NOP:
1015 return VINF_SUCCESS;
1016
1017 /*
1018 * For testing Ring-0 APIs invoked in this environment.
1019 */
1020 case VMMR0_DO_TESTS:
1021 /** @todo make new test */
1022 return VINF_SUCCESS;
1023
1024
1025#if HC_ARCH_BITS == 32 && defined(VBOX_WITH_64_BITS_GUESTS) && !defined(VBOX_WITH_HYBRID_32BIT_KERNEL)
1026 case VMMR0_DO_TEST_SWITCHER3264:
1027 if (idCpu == NIL_VMCPUID)
1028 return VERR_INVALID_CPU_ID;
1029 return HWACCMR0TestSwitcher3264(pVM);
1030#endif
1031 default:
1032 /*
1033 * We're returning VERR_NOT_SUPPORT here so we've got something else
1034 * than -1 which the interrupt gate glue code might return.
1035 */
1036 Log(("operation %#x is not supported\n", enmOperation));
1037 return VERR_NOT_SUPPORTED;
1038 }
1039}
1040
1041
1042/**
1043 * Argument for vmmR0EntryExWrapper containing the arguments for VMMR0EntryEx.
1044 */
1045typedef struct VMMR0ENTRYEXARGS
1046{
1047 PVM pVM;
1048 VMCPUID idCpu;
1049 VMMR0OPERATION enmOperation;
1050 PSUPVMMR0REQHDR pReq;
1051 uint64_t u64Arg;
1052 PSUPDRVSESSION pSession;
1053} VMMR0ENTRYEXARGS;
1054/** Pointer to a vmmR0EntryExWrapper argument package. */
1055typedef VMMR0ENTRYEXARGS *PVMMR0ENTRYEXARGS;
1056
1057/**
1058 * This is just a longjmp wrapper function for VMMR0EntryEx calls.
1059 *
1060 * @returns VBox status code.
1061 * @param pvArgs The argument package
1062 */
1063static int vmmR0EntryExWrapper(void *pvArgs)
1064{
1065 return vmmR0EntryExWorker(((PVMMR0ENTRYEXARGS)pvArgs)->pVM,
1066 ((PVMMR0ENTRYEXARGS)pvArgs)->idCpu,
1067 ((PVMMR0ENTRYEXARGS)pvArgs)->enmOperation,
1068 ((PVMMR0ENTRYEXARGS)pvArgs)->pReq,
1069 ((PVMMR0ENTRYEXARGS)pvArgs)->u64Arg,
1070 ((PVMMR0ENTRYEXARGS)pvArgs)->pSession);
1071}
1072
1073
1074/**
1075 * The Ring 0 entry point, called by the support library (SUP).
1076 *
1077 * @returns VBox status code.
1078 * @param pVM The VM to operate on.
1079 * @param idCpu Virtual CPU ID argument. Must be NIL_VMCPUID if pVM
1080 * is NIL_RTR0PTR, and may be NIL_VMCPUID if it isn't
1081 * @param enmOperation Which operation to execute.
1082 * @param pReq This points to a SUPVMMR0REQHDR packet. Optional.
1083 * @param u64Arg Some simple constant argument.
1084 * @param pSession The session of the caller.
1085 * @remarks Assume called with interrupts _enabled_.
1086 */
1087VMMR0DECL(int) VMMR0EntryEx(PVM pVM, VMCPUID idCpu, VMMR0OPERATION enmOperation, PSUPVMMR0REQHDR pReq, uint64_t u64Arg, PSUPDRVSESSION pSession)
1088{
1089 /*
1090 * Requests that should only happen on the EMT thread will be
1091 * wrapped in a setjmp so we can assert without causing trouble.
1092 */
1093 if ( VALID_PTR(pVM)
1094 && pVM->pVMR0
1095 && idCpu < pVM->cCPUs)
1096 {
1097 switch (enmOperation)
1098 {
1099 /* These might/will be called before VMMR3Init. */
1100 case VMMR0_DO_GMM_INITIAL_RESERVATION:
1101 case VMMR0_DO_GMM_UPDATE_RESERVATION:
1102 case VMMR0_DO_GMM_ALLOCATE_PAGES:
1103 case VMMR0_DO_GMM_FREE_PAGES:
1104 case VMMR0_DO_GMM_BALLOONED_PAGES:
1105 case VMMR0_DO_GMM_DEFLATED_BALLOON:
1106 /* On the mac we might not have a valid jmp buf, so check these as well. */
1107 case VMMR0_DO_VMMR0_INIT:
1108 case VMMR0_DO_VMMR0_TERM:
1109 {
1110 PVMCPU pVCpu = &pVM->aCpus[idCpu];
1111
1112 if (!pVCpu->vmm.s.CallRing3JmpBufR0.pvSavedStack)
1113 break;
1114
1115 /** @todo validate this EMT claim... GVM knows. */
1116 VMMR0ENTRYEXARGS Args;
1117 Args.pVM = pVM;
1118 Args.idCpu = idCpu;
1119 Args.enmOperation = enmOperation;
1120 Args.pReq = pReq;
1121 Args.u64Arg = u64Arg;
1122 Args.pSession = pSession;
1123 return vmmR0CallRing3SetJmpEx(&pVCpu->vmm.s.CallRing3JmpBufR0, vmmR0EntryExWrapper, &Args);
1124 }
1125
1126 default:
1127 break;
1128 }
1129 }
1130 return vmmR0EntryExWorker(pVM, idCpu, enmOperation, pReq, u64Arg, pSession);
1131}
1132
1133/**
1134 * Internal R0 logger worker: Flush logger.
1135 *
1136 * @param pLogger The logger instance to flush.
1137 * @remark This function must be exported!
1138 */
1139VMMR0DECL(void) vmmR0LoggerFlush(PRTLOGGER pLogger)
1140{
1141#ifdef LOG_ENABLED
1142 /*
1143 * Convert the pLogger into a VM handle and 'call' back to Ring-3.
1144 * (This is a bit paranoid code.)
1145 */
1146 PVMMR0LOGGER pR0Logger = (PVMMR0LOGGER)((uintptr_t)pLogger - RT_OFFSETOF(VMMR0LOGGER, Logger));
1147 if ( !VALID_PTR(pR0Logger)
1148 || !VALID_PTR(pR0Logger + 1)
1149 || pLogger->u32Magic != RTLOGGER_MAGIC)
1150 {
1151# ifdef DEBUG
1152 SUPR0Printf("vmmR0LoggerFlush: pLogger=%p!\n", pLogger);
1153# endif
1154 return;
1155 }
1156 if (pR0Logger->fFlushingDisabled)
1157 return; /* quietly */
1158
1159 PVM pVM = pR0Logger->pVM;
1160 if ( !VALID_PTR(pVM)
1161 || pVM->pVMR0 != pVM)
1162 {
1163# ifdef DEBUG
1164 SUPR0Printf("vmmR0LoggerFlush: pVM=%p! pVMR0=%p! pLogger=%p\n", pVM, pVM->pVMR0, pLogger);
1165# endif
1166 return;
1167 }
1168
1169 PVMCPU pVCpu = VMMGetCpu(pVM);
1170
1171 /*
1172 * Check that the jump buffer is armed.
1173 */
1174# ifdef RT_ARCH_X86
1175 if ( !pVCpu->vmm.s.CallRing3JmpBufR0.eip
1176 || pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call)
1177# else
1178 if ( !pVCpu->vmm.s.CallRing3JmpBufR0.rip
1179 || pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call)
1180# endif
1181 {
1182# ifdef DEBUG
1183 SUPR0Printf("vmmR0LoggerFlush: Jump buffer isn't armed!\n");
1184# endif
1185 return;
1186 }
1187 VMMRZCallRing3(pVM, pVCpu, VMMCALLRING3_VMM_LOGGER_FLUSH, 0);
1188#endif
1189}
1190
1191/**
1192 * Interal R0 logger worker: Custom prefix.
1193 *
1194 * @returns Number of chars written.
1195 *
1196 * @param pLogger The logger instance.
1197 * @param pchBuf The output buffer.
1198 * @param cchBuf The size of the buffer.
1199 * @param pvUser User argument (ignored).
1200 */
1201VMMR0DECL(size_t) vmmR0LoggerPrefix(PRTLOGGER pLogger, char *pchBuf, size_t cchBuf, void *pvUser)
1202{
1203 NOREF(pvUser);
1204#ifdef LOG_ENABLED
1205 PVMMR0LOGGER pR0Logger = (PVMMR0LOGGER)((uintptr_t)pLogger - RT_OFFSETOF(VMMR0LOGGER, Logger));
1206 if ( !VALID_PTR(pR0Logger)
1207 || !VALID_PTR(pR0Logger + 1)
1208 || pLogger->u32Magic != RTLOGGER_MAGIC
1209 || cchBuf < 2)
1210 return 0;
1211
1212 static const char s_szHex[17] = "0123456789abcdef";
1213 VMCPUID const idCpu = pR0Logger->idCpu;
1214 pchBuf[1] = s_szHex[ idCpu & 15];
1215 pchBuf[0] = s_szHex[(idCpu >> 4) & 15];
1216
1217 return 2;
1218#else
1219 return 0;
1220#endif
1221}
1222
1223
1224#ifdef LOG_ENABLED
1225/**
1226 * Disables flushing of the ring-0 debug log.
1227 *
1228 * @param pVCpu The shared virtual cpu structure.
1229 */
1230VMMR0DECL(void) VMMR0LogFlushDisable(PVMCPU pVCpu)
1231{
1232 PVM pVM = pVCpu->pVMR0;
1233 if (pVCpu->vmm.s.pR0LoggerR0)
1234 pVCpu->vmm.s.pR0LoggerR0->fFlushingDisabled = true;
1235}
1236
1237
1238/**
1239 * Enables flushing of the ring-0 debug log.
1240 *
1241 * @param pVCpu The shared virtual cpu structure.
1242 */
1243VMMR0DECL(void) VMMR0LogFlushEnable(PVMCPU pVCpu)
1244{
1245 PVM pVM = pVCpu->pVMR0;
1246 if (pVCpu->vmm.s.pR0LoggerR0)
1247 pVCpu->vmm.s.pR0LoggerR0->fFlushingDisabled = false;
1248}
1249#endif
1250
1251/**
1252 * Jump back to ring-3 if we're the EMT and the longjmp is armed.
1253 *
1254 * @returns true if the breakpoint should be hit, false if it should be ignored.
1255 */
1256DECLEXPORT(bool) RTCALL RTAssertShouldPanic(void)
1257{
1258#if 0
1259 return true;
1260#else
1261 PVM pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
1262 if (pVM)
1263 {
1264 PVMCPU pVCpu = VMMGetCpu(pVM);
1265
1266#ifdef RT_ARCH_X86
1267 if ( pVCpu->vmm.s.CallRing3JmpBufR0.eip
1268 && !pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call)
1269#else
1270 if ( pVCpu->vmm.s.CallRing3JmpBufR0.rip
1271 && !pVCpu->vmm.s.CallRing3JmpBufR0.fInRing3Call)
1272#endif
1273 {
1274 int rc = VMMRZCallRing3(pVM, pVCpu, VMMCALLRING3_VM_R0_ASSERTION, 0);
1275 return RT_FAILURE_NP(rc);
1276 }
1277 }
1278#ifdef RT_OS_LINUX
1279 return true;
1280#else
1281 return false;
1282#endif
1283#endif
1284}
1285
1286
1287/**
1288 * Override this so we can push it up to ring-3.
1289 *
1290 * @param pszExpr Expression. Can be NULL.
1291 * @param uLine Location line number.
1292 * @param pszFile Location file name.
1293 * @param pszFunction Location function name.
1294 */
1295DECLEXPORT(void) RTCALL AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
1296{
1297#if !defined(DEBUG_sandervl) && !defined(RT_OS_DARWIN)
1298 SUPR0Printf("\n!!R0-Assertion Failed!!\n"
1299 "Expression: %s\n"
1300 "Location : %s(%d) %s\n",
1301 pszExpr, pszFile, uLine, pszFunction);
1302#endif
1303 LogAlways(("\n!!R0-Assertion Failed!!\n"
1304 "Expression: %s\n"
1305 "Location : %s(%d) %s\n",
1306 pszExpr, pszFile, uLine, pszFunction));
1307
1308 PVM pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
1309 if (pVM)
1310 RTStrPrintf(pVM->vmm.s.szRing0AssertMsg1, sizeof(pVM->vmm.s.szRing0AssertMsg1),
1311 "\n!!R0-Assertion Failed!!\n"
1312 "Expression: %s\n"
1313 "Location : %s(%d) %s\n",
1314 pszExpr, pszFile, uLine, pszFunction);
1315#ifdef RT_OS_DARWIN
1316 RTAssertMsg1(pszExpr, uLine, pszFile, pszFunction);
1317#endif
1318}
1319
1320
1321/**
1322 * Callback for RTLogFormatV which writes to the ring-3 log port.
1323 * See PFNLOGOUTPUT() for details.
1324 */
1325static DECLCALLBACK(size_t) rtLogOutput(void *pv, const char *pachChars, size_t cbChars)
1326{
1327 for (size_t i = 0; i < cbChars; i++)
1328 {
1329#if !defined(DEBUG_sandervl) && !defined(RT_OS_DARWIN)
1330 SUPR0Printf("%c", pachChars[i]);
1331#endif
1332 LogAlways(("%c", pachChars[i]));
1333 }
1334
1335 return cbChars;
1336}
1337
1338
1339DECLEXPORT(void) RTCALL AssertMsg2(const char *pszFormat, ...)
1340{
1341 va_list va;
1342
1343 PRTLOGGER pLog = RTLogDefaultInstance(); /** @todo we want this for release as well! */
1344 if (pLog)
1345 {
1346 va_start(va, pszFormat);
1347 RTLogFormatV(rtLogOutput, pLog, pszFormat, va);
1348 va_end(va);
1349
1350 PVM pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
1351 if (pVM)
1352 {
1353 va_start(va, pszFormat);
1354 RTStrPrintfV(pVM->vmm.s.szRing0AssertMsg2, sizeof(pVM->vmm.s.szRing0AssertMsg2), pszFormat, va);
1355 va_end(va);
1356 }
1357 }
1358
1359#ifdef RT_OS_DARWIN
1360 va_start(va, pszFormat);
1361 RTAssertMsg2V(pszFormat, va);
1362 va_end(va);
1363#endif
1364}
1365
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