VirtualBox

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

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

VMM++: More on poking. Fixed broken R0 stats (wrong way of calling into VMMR0), use NIL_VMCPUID instead of 0 to VMMR0EntryEx when it is supposed to be irrellevant. Use VMCPUID. Allow for and check NIL_VMCPUID. Fixed a few missing/wrong idCpu checks (paranoia mostly).

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