VirtualBox

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

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

Cpu poke never worked in the VT-x/AMD-V case, because pVCpu->idHostCpu was never set.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette