VirtualBox

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

Last change on this file since 4738 was 4738, checked in by vboxsync, 18 years ago

more new phys code.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 26.4 KB
Line 
1/* $Id: VMMR0.cpp 4738 2007-09-12 16:00:54Z 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 * The Ring 0 entry point, called by the support library (SUP).
416 *
417 * @returns VBox status code.
418 * @param pVM The VM to operate on.
419 * @param uOperation Which operation to execute. (VMMR0OPERATION)
420 * @param pvArg Argument to the operation.
421 */
422VMMR0DECL(int) VMMR0Entry(PVM pVM, unsigned /* make me an enum */ uOperation, void *pvArg)
423{
424 switch (uOperation)
425 {
426 /*
427 * Switch to GC.
428 * These calls return whatever the GC returns.
429 */
430 case VMMR0_DO_RAW_RUN:
431 {
432 /* Safety precaution as VMX disables the switcher. */
433 Assert(!pVM->vmm.s.fSwitcherDisabled);
434 if (pVM->vmm.s.fSwitcherDisabled)
435 return VERR_NOT_SUPPORTED;
436
437 STAM_COUNTER_INC(&pVM->vmm.s.StatRunGC);
438 register int rc;
439 pVM->vmm.s.iLastGCRc = rc = pVM->vmm.s.pfnR0HostToGuest(pVM);
440
441#ifdef VBOX_WITH_STATISTICS
442 vmmR0RecordRC(pVM, rc);
443#endif
444
445 /*
446 * Check if there is an exit R0 action associated with the return code.
447 */
448 switch (rc)
449 {
450 /*
451 * Default - no action, just return.
452 */
453 default:
454 return rc;
455
456 /*
457 * We'll let TRPM change the stack frame so our return is different.
458 * Just keep in mind that after the call, things have changed!
459 */
460 case VINF_EM_RAW_INTERRUPT:
461 case VINF_EM_RAW_INTERRUPT_HYPER:
462 {
463#ifdef VBOX_WITHOUT_IDT_PATCHING
464 TRPMR0DispatchHostInterrupt(pVM);
465#else /* !VBOX_WITHOUT_IDT_PATCHING */
466 /*
467 * Don't trust the compiler to get this right.
468 * gcc -fomit-frame-pointer screws up big time here. This works fine in 64-bit
469 * mode too because we push the arguments on the stack in the IDT patch code.
470 */
471# if defined(__GNUC__)
472 void *pvRet = (uint8_t *)__builtin_frame_address(0) + sizeof(void *);
473# elif defined(_MSC_VER) && defined(RT_ARCH_AMD64) /** @todo check this with with VC7! */
474 void *pvRet = (uint8_t *)_AddressOfReturnAddress();
475# elif defined(RT_ARCH_X86)
476 void *pvRet = (uint8_t *)&pVM - sizeof(pVM);
477# else
478# error "huh?"
479# endif
480 if ( ((uintptr_t *)pvRet)[1] == (uintptr_t)pVM
481 && ((uintptr_t *)pvRet)[2] == (uintptr_t)uOperation
482 && ((uintptr_t *)pvRet)[3] == (uintptr_t)pvArg)
483 TRPMR0SetupInterruptDispatcherFrame(pVM, pvRet);
484 else
485 {
486# if defined(DEBUG) || defined(LOG_ENABLED)
487 static bool s_fHaveWarned = false;
488 if (!s_fHaveWarned)
489 {
490 s_fHaveWarned = true;
491 //RTLogPrintf("VMMR0.r0: The compiler can't find the stack frame!\n"); -- @todo export me!
492 RTLogComPrintf("VMMR0.r0: The compiler can't find the stack frame!\n");
493 }
494# endif
495 TRPMR0DispatchHostInterrupt(pVM);
496 }
497#endif /* !VBOX_WITHOUT_IDT_PATCHING */
498 return rc;
499 }
500 }
501 /* Won't get here! */
502 break;
503 }
504
505 /*
506 * Run guest code using the available hardware acceleration technology.
507 */
508 case VMMR0_DO_HWACC_RUN:
509 {
510 int rc;
511
512 STAM_COUNTER_INC(&pVM->vmm.s.StatRunGC);
513 rc = HWACCMR0Enable(pVM);
514 if (VBOX_SUCCESS(rc))
515 {
516#ifdef DEBUG_NO_RING0_ASSERTIONS
517 g_pVMAssert = pVM;
518#endif
519 rc = vmmR0CallHostSetJmp(&pVM->vmm.s.CallHostR0JmpBuf, HWACCMR0RunGuestCode, pVM); /* this may resume code. */
520#ifdef DEBUG_NO_RING0_ASSERTIONS
521 g_pVMAssert = 0;
522#endif
523 int rc2 = HWACCMR0Disable(pVM);
524 AssertRC(rc2);
525 }
526 pVM->vmm.s.iLastGCRc = rc;
527
528#ifdef VBOX_WITH_STATISTICS
529 vmmR0RecordRC(pVM, rc);
530#endif
531 /* No special action required for external interrupts, just return. */
532 return rc;
533 }
534
535 /*
536 * Initialize the R0 part of a VM instance.
537 */
538 case VMMR0_DO_VMMR0_INIT:
539 {
540 RTCCUINTREG fFlags = ASMIntDisableFlags();
541 int rc = VMMR0Init(pVM, (unsigned)(uintptr_t)pvArg);
542 ASMSetFlags(fFlags);
543 return rc;
544 }
545
546 /*
547 * Terminate the R0 part of a VM instance.
548 */
549 case VMMR0_DO_VMMR0_TERM:
550 {
551 RTCCUINTREG fFlags = ASMIntDisableFlags();
552 int rc = VMMR0Term(pVM);
553 ASMSetFlags(fFlags);
554 return rc;
555 }
556
557 /*
558 * Setup the hardware accelerated raw-mode session.
559 */
560 case VMMR0_DO_HWACC_SETUP_VM:
561 {
562 RTCCUINTREG fFlags = ASMIntDisableFlags();
563 int rc = HWACCMR0SetupVMX(pVM);
564 ASMSetFlags(fFlags);
565 return rc;
566 }
567
568 /*
569 * Switch to GC to execute Hypervisor function.
570 */
571 case VMMR0_DO_CALL_HYPERVISOR:
572 {
573 /* Safety precaution as VMX disables the switcher. */
574 Assert(!pVM->vmm.s.fSwitcherDisabled);
575 if (pVM->vmm.s.fSwitcherDisabled)
576 return VERR_NOT_SUPPORTED;
577
578 RTCCUINTREG fFlags = ASMIntDisableFlags();
579 int rc = pVM->vmm.s.pfnR0HostToGuest(pVM);
580 ASMSetFlags(fFlags);
581 return rc;
582 }
583
584 /*
585 * PGM wrappers.
586 */
587 case VMMR0_DO_PGM_ALLOCATE_HANDY_PAGES:
588 return PGMR0PhysAllocateHandyPages(pVM);
589
590#if 0
591 /*
592 * GMM wrappers
593 */
594 case VMMR0_DO_GMM_ALLOCATE_PAGES:
595 return GMMR0AllocatePages(pVM, ...);
596 case VMMR0_DO_GMM_FREE_PAGES:
597 return GMMR0FreePages(pVM, ...);
598 case VMMR0_DO_GMM_MAP_UNMAP_CHUNK:
599 return GMMR0FreeMapUnmapChunk(pVM, ...);
600 case VMMR0_DO_GMM_SEED_CHUNK:
601 return GMMR0SeedChunk(pVM, (RTR3PTR)pvArg);
602#endif
603
604
605
606#ifdef VBOX_WITH_INTERNAL_NETWORKING
607 /*
608 * Services.
609 */
610 case VMMR0_DO_INTNET_OPEN:
611 case VMMR0_DO_INTNET_IF_CLOSE:
612 case VMMR0_DO_INTNET_IF_GET_RING3_BUFFER:
613 case VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE:
614 case VMMR0_DO_INTNET_IF_SEND:
615 case VMMR0_DO_INTNET_IF_WAIT:
616 {
617 /*
618 * Validate arguments a bit first.
619 */
620 if (!VALID_PTR(pvArg))
621 return VERR_INVALID_POINTER;
622 if (!VALID_PTR(pVM))
623 return VERR_INVALID_POINTER;
624 if (pVM->pVMR0 != pVM)
625 return VERR_INVALID_POINTER;
626 if (!VALID_PTR(pVM->pSession))
627 return VERR_INVALID_POINTER;
628 if (!g_pIntNet)
629 return VERR_FILE_NOT_FOUND; ///@todo fix this status code!
630
631 /*
632 * Unpack the arguments and call the service.
633 */
634 switch (uOperation)
635 {
636 case VMMR0_DO_INTNET_OPEN:
637 {
638 PINTNETOPENARGS pArgs = (PINTNETOPENARGS)pvArg;
639 return INTNETR0Open(g_pIntNet, pVM->pSession, &pArgs->szNetwork[0], pArgs->cbSend, pArgs->cbRecv, pArgs->fRestrictAccess, &pArgs->hIf);
640 }
641
642 case VMMR0_DO_INTNET_IF_CLOSE:
643 {
644 PINTNETIFCLOSEARGS pArgs = (PINTNETIFCLOSEARGS)pvArg;
645 return INTNETR0IfClose(g_pIntNet, pArgs->hIf);
646 }
647
648 case VMMR0_DO_INTNET_IF_GET_RING3_BUFFER:
649 {
650 PINTNETIFGETRING3BUFFERARGS pArgs = (PINTNETIFGETRING3BUFFERARGS)pvArg;
651 return INTNETR0IfGetRing3Buffer(g_pIntNet, pArgs->hIf, &pArgs->pRing3Buf);
652 }
653
654 case VMMR0_DO_INTNET_IF_SET_PROMISCUOUS_MODE:
655 {
656 PINTNETIFSETPROMISCUOUSMODEARGS pArgs = (PINTNETIFSETPROMISCUOUSMODEARGS)pvArg;
657 return INTNETR0IfSetPromiscuousMode(g_pIntNet, pArgs->hIf, pArgs->fPromiscuous);
658 }
659
660 case VMMR0_DO_INTNET_IF_SEND:
661 {
662 PINTNETIFSENDARGS pArgs = (PINTNETIFSENDARGS)pvArg;
663 return INTNETR0IfSend(g_pIntNet, pArgs->hIf, pArgs->pvFrame, pArgs->cbFrame);
664 }
665
666 case VMMR0_DO_INTNET_IF_WAIT:
667 {
668 PINTNETIFWAITARGS pArgs = (PINTNETIFWAITARGS)pvArg;
669 return INTNETR0IfWait(g_pIntNet, pArgs->hIf, pArgs->cMillies);
670 }
671
672 default:
673 return VERR_NOT_SUPPORTED;
674 }
675 }
676#endif /* VBOX_WITH_INTERNAL_NETWORKING */
677
678 /*
679 * For profiling.
680 */
681 case VMMR0_DO_NOP:
682 return VINF_SUCCESS;
683
684 /*
685 * For testing Ring-0 APIs invoked in this environment.
686 */
687 case VMMR0_DO_TESTS:
688 /** @todo make new test */
689 return VINF_SUCCESS;
690
691
692 default:
693 /*
694 * We're returning VERR_NOT_SUPPORT here so we've got something else
695 * than -1 which the interrupt gate glue code might return.
696 */
697 Log(("operation %#x is not supported\n", uOperation));
698 return VERR_NOT_SUPPORTED;
699 }
700}
701
702
703/**
704 * Internal R0 logger worker: Flush logger.
705 *
706 * @param pLogger The logger instance to flush.
707 * @remark This function must be exported!
708 */
709VMMR0DECL(void) vmmR0LoggerFlush(PRTLOGGER pLogger)
710{
711 /*
712 * Convert the pLogger into a VM handle and 'call' back to Ring-3.
713 * (This is a bit paranoid code.)
714 */
715 PVMMR0LOGGER pR0Logger = (PVMMR0LOGGER)((uintptr_t)pLogger - RT_OFFSETOF(VMMR0LOGGER, Logger));
716 if ( !VALID_PTR(pR0Logger)
717 || !VALID_PTR(pR0Logger + 1)
718 || !VALID_PTR(pLogger)
719 || pLogger->u32Magic != RTLOGGER_MAGIC)
720 {
721 LogCom(("vmmR0LoggerFlush: pLogger=%p!\n", pLogger));
722 return;
723 }
724
725 PVM pVM = pR0Logger->pVM;
726 if ( !VALID_PTR(pVM)
727 || pVM->pVMHC != pVM)
728 {
729 LogCom(("vmmR0LoggerFlush: pVM=%p! pLogger=%p\n", pVM, pLogger));
730 return;
731 }
732
733 /*
734 * Check that the jump buffer is armed.
735 */
736#ifdef RT_ARCH_X86
737 if (!pVM->vmm.s.CallHostR0JmpBuf.eip)
738#else
739 if (!pVM->vmm.s.CallHostR0JmpBuf.rip)
740#endif
741 {
742 LogCom(("vmmR0LoggerFlush: Jump buffer isn't armed!\n"));
743 pLogger->offScratch = 0;
744 return;
745 }
746
747 VMMR0CallHost(pVM, VMMCALLHOST_VMM_LOGGER_FLUSH, 0);
748}
749
750
751void R0LogFlush(void)
752{
753 vmmR0LoggerFlush(RTLogDefaultInstance());
754}
755
756
757#ifdef DEBUG_NO_RING0_ASSERTIONS
758/**
759 * Check if we really want to hit a breakpoint.
760 * Can jump back to ring-3 when the longjmp is armed.
761 */
762DECLEXPORT(bool) RTCALL RTAssertDoBreakpoint()
763{
764 if (g_pVMAssert)
765 {
766 g_pVMAssert->vmm.s.enmCallHostOperation = VMMCALLHOST_VMM_LOGGER_FLUSH;
767 g_pVMAssert->vmm.s.u64CallHostArg = 0;
768 g_pVMAssert->vmm.s.rcCallHost = VERR_INTERNAL_ERROR;
769 int rc = vmmR0CallHostLongJmp(&g_pVMAssert->vmm.s.CallHostR0JmpBuf, VERR_INTERNAL_ERROR);
770 if (rc == VINF_SUCCESS)
771 rc = g_pVMAssert->vmm.s.rcCallHost;
772 }
773
774 return false;
775}
776#endif /* !DEBUG_NO_RING0_ASSERTIONS */
777
778
779
780# undef LOG_GROUP
781# define LOG_GROUP LOG_GROUP_EM
782
783/** Runtime assert implementation for Native Win32 Ring-0. */
784DECLEXPORT(void) RTCALL AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction)
785{
786 LogRel(("\n!!R0-Assertion Failed!!\n"
787 "Expression: %s\n"
788 "Location : %s(%d) %s\n",
789 pszExpr, pszFile, uLine, pszFunction));
790}
791
792/**
793 * Callback for RTLogFormatV which writes to the com port.
794 * See PFNLOGOUTPUT() for details.
795 */
796static DECLCALLBACK(size_t) rtLogOutput(void *pv, const char *pachChars, size_t cbChars)
797{
798 for (size_t i=0;i<cbChars;i++)
799 LogRel(("%c", pachChars[i]));
800
801 return cbChars;
802}
803
804DECLEXPORT(void) RTCALL AssertMsg2(const char *pszFormat, ...)
805{
806 PRTLOGGER pLog = RTLogDefaultInstance();
807 if (pLog)
808 {
809 va_list args;
810
811 va_start(args, pszFormat);
812 RTLogFormatV(rtLogOutput, pLog, pszFormat, args);
813 va_end(args);
814 R0LogFlush();
815 }
816}
Note: See TracBrowser for help on using the repository browser.

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