VirtualBox

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

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

Accidental commit removed

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 29.3 KB
Line 
1/* $Id: VMMR0.cpp 4954 2007-09-21 14:10:37Z vboxsync $ */
2/** @file
3 * VMM - Host Context Ring 0.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_VMM
23#include <VBox/vmm.h>
24#include <VBox/sup.h>
25#include <VBox/trpm.h>
26#include <VBox/cpum.h>
27#include <VBox/stam.h>
28#include <VBox/tm.h>
29#include "VMMInternal.h"
30#include <VBox/vm.h>
31#include <VBox/intnet.h>
32#include <VBox/hwaccm.h>
33
34#include <VBox/err.h>
35#include <VBox/version.h>
36#include <VBox/log.h>
37#include <iprt/assert.h>
38#include <iprt/stdarg.h>
39
40#if defined(_MSC_VER) && defined(RT_ARCH_AMD64) /** @todo check this with with VC7! */
41# pragma intrinsic(_AddressOfReturnAddress)
42#endif
43
44
45/*******************************************************************************
46* Internal Functions *
47*******************************************************************************/
48static int VMMR0Init(PVM pVM, unsigned uVersion);
49static int VMMR0Term(PVM pVM);
50__BEGIN_DECLS
51VMMR0DECL(int) ModuleInit(void);
52VMMR0DECL(void) ModuleTerm(void);
53__END_DECLS
54
55
56/** @def DEBUG_NO_RING0_ASSERTIONS
57 * Define this if you don't wish to BSOD on debug assertions in
58 * the VMMR0.r0 code. If would of course be nice to have this
59 * feature enabled by default of course, but it's not currently
60 * safe when more than one VM is running or when using internal
61 * networking. */
62#if defined(DEBUG_sandervl) /*|| defined(DEBUG_bird)*/
63# define DEBUG_NO_RING0_ASSERTIONS
64#endif
65#ifdef DEBUG_NO_RING0_ASSERTIONS
66static PVM g_pVMAssert = 0;
67#endif
68
69/*******************************************************************************
70* Global Variables *
71*******************************************************************************/
72#ifdef VBOX_WITH_INTERNAL_NETWORKING
73/** Pointer to the internal networking service instance. */
74PINTNET g_pIntNet = 0;
75#endif
76
77
78/**
79 * Initialize the module.
80 * This is called when we're first loaded.
81 *
82 * @returns 0 on success.
83 * @returns VBox status on failure.
84 */
85VMMR0DECL(int) ModuleInit(void)
86{
87#ifdef VBOX_WITH_INTERNAL_NETWORKING
88 LogFlow(("ModuleInit: g_pIntNet=%p\n", g_pIntNet));
89 g_pIntNet = NULL;
90 LogFlow(("ModuleInit: g_pIntNet=%p should be NULL now...\n", g_pIntNet));
91 int rc = INTNETR0Create(&g_pIntNet);
92 if (VBOX_SUCCESS(rc))
93 {
94 LogFlow(("ModuleInit: returns success. g_pIntNet=%p\n", g_pIntNet));
95 return 0;
96 }
97 g_pIntNet = NULL;
98 LogFlow(("ModuleTerm: returns %Vrc\n", rc));
99 return rc;
100#else
101 return 0;
102#endif
103}
104
105
106/**
107 * Terminate the module.
108 * This is called when we're finally unloaded.
109 */
110VMMR0DECL(void) ModuleTerm(void)
111{
112#ifdef VBOX_WITH_INTERNAL_NETWORKING
113 LogFlow(("ModuleTerm:\n"));
114 if (g_pIntNet)
115 {
116 INTNETR0Destroy(g_pIntNet);
117 g_pIntNet = NULL;
118 }
119 LogFlow(("ModuleTerm: returns\n"));
120#endif
121}
122
123
124/**
125 * Initaties the R0 driver for a particular VM instance.
126 *
127 * @returns VBox status code.
128 *
129 * @param pVM The VM instance in question.
130 * @param uVersion The minimum module version required.
131 */
132static int VMMR0Init(PVM pVM, unsigned uVersion)
133{
134 /*
135 * Check if compatible version.
136 */
137 if ( uVersion != VBOX_VERSION
138 && ( VBOX_GET_VERSION_MAJOR(uVersion) != VBOX_VERSION_MAJOR
139 || VBOX_GET_VERSION_MINOR(uVersion) < VBOX_VERSION_MINOR))
140 return VERR_VERSION_MISMATCH;
141 if ( !VALID_PTR(pVM)
142 || pVM->pVMR0 != pVM)
143 return VERR_INVALID_PARAMETER;
144
145 /*
146 * Register the EMT R0 logger instance.
147 */
148 PVMMR0LOGGER pR0Logger = pVM->vmm.s.pR0Logger;
149 if (pR0Logger)
150 {
151#if 0 /* testing of the logger. */
152 LogCom(("VMMR0Init: before %p\n", RTLogDefaultInstance()));
153 LogCom(("VMMR0Init: pfnFlush=%p actual=%p\n", pR0Logger->Logger.pfnFlush, vmmR0LoggerFlush));
154 LogCom(("VMMR0Init: pfnLogger=%p actual=%p\n", pR0Logger->Logger.pfnLogger, vmmR0LoggerWrapper));
155 LogCom(("VMMR0Init: offScratch=%d fFlags=%#x fDestFlags=%#x\n", pR0Logger->Logger.offScratch, pR0Logger->Logger.fFlags, pR0Logger->Logger.fDestFlags));
156
157 RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
158 LogCom(("VMMR0Init: after %p reg\n", RTLogDefaultInstance()));
159 RTLogSetDefaultInstanceThread(NULL, 0);
160 LogCom(("VMMR0Init: after %p dereg\n", RTLogDefaultInstance()));
161
162 pR0Logger->Logger.pfnLogger("hello ring-0 logger\n");
163 LogCom(("VMMR0Init: returned succesfully from direct logger call.\n"));
164 pR0Logger->Logger.pfnFlush(&pR0Logger->Logger);
165 LogCom(("VMMR0Init: returned succesfully from direct flush call.\n"));
166
167 RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
168 LogCom(("VMMR0Init: after %p reg2\n", RTLogDefaultInstance()));
169 pR0Logger->Logger.pfnLogger("hello ring-0 logger\n");
170 LogCom(("VMMR0Init: returned succesfully from direct logger call (2). offScratch=%d\n", pR0Logger->Logger.offScratch));
171 RTLogSetDefaultInstanceThread(NULL, 0);
172 LogCom(("VMMR0Init: after %p dereg2\n", RTLogDefaultInstance()));
173
174 RTLogLoggerEx(&pR0Logger->Logger, 0, ~0U, "hello ring-0 logger (RTLogLoggerEx)\n");
175 LogCom(("VMMR0Init: RTLogLoggerEx returned fine offScratch=%d\n", pR0Logger->Logger.offScratch));
176
177 RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
178 RTLogPrintf("hello ring-0 logger (RTLogPrintf)\n");
179 LogCom(("VMMR0Init: RTLogPrintf returned fine offScratch=%d\n", pR0Logger->Logger.offScratch));
180#endif
181 RTLogSetDefaultInstanceThread(&pR0Logger->Logger, (uintptr_t)pVM->pSession);
182 }
183
184
185 /*
186 * Init VMXM.
187 */
188 int rc = HWACCMR0Init(pVM);
189 if (VBOX_FAILURE(rc))
190 {
191 RTLogSetDefaultInstanceThread(NULL, 0);
192 return rc;
193 }
194
195 /*
196 * Init CPUM.
197 */
198 rc = CPUMR0Init(pVM);
199
200 if (RT_FAILURE(rc))
201 RTLogSetDefaultInstanceThread(NULL, 0);
202 return rc;
203}
204
205
206/**
207 * Terminates the R0 driver for a particular VM instance.
208 *
209 * @returns VBox status code.
210 *
211 * @param pVM The VM instance in question.
212 */
213static int VMMR0Term(PVM pVM)
214{
215 /*
216 * Deregister the logger.
217 */
218 RTLogSetDefaultInstanceThread(NULL, 0);
219 return VINF_SUCCESS;
220}
221
222
223/**
224 * Calls the ring-3 host code.
225 *
226 * @returns VBox status code of the ring-3 call.
227 * @param pVM The VM handle.
228 * @param enmOperation The operation.
229 * @param uArg The argument to the operation.
230 */
231VMMR0DECL(int) VMMR0CallHost(PVM pVM, VMMCALLHOST enmOperation, uint64_t uArg)
232{
233/** @todo profile this! */
234 pVM->vmm.s.enmCallHostOperation = enmOperation;
235 pVM->vmm.s.u64CallHostArg = uArg;
236 pVM->vmm.s.rcCallHost = VERR_INTERNAL_ERROR;
237 int rc = vmmR0CallHostLongJmp(&pVM->vmm.s.CallHostR0JmpBuf, VINF_VMM_CALL_HOST);
238 if (rc == VINF_SUCCESS)
239 rc = pVM->vmm.s.rcCallHost;
240 return rc;
241}
242
243
244#ifdef VBOX_WITH_STATISTICS
245/**
246 * Record return code statistics
247 * @param pVM The VM handle.
248 * @param rc The status code.
249 */
250static void vmmR0RecordRC(PVM pVM, int rc)
251{
252 /*
253 * Collect statistics.
254 */
255 switch (rc)
256 {
257 case VINF_SUCCESS:
258 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetNormal);
259 break;
260 case VINF_EM_RAW_INTERRUPT:
261 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetInterrupt);
262 break;
263 case VINF_EM_RAW_INTERRUPT_HYPER:
264 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetInterruptHyper);
265 break;
266 case VINF_EM_RAW_GUEST_TRAP:
267 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetGuestTrap);
268 break;
269 case VINF_EM_RAW_RING_SWITCH:
270 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetRingSwitch);
271 break;
272 case VINF_EM_RAW_RING_SWITCH_INT:
273 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetRingSwitchInt);
274 break;
275 case VINF_EM_RAW_EXCEPTION_PRIVILEGED:
276 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetExceptionPrivilege);
277 break;
278 case VINF_EM_RAW_STALE_SELECTOR:
279 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetStaleSelector);
280 break;
281 case VINF_EM_RAW_IRET_TRAP:
282 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetIRETTrap);
283 break;
284 case VINF_IOM_HC_IOPORT_READ:
285 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetIORead);
286 break;
287 case VINF_IOM_HC_IOPORT_WRITE:
288 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetIOWrite);
289 break;
290 case VINF_IOM_HC_MMIO_READ:
291 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetMMIORead);
292 break;
293 case VINF_IOM_HC_MMIO_WRITE:
294 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetMMIOWrite);
295 break;
296 case VINF_IOM_HC_MMIO_READ_WRITE:
297 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetMMIOReadWrite);
298 break;
299 case VINF_PATM_HC_MMIO_PATCH_READ:
300 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetMMIOPatchRead);
301 break;
302 case VINF_PATM_HC_MMIO_PATCH_WRITE:
303 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetMMIOPatchWrite);
304 break;
305 case VINF_EM_RAW_EMULATE_INSTR:
306 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetEmulate);
307 break;
308 case VINF_PATCH_EMULATE_INSTR:
309 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPatchEmulate);
310 break;
311 case VINF_EM_RAW_EMULATE_INSTR_LDT_FAULT:
312 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetLDTFault);
313 break;
314 case VINF_EM_RAW_EMULATE_INSTR_GDT_FAULT:
315 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetGDTFault);
316 break;
317 case VINF_EM_RAW_EMULATE_INSTR_IDT_FAULT:
318 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetIDTFault);
319 break;
320 case VINF_EM_RAW_EMULATE_INSTR_TSS_FAULT:
321 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetTSSFault);
322 break;
323 case VINF_EM_RAW_EMULATE_INSTR_PD_FAULT:
324 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPDFault);
325 break;
326 case VINF_CSAM_PENDING_ACTION:
327 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetCSAMTask);
328 break;
329 case VINF_PGM_SYNC_CR3:
330 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetSyncCR3);
331 break;
332 case VINF_PATM_PATCH_INT3:
333 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPatchInt3);
334 break;
335 case VINF_PATM_PATCH_TRAP_PF:
336 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPatchPF);
337 break;
338 case VINF_PATM_PATCH_TRAP_GP:
339 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPatchGP);
340 break;
341 case VINF_PATM_PENDING_IRQ_AFTER_IRET:
342 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPatchIretIRQ);
343 break;
344 case VERR_REM_FLUSHED_PAGES_OVERFLOW:
345 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPageOverflow);
346 break;
347 case VINF_EM_RESCHEDULE_REM:
348 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetRescheduleREM);
349 break;
350 case VINF_EM_RAW_TO_R3:
351 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetToR3);
352 break;
353 case VINF_EM_RAW_TIMER_PENDING:
354 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetTimerPending);
355 break;
356 case VINF_EM_RAW_INTERRUPT_PENDING:
357 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetInterruptPending);
358 break;
359 case VINF_VMM_CALL_HOST:
360 switch (pVM->vmm.s.enmCallHostOperation)
361 {
362 case VMMCALLHOST_PDM_LOCK:
363 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPDMLock);
364 break;
365 case VMMCALLHOST_PDM_QUEUE_FLUSH:
366 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPDMQueueFlush);
367 break;
368 case VMMCALLHOST_PGM_POOL_GROW:
369 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPGMPoolGrow);
370 break;
371 case VMMCALLHOST_PGM_LOCK:
372 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPGMLock);
373 break;
374 case VMMCALLHOST_REM_REPLAY_HANDLER_NOTIFICATIONS:
375 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetRemReplay);
376 break;
377 case VMMCALLHOST_PGM_RAM_GROW_RANGE:
378 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPGMGrowRAM);
379 break;
380 case VMMCALLHOST_VMM_LOGGER_FLUSH:
381 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetLogFlush);
382 break;
383 case VMMCALLHOST_VM_SET_ERROR:
384 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetVMSetError);
385 break;
386 case VMMCALLHOST_VM_SET_RUNTIME_ERROR:
387 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetVMSetRuntimeError);
388 break;
389 default:
390 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetCallHost);
391 break;
392 }
393 break;
394 case VINF_PATM_DUPLICATE_FUNCTION:
395 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPATMDuplicateFn);
396 break;
397 case VINF_PGM_CHANGE_MODE:
398 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPGMChangeMode);
399 break;
400 case VINF_EM_RAW_EMULATE_INSTR_HLT:
401 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetEmulHlt);
402 break;
403 case VINF_EM_PENDING_REQUEST:
404 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetPendingRequest);
405 break;
406 default:
407 STAM_COUNTER_INC(&pVM->vmm.s.StatGCRetMisc);
408 break;
409 }
410}
411#endif /* VBOX_WITH_STATISTICS */
412
413
414
415/**
416 * The Ring 0 entry point, called by the interrupt gate.
417 *
418 * @returns VBox status code.
419 * @param pVM The VM to operate on.
420 * @param enmOperation Which operation to execute.
421 * @param pvArg Argument to the operation.
422 * @remarks Assume called with interrupts disabled.
423 */
424VMMR0DECL(int) VMMR0EntryInt(PVM pVM, VMMR0OPERATION enmOperation, void *pvArg)
425{
426 switch (enmOperation)
427 {
428#ifdef VBOX_WITH_IDT_PATCHING
429 /*
430 * Switch to GC.
431 * These calls return whatever the GC returns.
432 */
433 case VMMR0_DO_RAW_RUN:
434 {
435 /* Safety precaution as VMX disables the switcher. */
436 Assert(!pVM->vmm.s.fSwitcherDisabled);
437 if (pVM->vmm.s.fSwitcherDisabled)
438 return VERR_NOT_SUPPORTED;
439
440 STAM_COUNTER_INC(&pVM->vmm.s.StatRunGC);
441 register int rc;
442 pVM->vmm.s.iLastGCRc = rc = pVM->vmm.s.pfnR0HostToGuest(pVM);
443
444#ifdef VBOX_WITH_STATISTICS
445 vmmR0RecordRC(pVM, rc);
446#endif
447
448 /*
449 * We'll let TRPM change the stack frame so our return is different.
450 * Just keep in mind that after the call, things have changed!
451 */
452 if ( rc == VINF_EM_RAW_INTERRUPT
453 || rc == VINF_EM_RAW_INTERRUPT_HYPER)
454 {
455 /*
456 * Don't trust the compiler to get this right.
457 * gcc -fomit-frame-pointer screws up big time here. This works fine in 64-bit
458 * mode too because we push the arguments on the stack in the IDT patch code.
459 */
460# if defined(__GNUC__)
461 void *pvRet = (uint8_t *)__builtin_frame_address(0) + sizeof(void *);
462# elif defined(_MSC_VER) && defined(RT_ARCH_AMD64) /** @todo check this with with VC7! */
463 void *pvRet = (uint8_t *)_AddressOfReturnAddress();
464# elif defined(RT_ARCH_X86)
465 void *pvRet = (uint8_t *)&pVM - sizeof(pVM);
466# else
467# error "huh?"
468# endif
469 if ( ((uintptr_t *)pvRet)[1] == (uintptr_t)pVM
470 && ((uintptr_t *)pvRet)[2] == (uintptr_t)enmOperation
471 && ((uintptr_t *)pvRet)[3] == (uintptr_t)pvArg)
472 TRPMR0SetupInterruptDispatcherFrame(pVM, pvRet);
473 else
474 {
475# if defined(DEBUG) || defined(LOG_ENABLED)
476 static bool s_fHaveWarned = false;
477 if (!s_fHaveWarned)
478 {
479 s_fHaveWarned = true;
480 //RTLogPrintf("VMMR0.r0: The compiler can't find the stack frame!\n"); -- @todo export me!
481 RTLogComPrintf("VMMR0.r0: The compiler can't find the stack frame!\n");
482 }
483# endif
484 TRPMR0DispatchHostInterrupt(pVM);
485 }
486 }
487 return rc;
488 }
489
490 /*
491 * Initialize the R0 part of a VM instance.
492 */
493 case VMMR0_DO_VMMR0_INIT:
494 {
495 RTCCUINTREG fFlags = ASMIntDisableFlags();
496 int rc = VMMR0Init(pVM, (unsigned)(uintptr_t)pvArg);
497 ASMSetFlags(fFlags);
498 return rc;
499 }
500
501 /*
502 * Terminate the R0 part of a VM instance.
503 */
504 case VMMR0_DO_VMMR0_TERM:
505 {
506 RTCCUINTREG fFlags = ASMIntDisableFlags();
507 int rc = VMMR0Term(pVM);
508 ASMSetFlags(fFlags);
509 return rc;
510 }
511
512 /*
513 * Switch to GC to execute Hypervisor function.
514 */
515 case VMMR0_DO_CALL_HYPERVISOR:
516 {
517 /* Safety precaution as VMX disables the switcher. */
518 Assert(!pVM->vmm.s.fSwitcherDisabled);
519 if (pVM->vmm.s.fSwitcherDisabled)
520 return VERR_NOT_SUPPORTED;
521
522 RTCCUINTREG fFlags = ASMIntDisableFlags();
523 int rc = pVM->vmm.s.pfnR0HostToGuest(pVM);
524 ASMSetFlags(fFlags);
525 return rc;
526 }
527
528 /*
529 * For profiling.
530 */
531 case VMMR0_DO_NOP:
532 return VINF_SUCCESS;
533#endif /* VBOX_WITH_IDT_PATCHING */
534
535 default:
536 /*
537 * We're returning VERR_NOT_SUPPORT here so we've got something else
538 * than -1 which the interrupt gate glue code might return.
539 */
540 Log(("operation %#x is not supported\n", enmOperation));
541 return VERR_NOT_SUPPORTED;
542 }
543}
544
545
546/**
547 * The Ring 0 entry point, called by the fast-ioctl path.
548 *
549 * @returns VBox status code.
550 * @param pVM The VM to operate on.
551 * @param enmOperation Which operation to execute.
552 * @remarks Assume called with interrupts disabled.
553 */
554VMMR0DECL(int) VMMR0EntryFast(PVM pVM, VMMR0OPERATION enmOperation)
555{
556 switch (enmOperation)
557 {
558 /*
559 * Switch to GC and run guest raw mode code.
560 */
561 case VMMR0_DO_RAW_RUN:
562 {
563 /* We must disable interrupts here */
564 RTCCUINTREG uFlags = ASMIntDisableFlags();
565
566 /* Safety precaution as hwaccm disables the switcher. */
567 if (RT_LIKELY(!pVM->vmm.s.fSwitcherDisabled))
568 {
569 int rc = pVM->vmm.s.pfnR0HostToGuest(pVM);
570 pVM->vmm.s.iLastGCRc = rc;
571
572 if ( rc == VINF_EM_RAW_INTERRUPT
573 || rc == VINF_EM_RAW_INTERRUPT_HYPER)
574 TRPMR0DispatchHostInterrupt(pVM);
575
576#ifdef VBOX_WITH_STATISTICS
577 STAM_COUNTER_INC(&pVM->vmm.s.StatRunGC);
578 vmmR0RecordRC(pVM, rc);
579#endif
580 ASMSetFlags(uFlags);
581 return rc;
582 }
583 ASMSetFlags(uFlags);
584
585 Assert(!pVM->vmm.s.fSwitcherDisabled);
586 return VERR_NOT_SUPPORTED;
587 }
588
589 /*
590 * Run guest code using the available hardware acceleration technology.
591 */
592 case VMMR0_DO_HWACC_RUN:
593 {
594 STAM_COUNTER_INC(&pVM->vmm.s.StatRunGC);
595
596 int rc = HWACCMR0Enable(pVM);
597 if (VBOX_SUCCESS(rc))
598 {
599#ifdef DEBUG_NO_RING0_ASSERTIONS
600 g_pVMAssert = pVM;
601#endif
602 rc = vmmR0CallHostSetJmp(&pVM->vmm.s.CallHostR0JmpBuf, HWACCMR0RunGuestCode, pVM); /* this may resume code. */
603#ifdef DEBUG_NO_RING0_ASSERTIONS
604 g_pVMAssert = NULL;
605#endif
606 int rc2 = HWACCMR0Disable(pVM);
607 AssertRC(rc2);
608 }
609 pVM->vmm.s.iLastGCRc = rc;
610
611#ifdef VBOX_WITH_STATISTICS
612 vmmR0RecordRC(pVM, rc);
613#endif
614 /* No special action required for external interrupts, just return. */
615 return rc;
616 }
617
618 /*
619 * For profiling.
620 */
621 case VMMR0_DO_NOP:
622 return VINF_SUCCESS;
623
624 /*
625 * Impossible.
626 */
627 default:
628 AssertMsgFailed(("%#x\n", enmOperation));
629 return VERR_NOT_SUPPORTED;
630 }
631}
632
633
634/**
635 * The Ring 0 entry point, called by the support library (SUP).
636 *
637 * @returns VBox status code.
638 * @param pVM The VM to operate on.
639 * @param enmOperation Which operation to execute.
640 * @param pReq This points to a SUPVMMR0REQHDR packet. Optional.
641 * @param u64Arg Some simple constant argument.
642 * @remarks Assume called with interrupts _enabled_.
643 */
644VMMR0DECL(int) VMMR0EntryEx(PVM pVM, VMMR0OPERATION enmOperation, PSUPVMMR0REQHDR pReq, uint64_t u64Arg)
645{
646 switch (enmOperation)
647 {
648 /*
649 * Initialize the R0 part of a VM instance.
650 */
651 case VMMR0_DO_VMMR0_INIT:
652 {
653 RTCCUINTREG fFlags = ASMIntDisableFlags();
654 int rc = VMMR0Init(pVM, (unsigned)u64Arg);
655 ASMSetFlags(fFlags);
656 return rc;
657 }
658
659 /*
660 * Terminate the R0 part of a VM instance.
661 */
662 case VMMR0_DO_VMMR0_TERM:
663 {
664 RTCCUINTREG fFlags = ASMIntDisableFlags();
665 int rc = VMMR0Term(pVM);
666 ASMSetFlags(fFlags);
667 return rc;
668 }
669
670 /*
671 * Setup the hardware accelerated raw-mode session.
672 */
673 case VMMR0_DO_HWACC_SETUP_VM:
674 {
675 RTCCUINTREG fFlags = ASMIntDisableFlags();
676 int rc = HWACCMR0SetupVMX(pVM);
677 ASMSetFlags(fFlags);
678 return rc;
679 }
680
681 /*
682 * Switch to GC to execute Hypervisor function.
683 */
684 case VMMR0_DO_CALL_HYPERVISOR:
685 {
686 /* Safety precaution as VMX disables the switcher. */
687 Assert(!pVM->vmm.s.fSwitcherDisabled);
688 if (pVM->vmm.s.fSwitcherDisabled)
689 return VERR_NOT_SUPPORTED;
690
691 RTCCUINTREG fFlags = ASMIntDisableFlags();
692 int rc = pVM->vmm.s.pfnR0HostToGuest(pVM);
693 ASMSetFlags(fFlags);
694 return rc;
695 }
696
697 /*
698 * PGM wrappers.
699 */
700 case VMMR0_DO_PGM_ALLOCATE_HANDY_PAGES:
701 return PGMR0PhysAllocateHandyPages(pVM);
702
703#if 0
704 /*
705 * GMM wrappers
706 */
707 case VMMR0_DO_GMM_ALLOCATE_PAGES:
708 return GMMR0AllocatePages(pVM, ...);
709 case VMMR0_DO_GMM_FREE_PAGES:
710 return GMMR0FreePages(pVM, ...);
711 case VMMR0_DO_GMM_MAP_UNMAP_CHUNK:
712 return GMMR0FreeMapUnmapChunk(pVM, ...);
713 case VMMR0_DO_GMM_SEED_CHUNK:
714 return GMMR0SeedChunk(pVM, (RTR3PTR)pvArg);
715#endif
716
717
718
719#if 0//def VBOX_WITH_INTERNAL_NETWORKING - currently busted
720 /*
721 * Services.
722 */
723 case VMMR0_DO_INTNET_OPEN:
724 case VMMR0_DO_INTNET_IF_CLOSE:
725 case VMMR0_DO_INTNET_IF_GET_RING3_BUFFER:
726 case VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE:
727 case VMMR0_DO_INTNET_IF_SEND:
728 case VMMR0_DO_INTNET_IF_WAIT:
729 {
730 /*
731 * Validate arguments a bit first.
732 */
733 if (!VALID_PTR(pvArg))
734 return VERR_INVALID_POINTER;
735 if (!VALID_PTR(pVM))
736 return VERR_INVALID_POINTER;
737 if (pVM->pVMR0 != pVM)
738 return VERR_INVALID_POINTER;
739 if (!VALID_PTR(pVM->pSession))
740 return VERR_INVALID_POINTER;
741 if (!g_pIntNet)
742 return VERR_FILE_NOT_FOUND; ///@todo fix this status code!
743
744 /*
745 * Unpack the arguments and call the service.
746 */
747 switch (enmOperation)
748 {
749 case VMMR0_DO_INTNET_OPEN:
750 {
751 PINTNETOPENARGS pArgs = (PINTNETOPENARGS)pvArg;
752 return INTNETR0Open(g_pIntNet, pVM->pSession, &pArgs->szNetwork[0], pArgs->cbSend, pArgs->cbRecv, pArgs->fRestrictAccess, &pArgs->hIf);
753 }
754
755 case VMMR0_DO_INTNET_IF_CLOSE:
756 {
757 PINTNETIFCLOSEARGS pArgs = (PINTNETIFCLOSEARGS)pvArg;
758 return INTNETR0IfClose(g_pIntNet, pArgs->hIf);
759 }
760
761 case VMMR0_DO_INTNET_IF_GET_RING3_BUFFER:
762 {
763 PINTNETIFGETRING3BUFFERARGS pArgs = (PINTNETIFGETRING3BUFFERARGS)pvArg;
764 return INTNETR0IfGetRing3Buffer(g_pIntNet, pArgs->hIf, &pArgs->pRing3Buf);
765 }
766
767 case VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE:
768 {
769 PINTNETIFSETPROMISCUOUSMODEARGS pArgs = (PINTNETIFSETPROMISCUOUSMODEARGS)pvArg;
770 return INTNETR0IfSetPromiscuousMode(g_pIntNet, pArgs->hIf, pArgs->fPromiscuous);
771 }
772
773 case VMMR0_DO_INTNET_IF_SEND:
774 {
775 PINTNETIFSENDARGS pArgs = (PINTNETIFSENDARGS)pvArg;
776 return INTNETR0IfSend(g_pIntNet, pArgs->hIf, pArgs->pvFrame, pArgs->cbFrame);
777 }
778
779 case VMMR0_DO_INTNET_IF_WAIT:
780 {
781 PINTNETIFWAITARGS pArgs = (PINTNETIFWAITARGS)pvArg;
782 return INTNETR0IfWait(g_pIntNet, pArgs->hIf, pArgs->cMillies);
783 }
784
785 default:
786 return VERR_NOT_SUPPORTED;
787 }
788 }
789#endif /* VBOX_WITH_INTERNAL_NETWORKING */
790
791 /*
792 * For profiling.
793 */
794 case VMMR0_DO_NOP:
795 return VINF_SUCCESS;
796
797 /*
798 * For testing Ring-0 APIs invoked in this environment.
799 */
800 case VMMR0_DO_TESTS:
801 /** @todo make new test */
802 return VINF_SUCCESS;
803
804
805 default:
806 /*
807 * We're returning VERR_NOT_SUPPORT here so we've got something else
808 * than -1 which the interrupt gate glue code might return.
809 */
810 Log(("operation %#x is not supported\n", enmOperation));
811 return VERR_NOT_SUPPORTED;
812 }
813}
814
815
816
817
818/**
819 * Internal R0 logger worker: Flush logger.
820 *
821 * @param pLogger The logger instance to flush.
822 * @remark This function must be exported!
823 */
824VMMR0DECL(void) vmmR0LoggerFlush(PRTLOGGER pLogger)
825{
826 /*
827 * Convert the pLogger into a VM handle and 'call' back to Ring-3.
828 * (This is a bit paranoid code.)
829 */
830 PVMMR0LOGGER pR0Logger = (PVMMR0LOGGER)((uintptr_t)pLogger - RT_OFFSETOF(VMMR0LOGGER, Logger));
831 if ( !VALID_PTR(pR0Logger)
832 || !VALID_PTR(pR0Logger + 1)
833 || !VALID_PTR(pLogger)
834 || pLogger->u32Magic != RTLOGGER_MAGIC)
835 {
836 LogCom(("vmmR0LoggerFlush: pLogger=%p!\n", pLogger));
837 return;
838 }
839
840 PVM pVM = pR0Logger->pVM;
841 if ( !VALID_PTR(pVM)
842 || pVM->pVMR0 != pVM)
843 {
844 LogCom(("vmmR0LoggerFlush: pVM=%p! pLogger=%p\n", pVM, pLogger));
845 return;
846 }
847
848 /*
849 * Check that the jump buffer is armed.
850 */
851#ifdef RT_ARCH_X86
852 if (!pVM->vmm.s.CallHostR0JmpBuf.eip)
853#else
854 if (!pVM->vmm.s.CallHostR0JmpBuf.rip)
855#endif
856 {
857 LogCom(("vmmR0LoggerFlush: Jump buffer isn't armed!\n"));
858 pLogger->offScratch = 0;
859 return;
860 }
861
862 VMMR0CallHost(pVM, VMMCALLHOST_VMM_LOGGER_FLUSH, 0);
863}
864
865
866#ifdef DEBUG_NO_RING0_ASSERTIONS
867/**
868 * Check if we really want to hit a breakpoint.
869 * Can jump back to ring-3 when the longjmp is armed.
870 */
871DECLEXPORT(bool) RTCALL RTAssertDoBreakpoint()
872{
873 if (g_pVMAssert)
874 {
875 int rc = VMMR0CallHost(g_pVMAssert, VMMCALLHOST_VM_R0_HYPER_ASSERTION, 0);
876 if (rc == VINF_SUCCESS)
877 rc = g_pVMAssert->vmm.s.rcCallHost;
878 }
879
880 return false;
881}
882#endif /* !DEBUG_NO_RING0_ASSERTIONS */
883
884
885
886# undef LOG_GROUP
887# define LOG_GROUP LOG_GROUP_EM
888
889DECLEXPORT(void) RTCALL RTR0AssertBreakpoint(void *pvVM)
890{
891 if (pvVM)
892 {
893 PVM pVM = (PVM)pvVM;
894 VMMR0CallHost(pVM, VMMCALLHOST_VM_R0_HYPER_ASSERTION, 0);
895 /* does not return */
896 }
897}
898
899/** Runtime assert implementation for Native Win32 Ring-0. */
900DECLEXPORT(void) RTCALL AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
901{
902 LogRel(("\n!!R0-Assertion Failed!!\n"
903 "Expression: %s\n"
904 "Location : %s(%d) %s\n",
905 pszExpr, pszFile, uLine, pszFunction));
906}
907
908/**
909 * Callback for RTLogFormatV which writes to the com port.
910 * See PFNLOGOUTPUT() for details.
911 */
912static DECLCALLBACK(size_t) rtLogOutput(void *pv, const char *pachChars, size_t cbChars)
913{
914 for (size_t i=0;i<cbChars;i++)
915 LogRel(("%c", pachChars[i]));
916
917 return cbChars;
918}
919
920DECLEXPORT(void) RTCALL AssertMsg2(const char *pszFormat, ...)
921{
922 PRTLOGGER pLog = RTLogDefaultInstance();
923 if (pLog)
924 {
925 va_list args;
926
927 va_start(args, pszFormat);
928 RTLogFormatV(rtLogOutput, pLog, pszFormat, args);
929 va_end(args);
930 }
931}
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