VirtualBox

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

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

Missing update for last error handling.

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