VirtualBox

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

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

Misleading comment

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