VirtualBox

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

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

VMM: Use the custom logger prefix to indicate the virtual CPU ID.

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

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