VirtualBox

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

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

Moved VMCS allocation to ring 0.

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