VirtualBox

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

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

Rewrote VT-x & AMD-V mode changes. Requires the MP apis in our runtime to function properly. (only tested Windows)

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