VirtualBox

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

Last change on this file since 14499 was 14499, checked in by vboxsync, 16 years ago

VMM: Don't try use the invalid pvSavedStack pointer on darwin, set it to NULL instead. (assertions on EMT would cause corruption or crash)

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