VirtualBox

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

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

The Big Sun Rebranding Header Change

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 36.8 KB
Line 
1/* $Id: VMMR0.cpp 8155 2008-04-18 15:16:47Z 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 SUPR0Printf("-> GVMMR0CreateVMReq\n");
701 return GVMMR0CreateVMReq((PGVMMCREATEVMREQ)pReqHdr);
702
703 case VMMR0_DO_GVMM_DESTROY_VM:
704 if (pReqHdr || u64Arg)
705 return VERR_INVALID_PARAMETER;
706 return GVMMR0DestroyVM(pVM);
707
708 case VMMR0_DO_GVMM_SCHED_HALT:
709 if (pReqHdr)
710 return VERR_INVALID_PARAMETER;
711 return GVMMR0SchedHalt(pVM, u64Arg);
712
713 case VMMR0_DO_GVMM_SCHED_WAKE_UP:
714 if (pReqHdr || u64Arg)
715 return VERR_INVALID_PARAMETER;
716 return GVMMR0SchedWakeUp(pVM);
717
718 case VMMR0_DO_GVMM_SCHED_POLL:
719 if (pReqHdr || u64Arg > 1)
720 return VERR_INVALID_PARAMETER;
721 return GVMMR0SchedPoll(pVM, (bool)u64Arg);
722
723 case VMMR0_DO_GVMM_QUERY_STATISTICS:
724 if (u64Arg)
725 return VERR_INVALID_PARAMETER;
726 return GVMMR0QueryStatisticsReq(pVM, (PGVMMQUERYSTATISTICSSREQ)pReqHdr);
727
728 case VMMR0_DO_GVMM_RESET_STATISTICS:
729 if (u64Arg)
730 return VERR_INVALID_PARAMETER;
731 return GVMMR0ResetStatisticsReq(pVM, (PGVMMRESETSTATISTICSSREQ)pReqHdr);
732
733 /*
734 * Initialize the R0 part of a VM instance.
735 */
736 case VMMR0_DO_VMMR0_INIT:
737 return VMMR0Init(pVM, (unsigned)u64Arg);
738
739 /*
740 * Terminate the R0 part of a VM instance.
741 */
742 case VMMR0_DO_VMMR0_TERM:
743 return VMMR0Term(pVM);
744
745 /*
746 * Attempt to enable hwacc mode and check the current setting.
747 *
748 */
749 case VMMR0_DO_HWACC_ENABLE:
750 return HWACCMR0EnableAllCpus(pVM, (HWACCMSTATE)u64Arg);
751
752 /*
753 * Setup the hardware accelerated raw-mode session.
754 */
755 case VMMR0_DO_HWACC_SETUP_VM:
756 {
757 RTCCUINTREG fFlags = ASMIntDisableFlags();
758 int rc = HWACCMR0SetupVM(pVM);
759 ASMSetFlags(fFlags);
760 return rc;
761 }
762
763 /*
764 * Switch to GC to execute Hypervisor function.
765 */
766 case VMMR0_DO_CALL_HYPERVISOR:
767 {
768 /* Safety precaution as HWACCM can disable the switcher. */
769 Assert(!pVM->vmm.s.fSwitcherDisabled);
770 if (RT_UNLIKELY(pVM->vmm.s.fSwitcherDisabled))
771 return VERR_NOT_SUPPORTED;
772
773 RTCCUINTREG fFlags = ASMIntDisableFlags();
774 int rc = pVM->vmm.s.pfnR0HostToGuest(pVM);
775 /** @todo dispatch interrupts? */
776 ASMSetFlags(fFlags);
777 return rc;
778 }
779
780 /*
781 * PGM wrappers.
782 */
783 case VMMR0_DO_PGM_ALLOCATE_HANDY_PAGES:
784 return PGMR0PhysAllocateHandyPages(pVM);
785
786 /*
787 * GMM wrappers.
788 */
789 case VMMR0_DO_GMM_INITIAL_RESERVATION:
790 if (u64Arg)
791 return VERR_INVALID_PARAMETER;
792 return GMMR0InitialReservationReq(pVM, (PGMMINITIALRESERVATIONREQ)pReqHdr);
793 case VMMR0_DO_GMM_UPDATE_RESERVATION:
794 if (u64Arg)
795 return VERR_INVALID_PARAMETER;
796 return GMMR0UpdateReservationReq(pVM, (PGMMUPDATERESERVATIONREQ)pReqHdr);
797
798 case VMMR0_DO_GMM_ALLOCATE_PAGES:
799 if (u64Arg)
800 return VERR_INVALID_PARAMETER;
801 return GMMR0AllocatePagesReq(pVM, (PGMMALLOCATEPAGESREQ)pReqHdr);
802 case VMMR0_DO_GMM_FREE_PAGES:
803 if (u64Arg)
804 return VERR_INVALID_PARAMETER;
805 return GMMR0FreePagesReq(pVM, (PGMMFREEPAGESREQ)pReqHdr);
806 case VMMR0_DO_GMM_BALLOONED_PAGES:
807 if (u64Arg)
808 return VERR_INVALID_PARAMETER;
809 return GMMR0BalloonedPagesReq(pVM, (PGMMBALLOONEDPAGESREQ)pReqHdr);
810 case VMMR0_DO_GMM_DEFLATED_BALLOON:
811 if (pReqHdr)
812 return VERR_INVALID_PARAMETER;
813 return GMMR0DeflatedBalloon(pVM, (uint32_t)u64Arg);
814
815 case VMMR0_DO_GMM_MAP_UNMAP_CHUNK:
816 if (u64Arg)
817 return VERR_INVALID_PARAMETER;
818 return GMMR0MapUnmapChunkReq(pVM, (PGMMMAPUNMAPCHUNKREQ)pReqHdr);
819 case VMMR0_DO_GMM_SEED_CHUNK:
820 if (pReqHdr)
821 return VERR_INVALID_PARAMETER;
822 return GMMR0SeedChunk(pVM, (RTR3PTR)u64Arg);
823
824 /*
825 * A quick GCFGM mock-up.
826 */
827 /** @todo GCFGM with proper access control, ring-3 management interface and all that. */
828 case VMMR0_DO_GCFGM_SET_VALUE:
829 case VMMR0_DO_GCFGM_QUERY_VALUE:
830 {
831 if (pVM || !pReqHdr || u64Arg)
832 return VERR_INVALID_PARAMETER;
833 PGCFGMVALUEREQ pReq = (PGCFGMVALUEREQ)pReqHdr;
834 if (pReq->Hdr.cbReq != sizeof(*pReq))
835 return VERR_INVALID_PARAMETER;
836 int rc;
837 if (enmOperation == VMMR0_DO_GCFGM_SET_VALUE)
838 {
839 rc = GVMMR0SetConfig(pReq->pSession, &pReq->szName[0], pReq->u64Value);
840 //if (rc == VERR_CFGM_VALUE_NOT_FOUND)
841 // rc = GMMR0SetConfig(pReq->pSession, &pReq->szName[0], pReq->u64Value);
842 }
843 else
844 {
845 rc = GVMMR0QueryConfig(pReq->pSession, &pReq->szName[0], &pReq->u64Value);
846 //if (rc == VERR_CFGM_VALUE_NOT_FOUND)
847 // rc = GMMR0QueryConfig(pReq->pSession, &pReq->szName[0], &pReq->u64Value);
848 }
849 return rc;
850 }
851
852
853#ifdef VBOX_WITH_INTERNAL_NETWORKING
854 /*
855 * Requests to the internal networking service.
856 */
857 case VMMR0_DO_INTNET_OPEN:
858 if (!pVM || u64Arg)
859 return VERR_INVALID_PARAMETER;
860 if (!g_pIntNet)
861 return VERR_NOT_SUPPORTED;
862 return INTNETR0OpenReq(g_pIntNet, pVM->pSession, (PINTNETOPENREQ)pReqHdr);
863
864 case VMMR0_DO_INTNET_IF_CLOSE:
865 if (!pVM || u64Arg)
866 return VERR_INVALID_PARAMETER;
867 if (!g_pIntNet)
868 return VERR_NOT_SUPPORTED;
869 return INTNETR0IfCloseReq(g_pIntNet, (PINTNETIFCLOSEREQ)pReqHdr);
870
871 case VMMR0_DO_INTNET_IF_GET_RING3_BUFFER:
872 if (!pVM || u64Arg)
873 return VERR_INVALID_PARAMETER;
874 if (!g_pIntNet)
875 return VERR_NOT_SUPPORTED;
876 return INTNETR0IfGetRing3BufferReq(g_pIntNet, (PINTNETIFGETRING3BUFFERREQ)pReqHdr);
877
878 case VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE:
879 if (!pVM || u64Arg)
880 return VERR_INVALID_PARAMETER;
881 if (!g_pIntNet)
882 return VERR_NOT_SUPPORTED;
883 return INTNETR0IfSetPromiscuousModeReq(g_pIntNet, (PINTNETIFSETPROMISCUOUSMODEREQ)pReqHdr);
884
885 case VMMR0_DO_INTNET_IF_SEND:
886 if (!pVM || u64Arg)
887 return VERR_INVALID_PARAMETER;
888 if (!g_pIntNet)
889 return VERR_NOT_SUPPORTED;
890 return INTNETR0IfSendReq(g_pIntNet, (PINTNETIFSENDREQ)pReqHdr);
891
892 case VMMR0_DO_INTNET_IF_WAIT:
893 if (!pVM || u64Arg)
894 return VERR_INVALID_PARAMETER;
895 if (!g_pIntNet)
896 return VERR_NOT_SUPPORTED;
897 return INTNETR0IfWaitReq(g_pIntNet, (PINTNETIFWAITREQ)pReqHdr);
898#endif /* VBOX_WITH_INTERNAL_NETWORKING */
899
900 /*
901 * For profiling.
902 */
903 case VMMR0_DO_NOP:
904 return VINF_SUCCESS;
905
906 /*
907 * For testing Ring-0 APIs invoked in this environment.
908 */
909 case VMMR0_DO_TESTS:
910 /** @todo make new test */
911 return VINF_SUCCESS;
912
913
914 default:
915 /*
916 * We're returning VERR_NOT_SUPPORT here so we've got something else
917 * than -1 which the interrupt gate glue code might return.
918 */
919 Log(("operation %#x is not supported\n", enmOperation));
920 return VERR_NOT_SUPPORTED;
921 }
922}
923
924
925/**
926 * Argument for vmmR0EntryExWrapper containing the argument s ofr VMMR0EntryEx.
927 */
928typedef struct VMMR0ENTRYEXARGS
929{
930 PVM pVM;
931 VMMR0OPERATION enmOperation;
932 PSUPVMMR0REQHDR pReq;
933 uint64_t u64Arg;
934} VMMR0ENTRYEXARGS;
935/** Pointer to a vmmR0EntryExWrapper argument package. */
936typedef VMMR0ENTRYEXARGS *PVMMR0ENTRYEXARGS;
937
938/**
939 * This is just a longjmp wrapper function for VMMR0EntryEx calls.
940 *
941 * @returns VBox status code.
942 * @param pvArgs The argument package
943 */
944static int vmmR0EntryExWrapper(void *pvArgs)
945{
946 return vmmR0EntryExWorker(((PVMMR0ENTRYEXARGS)pvArgs)->pVM,
947 ((PVMMR0ENTRYEXARGS)pvArgs)->enmOperation,
948 ((PVMMR0ENTRYEXARGS)pvArgs)->pReq,
949 ((PVMMR0ENTRYEXARGS)pvArgs)->u64Arg);
950}
951
952
953/**
954 * The Ring 0 entry point, called by the support library (SUP).
955 *
956 * @returns VBox status code.
957 * @param pVM The VM to operate on.
958 * @param enmOperation Which operation to execute.
959 * @param pReq This points to a SUPVMMR0REQHDR packet. Optional.
960 * @param u64Arg Some simple constant argument.
961 * @remarks Assume called with interrupts _enabled_.
962 */
963VMMR0DECL(int) VMMR0EntryEx(PVM pVM, VMMR0OPERATION enmOperation, PSUPVMMR0REQHDR pReq, uint64_t u64Arg)
964{
965 /*
966 * Requests that should only happen on the EMT thread will be
967 * wrapped in a setjmp so we can assert without causing trouble.
968 */
969 if ( VALID_PTR(pVM)
970 && pVM->pVMR0)
971 {
972 switch (enmOperation)
973 {
974 case VMMR0_DO_VMMR0_INIT:
975 case VMMR0_DO_VMMR0_TERM:
976 case VMMR0_DO_GMM_INITIAL_RESERVATION:
977 case VMMR0_DO_GMM_UPDATE_RESERVATION:
978 case VMMR0_DO_GMM_ALLOCATE_PAGES:
979 case VMMR0_DO_GMM_FREE_PAGES:
980 case VMMR0_DO_GMM_BALLOONED_PAGES:
981 case VMMR0_DO_GMM_DEFLATED_BALLOON:
982 case VMMR0_DO_GMM_MAP_UNMAP_CHUNK:
983 case VMMR0_DO_GMM_SEED_CHUNK:
984 {
985 /** @todo validate this EMT claim... GVM knows. */
986 VMMR0ENTRYEXARGS Args;
987 Args.pVM = pVM;
988 Args.enmOperation = enmOperation;
989 Args.pReq = pReq;
990 Args.u64Arg = u64Arg;
991 return vmmR0CallHostSetJmpEx(&pVM->vmm.s.CallHostR0JmpBuf, vmmR0EntryExWrapper, &Args);
992 }
993
994 default:
995 break;
996 }
997 }
998 return vmmR0EntryExWorker(pVM, enmOperation, pReq, u64Arg);
999}
1000
1001
1002
1003/**
1004 * Internal R0 logger worker: Flush logger.
1005 *
1006 * @param pLogger The logger instance to flush.
1007 * @remark This function must be exported!
1008 */
1009VMMR0DECL(void) vmmR0LoggerFlush(PRTLOGGER pLogger)
1010{
1011 /*
1012 * Convert the pLogger into a VM handle and 'call' back to Ring-3.
1013 * (This is a bit paranoid code.)
1014 */
1015 PVMMR0LOGGER pR0Logger = (PVMMR0LOGGER)((uintptr_t)pLogger - RT_OFFSETOF(VMMR0LOGGER, Logger));
1016 if ( !VALID_PTR(pR0Logger)
1017 || !VALID_PTR(pR0Logger + 1)
1018 || !VALID_PTR(pLogger)
1019 || pLogger->u32Magic != RTLOGGER_MAGIC)
1020 {
1021#ifdef DEBUG
1022 SUPR0Printf("vmmR0LoggerFlush: pLogger=%p!\n", pLogger);
1023#endif
1024 return;
1025 }
1026
1027 PVM pVM = pR0Logger->pVM;
1028 if ( !VALID_PTR(pVM)
1029 || pVM->pVMR0 != pVM)
1030 {
1031#ifdef DEBUG
1032 SUPR0Printf("vmmR0LoggerFlush: pVM=%p! pVMR0=%p! pLogger=%p\n", pVM, pVM->pVMR0, pLogger);
1033#endif
1034 return;
1035 }
1036
1037 /*
1038 * Check that the jump buffer is armed.
1039 */
1040#ifdef RT_ARCH_X86
1041 if (!pVM->vmm.s.CallHostR0JmpBuf.eip)
1042#else
1043 if (!pVM->vmm.s.CallHostR0JmpBuf.rip)
1044#endif
1045 {
1046#ifdef DEBUG
1047 SUPR0Printf("vmmR0LoggerFlush: Jump buffer isn't armed!\n");
1048#endif
1049 pLogger->offScratch = 0;
1050 return;
1051 }
1052 VMMR0CallHost(pVM, VMMCALLHOST_VMM_LOGGER_FLUSH, 0);
1053}
1054
1055
1056
1057/**
1058 * Jump back to ring-3 if we're the EMT and the longjmp is armed.
1059 *
1060 * @returns true if the breakpoint should be hit, false if it should be ignored.
1061 * @remark The RTDECL() makes this a bit difficult to override on windows. Sorry.
1062 */
1063DECLEXPORT(bool) RTCALL RTAssertDoBreakpoint(void)
1064{
1065 PVM pVM = GVMMR0GetVMByEMT(NIL_RTNATIVETHREAD);
1066 if (pVM)
1067 {
1068#ifdef RT_ARCH_X86
1069 if (pVM->vmm.s.CallHostR0JmpBuf.eip)
1070#else
1071 if (pVM->vmm.s.CallHostR0JmpBuf.rip)
1072#endif
1073 {
1074 int rc = VMMR0CallHost(pVM, VMMCALLHOST_VM_R0_HYPER_ASSERTION, 0);
1075 return RT_FAILURE_NP(rc);
1076 }
1077 }
1078#ifdef RT_OS_LINUX
1079 return true;
1080#else
1081 return false;
1082#endif
1083}
1084
1085
1086
1087# undef LOG_GROUP
1088# define LOG_GROUP LOG_GROUP_EM
1089
1090/**
1091 * Override this so we can push
1092 *
1093 * @param pszExpr Expression. Can be NULL.
1094 * @param uLine Location line number.
1095 * @param pszFile Location file name.
1096 * @param pszFunction Location function name.
1097 * @remark This API exists in HC Ring-3 and GC.
1098 */
1099DECLEXPORT(void) RTCALL AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
1100{
1101 SUPR0Printf("\n!!R0-Assertion Failed!!\n"
1102 "Expression: %s\n"
1103 "Location : %s(%d) %s\n",
1104 pszExpr, pszFile, uLine, pszFunction);
1105
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 SUPR0Printf("%c", pachChars[i]);
1123 }
1124
1125 return cbChars;
1126}
1127
1128
1129DECLEXPORT(void) RTCALL AssertMsg2(const char *pszFormat, ...)
1130{
1131 PRTLOGGER pLog = RTLogDefaultInstance();
1132 if (pLog)
1133 {
1134 va_list args;
1135
1136 va_start(args, pszFormat);
1137 RTLogFormatV(rtLogOutput, pLog, pszFormat, args);
1138 va_end(args);
1139 }
1140}
1141
1142
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