VirtualBox

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

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

Introduced GVMMR0RegisterVCpu. (not used yet; untested)

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