VirtualBox

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

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

iprt/asm*.h: split out asm-math.h, don't include asm-*.h from asm.h, don't include asm.h from sup.h. Fixed a couple file headers.

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