VirtualBox

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

Last change on this file since 28853 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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