VirtualBox

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

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

GVMMR0: Don't forget to call GMM for init and cleanup on VM object destruction.

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