VirtualBox

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

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

fixed log statement.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 56.5 KB
Line 
1/* $Id: GVMMR0.cpp 6542 2008-01-28 20:26:14Z vboxsync $ */
2/** @file
3 * GVMM - Global VM Manager.
4 */
5
6/*
7 * Copyright (C) 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/** @page pg_GVMM GVMM - The Global VM Manager
20 *
21 * The Global VM Manager lives in ring-0. It's main function at the moment
22 * is to manage a list of all running VMs, keep a ring-0 only structure (GVM)
23 * for each of them, and assign them unique identifiers (so GMM can track
24 * page owners). The idea for the future is to add an idle priority kernel
25 * thread that can take care of tasks like page sharing.
26 *
27 * The GVMM will create a ring-0 object for each VM when it's registered,
28 * this is both for session cleanup purposes and for having a point where
29 * it's possible to implement usage polices later (in SUPR0ObjRegister).
30 */
31
32
33/*******************************************************************************
34* Header Files *
35*******************************************************************************/
36#define LOG_GROUP LOG_GROUP_GVMM
37#include <VBox/gvmm.h>
38#include "GVMMR0Internal.h"
39#include <VBox/gvm.h>
40#include <VBox/vm.h>
41#include <VBox/err.h>
42#include <iprt/alloc.h>
43#include <iprt/semaphore.h>
44#include <iprt/time.h>
45#include <VBox/log.h>
46#include <iprt/thread.h>
47#include <iprt/param.h>
48#include <iprt/string.h>
49#include <iprt/assert.h>
50#include <iprt/mem.h>
51#include <iprt/memobj.h>
52
53
54/*******************************************************************************
55* Structures and Typedefs *
56*******************************************************************************/
57
58/**
59 * Global VM handle.
60 */
61typedef struct GVMHANDLE
62{
63 /** The index of the next handle in the list (free or used). (0 is nil.) */
64 uint16_t volatile iNext;
65 /** Our own index / handle value. */
66 uint16_t iSelf;
67 /** The pointer to the ring-0 only (aka global) VM structure. */
68 PGVM pGVM;
69 /** The ring-0 mapping of the shared VM instance data. */
70 PVM pVM;
71 /** The virtual machine object. */
72 void *pvObj;
73 /** The session this VM is associated with. */
74 PSUPDRVSESSION pSession;
75 /** The ring-0 handle of the EMT thread.
76 * This is used for assertions and similar cases where we need to find the VM handle. */
77 RTNATIVETHREAD hEMT;
78} GVMHANDLE;
79/** Pointer to a global VM handle. */
80typedef GVMHANDLE *PGVMHANDLE;
81
82/**
83 * The GVMM instance data.
84 */
85typedef struct GVMM
86{
87 /** Eyecatcher / magic. */
88 uint32_t u32Magic;
89 /** The index of the head of the free handle chain. (0 is nil.) */
90 uint16_t volatile iFreeHead;
91 /** The index of the head of the active handle chain. (0 is nil.) */
92 uint16_t volatile iUsedHead;
93 /** The number of VMs. */
94 uint16_t volatile cVMs;
95// /** The number of halted EMT threads. */
96// uint16_t volatile cHaltedEMTs;
97 /** The lock used to serialize VM creation, destruction and associated events that
98 * isn't performance critical. Owners may acquire the list lock. */
99 RTSEMFASTMUTEX CreateDestroyLock;
100 /** The lock used to serialize used list updates and accesses.
101 * This indirectly includes scheduling since the scheduler will have to walk the
102 * used list to examin running VMs. Owners may not acquire any other locks. */
103 RTSEMFASTMUTEX UsedLock;
104 /** The handle array.
105 * The size of this array defines the maximum number of currently running VMs.
106 * The first entry is unused as it represents the NIL handle. */
107 GVMHANDLE aHandles[128];
108
109 /** @gcfgm{/GVMM/cVMsMeansCompany, 32-bit, 0, UINT32_MAX, 1}
110 * The number of VMs that means we no longer consider ourselves alone on a CPU/Core.
111 */
112 uint32_t cVMsMeansCompany;
113 /** @gcfgm{/GVMM/MinSleepAlone,32-bit, 0, 100000000, 750000, ns}
114 * The minimum sleep time for when we're alone, in nano seconds.
115 */
116 uint32_t nsMinSleepAlone;
117 /** @gcfgm{/GVMM/MinSleepCompany,32-bit,0, 100000000, 15000, ns}
118 * The minimum sleep time for when we've got company, in nano seconds.
119 */
120 uint32_t nsMinSleepCompany;
121 /** @gcfgm{/GVMM/EarlyWakeUp1, 32-bit, 0, 100000000, 25000, ns}
122 * The limit for the first round of early wakeups, given in nano seconds.
123 */
124 uint32_t nsEarlyWakeUp1;
125 /** @gcfgm{/GVMM/EarlyWakeUp2, 32-bit, 0, 100000000, 50000, ns}
126 * The limit for the second round of early wakeups, given in nano seconds.
127 */
128 uint32_t nsEarlyWakeUp2;
129} GVMM;
130/** Pointer to the GVMM instance data. */
131typedef GVMM *PGVMM;
132
133/** The GVMM::u32Magic value (Charlie Haden). */
134#define GVMM_MAGIC 0x19370806
135
136
137
138/*******************************************************************************
139* Global Variables *
140*******************************************************************************/
141/** Pointer to the GVMM instance data.
142 * (Just my general dislike for global variables.) */
143static PGVMM g_pGVMM = NULL;
144
145/** Macro for obtaining and validating the g_pGVMM pointer.
146 * On failure it will return from the invoking function with the specified return value.
147 *
148 * @param pGVMM The name of the pGVMM variable.
149 * @param rc The return value on failure. Use VERR_INTERNAL_ERROR for
150 * VBox status codes.
151 */
152#define GVMM_GET_VALID_INSTANCE(pGVMM, rc) \
153 do { \
154 (pGVMM) = g_pGVMM;\
155 AssertPtrReturn((pGVMM), (rc)); \
156 AssertMsgReturn((pGVMM)->u32Magic == GVMM_MAGIC, ("%p - %#x\n", (pGVMM), (pGVMM)->u32Magic), (rc)); \
157 } while (0)
158
159/** Macro for obtaining and validating the g_pGVMM pointer, void function variant.
160 * On failure it will return from the invoking function.
161 *
162 * @param pGVMM The name of the pGVMM variable.
163 */
164#define GVMM_GET_VALID_INSTANCE_VOID(pGVMM) \
165 do { \
166 (pGVMM) = g_pGVMM;\
167 AssertPtrReturnVoid((pGVMM)); \
168 AssertMsgReturnVoid((pGVMM)->u32Magic == GVMM_MAGIC, ("%p - %#x\n", (pGVMM), (pGVMM)->u32Magic)); \
169 } while (0)
170
171
172/*******************************************************************************
173* Internal Functions *
174*******************************************************************************/
175static void gvmmR0InitPerVMData(PGVM pGVM);
176static DECLCALLBACK(void) gvmmR0HandleObjDestructor(void *pvObj, void *pvGVMM, void *pvHandle);
177static int gvmmR0ByVM(PVM pVM, PGVM *ppGVM, PGVMM *ppGVMM, bool fTakeUsedLock);
178static int gvmmR0ByVMAndEMT(PVM pVM, PGVM *ppGVM, PGVMM *ppGVMM);
179
180
181/**
182 * Initializes the GVMM.
183 *
184 * This is called while owninng the loader sempahore (see supdrvIOCtl_LdrLoad()).
185 *
186 * @returns VBox status code.
187 */
188GVMMR0DECL(int) GVMMR0Init(void)
189{
190 LogFlow(("GVMMR0Init:\n"));
191
192 /*
193 * Allocate and initialize the instance data.
194 */
195 PGVMM pGVMM = (PGVMM)RTMemAllocZ(sizeof(*pGVMM));
196 if (!pGVMM)
197 return VERR_NO_MEMORY;
198 int rc = RTSemFastMutexCreate(&pGVMM->CreateDestroyLock);
199 if (RT_SUCCESS(rc))
200 {
201 rc = RTSemFastMutexCreate(&pGVMM->UsedLock);
202 if (RT_SUCCESS(rc))
203 {
204 pGVMM->u32Magic = GVMM_MAGIC;
205 pGVMM->iUsedHead = 0;
206 pGVMM->iFreeHead = 1;
207
208 /* the nil handle */
209 pGVMM->aHandles[0].iSelf = 0;
210 pGVMM->aHandles[0].iNext = 0;
211
212 /* the tail */
213 unsigned i = RT_ELEMENTS(pGVMM->aHandles) - 1;
214 pGVMM->aHandles[i].iSelf = i;
215 pGVMM->aHandles[i].iNext = 0; /* nil */
216
217 /* the rest */
218 while (i-- > 1)
219 {
220 pGVMM->aHandles[i].iSelf = i;
221 pGVMM->aHandles[i].iNext = i + 1;
222 }
223
224 /* The default configuration values. */
225 pGVMM->cVMsMeansCompany = 1; /** @todo should be adjusted to relative to the cpu count or something... */
226 pGVMM->nsMinSleepAlone = 750000 /* ns (0.750 ms) */; /** @todo this should be adjusted to be 75% (or something) of the scheduler granularity... */
227 pGVMM->nsMinSleepCompany = 15000 /* ns (0.015 ms) */;
228 pGVMM->nsEarlyWakeUp1 = 25000 /* ns (0.025 ms) */;
229 pGVMM->nsEarlyWakeUp2 = 50000 /* ns (0.050 ms) */;
230
231 g_pGVMM = pGVMM;
232 LogFlow(("GVMMR0Init: pGVMM=%p\n", pGVMM));
233 return VINF_SUCCESS;
234 }
235
236 RTSemFastMutexDestroy(pGVMM->CreateDestroyLock);
237 }
238
239 RTMemFree(pGVMM);
240 return rc;
241}
242
243
244/**
245 * Terminates the GVM.
246 *
247 * This is called while owning the loader semaphore (see supdrvLdrFree()).
248 * And unless something is wrong, there should be absolutely no VMs
249 * registered at this point.
250 */
251GVMMR0DECL(void) GVMMR0Term(void)
252{
253 LogFlow(("GVMMR0Term:\n"));
254
255 PGVMM pGVMM = g_pGVMM;
256 g_pGVMM = NULL;
257 if (RT_UNLIKELY(!VALID_PTR(pGVMM)))
258 {
259 SUPR0Printf("GVMMR0Term: pGVMM=%p\n", pGVMM);
260 return;
261 }
262
263 pGVMM->u32Magic++;
264
265 RTSemFastMutexDestroy(pGVMM->UsedLock);
266 pGVMM->UsedLock = NIL_RTSEMFASTMUTEX;
267 RTSemFastMutexDestroy(pGVMM->CreateDestroyLock);
268 pGVMM->CreateDestroyLock = NIL_RTSEMFASTMUTEX;
269
270 pGVMM->iFreeHead = 0;
271 if (pGVMM->iUsedHead)
272 {
273 SUPR0Printf("GVMMR0Term: iUsedHead=%#x! (cVMs=%#x)\n", pGVMM->iUsedHead, pGVMM->cVMs);
274 pGVMM->iUsedHead = 0;
275 }
276
277 RTMemFree(pGVMM);
278}
279
280
281/**
282 * A quick hack for setting global config values.
283 *
284 * @returns VBox status code.
285 *
286 * @param pSession The session handle. Used for authentication.
287 * @param pszName The variable name.
288 * @param u64Value The new value.
289 */
290GVMMR0DECL(int) GVMMR0SetConfig(PSUPDRVSESSION pSession, const char *pszName, uint64_t u64Value)
291{
292 /*
293 * Validate input.
294 */
295 PGVMM pGVMM;
296 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
297 AssertPtrReturn(pSession, VERR_INVALID_HANDLE);
298 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
299
300 /*
301 * String switch time!
302 */
303 if (strncmp(pszName, "/GVMM/", sizeof("/GVMM/") - 1))
304 return VERR_CFGM_VALUE_NOT_FOUND; /* borrow status codes from CFGM... */
305 int rc = VINF_SUCCESS;
306 pszName += sizeof("/GVMM/") - 1;
307 if (!strcmp(pszName, "cVMsMeansCompany"))
308 {
309 if (u64Value <= UINT32_MAX)
310 pGVMM->cVMsMeansCompany = u64Value;
311 else
312 rc = VERR_OUT_OF_RANGE;
313 }
314 else if (!strcmp(pszName, "MinSleepAlone"))
315 {
316 if (u64Value <= 100000000)
317 pGVMM->nsMinSleepAlone = u64Value;
318 else
319 rc = VERR_OUT_OF_RANGE;
320 }
321 else if (!strcmp(pszName, "MinSleepCompany"))
322 {
323 if (u64Value <= 100000000)
324 pGVMM->nsMinSleepCompany = u64Value;
325 else
326 rc = VERR_OUT_OF_RANGE;
327 }
328 else if (!strcmp(pszName, "EarlyWakeUp1"))
329 {
330 if (u64Value <= 100000000)
331 pGVMM->nsEarlyWakeUp1 = u64Value;
332 else
333 rc = VERR_OUT_OF_RANGE;
334 }
335 else if (!strcmp(pszName, "EarlyWakeUp2"))
336 {
337 if (u64Value <= 100000000)
338 pGVMM->nsEarlyWakeUp2 = u64Value;
339 else
340 rc = VERR_OUT_OF_RANGE;
341 }
342 else
343 rc = VERR_CFGM_VALUE_NOT_FOUND;
344 return rc;
345}
346
347
348/**
349 * A quick hack for getting global config values.
350 *
351 * @returns VBox status code.
352 *
353 * @param pSession The session handle. Used for authentication.
354 * @param pszName The variable name.
355 * @param u64Value The new value.
356 */
357GVMMR0DECL(int) GVMMR0QueryConfig(PSUPDRVSESSION pSession, const char *pszName, uint64_t *pu64Value)
358{
359 /*
360 * Validate input.
361 */
362 PGVMM pGVMM;
363 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
364 AssertPtrReturn(pSession, VERR_INVALID_HANDLE);
365 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
366 AssertPtrReturn(pu64Value, VERR_INVALID_POINTER);
367
368 /*
369 * String switch time!
370 */
371 if (strncmp(pszName, "/GVMM/", sizeof("/GVMM/") - 1))
372 return VERR_CFGM_VALUE_NOT_FOUND; /* borrow status codes from CFGM... */
373 int rc = VINF_SUCCESS;
374 pszName += sizeof("/GVMM/") - 1;
375 if (!strcmp(pszName, "cVMsMeansCompany"))
376 *pu64Value = pGVMM->cVMsMeansCompany;
377 else if (!strcmp(pszName, "MinSleepAlone"))
378 *pu64Value = pGVMM->nsMinSleepAlone;
379 else if (!strcmp(pszName, "MinSleepCompany"))
380 *pu64Value = pGVMM->nsMinSleepCompany;
381 else if (!strcmp(pszName, "EarlyWakeUp1"))
382 *pu64Value = pGVMM->nsEarlyWakeUp1;
383 else if (!strcmp(pszName, "EarlyWakeUp2"))
384 *pu64Value = pGVMM->nsEarlyWakeUp2;
385 else
386 rc = VERR_CFGM_VALUE_NOT_FOUND;
387 return rc;
388}
389
390
391/**
392 * Request wrapper for the GVMMR0CreateVM API.
393 *
394 * @returns VBox status code.
395 * @param pReq The request buffer.
396 */
397GVMMR0DECL(int) GVMMR0CreateVMReq(PGVMMCREATEVMREQ pReq)
398{
399 /*
400 * Validate the request.
401 */
402 if (!VALID_PTR(pReq))
403 return VERR_INVALID_POINTER;
404 if (pReq->Hdr.cbReq != sizeof(*pReq))
405 return VERR_INVALID_PARAMETER;
406 if (!VALID_PTR(pReq->pSession))
407 return VERR_INVALID_POINTER;
408
409 /*
410 * Execute it.
411 */
412 PVM pVM;
413 pReq->pVMR0 = NULL;
414 pReq->pVMR3 = NIL_RTR3PTR;
415 int rc = GVMMR0CreateVM(pReq->pSession, &pVM);
416 if (RT_SUCCESS(rc))
417 {
418 pReq->pVMR0 = pVM;
419 pReq->pVMR3 = pVM->pVMR3;
420 }
421 return rc;
422}
423
424
425/**
426 * Allocates the VM structure and registers it with GVM.
427 *
428 * @returns VBox status code.
429 * @param pSession The support driver session.
430 * @param ppVM Where to store the pointer to the VM structure.
431 *
432 * @thread Any thread.
433 */
434GVMMR0DECL(int) GVMMR0CreateVM(PSUPDRVSESSION pSession, PVM *ppVM)
435{
436 LogFlow(("GVMMR0CreateVM: pSession=%p\n", pSession));
437 PGVMM pGVMM;
438 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
439
440 AssertPtrReturn(ppVM, VERR_INVALID_POINTER);
441 *ppVM = NULL;
442
443 AssertReturn(RTThreadNativeSelf() != NIL_RTNATIVETHREAD, VERR_INTERNAL_ERROR);
444
445 /*
446 * The whole allocation process is protected by the lock.
447 */
448 int rc = RTSemFastMutexRequest(pGVMM->CreateDestroyLock);
449 AssertRCReturn(rc, rc);
450
451 /*
452 * Allocate a handle first so we don't waste resources unnecessarily.
453 */
454 uint16_t iHandle = pGVMM->iFreeHead;
455 if (iHandle)
456 {
457 PGVMHANDLE pHandle = &pGVMM->aHandles[iHandle];
458
459 /* consistency checks, a bit paranoid as always. */
460 if ( !pHandle->pVM
461 && !pHandle->pGVM
462 && !pHandle->pvObj
463 && pHandle->iSelf == iHandle)
464 {
465 pHandle->pvObj = SUPR0ObjRegister(pSession, SUPDRVOBJTYPE_VM, gvmmR0HandleObjDestructor, pGVMM, pHandle);
466 if (pHandle->pvObj)
467 {
468 /*
469 * Move the handle from the free to used list and perform permission checks.
470 */
471 rc = RTSemFastMutexRequest(pGVMM->UsedLock);
472 AssertRC(rc);
473
474 pGVMM->iFreeHead = pHandle->iNext;
475 pHandle->iNext = pGVMM->iUsedHead;
476 pGVMM->iUsedHead = iHandle;
477 pGVMM->cVMs++;
478
479 pHandle->pVM = NULL;
480 pHandle->pGVM = NULL;
481 pHandle->pSession = pSession;
482 pHandle->hEMT = NIL_RTNATIVETHREAD;
483
484 RTSemFastMutexRelease(pGVMM->UsedLock);
485
486 rc = SUPR0ObjVerifyAccess(pHandle->pvObj, pSession, NULL);
487 if (RT_SUCCESS(rc))
488 {
489 /*
490 * Allocate the global VM structure (GVM) and initialize it.
491 */
492 PGVM pGVM = (PGVM)RTMemAllocZ(sizeof(*pGVM));
493 if (pGVM)
494 {
495 pGVM->u32Magic = GVM_MAGIC;
496 pGVM->hSelf = iHandle;
497 pGVM->hEMT = NIL_RTNATIVETHREAD;
498 pGVM->pVM = NULL;
499
500 gvmmR0InitPerVMData(pGVM);
501 /* GMMR0InitPerVMData(pGVM); - later */
502
503 /*
504 * Allocate the shared VM structure and associated page array.
505 */
506 const size_t cPages = RT_ALIGN(sizeof(VM), PAGE_SIZE) >> PAGE_SHIFT;
507 rc = RTR0MemObjAllocLow(&pGVM->gvmm.s.VMMemObj, cPages << PAGE_SHIFT, false /* fExecutable */);
508 if (RT_SUCCESS(rc))
509 {
510 PVM pVM = (PVM)RTR0MemObjAddress(pGVM->gvmm.s.VMMemObj); AssertPtr(pVM);
511 memset(pVM, 0, cPages << PAGE_SHIFT);
512 pVM->enmVMState = VMSTATE_CREATING;
513 pVM->pVMR0 = pVM;
514 pVM->pSession = pSession;
515 pVM->hSelf = iHandle;
516
517 rc = RTR0MemObjAllocPage(&pGVM->gvmm.s.VMPagesMemObj, cPages * sizeof(SUPPAGE), false /* fExecutable */);
518 if (RT_SUCCESS(rc))
519 {
520 PSUPPAGE paPages = (PSUPPAGE)RTR0MemObjAddress(pGVM->gvmm.s.VMPagesMemObj); AssertPtr(paPages);
521 for (size_t iPage = 0; iPage < cPages; iPage++)
522 {
523 paPages[iPage].uReserved = 0;
524 paPages[iPage].Phys = RTR0MemObjGetPagePhysAddr(pGVM->gvmm.s.VMMemObj, iPage);
525 Assert(paPages[iPage].Phys != NIL_RTHCPHYS);
526 }
527
528 /*
529 * Map them into ring-3.
530 */
531 rc = RTR0MemObjMapUser(&pGVM->gvmm.s.VMMapObj, pGVM->gvmm.s.VMMemObj, (RTR3PTR)-1, 0,
532 RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
533 if (RT_SUCCESS(rc))
534 {
535 pVM->pVMR3 = RTR0MemObjAddressR3(pGVM->gvmm.s.VMMapObj);
536 AssertPtr((void *)pVM->pVMR3);
537
538 rc = RTR0MemObjMapUser(&pGVM->gvmm.s.VMPagesMapObj, pGVM->gvmm.s.VMPagesMemObj, (RTR3PTR)-1, 0,
539 RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
540 if (RT_SUCCESS(rc))
541 {
542 pVM->paVMPagesR3 = RTR0MemObjAddressR3(pGVM->gvmm.s.VMPagesMapObj);
543 AssertPtr((void *)pVM->paVMPagesR3);
544
545 /* complete the handle - take the UsedLock sem just to be careful. */
546 rc = RTSemFastMutexRequest(pGVMM->UsedLock);
547 AssertRC(rc);
548
549 pHandle->pVM = pVM;
550 pHandle->pGVM = pGVM;
551 pGVM->pVM = pVM;
552
553
554 RTSemFastMutexRelease(pGVMM->UsedLock);
555 RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
556
557 *ppVM = pVM;
558 Log(("GVMMR0CreateVM: pVM=%p pVMR3=%p pGVM=%p hGVM=%d\n", pVM, pVM->pVMR3, pGVM, iHandle));
559 return VINF_SUCCESS;
560 }
561
562 RTR0MemObjFree(pGVM->gvmm.s.VMMapObj, false /* fFreeMappings */);
563 pGVM->gvmm.s.VMMapObj = NIL_RTR0MEMOBJ;
564 }
565 RTR0MemObjFree(pGVM->gvmm.s.VMPagesMemObj, false /* fFreeMappings */);
566 pGVM->gvmm.s.VMPagesMemObj = NIL_RTR0MEMOBJ;
567 }
568 RTR0MemObjFree(pGVM->gvmm.s.VMMemObj, false /* fFreeMappings */);
569 pGVM->gvmm.s.VMMemObj = NIL_RTR0MEMOBJ;
570 }
571 }
572 }
573 /* else: The user wasn't permitted to create this VM. */
574
575 /*
576 * The handle will be freed by gvmmR0HandleObjDestructor as we release the
577 * object reference here. A little extra mess because of non-recursive lock.
578 */
579 void *pvObj = pHandle->pvObj;
580 pHandle->pvObj = NULL;
581 RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
582
583 SUPR0ObjRelease(pvObj, pSession);
584
585 SUPR0Printf("GVMMR0CreateVM: failed, rc=%d\n", rc);
586 return rc;
587 }
588
589 rc = VERR_NO_MEMORY;
590 }
591 else
592 rc = VERR_INTERNAL_ERROR;
593 }
594 else
595 rc = VERR_GVM_TOO_MANY_VMS;
596
597 RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
598 return rc;
599}
600
601
602/**
603 * Initializes the per VM data belonging to GVMM.
604 *
605 * @param pGVM Pointer to the global VM structure.
606 */
607static void gvmmR0InitPerVMData(PGVM pGVM)
608{
609 AssertCompile(RT_SIZEOFMEMB(GVM,gvmm.s) <= RT_SIZEOFMEMB(GVM,gvmm.padding));
610 Assert(RT_SIZEOFMEMB(GVM,gvmm.s) <= RT_SIZEOFMEMB(GVM,gvmm.padding));
611 pGVM->gvmm.s.VMMemObj = NIL_RTR0MEMOBJ;
612 pGVM->gvmm.s.VMMapObj = NIL_RTR0MEMOBJ;
613 pGVM->gvmm.s.VMPagesMemObj = NIL_RTR0MEMOBJ;
614 pGVM->gvmm.s.VMPagesMapObj = NIL_RTR0MEMOBJ;
615 pGVM->gvmm.s.HaltEventMulti = NIL_RTSEMEVENTMULTI;
616}
617
618
619/**
620 * Associates an EMT thread with a VM.
621 *
622 * This is called early during the ring-0 VM initialization so assertions later in
623 * the process can be handled gracefully.
624 *
625 * @returns VBox status code.
626 *
627 * @param pVM The VM instance data (aka handle), ring-0 mapping of course.
628 * @thread EMT.
629 */
630GVMMR0DECL(int) GVMMR0AssociateEMTWithVM(PVM pVM)
631{
632 LogFlow(("GVMMR0AssociateEMTWithVM: pVM=%p\n", pVM));
633 PGVMM pGVMM;
634 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
635
636 /*
637 * Validate the VM structure, state and handle.
638 */
639 AssertPtrReturn(pVM, VERR_INVALID_POINTER);
640 AssertReturn(!((uintptr_t)pVM & PAGE_OFFSET_MASK), VERR_INVALID_POINTER);
641 AssertMsgReturn(pVM->enmVMState == VMSTATE_CREATING, ("%d\n", pVM->enmVMState), VERR_WRONG_ORDER);
642
643 RTNATIVETHREAD hEMT = RTThreadNativeSelf();
644 AssertReturn(hEMT != NIL_RTNATIVETHREAD, VERR_NOT_SUPPORTED);
645
646 const uint16_t hGVM = pVM->hSelf;
647 AssertReturn(hGVM != NIL_GVM_HANDLE, VERR_INVALID_HANDLE);
648 AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), VERR_INVALID_HANDLE);
649
650 PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
651 AssertReturn(pHandle->pVM == pVM, VERR_NOT_OWNER);
652
653 /*
654 * Take the lock, validate the handle and update the structure members.
655 */
656 int rc = RTSemFastMutexRequest(pGVMM->CreateDestroyLock);
657 AssertRCReturn(rc, rc);
658 rc = RTSemFastMutexRequest(pGVMM->UsedLock);
659 AssertRC(rc);
660
661 if ( pHandle->pVM == pVM
662 && VALID_PTR(pHandle->pvObj)
663 && VALID_PTR(pHandle->pSession)
664 && VALID_PTR(pHandle->pGVM)
665 && pHandle->pGVM->u32Magic == GVM_MAGIC)
666 {
667 pHandle->hEMT = hEMT;
668 pHandle->pGVM->hEMT = hEMT;
669 }
670 else
671 rc = VERR_INTERNAL_ERROR;
672
673 RTSemFastMutexRelease(pGVMM->UsedLock);
674 RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
675 LogFlow(("GVMMR0AssociateEMTWithVM: returns %Vrc (hEMT=%RTnthrd)\n", rc, hEMT));
676 return rc;
677}
678
679
680/**
681 * Does the VM initialization.
682 *
683 * @returns VBox status code.
684 * @param pVM Pointer to the shared VM structure.
685 */
686GVMMR0DECL(int) GVMMR0InitVM(PVM pVM)
687{
688 LogFlow(("GVMMR0InitVM: pVM=%p\n", pVM));
689
690 /*
691 * Validate the VM structure, state and handle.
692 */
693 PGVM pGVM;
694 PGVMM pGVMM;
695 int rc = gvmmR0ByVMAndEMT(pVM, &pGVM, &pGVMM);
696 if (RT_SUCCESS(rc))
697 {
698 if (pGVM->gvmm.s.HaltEventMulti == NIL_RTSEMEVENTMULTI)
699 {
700 rc = RTSemEventMultiCreate(&pGVM->gvmm.s.HaltEventMulti);
701 if (RT_FAILURE(rc))
702 pGVM->gvmm.s.HaltEventMulti = NIL_RTSEMEVENTMULTI;
703 }
704 else
705 rc = VERR_WRONG_ORDER;
706 }
707
708 LogFlow(("GVMMR0InitVM: returns %Rrc\n", rc));
709 return rc;
710}
711
712
713/**
714 * Disassociates the EMT thread from a VM.
715 *
716 * This is called last in the ring-0 VM termination. After this point anyone is
717 * allowed to destroy the VM. Ideally, we should associate the VM with the thread
718 * that's going to call GVMMR0DestroyVM for optimal security, but that's impractical
719 * at present.
720 *
721 * @returns VBox status code.
722 *
723 * @param pVM The VM instance data (aka handle), ring-0 mapping of course.
724 * @thread EMT.
725 */
726GVMMR0DECL(int) GVMMR0DisassociateEMTFromVM(PVM pVM)
727{
728 LogFlow(("GVMMR0DisassociateEMTFromVM: pVM=%p\n", pVM));
729 PGVMM pGVMM;
730 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
731
732 /*
733 * Validate the VM structure, state and handle.
734 */
735 AssertPtrReturn(pVM, VERR_INVALID_POINTER);
736 AssertReturn(!((uintptr_t)pVM & PAGE_OFFSET_MASK), VERR_INVALID_POINTER);
737 AssertMsgReturn(pVM->enmVMState >= VMSTATE_CREATING && pVM->enmVMState <= VMSTATE_DESTROYING, ("%d\n", pVM->enmVMState), VERR_WRONG_ORDER);
738
739 RTNATIVETHREAD hEMT = RTThreadNativeSelf();
740 AssertReturn(hEMT != NIL_RTNATIVETHREAD, VERR_NOT_SUPPORTED);
741
742 const uint16_t hGVM = pVM->hSelf;
743 AssertReturn(hGVM != NIL_GVM_HANDLE, VERR_INVALID_HANDLE);
744 AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), VERR_INVALID_HANDLE);
745
746 PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
747 AssertReturn(pHandle->pVM == pVM, VERR_NOT_OWNER);
748
749 /*
750 * Take the lock, validate the handle and update the structure members.
751 */
752 int rc = RTSemFastMutexRequest(pGVMM->CreateDestroyLock);
753 AssertRCReturn(rc, rc);
754 rc = RTSemFastMutexRequest(pGVMM->UsedLock);
755 AssertRC(rc);
756
757 if ( VALID_PTR(pHandle->pvObj)
758 && VALID_PTR(pHandle->pSession)
759 && VALID_PTR(pHandle->pGVM)
760 && pHandle->pGVM->u32Magic == GVM_MAGIC)
761 {
762 if ( pHandle->pVM == pVM
763 && pHandle->hEMT == hEMT)
764 {
765 pHandle->hEMT = NIL_RTNATIVETHREAD;
766 pHandle->pGVM->hEMT = NIL_RTNATIVETHREAD;
767 }
768 else
769 rc = VERR_NOT_OWNER;
770 }
771 else
772 rc = VERR_INVALID_HANDLE;
773
774 RTSemFastMutexRelease(pGVMM->UsedLock);
775 RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
776 LogFlow(("GVMMR0DisassociateEMTFromVM: returns %Vrc (hEMT=%RTnthrd)\n", rc, hEMT));
777 return rc;
778}
779
780
781/**
782 * Destroys the VM, freeing all associated resources (the ring-0 ones anyway).
783 *
784 * This is call from the vmR3DestroyFinalBit and from a error path in VMR3Create,
785 * and the caller is not the EMT thread, unfortunately. For security reasons, it
786 * would've been nice if the caller was actually the EMT thread or that we somehow
787 * could've associated the calling thread with the VM up front.
788 *
789 * @returns VBox status code.
790 * @param pVM Where to store the pointer to the VM structure.
791 *
792 * @thread EMT if it's associated with the VM, otherwise any thread.
793 */
794GVMMR0DECL(int) GVMMR0DestroyVM(PVM pVM)
795{
796 LogFlow(("GVMMR0DestroyVM: pVM=%p\n", pVM));
797 PGVMM pGVMM;
798 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
799
800
801 /*
802 * Validate the VM structure, state and caller.
803 */
804 AssertPtrReturn(pVM, VERR_INVALID_POINTER);
805 AssertReturn(!((uintptr_t)pVM & PAGE_OFFSET_MASK), VERR_INVALID_POINTER);
806 AssertMsgReturn(pVM->enmVMState >= VMSTATE_CREATING && pVM->enmVMState <= VMSTATE_TERMINATED, ("%d\n", pVM->enmVMState), VERR_WRONG_ORDER);
807
808 uint32_t hGVM = pVM->hSelf;
809 AssertReturn(hGVM != NIL_GVM_HANDLE, VERR_INVALID_HANDLE);
810 AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), VERR_INVALID_HANDLE);
811
812 PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
813 AssertReturn(pHandle->pVM == pVM, VERR_NOT_OWNER);
814
815 RTNATIVETHREAD hSelf = RTThreadNativeSelf();
816 AssertReturn(pHandle->hEMT == hSelf || pHandle->hEMT == NIL_RTNATIVETHREAD, VERR_NOT_OWNER);
817
818 /*
819 * Lookup the handle and destroy the object.
820 * Since the lock isn't recursive and we'll have to leave it before dereferencing the
821 * object, we take some precautions against racing callers just in case...
822 */
823 int rc = RTSemFastMutexRequest(pGVMM->CreateDestroyLock);
824 AssertRC(rc);
825
826 /* be careful here because we might theoretically be racing someone else cleaning up. */
827 if ( pHandle->pVM == pVM
828 && ( pHandle->hEMT == hSelf
829 || pHandle->hEMT == NIL_RTNATIVETHREAD)
830 && VALID_PTR(pHandle->pvObj)
831 && VALID_PTR(pHandle->pSession)
832 && VALID_PTR(pHandle->pGVM)
833 && pHandle->pGVM->u32Magic == GVM_MAGIC)
834 {
835 void *pvObj = pHandle->pvObj;
836 pHandle->pvObj = NULL;
837 RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
838
839 SUPR0ObjRelease(pvObj, pHandle->pSession);
840 }
841 else
842 {
843 SUPR0Printf("GVMMR0DestroyVM: pHandle=%p:{.pVM=%p, hEMT=%p, .pvObj=%p} pVM=%p hSelf=%p\n",
844 pHandle, pHandle->pVM, pHandle->hEMT, pHandle->pvObj, pVM, hSelf);
845 RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
846 rc = VERR_INTERNAL_ERROR;
847 }
848
849 return rc;
850}
851
852
853/**
854 * Handle destructor.
855 *
856 * @param pvGVMM The GVM instance pointer.
857 * @param pvHandle The handle pointer.
858 */
859static DECLCALLBACK(void) gvmmR0HandleObjDestructor(void *pvObj, void *pvGVMM, void *pvHandle)
860{
861 LogFlow(("gvmmR0HandleObjDestructor: %p %p %p\n", pvObj, pvGVMM, pvHandle));
862
863 /*
864 * Some quick, paranoid, input validation.
865 */
866 PGVMHANDLE pHandle = (PGVMHANDLE)pvHandle;
867 AssertPtr(pHandle);
868 PGVMM pGVMM = (PGVMM)pvGVMM;
869 Assert(pGVMM == g_pGVMM);
870 const uint16_t iHandle = pHandle - &pGVMM->aHandles[0];
871 if ( !iHandle
872 || iHandle >= RT_ELEMENTS(pGVMM->aHandles)
873 || iHandle != pHandle->iSelf)
874 {
875 SUPR0Printf("GVM: handle %d is out of range or corrupt (iSelf=%d)!\n", iHandle, pHandle->iSelf);
876 return;
877 }
878
879 int rc = RTSemFastMutexRequest(pGVMM->CreateDestroyLock);
880 AssertRC(rc);
881 rc = RTSemFastMutexRequest(pGVMM->UsedLock);
882 AssertRC(rc);
883
884 /*
885 * This is a tad slow but a doubly linked list is too much hazzle.
886 */
887 if (RT_UNLIKELY(pHandle->iNext >= RT_ELEMENTS(pGVMM->aHandles)))
888 {
889 SUPR0Printf("GVM: used list index %d is out of range!\n", pHandle->iNext);
890 RTSemFastMutexRelease(pGVMM->UsedLock);
891 RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
892 return;
893 }
894
895 if (pGVMM->iUsedHead == iHandle)
896 pGVMM->iUsedHead = pHandle->iNext;
897 else
898 {
899 uint16_t iPrev = pGVMM->iUsedHead;
900 int c = RT_ELEMENTS(pGVMM->aHandles) + 2;
901 while (!iPrev)
902 {
903 if (RT_UNLIKELY(iPrev >= RT_ELEMENTS(pGVMM->aHandles)))
904 {
905 SUPR0Printf("GVM: used list index %d is out of range!\n");
906 RTSemFastMutexRelease(pGVMM->UsedLock);
907 RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
908 return;
909 }
910 if (RT_UNLIKELY(c-- <= 0))
911 {
912 iPrev = 0;
913 break;
914 }
915
916 if (pGVMM->aHandles[iPrev].iNext == iHandle)
917 break;
918 iPrev = pGVMM->aHandles[iPrev].iNext;
919 }
920 if (!iPrev)
921 {
922 SUPR0Printf("GVM: can't find the handle previous previous of %d!\n", pHandle->iSelf);
923 RTSemFastMutexRelease(pGVMM->UsedLock);
924 RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
925 return;
926 }
927
928 pGVMM->aHandles[iPrev].iNext = pHandle->iNext;
929 }
930 pHandle->iNext = 0;
931 pGVMM->cVMs--;
932
933 RTSemFastMutexRelease(pGVMM->UsedLock);
934
935 /*
936 * Do the global cleanup round.
937 */
938 PGVM pGVM = pHandle->pGVM;
939 if ( VALID_PTR(pGVM)
940 && pGVM->u32Magic == GVM_MAGIC)
941 {
942 /// @todo GMMR0CleanupVM(pGVM);
943
944 /*
945 * Do the GVMM cleanup - must be done last.
946 */
947 /* The VM and VM pages mappings/allocations. */
948 if (pGVM->gvmm.s.VMPagesMapObj != NIL_RTR0MEMOBJ)
949 {
950 rc = RTR0MemObjFree(pGVM->gvmm.s.VMPagesMapObj, false /* fFreeMappings */); AssertRC(rc);
951 pGVM->gvmm.s.VMPagesMapObj = NIL_RTR0MEMOBJ;
952 }
953
954 if (pGVM->gvmm.s.VMMapObj != NIL_RTR0MEMOBJ)
955 {
956 rc = RTR0MemObjFree(pGVM->gvmm.s.VMMapObj, false /* fFreeMappings */); AssertRC(rc);
957 pGVM->gvmm.s.VMMapObj = NIL_RTR0MEMOBJ;
958 }
959
960 if (pGVM->gvmm.s.VMPagesMemObj != NIL_RTR0MEMOBJ)
961 {
962 rc = RTR0MemObjFree(pGVM->gvmm.s.VMPagesMemObj, false /* fFreeMappings */); AssertRC(rc);
963 pGVM->gvmm.s.VMPagesMemObj = NIL_RTR0MEMOBJ;
964 }
965
966 if (pGVM->gvmm.s.VMMemObj != NIL_RTR0MEMOBJ)
967 {
968 rc = RTR0MemObjFree(pGVM->gvmm.s.VMMemObj, false /* fFreeMappings */); AssertRC(rc);
969 pGVM->gvmm.s.VMMemObj = NIL_RTR0MEMOBJ;
970 }
971
972 /* the GVM structure itself. */
973 pGVM->u32Magic++;
974 RTMemFree(pGVM);
975 }
976 /* else: GVMMR0CreateVM cleanup. */
977
978 /*
979 * Free the handle.
980 * Reacquire the UsedLock here to since we're updating handle fields.
981 */
982 rc = RTSemFastMutexRequest(pGVMM->UsedLock);
983 AssertRC(rc);
984
985 pHandle->iNext = pGVMM->iFreeHead;
986 pGVMM->iFreeHead = iHandle;
987 ASMAtomicXchgPtr((void * volatile *)&pHandle->pGVM, NULL);
988 ASMAtomicXchgPtr((void * volatile *)&pHandle->pVM, NULL);
989 ASMAtomicXchgPtr((void * volatile *)&pHandle->pvObj, NULL);
990 ASMAtomicXchgPtr((void * volatile *)&pHandle->pSession, NULL);
991 ASMAtomicXchgSize(&pHandle->hEMT, NIL_RTNATIVETHREAD);
992
993 RTSemFastMutexRelease(pGVMM->UsedLock);
994 RTSemFastMutexRelease(pGVMM->CreateDestroyLock);
995 LogFlow(("gvmmR0HandleObjDestructor: returns\n"));
996}
997
998
999/**
1000 * Lookup a GVM structure by its handle.
1001 *
1002 * @returns The GVM pointer on success, NULL on failure.
1003 * @param hGVM The global VM handle. Asserts on bad handle.
1004 */
1005GVMMR0DECL(PGVM) GVMMR0ByHandle(uint32_t hGVM)
1006{
1007 PGVMM pGVMM;
1008 GVMM_GET_VALID_INSTANCE(pGVMM, NULL);
1009
1010 /*
1011 * Validate.
1012 */
1013 AssertReturn(hGVM != NIL_GVM_HANDLE, NULL);
1014 AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), NULL);
1015
1016 /*
1017 * Look it up.
1018 */
1019 PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
1020 AssertPtrReturn(pHandle->pVM, NULL);
1021 AssertPtrReturn(pHandle->pvObj, NULL);
1022 PGVM pGVM = pHandle->pGVM;
1023 AssertPtrReturn(pGVM, NULL);
1024 AssertReturn(pGVM->pVM == pHandle->pVM, NULL);
1025
1026 return pHandle->pGVM;
1027}
1028
1029
1030/**
1031 * Lookup a GVM structure by the shared VM structure.
1032 *
1033 * @returns VBox status code.
1034 * @param pVM The shared VM structure (the ring-0 mapping).
1035 * @param ppGVM Where to store the GVM pointer.
1036 * @param ppGVMM Where to store the pointer to the GVMM instance data.
1037 * @param fTakeUsedLock Whether to take the used lock or not.
1038 * Be very careful if not taking the lock as it's possible that
1039 * the VM will disappear then.
1040 *
1041 * @remark This will not assert on an invalid pVM but try return sliently.
1042 */
1043static int gvmmR0ByVM(PVM pVM, PGVM *ppGVM, PGVMM *ppGVMM, bool fTakeUsedLock)
1044{
1045 PGVMM pGVMM;
1046 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
1047
1048 /*
1049 * Validate.
1050 */
1051 if (RT_UNLIKELY( !VALID_PTR(pVM)
1052 || ((uintptr_t)pVM & PAGE_OFFSET_MASK)))
1053 return VERR_INVALID_POINTER;
1054 if (RT_UNLIKELY( pVM->enmVMState < VMSTATE_CREATING
1055 || pVM->enmVMState >= VMSTATE_TERMINATED))
1056 return VERR_INVALID_POINTER;
1057
1058 uint16_t hGVM = pVM->hSelf;
1059 if (RT_UNLIKELY( hGVM == NIL_GVM_HANDLE
1060 || hGVM >= RT_ELEMENTS(pGVMM->aHandles)))
1061 return VERR_INVALID_HANDLE;
1062
1063 /*
1064 * Look it up.
1065 */
1066 PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
1067 PGVM pGVM;
1068 if (fTakeUsedLock)
1069 {
1070 int rc = RTSemFastMutexRequest(pGVMM->UsedLock);
1071 AssertRCReturn(rc, rc);
1072
1073 pGVM = pHandle->pGVM;
1074 if (RT_UNLIKELY( pHandle->pVM != pVM
1075 || !VALID_PTR(pHandle->pvObj)
1076 || !VALID_PTR(pGVM)
1077 || pGVM->pVM != pVM))
1078 {
1079 RTSemFastMutexRelease(pGVMM->UsedLock);
1080 return VERR_INVALID_HANDLE;
1081 }
1082 }
1083 else
1084 {
1085 if (RT_UNLIKELY(pHandle->pVM != pVM))
1086 return VERR_INVALID_HANDLE;
1087 if (RT_UNLIKELY(!VALID_PTR(pHandle->pvObj)))
1088 return VERR_INVALID_HANDLE;
1089
1090 pGVM = pHandle->pGVM;
1091 if (!RT_UNLIKELY(!VALID_PTR(pGVM)))
1092 return VERR_INVALID_HANDLE;
1093 if (!RT_UNLIKELY(pGVM->pVM != pVM))
1094 return VERR_INVALID_HANDLE;
1095 }
1096
1097 *ppGVM = pGVM;
1098 *ppGVMM = pGVMM;
1099 return VINF_SUCCESS;
1100}
1101
1102
1103/**
1104 * Lookup a GVM structure by the shared VM structure.
1105 *
1106 * @returns The GVM pointer on success, NULL on failure.
1107 * @param pVM The shared VM structure (the ring-0 mapping).
1108 *
1109 * @remark This will not take the 'used'-lock because it doesn't do
1110 * nesting and this function will be used from under the lock.
1111 */
1112GVMMR0DECL(PGVM) GVMMR0ByVM(PVM pVM)
1113{
1114 PGVMM pGVMM;
1115 PGVM pGVM;
1116 int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, false /* fTakeUsedLock */);
1117 if (RT_SUCCESS(rc))
1118 return pGVM;
1119 AssertRC(rc);
1120 return NULL;
1121}
1122
1123
1124/**
1125 * Lookup a GVM structure by the shared VM structure
1126 * and ensuring that the caller is the EMT thread.
1127 *
1128 * @returns VBox status code.
1129 * @param pVM The shared VM structure (the ring-0 mapping).
1130 * @param ppGVM Where to store the GVM pointer.
1131 * @param ppGVMM Where to store the pointer to the GVMM instance data.
1132 * @thread EMT
1133 *
1134 * @remark This will assert in failure paths.
1135 */
1136static int gvmmR0ByVMAndEMT(PVM pVM, PGVM *ppGVM, PGVMM *ppGVMM)
1137{
1138 PGVMM pGVMM;
1139 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
1140
1141 /*
1142 * Validate.
1143 */
1144 AssertPtrReturn(pVM, VERR_INVALID_POINTER);
1145 AssertReturn(!((uintptr_t)pVM & PAGE_OFFSET_MASK), VERR_INVALID_POINTER);
1146
1147 uint16_t hGVM = pVM->hSelf;
1148 AssertReturn(hGVM != NIL_GVM_HANDLE, VERR_INVALID_HANDLE);
1149 AssertReturn(hGVM < RT_ELEMENTS(pGVMM->aHandles), VERR_INVALID_HANDLE);
1150
1151 /*
1152 * Look it up.
1153 */
1154 PGVMHANDLE pHandle = &pGVMM->aHandles[hGVM];
1155 RTNATIVETHREAD hAllegedEMT = RTThreadNativeSelf();
1156 AssertMsgReturn(pHandle->hEMT == hAllegedEMT, ("hEMT %x hAllegedEMT %x\n", pHandle->hEMT, hAllegedEMT), VERR_NOT_OWNER);
1157 AssertReturn(pHandle->pVM == pVM, VERR_NOT_OWNER);
1158 AssertPtrReturn(pHandle->pvObj, VERR_INTERNAL_ERROR);
1159
1160 PGVM pGVM = pHandle->pGVM;
1161 AssertPtrReturn(pGVM, VERR_INTERNAL_ERROR);
1162 AssertReturn(pGVM->pVM == pVM, VERR_INTERNAL_ERROR);
1163 AssertReturn(pGVM->hEMT == hAllegedEMT, VERR_INTERNAL_ERROR);
1164
1165 *ppGVM = pGVM;
1166 *ppGVMM = pGVMM;
1167 return VINF_SUCCESS;
1168}
1169
1170
1171/**
1172 * Lookup a GVM structure by the shared VM structure
1173 * and ensuring that the caller is the EMT thread.
1174 *
1175 * @returns VBox status code.
1176 * @param pVM The shared VM structure (the ring-0 mapping).
1177 * @param ppGVM Where to store the GVM pointer.
1178 * @thread EMT
1179 */
1180GVMMR0DECL(int) GVMMR0ByVMAndEMT(PVM pVM, PGVM *ppGVM)
1181{
1182 AssertPtrReturn(ppGVM, VERR_INVALID_POINTER);
1183 PGVMM pGVMM;
1184 return gvmmR0ByVMAndEMT(pVM, ppGVM, &pGVMM);
1185}
1186
1187
1188/**
1189 * Lookup a VM by its global handle.
1190 *
1191 * @returns The VM handle on success, NULL on failure.
1192 * @param hGVM The global VM handle. Asserts on bad handle.
1193 */
1194GVMMR0DECL(PVM) GVMMR0GetVMByHandle(uint32_t hGVM)
1195{
1196 PGVM pGVM = GVMMR0ByHandle(hGVM);
1197 return pGVM ? pGVM->pVM : NULL;
1198}
1199
1200
1201/**
1202 * Looks up the VM belonging to the specified EMT thread.
1203 *
1204 * This is used by the assertion machinery in VMMR0.cpp to avoid causing
1205 * unnecessary kernel panics when the EMT thread hits an assertion. The
1206 * call may or not be an EMT thread.
1207 *
1208 * @returns The VM handle on success, NULL on failure.
1209 * @param hEMT The native thread handle of the EMT.
1210 * NIL_RTNATIVETHREAD means the current thread
1211 */
1212GVMMR0DECL(PVM) GVMMR0GetVMByEMT(RTNATIVETHREAD hEMT)
1213{
1214 /*
1215 * No Assertions here as we're usually called in a AssertMsgN or
1216 * RTAssert* context.
1217 */
1218 PGVMM pGVMM = g_pGVMM;
1219 if ( !VALID_PTR(pGVMM)
1220 || pGVMM->u32Magic != GVMM_MAGIC)
1221 return NULL;
1222
1223 if (hEMT == NIL_RTNATIVETHREAD)
1224 hEMT = RTThreadNativeSelf();
1225
1226 /*
1227 * Search the handles in a linear fashion as we don't dare take the lock (assert).
1228 */
1229 for (unsigned i = 1; i < RT_ELEMENTS(pGVMM->aHandles); i++)
1230 if ( pGVMM->aHandles[i].hEMT == hEMT
1231 && pGVMM->aHandles[i].iSelf == i
1232 && VALID_PTR(pGVMM->aHandles[i].pvObj)
1233 && VALID_PTR(pGVMM->aHandles[i].pVM))
1234 return pGVMM->aHandles[i].pVM;
1235
1236 return NULL;
1237}
1238
1239
1240/**
1241 * This is will wake up expired and soon-to-be expired VMs.
1242 *
1243 * @returns Number of VMs that has been woken up.
1244 * @param pGVMM Pointer to the GVMM instance data.
1245 * @param u64Now The current time.
1246 */
1247static unsigned gvmmR0SchedDoWakeUps(PGVMM pGVMM, uint64_t u64Now)
1248{
1249 /*
1250 * The first pass will wake up VMs which has actually expired
1251 * and look for VMs that should be woken up in the 2nd and 3rd passes.
1252 */
1253 unsigned cWoken = 0;
1254 unsigned cHalted = 0;
1255 unsigned cTodo2nd = 0;
1256 unsigned cTodo3rd = 0;
1257 for (unsigned i = pGVMM->iUsedHead;
1258 i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
1259 i = pGVMM->aHandles[i].iNext)
1260 {
1261 PGVM pCurGVM = pGVMM->aHandles[i].pGVM;
1262 if ( VALID_PTR(pCurGVM)
1263 && pCurGVM->u32Magic == GVM_MAGIC)
1264 {
1265 uint64_t u64 = pCurGVM->gvmm.s.u64HaltExpire;
1266 if (u64)
1267 {
1268 if (u64 <= u64Now)
1269 {
1270 if (ASMAtomicXchgU64(&pCurGVM->gvmm.s.u64HaltExpire, 0))
1271 {
1272 int rc = RTSemEventMultiSignal(pCurGVM->gvmm.s.HaltEventMulti);
1273 AssertRC(rc);
1274 cWoken++;
1275 }
1276 }
1277 else
1278 {
1279 cHalted++;
1280 if (u64 <= u64Now + pGVMM->nsEarlyWakeUp1)
1281 cTodo2nd++;
1282 else if (u64 <= u64Now + pGVMM->nsEarlyWakeUp2)
1283 cTodo3rd++;
1284 }
1285 }
1286 }
1287 }
1288
1289 if (cTodo2nd)
1290 {
1291 for (unsigned i = pGVMM->iUsedHead;
1292 i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
1293 i = pGVMM->aHandles[i].iNext)
1294 {
1295 PGVM pCurGVM = pGVMM->aHandles[i].pGVM;
1296 if ( VALID_PTR(pCurGVM)
1297 && pCurGVM->u32Magic == GVM_MAGIC
1298 && pCurGVM->gvmm.s.u64HaltExpire
1299 && pCurGVM->gvmm.s.u64HaltExpire <= u64Now + pGVMM->nsEarlyWakeUp1)
1300 {
1301 if (ASMAtomicXchgU64(&pCurGVM->gvmm.s.u64HaltExpire, 0))
1302 {
1303 int rc = RTSemEventMultiSignal(pCurGVM->gvmm.s.HaltEventMulti);
1304 AssertRC(rc);
1305 cWoken++;
1306 }
1307 }
1308 }
1309 }
1310
1311 if (cTodo3rd)
1312 {
1313 for (unsigned i = pGVMM->iUsedHead;
1314 i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
1315 i = pGVMM->aHandles[i].iNext)
1316 {
1317 PGVM pCurGVM = pGVMM->aHandles[i].pGVM;
1318 if ( VALID_PTR(pCurGVM)
1319 && pCurGVM->u32Magic == GVM_MAGIC
1320 && pCurGVM->gvmm.s.u64HaltExpire
1321 && pCurGVM->gvmm.s.u64HaltExpire <= u64Now + pGVMM->nsEarlyWakeUp2)
1322 {
1323 if (ASMAtomicXchgU64(&pCurGVM->gvmm.s.u64HaltExpire, 0))
1324 {
1325 int rc = RTSemEventMultiSignal(pCurGVM->gvmm.s.HaltEventMulti);
1326 AssertRC(rc);
1327 cWoken++;
1328 }
1329 }
1330 }
1331 }
1332
1333 return cWoken;
1334}
1335
1336
1337/**
1338 * Halt the EMT thread.
1339 *
1340 * @returns VINF_SUCCESS normal wakeup (timeout or kicked by other thread).
1341 * VERR_INTERRUPTED if a signal was scheduled for the thread.
1342 * @param pVM Pointer to the shared VM structure.
1343 * @param u64ExpireGipTime The time for the sleep to expire expressed as GIP time.
1344 * @thread EMT.
1345 */
1346GVMMR0DECL(int) GVMMR0SchedHalt(PVM pVM, uint64_t u64ExpireGipTime)
1347{
1348 LogFlow(("GVMMR0SchedHalt: pVM=%p\n", pVM));
1349
1350 /*
1351 * Validate the VM structure, state and handle.
1352 */
1353 PGVMM pGVMM;
1354 PGVM pGVM;
1355 int rc = gvmmR0ByVMAndEMT(pVM, &pGVM, &pGVMM);
1356 if (RT_FAILURE(rc))
1357 return rc;
1358 pGVM->gvmm.s.StatsSched.cHaltCalls++;
1359
1360 Assert(!pGVM->gvmm.s.u64HaltExpire);
1361
1362 /*
1363 * Take the UsedList semaphore, get the current time
1364 * and check if anyone needs waking up.
1365 * Interrupts must NOT be disabled at this point because we ask for GIP time!
1366 */
1367 rc = RTSemFastMutexRequest(pGVMM->UsedLock);
1368 AssertRC(rc);
1369
1370 pGVM->gvmm.s.iCpuEmt = ASMGetApicId();
1371
1372 Assert(ASMGetFlags() & X86_EFL_IF);
1373 const uint64_t u64Now = RTTimeNanoTS(); /* (GIP time) */
1374 pGVM->gvmm.s.StatsSched.cHaltWakeUps += gvmmR0SchedDoWakeUps(pGVMM, u64Now);
1375
1376 /*
1377 * Go to sleep if we must...
1378 */
1379 if ( u64Now < u64ExpireGipTime
1380 && u64ExpireGipTime - u64Now > (pGVMM->cVMs > pGVMM->cVMsMeansCompany
1381 ? pGVMM->nsMinSleepCompany
1382 : pGVMM->nsMinSleepAlone))
1383 {
1384 pGVM->gvmm.s.StatsSched.cHaltBlocking++;
1385 ASMAtomicXchgU64(&pGVM->gvmm.s.u64HaltExpire, u64ExpireGipTime);
1386 RTSemFastMutexRelease(pGVMM->UsedLock);
1387
1388 uint32_t cMillies = (u64ExpireGipTime - u64Now) / 1000000;
1389 rc = RTSemEventMultiWaitNoResume(pGVM->gvmm.s.HaltEventMulti, cMillies ? cMillies : 1);
1390 ASMAtomicXchgU64(&pGVM->gvmm.s.u64HaltExpire, 0);
1391 if (rc == VERR_TIMEOUT)
1392 {
1393 pGVM->gvmm.s.StatsSched.cHaltTimeouts++;
1394 rc = VINF_SUCCESS;
1395 }
1396 }
1397 else
1398 {
1399 pGVM->gvmm.s.StatsSched.cHaltNotBlocking++;
1400 RTSemFastMutexRelease(pGVMM->UsedLock);
1401 }
1402
1403 /* Make sure false wake up calls (gvmmR0SchedDoWakeUps) cause us to spin. */
1404 RTSemEventMultiReset(pGVM->gvmm.s.HaltEventMulti);
1405
1406 return rc;
1407}
1408
1409
1410/**
1411 * Wakes up the halted EMT thread so it can service a pending request.
1412 *
1413 * @returns VINF_SUCCESS if not yielded.
1414 * VINF_GVM_NOT_BLOCKED if the EMT thread wasn't blocked.
1415 * @param pVM Pointer to the shared VM structure.
1416 * @thread Any but EMT.
1417 */
1418GVMMR0DECL(int) GVMMR0SchedWakeUp(PVM pVM)
1419{
1420 /*
1421 * Validate input and take the UsedLock.
1422 */
1423 PGVM pGVM;
1424 PGVMM pGVMM;
1425 int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, true /* fTakeUsedLock */);
1426 if (RT_SUCCESS(rc))
1427 {
1428 pGVM->gvmm.s.StatsSched.cWakeUpCalls++;
1429
1430 /*
1431 * Signal the semaphore regardless of whether it's current blocked on it.
1432 *
1433 * The reason for this is that there is absolutely no way we can be 100%
1434 * certain that it isn't *about* go to go to sleep on it and just got
1435 * delayed a bit en route. So, we will always signal the semaphore when
1436 * the it is flagged as halted in the VMM.
1437 */
1438 if (pGVM->gvmm.s.u64HaltExpire)
1439 {
1440 rc = VINF_SUCCESS;
1441 ASMAtomicXchgU64(&pGVM->gvmm.s.u64HaltExpire, 0);
1442 }
1443 else
1444 {
1445 rc = VINF_GVM_NOT_BLOCKED;
1446 pGVM->gvmm.s.StatsSched.cWakeUpNotHalted++;
1447 }
1448
1449 int rc2 = RTSemEventMultiSignal(pGVM->gvmm.s.HaltEventMulti);
1450 AssertRC(rc2);
1451
1452 /*
1453 * While we're here, do a round of scheduling.
1454 */
1455 Assert(ASMGetFlags() & X86_EFL_IF);
1456 const uint64_t u64Now = RTTimeNanoTS(); /* (GIP time) */
1457 pGVM->gvmm.s.StatsSched.cWakeUpWakeUps += gvmmR0SchedDoWakeUps(pGVMM, u64Now);
1458
1459
1460 rc2 = RTSemFastMutexRelease(pGVMM->UsedLock);
1461 AssertRC(rc2);
1462 }
1463
1464 LogFlow(("GVMMR0SchedWakeUp: returns %Rrc\n", rc));
1465 return rc;
1466}
1467
1468
1469/**
1470 * Poll the schedule to see if someone else should get a chance to run.
1471 *
1472 * This is a bit hackish and will not work too well if the machine is
1473 * under heavy load from non-VM processes.
1474 *
1475 * @returns VINF_SUCCESS if not yielded.
1476 * VINF_GVM_YIELDED if an attempt to switch to a different VM task was made.
1477 * @param pVM Pointer to the shared VM structure.
1478 * @param u64ExpireGipTime The time for the sleep to expire expressed as GIP time.
1479 * @param fYield Whether to yield or not.
1480 * This is for when we're spinning in the halt loop.
1481 * @thread EMT.
1482 */
1483GVMMR0DECL(int) GVMMR0SchedPoll(PVM pVM, bool fYield)
1484{
1485 /*
1486 * Validate input.
1487 */
1488 PGVM pGVM;
1489 PGVMM pGVMM;
1490 int rc = gvmmR0ByVMAndEMT(pVM, &pGVM, &pGVMM);
1491 if (RT_SUCCESS(rc))
1492 {
1493 rc = RTSemFastMutexRequest(pGVMM->UsedLock);
1494 AssertRC(rc);
1495 pGVM->gvmm.s.StatsSched.cPollCalls++;
1496
1497 Assert(ASMGetFlags() & X86_EFL_IF);
1498 const uint64_t u64Now = RTTimeNanoTS(); /* (GIP time) */
1499
1500 if (!fYield)
1501 pGVM->gvmm.s.StatsSched.cPollWakeUps += gvmmR0SchedDoWakeUps(pGVMM, u64Now);
1502 else
1503 {
1504 /** @todo implement this... */
1505 rc = VERR_NOT_IMPLEMENTED;
1506 }
1507
1508 RTSemFastMutexRelease(pGVMM->UsedLock);
1509 }
1510
1511 LogFlow(("GVMMR0SchedWakeUp: returns %Rrc\n", rc));
1512 return rc;
1513}
1514
1515
1516
1517/**
1518 * Retrieves the GVMM statistics visible to the caller.
1519 *
1520 * @returns VBox status code.
1521 *
1522 * @param pStats Where to put the statistics.
1523 * @param pSession The current session.
1524 * @param pVM The VM to obtain statistics for. Optional.
1525 */
1526GVMMR0DECL(int) GVMMR0QueryStatistics(PGVMMSTATS pStats, PSUPDRVSESSION pSession, PVM pVM)
1527{
1528 LogFlow(("GVMMR0QueryStatistics: pStats=%p pSession=%p pVM=%p\n", pStats, pSession, pVM));
1529
1530 /*
1531 * Validate input.
1532 */
1533 AssertPtrReturn(pSession, VERR_INVALID_POINTER);
1534 AssertPtrReturn(pStats, VERR_INVALID_POINTER);
1535 pStats->cVMs = 0; /* (crash before taking the sem...) */
1536
1537 /*
1538 * Take the lock and get the VM statistics.
1539 */
1540 PGVMM pGVMM;
1541 if (pVM)
1542 {
1543 PGVM pGVM;
1544 int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, true /*fTakeUsedLock*/);
1545 if (RT_FAILURE(rc))
1546 return rc;
1547 pStats->SchedVM = pGVM->gvmm.s.StatsSched;
1548 }
1549 else
1550 {
1551 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
1552 memset(&pStats->SchedVM, 0, sizeof(pStats->SchedVM));
1553
1554 int rc = RTSemFastMutexRequest(pGVMM->UsedLock);
1555 AssertRCReturn(rc, rc);
1556 }
1557
1558 /*
1559 * Enumerate the VMs and add the ones visibile to the statistics.
1560 */
1561 pStats->cVMs = 0;
1562 memset(&pStats->SchedSum, 0, sizeof(pStats->SchedSum));
1563
1564 for (unsigned i = pGVMM->iUsedHead;
1565 i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
1566 i = pGVMM->aHandles[i].iNext)
1567 {
1568 PGVM pGVM = pGVMM->aHandles[i].pGVM;
1569 void *pvObj = pGVMM->aHandles[i].pvObj;
1570 if ( VALID_PTR(pvObj)
1571 && VALID_PTR(pGVM)
1572 && pGVM->u32Magic == GVM_MAGIC
1573 && RT_SUCCESS(SUPR0ObjVerifyAccess(pvObj, pSession, NULL)))
1574 {
1575 pStats->cVMs++;
1576
1577 pStats->SchedSum.cHaltCalls += pGVM->gvmm.s.StatsSched.cHaltCalls;
1578 pStats->SchedSum.cHaltBlocking += pGVM->gvmm.s.StatsSched.cHaltBlocking;
1579 pStats->SchedSum.cHaltTimeouts += pGVM->gvmm.s.StatsSched.cHaltTimeouts;
1580 pStats->SchedSum.cHaltNotBlocking += pGVM->gvmm.s.StatsSched.cHaltNotBlocking;
1581 pStats->SchedSum.cHaltWakeUps += pGVM->gvmm.s.StatsSched.cHaltWakeUps;
1582
1583 pStats->SchedSum.cWakeUpCalls += pGVM->gvmm.s.StatsSched.cWakeUpCalls;
1584 pStats->SchedSum.cWakeUpNotHalted += pGVM->gvmm.s.StatsSched.cWakeUpNotHalted;
1585 pStats->SchedSum.cWakeUpWakeUps += pGVM->gvmm.s.StatsSched.cWakeUpWakeUps;
1586
1587 pStats->SchedSum.cPollCalls += pGVM->gvmm.s.StatsSched.cPollCalls;
1588 pStats->SchedSum.cPollHalts += pGVM->gvmm.s.StatsSched.cPollHalts;
1589 pStats->SchedSum.cPollWakeUps += pGVM->gvmm.s.StatsSched.cPollWakeUps;
1590 }
1591 }
1592
1593 RTSemFastMutexRelease(pGVMM->UsedLock);
1594
1595 return VINF_SUCCESS;
1596}
1597
1598
1599/**
1600 * VMMR0 request wrapper for GVMMR0QueryStatistics.
1601 *
1602 * @returns see GVMMR0QueryStatistics.
1603 * @param pVM Pointer to the shared VM structure. Optional.
1604 * @param pReq The request packet.
1605 */
1606GVMMR0DECL(int) GVMMR0QueryStatisticsReq(PVM pVM, PGVMMQUERYSTATISTICSSREQ pReq)
1607{
1608 /*
1609 * Validate input and pass it on.
1610 */
1611 AssertPtrReturn(pReq, VERR_INVALID_POINTER);
1612 AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
1613
1614 return GVMMR0QueryStatistics(&pReq->Stats, pReq->pSession, pVM);
1615}
1616
1617
1618/**
1619 * Resets the specified GVMM statistics.
1620 *
1621 * @returns VBox status code.
1622 *
1623 * @param pStats Which statistics to reset, that is, non-zero fields indicates which to reset.
1624 * @param pSession The current session.
1625 * @param pVM The VM to reset statistics for. Optional.
1626 */
1627GVMMR0DECL(int) GVMMR0ResetStatistics(PCGVMMSTATS pStats, PSUPDRVSESSION pSession, PVM pVM)
1628{
1629 LogFlow(("GVMMR0ResetStatistics: pStats=%p pSession=%p pVM=%p\n", pStats, pSession, pVM));
1630
1631 /*
1632 * Validate input.
1633 */
1634 AssertPtrReturn(pSession, VERR_INVALID_POINTER);
1635 AssertPtrReturn(pStats, VERR_INVALID_POINTER);
1636
1637 /*
1638 * Take the lock and get the VM statistics.
1639 */
1640 PGVMM pGVMM;
1641 if (pVM)
1642 {
1643 PGVM pGVM;
1644 int rc = gvmmR0ByVM(pVM, &pGVM, &pGVMM, true /*fTakeUsedLock*/);
1645 if (RT_FAILURE(rc))
1646 return rc;
1647# define MAYBE_RESET_FIELD(field) \
1648 do { if (pStats->SchedVM. field ) { pGVM->gvmm.s.StatsSched. field = 0; } } while (0)
1649 MAYBE_RESET_FIELD(cHaltCalls);
1650 MAYBE_RESET_FIELD(cHaltBlocking);
1651 MAYBE_RESET_FIELD(cHaltTimeouts);
1652 MAYBE_RESET_FIELD(cHaltNotBlocking);
1653 MAYBE_RESET_FIELD(cHaltWakeUps);
1654 MAYBE_RESET_FIELD(cWakeUpCalls);
1655 MAYBE_RESET_FIELD(cWakeUpNotHalted);
1656 MAYBE_RESET_FIELD(cWakeUpWakeUps);
1657 MAYBE_RESET_FIELD(cPollCalls);
1658 MAYBE_RESET_FIELD(cPollHalts);
1659 MAYBE_RESET_FIELD(cPollWakeUps);
1660# undef MAYBE_RESET_FIELD
1661 }
1662 else
1663 {
1664 GVMM_GET_VALID_INSTANCE(pGVMM, VERR_INTERNAL_ERROR);
1665
1666 int rc = RTSemFastMutexRequest(pGVMM->UsedLock);
1667 AssertRCReturn(rc, rc);
1668 }
1669
1670 /*
1671 * Enumerate the VMs and add the ones visibile to the statistics.
1672 */
1673 if (ASMMemIsAll8(&pStats->SchedSum, sizeof(pStats->SchedSum), 0))
1674 {
1675 for (unsigned i = pGVMM->iUsedHead;
1676 i != NIL_GVM_HANDLE && i < RT_ELEMENTS(pGVMM->aHandles);
1677 i = pGVMM->aHandles[i].iNext)
1678 {
1679 PGVM pGVM = pGVMM->aHandles[i].pGVM;
1680 void *pvObj = pGVMM->aHandles[i].pvObj;
1681 if ( VALID_PTR(pvObj)
1682 && VALID_PTR(pGVM)
1683 && pGVM->u32Magic == GVM_MAGIC
1684 && RT_SUCCESS(SUPR0ObjVerifyAccess(pvObj, pSession, NULL)))
1685 {
1686# define MAYBE_RESET_FIELD(field) \
1687 do { if (pStats->SchedSum. field ) { pGVM->gvmm.s.StatsSched. field = 0; } } while (0)
1688 MAYBE_RESET_FIELD(cHaltCalls);
1689 MAYBE_RESET_FIELD(cHaltBlocking);
1690 MAYBE_RESET_FIELD(cHaltTimeouts);
1691 MAYBE_RESET_FIELD(cHaltNotBlocking);
1692 MAYBE_RESET_FIELD(cHaltWakeUps);
1693 MAYBE_RESET_FIELD(cWakeUpCalls);
1694 MAYBE_RESET_FIELD(cWakeUpNotHalted);
1695 MAYBE_RESET_FIELD(cWakeUpWakeUps);
1696 MAYBE_RESET_FIELD(cPollCalls);
1697 MAYBE_RESET_FIELD(cPollHalts);
1698 MAYBE_RESET_FIELD(cPollWakeUps);
1699# undef MAYBE_RESET_FIELD
1700 }
1701 }
1702 }
1703
1704 RTSemFastMutexRelease(pGVMM->UsedLock);
1705
1706 return VINF_SUCCESS;
1707}
1708
1709
1710/**
1711 * VMMR0 request wrapper for GVMMR0ResetStatistics.
1712 *
1713 * @returns see GVMMR0ResetStatistics.
1714 * @param pVM Pointer to the shared VM structure. Optional.
1715 * @param pReq The request packet.
1716 */
1717GVMMR0DECL(int) GVMMR0ResetStatisticsReq(PVM pVM, PGVMMRESETSTATISTICSSREQ pReq)
1718{
1719 /*
1720 * Validate input and pass it on.
1721 */
1722 AssertPtrReturn(pReq, VERR_INVALID_POINTER);
1723 AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
1724
1725 return GVMMR0ResetStatistics(&pReq->Stats, pReq->pSession, pVM);
1726}
1727
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