VirtualBox

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

Last change on this file since 25062 was 24985, checked in by vboxsync, 15 years ago

Note about missing RTMpPokeCpu implementation on some host platforms.

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