VirtualBox

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

Last change on this file since 8736 was 8736, checked in by vboxsync, 17 years ago

Excessive debug logging can halt the host.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 36.8 KB
Line 
1/* $Id: VMMR0.cpp 8736 2008-05-09 10:09:31Z 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/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP LOG_GROUP_VMM
27#include <VBox/vmm.h>
28#include <VBox/sup.h>
29#include <VBox/trpm.h>
30#include <VBox/cpum.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
48#if defined(_MSC_VER) && defined(RT_ARCH_AMD64) /** @todo check this with with VC7! */
49# pragma intrinsic(_AddressOfReturnAddress)
50#endif
51
52
53/*******************************************************************************
54* Internal Functions *
55*******************************************************************************/
56static int VMMR0Init(PVM pVM, unsigned uVersion);
57static int VMMR0Term(PVM pVM);
58__BEGIN_DECLS
59VMMR0DECL(int) ModuleInit(void);
60VMMR0DECL(void) ModuleTerm(void);
61__END_DECLS
62
63
64/*******************************************************************************
65* Global Variables *
66*******************************************************************************/
67#ifdef VBOX_WITH_INTERNAL_NETWORKING
68/** Pointer to the internal networking service instance. */
69PINTNET g_pIntNet = 0;
70#endif
71
72
73/**
74 * Initialize the module.
75 * This is called when we're first loaded.
76 *
77 * @returns 0 on success.
78 * @returns VBox status on failure.
79 */
80VMMR0DECL(int) ModuleInit(void)
81{
82 LogFlow(("ModuleInit:\n"));
83
84 /*
85 * Initialize the GVMM, GMM.& HWACCM
86 */
87 int rc = GVMMR0Init();
88 if (RT_SUCCESS(rc))
89 {
90 rc = GMMR0Init();
91 if (RT_SUCCESS(rc))
92 {
93 rc = HWACCMR0Init();
94 if (RT_SUCCESS(rc))
95 {
96#ifdef VBOX_WITH_INTERNAL_NETWORKING
97 LogFlow(("ModuleInit: g_pIntNet=%p\n", g_pIntNet));
98 g_pIntNet = NULL;
99 LogFlow(("ModuleInit: g_pIntNet=%p should be NULL now...\n", g_pIntNet));
100 rc = INTNETR0Create(&g_pIntNet);
101 if (VBOX_SUCCESS(rc))
102 {
103 LogFlow(("ModuleInit: returns success. g_pIntNet=%p\n", g_pIntNet));
104 return VINF_SUCCESS;
105 }
106 g_pIntNet = NULL;
107 LogFlow(("ModuleTerm: returns %Vrc\n", rc));
108#else
109 LogFlow(("ModuleInit: returns success.\n"));
110 return VINF_SUCCESS;
111#endif
112 }
113 }
114 }
115
116 LogFlow(("ModuleInit: failed %Rrc\n", rc));
117 return rc;
118}
119
120
121/**
122 * Terminate the module.
123 * This is called when we're finally unloaded.
124 */
125VMMR0DECL(void) ModuleTerm(void)
126{
127 LogFlow(("ModuleTerm:\n"));
128
129#ifdef VBOX_WITH_INTERNAL_NETWORKING
130 /*
131 * Destroy the internal networking instance.
132 */
133 if (g_pIntNet)
134 {
135 INTNETR0Destroy(g_pIntNet);
136 g_pIntNet = NULL;
137 }
138#endif
139
140 /* Global HWACCM cleanup */
141 HWACCMR0Term();
142
143 /*
144 * Destroy the GMM and GVMM instances.
145 */
146 GMMR0Term();
147 GVMMR0Term();
148
149 LogFlow(("ModuleTerm: returns\n"));
150}
151
152
153/**
154 * Initaties the R0 driver for a particular VM instance.
155 *
156 * @returns VBox status code.
157 *
158 * @param pVM The VM instance in question.
159 * @param uVersion The minimum module version required.
160 * @thread EMT.
161 */
162static int VMMR0Init(PVM pVM, unsigned uVersion)
163{
164 /*
165 * Check if compatible version.
166 */
167 if ( uVersion != VBOX_VERSION
168 && ( VBOX_GET_VERSION_MAJOR(uVersion) != VBOX_VERSION_MAJOR
169 || VBOX_GET_VERSION_MINOR(uVersion) < VBOX_VERSION_MINOR))
170 return VERR_VERSION_MISMATCH;
171 if ( !VALID_PTR(pVM)
172 || pVM->pVMR0 != pVM)
173 return VERR_INVALID_PARAMETER;
174
175 /*
176 * Register the EMT R0 logger instance.
177 */
178 PVMMR0LOGGER pR0Logger = pVM->vmm.s.pR0Logger;
179 if (pR0Logger)
180 {
181#if 0 /* testing of the logger. */
182 LogCom(("VMMR0Init: before %p\n", RTLogDefaultInstance()));
183 LogCom(("VMMR0Init: pfnFlush=%p actual=%p\n", pR0Logger->Logger.pfnFlush, vmmR0LoggerFlush));
184 LogCom(("VMMR0Init: pfnLogger=%p actual=%p\n", pR0Logger->Logger.pfnLogger, vmmR0LoggerWrapper));
185 LogCom(("VMMR0Init: offScratch=%d fFlags=%#x fDestFlags=%#x\n", pR0Logger->Logger.offScratch, pR0Logger->Logger.fFlags, pR0Logger->Logger.fDestFlags));
186
187 RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
188 LogCom(("VMMR0Init: after %p reg\n", RTLogDefaultInstance()));
189 RTLogSetDefaultInstanceThread(NULL, 0);
190 LogCom(("VMMR0Init: after %p dereg\n", RTLogDefaultInstance()));
191
192 pR0Logger->Logger.pfnLogger("hello ring-0 logger\n");
193 LogCom(("VMMR0Init: returned succesfully from direct logger call.\n"));
194 pR0Logger->Logger.pfnFlush(&pR0Logger->Logger);
195 LogCom(("VMMR0Init: returned succesfully from direct flush call.\n"));
196
197 RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
198 LogCom(("VMMR0Init: after %p reg2\n", RTLogDefaultInstance()));
199 pR0Logger->Logger.pfnLogger("hello ring-0 logger\n");
200 LogCom(("VMMR0Init: returned succesfully from direct logger call (2). offScratch=%d\n", pR0Logger->Logger.offScratch));
201 RTLogSetDefaultInstanceThread(NULL, 0);
202 LogCom(("VMMR0Init: after %p dereg2\n", RTLogDefaultInstance()));
203
204 RTLogLoggerEx(&pR0Logger->Logger, 0, ~0U, "hello ring-0 logger (RTLogLoggerEx)\n");
205 LogCom(("VMMR0Init: RTLogLoggerEx returned fine offScratch=%d\n", pR0Logger->Logger.offScratch));
206
207 RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
208 RTLogPrintf("hello ring-0 logger (RTLogPrintf)\n");
209 LogCom(("VMMR0Init: RTLogPrintf returned fine offScratch=%d\n", pR0Logger->Logger.offScratch));
210#endif
211 Log(("Switching to per-thread logging instance %p (key=%p)\n", &pR0Logger->Logger, pVM->pSession));
212 RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
213 }
214
215 /*
216 * Initialize the per VM data for GVMM and GMM.
217 */
218 int rc = GVMMR0InitVM(pVM);
219// if (RT_SUCCESS(rc))
220// rc = GMMR0InitPerVMData(pVM);
221 if (RT_SUCCESS(rc))
222 {
223 /*
224 * Init HWACCM.
225 */
226 rc = HWACCMR0InitVM(pVM);
227 if (RT_SUCCESS(rc))
228 {
229 /*
230 * Init CPUM.
231 */
232 rc = CPUMR0Init(pVM);
233 if (RT_SUCCESS(rc))
234 return rc;
235 }
236 }
237
238 /* failed */
239 RTLogSetDefaultInstanceThread(NULL, 0);
240 return rc;
241}
242
243
244/**
245 * Terminates the R0 driver for a particular VM instance.
246 *
247 * @returns VBox status code.
248 *
249 * @param pVM The VM instance in question.
250 * @thread EMT.
251 */
252static int VMMR0Term(PVM pVM)
253{
254 HWACCMR0TermVM(pVM);
255
256 /*
257 * Deregister the logger.
258 */
259 RTLogSetDefaultInstanceThread(NULL, 0);
260 return VINF_SUCCESS;
261}
262
263
264/**
265 * Calls the ring-3 host code.
266 *
267 * @returns VBox status code of the ring-3 call.
268 * @param pVM The VM handle.
269 * @param enmOperation The operation.
270 * @param uArg The argument to the operation.
271 */
272VMMR0DECL(int) VMMR0CallHost(PVM pVM, VMMCALLHOST enmOperation, uint64_t uArg)
273{
274/** @todo profile this! */
275 pVM->vmm.s.enmCallHostOperation = enmOperation;
276 pVM->vmm.s.u64CallHostArg = uArg;
277 pVM->vmm.s.rcCallHost = VERR_INTERNAL_ERROR;
278 int rc = vmmR0CallHostLongJmp(&pVM->vmm.s.CallHostR0JmpBuf, VINF_VMM_CALL_HOST);
279 if (rc == VINF_SUCCESS)
280 rc = pVM->vmm.s.rcCallHost;
281 return rc;
282}
283
284
285#ifdef VBOX_WITH_STATISTICS
286/**
287 * Record return code statistics
288 * @param pVM The VM handle.
289 * @param rc The status code.
290 */
291static void vmmR0RecordRC(PVM pVM, int rc)
292{
293 /*
294 * Collect statistics.
295 */
296 switch (rc)
297 {
298 case VINF_SUCCESS:
299 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetNormal);
300 break;
301 case VINF_EM_RAW_INTERRUPT:
302 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetInterrupt);
303 break;
304 case VINF_EM_RAW_INTERRUPT_HYPER:
305 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetInterruptHyper);
306 break;
307 case VINF_EM_RAW_GUEST_TRAP:
308 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetGuestTrap);
309 break;
310 case VINF_EM_RAW_RING_SWITCH:
311 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetRingSwitch);
312 break;
313 case VINF_EM_RAW_RING_SWITCH_INT:
314 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetRingSwitchInt);
315 break;
316 case VINF_EM_RAW_EXCEPTION_PRIVILEGED:
317 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetExceptionPrivilege);
318 break;
319 case VINF_EM_RAW_STALE_SELECTOR:
320 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetStaleSelector);
321 break;
322 case VINF_EM_RAW_IRET_TRAP:
323 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetIRETTrap);
324 break;
325 case VINF_IOM_HC_IOPORT_READ:
326 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetIORead);
327 break;
328 case VINF_IOM_HC_IOPORT_WRITE:
329 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetIOWrite);
330 break;
331 case VINF_IOM_HC_MMIO_READ:
332 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetMMIORead);
333 break;
334 case VINF_IOM_HC_MMIO_WRITE:
335 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetMMIOWrite);
336 break;
337 case VINF_IOM_HC_MMIO_READ_WRITE:
338 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetMMIOReadWrite);
339 break;
340 case VINF_PATM_HC_MMIO_PATCH_READ:
341 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetMMIOPatchRead);
342 break;
343 case VINF_PATM_HC_MMIO_PATCH_WRITE:
344 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetMMIOPatchWrite);
345 break;
346 case VINF_EM_RAW_EMULATE_INSTR:
347 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetEmulate);
348 break;
349 case VINF_PATCH_EMULATE_INSTR:
350 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPatchEmulate);
351 break;
352 case VINF_EM_RAW_EMULATE_INSTR_LDT_FAULT:
353 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetLDTFault);
354 break;
355 case VINF_EM_RAW_EMULATE_INSTR_GDT_FAULT:
356 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetGDTFault);
357 break;
358 case VINF_EM_RAW_EMULATE_INSTR_IDT_FAULT:
359 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetIDTFault);
360 break;
361 case VINF_EM_RAW_EMULATE_INSTR_TSS_FAULT:
362 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetTSSFault);
363 break;
364 case VINF_EM_RAW_EMULATE_INSTR_PD_FAULT:
365 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPDFault);
366 break;
367 case VINF_CSAM_PENDING_ACTION:
368 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetCSAMTask);
369 break;
370 case VINF_PGM_SYNC_CR3:
371 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetSyncCR3);
372 break;
373 case VINF_PATM_PATCH_INT3:
374 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPatchInt3);
375 break;
376 case VINF_PATM_PATCH_TRAP_PF:
377 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPatchPF);
378 break;
379 case VINF_PATM_PATCH_TRAP_GP:
380 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPatchGP);
381 break;
382 case VINF_PATM_PENDING_IRQ_AFTER_IRET:
383 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPatchIretIRQ);
384 break;
385 case VERR_REM_FLUSHED_PAGES_OVERFLOW:
386 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPageOverflow);
387 break;
388 case VINF_EM_RESCHEDULE_REM:
389 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetRescheduleREM);
390 break;
391 case VINF_EM_RAW_TO_R3:
392 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetToR3);
393 break;
394 case VINF_EM_RAW_TIMER_PENDING:
395 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetTimerPending);
396 break;
397 case VINF_EM_RAW_INTERRUPT_PENDING:
398 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetInterruptPending);
399 break;
400 case VINF_VMM_CALL_HOST:
401 switch (pVM->vmm.s.enmCallHostOperation)
402 {
403 case VMMCALLHOST_PDM_LOCK:
404 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPDMLock);
405 break;
406 case VMMCALLHOST_PDM_QUEUE_FLUSH:
407 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPDMQueueFlush);
408 break;
409 case VMMCALLHOST_PGM_POOL_GROW:
410 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPGMPoolGrow);
411 break;
412 case VMMCALLHOST_PGM_LOCK:
413 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPGMLock);
414 break;
415 case VMMCALLHOST_REM_REPLAY_HANDLER_NOTIFICATIONS:
416 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetRemReplay);
417 break;
418 case VMMCALLHOST_PGM_RAM_GROW_RANGE:
419 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPGMGrowRAM);
420 break;
421 case VMMCALLHOST_VMM_LOGGER_FLUSH:
422 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetLogFlush);
423 break;
424 case VMMCALLHOST_VM_SET_ERROR:
425 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetVMSetError);
426 break;
427 case VMMCALLHOST_VM_SET_RUNTIME_ERROR:
428 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetVMSetRuntimeError);
429 break;
430 default:
431 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetCallHost);
432 break;
433 }
434 break;
435 case VINF_PATM_DUPLICATE_FUNCTION:
436 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPATMDuplicateFn);
437 break;
438 case VINF_PGM_CHANGE_MODE:
439 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPGMChangeMode);
440 break;
441 case VINF_EM_RAW_EMULATE_INSTR_HLT:
442 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetEmulHlt);
443 break;
444 case VINF_EM_PENDING_REQUEST:
445 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPendingRequest);
446 break;
447 default:
448 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetMisc);
449 break;
450 }
451}
452#endif /* VBOX_WITH_STATISTICS */
453
454
455
456/**
457 * The Ring 0 entry point, called by the interrupt gate.
458 *
459 * @returns VBox status code.
460 * @param pVM The VM to operate on.
461 * @param enmOperation Which operation to execute.
462 * @param pvArg Argument to the operation.
463 * @remarks Assume called with interrupts disabled.
464 */
465VMMR0DECL(int) VMMR0EntryInt(PVM pVM, VMMR0OPERATION enmOperation, void *pvArg)
466{
467 switch (enmOperation)
468 {
469#ifdef VBOX_WITH_IDT_PATCHING
470 /*
471 * Switch to GC.
472 * These calls return whatever the GC returns.
473 */
474 case VMMR0_DO_RAW_RUN:
475 {
476 /* Safety precaution as VMX disables the switcher. */
477 Assert(!pVM->vmm.s.fSwitcherDisabled);
478 if (pVM->vmm.s.fSwitcherDisabled)
479 return VERR_NOT_SUPPORTED;
480
481 STAM_COUNTER_INC(&pVM->vmm.s.StatRunGC);
482 register int rc;
483 pVM->vmm.s.iLastGCRc = rc = pVM->vmm.s.pfnR0HostToGuest(pVM);
484
485#ifdef VBOX_WITH_STATISTICS
486 vmmR0RecordRC(pVM, rc);
487#endif
488
489 /*
490 * We'll let TRPM change the stack frame so our return is different.
491 * Just keep in mind that after the call, things have changed!
492 */
493 if ( rc == VINF_EM_RAW_INTERRUPT
494 || rc == VINF_EM_RAW_INTERRUPT_HYPER)
495 {
496 /*
497 * Don't trust the compiler to get this right.
498 * gcc -fomit-frame-pointer screws up big time here. This works fine in 64-bit
499 * mode too because we push the arguments on the stack in the IDT patch code.
500 */
501# if defined(__GNUC__)
502 void *pvRet = (uint8_t *)__builtin_frame_address(0) + sizeof(void *);
503# elif defined(_MSC_VER) && defined(RT_ARCH_AMD64) /** @todo check this with with VC7! */
504 void *pvRet = (uint8_t *)_AddressOfReturnAddress();
505# elif defined(RT_ARCH_X86)
506 void *pvRet = (uint8_t *)&pVM - sizeof(pVM);
507# else
508# error "huh?"
509# endif
510 if ( ((uintptr_t *)pvRet)[1] == (uintptr_t)pVM
511 && ((uintptr_t *)pvRet)[2] == (uintptr_t)enmOperation
512 && ((uintptr_t *)pvRet)[3] == (uintptr_t)pvArg)
513 TRPMR0SetupInterruptDispatcherFrame(pVM, pvRet);
514 else
515 {
516# if defined(DEBUG) || defined(LOG_ENABLED)
517 static bool s_fHaveWarned = false;
518 if (!s_fHaveWarned)
519 {
520 s_fHaveWarned = true;
521 RTLogPrintf("VMMR0.r0: The compiler can't find the stack frame!\n");
522 RTLogComPrintf("VMMR0.r0: The compiler can't find the stack frame!\n");
523 }
524# endif
525 TRPMR0DispatchHostInterrupt(pVM);
526 }
527 }
528 return rc;
529 }
530
531 /*
532 * Switch to GC to execute Hypervisor function.
533 */
534 case VMMR0_DO_CALL_HYPERVISOR:
535 {
536 /* Safety precaution as VMX disables the switcher. */
537 Assert(!pVM->vmm.s.fSwitcherDisabled);
538 if (pVM->vmm.s.fSwitcherDisabled)
539 return VERR_NOT_SUPPORTED;
540
541 RTCCUINTREG fFlags = ASMIntDisableFlags();
542 int rc = pVM->vmm.s.pfnR0HostToGuest(pVM);
543 /** @todo dispatch interrupts? */
544 ASMSetFlags(fFlags);
545 return rc;
546 }
547
548 /*
549 * For profiling.
550 */
551 case VMMR0_DO_NOP:
552 return VINF_SUCCESS;
553#endif /* VBOX_WITH_IDT_PATCHING */
554
555 default:
556 /*
557 * We're returning VERR_NOT_SUPPORT here so we've got something else
558 * than -1 which the interrupt gate glue code might return.
559 */
560 Log(("operation %#x is not supported\n", enmOperation));
561 return VERR_NOT_SUPPORTED;
562 }
563}
564
565
566/**
567 * The Ring 0 entry point, called by the fast-ioctl path.
568 *
569 * @returns VBox status code.
570 * @param pVM The VM to operate on.
571 * @param enmOperation Which operation to execute.
572 * @remarks Assume called with interrupts _enabled_.
573 */
574VMMR0DECL(int) VMMR0EntryFast(PVM pVM, VMMR0OPERATION enmOperation)
575{
576 switch (enmOperation)
577 {
578 /*
579 * Switch to GC and run guest raw mode code.
580 * Disable interrupts before doing the world switch.
581 */
582 case VMMR0_DO_RAW_RUN:
583 {
584 /* Safety precaution as hwaccm disables the switcher. */
585 if (RT_LIKELY(!pVM->vmm.s.fSwitcherDisabled))
586 {
587 RTCCUINTREG uFlags = ASMIntDisableFlags();
588
589 int rc = pVM->vmm.s.pfnR0HostToGuest(pVM);
590 pVM->vmm.s.iLastGCRc = rc;
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.StatRunGC);
600 vmmR0RecordRC(pVM, rc);
601#endif
602 return rc;
603 }
604
605 Assert(!pVM->vmm.s.fSwitcherDisabled);
606 return VERR_NOT_SUPPORTED;
607 }
608
609 /*
610 * Run guest code using the available hardware acceleration technology.
611 *
612 * Disable interrupts before we do anything interesting. On Windows we avoid
613 * this by having the support driver raise the IRQL before calling us, this way
614 * we hope to get away we page faults and later calling into the kernel.
615 */
616 case VMMR0_DO_HWACC_RUN:
617 {
618 STAM_COUNTER_INC(&pVM->vmm.s.StatRunGC);
619
620#ifndef RT_OS_WINDOWS /** @todo check other hosts */
621 RTCCUINTREG uFlags = ASMIntDisableFlags();
622#endif
623 int rc = HWACCMR0Enter(pVM);
624 if (VBOX_SUCCESS(rc))
625 {
626 rc = vmmR0CallHostSetJmp(&pVM->vmm.s.CallHostR0JmpBuf, HWACCMR0RunGuestCode, pVM); /* this may resume code. */
627 int rc2 = HWACCMR0Leave(pVM);
628 AssertRC(rc2);
629 }
630 pVM->vmm.s.iLastGCRc = rc;
631#ifndef RT_OS_WINDOWS /** @todo check other hosts */
632 ASMSetFlags(uFlags);
633#endif
634
635#ifdef VBOX_WITH_STATISTICS
636 vmmR0RecordRC(pVM, rc);
637#endif
638 /* No special action required for external interrupts, just return. */
639 return rc;
640 }
641
642 /*
643 * For profiling.
644 */
645 case VMMR0_DO_NOP:
646 return VINF_SUCCESS;
647
648 /*
649 * Impossible.
650 */
651 default:
652 AssertMsgFailed(("%#x\n", enmOperation));
653 return VERR_NOT_SUPPORTED;
654 }
655}
656
657
658/**
659 * VMMR0EntryEx worker function, either called directly or when ever possible
660 * called thru a longjmp so we can exit safely on failure.
661 *
662 * @returns VBox status code.
663 * @param pVM The VM to operate on.
664 * @param enmOperation Which operation to execute.
665 * @param pReqHdr This points to a SUPVMMR0REQHDR packet. Optional.
666 * @param u64Arg Some simple constant argument.
667 * @remarks Assume called with interrupts _enabled_.
668 */
669static int vmmR0EntryExWorker(PVM pVM, VMMR0OPERATION enmOperation, PSUPVMMR0REQHDR pReqHdr, uint64_t u64Arg)
670{
671 /*
672 * Common VM pointer validation.
673 */
674 if (pVM)
675 {
676 if (RT_UNLIKELY( !VALID_PTR(pVM)
677 || ((uintptr_t)pVM & PAGE_OFFSET_MASK)))
678 {
679 SUPR0Printf("vmmR0EntryExWorker: Invalid pVM=%p! (op=%d)\n", pVM, enmOperation);
680 return VERR_INVALID_POINTER;
681 }
682 if (RT_UNLIKELY( pVM->enmVMState < VMSTATE_CREATING
683 || pVM->enmVMState > VMSTATE_TERMINATED
684 || pVM->pVMR0 != pVM))
685 {
686 SUPR0Printf("vmmR0EntryExWorker: Invalid pVM=%p:{enmVMState=%d, .pVMR0=%p}! (op=%d)\n",
687 pVM, pVM->enmVMState, pVM->pVMR0, enmOperation);
688 return VERR_INVALID_POINTER;
689 }
690 }
691
692 switch (enmOperation)
693 {
694 /*
695 * GVM requests
696 */
697 case VMMR0_DO_GVMM_CREATE_VM:
698 if (pVM || u64Arg)
699 return VERR_INVALID_PARAMETER;
700 return GVMMR0CreateVMReq((PGVMMCREATEVMREQ)pReqHdr);
701
702 case VMMR0_DO_GVMM_DESTROY_VM:
703 if (pReqHdr || u64Arg)
704 return VERR_INVALID_PARAMETER;
705 return GVMMR0DestroyVM(pVM);
706
707 case VMMR0_DO_GVMM_SCHED_HALT:
708 if (pReqHdr)
709 return VERR_INVALID_PARAMETER;
710 return GVMMR0SchedHalt(pVM, u64Arg);
711
712 case VMMR0_DO_GVMM_SCHED_WAKE_UP:
713 if (pReqHdr || u64Arg)
714 return VERR_INVALID_PARAMETER;
715 return GVMMR0SchedWakeUp(pVM);
716
717 case VMMR0_DO_GVMM_SCHED_POLL:
718 if (pReqHdr || u64Arg > 1)
719 return VERR_INVALID_PARAMETER;
720 return GVMMR0SchedPoll(pVM, (bool)u64Arg);
721
722 case VMMR0_DO_GVMM_QUERY_STATISTICS:
723 if (u64Arg)
724 return VERR_INVALID_PARAMETER;
725 return GVMMR0QueryStatisticsReq(pVM, (PGVMMQUERYSTATISTICSSREQ)pReqHdr);
726
727 case VMMR0_DO_GVMM_RESET_STATISTICS:
728 if (u64Arg)
729 return VERR_INVALID_PARAMETER;
730 return GVMMR0ResetStatisticsReq(pVM, (PGVMMRESETSTATISTICSSREQ)pReqHdr);
731
732 /*
733 * Initialize the R0 part of a VM instance.
734 */
735 case VMMR0_DO_VMMR0_INIT:
736 return VMMR0Init(pVM, (unsigned)u64Arg);
737
738 /*
739 * Terminate the R0 part of a VM instance.
740 */
741 case VMMR0_DO_VMMR0_TERM:
742 return VMMR0Term(pVM);
743
744 /*
745 * Attempt to enable hwacc mode and check the current setting.
746 *
747 */
748 case VMMR0_DO_HWACC_ENABLE:
749 return HWACCMR0EnableAllCpus(pVM, (HWACCMSTATE)u64Arg);
750
751 /*
752 * Setup the hardware accelerated raw-mode session.
753 */
754 case VMMR0_DO_HWACC_SETUP_VM:
755 {
756 RTCCUINTREG fFlags = ASMIntDisableFlags();
757 int rc = HWACCMR0SetupVM(pVM);
758 ASMSetFlags(fFlags);
759 return rc;
760 }
761
762 /*
763 * Switch to GC to execute Hypervisor function.
764 */
765 case VMMR0_DO_CALL_HYPERVISOR:
766 {
767 /* Safety precaution as HWACCM can disable the switcher. */
768 Assert(!pVM->vmm.s.fSwitcherDisabled);
769 if (RT_UNLIKELY(pVM->vmm.s.fSwitcherDisabled))
770 return VERR_NOT_SUPPORTED;
771
772 RTCCUINTREG fFlags = ASMIntDisableFlags();
773 int rc = pVM->vmm.s.pfnR0HostToGuest(pVM);
774 /** @todo dispatch interrupts? */
775 ASMSetFlags(fFlags);
776 return rc;
777 }
778
779 /*
780 * PGM wrappers.
781 */
782 case VMMR0_DO_PGM_ALLOCATE_HANDY_PAGES:
783 return PGMR0PhysAllocateHandyPages(pVM);
784
785 /*
786 * GMM wrappers.
787 */
788 case VMMR0_DO_GMM_INITIAL_RESERVATION:
789 if (u64Arg)
790 return VERR_INVALID_PARAMETER;
791 return GMMR0InitialReservationReq(pVM, (PGMMINITIALRESERVATIONREQ)pReqHdr);
792 case VMMR0_DO_GMM_UPDATE_RESERVATION:
793 if (u64Arg)
794 return VERR_INVALID_PARAMETER;
795 return GMMR0UpdateReservationReq(pVM, (PGMMUPDATERESERVATIONREQ)pReqHdr);
796
797 case VMMR0_DO_GMM_ALLOCATE_PAGES:
798 if (u64Arg)
799 return VERR_INVALID_PARAMETER;
800 return GMMR0AllocatePagesReq(pVM, (PGMMALLOCATEPAGESREQ)pReqHdr);
801 case VMMR0_DO_GMM_FREE_PAGES:
802 if (u64Arg)
803 return VERR_INVALID_PARAMETER;
804 return GMMR0FreePagesReq(pVM, (PGMMFREEPAGESREQ)pReqHdr);
805 case VMMR0_DO_GMM_BALLOONED_PAGES:
806 if (u64Arg)
807 return VERR_INVALID_PARAMETER;
808 return GMMR0BalloonedPagesReq(pVM, (PGMMBALLOONEDPAGESREQ)pReqHdr);
809 case VMMR0_DO_GMM_DEFLATED_BALLOON:
810 if (pReqHdr)
811 return VERR_INVALID_PARAMETER;
812 return GMMR0DeflatedBalloon(pVM, (uint32_t)u64Arg);
813
814 case VMMR0_DO_GMM_MAP_UNMAP_CHUNK:
815 if (u64Arg)
816 return VERR_INVALID_PARAMETER;
817 return GMMR0MapUnmapChunkReq(pVM, (PGMMMAPUNMAPCHUNKREQ)pReqHdr);
818 case VMMR0_DO_GMM_SEED_CHUNK:
819 if (pReqHdr)
820 return VERR_INVALID_PARAMETER;
821 return GMMR0SeedChunk(pVM, (RTR3PTR)u64Arg);
822
823 /*
824 * A quick GCFGM mock-up.
825 */
826 /** @todo GCFGM with proper access control, ring-3 management interface and all that. */
827 case VMMR0_DO_GCFGM_SET_VALUE:
828 case VMMR0_DO_GCFGM_QUERY_VALUE:
829 {
830 if (pVM || !pReqHdr || u64Arg)
831 return VERR_INVALID_PARAMETER;
832 PGCFGMVALUEREQ pReq = (PGCFGMVALUEREQ)pReqHdr;
833 if (pReq->Hdr.cbReq != sizeof(*pReq))
834 return VERR_INVALID_PARAMETER;
835 int rc;
836 if (enmOperation == VMMR0_DO_GCFGM_SET_VALUE)
837 {
838 rc = GVMMR0SetConfig(pReq->pSession, &pReq->szName[0], pReq->u64Value);
839 //if (rc == VERR_CFGM_VALUE_NOT_FOUND)
840 // rc = GMMR0SetConfig(pReq->pSession, &pReq->szName[0], pReq->u64Value);
841 }
842 else
843 {
844 rc = GVMMR0QueryConfig(pReq->pSession, &pReq->szName[0], &pReq->u64Value);
845 //if (rc == VERR_CFGM_VALUE_NOT_FOUND)
846 // rc = GMMR0QueryConfig(pReq->pSession, &pReq->szName[0], &pReq->u64Value);
847 }
848 return rc;
849 }
850
851
852#ifdef VBOX_WITH_INTERNAL_NETWORKING
853 /*
854 * Requests to the internal networking service.
855 */
856 case VMMR0_DO_INTNET_OPEN:
857 if (!pVM || u64Arg)
858 return VERR_INVALID_PARAMETER;
859 if (!g_pIntNet)
860 return VERR_NOT_SUPPORTED;
861 return INTNETR0OpenReq(g_pIntNet, pVM->pSession, (PINTNETOPENREQ)pReqHdr);
862
863 case VMMR0_DO_INTNET_IF_CLOSE:
864 if (!pVM || u64Arg)
865 return VERR_INVALID_PARAMETER;
866 if (!g_pIntNet)
867 return VERR_NOT_SUPPORTED;
868 return INTNETR0IfCloseReq(g_pIntNet, (PINTNETIFCLOSEREQ)pReqHdr);
869
870 case VMMR0_DO_INTNET_IF_GET_RING3_BUFFER:
871 if (!pVM || u64Arg)
872 return VERR_INVALID_PARAMETER;
873 if (!g_pIntNet)
874 return VERR_NOT_SUPPORTED;
875 return INTNETR0IfGetRing3BufferReq(g_pIntNet, (PINTNETIFGETRING3BUFFERREQ)pReqHdr);
876
877 case VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE:
878 if (!pVM || u64Arg)
879 return VERR_INVALID_PARAMETER;
880 if (!g_pIntNet)
881 return VERR_NOT_SUPPORTED;
882 return INTNETR0IfSetPromiscuousModeReq(g_pIntNet, (PINTNETIFSETPROMISCUOUSMODEREQ)pReqHdr);
883
884 case VMMR0_DO_INTNET_IF_SEND:
885 if (!pVM || u64Arg)
886 return VERR_INVALID_PARAMETER;
887 if (!g_pIntNet)
888 return VERR_NOT_SUPPORTED;
889 return INTNETR0IfSendReq(g_pIntNet, (PINTNETIFSENDREQ)pReqHdr);
890
891 case VMMR0_DO_INTNET_IF_WAIT:
892 if (!pVM || u64Arg)
893 return VERR_INVALID_PARAMETER;
894 if (!g_pIntNet)
895 return VERR_NOT_SUPPORTED;
896 return INTNETR0IfWaitReq(g_pIntNet, (PINTNETIFWAITREQ)pReqHdr);
897#endif /* VBOX_WITH_INTERNAL_NETWORKING */
898
899 /*
900 * For profiling.
901 */
902 case VMMR0_DO_NOP:
903 return VINF_SUCCESS;
904
905 /*
906 * For testing Ring-0 APIs invoked in this environment.
907 */
908 case VMMR0_DO_TESTS:
909 /** @todo make new test */
910 return VINF_SUCCESS;
911
912
913 default:
914 /*
915 * We're returning VERR_NOT_SUPPORT here so we've got something else
916 * than -1 which the interrupt gate glue code might return.
917 */
918 Log(("operation %#x is not supported\n", enmOperation));
919 return VERR_NOT_SUPPORTED;
920 }
921}
922
923
924/**
925 * Argument for vmmR0EntryExWrapper containing the argument s ofr VMMR0EntryEx.
926 */
927typedef struct VMMR0ENTRYEXARGS
928{
929 PVM pVM;
930 VMMR0OPERATION enmOperation;
931 PSUPVMMR0REQHDR pReq;
932 uint64_t u64Arg;
933} VMMR0ENTRYEXARGS;
934/** Pointer to a vmmR0EntryExWrapper argument package. */
935typedef VMMR0ENTRYEXARGS *PVMMR0ENTRYEXARGS;
936
937/**
938 * This is just a longjmp wrapper function for VMMR0EntryEx calls.
939 *
940 * @returns VBox status code.
941 * @param pvArgs The argument package
942 */
943static int vmmR0EntryExWrapper(void *pvArgs)
944{
945 return vmmR0EntryExWorker(((PVMMR0ENTRYEXARGS)pvArgs)->pVM,
946 ((PVMMR0ENTRYEXARGS)pvArgs)->enmOperation,
947 ((PVMMR0ENTRYEXARGS)pvArgs)->pReq,
948 ((PVMMR0ENTRYEXARGS)pvArgs)->u64Arg);
949}
950
951
952/**
953 * The Ring 0 entry point, called by the support library (SUP).
954 *
955 * @returns VBox status code.
956 * @param pVM The VM to operate on.
957 * @param enmOperation Which operation to execute.
958 * @param pReq This points to a SUPVMMR0REQHDR packet. Optional.
959 * @param u64Arg Some simple constant argument.
960 * @remarks Assume called with interrupts _enabled_.
961 */
962VMMR0DECL(int) VMMR0EntryEx(PVM pVM, VMMR0OPERATION enmOperation, PSUPVMMR0REQHDR pReq, uint64_t u64Arg)
963{
964 /*
965 * Requests that should only happen on the EMT thread will be
966 * wrapped in a setjmp so we can assert without causing trouble.
967 */
968 if ( VALID_PTR(pVM)
969 && pVM->pVMR0)
970 {
971 switch (enmOperation)
972 {
973 case VMMR0_DO_VMMR0_INIT:
974 case VMMR0_DO_VMMR0_TERM:
975 case VMMR0_DO_GMM_INITIAL_RESERVATION:
976 case VMMR0_DO_GMM_UPDATE_RESERVATION:
977 case VMMR0_DO_GMM_ALLOCATE_PAGES:
978 case VMMR0_DO_GMM_FREE_PAGES:
979 case VMMR0_DO_GMM_BALLOONED_PAGES:
980 case VMMR0_DO_GMM_DEFLATED_BALLOON:
981 case VMMR0_DO_GMM_MAP_UNMAP_CHUNK:
982 case VMMR0_DO_GMM_SEED_CHUNK:
983 {
984 /** @todo validate this EMT claim... GVM knows. */
985 VMMR0ENTRYEXARGS Args;
986 Args.pVM = pVM;
987 Args.enmOperation = enmOperation;
988 Args.pReq = pReq;
989 Args.u64Arg = u64Arg;
990 return vmmR0CallHostSetJmpEx(&pVM->vmm.s.CallHostR0JmpBuf, vmmR0EntryExWrapper, &Args);
991 }
992
993 default:
994 break;
995 }
996 }
997 return vmmR0EntryExWorker(pVM, enmOperation, pReq, u64Arg);
998}
999
1000
1001
1002/**
1003 * Internal R0 logger worker: Flush logger.
1004 *
1005 * @param pLogger The logger instance to flush.
1006 * @remark This function must be exported!
1007 */
1008VMMR0DECL(void) vmmR0LoggerFlush(PRTLOGGER pLogger)
1009{
1010 /*
1011 * Convert the pLogger into a VM handle and 'call' back to Ring-3.
1012 * (This is a bit paranoid code.)
1013 */
1014 PVMMR0LOGGER pR0Logger = (PVMMR0LOGGER)((uintptr_t)pLogger - RT_OFFSETOF(VMMR0LOGGER, Logger));
1015 if ( !VALID_PTR(pR0Logger)
1016 || !VALID_PTR(pR0Logger + 1)
1017 || !VALID_PTR(pLogger)
1018 || pLogger->u32Magic != RTLOGGER_MAGIC)
1019 {
1020#ifdef DEBUG
1021 SUPR0Printf("vmmR0LoggerFlush: pLogger=%p!\n", pLogger);
1022#endif
1023 return;
1024 }
1025
1026 PVM pVM = pR0Logger->pVM;
1027 if ( !VALID_PTR(pVM)
1028 || pVM->pVMR0 != pVM)
1029 {
1030#ifdef DEBUG
1031 SUPR0Printf("vmmR0LoggerFlush: pVM=%p! pVMR0=%p! pLogger=%p\n", pVM, pVM->pVMR0, pLogger);
1032#endif
1033 return;
1034 }
1035
1036 /*
1037 * Check that the jump buffer is armed.
1038 */
1039#ifdef RT_ARCH_X86
1040 if (!pVM->vmm.s.CallHostR0JmpBuf.eip)
1041#else
1042 if (!pVM->vmm.s.CallHostR0JmpBuf.rip)
1043#endif
1044 {
1045#ifdef DEBUG
1046 SUPR0Printf("vmmR0LoggerFlush: Jump buffer isn't armed!\n");
1047#endif
1048 pLogger->offScratch = 0;
1049 return;
1050 }
1051 VMMR0CallHost(pVM, VMMCALLHOST_VMM_LOGGER_FLUSH, 0);
1052}
1053
1054
1055
1056/**
1057 * Jump back to ring-3 if we're the EMT and the longjmp is armed.
1058 *
1059 * @returns true if the breakpoint should be hit, false if it should be ignored.
1060 * @remark The RTDECL() makes this a bit difficult to override on windows. Sorry.
1061 */
1062DECLEXPORT(bool) RTCALL RTAssertDoBreakpoint(void)
1063{
1064 PVM pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
1065 if (pVM)
1066 {
1067#ifdef RT_ARCH_X86
1068 if (pVM->vmm.s.CallHostR0JmpBuf.eip)
1069#else
1070 if (pVM->vmm.s.CallHostR0JmpBuf.rip)
1071#endif
1072 {
1073 int rc = VMMR0CallHost(pVM, VMMCALLHOST_VM_R0_HYPER_ASSERTION, 0);
1074 return RT_FAILURE_NP(rc);
1075 }
1076 }
1077#ifdef RT_OS_LINUX
1078 return true;
1079#else
1080 return false;
1081#endif
1082}
1083
1084
1085
1086# undef LOG_GROUP
1087# define LOG_GROUP LOG_GROUP_EM
1088
1089/**
1090 * Override this so we can push
1091 *
1092 * @param pszExpr Expression. Can be NULL.
1093 * @param uLine Location line number.
1094 * @param pszFile Location file name.
1095 * @param pszFunction Location function name.
1096 * @remark This API exists in HC Ring-3 and GC.
1097 */
1098DECLEXPORT(void) RTCALL AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
1099{
1100#ifndef DEBUG_sandervl
1101 SUPR0Printf("\n!!R0-Assertion Failed!!\n"
1102 "Expression: %s\n"
1103 "Location : %s(%d) %s\n",
1104 pszExpr, pszFile, uLine, pszFunction);
1105#endif
1106 LogRel(("\n!!R0-Assertion Failed!!\n"
1107 "Expression: %s\n"
1108 "Location : %s(%d) %s\n",
1109 pszExpr, pszFile, uLine, pszFunction));
1110}
1111
1112
1113/**
1114 * Callback for RTLogFormatV which writes to the com port.
1115 * See PFNLOGOUTPUT() for details.
1116 */
1117static DECLCALLBACK(size_t) rtLogOutput(void *pv, const char *pachChars, size_t cbChars)
1118{
1119 for (size_t i = 0; i < cbChars; i++)
1120 {
1121 LogRel(("%c", pachChars[i])); /** @todo this isn't any release logging in ring-0 from what I can tell... */
1122#ifndef DEBUG_sandervl
1123 SUPR0Printf("%c", pachChars[i]);
1124#endif
1125 }
1126
1127 return cbChars;
1128}
1129
1130
1131DECLEXPORT(void) RTCALL AssertMsg2(const char *pszFormat, ...)
1132{
1133 PRTLOGGER pLog = RTLogDefaultInstance();
1134 if (pLog)
1135 {
1136 va_list args;
1137
1138 va_start(args, pszFormat);
1139 RTLogFormatV(rtLogOutput, pLog, pszFormat, args);
1140 va_end(args);
1141 }
1142}
1143
1144
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