VirtualBox

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

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

Guest SMP: force all VCPUs to go back to ring 3 when a pgm pool flush is pending. Not doing so might cause trouble on a loaded host.

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